xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision c8cea3b8a864473bce9f89efc3021092135609c3)
1bdd2596dSAchin Gupta /*
2cc6047b3SKathleen Capella  * Copyright (c) 2020-2024, 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 /*******************************************************************************
490f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
500f14d02fSMax Shvetsov  * on secondary cores.
510f14d02fSMax Shvetsov  ******************************************************************************/
520f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
530f14d02fSMax Shvetsov 
540f14d02fSMax Shvetsov /*******************************************************************************
5552696946SOlivier Deprez  * SPM Core context on current CPU get helper.
5652696946SOlivier Deprez  ******************************************************************************/
5752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
5852696946SOlivier Deprez {
59*c8cea3b8SOlivier Deprez 	return &spm_core_context[plat_my_core_pos()];
6052696946SOlivier Deprez }
6152696946SOlivier Deprez 
6252696946SOlivier Deprez /*******************************************************************************
63a92bc73bSOlivier Deprez  * SPM Core ID getter.
64a92bc73bSOlivier Deprez  ******************************************************************************/
65a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
66a92bc73bSOlivier Deprez {
67a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
68a92bc73bSOlivier Deprez }
69a92bc73bSOlivier Deprez 
70a92bc73bSOlivier Deprez /*******************************************************************************
710f14d02fSMax Shvetsov  * Static function declaration.
720f14d02fSMax Shvetsov  ******************************************************************************/
730f14d02fSMax Shvetsov static int32_t spmd_init(void);
7423d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
7595f7f6d8SRaghu Krishnamurthy 
7652696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
7752696946SOlivier Deprez 				 bool secure_origin,
7852696946SOlivier Deprez 				 uint64_t x1,
7952696946SOlivier Deprez 				 uint64_t x2,
8052696946SOlivier Deprez 				 uint64_t x3,
8152696946SOlivier Deprez 				 uint64_t x4,
82bb01a673SMarc Bonnici 				 void *cookie,
83bb01a673SMarc Bonnici 				 void *handle,
84bb01a673SMarc Bonnici 				 uint64_t flags);
85bdd2596dSAchin Gupta 
869944f557SDaniel Boulby /******************************************************************************
879944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
889944f557SDaniel Boulby  *****************************************************************************/
899944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
909944f557SDaniel Boulby 			     unsigned long long message)
919944f557SDaniel Boulby {
929944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
939944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
949944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
959944f557SDaniel Boulby 		 spmd_spmc_id_get());
969944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
979944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
9876d53ee1SOlivier Deprez 
9976d53ee1SOlivier Deprez 	/* Zero out x4-x7 for the direct request emitted towards the SPMC. */
10076d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
10176d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
10276d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
10376d53ee1SOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
1049944f557SDaniel Boulby }
1059944f557SDaniel Boulby 
1069944f557SDaniel Boulby 
107bdd2596dSAchin Gupta /*******************************************************************************
10852696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
10952696946SOlivier Deprez  * SPMC entry.
110bdd2596dSAchin Gupta  ******************************************************************************/
111bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
112bdd2596dSAchin Gupta {
113bdd2596dSAchin Gupta 	uint64_t rc;
114bdd2596dSAchin Gupta 
115bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
116bdd2596dSAchin Gupta 
117bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
118bdd2596dSAchin Gupta 
119bdd2596dSAchin Gupta 	/* Restore the context assigned above */
120033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
12128f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
122678ce223SOlivier Deprez #else
123678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
124033039f8SMax Shvetsov #endif
125bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
126bdd2596dSAchin Gupta 
127033039f8SMax Shvetsov 	/* Enter SPMC */
128bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
129bdd2596dSAchin Gupta 
130bdd2596dSAchin Gupta 	/* Save secure state */
131033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
13228f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
133678ce223SOlivier Deprez #else
134678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
135033039f8SMax Shvetsov #endif
136bdd2596dSAchin Gupta 
137bdd2596dSAchin Gupta 	return rc;
138bdd2596dSAchin Gupta }
139bdd2596dSAchin Gupta 
140bdd2596dSAchin Gupta /*******************************************************************************
14152696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
142bdd2596dSAchin Gupta  * called originally.
143bdd2596dSAchin Gupta  ******************************************************************************/
144bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
145bdd2596dSAchin Gupta {
14652696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
147bdd2596dSAchin Gupta 
14852696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
149bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
150bdd2596dSAchin Gupta 
151bdd2596dSAchin Gupta 	/*
152bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
153bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
154bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
155bdd2596dSAchin Gupta 	 */
156bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
157bdd2596dSAchin Gupta 
158bdd2596dSAchin Gupta 	panic();
159bdd2596dSAchin Gupta }
160bdd2596dSAchin Gupta 
161bdd2596dSAchin Gupta /*******************************************************************************
16252696946SOlivier Deprez  * Jump to the SPM Core for the first time.
163bdd2596dSAchin Gupta  ******************************************************************************/
164bdd2596dSAchin Gupta static int32_t spmd_init(void)
165bdd2596dSAchin Gupta {
16652696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
16752696946SOlivier Deprez 	uint64_t rc;
168bdd2596dSAchin Gupta 
16952696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1709dcf63ddSOlivier Deprez 
171f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
172f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
173bdd2596dSAchin Gupta 
174bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
17552696946SOlivier Deprez 	if (rc != 0ULL) {
1764ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
17752696946SOlivier Deprez 		return 0;
178bdd2596dSAchin Gupta 	}
179bdd2596dSAchin Gupta 
1809dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
1819dcf63ddSOlivier Deprez 
18252696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
183bdd2596dSAchin Gupta 
184890b5088SRaghu Krishnamurthy 	spmd_logical_sp_set_spmc_initialized();
185890b5088SRaghu Krishnamurthy 	rc = spmd_logical_sp_init();
186890b5088SRaghu Krishnamurthy 	if (rc != 0) {
187890b5088SRaghu Krishnamurthy 		WARN("SPMD Logical partitions failed init.\n");
188890b5088SRaghu Krishnamurthy 	}
189890b5088SRaghu Krishnamurthy 
190bdd2596dSAchin Gupta 	return 1;
191bdd2596dSAchin Gupta }
192bdd2596dSAchin Gupta 
193bdd2596dSAchin Gupta /*******************************************************************************
1948cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
1958cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
1968cb99c3fSOlivier Deprez  * itself or a Secure Partition.
1978cb99c3fSOlivier Deprez  ******************************************************************************/
1988cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
1998cb99c3fSOlivier Deprez 					      uint32_t flags,
2008cb99c3fSOlivier Deprez 					      void *handle,
2018cb99c3fSOlivier Deprez 					      void *cookie)
2028cb99c3fSOlivier Deprez {
2038cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2048cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2058cb99c3fSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
2068cb99c3fSOlivier Deprez 	int64_t rc;
2078cb99c3fSOlivier Deprez 
2088cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2098cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2108cb99c3fSOlivier Deprez 
2118cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2128cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2138cb99c3fSOlivier Deprez 
2148cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2158cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2168cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2172d960a11SMadhukar Pappireddy #else
2182d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_save(NON_SECURE);
2198cb99c3fSOlivier Deprez #endif
2208cb99c3fSOlivier Deprez 
2218cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2228cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2238cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2248cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2258cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2268cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2278cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2288cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2298cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2308cb99c3fSOlivier Deprez 
2318cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2328cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2338cb99c3fSOlivier Deprez 
2348cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
2358cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
2360c23e6f4SOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
2378cb99c3fSOlivier Deprez 	}
2388cb99c3fSOlivier Deprez 
2398cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2408cb99c3fSOlivier Deprez 
2418cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2428cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2432d960a11SMadhukar Pappireddy #else
2442d960a11SMadhukar Pappireddy 	cm_el1_sysregs_context_restore(NON_SECURE);
2458cb99c3fSOlivier Deprez #endif
2468cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2478cb99c3fSOlivier Deprez 
2488cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2498cb99c3fSOlivier Deprez }
2508cb99c3fSOlivier Deprez 
251bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
252a1e0e871SMadhukar Pappireddy /*******************************************************************************
253a1e0e871SMadhukar Pappireddy  * spmd_group0_interrupt_handler_nwd
254a1e0e871SMadhukar Pappireddy  * Group0 secure interrupt in the normal world are trapped to EL3. Delegate the
255a1e0e871SMadhukar Pappireddy  * handling of the interrupt to the platform handler, and return only upon
256a1e0e871SMadhukar Pappireddy  * successfully handling the Group0 interrupt.
257a1e0e871SMadhukar Pappireddy  ******************************************************************************/
258a1e0e871SMadhukar Pappireddy static uint64_t spmd_group0_interrupt_handler_nwd(uint32_t id,
259a1e0e871SMadhukar Pappireddy 						  uint32_t flags,
260a1e0e871SMadhukar Pappireddy 						  void *handle,
261a1e0e871SMadhukar Pappireddy 						  void *cookie)
262a1e0e871SMadhukar Pappireddy {
263a1e0e871SMadhukar Pappireddy 	uint32_t intid;
264a1e0e871SMadhukar Pappireddy 
265a1e0e871SMadhukar Pappireddy 	/* Sanity check the security state when the exception was generated. */
266a1e0e871SMadhukar Pappireddy 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
267a1e0e871SMadhukar Pappireddy 
268a1e0e871SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context. */
269a1e0e871SMadhukar Pappireddy 	assert(handle == cm_get_context(NON_SECURE));
270a1e0e871SMadhukar Pappireddy 
271a1e0e871SMadhukar Pappireddy 	assert(id == INTR_ID_UNAVAILABLE);
272a1e0e871SMadhukar Pappireddy 
273a1e0e871SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
274a1e0e871SMadhukar Pappireddy 
2756c91fc44SMadhukar Pappireddy 	intid = plat_ic_acknowledge_interrupt();
276a1e0e871SMadhukar Pappireddy 
277a1e0e871SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
278a1e0e871SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
279a1e0e871SMadhukar Pappireddy 		panic();
280a1e0e871SMadhukar Pappireddy 	}
281a1e0e871SMadhukar Pappireddy 
2826c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
2836c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
2846c91fc44SMadhukar Pappireddy 
285a1e0e871SMadhukar Pappireddy 	return 0U;
286a1e0e871SMadhukar Pappireddy }
287bb6d0a17SOlivier Deprez #endif
288a1e0e871SMadhukar Pappireddy 
2896671b3d8SMadhukar Pappireddy /*******************************************************************************
2906671b3d8SMadhukar Pappireddy  * spmd_handle_group0_intr_swd
2916671b3d8SMadhukar Pappireddy  * SPMC delegates handling of Group0 secure interrupt to EL3 firmware using
2926671b3d8SMadhukar Pappireddy  * FFA_EL3_INTR_HANDLE SMC call. Further, SPMD delegates the handling of the
2936671b3d8SMadhukar Pappireddy  * interrupt to the platform handler, and returns only upon successfully
2946671b3d8SMadhukar Pappireddy  * handling the Group0 interrupt.
2956671b3d8SMadhukar Pappireddy  ******************************************************************************/
2966671b3d8SMadhukar Pappireddy static uint64_t spmd_handle_group0_intr_swd(void *handle)
2976671b3d8SMadhukar Pappireddy {
2986671b3d8SMadhukar Pappireddy 	uint32_t intid;
2996671b3d8SMadhukar Pappireddy 
3006671b3d8SMadhukar Pappireddy 	/* Sanity check the pointer to this cpu's context */
3016671b3d8SMadhukar Pappireddy 	assert(handle == cm_get_context(SECURE));
3026671b3d8SMadhukar Pappireddy 
3036671b3d8SMadhukar Pappireddy 	assert(plat_ic_get_pending_interrupt_type() == INTR_TYPE_EL3);
3046671b3d8SMadhukar Pappireddy 
3056c91fc44SMadhukar Pappireddy 	intid = plat_ic_acknowledge_interrupt();
3066671b3d8SMadhukar Pappireddy 
3076671b3d8SMadhukar Pappireddy 	/*
3086671b3d8SMadhukar Pappireddy 	 * TODO: Currently due to a limitation in SPMD implementation, the
3096671b3d8SMadhukar Pappireddy 	 * platform handler is expected to not delegate handling to NWd while
3106671b3d8SMadhukar Pappireddy 	 * processing Group0 secure interrupt.
3116671b3d8SMadhukar Pappireddy 	 */
3126671b3d8SMadhukar Pappireddy 	if (plat_spmd_handle_group0_interrupt(intid) < 0) {
3136671b3d8SMadhukar Pappireddy 		/* Group0 interrupt was not handled by the platform. */
3146671b3d8SMadhukar Pappireddy 		ERROR("Group0 interrupt %u not handled\n", intid);
3156671b3d8SMadhukar Pappireddy 		panic();
3166671b3d8SMadhukar Pappireddy 	}
3176671b3d8SMadhukar Pappireddy 
3186c91fc44SMadhukar Pappireddy 	/* Deactivate the corresponding Group0 interrupt. */
3196c91fc44SMadhukar Pappireddy 	plat_ic_end_of_interrupt(intid);
3206c91fc44SMadhukar Pappireddy 
3216671b3d8SMadhukar Pappireddy 	/* Return success. */
3226671b3d8SMadhukar Pappireddy 	SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3236671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
3246671b3d8SMadhukar Pappireddy 		 FFA_PARAM_MBZ);
3256671b3d8SMadhukar Pappireddy }
3266671b3d8SMadhukar Pappireddy 
3270cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
3280cea2ae0SManish V Badarkhe static int spmd_dynamic_map_mem(uintptr_t base_addr, size_t size,
3290cea2ae0SManish V Badarkhe 				 unsigned int attr, uintptr_t *align_addr,
3300cea2ae0SManish V Badarkhe 				 size_t *align_size)
3310cea2ae0SManish V Badarkhe {
3320cea2ae0SManish V Badarkhe 	uintptr_t base_addr_align;
3330cea2ae0SManish V Badarkhe 	size_t mapped_size_align;
3340cea2ae0SManish V Badarkhe 	int rc;
3350cea2ae0SManish V Badarkhe 
3360cea2ae0SManish V Badarkhe 	/* Page aligned address and size if necessary */
3370cea2ae0SManish V Badarkhe 	base_addr_align = page_align(base_addr, DOWN);
3380cea2ae0SManish V Badarkhe 	mapped_size_align = page_align(size, UP);
3390cea2ae0SManish V Badarkhe 
3400cea2ae0SManish V Badarkhe 	if ((base_addr != base_addr_align) &&
3410cea2ae0SManish V Badarkhe 	    (size == mapped_size_align)) {
3420cea2ae0SManish V Badarkhe 		mapped_size_align += PAGE_SIZE;
3430cea2ae0SManish V Badarkhe 	}
3440cea2ae0SManish V Badarkhe 
3450cea2ae0SManish V Badarkhe 	/*
3460cea2ae0SManish V Badarkhe 	 * Map dynamically given region with its aligned base address and
3470cea2ae0SManish V Badarkhe 	 * size
3480cea2ae0SManish V Badarkhe 	 */
3490cea2ae0SManish V Badarkhe 	rc = mmap_add_dynamic_region((unsigned long long)base_addr_align,
3500cea2ae0SManish V Badarkhe 				     base_addr_align,
3510cea2ae0SManish V Badarkhe 				     mapped_size_align,
3520cea2ae0SManish V Badarkhe 				     attr);
3530cea2ae0SManish V Badarkhe 	if (rc == 0) {
3540cea2ae0SManish V Badarkhe 		*align_addr = base_addr_align;
3550cea2ae0SManish V Badarkhe 		*align_size = mapped_size_align;
3560cea2ae0SManish V Badarkhe 	}
3570cea2ae0SManish V Badarkhe 
3580cea2ae0SManish V Badarkhe 	return rc;
3590cea2ae0SManish V Badarkhe }
3600cea2ae0SManish V Badarkhe 
3610cea2ae0SManish V Badarkhe static void spmd_do_sec_cpy(uintptr_t root_base_addr, uintptr_t sec_base_addr,
3620cea2ae0SManish V Badarkhe 			    size_t size)
3630cea2ae0SManish V Badarkhe {
3640cea2ae0SManish V Badarkhe 	uintptr_t root_base_addr_align, sec_base_addr_align;
3650cea2ae0SManish V Badarkhe 	size_t root_mapped_size_align, sec_mapped_size_align;
3660cea2ae0SManish V Badarkhe 	int rc;
3670cea2ae0SManish V Badarkhe 
3680cea2ae0SManish V Badarkhe 	assert(root_base_addr != 0UL);
3690cea2ae0SManish V Badarkhe 	assert(sec_base_addr != 0UL);
3700cea2ae0SManish V Badarkhe 	assert(size != 0UL);
3710cea2ae0SManish V Badarkhe 
3720cea2ae0SManish V Badarkhe 	/* Map the memory with required attributes */
3730cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(root_base_addr, size, MT_RO_DATA | MT_ROOT,
3740cea2ae0SManish V Badarkhe 				  &root_base_addr_align,
3750cea2ae0SManish V Badarkhe 				  &root_mapped_size_align);
3760cea2ae0SManish V Badarkhe 	if (rc != 0) {
3770cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping", "root region",
3780cea2ae0SManish V Badarkhe 		      root_base_addr, rc);
3790cea2ae0SManish V Badarkhe 		panic();
3800cea2ae0SManish V Badarkhe 	}
3810cea2ae0SManish V Badarkhe 
3820cea2ae0SManish V Badarkhe 	rc = spmd_dynamic_map_mem(sec_base_addr, size, MT_RW_DATA | MT_SECURE,
3830cea2ae0SManish V Badarkhe 				  &sec_base_addr_align, &sec_mapped_size_align);
3840cea2ae0SManish V Badarkhe 	if (rc != 0) {
3850cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while mapping",
3860cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr, rc);
3870cea2ae0SManish V Badarkhe 		panic();
3880cea2ae0SManish V Badarkhe 	}
3890cea2ae0SManish V Badarkhe 
3900cea2ae0SManish V Badarkhe 	/* Do copy operation */
3910cea2ae0SManish V Badarkhe 	(void)memcpy((void *)sec_base_addr, (void *)root_base_addr, size);
3920cea2ae0SManish V Badarkhe 
3930cea2ae0SManish V Badarkhe 	/* Unmap root memory region */
3940cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(root_base_addr_align,
3950cea2ae0SManish V Badarkhe 					root_mapped_size_align);
3960cea2ae0SManish V Badarkhe 	if (rc != 0) {
3970cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
3980cea2ae0SManish V Badarkhe 		      "root region", root_base_addr_align, rc);
3990cea2ae0SManish V Badarkhe 		panic();
4000cea2ae0SManish V Badarkhe 	}
4010cea2ae0SManish V Badarkhe 
4020cea2ae0SManish V Badarkhe 	/* Unmap secure memory region */
4030cea2ae0SManish V Badarkhe 	rc = mmap_remove_dynamic_region(sec_base_addr_align,
4040cea2ae0SManish V Badarkhe 					sec_mapped_size_align);
4050cea2ae0SManish V Badarkhe 	if (rc != 0) {
4060cea2ae0SManish V Badarkhe 		ERROR("%s %s %lu (%d)\n", "Error while unmapping",
4070cea2ae0SManish V Badarkhe 		      "secure region", sec_base_addr_align, rc);
4080cea2ae0SManish V Badarkhe 		panic();
4090cea2ae0SManish V Badarkhe 	}
4100cea2ae0SManish V Badarkhe }
4110cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
4120cea2ae0SManish V Badarkhe 
4138cb99c3fSOlivier Deprez /*******************************************************************************
41452696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
4150f14d02fSMax Shvetsov  ******************************************************************************/
41623d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
4170f14d02fSMax Shvetsov {
418f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
419f2dcf418SOlivier Deprez 	unsigned int core_id;
4208cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
42152696946SOlivier Deprez 	int rc;
4220cea2ae0SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *image_info __unused;
4230f14d02fSMax Shvetsov 
42452696946SOlivier Deprez 	/* Load the SPM Core manifest */
42523d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
4260f14d02fSMax Shvetsov 	if (rc != 0) {
42752696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
42852696946SOlivier Deprez 		return rc;
4290f14d02fSMax Shvetsov 	}
4300f14d02fSMax Shvetsov 
4310f14d02fSMax Shvetsov 	/*
43252696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
43352696946SOlivier Deprez 	 * Dispatcher version.
4340f14d02fSMax Shvetsov 	 */
435662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
436662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
437662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
4380f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
43952696946SOlivier Deprez 		return -EINVAL;
4400f14d02fSMax Shvetsov 	}
4410f14d02fSMax Shvetsov 
442662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
4430f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
4440f14d02fSMax Shvetsov 
44552696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
446033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
4470f14d02fSMax Shvetsov 
448ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
44952696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
45052696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
45152696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
45252696946SOlivier Deprez 		return -EINVAL;
453ac03ac5eSMax Shvetsov 	}
454ac03ac5eSMax Shvetsov 
45552696946SOlivier Deprez 	/* Validate the SPM Core execution state */
4560f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
4570f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
45823d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
4590f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
46052696946SOlivier Deprez 		return -EINVAL;
4610f14d02fSMax Shvetsov 	}
4620f14d02fSMax Shvetsov 
46323d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
46423d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
4650f14d02fSMax Shvetsov 
466033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
467033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
468033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
469033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
47052696946SOlivier Deprez 		return -EINVAL;
4710f14d02fSMax Shvetsov 	}
4720f14d02fSMax Shvetsov 
4730f14d02fSMax Shvetsov 	/*
4740f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
4750f14d02fSMax Shvetsov 	 * is required for SPM
4760f14d02fSMax Shvetsov 	 */
477623f6140SAndre Przywara 	if (!is_feat_sel2_supported()) {
47852696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
47952696946SOlivier Deprez 		return -EINVAL;
4800f14d02fSMax Shvetsov 	}
481033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
4820f14d02fSMax Shvetsov 
4830f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
4840f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
48552696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
4860f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
4870f14d02fSMax Shvetsov 	}
4880f14d02fSMax Shvetsov 
4890f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
4900f14d02fSMax Shvetsov 
4910f14d02fSMax Shvetsov 	/*
49252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
49352696946SOlivier Deprez 	 * manifest.
4940f14d02fSMax Shvetsov 	 */
4950f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
4960f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
4970f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
4980f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
4990f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
5000f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
5010f14d02fSMax Shvetsov 	} else {
502033039f8SMax Shvetsov 
503033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
504033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
505033039f8SMax Shvetsov #else
506033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
507033039f8SMax Shvetsov #endif
508033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
5090f14d02fSMax Shvetsov 					     MODE_SP_ELX,
5100f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
5110f14d02fSMax Shvetsov 	}
5120f14d02fSMax Shvetsov 
5130cea2ae0SManish V Badarkhe #if ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31
5140cea2ae0SManish V Badarkhe 	image_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TOS_FW_CONFIG_ID);
5150cea2ae0SManish V Badarkhe 	assert(image_info != NULL);
5160cea2ae0SManish V Badarkhe 
5170cea2ae0SManish V Badarkhe 	if ((image_info->config_addr == 0UL) ||
5180cea2ae0SManish V Badarkhe 	    (image_info->secondary_config_addr == 0UL) ||
5190cea2ae0SManish V Badarkhe 	    (image_info->config_max_size == 0UL)) {
5200cea2ae0SManish V Badarkhe 		return -EINVAL;
5210cea2ae0SManish V Badarkhe 	}
5220cea2ae0SManish V Badarkhe 
5230cea2ae0SManish V Badarkhe 	/* Copy manifest from root->secure region */
5240cea2ae0SManish V Badarkhe 	spmd_do_sec_cpy(image_info->config_addr,
5250cea2ae0SManish V Badarkhe 			image_info->secondary_config_addr,
5260cea2ae0SManish V Badarkhe 			image_info->config_max_size);
5270cea2ae0SManish V Badarkhe 
5280cea2ae0SManish V Badarkhe 	/* Update ep info of BL32 */
5290cea2ae0SManish V Badarkhe 	assert(spmc_ep_info != NULL);
5300cea2ae0SManish V Badarkhe 	spmc_ep_info->args.arg0 = image_info->secondary_config_addr;
5310cea2ae0SManish V Badarkhe #endif /* ENABLE_RME && SPMD_SPM_AT_SEL2 && !RESET_TO_BL31 */
5320cea2ae0SManish V Badarkhe 
533f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
534f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
535f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
5360f14d02fSMax Shvetsov 
537f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
538f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
539f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
5400f14d02fSMax Shvetsov 
541f2dcf418SOlivier Deprez 		/*
542f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
543f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
544f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
545f2dcf418SOlivier Deprez 		 */
546f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
547f2dcf418SOlivier Deprez 	}
5480f14d02fSMax Shvetsov 
549a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
550a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
551a334c4e6SOlivier Deprez 
5520f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
5530f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
5540f14d02fSMax Shvetsov 
555f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
556f2dcf418SOlivier Deprez 
5578cb99c3fSOlivier Deprez 	/*
5588cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
5598cb99c3fSOlivier Deprez 	 * while the NWd is running.
5608cb99c3fSOlivier Deprez 	 */
5618cb99c3fSOlivier Deprez 	flags = 0;
5628cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
5638cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
5648cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
5658cb99c3fSOlivier Deprez 					     flags);
5668cb99c3fSOlivier Deprez 	if (rc != 0) {
5678cb99c3fSOlivier Deprez 		panic();
5688cb99c3fSOlivier Deprez 	}
5698cb99c3fSOlivier Deprez 
570a1e0e871SMadhukar Pappireddy 	/*
571bb6d0a17SOlivier Deprez 	 * Permit configurations where the SPM resides at S-EL1/2 and upon a
572bb6d0a17SOlivier Deprez 	 * Group0 interrupt triggering while the normal world runs, the
573bb6d0a17SOlivier Deprez 	 * interrupt is routed either through the EHF or directly to the SPMD:
574bb6d0a17SOlivier Deprez 	 *
575bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=0: the Group0 interrupt is routed to the SPMD
576bb6d0a17SOlivier Deprez 	 *                   for handling by spmd_group0_interrupt_handler_nwd.
577bb6d0a17SOlivier Deprez 	 *
578bb6d0a17SOlivier Deprez 	 * EL3_EXCEPTION_HANDLING=1: the Group0 interrupt is routed to the EHF.
579bb6d0a17SOlivier Deprez 	 *
580bb6d0a17SOlivier Deprez 	 */
581bb6d0a17SOlivier Deprez #if (EL3_EXCEPTION_HANDLING == 0)
582bb6d0a17SOlivier Deprez 	/*
583fca5f0ebSMadhukar Pappireddy 	 * If EL3 interrupts are supported by the platform, register an
584fca5f0ebSMadhukar Pappireddy 	 * interrupt handler routing Group0 interrupts to SPMD while the NWd is
585fca5f0ebSMadhukar Pappireddy 	 * running.
586a1e0e871SMadhukar Pappireddy 	 */
587fca5f0ebSMadhukar Pappireddy 	if (plat_ic_has_interrupt_type(INTR_TYPE_EL3)) {
588a1e0e871SMadhukar Pappireddy 		rc = register_interrupt_type_handler(INTR_TYPE_EL3,
589a1e0e871SMadhukar Pappireddy 						     spmd_group0_interrupt_handler_nwd,
590a1e0e871SMadhukar Pappireddy 						     flags);
591a1e0e871SMadhukar Pappireddy 		if (rc != 0) {
592a1e0e871SMadhukar Pappireddy 			panic();
593a1e0e871SMadhukar Pappireddy 		}
594fca5f0ebSMadhukar Pappireddy 	}
595bb6d0a17SOlivier Deprez #endif
596bb6d0a17SOlivier Deprez 
5970f14d02fSMax Shvetsov 	return 0;
5980f14d02fSMax Shvetsov }
5990f14d02fSMax Shvetsov 
6000f14d02fSMax Shvetsov /*******************************************************************************
60152696946SOlivier Deprez  * Initialize context of SPM Core.
602bdd2596dSAchin Gupta  ******************************************************************************/
6030f14d02fSMax Shvetsov int spmd_setup(void)
604bdd2596dSAchin Gupta {
605bdd2596dSAchin Gupta 	int rc;
6066da76075SMarc Bonnici 	void *spmc_manifest;
6076da76075SMarc Bonnici 
6086da76075SMarc Bonnici 	/*
6096da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
6106da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
6116da76075SMarc Bonnici 	 */
6126da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
6136da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
6146da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
6156da76075SMarc Bonnici 
6166da76075SMarc Bonnici 		rc = spmc_setup();
6176da76075SMarc Bonnici 		if (rc != 0) {
6180d33649eSOlivier Deprez 			WARN("SPMC initialisation failed 0x%x.\n", rc);
6196da76075SMarc Bonnici 		}
6200d33649eSOlivier Deprez 		return 0;
6216da76075SMarc Bonnici 	}
622bdd2596dSAchin Gupta 
623bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
62452696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
62552696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
6260d33649eSOlivier Deprez 		return 0;
627bdd2596dSAchin Gupta 	}
628bdd2596dSAchin Gupta 
629bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
63052696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
631bdd2596dSAchin Gupta 
632bdd2596dSAchin Gupta 	/*
633bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
63452696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
635bdd2596dSAchin Gupta 	 */
63623d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
63723d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
6380d33649eSOlivier Deprez 		WARN("Invalid or absent SPM Core manifest.\n");
6390d33649eSOlivier Deprez 		return 0;
640bdd2596dSAchin Gupta 	}
641bdd2596dSAchin Gupta 
6420f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
64323d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
6440f14d02fSMax Shvetsov 	if (rc != 0) {
64552696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
646bdd2596dSAchin Gupta 	}
647bdd2596dSAchin Gupta 
6480d33649eSOlivier Deprez 	return 0;
6490f14d02fSMax Shvetsov }
6500f14d02fSMax Shvetsov 
6510f14d02fSMax Shvetsov /*******************************************************************************
652bb01a673SMarc Bonnici  * Forward FF-A SMCs to the other security state.
6530f14d02fSMax Shvetsov  ******************************************************************************/
654bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid,
65552696946SOlivier Deprez 			       bool secure_origin,
65652696946SOlivier Deprez 			       uint64_t x1,
65752696946SOlivier Deprez 			       uint64_t x2,
65852696946SOlivier Deprez 			       uint64_t x3,
65952696946SOlivier Deprez 			       uint64_t x4,
660c925867eSOlivier Deprez 			       void *handle,
661c925867eSOlivier Deprez 			       uint64_t flags)
6620f14d02fSMax Shvetsov {
663c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
664c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
66593ff138bSOlivier Deprez 
666c925867eSOlivier Deprez #if SPMD_SPM_AT_SEL2
667c925867eSOlivier Deprez 	if ((secure_state_out == SECURE) && (is_sve_hint_set(flags) == true)) {
668c925867eSOlivier Deprez 		/*
669c925867eSOlivier Deprez 		 * Set the SVE hint bit in x0 and pass to the lower secure EL,
670c925867eSOlivier Deprez 		 * if it was set by the caller.
671c925867eSOlivier Deprez 		 */
672c925867eSOlivier Deprez 		smc_fid |= (FUNCID_SVE_HINT_MASK << FUNCID_SVE_HINT_SHIFT);
673c925867eSOlivier Deprez 	}
674c925867eSOlivier Deprez #endif
675c925867eSOlivier Deprez 
6760f14d02fSMax Shvetsov 	/* Save incoming security state */
677033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
67893ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
679678ce223SOlivier Deprez #else
680678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
681033039f8SMax Shvetsov #endif
6820f14d02fSMax Shvetsov 
6830f14d02fSMax Shvetsov 	/* Restore outgoing security state */
684033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
68593ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
686678ce223SOlivier Deprez #else
687678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
688033039f8SMax Shvetsov #endif
68993ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
6900f14d02fSMax Shvetsov 
691eaaf517cSRaghu Krishnamurthy #if SPMD_SPM_AT_SEL2
692eaaf517cSRaghu Krishnamurthy 	/*
693eaaf517cSRaghu Krishnamurthy 	 * If SPMC is at SEL2, save additional registers x8-x17, which may
694eaaf517cSRaghu Krishnamurthy 	 * be used in FF-A calls such as FFA_PARTITION_INFO_GET_REGS.
695eaaf517cSRaghu Krishnamurthy 	 * Note that technically, all SPMCs can support this, but this code is
696eaaf517cSRaghu Krishnamurthy 	 * under ifdef to minimize breakage in case other SPMCs do not save
697eaaf517cSRaghu Krishnamurthy 	 * and restore x8-x17.
698eaaf517cSRaghu Krishnamurthy 	 * We also need to pass through these registers since not all FF-A ABIs
699eaaf517cSRaghu Krishnamurthy 	 * modify x8-x17, in which case, SMCCC requires that these registers be
700eaaf517cSRaghu Krishnamurthy 	 * preserved, so the SPMD passes through these registers and expects the
701eaaf517cSRaghu Krishnamurthy 	 * SPMC to save and restore (potentially also modify) them.
702eaaf517cSRaghu Krishnamurthy 	 */
703eaaf517cSRaghu Krishnamurthy 	SMC_RET18(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
704eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X5),
705eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X6),
706eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X7),
707eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X8),
708eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X9),
709eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X10),
710eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X11),
711eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X12),
712eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X13),
713eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X14),
714eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X15),
715eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X16),
716eaaf517cSRaghu Krishnamurthy 			SMC_GET_GP(handle, CTX_GPREG_X17)
717eaaf517cSRaghu Krishnamurthy 			);
718eaaf517cSRaghu Krishnamurthy 
719eaaf517cSRaghu Krishnamurthy #else
72093ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
7210f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
7220f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
7230f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
724eaaf517cSRaghu Krishnamurthy #endif
7250f14d02fSMax Shvetsov }
7260f14d02fSMax Shvetsov 
7270f14d02fSMax Shvetsov /*******************************************************************************
728bb01a673SMarc Bonnici  * Forward SMCs to the other security state.
729bb01a673SMarc Bonnici  ******************************************************************************/
730bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid,
731bb01a673SMarc Bonnici 				 bool secure_origin,
732bb01a673SMarc Bonnici 				 uint64_t x1,
733bb01a673SMarc Bonnici 				 uint64_t x2,
734bb01a673SMarc Bonnici 				 uint64_t x3,
735bb01a673SMarc Bonnici 				 uint64_t x4,
736bb01a673SMarc Bonnici 				 void *cookie,
737bb01a673SMarc Bonnici 				 void *handle,
738bb01a673SMarc Bonnici 				 uint64_t flags)
739bb01a673SMarc Bonnici {
740bb01a673SMarc Bonnici 	if (is_spmc_at_el3() && !secure_origin) {
741bb01a673SMarc Bonnici 		return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4,
742bb01a673SMarc Bonnici 					cookie, handle, flags);
743bb01a673SMarc Bonnici 	}
744c925867eSOlivier Deprez 
745bb01a673SMarc Bonnici 	return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4,
746c925867eSOlivier Deprez 				     handle, flags);
747bb01a673SMarc Bonnici 
748bb01a673SMarc Bonnici }
749bb01a673SMarc Bonnici 
750bb01a673SMarc Bonnici /*******************************************************************************
751662af36dSJ-Alves  * Return FFA_ERROR with specified error code
7520f14d02fSMax Shvetsov  ******************************************************************************/
75395f7f6d8SRaghu Krishnamurthy uint64_t spmd_ffa_error_return(void *handle, int error_code)
7540f14d02fSMax Shvetsov {
755e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
756e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
757662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
758662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
759bdd2596dSAchin Gupta }
760bdd2596dSAchin Gupta 
761f0d743dbSOlivier Deprez /*******************************************************************************
762f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
763f0d743dbSOlivier Deprez  ******************************************************************************/
764f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
765f0d743dbSOlivier Deprez {
766f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
767f0d743dbSOlivier Deprez 
768f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
769f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
770f0d743dbSOlivier Deprez }
771f0d743dbSOlivier Deprez 
772c2901419SOlivier Deprez /******************************************************************************
773c2901419SOlivier Deprez  * spmd_is_spmc_message
774c2901419SOlivier Deprez  *****************************************************************************/
775c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
776c2901419SOlivier Deprez {
777bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
778bb01a673SMarc Bonnici 		return false;
779bb01a673SMarc Bonnici 	}
780bb01a673SMarc Bonnici 
781c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
782c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
783c2901419SOlivier Deprez }
784c2901419SOlivier Deprez 
785f0d743dbSOlivier Deprez /******************************************************************************
786f0d743dbSOlivier Deprez  * spmd_handle_spmc_message
787f0d743dbSOlivier Deprez  *****************************************************************************/
788a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg,
789a92bc73bSOlivier Deprez 		unsigned long long parm1, unsigned long long parm2,
790a92bc73bSOlivier Deprez 		unsigned long long parm3, unsigned long long parm4)
791f0d743dbSOlivier Deprez {
792f0d743dbSOlivier Deprez 	VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
793f0d743dbSOlivier Deprez 		msg, parm1, parm2, parm3, parm4);
794f0d743dbSOlivier Deprez 
795f0d743dbSOlivier Deprez 	return -EINVAL;
796f0d743dbSOlivier Deprez }
797f0d743dbSOlivier Deprez 
798bdd2596dSAchin Gupta /*******************************************************************************
799bb01a673SMarc Bonnici  * This function forwards FF-A SMCs to either the main SPMD handler or the
800bb01a673SMarc Bonnici  * SPMC at EL3, depending on the origin security state, if enabled.
801bb01a673SMarc Bonnici  ******************************************************************************/
802bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid,
803bb01a673SMarc Bonnici 			      uint64_t x1,
804bb01a673SMarc Bonnici 			      uint64_t x2,
805bb01a673SMarc Bonnici 			      uint64_t x3,
806bb01a673SMarc Bonnici 			      uint64_t x4,
807bb01a673SMarc Bonnici 			      void *cookie,
808bb01a673SMarc Bonnici 			      void *handle,
809bb01a673SMarc Bonnici 			      uint64_t flags)
810bb01a673SMarc Bonnici {
811bb01a673SMarc Bonnici 	if (is_spmc_at_el3()) {
812bb01a673SMarc Bonnici 		/*
813bb01a673SMarc Bonnici 		 * If we have an SPMC at EL3 allow handling of the SMC first.
814bb01a673SMarc Bonnici 		 * The SPMC will call back through to SPMD handler if required.
815bb01a673SMarc Bonnici 		 */
816bb01a673SMarc Bonnici 		if (is_caller_secure(flags)) {
817bb01a673SMarc Bonnici 			return spmc_smc_handler(smc_fid,
818bb01a673SMarc Bonnici 						is_caller_secure(flags),
819bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
820bb01a673SMarc Bonnici 						handle, flags);
821bb01a673SMarc Bonnici 		}
822bb01a673SMarc Bonnici 	}
823bb01a673SMarc Bonnici 	return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
824bb01a673SMarc Bonnici 				handle, flags);
825bb01a673SMarc Bonnici }
826bb01a673SMarc Bonnici 
827bb01a673SMarc Bonnici /*******************************************************************************
828662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
829bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
830bdd2596dSAchin Gupta  ******************************************************************************/
83152696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
83252696946SOlivier Deprez 			  uint64_t x1,
83352696946SOlivier Deprez 			  uint64_t x2,
83452696946SOlivier Deprez 			  uint64_t x3,
83552696946SOlivier Deprez 			  uint64_t x4,
83652696946SOlivier Deprez 			  void *cookie,
83752696946SOlivier Deprez 			  void *handle,
838bdd2596dSAchin Gupta 			  uint64_t flags)
839bdd2596dSAchin Gupta {
840cdb49d47SOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
84152696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
84293ff138bSOlivier Deprez 	bool secure_origin;
8436873088cSJ-Alves 	int ret;
8444388f28fSJ-Alves 	uint32_t input_version;
845bdd2596dSAchin Gupta 
846bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
84793ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
848bdd2596dSAchin Gupta 
8494ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
8504ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
851cdb49d47SOlivier Deprez 		    linear_id, smc_fid, x1, x2, x3, x4,
852cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
853bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
854bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
855bdd2596dSAchin Gupta 
8560b850e9eSRaghu Krishnamurthy 	/*
8570b850e9eSRaghu Krishnamurthy 	 * If there is an on-going info regs from EL3 SPMD LP, unconditionally
8580b850e9eSRaghu Krishnamurthy 	 * return, we don't expect any other FF-A ABIs to be called between
8590b850e9eSRaghu Krishnamurthy 	 * calls to FFA_PARTITION_INFO_GET_REGS.
8600b850e9eSRaghu Krishnamurthy 	 */
8610b850e9eSRaghu Krishnamurthy 	if (is_spmd_logical_sp_info_regs_req_in_progress(ctx)) {
8620b850e9eSRaghu Krishnamurthy 		assert(secure_origin);
8630b850e9eSRaghu Krishnamurthy 		spmd_spm_core_sync_exit(0ULL);
8640b850e9eSRaghu Krishnamurthy 	}
8650b850e9eSRaghu Krishnamurthy 
866bdd2596dSAchin Gupta 	switch (smc_fid) {
867662af36dSJ-Alves 	case FFA_ERROR:
868bdd2596dSAchin Gupta 		/*
869bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
87052696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
871bdd2596dSAchin Gupta 		 * unsuccessfully.
872bdd2596dSAchin Gupta 		 */
8739dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
874bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
8750f14d02fSMax Shvetsov 		}
876bdd2596dSAchin Gupta 
87766bdfd6eSRaghu Krishnamurthy 		/*
87866bdfd6eSRaghu Krishnamurthy 		 * If there was an SPMD logical partition direct request on-going,
87966bdfd6eSRaghu Krishnamurthy 		 * return back to the SPMD logical partition so the error can be
88066bdfd6eSRaghu Krishnamurthy 		 * consumed.
88166bdfd6eSRaghu Krishnamurthy 		 */
88266bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
88366bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
88466bdfd6eSRaghu Krishnamurthy 			spmd_spm_core_sync_exit(0ULL);
88566bdfd6eSRaghu Krishnamurthy 		}
88666bdfd6eSRaghu Krishnamurthy 
88793ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
888bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
889bb01a673SMarc Bonnici 					handle, flags);
890bdd2596dSAchin Gupta 		break; /* not reached */
891bdd2596dSAchin Gupta 
892662af36dSJ-Alves 	case FFA_VERSION:
8934388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
894bdd2596dSAchin Gupta 		/*
8954388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
8964388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
8974388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
8989576fa93SMarc Bonnici 		 * forward to the EL3 SPMC if enabled, otherwise return
8999576fa93SMarc Bonnici 		 * the SPMC version if implemented at a lower EL.
9004388f28fSJ-Alves 		 * Sanity check to "input_version".
901bb01a673SMarc Bonnici 		 * If the EL3 SPMC is enabled, ignore the SPMC state as
902bb01a673SMarc Bonnici 		 * this is not used.
903bdd2596dSAchin Gupta 		 */
9044388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
905bb01a673SMarc Bonnici 		    (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) {
9064388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
9074388f28fSJ-Alves 		} else if (!secure_origin) {
9089576fa93SMarc Bonnici 			if (is_spmc_at_el3()) {
9099576fa93SMarc Bonnici 				/*
9109576fa93SMarc Bonnici 				 * Forward the call directly to the EL3 SPMC, if
9119576fa93SMarc Bonnici 				 * enabled, as we don't need to wrap the call in
9129576fa93SMarc Bonnici 				 * a direct request.
9139576fa93SMarc Bonnici 				 */
9149576fa93SMarc Bonnici 				return spmd_smc_forward(smc_fid, secure_origin,
9159576fa93SMarc Bonnici 							x1, x2, x3, x4, cookie,
9169576fa93SMarc Bonnici 							handle, flags);
9179576fa93SMarc Bonnici 			}
9189576fa93SMarc Bonnici 
9199944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
9209944f557SDaniel Boulby 			uint64_t rc;
9219944f557SDaniel Boulby 
9229944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
9239944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
924e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
925e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
9269944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
9279944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
9289944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
9299944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
9309944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
9319944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
9329944f557SDaniel Boulby 				break;
9339944f557SDaniel Boulby 			}
9349944f557SDaniel Boulby 			/* Save non-secure system registers context */
9359944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
9369944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
9372d960a11SMadhukar Pappireddy #else
9382d960a11SMadhukar Pappireddy 			cm_el1_sysregs_context_save(NON_SECURE);
9399944f557SDaniel Boulby #endif
9409944f557SDaniel Boulby 
9419944f557SDaniel Boulby 			/*
9429944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
9439944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
9449944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
9459944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
9469944f557SDaniel Boulby 			 */
9479944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
9489944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
9499944f557SDaniel Boulby 						input_version);
9509944f557SDaniel Boulby 
95176d53ee1SOlivier Deprez 			/*
95276d53ee1SOlivier Deprez 			 * Ensure x8-x17 NS GP register values are untouched when returning
95376d53ee1SOlivier Deprez 			 * from the SPMC.
95476d53ee1SOlivier Deprez 			 */
95576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X8, SMC_GET_GP(handle, CTX_GPREG_X8));
95676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X9, SMC_GET_GP(handle, CTX_GPREG_X9));
95776d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X10, SMC_GET_GP(handle, CTX_GPREG_X10));
95876d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X11, SMC_GET_GP(handle, CTX_GPREG_X11));
95976d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X12, SMC_GET_GP(handle, CTX_GPREG_X12));
96076d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X13, SMC_GET_GP(handle, CTX_GPREG_X13));
96176d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X14, SMC_GET_GP(handle, CTX_GPREG_X14));
96276d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X15, SMC_GET_GP(handle, CTX_GPREG_X15));
96376d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X16, SMC_GET_GP(handle, CTX_GPREG_X16));
96476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X17, SMC_GET_GP(handle, CTX_GPREG_X17));
96576d53ee1SOlivier Deprez 
9669944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
9679944f557SDaniel Boulby 
9689944f557SDaniel Boulby 			if ((rc != 0ULL) ||
9699944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
9709944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
9719944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
97259bd2ad8SMarc Bonnici 				(FFA_FWK_MSG_BIT |
9739944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
9749944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
9759944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
9769944f557SDaniel Boulby 			} else {
9779944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
9789944f557SDaniel Boulby 			}
9799944f557SDaniel Boulby 
9809944f557SDaniel Boulby 			/*
98176d53ee1SOlivier Deprez 			 * x0-x4 are updated by spmd_smc_forward below.
98276d53ee1SOlivier Deprez 			 * Zero out x5-x7 in the FFA_VERSION response.
98376d53ee1SOlivier Deprez 			 */
98476d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
98576d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
98676d53ee1SOlivier Deprez 			write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
98776d53ee1SOlivier Deprez 
98876d53ee1SOlivier Deprez 			/*
9899944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
9909944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
9919944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
9929944f557SDaniel Boulby 			 */
9939944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
9949944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
995bb01a673SMarc Bonnici 						FFA_PARAM_MBZ, cookie, gpregs,
996bb01a673SMarc Bonnici 						flags);
9974388f28fSJ-Alves 		} else {
998e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
999e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
10004388f28fSJ-Alves 		}
10014388f28fSJ-Alves 
1002e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
1003e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1004e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
1005bdd2596dSAchin Gupta 		break; /* not reached */
1006bdd2596dSAchin Gupta 
1007662af36dSJ-Alves 	case FFA_FEATURES:
1008bdd2596dSAchin Gupta 		/*
1009bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
101052696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
1011bdd2596dSAchin Gupta 		 */
1012bdd2596dSAchin Gupta 
101352696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
101493ff138bSOlivier Deprez 		if (!secure_origin) {
101593ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1016bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1017bb01a673SMarc Bonnici 						handle, flags);
101852696946SOlivier Deprez 		}
101952696946SOlivier Deprez 
1020bdd2596dSAchin Gupta 		/*
1021bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
1022662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
1023bdd2596dSAchin Gupta 		 * nop.
1024bdd2596dSAchin Gupta 		 */
1025662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
1026bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
1027bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
1028bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
10290f14d02fSMax Shvetsov 
1030bdd2596dSAchin Gupta 		break; /* not reached */
1031bdd2596dSAchin Gupta 
1032662af36dSJ-Alves 	case FFA_ID_GET:
1033ac03ac5eSMax Shvetsov 		/*
1034662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
1035ac03ac5eSMax Shvetsov 		 */
1036ac03ac5eSMax Shvetsov 		if (!secure_origin) {
1037662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1038662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
1039662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1040662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1041662af36dSJ-Alves 				 FFA_PARAM_MBZ);
104252696946SOlivier Deprez 		}
104352696946SOlivier Deprez 
1044662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
1045662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
1046662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1047662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1048662af36dSJ-Alves 			 FFA_PARAM_MBZ);
1049ac03ac5eSMax Shvetsov 
1050ac03ac5eSMax Shvetsov 		break; /* not reached */
1051ac03ac5eSMax Shvetsov 
1052cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
1053cdb49d47SOlivier Deprez 		if (secure_origin) {
1054cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
1055cdb49d47SOlivier Deprez 
1056cdb49d47SOlivier Deprez 			if (ret < 0) {
1057cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
1058cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
1059cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1060cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1061cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1062cdb49d47SOlivier Deprez 			} else {
1063cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
1064cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
1065cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1066cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1067cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
1068cdb49d47SOlivier Deprez 			}
1069cdb49d47SOlivier Deprez 		}
1070cdb49d47SOlivier Deprez 
1071cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1072cdb49d47SOlivier Deprez 		break; /* Not reached */
1073cdb49d47SOlivier Deprez 
107470c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
107570c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
107670c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
107770c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
107870c121a2SDaniel Boulby 		}
107970c121a2SDaniel Boulby 		/*
108070c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
108170c121a2SDaniel Boulby 		 * instance where this function is invoked
108270c121a2SDaniel Boulby 		 */
108370c121a2SDaniel Boulby 		if (!secure_origin) {
108470c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
108570c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
108670c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
108770c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
108870c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
108970c121a2SDaniel Boulby 		}
109070c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
109170c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
109270c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
109370c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
109470c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
109570c121a2SDaniel Boulby 
109670c121a2SDaniel Boulby 		break; /* not reached */
109770c121a2SDaniel Boulby 
1098f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
10995519f07cSShruti 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
110066bdfd6eSRaghu Krishnamurthy 		/*
110166bdfd6eSRaghu Krishnamurthy 		 * Regardless of secure_origin, SPMD logical partitions cannot
110266bdfd6eSRaghu Krishnamurthy 		 * handle direct messages. They can only initiate direct
110366bdfd6eSRaghu Krishnamurthy 		 * messages and consume direct responses or errors.
110466bdfd6eSRaghu Krishnamurthy 		 */
110566bdfd6eSRaghu Krishnamurthy 		if (is_spmd_lp_id(ffa_endpoint_source(x1)) ||
110666bdfd6eSRaghu Krishnamurthy 				  is_spmd_lp_id(ffa_endpoint_destination(x1))) {
110766bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
110866bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_INVALID_PARAMETER
110966bdfd6eSRaghu Krishnamurthy 						     );
111066bdfd6eSRaghu Krishnamurthy 		}
111166bdfd6eSRaghu Krishnamurthy 
111266bdfd6eSRaghu Krishnamurthy 		/*
111366bdfd6eSRaghu Krishnamurthy 		 * When there is an ongoing SPMD logical partition direct
111466bdfd6eSRaghu Krishnamurthy 		 * request, there cannot be another direct request. Return
111566bdfd6eSRaghu Krishnamurthy 		 * error in this case. Panic'ing is an option but that does
111666bdfd6eSRaghu Krishnamurthy 		 * not provide the opportunity for caller to abort based on
111766bdfd6eSRaghu Krishnamurthy 		 * error codes.
111866bdfd6eSRaghu Krishnamurthy 		 */
111966bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
112066bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
112166bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
112266bdfd6eSRaghu Krishnamurthy 						     FFA_ERROR_DENIED);
112366bdfd6eSRaghu Krishnamurthy 		}
112466bdfd6eSRaghu Krishnamurthy 
11255519f07cSShruti 		if (!secure_origin) {
11265519f07cSShruti 			/* Validate source endpoint is non-secure for non-secure caller. */
11275519f07cSShruti 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
11285519f07cSShruti 				return spmd_ffa_error_return(handle,
11295519f07cSShruti 						FFA_ERROR_INVALID_PARAMETER);
11305519f07cSShruti 			}
11315519f07cSShruti 		}
1132f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
1133f0d743dbSOlivier Deprez 			ret = spmd_handle_spmc_message(x3, x4,
1134f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X5),
1135f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X6),
1136f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X7));
1137f0d743dbSOlivier Deprez 
1138f0d743dbSOlivier Deprez 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
1139f0d743dbSOlivier Deprez 				FFA_TARGET_INFO_MBZ, ret,
1140f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1141f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
1142f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ);
1143f0d743dbSOlivier Deprez 		} else {
1144f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1145f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1146bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1147bb01a673SMarc Bonnici 						handle, flags);
1148f0d743dbSOlivier Deprez 		}
1149f0d743dbSOlivier Deprez 		break; /* Not reached */
1150f0d743dbSOlivier Deprez 
1151cc6047b3SKathleen Capella 	case FFA_MSG_SEND_DIRECT_REQ2_SMC64:
1152cc6047b3SKathleen Capella 		if (!secure_origin) {
1153cc6047b3SKathleen Capella 			/* Validate source endpoint is non-secure for non-secure caller. */
1154cc6047b3SKathleen Capella 			if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) {
1155cc6047b3SKathleen Capella 				return spmd_ffa_error_return(handle,
1156cc6047b3SKathleen Capella 						FFA_ERROR_INVALID_PARAMETER);
1157cc6047b3SKathleen Capella 			}
1158cc6047b3SKathleen Capella 		}
1159cc6047b3SKathleen Capella 		/* FFA_MSG_SEND_DIRECT_REQ2 not used for framework messages. */
1160cc6047b3SKathleen Capella 		if (secure_origin && spmd_is_spmc_message(x1)) {
1161cc6047b3SKathleen Capella 			return spmd_ffa_error_return(handle, FFA_ERROR_INVALID_PARAMETER);
1162cc6047b3SKathleen Capella 		} else {
1163cc6047b3SKathleen Capella 			/* Forward direct message to the other world */
1164cc6047b3SKathleen Capella 			return spmd_smc_forward(smc_fid, secure_origin,
1165cc6047b3SKathleen Capella 						x1, x2, x3, x4, cookie,
1166cc6047b3SKathleen Capella 						handle, flags);
1167cc6047b3SKathleen Capella 		}
1168cc6047b3SKathleen Capella 		break; /* Not reached */
1169cc6047b3SKathleen Capella 
1170f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
117166bdfd6eSRaghu Krishnamurthy 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
117266bdfd6eSRaghu Krishnamurthy 		if (secure_origin && (spmd_is_spmc_message(x1) ||
117366bdfd6eSRaghu Krishnamurthy 		    is_spmd_logical_sp_dir_req_in_progress(ctx))) {
11748cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1175f0d743dbSOlivier Deprez 		} else {
1176f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
1177f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
1178bb01a673SMarc Bonnici 						x1, x2, x3, x4, cookie,
1179bb01a673SMarc Bonnici 						handle, flags);
1180f0d743dbSOlivier Deprez 		}
1181f0d743dbSOlivier Deprez 		break; /* Not reached */
11820651b7beSKathleen Capella 	case FFA_MSG_SEND_DIRECT_RESP2_SMC64:
11830651b7beSKathleen Capella 		/* Forward direct message to the other world */
11840651b7beSKathleen Capella 		return spmd_smc_forward(smc_fid, secure_origin,
11850651b7beSKathleen Capella 					x1, x2, x3, x4, cookie,
11860651b7beSKathleen Capella 					handle, flags);
11870651b7beSKathleen Capella 		break; /* Not reached */
1188662af36dSJ-Alves 	case FFA_RX_RELEASE:
1189662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
1190662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
1191662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
1192545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
1193fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1194fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
1195fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
1196fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
1197fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
1198fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
1199fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
1200fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
1201fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
1202c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
1203d555233fSFederico Recanati 	case FFA_RX_ACQUIRE:
1204fc3f4800SJ-Alves #endif
1205662af36dSJ-Alves 	case FFA_MSG_RUN:
1206c2eba07cSFederico Recanati 		/*
1207c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
1208c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
1209c2eba07cSFederico Recanati 		 */
121093ff138bSOlivier Deprez 		if (secure_origin) {
1211662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1212662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
1213bdd2596dSAchin Gupta 		}
1214bdd2596dSAchin Gupta 
1215e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1216e138400dSBoyan Karatotev 		/* fallthrough */
1217662af36dSJ-Alves 	case FFA_MSG_SEND:
1218662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
1219662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
1220662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
1221662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
1222662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
1223662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
1224662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
1225662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
1226662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
1227662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
1228662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
1229642db984SMarc Bonnici 	case FFA_MEM_FRAG_TX:
1230642db984SMarc Bonnici 	case FFA_MEM_FRAG_RX:
1231662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
1232662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
1233bdd2596dSAchin Gupta 		/*
123466bdfd6eSRaghu Krishnamurthy 		 * If there is an ongoing direct request from an SPMD logical
123566bdfd6eSRaghu Krishnamurthy 		 * partition, return an error.
1236bdd2596dSAchin Gupta 		 */
123766bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
123866bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
123966bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
124066bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
124166bdfd6eSRaghu Krishnamurthy 		}
1242bdd2596dSAchin Gupta 
124393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1244bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1245bb01a673SMarc Bonnici 					handle, flags);
1246bdd2596dSAchin Gupta 		break; /* not reached */
1247bdd2596dSAchin Gupta 
1248662af36dSJ-Alves 	case FFA_MSG_WAIT:
1249bdd2596dSAchin Gupta 		/*
1250bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
1251bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
125252696946SOlivier Deprez 		 * SPM Core initialised successfully.
1253bdd2596dSAchin Gupta 		 */
12549dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
12558cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
1256bdd2596dSAchin Gupta 		}
1257bdd2596dSAchin Gupta 
1258e138400dSBoyan Karatotev 		/* Forward the call to the other world */
1259e138400dSBoyan Karatotev 		/* fallthrough */
1260386dc365SOlivier Deprez 	case FFA_INTERRUPT:
1261662af36dSJ-Alves 	case FFA_MSG_YIELD:
1262bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
126393ff138bSOlivier Deprez 		if (!secure_origin) {
1264662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
1265662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
1266bdd2596dSAchin Gupta 		}
1267bdd2596dSAchin Gupta 
126866bdfd6eSRaghu Krishnamurthy 		if (is_spmd_logical_sp_dir_req_in_progress(ctx)) {
126966bdfd6eSRaghu Krishnamurthy 			assert(secure_origin);
127066bdfd6eSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle,
127166bdfd6eSRaghu Krishnamurthy 					FFA_ERROR_DENIED);
127266bdfd6eSRaghu Krishnamurthy 		}
127366bdfd6eSRaghu Krishnamurthy 
127493ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
1275bb01a673SMarc Bonnici 					x1, x2, x3, x4, cookie,
1276bb01a673SMarc Bonnici 					handle, flags);
1277bdd2596dSAchin Gupta 		break; /* not reached */
1278bdd2596dSAchin Gupta 
12798cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
12808cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
12818cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
12828cb99c3fSOlivier Deprez 		} else {
12838cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
12848cb99c3fSOlivier Deprez 		}
12858cb99c3fSOlivier Deprez 		break; /* Not reached */
1286eaaf517cSRaghu Krishnamurthy #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
1287eaaf517cSRaghu Krishnamurthy 	case FFA_PARTITION_INFO_GET_REGS_SMC64:
1288eaaf517cSRaghu Krishnamurthy 		if (secure_origin) {
128995f7f6d8SRaghu Krishnamurthy 			return spmd_el3_populate_logical_partition_info(handle, x1,
129095f7f6d8SRaghu Krishnamurthy 								   x2, x3);
1291eaaf517cSRaghu Krishnamurthy 		}
12928cb99c3fSOlivier Deprez 
1293eaaf517cSRaghu Krishnamurthy 		/* Call only supported with SMCCC 1.2+ */
1294eaaf517cSRaghu Krishnamurthy 		if (MAKE_SMCCC_VERSION(SMCCC_MAJOR_VERSION, SMCCC_MINOR_VERSION) < 0x10002) {
1295eaaf517cSRaghu Krishnamurthy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1296eaaf517cSRaghu Krishnamurthy 		}
1297eaaf517cSRaghu Krishnamurthy 
1298eaaf517cSRaghu Krishnamurthy 		return spmd_smc_forward(smc_fid, secure_origin,
1299eaaf517cSRaghu Krishnamurthy 					x1, x2, x3, x4, cookie,
1300eaaf517cSRaghu Krishnamurthy 					handle, flags);
1301eaaf517cSRaghu Krishnamurthy 		break; /* Not reached */
1302eaaf517cSRaghu Krishnamurthy #endif
1303638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC32:
1304638a6f8eSShruti Gupta 	case FFA_CONSOLE_LOG_SMC64:
1305638a6f8eSShruti Gupta 		/* This interface must not be forwarded to other worlds. */
1306638a6f8eSShruti Gupta 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1307638a6f8eSShruti Gupta 		break; /* not reached */
1308638a6f8eSShruti Gupta 
13096671b3d8SMadhukar Pappireddy 	case FFA_EL3_INTR_HANDLE:
13106671b3d8SMadhukar Pappireddy 		if (secure_origin) {
13116671b3d8SMadhukar Pappireddy 			return spmd_handle_group0_intr_swd(handle);
13126671b3d8SMadhukar Pappireddy 		} else {
13136c91fc44SMadhukar Pappireddy 			return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
13146671b3d8SMadhukar Pappireddy 		}
1315bdd2596dSAchin Gupta 	default:
1316bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
1317662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
1318bdd2596dSAchin Gupta 	}
1319bdd2596dSAchin Gupta }
1320