xref: /freebsd/sys/riscv/riscv/machdep.c (revision 962b0bcb)
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * Portions of this software were developed by SRI International and the
7  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Portions of this software were developed by the University of Cambridge
11  * Computer Laboratory as part of the CTSRD Project, with support from the
12  * UK Higher Education Innovation Fund (HEIF).
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "opt_ddb.h"
37 #include "opt_kstack_pages.h"
38 #include "opt_platform.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/boot.h>
43 #include <sys/buf.h>
44 #include <sys/bus.h>
45 #include <sys/cons.h>
46 #include <sys/cpu.h>
47 #include <sys/devmap.h>
48 #include <sys/exec.h>
49 #include <sys/imgact.h>
50 #include <sys/kdb.h>
51 #include <sys/kernel.h>
52 #include <sys/ktr.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
55 #include <sys/msgbuf.h>
56 #include <sys/pcpu.h>
57 #include <sys/physmem.h>
58 #include <sys/proc.h>
59 #include <sys/ptrace.h>
60 #include <sys/reboot.h>
61 #include <sys/reg.h>
62 #include <sys/rwlock.h>
63 #include <sys/sched.h>
64 #include <sys/signalvar.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysent.h>
67 #include <sys/sysproto.h>
68 #include <sys/tslog.h>
69 #include <sys/ucontext.h>
70 #include <sys/vmmeter.h>
71 
72 #include <vm/vm.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_phys.h>
78 #include <vm/pmap.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_pager.h>
81 
82 #include <machine/cpu.h>
83 #include <machine/fpe.h>
84 #include <machine/intr.h>
85 #include <machine/kdb.h>
86 #include <machine/machdep.h>
87 #include <machine/metadata.h>
88 #include <machine/pcb.h>
89 #include <machine/pte.h>
90 #include <machine/riscvreg.h>
91 #include <machine/sbi.h>
92 #include <machine/trap.h>
93 #include <machine/vmparam.h>
94 
95 #ifdef DDB
96 #include <ddb/ddb.h>
97 #endif
98 
99 #ifdef FDT
100 #include <contrib/libfdt/libfdt.h>
101 #include <dev/fdt/fdt_common.h>
102 #include <dev/ofw/openfirm.h>
103 #endif
104 
105 struct pcpu __pcpu[MAXCPU];
106 
107 static struct trapframe proc0_tf;
108 
109 int early_boot = 1;
110 int cold = 1;
111 
112 #define	DTB_SIZE_MAX	(1024 * 1024)
113 
114 struct kva_md_info kmi;
115 
116 int64_t dcache_line_size;	/* The minimum D cache line size */
117 int64_t icache_line_size;	/* The minimum I cache line size */
118 int64_t idcache_line_size;	/* The minimum cache line size */
119 
120 #define BOOT_HART_INVALID	0xffffffff
121 uint32_t boot_hart = BOOT_HART_INVALID;	/* The hart we booted on. */
122 
123 cpuset_t all_harts;
124 
125 extern int *end;
126 
127 static char static_kenv[PAGE_SIZE];
128 
129 static void
cpu_startup(void * dummy)130 cpu_startup(void *dummy)
131 {
132 
133 	sbi_print_version();
134 	printcpuinfo(0);
135 
136 	printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
137 	    ptoa((uintmax_t)realmem) / (1024 * 1024));
138 
139 	/*
140 	 * Display any holes after the first chunk of extended memory.
141 	 */
142 	if (bootverbose) {
143 		int indx;
144 
145 		printf("Physical memory chunk(s):\n");
146 		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
147 			vm_paddr_t size;
148 
149 			size = phys_avail[indx + 1] - phys_avail[indx];
150 			printf(
151 			    "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
152 			    (uintmax_t)phys_avail[indx],
153 			    (uintmax_t)phys_avail[indx + 1] - 1,
154 			    (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
155 		}
156 	}
157 
158 	vm_ksubmap_init(&kmi);
159 
160 	printf("avail memory = %ju (%ju MB)\n",
161 	    ptoa((uintmax_t)vm_free_count()),
162 	    ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
163 	if (bootverbose)
164 		devmap_print_table();
165 
166 	bufinit();
167 	vm_pager_bufferinit();
168 }
169 
170 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
171 
172 int
cpu_idle_wakeup(int cpu)173 cpu_idle_wakeup(int cpu)
174 {
175 
176 	return (0);
177 }
178 
179 void
cpu_idle(int busy)180 cpu_idle(int busy)
181 {
182 
183 	spinlock_enter();
184 	if (!busy)
185 		cpu_idleclock();
186 	if (!sched_runnable())
187 		__asm __volatile(
188 		    "fence \n"
189 		    "wfi   \n");
190 	if (!busy)
191 		cpu_activeclock();
192 	spinlock_exit();
193 }
194 
195 void
cpu_halt(void)196 cpu_halt(void)
197 {
198 
199 	/*
200 	 * Try to power down using the HSM SBI extension and fall back to a
201 	 * simple wfi loop.
202 	 */
203 	intr_disable();
204 	if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
205 		sbi_hsm_hart_stop();
206 	for (;;)
207 		__asm __volatile("wfi");
208 	/* NOTREACHED */
209 }
210 
211 /*
212  * Flush the D-cache for non-DMA I/O so that the I-cache can
213  * be made coherent later.
214  */
215 void
cpu_flush_dcache(void * ptr,size_t len)216 cpu_flush_dcache(void *ptr, size_t len)
217 {
218 
219 	/* TBD */
220 }
221 
222 /* Get current clock frequency for the given CPU ID. */
223 int
cpu_est_clockrate(int cpu_id,uint64_t * rate)224 cpu_est_clockrate(int cpu_id, uint64_t *rate)
225 {
226 
227 	panic("cpu_est_clockrate");
228 }
229 
230 void
cpu_pcpu_init(struct pcpu * pcpu,int cpuid,size_t size)231 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
232 {
233 }
234 
235 void
spinlock_enter(void)236 spinlock_enter(void)
237 {
238 	struct thread *td;
239 	register_t reg;
240 
241 	td = curthread;
242 	if (td->td_md.md_spinlock_count == 0) {
243 		reg = intr_disable();
244 		td->td_md.md_spinlock_count = 1;
245 		td->td_md.md_saved_sstatus_ie = reg;
246 		critical_enter();
247 	} else
248 		td->td_md.md_spinlock_count++;
249 }
250 
251 void
spinlock_exit(void)252 spinlock_exit(void)
253 {
254 	struct thread *td;
255 	register_t sstatus_ie;
256 
257 	td = curthread;
258 	sstatus_ie = td->td_md.md_saved_sstatus_ie;
259 	td->td_md.md_spinlock_count--;
260 	if (td->td_md.md_spinlock_count == 0) {
261 		critical_exit();
262 		intr_restore(sstatus_ie);
263 	}
264 }
265 
266 /*
267  * Construct a PCB from a trapframe. This is called from kdb_trap() where
268  * we want to start a backtrace from the function that caused us to enter
269  * the debugger. We have the context in the trapframe, but base the trace
270  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
271  * enough for a backtrace.
272  */
273 void
makectx(struct trapframe * tf,struct pcb * pcb)274 makectx(struct trapframe *tf, struct pcb *pcb)
275 {
276 
277 	memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
278 
279 	pcb->pcb_ra = tf->tf_sepc;
280 	pcb->pcb_sp = tf->tf_sp;
281 	pcb->pcb_gp = tf->tf_gp;
282 	pcb->pcb_tp = tf->tf_tp;
283 }
284 
285 static void
init_proc0(vm_offset_t kstack)286 init_proc0(vm_offset_t kstack)
287 {
288 	struct pcpu *pcpup;
289 
290 	pcpup = &__pcpu[0];
291 
292 	proc_linkup0(&proc0, &thread0);
293 	thread0.td_kstack = kstack;
294 	thread0.td_kstack_pages = KSTACK_PAGES;
295 	thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
296 	    thread0.td_kstack_pages * PAGE_SIZE) - 1;
297 	thread0.td_pcb->pcb_fpflags = 0;
298 	thread0.td_frame = &proc0_tf;
299 	pcpup->pc_curpcb = thread0.td_pcb;
300 }
301 
302 #ifdef FDT
303 static void
try_load_dtb(caddr_t kmdp)304 try_load_dtb(caddr_t kmdp)
305 {
306 	vm_offset_t dtbp;
307 
308 	dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
309 
310 #if defined(FDT_DTB_STATIC)
311 	/*
312 	 * In case the device tree blob was not retrieved (from metadata) try
313 	 * to use the statically embedded one.
314 	 */
315 	if (dtbp == (vm_offset_t)NULL)
316 		dtbp = (vm_offset_t)&fdt_static_dtb;
317 #endif
318 
319 	if (dtbp == (vm_offset_t)NULL) {
320 		printf("ERROR loading DTB\n");
321 		return;
322 	}
323 
324 	if (OF_install(OFW_FDT, 0) == FALSE)
325 		panic("Cannot install FDT");
326 
327 	if (OF_init((void *)dtbp) != 0)
328 		panic("OF_init failed with the found device tree");
329 }
330 #endif
331 
332 static void
cache_setup(void)333 cache_setup(void)
334 {
335 
336 	/* TODO */
337 
338 	dcache_line_size = 0;
339 	icache_line_size = 0;
340 	idcache_line_size = 0;
341 }
342 
343 /*
344  * Fake up a boot descriptor table.
345  */
346 static void
fake_preload_metadata(struct riscv_bootparams * rvbp)347 fake_preload_metadata(struct riscv_bootparams *rvbp)
348 {
349 	static uint32_t fake_preload[48];
350 	vm_offset_t lastaddr;
351 	size_t fake_size, dtb_size;
352 
353 #define PRELOAD_PUSH_VALUE(type, value) do {			\
354 	*(type *)((char *)fake_preload + fake_size) = (value);	\
355 	fake_size += sizeof(type);				\
356 } while (0)
357 
358 #define PRELOAD_PUSH_STRING(str) do {				\
359 	uint32_t ssize;						\
360 	ssize = strlen(str) + 1;				\
361 	PRELOAD_PUSH_VALUE(uint32_t, ssize);			\
362 	strcpy(((char *)fake_preload + fake_size), str);	\
363 	fake_size += ssize;					\
364 	fake_size = roundup(fake_size, sizeof(u_long));		\
365 } while (0)
366 
367 	fake_size = 0;
368 	lastaddr = (vm_offset_t)&end;
369 
370 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
371 	PRELOAD_PUSH_STRING("kernel");
372 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
373 	PRELOAD_PUSH_STRING("elf kernel");
374 
375 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
376 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
377 	PRELOAD_PUSH_VALUE(uint64_t, KERNBASE);
378 
379 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
380 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
381 	PRELOAD_PUSH_VALUE(uint64_t, (size_t)((vm_offset_t)&end - KERNBASE));
382 
383 	/* Copy the DTB to KVA space. */
384 	lastaddr = roundup(lastaddr, sizeof(int));
385 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
386 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
387 	PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
388 	dtb_size = fdt_totalsize(rvbp->dtbp_virt);
389 	memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
390 	lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
391 
392 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
393 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
394 	PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
395 
396 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
397 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
398 	PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
399 
400 	/* End marker */
401 	PRELOAD_PUSH_VALUE(uint32_t, 0);
402 	PRELOAD_PUSH_VALUE(uint32_t, 0);
403 	preload_metadata = (caddr_t)fake_preload;
404 
405 	/* Check if bootloader clobbered part of the kernel with the DTB. */
406 	KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
407 		rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
408 	    ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
409 		rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
410 		rvbp->kern_phys + (lastaddr - KERNBASE)));
411 	KASSERT(fake_size < sizeof(fake_preload),
412 	    ("Too many fake_preload items"));
413 
414 	if (boothowto & RB_VERBOSE)
415 		printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
416 		    rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
417 		    rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
418 }
419 
420 /* Support for FDT configurations only. */
421 CTASSERT(FDT);
422 
423 #ifdef FDT
424 static void
parse_fdt_bootargs(void)425 parse_fdt_bootargs(void)
426 {
427 	char bootargs[512];
428 
429 	bootargs[sizeof(bootargs) - 1] = '\0';
430 	if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
431 		boothowto |= boot_parse_cmdline(bootargs);
432 	}
433 }
434 #endif
435 
436 static vm_offset_t
parse_metadata(void)437 parse_metadata(void)
438 {
439 	caddr_t kmdp;
440 	vm_offset_t lastaddr;
441 #ifdef DDB
442 	vm_offset_t ksym_start, ksym_end;
443 #endif
444 	char *kern_envp;
445 
446 	/* Find the kernel address */
447 	kmdp = preload_search_by_type("elf kernel");
448 	if (kmdp == NULL)
449 		kmdp = preload_search_by_type("elf64 kernel");
450 	KASSERT(kmdp != NULL, ("No preload metadata found!"));
451 
452 	/* Read the boot metadata */
453 	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
454 	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
455 	kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
456 	if (kern_envp != NULL)
457 		init_static_kenv(kern_envp, 0);
458 	else
459 		init_static_kenv(static_kenv, sizeof(static_kenv));
460 #ifdef DDB
461 	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
462 	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
463 	db_fetch_ksymtab(ksym_start, ksym_end, 0);
464 #endif
465 #ifdef FDT
466 	try_load_dtb(kmdp);
467 	if (kern_envp == NULL)
468 		parse_fdt_bootargs();
469 #endif
470 	return (lastaddr);
471 }
472 
473 void
initriscv(struct riscv_bootparams * rvbp)474 initriscv(struct riscv_bootparams *rvbp)
475 {
476 	struct mem_region mem_regions[FDT_MEM_REGIONS];
477 	struct pcpu *pcpup;
478 	int mem_regions_sz;
479 	vm_offset_t lastaddr;
480 	vm_size_t kernlen;
481 #ifdef FDT
482 	phandle_t chosen;
483 	uint32_t hart;
484 #endif
485 	char *env;
486 
487 	TSRAW(&thread0, TS_ENTER, __func__, NULL);
488 
489 	/* Set the pcpu data, this is needed by pmap_bootstrap */
490 	pcpup = &__pcpu[0];
491 	pcpu_init(pcpup, 0, sizeof(struct pcpu));
492 
493 	/* Set the pcpu pointer */
494 	__asm __volatile("mv tp, %0" :: "r"(pcpup));
495 
496 	PCPU_SET(curthread, &thread0);
497 
498 	/* Initialize SBI interface. */
499 	sbi_init();
500 
501 	/* Parse the boot metadata. */
502 	if (rvbp->modulep != 0) {
503 		preload_metadata = (caddr_t)rvbp->modulep;
504 	} else {
505 		fake_preload_metadata(rvbp);
506 	}
507 	lastaddr = parse_metadata();
508 
509 #ifdef FDT
510 	/*
511 	 * Look for the boot hart ID. This was either passed in directly from
512 	 * the SBI firmware and handled by locore, or was stored in the device
513 	 * tree by an earlier boot stage.
514 	 */
515 	chosen = OF_finddevice("/chosen");
516 	if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
517 		boot_hart = hart;
518 	}
519 #endif
520 	if (boot_hart == BOOT_HART_INVALID) {
521 		panic("Boot hart ID was not properly set");
522 	}
523 	pcpup->pc_hart = boot_hart;
524 
525 #ifdef FDT
526 	/*
527 	 * Exclude reserved memory specified by the device tree. Typically,
528 	 * this contains an entry for memory used by the runtime SBI firmware.
529 	 */
530 	if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
531 		physmem_exclude_regions(mem_regions, mem_regions_sz,
532 		    EXFLAG_NODUMP | EXFLAG_NOALLOC);
533 	}
534 
535 	/* Grab physical memory regions information from device tree. */
536 	if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
537 		panic("Cannot get physical memory regions");
538 	}
539 	physmem_hardware_regions(mem_regions, mem_regions_sz);
540 #endif
541 
542 	/*
543 	 * Identify CPU/ISA features.
544 	 */
545 	identify_cpu(0);
546 
547 	/* Do basic tuning, hz etc */
548 	init_param1();
549 
550 	cache_setup();
551 
552 	/* Bootstrap enough of pmap to enter the kernel proper */
553 	kernlen = (lastaddr - KERNBASE);
554 	pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
555 
556 #ifdef FDT
557 	/*
558 	 * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
559 	 * this area is assumed to contain the SBI firmware. This is a little
560 	 * fragile, but it is consistent with the platforms we support so far.
561 	 *
562 	 * TODO: remove this when the all regular booting methods properly
563 	 * report their reserved memory in the device tree.
564 	 */
565 	physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
566 	    EXFLAG_NODUMP | EXFLAG_NOALLOC);
567 #endif
568 	physmem_init_kernel_globals();
569 
570 	/* Establish static device mappings */
571 	devmap_bootstrap(0, NULL);
572 
573 	cninit();
574 
575 	/*
576 	 * Dump the boot metadata. We have to wait for cninit() since console
577 	 * output is required. If it's grossly incorrect the kernel will never
578 	 * make it this far.
579 	 */
580 	if (getenv_is_true("debug.dump_modinfo_at_boot"))
581 		preload_dump();
582 
583 	init_proc0(rvbp->kern_stack);
584 
585 	msgbufinit(msgbufp, msgbufsize);
586 	mutex_init();
587 	init_param2(physmem);
588 	kdb_init();
589 #ifdef KDB
590 	if ((boothowto & RB_KDB) != 0)
591 		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
592 #endif
593 
594 	env = kern_getenv("kernelname");
595 	if (env != NULL)
596 		strlcpy(kernelname, env, sizeof(kernelname));
597 
598 	if (boothowto & RB_VERBOSE)
599 		physmem_print_tables();
600 
601 	early_boot = 0;
602 
603 	if (bootverbose && kstack_pages != KSTACK_PAGES)
604 		printf("kern.kstack_pages = %d ignored for thread0\n",
605 		    kstack_pages);
606 
607 	TSEXIT();
608 }
609