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