Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 3c817f4 | 2018-03-21 10:49:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 7 | #include <amu.h> |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 8 | #include <arch.h> |
| 9 | #include <arch_helpers.h> |
| 10 | #include <assert.h> |
| 11 | #include <bl_common.h> |
| 12 | #include <context.h> |
| 13 | #include <context_mgmt.h> |
| 14 | #include <platform.h> |
| 15 | #include <platform_def.h> |
Antonio Nino Diaz | 3c817f4 | 2018-03-21 10:49:27 +0000 | [diff] [blame] | 16 | #include <smccc_helpers.h> |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 17 | #include <string.h> |
Douglas Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 18 | #include <utils.h> |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 19 | |
| 20 | /******************************************************************************* |
| 21 | * Context management library initialisation routine. This library is used by |
| 22 | * runtime services to share pointers to 'cpu_context' structures for the secure |
| 23 | * and non-secure states. Management of the structures and their associated |
| 24 | * memory is not done by the context management library e.g. the PSCI service |
| 25 | * manages the cpu context used for entry from and exit to the non-secure state. |
| 26 | * The Secure payload manages the context(s) corresponding to the secure state. |
| 27 | * It also uses this library to get access to the non-secure |
| 28 | * state cpu context pointers. |
| 29 | ******************************************************************************/ |
| 30 | void cm_init(void) |
| 31 | { |
| 32 | /* |
| 33 | * The context management library has only global data to initialize, but |
| 34 | * that will be done when the BSS is zeroed out |
| 35 | */ |
| 36 | } |
| 37 | |
| 38 | /******************************************************************************* |
| 39 | * The following function initializes the cpu_context 'ctx' for |
| 40 | * first use, and sets the initial entrypoint state as specified by the |
| 41 | * entry_point_info structure. |
| 42 | * |
| 43 | * The security state to initialize is determined by the SECURE attribute |
Antonio Nino Diaz | 28dce9e | 2018-05-22 10:09:10 +0100 | [diff] [blame] | 44 | * of the entry_point_info. |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 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 | ******************************************************************************/ |
Antonio Nino Diaz | 28dce9e | 2018-05-22 10:09:10 +0100 | [diff] [blame] | 53 | void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep) |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 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 Raillard | a8954fc | 2017-01-26 15:54:44 +0000 | [diff] [blame] | 64 | zeromem(ctx, sizeof(*ctx)); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 65 | |
Soby Mathew | b4a970a | 2016-08-31 12:34:33 +0100 | [diff] [blame] | 66 | reg_ctx = get_regs_ctx(ctx); |
| 67 | |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 68 | /* |
| 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 Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 78 | if (security_state != SECURE) { |
Soby Mathew | a993c42 | 2016-09-29 14:15:57 +0100 | [diff] [blame] | 79 | /* |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 80 | * 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 Mathew | a993c42 | 2016-09-29 14:15:57 +0100 | [diff] [blame] | 95 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 96 | 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 Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 101 | write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr); |
| 102 | } |
| 103 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 104 | /* |
| 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 Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 108 | if (GET_M32(ep->spsr) == MODE32_hyp) |
| 109 | scr |= SCR_HCE_BIT; |
| 110 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 111 | /* |
| 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 Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 116 | 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 Papastamos | 1e6f93e | 2017-11-07 09:55:29 +0000 | [diff] [blame] | 128 | * 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 | ******************************************************************************/ |
| 132 | static void enable_extensions_nonsecure(int el2_unused) |
| 133 | { |
| 134 | #if IMAGE_BL32 |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 135 | #if ENABLE_AMU |
| 136 | amu_enable(el2_unused); |
| 137 | #endif |
Dimitris Papastamos | 1e6f93e | 2017-11-07 09:55:29 +0000 | [diff] [blame] | 138 | #endif |
| 139 | } |
| 140 | |
| 141 | /******************************************************************************* |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 142 | * The following function initializes the cpu_context for a CPU specified by |
| 143 | * its `cpu_idx` for first use, and sets the initial entrypoint state as |
| 144 | * specified by the entry_point_info structure. |
| 145 | ******************************************************************************/ |
| 146 | void cm_init_context_by_index(unsigned int cpu_idx, |
| 147 | const entry_point_info_t *ep) |
| 148 | { |
| 149 | cpu_context_t *ctx; |
| 150 | ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr)); |
Antonio Nino Diaz | 28dce9e | 2018-05-22 10:09:10 +0100 | [diff] [blame] | 151 | cm_setup_context(ctx, ep); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /******************************************************************************* |
| 155 | * The following function initializes the cpu_context for the current CPU |
| 156 | * for first use, and sets the initial entrypoint state as specified by the |
| 157 | * entry_point_info structure. |
| 158 | ******************************************************************************/ |
| 159 | void cm_init_my_context(const entry_point_info_t *ep) |
| 160 | { |
| 161 | cpu_context_t *ctx; |
| 162 | ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr)); |
Antonio Nino Diaz | 28dce9e | 2018-05-22 10:09:10 +0100 | [diff] [blame] | 163 | cm_setup_context(ctx, ep); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /******************************************************************************* |
| 167 | * Prepare the CPU system registers for first entry into secure or normal world |
| 168 | * |
| 169 | * If execution is requested to hyp mode, HSCTLR is initialized |
| 170 | * If execution is requested to non-secure PL1, and the CPU supports |
| 171 | * HYP mode then HYP mode is disabled by configuring all necessary HYP mode |
| 172 | * registers. |
| 173 | ******************************************************************************/ |
| 174 | void cm_prepare_el3_exit(uint32_t security_state) |
| 175 | { |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 176 | uint32_t hsctlr, scr; |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 177 | cpu_context_t *ctx = cm_get_context(security_state); |
Dimitris Papastamos | 1e6f93e | 2017-11-07 09:55:29 +0000 | [diff] [blame] | 178 | int el2_unused = 0; |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 179 | |
| 180 | assert(ctx); |
| 181 | |
| 182 | if (security_state == NON_SECURE) { |
| 183 | scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR); |
| 184 | if (scr & SCR_HCE_BIT) { |
| 185 | /* Use SCTLR value to initialize HSCTLR */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 186 | hsctlr = read_ctx_reg(get_regs_ctx(ctx), |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 187 | CTX_NS_SCTLR); |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 188 | hsctlr |= HSCTLR_RES1; |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 189 | /* Temporarily set the NS bit to access HSCTLR */ |
| 190 | write_scr(read_scr() | SCR_NS_BIT); |
| 191 | /* |
| 192 | * Make sure the write to SCR is complete so that |
| 193 | * we can access HSCTLR |
| 194 | */ |
| 195 | isb(); |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 196 | write_hsctlr(hsctlr); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 197 | isb(); |
| 198 | |
| 199 | write_scr(read_scr() & ~SCR_NS_BIT); |
| 200 | isb(); |
| 201 | } else if (read_id_pfr1() & |
| 202 | (ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) { |
Dimitris Papastamos | 1e6f93e | 2017-11-07 09:55:29 +0000 | [diff] [blame] | 203 | el2_unused = 1; |
| 204 | |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Set the NS bit to access NS copies of certain banked |
| 207 | * registers |
| 208 | */ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 209 | write_scr(read_scr() | SCR_NS_BIT); |
| 210 | isb(); |
| 211 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 212 | /* |
| 213 | * Hyp / PL2 present but unused, need to disable safely. |
| 214 | * HSCTLR can be ignored in this case. |
| 215 | * |
| 216 | * Set HCR to its architectural reset value so that |
| 217 | * Non-secure operations do not trap to Hyp mode. |
| 218 | */ |
| 219 | write_hcr(HCR_RESET_VAL); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 220 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 221 | /* |
| 222 | * Set HCPTR to its architectural reset value so that |
| 223 | * Non-secure access from EL1 or EL0 to trace and to |
| 224 | * Advanced SIMD and floating point functionality does |
| 225 | * not trap to Hyp mode. |
| 226 | */ |
| 227 | write_hcptr(HCPTR_RESET_VAL); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 228 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 229 | /* |
| 230 | * Initialise CNTHCTL. All fields are architecturally |
| 231 | * UNKNOWN on reset and are set to zero except for |
| 232 | * field(s) listed below. |
| 233 | * |
| 234 | * CNTHCTL.PL1PCEN: Disable traps to Hyp mode of |
| 235 | * Non-secure EL0 and EL1 accessed to the physical |
| 236 | * timer registers. |
| 237 | * |
| 238 | * CNTHCTL.PL1PCTEN: Disable traps to Hyp mode of |
| 239 | * Non-secure EL0 and EL1 accessed to the physical |
| 240 | * counter registers. |
| 241 | */ |
| 242 | write_cnthctl(CNTHCTL_RESET_VAL | |
| 243 | PL1PCEN_BIT | PL1PCTEN_BIT); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 244 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Initialise CNTVOFF to zero as it resets to an |
| 247 | * IMPLEMENTATION DEFINED value. |
| 248 | */ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 249 | write64_cntvoff(0); |
| 250 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 251 | /* |
| 252 | * Set VPIDR and VMPIDR to match MIDR_EL1 and MPIDR |
| 253 | * respectively. |
| 254 | */ |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 255 | write_vpidr(read_midr()); |
| 256 | write_vmpidr(read_mpidr()); |
| 257 | |
| 258 | /* |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 259 | * Initialise VTTBR, setting all fields rather than |
| 260 | * relying on the hw. Some fields are architecturally |
| 261 | * UNKNOWN at reset. |
| 262 | * |
| 263 | * VTTBR.VMID: Set to zero which is the architecturally |
| 264 | * defined reset value. Even though EL1&0 stage 2 |
| 265 | * address translation is disabled, cache maintenance |
| 266 | * operations depend on the VMID. |
| 267 | * |
| 268 | * VTTBR.BADDR: Set to zero as EL1&0 stage 2 address |
| 269 | * translation is disabled. |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 270 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 271 | write64_vttbr(VTTBR_RESET_VAL & |
| 272 | ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
| 273 | | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT))); |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 274 | |
| 275 | /* |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 276 | * Initialise HDCR, setting all the fields rather than |
| 277 | * relying on hw. |
| 278 | * |
| 279 | * HDCR.HPMN: Set to value of PMCR.N which is the |
| 280 | * architecturally-defined reset value. |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 281 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 282 | write_hdcr(HDCR_RESET_VAL | |
| 283 | ((read_pmcr() & PMCR_N_BITS) >> PMCR_N_SHIFT)); |
David Cunado | c14b08e | 2016-11-25 00:21:59 +0000 | [diff] [blame] | 284 | |
| 285 | /* |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 286 | * Set HSTR to its architectural reset value so that |
| 287 | * access to system registers in the cproc=1111 |
| 288 | * encoding space do not trap to Hyp mode. |
| 289 | */ |
| 290 | write_hstr(HSTR_RESET_VAL); |
| 291 | /* |
| 292 | * Set CNTHP_CTL to its architectural reset value to |
| 293 | * disable the EL2 physical timer and prevent timer |
| 294 | * interrupts. Some fields are architecturally UNKNOWN |
| 295 | * on reset and are set to zero. |
David Cunado | c14b08e | 2016-11-25 00:21:59 +0000 | [diff] [blame] | 296 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 297 | write_cnthp_ctl(CNTHP_CTL_RESET_VAL); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 298 | isb(); |
| 299 | |
| 300 | write_scr(read_scr() & ~SCR_NS_BIT); |
| 301 | isb(); |
| 302 | } |
Dimitris Papastamos | 1e6f93e | 2017-11-07 09:55:29 +0000 | [diff] [blame] | 303 | enable_extensions_nonsecure(el2_unused); |
Soby Mathew | 748be1d | 2016-05-05 14:10:46 +0100 | [diff] [blame] | 304 | } |
| 305 | } |