blob: 28f0df13f0d63262d9c5cc9efda1de3ee47a85a2 [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:
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 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
Stephen Warren81c21372017-11-02 18:11:27 -060061#if CONFIG_POSITION_INDEPENDENT
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
Alexander Grafb3e9dc62019-02-20 17:14:49 +0100107#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_SPL_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
Oded Gabbay97a8d652016-12-27 11:19:43 +0200177#if defined(CONFIG_ARMV8_SPIN_TABLE) && !defined(CONFIG_SPL_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 */
181#elif defined(CONFIG_ARMV8_MULTIENTRY)
Andre Przywara93b9ce72022-02-11 11:29:39 +0000182 branch_if_master x0, master_cpu
David Feng85fd5f12013-12-14 11:47:35 +0800183
184 /*
185 * Slave CPUs
186 */
187slave_cpu:
188 wfe
189 ldr x1, =CPU_RELEASE_ADDR
190 ldr x0, [x1]
191 cbz x0, slave_cpu
192 br x0 /* branch to the given address */
Linus Walleij74771392015-03-09 10:53:21 +0100193#endif /* CONFIG_ARMV8_MULTIENTRY */
Masahiro Yamada2663cd62016-06-27 19:31:05 +0900194master_cpu:
Andre Przywara8a9cc732022-02-11 11:29:36 +0000195 msr SPSel, #1 /* make sure we use SP_ELx */
David Feng85fd5f12013-12-14 11:47:35 +0800196 bl _main
197
198/*-----------------------------------------------------------------------*/
199
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530200WEAK(apply_core_errata)
201
202 mov x29, lr /* Save LR */
Alison Wangc1293872017-12-28 13:00:55 +0800203 /* For now, we support Cortex-A53, Cortex-A57 specific errata */
204
205 /* Check if we are running on a Cortex-A53 core */
206 branch_if_a53_core x0, apply_a53_core_errata
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530207
208 /* Check if we are running on a Cortex-A57 core */
209 branch_if_a57_core x0, apply_a57_core_errata
2100:
211 mov lr, x29 /* Restore LR */
212 ret
213
Alison Wangc1293872017-12-28 13:00:55 +0800214apply_a53_core_errata:
215
216#ifdef CONFIG_ARM_ERRATA_855873
217 mrs x0, midr_el1
218 tst x0, #(0xf << 20)
219 b.ne 0b
220
221 mrs x0, midr_el1
222 and x0, x0, #0xf
223 cmp x0, #3
224 b.lt 0b
225
226 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
227 /* Enable data cache clean as data cache clean/invalidate */
228 orr x0, x0, #1 << 44
229 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000230 isb
Alison Wangc1293872017-12-28 13:00:55 +0800231#endif
232 b 0b
233
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530234apply_a57_core_errata:
235
236#ifdef CONFIG_ARM_ERRATA_828024
237 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
238 /* Disable non-allocate hint of w-b-n-a memory type */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530239 orr x0, x0, #1 << 49
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530240 /* Disable write streaming no L1-allocate threshold */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530241 orr x0, x0, #3 << 25
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530242 /* Disable write streaming no-allocate threshold */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530243 orr x0, x0, #3 << 27
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530244 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000245 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530246#endif
247
248#ifdef CONFIG_ARM_ERRATA_826974
249 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
250 /* Disable speculative load execution ahead of a DMB */
Bhupesh Sharma06a0f1d2015-05-28 14:54:13 +0530251 orr x0, x0, #1 << 59
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530252 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000253 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530254#endif
255
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530256#ifdef CONFIG_ARM_ERRATA_833471
257 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
258 /* FPSCR write flush.
259 * Note that in some cases where a flush is unnecessary this
260 could impact performance. */
261 orr x0, x0, #1 << 38
262 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000263 isb
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530264#endif
265
266#ifdef CONFIG_ARM_ERRATA_829520
267 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
268 /* Disable Indirect Predictor bit will prevent this erratum
269 from occurring
270 * Note that in some cases where a flush is unnecessary this
271 could impact performance. */
272 orr x0, x0, #1 << 4
273 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000274 isb
Ashish kumar9c6d33c2016-01-27 18:09:32 +0530275#endif
276
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530277#ifdef CONFIG_ARM_ERRATA_833069
278 mrs x0, S3_1_c15_c2_0 /* cpuactlr_el1 */
279 /* Disable Enable Invalidates of BTB bit */
280 and x0, x0, #0xE
281 msr S3_1_c15_c2_0, x0 /* cpuactlr_el1 */
Volodymyr Babchukb2b52252020-06-24 01:05:19 +0000282 isb
Bhupesh Sharma80a7e352015-01-23 15:50:04 +0530283#endif
284 b 0b
285ENDPROC(apply_core_errata)
286
287/*-----------------------------------------------------------------------*/
288
David Feng85fd5f12013-12-14 11:47:35 +0800289WEAK(lowlevel_init)
David Feng85fd5f12013-12-14 11:47:35 +0800290 mov x29, lr /* Save LR */
David Feng85fd5f12013-12-14 11:47:35 +0800291
David Feng79bbde02014-03-14 14:26:27 +0800292#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
293 branch_if_slave x0, 1f
294 ldr x0, =GICD_BASE
295 bl gic_init_secure
2961:
297#if defined(CONFIG_GICV3)
298 ldr x0, =GICR_BASE
299 bl gic_init_secure_percpu
300#elif defined(CONFIG_GICV2)
301 ldr x0, =GICD_BASE
302 ldr x1, =GICC_BASE
303 bl gic_init_secure_percpu
304#endif
Stephen Warren73f47af2016-04-28 12:45:44 -0600305#endif
David Feng79bbde02014-03-14 14:26:27 +0800306
Masahiro Yamadae4ce25f2016-05-20 12:13:10 +0900307#ifdef CONFIG_ARMV8_MULTIENTRY
Andre Przywara93b9ce72022-02-11 11:29:39 +0000308 branch_if_master x0, 2f
David Feng85fd5f12013-12-14 11:47:35 +0800309
310 /*
311 * Slave should wait for master clearing spin table.
312 * This sync prevent salves observing incorrect
313 * value of spin table and jumping to wrong place.
314 */
David Feng79bbde02014-03-14 14:26:27 +0800315#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
316#ifdef CONFIG_GICV2
317 ldr x0, =GICC_BASE
318#endif
319 bl gic_wait_for_interrupt
320#endif
David Feng85fd5f12013-12-14 11:47:35 +0800321
322 /*
David Feng79bbde02014-03-14 14:26:27 +0800323 * All slaves will enter EL2 and optionally EL1.
David Feng85fd5f12013-12-14 11:47:35 +0800324 */
Alison Wangeb2088d2017-01-17 09:39:17 +0800325 adr x4, lowlevel_in_el2
326 ldr x5, =ES_TO_AARCH64
David Feng85fd5f12013-12-14 11:47:35 +0800327 bl armv8_switch_to_el2
Alison Wang73818d52016-11-10 10:49:03 +0800328
329lowlevel_in_el2:
David Feng85fd5f12013-12-14 11:47:35 +0800330#ifdef CONFIG_ARMV8_SWITCH_TO_EL1
Alison Wangeb2088d2017-01-17 09:39:17 +0800331 adr x4, lowlevel_in_el1
332 ldr x5, =ES_TO_AARCH64
David Feng85fd5f12013-12-14 11:47:35 +0800333 bl armv8_switch_to_el1
Alison Wang73818d52016-11-10 10:49:03 +0800334
335lowlevel_in_el1:
David Feng85fd5f12013-12-14 11:47:35 +0800336#endif
337
Linus Walleij74771392015-03-09 10:53:21 +0100338#endif /* CONFIG_ARMV8_MULTIENTRY */
339
David Feng79bbde02014-03-14 14:26:27 +08003402:
David Feng85fd5f12013-12-14 11:47:35 +0800341 mov lr, x29 /* Restore LR */
342 ret
343ENDPROC(lowlevel_init)
344
David Feng79bbde02014-03-14 14:26:27 +0800345WEAK(smp_kick_all_cpus)
346 /* Kick secondary cpus up by SGI 0 interrupt */
David Feng79bbde02014-03-14 14:26:27 +0800347#if defined(CONFIG_GICV2) || defined(CONFIG_GICV3)
348 ldr x0, =GICD_BASE
Masahiro Yamadab9140092016-06-17 18:32:47 +0900349 b gic_kick_secondary_cpus
David Feng79bbde02014-03-14 14:26:27 +0800350#endif
David Feng79bbde02014-03-14 14:26:27 +0800351 ret
352ENDPROC(smp_kick_all_cpus)
353
David Feng85fd5f12013-12-14 11:47:35 +0800354/*-----------------------------------------------------------------------*/
355
356ENTRY(c_runtime_cpu_setup)
Alexander Grafb3e9dc62019-02-20 17:14:49 +0100357#if defined(CONFIG_ARMV8_SPL_EXCEPTION_VECTORS) || !defined(CONFIG_SPL_BUILD)
David Feng85fd5f12013-12-14 11:47:35 +0800358 /* Relocate vBAR */
359 adr x0, vectors
360 switch_el x1, 3f, 2f, 1f
3613: msr vbar_el3, x0
362 b 0f
3632: msr vbar_el2, x0
364 b 0f
3651: msr vbar_el1, x0
3660:
Andre Przywara4eecab72018-07-25 00:57:01 +0100367#endif
David Feng85fd5f12013-12-14 11:47:35 +0800368
369 ret
370ENDPROC(c_runtime_cpu_setup)
Stephen Warren100a4792016-07-18 17:01:50 -0600371
372WEAK(save_boot_params)
373 b save_boot_params_ret /* back to my caller */
374ENDPROC(save_boot_params)