blob: d3a8a7c4787b13967add2ec1016cb26a8f12cc1e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
David Feng85fd5f12013-12-14 11:47:35 +08002/*
3 * (C) Copyright 2013
4 * David Feng <fenghua@phytium.com.cn>
David Feng85fd5f12013-12-14 11:47:35 +08005 */
6
7#include <asm-offsets.h>
8#include <config.h>
David Feng85fd5f12013-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 Kaukaba0dcfc52019-06-13 14:46:44 +020021#if defined(CONFIG_LINUX_KERNEL_IMAGE_HEADER)
Stephen Warren80a93652018-01-03 14:31:51 -070022#include <asm/boot0-linux-kernel-header.h>
23#elif defined(CONFIG_ENABLE_ARM_SOC_BOOT0_HOOK)
Andre Przywara48321ba2016-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 Przywara313a5782017-01-02 11:48:33 +000030#else
31 b reset
Andre Przywara48321ba2016-05-31 10:45:06 -070032#endif
33
David Feng85fd5f12013-12-14 11:47:35 +080034 .align 3
35
36.globl _TEXT_BASE
37_TEXT_BASE:
Simon Glass72cc5382022-10-20 18:22:39 -060038 .quad CONFIG_TEXT_BASE
David Feng85fd5f12013-12-14 11:47:35 +080039
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 Warren100a4792016-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
Simon Glass85ed77d2024-09-29 19:49:46 -060061#if CONFIG_POSITION_INDEPENDENT && !defined(CONFIG_XPL_BUILD)
Edgar E. Iglesias67c9c932020-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 Warren81c21372017-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 Przywarae0d397c2020-09-30 17:39:14 +010086 subs x9, x0, x1 /* x9 <- Run-vs-link offset */
87 beq pie_fixup_done
Edgar E. Iglesias545474e2020-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 Warren81c21372017-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
Simon Glass85ed77d2024-09-29 19:49:46 -0600107#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_XPL_BUILD)
Andre Przywara4eecab72018-07-25 00:57:01 +0100108.macro set_vbar, regname, reg
109 msr \regname, \reg
110.endm
111 adr x0, vectors
112#else
113.macro set_vbar, regname, reg
114.endm
115#endif
David Feng85fd5f12013-12-14 11:47:35 +0800116 /*
117 * Could be EL3/EL2/EL1, Initial State:
118 * Little Endian, MMU Disabled, i/dCache Disabled
119 */
David Feng85fd5f12013-12-14 11:47:35 +0800120 switch_el x1, 3f, 2f, 1f
Andre Przywara4eecab72018-07-25 00:57:01 +01001213: set_vbar vbar_el3, x0
David Feng7c5eca72014-04-19 09:45:21 +0800122 mrs x0, scr_el3
David Feng79bbde02014-03-14 14:26:27 +0800123 orr x0, x0, #0xf /* SCR_EL3.NS|IRQ|FIQ|EA */
124 msr scr_el3, x0
David Feng85fd5f12013-12-14 11:47:35 +0800125 msr cptr_el3, xzr /* Enable FP/SIMD */
David Feng85fd5f12013-12-14 11:47:35 +0800126 b 0f
Mark Kettenis34b47162021-02-10 20:14:55 +01001272: mrs x1, hcr_el2
Andre Przywara630a7942022-06-14 00:11:10 +0100128 tbnz x1, #HCR_EL2_E2H_BIT, 1f /* HCR_EL2.E2H */
Andre Przywara3a5c0522022-02-11 11:29:35 +0000129 orr x1, x1, #HCR_EL2_AMO_EL2 /* Route SErrors to EL2 */
130 msr hcr_el2, x1
Mark Kettenis34b47162021-02-10 20:14:55 +0100131 set_vbar vbar_el2, x0
David Feng85fd5f12013-12-14 11:47:35 +0800132 mov x0, #0x33ff
133 msr cptr_el2, x0 /* Enable FP/SIMD */
134 b 0f
Mark Kettenis34b47162021-02-10 20:14:55 +01001351: set_vbar vbar_el1, x0
David Feng85fd5f12013-12-14 11:47:35 +0800136 mov x0, #3 << 20
137 msr cpacr_el1, x0 /* Enable FP/SIMD */
1380:
Andre Przywara3a5c0522022-02-11 11:29:35 +0000139 msr daifclr, #0x4 /* Unmask SError interrupts */
Peter Hoyes55262102021-07-12 15:04:21 +0100140
Peng Fane7c59392022-04-13 17:47:22 +0800141#if CONFIG_COUNTER_FREQUENCY
Peter Hoyes55262102021-07-12 15:04:21 +0100142 branch_if_not_highest_el x0, 4f
Peng Fane7c59392022-04-13 17:47:22 +0800143 ldr x0, =CONFIG_COUNTER_FREQUENCY
Peter Hoyes55262102021-07-12 15:04:21 +0100144 msr cntfrq_el0, x0 /* Initialize CNTFRQ */
145#endif
146
1474: isb
David Feng85fd5f12013-12-14 11:47:35 +0800148
Mingkai Hu553d4052017-01-06 17:41:10 +0800149 /*
Dinh Nguyenc3e970a2017-04-26 23:36:03 -0500150 * Enable SMPEN bit for coherency.
Mingkai Hu553d4052017-01-06 17:41:10 +0800151 * This register is not architectural but at the moment
152 * this bit should be set for A53/A57/A72.
153 */
154#ifdef CONFIG_ARMV8_SET_SMPEN
York Sune6b871e2017-05-15 08:51:59 -0700155 switch_el x1, 3f, 1f, 1f
1563:
Dinh Nguyenc3e970a2017-04-26 23:36:03 -0500157 mrs x0, S3_1_c15_c2_1 /* cpuectlr_el1 */
Mingkai Hu553d4052017-01-06 17:41:10 +0800158 orr x0, x0, #0x40
159 msr S3_1_c15_c2_1, x0
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000160 isb
York Sune6b871e2017-05-15 08:51:59 -07001611:
Mingkai Hu553d4052017-01-06 17:41:10 +0800162#endif
163
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530164 /* Apply ARM core specific erratas */
165 bl apply_core_errata
166
York Sunef042012014-02-26 13:26:04 -0800167 /*
168 * Cache/BPB/TLB Invalidate
169 * i-cache is invalidated before enabled in icache_enable()
170 * tlb is invalidated before mmu is enabled in dcache_enable()
171 * d-cache is invalidated before enabled in dcache_enable()
172 */
David Feng85fd5f12013-12-14 11:47:35 +0800173
174 /* Processor specific initialization */
175 bl lowlevel_init
176
Simon Glass85ed77d2024-09-29 19:49:46 -0600177#if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_XPL_BUILD)
Andre Przywara93b9ce72022-02-11 11:29:39 +0000178 branch_if_master x0, master_cpu
Masahiro Yamada2663cd62016-06-27 19:31:05 +0900179 b spin_table_secondary_jump
180 /* never return */
Patrick Rudolpha2e4deb2024-10-23 15:20:14 +0200181#elif defined(CONFIG_ACPI_PARKING_PROTOCOL) && !defined(CONFIG_SPL_BUILD)
182 branch_if_master x0, master_cpu
183 /*
184 * Waits for ACPI parking protocol memory to be allocated and the spin-table
185 * code to be written. Once ready the secondary CPUs will jump and spin in
186 * their own 4KiB memory region, which is also used as mailbox, until released
187 * by the OS.
188 * The mechanism is similar to the DT enable-method = "spin-table", but works
189 * with ACPI enabled platforms.
190 */
191 b acpi_pp_secondary_jump
192 /* never return */
Masahiro Yamada2663cd62016-06-27 19:31:05 +0900193#elif defined(CONFIG_ARMV8_MULTIENTRY)
Andre Przywara93b9ce72022-02-11 11:29:39 +0000194 branch_if_master x0, master_cpu
David Feng85fd5f12013-12-14 11:47:35 +0800195
196 /*
197 * Slave CPUs
198 */
199slave_cpu:
200 wfe
201 ldr x1, =CPU_RELEASE_ADDR
202 ldr x0, [x1]
203 cbz x0, slave_cpu
204 br x0 /* branch to the given address */
Linus Walleij74771392015-03-09 10:53:21 +0100205#endif /* CONFIG_ARMV8_MULTIENTRY */
Masahiro Yamada2663cd62016-06-27 19:31:05 +0900206master_cpu:
Andre Przywara8a9cc732022-02-11 11:29:36 +0000207 msr SPSel, #1 /* make sure we use SP_ELx */
David Feng85fd5f12013-12-14 11:47:35 +0800208 bl _main
209
210/*-----------------------------------------------------------------------*/
211
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530212WEAK(apply_core_errata)
213
214 mov x29, lr /* Save LR */
Alison Wangc1293872017-12-28 13:00:55 +0800215 /* For now, we support Cortex-A53, Cortex-A57 specific errata */
216
217 /* Check if we are running on a Cortex-A53 core */
218 branch_if_a53_core x0, apply_a53_core_errata
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530219
220 /* Check if we are running on a Cortex-A57 core */
221 branch_if_a57_core x0, apply_a57_core_errata
2220:
223 mov lr, x29 /* Restore LR */
224 ret
225
Alison Wangc1293872017-12-28 13:00:55 +0800226apply_a53_core_errata:
227
228#ifdef CONFIG_ARM_ERRATA_855873
229 mrs x0, midr_el1
230 tst x0, #(0xf << 20)
231 b.ne 0b
232
233 mrs x0, midr_el1
234 and x0, x0, #0xf
235 cmp x0, #3
236 b.lt 0b
237
238 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
239 /* Enable data cache clean as data cache clean/invalidate */
240 orr x0, x0, #1 << 44
241 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000242 isb
Alison Wangc1293872017-12-28 13:00:55 +0800243#endif
244 b 0b
245
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530246apply_a57_core_errata:
247
248#ifdef CONFIG_ARM_ERRATA_828024
249 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
250 /* Disable non-allocate hint of w-b-n-a memory type */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530251 orr x0, x0, #1 << 49
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530252 /* Disable write streaming no L1-allocate threshold */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530253 orr x0, x0, #3 << 25
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530254 /* Disable write streaming no-allocate threshold */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530255 orr x0, x0, #3 << 27
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530256 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000257 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530258#endif
259
260#ifdef CONFIG_ARM_ERRATA_826974
261 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
262 /* Disable speculative load execution ahead of a DMB */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530263 orr x0, x0, #1 << 59
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530264 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000265 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530266#endif
267
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530268#ifdef CONFIG_ARM_ERRATA_833471
269 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
270 /* FPSCR write flush.
271 * Note that in some cases where a flush is unnecessary this
272 could impact performance. */
273 orr x0, x0, #1 << 38
274 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000275 isb
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530276#endif
277
278#ifdef CONFIG_ARM_ERRATA_829520
279 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
280 /* Disable Indirect Predictor bit will prevent this erratum
281 from occurring
282 * Note that in some cases where a flush is unnecessary this
283 could impact performance. */
284 orr x0, x0, #1 << 4
285 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000286 isb
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530287#endif
288
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530289#ifdef CONFIG_ARM_ERRATA_833069
290 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
291 /* Disable Enable Invalidates of BTB bit */
292 and x0, x0, #0xE
293 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000294 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530295#endif
296 b 0b
297ENDPROC(apply_core_errata)
298
299/*-----------------------------------------------------------------------*/
300
David Feng85fd5f12013-12-14 11:47:35 +0800301WEAK(lowlevel_init)
David Feng85fd5f12013-12-14 11:47:35 +0800302 mov x29, lr /* Save LR */
David Feng85fd5f12013-12-14 11:47:35 +0800303
David Feng79bbde02014-03-14 14:26:27 +0800304#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
305 branch_if_slave x0, 1f
306 ldr x0, =GICD_BASE
307 bl gic_init_secure
3081:
309#if defined(CONFIG_GICV3)
310 ldr x0, =GICR_BASE
311 bl gic_init_secure_percpu
312#elif defined(CONFIG_GICV2)
313 ldr x0, =GICD_BASE
314 ldr x1, =GICC_BASE
315 bl gic_init_secure_percpu
316#endif
Stephen Warren73f47af2016-04-28 12:45:44 -0600317#endif
David Feng79bbde02014-03-14 14:26:27 +0800318
Masahiro Yamadae4ce25f2016-05-20 12:13:10 +0900319#ifdef CONFIG_ARMV8_MULTIENTRY
Andre Przywara93b9ce72022-02-11 11:29:39 +0000320 branch_if_master x0, 2f
David Feng85fd5f12013-12-14 11:47:35 +0800321
322 /*
323 * Slave should wait for master clearing spin table.
324 * This sync prevent salves observing incorrect
325 * value of spin table and jumping to wrong place.
326 */
David Feng79bbde02014-03-14 14:26:27 +0800327#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
328#ifdef CONFIG_GICV2
329 ldr x0, =GICC_BASE
330#endif
331 bl gic_wait_for_interrupt
332#endif
David Feng85fd5f12013-12-14 11:47:35 +0800333
334 /*
David Feng79bbde02014-03-14 14:26:27 +0800335 * All slaves will enter EL2 and optionally EL1.
David Feng85fd5f12013-12-14 11:47:35 +0800336 */
Alison Wangeb2088d2017-01-17 09:39:17 +0800337 adr x4, lowlevel_in_el2
338 ldr x5, =ES_TO_AARCH64
David Feng85fd5f12013-12-14 11:47:35 +0800339 bl armv8_switch_to_el2
Alison Wang73818d52016-11-10 10:49:03 +0800340
341lowlevel_in_el2:
David Feng85fd5f12013-12-14 11:47:35 +0800342#ifdef CONFIG_ARMV8_SWITCH_TO_EL1
Alison Wangeb2088d2017-01-17 09:39:17 +0800343 adr x4, lowlevel_in_el1
344 ldr x5, =ES_TO_AARCH64
David Feng85fd5f12013-12-14 11:47:35 +0800345 bl armv8_switch_to_el1
Alison Wang73818d52016-11-10 10:49:03 +0800346
347lowlevel_in_el1:
David Feng85fd5f12013-12-14 11:47:35 +0800348#endif
349
Linus Walleij74771392015-03-09 10:53:21 +0100350#endif /* CONFIG_ARMV8_MULTIENTRY */
351
David Feng79bbde02014-03-14 14:26:27 +08003522:
David Feng85fd5f12013-12-14 11:47:35 +0800353 mov lr, x29 /* Restore LR */
354 ret
355ENDPROC(lowlevel_init)
356
David Feng79bbde02014-03-14 14:26:27 +0800357WEAK(smp_kick_all_cpus)
358 /* Kick secondary cpus up by SGI 0 interrupt */
David Feng79bbde02014-03-14 14:26:27 +0800359#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
360 ldr x0, =GICD_BASE
Masahiro Yamadab9140092016-06-17 18:32:47 +0900361 b gic_kick_secondary_cpus
David Feng79bbde02014-03-14 14:26:27 +0800362#endif
David Feng79bbde02014-03-14 14:26:27 +0800363 ret
364ENDPROC(smp_kick_all_cpus)
365
David Feng85fd5f12013-12-14 11:47:35 +0800366/*-----------------------------------------------------------------------*/
367
368ENTRY(c_runtime_cpu_setup)
Simon Glass85ed77d2024-09-29 19:49:46 -0600369#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_XPL_BUILD)
David Feng85fd5f12013-12-14 11:47:35 +0800370 /* Relocate vBAR */
371 adr x0, vectors
372 switch_el x1, 3f, 2f, 1f
3733: msr vbar_el3, x0
374 b 0f
3752: msr vbar_el2, x0
376 b 0f
3771: msr vbar_el1, x0
3780:
Andre Przywara4eecab72018-07-25 00:57:01 +0100379#endif
David Feng85fd5f12013-12-14 11:47:35 +0800380
381 ret
382ENDPROC(c_runtime_cpu_setup)
Stephen Warren100a4792016-07-18 17:01:50 -0600383
384WEAK(save_boot_params)
Raymond Maof2c5f7c2024-02-03 08:36:24 -0800385#if (IS_ENABLED(CONFIG_BLOBLIST))
386 /* Calculate the PC-relative address of saved_args */
387 adr x9, saved_args_offset
388 ldr w10, saved_args_offset
389 add x9, x9, w10, sxtw
390
391 stp x0, x1, [x9]
392 stp x2, x3, [x9, #16]
393#endif
Stephen Warren100a4792016-07-18 17:01:50 -0600394 b save_boot_params_ret /* back to my caller */
395ENDPROC(save_boot_params)
Raymond Maof2c5f7c2024-02-03 08:36:24 -0800396
397#if (IS_ENABLED(CONFIG_BLOBLIST))
398saved_args_offset:
399 .long saved_args - . /* offset from current code to save_args */
400
401 .section .data
402 .align 2
403 .global saved_args
404saved_args:
405 .rept 4
406 .dword 0
407 .endr
408END(saved_args)
409#endif