fc99730869466f6652007dec7db645dc36bf85da
[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/thread_info.h>
7 #include <asm/asm-offsets.h>
8 #include <asm/asm.h>
9 #include <linux/init.h>
10 #include <linux/linkage.h>
11 #include <asm/thread_info.h>
12 #include <asm/page.h>
13 #include <asm/csr.h>
14 #include <asm/image.h>
15
16 __INIT
17 ENTRY(_start)
18         /*
19          * Image header expected by Linux boot-loaders. The image header data
20          * structure is described in asm/image.h.
21          * Do not modify it without modifying the structure and all bootloaders
22          * that expects this header format!!
23          */
24         /* jump to start kernel */
25         j _start_kernel
26         /* reserved */
27         .word 0
28         .balign 8
29 #if __riscv_xlen == 64
30         /* Image load offset(2MB) from start of RAM */
31         .dword 0x200000
32 #else
33         /* Image load offset(4MB) from start of RAM */
34         .dword 0x400000
35 #endif
36         /* Effective size of kernel image */
37         .dword _end - _start
38         .dword __HEAD_FLAGS
39         .word RISCV_HEADER_VERSION
40         .word 0
41         .dword 0
42         .ascii RISCV_IMAGE_MAGIC
43         .balign 4
44         .ascii RISCV_IMAGE_MAGIC2
45         .word 0
46
47 .global _start_kernel
48 _start_kernel:
49         /* Mask all interrupts */
50         csrw CSR_IE, zero
51         csrw CSR_IP, zero
52
53 #ifdef CONFIG_RISCV_M_MODE
54         /*
55          * The hartid in a0 is expected later on, and we have no firmware
56          * to hand it to us.
57          */
58         csrr a0, CSR_MHARTID
59 #endif
60
61         /* Load the global pointer */
62 .option push
63 .option norelax
64         la gp, __global_pointer$
65 .option pop
66
67         /*
68          * Disable FPU to detect illegal usage of
69          * floating point in kernel space
70          */
71         li t0, SR_FS
72         csrc CSR_STATUS, t0
73
74 #ifdef CONFIG_SMP
75         li t0, CONFIG_NR_CPUS
76         bgeu a0, t0, .Lsecondary_park
77 #endif
78
79         /* Pick one hart to run the main boot sequence */
80         la a3, hart_lottery
81         li a2, 1
82         amoadd.w a3, a2, (a3)
83         bnez a3, .Lsecondary_start
84
85         /* Clear BSS for flat non-ELF images */
86         la a3, __bss_start
87         la a4, __bss_stop
88         ble a4, a3, clear_bss_done
89 clear_bss:
90         REG_S zero, (a3)
91         add a3, a3, RISCV_SZPTR
92         blt a3, a4, clear_bss
93 clear_bss_done:
94
95         /* Save hart ID and DTB physical address */
96         mv s0, a0
97         mv s1, a1
98         la a2, boot_cpu_hartid
99         REG_S a0, (a2)
100
101         /* Initialize page tables and relocate to virtual addresses */
102         la sp, init_thread_union + THREAD_SIZE
103         mv a0, s1
104         call setup_vm
105         la a0, early_pg_dir
106         call relocate
107
108         /* Restore C environment */
109         la tp, init_task
110         sw zero, TASK_TI_CPU(tp)
111         la sp, init_thread_union + THREAD_SIZE
112
113         /* Start the kernel */
114         call parse_dtb
115         tail start_kernel
116
117 relocate:
118         /* Relocate return address */
119         li a1, PAGE_OFFSET
120         la a2, _start
121         sub a1, a1, a2
122         add ra, ra, a1
123
124         /* Point stvec to virtual address of intruction after satp write */
125         la a2, 1f
126         add a2, a2, a1
127         csrw CSR_TVEC, a2
128
129         /* Compute satp for kernel page tables, but don't load it yet */
130         srl a2, a0, PAGE_SHIFT
131         li a1, SATP_MODE
132         or a2, a2, a1
133
134         /*
135          * Load trampoline page directory, which will cause us to trap to
136          * stvec if VA != PA, or simply fall through if VA == PA.  We need a
137          * full fence here because setup_vm() just wrote these PTEs and we need
138          * to ensure the new translations are in use.
139          */
140         la a0, trampoline_pg_dir
141         srl a0, a0, PAGE_SHIFT
142         or a0, a0, a1
143         sfence.vma
144         csrw CSR_SATP, a0
145 .align 2
146 1:
147         /* Set trap vector to spin forever to help debug */
148         la a0, .Lsecondary_park
149         csrw CSR_TVEC, a0
150
151         /* Reload the global pointer */
152 .option push
153 .option norelax
154         la gp, __global_pointer$
155 .option pop
156
157         /*
158          * Switch to kernel page tables.  A full fence is necessary in order to
159          * avoid using the trampoline translations, which are only correct for
160          * the first superpage.  Fetching the fence is guarnteed to work
161          * because that first superpage is translated the same way.
162          */
163         csrw CSR_SATP, a2
164         sfence.vma
165
166         ret
167
168 .Lsecondary_start:
169 #ifdef CONFIG_SMP
170         /* Set trap vector to spin forever to help debug */
171         la a3, .Lsecondary_park
172         csrw CSR_TVEC, a3
173
174         slli a3, a0, LGREG
175         la a1, __cpu_up_stack_pointer
176         la a2, __cpu_up_task_pointer
177         add a1, a3, a1
178         add a2, a3, a2
179
180         /*
181          * This hart didn't win the lottery, so we wait for the winning hart to
182          * get far enough along the boot process that it should continue.
183          */
184 .Lwait_for_cpu_up:
185         /* FIXME: We should WFI to save some energy here. */
186         REG_L sp, (a1)
187         REG_L tp, (a2)
188         beqz sp, .Lwait_for_cpu_up
189         beqz tp, .Lwait_for_cpu_up
190         fence
191
192         /* Enable virtual memory and relocate to virtual address */
193         la a0, swapper_pg_dir
194         call relocate
195
196         tail smp_callin
197 #endif
198
199 .align 2
200 .Lsecondary_park:
201         /* We lack SMP support or have too many harts, so park this hart */
202         wfi
203         j .Lsecondary_park
204 END(_start)
205
206 __PAGE_ALIGNED_BSS
207         /* Empty zero page */
208         .balign PAGE_SIZE