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