blob: fd84d9f676ea3d13bdb54a2fbfe13347ff01300d [file] [log] [blame]
Achin Guptabdd25962019-10-11 15:41:16 +01001/*
Kathleen Capellacc6047b2023-07-31 14:45:58 -04002 * Copyright (c) 2020-2024, Arm Limited and Contributors. All rights reserved.
Achin Guptabdd25962019-10-11 15:41:16 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
Scott Branden4ce3e992020-08-25 13:49:32 -07009#include <inttypes.h>
10#include <stdint.h>
Achin Guptabdd25962019-10-11 15:41:16 +010011#include <string.h>
12
13#include <arch_helpers.h>
Olivier Deprez52696942020-04-16 13:39:06 +020014#include <arch/aarch64/arch_features.h>
Achin Guptabdd25962019-10-11 15:41:16 +010015#include <bl31/bl31.h>
Olivier Deprez8cb99c32020-08-05 11:27:42 +020016#include <bl31/interrupt_mgmt.h>
Achin Guptabdd25962019-10-11 15:41:16 +010017#include <common/debug.h>
18#include <common/runtime_svc.h>
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +000019#include <common/tbbr/tbbr_img_def.h>
Achin Guptabdd25962019-10-11 15:41:16 +010020#include <lib/el3_runtime/context_mgmt.h>
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +000021#include <lib/fconf/fconf.h>
22#include <lib/fconf/fconf_dyn_cfg_getter.h>
Achin Guptabdd25962019-10-11 15:41:16 +010023#include <lib/smccc.h>
24#include <lib/spinlock.h>
25#include <lib/utils.h>
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +000026#include <lib/xlat_tables/xlat_tables_v2.h>
Achin Guptabdd25962019-10-11 15:41:16 +010027#include <plat/common/common_def.h>
28#include <plat/common/platform.h>
29#include <platform_def.h>
Raghu Krishnamurthy890b5082023-02-25 13:26:10 -080030#include <services/el3_spmd_logical_sp.h>
J-Alves662af362020-05-07 18:42:25 +010031#include <services/ffa_svc.h>
Marc Bonnici6da76072021-11-29 17:57:03 +000032#include <services/spmc_svc.h>
Achin Guptabdd25962019-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 Deprez52696942020-04-16 13:39:06 +020040static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
Achin Guptabdd25962019-10-11 15:41:16 +010041
42/*******************************************************************************
Marc Bonnici6da76072021-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 Guptabdd25962019-10-11 15:41:16 +010045 ******************************************************************************/
Olivier Deprez52696942020-04-16 13:39:06 +020046static spmc_manifest_attribute_t spmc_attrs;
Max Shvetsov0f14d022020-02-27 14:54:21 +000047
48/*******************************************************************************
49 * 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 Deprez52696942020-04-16 13:39:06 +020055 * SPM Core context on current CPU get helper.
Max Shvetsov0f14d022020-02-27 14:54:21 +000056 ******************************************************************************/
Olivier Deprez52696942020-04-16 13:39:06 +020057spmd_spm_core_context_t *spmd_get_context(void)
58{
Olivier Deprezc8cea3b2024-06-07 08:51:20 +020059 return &spm_core_context[plat_my_core_pos()];
Olivier Deprez52696942020-04-16 13:39:06 +020060}
Achin Guptabdd25962019-10-11 15:41:16 +010061
62/*******************************************************************************
Olivier Depreza92bc732020-03-23 09:53:06 +010063 * SPM Core ID getter.
64 ******************************************************************************/
65uint16_t spmd_spmc_id_get(void)
66{
67 return spmc_attrs.spmc_id;
68}
69
70/*******************************************************************************
Olivier Deprez52696942020-04-16 13:39:06 +020071 * Static function declaration.
72 ******************************************************************************/
73static int32_t spmd_init(void);
Olivier Deprez23d5ba82020-02-07 15:44:43 +010074static int spmd_spmc_init(void *pm_addr);
Raghu Krishnamurthy95f7f6d2023-04-22 18:00:02 -070075
Olivier Deprez52696942020-04-16 13:39:06 +020076static uint64_t spmd_smc_forward(uint32_t smc_fid,
77 bool secure_origin,
78 uint64_t x1,
79 uint64_t x2,
80 uint64_t x3,
81 uint64_t x4,
Marc Bonnicibb01a672021-11-29 18:02:45 +000082 void *cookie,
83 void *handle,
84 uint64_t flags);
Olivier Deprez52696942020-04-16 13:39:06 +020085
Daniel Boulby9944f552021-12-09 11:20:13 +000086/******************************************************************************
87 * Builds an SPMD to SPMC direct message request.
88 *****************************************************************************/
89void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
90 unsigned long long message)
91{
92 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
93 write_ctx_reg(gpregs, CTX_GPREG_X1,
94 (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
95 spmd_spmc_id_get());
96 write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
97 write_ctx_reg(gpregs, CTX_GPREG_X3, message);
Olivier Deprez76d53ee2023-07-10 11:04:30 +020098
99 /* Zero out x4-x7 for the direct request emitted towards the SPMC. */
100 write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
101 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
102 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
103 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
Daniel Boulby9944f552021-12-09 11:20:13 +0000104}
105
106
Olivier Deprez52696942020-04-16 13:39:06 +0200107/*******************************************************************************
108 * This function takes an SPMC context pointer and performs a synchronous
109 * SPMC entry.
Achin Guptabdd25962019-10-11 15:41:16 +0100110 ******************************************************************************/
111uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
112{
113 uint64_t rc;
114
115 assert(spmc_ctx != NULL);
116
117 cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
118
119 /* Restore the context assigned above */
Max Shvetsov033039f2020-02-25 13:55:00 +0000120#if SPMD_SPM_AT_SEL2
Max Shvetsov28f39f02020-02-25 13:56:19 +0000121 cm_el2_sysregs_context_restore(SECURE);
Olivier Deprez678ce222021-05-21 18:00:04 +0200122#else
123 cm_el1_sysregs_context_restore(SECURE);
Max Shvetsov033039f2020-02-25 13:55:00 +0000124#endif
Achin Guptabdd25962019-10-11 15:41:16 +0100125 cm_set_next_eret_context(SECURE);
126
Max Shvetsov033039f2020-02-25 13:55:00 +0000127 /* Enter SPMC */
Achin Guptabdd25962019-10-11 15:41:16 +0100128 rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
129
130 /* Save secure state */
Max Shvetsov033039f2020-02-25 13:55:00 +0000131#if SPMD_SPM_AT_SEL2
Max Shvetsov28f39f02020-02-25 13:56:19 +0000132 cm_el2_sysregs_context_save(SECURE);
Olivier Deprez678ce222021-05-21 18:00:04 +0200133#else
134 cm_el1_sysregs_context_save(SECURE);
Max Shvetsov033039f2020-02-25 13:55:00 +0000135#endif
Achin Guptabdd25962019-10-11 15:41:16 +0100136
137 return rc;
138}
139
140/*******************************************************************************
Olivier Deprez52696942020-04-16 13:39:06 +0200141 * This function returns to the place where spmd_spm_core_sync_entry() was
Achin Guptabdd25962019-10-11 15:41:16 +0100142 * called originally.
143 ******************************************************************************/
144__dead2 void spmd_spm_core_sync_exit(uint64_t rc)
145{
Olivier Deprez52696942020-04-16 13:39:06 +0200146 spmd_spm_core_context_t *ctx = spmd_get_context();
Achin Guptabdd25962019-10-11 15:41:16 +0100147
Olivier Deprez52696942020-04-16 13:39:06 +0200148 /* Get current CPU context from SPMC context */
Achin Guptabdd25962019-10-11 15:41:16 +0100149 assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
150
151 /*
152 * The SPMD must have initiated the original request through a
153 * synchronous entry into SPMC. Jump back to the original C runtime
154 * context with the value of rc in x0;
155 */
156 spmd_spm_core_exit(ctx->c_rt_ctx, rc);
157
158 panic();
159}
160
161/*******************************************************************************
Olivier Deprez52696942020-04-16 13:39:06 +0200162 * Jump to the SPM Core for the first time.
Achin Guptabdd25962019-10-11 15:41:16 +0100163 ******************************************************************************/
164static int32_t spmd_init(void)
165{
Olivier Deprez52696942020-04-16 13:39:06 +0200166 spmd_spm_core_context_t *ctx = spmd_get_context();
167 uint64_t rc;
Achin Guptabdd25962019-10-11 15:41:16 +0100168
Olivier Deprez52696942020-04-16 13:39:06 +0200169 VERBOSE("SPM Core init start.\n");
Olivier Deprez9dcf63d2019-10-28 09:03:13 +0000170
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200171 /* Primary boot core enters the SPMC for initialization. */
172 ctx->state = SPMC_STATE_ON_PENDING;
Achin Guptabdd25962019-10-11 15:41:16 +0100173
174 rc = spmd_spm_core_sync_entry(ctx);
Olivier Deprez52696942020-04-16 13:39:06 +0200175 if (rc != 0ULL) {
Scott Branden4ce3e992020-08-25 13:49:32 -0700176 ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
Olivier Deprez52696942020-04-16 13:39:06 +0200177 return 0;
Achin Guptabdd25962019-10-11 15:41:16 +0100178 }
179
Olivier Deprez9dcf63d2019-10-28 09:03:13 +0000180 ctx->state = SPMC_STATE_ON;
181
Olivier Deprez52696942020-04-16 13:39:06 +0200182 VERBOSE("SPM Core init end.\n");
Achin Guptabdd25962019-10-11 15:41:16 +0100183
Raghu Krishnamurthy890b5082023-02-25 13:26:10 -0800184 spmd_logical_sp_set_spmc_initialized();
185 rc = spmd_logical_sp_init();
186 if (rc != 0) {
187 WARN("SPMD Logical partitions failed init.\n");
188 }
189
Achin Guptabdd25962019-10-11 15:41:16 +0100190 return 1;
191}
192
193/*******************************************************************************
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200194 * spmd_secure_interrupt_handler
195 * Enter the SPMC for further handling of the secure interrupt by the SPMC
196 * itself or a Secure Partition.
197 ******************************************************************************/
198static uint64_t spmd_secure_interrupt_handler(uint32_t id,
199 uint32_t flags,
200 void *handle,
201 void *cookie)
202{
203 spmd_spm_core_context_t *ctx = spmd_get_context();
204 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
205 unsigned int linear_id = plat_my_core_pos();
206 int64_t rc;
207
208 /* Sanity check the security state when the exception was generated */
209 assert(get_interrupt_src_ss(flags) == NON_SECURE);
210
211 /* Sanity check the pointer to this cpu's context */
212 assert(handle == cm_get_context(NON_SECURE));
213
214 /* Save the non-secure context before entering SPMC */
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200215#if SPMD_SPM_AT_SEL2
216 cm_el2_sysregs_context_save(NON_SECURE);
Madhukar Pappireddy2d960a12024-01-29 16:43:56 -0600217#else
218 cm_el1_sysregs_context_save(NON_SECURE);
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200219#endif
220
221 /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
222 write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
223 write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
224 write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
225 write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
226 write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
227 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
228 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
229 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
230
231 /* Mark current core as handling a secure interrupt. */
232 ctx->secure_interrupt_ongoing = true;
233
234 rc = spmd_spm_core_sync_entry(ctx);
235 if (rc != 0ULL) {
Olivier Deprez0c23e6f2021-11-09 12:37:20 +0100236 ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200237 }
238
239 ctx->secure_interrupt_ongoing = false;
240
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200241#if SPMD_SPM_AT_SEL2
242 cm_el2_sysregs_context_restore(NON_SECURE);
Madhukar Pappireddy2d960a12024-01-29 16:43:56 -0600243#else
244 cm_el1_sysregs_context_restore(NON_SECURE);
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200245#endif
246 cm_set_next_eret_context(NON_SECURE);
247
248 SMC_RET0(&ctx->cpu_ctx);
249}
250
Olivier Deprezbb6d0a12023-06-08 18:23:26 +0200251#if (EL3_EXCEPTION_HANDLING == 0)
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600252/*******************************************************************************
253 * spmd_group0_interrupt_handler_nwd
254 * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
255 * handling of the interrupt to the platform handler, and return only upon
256 * successfully handling the Group0 interrupt.
257 ******************************************************************************/
258static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
259 uint32_t flags,
260 void *handle,
261 void *cookie)
262{
263 uint32_t intid;
264
265 /* Sanity check the security state when the exception was generated. */
266 assert(get_interrupt_src_ss(flags) == NON_SECURE);
267
268 /* Sanity check the pointer to this cpu's context. */
269 assert(handle == cm_get_context(NON_SECURE));
270
271 assert(id == INTR_ID_UNAVAILABLE);
272
273 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
274
Madhukar Pappireddy6c91fc42023-07-12 16:28:05 -0500275 intid = plat_ic_acknowledge_interrupt();
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600276
277 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
278 ERROR("Group0 interrupt %u not handled\n", intid);
279 panic();
280 }
281
Madhukar Pappireddy6c91fc42023-07-12 16:28:05 -0500282 /* Deactivate the corresponding Group0 interrupt. */
283 plat_ic_end_of_interrupt(intid);
284
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600285 return 0U;
286}
Olivier Deprezbb6d0a12023-06-08 18:23:26 +0200287#endif
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600288
Madhukar Pappireddy6671b3d2023-03-02 16:04:38 -0600289/*******************************************************************************
290 * spmd_handle_group0_intr_swd
291 * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
292 * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
293 * interrupt to the platform handler, and returns only upon successfully
294 * handling the Group0 interrupt.
295 ******************************************************************************/
296static uint64_t spmd_handle_group0_intr_swd(void *handle)
297{
298 uint32_t intid;
299
300 /* Sanity check the pointer to this cpu's context */
301 assert(handle == cm_get_context(SECURE));
302
303 assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
304
Madhukar Pappireddy6c91fc42023-07-12 16:28:05 -0500305 intid = plat_ic_acknowledge_interrupt();
Madhukar Pappireddy6671b3d2023-03-02 16:04:38 -0600306
307 /*
308 * TODO: Currently due to a limitation in SPMD implementation, the
309 * platform handler is expected to not delegate handling to NWd while
310 * processing Group0 secure interrupt.
311 */
312 if (plat_spmd_handle_group0_interrupt(intid) < 0) {
313 /* Group0 interrupt was not handled by the platform. */
314 ERROR("Group0 interrupt %u not handled\n", intid);
315 panic();
316 }
317
Madhukar Pappireddy6c91fc42023-07-12 16:28:05 -0500318 /* Deactivate the corresponding Group0 interrupt. */
319 plat_ic_end_of_interrupt(intid);
320
Madhukar Pappireddy6671b3d2023-03-02 16:04:38 -0600321 /* Return success. */
322 SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
323 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
324 FFA_PARAM_MBZ);
325}
326
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +0000327#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
328static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
329 unsigned int attr, uintptr_t *align_addr,
330 size_t *align_size)
331{
332 uintptr_t base_addr_align;
333 size_t mapped_size_align;
334 int rc;
335
336 /* Page aligned address and size if necessary */
337 base_addr_align = page_align(base_addr, DOWN);
338 mapped_size_align = page_align(size, UP);
339
340 if ((base_addr != base_addr_align) &&
341 (size == mapped_size_align)) {
342 mapped_size_align += PAGE_SIZE;
343 }
344
345 /*
346 * Map dynamically given region with its aligned base address and
347 * size
348 */
349 rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
350 base_addr_align,
351 mapped_size_align,
352 attr);
353 if (rc == 0) {
354 *align_addr = base_addr_align;
355 *align_size = mapped_size_align;
356 }
357
358 return rc;
359}
360
361static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
362 size_t size)
363{
364 uintptr_t root_base_addr_align, sec_base_addr_align;
365 size_t root_mapped_size_align, sec_mapped_size_align;
366 int rc;
367
368 assert(root_base_addr != 0UL);
369 assert(sec_base_addr != 0UL);
370 assert(size != 0UL);
371
372 /* Map the memory with required attributes */
373 rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
374 &root_base_addr_align,
375 &root_mapped_size_align);
376 if (rc != 0) {
377 ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
378 root_base_addr, rc);
379 panic();
380 }
381
382 rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
383 &sec_base_addr_align, &sec_mapped_size_align);
384 if (rc != 0) {
385 ERROR("%s %s %lu (%d)\n", "Error while mapping",
386 "secure region", sec_base_addr, rc);
387 panic();
388 }
389
390 /* Do copy operation */
391 (void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
392
393 /* Unmap root memory region */
394 rc = mmap_remove_dynamic_region(root_base_addr_align,
395 root_mapped_size_align);
396 if (rc != 0) {
397 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
398 "root region", root_base_addr_align, rc);
399 panic();
400 }
401
402 /* Unmap secure memory region */
403 rc = mmap_remove_dynamic_region(sec_base_addr_align,
404 sec_mapped_size_align);
405 if (rc != 0) {
406 ERROR("%s %s %lu (%d)\n", "Error while unmapping",
407 "secure region", sec_base_addr_align, rc);
408 panic();
409 }
410}
411#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
412
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200413/*******************************************************************************
Olivier Deprez52696942020-04-16 13:39:06 +0200414 * Loads SPMC manifest and inits SPMC.
Max Shvetsov0f14d022020-02-27 14:54:21 +0000415 ******************************************************************************/
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100416static int spmd_spmc_init(void *pm_addr)
Max Shvetsov0f14d022020-02-27 14:54:21 +0000417{
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200418 cpu_context_t *cpu_ctx;
419 unsigned int core_id;
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200420 uint32_t ep_attr, flags;
Olivier Deprez52696942020-04-16 13:39:06 +0200421 int rc;
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +0000422 const struct dyn_cfg_dtb_info_t *image_info __unused;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000423
Olivier Deprez52696942020-04-16 13:39:06 +0200424 /* Load the SPM Core manifest */
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100425 rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000426 if (rc != 0) {
Olivier Deprez52696942020-04-16 13:39:06 +0200427 WARN("No or invalid SPM Core manifest image provided by BL2\n");
428 return rc;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000429 }
430
431 /*
Olivier Deprez52696942020-04-16 13:39:06 +0200432 * Ensure that the SPM Core version is compatible with the SPM
433 * Dispatcher version.
Max Shvetsov0f14d022020-02-27 14:54:21 +0000434 */
J-Alves662af362020-05-07 18:42:25 +0100435 if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
436 (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
437 WARN("Unsupported FFA version (%u.%u)\n",
Max Shvetsov0f14d022020-02-27 14:54:21 +0000438 spmc_attrs.major_version, spmc_attrs.minor_version);
Olivier Deprez52696942020-04-16 13:39:06 +0200439 return -EINVAL;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000440 }
441
J-Alves662af362020-05-07 18:42:25 +0100442 VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
Max Shvetsov0f14d022020-02-27 14:54:21 +0000443 spmc_attrs.minor_version);
444
Olivier Deprez52696942020-04-16 13:39:06 +0200445 VERBOSE("SPM Core run time EL%x.\n",
Max Shvetsov033039f2020-02-25 13:55:00 +0000446 SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000447
Max Shvetsovac03ac52020-03-12 15:16:40 +0000448 /* Validate the SPMC ID, Ensure high bit is set */
Olivier Deprez52696942020-04-16 13:39:06 +0200449 if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
450 SPMC_SECURE_ID_MASK) == 0U) {
451 WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
452 return -EINVAL;
Max Shvetsovac03ac52020-03-12 15:16:40 +0000453 }
454
Olivier Deprez52696942020-04-16 13:39:06 +0200455 /* Validate the SPM Core execution state */
Max Shvetsov0f14d022020-02-27 14:54:21 +0000456 if ((spmc_attrs.exec_state != MODE_RW_64) &&
457 (spmc_attrs.exec_state != MODE_RW_32)) {
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100458 WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
Max Shvetsov0f14d022020-02-27 14:54:21 +0000459 spmc_attrs.exec_state);
Olivier Deprez52696942020-04-16 13:39:06 +0200460 return -EINVAL;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000461 }
462
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100463 VERBOSE("%s%x.\n", "SPM Core execution state 0x",
464 spmc_attrs.exec_state);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000465
Max Shvetsov033039f2020-02-25 13:55:00 +0000466#if SPMD_SPM_AT_SEL2
467 /* Ensure manifest has not requested AArch32 state in S-EL2 */
468 if (spmc_attrs.exec_state == MODE_RW_32) {
469 WARN("AArch32 state at S-EL2 is not supported.\n");
Olivier Deprez52696942020-04-16 13:39:06 +0200470 return -EINVAL;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000471 }
472
473 /*
474 * Check if S-EL2 is supported on this system if S-EL2
475 * is required for SPM
476 */
Andre Przywara623f6142023-02-22 16:53:50 +0000477 if (!is_feat_sel2_supported()) {
Olivier Deprez52696942020-04-16 13:39:06 +0200478 WARN("SPM Core run time S-EL2 is not supported.\n");
479 return -EINVAL;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000480 }
Max Shvetsov033039f2020-02-25 13:55:00 +0000481#endif /* SPMD_SPM_AT_SEL2 */
Max Shvetsov0f14d022020-02-27 14:54:21 +0000482
483 /* Initialise an entrypoint to set up the CPU context */
484 ep_attr = SECURE | EP_ST_ENABLE;
Olivier Deprez52696942020-04-16 13:39:06 +0200485 if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
Max Shvetsov0f14d022020-02-27 14:54:21 +0000486 ep_attr |= EP_EE_BIG;
487 }
488
489 SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000490
491 /*
Olivier Deprez52696942020-04-16 13:39:06 +0200492 * Populate SPSR for SPM Core based upon validated parameters from the
493 * manifest.
Max Shvetsov0f14d022020-02-27 14:54:21 +0000494 */
495 if (spmc_attrs.exec_state == MODE_RW_32) {
496 spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
497 SPSR_E_LITTLE,
498 DAIF_FIQ_BIT |
499 DAIF_IRQ_BIT |
500 DAIF_ABT_BIT);
501 } else {
Max Shvetsov033039f2020-02-25 13:55:00 +0000502
503#if SPMD_SPM_AT_SEL2
504 static const uint32_t runtime_el = MODE_EL2;
505#else
506 static const uint32_t runtime_el = MODE_EL1;
507#endif
508 spmc_ep_info->spsr = SPSR_64(runtime_el,
Max Shvetsov0f14d022020-02-27 14:54:21 +0000509 MODE_SP_ELX,
510 DISABLE_ALL_EXCEPTIONS);
511 }
512
Manish V Badarkhe0cea2ae2023-02-07 11:26:38 +0000513#if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
514 image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
515 assert(image_info != NULL);
516
517 if ((image_info->config_addr == 0UL) ||
518 (image_info->secondary_config_addr == 0UL) ||
519 (image_info->config_max_size == 0UL)) {
520 return -EINVAL;
521 }
522
523 /* Copy manifest from root->secure region */
524 spmd_do_sec_cpy(image_info->config_addr,
525 image_info->secondary_config_addr,
526 image_info->config_max_size);
527
528 /* Update ep info of BL32 */
529 assert(spmc_ep_info != NULL);
530 spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
531#endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
532
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200533 /* Set an initial SPMC context state for all cores. */
534 for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
535 spm_core_context[core_id].state = SPMC_STATE_OFF;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000536
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200537 /* Setup an initial cpu context for the SPMC. */
538 cpu_ctx = &spm_core_context[core_id].cpu_ctx;
539 cm_setup_context(cpu_ctx, spmc_ep_info);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000540
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200541 /*
542 * Pass the core linear ID to the SPMC through x4.
543 * (TF-A implementation defined behavior helping
544 * a legacy TOS migration to adopt FF-A).
545 */
546 write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
547 }
Max Shvetsov0f14d022020-02-27 14:54:21 +0000548
Olivier Depreza334c4e2019-10-28 09:15:52 +0000549 /* Register power management hooks with PSCI */
550 psci_register_spd_pm_hook(&spmd_pm);
551
Olivier Deprez52696942020-04-16 13:39:06 +0200552 /* Register init function for deferred init. */
Max Shvetsov0f14d022020-02-27 14:54:21 +0000553 bl31_register_bl32_init(&spmd_init);
554
Olivier Deprezf2dcf412021-06-21 09:47:13 +0200555 INFO("SPM Core setup done.\n");
556
Olivier Deprez8cb99c32020-08-05 11:27:42 +0200557 /*
558 * Register an interrupt handler routing secure interrupts to SPMD
559 * while the NWd is running.
560 */
561 flags = 0;
562 set_interrupt_rm_flag(flags, NON_SECURE);
563 rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
564 spmd_secure_interrupt_handler,
565 flags);
566 if (rc != 0) {
567 panic();
568 }
569
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600570 /*
Olivier Deprezbb6d0a12023-06-08 18:23:26 +0200571 * Permit configurations where the SPM resides at S-EL1/2 and upon a
572 * Group0 interrupt triggering while the normal world runs, the
573 * interrupt is routed either through the EHF or directly to the SPMD:
574 *
575 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
576 * for handling by spmd_group0_interrupt_handler_nwd.
577 *
578 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
579 *
580 */
581#if (EL3_EXCEPTION_HANDLING == 0)
582 /*
Madhukar Pappireddyfca5f0e2024-03-26 09:21:25 -0500583 * If EL3 interrupts are supported by the platform, register an
584 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
585 * running.
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600586 */
Madhukar Pappireddyfca5f0e2024-03-26 09:21:25 -0500587 if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
588 rc = register_interrupt_type_handler(INTR_TYPE_EL3,
589 spmd_group0_interrupt_handler_nwd,
590 flags);
591 if (rc != 0) {
592 panic();
593 }
Madhukar Pappireddya1e0e872023-03-02 15:34:05 -0600594 }
Olivier Deprezbb6d0a12023-06-08 18:23:26 +0200595#endif
596
Max Shvetsov0f14d022020-02-27 14:54:21 +0000597 return 0;
598}
599
600/*******************************************************************************
Olivier Deprez52696942020-04-16 13:39:06 +0200601 * Initialize context of SPM Core.
Achin Guptabdd25962019-10-11 15:41:16 +0100602 ******************************************************************************/
Max Shvetsov0f14d022020-02-27 14:54:21 +0000603int spmd_setup(void)
Achin Guptabdd25962019-10-11 15:41:16 +0100604{
605 int rc;
Marc Bonnici6da76072021-11-29 17:57:03 +0000606 void *spmc_manifest;
607
608 /*
609 * If the SPMC is at EL3, then just initialise it directly. The
610 * shenanigans of when it is at a lower EL are not needed.
611 */
612 if (is_spmc_at_el3()) {
613 /* Allow the SPMC to populate its attributes directly. */
614 spmc_populate_attrs(&spmc_attrs);
615
616 rc = spmc_setup();
617 if (rc != 0) {
Olivier Deprez0d336492022-11-16 16:46:23 +0100618 WARN("SPMC initialisation failed 0x%x.\n", rc);
Marc Bonnici6da76072021-11-29 17:57:03 +0000619 }
Olivier Deprez0d336492022-11-16 16:46:23 +0100620 return 0;
Marc Bonnici6da76072021-11-29 17:57:03 +0000621 }
Achin Guptabdd25962019-10-11 15:41:16 +0100622
623 spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
Olivier Deprez52696942020-04-16 13:39:06 +0200624 if (spmc_ep_info == NULL) {
625 WARN("No SPM Core image provided by BL2 boot loader.\n");
Olivier Deprez0d336492022-11-16 16:46:23 +0100626 return 0;
Achin Guptabdd25962019-10-11 15:41:16 +0100627 }
628
629 /* Under no circumstances will this parameter be 0 */
Olivier Deprez52696942020-04-16 13:39:06 +0200630 assert(spmc_ep_info->pc != 0ULL);
Achin Guptabdd25962019-10-11 15:41:16 +0100631
632 /*
633 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
Olivier Deprez52696942020-04-16 13:39:06 +0200634 * be used as a manifest for the SPM Core at the next lower EL/mode.
Achin Guptabdd25962019-10-11 15:41:16 +0100635 */
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100636 spmc_manifest = (void *)spmc_ep_info->args.arg0;
637 if (spmc_manifest == NULL) {
Olivier Deprez0d336492022-11-16 16:46:23 +0100638 WARN("Invalid or absent SPM Core manifest.\n");
639 return 0;
Achin Guptabdd25962019-10-11 15:41:16 +0100640 }
641
Max Shvetsov0f14d022020-02-27 14:54:21 +0000642 /* Load manifest, init SPMC */
Olivier Deprez23d5ba82020-02-07 15:44:43 +0100643 rc = spmd_spmc_init(spmc_manifest);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000644 if (rc != 0) {
Olivier Deprez52696942020-04-16 13:39:06 +0200645 WARN("Booting device without SPM initialization.\n");
Achin Guptabdd25962019-10-11 15:41:16 +0100646 }
647
Olivier Deprez0d336492022-11-16 16:46:23 +0100648 return 0;
Max Shvetsov0f14d022020-02-27 14:54:21 +0000649}
Achin Guptabdd25962019-10-11 15:41:16 +0100650
Max Shvetsov0f14d022020-02-27 14:54:21 +0000651/*******************************************************************************
Marc Bonnicibb01a672021-11-29 18:02:45 +0000652 * Forward FF-A SMCs to the other security state.
Max Shvetsov0f14d022020-02-27 14:54:21 +0000653 ******************************************************************************/
Marc Bonnicibb01a672021-11-29 18:02:45 +0000654uint64_t spmd_smc_switch_state(uint32_t smc_fid,
655 bool secure_origin,
656 uint64_t x1,
657 uint64_t x2,
658 uint64_t x3,
659 uint64_t x4,
Olivier Deprezc9258672022-10-31 12:38:17 +0100660 void *handle,
661 uint64_t flags)
Max Shvetsov0f14d022020-02-27 14:54:21 +0000662{
Olivier Deprezc2901412020-04-16 16:59:21 +0200663 unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
664 unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
Olivier Deprez107e3cc2024-06-07 09:24:43 +0200665 void *ctx_out;
Olivier Deprez93ff1382019-12-23 16:21:12 +0100666
Olivier Deprezc9258672022-10-31 12:38:17 +0100667#if SPMD_SPM_AT_SEL2
668 if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
669 /*
670 * Set the SVE hint bit in x0 and pass to the lower secure EL,
671 * if it was set by the caller.
672 */
673 smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
674 }
675#endif
676
Max Shvetsov0f14d022020-02-27 14:54:21 +0000677 /* Save incoming security state */
Max Shvetsov033039f2020-02-25 13:55:00 +0000678#if SPMD_SPM_AT_SEL2
Olivier Deprez93ff1382019-12-23 16:21:12 +0100679 cm_el2_sysregs_context_save(secure_state_in);
Olivier Deprez678ce222021-05-21 18:00:04 +0200680#else
681 cm_el1_sysregs_context_save(secure_state_in);
Max Shvetsov033039f2020-02-25 13:55:00 +0000682#endif
Achin Guptabdd25962019-10-11 15:41:16 +0100683
Max Shvetsov0f14d022020-02-27 14:54:21 +0000684 /* Restore outgoing security state */
Max Shvetsov033039f2020-02-25 13:55:00 +0000685#if SPMD_SPM_AT_SEL2
Olivier Deprez93ff1382019-12-23 16:21:12 +0100686 cm_el2_sysregs_context_restore(secure_state_out);
Olivier Deprez678ce222021-05-21 18:00:04 +0200687#else
688 cm_el1_sysregs_context_restore(secure_state_out);
Max Shvetsov033039f2020-02-25 13:55:00 +0000689#endif
Olivier Deprez93ff1382019-12-23 16:21:12 +0100690 cm_set_next_eret_context(secure_state_out);
Achin Guptabdd25962019-10-11 15:41:16 +0100691
Olivier Deprez107e3cc2024-06-07 09:24:43 +0200692 ctx_out = cm_get_context(secure_state_out);
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -0800693#if SPMD_SPM_AT_SEL2
694 /*
695 * If SPMC is at SEL2, save additional registers x8-x17, which may
696 * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
697 * Note that technically, all SPMCs can support this, but this code is
698 * under ifdef to minimize breakage in case other SPMCs do not save
699 * and restore x8-x17.
700 * We also need to pass through these registers since not all FF-A ABIs
701 * modify x8-x17, in which case, SMCCC requires that these registers be
702 * preserved, so the SPMD passes through these registers and expects the
703 * SPMC to save and restore (potentially also modify) them.
704 */
Olivier Deprez107e3cc2024-06-07 09:24:43 +0200705 SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -0800706 SMC_GET_GP(handle, CTX_GPREG_X5),
707 SMC_GET_GP(handle, CTX_GPREG_X6),
708 SMC_GET_GP(handle, CTX_GPREG_X7),
709 SMC_GET_GP(handle, CTX_GPREG_X8),
710 SMC_GET_GP(handle, CTX_GPREG_X9),
711 SMC_GET_GP(handle, CTX_GPREG_X10),
712 SMC_GET_GP(handle, CTX_GPREG_X11),
713 SMC_GET_GP(handle, CTX_GPREG_X12),
714 SMC_GET_GP(handle, CTX_GPREG_X13),
715 SMC_GET_GP(handle, CTX_GPREG_X14),
716 SMC_GET_GP(handle, CTX_GPREG_X15),
717 SMC_GET_GP(handle, CTX_GPREG_X16),
718 SMC_GET_GP(handle, CTX_GPREG_X17)
719 );
720
721#else
Olivier Deprez107e3cc2024-06-07 09:24:43 +0200722 SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
Max Shvetsov0f14d022020-02-27 14:54:21 +0000723 SMC_GET_GP(handle, CTX_GPREG_X5),
724 SMC_GET_GP(handle, CTX_GPREG_X6),
725 SMC_GET_GP(handle, CTX_GPREG_X7));
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -0800726#endif
Max Shvetsov0f14d022020-02-27 14:54:21 +0000727}
728
729/*******************************************************************************
Marc Bonnicibb01a672021-11-29 18:02:45 +0000730 * Forward SMCs to the other security state.
731 ******************************************************************************/
732static uint64_t spmd_smc_forward(uint32_t smc_fid,
733 bool secure_origin,
734 uint64_t x1,
735 uint64_t x2,
736 uint64_t x3,
737 uint64_t x4,
738 void *cookie,
739 void *handle,
740 uint64_t flags)
741{
742 if (is_spmc_at_el3() && !secure_origin) {
743 return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
744 cookie, handle, flags);
745 }
Olivier Deprezc9258672022-10-31 12:38:17 +0100746
Marc Bonnicibb01a672021-11-29 18:02:45 +0000747 return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
Olivier Deprezc9258672022-10-31 12:38:17 +0100748 handle, flags);
Marc Bonnicibb01a672021-11-29 18:02:45 +0000749
750}
751
752/*******************************************************************************
J-Alves662af362020-05-07 18:42:25 +0100753 * Return FFA_ERROR with specified error code
Max Shvetsov0f14d022020-02-27 14:54:21 +0000754 ******************************************************************************/
Raghu Krishnamurthy95f7f6d2023-04-22 18:00:02 -0700755uint64_t spmd_ffa_error_return(void *handle, int error_code)
Max Shvetsov0f14d022020-02-27 14:54:21 +0000756{
J-Alvese46b2fd2021-03-01 10:26:59 +0000757 SMC_RET8(handle, (uint32_t) FFA_ERROR,
758 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
J-Alves662af362020-05-07 18:42:25 +0100759 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
760 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Guptabdd25962019-10-11 15:41:16 +0100761}
762
Olivier Deprezf0d743d2020-04-16 17:54:27 +0200763/*******************************************************************************
764 * spmd_check_address_in_binary_image
765 ******************************************************************************/
766bool spmd_check_address_in_binary_image(uint64_t address)
767{
768 assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
769
770 return ((address >= spmc_attrs.load_address) &&
771 (address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
772}
773
Olivier Deprezc2901412020-04-16 16:59:21 +0200774/******************************************************************************
775 * spmd_is_spmc_message
776 *****************************************************************************/
777static bool spmd_is_spmc_message(unsigned int ep)
778{
Marc Bonnicibb01a672021-11-29 18:02:45 +0000779 if (is_spmc_at_el3()) {
780 return false;
781 }
782
Olivier Deprezc2901412020-04-16 16:59:21 +0200783 return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
784 && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
785}
786
Olivier Deprezf0d743d2020-04-16 17:54:27 +0200787/******************************************************************************
788 * spmd_handle_spmc_message
789 *****************************************************************************/
Olivier Depreza92bc732020-03-23 09:53:06 +0100790static int spmd_handle_spmc_message(unsigned long long msg,
791 unsigned long long parm1, unsigned long long parm2,
792 unsigned long long parm3, unsigned long long parm4)
Olivier Deprezf0d743d2020-04-16 17:54:27 +0200793{
794 VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
795 msg, parm1, parm2, parm3, parm4);
796
Olivier Deprezf0d743d2020-04-16 17:54:27 +0200797 return -EINVAL;
798}
799
Achin Guptabdd25962019-10-11 15:41:16 +0100800/*******************************************************************************
Marc Bonnicibb01a672021-11-29 18:02:45 +0000801 * This function forwards FF-A SMCs to either the main SPMD handler or the
802 * SPMC at EL3, depending on the origin security state, if enabled.
803 ******************************************************************************/
804uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
805 uint64_t x1,
806 uint64_t x2,
807 uint64_t x3,
808 uint64_t x4,
809 void *cookie,
810 void *handle,
811 uint64_t flags)
812{
813 if (is_spmc_at_el3()) {
814 /*
815 * If we have an SPMC at EL3 allow handling of the SMC first.
816 * The SPMC will call back through to SPMD handler if required.
817 */
818 if (is_caller_secure(flags)) {
819 return spmc_smc_handler(smc_fid,
820 is_caller_secure(flags),
821 x1, x2, x3, x4, cookie,
822 handle, flags);
823 }
824 }
825 return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
826 handle, flags);
827}
828
829/*******************************************************************************
J-Alves662af362020-05-07 18:42:25 +0100830 * This function handles all SMCs in the range reserved for FFA. Each call is
Achin Guptabdd25962019-10-11 15:41:16 +0100831 * either forwarded to the other security state or handled by the SPM dispatcher
832 ******************************************************************************/
Olivier Deprez52696942020-04-16 13:39:06 +0200833uint64_t spmd_smc_handler(uint32_t smc_fid,
834 uint64_t x1,
835 uint64_t x2,
836 uint64_t x3,
837 uint64_t x4,
838 void *cookie,
839 void *handle,
Achin Guptabdd25962019-10-11 15:41:16 +0100840 uint64_t flags)
841{
Olivier Deprezcdb49d42021-01-19 15:06:47 +0100842 unsigned int linear_id = plat_my_core_pos();
Olivier Deprez52696942020-04-16 13:39:06 +0200843 spmd_spm_core_context_t *ctx = spmd_get_context();
Olivier Deprez93ff1382019-12-23 16:21:12 +0100844 bool secure_origin;
J-Alves68730882023-10-04 17:16:45 +0100845 int ret;
J-Alves4388f282020-05-26 14:03:05 +0100846 uint32_t input_version;
Achin Guptabdd25962019-10-11 15:41:16 +0100847
848 /* Determine which security state this SMC originated from */
Olivier Deprez93ff1382019-12-23 16:21:12 +0100849 secure_origin = is_caller_secure(flags);
Achin Guptabdd25962019-10-11 15:41:16 +0100850
Scott Branden4ce3e992020-08-25 13:49:32 -0700851 VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
852 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
853 linear_id, smc_fid, x1, x2, x3, x4,
854 SMC_GET_GP(handle, CTX_GPREG_X5),
855 SMC_GET_GP(handle, CTX_GPREG_X6),
856 SMC_GET_GP(handle, CTX_GPREG_X7));
Achin Guptabdd25962019-10-11 15:41:16 +0100857
Raghu Krishnamurthy0b850e92023-04-22 11:28:38 -0700858 /*
859 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
860 * return, we don't expect any other FF-A ABIs to be called between
861 * calls to FFA_PARTITION_INFO_GET_REGS.
862 */
863 if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
864 assert(secure_origin);
865 spmd_spm_core_sync_exit(0ULL);
866 }
867
Achin Guptabdd25962019-10-11 15:41:16 +0100868 switch (smc_fid) {
J-Alves662af362020-05-07 18:42:25 +0100869 case FFA_ERROR:
Achin Guptabdd25962019-10-11 15:41:16 +0100870 /*
871 * Check if this is the first invocation of this interface on
Olivier Deprez52696942020-04-16 13:39:06 +0200872 * this CPU. If so, then indicate that the SPM Core initialised
Achin Guptabdd25962019-10-11 15:41:16 +0100873 * unsuccessfully.
874 */
Olivier Deprez9dcf63d2019-10-28 09:03:13 +0000875 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Achin Guptabdd25962019-10-11 15:41:16 +0100876 spmd_spm_core_sync_exit(x2);
Max Shvetsov0f14d022020-02-27 14:54:21 +0000877 }
Achin Guptabdd25962019-10-11 15:41:16 +0100878
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -0800879 /*
880 * If there was an SPMD logical partition direct request on-going,
881 * return back to the SPMD logical partition so the error can be
882 * consumed.
883 */
884 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
885 assert(secure_origin);
886 spmd_spm_core_sync_exit(0ULL);
887 }
888
Olivier Deprez93ff1382019-12-23 16:21:12 +0100889 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +0000890 x1, x2, x3, x4, cookie,
891 handle, flags);
Achin Guptabdd25962019-10-11 15:41:16 +0100892 break; /* not reached */
893
J-Alves662af362020-05-07 18:42:25 +0100894 case FFA_VERSION:
J-Alves4388f282020-05-26 14:03:05 +0100895 input_version = (uint32_t)(0xFFFFFFFF & x1);
Achin Guptabdd25962019-10-11 15:41:16 +0100896 /*
J-Alves4388f282020-05-26 14:03:05 +0100897 * If caller is secure and SPMC was initialized,
898 * return FFA_VERSION of SPMD.
899 * If caller is non secure and SPMC was initialized,
Marc Bonnici9576fa92021-12-08 14:27:40 +0000900 * forward to the EL3 SPMC if enabled, otherwise return
901 * the SPMC version if implemented at a lower EL.
J-Alves4388f282020-05-26 14:03:05 +0100902 * Sanity check to "input_version".
Marc Bonnicibb01a672021-11-29 18:02:45 +0000903 * If the EL3 SPMC is enabled, ignore the SPMC state as
904 * this is not used.
Achin Guptabdd25962019-10-11 15:41:16 +0100905 */
J-Alves4388f282020-05-26 14:03:05 +0100906 if ((input_version & FFA_VERSION_BIT31_MASK) ||
Marc Bonnicibb01a672021-11-29 18:02:45 +0000907 (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
J-Alves4388f282020-05-26 14:03:05 +0100908 ret = FFA_ERROR_NOT_SUPPORTED;
909 } else if (!secure_origin) {
Marc Bonnici9576fa92021-12-08 14:27:40 +0000910 if (is_spmc_at_el3()) {
911 /*
912 * Forward the call directly to the EL3 SPMC, if
913 * enabled, as we don't need to wrap the call in
914 * a direct request.
915 */
916 return spmd_smc_forward(smc_fid, secure_origin,
917 x1, x2, x3, x4, cookie,
918 handle, flags);
919 }
920
Daniel Boulby9944f552021-12-09 11:20:13 +0000921 gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
922 uint64_t rc;
923
924 if (spmc_attrs.major_version == 1 &&
925 spmc_attrs.minor_version == 0) {
926 ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
927 spmc_attrs.minor_version);
928 SMC_RET8(handle, (uint32_t)ret,
929 FFA_TARGET_INFO_MBZ,
930 FFA_TARGET_INFO_MBZ,
931 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
932 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
933 FFA_PARAM_MBZ);
934 break;
935 }
936 /* Save non-secure system registers context */
Daniel Boulby9944f552021-12-09 11:20:13 +0000937#if SPMD_SPM_AT_SEL2
938 cm_el2_sysregs_context_save(NON_SECURE);
Madhukar Pappireddy2d960a12024-01-29 16:43:56 -0600939#else
940 cm_el1_sysregs_context_save(NON_SECURE);
Daniel Boulby9944f552021-12-09 11:20:13 +0000941#endif
942
943 /*
944 * The incoming request has FFA_VERSION as X0 smc_fid
945 * and requested version in x1. Prepare a direct request
946 * from SPMD to SPMC with FFA_VERSION framework function
947 * identifier in X2 and requested version in X3.
948 */
949 spmd_build_spmc_message(gpregs,
950 SPMD_FWK_MSG_FFA_VERSION_REQ,
951 input_version);
952
Olivier Deprez76d53ee2023-07-10 11:04:30 +0200953 /*
954 * Ensure x8-x17 NS GP register values are untouched when returning
955 * from the SPMC.
956 */
957 write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
958 write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
959 write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
960 write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
961 write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
962 write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
963 write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
964 write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
965 write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
966 write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
967
Daniel Boulby9944f552021-12-09 11:20:13 +0000968 rc = spmd_spm_core_sync_entry(ctx);
969
970 if ((rc != 0ULL) ||
971 (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
972 FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
973 (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
Marc Bonnici59bd2ad2022-04-12 17:18:13 +0100974 (FFA_FWK_MSG_BIT |
Daniel Boulby9944f552021-12-09 11:20:13 +0000975 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
976 ERROR("Failed to forward FFA_VERSION\n");
977 ret = FFA_ERROR_NOT_SUPPORTED;
978 } else {
979 ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
980 }
981
982 /*
Olivier Deprez76d53ee2023-07-10 11:04:30 +0200983 * x0-x4 are updated by spmd_smc_forward below.
984 * Zero out x5-x7 in the FFA_VERSION response.
985 */
986 write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
987 write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
988 write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
989
990 /*
Daniel Boulby9944f552021-12-09 11:20:13 +0000991 * Return here after SPMC has handled FFA_VERSION.
992 * The returned SPMC version is held in X3.
993 * Forward this version in X0 to the non-secure caller.
994 */
995 return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
996 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
Marc Bonnicibb01a672021-11-29 18:02:45 +0000997 FFA_PARAM_MBZ, cookie, gpregs,
998 flags);
J-Alves4388f282020-05-26 14:03:05 +0100999 } else {
J-Alvese46b2fd2021-03-01 10:26:59 +00001000 ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1001 FFA_VERSION_MINOR);
J-Alves4388f282020-05-26 14:03:05 +01001002 }
1003
J-Alvese46b2fd2021-03-01 10:26:59 +00001004 SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1005 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1006 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
Achin Guptabdd25962019-10-11 15:41:16 +01001007 break; /* not reached */
1008
J-Alves662af362020-05-07 18:42:25 +01001009 case FFA_FEATURES:
Achin Guptabdd25962019-10-11 15:41:16 +01001010 /*
1011 * This is an optional interface. Do the minimal checks and
Olivier Deprez52696942020-04-16 13:39:06 +02001012 * forward to SPM Core which will handle it if implemented.
Achin Guptabdd25962019-10-11 15:41:16 +01001013 */
1014
Olivier Deprez52696942020-04-16 13:39:06 +02001015 /* Forward SMC from Normal world to the SPM Core */
Olivier Deprez93ff1382019-12-23 16:21:12 +01001016 if (!secure_origin) {
1017 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +00001018 x1, x2, x3, x4, cookie,
1019 handle, flags);
Achin Guptabdd25962019-10-11 15:41:16 +01001020 }
Max Shvetsov0f14d022020-02-27 14:54:21 +00001021
Olivier Deprez52696942020-04-16 13:39:06 +02001022 /*
1023 * Return success if call was from secure world i.e. all
J-Alves662af362020-05-07 18:42:25 +01001024 * FFA functions are supported. This is essentially a
Olivier Deprez52696942020-04-16 13:39:06 +02001025 * nop.
1026 */
J-Alves662af362020-05-07 18:42:25 +01001027 SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
Olivier Deprez52696942020-04-16 13:39:06 +02001028 SMC_GET_GP(handle, CTX_GPREG_X5),
1029 SMC_GET_GP(handle, CTX_GPREG_X6),
1030 SMC_GET_GP(handle, CTX_GPREG_X7));
1031
Achin Guptabdd25962019-10-11 15:41:16 +01001032 break; /* not reached */
1033
J-Alves662af362020-05-07 18:42:25 +01001034 case FFA_ID_GET:
Max Shvetsovac03ac52020-03-12 15:16:40 +00001035 /*
J-Alves662af362020-05-07 18:42:25 +01001036 * Returns the ID of the calling FFA component.
Olivier Deprez52696942020-04-16 13:39:06 +02001037 */
Max Shvetsovac03ac52020-03-12 15:16:40 +00001038 if (!secure_origin) {
J-Alves662af362020-05-07 18:42:25 +01001039 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1040 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1041 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1042 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1043 FFA_PARAM_MBZ);
Max Shvetsovac03ac52020-03-12 15:16:40 +00001044 }
1045
J-Alves662af362020-05-07 18:42:25 +01001046 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1047 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1048 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1049 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1050 FFA_PARAM_MBZ);
Olivier Deprez52696942020-04-16 13:39:06 +02001051
Max Shvetsovac03ac52020-03-12 15:16:40 +00001052 break; /* not reached */
1053
Olivier Deprezcdb49d42021-01-19 15:06:47 +01001054 case FFA_SECONDARY_EP_REGISTER_SMC64:
1055 if (secure_origin) {
1056 ret = spmd_pm_secondary_ep_register(x1);
1057
1058 if (ret < 0) {
1059 SMC_RET8(handle, FFA_ERROR_SMC64,
1060 FFA_TARGET_INFO_MBZ, ret,
1061 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1062 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1063 FFA_PARAM_MBZ);
1064 } else {
1065 SMC_RET8(handle, FFA_SUCCESS_SMC64,
1066 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1067 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1068 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1069 FFA_PARAM_MBZ);
1070 }
1071 }
1072
1073 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1074 break; /* Not reached */
1075
Daniel Boulby70c121a2021-02-03 12:13:19 +00001076 case FFA_SPM_ID_GET:
1077 if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
1078 return spmd_ffa_error_return(handle,
1079 FFA_ERROR_NOT_SUPPORTED);
1080 }
1081 /*
1082 * Returns the ID of the SPMC or SPMD depending on the FF-A
1083 * instance where this function is invoked
1084 */
1085 if (!secure_origin) {
1086 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1087 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1088 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1089 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1090 FFA_PARAM_MBZ);
1091 }
1092 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1093 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
1094 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1095 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1096 FFA_PARAM_MBZ);
1097
1098 break; /* not reached */
1099
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001100 case FFA_MSG_SEND_DIRECT_REQ_SMC32:
Shruti5519f072022-06-09 11:03:11 +01001101 case FFA_MSG_SEND_DIRECT_REQ_SMC64:
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -08001102 /*
1103 * Regardless of secure_origin, SPMD logical partitions cannot
1104 * handle direct messages. They can only initiate direct
1105 * messages and consume direct responses or errors.
1106 */
1107 if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
1108 is_spmd_lp_id(ffa_endpoint_destination(x1))) {
1109 return spmd_ffa_error_return(handle,
1110 FFA_ERROR_INVALID_PARAMETER
1111 );
1112 }
1113
1114 /*
1115 * When there is an ongoing SPMD logical partition direct
1116 * request, there cannot be another direct request. Return
1117 * error in this case. Panic'ing is an option but that does
1118 * not provide the opportunity for caller to abort based on
1119 * error codes.
1120 */
1121 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1122 assert(secure_origin);
1123 return spmd_ffa_error_return(handle,
1124 FFA_ERROR_DENIED);
1125 }
1126
Shruti5519f072022-06-09 11:03:11 +01001127 if (!secure_origin) {
1128 /* Validate source endpoint is non-secure for non-secure caller. */
1129 if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1130 return spmd_ffa_error_return(handle,
1131 FFA_ERROR_INVALID_PARAMETER);
1132 }
1133 }
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001134 if (secure_origin && spmd_is_spmc_message(x1)) {
1135 ret = spmd_handle_spmc_message(x3, x4,
1136 SMC_GET_GP(handle, CTX_GPREG_X5),
1137 SMC_GET_GP(handle, CTX_GPREG_X6),
1138 SMC_GET_GP(handle, CTX_GPREG_X7));
1139
1140 SMC_RET8(handle, FFA_SUCCESS_SMC32,
1141 FFA_TARGET_INFO_MBZ, ret,
1142 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1143 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1144 FFA_PARAM_MBZ);
1145 } else {
1146 /* Forward direct message to the other world */
1147 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +00001148 x1, x2, x3, x4, cookie,
1149 handle, flags);
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001150 }
1151 break; /* Not reached */
1152
Kathleen Capellacc6047b2023-07-31 14:45:58 -04001153 case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1154 if (!secure_origin) {
1155 /* Validate source endpoint is non-secure for non-secure caller. */
1156 if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1157 return spmd_ffa_error_return(handle,
1158 FFA_ERROR_INVALID_PARAMETER);
1159 }
1160 }
1161 /* FFA_MSG_SEND_DIRECT_REQ2 not used for framework messages. */
1162 if (secure_origin && spmd_is_spmc_message(x1)) {
1163 return spmd_ffa_error_return(handle, FFA_ERROR_INVALID_PARAMETER);
1164 } else {
1165 /* Forward direct message to the other world */
1166 return spmd_smc_forward(smc_fid, secure_origin,
1167 x1, x2, x3, x4, cookie,
1168 handle, flags);
1169 }
1170 break; /* Not reached */
1171
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001172 case FFA_MSG_SEND_DIRECT_RESP_SMC32:
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -08001173 case FFA_MSG_SEND_DIRECT_RESP_SMC64:
1174 if (secure_origin && (spmd_is_spmc_message(x1) ||
1175 is_spmd_logical_sp_dir_req_in_progress(ctx))) {
Olivier Deprez8cb99c32020-08-05 11:27:42 +02001176 spmd_spm_core_sync_exit(0ULL);
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001177 } else {
1178 /* Forward direct message to the other world */
1179 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +00001180 x1, x2, x3, x4, cookie,
1181 handle, flags);
Olivier Deprezf0d743d2020-04-16 17:54:27 +02001182 }
1183 break; /* Not reached */
Kathleen Capella0651b7b2023-09-08 17:45:45 -04001184 case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1185 /* Forward direct message to the other world */
1186 return spmd_smc_forward(smc_fid, secure_origin,
1187 x1, x2, x3, x4, cookie,
1188 handle, flags);
1189 break; /* Not reached */
J-Alves662af362020-05-07 18:42:25 +01001190 case FFA_RX_RELEASE:
1191 case FFA_RXTX_MAP_SMC32:
1192 case FFA_RXTX_MAP_SMC64:
1193 case FFA_RXTX_UNMAP:
Ruari Phipps545b8eb2020-07-28 10:33:35 +01001194 case FFA_PARTITION_INFO_GET:
J-Alvesfc3f4802021-03-11 17:46:47 +00001195#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1196 case FFA_NOTIFICATION_BITMAP_CREATE:
1197 case FFA_NOTIFICATION_BITMAP_DESTROY:
1198 case FFA_NOTIFICATION_BIND:
1199 case FFA_NOTIFICATION_UNBIND:
1200 case FFA_NOTIFICATION_SET:
1201 case FFA_NOTIFICATION_GET:
1202 case FFA_NOTIFICATION_INFO_GET:
1203 case FFA_NOTIFICATION_INFO_GET_SMC64:
Federico Recanatic2eba072022-02-03 17:22:37 +01001204 case FFA_MSG_SEND2:
Federico Recanatid5552332022-03-18 10:30:00 +01001205 case FFA_RX_ACQUIRE:
J-Alvesfc3f4802021-03-11 17:46:47 +00001206#endif
J-Alves662af362020-05-07 18:42:25 +01001207 case FFA_MSG_RUN:
Federico Recanatic2eba072022-02-03 17:22:37 +01001208 /*
1209 * Above calls should be invoked only by the Normal world and
1210 * must not be forwarded from Secure world to Normal world.
1211 */
Olivier Deprez93ff1382019-12-23 16:21:12 +01001212 if (secure_origin) {
J-Alves662af362020-05-07 18:42:25 +01001213 return spmd_ffa_error_return(handle,
Ruari Phipps545b8eb2020-07-28 10:33:35 +01001214 FFA_ERROR_NOT_SUPPORTED);
Achin Guptabdd25962019-10-11 15:41:16 +01001215 }
1216
Boyan Karatoteve1384002022-11-18 14:17:17 +00001217 /* Forward the call to the other world */
1218 /* fallthrough */
J-Alves662af362020-05-07 18:42:25 +01001219 case FFA_MSG_SEND:
J-Alves662af362020-05-07 18:42:25 +01001220 case FFA_MEM_DONATE_SMC32:
1221 case FFA_MEM_DONATE_SMC64:
1222 case FFA_MEM_LEND_SMC32:
1223 case FFA_MEM_LEND_SMC64:
1224 case FFA_MEM_SHARE_SMC32:
1225 case FFA_MEM_SHARE_SMC64:
1226 case FFA_MEM_RETRIEVE_REQ_SMC32:
1227 case FFA_MEM_RETRIEVE_REQ_SMC64:
1228 case FFA_MEM_RETRIEVE_RESP:
1229 case FFA_MEM_RELINQUISH:
1230 case FFA_MEM_RECLAIM:
Marc Bonnici642db982021-09-23 09:44:14 +01001231 case FFA_MEM_FRAG_TX:
1232 case FFA_MEM_FRAG_RX:
J-Alves662af362020-05-07 18:42:25 +01001233 case FFA_SUCCESS_SMC32:
1234 case FFA_SUCCESS_SMC64:
Achin Guptabdd25962019-10-11 15:41:16 +01001235 /*
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -08001236 * If there is an ongoing direct request from an SPMD logical
1237 * partition, return an error.
Achin Guptabdd25962019-10-11 15:41:16 +01001238 */
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -08001239 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1240 assert(secure_origin);
1241 return spmd_ffa_error_return(handle,
1242 FFA_ERROR_DENIED);
1243 }
Achin Guptabdd25962019-10-11 15:41:16 +01001244
Olivier Deprez93ff1382019-12-23 16:21:12 +01001245 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +00001246 x1, x2, x3, x4, cookie,
1247 handle, flags);
Achin Guptabdd25962019-10-11 15:41:16 +01001248 break; /* not reached */
1249
J-Alves662af362020-05-07 18:42:25 +01001250 case FFA_MSG_WAIT:
Achin Guptabdd25962019-10-11 15:41:16 +01001251 /*
1252 * Check if this is the first invocation of this interface on
1253 * this CPU from the Secure world. If so, then indicate that the
Olivier Deprez52696942020-04-16 13:39:06 +02001254 * SPM Core initialised successfully.
Achin Guptabdd25962019-10-11 15:41:16 +01001255 */
Olivier Deprez9dcf63d2019-10-28 09:03:13 +00001256 if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
Olivier Deprez8cb99c32020-08-05 11:27:42 +02001257 spmd_spm_core_sync_exit(0ULL);
Achin Guptabdd25962019-10-11 15:41:16 +01001258 }
1259
Boyan Karatoteve1384002022-11-18 14:17:17 +00001260 /* Forward the call to the other world */
1261 /* fallthrough */
Olivier Deprez386dc362021-04-02 11:09:10 +02001262 case FFA_INTERRUPT:
J-Alves662af362020-05-07 18:42:25 +01001263 case FFA_MSG_YIELD:
Achin Guptabdd25962019-10-11 15:41:16 +01001264 /* This interface must be invoked only by the Secure world */
Olivier Deprez93ff1382019-12-23 16:21:12 +01001265 if (!secure_origin) {
J-Alves662af362020-05-07 18:42:25 +01001266 return spmd_ffa_error_return(handle,
1267 FFA_ERROR_NOT_SUPPORTED);
Achin Guptabdd25962019-10-11 15:41:16 +01001268 }
1269
Raghu Krishnamurthy66bdfd62023-03-03 06:41:29 -08001270 if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
1271 assert(secure_origin);
1272 return spmd_ffa_error_return(handle,
1273 FFA_ERROR_DENIED);
1274 }
1275
Olivier Deprez93ff1382019-12-23 16:21:12 +01001276 return spmd_smc_forward(smc_fid, secure_origin,
Marc Bonnicibb01a672021-11-29 18:02:45 +00001277 x1, x2, x3, x4, cookie,
1278 handle, flags);
Achin Guptabdd25962019-10-11 15:41:16 +01001279 break; /* not reached */
1280
Olivier Deprez8cb99c32020-08-05 11:27:42 +02001281 case FFA_NORMAL_WORLD_RESUME:
1282 if (secure_origin && ctx->secure_interrupt_ongoing) {
1283 spmd_spm_core_sync_exit(0ULL);
1284 } else {
1285 return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
1286 }
1287 break; /* Not reached */
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -08001288#if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1289 case FFA_PARTITION_INFO_GET_REGS_SMC64:
1290 if (secure_origin) {
Raghu Krishnamurthy95f7f6d2023-04-22 18:00:02 -07001291 return spmd_el3_populate_logical_partition_info(handle, x1,
1292 x2, x3);
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -08001293 }
Olivier Deprez8cb99c32020-08-05 11:27:42 +02001294
Raghu Krishnamurthyeaaf5172022-12-25 13:02:00 -08001295 /* Call only supported with SMCCC 1.2+ */
1296 if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1297 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1298 }
1299
1300 return spmd_smc_forward(smc_fid, secure_origin,
1301 x1, x2, x3, x4, cookie,
1302 handle, flags);
1303 break; /* Not reached */
1304#endif
Shruti Gupta638a6f82023-01-19 21:50:55 +00001305 case FFA_CONSOLE_LOG_SMC32:
1306 case FFA_CONSOLE_LOG_SMC64:
1307 /* This interface must not be forwarded to other worlds. */
1308 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1309 break; /* not reached */
1310
Madhukar Pappireddy6671b3d2023-03-02 16:04:38 -06001311 case FFA_EL3_INTR_HANDLE:
1312 if (secure_origin) {
1313 return spmd_handle_group0_intr_swd(handle);
1314 } else {
Madhukar Pappireddy6c91fc42023-07-12 16:28:05 -05001315 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
Madhukar Pappireddy6671b3d2023-03-02 16:04:38 -06001316 }
Achin Guptabdd25962019-10-11 15:41:16 +01001317 default:
1318 WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
J-Alves662af362020-05-07 18:42:25 +01001319 return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
Achin Guptabdd25962019-10-11 15:41:16 +01001320 }
1321}