start.S 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /*
  80. * Could be EL3/EL2/EL1, Initial State:
  81. * Little Endian, MMU Disabled, i/dCache Disabled
  82. */
  83. adr x0, vectors
  84. switch_el x1, 3f, 2f, 1f
  85. 3: msr vbar_el3, x0
  86. mrs x0, scr_el3
  87. orr x0, x0, #0xf /* SCR_EL3.NS|IRQ|FIQ|EA */
  88. msr scr_el3, x0
  89. msr cptr_el3, xzr /* Enable FP/SIMD */
  90. #ifdef COUNTER_FREQUENCY
  91. ldr x0, =COUNTER_FREQUENCY
  92. msr cntfrq_el0, x0 /* Initialize CNTFRQ */
  93. #endif
  94. b 0f
  95. 2: msr vbar_el2, x0
  96. mov x0, #0x33ff
  97. msr cptr_el2, x0 /* Enable FP/SIMD */
  98. b 0f
  99. 1: msr vbar_el1, x0
  100. mov x0, #3 << 20
  101. msr cpacr_el1, x0 /* Enable FP/SIMD */
  102. 0:
  103. /*
  104. * Enable SMPEN bit for coherency.
  105. * This register is not architectural but at the moment
  106. * this bit should be set for A53/A57/A72.
  107. */
  108. #ifdef CONFIG_ARMV8_SET_SMPEN
  109. switch_el x1, 3f, 1f, 1f
  110. 3:
  111. mrs x0, S3_1_c15_c2_1 /* cpuectlr_el1 */
  112. orr x0, x0, #0x40
  113. msr S3_1_c15_c2_1, x0
  114. 1:
  115. #endif
  116. /* Apply ARM core specific erratas */
  117. bl apply_core_errata
  118. /*
  119. * Cache/BPB/TLB Invalidate
  120. * i-cache is invalidated before enabled in icache_enable()
  121. * tlb is invalidated before mmu is enabled in dcache_enable()
  122. * d-cache is invalidated before enabled in dcache_enable()
  123. */
  124. /* Processor specific initialization */
  125. bl lowlevel_init
  126. #if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_BUILD)
  127. branch_if_master x0, x1, master_cpu
  128. b spin_table_secondary_jump
  129. /* never return */
  130. #elif defined(CONFIG_ARMV8_MULTIENTRY)
  131. branch_if_master x0, x1, master_cpu
  132. /*
  133. * Slave CPUs
  134. */
  135. slave_cpu:
  136. wfe
  137. ldr x1, =CPU_RELEASE_ADDR
  138. ldr x0, [x1]
  139. cbz x0, slave_cpu
  140. br x0 /* branch to the given address */
  141. #endif /* CONFIG_ARMV8_MULTIENTRY */
  142. master_cpu:
  143. bl _main
  144. #ifdef CONFIG_SYS_RESET_SCTRL
  145. reset_sctrl:
  146. switch_el x1, 3f, 2f, 1f
  147. 3:
  148. mrs x0, sctlr_el3
  149. b 0f
  150. 2:
  151. mrs x0, sctlr_el2
  152. b 0f
  153. 1:
  154. mrs x0, sctlr_el1
  155. 0:
  156. ldr x1, =0xfdfffffa
  157. and x0, x0, x1
  158. switch_el x1, 6f, 5f, 4f
  159. 6:
  160. msr sctlr_el3, x0
  161. b 7f
  162. 5:
  163. msr sctlr_el2, x0
  164. b 7f
  165. 4:
  166. msr sctlr_el1, x0
  167. 7:
  168. dsb sy
  169. isb
  170. b __asm_invalidate_tlb_all
  171. ret
  172. #endif
  173. /*-----------------------------------------------------------------------*/
  174. WEAK(apply_core_errata)
  175. mov x29, lr /* Save LR */
  176. /* For now, we support Cortex-A53, Cortex-A57 specific errata */
  177. /* Check if we are running on a Cortex-A53 core */
  178. branch_if_a53_core x0, apply_a53_core_errata
  179. /* Check if we are running on a Cortex-A57 core */
  180. branch_if_a57_core x0, apply_a57_core_errata
  181. 0:
  182. mov lr, x29 /* Restore LR */
  183. ret
  184. apply_a53_core_errata:
  185. #ifdef CONFIG_ARM_ERRATA_855873
  186. mrs x0, midr_el1
  187. tst x0, #(0xf << 20)
  188. b.ne 0b
  189. mrs x0, midr_el1
  190. and x0, x0, #0xf
  191. cmp x0, #3
  192. b.lt 0b
  193. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  194. /* Enable data cache clean as data cache clean/invalidate */
  195. orr x0, x0, #1 << 44
  196. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  197. #endif
  198. b 0b
  199. apply_a57_core_errata:
  200. #ifdef CONFIG_ARM_ERRATA_828024
  201. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  202. /* Disable non-allocate hint of w-b-n-a memory type */
  203. orr x0, x0, #1 << 49
  204. /* Disable write streaming no L1-allocate threshold */
  205. orr x0, x0, #3 << 25
  206. /* Disable write streaming no-allocate threshold */
  207. orr x0, x0, #3 << 27
  208. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  209. #endif
  210. #ifdef CONFIG_ARM_ERRATA_826974
  211. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  212. /* Disable speculative load execution ahead of a DMB */
  213. orr x0, x0, #1 << 59
  214. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  215. #endif
  216. #ifdef CONFIG_ARM_ERRATA_833471
  217. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  218. /* FPSCR write flush.
  219. * Note that in some cases where a flush is unnecessary this
  220. could impact performance. */
  221. orr x0, x0, #1 << 38
  222. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  223. #endif
  224. #ifdef CONFIG_ARM_ERRATA_829520
  225. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  226. /* Disable Indirect Predictor bit will prevent this erratum
  227. from occurring
  228. * Note that in some cases where a flush is unnecessary this
  229. could impact performance. */
  230. orr x0, x0, #1 << 4
  231. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  232. #endif
  233. #ifdef CONFIG_ARM_ERRATA_833069
  234. mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
  235. /* Disable Enable Invalidates of BTB bit */
  236. and x0, x0, #0xE
  237. msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
  238. #endif
  239. b 0b
  240. ENDPROC(apply_core_errata)
  241. /*-----------------------------------------------------------------------*/
  242. WEAK(lowlevel_init)
  243. mov x29, lr /* Save LR */
  244. #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
  245. branch_if_slave x0, 1f
  246. ldr x0, =GICD_BASE
  247. bl gic_init_secure
  248. 1:
  249. #if defined(CONFIG_GICV3)
  250. ldr x0, =GICR_BASE
  251. bl gic_init_secure_percpu
  252. #elif defined(CONFIG_GICV2)
  253. ldr x0, =GICD_BASE
  254. ldr x1, =GICC_BASE
  255. bl gic_init_secure_percpu
  256. #endif
  257. #endif
  258. #ifdef CONFIG_ARMV8_MULTIENTRY
  259. branch_if_master x0, x1, 2f
  260. /*
  261. * Slave should wait for master clearing spin table.
  262. * This sync prevent salves observing incorrect
  263. * value of spin table and jumping to wrong place.
  264. */
  265. #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
  266. #ifdef CONFIG_GICV2
  267. ldr x0, =GICC_BASE
  268. #endif
  269. bl gic_wait_for_interrupt
  270. #endif
  271. /*
  272. * All slaves will enter EL2 and optionally EL1.
  273. */
  274. adr x4, lowlevel_in_el2
  275. ldr x5, =ES_TO_AARCH64
  276. bl armv8_switch_to_el2
  277. lowlevel_in_el2:
  278. #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
  279. adr x4, lowlevel_in_el1
  280. ldr x5, =ES_TO_AARCH64
  281. bl armv8_switch_to_el1
  282. lowlevel_in_el1:
  283. #endif
  284. #endif /* CONFIG_ARMV8_MULTIENTRY */
  285. 2:
  286. mov lr, x29 /* Restore LR */
  287. ret
  288. ENDPROC(lowlevel_init)
  289. WEAK(smp_kick_all_cpus)
  290. /* Kick secondary cpus up by SGI 0 interrupt */
  291. #if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
  292. ldr x0, =GICD_BASE
  293. b gic_kick_secondary_cpus
  294. #endif
  295. ret
  296. ENDPROC(smp_kick_all_cpus)
  297. /*-----------------------------------------------------------------------*/
  298. ENTRY(c_runtime_cpu_setup)
  299. /* Relocate vBAR */
  300. adr x0, vectors
  301. switch_el x1, 3f, 2f, 1f
  302. 3: msr vbar_el3, x0
  303. b 0f
  304. 2: msr vbar_el2, x0
  305. b 0f
  306. 1: msr vbar_el1, x0
  307. 0:
  308. ret
  309. ENDPROC(c_runtime_cpu_setup)
  310. WEAK(save_boot_params)
  311. b save_boot_params_ret /* back to my caller */
  312. ENDPROC(save_boot_params)