blob: 30a82e1d8d0810b103c1c7e5338379ab5b1d9420 [file] [log] [blame]
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +01001/*
2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <arch_helpers.h>
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +01009#include <assert.h>
10#include <common_def.h>
11#include <context.h>
12#include <context_mgmt.h>
13#include <debug.h>
14#include <platform_def.h>
15#include <platform.h>
16#include <secure_partition.h>
17#include <string.h>
18#include <types.h>
19#include <xlat_tables_v2.h>
20
21#include "spm_private.h"
22#include "spm_shim_private.h"
23
24/* Allocate and initialise the translation context for the secure partition. */
25REGISTER_XLAT_CONTEXT2(secure_partition,
26 PLAT_SP_IMAGE_MMAP_REGIONS,
27 PLAT_SP_IMAGE_MAX_XLAT_TABLES,
28 PLAT_VIRT_ADDR_SPACE_SIZE, PLAT_PHY_ADDR_SPACE_SIZE,
29 EL1_EL0_REGIME);
30
31/* Export a handle on the secure partition translation context */
32xlat_ctx_t *secure_partition_xlat_ctx_handle = &secure_partition_xlat_ctx;
33
34/* Setup context of the Secure Partition */
35void secure_partition_setup(void)
36{
37 VERBOSE("S-EL1/S-EL0 context setup start...\n");
38
39 cpu_context_t *ctx = cm_get_context(SECURE);
40
41 /* Make sure that we got a Secure context. */
42 assert(ctx != NULL);
43
44 /* Assert we are in Secure state. */
45 assert((read_scr_el3() & SCR_NS_BIT) == 0);
46
47 /* Disable MMU at EL1. */
48 disable_mmu_icache_el1();
49
50 /* Invalidate TLBs at EL1. */
51 tlbivmalle1();
52
53 /*
54 * General-Purpose registers
55 * -------------------------
56 */
57
58 /*
59 * X0: Virtual address of a buffer shared between EL3 and Secure EL0.
60 * The buffer will be mapped in the Secure EL1 translation regime
61 * with Normal IS WBWA attributes and RO data and Execute Never
62 * instruction access permissions.
63 *
64 * X1: Size of the buffer in bytes
65 *
66 * X2: cookie value (Implementation Defined)
67 *
68 * X3: cookie value (Implementation Defined)
69 *
70 * X4 to X30 = 0 (already done by cm_init_my_context())
71 */
72 write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, PLAT_SPM_BUF_BASE);
73 write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, PLAT_SPM_BUF_SIZE);
74 write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, PLAT_SPM_COOKIE_0);
75 write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, PLAT_SPM_COOKIE_1);
76
77 /*
78 * SP_EL0: A non-zero value will indicate to the SP that the SPM has
79 * initialized the stack pointer for the current CPU through
80 * implementation defined means. The value will be 0 otherwise.
81 */
82 write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_SP_EL0,
83 PLAT_SP_IMAGE_STACK_BASE + PLAT_SP_IMAGE_STACK_PCPU_SIZE);
84
85 /*
86 * Setup translation tables
87 * ------------------------
88 */
89
90#if ENABLE_ASSERTIONS
91
92 /* Get max granularity supported by the platform. */
93
Antonio Nino Diaz9efd6e52017-11-14 13:41:27 +000094 u_register_t id_aa64mmfr0_el1 = read_id_aa64mmfr0_el1();
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +010095
96 int tgran64_supported =
Antonio Nino Diaz9efd6e52017-11-14 13:41:27 +000097 ((id_aa64mmfr0_el1 >> ID_AA64MMFR0_EL1_TGRAN64_SHIFT) &
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +010098 ID_AA64MMFR0_EL1_TGRAN64_MASK) ==
99 ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED;
100
101 int tgran16_supported =
Antonio Nino Diaz9efd6e52017-11-14 13:41:27 +0000102 ((id_aa64mmfr0_el1 >> ID_AA64MMFR0_EL1_TGRAN16_SHIFT) &
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +0100103 ID_AA64MMFR0_EL1_TGRAN16_MASK) ==
104 ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED;
105
106 int tgran4_supported =
Antonio Nino Diaz9efd6e52017-11-14 13:41:27 +0000107 ((id_aa64mmfr0_el1 >> ID_AA64MMFR0_EL1_TGRAN4_SHIFT) &
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +0100108 ID_AA64MMFR0_EL1_TGRAN4_MASK) ==
109 ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED;
110
111 uintptr_t max_granule_size;
112
113 if (tgran64_supported) {
114 max_granule_size = 64 * 1024;
115 } else if (tgran16_supported) {
116 max_granule_size = 16 * 1024;
117 } else {
118 assert(tgran4_supported);
119 max_granule_size = 4 * 1024;
120 }
121
122 VERBOSE("Max translation granule supported: %lu KiB\n",
Antonio Nino Diaz9efd6e52017-11-14 13:41:27 +0000123 max_granule_size / 1024);
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +0100124
125 uintptr_t max_granule_size_mask = max_granule_size - 1;
126
127 /* Base must be aligned to the max granularity */
128 assert((ARM_SP_IMAGE_NS_BUF_BASE & max_granule_size_mask) == 0);
129
130 /* Size must be a multiple of the max granularity */
131 assert((ARM_SP_IMAGE_NS_BUF_SIZE & max_granule_size_mask) == 0);
132
133#endif /* ENABLE_ASSERTIONS */
134
135 /* This region contains the exception vectors used at S-EL1. */
136 const mmap_region_t sel1_exception_vectors =
137 MAP_REGION_FLAT(SPM_SHIM_EXCEPTIONS_START,
138 SPM_SHIM_EXCEPTIONS_SIZE,
139 MT_CODE | MT_SECURE | MT_PRIVILEGED);
140 mmap_add_region_ctx(&secure_partition_xlat_ctx,
141 &sel1_exception_vectors);
142
143 mmap_add_ctx(&secure_partition_xlat_ctx,
144 plat_get_secure_partition_mmap(NULL));
145
146 init_xlat_tables_ctx(&secure_partition_xlat_ctx);
147
148 /*
149 * MMU-related registers
150 * ---------------------
151 */
152
153 /* Set attributes in the right indices of the MAIR */
154 u_register_t mair_el1 =
155 MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX) |
156 MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, ATTR_IWBWA_OWBWA_NTR_INDEX) |
157 MAIR_ATTR_SET(ATTR_NON_CACHEABLE, ATTR_NON_CACHEABLE_INDEX);
158
159 write_ctx_reg(get_sysregs_ctx(ctx), CTX_MAIR_EL1, mair_el1);
160
161 /* Setup TCR_EL1. */
162 u_register_t tcr_ps_bits = tcr_physical_addr_size_bits(PLAT_PHY_ADDR_SPACE_SIZE);
163
164 u_register_t tcr_el1 =
165 /* Size of region addressed by TTBR0_EL1 = 2^(64-T0SZ) bytes. */
166 (64 - __builtin_ctzl(PLAT_VIRT_ADDR_SPACE_SIZE)) |
167 /* Inner and outer WBWA, shareable. */
168 TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA |
169 /* Set the granularity to 4KB. */
170 TCR_TG0_4K |
171 /* Limit Intermediate Physical Address Size. */
172 tcr_ps_bits << TCR_EL1_IPS_SHIFT |
173 /* Disable translations using TBBR1_EL1. */
174 TCR_EPD1_BIT
175 /* The remaining fields related to TBBR1_EL1 are left as zero. */
176 ;
177
178 tcr_el1 &= ~(
179 /* Enable translations using TBBR0_EL1 */
180 TCR_EPD0_BIT
181 );
182
183 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TCR_EL1, tcr_el1);
184
185 /* Setup SCTLR_EL1 */
186 u_register_t sctlr_el1 = read_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1);
187
188 sctlr_el1 |=
189 /*SCTLR_EL1_RES1 |*/
190 /* Don't trap DC CVAU, DC CIVAC, DC CVAC, DC CVAP, or IC IVAU */
191 SCTLR_UCI_BIT |
192 /* RW regions at xlat regime EL1&0 are forced to be XN. */
193 SCTLR_WXN_BIT |
194 /* Don't trap to EL1 execution of WFI or WFE at EL0. */
195 SCTLR_NTWI_BIT | SCTLR_NTWE_BIT |
196 /* Don't trap to EL1 accesses to CTR_EL0 from EL0. */
197 SCTLR_UCT_BIT |
198 /* Don't trap to EL1 execution of DZ ZVA at EL0. */
199 SCTLR_DZE_BIT |
200 /* Enable SP Alignment check for EL0 */
201 SCTLR_SA0_BIT |
202 /* Allow cacheable data and instr. accesses to normal memory. */
203 SCTLR_C_BIT | SCTLR_I_BIT |
204 /* Alignment fault checking enabled when at EL1 and EL0. */
205 SCTLR_A_BIT |
206 /* Enable MMU. */
207 SCTLR_M_BIT
208 ;
209
210 sctlr_el1 &= ~(
211 /* Explicit data accesses at EL0 are little-endian. */
212 SCTLR_E0E_BIT |
213 /* Accesses to DAIF from EL0 are trapped to EL1. */
214 SCTLR_UMA_BIT
215 );
216
217 write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_el1);
218
219 /* Point TTBR0_EL1 at the tables of the context created for the SP. */
220 write_ctx_reg(get_sysregs_ctx(ctx), CTX_TTBR0_EL1,
221 (u_register_t)secure_partition_base_xlat_table);
222
223 /*
224 * Setup other system registers
225 * ----------------------------
226 */
227
228 /* Shim Exception Vector Base Address */
229 write_ctx_reg(get_sysregs_ctx(ctx), CTX_VBAR_EL1,
230 SPM_SHIM_EXCEPTIONS_PTR);
231
232 /*
233 * FPEN: Forbid the Secure Partition to access FP/SIMD registers.
234 * TTA: Enable access to trace registers.
235 * ZEN (v8.2): Trap SVE instructions and access to SVE registers.
236 */
237 write_ctx_reg(get_sysregs_ctx(ctx), CTX_CPACR_EL1,
238 CPACR_EL1_FPEN(CPACR_EL1_FP_TRAP_ALL));
239
240 /*
241 * Prepare information in buffer shared between EL3 and S-EL0
242 * ----------------------------------------------------------
243 */
244
245 void *shared_buf_ptr = (void *) PLAT_SPM_BUF_BASE;
246
247 /* Copy the boot information into the shared buffer with the SP. */
248 assert((uintptr_t)shared_buf_ptr + sizeof(secure_partition_boot_info_t)
249 <= (PLAT_SPM_BUF_BASE + PLAT_SPM_BUF_SIZE));
250
251 assert(PLAT_SPM_BUF_BASE <= (UINTPTR_MAX - PLAT_SPM_BUF_SIZE + 1));
252
253 const secure_partition_boot_info_t *sp_boot_info =
254 plat_get_secure_partition_boot_info(NULL);
255
256 assert(sp_boot_info != NULL);
257
258 memcpy((void *) shared_buf_ptr, (const void *) sp_boot_info,
259 sizeof(secure_partition_boot_info_t));
260
261 /* Pointer to the MP information from the platform port. */
262 secure_partition_mp_info_t *sp_mp_info =
263 ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info;
264
265 assert(sp_mp_info != NULL);
266
267 /*
268 * Point the shared buffer MP information pointer to where the info will
269 * be populated, just after the boot info.
270 */
271 ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info =
Antonio Nino Diazff7d0802017-11-10 12:25:49 +0000272 (secure_partition_mp_info_t *) ((uintptr_t)shared_buf_ptr
273 + sizeof(secure_partition_boot_info_t));
Antonio Nino Diaz2fccb222017-10-24 10:07:35 +0100274
275 /*
276 * Update the shared buffer pointer to where the MP information for the
277 * payload will be populated
278 */
279 shared_buf_ptr = ((secure_partition_boot_info_t *) shared_buf_ptr)->mp_info;
280
281 /*
282 * Copy the cpu information into the shared buffer area after the boot
283 * information.
284 */
285 assert(sp_boot_info->num_cpus <= PLATFORM_CORE_COUNT);
286
287 assert((uintptr_t)shared_buf_ptr
288 <= (PLAT_SPM_BUF_BASE + PLAT_SPM_BUF_SIZE -
289 (sp_boot_info->num_cpus * sizeof(*sp_mp_info))));
290
291 memcpy(shared_buf_ptr, (const void *) sp_mp_info,
292 sp_boot_info->num_cpus * sizeof(*sp_mp_info));
293
294 /*
295 * Calculate the linear indices of cores in boot information for the
296 * secure partition and flag the primary CPU
297 */
298 sp_mp_info = (secure_partition_mp_info_t *) shared_buf_ptr;
299
300 for (unsigned int index = 0; index < sp_boot_info->num_cpus; index++) {
301 u_register_t mpidr = sp_mp_info[index].mpidr;
302
303 sp_mp_info[index].linear_id = plat_core_pos_by_mpidr(mpidr);
304 if (plat_my_core_pos() == sp_mp_info[index].linear_id)
305 sp_mp_info[index].flags |= MP_INFO_FLAG_PRIMARY_CPU;
306 }
307
308 VERBOSE("S-EL1/S-EL0 context setup end.\n");
309}