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