xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 6da76075bf4b953d621aa15c379e62a5f785de3f)
1bdd2596dSAchin Gupta /*
29944f557SDaniel Boulby  * Copyright (c) 2020-2022, 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>
19bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h>
20bdd2596dSAchin Gupta #include <lib/smccc.h>
21bdd2596dSAchin Gupta #include <lib/spinlock.h>
22bdd2596dSAchin Gupta #include <lib/utils.h>
23bdd2596dSAchin Gupta #include <plat/common/common_def.h>
24bdd2596dSAchin Gupta #include <plat/common/platform.h>
25bdd2596dSAchin Gupta #include <platform_def.h>
26662af36dSJ-Alves #include <services/ffa_svc.h>
27*6da76075SMarc Bonnici #include <services/spmc_svc.h>
28bdd2596dSAchin Gupta #include <services/spmd_svc.h>
29bdd2596dSAchin Gupta #include <smccc_helpers.h>
30bdd2596dSAchin Gupta #include "spmd_private.h"
31bdd2596dSAchin Gupta 
32bdd2596dSAchin Gupta /*******************************************************************************
33bdd2596dSAchin Gupta  * SPM Core context information.
34bdd2596dSAchin Gupta  ******************************************************************************/
3552696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
36bdd2596dSAchin Gupta 
37bdd2596dSAchin Gupta /*******************************************************************************
38*6da76075SMarc Bonnici  * SPM Core attribute information is read from its manifest if the SPMC is not
39*6da76075SMarc Bonnici  * at EL3. Else, it is populated from the SPMC directly.
40bdd2596dSAchin Gupta  ******************************************************************************/
4152696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
420f14d02fSMax Shvetsov 
430f14d02fSMax Shvetsov /*******************************************************************************
440f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
450f14d02fSMax Shvetsov  * on secondary cores.
460f14d02fSMax Shvetsov  ******************************************************************************/
470f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
480f14d02fSMax Shvetsov 
490f14d02fSMax Shvetsov /*******************************************************************************
5002d50bb0SOlivier Deprez  * SPM Core context on CPU based on mpidr.
5102d50bb0SOlivier Deprez  ******************************************************************************/
5202d50bb0SOlivier Deprez spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr)
5302d50bb0SOlivier Deprez {
54f7fb0bf7SMax Shvetsov 	int core_idx = plat_core_pos_by_mpidr(mpidr);
55f7fb0bf7SMax Shvetsov 
56f7fb0bf7SMax Shvetsov 	if (core_idx < 0) {
574ce3e99aSScott Branden 		ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx);
58f7fb0bf7SMax Shvetsov 		panic();
59f7fb0bf7SMax Shvetsov 	}
60f7fb0bf7SMax Shvetsov 
61f7fb0bf7SMax Shvetsov 	return &spm_core_context[core_idx];
6202d50bb0SOlivier Deprez }
6302d50bb0SOlivier Deprez 
6402d50bb0SOlivier Deprez /*******************************************************************************
6552696946SOlivier Deprez  * SPM Core context on current CPU get helper.
6652696946SOlivier Deprez  ******************************************************************************/
6752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
6852696946SOlivier Deprez {
6902d50bb0SOlivier Deprez 	return spmd_get_context_by_mpidr(read_mpidr());
7052696946SOlivier Deprez }
7152696946SOlivier Deprez 
7252696946SOlivier Deprez /*******************************************************************************
73a92bc73bSOlivier Deprez  * SPM Core ID getter.
74a92bc73bSOlivier Deprez  ******************************************************************************/
75a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
76a92bc73bSOlivier Deprez {
77a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
78a92bc73bSOlivier Deprez }
79a92bc73bSOlivier Deprez 
80a92bc73bSOlivier Deprez /*******************************************************************************
810f14d02fSMax Shvetsov  * Static function declaration.
820f14d02fSMax Shvetsov  ******************************************************************************/
830f14d02fSMax Shvetsov static int32_t spmd_init(void);
8423d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
85662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle,
8652696946SOlivier Deprez 				       int error_code);
8752696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
8852696946SOlivier Deprez 				 bool secure_origin,
8952696946SOlivier Deprez 				 uint64_t x1,
9052696946SOlivier Deprez 				 uint64_t x2,
9152696946SOlivier Deprez 				 uint64_t x3,
9252696946SOlivier Deprez 				 uint64_t x4,
9352696946SOlivier Deprez 				 void *handle);
94bdd2596dSAchin Gupta 
959944f557SDaniel Boulby /******************************************************************************
969944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
979944f557SDaniel Boulby  *****************************************************************************/
989944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
999944f557SDaniel Boulby 			     unsigned long long message)
1009944f557SDaniel Boulby {
1019944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
1029944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
1039944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
1049944f557SDaniel Boulby 		 spmd_spmc_id_get());
1059944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
1069944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
1079944f557SDaniel Boulby }
1089944f557SDaniel Boulby 
1099944f557SDaniel Boulby 
110bdd2596dSAchin Gupta /*******************************************************************************
11152696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
11252696946SOlivier Deprez  * SPMC entry.
113bdd2596dSAchin Gupta  ******************************************************************************/
114bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
115bdd2596dSAchin Gupta {
116bdd2596dSAchin Gupta 	uint64_t rc;
117bdd2596dSAchin Gupta 
118bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
119bdd2596dSAchin Gupta 
120bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
121bdd2596dSAchin Gupta 
122bdd2596dSAchin Gupta 	/* Restore the context assigned above */
123033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
12428f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
125678ce223SOlivier Deprez #else
126678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
127033039f8SMax Shvetsov #endif
128bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
129bdd2596dSAchin Gupta 
130033039f8SMax Shvetsov 	/* Enter SPMC */
131bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
132bdd2596dSAchin Gupta 
133bdd2596dSAchin Gupta 	/* Save secure state */
134033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
13528f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
136678ce223SOlivier Deprez #else
137678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
138033039f8SMax Shvetsov #endif
139bdd2596dSAchin Gupta 
140bdd2596dSAchin Gupta 	return rc;
141bdd2596dSAchin Gupta }
142bdd2596dSAchin Gupta 
143bdd2596dSAchin Gupta /*******************************************************************************
14452696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
145bdd2596dSAchin Gupta  * called originally.
146bdd2596dSAchin Gupta  ******************************************************************************/
147bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
148bdd2596dSAchin Gupta {
14952696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
150bdd2596dSAchin Gupta 
15152696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
152bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
153bdd2596dSAchin Gupta 
154bdd2596dSAchin Gupta 	/*
155bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
156bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
157bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
158bdd2596dSAchin Gupta 	 */
159bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
160bdd2596dSAchin Gupta 
161bdd2596dSAchin Gupta 	panic();
162bdd2596dSAchin Gupta }
163bdd2596dSAchin Gupta 
164bdd2596dSAchin Gupta /*******************************************************************************
16552696946SOlivier Deprez  * Jump to the SPM Core for the first time.
166bdd2596dSAchin Gupta  ******************************************************************************/
167bdd2596dSAchin Gupta static int32_t spmd_init(void)
168bdd2596dSAchin Gupta {
16952696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
17052696946SOlivier Deprez 	uint64_t rc;
171bdd2596dSAchin Gupta 
17252696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1739dcf63ddSOlivier Deprez 
174f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
175f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
176bdd2596dSAchin Gupta 
177bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
17852696946SOlivier Deprez 	if (rc != 0ULL) {
1794ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
18052696946SOlivier Deprez 		return 0;
181bdd2596dSAchin Gupta 	}
182bdd2596dSAchin Gupta 
1839dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
1849dcf63ddSOlivier Deprez 
18552696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
186bdd2596dSAchin Gupta 
187bdd2596dSAchin Gupta 	return 1;
188bdd2596dSAchin Gupta }
189bdd2596dSAchin Gupta 
190bdd2596dSAchin Gupta /*******************************************************************************
1918cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
1928cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
1938cb99c3fSOlivier Deprez  * itself or a Secure Partition.
1948cb99c3fSOlivier Deprez  ******************************************************************************/
1958cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
1968cb99c3fSOlivier Deprez 					      uint32_t flags,
1978cb99c3fSOlivier Deprez 					      void *handle,
1988cb99c3fSOlivier Deprez 					      void *cookie)
1998cb99c3fSOlivier Deprez {
2008cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
2018cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2028cb99c3fSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
2038cb99c3fSOlivier Deprez 	int64_t rc;
2048cb99c3fSOlivier Deprez 
2058cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2068cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2078cb99c3fSOlivier Deprez 
2088cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2098cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2108cb99c3fSOlivier Deprez 
2118cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2128cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_save(NON_SECURE);
2138cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2148cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2158cb99c3fSOlivier Deprez #endif
2168cb99c3fSOlivier Deprez 
2178cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2188cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2198cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2208cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2218cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2228cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2238cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2248cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2258cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2268cb99c3fSOlivier Deprez 
2278cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2288cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2298cb99c3fSOlivier Deprez 
2308cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
2318cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
2320c23e6f4SOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
2338cb99c3fSOlivier Deprez 	}
2348cb99c3fSOlivier Deprez 
2358cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2368cb99c3fSOlivier Deprez 
2378cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_restore(NON_SECURE);
2388cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2398cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2408cb99c3fSOlivier Deprez #endif
2418cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2428cb99c3fSOlivier Deprez 
2438cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2448cb99c3fSOlivier Deprez }
2458cb99c3fSOlivier Deprez 
2468cb99c3fSOlivier Deprez /*******************************************************************************
24752696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
2480f14d02fSMax Shvetsov  ******************************************************************************/
24923d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
2500f14d02fSMax Shvetsov {
251f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
252f2dcf418SOlivier Deprez 	unsigned int core_id;
2538cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
25452696946SOlivier Deprez 	int rc;
2550f14d02fSMax Shvetsov 
25652696946SOlivier Deprez 	/* Load the SPM Core manifest */
25723d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
2580f14d02fSMax Shvetsov 	if (rc != 0) {
25952696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
26052696946SOlivier Deprez 		return rc;
2610f14d02fSMax Shvetsov 	}
2620f14d02fSMax Shvetsov 
2630f14d02fSMax Shvetsov 	/*
26452696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
26552696946SOlivier Deprez 	 * Dispatcher version.
2660f14d02fSMax Shvetsov 	 */
267662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
268662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
269662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
2700f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
27152696946SOlivier Deprez 		return -EINVAL;
2720f14d02fSMax Shvetsov 	}
2730f14d02fSMax Shvetsov 
274662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
2750f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
2760f14d02fSMax Shvetsov 
27752696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
278033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
2790f14d02fSMax Shvetsov 
280ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
28152696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
28252696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
28352696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
28452696946SOlivier Deprez 		return -EINVAL;
285ac03ac5eSMax Shvetsov 	}
286ac03ac5eSMax Shvetsov 
28752696946SOlivier Deprez 	/* Validate the SPM Core execution state */
2880f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
2890f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
29023d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
2910f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
29252696946SOlivier Deprez 		return -EINVAL;
2930f14d02fSMax Shvetsov 	}
2940f14d02fSMax Shvetsov 
29523d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
29623d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
2970f14d02fSMax Shvetsov 
298033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
299033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
300033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
301033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
30252696946SOlivier Deprez 		return -EINVAL;
3030f14d02fSMax Shvetsov 	}
3040f14d02fSMax Shvetsov 
3050f14d02fSMax Shvetsov 	/*
3060f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
3070f14d02fSMax Shvetsov 	 * is required for SPM
3080f14d02fSMax Shvetsov 	 */
30952696946SOlivier Deprez 	if (!is_armv8_4_sel2_present()) {
31052696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
31152696946SOlivier Deprez 		return -EINVAL;
3120f14d02fSMax Shvetsov 	}
313033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
3140f14d02fSMax Shvetsov 
3150f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
3160f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
31752696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
3180f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
3190f14d02fSMax Shvetsov 	}
3200f14d02fSMax Shvetsov 
3210f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
3220f14d02fSMax Shvetsov 
3230f14d02fSMax Shvetsov 	/*
32452696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
32552696946SOlivier Deprez 	 * manifest.
3260f14d02fSMax Shvetsov 	 */
3270f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
3280f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
3290f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
3300f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
3310f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
3320f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
3330f14d02fSMax Shvetsov 	} else {
334033039f8SMax Shvetsov 
335033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
336033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
337033039f8SMax Shvetsov #else
338033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
339033039f8SMax Shvetsov #endif
340033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
3410f14d02fSMax Shvetsov 					     MODE_SP_ELX,
3420f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
3430f14d02fSMax Shvetsov 	}
3440f14d02fSMax Shvetsov 
345f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
346f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
347f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
3480f14d02fSMax Shvetsov 
349f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
350f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
351f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
3520f14d02fSMax Shvetsov 
353f2dcf418SOlivier Deprez 		/*
354f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
355f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
356f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
357f2dcf418SOlivier Deprez 		 */
358f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
359f2dcf418SOlivier Deprez 	}
3600f14d02fSMax Shvetsov 
361a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
362a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
363a334c4e6SOlivier Deprez 
3640f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
3650f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
3660f14d02fSMax Shvetsov 
367f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
368f2dcf418SOlivier Deprez 
3698cb99c3fSOlivier Deprez 	/*
3708cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
3718cb99c3fSOlivier Deprez 	 * while the NWd is running.
3728cb99c3fSOlivier Deprez 	 */
3738cb99c3fSOlivier Deprez 	flags = 0;
3748cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
3758cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
3768cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
3778cb99c3fSOlivier Deprez 					     flags);
3788cb99c3fSOlivier Deprez 	if (rc != 0) {
3798cb99c3fSOlivier Deprez 		panic();
3808cb99c3fSOlivier Deprez 	}
3818cb99c3fSOlivier Deprez 
3820f14d02fSMax Shvetsov 	return 0;
3830f14d02fSMax Shvetsov }
3840f14d02fSMax Shvetsov 
3850f14d02fSMax Shvetsov /*******************************************************************************
38652696946SOlivier Deprez  * Initialize context of SPM Core.
387bdd2596dSAchin Gupta  ******************************************************************************/
3880f14d02fSMax Shvetsov int spmd_setup(void)
389bdd2596dSAchin Gupta {
390bdd2596dSAchin Gupta 	int rc;
391*6da76075SMarc Bonnici 	void *spmc_manifest;
392*6da76075SMarc Bonnici 
393*6da76075SMarc Bonnici 	/*
394*6da76075SMarc Bonnici 	 * If the SPMC is at EL3, then just initialise it directly. The
395*6da76075SMarc Bonnici 	 * shenanigans of when it is at a lower EL are not needed.
396*6da76075SMarc Bonnici 	 */
397*6da76075SMarc Bonnici 	if (is_spmc_at_el3()) {
398*6da76075SMarc Bonnici 		/* Allow the SPMC to populate its attributes directly. */
399*6da76075SMarc Bonnici 		spmc_populate_attrs(&spmc_attrs);
400*6da76075SMarc Bonnici 
401*6da76075SMarc Bonnici 		rc = spmc_setup();
402*6da76075SMarc Bonnici 		if (rc != 0) {
403*6da76075SMarc Bonnici 			ERROR("SPMC initialisation failed 0x%x.\n", rc);
404*6da76075SMarc Bonnici 		}
405*6da76075SMarc Bonnici 		return rc;
406*6da76075SMarc Bonnici 	}
407bdd2596dSAchin Gupta 
408bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
40952696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
41052696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
41152696946SOlivier Deprez 		return -EINVAL;
412bdd2596dSAchin Gupta 	}
413bdd2596dSAchin Gupta 
414bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
41552696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
416bdd2596dSAchin Gupta 
417bdd2596dSAchin Gupta 	/*
418bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
41952696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
420bdd2596dSAchin Gupta 	 */
42123d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
42223d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
42323d5ba86SOlivier Deprez 		ERROR("Invalid or absent SPM Core manifest.\n");
42423d5ba86SOlivier Deprez 		return -EINVAL;
425bdd2596dSAchin Gupta 	}
426bdd2596dSAchin Gupta 
4270f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
42823d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
4290f14d02fSMax Shvetsov 	if (rc != 0) {
43052696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
431bdd2596dSAchin Gupta 	}
432bdd2596dSAchin Gupta 
4330f14d02fSMax Shvetsov 	return rc;
4340f14d02fSMax Shvetsov }
4350f14d02fSMax Shvetsov 
4360f14d02fSMax Shvetsov /*******************************************************************************
4370f14d02fSMax Shvetsov  * Forward SMC to the other security state
4380f14d02fSMax Shvetsov  ******************************************************************************/
43952696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
44052696946SOlivier Deprez 				 bool secure_origin,
44152696946SOlivier Deprez 				 uint64_t x1,
44252696946SOlivier Deprez 				 uint64_t x2,
44352696946SOlivier Deprez 				 uint64_t x3,
44452696946SOlivier Deprez 				 uint64_t x4,
44552696946SOlivier Deprez 				 void *handle)
4460f14d02fSMax Shvetsov {
447c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
448c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
44993ff138bSOlivier Deprez 
4500f14d02fSMax Shvetsov 	/* Save incoming security state */
451033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
452678ce223SOlivier Deprez 	if (secure_state_in == NON_SECURE) {
453678ce223SOlivier Deprez 		cm_el1_sysregs_context_save(secure_state_in);
454678ce223SOlivier Deprez 	}
45593ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
456678ce223SOlivier Deprez #else
457678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
458033039f8SMax Shvetsov #endif
4590f14d02fSMax Shvetsov 
4600f14d02fSMax Shvetsov 	/* Restore outgoing security state */
461033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
462678ce223SOlivier Deprez 	if (secure_state_out == NON_SECURE) {
463678ce223SOlivier Deprez 		cm_el1_sysregs_context_restore(secure_state_out);
464678ce223SOlivier Deprez 	}
46593ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
466678ce223SOlivier Deprez #else
467678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
468033039f8SMax Shvetsov #endif
46993ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
4700f14d02fSMax Shvetsov 
47193ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
4720f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
4730f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
4740f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
4750f14d02fSMax Shvetsov }
4760f14d02fSMax Shvetsov 
4770f14d02fSMax Shvetsov /*******************************************************************************
478662af36dSJ-Alves  * Return FFA_ERROR with specified error code
4790f14d02fSMax Shvetsov  ******************************************************************************/
480662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code)
4810f14d02fSMax Shvetsov {
482e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
483e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
484662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
485662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
486bdd2596dSAchin Gupta }
487bdd2596dSAchin Gupta 
488f0d743dbSOlivier Deprez /*******************************************************************************
489f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
490f0d743dbSOlivier Deprez  ******************************************************************************/
491f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
492f0d743dbSOlivier Deprez {
493f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
494f0d743dbSOlivier Deprez 
495f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
496f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
497f0d743dbSOlivier Deprez }
498f0d743dbSOlivier Deprez 
499c2901419SOlivier Deprez /******************************************************************************
500c2901419SOlivier Deprez  * spmd_is_spmc_message
501c2901419SOlivier Deprez  *****************************************************************************/
502c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
503c2901419SOlivier Deprez {
504c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
505c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
506c2901419SOlivier Deprez }
507c2901419SOlivier Deprez 
508f0d743dbSOlivier Deprez /******************************************************************************
509f0d743dbSOlivier Deprez  * spmd_handle_spmc_message
510f0d743dbSOlivier Deprez  *****************************************************************************/
511a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg,
512a92bc73bSOlivier Deprez 		unsigned long long parm1, unsigned long long parm2,
513a92bc73bSOlivier Deprez 		unsigned long long parm3, unsigned long long parm4)
514f0d743dbSOlivier Deprez {
515f0d743dbSOlivier Deprez 	VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
516f0d743dbSOlivier Deprez 		msg, parm1, parm2, parm3, parm4);
517f0d743dbSOlivier Deprez 
518f0d743dbSOlivier Deprez 	return -EINVAL;
519f0d743dbSOlivier Deprez }
520f0d743dbSOlivier Deprez 
521bdd2596dSAchin Gupta /*******************************************************************************
522662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
523bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
524bdd2596dSAchin Gupta  ******************************************************************************/
52552696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
52652696946SOlivier Deprez 			  uint64_t x1,
52752696946SOlivier Deprez 			  uint64_t x2,
52852696946SOlivier Deprez 			  uint64_t x3,
52952696946SOlivier Deprez 			  uint64_t x4,
53052696946SOlivier Deprez 			  void *cookie,
53152696946SOlivier Deprez 			  void *handle,
532bdd2596dSAchin Gupta 			  uint64_t flags)
533bdd2596dSAchin Gupta {
534cdb49d47SOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
53552696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
53693ff138bSOlivier Deprez 	bool secure_origin;
53793ff138bSOlivier Deprez 	int32_t ret;
5384388f28fSJ-Alves 	uint32_t input_version;
539bdd2596dSAchin Gupta 
540bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
54193ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
542bdd2596dSAchin Gupta 
5434ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
5444ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
545cdb49d47SOlivier Deprez 		    linear_id, smc_fid, x1, x2, x3, x4,
546cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
547bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
548bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
549bdd2596dSAchin Gupta 
550bdd2596dSAchin Gupta 	switch (smc_fid) {
551662af36dSJ-Alves 	case FFA_ERROR:
552bdd2596dSAchin Gupta 		/*
553bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
55452696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
555bdd2596dSAchin Gupta 		 * unsuccessfully.
556bdd2596dSAchin Gupta 		 */
5579dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
558bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
5590f14d02fSMax Shvetsov 		}
560bdd2596dSAchin Gupta 
56193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5620f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
563bdd2596dSAchin Gupta 		break; /* not reached */
564bdd2596dSAchin Gupta 
565662af36dSJ-Alves 	case FFA_VERSION:
5664388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
567bdd2596dSAchin Gupta 		/*
5684388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
5694388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
5704388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
5714388f28fSJ-Alves 		 * return SPMC's version.
5724388f28fSJ-Alves 		 * Sanity check to "input_version".
573bdd2596dSAchin Gupta 		 */
5744388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
5754388f28fSJ-Alves 			(ctx->state == SPMC_STATE_RESET)) {
5764388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
5774388f28fSJ-Alves 		} else if (!secure_origin) {
5789944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
5799944f557SDaniel Boulby 			uint64_t rc;
5809944f557SDaniel Boulby 
5819944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
5829944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
583e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
584e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
5859944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
5869944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
5879944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
5889944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
5899944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
5909944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
5919944f557SDaniel Boulby 				break;
5929944f557SDaniel Boulby 			}
5939944f557SDaniel Boulby 			/* Save non-secure system registers context */
5949944f557SDaniel Boulby 			cm_el1_sysregs_context_save(NON_SECURE);
5959944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
5969944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
5979944f557SDaniel Boulby #endif
5989944f557SDaniel Boulby 
5999944f557SDaniel Boulby 			/*
6009944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
6019944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
6029944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
6039944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
6049944f557SDaniel Boulby 			 */
6059944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
6069944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
6079944f557SDaniel Boulby 						input_version);
6089944f557SDaniel Boulby 
6099944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
6109944f557SDaniel Boulby 
6119944f557SDaniel Boulby 			if ((rc != 0ULL) ||
6129944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
6139944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
6149944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
6159944f557SDaniel Boulby 				(SPMD_FWK_MSG_BIT |
6169944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
6179944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
6189944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
6199944f557SDaniel Boulby 			} else {
6209944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
6219944f557SDaniel Boulby 			}
6229944f557SDaniel Boulby 
6239944f557SDaniel Boulby 			/*
6249944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
6259944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
6269944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
6279944f557SDaniel Boulby 			 */
6289944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
6299944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
6309944f557SDaniel Boulby 						FFA_PARAM_MBZ, gpregs);
6314388f28fSJ-Alves 		} else {
632e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
633e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
6344388f28fSJ-Alves 		}
6354388f28fSJ-Alves 
636e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
637e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
638e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
639bdd2596dSAchin Gupta 		break; /* not reached */
640bdd2596dSAchin Gupta 
641662af36dSJ-Alves 	case FFA_FEATURES:
642bdd2596dSAchin Gupta 		/*
643bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
64452696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
645bdd2596dSAchin Gupta 		 */
646bdd2596dSAchin Gupta 
64752696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
64893ff138bSOlivier Deprez 		if (!secure_origin) {
64993ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
6500f14d02fSMax Shvetsov 						x1, x2, x3, x4, handle);
65152696946SOlivier Deprez 		}
65252696946SOlivier Deprez 
653bdd2596dSAchin Gupta 		/*
654bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
655662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
656bdd2596dSAchin Gupta 		 * nop.
657bdd2596dSAchin Gupta 		 */
658662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
659bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
660bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
661bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
6620f14d02fSMax Shvetsov 
663bdd2596dSAchin Gupta 		break; /* not reached */
664bdd2596dSAchin Gupta 
665662af36dSJ-Alves 	case FFA_ID_GET:
666ac03ac5eSMax Shvetsov 		/*
667662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
668ac03ac5eSMax Shvetsov 		 */
669ac03ac5eSMax Shvetsov 		if (!secure_origin) {
670662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
671662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
672662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
673662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
674662af36dSJ-Alves 				 FFA_PARAM_MBZ);
67552696946SOlivier Deprez 		}
67652696946SOlivier Deprez 
677662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
678662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
679662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
680662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
681662af36dSJ-Alves 			 FFA_PARAM_MBZ);
682ac03ac5eSMax Shvetsov 
683ac03ac5eSMax Shvetsov 		break; /* not reached */
684ac03ac5eSMax Shvetsov 
685cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
686cdb49d47SOlivier Deprez 		if (secure_origin) {
687cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
688cdb49d47SOlivier Deprez 
689cdb49d47SOlivier Deprez 			if (ret < 0) {
690cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
691cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
692cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
693cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
694cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
695cdb49d47SOlivier Deprez 			} else {
696cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
697cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
698cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
699cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
700cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
701cdb49d47SOlivier Deprez 			}
702cdb49d47SOlivier Deprez 		}
703cdb49d47SOlivier Deprez 
704cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
705cdb49d47SOlivier Deprez 		break; /* Not reached */
706cdb49d47SOlivier Deprez 
70770c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
70870c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
70970c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
71070c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
71170c121a2SDaniel Boulby 		}
71270c121a2SDaniel Boulby 		/*
71370c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
71470c121a2SDaniel Boulby 		 * instance where this function is invoked
71570c121a2SDaniel Boulby 		 */
71670c121a2SDaniel Boulby 		if (!secure_origin) {
71770c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
71870c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
71970c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
72070c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
72170c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
72270c121a2SDaniel Boulby 		}
72370c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
72470c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
72570c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
72670c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
72770c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
72870c121a2SDaniel Boulby 
72970c121a2SDaniel Boulby 		break; /* not reached */
73070c121a2SDaniel Boulby 
731f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
732f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
733f0d743dbSOlivier Deprez 			ret = spmd_handle_spmc_message(x3, x4,
734f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X5),
735f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X6),
736f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X7));
737f0d743dbSOlivier Deprez 
738f0d743dbSOlivier Deprez 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
739f0d743dbSOlivier Deprez 				FFA_TARGET_INFO_MBZ, ret,
740f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
741f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
742f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ);
743f0d743dbSOlivier Deprez 		} else {
744f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
745f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
746f0d743dbSOlivier Deprez 				x1, x2, x3, x4, handle);
747f0d743dbSOlivier Deprez 		}
748f0d743dbSOlivier Deprez 		break; /* Not reached */
749f0d743dbSOlivier Deprez 
750f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
751f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
7528cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
753f0d743dbSOlivier Deprez 		} else {
754f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
755f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
756f0d743dbSOlivier Deprez 				x1, x2, x3, x4, handle);
757f0d743dbSOlivier Deprez 		}
758f0d743dbSOlivier Deprez 		break; /* Not reached */
759f0d743dbSOlivier Deprez 
760662af36dSJ-Alves 	case FFA_RX_RELEASE:
761662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
762662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
763662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
764545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
765fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
766fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
767fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
768fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
769fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
770fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
771fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
772fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
773fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
774c2eba07cSFederico Recanati 	case FFA_MSG_SEND2:
775fc3f4800SJ-Alves #endif
776662af36dSJ-Alves 	case FFA_MSG_RUN:
777c2eba07cSFederico Recanati 		/*
778c2eba07cSFederico Recanati 		 * Above calls should be invoked only by the Normal world and
779c2eba07cSFederico Recanati 		 * must not be forwarded from Secure world to Normal world.
780c2eba07cSFederico Recanati 		 */
78193ff138bSOlivier Deprez 		if (secure_origin) {
782662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
783662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
784bdd2596dSAchin Gupta 		}
785bdd2596dSAchin Gupta 
786bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
787662af36dSJ-Alves 	case FFA_MSG_SEND:
788662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
789662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
790662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
791662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
792662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
793662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
794662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
795662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
796662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
797662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
798662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
799662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
800662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
801662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
802662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
803bdd2596dSAchin Gupta 		/*
804bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
805bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
806bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
807bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
808bdd2596dSAchin Gupta 		 */
809bdd2596dSAchin Gupta 
81093ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
8110f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
812bdd2596dSAchin Gupta 		break; /* not reached */
813bdd2596dSAchin Gupta 
814662af36dSJ-Alves 	case FFA_MSG_WAIT:
815bdd2596dSAchin Gupta 		/*
816bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
817bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
81852696946SOlivier Deprez 		 * SPM Core initialised successfully.
819bdd2596dSAchin Gupta 		 */
8209dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
8218cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
822bdd2596dSAchin Gupta 		}
823bdd2596dSAchin Gupta 
8240f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
825386dc365SOlivier Deprez 	case FFA_INTERRUPT:
826662af36dSJ-Alves 	case FFA_MSG_YIELD:
827bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
82893ff138bSOlivier Deprez 		if (!secure_origin) {
829662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
830662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
831bdd2596dSAchin Gupta 		}
832bdd2596dSAchin Gupta 
83393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
8340f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
835bdd2596dSAchin Gupta 		break; /* not reached */
836bdd2596dSAchin Gupta 
8378cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
8388cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
8398cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
8408cb99c3fSOlivier Deprez 		} else {
8418cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
8428cb99c3fSOlivier Deprez 		}
8438cb99c3fSOlivier Deprez 		break; /* Not reached */
8448cb99c3fSOlivier Deprez 
845bdd2596dSAchin Gupta 	default:
846bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
847662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
848bdd2596dSAchin Gupta 	}
849bdd2596dSAchin Gupta }
850