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