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