David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2013 |
| 3 | * David Feng <fenghua@phytium.com.cn> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <asm-offsets.h> |
| 9 | #include <config.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 10 | #include <linux/linkage.h> |
| 11 | #include <asm/macro.h> |
| 12 | #include <asm/armv8/mmu.h> |
| 13 | |
| 14 | /************************************************************************* |
| 15 | * |
| 16 | * Startup Code (reset vector) |
| 17 | * |
| 18 | *************************************************************************/ |
| 19 | |
| 20 | .globl _start |
| 21 | _start: |
Andre Przywara | 48321ba | 2016-05-31 10:45:06 -0700 | [diff] [blame] | 22 | #ifdef CONFIG_ENABLE_ARM_SOC_BOOT0_HOOK |
| 23 | /* |
| 24 | * Various SoCs need something special and SoC-specific up front in |
| 25 | * order to boot, allow them to set that in their boot0.h file and then |
| 26 | * use it here. |
| 27 | */ |
| 28 | #include <asm/arch/boot0.h> |
Andre Przywara | 313a578 | 2017-01-02 11:48:33 +0000 | [diff] [blame] | 29 | #else |
| 30 | b reset |
Andre Przywara | 48321ba | 2016-05-31 10:45:06 -0700 | [diff] [blame] | 31 | #endif |
| 32 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 33 | .align 3 |
| 34 | |
| 35 | .globl _TEXT_BASE |
| 36 | _TEXT_BASE: |
| 37 | .quad CONFIG_SYS_TEXT_BASE |
| 38 | |
| 39 | /* |
| 40 | * These are defined in the linker script. |
| 41 | */ |
| 42 | .globl _end_ofs |
| 43 | _end_ofs: |
| 44 | .quad _end - _start |
| 45 | |
| 46 | .globl _bss_start_ofs |
| 47 | _bss_start_ofs: |
| 48 | .quad __bss_start - _start |
| 49 | |
| 50 | .globl _bss_end_ofs |
| 51 | _bss_end_ofs: |
| 52 | .quad __bss_end - _start |
| 53 | |
| 54 | reset: |
Stephen Warren | 100a479 | 2016-07-18 17:01:50 -0600 | [diff] [blame] | 55 | /* Allow the board to save important registers */ |
| 56 | b save_boot_params |
| 57 | .globl save_boot_params_ret |
| 58 | save_boot_params_ret: |
| 59 | |
Stephen Warren | 81c2137 | 2017-11-02 18:11:27 -0600 | [diff] [blame] | 60 | #if CONFIG_POSITION_INDEPENDENT |
| 61 | /* |
| 62 | * Fix .rela.dyn relocations. This allows U-Boot to be loaded to and |
| 63 | * executed at a different address than it was linked at. |
| 64 | */ |
| 65 | pie_fixup: |
| 66 | adr x0, _start /* x0 <- Runtime value of _start */ |
| 67 | ldr x1, _TEXT_BASE /* x1 <- Linked value of _start */ |
| 68 | sub x9, x0, x1 /* x9 <- Run-vs-link offset */ |
| 69 | adr x2, __rel_dyn_start /* x2 <- Runtime &__rel_dyn_start */ |
| 70 | adr x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */ |
| 71 | pie_fix_loop: |
| 72 | ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */ |
| 73 | ldr x4, [x2], #8 /* x4 <- addend */ |
| 74 | cmp w1, #1027 /* relative fixup? */ |
| 75 | bne pie_skip_reloc |
| 76 | /* relative fix: store addend plus offset at dest location */ |
| 77 | add x0, x0, x9 |
| 78 | add x4, x4, x9 |
| 79 | str x4, [x0] |
| 80 | pie_skip_reloc: |
| 81 | cmp x2, x3 |
| 82 | b.lo pie_fix_loop |
| 83 | pie_fixup_done: |
| 84 | #endif |
| 85 | |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_SYS_RESET_SCTRL |
| 87 | bl reset_sctrl |
| 88 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 89 | /* |
| 90 | * Could be EL3/EL2/EL1, Initial State: |
| 91 | * Little Endian, MMU Disabled, i/dCache Disabled |
| 92 | */ |
| 93 | adr x0, vectors |
| 94 | switch_el x1, 3f, 2f, 1f |
David Feng | 7c5eca7 | 2014-04-19 09:45:21 +0800 | [diff] [blame] | 95 | 3: msr vbar_el3, x0 |
| 96 | mrs x0, scr_el3 |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 97 | orr x0, x0, #0xf /* SCR_EL3.NS|IRQ|FIQ|EA */ |
| 98 | msr scr_el3, x0 |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 99 | msr cptr_el3, xzr /* Enable FP/SIMD */ |
Thierry Reding | 2565792 | 2015-08-20 11:42:18 +0200 | [diff] [blame] | 100 | #ifdef COUNTER_FREQUENCY |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 101 | ldr x0, =COUNTER_FREQUENCY |
| 102 | msr cntfrq_el0, x0 /* Initialize CNTFRQ */ |
Thierry Reding | 2565792 | 2015-08-20 11:42:18 +0200 | [diff] [blame] | 103 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 104 | b 0f |
| 105 | 2: msr vbar_el2, x0 |
| 106 | mov x0, #0x33ff |
| 107 | msr cptr_el2, x0 /* Enable FP/SIMD */ |
| 108 | b 0f |
| 109 | 1: msr vbar_el1, x0 |
| 110 | mov x0, #3 << 20 |
| 111 | msr cpacr_el1, x0 /* Enable FP/SIMD */ |
| 112 | 0: |
| 113 | |
Mingkai Hu | 553d405 | 2017-01-06 17:41:10 +0800 | [diff] [blame] | 114 | /* |
Dinh Nguyen | c3e970a | 2017-04-26 23:36:03 -0500 | [diff] [blame] | 115 | * Enable SMPEN bit for coherency. |
Mingkai Hu | 553d405 | 2017-01-06 17:41:10 +0800 | [diff] [blame] | 116 | * This register is not architectural but at the moment |
| 117 | * this bit should be set for A53/A57/A72. |
| 118 | */ |
| 119 | #ifdef CONFIG_ARMV8_SET_SMPEN |
York Sun | e6b871e | 2017-05-15 08:51:59 -0700 | [diff] [blame] | 120 | switch_el x1, 3f, 1f, 1f |
| 121 | 3: |
Dinh Nguyen | c3e970a | 2017-04-26 23:36:03 -0500 | [diff] [blame] | 122 | mrs x0, S3_1_c15_c2_1 /* cpuectlr_el1 */ |
Mingkai Hu | 553d405 | 2017-01-06 17:41:10 +0800 | [diff] [blame] | 123 | orr x0, x0, #0x40 |
| 124 | msr S3_1_c15_c2_1, x0 |
York Sun | e6b871e | 2017-05-15 08:51:59 -0700 | [diff] [blame] | 125 | 1: |
Mingkai Hu | 553d405 | 2017-01-06 17:41:10 +0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 128 | /* Apply ARM core specific erratas */ |
| 129 | bl apply_core_errata |
| 130 | |
York Sun | ef04201 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 131 | /* |
| 132 | * Cache/BPB/TLB Invalidate |
| 133 | * i-cache is invalidated before enabled in icache_enable() |
| 134 | * tlb is invalidated before mmu is enabled in dcache_enable() |
| 135 | * d-cache is invalidated before enabled in dcache_enable() |
| 136 | */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 137 | |
| 138 | /* Processor specific initialization */ |
| 139 | bl lowlevel_init |
| 140 | |
Oded Gabbay | 97a8d65 | 2016-12-27 11:19:43 +0200 | [diff] [blame] | 141 | #if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_BUILD) |
Masahiro Yamada | 2663cd6 | 2016-06-27 19:31:05 +0900 | [diff] [blame] | 142 | branch_if_master x0, x1, master_cpu |
| 143 | b spin_table_secondary_jump |
| 144 | /* never return */ |
| 145 | #elif defined(CONFIG_ARMV8_MULTIENTRY) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 146 | branch_if_master x0, x1, master_cpu |
| 147 | |
| 148 | /* |
| 149 | * Slave CPUs |
| 150 | */ |
| 151 | slave_cpu: |
| 152 | wfe |
| 153 | ldr x1, =CPU_RELEASE_ADDR |
| 154 | ldr x0, [x1] |
| 155 | cbz x0, slave_cpu |
| 156 | br x0 /* branch to the given address */ |
Linus Walleij | 7477139 | 2015-03-09 10:53:21 +0100 | [diff] [blame] | 157 | #endif /* CONFIG_ARMV8_MULTIENTRY */ |
Masahiro Yamada | 2663cd6 | 2016-06-27 19:31:05 +0900 | [diff] [blame] | 158 | master_cpu: |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 159 | bl _main |
| 160 | |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 161 | #ifdef CONFIG_SYS_RESET_SCTRL |
| 162 | reset_sctrl: |
| 163 | switch_el x1, 3f, 2f, 1f |
| 164 | 3: |
| 165 | mrs x0, sctlr_el3 |
| 166 | b 0f |
| 167 | 2: |
| 168 | mrs x0, sctlr_el2 |
| 169 | b 0f |
| 170 | 1: |
| 171 | mrs x0, sctlr_el1 |
| 172 | |
| 173 | 0: |
| 174 | ldr x1, =0xfdfffffa |
| 175 | and x0, x0, x1 |
| 176 | |
| 177 | switch_el x1, 6f, 5f, 4f |
| 178 | 6: |
| 179 | msr sctlr_el3, x0 |
| 180 | b 7f |
| 181 | 5: |
| 182 | msr sctlr_el2, x0 |
| 183 | b 7f |
| 184 | 4: |
| 185 | msr sctlr_el1, x0 |
| 186 | |
| 187 | 7: |
| 188 | dsb sy |
| 189 | isb |
| 190 | b __asm_invalidate_tlb_all |
| 191 | ret |
| 192 | #endif |
| 193 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 194 | /*-----------------------------------------------------------------------*/ |
| 195 | |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 196 | WEAK(apply_core_errata) |
| 197 | |
| 198 | mov x29, lr /* Save LR */ |
| 199 | /* For now, we support Cortex-A57 specific errata only */ |
| 200 | |
| 201 | /* Check if we are running on a Cortex-A57 core */ |
| 202 | branch_if_a57_core x0, apply_a57_core_errata |
| 203 | 0: |
| 204 | mov lr, x29 /* Restore LR */ |
| 205 | ret |
| 206 | |
| 207 | apply_a57_core_errata: |
| 208 | |
| 209 | #ifdef CONFIG_ARM_ERRATA_828024 |
| 210 | mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ |
| 211 | /* Disable non-allocate hint of w-b-n-a memory type */ |
Bhupesh Sharma | 06a0f1d | 2015-05-28 14:54:13 +0530 | [diff] [blame] | 212 | orr x0, x0, #1 << 49 |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 213 | /* Disable write streaming no L1-allocate threshold */ |
Bhupesh Sharma | 06a0f1d | 2015-05-28 14:54:13 +0530 | [diff] [blame] | 214 | orr x0, x0, #3 << 25 |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 215 | /* Disable write streaming no-allocate threshold */ |
Bhupesh Sharma | 06a0f1d | 2015-05-28 14:54:13 +0530 | [diff] [blame] | 216 | orr x0, x0, #3 << 27 |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 217 | msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ |
| 218 | #endif |
| 219 | |
| 220 | #ifdef CONFIG_ARM_ERRATA_826974 |
| 221 | mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ |
| 222 | /* Disable speculative load execution ahead of a DMB */ |
Bhupesh Sharma | 06a0f1d | 2015-05-28 14:54:13 +0530 | [diff] [blame] | 223 | orr x0, x0, #1 << 59 |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 224 | msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ |
| 225 | #endif |
| 226 | |
Ashish kumar | 9c6d33c | 2016-01-27 18:09:32 +0530 | [diff] [blame] | 227 | #ifdef CONFIG_ARM_ERRATA_833471 |
| 228 | mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ |
| 229 | /* FPSCR write flush. |
| 230 | * Note that in some cases where a flush is unnecessary this |
| 231 | could impact performance. */ |
| 232 | orr x0, x0, #1 << 38 |
| 233 | msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ |
| 234 | #endif |
| 235 | |
| 236 | #ifdef CONFIG_ARM_ERRATA_829520 |
| 237 | mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ |
| 238 | /* Disable Indirect Predictor bit will prevent this erratum |
| 239 | from occurring |
| 240 | * Note that in some cases where a flush is unnecessary this |
| 241 | could impact performance. */ |
| 242 | orr x0, x0, #1 << 4 |
| 243 | msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ |
| 244 | #endif |
| 245 | |
Bhupesh Sharma | 80a7e35 | 2015-01-23 15:50:04 +0530 | [diff] [blame] | 246 | #ifdef CONFIG_ARM_ERRATA_833069 |
| 247 | mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ |
| 248 | /* Disable Enable Invalidates of BTB bit */ |
| 249 | and x0, x0, #0xE |
| 250 | msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ |
| 251 | #endif |
| 252 | b 0b |
| 253 | ENDPROC(apply_core_errata) |
| 254 | |
| 255 | /*-----------------------------------------------------------------------*/ |
| 256 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 257 | WEAK(lowlevel_init) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 258 | mov x29, lr /* Save LR */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 259 | |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 260 | #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3) |
| 261 | branch_if_slave x0, 1f |
| 262 | ldr x0, =GICD_BASE |
| 263 | bl gic_init_secure |
| 264 | 1: |
| 265 | #if defined(CONFIG_GICV3) |
| 266 | ldr x0, =GICR_BASE |
| 267 | bl gic_init_secure_percpu |
| 268 | #elif defined(CONFIG_GICV2) |
| 269 | ldr x0, =GICD_BASE |
| 270 | ldr x1, =GICC_BASE |
| 271 | bl gic_init_secure_percpu |
| 272 | #endif |
Stephen Warren | 73f47af | 2016-04-28 12:45:44 -0600 | [diff] [blame] | 273 | #endif |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 274 | |
Masahiro Yamada | e4ce25f | 2016-05-20 12:13:10 +0900 | [diff] [blame] | 275 | #ifdef CONFIG_ARMV8_MULTIENTRY |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 276 | branch_if_master x0, x1, 2f |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Slave should wait for master clearing spin table. |
| 280 | * This sync prevent salves observing incorrect |
| 281 | * value of spin table and jumping to wrong place. |
| 282 | */ |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 283 | #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3) |
| 284 | #ifdef CONFIG_GICV2 |
| 285 | ldr x0, =GICC_BASE |
| 286 | #endif |
| 287 | bl gic_wait_for_interrupt |
| 288 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 289 | |
| 290 | /* |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 291 | * All slaves will enter EL2 and optionally EL1. |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 292 | */ |
Alison Wang | eb2088d | 2017-01-17 09:39:17 +0800 | [diff] [blame] | 293 | adr x4, lowlevel_in_el2 |
| 294 | ldr x5, =ES_TO_AARCH64 |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 295 | bl armv8_switch_to_el2 |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 296 | |
| 297 | lowlevel_in_el2: |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 298 | #ifdef CONFIG_ARMV8_SWITCH_TO_EL1 |
Alison Wang | eb2088d | 2017-01-17 09:39:17 +0800 | [diff] [blame] | 299 | adr x4, lowlevel_in_el1 |
| 300 | ldr x5, =ES_TO_AARCH64 |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 301 | bl armv8_switch_to_el1 |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 302 | |
| 303 | lowlevel_in_el1: |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 304 | #endif |
| 305 | |
Linus Walleij | 7477139 | 2015-03-09 10:53:21 +0100 | [diff] [blame] | 306 | #endif /* CONFIG_ARMV8_MULTIENTRY */ |
| 307 | |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 308 | 2: |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 309 | mov lr, x29 /* Restore LR */ |
| 310 | ret |
| 311 | ENDPROC(lowlevel_init) |
| 312 | |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 313 | WEAK(smp_kick_all_cpus) |
| 314 | /* Kick secondary cpus up by SGI 0 interrupt */ |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 315 | #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3) |
| 316 | ldr x0, =GICD_BASE |
Masahiro Yamada | b914009 | 2016-06-17 18:32:47 +0900 | [diff] [blame] | 317 | b gic_kick_secondary_cpus |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 318 | #endif |
David Feng | 79bbde0 | 2014-03-14 14:26:27 +0800 | [diff] [blame] | 319 | ret |
| 320 | ENDPROC(smp_kick_all_cpus) |
| 321 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 322 | /*-----------------------------------------------------------------------*/ |
| 323 | |
| 324 | ENTRY(c_runtime_cpu_setup) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 325 | /* Relocate vBAR */ |
| 326 | adr x0, vectors |
| 327 | switch_el x1, 3f, 2f, 1f |
| 328 | 3: msr vbar_el3, x0 |
| 329 | b 0f |
| 330 | 2: msr vbar_el2, x0 |
| 331 | b 0f |
| 332 | 1: msr vbar_el1, x0 |
| 333 | 0: |
| 334 | |
| 335 | ret |
| 336 | ENDPROC(c_runtime_cpu_setup) |
Stephen Warren | 100a479 | 2016-07-18 17:01:50 -0600 | [diff] [blame] | 337 | |
| 338 | WEAK(save_boot_params) |
| 339 | b save_boot_params_ret /* back to my caller */ |
| 340 | ENDPROC(save_boot_params) |