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