xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 033039f8e5ad0ff231261e316f27bf22bc5713a2)
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>
12bdd2596dSAchin Gupta #include <bl31/bl31.h>
13bdd2596dSAchin Gupta #include <common/debug.h>
14bdd2596dSAchin Gupta #include <common/runtime_svc.h>
15bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h>
16bdd2596dSAchin Gupta #include <lib/smccc.h>
17bdd2596dSAchin Gupta #include <lib/spinlock.h>
18bdd2596dSAchin Gupta #include <lib/utils.h>
19bdd2596dSAchin Gupta #include <lib/xlat_tables/xlat_tables_v2.h>
20bdd2596dSAchin Gupta #include <plat/common/common_def.h>
21bdd2596dSAchin Gupta #include <plat/common/platform.h>
22bdd2596dSAchin Gupta #include <platform_def.h>
23bdd2596dSAchin Gupta #include <services/spci_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  ******************************************************************************/
31bdd2596dSAchin Gupta 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  ******************************************************************************/
360f14d02fSMax Shvetsov static spmc_manifest_sect_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 /*******************************************************************************
450f14d02fSMax Shvetsov  * Static function declaration.
460f14d02fSMax Shvetsov  ******************************************************************************/
470f14d02fSMax Shvetsov static int32_t	spmd_init(void);
480f14d02fSMax Shvetsov static int	spmd_spmc_init(void *rd_base, size_t rd_size);
490f14d02fSMax Shvetsov static uint64_t	spmd_spci_error_return(void *handle, int error_code);
5093ff138bSOlivier Deprez static uint64_t	spmd_smc_forward(uint32_t smc_fid, bool secure_origin,
5193ff138bSOlivier Deprez 				 uint64_t x1, uint64_t x2, uint64_t x3,
5293ff138bSOlivier Deprez 				 uint64_t x4, void *handle);
53bdd2596dSAchin Gupta 
54bdd2596dSAchin Gupta /*******************************************************************************
55bdd2596dSAchin Gupta  * This function takes an SP context pointer and performs a synchronous entry
56bdd2596dSAchin Gupta  * into it.
57bdd2596dSAchin Gupta  ******************************************************************************/
58bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
59bdd2596dSAchin Gupta {
60bdd2596dSAchin Gupta 	uint64_t rc;
61bdd2596dSAchin Gupta 
62bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
63bdd2596dSAchin Gupta 
64bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
65bdd2596dSAchin Gupta 
66bdd2596dSAchin Gupta 	/* Restore the context assigned above */
67bdd2596dSAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
68*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
6928f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
70*033039f8SMax Shvetsov #endif
71bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
72bdd2596dSAchin Gupta 
73*033039f8SMax Shvetsov 	/* Enter SPMC */
74bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
75bdd2596dSAchin Gupta 
76bdd2596dSAchin Gupta 	/* Save secure state */
77bdd2596dSAchin Gupta 	cm_el1_sysregs_context_save(SECURE);
78*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
7928f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
80*033039f8SMax Shvetsov #endif
81bdd2596dSAchin Gupta 
82bdd2596dSAchin Gupta 	return rc;
83bdd2596dSAchin Gupta }
84bdd2596dSAchin Gupta 
85bdd2596dSAchin Gupta /*******************************************************************************
86bdd2596dSAchin Gupta  * This function returns to the place where spm_sp_synchronous_entry() was
87bdd2596dSAchin Gupta  * called originally.
88bdd2596dSAchin Gupta  ******************************************************************************/
89bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
90bdd2596dSAchin Gupta {
91bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
92bdd2596dSAchin Gupta 
93bdd2596dSAchin Gupta 	/* Get context of the SP in use by this CPU. */
94bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
95bdd2596dSAchin Gupta 
96bdd2596dSAchin Gupta 	/*
97bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
98bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
99bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
100bdd2596dSAchin Gupta 	 */
101bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
102bdd2596dSAchin Gupta 
103bdd2596dSAchin Gupta 	panic();
104bdd2596dSAchin Gupta }
105bdd2596dSAchin Gupta 
106bdd2596dSAchin Gupta /*******************************************************************************
107bdd2596dSAchin Gupta  * Jump to the SPM core for the first time.
108bdd2596dSAchin Gupta  ******************************************************************************/
109bdd2596dSAchin Gupta static int32_t spmd_init(void)
110bdd2596dSAchin Gupta {
111bdd2596dSAchin Gupta 	uint64_t rc = 0;
112bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
113bdd2596dSAchin Gupta 
114bdd2596dSAchin Gupta 	INFO("SPM Core init start.\n");
115bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_RESET;
116bdd2596dSAchin Gupta 
117bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
118bdd2596dSAchin Gupta 	if (rc) {
119bdd2596dSAchin Gupta 		ERROR("SPMC initialisation failed 0x%llx\n", rc);
120bdd2596dSAchin Gupta 		panic();
121bdd2596dSAchin Gupta 	}
122bdd2596dSAchin Gupta 
123bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_IDLE;
124bdd2596dSAchin Gupta 	INFO("SPM Core init end.\n");
125bdd2596dSAchin Gupta 
126bdd2596dSAchin Gupta 	return 1;
127bdd2596dSAchin Gupta }
128bdd2596dSAchin Gupta 
129bdd2596dSAchin Gupta /*******************************************************************************
1300f14d02fSMax Shvetsov  * Load SPMC manifest, init SPMC.
1310f14d02fSMax Shvetsov  ******************************************************************************/
1320f14d02fSMax Shvetsov static int spmd_spmc_init(void *rd_base, size_t rd_size)
1330f14d02fSMax Shvetsov {
1340f14d02fSMax Shvetsov 	int rc;
1350f14d02fSMax Shvetsov 	uint32_t ep_attr;
1360f14d02fSMax Shvetsov 	unsigned int linear_id = plat_my_core_pos();
1370f14d02fSMax Shvetsov 	spmd_spm_core_context_t *spm_ctx = &spm_core_context[linear_id];
1380f14d02fSMax Shvetsov 
1390f14d02fSMax Shvetsov 	/* Load the SPM core manifest */
1400f14d02fSMax Shvetsov 	rc = plat_spm_core_manifest_load(&spmc_attrs, rd_base, rd_size);
1410f14d02fSMax Shvetsov 	if (rc != 0) {
1420f14d02fSMax Shvetsov 		WARN("No or invalid SPM core manifest image provided by BL2 "
1430f14d02fSMax Shvetsov 		     "boot loader. ");
1440f14d02fSMax Shvetsov 		return 1;
1450f14d02fSMax Shvetsov 	}
1460f14d02fSMax Shvetsov 
1470f14d02fSMax Shvetsov 	/*
1480f14d02fSMax Shvetsov 	 * Ensure that the SPM core version is compatible with the SPM
1490f14d02fSMax Shvetsov 	 * dispatcher version
1500f14d02fSMax Shvetsov 	 */
1510f14d02fSMax Shvetsov 	if ((spmc_attrs.major_version != SPCI_VERSION_MAJOR) ||
1520f14d02fSMax Shvetsov 	    (spmc_attrs.minor_version > SPCI_VERSION_MINOR)) {
1530f14d02fSMax Shvetsov 		WARN("Unsupported SPCI version (%x.%x) specified in SPM core "
1540f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
1550f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
1560f14d02fSMax Shvetsov 		return 1;
1570f14d02fSMax Shvetsov 	}
1580f14d02fSMax Shvetsov 
1590f14d02fSMax Shvetsov 	INFO("SPCI version (%x.%x).\n", spmc_attrs.major_version,
1600f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
1610f14d02fSMax Shvetsov 
162*033039f8SMax Shvetsov 	INFO("SPM core run time EL%x.\n",
163*033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
1640f14d02fSMax Shvetsov 
1650f14d02fSMax Shvetsov 	/* Validate the SPM core execution state */
1660f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
1670f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
1680f14d02fSMax Shvetsov 		WARN("Unsupported SPM core execution state %x specified in "
1690f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
1700f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
1710f14d02fSMax Shvetsov 		return 1;
1720f14d02fSMax Shvetsov 	}
1730f14d02fSMax Shvetsov 
1740f14d02fSMax Shvetsov 	INFO("SPM core execution state %x.\n", spmc_attrs.exec_state);
1750f14d02fSMax Shvetsov 
176*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
177*033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
178*033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
179*033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
1800f14d02fSMax Shvetsov 		return 1;
1810f14d02fSMax Shvetsov 	}
1820f14d02fSMax Shvetsov 
1830f14d02fSMax Shvetsov 	/*
1840f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
1850f14d02fSMax Shvetsov 	 * is required for SPM
1860f14d02fSMax Shvetsov 	 */
1870f14d02fSMax Shvetsov 	uint64_t sel2 = read_id_aa64pfr0_el1();
1880f14d02fSMax Shvetsov 
1890f14d02fSMax Shvetsov 	sel2 >>= ID_AA64PFR0_SEL2_SHIFT;
1900f14d02fSMax Shvetsov 	sel2 &= ID_AA64PFR0_SEL2_MASK;
1910f14d02fSMax Shvetsov 
1920f14d02fSMax Shvetsov 	if (!sel2) {
193*033039f8SMax Shvetsov 		WARN("SPM core run time S-EL2 is not supported.");
1940f14d02fSMax Shvetsov 		return 1;
1950f14d02fSMax Shvetsov 	}
196*033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
1970f14d02fSMax Shvetsov 
1980f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
1990f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
2000f14d02fSMax Shvetsov 	if (read_sctlr_el3() & SCTLR_EE_BIT) {
2010f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
2020f14d02fSMax Shvetsov 	}
2030f14d02fSMax Shvetsov 
2040f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
2050f14d02fSMax Shvetsov 	assert(spmc_ep_info->pc == BL32_BASE);
2060f14d02fSMax Shvetsov 
2070f14d02fSMax Shvetsov 	/*
2080f14d02fSMax Shvetsov 	 * Populate SPSR for SPM core based upon validated parameters from the
2090f14d02fSMax Shvetsov 	 * manifest
2100f14d02fSMax Shvetsov 	 */
2110f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
2120f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
2130f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
2140f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
2150f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
2160f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
2170f14d02fSMax Shvetsov 	} else {
218*033039f8SMax Shvetsov 
219*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
220*033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
221*033039f8SMax Shvetsov #else
222*033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
223*033039f8SMax Shvetsov #endif
224*033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
2250f14d02fSMax Shvetsov 					     MODE_SP_ELX,
2260f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
2270f14d02fSMax Shvetsov 	}
2280f14d02fSMax Shvetsov 
2290f14d02fSMax Shvetsov 	/* Initialise SPM core context with this entry point information */
2300f14d02fSMax Shvetsov 	cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info);
2310f14d02fSMax Shvetsov 
2320f14d02fSMax Shvetsov 	/* Reuse PSCI affinity states to mark this SPMC context as off */
2330f14d02fSMax Shvetsov 	spm_ctx->state = AFF_STATE_OFF;
2340f14d02fSMax Shvetsov 
2350f14d02fSMax Shvetsov 	INFO("SPM core setup done.\n");
2360f14d02fSMax Shvetsov 
2370f14d02fSMax Shvetsov 	/* Register init function for deferred init.  */
2380f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
2390f14d02fSMax Shvetsov 
2400f14d02fSMax Shvetsov 	return 0;
2410f14d02fSMax Shvetsov }
2420f14d02fSMax Shvetsov 
2430f14d02fSMax Shvetsov /*******************************************************************************
244bdd2596dSAchin Gupta  * Initialize context of SPM core.
245bdd2596dSAchin Gupta  ******************************************************************************/
2460f14d02fSMax Shvetsov int spmd_setup(void)
247bdd2596dSAchin Gupta {
248bdd2596dSAchin Gupta 	int rc;
249bdd2596dSAchin Gupta 	void *rd_base;
250bdd2596dSAchin Gupta 	size_t rd_size;
251bdd2596dSAchin Gupta 	uintptr_t rd_base_align;
252bdd2596dSAchin Gupta 	uintptr_t rd_size_align;
253bdd2596dSAchin Gupta 
254bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
255bdd2596dSAchin Gupta 	if (!spmc_ep_info) {
256bdd2596dSAchin Gupta 		WARN("No SPM core image provided by BL2 boot loader, Booting "
257bdd2596dSAchin Gupta 		     "device without SP initialization. SMC`s destined for SPM "
258bdd2596dSAchin Gupta 		     "core will return SMC_UNK\n");
259bdd2596dSAchin Gupta 		return 1;
260bdd2596dSAchin Gupta 	}
261bdd2596dSAchin Gupta 
262bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
263bdd2596dSAchin Gupta 	assert(spmc_ep_info->pc != 0U);
264bdd2596dSAchin Gupta 
265bdd2596dSAchin Gupta 	/*
266bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
267bdd2596dSAchin Gupta 	 * be used as a manifest for the SPM core at the next lower EL/mode.
268bdd2596dSAchin Gupta 	 */
269bdd2596dSAchin Gupta 	if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) {
270bdd2596dSAchin Gupta 		ERROR("Invalid or absent SPM core manifest\n");
271bdd2596dSAchin Gupta 		panic();
272bdd2596dSAchin Gupta 	}
273bdd2596dSAchin Gupta 
274bdd2596dSAchin Gupta 	/* Obtain whereabouts of SPM core manifest */
275bdd2596dSAchin Gupta 	rd_base = (void *) spmc_ep_info->args.arg0;
276bdd2596dSAchin Gupta 	rd_size = spmc_ep_info->args.arg2;
277bdd2596dSAchin Gupta 
278bdd2596dSAchin Gupta 	rd_base_align = page_align((uintptr_t) rd_base, DOWN);
279bdd2596dSAchin Gupta 	rd_size_align = page_align((uintptr_t) rd_size, UP);
280bdd2596dSAchin Gupta 
281bdd2596dSAchin Gupta 	/* Map the manifest in the SPMD translation regime first */
282bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest base : 0x%lx\n", rd_base_align);
283bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest size : 0x%lx\n", rd_size_align);
284bdd2596dSAchin Gupta 	rc = mmap_add_dynamic_region((unsigned long long) rd_base_align,
285bdd2596dSAchin Gupta 				     (uintptr_t) rd_base_align,
286bdd2596dSAchin Gupta 				     rd_size_align,
287bdd2596dSAchin Gupta 				     MT_RO_DATA);
2880f14d02fSMax Shvetsov 	if (rc != 0) {
289bdd2596dSAchin Gupta 		ERROR("Error while mapping SPM core manifest (%d).\n", rc);
290bdd2596dSAchin Gupta 		panic();
291bdd2596dSAchin Gupta 	}
292bdd2596dSAchin Gupta 
2930f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
2940f14d02fSMax Shvetsov 	rc = spmd_spmc_init(rd_base, rd_size);
2950f14d02fSMax Shvetsov 	if (rc != 0) {
2960f14d02fSMax Shvetsov 		int mmap_rc;
297bdd2596dSAchin Gupta 
298bdd2596dSAchin Gupta 		WARN("Booting device without SPM initialization. "
299bdd2596dSAchin Gupta 		     "SPCI SMCs destined for SPM core will return "
300bdd2596dSAchin Gupta 		     "ENOTSUPPORTED\n");
301bdd2596dSAchin Gupta 
3020f14d02fSMax Shvetsov 		mmap_rc = mmap_remove_dynamic_region(rd_base_align,
3030f14d02fSMax Shvetsov 						     rd_size_align);
3040f14d02fSMax Shvetsov 		if (mmap_rc != 0) {
305bdd2596dSAchin Gupta 			ERROR("Error while unmapping SPM core manifest (%d).\n",
3060f14d02fSMax Shvetsov 			      mmap_rc);
307bdd2596dSAchin Gupta 			panic();
308bdd2596dSAchin Gupta 		}
309bdd2596dSAchin Gupta 
3100f14d02fSMax Shvetsov 		return rc;
3110f14d02fSMax Shvetsov 	}
3120f14d02fSMax Shvetsov 
3130f14d02fSMax Shvetsov 	return 0;
3140f14d02fSMax Shvetsov }
3150f14d02fSMax Shvetsov 
3160f14d02fSMax Shvetsov /*******************************************************************************
3170f14d02fSMax Shvetsov  * Forward SMC to the other security state
3180f14d02fSMax Shvetsov  ******************************************************************************/
31993ff138bSOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, bool secure_origin,
32093ff138bSOlivier Deprez 				 uint64_t x1, uint64_t x2, uint64_t x3,
32193ff138bSOlivier Deprez 				 uint64_t x4, void *handle)
3220f14d02fSMax Shvetsov {
32393ff138bSOlivier Deprez 	uint32_t secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
32493ff138bSOlivier Deprez 	uint32_t secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
32593ff138bSOlivier Deprez 
3260f14d02fSMax Shvetsov 	/* Save incoming security state */
32793ff138bSOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
328*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
32993ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
330*033039f8SMax Shvetsov #endif
3310f14d02fSMax Shvetsov 
3320f14d02fSMax Shvetsov 	/* Restore outgoing security state */
33393ff138bSOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
334*033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
33593ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
336*033039f8SMax Shvetsov #endif
33793ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
3380f14d02fSMax Shvetsov 
33993ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
3400f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
3410f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
3420f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
3430f14d02fSMax Shvetsov }
3440f14d02fSMax Shvetsov 
3450f14d02fSMax Shvetsov /*******************************************************************************
3460f14d02fSMax Shvetsov  * Return SPCI_ERROR with specified error code
3470f14d02fSMax Shvetsov  ******************************************************************************/
3480f14d02fSMax Shvetsov static uint64_t spmd_spci_error_return(void *handle, int error_code)
3490f14d02fSMax Shvetsov {
3500f14d02fSMax Shvetsov 	SMC_RET8(handle, SPCI_ERROR,
3510f14d02fSMax Shvetsov 		 SPCI_TARGET_INFO_MBZ, error_code,
3520f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
3530f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
354bdd2596dSAchin Gupta }
355bdd2596dSAchin Gupta 
356bdd2596dSAchin Gupta /*******************************************************************************
357bdd2596dSAchin Gupta  * This function handles all SMCs in the range reserved for SPCI. Each call is
358bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
359bdd2596dSAchin Gupta  ******************************************************************************/
360bdd2596dSAchin Gupta uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
361bdd2596dSAchin Gupta 			  uint64_t x3, uint64_t x4, void *cookie, void *handle,
362bdd2596dSAchin Gupta 			  uint64_t flags)
363bdd2596dSAchin Gupta {
364bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
36593ff138bSOlivier Deprez 	bool secure_origin;
36693ff138bSOlivier Deprez 	int32_t ret;
367bdd2596dSAchin Gupta 
368bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
36993ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
370bdd2596dSAchin Gupta 
371bdd2596dSAchin Gupta 	INFO("SPM: 0x%x, 0x%llx, 0x%llx, 0x%llx, 0x%llx, "
372bdd2596dSAchin Gupta 	     "0x%llx, 0x%llx, 0x%llx\n",
373bdd2596dSAchin Gupta 	     smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5),
374bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X6),
375bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X7));
376bdd2596dSAchin Gupta 
377bdd2596dSAchin Gupta 	switch (smc_fid) {
378bdd2596dSAchin Gupta 	case SPCI_ERROR:
379bdd2596dSAchin Gupta 		/*
380bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
381bdd2596dSAchin Gupta 		 * this CPU. If so, then indicate that the SPM core initialised
382bdd2596dSAchin Gupta 		 * unsuccessfully.
383bdd2596dSAchin Gupta 		 */
38493ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
385bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
3860f14d02fSMax Shvetsov 		}
387bdd2596dSAchin Gupta 
38893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
3890f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
390bdd2596dSAchin Gupta 		break; /* not reached */
391bdd2596dSAchin Gupta 
392bdd2596dSAchin Gupta 	case SPCI_VERSION:
393bdd2596dSAchin Gupta 		/*
394bdd2596dSAchin Gupta 		 * TODO: This is an optimization that the version information
395bdd2596dSAchin Gupta 		 * provided by the SPM core manifest is returned by the SPM
396bdd2596dSAchin Gupta 		 * dispatcher. It might be a better idea to simply forward this
397bdd2596dSAchin Gupta 		 * call to the SPM core and wash our hands completely.
398bdd2596dSAchin Gupta 		 */
399bdd2596dSAchin Gupta 		ret = MAKE_SPCI_VERSION(spmc_attrs.major_version,
400bdd2596dSAchin Gupta 					spmc_attrs.minor_version);
401bdd2596dSAchin Gupta 		SMC_RET8(handle, SPCI_SUCCESS_SMC32, SPCI_TARGET_INFO_MBZ, ret,
402bdd2596dSAchin Gupta 			 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
403bdd2596dSAchin Gupta 			 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
404bdd2596dSAchin Gupta 		break; /* not reached */
405bdd2596dSAchin Gupta 
406bdd2596dSAchin Gupta 	case SPCI_FEATURES:
407bdd2596dSAchin Gupta 		/*
408bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
409bdd2596dSAchin Gupta 		 * forward to SPM core which will handle it if implemented.
410bdd2596dSAchin Gupta 		 */
411bdd2596dSAchin Gupta 
412bdd2596dSAchin Gupta 		/*
4130f14d02fSMax Shvetsov 		 * Check if x1 holds a valid SPCI fid. This is an
414bdd2596dSAchin Gupta 		 * optimization.
415bdd2596dSAchin Gupta 		 */
4160f14d02fSMax Shvetsov 		if (!is_spci_fid(x1)) {
4170f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
4180f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
4190f14d02fSMax Shvetsov 		}
420bdd2596dSAchin Gupta 
421bdd2596dSAchin Gupta 		/* Forward SMC from Normal world to the SPM core */
42293ff138bSOlivier Deprez 		if (!secure_origin) {
42393ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
4240f14d02fSMax Shvetsov 						x1, x2, x3, x4, handle);
425bdd2596dSAchin Gupta 		} else {
426bdd2596dSAchin Gupta 			/*
427bdd2596dSAchin Gupta 			 * Return success if call was from secure world i.e. all
428bdd2596dSAchin Gupta 			 * SPCI functions are supported. This is essentially a
429bdd2596dSAchin Gupta 			 * nop.
430bdd2596dSAchin Gupta 			 */
431bdd2596dSAchin Gupta 			SMC_RET8(handle, SPCI_SUCCESS_SMC32, x1, x2, x3, x4,
432bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X5),
433bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X6),
434bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X7));
435bdd2596dSAchin Gupta 		}
4360f14d02fSMax Shvetsov 
437bdd2596dSAchin Gupta 		break; /* not reached */
438bdd2596dSAchin Gupta 
439bdd2596dSAchin Gupta 	case SPCI_RX_RELEASE:
440bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC32:
441bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC64:
442bdd2596dSAchin Gupta 	case SPCI_RXTX_UNMAP:
443bdd2596dSAchin Gupta 	case SPCI_MSG_RUN:
444bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Normal world */
44593ff138bSOlivier Deprez 		if (secure_origin) {
4460f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
4470f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
448bdd2596dSAchin Gupta 		}
449bdd2596dSAchin Gupta 
450bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
451bdd2596dSAchin Gupta 
452bdd2596dSAchin Gupta 	case SPCI_PARTITION_INFO_GET:
453bdd2596dSAchin Gupta 	case SPCI_MSG_SEND:
454bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC32:
455bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC64:
456bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC32:
457bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC64:
458bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC32:
459bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC64:
460bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC32:
461bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC64:
462bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC32:
463bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC64:
464bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC32:
465bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC64:
466bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_RESP:
467bdd2596dSAchin Gupta 	case SPCI_MEM_RELINQUISH:
468bdd2596dSAchin Gupta 	case SPCI_MEM_RECLAIM:
469bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC32:
470bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC64:
471bdd2596dSAchin Gupta 		/*
472bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
473bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
474bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
475bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
476bdd2596dSAchin Gupta 		 */
477bdd2596dSAchin Gupta 
47893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
4790f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
480bdd2596dSAchin Gupta 		break; /* not reached */
481bdd2596dSAchin Gupta 
482bdd2596dSAchin Gupta 	case SPCI_MSG_WAIT:
483bdd2596dSAchin Gupta 		/*
484bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
485bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
486bdd2596dSAchin Gupta 		 * SPM core initialised successfully.
487bdd2596dSAchin Gupta 		 */
48893ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
489bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(0);
490bdd2596dSAchin Gupta 		}
491bdd2596dSAchin Gupta 
4920f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
493bdd2596dSAchin Gupta 
494bdd2596dSAchin Gupta 	case SPCI_MSG_YIELD:
495bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
49693ff138bSOlivier Deprez 		if (!secure_origin) {
4970f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
4980f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
499bdd2596dSAchin Gupta 		}
500bdd2596dSAchin Gupta 
50193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5020f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
503bdd2596dSAchin Gupta 		break; /* not reached */
504bdd2596dSAchin Gupta 
505bdd2596dSAchin Gupta 	default:
506bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
5070f14d02fSMax Shvetsov 		return spmd_spci_error_return(handle, SPCI_ERROR_NOT_SUPPORTED);
508bdd2596dSAchin Gupta 	}
509bdd2596dSAchin Gupta }
510