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