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