mtk_smc_handlers.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2022, 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. IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_START__, MTK_SMC_POOL_START);
  71. IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_END_UNALIGNED__, MTK_SMC_POOL_END_UNALIGNED);
  72. static const struct smc_descriptor *smc_handler_pool;
  73. static short smc_id_descriptor_max;
  74. #if !MTK_SIP_KERNEL_BOOT_ENABLE
  75. /*
  76. * If there is no SMC request needs to be served in 2nd bootloader,
  77. * disable the service path inherently.
  78. */
  79. bool is_el1_2nd_bootloader(void)
  80. {
  81. return false;
  82. }
  83. #endif
  84. static void print_smc_descriptor(const struct smc_descriptor pool[])
  85. {
  86. const struct smc_descriptor *p_smc_desc;
  87. INFO("print smc descriptor pool\n");
  88. for (p_smc_desc = &pool[0];
  89. (char *)p_smc_desc < (char *)MTK_SMC_POOL_END_UNALIGNED;
  90. p_smc_desc++) {
  91. INFO("descriptor name:%s\n", p_smc_desc->smc_name);
  92. INFO("descriptor index:%d\n", *p_smc_desc->smc_descriptor_index);
  93. INFO("smc id 32:0x%x, smc id 64:0x%x\n",
  94. p_smc_desc->smc_id_aarch32, p_smc_desc->smc_id_aarch64);
  95. }
  96. }
  97. static int mtk_smc_handler_init(void)
  98. {
  99. const struct smc_descriptor *iter;
  100. short index_cnt;
  101. int ret = 0;
  102. smc_handler_pool = (const struct smc_descriptor *)MTK_SMC_POOL_START;
  103. /* Designate descriptor index point to smc_handler_pool */
  104. for (index_cnt = 0, iter = &smc_handler_pool[0];
  105. (char *)iter < (char *)MTK_SMC_POOL_END_UNALIGNED;
  106. iter++, index_cnt++) {
  107. if (index_cnt < 0) {
  108. SMC_HANDLER_DEBUG("smc handler pool index overflow!\n");
  109. ret = -EPERM;
  110. assert(0);
  111. break;
  112. }
  113. *(iter->smc_descriptor_index) = index_cnt;
  114. }
  115. smc_id_descriptor_max = index_cnt;
  116. print_smc_descriptor(smc_handler_pool);
  117. return ret;
  118. }
  119. MTK_EARLY_PLAT_INIT(mtk_smc_handler_init);
  120. /* This function handles Mediatek defined SiP Calls from Bootloader */
  121. static uintptr_t mtk_smc_handler_bl33(uint32_t smc_id,
  122. u_register_t x1,
  123. u_register_t x2,
  124. u_register_t x3,
  125. u_register_t x4,
  126. void *cookie,
  127. void *handle,
  128. u_register_t flags)
  129. {
  130. uintptr_t ret = MTK_SIP_E_SUCCESS;
  131. struct smccc_res smc_ret = {0};
  132. switch (smc_id) {
  133. MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
  134. default:
  135. INFO("BL33 SMC ID:0x%x not supported\n", smc_id);
  136. ret = SMC_UNK;
  137. break;
  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 Kernel */
  142. static uintptr_t mtk_smc_handler_nsel1(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_NS_EL1_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
  155. default:
  156. INFO("NSEL1 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. static uintptr_t mtk_smc_handler(uint32_t smc_id,
  163. u_register_t x1,
  164. u_register_t x2,
  165. u_register_t x3,
  166. u_register_t x4,
  167. void *cookie,
  168. void *handle,
  169. u_register_t flags)
  170. {
  171. uintptr_t ret = SMC_UNK;
  172. uint32_t ns;
  173. uint32_t smc_ori;
  174. uint32_t smc_num;
  175. /* Get SMC Originator bit 14.15 */
  176. smc_ori = GET_SMC_ORI(smc_id);
  177. /* Get SMC Number. Clean bit 14.15 */
  178. smc_num = GET_SMC_ORI_NUM(smc_id);
  179. /* Determine which security state this SMC originated from */
  180. ns = is_caller_non_secure(flags);
  181. if (ns && is_smc_forbidden(smc_ori)) {
  182. ERROR("%s: Forbidden SMC call (0x%x)\n", __func__, smc_id);
  183. SMC_RET1(handle, ret);
  184. }
  185. if (!ns) {
  186. /* SiP SMC service secure world's call */
  187. INFO("Secure SMC ID:0x%x not supported\n", smc_id);
  188. SMC_RET1(handle, ret);
  189. }
  190. if (is_from_bl33(smc_ori)) {
  191. /* SiP SMC service secure bootloader's call */
  192. return mtk_smc_handler_bl33(smc_num, x1, x2, x3, x4,
  193. cookie, handle, flags);
  194. } else if (is_from_nsel1(smc_ori)) {
  195. /* SiP SMC service kernel's call */
  196. return mtk_smc_handler_nsel1(smc_num, x1, x2, x3, x4,
  197. cookie, handle, flags);
  198. }
  199. INFO("SMC ID:0x%x not supported\n", smc_id);
  200. SMC_RET1(handle, ret);
  201. }
  202. /* Define a runtime service descriptor for fast SMC calls */
  203. DECLARE_RT_SVC(
  204. mtk_smc_handler,
  205. OEN_SIP_START,
  206. OEN_SIP_END,
  207. SMC_TYPE_FAST,
  208. NULL,
  209. mtk_smc_handler
  210. );