Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 1 | /* |
Douglas Raillard | 306593d | 2017-02-24 18:14:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __EL3_COMMON_MACROS_S__ |
| 8 | #define __EL3_COMMON_MACROS_S__ |
| 9 | |
| 10 | #include <arch.h> |
| 11 | #include <asm_macros.S> |
| 12 | #include <assert_macros.S> |
| 13 | |
| 14 | /* |
| 15 | * Helper macro to initialise EL3 registers we care about. |
| 16 | */ |
| 17 | .macro el3_arch_init_common _exception_vectors |
| 18 | /* --------------------------------------------------------------------- |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 19 | * SCTLR has already been initialised - read current value before |
| 20 | * modifying. |
| 21 | * |
| 22 | * SCTLR.I: Enable the instruction cache. |
| 23 | * |
| 24 | * SCTLR.A: Enable Alignment fault checking. All instructions that load |
| 25 | * or store one or more registers have an alignment check that the |
| 26 | * address being accessed is aligned to the size of the data element(s) |
| 27 | * being accessed. |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 28 | * --------------------------------------------------------------------- |
| 29 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 30 | ldr r1, =(SCTLR_I_BIT | SCTLR_A_BIT) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 31 | ldcopr r0, SCTLR |
| 32 | orr r0, r0, r1 |
| 33 | stcopr r0, SCTLR |
| 34 | isb |
| 35 | |
| 36 | /* --------------------------------------------------------------------- |
| 37 | * Set the exception vectors (VBAR/MVBAR). |
| 38 | * --------------------------------------------------------------------- |
| 39 | */ |
| 40 | ldr r0, =\_exception_vectors |
| 41 | stcopr r0, VBAR |
| 42 | stcopr r0, MVBAR |
| 43 | isb |
| 44 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 45 | /* --------------------------------------------------------------------- |
| 46 | * Initialise SCR, setting all fields rather than relying on the hw. |
| 47 | * |
| 48 | * SCR.SIF: Enabled so that Secure state instruction fetches from |
| 49 | * Non-secure memory are not permitted. |
| 50 | * --------------------------------------------------------------------- |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 51 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 52 | ldr r0, =(SCR_RESET_VAL | SCR_SIF_BIT) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 53 | stcopr r0, SCR |
| 54 | |
| 55 | /* ----------------------------------------------------- |
| 56 | * Enable the Asynchronous data abort now that the |
| 57 | * exception vectors have been setup. |
| 58 | * ----------------------------------------------------- |
| 59 | */ |
| 60 | cpsie a |
| 61 | isb |
| 62 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 63 | /* --------------------------------------------------------------------- |
| 64 | * Initialise NSACR, setting all the fields, except for the |
| 65 | * IMPLEMENTATION DEFINED field, rather than relying on the hw. Some |
| 66 | * fields are architecturally UNKNOWN on reset. |
| 67 | * |
| 68 | * NSACR_ENABLE_FP_ACCESS: Represents NSACR.cp11 and NSACR.cp10. The |
| 69 | * cp11 field is ignored, but is set to same value as cp10. The cp10 |
| 70 | * field is set to allow access to Advanced SIMD and floating point |
| 71 | * features from both Security states. |
| 72 | * --------------------------------------------------------------------- |
| 73 | */ |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 74 | ldcopr r0, NSACR |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 75 | and r0, r0, #NSACR_IMP_DEF_MASK |
| 76 | orr r0, r0, #(NSACR_RESET_VAL | NSACR_ENABLE_FP_ACCESS) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 77 | stcopr r0, NSACR |
| 78 | isb |
| 79 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 80 | /* --------------------------------------------------------------------- |
| 81 | * Initialise CPACR, setting all fields rather than relying on hw. Some |
| 82 | * fields are architecturally UNKNOWN on reset. |
| 83 | * |
| 84 | * CPACR.TRCDIS: Trap control for PL0 and PL1 System register accesses |
| 85 | * to trace registers. Set to zero to allow access. |
| 86 | * |
| 87 | * CPACR_ENABLE_FP_ACCESS: Represents CPACR.cp11 and CPACR.cp10. The |
| 88 | * cp11 field is ignored, but is set to same value as cp10. The cp10 |
| 89 | * field is set to allow full access from PL0 and PL1 to floating-point |
| 90 | * and Advanced SIMD features. |
| 91 | * --------------------------------------------------------------------- |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 92 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 93 | ldr r0, =((CPACR_RESET_VAL | CPACR_ENABLE_FP_ACCESS) & ~(TRCDIS_BIT)) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 94 | stcopr r0, CPACR |
| 95 | isb |
| 96 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 97 | /* --------------------------------------------------------------------- |
| 98 | * Initialise FPEXC, setting all fields rather than relying on hw. Some |
| 99 | * fields are architecturally UNKNOWN on reset and are set to zero |
| 100 | * except for field(s) listed below. |
| 101 | * |
| 102 | * FPEXC.EN: Enable access to Advanced SIMD and floating point features |
| 103 | * from all exception levels. |
| 104 | * --------------------------------------------------------------------- |
| 105 | */ |
| 106 | ldr r0, =(FPEXC_RESET_VAL | FPEXC_EN_BIT) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 107 | vmsr FPEXC, r0 |
| 108 | isb |
dp-arm | 595d0d5 | 2017-02-08 11:51:50 +0000 | [diff] [blame] | 109 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 110 | /* --------------------------------------------------------------------- |
| 111 | * Initialise SDCR, setting all the fields rather than relying on hw. |
| 112 | * |
| 113 | * SDCR.SPD: Disable AArch32 privileged debug. Debug exceptions from |
| 114 | * Secure EL1 are disabled. |
| 115 | * --------------------------------------------------------------------- |
| 116 | */ |
| 117 | ldr r0, =(SDCR_RESET_VAL | SDCR_SPD(SDCR_SPD_DISABLE)) |
dp-arm | 595d0d5 | 2017-02-08 11:51:50 +0000 | [diff] [blame] | 118 | stcopr r0, SDCR |
| 119 | |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 120 | .endm |
| 121 | |
| 122 | /* ----------------------------------------------------------------------------- |
| 123 | * This is the super set of actions that need to be performed during a cold boot |
| 124 | * or a warm boot in EL3. This code is shared by BL1 and BL32 (SP_MIN). |
| 125 | * |
| 126 | * This macro will always perform reset handling, architectural initialisations |
| 127 | * and stack setup. The rest of the actions are optional because they might not |
| 128 | * be needed, depending on the context in which this macro is called. This is |
| 129 | * why this macro is parameterised ; each parameter allows to enable/disable |
| 130 | * some actions. |
| 131 | * |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 132 | * _init_sctlr: |
| 133 | * Whether the macro needs to initialise the SCTLR register including |
| 134 | * configuring the endianness of data accesses. |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 135 | * |
| 136 | * _warm_boot_mailbox: |
| 137 | * Whether the macro needs to detect the type of boot (cold/warm). The |
| 138 | * detection is based on the platform entrypoint address : if it is zero |
| 139 | * then it is a cold boot, otherwise it is a warm boot. In the latter case, |
| 140 | * this macro jumps on the platform entrypoint address. |
| 141 | * |
| 142 | * _secondary_cold_boot: |
| 143 | * Whether the macro needs to identify the CPU that is calling it: primary |
| 144 | * CPU or secondary CPU. The primary CPU will be allowed to carry on with |
| 145 | * the platform initialisations, while the secondaries will be put in a |
| 146 | * platform-specific state in the meantime. |
| 147 | * |
| 148 | * If the caller knows this macro will only be called by the primary CPU |
| 149 | * then this parameter can be defined to 0 to skip this step. |
| 150 | * |
| 151 | * _init_memory: |
| 152 | * Whether the macro needs to initialise the memory. |
| 153 | * |
| 154 | * _init_c_runtime: |
| 155 | * Whether the macro needs to initialise the C runtime environment. |
| 156 | * |
| 157 | * _exception_vectors: |
| 158 | * Address of the exception vectors to program in the VBAR_EL3 register. |
| 159 | * ----------------------------------------------------------------------------- |
| 160 | */ |
| 161 | .macro el3_entrypoint_common \ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 162 | _init_sctlr, _warm_boot_mailbox, _secondary_cold_boot, \ |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 163 | _init_memory, _init_c_runtime, _exception_vectors |
| 164 | |
| 165 | /* Make sure we are in Secure Mode */ |
Antonio Nino Diaz | 7c65c1e | 2017-04-20 09:58:28 +0100 | [diff] [blame] | 166 | #if ENABLE_ASSERTIONS |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 167 | ldcopr r0, SCR |
| 168 | tst r0, #SCR_NS_BIT |
| 169 | ASM_ASSERT(eq) |
| 170 | #endif |
| 171 | |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 172 | .if \_init_sctlr |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 173 | /* ------------------------------------------------------------- |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 174 | * This is the initialisation of SCTLR and so must ensure that |
| 175 | * all fields are explicitly set rather than relying on hw. Some |
| 176 | * fields reset to an IMPLEMENTATION DEFINED value. |
| 177 | * |
| 178 | * SCTLR.TE: Set to zero so that exceptions to an Exception |
| 179 | * Level executing at PL1 are taken to A32 state. |
| 180 | * |
| 181 | * SCTLR.EE: Set the CPU endianness before doing anything that |
| 182 | * might involve memory reads or writes. Set to zero to select |
| 183 | * Little Endian. |
| 184 | * |
| 185 | * SCTLR.V: Set to zero to select the normal exception vectors |
| 186 | * with base address held in VBAR. |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 187 | * ------------------------------------------------------------- |
| 188 | */ |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 189 | ldr r0, =(SCTLR_RESET_VAL & ~(SCTLR_TE_BIT | SCTLR_EE_BIT | SCTLR_V_BIT)) |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 190 | stcopr r0, SCTLR |
| 191 | isb |
David Cunado | fee8653 | 2017-04-13 22:38:29 +0100 | [diff] [blame] | 192 | .endif /* _init_sctlr */ |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 193 | |
| 194 | /* Switch to monitor mode */ |
| 195 | cps #MODE32_mon |
| 196 | isb |
| 197 | |
| 198 | .if \_warm_boot_mailbox |
| 199 | /* ------------------------------------------------------------- |
| 200 | * This code will be executed for both warm and cold resets. |
| 201 | * Now is the time to distinguish between the two. |
| 202 | * Query the platform entrypoint address and if it is not zero |
| 203 | * then it means it is a warm boot so jump to this address. |
| 204 | * ------------------------------------------------------------- |
| 205 | */ |
| 206 | bl plat_get_my_entrypoint |
| 207 | cmp r0, #0 |
| 208 | bxne r0 |
| 209 | .endif /* _warm_boot_mailbox */ |
| 210 | |
| 211 | /* --------------------------------------------------------------------- |
| 212 | * It is a cold boot. |
| 213 | * Perform any processor specific actions upon reset e.g. cache, TLB |
| 214 | * invalidations etc. |
| 215 | * --------------------------------------------------------------------- |
| 216 | */ |
| 217 | bl reset_handler |
| 218 | |
| 219 | el3_arch_init_common \_exception_vectors |
| 220 | |
| 221 | .if \_secondary_cold_boot |
| 222 | /* ------------------------------------------------------------- |
| 223 | * Check if this is a primary or secondary CPU cold boot. |
| 224 | * The primary CPU will set up the platform while the |
| 225 | * secondaries are placed in a platform-specific state until the |
| 226 | * primary CPU performs the necessary actions to bring them out |
| 227 | * of that state and allows entry into the OS. |
| 228 | * ------------------------------------------------------------- |
| 229 | */ |
| 230 | bl plat_is_my_cpu_primary |
| 231 | cmp r0, #0 |
| 232 | bne do_primary_cold_boot |
| 233 | |
| 234 | /* This is a cold boot on a secondary CPU */ |
| 235 | bl plat_secondary_cold_boot_setup |
| 236 | /* plat_secondary_cold_boot_setup() is not supposed to return */ |
Jeenu Viswambharan | 68aef10 | 2016-11-30 15:21:11 +0000 | [diff] [blame] | 237 | no_ret plat_panic_handler |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 238 | |
| 239 | do_primary_cold_boot: |
| 240 | .endif /* _secondary_cold_boot */ |
| 241 | |
| 242 | /* --------------------------------------------------------------------- |
| 243 | * Initialize memory now. Secondary CPU initialization won't get to this |
| 244 | * point. |
| 245 | * --------------------------------------------------------------------- |
| 246 | */ |
| 247 | |
| 248 | .if \_init_memory |
| 249 | bl platform_mem_init |
| 250 | .endif /* _init_memory */ |
| 251 | |
| 252 | /* --------------------------------------------------------------------- |
| 253 | * Init C runtime environment: |
| 254 | * - Zero-initialise the NOBITS sections. There are 2 of them: |
| 255 | * - the .bss section; |
| 256 | * - the coherent memory section (if any). |
| 257 | * - Relocate the data section from ROM to RAM, if required. |
| 258 | * --------------------------------------------------------------------- |
| 259 | */ |
| 260 | .if \_init_c_runtime |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 261 | #ifdef IMAGE_BL32 |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 262 | /* ----------------------------------------------------------------- |
| 263 | * Invalidate the RW memory used by the BL32 (SP_MIN) image. This |
| 264 | * includes the data and NOBITS sections. This is done to |
| 265 | * safeguard against possible corruption of this memory by |
| 266 | * dirty cache lines in a system cache as a result of use by |
| 267 | * an earlier boot loader stage. |
| 268 | * ----------------------------------------------------------------- |
| 269 | */ |
| 270 | ldr r0, =__RW_START__ |
| 271 | ldr r1, =__RW_END__ |
| 272 | sub r1, r1, r0 |
| 273 | bl inv_dcache_range |
| 274 | #endif /* IMAGE_BL32 */ |
| 275 | |
| 276 | ldr r0, =__BSS_START__ |
| 277 | ldr r1, =__BSS_SIZE__ |
| 278 | bl zeromem |
| 279 | |
| 280 | #if USE_COHERENT_MEM |
| 281 | ldr r0, =__COHERENT_RAM_START__ |
| 282 | ldr r1, =__COHERENT_RAM_UNALIGNED_SIZE__ |
| 283 | bl zeromem |
| 284 | #endif |
| 285 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 286 | #ifdef IMAGE_BL1 |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 287 | /* ----------------------------------------------------- |
| 288 | * Copy data from ROM to RAM. |
| 289 | * ----------------------------------------------------- |
| 290 | */ |
| 291 | ldr r0, =__DATA_RAM_START__ |
| 292 | ldr r1, =__DATA_ROM_START__ |
| 293 | ldr r2, =__DATA_SIZE__ |
Yatharth Kochar | c44c5af | 2016-09-28 11:00:05 +0100 | [diff] [blame] | 294 | bl memcpy4 |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 295 | #endif |
| 296 | .endif /* _init_c_runtime */ |
| 297 | |
| 298 | /* --------------------------------------------------------------------- |
| 299 | * Allocate a stack whose memory will be marked as Normal-IS-WBWA when |
| 300 | * the MMU is enabled. There is no risk of reading stale stack memory |
| 301 | * after enabling the MMU as only the primary CPU is running at the |
| 302 | * moment. |
| 303 | * --------------------------------------------------------------------- |
| 304 | */ |
| 305 | bl plat_set_my_stack |
Douglas Raillard | 306593d | 2017-02-24 18:14:15 +0000 | [diff] [blame] | 306 | |
| 307 | #if STACK_PROTECTOR_ENABLED |
| 308 | .if \_init_c_runtime |
| 309 | bl update_stack_protector_canary |
| 310 | .endif /* _init_c_runtime */ |
| 311 | #endif |
Yatharth Kochar | f528faf | 2016-06-28 16:58:26 +0100 | [diff] [blame] | 312 | .endm |
| 313 | |
| 314 | #endif /* __EL3_COMMON_MACROS_S__ */ |