1e33b78a6SSoby Mathew /* 232f0d3c6SDouglas Raillard * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3e33b78a6SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5e33b78a6SSoby Mathew */ 6e33b78a6SSoby Mathew 7e33b78a6SSoby Mathew #include <arch.h> 8e33b78a6SSoby Mathew #include <arch_helpers.h> 9e33b78a6SSoby Mathew #include <assert.h> 10e33b78a6SSoby Mathew #include <bl_common.h> 11e33b78a6SSoby Mathew #include <context.h> 12e33b78a6SSoby Mathew #include <context_mgmt.h> 13e33b78a6SSoby Mathew #include <platform.h> 14e33b78a6SSoby Mathew #include <platform_def.h> 15e33b78a6SSoby Mathew #include <smcc_helpers.h> 16e33b78a6SSoby Mathew #include <string.h> 1732f0d3c6SDouglas Raillard #include <utils.h> 18e33b78a6SSoby Mathew 19e33b78a6SSoby Mathew /******************************************************************************* 20e33b78a6SSoby Mathew * Context management library initialisation routine. This library is used by 21e33b78a6SSoby Mathew * runtime services to share pointers to 'cpu_context' structures for the secure 22e33b78a6SSoby Mathew * and non-secure states. Management of the structures and their associated 23e33b78a6SSoby Mathew * memory is not done by the context management library e.g. the PSCI service 24e33b78a6SSoby Mathew * manages the cpu context used for entry from and exit to the non-secure state. 25e33b78a6SSoby Mathew * The Secure payload manages the context(s) corresponding to the secure state. 26e33b78a6SSoby Mathew * It also uses this library to get access to the non-secure 27e33b78a6SSoby Mathew * state cpu context pointers. 28e33b78a6SSoby Mathew ******************************************************************************/ 29e33b78a6SSoby Mathew void cm_init(void) 30e33b78a6SSoby Mathew { 31e33b78a6SSoby Mathew /* 32e33b78a6SSoby Mathew * The context management library has only global data to initialize, but 33e33b78a6SSoby Mathew * that will be done when the BSS is zeroed out 34e33b78a6SSoby Mathew */ 35e33b78a6SSoby Mathew } 36e33b78a6SSoby Mathew 37e33b78a6SSoby Mathew /******************************************************************************* 38e33b78a6SSoby Mathew * The following function initializes the cpu_context 'ctx' for 39e33b78a6SSoby Mathew * first use, and sets the initial entrypoint state as specified by the 40e33b78a6SSoby Mathew * entry_point_info structure. 41e33b78a6SSoby Mathew * 42e33b78a6SSoby Mathew * The security state to initialize is determined by the SECURE attribute 43e33b78a6SSoby Mathew * of the entry_point_info. The function returns a pointer to the initialized 44e33b78a6SSoby Mathew * context and sets this as the next context to return to. 45e33b78a6SSoby Mathew * 46e33b78a6SSoby Mathew * The EE and ST attributes are used to configure the endianness and secure 47e33b78a6SSoby Mathew * timer availability for the new execution context. 48e33b78a6SSoby Mathew * 49e33b78a6SSoby Mathew * To prepare the register state for entry call cm_prepare_el3_exit() and 50e33b78a6SSoby Mathew * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to 51e33b78a6SSoby Mathew * cm_e1_sysreg_context_restore(). 52e33b78a6SSoby Mathew ******************************************************************************/ 53e33b78a6SSoby Mathew static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep) 54e33b78a6SSoby Mathew { 55e33b78a6SSoby Mathew unsigned int security_state; 56e33b78a6SSoby Mathew uint32_t scr, sctlr; 57e33b78a6SSoby Mathew regs_t *reg_ctx; 58e33b78a6SSoby Mathew 59e33b78a6SSoby Mathew assert(ctx); 60e33b78a6SSoby Mathew 61e33b78a6SSoby Mathew security_state = GET_SECURITY_STATE(ep->h.attr); 62e33b78a6SSoby Mathew 63e33b78a6SSoby Mathew /* Clear any residual register values from the context */ 6432f0d3c6SDouglas Raillard zeromem(ctx, sizeof(*ctx)); 65e33b78a6SSoby Mathew 669e3b4cbbSSoby Mathew reg_ctx = get_regs_ctx(ctx); 679e3b4cbbSSoby Mathew 68e33b78a6SSoby Mathew /* 69e33b78a6SSoby Mathew * Base the context SCR on the current value, adjust for entry point 70e33b78a6SSoby Mathew * specific requirements 71e33b78a6SSoby Mathew */ 72e33b78a6SSoby Mathew scr = read_scr(); 73e33b78a6SSoby Mathew scr &= ~(SCR_NS_BIT | SCR_HCE_BIT); 74e33b78a6SSoby Mathew 75e33b78a6SSoby Mathew if (security_state != SECURE) 76e33b78a6SSoby Mathew scr |= SCR_NS_BIT; 77e33b78a6SSoby Mathew 78e33b78a6SSoby Mathew if (security_state != SECURE) { 79b7b0787dSSoby Mathew /* 8018f2efd6SDavid Cunado * Set up SCTLR for the Non-secure context. 8118f2efd6SDavid Cunado * 8218f2efd6SDavid Cunado * SCTLR.EE: Endianness is taken from the entrypoint attributes. 8318f2efd6SDavid Cunado * 8418f2efd6SDavid Cunado * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as 8518f2efd6SDavid Cunado * required by PSCI specification) 8618f2efd6SDavid Cunado * 8718f2efd6SDavid Cunado * Set remaining SCTLR fields to their architecturally defined 8818f2efd6SDavid Cunado * values. Some fields reset to an IMPLEMENTATION DEFINED value: 8918f2efd6SDavid Cunado * 9018f2efd6SDavid Cunado * SCTLR.TE: Set to zero so that exceptions to an Exception 9118f2efd6SDavid Cunado * Level executing at PL1 are taken to A32 state. 9218f2efd6SDavid Cunado * 9318f2efd6SDavid Cunado * SCTLR.V: Set to zero to select the normal exception vectors 9418f2efd6SDavid Cunado * with base address held in VBAR. 95b7b0787dSSoby Mathew */ 9618f2efd6SDavid Cunado assert(((ep->spsr >> SPSR_E_SHIFT) & SPSR_E_MASK) == 9718f2efd6SDavid Cunado (EP_GET_EE(ep->h.attr) >> EP_EE_SHIFT)); 9818f2efd6SDavid Cunado 9918f2efd6SDavid Cunado sctlr = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0; 10018f2efd6SDavid Cunado sctlr |= (SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_V_BIT)); 101e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr); 102e33b78a6SSoby Mathew } 103e33b78a6SSoby Mathew 10418f2efd6SDavid Cunado /* 10518f2efd6SDavid Cunado * The target exception level is based on the spsr mode requested. If 10618f2efd6SDavid Cunado * execution is requested to hyp mode, HVC is enabled via SCR.HCE. 10718f2efd6SDavid Cunado */ 108e33b78a6SSoby Mathew if (GET_M32(ep->spsr) == MODE32_hyp) 109e33b78a6SSoby Mathew scr |= SCR_HCE_BIT; 110e33b78a6SSoby Mathew 11118f2efd6SDavid Cunado /* 11218f2efd6SDavid Cunado * Store the initialised values for SCTLR and SCR in the cpu_context. 11318f2efd6SDavid Cunado * The Hyp mode registers are not part of the saved context and are 11418f2efd6SDavid Cunado * set-up in cm_prepare_el3_exit(). 11518f2efd6SDavid Cunado */ 116e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_SCR, scr); 117e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_LR, ep->pc); 118e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr); 119e33b78a6SSoby Mathew 120e33b78a6SSoby Mathew /* 121e33b78a6SSoby Mathew * Store the r0-r3 value from the entrypoint into the context 122e33b78a6SSoby Mathew * Use memcpy as we are in control of the layout of the structures 123e33b78a6SSoby Mathew */ 124e33b78a6SSoby Mathew memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t)); 125e33b78a6SSoby Mathew } 126e33b78a6SSoby Mathew 127e33b78a6SSoby Mathew /******************************************************************************* 128*0fd0f222SDimitris Papastamos * Enable architecture extensions on first entry to Non-secure world. 129*0fd0f222SDimitris Papastamos * When EL2 is implemented but unused `el2_unused` is non-zero, otherwise 130*0fd0f222SDimitris Papastamos * it is zero. 131*0fd0f222SDimitris Papastamos ******************************************************************************/ 132*0fd0f222SDimitris Papastamos static void enable_extensions_nonsecure(int el2_unused) 133*0fd0f222SDimitris Papastamos { 134*0fd0f222SDimitris Papastamos #if IMAGE_BL32 135*0fd0f222SDimitris Papastamos #endif 136*0fd0f222SDimitris Papastamos } 137*0fd0f222SDimitris Papastamos 138*0fd0f222SDimitris Papastamos /******************************************************************************* 139e33b78a6SSoby Mathew * The following function initializes the cpu_context for a CPU specified by 140e33b78a6SSoby Mathew * its `cpu_idx` for first use, and sets the initial entrypoint state as 141e33b78a6SSoby Mathew * specified by the entry_point_info structure. 142e33b78a6SSoby Mathew ******************************************************************************/ 143e33b78a6SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx, 144e33b78a6SSoby Mathew const entry_point_info_t *ep) 145e33b78a6SSoby Mathew { 146e33b78a6SSoby Mathew cpu_context_t *ctx; 147e33b78a6SSoby Mathew ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr)); 148e33b78a6SSoby Mathew cm_init_context_common(ctx, ep); 149e33b78a6SSoby Mathew } 150e33b78a6SSoby Mathew 151e33b78a6SSoby Mathew /******************************************************************************* 152e33b78a6SSoby Mathew * The following function initializes the cpu_context for the current CPU 153e33b78a6SSoby Mathew * for first use, and sets the initial entrypoint state as specified by the 154e33b78a6SSoby Mathew * entry_point_info structure. 155e33b78a6SSoby Mathew ******************************************************************************/ 156e33b78a6SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep) 157e33b78a6SSoby Mathew { 158e33b78a6SSoby Mathew cpu_context_t *ctx; 159e33b78a6SSoby Mathew ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr)); 160e33b78a6SSoby Mathew cm_init_context_common(ctx, ep); 161e33b78a6SSoby Mathew } 162e33b78a6SSoby Mathew 163e33b78a6SSoby Mathew /******************************************************************************* 164e33b78a6SSoby Mathew * Prepare the CPU system registers for first entry into secure or normal world 165e33b78a6SSoby Mathew * 166e33b78a6SSoby Mathew * If execution is requested to hyp mode, HSCTLR is initialized 167e33b78a6SSoby Mathew * If execution is requested to non-secure PL1, and the CPU supports 168e33b78a6SSoby Mathew * HYP mode then HYP mode is disabled by configuring all necessary HYP mode 169e33b78a6SSoby Mathew * registers. 170e33b78a6SSoby Mathew ******************************************************************************/ 171e33b78a6SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state) 172e33b78a6SSoby Mathew { 17318f2efd6SDavid Cunado uint32_t hsctlr, scr; 174e33b78a6SSoby Mathew cpu_context_t *ctx = cm_get_context(security_state); 175*0fd0f222SDimitris Papastamos int el2_unused = 0; 176e33b78a6SSoby Mathew 177e33b78a6SSoby Mathew assert(ctx); 178e33b78a6SSoby Mathew 179e33b78a6SSoby Mathew if (security_state == NON_SECURE) { 180e33b78a6SSoby Mathew scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR); 181e33b78a6SSoby Mathew if (scr & SCR_HCE_BIT) { 182e33b78a6SSoby Mathew /* Use SCTLR value to initialize HSCTLR */ 18318f2efd6SDavid Cunado hsctlr = read_ctx_reg(get_regs_ctx(ctx), 184e33b78a6SSoby Mathew CTX_NS_SCTLR); 18518f2efd6SDavid Cunado hsctlr |= HSCTLR_RES1; 186e33b78a6SSoby Mathew /* Temporarily set the NS bit to access HSCTLR */ 187e33b78a6SSoby Mathew write_scr(read_scr() | SCR_NS_BIT); 188e33b78a6SSoby Mathew /* 189e33b78a6SSoby Mathew * Make sure the write to SCR is complete so that 190e33b78a6SSoby Mathew * we can access HSCTLR 191e33b78a6SSoby Mathew */ 192e33b78a6SSoby Mathew isb(); 19318f2efd6SDavid Cunado write_hsctlr(hsctlr); 194e33b78a6SSoby Mathew isb(); 195e33b78a6SSoby Mathew 196e33b78a6SSoby Mathew write_scr(read_scr() & ~SCR_NS_BIT); 197e33b78a6SSoby Mathew isb(); 198e33b78a6SSoby Mathew } else if (read_id_pfr1() & 199e33b78a6SSoby Mathew (ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) { 200*0fd0f222SDimitris Papastamos el2_unused = 1; 201*0fd0f222SDimitris Papastamos 202495f3d3cSDavid Cunado /* 203495f3d3cSDavid Cunado * Set the NS bit to access NS copies of certain banked 204495f3d3cSDavid Cunado * registers 205495f3d3cSDavid Cunado */ 206e33b78a6SSoby Mathew write_scr(read_scr() | SCR_NS_BIT); 207e33b78a6SSoby Mathew isb(); 208e33b78a6SSoby Mathew 20918f2efd6SDavid Cunado /* 21018f2efd6SDavid Cunado * Hyp / PL2 present but unused, need to disable safely. 21118f2efd6SDavid Cunado * HSCTLR can be ignored in this case. 21218f2efd6SDavid Cunado * 21318f2efd6SDavid Cunado * Set HCR to its architectural reset value so that 21418f2efd6SDavid Cunado * Non-secure operations do not trap to Hyp mode. 21518f2efd6SDavid Cunado */ 21618f2efd6SDavid Cunado write_hcr(HCR_RESET_VAL); 217e33b78a6SSoby Mathew 21818f2efd6SDavid Cunado /* 21918f2efd6SDavid Cunado * Set HCPTR to its architectural reset value so that 22018f2efd6SDavid Cunado * Non-secure access from EL1 or EL0 to trace and to 22118f2efd6SDavid Cunado * Advanced SIMD and floating point functionality does 22218f2efd6SDavid Cunado * not trap to Hyp mode. 22318f2efd6SDavid Cunado */ 22418f2efd6SDavid Cunado write_hcptr(HCPTR_RESET_VAL); 225e33b78a6SSoby Mathew 22618f2efd6SDavid Cunado /* 22718f2efd6SDavid Cunado * Initialise CNTHCTL. All fields are architecturally 22818f2efd6SDavid Cunado * UNKNOWN on reset and are set to zero except for 22918f2efd6SDavid Cunado * field(s) listed below. 23018f2efd6SDavid Cunado * 23118f2efd6SDavid Cunado * CNTHCTL.PL1PCEN: Disable traps to Hyp mode of 23218f2efd6SDavid Cunado * Non-secure EL0 and EL1 accessed to the physical 23318f2efd6SDavid Cunado * timer registers. 23418f2efd6SDavid Cunado * 23518f2efd6SDavid Cunado * CNTHCTL.PL1PCTEN: Disable traps to Hyp mode of 23618f2efd6SDavid Cunado * Non-secure EL0 and EL1 accessed to the physical 23718f2efd6SDavid Cunado * counter registers. 23818f2efd6SDavid Cunado */ 23918f2efd6SDavid Cunado write_cnthctl(CNTHCTL_RESET_VAL | 24018f2efd6SDavid Cunado PL1PCEN_BIT | PL1PCTEN_BIT); 241e33b78a6SSoby Mathew 24218f2efd6SDavid Cunado /* 24318f2efd6SDavid Cunado * Initialise CNTVOFF to zero as it resets to an 24418f2efd6SDavid Cunado * IMPLEMENTATION DEFINED value. 24518f2efd6SDavid Cunado */ 246e33b78a6SSoby Mathew write64_cntvoff(0); 247e33b78a6SSoby Mathew 24818f2efd6SDavid Cunado /* 24918f2efd6SDavid Cunado * Set VPIDR and VMPIDR to match MIDR_EL1 and MPIDR 25018f2efd6SDavid Cunado * respectively. 25118f2efd6SDavid Cunado */ 252e33b78a6SSoby Mathew write_vpidr(read_midr()); 253e33b78a6SSoby Mathew write_vmpidr(read_mpidr()); 254e33b78a6SSoby Mathew 255e33b78a6SSoby Mathew /* 25618f2efd6SDavid Cunado * Initialise VTTBR, setting all fields rather than 25718f2efd6SDavid Cunado * relying on the hw. Some fields are architecturally 25818f2efd6SDavid Cunado * UNKNOWN at reset. 25918f2efd6SDavid Cunado * 26018f2efd6SDavid Cunado * VTTBR.VMID: Set to zero which is the architecturally 26118f2efd6SDavid Cunado * defined reset value. Even though EL1&0 stage 2 26218f2efd6SDavid Cunado * address translation is disabled, cache maintenance 26318f2efd6SDavid Cunado * operations depend on the VMID. 26418f2efd6SDavid Cunado * 26518f2efd6SDavid Cunado * VTTBR.BADDR: Set to zero as EL1&0 stage 2 address 26618f2efd6SDavid Cunado * translation is disabled. 267e33b78a6SSoby Mathew */ 26818f2efd6SDavid Cunado write64_vttbr(VTTBR_RESET_VAL & 26918f2efd6SDavid Cunado ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) 27018f2efd6SDavid Cunado | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT))); 271495f3d3cSDavid Cunado 272495f3d3cSDavid Cunado /* 27318f2efd6SDavid Cunado * Initialise HDCR, setting all the fields rather than 27418f2efd6SDavid Cunado * relying on hw. 27518f2efd6SDavid Cunado * 27618f2efd6SDavid Cunado * HDCR.HPMN: Set to value of PMCR.N which is the 27718f2efd6SDavid Cunado * architecturally-defined reset value. 278495f3d3cSDavid Cunado */ 27918f2efd6SDavid Cunado write_hdcr(HDCR_RESET_VAL | 28018f2efd6SDavid Cunado ((read_pmcr() & PMCR_N_BITS) >> PMCR_N_SHIFT)); 281939f66d6SDavid Cunado 282939f66d6SDavid Cunado /* 28318f2efd6SDavid Cunado * Set HSTR to its architectural reset value so that 28418f2efd6SDavid Cunado * access to system registers in the cproc=1111 28518f2efd6SDavid Cunado * encoding space do not trap to Hyp mode. 286939f66d6SDavid Cunado */ 28718f2efd6SDavid Cunado write_hstr(HSTR_RESET_VAL); 28818f2efd6SDavid Cunado /* 28918f2efd6SDavid Cunado * Set CNTHP_CTL to its architectural reset value to 29018f2efd6SDavid Cunado * disable the EL2 physical timer and prevent timer 29118f2efd6SDavid Cunado * interrupts. Some fields are architecturally UNKNOWN 29218f2efd6SDavid Cunado * on reset and are set to zero. 29318f2efd6SDavid Cunado */ 29418f2efd6SDavid Cunado write_cnthp_ctl(CNTHP_CTL_RESET_VAL); 295e33b78a6SSoby Mathew isb(); 296e33b78a6SSoby Mathew 297e33b78a6SSoby Mathew write_scr(read_scr() & ~SCR_NS_BIT); 298e33b78a6SSoby Mathew isb(); 299e33b78a6SSoby Mathew } 300*0fd0f222SDimitris Papastamos enable_extensions_nonsecure(el2_unused); 301e33b78a6SSoby Mathew } 302e33b78a6SSoby Mathew } 303