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