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