start.S 8.1 KB

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