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