xref: /openbsd/sys/arch/armv7/armv7/armv7_machdep.c (revision 46e85305)
1 /*	$OpenBSD: armv7_machdep.c,v 1.66 2023/07/05 08:15:34 jsg Exp $ */
2 /*	$NetBSD: lubbock_machdep.c,v 1.2 2003/07/15 00:25:06 lukem Exp $ */
3 
4 /*
5  * Copyright (c) 2002, 2003  Genetec Corporation.  All rights reserved.
6  * Written by Hiroyuki Bessho for Genetec Corporation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of Genetec Corporation may not be used to endorse or
17  *    promote products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Machine dependant functions for kernel setup for
33  * Intel DBPXA250 evaluation board (a.k.a. Lubbock).
34  * Based on iq80310_machhdep.c
35  */
36 /*
37  * Copyright (c) 2001 Wasabi Systems, Inc.
38  * All rights reserved.
39  *
40  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed for the NetBSD Project by
53  *	Wasabi Systems, Inc.
54  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
55  *    or promote products derived from this software without specific prior
56  *    written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  */
70 
71 /*
72  * Copyright (c) 1997,1998 Mark Brinicombe.
73  * Copyright (c) 1997,1998 Causality Limited.
74  * All rights reserved.
75  *
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions
78  * are met:
79  * 1. Redistributions of source code must retain the above copyright
80  *    notice, this list of conditions and the following disclaimer.
81  * 2. Redistributions in binary form must reproduce the above copyright
82  *    notice, this list of conditions and the following disclaimer in the
83  *    documentation and/or other materials provided with the distribution.
84  * 3. All advertising materials mentioning features or use of this software
85  *    must display the following acknowledgement:
86  *	This product includes software developed by Mark Brinicombe
87  *	for the NetBSD Project.
88  * 4. The name of the company nor the name of the author may be used to
89  *    endorse or promote products derived from this software without specific
90  *    prior written permission.
91  *
92  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
93  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
94  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
95  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
96  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
97  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
98  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102  * SUCH DAMAGE.
103  *
104  * Machine dependant functions for kernel setup for ARMv7 boards using
105  * u-boot/EFI firmware.
106  */
107 
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/proc.h>
111 #include <sys/reboot.h>
112 
113 #include <machine/db_machdep.h>
114 #include <machine/bootconfig.h>
115 #include <machine/machine_reg.h>
116 #include <machine/bus.h>
117 
118 #include <arm/undefined.h>
119 #include <arm/machdep.h>
120 #include <arm/armv7/armv7var.h>
121 #include <armv7/armv7/armv7_machdep.h>
122 
123 #include <dev/cons.h>
124 #include <dev/efi/efi.h>
125 #include <dev/ofw/fdt.h>
126 #include <dev/ofw/openfirm.h>
127 
128 #include <net/if.h>
129 
130 #include <ddb/db_extern.h>
131 
132 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
133 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00000000)
134 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x04000000)
135 #define KERNEL_VM_SIZE		VM_KERNEL_SPACE_SIZE
136 
137 /*
138  * Address to call from cpu_reset() to reset the machine.
139  * This is machine architecture dependant as it varies depending
140  * on where the ROM appears when you turn the MMU off.
141  */
142 
143 /* Define various stack sizes in pages */
144 #define IRQ_STACK_SIZE	1
145 #define ABT_STACK_SIZE	1
146 #define UND_STACK_SIZE	1
147 
148 BootConfig bootconfig;		/* Boot config storage */
149 char *boot_args = NULL;
150 char *boot_file = "";
151 uint8_t *bootmac = NULL;
152 u_int cpu_reset_address = 0;
153 
154 vaddr_t physical_freestart;
155 int physmem;
156 
157 /* Physical and virtual addresses for some global pages */
158 pv_addr_t systempage;
159 pv_addr_t irqstack;
160 pv_addr_t undstack;
161 pv_addr_t abtstack;
162 extern pv_addr_t kernelstack;
163 
164 vaddr_t msgbufphys;
165 
166 extern u_int data_abort_handler_address;
167 extern u_int prefetch_abort_handler_address;
168 extern u_int undefined_handler_address;
169 
170 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
171 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
172 #define	KERNEL_PT_KERNEL_NUM	32
173 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
174 				        /* Page tables for mapping kernel VM */
175 #define	KERNEL_PT_VMDATA_NUM	8	/* start with 16MB of KVM */
176 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
177 
178 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
179 
180 extern struct user *proc0paddr;
181 
182 /*
183  * safepri is a safe priority for sleep to set for a spin-wait
184  * during autoconfiguration or after a panic.
185  */
186 int   safepri = 0;
187 
188 /* Prototypes */
189 
190 int	bootstrap_bs_map(void *, uint64_t, bus_size_t, int,
191     bus_space_handle_t *);
192 void	collect_kernel_args(const char *);
193 void	process_kernel_args(void);
194 void	consinit(void);
195 
196 bs_protos(bs_notimpl);
197 
198 int stdout_node;
199 int stdout_speed;
200 
201 void (*cpuresetfn)(void);
202 void (*powerdownfn)(void);
203 
204 /*
205  * void boot(int howto, char *bootstr)
206  *
207  * Reboots the system
208  *
209  * Deal with any syncing, unmounting, dumping and shutdown hooks,
210  * then reset the CPU.
211  */
212 __dead void
boot(int howto)213 boot(int howto)
214 {
215 	if ((howto & RB_RESET) != 0)
216 		goto doreset;
217 
218 	if (cold) {
219 		if ((howto & RB_USERREQ) == 0)
220 			howto |= RB_HALT;
221 		goto haltsys;
222 	}
223 
224 	/* Disable console buffering */
225 /*	cnpollc(1);*/
226 
227 	/*
228 	 * If RB_NOSYNC was not specified sync the discs.
229 	 * Note: Unless cold is set to 1 here, syslogd will die during the
230 	 * unmount.  It looks like syslogd is getting woken up only to find
231 	 * that it cannot page part of the binary in as the filesystem has
232 	 * been unmounted.
233 	 */
234 	if ((howto & RB_NOSYNC) == 0)
235 		bootsync(howto);
236 
237 	if_downall();
238 
239 	uvm_shutdown();
240 	splhigh();
241 	cold = 1;
242 
243 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
244 		dumpsys();
245 
246 haltsys:
247 	config_suspend_all(DVACT_POWERDOWN);
248 
249 	/* Make sure IRQ's are disabled */
250 	intr_disable();
251 
252 	if ((howto & RB_HALT) != 0) {
253 		if ((howto & RB_POWERDOWN) != 0) {
254 			printf("\nAttempting to power down...\n");
255 			delay(500000);
256 			if (powerdownfn)
257 				(*powerdownfn)();
258 		}
259 
260 		printf("The operating system has halted.\n");
261 		printf("Please press any key to reboot.\n\n");
262 		cngetc();
263 	}
264 
265 doreset:
266 	printf("rebooting...\n");
267 	delay(500000);
268 	if (cpuresetfn)
269 		(*cpuresetfn)();
270 	printf("reboot failed; spinning\n");
271 	for (;;)
272 		continue;
273 	/* NOTREACHED */
274 }
275 
276 static __inline
277 pd_entry_t *
read_ttb(void)278 read_ttb(void)
279 {
280   long ttb;
281 
282   __asm volatile("mrc	p15, 0, %0, c2, c0, 0" : "=r" (ttb));
283 
284 
285   return (pd_entry_t *)(ttb & ~((1<<14)-1));
286 }
287 
288 #define VERBOSE_INIT_ARM
289 
290 /*
291  * simple memory mapping function used in early bootstrap stage
292  * before pmap is initialized.
293  * ignores cacheability and does map the sections with nocache.
294  */
295 static vaddr_t section_free = 0xfd000000; /* XXX - huh */
296 
297 int
bootstrap_bs_map(void * t,uint64_t bpa,bus_size_t size,int flags,bus_space_handle_t * bshp)298 bootstrap_bs_map(void *t, uint64_t bpa, bus_size_t size,
299     int flags, bus_space_handle_t *bshp)
300 {
301 	u_long startpa, pa, endpa;
302 	vaddr_t va;
303 	pd_entry_t *pagedir = read_ttb();
304 	/* This assumes PA==VA for page directory */
305 
306 	va = section_free;
307 
308 	startpa = bpa & ~L1_S_OFFSET;
309 	endpa = (bpa + size) & ~L1_S_OFFSET;
310 	if ((bpa + size) & L1_S_OFFSET)
311 		endpa += L1_S_SIZE;
312 
313 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
314 
315 	for (pa = startpa; pa < endpa; pa += L1_S_SIZE, va += L1_S_SIZE)
316 		pmap_map_section((vaddr_t)pagedir, va, pa,
317 		    PROT_READ | PROT_WRITE, PTE_NOCACHE);
318 
319 	cpu_tlb_flushD();
320 
321 	section_free = va;
322 
323 	return 0;
324 }
325 
326 static void
copy_io_area_map(pd_entry_t * new_pd)327 copy_io_area_map(pd_entry_t *new_pd)
328 {
329 	pd_entry_t *cur_pd = read_ttb();
330 	vaddr_t va;
331 
332 	for (va = MACHINE_IO_AREA_VBASE;
333 	     (cur_pd[va>>L1_S_SHIFT] & L1_TYPE_MASK) == L1_TYPE_S;
334 	     va += L1_S_SIZE) {
335 
336 		new_pd[va>>L1_S_SHIFT] = cur_pd[va>>L1_S_SHIFT];
337 		if (va == (ARM_VECTORS_HIGH & ~(0x00400000 - 1)))
338 			break; /* STUPID */
339 
340 	}
341 }
342 
343 uint64_t mmap_start;
344 uint32_t mmap_size;
345 uint32_t mmap_desc_size;
346 uint32_t mmap_desc_ver;
347 
348 EFI_MEMORY_DESCRIPTOR *mmap;
349 
350 /*
351  * u_int initarm(...)
352  *
353  * Initial entry point on startup. This gets called before main() is
354  * entered.
355  * It should be responsible for setting up everything that must be
356  * in place when main is called.
357  * This includes
358  *   Taking a copy of the FDT.
359  *   Initialising the physical console so characters can be printed.
360  *   Setting up page tables for the kernel.
361  */
362 u_int
initarm(void * arg0,void * arg1,void * arg2,paddr_t loadaddr)363 initarm(void *arg0, void *arg1, void *arg2, paddr_t loadaddr)
364 {
365 	int loop, loop1;
366 	u_int l1pagetable;
367 	pv_addr_t kernel_l1pt;
368 	pv_addr_t fdt, map;
369 	struct fdt_reg reg;
370 	paddr_t memstart, memend;
371 	void *config;
372 	size_t size;
373 	void *node;
374 	extern uint32_t esym; /* &_end if no symbols are loaded */
375 
376 	/* early bus_space_map support */
377 	struct bus_space tmp_bs_tag;
378 	int	(*map_func_save)(void *, uint64_t, bus_size_t, int,
379 	    bus_space_handle_t *);
380 
381 	if (arg0)
382 		esym = (uint32_t)arg0;
383 
384 	/*
385 	 * Heads up ... Setup the CPU / MMU / TLB functions
386 	 */
387 	if (set_cpufuncs())
388 		panic("cpu not recognized!");
389 
390 	/*
391 	 * Temporarily replace bus_space_map() functions so that
392 	 * console devices can get mapped.
393 	 */
394 	tmp_bs_tag = armv7_bs_tag;
395 	map_func_save = armv7_bs_tag.bs_map;
396 	armv7_bs_tag.bs_map = bootstrap_bs_map;
397 	tmp_bs_tag.bs_map = bootstrap_bs_map;
398 
399 	/*
400 	 * Now, map the FDT area.
401 	 *
402 	 * As we don't know the size of a possible FDT, map the size of a
403 	 * typical bootstrap bs map.  The FDT might not be aligned, so this
404 	 * might take up to two L1_S_SIZEd mappings.
405 	 *
406 	 * XXX: There's (currently) no way to unmap a bootstrap mapping, so
407 	 * we might lose a bit of the bootstrap address space.
408 	 */
409 	bootstrap_bs_map(NULL, (bus_addr_t)arg2, L1_S_SIZE, 0,
410 	    (bus_space_handle_t *)&config);
411 
412 	if (!fdt_init(config) || fdt_get_size(config) == 0)
413 		panic("initarm: no FDT");
414 
415 	node = fdt_find_node("/chosen");
416 	if (node != NULL) {
417 		char *prop;
418 		int len;
419 		static uint8_t lladdr[6];
420 
421 		len = fdt_node_property(node, "bootargs", &prop);
422 		if (len > 0)
423 			collect_kernel_args(prop);
424 
425 		len = fdt_node_property(node, "openbsd,boothowto", &prop);
426 		if (len == sizeof(boothowto))
427 			boothowto = bemtoh32((uint32_t *)prop);
428 
429 		len = fdt_node_property(node, "openbsd,bootduid", &prop);
430 		if (len == sizeof(bootduid))
431 			memcpy(bootduid, prop, sizeof(bootduid));
432 
433 		len = fdt_node_property(node, "openbsd,bootmac", &prop);
434 		if (len == sizeof(lladdr)) {
435 			memcpy(lladdr, prop, sizeof(lladdr));
436 			bootmac = lladdr;
437 		}
438 
439 		len = fdt_node_property(node, "openbsd,uefi-mmap-start", &prop);
440 		if (len == sizeof(mmap_start))
441 			mmap_start = bemtoh64((uint64_t *)prop);
442 		len = fdt_node_property(node, "openbsd,uefi-mmap-size", &prop);
443 		if (len == sizeof(mmap_size))
444 			mmap_size = bemtoh32((uint32_t *)prop);
445 		len = fdt_node_property(node, "openbsd,uefi-mmap-desc-size", &prop);
446 		if (len == sizeof(mmap_desc_size))
447 			mmap_desc_size = bemtoh32((uint32_t *)prop);
448 		len = fdt_node_property(node, "openbsd,uefi-mmap-desc-ver", &prop);
449 		if (len == sizeof(mmap_desc_ver))
450 			mmap_desc_ver = bemtoh32((uint32_t *)prop);
451 
452 		len = fdt_node_property(node, "openbsd,dma-constraint", &prop);
453 		if (len == sizeof(uint64_t[2])) {
454 			dma_constraint.ucr_low = bemtoh64((uint64_t *)prop);
455 			dma_constraint.ucr_high = bemtoh64((uint64_t *)prop + 1);
456 		}
457 	}
458 
459 	process_kernel_args();
460 
461 	if (mmap_start != 0)
462 		bootstrap_bs_map(NULL, mmap_start, mmap_size, 0,
463 		    (bus_space_handle_t *)&mmap);
464 
465 	platform_init();
466 
467 	/* setup a serial console for very early boot */
468 	consinit();
469 
470 	/* Talk to the user */
471 	printf("\nOpenBSD/armv7 booting ...\n");
472 
473 	printf("arg0 %p arg1 %p arg2 %p\n", arg0, arg1, arg2);
474 
475 #ifdef RAMDISK_HOOKS
476 	boothowto |= RB_DFLTROOT;
477 #endif /* RAMDISK_HOOKS */
478 
479 	physical_freestart = (((unsigned long)esym - KERNEL_TEXT_BASE + 0xfff) & ~0xfff) + loadaddr;
480 
481 	/* The bootloader has loaded us ubto a 32MB block. */
482 	memstart = loadaddr;
483 	memend = memstart + 32 * 1024 * 1024;
484 
485 	/*
486 	 * Okay, the kernel starts 2MB in from the bottom of physical
487 	 * memory.  We are going to allocate our bootstrap pages downwards
488 	 * from there.
489 	 *
490 	 * We need to allocate some fixed page tables to get the kernel
491 	 * going.  We allocate one page directory and a number of page
492 	 * tables and store the physical addresses in the kernel_pt_table
493 	 * array.
494 	 *
495 	 * The kernel page directory must be on a 16K boundary.  The page
496 	 * tables must be on 4K boundaries.  What we do is allocate the
497 	 * page directory on the first 16K boundary that we encounter, and
498 	 * the page tables on 4K boundaries otherwise.  Since we allocate
499 	 * at least 3 L2 page tables, we are guaranteed to encounter at
500 	 * least one 16K aligned region.
501 	 */
502 
503 #ifdef VERBOSE_INIT_ARM
504 	printf("Allocating page tables\n");
505 #endif
506 
507 	/* Define a macro to simplify memory allocation */
508 #define	valloc_pages(var, np)				\
509 	alloc_pages((var).pv_pa, (np));			\
510 	(var).pv_va = KERNEL_BASE + (var).pv_pa - loadaddr;
511 
512 #define alloc_pages(var, np)				\
513 	(var) = physical_freestart;			\
514 	physical_freestart += ((np) * PAGE_SIZE);	\
515 	if (physical_freestart > memend)		\
516 		panic("initarm: out of memory");	\
517 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
518 
519 	loop1 = 0;
520 	kernel_l1pt.pv_pa = 0;
521 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
522 		/* Are we 16KB aligned for an L1 ? */
523 		if (((physical_freestart) & (L1_TABLE_SIZE - 1)) == 0
524 		    && kernel_l1pt.pv_pa == 0) {
525 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
526 		} else {
527 			valloc_pages(kernel_pt_table[loop1],
528 			    L2_TABLE_SIZE / PAGE_SIZE);
529 			++loop1;
530 		}
531 	}
532 
533 	/* This should never be able to happen but better confirm that. */
534 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
535 		panic("initarm: Failed to align the kernel page directory");
536 
537 	/*
538 	 * Allocate a page for the system page mapped to V0x00000000
539 	 * This page will just contain the system vectors and can be
540 	 * shared by all processes.
541 	 */
542 	vector_page = ARM_VECTORS_HIGH;
543 	alloc_pages(systempage.pv_pa, 1);
544 	systempage.pv_va = vector_page;
545 
546 	/* Allocate stacks for all modes */
547 	valloc_pages(irqstack, IRQ_STACK_SIZE);
548 	valloc_pages(abtstack, ABT_STACK_SIZE);
549 	valloc_pages(undstack, UND_STACK_SIZE);
550 	valloc_pages(kernelstack, UPAGES);
551 
552 	/* Allocate enough pages for cleaning the Mini-Data cache. */
553 
554 #ifdef VERBOSE_INIT_ARM
555 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
556 	    irqstack.pv_va);
557 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
558 	    abtstack.pv_va);
559 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
560 	    undstack.pv_va);
561 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
562 	    kernelstack.pv_va);
563 #endif
564 
565 	/* Relocate the FDT to safe memory. */
566 	size = fdt_get_size(config);
567 	valloc_pages(fdt, round_page(size) / PAGE_SIZE);
568 	memcpy((void *)fdt.pv_pa, config, size);
569 
570 	/* Relocate the EFI memory map too. */
571 	if (mmap_start != 0) {
572 		valloc_pages(map, round_page(mmap_size) / PAGE_SIZE);
573 		memcpy((void *)map.pv_pa, mmap, mmap_size);
574 	}
575 
576 	/*
577 	 * XXX Defer this to later so that we can reclaim the memory
578 	 */
579 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
580 
581 	/*
582 	 * Ok we have allocated physical pages for the primary kernel
583 	 * page tables
584 	 */
585 
586 #ifdef VERBOSE_INIT_ARM
587 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
588 #endif
589 
590 	/*
591 	 * Now we start construction of the L1 page table
592 	 * We start by mapping the L2 page tables into the L1.
593 	 * This means that we can replace L1 mappings later on if necessary
594 	 */
595 	l1pagetable = kernel_l1pt.pv_pa;
596 
597 	/* Map the L2 pages tables in the L1 page table */
598 	pmap_link_l2pt(l1pagetable, vector_page & ~(0x00400000 - 1),
599 	    &kernel_pt_table[KERNEL_PT_SYS]);
600 
601 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
602 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
603 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
604 
605 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
606 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
607 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
608 
609 	/* update the top of the kernel VM */
610 	pmap_curmaxkvaddr =
611 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
612 
613 #ifdef VERBOSE_INIT_ARM
614 	printf("Mapping kernel\n");
615 #endif
616 
617 	/* Now we fill in the L2 pagetable for the kernel static code/data */
618 	{
619 		extern char __text_start[], _etext[];
620 		extern char __rodata_start[], _erodata[];
621 		size_t textsize = (u_int32_t) (_etext - __text_start);
622 		size_t rodatasize = (u_int32_t) (_erodata - __rodata_start);
623 		size_t totalsize = esym - (u_int32_t)__text_start;
624 		u_int logical;
625 
626 		textsize = (textsize + PGOFSET) & ~PGOFSET;
627 		rodatasize = (rodatasize + PGOFSET) & ~PGOFSET;
628 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
629 
630 		logical = 0x00300000;	/* offset of kernel in RAM */
631 
632 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
633 		    loadaddr + logical, textsize,
634 		    PROT_READ | PROT_EXEC, PTE_CACHE);
635 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
636 		    loadaddr + logical, rodatasize,
637 		    PROT_READ, PTE_CACHE);
638 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
639 		    loadaddr + logical, totalsize - (textsize + rodatasize),
640 		    PROT_READ | PROT_WRITE, PTE_CACHE);
641 	}
642 
643 #ifdef VERBOSE_INIT_ARM
644 	printf("Constructing L2 page tables\n");
645 #endif
646 
647 	/* Map the stack pages */
648 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
649 	    IRQ_STACK_SIZE * PAGE_SIZE, PROT_READ | PROT_WRITE, PTE_CACHE);
650 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
651 	    ABT_STACK_SIZE * PAGE_SIZE, PROT_READ | PROT_WRITE, PTE_CACHE);
652 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
653 	    UND_STACK_SIZE * PAGE_SIZE, PROT_READ | PROT_WRITE, PTE_CACHE);
654 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
655 	    UPAGES * PAGE_SIZE, PROT_READ | PROT_WRITE, PTE_CACHE);
656 
657 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
658 	    L1_TABLE_SIZE, PROT_READ | PROT_WRITE, PTE_PAGETABLE);
659 
660 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
661 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
662 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
663 		    PROT_READ | PROT_WRITE, PTE_PAGETABLE);
664 	}
665 
666 	/* Map the Mini-Data cache clean area. */
667 
668 	/* Map the vector page. */
669 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
670 	    PROT_READ | PROT_WRITE, PTE_CACHE);
671 
672 	/* Map the FDT. */
673 	pmap_map_chunk(l1pagetable, fdt.pv_va, fdt.pv_pa,
674 	    round_page(fdt_get_size((void *)fdt.pv_pa)),
675 	    PROT_READ | PROT_WRITE, PTE_CACHE);
676 
677 	/* Map the EFI memory map. */
678 	if (mmap_start != 0) {
679 		pmap_map_chunk(l1pagetable, map.pv_va, map.pv_pa,
680 		    round_page(mmap_size),
681 		    PROT_READ | PROT_WRITE, PTE_CACHE);
682 		mmap = (void *)map.pv_va;
683 	}
684 
685 	/*
686 	 * map integrated peripherals at same address in l1pagetable
687 	 * so that we can continue to use console.
688 	 */
689 	copy_io_area_map((pd_entry_t *)l1pagetable);
690 
691 	/*
692 	 * Now we have the real page tables in place so we can switch to them.
693 	 * Once this is done we will be running with the REAL kernel page
694 	 * tables.
695 	 */
696 	setttb(kernel_l1pt.pv_pa);
697 	cpu_tlb_flushID();
698 
699 	/*
700 	 * Moved from cpu_startup() as data_abort_handler() references
701 	 * this during uvm init
702 	 */
703 	proc0paddr = (struct user *)kernelstack.pv_va;
704 	proc0.p_addr = proc0paddr;
705 
706 	arm32_vector_init(vector_page, ARM_VEC_ALL);
707 
708 	/*
709 	 * Pages were allocated during the secondary bootstrap for the
710 	 * stacks for different CPU modes.
711 	 * We must now set the r13 registers in the different CPU modes to
712 	 * point to these stacks.
713 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
714 	 * of the stack memory.
715 	 */
716 
717 	set_stackptr(PSR_IRQ32_MODE,
718 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
719 	set_stackptr(PSR_ABT32_MODE,
720 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
721 	set_stackptr(PSR_UND32_MODE,
722 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
723 
724 	/*
725 	 * Well we should set a data abort handler.
726 	 * Once things get going this will change as we will need a proper
727 	 * handler.
728 	 * Until then we will use a handler that just panics but tells us
729 	 * why.
730 	 * Initialisation of the vectors will just panic on a data abort.
731 	 * This just fills in a slightly better one.
732 	 */
733 
734 	data_abort_handler_address = (u_int)data_abort_handler;
735 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
736 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
737 
738 	/* Now we can reinit the FDT, using the virtual address. */
739 	fdt_init((void *)fdt.pv_va);
740 
741 	/* Initialise the undefined instruction handlers */
742 #ifdef VERBOSE_INIT_ARM
743 	printf("undefined ");
744 #endif
745 	undefined_init();
746 
747 	/* Load memory into UVM. */
748 #ifdef VERBOSE_INIT_ARM
749 	printf("page ");
750 #endif
751 	uvm_setpagesize();        /* initialize PAGE_SIZE-dependent variables */
752 
753 	/* Make what's left of the initial 32MB block available to UVM. */
754 	uvm_page_physload(atop(physical_freestart), atop(memend),
755 	    atop(physical_freestart), atop(memend), 0);
756 	physmem = atop(memend - memstart);
757 
758 	/* Make all other physical memory available to UVM. */
759 	if (mmap && mmap_desc_ver == EFI_MEMORY_DESCRIPTOR_VERSION) {
760 		EFI_MEMORY_DESCRIPTOR *desc = mmap;
761 		int i;
762 
763 		/*
764 		 * Load all memory marked as EfiConventionalMemory.
765 		 * Don't bother with blocks smaller than 64KB.  The
766 		 * initial 64MB memory block should be marked as
767 		 * EfiLoaderData so it won't be added again here.
768 		 */
769 		for (i = 0; i < mmap_size / mmap_desc_size; i++) {
770 			printf("type 0x%x pa 0x%llx va 0x%llx pages 0x%llx attr 0x%llx\n",
771 			    desc->Type, desc->PhysicalStart,
772 			    desc->VirtualStart, desc->NumberOfPages,
773 			    desc->Attribute);
774 			if (desc->Type == EfiConventionalMemory &&
775 			    desc->NumberOfPages >= 16) {
776 				uvm_page_physload(atop(desc->PhysicalStart),
777 				    atop(desc->PhysicalStart) +
778 				    desc->NumberOfPages,
779 				    atop(desc->PhysicalStart),
780 				    atop(desc->PhysicalStart) +
781 				    desc->NumberOfPages, 0);
782 				physmem += desc->NumberOfPages;
783 			}
784 			desc = NextMemoryDescriptor(desc, mmap_desc_size);
785 		}
786 	} else {
787 		paddr_t start, end;
788 		int i;
789 
790 		node = fdt_find_node("/memory");
791 		if (node == NULL)
792 			panic("%s: no memory specified", __func__);
793 
794 		for (i = 0; i < VM_PHYSSEG_MAX; i++) {
795 			if (fdt_get_reg(node, i, &reg))
796 				break;
797 			if (reg.size == 0)
798 				continue;
799 
800 			start = reg.addr;
801 			end = MIN(reg.addr + reg.size, (paddr_t)-PAGE_SIZE);
802 
803 			/*
804 			 * The initial 32MB block is not excluded, so we need
805 			 * to make sure we don't add it here.
806 			 */
807 			if (start < memend && end > memstart) {
808 				if (start < memstart) {
809 					uvm_page_physload(atop(start),
810 					    atop(memstart), atop(start),
811 					    atop(memstart), 0);
812 					physmem += atop(memstart - start);
813 				}
814 				if (end > memend) {
815 					uvm_page_physload(atop(memend),
816 					    atop(end), atop(memend),
817 					    atop(end), 0);
818 					physmem += atop(end - memend);
819 				}
820 			} else {
821 				uvm_page_physload(atop(start), atop(end),
822 				    atop(start), atop(end), 0);
823 				physmem += atop(end - start);
824 			}
825 		}
826 	}
827 
828 	/* Boot strap pmap telling it where the kernel page table is */
829 #ifdef VERBOSE_INIT_ARM
830 	printf("pmap ");
831 #endif
832 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
833 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
834 
835 	vector_page_setprot(PROT_READ | PROT_EXEC);
836 
837 	/*
838 	 * Restore proper bus_space operation, now that pmap is initialized.
839 	 */
840 	armv7_bs_tag.bs_map = map_func_save;
841 
842 #ifdef DDB
843 	db_machine_init();
844 
845 	/* Firmware doesn't load symbols. */
846 	ddb_init();
847 
848 	if (boothowto & RB_KDB)
849 		db_enter();
850 #endif
851 
852 	cpu_setup();
853 
854 	/* We return the new stack pointer address */
855 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
856 }
857 
858 char	bootargs[256];
859 
860 void
collect_kernel_args(const char * args)861 collect_kernel_args(const char *args)
862 {
863 	/* Make a local copy of the bootargs */
864 	strlcpy(bootargs, args, sizeof(bootargs));
865 }
866 
867 void
process_kernel_args(void)868 process_kernel_args(void)
869 {
870 	char *cp = bootargs;
871 
872 	if (*cp == 0)
873 		return;
874 
875 	boot_file = bootargs;
876 
877 	/* Skip the kernel image filename */
878 	while (*cp != ' ' && *cp != 0)
879 		cp++;
880 
881 	if (*cp != 0)
882 		*cp++ = 0;
883 
884 	while (*cp == ' ')
885 		cp++;
886 
887 	boot_args = cp;
888 
889 	printf("bootfile: %s\n", boot_file);
890 	printf("bootargs: %s\n", boot_args);
891 
892 	/* Setup pointer to boot flags */
893 	while (*cp != '-')
894 		if (*cp++ == '\0')
895 			return;
896 
897 	while (*cp != 0) {
898 		switch(*cp) {
899 		case 'a':
900 			boothowto |= RB_ASKNAME;
901 			break;
902 		case 'c':
903 			boothowto |= RB_CONFIG;
904 			break;
905 		case 'd':
906 			boothowto |= RB_KDB;
907 			break;
908 		case 's':
909 			boothowto |= RB_SINGLE;
910 			break;
911 		default:
912 			printf("unknown option `%c'\n", *cp);
913 			break;
914 		}
915 		cp++;
916 	}
917 }
918 
919 static int
atoi(const char * s)920 atoi(const char *s)
921 {
922 	int n, neg;
923 
924 	n = 0;
925 	neg = 0;
926 
927 	while (*s == '-') {
928 		s++;
929 		neg = !neg;
930 	}
931 
932 	while (*s != '\0') {
933 		if (*s < '0' || *s > '9')
934 			break;
935 
936 		n = (10 * n) + (*s - '0');
937 		s++;
938 	}
939 
940 	return (neg ? -n : n);
941 }
942 
943 void *
fdt_find_cons(const char * name)944 fdt_find_cons(const char *name)
945 {
946 	char *alias = "serial0";
947 	char buf[128];
948 	char *stdout = NULL;
949 	char *p;
950 	void *node;
951 
952 	/* First check if "stdout-path" is set. */
953 	node = fdt_find_node("/chosen");
954 	if (node) {
955 		if (fdt_node_property(node, "stdout-path", &stdout) > 0) {
956 			if (strchr(stdout, ':') != NULL) {
957 				strlcpy(buf, stdout, sizeof(buf));
958 				if ((p = strchr(buf, ':')) != NULL) {
959 					*p++ = '\0';
960 					stdout_speed = atoi(p);
961 				}
962 				stdout = buf;
963 			}
964 			if (stdout[0] != '/') {
965 				/* It's an alias. */
966 				alias = stdout;
967 				stdout = NULL;
968 			}
969 		}
970 	}
971 
972 	/* Perform alias lookup if necessary. */
973 	if (stdout == NULL) {
974 		node = fdt_find_node("/aliases");
975 		if (node)
976 			fdt_node_property(node, alias, &stdout);
977 	}
978 
979 	/* Lookup the physical address of the interface. */
980 	if (stdout) {
981 		node = fdt_find_node(stdout);
982 		if (node && fdt_is_compatible(node, name)) {
983 			stdout_node = OF_finddevice(stdout);
984 			return (node);
985 		}
986 	}
987 
988 	return (NULL);
989 }
990 
991 void
consinit(void)992 consinit(void)
993 {
994 	static int consinit_called = 0;
995 
996 	if (consinit_called != 0)
997 		return;
998 
999 	consinit_called = 1;
1000 
1001 	platform_init_cons();
1002 }
1003 
1004 void
board_startup(void)1005 board_startup(void)
1006 {
1007         if (boothowto & RB_CONFIG) {
1008 #ifdef BOOT_CONFIG
1009 		user_config();
1010 #else
1011 		printf("kernel does not support -c; continuing..\n");
1012 #endif
1013 	}
1014 }
1015 
1016 unsigned int
cpu_rnd_messybits(void)1017 cpu_rnd_messybits(void)
1018 {
1019 	struct timespec ts;
1020 
1021 	nanotime(&ts);
1022 	return (ts.tv_nsec ^ (ts.tv_sec << 20));
1023 }
1024