xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision bb9fc8c0bbf5f68f7461296ba36bf398091cb675)
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"
36bdd2596dSAchin Gupta 
37bdd2596dSAchin Gupta /*******************************************************************************
38bdd2596dSAchin Gupta  * SPM Core context information.
39bdd2596dSAchin Gupta  ******************************************************************************/
4052696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
41bdd2596dSAchin Gupta 
42bdd2596dSAchin Gupta /*******************************************************************************
436da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
446da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
45bdd2596dSAchin Gupta  ******************************************************************************/
4652696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
470f14d02fSMax Shvetsov 
480f14d02fSMax Shvetsov /*******************************************************************************
49*bb9fc8c0SJay Monkman  * FFA version used by nonsecure endpoint.
50*bb9fc8c0SJay Monkman  ******************************************************************************/
51*bb9fc8c0SJay Monkman static uint32_t nonsecure_ffa_version;
52*bb9fc8c0SJay Monkman 
53*bb9fc8c0SJay Monkman /*******************************************************************************
54*bb9fc8c0SJay Monkman  * Whether the normal world finished negotiating its version.
55*bb9fc8c0SJay Monkman  ******************************************************************************/
56*bb9fc8c0SJay Monkman static bool nonsecure_version_negotiated;
57*bb9fc8c0SJay Monkman 
58*bb9fc8c0SJay Monkman /*******************************************************************************
59*bb9fc8c0SJay Monkman  * FFA version used by SPMC, as seen by the normal world.
60*bb9fc8c0SJay Monkman  ******************************************************************************/
61*bb9fc8c0SJay Monkman static uint32_t spmc_nwd_ffa_version;
62*bb9fc8c0SJay Monkman 
63*bb9fc8c0SJay Monkman /*******************************************************************************
640f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
650f14d02fSMax Shvetsov  * on secondary cores.
660f14d02fSMax Shvetsov  ******************************************************************************/
670f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
680f14d02fSMax Shvetsov 
690f14d02fSMax Shvetsov /*******************************************************************************
7052696946SOlivier Deprez  * SPM Core context on current CPU get helper.
7152696946SOlivier Deprez  ******************************************************************************/
7252696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
7352696946SOlivier Deprez {
74c8cea3b8SOlivier Deprez 	return &spm_core_context[plat_my_core_pos()];
7552696946SOlivier Deprez }
7652696946SOlivier Deprez 
7752696946SOlivier Deprez /*******************************************************************************
78a92bc73bSOlivier Deprez  * SPM Core ID getter.
79a92bc73bSOlivier Deprez  ******************************************************************************/
80a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
81a92bc73bSOlivier Deprez {
82a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
83a92bc73bSOlivier Deprez }
84a92bc73bSOlivier Deprez 
85a92bc73bSOlivier Deprez /*******************************************************************************
860f14d02fSMax Shvetsov  * Static function declaration.
870f14d02fSMax Shvetsov  ******************************************************************************/
880f14d02fSMax Shvetsov static int32_t spmd_init(void);
8923d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
9095f7f6d8SRaghu Krishnamurthy 
9152696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
9252696946SOlivier Deprez 				 bool secure_origin,
9352696946SOlivier Deprez 				 uint64_t x1,
9452696946SOlivier Deprez 				 uint64_t x2,
9552696946SOlivier Deprez 				 uint64_t x3,
9652696946SOlivier Deprez 				 uint64_t x4,
97bb01a673SMarc Bonnici 				 void *cookie,
98bb01a673SMarc Bonnici 				 void *handle,
99*bb9fc8c0SJay Monkman 				 uint64_t flags,
100*bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version);
101bdd2596dSAchin Gupta 
1029944f557SDaniel Boulby /******************************************************************************
1039944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
1049944f557SDaniel Boulby  *****************************************************************************/
1059944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
1069944f557SDaniel Boulby 			     unsigned long long message)
1079944f557SDaniel Boulby {
1089944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1099944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1109944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1119944f557SDaniel Boulby 		 spmd_spmc_id_get());
1129944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1139944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
11476d53ee1SOlivier Deprez 
11576d53ee1SOlivier Deprez 	/* Zero out x4-x7 for the direct request emitted towards the SPMC. */
11676d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
11776d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
11876d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
11976d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1209944f557SDaniel Boulby }
1219944f557SDaniel Boulby 
1229944f557SDaniel Boulby 
123bdd2596dSAchin Gupta /*******************************************************************************
12452696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
12552696946SOlivier Deprez  * SPMC entry.
126bdd2596dSAchin Gupta  ******************************************************************************/
127bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
128bdd2596dSAchin Gupta {
129bdd2596dSAchin Gupta 	uint64_t rc;
130bdd2596dSAchin Gupta 
131bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
132bdd2596dSAchin Gupta 
133bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
134bdd2596dSAchin Gupta 
135bdd2596dSAchin Gupta 	/* Restore the context assigned above */
136033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
13728f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
138678ce223SOlivier Deprez #else
139678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
140033039f8SMax Shvetsov #endif
141bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
142bdd2596dSAchin Gupta 
143033039f8SMax Shvetsov 	/* Enter SPMC */
144bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
145bdd2596dSAchin Gupta 
146bdd2596dSAchin Gupta 	/* Save secure state */
147033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
14828f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
149678ce223SOlivier Deprez #else
150678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
151033039f8SMax Shvetsov #endif
152bdd2596dSAchin Gupta 
153bdd2596dSAchin Gupta 	return rc;
154bdd2596dSAchin Gupta }
155bdd2596dSAchin Gupta 
156bdd2596dSAchin Gupta /*******************************************************************************
15752696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
158bdd2596dSAchin Gupta  * called originally.
159bdd2596dSAchin Gupta  ******************************************************************************/
160bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
161bdd2596dSAchin Gupta {
16252696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
163bdd2596dSAchin Gupta 
16452696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
165bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
166bdd2596dSAchin Gupta 
167bdd2596dSAchin Gupta 	/*
168bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
169bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
170bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
171bdd2596dSAchin Gupta 	 */
172bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
173bdd2596dSAchin Gupta 
174bdd2596dSAchin Gupta 	panic();
175bdd2596dSAchin Gupta }
176bdd2596dSAchin Gupta 
177bdd2596dSAchin Gupta /*******************************************************************************
17852696946SOlivier Deprez  * Jump to the SPM Core for the first time.
179bdd2596dSAchin Gupta  ******************************************************************************/
180bdd2596dSAchin Gupta static int32_t spmd_init(void)
181bdd2596dSAchin Gupta {
18252696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
18352696946SOlivier Deprez 	uint64_t rc;
184bdd2596dSAchin Gupta 
18552696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1869dcf63ddSOlivier Deprez 
187f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
188f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
189bdd2596dSAchin Gupta 
190bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
19152696946SOlivier Deprez 	if (rc != 0ULL) {
1924ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
19352696946SOlivier Deprez 		return 0;
194bdd2596dSAchin Gupta 	}
195bdd2596dSAchin Gupta 
1969dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
1979dcf63ddSOlivier Deprez 
19852696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
199bdd2596dSAchin Gupta 
200890b5088SRaghu Krishnamurthy 	spmd_logical_sp_set_spmc_initialized();
201890b5088SRaghu Krishnamurthy 	rc = spmd_logical_sp_init();
202890b5088SRaghu Krishnamurthy 	if (rc != 0) {
203890b5088SRaghu Krishnamurthy 		WARN("SPMD Logical partitions failed init.\n");
204890b5088SRaghu Krishnamurthy 	}
205890b5088SRaghu Krishnamurthy 
206bdd2596dSAchin Gupta 	return 1;
207bdd2596dSAchin Gupta }
208bdd2596dSAchin Gupta 
209bdd2596dSAchin Gupta /*******************************************************************************
2108cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
2118cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
2128cb99c3fSOlivier Deprez  * itself or a Secure Partition.
2138cb99c3fSOlivier Deprez  ******************************************************************************/
2148cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
2158cb99c3fSOlivier Deprez 					      uint32_t flags,
2168cb99c3fSOlivier Deprez 					      void *handle,
2178cb99c3fSOlivier Deprez 					      void *cookie)
2188cb99c3fSOlivier Deprez {
2198cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2208cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2218cb99c3fSOlivier Deprez 	int64_t rc;
2228cb99c3fSOlivier Deprez 
2238cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2248cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2258cb99c3fSOlivier Deprez 
2268cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2278cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2288cb99c3fSOlivier Deprez 
2298cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2308cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2318cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2322d960a11SMadhukar Pappireddy #else
2332d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_save(NON_SECURE);
23459bdcc58SMadhukar Pappireddy 
23559bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
23659bdcc58SMadhukar Pappireddy 	/*
23759bdcc58SMadhukar Pappireddy 	 * The hint bit denoting absence of SVE live state is effectively false
23859bdcc58SMadhukar Pappireddy 	 * in this scenario where execution was trapped to EL3 due to FIQ.
23959bdcc58SMadhukar Pappireddy 	 */
24059bdcc58SMadhukar Pappireddy 	simd_ctx_save(NON_SECURE, false);
2418f60d99fSRakshit Goyal 	simd_ctx_restore(SECURE);
24259bdcc58SMadhukar Pappireddy #endif
2438cb99c3fSOlivier Deprez #endif
2448cb99c3fSOlivier Deprez 
2458cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2468cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2478cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2488cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2498cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2508cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2518cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2528cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2538cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2548cb99c3fSOlivier Deprez 
2558cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2568cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2578cb99c3fSOlivier Deprez 
2588cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
25959bdcc58SMadhukar Pappireddy 
2608cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
261eac8077aSOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, plat_my_core_pos());
2628cb99c3fSOlivier Deprez 	}
2638cb99c3fSOlivier Deprez 
2648cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2658cb99c3fSOlivier Deprez 
2668cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2678cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2682d960a11SMadhukar Pappireddy #else
2692d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_restore(NON_SECURE);
27059bdcc58SMadhukar Pappireddy 
27159bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
2728f60d99fSRakshit Goyal 	simd_ctx_save(SECURE, false);
27359bdcc58SMadhukar Pappireddy 	simd_ctx_restore(NON_SECURE);
27459bdcc58SMadhukar Pappireddy #endif
2758cb99c3fSOlivier Deprez #endif
2768cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2778cb99c3fSOlivier Deprez 
2788cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2798cb99c3fSOlivier Deprez }
2808cb99c3fSOlivier Deprez 
281bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
282a1e0e871SMadhukar Pappireddy /*******************************************************************************
283a1e0e871SMadhukar Pappireddy  * spmd_group0_interrupt_handler_nwd
284a1e0e871SMadhukar Pappireddy  * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
285a1e0e871SMadhukar Pappireddy  * handling of the interrupt to the platform handler, and return only upon
286a1e0e871SMadhukar Pappireddy  * successfully handling the Group0 interrupt.
287a1e0e871SMadhukar Pappireddy  ******************************************************************************/
288a1e0e871SMadhukar Pappireddy static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
289a1e0e871SMadhukar Pappireddy 						  uint32_t flags,
290a1e0e871SMadhukar Pappireddy 						  void *handle,
291a1e0e871SMadhukar Pappireddy 						  void *cookie)
292a1e0e871SMadhukar Pappireddy {
293a1e0e871SMadhukar Pappireddy 	uint32_t intid;
294a1e0e871SMadhukar Pappireddy 
295a1e0e871SMadhukar Pappireddy 	/* Sanity check the security state when the exception was generated. */
296a1e0e871SMadhukar Pappireddy 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
297a1e0e871SMadhukar Pappireddy 
298a1e0e871SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context. */
299a1e0e871SMadhukar Pappireddy 	assert(handle == cm_get_context(NON_SECURE));
300a1e0e871SMadhukar Pappireddy 
301a1e0e871SMadhukar Pappireddy 	assert(id == INTR_ID_UNAVAILABLE);
302a1e0e871SMadhukar Pappireddy 
303a1e0e871SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
304a1e0e871SMadhukar Pappireddy 
3056c91fc44SMadhukar Pappireddy 	intid = plat_ic_acknowledge_interrupt();
306a1e0e871SMadhukar Pappireddy 
307a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
308a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
309a1e0e871SMadhukar Pappireddy 		panic();
310a1e0e871SMadhukar Pappireddy 	}
311a1e0e871SMadhukar Pappireddy 
3126c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3136c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3146c91fc44SMadhukar Pappireddy 
315a1e0e871SMadhukar Pappireddy 	return 0U;
316a1e0e871SMadhukar Pappireddy }
317bb6d0a17SOlivier Deprez #endif
318a1e0e871SMadhukar Pappireddy 
3196671b3d8SMadhukar Pappireddy /*******************************************************************************
3206671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
3216671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
3226671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
3236671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
3246671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
3256671b3d8SMadhukar Pappireddy  ******************************************************************************/
3266671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
3276671b3d8SMadhukar Pappireddy {
3286671b3d8SMadhukar Pappireddy 	uint32_t intid;
3296671b3d8SMadhukar Pappireddy 
3306671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3316671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3326671b3d8SMadhukar Pappireddy 
3336671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3346671b3d8SMadhukar Pappireddy 
3356c91fc44SMadhukar Pappireddy 	intid = plat_ic_acknowledge_interrupt();
3366671b3d8SMadhukar Pappireddy 
3376671b3d8SMadhukar Pappireddy 	/*
3386671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3396671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3406671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3416671b3d8SMadhukar Pappireddy 	 */
3426671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3436671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3446671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3456671b3d8SMadhukar Pappireddy 		panic();
3466671b3d8SMadhukar Pappireddy 	}
3476671b3d8SMadhukar Pappireddy 
3486c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3496c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3506c91fc44SMadhukar Pappireddy 
3516671b3d8SMadhukar Pappireddy 	/* Return success. */
3526671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3536671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3546671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3556671b3d8SMadhukar Pappireddy }
3566671b3d8SMadhukar Pappireddy 
3570cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3580cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3590cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3600cea2ae0SManish V Badarkhe 				 size_t *align_size)
3610cea2ae0SManish V Badarkhe {
3620cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3630cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3640cea2ae0SManish V Badarkhe 	int rc;
3650cea2ae0SManish V Badarkhe 
3660cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3670cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
3680cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
3690cea2ae0SManish V Badarkhe 
3700cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
3710cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
3720cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
3730cea2ae0SManish V Badarkhe 	}
3740cea2ae0SManish V Badarkhe 
3750cea2ae0SManish V Badarkhe 	/*
3760cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
3770cea2ae0SManish V Badarkhe 	 * size
3780cea2ae0SManish V Badarkhe 	 */
3790cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
3800cea2ae0SManish V Badarkhe 				     base_addr_align,
3810cea2ae0SManish V Badarkhe 				     mapped_size_align,
3820cea2ae0SManish V Badarkhe 				     attr);
3830cea2ae0SManish V Badarkhe 	if (rc == 0) {
3840cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
3850cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
3860cea2ae0SManish V Badarkhe 	}
3870cea2ae0SManish V Badarkhe 
3880cea2ae0SManish V Badarkhe 	return rc;
3890cea2ae0SManish V Badarkhe }
3900cea2ae0SManish V Badarkhe 
3910cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
3920cea2ae0SManish V Badarkhe 			    size_t size)
3930cea2ae0SManish V Badarkhe {
3940cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
3950cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
3960cea2ae0SManish V Badarkhe 	int rc;
3970cea2ae0SManish V Badarkhe 
3980cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
3990cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
4000cea2ae0SManish V Badarkhe 	assert(size != 0UL);
4010cea2ae0SManish V Badarkhe 
4020cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
4030cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
4040cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
4050cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
4060cea2ae0SManish V Badarkhe 	if (rc != 0) {
4070cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
4080cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
4090cea2ae0SManish V Badarkhe 		panic();
4100cea2ae0SManish V Badarkhe 	}
4110cea2ae0SManish V Badarkhe 
4120cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
4130cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
4140cea2ae0SManish V Badarkhe 	if (rc != 0) {
4150cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
4160cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
4170cea2ae0SManish V Badarkhe 		panic();
4180cea2ae0SManish V Badarkhe 	}
4190cea2ae0SManish V Badarkhe 
4200cea2ae0SManish V Badarkhe 	/* Do copy operation */
4210cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
4220cea2ae0SManish V Badarkhe 
4230cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
4240cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
4250cea2ae0SManish V Badarkhe 					root_mapped_size_align);
4260cea2ae0SManish V Badarkhe 	if (rc != 0) {
4270cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4280cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
4290cea2ae0SManish V Badarkhe 		panic();
4300cea2ae0SManish V Badarkhe 	}
4310cea2ae0SManish V Badarkhe 
4320cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4330cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4340cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4350cea2ae0SManish V Badarkhe 	if (rc != 0) {
4360cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4370cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4380cea2ae0SManish V Badarkhe 		panic();
4390cea2ae0SManish V Badarkhe 	}
4400cea2ae0SManish V Badarkhe }
4410cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4420cea2ae0SManish V Badarkhe 
4438cb99c3fSOlivier Deprez /*******************************************************************************
44452696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4450f14d02fSMax Shvetsov  ******************************************************************************/
44623d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4470f14d02fSMax Shvetsov {
448f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
449f2dcf418SOlivier Deprez 	unsigned int core_id;
4508cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
45152696946SOlivier Deprez 	int rc;
4520cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4530f14d02fSMax Shvetsov 
45452696946SOlivier Deprez 	/* Load the SPM Core manifest */
45523d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4560f14d02fSMax Shvetsov 	if (rc != 0) {
45752696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
45852696946SOlivier Deprez 		return rc;
4590f14d02fSMax Shvetsov 	}
4600f14d02fSMax Shvetsov 
4610f14d02fSMax Shvetsov 	/*
46252696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
46352696946SOlivier Deprez 	 * Dispatcher version.
4640f14d02fSMax Shvetsov 	 */
465662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
466662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
467662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4680f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
46952696946SOlivier Deprez 		return -EINVAL;
4700f14d02fSMax Shvetsov 	}
4710f14d02fSMax Shvetsov 
472662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
4730f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
4740f14d02fSMax Shvetsov 
47552696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
476033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
4770f14d02fSMax Shvetsov 
478ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
47952696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
48052696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
48152696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
48252696946SOlivier Deprez 		return -EINVAL;
483ac03ac5eSMax Shvetsov 	}
484ac03ac5eSMax Shvetsov 
48552696946SOlivier Deprez 	/* Validate the SPM Core execution state */
4860f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
4870f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
48823d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
4890f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
49052696946SOlivier Deprez 		return -EINVAL;
4910f14d02fSMax Shvetsov 	}
4920f14d02fSMax Shvetsov 
49323d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
49423d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
4950f14d02fSMax Shvetsov 
496033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
497033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
498033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
499033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
50052696946SOlivier Deprez 		return -EINVAL;
5010f14d02fSMax Shvetsov 	}
5020f14d02fSMax Shvetsov 
5030f14d02fSMax Shvetsov 	/*
5040f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
5050f14d02fSMax Shvetsov 	 * is required for SPM
5060f14d02fSMax Shvetsov 	 */
507623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
50852696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
50952696946SOlivier Deprez 		return -EINVAL;
5100f14d02fSMax Shvetsov 	}
511033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
5120f14d02fSMax Shvetsov 
5130f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
5140f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
51552696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
5160f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
5170f14d02fSMax Shvetsov 	}
5180f14d02fSMax Shvetsov 
5190f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
5200f14d02fSMax Shvetsov 
5210f14d02fSMax Shvetsov 	/*
52252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
52352696946SOlivier Deprez 	 * manifest.
5240f14d02fSMax Shvetsov 	 */
5250f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
5260f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
5270f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
5280f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
5290f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5300f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5310f14d02fSMax Shvetsov 	} else {
532033039f8SMax Shvetsov 
533033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
534033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
535033039f8SMax Shvetsov #else
536033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
537033039f8SMax Shvetsov #endif
538033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5390f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5400f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5410f14d02fSMax Shvetsov 	}
5420f14d02fSMax Shvetsov 
5430cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5440cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5450cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5460cea2ae0SManish V Badarkhe 
5470cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5480cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5490cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5500cea2ae0SManish V Badarkhe 		return -EINVAL;
5510cea2ae0SManish V Badarkhe 	}
5520cea2ae0SManish V Badarkhe 
5530cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5540cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5550cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5560cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5570cea2ae0SManish V Badarkhe 
5580cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5590cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5600cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5610cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5620cea2ae0SManish V Badarkhe 
563f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
564f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
565f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
5660f14d02fSMax Shvetsov 
567f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
568f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
569f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
5700f14d02fSMax Shvetsov 
571f2dcf418SOlivier Deprez 		/*
572f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
573f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
574f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
575f2dcf418SOlivier Deprez 		 */
576f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
577f2dcf418SOlivier Deprez 	}
5780f14d02fSMax Shvetsov 
579a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
580a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
581a334c4e6SOlivier Deprez 
5820f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5830f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
5840f14d02fSMax Shvetsov 
585f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
586f2dcf418SOlivier Deprez 
5878cb99c3fSOlivier Deprez 	/*
5888cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
5898cb99c3fSOlivier Deprez 	 * while the NWd is running.
5908cb99c3fSOlivier Deprez 	 */
5918cb99c3fSOlivier Deprez 	flags = 0;
5928cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
5938cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
5948cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
5958cb99c3fSOlivier Deprez 					     flags);
5968cb99c3fSOlivier Deprez 	if (rc != 0) {
5978cb99c3fSOlivier Deprez 		panic();
5988cb99c3fSOlivier Deprez 	}
5998cb99c3fSOlivier Deprez 
600a1e0e871SMadhukar Pappireddy 	/*
601bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
602bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
603bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
604bb6d0a17SOlivier Deprez 	 *
605bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
606bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
607bb6d0a17SOlivier Deprez 	 *
608bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
609bb6d0a17SOlivier Deprez 	 *
610bb6d0a17SOlivier Deprez 	 */
611bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
612bb6d0a17SOlivier Deprez 	/*
613fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
614fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
615fca5f0ebSMadhukar Pappireddy 	 * running.
616a1e0e871SMadhukar Pappireddy 	 */
617fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
618a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
619a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
620a1e0e871SMadhukar Pappireddy 						     flags);
621a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
622a1e0e871SMadhukar Pappireddy 			panic();
623a1e0e871SMadhukar Pappireddy 		}
624fca5f0ebSMadhukar Pappireddy 	}
625bb6d0a17SOlivier Deprez #endif
626bb6d0a17SOlivier Deprez 
6270f14d02fSMax Shvetsov 	return 0;
6280f14d02fSMax Shvetsov }
6290f14d02fSMax Shvetsov 
6300f14d02fSMax Shvetsov /*******************************************************************************
63152696946SOlivier Deprez  * Initialize context of SPM Core.
632bdd2596dSAchin Gupta  ******************************************************************************/
6330f14d02fSMax Shvetsov int spmd_setup(void)
634bdd2596dSAchin Gupta {
635bdd2596dSAchin Gupta 	int rc;
6366da76075SMarc Bonnici 	void *spmc_manifest;
6376da76075SMarc Bonnici 
6386da76075SMarc Bonnici 	/*
6396da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6406da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6416da76075SMarc Bonnici 	 */
6426da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6436da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6446da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6456da76075SMarc Bonnici 
6466da76075SMarc Bonnici 		rc = spmc_setup();
6476da76075SMarc Bonnici 		if (rc != 0) {
6480d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6496da76075SMarc Bonnici 		}
6500d33649eSOlivier Deprez 		return 0;
6516da76075SMarc Bonnici 	}
652bdd2596dSAchin Gupta 
653bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
65452696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
65552696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6560d33649eSOlivier Deprez 		return 0;
657bdd2596dSAchin Gupta 	}
658bdd2596dSAchin Gupta 
659bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
66052696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
661bdd2596dSAchin Gupta 
662bdd2596dSAchin Gupta 	/*
663bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
66452696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
665bdd2596dSAchin Gupta 	 */
66623d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
66723d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
6680d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
6690d33649eSOlivier Deprez 		return 0;
670bdd2596dSAchin Gupta 	}
671bdd2596dSAchin Gupta 
6720f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
67323d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
6740f14d02fSMax Shvetsov 	if (rc != 0) {
67552696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
676bdd2596dSAchin Gupta 	}
677bdd2596dSAchin Gupta 
6780d33649eSOlivier Deprez 	return 0;
6790f14d02fSMax Shvetsov }
6800f14d02fSMax Shvetsov 
6810f14d02fSMax Shvetsov /*******************************************************************************
682bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
6830f14d02fSMax Shvetsov  ******************************************************************************/
684bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
68552696946SOlivier Deprez 			       bool secure_origin,
68652696946SOlivier Deprez 			       uint64_t x1,
68752696946SOlivier Deprez 			       uint64_t x2,
68852696946SOlivier Deprez 			       uint64_t x3,
68952696946SOlivier Deprez 			       uint64_t x4,
690c925867eSOlivier Deprez 			       void *handle,
691*bb9fc8c0SJay Monkman 			       uint64_t flags,
692*bb9fc8c0SJay Monkman 			       uint32_t secure_ffa_version)
6930f14d02fSMax Shvetsov {
694c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
695c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
696*bb9fc8c0SJay Monkman 	uint32_t version_in = (secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
697*bb9fc8c0SJay Monkman 	uint32_t version_out = (!secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
698107e3cc0SOlivier Deprez 	void *ctx_out;
69993ff138bSOlivier Deprez 
700c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
701c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
702c925867eSOlivier Deprez 		/*
703c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
704c925867eSOlivier Deprez 		 * if it was set by the caller.
705c925867eSOlivier Deprez 		 */
706c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
707c925867eSOlivier Deprez 	}
708c925867eSOlivier Deprez #endif
709c925867eSOlivier Deprez 
7100f14d02fSMax Shvetsov 	/* Save incoming security state */
711033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
71293ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
713678ce223SOlivier Deprez #else
714678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
71559bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
71659bdcc58SMadhukar Pappireddy 	/* Forward the hint bit denoting the absence of SVE live state. */
71759bdcc58SMadhukar Pappireddy 	simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
71859bdcc58SMadhukar Pappireddy #endif
719033039f8SMax Shvetsov #endif
7200f14d02fSMax Shvetsov 
7210f14d02fSMax Shvetsov 	/* Restore outgoing security state */
722033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
72393ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
724678ce223SOlivier Deprez #else
725678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
72659bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
72759bdcc58SMadhukar Pappireddy 	simd_ctx_restore(secure_state_out);
72859bdcc58SMadhukar Pappireddy #endif
729033039f8SMax Shvetsov #endif
73093ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
7310f14d02fSMax Shvetsov 
732107e3cc0SOlivier Deprez 	ctx_out = cm_get_context(secure_state_out);
733a0a7f158SAndrei Homescu 	if (smc_fid == FFA_NORMAL_WORLD_RESUME) {
734a0a7f158SAndrei Homescu 		SMC_RET0(ctx_out);
735a0a7f158SAndrei Homescu 	}
736a0a7f158SAndrei Homescu 
737*bb9fc8c0SJay Monkman 	if ((GET_SMC_CC(smc_fid) == SMC_64) && (version_out >= MAKE_FFA_VERSION(U(1), U(2)))) {
738*bb9fc8c0SJay Monkman 		if (version_in < MAKE_FFA_VERSION(U(1), U(2))) {
739*bb9fc8c0SJay Monkman 			/* FFA version mismatch, with dest >= 1.2 - set outgoing x8-x17 to zero */
740*bb9fc8c0SJay Monkman 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
741*bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X5),
742*bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X6),
743*bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X7),
744*bb9fc8c0SJay Monkman 				  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
745*bb9fc8c0SJay Monkman 		} else {
746*bb9fc8c0SJay Monkman 			/* Both FFA versions >= 1.2 - pass incoming x8-x17 to dest */
747107e3cc0SOlivier Deprez 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
748eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X5),
749eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X6),
750eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X7),
751eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X8),
752eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X9),
753eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X10),
754eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X11),
755eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X12),
756eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X13),
757eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X14),
758eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X15),
759eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X16),
760eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X17)
761eaaf517cSRaghu Krishnamurthy 				);
762*bb9fc8c0SJay Monkman 		}
763*bb9fc8c0SJay Monkman 	} else {
764*bb9fc8c0SJay Monkman 		/* 32 bit call or dest has FFA version < 1.2 or unknown */
765107e3cc0SOlivier Deprez 		SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
7660f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X5),
7670f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X6),
7680f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X7));
769*bb9fc8c0SJay Monkman 	}
7700f14d02fSMax Shvetsov }
7710f14d02fSMax Shvetsov 
7720f14d02fSMax Shvetsov /*******************************************************************************
773bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
774bb01a673SMarc Bonnici  ******************************************************************************/
775bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
776bb01a673SMarc Bonnici 				 bool secure_origin,
777bb01a673SMarc Bonnici 				 uint64_t x1,
778bb01a673SMarc Bonnici 				 uint64_t x2,
779bb01a673SMarc Bonnici 				 uint64_t x3,
780bb01a673SMarc Bonnici 				 uint64_t x4,
781bb01a673SMarc Bonnici 				 void *cookie,
782bb01a673SMarc Bonnici 				 void *handle,
783*bb9fc8c0SJay Monkman 				 uint64_t flags,
784*bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version)
785bb01a673SMarc Bonnici {
786bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
787bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
788bb01a673SMarc Bonnici 					cookie, handle, flags);
789bb01a673SMarc Bonnici 	}
790c925867eSOlivier Deprez 
791bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
792*bb9fc8c0SJay Monkman 				     handle, flags, secure_ffa_version);
793bb01a673SMarc Bonnici 
794bb01a673SMarc Bonnici }
795bb01a673SMarc Bonnici 
796bb01a673SMarc Bonnici /*******************************************************************************
797662af36dSJ-Alves  * Return FFA_ERROR with specified error code
7980f14d02fSMax Shvetsov  ******************************************************************************/
79995f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
8000f14d02fSMax Shvetsov {
801e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
802e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
803662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
804662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
805bdd2596dSAchin Gupta }
806bdd2596dSAchin Gupta 
807f0d743dbSOlivier Deprez /*******************************************************************************
808f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
809f0d743dbSOlivier Deprez  ******************************************************************************/
810f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
811f0d743dbSOlivier Deprez {
812f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
813f0d743dbSOlivier Deprez 
814f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
815f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
816f0d743dbSOlivier Deprez }
817f0d743dbSOlivier Deprez 
818c2901419SOlivier Deprez /******************************************************************************
819c2901419SOlivier Deprez  * spmd_is_spmc_message
820c2901419SOlivier Deprez  *****************************************************************************/
821c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
822c2901419SOlivier Deprez {
823bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
824bb01a673SMarc Bonnici 		return false;
825bb01a673SMarc Bonnici 	}
826bb01a673SMarc Bonnici 
827c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
828c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
829c2901419SOlivier Deprez }
830c2901419SOlivier Deprez 
831bdd2596dSAchin Gupta /*******************************************************************************
832bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
833bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
834bb01a673SMarc Bonnici  ******************************************************************************/
835bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
836bb01a673SMarc Bonnici 			      uint64_t x1,
837bb01a673SMarc Bonnici 			      uint64_t x2,
838bb01a673SMarc Bonnici 			      uint64_t x3,
839bb01a673SMarc Bonnici 			      uint64_t x4,
840bb01a673SMarc Bonnici 			      void *cookie,
841bb01a673SMarc Bonnici 			      void *handle,
842bb01a673SMarc Bonnici 			      uint64_t flags)
843bb01a673SMarc Bonnici {
844bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
845bb01a673SMarc Bonnici 		/*
846bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
847bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
848bb01a673SMarc Bonnici 		 */
849bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
850bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
851bb01a673SMarc Bonnici 						is_caller_secure(flags),
852bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
853bb01a673SMarc Bonnici 						handle, flags);
854bb01a673SMarc Bonnici 		}
855bb01a673SMarc Bonnici 	}
856bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
857*bb9fc8c0SJay Monkman 				handle, flags, spmc_nwd_ffa_version);
858*bb9fc8c0SJay Monkman }
859*bb9fc8c0SJay Monkman 
860*bb9fc8c0SJay Monkman static uint32_t get_common_ffa_version(uint32_t secure_ffa_version)
861*bb9fc8c0SJay Monkman {
862*bb9fc8c0SJay Monkman 	if (secure_ffa_version <= nonsecure_ffa_version) {
863*bb9fc8c0SJay Monkman 		return secure_ffa_version;
864*bb9fc8c0SJay Monkman 	} else {
865*bb9fc8c0SJay Monkman 		return nonsecure_ffa_version;
866*bb9fc8c0SJay Monkman 	}
867bb01a673SMarc Bonnici }
868bb01a673SMarc Bonnici 
869bb01a673SMarc Bonnici /*******************************************************************************
870662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
871bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
872bdd2596dSAchin Gupta  ******************************************************************************/
87352696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
87452696946SOlivier Deprez 			  uint64_t x1,
87552696946SOlivier Deprez 			  uint64_t x2,
87652696946SOlivier Deprez 			  uint64_t x3,
87752696946SOlivier Deprez 			  uint64_t x4,
87852696946SOlivier Deprez 			  void *cookie,
87952696946SOlivier Deprez 			  void *handle,
880*bb9fc8c0SJay Monkman 			  uint64_t flags,
881*bb9fc8c0SJay Monkman 			  uint32_t secure_ffa_version)
882bdd2596dSAchin Gupta {
88352696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
88493ff138bSOlivier Deprez 	bool secure_origin;
8856873088cSJ-Alves 	int ret;
8864388f28fSJ-Alves 	uint32_t input_version;
887bdd2596dSAchin Gupta 
888bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
88993ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
890bdd2596dSAchin Gupta 
8914ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
8924ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
893eac8077aSOlivier Deprez 		    plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
894cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
895bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
896bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
897bdd2596dSAchin Gupta 
8980b850e9eSRaghu Krishnamurthy 	/*
8990b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
9000b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
9010b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
9020b850e9eSRaghu Krishnamurthy 	 */
9030b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
9040b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
9050b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
9060b850e9eSRaghu Krishnamurthy 	}
9070b850e9eSRaghu Krishnamurthy 
908*bb9fc8c0SJay Monkman 	if ((!secure_origin) && (smc_fid != FFA_VERSION)) {
909*bb9fc8c0SJay Monkman 		/*
910*bb9fc8c0SJay Monkman 		 * Once the caller invokes any FF-A ABI other than FFA_VERSION,
911*bb9fc8c0SJay Monkman 		 * the version negotiation phase is complete.
912*bb9fc8c0SJay Monkman 		 */
913*bb9fc8c0SJay Monkman 		nonsecure_version_negotiated = true;
914*bb9fc8c0SJay Monkman 	}
915*bb9fc8c0SJay Monkman 
916bdd2596dSAchin Gupta 	switch (smc_fid) {
917662af36dSJ-Alves 	case FFA_ERROR:
918bdd2596dSAchin Gupta 		/*
919bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
92052696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
921bdd2596dSAchin Gupta 		 * unsuccessfully.
922bdd2596dSAchin Gupta 		 */
9239dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
924bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
9250f14d02fSMax Shvetsov 		}
926bdd2596dSAchin Gupta 
92766bdfd6eSRaghu Krishnamurthy 		/*
9288723eaf2SMadhukar Pappireddy 		 * Perform a synchronous exit:
9298723eaf2SMadhukar Pappireddy 		 * 1. If there was an SPMD logical partition direct request on-going,
93066bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
93166bdfd6eSRaghu Krishnamurthy 		 * consumed.
9328723eaf2SMadhukar Pappireddy 		 * 2. SPMC sent FFA_ERROR in response to a power management
9338723eaf2SMadhukar Pappireddy 		 * operation sent through direct request.
93466bdfd6eSRaghu Krishnamurthy 		 */
9358723eaf2SMadhukar Pappireddy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx) ||
9368723eaf2SMadhukar Pappireddy 		    ctx->psci_operation_ongoing) {
93766bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
93866bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
93966bdfd6eSRaghu Krishnamurthy 		}
94066bdfd6eSRaghu Krishnamurthy 
94193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
942bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
943*bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
944bdd2596dSAchin Gupta 		break; /* not reached */
945bdd2596dSAchin Gupta 
946662af36dSJ-Alves 	case FFA_VERSION:
9474388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
948bdd2596dSAchin Gupta 		/*
9494388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
9504388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
9514388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
952*bb9fc8c0SJay Monkman 		 * forward to the EL3 SPMC if enabled, otherwise send a
953*bb9fc8c0SJay Monkman 		 * framework message to the SPMC at the lower EL to
954*bb9fc8c0SJay Monkman 		 * negotiate a version that is compatible between the
955*bb9fc8c0SJay Monkman 		 * normal world and the SPMC.
9564388f28fSJ-Alves 		 * Sanity check to "input_version".
957bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
958bb01a673SMarc Bonnici 		 * this is not used.
959bdd2596dSAchin Gupta 		 */
9604388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
961bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
9624388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
9634388f28fSJ-Alves 		} else if (!secure_origin) {
964*bb9fc8c0SJay Monkman 			if (!nonsecure_version_negotiated) {
965*bb9fc8c0SJay Monkman 				/*
966*bb9fc8c0SJay Monkman 				 * Once an FF-A version has been negotiated
967*bb9fc8c0SJay Monkman 				 * between a caller and a callee, the version
968*bb9fc8c0SJay Monkman 				 * may not be changed for the lifetime of
969*bb9fc8c0SJay Monkman 				 * the calling component.
970*bb9fc8c0SJay Monkman 				 */
971*bb9fc8c0SJay Monkman 				nonsecure_ffa_version = input_version;
972*bb9fc8c0SJay Monkman 			}
973*bb9fc8c0SJay Monkman 
9749576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
9759576fa93SMarc Bonnici 				/*
9769576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
9779576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
9789576fa93SMarc Bonnici 				 * a direct request.
9799576fa93SMarc Bonnici 				 */
980*bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version =
981*bb9fc8c0SJay Monkman 					MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
982*bb9fc8c0SJay Monkman 				return spmc_smc_handler(smc_fid, secure_origin,
9839576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
9849576fa93SMarc Bonnici 							handle, flags);
9859576fa93SMarc Bonnici 			}
9869576fa93SMarc Bonnici 
9879944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
9889944f557SDaniel Boulby 			uint64_t rc;
9899944f557SDaniel Boulby 
9909944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
9919944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
992e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
993e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
994*bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
9959944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
9969944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
9979944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
9989944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
9999944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10009944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
10019944f557SDaniel Boulby 				break;
10029944f557SDaniel Boulby 			}
10039944f557SDaniel Boulby 			/* Save non-secure system registers context */
10049944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
10059944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
10062d960a11SMadhukar Pappireddy #else
10072d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
10089944f557SDaniel Boulby #endif
10099944f557SDaniel Boulby 
10109944f557SDaniel Boulby 			/*
10119944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
10129944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
10139944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
10149944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
10159944f557SDaniel Boulby 			 */
10169944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
10179944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
10189944f557SDaniel Boulby 						input_version);
10199944f557SDaniel Boulby 
102076d53ee1SOlivier Deprez 			/*
102176d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
102276d53ee1SOlivier Deprez 			 * from the SPMC.
102376d53ee1SOlivier Deprez 			 */
102476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
102576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
102676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
102776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
102876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
102976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
103076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
103176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
103276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
103376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
103476d53ee1SOlivier Deprez 
10359944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
10369944f557SDaniel Boulby 
10379944f557SDaniel Boulby 			if ((rc != 0ULL) ||
10389944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
10399944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
10409944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
104159bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
10429944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
10439944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
10449944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
10459944f557SDaniel Boulby 			} else {
10469944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
1047*bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10489944f557SDaniel Boulby 			}
10499944f557SDaniel Boulby 
10509944f557SDaniel Boulby 			/*
105176d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
105276d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
105376d53ee1SOlivier Deprez 			 */
105476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
105576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
105676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
105776d53ee1SOlivier Deprez 
105876d53ee1SOlivier Deprez 			/*
10599944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
10609944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
10619944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
10629944f557SDaniel Boulby 			 */
10639944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
10649944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1065bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
1066*bb9fc8c0SJay Monkman 						flags, spmc_nwd_ffa_version);
10674388f28fSJ-Alves 		} else {
1068e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1069e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
10704388f28fSJ-Alves 		}
10714388f28fSJ-Alves 
1072e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1073e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1074e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1075bdd2596dSAchin Gupta 		break; /* not reached */
1076bdd2596dSAchin Gupta 
1077662af36dSJ-Alves 	case FFA_FEATURES:
1078bdd2596dSAchin Gupta 		/*
1079bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
108052696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1081bdd2596dSAchin Gupta 		 */
1082bdd2596dSAchin Gupta 
108352696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
108493ff138bSOlivier Deprez 		if (!secure_origin) {
108593ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1086bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1087*bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
108852696946SOlivier Deprez 		}
108952696946SOlivier Deprez 
1090bdd2596dSAchin Gupta 		/*
1091bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1092662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1093bdd2596dSAchin Gupta 		 * nop.
1094bdd2596dSAchin Gupta 		 */
1095662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1096bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1097bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1098bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
10990f14d02fSMax Shvetsov 
1100bdd2596dSAchin Gupta 		break; /* not reached */
1101bdd2596dSAchin Gupta 
1102662af36dSJ-Alves 	case FFA_ID_GET:
1103ac03ac5eSMax Shvetsov 		/*
1104662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1105ac03ac5eSMax Shvetsov 		 */
1106ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1107662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1108662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1109662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1110662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1111662af36dSJ-Alves 				 FFA_PARAM_MBZ);
111252696946SOlivier Deprez 		}
111352696946SOlivier Deprez 
1114662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1115662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1116662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1117662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1118662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1119ac03ac5eSMax Shvetsov 
1120ac03ac5eSMax Shvetsov 		break; /* not reached */
1121ac03ac5eSMax Shvetsov 
1122cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1123cdb49d47SOlivier Deprez 		if (secure_origin) {
1124cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1125cdb49d47SOlivier Deprez 
1126cdb49d47SOlivier Deprez 			if (ret < 0) {
1127cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1128cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1129cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1130cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1131cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1132cdb49d47SOlivier Deprez 			} else {
1133cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1134cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1135cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1136cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1137cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1138cdb49d47SOlivier Deprez 			}
1139cdb49d47SOlivier Deprez 		}
1140cdb49d47SOlivier Deprez 
1141cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1142cdb49d47SOlivier Deprez 		break; /* Not reached */
1143cdb49d47SOlivier Deprez 
114470c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
114570c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
114670c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
114770c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
114870c121a2SDaniel Boulby 		}
114970c121a2SDaniel Boulby 		/*
115070c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
115170c121a2SDaniel Boulby 		 * instance where this function is invoked
115270c121a2SDaniel Boulby 		 */
115370c121a2SDaniel Boulby 		if (!secure_origin) {
115470c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
115570c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
115670c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
115770c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
115870c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
115970c121a2SDaniel Boulby 		}
116070c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
116170c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
116270c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
116370c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
116470c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
116570c121a2SDaniel Boulby 
116670c121a2SDaniel Boulby 		break; /* not reached */
116770c121a2SDaniel Boulby 
1168*bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1169*bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1170*bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1171*bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1172*bb9fc8c0SJay Monkman 		}
1173*bb9fc8c0SJay Monkman 		/* fallthrough */
1174f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
11755519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
117666bdfd6eSRaghu Krishnamurthy 		/*
117766bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
117866bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
117966bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
118066bdfd6eSRaghu Krishnamurthy 		 */
118166bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
118266bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
118366bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
118466bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
118566bdfd6eSRaghu Krishnamurthy 						     );
118666bdfd6eSRaghu Krishnamurthy 		}
118766bdfd6eSRaghu Krishnamurthy 
118866bdfd6eSRaghu Krishnamurthy 		/*
118966bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
119066bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
119166bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
119266bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
119366bdfd6eSRaghu Krishnamurthy 		 * error codes.
119466bdfd6eSRaghu Krishnamurthy 		 */
119566bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
119666bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
119766bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
119866bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
119966bdfd6eSRaghu Krishnamurthy 		}
120066bdfd6eSRaghu Krishnamurthy 
12015519f07cSShruti 		if (!secure_origin) {
12025519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
12035519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
12045519f07cSShruti 				return spmd_ffa_error_return(handle,
12055519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
12065519f07cSShruti 			}
12075519f07cSShruti 		}
1208f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1209cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
12106c378c2fSKathleen Capella 						FFA_ERROR_DENIED);
1211cc6047b3SKathleen Capella 		} else {
1212cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1213cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1214cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1215*bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1216cc6047b3SKathleen Capella 		}
1217cc6047b3SKathleen Capella 		break; /* Not reached */
1218cc6047b3SKathleen Capella 
1219*bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1220*bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1221*bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1222*bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1223*bb9fc8c0SJay Monkman 		}
1224*bb9fc8c0SJay Monkman 		/* fallthrough */
1225f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
122666bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
122766bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
122866bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
12298cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1230f0d743dbSOlivier Deprez 		} else {
1231f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1232f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1233bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1234*bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1235f0d743dbSOlivier Deprez 		}
1236f0d743dbSOlivier Deprez 		break; /* Not reached */
1237662af36dSJ-Alves 	case FFA_RX_RELEASE:
1238662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1239662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1240662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1241545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1242fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1243fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1244fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1245fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1246fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1247fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1248fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1249fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1250fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1251c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1252d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
1253fc3f4800SJ-Alves #endif
1254662af36dSJ-Alves 	case FFA_MSG_RUN:
1255c2eba07cSFederico Recanati 		/*
1256c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1257c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1258c2eba07cSFederico Recanati 		 */
125993ff138bSOlivier Deprez 		if (secure_origin) {
1260662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1261662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1262bdd2596dSAchin Gupta 		}
1263bdd2596dSAchin Gupta 
1264e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1265e138400dSBoyan Karatotev 		/* fallthrough */
1266662af36dSJ-Alves 	case FFA_MSG_SEND:
1267662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1268662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1269662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1270662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1271662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1272662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1273662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1274662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1275662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1276662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1277662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1278642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1279642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1280662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1281662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1282bdd2596dSAchin Gupta 		/*
128366bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
128466bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1285bdd2596dSAchin Gupta 		 */
128666bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
128766bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
128866bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
128966bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
129066bdfd6eSRaghu Krishnamurthy 		}
1291bdd2596dSAchin Gupta 
129293ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1293bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1294*bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1295bdd2596dSAchin Gupta 		break; /* not reached */
1296bdd2596dSAchin Gupta 
1297662af36dSJ-Alves 	case FFA_MSG_WAIT:
1298bdd2596dSAchin Gupta 		/*
1299bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1300bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
130152696946SOlivier Deprez 		 * SPM Core initialised successfully.
1302bdd2596dSAchin Gupta 		 */
13039dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
13048cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1305bdd2596dSAchin Gupta 		}
1306bdd2596dSAchin Gupta 
1307e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1308e138400dSBoyan Karatotev 		/* fallthrough */
1309386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1310662af36dSJ-Alves 	case FFA_MSG_YIELD:
1311bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
131293ff138bSOlivier Deprez 		if (!secure_origin) {
1313662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1314662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1315bdd2596dSAchin Gupta 		}
1316bdd2596dSAchin Gupta 
131766bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
131866bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
131966bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
132066bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
132166bdfd6eSRaghu Krishnamurthy 		}
132266bdfd6eSRaghu Krishnamurthy 
132393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1324bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1325*bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1326bdd2596dSAchin Gupta 		break; /* not reached */
1327bdd2596dSAchin Gupta 
13288cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
13298cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
13308cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
13318cb99c3fSOlivier Deprez 		} else {
13328cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
13338cb99c3fSOlivier Deprez 		}
13348cb99c3fSOlivier Deprez 		break; /* Not reached */
1335eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1336eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1337eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
133895f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
133995f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1340eaaf517cSRaghu Krishnamurthy 		}
13418cb99c3fSOlivier Deprez 
1342eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1343eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1344eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1345eaaf517cSRaghu Krishnamurthy 		}
1346eaaf517cSRaghu Krishnamurthy 
1347eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1348eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1349*bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1350eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1351eaaf517cSRaghu Krishnamurthy #endif
1352638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1353638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1354638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1355638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1356638a6f8eSShruti Gupta 		break; /* not reached */
1357638a6f8eSShruti Gupta 
13586671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13596671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13606671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
13616671b3d8SMadhukar Pappireddy 		} else {
13626c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
13636671b3d8SMadhukar Pappireddy 		}
1364bdd2596dSAchin Gupta 	default:
1365bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1366662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1367bdd2596dSAchin Gupta 	}
1368bdd2596dSAchin Gupta }
1369