mtk_smc_handlers.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2022-2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #if MTK_SIP_KERNEL_BOOT_ENABLE
  9. #include <cold_boot.h>
  10. #endif
  11. #include <common/debug.h>
  12. #include <common/runtime_svc.h>
  13. #include <lib/mtk_init/mtk_init.h>
  14. #include <mtk_sip_svc.h>
  15. #define SMC_HANDLER_DEBUG(...) VERBOSE(__VA_ARGS__)
  16. #define SMC_HANDLER_DEBUG_NOT_IMP_MSG "%s[0x%x] smc handler not implemented\n"
  17. #define SMC_HANDLER_DEBUG_START_MSG "%s[0x%x] smc handler start, smc desc. index:%d\n"
  18. #define SMC_HANDLER_DEBUG_END_MSG "%s[0x%x] smc handler end\n"
  19. /*
  20. * These macros below are used to identify SIP calls from Kernel,
  21. * Hypervisor, or 2ndBootloader
  22. */
  23. #define SIP_FID_ORI_MASK (0xc000)
  24. #define SIP_FID_ORI_SHIFT (14)
  25. #define SIP_FID_KERNEL (0x0)
  26. #define SIP_FID_KERNEL_VIA_GZ (0x1)
  27. #define SIP_FID_GZ (0x2)
  28. #define GET_SMC_ORI(_fid) (((_fid) & SIP_FID_ORI_MASK) >> SIP_FID_ORI_SHIFT)
  29. #define GET_SMC_ORI_NUM(_fid) ((_fid) & ~(SIP_FID_ORI_MASK))
  30. #define is_from_nsel2(_ori) (_ori == SIP_FID_GZ)
  31. #define is_from_bl33(_ori) \
  32. ((_ori != SIP_FID_GZ) && (is_el1_2nd_bootloader() == 1))
  33. #define is_from_nsel1(_ori) \
  34. (((_ori == SIP_FID_KERNEL) || \
  35. (_ori == SIP_FID_KERNEL_VIA_GZ)) && \
  36. (is_el1_2nd_bootloader() == 0))
  37. #define is_smc_forbidden(_ori) (_ori == SIP_FID_KERNEL_VIA_GZ)
  38. #define MASK_32_BIT (0xffffffffU)
  39. #define SMC_ID_EXPAND_AS_SMC_OPERATION(_smc_id, _smc_num) \
  40. case _smc_id##_AARCH32: \
  41. { \
  42. x1 = x1 & MASK_32_BIT; \
  43. x2 = x2 & MASK_32_BIT; \
  44. x3 = x3 & MASK_32_BIT; \
  45. x4 = x4 & MASK_32_BIT; \
  46. } \
  47. /* fallthrough */ \
  48. case _smc_id##_AARCH64: \
  49. { \
  50. if (_smc_id##_descriptor_index < 0) { \
  51. SMC_HANDLER_DEBUG(SMC_HANDLER_DEBUG_NOT_IMP_MSG, #_smc_id, smc_id); \
  52. break; \
  53. } \
  54. if (_smc_id##_descriptor_index >= smc_id_descriptor_max) { \
  55. SMC_HANDLER_DEBUG("smc descriptor index[%d] exceed max[%d]\n", \
  56. _smc_id##_descriptor_index, smc_id_descriptor_max); \
  57. break; \
  58. } \
  59. SMC_HANDLER_DEBUG(SMC_HANDLER_DEBUG_START_MSG, #_smc_id, smc_id, \
  60. _smc_id##_descriptor_index); \
  61. ret = smc_handler_pool[_smc_id##_descriptor_index].smc_handler(x1,\
  62. x2, x3, x4, handle, &smc_ret); \
  63. SMC_HANDLER_DEBUG(SMC_HANDLER_DEBUG_END_MSG, #_smc_id, smc_id); \
  64. break; \
  65. }
  66. #define SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX(_smc_id, _smc_num) \
  67. short _smc_id##_descriptor_index __section(".mtk_plat_ro") = -1;
  68. MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX);
  69. MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX);
  70. MTK_SIP_SMC_FROM_S_EL1_TABLE(SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX);
  71. IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_START__, MTK_SMC_POOL_START);
  72. IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_END_UNALIGNED__, MTK_SMC_POOL_END_UNALIGNED);
  73. static const struct smc_descriptor *smc_handler_pool;
  74. static short smc_id_descriptor_max;
  75. #if !MTK_SIP_KERNEL_BOOT_ENABLE
  76. /*
  77. * If there is no SMC request needs to be served in 2nd bootloader,
  78. * disable the service path inherently.
  79. */
  80. bool is_el1_2nd_bootloader(void)
  81. {
  82. return false;
  83. }
  84. #endif
  85. static void print_smc_descriptor(const struct smc_descriptor pool[])
  86. {
  87. const struct smc_descriptor *p_smc_desc;
  88. VERBOSE("print smc descriptor pool\n");
  89. for (p_smc_desc = &pool[0];
  90. (char *)p_smc_desc < (char *)MTK_SMC_POOL_END_UNALIGNED;
  91. p_smc_desc++) {
  92. VERBOSE("descriptor name:%s\n", p_smc_desc->smc_name);
  93. VERBOSE("descriptor index:%d\n", *p_smc_desc->smc_descriptor_index);
  94. VERBOSE("smc id 32:0x%x, smc id 64:0x%x\n",
  95. p_smc_desc->smc_id_aarch32, p_smc_desc->smc_id_aarch64);
  96. }
  97. }
  98. static int mtk_smc_handler_init(void)
  99. {
  100. const struct smc_descriptor *iter;
  101. short index_cnt;
  102. int ret = 0;
  103. smc_handler_pool = (const struct smc_descriptor *)MTK_SMC_POOL_START;
  104. /* Designate descriptor index point to smc_handler_pool */
  105. for (index_cnt = 0, iter = &smc_handler_pool[0];
  106. (char *)iter < (char *)MTK_SMC_POOL_END_UNALIGNED;
  107. iter++, index_cnt++) {
  108. if (index_cnt < 0) {
  109. SMC_HANDLER_DEBUG("smc handler pool index overflow!\n");
  110. ret = -EPERM;
  111. assert(0);
  112. break;
  113. }
  114. *(iter->smc_descriptor_index) = index_cnt;
  115. }
  116. smc_id_descriptor_max = index_cnt;
  117. print_smc_descriptor(smc_handler_pool);
  118. return ret;
  119. }
  120. MTK_EARLY_PLAT_INIT(mtk_smc_handler_init);
  121. /* This function handles Mediatek defined SiP Calls from Secure world */
  122. static u_register_t mtk_smc_handler_sel1(uint32_t smc_id,
  123. u_register_t x1,
  124. u_register_t x2,
  125. u_register_t x3,
  126. u_register_t x4,
  127. void *cookie,
  128. void *handle,
  129. u_register_t flags)
  130. {
  131. u_register_t ret = MTK_SIP_E_SUCCESS;
  132. struct smccc_res smc_ret = {0};
  133. switch (smc_id) {
  134. MTK_SIP_SMC_FROM_S_EL1_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
  135. default:
  136. INFO("SEL1 SMC ID:0x%x not support\n", smc_id);
  137. ret = SMC_UNK;
  138. }
  139. SMC_RET4(handle, ret, smc_ret.a1, smc_ret.a2, smc_ret.a3);
  140. }
  141. /* This function handles Mediatek defined SiP Calls from Bootloader */
  142. static uintptr_t mtk_smc_handler_bl33(uint32_t smc_id,
  143. u_register_t x1,
  144. u_register_t x2,
  145. u_register_t x3,
  146. u_register_t x4,
  147. void *cookie,
  148. void *handle,
  149. u_register_t flags)
  150. {
  151. uintptr_t ret = MTK_SIP_E_SUCCESS;
  152. struct smccc_res smc_ret = {0};
  153. switch (smc_id) {
  154. MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
  155. default:
  156. INFO("BL33 SMC ID:0x%x not supported\n", smc_id);
  157. ret = SMC_UNK;
  158. break;
  159. }
  160. SMC_RET4(handle, ret, smc_ret.a1, smc_ret.a2, smc_ret.a3);
  161. }
  162. /* This function handles Mediatek defined SiP Calls from Kernel */
  163. static uintptr_t mtk_smc_handler_nsel1(uint32_t smc_id,
  164. u_register_t x1,
  165. u_register_t x2,
  166. u_register_t x3,
  167. u_register_t x4,
  168. void *cookie,
  169. void *handle,
  170. u_register_t flags)
  171. {
  172. uintptr_t ret = MTK_SIP_E_SUCCESS;
  173. struct smccc_res smc_ret = {0};
  174. switch (smc_id) {
  175. MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
  176. default:
  177. INFO("NSEL1 SMC ID:0x%x not supported\n", smc_id);
  178. ret = SMC_UNK;
  179. break;
  180. }
  181. SMC_RET4(handle, ret, smc_ret.a1, smc_ret.a2, smc_ret.a3);
  182. }
  183. static uintptr_t mtk_smc_handler(uint32_t smc_id,
  184. u_register_t x1,
  185. u_register_t x2,
  186. u_register_t x3,
  187. u_register_t x4,
  188. void *cookie,
  189. void *handle,
  190. u_register_t flags)
  191. {
  192. uintptr_t ret = SMC_UNK;
  193. uint32_t ns;
  194. uint32_t smc_ori;
  195. uint32_t smc_num;
  196. /* Get SMC Originator bit 14.15 */
  197. smc_ori = GET_SMC_ORI(smc_id);
  198. /* Get SMC Number. Clean bit 14.15 */
  199. smc_num = GET_SMC_ORI_NUM(smc_id);
  200. /* Determine which security state this SMC originated from */
  201. ns = is_caller_non_secure(flags);
  202. if (ns && is_smc_forbidden(smc_ori)) {
  203. ERROR("%s: Forbidden SMC call (0x%x)\n", __func__, smc_id);
  204. SMC_RET1(handle, ret);
  205. }
  206. if (!ns) {
  207. /* SiP SMC service secure world's call */
  208. return mtk_smc_handler_sel1(smc_num, x1, x2, x3, x4,
  209. cookie, handle, flags);
  210. }
  211. if (is_from_bl33(smc_ori)) {
  212. /* SiP SMC service secure bootloader's call */
  213. return mtk_smc_handler_bl33(smc_num, x1, x2, x3, x4,
  214. cookie, handle, flags);
  215. } else if (is_from_nsel1(smc_ori)) {
  216. /* SiP SMC service kernel's call */
  217. return mtk_smc_handler_nsel1(smc_num, x1, x2, x3, x4,
  218. cookie, handle, flags);
  219. }
  220. INFO("SMC ID:0x%x not supported\n", smc_id);
  221. SMC_RET1(handle, ret);
  222. }
  223. /* Define a runtime service descriptor for fast SMC calls */
  224. DECLARE_RT_SVC(
  225. mtk_smc_handler,
  226. OEN_SIP_START,
  227. OEN_SIP_END,
  228. SMC_TYPE_FAST,
  229. NULL,
  230. mtk_smc_handler
  231. );