Thomas Gleixner | 50acfb2 | 2019-05-29 07:18:00 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Regents of the University of California |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 6 | #include <asm/asm-offsets.h> |
| 7 | #include <asm/asm.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/linkage.h> |
| 10 | #include <asm/thread_info.h> |
| 11 | #include <asm/page.h> |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 12 | #include <asm/pgtable.h> |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 13 | #include <asm/csr.h> |
Atish Patra | 9a2451f | 2022-01-20 01:09:13 -0800 | [diff] [blame] | 14 | #include <asm/cpu_ops_sbi.h> |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 15 | #include <asm/hwcap.h> |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 16 | #include <asm/image.h> |
Atish Patra | cb7d2dd | 2020-09-17 15:37:13 -0700 | [diff] [blame] | 17 | #include "efi-header.S" |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 18 | |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 19 | #ifdef CONFIG_XIP_KERNEL |
| 20 | .macro XIP_FIXUP_OFFSET reg |
| 21 | REG_L t0, _xip_fixup |
| 22 | add \reg, \reg, t0 |
| 23 | .endm |
Vitaly Wool | f9ace4e | 2021-10-11 11:14:14 +0200 | [diff] [blame] | 24 | .macro XIP_FIXUP_FLASH_OFFSET reg |
| 25 | la t1, __data_loc |
| 26 | li t0, XIP_OFFSET_MASK |
| 27 | and t1, t1, t0 |
| 28 | li t1, XIP_OFFSET |
| 29 | sub t0, t0, t1 |
| 30 | sub \reg, \reg, t0 |
| 31 | .endm |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 32 | _xip_fixup: .dword CONFIG_PHYS_RAM_BASE - CONFIG_XIP_PHYS_ADDR - XIP_OFFSET |
| 33 | #else |
| 34 | .macro XIP_FIXUP_OFFSET reg |
| 35 | .endm |
Vitaly Wool | f9ace4e | 2021-10-11 11:14:14 +0200 | [diff] [blame] | 36 | .macro XIP_FIXUP_FLASH_OFFSET reg |
| 37 | .endm |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 38 | #endif /* CONFIG_XIP_KERNEL */ |
| 39 | |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 40 | __HEAD |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 41 | ENTRY(_start) |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 42 | /* |
| 43 | * Image header expected by Linux boot-loaders. The image header data |
| 44 | * structure is described in asm/image.h. |
| 45 | * Do not modify it without modifying the structure and all bootloaders |
| 46 | * that expects this header format!! |
| 47 | */ |
Atish Patra | cb7d2dd | 2020-09-17 15:37:13 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_EFI |
| 49 | /* |
| 50 | * This instruction decodes to "MZ" ASCII required by UEFI. |
| 51 | */ |
| 52 | c.li s4,-13 |
| 53 | j _start_kernel |
| 54 | #else |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 55 | /* jump to start kernel */ |
| 56 | j _start_kernel |
| 57 | /* reserved */ |
| 58 | .word 0 |
Atish Patra | cb7d2dd | 2020-09-17 15:37:13 -0700 | [diff] [blame] | 59 | #endif |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 60 | .balign 8 |
Sean Anderson | 79605f1 | 2020-10-22 16:30:12 -0400 | [diff] [blame] | 61 | #ifdef CONFIG_RISCV_M_MODE |
| 62 | /* Image load offset (0MB) from start of RAM for M-mode */ |
| 63 | .dword 0 |
| 64 | #else |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 65 | #if __riscv_xlen == 64 |
| 66 | /* Image load offset(2MB) from start of RAM */ |
| 67 | .dword 0x200000 |
| 68 | #else |
| 69 | /* Image load offset(4MB) from start of RAM */ |
| 70 | .dword 0x400000 |
| 71 | #endif |
Sean Anderson | 79605f1 | 2020-10-22 16:30:12 -0400 | [diff] [blame] | 72 | #endif |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 73 | /* Effective size of kernel image */ |
| 74 | .dword _end - _start |
| 75 | .dword __HEAD_FLAGS |
| 76 | .word RISCV_HEADER_VERSION |
| 77 | .word 0 |
| 78 | .dword 0 |
Paul Walmsley | 474efec | 2019-09-13 18:35:50 -0700 | [diff] [blame] | 79 | .ascii RISCV_IMAGE_MAGIC |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 80 | .balign 4 |
Paul Walmsley | 474efec | 2019-09-13 18:35:50 -0700 | [diff] [blame] | 81 | .ascii RISCV_IMAGE_MAGIC2 |
Atish Patra | cb7d2dd | 2020-09-17 15:37:13 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_EFI |
| 83 | .word pe_head_start - _start |
| 84 | pe_head_start: |
| 85 | |
| 86 | __EFI_PE_HEADER |
| 87 | #else |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 88 | .word 0 |
Atish Patra | cb7d2dd | 2020-09-17 15:37:13 -0700 | [diff] [blame] | 89 | #endif |
Atish Patra | 0f327f2 | 2019-06-06 16:08:00 -0700 | [diff] [blame] | 90 | |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 91 | .align 2 |
| 92 | #ifdef CONFIG_MMU |
| 93 | relocate: |
| 94 | /* Relocate return address */ |
Alexandre Ghiti | 658e2c5 | 2021-06-17 15:53:07 +0200 | [diff] [blame] | 95 | la a1, kernel_map |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 96 | XIP_FIXUP_OFFSET a1 |
Alexandre Ghiti | 658e2c5 | 2021-06-17 15:53:07 +0200 | [diff] [blame] | 97 | REG_L a1, KERNEL_MAP_VIRT_ADDR(a1) |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 98 | la a2, _start |
| 99 | sub a1, a1, a2 |
| 100 | add ra, ra, a1 |
| 101 | |
| 102 | /* Point stvec to virtual address of intruction after satp write */ |
| 103 | la a2, 1f |
| 104 | add a2, a2, a1 |
| 105 | csrw CSR_TVEC, a2 |
| 106 | |
| 107 | /* Compute satp for kernel page tables, but don't load it yet */ |
| 108 | srl a2, a0, PAGE_SHIFT |
Alexandre Ghiti | e8a62cc | 2021-12-06 11:46:51 +0100 | [diff] [blame] | 109 | la a1, satp_mode |
| 110 | REG_L a1, 0(a1) |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 111 | or a2, a2, a1 |
| 112 | |
| 113 | /* |
| 114 | * Load trampoline page directory, which will cause us to trap to |
| 115 | * stvec if VA != PA, or simply fall through if VA == PA. We need a |
| 116 | * full fence here because setup_vm() just wrote these PTEs and we need |
| 117 | * to ensure the new translations are in use. |
| 118 | */ |
| 119 | la a0, trampoline_pg_dir |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 120 | XIP_FIXUP_OFFSET a0 |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 121 | srl a0, a0, PAGE_SHIFT |
| 122 | or a0, a0, a1 |
| 123 | sfence.vma |
| 124 | csrw CSR_SATP, a0 |
| 125 | .align 2 |
| 126 | 1: |
Qiu Wenbo | 76d4467 | 2020-08-13 11:38:04 +0800 | [diff] [blame] | 127 | /* Set trap vector to spin forever to help debug */ |
| 128 | la a0, .Lsecondary_park |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 129 | csrw CSR_TVEC, a0 |
| 130 | |
| 131 | /* Reload the global pointer */ |
| 132 | .option push |
| 133 | .option norelax |
| 134 | la gp, __global_pointer$ |
| 135 | .option pop |
| 136 | |
| 137 | /* |
| 138 | * Switch to kernel page tables. A full fence is necessary in order to |
| 139 | * avoid using the trampoline translations, which are only correct for |
hasheddan | 8ee3043 | 2021-11-20 09:26:05 -0500 | [diff] [blame] | 140 | * the first superpage. Fetching the fence is guaranteed to work |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 141 | * because that first superpage is translated the same way. |
| 142 | */ |
| 143 | csrw CSR_SATP, a2 |
| 144 | sfence.vma |
| 145 | |
| 146 | ret |
| 147 | #endif /* CONFIG_MMU */ |
| 148 | #ifdef CONFIG_SMP |
Atish Patra | cfafe26 | 2020-03-17 18:11:43 -0700 | [diff] [blame] | 149 | .global secondary_start_sbi |
| 150 | secondary_start_sbi: |
| 151 | /* Mask all interrupts */ |
| 152 | csrw CSR_IE, zero |
| 153 | csrw CSR_IP, zero |
| 154 | |
| 155 | /* Load the global pointer */ |
| 156 | .option push |
| 157 | .option norelax |
| 158 | la gp, __global_pointer$ |
| 159 | .option pop |
| 160 | |
| 161 | /* |
| 162 | * Disable FPU to detect illegal usage of |
| 163 | * floating point in kernel space |
| 164 | */ |
| 165 | li t0, SR_FS |
| 166 | csrc CSR_STATUS, t0 |
| 167 | |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 168 | /* Set trap vector to spin forever to help debug */ |
| 169 | la a3, .Lsecondary_park |
| 170 | csrw CSR_TVEC, a3 |
| 171 | |
Atish Patra | 9a2451f | 2022-01-20 01:09:13 -0800 | [diff] [blame] | 172 | /* a0 contains the hartid & a1 contains boot data */ |
| 173 | li a2, SBI_HART_BOOT_TASK_PTR_OFFSET |
| 174 | XIP_FIXUP_OFFSET a2 |
| 175 | add a2, a2, a1 |
| 176 | REG_L tp, (a2) |
| 177 | li a3, SBI_HART_BOOT_STACK_PTR_OFFSET |
| 178 | XIP_FIXUP_OFFSET a3 |
| 179 | add a3, a3, a1 |
| 180 | REG_L sp, (a3) |
Atish Patra | cfafe26 | 2020-03-17 18:11:43 -0700 | [diff] [blame] | 181 | |
Jisheng Zhang | 153c46f | 2021-11-29 00:07:38 +0800 | [diff] [blame] | 182 | .Lsecondary_start_common: |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 183 | |
| 184 | #ifdef CONFIG_MMU |
| 185 | /* Enable virtual memory and relocate to virtual address */ |
| 186 | la a0, swapper_pg_dir |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 187 | XIP_FIXUP_OFFSET a0 |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 188 | call relocate |
| 189 | #endif |
Qiu Wenbo | 76d4467 | 2020-08-13 11:38:04 +0800 | [diff] [blame] | 190 | call setup_trap_vector |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 191 | tail smp_callin |
| 192 | #endif /* CONFIG_SMP */ |
| 193 | |
Qiu Wenbo | 76d4467 | 2020-08-13 11:38:04 +0800 | [diff] [blame] | 194 | .align 2 |
| 195 | setup_trap_vector: |
| 196 | /* Set trap vector to exception handler */ |
| 197 | la a0, handle_exception |
| 198 | csrw CSR_TVEC, a0 |
| 199 | |
| 200 | /* |
| 201 | * Set sup0 scratch register to 0, indicating to exception vector that |
| 202 | * we are presently executing in kernel. |
| 203 | */ |
| 204 | csrw CSR_SCRATCH, zero |
| 205 | ret |
| 206 | |
Chen Lu | 64a1959 | 2021-10-18 13:22:38 +0800 | [diff] [blame] | 207 | .align 2 |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 208 | .Lsecondary_park: |
| 209 | /* We lack SMP support or have too many harts, so park this hart */ |
| 210 | wfi |
| 211 | j .Lsecondary_park |
| 212 | |
| 213 | END(_start) |
| 214 | |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 215 | ENTRY(_start_kernel) |
Anup Patel | a3182c9 | 2019-04-25 08:38:41 +0000 | [diff] [blame] | 216 | /* Mask all interrupts */ |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 217 | csrw CSR_IE, zero |
| 218 | csrw CSR_IP, zero |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 219 | |
Damien Le Moal | accb9db | 2019-10-28 13:10:39 +0100 | [diff] [blame] | 220 | #ifdef CONFIG_RISCV_M_MODE |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 221 | /* flush the instruction cache */ |
| 222 | fence.i |
| 223 | |
| 224 | /* Reset all registers except ra, a0, a1 */ |
| 225 | call reset_regs |
| 226 | |
Palmer Dabbelt | eb077c9 | 2020-04-07 11:33:40 -0700 | [diff] [blame] | 227 | /* |
| 228 | * Setup a PMP to permit access to all of memory. Some machines may |
| 229 | * not implement PMPs, so we set up a quick trap handler to just skip |
| 230 | * touching the PMPs on any trap. |
| 231 | */ |
| 232 | la a0, pmp_done |
| 233 | csrw CSR_TVEC, a0 |
| 234 | |
Greentime Hu | c68a903 | 2020-01-09 11:17:40 +0800 | [diff] [blame] | 235 | li a0, -1 |
| 236 | csrw CSR_PMPADDR0, a0 |
| 237 | li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X) |
| 238 | csrw CSR_PMPCFG0, a0 |
Palmer Dabbelt | eb077c9 | 2020-04-07 11:33:40 -0700 | [diff] [blame] | 239 | .align 2 |
| 240 | pmp_done: |
Greentime Hu | c68a903 | 2020-01-09 11:17:40 +0800 | [diff] [blame] | 241 | |
Damien Le Moal | accb9db | 2019-10-28 13:10:39 +0100 | [diff] [blame] | 242 | /* |
| 243 | * The hartid in a0 is expected later on, and we have no firmware |
| 244 | * to hand it to us. |
| 245 | */ |
| 246 | csrr a0, CSR_MHARTID |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 247 | #endif /* CONFIG_RISCV_M_MODE */ |
Damien Le Moal | accb9db | 2019-10-28 13:10:39 +0100 | [diff] [blame] | 248 | |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 249 | /* Load the global pointer */ |
| 250 | .option push |
| 251 | .option norelax |
| 252 | la gp, __global_pointer$ |
| 253 | .option pop |
| 254 | |
| 255 | /* |
| 256 | * Disable FPU to detect illegal usage of |
| 257 | * floating point in kernel space |
| 258 | */ |
| 259 | li t0, SR_FS |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 260 | csrc CSR_STATUS, t0 |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 261 | |
Atish Patra | 2ffc48f | 2022-01-20 01:09:17 -0800 | [diff] [blame] | 262 | #ifdef CONFIG_RISCV_BOOT_SPINWAIT |
Xiang Wang | b47613d | 2019-09-06 11:56:09 +0800 | [diff] [blame] | 263 | li t0, CONFIG_NR_CPUS |
Greentime Hu | 20d2292 | 2020-01-15 14:54:36 +0800 | [diff] [blame] | 264 | blt a0, t0, .Lgood_cores |
| 265 | tail .Lsecondary_park |
| 266 | .Lgood_cores: |
Xiang Wang | b47613d | 2019-09-06 11:56:09 +0800 | [diff] [blame] | 267 | |
Atish Patra | 0b39eb3 | 2022-01-20 01:09:16 -0800 | [diff] [blame] | 268 | /* The lottery system is only required for spinwait booting method */ |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 269 | #ifndef CONFIG_XIP_KERNEL |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 270 | /* Pick one hart to run the main boot sequence */ |
| 271 | la a3, hart_lottery |
| 272 | li a2, 1 |
| 273 | amoadd.w a3, a2, (a3) |
| 274 | bnez a3, .Lsecondary_start |
| 275 | |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 276 | #else |
| 277 | /* hart_lottery in flash contains a magic number */ |
| 278 | la a3, hart_lottery |
| 279 | mv a2, a3 |
| 280 | XIP_FIXUP_OFFSET a2 |
Vitaly Wool | f9ace4e | 2021-10-11 11:14:14 +0200 | [diff] [blame] | 281 | XIP_FIXUP_FLASH_OFFSET a3 |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 282 | lw t1, (a3) |
| 283 | amoswap.w t0, t1, (a2) |
| 284 | /* first time here if hart_lottery in RAM is not set */ |
| 285 | beq t0, t1, .Lsecondary_start |
| 286 | |
Atish Patra | 0b39eb3 | 2022-01-20 01:09:16 -0800 | [diff] [blame] | 287 | #endif /* CONFIG_XIP */ |
Atish Patra | 2ffc48f | 2022-01-20 01:09:17 -0800 | [diff] [blame] | 288 | #endif /* CONFIG_RISCV_BOOT_SPINWAIT */ |
Atish Patra | 0b39eb3 | 2022-01-20 01:09:16 -0800 | [diff] [blame] | 289 | |
| 290 | #ifdef CONFIG_XIP_KERNEL |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 291 | la sp, _end + THREAD_SIZE |
| 292 | XIP_FIXUP_OFFSET sp |
| 293 | mv s0, a0 |
| 294 | call __copy_data |
| 295 | |
| 296 | /* Restore a0 copy */ |
| 297 | mv a0, s0 |
| 298 | #endif |
| 299 | |
| 300 | #ifndef CONFIG_XIP_KERNEL |
Anup Patel | c0fbcd9 | 2018-11-12 11:25:15 +0530 | [diff] [blame] | 301 | /* Clear BSS for flat non-ELF images */ |
| 302 | la a3, __bss_start |
| 303 | la a4, __bss_stop |
| 304 | ble a4, a3, clear_bss_done |
| 305 | clear_bss: |
| 306 | REG_S zero, (a3) |
| 307 | add a3, a3, RISCV_SZPTR |
| 308 | blt a3, a4, clear_bss |
| 309 | clear_bss_done: |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 310 | #endif |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 311 | /* Save hart ID and DTB physical address */ |
| 312 | mv s0, a0 |
| 313 | mv s1, a1 |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 314 | |
Atish Patra | f99fb60 | 2018-10-02 12:15:05 -0700 | [diff] [blame] | 315 | la a2, boot_cpu_hartid |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 316 | XIP_FIXUP_OFFSET a2 |
Atish Patra | f99fb60 | 2018-10-02 12:15:05 -0700 | [diff] [blame] | 317 | REG_S a0, (a2) |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 318 | |
| 319 | /* Initialize page tables and relocate to virtual addresses */ |
| 320 | la sp, init_thread_union + THREAD_SIZE |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 321 | XIP_FIXUP_OFFSET sp |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 322 | #ifdef CONFIG_BUILTIN_DTB |
| 323 | la a0, __dtb_start |
Vitaly Wool | f9ace4e | 2021-10-11 11:14:14 +0200 | [diff] [blame] | 324 | XIP_FIXUP_OFFSET a0 |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 325 | #else |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 326 | mv a0, s1 |
Vitaly Wool | f105aa9 | 2021-01-16 01:49:48 +0200 | [diff] [blame] | 327 | #endif /* CONFIG_BUILTIN_DTB */ |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 328 | call setup_vm |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 329 | #ifdef CONFIG_MMU |
Anup Patel | 671f9a3 | 2019-06-28 13:36:21 -0700 | [diff] [blame] | 330 | la a0, early_pg_dir |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 331 | XIP_FIXUP_OFFSET a0 |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 332 | call relocate |
Christoph Hellwig | 6bd33e1 | 2019-10-28 13:10:41 +0100 | [diff] [blame] | 333 | #endif /* CONFIG_MMU */ |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 334 | |
Qiu Wenbo | 76d4467 | 2020-08-13 11:38:04 +0800 | [diff] [blame] | 335 | call setup_trap_vector |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 336 | /* Restore C environment */ |
| 337 | la tp, init_task |
Christoph Hellwig | c637b91 | 2019-04-15 11:14:37 +0200 | [diff] [blame] | 338 | la sp, init_thread_union + THREAD_SIZE |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 339 | |
Nick Hu | 8ad8b72 | 2020-01-06 10:38:32 -0800 | [diff] [blame] | 340 | #ifdef CONFIG_KASAN |
| 341 | call kasan_early_init |
| 342 | #endif |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 343 | /* Start the kernel */ |
Damien Le Moal | 335b139 | 2020-03-16 09:47:38 +0900 | [diff] [blame] | 344 | call soc_early_init |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 345 | tail start_kernel |
| 346 | |
Atish Patra | 2ffc48f | 2022-01-20 01:09:17 -0800 | [diff] [blame] | 347 | #if CONFIG_RISCV_BOOT_SPINWAIT |
Atish Patra | 0b39eb3 | 2022-01-20 01:09:16 -0800 | [diff] [blame] | 348 | .Lsecondary_start: |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 349 | /* Set trap vector to spin forever to help debug */ |
| 350 | la a3, .Lsecondary_park |
Christoph Hellwig | a4c3733 | 2019-10-28 13:10:32 +0100 | [diff] [blame] | 351 | csrw CSR_TVEC, a3 |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 352 | |
| 353 | slli a3, a0, LGREG |
Atish Patra | c78f94f | 2022-01-20 01:09:15 -0800 | [diff] [blame] | 354 | la a1, __cpu_spinwait_stack_pointer |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 355 | XIP_FIXUP_OFFSET a1 |
Atish Patra | c78f94f | 2022-01-20 01:09:15 -0800 | [diff] [blame] | 356 | la a2, __cpu_spinwait_task_pointer |
Vitaly Wool | 44c9225 | 2021-04-13 02:35:14 -0400 | [diff] [blame] | 357 | XIP_FIXUP_OFFSET a2 |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 358 | add a1, a3, a1 |
| 359 | add a2, a3, a2 |
| 360 | |
| 361 | /* |
| 362 | * This hart didn't win the lottery, so we wait for the winning hart to |
| 363 | * get far enough along the boot process that it should continue. |
| 364 | */ |
| 365 | .Lwait_for_cpu_up: |
| 366 | /* FIXME: We should WFI to save some energy here. */ |
| 367 | REG_L sp, (a1) |
| 368 | REG_L tp, (a2) |
| 369 | beqz sp, .Lwait_for_cpu_up |
| 370 | beqz tp, .Lwait_for_cpu_up |
| 371 | fence |
| 372 | |
Jisheng Zhang | 153c46f | 2021-11-29 00:07:38 +0800 | [diff] [blame] | 373 | tail .Lsecondary_start_common |
Atish Patra | 2ffc48f | 2022-01-20 01:09:17 -0800 | [diff] [blame] | 374 | #endif /* CONFIG_RISCV_BOOT_SPINWAIT */ |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 375 | |
Atish Patra | e011995 | 2020-03-17 18:11:39 -0700 | [diff] [blame] | 376 | END(_start_kernel) |
Palmer Dabbelt | 76d2a04 | 2017-07-10 18:00:26 -0700 | [diff] [blame] | 377 | |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 378 | #ifdef CONFIG_RISCV_M_MODE |
| 379 | ENTRY(reset_regs) |
| 380 | li sp, 0 |
| 381 | li gp, 0 |
| 382 | li tp, 0 |
| 383 | li t0, 0 |
| 384 | li t1, 0 |
| 385 | li t2, 0 |
| 386 | li s0, 0 |
| 387 | li s1, 0 |
| 388 | li a2, 0 |
| 389 | li a3, 0 |
| 390 | li a4, 0 |
| 391 | li a5, 0 |
| 392 | li a6, 0 |
| 393 | li a7, 0 |
| 394 | li s2, 0 |
| 395 | li s3, 0 |
| 396 | li s4, 0 |
| 397 | li s5, 0 |
| 398 | li s6, 0 |
| 399 | li s7, 0 |
| 400 | li s8, 0 |
| 401 | li s9, 0 |
| 402 | li s10, 0 |
| 403 | li s11, 0 |
| 404 | li t3, 0 |
| 405 | li t4, 0 |
| 406 | li t5, 0 |
| 407 | li t6, 0 |
Greentime Hu | d411cf0 | 2019-12-19 14:44:59 +0800 | [diff] [blame] | 408 | csrw CSR_SCRATCH, 0 |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 409 | |
| 410 | #ifdef CONFIG_FPU |
| 411 | csrr t0, CSR_MISA |
| 412 | andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D) |
Guo Ren | dc6fcba | 2020-01-05 10:52:14 +0800 | [diff] [blame] | 413 | beqz t0, .Lreset_regs_done |
Christoph Hellwig | 9e80635 | 2019-10-28 13:10:40 +0100 | [diff] [blame] | 414 | |
| 415 | li t1, SR_FS |
| 416 | csrs CSR_STATUS, t1 |
| 417 | fmv.s.x f0, zero |
| 418 | fmv.s.x f1, zero |
| 419 | fmv.s.x f2, zero |
| 420 | fmv.s.x f3, zero |
| 421 | fmv.s.x f4, zero |
| 422 | fmv.s.x f5, zero |
| 423 | fmv.s.x f6, zero |
| 424 | fmv.s.x f7, zero |
| 425 | fmv.s.x f8, zero |
| 426 | fmv.s.x f9, zero |
| 427 | fmv.s.x f10, zero |
| 428 | fmv.s.x f11, zero |
| 429 | fmv.s.x f12, zero |
| 430 | fmv.s.x f13, zero |
| 431 | fmv.s.x f14, zero |
| 432 | fmv.s.x f15, zero |
| 433 | fmv.s.x f16, zero |
| 434 | fmv.s.x f17, zero |
| 435 | fmv.s.x f18, zero |
| 436 | fmv.s.x f19, zero |
| 437 | fmv.s.x f20, zero |
| 438 | fmv.s.x f21, zero |
| 439 | fmv.s.x f22, zero |
| 440 | fmv.s.x f23, zero |
| 441 | fmv.s.x f24, zero |
| 442 | fmv.s.x f25, zero |
| 443 | fmv.s.x f26, zero |
| 444 | fmv.s.x f27, zero |
| 445 | fmv.s.x f28, zero |
| 446 | fmv.s.x f29, zero |
| 447 | fmv.s.x f30, zero |
| 448 | fmv.s.x f31, zero |
| 449 | csrw fcsr, 0 |
| 450 | /* note that the caller must clear SR_FS */ |
| 451 | #endif /* CONFIG_FPU */ |
| 452 | .Lreset_regs_done: |
| 453 | ret |
| 454 | END(reset_regs) |
| 455 | #endif /* CONFIG_RISCV_M_MODE */ |