1
2
3
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/hwcap.h>
15#include <asm/image.h>
16
17__HEAD
18ENTRY(_start)
19
20
21
22
23
24
25
26 j _start_kernel
27
28 .word 0
29 .balign 8
30
31
32 .dword 0x200000
33#else
34
35 .dword 0x400000
36#endif
37
38 .dword _end - _start
39 .dword __HEAD_FLAGS
40 .word RISCV_HEADER_VERSION
41 .word 0
42 .dword 0
43 .ascii RISCV_IMAGE_MAGIC
44 .balign 4
45 .ascii RISCV_IMAGE_MAGIC2
46 .word 0
47
48.align 2
49#ifdef CONFIG_MMU
50relocate:
51
52 li a1, PAGE_OFFSET
53 la a2, _start
54 sub a1, a1, a2
55 add ra, ra, a1
56
57
58 la a2, 1f
59 add a2, a2, a1
60 csrw CSR_TVEC, a2
61
62
63 srl a2, a0, PAGE_SHIFT
64 li a1, SATP_MODE
65 or a2, a2, a1
66
67
68
69
70
71
72
73 la a0, trampoline_pg_dir
74 srl a0, a0, PAGE_SHIFT
75 or a0, a0, a1
76 sfence.vma
77 csrw CSR_SATP, a0
78.align 2
791:
80
81 la a0, .Lsecondary_park
82 csrw CSR_TVEC, a0
83
84
85.option push
86.option norelax
87 la gp, __global_pointer$
88.option pop
89
90
91
92
93
94
95
96 csrw CSR_SATP, a2
97 sfence.vma
98
99 ret
100#endif
101#ifdef CONFIG_SMP
102 .global secondary_start_sbi
103secondary_start_sbi:
104
105 csrw CSR_IE, zero
106 csrw CSR_IP, zero
107
108
109 .option push
110 .option norelax
111 la gp, __global_pointer$
112 .option pop
113
114
115
116
117
118 li t0, SR_FS
119 csrc CSR_STATUS, t0
120
121
122 la a3, .Lsecondary_park
123 csrw CSR_TVEC, a3
124
125 slli a3, a0, LGREG
126 la a4, __cpu_up_stack_pointer
127 la a5, __cpu_up_task_pointer
128 add a4, a3, a4
129 add a5, a3, a5
130 REG_L sp, (a4)
131 REG_L tp, (a5)
132
133 .global secondary_start_common
134secondary_start_common:
135
136#ifdef CONFIG_MMU
137
138 la a0, swapper_pg_dir
139 call relocate
140#endif
141 call setup_trap_vector
142 tail smp_callin
143#endif
144
145.align 2
146setup_trap_vector:
147
148 la a0, handle_exception
149 csrw CSR_TVEC, a0
150
151
152
153
154
155 csrw CSR_SCRATCH, zero
156 ret
157
158.Lsecondary_park:
159
160 wfi
161 j .Lsecondary_park
162
163END(_start)
164
165 __INIT
166ENTRY(_start_kernel)
167
168 csrw CSR_IE, zero
169 csrw CSR_IP, zero
170
171#ifdef CONFIG_RISCV_M_MODE
172
173 fence.i
174
175
176 call reset_regs
177
178
179
180
181
182
183 la a0, pmp_done
184 csrw CSR_TVEC, a0
185
186 li a0, -1
187 csrw CSR_PMPADDR0, a0
188 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
189 csrw CSR_PMPCFG0, a0
190.align 2
191pmp_done:
192
193
194
195
196
197 csrr a0, CSR_MHARTID
198#endif
199
200
201.option push
202.option norelax
203 la gp, __global_pointer$
204.option pop
205
206
207
208
209
210 li t0, SR_FS
211 csrc CSR_STATUS, t0
212
213#ifdef CONFIG_SMP
214 li t0, CONFIG_NR_CPUS
215 blt a0, t0, .Lgood_cores
216 tail .Lsecondary_park
217.Lgood_cores:
218#endif
219
220
221 la a3, hart_lottery
222 li a2, 1
223 amoadd.w a3, a2, (a3)
224 bnez a3, .Lsecondary_start
225
226
227 la a3, __bss_start
228 la a4, __bss_stop
229 ble a4, a3, clear_bss_done
230clear_bss:
231 REG_S zero, (a3)
232 add a3, a3, RISCV_SZPTR
233 blt a3, a4, clear_bss
234clear_bss_done:
235
236
237 mv s0, a0
238 mv s1, a1
239 la a2, boot_cpu_hartid
240 REG_S a0, (a2)
241
242
243 la sp, init_thread_union + THREAD_SIZE
244 mv a0, s1
245 call setup_vm
246#ifdef CONFIG_MMU
247 la a0, early_pg_dir
248 call relocate
249#endif
250
251 call setup_trap_vector
252
253 la tp, init_task
254 sw zero, TASK_TI_CPU(tp)
255 la sp, init_thread_union + THREAD_SIZE
256
257#ifdef CONFIG_KASAN
258 call kasan_early_init
259#endif
260
261 call soc_early_init
262 call parse_dtb
263 tail start_kernel
264
265.Lsecondary_start:
266#ifdef CONFIG_SMP
267
268 la a3, .Lsecondary_park
269 csrw CSR_TVEC, a3
270
271 slli a3, a0, LGREG
272 la a1, __cpu_up_stack_pointer
273 la a2, __cpu_up_task_pointer
274 add a1, a3, a1
275 add a2, a3, a2
276
277
278
279
280
281.Lwait_for_cpu_up:
282
283 REG_L sp, (a1)
284 REG_L tp, (a2)
285 beqz sp, .Lwait_for_cpu_up
286 beqz tp, .Lwait_for_cpu_up
287 fence
288
289 tail secondary_start_common
290#endif
291
292END(_start_kernel)
293
294#ifdef CONFIG_RISCV_M_MODE
295ENTRY(reset_regs)
296 li sp, 0
297 li gp, 0
298 li tp, 0
299 li t0, 0
300 li t1, 0
301 li t2, 0
302 li s0, 0
303 li s1, 0
304 li a2, 0
305 li a3, 0
306 li a4, 0
307 li a5, 0
308 li a6, 0
309 li a7, 0
310 li s2, 0
311 li s3, 0
312 li s4, 0
313 li s5, 0
314 li s6, 0
315 li s7, 0
316 li s8, 0
317 li s9, 0
318 li s10, 0
319 li s11, 0
320 li t3, 0
321 li t4, 0
322 li t5, 0
323 li t6, 0
324 csrw CSR_SCRATCH, 0
325
326#ifdef CONFIG_FPU
327 csrr t0, CSR_MISA
328 andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D)
329 beqz t0, .Lreset_regs_done
330
331 li t1, SR_FS
332 csrs CSR_STATUS, t1
333 fmv.s.x f0, zero
334 fmv.s.x f1, zero
335 fmv.s.x f2, zero
336 fmv.s.x f3, zero
337 fmv.s.x f4, zero
338 fmv.s.x f5, zero
339 fmv.s.x f6, zero
340 fmv.s.x f7, zero
341 fmv.s.x f8, zero
342 fmv.s.x f9, zero
343 fmv.s.x f10, zero
344 fmv.s.x f11, zero
345 fmv.s.x f12, zero
346 fmv.s.x f13, zero
347 fmv.s.x f14, zero
348 fmv.s.x f15, zero
349 fmv.s.x f16, zero
350 fmv.s.x f17, zero
351 fmv.s.x f18, zero
352 fmv.s.x f19, zero
353 fmv.s.x f20, zero
354 fmv.s.x f21, zero
355 fmv.s.x f22, zero
356 fmv.s.x f23, zero
357 fmv.s.x f24, zero
358 fmv.s.x f25, zero
359 fmv.s.x f26, zero
360 fmv.s.x f27, zero
361 fmv.s.x f28, zero
362 fmv.s.x f29, zero
363 fmv.s.x f30, zero
364 fmv.s.x f31, zero
365 csrw fcsr, 0
366
367#endif
368.Lreset_regs_done:
369 ret
370END(reset_regs)
371#endif
372
373__PAGE_ALIGNED_BSS
374
375 .balign PAGE_SIZE
376