Home | History | Annotate | Line # | Download | only in npwr_fc
npwr_fc_machdep.c revision 1.12
      1 /*	$NetBSD: npwr_fc_machdep.c,v 1.12 2009/11/27 03:23:07 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1997,1998 Mark Brinicombe.
     40  * Copyright (c) 1997,1998 Causality Limited.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Mark Brinicombe
     54  *	for the NetBSD Project.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * Machine dependant functions for kernel setup for Intel IQ80321 evaluation
     72  * boards using RedBoot firmware.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: npwr_fc_machdep.c,v 1.12 2009/11/27 03:23:07 rmind Exp $");
     77 
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 #include "opt_pmap_debug.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/device.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/exec.h>
     87 #include <sys/proc.h>
     88 #include <sys/msgbuf.h>
     89 #include <sys/reboot.h>
     90 #include <sys/termios.h>
     91 #include <sys/ksyms.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <dev/cons.h>
     96 
     97 #include <machine/db_machdep.h>
     98 #include <ddb/db_sym.h>
     99 #include <ddb/db_extern.h>
    100 
    101 #include <machine/bootconfig.h>
    102 #include <machine/bus.h>
    103 #include <machine/cpu.h>
    104 #include <machine/frame.h>
    105 #include <arm/undefined.h>
    106 
    107 #include <arm/arm32/machdep.h>
    108 
    109 #include <arm/xscale/i80321reg.h>
    110 #include <arm/xscale/i80321var.h>
    111 
    112 #include <dev/pci/ppbreg.h>
    113 
    114 #include <evbarm/iq80321/iq80321reg.h>
    115 #include <evbarm/iq80321/iq80321var.h>
    116 #include <evbarm/iq80321/obiovar.h>
    117 
    118 #include "ksyms.h"
    119 
    120 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    121 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    122 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    123 
    124 /*
    125  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    126  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    127  */
    128 #define KERNEL_VM_SIZE		0x0C000000
    129 
    130 /*
    131  * Address to call from cpu_reset() to reset the machine.
    132  * This is machine architecture dependant as it varies depending
    133  * on where the ROM appears when you turn the MMU off.
    134  *
    135  * XXX Not actally used on IQ80321 -- clean up the generic
    136  * ARM code.
    137  */
    138 
    139 u_int cpu_reset_address = 0x00000000;
    140 
    141 /* Define various stack sizes in pages */
    142 #define IRQ_STACK_SIZE	1
    143 #define ABT_STACK_SIZE	1
    144 #define UND_STACK_SIZE	1
    145 
    146 BootConfig bootconfig;		/* Boot config storage */
    147 char *boot_args = NULL;
    148 char *boot_file = NULL;
    149 
    150 vm_offset_t physical_start;
    151 vm_offset_t physical_freestart;
    152 vm_offset_t physical_freeend;
    153 vm_offset_t physical_end;
    154 u_int free_pages;
    155 vm_offset_t pagetables_start;
    156 
    157 /*int debug_flags;*/
    158 #ifndef PMAP_STATIC_L1S
    159 int max_processes = 64;			/* Default number */
    160 #endif	/* !PMAP_STATIC_L1S */
    161 
    162 /* Physical and virtual addresses for some global pages */
    163 pv_addr_t irqstack;
    164 pv_addr_t undstack;
    165 pv_addr_t abtstack;
    166 pv_addr_t kernelstack;
    167 pv_addr_t minidataclean;
    168 
    169 vm_offset_t msgbufphys;
    170 
    171 extern u_int data_abort_handler_address;
    172 extern u_int prefetch_abort_handler_address;
    173 extern u_int undefined_handler_address;
    174 
    175 #ifdef PMAP_DEBUG
    176 extern int pmap_debug_level;
    177 #endif
    178 
    179 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    180 
    181 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    182 #define	KERNEL_PT_KERNEL_NUM	4
    183 
    184 					/* L2 table for mapping i80321 */
    185 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    186 
    187 					/* L2 tables for mapping kernel VM */
    188 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
    189 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    190 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    191 
    192 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    193 
    194 /* Prototypes */
    195 
    196 void	consinit(void);
    197 
    198 #include "com.h"
    199 #if NCOM > 0
    200 #include <dev/ic/comreg.h>
    201 #include <dev/ic/comvar.h>
    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 /* ! CONSPEED */
    211 
    212 #ifndef CONUNIT
    213 #define	CONUNIT	0
    214 #endif
    215 
    216 #ifndef CONMODE
    217 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    218 #endif
    219 
    220 int comcnspeed = CONSPEED;
    221 int comcnmode = CONMODE;
    222 int comcnunit = CONUNIT;
    223 
    224 #if KGDB
    225 #ifndef KGDB_DEVNAME
    226 #error Must define KGDB_DEVNAME
    227 #endif
    228 const char kgdb_devname[] = KGDB_DEVNAME;
    229 
    230 #ifndef KGDB_DEVADDR
    231 #error Must define KGDB_DEVADDR
    232 #endif
    233 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    234 
    235 #ifndef KGDB_DEVRATE
    236 #define KGDB_DEVRATE	CONSPEED
    237 #endif
    238 int kgdb_devrate = KGDB_DEVRATE;
    239 
    240 #ifndef KGDB_DEVMODE
    241 #define KGDB_DEVMODE	CONMODE
    242 #endif
    243 int kgdb_devmode = KGDB_DEVMODE;
    244 #endif /* KGDB */
    245 
    246 /*
    247  * void cpu_reboot(int howto, char *bootstr)
    248  *
    249  * Reboots the system
    250  *
    251  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    252  * then reset the CPU.
    253  */
    254 void
    255 cpu_reboot(int howto, char *bootstr)
    256 {
    257 
    258 	/*
    259 	 * If we are still cold then hit the air brakes
    260 	 * and crash to earth fast
    261 	 */
    262 	if (cold) {
    263 		doshutdownhooks();
    264 		pmf_system_shutdown(boothowto);
    265 		printf("The operating system has halted.\n");
    266 		printf("Please press any key to reboot.\n\n");
    267 		cngetc();
    268 		printf("rebooting...\n");
    269 		goto reset;
    270 	}
    271 
    272 	/* Disable console buffering */
    273 
    274 	/*
    275 	 * If RB_NOSYNC was not specified sync the discs.
    276 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    277 	 * unmount.  It looks like syslogd is getting woken up only to find
    278 	 * that it cannot page part of the binary in as the filesystem has
    279 	 * been unmounted.
    280 	 */
    281 	if (!(howto & RB_NOSYNC))
    282 		bootsync();
    283 
    284 	/* Say NO to interrupts */
    285 	splhigh();
    286 
    287 	/* Do a dump if requested. */
    288 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    289 		dumpsys();
    290 
    291 	/* Run any shutdown hooks */
    292 	doshutdownhooks();
    293 
    294 	pmf_system_shutdown(boothowto);
    295 
    296 	/* Make sure IRQ's are disabled */
    297 	IRQdisable;
    298 
    299 	if (howto & RB_HALT) {
    300 		printf("The operating system has halted.\n");
    301 		printf("Please press any key to reboot.\n\n");
    302 		cngetc();
    303 	}
    304 
    305 	printf("rebooting...\n\r");
    306  reset:
    307 	/*
    308 	 * Make really really sure that all interrupts are disabled,
    309 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    310 	 */
    311 	(void) disable_interrupts(I32_bit|F32_bit);
    312 	*(volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
    313 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
    314 
    315 	/* ...and if that didn't work, just croak. */
    316 	printf("RESET FAILED!\n");
    317 	for (;;);
    318 }
    319 
    320 /* Static device mappings. */
    321 static const struct pmap_devmap iq80321_devmap[] = {
    322     /*
    323      * Map the on-board devices VA == PA so that we can access them
    324      * with the MMU on or off.
    325      */
    326     {
    327 	IQ80321_OBIO_BASE,
    328 	IQ80321_OBIO_BASE,
    329 	IQ80321_OBIO_SIZE,
    330 	VM_PROT_READ|VM_PROT_WRITE,
    331 	PTE_NOCACHE,
    332     },
    333 
    334     {
    335 	IQ80321_IOW_VBASE,
    336 	VERDE_OUT_XLATE_IO_WIN0_BASE,
    337 	VERDE_OUT_XLATE_IO_WIN_SIZE,
    338 	VM_PROT_READ|VM_PROT_WRITE,
    339 	PTE_NOCACHE,
    340    },
    341 
    342    {
    343 	IQ80321_80321_VBASE,
    344 	VERDE_PMMR_BASE,
    345 	VERDE_PMMR_SIZE,
    346 	VM_PROT_READ|VM_PROT_WRITE,
    347 	PTE_NOCACHE,
    348    },
    349 
    350    {
    351 	0,
    352 	0,
    353 	0,
    354 	0,
    355 	0,
    356     }
    357 };
    358 
    359 /*
    360  * u_int initarm(...)
    361  *
    362  * Initial entry point on startup. This gets called before main() is
    363  * entered.
    364  * It should be responsible for setting up everything that must be
    365  * in place when main is called.
    366  * This includes
    367  *   Taking a copy of the boot configuration structure.
    368  *   Initialising the physical console so characters can be printed.
    369  *   Setting up page tables for the kernel
    370  *   Relocating the kernel to the bottom of physical memory
    371  */
    372 u_int
    373 initarm(void *arg)
    374 {
    375 	extern vaddr_t xscale_cache_clean_addr;
    376 #ifdef DIAGNOSTIC
    377 	extern vsize_t xscale_minidata_clean_size;
    378 #endif
    379 	int loop;
    380 	int loop1;
    381 	u_int l1pagetable;
    382 	paddr_t memstart;
    383 	psize_t memsize;
    384 
    385 	/* Calibrate the delay loop. */
    386 	i80321_calibrate_delay();
    387 	i80321_hardclock_hook = NULL;
    388 
    389 	/*
    390 	 * Since we map the on-board devices VA==PA, and the kernel
    391 	 * is running VA==PA, it's possible for us to initialize
    392 	 * the console now.
    393 	 */
    394 	consinit();
    395 
    396 #ifdef VERBOSE_INIT_ARM
    397 	/* Talk to the user */
    398 	printf("\nNetBSD/evbarm (NPWR_FC) booting ...\n");
    399 #endif
    400 
    401 	/*
    402 	 * Heads up ... Setup the CPU / MMU / TLB functions
    403 	 */
    404 	if (set_cpufuncs())
    405 		panic("cpu not recognized!");
    406 
    407 	/*
    408 	 * We are currently running with the MMU enabled and the
    409 	 * entire address space mapped VA==PA, except for the
    410 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    411 	 * There is an L1 page table at 0xa0004000.
    412 	 */
    413 
    414 	/*
    415 	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
    416 	 * registers.
    417 	 */
    418 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    419 	    &memstart, &memsize);
    420 
    421 #ifdef VERBOSE_INIT_ARM
    422 	printf("initarm: Configuring system ...\n");
    423 #endif
    424 
    425 	/* Fake bootconfig structure for the benefit of pmap.c */
    426 	/* XXX must make the memory description h/w independent */
    427 	bootconfig.dramblocks = 1;
    428 	bootconfig.dram[0].address = memstart;
    429 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    430 
    431 	/*
    432 	 * Set up the variables that define the availablilty of
    433 	 * physical memory.  For now, we're going to set
    434 	 * physical_freestart to 0xa0200000 (where the kernel
    435 	 * was loaded), and allocate the memory we need downwards.
    436 	 * If we get too close to the L1 table that we set up, we
    437 	 * will panic.  We will update physical_freestart and
    438 	 * physical_freeend later to reflect what pmap_bootstrap()
    439 	 * wants to see.
    440 	 *
    441 	 * XXX pmap_bootstrap() needs an enema.
    442 	 */
    443 	physical_start = bootconfig.dram[0].address;
    444 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    445 
    446 	physical_freestart = 0xa0009000UL;
    447 	physical_freeend = 0xa0200000UL;
    448 
    449 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    450 
    451 #ifdef VERBOSE_INIT_ARM
    452 	/* Tell the user about the memory */
    453 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    454 	    physical_start, physical_end - 1);
    455 #endif
    456 
    457 	/*
    458 	 * Okay, the kernel starts 2MB in from the bottom of physical
    459 	 * memory.  We are going to allocate our bootstrap pages downwards
    460 	 * from there.
    461 	 *
    462 	 * We need to allocate some fixed page tables to get the kernel
    463 	 * going.  We allocate one page directory and a number of page
    464 	 * tables and store the physical addresses in the kernel_pt_table
    465 	 * array.
    466 	 *
    467 	 * The kernel page directory must be on a 16K boundary.  The page
    468 	 * tables must be on 4K bounaries.  What we do is allocate the
    469 	 * page directory on the first 16K boundary that we encounter, and
    470 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    471 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    472 	 * least one 16K aligned region.
    473 	 */
    474 
    475 #ifdef VERBOSE_INIT_ARM
    476 	printf("Allocating page tables\n");
    477 #endif
    478 
    479 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    480 
    481 #ifdef VERBOSE_INIT_ARM
    482 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    483 	       physical_freestart, free_pages, free_pages);
    484 #endif
    485 
    486 	/* Define a macro to simplify memory allocation */
    487 #define	valloc_pages(var, np)				\
    488 	alloc_pages((var).pv_pa, (np));			\
    489 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    490 
    491 #define alloc_pages(var, np)				\
    492 	physical_freeend -= ((np) * PAGE_SIZE);		\
    493 	if (physical_freeend < physical_freestart)	\
    494 		panic("initarm: out of memory");	\
    495 	(var) = physical_freeend;			\
    496 	free_pages -= (np);				\
    497 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    498 
    499 	loop1 = 0;
    500 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    501 		/* Are we 16KB aligned for an L1 ? */
    502 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    503 		    && kernel_l1pt.pv_pa == 0) {
    504 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    505 		} else {
    506 			valloc_pages(kernel_pt_table[loop1],
    507 			    L2_TABLE_SIZE / PAGE_SIZE);
    508 			++loop1;
    509 		}
    510 	}
    511 
    512 	/* This should never be able to happen but better confirm that. */
    513 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    514 		panic("initarm: Failed to align the kernel page directory");
    515 
    516 	/*
    517 	 * Allocate a page for the system page mapped to V0x00000000
    518 	 * This page will just contain the system vectors and can be
    519 	 * shared by all processes.
    520 	 */
    521 	alloc_pages(systempage.pv_pa, 1);
    522 
    523 	/* Allocate stacks for all modes */
    524 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    525 	valloc_pages(abtstack, ABT_STACK_SIZE);
    526 	valloc_pages(undstack, UND_STACK_SIZE);
    527 	valloc_pages(kernelstack, UPAGES);
    528 
    529 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    530 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    531 	valloc_pages(minidataclean, 1);
    532 
    533 #ifdef VERBOSE_INIT_ARM
    534 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    535 	    irqstack.pv_va);
    536 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    537 	    abtstack.pv_va);
    538 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    539 	    undstack.pv_va);
    540 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    541 	    kernelstack.pv_va);
    542 #endif
    543 
    544 	/*
    545 	 * XXX Defer this to later so that we can reclaim the memory
    546 	 * XXX used by the RedBoot page tables.
    547 	 */
    548 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    549 
    550 	/*
    551 	 * Ok we have allocated physical pages for the primary kernel
    552 	 * page tables
    553 	 */
    554 
    555 #ifdef VERBOSE_INIT_ARM
    556 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    557 #endif
    558 
    559 	/*
    560 	 * Now we start construction of the L1 page table
    561 	 * We start by mapping the L2 page tables into the L1.
    562 	 * This means that we can replace L1 mappings later on if necessary
    563 	 */
    564 	l1pagetable = kernel_l1pt.pv_pa;
    565 
    566 	/* Map the L2 pages tables in the L1 page table */
    567 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    568 	    &kernel_pt_table[KERNEL_PT_SYS]);
    569 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    570 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    571 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    572 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
    573 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    574 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    575 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    576 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    577 
    578 	/* update the top of the kernel VM */
    579 	pmap_curmaxkvaddr =
    580 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    581 
    582 #ifdef VERBOSE_INIT_ARM
    583 	printf("Mapping kernel\n");
    584 #endif
    585 
    586 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    587 	{
    588 		extern char etext[], _end[];
    589 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    590 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    591 		u_int logical;
    592 
    593 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    594 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    595 
    596 		logical = 0x00200000;	/* offset of kernel in RAM */
    597 
    598 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    599 		    physical_start + logical, textsize,
    600 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    601 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    602 		    physical_start + logical, totalsize - textsize,
    603 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    604 	}
    605 
    606 #ifdef VERBOSE_INIT_ARM
    607 	printf("Constructing L2 page tables\n");
    608 #endif
    609 
    610 	/* Map the stack pages */
    611 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    612 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    613 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    614 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    615 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    616 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    617 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    618 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    619 
    620 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    621 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    622 
    623 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    624 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    625 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    626 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    627 	}
    628 
    629 	/* Map the Mini-Data cache clean area. */
    630 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    631 	    minidataclean.pv_pa);
    632 
    633 	/* Map the vector page. */
    634 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    635 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    636 
    637 	/* Map the statically mapped devices. */
    638 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
    639 
    640 	/*
    641 	 * Give the XScale global cache clean code an appropriately
    642 	 * sized chunk of unmapped VA space starting at 0xff000000
    643 	 * (our device mappings end before this address).
    644 	 */
    645 	xscale_cache_clean_addr = 0xff000000U;
    646 
    647 	/*
    648 	 * Now we have the real page tables in place so we can switch to them.
    649 	 * Once this is done we will be running with the REAL kernel page
    650 	 * tables.
    651 	 */
    652 
    653 	/*
    654 	 * Update the physical_freestart/physical_freeend/free_pages
    655 	 * variables.
    656 	 */
    657 	{
    658 		extern char _end[];
    659 
    660 		physical_freestart = physical_start +
    661 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    662 		     KERNEL_BASE);
    663 		physical_freeend = physical_end;
    664 		free_pages =
    665 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    666 	}
    667 
    668 	/* Switch tables */
    669 #ifdef VERBOSE_INIT_ARM
    670 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    671 	       physical_freestart, free_pages, free_pages);
    672 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    673 #endif
    674 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    675 	setttb(kernel_l1pt.pv_pa);
    676 	cpu_tlb_flushID();
    677 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    678 
    679 	/*
    680 	 * Moved from cpu_startup() as data_abort_handler() references
    681 	 * this during uvm init
    682 	 */
    683 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    684 
    685 #ifdef VERBOSE_INIT_ARM
    686 	printf("done!\n");
    687 #endif
    688 
    689 #ifdef VERBOSE_INIT_ARM
    690 	printf("bootstrap done.\n");
    691 #endif
    692 
    693 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    694 
    695 	/*
    696 	 * Pages were allocated during the secondary bootstrap for the
    697 	 * stacks for different CPU modes.
    698 	 * We must now set the r13 registers in the different CPU modes to
    699 	 * point to these stacks.
    700 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    701 	 * of the stack memory.
    702 	 */
    703 #ifdef VERBOSE_INIT_ARM
    704 	printf("init subsystems: stacks ");
    705 #endif
    706 
    707 	set_stackptr(PSR_IRQ32_MODE,
    708 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    709 	set_stackptr(PSR_ABT32_MODE,
    710 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    711 	set_stackptr(PSR_UND32_MODE,
    712 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    713 
    714 	/*
    715 	 * Well we should set a data abort handler.
    716 	 * Once things get going this will change as we will need a proper
    717 	 * handler.
    718 	 * Until then we will use a handler that just panics but tells us
    719 	 * why.
    720 	 * Initialisation of the vectors will just panic on a data abort.
    721 	 * This just fills in a slighly better one.
    722 	 */
    723 #ifdef VERBOSE_INIT_ARM
    724 	printf("vectors ");
    725 #endif
    726 	data_abort_handler_address = (u_int)data_abort_handler;
    727 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    728 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    729 
    730 	/* Initialise the undefined instruction handlers */
    731 #ifdef VERBOSE_INIT_ARM
    732 	printf("undefined ");
    733 #endif
    734 	undefined_init();
    735 
    736 	/* Load memory into UVM. */
    737 #ifdef VERBOSE_INIT_ARM
    738 	printf("page ");
    739 #endif
    740 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    741 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    742 	    atop(physical_freestart), atop(physical_freeend),
    743 	    VM_FREELIST_DEFAULT);
    744 
    745 	/* Boot strap pmap telling it where the kernel page table is */
    746 #ifdef VERBOSE_INIT_ARM
    747 	printf("pmap ");
    748 #endif
    749 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    750 
    751 	/* Setup the IRQ system */
    752 #ifdef VERBOSE_INIT_ARM
    753 	printf("irq ");
    754 #endif
    755 	i80321_intr_init();
    756 
    757 #ifdef VERBOSE_INIT_ARM
    758 	printf("done.\n");
    759 #endif
    760 
    761 #ifdef BOOTHOWTO
    762 	boothowto = BOOTHOWTO;
    763 #endif
    764 
    765 #ifdef DDB
    766 	db_machine_init();
    767 	if (boothowto & RB_KDB)
    768 		Debugger();
    769 #endif
    770 
    771 	/* We return the new stack pointer address */
    772 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    773 }
    774 
    775 void
    776 consinit(void)
    777 {
    778 	static const bus_addr_t comcnaddrs[] = {
    779 		IQ80321_UART1,		/* com0 */
    780 	};
    781 	static int consinit_called;
    782 
    783 	if (consinit_called != 0)
    784 		return;
    785 
    786 	consinit_called = 1;
    787 
    788 	/*
    789 	 * Console devices are mapped VA==PA.  Our devmap reflects
    790 	 * this, so register it now so drivers can map the console
    791 	 * device.
    792 	 */
    793 	pmap_devmap_register(iq80321_devmap);
    794 
    795 #if NCOM > 0
    796 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    797 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    798 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    799 #else
    800 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    801 #endif
    802 #if KGDB
    803 #if NCOM > 0
    804 	if (strcmp(kgdb_devname, "com") == 0) {
    805 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
    806 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
    807 	}
    808 #endif	/* NCOM > 0 */
    809 #endif	/* KGDB */
    810 }
    811