Home | History | Annotate | Line # | Download | only in iq80310
iq80310_machdep.c revision 1.60
      1 /*	$NetBSD: iq80310_machdep.c,v 1.60 2003/06/15 18:43:49 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe 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 IQ80310 evaluation
     72  * boards using RedBoot firmware.
     73  */
     74 
     75 #include "opt_ddb.h"
     76 #include "opt_pmap_debug.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/device.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/exec.h>
     83 #include <sys/proc.h>
     84 #include <sys/msgbuf.h>
     85 #include <sys/reboot.h>
     86 #include <sys/termios.h>
     87 #include <sys/ksyms.h>
     88 
     89 #include <uvm/uvm_extern.h>
     90 
     91 #include <dev/cons.h>
     92 
     93 #include <machine/db_machdep.h>
     94 #include <ddb/db_sym.h>
     95 #include <ddb/db_extern.h>
     96 
     97 #include <machine/bootconfig.h>
     98 #include <machine/bus.h>
     99 #include <machine/cpu.h>
    100 #include <machine/frame.h>
    101 #include <arm/undefined.h>
    102 
    103 #include <arm/arm32/machdep.h>
    104 
    105 #include <arm/xscale/i80312reg.h>
    106 #include <arm/xscale/i80312var.h>
    107 
    108 #include <dev/pci/ppbreg.h>
    109 
    110 #include <evbarm/iq80310/iq80310reg.h>
    111 #include <evbarm/iq80310/iq80310var.h>
    112 #include <evbarm/iq80310/obiovar.h>
    113 
    114 #include "opt_ipkdb.h"
    115 #include "ksyms.h"
    116 
    117 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    118 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    119 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    120 
    121 /*
    122  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    123  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    124  */
    125 #define KERNEL_VM_SIZE		0x0C000000
    126 
    127 /*
    128  * Address to call from cpu_reset() to reset the machine.
    129  * This is machine architecture dependant as it varies depending
    130  * on where the ROM appears when you turn the MMU off.
    131  */
    132 
    133 u_int cpu_reset_address = 0;
    134 
    135 /* Define various stack sizes in pages */
    136 #define IRQ_STACK_SIZE	1
    137 #define ABT_STACK_SIZE	1
    138 #ifdef IPKDB
    139 #define UND_STACK_SIZE	2
    140 #else
    141 #define UND_STACK_SIZE	1
    142 #endif
    143 
    144 BootConfig bootconfig;		/* Boot config storage */
    145 char *boot_args = NULL;
    146 char *boot_file = NULL;
    147 
    148 vm_offset_t physical_start;
    149 vm_offset_t physical_freestart;
    150 vm_offset_t physical_freeend;
    151 vm_offset_t physical_end;
    152 u_int free_pages;
    153 vm_offset_t pagetables_start;
    154 int physmem = 0;
    155 
    156 /*int debug_flags;*/
    157 #ifndef PMAP_STATIC_L1S
    158 int max_processes = 64;			/* Default number */
    159 #endif	/* !PMAP_STATIC_L1S */
    160 
    161 /* Physical and virtual addresses for some global pages */
    162 pv_addr_t systempage;
    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	2
    183 
    184 					/* L2 table for mapping i80312 */
    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 struct user *proc0paddr;
    195 
    196 /* Prototypes */
    197 
    198 void	consinit(void);
    199 
    200 #include "com.h"
    201 #if NCOM > 0
    202 #include <dev/ic/comreg.h>
    203 #include <dev/ic/comvar.h>
    204 #endif
    205 
    206 /*
    207  * Define the default console speed for the board.  This is generally
    208  * what the firmware provided with the board defaults to.
    209  */
    210 #ifndef CONSPEED
    211 #define CONSPEED B115200
    212 #endif /* ! CONSPEED */
    213 
    214 #ifndef CONUNIT
    215 #define	CONUNIT	0
    216 #endif
    217 
    218 #ifndef CONMODE
    219 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    220 #endif
    221 
    222 int comcnspeed = CONSPEED;
    223 int comcnmode = CONMODE;
    224 int comcnunit = CONUNIT;
    225 
    226 /*
    227  * void cpu_reboot(int howto, char *bootstr)
    228  *
    229  * Reboots the system
    230  *
    231  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    232  * then reset the CPU.
    233  */
    234 void
    235 cpu_reboot(int howto, char *bootstr)
    236 {
    237 
    238 	/*
    239 	 * If we are still cold then hit the air brakes
    240 	 * and crash to earth fast
    241 	 */
    242 	if (cold) {
    243 		doshutdownhooks();
    244 		printf("The operating system has halted.\n");
    245 		printf("Please press any key to reboot.\n\n");
    246 		cngetc();
    247 		printf("rebooting...\n");
    248 		cpu_reset();
    249 		/*NOTREACHED*/
    250 	}
    251 
    252 	/* Disable console buffering */
    253 
    254 	/*
    255 	 * If RB_NOSYNC was not specified sync the discs.
    256 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    257 	 * unmount.  It looks like syslogd is getting woken up only to find
    258 	 * that it cannot page part of the binary in as the filesystem has
    259 	 * been unmounted.
    260 	 */
    261 	if (!(howto & RB_NOSYNC))
    262 		bootsync();
    263 
    264 	/* Say NO to interrupts */
    265 	splhigh();
    266 
    267 	/* Do a dump if requested. */
    268 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    269 		dumpsys();
    270 
    271 	/* Run any shutdown hooks */
    272 	doshutdownhooks();
    273 
    274 	/* Make sure IRQ's are disabled */
    275 	IRQdisable;
    276 
    277 	if (howto & RB_HALT) {
    278 		iq80310_7seg('.', '.');
    279 		printf("The operating system has halted.\n");
    280 		printf("Please press any key to reboot.\n\n");
    281 		cngetc();
    282 	}
    283 
    284 	printf("rebooting...\n");
    285 	cpu_reset();
    286 	/*NOTREACHED*/
    287 }
    288 
    289 /* Static device mappings. */
    290 static const struct pmap_devmap iq80310_devmap[] = {
    291     /*
    292      * Map the on-board devices VA == PA so that we can access them
    293      * with the MMU on or off.
    294      */
    295     {
    296 	IQ80310_OBIO_BASE,
    297 	IQ80310_OBIO_BASE,
    298 	IQ80310_OBIO_SIZE,
    299 	VM_PROT_READ|VM_PROT_WRITE,
    300 	PTE_NOCACHE,
    301     },
    302     {
    303 	IQ80310_PIOW_VBASE,
    304 	I80312_PCI_XLATE_PIOW_BASE,
    305 	I80312_PCI_XLATE_IOSIZE,
    306 	VM_PROT_READ|VM_PROT_WRITE,
    307 	PTE_NOCACHE,
    308     },
    309     {
    310 	IQ80310_SIOW_VBASE,
    311 	I80312_PCI_XLATE_SIOW_BASE,
    312 	I80312_PCI_XLATE_IOSIZE,
    313 	VM_PROT_READ|VM_PROT_WRITE,
    314 	PTE_NOCACHE,
    315     },
    316     {
    317 	IQ80310_80312_VBASE,
    318 	I80312_PMMR_BASE,
    319 	I80312_PMMR_SIZE,
    320 	VM_PROT_READ|VM_PROT_WRITE,
    321 	PTE_NOCACHE,
    322     },
    323 
    324     {
    325 	0,
    326 	0,
    327 	0,
    328 	0,
    329 	0,
    330     }
    331 };
    332 
    333 /*
    334  * u_int initarm(...)
    335  *
    336  * Initial entry point on startup. This gets called before main() is
    337  * entered.
    338  * It should be responsible for setting up everything that must be
    339  * in place when main is called.
    340  * This includes
    341  *   Taking a copy of the boot configuration structure.
    342  *   Initialising the physical console so characters can be printed.
    343  *   Setting up page tables for the kernel
    344  *   Relocating the kernel to the bottom of physical memory
    345  */
    346 u_int
    347 initarm(void *arg)
    348 {
    349 	extern vaddr_t xscale_cache_clean_addr;
    350 #ifdef DIAGNOSTIC
    351 	extern vsize_t xscale_minidata_clean_size;
    352 #endif
    353 	int loop;
    354 	int loop1;
    355 	u_int l1pagetable;
    356 	pv_addr_t kernel_l1pt;
    357 	paddr_t memstart;
    358 	psize_t memsize;
    359 
    360 	/*
    361 	 * Clear out the 7-segment display.  Whee, the first visual
    362 	 * indication that we're running kernel code.
    363 	 */
    364 	iq80310_7seg(' ', ' ');
    365 
    366 	/*
    367 	 * Heads up ... Setup the CPU / MMU / TLB functions
    368 	 */
    369 	if (set_cpufuncs())
    370 		panic("cpu not recognized!");
    371 
    372 	/* Calibrate the delay loop. */
    373 	iq80310_calibrate_delay();
    374 
    375 	/*
    376 	 * Since we map the on-board devices VA==PA, and the kernel
    377 	 * is running VA==PA, it's possible for us to initialize
    378 	 * the console now.
    379 	 */
    380 	consinit();
    381 
    382 #ifdef VERBOSE_INIT_ARM
    383 	/* Talk to the user */
    384 	printf("\nNetBSD/evbarm (IQ80310) booting ...\n");
    385 #endif
    386 
    387 	/*
    388 	 * Reset the secondary PCI bus.  RedBoot doesn't stop devices
    389 	 * on the PCI bus before handing us control, so we have to
    390 	 * do this.
    391 	 *
    392 	 * XXX This is arguably a bug in RedBoot, and doing this reset
    393 	 * XXX could be problematic in the future if we encounter an
    394 	 * XXX application where the PPB in the i80312 is used as a
    395 	 * XXX PPB.
    396 	 */
    397 	{
    398 		uint32_t reg;
    399 
    400 #ifdef VERBOSE_INIT_ARM
    401 		printf("Resetting secondary PCI bus...\n");
    402 #endif
    403 		reg = bus_space_read_4(&obio_bs_tag,
    404 		    I80312_PMMR_BASE + I80312_PPB_BASE, PPB_REG_BRIDGECONTROL);
    405 		bus_space_write_4(&obio_bs_tag,
    406 		    I80312_PMMR_BASE + I80312_PPB_BASE, PPB_REG_BRIDGECONTROL,
    407 		    reg | PPB_BC_SECONDARY_RESET);
    408 		delay(10 * 1000);	/* 10ms enough? */
    409 		bus_space_write_4(&obio_bs_tag,
    410 		    I80312_PMMR_BASE + I80312_PPB_BASE, PPB_REG_BRIDGECONTROL,
    411 		    reg);
    412 	}
    413 
    414 	/*
    415 	 * We are currently running with the MMU enabled and the
    416 	 * entire address space mapped VA==PA, except for the
    417 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    418 	 * There is an L1 page table at 0xa0004000.
    419 	 */
    420 
    421 	/*
    422 	 * Fetch the SDRAM start/size from the i80312 SDRAM configration
    423 	 * registers.
    424 	 */
    425 	i80312_sdram_bounds(&obio_bs_tag, I80312_PMMR_BASE + I80312_MEM_BASE,
    426 	    &memstart, &memsize);
    427 
    428 #ifdef VERBOSE_INIT_ARM
    429 	printf("initarm: Configuring system ...\n");
    430 #endif
    431 
    432 	/* Fake bootconfig structure for the benefit of pmap.c */
    433 	/* XXX must make the memory description h/w independant */
    434 	bootconfig.dramblocks = 1;
    435 	bootconfig.dram[0].address = memstart;
    436 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    437 
    438 	/*
    439 	 * Set up the variables that define the availablilty of
    440 	 * physical memory.  For now, we're going to set
    441 	 * physical_freestart to 0xa0200000 (where the kernel
    442 	 * was loaded), and allocate the memory we need downwards.
    443 	 * If we get too close to the L1 table that we set up, we
    444 	 * will panic.  We will update physical_freestart and
    445 	 * physical_freeend later to reflect what pmap_bootstrap()
    446 	 * wants to see.
    447 	 *
    448 	 * XXX pmap_bootstrap() needs an enema.
    449 	 */
    450 	physical_start = bootconfig.dram[0].address;
    451 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    452 
    453 	physical_freestart = 0xa0009000UL;
    454 	physical_freeend = 0xa0200000UL;
    455 
    456 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    457 
    458 #ifdef VERBOSE_INIT_ARM
    459 	/* Tell the user about the memory */
    460 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    461 	    physical_start, physical_end - 1);
    462 #endif
    463 
    464 	/*
    465 	 * Okay, the kernel starts 2MB in from the bottom of physical
    466 	 * memory.  We are going to allocate our bootstrap pages downwards
    467 	 * from there.
    468 	 *
    469 	 * We need to allocate some fixed page tables to get the kernel
    470 	 * going.  We allocate one page directory and a number of page
    471 	 * tables and store the physical addresses in the kernel_pt_table
    472 	 * array.
    473 	 *
    474 	 * The kernel page directory must be on a 16K boundary.  The page
    475 	 * tables must be on 4K bounaries.  What we do is allocate the
    476 	 * page directory on the first 16K boundary that we encounter, and
    477 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    478 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    479 	 * least one 16K aligned region.
    480 	 */
    481 
    482 #ifdef VERBOSE_INIT_ARM
    483 	printf("Allocating page tables\n");
    484 #endif
    485 
    486 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    487 
    488 #ifdef VERBOSE_INIT_ARM
    489 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    490 	       physical_freestart, free_pages, free_pages);
    491 #endif
    492 
    493 	/* Define a macro to simplify memory allocation */
    494 #define	valloc_pages(var, np)				\
    495 	alloc_pages((var).pv_pa, (np));			\
    496 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    497 
    498 #define alloc_pages(var, np)				\
    499 	physical_freeend -= ((np) * PAGE_SIZE);		\
    500 	if (physical_freeend < physical_freestart)	\
    501 		panic("initarm: out of memory");	\
    502 	(var) = physical_freeend;			\
    503 	free_pages -= (np);				\
    504 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    505 
    506 	loop1 = 0;
    507 	kernel_l1pt.pv_pa = 0;
    508 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    509 		/* Are we 16KB aligned for an L1 ? */
    510 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    511 		    && kernel_l1pt.pv_pa == 0) {
    512 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    513 		} else {
    514 			valloc_pages(kernel_pt_table[loop1],
    515 			    L2_TABLE_SIZE / PAGE_SIZE);
    516 			++loop1;
    517 		}
    518 	}
    519 
    520 	/* This should never be able to happen but better confirm that. */
    521 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    522 		panic("initarm: Failed to align the kernel page directory");
    523 
    524 	/*
    525 	 * Allocate a page for the system page mapped to V0x00000000
    526 	 * This page will just contain the system vectors and can be
    527 	 * shared by all processes.
    528 	 */
    529 	alloc_pages(systempage.pv_pa, 1);
    530 
    531 	/* Allocate stacks for all modes */
    532 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    533 	valloc_pages(abtstack, ABT_STACK_SIZE);
    534 	valloc_pages(undstack, UND_STACK_SIZE);
    535 	valloc_pages(kernelstack, UPAGES);
    536 
    537 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    538 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    539 	valloc_pages(minidataclean, 1);
    540 
    541 #ifdef VERBOSE_INIT_ARM
    542 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    543 	    irqstack.pv_va);
    544 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    545 	    abtstack.pv_va);
    546 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    547 	    undstack.pv_va);
    548 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    549 	    kernelstack.pv_va);
    550 #endif
    551 
    552 	/*
    553 	 * XXX Defer this to later so that we can reclaim the memory
    554 	 * XXX used by the RedBoot page tables.
    555 	 */
    556 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    557 
    558 	/*
    559 	 * Ok we have allocated physical pages for the primary kernel
    560 	 * page tables
    561 	 */
    562 
    563 #ifdef VERBOSE_INIT_ARM
    564 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    565 #endif
    566 
    567 	/*
    568 	 * Now we start construction of the L1 page table
    569 	 * We start by mapping the L2 page tables into the L1.
    570 	 * This means that we can replace L1 mappings later on if necessary
    571 	 */
    572 	l1pagetable = kernel_l1pt.pv_pa;
    573 
    574 	/* Map the L2 pages tables in the L1 page table */
    575 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    576 	    &kernel_pt_table[KERNEL_PT_SYS]);
    577 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    578 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    579 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    580 	pmap_link_l2pt(l1pagetable, IQ80310_IOPXS_VBASE,
    581 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    582 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    583 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    584 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    585 
    586 	/* update the top of the kernel VM */
    587 	pmap_curmaxkvaddr =
    588 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    589 
    590 #ifdef VERBOSE_INIT_ARM
    591 	printf("Mapping kernel\n");
    592 #endif
    593 
    594 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    595 	{
    596 		extern char etext[], _end[];
    597 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    598 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    599 		u_int logical;
    600 
    601 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    602 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    603 
    604 		logical = 0x00200000;	/* offset of kernel in RAM */
    605 
    606 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    607 		    physical_start + logical, textsize,
    608 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    609 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    610 		    physical_start + logical, totalsize - textsize,
    611 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    612 	}
    613 
    614 #ifdef VERBOSE_INIT_ARM
    615 	printf("Constructing L2 page tables\n");
    616 #endif
    617 
    618 	/* Map the stack pages */
    619 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    620 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    621 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    622 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    623 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    624 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    625 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    626 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    627 
    628 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    629 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    630 
    631 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    632 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    633 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    634 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    635 	}
    636 
    637 	/* Map the Mini-Data cache clean area. */
    638 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    639 	    minidataclean.pv_pa);
    640 
    641 	/* Map the vector page. */
    642 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    643 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    644 
    645 	/* Map the statically mapped devices. */
    646 	pmap_devmap_bootstrap(l1pagetable, iq80310_devmap);
    647 
    648 	/*
    649 	 * Give the XScale global cache clean code an appropriately
    650 	 * sized chunk of unmapped VA space starting at 0xff000000
    651 	 * (our device mappings end before this address).
    652 	 */
    653 	xscale_cache_clean_addr = 0xff000000U;
    654 
    655 	/*
    656 	 * Now we have the real page tables in place so we can switch to them.
    657 	 * Once this is done we will be running with the REAL kernel page
    658 	 * tables.
    659 	 */
    660 
    661 	/*
    662 	 * Update the physical_freestart/physical_freeend/free_pages
    663 	 * variables.
    664 	 */
    665 	{
    666 		extern char _end[];
    667 
    668 		physical_freestart = physical_start +
    669 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    670 		     KERNEL_BASE);
    671 		physical_freeend = physical_end;
    672 		free_pages =
    673 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    674 	}
    675 
    676 	/* Switch tables */
    677 #ifdef VERBOSE_INIT_ARM
    678 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    679 	       physical_freestart, free_pages, free_pages);
    680 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    681 #endif
    682 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    683 	setttb(kernel_l1pt.pv_pa);
    684 	cpu_tlb_flushID();
    685 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    686 
    687 	/*
    688 	 * Moved from cpu_startup() as data_abort_handler() references
    689 	 * this during uvm init
    690 	 */
    691 	proc0paddr = (struct user *)kernelstack.pv_va;
    692 	lwp0.l_addr = proc0paddr;
    693 
    694 #ifdef VERBOSE_INIT_ARM
    695 	printf("done!\n");
    696 #endif
    697 
    698 #ifdef VERBOSE_INIT_ARM
    699 	printf("bootstrap done.\n");
    700 #endif
    701 
    702 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    703 
    704 	/*
    705 	 * Pages were allocated during the secondary bootstrap for the
    706 	 * stacks for different CPU modes.
    707 	 * We must now set the r13 registers in the different CPU modes to
    708 	 * point to these stacks.
    709 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    710 	 * of the stack memory.
    711 	 */
    712 #ifdef VERBOSE_INIT_ARM
    713 	printf("init subsystems: stacks ");
    714 #endif
    715 
    716 	set_stackptr(PSR_IRQ32_MODE,
    717 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    718 	set_stackptr(PSR_ABT32_MODE,
    719 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    720 	set_stackptr(PSR_UND32_MODE,
    721 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    722 
    723 	/*
    724 	 * Well we should set a data abort handler.
    725 	 * Once things get going this will change as we will need a proper
    726 	 * handler.
    727 	 * Until then we will use a handler that just panics but tells us
    728 	 * why.
    729 	 * Initialisation of the vectors will just panic on a data abort.
    730 	 * This just fills in a slighly better one.
    731 	 */
    732 #ifdef VERBOSE_INIT_ARM
    733 	printf("vectors ");
    734 #endif
    735 	data_abort_handler_address = (u_int)data_abort_handler;
    736 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    737 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    738 
    739 	/* Initialise the undefined instruction handlers */
    740 #ifdef VERBOSE_INIT_ARM
    741 	printf("undefined ");
    742 #endif
    743 	undefined_init();
    744 
    745 	/* Load memory into UVM. */
    746 #ifdef VERBOSE_INIT_ARM
    747 	printf("page ");
    748 #endif
    749 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    750 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    751 	    atop(physical_freestart), atop(physical_freeend),
    752 	    VM_FREELIST_DEFAULT);
    753 
    754 	/* Boot strap pmap telling it where the kernel page table is */
    755 #ifdef VERBOSE_INIT_ARM
    756 	printf("pmap ");
    757 #endif
    758 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    759 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    760 
    761 	/* Setup the IRQ system */
    762 #ifdef VERBOSE_INIT_ARM
    763 	printf("irq ");
    764 #endif
    765 	iq80310_intr_init();
    766 
    767 #ifdef VERBOSE_INIT_ARM
    768 	printf("done.\n");
    769 #endif
    770 
    771 #ifdef IPKDB
    772 	/* Initialise ipkdb */
    773 	ipkdb_init();
    774 	if (boothowto & RB_KDB)
    775 		ipkdb_connect(0);
    776 #endif
    777 
    778 #if NKSYMS || defined(DDB) || defined(LKM)
    779 	/* Firmware doesn't load symbols. */
    780 	ksyms_init(0, NULL, NULL);
    781 #endif
    782 
    783 #ifdef DDB
    784 	db_machine_init();
    785 	if (boothowto & RB_KDB)
    786 		Debugger();
    787 #endif
    788 
    789 	/* We return the new stack pointer address */
    790 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    791 }
    792 
    793 void
    794 consinit(void)
    795 {
    796 	static const bus_addr_t comcnaddrs[] = {
    797 		IQ80310_UART2,		/* com0 (J9) */
    798 		IQ80310_UART1,		/* com1 (J10) */
    799 	};
    800 	static int consinit_called;
    801 
    802 	if (consinit_called != 0)
    803 		return;
    804 
    805 	consinit_called = 1;
    806 
    807 	/*
    808 	 * Console devices are mapped VA==PA.  Our devmap reflects
    809 	 * this, so register it now so drivers can map the console
    810 	 * device.
    811 	 */
    812 	pmap_devmap_register(iq80310_devmap);
    813 
    814 #if NCOM > 0
    815 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    816 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    817 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    818 #else
    819 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    820 #endif
    821 }
    822