xref: /rk3399_ARM-atf/services/std_svc/spmd/spmd_main.c (revision 0f14d02f8fdc23aae7da452cbb18a92eba1feda2)
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  ******************************************************************************/
36*0f14d02fSMax Shvetsov static spmc_manifest_sect_attribute_t spmc_attrs;
37*0f14d02fSMax Shvetsov 
38*0f14d02fSMax Shvetsov /*******************************************************************************
39*0f14d02fSMax Shvetsov  * SPM Core entry point information. Discovered on the primary core and reused
40*0f14d02fSMax Shvetsov  * on secondary cores.
41*0f14d02fSMax Shvetsov  ******************************************************************************/
42*0f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info;
43*0f14d02fSMax Shvetsov 
44*0f14d02fSMax Shvetsov /*******************************************************************************
45*0f14d02fSMax Shvetsov  * Static function declaration.
46*0f14d02fSMax Shvetsov  ******************************************************************************/
47*0f14d02fSMax Shvetsov static int32_t	spmd_init(void);
48*0f14d02fSMax Shvetsov static int	spmd_spmc_init(void *rd_base, size_t rd_size);
49*0f14d02fSMax Shvetsov static uint64_t	spmd_spci_error_return(void *handle, int error_code);
50*0f14d02fSMax Shvetsov static uint64_t	spmd_smc_forward(uint32_t smc_fid, uint32_t in_sstate,
51*0f14d02fSMax Shvetsov 				 uint32_t out_sstate, uint64_t x1,
52*0f14d02fSMax Shvetsov 				 uint64_t x2, uint64_t x3, uint64_t x4,
53*0f14d02fSMax Shvetsov 				 void *handle);
54bdd2596dSAchin Gupta 
55bdd2596dSAchin Gupta /*******************************************************************************
56bdd2596dSAchin Gupta  * This function takes an SP context pointer and performs a synchronous entry
57bdd2596dSAchin Gupta  * into it.
58bdd2596dSAchin Gupta  ******************************************************************************/
59bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx)
60bdd2596dSAchin Gupta {
61bdd2596dSAchin Gupta 	uint64_t rc;
62bdd2596dSAchin Gupta 
63bdd2596dSAchin Gupta 	assert(spmc_ctx != NULL);
64bdd2596dSAchin Gupta 
65bdd2596dSAchin Gupta 	cm_set_context(&(spmc_ctx->cpu_ctx), SECURE);
66bdd2596dSAchin Gupta 
67bdd2596dSAchin Gupta 	/* Restore the context assigned above */
68bdd2596dSAchin Gupta 	cm_el1_sysregs_context_restore(SECURE);
6928f39f02SMax Shvetsov 	cm_el2_sysregs_context_restore(SECURE);
70bdd2596dSAchin Gupta 	cm_set_next_eret_context(SECURE);
71bdd2596dSAchin Gupta 
72bdd2596dSAchin Gupta 	/* Invalidate TLBs at EL1. */
73bdd2596dSAchin Gupta 	tlbivmalle1();
74bdd2596dSAchin Gupta 	dsbish();
75bdd2596dSAchin Gupta 
76bdd2596dSAchin Gupta 	/* Enter Secure Partition */
77bdd2596dSAchin Gupta 	rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx);
78bdd2596dSAchin Gupta 
79bdd2596dSAchin Gupta 	/* Save secure state */
80bdd2596dSAchin Gupta 	cm_el1_sysregs_context_save(SECURE);
8128f39f02SMax Shvetsov 	cm_el2_sysregs_context_save(SECURE);
82bdd2596dSAchin Gupta 
83bdd2596dSAchin Gupta 	return rc;
84bdd2596dSAchin Gupta }
85bdd2596dSAchin Gupta 
86bdd2596dSAchin Gupta /*******************************************************************************
87bdd2596dSAchin Gupta  * This function returns to the place where spm_sp_synchronous_entry() was
88bdd2596dSAchin Gupta  * called originally.
89bdd2596dSAchin Gupta  ******************************************************************************/
90bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc)
91bdd2596dSAchin Gupta {
92bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
93bdd2596dSAchin Gupta 
94bdd2596dSAchin Gupta 	/* Get context of the SP in use by this CPU. */
95bdd2596dSAchin Gupta 	assert(cm_get_context(SECURE) == &(ctx->cpu_ctx));
96bdd2596dSAchin Gupta 
97bdd2596dSAchin Gupta 	/*
98bdd2596dSAchin Gupta 	 * The SPMD must have initiated the original request through a
99bdd2596dSAchin Gupta 	 * synchronous entry into SPMC. Jump back to the original C runtime
100bdd2596dSAchin Gupta 	 * context with the value of rc in x0;
101bdd2596dSAchin Gupta 	 */
102bdd2596dSAchin Gupta 	spmd_spm_core_exit(ctx->c_rt_ctx, rc);
103bdd2596dSAchin Gupta 
104bdd2596dSAchin Gupta 	panic();
105bdd2596dSAchin Gupta }
106bdd2596dSAchin Gupta 
107bdd2596dSAchin Gupta /*******************************************************************************
108bdd2596dSAchin Gupta  * Jump to the SPM core for the first time.
109bdd2596dSAchin Gupta  ******************************************************************************/
110bdd2596dSAchin Gupta static int32_t spmd_init(void)
111bdd2596dSAchin Gupta {
112bdd2596dSAchin Gupta 	uint64_t rc = 0;
113bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
114bdd2596dSAchin Gupta 
115bdd2596dSAchin Gupta 	INFO("SPM Core init start.\n");
116bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_RESET;
117bdd2596dSAchin Gupta 
118bdd2596dSAchin Gupta 	rc = spmd_spm_core_sync_entry(ctx);
119bdd2596dSAchin Gupta 	if (rc) {
120bdd2596dSAchin Gupta 		ERROR("SPMC initialisation failed 0x%llx\n", rc);
121bdd2596dSAchin Gupta 		panic();
122bdd2596dSAchin Gupta 	}
123bdd2596dSAchin Gupta 
124bdd2596dSAchin Gupta 	ctx->state = SPMC_STATE_IDLE;
125bdd2596dSAchin Gupta 	INFO("SPM Core init end.\n");
126bdd2596dSAchin Gupta 
127bdd2596dSAchin Gupta 	return 1;
128bdd2596dSAchin Gupta }
129bdd2596dSAchin Gupta 
130bdd2596dSAchin Gupta /*******************************************************************************
131*0f14d02fSMax Shvetsov  * Load SPMC manifest, init SPMC.
132*0f14d02fSMax Shvetsov  ******************************************************************************/
133*0f14d02fSMax Shvetsov static int spmd_spmc_init(void *rd_base, size_t rd_size)
134*0f14d02fSMax Shvetsov {
135*0f14d02fSMax Shvetsov 	int rc;
136*0f14d02fSMax Shvetsov 	uint32_t ep_attr;
137*0f14d02fSMax Shvetsov 	unsigned int linear_id = plat_my_core_pos();
138*0f14d02fSMax Shvetsov 	spmd_spm_core_context_t *spm_ctx = &spm_core_context[linear_id];
139*0f14d02fSMax Shvetsov 
140*0f14d02fSMax Shvetsov 	/* Load the SPM core manifest */
141*0f14d02fSMax Shvetsov 	rc = plat_spm_core_manifest_load(&spmc_attrs, rd_base, rd_size);
142*0f14d02fSMax Shvetsov 	if (rc != 0) {
143*0f14d02fSMax Shvetsov 		WARN("No or invalid SPM core manifest image provided by BL2 "
144*0f14d02fSMax Shvetsov 		     "boot loader. ");
145*0f14d02fSMax Shvetsov 		return 1;
146*0f14d02fSMax Shvetsov 	}
147*0f14d02fSMax Shvetsov 
148*0f14d02fSMax Shvetsov 	/*
149*0f14d02fSMax Shvetsov 	 * Ensure that the SPM core version is compatible with the SPM
150*0f14d02fSMax Shvetsov 	 * dispatcher version
151*0f14d02fSMax Shvetsov 	 */
152*0f14d02fSMax Shvetsov 	if ((spmc_attrs.major_version != SPCI_VERSION_MAJOR) ||
153*0f14d02fSMax Shvetsov 	    (spmc_attrs.minor_version > SPCI_VERSION_MINOR)) {
154*0f14d02fSMax Shvetsov 		WARN("Unsupported SPCI version (%x.%x) specified in SPM core "
155*0f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
156*0f14d02fSMax Shvetsov 		     spmc_attrs.major_version, spmc_attrs.minor_version);
157*0f14d02fSMax Shvetsov 		return 1;
158*0f14d02fSMax Shvetsov 	}
159*0f14d02fSMax Shvetsov 
160*0f14d02fSMax Shvetsov 	INFO("SPCI version (%x.%x).\n", spmc_attrs.major_version,
161*0f14d02fSMax Shvetsov 	     spmc_attrs.minor_version);
162*0f14d02fSMax Shvetsov 
163*0f14d02fSMax Shvetsov 	/* Validate the SPM core runtime EL */
164*0f14d02fSMax Shvetsov 	if ((spmc_attrs.runtime_el != MODE_EL1) &&
165*0f14d02fSMax Shvetsov 	    (spmc_attrs.runtime_el != MODE_EL2)) {
166*0f14d02fSMax Shvetsov 		WARN("Unsupported SPM core run time EL%x specified in "
167*0f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
168*0f14d02fSMax Shvetsov 		     spmc_attrs.runtime_el);
169*0f14d02fSMax Shvetsov 		return 1;
170*0f14d02fSMax Shvetsov 	}
171*0f14d02fSMax Shvetsov 
172*0f14d02fSMax Shvetsov 	INFO("SPM core run time EL%x.\n", spmc_attrs.runtime_el);
173*0f14d02fSMax Shvetsov 
174*0f14d02fSMax Shvetsov 	/* Validate the SPM core execution state */
175*0f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state != MODE_RW_64) &&
176*0f14d02fSMax Shvetsov 	    (spmc_attrs.exec_state != MODE_RW_32)) {
177*0f14d02fSMax Shvetsov 		WARN("Unsupported SPM core execution state %x specified in "
178*0f14d02fSMax Shvetsov 		     "manifest image provided by BL2 boot loader.\n",
179*0f14d02fSMax Shvetsov 		     spmc_attrs.exec_state);
180*0f14d02fSMax Shvetsov 		return 1;
181*0f14d02fSMax Shvetsov 	}
182*0f14d02fSMax Shvetsov 
183*0f14d02fSMax Shvetsov 	INFO("SPM core execution state %x.\n", spmc_attrs.exec_state);
184*0f14d02fSMax Shvetsov 
185*0f14d02fSMax Shvetsov 	/* Ensure manifest has not requested S-EL2 in AArch32 state */
186*0f14d02fSMax Shvetsov 	if ((spmc_attrs.exec_state == MODE_RW_32) &&
187*0f14d02fSMax Shvetsov 	    (spmc_attrs.runtime_el == MODE_EL2)) {
188*0f14d02fSMax Shvetsov 		WARN("Invalid combination of SPM core execution state (%x) "
189*0f14d02fSMax Shvetsov 		     "and run time EL (%x).\n", spmc_attrs.exec_state,
190*0f14d02fSMax Shvetsov 		     spmc_attrs.runtime_el);
191*0f14d02fSMax Shvetsov 		return 1;
192*0f14d02fSMax Shvetsov 	}
193*0f14d02fSMax Shvetsov 
194*0f14d02fSMax Shvetsov 	/*
195*0f14d02fSMax Shvetsov 	 * Check if S-EL2 is supported on this system if S-EL2
196*0f14d02fSMax Shvetsov 	 * is required for SPM
197*0f14d02fSMax Shvetsov 	 */
198*0f14d02fSMax Shvetsov 	if (spmc_attrs.runtime_el == MODE_EL2) {
199*0f14d02fSMax Shvetsov 		uint64_t sel2 = read_id_aa64pfr0_el1();
200*0f14d02fSMax Shvetsov 
201*0f14d02fSMax Shvetsov 		sel2 >>= ID_AA64PFR0_SEL2_SHIFT;
202*0f14d02fSMax Shvetsov 		sel2 &= ID_AA64PFR0_SEL2_MASK;
203*0f14d02fSMax Shvetsov 
204*0f14d02fSMax Shvetsov 		if (!sel2) {
205*0f14d02fSMax Shvetsov 			WARN("SPM core run time EL: S-EL%x is not supported "
206*0f14d02fSMax Shvetsov 			     "but specified in manifest image provided by "
207*0f14d02fSMax Shvetsov 			     "BL2 boot loader.\n", spmc_attrs.runtime_el);
208*0f14d02fSMax Shvetsov 			return 1;
209*0f14d02fSMax Shvetsov 		}
210*0f14d02fSMax Shvetsov 	}
211*0f14d02fSMax Shvetsov 
212*0f14d02fSMax Shvetsov 	/* Initialise an entrypoint to set up the CPU context */
213*0f14d02fSMax Shvetsov 	ep_attr = SECURE | EP_ST_ENABLE;
214*0f14d02fSMax Shvetsov 	if (read_sctlr_el3() & SCTLR_EE_BIT) {
215*0f14d02fSMax Shvetsov 		ep_attr |= EP_EE_BIG;
216*0f14d02fSMax Shvetsov 	}
217*0f14d02fSMax Shvetsov 
218*0f14d02fSMax Shvetsov 	SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr);
219*0f14d02fSMax Shvetsov 	assert(spmc_ep_info->pc == BL32_BASE);
220*0f14d02fSMax Shvetsov 
221*0f14d02fSMax Shvetsov 	/*
222*0f14d02fSMax Shvetsov 	 * Populate SPSR for SPM core based upon validated parameters from the
223*0f14d02fSMax Shvetsov 	 * manifest
224*0f14d02fSMax Shvetsov 	 */
225*0f14d02fSMax Shvetsov 	if (spmc_attrs.exec_state == MODE_RW_32) {
226*0f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
227*0f14d02fSMax Shvetsov 						 SPSR_E_LITTLE,
228*0f14d02fSMax Shvetsov 						 DAIF_FIQ_BIT |
229*0f14d02fSMax Shvetsov 						 DAIF_IRQ_BIT |
230*0f14d02fSMax Shvetsov 						 DAIF_ABT_BIT);
231*0f14d02fSMax Shvetsov 	} else {
232*0f14d02fSMax Shvetsov 		spmc_ep_info->spsr = SPSR_64(spmc_attrs.runtime_el,
233*0f14d02fSMax Shvetsov 					     MODE_SP_ELX,
234*0f14d02fSMax Shvetsov 					     DISABLE_ALL_EXCEPTIONS);
235*0f14d02fSMax Shvetsov 	}
236*0f14d02fSMax Shvetsov 
237*0f14d02fSMax Shvetsov 	/* Initialise SPM core context with this entry point information */
238*0f14d02fSMax Shvetsov 	cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info);
239*0f14d02fSMax Shvetsov 
240*0f14d02fSMax Shvetsov 	/* Reuse PSCI affinity states to mark this SPMC context as off */
241*0f14d02fSMax Shvetsov 	spm_ctx->state = AFF_STATE_OFF;
242*0f14d02fSMax Shvetsov 
243*0f14d02fSMax Shvetsov 	INFO("SPM core setup done.\n");
244*0f14d02fSMax Shvetsov 
245*0f14d02fSMax Shvetsov 	/* Register init function for deferred init.  */
246*0f14d02fSMax Shvetsov 	bl31_register_bl32_init(&spmd_init);
247*0f14d02fSMax Shvetsov 
248*0f14d02fSMax Shvetsov 	return 0;
249*0f14d02fSMax Shvetsov }
250*0f14d02fSMax Shvetsov 
251*0f14d02fSMax Shvetsov /*******************************************************************************
252bdd2596dSAchin Gupta  * Initialize context of SPM core.
253bdd2596dSAchin Gupta  ******************************************************************************/
254*0f14d02fSMax Shvetsov int spmd_setup(void)
255bdd2596dSAchin Gupta {
256bdd2596dSAchin Gupta 	int rc;
257bdd2596dSAchin Gupta 	void *rd_base;
258bdd2596dSAchin Gupta 	size_t rd_size;
259bdd2596dSAchin Gupta 	uintptr_t rd_base_align;
260bdd2596dSAchin Gupta 	uintptr_t rd_size_align;
261bdd2596dSAchin Gupta 
262bdd2596dSAchin Gupta 	spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
263bdd2596dSAchin Gupta 	if (!spmc_ep_info) {
264bdd2596dSAchin Gupta 		WARN("No SPM core image provided by BL2 boot loader, Booting "
265bdd2596dSAchin Gupta 		     "device without SP initialization. SMC`s destined for SPM "
266bdd2596dSAchin Gupta 		     "core will return SMC_UNK\n");
267bdd2596dSAchin Gupta 		return 1;
268bdd2596dSAchin Gupta 	}
269bdd2596dSAchin Gupta 
270bdd2596dSAchin Gupta 	/* Under no circumstances will this parameter be 0 */
271bdd2596dSAchin Gupta 	assert(spmc_ep_info->pc != 0U);
272bdd2596dSAchin Gupta 
273bdd2596dSAchin Gupta 	/*
274bdd2596dSAchin Gupta 	 * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will
275bdd2596dSAchin Gupta 	 * be used as a manifest for the SPM core at the next lower EL/mode.
276bdd2596dSAchin Gupta 	 */
277bdd2596dSAchin Gupta 	if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) {
278bdd2596dSAchin Gupta 		ERROR("Invalid or absent SPM core manifest\n");
279bdd2596dSAchin Gupta 		panic();
280bdd2596dSAchin Gupta 	}
281bdd2596dSAchin Gupta 
282bdd2596dSAchin Gupta 	/* Obtain whereabouts of SPM core manifest */
283bdd2596dSAchin Gupta 	rd_base = (void *) spmc_ep_info->args.arg0;
284bdd2596dSAchin Gupta 	rd_size = spmc_ep_info->args.arg2;
285bdd2596dSAchin Gupta 
286bdd2596dSAchin Gupta 	rd_base_align = page_align((uintptr_t) rd_base, DOWN);
287bdd2596dSAchin Gupta 	rd_size_align = page_align((uintptr_t) rd_size, UP);
288bdd2596dSAchin Gupta 
289bdd2596dSAchin Gupta 	/* Map the manifest in the SPMD translation regime first */
290bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest base : 0x%lx\n", rd_base_align);
291bdd2596dSAchin Gupta 	VERBOSE("SPM core manifest size : 0x%lx\n", rd_size_align);
292bdd2596dSAchin Gupta 	rc = mmap_add_dynamic_region((unsigned long long) rd_base_align,
293bdd2596dSAchin Gupta 				     (uintptr_t) rd_base_align,
294bdd2596dSAchin Gupta 				     rd_size_align,
295bdd2596dSAchin Gupta 				     MT_RO_DATA);
296*0f14d02fSMax Shvetsov 	if (rc != 0) {
297bdd2596dSAchin Gupta 		ERROR("Error while mapping SPM core manifest (%d).\n", rc);
298bdd2596dSAchin Gupta 		panic();
299bdd2596dSAchin Gupta 	}
300bdd2596dSAchin Gupta 
301*0f14d02fSMax Shvetsov 	/* Load manifest, init SPMC */
302*0f14d02fSMax Shvetsov 	rc = spmd_spmc_init(rd_base, rd_size);
303*0f14d02fSMax Shvetsov 	if (rc != 0) {
304*0f14d02fSMax Shvetsov 		int mmap_rc;
305bdd2596dSAchin Gupta 
306bdd2596dSAchin Gupta 		WARN("Booting device without SPM initialization. "
307bdd2596dSAchin Gupta 		     "SPCI SMCs destined for SPM core will return "
308bdd2596dSAchin Gupta 		     "ENOTSUPPORTED\n");
309bdd2596dSAchin Gupta 
310*0f14d02fSMax Shvetsov 		mmap_rc = mmap_remove_dynamic_region(rd_base_align,
311*0f14d02fSMax Shvetsov 						     rd_size_align);
312*0f14d02fSMax Shvetsov 		if (mmap_rc != 0) {
313bdd2596dSAchin Gupta 			ERROR("Error while unmapping SPM core manifest (%d).\n",
314*0f14d02fSMax Shvetsov 			      mmap_rc);
315bdd2596dSAchin Gupta 			panic();
316bdd2596dSAchin Gupta 		}
317bdd2596dSAchin Gupta 
318*0f14d02fSMax Shvetsov 		return rc;
319*0f14d02fSMax Shvetsov 	}
320*0f14d02fSMax Shvetsov 
321*0f14d02fSMax Shvetsov 	return 0;
322*0f14d02fSMax Shvetsov }
323*0f14d02fSMax Shvetsov 
324*0f14d02fSMax Shvetsov /*******************************************************************************
325*0f14d02fSMax Shvetsov  * Forward SMC to the other security state
326*0f14d02fSMax Shvetsov  ******************************************************************************/
327*0f14d02fSMax Shvetsov static uint64_t spmd_smc_forward(uint32_t smc_fid, uint32_t in_sstate,
328*0f14d02fSMax Shvetsov 				 uint32_t out_sstate, uint64_t x1,
329*0f14d02fSMax Shvetsov 				 uint64_t x2, uint64_t x3, uint64_t x4,
330*0f14d02fSMax Shvetsov 				 void *handle)
331*0f14d02fSMax Shvetsov {
332*0f14d02fSMax Shvetsov 	/* Save incoming security state */
333*0f14d02fSMax Shvetsov 	cm_el1_sysregs_context_save(in_sstate);
334*0f14d02fSMax Shvetsov 	cm_el2_sysregs_context_save(in_sstate);
335*0f14d02fSMax Shvetsov 
336*0f14d02fSMax Shvetsov 	/* Restore outgoing security state */
337*0f14d02fSMax Shvetsov 	cm_el1_sysregs_context_restore(out_sstate);
338*0f14d02fSMax Shvetsov 	cm_el2_sysregs_context_restore(out_sstate);
339*0f14d02fSMax Shvetsov 	cm_set_next_eret_context(out_sstate);
340*0f14d02fSMax Shvetsov 
341*0f14d02fSMax Shvetsov 	SMC_RET8(cm_get_context(out_sstate), smc_fid, x1, x2, x3, x4,
342*0f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X5),
343*0f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X6),
344*0f14d02fSMax Shvetsov 			SMC_GET_GP(handle, CTX_GPREG_X7));
345*0f14d02fSMax Shvetsov }
346*0f14d02fSMax Shvetsov 
347*0f14d02fSMax Shvetsov /*******************************************************************************
348*0f14d02fSMax Shvetsov  * Return SPCI_ERROR with specified error code
349*0f14d02fSMax Shvetsov  ******************************************************************************/
350*0f14d02fSMax Shvetsov static uint64_t spmd_spci_error_return(void *handle, int error_code)
351*0f14d02fSMax Shvetsov {
352*0f14d02fSMax Shvetsov 	SMC_RET8(handle, SPCI_ERROR,
353*0f14d02fSMax Shvetsov 		 SPCI_TARGET_INFO_MBZ, error_code,
354*0f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ,
355*0f14d02fSMax Shvetsov 		 SPCI_PARAM_MBZ, SPCI_PARAM_MBZ);
356bdd2596dSAchin Gupta }
357bdd2596dSAchin Gupta 
358bdd2596dSAchin Gupta /*******************************************************************************
359bdd2596dSAchin Gupta  * This function handles all SMCs in the range reserved for SPCI. Each call is
360bdd2596dSAchin Gupta  * either forwarded to the other security state or handled by the SPM dispatcher
361bdd2596dSAchin Gupta  ******************************************************************************/
362bdd2596dSAchin Gupta uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
363bdd2596dSAchin Gupta 			  uint64_t x3, uint64_t x4, void *cookie, void *handle,
364bdd2596dSAchin Gupta 			  uint64_t flags)
365bdd2596dSAchin Gupta {
366bdd2596dSAchin Gupta 	uint32_t in_sstate;
367bdd2596dSAchin Gupta 	uint32_t out_sstate;
368bdd2596dSAchin Gupta 	int32_t ret;
369bdd2596dSAchin Gupta 	spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()];
370bdd2596dSAchin Gupta 
371bdd2596dSAchin Gupta 	/* Determine which security state this SMC originated from */
372bdd2596dSAchin Gupta 	if (is_caller_secure(flags)) {
373bdd2596dSAchin Gupta 		in_sstate = SECURE;
374bdd2596dSAchin Gupta 		out_sstate = NON_SECURE;
375bdd2596dSAchin Gupta 	} else {
376bdd2596dSAchin Gupta 		in_sstate = NON_SECURE;
377bdd2596dSAchin Gupta 		out_sstate = SECURE;
378bdd2596dSAchin Gupta 	}
379bdd2596dSAchin Gupta 
380bdd2596dSAchin Gupta 	INFO("SPM: 0x%x, 0x%llx, 0x%llx, 0x%llx, 0x%llx, "
381bdd2596dSAchin Gupta 	     "0x%llx, 0x%llx, 0x%llx\n",
382bdd2596dSAchin Gupta 	     smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5),
383bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X6),
384bdd2596dSAchin Gupta 	     SMC_GET_GP(handle, CTX_GPREG_X7));
385bdd2596dSAchin Gupta 
386bdd2596dSAchin Gupta 	switch (smc_fid) {
387bdd2596dSAchin Gupta 	case SPCI_ERROR:
388bdd2596dSAchin Gupta 		/*
389bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
390bdd2596dSAchin Gupta 		 * this CPU. If so, then indicate that the SPM core initialised
391bdd2596dSAchin Gupta 		 * unsuccessfully.
392bdd2596dSAchin Gupta 		 */
393*0f14d02fSMax Shvetsov 		if ((in_sstate == SECURE) &&
394*0f14d02fSMax Shvetsov 		    (ctx->state == SPMC_STATE_RESET)) {
395bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(x2);
396*0f14d02fSMax Shvetsov 		}
397bdd2596dSAchin Gupta 
398*0f14d02fSMax Shvetsov 		return spmd_smc_forward(smc_fid, in_sstate, out_sstate,
399*0f14d02fSMax 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 		/*
423*0f14d02fSMax Shvetsov 		 * Check if x1 holds a valid SPCI fid. This is an
424bdd2596dSAchin Gupta 		 * optimization.
425bdd2596dSAchin Gupta 		 */
426*0f14d02fSMax Shvetsov 		if (!is_spci_fid(x1)) {
427*0f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
428*0f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
429*0f14d02fSMax Shvetsov 		}
430bdd2596dSAchin Gupta 
431bdd2596dSAchin Gupta 		/* Forward SMC from Normal world to the SPM core */
432bdd2596dSAchin Gupta 		if (in_sstate == NON_SECURE) {
433*0f14d02fSMax Shvetsov 			return spmd_smc_forward(smc_fid, in_sstate, out_sstate,
434*0f14d02fSMax 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 		}
446*0f14d02fSMax Shvetsov 
447bdd2596dSAchin Gupta 		break; /* not reached */
448bdd2596dSAchin Gupta 
449bdd2596dSAchin Gupta 	case SPCI_RX_RELEASE:
450bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC32:
451bdd2596dSAchin Gupta 	case SPCI_RXTX_MAP_SMC64:
452bdd2596dSAchin Gupta 	case SPCI_RXTX_UNMAP:
453bdd2596dSAchin Gupta 	case SPCI_MSG_RUN:
454bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Normal world */
455bdd2596dSAchin Gupta 		if (in_sstate == SECURE) {
456*0f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
457*0f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
458bdd2596dSAchin Gupta 		}
459bdd2596dSAchin Gupta 
460bdd2596dSAchin Gupta 		/* Fall through to forward the call to the other world */
461bdd2596dSAchin Gupta 
462bdd2596dSAchin Gupta 	case SPCI_PARTITION_INFO_GET:
463bdd2596dSAchin Gupta 	case SPCI_MSG_SEND:
464bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC32:
465bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_REQ_SMC64:
466bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC32:
467bdd2596dSAchin Gupta 	case SPCI_MSG_SEND_DIRECT_RESP_SMC64:
468bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC32:
469bdd2596dSAchin Gupta 	case SPCI_MEM_DONATE_SMC64:
470bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC32:
471bdd2596dSAchin Gupta 	case SPCI_MEM_LEND_SMC64:
472bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC32:
473bdd2596dSAchin Gupta 	case SPCI_MEM_SHARE_SMC64:
474bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC32:
475bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_REQ_SMC64:
476bdd2596dSAchin Gupta 	case SPCI_MEM_RETRIEVE_RESP:
477bdd2596dSAchin Gupta 	case SPCI_MEM_RELINQUISH:
478bdd2596dSAchin Gupta 	case SPCI_MEM_RECLAIM:
479bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC32:
480bdd2596dSAchin Gupta 	case SPCI_SUCCESS_SMC64:
481bdd2596dSAchin Gupta 		/*
482bdd2596dSAchin Gupta 		 * TODO: Assume that no requests originate from EL3 at the
483bdd2596dSAchin Gupta 		 * moment. This will change if a SP service is required in
484bdd2596dSAchin Gupta 		 * response to secure interrupts targeted to EL3. Until then
485bdd2596dSAchin Gupta 		 * simply forward the call to the Normal world.
486bdd2596dSAchin Gupta 		 */
487bdd2596dSAchin Gupta 
488*0f14d02fSMax Shvetsov 		return spmd_smc_forward(smc_fid, in_sstate, out_sstate,
489*0f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
490bdd2596dSAchin Gupta 		break; /* not reached */
491bdd2596dSAchin Gupta 
492bdd2596dSAchin Gupta 	case SPCI_MSG_WAIT:
493bdd2596dSAchin Gupta 		/*
494bdd2596dSAchin Gupta 		 * Check if this is the first invocation of this interface on
495bdd2596dSAchin Gupta 		 * this CPU from the Secure world. If so, then indicate that the
496bdd2596dSAchin Gupta 		 * SPM core initialised successfully.
497bdd2596dSAchin Gupta 		 */
498bdd2596dSAchin Gupta 		if ((in_sstate == SECURE) && (ctx->state == SPMC_STATE_RESET)) {
499bdd2596dSAchin Gupta 			spmd_spm_core_sync_exit(0);
500bdd2596dSAchin Gupta 		}
501bdd2596dSAchin Gupta 
502*0f14d02fSMax Shvetsov 		/* Fall through to forward the call to the other world */
503bdd2596dSAchin Gupta 
504bdd2596dSAchin Gupta 	case SPCI_MSG_YIELD:
505bdd2596dSAchin Gupta 		/* This interface must be invoked only by the Secure world */
506bdd2596dSAchin Gupta 		if (in_sstate == NON_SECURE) {
507*0f14d02fSMax Shvetsov 			return spmd_spci_error_return(handle,
508*0f14d02fSMax Shvetsov 						      SPCI_ERROR_NOT_SUPPORTED);
509bdd2596dSAchin Gupta 		}
510bdd2596dSAchin Gupta 
511*0f14d02fSMax Shvetsov 		return spmd_smc_forward(smc_fid, in_sstate, out_sstate,
512*0f14d02fSMax Shvetsov 					x1, x2, x3, x4, handle);
513bdd2596dSAchin Gupta 		break; /* not reached */
514bdd2596dSAchin Gupta 
515bdd2596dSAchin Gupta 	default:
516bdd2596dSAchin Gupta 		WARN("SPM: Unsupported call 0x%08x\n", smc_fid);
517*0f14d02fSMax Shvetsov 		return spmd_spci_error_return(handle, SPCI_ERROR_NOT_SUPPORTED);
518bdd2596dSAchin Gupta 	}
519bdd2596dSAchin Gupta }
520