xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 55fd56d7abece6c14607f03899fe94ac47b0fcae)
1bdd2596dSAchin Gupta /*
28f60d99fSRakshit Goyal  * Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
3bdd2596dSAchin Gupta  *
4bdd2596dSAchin Gupta  * SPDX-License-Identifier: BSD-3-Clause
5bdd2596dSAchin Gupta  */
6bdd2596dSAchin Gupta 
7bdd2596dSAchin Gupta #include <assert.h>
8bdd2596dSAchin Gupta #include <errno.h>
94ce3e99aSScott Branden #include <inttypes.h>
104ce3e99aSScott Branden #include <stdint.h>
11bdd2596dSAchin Gupta #include <string.h>
12bdd2596dSAchin Gupta 
13bdd2596dSAchin Gupta #include <arch_helpers.h>
1452696946SOlivier Deprez #include <arch/aarch64/arch_features.h>
15bdd2596dSAchin Gupta #include <bl31/bl31.h>
168cb99c3fSOlivier Deprez #include <bl31/interrupt_mgmt.h>
17bdd2596dSAchin Gupta #include <common/debug.h>
18bdd2596dSAchin Gupta #include <common/runtime_svc.h>
190cea2ae0SManish V Badarkhe #include <common/tbbr/tbbr_img_def.h>
20bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h>
210cea2ae0SManish V Badarkhe #include <lib/fconf/fconf.h>
220cea2ae0SManish V Badarkhe #include <lib/fconf/fconf_dyn_cfg_getter.h>
23bdd2596dSAchin Gupta #include <lib/smccc.h>
24bdd2596dSAchin Gupta #include <lib/spinlock.h>
25bdd2596dSAchin Gupta #include <lib/utils.h>
260cea2ae0SManish V Badarkhe #include <lib/xlat_tables/xlat_tables_v2.h>
27bdd2596dSAchin Gupta #include <plat/common/common_def.h>
28bdd2596dSAchin Gupta #include <plat/common/platform.h>
29bdd2596dSAchin Gupta #include <platform_def.h>
30890b5088SRaghu Krishnamurthy #include <services/el3_spmd_logical_sp.h>
31662af36dSJ-Alves #include <services/ffa_svc.h>
326da76075SMarc Bonnici #include <services/spmc_svc.h>
33bdd2596dSAchin Gupta #include <services/spmd_svc.h>
34bdd2596dSAchin Gupta #include <smccc_helpers.h>
35bdd2596dSAchin Gupta #include "spmd_private.h"
36*55fd56d7SYeoreum Yun #if TRANSFER_LIST
37*55fd56d7SYeoreum Yun #include <transfer_list.h>
38*55fd56d7SYeoreum Yun #endif
39bdd2596dSAchin Gupta 
40bdd2596dSAchin Gupta /*******************************************************************************
41bdd2596dSAchin Gupta  * SPM Core context information.
42bdd2596dSAchin Gupta  ******************************************************************************/
4352696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
44bdd2596dSAchin Gupta 
45bdd2596dSAchin Gupta /*******************************************************************************
466da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
476da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
48bdd2596dSAchin Gupta  ******************************************************************************/
4952696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
500f14d02fSMax Shvetsov 
510f14d02fSMax Shvetsov /*******************************************************************************
52bb9fc8c0SJay Monkman  * FFA version used by nonsecure endpoint.
53bb9fc8c0SJay Monkman  ******************************************************************************/
54bb9fc8c0SJay Monkman static uint32_t nonsecure_ffa_version;
55bb9fc8c0SJay Monkman 
56bb9fc8c0SJay Monkman /*******************************************************************************
57bb9fc8c0SJay Monkman  * Whether the normal world finished negotiating its version.
58bb9fc8c0SJay Monkman  ******************************************************************************/
59bb9fc8c0SJay Monkman static bool nonsecure_version_negotiated;
60bb9fc8c0SJay Monkman 
61bb9fc8c0SJay Monkman /*******************************************************************************
62bb9fc8c0SJay Monkman  * FFA version used by SPMC, as seen by the normal world.
63bb9fc8c0SJay Monkman  ******************************************************************************/
64bb9fc8c0SJay Monkman static uint32_t spmc_nwd_ffa_version;
65bb9fc8c0SJay Monkman 
66bb9fc8c0SJay Monkman /*******************************************************************************
670f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
680f14d02fSMax Shvetsov  * on secondary cores.
690f14d02fSMax Shvetsov  ******************************************************************************/
700f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
710f14d02fSMax Shvetsov 
720f14d02fSMax Shvetsov /*******************************************************************************
7352696946SOlivier Deprez  * SPM Core context on current CPU get helper.
7452696946SOlivier Deprez  ******************************************************************************/
7552696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
7652696946SOlivier Deprez {
77c8cea3b8SOlivier Deprez 	return &spm_core_context[plat_my_core_pos()];
7852696946SOlivier Deprez }
7952696946SOlivier Deprez 
8052696946SOlivier Deprez /*******************************************************************************
81a92bc73bSOlivier Deprez  * SPM Core ID getter.
82a92bc73bSOlivier Deprez  ******************************************************************************/
83a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
84a92bc73bSOlivier Deprez {
85a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
86a92bc73bSOlivier Deprez }
87a92bc73bSOlivier Deprez 
88a92bc73bSOlivier Deprez /*******************************************************************************
890f14d02fSMax Shvetsov  * Static function declaration.
900f14d02fSMax Shvetsov  ******************************************************************************/
910f14d02fSMax Shvetsov static int32_t spmd_init(void);
9223d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
9395f7f6d8SRaghu Krishnamurthy 
9452696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
9552696946SOlivier Deprez 				 bool secure_origin,
9652696946SOlivier Deprez 				 uint64_t x1,
9752696946SOlivier Deprez 				 uint64_t x2,
9852696946SOlivier Deprez 				 uint64_t x3,
9952696946SOlivier Deprez 				 uint64_t x4,
100bb01a673SMarc Bonnici 				 void *cookie,
101bb01a673SMarc Bonnici 				 void *handle,
102bb9fc8c0SJay Monkman 				 uint64_t flags,
103bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version);
104bdd2596dSAchin Gupta 
1059944f557SDaniel Boulby /******************************************************************************
1069944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
1079944f557SDaniel Boulby  *****************************************************************************/
1089944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
1099944f557SDaniel Boulby 			     unsigned long long message)
1109944f557SDaniel Boulby {
1119944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1129944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1139944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1149944f557SDaniel Boulby 		 spmd_spmc_id_get());
1159944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1169944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
11776d53ee1SOlivier Deprez 
11876d53ee1SOlivier Deprez 	/* Zero out x4-x7 for the direct request emitted towards the SPMC. */
11976d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
12076d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
12176d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
12276d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1239944f557SDaniel Boulby }
1249944f557SDaniel Boulby 
1259944f557SDaniel Boulby 
126bdd2596dSAchin Gupta /*******************************************************************************
12752696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
12852696946SOlivier Deprez  * SPMC entry.
129bdd2596dSAchin Gupta  ******************************************************************************/
130bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
131bdd2596dSAchin Gupta {
132bdd2596dSAchin Gupta 	uint64_t rc;
133bdd2596dSAchin Gupta 
134bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
135bdd2596dSAchin Gupta 
136bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
137bdd2596dSAchin Gupta 
138bdd2596dSAchin Gupta 	/* Restore the context assigned above */
139033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
14028f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
141678ce223SOlivier Deprez #else
142678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
143033039f8SMax Shvetsov #endif
144bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
145bdd2596dSAchin Gupta 
146033039f8SMax Shvetsov 	/* Enter SPMC */
147bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
148bdd2596dSAchin Gupta 
149bdd2596dSAchin Gupta 	/* Save secure state */
150033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
15128f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
152678ce223SOlivier Deprez #else
153678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
154033039f8SMax Shvetsov #endif
155bdd2596dSAchin Gupta 
156bdd2596dSAchin Gupta 	return rc;
157bdd2596dSAchin Gupta }
158bdd2596dSAchin Gupta 
159bdd2596dSAchin Gupta /*******************************************************************************
16052696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
161bdd2596dSAchin Gupta  * called originally.
162bdd2596dSAchin Gupta  ******************************************************************************/
163bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
164bdd2596dSAchin Gupta {
16552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
166bdd2596dSAchin Gupta 
16752696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
168bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
169bdd2596dSAchin Gupta 
170bdd2596dSAchin Gupta 	/*
171bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
172bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
173bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
174bdd2596dSAchin Gupta 	 */
175bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
176bdd2596dSAchin Gupta 
177bdd2596dSAchin Gupta 	panic();
178bdd2596dSAchin Gupta }
179bdd2596dSAchin Gupta 
180bdd2596dSAchin Gupta /*******************************************************************************
18152696946SOlivier Deprez  * Jump to the SPM Core for the first time.
182bdd2596dSAchin Gupta  ******************************************************************************/
183bdd2596dSAchin Gupta static int32_t spmd_init(void)
184bdd2596dSAchin Gupta {
18552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
18652696946SOlivier Deprez 	uint64_t rc;
187bdd2596dSAchin Gupta 
18852696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1899dcf63ddSOlivier Deprez 
190f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
191f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
192bdd2596dSAchin Gupta 
193bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
19452696946SOlivier Deprez 	if (rc != 0ULL) {
1954ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
19652696946SOlivier Deprez 		return 0;
197bdd2596dSAchin Gupta 	}
198bdd2596dSAchin Gupta 
1999dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
2009dcf63ddSOlivier Deprez 
20152696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
202bdd2596dSAchin Gupta 
203890b5088SRaghu Krishnamurthy 	spmd_logical_sp_set_spmc_initialized();
204890b5088SRaghu Krishnamurthy 	rc = spmd_logical_sp_init();
205890b5088SRaghu Krishnamurthy 	if (rc != 0) {
206890b5088SRaghu Krishnamurthy 		WARN("SPMD Logical partitions failed init.\n");
207890b5088SRaghu Krishnamurthy 	}
208890b5088SRaghu Krishnamurthy 
209bdd2596dSAchin Gupta 	return 1;
210bdd2596dSAchin Gupta }
211bdd2596dSAchin Gupta 
212bdd2596dSAchin Gupta /*******************************************************************************
2138cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
2148cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
2158cb99c3fSOlivier Deprez  * itself or a Secure Partition.
2168cb99c3fSOlivier Deprez  ******************************************************************************/
2178cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
2188cb99c3fSOlivier Deprez 					      uint32_t flags,
2198cb99c3fSOlivier Deprez 					      void *handle,
2208cb99c3fSOlivier Deprez 					      void *cookie)
2218cb99c3fSOlivier Deprez {
2228cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2238cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2248cb99c3fSOlivier Deprez 	int64_t rc;
2258cb99c3fSOlivier Deprez 
2268cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2278cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2288cb99c3fSOlivier Deprez 
2298cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2308cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2318cb99c3fSOlivier Deprez 
2328cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2338cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2348cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2352d960a11SMadhukar Pappireddy #else
2362d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_save(NON_SECURE);
23759bdcc58SMadhukar Pappireddy 
23859bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
23959bdcc58SMadhukar Pappireddy 	/*
24059bdcc58SMadhukar Pappireddy 	 * The hint bit denoting absence of SVE live state is effectively false
24159bdcc58SMadhukar Pappireddy 	 * in this scenario where execution was trapped to EL3 due to FIQ.
24259bdcc58SMadhukar Pappireddy 	 */
24359bdcc58SMadhukar Pappireddy 	simd_ctx_save(NON_SECURE, false);
2448f60d99fSRakshit Goyal 	simd_ctx_restore(SECURE);
24559bdcc58SMadhukar Pappireddy #endif
2468cb99c3fSOlivier Deprez #endif
2478cb99c3fSOlivier Deprez 
2488cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2498cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2508cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2518cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2528cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2538cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2548cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2558cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2568cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2578cb99c3fSOlivier Deprez 
2588cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2598cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2608cb99c3fSOlivier Deprez 
2618cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
26259bdcc58SMadhukar Pappireddy 
2638cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
264eac8077aSOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, plat_my_core_pos());
2658cb99c3fSOlivier Deprez 	}
2668cb99c3fSOlivier Deprez 
2678cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2688cb99c3fSOlivier Deprez 
2698cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2708cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2712d960a11SMadhukar Pappireddy #else
2722d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_restore(NON_SECURE);
27359bdcc58SMadhukar Pappireddy 
27459bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
2758f60d99fSRakshit Goyal 	simd_ctx_save(SECURE, false);
27659bdcc58SMadhukar Pappireddy 	simd_ctx_restore(NON_SECURE);
27759bdcc58SMadhukar Pappireddy #endif
2788cb99c3fSOlivier Deprez #endif
2798cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2808cb99c3fSOlivier Deprez 
2818cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2828cb99c3fSOlivier Deprez }
2838cb99c3fSOlivier Deprez 
284bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
285a1e0e871SMadhukar Pappireddy /*******************************************************************************
286a1e0e871SMadhukar Pappireddy  * spmd_group0_interrupt_handler_nwd
287a1e0e871SMadhukar Pappireddy  * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
288a1e0e871SMadhukar Pappireddy  * handling of the interrupt to the platform handler, and return only upon
289a1e0e871SMadhukar Pappireddy  * successfully handling the Group0 interrupt.
290a1e0e871SMadhukar Pappireddy  ******************************************************************************/
291a1e0e871SMadhukar Pappireddy static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
292a1e0e871SMadhukar Pappireddy 						  uint32_t flags,
293a1e0e871SMadhukar Pappireddy 						  void *handle,
294a1e0e871SMadhukar Pappireddy 						  void *cookie)
295a1e0e871SMadhukar Pappireddy {
2965b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
297a1e0e871SMadhukar Pappireddy 
298a1e0e871SMadhukar Pappireddy 	/* Sanity check the security state when the exception was generated. */
299a1e0e871SMadhukar Pappireddy 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
300a1e0e871SMadhukar Pappireddy 
301a1e0e871SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context. */
302a1e0e871SMadhukar Pappireddy 	assert(handle == cm_get_context(NON_SECURE));
303a1e0e871SMadhukar Pappireddy 
304a1e0e871SMadhukar Pappireddy 	assert(id == INTR_ID_UNAVAILABLE);
305a1e0e871SMadhukar Pappireddy 
306a1e0e871SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
307a1e0e871SMadhukar Pappireddy 
3085b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3095b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3105b10f25aSMadhukar Pappireddy 
3115b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3125b10f25aSMadhukar Pappireddy 		return 0U;
3135b10f25aSMadhukar Pappireddy 	}
314a1e0e871SMadhukar Pappireddy 
315a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
316a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
317a1e0e871SMadhukar Pappireddy 		panic();
318a1e0e871SMadhukar Pappireddy 	}
319a1e0e871SMadhukar Pappireddy 
3206c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3216c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3226c91fc44SMadhukar Pappireddy 
323a1e0e871SMadhukar Pappireddy 	return 0U;
324a1e0e871SMadhukar Pappireddy }
325bb6d0a17SOlivier Deprez #endif
326a1e0e871SMadhukar Pappireddy 
3276671b3d8SMadhukar Pappireddy /*******************************************************************************
3286671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
3296671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
3306671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
3316671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
3326671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
3336671b3d8SMadhukar Pappireddy  ******************************************************************************/
3346671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
3356671b3d8SMadhukar Pappireddy {
3365b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
3376671b3d8SMadhukar Pappireddy 
3386671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3396671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3406671b3d8SMadhukar Pappireddy 
3416671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3426671b3d8SMadhukar Pappireddy 
3435b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3445b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3455b10f25aSMadhukar Pappireddy 
3465b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3475b10f25aSMadhukar Pappireddy 		return 0U;
3485b10f25aSMadhukar Pappireddy 	}
3496671b3d8SMadhukar Pappireddy 
3506671b3d8SMadhukar Pappireddy 	/*
3516671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3526671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3536671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3546671b3d8SMadhukar Pappireddy 	 */
3556671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3566671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3576671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3586671b3d8SMadhukar Pappireddy 		panic();
3596671b3d8SMadhukar Pappireddy 	}
3606671b3d8SMadhukar Pappireddy 
3616c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3626c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3636c91fc44SMadhukar Pappireddy 
3646671b3d8SMadhukar Pappireddy 	/* Return success. */
3656671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3666671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3676671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3686671b3d8SMadhukar Pappireddy }
3696671b3d8SMadhukar Pappireddy 
3700cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3710cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3720cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3730cea2ae0SManish V Badarkhe 				 size_t *align_size)
3740cea2ae0SManish V Badarkhe {
3750cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3760cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3770cea2ae0SManish V Badarkhe 	int rc;
3780cea2ae0SManish V Badarkhe 
3790cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3800cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
3810cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
3820cea2ae0SManish V Badarkhe 
3830cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
3840cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
3850cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
3860cea2ae0SManish V Badarkhe 	}
3870cea2ae0SManish V Badarkhe 
3880cea2ae0SManish V Badarkhe 	/*
3890cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
3900cea2ae0SManish V Badarkhe 	 * size
3910cea2ae0SManish V Badarkhe 	 */
3920cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
3930cea2ae0SManish V Badarkhe 				     base_addr_align,
3940cea2ae0SManish V Badarkhe 				     mapped_size_align,
3950cea2ae0SManish V Badarkhe 				     attr);
3960cea2ae0SManish V Badarkhe 	if (rc == 0) {
3970cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
3980cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
3990cea2ae0SManish V Badarkhe 	}
4000cea2ae0SManish V Badarkhe 
4010cea2ae0SManish V Badarkhe 	return rc;
4020cea2ae0SManish V Badarkhe }
4030cea2ae0SManish V Badarkhe 
4040cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
4050cea2ae0SManish V Badarkhe 			    size_t size)
4060cea2ae0SManish V Badarkhe {
4070cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
4080cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
4090cea2ae0SManish V Badarkhe 	int rc;
4100cea2ae0SManish V Badarkhe 
4110cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
4120cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
4130cea2ae0SManish V Badarkhe 	assert(size != 0UL);
4140cea2ae0SManish V Badarkhe 
4150cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
4160cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
4170cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
4180cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
4190cea2ae0SManish V Badarkhe 	if (rc != 0) {
4200cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
4210cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
4220cea2ae0SManish V Badarkhe 		panic();
4230cea2ae0SManish V Badarkhe 	}
4240cea2ae0SManish V Badarkhe 
4250cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
4260cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
4270cea2ae0SManish V Badarkhe 	if (rc != 0) {
4280cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
4290cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
4300cea2ae0SManish V Badarkhe 		panic();
4310cea2ae0SManish V Badarkhe 	}
4320cea2ae0SManish V Badarkhe 
4330cea2ae0SManish V Badarkhe 	/* Do copy operation */
4340cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
4350cea2ae0SManish V Badarkhe 
4360cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
4370cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
4380cea2ae0SManish V Badarkhe 					root_mapped_size_align);
4390cea2ae0SManish V Badarkhe 	if (rc != 0) {
4400cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4410cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
4420cea2ae0SManish V Badarkhe 		panic();
4430cea2ae0SManish V Badarkhe 	}
4440cea2ae0SManish V Badarkhe 
4450cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4460cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4470cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4480cea2ae0SManish V Badarkhe 	if (rc != 0) {
4490cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4500cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4510cea2ae0SManish V Badarkhe 		panic();
4520cea2ae0SManish V Badarkhe 	}
4530cea2ae0SManish V Badarkhe }
4540cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4550cea2ae0SManish V Badarkhe 
4568cb99c3fSOlivier Deprez /*******************************************************************************
45752696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4580f14d02fSMax Shvetsov  ******************************************************************************/
45923d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4600f14d02fSMax Shvetsov {
461f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
462f2dcf418SOlivier Deprez 	unsigned int core_id;
4638cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
46452696946SOlivier Deprez 	int rc;
4650cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4660f14d02fSMax Shvetsov 
46752696946SOlivier Deprez 	/* Load the SPM Core manifest */
46823d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4690f14d02fSMax Shvetsov 	if (rc != 0) {
47052696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
47152696946SOlivier Deprez 		return rc;
4720f14d02fSMax Shvetsov 	}
4730f14d02fSMax Shvetsov 
4740f14d02fSMax Shvetsov 	/*
47552696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
47652696946SOlivier Deprez 	 * Dispatcher version.
4770f14d02fSMax Shvetsov 	 */
478662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
479662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
480662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4810f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
48252696946SOlivier Deprez 		return -EINVAL;
4830f14d02fSMax Shvetsov 	}
4840f14d02fSMax Shvetsov 
485662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
4860f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
4870f14d02fSMax Shvetsov 
48852696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
489033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
4900f14d02fSMax Shvetsov 
491ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
49252696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
49352696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
49452696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
49552696946SOlivier Deprez 		return -EINVAL;
496ac03ac5eSMax Shvetsov 	}
497ac03ac5eSMax Shvetsov 
49852696946SOlivier Deprez 	/* Validate the SPM Core execution state */
4990f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
5000f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
50123d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
5020f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
50352696946SOlivier Deprez 		return -EINVAL;
5040f14d02fSMax Shvetsov 	}
5050f14d02fSMax Shvetsov 
50623d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
50723d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
5080f14d02fSMax Shvetsov 
509033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
510033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
511033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
512033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
51352696946SOlivier Deprez 		return -EINVAL;
5140f14d02fSMax Shvetsov 	}
5150f14d02fSMax Shvetsov 
5160f14d02fSMax Shvetsov 	/*
5170f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
5180f14d02fSMax Shvetsov 	 * is required for SPM
5190f14d02fSMax Shvetsov 	 */
520623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
52152696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
52252696946SOlivier Deprez 		return -EINVAL;
5230f14d02fSMax Shvetsov 	}
524033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
5250f14d02fSMax Shvetsov 
5260f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
5270f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
52852696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
5290f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
5300f14d02fSMax Shvetsov 	}
5310f14d02fSMax Shvetsov 
5320f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
5330f14d02fSMax Shvetsov 
5340f14d02fSMax Shvetsov 	/*
53552696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
53652696946SOlivier Deprez 	 * manifest.
5370f14d02fSMax Shvetsov 	 */
5380f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
5390f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
5400f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
5410f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
5420f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5430f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5440f14d02fSMax Shvetsov 	} else {
545033039f8SMax Shvetsov 
546033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
547033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
548033039f8SMax Shvetsov #else
549033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
550033039f8SMax Shvetsov #endif
551033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5520f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5530f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5540f14d02fSMax Shvetsov 	}
5550f14d02fSMax Shvetsov 
5560cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5570cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5580cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5590cea2ae0SManish V Badarkhe 
5600cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5610cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5620cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5630cea2ae0SManish V Badarkhe 		return -EINVAL;
5640cea2ae0SManish V Badarkhe 	}
5650cea2ae0SManish V Badarkhe 
5660cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5670cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5680cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5690cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5700cea2ae0SManish V Badarkhe 
5710cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5720cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5730cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5740cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5750cea2ae0SManish V Badarkhe 
576f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
577f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
578f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
5790f14d02fSMax Shvetsov 
580f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
581f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
582f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
5830f14d02fSMax Shvetsov 
584f2dcf418SOlivier Deprez 		/*
585f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
586f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
587f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
588f2dcf418SOlivier Deprez 		 */
589f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
590f2dcf418SOlivier Deprez 	}
5910f14d02fSMax Shvetsov 
592a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
593a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
594a334c4e6SOlivier Deprez 
5950f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5960f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
5970f14d02fSMax Shvetsov 
598f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
599f2dcf418SOlivier Deprez 
6008cb99c3fSOlivier Deprez 	/*
6018cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
6028cb99c3fSOlivier Deprez 	 * while the NWd is running.
6038cb99c3fSOlivier Deprez 	 */
6048cb99c3fSOlivier Deprez 	flags = 0;
6058cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
6068cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
6078cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
6088cb99c3fSOlivier Deprez 					     flags);
6098cb99c3fSOlivier Deprez 	if (rc != 0) {
6108cb99c3fSOlivier Deprez 		panic();
6118cb99c3fSOlivier Deprez 	}
6128cb99c3fSOlivier Deprez 
613a1e0e871SMadhukar Pappireddy 	/*
614bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
615bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
616bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
617bb6d0a17SOlivier Deprez 	 *
618bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
619bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
620bb6d0a17SOlivier Deprez 	 *
621bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
622bb6d0a17SOlivier Deprez 	 *
623bb6d0a17SOlivier Deprez 	 */
624bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
625bb6d0a17SOlivier Deprez 	/*
626fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
627fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
628fca5f0ebSMadhukar Pappireddy 	 * running.
629a1e0e871SMadhukar Pappireddy 	 */
630fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
631a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
632a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
633a1e0e871SMadhukar Pappireddy 						     flags);
634a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
635a1e0e871SMadhukar Pappireddy 			panic();
636a1e0e871SMadhukar Pappireddy 		}
637fca5f0ebSMadhukar Pappireddy 	}
638bb6d0a17SOlivier Deprez #endif
639bb6d0a17SOlivier Deprez 
6400f14d02fSMax Shvetsov 	return 0;
6410f14d02fSMax Shvetsov }
6420f14d02fSMax Shvetsov 
6430f14d02fSMax Shvetsov /*******************************************************************************
64452696946SOlivier Deprez  * Initialize context of SPM Core.
645bdd2596dSAchin Gupta  ******************************************************************************/
6460f14d02fSMax Shvetsov int spmd_setup(void)
647bdd2596dSAchin Gupta {
648bdd2596dSAchin Gupta 	int rc;
6496da76075SMarc Bonnici 	void *spmc_manifest;
650*55fd56d7SYeoreum Yun 	struct transfer_list_header *tl __maybe_unused;
651*55fd56d7SYeoreum Yun 	struct transfer_list_entry *te __maybe_unused;
6526da76075SMarc Bonnici 
6536da76075SMarc Bonnici 	/*
6546da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6556da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6566da76075SMarc Bonnici 	 */
6576da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6586da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6596da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6606da76075SMarc Bonnici 
6616da76075SMarc Bonnici 		rc = spmc_setup();
6626da76075SMarc Bonnici 		if (rc != 0) {
6630d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6646da76075SMarc Bonnici 		}
6650d33649eSOlivier Deprez 		return 0;
6666da76075SMarc Bonnici 	}
667bdd2596dSAchin Gupta 
668bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
66952696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
67052696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6710d33649eSOlivier Deprez 		return 0;
672bdd2596dSAchin Gupta 	}
673bdd2596dSAchin Gupta 
674bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
67552696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
676bdd2596dSAchin Gupta 
677*55fd56d7SYeoreum Yun 
678*55fd56d7SYeoreum Yun #if TRANSFER_LIST && !RESET_TO_BL31
679*55fd56d7SYeoreum Yun 	tl = (struct transfer_list_header *)spmc_ep_info->args.arg3;
680*55fd56d7SYeoreum Yun 	te = transfer_list_find(tl, TL_TAG_DT_SPMC_MANIFEST);
681*55fd56d7SYeoreum Yun 	if (te == NULL) {
682*55fd56d7SYeoreum Yun 		WARN("SPM Core manifest absent in TRANSFER_LIST.\n");
683*55fd56d7SYeoreum Yun 		return -ENOENT;
684*55fd56d7SYeoreum Yun 	}
685*55fd56d7SYeoreum Yun 
686*55fd56d7SYeoreum Yun 	spmc_manifest = (void *)transfer_list_entry_data(te);
687*55fd56d7SYeoreum Yun 
688*55fd56d7SYeoreum Yun 	/* Change the DT in the handoff */
689*55fd56d7SYeoreum Yun 	if (sizeof(spmc_ep_info->args.arg0) == sizeof(uint64_t)) {
690*55fd56d7SYeoreum Yun 		spmc_ep_info->args.arg0 = (uintptr_t)spmc_manifest;
691*55fd56d7SYeoreum Yun 	} else {
692*55fd56d7SYeoreum Yun 		spmc_ep_info->args.arg3 = (uintptr_t)spmc_manifest;
693*55fd56d7SYeoreum Yun 	}
694*55fd56d7SYeoreum Yun #else
695bdd2596dSAchin Gupta 	/*
696bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
69752696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
698bdd2596dSAchin Gupta 	 */
69923d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
700*55fd56d7SYeoreum Yun #endif
701*55fd56d7SYeoreum Yun 
70223d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
7030d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
7040d33649eSOlivier Deprez 		return 0;
705bdd2596dSAchin Gupta 	}
706bdd2596dSAchin Gupta 
7070f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
70823d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
7090f14d02fSMax Shvetsov 	if (rc != 0) {
71052696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
711bdd2596dSAchin Gupta 	}
712bdd2596dSAchin Gupta 
7130d33649eSOlivier Deprez 	return 0;
7140f14d02fSMax Shvetsov }
7150f14d02fSMax Shvetsov 
7160f14d02fSMax Shvetsov /*******************************************************************************
717bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
7180f14d02fSMax Shvetsov  ******************************************************************************/
719bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
72052696946SOlivier Deprez 			       bool secure_origin,
72152696946SOlivier Deprez 			       uint64_t x1,
72252696946SOlivier Deprez 			       uint64_t x2,
72352696946SOlivier Deprez 			       uint64_t x3,
72452696946SOlivier Deprez 			       uint64_t x4,
725c925867eSOlivier Deprez 			       void *handle,
726bb9fc8c0SJay Monkman 			       uint64_t flags,
727bb9fc8c0SJay Monkman 			       uint32_t secure_ffa_version)
7280f14d02fSMax Shvetsov {
729c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
730c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
731bb9fc8c0SJay Monkman 	uint32_t version_in = (secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
732bb9fc8c0SJay Monkman 	uint32_t version_out = (!secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
733107e3cc0SOlivier Deprez 	void *ctx_out;
73493ff138bSOlivier Deprez 
735c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
736c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
737c925867eSOlivier Deprez 		/*
738c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
739c925867eSOlivier Deprez 		 * if it was set by the caller.
740c925867eSOlivier Deprez 		 */
741c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
742c925867eSOlivier Deprez 	}
743c925867eSOlivier Deprez #endif
744c925867eSOlivier Deprez 
7450f14d02fSMax Shvetsov 	/* Save incoming security state */
746033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
74793ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
748678ce223SOlivier Deprez #else
749678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
75059bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
75159bdcc58SMadhukar Pappireddy 	/* Forward the hint bit denoting the absence of SVE live state. */
75259bdcc58SMadhukar Pappireddy 	simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
75359bdcc58SMadhukar Pappireddy #endif
754033039f8SMax Shvetsov #endif
7550f14d02fSMax Shvetsov 
7560f14d02fSMax Shvetsov 	/* Restore outgoing security state */
757033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
75893ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
759678ce223SOlivier Deprez #else
760678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
76159bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
76259bdcc58SMadhukar Pappireddy 	simd_ctx_restore(secure_state_out);
76359bdcc58SMadhukar Pappireddy #endif
764033039f8SMax Shvetsov #endif
76593ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
7660f14d02fSMax Shvetsov 
767107e3cc0SOlivier Deprez 	ctx_out = cm_get_context(secure_state_out);
768a0a7f158SAndrei Homescu 	if (smc_fid == FFA_NORMAL_WORLD_RESUME) {
769a0a7f158SAndrei Homescu 		SMC_RET0(ctx_out);
770a0a7f158SAndrei Homescu 	}
771a0a7f158SAndrei Homescu 
772bb9fc8c0SJay Monkman 	if ((GET_SMC_CC(smc_fid) == SMC_64) && (version_out >= MAKE_FFA_VERSION(U(1), U(2)))) {
773bb9fc8c0SJay Monkman 		if (version_in < MAKE_FFA_VERSION(U(1), U(2))) {
774bb9fc8c0SJay Monkman 			/* FFA version mismatch, with dest >= 1.2 - set outgoing x8-x17 to zero */
775bb9fc8c0SJay Monkman 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
776bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X5),
777bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X6),
778bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X7),
779bb9fc8c0SJay Monkman 				  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
780bb9fc8c0SJay Monkman 		} else {
781bb9fc8c0SJay Monkman 			/* Both FFA versions >= 1.2 - pass incoming x8-x17 to dest */
782107e3cc0SOlivier Deprez 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
783eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X5),
784eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X6),
785eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X7),
786eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X8),
787eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X9),
788eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X10),
789eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X11),
790eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X12),
791eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X13),
792eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X14),
793eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X15),
794eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X16),
795eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X17)
796eaaf517cSRaghu Krishnamurthy 				);
797bb9fc8c0SJay Monkman 		}
798bb9fc8c0SJay Monkman 	} else {
799bb9fc8c0SJay Monkman 		/* 32 bit call or dest has FFA version < 1.2 or unknown */
800107e3cc0SOlivier Deprez 		SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
8010f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X5),
8020f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X6),
8030f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X7));
804bb9fc8c0SJay Monkman 	}
8050f14d02fSMax Shvetsov }
8060f14d02fSMax Shvetsov 
8070f14d02fSMax Shvetsov /*******************************************************************************
808bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
809bb01a673SMarc Bonnici  ******************************************************************************/
810bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
811bb01a673SMarc Bonnici 				 bool secure_origin,
812bb01a673SMarc Bonnici 				 uint64_t x1,
813bb01a673SMarc Bonnici 				 uint64_t x2,
814bb01a673SMarc Bonnici 				 uint64_t x3,
815bb01a673SMarc Bonnici 				 uint64_t x4,
816bb01a673SMarc Bonnici 				 void *cookie,
817bb01a673SMarc Bonnici 				 void *handle,
818bb9fc8c0SJay Monkman 				 uint64_t flags,
819bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version)
820bb01a673SMarc Bonnici {
821bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
822bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
823bb01a673SMarc Bonnici 					cookie, handle, flags);
824bb01a673SMarc Bonnici 	}
825c925867eSOlivier Deprez 
826bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
827bb9fc8c0SJay Monkman 				     handle, flags, secure_ffa_version);
828bb01a673SMarc Bonnici 
829bb01a673SMarc Bonnici }
830bb01a673SMarc Bonnici 
831bb01a673SMarc Bonnici /*******************************************************************************
832662af36dSJ-Alves  * Return FFA_ERROR with specified error code
8330f14d02fSMax Shvetsov  ******************************************************************************/
83495f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
8350f14d02fSMax Shvetsov {
836e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
837e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
838662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
839662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
840bdd2596dSAchin Gupta }
841bdd2596dSAchin Gupta 
842f0d743dbSOlivier Deprez /*******************************************************************************
843f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
844f0d743dbSOlivier Deprez  ******************************************************************************/
845f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
846f0d743dbSOlivier Deprez {
847f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
848f0d743dbSOlivier Deprez 
849f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
850f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
851f0d743dbSOlivier Deprez }
852f0d743dbSOlivier Deprez 
853c2901419SOlivier Deprez /******************************************************************************
854c2901419SOlivier Deprez  * spmd_is_spmc_message
855c2901419SOlivier Deprez  *****************************************************************************/
856c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
857c2901419SOlivier Deprez {
858bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
859bb01a673SMarc Bonnici 		return false;
860bb01a673SMarc Bonnici 	}
861bb01a673SMarc Bonnici 
862c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
863c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
864c2901419SOlivier Deprez }
865c2901419SOlivier Deprez 
866bdd2596dSAchin Gupta /*******************************************************************************
867bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
868bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
869bb01a673SMarc Bonnici  ******************************************************************************/
870bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
871bb01a673SMarc Bonnici 			      uint64_t x1,
872bb01a673SMarc Bonnici 			      uint64_t x2,
873bb01a673SMarc Bonnici 			      uint64_t x3,
874bb01a673SMarc Bonnici 			      uint64_t x4,
875bb01a673SMarc Bonnici 			      void *cookie,
876bb01a673SMarc Bonnici 			      void *handle,
877bb01a673SMarc Bonnici 			      uint64_t flags)
878bb01a673SMarc Bonnici {
879bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
880bb01a673SMarc Bonnici 		/*
881bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
882bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
883bb01a673SMarc Bonnici 		 */
884bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
885bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
886bb01a673SMarc Bonnici 						is_caller_secure(flags),
887bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
888bb01a673SMarc Bonnici 						handle, flags);
889bb01a673SMarc Bonnici 		}
890bb01a673SMarc Bonnici 	}
891bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
892bb9fc8c0SJay Monkman 				handle, flags, spmc_nwd_ffa_version);
893bb9fc8c0SJay Monkman }
894bb9fc8c0SJay Monkman 
895bb9fc8c0SJay Monkman static uint32_t get_common_ffa_version(uint32_t secure_ffa_version)
896bb9fc8c0SJay Monkman {
897bb9fc8c0SJay Monkman 	if (secure_ffa_version <= nonsecure_ffa_version) {
898bb9fc8c0SJay Monkman 		return secure_ffa_version;
899bb9fc8c0SJay Monkman 	} else {
900bb9fc8c0SJay Monkman 		return nonsecure_ffa_version;
901bb9fc8c0SJay Monkman 	}
902bb01a673SMarc Bonnici }
903bb01a673SMarc Bonnici 
904bb01a673SMarc Bonnici /*******************************************************************************
905662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
906bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
907bdd2596dSAchin Gupta  ******************************************************************************/
90852696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
90952696946SOlivier Deprez 			  uint64_t x1,
91052696946SOlivier Deprez 			  uint64_t x2,
91152696946SOlivier Deprez 			  uint64_t x3,
91252696946SOlivier Deprez 			  uint64_t x4,
91352696946SOlivier Deprez 			  void *cookie,
91452696946SOlivier Deprez 			  void *handle,
915bb9fc8c0SJay Monkman 			  uint64_t flags,
916bb9fc8c0SJay Monkman 			  uint32_t secure_ffa_version)
917bdd2596dSAchin Gupta {
91852696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
91993ff138bSOlivier Deprez 	bool secure_origin;
9206873088cSJ-Alves 	int ret;
9214388f28fSJ-Alves 	uint32_t input_version;
922bdd2596dSAchin Gupta 
923bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
92493ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
925bdd2596dSAchin Gupta 
9264ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
9274ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
928eac8077aSOlivier Deprez 		    plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
929cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
930bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
931bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
932bdd2596dSAchin Gupta 
9330b850e9eSRaghu Krishnamurthy 	/*
9340b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
9350b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
9360b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
9370b850e9eSRaghu Krishnamurthy 	 */
9380b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
9390b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
9400b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
9410b850e9eSRaghu Krishnamurthy 	}
9420b850e9eSRaghu Krishnamurthy 
943bb9fc8c0SJay Monkman 	if ((!secure_origin) && (smc_fid != FFA_VERSION)) {
944bb9fc8c0SJay Monkman 		/*
945bb9fc8c0SJay Monkman 		 * Once the caller invokes any FF-A ABI other than FFA_VERSION,
946bb9fc8c0SJay Monkman 		 * the version negotiation phase is complete.
947bb9fc8c0SJay Monkman 		 */
948bb9fc8c0SJay Monkman 		nonsecure_version_negotiated = true;
949bb9fc8c0SJay Monkman 	}
950bb9fc8c0SJay Monkman 
951bdd2596dSAchin Gupta 	switch (smc_fid) {
952662af36dSJ-Alves 	case FFA_ERROR:
953bdd2596dSAchin Gupta 		/*
954bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
95552696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
956bdd2596dSAchin Gupta 		 * unsuccessfully.
957bdd2596dSAchin Gupta 		 */
9589dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
959bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
9600f14d02fSMax Shvetsov 		}
961bdd2596dSAchin Gupta 
96266bdfd6eSRaghu Krishnamurthy 		/*
9638723eaf2SMadhukar Pappireddy 		 * Perform a synchronous exit:
9648723eaf2SMadhukar Pappireddy 		 * 1. If there was an SPMD logical partition direct request on-going,
96566bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
96666bdfd6eSRaghu Krishnamurthy 		 * consumed.
9678723eaf2SMadhukar Pappireddy 		 * 2. SPMC sent FFA_ERROR in response to a power management
9688723eaf2SMadhukar Pappireddy 		 * operation sent through direct request.
96966bdfd6eSRaghu Krishnamurthy 		 */
9708723eaf2SMadhukar Pappireddy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx) ||
9718723eaf2SMadhukar Pappireddy 		    ctx->psci_operation_ongoing) {
97266bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
97366bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
97466bdfd6eSRaghu Krishnamurthy 		}
97566bdfd6eSRaghu Krishnamurthy 
97693ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
977bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
978bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
979bdd2596dSAchin Gupta 		break; /* not reached */
980bdd2596dSAchin Gupta 
981662af36dSJ-Alves 	case FFA_VERSION:
9824388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
983bdd2596dSAchin Gupta 		/*
9844388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
9854388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
9864388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
987bb9fc8c0SJay Monkman 		 * forward to the EL3 SPMC if enabled, otherwise send a
988bb9fc8c0SJay Monkman 		 * framework message to the SPMC at the lower EL to
989bb9fc8c0SJay Monkman 		 * negotiate a version that is compatible between the
990bb9fc8c0SJay Monkman 		 * normal world and the SPMC.
9914388f28fSJ-Alves 		 * Sanity check to "input_version".
992bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
993bb01a673SMarc Bonnici 		 * this is not used.
994bdd2596dSAchin Gupta 		 */
9954388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
996bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
9974388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
9984388f28fSJ-Alves 		} else if (!secure_origin) {
999bb9fc8c0SJay Monkman 			if (!nonsecure_version_negotiated) {
1000bb9fc8c0SJay Monkman 				/*
1001bb9fc8c0SJay Monkman 				 * Once an FF-A version has been negotiated
1002bb9fc8c0SJay Monkman 				 * between a caller and a callee, the version
1003bb9fc8c0SJay Monkman 				 * may not be changed for the lifetime of
1004bb9fc8c0SJay Monkman 				 * the calling component.
1005bb9fc8c0SJay Monkman 				 */
1006bb9fc8c0SJay Monkman 				nonsecure_ffa_version = input_version;
1007bb9fc8c0SJay Monkman 			}
1008bb9fc8c0SJay Monkman 
10099576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
10109576fa93SMarc Bonnici 				/*
10119576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
10129576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
10139576fa93SMarc Bonnici 				 * a direct request.
10149576fa93SMarc Bonnici 				 */
1015bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version =
1016bb9fc8c0SJay Monkman 					MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
1017bb9fc8c0SJay Monkman 				return spmc_smc_handler(smc_fid, secure_origin,
10189576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
10199576fa93SMarc Bonnici 							handle, flags);
10209576fa93SMarc Bonnici 			}
10219576fa93SMarc Bonnici 
10229944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
10239944f557SDaniel Boulby 			uint64_t rc;
10249944f557SDaniel Boulby 
10259944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
10269944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
1027e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
1028e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
1029bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10309944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
10319944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10329944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10339944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10349944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10359944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
10369944f557SDaniel Boulby 				break;
10379944f557SDaniel Boulby 			}
10389944f557SDaniel Boulby 			/* Save non-secure system registers context */
10399944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
10409944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
10412d960a11SMadhukar Pappireddy #else
10422d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
10439944f557SDaniel Boulby #endif
10449944f557SDaniel Boulby 
10459944f557SDaniel Boulby 			/*
10469944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
10479944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
10489944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
10499944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
10509944f557SDaniel Boulby 			 */
10519944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
10529944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
10539944f557SDaniel Boulby 						input_version);
10549944f557SDaniel Boulby 
105576d53ee1SOlivier Deprez 			/*
105676d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
105776d53ee1SOlivier Deprez 			 * from the SPMC.
105876d53ee1SOlivier Deprez 			 */
105976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
106076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
106176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
106276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
106376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
106476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
106576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
106676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
106776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
106876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
106976d53ee1SOlivier Deprez 
10709944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
10719944f557SDaniel Boulby 
10729944f557SDaniel Boulby 			if ((rc != 0ULL) ||
10739944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
10749944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
10759944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
107659bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
10779944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
10789944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
10799944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
10809944f557SDaniel Boulby 			} else {
10819944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
1082bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10839944f557SDaniel Boulby 			}
10849944f557SDaniel Boulby 
10859944f557SDaniel Boulby 			/*
108676d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
108776d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
108876d53ee1SOlivier Deprez 			 */
108976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
109076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
109176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
109276d53ee1SOlivier Deprez 
109376d53ee1SOlivier Deprez 			/*
10949944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
10959944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
10969944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
10979944f557SDaniel Boulby 			 */
10989944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
10999944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1100bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
1101bb9fc8c0SJay Monkman 						flags, spmc_nwd_ffa_version);
11024388f28fSJ-Alves 		} else {
1103e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1104e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
11054388f28fSJ-Alves 		}
11064388f28fSJ-Alves 
1107e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1108e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1109e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1110bdd2596dSAchin Gupta 		break; /* not reached */
1111bdd2596dSAchin Gupta 
1112662af36dSJ-Alves 	case FFA_FEATURES:
1113bdd2596dSAchin Gupta 		/*
1114bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
111552696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1116bdd2596dSAchin Gupta 		 */
1117bdd2596dSAchin Gupta 
111852696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
111993ff138bSOlivier Deprez 		if (!secure_origin) {
112093ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1121bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1122bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
112352696946SOlivier Deprez 		}
112452696946SOlivier Deprez 
1125bdd2596dSAchin Gupta 		/*
1126bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1127662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1128bdd2596dSAchin Gupta 		 * nop.
1129bdd2596dSAchin Gupta 		 */
1130662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1131bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1132bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1133bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
11340f14d02fSMax Shvetsov 
1135bdd2596dSAchin Gupta 		break; /* not reached */
1136bdd2596dSAchin Gupta 
1137662af36dSJ-Alves 	case FFA_ID_GET:
1138ac03ac5eSMax Shvetsov 		/*
1139662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1140ac03ac5eSMax Shvetsov 		 */
1141ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1142662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1143662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1144662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1145662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1146662af36dSJ-Alves 				 FFA_PARAM_MBZ);
114752696946SOlivier Deprez 		}
114852696946SOlivier Deprez 
1149662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1150662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1151662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1152662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1153662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1154ac03ac5eSMax Shvetsov 
1155ac03ac5eSMax Shvetsov 		break; /* not reached */
1156ac03ac5eSMax Shvetsov 
1157cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1158cdb49d47SOlivier Deprez 		if (secure_origin) {
1159cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1160cdb49d47SOlivier Deprez 
1161cdb49d47SOlivier Deprez 			if (ret < 0) {
1162cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1163cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1164cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1165cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1166cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1167cdb49d47SOlivier Deprez 			} else {
1168cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1169cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1170cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1171cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1172cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1173cdb49d47SOlivier Deprez 			}
1174cdb49d47SOlivier Deprez 		}
1175cdb49d47SOlivier Deprez 
1176cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1177cdb49d47SOlivier Deprez 		break; /* Not reached */
1178cdb49d47SOlivier Deprez 
117970c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
118070c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
118170c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
118270c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
118370c121a2SDaniel Boulby 		}
118470c121a2SDaniel Boulby 		/*
118570c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
118670c121a2SDaniel Boulby 		 * instance where this function is invoked
118770c121a2SDaniel Boulby 		 */
118870c121a2SDaniel Boulby 		if (!secure_origin) {
118970c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
119070c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
119170c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119270c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119370c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
119470c121a2SDaniel Boulby 		}
119570c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
119670c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
119770c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119870c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119970c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
120070c121a2SDaniel Boulby 
120170c121a2SDaniel Boulby 		break; /* not reached */
120270c121a2SDaniel Boulby 
1203bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1204bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1205bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1206bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1207bb9fc8c0SJay Monkman 		}
1208bb9fc8c0SJay Monkman 		/* fallthrough */
1209f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
12105519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
121166bdfd6eSRaghu Krishnamurthy 		/*
121266bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
121366bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
121466bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
121566bdfd6eSRaghu Krishnamurthy 		 */
121666bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
121766bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
121866bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
121966bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
122066bdfd6eSRaghu Krishnamurthy 						     );
122166bdfd6eSRaghu Krishnamurthy 		}
122266bdfd6eSRaghu Krishnamurthy 
122366bdfd6eSRaghu Krishnamurthy 		/*
122466bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
122566bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
122666bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
122766bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
122866bdfd6eSRaghu Krishnamurthy 		 * error codes.
122966bdfd6eSRaghu Krishnamurthy 		 */
123066bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
123166bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
123266bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
123366bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
123466bdfd6eSRaghu Krishnamurthy 		}
123566bdfd6eSRaghu Krishnamurthy 
12365519f07cSShruti 		if (!secure_origin) {
12375519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
12385519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
12395519f07cSShruti 				return spmd_ffa_error_return(handle,
12405519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
12415519f07cSShruti 			}
12425519f07cSShruti 		}
1243f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1244cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
12456c378c2fSKathleen Capella 						FFA_ERROR_DENIED);
1246cc6047b3SKathleen Capella 		} else {
1247cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1248cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1249cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1250bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1251cc6047b3SKathleen Capella 		}
1252cc6047b3SKathleen Capella 		break; /* Not reached */
1253cc6047b3SKathleen Capella 
1254bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1255bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1256bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1257bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1258bb9fc8c0SJay Monkman 		}
1259bb9fc8c0SJay Monkman 		/* fallthrough */
1260f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
126166bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
126266bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
126366bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
12648cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1265f0d743dbSOlivier Deprez 		} else {
1266f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1267f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1268bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1269bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1270f0d743dbSOlivier Deprez 		}
1271f0d743dbSOlivier Deprez 		break; /* Not reached */
1272662af36dSJ-Alves 	case FFA_RX_RELEASE:
1273662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1274662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1275662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1276545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1277fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1278fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1279fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1280fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1281fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1282fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1283fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1284fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1285fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1286c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1287d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
1288fc3f4800SJ-Alves #endif
1289662af36dSJ-Alves 	case FFA_MSG_RUN:
1290c2eba07cSFederico Recanati 		/*
1291c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1292c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1293c2eba07cSFederico Recanati 		 */
129493ff138bSOlivier Deprez 		if (secure_origin) {
1295662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1296662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1297bdd2596dSAchin Gupta 		}
1298bdd2596dSAchin Gupta 
1299e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1300e138400dSBoyan Karatotev 		/* fallthrough */
1301662af36dSJ-Alves 	case FFA_MSG_SEND:
1302662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1303662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1304662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1305662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1306662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1307662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1308662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1309662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1310662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1311662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1312662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1313642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1314642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1315662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1316662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1317bdd2596dSAchin Gupta 		/*
131866bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
131966bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1320bdd2596dSAchin Gupta 		 */
132166bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
132266bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
132366bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
132466bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
132566bdfd6eSRaghu Krishnamurthy 		}
1326bdd2596dSAchin Gupta 
132793ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1328bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1329bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1330bdd2596dSAchin Gupta 		break; /* not reached */
1331bdd2596dSAchin Gupta 
1332662af36dSJ-Alves 	case FFA_MSG_WAIT:
1333bdd2596dSAchin Gupta 		/*
1334bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1335bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
133652696946SOlivier Deprez 		 * SPM Core initialised successfully.
1337bdd2596dSAchin Gupta 		 */
13389dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
13398cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1340bdd2596dSAchin Gupta 		}
1341bdd2596dSAchin Gupta 
1342e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1343e138400dSBoyan Karatotev 		/* fallthrough */
1344386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1345662af36dSJ-Alves 	case FFA_MSG_YIELD:
1346bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
134793ff138bSOlivier Deprez 		if (!secure_origin) {
1348662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1349662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1350bdd2596dSAchin Gupta 		}
1351bdd2596dSAchin Gupta 
135266bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
135366bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
135466bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
135566bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
135666bdfd6eSRaghu Krishnamurthy 		}
135766bdfd6eSRaghu Krishnamurthy 
135893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1359bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1360bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1361bdd2596dSAchin Gupta 		break; /* not reached */
1362bdd2596dSAchin Gupta 
13638cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
13648cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
13658cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
13668cb99c3fSOlivier Deprez 		} else {
13678cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
13688cb99c3fSOlivier Deprez 		}
13698cb99c3fSOlivier Deprez 		break; /* Not reached */
1370eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1371eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1372eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
137395f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
137495f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1375eaaf517cSRaghu Krishnamurthy 		}
13768cb99c3fSOlivier Deprez 
1377eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1378eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1379eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1380eaaf517cSRaghu Krishnamurthy 		}
1381eaaf517cSRaghu Krishnamurthy 
1382eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1383eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1384bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1385eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1386eaaf517cSRaghu Krishnamurthy #endif
1387638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1388638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1389638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1390638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1391638a6f8eSShruti Gupta 		break; /* not reached */
1392638a6f8eSShruti Gupta 
13936671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13946671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13956671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
13966671b3d8SMadhukar Pappireddy 		} else {
13976c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
13986671b3d8SMadhukar Pappireddy 		}
1399bdd2596dSAchin Gupta 	default:
1400bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1401662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1402bdd2596dSAchin Gupta 	}
1403bdd2596dSAchin Gupta }
1404