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