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