blob: 9e9c6140cd4a520b5a6fac0469ea10c2fdecdc88 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
David Feng0ae76532013-12-14 11:47:35 +08002/*
3 * (C) Copyright 2013
4 * David Feng <fenghua@phytium.com.cn>
David Feng0ae76532013-12-14 11:47:35 +08005 */
6
7#include <asm-offsets.h>
8#include <config.h>
David Feng0ae76532013-12-14 11:47:35 +08009#include <linux/linkage.h>
10#include <asm/macro.h>
11#include <asm/armv8/mmu.h>
12
13/*************************************************************************
14 *
15 * Startup Code (reset vector)
16 *
17 *************************************************************************/
18
19.globl _start
20_start:
Mian Yousaf Kaukabf2f83b22019-06-13 14:46:44 +020021#if defined(CONFIG_LINUX_KERNEL_IMAGE_HEADER)
Stephen Warren8163faf2018-01-03 14:31:51 -070022#include <asm/boot0-linux-kernel-header.h>
23#elif defined(CONFIG_ENABLE_ARM_SOC_BOOT0_HOOK)
Andre Przywaracdaa6332016-05-31 10:45:06 -070024/*
25 * Various SoCs need something special and SoC-specific up front in
26 * order to boot, allow them to set that in their boot0.h file and then
27 * use it here.
28 */
29#include <asm/arch/boot0.h>
Andre Przywaraa5168a52017-01-02 11:48:33 +000030#else
31 b reset
Andre Przywaracdaa6332016-05-31 10:45:06 -070032#endif
33
David Feng0ae76532013-12-14 11:47:35 +080034 .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:
Stephen Warren0e2b5352016-07-18 17:01:50 -060056 /* Allow the board to save important registers */
57 b save_boot_params
58.globl save_boot_params_ret
59save_boot_params_ret:
60
Stephen Warren49e93872017-11-02 18:11:27 -060061#if CONFIG_POSITION_INDEPENDENT
Edgar E. Iglesias04d13b52020-09-09 19:07:25 +020062 /* Verify that we're 4K aligned. */
63 adr x0, _start
64 ands x0, x0, #0xfff
65 b.eq 1f
660:
67 /*
68 * FATAL, can't continue.
69 * U-Boot needs to be loaded at a 4K aligned address.
70 *
71 * We use ADRP and ADD to load some symbol addresses during startup.
72 * The ADD uses an absolute (non pc-relative) lo12 relocation
73 * thus requiring 4K alignment.
74 */
75 wfi
76 b 0b
771:
78
Stephen Warren49e93872017-11-02 18:11:27 -060079 /*
80 * Fix .rela.dyn relocations. This allows U-Boot to be loaded to and
81 * executed at a different address than it was linked at.
82 */
83pie_fixup:
84 adr x0, _start /* x0 <- Runtime value of _start */
85 ldr x1, _TEXT_BASE /* x1 <- Linked value of _start */
Andre Przywara9a984f12020-09-30 17:39:14 +010086 subs x9, x0, x1 /* x9 <- Run-vs-link offset */
87 beq pie_fixup_done
Edgar E. Iglesias28c851f2020-09-09 19:07:26 +020088 adrp x2, __rel_dyn_start /* x2 <- Runtime &__rel_dyn_start */
89 add x2, x2, #:lo12:__rel_dyn_start
90 adrp x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */
91 add x3, x3, #:lo12:__rel_dyn_end
Stephen Warren49e93872017-11-02 18:11:27 -060092pie_fix_loop:
93 ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */
94 ldr x4, [x2], #8 /* x4 <- addend */
95 cmp w1, #1027 /* relative fixup? */
96 bne pie_skip_reloc
97 /* relative fix: store addend plus offset at dest location */
98 add x0, x0, x9
99 add x4, x4, x9
100 str x4, [x0]
101pie_skip_reloc:
102 cmp x2, x3
103 b.lo pie_fix_loop
104pie_fixup_done:
105#endif
106
Sergey Temerkhanov94f7ff32015-10-14 09:55:45 -0700107#ifdef CONFIG_SYS_RESET_SCTRL
108 bl reset_sctrl
109#endif
Andre Przywara1416e2d2018-07-25 00:57:01 +0100110
Alexander Grafef331e32019-02-20 17:14:49 +0100111#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_SPL_BUILD)
Andre Przywara1416e2d2018-07-25 00:57:01 +0100112.macro set_vbar, regname, reg
113 msr \regname, \reg
114.endm
115 adr x0, vectors
116#else
117.macro set_vbar, regname, reg
118.endm
119#endif
David Feng0ae76532013-12-14 11:47:35 +0800120 /*
121 * Could be EL3/EL2/EL1, Initial State:
122 * Little Endian, MMU Disabled, i/dCache Disabled
123 */
David Feng0ae76532013-12-14 11:47:35 +0800124 switch_el x1, 3f, 2f, 1f
Andre Przywara1416e2d2018-07-25 00:57:01 +01001253: set_vbar vbar_el3, x0
David Feng1277bac2014-04-19 09:45:21 +0800126 mrs x0, scr_el3
David Fengc71645a2014-03-14 14:26:27 +0800127 orr x0, x0, #0xf /* SCR_EL3.NS|IRQ|FIQ|EA */
128 msr scr_el3, x0
David Feng0ae76532013-12-14 11:47:35 +0800129 msr cptr_el3, xzr /* Enable FP/SIMD */
Thierry Reding70bcb432015-08-20 11:42:18 +0200130#ifdef COUNTER_FREQUENCY
David Feng0ae76532013-12-14 11:47:35 +0800131 ldr x0, =COUNTER_FREQUENCY
132 msr cntfrq_el0, x0 /* Initialize CNTFRQ */
Thierry Reding70bcb432015-08-20 11:42:18 +0200133#endif
David Feng0ae76532013-12-14 11:47:35 +0800134 b 0f
Mark Kettenisbfb79842021-02-10 20:14:55 +01001352: mrs x1, hcr_el2
136 tbnz x1, #34, 1f /* HCR_EL2.E2H */
137 set_vbar vbar_el2, x0
David Feng0ae76532013-12-14 11:47:35 +0800138 mov x0, #0x33ff
139 msr cptr_el2, x0 /* Enable FP/SIMD */
140 b 0f
Mark Kettenisbfb79842021-02-10 20:14:55 +01001411: set_vbar vbar_el1, x0
David Feng0ae76532013-12-14 11:47:35 +0800142 mov x0, #3 << 20
143 msr cpacr_el1, x0 /* Enable FP/SIMD */
1440:
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000145 isb
David Feng0ae76532013-12-14 11:47:35 +0800146
Mingkai Hu3aec4522017-01-06 17:41:10 +0800147 /*
Dinh Nguyen9ad71472017-04-26 23:36:03 -0500148 * Enable SMPEN bit for coherency.
Mingkai Hu3aec4522017-01-06 17:41:10 +0800149 * This register is not architectural but at the moment
150 * this bit should be set for A53/A57/A72.
151 */
152#ifdef CONFIG_ARMV8_SET_SMPEN
York Sun399e2bb2017-05-15 08:51:59 -0700153 switch_el x1, 3f, 1f, 1f
1543:
Dinh Nguyen9ad71472017-04-26 23:36:03 -0500155 mrs x0, S3_1_c15_c2_1 /* cpuectlr_el1 */
Mingkai Hu3aec4522017-01-06 17:41:10 +0800156 orr x0, x0, #0x40
157 msr S3_1_c15_c2_1, x0
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000158 isb
York Sun399e2bb2017-05-15 08:51:59 -07001591:
Mingkai Hu3aec4522017-01-06 17:41:10 +0800160#endif
161
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530162 /* Apply ARM core specific erratas */
163 bl apply_core_errata
164
York Sun1e6ad552014-02-26 13:26:04 -0800165 /*
166 * Cache/BPB/TLB Invalidate
167 * i-cache is invalidated before enabled in icache_enable()
168 * tlb is invalidated before mmu is enabled in dcache_enable()
169 * d-cache is invalidated before enabled in dcache_enable()
170 */
David Feng0ae76532013-12-14 11:47:35 +0800171
172 /* Processor specific initialization */
173 bl lowlevel_init
174
Oded Gabbay4b105f62016-12-27 11:19:43 +0200175#if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_BUILD)
Masahiro Yamada6b6024e2016-06-27 19:31:05 +0900176 branch_if_master x0, x1, master_cpu
177 b spin_table_secondary_jump
178 /* never return */
179#elif defined(CONFIG_ARMV8_MULTIENTRY)
David Feng0ae76532013-12-14 11:47:35 +0800180 branch_if_master x0, x1, master_cpu
181
182 /*
183 * Slave CPUs
184 */
185slave_cpu:
186 wfe
187 ldr x1, =CPU_RELEASE_ADDR
188 ldr x0, [x1]
189 cbz x0, slave_cpu
190 br x0 /* branch to the given address */
Linus Walleij23b58772015-03-09 10:53:21 +0100191#endif /* CONFIG_ARMV8_MULTIENTRY */
Masahiro Yamada6b6024e2016-06-27 19:31:05 +0900192master_cpu:
David Feng0ae76532013-12-14 11:47:35 +0800193 bl _main
194
Sergey Temerkhanov94f7ff32015-10-14 09:55:45 -0700195#ifdef CONFIG_SYS_RESET_SCTRL
196reset_sctrl:
197 switch_el x1, 3f, 2f, 1f
1983:
199 mrs x0, sctlr_el3
200 b 0f
2012:
202 mrs x0, sctlr_el2
203 b 0f
2041:
205 mrs x0, sctlr_el1
206
2070:
208 ldr x1, =0xfdfffffa
209 and x0, x0, x1
210
211 switch_el x1, 6f, 5f, 4f
2126:
213 msr sctlr_el3, x0
214 b 7f
2155:
216 msr sctlr_el2, x0
217 b 7f
2184:
219 msr sctlr_el1, x0
220
2217:
222 dsb sy
223 isb
224 b __asm_invalidate_tlb_all
225 ret
226#endif
227
David Feng0ae76532013-12-14 11:47:35 +0800228/*-----------------------------------------------------------------------*/
229
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530230WEAK(apply_core_errata)
231
232 mov x29, lr /* Save LR */
Alison Wangab0ab542017-12-28 13:00:55 +0800233 /* For now, we support Cortex-A53, Cortex-A57 specific errata */
234
235 /* Check if we are running on a Cortex-A53 core */
236 branch_if_a53_core x0, apply_a53_core_errata
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530237
238 /* Check if we are running on a Cortex-A57 core */
239 branch_if_a57_core x0, apply_a57_core_errata
2400:
241 mov lr, x29 /* Restore LR */
242 ret
243
Alison Wangab0ab542017-12-28 13:00:55 +0800244apply_a53_core_errata:
245
246#ifdef CONFIG_ARM_ERRATA_855873
247 mrs x0, midr_el1
248 tst x0, #(0xf << 20)
249 b.ne 0b
250
251 mrs x0, midr_el1
252 and x0, x0, #0xf
253 cmp x0, #3
254 b.lt 0b
255
256 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
257 /* Enable data cache clean as data cache clean/invalidate */
258 orr x0, x0, #1 << 44
259 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000260 isb
Alison Wangab0ab542017-12-28 13:00:55 +0800261#endif
262 b 0b
263
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530264apply_a57_core_errata:
265
266#ifdef CONFIG_ARM_ERRATA_828024
267 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
268 /* Disable non-allocate hint of w-b-n-a memory type */
Bhupesh Sharmaf299b5b2015-05-28 14:54:13 +0530269 orr x0, x0, #1 << 49
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530270 /* Disable write streaming no L1-allocate threshold */
Bhupesh Sharmaf299b5b2015-05-28 14:54:13 +0530271 orr x0, x0, #3 << 25
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530272 /* Disable write streaming no-allocate threshold */
Bhupesh Sharmaf299b5b2015-05-28 14:54:13 +0530273 orr x0, x0, #3 << 27
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530274 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000275 isb
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530276#endif
277
278#ifdef CONFIG_ARM_ERRATA_826974
279 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
280 /* Disable speculative load execution ahead of a DMB */
Bhupesh Sharmaf299b5b2015-05-28 14:54:13 +0530281 orr x0, x0, #1 << 59
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530282 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000283 isb
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530284#endif
285
Ashish kumar2ea3a442016-01-27 18:09:32 +0530286#ifdef CONFIG_ARM_ERRATA_833471
287 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
288 /* FPSCR write flush.
289 * Note that in some cases where a flush is unnecessary this
290 could impact performance. */
291 orr x0, x0, #1 << 38
292 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000293 isb
Ashish kumar2ea3a442016-01-27 18:09:32 +0530294#endif
295
296#ifdef CONFIG_ARM_ERRATA_829520
297 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
298 /* Disable Indirect Predictor bit will prevent this erratum
299 from occurring
300 * Note that in some cases where a flush is unnecessary this
301 could impact performance. */
302 orr x0, x0, #1 << 4
303 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000304 isb
Ashish kumar2ea3a442016-01-27 18:09:32 +0530305#endif
306
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530307#ifdef CONFIG_ARM_ERRATA_833069
308 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
309 /* Disable Enable Invalidates of BTB bit */
310 and x0, x0, #0xE
311 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukf8ddd8c2020-06-24 01:05:19 +0000312 isb
Bhupesh Sharma37118fb2015-01-23 15:50:04 +0530313#endif
314 b 0b
315ENDPROC(apply_core_errata)
316
317/*-----------------------------------------------------------------------*/
318
David Feng0ae76532013-12-14 11:47:35 +0800319WEAK(lowlevel_init)
David Feng0ae76532013-12-14 11:47:35 +0800320 mov x29, lr /* Save LR */
David Feng0ae76532013-12-14 11:47:35 +0800321
David Fengc71645a2014-03-14 14:26:27 +0800322#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
323 branch_if_slave x0, 1f
324 ldr x0, =GICD_BASE
325 bl gic_init_secure
3261:
327#if defined(CONFIG_GICV3)
328 ldr x0, =GICR_BASE
329 bl gic_init_secure_percpu
330#elif defined(CONFIG_GICV2)
331 ldr x0, =GICD_BASE
332 ldr x1, =GICC_BASE
333 bl gic_init_secure_percpu
334#endif
Stephen Warren11661192016-04-28 12:45:44 -0600335#endif
David Fengc71645a2014-03-14 14:26:27 +0800336
Masahiro Yamadad38fca42016-05-20 12:13:10 +0900337#ifdef CONFIG_ARMV8_MULTIENTRY
David Fengc71645a2014-03-14 14:26:27 +0800338 branch_if_master x0, x1, 2f
David Feng0ae76532013-12-14 11:47:35 +0800339
340 /*
341 * Slave should wait for master clearing spin table.
342 * This sync prevent salves observing incorrect
343 * value of spin table and jumping to wrong place.
344 */
David Fengc71645a2014-03-14 14:26:27 +0800345#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
346#ifdef CONFIG_GICV2
347 ldr x0, =GICC_BASE
348#endif
349 bl gic_wait_for_interrupt
350#endif
David Feng0ae76532013-12-14 11:47:35 +0800351
352 /*
David Fengc71645a2014-03-14 14:26:27 +0800353 * All slaves will enter EL2 and optionally EL1.
David Feng0ae76532013-12-14 11:47:35 +0800354 */
Alison Wang7c5e1fe2017-01-17 09:39:17 +0800355 adr x4, lowlevel_in_el2
356 ldr x5, =ES_TO_AARCH64
David Feng0ae76532013-12-14 11:47:35 +0800357 bl armv8_switch_to_el2
Alison Wangec6617c2016-11-10 10:49:03 +0800358
359lowlevel_in_el2:
David Feng0ae76532013-12-14 11:47:35 +0800360#ifdef CONFIG_ARMV8_SWITCH_TO_EL1
Alison Wang7c5e1fe2017-01-17 09:39:17 +0800361 adr x4, lowlevel_in_el1
362 ldr x5, =ES_TO_AARCH64
David Feng0ae76532013-12-14 11:47:35 +0800363 bl armv8_switch_to_el1
Alison Wangec6617c2016-11-10 10:49:03 +0800364
365lowlevel_in_el1:
David Feng0ae76532013-12-14 11:47:35 +0800366#endif
367
Linus Walleij23b58772015-03-09 10:53:21 +0100368#endif /* CONFIG_ARMV8_MULTIENTRY */
369
David Fengc71645a2014-03-14 14:26:27 +08003702:
David Feng0ae76532013-12-14 11:47:35 +0800371 mov lr, x29 /* Restore LR */
372 ret
373ENDPROC(lowlevel_init)
374
David Fengc71645a2014-03-14 14:26:27 +0800375WEAK(smp_kick_all_cpus)
376 /* Kick secondary cpus up by SGI 0 interrupt */
David Fengc71645a2014-03-14 14:26:27 +0800377#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
378 ldr x0, =GICD_BASE
Masahiro Yamadaafedf542016-06-17 18:32:47 +0900379 b gic_kick_secondary_cpus
David Fengc71645a2014-03-14 14:26:27 +0800380#endif
David Fengc71645a2014-03-14 14:26:27 +0800381 ret
382ENDPROC(smp_kick_all_cpus)
383
David Feng0ae76532013-12-14 11:47:35 +0800384/*-----------------------------------------------------------------------*/
385
386ENTRY(c_runtime_cpu_setup)
Alexander Grafef331e32019-02-20 17:14:49 +0100387#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_SPL_BUILD)
David Feng0ae76532013-12-14 11:47:35 +0800388 /* Relocate vBAR */
389 adr x0, vectors
390 switch_el x1, 3f, 2f, 1f
3913: msr vbar_el3, x0
392 b 0f
3932: msr vbar_el2, x0
394 b 0f
3951: msr vbar_el1, x0
3960:
Andre Przywara1416e2d2018-07-25 00:57:01 +0100397#endif
David Feng0ae76532013-12-14 11:47:35 +0800398
399 ret
400ENDPROC(c_runtime_cpu_setup)
Stephen Warren0e2b5352016-07-18 17:01:50 -0600401
402WEAK(save_boot_params)
403 b save_boot_params_ret /* back to my caller */
404ENDPROC(save_boot_params)