head.S 8.8 KB

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