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