xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 9f3f4d87b9bdd613d3f7f300e8ed0bc71d1cd7c8)
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"
3655fd56d7SYeoreum Yun #if TRANSFER_LIST
3755fd56d7SYeoreum Yun #include <transfer_list.h>
3855fd56d7SYeoreum Yun #endif
39bdd2596dSAchin Gupta 
40bdd2596dSAchin Gupta /*******************************************************************************
41bdd2596dSAchin Gupta  * SPM Core context information.
42bdd2596dSAchin Gupta  ******************************************************************************/
4352696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
44bdd2596dSAchin Gupta 
45bdd2596dSAchin Gupta /*******************************************************************************
466da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
476da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
48bdd2596dSAchin Gupta  ******************************************************************************/
4952696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
500f14d02fSMax Shvetsov 
510f14d02fSMax Shvetsov /*******************************************************************************
52bb9fc8c0SJay Monkman  * FFA version used by nonsecure endpoint.
53bb9fc8c0SJay Monkman  ******************************************************************************/
54bb9fc8c0SJay Monkman static uint32_t nonsecure_ffa_version;
55bb9fc8c0SJay Monkman 
56bb9fc8c0SJay Monkman /*******************************************************************************
57bb9fc8c0SJay Monkman  * Whether the normal world finished negotiating its version.
58bb9fc8c0SJay Monkman  ******************************************************************************/
59bb9fc8c0SJay Monkman static bool nonsecure_version_negotiated;
60bb9fc8c0SJay Monkman 
61bb9fc8c0SJay Monkman /*******************************************************************************
62bb9fc8c0SJay Monkman  * FFA version used by SPMC, as seen by the normal world.
63bb9fc8c0SJay Monkman  ******************************************************************************/
64bb9fc8c0SJay Monkman static uint32_t spmc_nwd_ffa_version;
65bb9fc8c0SJay Monkman 
66bb9fc8c0SJay Monkman /*******************************************************************************
670f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
680f14d02fSMax Shvetsov  * on secondary cores.
690f14d02fSMax Shvetsov  ******************************************************************************/
700f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
710f14d02fSMax Shvetsov 
720f14d02fSMax Shvetsov /*******************************************************************************
7352696946SOlivier Deprez  * SPM Core context on current CPU get helper.
7452696946SOlivier Deprez  ******************************************************************************/
7552696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
7652696946SOlivier Deprez {
77c8cea3b8SOlivier Deprez 	return &spm_core_context[plat_my_core_pos()];
7852696946SOlivier Deprez }
7952696946SOlivier Deprez 
8052696946SOlivier Deprez /*******************************************************************************
81a92bc73bSOlivier Deprez  * SPM Core ID getter.
82a92bc73bSOlivier Deprez  ******************************************************************************/
83a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
84a92bc73bSOlivier Deprez {
85a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
86a92bc73bSOlivier Deprez }
87a92bc73bSOlivier Deprez 
88a92bc73bSOlivier Deprez /*******************************************************************************
890f14d02fSMax Shvetsov  * Static function declaration.
900f14d02fSMax Shvetsov  ******************************************************************************/
910f14d02fSMax Shvetsov static int32_t spmd_init(void);
9223d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
9395f7f6d8SRaghu Krishnamurthy 
9452696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
9552696946SOlivier Deprez 				 bool secure_origin,
9652696946SOlivier Deprez 				 uint64_t x1,
9752696946SOlivier Deprez 				 uint64_t x2,
9852696946SOlivier Deprez 				 uint64_t x3,
9952696946SOlivier Deprez 				 uint64_t x4,
100bb01a673SMarc Bonnici 				 void *cookie,
101bb01a673SMarc Bonnici 				 void *handle,
102bb9fc8c0SJay Monkman 				 uint64_t flags,
103bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version);
104bdd2596dSAchin Gupta 
1059944f557SDaniel Boulby /******************************************************************************
1069944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
1079944f557SDaniel Boulby  *****************************************************************************/
1089944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
1099944f557SDaniel Boulby 			     unsigned long long message)
1109944f557SDaniel Boulby {
1119944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1129944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1139944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1149944f557SDaniel Boulby 		 spmd_spmc_id_get());
1159944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1169944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
11776d53ee1SOlivier Deprez 
11876d53ee1SOlivier Deprez 	/* Zero out x4-x7 for the direct request emitted towards the SPMC. */
11976d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
12076d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
12176d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
12276d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1239944f557SDaniel Boulby }
1249944f557SDaniel Boulby 
1259944f557SDaniel Boulby 
126bdd2596dSAchin Gupta /*******************************************************************************
12752696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
12852696946SOlivier Deprez  * SPMC entry.
129bdd2596dSAchin Gupta  ******************************************************************************/
130bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
131bdd2596dSAchin Gupta {
132bdd2596dSAchin Gupta 	uint64_t rc;
133bdd2596dSAchin Gupta 
134bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
135bdd2596dSAchin Gupta 
136bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
137bdd2596dSAchin Gupta 
138bdd2596dSAchin Gupta 	/* Restore the context assigned above */
139033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
14028f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
141678ce223SOlivier Deprez #else
142678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
143033039f8SMax Shvetsov #endif
144bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
145bdd2596dSAchin Gupta 
146033039f8SMax Shvetsov 	/* Enter SPMC */
147bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
148bdd2596dSAchin Gupta 
149bdd2596dSAchin Gupta 	/* Save secure state */
150033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
15128f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
152678ce223SOlivier Deprez #else
153678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
154033039f8SMax Shvetsov #endif
155bdd2596dSAchin Gupta 
156bdd2596dSAchin Gupta 	return rc;
157bdd2596dSAchin Gupta }
158bdd2596dSAchin Gupta 
159bdd2596dSAchin Gupta /*******************************************************************************
16052696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
161bdd2596dSAchin Gupta  * called originally.
162bdd2596dSAchin Gupta  ******************************************************************************/
163bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
164bdd2596dSAchin Gupta {
16552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
166bdd2596dSAchin Gupta 
16752696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
168bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
169bdd2596dSAchin Gupta 
170bdd2596dSAchin Gupta 	/*
171bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
172bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
173bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
174bdd2596dSAchin Gupta 	 */
175bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
176bdd2596dSAchin Gupta 
177bdd2596dSAchin Gupta 	panic();
178bdd2596dSAchin Gupta }
179bdd2596dSAchin Gupta 
180*9f3f4d87SBoyan Karatotev void spmd_setup_context(unsigned int core_id)
181*9f3f4d87SBoyan Karatotev {
182*9f3f4d87SBoyan Karatotev 	cpu_context_t *cpu_ctx;
183*9f3f4d87SBoyan Karatotev 
184*9f3f4d87SBoyan Karatotev 	spm_core_context[core_id].state = SPMC_STATE_OFF;
185*9f3f4d87SBoyan Karatotev 
186*9f3f4d87SBoyan Karatotev 	/* Setup an initial cpu context for the SPMC. */
187*9f3f4d87SBoyan Karatotev 	cpu_ctx = &spm_core_context[core_id].cpu_ctx;
188*9f3f4d87SBoyan Karatotev 	cm_setup_context(cpu_ctx, spmc_ep_info);
189*9f3f4d87SBoyan Karatotev 
190*9f3f4d87SBoyan Karatotev 	/*
191*9f3f4d87SBoyan Karatotev 	 * Pass the core linear ID to the SPMC through x4.
192*9f3f4d87SBoyan Karatotev 	 * (TF-A implementation defined behavior helping
193*9f3f4d87SBoyan Karatotev 	 * a legacy TOS migration to adopt FF-A).
194*9f3f4d87SBoyan Karatotev 	 */
195*9f3f4d87SBoyan Karatotev 	write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
196*9f3f4d87SBoyan Karatotev }
197*9f3f4d87SBoyan Karatotev 
198bdd2596dSAchin Gupta /*******************************************************************************
19952696946SOlivier Deprez  * Jump to the SPM Core for the first time.
200bdd2596dSAchin Gupta  ******************************************************************************/
201bdd2596dSAchin Gupta static int32_t spmd_init(void)
202bdd2596dSAchin Gupta {
20352696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
20452696946SOlivier Deprez 	uint64_t rc;
205bdd2596dSAchin Gupta 
20652696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
2079dcf63ddSOlivier Deprez 
208f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
209f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
210bdd2596dSAchin Gupta 
211bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
21252696946SOlivier Deprez 	if (rc != 0ULL) {
2134ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
21452696946SOlivier Deprez 		return 0;
215bdd2596dSAchin Gupta 	}
216bdd2596dSAchin Gupta 
2179dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
2189dcf63ddSOlivier Deprez 
21952696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
220bdd2596dSAchin Gupta 
221890b5088SRaghu Krishnamurthy 	spmd_logical_sp_set_spmc_initialized();
222890b5088SRaghu Krishnamurthy 	rc = spmd_logical_sp_init();
223890b5088SRaghu Krishnamurthy 	if (rc != 0) {
224890b5088SRaghu Krishnamurthy 		WARN("SPMD Logical partitions failed init.\n");
225890b5088SRaghu Krishnamurthy 	}
226890b5088SRaghu Krishnamurthy 
227bdd2596dSAchin Gupta 	return 1;
228bdd2596dSAchin Gupta }
229bdd2596dSAchin Gupta 
230bdd2596dSAchin Gupta /*******************************************************************************
2318cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
2328cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
2338cb99c3fSOlivier Deprez  * itself or a Secure Partition.
2348cb99c3fSOlivier Deprez  ******************************************************************************/
2358cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
2368cb99c3fSOlivier Deprez 					      uint32_t flags,
2378cb99c3fSOlivier Deprez 					      void *handle,
2388cb99c3fSOlivier Deprez 					      void *cookie)
2398cb99c3fSOlivier Deprez {
2408cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2418cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2428cb99c3fSOlivier Deprez 	int64_t rc;
2438cb99c3fSOlivier Deprez 
2448cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2458cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2468cb99c3fSOlivier Deprez 
2478cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2488cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2498cb99c3fSOlivier Deprez 
2508cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2518cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2528cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2532d960a11SMadhukar Pappireddy #else
2542d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_save(NON_SECURE);
25559bdcc58SMadhukar Pappireddy 
25659bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
25759bdcc58SMadhukar Pappireddy 	/*
25859bdcc58SMadhukar Pappireddy 	 * The hint bit denoting absence of SVE live state is effectively false
25959bdcc58SMadhukar Pappireddy 	 * in this scenario where execution was trapped to EL3 due to FIQ.
26059bdcc58SMadhukar Pappireddy 	 */
26159bdcc58SMadhukar Pappireddy 	simd_ctx_save(NON_SECURE, false);
2628f60d99fSRakshit Goyal 	simd_ctx_restore(SECURE);
26359bdcc58SMadhukar Pappireddy #endif
2648cb99c3fSOlivier Deprez #endif
2658cb99c3fSOlivier Deprez 
2668cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2678cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2688cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2698cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2708cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2718cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2728cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2738cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2748cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2758cb99c3fSOlivier Deprez 
2768cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2778cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2788cb99c3fSOlivier Deprez 
2798cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
28059bdcc58SMadhukar Pappireddy 
2818cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
282eac8077aSOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, plat_my_core_pos());
2838cb99c3fSOlivier Deprez 	}
2848cb99c3fSOlivier Deprez 
2858cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2868cb99c3fSOlivier Deprez 
2878cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2888cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2892d960a11SMadhukar Pappireddy #else
2902d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_restore(NON_SECURE);
29159bdcc58SMadhukar Pappireddy 
29259bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
2938f60d99fSRakshit Goyal 	simd_ctx_save(SECURE, false);
29459bdcc58SMadhukar Pappireddy 	simd_ctx_restore(NON_SECURE);
29559bdcc58SMadhukar Pappireddy #endif
2968cb99c3fSOlivier Deprez #endif
2978cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2988cb99c3fSOlivier Deprez 
2998cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
3008cb99c3fSOlivier Deprez }
3018cb99c3fSOlivier Deprez 
302bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
303a1e0e871SMadhukar Pappireddy /*******************************************************************************
304a1e0e871SMadhukar Pappireddy  * spmd_group0_interrupt_handler_nwd
305a1e0e871SMadhukar Pappireddy  * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
306a1e0e871SMadhukar Pappireddy  * handling of the interrupt to the platform handler, and return only upon
307a1e0e871SMadhukar Pappireddy  * successfully handling the Group0 interrupt.
308a1e0e871SMadhukar Pappireddy  ******************************************************************************/
309a1e0e871SMadhukar Pappireddy static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
310a1e0e871SMadhukar Pappireddy 						  uint32_t flags,
311a1e0e871SMadhukar Pappireddy 						  void *handle,
312a1e0e871SMadhukar Pappireddy 						  void *cookie)
313a1e0e871SMadhukar Pappireddy {
3145b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
315a1e0e871SMadhukar Pappireddy 
316a1e0e871SMadhukar Pappireddy 	/* Sanity check the security state when the exception was generated. */
317a1e0e871SMadhukar Pappireddy 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
318a1e0e871SMadhukar Pappireddy 
319a1e0e871SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context. */
320a1e0e871SMadhukar Pappireddy 	assert(handle == cm_get_context(NON_SECURE));
321a1e0e871SMadhukar Pappireddy 
322a1e0e871SMadhukar Pappireddy 	assert(id == INTR_ID_UNAVAILABLE);
323a1e0e871SMadhukar Pappireddy 
324a1e0e871SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
325a1e0e871SMadhukar Pappireddy 
3265b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3275b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3285b10f25aSMadhukar Pappireddy 
3295b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3305b10f25aSMadhukar Pappireddy 		return 0U;
3315b10f25aSMadhukar Pappireddy 	}
332a1e0e871SMadhukar Pappireddy 
333a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
334a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
335a1e0e871SMadhukar Pappireddy 		panic();
336a1e0e871SMadhukar Pappireddy 	}
337a1e0e871SMadhukar Pappireddy 
3386c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3396c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3406c91fc44SMadhukar Pappireddy 
341a1e0e871SMadhukar Pappireddy 	return 0U;
342a1e0e871SMadhukar Pappireddy }
343bb6d0a17SOlivier Deprez #endif
344a1e0e871SMadhukar Pappireddy 
3456671b3d8SMadhukar Pappireddy /*******************************************************************************
3466671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
3476671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
3486671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
3496671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
3506671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
3516671b3d8SMadhukar Pappireddy  ******************************************************************************/
3526671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
3536671b3d8SMadhukar Pappireddy {
3545b10f25aSMadhukar Pappireddy 	uint32_t intid, intr_raw;
3556671b3d8SMadhukar Pappireddy 
3566671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3576671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3586671b3d8SMadhukar Pappireddy 
3596671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3606671b3d8SMadhukar Pappireddy 
3615b10f25aSMadhukar Pappireddy 	intr_raw = plat_ic_acknowledge_interrupt();
3625b10f25aSMadhukar Pappireddy 	intid = plat_ic_get_interrupt_id(intr_raw);
3635b10f25aSMadhukar Pappireddy 
3645b10f25aSMadhukar Pappireddy 	if (intid == INTR_ID_UNAVAILABLE) {
3655b10f25aSMadhukar Pappireddy 		return 0U;
3665b10f25aSMadhukar Pappireddy 	}
3676671b3d8SMadhukar Pappireddy 
3686671b3d8SMadhukar Pappireddy 	/*
3696671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3706671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3716671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3726671b3d8SMadhukar Pappireddy 	 */
3736671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3746671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3756671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3766671b3d8SMadhukar Pappireddy 		panic();
3776671b3d8SMadhukar Pappireddy 	}
3786671b3d8SMadhukar Pappireddy 
3796c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3806c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3816c91fc44SMadhukar Pappireddy 
3826671b3d8SMadhukar Pappireddy 	/* Return success. */
3836671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3846671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3856671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3866671b3d8SMadhukar Pappireddy }
3876671b3d8SMadhukar Pappireddy 
3880cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3890cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3900cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3910cea2ae0SManish V Badarkhe 				 size_t *align_size)
3920cea2ae0SManish V Badarkhe {
3930cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3940cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3950cea2ae0SManish V Badarkhe 	int rc;
3960cea2ae0SManish V Badarkhe 
3970cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3980cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
3990cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
4000cea2ae0SManish V Badarkhe 
4010cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
4020cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
4030cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
4040cea2ae0SManish V Badarkhe 	}
4050cea2ae0SManish V Badarkhe 
4060cea2ae0SManish V Badarkhe 	/*
4070cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
4080cea2ae0SManish V Badarkhe 	 * size
4090cea2ae0SManish V Badarkhe 	 */
4100cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
4110cea2ae0SManish V Badarkhe 				     base_addr_align,
4120cea2ae0SManish V Badarkhe 				     mapped_size_align,
4130cea2ae0SManish V Badarkhe 				     attr);
4140cea2ae0SManish V Badarkhe 	if (rc == 0) {
4150cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
4160cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
4170cea2ae0SManish V Badarkhe 	}
4180cea2ae0SManish V Badarkhe 
4190cea2ae0SManish V Badarkhe 	return rc;
4200cea2ae0SManish V Badarkhe }
4210cea2ae0SManish V Badarkhe 
4220cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
4230cea2ae0SManish V Badarkhe 			    size_t size)
4240cea2ae0SManish V Badarkhe {
4250cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
4260cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
4270cea2ae0SManish V Badarkhe 	int rc;
4280cea2ae0SManish V Badarkhe 
4290cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
4300cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
4310cea2ae0SManish V Badarkhe 	assert(size != 0UL);
4320cea2ae0SManish V Badarkhe 
4330cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
4340cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
4350cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
4360cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
4370cea2ae0SManish V Badarkhe 	if (rc != 0) {
4380cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
4390cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
4400cea2ae0SManish V Badarkhe 		panic();
4410cea2ae0SManish V Badarkhe 	}
4420cea2ae0SManish V Badarkhe 
4430cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
4440cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
4450cea2ae0SManish V Badarkhe 	if (rc != 0) {
4460cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
4470cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
4480cea2ae0SManish V Badarkhe 		panic();
4490cea2ae0SManish V Badarkhe 	}
4500cea2ae0SManish V Badarkhe 
4510cea2ae0SManish V Badarkhe 	/* Do copy operation */
4520cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
4530cea2ae0SManish V Badarkhe 
4540cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
4550cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
4560cea2ae0SManish V Badarkhe 					root_mapped_size_align);
4570cea2ae0SManish V Badarkhe 	if (rc != 0) {
4580cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4590cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
4600cea2ae0SManish V Badarkhe 		panic();
4610cea2ae0SManish V Badarkhe 	}
4620cea2ae0SManish V Badarkhe 
4630cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4640cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4650cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4660cea2ae0SManish V Badarkhe 	if (rc != 0) {
4670cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4680cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4690cea2ae0SManish V Badarkhe 		panic();
4700cea2ae0SManish V Badarkhe 	}
4710cea2ae0SManish V Badarkhe }
4720cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4730cea2ae0SManish V Badarkhe 
4748cb99c3fSOlivier Deprez /*******************************************************************************
47552696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4760f14d02fSMax Shvetsov  ******************************************************************************/
47723d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4780f14d02fSMax Shvetsov {
4798cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
48052696946SOlivier Deprez 	int rc;
4810cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4820f14d02fSMax Shvetsov 
48352696946SOlivier Deprez 	/* Load the SPM Core manifest */
48423d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4850f14d02fSMax Shvetsov 	if (rc != 0) {
48652696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
48752696946SOlivier Deprez 		return rc;
4880f14d02fSMax Shvetsov 	}
4890f14d02fSMax Shvetsov 
4900f14d02fSMax Shvetsov 	/*
49152696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
49252696946SOlivier Deprez 	 * Dispatcher version.
4930f14d02fSMax Shvetsov 	 */
494662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
495662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
496662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4970f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
49852696946SOlivier Deprez 		return -EINVAL;
4990f14d02fSMax Shvetsov 	}
5000f14d02fSMax Shvetsov 
501662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
5020f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
5030f14d02fSMax Shvetsov 
50452696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
505033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
5060f14d02fSMax Shvetsov 
507ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
50852696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
50952696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
51052696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
51152696946SOlivier Deprez 		return -EINVAL;
512ac03ac5eSMax Shvetsov 	}
513ac03ac5eSMax Shvetsov 
51452696946SOlivier Deprez 	/* Validate the SPM Core execution state */
5150f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
5160f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
51723d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
5180f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
51952696946SOlivier Deprez 		return -EINVAL;
5200f14d02fSMax Shvetsov 	}
5210f14d02fSMax Shvetsov 
52223d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
52323d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
5240f14d02fSMax Shvetsov 
525033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
526033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
527033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
528033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
52952696946SOlivier Deprez 		return -EINVAL;
5300f14d02fSMax Shvetsov 	}
5310f14d02fSMax Shvetsov 
5320f14d02fSMax Shvetsov 	/*
5330f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
5340f14d02fSMax Shvetsov 	 * is required for SPM
5350f14d02fSMax Shvetsov 	 */
536623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
53752696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
53852696946SOlivier Deprez 		return -EINVAL;
5390f14d02fSMax Shvetsov 	}
540033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
5410f14d02fSMax Shvetsov 
5420f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
5430f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
54452696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
5450f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
5460f14d02fSMax Shvetsov 	}
5470f14d02fSMax Shvetsov 
5480f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
5490f14d02fSMax Shvetsov 
5500f14d02fSMax Shvetsov 	/*
55152696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
55252696946SOlivier Deprez 	 * manifest.
5530f14d02fSMax Shvetsov 	 */
5540f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
5550f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
5560f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
5570f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
5580f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5590f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5600f14d02fSMax Shvetsov 	} else {
561033039f8SMax Shvetsov 
562033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
563033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
564033039f8SMax Shvetsov #else
565033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
566033039f8SMax Shvetsov #endif
567033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5680f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5690f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5700f14d02fSMax Shvetsov 	}
5710f14d02fSMax Shvetsov 
5720cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5730cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5740cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5750cea2ae0SManish V Badarkhe 
5760cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5770cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5780cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5790cea2ae0SManish V Badarkhe 		return -EINVAL;
5800cea2ae0SManish V Badarkhe 	}
5810cea2ae0SManish V Badarkhe 
5820cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5830cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5840cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5850cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5860cea2ae0SManish V Badarkhe 
5870cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5880cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5890cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5900cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5910cea2ae0SManish V Badarkhe 
592*9f3f4d87SBoyan Karatotev 	spmd_setup_context(plat_my_core_pos());
5930f14d02fSMax Shvetsov 
594a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
595a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
596a334c4e6SOlivier Deprez 
5970f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5980f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
5990f14d02fSMax Shvetsov 
600f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
601f2dcf418SOlivier Deprez 
6028cb99c3fSOlivier Deprez 	/*
6038cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
6048cb99c3fSOlivier Deprez 	 * while the NWd is running.
6058cb99c3fSOlivier Deprez 	 */
6068cb99c3fSOlivier Deprez 	flags = 0;
6078cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
6088cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
6098cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
6108cb99c3fSOlivier Deprez 					     flags);
6118cb99c3fSOlivier Deprez 	if (rc != 0) {
6128cb99c3fSOlivier Deprez 		panic();
6138cb99c3fSOlivier Deprez 	}
6148cb99c3fSOlivier Deprez 
615a1e0e871SMadhukar Pappireddy 	/*
616bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
617bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
618bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
619bb6d0a17SOlivier Deprez 	 *
620bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
621bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
622bb6d0a17SOlivier Deprez 	 *
623bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
624bb6d0a17SOlivier Deprez 	 *
625bb6d0a17SOlivier Deprez 	 */
626bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
627bb6d0a17SOlivier Deprez 	/*
628fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
629fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
630fca5f0ebSMadhukar Pappireddy 	 * running.
631a1e0e871SMadhukar Pappireddy 	 */
632fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
633a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
634a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
635a1e0e871SMadhukar Pappireddy 						     flags);
636a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
637a1e0e871SMadhukar Pappireddy 			panic();
638a1e0e871SMadhukar Pappireddy 		}
639fca5f0ebSMadhukar Pappireddy 	}
640bb6d0a17SOlivier Deprez #endif
641bb6d0a17SOlivier Deprez 
6420f14d02fSMax Shvetsov 	return 0;
6430f14d02fSMax Shvetsov }
6440f14d02fSMax Shvetsov 
6450f14d02fSMax Shvetsov /*******************************************************************************
64652696946SOlivier Deprez  * Initialize context of SPM Core.
647bdd2596dSAchin Gupta  ******************************************************************************/
6480f14d02fSMax Shvetsov int spmd_setup(void)
649bdd2596dSAchin Gupta {
650bdd2596dSAchin Gupta 	int rc;
6516da76075SMarc Bonnici 	void *spmc_manifest;
65255fd56d7SYeoreum Yun 	struct transfer_list_header *tl __maybe_unused;
65355fd56d7SYeoreum Yun 	struct transfer_list_entry *te __maybe_unused;
6546da76075SMarc Bonnici 
6556da76075SMarc Bonnici 	/*
6566da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6576da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6586da76075SMarc Bonnici 	 */
6596da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6606da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6616da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6626da76075SMarc Bonnici 
6636da76075SMarc Bonnici 		rc = spmc_setup();
6646da76075SMarc Bonnici 		if (rc != 0) {
6650d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6666da76075SMarc Bonnici 		}
6670d33649eSOlivier Deprez 		return 0;
6686da76075SMarc Bonnici 	}
669bdd2596dSAchin Gupta 
670bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
67152696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
67252696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6730d33649eSOlivier Deprez 		return 0;
674bdd2596dSAchin Gupta 	}
675bdd2596dSAchin Gupta 
676bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
67752696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
678bdd2596dSAchin Gupta 
67955fd56d7SYeoreum Yun 
68055fd56d7SYeoreum Yun #if TRANSFER_LIST && !RESET_TO_BL31
68155fd56d7SYeoreum Yun 	tl = (struct transfer_list_header *)spmc_ep_info->args.arg3;
68255fd56d7SYeoreum Yun 	te = transfer_list_find(tl, TL_TAG_DT_SPMC_MANIFEST);
68355fd56d7SYeoreum Yun 	if (te == NULL) {
68455fd56d7SYeoreum Yun 		WARN("SPM Core manifest absent in TRANSFER_LIST.\n");
68555fd56d7SYeoreum Yun 		return -ENOENT;
68655fd56d7SYeoreum Yun 	}
68755fd56d7SYeoreum Yun 
68855fd56d7SYeoreum Yun 	spmc_manifest = (void *)transfer_list_entry_data(te);
68955fd56d7SYeoreum Yun 
69055fd56d7SYeoreum Yun 	/* Change the DT in the handoff */
69155fd56d7SYeoreum Yun 	if (sizeof(spmc_ep_info->args.arg0) == sizeof(uint64_t)) {
69255fd56d7SYeoreum Yun 		spmc_ep_info->args.arg0 = (uintptr_t)spmc_manifest;
69355fd56d7SYeoreum Yun 	} else {
69455fd56d7SYeoreum Yun 		spmc_ep_info->args.arg3 = (uintptr_t)spmc_manifest;
69555fd56d7SYeoreum Yun 	}
69655fd56d7SYeoreum Yun #else
697bdd2596dSAchin Gupta 	/*
698bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
69952696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
700bdd2596dSAchin Gupta 	 */
70123d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
70255fd56d7SYeoreum Yun #endif
70355fd56d7SYeoreum Yun 
70423d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
7050d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
7060d33649eSOlivier Deprez 		return 0;
707bdd2596dSAchin Gupta 	}
708bdd2596dSAchin Gupta 
7090f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
71023d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
7110f14d02fSMax Shvetsov 	if (rc != 0) {
71252696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
713bdd2596dSAchin Gupta 	}
714bdd2596dSAchin Gupta 
7150d33649eSOlivier Deprez 	return 0;
7160f14d02fSMax Shvetsov }
7170f14d02fSMax Shvetsov 
7180f14d02fSMax Shvetsov /*******************************************************************************
719bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
7200f14d02fSMax Shvetsov  ******************************************************************************/
721bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
72252696946SOlivier Deprez 			       bool secure_origin,
72352696946SOlivier Deprez 			       uint64_t x1,
72452696946SOlivier Deprez 			       uint64_t x2,
72552696946SOlivier Deprez 			       uint64_t x3,
72652696946SOlivier Deprez 			       uint64_t x4,
727c925867eSOlivier Deprez 			       void *handle,
728bb9fc8c0SJay Monkman 			       uint64_t flags,
729bb9fc8c0SJay Monkman 			       uint32_t secure_ffa_version)
7300f14d02fSMax Shvetsov {
731c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
732c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
733bb9fc8c0SJay Monkman 	uint32_t version_in = (secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
734bb9fc8c0SJay Monkman 	uint32_t version_out = (!secure_origin) ? secure_ffa_version : nonsecure_ffa_version;
735107e3cc0SOlivier Deprez 	void *ctx_out;
73693ff138bSOlivier Deprez 
737c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
738c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
739c925867eSOlivier Deprez 		/*
740c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
741c925867eSOlivier Deprez 		 * if it was set by the caller.
742c925867eSOlivier Deprez 		 */
743c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
744c925867eSOlivier Deprez 	}
745c925867eSOlivier Deprez #endif
746c925867eSOlivier Deprez 
7470f14d02fSMax Shvetsov 	/* Save incoming security state */
748033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
74993ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
750678ce223SOlivier Deprez #else
751678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
75259bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
75359bdcc58SMadhukar Pappireddy 	/* Forward the hint bit denoting the absence of SVE live state. */
75459bdcc58SMadhukar Pappireddy 	simd_ctx_save(secure_state_in, (!secure_origin && (is_sve_hint_set(flags) == true)));
75559bdcc58SMadhukar Pappireddy #endif
756033039f8SMax Shvetsov #endif
7570f14d02fSMax Shvetsov 
7580f14d02fSMax Shvetsov 	/* Restore outgoing security state */
759033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
76093ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
761678ce223SOlivier Deprez #else
762678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
76359bdcc58SMadhukar Pappireddy #if CTX_INCLUDE_FPREGS || CTX_INCLUDE_SVE_REGS
76459bdcc58SMadhukar Pappireddy 	simd_ctx_restore(secure_state_out);
76559bdcc58SMadhukar Pappireddy #endif
766033039f8SMax Shvetsov #endif
76793ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
7680f14d02fSMax Shvetsov 
769107e3cc0SOlivier Deprez 	ctx_out = cm_get_context(secure_state_out);
770a0a7f158SAndrei Homescu 	if (smc_fid == FFA_NORMAL_WORLD_RESUME) {
771a0a7f158SAndrei Homescu 		SMC_RET0(ctx_out);
772a0a7f158SAndrei Homescu 	}
773a0a7f158SAndrei Homescu 
774bb9fc8c0SJay Monkman 	if ((GET_SMC_CC(smc_fid) == SMC_64) && (version_out >= MAKE_FFA_VERSION(U(1), U(2)))) {
775bb9fc8c0SJay Monkman 		if (version_in < MAKE_FFA_VERSION(U(1), U(2))) {
776bb9fc8c0SJay Monkman 			/* FFA version mismatch, with dest >= 1.2 - set outgoing x8-x17 to zero */
777bb9fc8c0SJay Monkman 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
778bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X5),
779bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X6),
780bb9fc8c0SJay Monkman 				  SMC_GET_GP(handle, CTX_GPREG_X7),
781bb9fc8c0SJay Monkman 				  0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
782bb9fc8c0SJay Monkman 		} else {
783bb9fc8c0SJay Monkman 			/* Both FFA versions >= 1.2 - pass incoming x8-x17 to dest */
784107e3cc0SOlivier Deprez 			SMC_RET18(ctx_out, smc_fid, x1, x2, x3, x4,
785eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X5),
786eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X6),
787eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X7),
788eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X8),
789eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X9),
790eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X10),
791eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X11),
792eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X12),
793eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X13),
794eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X14),
795eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X15),
796eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X16),
797eaaf517cSRaghu Krishnamurthy 				  SMC_GET_GP(handle, CTX_GPREG_X17)
798eaaf517cSRaghu Krishnamurthy 				);
799bb9fc8c0SJay Monkman 		}
800bb9fc8c0SJay Monkman 	} else {
801bb9fc8c0SJay Monkman 		/* 32 bit call or dest has FFA version < 1.2 or unknown */
802107e3cc0SOlivier Deprez 		SMC_RET8(ctx_out, smc_fid, x1, x2, x3, x4,
8030f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X5),
8040f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X6),
8050f14d02fSMax Shvetsov 			 SMC_GET_GP(handle, CTX_GPREG_X7));
806bb9fc8c0SJay Monkman 	}
8070f14d02fSMax Shvetsov }
8080f14d02fSMax Shvetsov 
8090f14d02fSMax Shvetsov /*******************************************************************************
810bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
811bb01a673SMarc Bonnici  ******************************************************************************/
812bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
813bb01a673SMarc Bonnici 				 bool secure_origin,
814bb01a673SMarc Bonnici 				 uint64_t x1,
815bb01a673SMarc Bonnici 				 uint64_t x2,
816bb01a673SMarc Bonnici 				 uint64_t x3,
817bb01a673SMarc Bonnici 				 uint64_t x4,
818bb01a673SMarc Bonnici 				 void *cookie,
819bb01a673SMarc Bonnici 				 void *handle,
820bb9fc8c0SJay Monkman 				 uint64_t flags,
821bb9fc8c0SJay Monkman 				 uint32_t secure_ffa_version)
822bb01a673SMarc Bonnici {
823bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
824bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
825bb01a673SMarc Bonnici 					cookie, handle, flags);
826bb01a673SMarc Bonnici 	}
827c925867eSOlivier Deprez 
828bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
829bb9fc8c0SJay Monkman 				     handle, flags, secure_ffa_version);
830bb01a673SMarc Bonnici 
831bb01a673SMarc Bonnici }
832bb01a673SMarc Bonnici 
833bb01a673SMarc Bonnici /*******************************************************************************
834662af36dSJ-Alves  * Return FFA_ERROR with specified error code
8350f14d02fSMax Shvetsov  ******************************************************************************/
83695f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
8370f14d02fSMax Shvetsov {
838e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
839e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
840662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
841662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
842bdd2596dSAchin Gupta }
843bdd2596dSAchin Gupta 
844f0d743dbSOlivier Deprez /*******************************************************************************
845f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
846f0d743dbSOlivier Deprez  ******************************************************************************/
847f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
848f0d743dbSOlivier Deprez {
849f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
850f0d743dbSOlivier Deprez 
851f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
852f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
853f0d743dbSOlivier Deprez }
854f0d743dbSOlivier Deprez 
855c2901419SOlivier Deprez /******************************************************************************
856c2901419SOlivier Deprez  * spmd_is_spmc_message
857c2901419SOlivier Deprez  *****************************************************************************/
858c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
859c2901419SOlivier Deprez {
860bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
861bb01a673SMarc Bonnici 		return false;
862bb01a673SMarc Bonnici 	}
863bb01a673SMarc Bonnici 
864c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
865c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
866c2901419SOlivier Deprez }
867c2901419SOlivier Deprez 
868bdd2596dSAchin Gupta /*******************************************************************************
869bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
870bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
871bb01a673SMarc Bonnici  ******************************************************************************/
872bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
873bb01a673SMarc Bonnici 			      uint64_t x1,
874bb01a673SMarc Bonnici 			      uint64_t x2,
875bb01a673SMarc Bonnici 			      uint64_t x3,
876bb01a673SMarc Bonnici 			      uint64_t x4,
877bb01a673SMarc Bonnici 			      void *cookie,
878bb01a673SMarc Bonnici 			      void *handle,
879bb01a673SMarc Bonnici 			      uint64_t flags)
880bb01a673SMarc Bonnici {
881bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
882bb01a673SMarc Bonnici 		/*
883bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
884bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
885bb01a673SMarc Bonnici 		 */
886bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
887bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
888bb01a673SMarc Bonnici 						is_caller_secure(flags),
889bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
890bb01a673SMarc Bonnici 						handle, flags);
891bb01a673SMarc Bonnici 		}
892bb01a673SMarc Bonnici 	}
893bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
894bb9fc8c0SJay Monkman 				handle, flags, spmc_nwd_ffa_version);
895bb9fc8c0SJay Monkman }
896bb9fc8c0SJay Monkman 
897bb9fc8c0SJay Monkman static uint32_t get_common_ffa_version(uint32_t secure_ffa_version)
898bb9fc8c0SJay Monkman {
899bb9fc8c0SJay Monkman 	if (secure_ffa_version <= nonsecure_ffa_version) {
900bb9fc8c0SJay Monkman 		return secure_ffa_version;
901bb9fc8c0SJay Monkman 	} else {
902bb9fc8c0SJay Monkman 		return nonsecure_ffa_version;
903bb9fc8c0SJay Monkman 	}
904bb01a673SMarc Bonnici }
905bb01a673SMarc Bonnici 
906bb01a673SMarc Bonnici /*******************************************************************************
907662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
908bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
909bdd2596dSAchin Gupta  ******************************************************************************/
91052696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
91152696946SOlivier Deprez 			  uint64_t x1,
91252696946SOlivier Deprez 			  uint64_t x2,
91352696946SOlivier Deprez 			  uint64_t x3,
91452696946SOlivier Deprez 			  uint64_t x4,
91552696946SOlivier Deprez 			  void *cookie,
91652696946SOlivier Deprez 			  void *handle,
917bb9fc8c0SJay Monkman 			  uint64_t flags,
918bb9fc8c0SJay Monkman 			  uint32_t secure_ffa_version)
919bdd2596dSAchin Gupta {
92052696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
92193ff138bSOlivier Deprez 	bool secure_origin;
9226873088cSJ-Alves 	int ret;
9234388f28fSJ-Alves 	uint32_t input_version;
924bdd2596dSAchin Gupta 
925bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
92693ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
927bdd2596dSAchin Gupta 
9284ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
9294ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
930eac8077aSOlivier Deprez 		    plat_my_core_pos(), smc_fid, x1, x2, x3, x4,
931cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
932bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
933bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
934bdd2596dSAchin Gupta 
9350b850e9eSRaghu Krishnamurthy 	/*
9360b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
9370b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
9380b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
9390b850e9eSRaghu Krishnamurthy 	 */
9400b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
9410b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
9420b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
9430b850e9eSRaghu Krishnamurthy 	}
9440b850e9eSRaghu Krishnamurthy 
945bb9fc8c0SJay Monkman 	if ((!secure_origin) && (smc_fid != FFA_VERSION)) {
946bb9fc8c0SJay Monkman 		/*
947bb9fc8c0SJay Monkman 		 * Once the caller invokes any FF-A ABI other than FFA_VERSION,
948bb9fc8c0SJay Monkman 		 * the version negotiation phase is complete.
949bb9fc8c0SJay Monkman 		 */
950bb9fc8c0SJay Monkman 		nonsecure_version_negotiated = true;
951bb9fc8c0SJay Monkman 	}
952bb9fc8c0SJay Monkman 
953bdd2596dSAchin Gupta 	switch (smc_fid) {
954662af36dSJ-Alves 	case FFA_ERROR:
955bdd2596dSAchin Gupta 		/*
956bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
95752696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
958bdd2596dSAchin Gupta 		 * unsuccessfully.
959bdd2596dSAchin Gupta 		 */
9609dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
961bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
9620f14d02fSMax Shvetsov 		}
963bdd2596dSAchin Gupta 
96466bdfd6eSRaghu Krishnamurthy 		/*
9658723eaf2SMadhukar Pappireddy 		 * Perform a synchronous exit:
9668723eaf2SMadhukar Pappireddy 		 * 1. If there was an SPMD logical partition direct request on-going,
96766bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
96866bdfd6eSRaghu Krishnamurthy 		 * consumed.
9698723eaf2SMadhukar Pappireddy 		 * 2. SPMC sent FFA_ERROR in response to a power management
9708723eaf2SMadhukar Pappireddy 		 * operation sent through direct request.
97166bdfd6eSRaghu Krishnamurthy 		 */
9728723eaf2SMadhukar Pappireddy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx) ||
9738723eaf2SMadhukar Pappireddy 		    ctx->psci_operation_ongoing) {
97466bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
97566bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
97666bdfd6eSRaghu Krishnamurthy 		}
97766bdfd6eSRaghu Krishnamurthy 
97893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
979bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
980bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
981bdd2596dSAchin Gupta 		break; /* not reached */
982bdd2596dSAchin Gupta 
983662af36dSJ-Alves 	case FFA_VERSION:
9844388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
985bdd2596dSAchin Gupta 		/*
9864388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
9874388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
9884388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
989bb9fc8c0SJay Monkman 		 * forward to the EL3 SPMC if enabled, otherwise send a
990bb9fc8c0SJay Monkman 		 * framework message to the SPMC at the lower EL to
991bb9fc8c0SJay Monkman 		 * negotiate a version that is compatible between the
992bb9fc8c0SJay Monkman 		 * normal world and the SPMC.
9934388f28fSJ-Alves 		 * Sanity check to "input_version".
994bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
995bb01a673SMarc Bonnici 		 * this is not used.
996bdd2596dSAchin Gupta 		 */
9974388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
998bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
9994388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
10004388f28fSJ-Alves 		} else if (!secure_origin) {
1001bb9fc8c0SJay Monkman 			if (!nonsecure_version_negotiated) {
1002bb9fc8c0SJay Monkman 				/*
1003bb9fc8c0SJay Monkman 				 * Once an FF-A version has been negotiated
1004bb9fc8c0SJay Monkman 				 * between a caller and a callee, the version
1005bb9fc8c0SJay Monkman 				 * may not be changed for the lifetime of
1006bb9fc8c0SJay Monkman 				 * the calling component.
1007bb9fc8c0SJay Monkman 				 */
1008bb9fc8c0SJay Monkman 				nonsecure_ffa_version = input_version;
1009bb9fc8c0SJay Monkman 			}
1010bb9fc8c0SJay Monkman 
10119576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
10129576fa93SMarc Bonnici 				/*
10139576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
10149576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
10159576fa93SMarc Bonnici 				 * a direct request.
10169576fa93SMarc Bonnici 				 */
1017bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version =
1018bb9fc8c0SJay Monkman 					MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR);
1019bb9fc8c0SJay Monkman 				return spmc_smc_handler(smc_fid, secure_origin,
10209576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
10219576fa93SMarc Bonnici 							handle, flags);
10229576fa93SMarc Bonnici 			}
10239576fa93SMarc Bonnici 
10249944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
10259944f557SDaniel Boulby 			uint64_t rc;
10269944f557SDaniel Boulby 
10279944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
10289944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
1029e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
1030e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
1031bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10329944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
10339944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10349944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
10359944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10369944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
10379944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
10389944f557SDaniel Boulby 				break;
10399944f557SDaniel Boulby 			}
10409944f557SDaniel Boulby 			/* Save non-secure system registers context */
10419944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
10429944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
10432d960a11SMadhukar Pappireddy #else
10442d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
10459944f557SDaniel Boulby #endif
10469944f557SDaniel Boulby 
10479944f557SDaniel Boulby 			/*
10489944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
10499944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
10509944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
10519944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
10529944f557SDaniel Boulby 			 */
10539944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
10549944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
10559944f557SDaniel Boulby 						input_version);
10569944f557SDaniel Boulby 
105776d53ee1SOlivier Deprez 			/*
105876d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
105976d53ee1SOlivier Deprez 			 * from the SPMC.
106076d53ee1SOlivier Deprez 			 */
106176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
106276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
106376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
106476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
106576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
106676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
106776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
106876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
106976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
107076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
107176d53ee1SOlivier Deprez 
10729944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
10739944f557SDaniel Boulby 
10749944f557SDaniel Boulby 			if ((rc != 0ULL) ||
10759944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
10769944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
10779944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
107859bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
10799944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
10809944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
10819944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
10829944f557SDaniel Boulby 			} else {
10839944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
1084bb9fc8c0SJay Monkman 				spmc_nwd_ffa_version = (uint32_t)ret;
10859944f557SDaniel Boulby 			}
10869944f557SDaniel Boulby 
10879944f557SDaniel Boulby 			/*
108876d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
108976d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
109076d53ee1SOlivier Deprez 			 */
109176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
109276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
109376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
109476d53ee1SOlivier Deprez 
109576d53ee1SOlivier Deprez 			/*
10969944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
10979944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
10989944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
10999944f557SDaniel Boulby 			 */
11009944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
11019944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1102bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
1103bb9fc8c0SJay Monkman 						flags, spmc_nwd_ffa_version);
11044388f28fSJ-Alves 		} else {
1105e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
1106e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
11074388f28fSJ-Alves 		}
11084388f28fSJ-Alves 
1109e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1110e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1111e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1112bdd2596dSAchin Gupta 		break; /* not reached */
1113bdd2596dSAchin Gupta 
1114662af36dSJ-Alves 	case FFA_FEATURES:
1115bdd2596dSAchin Gupta 		/*
1116bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
111752696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1118bdd2596dSAchin Gupta 		 */
1119bdd2596dSAchin Gupta 
112052696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
112193ff138bSOlivier Deprez 		if (!secure_origin) {
112293ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1123bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1124bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
112552696946SOlivier Deprez 		}
112652696946SOlivier Deprez 
1127bdd2596dSAchin Gupta 		/*
1128bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1129662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1130bdd2596dSAchin Gupta 		 * nop.
1131bdd2596dSAchin Gupta 		 */
1132662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1133bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1134bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1135bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
11360f14d02fSMax Shvetsov 
1137bdd2596dSAchin Gupta 		break; /* not reached */
1138bdd2596dSAchin Gupta 
1139662af36dSJ-Alves 	case FFA_ID_GET:
1140ac03ac5eSMax Shvetsov 		/*
1141662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1142ac03ac5eSMax Shvetsov 		 */
1143ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1144662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1145662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1146662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1147662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1148662af36dSJ-Alves 				 FFA_PARAM_MBZ);
114952696946SOlivier Deprez 		}
115052696946SOlivier Deprez 
1151662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1152662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1153662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1154662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1155662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1156ac03ac5eSMax Shvetsov 
1157ac03ac5eSMax Shvetsov 		break; /* not reached */
1158ac03ac5eSMax Shvetsov 
1159cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1160cdb49d47SOlivier Deprez 		if (secure_origin) {
1161cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1162cdb49d47SOlivier Deprez 
1163cdb49d47SOlivier Deprez 			if (ret < 0) {
1164cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1165cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1166cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1167cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1168cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1169cdb49d47SOlivier Deprez 			} else {
1170cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1171cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1172cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1173cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1174cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1175cdb49d47SOlivier Deprez 			}
1176cdb49d47SOlivier Deprez 		}
1177cdb49d47SOlivier Deprez 
1178cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1179cdb49d47SOlivier Deprez 		break; /* Not reached */
1180cdb49d47SOlivier Deprez 
118170c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
118270c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
118370c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
118470c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
118570c121a2SDaniel Boulby 		}
118670c121a2SDaniel Boulby 		/*
118770c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
118870c121a2SDaniel Boulby 		 * instance where this function is invoked
118970c121a2SDaniel Boulby 		 */
119070c121a2SDaniel Boulby 		if (!secure_origin) {
119170c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
119270c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
119370c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119470c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
119570c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
119670c121a2SDaniel Boulby 		}
119770c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
119870c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
119970c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
120070c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
120170c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
120270c121a2SDaniel Boulby 
120370c121a2SDaniel Boulby 		break; /* not reached */
120470c121a2SDaniel Boulby 
1205bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1206bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1207bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1208bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1209bb9fc8c0SJay Monkman 		}
1210bb9fc8c0SJay Monkman 		/* fallthrough */
1211f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
12125519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
121366bdfd6eSRaghu Krishnamurthy 		/*
121466bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
121566bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
121666bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
121766bdfd6eSRaghu Krishnamurthy 		 */
121866bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
121966bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
122066bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
122166bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
122266bdfd6eSRaghu Krishnamurthy 						     );
122366bdfd6eSRaghu Krishnamurthy 		}
122466bdfd6eSRaghu Krishnamurthy 
122566bdfd6eSRaghu Krishnamurthy 		/*
122666bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
122766bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
122866bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
122966bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
123066bdfd6eSRaghu Krishnamurthy 		 * error codes.
123166bdfd6eSRaghu Krishnamurthy 		 */
123266bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
123366bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
123466bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
123566bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
123666bdfd6eSRaghu Krishnamurthy 		}
123766bdfd6eSRaghu Krishnamurthy 
12385519f07cSShruti 		if (!secure_origin) {
12395519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
12405519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
12415519f07cSShruti 				return spmd_ffa_error_return(handle,
12425519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
12435519f07cSShruti 			}
12445519f07cSShruti 		}
1245f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1246cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
12476c378c2fSKathleen Capella 						FFA_ERROR_DENIED);
1248cc6047b3SKathleen Capella 		} else {
1249cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1250cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1251cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1252bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1253cc6047b3SKathleen Capella 		}
1254cc6047b3SKathleen Capella 		break; /* Not reached */
1255cc6047b3SKathleen Capella 
1256bb9fc8c0SJay Monkman 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
1257bb9fc8c0SJay Monkman 		if (get_common_ffa_version(secure_ffa_version) < MAKE_FFA_VERSION(U(1), U(2))) {
1258bb9fc8c0SJay Monkman 			/* Call not supported at this version */
1259bb9fc8c0SJay Monkman 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1260bb9fc8c0SJay Monkman 		}
1261bb9fc8c0SJay Monkman 		/* fallthrough */
1262f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
126366bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
126466bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
126566bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
12668cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1267f0d743dbSOlivier Deprez 		} else {
1268f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1269f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1270bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1271bb9fc8c0SJay Monkman 						handle, flags, secure_ffa_version);
1272f0d743dbSOlivier Deprez 		}
1273f0d743dbSOlivier Deprez 		break; /* Not reached */
1274662af36dSJ-Alves 	case FFA_RX_RELEASE:
1275662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1276662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1277662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1278545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1279fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1280fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1281fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1282fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1283fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1284fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1285fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1286fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1287fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1288c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1289d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
129008f9ba5bSJ-Alves 	case FFA_NS_RES_INFO_GET_SMC64:
1291fc3f4800SJ-Alves #endif
1292662af36dSJ-Alves 	case FFA_MSG_RUN:
1293c2eba07cSFederico Recanati 		/*
1294c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1295c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1296c2eba07cSFederico Recanati 		 */
129793ff138bSOlivier Deprez 		if (secure_origin) {
1298662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1299662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1300bdd2596dSAchin Gupta 		}
1301bdd2596dSAchin Gupta 
1302e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1303e138400dSBoyan Karatotev 		/* fallthrough */
1304662af36dSJ-Alves 	case FFA_MSG_SEND:
1305662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1306662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1307662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1308662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1309662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1310662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1311662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1312662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1313662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1314662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1315662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1316642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1317642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1318662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1319662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1320bdd2596dSAchin Gupta 		/*
132166bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
132266bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1323bdd2596dSAchin Gupta 		 */
132466bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
132566bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
132666bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
132766bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
132866bdfd6eSRaghu Krishnamurthy 		}
1329bdd2596dSAchin Gupta 
133093ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1331bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1332bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1333bdd2596dSAchin Gupta 		break; /* not reached */
1334bdd2596dSAchin Gupta 
1335662af36dSJ-Alves 	case FFA_MSG_WAIT:
1336bdd2596dSAchin Gupta 		/*
1337bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1338bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
133952696946SOlivier Deprez 		 * SPM Core initialised successfully.
1340bdd2596dSAchin Gupta 		 */
13419dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
13428cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1343bdd2596dSAchin Gupta 		}
1344bdd2596dSAchin Gupta 
1345e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1346e138400dSBoyan Karatotev 		/* fallthrough */
1347386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1348662af36dSJ-Alves 	case FFA_MSG_YIELD:
1349bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
135093ff138bSOlivier Deprez 		if (!secure_origin) {
1351662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1352662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1353bdd2596dSAchin Gupta 		}
1354bdd2596dSAchin Gupta 
135566bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
135666bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
135766bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
135866bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
135966bdfd6eSRaghu Krishnamurthy 		}
136066bdfd6eSRaghu Krishnamurthy 
136193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1362bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1363bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1364bdd2596dSAchin Gupta 		break; /* not reached */
1365bdd2596dSAchin Gupta 
13668cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
13678cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
13688cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
13698cb99c3fSOlivier Deprez 		} else {
13708cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
13718cb99c3fSOlivier Deprez 		}
13728cb99c3fSOlivier Deprez 		break; /* Not reached */
1373eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1374eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1375eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
137695f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
137795f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1378eaaf517cSRaghu Krishnamurthy 		}
13798cb99c3fSOlivier Deprez 
1380eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1381eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1382eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1383eaaf517cSRaghu Krishnamurthy 		}
1384eaaf517cSRaghu Krishnamurthy 
1385eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1386eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1387bb9fc8c0SJay Monkman 					handle, flags, secure_ffa_version);
1388eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1389eaaf517cSRaghu Krishnamurthy #endif
1390638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1391638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1392638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1393638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1394638a6f8eSShruti Gupta 		break; /* not reached */
1395638a6f8eSShruti Gupta 
13966671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13976671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13986671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
13996671b3d8SMadhukar Pappireddy 		} else {
14006c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
14016671b3d8SMadhukar Pappireddy 		}
1402b3dcd505SMadhukar Pappireddy 	case FFA_ABORT_SMC32:
1403b3dcd505SMadhukar Pappireddy 	case FFA_ABORT_SMC64:
1404b3dcd505SMadhukar Pappireddy 		/* This interface must be invoked only by the Secure world */
1405b3dcd505SMadhukar Pappireddy 		if (!secure_origin) {
1406b3dcd505SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1407b3dcd505SMadhukar Pappireddy 		}
1408b3dcd505SMadhukar Pappireddy 
1409b3dcd505SMadhukar Pappireddy 		ERROR("SPMC encountered a fatal error. Aborting now\n");
1410b3dcd505SMadhukar Pappireddy 		panic();
1411b3dcd505SMadhukar Pappireddy 
1412b3dcd505SMadhukar Pappireddy 		/* Not reached. */
1413b3dcd505SMadhukar Pappireddy 		SMC_RET0(handle);
1414bdd2596dSAchin Gupta 	default:
1415bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1416662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1417bdd2596dSAchin Gupta 	}
1418bdd2596dSAchin Gupta }
1419