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