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