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