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> 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: 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> 29#else 30 b reset 31#endif 32 33#if !CONFIG_IS_ENABLED(TINY_FRAMEWORK) 34 .align 3 35 36.globl _TEXT_BASE 37_TEXT_BASE: 38 .quad CONFIG_SYS_TEXT_BASE 39 40/* 41 * These are defined in the linker script. 42 */ 43.globl _end_ofs 44_end_ofs: 45 .quad _end - _start 46 47.globl _bss_start_ofs 48_bss_start_ofs: 49 .quad __bss_start - _start 50 51.globl _bss_end_ofs 52_bss_end_ofs: 53 .quad __bss_end - _start 54 55reset: 56 /* Allow the board to save important registers */ 57 b save_boot_params 58.globl save_boot_params_ret 59save_boot_params_ret: 60 61#if CONFIG_POSITION_INDEPENDENT 62 /* 63 * Fix .rela.dyn relocations. This allows U-Boot to be loaded to and 64 * executed at a different address than it was linked at. 65 */ 66pie_fixup: 67 adr x0, _start /* x0 <- Runtime value of _start */ 68 ldr x1, _TEXT_BASE /* x1 <- Linked value of _start */ 69 sub x9, x0, x1 /* x9 <- Run-vs-link offset */ 70 adr x2, __rel_dyn_start /* x2 <- Runtime &__rel_dyn_start */ 71 adr x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */ 72pie_fix_loop: 73 ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */ 74 ldr x4, [x2], #8 /* x4 <- addend */ 75 cmp w1, #1027 /* relative fixup? */ 76 bne pie_skip_reloc 77 /* relative fix: store addend plus offset at dest location */ 78 add x0, x0, x9 79 add x4, x4, x9 80 str x4, [x0] 81pie_skip_reloc: 82 cmp x2, x3 83 b.lo pie_fix_loop 84pie_fixup_done: 85#endif 86 87#ifdef CONFIG_SYS_RESET_SCTRL 88 bl reset_sctrl 89#endif 90 /* 91 * Could be EL3/EL2/EL1, Initial State: 92 * Little Endian, MMU Disabled, i/dCache Disabled 93 */ 94 adr x0, vectors 95 switch_el x1, 3f, 2f, 1f 963: msr vbar_el3, x0 97 mrs x0, scr_el3 98 orr x0, x0, #0xf /* SCR_EL3.NS|IRQ|FIQ|EA */ 99 msr scr_el3, x0 100 msr cptr_el3, xzr /* Enable FP/SIMD */ 101#ifdef COUNTER_FREQUENCY 102 ldr x0, =COUNTER_FREQUENCY 103 msr cntfrq_el0, x0 /* Initialize CNTFRQ */ 104#endif 105 b 0f 1062: msr vbar_el2, x0 107 mov x0, #0x33ff 108 msr cptr_el2, x0 /* Enable FP/SIMD */ 109 b 0f 1101: msr vbar_el1, x0 111 mov x0, #3 << 20 112 msr cpacr_el1, x0 /* Enable FP/SIMD */ 1130: 114 115 /* 116 * Enable SMPEN bit for coherency. 117 * This register is not architectural but at the moment 118 * this bit should be set for A53/A57/A72. 119 */ 120#ifdef CONFIG_ARMV8_SET_SMPEN 121 switch_el x1, 3f, 1f, 1f 1223: 123 mrs x0, S3_1_c15_c2_1 /* cpuectlr_el1 */ 124 orr x0, x0, #0x40 125 msr S3_1_c15_c2_1, x0 1261: 127#endif 128 129 /* Apply ARM core specific erratas */ 130 bl apply_core_errata 131 132 /* 133 * Cache/BPB/TLB Invalidate 134 * i-cache is invalidated before enabled in icache_enable() 135 * tlb is invalidated before mmu is enabled in dcache_enable() 136 * d-cache is invalidated before enabled in dcache_enable() 137 */ 138 139 /* Processor specific initialization */ 140 bl lowlevel_init 141 142#if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_BUILD) 143 branch_if_master x0, x1, master_cpu 144 b spin_table_secondary_jump 145 /* never return */ 146#elif defined(CONFIG_ARMV8_MULTIENTRY) 147 branch_if_master x0, x1, master_cpu 148 149 /* 150 * Slave CPUs 151 */ 152slave_cpu: 153 wfe 154 ldr x1, =CPU_RELEASE_ADDR 155 ldr x0, [x1] 156 cbz x0, slave_cpu 157 br x0 /* branch to the given address */ 158#endif /* CONFIG_ARMV8_MULTIENTRY */ 159master_cpu: 160 bl _main 161 162#ifdef CONFIG_SYS_RESET_SCTRL 163reset_sctrl: 164 switch_el x1, 3f, 2f, 1f 1653: 166 mrs x0, sctlr_el3 167 b 0f 1682: 169 mrs x0, sctlr_el2 170 b 0f 1711: 172 mrs x0, sctlr_el1 173 1740: 175 ldr x1, =0xfdfffffa 176 and x0, x0, x1 177 178 switch_el x1, 6f, 5f, 4f 1796: 180 msr sctlr_el3, x0 181 b 7f 1825: 183 msr sctlr_el2, x0 184 b 7f 1854: 186 msr sctlr_el1, x0 187 1887: 189 dsb sy 190 isb 191 b __asm_invalidate_tlb_all 192 ret 193#endif 194 195/*-----------------------------------------------------------------------*/ 196 197WEAK(apply_core_errata) 198 199 mov x29, lr /* Save LR */ 200 /* For now, we support Cortex-A57 specific errata only */ 201 202 /* Check if we are running on a Cortex-A57 core */ 203 branch_if_a57_core x0, apply_a57_core_errata 2040: 205 mov lr, x29 /* Restore LR */ 206 ret 207 208apply_a57_core_errata: 209 210#ifdef CONFIG_ARM_ERRATA_828024 211 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ 212 /* Disable non-allocate hint of w-b-n-a memory type */ 213 orr x0, x0, #1 << 49 214 /* Disable write streaming no L1-allocate threshold */ 215 orr x0, x0, #3 << 25 216 /* Disable write streaming no-allocate threshold */ 217 orr x0, x0, #3 << 27 218 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ 219#endif 220 221#ifdef CONFIG_ARM_ERRATA_826974 222 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ 223 /* Disable speculative load execution ahead of a DMB */ 224 orr x0, x0, #1 << 59 225 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ 226#endif 227 228#ifdef CONFIG_ARM_ERRATA_833471 229 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ 230 /* FPSCR write flush. 231 * Note that in some cases where a flush is unnecessary this 232 could impact performance. */ 233 orr x0, x0, #1 << 38 234 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ 235#endif 236 237#ifdef CONFIG_ARM_ERRATA_829520 238 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ 239 /* Disable Indirect Predictor bit will prevent this erratum 240 from occurring 241 * Note that in some cases where a flush is unnecessary this 242 could impact performance. */ 243 orr x0, x0, #1 << 4 244 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ 245#endif 246 247#ifdef CONFIG_ARM_ERRATA_833069 248 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */ 249 /* Disable Enable Invalidates of BTB bit */ 250 and x0, x0, #0xE 251 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */ 252#endif 253 b 0b 254ENDPROC(apply_core_errata) 255 256/*-----------------------------------------------------------------------*/ 257 258WEAK(lowlevel_init) 259 mov x29, lr /* Save LR */ 260 261#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_IRQ) 262 branch_if_slave x0, 1f 263 ldr x0, =GICD_BASE 264 bl gic_init_secure 2651: 266#if defined(CONFIG_GICV3) 267 ldr x0, =GICR_BASE 268 bl gic_init_secure_percpu 269#elif defined(CONFIG_GICV2) 270 ldr x0, =GICD_BASE 271 ldr x1, =GICC_BASE 272 bl gic_init_secure_percpu 273#endif 274#endif 275 276#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_IRQ) 277 /* 278 * Setting HCR_EL2.TGE AMO IMO FMO for exception rounting to EL2 279 */ 280 mrs x0, CurrentEL /* check currentEL */ 281 cmp x0, 0x8 282 b.ne end /* currentEL != EL2 */ 283 284 mrs x9, hcr_el2 285 orr x9, x9, #(7 << 3) /* HCR_EL2.AMO IMO FMO set */ 286 orr x9, x9, #(1 << 27) /* HCR_EL2.TGE set */ 287 msr hcr_el2, x9 288 289end: 290 nop 291#endif /* CONFIG_IRQ */ 292 293#ifdef CONFIG_ARMV8_MULTIENTRY 294 branch_if_master x0, x1, 2f 295 296 /* 297 * Slave should wait for master clearing spin table. 298 * This sync prevent salves observing incorrect 299 * value of spin table and jumping to wrong place. 300 */ 301#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3) 302#ifdef CONFIG_GICV2 303 ldr x0, =GICC_BASE 304#endif 305 bl gic_wait_for_interrupt 306#endif 307 308 /* 309 * All slaves will enter EL2 and optionally EL1. 310 */ 311 adr x4, lowlevel_in_el2 312 ldr x5, =ES_TO_AARCH64 313 bl armv8_switch_to_el2 314 315lowlevel_in_el2: 316#ifdef CONFIG_ARMV8_SWITCH_TO_EL1 317 adr x4, lowlevel_in_el1 318 ldr x5, =ES_TO_AARCH64 319 bl armv8_switch_to_el1 320 321lowlevel_in_el1: 322#endif 323 324#endif /* CONFIG_ARMV8_MULTIENTRY */ 325 3262: 327 mov lr, x29 /* Restore LR */ 328 ret 329ENDPROC(lowlevel_init) 330 331WEAK(smp_kick_all_cpus) 332 /* Kick secondary cpus up by SGI 0 interrupt */ 333#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3) 334 ldr x0, =GICD_BASE 335 b gic_kick_secondary_cpus 336#endif 337 ret 338ENDPROC(smp_kick_all_cpus) 339 340/*-----------------------------------------------------------------------*/ 341 342ENTRY(c_runtime_cpu_setup) 343 /* Relocate vBAR */ 344 adr x0, vectors 345 switch_el x1, 3f, 2f, 1f 3463: msr vbar_el3, x0 347 b 0f 3482: msr vbar_el2, x0 349 b 0f 3501: msr vbar_el1, x0 3510: 352 353 ret 354ENDPROC(c_runtime_cpu_setup) 355 356WEAK(save_boot_params) 357 b save_boot_params_ret /* back to my caller */ 358ENDPROC(save_boot_params) 359#endif 360