Home | History | Annotate | Line # | Download | only in iyonix
      1 /*	$NetBSD: iyonix_machdep.c,v 1.34 2023/10/12 11:33:39 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Based on code written by Jason R. Thorpe and Steve C. Woodford for
      8  * Wasabi Systems, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed for the NetBSD Project by
     21  *	Wasabi Systems, Inc.
     22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997,1998 Mark Brinicombe.
     41  * Copyright (c) 1997,1998 Causality Limited.
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Mark Brinicombe
     55  *	for the NetBSD Project.
     56  * 4. The name of the company nor the name of the author may be used to
     57  *    endorse or promote products derived from this software without specific
     58  *    prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     61  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     62  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     63  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     64  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     66  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  * Machine dependent functions for kernel setup for Iyonix.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: iyonix_machdep.c,v 1.34 2023/10/12 11:33:39 skrll Exp $");
     77 
     78 #include "opt_ddb.h"
     79 #include "opt_kgdb.h"
     80 
     81 #include <sys/param.h>
     82 #include <sys/device.h>
     83 #include <sys/systm.h>
     84 #include <sys/kernel.h>
     85 #include <sys/exec.h>
     86 #include <sys/proc.h>
     87 #include <sys/msgbuf.h>
     88 #include <sys/reboot.h>
     89 #include <sys/termios.h>
     90 #include <sys/ksyms.h>
     91 #include <sys/bus.h>
     92 #include <sys/cpu.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #include <dev/cons.h>
     97 
     98 #include <dev/pci/ppbreg.h>
     99 #include <dev/ic/i8259reg.h>
    100 
    101 #include <net/if.h>
    102 #include <net/if_ether.h>
    103 
    104 #include <machine/db_machdep.h>
    105 #include <ddb/db_sym.h>
    106 #include <ddb/db_extern.h>
    107 
    108 #include <acorn32/include/bootconfig.h>
    109 #include <arm/locore.h>
    110 #include <arm/undefined.h>
    111 
    112 #include <arm/arm32/machdep.h>
    113 
    114 #include <arm/xscale/i80321reg.h>
    115 #include <arm/xscale/i80321var.h>
    116 
    117 #include <iyonix/iyonix/iyonixreg.h>
    118 #include <iyonix/iyonix/obiovar.h>
    119 
    120 #include "ksyms.h"
    121 
    122 #define	KERNEL_TEXT_BASE	KERNEL_BASE
    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 struct bootconfig bootconfig;		/* Boot config storage */
    132 
    133 char *boot_args;
    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 vaddr_t pagetables_start;
    141 
    142 /*int debug_flags;*/
    143 #ifndef PMAP_STATIC_L1S
    144 int max_processes = 64;			/* Default number */
    145 #endif	/* !PMAP_STATIC_L1S */
    146 
    147 /* Physical and virtual addresses for some global pages */
    148 pv_addr_t minidataclean;
    149 
    150 paddr_t msgbufphys;
    151 
    152 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    153 
    154 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    155 #define	KERNEL_PT_KERNEL_NUM	4
    156 
    157 					/* L2 table for mapping i80321 */
    158 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    159 
    160 					/* L2 tables for mapping kernel VM */
    161 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
    162 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    163 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    164 
    165 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    166 
    167 char iyonix_macaddr[ETHER_ADDR_LEN];
    168 
    169 char boot_consdev[16];
    170 
    171 /* Prototypes */
    172 
    173 void	iyonix_pic_init(void);
    174 void	iyonix_read_machineid(void);
    175 
    176 void	consinit(void);
    177 
    178 static void consinit_com(const char *consdev);
    179 static void consinit_genfb(const char *consdev);
    180 static void process_kernel_args(void);
    181 static void parse_iyonix_bootargs(char *args);
    182 
    183 #include "com.h"
    184 #if NCOM > 0
    185 #include <dev/ic/comreg.h>
    186 #include <dev/ic/comvar.h>
    187 #endif
    188 
    189 #include "genfb.h"
    190 
    191 #if (NGENFB == 0) && (NCOM == 0)
    192 # error "No valid console device (com or genfb)"
    193 #elif defined(COMCONSOLE) || (NGENFB == 0)
    194 # define DEFAULT_CONSDEV "com"
    195 #else
    196 # define DEFAULT_CONSDEV "genfb"
    197 #endif
    198 
    199 /*
    200  * Define the default console speed for the machine.
    201  */
    202 #ifndef CONSPEED
    203 #define CONSPEED B9600
    204 #endif /* ! CONSPEED */
    205 
    206 #ifndef CONUNIT
    207 #define	CONUNIT	0
    208 #endif
    209 
    210 #ifndef CONMODE
    211 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    212 #endif
    213 
    214 int comcnspeed = CONSPEED;
    215 int comcnmode = CONMODE;
    216 int comcnunit = CONUNIT;
    217 
    218 #if KGDB
    219 #ifndef KGDB_DEVNAME
    220 #error Must define KGDB_DEVNAME
    221 #endif
    222 const char kgdb_devname[] = KGDB_DEVNAME;
    223 
    224 #ifndef KGDB_DEVADDR
    225 #error Must define KGDB_DEVADDR
    226 #endif
    227 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    228 
    229 #ifndef KGDB_DEVRATE
    230 #define KGDB_DEVRATE	CONSPEED
    231 #endif
    232 int kgdb_devrate = KGDB_DEVRATE;
    233 
    234 #ifndef KGDB_DEVMODE
    235 #define KGDB_DEVMODE	CONMODE
    236 #endif
    237 int kgdb_devmode = KGDB_DEVMODE;
    238 #endif /* KGDB */
    239 
    240 /*
    241  * void cpu_reboot(int howto, char *bootstr)
    242  *
    243  * Reboots the system
    244  *
    245  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    246  * then reset the CPU.
    247  */
    248 void
    249 cpu_reboot(int howto, char *bootstr)
    250 {
    251 
    252 	/*
    253 	 * If we are still cold then hit the air brakes
    254 	 * and crash to earth fast
    255 	 */
    256 	if (cold) {
    257 		doshutdownhooks();
    258 		pmf_system_shutdown(boothowto);
    259 		printf("The operating system has halted.\n");
    260 		printf("Please press any key to reboot.\n\n");
    261 		cngetc();
    262 		printf("rebooting...\n");
    263 		goto reset;
    264 	}
    265 
    266 	/* Disable console buffering */
    267 
    268 	/*
    269 	 * If RB_NOSYNC was not specified sync the discs.
    270 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    271 	 * unmount.  It looks like syslogd is getting woken up only to find
    272 	 * that it cannot page part of the binary in as the filesystem has
    273 	 * been unmounted.
    274 	 */
    275 	if (!(howto & RB_NOSYNC))
    276 		bootsync();
    277 
    278 	/* Say NO to interrupts */
    279 	splhigh();
    280 
    281 	/* Do a dump if requested. */
    282 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    283 		dumpsys();
    284 
    285 	/* Run any shutdown hooks */
    286 	doshutdownhooks();
    287 
    288 	pmf_system_shutdown(boothowto);
    289 
    290 	/* Make sure IRQ's are disabled */
    291 	IRQdisable;
    292 
    293 	if (howto & RB_HALT) {
    294 		printf("The operating system has halted.\n");
    295 		printf("Please press any key to reboot.\n\n");
    296 		cngetc();
    297 	}
    298 
    299 	printf("rebooting...\n\r");
    300  reset:
    301 	/*
    302 	 * Make really really sure that all interrupts are disabled,
    303 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    304 	 */
    305 	(void) disable_interrupts(I32_bit|F32_bit);
    306 	*(volatile uint32_t *)(IYONIX_80321_VBASE + VERDE_ATU_BASE +
    307 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
    308 
    309 	/* ...and if that didn't work, just croak. */
    310 	printf("RESET FAILED!\n");
    311 	for (;;);
    312 }
    313 
    314 /* Static device mappings. */
    315 static const struct pmap_devmap iyonix_devmap[] = {
    316     /*
    317      * Map the on-board devices VA == PA so that we can access them
    318      * with the MMU on or off.
    319      */
    320     DEVMAP_ENTRY(
    321 	IYONIX_OBIO_BASE,
    322 	IYONIX_OBIO_BASE,
    323 	IYONIX_OBIO_SIZE
    324     ),
    325 
    326     DEVMAP_ENTRY(
    327 	IYONIX_IOW_VBASE,
    328 	VERDE_OUT_XLATE_IO_WIN0_BASE,
    329 	VERDE_OUT_XLATE_IO_WIN_SIZE
    330    ),
    331 
    332    DEVMAP_ENTRY(
    333 	IYONIX_80321_VBASE,
    334 	VERDE_PMMR_BASE,
    335 	VERDE_PMMR_SIZE
    336    ),
    337 
    338    DEVMAP_ENTRY(
    339 	IYONIX_FLASH_BASE,
    340 	IYONIX_FLASH_BASE,
    341 	IYONIX_FLASH_SIZE
    342    ),
    343 
    344    DEVMAP_ENTRY_END
    345 };
    346 
    347 /* Read out the Machine ID from the flash, and stash it away for later use. */
    348 
    349 void
    350 iyonix_read_machineid(void)
    351 {
    352 	volatile uint32_t *flashbase = (uint32_t *)IYONIX_FLASH_BASE;
    353 	volatile uint16_t *flashword = (uint16_t *)IYONIX_FLASH_BASE;
    354 	union {
    355 		uint32_t w[2];
    356 		uint8_t  b[8];
    357 	} machid;
    358 
    359 	/* Enter SecSi Sector Region */
    360 	flashword[0x555] = 0xAA;
    361 	flashword[0x2AA] = 0x55;
    362 	flashword[0x555] = 0x88;
    363 
    364 	machid.w[0] = flashbase[0];
    365 	machid.w[1] = flashbase[1];
    366 
    367 	iyonix_macaddr[0] = machid.b[6];
    368 	iyonix_macaddr[1] = machid.b[5];
    369 	iyonix_macaddr[2] = machid.b[4];
    370 	iyonix_macaddr[3] = machid.b[3];
    371 	iyonix_macaddr[4] = machid.b[2];
    372 	iyonix_macaddr[5] = machid.b[1];
    373 
    374 	/* Exit SecSi Sector Region */
    375 	flashword[0x555] = 0xAA;
    376 	flashword[0x2AA] = 0x55;
    377 	flashword[0x555] = 0x90;
    378 	flashword[0x555] = 0x00;
    379 }
    380 
    381 #define IYONIX_PIC_WRITE(a,v) (*((char *)IYONIX_OBIO_BASE + (a)) = (v))
    382 
    383 void
    384 iyonix_pic_init(void)
    385 {
    386 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
    387 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW2, ICW2_IRL(0));
    388 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW3, ICW3_CASCADE(2));
    389 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_ICW4, ICW4_8086);
    390 	IYONIX_PIC_WRITE(IYONIX_MASTER_PIC + PIC_OCW1, 0x0); /* Unmask */
    391 
    392 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW1, ICW1_IC4|ICW1_SELECT);
    393 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW2, ICW2_IRL(0));
    394 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW3, ICW3_CASCADE(1));
    395 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_ICW4, ICW4_8086);
    396 	IYONIX_PIC_WRITE(IYONIX_SLAVE_PIC + PIC_OCW1, 0x0); /* Unmask */
    397 
    398 }
    399 
    400 /*
    401  * vaddr_t initarm(...)
    402  *
    403  * Initial entry point on startup. This gets called before main() is
    404  * entered.
    405  * It should be responsible for setting up everything that must be
    406  * in place when main is called.
    407  * This includes
    408  *   Taking a copy of the boot configuration structure.
    409  *   Initialising the physical console so characters can be printed.
    410  *   Setting up page tables for the kernel
    411  *   Initialising interrupt controllers to a sane default state
    412  */
    413 vaddr_t
    414 initarm(void *arg)
    415 {
    416 	struct bootconfig *passed_bootconfig = arg;
    417 	extern char _end[];
    418 	int loop;
    419 	int loop1;
    420 	u_int l1pagetable;
    421 	paddr_t memstart = 0;
    422 	psize_t memsize = 0;
    423 
    424 	/* Calibrate the delay loop. */
    425 	i80321_calibrate_delay();
    426 
    427 	/* Ensure bootconfig has valid magic */
    428 	if (passed_bootconfig->magic != BOOTCONFIG_MAGIC)
    429 		printf("Bad bootconfig magic: %x\n", bootconfig.magic);
    430 
    431 	bootconfig = *passed_bootconfig;
    432 
    433 	/* Fake bootconfig structure for anything that still needs it */
    434 	/* XXX must make the memory description h/w independent */
    435 	bootconfig.dram[0].address = memstart;
    436 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    437 	bootconfig.dramblocks = 1;
    438 
    439 	/* process arguments - can update boothowto */
    440 	process_kernel_args();
    441 
    442 	/*
    443 	 * Since we map the on-board devices VA==PA, and the kernel
    444 	 * is running VA==PA, it's possible for us to initialize
    445 	 * the console now.
    446 	 */
    447 	consinit();
    448 
    449 #ifdef VERBOSE_INIT_ARM
    450 	/* Talk to the user */
    451 	printf("\nNetBSD/iyonix booting ...\n");
    452 #endif
    453 
    454 	/*
    455 	 * Heads up ... Setup the CPU / MMU / TLB functions
    456 	 */
    457 	if (set_cpufuncs())
    458 		panic("cpu not recognized!");
    459 
    460 	/*
    461 	 * We are currently running with the MMU enabled and the
    462 	 * entire address space mapped VA==PA.
    463 	 */
    464 
    465 	/*
    466 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
    467 	 * registers.
    468 	 */
    469 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    470 	    &memstart, &memsize);
    471 
    472 #ifdef VERBOSE_INIT_ARM
    473 	printf("initarm: Configuring system ...\n");
    474 #endif
    475 
    476 	/*
    477 	 * Set up the variables that define the availability of
    478 	 * physical memory.
    479 	 */
    480 	physical_start = memstart;
    481 	physical_end = physical_start + memsize;
    482 
    483 	physical_freestart = physical_start +
    484 	    (((uintptr_t) _end - KERNEL_TEXT_BASE + PGOFSET) & ~PGOFSET);
    485 	physical_freeend = physical_end;
    486 
    487 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    488 
    489 #ifdef VERBOSE_INIT_ARM
    490 	/* Tell the user about the memory */
    491 	printf("physmemory: %ld pages at 0x%08lx -> 0x%08lx\n", physmem,
    492 	    physical_start, physical_end - 1);
    493 #endif
    494 
    495 	/*
    496 	 * The kernel is loaded at the base of physical memory. We allocate
    497 	 * pages upwards from the top of the kernel.
    498 	 *
    499 	 * We need to allocate some fixed page tables to get the kernel
    500 	 * going.  We allocate one page directory and a number of page
    501 	 * tables and store the physical addresses in the kernel_pt_table
    502 	 * array.
    503 	 *
    504 	 * The kernel page directory must be on a 16K boundary.  The page
    505 	 * tables must be on 4K boundaries.  What we do is allocate the
    506 	 * page directory on the first 16K boundary that we encounter, and
    507 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    508 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    509 	 * least one 16K aligned region.
    510 	 */
    511 
    512 #ifdef VERBOSE_INIT_ARM
    513 	printf("Allocating page tables\n");
    514 #endif
    515 
    516 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    517 
    518 #ifdef VERBOSE_INIT_ARM
    519 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    520 	       physical_freestart, free_pages, free_pages);
    521 #endif
    522 
    523 	/* Define a macro to simplify memory allocation */
    524 #define	valloc_pages(var, np)				\
    525 	alloc_pages((var).pv_pa, (np));			\
    526 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    527 
    528 #define alloc_pages(var, np)				\
    529 	(var) = physical_freestart;			\
    530 	physical_freestart += ((np) * PAGE_SIZE);	\
    531 	if (physical_freeend < physical_freestart)	\
    532 		panic("initarm: out of memory");	\
    533 	free_pages -= (np);				\
    534 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    535 
    536 	loop1 = 0;
    537 	kernel_l1pt.pv_pa = kernel_l1pt.pv_va = 0;
    538 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    539 		/* Are we 16KB aligned for an L1 ? */
    540 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
    541 		    && kernel_l1pt.pv_pa == 0) {
    542 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    543 		} else {
    544 			valloc_pages(kernel_pt_table[loop1],
    545 			    L2_TABLE_SIZE / PAGE_SIZE);
    546 			++loop1;
    547 		}
    548 	}
    549 
    550 	/* This should never be able to happen but better confirm that. */
    551 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    552 		panic("initarm: Failed to align the kernel page directory");
    553 
    554 	/*
    555 	 * Allocate a page for the system page mapped to V0x00000000
    556 	 * This page will just contain the system vectors and can be
    557 	 * shared by all processes.
    558 	 */
    559 	alloc_pages(systempage.pv_pa, 1);
    560 
    561 	/* Allocate stacks for all modes */
    562 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    563 	valloc_pages(abtstack, ABT_STACK_SIZE);
    564 	valloc_pages(undstack, UND_STACK_SIZE);
    565 	valloc_pages(kernelstack, UPAGES);
    566 
    567 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    568 #ifdef DIAGNOSTIC
    569 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    570 #endif
    571 	valloc_pages(minidataclean, 1);
    572 
    573 #ifdef VERBOSE_INIT_ARM
    574 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    575 	    irqstack.pv_va);
    576 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    577 	    abtstack.pv_va);
    578 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    579 	    undstack.pv_va);
    580 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    581 	    kernelstack.pv_va);
    582 #endif
    583 
    584 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    585 
    586 	/*
    587 	 * Ok we have allocated physical pages for the primary kernel
    588 	 * page tables
    589 	 */
    590 
    591 #ifdef VERBOSE_INIT_ARM
    592 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    593 #endif
    594 
    595 	/*
    596 	 * Now we start construction of the L1 page table
    597 	 * We start by mapping the L2 page tables into the L1.
    598 	 * This means that we can replace L1 mappings later on if necessary
    599 	 */
    600 	l1pagetable = kernel_l1pt.pv_pa;
    601 
    602 	/* Map the L2 pages tables in the L1 page table */
    603 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    604 	    &kernel_pt_table[KERNEL_PT_SYS]);
    605 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    606 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    607 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    608 	pmap_link_l2pt(l1pagetable, IYONIX_IOPXS_VBASE,
    609 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    610 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    611 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    612 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    613 
    614 	/* update the top of the kernel VM */
    615 	pmap_curmaxkvaddr =
    616 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    617 
    618 #ifdef VERBOSE_INIT_ARM
    619 	printf("Mapping kernel\n");
    620 #endif
    621 
    622 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    623 	{
    624 		extern char etext[], _end[];
    625 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    626 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    627 		u_int logical;
    628 
    629 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    630 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    631 
    632 		logical = 0;	/* offset of kernel in RAM */
    633 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    634 		    physical_start + logical, textsize,
    635 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    636 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    637 		    physical_start + logical, totalsize - textsize,
    638 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    639 	}
    640 
    641 #ifdef VERBOSE_INIT_ARM
    642 	printf("Constructing L2 page tables\n");
    643 #endif
    644 
    645 	/* Map the stack pages */
    646 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    647 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    648 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    649 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    650 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    651 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    652 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    653 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    654 
    655 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    656 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    657 
    658 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    659 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    660 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    661 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    662 	}
    663 
    664 	/* Map the Mini-Data cache clean area. */
    665 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    666 	    minidataclean.pv_pa);
    667 
    668 	/* Map the vector page. */
    669 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    670 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    671 
    672 	/* Map the statically mapped devices. */
    673 	pmap_devmap_bootstrap(l1pagetable, iyonix_devmap);
    674 
    675 	/*
    676 	 * Give the XScale global cache clean code an appropriately
    677 	 * sized chunk of unmapped VA space starting at 0xff000000
    678 	 * (our device mappings end before this address).
    679 	 */
    680 	xscale_cache_clean_addr = 0xff000000U;
    681 
    682 	/*
    683 	 * Now we have the real page tables in place so we can switch to them.
    684 	 * Once this is done we will be running with the REAL kernel page
    685 	 * tables.
    686 	 */
    687 
    688 	/* Switch tables */
    689 #ifdef VERBOSE_INIT_ARM
    690 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    691 	       physical_freestart, free_pages, free_pages);
    692 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    693 #endif
    694 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    695 	cpu_setttb(kernel_l1pt.pv_pa, true);
    696 	cpu_tlb_flushID();
    697 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    698 
    699 	iyonix_read_machineid();
    700 
    701 	/*
    702 	 * Moved from cpu_startup() as data_abort_handler() references
    703 	 * this during uvm init
    704 	 */
    705 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    706 
    707 #ifdef VERBOSE_INIT_ARM
    708 	printf("done!\n");
    709 #endif
    710 
    711 #ifdef VERBOSE_INIT_ARM
    712 	printf("bootstrap done.\n");
    713 #endif
    714 
    715 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    716 
    717 	/*
    718 	 * Pages were allocated during the secondary bootstrap for the
    719 	 * stacks for different CPU modes.
    720 	 * We must now set the r13 registers in the different CPU modes to
    721 	 * point to these stacks.
    722 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    723 	 * of the stack memory.
    724 	 */
    725 #ifdef VERBOSE_INIT_ARM
    726 	printf("init subsystems: stacks ");
    727 #endif
    728 
    729 	set_stackptr(PSR_IRQ32_MODE,
    730 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    731 	set_stackptr(PSR_ABT32_MODE,
    732 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    733 	set_stackptr(PSR_UND32_MODE,
    734 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    735 
    736 	/*
    737 	 * Well we should set a data abort handler.
    738 	 * Once things get going this will change as we will need a proper
    739 	 * handler.
    740 	 * Until then we will use a handler that just panics but tells us
    741 	 * why.
    742 	 * Initialisation of the vectors will just panic on a data abort.
    743 	 * This just fills in a slightly better one.
    744 	 */
    745 #ifdef VERBOSE_INIT_ARM
    746 	printf("vectors ");
    747 #endif
    748 	data_abort_handler_address = (u_int)data_abort_handler;
    749 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    750 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    751 
    752 	/* Initialise the undefined instruction handlers */
    753 #ifdef VERBOSE_INIT_ARM
    754 	printf("undefined ");
    755 #endif
    756 	undefined_init();
    757 
    758 	/* Load memory into UVM. */
    759 #ifdef VERBOSE_INIT_ARM
    760 	printf("page ");
    761 #endif
    762 	uvm_md_init();
    763 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    764 	    atop(physical_freestart), atop(physical_freeend),
    765 	    VM_FREELIST_DEFAULT);
    766 
    767 	/* Boot strap pmap telling it where managed kernel virtual memory is */
    768 #ifdef VERBOSE_INIT_ARM
    769 	printf("pmap ");
    770 #endif
    771 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    772 
    773 	/* Setup the IRQ system */
    774 #ifdef VERBOSE_INIT_ARM
    775 	printf("irq ");
    776 #endif
    777 	i80321_intr_init();
    778 
    779 #ifdef VERBOSE_INIT_ARM
    780 	printf("done.\n");
    781 #endif
    782 
    783 #ifdef DDB
    784 	db_machine_init();
    785 	if (boothowto & RB_KDB)
    786 		Debugger();
    787 #endif
    788 
    789 	iyonix_pic_init();
    790 
    791 	printf("args: %s\n", bootconfig.args);
    792 	printf("howto: %x\n", boothowto);
    793 
    794 	/* We return the new stack pointer address */
    795 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
    796 }
    797 
    798 void
    799 consinit(void)
    800 {
    801 	static int consinit_called;
    802 
    803 	if (consinit_called != 0)
    804 		return;
    805 
    806 	consinit_called = 1;
    807 
    808 	/* We let consinit_<foo> worry about device numbers */
    809 	if (strncmp(boot_consdev, "genfb", 5) &&
    810 	    strncmp(boot_consdev, "com", 3))
    811 	        strcpy(boot_consdev, DEFAULT_CONSDEV);
    812 
    813 	if (!strncmp(boot_consdev, "com", 3))
    814 		consinit_com(boot_consdev);
    815 	else
    816 		consinit_genfb(boot_consdev);
    817 }
    818 
    819 static void
    820 consinit_com(const char *consdev)
    821 {
    822 	static const bus_addr_t comcnaddrs[] = {
    823 		IYONIX_UART1,		/* com0 */
    824 	};
    825 	/*
    826 	 * Console devices are mapped VA==PA.  Our devmap reflects
    827 	 * this, so register it now so drivers can map the console
    828 	 * device.
    829 	 */
    830 	pmap_devmap_register(iyonix_devmap);
    831 
    832 	/* When we support more than the first serial port as console,
    833 	 * we should check consdev for a number.
    834 	 */
    835 #if NCOM > 0
    836 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    837 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    838 	{
    839 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    840 	}
    841 #else
    842 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    843 #endif
    844 
    845 #if KGDB
    846 #if NCOM > 0
    847 	if (strcmp(kgdb_devname, "com") == 0) {
    848 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
    849 		    COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
    850 	}
    851 #endif	/* NCOM > 0 */
    852 #endif	/* KGDB */
    853 }
    854 
    855 static void
    856 consinit_genfb(const char *consdev)
    857 {
    858 	/* NOTYET */
    859 }
    860 
    861 static void
    862 process_kernel_args(void)
    863 {
    864 	char *args;
    865 
    866 	/* Ok now we will check the arguments for interesting parameters. */
    867 	args = bootconfig.args;
    868 
    869 #ifdef BOOTHOWTO
    870 	boothowto = BOOTHOWTO;
    871 #else
    872 	boothowto = 0;
    873 #endif
    874 
    875 	/* Only arguments itself are passed from the bootloader */
    876 	while (*args == ' ')
    877 		++args;
    878 
    879 	boot_args = args;
    880 	parse_mi_bootargs(boot_args);
    881 	parse_iyonix_bootargs(boot_args);
    882 }
    883 
    884 static void
    885 parse_iyonix_bootargs(char *args)
    886 {
    887 	char *ptr;
    888 
    889 	if (get_bootconf_option(args, "consdev", BOOTOPT_TYPE_STRING, &ptr))
    890 	{
    891 		/* ptr may have trailing clutter */
    892 		strlcpy(boot_consdev, ptr, sizeof(boot_consdev));
    893 		if ( (ptr = strchr(boot_consdev, ' ')) )
    894 			*ptr = 0;
    895 	}
    896 }
    897