xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 5b10f25a1ff3fcbbe431eebb36722ec1c5d04797)
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 /*******************************************************************************
49bb9fc8c0SJay Monkman  * FFA version used by nonsecure endpoint.
50bb9fc8c0SJay Monkman  ******************************************************************************/
51bb9fc8c0SJay Monkman static uint32_t nonsecure_ffa_version;
52bb9fc8c0SJay Monkman 
53bb9fc8c0SJay Monkman /*******************************************************************************
54bb9fc8c0SJay Monkman  * Whether the normal world finished negotiating its version.
55bb9fc8c0SJay Monkman  ******************************************************************************/
56bb9fc8c0SJay Monkman static bool nonsecure_version_negotiated;
57bb9fc8c0SJay Monkman 
58bb9fc8c0SJay Monkman /*******************************************************************************
59bb9fc8c0SJay Monkman  * FFA version used by SPMC, as seen by the normal world.
60bb9fc8c0SJay Monkman  ******************************************************************************/
61bb9fc8c0SJay Monkman static uint32_t spmc_nwd_ffa_version;
62bb9fc8c0SJay Monkman 
63bb9fc8c0SJay 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,
99bb9fc8c0SJay Monkman 				 uint64_t flags,
100bb9fc8c0SJay 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 {
293*5b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
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 
305*5b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
306*5b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
307*5b10f25aSMadhukar Pappireddy 
308*5b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
309*5b10f25aSMadhukar Pappireddy 		return 0U;
310*5b10f25aSMadhukar Pappireddy 	}
311a1e0e871SMadhukar Pappireddy 
312a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
313a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
314a1e0e871SMadhukar Pappireddy 		panic();
315a1e0e871SMadhukar Pappireddy 	}
316a1e0e871SMadhukar Pappireddy 
3176c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3186c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3196c91fc44SMadhukar Pappireddy 
320a1e0e871SMadhukar Pappireddy 	return 0U;
321a1e0e871SMadhukar Pappireddy }
322bb6d0a17SOlivier Deprez #endif
323a1e0e871SMadhukar Pappireddy 
3246671b3d8SMadhukar Pappireddy /*******************************************************************************
3256671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
3266671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
3276671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
3286671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
3296671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
3306671b3d8SMadhukar Pappireddy  ******************************************************************************/
3316671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
3326671b3d8SMadhukar Pappireddy {
333*5b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
3346671b3d8SMadhukar Pappireddy 
3356671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3366671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3376671b3d8SMadhukar Pappireddy 
3386671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3396671b3d8SMadhukar Pappireddy 
340*5b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
341*5b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
342*5b10f25aSMadhukar Pappireddy 
343*5b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
344*5b10f25aSMadhukar Pappireddy 		return 0U;
345*5b10f25aSMadhukar Pappireddy 	}
3466671b3d8SMadhukar Pappireddy 
3476671b3d8SMadhukar Pappireddy 	/*
3486671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3496671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3506671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3516671b3d8SMadhukar Pappireddy 	 */
3526671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3536671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3546671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3556671b3d8SMadhukar Pappireddy 		panic();
3566671b3d8SMadhukar Pappireddy 	}
3576671b3d8SMadhukar Pappireddy 
3586c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3596c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3606c91fc44SMadhukar Pappireddy 
3616671b3d8SMadhukar Pappireddy 	/* Return success. */
3626671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3636671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3646671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3656671b3d8SMadhukar Pappireddy }
3666671b3d8SMadhukar Pappireddy 
3670cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3680cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3690cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3700cea2ae0SManish V Badarkhe 				 size_t *align_size)
3710cea2ae0SManish V Badarkhe {
3720cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3730cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3740cea2ae0SManish V Badarkhe 	int rc;
3750cea2ae0SManish V Badarkhe 
3760cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3770cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
3780cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
3790cea2ae0SManish V Badarkhe 
3800cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
3810cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
3820cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
3830cea2ae0SManish V Badarkhe 	}
3840cea2ae0SManish V Badarkhe 
3850cea2ae0SManish V Badarkhe 	/*
3860cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
3870cea2ae0SManish V Badarkhe 	 * size
3880cea2ae0SManish V Badarkhe 	 */
3890cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
3900cea2ae0SManish V Badarkhe 				     base_addr_align,
3910cea2ae0SManish V Badarkhe 				     mapped_size_align,
3920cea2ae0SManish V Badarkhe 				     attr);
3930cea2ae0SManish V Badarkhe 	if (rc == 0) {
3940cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
3950cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
3960cea2ae0SManish V Badarkhe 	}
3970cea2ae0SManish V Badarkhe 
3980cea2ae0SManish V Badarkhe 	return rc;
3990cea2ae0SManish V Badarkhe }
4000cea2ae0SManish V Badarkhe 
4010cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
4020cea2ae0SManish V Badarkhe 			    size_t size)
4030cea2ae0SManish V Badarkhe {
4040cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
4050cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
4060cea2ae0SManish V Badarkhe 	int rc;
4070cea2ae0SManish V Badarkhe 
4080cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
4090cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
4100cea2ae0SManish V Badarkhe 	assert(size != 0UL);
4110cea2ae0SManish V Badarkhe 
4120cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
4130cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
4140cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
4150cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
4160cea2ae0SManish V Badarkhe 	if (rc != 0) {
4170cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
4180cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
4190cea2ae0SManish V Badarkhe 		panic();
4200cea2ae0SManish V Badarkhe 	}
4210cea2ae0SManish V Badarkhe 
4220cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
4230cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
4240cea2ae0SManish V Badarkhe 	if (rc != 0) {
4250cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
4260cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
4270cea2ae0SManish V Badarkhe 		panic();
4280cea2ae0SManish V Badarkhe 	}
4290cea2ae0SManish V Badarkhe 
4300cea2ae0SManish V Badarkhe 	/* Do copy operation */
4310cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
4320cea2ae0SManish V Badarkhe 
4330cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
4340cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
4350cea2ae0SManish V Badarkhe 					root_mapped_size_align);
4360cea2ae0SManish V Badarkhe 	if (rc != 0) {
4370cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4380cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
4390cea2ae0SManish V Badarkhe 		panic();
4400cea2ae0SManish V Badarkhe 	}
4410cea2ae0SManish V Badarkhe 
4420cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4430cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4440cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4450cea2ae0SManish V Badarkhe 	if (rc != 0) {
4460cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4470cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4480cea2ae0SManish V Badarkhe 		panic();
4490cea2ae0SManish V Badarkhe 	}
4500cea2ae0SManish V Badarkhe }
4510cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4520cea2ae0SManish V Badarkhe 
4538cb99c3fSOlivier Deprez /*******************************************************************************
45452696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4550f14d02fSMax Shvetsov  ******************************************************************************/
45623d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4570f14d02fSMax Shvetsov {
458f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
459f2dcf418SOlivier Deprez 	unsigned int core_id;
4608cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
46152696946SOlivier Deprez 	int rc;
4620cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4630f14d02fSMax Shvetsov 
46452696946SOlivier Deprez 	/* Load the SPM Core manifest */
46523d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4660f14d02fSMax Shvetsov 	if (rc != 0) {
46752696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
46852696946SOlivier Deprez 		return rc;
4690f14d02fSMax Shvetsov 	}
4700f14d02fSMax Shvetsov 
4710f14d02fSMax Shvetsov 	/*
47252696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
47352696946SOlivier Deprez 	 * Dispatcher version.
4740f14d02fSMax Shvetsov 	 */
475662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
476662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
477662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4780f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
47952696946SOlivier Deprez 		return -EINVAL;
4800f14d02fSMax Shvetsov 	}
4810f14d02fSMax Shvetsov 
482662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
4830f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
4840f14d02fSMax Shvetsov 
48552696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
486033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
4870f14d02fSMax Shvetsov 
488ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
48952696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
49052696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
49152696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
49252696946SOlivier Deprez 		return -EINVAL;
493ac03ac5eSMax Shvetsov 	}
494ac03ac5eSMax Shvetsov 
49552696946SOlivier Deprez 	/* Validate the SPM Core execution state */
4960f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
4970f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
49823d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
4990f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
50052696946SOlivier Deprez 		return -EINVAL;
5010f14d02fSMax Shvetsov 	}
5020f14d02fSMax Shvetsov 
50323d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
50423d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
5050f14d02fSMax Shvetsov 
506033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
507033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
508033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
509033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
51052696946SOlivier Deprez 		return -EINVAL;
5110f14d02fSMax Shvetsov 	}
5120f14d02fSMax Shvetsov 
5130f14d02fSMax Shvetsov 	/*
5140f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
5150f14d02fSMax Shvetsov 	 * is required for SPM
5160f14d02fSMax Shvetsov 	 */
517623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
51852696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
51952696946SOlivier Deprez 		return -EINVAL;
5200f14d02fSMax Shvetsov 	}
521033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
5220f14d02fSMax Shvetsov 
5230f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
5240f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
52552696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
5260f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
5270f14d02fSMax Shvetsov 	}
5280f14d02fSMax Shvetsov 
5290f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
5300f14d02fSMax Shvetsov 
5310f14d02fSMax Shvetsov 	/*
53252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
53352696946SOlivier Deprez 	 * manifest.
5340f14d02fSMax Shvetsov 	 */
5350f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
5360f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
5370f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
5380f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
5390f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5400f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5410f14d02fSMax Shvetsov 	} else {
542033039f8SMax Shvetsov 
543033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
544033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
545033039f8SMax Shvetsov #else
546033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
547033039f8SMax Shvetsov #endif
548033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5490f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5500f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5510f14d02fSMax Shvetsov 	}
5520f14d02fSMax Shvetsov 
5530cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5540cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5550cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5560cea2ae0SManish V Badarkhe 
5570cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5580cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5590cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5600cea2ae0SManish V Badarkhe 		return -EINVAL;
5610cea2ae0SManish V Badarkhe 	}
5620cea2ae0SManish V Badarkhe 
5630cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5640cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5650cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5660cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5670cea2ae0SManish V Badarkhe 
5680cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5690cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5700cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5710cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5720cea2ae0SManish V Badarkhe 
573f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
574f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
575f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
5760f14d02fSMax Shvetsov 
577f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
578f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
579f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
5800f14d02fSMax Shvetsov 
581f2dcf418SOlivier Deprez 		/*
582f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
583f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
584f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
585f2dcf418SOlivier Deprez 		 */
586f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
587f2dcf418SOlivier Deprez 	}
5880f14d02fSMax Shvetsov 
589a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
590a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
591a334c4e6SOlivier Deprez 
5920f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5930f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
5940f14d02fSMax Shvetsov 
595f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
596f2dcf418SOlivier Deprez 
5978cb99c3fSOlivier Deprez 	/*
5988cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
5998cb99c3fSOlivier Deprez 	 * while the NWd is running.
6008cb99c3fSOlivier Deprez 	 */
6018cb99c3fSOlivier Deprez 	flags = 0;
6028cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
6038cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
6048cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
6058cb99c3fSOlivier Deprez 					     flags);
6068cb99c3fSOlivier Deprez 	if (rc != 0) {
6078cb99c3fSOlivier Deprez 		panic();
6088cb99c3fSOlivier Deprez 	}
6098cb99c3fSOlivier Deprez 
610a1e0e871SMadhukar Pappireddy 	/*
611bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
612bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
613bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
614bb6d0a17SOlivier Deprez 	 *
615bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
616bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
617bb6d0a17SOlivier Deprez 	 *
618bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
619bb6d0a17SOlivier Deprez 	 *
620bb6d0a17SOlivier Deprez 	 */
621bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
622bb6d0a17SOlivier Deprez 	/*
623fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
624fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
625fca5f0ebSMadhukar Pappireddy 	 * running.
626a1e0e871SMadhukar Pappireddy 	 */
627fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
628a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
629a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
630a1e0e871SMadhukar Pappireddy 						     flags);
631a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
632a1e0e871SMadhukar Pappireddy 			panic();
633a1e0e871SMadhukar Pappireddy 		}
634fca5f0ebSMadhukar Pappireddy 	}
635bb6d0a17SOlivier Deprez #endif
636bb6d0a17SOlivier Deprez 
6370f14d02fSMax Shvetsov 	return 0;
6380f14d02fSMax Shvetsov }
6390f14d02fSMax Shvetsov 
6400f14d02fSMax Shvetsov /*******************************************************************************
64152696946SOlivier Deprez  * Initialize context of SPM Core.
642bdd2596dSAchin Gupta  ******************************************************************************/
6430f14d02fSMax Shvetsov int spmd_setup(void)
644bdd2596dSAchin Gupta {
645bdd2596dSAchin Gupta 	int rc;
6466da76075SMarc Bonnici 	void *spmc_manifest;
6476da76075SMarc Bonnici 
6486da76075SMarc Bonnici 	/*
6496da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6506da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6516da76075SMarc Bonnici 	 */
6526da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6536da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6546da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6556da76075SMarc Bonnici 
6566da76075SMarc Bonnici 		rc = spmc_setup();
6576da76075SMarc Bonnici 		if (rc != 0) {
6580d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6596da76075SMarc Bonnici 		}
6600d33649eSOlivier Deprez 		return 0;
6616da76075SMarc Bonnici 	}
662bdd2596dSAchin Gupta 
663bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
66452696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
66552696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6660d33649eSOlivier Deprez 		return 0;
667bdd2596dSAchin Gupta 	}
668bdd2596dSAchin Gupta 
669bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
67052696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
671bdd2596dSAchin Gupta 
672bdd2596dSAchin Gupta 	/*
673bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
67452696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
675bdd2596dSAchin Gupta 	 */
67623d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
67723d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
6780d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
6790d33649eSOlivier Deprez 		return 0;
680bdd2596dSAchin Gupta 	}
681bdd2596dSAchin Gupta 
6820f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
68323d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
6840f14d02fSMax Shvetsov 	if (rc != 0) {
68552696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
686bdd2596dSAchin Gupta 	}
687bdd2596dSAchin Gupta 
6880d33649eSOlivier Deprez 	return 0;
6890f14d02fSMax Shvetsov }
6900f14d02fSMax Shvetsov 
6910f14d02fSMax Shvetsov /*******************************************************************************
692bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
6930f14d02fSMax Shvetsov  ******************************************************************************/
694bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
69552696946SOlivier Deprez 			       bool secure_origin,
69652696946SOlivier Deprez 			       uint64_t x1,
69752696946SOlivier Deprez 			       uint64_t x2,
69852696946SOlivier Deprez 			       uint64_t x3,
69952696946SOlivier Deprez 			       uint64_t x4,
700c925867eSOlivier Deprez 			       void *handle,
701bb9fc8c0SJay Monkman 			       uint64_t flags,
702bb9fc8c0SJay Monkman 			       uint32_t secure_ffa_version)
7030f14d02fSMax Shvetsov {
704c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
705c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
706bb9fc8c0SJay Monkman 	uint32_t version_in = (secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
707bb9fc8c0SJay Monkman 	uint32_t version_out = (!secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
708107e3cc0SOlivier Deprez 	void *ctx_out;
70993ff138bSOlivier Deprez 
710c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
711c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
712c925867eSOlivier Deprez 		/*
713c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
714c925867eSOlivier Deprez 		 * if it was set by the caller.
715c925867eSOlivier Deprez 		 */
716c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
717c925867eSOlivier Deprez 	}
718c925867eSOlivier Deprez #endif
719c925867eSOlivier Deprez 
7200f14d02fSMax Shvetsov 	/* Save incoming security state */
721033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
72293ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
723678ce223SOlivier Deprez #else
724678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
72559bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
72659bdcc58SMadhukar Pappireddy 	/* Forward the hint bit denoting the absence of SVE live state. */
72759bdcc58SMadhukar Pappireddy 	simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
72859bdcc58SMadhukar Pappireddy #endif
729033039f8SMax Shvetsov #endif
7300f14d02fSMax Shvetsov 
7310f14d02fSMax Shvetsov 	/* Restore outgoing security state */
732033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
73393ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
734678ce223SOlivier Deprez #else
735678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
73659bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
73759bdcc58SMadhukar Pappireddy 	simd_ctx_restore(secure_state_out);
73859bdcc58SMadhukar Pappireddy #endif
739033039f8SMax Shvetsov #endif
74093ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
7410f14d02fSMax Shvetsov 
742107e3cc0SOlivier Deprez 	ctx_out = cm_get_context(secure_state_out);
743a0a7f158SAndrei Homescu 	if (smc_fid == FFA_NORMAL_WORLD_RESUME) {
744a0a7f158SAndrei Homescu 		SMC_RET0(ctx_out);
745a0a7f158SAndrei Homescu 	}
746a0a7f158SAndrei Homescu 
747bb9fc8c0SJay Monkman 	if ((GET_SMC_CC(smc_fid) == SMC_64) && (version_out >= MAKE_FFA_VERSION(U(1), U(2)))) {
748bb9fc8c0SJay Monkman 		if (version_in < MAKE_FFA_VERSION(U(1), U(2))) {
749bb9fc8c0SJay Monkman 			/* FFA version mismatch, with dest >= 1.2 - set outgoing x8-x17 to zero */
750bb9fc8c0SJay Monkman 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
751bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X5),
752bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X6),
753bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X7),
754bb9fc8c0SJay Monkman 				  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
755bb9fc8c0SJay Monkman 		} else {
756bb9fc8c0SJay Monkman 			/* Both FFA versions >= 1.2 - pass incoming x8-x17 to dest */
757107e3cc0SOlivier Deprez 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
758eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X5),
759eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X6),
760eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X7),
761eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X8),
762eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X9),
763eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X10),
764eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X11),
765eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X12),
766eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X13),
767eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X14),
768eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X15),
769eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X16),
770eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X17)
771eaaf517cSRaghu Krishnamurthy 				);
772bb9fc8c0SJay Monkman 		}
773bb9fc8c0SJay Monkman 	} else {
774bb9fc8c0SJay Monkman 		/* 32 bit call or dest has FFA version < 1.2 or unknown */
775107e3cc0SOlivier Deprez 		SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
7760f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X5),
7770f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X6),
7780f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X7));
779bb9fc8c0SJay Monkman 	}
7800f14d02fSMax Shvetsov }
7810f14d02fSMax Shvetsov 
7820f14d02fSMax Shvetsov /*******************************************************************************
783bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
784bb01a673SMarc Bonnici  ******************************************************************************/
785bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
786bb01a673SMarc Bonnici 				 bool secure_origin,
787bb01a673SMarc Bonnici 				 uint64_t x1,
788bb01a673SMarc Bonnici 				 uint64_t x2,
789bb01a673SMarc Bonnici 				 uint64_t x3,
790bb01a673SMarc Bonnici 				 uint64_t x4,
791bb01a673SMarc Bonnici 				 void *cookie,
792bb01a673SMarc Bonnici 				 void *handle,
793bb9fc8c0SJay Monkman 				 uint64_t flags,
794bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version)
795bb01a673SMarc Bonnici {
796bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
797bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
798bb01a673SMarc Bonnici 					cookie, handle, flags);
799bb01a673SMarc Bonnici 	}
800c925867eSOlivier Deprez 
801bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
802bb9fc8c0SJay Monkman 				     handle, flags, secure_ffa_version);
803bb01a673SMarc Bonnici 
804bb01a673SMarc Bonnici }
805bb01a673SMarc Bonnici 
806bb01a673SMarc Bonnici /*******************************************************************************
807662af36dSJ-Alves  * Return FFA_ERROR with specified error code
8080f14d02fSMax Shvetsov  ******************************************************************************/
80995f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
8100f14d02fSMax Shvetsov {
811e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
812e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
813662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
814662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
815bdd2596dSAchin Gupta }
816bdd2596dSAchin Gupta 
817f0d743dbSOlivier Deprez /*******************************************************************************
818f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
819f0d743dbSOlivier Deprez  ******************************************************************************/
820f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
821f0d743dbSOlivier Deprez {
822f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
823f0d743dbSOlivier Deprez 
824f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
825f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
826f0d743dbSOlivier Deprez }
827f0d743dbSOlivier Deprez 
828c2901419SOlivier Deprez /******************************************************************************
829c2901419SOlivier Deprez  * spmd_is_spmc_message
830c2901419SOlivier Deprez  *****************************************************************************/
831c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
832c2901419SOlivier Deprez {
833bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
834bb01a673SMarc Bonnici 		return false;
835bb01a673SMarc Bonnici 	}
836bb01a673SMarc Bonnici 
837c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
838c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
839c2901419SOlivier Deprez }
840c2901419SOlivier Deprez 
841bdd2596dSAchin Gupta /*******************************************************************************
842bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
843bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
844bb01a673SMarc Bonnici  ******************************************************************************/
845bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
846bb01a673SMarc Bonnici 			      uint64_t x1,
847bb01a673SMarc Bonnici 			      uint64_t x2,
848bb01a673SMarc Bonnici 			      uint64_t x3,
849bb01a673SMarc Bonnici 			      uint64_t x4,
850bb01a673SMarc Bonnici 			      void *cookie,
851bb01a673SMarc Bonnici 			      void *handle,
852bb01a673SMarc Bonnici 			      uint64_t flags)
853bb01a673SMarc Bonnici {
854bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
855bb01a673SMarc Bonnici 		/*
856bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
857bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
858bb01a673SMarc Bonnici 		 */
859bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
860bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
861bb01a673SMarc Bonnici 						is_caller_secure(flags),
862bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
863bb01a673SMarc Bonnici 						handle, flags);
864bb01a673SMarc Bonnici 		}
865bb01a673SMarc Bonnici 	}
866bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
867bb9fc8c0SJay Monkman 				handle, flags, spmc_nwd_ffa_version);
868bb9fc8c0SJay Monkman }
869bb9fc8c0SJay Monkman 
870bb9fc8c0SJay Monkman static uint32_t get_common_ffa_version(uint32_t secure_ffa_version)
871bb9fc8c0SJay Monkman {
872bb9fc8c0SJay Monkman 	if (secure_ffa_version <= nonsecure_ffa_version) {
873bb9fc8c0SJay Monkman 		return secure_ffa_version;
874bb9fc8c0SJay Monkman 	} else {
875bb9fc8c0SJay Monkman 		return nonsecure_ffa_version;
876bb9fc8c0SJay Monkman 	}
877bb01a673SMarc Bonnici }
878bb01a673SMarc Bonnici 
879bb01a673SMarc Bonnici /*******************************************************************************
880662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
881bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
882bdd2596dSAchin Gupta  ******************************************************************************/
88352696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
88452696946SOlivier Deprez 			  uint64_t x1,
88552696946SOlivier Deprez 			  uint64_t x2,
88652696946SOlivier Deprez 			  uint64_t x3,
88752696946SOlivier Deprez 			  uint64_t x4,
88852696946SOlivier Deprez 			  void *cookie,
88952696946SOlivier Deprez 			  void *handle,
890bb9fc8c0SJay Monkman 			  uint64_t flags,
891bb9fc8c0SJay Monkman 			  uint32_t secure_ffa_version)
892bdd2596dSAchin Gupta {
89352696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
89493ff138bSOlivier Deprez 	bool secure_origin;
8956873088cSJ-Alves 	int ret;
8964388f28fSJ-Alves 	uint32_t input_version;
897bdd2596dSAchin Gupta 
898bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
89993ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
900bdd2596dSAchin Gupta 
9014ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
9024ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
903eac8077aSOlivier Deprez 		    plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
904cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
905bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
906bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
907bdd2596dSAchin Gupta 
9080b850e9eSRaghu Krishnamurthy 	/*
9090b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
9100b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
9110b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
9120b850e9eSRaghu Krishnamurthy 	 */
9130b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
9140b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
9150b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
9160b850e9eSRaghu Krishnamurthy 	}
9170b850e9eSRaghu Krishnamurthy 
918bb9fc8c0SJay Monkman 	if ((!secure_origin) && (smc_fid != FFA_VERSION)) {
919bb9fc8c0SJay Monkman 		/*
920bb9fc8c0SJay Monkman 		 * Once the caller invokes any FF-A ABI other than FFA_VERSION,
921bb9fc8c0SJay Monkman 		 * the version negotiation phase is complete.
922bb9fc8c0SJay Monkman 		 */
923bb9fc8c0SJay Monkman 		nonsecure_version_negotiated = true;
924bb9fc8c0SJay Monkman 	}
925bb9fc8c0SJay Monkman 
926bdd2596dSAchin Gupta 	switch (smc_fid) {
927662af36dSJ-Alves 	case FFA_ERROR:
928bdd2596dSAchin Gupta 		/*
929bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
93052696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
931bdd2596dSAchin Gupta 		 * unsuccessfully.
932bdd2596dSAchin Gupta 		 */
9339dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
934bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
9350f14d02fSMax Shvetsov 		}
936bdd2596dSAchin Gupta 
93766bdfd6eSRaghu Krishnamurthy 		/*
9388723eaf2SMadhukar Pappireddy 		 * Perform a synchronous exit:
9398723eaf2SMadhukar Pappireddy 		 * 1. If there was an SPMD logical partition direct request on-going,
94066bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
94166bdfd6eSRaghu Krishnamurthy 		 * consumed.
9428723eaf2SMadhukar Pappireddy 		 * 2. SPMC sent FFA_ERROR in response to a power management
9438723eaf2SMadhukar Pappireddy 		 * operation sent through direct request.
94466bdfd6eSRaghu Krishnamurthy 		 */
9458723eaf2SMadhukar Pappireddy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx) ||
9468723eaf2SMadhukar Pappireddy 		    ctx->psci_operation_ongoing) {
94766bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
94866bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
94966bdfd6eSRaghu Krishnamurthy 		}
95066bdfd6eSRaghu Krishnamurthy 
95193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
952bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
953bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
954bdd2596dSAchin Gupta 		break; /* not reached */
955bdd2596dSAchin Gupta 
956662af36dSJ-Alves 	case FFA_VERSION:
9574388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
958bdd2596dSAchin Gupta 		/*
9594388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
9604388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
9614388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
962bb9fc8c0SJay Monkman 		 * forward to the EL3 SPMC if enabled, otherwise send a
963bb9fc8c0SJay Monkman 		 * framework message to the SPMC at the lower EL to
964bb9fc8c0SJay Monkman 		 * negotiate a version that is compatible between the
965bb9fc8c0SJay Monkman 		 * normal world and the SPMC.
9664388f28fSJ-Alves 		 * Sanity check to "input_version".
967bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
968bb01a673SMarc Bonnici 		 * this is not used.
969bdd2596dSAchin Gupta 		 */
9704388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
971bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
9724388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
9734388f28fSJ-Alves 		} else if (!secure_origin) {
974bb9fc8c0SJay Monkman 			if (!nonsecure_version_negotiated) {
975bb9fc8c0SJay Monkman 				/*
976bb9fc8c0SJay Monkman 				 * Once an FF-A version has been negotiated
977bb9fc8c0SJay Monkman 				 * between a caller and a callee, the version
978bb9fc8c0SJay Monkman 				 * may not be changed for the lifetime of
979bb9fc8c0SJay Monkman 				 * the calling component.
980bb9fc8c0SJay Monkman 				 */
981bb9fc8c0SJay Monkman 				nonsecure_ffa_version = input_version;
982bb9fc8c0SJay Monkman 			}
983bb9fc8c0SJay Monkman 
9849576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
9859576fa93SMarc Bonnici 				/*
9869576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
9879576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
9889576fa93SMarc Bonnici 				 * a direct request.
9899576fa93SMarc Bonnici 				 */
990bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version =
991bb9fc8c0SJay Monkman 					MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
992bb9fc8c0SJay Monkman 				return spmc_smc_handler(smc_fid, secure_origin,
9939576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
9949576fa93SMarc Bonnici 							handle, flags);
9959576fa93SMarc Bonnici 			}
9969576fa93SMarc Bonnici 
9979944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
9989944f557SDaniel Boulby 			uint64_t rc;
9999944f557SDaniel Boulby 
10009944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
10019944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
1002e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
1003e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
1004bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10059944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
10069944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10079944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10089944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10099944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10109944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
10119944f557SDaniel Boulby 				break;
10129944f557SDaniel Boulby 			}
10139944f557SDaniel Boulby 			/* Save non-secure system registers context */
10149944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
10159944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
10162d960a11SMadhukar Pappireddy #else
10172d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
10189944f557SDaniel Boulby #endif
10199944f557SDaniel Boulby 
10209944f557SDaniel Boulby 			/*
10219944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
10229944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
10239944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
10249944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
10259944f557SDaniel Boulby 			 */
10269944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
10279944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
10289944f557SDaniel Boulby 						input_version);
10299944f557SDaniel Boulby 
103076d53ee1SOlivier Deprez 			/*
103176d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
103276d53ee1SOlivier Deprez 			 * from the SPMC.
103376d53ee1SOlivier Deprez 			 */
103476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
103576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
103676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
103776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
103876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
103976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
104076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
104176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
104276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
104376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
104476d53ee1SOlivier Deprez 
10459944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
10469944f557SDaniel Boulby 
10479944f557SDaniel Boulby 			if ((rc != 0ULL) ||
10489944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
10499944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
10509944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
105159bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
10529944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
10539944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
10549944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
10559944f557SDaniel Boulby 			} else {
10569944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
1057bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10589944f557SDaniel Boulby 			}
10599944f557SDaniel Boulby 
10609944f557SDaniel Boulby 			/*
106176d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
106276d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
106376d53ee1SOlivier Deprez 			 */
106476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
106576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
106676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
106776d53ee1SOlivier Deprez 
106876d53ee1SOlivier Deprez 			/*
10699944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
10709944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
10719944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
10729944f557SDaniel Boulby 			 */
10739944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
10749944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1075bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
1076bb9fc8c0SJay Monkman 						flags, spmc_nwd_ffa_version);
10774388f28fSJ-Alves 		} else {
1078e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1079e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
10804388f28fSJ-Alves 		}
10814388f28fSJ-Alves 
1082e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1083e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1084e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1085bdd2596dSAchin Gupta 		break; /* not reached */
1086bdd2596dSAchin Gupta 
1087662af36dSJ-Alves 	case FFA_FEATURES:
1088bdd2596dSAchin Gupta 		/*
1089bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
109052696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1091bdd2596dSAchin Gupta 		 */
1092bdd2596dSAchin Gupta 
109352696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
109493ff138bSOlivier Deprez 		if (!secure_origin) {
109593ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1096bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1097bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
109852696946SOlivier Deprez 		}
109952696946SOlivier Deprez 
1100bdd2596dSAchin Gupta 		/*
1101bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1102662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1103bdd2596dSAchin Gupta 		 * nop.
1104bdd2596dSAchin Gupta 		 */
1105662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1106bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1107bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1108bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
11090f14d02fSMax Shvetsov 
1110bdd2596dSAchin Gupta 		break; /* not reached */
1111bdd2596dSAchin Gupta 
1112662af36dSJ-Alves 	case FFA_ID_GET:
1113ac03ac5eSMax Shvetsov 		/*
1114662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1115ac03ac5eSMax Shvetsov 		 */
1116ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1117662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1118662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1119662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1120662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1121662af36dSJ-Alves 				 FFA_PARAM_MBZ);
112252696946SOlivier Deprez 		}
112352696946SOlivier Deprez 
1124662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1125662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1126662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1127662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1128662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1129ac03ac5eSMax Shvetsov 
1130ac03ac5eSMax Shvetsov 		break; /* not reached */
1131ac03ac5eSMax Shvetsov 
1132cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1133cdb49d47SOlivier Deprez 		if (secure_origin) {
1134cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1135cdb49d47SOlivier Deprez 
1136cdb49d47SOlivier Deprez 			if (ret < 0) {
1137cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1138cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1139cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1140cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1141cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1142cdb49d47SOlivier Deprez 			} else {
1143cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1144cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1145cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1146cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1147cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1148cdb49d47SOlivier Deprez 			}
1149cdb49d47SOlivier Deprez 		}
1150cdb49d47SOlivier Deprez 
1151cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1152cdb49d47SOlivier Deprez 		break; /* Not reached */
1153cdb49d47SOlivier Deprez 
115470c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
115570c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
115670c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
115770c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
115870c121a2SDaniel Boulby 		}
115970c121a2SDaniel Boulby 		/*
116070c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
116170c121a2SDaniel Boulby 		 * instance where this function is invoked
116270c121a2SDaniel Boulby 		 */
116370c121a2SDaniel Boulby 		if (!secure_origin) {
116470c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
116570c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
116670c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
116770c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
116870c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
116970c121a2SDaniel Boulby 		}
117070c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
117170c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
117270c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
117370c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
117470c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
117570c121a2SDaniel Boulby 
117670c121a2SDaniel Boulby 		break; /* not reached */
117770c121a2SDaniel Boulby 
1178bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1179bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1180bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1181bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1182bb9fc8c0SJay Monkman 		}
1183bb9fc8c0SJay Monkman 		/* fallthrough */
1184f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
11855519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
118666bdfd6eSRaghu Krishnamurthy 		/*
118766bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
118866bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
118966bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
119066bdfd6eSRaghu Krishnamurthy 		 */
119166bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
119266bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
119366bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
119466bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
119566bdfd6eSRaghu Krishnamurthy 						     );
119666bdfd6eSRaghu Krishnamurthy 		}
119766bdfd6eSRaghu Krishnamurthy 
119866bdfd6eSRaghu Krishnamurthy 		/*
119966bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
120066bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
120166bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
120266bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
120366bdfd6eSRaghu Krishnamurthy 		 * error codes.
120466bdfd6eSRaghu Krishnamurthy 		 */
120566bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
120666bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
120766bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
120866bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
120966bdfd6eSRaghu Krishnamurthy 		}
121066bdfd6eSRaghu Krishnamurthy 
12115519f07cSShruti 		if (!secure_origin) {
12125519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
12135519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
12145519f07cSShruti 				return spmd_ffa_error_return(handle,
12155519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
12165519f07cSShruti 			}
12175519f07cSShruti 		}
1218f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1219cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
12206c378c2fSKathleen Capella 						FFA_ERROR_DENIED);
1221cc6047b3SKathleen Capella 		} else {
1222cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1223cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1224cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1225bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1226cc6047b3SKathleen Capella 		}
1227cc6047b3SKathleen Capella 		break; /* Not reached */
1228cc6047b3SKathleen Capella 
1229bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1230bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1231bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1232bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1233bb9fc8c0SJay Monkman 		}
1234bb9fc8c0SJay Monkman 		/* fallthrough */
1235f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
123666bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
123766bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
123866bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
12398cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1240f0d743dbSOlivier Deprez 		} else {
1241f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1242f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1243bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1244bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1245f0d743dbSOlivier Deprez 		}
1246f0d743dbSOlivier Deprez 		break; /* Not reached */
1247662af36dSJ-Alves 	case FFA_RX_RELEASE:
1248662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1249662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1250662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1251545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1252fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1253fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1254fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1255fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1256fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1257fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1258fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1259fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1260fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1261c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1262d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
1263fc3f4800SJ-Alves #endif
1264662af36dSJ-Alves 	case FFA_MSG_RUN:
1265c2eba07cSFederico Recanati 		/*
1266c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1267c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1268c2eba07cSFederico Recanati 		 */
126993ff138bSOlivier Deprez 		if (secure_origin) {
1270662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1271662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1272bdd2596dSAchin Gupta 		}
1273bdd2596dSAchin Gupta 
1274e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1275e138400dSBoyan Karatotev 		/* fallthrough */
1276662af36dSJ-Alves 	case FFA_MSG_SEND:
1277662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1278662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1279662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1280662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1281662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1282662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1283662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1284662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1285662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1286662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1287662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1288642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1289642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1290662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1291662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1292bdd2596dSAchin Gupta 		/*
129366bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
129466bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1295bdd2596dSAchin Gupta 		 */
129666bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
129766bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
129866bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
129966bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
130066bdfd6eSRaghu Krishnamurthy 		}
1301bdd2596dSAchin Gupta 
130293ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1303bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1304bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1305bdd2596dSAchin Gupta 		break; /* not reached */
1306bdd2596dSAchin Gupta 
1307662af36dSJ-Alves 	case FFA_MSG_WAIT:
1308bdd2596dSAchin Gupta 		/*
1309bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1310bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
131152696946SOlivier Deprez 		 * SPM Core initialised successfully.
1312bdd2596dSAchin Gupta 		 */
13139dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
13148cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1315bdd2596dSAchin Gupta 		}
1316bdd2596dSAchin Gupta 
1317e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1318e138400dSBoyan Karatotev 		/* fallthrough */
1319386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1320662af36dSJ-Alves 	case FFA_MSG_YIELD:
1321bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
132293ff138bSOlivier Deprez 		if (!secure_origin) {
1323662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1324662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1325bdd2596dSAchin Gupta 		}
1326bdd2596dSAchin Gupta 
132766bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
132866bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
132966bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
133066bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
133166bdfd6eSRaghu Krishnamurthy 		}
133266bdfd6eSRaghu Krishnamurthy 
133393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1334bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1335bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1336bdd2596dSAchin Gupta 		break; /* not reached */
1337bdd2596dSAchin Gupta 
13388cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
13398cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
13408cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
13418cb99c3fSOlivier Deprez 		} else {
13428cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
13438cb99c3fSOlivier Deprez 		}
13448cb99c3fSOlivier Deprez 		break; /* Not reached */
1345eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1346eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1347eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
134895f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
134995f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1350eaaf517cSRaghu Krishnamurthy 		}
13518cb99c3fSOlivier Deprez 
1352eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1353eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1354eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1355eaaf517cSRaghu Krishnamurthy 		}
1356eaaf517cSRaghu Krishnamurthy 
1357eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1358eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1359bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1360eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1361eaaf517cSRaghu Krishnamurthy #endif
1362638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1363638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1364638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1365638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1366638a6f8eSShruti Gupta 		break; /* not reached */
1367638a6f8eSShruti Gupta 
13686671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13696671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13706671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
13716671b3d8SMadhukar Pappireddy 		} else {
13726c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
13736671b3d8SMadhukar Pappireddy 		}
1374bdd2596dSAchin Gupta 	default:
1375bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1376662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1377bdd2596dSAchin Gupta 	}
1378bdd2596dSAchin Gupta }
1379