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