Home | History | Annotate | Line # | Download | only in integrator
integrator_machdep.c revision 1.6
      1 /*	$NetBSD: integrator_machdep.c,v 1.6 2001/11/23 21:18:34 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 <arm/undefined.h>
     93 
     94 #include <evbarm/integrator/integrator_boot.h>
     95 
     96 #include "opt_ipkdb.h"
     97 #include "pci.h"
     98 
     99 void ifpga_reset(void) __attribute__((noreturn));
    100 /*
    101  * Address to call from cpu_reset() to reset the machine.
    102  * This is machine architecture dependant as it varies depending
    103  * on where the ROM appears when you turn the MMU off.
    104  */
    105 
    106 u_int cpu_reset_address = (u_int) ifpga_reset;
    107 
    108 /* Define various stack sizes in pages */
    109 #define IRQ_STACK_SIZE	1
    110 #define ABT_STACK_SIZE	1
    111 #ifdef IPKDB
    112 #define UND_STACK_SIZE	2
    113 #else
    114 #define UND_STACK_SIZE	1
    115 #endif
    116 
    117 struct intbootinfo intbootinfo;
    118 BootConfig bootconfig;		/* Boot config storage */
    119 static char bootargs[MAX_BOOT_STRING + 1];
    120 char *boot_args = NULL;
    121 char *boot_file = NULL;
    122 
    123 vm_offset_t physical_start;
    124 vm_offset_t physical_freestart;
    125 vm_offset_t physical_freeend;
    126 vm_offset_t physical_end;
    127 u_int free_pages;
    128 vm_offset_t pagetables_start;
    129 int physmem = 0;
    130 
    131 /*int debug_flags;*/
    132 #ifndef PMAP_STATIC_L1S
    133 int max_processes = 64;			/* Default number */
    134 #endif	/* !PMAP_STATIC_L1S */
    135 
    136 /* Physical and virtual addresses for some global pages */
    137 pv_addr_t systempage;
    138 pv_addr_t irqstack;
    139 pv_addr_t undstack;
    140 pv_addr_t abtstack;
    141 pv_addr_t kernelstack;
    142 
    143 vm_offset_t msgbufphys;
    144 
    145 extern u_int data_abort_handler_address;
    146 extern u_int prefetch_abort_handler_address;
    147 extern u_int undefined_handler_address;
    148 
    149 #ifdef PMAP_DEBUG
    150 extern int pmap_debug_level;
    151 #endif
    152 
    153 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    154 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    155 #define KERNEL_PT_VMDATA	2	/* Page tables for mapping kernel VM */
    156 #define	KERNEL_PT_VMDATA_NUM	(KERNEL_VM_SIZE >> (PDSHIFT + 2))
    157 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    158 
    159 pt_entry_t kernel_pt_table[NUM_KERNEL_PTS];
    160 
    161 struct user *proc0paddr;
    162 
    163 /* Prototypes */
    164 
    165 void consinit		__P((void));
    166 
    167 void map_section	__P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa,
    168 			     int cacheable));
    169 void map_pagetable	__P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
    170 void map_entry		__P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
    171 void map_entry_nc	__P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
    172 void map_entry_ro	__P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
    173 vm_size_t map_chunk	__P((vm_offset_t pd, vm_offset_t pt, vm_offset_t va,
    174 			     vm_offset_t pa, vm_size_t size, u_int acc,
    175 			     u_int flg));
    176 
    177 void process_kernel_args	__P((char *));
    178 void data_abort_handler		__P((trapframe_t *frame));
    179 void prefetch_abort_handler	__P((trapframe_t *frame));
    180 void undefinedinstruction_bounce	__P((trapframe_t *frame));
    181 extern void configure		__P((void));
    182 extern void parse_mi_bootargs	__P((char *args));
    183 extern void dumpsys		__P((void));
    184 
    185 /* A load of console goo. */
    186 #include "vga.h"
    187 #if (NVGA > 0)
    188 #include <dev/ic/mc6845reg.h>
    189 #include <dev/ic/pcdisplayvar.h>
    190 #include <dev/ic/vgareg.h>
    191 #include <dev/ic/vgavar.h>
    192 #endif
    193 
    194 #include "pckbc.h"
    195 #if (NPCKBC > 0)
    196 #include <dev/ic/i8042reg.h>
    197 #include <dev/ic/pckbcvar.h>
    198 #endif
    199 
    200 #include "com.h"
    201 #if (NCOM > 0)
    202 #include <dev/ic/comreg.h>
    203 #include <dev/ic/comvar.h>
    204 #ifndef CONCOMADDR
    205 #define CONCOMADDR 0x3f8
    206 #endif
    207 #endif
    208 
    209 #define CONSPEED B115200
    210 #ifndef CONSPEED
    211 #define CONSPEED B9600	/* TTYDEF_SPEED */
    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 #include "plcom.h"
    221 #if (NPLCOM > 0)
    222 #include <evbarm/dev/plcomreg.h>
    223 #include <evbarm/dev/plcomvar.h>
    224 
    225 #include <evbarm/ifpga/ifpgamem.h>
    226 #include <evbarm/ifpga/ifpgareg.h>
    227 #include <evbarm/ifpga/ifpgavar.h>
    228 #endif
    229 
    230 #ifndef CONSDEVNAME
    231 #define CONSDEVNAME "plcom"
    232 #endif
    233 
    234 #ifndef PLCONSPEED
    235 #define PLCONSPEED B38400
    236 #endif
    237 #ifndef PLCONMODE
    238 #define PLCONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    239 #endif
    240 #ifndef PLCOMCNUNIT
    241 #define PLCOMCNUNIT -1
    242 #endif
    243 
    244 int plcomcnspeed = PLCONSPEED;
    245 int plcomcnmode = PLCONMODE;
    246 
    247 #if 0
    248 extern struct consdev kcomcons;
    249 static void kcomcnputc(dev_t, int);
    250 #endif
    251 
    252 /*
    253  * void cpu_reboot(int howto, char *bootstr)
    254  *
    255  * Reboots the system
    256  *
    257  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    258  * then reset the CPU.
    259  */
    260 
    261 void
    262 cpu_reboot(howto, bootstr)
    263 	int howto;
    264 	char *bootstr;
    265 {
    266 #ifdef DIAGNOSTIC
    267 	/* info */
    268 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    269 #endif
    270 
    271 	/*
    272 	 * If we are still cold then hit the air brakes
    273 	 * and crash to earth fast
    274 	 */
    275 	if (cold) {
    276 		doshutdownhooks();
    277 		printf("The operating system has halted.\n");
    278 		printf("Please press any key to reboot.\n\n");
    279 		cngetc();
    280 		printf("rebooting...\n");
    281 		ifpga_reset();
    282 		/*NOTREACHED*/
    283 	}
    284 
    285 	/* Disable console buffering */
    286 /*	cnpollc(1);*/
    287 
    288 	/*
    289 	 * If RB_NOSYNC was not specified sync the discs.
    290 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
    291 	 * It looks like syslogd is getting woken up only to find that it cannot
    292 	 * page part of the binary in as the filesystem has been unmounted.
    293 	 */
    294 	if (!(howto & RB_NOSYNC))
    295 		bootsync();
    296 
    297 	/* Say NO to interrupts */
    298 	splhigh();
    299 
    300 	/* Do a dump if requested. */
    301 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    302 		dumpsys();
    303 
    304 	/* Run any shutdown hooks */
    305 	doshutdownhooks();
    306 
    307 	/* Make sure IRQ's are disabled */
    308 	IRQdisable;
    309 
    310 	if (howto & RB_HALT) {
    311 		printf("The operating system has halted.\n");
    312 		printf("Please press any key to reboot.\n\n");
    313 		cngetc();
    314 	}
    315 
    316 	printf("rebooting...\n");
    317 	ifpga_reset();
    318 	/*NOTREACHED*/
    319 }
    320 
    321 /*
    322  * Mapping table for core kernel memory. This memory is mapped at init
    323  * time with section mappings.
    324  */
    325 struct l1_sec_map {
    326 	vm_offset_t	va;
    327 	vm_offset_t	pa;
    328 	vm_size_t	size;
    329 	int		flags;
    330 } l1_sec_table[] = {
    331 #if NPLCOM > 0 && defined(PLCONSOLE)
    332 	{ UART0_BOOT_BASE, IFPGA_IO_BASE + IFPGA_UART0, 1024 * 1024, 0},
    333 	{ UART1_BOOT_BASE, IFPGA_IO_BASE + IFPGA_UART1, 1024 * 1024, 0},
    334 #endif
    335 #if NPCI > 0
    336 	{ IFPGA_PCI_IO_VBASE, IFPGA_PCI_IO_BASE, IFPGA_PCI_IO_VSIZE, 0},
    337 	{ IFPGA_PCI_CONF_VBASE, IFPGA_PCI_CONF_BASE, IFPGA_PCI_CONF_VSIZE, 0},
    338 #endif
    339 
    340 	{ 0, 0, 0, 0 }
    341 };
    342 
    343 /*
    344  * u_int initarm(struct ebsaboot *bootinfo)
    345  *
    346  * Initial entry point on startup. This gets called before main() is
    347  * entered.
    348  * It should be responsible for setting up everything that must be
    349  * in place when main is called.
    350  * This includes
    351  *   Taking a copy of the boot configuration structure.
    352  *   Initialising the physical console so characters can be printed.
    353  *   Setting up page tables for the kernel
    354  *   Relocating the kernel to the bottom of physical memory
    355  */
    356 
    357 u_int
    358 initarm(bootinfo)
    359 	struct intbootinfo *bootinfo;
    360 {
    361 	int loop;
    362 	int loop1;
    363 	u_int l1pagetable;
    364 	u_int l2pagetable;
    365 	extern char page0[], page0_end[];
    366 	extern int etext asm ("_etext");
    367 	extern int end asm ("_end");
    368 	pv_addr_t kernel_l1pt;
    369 	pv_addr_t kernel_ptpt;
    370 #if NPLCOM > 0 && defined(PLCONSOLE)
    371 	static struct bus_space plcom_bus_space;
    372 #endif
    373 
    374 
    375 #if 0
    376 	cn_tab = &kcomcons;
    377 #endif
    378 	/*
    379 	 * Heads up ... Setup the CPU / MMU / TLB functions
    380 	 */
    381 	if (set_cpufuncs())
    382 		panic("cpu not recognized!");
    383 
    384 	/*    - intbootinfo.bt_memstart) / NBPG */;
    385 
    386 #if NPLCOM > 0 && defined(PLCONSOLE)
    387 	/*
    388 	 * Initialise the diagnostic serial console
    389 	 * This allows a means of generating output during initarm().
    390 	 * Once all the memory map changes are complete we can call consinit()
    391 	 * and not have to worry about things moving.
    392 	 */
    393 
    394 	if (PLCOMCNUNIT == 0) {
    395 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd600000);
    396 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
    397 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
    398 	} else if (PLCOMCNUNIT == 1) {
    399 		ifpga_create_io_bs_tag(&plcom_bus_space, (void*)0xfd700000);
    400 		plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
    401 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT);
    402 	}
    403 #endif
    404 
    405 	/* Talk to the user */
    406 	printf("\nNetBSD/integrator booting ...\n");
    407 
    408 #if 0
    409 	if (intbootinfo.bt_magic != BT_MAGIC_NUMBER_EBSA
    410 	    && intbootinfo.bt_magic != BT_MAGIC_NUMBER_CATS)
    411 		panic("Incompatible magic number passed in boot args\n");
    412 #endif
    413 
    414 /*	{
    415 	int loop;
    416 	for (loop = 0; loop < 8; ++loop) {
    417 		printf("%08x\n", *(((int *)bootinfo)+loop));
    418 	}
    419 	}*/
    420 
    421 	/*
    422 	 * Ok we have the following memory map
    423 	 *
    424 	 * virtual address == physical address apart from the areas:
    425 	 * 0x00000000 -> 0x000fffff which is mapped to
    426 	 * top 1MB of physical memory
    427 	 * 0x00100000 -> 0x0fffffff which is mapped to
    428 	 * physical addresses 0x00100000 -> 0x0fffffff
    429 	 * 0x10000000 -> 0x1fffffff which is mapped to
    430 	 * physical addresses 0x00000000 -> 0x0fffffff
    431 	 * 0x20000000 -> 0xefffffff which is mapped to
    432 	 * physical addresses 0x20000000 -> 0xefffffff
    433 	 * 0xf0000000 -> 0xf03fffff which is mapped to
    434 	 * physical addresses 0x00000000 -> 0x003fffff
    435 	 *
    436 	 * This means that the kernel is mapped suitably for continuing
    437 	 * execution, all I/O is mapped 1:1 virtual to physical and
    438 	 * physical memory is accessible.
    439 	 *
    440 	 * The initarm() has the responsibility for creating the kernel
    441 	 * page tables.
    442 	 * It must also set up various memory pointers that are used
    443 	 * by pmap etc.
    444 	 */
    445 
    446 	/*
    447 	 * Examine the boot args string for options we need to know about
    448 	 * now.
    449 	 */
    450 #if 0
    451 	process_kernel_args((char *)intbootinfo.bt_args);
    452 #endif
    453 
    454 	printf("initarm: Configuring system ...\n");
    455 
    456 	/*
    457 	 * Set up the variables that define the availablilty of
    458 	 * physical memory
    459 	 */
    460 	physical_start = 0 /*intbootinfo.bt_memstart*/;
    461 	physical_freestart = physical_start;
    462 
    463 #if 0
    464 	physical_end = /*intbootinfo.bt_memend*/ /*intbootinfo.bi_nrpages * NBPG */ 32*1024*1024;
    465 #else
    466 	{
    467 		volatile unsigned long *cm_sdram
    468 		    = (volatile unsigned long *)0x10000020;
    469 
    470 		switch ((*cm_sdram >> 2) & 0x7)
    471 		{
    472 		case 0:
    473 			physical_end = 16 * 1024 * 1024;
    474 			break;
    475 		case 1:
    476 			physical_end = 32 * 1024 * 1024;
    477 			break;
    478 		case 2:
    479 			physical_end = 64 * 1024 * 1024;
    480 			break;
    481 		case 3:
    482 			physical_end = 128 * 1024 * 1024;
    483 			break;
    484 		case 4:
    485 			physical_end = 256 * 1024 * 1024;
    486 			break;
    487 		default:
    488 			printf("CM_SDRAM retuns unknown value, using 16M\n");
    489 			physical_end = 16 * 1024 * 1024;
    490 			break;
    491 		}
    492 	}
    493 #endif
    494 
    495 	physical_freeend = physical_end;
    496 	free_pages = (physical_end - physical_start) / NBPG;
    497 
    498 	/* Set up the bootconfig structure for the benefit of pmap.c */
    499 	bootconfig.dramblocks = 1;
    500 	bootconfig.dram[0].address = physical_start;
    501 	bootconfig.dram[0].pages = free_pages;
    502 
    503 	physmem = (physical_end - physical_start) / NBPG;
    504 
    505 	/* Tell the user about the memory */
    506 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    507 	    physical_start, physical_end - 1);
    508 
    509 	/*
    510 	 * Ok the kernel occupies the bottom of physical memory.
    511 	 * The first free page after the kernel can be found in
    512 	 * intbootinfo->bt_memavail
    513 	 * We now need to allocate some fixed page tables to get the kernel
    514 	 * going.
    515 	 * We allocate one page directory and a number page tables and store
    516 	 * the physical addresses in the kernel_pt_table array.
    517 	 *
    518 	 * Ok the next bit of physical allocation may look complex but it is
    519 	 * simple really. I have done it like this so that no memory gets
    520 	 * wasted during the allocation of various pages and tables that are
    521 	 * all different sizes.
    522 	 * The start addresses will be page aligned.
    523 	 * We allocate the kernel page directory on the first free 16KB boundry
    524 	 * we find.
    525 	 * We allocate the kernel page tables on the first 4KB boundry we find.
    526 	 * Since we allocate at least 3 L2 pagetables we know that we must
    527 	 * encounter at least one 16KB aligned address.
    528 	 */
    529 
    530 #ifdef VERBOSE_INIT_ARM
    531 	printf("Allocating page tables\n");
    532 #endif
    533 
    534 	/* Update the address of the first free 16KB chunk of physical memory */
    535         physical_freestart = ((uintptr_t) &end - KERNEL_TEXT_BASE + PGOFSET)
    536 	    & ~PGOFSET;
    537 #if 0
    538         physical_freestart += (kernexec->a_syms + sizeof(int)
    539 		    + *(u_int *)((int)end + kernexec->a_syms + sizeof(int))
    540 		    + (NBPG - 1)) & ~(NBPG - 1);
    541 #endif
    542 
    543 	free_pages -= (physical_freestart - physical_start) / NBPG;
    544 #ifdef VERBOSE_INIT_ARM
    545 	printf("freestart = %#lx, free_pages = %d (%#x)\n",
    546 	       physical_freestart, free_pages, free_pages);
    547 #endif
    548 
    549 	/* Define a macro to simplify memory allocation */
    550 #define	valloc_pages(var, np)			\
    551 	alloc_pages((var).pv_pa, (np));		\
    552 	(var).pv_va = KERNEL_TEXT_BASE + (var).pv_pa - physical_start;
    553 
    554 #define alloc_pages(var, np)			\
    555 	(var) = physical_freestart;		\
    556 	physical_freestart += ((np) * NBPG);	\
    557 	free_pages -= (np);			\
    558 	memset((char *)(var), 0, ((np) * NBPG));
    559 
    560 	loop1 = 0;
    561 	kernel_l1pt.pv_pa = 0;
    562 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    563 		/* Are we 16KB aligned for an L1 ? */
    564 		if ((physical_freestart & (PD_SIZE - 1)) == 0
    565 		    && kernel_l1pt.pv_pa == 0) {
    566 			valloc_pages(kernel_l1pt, PD_SIZE / NBPG);
    567 		} else {
    568 			alloc_pages(kernel_pt_table[loop1], PT_SIZE / NBPG);
    569 			++loop1;
    570 		}
    571 	}
    572 
    573 	/* This should never be able to happen but better confirm that. */
    574 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (PD_SIZE-1)) != 0)
    575 		panic("initarm: Failed to align the kernel page directory\n");
    576 
    577 	/*
    578 	 * Allocate a page for the system page mapped to V0x00000000
    579 	 * This page will just contain the system vectors and can be
    580 	 * shared by all processes.
    581 	 */
    582 	alloc_pages(systempage.pv_pa, 1);
    583 
    584 	/* Allocate a page for the page table to map kernel page tables*/
    585 	valloc_pages(kernel_ptpt, PT_SIZE / NBPG);
    586 
    587 	/* Allocate stacks for all modes */
    588 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    589 	valloc_pages(abtstack, ABT_STACK_SIZE);
    590 	valloc_pages(undstack, UND_STACK_SIZE);
    591 	valloc_pages(kernelstack, UPAGES);
    592 
    593 #ifdef VERBOSE_INIT_ARM
    594 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
    595 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
    596 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
    597 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
    598 #endif
    599 
    600 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
    601 
    602 	/*
    603 	 * Ok we have allocated physical pages for the primary kernel
    604 	 * page tables
    605 	 */
    606 
    607 #ifdef VERBOSE_INIT_ARM
    608 	printf("Creating L1 page table at %#lx\n", kernel_l1pt.pv_pa);
    609 #endif
    610 
    611 	/*
    612 	 * Now we start consturction of the L1 page table
    613 	 * We start by mapping the L2 page tables into the L1.
    614 	 * This means that we can replace L1 mappings later on if necessary
    615 	 */
    616 	l1pagetable = kernel_l1pt.pv_pa;
    617 
    618 	/* Map the L2 pages tables in the L1 page table */
    619 	map_pagetable(l1pagetable, 0x00000000,
    620 	    kernel_pt_table[KERNEL_PT_SYS]);
    621 	map_pagetable(l1pagetable, KERNEL_BASE,
    622 	    kernel_pt_table[KERNEL_PT_KERNEL]);
    623 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
    624 		map_pagetable(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    625 		    kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    626 	map_pagetable(l1pagetable, PROCESS_PAGE_TBLS_BASE,
    627 	    kernel_ptpt.pv_pa);
    628 
    629 #ifdef VERBOSE_INIT_ARM
    630 	printf("Mapping kernel\n");
    631 #endif
    632 
    633 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    634 	l2pagetable = kernel_pt_table[KERNEL_PT_KERNEL];
    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  = map_chunk(l1pagetable, l2pagetable, KERNEL_BASE,
    646 		    physical_start, KERNEL_TEXT_BASE - KERNEL_BASE,
    647 		    AP_KRW, PT_CACHEABLE); */
    648 		logical = map_chunk(l1pagetable, l2pagetable,
    649 		    KERNEL_TEXT_BASE, physical_start, textsize,
    650 		    AP_KRW, PT_CACHEABLE);
    651 		logical += map_chunk(l1pagetable, l2pagetable,
    652 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
    653 		    totalsize - textsize, AP_KRW, PT_CACHEABLE);
    654 #if 0
    655 		logical += map_chunk(0, l2pagetable, KERNEL_BASE + logical,
    656 		    physical_start + logical, kernexec->a_syms + sizeof(int)
    657 		    + *(u_int *)((int)end + kernexec->a_syms + sizeof(int)),
    658 		    AP_KRW, PT_CACHEABLE);
    659 #endif
    660 	}
    661 
    662 #ifdef VERBOSE_INIT_ARM
    663 	printf("Constructing L2 page tables\n");
    664 #endif
    665 
    666 	/* Map the boot arguments page */
    667 #if 0
    668 	map_entry_ro(l2pagetable, intbootinfo.bt_vargp, intbootinfo.bt_pargp);
    669 #endif
    670 
    671 	/* Map the stack pages */
    672 	map_chunk(0, l2pagetable, irqstack.pv_va, irqstack.pv_pa,
    673 	    IRQ_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
    674 	map_chunk(0, l2pagetable, abtstack.pv_va, abtstack.pv_pa,
    675 	    ABT_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
    676 	map_chunk(0, l2pagetable, undstack.pv_va, undstack.pv_pa,
    677 	    UND_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
    678 	map_chunk(0, l2pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    679 	    UPAGES * NBPG, AP_KRW, PT_CACHEABLE);
    680 	map_chunk(0, l2pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    681 	    PD_SIZE, AP_KRW, 0);
    682 
    683 	/* Map the page table that maps the kernel pages */
    684 	map_entry_nc(l2pagetable, kernel_ptpt.pv_pa, kernel_ptpt.pv_pa);
    685 
    686 	/*
    687 	 * Map entries in the page table used to map PTE's
    688 	 * Basically every kernel page table gets mapped here
    689 	 */
    690 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
    691 	l2pagetable = kernel_ptpt.pv_pa;
    692 	map_entry_nc(l2pagetable, (KERNEL_BASE >> (PGSHIFT-2)),
    693 	    kernel_pt_table[KERNEL_PT_KERNEL]);
    694 	map_entry_nc(l2pagetable, (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT-2)),
    695 	    kernel_ptpt.pv_pa);
    696 	map_entry_nc(l2pagetable, (0x00000000 >> (PGSHIFT-2)),
    697 	    kernel_pt_table[KERNEL_PT_SYS]);
    698 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
    699 		map_entry_nc(l2pagetable, ((KERNEL_VM_BASE +
    700 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
    701 		    kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    702 
    703 	/*
    704 	 * Map the system page in the kernel page table for the bottom 1Meg
    705 	 * of the virtual memory map.
    706 	 */
    707 	l2pagetable = kernel_pt_table[KERNEL_PT_SYS];
    708 #if 1
    709 	/* MULTI-ICE requires that page 0 is NC/NB so that it can download
    710 	   the cache-clean code there.  */
    711 	map_entry_nc(l2pagetable, 0x00000000, systempage.pv_pa);
    712 #else
    713 	map_entry_nc(l2pagetable, 0x00000000, systempage.pv_pa);
    714 #endif
    715 	/* Map the core memory needed before autoconfig */
    716 	loop = 0;
    717 	while (l1_sec_table[loop].size) {
    718 		vm_size_t sz;
    719 
    720 #ifdef VERBOSE_INIT_ARM
    721 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
    722 		    l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
    723 		    l1_sec_table[loop].va);
    724 #endif
    725 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_SEC_SIZE)
    726 			map_section(l1pagetable, l1_sec_table[loop].va + sz,
    727 			    l1_sec_table[loop].pa + sz,
    728 			    l1_sec_table[loop].flags);
    729 		++loop;
    730 	}
    731 
    732 	/*
    733 	 * Now we have the real page tables in place so we can switch to them.
    734 	 * Once this is done we will be running with the REAL kernel page tables.
    735 	 */
    736 
    737 	/* Switch tables */
    738 #ifdef VERBOSE_INIT_ARM
    739 	printf("freestart = %#lx, free_pages = %d (%#x)\n",
    740 	       physical_freestart, free_pages, free_pages);
    741 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    742 #endif
    743 
    744 	setttb(kernel_l1pt.pv_pa);
    745 
    746 #ifdef VERBOSE_INIT_ARM
    747 	printf("done!\n");
    748 #endif
    749 
    750 #ifdef PLCONSOLE
    751 	/*
    752 	 * The IFPGA registers have just moved.
    753 	 * Detach the diagnostic serial port and reattach at the new address.
    754 	 */
    755 	plcomcndetach();
    756 #endif
    757 
    758 	/*
    759 	 * XXX this should only be done in main() but it useful to
    760 	 * have output earlier ...
    761 	 */
    762 	consinit();
    763 
    764 #ifdef VERBOSE_INIT_ARM
    765 	printf("bootstrap done.\n");
    766 #endif
    767 
    768 	/* Right set up the vectors at the bottom of page 0 */
    769 	memcpy((char *)0x00000000, page0, page0_end - page0);
    770 
    771 	/* We have modified a text page so sync the icache */
    772 	cpu_cache_syncI();
    773 
    774 	/*
    775 	 * Pages were allocated during the secondary bootstrap for the
    776 	 * stacks for different CPU modes.
    777 	 * We must now set the r13 registers in the different CPU modes to
    778 	 * point to these stacks.
    779 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    780 	 * of the stack memory.
    781 	 */
    782 	printf("init subsystems: stacks ");
    783 
    784 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
    785 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
    786 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
    787 
    788 	/*
    789 	 * Well we should set a data abort handler.
    790 	 * Once things get going this will change as we will need a proper handler.
    791 	 * Until then we will use a handler that just panics but tells us
    792 	 * why.
    793 	 * Initialisation of the vectors will just panic on a data abort.
    794 	 * This just fills in a slighly better one.
    795 	 */
    796 	printf("vectors ");
    797 	data_abort_handler_address = (u_int)data_abort_handler;
    798 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    799 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    800 
    801 	/* At last !
    802 	 * We now have the kernel in physical memory from the bottom upwards.
    803 	 * Kernel page tables are physically above this.
    804 	 * The kernel is mapped to KERNEL_TEXT_BASE
    805 	 * The kernel data PTs will handle the mapping of 0xf1000000-0xf3ffffff
    806 	 * The page tables are mapped to 0xefc00000
    807 	 */
    808 
    809 	/* Initialise the undefined instruction handlers */
    810 	printf("undefined ");
    811 	undefined_init();
    812 
    813 	/* Boot strap pmap telling it where the kernel page table is */
    814 	printf("pmap ");
    815 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
    816 
    817 	/* Setup the IRQ system */
    818 	printf("irq ");
    819 	irq_init();
    820 
    821 	printf("done.\n");
    822 
    823 #ifdef IPKDB
    824 	/* Initialise ipkdb */
    825 	ipkdb_init();
    826 	if (boothowto & RB_KDB)
    827 		ipkdb_connect(0);
    828 #endif
    829 
    830 #ifdef DDB
    831 	db_machine_init();
    832 
    833 	/* Firmware doesn't load symbols. */
    834 	ddb_init(0, NULL, NULL);
    835 
    836 	if (boothowto & RB_KDB)
    837 		Debugger();
    838 #endif
    839 
    840 	/* We return the new stack pointer address */
    841 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    842 }
    843 
    844 void
    845 process_kernel_args(args)
    846 	char *args;
    847 {
    848 
    849 	boothowto = 0;
    850 
    851 	/* Make a local copy of the bootargs */
    852 	strncpy(bootargs, args, MAX_BOOT_STRING);
    853 
    854 	args = bootargs;
    855 	boot_file = bootargs;
    856 
    857 	/* Skip the kernel image filename */
    858 	while (*args != ' ' && *args != 0)
    859 		++args;
    860 
    861 	if (*args != 0)
    862 		*args++ = 0;
    863 
    864 	while (*args == ' ')
    865 		++args;
    866 
    867 	boot_args = args;
    868 
    869 	printf("bootfile: %s\n", boot_file);
    870 	printf("bootargs: %s\n", boot_args);
    871 
    872 	parse_mi_bootargs(boot_args);
    873 }
    874 
    875 void
    876 consinit(void)
    877 {
    878 	static int consinit_called = 0;
    879 #if NPLCOM > 0 && defined(PLCONSOLE)
    880 	static struct bus_space plcom_bus_space;
    881 #endif
    882 #if 0
    883 	char *console = CONSDEVNAME;
    884 #endif
    885 
    886 	if (consinit_called != 0)
    887 		return;
    888 
    889 	consinit_called = 1;
    890 
    891 #if NPLCOM > 0 && defined(PLCONSOLE)
    892 	if (PLCOMCNUNIT == 0) {
    893 		ifpga_create_io_bs_tag(&plcom_bus_space,
    894 		    (void*)UART0_BOOT_BASE);
    895 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
    896 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
    897 			panic("can't init serial console");
    898 		return;
    899 	} else if (PLCOMCNUNIT == 1) {
    900 		ifpga_create_io_bs_tag(&plcom_bus_space,
    901 		    (void*)UART0_BOOT_BASE);
    902 		if (plcomcnattach(&plcom_bus_space, 0, plcomcnspeed,
    903 		    IFPGA_UART_CLK, plcomcnmode, PLCOMCNUNIT))
    904 			panic("can't init serial console");
    905 		return;
    906 	}
    907 #endif
    908 #if (NCOM > 0)
    909 	if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
    910 	    COM_FREQ, comcnmode))
    911 		panic("can't init serial console @%x", CONCOMADDR);
    912 	return;
    913 #endif
    914 	panic("No serial console configured");
    915 }
    916 
    917 #if 0
    918 static bus_space_handle_t kcom_base = (bus_space_handle_t) (DC21285_PCI_IO_VBASE + CONCOMADDR);
    919 
    920 u_int8_t footbridge_bs_r_1(void *, bus_space_handle_t, bus_size_t);
    921 void footbridge_bs_w_1(void *, bus_space_handle_t, bus_size_t, u_int8_t);
    922 
    923 #define	KCOM_GETBYTE(r)		footbridge_bs_r_1(0, kcom_base, (r))
    924 #define	KCOM_PUTBYTE(r,v)	footbridge_bs_w_1(0, kcom_base, (r), (v))
    925 
    926 static int
    927 kcomcngetc(dev_t dev)
    928 {
    929 	int stat, c;
    930 
    931 	/* block until a character becomes available */
    932 	while (!ISSET(stat = KCOM_GETBYTE(com_lsr), LSR_RXRDY))
    933 		;
    934 
    935 	c = KCOM_GETBYTE(com_data);
    936 	stat = KCOM_GETBYTE(com_iir);
    937 	return c;
    938 }
    939 
    940 /*
    941  * Console kernel output character routine.
    942  */
    943 static void
    944 kcomcnputc(dev_t dev, int c)
    945 {
    946 	int timo;
    947 
    948 	/* wait for any pending transmission to finish */
    949 	timo = 150000;
    950 	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
    951 		continue;
    952 
    953 	KCOM_PUTBYTE(com_data, c);
    954 
    955 	/* wait for this transmission to complete */
    956 	timo = 1500000;
    957 	while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
    958 		continue;
    959 }
    960 
    961 static void
    962 kcomcnpollc(dev_t dev, int on)
    963 {
    964 }
    965 
    966 struct consdev kcomcons = {
    967 	NULL, NULL, kcomcngetc, kcomcnputc, kcomcnpollc, NULL,
    968 	NODEV, CN_NORMAL
    969 };
    970 
    971 #endif
    972