Home | History | Annotate | Line # | Download | only in iq80310
      1 /*	$NetBSD: iq80310_machdep.c,v 1.97 2024/02/20 23:36:02 andvar 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.97 2024/02/20 23:36:02 andvar 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     DEVMAP_ENTRY(
    267 	IQ80310_OBIO_BASE,
    268 	IQ80310_OBIO_BASE,
    269 	IQ80310_OBIO_SIZE
    270     ),
    271     DEVMAP_ENTRY(
    272 	IQ80310_PIOW_VBASE,
    273 	I80312_PCI_XLATE_PIOW_BASE,
    274 	I80312_PCI_XLATE_IOSIZE
    275     ),
    276     DEVMAP_ENTRY(
    277 	IQ80310_SIOW_VBASE,
    278 	I80312_PCI_XLATE_SIOW_BASE,
    279 	I80312_PCI_XLATE_IOSIZE
    280     ),
    281     DEVMAP_ENTRY(
    282 	IQ80310_80312_VBASE,
    283 	I80312_PMMR_BASE,
    284 	I80312_PMMR_SIZE
    285     ),
    286 
    287     DEVMAP_ENTRY_END
    288 };
    289 
    290 /*
    291  * vaddr_t initarm(...)
    292  *
    293  * Initial entry point on startup. This gets called before main() is
    294  * entered.
    295  * It should be responsible for setting up everything that must be
    296  * in place when main is called.
    297  * This includes
    298  *   Taking a copy of the boot configuration structure.
    299  *   Initialising the physical console so characters can be printed.
    300  *   Setting up page tables for the kernel
    301  *   Relocating the kernel to the bottom of physical memory
    302  */
    303 vaddr_t
    304 initarm(void *arg)
    305 {
    306 	int loop;
    307 	int loop1;
    308 	u_int l1pagetable;
    309 	paddr_t memstart;
    310 	psize_t memsize;
    311 
    312 	/*
    313 	 * Clear out the 7-segment display.  Whee, the first visual
    314 	 * indication that we're running kernel code.
    315 	 */
    316 	iq80310_7seg(' ', ' ');
    317 
    318 	/*
    319 	 * Heads up ... Setup the CPU / MMU / TLB functions
    320 	 */
    321 	if (set_cpufuncs())
    322 		panic("CPU not recognized!");
    323 
    324 	/* Calibrate the delay loop. */
    325 	iq80310_calibrate_delay();
    326 
    327 	/*
    328 	 * Since we map the on-board devices VA==PA, and the kernel
    329 	 * is running VA==PA, it's possible for us to initialize
    330 	 * the console now.
    331 	 */
    332 	consinit();
    333 
    334 #ifdef VERBOSE_INIT_ARM
    335 	/* Talk to the user */
    336 	printf("\nNetBSD/evbarm (IQ80310) booting ...\n");
    337 #endif
    338 
    339 	/*
    340 	 * Reset the secondary PCI bus.  RedBoot doesn't stop devices
    341 	 * on the PCI bus before handing us control, so we have to
    342 	 * do this.
    343 	 *
    344 	 * XXX This is arguably a bug in RedBoot, and doing this reset
    345 	 * XXX could be problematic in the future if we encounter an
    346 	 * XXX application where the PPB in the i80312 is used as a
    347 	 * XXX PPB.
    348 	 */
    349 	{
    350 		uint32_t reg;
    351 
    352 #ifdef VERBOSE_INIT_ARM
    353 		printf("Resetting secondary PCI bus...\n");
    354 #endif
    355 		reg = bus_space_read_4(&obio_bs_tag,
    356 		    I80312_PMMR_BASE + I80312_PPB_BASE, PCI_BRIDGE_CONTROL_REG);
    357 		bus_space_write_4(&obio_bs_tag,
    358 		    I80312_PMMR_BASE + I80312_PPB_BASE, PCI_BRIDGE_CONTROL_REG,
    359 		    reg | PCI_BRIDGE_CONTROL_SECBR);
    360 		delay(10 * 1000);	/* 10ms enough? */
    361 		bus_space_write_4(&obio_bs_tag,
    362 		    I80312_PMMR_BASE + I80312_PPB_BASE, PCI_BRIDGE_CONTROL_REG,
    363 		    reg);
    364 	}
    365 
    366 	/*
    367 	 * We are currently running with the MMU enabled and the
    368 	 * entire address space mapped VA==PA, except for the
    369 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    370 	 * There is an L1 page table at 0xa0004000.
    371 	 */
    372 
    373 	/*
    374 	 * Fetch the SDRAM start/size from the i80312 SDRAM configuration
    375 	 * registers.
    376 	 */
    377 	i80312_sdram_bounds(&obio_bs_tag, I80312_PMMR_BASE + I80312_MEM_BASE,
    378 	    &memstart, &memsize);
    379 
    380 #ifdef VERBOSE_INIT_ARM
    381 	printf("initarm: Configuring system ...\n");
    382 #endif
    383 
    384 	/* Fake bootconfig structure for the benefit of pmap.c */
    385 	/* XXX must make the memory description h/w independent */
    386 	bootconfig.dramblocks = 1;
    387 	bootconfig.dram[0].address = memstart;
    388 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    389 
    390 	/*
    391 	 * Set up the variables that define the availability of
    392 	 * physical memory.  For now, we're going to set
    393 	 * physical_freestart to 0xa0200000 (where the kernel
    394 	 * was loaded), and allocate the memory we need downwards.
    395 	 * If we get too close to the L1 table that we set up, we
    396 	 * will panic.  We will update physical_freestart and
    397 	 * physical_freeend later to reflect what pmap_bootstrap()
    398 	 * wants to see.
    399 	 *
    400 	 * XXX pmap_bootstrap() needs an enema.
    401 	 */
    402 	physical_start = bootconfig.dram[0].address;
    403 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    404 
    405 	physical_freestart = 0xa0009000UL;
    406 	physical_freeend = 0xa0200000UL;
    407 
    408 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    409 
    410 #ifdef VERBOSE_INIT_ARM
    411 	/* Tell the user about the memory */
    412 	printf("physmemory: 0x%"PRIxPSIZE" pages at 0x%08lx -> 0x%08lx\n", physmem,
    413 	    physical_start, physical_end - 1);
    414 #endif
    415 
    416 	/*
    417 	 * Okay, the kernel starts 2MB in from the bottom of physical
    418 	 * memory.  We are going to allocate our bootstrap pages downwards
    419 	 * from there.
    420 	 *
    421 	 * We need to allocate some fixed page tables to get the kernel
    422 	 * going.  We allocate one page directory and a number of page
    423 	 * tables and store the physical addresses in the kernel_pt_table
    424 	 * array.
    425 	 *
    426 	 * The kernel page directory must be on a 16K boundary.  The page
    427 	 * tables must be on 4K boundaries.  What we do is allocate the
    428 	 * page directory on the first 16K boundary that we encounter, and
    429 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    430 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    431 	 * least one 16K aligned region.
    432 	 */
    433 
    434 #ifdef VERBOSE_INIT_ARM
    435 	printf("Allocating page tables\n");
    436 #endif
    437 
    438 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    439 
    440 #ifdef VERBOSE_INIT_ARM
    441 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    442 	       physical_freestart, free_pages, free_pages);
    443 #endif
    444 
    445 	/* Define a macro to simplify memory allocation */
    446 #define	valloc_pages(var, np)				\
    447 	alloc_pages((var).pv_pa, (np));			\
    448 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    449 
    450 #define alloc_pages(var, np)				\
    451 	physical_freeend -= ((np) * PAGE_SIZE);		\
    452 	if (physical_freeend < physical_freestart)	\
    453 		panic("initarm: out of memory");	\
    454 	(var) = physical_freeend;			\
    455 	free_pages -= (np);				\
    456 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    457 
    458 	loop1 = 0;
    459 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    460 		/* Are we 16KB aligned for an L1 ? */
    461 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    462 		    && kernel_l1pt.pv_pa == 0) {
    463 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    464 		} else {
    465 			valloc_pages(kernel_pt_table[loop1],
    466 			    L2_TABLE_SIZE / PAGE_SIZE);
    467 			++loop1;
    468 		}
    469 	}
    470 
    471 	/* This should never be able to happen but better confirm that. */
    472 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    473 		panic("initarm: Failed to align the kernel page directory");
    474 
    475 	/*
    476 	 * Allocate a page for the system page mapped to V0x00000000
    477 	 * This page will just contain the system vectors and can be
    478 	 * shared by all processes.
    479 	 */
    480 	alloc_pages(systempage.pv_pa, 1);
    481 
    482 	/* Allocate stacks for all modes */
    483 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    484 	valloc_pages(abtstack, ABT_STACK_SIZE);
    485 	valloc_pages(undstack, UND_STACK_SIZE);
    486 	valloc_pages(kernelstack, UPAGES);
    487 
    488 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    489 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    490 	valloc_pages(minidataclean, 1);
    491 
    492 #ifdef VERBOSE_INIT_ARM
    493 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    494 	    irqstack.pv_va);
    495 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    496 	    abtstack.pv_va);
    497 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    498 	    undstack.pv_va);
    499 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    500 	    kernelstack.pv_va);
    501 #endif
    502 
    503 	/*
    504 	 * XXX Defer this to later so that we can reclaim the memory
    505 	 * XXX used by the RedBoot page tables.
    506 	 */
    507 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    508 
    509 	/*
    510 	 * Ok we have allocated physical pages for the primary kernel
    511 	 * page tables
    512 	 */
    513 
    514 #ifdef VERBOSE_INIT_ARM
    515 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    516 #endif
    517 
    518 	/*
    519 	 * Now we start construction of the L1 page table
    520 	 * We start by mapping the L2 page tables into the L1.
    521 	 * This means that we can replace L1 mappings later on if necessary
    522 	 */
    523 	l1pagetable = kernel_l1pt.pv_pa;
    524 
    525 	/* Map the L2 pages tables in the L1 page table */
    526 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    527 	    &kernel_pt_table[KERNEL_PT_SYS]);
    528 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    529 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    530 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    531 	pmap_link_l2pt(l1pagetable, IQ80310_IOPXS_VBASE,
    532 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    533 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    534 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    535 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    536 
    537 	/* update the top of the kernel VM */
    538 	pmap_curmaxkvaddr =
    539 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    540 
    541 #ifdef VERBOSE_INIT_ARM
    542 	printf("Mapping kernel\n");
    543 #endif
    544 
    545 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    546 	{
    547 		extern char etext[], _end[];
    548 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    549 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    550 		u_int logical;
    551 
    552 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    553 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    554 
    555 		logical = 0x00200000;	/* offset of kernel in RAM */
    556 
    557 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    558 		    physical_start + logical, textsize,
    559 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    560 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    561 		    physical_start + logical, totalsize - textsize,
    562 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    563 	}
    564 
    565 #ifdef VERBOSE_INIT_ARM
    566 	printf("Constructing L2 page tables\n");
    567 #endif
    568 
    569 	/* Map the stack pages */
    570 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    571 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    572 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    573 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    574 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    575 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    576 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    577 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    578 
    579 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    580 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    581 
    582 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    583 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    584 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    585 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    586 	}
    587 
    588 	/* Map the Mini-Data cache clean area. */
    589 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    590 	    minidataclean.pv_pa);
    591 
    592 	/* Map the vector page. */
    593 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    594 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    595 
    596 	/* Map the statically mapped devices. */
    597 	pmap_devmap_bootstrap(l1pagetable, iq80310_devmap);
    598 
    599 	/*
    600 	 * Give the XScale global cache clean code an appropriately
    601 	 * sized chunk of unmapped VA space starting at 0xff000000
    602 	 * (our device mappings end before this address).
    603 	 */
    604 	xscale_cache_clean_addr = 0xff000000U;
    605 
    606 	/*
    607 	 * Now we have the real page tables in place so we can switch to them.
    608 	 * Once this is done we will be running with the REAL kernel page
    609 	 * tables.
    610 	 */
    611 
    612 	/*
    613 	 * Update the physical_freestart/physical_freeend/free_pages
    614 	 * variables.
    615 	 */
    616 	{
    617 		extern char _end[];
    618 
    619 		physical_freestart = physical_start +
    620 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    621 		     KERNEL_BASE);
    622 		physical_freeend = physical_end;
    623 		free_pages =
    624 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    625 	}
    626 
    627 	/* Switch tables */
    628 #ifdef VERBOSE_INIT_ARM
    629 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    630 	       physical_freestart, free_pages, free_pages);
    631 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    632 #endif
    633 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    634 	cpu_setttb(kernel_l1pt.pv_pa, true);
    635 	cpu_tlb_flushID();
    636 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    637 
    638 	/*
    639 	 * Moved from cpu_startup() as data_abort_handler() references
    640 	 * this during uvm init
    641 	 */
    642 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    643 
    644 #ifdef VERBOSE_INIT_ARM
    645 	printf("done!\n");
    646 #endif
    647 
    648 #ifdef VERBOSE_INIT_ARM
    649 	printf("bootstrap done.\n");
    650 #endif
    651 
    652 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    653 
    654 	/*
    655 	 * Pages were allocated during the secondary bootstrap for the
    656 	 * stacks for different CPU modes.
    657 	 * We must now set the r13 registers in the different CPU modes to
    658 	 * point to these stacks.
    659 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    660 	 * of the stack memory.
    661 	 */
    662 #ifdef VERBOSE_INIT_ARM
    663 	printf("init subsystems: stacks ");
    664 #endif
    665 
    666 	set_stackptr(PSR_IRQ32_MODE,
    667 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    668 	set_stackptr(PSR_ABT32_MODE,
    669 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    670 	set_stackptr(PSR_UND32_MODE,
    671 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    672 
    673 	/*
    674 	 * Well we should set a data abort handler.
    675 	 * Once things get going this will change as we will need a proper
    676 	 * handler.
    677 	 * Until then we will use a handler that just panics but tells us
    678 	 * why.
    679 	 * Initialisation of the vectors will just panic on a data abort.
    680 	 * This just fills in a slightly better one.
    681 	 */
    682 #ifdef VERBOSE_INIT_ARM
    683 	printf("vectors ");
    684 #endif
    685 	data_abort_handler_address = (u_int)data_abort_handler;
    686 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    687 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    688 
    689 	/* Initialise the undefined instruction handlers */
    690 #ifdef VERBOSE_INIT_ARM
    691 	printf("undefined ");
    692 #endif
    693 	undefined_init();
    694 
    695 	/* Load memory into UVM. */
    696 #ifdef VERBOSE_INIT_ARM
    697 	printf("page ");
    698 #endif
    699 	uvm_md_init();
    700 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    701 	    atop(physical_freestart), atop(physical_freeend),
    702 	    VM_FREELIST_DEFAULT);
    703 
    704 	/* Boot strap pmap telling it where managed kernel virtual memory is */
    705 #ifdef VERBOSE_INIT_ARM
    706 	printf("pmap ");
    707 #endif
    708 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    709 
    710 	/* Setup the IRQ system */
    711 #ifdef VERBOSE_INIT_ARM
    712 	printf("irq ");
    713 #endif
    714 	iq80310_intr_init();
    715 
    716 #ifdef VERBOSE_INIT_ARM
    717 	printf("done.\n");
    718 #endif
    719 
    720 #ifdef DDB
    721 	db_machine_init();
    722 	if (boothowto & RB_KDB)
    723 		Debugger();
    724 #endif
    725 
    726 	/* We return the new stack pointer address */
    727 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    728 }
    729 
    730 void
    731 consinit(void)
    732 {
    733 	static const bus_addr_t comcnaddrs[] = {
    734 		IQ80310_UART2,		/* com0 (J9) */
    735 		IQ80310_UART1,		/* com1 (J10) */
    736 	};
    737 	static int consinit_called;
    738 
    739 	if (consinit_called != 0)
    740 		return;
    741 
    742 	consinit_called = 1;
    743 
    744 	/*
    745 	 * Console devices are mapped VA==PA.  Our devmap reflects
    746 	 * this, so register it now so drivers can map the console
    747 	 * device.
    748 	 */
    749 	pmap_devmap_register(iq80310_devmap);
    750 
    751 #if NCOM > 0
    752 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    753 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    754 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    755 #else
    756 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    757 #endif
    758 }
    759