Home | History | Annotate | Line # | Download | only in npwr_fc
npwr_fc_machdep.c revision 1.7
      1 /*	$NetBSD: npwr_fc_machdep.c,v 1.7 2008/11/11 06:46:41 dyoung 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.7 2008/11/11 06:46:41 dyoung 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 int physmem = 0;
    157 
    158 /*int debug_flags;*/
    159 #ifndef PMAP_STATIC_L1S
    160 int max_processes = 64;			/* Default number */
    161 #endif	/* !PMAP_STATIC_L1S */
    162 
    163 /* Physical and virtual addresses for some global pages */
    164 pv_addr_t irqstack;
    165 pv_addr_t undstack;
    166 pv_addr_t abtstack;
    167 pv_addr_t kernelstack;
    168 pv_addr_t minidataclean;
    169 
    170 vm_offset_t msgbufphys;
    171 
    172 extern u_int data_abort_handler_address;
    173 extern u_int prefetch_abort_handler_address;
    174 extern u_int undefined_handler_address;
    175 
    176 #ifdef PMAP_DEBUG
    177 extern int pmap_debug_level;
    178 #endif
    179 
    180 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    181 
    182 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    183 #define	KERNEL_PT_KERNEL_NUM	4
    184 
    185 					/* L2 table for mapping i80321 */
    186 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    187 
    188 					/* L2 tables for mapping kernel VM */
    189 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
    190 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    191 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    192 
    193 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    194 
    195 struct user *proc0paddr;
    196 
    197 /* Prototypes */
    198 
    199 void	consinit(void);
    200 
    201 #include "com.h"
    202 #if NCOM > 0
    203 #include <dev/ic/comreg.h>
    204 #include <dev/ic/comvar.h>
    205 #endif
    206 
    207 /*
    208  * Define the default console speed for the board.  This is generally
    209  * what the firmware provided with the board defaults to.
    210  */
    211 #ifndef CONSPEED
    212 #define CONSPEED B115200
    213 #endif /* ! CONSPEED */
    214 
    215 #ifndef CONUNIT
    216 #define	CONUNIT	0
    217 #endif
    218 
    219 #ifndef CONMODE
    220 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    221 #endif
    222 
    223 int comcnspeed = CONSPEED;
    224 int comcnmode = CONMODE;
    225 int comcnunit = CONUNIT;
    226 
    227 #if KGDB
    228 #ifndef KGDB_DEVNAME
    229 #error Must define KGDB_DEVNAME
    230 #endif
    231 const char kgdb_devname[] = KGDB_DEVNAME;
    232 
    233 #ifndef KGDB_DEVADDR
    234 #error Must define KGDB_DEVADDR
    235 #endif
    236 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    237 
    238 #ifndef KGDB_DEVRATE
    239 #define KGDB_DEVRATE	CONSPEED
    240 #endif
    241 int kgdb_devrate = KGDB_DEVRATE;
    242 
    243 #ifndef KGDB_DEVMODE
    244 #define KGDB_DEVMODE	CONMODE
    245 #endif
    246 int kgdb_devmode = KGDB_DEVMODE;
    247 #endif /* KGDB */
    248 
    249 /*
    250  * void cpu_reboot(int howto, char *bootstr)
    251  *
    252  * Reboots the system
    253  *
    254  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    255  * then reset the CPU.
    256  */
    257 void
    258 cpu_reboot(int howto, char *bootstr)
    259 {
    260 
    261 	/*
    262 	 * If we are still cold then hit the air brakes
    263 	 * and crash to earth fast
    264 	 */
    265 	if (cold) {
    266 		doshutdownhooks();
    267 		pmf_system_shutdown(boothowto);
    268 		printf("The operating system has halted.\n");
    269 		printf("Please press any key to reboot.\n\n");
    270 		cngetc();
    271 		printf("rebooting...\n");
    272 		goto reset;
    273 	}
    274 
    275 	/* Disable console buffering */
    276 
    277 	/*
    278 	 * If RB_NOSYNC was not specified sync the discs.
    279 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    280 	 * unmount.  It looks like syslogd is getting woken up only to find
    281 	 * that it cannot page part of the binary in as the filesystem has
    282 	 * been unmounted.
    283 	 */
    284 	if (!(howto & RB_NOSYNC))
    285 		bootsync();
    286 
    287 	/* Say NO to interrupts */
    288 	splhigh();
    289 
    290 	/* Do a dump if requested. */
    291 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    292 		dumpsys();
    293 
    294 	/* Run any shutdown hooks */
    295 	doshutdownhooks();
    296 
    297 	pmf_system_shutdown(boothowto);
    298 
    299 	/* Make sure IRQ's are disabled */
    300 	IRQdisable;
    301 
    302 	if (howto & RB_HALT) {
    303 		printf("The operating system has halted.\n");
    304 		printf("Please press any key to reboot.\n\n");
    305 		cngetc();
    306 	}
    307 
    308 	printf("rebooting...\n\r");
    309  reset:
    310 	/*
    311 	 * Make really really sure that all interrupts are disabled,
    312 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    313 	 */
    314 	(void) disable_interrupts(I32_bit|F32_bit);
    315 	*(volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
    316 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
    317 
    318 	/* ...and if that didn't work, just croak. */
    319 	printf("RESET FAILED!\n");
    320 	for (;;);
    321 }
    322 
    323 /* Static device mappings. */
    324 static const struct pmap_devmap iq80321_devmap[] = {
    325     /*
    326      * Map the on-board devices VA == PA so that we can access them
    327      * with the MMU on or off.
    328      */
    329     {
    330 	IQ80321_OBIO_BASE,
    331 	IQ80321_OBIO_BASE,
    332 	IQ80321_OBIO_SIZE,
    333 	VM_PROT_READ|VM_PROT_WRITE,
    334 	PTE_NOCACHE,
    335     },
    336 
    337     {
    338 	IQ80321_IOW_VBASE,
    339 	VERDE_OUT_XLATE_IO_WIN0_BASE,
    340 	VERDE_OUT_XLATE_IO_WIN_SIZE,
    341 	VM_PROT_READ|VM_PROT_WRITE,
    342 	PTE_NOCACHE,
    343    },
    344 
    345    {
    346 	IQ80321_80321_VBASE,
    347 	VERDE_PMMR_BASE,
    348 	VERDE_PMMR_SIZE,
    349 	VM_PROT_READ|VM_PROT_WRITE,
    350 	PTE_NOCACHE,
    351    },
    352 
    353    {
    354 	0,
    355 	0,
    356 	0,
    357 	0,
    358 	0,
    359     }
    360 };
    361 
    362 /*
    363  * u_int initarm(...)
    364  *
    365  * Initial entry point on startup. This gets called before main() is
    366  * entered.
    367  * It should be responsible for setting up everything that must be
    368  * in place when main is called.
    369  * This includes
    370  *   Taking a copy of the boot configuration structure.
    371  *   Initialising the physical console so characters can be printed.
    372  *   Setting up page tables for the kernel
    373  *   Relocating the kernel to the bottom of physical memory
    374  */
    375 u_int
    376 initarm(void *arg)
    377 {
    378 	extern vaddr_t xscale_cache_clean_addr;
    379 #ifdef DIAGNOSTIC
    380 	extern vsize_t xscale_minidata_clean_size;
    381 #endif
    382 	int loop;
    383 	int loop1;
    384 	u_int l1pagetable;
    385 	paddr_t memstart;
    386 	psize_t memsize;
    387 
    388 	/* Calibrate the delay loop. */
    389 	i80321_calibrate_delay();
    390 	i80321_hardclock_hook = NULL;
    391 
    392 	/*
    393 	 * Since we map the on-board devices VA==PA, and the kernel
    394 	 * is running VA==PA, it's possible for us to initialize
    395 	 * the console now.
    396 	 */
    397 	consinit();
    398 
    399 #ifdef VERBOSE_INIT_ARM
    400 	/* Talk to the user */
    401 	printf("\nNetBSD/evbarm (NPWR_FC) booting ...\n");
    402 #endif
    403 
    404 	/*
    405 	 * Heads up ... Setup the CPU / MMU / TLB functions
    406 	 */
    407 	if (set_cpufuncs())
    408 		panic("cpu not recognized!");
    409 
    410 	/*
    411 	 * We are currently running with the MMU enabled and the
    412 	 * entire address space mapped VA==PA, except for the
    413 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    414 	 * There is an L1 page table at 0xa0004000.
    415 	 */
    416 
    417 	/*
    418 	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
    419 	 * registers.
    420 	 */
    421 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    422 	    &memstart, &memsize);
    423 
    424 #ifdef VERBOSE_INIT_ARM
    425 	printf("initarm: Configuring system ...\n");
    426 #endif
    427 
    428 	/* Fake bootconfig structure for the benefit of pmap.c */
    429 	/* XXX must make the memory description h/w independent */
    430 	bootconfig.dramblocks = 1;
    431 	bootconfig.dram[0].address = memstart;
    432 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    433 
    434 	/*
    435 	 * Set up the variables that define the availablilty of
    436 	 * physical memory.  For now, we're going to set
    437 	 * physical_freestart to 0xa0200000 (where the kernel
    438 	 * was loaded), and allocate the memory we need downwards.
    439 	 * If we get too close to the L1 table that we set up, we
    440 	 * will panic.  We will update physical_freestart and
    441 	 * physical_freeend later to reflect what pmap_bootstrap()
    442 	 * wants to see.
    443 	 *
    444 	 * XXX pmap_bootstrap() needs an enema.
    445 	 */
    446 	physical_start = bootconfig.dram[0].address;
    447 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    448 
    449 	physical_freestart = 0xa0009000UL;
    450 	physical_freeend = 0xa0200000UL;
    451 
    452 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    453 
    454 #ifdef VERBOSE_INIT_ARM
    455 	/* Tell the user about the memory */
    456 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    457 	    physical_start, physical_end - 1);
    458 #endif
    459 
    460 	/*
    461 	 * Okay, the kernel starts 2MB in from the bottom of physical
    462 	 * memory.  We are going to allocate our bootstrap pages downwards
    463 	 * from there.
    464 	 *
    465 	 * We need to allocate some fixed page tables to get the kernel
    466 	 * going.  We allocate one page directory and a number of page
    467 	 * tables and store the physical addresses in the kernel_pt_table
    468 	 * array.
    469 	 *
    470 	 * The kernel page directory must be on a 16K boundary.  The page
    471 	 * tables must be on 4K bounaries.  What we do is allocate the
    472 	 * page directory on the first 16K boundary that we encounter, and
    473 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    474 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    475 	 * least one 16K aligned region.
    476 	 */
    477 
    478 #ifdef VERBOSE_INIT_ARM
    479 	printf("Allocating page tables\n");
    480 #endif
    481 
    482 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    483 
    484 #ifdef VERBOSE_INIT_ARM
    485 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    486 	       physical_freestart, free_pages, free_pages);
    487 #endif
    488 
    489 	/* Define a macro to simplify memory allocation */
    490 #define	valloc_pages(var, np)				\
    491 	alloc_pages((var).pv_pa, (np));			\
    492 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    493 
    494 #define alloc_pages(var, np)				\
    495 	physical_freeend -= ((np) * PAGE_SIZE);		\
    496 	if (physical_freeend < physical_freestart)	\
    497 		panic("initarm: out of memory");	\
    498 	(var) = physical_freeend;			\
    499 	free_pages -= (np);				\
    500 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    501 
    502 	loop1 = 0;
    503 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    504 		/* Are we 16KB aligned for an L1 ? */
    505 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    506 		    && kernel_l1pt.pv_pa == 0) {
    507 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    508 		} else {
    509 			valloc_pages(kernel_pt_table[loop1],
    510 			    L2_TABLE_SIZE / PAGE_SIZE);
    511 			++loop1;
    512 		}
    513 	}
    514 
    515 	/* This should never be able to happen but better confirm that. */
    516 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    517 		panic("initarm: Failed to align the kernel page directory");
    518 
    519 	/*
    520 	 * Allocate a page for the system page mapped to V0x00000000
    521 	 * This page will just contain the system vectors and can be
    522 	 * shared by all processes.
    523 	 */
    524 	alloc_pages(systempage.pv_pa, 1);
    525 
    526 	/* Allocate stacks for all modes */
    527 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    528 	valloc_pages(abtstack, ABT_STACK_SIZE);
    529 	valloc_pages(undstack, UND_STACK_SIZE);
    530 	valloc_pages(kernelstack, UPAGES);
    531 
    532 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    533 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    534 	valloc_pages(minidataclean, 1);
    535 
    536 #ifdef VERBOSE_INIT_ARM
    537 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    538 	    irqstack.pv_va);
    539 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    540 	    abtstack.pv_va);
    541 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    542 	    undstack.pv_va);
    543 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    544 	    kernelstack.pv_va);
    545 #endif
    546 
    547 	/*
    548 	 * XXX Defer this to later so that we can reclaim the memory
    549 	 * XXX used by the RedBoot page tables.
    550 	 */
    551 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    552 
    553 	/*
    554 	 * Ok we have allocated physical pages for the primary kernel
    555 	 * page tables
    556 	 */
    557 
    558 #ifdef VERBOSE_INIT_ARM
    559 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    560 #endif
    561 
    562 	/*
    563 	 * Now we start construction of the L1 page table
    564 	 * We start by mapping the L2 page tables into the L1.
    565 	 * This means that we can replace L1 mappings later on if necessary
    566 	 */
    567 	l1pagetable = kernel_l1pt.pv_pa;
    568 
    569 	/* Map the L2 pages tables in the L1 page table */
    570 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    571 	    &kernel_pt_table[KERNEL_PT_SYS]);
    572 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    573 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    574 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    575 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
    576 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    577 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    578 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    579 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    580 
    581 	/* update the top of the kernel VM */
    582 	pmap_curmaxkvaddr =
    583 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    584 
    585 #ifdef VERBOSE_INIT_ARM
    586 	printf("Mapping kernel\n");
    587 #endif
    588 
    589 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    590 	{
    591 		extern char etext[], _end[];
    592 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    593 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    594 		u_int logical;
    595 
    596 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    597 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    598 
    599 		logical = 0x00200000;	/* offset of kernel in RAM */
    600 
    601 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    602 		    physical_start + logical, textsize,
    603 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    604 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    605 		    physical_start + logical, totalsize - textsize,
    606 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    607 	}
    608 
    609 #ifdef VERBOSE_INIT_ARM
    610 	printf("Constructing L2 page tables\n");
    611 #endif
    612 
    613 	/* Map the stack pages */
    614 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    615 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    616 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    617 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    618 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    619 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    620 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    621 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    622 
    623 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    624 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    625 
    626 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    627 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    628 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    629 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    630 	}
    631 
    632 	/* Map the Mini-Data cache clean area. */
    633 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    634 	    minidataclean.pv_pa);
    635 
    636 	/* Map the vector page. */
    637 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    638 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    639 
    640 	/* Map the statically mapped devices. */
    641 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
    642 
    643 	/*
    644 	 * Give the XScale global cache clean code an appropriately
    645 	 * sized chunk of unmapped VA space starting at 0xff000000
    646 	 * (our device mappings end before this address).
    647 	 */
    648 	xscale_cache_clean_addr = 0xff000000U;
    649 
    650 	/*
    651 	 * Now we have the real page tables in place so we can switch to them.
    652 	 * Once this is done we will be running with the REAL kernel page
    653 	 * tables.
    654 	 */
    655 
    656 	/*
    657 	 * Update the physical_freestart/physical_freeend/free_pages
    658 	 * variables.
    659 	 */
    660 	{
    661 		extern char _end[];
    662 
    663 		physical_freestart = physical_start +
    664 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    665 		     KERNEL_BASE);
    666 		physical_freeend = physical_end;
    667 		free_pages =
    668 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    669 	}
    670 
    671 	/* Switch tables */
    672 #ifdef VERBOSE_INIT_ARM
    673 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    674 	       physical_freestart, free_pages, free_pages);
    675 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    676 #endif
    677 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    678 	setttb(kernel_l1pt.pv_pa);
    679 	cpu_tlb_flushID();
    680 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    681 
    682 	/*
    683 	 * Moved from cpu_startup() as data_abort_handler() references
    684 	 * this during uvm init
    685 	 */
    686 	proc0paddr = (struct user *)kernelstack.pv_va;
    687 	lwp0.l_addr = proc0paddr;
    688 
    689 #ifdef VERBOSE_INIT_ARM
    690 	printf("done!\n");
    691 #endif
    692 
    693 #ifdef VERBOSE_INIT_ARM
    694 	printf("bootstrap done.\n");
    695 #endif
    696 
    697 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    698 
    699 	/*
    700 	 * Pages were allocated during the secondary bootstrap for the
    701 	 * stacks for different CPU modes.
    702 	 * We must now set the r13 registers in the different CPU modes to
    703 	 * point to these stacks.
    704 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    705 	 * of the stack memory.
    706 	 */
    707 #ifdef VERBOSE_INIT_ARM
    708 	printf("init subsystems: stacks ");
    709 #endif
    710 
    711 	set_stackptr(PSR_IRQ32_MODE,
    712 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    713 	set_stackptr(PSR_ABT32_MODE,
    714 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    715 	set_stackptr(PSR_UND32_MODE,
    716 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    717 
    718 	/*
    719 	 * Well we should set a data abort handler.
    720 	 * Once things get going this will change as we will need a proper
    721 	 * handler.
    722 	 * Until then we will use a handler that just panics but tells us
    723 	 * why.
    724 	 * Initialisation of the vectors will just panic on a data abort.
    725 	 * This just fills in a slighly better one.
    726 	 */
    727 #ifdef VERBOSE_INIT_ARM
    728 	printf("vectors ");
    729 #endif
    730 	data_abort_handler_address = (u_int)data_abort_handler;
    731 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    732 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    733 
    734 	/* Initialise the undefined instruction handlers */
    735 #ifdef VERBOSE_INIT_ARM
    736 	printf("undefined ");
    737 #endif
    738 	undefined_init();
    739 
    740 	/* Load memory into UVM. */
    741 #ifdef VERBOSE_INIT_ARM
    742 	printf("page ");
    743 #endif
    744 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    745 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    746 	    atop(physical_freestart), atop(physical_freeend),
    747 	    VM_FREELIST_DEFAULT);
    748 
    749 	/* Boot strap pmap telling it where the kernel page table is */
    750 #ifdef VERBOSE_INIT_ARM
    751 	printf("pmap ");
    752 #endif
    753 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    754 
    755 	/* Setup the IRQ system */
    756 #ifdef VERBOSE_INIT_ARM
    757 	printf("irq ");
    758 #endif
    759 	i80321_intr_init();
    760 
    761 #ifdef VERBOSE_INIT_ARM
    762 	printf("done.\n");
    763 #endif
    764 
    765 #ifdef BOOTHOWTO
    766 	boothowto = BOOTHOWTO;
    767 #endif
    768 
    769 #if NKSYMS || defined(DDB) || defined(LKM)
    770 	/* Firmware doesn't load symbols. */
    771 	ksyms_init(0, NULL, NULL);
    772 #endif
    773 
    774 #ifdef DDB
    775 	db_machine_init();
    776 	if (boothowto & RB_KDB)
    777 		Debugger();
    778 #endif
    779 
    780 	/* We return the new stack pointer address */
    781 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    782 }
    783 
    784 void
    785 consinit(void)
    786 {
    787 	static const bus_addr_t comcnaddrs[] = {
    788 		IQ80321_UART1,		/* com0 */
    789 	};
    790 	static int consinit_called;
    791 
    792 	if (consinit_called != 0)
    793 		return;
    794 
    795 	consinit_called = 1;
    796 
    797 	/*
    798 	 * Console devices are mapped VA==PA.  Our devmap reflects
    799 	 * this, so register it now so drivers can map the console
    800 	 * device.
    801 	 */
    802 	pmap_devmap_register(iq80321_devmap);
    803 
    804 #if NCOM > 0
    805 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    806 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    807 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    808 #else
    809 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    810 #endif
    811 #if KGDB
    812 #if NCOM > 0
    813 	if (strcmp(kgdb_devname, "com") == 0) {
    814 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
    815 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
    816 	}
    817 #endif	/* NCOM > 0 */
    818 #endif	/* KGDB */
    819 }
    820