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