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