Home | History | Annotate | Line # | Download | only in eb7500atx
eb7500atx_machdep.c revision 1.13.4.1
      1 /*	$NetBSD: eb7500atx_machdep.c,v 1.13.4.1 2009/05/13 17:16:03 jym Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000-2002 Reinoud Zandijk.
      5  * Copyright (c) 1994-1998 Mark Brinicombe.
      6  * Copyright (c) 1994 Brini.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software written for Brini by Mark Brinicombe
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by Brini.
     22  * 4. The name of the company nor the name of the author may be used to
     23  *    endorse or promote products derived from this software without specific
     24  *    prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * RiscBSD kernel project
     39  *
     40  * machdep.c
     41  *
     42  * Machine dependant functions for kernel setup
     43  *
     44  * This file still needs a lot of work
     45  *
     46  * Created      : 17/09/94
     47  * Updated for yet another new bootloader 28/12/02
     48  */
     49 
     50 #include "opt_ddb.h"
     51 #include "opt_modular.h"
     52 #include "opt_pmap_debug.h"
     53 #include "vidcvideo.h"
     54 #include "pckbc.h"
     55 
     56 #include <sys/param.h>
     57 
     58 __KERNEL_RCSID(0, "$NetBSD: eb7500atx_machdep.c,v 1.13.4.1 2009/05/13 17:16:03 jym Exp $");
     59 
     60 #include <sys/systm.h>
     61 #include <sys/kernel.h>
     62 #include <sys/reboot.h>
     63 #include <sys/proc.h>
     64 #include <sys/msgbuf.h>
     65 #include <sys/exec.h>
     66 #include <sys/ksyms.h>
     67 
     68 #include <dev/cons.h>
     69 
     70 #include <machine/db_machdep.h>
     71 #include <ddb/db_sym.h>
     72 #include <ddb/db_extern.h>
     73 
     74 #include <uvm/uvm.h>
     75 
     76 #include <machine/signal.h>
     77 #include <machine/frame.h>
     78 #include <machine/bootconfig.h>
     79 #include <machine/cpu.h>
     80 #include <machine/io.h>
     81 #include <machine/intr.h>
     82 #include <arm/cpuconf.h>
     83 #include <arm/arm32/katelib.h>
     84 #include <arm/arm32/machdep.h>
     85 #include <arm/undefined.h>
     86 #include <machine/rtc.h>
     87 #include <machine/bus.h>
     88 
     89 #include <arm/iomd/vidc.h>
     90 #include <arm/iomd/iomdreg.h>
     91 #include <arm/iomd/iomdvar.h>
     92 
     93 #include <arm/iomd/vidcvideo.h>
     94 
     95 #include <sys/device.h>
     96 #include <dev/ic/pckbcvar.h>
     97 
     98 #include <dev/i2c/i2cvar.h>
     99 #include <dev/i2c/pcf8583var.h>
    100 #include <arm/iomd/iomdiicvar.h>
    101 
    102 /* static i2c_tag_t acorn32_i2c_tag;*/
    103 
    104 #include "ksyms.h"
    105 
    106 /* Kernel text starts at the base of the kernel address space. */
    107 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00000000)
    108 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    109 
    110 /*
    111  * The range 0xf1000000 - 0xf5ffffff is available for kernel VM space
    112  * Fixed mappings exist from 0xf6000000 - 0xffffffff
    113  */
    114 #define	KERNEL_VM_SIZE		0x05000000
    115 
    116 /*
    117  * Address to call from cpu_reset() to reset the machine.
    118  * This is machine architecture dependant as it varies depending
    119  * on where the ROM appears when you turn the MMU off.
    120  */
    121 u_int cpu_reset_address = 0x0; /* XXX 0x3800000 too for rev0 RiscPC 600 */
    122 
    123 
    124 #define VERBOSE_INIT_ARM
    125 
    126 
    127 /* Define various stack sizes in pages */
    128 #define IRQ_STACK_SIZE	1
    129 #define ABT_STACK_SIZE	1
    130 #define UND_STACK_SIZE	1
    131 
    132 
    133 struct bootconfig bootconfig;	/* Boot config storage */
    134 videomemory_t videomemory;	/* Video memory descriptor */
    135 
    136 char *boot_args = NULL;		/* holds the pre-processed boot arguments */
    137 extern char *booted_kernel;	/* used for ioctl to retrieve booted kernel */
    138 
    139 extern int       *vidc_base;
    140 extern u_int32_t  iomd_base;
    141 extern struct bus_space iomd_bs_tag;
    142 
    143 paddr_t physical_start;
    144 paddr_t physical_freestart;
    145 paddr_t physical_freeend;
    146 paddr_t physical_end;
    147 paddr_t dma_range_begin;
    148 paddr_t dma_range_end;
    149 
    150 u_int free_pages;
    151 int physmem = 0;
    152 paddr_t memoryblock_end;
    153 
    154 #ifndef PMAP_STATIC_L1S
    155 int max_processes = 64;		/* Default number */
    156 #endif	/* !PMAP_STATIC_L1S */
    157 
    158 u_int videodram_size = 0;	/* Amount of DRAM to reserve for video */
    159 
    160 /* Physical and virtual addresses for some global pages */
    161 pv_addr_t systempage;
    162 pv_addr_t irqstack;
    163 pv_addr_t undstack;
    164 pv_addr_t abtstack;
    165 pv_addr_t kernelstack;
    166 
    167 paddr_t msgbufphys;
    168 
    169 extern u_int data_abort_handler_address;
    170 extern u_int prefetch_abort_handler_address;
    171 extern u_int undefined_handler_address;
    172 
    173 #ifdef PMAP_DEBUG
    174 extern int pmap_debug_level;
    175 #endif	/* PMAP_DEBUG */
    176 
    177 #define	KERNEL_PT_VMEM		0 /* Page table for mapping video memory */
    178 #define	KERNEL_PT_SYS		1 /* Page table for mapping proc0 zero page */
    179 #define	KERNEL_PT_KERNEL	2 /* Page table for mapping kernel */
    180 #define	KERNEL_PT_VMDATA	3 /* Page tables for mapping kernel VM */
    181 #define	KERNEL_PT_VMDATA_NUM	4 /* start with 16MB of KVM */
    182 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    183 
    184 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    185 
    186 struct user *proc0paddr;
    187 
    188 #ifdef CPU_SA110
    189 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
    190 static vaddr_t sa110_cc_base;
    191 #endif	/* CPU_SA110 */
    192 
    193 /* Prototypes */
    194 void physcon_display_base(u_int);
    195 extern void consinit(void);
    196 
    197 void data_abort_handler(trapframe_t *);
    198 void prefetch_abort_handler(trapframe_t *);
    199 void undefinedinstruction_bounce(trapframe_t *frame);
    200 
    201 static void canonicalise_bootconfig(struct bootconfig *, struct bootconfig *);
    202 static void process_kernel_args(void);
    203 
    204 extern void dump_spl_masks(void);
    205 
    206 void rpc_sa110_cc_setup(void);
    207 
    208 void parse_rpc_bootargs(char *args);
    209 
    210 extern void dumpsys(void);
    211 
    212 
    213 #	define console_flush()		/* empty */
    214 
    215 
    216 #define panic2(a) do {							\
    217 	memset((void *) (videomemory.vidm_vbase), 0x55, 50*1024);	\
    218 	consinit();							\
    219 	panic a;							\
    220 } while (/* CONSTCOND */ 0)
    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 
    231 /* NOTE: These variables will be removed, well some of them */
    232 
    233 extern u_int current_mask;
    234 
    235 void
    236 cpu_reboot(int howto, char *bootstr)
    237 {
    238 
    239 #ifdef DIAGNOSTIC
    240 	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
    241 
    242 	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
    243 	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
    244 	    irqmasks[IPL_VM]);
    245 	printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
    246 	    irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
    247 
    248 	/* dump_spl_masks(); */
    249 #endif	/* DIAGNOSTIC */
    250 
    251 	/*
    252 	 * If we are still cold then hit the air brakes
    253 	 * and crash to earth fast
    254 	 */
    255 	if (cold) {
    256 		doshutdownhooks();
    257 		pmf_system_shutdown(boothowto);
    258 		printf("Halted while still in the ICE age.\n");
    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 		cpu_reset();
    264 		/*NOTREACHED*/
    265 	}
    266 
    267 	/* Disable console buffering */
    268 	cnpollc(1);
    269 
    270 	/*
    271 	 * If RB_NOSYNC was not specified sync the discs.
    272 	 * Note: Unless cold is set to 1 here, syslogd will die during
    273 	 * the unmount.  It looks like syslogd is getting woken up
    274 	 * only to find that it cannot page part of the binary in as
    275 	 * the filesystem has been unmounted.
    276 	 */
    277 	if (!(howto & RB_NOSYNC))
    278 		bootsync();
    279 
    280 	/* Say NO to interrupts */
    281 	splhigh();
    282 
    283 	/* Do a dump if requested. */
    284 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    285 		dumpsys();
    286 
    287 	/*
    288 	 * Auto reboot overload protection
    289 	 *
    290 	 * This code stops the kernel entering an endless loop of reboot
    291 	 * - panic cycles. This will have the effect of stopping further
    292 	 * reboots after it has rebooted 8 times after panics. A clean
    293 	 * halt or reboot will reset the counter.
    294 	 */
    295 
    296 	/* Run any shutdown hooks */
    297 	doshutdownhooks();
    298 
    299 	pmf_system_shutdown(boothowto);
    300 
    301 	/* Make sure IRQ's are disabled */
    302 	IRQdisable;
    303 
    304 	if (howto & RB_HALT) {
    305 		printf("The operating system has halted.\n");
    306 		printf("Please press any key to reboot.\n\n");
    307 		cngetc();
    308 	}
    309 
    310 	printf("rebooting...\n");
    311 	cpu_reset();
    312 	/*NOTREACHED*/
    313 }
    314 
    315 
    316 /*
    317  * u_int initarm(BootConfig *bootconf)
    318  *
    319  * Initial entry point on startup. This gets called before main() is
    320  * entered.
    321  * It should be responsible for setting up everything that must be
    322  * in place when main is called.
    323  * This includes
    324  *   Taking a copy of the boot configuration structure.
    325  *   Initialising the physical console so characters can be printed.
    326  *   Setting up page tables for the kernel
    327  *   Relocating the kernel to the bottom of physical memory
    328  */
    329 
    330 /*
    331  * this part is completely rewritten for the new bootloader ... It features
    332  * a flat memory map with a mapping comparable to the EBSA arm32 machine
    333  * to boost the portability and likeness of the code
    334  */
    335 
    336 /*
    337  * Mapping table for core kernel memory. This memory is mapped at init
    338  * time with section mappings.
    339  *
    340  * XXX One big assumption in the current architecture seems that the kernel is
    341  * XXX supposed to be mapped into bootconfig.dram[0].
    342  */
    343 
    344 #define ONE_MB	0x100000
    345 
    346 struct l1_sec_map {
    347 	vaddr_t		va;
    348 	paddr_t		pa;
    349 	vsize_t		size;
    350 	vm_prot_t	prot;
    351 	int		cache;
    352 } l1_sec_table[] = {
    353 	/* Map 1Mb section for VIDC20 */
    354 	{ VIDC_BASE,		VIDC_HW_BASE,
    355 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
    356 	    PTE_NOCACHE },
    357 
    358 	/* Map 1Mb section from IOMD */
    359 	{ IOMD_BASE,		IOMD_HW_BASE,
    360 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
    361 	    PTE_NOCACHE },
    362 
    363 	/* Map 1Mb of COMBO (and module space) */
    364 	{ IO_BASE,		IO_HW_BASE,
    365 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
    366 	    PTE_NOCACHE },
    367 	{ 0, 0, 0, 0, 0 }
    368 };
    369 
    370 
    371 static void
    372 canonicalise_bootconfig(struct bootconfig *bootconf, struct bootconfig *raw_bootconf)
    373 {
    374 	/* check for bootconfig v2+ structure */
    375 	if (raw_bootconf->magic == BOOTCONFIG_MAGIC) {
    376 		/* v2+ cleaned up structure found */
    377 		*bootconf = *raw_bootconf;
    378 		return;
    379 	} else {
    380 		panic2(("Internal error: no valid bootconfig block found"));
    381 	}
    382 }
    383 
    384 
    385 u_int
    386 initarm(void *cookie)
    387 {
    388 	struct bootconfig *raw_bootconf = cookie;
    389 	int loop;
    390 	int loop1;
    391 	u_int logical;
    392 	u_int kerneldatasize;
    393 	u_int l1pagetable;
    394 	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
    395 
    396 	/*
    397 	 * Heads up ... Setup the CPU / MMU / TLB functions
    398 	 */
    399 	set_cpufuncs();
    400 
    401 	/* canonicalise the boot configuration structure to alow versioning */
    402 	canonicalise_bootconfig(&bootconfig, raw_bootconf);
    403 	booted_kernel = bootconfig.kernelname;
    404 
    405 	/* if the wscons interface is used, switch off VERBOSE booting :( */
    406 #if NVIDCVIDEO>0
    407 #	undef VERBOSE_INIT_ARM
    408 #	undef PMAP_DEBUG
    409 #endif
    410 
    411 	/*
    412 	 * Initialise the video memory descriptor
    413 	 *
    414 	 * Note: all references to the video memory virtual/physical address
    415 	 * should go via this structure.
    416 	 */
    417 
    418 	/* Hardwire it on the place the bootloader tells us */
    419 	videomemory.vidm_vbase = bootconfig.display_start;
    420 	videomemory.vidm_pbase = bootconfig.display_phys;
    421 	videomemory.vidm_size = bootconfig.display_size;
    422 	if (bootconfig.vram[0].pages)
    423 		videomemory.vidm_type = VIDEOMEM_TYPE_VRAM;
    424 	else
    425 		videomemory.vidm_type = VIDEOMEM_TYPE_DRAM;
    426 	vidc_base = (int *) VIDC_HW_BASE;
    427 	iomd_base =         IOMD_HW_BASE;
    428 
    429 	/*
    430 	 * Initialise the physical console
    431 	 * This is done in main() but for the moment we do it here so that
    432 	 * we can use printf in initarm() before main() has been called.
    433 	 * only for `vidcconsole!' ... not wscons
    434 	 */
    435 #if NVIDCVIDEO == 0
    436 	consinit();
    437 #endif
    438 
    439 	/*
    440 	 * Initialise the diagnostic serial console
    441 	 * This allows a means of generating output during initarm().
    442 	 * Once all the memory map changes are complete we can call consinit()
    443 	 * and not have to worry about things moving.
    444 	 */
    445 	/* fcomcnattach(DC21285_ARMCSR_BASE, comcnspeed, comcnmode); */
    446 	/* XXX snif .... i am still not able to this */
    447 
    448 	/*
    449 	 * We have the following memory map (derived from EBSA)
    450 	 *
    451 	 * virtual address == physical address apart from the areas:
    452 	 * 0x00000000 -> 0x000fffff which is mapped to
    453 	 * top 1MB of physical memory
    454 	 * 0xf0000000 -> 0xf0ffffff wich is mapped to
    455 	 * physical address 0x01000000 -> 0x01ffffff (DRAM0a, dram[0])
    456 	 *
    457 	 * This means that the kernel is mapped suitably for continuing
    458 	 * execution, all I/O is mapped 1:1 virtual to physical and
    459 	 * physical memory is accessible.
    460 	 *
    461 	 * The initarm() has the responsibility for creating the kernel
    462 	 * page tables.
    463 	 * It must also set up various memory pointers that are used
    464 	 * by pmap etc.
    465 	 */
    466 
    467 	/* START OF REAL NEW STUFF */
    468 
    469 	/* Check to make sure the page size is correct */
    470 	if (PAGE_SIZE != bootconfig.pagesize)
    471 		panic2(("Page size is %d bytes instead of %d !! (huh?)\n",
    472 			   bootconfig.pagesize, PAGE_SIZE));
    473 
    474 	/* process arguments */
    475 	process_kernel_args();
    476 
    477 
    478 	/*
    479 	 * Now set up the page tables for the kernel ... this part is copied
    480 	 * in a (modified?) way from the EBSA machine port....
    481 	 */
    482 
    483 #ifdef VERBOSE_INIT_ARM
    484 	printf("Allocating page tables\n");
    485 #endif
    486 	/*
    487 	 * Set up the variables that define the availablilty of physical
    488 	 * memory
    489 	 */
    490 	physical_start = 0xffffffff;
    491 	physical_end = 0;
    492 	for (loop = 0, physmem = 0; loop < bootconfig.dramblocks; ++loop) {
    493 	    	if (bootconfig.dram[loop].address < physical_start)
    494 			physical_start = bootconfig.dram[loop].address;
    495 		memoryblock_end = bootconfig.dram[loop].address +
    496 		    bootconfig.dram[loop].pages * PAGE_SIZE;
    497 		if (memoryblock_end > physical_end)
    498 			physical_end = memoryblock_end;
    499 		physmem += bootconfig.dram[loop].pages;
    500 	};
    501 	/* constants for now, but might be changed/configured */
    502 	dma_range_begin = (paddr_t) physical_start;
    503 	dma_range_end   = (paddr_t) MIN(physical_end, 512*1024*1024);
    504 	/* XXX HACK HACK XXX */
    505 	/* dma_range_end   = 0x18000000; */
    506 
    507 	if (physical_start !=  bootconfig.dram[0].address) {
    508 		int oldblocks = 0;
    509 
    510 		/*
    511 		 * must be a kinetic, as it's the only thing to shuffle memory
    512 		 * around
    513 		 */
    514 		/* hack hack - throw away the slow dram */
    515 		for (loop = 0; loop < bootconfig.dramblocks; ++loop) {
    516 			if (bootconfig.dram[loop].address <
    517 			    bootconfig.dram[0].address)	{
    518 				/* non kinetic ram */
    519 				bootconfig.dram[loop].address = 0;
    520 				physmem -= bootconfig.dram[loop].pages;
    521 				bootconfig.drampages -=
    522 				    bootconfig.dram[loop].pages;
    523 				bootconfig.dram[loop].pages = 0;
    524 				oldblocks++;
    525 			}
    526 		}
    527 		physical_start = bootconfig.dram[0].address;
    528 		bootconfig.dramblocks -= oldblocks;
    529 	}
    530 
    531 	physical_freestart = physical_start;
    532 	free_pages = bootconfig.drampages;
    533 	physical_freeend = physical_end;
    534 
    535 
    536 	/*
    537 	 * AHUM !! set this variable ... it was set up in the old 1st
    538 	 * stage bootloader
    539 	 */
    540 	kerneldatasize = bootconfig.kernsize + bootconfig.MDFsize;
    541 
    542 	/* Update the address of the first free page of physical memory */
    543 	/* XXX Assumption that the kernel and stuff is at the LOWEST physical memory address? XXX */
    544 	physical_freestart +=
    545 	    bootconfig.kernsize + bootconfig.MDFsize + bootconfig.scratchsize;
    546 	free_pages -= (physical_freestart - physical_start) / PAGE_SIZE;
    547 
    548 	/* Define a macro to simplify memory allocation */
    549 #define	valloc_pages(var, np)						\
    550 	alloc_pages((var).pv_pa, (np));					\
    551 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    552 
    553 #define alloc_pages(var, np)						\
    554 	(var) = physical_freestart;					\
    555 	physical_freestart += ((np) * PAGE_SIZE);			\
    556 	free_pages -= (np);						\
    557 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    558 
    559 	loop1 = 0;
    560 	kernel_l1pt.pv_pa = 0;
    561 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    562 		/* Are we 16KB aligned for an L1 ? */
    563 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
    564 		    && kernel_l1pt.pv_pa == 0) {
    565 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    566 		} else {
    567 			valloc_pages(kernel_pt_table[loop1],
    568 					L2_TABLE_SIZE / PAGE_SIZE);
    569 			++loop1;
    570 		}
    571 	}
    572 
    573 
    574 #ifdef DIAGNOSTIC
    575 	/* This should never be able to happen but better confirm that. */
    576 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    577 		panic2(("initarm: Failed to align the kernel page "
    578 		    "directory\n"));
    579 #endif
    580 
    581 	/*
    582 	 * Allocate a page for the system page mapped to V0x00000000
    583 	 * This page will just contain the system vectors and can be
    584 	 * shared by all processes.
    585 	 */
    586 	alloc_pages(systempage.pv_pa, 1);
    587 
    588 	/* Allocate stacks for all modes */
    589 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    590 	valloc_pages(abtstack, ABT_STACK_SIZE);
    591 	valloc_pages(undstack, UND_STACK_SIZE);
    592 	valloc_pages(kernelstack, UPAGES);
    593 
    594 #ifdef VERBOSE_INIT_ARM
    595 	printf("Setting up stacks :\n");
    596 	printf("IRQ stack: p0x%08lx v0x%08lx\n",
    597 	    irqstack.pv_pa, irqstack.pv_va);
    598 	printf("ABT stack: p0x%08lx v0x%08lx\n",
    599 	    abtstack.pv_pa, abtstack.pv_va);
    600 	printf("UND stack: p0x%08lx v0x%08lx\n",
    601 	    undstack.pv_pa, undstack.pv_va);
    602 	printf("SVC stack: p0x%08lx v0x%08lx\n",
    603 	    kernelstack.pv_pa, kernelstack.pv_va);
    604 	printf("\n");
    605 #endif
    606 
    607 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    608 
    609 #ifdef CPU_SA110
    610 	/*
    611 	 * XXX totally stuffed hack to work round problems introduced
    612 	 * in recent versions of the pmap code. Due to the calls used there
    613 	 * we cannot allocate virtual memory during bootstrap.
    614 	 */
    615 	sa110_cc_base = (KERNEL_BASE + (physical_freestart - physical_start)
    616 	    + (CPU_SA110_CACHE_CLEAN_SIZE - 1))
    617 	    & ~(CPU_SA110_CACHE_CLEAN_SIZE - 1);
    618 #endif	/* CPU_SA110 */
    619 
    620 	/*
    621 	 * Ok we have allocated physical pages for the primary kernel
    622 	 * page tables
    623 	 */
    624 
    625 #ifdef VERBOSE_INIT_ARM
    626 	printf("Creating L1 page table\n");
    627 #endif
    628 
    629 	/*
    630 	 * Now we start construction of the L1 page table
    631 	 * We start by mapping the L2 page tables into the L1.
    632 	 * This means that we can replace L1 mappings later on if necessary
    633 	 */
    634 	l1pagetable = kernel_l1pt.pv_pa;
    635 
    636 	/* Map the L2 pages tables in the L1 page table */
    637 	pmap_link_l2pt(l1pagetable, 0x00000000,
    638 	    &kernel_pt_table[KERNEL_PT_SYS]);
    639 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
    640 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
    641 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
    642 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    643 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    644 	pmap_link_l2pt(l1pagetable, VMEM_VBASE,
    645 	    &kernel_pt_table[KERNEL_PT_VMEM]);
    646 
    647 	/* update the top of the kernel VM */
    648 	pmap_curmaxkvaddr =
    649 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    650 
    651 #ifdef VERBOSE_INIT_ARM
    652 	printf("Mapping kernel\n");
    653 #endif
    654 
    655 	/* Now we fill in the L2 pagetable for the kernel code/data */
    656 	/* XXX Kernel doesn't have to be on physical_start (!) use bootconfig XXX */
    657 	/*
    658 	 * The defines are a workaround for a recent problem that occurred
    659 	 * with ARM 610 processors and some ARM 710 processors
    660 	 * Other ARM 710 and StrongARM processors don't have a problem.
    661 	 */
    662 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
    663 #if defined(CPU_ARM6) || defined(CPU_ARM7)
    664 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
    665 		    physical_start, kernexec->a_text,
    666 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    667 #else	/* CPU_ARM6 || CPU_ARM7 */
    668 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
    669 		    physical_start, kernexec->a_text,
    670 		    VM_PROT_READ, PTE_CACHE);
    671 #endif	/* CPU_ARM6 || CPU_ARM7 */
    672 		logical += pmap_map_chunk(l1pagetable,
    673 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
    674 		    kerneldatasize - kernexec->a_text,
    675 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    676 	} else {	/* !ZMAGIC */
    677 		/*
    678 		 * Most likely an ELF kernel ...
    679 		 * XXX no distinction yet between read only and
    680 		 * read/write area's ...
    681 		 */
    682 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
    683 		    physical_start, kerneldatasize,
    684 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    685 	};
    686 
    687 
    688 #ifdef VERBOSE_INIT_ARM
    689 	printf("Constructing L2 page tables\n");
    690 #endif
    691 
    692 	/* Map the stack pages */
    693 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    694 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    695 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    696 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    697 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    698 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    699 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    700 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    701 
    702 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    703 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    704 
    705 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    706 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    707 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    708 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    709 	}
    710 
    711 	/* Now we fill in the L2 pagetable for the VRAM */
    712 	/*
    713 	 * Current architectures mean that the VRAM is always in 1
    714 	 * continuous bank.  This means that we can just map the 2 meg
    715 	 * that the VRAM would occupy.  In theory we don't need a page
    716 	 * table for VRAM, we could section map it but we would need
    717 	 * the page tables if DRAM was in use.
    718 	 * XXX please map two adjacent virtual areas to ONE physical
    719 	 * area
    720 	 */
    721 	pmap_map_chunk(l1pagetable, VMEM_VBASE, videomemory.vidm_pbase,
    722 	    videomemory.vidm_size, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    723 	pmap_map_chunk(l1pagetable, VMEM_VBASE + videomemory.vidm_size,
    724 	    videomemory.vidm_pbase, videomemory.vidm_size,
    725 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    726 
    727 	/* Map the vector page. */
    728 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    729 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    730 
    731 	/* Map the core memory needed before autoconfig */
    732 	loop = 0;
    733 	while (l1_sec_table[loop].size) {
    734 		vm_size_t sz;
    735 
    736 #ifdef VERBOSE_INIT_ARM
    737 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
    738 			l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
    739 			l1_sec_table[loop].va);
    740 #endif
    741 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
    742 			pmap_map_section(l1pagetable,
    743 			    l1_sec_table[loop].va + sz,
    744 			    l1_sec_table[loop].pa + sz,
    745 			    l1_sec_table[loop].prot,
    746 			    l1_sec_table[loop].cache);
    747 		++loop;
    748 	}
    749 
    750 	/*
    751 	 * Now we have the real page tables in place so we can switch
    752 	 * to them.  Once this is done we will be running with the
    753 	 * REAL kernel page tables.
    754 	 */
    755 
    756 #ifdef VERBOSE_INIT_ARM
    757 	printf("switching domains\n");
    758 #endif
    759 	/* be a client to all domains */
    760 	cpu_domains(0x55555555);
    761 
    762 	/* Switch tables */
    763 #ifdef VERBOSE_INIT_ARM
    764 	printf("switching to new L1 page table\n");
    765 #endif
    766 	setttb(kernel_l1pt.pv_pa);
    767 
    768 	/*
    769 	 * We must now clean the cache again....
    770 	 * Cleaning may be done by reading new data to displace any
    771 	 * dirty data in the cache. This will have happened in setttb()
    772 	 * but since we are boot strapping the addresses used for the read
    773 	 * may have just been remapped and thus the cache could be out
    774 	 * of sync. A re-clean after the switch will cure this.
    775 	 * After booting there are no gross reloations of the kernel thus
    776 	 * this problem will not occur after initarm().
    777 	 */
    778 	cpu_idcache_wbinv_all();
    779 	cpu_tlb_flushID();
    780 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    781 
    782 	/*
    783 	 * Moved from cpu_startup() as data_abort_handler() references
    784 	 * this during uvm init
    785 	 */
    786 	proc0paddr = (struct user *)kernelstack.pv_va;
    787 	lwp0.l_addr = proc0paddr;
    788 
    789 	/*
    790 	 * if there is support for a serial console ...we should now
    791 	 * reattach it
    792 	 */
    793 	/*      fcomcndetach();*/
    794 
    795 	/*
    796 	 * Reflect videomemory relocation in the videomemory structure
    797 	 * and reinit console
    798 	 */
    799 	if (bootconfig.vram[0].pages == 0) {
    800 		videomemory.vidm_vbase   = VMEM_VBASE;
    801 	} else {
    802 		videomemory.vidm_vbase   = VMEM_VBASE;
    803 		bootconfig.display_start = VMEM_VBASE;
    804 	};
    805 	vidc_base = (int *) VIDC_BASE;
    806 	iomd_base =         IOMD_BASE;
    807 
    808 #ifdef VERBOSE_INIT_ARM
    809 	printf("running on the new L1 page table!\n");
    810 	printf("done.\n");
    811 #endif
    812 
    813 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    814 
    815 #ifdef VERBOSE_INIT_ARM
    816 	printf("\n");
    817 #endif
    818 
    819 	/*
    820 	 * Pages were allocated during the secondary bootstrap for the
    821 	 * stacks for different CPU modes.
    822 	 * We must now set the r13 registers in the different CPU modes to
    823 	 * point to these stacks.
    824 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    825 	 * of the stack memory.
    826 	 */
    827 #ifdef VERBOSE_INIT_ARM
    828 	printf("init subsystems: stacks ");
    829 	console_flush();
    830 #endif
    831 
    832 	set_stackptr(PSR_IRQ32_MODE,
    833 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    834 	set_stackptr(PSR_ABT32_MODE,
    835 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    836 	set_stackptr(PSR_UND32_MODE,
    837 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    838 #ifdef PMAP_DEBUG
    839 	if (pmap_debug_level >= 0)
    840 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
    841 		    kernelstack.pv_pa);
    842 #endif	/* PMAP_DEBUG */
    843 
    844 	/*
    845 	 * Well we should set a data abort handler.
    846 	 * Once things get going this will change as we will need a proper
    847 	 * handler. Until then we will use a handler that just panics but
    848 	 * tells us why.
    849 	 * Initialisation of the vectors will just panic on a data abort.
    850 	 * This just fills in a slightly better one.
    851 	 */
    852 #ifdef VERBOSE_INIT_ARM
    853 	printf("vectors ");
    854 #endif
    855 	data_abort_handler_address = (u_int)data_abort_handler;
    856 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    857 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    858 	console_flush();
    859 
    860 
    861 	/*
    862 	 * At last !
    863 	 * We now have the kernel in physical memory from the bottom upwards.
    864 	 * Kernel page tables are physically above this.
    865 	 * The kernel is mapped to 0xf0000000
    866 	 * The kernel data PTs will handle the mapping of
    867 	 *   0xf1000000-0xf5ffffff (80 Mb)
    868 	 * 2Meg of VRAM is mapped to 0xf7000000
    869 	 * The page tables are mapped to 0xefc00000
    870 	 * The IOMD is mapped to 0xf6000000
    871 	 * The VIDC is mapped to 0xf6100000
    872 	 * The IOMD/VIDC could be pushed up higher but i havent got
    873 	 * sufficient documentation to do so; the addresses are not
    874 	 * parametized yet and hard to read... better fix this before;
    875 	 * its pretty unforgiving.
    876 	 */
    877 
    878 	/* Initialise the undefined instruction handlers */
    879 #ifdef VERBOSE_INIT_ARM
    880 	printf("undefined ");
    881 #endif
    882 	undefined_init();
    883 	console_flush();
    884 
    885 	/* Load memory into UVM. */
    886 #ifdef VERBOSE_INIT_ARM
    887 	printf("page ");
    888 #endif
    889 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    890 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
    891 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
    892 		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
    893 
    894 		if (start < physical_freestart)
    895 			start = physical_freestart;
    896 		if (end > physical_freeend)
    897 			end = physical_freeend;
    898 
    899 		/* XXX Consider DMA range intersection checking. */
    900 
    901 		uvm_page_physload(atop(start), atop(end),
    902 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
    903 	}
    904 
    905 	/* Boot strap pmap telling it where the kernel page table is */
    906 #ifdef VERBOSE_INIT_ARM
    907 	printf("pmap ");
    908 #endif
    909 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    910 	console_flush();
    911 
    912 	/* Setup the IRQ system */
    913 #ifdef VERBOSE_INIT_ARM
    914 	printf("irq ");
    915 #endif
    916 	console_flush();
    917 	irq_init();
    918 #ifdef VERBOSE_INIT_ARM
    919 	printf("done.\n\n");
    920 #endif
    921 
    922 #if NVIDCVIDEO>0
    923 	consinit();		/* necessary ? */
    924 #endif
    925 
    926 	/* Talk to the user */
    927 	printf("NetBSD/evbarm booting ... \n");
    928 
    929 	/* Tell the user if his boot loader is too old */
    930 	if ((bootconfig.magic < BOOTCONFIG_MAGIC) ||
    931 	    (bootconfig.version != BOOTCONFIG_VERSION)) {
    932 		printf("\nDETECTED AN OLD BOOTLOADER. PLEASE UPGRADE IT\n\n");
    933 		delay(5000000);
    934 	}
    935 
    936 	printf("Kernel loaded from file %s\n", bootconfig.kernelname);
    937 	printf("Kernel arg string (@%p) %s\n",
    938 	    bootconfig.args, bootconfig.args);
    939 	printf("\nBoot configuration structure reports the following "
    940 	    "memory\n");
    941 
    942 	printf(" DRAM block 0a at %08x size %08x "
    943 	    "DRAM block 0b at %08x size %08x\n\r",
    944 	    bootconfig.dram[0].address,
    945 	    bootconfig.dram[0].pages * bootconfig.pagesize,
    946 	    bootconfig.dram[1].address,
    947 	    bootconfig.dram[1].pages * bootconfig.pagesize);
    948 	printf(" DRAM block 1a at %08x size %08x "
    949 	    "DRAM block 1b at %08x size %08x\n\r",
    950 	    bootconfig.dram[2].address,
    951 	    bootconfig.dram[2].pages * bootconfig.pagesize,
    952 	    bootconfig.dram[3].address,
    953 	    bootconfig.dram[3].pages * bootconfig.pagesize);
    954 	printf(" VRAM block 0  at %08x size %08x\n\r",
    955 	    bootconfig.vram[0].address,
    956 	    bootconfig.vram[0].pages * bootconfig.pagesize);
    957 
    958 #if NKSYMS || defined(DDB) || defined(MODULAR)
    959 	ksyms_addsyms_elf(bootconfig.ksym_end - bootconfig.ksym_start,
    960 		(void *) bootconfig.ksym_start, (void *) bootconfig.ksym_end);
    961 #endif
    962 
    963 
    964 #ifdef DDB
    965 	db_machine_init();
    966 	if (boothowto & RB_KDB)
    967 		Debugger();
    968 #endif	/* DDB */
    969 
    970 	/* We return the new stack pointer address */
    971 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    972 }
    973 
    974 
    975 static void
    976 process_kernel_args(void)
    977 {
    978 	char *args;
    979 
    980 	/* Ok now we will check the arguments for interesting parameters. */
    981 	args = bootconfig.args;
    982 	boothowto = 0;
    983 
    984 	/* Only arguments itself are passed from the new bootloader */
    985 	while (*args == ' ')
    986 		++args;
    987 
    988 	boot_args = args;
    989 	parse_mi_bootargs(boot_args);
    990 	parse_rpc_bootargs(boot_args);
    991 }
    992 
    993 
    994 void
    995 parse_rpc_bootargs(char *args)
    996 {
    997 	int integer;
    998 
    999 	if (get_bootconf_option(args, "videodram", BOOTOPT_TYPE_INT,
   1000 	    &integer)) {
   1001 		videodram_size = integer;
   1002 		/* Round to 4K page */
   1003 		videodram_size *= 1024;
   1004 		videodram_size = round_page(videodram_size);
   1005 		if (videodram_size > 1024*1024)
   1006 			videodram_size = 1024*1024;
   1007 	}
   1008 }
   1009 /* End of machdep.c */
   1010