blob: a8a5a482e4956528d36e51adc931e0f58dc8345a [file] [log] [blame]
developer22e6dcb2022-06-08 10:49:24 +08001/*
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>
developer881307f2022-09-08 15:31:05 +08009#if MTK_SIP_KERNEL_BOOT_ENABLE
10#include <cold_boot.h>
11#endif
developer22e6dcb2022-06-08 10:49:24 +080012#include <common/debug.h>
13#include <common/runtime_svc.h>
developer22e6dcb2022-06-08 10:49:24 +080014#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 } \
Boyan Karatotev87266002022-11-18 14:17:17 +000054 /* fallthrough */ \
developer22e6dcb2022-06-08 10:49:24 +080055 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) \
Chris Kay33bfc5e2023-02-14 11:30:04 +000075 short _smc_id##_descriptor_index __section(".mtk_plat_ro") = -1;
developer22e6dcb2022-06-08 10:49:24 +080076
77MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX);
78MTK_SIP_SMC_FROM_NS_EL1_TABLE(SMC_ID_EXPAND_AS_DESCRIPTOR_INDEX);
79
80IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_START__, MTK_SMC_POOL_START);
81IMPORT_SYM(uintptr_t, __MTK_SMC_POOL_END_UNALIGNED__, MTK_SMC_POOL_END_UNALIGNED);
82
83static const struct smc_descriptor *smc_handler_pool;
84static short smc_id_descriptor_max;
85
86#if !MTK_SIP_KERNEL_BOOT_ENABLE
87/*
88 * If there is no SMC request needs to be served in 2nd bootloader,
89 * disable the service path inherently.
90 */
91bool is_el1_2nd_bootloader(void)
92{
93 return false;
94}
95#endif
96
97static void print_smc_descriptor(const struct smc_descriptor pool[])
98{
99 const struct smc_descriptor *p_smc_desc;
100
101 INFO("print smc descriptor pool\n");
102 for (p_smc_desc = &pool[0];
103 (char *)p_smc_desc < (char *)MTK_SMC_POOL_END_UNALIGNED;
104 p_smc_desc++) {
105 INFO("descriptor name:%s\n", p_smc_desc->smc_name);
106 INFO("descriptor index:%d\n", *p_smc_desc->smc_descriptor_index);
107 INFO("smc id 32:0x%x, smc id 64:0x%x\n",
108 p_smc_desc->smc_id_aarch32, p_smc_desc->smc_id_aarch64);
109 }
110}
111
112static int mtk_smc_handler_init(void)
113{
114 const struct smc_descriptor *iter;
115 short index_cnt;
116 int ret = 0;
117
118 smc_handler_pool = (const struct smc_descriptor *)MTK_SMC_POOL_START;
119 /* Designate descriptor index point to smc_handler_pool */
120 for (index_cnt = 0, iter = &smc_handler_pool[0];
121 (char *)iter < (char *)MTK_SMC_POOL_END_UNALIGNED;
122 iter++, index_cnt++) {
123 if (index_cnt < 0) {
124 SMC_HANDLER_DEBUG("smc handler pool index overflow!\n");
125 ret = -EPERM;
126 assert(0);
127 break;
128 }
129 *(iter->smc_descriptor_index) = index_cnt;
130 }
131 smc_id_descriptor_max = index_cnt;
132 print_smc_descriptor(smc_handler_pool);
133 return ret;
134}
135MTK_EARLY_PLAT_INIT(mtk_smc_handler_init);
136
137/* This function handles Mediatek defined SiP Calls from Bootloader */
138static uintptr_t mtk_smc_handler_bl33(uint32_t smc_id,
139 u_register_t x1,
140 u_register_t x2,
141 u_register_t x3,
142 u_register_t x4,
143 void *cookie,
144 void *handle,
145 u_register_t flags)
146{
147 uintptr_t ret = MTK_SIP_E_SUCCESS;
148 struct smccc_res smc_ret = {0};
149
150 switch (smc_id) {
151 MTK_SIP_SMC_FROM_BL33_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
152 default:
153 INFO("BL33 SMC ID:0x%x not supported\n", smc_id);
154 ret = SMC_UNK;
155 break;
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 Kernel */
161static uintptr_t mtk_smc_handler_nsel1(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_NS_EL1_TABLE(SMC_ID_EXPAND_AS_SMC_OPERATION);
175 default:
176 INFO("NSEL1 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
183static 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
197 /* Get SMC Originator bit 14.15 */
198 smc_ori = GET_SMC_ORI(smc_id);
199 /* Get SMC Number. Clean bit 14.15 */
200 smc_num = GET_SMC_ORI_NUM(smc_id);
201
202 /* Determine which security state this SMC originated from */
203 ns = is_caller_non_secure(flags);
204
205 if (ns && is_smc_forbidden(smc_ori)) {
206 ERROR("%s: Forbidden SMC call (0x%x)\n", __func__, smc_id);
207 SMC_RET1(handle, ret);
208 }
209
210 if (!ns) {
211 /* SiP SMC service secure world's call */
212 INFO("Secure SMC ID:0x%x not supported\n", smc_id);
213 SMC_RET1(handle, ret);
214 }
215 if (is_from_bl33(smc_ori)) {
216 /* SiP SMC service secure bootloader's call */
217 return mtk_smc_handler_bl33(smc_num, x1, x2, x3, x4,
218 cookie, handle, flags);
219 } else if (is_from_nsel1(smc_ori)) {
220 /* SiP SMC service kernel's call */
221 return mtk_smc_handler_nsel1(smc_num, x1, x2, x3, x4,
222 cookie, handle, flags);
223 }
224 INFO("SMC ID:0x%x not supported\n", smc_id);
225 SMC_RET1(handle, ret);
226}
227
228/* Define a runtime service descriptor for fast SMC calls */
229DECLARE_RT_SVC(
230 mtk_smc_handler,
231 OEN_SIP_START,
232 OEN_SIP_END,
233 SMC_TYPE_FAST,
234 NULL,
235 mtk_smc_handler
236);