1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Regents of the University of California
6 #include <asm/thread_info.h>
7 #include <asm/asm-offsets.h>
9 #include <linux/init.h>
10 #include <linux/linkage.h>
11 #include <asm/thread_info.h>
14 #include <asm/hwcap.h>
15 #include <asm/image.h>
20 * Image header expected by Linux boot-loaders. The image header data
21 * structure is described in asm/image.h.
22 * Do not modify it without modifying the structure and all bootloaders
23 * that expects this header format!!
25 /* jump to start kernel */
30 #if __riscv_xlen == 64
31 /* Image load offset(2MB) from start of RAM */
34 /* Image load offset(4MB) from start of RAM */
37 /* Effective size of kernel image */
40 .word RISCV_HEADER_VERSION
43 .ascii RISCV_IMAGE_MAGIC
45 .ascii RISCV_IMAGE_MAGIC2
51 /* Relocate return address */
57 /* Point stvec to virtual address of intruction after satp write */
62 /* Compute satp for kernel page tables, but don't load it yet */
63 srl a2, a0, PAGE_SHIFT
68 * Load trampoline page directory, which will cause us to trap to
69 * stvec if VA != PA, or simply fall through if VA == PA. We need a
70 * full fence here because setup_vm() just wrote these PTEs and we need
71 * to ensure the new translations are in use.
73 la a0, trampoline_pg_dir
74 srl a0, a0, PAGE_SHIFT
80 /* Set trap vector to spin forever to help debug */
81 la a0, .Lsecondary_park
84 /* Reload the global pointer */
87 la gp, __global_pointer$
91 * Switch to kernel page tables. A full fence is necessary in order to
92 * avoid using the trampoline translations, which are only correct for
93 * the first superpage. Fetching the fence is guarnteed to work
94 * because that first superpage is translated the same way.
100 #endif /* CONFIG_MMU */
102 .global secondary_start_sbi
104 /* Mask all interrupts */
108 /* Load the global pointer */
111 la gp, __global_pointer$
115 * Disable FPU to detect illegal usage of
116 * floating point in kernel space
121 /* Set trap vector to spin forever to help debug */
122 la a3, .Lsecondary_park
126 la a4, __cpu_up_stack_pointer
127 la a5, __cpu_up_task_pointer
133 .global secondary_start_common
134 secondary_start_common:
137 /* Enable virtual memory and relocate to virtual address */
138 la a0, swapper_pg_dir
141 call setup_trap_vector
143 #endif /* CONFIG_SMP */
147 /* Set trap vector to exception handler */
148 la a0, handle_exception
152 * Set sup0 scratch register to 0, indicating to exception vector that
153 * we are presently executing in kernel.
155 csrw CSR_SCRATCH, zero
159 /* We lack SMP support or have too many harts, so park this hart */
167 /* Mask all interrupts */
171 #ifdef CONFIG_RISCV_M_MODE
172 /* flush the instruction cache */
175 /* Reset all registers except ra, a0, a1 */
179 * Setup a PMP to permit access to all of memory. Some machines may
180 * not implement PMPs, so we set up a quick trap handler to just skip
181 * touching the PMPs on any trap.
187 csrw CSR_PMPADDR0, a0
188 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
194 * The hartid in a0 is expected later on, and we have no firmware
198 #endif /* CONFIG_RISCV_M_MODE */
200 /* Load the global pointer */
203 la gp, __global_pointer$
207 * Disable FPU to detect illegal usage of
208 * floating point in kernel space
214 li t0, CONFIG_NR_CPUS
215 blt a0, t0, .Lgood_cores
216 tail .Lsecondary_park
220 /* Pick one hart to run the main boot sequence */
223 amoadd.w a3, a2, (a3)
224 bnez a3, .Lsecondary_start
226 /* Clear BSS for flat non-ELF images */
229 ble a4, a3, clear_bss_done
232 add a3, a3, RISCV_SZPTR
233 blt a3, a4, clear_bss
236 /* Save hart ID and DTB physical address */
239 la a2, boot_cpu_hartid
242 /* Initialize page tables and relocate to virtual addresses */
243 la sp, init_thread_union + THREAD_SIZE
249 #endif /* CONFIG_MMU */
251 call setup_trap_vector
252 /* Restore C environment */
254 sw zero, TASK_TI_CPU(tp)
255 la sp, init_thread_union + THREAD_SIZE
258 call kasan_early_init
260 /* Start the kernel */
267 /* Set trap vector to spin forever to help debug */
268 la a3, .Lsecondary_park
272 la a1, __cpu_up_stack_pointer
273 la a2, __cpu_up_task_pointer
278 * This hart didn't win the lottery, so we wait for the winning hart to
279 * get far enough along the boot process that it should continue.
282 /* FIXME: We should WFI to save some energy here. */
285 beqz sp, .Lwait_for_cpu_up
286 beqz tp, .Lwait_for_cpu_up
289 tail secondary_start_common
294 #ifdef CONFIG_RISCV_M_MODE
328 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
329 beqz t0, .Lreset_regs_done
366 /* note that the caller must clear SR_FS */
367 #endif /* CONFIG_FPU */
371 #endif /* CONFIG_RISCV_M_MODE */
374 /* Empty zero page */