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