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