xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 662af36d9ca38eec53eed8613b916d12f8442e3e)
1bdd2596dSAchin Gupta /*
2bdd2596dSAchin Gupta  * Copyright (c) 2020, 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>
9bdd2596dSAchin Gupta #include <string.h>
10bdd2596dSAchin Gupta 
11bdd2596dSAchin Gupta #include <arch_helpers.h>
1252696946SOlivier Deprez #include <arch/aarch64/arch_features.h>
13bdd2596dSAchin Gupta #include <bl31/bl31.h>
14bdd2596dSAchin Gupta #include <common/debug.h>
15bdd2596dSAchin Gupta #include <common/runtime_svc.h>
16bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h>
17bdd2596dSAchin Gupta #include <lib/smccc.h>
18bdd2596dSAchin Gupta #include <lib/spinlock.h>
19bdd2596dSAchin Gupta #include <lib/utils.h>
20bdd2596dSAchin Gupta #include <plat/common/common_def.h>
21bdd2596dSAchin Gupta #include <plat/common/platform.h>
22bdd2596dSAchin Gupta #include <platform_def.h>
23*662af36dSJ-Alves #include <services/ffa_svc.h>
24bdd2596dSAchin Gupta #include <services/spmd_svc.h>
25bdd2596dSAchin Gupta #include <smccc_helpers.h>
26bdd2596dSAchin Gupta #include "spmd_private.h"
27bdd2596dSAchin Gupta 
28bdd2596dSAchin Gupta /*******************************************************************************
29bdd2596dSAchin Gupta  * SPM Core context information.
30bdd2596dSAchin Gupta  ******************************************************************************/
3152696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT];
32bdd2596dSAchin Gupta 
33bdd2596dSAchin Gupta /*******************************************************************************
34bdd2596dSAchin Gupta  * SPM Core attribute information read from its manifest.
35bdd2596dSAchin Gupta  ******************************************************************************/
3652696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs;
370f14d02fSMax Shvetsov 
380f14d02fSMax Shvetsov /*******************************************************************************
390f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
400f14d02fSMax Shvetsov  * on secondary cores.
410f14d02fSMax Shvetsov  ******************************************************************************/
420f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
430f14d02fSMax Shvetsov 
440f14d02fSMax Shvetsov /*******************************************************************************
4552696946SOlivier Deprez  * SPM Core context on current CPU get helper.
4652696946SOlivier Deprez  ******************************************************************************/
4752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void)
4852696946SOlivier Deprez {
4952696946SOlivier Deprez 	unsigned int linear_id = plat_my_core_pos();
5052696946SOlivier Deprez 
5152696946SOlivier Deprez 	return &spm_core_context[linear_id];
5252696946SOlivier Deprez }
5352696946SOlivier Deprez 
5452696946SOlivier Deprez /*******************************************************************************
550f14d02fSMax Shvetsov  * Static function declaration.
560f14d02fSMax Shvetsov  ******************************************************************************/
570f14d02fSMax Shvetsov static int32_t spmd_init(void);
5823d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr);
59*662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle,
6052696946SOlivier Deprez 				       int error_code);
6152696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
6252696946SOlivier Deprez 				 bool secure_origin,
6352696946SOlivier Deprez 				 uint64_t x1,
6452696946SOlivier Deprez 				 uint64_t x2,
6552696946SOlivier Deprez 				 uint64_t x3,
6652696946SOlivier Deprez 				 uint64_t x4,
6752696946SOlivier Deprez 				 void *handle);
68bdd2596dSAchin Gupta 
69bdd2596dSAchin Gupta /*******************************************************************************
7052696946SOlivier Deprez  * This function takes an SPMC context pointer and performs a synchronous
7152696946SOlivier Deprez  * SPMC entry.
72bdd2596dSAchin Gupta  ******************************************************************************/
73bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
74bdd2596dSAchin Gupta {
75bdd2596dSAchin Gupta 	uint64_t rc;
76bdd2596dSAchin Gupta 
77bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
78bdd2596dSAchin Gupta 
79bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
80bdd2596dSAchin Gupta 
81bdd2596dSAchin Gupta 	/* Restore the context assigned above */
82bdd2596dSAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
83033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
8428f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
85033039f8SMax Shvetsov #endif
86bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
87bdd2596dSAchin Gupta 
88033039f8SMax Shvetsov 	/* Enter SPMC */
89bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
90bdd2596dSAchin Gupta 
91bdd2596dSAchin Gupta 	/* Save secure state */
92bdd2596dSAchin Gupta 	cm_el1_sysregs_context_save(SECURE);
93033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
9428f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
95033039f8SMax Shvetsov #endif
96bdd2596dSAchin Gupta 
97bdd2596dSAchin Gupta 	return rc;
98bdd2596dSAchin Gupta }
99bdd2596dSAchin Gupta 
100bdd2596dSAchin Gupta /*******************************************************************************
10152696946SOlivier Deprez  * This function returns to the place where spmd_spm_core_sync_entry() was
102bdd2596dSAchin Gupta  * called originally.
103bdd2596dSAchin Gupta  ******************************************************************************/
104bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
105bdd2596dSAchin Gupta {
10652696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
107bdd2596dSAchin Gupta 
10852696946SOlivier Deprez 	/* Get current CPU context from SPMC context */
109bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
110bdd2596dSAchin Gupta 
111bdd2596dSAchin Gupta 	/*
112bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
113bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
114bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
115bdd2596dSAchin Gupta 	 */
116bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
117bdd2596dSAchin Gupta 
118bdd2596dSAchin Gupta 	panic();
119bdd2596dSAchin Gupta }
120bdd2596dSAchin Gupta 
121bdd2596dSAchin Gupta /*******************************************************************************
12252696946SOlivier Deprez  * Jump to the SPM Core for the first time.
123bdd2596dSAchin Gupta  ******************************************************************************/
124bdd2596dSAchin Gupta static int32_t spmd_init(void)
125bdd2596dSAchin Gupta {
12652696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
12752696946SOlivier Deprez 	uint64_t rc;
128bdd2596dSAchin Gupta 
12952696946SOlivier Deprez 	VERBOSE("SPM Core init start.\n");
130bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_RESET;
131bdd2596dSAchin Gupta 
132bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
13352696946SOlivier Deprez 	if (rc != 0ULL) {
134bdd2596dSAchin Gupta 		ERROR("SPMC initialisation failed 0x%llx\n", rc);
13552696946SOlivier Deprez 		return 0;
136bdd2596dSAchin Gupta 	}
137bdd2596dSAchin Gupta 
138bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_IDLE;
13952696946SOlivier Deprez 	VERBOSE("SPM Core init end.\n");
140bdd2596dSAchin Gupta 
141bdd2596dSAchin Gupta 	return 1;
142bdd2596dSAchin Gupta }
143bdd2596dSAchin Gupta 
144bdd2596dSAchin Gupta /*******************************************************************************
14552696946SOlivier Deprez  * Loads SPMC manifest and inits SPMC.
1460f14d02fSMax Shvetsov  ******************************************************************************/
14723d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr)
1480f14d02fSMax Shvetsov {
14952696946SOlivier Deprez 	spmd_spm_core_context_t *spm_ctx = spmd_get_context();
1500f14d02fSMax Shvetsov 	uint32_t ep_attr;
15152696946SOlivier Deprez 	int rc;
1520f14d02fSMax Shvetsov 
15352696946SOlivier Deprez 	/* Load the SPM Core manifest */
15423d5ba86SOlivier Deprez 	rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr);
1550f14d02fSMax Shvetsov 	if (rc != 0) {
15652696946SOlivier Deprez 		WARN("No or invalid SPM Core manifest image provided by BL2\n");
15752696946SOlivier Deprez 		return rc;
1580f14d02fSMax Shvetsov 	}
1590f14d02fSMax Shvetsov 
1600f14d02fSMax Shvetsov 	/*
16152696946SOlivier Deprez 	 * Ensure that the SPM Core version is compatible with the SPM
16252696946SOlivier Deprez 	 * Dispatcher version.
1630f14d02fSMax Shvetsov 	 */
164*662af36dSJ-Alves 	if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) ||
165*662af36dSJ-Alves 	    (spmc_attrs.minor_version > FFA_VERSION_MINOR)) {
166*662af36dSJ-Alves 		WARN("Unsupported FFA version (%u.%u)\n",
1670f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
16852696946SOlivier Deprez 		return -EINVAL;
1690f14d02fSMax Shvetsov 	}
1700f14d02fSMax Shvetsov 
171*662af36dSJ-Alves 	VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version,
1720f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
1730f14d02fSMax Shvetsov 
17452696946SOlivier Deprez 	VERBOSE("SPM Core run time EL%x.\n",
175033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
1760f14d02fSMax Shvetsov 
177ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
17852696946SOlivier Deprez 	if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
17952696946SOlivier Deprez 			SPMC_SECURE_ID_MASK) == 0U) {
18052696946SOlivier Deprez 		WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id);
18152696946SOlivier Deprez 		return -EINVAL;
182ac03ac5eSMax Shvetsov 	}
183ac03ac5eSMax Shvetsov 
18452696946SOlivier Deprez 	/* Validate the SPM Core execution state */
1850f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
1860f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
18723d5ba86SOlivier Deprez 		WARN("Unsupported %s%x.\n", "SPM Core execution state 0x",
1880f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
18952696946SOlivier Deprez 		return -EINVAL;
1900f14d02fSMax Shvetsov 	}
1910f14d02fSMax Shvetsov 
19223d5ba86SOlivier Deprez 	VERBOSE("%s%x.\n", "SPM Core execution state 0x",
19323d5ba86SOlivier Deprez 		spmc_attrs.exec_state);
1940f14d02fSMax Shvetsov 
195033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
196033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
197033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
198033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
19952696946SOlivier Deprez 		return -EINVAL;
2000f14d02fSMax Shvetsov 	}
2010f14d02fSMax Shvetsov 
2020f14d02fSMax Shvetsov 	/*
2030f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
2040f14d02fSMax Shvetsov 	 * is required for SPM
2050f14d02fSMax Shvetsov 	 */
20652696946SOlivier Deprez 	if (!is_armv8_4_sel2_present()) {
20752696946SOlivier Deprez 		WARN("SPM Core run time S-EL2 is not supported.\n");
20852696946SOlivier Deprez 		return -EINVAL;
2090f14d02fSMax Shvetsov 	}
210033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
2110f14d02fSMax Shvetsov 
2120f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
2130f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
21452696946SOlivier Deprez 	if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) {
2150f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
2160f14d02fSMax Shvetsov 	}
2170f14d02fSMax Shvetsov 
2180f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
2190f14d02fSMax Shvetsov 	assert(spmc_ep_info->pc == BL32_BASE);
2200f14d02fSMax Shvetsov 
2210f14d02fSMax Shvetsov 	/*
22252696946SOlivier Deprez 	 * Populate SPSR for SPM Core based upon validated parameters from the
22352696946SOlivier Deprez 	 * manifest.
2240f14d02fSMax Shvetsov 	 */
2250f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
2260f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
2270f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
2280f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
2290f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
2300f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
2310f14d02fSMax Shvetsov 	} else {
232033039f8SMax Shvetsov 
233033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
234033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
235033039f8SMax Shvetsov #else
236033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
237033039f8SMax Shvetsov #endif
238033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
2390f14d02fSMax Shvetsov 					     MODE_SP_ELX,
2400f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
2410f14d02fSMax Shvetsov 	}
2420f14d02fSMax Shvetsov 
24352696946SOlivier Deprez 	/* Initialise SPM Core context with this entry point information */
2440f14d02fSMax Shvetsov 	cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info);
2450f14d02fSMax Shvetsov 
2460f14d02fSMax Shvetsov 	/* Reuse PSCI affinity states to mark this SPMC context as off */
2470f14d02fSMax Shvetsov 	spm_ctx->state = AFF_STATE_OFF;
2480f14d02fSMax Shvetsov 
24952696946SOlivier Deprez 	INFO("SPM Core setup done.\n");
2500f14d02fSMax Shvetsov 
2510f14d02fSMax Shvetsov 	/* Register init function for deferred init. */
2520f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
2530f14d02fSMax Shvetsov 
2540f14d02fSMax Shvetsov 	return 0;
2550f14d02fSMax Shvetsov }
2560f14d02fSMax Shvetsov 
2570f14d02fSMax Shvetsov /*******************************************************************************
25852696946SOlivier Deprez  * Initialize context of SPM Core.
259bdd2596dSAchin Gupta  ******************************************************************************/
2600f14d02fSMax Shvetsov int spmd_setup(void)
261bdd2596dSAchin Gupta {
26223d5ba86SOlivier Deprez 	void *spmc_manifest;
263bdd2596dSAchin Gupta 	int rc;
264bdd2596dSAchin Gupta 
265bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
26652696946SOlivier Deprez 	if (spmc_ep_info == NULL) {
26752696946SOlivier Deprez 		WARN("No SPM Core image provided by BL2 boot loader.\n");
26852696946SOlivier Deprez 		return -EINVAL;
269bdd2596dSAchin Gupta 	}
270bdd2596dSAchin Gupta 
271bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
27252696946SOlivier Deprez 	assert(spmc_ep_info->pc != 0ULL);
273bdd2596dSAchin Gupta 
274bdd2596dSAchin Gupta 	/*
275bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
27652696946SOlivier Deprez 	 * be used as a manifest for the SPM Core at the next lower EL/mode.
277bdd2596dSAchin Gupta 	 */
27823d5ba86SOlivier Deprez 	spmc_manifest = (void *)spmc_ep_info->args.arg0;
27923d5ba86SOlivier Deprez 	if (spmc_manifest == NULL) {
28023d5ba86SOlivier Deprez 		ERROR("Invalid or absent SPM Core manifest.\n");
28123d5ba86SOlivier Deprez 		return -EINVAL;
282bdd2596dSAchin Gupta 	}
283bdd2596dSAchin Gupta 
2840f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
28523d5ba86SOlivier Deprez 	rc = spmd_spmc_init(spmc_manifest);
2860f14d02fSMax Shvetsov 	if (rc != 0) {
28752696946SOlivier Deprez 		WARN("Booting device without SPM initialization.\n");
288bdd2596dSAchin Gupta 	}
289bdd2596dSAchin Gupta 
2900f14d02fSMax Shvetsov 	return rc;
2910f14d02fSMax Shvetsov }
2920f14d02fSMax Shvetsov 
2930f14d02fSMax Shvetsov /*******************************************************************************
2940f14d02fSMax Shvetsov  * Forward SMC to the other security state
2950f14d02fSMax Shvetsov  ******************************************************************************/
29652696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid,
29752696946SOlivier Deprez 				 bool secure_origin,
29852696946SOlivier Deprez 				 uint64_t x1,
29952696946SOlivier Deprez 				 uint64_t x2,
30052696946SOlivier Deprez 				 uint64_t x3,
30152696946SOlivier Deprez 				 uint64_t x4,
30252696946SOlivier Deprez 				 void *handle)
3030f14d02fSMax Shvetsov {
30493ff138bSOlivier Deprez 	uint32_t secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
30593ff138bSOlivier Deprez 	uint32_t secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
30693ff138bSOlivier Deprez 
3070f14d02fSMax Shvetsov 	/* Save incoming security state */
30893ff138bSOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
309033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
31093ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
311033039f8SMax Shvetsov #endif
3120f14d02fSMax Shvetsov 
3130f14d02fSMax Shvetsov 	/* Restore outgoing security state */
31493ff138bSOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
315033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
31693ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
317033039f8SMax Shvetsov #endif
31893ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
3190f14d02fSMax Shvetsov 
32093ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
3210f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
3220f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
3230f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
3240f14d02fSMax Shvetsov }
3250f14d02fSMax Shvetsov 
3260f14d02fSMax Shvetsov /*******************************************************************************
327*662af36dSJ-Alves  * Return FFA_ERROR with specified error code
3280f14d02fSMax Shvetsov  ******************************************************************************/
329*662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code)
3300f14d02fSMax Shvetsov {
331*662af36dSJ-Alves 	SMC_RET8(handle, FFA_ERROR,
332*662af36dSJ-Alves 		 FFA_TARGET_INFO_MBZ, error_code,
333*662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
334*662af36dSJ-Alves 		 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
335bdd2596dSAchin Gupta }
336bdd2596dSAchin Gupta 
337bdd2596dSAchin Gupta /*******************************************************************************
338*662af36dSJ-Alves  * This function handles all SMCs in the range reserved for FFA. Each call is
339bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
340bdd2596dSAchin Gupta  ******************************************************************************/
34152696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid,
34252696946SOlivier Deprez 			  uint64_t x1,
34352696946SOlivier Deprez 			  uint64_t x2,
34452696946SOlivier Deprez 			  uint64_t x3,
34552696946SOlivier Deprez 			  uint64_t x4,
34652696946SOlivier Deprez 			  void *cookie,
34752696946SOlivier Deprez 			  void *handle,
348bdd2596dSAchin Gupta 			  uint64_t flags)
349bdd2596dSAchin Gupta {
35052696946SOlivier Deprez 	spmd_spm_core_context_t *ctx = spmd_get_context();
35193ff138bSOlivier Deprez 	bool secure_origin;
35293ff138bSOlivier Deprez 	int32_t ret;
353bdd2596dSAchin Gupta 
354bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
35593ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
356bdd2596dSAchin Gupta 
35752696946SOlivier Deprez 	INFO("SPM: 0x%x 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
358bdd2596dSAchin Gupta 	     smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5),
359bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X6),
360bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X7));
361bdd2596dSAchin Gupta 
362bdd2596dSAchin Gupta 	switch (smc_fid) {
363*662af36dSJ-Alves 	case FFA_ERROR:
364bdd2596dSAchin Gupta 		/*
365bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
36652696946SOlivier Deprez 		 * this CPU. If so, then indicate that the SPM Core initialised
367bdd2596dSAchin Gupta 		 * unsuccessfully.
368bdd2596dSAchin Gupta 		 */
36993ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
370bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
3710f14d02fSMax Shvetsov 		}
372bdd2596dSAchin Gupta 
37393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
3740f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
375bdd2596dSAchin Gupta 		break; /* not reached */
376bdd2596dSAchin Gupta 
377*662af36dSJ-Alves 	case FFA_VERSION:
378bdd2596dSAchin Gupta 		/*
379bdd2596dSAchin Gupta 		 * TODO: This is an optimization that the version information
38052696946SOlivier Deprez 		 * provided by the SPM Core manifest is returned by the SPM
381bdd2596dSAchin Gupta 		 * dispatcher. It might be a better idea to simply forward this
38252696946SOlivier Deprez 		 * call to the SPM Core and wash our hands completely.
383bdd2596dSAchin Gupta 		 */
384*662af36dSJ-Alves 		ret = MAKE_FFA_VERSION(spmc_attrs.major_version,
385bdd2596dSAchin Gupta 					spmc_attrs.minor_version);
386*662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, FFA_TARGET_INFO_MBZ, ret,
387*662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ,
388*662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ);
389bdd2596dSAchin Gupta 		break; /* not reached */
390bdd2596dSAchin Gupta 
391*662af36dSJ-Alves 	case FFA_FEATURES:
392bdd2596dSAchin Gupta 		/*
393bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
39452696946SOlivier Deprez 		 * forward to SPM Core which will handle it if implemented.
395bdd2596dSAchin Gupta 		 */
396bdd2596dSAchin Gupta 
397bdd2596dSAchin Gupta 		/*
398*662af36dSJ-Alves 		 * Check if x1 holds a valid FFA fid. This is an
399bdd2596dSAchin Gupta 		 * optimization.
400bdd2596dSAchin Gupta 		 */
401*662af36dSJ-Alves 		if (!is_ffa_fid(x1)) {
402*662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
403*662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
4040f14d02fSMax Shvetsov 		}
405bdd2596dSAchin Gupta 
40652696946SOlivier Deprez 		/* Forward SMC from Normal world to the SPM Core */
40793ff138bSOlivier Deprez 		if (!secure_origin) {
40893ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
4090f14d02fSMax Shvetsov 						x1, x2, x3, x4, handle);
41052696946SOlivier Deprez 		}
41152696946SOlivier Deprez 
412bdd2596dSAchin Gupta 		/*
413bdd2596dSAchin Gupta 		 * Return success if call was from secure world i.e. all
414*662af36dSJ-Alves 		 * FFA functions are supported. This is essentially a
415bdd2596dSAchin Gupta 		 * nop.
416bdd2596dSAchin Gupta 		 */
417*662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4,
418bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X5),
419bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X6),
420bdd2596dSAchin Gupta 			 SMC_GET_GP(handle, CTX_GPREG_X7));
4210f14d02fSMax Shvetsov 
422bdd2596dSAchin Gupta 		break; /* not reached */
423bdd2596dSAchin Gupta 
424*662af36dSJ-Alves 	case FFA_ID_GET:
425ac03ac5eSMax Shvetsov 		/*
426*662af36dSJ-Alves 		 * Returns the ID of the calling FFA component.
427ac03ac5eSMax Shvetsov 		 */
428ac03ac5eSMax Shvetsov 		if (!secure_origin) {
429*662af36dSJ-Alves 			SMC_RET8(handle, FFA_SUCCESS_SMC32,
430*662af36dSJ-Alves 				 FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID,
431*662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
432*662af36dSJ-Alves 				 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
433*662af36dSJ-Alves 				 FFA_PARAM_MBZ);
43452696946SOlivier Deprez 		}
43552696946SOlivier Deprez 
436*662af36dSJ-Alves 		SMC_RET8(handle, FFA_SUCCESS_SMC32,
437*662af36dSJ-Alves 			 FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
438*662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
439*662af36dSJ-Alves 			 FFA_PARAM_MBZ, FFA_PARAM_MBZ,
440*662af36dSJ-Alves 			 FFA_PARAM_MBZ);
441ac03ac5eSMax Shvetsov 
442ac03ac5eSMax Shvetsov 		break; /* not reached */
443ac03ac5eSMax Shvetsov 
444*662af36dSJ-Alves 	case FFA_RX_RELEASE:
445*662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC32:
446*662af36dSJ-Alves 	case FFA_RXTX_MAP_SMC64:
447*662af36dSJ-Alves 	case FFA_RXTX_UNMAP:
448*662af36dSJ-Alves 	case FFA_MSG_RUN:
449bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Normal world */
45093ff138bSOlivier Deprez 		if (secure_origin) {
451*662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
452*662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
453bdd2596dSAchin Gupta 		}
454bdd2596dSAchin Gupta 
455bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
456bdd2596dSAchin Gupta 
457*662af36dSJ-Alves 	case FFA_PARTITION_INFO_GET:
458*662af36dSJ-Alves 	case FFA_MSG_SEND:
459*662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_REQ_SMC32:
460*662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_REQ_SMC64:
461*662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_RESP_SMC32:
462*662af36dSJ-Alves 	case FFA_MSG_SEND_DIRECT_RESP_SMC64:
463*662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC32:
464*662af36dSJ-Alves 	case FFA_MEM_DONATE_SMC64:
465*662af36dSJ-Alves 	case FFA_MEM_LEND_SMC32:
466*662af36dSJ-Alves 	case FFA_MEM_LEND_SMC64:
467*662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC32:
468*662af36dSJ-Alves 	case FFA_MEM_SHARE_SMC64:
469*662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC32:
470*662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_REQ_SMC64:
471*662af36dSJ-Alves 	case FFA_MEM_RETRIEVE_RESP:
472*662af36dSJ-Alves 	case FFA_MEM_RELINQUISH:
473*662af36dSJ-Alves 	case FFA_MEM_RECLAIM:
474*662af36dSJ-Alves 	case FFA_SUCCESS_SMC32:
475*662af36dSJ-Alves 	case FFA_SUCCESS_SMC64:
476bdd2596dSAchin Gupta 		/*
477bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
478bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
479bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
480bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
481bdd2596dSAchin Gupta 		 */
482bdd2596dSAchin Gupta 
48393ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
4840f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
485bdd2596dSAchin Gupta 		break; /* not reached */
486bdd2596dSAchin Gupta 
487*662af36dSJ-Alves 	case FFA_MSG_WAIT:
488bdd2596dSAchin Gupta 		/*
489bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
490bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
49152696946SOlivier Deprez 		 * SPM Core initialised successfully.
492bdd2596dSAchin Gupta 		 */
49393ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
494bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(0);
495bdd2596dSAchin Gupta 		}
496bdd2596dSAchin Gupta 
4970f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
498bdd2596dSAchin Gupta 
499*662af36dSJ-Alves 	case FFA_MSG_YIELD:
500bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
50193ff138bSOlivier Deprez 		if (!secure_origin) {
502*662af36dSJ-Alves 			return spmd_ffa_error_return(handle,
503*662af36dSJ-Alves 						      FFA_ERROR_NOT_SUPPORTED);
504bdd2596dSAchin Gupta 		}
505bdd2596dSAchin Gupta 
50693ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5070f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
508bdd2596dSAchin Gupta 		break; /* not reached */
509bdd2596dSAchin Gupta 
510bdd2596dSAchin Gupta 	default:
511bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
512*662af36dSJ-Alves 		return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED);
513bdd2596dSAchin Gupta 	}
514bdd2596dSAchin Gupta }
515