1 /*
2 * Copyright (c) 2014-2025, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <stdint.h>
9
10 #include <common/debug.h>
11 #include <common/runtime_svc.h>
12 #include <lib/el3_runtime/cpu_data.h>
13 #include <lib/pmf/pmf.h>
14 #include <lib/psci/psci.h>
15 #include <lib/runtime_instr.h>
16 #include <services/drtm_svc.h>
17 #include <services/errata_abi_svc.h>
18 #include <services/lfa_svc.h>
19 #include <services/pci_svc.h>
20 #include <services/rmmd_svc.h>
21 #include <services/sdei.h>
22 #include <services/spm_mm_svc.h>
23 #include <services/spmc_svc.h>
24 #include <services/spmd_svc.h>
25 #include <services/std_svc.h>
26 #include <services/trng_svc.h>
27 #include <smccc_helpers.h>
28 #include <tools_share/uuid.h>
29
30 /* Standard Service UUID */
31 static uuid_t arm_svc_uid = {
32 {0x5b, 0x90, 0x8d, 0x10},
33 {0x63, 0xf8},
34 {0xe8, 0x47},
35 0xae, 0x2d,
36 {0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2}
37 };
38
39 /* Setup Standard Services */
std_svc_setup(void)40 static int32_t std_svc_setup(void)
41 {
42 uintptr_t svc_arg;
43 int ret = 0;
44
45 svc_arg = get_arm_std_svc_args(PSCI_FID_MASK);
46 assert(svc_arg);
47
48 /*
49 * PSCI is one of the specifications implemented as a Standard Service.
50 * The `psci_setup()` also does EL3 architectural setup.
51 */
52 if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) {
53 ret = 1;
54 }
55
56 #if SPM_MM
57 if (spm_mm_setup() != 0) {
58 ret = 1;
59 }
60 #endif
61
62 #if defined(SPD_spmd)
63 if (spmd_setup() != 0) {
64 ret = 1;
65 }
66 #endif
67
68 #if ENABLE_RME
69 if (rmmd_setup() != 0) {
70 WARN("RMMD setup failed. Continuing boot.\n");
71 }
72 #endif
73
74 #if SDEI_SUPPORT
75 /* SDEI initialisation */
76 sdei_init();
77 #endif
78
79 #if TRNG_SUPPORT
80 /* TRNG initialisation */
81 trng_setup();
82 #endif /* TRNG_SUPPORT */
83
84 #if DRTM_SUPPORT
85 if (drtm_setup() != 0) {
86 ret = 1;
87 }
88 #endif /* DRTM_SUPPORT */
89
90 #if LFA_SUPPORT
91 /*
92 * Setup/Initialize resources useful during LFA
93 */
94 if (lfa_setup() != 0) {
95 ret = 1;
96 }
97 #endif /* LFA_SUPPORT */
98
99 return ret;
100 }
101
102 /*
103 * Top-level Standard Service SMC handler. This handler will in turn dispatch
104 * calls to PSCI SMC handler
105 */
std_svc_smc_handler(uint32_t smc_fid,u_register_t x1_arg,u_register_t x2_arg,u_register_t x3_arg,u_register_t x4_arg,void * cookie,void * handle,u_register_t flags)106 static uintptr_t std_svc_smc_handler(uint32_t smc_fid,
107 u_register_t x1_arg,
108 u_register_t x2_arg,
109 u_register_t x3_arg,
110 u_register_t x4_arg,
111 void *cookie,
112 void *handle,
113 u_register_t flags)
114 {
115 u_register_t x1 = x1_arg;
116 u_register_t x2 = x2_arg;
117 u_register_t x3 = x3_arg;
118 u_register_t x4 = x4_arg;
119
120 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
121 /* 32-bit SMC function, clear top parameter bits */
122
123 x1 &= UINT32_MAX;
124 x2 &= UINT32_MAX;
125 x3 &= UINT32_MAX;
126 x4 &= UINT32_MAX;
127 }
128
129 /*
130 * Dispatch PSCI calls to PSCI SMC handler and return its return
131 * value
132 */
133 if (is_psci_fid(smc_fid)) {
134 uint64_t ret;
135
136 #if ENABLE_RUNTIME_INSTRUMENTATION
137
138 /*
139 * Flush cache line so that even if CPU power down happens
140 * the timestamp update is reflected in memory.
141 */
142 PMF_WRITE_TIMESTAMP(rt_instr_svc,
143 RT_INSTR_ENTER_PSCI,
144 PMF_CACHE_MAINT,
145 get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX]));
146 #endif
147
148 ret = psci_smc_handler(smc_fid, x1, x2, x3, x4,
149 cookie, handle, flags);
150
151 #if ENABLE_RUNTIME_INSTRUMENTATION
152 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
153 RT_INSTR_EXIT_PSCI,
154 PMF_NO_CACHE_MAINT);
155 #endif
156
157 SMC_RET1(handle, ret);
158 }
159
160 #if SPM_MM
161 /*
162 * Dispatch SPM calls to SPM SMC handler and return its return
163 * value
164 */
165 if (is_spm_mm_fid(smc_fid)) {
166 return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
167 handle, flags);
168 }
169 #endif
170
171 #if defined(SPD_spmd)
172 /*
173 * Dispatch FFA calls to the FFA SMC handler implemented by the SPM
174 * dispatcher and return its return value
175 */
176 if (is_ffa_fid(smc_fid)) {
177 return spmd_ffa_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
178 handle, flags);
179 }
180 #endif
181
182 #if SDEI_SUPPORT
183 if (is_sdei_fid(smc_fid)) {
184 return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
185 flags);
186 }
187 #endif
188
189 #if TRNG_SUPPORT
190 if (is_trng_fid(smc_fid)) {
191 return trng_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
192 flags);
193 }
194 #endif /* TRNG_SUPPORT */
195
196 #if ERRATA_ABI_SUPPORT
197 if (is_errata_fid(smc_fid)) {
198 return errata_abi_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
199 handle, flags);
200 }
201 #endif /* ERRATA_ABI_SUPPORT */
202
203 #if ENABLE_RME
204
205 if (is_rmmd_el3_fid(smc_fid)) {
206 return rmmd_rmm_el3_handler(smc_fid, x1, x2, x3, x4, cookie,
207 handle, flags);
208 }
209
210 if (is_rmi_fid(smc_fid)) {
211 return rmmd_rmi_handler(smc_fid, x1, x2, x3, x4, cookie,
212 handle, flags);
213 }
214 #endif
215
216 #if SMC_PCI_SUPPORT
217 if (is_pci_fid(smc_fid)) {
218 return pci_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
219 flags);
220 }
221 #endif
222
223 #if DRTM_SUPPORT
224 if (is_drtm_fid(smc_fid)) {
225 return drtm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle,
226 flags);
227 }
228 #endif /* DRTM_SUPPORT */
229
230 #if LFA_SUPPORT
231 if (is_lfa_fid(smc_fid)) {
232 return lfa_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
233 }
234 #endif /* LFA_SUPPORT */
235
236
237 switch (smc_fid) {
238 case ARM_STD_SVC_CALL_COUNT:
239 /*
240 * Return the number of Standard Service Calls. PSCI is the only
241 * standard service implemented; so return number of PSCI calls
242 */
243 SMC_RET1(handle, PSCI_NUM_CALLS);
244
245 case ARM_STD_SVC_UID:
246 /* Return UID to the caller */
247 SMC_UUID_RET(handle, arm_svc_uid);
248
249 case ARM_STD_SVC_VERSION:
250 /* Return the version of current implementation */
251 SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR);
252
253 default:
254 VERBOSE("Unimplemented Standard Service Call: 0x%x \n", smc_fid);
255 SMC_RET1(handle, SMC_UNK);
256 }
257 }
258
259 /* Register Standard Service Calls as runtime service */
260 DECLARE_RT_SVC(
261 std_svc,
262
263 OEN_STD_START,
264 OEN_STD_END,
265 SMC_TYPE_FAST,
266 std_svc_setup,
267 std_svc_smc_handler
268 );
269