blob: d376fdac5f009fe18b9a2c842a638864e7615e74 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Douglas Raillarda8954fc2017-01-26 15:54:44 +00002 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00005 */
6
Achin Gupta27b895e2014-05-04 18:38:28 +01007#include <arch.h>
Achin Gupta7aea9082014-02-01 07:51:28 +00008#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +01009#include <assert.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000010#include <bl_common.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010011#include <context.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000012#include <context_mgmt.h>
Achin Gupta191e86e2014-05-09 10:03:15 +010013#include <interrupt_mgmt.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010014#include <platform.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010015#include <platform_def.h>
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010016#include <smcc_helpers.h>
Andrew Thoelke4e126072014-06-04 21:10:52 +010017#include <string.h>
Douglas Raillarda8954fc2017-01-26 15:54:44 +000018#include <utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000019
Achin Gupta7aea9082014-02-01 07:51:28 +000020
21/*******************************************************************************
22 * Context management library initialisation routine. This library is used by
23 * runtime services to share pointers to 'cpu_context' structures for the secure
24 * and non-secure states. Management of the structures and their associated
25 * memory is not done by the context management library e.g. the PSCI service
26 * manages the cpu context used for entry from and exit to the non-secure state.
27 * The Secure payload dispatcher service manages the context(s) corresponding to
28 * the secure state. It also uses this library to get access to the non-secure
29 * state cpu context pointers.
30 * Lastly, this library provides the api to make SP_EL3 point to the cpu context
31 * which will used for programming an entry into a lower EL. The same context
32 * will used to save state upon exception entry from that EL.
33 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +010034void cm_init(void)
Achin Gupta7aea9082014-02-01 07:51:28 +000035{
36 /*
37 * The context management library has only global data to intialize, but
38 * that will be done when the BSS is zeroed out
39 */
40}
41
42/*******************************************************************************
Soby Mathewb0082d22015-04-09 13:40:55 +010043 * The following function initializes the cpu_context 'ctx' for
Andrew Thoelke4e126072014-06-04 21:10:52 +010044 * first use, and sets the initial entrypoint state as specified by the
45 * entry_point_info structure.
46 *
47 * The security state to initialize is determined by the SECURE attribute
48 * of the entry_point_info. The function returns a pointer to the initialized
49 * context and sets this as the next context to return to.
50 *
51 * The EE and ST attributes are used to configure the endianess and secure
Soby Mathewb0082d22015-04-09 13:40:55 +010052 * timer availability for the new execution context.
Andrew Thoelke4e126072014-06-04 21:10:52 +010053 *
54 * To prepare the register state for entry call cm_prepare_el3_exit() and
55 * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
56 * cm_e1_sysreg_context_restore().
57 ******************************************************************************/
Soby Mathewb0082d22015-04-09 13:40:55 +010058static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke4e126072014-06-04 21:10:52 +010059{
Soby Mathewb0082d22015-04-09 13:40:55 +010060 unsigned int security_state;
Andrew Thoelke4e126072014-06-04 21:10:52 +010061 uint32_t scr_el3;
62 el3_state_t *state;
63 gp_regs_t *gp_regs;
64 unsigned long sctlr_elx;
65
Andrew Thoelke4e126072014-06-04 21:10:52 +010066 assert(ctx);
67
Soby Mathewb0082d22015-04-09 13:40:55 +010068 security_state = GET_SECURITY_STATE(ep->h.attr);
69
Andrew Thoelke4e126072014-06-04 21:10:52 +010070 /* Clear any residual register values from the context */
Douglas Raillarda8954fc2017-01-26 15:54:44 +000071 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke4e126072014-06-04 21:10:52 +010072
73 /*
74 * Base the context SCR on the current value, adjust for entry point
75 * specific requirements and set trap bits from the IMF
76 * TODO: provide the base/global SCR bits using another mechanism?
77 */
78 scr_el3 = read_scr();
79 scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
80 SCR_ST_BIT | SCR_HCE_BIT);
81
82 if (security_state != SECURE)
83 scr_el3 |= SCR_NS_BIT;
84
85 if (GET_RW(ep->spsr) == MODE_RW_64)
86 scr_el3 |= SCR_RW_BIT;
87
88 if (EP_GET_ST(ep->h.attr))
89 scr_el3 |= SCR_ST_BIT;
90
Gerald Lejeune632d6df2016-03-22 09:29:23 +010091#ifndef HANDLE_EA_EL3_FIRST
92 /* Explicitly stop to trap aborts from lower exception levels. */
93 scr_el3 &= ~SCR_EA_BIT;
94#endif
95
Masahiro Yamada441bfdd2016-12-25 23:36:24 +090096#ifdef IMAGE_BL31
Yatharth Kochar6c0566c2015-10-02 17:56:48 +010097 /*
98 * IRQ/FIQ bits only need setting if interrupt routing
99 * model has been set up for BL31.
100 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100101 scr_el3 |= get_scr_el3_from_routing_model(security_state);
Yatharth Kochar6c0566c2015-10-02 17:56:48 +0100102#endif
Andrew Thoelke4e126072014-06-04 21:10:52 +0100103
104 /*
105 * Set up SCTLR_ELx for the target exception level:
Soren Brinkmanna3b3c052016-04-18 10:46:19 -0700106 * EE bit is taken from the entrypoint attributes
Andrew Thoelke4e126072014-06-04 21:10:52 +0100107 * M, C and I bits must be zero (as required by PSCI specification)
108 *
109 * The target exception level is based on the spsr mode requested.
110 * If execution is requested to EL2 or hyp mode, HVC is enabled
111 * via SCR_EL3.HCE.
112 *
113 * Always compute the SCTLR_EL1 value and save in the cpu_context
114 * - the EL2 registers are set up by cm_preapre_ns_entry() as they
115 * are not part of the stored cpu_context
116 *
117 * TODO: In debug builds the spsr should be validated and checked
118 * against the CPU support, security state, endianess and pc
119 */
120 sctlr_elx = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0;
Jens Wiklanderc93c9df2014-09-04 10:23:27 +0200121 if (GET_RW(ep->spsr) == MODE_RW_64)
122 sctlr_elx |= SCTLR_EL1_RES1;
Soby Mathewa993c422016-09-29 14:15:57 +0100123 else {
Jens Wiklanderc93c9df2014-09-04 10:23:27 +0200124 sctlr_elx |= SCTLR_AARCH32_EL1_RES1;
Soby Mathewa993c422016-09-29 14:15:57 +0100125 /*
126 * If lower non-secure EL is AArch32, enable the CP15BEN, nTWI
127 * & nTWI bits. This aligns with SCTLR initialization on
128 * systems with an AArch32 EL3, where these bits
129 * architecturally reset to 1.
130 */
131 if (security_state != SECURE)
132 sctlr_elx |= SCTLR_CP15BEN_BIT | SCTLR_NTWI_BIT
133 | SCTLR_NTWE_BIT;
134 }
135
Andrew Thoelke4e126072014-06-04 21:10:52 +0100136 write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
137
138 if ((GET_RW(ep->spsr) == MODE_RW_64
139 && GET_EL(ep->spsr) == MODE_EL2)
140 || (GET_RW(ep->spsr) != MODE_RW_64
141 && GET_M32(ep->spsr) == MODE32_hyp)) {
142 scr_el3 |= SCR_HCE_BIT;
143 }
144
145 /* Populate EL3 state so that we've the right context before doing ERET */
146 state = get_el3state_ctx(ctx);
147 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
148 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
149 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
150
151 /*
152 * Store the X0-X7 value from the entrypoint into the context
153 * Use memcpy as we are in control of the layout of the structures
154 */
155 gp_regs = get_gpregs_ctx(ctx);
156 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
157}
158
159/*******************************************************************************
Soby Mathewb0082d22015-04-09 13:40:55 +0100160 * The following function initializes the cpu_context for a CPU specified by
161 * its `cpu_idx` for first use, and sets the initial entrypoint state as
162 * specified by the entry_point_info structure.
163 ******************************************************************************/
164void cm_init_context_by_index(unsigned int cpu_idx,
165 const entry_point_info_t *ep)
166{
167 cpu_context_t *ctx;
168 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
169 cm_init_context_common(ctx, ep);
170}
171
172/*******************************************************************************
173 * The following function initializes the cpu_context for the current CPU
174 * for first use, and sets the initial entrypoint state as specified by the
175 * entry_point_info structure.
176 ******************************************************************************/
177void cm_init_my_context(const entry_point_info_t *ep)
178{
179 cpu_context_t *ctx;
180 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
181 cm_init_context_common(ctx, ep);
182}
183
184/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +0100185 * Prepare the CPU system registers for first entry into secure or normal world
186 *
187 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
188 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
189 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
190 * For all entries, the EL1 registers are initialized from the cpu_context
191 ******************************************************************************/
192void cm_prepare_el3_exit(uint32_t security_state)
193{
194 uint32_t sctlr_elx, scr_el3, cptr_el2;
195 cpu_context_t *ctx = cm_get_context(security_state);
196
197 assert(ctx);
198
199 if (security_state == NON_SECURE) {
200 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
201 if (scr_el3 & SCR_HCE_BIT) {
202 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
203 sctlr_elx = read_ctx_reg(get_sysregs_ctx(ctx),
204 CTX_SCTLR_EL1);
205 sctlr_elx &= ~SCTLR_EE_BIT;
206 sctlr_elx |= SCTLR_EL2_RES1;
207 write_sctlr_el2(sctlr_elx);
208 } else if (read_id_aa64pfr0_el1() &
209 (ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) {
210 /* EL2 present but unused, need to disable safely */
211
212 /* HCR_EL2 = 0, except RW bit set to match SCR_EL3 */
213 write_hcr_el2((scr_el3 & SCR_RW_BIT) ? HCR_RW_BIT : 0);
214
215 /* SCTLR_EL2 : can be ignored when bypassing */
216
217 /* CPTR_EL2 : disable all traps TCPAC, TTA, TFP */
218 cptr_el2 = read_cptr_el2();
219 cptr_el2 &= ~(TCPAC_BIT | TTA_BIT | TFP_BIT);
220 write_cptr_el2(cptr_el2);
221
222 /* Enable EL1 access to timer */
223 write_cnthctl_el2(EL1PCEN_BIT | EL1PCTEN_BIT);
224
Soby Mathewfeddfcf2014-08-29 14:41:58 +0100225 /* Reset CNTVOFF_EL2 */
226 write_cntvoff_el2(0);
227
Andrew Thoelke4e126072014-06-04 21:10:52 +0100228 /* Set VPIDR, VMPIDR to match MIDR, MPIDR */
229 write_vpidr_el2(read_midr_el1());
230 write_vmpidr_el2(read_mpidr_el1());
Sandrine Bailleux8b0eafe2015-11-25 17:00:44 +0000231
232 /*
233 * Reset VTTBR_EL2.
234 * Needed because cache maintenance operations depend on
235 * the VMID even when non-secure EL1&0 stage 2 address
236 * translation are disabled.
237 */
238 write_vttbr_el2(0);
David Cunado5f55e282016-10-31 17:37:34 +0000239 /*
240 * Avoid unexpected debug traps in case where MDCR_EL2
241 * is not completely reset by the hardware - set
242 * MDCR_EL2.HPMN to PMCR_EL0.N and zero the remaining
243 * bits.
244 * MDCR_EL2.HPMN and PMCR_EL0.N fields are the same size
245 * (5 bits) and HPMN is at offset zero within MDCR_EL2.
246 */
247 write_mdcr_el2((read_pmcr_el0() & PMCR_EL0_N_BITS)
248 >> PMCR_EL0_N_SHIFT);
David Cunadoc14b08e2016-11-25 00:21:59 +0000249 /*
250 * Avoid unexpected traps of non-secure access to
251 * certain system registers at EL1 or lower where
252 * HSTR_EL2 is not completely reset to zero by the
253 * hardware - zero the entire register.
254 */
255 write_hstr_el2(0);
256 /*
257 * Reset CNTHP_CTL_EL2 to disable the EL2 physical timer
258 * and therefore prevent timer interrupts.
259 */
260 write_cnthp_ctl_el2(0);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100261 }
262 }
263
264 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
265
266 cm_set_next_context(ctx);
267}
268
269/*******************************************************************************
Soby Mathew2ed46e92014-07-04 16:02:26 +0100270 * The next four functions are used by runtime services to save and restore
271 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +0000272 * state.
273 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +0000274void cm_el1_sysregs_context_save(uint32_t security_state)
275{
Dan Handleye2712bc2014-04-10 15:37:22 +0100276 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000277
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100278 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000279 assert(ctx);
280
281 el1_sysregs_context_save(get_sysregs_ctx(ctx));
282}
283
284void cm_el1_sysregs_context_restore(uint32_t security_state)
285{
Dan Handleye2712bc2014-04-10 15:37:22 +0100286 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000287
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100288 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000289 assert(ctx);
290
291 el1_sysregs_context_restore(get_sysregs_ctx(ctx));
292}
293
294/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +0100295 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
296 * given security state with the given entrypoint
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000297 ******************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +0100298void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000299{
Dan Handleye2712bc2014-04-10 15:37:22 +0100300 cpu_context_t *ctx;
301 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000302
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100303 ctx = cm_get_context(security_state);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000304 assert(ctx);
305
Andrew Thoelke4e126072014-06-04 21:10:52 +0100306 /* Populate EL3 state so that ERET jumps to the correct entry */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000307 state = get_el3state_ctx(ctx);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000308 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000309}
310
311/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +0100312 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
313 * pertaining to the given security state
Achin Gupta607084e2014-02-09 18:24:19 +0000314 ******************************************************************************/
Andrew Thoelke4e126072014-06-04 21:10:52 +0100315void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathewa0fedc42016-06-16 14:52:04 +0100316 uintptr_t entrypoint, uint32_t spsr)
Achin Gupta607084e2014-02-09 18:24:19 +0000317{
Dan Handleye2712bc2014-04-10 15:37:22 +0100318 cpu_context_t *ctx;
319 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +0000320
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100321 ctx = cm_get_context(security_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000322 assert(ctx);
323
324 /* Populate EL3 state so that ERET jumps to the correct entry */
325 state = get_el3state_ctx(ctx);
326 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100327 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
Achin Gupta607084e2014-02-09 18:24:19 +0000328}
329
330/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +0100331 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
332 * pertaining to the given security state using the value and bit position
333 * specified in the parameters. It preserves all other bits.
334 ******************************************************************************/
335void cm_write_scr_el3_bit(uint32_t security_state,
336 uint32_t bit_pos,
337 uint32_t value)
338{
339 cpu_context_t *ctx;
340 el3_state_t *state;
341 uint32_t scr_el3;
342
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100343 ctx = cm_get_context(security_state);
Achin Gupta27b895e2014-05-04 18:38:28 +0100344 assert(ctx);
345
346 /* Ensure that the bit position is a valid one */
347 assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
348
349 /* Ensure that the 'value' is only a bit wide */
350 assert(value <= 1);
351
352 /*
353 * Get the SCR_EL3 value from the cpu context, clear the desired bit
354 * and set it to its new value.
355 */
356 state = get_el3state_ctx(ctx);
357 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
358 scr_el3 &= ~(1 << bit_pos);
359 scr_el3 |= value << bit_pos;
360 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
361}
362
363/*******************************************************************************
364 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
365 * given security state.
366 ******************************************************************************/
367uint32_t cm_get_scr_el3(uint32_t security_state)
368{
369 cpu_context_t *ctx;
370 el3_state_t *state;
371
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100372 ctx = cm_get_context(security_state);
Achin Gupta27b895e2014-05-04 18:38:28 +0100373 assert(ctx);
374
375 /* Populate EL3 state so that ERET jumps to the correct entry */
376 state = get_el3state_ctx(ctx);
377 return read_ctx_reg(state, CTX_SCR_EL3);
378}
379
380/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000381 * This function is used to program the context that's used for exception
382 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
383 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +0000384 ******************************************************************************/
385void cm_set_next_eret_context(uint32_t security_state)
386{
Dan Handleye2712bc2014-04-10 15:37:22 +0100387 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000388
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100389 ctx = cm_get_context(security_state);
Achin Gupta7aea9082014-02-01 07:51:28 +0000390 assert(ctx);
391
Andrew Thoelke4e126072014-06-04 21:10:52 +0100392 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000393}