blob: 3402d09a05426ebeb86dd4935b13432fd1d70c19 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Rick Chene76b8042017-12-26 13:55:48 +08002/*
3 * Startup Code for RISC-V Core
4 *
5 * Copyright (c) 2017 Microsemi Corporation.
6 * Copyright (c) 2017 Padmarao Begari <Padmarao.Begari@microsemi.com>
7 *
8 * Copyright (C) 2017 Andes Technology Corporation
9 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
Rick Chene76b8042017-12-26 13:55:48 +080010 */
11
12#include <asm-offsets.h>
13#include <config.h>
14#include <common.h>
15#include <elf.h>
Lukas Auera3596652019-03-17 19:28:37 +010016#include <asm/csr.h>
Rick Chene76b8042017-12-26 13:55:48 +080017#include <asm/encoding.h>
Bin Meng89681a72018-12-12 06:12:45 -080018#include <generated/asm-offsets.h>
Rick Chene76b8042017-12-26 13:55:48 +080019
20#ifdef CONFIG_32BIT
Lukas Auer7cf43682018-11-22 11:26:24 +010021#define LREG lw
22#define SREG sw
23#define REGBYTES 4
Rick Chene76b8042017-12-26 13:55:48 +080024#define RELOC_TYPE R_RISCV_32
25#define SYM_INDEX 0x8
26#define SYM_SIZE 0x10
27#else
Lukas Auer7cf43682018-11-22 11:26:24 +010028#define LREG ld
29#define SREG sd
30#define REGBYTES 8
Rick Chene76b8042017-12-26 13:55:48 +080031#define RELOC_TYPE R_RISCV_64
32#define SYM_INDEX 0x20
33#define SYM_SIZE 0x18
34#endif
35
Lukas Auercddde092019-03-17 19:28:40 +010036.section .data
37secondary_harts_relocation_error:
38 .ascii "Relocation of secondary harts has failed, error %d\n"
39
Lukas Auer7cf43682018-11-22 11:26:24 +010040.section .text
Rick Chene76b8042017-12-26 13:55:48 +080041.globl _start
42_start:
Lukas Auer9ebf2942019-03-17 19:28:39 +010043#ifdef CONFIG_RISCV_MMODE
44 csrr a0, mhartid
45#endif
46
Lukas Auer39a652b2018-11-22 11:26:29 +010047 /* save hart id and dtb pointer */
Lukas Auer8de4b3e2019-03-17 19:28:36 +010048 mv tp, a0
Lukas Auer39a652b2018-11-22 11:26:29 +010049 mv s1, a1
50
Lukas Auer7cf43682018-11-22 11:26:24 +010051 la t0, trap_entry
Anup Patel89b39342018-12-03 10:57:40 +053052 csrw MODE_PREFIX(tvec), t0
Lukas Auer8598e6b2018-11-22 11:26:28 +010053
54 /* mask all interrupts */
Anup Patel89b39342018-12-03 10:57:40 +053055 csrw MODE_PREFIX(ie), zero
Rick Chene76b8042017-12-26 13:55:48 +080056
Lukas Auera3596652019-03-17 19:28:37 +010057#ifdef CONFIG_SMP
58 /* check if hart is within range */
59 /* tp: hart id */
60 li t0, CONFIG_NR_CPUS
61 bge tp, t0, hart_out_of_bounds_loop
62#endif
63
64#ifdef CONFIG_SMP
65 /* set xSIE bit to receive IPIs */
66#ifdef CONFIG_RISCV_MMODE
67 li t0, MIE_MSIE
68#else
69 li t0, SIE_SSIE
70#endif
71 csrs MODE_PREFIX(ie), t0
72#endif
73
Rick Chene76b8042017-12-26 13:55:48 +080074/*
Rick Chene76b8042017-12-26 13:55:48 +080075 * Set stackpointer in internal/ex RAM to call board_init_f
76 */
77call_board_init_f:
Lukas Auer7cf43682018-11-22 11:26:24 +010078 li t0, -16
79 li t1, CONFIG_SYS_INIT_SP_ADDR
80 and sp, t1, t0 /* force 16 byte alignment */
Rick Chene76b8042017-12-26 13:55:48 +080081
Rick Chene76b8042017-12-26 13:55:48 +080082call_board_init_f_0:
83 mv a0, sp
84 jal board_init_f_alloc_reserve
Lukas Auera3596652019-03-17 19:28:37 +010085
86 /*
87 * Set global data pointer here for all harts, uninitialized at this
88 * point.
89 */
90 mv gp, a0
91
92 /* setup stack */
93#ifdef CONFIG_SMP
94 /* tp: hart id */
95 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
96 sub sp, a0, t0
97#else
Rick Chene76b8042017-12-26 13:55:48 +080098 mv sp, a0
Lukas Auera3596652019-03-17 19:28:37 +010099#endif
100
Rick Chene5e6c362019-04-30 13:49:33 +0800101#ifndef CONFIG_XIP
Lukas Auera3596652019-03-17 19:28:37 +0100102 /*
103 * Pick hart to initialize global data and run U-Boot. The other harts
104 * wait for initialization to complete.
105 */
106 la t0, hart_lottery
107 li s2, 1
108 amoswap.w s2, t1, 0(t0)
109 bnez s2, wait_for_gd_init
Rick Chene5e6c362019-04-30 13:49:33 +0800110#else
111 bnez tp, secondary_hart_loop
112#endif
Lukas Auer39a652b2018-11-22 11:26:29 +0100113
114 la t0, prior_stage_fdt_address
115 SREG s1, 0(t0)
116
Rick Chene76b8042017-12-26 13:55:48 +0800117 jal board_init_f_init_reserve
118
Bin Meng89681a72018-12-12 06:12:45 -0800119 /* save the boot hart id to global_data */
Lukas Auer8de4b3e2019-03-17 19:28:36 +0100120 SREG tp, GD_BOOT_HART(gp)
Bin Meng89681a72018-12-12 06:12:45 -0800121
Rick Chene5e6c362019-04-30 13:49:33 +0800122#ifndef CONFIG_XIP
Lukas Auera3596652019-03-17 19:28:37 +0100123 la t0, available_harts_lock
124 fence rw, w
125 amoswap.w zero, zero, 0(t0)
126
127wait_for_gd_init:
128 la t0, available_harts_lock
129 li t1, 1
1301: amoswap.w t1, t1, 0(t0)
131 fence r, rw
132 bnez t1, 1b
133
134 /* register available harts in the available_harts mask */
135 li t1, 1
136 sll t1, t1, tp
137 LREG t2, GD_AVAILABLE_HARTS(gp)
138 or t2, t2, t1
139 SREG t2, GD_AVAILABLE_HARTS(gp)
140
141 fence rw, w
142 amoswap.w zero, zero, 0(t0)
143
144 /*
145 * Continue on hart lottery winner, others branch to
146 * secondary_hart_loop.
147 */
148 bnez s2, secondary_hart_loop
Rick Chene5e6c362019-04-30 13:49:33 +0800149#endif
Lukas Auera3596652019-03-17 19:28:37 +0100150
Lukas Auer01558e22019-03-17 19:28:35 +0100151 /* Enable cache */
152 jal icache_enable
153 jal dcache_enable
154
155#ifdef CONFIG_DEBUG_UART
156 jal debug_uart_init
157#endif
158
Lukas Auer7cf43682018-11-22 11:26:24 +0100159 mv a0, zero /* a0 <-- boot_flags = 0 */
160 la t5, board_init_f
161 jr t5 /* jump to board_init_f() */
Rick Chene76b8042017-12-26 13:55:48 +0800162
163/*
164 * void relocate_code (addr_sp, gd, addr_moni)
165 *
166 * This "function" does not return, instead it continues in RAM
167 * after relocating the monitor code.
168 *
169 */
170.globl relocate_code
171relocate_code:
Lukas Auer7cf43682018-11-22 11:26:24 +0100172 mv s2, a0 /* save addr_sp */
173 mv s3, a1 /* save addr of gd */
174 mv s4, a2 /* save addr of destination */
Rick Chene76b8042017-12-26 13:55:48 +0800175
176/*
177 *Set up the stack
178 */
179stack_setup:
Lukas Auera3596652019-03-17 19:28:37 +0100180#ifdef CONFIG_SMP
181 /* tp: hart id */
182 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
183 sub sp, s2, t0
184#else
Lukas Auer7cf43682018-11-22 11:26:24 +0100185 mv sp, s2
Lukas Auera3596652019-03-17 19:28:37 +0100186#endif
187
Lukas Auer7cf43682018-11-22 11:26:24 +0100188 la t0, _start
189 sub t6, s4, t0 /* t6 <- relocation offset */
190 beq t0, s4, clear_bss /* skip relocation */
Rick Chene76b8042017-12-26 13:55:48 +0800191
Lukas Auer7cf43682018-11-22 11:26:24 +0100192 mv t1, s4 /* t1 <- scratch for copy_loop */
193 la t3, __bss_start
194 sub t3, t3, t0 /* t3 <- __bss_start_ofs */
195 add t2, t0, t3 /* t2 <- source end address */
Rick Chene76b8042017-12-26 13:55:48 +0800196
197copy_loop:
Lukas Auer7cf43682018-11-22 11:26:24 +0100198 LREG t5, 0(t0)
199 addi t0, t0, REGBYTES
200 SREG t5, 0(t1)
201 addi t1, t1, REGBYTES
202 blt t0, t2, copy_loop
Rick Chene76b8042017-12-26 13:55:48 +0800203
204/*
205 * Update dynamic relocations after board_init_f
206 */
207fix_rela_dyn:
Lukas Auer7cf43682018-11-22 11:26:24 +0100208 la t1, __rel_dyn_start
209 la t2, __rel_dyn_end
210 beq t1, t2, clear_bss
211 add t1, t1, t6 /* t1 <- rela_dyn_start in RAM */
212 add t2, t2, t6 /* t2 <- rela_dyn_end in RAM */
Rick Chene76b8042017-12-26 13:55:48 +0800213
214/*
215 * skip first reserved entry: address, type, addend
216 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100217 bne t1, t2, 7f
Rick Chene76b8042017-12-26 13:55:48 +0800218
2196:
Lukas Auer7cf43682018-11-22 11:26:24 +0100220 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
221 li t3, R_RISCV_RELATIVE /* reloc type R_RISCV_RELATIVE */
222 bne t5, t3, 8f /* skip non-RISCV_RELOC entries */
223 LREG t3, -(REGBYTES*3)(t1)
224 LREG t5, -(REGBYTES)(t1) /* t5 <-- addend */
225 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
226 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
227 SREG t5, 0(t3)
Rick Chene76b8042017-12-26 13:55:48 +08002287:
Lukas Auer7cf43682018-11-22 11:26:24 +0100229 addi t1, t1, (REGBYTES*3)
230 ble t1, t2, 6b
Rick Chene76b8042017-12-26 13:55:48 +0800231
2328:
Lukas Auer7cf43682018-11-22 11:26:24 +0100233 la t4, __dyn_sym_start
234 add t4, t4, t6
Rick Chene76b8042017-12-26 13:55:48 +0800235
2369:
Lukas Auer7cf43682018-11-22 11:26:24 +0100237 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
238 srli t0, t5, SYM_INDEX /* t0 <--- sym table index */
239 andi t5, t5, 0xFF /* t5 <--- relocation type */
240 li t3, RELOC_TYPE
241 bne t5, t3, 10f /* skip non-addned entries */
Rick Chene76b8042017-12-26 13:55:48 +0800242
Lukas Auer7cf43682018-11-22 11:26:24 +0100243 LREG t3, -(REGBYTES*3)(t1)
244 li t5, SYM_SIZE
245 mul t0, t0, t5
Lukas Auer39a652b2018-11-22 11:26:29 +0100246 add s5, t4, t0
247 LREG t5, REGBYTES(s5)
Lukas Auer7cf43682018-11-22 11:26:24 +0100248 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
249 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
250 SREG t5, 0(t3)
Rick Chene76b8042017-12-26 13:55:48 +080025110:
Lukas Auer7cf43682018-11-22 11:26:24 +0100252 addi t1, t1, (REGBYTES*3)
253 ble t1, t2, 9b
Rick Chene76b8042017-12-26 13:55:48 +0800254
255/*
256 * trap update
257*/
Lukas Auer7cf43682018-11-22 11:26:24 +0100258 la t0, trap_entry
259 add t0, t0, t6
Anup Patel89b39342018-12-03 10:57:40 +0530260 csrw MODE_PREFIX(tvec), t0
Rick Chene76b8042017-12-26 13:55:48 +0800261
262clear_bss:
Lukas Auer7cf43682018-11-22 11:26:24 +0100263 la t0, __bss_start /* t0 <- rel __bss_start in FLASH */
264 add t0, t0, t6 /* t0 <- rel __bss_start in RAM */
265 la t1, __bss_end /* t1 <- rel __bss_end in FLASH */
266 add t1, t1, t6 /* t1 <- rel __bss_end in RAM */
Lukas Auera3596652019-03-17 19:28:37 +0100267 beq t0, t1, relocate_secondary_harts
Rick Chene76b8042017-12-26 13:55:48 +0800268
269clbss_l:
Lukas Auer8598e6b2018-11-22 11:26:28 +0100270 SREG zero, 0(t0) /* clear loop... */
Lukas Auer7cf43682018-11-22 11:26:24 +0100271 addi t0, t0, REGBYTES
272 bne t0, t1, clbss_l
Rick Chene76b8042017-12-26 13:55:48 +0800273
Lukas Auera3596652019-03-17 19:28:37 +0100274relocate_secondary_harts:
275#ifdef CONFIG_SMP
276 /* send relocation IPI */
277 la t0, secondary_hart_relocate
278 add a0, t0, t6
279
280 /* store relocation offset */
281 mv s5, t6
282
283 mv a1, s2
284 mv a2, s3
285 jal smp_call_function
286
Lukas Auercddde092019-03-17 19:28:40 +0100287 /* hang if relocation of secondary harts has failed */
288 beqz a0, 1f
289 mv a1, a0
290 la a0, secondary_harts_relocation_error
291 jal printf
292 jal hang
293
Lukas Auera3596652019-03-17 19:28:37 +0100294 /* restore relocation offset */
Lukas Auercddde092019-03-17 19:28:40 +01002951: mv t6, s5
Lukas Auera3596652019-03-17 19:28:37 +0100296#endif
297
Rick Chene76b8042017-12-26 13:55:48 +0800298/*
299 * We are done. Do not return, instead branch to second part of board
300 * initialization, now running from RAM.
301 */
302call_board_init_r:
Rick Chen842d5802018-11-07 09:34:06 +0800303 jal invalidate_icache_all
304 jal flush_dcache_all
Lukas Auer7cf43682018-11-22 11:26:24 +0100305 la t0, board_init_r
306 mv t4, t0 /* offset of board_init_r() */
307 add t4, t4, t6 /* real address of board_init_r() */
Rick Chene76b8042017-12-26 13:55:48 +0800308/*
309 * setup parameters for board_init_r
310 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100311 mv a0, s3 /* gd_t */
312 mv a1, s4 /* dest_addr */
Rick Chene76b8042017-12-26 13:55:48 +0800313
314/*
315 * jump to it ...
316 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100317 jr t4 /* jump to board_init_r() */
Lukas Auera3596652019-03-17 19:28:37 +0100318
319#ifdef CONFIG_SMP
320hart_out_of_bounds_loop:
321 /* Harts in this loop are out of bounds, increase CONFIG_NR_CPUS. */
322 wfi
323 j hart_out_of_bounds_loop
324#endif
325
326#ifdef CONFIG_SMP
327/* SMP relocation entry */
328secondary_hart_relocate:
329 /* a1: new sp */
330 /* a2: new gd */
331 /* tp: hart id */
332
333 /* setup stack */
334 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
335 sub sp, a1, t0
336
337 /* update global data pointer */
338 mv gp, a2
339#endif
340
341secondary_hart_loop:
342 wfi
343
344#ifdef CONFIG_SMP
345 csrr t0, MODE_PREFIX(ip)
346#ifdef CONFIG_RISCV_MMODE
347 andi t0, t0, MIE_MSIE
348#else
349 andi t0, t0, SIE_SSIE
350#endif
351 beqz t0, secondary_hart_loop
352
353 mv a0, tp
354 jal handle_ipi
355#endif
356
357 j secondary_hart_loop