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