blob: 6b1637314b20964a68754521803086d6578e46b0 [file] [log] [blame]
Achin Gupta86f23532019-10-11 15:41:16 +01001/*
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +00002 * Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
Achin Gupta86f23532019-10-11 15:41:16 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
Scott Brandene5dcf982020-08-25 13:49:32 -07009#include <inttypes.h>
10#include <stdint.h>
Achin Gupta86f23532019-10-11 15:41:16 +010011#include <string.h>
12
13#include <arch_helpers.h>
Olivier Deprez2bae35f2020-04-16 13:39:06 +020014#include <arch/aarch64/arch_features.h>
Achin Gupta86f23532019-10-11 15:41:16 +010015#include <bl31/bl31.h>
Olivier Depreza664c492020-08-05 11:27:42 +020016#include <bl31/interrupt_mgmt.h>
Achin Gupta86f23532019-10-11 15:41:16 +010017#include <common/debug.h>
18#include <common/runtime_svc.h>
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +000019#include <common/tbbr/tbbr_img_def.h>
Achin Gupta86f23532019-10-11 15:41:16 +010020#include <lib/el3_runtime/context_mgmt.h>
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +000021#include <lib/fconf/fconf.h>
22#include <lib/fconf/fconf_dyn_cfg_getter.h>
Achin Gupta86f23532019-10-11 15:41:16 +010023#include <lib/smccc.h>
24#include <lib/spinlock.h>
25#include <lib/utils.h>
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +000026#include <lib/xlat_tables/xlat_tables_v2.h>
Achin Gupta86f23532019-10-11 15:41:16 +010027#include <plat/common/common_def.h>
28#include <plat/common/platform.h>
29#include <platform_def.h>
J-Alves2672cde2020-05-07 18:42:25 +010030#include <services/ffa_svc.h>
Marc Bonnici1c33cc32021-11-29 17:57:03 +000031#include <services/spmc_svc.h>
Achin Gupta86f23532019-10-11 15:41:16 +010032#include <services/spmd_svc.h>
33#include <smccc_helpers.h>
34#include "spmd_private.h"
35
36/*******************************************************************************
37 * SPM Core context information.
38 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020039static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
Achin Gupta86f23532019-10-11 15:41:16 +010040
41/*******************************************************************************
Marc Bonnici1c33cc32021-11-29 17:57:03 +000042 * SPM Core attribute information is read from its manifest if the SPMC is not
43 * at EL3. Else, it is populated from the SPMC directly.
Achin Gupta86f23532019-10-11 15:41:16 +010044 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020045static spmc_manifest_attribute_t spmc_attrs;
Achin Gupta86f23532019-10-11 15:41:16 +010046
47/*******************************************************************************
Max Shvetsov745889c2020-02-27 14:54:21 +000048 * SPM Core entry point information. Discovered on the primary core and reused
49 * on secondary cores.
50 ******************************************************************************/
51static entry_point_info_t *spmc_ep_info;
52
53/*******************************************************************************
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020054 * SPM Core context on CPU based on mpidr.
55 ******************************************************************************/
56spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr)
57{
Max Shvetsovf80c64d2020-08-25 11:50:18 +010058 int core_idx = plat_core_pos_by_mpidr(mpidr);
59
60 if (core_idx < 0) {
Scott Brandene5dcf982020-08-25 13:49:32 -070061 ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx);
Max Shvetsovf80c64d2020-08-25 11:50:18 +010062 panic();
63 }
64
65 return &spm_core_context[core_idx];
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020066}
67
68/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +020069 * SPM Core context on current CPU get helper.
70 ******************************************************************************/
71spmd_spm_core_context_t *spmd_get_context(void)
72{
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020073 return spmd_get_context_by_mpidr(read_mpidr());
Olivier Deprez2bae35f2020-04-16 13:39:06 +020074}
75
76/*******************************************************************************
Olivier Deprezc7631a52020-03-23 09:53:06 +010077 * SPM Core ID getter.
78 ******************************************************************************/
79uint16_t spmd_spmc_id_get(void)
80{
81 return spmc_attrs.spmc_id;
82}
83
84/*******************************************************************************
Max Shvetsov745889c2020-02-27 14:54:21 +000085 * Static function declaration.
86 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020087static int32_t spmd_init(void);
Olivier Deprez69ca84a2020-02-07 15:44:43 +010088static int spmd_spmc_init(void *pm_addr);
J-Alves2672cde2020-05-07 18:42:25 +010089static uint64_t spmd_ffa_error_return(void *handle,
Olivier Deprez2bae35f2020-04-16 13:39:06 +020090 int error_code);
91static uint64_t spmd_smc_forward(uint32_t smc_fid,
92 bool secure_origin,
93 uint64_t x1,
94 uint64_t x2,
95 uint64_t x3,
96 uint64_t x4,
Marc Bonnicida2c9e12021-11-29 18:02:45 +000097 void *cookie,
98 void *handle,
99 uint64_t flags);
Max Shvetsov745889c2020-02-27 14:54:21 +0000100
Daniel Boulby9460a232021-12-09 11:20:13 +0000101/******************************************************************************
102 * Builds an SPMD to SPMC direct message request.
103 *****************************************************************************/
104void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
105 unsigned long long message)
106{
107 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
108 write_ctx_reg(gpregs, CTX_GPREG_X1,
109 (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
110 spmd_spmc_id_get());
111 write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
112 write_ctx_reg(gpregs, CTX_GPREG_X3, message);
113}
114
115
Max Shvetsov745889c2020-02-27 14:54:21 +0000116/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200117 * This function takes an SPMC context pointer and performs a synchronous
118 * SPMC entry.
Achin Gupta86f23532019-10-11 15:41:16 +0100119 ******************************************************************************/
120uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
121{
122 uint64_t rc;
123
124 assert(spmc_ctx != NULL);
125
126 cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
127
128 /* Restore the context assigned above */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000129#if SPMD_SPM_AT_SEL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000130 cm_el2_sysregs_context_restore(SECURE);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200131#else
132 cm_el1_sysregs_context_restore(SECURE);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000133#endif
Achin Gupta86f23532019-10-11 15:41:16 +0100134 cm_set_next_eret_context(SECURE);
135
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000136 /* Enter SPMC */
Achin Gupta86f23532019-10-11 15:41:16 +0100137 rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
138
139 /* Save secure state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000140#if SPMD_SPM_AT_SEL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000141 cm_el2_sysregs_context_save(SECURE);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200142#else
143 cm_el1_sysregs_context_save(SECURE);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000144#endif
Achin Gupta86f23532019-10-11 15:41:16 +0100145
146 return rc;
147}
148
149/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200150 * This function returns to the place where spmd_spm_core_sync_entry() was
Achin Gupta86f23532019-10-11 15:41:16 +0100151 * called originally.
152 ******************************************************************************/
153__dead2 void spmd_spm_core_sync_exit(uint64_t rc)
154{
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200155 spmd_spm_core_context_t *ctx = spmd_get_context();
Achin Gupta86f23532019-10-11 15:41:16 +0100156
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200157 /* Get current CPU context from SPMC context */
Achin Gupta86f23532019-10-11 15:41:16 +0100158 assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
159
160 /*
161 * The SPMD must have initiated the original request through a
162 * synchronous entry into SPMC. Jump back to the original C runtime
163 * context with the value of rc in x0;
164 */
165 spmd_spm_core_exit(ctx->c_rt_ctx, rc);
166
167 panic();
168}
169
170/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200171 * Jump to the SPM Core for the first time.
Achin Gupta86f23532019-10-11 15:41:16 +0100172 ******************************************************************************/
173static int32_t spmd_init(void)
174{
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200175 spmd_spm_core_context_t *ctx = spmd_get_context();
176 uint64_t rc;
Achin Gupta86f23532019-10-11 15:41:16 +0100177
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200178 VERBOSE("SPM Core init start.\n");
Olivier Deprez7c016332019-10-28 09:03:13 +0000179
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200180 /* Primary boot core enters the SPMC for initialization. */
181 ctx->state = SPMC_STATE_ON_PENDING;
Achin Gupta86f23532019-10-11 15:41:16 +0100182
183 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200184 if (rc != 0ULL) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700185 ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200186 return 0;
Achin Gupta86f23532019-10-11 15:41:16 +0100187 }
188
Olivier Deprez7c016332019-10-28 09:03:13 +0000189 ctx->state = SPMC_STATE_ON;
190
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200191 VERBOSE("SPM Core init end.\n");
Achin Gupta86f23532019-10-11 15:41:16 +0100192
193 return 1;
194}
195
196/*******************************************************************************
Olivier Depreza664c492020-08-05 11:27:42 +0200197 * spmd_secure_interrupt_handler
198 * Enter the SPMC for further handling of the secure interrupt by the SPMC
199 * itself or a Secure Partition.
200 ******************************************************************************/
201static uint64_t spmd_secure_interrupt_handler(uint32_t id,
202 uint32_t flags,
203 void *handle,
204 void *cookie)
205{
206 spmd_spm_core_context_t *ctx = spmd_get_context();
207 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
208 unsigned int linear_id = plat_my_core_pos();
209 int64_t rc;
210
211 /* Sanity check the security state when the exception was generated */
212 assert(get_interrupt_src_ss(flags) == NON_SECURE);
213
214 /* Sanity check the pointer to this cpu's context */
215 assert(handle == cm_get_context(NON_SECURE));
216
217 /* Save the non-secure context before entering SPMC */
218 cm_el1_sysregs_context_save(NON_SECURE);
219#if SPMD_SPM_AT_SEL2
220 cm_el2_sysregs_context_save(NON_SECURE);
221#endif
222
223 /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
224 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
225 write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
226 write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
227 write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
228 write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
229 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
230 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
231 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
232
233 /* Mark current core as handling a secure interrupt. */
234 ctx->secure_interrupt_ongoing = true;
235
236 rc = spmd_spm_core_sync_entry(ctx);
237 if (rc != 0ULL) {
Olivier Deprezba100f22021-11-09 12:37:20 +0100238 ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
Olivier Depreza664c492020-08-05 11:27:42 +0200239 }
240
241 ctx->secure_interrupt_ongoing = false;
242
243 cm_el1_sysregs_context_restore(NON_SECURE);
244#if SPMD_SPM_AT_SEL2
245 cm_el2_sysregs_context_restore(NON_SECURE);
246#endif
247 cm_set_next_eret_context(NON_SECURE);
248
249 SMC_RET0(&ctx->cpu_ctx);
250}
251
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200252#if (EL3_EXCEPTION_HANDLING == 0)
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600253/*******************************************************************************
254 * spmd_group0_interrupt_handler_nwd
255 * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
256 * handling of the interrupt to the platform handler, and return only upon
257 * successfully handling the Group0 interrupt.
258 ******************************************************************************/
259static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
260 uint32_t flags,
261 void *handle,
262 void *cookie)
263{
264 uint32_t intid;
265
266 /* Sanity check the security state when the exception was generated. */
267 assert(get_interrupt_src_ss(flags) == NON_SECURE);
268
269 /* Sanity check the pointer to this cpu's context. */
270 assert(handle == cm_get_context(NON_SECURE));
271
272 assert(id == INTR_ID_UNAVAILABLE);
273
274 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
275
276 intid = plat_ic_get_pending_interrupt_id();
277
278 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
279 ERROR("Group0 interrupt %u not handled\n", intid);
280 panic();
281 }
282
283 return 0U;
284}
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200285#endif
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600286
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -0600287/*******************************************************************************
288 * spmd_handle_group0_intr_swd
289 * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
290 * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
291 * interrupt to the platform handler, and returns only upon successfully
292 * handling the Group0 interrupt.
293 ******************************************************************************/
294static uint64_t spmd_handle_group0_intr_swd(void *handle)
295{
296 uint32_t intid;
297
298 /* Sanity check the pointer to this cpu's context */
299 assert(handle == cm_get_context(SECURE));
300
301 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
302
303 intid = plat_ic_get_pending_interrupt_id();
304
305 /*
306 * TODO: Currently due to a limitation in SPMD implementation, the
307 * platform handler is expected to not delegate handling to NWd while
308 * processing Group0 secure interrupt.
309 */
310 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
311 /* Group0 interrupt was not handled by the platform. */
312 ERROR("Group0 interrupt %u not handled\n", intid);
313 panic();
314 }
315
316 /* Return success. */
317 SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
318 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
319 FFA_PARAM_MBZ);
320}
321
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000322#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
323static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
324 unsigned int attr, uintptr_t *align_addr,
325 size_t *align_size)
326{
327 uintptr_t base_addr_align;
328 size_t mapped_size_align;
329 int rc;
330
331 /* Page aligned address and size if necessary */
332 base_addr_align = page_align(base_addr, DOWN);
333 mapped_size_align = page_align(size, UP);
334
335 if ((base_addr != base_addr_align) &&
336 (size == mapped_size_align)) {
337 mapped_size_align += PAGE_SIZE;
338 }
339
340 /*
341 * Map dynamically given region with its aligned base address and
342 * size
343 */
344 rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
345 base_addr_align,
346 mapped_size_align,
347 attr);
348 if (rc == 0) {
349 *align_addr = base_addr_align;
350 *align_size = mapped_size_align;
351 }
352
353 return rc;
354}
355
356static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
357 size_t size)
358{
359 uintptr_t root_base_addr_align, sec_base_addr_align;
360 size_t root_mapped_size_align, sec_mapped_size_align;
361 int rc;
362
363 assert(root_base_addr != 0UL);
364 assert(sec_base_addr != 0UL);
365 assert(size != 0UL);
366
367 /* Map the memory with required attributes */
368 rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
369 &root_base_addr_align,
370 &root_mapped_size_align);
371 if (rc != 0) {
372 ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
373 root_base_addr, rc);
374 panic();
375 }
376
377 rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
378 &sec_base_addr_align, &sec_mapped_size_align);
379 if (rc != 0) {
380 ERROR("%s %s %lu (%d)\n", "Error while mapping",
381 "secure region", sec_base_addr, rc);
382 panic();
383 }
384
385 /* Do copy operation */
386 (void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
387
388 /* Unmap root memory region */
389 rc = mmap_remove_dynamic_region(root_base_addr_align,
390 root_mapped_size_align);
391 if (rc != 0) {
392 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
393 "root region", root_base_addr_align, rc);
394 panic();
395 }
396
397 /* Unmap secure memory region */
398 rc = mmap_remove_dynamic_region(sec_base_addr_align,
399 sec_mapped_size_align);
400 if (rc != 0) {
401 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
402 "secure region", sec_base_addr_align, rc);
403 panic();
404 }
405}
406#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
407
Olivier Depreza664c492020-08-05 11:27:42 +0200408/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200409 * Loads SPMC manifest and inits SPMC.
Achin Gupta86f23532019-10-11 15:41:16 +0100410 ******************************************************************************/
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100411static int spmd_spmc_init(void *pm_addr)
Achin Gupta86f23532019-10-11 15:41:16 +0100412{
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200413 cpu_context_t *cpu_ctx;
414 unsigned int core_id;
Olivier Depreza664c492020-08-05 11:27:42 +0200415 uint32_t ep_attr, flags;
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200416 int rc;
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000417 const struct dyn_cfg_dtb_info_t *image_info __unused;
Achin Gupta86f23532019-10-11 15:41:16 +0100418
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200419 /* Load the SPM Core manifest */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100420 rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
Max Shvetsov745889c2020-02-27 14:54:21 +0000421 if (rc != 0) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200422 WARN("No or invalid SPM Core manifest image provided by BL2\n");
423 return rc;
Achin Gupta86f23532019-10-11 15:41:16 +0100424 }
425
426 /*
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200427 * Ensure that the SPM Core version is compatible with the SPM
428 * Dispatcher version.
Achin Gupta86f23532019-10-11 15:41:16 +0100429 */
J-Alves2672cde2020-05-07 18:42:25 +0100430 if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
431 (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
432 WARN("Unsupported FFA version (%u.%u)\n",
Achin Gupta86f23532019-10-11 15:41:16 +0100433 spmc_attrs.major_version, spmc_attrs.minor_version);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200434 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100435 }
436
J-Alves2672cde2020-05-07 18:42:25 +0100437 VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
Achin Gupta86f23532019-10-11 15:41:16 +0100438 spmc_attrs.minor_version);
439
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200440 VERBOSE("SPM Core run time EL%x.\n",
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000441 SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
Achin Gupta86f23532019-10-11 15:41:16 +0100442
Max Shvetsove79062e2020-03-12 15:16:40 +0000443 /* Validate the SPMC ID, Ensure high bit is set */
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200444 if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
445 SPMC_SECURE_ID_MASK) == 0U) {
446 WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
447 return -EINVAL;
Max Shvetsove79062e2020-03-12 15:16:40 +0000448 }
449
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200450 /* Validate the SPM Core execution state */
Achin Gupta86f23532019-10-11 15:41:16 +0100451 if ((spmc_attrs.exec_state != MODE_RW_64) &&
452 (spmc_attrs.exec_state != MODE_RW_32)) {
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100453 WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
Achin Gupta86f23532019-10-11 15:41:16 +0100454 spmc_attrs.exec_state);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200455 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100456 }
457
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100458 VERBOSE("%s%x.\n", "SPM Core execution state 0x",
459 spmc_attrs.exec_state);
Achin Gupta86f23532019-10-11 15:41:16 +0100460
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000461#if SPMD_SPM_AT_SEL2
462 /* Ensure manifest has not requested AArch32 state in S-EL2 */
463 if (spmc_attrs.exec_state == MODE_RW_32) {
464 WARN("AArch32 state at S-EL2 is not supported.\n");
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200465 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100466 }
467
468 /*
469 * Check if S-EL2 is supported on this system if S-EL2
470 * is required for SPM
471 */
Andre Przywara6dd2d062023-02-22 16:53:50 +0000472 if (!is_feat_sel2_supported()) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200473 WARN("SPM Core run time S-EL2 is not supported.\n");
474 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100475 }
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000476#endif /* SPMD_SPM_AT_SEL2 */
Achin Gupta86f23532019-10-11 15:41:16 +0100477
478 /* Initialise an entrypoint to set up the CPU context */
479 ep_attr = SECURE | EP_ST_ENABLE;
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200480 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
Achin Gupta86f23532019-10-11 15:41:16 +0100481 ep_attr |= EP_EE_BIG;
Max Shvetsov745889c2020-02-27 14:54:21 +0000482 }
483
Achin Gupta86f23532019-10-11 15:41:16 +0100484 SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta86f23532019-10-11 15:41:16 +0100485
486 /*
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200487 * Populate SPSR for SPM Core based upon validated parameters from the
488 * manifest.
Achin Gupta86f23532019-10-11 15:41:16 +0100489 */
490 if (spmc_attrs.exec_state == MODE_RW_32) {
491 spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
492 SPSR_E_LITTLE,
493 DAIF_FIQ_BIT |
494 DAIF_IRQ_BIT |
495 DAIF_ABT_BIT);
496 } else {
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000497
498#if SPMD_SPM_AT_SEL2
499 static const uint32_t runtime_el = MODE_EL2;
500#else
501 static const uint32_t runtime_el = MODE_EL1;
502#endif
503 spmc_ep_info->spsr = SPSR_64(runtime_el,
Achin Gupta86f23532019-10-11 15:41:16 +0100504 MODE_SP_ELX,
505 DISABLE_ALL_EXCEPTIONS);
506 }
507
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000508#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
509 image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
510 assert(image_info != NULL);
511
512 if ((image_info->config_addr == 0UL) ||
513 (image_info->secondary_config_addr == 0UL) ||
514 (image_info->config_max_size == 0UL)) {
515 return -EINVAL;
516 }
517
518 /* Copy manifest from root->secure region */
519 spmd_do_sec_cpy(image_info->config_addr,
520 image_info->secondary_config_addr,
521 image_info->config_max_size);
522
523 /* Update ep info of BL32 */
524 assert(spmc_ep_info != NULL);
525 spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
526#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
527
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200528 /* Set an initial SPMC context state for all cores. */
529 for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
530 spm_core_context[core_id].state = SPMC_STATE_OFF;
Max Shvetsov745889c2020-02-27 14:54:21 +0000531
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200532 /* Setup an initial cpu context for the SPMC. */
533 cpu_ctx = &spm_core_context[core_id].cpu_ctx;
534 cm_setup_context(cpu_ctx, spmc_ep_info);
Achin Gupta86f23532019-10-11 15:41:16 +0100535
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200536 /*
537 * Pass the core linear ID to the SPMC through x4.
538 * (TF-A implementation defined behavior helping
539 * a legacy TOS migration to adopt FF-A).
540 */
541 write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
542 }
Achin Gupta86f23532019-10-11 15:41:16 +0100543
Olivier Deprez9afca122019-10-28 09:15:52 +0000544 /* Register power management hooks with PSCI */
545 psci_register_spd_pm_hook(&spmd_pm);
546
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200547 /* Register init function for deferred init. */
Achin Gupta86f23532019-10-11 15:41:16 +0100548 bl31_register_bl32_init(&spmd_init);
549
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200550 INFO("SPM Core setup done.\n");
551
Olivier Depreza664c492020-08-05 11:27:42 +0200552 /*
553 * Register an interrupt handler routing secure interrupts to SPMD
554 * while the NWd is running.
555 */
556 flags = 0;
557 set_interrupt_rm_flag(flags, NON_SECURE);
558 rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
559 spmd_secure_interrupt_handler,
560 flags);
561 if (rc != 0) {
562 panic();
563 }
564
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600565 /*
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200566 * Permit configurations where the SPM resides at S-EL1/2 and upon a
567 * Group0 interrupt triggering while the normal world runs, the
568 * interrupt is routed either through the EHF or directly to the SPMD:
569 *
570 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
571 * for handling by spmd_group0_interrupt_handler_nwd.
572 *
573 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
574 *
575 */
576#if (EL3_EXCEPTION_HANDLING == 0)
577 /*
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600578 * Register an interrupt handler routing Group0 interrupts to SPMD
579 * while the NWd is running.
580 */
581 rc = register_interrupt_type_handler(INTR_TYPE_EL3,
582 spmd_group0_interrupt_handler_nwd,
583 flags);
584 if (rc != 0) {
585 panic();
586 }
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200587#endif
588
Achin Gupta86f23532019-10-11 15:41:16 +0100589 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000590}
Achin Gupta86f23532019-10-11 15:41:16 +0100591
Max Shvetsov745889c2020-02-27 14:54:21 +0000592/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200593 * Initialize context of SPM Core.
Max Shvetsov745889c2020-02-27 14:54:21 +0000594 ******************************************************************************/
595int spmd_setup(void)
596{
597 int rc;
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000598 void *spmc_manifest;
599
600 /*
601 * If the SPMC is at EL3, then just initialise it directly. The
602 * shenanigans of when it is at a lower EL are not needed.
603 */
604 if (is_spmc_at_el3()) {
605 /* Allow the SPMC to populate its attributes directly. */
606 spmc_populate_attrs(&spmc_attrs);
607
608 rc = spmc_setup();
609 if (rc != 0) {
Olivier Deprez3d203f42022-11-16 16:46:23 +0100610 WARN("SPMC initialisation failed 0x%x.\n", rc);
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000611 }
Olivier Deprez3d203f42022-11-16 16:46:23 +0100612 return 0;
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000613 }
Achin Gupta86f23532019-10-11 15:41:16 +0100614
Max Shvetsov745889c2020-02-27 14:54:21 +0000615 spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200616 if (spmc_ep_info == NULL) {
617 WARN("No SPM Core image provided by BL2 boot loader.\n");
Olivier Deprez3d203f42022-11-16 16:46:23 +0100618 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000619 }
620
621 /* Under no circumstances will this parameter be 0 */
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200622 assert(spmc_ep_info->pc != 0ULL);
Max Shvetsov745889c2020-02-27 14:54:21 +0000623
624 /*
625 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200626 * be used as a manifest for the SPM Core at the next lower EL/mode.
Max Shvetsov745889c2020-02-27 14:54:21 +0000627 */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100628 spmc_manifest = (void *)spmc_ep_info->args.arg0;
629 if (spmc_manifest == NULL) {
Olivier Deprez3d203f42022-11-16 16:46:23 +0100630 WARN("Invalid or absent SPM Core manifest.\n");
631 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000632 }
633
634 /* Load manifest, init SPMC */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100635 rc = spmd_spmc_init(spmc_manifest);
Max Shvetsov745889c2020-02-27 14:54:21 +0000636 if (rc != 0) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200637 WARN("Booting device without SPM initialization.\n");
Max Shvetsov745889c2020-02-27 14:54:21 +0000638 }
639
Olivier Deprez3d203f42022-11-16 16:46:23 +0100640 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000641}
642
643/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000644 * Forward FF-A SMCs to the other security state.
Max Shvetsov745889c2020-02-27 14:54:21 +0000645 ******************************************************************************/
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000646uint64_t spmd_smc_switch_state(uint32_t smc_fid,
647 bool secure_origin,
648 uint64_t x1,
649 uint64_t x2,
650 uint64_t x3,
651 uint64_t x4,
652 void *handle)
Max Shvetsov745889c2020-02-27 14:54:21 +0000653{
Olivier Deprezebc34772020-04-16 16:59:21 +0200654 unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
655 unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100656
Max Shvetsov745889c2020-02-27 14:54:21 +0000657 /* Save incoming security state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000658#if SPMD_SPM_AT_SEL2
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200659 if (secure_state_in == NON_SECURE) {
660 cm_el1_sysregs_context_save(secure_state_in);
661 }
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100662 cm_el2_sysregs_context_save(secure_state_in);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200663#else
664 cm_el1_sysregs_context_save(secure_state_in);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000665#endif
Max Shvetsov745889c2020-02-27 14:54:21 +0000666
667 /* Restore outgoing security state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000668#if SPMD_SPM_AT_SEL2
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200669 if (secure_state_out == NON_SECURE) {
670 cm_el1_sysregs_context_restore(secure_state_out);
671 }
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100672 cm_el2_sysregs_context_restore(secure_state_out);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200673#else
674 cm_el1_sysregs_context_restore(secure_state_out);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000675#endif
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100676 cm_set_next_eret_context(secure_state_out);
Max Shvetsov745889c2020-02-27 14:54:21 +0000677
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -0800678#if SPMD_SPM_AT_SEL2
679 /*
680 * If SPMC is at SEL2, save additional registers x8-x17, which may
681 * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
682 * Note that technically, all SPMCs can support this, but this code is
683 * under ifdef to minimize breakage in case other SPMCs do not save
684 * and restore x8-x17.
685 * We also need to pass through these registers since not all FF-A ABIs
686 * modify x8-x17, in which case, SMCCC requires that these registers be
687 * preserved, so the SPMD passes through these registers and expects the
688 * SPMC to save and restore (potentially also modify) them.
689 */
690 SMC_RET18(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
691 SMC_GET_GP(handle, CTX_GPREG_X5),
692 SMC_GET_GP(handle, CTX_GPREG_X6),
693 SMC_GET_GP(handle, CTX_GPREG_X7),
694 SMC_GET_GP(handle, CTX_GPREG_X8),
695 SMC_GET_GP(handle, CTX_GPREG_X9),
696 SMC_GET_GP(handle, CTX_GPREG_X10),
697 SMC_GET_GP(handle, CTX_GPREG_X11),
698 SMC_GET_GP(handle, CTX_GPREG_X12),
699 SMC_GET_GP(handle, CTX_GPREG_X13),
700 SMC_GET_GP(handle, CTX_GPREG_X14),
701 SMC_GET_GP(handle, CTX_GPREG_X15),
702 SMC_GET_GP(handle, CTX_GPREG_X16),
703 SMC_GET_GP(handle, CTX_GPREG_X17)
704 );
705
706#else
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100707 SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
Max Shvetsov745889c2020-02-27 14:54:21 +0000708 SMC_GET_GP(handle, CTX_GPREG_X5),
709 SMC_GET_GP(handle, CTX_GPREG_X6),
710 SMC_GET_GP(handle, CTX_GPREG_X7));
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -0800711#endif
Max Shvetsov745889c2020-02-27 14:54:21 +0000712}
713
714/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000715 * Forward SMCs to the other security state.
716 ******************************************************************************/
717static uint64_t spmd_smc_forward(uint32_t smc_fid,
718 bool secure_origin,
719 uint64_t x1,
720 uint64_t x2,
721 uint64_t x3,
722 uint64_t x4,
723 void *cookie,
724 void *handle,
725 uint64_t flags)
726{
727 if (is_spmc_at_el3() && !secure_origin) {
728 return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
729 cookie, handle, flags);
730 }
731 return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
732 handle);
733
734}
735
736/*******************************************************************************
J-Alves2672cde2020-05-07 18:42:25 +0100737 * Return FFA_ERROR with specified error code
Max Shvetsov745889c2020-02-27 14:54:21 +0000738 ******************************************************************************/
J-Alves2672cde2020-05-07 18:42:25 +0100739static uint64_t spmd_ffa_error_return(void *handle, int error_code)
Max Shvetsov745889c2020-02-27 14:54:21 +0000740{
J-Alves64ff9932021-03-01 10:26:59 +0000741 SMC_RET8(handle, (uint32_t) FFA_ERROR,
742 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
J-Alves2672cde2020-05-07 18:42:25 +0100743 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
744 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Gupta86f23532019-10-11 15:41:16 +0100745}
746
Olivier Deprez33e44122020-04-16 17:54:27 +0200747/*******************************************************************************
748 * spmd_check_address_in_binary_image
749 ******************************************************************************/
750bool spmd_check_address_in_binary_image(uint64_t address)
751{
752 assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
753
754 return ((address >= spmc_attrs.load_address) &&
755 (address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
756}
757
Olivier Deprezebc34772020-04-16 16:59:21 +0200758/******************************************************************************
759 * spmd_is_spmc_message
760 *****************************************************************************/
761static bool spmd_is_spmc_message(unsigned int ep)
762{
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000763 if (is_spmc_at_el3()) {
764 return false;
765 }
766
Olivier Deprezebc34772020-04-16 16:59:21 +0200767 return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
768 && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
769}
770
Olivier Deprez33e44122020-04-16 17:54:27 +0200771/******************************************************************************
772 * spmd_handle_spmc_message
773 *****************************************************************************/
Olivier Deprezc7631a52020-03-23 09:53:06 +0100774static int spmd_handle_spmc_message(unsigned long long msg,
775 unsigned long long parm1, unsigned long long parm2,
776 unsigned long long parm3, unsigned long long parm4)
Olivier Deprez33e44122020-04-16 17:54:27 +0200777{
778 VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
779 msg, parm1, parm2, parm3, parm4);
780
Olivier Deprez33e44122020-04-16 17:54:27 +0200781 return -EINVAL;
782}
783
Achin Gupta86f23532019-10-11 15:41:16 +0100784/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000785 * This function forwards FF-A SMCs to either the main SPMD handler or the
786 * SPMC at EL3, depending on the origin security state, if enabled.
787 ******************************************************************************/
788uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
789 uint64_t x1,
790 uint64_t x2,
791 uint64_t x3,
792 uint64_t x4,
793 void *cookie,
794 void *handle,
795 uint64_t flags)
796{
797 if (is_spmc_at_el3()) {
798 /*
799 * If we have an SPMC at EL3 allow handling of the SMC first.
800 * The SPMC will call back through to SPMD handler if required.
801 */
802 if (is_caller_secure(flags)) {
803 return spmc_smc_handler(smc_fid,
804 is_caller_secure(flags),
805 x1, x2, x3, x4, cookie,
806 handle, flags);
807 }
808 }
809 return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
810 handle, flags);
811}
812
813/*******************************************************************************
J-Alves2672cde2020-05-07 18:42:25 +0100814 * This function handles all SMCs in the range reserved for FFA. Each call is
Achin Gupta86f23532019-10-11 15:41:16 +0100815 * either forwarded to the other security state or handled by the SPM dispatcher
816 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200817uint64_t spmd_smc_handler(uint32_t smc_fid,
818 uint64_t x1,
819 uint64_t x2,
820 uint64_t x3,
821 uint64_t x4,
822 void *cookie,
823 void *handle,
Achin Gupta86f23532019-10-11 15:41:16 +0100824 uint64_t flags)
825{
Olivier Deprezeae45962021-01-19 15:06:47 +0100826 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200827 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100828 bool secure_origin;
829 int32_t ret;
J-Alves4c95c702020-05-26 14:03:05 +0100830 uint32_t input_version;
Achin Gupta86f23532019-10-11 15:41:16 +0100831
832 /* Determine which security state this SMC originated from */
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100833 secure_origin = is_caller_secure(flags);
Achin Gupta86f23532019-10-11 15:41:16 +0100834
Scott Brandene5dcf982020-08-25 13:49:32 -0700835 VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
836 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
837 linear_id, smc_fid, x1, x2, x3, x4,
838 SMC_GET_GP(handle, CTX_GPREG_X5),
839 SMC_GET_GP(handle, CTX_GPREG_X6),
840 SMC_GET_GP(handle, CTX_GPREG_X7));
Achin Gupta86f23532019-10-11 15:41:16 +0100841
842 switch (smc_fid) {
J-Alves2672cde2020-05-07 18:42:25 +0100843 case FFA_ERROR:
Achin Gupta86f23532019-10-11 15:41:16 +0100844 /*
845 * Check if this is the first invocation of this interface on
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200846 * this CPU. If so, then indicate that the SPM Core initialised
Achin Gupta86f23532019-10-11 15:41:16 +0100847 * unsuccessfully.
848 */
Olivier Deprez7c016332019-10-28 09:03:13 +0000849 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Achin Gupta86f23532019-10-11 15:41:16 +0100850 spmd_spm_core_sync_exit(x2);
Max Shvetsov745889c2020-02-27 14:54:21 +0000851 }
Achin Gupta86f23532019-10-11 15:41:16 +0100852
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100853 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000854 x1, x2, x3, x4, cookie,
855 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +0100856 break; /* not reached */
857
J-Alves2672cde2020-05-07 18:42:25 +0100858 case FFA_VERSION:
J-Alves4c95c702020-05-26 14:03:05 +0100859 input_version = (uint32_t)(0xFFFFFFFF & x1);
Achin Gupta86f23532019-10-11 15:41:16 +0100860 /*
J-Alves4c95c702020-05-26 14:03:05 +0100861 * If caller is secure and SPMC was initialized,
862 * return FFA_VERSION of SPMD.
863 * If caller is non secure and SPMC was initialized,
Marc Bonnici815d1012021-12-08 14:27:40 +0000864 * forward to the EL3 SPMC if enabled, otherwise return
865 * the SPMC version if implemented at a lower EL.
J-Alves4c95c702020-05-26 14:03:05 +0100866 * Sanity check to "input_version".
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000867 * If the EL3 SPMC is enabled, ignore the SPMC state as
868 * this is not used.
Achin Gupta86f23532019-10-11 15:41:16 +0100869 */
J-Alves4c95c702020-05-26 14:03:05 +0100870 if ((input_version & FFA_VERSION_BIT31_MASK) ||
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000871 (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
J-Alves4c95c702020-05-26 14:03:05 +0100872 ret = FFA_ERROR_NOT_SUPPORTED;
873 } else if (!secure_origin) {
Marc Bonnici815d1012021-12-08 14:27:40 +0000874 if (is_spmc_at_el3()) {
875 /*
876 * Forward the call directly to the EL3 SPMC, if
877 * enabled, as we don't need to wrap the call in
878 * a direct request.
879 */
880 return spmd_smc_forward(smc_fid, secure_origin,
881 x1, x2, x3, x4, cookie,
882 handle, flags);
883 }
884
Daniel Boulby9460a232021-12-09 11:20:13 +0000885 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
886 uint64_t rc;
887
888 if (spmc_attrs.major_version == 1 &&
889 spmc_attrs.minor_version == 0) {
890 ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
891 spmc_attrs.minor_version);
892 SMC_RET8(handle, (uint32_t)ret,
893 FFA_TARGET_INFO_MBZ,
894 FFA_TARGET_INFO_MBZ,
895 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
896 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
897 FFA_PARAM_MBZ);
898 break;
899 }
900 /* Save non-secure system registers context */
901 cm_el1_sysregs_context_save(NON_SECURE);
902#if SPMD_SPM_AT_SEL2
903 cm_el2_sysregs_context_save(NON_SECURE);
904#endif
905
906 /*
907 * The incoming request has FFA_VERSION as X0 smc_fid
908 * and requested version in x1. Prepare a direct request
909 * from SPMD to SPMC with FFA_VERSION framework function
910 * identifier in X2 and requested version in X3.
911 */
912 spmd_build_spmc_message(gpregs,
913 SPMD_FWK_MSG_FFA_VERSION_REQ,
914 input_version);
915
916 rc = spmd_spm_core_sync_entry(ctx);
917
918 if ((rc != 0ULL) ||
919 (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
920 FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
921 (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
Marc Bonnici25f4b542022-04-12 17:18:13 +0100922 (FFA_FWK_MSG_BIT |
Daniel Boulby9460a232021-12-09 11:20:13 +0000923 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
924 ERROR("Failed to forward FFA_VERSION\n");
925 ret = FFA_ERROR_NOT_SUPPORTED;
926 } else {
927 ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
928 }
929
930 /*
931 * Return here after SPMC has handled FFA_VERSION.
932 * The returned SPMC version is held in X3.
933 * Forward this version in X0 to the non-secure caller.
934 */
935 return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
936 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000937 FFA_PARAM_MBZ, cookie, gpregs,
938 flags);
J-Alves4c95c702020-05-26 14:03:05 +0100939 } else {
J-Alves64ff9932021-03-01 10:26:59 +0000940 ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
941 FFA_VERSION_MINOR);
J-Alves4c95c702020-05-26 14:03:05 +0100942 }
943
J-Alves64ff9932021-03-01 10:26:59 +0000944 SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
945 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
946 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Gupta86f23532019-10-11 15:41:16 +0100947 break; /* not reached */
948
J-Alves2672cde2020-05-07 18:42:25 +0100949 case FFA_FEATURES:
Achin Gupta86f23532019-10-11 15:41:16 +0100950 /*
951 * This is an optional interface. Do the minimal checks and
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200952 * forward to SPM Core which will handle it if implemented.
Achin Gupta86f23532019-10-11 15:41:16 +0100953 */
954
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200955 /* Forward SMC from Normal world to the SPM Core */
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100956 if (!secure_origin) {
957 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000958 x1, x2, x3, x4, cookie,
959 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +0100960 }
Max Shvetsov745889c2020-02-27 14:54:21 +0000961
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200962 /*
963 * Return success if call was from secure world i.e. all
J-Alves2672cde2020-05-07 18:42:25 +0100964 * FFA functions are supported. This is essentially a
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200965 * nop.
966 */
J-Alves2672cde2020-05-07 18:42:25 +0100967 SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200968 SMC_GET_GP(handle, CTX_GPREG_X5),
969 SMC_GET_GP(handle, CTX_GPREG_X6),
970 SMC_GET_GP(handle, CTX_GPREG_X7));
971
Achin Gupta86f23532019-10-11 15:41:16 +0100972 break; /* not reached */
973
J-Alves2672cde2020-05-07 18:42:25 +0100974 case FFA_ID_GET:
Max Shvetsove79062e2020-03-12 15:16:40 +0000975 /*
J-Alves2672cde2020-05-07 18:42:25 +0100976 * Returns the ID of the calling FFA component.
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200977 */
Max Shvetsove79062e2020-03-12 15:16:40 +0000978 if (!secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +0100979 SMC_RET8(handle, FFA_SUCCESS_SMC32,
980 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
981 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
982 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
983 FFA_PARAM_MBZ);
Max Shvetsove79062e2020-03-12 15:16:40 +0000984 }
985
J-Alves2672cde2020-05-07 18:42:25 +0100986 SMC_RET8(handle, FFA_SUCCESS_SMC32,
987 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
988 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
989 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
990 FFA_PARAM_MBZ);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200991
Max Shvetsove79062e2020-03-12 15:16:40 +0000992 break; /* not reached */
993
Olivier Deprezeae45962021-01-19 15:06:47 +0100994 case FFA_SECONDARY_EP_REGISTER_SMC64:
995 if (secure_origin) {
996 ret = spmd_pm_secondary_ep_register(x1);
997
998 if (ret < 0) {
999 SMC_RET8(handle, FFA_ERROR_SMC64,
1000 FFA_TARGET_INFO_MBZ, ret,
1001 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1002 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1003 FFA_PARAM_MBZ);
1004 } else {
1005 SMC_RET8(handle, FFA_SUCCESS_SMC64,
1006 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1007 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1008 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1009 FFA_PARAM_MBZ);
1010 }
1011 }
1012
1013 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1014 break; /* Not reached */
1015
Daniel Boulby27f35df2021-02-03 12:13:19 +00001016 case FFA_SPM_ID_GET:
1017 if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
1018 return spmd_ffa_error_return(handle,
1019 FFA_ERROR_NOT_SUPPORTED);
1020 }
1021 /*
1022 * Returns the ID of the SPMC or SPMD depending on the FF-A
1023 * instance where this function is invoked
1024 */
1025 if (!secure_origin) {
1026 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1027 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1028 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1029 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1030 FFA_PARAM_MBZ);
1031 }
1032 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1033 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
1034 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1035 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1036 FFA_PARAM_MBZ);
1037
1038 break; /* not reached */
1039
Olivier Deprez33e44122020-04-16 17:54:27 +02001040 case FFA_MSG_SEND_DIRECT_REQ_SMC32:
Shruti3d859672022-06-09 11:03:11 +01001041 case FFA_MSG_SEND_DIRECT_REQ_SMC64:
1042 if (!secure_origin) {
1043 /* Validate source endpoint is non-secure for non-secure caller. */
1044 if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1045 return spmd_ffa_error_return(handle,
1046 FFA_ERROR_INVALID_PARAMETER);
1047 }
1048 }
Olivier Deprez33e44122020-04-16 17:54:27 +02001049 if (secure_origin && spmd_is_spmc_message(x1)) {
1050 ret = spmd_handle_spmc_message(x3, x4,
1051 SMC_GET_GP(handle, CTX_GPREG_X5),
1052 SMC_GET_GP(handle, CTX_GPREG_X6),
1053 SMC_GET_GP(handle, CTX_GPREG_X7));
1054
1055 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1056 FFA_TARGET_INFO_MBZ, ret,
1057 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1058 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1059 FFA_PARAM_MBZ);
1060 } else {
1061 /* Forward direct message to the other world */
1062 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001063 x1, x2, x3, x4, cookie,
1064 handle, flags);
Olivier Deprez33e44122020-04-16 17:54:27 +02001065 }
1066 break; /* Not reached */
1067
1068 case FFA_MSG_SEND_DIRECT_RESP_SMC32:
1069 if (secure_origin && spmd_is_spmc_message(x1)) {
Olivier Depreza664c492020-08-05 11:27:42 +02001070 spmd_spm_core_sync_exit(0ULL);
Olivier Deprez33e44122020-04-16 17:54:27 +02001071 } else {
1072 /* Forward direct message to the other world */
1073 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001074 x1, x2, x3, x4, cookie,
1075 handle, flags);
Olivier Deprez33e44122020-04-16 17:54:27 +02001076 }
1077 break; /* Not reached */
1078
J-Alves2672cde2020-05-07 18:42:25 +01001079 case FFA_RX_RELEASE:
1080 case FFA_RXTX_MAP_SMC32:
1081 case FFA_RXTX_MAP_SMC64:
1082 case FFA_RXTX_UNMAP:
Ruari Phipps93dff702020-07-28 10:33:35 +01001083 case FFA_PARTITION_INFO_GET:
J-Alves2621cfd2021-03-11 17:46:47 +00001084#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1085 case FFA_NOTIFICATION_BITMAP_CREATE:
1086 case FFA_NOTIFICATION_BITMAP_DESTROY:
1087 case FFA_NOTIFICATION_BIND:
1088 case FFA_NOTIFICATION_UNBIND:
1089 case FFA_NOTIFICATION_SET:
1090 case FFA_NOTIFICATION_GET:
1091 case FFA_NOTIFICATION_INFO_GET:
1092 case FFA_NOTIFICATION_INFO_GET_SMC64:
Federico Recanatieecb4b02022-02-03 17:22:37 +01001093 case FFA_MSG_SEND2:
Federico Recanati5c7c5c42022-03-18 10:30:00 +01001094 case FFA_RX_ACQUIRE:
J-Alves2621cfd2021-03-11 17:46:47 +00001095#endif
Federico Recanatieecb4b02022-02-03 17:22:37 +01001096 case FFA_MSG_RUN:
Ruari Phipps93dff702020-07-28 10:33:35 +01001097 /*
Federico Recanatieecb4b02022-02-03 17:22:37 +01001098 * Above calls should be invoked only by the Normal world and
1099 * must not be forwarded from Secure world to Normal world.
Ruari Phipps93dff702020-07-28 10:33:35 +01001100 */
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001101 if (secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +01001102 return spmd_ffa_error_return(handle,
Ruari Phipps93dff702020-07-28 10:33:35 +01001103 FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001104 }
1105
Boyan Karatotev87266002022-11-18 14:17:17 +00001106 /* Forward the call to the other world */
1107 /* fallthrough */
J-Alves2672cde2020-05-07 18:42:25 +01001108 case FFA_MSG_SEND:
J-Alves2672cde2020-05-07 18:42:25 +01001109 case FFA_MSG_SEND_DIRECT_RESP_SMC64:
1110 case FFA_MEM_DONATE_SMC32:
1111 case FFA_MEM_DONATE_SMC64:
1112 case FFA_MEM_LEND_SMC32:
1113 case FFA_MEM_LEND_SMC64:
1114 case FFA_MEM_SHARE_SMC32:
1115 case FFA_MEM_SHARE_SMC64:
1116 case FFA_MEM_RETRIEVE_REQ_SMC32:
1117 case FFA_MEM_RETRIEVE_REQ_SMC64:
1118 case FFA_MEM_RETRIEVE_RESP:
1119 case FFA_MEM_RELINQUISH:
1120 case FFA_MEM_RECLAIM:
Marc Bonnici9fa01e92021-09-23 09:44:14 +01001121 case FFA_MEM_FRAG_TX:
1122 case FFA_MEM_FRAG_RX:
J-Alves2672cde2020-05-07 18:42:25 +01001123 case FFA_SUCCESS_SMC32:
1124 case FFA_SUCCESS_SMC64:
Achin Gupta86f23532019-10-11 15:41:16 +01001125 /*
1126 * TODO: Assume that no requests originate from EL3 at the
1127 * moment. This will change if a SP service is required in
1128 * response to secure interrupts targeted to EL3. Until then
1129 * simply forward the call to the Normal world.
1130 */
1131
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001132 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001133 x1, x2, x3, x4, cookie,
1134 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +01001135 break; /* not reached */
1136
J-Alves2672cde2020-05-07 18:42:25 +01001137 case FFA_MSG_WAIT:
Achin Gupta86f23532019-10-11 15:41:16 +01001138 /*
1139 * Check if this is the first invocation of this interface on
1140 * this CPU from the Secure world. If so, then indicate that the
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001141 * SPM Core initialised successfully.
Achin Gupta86f23532019-10-11 15:41:16 +01001142 */
Olivier Deprez7c016332019-10-28 09:03:13 +00001143 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Olivier Depreza664c492020-08-05 11:27:42 +02001144 spmd_spm_core_sync_exit(0ULL);
Achin Gupta86f23532019-10-11 15:41:16 +01001145 }
1146
Boyan Karatotev87266002022-11-18 14:17:17 +00001147 /* Forward the call to the other world */
1148 /* fallthrough */
Olivier Deprezae18caf2021-04-02 11:09:10 +02001149 case FFA_INTERRUPT:
J-Alves2672cde2020-05-07 18:42:25 +01001150 case FFA_MSG_YIELD:
Achin Gupta86f23532019-10-11 15:41:16 +01001151 /* This interface must be invoked only by the Secure world */
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001152 if (!secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +01001153 return spmd_ffa_error_return(handle,
1154 FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001155 }
1156
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001157 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001158 x1, x2, x3, x4, cookie,
1159 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +01001160 break; /* not reached */
1161
Olivier Depreza664c492020-08-05 11:27:42 +02001162 case FFA_NORMAL_WORLD_RESUME:
1163 if (secure_origin && ctx->secure_interrupt_ongoing) {
1164 spmd_spm_core_sync_exit(0ULL);
1165 } else {
1166 return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
1167 }
1168 break; /* Not reached */
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -08001169#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1170 case FFA_PARTITION_INFO_GET_REGS_SMC64:
1171 if (secure_origin) {
1172 /* TODO: Future patches to enable support for this */
1173 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1174 }
Olivier Depreza664c492020-08-05 11:27:42 +02001175
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -08001176 /* Call only supported with SMCCC 1.2+ */
1177 if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1178 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1179 }
1180
1181 return spmd_smc_forward(smc_fid, secure_origin,
1182 x1, x2, x3, x4, cookie,
1183 handle, flags);
1184 break; /* Not reached */
1185#endif
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -06001186 case FFA_EL3_INTR_HANDLE:
1187 if (secure_origin) {
1188 return spmd_handle_group0_intr_swd(handle);
1189 } else {
1190 return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
1191 }
Achin Gupta86f23532019-10-11 15:41:16 +01001192 default:
1193 WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
J-Alves2672cde2020-05-07 18:42:25 +01001194 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001195 }
1196}