smccc.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef SMCCC_H
  7. #define SMCCC_H
  8. #include <lib/utils_def.h>
  9. #define SMCCC_VERSION_MAJOR_SHIFT U(16)
  10. #define SMCCC_VERSION_MAJOR_MASK U(0x7FFF)
  11. #define SMCCC_VERSION_MINOR_SHIFT U(0)
  12. #define SMCCC_VERSION_MINOR_MASK U(0xFFFF)
  13. #define MAKE_SMCCC_VERSION(_major, _minor) \
  14. ((((uint32_t)(_major) & SMCCC_VERSION_MAJOR_MASK) << \
  15. SMCCC_VERSION_MAJOR_SHIFT) \
  16. | (((uint32_t)(_minor) & SMCCC_VERSION_MINOR_MASK) << \
  17. SMCCC_VERSION_MINOR_SHIFT))
  18. #define SMCCC_MAJOR_VERSION U(1)
  19. #define SMCCC_MINOR_VERSION U(5)
  20. /*******************************************************************************
  21. * Bit definitions inside the function id as per the SMC calling convention
  22. ******************************************************************************/
  23. #define FUNCID_TYPE_SHIFT U(31)
  24. #define FUNCID_TYPE_MASK U(0x1)
  25. #define FUNCID_TYPE_WIDTH U(1)
  26. #define FUNCID_CC_SHIFT U(30)
  27. #define FUNCID_CC_MASK U(0x1)
  28. #define FUNCID_CC_WIDTH U(1)
  29. #define FUNCID_OEN_SHIFT U(24)
  30. #define FUNCID_OEN_MASK U(0x3f)
  31. #define FUNCID_OEN_WIDTH U(6)
  32. #define FUNCID_FC_RESERVED_SHIFT U(17)
  33. #define FUNCID_FC_RESERVED_MASK U(0x7f)
  34. #define FUNCID_FC_RESERVED_WIDTH U(7)
  35. #define FUNCID_SVE_HINT_SHIFT U(16)
  36. #define FUNCID_SVE_HINT_MASK U(1)
  37. #define FUNCID_SVE_HINT_WIDTH U(1)
  38. #define FUNCID_NUM_SHIFT U(0)
  39. #define FUNCID_NUM_MASK U(0xffff)
  40. #define FUNCID_NUM_WIDTH U(16)
  41. #define FUNCID_MASK U(0xffffffff)
  42. #define GET_SMC_NUM(id) (((id) >> FUNCID_NUM_SHIFT) & \
  43. FUNCID_NUM_MASK)
  44. #define GET_SMC_TYPE(id) (((id) >> FUNCID_TYPE_SHIFT) & \
  45. FUNCID_TYPE_MASK)
  46. #define GET_SMC_CC(id) (((id) >> FUNCID_CC_SHIFT) & \
  47. FUNCID_CC_MASK)
  48. #define GET_SMC_OEN(id) (((id) >> FUNCID_OEN_SHIFT) & \
  49. FUNCID_OEN_MASK)
  50. /*******************************************************************************
  51. * SMCCC_ARCH_SOC_ID SoC version & revision bit definition
  52. ******************************************************************************/
  53. #define SOC_ID_JEP_106_BANK_IDX_MASK GENMASK_32(30, 24)
  54. #define SOC_ID_JEP_106_BANK_IDX_SHIFT U(24)
  55. #define SOC_ID_JEP_106_ID_CODE_MASK GENMASK_32(23, 16)
  56. #define SOC_ID_JEP_106_ID_CODE_SHIFT U(16)
  57. #define SOC_ID_IMPL_DEF_MASK GENMASK_32(15, 0)
  58. #define SOC_ID_IMPL_DEF_SHIFT U(0)
  59. #define SOC_ID_SET_JEP_106(bkid, mfid) ((((bkid) << SOC_ID_JEP_106_BANK_IDX_SHIFT) & \
  60. SOC_ID_JEP_106_BANK_IDX_MASK) | \
  61. (((mfid) << SOC_ID_JEP_106_ID_CODE_SHIFT) & \
  62. SOC_ID_JEP_106_ID_CODE_MASK))
  63. #define SOC_ID_REV_MASK GENMASK_32(30, 0)
  64. #define SOC_ID_REV_SHIFT U(0)
  65. /*******************************************************************************
  66. * Owning entity number definitions inside the function id as per the SMC
  67. * calling convention
  68. ******************************************************************************/
  69. #define OEN_ARM_START U(0)
  70. #define OEN_ARM_END U(0)
  71. #define OEN_CPU_START U(1)
  72. #define OEN_CPU_END U(1)
  73. #define OEN_SIP_START U(2)
  74. #define OEN_SIP_END U(2)
  75. #define OEN_OEM_START U(3)
  76. #define OEN_OEM_END U(3)
  77. #define OEN_STD_START U(4) /* Standard Service Calls */
  78. #define OEN_STD_END U(4)
  79. #define OEN_STD_HYP_START U(5) /* Standard Hypervisor Service calls */
  80. #define OEN_STD_HYP_END U(5)
  81. #define OEN_VEN_HYP_START U(6) /* Vendor Hypervisor Service calls */
  82. #define OEN_VEN_HYP_END U(6)
  83. #define OEN_VEN_EL3_START U(7) /* Vendor Specific EL3 Monitor Calls */
  84. #define OEN_VEN_EL3_END U(7)
  85. #define OEN_TAP_START U(48) /* Trusted Applications */
  86. #define OEN_TAP_END U(49)
  87. #define OEN_TOS_START U(50) /* Trusted OS */
  88. #define OEN_TOS_END U(63)
  89. #define OEN_LIMIT U(64)
  90. /* Flags and error codes */
  91. #define SMC_64 U(1)
  92. #define SMC_32 U(0)
  93. #define SMC_TYPE_FAST UL(1)
  94. #define SMC_TYPE_YIELD UL(0)
  95. #define SMC_OK ULL(0)
  96. #define SMC_UNK -1
  97. #define SMC_PREEMPTED -2 /* Not defined by the SMCCC */
  98. #define SMC_DENIED -3 /* Not defined by the SMCCC */
  99. #define SMC_INVALID_PARAM -4 /* Not defined by the SMCCC */
  100. /* Return codes for Arm Architecture Service SMC calls */
  101. #define SMC_ARCH_CALL_SUCCESS 0
  102. #define SMC_ARCH_CALL_NOT_SUPPORTED -1
  103. #define SMC_ARCH_CALL_NOT_REQUIRED -2
  104. #define SMC_ARCH_CALL_INVAL_PARAM -3
  105. /*
  106. * Various flags passed to SMC handlers
  107. *
  108. * Bit 5 and bit 0 of the flag are used to
  109. * determine the source security state as
  110. * follows:
  111. * ---------------------------------
  112. * Bit 5 | Bit 0 | Security state
  113. * ---------------------------------
  114. * 0 0 SMC_FROM_SECURE
  115. * 0 1 SMC_FROM_NON_SECURE
  116. * 1 1 SMC_FROM_REALM
  117. *
  118. * Bit 16 of flags records the caller's SMC
  119. * SVE hint bit according to SMCCCv1.3.
  120. * It can be consumed by dispatchers using
  121. * is_sve_hint_set macro.
  122. *
  123. */
  124. #define SMC_FROM_SECURE (U(0) << 0)
  125. #define SMC_FROM_NON_SECURE (U(1) << 0)
  126. #define SMC_FROM_REALM U(0x21)
  127. #define SMC_FROM_MASK U(0x21)
  128. #ifndef __ASSEMBLER__
  129. #include <stdint.h>
  130. #include <lib/cassert.h>
  131. #if ENABLE_RME
  132. #define is_caller_non_secure(_f) (((_f) & SMC_FROM_MASK) \
  133. == SMC_FROM_NON_SECURE)
  134. #define is_caller_secure(_f) (((_f) & SMC_FROM_MASK) \
  135. == SMC_FROM_SECURE)
  136. #define is_caller_realm(_f) (((_f) & SMC_FROM_MASK) \
  137. == SMC_FROM_REALM)
  138. #define caller_sec_state(_f) ((_f) & SMC_FROM_MASK)
  139. #else /* ENABLE_RME */
  140. #define is_caller_non_secure(_f) (((_f) & SMC_FROM_NON_SECURE) != U(0))
  141. #define is_caller_secure(_f) (!is_caller_non_secure(_f))
  142. #endif /* ENABLE_RME */
  143. #define is_sve_hint_set(_f) (((_f) & (FUNCID_SVE_HINT_MASK \
  144. << FUNCID_SVE_HINT_SHIFT)) != U(0))
  145. /* The macro below is used to identify a Standard Service SMC call */
  146. #define is_std_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_STD_START)
  147. /* The macro below is used to identify a Arm Architectural Service SMC call */
  148. #define is_arm_arch_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_ARM_START)
  149. /* The macro below is used to identify a valid Fast SMC call */
  150. #define is_valid_fast_smc(_fid) ((!(((_fid) >> 16) & U(0xff))) && \
  151. (GET_SMC_TYPE(_fid) \
  152. == (uint32_t)SMC_TYPE_FAST))
  153. /*
  154. * Macro to define UUID for services. Apart from defining and initializing a
  155. * uuid_t structure, this macro verifies that the first word of the defined UUID
  156. * does not equal SMC_UNK. This is to ensure that the caller won't mistake the
  157. * returned UUID in x0 for an invalid SMC error return
  158. */
  159. #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch, \
  160. _n0, _n1, _n2, _n3, _n4, _n5) \
  161. CASSERT((uint32_t)(_tl) != (uint32_t)SMC_UNK, \
  162. invalid_svc_uuid_##_name); \
  163. static const uuid_t _name = { \
  164. {((_tl) >> 24) & 0xFF, \
  165. ((_tl) >> 16) & 0xFF, \
  166. ((_tl) >> 8) & 0xFF, \
  167. ((_tl) & 0xFF)}, \
  168. {((_tm) >> 8) & 0xFF, \
  169. ((_tm) & 0xFF)}, \
  170. {((_th) >> 8) & 0xFF, \
  171. ((_th) & 0xFF)}, \
  172. (_cl), (_ch), \
  173. { (_n0), (_n1), (_n2), (_n3), (_n4), (_n5) } \
  174. }
  175. /*
  176. * Return a UUID in the SMC return registers.
  177. *
  178. * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single
  179. * 128-bit value using the SMC32 calling convention. This value is mapped to
  180. * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example
  181. * shall hold bytes 0 to 3, with byte 0 in the low-order bits.
  182. */
  183. static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
  184. {
  185. return ((uint32_t) b0) | (((uint32_t) b1) << 8) |
  186. (((uint32_t) b2) << 16) | (((uint32_t) b3) << 24);
  187. }
  188. #define SMC_UUID_RET(_h, _uuid) \
  189. SMC_RET4(handle, \
  190. smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1], \
  191. (_uuid).time_low[2], (_uuid).time_low[3]), \
  192. smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1], \
  193. (_uuid).time_hi_and_version[0], \
  194. (_uuid).time_hi_and_version[1]), \
  195. smc_uuid_word((_uuid).clock_seq_hi_and_reserved, \
  196. (_uuid).clock_seq_low, (_uuid).node[0], \
  197. (_uuid).node[1]), \
  198. smc_uuid_word((_uuid).node[2], (_uuid).node[3], \
  199. (_uuid).node[4], (_uuid).node[5]))
  200. #endif /*__ASSEMBLER__*/
  201. #endif /* SMCCC_H */