1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Regents of the University of California
6 #include <asm/asm-offsets.h>
8 #include <linux/init.h>
9 #include <linux/linkage.h>
10 #include <asm/thread_info.h>
13 #include <asm/hwcap.h>
14 #include <asm/image.h>
15 #include "efi-header.S"
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!!
27 * This instruction decodes to "MZ" ASCII required by UEFI.
32 /* jump to start kernel */
38 #ifdef CONFIG_RISCV_M_MODE
39 /* Image load offset (0MB) from start of RAM for M-mode */
42 #if __riscv_xlen == 64
43 /* Image load offset(2MB) from start of RAM */
46 /* Image load offset(4MB) from start of RAM */
50 /* Effective size of kernel image */
53 .word RISCV_HEADER_VERSION
56 .ascii RISCV_IMAGE_MAGIC
58 .ascii RISCV_IMAGE_MAGIC2
60 .word pe_head_start - _start
71 /* Relocate return address */
77 /* Point stvec to virtual address of intruction after satp write */
82 /* Compute satp for kernel page tables, but don't load it yet */
83 srl a2, a0, PAGE_SHIFT
88 * Load trampoline page directory, which will cause us to trap to
89 * stvec if VA != PA, or simply fall through if VA == PA. We need a
90 * full fence here because setup_vm() just wrote these PTEs and we need
91 * to ensure the new translations are in use.
93 la a0, trampoline_pg_dir
94 srl a0, a0, PAGE_SHIFT
100 /* Set trap vector to spin forever to help debug */
101 la a0, .Lsecondary_park
104 /* Reload the global pointer */
107 la gp, __global_pointer$
111 * Switch to kernel page tables. A full fence is necessary in order to
112 * avoid using the trampoline translations, which are only correct for
113 * the first superpage. Fetching the fence is guarnteed to work
114 * because that first superpage is translated the same way.
120 #endif /* CONFIG_MMU */
122 .global secondary_start_sbi
124 /* Mask all interrupts */
128 /* Load the global pointer */
131 la gp, __global_pointer$
135 * Disable FPU to detect illegal usage of
136 * floating point in kernel space
141 /* Set trap vector to spin forever to help debug */
142 la a3, .Lsecondary_park
146 la a4, __cpu_up_stack_pointer
147 la a5, __cpu_up_task_pointer
153 .global secondary_start_common
154 secondary_start_common:
157 /* Enable virtual memory and relocate to virtual address */
158 la a0, swapper_pg_dir
161 call setup_trap_vector
163 #endif /* CONFIG_SMP */
167 /* Set trap vector to exception handler */
168 la a0, handle_exception
172 * Set sup0 scratch register to 0, indicating to exception vector that
173 * we are presently executing in kernel.
175 csrw CSR_SCRATCH, zero
179 /* We lack SMP support or have too many harts, so park this hart */
186 /* Mask all interrupts */
190 #ifdef CONFIG_RISCV_M_MODE
191 /* flush the instruction cache */
194 /* Reset all registers except ra, a0, a1 */
198 * Setup a PMP to permit access to all of memory. Some machines may
199 * not implement PMPs, so we set up a quick trap handler to just skip
200 * touching the PMPs on any trap.
206 csrw CSR_PMPADDR0, a0
207 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
213 * The hartid in a0 is expected later on, and we have no firmware
217 #endif /* CONFIG_RISCV_M_MODE */
219 /* Load the global pointer */
222 la gp, __global_pointer$
226 * Disable FPU to detect illegal usage of
227 * floating point in kernel space
233 li t0, CONFIG_NR_CPUS
234 blt a0, t0, .Lgood_cores
235 tail .Lsecondary_park
239 /* Pick one hart to run the main boot sequence */
242 amoadd.w a3, a2, (a3)
243 bnez a3, .Lsecondary_start
245 /* Clear BSS for flat non-ELF images */
248 ble a4, a3, clear_bss_done
251 add a3, a3, RISCV_SZPTR
252 blt a3, a4, clear_bss
255 /* Save hart ID and DTB physical address */
258 la a2, boot_cpu_hartid
261 /* Initialize page tables and relocate to virtual addresses */
262 la sp, init_thread_union + THREAD_SIZE
268 #endif /* CONFIG_MMU */
270 call setup_trap_vector
271 /* Restore C environment */
273 sw zero, TASK_TI_CPU(tp)
274 la sp, init_thread_union + THREAD_SIZE
277 call kasan_early_init
279 /* Start the kernel */
285 /* Set trap vector to spin forever to help debug */
286 la a3, .Lsecondary_park
290 la a1, __cpu_up_stack_pointer
291 la a2, __cpu_up_task_pointer
296 * This hart didn't win the lottery, so we wait for the winning hart to
297 * get far enough along the boot process that it should continue.
300 /* FIXME: We should WFI to save some energy here. */
303 beqz sp, .Lwait_for_cpu_up
304 beqz tp, .Lwait_for_cpu_up
307 tail secondary_start_common
312 #ifdef CONFIG_RISCV_M_MODE
346 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
347 beqz t0, .Lreset_regs_done
384 /* note that the caller must clear SR_FS */
385 #endif /* CONFIG_FPU */
389 #endif /* CONFIG_RISCV_M_MODE */
392 /* Empty zero page */