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