Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 1 | /* |
Douglas Raillard | 21362a9 | 2016-12-02 13:51:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +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 | |
| 13 | /* |
| 14 | * Helper macro to initialise EL3 registers we care about. |
| 15 | */ |
| 16 | .macro el3_arch_init_common _exception_vectors |
| 17 | /* --------------------------------------------------------------------- |
| 18 | * Enable the instruction cache, stack pointer and data access alignment |
| 19 | * checks |
| 20 | * --------------------------------------------------------------------- |
| 21 | */ |
| 22 | mov x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT) |
| 23 | mrs x0, sctlr_el3 |
| 24 | orr x0, x0, x1 |
| 25 | msr sctlr_el3, x0 |
| 26 | isb |
| 27 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 28 | #ifdef IMAGE_BL31 |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 29 | /* --------------------------------------------------------------------- |
| 30 | * Initialise the per-cpu cache pointer to the CPU. |
| 31 | * This is done early to enable crash reporting to have access to crash |
| 32 | * stack. Since crash reporting depends on cpu_data to report the |
| 33 | * unhandled exception, not doing so can lead to recursive exceptions |
| 34 | * due to a NULL TPIDR_EL3. |
| 35 | * --------------------------------------------------------------------- |
| 36 | */ |
| 37 | bl init_cpu_data_ptr |
| 38 | #endif /* IMAGE_BL31 */ |
| 39 | |
| 40 | /* --------------------------------------------------------------------- |
| 41 | * Set the exception vectors. |
| 42 | * --------------------------------------------------------------------- |
| 43 | */ |
| 44 | adr x0, \_exception_vectors |
| 45 | msr vbar_el3, x0 |
| 46 | isb |
| 47 | |
| 48 | /* --------------------------------------------------------------------- |
Soby Mathew | 074e05a | 2016-04-04 12:34:24 +0100 | [diff] [blame] | 49 | * Early set RES1 bits in SCR_EL3. Set EA bit to catch both |
| 50 | * External Aborts and SError Interrupts in EL3 and also the SIF bit |
| 51 | * to disable instruction fetches from Non-secure memory. |
Gerald Lejeune | 632d6df | 2016-03-22 09:29:23 +0100 | [diff] [blame] | 52 | * --------------------------------------------------------------------- |
| 53 | */ |
Soby Mathew | 074e05a | 2016-04-04 12:34:24 +0100 | [diff] [blame] | 54 | mov x0, #(SCR_RES1_BITS | SCR_EA_BIT | SCR_SIF_BIT) |
Gerald Lejeune | 632d6df | 2016-03-22 09:29:23 +0100 | [diff] [blame] | 55 | msr scr_el3, x0 |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 56 | |
| 57 | /* --------------------------------------------------------------------- |
dp-arm | 595d0d5 | 2017-02-08 11:51:50 +0000 | [diff] [blame] | 58 | * Disable secure self-hosted invasive debug. |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 59 | * --------------------------------------------------------------------- |
| 60 | */ |
dp-arm | 595d0d5 | 2017-02-08 11:51:50 +0000 | [diff] [blame] | 61 | mov_imm x0, MDCR_DEF_VAL |
| 62 | msr mdcr_el3, x0 |
David Cunado | 5f55e28 | 2016-10-31 17:37:34 +0000 | [diff] [blame] | 63 | |
Gerald Lejeune | 632d6df | 2016-03-22 09:29:23 +0100 | [diff] [blame] | 64 | /* --------------------------------------------------------------------- |
| 65 | * Enable External Aborts and SError Interrupts now that the exception |
| 66 | * vectors have been setup. |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 67 | * --------------------------------------------------------------------- |
| 68 | */ |
| 69 | msr daifclr, #DAIF_ABT_BIT |
| 70 | |
| 71 | /* --------------------------------------------------------------------- |
| 72 | * The initial state of the Architectural feature trap register |
| 73 | * (CPTR_EL3) is unknown and it must be set to a known state. All |
| 74 | * feature traps are disabled. Some bits in this register are marked as |
| 75 | * reserved and should not be modified. |
| 76 | * |
| 77 | * CPTR_EL3.TCPAC: This causes a direct access to the CPACR_EL1 from EL1 |
| 78 | * or the CPTR_EL2 from EL2 to trap to EL3 unless it is trapped at EL2. |
| 79 | * |
| 80 | * CPTR_EL3.TTA: This causes access to the Trace functionality to trap |
| 81 | * to EL3 when executed from EL0, EL1, EL2, or EL3. If system register |
| 82 | * access to trace functionality is not supported, this bit is RES0. |
| 83 | * |
| 84 | * CPTR_EL3.TFP: This causes instructions that access the registers |
| 85 | * associated with Floating Point and Advanced SIMD execution to trap |
| 86 | * to EL3 when executed from any exception level, unless trapped to EL1 |
| 87 | * or EL2. |
| 88 | * --------------------------------------------------------------------- |
| 89 | */ |
| 90 | mrs x0, cptr_el3 |
| 91 | bic w0, w0, #TCPAC_BIT |
| 92 | bic w0, w0, #TTA_BIT |
| 93 | bic w0, w0, #TFP_BIT |
| 94 | msr cptr_el3, x0 |
| 95 | .endm |
| 96 | |
| 97 | /* ----------------------------------------------------------------------------- |
| 98 | * This is the super set of actions that need to be performed during a cold boot |
Juan Castillo | 7d19941 | 2015-12-14 09:35:25 +0000 | [diff] [blame] | 99 | * or a warm boot in EL3. This code is shared by BL1 and BL31. |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 100 | * |
| 101 | * This macro will always perform reset handling, architectural initialisations |
| 102 | * and stack setup. The rest of the actions are optional because they might not |
| 103 | * be needed, depending on the context in which this macro is called. This is |
| 104 | * why this macro is parameterised ; each parameter allows to enable/disable |
| 105 | * some actions. |
| 106 | * |
| 107 | * _set_endian: |
| 108 | * Whether the macro needs to configure the endianness of data accesses. |
| 109 | * |
| 110 | * _warm_boot_mailbox: |
| 111 | * Whether the macro needs to detect the type of boot (cold/warm). The |
| 112 | * detection is based on the platform entrypoint address : if it is zero |
| 113 | * then it is a cold boot, otherwise it is a warm boot. In the latter case, |
| 114 | * this macro jumps on the platform entrypoint address. |
| 115 | * |
| 116 | * _secondary_cold_boot: |
| 117 | * Whether the macro needs to identify the CPU that is calling it: primary |
| 118 | * CPU or secondary CPU. The primary CPU will be allowed to carry on with |
| 119 | * the platform initialisations, while the secondaries will be put in a |
| 120 | * platform-specific state in the meantime. |
| 121 | * |
| 122 | * If the caller knows this macro will only be called by the primary CPU |
| 123 | * then this parameter can be defined to 0 to skip this step. |
| 124 | * |
| 125 | * _init_memory: |
| 126 | * Whether the macro needs to initialise the memory. |
| 127 | * |
| 128 | * _init_c_runtime: |
| 129 | * Whether the macro needs to initialise the C runtime environment. |
| 130 | * |
| 131 | * _exception_vectors: |
| 132 | * Address of the exception vectors to program in the VBAR_EL3 register. |
| 133 | * ----------------------------------------------------------------------------- |
| 134 | */ |
| 135 | .macro el3_entrypoint_common \ |
| 136 | _set_endian, _warm_boot_mailbox, _secondary_cold_boot, \ |
| 137 | _init_memory, _init_c_runtime, _exception_vectors |
| 138 | |
| 139 | .if \_set_endian |
| 140 | /* ------------------------------------------------------------- |
| 141 | * Set the CPU endianness before doing anything that might |
| 142 | * involve memory reads or writes. |
| 143 | * ------------------------------------------------------------- |
| 144 | */ |
| 145 | mrs x0, sctlr_el3 |
| 146 | bic x0, x0, #SCTLR_EE_BIT |
| 147 | msr sctlr_el3, x0 |
| 148 | isb |
| 149 | .endif /* _set_endian */ |
| 150 | |
| 151 | .if \_warm_boot_mailbox |
| 152 | /* ------------------------------------------------------------- |
| 153 | * This code will be executed for both warm and cold resets. |
| 154 | * Now is the time to distinguish between the two. |
| 155 | * Query the platform entrypoint address and if it is not zero |
| 156 | * then it means it is a warm boot so jump to this address. |
| 157 | * ------------------------------------------------------------- |
| 158 | */ |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 159 | bl plat_get_my_entrypoint |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 160 | cbz x0, do_cold_boot |
| 161 | br x0 |
| 162 | |
| 163 | do_cold_boot: |
| 164 | .endif /* _warm_boot_mailbox */ |
| 165 | |
Antonio Nino Diaz | 4357b41 | 2016-02-23 12:04:58 +0000 | [diff] [blame] | 166 | /* --------------------------------------------------------------------- |
| 167 | * It is a cold boot. |
| 168 | * Perform any processor specific actions upon reset e.g. cache, TLB |
| 169 | * invalidations etc. |
| 170 | * --------------------------------------------------------------------- |
| 171 | */ |
| 172 | bl reset_handler |
| 173 | |
| 174 | el3_arch_init_common \_exception_vectors |
| 175 | |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 176 | .if \_secondary_cold_boot |
| 177 | /* ------------------------------------------------------------- |
Antonio Nino Diaz | 4357b41 | 2016-02-23 12:04:58 +0000 | [diff] [blame] | 178 | * Check if this is a primary or secondary CPU cold boot. |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 179 | * The primary CPU will set up the platform while the |
| 180 | * secondaries are placed in a platform-specific state until the |
| 181 | * primary CPU performs the necessary actions to bring them out |
| 182 | * of that state and allows entry into the OS. |
| 183 | * ------------------------------------------------------------- |
| 184 | */ |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 185 | bl plat_is_my_cpu_primary |
Soby Mathew | eb3bbf1 | 2015-06-08 12:32:50 +0100 | [diff] [blame] | 186 | cbnz w0, do_primary_cold_boot |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 187 | |
| 188 | /* This is a cold boot on a secondary CPU */ |
| 189 | bl plat_secondary_cold_boot_setup |
| 190 | /* plat_secondary_cold_boot_setup() is not supposed to return */ |
Antonio Nino Diaz | 1f21bcf | 2016-02-01 13:57:25 +0000 | [diff] [blame] | 191 | bl el3_panic |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 192 | |
| 193 | do_primary_cold_boot: |
| 194 | .endif /* _secondary_cold_boot */ |
| 195 | |
| 196 | /* --------------------------------------------------------------------- |
Antonio Nino Diaz | 4357b41 | 2016-02-23 12:04:58 +0000 | [diff] [blame] | 197 | * Initialize memory now. Secondary CPU initialization won't get to this |
| 198 | * point. |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 199 | * --------------------------------------------------------------------- |
| 200 | */ |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 201 | |
| 202 | .if \_init_memory |
| 203 | bl platform_mem_init |
| 204 | .endif /* _init_memory */ |
| 205 | |
| 206 | /* --------------------------------------------------------------------- |
| 207 | * Init C runtime environment: |
| 208 | * - Zero-initialise the NOBITS sections. There are 2 of them: |
| 209 | * - the .bss section; |
| 210 | * - the coherent memory section (if any). |
| 211 | * - Relocate the data section from ROM to RAM, if required. |
| 212 | * --------------------------------------------------------------------- |
| 213 | */ |
| 214 | .if \_init_c_runtime |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 215 | #ifdef IMAGE_BL31 |
Achin Gupta | e9c4a64 | 2015-09-11 16:03:13 +0100 | [diff] [blame] | 216 | /* ------------------------------------------------------------- |
| 217 | * Invalidate the RW memory used by the BL31 image. This |
| 218 | * includes the data and NOBITS sections. This is done to |
| 219 | * safeguard against possible corruption of this memory by |
| 220 | * dirty cache lines in a system cache as a result of use by |
| 221 | * an earlier boot loader stage. |
| 222 | * ------------------------------------------------------------- |
| 223 | */ |
| 224 | adr x0, __RW_START__ |
| 225 | adr x1, __RW_END__ |
| 226 | sub x1, x1, x0 |
| 227 | bl inv_dcache_range |
| 228 | #endif /* IMAGE_BL31 */ |
| 229 | |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 230 | ldr x0, =__BSS_START__ |
| 231 | ldr x1, =__BSS_SIZE__ |
Douglas Raillard | 21362a9 | 2016-12-02 13:51:54 +0000 | [diff] [blame] | 232 | bl zeromem |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 233 | |
| 234 | #if USE_COHERENT_MEM |
| 235 | ldr x0, =__COHERENT_RAM_START__ |
| 236 | ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ |
Douglas Raillard | 21362a9 | 2016-12-02 13:51:54 +0000 | [diff] [blame] | 237 | bl zeromem |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 238 | #endif |
| 239 | |
Masahiro Yamada | 441bfdd | 2016-12-25 23:36:24 +0900 | [diff] [blame] | 240 | #ifdef IMAGE_BL1 |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 241 | ldr x0, =__DATA_RAM_START__ |
| 242 | ldr x1, =__DATA_ROM_START__ |
| 243 | ldr x2, =__DATA_SIZE__ |
| 244 | bl memcpy16 |
| 245 | #endif |
| 246 | .endif /* _init_c_runtime */ |
| 247 | |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 248 | /* --------------------------------------------------------------------- |
| 249 | * Use SP_EL0 for the C runtime stack. |
| 250 | * --------------------------------------------------------------------- |
| 251 | */ |
| 252 | msr spsel, #0 |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 253 | |
| 254 | /* --------------------------------------------------------------------- |
| 255 | * Allocate a stack whose memory will be marked as Normal-IS-WBWA when |
| 256 | * the MMU is enabled. There is no risk of reading stale stack memory |
| 257 | * after enabling the MMU as only the primary CPU is running at the |
| 258 | * moment. |
| 259 | * --------------------------------------------------------------------- |
| 260 | */ |
Soby Mathew | 3700a92 | 2015-07-13 11:21:11 +0100 | [diff] [blame] | 261 | bl plat_set_my_stack |
Douglas Raillard | 306593d | 2017-02-24 18:14:15 +0000 | [diff] [blame] | 262 | |
| 263 | #if STACK_PROTECTOR_ENABLED |
| 264 | .if \_init_c_runtime |
| 265 | bl update_stack_protector_canary |
| 266 | .endif /* _init_c_runtime */ |
| 267 | #endif |
Sandrine Bailleux | acde8b0 | 2015-05-19 11:54:45 +0100 | [diff] [blame] | 268 | .endm |
| 269 | |
| 270 | #endif /* __EL3_COMMON_MACROS_S__ */ |