blob: 62b9aa0556806dc8c6e785156c2d9973d571f658 [file] [log] [blame]
Achin Gupta86f23532019-10-11 15:41:16 +01001/*
Kathleen Capella9d826a12023-07-31 14:45:58 -04002 * Copyright (c) 2020-2024, 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>
Raghu Krishnamurthy7f046c12023-02-25 13:26:10 -080030#include <services/el3_spmd_logical_sp.h>
J-Alves2672cde2020-05-07 18:42:25 +010031#include <services/ffa_svc.h>
Marc Bonnici1c33cc32021-11-29 17:57:03 +000032#include <services/spmc_svc.h>
Achin Gupta86f23532019-10-11 15:41:16 +010033#include <services/spmd_svc.h>
34#include <smccc_helpers.h>
35#include "spmd_private.h"
36
37/*******************************************************************************
38 * SPM Core context information.
39 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020040static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
Achin Gupta86f23532019-10-11 15:41:16 +010041
42/*******************************************************************************
Marc Bonnici1c33cc32021-11-29 17:57:03 +000043 * SPM Core attribute information is read from its manifest if the SPMC is not
44 * at EL3. Else, it is populated from the SPMC directly.
Achin Gupta86f23532019-10-11 15:41:16 +010045 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020046static spmc_manifest_attribute_t spmc_attrs;
Achin Gupta86f23532019-10-11 15:41:16 +010047
48/*******************************************************************************
Max Shvetsov745889c2020-02-27 14:54:21 +000049 * SPM Core entry point information. Discovered on the primary core and reused
50 * on secondary cores.
51 ******************************************************************************/
52static entry_point_info_t *spmc_ep_info;
53
54/*******************************************************************************
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020055 * SPM Core context on CPU based on mpidr.
56 ******************************************************************************/
57spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr)
58{
Max Shvetsovf80c64d2020-08-25 11:50:18 +010059 int core_idx = plat_core_pos_by_mpidr(mpidr);
60
61 if (core_idx < 0) {
Scott Brandene5dcf982020-08-25 13:49:32 -070062 ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx);
Max Shvetsovf80c64d2020-08-25 11:50:18 +010063 panic();
64 }
65
66 return &spm_core_context[core_idx];
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020067}
68
69/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +020070 * SPM Core context on current CPU get helper.
71 ******************************************************************************/
72spmd_spm_core_context_t *spmd_get_context(void)
73{
Olivier Deprez73ef0dc2020-06-19 15:33:41 +020074 return spmd_get_context_by_mpidr(read_mpidr());
Olivier Deprez2bae35f2020-04-16 13:39:06 +020075}
76
77/*******************************************************************************
Olivier Deprezc7631a52020-03-23 09:53:06 +010078 * SPM Core ID getter.
79 ******************************************************************************/
80uint16_t spmd_spmc_id_get(void)
81{
82 return spmc_attrs.spmc_id;
83}
84
85/*******************************************************************************
Max Shvetsov745889c2020-02-27 14:54:21 +000086 * Static function declaration.
87 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +020088static int32_t spmd_init(void);
Olivier Deprez69ca84a2020-02-07 15:44:43 +010089static int spmd_spmc_init(void *pm_addr);
Raghu Krishnamurthy9d9584f2023-04-22 18:00:02 -070090
Olivier Deprez2bae35f2020-04-16 13:39:06 +020091static 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);
Olivier Deprez4911eb82023-07-10 11:04:30 +0200113
114 /* Zero out x4-x7 for the direct request emitted towards the SPMC. */
115 write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
116 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
117 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
118 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
Daniel Boulby9460a232021-12-09 11:20:13 +0000119}
120
121
Max Shvetsov745889c2020-02-27 14:54:21 +0000122/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200123 * This function takes an SPMC context pointer and performs a synchronous
124 * SPMC entry.
Achin Gupta86f23532019-10-11 15:41:16 +0100125 ******************************************************************************/
126uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
127{
128 uint64_t rc;
129
130 assert(spmc_ctx != NULL);
131
132 cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
133
134 /* Restore the context assigned above */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000135#if SPMD_SPM_AT_SEL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000136 cm_el2_sysregs_context_restore(SECURE);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200137#else
138 cm_el1_sysregs_context_restore(SECURE);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000139#endif
Achin Gupta86f23532019-10-11 15:41:16 +0100140 cm_set_next_eret_context(SECURE);
141
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000142 /* Enter SPMC */
Achin Gupta86f23532019-10-11 15:41:16 +0100143 rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
144
145 /* Save secure state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000146#if SPMD_SPM_AT_SEL2
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000147 cm_el2_sysregs_context_save(SECURE);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200148#else
149 cm_el1_sysregs_context_save(SECURE);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000150#endif
Achin Gupta86f23532019-10-11 15:41:16 +0100151
152 return rc;
153}
154
155/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200156 * This function returns to the place where spmd_spm_core_sync_entry() was
Achin Gupta86f23532019-10-11 15:41:16 +0100157 * called originally.
158 ******************************************************************************/
159__dead2 void spmd_spm_core_sync_exit(uint64_t rc)
160{
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200161 spmd_spm_core_context_t *ctx = spmd_get_context();
Achin Gupta86f23532019-10-11 15:41:16 +0100162
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200163 /* Get current CPU context from SPMC context */
Achin Gupta86f23532019-10-11 15:41:16 +0100164 assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
165
166 /*
167 * The SPMD must have initiated the original request through a
168 * synchronous entry into SPMC. Jump back to the original C runtime
169 * context with the value of rc in x0;
170 */
171 spmd_spm_core_exit(ctx->c_rt_ctx, rc);
172
173 panic();
174}
175
176/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200177 * Jump to the SPM Core for the first time.
Achin Gupta86f23532019-10-11 15:41:16 +0100178 ******************************************************************************/
179static int32_t spmd_init(void)
180{
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200181 spmd_spm_core_context_t *ctx = spmd_get_context();
182 uint64_t rc;
Achin Gupta86f23532019-10-11 15:41:16 +0100183
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200184 VERBOSE("SPM Core init start.\n");
Olivier Deprez7c016332019-10-28 09:03:13 +0000185
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200186 /* Primary boot core enters the SPMC for initialization. */
187 ctx->state = SPMC_STATE_ON_PENDING;
Achin Gupta86f23532019-10-11 15:41:16 +0100188
189 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200190 if (rc != 0ULL) {
Scott Brandene5dcf982020-08-25 13:49:32 -0700191 ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200192 return 0;
Achin Gupta86f23532019-10-11 15:41:16 +0100193 }
194
Olivier Deprez7c016332019-10-28 09:03:13 +0000195 ctx->state = SPMC_STATE_ON;
196
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200197 VERBOSE("SPM Core init end.\n");
Achin Gupta86f23532019-10-11 15:41:16 +0100198
Raghu Krishnamurthy7f046c12023-02-25 13:26:10 -0800199 spmd_logical_sp_set_spmc_initialized();
200 rc = spmd_logical_sp_init();
201 if (rc != 0) {
202 WARN("SPMD Logical partitions failed init.\n");
203 }
204
Achin Gupta86f23532019-10-11 15:41:16 +0100205 return 1;
206}
207
208/*******************************************************************************
Olivier Depreza664c492020-08-05 11:27:42 +0200209 * spmd_secure_interrupt_handler
210 * Enter the SPMC for further handling of the secure interrupt by the SPMC
211 * itself or a Secure Partition.
212 ******************************************************************************/
213static uint64_t spmd_secure_interrupt_handler(uint32_t id,
214 uint32_t flags,
215 void *handle,
216 void *cookie)
217{
218 spmd_spm_core_context_t *ctx = spmd_get_context();
219 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
220 unsigned int linear_id = plat_my_core_pos();
221 int64_t rc;
222
223 /* Sanity check the security state when the exception was generated */
224 assert(get_interrupt_src_ss(flags) == NON_SECURE);
225
226 /* Sanity check the pointer to this cpu's context */
227 assert(handle == cm_get_context(NON_SECURE));
228
229 /* Save the non-secure context before entering SPMC */
230 cm_el1_sysregs_context_save(NON_SECURE);
231#if SPMD_SPM_AT_SEL2
232 cm_el2_sysregs_context_save(NON_SECURE);
233#endif
234
235 /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
236 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
237 write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
238 write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
239 write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
240 write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
241 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
242 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
243 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
244
245 /* Mark current core as handling a secure interrupt. */
246 ctx->secure_interrupt_ongoing = true;
247
248 rc = spmd_spm_core_sync_entry(ctx);
249 if (rc != 0ULL) {
Olivier Deprezba100f22021-11-09 12:37:20 +0100250 ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
Olivier Depreza664c492020-08-05 11:27:42 +0200251 }
252
253 ctx->secure_interrupt_ongoing = false;
254
255 cm_el1_sysregs_context_restore(NON_SECURE);
256#if SPMD_SPM_AT_SEL2
257 cm_el2_sysregs_context_restore(NON_SECURE);
258#endif
259 cm_set_next_eret_context(NON_SECURE);
260
261 SMC_RET0(&ctx->cpu_ctx);
262}
263
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200264#if (EL3_EXCEPTION_HANDLING == 0)
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600265/*******************************************************************************
266 * spmd_group0_interrupt_handler_nwd
267 * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
268 * handling of the interrupt to the platform handler, and return only upon
269 * successfully handling the Group0 interrupt.
270 ******************************************************************************/
271static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
272 uint32_t flags,
273 void *handle,
274 void *cookie)
275{
276 uint32_t intid;
277
278 /* Sanity check the security state when the exception was generated. */
279 assert(get_interrupt_src_ss(flags) == NON_SECURE);
280
281 /* Sanity check the pointer to this cpu's context. */
282 assert(handle == cm_get_context(NON_SECURE));
283
284 assert(id == INTR_ID_UNAVAILABLE);
285
286 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
287
Madhukar Pappireddy2ca75702023-07-12 16:28:05 -0500288 intid = plat_ic_acknowledge_interrupt();
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600289
290 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
291 ERROR("Group0 interrupt %u not handled\n", intid);
292 panic();
293 }
294
Madhukar Pappireddy2ca75702023-07-12 16:28:05 -0500295 /* Deactivate the corresponding Group0 interrupt. */
296 plat_ic_end_of_interrupt(intid);
297
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600298 return 0U;
299}
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200300#endif
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600301
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -0600302/*******************************************************************************
303 * spmd_handle_group0_intr_swd
304 * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
305 * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
306 * interrupt to the platform handler, and returns only upon successfully
307 * handling the Group0 interrupt.
308 ******************************************************************************/
309static uint64_t spmd_handle_group0_intr_swd(void *handle)
310{
311 uint32_t intid;
312
313 /* Sanity check the pointer to this cpu's context */
314 assert(handle == cm_get_context(SECURE));
315
316 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
317
Madhukar Pappireddy2ca75702023-07-12 16:28:05 -0500318 intid = plat_ic_acknowledge_interrupt();
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -0600319
320 /*
321 * TODO: Currently due to a limitation in SPMD implementation, the
322 * platform handler is expected to not delegate handling to NWd while
323 * processing Group0 secure interrupt.
324 */
325 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
326 /* Group0 interrupt was not handled by the platform. */
327 ERROR("Group0 interrupt %u not handled\n", intid);
328 panic();
329 }
330
Madhukar Pappireddy2ca75702023-07-12 16:28:05 -0500331 /* Deactivate the corresponding Group0 interrupt. */
332 plat_ic_end_of_interrupt(intid);
333
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -0600334 /* Return success. */
335 SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
336 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
337 FFA_PARAM_MBZ);
338}
339
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000340#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
341static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
342 unsigned int attr, uintptr_t *align_addr,
343 size_t *align_size)
344{
345 uintptr_t base_addr_align;
346 size_t mapped_size_align;
347 int rc;
348
349 /* Page aligned address and size if necessary */
350 base_addr_align = page_align(base_addr, DOWN);
351 mapped_size_align = page_align(size, UP);
352
353 if ((base_addr != base_addr_align) &&
354 (size == mapped_size_align)) {
355 mapped_size_align += PAGE_SIZE;
356 }
357
358 /*
359 * Map dynamically given region with its aligned base address and
360 * size
361 */
362 rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
363 base_addr_align,
364 mapped_size_align,
365 attr);
366 if (rc == 0) {
367 *align_addr = base_addr_align;
368 *align_size = mapped_size_align;
369 }
370
371 return rc;
372}
373
374static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
375 size_t size)
376{
377 uintptr_t root_base_addr_align, sec_base_addr_align;
378 size_t root_mapped_size_align, sec_mapped_size_align;
379 int rc;
380
381 assert(root_base_addr != 0UL);
382 assert(sec_base_addr != 0UL);
383 assert(size != 0UL);
384
385 /* Map the memory with required attributes */
386 rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
387 &root_base_addr_align,
388 &root_mapped_size_align);
389 if (rc != 0) {
390 ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
391 root_base_addr, rc);
392 panic();
393 }
394
395 rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
396 &sec_base_addr_align, &sec_mapped_size_align);
397 if (rc != 0) {
398 ERROR("%s %s %lu (%d)\n", "Error while mapping",
399 "secure region", sec_base_addr, rc);
400 panic();
401 }
402
403 /* Do copy operation */
404 (void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
405
406 /* Unmap root memory region */
407 rc = mmap_remove_dynamic_region(root_base_addr_align,
408 root_mapped_size_align);
409 if (rc != 0) {
410 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
411 "root region", root_base_addr_align, rc);
412 panic();
413 }
414
415 /* Unmap secure memory region */
416 rc = mmap_remove_dynamic_region(sec_base_addr_align,
417 sec_mapped_size_align);
418 if (rc != 0) {
419 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
420 "secure region", sec_base_addr_align, rc);
421 panic();
422 }
423}
424#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
425
Olivier Depreza664c492020-08-05 11:27:42 +0200426/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200427 * Loads SPMC manifest and inits SPMC.
Achin Gupta86f23532019-10-11 15:41:16 +0100428 ******************************************************************************/
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100429static int spmd_spmc_init(void *pm_addr)
Achin Gupta86f23532019-10-11 15:41:16 +0100430{
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200431 cpu_context_t *cpu_ctx;
432 unsigned int core_id;
Olivier Depreza664c492020-08-05 11:27:42 +0200433 uint32_t ep_attr, flags;
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200434 int rc;
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000435 const struct dyn_cfg_dtb_info_t *image_info __unused;
Achin Gupta86f23532019-10-11 15:41:16 +0100436
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200437 /* Load the SPM Core manifest */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100438 rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
Max Shvetsov745889c2020-02-27 14:54:21 +0000439 if (rc != 0) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200440 WARN("No or invalid SPM Core manifest image provided by BL2\n");
441 return rc;
Achin Gupta86f23532019-10-11 15:41:16 +0100442 }
443
444 /*
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200445 * Ensure that the SPM Core version is compatible with the SPM
446 * Dispatcher version.
Achin Gupta86f23532019-10-11 15:41:16 +0100447 */
J-Alves2672cde2020-05-07 18:42:25 +0100448 if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
449 (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
450 WARN("Unsupported FFA version (%u.%u)\n",
Achin Gupta86f23532019-10-11 15:41:16 +0100451 spmc_attrs.major_version, spmc_attrs.minor_version);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200452 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100453 }
454
J-Alves2672cde2020-05-07 18:42:25 +0100455 VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
Achin Gupta86f23532019-10-11 15:41:16 +0100456 spmc_attrs.minor_version);
457
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200458 VERBOSE("SPM Core run time EL%x.\n",
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000459 SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
Achin Gupta86f23532019-10-11 15:41:16 +0100460
Max Shvetsove79062e2020-03-12 15:16:40 +0000461 /* Validate the SPMC ID, Ensure high bit is set */
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200462 if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
463 SPMC_SECURE_ID_MASK) == 0U) {
464 WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
465 return -EINVAL;
Max Shvetsove79062e2020-03-12 15:16:40 +0000466 }
467
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200468 /* Validate the SPM Core execution state */
Achin Gupta86f23532019-10-11 15:41:16 +0100469 if ((spmc_attrs.exec_state != MODE_RW_64) &&
470 (spmc_attrs.exec_state != MODE_RW_32)) {
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100471 WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
Achin Gupta86f23532019-10-11 15:41:16 +0100472 spmc_attrs.exec_state);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200473 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100474 }
475
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100476 VERBOSE("%s%x.\n", "SPM Core execution state 0x",
477 spmc_attrs.exec_state);
Achin Gupta86f23532019-10-11 15:41:16 +0100478
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000479#if SPMD_SPM_AT_SEL2
480 /* Ensure manifest has not requested AArch32 state in S-EL2 */
481 if (spmc_attrs.exec_state == MODE_RW_32) {
482 WARN("AArch32 state at S-EL2 is not supported.\n");
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200483 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100484 }
485
486 /*
487 * Check if S-EL2 is supported on this system if S-EL2
488 * is required for SPM
489 */
Andre Przywara6dd2d062023-02-22 16:53:50 +0000490 if (!is_feat_sel2_supported()) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200491 WARN("SPM Core run time S-EL2 is not supported.\n");
492 return -EINVAL;
Achin Gupta86f23532019-10-11 15:41:16 +0100493 }
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000494#endif /* SPMD_SPM_AT_SEL2 */
Achin Gupta86f23532019-10-11 15:41:16 +0100495
496 /* Initialise an entrypoint to set up the CPU context */
497 ep_attr = SECURE | EP_ST_ENABLE;
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200498 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
Achin Gupta86f23532019-10-11 15:41:16 +0100499 ep_attr |= EP_EE_BIG;
Max Shvetsov745889c2020-02-27 14:54:21 +0000500 }
501
Achin Gupta86f23532019-10-11 15:41:16 +0100502 SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta86f23532019-10-11 15:41:16 +0100503
504 /*
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200505 * Populate SPSR for SPM Core based upon validated parameters from the
506 * manifest.
Achin Gupta86f23532019-10-11 15:41:16 +0100507 */
508 if (spmc_attrs.exec_state == MODE_RW_32) {
509 spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
510 SPSR_E_LITTLE,
511 DAIF_FIQ_BIT |
512 DAIF_IRQ_BIT |
513 DAIF_ABT_BIT);
514 } else {
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000515
516#if SPMD_SPM_AT_SEL2
517 static const uint32_t runtime_el = MODE_EL2;
518#else
519 static const uint32_t runtime_el = MODE_EL1;
520#endif
521 spmc_ep_info->spsr = SPSR_64(runtime_el,
Achin Gupta86f23532019-10-11 15:41:16 +0100522 MODE_SP_ELX,
523 DISABLE_ALL_EXCEPTIONS);
524 }
525
Manish V Badarkhe2f4279a2023-02-07 11:26:38 +0000526#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
527 image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
528 assert(image_info != NULL);
529
530 if ((image_info->config_addr == 0UL) ||
531 (image_info->secondary_config_addr == 0UL) ||
532 (image_info->config_max_size == 0UL)) {
533 return -EINVAL;
534 }
535
536 /* Copy manifest from root->secure region */
537 spmd_do_sec_cpy(image_info->config_addr,
538 image_info->secondary_config_addr,
539 image_info->config_max_size);
540
541 /* Update ep info of BL32 */
542 assert(spmc_ep_info != NULL);
543 spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
544#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
545
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200546 /* Set an initial SPMC context state for all cores. */
547 for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
548 spm_core_context[core_id].state = SPMC_STATE_OFF;
Max Shvetsov745889c2020-02-27 14:54:21 +0000549
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200550 /* Setup an initial cpu context for the SPMC. */
551 cpu_ctx = &spm_core_context[core_id].cpu_ctx;
552 cm_setup_context(cpu_ctx, spmc_ep_info);
Achin Gupta86f23532019-10-11 15:41:16 +0100553
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200554 /*
555 * Pass the core linear ID to the SPMC through x4.
556 * (TF-A implementation defined behavior helping
557 * a legacy TOS migration to adopt FF-A).
558 */
559 write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
560 }
Achin Gupta86f23532019-10-11 15:41:16 +0100561
Olivier Deprez9afca122019-10-28 09:15:52 +0000562 /* Register power management hooks with PSCI */
563 psci_register_spd_pm_hook(&spmd_pm);
564
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200565 /* Register init function for deferred init. */
Achin Gupta86f23532019-10-11 15:41:16 +0100566 bl31_register_bl32_init(&spmd_init);
567
Olivier Deprez4ab7a4a2021-06-21 09:47:13 +0200568 INFO("SPM Core setup done.\n");
569
Olivier Depreza664c492020-08-05 11:27:42 +0200570 /*
571 * Register an interrupt handler routing secure interrupts to SPMD
572 * while the NWd is running.
573 */
574 flags = 0;
575 set_interrupt_rm_flag(flags, NON_SECURE);
576 rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
577 spmd_secure_interrupt_handler,
578 flags);
579 if (rc != 0) {
580 panic();
581 }
582
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600583 /*
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200584 * Permit configurations where the SPM resides at S-EL1/2 and upon a
585 * Group0 interrupt triggering while the normal world runs, the
586 * interrupt is routed either through the EHF or directly to the SPMD:
587 *
588 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
589 * for handling by spmd_group0_interrupt_handler_nwd.
590 *
591 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
592 *
593 */
594#if (EL3_EXCEPTION_HANDLING == 0)
595 /*
Madhukar Pappireddy89e84562024-03-26 09:21:25 -0500596 * If EL3 interrupts are supported by the platform, register an
597 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
598 * running.
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600599 */
Madhukar Pappireddy89e84562024-03-26 09:21:25 -0500600 if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
601 rc = register_interrupt_type_handler(INTR_TYPE_EL3,
602 spmd_group0_interrupt_handler_nwd,
603 flags);
604 if (rc != 0) {
605 panic();
606 }
Madhukar Pappireddyb494acf2023-03-02 15:34:05 -0600607 }
Olivier Deprez35bbcf22023-06-08 18:23:26 +0200608#endif
609
Achin Gupta86f23532019-10-11 15:41:16 +0100610 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000611}
Achin Gupta86f23532019-10-11 15:41:16 +0100612
Max Shvetsov745889c2020-02-27 14:54:21 +0000613/*******************************************************************************
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200614 * Initialize context of SPM Core.
Max Shvetsov745889c2020-02-27 14:54:21 +0000615 ******************************************************************************/
616int spmd_setup(void)
617{
618 int rc;
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000619 void *spmc_manifest;
620
621 /*
622 * If the SPMC is at EL3, then just initialise it directly. The
623 * shenanigans of when it is at a lower EL are not needed.
624 */
625 if (is_spmc_at_el3()) {
626 /* Allow the SPMC to populate its attributes directly. */
627 spmc_populate_attrs(&spmc_attrs);
628
629 rc = spmc_setup();
630 if (rc != 0) {
Olivier Deprez3d203f42022-11-16 16:46:23 +0100631 WARN("SPMC initialisation failed 0x%x.\n", rc);
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000632 }
Olivier Deprez3d203f42022-11-16 16:46:23 +0100633 return 0;
Marc Bonnici1c33cc32021-11-29 17:57:03 +0000634 }
Achin Gupta86f23532019-10-11 15:41:16 +0100635
Max Shvetsov745889c2020-02-27 14:54:21 +0000636 spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200637 if (spmc_ep_info == NULL) {
638 WARN("No SPM Core image provided by BL2 boot loader.\n");
Olivier Deprez3d203f42022-11-16 16:46:23 +0100639 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000640 }
641
642 /* Under no circumstances will this parameter be 0 */
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200643 assert(spmc_ep_info->pc != 0ULL);
Max Shvetsov745889c2020-02-27 14:54:21 +0000644
645 /*
646 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200647 * be used as a manifest for the SPM Core at the next lower EL/mode.
Max Shvetsov745889c2020-02-27 14:54:21 +0000648 */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100649 spmc_manifest = (void *)spmc_ep_info->args.arg0;
650 if (spmc_manifest == NULL) {
Olivier Deprez3d203f42022-11-16 16:46:23 +0100651 WARN("Invalid or absent SPM Core manifest.\n");
652 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000653 }
654
655 /* Load manifest, init SPMC */
Olivier Deprez69ca84a2020-02-07 15:44:43 +0100656 rc = spmd_spmc_init(spmc_manifest);
Max Shvetsov745889c2020-02-27 14:54:21 +0000657 if (rc != 0) {
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200658 WARN("Booting device without SPM initialization.\n");
Max Shvetsov745889c2020-02-27 14:54:21 +0000659 }
660
Olivier Deprez3d203f42022-11-16 16:46:23 +0100661 return 0;
Max Shvetsov745889c2020-02-27 14:54:21 +0000662}
663
664/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000665 * Forward FF-A SMCs to the other security state.
Max Shvetsov745889c2020-02-27 14:54:21 +0000666 ******************************************************************************/
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000667uint64_t spmd_smc_switch_state(uint32_t smc_fid,
668 bool secure_origin,
669 uint64_t x1,
670 uint64_t x2,
671 uint64_t x3,
672 uint64_t x4,
Olivier Deprezdce23c02022-10-31 12:38:17 +0100673 void *handle,
674 uint64_t flags)
Max Shvetsov745889c2020-02-27 14:54:21 +0000675{
Olivier Deprezebc34772020-04-16 16:59:21 +0200676 unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
677 unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100678
Olivier Deprezdce23c02022-10-31 12:38:17 +0100679#if SPMD_SPM_AT_SEL2
680 if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
681 /*
682 * Set the SVE hint bit in x0 and pass to the lower secure EL,
683 * if it was set by the caller.
684 */
685 smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
686 }
687#endif
688
Max Shvetsov745889c2020-02-27 14:54:21 +0000689 /* Save incoming security state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000690#if SPMD_SPM_AT_SEL2
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200691 if (secure_state_in == NON_SECURE) {
692 cm_el1_sysregs_context_save(secure_state_in);
693 }
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100694 cm_el2_sysregs_context_save(secure_state_in);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200695#else
696 cm_el1_sysregs_context_save(secure_state_in);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000697#endif
Max Shvetsov745889c2020-02-27 14:54:21 +0000698
699 /* Restore outgoing security state */
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000700#if SPMD_SPM_AT_SEL2
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200701 if (secure_state_out == NON_SECURE) {
702 cm_el1_sysregs_context_restore(secure_state_out);
703 }
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100704 cm_el2_sysregs_context_restore(secure_state_out);
Olivier Deprez9a2e5be2021-05-21 18:00:04 +0200705#else
706 cm_el1_sysregs_context_restore(secure_state_out);
Max Shvetsove7fd80e2020-02-25 13:55:00 +0000707#endif
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100708 cm_set_next_eret_context(secure_state_out);
Max Shvetsov745889c2020-02-27 14:54:21 +0000709
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -0800710#if SPMD_SPM_AT_SEL2
711 /*
712 * If SPMC is at SEL2, save additional registers x8-x17, which may
713 * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
714 * Note that technically, all SPMCs can support this, but this code is
715 * under ifdef to minimize breakage in case other SPMCs do not save
716 * and restore x8-x17.
717 * We also need to pass through these registers since not all FF-A ABIs
718 * modify x8-x17, in which case, SMCCC requires that these registers be
719 * preserved, so the SPMD passes through these registers and expects the
720 * SPMC to save and restore (potentially also modify) them.
721 */
722 SMC_RET18(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
723 SMC_GET_GP(handle, CTX_GPREG_X5),
724 SMC_GET_GP(handle, CTX_GPREG_X6),
725 SMC_GET_GP(handle, CTX_GPREG_X7),
726 SMC_GET_GP(handle, CTX_GPREG_X8),
727 SMC_GET_GP(handle, CTX_GPREG_X9),
728 SMC_GET_GP(handle, CTX_GPREG_X10),
729 SMC_GET_GP(handle, CTX_GPREG_X11),
730 SMC_GET_GP(handle, CTX_GPREG_X12),
731 SMC_GET_GP(handle, CTX_GPREG_X13),
732 SMC_GET_GP(handle, CTX_GPREG_X14),
733 SMC_GET_GP(handle, CTX_GPREG_X15),
734 SMC_GET_GP(handle, CTX_GPREG_X16),
735 SMC_GET_GP(handle, CTX_GPREG_X17)
736 );
737
738#else
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100739 SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
Max Shvetsov745889c2020-02-27 14:54:21 +0000740 SMC_GET_GP(handle, CTX_GPREG_X5),
741 SMC_GET_GP(handle, CTX_GPREG_X6),
742 SMC_GET_GP(handle, CTX_GPREG_X7));
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -0800743#endif
Max Shvetsov745889c2020-02-27 14:54:21 +0000744}
745
746/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000747 * Forward SMCs to the other security state.
748 ******************************************************************************/
749static uint64_t spmd_smc_forward(uint32_t smc_fid,
750 bool secure_origin,
751 uint64_t x1,
752 uint64_t x2,
753 uint64_t x3,
754 uint64_t x4,
755 void *cookie,
756 void *handle,
757 uint64_t flags)
758{
759 if (is_spmc_at_el3() && !secure_origin) {
760 return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
761 cookie, handle, flags);
762 }
Olivier Deprezdce23c02022-10-31 12:38:17 +0100763
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000764 return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
Olivier Deprezdce23c02022-10-31 12:38:17 +0100765 handle, flags);
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000766
767}
768
769/*******************************************************************************
J-Alves2672cde2020-05-07 18:42:25 +0100770 * Return FFA_ERROR with specified error code
Max Shvetsov745889c2020-02-27 14:54:21 +0000771 ******************************************************************************/
Raghu Krishnamurthy9d9584f2023-04-22 18:00:02 -0700772uint64_t spmd_ffa_error_return(void *handle, int error_code)
Max Shvetsov745889c2020-02-27 14:54:21 +0000773{
J-Alves64ff9932021-03-01 10:26:59 +0000774 SMC_RET8(handle, (uint32_t) FFA_ERROR,
775 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
J-Alves2672cde2020-05-07 18:42:25 +0100776 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
777 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Gupta86f23532019-10-11 15:41:16 +0100778}
779
Olivier Deprez33e44122020-04-16 17:54:27 +0200780/*******************************************************************************
781 * spmd_check_address_in_binary_image
782 ******************************************************************************/
783bool spmd_check_address_in_binary_image(uint64_t address)
784{
785 assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
786
787 return ((address >= spmc_attrs.load_address) &&
788 (address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
789}
790
Olivier Deprezebc34772020-04-16 16:59:21 +0200791/******************************************************************************
792 * spmd_is_spmc_message
793 *****************************************************************************/
794static bool spmd_is_spmc_message(unsigned int ep)
795{
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000796 if (is_spmc_at_el3()) {
797 return false;
798 }
799
Olivier Deprezebc34772020-04-16 16:59:21 +0200800 return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
801 && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
802}
803
Olivier Deprez33e44122020-04-16 17:54:27 +0200804/******************************************************************************
805 * spmd_handle_spmc_message
806 *****************************************************************************/
Olivier Deprezc7631a52020-03-23 09:53:06 +0100807static int spmd_handle_spmc_message(unsigned long long msg,
808 unsigned long long parm1, unsigned long long parm2,
809 unsigned long long parm3, unsigned long long parm4)
Olivier Deprez33e44122020-04-16 17:54:27 +0200810{
811 VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
812 msg, parm1, parm2, parm3, parm4);
813
Olivier Deprez33e44122020-04-16 17:54:27 +0200814 return -EINVAL;
815}
816
Achin Gupta86f23532019-10-11 15:41:16 +0100817/*******************************************************************************
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000818 * This function forwards FF-A SMCs to either the main SPMD handler or the
819 * SPMC at EL3, depending on the origin security state, if enabled.
820 ******************************************************************************/
821uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
822 uint64_t x1,
823 uint64_t x2,
824 uint64_t x3,
825 uint64_t x4,
826 void *cookie,
827 void *handle,
828 uint64_t flags)
829{
830 if (is_spmc_at_el3()) {
831 /*
832 * If we have an SPMC at EL3 allow handling of the SMC first.
833 * The SPMC will call back through to SPMD handler if required.
834 */
835 if (is_caller_secure(flags)) {
836 return spmc_smc_handler(smc_fid,
837 is_caller_secure(flags),
838 x1, x2, x3, x4, cookie,
839 handle, flags);
840 }
841 }
842 return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
843 handle, flags);
844}
845
846/*******************************************************************************
J-Alves2672cde2020-05-07 18:42:25 +0100847 * This function handles all SMCs in the range reserved for FFA. Each call is
Achin Gupta86f23532019-10-11 15:41:16 +0100848 * either forwarded to the other security state or handled by the SPM dispatcher
849 ******************************************************************************/
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200850uint64_t spmd_smc_handler(uint32_t smc_fid,
851 uint64_t x1,
852 uint64_t x2,
853 uint64_t x3,
854 uint64_t x4,
855 void *cookie,
856 void *handle,
Achin Gupta86f23532019-10-11 15:41:16 +0100857 uint64_t flags)
858{
Olivier Deprezeae45962021-01-19 15:06:47 +0100859 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200860 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100861 bool secure_origin;
J-Alves8676f242023-10-04 17:16:45 +0100862 int ret;
J-Alves4c95c702020-05-26 14:03:05 +0100863 uint32_t input_version;
Achin Gupta86f23532019-10-11 15:41:16 +0100864
865 /* Determine which security state this SMC originated from */
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100866 secure_origin = is_caller_secure(flags);
Achin Gupta86f23532019-10-11 15:41:16 +0100867
Scott Brandene5dcf982020-08-25 13:49:32 -0700868 VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
869 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
870 linear_id, smc_fid, x1, x2, x3, x4,
871 SMC_GET_GP(handle, CTX_GPREG_X5),
872 SMC_GET_GP(handle, CTX_GPREG_X6),
873 SMC_GET_GP(handle, CTX_GPREG_X7));
Achin Gupta86f23532019-10-11 15:41:16 +0100874
Raghu Krishnamurthy43fda972023-04-22 11:28:38 -0700875 /*
876 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
877 * return, we don't expect any other FF-A ABIs to be called between
878 * calls to FFA_PARTITION_INFO_GET_REGS.
879 */
880 if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
881 assert(secure_origin);
882 spmd_spm_core_sync_exit(0ULL);
883 }
884
Achin Gupta86f23532019-10-11 15:41:16 +0100885 switch (smc_fid) {
J-Alves2672cde2020-05-07 18:42:25 +0100886 case FFA_ERROR:
Achin Gupta86f23532019-10-11 15:41:16 +0100887 /*
888 * Check if this is the first invocation of this interface on
Olivier Deprez2bae35f2020-04-16 13:39:06 +0200889 * this CPU. If so, then indicate that the SPM Core initialised
Achin Gupta86f23532019-10-11 15:41:16 +0100890 * unsuccessfully.
891 */
Olivier Deprez7c016332019-10-28 09:03:13 +0000892 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Achin Gupta86f23532019-10-11 15:41:16 +0100893 spmd_spm_core_sync_exit(x2);
Max Shvetsov745889c2020-02-27 14:54:21 +0000894 }
Achin Gupta86f23532019-10-11 15:41:16 +0100895
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -0800896 /*
897 * If there was an SPMD logical partition direct request on-going,
898 * return back to the SPMD logical partition so the error can be
899 * consumed.
900 */
901 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
902 assert(secure_origin);
903 spmd_spm_core_sync_exit(0ULL);
904 }
905
Olivier Deprez41ff36a2019-12-23 16:21:12 +0100906 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000907 x1, x2, x3, x4, cookie,
908 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +0100909 break; /* not reached */
910
J-Alves2672cde2020-05-07 18:42:25 +0100911 case FFA_VERSION:
J-Alves4c95c702020-05-26 14:03:05 +0100912 input_version = (uint32_t)(0xFFFFFFFF & x1);
Achin Gupta86f23532019-10-11 15:41:16 +0100913 /*
J-Alves4c95c702020-05-26 14:03:05 +0100914 * If caller is secure and SPMC was initialized,
915 * return FFA_VERSION of SPMD.
916 * If caller is non secure and SPMC was initialized,
Marc Bonnici815d1012021-12-08 14:27:40 +0000917 * forward to the EL3 SPMC if enabled, otherwise return
918 * the SPMC version if implemented at a lower EL.
J-Alves4c95c702020-05-26 14:03:05 +0100919 * Sanity check to "input_version".
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000920 * If the EL3 SPMC is enabled, ignore the SPMC state as
921 * this is not used.
Achin Gupta86f23532019-10-11 15:41:16 +0100922 */
J-Alves4c95c702020-05-26 14:03:05 +0100923 if ((input_version & FFA_VERSION_BIT31_MASK) ||
Marc Bonnicida2c9e12021-11-29 18:02:45 +0000924 (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
J-Alves4c95c702020-05-26 14:03:05 +0100925 ret = FFA_ERROR_NOT_SUPPORTED;
926 } else if (!secure_origin) {
Marc Bonnici815d1012021-12-08 14:27:40 +0000927 if (is_spmc_at_el3()) {
928 /*
929 * Forward the call directly to the EL3 SPMC, if
930 * enabled, as we don't need to wrap the call in
931 * a direct request.
932 */
933 return spmd_smc_forward(smc_fid, secure_origin,
934 x1, x2, x3, x4, cookie,
935 handle, flags);
936 }
937
Daniel Boulby9460a232021-12-09 11:20:13 +0000938 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
939 uint64_t rc;
940
941 if (spmc_attrs.major_version == 1 &&
942 spmc_attrs.minor_version == 0) {
943 ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
944 spmc_attrs.minor_version);
945 SMC_RET8(handle, (uint32_t)ret,
946 FFA_TARGET_INFO_MBZ,
947 FFA_TARGET_INFO_MBZ,
948 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
949 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
950 FFA_PARAM_MBZ);
951 break;
952 }
953 /* Save non-secure system registers context */
954 cm_el1_sysregs_context_save(NON_SECURE);
955#if SPMD_SPM_AT_SEL2
956 cm_el2_sysregs_context_save(NON_SECURE);
957#endif
958
959 /*
960 * The incoming request has FFA_VERSION as X0 smc_fid
961 * and requested version in x1. Prepare a direct request
962 * from SPMD to SPMC with FFA_VERSION framework function
963 * identifier in X2 and requested version in X3.
964 */
965 spmd_build_spmc_message(gpregs,
966 SPMD_FWK_MSG_FFA_VERSION_REQ,
967 input_version);
968
Olivier Deprez4911eb82023-07-10 11:04:30 +0200969 /*
970 * Ensure x8-x17 NS GP register values are untouched when returning
971 * from the SPMC.
972 */
973 write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
974 write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
975 write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
976 write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
977 write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
978 write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
979 write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
980 write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
981 write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
982 write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
983
Daniel Boulby9460a232021-12-09 11:20:13 +0000984 rc = spmd_spm_core_sync_entry(ctx);
985
986 if ((rc != 0ULL) ||
987 (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
988 FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
989 (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
Marc Bonnici25f4b542022-04-12 17:18:13 +0100990 (FFA_FWK_MSG_BIT |
Daniel Boulby9460a232021-12-09 11:20:13 +0000991 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
992 ERROR("Failed to forward FFA_VERSION\n");
993 ret = FFA_ERROR_NOT_SUPPORTED;
994 } else {
995 ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
996 }
997
998 /*
Olivier Deprez4911eb82023-07-10 11:04:30 +0200999 * x0-x4 are updated by spmd_smc_forward below.
1000 * Zero out x5-x7 in the FFA_VERSION response.
1001 */
1002 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
1003 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
1004 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1005
1006 /*
Daniel Boulby9460a232021-12-09 11:20:13 +00001007 * Return here after SPMC has handled FFA_VERSION.
1008 * The returned SPMC version is held in X3.
1009 * Forward this version in X0 to the non-secure caller.
1010 */
1011 return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
1012 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001013 FFA_PARAM_MBZ, cookie, gpregs,
1014 flags);
J-Alves4c95c702020-05-26 14:03:05 +01001015 } else {
J-Alves64ff9932021-03-01 10:26:59 +00001016 ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1017 FFA_VERSION_MINOR);
J-Alves4c95c702020-05-26 14:03:05 +01001018 }
1019
J-Alves64ff9932021-03-01 10:26:59 +00001020 SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1021 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1022 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Gupta86f23532019-10-11 15:41:16 +01001023 break; /* not reached */
1024
J-Alves2672cde2020-05-07 18:42:25 +01001025 case FFA_FEATURES:
Achin Gupta86f23532019-10-11 15:41:16 +01001026 /*
1027 * This is an optional interface. Do the minimal checks and
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001028 * forward to SPM Core which will handle it if implemented.
Achin Gupta86f23532019-10-11 15:41:16 +01001029 */
1030
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001031 /* Forward SMC from Normal world to the SPM Core */
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001032 if (!secure_origin) {
1033 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001034 x1, x2, x3, x4, cookie,
1035 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +01001036 }
Max Shvetsov745889c2020-02-27 14:54:21 +00001037
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001038 /*
1039 * Return success if call was from secure world i.e. all
J-Alves2672cde2020-05-07 18:42:25 +01001040 * FFA functions are supported. This is essentially a
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001041 * nop.
1042 */
J-Alves2672cde2020-05-07 18:42:25 +01001043 SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001044 SMC_GET_GP(handle, CTX_GPREG_X5),
1045 SMC_GET_GP(handle, CTX_GPREG_X6),
1046 SMC_GET_GP(handle, CTX_GPREG_X7));
1047
Achin Gupta86f23532019-10-11 15:41:16 +01001048 break; /* not reached */
1049
J-Alves2672cde2020-05-07 18:42:25 +01001050 case FFA_ID_GET:
Max Shvetsove79062e2020-03-12 15:16:40 +00001051 /*
J-Alves2672cde2020-05-07 18:42:25 +01001052 * Returns the ID of the calling FFA component.
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001053 */
Max Shvetsove79062e2020-03-12 15:16:40 +00001054 if (!secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +01001055 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1056 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1057 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1058 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1059 FFA_PARAM_MBZ);
Max Shvetsove79062e2020-03-12 15:16:40 +00001060 }
1061
J-Alves2672cde2020-05-07 18:42:25 +01001062 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1063 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1064 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1065 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1066 FFA_PARAM_MBZ);
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001067
Max Shvetsove79062e2020-03-12 15:16:40 +00001068 break; /* not reached */
1069
Olivier Deprezeae45962021-01-19 15:06:47 +01001070 case FFA_SECONDARY_EP_REGISTER_SMC64:
1071 if (secure_origin) {
1072 ret = spmd_pm_secondary_ep_register(x1);
1073
1074 if (ret < 0) {
1075 SMC_RET8(handle, FFA_ERROR_SMC64,
1076 FFA_TARGET_INFO_MBZ, ret,
1077 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1078 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1079 FFA_PARAM_MBZ);
1080 } else {
1081 SMC_RET8(handle, FFA_SUCCESS_SMC64,
1082 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1083 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1084 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1085 FFA_PARAM_MBZ);
1086 }
1087 }
1088
1089 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1090 break; /* Not reached */
1091
Daniel Boulby27f35df2021-02-03 12:13:19 +00001092 case FFA_SPM_ID_GET:
1093 if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
1094 return spmd_ffa_error_return(handle,
1095 FFA_ERROR_NOT_SUPPORTED);
1096 }
1097 /*
1098 * Returns the ID of the SPMC or SPMD depending on the FF-A
1099 * instance where this function is invoked
1100 */
1101 if (!secure_origin) {
1102 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1103 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1104 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1105 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1106 FFA_PARAM_MBZ);
1107 }
1108 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1109 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
1110 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1111 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1112 FFA_PARAM_MBZ);
1113
1114 break; /* not reached */
1115
Olivier Deprez33e44122020-04-16 17:54:27 +02001116 case FFA_MSG_SEND_DIRECT_REQ_SMC32:
Shruti3d859672022-06-09 11:03:11 +01001117 case FFA_MSG_SEND_DIRECT_REQ_SMC64:
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -08001118 /*
1119 * Regardless of secure_origin, SPMD logical partitions cannot
1120 * handle direct messages. They can only initiate direct
1121 * messages and consume direct responses or errors.
1122 */
1123 if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
1124 is_spmd_lp_id(ffa_endpoint_destination(x1))) {
1125 return spmd_ffa_error_return(handle,
1126 FFA_ERROR_INVALID_PARAMETER
1127 );
1128 }
1129
1130 /*
1131 * When there is an ongoing SPMD logical partition direct
1132 * request, there cannot be another direct request. Return
1133 * error in this case. Panic'ing is an option but that does
1134 * not provide the opportunity for caller to abort based on
1135 * error codes.
1136 */
1137 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1138 assert(secure_origin);
1139 return spmd_ffa_error_return(handle,
1140 FFA_ERROR_DENIED);
1141 }
1142
Shruti3d859672022-06-09 11:03:11 +01001143 if (!secure_origin) {
1144 /* Validate source endpoint is non-secure for non-secure caller. */
1145 if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1146 return spmd_ffa_error_return(handle,
1147 FFA_ERROR_INVALID_PARAMETER);
1148 }
1149 }
Olivier Deprez33e44122020-04-16 17:54:27 +02001150 if (secure_origin && spmd_is_spmc_message(x1)) {
1151 ret = spmd_handle_spmc_message(x3, x4,
1152 SMC_GET_GP(handle, CTX_GPREG_X5),
1153 SMC_GET_GP(handle, CTX_GPREG_X6),
1154 SMC_GET_GP(handle, CTX_GPREG_X7));
1155
1156 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1157 FFA_TARGET_INFO_MBZ, ret,
1158 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1159 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1160 FFA_PARAM_MBZ);
1161 } else {
1162 /* Forward direct message to the other world */
1163 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001164 x1, x2, x3, x4, cookie,
1165 handle, flags);
Olivier Deprez33e44122020-04-16 17:54:27 +02001166 }
1167 break; /* Not reached */
1168
Kathleen Capella9d826a12023-07-31 14:45:58 -04001169 case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1170 if (!secure_origin) {
1171 /* Validate source endpoint is non-secure for non-secure caller. */
1172 if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1173 return spmd_ffa_error_return(handle,
1174 FFA_ERROR_INVALID_PARAMETER);
1175 }
1176 }
1177 /* FFA_MSG_SEND_DIRECT_REQ2 not used for framework messages. */
1178 if (secure_origin && spmd_is_spmc_message(x1)) {
1179 return spmd_ffa_error_return(handle, FFA_ERROR_INVALID_PARAMETER);
1180 } else {
1181 /* Forward direct message to the other world */
1182 return spmd_smc_forward(smc_fid, secure_origin,
1183 x1, x2, x3, x4, cookie,
1184 handle, flags);
1185 }
1186 break; /* Not reached */
1187
Olivier Deprez33e44122020-04-16 17:54:27 +02001188 case FFA_MSG_SEND_DIRECT_RESP_SMC32:
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -08001189 case FFA_MSG_SEND_DIRECT_RESP_SMC64:
1190 if (secure_origin && (spmd_is_spmc_message(x1) ||
1191 is_spmd_logical_sp_dir_req_in_progress(ctx))) {
Olivier Depreza664c492020-08-05 11:27:42 +02001192 spmd_spm_core_sync_exit(0ULL);
Olivier Deprez33e44122020-04-16 17:54:27 +02001193 } else {
1194 /* Forward direct message to the other world */
1195 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001196 x1, x2, x3, x4, cookie,
1197 handle, flags);
Olivier Deprez33e44122020-04-16 17:54:27 +02001198 }
1199 break; /* Not reached */
Kathleen Capellaa85a9d12023-09-08 17:45:45 -04001200 case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1201 /* Forward direct message to the other world */
1202 return spmd_smc_forward(smc_fid, secure_origin,
1203 x1, x2, x3, x4, cookie,
1204 handle, flags);
1205 break; /* Not reached */
J-Alves2672cde2020-05-07 18:42:25 +01001206 case FFA_RX_RELEASE:
1207 case FFA_RXTX_MAP_SMC32:
1208 case FFA_RXTX_MAP_SMC64:
1209 case FFA_RXTX_UNMAP:
Ruari Phipps93dff702020-07-28 10:33:35 +01001210 case FFA_PARTITION_INFO_GET:
J-Alves2621cfd2021-03-11 17:46:47 +00001211#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1212 case FFA_NOTIFICATION_BITMAP_CREATE:
1213 case FFA_NOTIFICATION_BITMAP_DESTROY:
1214 case FFA_NOTIFICATION_BIND:
1215 case FFA_NOTIFICATION_UNBIND:
1216 case FFA_NOTIFICATION_SET:
1217 case FFA_NOTIFICATION_GET:
1218 case FFA_NOTIFICATION_INFO_GET:
1219 case FFA_NOTIFICATION_INFO_GET_SMC64:
Federico Recanatieecb4b02022-02-03 17:22:37 +01001220 case FFA_MSG_SEND2:
Federico Recanati5c7c5c42022-03-18 10:30:00 +01001221 case FFA_RX_ACQUIRE:
J-Alves2621cfd2021-03-11 17:46:47 +00001222#endif
Federico Recanatieecb4b02022-02-03 17:22:37 +01001223 case FFA_MSG_RUN:
Ruari Phipps93dff702020-07-28 10:33:35 +01001224 /*
Federico Recanatieecb4b02022-02-03 17:22:37 +01001225 * Above calls should be invoked only by the Normal world and
1226 * must not be forwarded from Secure world to Normal world.
Ruari Phipps93dff702020-07-28 10:33:35 +01001227 */
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001228 if (secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +01001229 return spmd_ffa_error_return(handle,
Ruari Phipps93dff702020-07-28 10:33:35 +01001230 FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001231 }
1232
Boyan Karatotev87266002022-11-18 14:17:17 +00001233 /* Forward the call to the other world */
1234 /* fallthrough */
J-Alves2672cde2020-05-07 18:42:25 +01001235 case FFA_MSG_SEND:
J-Alves2672cde2020-05-07 18:42:25 +01001236 case FFA_MEM_DONATE_SMC32:
1237 case FFA_MEM_DONATE_SMC64:
1238 case FFA_MEM_LEND_SMC32:
1239 case FFA_MEM_LEND_SMC64:
1240 case FFA_MEM_SHARE_SMC32:
1241 case FFA_MEM_SHARE_SMC64:
1242 case FFA_MEM_RETRIEVE_REQ_SMC32:
1243 case FFA_MEM_RETRIEVE_REQ_SMC64:
1244 case FFA_MEM_RETRIEVE_RESP:
1245 case FFA_MEM_RELINQUISH:
1246 case FFA_MEM_RECLAIM:
Marc Bonnici9fa01e92021-09-23 09:44:14 +01001247 case FFA_MEM_FRAG_TX:
1248 case FFA_MEM_FRAG_RX:
J-Alves2672cde2020-05-07 18:42:25 +01001249 case FFA_SUCCESS_SMC32:
1250 case FFA_SUCCESS_SMC64:
Achin Gupta86f23532019-10-11 15:41:16 +01001251 /*
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -08001252 * If there is an ongoing direct request from an SPMD logical
1253 * partition, return an error.
Achin Gupta86f23532019-10-11 15:41:16 +01001254 */
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -08001255 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1256 assert(secure_origin);
1257 return spmd_ffa_error_return(handle,
1258 FFA_ERROR_DENIED);
1259 }
Achin Gupta86f23532019-10-11 15:41:16 +01001260
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001261 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001262 x1, x2, x3, x4, cookie,
1263 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +01001264 break; /* not reached */
1265
J-Alves2672cde2020-05-07 18:42:25 +01001266 case FFA_MSG_WAIT:
Achin Gupta86f23532019-10-11 15:41:16 +01001267 /*
1268 * Check if this is the first invocation of this interface on
1269 * this CPU from the Secure world. If so, then indicate that the
Olivier Deprez2bae35f2020-04-16 13:39:06 +02001270 * SPM Core initialised successfully.
Achin Gupta86f23532019-10-11 15:41:16 +01001271 */
Olivier Deprez7c016332019-10-28 09:03:13 +00001272 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Olivier Depreza664c492020-08-05 11:27:42 +02001273 spmd_spm_core_sync_exit(0ULL);
Achin Gupta86f23532019-10-11 15:41:16 +01001274 }
1275
Boyan Karatotev87266002022-11-18 14:17:17 +00001276 /* Forward the call to the other world */
1277 /* fallthrough */
Olivier Deprezae18caf2021-04-02 11:09:10 +02001278 case FFA_INTERRUPT:
J-Alves2672cde2020-05-07 18:42:25 +01001279 case FFA_MSG_YIELD:
Achin Gupta86f23532019-10-11 15:41:16 +01001280 /* This interface must be invoked only by the Secure world */
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001281 if (!secure_origin) {
J-Alves2672cde2020-05-07 18:42:25 +01001282 return spmd_ffa_error_return(handle,
1283 FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001284 }
1285
Raghu Krishnamurthy6a305142023-03-03 06:41:29 -08001286 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1287 assert(secure_origin);
1288 return spmd_ffa_error_return(handle,
1289 FFA_ERROR_DENIED);
1290 }
1291
Olivier Deprez41ff36a2019-12-23 16:21:12 +01001292 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicida2c9e12021-11-29 18:02:45 +00001293 x1, x2, x3, x4, cookie,
1294 handle, flags);
Achin Gupta86f23532019-10-11 15:41:16 +01001295 break; /* not reached */
1296
Olivier Depreza664c492020-08-05 11:27:42 +02001297 case FFA_NORMAL_WORLD_RESUME:
1298 if (secure_origin && ctx->secure_interrupt_ongoing) {
1299 spmd_spm_core_sync_exit(0ULL);
1300 } else {
1301 return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
1302 }
1303 break; /* Not reached */
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -08001304#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1305 case FFA_PARTITION_INFO_GET_REGS_SMC64:
1306 if (secure_origin) {
Raghu Krishnamurthy9d9584f2023-04-22 18:00:02 -07001307 return spmd_el3_populate_logical_partition_info(handle, x1,
1308 x2, x3);
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -08001309 }
Olivier Depreza664c492020-08-05 11:27:42 +02001310
Raghu Krishnamurthy435f11c2022-12-25 13:02:00 -08001311 /* Call only supported with SMCCC 1.2+ */
1312 if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1313 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1314 }
1315
1316 return spmd_smc_forward(smc_fid, secure_origin,
1317 x1, x2, x3, x4, cookie,
1318 handle, flags);
1319 break; /* Not reached */
1320#endif
Shruti Guptaa5a1cbd2023-01-19 21:50:55 +00001321 case FFA_CONSOLE_LOG_SMC32:
1322 case FFA_CONSOLE_LOG_SMC64:
1323 /* This interface must not be forwarded to other worlds. */
1324 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1325 break; /* not reached */
1326
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -06001327 case FFA_EL3_INTR_HANDLE:
1328 if (secure_origin) {
1329 return spmd_handle_group0_intr_swd(handle);
1330 } else {
Madhukar Pappireddy2ca75702023-07-12 16:28:05 -05001331 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
Madhukar Pappireddy41416cc2023-03-02 16:04:38 -06001332 }
Achin Gupta86f23532019-10-11 15:41:16 +01001333 default:
1334 WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
J-Alves2672cde2020-05-07 18:42:25 +01001335 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
Achin Gupta86f23532019-10-11 15:41:16 +01001336 }
1337}