1 /*	$NetBSD: integrator_machdep.c,v 1.22 2002/04/12 06:13:42 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 ARM Ltd
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the company may not be used to endorse or promote
16  *    products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * Copyright (c) 1997,1998 Mark Brinicombe.
32  * Copyright (c) 1997,1998 Causality Limited.
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Mark Brinicombe
46  *	for the NetBSD Project.
47  * 4. The name of the company nor the name of the author may be used to
48  *    endorse or promote products derived from this software without specific
49  *    prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
52  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
55  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  * Machine dependant functions for kernel setup for integrator board
64  *
65  * Created      : 24/11/97
66  */
67 
68 #include "opt_ddb.h"
69 #include "opt_pmap_debug.h"
70 
71 #include <sys/param.h>
72 #include <sys/device.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/exec.h>
76 #include <sys/proc.h>
77 #include <sys/msgbuf.h>
78 #include <sys/reboot.h>
79 #include <sys/termios.h>
80 
81 #include <dev/cons.h>
82 
83 #include <machine/db_machdep.h>
84 #include <ddb/db_sym.h>
85 #include <ddb/db_extern.h>
86 
87 #include <machine/bootconfig.h>
88 #include <machine/bus.h>
89 #include <machine/cpu.h>
90 #include <machine/frame.h>
91 #include <machine/intr.h>
92 #include <evbarm/ifpga/irqhandler.h>	/* XXX XXX XXX */
93 #include <arm/undefined.h>
94 
95 #include <evbarm/integrator/integrator_boot.h>
96 
97 #include "opt_ipkdb.h"
98 #include "pci.h"
99 
100 void ifpga_reset(void) __attribute__((noreturn));
101 /*
102  * Address to call from cpu_reset() to reset the machine.
103  * This is machine architecture dependant as it varies depending
104  * on where the ROM appears when you turn the MMU off.
105  */
106 
107 u_int cpu_reset_address = (u_int) ifpga_reset;
108 
109 /* Define various stack sizes in pages */
110 #define IRQ_STACK_SIZE	1
111 #define ABT_STACK_SIZE	1
112 #ifdef IPKDB
113 #define UND_STACK_SIZE	2
114 #else
115 #define UND_STACK_SIZE	1
116 #endif
117 
118 struct intbootinfo intbootinfo;
119 BootConfig bootconfig;		/* Boot config storage */
120 static char bootargs[MAX_BOOT_STRING + 1];
121 char *boot_args = NULL;
122 char *boot_file = NULL;
123 
124 vm_offset_t physical_start;
125 vm_offset_t physical_freestart;
126 vm_offset_t physical_freeend;
127 vm_offset_t physical_end;
128 u_int free_pages;
129 vm_offset_t pagetables_start;
130 int physmem = 0;
131 
132 /*int debug_flags;*/
133 #ifndef PMAP_STATIC_L1S
134 int max_processes = 64;			/* Default number */
135 #endif	/* !PMAP_STATIC_L1S */
136 
137 /* Physical and virtual addresses for some global pages */
138 pv_addr_t systempage;
139 pv_addr_t irqstack;
140 pv_addr_t undstack;
141 pv_addr_t abtstack;
142 pv_addr_t kernelstack;
143 
144 vm_offset_t msgbufphys;
145 
146 extern u_int data_abort_handler_address;
147 extern u_int prefetch_abort_handler_address;
148 extern u_int undefined_handler_address;
149 
150 #ifdef PMAP_DEBUG
151 extern int pmap_debug_level;
152 #endif
153 
154 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
155 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
156 #define KERNEL_PT_VMDATA	2	/* Page tables for mapping kernel VM */
157 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
158 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
159 
160 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
161 
162 struct user *proc0paddr;
163 
164 /* Prototypes */
165 
166 void consinit		__P((void));
167 
168 void process_kernel_args	__P((char *));
169 void data_abort_handler		__P((trapframe_t *frame));
170 void prefetch_abort_handler	__P((trapframe_t *frame));
171 void undefinedinstruction_bounce	__P((trapframe_t *frame));
172 extern void configure		__P((void));
173 extern void parse_mi_bootargs	__P((char *args));
174 extern void dumpsys		__P((void));
175 
176 /* A load of console goo. */
177 #include "vga.h"
178 #if (NVGA > 0)
179 #include <dev/ic/mc6845reg.h>
180 #include <dev/ic/pcdisplayvar.h>
181 #include <dev/ic/vgareg.h>
182 #include <dev/ic/vgavar.h>
183 #endif
184 
185 #include "pckbc.h"
186 #if (NPCKBC > 0)
187 #include <dev/ic/i8042reg.h>
188 #include <dev/ic/pckbcvar.h>
189 #endif
190 
191 #include "com.h"
192 #if (NCOM > 0)
193 #include <dev/ic/comreg.h>
194 #include <dev/ic/comvar.h>
195 #ifndef CONCOMADDR
196 #define CONCOMADDR 0x3f8
197 #endif
198 #endif
199 
200 #define CONSPEED B115200
201 #ifndef CONSPEED
202 #define CONSPEED B9600	/* TTYDEF_SPEED */
203 #endif
204 #ifndef CONMODE
205 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
206 #endif
207 
208 int comcnspeed = CONSPEED;
209 int comcnmode = CONMODE;
210 
211 #include "plcom.h"
212 #if (NPLCOM > 0)
213 #include <evbarm/dev/plcomreg.h>
214 #include <evbarm/dev/plcomvar.h>
215 
216 #include <evbarm/ifpga/ifpgamem.h>
217 #include <evbarm/ifpga/ifpgareg.h>
218 #include <evbarm/ifpga/ifpgavar.h>
219 #endif
220 
221 #ifndef CONSDEVNAME
222 #define CONSDEVNAME "plcom"
223 #endif
224 
225 #ifndef PLCONSPEED
226 #define PLCONSPEED B38400
227 #endif
228 #ifndef PLCONMODE
229 #define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
230 #endif
231 #ifndef PLCOMCNUNIT
232 #define PLCOMCNUNIT -1
233 #endif
234 
235 int plcomcnspeed = PLCONSPEED;
236 int plcomcnmode = PLCONMODE;
237 
238 #if 0
239 extern struct consdev kcomcons;
240 static void kcomcnputc(dev_t, int);
241 #endif
242 
243 /*
244  * void cpu_reboot(int howto, char *bootstr)
245  *
246  * Reboots the system
247  *
248  * Deal with any syncing, unmounting, dumping and shutdown hooks,
249  * then reset the CPU.
250  */
251 
252 void
253 cpu_reboot(howto, bootstr)
254 	int howto;
255 	char *bootstr;
256 {
257 #ifdef DIAGNOSTIC
258 	/* info */
259 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
260 #endif
261 
262 	/*
263 	 * If we are still cold then hit the air brakes
264 	 * and crash to earth fast
265 	 */
266 	if (cold) {
267 		doshutdownhooks();
268 		printf("The operating system has halted.\n");
269 		printf("Please press any key to reboot.\n\n");
270 		cngetc();
271 		printf("rebooting...\n");
272 		ifpga_reset();
273 		/*NOTREACHED*/
274 	}
275 
276 	/* Disable console buffering */
277 /*	cnpollc(1);*/
278 
279 	/*
280 	 * If RB_NOSYNC was not specified sync the discs.
281 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
282 	 * It looks like syslogd is getting woken up only to find that it cannot
283 	 * page part of the binary in as the filesystem has been unmounted.
284 	 */
285 	if (!(howto & RB_NOSYNC))
286 		bootsync();
287 
288 	/* Say NO to interrupts */
289 	splhigh();
290 
291 	/* Do a dump if requested. */
292 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
293 		dumpsys();
294 
295 	/* Run any shutdown hooks */
296 	doshutdownhooks();
297 
298 	/* Make sure IRQ's are disabled */
299 	IRQdisable;
300 
301 	if (howto & RB_HALT) {
302 		printf("The operating system has halted.\n");
303 		printf("Please press any key to reboot.\n\n");
304 		cngetc();
305 	}
306 
307 	printf("rebooting...\n");
308 	ifpga_reset();
309 	/*NOTREACHED*/
310 }
311 
312 /*
313  * Mapping table for core kernel memory. This memory is mapped at init
314  * time with section mappings.
315  */
316 struct l1_sec_map {
317 	vm_offset_t	va;
318 	vm_offset_t	pa;
319 	vm_size_t	size;
320 	vm_prot_t	prot;
321 	int		cache;
322 } l1_sec_table[] = {
323 #if NPLCOM > 0 && defined(PLCONSOLE)
324 	{ UART0_BOOT_BASE, IFPGA_IO_BASE + IFPGA_UART0, 1024 * 1024,
325 	  VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE },
326 	{ UART1_BOOT_BASE, IFPGA_IO_BASE + IFPGA_UART1, 1024 * 1024,
327 	  VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE },
328 #endif
329 #if NPCI > 0
330 	{ IFPGA_PCI_IO_VBASE, IFPGA_PCI_IO_BASE, IFPGA_PCI_IO_VSIZE,
331 	  VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE },
332 	{ IFPGA_PCI_CONF_VBASE, IFPGA_PCI_CONF_BASE, IFPGA_PCI_CONF_VSIZE,
333 	  VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE },
334 #endif
335 
336 	{ 0, 0, 0, 0, 0 }
337 };
338 
339 /*
340  * u_int initarm(struct ebsaboot *bootinfo)
341  *
342  * Initial entry point on startup. This gets called before main() is
343  * entered.
344  * It should be responsible for setting up everything that must be
345  * in place when main is called.
346  * This includes
347  *   Taking a copy of the boot configuration structure.
348  *   Initialising the physical console so characters can be printed.
349  *   Setting up page tables for the kernel
350  *   Relocating the kernel to the bottom of physical memory
351  */
352 
353 u_int
354 initarm(bootinfo)
355 	struct intbootinfo *bootinfo;
356 {
357 	int loop;
358 	int loop1;
359 	u_int l1pagetable;
360 	extern int etext asm ("_etext");
361 	extern int end asm ("_end");
362 	pv_addr_t kernel_l1pt;
363 	pv_addr_t kernel_ptpt;
364 #if NPLCOM > 0 && defined(PLCONSOLE)
365 	static struct bus_space plcom_bus_space;
366 #endif
367 
368 
369 #if 0
370 	cn_tab = &kcomcons;
371 #endif
372 	/*
373 	 * Heads up ... Setup the CPU / MMU / TLB functions
374 	 */
375 	if (set_cpufuncs())
376 		panic("cpu not recognized!");
377 
378 	/*    - intbootinfo.bt_memstart) / NBPG */;
379 
380 #if NPLCOM > 0 && defined(PLCONSOLE)
381 	/*
382 	 * Initialise the diagnostic serial console
383 	 * This allows a means of generating output during initarm().
384 	 * Once all the memory map changes are complete we can call consinit()
385 	 * and not have to worry about things moving.
386 	 */
387 
388 	if (PLCOMCNUNIT == 0) {
389 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd600000);
390 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
391 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
392 	} else if (PLCOMCNUNIT == 1) {
393 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd700000);
394 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
395 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
396 	}
397 #endif
398 
399 	/* Talk to the user */
400 	printf("\nNetBSD/integrator booting ...\n");
401 
402 #if 0
403 	if (intbootinfo.bt_magic != BT_MAGIC_NUMBER_EBSA
404 	    && intbootinfo.bt_magic != BT_MAGIC_NUMBER_CATS)
405 		panic("Incompatible magic number passed in boot args\n");
406 #endif
407 
408 /*	{
409 	int loop;
410 	for (loop = 0; loop < 8; ++loop) {
411 		printf("%08x\n", *(((int *)bootinfo)+loop));
412 	}
413 	}*/
414 
415 	/*
416 	 * Ok we have the following memory map
417 	 *
418 	 * virtual address == physical address apart from the areas:
419 	 * 0x00000000 -> 0x000fffff which is mapped to
420 	 * top 1MB of physical memory
421 	 * 0x00100000 -> 0x0fffffff which is mapped to
422 	 * physical addresses 0x00100000 -> 0x0fffffff
423 	 * 0x10000000 -> 0x1fffffff which is mapped to
424 	 * physical addresses 0x00000000 -> 0x0fffffff
425 	 * 0x20000000 -> 0xefffffff which is mapped to
426 	 * physical addresses 0x20000000 -> 0xefffffff
427 	 * 0xf0000000 -> 0xf03fffff which is mapped to
428 	 * physical addresses 0x00000000 -> 0x003fffff
429 	 *
430 	 * This means that the kernel is mapped suitably for continuing
431 	 * execution, all I/O is mapped 1:1 virtual to physical and
432 	 * physical memory is accessible.
433 	 *
434 	 * The initarm() has the responsibility for creating the kernel
435 	 * page tables.
436 	 * It must also set up various memory pointers that are used
437 	 * by pmap etc.
438 	 */
439 
440 	/*
441 	 * Examine the boot args string for options we need to know about
442 	 * now.
443 	 */
444 #if 0
445 	process_kernel_args((char *)intbootinfo.bt_args);
446 #endif
447 
448 	printf("initarm: Configuring system ...\n");
449 
450 	/*
451 	 * Set up the variables that define the availablilty of
452 	 * physical memory
453 	 */
454 	physical_start = 0 /*intbootinfo.bt_memstart*/;
455 	physical_freestart = physical_start;
456 
457 #if 0
458 	physical_end = /*intbootinfo.bt_memend*/ /*intbootinfo.bi_nrpages * NBPG */ 32*1024*1024;
459 #else
460 	{
461 		volatile unsigned long *cm_sdram
462 		    = (volatile unsigned long *)0x10000020;
463 
464 		switch ((*cm_sdram >> 2) & 0x7)
465 		{
466 		case 0:
467 			physical_end = 16 * 1024 * 1024;
468 			break;
469 		case 1:
470 			physical_end = 32 * 1024 * 1024;
471 			break;
472 		case 2:
473 			physical_end = 64 * 1024 * 1024;
474 			break;
475 		case 3:
476 			physical_end = 128 * 1024 * 1024;
477 			break;
478 		case 4:
479 			physical_end = 256 * 1024 * 1024;
480 			break;
481 		default:
482 			printf("CM_SDRAM retuns unknown value, using 16M\n");
483 			physical_end = 16 * 1024 * 1024;
484 			break;
485 		}
486 	}
487 #endif
488 
489 	physical_freeend = physical_end;
490 	free_pages = (physical_end - physical_start) / NBPG;
491 
492 	/* Set up the bootconfig structure for the benefit of pmap.c */
493 	bootconfig.dramblocks = 1;
494 	bootconfig.dram[0].address = physical_start;
495 	bootconfig.dram[0].pages = free_pages;
496 
497 	physmem = (physical_end - physical_start) / NBPG;
498 
499 	/* Tell the user about the memory */
500 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
501 	    physical_start, physical_end - 1);
502 
503 	/*
504 	 * Ok the kernel occupies the bottom of physical memory.
505 	 * The first free page after the kernel can be found in
506 	 * intbootinfo->bt_memavail
507 	 * We now need to allocate some fixed page tables to get the kernel
508 	 * going.
509 	 * We allocate one page directory and a number page tables and store
510 	 * the physical addresses in the kernel_pt_table array.
511 	 *
512 	 * Ok the next bit of physical allocation may look complex but it is
513 	 * simple really. I have done it like this so that no memory gets
514 	 * wasted during the allocation of various pages and tables that are
515 	 * all different sizes.
516 	 * The start addresses will be page aligned.
517 	 * We allocate the kernel page directory on the first free 16KB boundry
518 	 * we find.
519 	 * We allocate the kernel page tables on the first 4KB boundry we find.
520 	 * Since we allocate at least 3 L2 pagetables we know that we must
521 	 * encounter at least one 16KB aligned address.
522 	 */
523 
524 #ifdef VERBOSE_INIT_ARM
525 	printf("Allocating page tables\n");
526 #endif
527 
528 	/* Update the address of the first free 16KB chunk of physical memory */
529         physical_freestart = ((uintptr_t) &end - KERNEL_TEXT_BASE + PGOFSET)
530 	    & ~PGOFSET;
531 #if 0
532         physical_freestart += (kernexec->a_syms + sizeof(int)
533 		    + *(u_int *)((int)end + kernexec->a_syms + sizeof(int))
534 		    + (NBPG - 1)) & ~(NBPG - 1);
535 #endif
536 
537 	free_pages -= (physical_freestart - physical_start) / NBPG;
538 #ifdef VERBOSE_INIT_ARM
539 	printf("freestart = %#lx, free_pages = %d (%#x)\n",
540 	       physical_freestart, free_pages, free_pages);
541 #endif
542 
543 	/* Define a macro to simplify memory allocation */
544 #define	valloc_pages(var, np)			\
545 	alloc_pages((var).pv_pa, (np));		\
546 	(var).pv_va = KERNEL_TEXT_BASE + (var).pv_pa - physical_start;
547 
548 #define alloc_pages(var, np)			\
549 	(var) = physical_freestart;		\
550 	physical_freestart += ((np) * NBPG);	\
551 	free_pages -= (np);			\
552 	memset((char *)(var), 0, ((np) * NBPG));
553 
554 	loop1 = 0;
555 	kernel_l1pt.pv_pa = 0;
556 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
557 		/* Are we 16KB aligned for an L1 ? */
558 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
559 		    && kernel_l1pt.pv_pa == 0) {
560 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
561 		} else {
562 			alloc_pages(kernel_pt_table[loop1].pv_pa,
563 			    L2_TABLE_SIZE / NBPG);
564 			++loop1;
565 			kernel_pt_table[loop1].pv_va =
566 			    kernel_pt_table[loop1].pv_pa;
567 		}
568 	}
569 
570 	/* This should never be able to happen but better confirm that. */
571 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
572 		panic("initarm: Failed to align the kernel page directory\n");
573 
574 	/*
575 	 * Allocate a page for the system page mapped to V0x00000000
576 	 * This page will just contain the system vectors and can be
577 	 * shared by all processes.
578 	 */
579 	alloc_pages(systempage.pv_pa, 1);
580 
581 	/* Allocate a page for the page table to map kernel page tables*/
582 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
583 
584 	/* Allocate stacks for all modes */
585 	valloc_pages(irqstack, IRQ_STACK_SIZE);
586 	valloc_pages(abtstack, ABT_STACK_SIZE);
587 	valloc_pages(undstack, UND_STACK_SIZE);
588 	valloc_pages(kernelstack, UPAGES);
589 
590 #ifdef VERBOSE_INIT_ARM
591 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
592 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
593 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
594 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
595 #endif
596 
597 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
598 
599 	/*
600 	 * Ok we have allocated physical pages for the primary kernel
601 	 * page tables
602 	 */
603 
604 #ifdef VERBOSE_INIT_ARM
605 	printf("Creating L1 page table at %#lx\n", kernel_l1pt.pv_pa);
606 #endif
607 
608 	/*
609 	 * Now we start consturction of the L1 page table
610 	 * We start by mapping the L2 page tables into the L1.
611 	 * This means that we can replace L1 mappings later on if necessary
612 	 */
613 	l1pagetable = kernel_l1pt.pv_pa;
614 
615 	/* Map the L2 pages tables in the L1 page table */
616 	pmap_link_l2pt(l1pagetable, 0x00000000,
617 	    &kernel_pt_table[KERNEL_PT_SYS]);
618 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
619 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
620 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
621 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
622 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
623 	pmap_link_l2pt(l1pagetable, PTE_BASE,
624 	    &kernel_ptpt);
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 	{
637 		u_int logical;
638 		size_t textsize = (uintptr_t) &etext - KERNEL_TEXT_BASE;
639 		size_t totalsize = (uintptr_t) &end - KERNEL_TEXT_BASE;
640 
641 		/* Round down text size and round up total size
642 		 */
643 		textsize = textsize & ~PGOFSET;
644 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
645 		/* logical = pmap_map_chunk(l1pagetable,
646 		    KERNEL_BASE, physical_start, KERNEL_TEXT_BASE - KERNEL_BASE,
647 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); */
648 		logical = pmap_map_chunk(l1pagetable,
649 		    KERNEL_TEXT_BASE, physical_start, textsize,
650 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
651 		logical += pmap_map_chunk(l1pagetable,
652 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
653 		    totalsize - textsize, VM_PROT_READ|VM_PROT_WRITE,
654 		    PTE_CACHE);
655 #if 0
656 		logical += pmap_map_chunk(l1pagetable,
657 		    KERNEL_BASE + logical,
658 		    physical_start + logical, kernexec->a_syms + sizeof(int)
659 		    + *(u_int *)((int)end + kernexec->a_syms + sizeof(int)),
660 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
661 #endif
662 	}
663 
664 #ifdef VERBOSE_INIT_ARM
665 	printf("Constructing L2 page tables\n");
666 #endif
667 
668 	/* Map the boot arguments page */
669 #if 0
670 	pmap_map_entry(l1pagetable, intbootinfo.bt_vargp,
671 	    intbootinfo.bt_pargp, VM_PROT_READ, PTE_CACHE);
672 #endif
673 
674 	/* Map the stack pages */
675 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
676 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
677 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
678 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
679 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
680 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
681 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
682 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
683 
684 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
685 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
686 
687 	/* Map the page table that maps the kernel pages */
688 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
689 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
690 
691 	/*
692 	 * Map entries in the page table used to map PTE's
693 	 * Basically every kernel page table gets mapped here
694 	 */
695 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
696 	pmap_map_entry(l1pagetable,
697 	    PTE_BASE + (KERNEL_BASE >> (PGSHIFT-2)),
698 	    kernel_pt_table[KERNEL_PT_KERNEL].pv_pa,
699 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
700 	pmap_map_entry(l1pagetable,
701 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
702 	    kernel_ptpt.pv_pa,
703 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
704 	pmap_map_entry(l1pagetable,
705 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
706 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
707 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
708 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
709 		pmap_map_entry(l1pagetable,
710 		    PTE_BASE + ((KERNEL_VM_BASE +
711 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
712 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
713 		    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
714 
715 	/* Map the vector page. */
716 #if 1
717 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download
718 	   the cache-clean code there.  */
719 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
720 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
721 #else
722 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
723 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
724 #endif
725 	/* Map the core memory needed before autoconfig */
726 	loop = 0;
727 	while (l1_sec_table[loop].size) {
728 		vm_size_t sz;
729 
730 #ifdef VERBOSE_INIT_ARM
731 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
732 		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
733 		    l1_sec_table[loop].va);
734 #endif
735 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
736 			pmap_map_section(l1pagetable,
737 			    l1_sec_table[loop].va + sz,
738 			    l1_sec_table[loop].pa + sz,
739 			    l1_sec_table[loop].prot,
740 			    l1_sec_table[loop].cache);
741 		++loop;
742 	}
743 
744 	/*
745 	 * Now we have the real page tables in place so we can switch to them.
746 	 * Once this is done we will be running with the REAL kernel page tables.
747 	 */
748 
749 	/* Switch tables */
750 #ifdef VERBOSE_INIT_ARM
751 	printf("freestart = %#lx, free_pages = %d (%#x)\n",
752 	       physical_freestart, free_pages, free_pages);
753 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
754 #endif
755 
756 	setttb(kernel_l1pt.pv_pa);
757 
758 #ifdef VERBOSE_INIT_ARM
759 	printf("done!\n");
760 #endif
761 
762 #ifdef PLCONSOLE
763 	/*
764 	 * The IFPGA registers have just moved.
765 	 * Detach the diagnostic serial port and reattach at the new address.
766 	 */
767 	plcomcndetach();
768 #endif
769 
770 	/*
771 	 * XXX this should only be done in main() but it useful to
772 	 * have output earlier ...
773 	 */
774 	consinit();
775 
776 #ifdef VERBOSE_INIT_ARM
777 	printf("bootstrap done.\n");
778 #endif
779 
780 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
781 
782 	/*
783 	 * Pages were allocated during the secondary bootstrap for the
784 	 * stacks for different CPU modes.
785 	 * We must now set the r13 registers in the different CPU modes to
786 	 * point to these stacks.
787 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
788 	 * of the stack memory.
789 	 */
790 	printf("init subsystems: stacks ");
791 
792 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
793 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
794 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
795 
796 	/*
797 	 * Well we should set a data abort handler.
798 	 * Once things get going this will change as we will need a proper handler.
799 	 * Until then we will use a handler that just panics but tells us
800 	 * why.
801 	 * Initialisation of the vectors will just panic on a data abort.
802 	 * This just fills in a slighly better one.
803 	 */
804 	printf("vectors ");
805 	data_abort_handler_address = (u_int)data_abort_handler;
806 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
807 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
808 
809 	/* At last !
810 	 * We now have the kernel in physical memory from the bottom upwards.
811 	 * Kernel page tables are physically above this.
812 	 * The kernel is mapped to KERNEL_TEXT_BASE
813 	 * The kernel data PTs will handle the mapping of 0xf1000000-0xf3ffffff
814 	 * The page tables are mapped to 0xefc00000
815 	 */
816 
817 	/* Initialise the undefined instruction handlers */
818 	printf("undefined ");
819 	undefined_init();
820 
821 	/* Boot strap pmap telling it where the kernel page table is */
822 	printf("pmap ");
823 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
824 
825 	/* Setup the IRQ system */
826 	printf("irq ");
827 	irq_init();
828 
829 	printf("done.\n");
830 
831 #ifdef IPKDB
832 	/* Initialise ipkdb */
833 	ipkdb_init();
834 	if (boothowto & RB_KDB)
835 		ipkdb_connect(0);
836 #endif
837 
838 #ifdef DDB
839 	db_machine_init();
840 
841 	/* Firmware doesn't load symbols. */
842 	ddb_init(0, NULL, NULL);
843 
844 	if (boothowto & RB_KDB)
845 		Debugger();
846 #endif
847 
848 	/* We return the new stack pointer address */
849 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
850 }
851 
852 void
853 process_kernel_args(args)
854 	char *args;
855 {
856 
857 	boothowto = 0;
858 
859 	/* Make a local copy of the bootargs */
860 	strncpy(bootargs, args, MAX_BOOT_STRING);
861 
862 	args = bootargs;
863 	boot_file = bootargs;
864 
865 	/* Skip the kernel image filename */
866 	while (*args != ' ' && *args != 0)
867 		++args;
868 
869 	if (*args != 0)
870 		*args++ = 0;
871 
872 	while (*args == ' ')
873 		++args;
874 
875 	boot_args = args;
876 
877 	printf("bootfile: %s\n", boot_file);
878 	printf("bootargs: %s\n", boot_args);
879 
880 	parse_mi_bootargs(boot_args);
881 }
882 
883 void
884 consinit(void)
885 {
886 	static int consinit_called = 0;
887 #if NPLCOM > 0 && defined(PLCONSOLE)
888 	static struct bus_space plcom_bus_space;
889 #endif
890 #if 0
891 	char *console = CONSDEVNAME;
892 #endif
893 
894 	if (consinit_called != 0)
895 		return;
896 
897 	consinit_called = 1;
898 
899 #if NPLCOM > 0 && defined(PLCONSOLE)
900 	if (PLCOMCNUNIT == 0) {
901 		ifpga_create_io_bs_tag(&plcom_bus_space,
902 		    (void*)UART0_BOOT_BASE);
903 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
904 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
905 			panic("can't init serial console");
906 		return;
907 	} else if (PLCOMCNUNIT == 1) {
908 		ifpga_create_io_bs_tag(&plcom_bus_space,
909 		    (void*)UART0_BOOT_BASE);
910 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
911 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
912 			panic("can't init serial console");
913 		return;
914 	}
915 #endif
916 #if (NCOM > 0)
917 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
918 	    COM_FREQ, comcnmode))
919 		panic("can't init serial console @%x", CONCOMADDR);
920 	return;
921 #endif
922 	panic("No serial console configured");
923 }
924