blob: a8672d6ca0f01bdb8ae5d869cad596cbfd9c450b [file] [log] [blame]
Soby Mathewe33b78a2016-05-05 14:10:46 +01001/*
Douglas Raillard32f0d3c2017-01-26 15:54:44 +00002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Soby Mathewe33b78a2016-05-05 14:10:46 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewe33b78a2016-05-05 14:10:46 +01005 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <bl_common.h>
11#include <context.h>
12#include <context_mgmt.h>
13#include <platform.h>
14#include <platform_def.h>
15#include <smcc_helpers.h>
16#include <string.h>
Douglas Raillard32f0d3c2017-01-26 15:54:44 +000017#include <utils.h>
Soby Mathewe33b78a2016-05-05 14:10:46 +010018
19/*******************************************************************************
20 * Context management library initialisation routine. This library is used by
21 * runtime services to share pointers to 'cpu_context' structures for the secure
22 * and non-secure states. Management of the structures and their associated
23 * memory is not done by the context management library e.g. the PSCI service
24 * manages the cpu context used for entry from and exit to the non-secure state.
25 * The Secure payload manages the context(s) corresponding to the secure state.
26 * It also uses this library to get access to the non-secure
27 * state cpu context pointers.
28 ******************************************************************************/
29void cm_init(void)
30{
31 /*
32 * The context management library has only global data to initialize, but
33 * that will be done when the BSS is zeroed out
34 */
35}
36
37/*******************************************************************************
38 * The following function initializes the cpu_context 'ctx' for
39 * first use, and sets the initial entrypoint state as specified by the
40 * entry_point_info structure.
41 *
42 * The security state to initialize is determined by the SECURE attribute
43 * of the entry_point_info. The function returns a pointer to the initialized
44 * context and sets this as the next context to return to.
45 *
46 * The EE and ST attributes are used to configure the endianness and secure
47 * timer availability for the new execution context.
48 *
49 * To prepare the register state for entry call cm_prepare_el3_exit() and
50 * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
51 * cm_e1_sysreg_context_restore().
52 ******************************************************************************/
53static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
54{
55 unsigned int security_state;
56 uint32_t scr, sctlr;
57 regs_t *reg_ctx;
58
59 assert(ctx);
60
61 security_state = GET_SECURITY_STATE(ep->h.attr);
62
63 /* Clear any residual register values from the context */
Douglas Raillard32f0d3c2017-01-26 15:54:44 +000064 zeromem(ctx, sizeof(*ctx));
Soby Mathewe33b78a2016-05-05 14:10:46 +010065
Soby Mathew9e3b4cb2016-08-31 12:34:33 +010066 reg_ctx = get_regs_ctx(ctx);
67
Soby Mathewe33b78a2016-05-05 14:10:46 +010068 /*
69 * Base the context SCR on the current value, adjust for entry point
70 * specific requirements
71 */
72 scr = read_scr();
73 scr &= ~(SCR_NS_BIT | SCR_HCE_BIT);
74
75 if (security_state != SECURE)
76 scr |= SCR_NS_BIT;
77
Soby Mathewe33b78a2016-05-05 14:10:46 +010078 if (security_state != SECURE) {
Soby Mathewb7b07872016-09-29 14:15:57 +010079 /*
David Cunado18f2efd2017-04-13 22:38:29 +010080 * Set up SCTLR for the Non-secure context.
81 *
82 * SCTLR.EE: Endianness is taken from the entrypoint attributes.
83 *
84 * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
85 * required by PSCI specification)
86 *
87 * Set remaining SCTLR fields to their architecturally defined
88 * values. Some fields reset to an IMPLEMENTATION DEFINED value:
89 *
90 * SCTLR.TE: Set to zero so that exceptions to an Exception
91 * Level executing at PL1 are taken to A32 state.
92 *
93 * SCTLR.V: Set to zero to select the normal exception vectors
94 * with base address held in VBAR.
Soby Mathewb7b07872016-09-29 14:15:57 +010095 */
David Cunado18f2efd2017-04-13 22:38:29 +010096 assert(((ep->spsr >> SPSR_E_SHIFT) & SPSR_E_MASK) ==
97 (EP_GET_EE(ep->h.attr) >> EP_EE_SHIFT));
98
99 sctlr = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0;
100 sctlr |= (SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_V_BIT));
Soby Mathewe33b78a2016-05-05 14:10:46 +0100101 write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr);
102 }
103
David Cunado18f2efd2017-04-13 22:38:29 +0100104 /*
105 * The target exception level is based on the spsr mode requested. If
106 * execution is requested to hyp mode, HVC is enabled via SCR.HCE.
107 */
Soby Mathewe33b78a2016-05-05 14:10:46 +0100108 if (GET_M32(ep->spsr) == MODE32_hyp)
109 scr |= SCR_HCE_BIT;
110
David Cunado18f2efd2017-04-13 22:38:29 +0100111 /*
112 * Store the initialised values for SCTLR and SCR in the cpu_context.
113 * The Hyp mode registers are not part of the saved context and are
114 * set-up in cm_prepare_el3_exit().
115 */
Soby Mathewe33b78a2016-05-05 14:10:46 +0100116 write_ctx_reg(reg_ctx, CTX_SCR, scr);
117 write_ctx_reg(reg_ctx, CTX_LR, ep->pc);
118 write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr);
119
120 /*
121 * Store the r0-r3 value from the entrypoint into the context
122 * Use memcpy as we are in control of the layout of the structures
123 */
124 memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t));
125}
126
127/*******************************************************************************
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000128 * Enable architecture extensions on first entry to Non-secure world.
129 * When EL2 is implemented but unused `el2_unused` is non-zero, otherwise
130 * it is zero.
131 ******************************************************************************/
132static void enable_extensions_nonsecure(int el2_unused)
133{
134#if IMAGE_BL32
135#endif
136}
137
138/*******************************************************************************
Soby Mathewe33b78a2016-05-05 14:10:46 +0100139 * The following function initializes the cpu_context for a CPU specified by
140 * its `cpu_idx` for first use, and sets the initial entrypoint state as
141 * specified by the entry_point_info structure.
142 ******************************************************************************/
143void cm_init_context_by_index(unsigned int cpu_idx,
144 const entry_point_info_t *ep)
145{
146 cpu_context_t *ctx;
147 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
148 cm_init_context_common(ctx, ep);
149}
150
151/*******************************************************************************
152 * The following function initializes the cpu_context for the current CPU
153 * for first use, and sets the initial entrypoint state as specified by the
154 * entry_point_info structure.
155 ******************************************************************************/
156void cm_init_my_context(const entry_point_info_t *ep)
157{
158 cpu_context_t *ctx;
159 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
160 cm_init_context_common(ctx, ep);
161}
162
163/*******************************************************************************
164 * Prepare the CPU system registers for first entry into secure or normal world
165 *
166 * If execution is requested to hyp mode, HSCTLR is initialized
167 * If execution is requested to non-secure PL1, and the CPU supports
168 * HYP mode then HYP mode is disabled by configuring all necessary HYP mode
169 * registers.
170 ******************************************************************************/
171void cm_prepare_el3_exit(uint32_t security_state)
172{
David Cunado18f2efd2017-04-13 22:38:29 +0100173 uint32_t hsctlr, scr;
Soby Mathewe33b78a2016-05-05 14:10:46 +0100174 cpu_context_t *ctx = cm_get_context(security_state);
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000175 int el2_unused = 0;
Soby Mathewe33b78a2016-05-05 14:10:46 +0100176
177 assert(ctx);
178
179 if (security_state == NON_SECURE) {
180 scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR);
181 if (scr & SCR_HCE_BIT) {
182 /* Use SCTLR value to initialize HSCTLR */
David Cunado18f2efd2017-04-13 22:38:29 +0100183 hsctlr = read_ctx_reg(get_regs_ctx(ctx),
Soby Mathewe33b78a2016-05-05 14:10:46 +0100184 CTX_NS_SCTLR);
David Cunado18f2efd2017-04-13 22:38:29 +0100185 hsctlr |= HSCTLR_RES1;
Soby Mathewe33b78a2016-05-05 14:10:46 +0100186 /* Temporarily set the NS bit to access HSCTLR */
187 write_scr(read_scr() | SCR_NS_BIT);
188 /*
189 * Make sure the write to SCR is complete so that
190 * we can access HSCTLR
191 */
192 isb();
David Cunado18f2efd2017-04-13 22:38:29 +0100193 write_hsctlr(hsctlr);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100194 isb();
195
196 write_scr(read_scr() & ~SCR_NS_BIT);
197 isb();
198 } else if (read_id_pfr1() &
199 (ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) {
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000200 el2_unused = 1;
201
David Cunado495f3d32016-10-31 17:37:34 +0000202 /*
203 * Set the NS bit to access NS copies of certain banked
204 * registers
205 */
Soby Mathewe33b78a2016-05-05 14:10:46 +0100206 write_scr(read_scr() | SCR_NS_BIT);
207 isb();
208
David Cunado18f2efd2017-04-13 22:38:29 +0100209 /*
210 * Hyp / PL2 present but unused, need to disable safely.
211 * HSCTLR can be ignored in this case.
212 *
213 * Set HCR to its architectural reset value so that
214 * Non-secure operations do not trap to Hyp mode.
215 */
216 write_hcr(HCR_RESET_VAL);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100217
David Cunado18f2efd2017-04-13 22:38:29 +0100218 /*
219 * Set HCPTR to its architectural reset value so that
220 * Non-secure access from EL1 or EL0 to trace and to
221 * Advanced SIMD and floating point functionality does
222 * not trap to Hyp mode.
223 */
224 write_hcptr(HCPTR_RESET_VAL);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100225
David Cunado18f2efd2017-04-13 22:38:29 +0100226 /*
227 * Initialise CNTHCTL. All fields are architecturally
228 * UNKNOWN on reset and are set to zero except for
229 * field(s) listed below.
230 *
231 * CNTHCTL.PL1PCEN: Disable traps to Hyp mode of
232 * Non-secure EL0 and EL1 accessed to the physical
233 * timer registers.
234 *
235 * CNTHCTL.PL1PCTEN: Disable traps to Hyp mode of
236 * Non-secure EL0 and EL1 accessed to the physical
237 * counter registers.
238 */
239 write_cnthctl(CNTHCTL_RESET_VAL |
240 PL1PCEN_BIT | PL1PCTEN_BIT);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100241
David Cunado18f2efd2017-04-13 22:38:29 +0100242 /*
243 * Initialise CNTVOFF to zero as it resets to an
244 * IMPLEMENTATION DEFINED value.
245 */
Soby Mathewe33b78a2016-05-05 14:10:46 +0100246 write64_cntvoff(0);
247
David Cunado18f2efd2017-04-13 22:38:29 +0100248 /*
249 * Set VPIDR and VMPIDR to match MIDR_EL1 and MPIDR
250 * respectively.
251 */
Soby Mathewe33b78a2016-05-05 14:10:46 +0100252 write_vpidr(read_midr());
253 write_vmpidr(read_mpidr());
254
255 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100256 * Initialise VTTBR, setting all fields rather than
257 * relying on the hw. Some fields are architecturally
258 * UNKNOWN at reset.
259 *
260 * VTTBR.VMID: Set to zero which is the architecturally
261 * defined reset value. Even though EL1&0 stage 2
262 * address translation is disabled, cache maintenance
263 * operations depend on the VMID.
264 *
265 * VTTBR.BADDR: Set to zero as EL1&0 stage 2 address
266 * translation is disabled.
Soby Mathewe33b78a2016-05-05 14:10:46 +0100267 */
David Cunado18f2efd2017-04-13 22:38:29 +0100268 write64_vttbr(VTTBR_RESET_VAL &
269 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT)
270 | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
David Cunado495f3d32016-10-31 17:37:34 +0000271
272 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100273 * Initialise HDCR, setting all the fields rather than
274 * relying on hw.
275 *
276 * HDCR.HPMN: Set to value of PMCR.N which is the
277 * architecturally-defined reset value.
David Cunado495f3d32016-10-31 17:37:34 +0000278 */
David Cunado18f2efd2017-04-13 22:38:29 +0100279 write_hdcr(HDCR_RESET_VAL |
280 ((read_pmcr() & PMCR_N_BITS) >> PMCR_N_SHIFT));
David Cunado939f66d2016-11-25 00:21:59 +0000281
282 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100283 * Set HSTR to its architectural reset value so that
284 * access to system registers in the cproc=1111
285 * encoding space do not trap to Hyp mode.
David Cunado939f66d2016-11-25 00:21:59 +0000286 */
David Cunado18f2efd2017-04-13 22:38:29 +0100287 write_hstr(HSTR_RESET_VAL);
288 /*
289 * Set CNTHP_CTL to its architectural reset value to
290 * disable the EL2 physical timer and prevent timer
291 * interrupts. Some fields are architecturally UNKNOWN
292 * on reset and are set to zero.
293 */
294 write_cnthp_ctl(CNTHP_CTL_RESET_VAL);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100295 isb();
296
297 write_scr(read_scr() & ~SCR_NS_BIT);
298 isb();
299 }
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000300 enable_extensions_nonsecure(el2_unused);
Soby Mathewe33b78a2016-05-05 14:10:46 +0100301 }
302}