Home | History | Annotate | Line # | Download | only in ixm1200
ixm1200_machdep.c revision 1.13
      1 /*	$NetBSD: ixm1200_machdep.c,v 1.13 2003/03/25 06:53:15 igy Exp $ */
      2 #undef DEBUG_BEFOREMMU
      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.13 2003/03/25 06:53:15 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 
     85 #include <dev/cons.h>
     86 
     87 #ifdef DDB
     88 #include <machine/db_machdep.h>
     89 #include <ddb/db_sym.h>
     90 #include <ddb/db_extern.h>
     91 #ifndef DB_ELFSIZE
     92 #error Must define DB_ELFSIZE!
     93 #endif
     94 #define ELFSIZE	DB_ELFSIZE
     95 #include <sys/exec_elf.h>
     96 #endif
     97 
     98 #include <machine/bootconfig.h>
     99 #include <machine/bus.h>
    100 #include <machine/cpu.h>
    101 #include <machine/frame.h>
    102 #include <arm/undefined.h>
    103 
    104 #include <arm/arm32/machdep.h>
    105 
    106 #include <arm/ixp12x0/ixp12x0reg.h>
    107 #include <arm/ixp12x0/ixp12x0var.h>
    108 #include <arm/ixp12x0/ixp12x0_comreg.h>
    109 #include <arm/ixp12x0/ixp12x0_comvar.h>
    110 #include <arm/ixp12x0/ixp12x0_pcireg.h>
    111 
    112 #include <evbarm/ixm1200/ixm1200reg.h>
    113 #include <evbarm/ixm1200/ixm1200var.h>
    114 
    115 #include "opt_ipkdb.h"
    116 
    117 /* XXX for consinit related hacks */
    118 #include <sys/conf.h>
    119 
    120 void ixp12x0_reset(void) __attribute__((noreturn));
    121 
    122 /*
    123  * Address to call from cpu_reset() to reset the machine.
    124  * This is machine architecture dependant as it varies depending
    125  * on where the ROM appears when you turn the MMU off.
    126  */
    127 
    128 u_int cpu_reset_address = (u_int) ixp12x0_reset;
    129 
    130 /*
    131  * Define the default console speed for the board.
    132  */
    133 #ifndef CONMODE
    134 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
    135 #endif
    136 #ifndef CONSPEED
    137 #define CONSPEED B38400
    138 #endif
    139 #ifndef CONADDR
    140 #define CONADDR IXPCOM_UART_BASE
    141 #endif
    142 
    143 cons_decl(com);
    144 cons_decl(ixpcom);
    145 
    146 struct consdev constab[] = {
    147 #if (NIXPCOM > 0)
    148 	cons_init(ixpcom),
    149 #endif
    150 	{ 0 },
    151 };
    152 
    153 /* Define various stack sizes in pages */
    154 #define IRQ_STACK_SIZE  1
    155 #define ABT_STACK_SIZE  1
    156 #ifdef IPKDB
    157 #define UND_STACK_SIZE  2
    158 #else
    159 #define UND_STACK_SIZE  1
    160 #endif
    161 
    162 BootConfig bootconfig;          /* Boot config storage */
    163 char *boot_args = NULL;
    164 char *boot_file = NULL;
    165 
    166 vm_offset_t physical_start;
    167 vm_offset_t physical_freestart;
    168 vm_offset_t physical_freeend;
    169 vm_offset_t physical_end;
    170 u_int free_pages;
    171 vm_offset_t pagetables_start;
    172 int physmem = 0;
    173 
    174 /*int debug_flags;*/
    175 #ifndef PMAP_STATIC_L1S
    176 int max_processes = 64;                 /* Default number */
    177 #endif  /* !PMAP_STATIC_L1S */
    178 
    179 /* Physical and virtual addresses for some global pages */
    180 pv_addr_t systempage;
    181 pv_addr_t irqstack;
    182 pv_addr_t undstack;
    183 pv_addr_t abtstack;
    184 pv_addr_t kernelstack;
    185 
    186 vm_offset_t msgbufphys;
    187 
    188 extern u_int data_abort_handler_address;
    189 extern u_int prefetch_abort_handler_address;
    190 extern u_int undefined_handler_address;
    191 extern int end;
    192 
    193 #ifdef PMAP_DEBUG
    194 extern int pmap_debug_level;
    195 #endif  /* PMAP_DEBUG */
    196 
    197 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    198 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    199 #define KERNEL_PT_KERNEL_NUM	2
    200 #define KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    201 					/* Page table for mapping IO */
    202 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
    203 					/* Page tables for mapping kernel VM */
    204 #define KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    205 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    206 
    207 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    208 
    209 struct user *proc0paddr;
    210 
    211 #ifdef CPU_IXP12X0
    212 #define CPU_IXP12X0_CACHE_CLEAN_SIZE (0x4000 * 2)
    213 extern unsigned int ixp12x0_cache_clean_addr;
    214 extern unsigned int ixp12x0_cache_clean_size;
    215 static vaddr_t ixp12x0_cc_base;
    216 #endif  /* CPU_IXP12X0 */
    217 
    218 /* Prototypes */
    219 
    220 void consinit		__P((void));
    221 u_int cpu_get_control	__P((void));
    222 
    223 void ixdp_ixp12x0_cc_setup(void);
    224 
    225 #ifdef DEBUG_BEFOREMMU
    226 static void fakecninit();
    227 #endif
    228 
    229 extern int db_trapper(u_int, u_int, trapframe_t *, int);
    230 
    231 /*
    232  * void cpu_reboot(int howto, char *bootstr)
    233  *
    234  * Reboots the system
    235  *
    236  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    237  * then reset the CPU.
    238  */
    239 
    240 void
    241 cpu_reboot(howto, bootstr)
    242 	int howto;
    243 	char *bootstr;
    244 {
    245 	/*
    246 	 * If we are still cold then hit the air brakes
    247 	 * and crash to earth fast
    248 	 */
    249 	if (cold) {
    250 		doshutdownhooks();
    251 		printf("Halted while still in the ICE age.\n");
    252 		printf("The operating system has halted.\n");
    253 		printf("Please press any key to reboot.\n\n");
    254 		cngetc();
    255 		printf("rebooting...\n");
    256 		ixp12x0_reset();
    257 	}
    258 
    259 	/* Disable console buffering */
    260 	cnpollc(1);
    261 
    262 	/*
    263 	 * If RB_NOSYNC was not specified sync the discs.
    264 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
    265 	 * It looks like syslogd is getting woken up only to find that it cannot
    266 	 * page part of the binary in as the filesystem has been unmounted.
    267 	 */
    268 	if (!(howto & RB_NOSYNC))
    269 		bootsync();
    270 
    271 	/* Say NO to interrupts */
    272 	splhigh();
    273 
    274 	/* Do a dump if requested. */
    275 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    276 		dumpsys();
    277 
    278 	/* Run any shutdown hooks */
    279 	doshutdownhooks();
    280 
    281 	/* Make sure IRQ's are disabled */
    282 	IRQdisable;
    283 
    284 	if (howto & RB_HALT) {
    285 		printf("The operating system has halted.\n");
    286 		printf("Please press any key to reboot.\n\n");
    287 		cngetc();
    288 	}
    289 
    290 	printf("rebooting...\n");
    291 
    292 	/* all interrupts are disabled */
    293 	disable_interrupts(I32_bit);
    294 
    295 	ixp12x0_reset();
    296 
    297 	/* ...and if that didn't work, just croak. */
    298 	printf("RESET FAILED!\n");
    299 	for (;;);
    300 }
    301 
    302 /*
    303  * Initial entry point on startup. This gets called before main() is
    304  * entered.
    305  * It should be responsible for setting up everything that must be
    306  * in place when main is called.
    307  * This includes
    308  *   Taking a copy of the boot configuration structure.
    309  *   Initialising the physical console so characters can be printed.
    310  *   Setting up page tables for the kernel
    311  *   Relocating the kernel to the bottom of physical memory
    312  */
    313 u_int
    314 initarm(void *arg)
    315 {
    316         int loop;
    317 	int loop1;
    318 	u_int kerneldatasize, symbolsize;
    319 	vaddr_t l1pagetable;
    320 	vaddr_t freemempos;
    321 	pv_addr_t kernel_l1pt;
    322 	pv_addr_t kernel_ptpt;
    323 #ifdef DDB
    324         Elf_Shdr *sh;
    325 #endif
    326 
    327 #ifdef DEBUG_BEFOREMMU
    328 	/*
    329 	 * At this point, we cannot call real consinit().
    330 	 * Just call a faked up version of consinit(), which does the thing
    331 	 * with MMU disabled.
    332 	 */
    333 	fakecninit();
    334 #endif
    335         /*
    336          * Since we map v0xf0000000 == p0x90000000, it's possible for
    337          * us to initialize the console now.
    338          */
    339 	consinit();
    340 
    341 	/* Talk to the user */
    342 	printf("\nNetBSD/evbarm (IXM1200) booting ...\n");
    343 
    344 	/*
    345 	 * Heads up ... Setup the CPU / MMU / TLB functions
    346 	 */
    347 	if (set_cpufuncs())
    348 		panic("cpu not recognized!");
    349 
    350 	/* XXX overwrite bootconfig to hardcoded values */
    351 	bootconfig.dram[0].address = 0xc0000000;
    352 	bootconfig.dram[0].pages   = 0x10000000 / NBPG;	/* SDRAM 256MB */
    353 	bootconfig.dramblocks = 1;
    354 
    355 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
    356 
    357 	symbolsize = 0;
    358 
    359 #ifdef PMAP_DEBUG
    360 	pmap_debug(-1);
    361 #endif
    362 
    363 #ifdef DDB
    364         if (! memcmp(&end, "\177ELF", 4)) {
    365                 sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
    366                 loop = ((Elf_Ehdr *)&end)->e_shnum;
    367                 for(; loop; loop--, sh++)
    368                         if (sh->sh_offset > 0 &&
    369                             (sh->sh_offset + sh->sh_size) > symbolsize)
    370                                 symbolsize = sh->sh_offset + sh->sh_size;
    371         }
    372 #endif
    373 	printf("kernsize=0x%x\n", kerneldatasize);
    374 	kerneldatasize += symbolsize;
    375 	kerneldatasize = ((kerneldatasize - 1) & ~(NBPG * 4 - 1)) + NBPG * 8;
    376 
    377 	/*
    378 	 * Set up the variables that define the availablilty of physcial
    379 	 * memory
    380 	 */
    381 	physical_start = bootconfig.dram[0].address;
    382 	physical_end = physical_start + (bootconfig.dram[0].pages * NBPG);
    383 
    384 	physical_freestart = physical_start
    385 		+ (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
    386 	physical_freeend = physical_end;
    387 
    388 	physmem = (physical_end - physical_start) / NBPG;
    389 
    390 	freemempos = 0xc0000000;
    391 
    392 #ifdef VERBOSE_INIT_ARM
    393 	printf("Allocating page tables\n");
    394 #endif
    395 	free_pages = (physical_freeend - physical_freestart) / NBPG;
    396 
    397 #ifdef VERBOSE_INIT_ARM
    398 	printf("CP15 Register1 = 0x%08x\n", cpu_get_control());
    399 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    400 		physical_freestart, free_pages, free_pages);
    401 	printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
    402 		physical_start, physical_end);
    403 #endif
    404 
    405 	/* Define a macro to simplify memory allocation */
    406 #define valloc_pages(var, np)			\
    407 	alloc_pages((var).pv_pa, (np));		\
    408 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    409 #define alloc_pages(var, np)				\
    410 	(var) = freemempos;				\
    411 	memset((char *)(var), 0, ((np) * NBPG));	\
    412 	freemempos += (np) * NBPG;
    413 
    414 	loop1 = 0;
    415 	kernel_l1pt.pv_pa = 0;
    416 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    417 		/* Are we 16KB aligned for an L1 ? */
    418 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    419 		    && kernel_l1pt.pv_pa == 0) {
    420 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
    421 		} else {
    422 			alloc_pages(kernel_pt_table[loop1].pv_pa,
    423 			    L2_TABLE_SIZE / NBPG);
    424 			kernel_pt_table[loop1].pv_va =
    425 			    kernel_pt_table[loop1].pv_pa;
    426 			++loop1;
    427 		}
    428 	}
    429 
    430 #ifdef DIAGNOSTIC
    431 	/* This should never be able to happen but better confirm that. */
    432 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    433 		panic("initarm: Failed to align the kernel page directory");
    434 #endif
    435 
    436 	/*
    437 	 * Allocate a page for the system page mapped to V0x00000000
    438 	 * This page will just contain the system vectors and can be
    439 	 * shared by all processes.
    440 	 */
    441 	alloc_pages(systempage.pv_pa, 1);
    442 
    443 	/* Allocate a page for the page table to map kernel page tables. */
    444 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
    445 
    446 	/* Allocate stacks for all modes */
    447 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    448 	valloc_pages(abtstack, ABT_STACK_SIZE);
    449 	valloc_pages(undstack, UND_STACK_SIZE);
    450 	valloc_pages(kernelstack, UPAGES);
    451 
    452 #ifdef VERBOSE_INIT_ARM
    453 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
    454 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
    455 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
    456 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
    457 #endif
    458 
    459 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
    460 
    461 #ifdef CPU_IXP12X0
    462         /*
    463          * XXX totally stuffed hack to work round problems introduced
    464          * in recent versions of the pmap code. Due to the calls used there
    465          * we cannot allocate virtual memory during bootstrap.
    466          */
    467 	for(;;) {
    468 		alloc_pages(ixp12x0_cc_base, 1);
    469 		if (! (ixp12x0_cc_base & (CPU_IXP12X0_CACHE_CLEAN_SIZE - 1)))
    470 			break;
    471 	}
    472 	{
    473 		vaddr_t dummy;
    474 		alloc_pages(dummy, CPU_IXP12X0_CACHE_CLEAN_SIZE / NBPG - 1);
    475 	}
    476 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
    477 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
    478 #endif /* CPU_IXP12X0 */
    479 
    480 #ifdef VERBOSE_INIT_ARM
    481 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    482 #endif
    483 
    484 	/*
    485 	 * Now we start construction of the L1 page table
    486 	 * We start by mapping the L2 page tables into the L1.
    487 	 * This means that we can replace L1 mappings later on if necessary
    488 	 */
    489 	l1pagetable = kernel_l1pt.pv_pa;
    490 
    491 	/* Map the L2 pages tables in the L1 page table */
    492 	pmap_link_l2pt(l1pagetable, 0x00000000,
    493 	    &kernel_pt_table[KERNEL_PT_SYS]);
    494 
    495 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    496 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    497 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    498 
    499 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    500 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    501 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    502 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
    503 
    504 	/* update the top of the kernel VM */
    505 	pmap_curmaxkvaddr =
    506 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    507 
    508 	pmap_link_l2pt(l1pagetable, IXP12X0_IO_VBASE,
    509 	    &kernel_pt_table[KERNEL_PT_IO]);
    510 
    511 #ifdef VERBOSE_INIT_ARM
    512 	printf("Mapping kernel\n");
    513 #endif
    514 
    515 #if XXX
    516 	/* Now we fill in the L2 pagetable for the kernel code/data */
    517 	{
    518 		extern char etext[], _end[];
    519 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    520 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    521 		u_int logical;
    522 
    523 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    524 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    525 
    526 		logical = 0x00200000;   /* offset of kernel in RAM */
    527 
    528 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    529 		    physical_start + logical, textsize,
    530 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    531 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    532 		    physical_start + logical, totalsize - textsize,
    533 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    534 	}
    535 #else
    536 	{
    537 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
    538                     KERNEL_TEXT_BASE, kerneldatasize,
    539                     VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    540 	}
    541 #endif
    542 
    543 #ifdef VERBOSE_INIT_ARM
    544         printf("Constructing L2 page tables\n");
    545 #endif
    546 
    547 	/* Map the stack pages */
    548 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    549 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    550 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    551 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    552 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    553 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    554 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    555 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    556 
    557 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    558 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    559 
    560 	/* Map the page table that maps the kernel pages */
    561 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
    562 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    563 
    564 	/*
    565 	 * Map entries in the page table used to map PTE's
    566 	 * Basically every kernel page table gets mapped here
    567 	 */
    568 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
    569 	pmap_map_entry(l1pagetable,
    570 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
    571 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
    572 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    573 
    574 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    575 		pmap_map_entry(l1pagetable,
    576 		    PTE_BASE + ((KERNEL_BASE +
    577 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
    578 		    kernel_pt_table[KERNEL_PT_KERNEL + loop].pv_pa,
    579 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    580 
    581 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    582 		pmap_map_entry(l1pagetable,
    583 		    PTE_BASE + ((KERNEL_VM_BASE +
    584 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
    585 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
    586 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    587 
    588 	pmap_map_entry(l1pagetable,
    589 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
    590 	    kernel_ptpt.pv_pa, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
    591 
    592 	/* Map the vector page. */
    593 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
    594 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    595 
    596 #ifdef VERBOSE_INIT_ARM
    597 	printf("systempage (vector page): p0x%08lx v0x%08lx\n",
    598 	       systempage.pv_pa, vector_page);
    599 #endif
    600 
    601 	/*
    602 	 * Map the PCI I/O spaces and IXP12x0 registers
    603 	 */
    604 
    605 	ixp12x0_pmap_io_reg(l1pagetable);
    606 
    607 	printf("done.\n");
    608 
    609 	/*
    610 	 * Map the Dcache Flush page.
    611 	 * Hw Ref Manual 3.2.4.5 Software Dcache Flush
    612 	 */
    613 	pmap_map_chunk(l1pagetable, ixp12x0_cache_clean_addr, 0xe0000000,
    614 	    CPU_IXP12X0_CACHE_CLEAN_SIZE, VM_PROT_READ, PTE_CACHE);
    615 
    616 	/*
    617 	 * Now we have the real page tables in place so we can switch to them.
    618 	 * Once this is done we will be running with the REAL kernel page
    619 	 * tables.
    620 	 */
    621 
    622 	/* Switch tables */
    623 	setttb(kernel_l1pt.pv_pa);
    624 	cpu_tlb_flushID();
    625 
    626 	/*
    627 	 * We must now clean the cache again....
    628 	 * Cleaning may be done by reading new data to displace any
    629 	 * dirty data in the cache. This will have happened in setttb()
    630 	 * but since we are boot strapping the addresses used for the read
    631 	 * may have just been remapped and thus the cache could be out
    632 	 * of sync. A re-clean after the switch will cure this.
    633 	 * After booting there are no gross reloations of the kernel thus
    634 	 * this problem will not occur after initarm().
    635 	 */
    636 	cpu_idcache_wbinv_all();
    637 
    638 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
    639 
    640 	/*
    641 	 * Pages were allocated during the secondary bootstrap for the
    642 	 * stacks for different CPU modes.
    643 	 * We must now set the r13 registers in the different CPU modes to
    644 	 * point to these stacks.
    645 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    646 	 * of the stack memory.
    647 	 */
    648 	printf("init subsystems: stacks ");
    649 
    650 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
    651 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
    652 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
    653 #ifdef PMAP_DEBUG
    654 	if (pmap_debug_level >= 0)
    655 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
    656 		    kernelstack.pv_pa);
    657 #endif  /* PMAP_DEBUG */
    658 
    659 	/*
    660 	 * Well we should set a data abort handler.
    661 	 * Once things get going this will change as we will need a proper
    662 	 * handler. Until then we will use a handler that just panics but
    663 	 * tells us why.
    664 	 * Initialisation of the vetcors will just panic on a data abort.
    665 	 * This just fills in a slighly better one.
    666 	 */
    667 	printf("vectors ");
    668 	data_abort_handler_address = (u_int)data_abort_handler;
    669 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    670 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    671 	printf("\ndata_abort_handler_address = %08x\n", data_abort_handler_address);
    672 	printf("prefetch_abort_handler_address = %08x\n", prefetch_abort_handler_address);
    673 	printf("undefined_handler_address = %08x\n", undefined_handler_address);
    674 
    675 	/* Initialise the undefined instruction handlers */
    676 	printf("undefined ");
    677 	undefined_init();
    678 
    679 	/* Load memory into UVM. */
    680 	printf("page ");
    681 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    682 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    683 	    atop(physical_freestart), atop(physical_freeend),
    684 	    VM_FREELIST_DEFAULT);
    685 
    686 	/* Boot strap pmap telling it where the kernel page table is */
    687 	printf("pmap ");
    688 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
    689 
    690 	/* Setup the IRQ system */
    691 	printf("irq ");
    692 	ixp12x0_intr_init();
    693 	printf("done.\n");
    694 
    695 #ifdef VERBOSE_INIT_ARM
    696 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    697 		physical_freestart, free_pages, free_pages);
    698 	printf("freemempos=%08lx\n", freemempos);
    699 	printf("switching to new L1 page table  @%#lx... \n", kernel_l1pt.pv_pa);
    700 #endif
    701 
    702 	consinit();
    703 	printf("consinit \n");
    704 
    705 	ixdp_ixp12x0_cc_setup();
    706 
    707 	printf("bootstrap done.\n");
    708 
    709 #ifdef IPKDB
    710 	/* Initialise ipkdb */
    711 	ipkdb_init();
    712 	if (boothowto & RB_KDB)
    713 		ipkdb_connect(0);
    714 #endif  /* NIPKDB */
    715 
    716 #ifdef DDB
    717 	{
    718 		static struct undefined_handler uh;
    719 
    720 		uh.uh_handler = db_trapper;
    721 		install_coproc_handler_static(0, &uh);
    722 	}
    723 	ddb_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
    724 
    725 	if (boothowto & RB_KDB)
    726 		Debugger();
    727 #endif
    728 
    729 	/* We return the new stack pointer address */
    730 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    731 }
    732 
    733 void
    734 consinit(void)
    735 {
    736 	extern struct bus_space ixpsip_bs_tag;
    737 	static int consinit_called = 0;
    738 
    739 	if (consinit_called != 0)
    740 		return;
    741 
    742 	consinit_called = 1;
    743 
    744 	if (ixpcomcnattach(&ixpsip_bs_tag,
    745 			   IXPCOM_UART_HWBASE, IXPCOM_UART_VBASE,
    746 			   CONSPEED, CONMODE))
    747 		panic("can't init serial console @%lx", IXPCOM_UART_HWBASE);
    748 }
    749 
    750 #ifdef DEBUG_BEFOREMMU
    751 cons_decl(ixpcom);
    752 void
    753 fakecninit()
    754 {
    755 	static struct consdev fakecntab = cons_init(ixpcom);
    756 	cn_tab = &fakecntab;
    757 
    758 	(*cn_tab->cn_init)(0);
    759 	cn_tab->cn_pri = CN_REMOTE;
    760 }
    761 #endif
    762 
    763 /*
    764  * For optimal cache cleaning we need two 16K banks of
    765  * virtual address space that NOTHING else will access
    766  * and then we alternate the cache cleaning between the
    767  * two banks.
    768  * The cache cleaning code requires requires 2 banks aligned
    769  * on total size boundry so the banks can be alternated by
    770  * eorring the size bit (assumes the bank size is a power of 2)
    771  */
    772 void
    773 ixdp_ixp12x0_cc_setup(void)
    774 {
    775 	int loop;
    776 	paddr_t kaddr;
    777 	pt_entry_t *pte;
    778 
    779 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
    780 	for (loop = 0; loop < CPU_IXP12X0_CACHE_CLEAN_SIZE; loop += NBPG) {
    781                 pte = vtopte(ixp12x0_cc_base + loop);
    782                 *pte = L2_S_PROTO | kaddr |
    783                     L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
    784 		PTE_SYNC(pte);
    785         }
    786 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
    787 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
    788 }
    789