Home | History | Annotate | Line # | Download | only in ixm1200
ixm1200_machdep.c revision 1.26
      1 /*	$NetBSD: ixm1200_machdep.c,v 1.26 2003/07/13 01:01:51 igy Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003
      5  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Ichiro FUKUHARA.
     19  * 4. The name of the company nor the name of the author may be used to
     20  *    endorse or promote products derived from this software without specific
     21  *    prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 /*
     36  * Copyright (c) 1997,1998 Mark Brinicombe.
     37  * Copyright (c) 1997,1998 Causality Limited.
     38  * All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *      This product includes software developed by Mark Brinicombe
     51  *      for the NetBSD Project.
     52  * 4. The name of the company nor the name of the author may be used to
     53  *    endorse or promote products derived from this software without specific
     54  *    prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     57  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     58  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     59  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     60  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     61  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     62  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: ixm1200_machdep.c,v 1.26 2003/07/13 01:01:51 igy Exp $");
     71 
     72 #include "opt_ddb.h"
     73 #include "opt_pmap_debug.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/device.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/exec.h>
     80 #include <sys/proc.h>
     81 #include <sys/msgbuf.h>
     82 #include <sys/reboot.h>
     83 #include <sys/termios.h>
     84 #include <sys/ksyms.h>
     85 
     86 #include <uvm/uvm_extern.h>
     87 
     88 #include <dev/cons.h>
     89 
     90 #include "ksyms.h"
     91 
     92 #if NKSYMS || defined(DDB) || defined(LKM)
     93 #include <machine/db_machdep.h>
     94 #include <ddb/db_sym.h>
     95 #include <ddb/db_extern.h>
     96 #ifndef DB_ELFSIZE
     97 #error Must define DB_ELFSIZE!
     98 #endif
     99 #define ELFSIZE	DB_ELFSIZE
    100 #include <sys/exec_elf.h>
    101 #endif
    102 
    103 #include <machine/bootconfig.h>
    104 #include <machine/bus.h>
    105 #include <machine/cpu.h>
    106 #include <machine/frame.h>
    107 #include <arm/undefined.h>
    108 
    109 #include <arm/arm32/machdep.h>
    110 
    111 #include <arm/ixp12x0/ixp12x0reg.h>
    112 #include <arm/ixp12x0/ixp12x0var.h>
    113 #include <arm/ixp12x0/ixp12x0_comreg.h>
    114 #include <arm/ixp12x0/ixp12x0_comvar.h>
    115 #include <arm/ixp12x0/ixp12x0_pcireg.h>
    116 
    117 #include <evbarm/ixm1200/ixm1200reg.h>
    118 #include <evbarm/ixm1200/ixm1200var.h>
    119 
    120 #include "opt_ipkdb.h"
    121 
    122 /* XXX for consinit related hacks */
    123 #include <sys/conf.h>
    124 
    125 void ixp12x0_reset(void) __attribute__((noreturn));
    126 
    127 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    128 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    129 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    130 
    131 /*
    132  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    133  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    134  */
    135 #define KERNEL_VM_SIZE		0x0C000000
    136 
    137 /*
    138  * Address to call from cpu_reset() to reset the machine.
    139  * This is machine architecture dependant as it varies depending
    140  * on where the ROM appears when you turn the MMU off.
    141  */
    142 
    143 u_int cpu_reset_address = (u_int) ixp12x0_reset;
    144 
    145 /*
    146  * Define the default console speed for the board.
    147  */
    148 #ifndef CONMODE
    149 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
    150 #endif
    151 #ifndef CONSPEED
    152 #define CONSPEED B38400
    153 #endif
    154 #ifndef CONADDR
    155 #define CONADDR IXPCOM_UART_BASE
    156 #endif
    157 
    158 /* Define various stack sizes in pages */
    159 #define IRQ_STACK_SIZE  1
    160 #define ABT_STACK_SIZE  1
    161 #ifdef IPKDB
    162 #define UND_STACK_SIZE  2
    163 #else
    164 #define UND_STACK_SIZE  1
    165 #endif
    166 
    167 BootConfig bootconfig;          /* Boot config storage */
    168 char *boot_args = NULL;
    169 char *boot_file = NULL;
    170 
    171 vm_offset_t physical_start;
    172 vm_offset_t physical_freestart;
    173 vm_offset_t physical_freeend;
    174 vm_offset_t physical_end;
    175 u_int free_pages;
    176 vm_offset_t pagetables_start;
    177 int physmem = 0;
    178 
    179 /*int debug_flags;*/
    180 #ifndef PMAP_STATIC_L1S
    181 int max_processes = 64;                 /* Default number */
    182 #endif  /* !PMAP_STATIC_L1S */
    183 
    184 /* Physical and virtual addresses for some global pages */
    185 pv_addr_t systempage;
    186 pv_addr_t irqstack;
    187 pv_addr_t undstack;
    188 pv_addr_t abtstack;
    189 pv_addr_t kernelstack;
    190 
    191 vm_offset_t msgbufphys;
    192 
    193 extern u_int data_abort_handler_address;
    194 extern u_int prefetch_abort_handler_address;
    195 extern u_int undefined_handler_address;
    196 extern int end;
    197 
    198 #ifdef PMAP_DEBUG
    199 extern int pmap_debug_level;
    200 #endif  /* PMAP_DEBUG */
    201 
    202 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    203 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    204 #define KERNEL_PT_KERNEL_NUM	2
    205 #define KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    206 					/* Page table for mapping IO */
    207 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
    208 					/* Page tables for mapping kernel VM */
    209 #define KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    210 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    211 
    212 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    213 
    214 struct user *proc0paddr;
    215 
    216 #ifdef CPU_IXP12X0
    217 #define CPU_IXP12X0_CACHE_CLEAN_SIZE (0x4000 * 2)
    218 extern unsigned int ixp12x0_cache_clean_addr;
    219 extern unsigned int ixp12x0_cache_clean_size;
    220 static vaddr_t ixp12x0_cc_base;
    221 #endif  /* CPU_IXP12X0 */
    222 
    223 /* Prototypes */
    224 
    225 void consinit		__P((void));
    226 u_int cpu_get_control	__P((void));
    227 
    228 void ixdp_ixp12x0_cc_setup(void);
    229 
    230 extern int db_trapper(u_int, u_int, trapframe_t *, int);
    231 
    232 /*
    233  * void cpu_reboot(int howto, char *bootstr)
    234  *
    235  * Reboots the system
    236  *
    237  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    238  * then reset the CPU.
    239  */
    240 
    241 void
    242 cpu_reboot(howto, bootstr)
    243 	int howto;
    244 	char *bootstr;
    245 {
    246 	/*
    247 	 * If we are still cold then hit the air brakes
    248 	 * and crash to earth fast
    249 	 */
    250 	if (cold) {
    251 		doshutdownhooks();
    252 		printf("Halted while still in the ICE age.\n");
    253 		printf("The operating system has halted.\n");
    254 		printf("Please press any key to reboot.\n\n");
    255 		cngetc();
    256 		printf("rebooting...\n");
    257 		ixp12x0_reset();
    258 	}
    259 
    260 	/* Disable console buffering */
    261 	cnpollc(1);
    262 
    263 	/*
    264 	 * If RB_NOSYNC was not specified sync the discs.
    265 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
    266 	 * It looks like syslogd is getting woken up only to find that it cannot
    267 	 * page part of the binary in as the filesystem has been unmounted.
    268 	 */
    269 	if (!(howto & RB_NOSYNC))
    270 		bootsync();
    271 
    272 	/* Say NO to interrupts */
    273 	splhigh();
    274 
    275 	/* Do a dump if requested. */
    276 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    277 		dumpsys();
    278 
    279 	/* Run any shutdown hooks */
    280 	doshutdownhooks();
    281 
    282 	/* Make sure IRQ's are disabled */
    283 	IRQdisable;
    284 
    285 	if (howto & RB_HALT) {
    286 		printf("The operating system has halted.\n");
    287 		printf("Please press any key to reboot.\n\n");
    288 		cngetc();
    289 	}
    290 
    291 	printf("rebooting...\n");
    292 
    293 	/* all interrupts are disabled */
    294 	disable_interrupts(I32_bit);
    295 
    296 	ixp12x0_reset();
    297 
    298 	/* ...and if that didn't work, just croak. */
    299 	printf("RESET FAILED!\n");
    300 	for (;;);
    301 }
    302 
    303 /* Static device mappings. */
    304 static const struct pmap_devmap ixm1200_devmap[] = {
    305 	/* StrongARM System and Peripheral Registers */
    306 	{
    307 		IXP12X0_SYS_VBASE,
    308 		IXP12X0_SYS_HWBASE,
    309 		IXP12X0_SYS_SIZE,
    310 		VM_PROT_READ|VM_PROT_WRITE,
    311 		PTE_NOCACHE,
    312 	},
    313 	/* PCI Registers Accessible Through StrongARM Core */
    314 	{
    315 		IXP12X0_PCI_VBASE, IXP12X0_PCI_HWBASE,
    316 		IXP12X0_PCI_SIZE,
    317 		VM_PROT_READ|VM_PROT_WRITE,
    318 		PTE_NOCACHE,
    319 	},
    320 	/* PCI Registers Accessible Through I/O Cycle Access */
    321 	{
    322 		IXP12X0_PCI_IO_VBASE, IXP12X0_PCI_IO_HWBASE,
    323 		IXP12X0_PCI_IO_SIZE,
    324 		VM_PROT_READ|VM_PROT_WRITE,
    325 		PTE_NOCACHE,
    326 	},
    327 	/* PCI Type0 Configuration Space */
    328 	{
    329 		IXP12X0_PCI_TYPE0_VBASE, IXP12X0_PCI_TYPE0_HWBASE,
    330 		IXP12X0_PCI_TYPE0_SIZE,
    331 		VM_PROT_READ|VM_PROT_WRITE,
    332 		PTE_NOCACHE,
    333 	},
    334 	/* PCI Type1 Configuration Space */
    335 	{
    336 		IXP12X0_PCI_TYPE1_VBASE, IXP12X0_PCI_TYPE1_HWBASE,
    337 		IXP12X0_PCI_TYPE1_SIZE,
    338 		VM_PROT_READ|VM_PROT_WRITE,
    339 		PTE_NOCACHE,
    340 	},
    341 	{
    342 		0,
    343 		0,
    344 		0,
    345 		0,
    346 		0
    347 	},
    348 };
    349 
    350 /*
    351  * Initial entry point on startup. This gets called before main() is
    352  * entered.
    353  * It should be responsible for setting up everything that must be
    354  * in place when main is called.
    355  * This includes
    356  *   Taking a copy of the boot configuration structure.
    357  *   Initialising the physical console so characters can be printed.
    358  *   Setting up page tables for the kernel
    359  *   Relocating the kernel to the bottom of physical memory
    360  */
    361 u_int
    362 initarm(void *arg)
    363 {
    364         int loop;
    365 	int loop1;
    366 	u_int kerneldatasize, symbolsize;
    367 	vaddr_t l1pagetable;
    368 	vaddr_t freemempos;
    369 	pv_addr_t kernel_l1pt;
    370 #if NKSYMS || defined(DDB) || defined(LKM)
    371         Elf_Shdr *sh;
    372 #endif
    373 
    374         /*
    375          * Since we map v0xf0000000 == p0x90000000, it's possible for
    376          * us to initialize the console now.
    377          */
    378 	consinit();
    379 
    380 #ifdef VERBOSE_INIT_ARM
    381 	/* Talk to the user */
    382 	printf("\nNetBSD/evbarm (IXM1200) booting ...\n");
    383 #endif
    384 
    385 	/*
    386 	 * Heads up ... Setup the CPU / MMU / TLB functions
    387 	 */
    388 	if (set_cpufuncs())
    389 		panic("cpu not recognized!");
    390 
    391 	/* XXX overwrite bootconfig to hardcoded values */
    392 	bootconfig.dram[0].address = 0xc0000000;
    393 	bootconfig.dram[0].pages   = 0x10000000 / PAGE_SIZE; /* SDRAM 256MB */
    394 	bootconfig.dramblocks = 1;
    395 
    396 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
    397 
    398 	symbolsize = 0;
    399 
    400 #ifdef PMAP_DEBUG
    401 	pmap_debug(-1);
    402 #endif
    403 
    404 #if NKSYMS || defined(DDB) || defined(LKM)
    405         if (! memcmp(&end, "\177ELF", 4)) {
    406                 sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
    407                 loop = ((Elf_Ehdr *)&end)->e_shnum;
    408                 for(; loop; loop--, sh++)
    409                         if (sh->sh_offset > 0 &&
    410                             (sh->sh_offset + sh->sh_size) > symbolsize)
    411                                 symbolsize = sh->sh_offset + sh->sh_size;
    412         }
    413 #endif
    414 #ifdef VERBOSE_INIT_ARM
    415 	printf("kernsize=0x%x\n", kerneldatasize);
    416 #endif
    417 	kerneldatasize += symbolsize;
    418 	kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) + PAGE_SIZE * 8;
    419 
    420 	/*
    421 	 * Set up the variables that define the availablilty of physcial
    422 	 * memory
    423 	 */
    424 	physical_start = bootconfig.dram[0].address;
    425 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    426 
    427 	physical_freestart = physical_start
    428 		+ (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
    429 	physical_freeend = physical_end;
    430 
    431 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    432 
    433 	freemempos = 0xc0000000;
    434 
    435 #ifdef VERBOSE_INIT_ARM
    436 	printf("Allocating page tables\n");
    437 #endif
    438 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    439 
    440 #ifdef VERBOSE_INIT_ARM
    441 	printf("CP15 Register1 = 0x%08x\n", cpu_get_control());
    442 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    443 		physical_freestart, free_pages, free_pages);
    444 	printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
    445 		physical_start, physical_end);
    446 #endif
    447 
    448 	/* Define a macro to simplify memory allocation */
    449 #define valloc_pages(var, np)			\
    450 	alloc_pages((var).pv_pa, (np));		\
    451 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    452 #define alloc_pages(var, np)				\
    453 	(var) = freemempos;				\
    454 	memset((char *)(var), 0, ((np) * PAGE_SIZE));	\
    455 	freemempos += (np) * PAGE_SIZE;
    456 
    457 	loop1 = 0;
    458 	kernel_l1pt.pv_pa = 0;
    459 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    460 		/* Are we 16KB aligned for an L1 ? */
    461 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    462 		    && kernel_l1pt.pv_pa == 0) {
    463 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    464 		} else {
    465 			valloc_pages(kernel_pt_table[loop1],
    466 			    L2_TABLE_SIZE / PAGE_SIZE);
    467 			++loop1;
    468 		}
    469 	}
    470 
    471 #ifdef DIAGNOSTIC
    472 	/* This should never be able to happen but better confirm that. */
    473 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    474 		panic("initarm: Failed to align the kernel page directory");
    475 #endif
    476 
    477 	/*
    478 	 * Allocate a page for the system page mapped to V0x00000000
    479 	 * This page will just contain the system vectors and can be
    480 	 * shared by all processes.
    481 	 */
    482 	alloc_pages(systempage.pv_pa, 1);
    483 
    484 	/* Allocate stacks for all modes */
    485 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    486 	valloc_pages(abtstack, ABT_STACK_SIZE);
    487 	valloc_pages(undstack, UND_STACK_SIZE);
    488 	valloc_pages(kernelstack, UPAGES);
    489 
    490 #ifdef VERBOSE_INIT_ARM
    491 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
    492 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
    493 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
    494 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
    495 #endif
    496 
    497 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    498 
    499 #ifdef CPU_IXP12X0
    500         /*
    501          * XXX totally stuffed hack to work round problems introduced
    502          * in recent versions of the pmap code. Due to the calls used there
    503          * we cannot allocate virtual memory during bootstrap.
    504          */
    505 	for(;;) {
    506 		alloc_pages(ixp12x0_cc_base, 1);
    507 		if (! (ixp12x0_cc_base & (CPU_IXP12X0_CACHE_CLEAN_SIZE - 1)))
    508 			break;
    509 	}
    510 	{
    511 		vaddr_t dummy;
    512 		alloc_pages(dummy, CPU_IXP12X0_CACHE_CLEAN_SIZE / PAGE_SIZE - 1);
    513 	}
    514 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
    515 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
    516 #endif /* CPU_IXP12X0 */
    517 
    518 #ifdef VERBOSE_INIT_ARM
    519 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    520 #endif
    521 
    522 	/*
    523 	 * Now we start construction of the L1 page table
    524 	 * We start by mapping the L2 page tables into the L1.
    525 	 * This means that we can replace L1 mappings later on if necessary
    526 	 */
    527 	l1pagetable = kernel_l1pt.pv_pa;
    528 
    529 	/* Map the L2 pages tables in the L1 page table */
    530 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    531 	    &kernel_pt_table[KERNEL_PT_SYS]);
    532 
    533 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    534 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    535 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    536 
    537 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    538 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    539 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    540 
    541 	/* update the top of the kernel VM */
    542 	pmap_curmaxkvaddr =
    543 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    544 
    545 	pmap_link_l2pt(l1pagetable, IXP12X0_IO_VBASE,
    546 	    &kernel_pt_table[KERNEL_PT_IO]);
    547 
    548 #ifdef VERBOSE_INIT_ARM
    549 	printf("Mapping kernel\n");
    550 #endif
    551 
    552 #if XXX
    553 	/* Now we fill in the L2 pagetable for the kernel code/data */
    554 	{
    555 		extern char etext[], _end[];
    556 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    557 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    558 		u_int logical;
    559 
    560 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    561 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    562 
    563 		logical = 0x00200000;   /* offset of kernel in RAM */
    564 
    565 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    566 		    physical_start + logical, textsize,
    567 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    568 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    569 		    physical_start + logical, totalsize - textsize,
    570 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    571 	}
    572 #else
    573 	{
    574 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
    575                     KERNEL_TEXT_BASE, kerneldatasize,
    576                     VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    577 	}
    578 #endif
    579 
    580 #ifdef VERBOSE_INIT_ARM
    581         printf("Constructing L2 page tables\n");
    582 #endif
    583 
    584 	/* Map the stack pages */
    585 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    586 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    587 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    588 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    589 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    590 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    591 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    592 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    593 
    594 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    595 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    596 
    597 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    598 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    599 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    600 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    601 	}
    602 
    603 	/* Map the vector page. */
    604 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    605 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    606 
    607 #ifdef VERBOSE_INIT_ARM
    608 	printf("systempage (vector page): p0x%08lx v0x%08lx\n",
    609 	       systempage.pv_pa, vector_page);
    610 #endif
    611 
    612 	/* Map the statically mapped devices. */
    613 	pmap_devmap_bootstrap(l1pagetable, ixm1200_devmap);
    614 
    615 #ifdef VERBOSE_INIT_ARM
    616 	printf("done.\n");
    617 #endif
    618 
    619 	/*
    620 	 * Map the Dcache Flush page.
    621 	 * Hw Ref Manual 3.2.4.5 Software Dcache Flush
    622 	 */
    623 	pmap_map_chunk(l1pagetable, ixp12x0_cache_clean_addr, 0xe0000000,
    624 	    CPU_IXP12X0_CACHE_CLEAN_SIZE, VM_PROT_READ, PTE_CACHE);
    625 
    626 	/*
    627 	 * Now we have the real page tables in place so we can switch to them.
    628 	 * Once this is done we will be running with the REAL kernel page
    629 	 * tables.
    630 	 */
    631 
    632 	/* Switch tables */
    633 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    634 	setttb(kernel_l1pt.pv_pa);
    635 	cpu_tlb_flushID();
    636 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    637 
    638 	/*
    639 	 * Moved here from cpu_startup() as data_abort_handler() references
    640 	 * this during init
    641 	 */
    642 	proc0paddr = (struct user *)kernelstack.pv_va;
    643 	lwp0.l_addr = proc0paddr;
    644 
    645 	/*
    646 	 * We must now clean the cache again....
    647 	 * Cleaning may be done by reading new data to displace any
    648 	 * dirty data in the cache. This will have happened in setttb()
    649 	 * but since we are boot strapping the addresses used for the read
    650 	 * may have just been remapped and thus the cache could be out
    651 	 * of sync. A re-clean after the switch will cure this.
    652 	 * After booting there are no gross reloations of the kernel thus
    653 	 * this problem will not occur after initarm().
    654 	 */
    655 	cpu_idcache_wbinv_all();
    656 
    657 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    658 
    659 	/*
    660 	 * Pages were allocated during the secondary bootstrap for the
    661 	 * stacks for different CPU modes.
    662 	 * We must now set the r13 registers in the different CPU modes to
    663 	 * point to these stacks.
    664 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    665 	 * of the stack memory.
    666 	 */
    667 #ifdef VERBOSE_INIT_ARM
    668 	printf("init subsystems: stacks ");
    669 #endif
    670 
    671 	set_stackptr(PSR_IRQ32_MODE,
    672 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    673 	set_stackptr(PSR_ABT32_MODE,
    674 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    675 	set_stackptr(PSR_UND32_MODE,
    676 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    677 #ifdef PMAP_DEBUG
    678 	if (pmap_debug_level >= 0)
    679 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
    680 		    kernelstack.pv_pa);
    681 #endif  /* PMAP_DEBUG */
    682 
    683 	/*
    684 	 * Well we should set a data abort handler.
    685 	 * Once things get going this will change as we will need a proper
    686 	 * handler. Until then we will use a handler that just panics but
    687 	 * tells us why.
    688 	 * Initialisation of the vetcors will just panic on a data abort.
    689 	 * This just fills in a slighly better one.
    690 	 */
    691 #ifdef VERBOSE_INIT_ARM
    692 	printf("vectors ");
    693 #endif
    694 	data_abort_handler_address = (u_int)data_abort_handler;
    695 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    696 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    697 #ifdef VERBOSE_INIT_ARM
    698 	printf("\ndata_abort_handler_address = %08x\n", data_abort_handler_address);
    699 	printf("prefetch_abort_handler_address = %08x\n", prefetch_abort_handler_address);
    700 	printf("undefined_handler_address = %08x\n", undefined_handler_address);
    701 #endif
    702 
    703 	/* Initialise the undefined instruction handlers */
    704 #ifdef VERBOSE_INIT_ARM
    705 	printf("undefined ");
    706 #endif
    707 	undefined_init();
    708 
    709 	/* Load memory into UVM. */
    710 #ifdef VERBOSE_INIT_ARM
    711 	printf("page ");
    712 #endif
    713 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    714 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    715 	    atop(physical_freestart), atop(physical_freeend),
    716 	    VM_FREELIST_DEFAULT);
    717 
    718 	/* Boot strap pmap telling it where the kernel page table is */
    719 #ifdef VERBOSE_INIT_ARM
    720 	printf("pmap ");
    721 #endif
    722 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
    723 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
    724 
    725 	/* Setup the IRQ system */
    726 #ifdef VERBOSE_INIT_ARM
    727 	printf("irq ");
    728 #endif
    729 	ixp12x0_intr_init();
    730 
    731 #ifdef VERBOSE_INIT_ARM
    732 	printf("done.\n");
    733 #endif
    734 
    735 #ifdef VERBOSE_INIT_ARM
    736 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    737 		physical_freestart, free_pages, free_pages);
    738 	printf("freemempos=%08lx\n", freemempos);
    739 	printf("switching to new L1 page table  @%#lx... \n", kernel_l1pt.pv_pa);
    740 #endif
    741 
    742 	consinit();
    743 #ifdef VERBOSE_INIT_ARM
    744 	printf("consinit \n");
    745 #endif
    746 
    747 	ixdp_ixp12x0_cc_setup();
    748 
    749 #ifdef VERBOSE_INIT_ARM
    750 	printf("bootstrap done.\n");
    751 #endif
    752 
    753 #ifdef IPKDB
    754 	/* Initialise ipkdb */
    755 	ipkdb_init();
    756 	if (boothowto & RB_KDB)
    757 		ipkdb_connect(0);
    758 #endif  /* NIPKDB */
    759 
    760 #if NKSYMS || defined(DDB) || defined(LKM)
    761 	ksyms_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
    762 #endif
    763 
    764 #ifdef DDB
    765 	{
    766 		static struct undefined_handler uh;
    767 
    768 		uh.uh_handler = db_trapper;
    769 		install_coproc_handler_static(0, &uh);
    770 	}
    771 
    772 	if (boothowto & RB_KDB)
    773 		Debugger();
    774 #endif
    775 
    776 	/* We return the new stack pointer address */
    777 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    778 }
    779 
    780 void
    781 consinit(void)
    782 {
    783 	extern struct bus_space ixpsip_bs_tag;
    784 	static int consinit_called = 0;
    785 
    786 	if (consinit_called != 0)
    787 		return;
    788 
    789 	consinit_called = 1;
    790 
    791 	pmap_devmap_register(ixm1200_devmap);
    792 
    793 	if (ixpcomcnattach(&ixpsip_bs_tag,
    794 			   IXPCOM_UART_HWBASE, IXPCOM_UART_VBASE,
    795 			   CONSPEED, CONMODE))
    796 		panic("can't init serial console @%lx", IXPCOM_UART_HWBASE);
    797 }
    798 
    799 /*
    800  * For optimal cache cleaning we need two 16K banks of
    801  * virtual address space that NOTHING else will access
    802  * and then we alternate the cache cleaning between the
    803  * two banks.
    804  * The cache cleaning code requires requires 2 banks aligned
    805  * on total size boundry so the banks can be alternated by
    806  * eorring the size bit (assumes the bank size is a power of 2)
    807  */
    808 void
    809 ixdp_ixp12x0_cc_setup(void)
    810 {
    811 	int loop;
    812 	paddr_t kaddr;
    813 	pt_entry_t *pte;
    814 
    815 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
    816 	for (loop = 0; loop < CPU_IXP12X0_CACHE_CLEAN_SIZE; loop += PAGE_SIZE) {
    817                 pte = vtopte(ixp12x0_cc_base + loop);
    818                 *pte = L2_S_PROTO | kaddr |
    819                     L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
    820 		PTE_SYNC(pte);
    821         }
    822 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
    823 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
    824 }
    825