xref: /arm-trusted-firmware/services/std_svc/spm/spm_mm/spm_mm_setup.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2017-2023, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  * Copyright (c) 2021, NVIDIA Corporation. All rights reserved.
4*91f16700Schasinglulu  *
5*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <assert.h>
9*91f16700Schasinglulu #include <string.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <arch.h>
12*91f16700Schasinglulu #include <arch_helpers.h>
13*91f16700Schasinglulu #include <context.h>
14*91f16700Schasinglulu #include <common/debug.h>
15*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
16*91f16700Schasinglulu #include <lib/xlat_tables/xlat_tables_v2.h>
17*91f16700Schasinglulu #include <platform_def.h>
18*91f16700Schasinglulu #include <plat/common/common_def.h>
19*91f16700Schasinglulu #include <plat/common/platform.h>
20*91f16700Schasinglulu #include <services/spm_mm_partition.h>
21*91f16700Schasinglulu 
22*91f16700Schasinglulu #include "spm_common.h"
23*91f16700Schasinglulu #include "spm_mm_private.h"
24*91f16700Schasinglulu #include "spm_shim_private.h"
25*91f16700Schasinglulu 
26*91f16700Schasinglulu /* Setup context of the Secure Partition */
27*91f16700Schasinglulu void spm_sp_setup(sp_context_t *sp_ctx)
28*91f16700Schasinglulu {
29*91f16700Schasinglulu 	cpu_context_t *ctx = &(sp_ctx->cpu_ctx);
30*91f16700Schasinglulu 
31*91f16700Schasinglulu 	/* Pointer to the MP information from the platform port. */
32*91f16700Schasinglulu 	const spm_mm_boot_info_t *sp_boot_info =
33*91f16700Schasinglulu 			plat_get_secure_partition_boot_info(NULL);
34*91f16700Schasinglulu 
35*91f16700Schasinglulu 	/*
36*91f16700Schasinglulu 	 * Initialize CPU context
37*91f16700Schasinglulu 	 * ----------------------
38*91f16700Schasinglulu 	 */
39*91f16700Schasinglulu 
40*91f16700Schasinglulu 	entry_point_info_t ep_info = {0};
41*91f16700Schasinglulu 
42*91f16700Schasinglulu 	SET_PARAM_HEAD(&ep_info, PARAM_EP, VERSION_1, SECURE | EP_ST_ENABLE);
43*91f16700Schasinglulu 
44*91f16700Schasinglulu 	/* Setup entrypoint and SPSR */
45*91f16700Schasinglulu 	ep_info.pc = sp_boot_info->sp_image_base;
46*91f16700Schasinglulu 	ep_info.spsr = SPSR_64(MODE_EL0, MODE_SP_EL0, DISABLE_ALL_EXCEPTIONS);
47*91f16700Schasinglulu 
48*91f16700Schasinglulu 	/*
49*91f16700Schasinglulu 	 * X0: Virtual address of a buffer shared between EL3 and Secure EL0.
50*91f16700Schasinglulu 	 *     The buffer will be mapped in the Secure EL1 translation regime
51*91f16700Schasinglulu 	 *     with Normal IS WBWA attributes and RO data and Execute Never
52*91f16700Schasinglulu 	 *     instruction access permissions.
53*91f16700Schasinglulu 	 *
54*91f16700Schasinglulu 	 * X1: Size of the buffer in bytes
55*91f16700Schasinglulu 	 *
56*91f16700Schasinglulu 	 * X2: cookie value (Implementation Defined)
57*91f16700Schasinglulu 	 *
58*91f16700Schasinglulu 	 * X3: cookie value (Implementation Defined)
59*91f16700Schasinglulu 	 *
60*91f16700Schasinglulu 	 * X4 to X7 = 0
61*91f16700Schasinglulu 	 */
62*91f16700Schasinglulu 	ep_info.args.arg0 = sp_boot_info->sp_shared_buf_base;
63*91f16700Schasinglulu 	ep_info.args.arg1 = sp_boot_info->sp_shared_buf_size;
64*91f16700Schasinglulu 	ep_info.args.arg2 = PLAT_SPM_COOKIE_0;
65*91f16700Schasinglulu 	ep_info.args.arg3 = PLAT_SPM_COOKIE_1;
66*91f16700Schasinglulu 
67*91f16700Schasinglulu 	cm_setup_context(ctx, &ep_info);
68*91f16700Schasinglulu 
69*91f16700Schasinglulu 	/*
70*91f16700Schasinglulu 	 * SP_EL0: A non-zero value will indicate to the SP that the SPM has
71*91f16700Schasinglulu 	 * initialized the stack pointer for the current CPU through
72*91f16700Schasinglulu 	 * implementation defined means. The value will be 0 otherwise.
73*91f16700Schasinglulu 	 */
74*91f16700Schasinglulu 	write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_SP_EL0,
75*91f16700Schasinglulu 			sp_boot_info->sp_stack_base + sp_boot_info->sp_pcpu_stack_size);
76*91f16700Schasinglulu 
77*91f16700Schasinglulu 	/*
78*91f16700Schasinglulu 	 * Setup translation tables
79*91f16700Schasinglulu 	 * ------------------------
80*91f16700Schasinglulu 	 */
81*91f16700Schasinglulu 
82*91f16700Schasinglulu #if ENABLE_ASSERTIONS
83*91f16700Schasinglulu 
84*91f16700Schasinglulu 	/* Get max granularity supported by the platform. */
85*91f16700Schasinglulu 	unsigned int max_granule = xlat_arch_get_max_supported_granule_size();
86*91f16700Schasinglulu 
87*91f16700Schasinglulu 	VERBOSE("Max translation granule size supported: %u KiB\n",
88*91f16700Schasinglulu 		max_granule / 1024U);
89*91f16700Schasinglulu 
90*91f16700Schasinglulu 	unsigned int max_granule_mask = max_granule - 1U;
91*91f16700Schasinglulu 
92*91f16700Schasinglulu 	/* Base must be aligned to the max granularity */
93*91f16700Schasinglulu 	assert((sp_boot_info->sp_ns_comm_buf_base & max_granule_mask) == 0);
94*91f16700Schasinglulu 
95*91f16700Schasinglulu 	/* Size must be a multiple of the max granularity */
96*91f16700Schasinglulu 	assert((sp_boot_info->sp_ns_comm_buf_size & max_granule_mask) == 0);
97*91f16700Schasinglulu 
98*91f16700Schasinglulu #endif /* ENABLE_ASSERTIONS */
99*91f16700Schasinglulu 
100*91f16700Schasinglulu 	/* This region contains the exception vectors used at S-EL1. */
101*91f16700Schasinglulu 	const mmap_region_t sel1_exception_vectors =
102*91f16700Schasinglulu 		MAP_REGION_FLAT(SPM_SHIM_EXCEPTIONS_START,
103*91f16700Schasinglulu 				SPM_SHIM_EXCEPTIONS_SIZE,
104*91f16700Schasinglulu 				MT_CODE | MT_SECURE | MT_PRIVILEGED);
105*91f16700Schasinglulu 	mmap_add_region_ctx(sp_ctx->xlat_ctx_handle,
106*91f16700Schasinglulu 			    &sel1_exception_vectors);
107*91f16700Schasinglulu 
108*91f16700Schasinglulu 	mmap_add_ctx(sp_ctx->xlat_ctx_handle,
109*91f16700Schasinglulu 		     plat_get_secure_partition_mmap(NULL));
110*91f16700Schasinglulu 
111*91f16700Schasinglulu 	init_xlat_tables_ctx(sp_ctx->xlat_ctx_handle);
112*91f16700Schasinglulu 
113*91f16700Schasinglulu 	/*
114*91f16700Schasinglulu 	 * MMU-related registers
115*91f16700Schasinglulu 	 * ---------------------
116*91f16700Schasinglulu 	 */
117*91f16700Schasinglulu 	xlat_ctx_t *xlat_ctx = sp_ctx->xlat_ctx_handle;
118*91f16700Schasinglulu 
119*91f16700Schasinglulu 	uint64_t mmu_cfg_params[MMU_CFG_PARAM_MAX];
120*91f16700Schasinglulu 
121*91f16700Schasinglulu 	setup_mmu_cfg((uint64_t *)&mmu_cfg_params, 0, xlat_ctx->base_table,
122*91f16700Schasinglulu 		      xlat_ctx->pa_max_address, xlat_ctx->va_max_address,
123*91f16700Schasinglulu 		      EL1_EL0_REGIME);
124*91f16700Schasinglulu 
125*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_MAIR_EL1,
126*91f16700Schasinglulu 		      mmu_cfg_params[MMU_CFG_MAIR]);
127*91f16700Schasinglulu 
128*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_TCR_EL1,
129*91f16700Schasinglulu 		      mmu_cfg_params[MMU_CFG_TCR]);
130*91f16700Schasinglulu 
131*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_TTBR0_EL1,
132*91f16700Schasinglulu 		      mmu_cfg_params[MMU_CFG_TTBR0]);
133*91f16700Schasinglulu 
134*91f16700Schasinglulu 	/* Setup SCTLR_EL1 */
135*91f16700Schasinglulu 	u_register_t sctlr_el1 = read_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1);
136*91f16700Schasinglulu 
137*91f16700Schasinglulu 	sctlr_el1 |=
138*91f16700Schasinglulu 		/*SCTLR_EL1_RES1 |*/
139*91f16700Schasinglulu 		/* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */
140*91f16700Schasinglulu 		SCTLR_UCI_BIT							|
141*91f16700Schasinglulu 		/* RW regions at xlat regime EL1&0 are forced to be XN. */
142*91f16700Schasinglulu 		SCTLR_WXN_BIT							|
143*91f16700Schasinglulu 		/* Don't trap to EL1 execution of WFI or WFE at EL0. */
144*91f16700Schasinglulu 		SCTLR_NTWI_BIT | SCTLR_NTWE_BIT					|
145*91f16700Schasinglulu 		/* Don't trap to EL1 accesses to CTR_EL0 from EL0. */
146*91f16700Schasinglulu 		SCTLR_UCT_BIT							|
147*91f16700Schasinglulu 		/* Don't trap to EL1 execution of DZ ZVA at EL0. */
148*91f16700Schasinglulu 		SCTLR_DZE_BIT							|
149*91f16700Schasinglulu 		/* Enable SP Alignment check for EL0 */
150*91f16700Schasinglulu 		SCTLR_SA0_BIT							|
151*91f16700Schasinglulu 		/* Don't change PSTATE.PAN on taking an exception to EL1 */
152*91f16700Schasinglulu 		SCTLR_SPAN_BIT							|
153*91f16700Schasinglulu 		/* Allow cacheable data and instr. accesses to normal memory. */
154*91f16700Schasinglulu 		SCTLR_C_BIT | SCTLR_I_BIT					|
155*91f16700Schasinglulu 		/* Enable MMU. */
156*91f16700Schasinglulu 		SCTLR_M_BIT
157*91f16700Schasinglulu 	;
158*91f16700Schasinglulu 
159*91f16700Schasinglulu 	sctlr_el1 &= ~(
160*91f16700Schasinglulu 		/* Explicit data accesses at EL0 are little-endian. */
161*91f16700Schasinglulu 		SCTLR_E0E_BIT							|
162*91f16700Schasinglulu 		/*
163*91f16700Schasinglulu 		 * Alignment fault checking disabled when at EL1 and EL0 as
164*91f16700Schasinglulu 		 * the UEFI spec permits unaligned accesses.
165*91f16700Schasinglulu 		 */
166*91f16700Schasinglulu 		SCTLR_A_BIT							|
167*91f16700Schasinglulu 		/* Accesses to DAIF from EL0 are trapped to EL1. */
168*91f16700Schasinglulu 		SCTLR_UMA_BIT
169*91f16700Schasinglulu 	);
170*91f16700Schasinglulu 
171*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1);
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	/*
174*91f16700Schasinglulu 	 * Setup other system registers
175*91f16700Schasinglulu 	 * ----------------------------
176*91f16700Schasinglulu 	 */
177*91f16700Schasinglulu 
178*91f16700Schasinglulu 	/* Shim Exception Vector Base Address */
179*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_VBAR_EL1,
180*91f16700Schasinglulu 			SPM_SHIM_EXCEPTIONS_PTR);
181*91f16700Schasinglulu 
182*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_CNTKCTL_EL1,
183*91f16700Schasinglulu 		      EL0PTEN_BIT | EL0VTEN_BIT | EL0PCTEN_BIT | EL0VCTEN_BIT);
184*91f16700Schasinglulu 
185*91f16700Schasinglulu 	/*
186*91f16700Schasinglulu 	 * FPEN: Allow the Secure Partition to access FP/SIMD registers.
187*91f16700Schasinglulu 	 * Note that SPM will not do any saving/restoring of these registers on
188*91f16700Schasinglulu 	 * behalf of the SP. This falls under the SP's responsibility.
189*91f16700Schasinglulu 	 * TTA: Enable access to trace registers.
190*91f16700Schasinglulu 	 * ZEN (v8.2): Trap SVE instructions and access to SVE registers.
191*91f16700Schasinglulu 	 */
192*91f16700Schasinglulu 	write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_CPACR_EL1,
193*91f16700Schasinglulu 			CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_NONE));
194*91f16700Schasinglulu 
195*91f16700Schasinglulu 	/*
196*91f16700Schasinglulu 	 * Prepare information in buffer shared between EL3 and S-EL0
197*91f16700Schasinglulu 	 * ----------------------------------------------------------
198*91f16700Schasinglulu 	 */
199*91f16700Schasinglulu 
200*91f16700Schasinglulu 	void *shared_buf_ptr = (void *) sp_boot_info->sp_shared_buf_base;
201*91f16700Schasinglulu 
202*91f16700Schasinglulu 	/* Copy the boot information into the shared buffer with the SP. */
203*91f16700Schasinglulu 	assert((uintptr_t)shared_buf_ptr + sizeof(spm_mm_boot_info_t)
204*91f16700Schasinglulu 	       <= (sp_boot_info->sp_shared_buf_base + sp_boot_info->sp_shared_buf_size));
205*91f16700Schasinglulu 
206*91f16700Schasinglulu 	assert(sp_boot_info->sp_shared_buf_base <=
207*91f16700Schasinglulu 				(UINTPTR_MAX - sp_boot_info->sp_shared_buf_size + 1));
208*91f16700Schasinglulu 
209*91f16700Schasinglulu 	assert(sp_boot_info != NULL);
210*91f16700Schasinglulu 
211*91f16700Schasinglulu 	memcpy((void *) shared_buf_ptr, (const void *) sp_boot_info,
212*91f16700Schasinglulu 	       sizeof(spm_mm_boot_info_t));
213*91f16700Schasinglulu 
214*91f16700Schasinglulu 	/* Pointer to the MP information from the platform port. */
215*91f16700Schasinglulu 	spm_mm_mp_info_t *sp_mp_info =
216*91f16700Schasinglulu 		((spm_mm_boot_info_t *) shared_buf_ptr)->mp_info;
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	assert(sp_mp_info != NULL);
219*91f16700Schasinglulu 
220*91f16700Schasinglulu 	/*
221*91f16700Schasinglulu 	 * Point the shared buffer MP information pointer to where the info will
222*91f16700Schasinglulu 	 * be populated, just after the boot info.
223*91f16700Schasinglulu 	 */
224*91f16700Schasinglulu 	((spm_mm_boot_info_t *) shared_buf_ptr)->mp_info =
225*91f16700Schasinglulu 		(spm_mm_mp_info_t *) ((uintptr_t)shared_buf_ptr
226*91f16700Schasinglulu 				+ sizeof(spm_mm_boot_info_t));
227*91f16700Schasinglulu 
228*91f16700Schasinglulu 	/*
229*91f16700Schasinglulu 	 * Update the shared buffer pointer to where the MP information for the
230*91f16700Schasinglulu 	 * payload will be populated
231*91f16700Schasinglulu 	 */
232*91f16700Schasinglulu 	shared_buf_ptr = ((spm_mm_boot_info_t *) shared_buf_ptr)->mp_info;
233*91f16700Schasinglulu 
234*91f16700Schasinglulu 	/*
235*91f16700Schasinglulu 	 * Copy the cpu information into the shared buffer area after the boot
236*91f16700Schasinglulu 	 * information.
237*91f16700Schasinglulu 	 */
238*91f16700Schasinglulu 	assert(sp_boot_info->num_cpus <= PLATFORM_CORE_COUNT);
239*91f16700Schasinglulu 
240*91f16700Schasinglulu 	assert((uintptr_t)shared_buf_ptr
241*91f16700Schasinglulu 	       <= (sp_boot_info->sp_shared_buf_base + sp_boot_info->sp_shared_buf_size -
242*91f16700Schasinglulu 		       (sp_boot_info->num_cpus * sizeof(*sp_mp_info))));
243*91f16700Schasinglulu 
244*91f16700Schasinglulu 	memcpy(shared_buf_ptr, (const void *) sp_mp_info,
245*91f16700Schasinglulu 		sp_boot_info->num_cpus * sizeof(*sp_mp_info));
246*91f16700Schasinglulu 
247*91f16700Schasinglulu 	/*
248*91f16700Schasinglulu 	 * Calculate the linear indices of cores in boot information for the
249*91f16700Schasinglulu 	 * secure partition and flag the primary CPU
250*91f16700Schasinglulu 	 */
251*91f16700Schasinglulu 	sp_mp_info = (spm_mm_mp_info_t *) shared_buf_ptr;
252*91f16700Schasinglulu 
253*91f16700Schasinglulu 	for (unsigned int index = 0; index < sp_boot_info->num_cpus; index++) {
254*91f16700Schasinglulu 		u_register_t mpidr = sp_mp_info[index].mpidr;
255*91f16700Schasinglulu 
256*91f16700Schasinglulu 		sp_mp_info[index].linear_id = plat_core_pos_by_mpidr(mpidr);
257*91f16700Schasinglulu 		if (plat_my_core_pos() == sp_mp_info[index].linear_id)
258*91f16700Schasinglulu 			sp_mp_info[index].flags |= MP_INFO_FLAG_PRIMARY_CPU;
259*91f16700Schasinglulu 	}
260*91f16700Schasinglulu }
261