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