1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Startup Code for RISC-V Core
5 * Copyright (c) 2017 Microsemi Corporation.
8 * Copyright (C) 2017 Andes Technology Corporation
12 #include <asm-offsets.h>
17 #include <asm/encoding.h>
18 #include <generated/asm-offsets.h>
24 #define RELOC_TYPE R_RISCV_32
31 #define RELOC_TYPE R_RISCV_64
32 #define SYM_INDEX 0x20
37 secondary_harts_relocation_error:
38 .ascii "Relocation of secondary harts has failed, error %d\n"
43 #ifdef CONFIG_RISCV_MMODE
47 /* save hart id and dtb pointer */
52 csrw MODE_PREFIX(tvec), t0
54 /* mask all interrupts */
55 csrw MODE_PREFIX(ie), zero
58 /* check if hart is within range */
61 bge tp, t0, hart_out_of_bounds_loop
65 /* set xSIE bit to receive IPIs */
66 #ifdef CONFIG_RISCV_MMODE
71 csrs MODE_PREFIX(ie), t0
75 * Set stackpointer in internal/ex RAM to call board_init_f
79 li t1, CONFIG_SYS_INIT_SP_ADDR
80 and sp, t1, t0 /* force 16 byte alignment */
84 jal board_init_f_alloc_reserve
87 * Set global data pointer here for all harts, uninitialized at this
95 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
102 * Pick hart to initialize global data and run U-Boot. The other harts
103 * wait for initialization to complete.
107 amoswap.w s2, t1, 0(t0)
108 bnez s2, wait_for_gd_init
110 la t0, prior_stage_fdt_address
113 jal board_init_f_init_reserve
115 /* save the boot hart id to global_data */
116 SREG tp, GD_BOOT_HART(gp)
118 la t0, available_harts_lock
120 amoswap.w zero, zero, 0(t0)
123 la t0, available_harts_lock
125 1: amoswap.w t1, t1, 0(t0)
129 /* register available harts in the available_harts mask */
132 LREG t2, GD_AVAILABLE_HARTS(gp)
134 SREG t2, GD_AVAILABLE_HARTS(gp)
137 amoswap.w zero, zero, 0(t0)
140 * Continue on hart lottery winner, others branch to
141 * secondary_hart_loop.
143 bnez s2, secondary_hart_loop
149 #ifdef CONFIG_DEBUG_UART
153 mv a0, zero /* a0 <-- boot_flags = 0 */
155 jr t5 /* jump to board_init_f() */
158 * void relocate_code (addr_sp, gd, addr_moni)
160 * This "function" does not return, instead it continues in RAM
161 * after relocating the monitor code.
166 mv s2, a0 /* save addr_sp */
167 mv s3, a1 /* save addr of gd */
168 mv s4, a2 /* save addr of destination */
176 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
183 sub t6, s4, t0 /* t6 <- relocation offset */
184 beq t0, s4, clear_bss /* skip relocation */
186 mv t1, s4 /* t1 <- scratch for copy_loop */
188 sub t3, t3, t0 /* t3 <- __bss_start_ofs */
189 add t2, t0, t3 /* t2 <- source end address */
193 addi t0, t0, REGBYTES
195 addi t1, t1, REGBYTES
196 blt t0, t2, copy_loop
199 * Update dynamic relocations after board_init_f
202 la t1, __rel_dyn_start
204 beq t1, t2, clear_bss
205 add t1, t1, t6 /* t1 <- rela_dyn_start in RAM */
206 add t2, t2, t6 /* t2 <- rela_dyn_end in RAM */
209 * skip first reserved entry: address, type, addend
214 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
215 li t3, R_RISCV_RELATIVE /* reloc type R_RISCV_RELATIVE */
216 bne t5, t3, 8f /* skip non-RISCV_RELOC entries */
217 LREG t3, -(REGBYTES*3)(t1)
218 LREG t5, -(REGBYTES)(t1) /* t5 <-- addend */
219 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
220 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
223 addi t1, t1, (REGBYTES*3)
227 la t4, __dyn_sym_start
231 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
232 srli t0, t5, SYM_INDEX /* t0 <--- sym table index */
233 andi t5, t5, 0xFF /* t5 <--- relocation type */
235 bne t5, t3, 10f /* skip non-addned entries */
237 LREG t3, -(REGBYTES*3)(t1)
241 LREG t5, REGBYTES(s5)
242 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
243 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
246 addi t1, t1, (REGBYTES*3)
254 csrw MODE_PREFIX(tvec), t0
257 la t0, __bss_start /* t0 <- rel __bss_start in FLASH */
258 add t0, t0, t6 /* t0 <- rel __bss_start in RAM */
259 la t1, __bss_end /* t1 <- rel __bss_end in FLASH */
260 add t1, t1, t6 /* t1 <- rel __bss_end in RAM */
261 beq t0, t1, relocate_secondary_harts
264 SREG zero, 0(t0) /* clear loop... */
265 addi t0, t0, REGBYTES
268 relocate_secondary_harts:
270 /* send relocation IPI */
271 la t0, secondary_hart_relocate
274 /* store relocation offset */
279 jal smp_call_function
281 /* hang if relocation of secondary harts has failed */
284 la a0, secondary_harts_relocation_error
288 /* restore relocation offset */
293 * We are done. Do not return, instead branch to second part of board
294 * initialization, now running from RAM.
297 jal invalidate_icache_all
300 mv t4, t0 /* offset of board_init_r() */
301 add t4, t4, t6 /* real address of board_init_r() */
303 * setup parameters for board_init_r
306 mv a1, s4 /* dest_addr */
311 jr t4 /* jump to board_init_r() */
314 hart_out_of_bounds_loop:
315 /* Harts in this loop are out of bounds, increase CONFIG_NR_CPUS. */
317 j hart_out_of_bounds_loop
321 /* SMP relocation entry */
322 secondary_hart_relocate:
328 slli t0, tp, CONFIG_STACK_SIZE_SHIFT
331 /* update global data pointer */
339 csrr t0, MODE_PREFIX(ip)
340 #ifdef CONFIG_RISCV_MMODE
341 andi t0, t0, MIE_MSIE
343 andi t0, t0, SIE_SSIE
345 beqz t0, secondary_hart_loop
351 j secondary_hart_loop