1e33b78a6SSoby Mathew /* 2*32f0d3c6SDouglas Raillard * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3e33b78a6SSoby Mathew * 4e33b78a6SSoby Mathew * Redistribution and use in source and binary forms, with or without 5e33b78a6SSoby Mathew * modification, are permitted provided that the following conditions are met: 6e33b78a6SSoby Mathew * 7e33b78a6SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8e33b78a6SSoby Mathew * list of conditions and the following disclaimer. 9e33b78a6SSoby Mathew * 10e33b78a6SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11e33b78a6SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12e33b78a6SSoby Mathew * and/or other materials provided with the distribution. 13e33b78a6SSoby Mathew * 14e33b78a6SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15e33b78a6SSoby Mathew * to endorse or promote products derived from this software without specific 16e33b78a6SSoby Mathew * prior written permission. 17e33b78a6SSoby Mathew * 18e33b78a6SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19e33b78a6SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20e33b78a6SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21e33b78a6SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22e33b78a6SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23e33b78a6SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24e33b78a6SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25e33b78a6SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26e33b78a6SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27e33b78a6SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28e33b78a6SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29e33b78a6SSoby Mathew */ 30e33b78a6SSoby Mathew 31e33b78a6SSoby Mathew #include <arch.h> 32e33b78a6SSoby Mathew #include <arch_helpers.h> 33e33b78a6SSoby Mathew #include <assert.h> 34e33b78a6SSoby Mathew #include <bl_common.h> 35e33b78a6SSoby Mathew #include <context.h> 36e33b78a6SSoby Mathew #include <context_mgmt.h> 37e33b78a6SSoby Mathew #include <platform.h> 38e33b78a6SSoby Mathew #include <platform_def.h> 39e33b78a6SSoby Mathew #include <smcc_helpers.h> 40e33b78a6SSoby Mathew #include <string.h> 41*32f0d3c6SDouglas Raillard #include <utils.h> 42e33b78a6SSoby Mathew 43e33b78a6SSoby Mathew /******************************************************************************* 44e33b78a6SSoby Mathew * Context management library initialisation routine. This library is used by 45e33b78a6SSoby Mathew * runtime services to share pointers to 'cpu_context' structures for the secure 46e33b78a6SSoby Mathew * and non-secure states. Management of the structures and their associated 47e33b78a6SSoby Mathew * memory is not done by the context management library e.g. the PSCI service 48e33b78a6SSoby Mathew * manages the cpu context used for entry from and exit to the non-secure state. 49e33b78a6SSoby Mathew * The Secure payload manages the context(s) corresponding to the secure state. 50e33b78a6SSoby Mathew * It also uses this library to get access to the non-secure 51e33b78a6SSoby Mathew * state cpu context pointers. 52e33b78a6SSoby Mathew ******************************************************************************/ 53e33b78a6SSoby Mathew void cm_init(void) 54e33b78a6SSoby Mathew { 55e33b78a6SSoby Mathew /* 56e33b78a6SSoby Mathew * The context management library has only global data to initialize, but 57e33b78a6SSoby Mathew * that will be done when the BSS is zeroed out 58e33b78a6SSoby Mathew */ 59e33b78a6SSoby Mathew } 60e33b78a6SSoby Mathew 61e33b78a6SSoby Mathew /******************************************************************************* 62e33b78a6SSoby Mathew * The following function initializes the cpu_context 'ctx' for 63e33b78a6SSoby Mathew * first use, and sets the initial entrypoint state as specified by the 64e33b78a6SSoby Mathew * entry_point_info structure. 65e33b78a6SSoby Mathew * 66e33b78a6SSoby Mathew * The security state to initialize is determined by the SECURE attribute 67e33b78a6SSoby Mathew * of the entry_point_info. The function returns a pointer to the initialized 68e33b78a6SSoby Mathew * context and sets this as the next context to return to. 69e33b78a6SSoby Mathew * 70e33b78a6SSoby Mathew * The EE and ST attributes are used to configure the endianness and secure 71e33b78a6SSoby Mathew * timer availability for the new execution context. 72e33b78a6SSoby Mathew * 73e33b78a6SSoby Mathew * To prepare the register state for entry call cm_prepare_el3_exit() and 74e33b78a6SSoby Mathew * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to 75e33b78a6SSoby Mathew * cm_e1_sysreg_context_restore(). 76e33b78a6SSoby Mathew ******************************************************************************/ 77e33b78a6SSoby Mathew static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep) 78e33b78a6SSoby Mathew { 79e33b78a6SSoby Mathew unsigned int security_state; 80e33b78a6SSoby Mathew uint32_t scr, sctlr; 81e33b78a6SSoby Mathew regs_t *reg_ctx; 82e33b78a6SSoby Mathew 83e33b78a6SSoby Mathew assert(ctx); 84e33b78a6SSoby Mathew 85e33b78a6SSoby Mathew security_state = GET_SECURITY_STATE(ep->h.attr); 86e33b78a6SSoby Mathew 87e33b78a6SSoby Mathew /* Clear any residual register values from the context */ 88*32f0d3c6SDouglas Raillard zeromem(ctx, sizeof(*ctx)); 89e33b78a6SSoby Mathew 909e3b4cbbSSoby Mathew reg_ctx = get_regs_ctx(ctx); 919e3b4cbbSSoby Mathew 92e33b78a6SSoby Mathew /* 93e33b78a6SSoby Mathew * Base the context SCR on the current value, adjust for entry point 94e33b78a6SSoby Mathew * specific requirements 95e33b78a6SSoby Mathew */ 96e33b78a6SSoby Mathew scr = read_scr(); 97e33b78a6SSoby Mathew scr &= ~(SCR_NS_BIT | SCR_HCE_BIT); 98e33b78a6SSoby Mathew 99e33b78a6SSoby Mathew if (security_state != SECURE) 100e33b78a6SSoby Mathew scr |= SCR_NS_BIT; 101e33b78a6SSoby Mathew 102e33b78a6SSoby Mathew /* 103e33b78a6SSoby Mathew * Set up SCTLR for the Non Secure context. 104e33b78a6SSoby Mathew * EE bit is taken from the entrypoint attributes 105e33b78a6SSoby Mathew * M, C and I bits must be zero (as required by PSCI specification) 106e33b78a6SSoby Mathew * 107e33b78a6SSoby Mathew * The target exception level is based on the spsr mode requested. 108e33b78a6SSoby Mathew * If execution is requested to hyp mode, HVC is enabled 109e33b78a6SSoby Mathew * via SCR.HCE. 110e33b78a6SSoby Mathew * 111e33b78a6SSoby Mathew * Always compute the SCTLR_EL1 value and save in the cpu_context 112e33b78a6SSoby Mathew * - the HYP registers are set up by cm_preapre_ns_entry() as they 113e33b78a6SSoby Mathew * are not part of the stored cpu_context 114e33b78a6SSoby Mathew * 115e33b78a6SSoby Mathew * TODO: In debug builds the spsr should be validated and checked 116e33b78a6SSoby Mathew * against the CPU support, security state, endianness and pc 117e33b78a6SSoby Mathew */ 118e33b78a6SSoby Mathew if (security_state != SECURE) { 119e33b78a6SSoby Mathew sctlr = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0; 120b7b0787dSSoby Mathew /* 121b7b0787dSSoby Mathew * In addition to SCTLR_RES1, set the CP15_BEN, nTWI & nTWE 122b7b0787dSSoby Mathew * bits that architecturally reset to 1. 123b7b0787dSSoby Mathew */ 124b7b0787dSSoby Mathew sctlr |= SCTLR_RES1 | SCTLR_CP15BEN_BIT | 125b7b0787dSSoby Mathew SCTLR_NTWI_BIT | SCTLR_NTWE_BIT; 126e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr); 127e33b78a6SSoby Mathew } 128e33b78a6SSoby Mathew 129e33b78a6SSoby Mathew if (GET_M32(ep->spsr) == MODE32_hyp) 130e33b78a6SSoby Mathew scr |= SCR_HCE_BIT; 131e33b78a6SSoby Mathew 132e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_SCR, scr); 133e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_LR, ep->pc); 134e33b78a6SSoby Mathew write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr); 135e33b78a6SSoby Mathew 136e33b78a6SSoby Mathew /* 137e33b78a6SSoby Mathew * Store the r0-r3 value from the entrypoint into the context 138e33b78a6SSoby Mathew * Use memcpy as we are in control of the layout of the structures 139e33b78a6SSoby Mathew */ 140e33b78a6SSoby Mathew memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t)); 141e33b78a6SSoby Mathew } 142e33b78a6SSoby Mathew 143e33b78a6SSoby Mathew /******************************************************************************* 144e33b78a6SSoby Mathew * The following function initializes the cpu_context for a CPU specified by 145e33b78a6SSoby Mathew * its `cpu_idx` for first use, and sets the initial entrypoint state as 146e33b78a6SSoby Mathew * specified by the entry_point_info structure. 147e33b78a6SSoby Mathew ******************************************************************************/ 148e33b78a6SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx, 149e33b78a6SSoby Mathew const entry_point_info_t *ep) 150e33b78a6SSoby Mathew { 151e33b78a6SSoby Mathew cpu_context_t *ctx; 152e33b78a6SSoby Mathew ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr)); 153e33b78a6SSoby Mathew cm_init_context_common(ctx, ep); 154e33b78a6SSoby Mathew } 155e33b78a6SSoby Mathew 156e33b78a6SSoby Mathew /******************************************************************************* 157e33b78a6SSoby Mathew * The following function initializes the cpu_context for the current CPU 158e33b78a6SSoby Mathew * for first use, and sets the initial entrypoint state as specified by the 159e33b78a6SSoby Mathew * entry_point_info structure. 160e33b78a6SSoby Mathew ******************************************************************************/ 161e33b78a6SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep) 162e33b78a6SSoby Mathew { 163e33b78a6SSoby Mathew cpu_context_t *ctx; 164e33b78a6SSoby Mathew ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr)); 165e33b78a6SSoby Mathew cm_init_context_common(ctx, ep); 166e33b78a6SSoby Mathew } 167e33b78a6SSoby Mathew 168e33b78a6SSoby Mathew /******************************************************************************* 169e33b78a6SSoby Mathew * Prepare the CPU system registers for first entry into secure or normal world 170e33b78a6SSoby Mathew * 171e33b78a6SSoby Mathew * If execution is requested to hyp mode, HSCTLR is initialized 172e33b78a6SSoby Mathew * If execution is requested to non-secure PL1, and the CPU supports 173e33b78a6SSoby Mathew * HYP mode then HYP mode is disabled by configuring all necessary HYP mode 174e33b78a6SSoby Mathew * registers. 175e33b78a6SSoby Mathew ******************************************************************************/ 176e33b78a6SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state) 177e33b78a6SSoby Mathew { 178e33b78a6SSoby Mathew uint32_t sctlr, scr, hcptr; 179e33b78a6SSoby Mathew cpu_context_t *ctx = cm_get_context(security_state); 180e33b78a6SSoby Mathew 181e33b78a6SSoby Mathew assert(ctx); 182e33b78a6SSoby Mathew 183e33b78a6SSoby Mathew if (security_state == NON_SECURE) { 184e33b78a6SSoby Mathew scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR); 185e33b78a6SSoby Mathew if (scr & SCR_HCE_BIT) { 186e33b78a6SSoby Mathew /* Use SCTLR value to initialize HSCTLR */ 187e33b78a6SSoby Mathew sctlr = read_ctx_reg(get_regs_ctx(ctx), 188e33b78a6SSoby Mathew CTX_NS_SCTLR); 189e33b78a6SSoby Mathew sctlr |= HSCTLR_RES1; 190e33b78a6SSoby Mathew /* Temporarily set the NS bit to access HSCTLR */ 191e33b78a6SSoby Mathew write_scr(read_scr() | SCR_NS_BIT); 192e33b78a6SSoby Mathew /* 193e33b78a6SSoby Mathew * Make sure the write to SCR is complete so that 194e33b78a6SSoby Mathew * we can access HSCTLR 195e33b78a6SSoby Mathew */ 196e33b78a6SSoby Mathew isb(); 197e33b78a6SSoby Mathew write_hsctlr(sctlr); 198e33b78a6SSoby Mathew isb(); 199e33b78a6SSoby Mathew 200e33b78a6SSoby Mathew write_scr(read_scr() & ~SCR_NS_BIT); 201e33b78a6SSoby Mathew isb(); 202e33b78a6SSoby Mathew } else if (read_id_pfr1() & 203e33b78a6SSoby Mathew (ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) { 204495f3d3cSDavid Cunado /* 205495f3d3cSDavid Cunado * Set the NS bit to access NS copies of certain banked 206495f3d3cSDavid Cunado * registers 207495f3d3cSDavid Cunado */ 208e33b78a6SSoby Mathew write_scr(read_scr() | SCR_NS_BIT); 209e33b78a6SSoby Mathew isb(); 210e33b78a6SSoby Mathew 211e33b78a6SSoby Mathew /* PL2 present but unused, need to disable safely */ 212e33b78a6SSoby Mathew write_hcr(0); 213e33b78a6SSoby Mathew 214e33b78a6SSoby Mathew /* HSCTLR : can be ignored when bypassing */ 215e33b78a6SSoby Mathew 216e33b78a6SSoby Mathew /* HCPTR : disable all traps TCPAC, TTA, TCP */ 217e33b78a6SSoby Mathew hcptr = read_hcptr(); 218e33b78a6SSoby Mathew hcptr &= ~(TCPAC_BIT | TTA_BIT | TCP11_BIT | TCP10_BIT); 219e33b78a6SSoby Mathew write_hcptr(hcptr); 220e33b78a6SSoby Mathew 221e33b78a6SSoby Mathew /* Enable EL1 access to timer */ 222e33b78a6SSoby Mathew write_cnthctl(PL1PCEN_BIT | PL1PCTEN_BIT); 223e33b78a6SSoby Mathew 224e33b78a6SSoby Mathew /* Reset CNTVOFF_EL2 */ 225e33b78a6SSoby Mathew write64_cntvoff(0); 226e33b78a6SSoby Mathew 227e33b78a6SSoby Mathew /* Set VPIDR, VMPIDR to match MIDR, MPIDR */ 228e33b78a6SSoby Mathew write_vpidr(read_midr()); 229e33b78a6SSoby Mathew write_vmpidr(read_mpidr()); 230e33b78a6SSoby Mathew 231e33b78a6SSoby Mathew /* 232e33b78a6SSoby Mathew * Reset VTTBR. 233e33b78a6SSoby Mathew * Needed because cache maintenance operations depend on 234e33b78a6SSoby Mathew * the VMID even when non-secure EL1&0 stage 2 address 235e33b78a6SSoby Mathew * translation are disabled. 236e33b78a6SSoby Mathew */ 237e33b78a6SSoby Mathew write64_vttbr(0); 238495f3d3cSDavid Cunado 239495f3d3cSDavid Cunado /* 240495f3d3cSDavid Cunado * Avoid unexpected debug traps in case where HDCR 241495f3d3cSDavid Cunado * is not completely reset by the hardware - set 242495f3d3cSDavid Cunado * HDCR.HPMN to PMCR.N and zero the remaining bits. 243495f3d3cSDavid Cunado * The HDCR.HPMN and PMCR.N fields are the same size 244495f3d3cSDavid Cunado * (5 bits) and HPMN is at offset zero within HDCR. 245495f3d3cSDavid Cunado */ 246495f3d3cSDavid Cunado write_hdcr((read_pmcr() & PMCR_N_BITS) >> PMCR_N_SHIFT); 247939f66d6SDavid Cunado 248939f66d6SDavid Cunado /* 249939f66d6SDavid Cunado * Reset CNTHP_CTL to disable the EL2 physical timer and 250939f66d6SDavid Cunado * therefore prevent timer interrupts. 251939f66d6SDavid Cunado */ 252939f66d6SDavid Cunado write_cnthp_ctl(0); 253e33b78a6SSoby Mathew isb(); 254e33b78a6SSoby Mathew 255e33b78a6SSoby Mathew write_scr(read_scr() & ~SCR_NS_BIT); 256e33b78a6SSoby Mathew isb(); 257e33b78a6SSoby Mathew } 258e33b78a6SSoby Mathew } 259e33b78a6SSoby Mathew } 260