xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision ac03ac5ebb081678261a52dec4472931c48523e3)
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);
68033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
6928f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
70033039f8SMax Shvetsov #endif
71bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
72bdd2596dSAchin Gupta 
73033039f8SMax 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);
78033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
7928f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
80033039f8SMax 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 
162033039f8SMax Shvetsov 	INFO("SPM core run time EL%x.\n",
163033039f8SMax Shvetsov 	     SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1);
1640f14d02fSMax Shvetsov 
165*ac03ac5eSMax Shvetsov 	/* Validate the SPMC ID, Ensure high bit is set */
166*ac03ac5eSMax Shvetsov 	if (!(spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) &
167*ac03ac5eSMax Shvetsov 			SPMC_SECURE_ID_MASK) {
168*ac03ac5eSMax Shvetsov 		WARN("Invalid ID (0x%x) for SPMC.\n",
169*ac03ac5eSMax Shvetsov 		     spmc_attrs.spmc_id);
170*ac03ac5eSMax Shvetsov 		return 1;
171*ac03ac5eSMax Shvetsov 	}
172*ac03ac5eSMax Shvetsov 
173*ac03ac5eSMax Shvetsov 	INFO("SPMC ID %x.\n", spmc_attrs.spmc_id);
174*ac03ac5eSMax Shvetsov 
1750f14d02fSMax Shvetsov 	/* Validate the SPM core execution state */
1760f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
1770f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
1780f14d02fSMax Shvetsov 		WARN("Unsupported SPM core execution state %x specified in "
1790f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
1800f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
1810f14d02fSMax Shvetsov 		return 1;
1820f14d02fSMax Shvetsov 	}
1830f14d02fSMax Shvetsov 
1840f14d02fSMax Shvetsov 	INFO("SPM core execution state %x.\n", spmc_attrs.exec_state);
1850f14d02fSMax Shvetsov 
186033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
187033039f8SMax Shvetsov 	/* Ensure manifest has not requested AArch32 state in S-EL2 */
188033039f8SMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
189033039f8SMax Shvetsov 		WARN("AArch32 state at S-EL2 is not supported.\n");
1900f14d02fSMax Shvetsov 		return 1;
1910f14d02fSMax Shvetsov 	}
1920f14d02fSMax Shvetsov 
1930f14d02fSMax Shvetsov 	/*
1940f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
1950f14d02fSMax Shvetsov 	 * is required for SPM
1960f14d02fSMax Shvetsov 	 */
1970f14d02fSMax Shvetsov 	uint64_t sel2 = read_id_aa64pfr0_el1();
1980f14d02fSMax Shvetsov 
1990f14d02fSMax Shvetsov 	sel2 >>= ID_AA64PFR0_SEL2_SHIFT;
2000f14d02fSMax Shvetsov 	sel2 &= ID_AA64PFR0_SEL2_MASK;
2010f14d02fSMax Shvetsov 
2020f14d02fSMax Shvetsov 	if (!sel2) {
203033039f8SMax Shvetsov 		WARN("SPM core run time S-EL2 is not supported.");
2040f14d02fSMax Shvetsov 		return 1;
2050f14d02fSMax Shvetsov 	}
206033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */
2070f14d02fSMax Shvetsov 
2080f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
2090f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
2100f14d02fSMax Shvetsov 	if (read_sctlr_el3() & SCTLR_EE_BIT) {
2110f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
2120f14d02fSMax Shvetsov 	}
2130f14d02fSMax Shvetsov 
2140f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
2150f14d02fSMax Shvetsov 	assert(spmc_ep_info->pc == BL32_BASE);
2160f14d02fSMax Shvetsov 
2170f14d02fSMax Shvetsov 	/*
2180f14d02fSMax Shvetsov 	 * Populate SPSR for SPM core based upon validated parameters from the
2190f14d02fSMax Shvetsov 	 * manifest
2200f14d02fSMax Shvetsov 	 */
2210f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
2220f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
2230f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
2240f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
2250f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
2260f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
2270f14d02fSMax Shvetsov 	} else {
228033039f8SMax Shvetsov 
229033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
230033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL2;
231033039f8SMax Shvetsov #else
232033039f8SMax Shvetsov 		static const uint32_t runtime_el = MODE_EL1;
233033039f8SMax Shvetsov #endif
234033039f8SMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(runtime_el,
2350f14d02fSMax Shvetsov 					     MODE_SP_ELX,
2360f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
2370f14d02fSMax Shvetsov 	}
2380f14d02fSMax Shvetsov 
2390f14d02fSMax Shvetsov 	/* Initialise SPM core context with this entry point information */
2400f14d02fSMax Shvetsov 	cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info);
2410f14d02fSMax Shvetsov 
2420f14d02fSMax Shvetsov 	/* Reuse PSCI affinity states to mark this SPMC context as off */
2430f14d02fSMax Shvetsov 	spm_ctx->state = AFF_STATE_OFF;
2440f14d02fSMax Shvetsov 
2450f14d02fSMax Shvetsov 	INFO("SPM core setup done.\n");
2460f14d02fSMax Shvetsov 
2470f14d02fSMax Shvetsov 	/* Register init function for deferred init.  */
2480f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
2490f14d02fSMax Shvetsov 
2500f14d02fSMax Shvetsov 	return 0;
2510f14d02fSMax Shvetsov }
2520f14d02fSMax Shvetsov 
2530f14d02fSMax Shvetsov /*******************************************************************************
254bdd2596dSAchin Gupta  * Initialize context of SPM core.
255bdd2596dSAchin Gupta  ******************************************************************************/
2560f14d02fSMax Shvetsov int spmd_setup(void)
257bdd2596dSAchin Gupta {
258bdd2596dSAchin Gupta 	int rc;
259bdd2596dSAchin Gupta 	void *rd_base;
260bdd2596dSAchin Gupta 	size_t rd_size;
261bdd2596dSAchin Gupta 	uintptr_t rd_base_align;
262bdd2596dSAchin Gupta 	uintptr_t rd_size_align;
263bdd2596dSAchin Gupta 
264bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
265bdd2596dSAchin Gupta 	if (!spmc_ep_info) {
266bdd2596dSAchin Gupta 		WARN("No SPM core image provided by BL2 boot loader, Booting "
267bdd2596dSAchin Gupta 		     "device without SP initialization. SMC`s destined for SPM "
268bdd2596dSAchin Gupta 		     "core will return SMC_UNK\n");
269bdd2596dSAchin Gupta 		return 1;
270bdd2596dSAchin Gupta 	}
271bdd2596dSAchin Gupta 
272bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
273bdd2596dSAchin Gupta 	assert(spmc_ep_info->pc != 0U);
274bdd2596dSAchin Gupta 
275bdd2596dSAchin Gupta 	/*
276bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
277bdd2596dSAchin Gupta 	 * be used as a manifest for the SPM core at the next lower EL/mode.
278bdd2596dSAchin Gupta 	 */
279bdd2596dSAchin Gupta 	if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) {
280bdd2596dSAchin Gupta 		ERROR("Invalid or absent SPM core manifest\n");
281bdd2596dSAchin Gupta 		panic();
282bdd2596dSAchin Gupta 	}
283bdd2596dSAchin Gupta 
284bdd2596dSAchin Gupta 	/* Obtain whereabouts of SPM core manifest */
285bdd2596dSAchin Gupta 	rd_base = (void *) spmc_ep_info->args.arg0;
286bdd2596dSAchin Gupta 	rd_size = spmc_ep_info->args.arg2;
287bdd2596dSAchin Gupta 
288bdd2596dSAchin Gupta 	rd_base_align = page_align((uintptr_t) rd_base, DOWN);
289bdd2596dSAchin Gupta 	rd_size_align = page_align((uintptr_t) rd_size, UP);
290bdd2596dSAchin Gupta 
291bdd2596dSAchin Gupta 	/* Map the manifest in the SPMD translation regime first */
292bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest base : 0x%lx\n", rd_base_align);
293bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest size : 0x%lx\n", rd_size_align);
294bdd2596dSAchin Gupta 	rc = mmap_add_dynamic_region((unsigned long long) rd_base_align,
295bdd2596dSAchin Gupta 				     (uintptr_t) rd_base_align,
296bdd2596dSAchin Gupta 				     rd_size_align,
297bdd2596dSAchin Gupta 				     MT_RO_DATA);
2980f14d02fSMax Shvetsov 	if (rc != 0) {
299bdd2596dSAchin Gupta 		ERROR("Error while mapping SPM core manifest (%d).\n", rc);
300bdd2596dSAchin Gupta 		panic();
301bdd2596dSAchin Gupta 	}
302bdd2596dSAchin Gupta 
3030f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
3040f14d02fSMax Shvetsov 	rc = spmd_spmc_init(rd_base, rd_size);
3050f14d02fSMax Shvetsov 	if (rc != 0) {
3060f14d02fSMax Shvetsov 		int mmap_rc;
307bdd2596dSAchin Gupta 
308bdd2596dSAchin Gupta 		WARN("Booting device without SPM initialization. "
309bdd2596dSAchin Gupta 		     "SPCI SMCs destined for SPM core will return "
310bdd2596dSAchin Gupta 		     "ENOTSUPPORTED\n");
311bdd2596dSAchin Gupta 
3120f14d02fSMax Shvetsov 		mmap_rc = mmap_remove_dynamic_region(rd_base_align,
3130f14d02fSMax Shvetsov 						     rd_size_align);
3140f14d02fSMax Shvetsov 		if (mmap_rc != 0) {
315bdd2596dSAchin Gupta 			ERROR("Error while unmapping SPM core manifest (%d).\n",
3160f14d02fSMax Shvetsov 			      mmap_rc);
317bdd2596dSAchin Gupta 			panic();
318bdd2596dSAchin Gupta 		}
319bdd2596dSAchin Gupta 
3200f14d02fSMax Shvetsov 		return rc;
3210f14d02fSMax Shvetsov 	}
3220f14d02fSMax Shvetsov 
3230f14d02fSMax Shvetsov 	return 0;
3240f14d02fSMax Shvetsov }
3250f14d02fSMax Shvetsov 
3260f14d02fSMax Shvetsov /*******************************************************************************
3270f14d02fSMax Shvetsov  * Forward SMC to the other security state
3280f14d02fSMax Shvetsov  ******************************************************************************/
32993ff138bSOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, bool secure_origin,
33093ff138bSOlivier Deprez 				 uint64_t x1, uint64_t x2, uint64_t x3,
33193ff138bSOlivier Deprez 				 uint64_t x4, void *handle)
3320f14d02fSMax Shvetsov {
33393ff138bSOlivier Deprez 	uint32_t secure_state_in = (secure_origin) ? SECURE : NON_SECURE;
33493ff138bSOlivier Deprez 	uint32_t secure_state_out = (!secure_origin) ? SECURE : NON_SECURE;
33593ff138bSOlivier Deprez 
3360f14d02fSMax Shvetsov 	/* Save incoming security state */
33793ff138bSOlivier Deprez 	cm_el1_sysregs_context_save(secure_state_in);
338033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
33993ff138bSOlivier Deprez 	cm_el2_sysregs_context_save(secure_state_in);
340033039f8SMax Shvetsov #endif
3410f14d02fSMax Shvetsov 
3420f14d02fSMax Shvetsov 	/* Restore outgoing security state */
34393ff138bSOlivier Deprez 	cm_el1_sysregs_context_restore(secure_state_out);
344033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2
34593ff138bSOlivier Deprez 	cm_el2_sysregs_context_restore(secure_state_out);
346033039f8SMax Shvetsov #endif
34793ff138bSOlivier Deprez 	cm_set_next_eret_context(secure_state_out);
3480f14d02fSMax Shvetsov 
34993ff138bSOlivier Deprez 	SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4,
3500f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
3510f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
3520f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
3530f14d02fSMax Shvetsov }
3540f14d02fSMax Shvetsov 
3550f14d02fSMax Shvetsov /*******************************************************************************
3560f14d02fSMax Shvetsov  * Return SPCI_ERROR with specified error code
3570f14d02fSMax Shvetsov  ******************************************************************************/
3580f14d02fSMax Shvetsov static uint64_t spmd_spci_error_return(void *handle, int error_code)
3590f14d02fSMax Shvetsov {
3600f14d02fSMax Shvetsov 	SMC_RET8(handle, SPCI_ERROR,
3610f14d02fSMax Shvetsov 		 SPCI_TARGET_INFO_MBZ, error_code,
3620f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
3630f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
364bdd2596dSAchin Gupta }
365bdd2596dSAchin Gupta 
366bdd2596dSAchin Gupta /*******************************************************************************
367bdd2596dSAchin Gupta  * This function handles all SMCs in the range reserved for SPCI. Each call is
368bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
369bdd2596dSAchin Gupta  ******************************************************************************/
370bdd2596dSAchin Gupta uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
371bdd2596dSAchin Gupta 			  uint64_t x3, uint64_t x4, void *cookie, void *handle,
372bdd2596dSAchin Gupta 			  uint64_t flags)
373bdd2596dSAchin Gupta {
374bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
37593ff138bSOlivier Deprez 	bool secure_origin;
37693ff138bSOlivier Deprez 	int32_t ret;
377bdd2596dSAchin Gupta 
378bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
37993ff138bSOlivier Deprez 	secure_origin = is_caller_secure(flags);
380bdd2596dSAchin Gupta 
381bdd2596dSAchin Gupta 	INFO("SPM: 0x%x, 0x%llx, 0x%llx, 0x%llx, 0x%llx, "
382bdd2596dSAchin Gupta 	     "0x%llx, 0x%llx, 0x%llx\n",
383bdd2596dSAchin Gupta 	     smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5),
384bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X6),
385bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X7));
386bdd2596dSAchin Gupta 
387bdd2596dSAchin Gupta 	switch (smc_fid) {
388bdd2596dSAchin Gupta 	case SPCI_ERROR:
389bdd2596dSAchin Gupta 		/*
390bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
391bdd2596dSAchin Gupta 		 * this CPU. If so, then indicate that the SPM core initialised
392bdd2596dSAchin Gupta 		 * unsuccessfully.
393bdd2596dSAchin Gupta 		 */
39493ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
395bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
3960f14d02fSMax Shvetsov 		}
397bdd2596dSAchin Gupta 
39893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
3990f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
400bdd2596dSAchin Gupta 		break; /* not reached */
401bdd2596dSAchin Gupta 
402bdd2596dSAchin Gupta 	case SPCI_VERSION:
403bdd2596dSAchin Gupta 		/*
404bdd2596dSAchin Gupta 		 * TODO: This is an optimization that the version information
405bdd2596dSAchin Gupta 		 * provided by the SPM core manifest is returned by the SPM
406bdd2596dSAchin Gupta 		 * dispatcher. It might be a better idea to simply forward this
407bdd2596dSAchin Gupta 		 * call to the SPM core and wash our hands completely.
408bdd2596dSAchin Gupta 		 */
409bdd2596dSAchin Gupta 		ret = MAKE_SPCI_VERSION(spmc_attrs.major_version,
410bdd2596dSAchin Gupta 					spmc_attrs.minor_version);
411bdd2596dSAchin Gupta 		SMC_RET8(handle, SPCI_SUCCESS_SMC32, SPCI_TARGET_INFO_MBZ, ret,
412bdd2596dSAchin Gupta 			 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
413bdd2596dSAchin Gupta 			 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
414bdd2596dSAchin Gupta 		break; /* not reached */
415bdd2596dSAchin Gupta 
416bdd2596dSAchin Gupta 	case SPCI_FEATURES:
417bdd2596dSAchin Gupta 		/*
418bdd2596dSAchin Gupta 		 * This is an optional interface. Do the minimal checks and
419bdd2596dSAchin Gupta 		 * forward to SPM core which will handle it if implemented.
420bdd2596dSAchin Gupta 		 */
421bdd2596dSAchin Gupta 
422bdd2596dSAchin Gupta 		/*
4230f14d02fSMax Shvetsov 		 * Check if x1 holds a valid SPCI fid. This is an
424bdd2596dSAchin Gupta 		 * optimization.
425bdd2596dSAchin Gupta 		 */
4260f14d02fSMax Shvetsov 		if (!is_spci_fid(x1)) {
4270f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
4280f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
4290f14d02fSMax Shvetsov 		}
430bdd2596dSAchin Gupta 
431bdd2596dSAchin Gupta 		/* Forward SMC from Normal world to the SPM core */
43293ff138bSOlivier Deprez 		if (!secure_origin) {
43393ff138bSOlivier Deprez 			return spmd_smc_forward(smc_fid, secure_origin,
4340f14d02fSMax Shvetsov 						x1, x2, x3, x4, handle);
435bdd2596dSAchin Gupta 		} else {
436bdd2596dSAchin Gupta 			/*
437bdd2596dSAchin Gupta 			 * Return success if call was from secure world i.e. all
438bdd2596dSAchin Gupta 			 * SPCI functions are supported. This is essentially a
439bdd2596dSAchin Gupta 			 * nop.
440bdd2596dSAchin Gupta 			 */
441bdd2596dSAchin Gupta 			SMC_RET8(handle, SPCI_SUCCESS_SMC32, x1, x2, x3, x4,
442bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X5),
443bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X6),
444bdd2596dSAchin Gupta 				 SMC_GET_GP(handle, CTX_GPREG_X7));
445bdd2596dSAchin Gupta 		}
4460f14d02fSMax Shvetsov 
447bdd2596dSAchin Gupta 		break; /* not reached */
448bdd2596dSAchin Gupta 
449*ac03ac5eSMax Shvetsov 	case SPCI_ID_GET:
450*ac03ac5eSMax Shvetsov 		/*
451*ac03ac5eSMax Shvetsov 		 * Returns the ID of the calling SPCI component.
452*ac03ac5eSMax Shvetsov 		*/
453*ac03ac5eSMax Shvetsov 		if (!secure_origin) {
454*ac03ac5eSMax Shvetsov 			SMC_RET8(handle, SPCI_SUCCESS_SMC32,
455*ac03ac5eSMax Shvetsov 				 SPCI_TARGET_INFO_MBZ, SPCI_NS_ENDPOINT_ID,
456*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
457*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
458*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ);
459*ac03ac5eSMax Shvetsov 		} else {
460*ac03ac5eSMax Shvetsov 			SMC_RET8(handle, SPCI_SUCCESS_SMC32,
461*ac03ac5eSMax Shvetsov 				 SPCI_TARGET_INFO_MBZ, spmc_attrs.spmc_id,
462*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
463*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
464*ac03ac5eSMax Shvetsov 				 SPCI_PARAM_MBZ);
465*ac03ac5eSMax Shvetsov 		}
466*ac03ac5eSMax Shvetsov 
467*ac03ac5eSMax Shvetsov 		break; /* not reached */
468*ac03ac5eSMax Shvetsov 
469bdd2596dSAchin Gupta 	case SPCI_RX_RELEASE:
470bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC32:
471bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC64:
472bdd2596dSAchin Gupta 	case SPCI_RXTX_UNMAP:
473bdd2596dSAchin Gupta 	case SPCI_MSG_RUN:
474bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Normal world */
47593ff138bSOlivier Deprez 		if (secure_origin) {
4760f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
4770f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
478bdd2596dSAchin Gupta 		}
479bdd2596dSAchin Gupta 
480bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
481bdd2596dSAchin Gupta 
482bdd2596dSAchin Gupta 	case SPCI_PARTITION_INFO_GET:
483bdd2596dSAchin Gupta 	case SPCI_MSG_SEND:
484bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC32:
485bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC64:
486bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC32:
487bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC64:
488bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC32:
489bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC64:
490bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC32:
491bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC64:
492bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC32:
493bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC64:
494bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC32:
495bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC64:
496bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_RESP:
497bdd2596dSAchin Gupta 	case SPCI_MEM_RELINQUISH:
498bdd2596dSAchin Gupta 	case SPCI_MEM_RECLAIM:
499bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC32:
500bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC64:
501bdd2596dSAchin Gupta 		/*
502bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
503bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
504bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
505bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
506bdd2596dSAchin Gupta 		 */
507bdd2596dSAchin Gupta 
50893ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5090f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
510bdd2596dSAchin Gupta 		break; /* not reached */
511bdd2596dSAchin Gupta 
512bdd2596dSAchin Gupta 	case SPCI_MSG_WAIT:
513bdd2596dSAchin Gupta 		/*
514bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
515bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
516bdd2596dSAchin Gupta 		 * SPM core initialised successfully.
517bdd2596dSAchin Gupta 		 */
51893ff138bSOlivier Deprez 		if (secure_origin && (ctx->state == SPMC_STATE_RESET)) {
519bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(0);
520bdd2596dSAchin Gupta 		}
521bdd2596dSAchin Gupta 
5220f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
523bdd2596dSAchin Gupta 
524bdd2596dSAchin Gupta 	case SPCI_MSG_YIELD:
525bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
52693ff138bSOlivier Deprez 		if (!secure_origin) {
5270f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
5280f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
529bdd2596dSAchin Gupta 		}
530bdd2596dSAchin Gupta 
53193ff138bSOlivier Deprez 		return spmd_smc_forward(smc_fid, secure_origin,
5320f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
533bdd2596dSAchin Gupta 		break; /* not reached */
534bdd2596dSAchin Gupta 
535bdd2596dSAchin Gupta 	default:
536bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
5370f14d02fSMax Shvetsov 		return spmd_spci_error_return(handle, SPCI_ERROR_NOT_SUPPORTED);
538bdd2596dSAchin Gupta 	}
539bdd2596dSAchin Gupta }
540