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>
12 #include <asm/pgtable.h>
14 #include <asm/hwcap.h>
15 #include <asm/image.h>
16 #include "efi-header.S"
18 #ifdef CONFIG_XIP_KERNEL
19 .macro XIP_FIXUP_OFFSET reg
23 _xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET
25 .macro XIP_FIXUP_OFFSET reg
27 #endif /* CONFIG_XIP_KERNEL */
32 * Image header expected by Linux boot-loaders. The image header data
33 * structure is described in asm/image.h.
34 * Do not modify it without modifying the structure and all bootloaders
35 * that expects this header format!!
39 * This instruction decodes to "MZ" ASCII required by UEFI.
44 /* jump to start kernel */
50 #ifdef CONFIG_RISCV_M_MODE
51 /* Image load offset (0MB) from start of RAM for M-mode */
54 #if __riscv_xlen == 64
55 /* Image load offset(2MB) from start of RAM */
58 /* Image load offset(4MB) from start of RAM */
62 /* Effective size of kernel image */
65 .word RISCV_HEADER_VERSION
68 .ascii RISCV_IMAGE_MAGIC
70 .ascii RISCV_IMAGE_MAGIC2
72 .word pe_head_start - _start
83 /* Relocate return address */
84 la a1, kernel_virt_addr
91 /* Point stvec to virtual address of intruction after satp write */
96 /* Compute satp for kernel page tables, but don't load it yet */
97 srl a2, a0, PAGE_SHIFT
102 * Load trampoline page directory, which will cause us to trap to
103 * stvec if VA != PA, or simply fall through if VA == PA. We need a
104 * full fence here because setup_vm() just wrote these PTEs and we need
105 * to ensure the new translations are in use.
107 la a0, trampoline_pg_dir
109 srl a0, a0, PAGE_SHIFT
115 /* Set trap vector to spin forever to help debug */
116 la a0, .Lsecondary_park
119 /* Reload the global pointer */
122 la gp, __global_pointer$
126 * Switch to kernel page tables. A full fence is necessary in order to
127 * avoid using the trampoline translations, which are only correct for
128 * the first superpage. Fetching the fence is guarnteed to work
129 * because that first superpage is translated the same way.
135 #endif /* CONFIG_MMU */
137 .global secondary_start_sbi
139 /* Mask all interrupts */
143 /* Load the global pointer */
146 la gp, __global_pointer$
150 * Disable FPU to detect illegal usage of
151 * floating point in kernel space
156 /* Set trap vector to spin forever to help debug */
157 la a3, .Lsecondary_park
161 la a4, __cpu_up_stack_pointer
163 la a5, __cpu_up_task_pointer
170 .global secondary_start_common
171 secondary_start_common:
174 /* Enable virtual memory and relocate to virtual address */
175 la a0, swapper_pg_dir
179 call setup_trap_vector
181 #endif /* CONFIG_SMP */
185 /* Set trap vector to exception handler */
186 la a0, handle_exception
190 * Set sup0 scratch register to 0, indicating to exception vector that
191 * we are presently executing in kernel.
193 csrw CSR_SCRATCH, zero
197 /* We lack SMP support or have too many harts, so park this hart */
204 /* Mask all interrupts */
208 #ifdef CONFIG_RISCV_M_MODE
209 /* flush the instruction cache */
212 /* Reset all registers except ra, a0, a1 */
216 * Setup a PMP to permit access to all of memory. Some machines may
217 * not implement PMPs, so we set up a quick trap handler to just skip
218 * touching the PMPs on any trap.
224 csrw CSR_PMPADDR0, a0
225 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
231 * The hartid in a0 is expected later on, and we have no firmware
235 #endif /* CONFIG_RISCV_M_MODE */
237 /* Load the global pointer */
240 la gp, __global_pointer$
244 * Disable FPU to detect illegal usage of
245 * floating point in kernel space
251 li t0, CONFIG_NR_CPUS
252 blt a0, t0, .Lgood_cores
253 tail .Lsecondary_park
257 #ifndef CONFIG_XIP_KERNEL
258 /* Pick one hart to run the main boot sequence */
261 amoadd.w a3, a2, (a3)
262 bnez a3, .Lsecondary_start
265 /* hart_lottery in flash contains a magic number */
270 amoswap.w t0, t1, (a2)
271 /* first time here if hart_lottery in RAM is not set */
272 beq t0, t1, .Lsecondary_start
274 la sp, _end + THREAD_SIZE
279 /* Restore a0 copy */
283 #ifndef CONFIG_XIP_KERNEL
284 /* Clear BSS for flat non-ELF images */
287 ble a4, a3, clear_bss_done
290 add a3, a3, RISCV_SZPTR
291 blt a3, a4, clear_bss
294 /* Save hart ID and DTB physical address */
298 la a2, boot_cpu_hartid
302 /* Initialize page tables and relocate to virtual addresses */
303 la sp, init_thread_union + THREAD_SIZE
305 #ifdef CONFIG_BUILTIN_DTB
309 #endif /* CONFIG_BUILTIN_DTB */
315 #endif /* CONFIG_MMU */
317 call setup_trap_vector
318 /* Restore C environment */
320 sw zero, TASK_TI_CPU(tp)
321 la sp, init_thread_union + THREAD_SIZE
324 call kasan_early_init
326 /* Start the kernel */
332 /* Set trap vector to spin forever to help debug */
333 la a3, .Lsecondary_park
337 la a1, __cpu_up_stack_pointer
339 la a2, __cpu_up_task_pointer
345 * This hart didn't win the lottery, so we wait for the winning hart to
346 * get far enough along the boot process that it should continue.
349 /* FIXME: We should WFI to save some energy here. */
352 beqz sp, .Lwait_for_cpu_up
353 beqz tp, .Lwait_for_cpu_up
356 tail secondary_start_common
361 #ifdef CONFIG_RISCV_M_MODE
395 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
396 beqz t0, .Lreset_regs_done
433 /* note that the caller must clear SR_FS */
434 #endif /* CONFIG_FPU */
438 #endif /* CONFIG_RISCV_M_MODE */
441 /* Empty zero page */