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 */
187 /* Mask all interrupts */
191 #ifdef CONFIG_RISCV_M_MODE
192 /* flush the instruction cache */
195 /* Reset all registers except ra, a0, a1 */
199 * Setup a PMP to permit access to all of memory. Some machines may
200 * not implement PMPs, so we set up a quick trap handler to just skip
201 * touching the PMPs on any trap.
207 csrw CSR_PMPADDR0, a0
208 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
214 * The hartid in a0 is expected later on, and we have no firmware
218 #endif /* CONFIG_RISCV_M_MODE */
220 /* Load the global pointer */
223 la gp, __global_pointer$
227 * Disable FPU to detect illegal usage of
228 * floating point in kernel space
234 li t0, CONFIG_NR_CPUS
235 blt a0, t0, .Lgood_cores
236 tail .Lsecondary_park
240 /* Pick one hart to run the main boot sequence */
243 amoadd.w a3, a2, (a3)
244 bnez a3, .Lsecondary_start
246 /* Clear BSS for flat non-ELF images */
249 ble a4, a3, clear_bss_done
252 add a3, a3, RISCV_SZPTR
253 blt a3, a4, clear_bss
256 /* Save hart ID and DTB physical address */
259 la a2, boot_cpu_hartid
262 /* Initialize page tables and relocate to virtual addresses */
263 la sp, init_thread_union + THREAD_SIZE
269 #endif /* CONFIG_MMU */
271 call setup_trap_vector
272 /* Restore C environment */
274 sw zero, TASK_TI_CPU(tp)
275 la sp, init_thread_union + THREAD_SIZE
278 call kasan_early_init
280 /* Start the kernel */
286 /* Set trap vector to spin forever to help debug */
287 la a3, .Lsecondary_park
291 la a1, __cpu_up_stack_pointer
292 la a2, __cpu_up_task_pointer
297 * This hart didn't win the lottery, so we wait for the winning hart to
298 * get far enough along the boot process that it should continue.
301 /* FIXME: We should WFI to save some energy here. */
304 beqz sp, .Lwait_for_cpu_up
305 beqz tp, .Lwait_for_cpu_up
308 tail secondary_start_common
313 #ifdef CONFIG_RISCV_M_MODE
347 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
348 beqz t0, .Lreset_regs_done
385 /* note that the caller must clear SR_FS */
386 #endif /* CONFIG_FPU */
390 #endif /* CONFIG_RISCV_M_MODE */
393 /* Empty zero page */