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