runtime_svc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef RUNTIME_SVC_H
  7. #define RUNTIME_SVC_H
  8. #include <common/bl_common.h> /* to include exception types */
  9. #include <lib/cassert.h>
  10. #include <lib/utils_def.h>
  11. #include <smccc_helpers.h> /* to include SMCCC definitions */
  12. /*******************************************************************************
  13. * Structure definition, typedefs & constants for the runtime service framework
  14. ******************************************************************************/
  15. /*
  16. * Constants to allow the assembler access a runtime service
  17. * descriptor
  18. */
  19. #ifdef __aarch64__
  20. #define RT_SVC_SIZE_LOG2 U(5)
  21. #define RT_SVC_DESC_INIT U(16)
  22. #define RT_SVC_DESC_HANDLE U(24)
  23. #else
  24. #define RT_SVC_SIZE_LOG2 U(4)
  25. #define RT_SVC_DESC_INIT U(8)
  26. #define RT_SVC_DESC_HANDLE U(12)
  27. #endif /* __aarch64__ */
  28. #define SIZEOF_RT_SVC_DESC (U(1) << RT_SVC_SIZE_LOG2)
  29. /*
  30. * In SMCCC 1.X, the function identifier has 6 bits for the owning entity number
  31. * and a single bit for the type of smc call. When taken together, those values
  32. * limit the maximum number of runtime services to 128.
  33. */
  34. #define MAX_RT_SVCS U(128)
  35. #ifndef __ASSEMBLER__
  36. /* Prototype for runtime service initializing function */
  37. typedef int32_t (*rt_svc_init_t)(void);
  38. /*
  39. * Prototype for runtime service SMC handler function. x0 (SMC Function ID) to
  40. * x4 are as passed by the caller. Rest of the arguments to SMC and the context
  41. * can be accessed using the handle pointer. The cookie parameter is reserved
  42. * for future use
  43. */
  44. typedef uintptr_t (*rt_svc_handle_t)(uint32_t smc_fid,
  45. u_register_t x1,
  46. u_register_t x2,
  47. u_register_t x3,
  48. u_register_t x4,
  49. void *cookie,
  50. void *handle,
  51. u_register_t flags);
  52. typedef struct rt_svc_desc {
  53. uint8_t start_oen;
  54. uint8_t end_oen;
  55. uint8_t call_type;
  56. const char *name;
  57. rt_svc_init_t init;
  58. rt_svc_handle_t handle;
  59. } rt_svc_desc_t;
  60. /*
  61. * Convenience macros to declare a service descriptor
  62. */
  63. #define DECLARE_RT_SVC(_name, _start, _end, _type, _setup, _smch) \
  64. static const rt_svc_desc_t __svc_desc_ ## _name \
  65. __section(".rt_svc_descs") __used = { \
  66. .start_oen = (_start), \
  67. .end_oen = (_end), \
  68. .call_type = (_type), \
  69. .name = #_name, \
  70. .init = (_setup), \
  71. .handle = (_smch) \
  72. }
  73. /*
  74. * Compile time assertions related to the 'rt_svc_desc' structure to:
  75. * 1. ensure that the assembler and the compiler view of the size
  76. * of the structure are the same.
  77. * 2. ensure that the assembler and the compiler see the initialisation
  78. * routine at the same offset.
  79. * 3. ensure that the assembler and the compiler see the handler
  80. * routine at the same offset.
  81. */
  82. CASSERT((sizeof(rt_svc_desc_t) == SIZEOF_RT_SVC_DESC),
  83. assert_sizeof_rt_svc_desc_mismatch);
  84. CASSERT(RT_SVC_DESC_INIT == __builtin_offsetof(rt_svc_desc_t, init),
  85. assert_rt_svc_desc_init_offset_mismatch);
  86. CASSERT(RT_SVC_DESC_HANDLE == __builtin_offsetof(rt_svc_desc_t, handle),
  87. assert_rt_svc_desc_handle_offset_mismatch);
  88. /*
  89. * This function combines the call type and the owning entity number
  90. * corresponding to a runtime service to generate a unique owning entity number.
  91. * This unique oen is used to access an entry in the 'rt_svc_descs_indices'
  92. * array. The entry contains the index of the service descriptor in the
  93. * 'rt_svc_descs' array.
  94. */
  95. static inline uint32_t get_unique_oen(uint32_t oen, uint32_t call_type)
  96. {
  97. return ((call_type & FUNCID_TYPE_MASK) << FUNCID_OEN_WIDTH) |
  98. (oen & FUNCID_OEN_MASK);
  99. }
  100. /*
  101. * This function generates the unique owning entity number from the SMC Function
  102. * ID. This unique oen is used to access an entry in the 'rt_svc_descs_indices'
  103. * array to invoke the corresponding runtime service handler during SMC
  104. * handling.
  105. */
  106. static inline uint32_t get_unique_oen_from_smc_fid(uint32_t fid)
  107. {
  108. return get_unique_oen(GET_SMC_OEN(fid), GET_SMC_TYPE(fid));
  109. }
  110. /*******************************************************************************
  111. * Function & variable prototypes
  112. ******************************************************************************/
  113. void runtime_svc_init(void);
  114. uintptr_t handle_runtime_svc(uint32_t smc_fid, void *cookie, void *handle,
  115. unsigned int flags);
  116. IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_START__, RT_SVC_DESCS_START);
  117. IMPORT_SYM(uintptr_t, __RT_SVC_DESCS_END__, RT_SVC_DESCS_END);
  118. void init_crash_reporting(void);
  119. extern uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
  120. #endif /*__ASSEMBLER__*/
  121. #endif /* RUNTIME_SVC_H */