xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 9944f55761c4d5cc1feefaf5e33bf7fb83d8f5f3)
1bdd2596dSAchin Gupta /*
2*9944f557SDaniel 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>
27bdd2596dSAchin Gupta #include <services/spmd_svc.h>
28bdd2596dSAchin Gupta #include <smccc_helpers.h>
29bdd2596dSAchin Gupta #include "spmd_private.h"
30bdd2596dSAchin Gupta 
31bdd2596dSAchin Gupta /*******************************************************************************
32bdd2596dSAchin Gupta  * SPM Core context information.
33bdd2596dSAchin Gupta  ******************************************************************************/
3452696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
35bdd2596dSAchin Gupta 
36bdd2596dSAchin Gupta /*******************************************************************************
37bdd2596dSAchin Gupta  * SPM Core attribute information read from its manifest.
38bdd2596dSAchin Gupta  ******************************************************************************/
3952696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
400f14d02fSMax Shvetsov 
410f14d02fSMax Shvetsov /*******************************************************************************
420f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
430f14d02fSMax Shvetsov  * on secondary cores.
440f14d02fSMax Shvetsov  ******************************************************************************/
450f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
460f14d02fSMax Shvetsov 
470f14d02fSMax Shvetsov /*******************************************************************************
4802d50bb0SOlivier Deprez  * SPM Core context on CPU based on mpidr.
4902d50bb0SOlivier Deprez  ******************************************************************************/
5002d50bb0SOlivier Deprez spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr)
5102d50bb0SOlivier Deprez {
52f7fb0bf7SMax Shvetsov 	int core_idx = plat_core_pos_by_mpidr(mpidr);
53f7fb0bf7SMax Shvetsov 
54f7fb0bf7SMax Shvetsov 	if (core_idx < 0) {
554ce3e99aSScott Branden 		ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx);
56f7fb0bf7SMax Shvetsov 		panic();
57f7fb0bf7SMax Shvetsov 	}
58f7fb0bf7SMax Shvetsov 
59f7fb0bf7SMax Shvetsov 	return &spm_core_context[core_idx];
6002d50bb0SOlivier Deprez }
6102d50bb0SOlivier Deprez 
6202d50bb0SOlivier Deprez /*******************************************************************************
6352696946SOlivier Deprez  * SPM Core context on current CPU get helper.
6452696946SOlivier Deprez  ******************************************************************************/
6552696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
6652696946SOlivier Deprez {
6702d50bb0SOlivier Deprez 	return spmd_get_context_by_mpidr(read_mpidr());
6852696946SOlivier Deprez }
6952696946SOlivier Deprez 
7052696946SOlivier Deprez /*******************************************************************************
71a92bc73bSOlivier Deprez  * SPM Core ID getter.
72a92bc73bSOlivier Deprez  ******************************************************************************/
73a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void)
74a92bc73bSOlivier Deprez {
75a92bc73bSOlivier Deprez 	return spmc_attrs.spmc_id;
76a92bc73bSOlivier Deprez }
77a92bc73bSOlivier Deprez 
78a92bc73bSOlivier Deprez /*******************************************************************************
790f14d02fSMax Shvetsov  * Static function declaration.
800f14d02fSMax Shvetsov  ******************************************************************************/
810f14d02fSMax Shvetsov static int32_t spmd_init(void);
8223d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
83662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle,
8452696946SOlivier Deprez 				       int error_code);
8552696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
8652696946SOlivier Deprez 				 bool secure_origin,
8752696946SOlivier Deprez 				 uint64_t x1,
8852696946SOlivier Deprez 				 uint64_t x2,
8952696946SOlivier Deprez 				 uint64_t x3,
9052696946SOlivier Deprez 				 uint64_t x4,
9152696946SOlivier Deprez 				 void *handle);
92bdd2596dSAchin Gupta 
93*9944f557SDaniel Boulby /******************************************************************************
94*9944f557SDaniel Boulby  * Builds an SPMD to SPMC direct message request.
95*9944f557SDaniel Boulby  *****************************************************************************/
96*9944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func,
97*9944f557SDaniel Boulby 			     unsigned long long message)
98*9944f557SDaniel Boulby {
99*9944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32);
100*9944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X1,
101*9944f557SDaniel Boulby 		(SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) |
102*9944f557SDaniel Boulby 		 spmd_spmc_id_get());
103*9944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func);
104*9944f557SDaniel Boulby 	write_ctx_reg(gpregs, CTX_GPREG_X3, message);
105*9944f557SDaniel Boulby }
106*9944f557SDaniel Boulby 
107*9944f557SDaniel Boulby 
108bdd2596dSAchin Gupta /*******************************************************************************
10952696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
11052696946SOlivier Deprez  * SPMC entry.
111bdd2596dSAchin Gupta  ******************************************************************************/
112bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
113bdd2596dSAchin Gupta {
114bdd2596dSAchin Gupta 	uint64_t rc;
115bdd2596dSAchin Gupta 
116bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
117bdd2596dSAchin Gupta 
118bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
119bdd2596dSAchin Gupta 
120bdd2596dSAchin Gupta 	/* Restore the context assigned above */
121033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
12228f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
123678ce223SOlivier Deprez #else
124678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(SECURE);
125033039f8SMax Shvetsov #endif
126bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
127bdd2596dSAchin Gupta 
128033039f8SMax Shvetsov 	/* Enter SPMC */
129bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
130bdd2596dSAchin Gupta 
131bdd2596dSAchin Gupta 	/* Save secure state */
132033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
13328f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
134678ce223SOlivier Deprez #else
135678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(SECURE);
136033039f8SMax Shvetsov #endif
137bdd2596dSAchin Gupta 
138bdd2596dSAchin Gupta 	return rc;
139bdd2596dSAchin Gupta }
140bdd2596dSAchin Gupta 
141bdd2596dSAchin Gupta /*******************************************************************************
14252696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
143bdd2596dSAchin Gupta  * called originally.
144bdd2596dSAchin Gupta  ******************************************************************************/
145bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
146bdd2596dSAchin Gupta {
14752696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
148bdd2596dSAchin Gupta 
14952696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
150bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
151bdd2596dSAchin Gupta 
152bdd2596dSAchin Gupta 	/*
153bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
154bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
155bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
156bdd2596dSAchin Gupta 	 */
157bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
158bdd2596dSAchin Gupta 
159bdd2596dSAchin Gupta 	panic();
160bdd2596dSAchin Gupta }
161bdd2596dSAchin Gupta 
162bdd2596dSAchin Gupta /*******************************************************************************
16352696946SOlivier Deprez  * Jump to the SPM Core for the first time.
164bdd2596dSAchin Gupta  ******************************************************************************/
165bdd2596dSAchin Gupta static int32_t spmd_init(void)
166bdd2596dSAchin Gupta {
16752696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
16852696946SOlivier Deprez 	uint64_t rc;
169bdd2596dSAchin Gupta 
17052696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
1719dcf63ddSOlivier Deprez 
172f2dcf418SOlivier Deprez 	/* Primary boot core enters the SPMC for initialization. */
173f2dcf418SOlivier Deprez 	ctx->state = SPMC_STATE_ON_PENDING;
174bdd2596dSAchin Gupta 
175bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
17652696946SOlivier Deprez 	if (rc != 0ULL) {
1774ce3e99aSScott Branden 		ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc);
17852696946SOlivier Deprez 		return 0;
179bdd2596dSAchin Gupta 	}
180bdd2596dSAchin Gupta 
1819dcf63ddSOlivier Deprez 	ctx->state = SPMC_STATE_ON;
1829dcf63ddSOlivier Deprez 
18352696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
184bdd2596dSAchin Gupta 
185bdd2596dSAchin Gupta 	return 1;
186bdd2596dSAchin Gupta }
187bdd2596dSAchin Gupta 
188bdd2596dSAchin Gupta /*******************************************************************************
1898cb99c3fSOlivier Deprez  * spmd_secure_interrupt_handler
1908cb99c3fSOlivier Deprez  * Enter the SPMC for further handling of the secure interrupt by the SPMC
1918cb99c3fSOlivier Deprez  * itself or a Secure Partition.
1928cb99c3fSOlivier Deprez  ******************************************************************************/
1938cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id,
1948cb99c3fSOlivier Deprez 					      uint32_t flags,
1958cb99c3fSOlivier Deprez 					      void *handle,
1968cb99c3fSOlivier Deprez 					      void *cookie)
1978cb99c3fSOlivier Deprez {
1988cb99c3fSOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
1998cb99c3fSOlivier Deprez 	gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
2008cb99c3fSOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
2018cb99c3fSOlivier Deprez 	int64_t rc;
2028cb99c3fSOlivier Deprez 
2038cb99c3fSOlivier Deprez 	/* Sanity check the security state when the exception was generated */
2048cb99c3fSOlivier Deprez 	assert(get_interrupt_src_ss(flags) == NON_SECURE);
2058cb99c3fSOlivier Deprez 
2068cb99c3fSOlivier Deprez 	/* Sanity check the pointer to this cpu's context */
2078cb99c3fSOlivier Deprez 	assert(handle == cm_get_context(NON_SECURE));
2088cb99c3fSOlivier Deprez 
2098cb99c3fSOlivier Deprez 	/* Save the non-secure context before entering SPMC */
2108cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_save(NON_SECURE);
2118cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2128cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_save(NON_SECURE);
2138cb99c3fSOlivier Deprez #endif
2148cb99c3fSOlivier Deprez 
2158cb99c3fSOlivier Deprez 	/* Convey the event to the SPMC through the FFA_INTERRUPT interface. */
2168cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT);
2178cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X1, 0);
2188cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X2, 0);
2198cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X3, 0);
2208cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X4, 0);
2218cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X5, 0);
2228cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X6, 0);
2238cb99c3fSOlivier Deprez 	write_ctx_reg(gpregs, CTX_GPREG_X7, 0);
2248cb99c3fSOlivier Deprez 
2258cb99c3fSOlivier Deprez 	/* Mark current core as handling a secure interrupt. */
2268cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = true;
2278cb99c3fSOlivier Deprez 
2288cb99c3fSOlivier Deprez 	rc = spmd_spm_core_sync_entry(ctx);
2298cb99c3fSOlivier Deprez 	if (rc != 0ULL) {
2300c23e6f4SOlivier Deprez 		ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id);
2318cb99c3fSOlivier Deprez 	}
2328cb99c3fSOlivier Deprez 
2338cb99c3fSOlivier Deprez 	ctx->secure_interrupt_ongoing = false;
2348cb99c3fSOlivier Deprez 
2358cb99c3fSOlivier Deprez 	cm_el1_sysregs_context_restore(NON_SECURE);
2368cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2
2378cb99c3fSOlivier Deprez 	cm_el2_sysregs_context_restore(NON_SECURE);
2388cb99c3fSOlivier Deprez #endif
2398cb99c3fSOlivier Deprez 	cm_set_next_eret_context(NON_SECURE);
2408cb99c3fSOlivier Deprez 
2418cb99c3fSOlivier Deprez 	SMC_RET0(&ctx->cpu_ctx);
2428cb99c3fSOlivier Deprez }
2438cb99c3fSOlivier Deprez 
2448cb99c3fSOlivier Deprez /*******************************************************************************
24552696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
2460f14d02fSMax Shvetsov  ******************************************************************************/
24723d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
2480f14d02fSMax Shvetsov {
249f2dcf418SOlivier Deprez 	cpu_context_t *cpu_ctx;
250f2dcf418SOlivier Deprez 	unsigned int core_id;
2518cb99c3fSOlivier Deprez 	uint32_t ep_attr, flags;
25252696946SOlivier Deprez 	int rc;
2530f14d02fSMax Shvetsov 
25452696946SOlivier Deprez 	/* Load the SPM Core manifest */
25523d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
2560f14d02fSMax Shvetsov 	if (rc != 0) {
25752696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
25852696946SOlivier Deprez 		return rc;
2590f14d02fSMax Shvetsov 	}
2600f14d02fSMax Shvetsov 
2610f14d02fSMax Shvetsov 	/*
26252696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
26352696946SOlivier Deprez 	 * Dispatcher version.
2640f14d02fSMax Shvetsov 	 */
265662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
266662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
267662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
2680f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
26952696946SOlivier Deprez 		return -EINVAL;
2700f14d02fSMax Shvetsov 	}
2710f14d02fSMax Shvetsov 
272662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
2730f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
2740f14d02fSMax Shvetsov 
27552696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
276033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
2770f14d02fSMax Shvetsov 
278ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
27952696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
28052696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
28152696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
28252696946SOlivier Deprez 		return -EINVAL;
283ac03ac5eSMax Shvetsov 	}
284ac03ac5eSMax Shvetsov 
28552696946SOlivier Deprez 	/* Validate the SPM Core execution state */
2860f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
2870f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
28823d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
2890f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
29052696946SOlivier Deprez 		return -EINVAL;
2910f14d02fSMax Shvetsov 	}
2920f14d02fSMax Shvetsov 
29323d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
29423d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
2950f14d02fSMax Shvetsov 
296033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
297033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
298033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
299033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
30052696946SOlivier Deprez 		return -EINVAL;
3010f14d02fSMax Shvetsov 	}
3020f14d02fSMax Shvetsov 
3030f14d02fSMax Shvetsov 	/*
3040f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
3050f14d02fSMax Shvetsov 	 * is required for SPM
3060f14d02fSMax Shvetsov 	 */
30752696946SOlivier Deprez 	if (!is_armv8_4_sel2_present()) {
30852696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
30952696946SOlivier Deprez 		return -EINVAL;
3100f14d02fSMax Shvetsov 	}
311033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
3120f14d02fSMax Shvetsov 
3130f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
3140f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
31552696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
3160f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
3170f14d02fSMax Shvetsov 	}
3180f14d02fSMax Shvetsov 
3190f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
3200f14d02fSMax Shvetsov 
3210f14d02fSMax Shvetsov 	/*
32252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
32352696946SOlivier Deprez 	 * manifest.
3240f14d02fSMax Shvetsov 	 */
3250f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
3260f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
3270f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
3280f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
3290f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
3300f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
3310f14d02fSMax Shvetsov 	} else {
332033039f8SMax Shvetsov 
333033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
334033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
335033039f8SMax Shvetsov #else
336033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
337033039f8SMax Shvetsov #endif
338033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
3390f14d02fSMax Shvetsov 					     MODE_SP_ELX,
3400f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
3410f14d02fSMax Shvetsov 	}
3420f14d02fSMax Shvetsov 
343f2dcf418SOlivier Deprez 	/* Set an initial SPMC context state for all cores. */
344f2dcf418SOlivier Deprez 	for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) {
345f2dcf418SOlivier Deprez 		spm_core_context[core_id].state = SPMC_STATE_OFF;
3460f14d02fSMax Shvetsov 
347f2dcf418SOlivier Deprez 		/* Setup an initial cpu context for the SPMC. */
348f2dcf418SOlivier Deprez 		cpu_ctx = &spm_core_context[core_id].cpu_ctx;
349f2dcf418SOlivier Deprez 		cm_setup_context(cpu_ctx, spmc_ep_info);
3500f14d02fSMax Shvetsov 
351f2dcf418SOlivier Deprez 		/*
352f2dcf418SOlivier Deprez 		 * Pass the core linear ID to the SPMC through x4.
353f2dcf418SOlivier Deprez 		 * (TF-A implementation defined behavior helping
354f2dcf418SOlivier Deprez 		 * a legacy TOS migration to adopt FF-A).
355f2dcf418SOlivier Deprez 		 */
356f2dcf418SOlivier Deprez 		write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id);
357f2dcf418SOlivier Deprez 	}
3580f14d02fSMax Shvetsov 
359a334c4e6SOlivier Deprez 	/* Register power management hooks with PSCI */
360a334c4e6SOlivier Deprez 	psci_register_spd_pm_hook(&spmd_pm);
361a334c4e6SOlivier Deprez 
3620f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
3630f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
3640f14d02fSMax Shvetsov 
365f2dcf418SOlivier Deprez 	INFO("SPM Core setup done.\n");
366f2dcf418SOlivier Deprez 
3678cb99c3fSOlivier Deprez 	/*
3688cb99c3fSOlivier Deprez 	 * Register an interrupt handler routing secure interrupts to SPMD
3698cb99c3fSOlivier Deprez 	 * while the NWd is running.
3708cb99c3fSOlivier Deprez 	 */
3718cb99c3fSOlivier Deprez 	flags = 0;
3728cb99c3fSOlivier Deprez 	set_interrupt_rm_flag(flags, NON_SECURE);
3738cb99c3fSOlivier Deprez 	rc = register_interrupt_type_handler(INTR_TYPE_S_EL1,
3748cb99c3fSOlivier Deprez 					     spmd_secure_interrupt_handler,
3758cb99c3fSOlivier Deprez 					     flags);
3768cb99c3fSOlivier Deprez 	if (rc != 0) {
3778cb99c3fSOlivier Deprez 		panic();
3788cb99c3fSOlivier Deprez 	}
3798cb99c3fSOlivier Deprez 
3800f14d02fSMax Shvetsov 	return 0;
3810f14d02fSMax Shvetsov }
3820f14d02fSMax Shvetsov 
3830f14d02fSMax Shvetsov /*******************************************************************************
38452696946SOlivier Deprez  * Initialize context of SPM Core.
385bdd2596dSAchin Gupta  ******************************************************************************/
3860f14d02fSMax Shvetsov int spmd_setup(void)
387bdd2596dSAchin Gupta {
38823d5ba86SOlivier Deprez 	void *spmc_manifest;
389bdd2596dSAchin Gupta 	int rc;
390bdd2596dSAchin Gupta 
391bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
39252696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
39352696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
39452696946SOlivier Deprez 		return -EINVAL;
395bdd2596dSAchin Gupta 	}
396bdd2596dSAchin Gupta 
397bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
39852696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
399bdd2596dSAchin Gupta 
400bdd2596dSAchin Gupta 	/*
401bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
40252696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
403bdd2596dSAchin Gupta 	 */
40423d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
40523d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
40623d5ba86SOlivier Deprez 		ERROR("Invalid or absent SPM Core manifest.\n");
40723d5ba86SOlivier Deprez 		return -EINVAL;
408bdd2596dSAchin Gupta 	}
409bdd2596dSAchin Gupta 
4100f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
41123d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
4120f14d02fSMax Shvetsov 	if (rc != 0) {
41352696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
414bdd2596dSAchin Gupta 	}
415bdd2596dSAchin Gupta 
4160f14d02fSMax Shvetsov 	return rc;
4170f14d02fSMax Shvetsov }
4180f14d02fSMax Shvetsov 
4190f14d02fSMax Shvetsov /*******************************************************************************
4200f14d02fSMax Shvetsov  * Forward SMC to the other security state
4210f14d02fSMax Shvetsov  ******************************************************************************/
42252696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
42352696946SOlivier Deprez 				 bool secure_origin,
42452696946SOlivier Deprez 				 uint64_t x1,
42552696946SOlivier Deprez 				 uint64_t x2,
42652696946SOlivier Deprez 				 uint64_t x3,
42752696946SOlivier Deprez 				 uint64_t x4,
42852696946SOlivier Deprez 				 void *handle)
4290f14d02fSMax Shvetsov {
430c2901419SOlivier Deprez 	unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
431c2901419SOlivier Deprez 	unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
43293ff138bSOlivier Deprez 
4330f14d02fSMax Shvetsov 	/* Save incoming security state */
434033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
435678ce223SOlivier Deprez 	if (secure_state_in == NON_SECURE) {
436678ce223SOlivier Deprez 		cm_el1_sysregs_context_save(secure_state_in);
437678ce223SOlivier Deprez 	}
43893ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
439678ce223SOlivier Deprez #else
440678ce223SOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
441033039f8SMax Shvetsov #endif
4420f14d02fSMax Shvetsov 
4430f14d02fSMax Shvetsov 	/* Restore outgoing security state */
444033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
445678ce223SOlivier Deprez 	if (secure_state_out == NON_SECURE) {
446678ce223SOlivier Deprez 		cm_el1_sysregs_context_restore(secure_state_out);
447678ce223SOlivier Deprez 	}
44893ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
449678ce223SOlivier Deprez #else
450678ce223SOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
451033039f8SMax Shvetsov #endif
45293ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
4530f14d02fSMax Shvetsov 
45493ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
4550f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
4560f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
4570f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
4580f14d02fSMax Shvetsov }
4590f14d02fSMax Shvetsov 
4600f14d02fSMax Shvetsov /*******************************************************************************
461662af36dSJ-Alves  * Return FFA_ERROR with specified error code
4620f14d02fSMax Shvetsov  ******************************************************************************/
463662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code)
4640f14d02fSMax Shvetsov {
465e46b2fd2SJ-Alves 	SMC_RET8(handle, (uint32_t) FFA_ERROR,
466e46b2fd2SJ-Alves 		 FFA_TARGET_INFO_MBZ, (uint32_t)error_code,
467662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
468662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
469bdd2596dSAchin Gupta }
470bdd2596dSAchin Gupta 
471f0d743dbSOlivier Deprez /*******************************************************************************
472f0d743dbSOlivier Deprez  * spmd_check_address_in_binary_image
473f0d743dbSOlivier Deprez  ******************************************************************************/
474f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address)
475f0d743dbSOlivier Deprez {
476f0d743dbSOlivier Deprez 	assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size));
477f0d743dbSOlivier Deprez 
478f0d743dbSOlivier Deprez 	return ((address >= spmc_attrs.load_address) &&
479f0d743dbSOlivier Deprez 		(address < (spmc_attrs.load_address + spmc_attrs.binary_size)));
480f0d743dbSOlivier Deprez }
481f0d743dbSOlivier Deprez 
482c2901419SOlivier Deprez /******************************************************************************
483c2901419SOlivier Deprez  * spmd_is_spmc_message
484c2901419SOlivier Deprez  *****************************************************************************/
485c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep)
486c2901419SOlivier Deprez {
487c2901419SOlivier Deprez 	return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID)
488c2901419SOlivier Deprez 		&& (ffa_endpoint_source(ep) == spmc_attrs.spmc_id));
489c2901419SOlivier Deprez }
490c2901419SOlivier Deprez 
491f0d743dbSOlivier Deprez /******************************************************************************
492f0d743dbSOlivier Deprez  * spmd_handle_spmc_message
493f0d743dbSOlivier Deprez  *****************************************************************************/
494a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg,
495a92bc73bSOlivier Deprez 		unsigned long long parm1, unsigned long long parm2,
496a92bc73bSOlivier Deprez 		unsigned long long parm3, unsigned long long parm4)
497f0d743dbSOlivier Deprez {
498f0d743dbSOlivier Deprez 	VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__,
499f0d743dbSOlivier Deprez 		msg, parm1, parm2, parm3, parm4);
500f0d743dbSOlivier Deprez 
501f0d743dbSOlivier Deprez 	return -EINVAL;
502f0d743dbSOlivier Deprez }
503f0d743dbSOlivier Deprez 
504bdd2596dSAchin Gupta /*******************************************************************************
505662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
506bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
507bdd2596dSAchin Gupta  ******************************************************************************/
50852696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
50952696946SOlivier Deprez 			  uint64_t x1,
51052696946SOlivier Deprez 			  uint64_t x2,
51152696946SOlivier Deprez 			  uint64_t x3,
51252696946SOlivier Deprez 			  uint64_t x4,
51352696946SOlivier Deprez 			  void *cookie,
51452696946SOlivier Deprez 			  void *handle,
515bdd2596dSAchin Gupta 			  uint64_t flags)
516bdd2596dSAchin Gupta {
517cdb49d47SOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
51852696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
51993ff138bSOlivier Deprez 	bool secure_origin;
52093ff138bSOlivier Deprez 	int32_t ret;
5214388f28fSJ-Alves 	uint32_t input_version;
522bdd2596dSAchin Gupta 
523bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
52493ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
525bdd2596dSAchin Gupta 
5264ce3e99aSScott Branden 	VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64
5274ce3e99aSScott Branden 		" 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n",
528cdb49d47SOlivier Deprez 		    linear_id, smc_fid, x1, x2, x3, x4,
529cdb49d47SOlivier Deprez 		    SMC_GET_GP(handle, CTX_GPREG_X5),
530bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X6),
531bdd2596dSAchin Gupta 		    SMC_GET_GP(handle, CTX_GPREG_X7));
532bdd2596dSAchin Gupta 
533bdd2596dSAchin Gupta 	switch (smc_fid) {
534662af36dSJ-Alves 	case FFA_ERROR:
535bdd2596dSAchin Gupta 		/*
536bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
53752696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
538bdd2596dSAchin Gupta 		 * unsuccessfully.
539bdd2596dSAchin Gupta 		 */
5409dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
541bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
5420f14d02fSMax Shvetsov 		}
543bdd2596dSAchin Gupta 
54493ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5450f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
546bdd2596dSAchin Gupta 		break; /* not reached */
547bdd2596dSAchin Gupta 
548662af36dSJ-Alves 	case FFA_VERSION:
5494388f28fSJ-Alves 		input_version = (uint32_t)(0xFFFFFFFF & x1);
550bdd2596dSAchin Gupta 		/*
5514388f28fSJ-Alves 		 * If caller is secure and SPMC was initialized,
5524388f28fSJ-Alves 		 * return FFA_VERSION of SPMD.
5534388f28fSJ-Alves 		 * If caller is non secure and SPMC was initialized,
5544388f28fSJ-Alves 		 * return SPMC's version.
5554388f28fSJ-Alves 		 * Sanity check to "input_version".
556bdd2596dSAchin Gupta 		 */
5574388f28fSJ-Alves 		if ((input_version & FFA_VERSION_BIT31_MASK) ||
5584388f28fSJ-Alves 			(ctx->state == SPMC_STATE_RESET)) {
5594388f28fSJ-Alves 			ret = FFA_ERROR_NOT_SUPPORTED;
5604388f28fSJ-Alves 		} else if (!secure_origin) {
561*9944f557SDaniel Boulby 			gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx);
562*9944f557SDaniel Boulby 			uint64_t rc;
563*9944f557SDaniel Boulby 
564*9944f557SDaniel Boulby 			if (spmc_attrs.major_version == 1 &&
565*9944f557SDaniel Boulby 			    spmc_attrs.minor_version == 0) {
566e46b2fd2SJ-Alves 				ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
567e46b2fd2SJ-Alves 						       spmc_attrs.minor_version);
568*9944f557SDaniel Boulby 				SMC_RET8(handle, (uint32_t)ret,
569*9944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
570*9944f557SDaniel Boulby 					 FFA_TARGET_INFO_MBZ,
571*9944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
572*9944f557SDaniel Boulby 					 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
573*9944f557SDaniel Boulby 					 FFA_PARAM_MBZ);
574*9944f557SDaniel Boulby 				break;
575*9944f557SDaniel Boulby 			}
576*9944f557SDaniel Boulby 			/* Save non-secure system registers context */
577*9944f557SDaniel Boulby 			cm_el1_sysregs_context_save(NON_SECURE);
578*9944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2
579*9944f557SDaniel Boulby 			cm_el2_sysregs_context_save(NON_SECURE);
580*9944f557SDaniel Boulby #endif
581*9944f557SDaniel Boulby 
582*9944f557SDaniel Boulby 			/*
583*9944f557SDaniel Boulby 			 * The incoming request has FFA_VERSION as X0 smc_fid
584*9944f557SDaniel Boulby 			 * and requested version in x1. Prepare a direct request
585*9944f557SDaniel Boulby 			 * from SPMD to SPMC with FFA_VERSION framework function
586*9944f557SDaniel Boulby 			 * identifier in X2 and requested version in X3.
587*9944f557SDaniel Boulby 			 */
588*9944f557SDaniel Boulby 			spmd_build_spmc_message(gpregs,
589*9944f557SDaniel Boulby 						SPMD_FWK_MSG_FFA_VERSION_REQ,
590*9944f557SDaniel Boulby 						input_version);
591*9944f557SDaniel Boulby 
592*9944f557SDaniel Boulby 			rc = spmd_spm_core_sync_entry(ctx);
593*9944f557SDaniel Boulby 
594*9944f557SDaniel Boulby 			if ((rc != 0ULL) ||
595*9944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X0) !=
596*9944f557SDaniel Boulby 				FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
597*9944f557SDaniel Boulby 			    (SMC_GET_GP(gpregs, CTX_GPREG_X2) !=
598*9944f557SDaniel Boulby 				(SPMD_FWK_MSG_BIT |
599*9944f557SDaniel Boulby 				 SPMD_FWK_MSG_FFA_VERSION_RESP))) {
600*9944f557SDaniel Boulby 				ERROR("Failed to forward FFA_VERSION\n");
601*9944f557SDaniel Boulby 				ret = FFA_ERROR_NOT_SUPPORTED;
602*9944f557SDaniel Boulby 			} else {
603*9944f557SDaniel Boulby 				ret = SMC_GET_GP(gpregs, CTX_GPREG_X3);
604*9944f557SDaniel Boulby 			}
605*9944f557SDaniel Boulby 
606*9944f557SDaniel Boulby 			/*
607*9944f557SDaniel Boulby 			 * Return here after SPMC has handled FFA_VERSION.
608*9944f557SDaniel Boulby 			 * The returned SPMC version is held in X3.
609*9944f557SDaniel Boulby 			 * Forward this version in X0 to the non-secure caller.
610*9944f557SDaniel Boulby 			 */
611*9944f557SDaniel Boulby 			return spmd_smc_forward(ret, true, FFA_PARAM_MBZ,
612*9944f557SDaniel Boulby 						FFA_PARAM_MBZ, FFA_PARAM_MBZ,
613*9944f557SDaniel Boulby 						FFA_PARAM_MBZ, gpregs);
6144388f28fSJ-Alves 		} else {
615e46b2fd2SJ-Alves 			ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR,
616e46b2fd2SJ-Alves 					       FFA_VERSION_MINOR);
6174388f28fSJ-Alves 		}
6184388f28fSJ-Alves 
619e46b2fd2SJ-Alves 		SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ,
620e46b2fd2SJ-Alves 			 FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
621e46b2fd2SJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ);
622bdd2596dSAchin Gupta 		break; /* not reached */
623bdd2596dSAchin Gupta 
624662af36dSJ-Alves 	case FFA_FEATURES:
625bdd2596dSAchin Gupta 		/*
626bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
62752696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
628bdd2596dSAchin Gupta 		 */
629bdd2596dSAchin Gupta 
63052696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
63193ff138bSOlivier Deprez 		if (!secure_origin) {
63293ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
6330f14d02fSMax Shvetsov 						x1, x2, x3, x4, handle);
63452696946SOlivier Deprez 		}
63552696946SOlivier Deprez 
636bdd2596dSAchin Gupta 		/*
637bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
638662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
639bdd2596dSAchin Gupta 		 * nop.
640bdd2596dSAchin Gupta 		 */
641662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
642bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
643bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
644bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
6450f14d02fSMax Shvetsov 
646bdd2596dSAchin Gupta 		break; /* not reached */
647bdd2596dSAchin Gupta 
648662af36dSJ-Alves 	case FFA_ID_GET:
649ac03ac5eSMax Shvetsov 		/*
650662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
651ac03ac5eSMax Shvetsov 		 */
652ac03ac5eSMax Shvetsov 		if (!secure_origin) {
653662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
654662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
655662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
656662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
657662af36dSJ-Alves 				 FFA_PARAM_MBZ);
65852696946SOlivier Deprez 		}
65952696946SOlivier Deprez 
660662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
661662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
662662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
663662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
664662af36dSJ-Alves 			 FFA_PARAM_MBZ);
665ac03ac5eSMax Shvetsov 
666ac03ac5eSMax Shvetsov 		break; /* not reached */
667ac03ac5eSMax Shvetsov 
668cdb49d47SOlivier Deprez 	case FFA_SECONDARY_EP_REGISTER_SMC64:
669cdb49d47SOlivier Deprez 		if (secure_origin) {
670cdb49d47SOlivier Deprez 			ret = spmd_pm_secondary_ep_register(x1);
671cdb49d47SOlivier Deprez 
672cdb49d47SOlivier Deprez 			if (ret < 0) {
673cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_ERROR_SMC64,
674cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, ret,
675cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
676cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
677cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
678cdb49d47SOlivier Deprez 			} else {
679cdb49d47SOlivier Deprez 				SMC_RET8(handle, FFA_SUCCESS_SMC64,
680cdb49d47SOlivier Deprez 					FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ,
681cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
682cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ, FFA_PARAM_MBZ,
683cdb49d47SOlivier Deprez 					FFA_PARAM_MBZ);
684cdb49d47SOlivier Deprez 			}
685cdb49d47SOlivier Deprez 		}
686cdb49d47SOlivier Deprez 
687cdb49d47SOlivier Deprez 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
688cdb49d47SOlivier Deprez 		break; /* Not reached */
689cdb49d47SOlivier Deprez 
69070c121a2SDaniel Boulby 	case FFA_SPM_ID_GET:
69170c121a2SDaniel Boulby 		if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) {
69270c121a2SDaniel Boulby 			return spmd_ffa_error_return(handle,
69370c121a2SDaniel Boulby 						     FFA_ERROR_NOT_SUPPORTED);
69470c121a2SDaniel Boulby 		}
69570c121a2SDaniel Boulby 		/*
69670c121a2SDaniel Boulby 		 * Returns the ID of the SPMC or SPMD depending on the FF-A
69770c121a2SDaniel Boulby 		 * instance where this function is invoked
69870c121a2SDaniel Boulby 		 */
69970c121a2SDaniel Boulby 		if (!secure_origin) {
70070c121a2SDaniel Boulby 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
70170c121a2SDaniel Boulby 				 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
70270c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
70370c121a2SDaniel Boulby 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
70470c121a2SDaniel Boulby 				 FFA_PARAM_MBZ);
70570c121a2SDaniel Boulby 		}
70670c121a2SDaniel Boulby 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
70770c121a2SDaniel Boulby 			 FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID,
70870c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
70970c121a2SDaniel Boulby 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
71070c121a2SDaniel Boulby 			 FFA_PARAM_MBZ);
71170c121a2SDaniel Boulby 
71270c121a2SDaniel Boulby 		break; /* not reached */
71370c121a2SDaniel Boulby 
714f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
715f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
716f0d743dbSOlivier Deprez 			ret = spmd_handle_spmc_message(x3, x4,
717f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X5),
718f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X6),
719f0d743dbSOlivier Deprez 				SMC_GET_GP(handle, CTX_GPREG_X7));
720f0d743dbSOlivier Deprez 
721f0d743dbSOlivier Deprez 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
722f0d743dbSOlivier Deprez 				FFA_TARGET_INFO_MBZ, ret,
723f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
724f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ, FFA_PARAM_MBZ,
725f0d743dbSOlivier Deprez 				FFA_PARAM_MBZ);
726f0d743dbSOlivier Deprez 		} else {
727f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
728f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
729f0d743dbSOlivier Deprez 				x1, x2, x3, x4, handle);
730f0d743dbSOlivier Deprez 		}
731f0d743dbSOlivier Deprez 		break; /* Not reached */
732f0d743dbSOlivier Deprez 
733f0d743dbSOlivier Deprez 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
734f0d743dbSOlivier Deprez 		if (secure_origin && spmd_is_spmc_message(x1)) {
7358cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
736f0d743dbSOlivier Deprez 		} else {
737f0d743dbSOlivier Deprez 			/* Forward direct message to the other world */
738f0d743dbSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
739f0d743dbSOlivier Deprez 				x1, x2, x3, x4, handle);
740f0d743dbSOlivier Deprez 		}
741f0d743dbSOlivier Deprez 		break; /* Not reached */
742f0d743dbSOlivier Deprez 
743662af36dSJ-Alves 	case FFA_RX_RELEASE:
744662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
745662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
746662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
747545b8eb3SRuari Phipps 	case FFA_PARTITION_INFO_GET:
748fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED
749fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_CREATE:
750fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BITMAP_DESTROY:
751fc3f4800SJ-Alves 	case FFA_NOTIFICATION_BIND:
752fc3f4800SJ-Alves 	case FFA_NOTIFICATION_UNBIND:
753fc3f4800SJ-Alves 	case FFA_NOTIFICATION_SET:
754fc3f4800SJ-Alves 	case FFA_NOTIFICATION_GET:
755fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET:
756fc3f4800SJ-Alves 	case FFA_NOTIFICATION_INFO_GET_SMC64:
757fc3f4800SJ-Alves #endif
758545b8eb3SRuari Phipps 		/*
759fc3f4800SJ-Alves 		 * Above calls should not be forwarded from Secure world to
760fc3f4800SJ-Alves 		 * Normal world.
761545b8eb3SRuari Phipps 		 *
762545b8eb3SRuari Phipps 		 * Fall through to forward the call to the other world
763545b8eb3SRuari Phipps 		 */
764662af36dSJ-Alves 	case FFA_MSG_RUN:
765bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Normal world */
766545b8eb3SRuari Phipps 
76793ff138bSOlivier Deprez 		if (secure_origin) {
768662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
769662af36dSJ-Alves 						     FFA_ERROR_NOT_SUPPORTED);
770bdd2596dSAchin Gupta 		}
771bdd2596dSAchin Gupta 
772bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
773662af36dSJ-Alves 	case FFA_MSG_SEND:
774662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
775662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
776662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
777662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
778662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
779662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
780662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
781662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
782662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
783662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
784662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
785662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
786662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
787662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
788662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
789bdd2596dSAchin Gupta 		/*
790bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
791bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
792bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
793bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
794bdd2596dSAchin Gupta 		 */
795bdd2596dSAchin Gupta 
79693ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
7970f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
798bdd2596dSAchin Gupta 		break; /* not reached */
799bdd2596dSAchin Gupta 
800662af36dSJ-Alves 	case FFA_MSG_WAIT:
801bdd2596dSAchin Gupta 		/*
802bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
803bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
80452696946SOlivier Deprez 		 * SPM Core initialised successfully.
805bdd2596dSAchin Gupta 		 */
8069dcf63ddSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) {
8078cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
808bdd2596dSAchin Gupta 		}
809bdd2596dSAchin Gupta 
8100f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
811386dc365SOlivier Deprez 	case FFA_INTERRUPT:
812662af36dSJ-Alves 	case FFA_MSG_YIELD:
813bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
81493ff138bSOlivier Deprez 		if (!secure_origin) {
815662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
816662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
817bdd2596dSAchin Gupta 		}
818bdd2596dSAchin Gupta 
81993ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
8200f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
821bdd2596dSAchin Gupta 		break; /* not reached */
822bdd2596dSAchin Gupta 
8238cb99c3fSOlivier Deprez 	case FFA_NORMAL_WORLD_RESUME:
8248cb99c3fSOlivier Deprez 		if (secure_origin && ctx->secure_interrupt_ongoing) {
8258cb99c3fSOlivier Deprez 			spmd_spm_core_sync_exit(0ULL);
8268cb99c3fSOlivier Deprez 		} else {
8278cb99c3fSOlivier Deprez 			return spmd_ffa_error_return(handle, FFA_ERROR_DENIED);
8288cb99c3fSOlivier Deprez 		}
8298cb99c3fSOlivier Deprez 		break; /* Not reached */
8308cb99c3fSOlivier Deprez 
831bdd2596dSAchin Gupta 	default:
832bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
833662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
834bdd2596dSAchin Gupta 	}
835bdd2596dSAchin Gupta }
836