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