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