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