Home | History | Annotate | Line # | Download | only in npwr_fc
npwr_fc_machdep.c revision 1.21.16.3
      1 /*	$NetBSD: npwr_fc_machdep.c,v 1.21.16.3 2020/04/21 18:42:07 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1997,1998 Mark Brinicombe.
     40  * Copyright (c) 1997,1998 Causality Limited.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Mark Brinicombe
     54  *	for the NetBSD Project.
     55  * 4. The name of the company nor the name of the author may be used to
     56  *    endorse or promote products derived from this software without specific
     57  *    prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  * Machine dependent functions for kernel setup for Intel IQ80321 evaluation
     72  * boards using RedBoot firmware.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: npwr_fc_machdep.c,v 1.21.16.3 2020/04/21 18:42:07 martin Exp $");
     77 
     78 #include "opt_arm_debug.h"
     79 #include "opt_console.h"
     80 #include "opt_ddb.h"
     81 #include "opt_kgdb.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/device.h>
     85 #include <sys/systm.h>
     86 #include <sys/kernel.h>
     87 #include <sys/exec.h>
     88 #include <sys/proc.h>
     89 #include <sys/msgbuf.h>
     90 #include <sys/reboot.h>
     91 #include <sys/termios.h>
     92 #include <sys/ksyms.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #include <dev/cons.h>
     97 
     98 #include <machine/db_machdep.h>
     99 #include <ddb/db_sym.h>
    100 #include <ddb/db_extern.h>
    101 
    102 #include <machine/bootconfig.h>
    103 #include <sys/bus.h>
    104 #include <machine/cpu.h>
    105 #include <machine/frame.h>
    106 #include <arm/undefined.h>
    107 
    108 #include <arm/arm32/machdep.h>
    109 
    110 #include <arm/xscale/i80321reg.h>
    111 #include <arm/xscale/i80321var.h>
    112 
    113 #include <dev/pci/ppbreg.h>
    114 
    115 #include <evbarm/iq80321/iq80321reg.h>
    116 #include <evbarm/iq80321/iq80321var.h>
    117 #include <evbarm/iq80321/obiovar.h>
    118 
    119 #include "ksyms.h"
    120 
    121 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
    122 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
    123 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
    124 
    125 /*
    126  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
    127  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
    128  */
    129 #define KERNEL_VM_SIZE		0x0C000000
    130 
    131 BootConfig bootconfig;		/* Boot config storage */
    132 char *boot_args = NULL;
    133 char *boot_file = NULL;
    134 
    135 vaddr_t physical_start;
    136 vaddr_t physical_freestart;
    137 vaddr_t physical_freeend;
    138 vaddr_t physical_end;
    139 u_int free_pages;
    140 
    141 /*int debug_flags;*/
    142 #ifndef PMAP_STATIC_L1S
    143 int max_processes = 64;			/* Default number */
    144 #endif	/* !PMAP_STATIC_L1S */
    145 
    146 /* Physical and virtual addresses for some global pages */
    147 pv_addr_t minidataclean;
    148 
    149 paddr_t msgbufphys;
    150 
    151 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
    152 
    153 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
    154 #define	KERNEL_PT_KERNEL_NUM	4
    155 
    156 					/* L2 table for mapping i80321 */
    157 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
    158 
    159 					/* L2 tables for mapping kernel VM */
    160 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
    161 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    162 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    163 
    164 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    165 
    166 /* Prototypes */
    167 
    168 void	consinit(void);
    169 
    170 #include "com.h"
    171 #if NCOM > 0
    172 #include <dev/ic/comreg.h>
    173 #include <dev/ic/comvar.h>
    174 #endif
    175 
    176 /*
    177  * Define the default console speed for the board.  This is generally
    178  * what the firmware provided with the board defaults to.
    179  */
    180 #ifndef CONSPEED
    181 #define CONSPEED B115200
    182 #endif /* ! CONSPEED */
    183 
    184 #ifndef CONUNIT
    185 #define	CONUNIT	0
    186 #endif
    187 
    188 #ifndef CONMODE
    189 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    190 #endif
    191 
    192 int comcnspeed = CONSPEED;
    193 int comcnmode = CONMODE;
    194 int comcnunit = CONUNIT;
    195 
    196 #if KGDB
    197 #ifndef KGDB_DEVNAME
    198 #error Must define KGDB_DEVNAME
    199 #endif
    200 const char kgdb_devname[] = KGDB_DEVNAME;
    201 
    202 #ifndef KGDB_DEVADDR
    203 #error Must define KGDB_DEVADDR
    204 #endif
    205 unsigned long kgdb_devaddr = KGDB_DEVADDR;
    206 
    207 #ifndef KGDB_DEVRATE
    208 #define KGDB_DEVRATE	CONSPEED
    209 #endif
    210 int kgdb_devrate = KGDB_DEVRATE;
    211 
    212 #ifndef KGDB_DEVMODE
    213 #define KGDB_DEVMODE	CONMODE
    214 #endif
    215 int kgdb_devmode = KGDB_DEVMODE;
    216 #endif /* KGDB */
    217 
    218 /*
    219  * void cpu_reboot(int howto, char *bootstr)
    220  *
    221  * Reboots the system
    222  *
    223  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    224  * then reset the CPU.
    225  */
    226 void
    227 cpu_reboot(int howto, char *bootstr)
    228 {
    229 
    230 	/*
    231 	 * If we are still cold then hit the air brakes
    232 	 * and crash to earth fast
    233 	 */
    234 	if (cold) {
    235 		doshutdownhooks();
    236 		pmf_system_shutdown(boothowto);
    237 		printf("The operating system has halted.\n");
    238 		printf("Please press any key to reboot.\n\n");
    239 		cngetc();
    240 		printf("rebooting...\n");
    241 		goto reset;
    242 	}
    243 
    244 	/* Disable console buffering */
    245 
    246 	/*
    247 	 * If RB_NOSYNC was not specified sync the discs.
    248 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    249 	 * unmount.  It looks like syslogd is getting woken up only to find
    250 	 * that it cannot page part of the binary in as the filesystem has
    251 	 * been unmounted.
    252 	 */
    253 	if (!(howto & RB_NOSYNC))
    254 		bootsync();
    255 
    256 	/* Say NO to interrupts */
    257 	splhigh();
    258 
    259 	/* Do a dump if requested. */
    260 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    261 		dumpsys();
    262 
    263 	/* Run any shutdown hooks */
    264 	doshutdownhooks();
    265 
    266 	pmf_system_shutdown(boothowto);
    267 
    268 	/* Make sure IRQ's are disabled */
    269 	IRQdisable;
    270 
    271 	if (howto & RB_HALT) {
    272 		printf("The operating system has halted.\n");
    273 		printf("Please press any key to reboot.\n\n");
    274 		cngetc();
    275 	}
    276 
    277 	printf("rebooting...\n\r");
    278  reset:
    279 	/*
    280 	 * Make really really sure that all interrupts are disabled,
    281 	 * and poke the Internal Bus and Peripheral Bus reset lines.
    282 	 */
    283 	(void) disable_interrupts(I32_bit|F32_bit);
    284 	*(volatile uint32_t *)(IQ80321_80321_VBASE + VERDE_ATU_BASE +
    285 	    ATU_PCSR) = PCSR_RIB | PCSR_RPB;
    286 
    287 	/* ...and if that didn't work, just croak. */
    288 	printf("RESET FAILED!\n");
    289 	for (;;);
    290 }
    291 
    292 /* Static device mappings. */
    293 static const struct pmap_devmap iq80321_devmap[] = {
    294     /*
    295      * Map the on-board devices VA == PA so that we can access them
    296      * with the MMU on or off.
    297      */
    298     {
    299 	IQ80321_OBIO_BASE,
    300 	IQ80321_OBIO_BASE,
    301 	IQ80321_OBIO_SIZE,
    302 	VM_PROT_READ|VM_PROT_WRITE,
    303 	PTE_NOCACHE,
    304     },
    305 
    306     {
    307 	IQ80321_IOW_VBASE,
    308 	VERDE_OUT_XLATE_IO_WIN0_BASE,
    309 	VERDE_OUT_XLATE_IO_WIN_SIZE,
    310 	VM_PROT_READ|VM_PROT_WRITE,
    311 	PTE_NOCACHE,
    312    },
    313 
    314    {
    315 	IQ80321_80321_VBASE,
    316 	VERDE_PMMR_BASE,
    317 	VERDE_PMMR_SIZE,
    318 	VM_PROT_READ|VM_PROT_WRITE,
    319 	PTE_NOCACHE,
    320    },
    321 
    322    {
    323 	0,
    324 	0,
    325 	0,
    326 	0,
    327 	0,
    328     }
    329 };
    330 
    331 /*
    332  * vaddr_t initarm(...)
    333  *
    334  * Initial entry point on startup. This gets called before main() is
    335  * entered.
    336  * It should be responsible for setting up everything that must be
    337  * in place when main is called.
    338  * This includes
    339  *   Taking a copy of the boot configuration structure.
    340  *   Initialising the physical console so characters can be printed.
    341  *   Setting up page tables for the kernel
    342  *   Relocating the kernel to the bottom of physical memory
    343  */
    344 vaddr_t
    345 initarm(void *arg)
    346 {
    347 	extern vaddr_t xscale_cache_clean_addr;
    348 #ifdef DIAGNOSTIC
    349 	extern vsize_t xscale_minidata_clean_size;
    350 #endif
    351 	int loop;
    352 	int loop1;
    353 	u_int l1pagetable;
    354 	paddr_t memstart;
    355 	psize_t memsize;
    356 
    357 	/* Calibrate the delay loop. */
    358 	i80321_calibrate_delay();
    359 	i80321_hardclock_hook = NULL;
    360 
    361 	/*
    362 	 * Since we map the on-board devices VA==PA, and the kernel
    363 	 * is running VA==PA, it's possible for us to initialize
    364 	 * the console now.
    365 	 */
    366 	consinit();
    367 
    368 #ifdef VERBOSE_INIT_ARM
    369 	/* Talk to the user */
    370 	printf("\nNetBSD/evbarm (NPWR_FC) booting ...\n");
    371 #endif
    372 
    373 	/*
    374 	 * Heads up ... Setup the CPU / MMU / TLB functions
    375 	 */
    376 	if (set_cpufuncs())
    377 		panic("cpu not recognized!");
    378 
    379 	/*
    380 	 * We are currently running with the MMU enabled and the
    381 	 * entire address space mapped VA==PA, except for the
    382 	 * first 64M of RAM is also double-mapped at 0xc0000000.
    383 	 * There is an L1 page table at 0xa0004000.
    384 	 */
    385 
    386 	/*
    387 	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
    388 	 * registers.
    389 	 */
    390 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
    391 	    &memstart, &memsize);
    392 
    393 #ifdef VERBOSE_INIT_ARM
    394 	printf("initarm: Configuring system ...\n");
    395 #endif
    396 
    397 	/* Fake bootconfig structure for the benefit of pmap.c */
    398 	/* XXX must make the memory description h/w independent */
    399 	bootconfig.dramblocks = 1;
    400 	bootconfig.dram[0].address = memstart;
    401 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
    402 
    403 	/*
    404 	 * Set up the variables that define the availablilty of
    405 	 * physical memory.  For now, we're going to set
    406 	 * physical_freestart to 0xa0200000 (where the kernel
    407 	 * was loaded), and allocate the memory we need downwards.
    408 	 * If we get too close to the L1 table that we set up, we
    409 	 * will panic.  We will update physical_freestart and
    410 	 * physical_freeend later to reflect what pmap_bootstrap()
    411 	 * wants to see.
    412 	 *
    413 	 * XXX pmap_bootstrap() needs an enema.
    414 	 */
    415 	physical_start = bootconfig.dram[0].address;
    416 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
    417 
    418 	physical_freestart = 0xa0009000UL;
    419 	physical_freeend = 0xa0200000UL;
    420 
    421 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    422 
    423 #ifdef VERBOSE_INIT_ARM
    424 	/* Tell the user about the memory */
    425 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
    426 	    physical_start, physical_end - 1);
    427 #endif
    428 
    429 	/*
    430 	 * Okay, the kernel starts 2MB in from the bottom of physical
    431 	 * memory.  We are going to allocate our bootstrap pages downwards
    432 	 * from there.
    433 	 *
    434 	 * We need to allocate some fixed page tables to get the kernel
    435 	 * going.  We allocate one page directory and a number of page
    436 	 * tables and store the physical addresses in the kernel_pt_table
    437 	 * array.
    438 	 *
    439 	 * The kernel page directory must be on a 16K boundary.  The page
    440 	 * tables must be on 4K bounaries.  What we do is allocate the
    441 	 * page directory on the first 16K boundary that we encounter, and
    442 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    443 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    444 	 * least one 16K aligned region.
    445 	 */
    446 
    447 #ifdef VERBOSE_INIT_ARM
    448 	printf("Allocating page tables\n");
    449 #endif
    450 
    451 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    452 
    453 #ifdef VERBOSE_INIT_ARM
    454 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
    455 	       physical_freestart, free_pages, free_pages);
    456 #endif
    457 
    458 	/* Define a macro to simplify memory allocation */
    459 #define	valloc_pages(var, np)				\
    460 	alloc_pages((var).pv_pa, (np));			\
    461 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
    462 
    463 #define alloc_pages(var, np)				\
    464 	physical_freeend -= ((np) * PAGE_SIZE);		\
    465 	if (physical_freeend < physical_freestart)	\
    466 		panic("initarm: out of memory");	\
    467 	(var) = physical_freeend;			\
    468 	free_pages -= (np);				\
    469 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
    470 
    471 	loop1 = 0;
    472 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    473 		/* Are we 16KB aligned for an L1 ? */
    474 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
    475 		    && kernel_l1pt.pv_pa == 0) {
    476 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    477 		} else {
    478 			valloc_pages(kernel_pt_table[loop1],
    479 			    L2_TABLE_SIZE / PAGE_SIZE);
    480 			++loop1;
    481 		}
    482 	}
    483 
    484 	/* This should never be able to happen but better confirm that. */
    485 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    486 		panic("initarm: Failed to align the kernel page directory");
    487 
    488 	/*
    489 	 * Allocate a page for the system page mapped to V0x00000000
    490 	 * This page will just contain the system vectors and can be
    491 	 * shared by all processes.
    492 	 */
    493 	alloc_pages(systempage.pv_pa, 1);
    494 
    495 	/* Allocate stacks for all modes */
    496 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    497 	valloc_pages(abtstack, ABT_STACK_SIZE);
    498 	valloc_pages(undstack, UND_STACK_SIZE);
    499 	valloc_pages(kernelstack, UPAGES);
    500 
    501 	/* Allocate enough pages for cleaning the Mini-Data cache. */
    502 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
    503 	valloc_pages(minidataclean, 1);
    504 
    505 #ifdef VERBOSE_INIT_ARM
    506 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
    507 	    irqstack.pv_va);
    508 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
    509 	    abtstack.pv_va);
    510 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
    511 	    undstack.pv_va);
    512 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
    513 	    kernelstack.pv_va);
    514 #endif
    515 
    516 	/*
    517 	 * XXX Defer this to later so that we can reclaim the memory
    518 	 * XXX used by the RedBoot page tables.
    519 	 */
    520 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
    521 
    522 	/*
    523 	 * Ok we have allocated physical pages for the primary kernel
    524 	 * page tables
    525 	 */
    526 
    527 #ifdef VERBOSE_INIT_ARM
    528 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
    529 #endif
    530 
    531 	/*
    532 	 * Now we start construction of the L1 page table
    533 	 * We start by mapping the L2 page tables into the L1.
    534 	 * This means that we can replace L1 mappings later on if necessary
    535 	 */
    536 	l1pagetable = kernel_l1pt.pv_pa;
    537 
    538 	/* Map the L2 pages tables in the L1 page table */
    539 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
    540 	    &kernel_pt_table[KERNEL_PT_SYS]);
    541 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
    542 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
    543 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
    544 	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
    545 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
    546 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
    547 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
    548 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
    549 
    550 	/* update the top of the kernel VM */
    551 	pmap_curmaxkvaddr =
    552 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
    553 
    554 #ifdef VERBOSE_INIT_ARM
    555 	printf("Mapping kernel\n");
    556 #endif
    557 
    558 	/* Now we fill in the L2 pagetable for the kernel static code/data */
    559 	{
    560 		extern char etext[], _end[];
    561 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
    562 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
    563 		u_int logical;
    564 
    565 		textsize = (textsize + PGOFSET) & ~PGOFSET;
    566 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
    567 
    568 		logical = 0x00200000;	/* offset of kernel in RAM */
    569 
    570 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    571 		    physical_start + logical, textsize,
    572 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    573 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
    574 		    physical_start + logical, totalsize - textsize,
    575 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    576 	}
    577 
    578 #ifdef VERBOSE_INIT_ARM
    579 	printf("Constructing L2 page tables\n");
    580 #endif
    581 
    582 	/* Map the stack pages */
    583 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
    584 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    585 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
    586 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    587 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
    588 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    589 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
    590 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    591 
    592 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
    593 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    594 
    595 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    596 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
    597 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
    598 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
    599 	}
    600 
    601 	/* Map the Mini-Data cache clean area. */
    602 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
    603 	    minidataclean.pv_pa);
    604 
    605 	/* Map the vector page. */
    606 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
    607 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
    608 
    609 	/* Map the statically mapped devices. */
    610 	pmap_devmap_bootstrap(l1pagetable, iq80321_devmap);
    611 
    612 	/*
    613 	 * Give the XScale global cache clean code an appropriately
    614 	 * sized chunk of unmapped VA space starting at 0xff000000
    615 	 * (our device mappings end before this address).
    616 	 */
    617 	xscale_cache_clean_addr = 0xff000000U;
    618 
    619 	/*
    620 	 * Now we have the real page tables in place so we can switch to them.
    621 	 * Once this is done we will be running with the REAL kernel page
    622 	 * tables.
    623 	 */
    624 
    625 	/*
    626 	 * Update the physical_freestart/physical_freeend/free_pages
    627 	 * variables.
    628 	 */
    629 	{
    630 		extern char _end[];
    631 
    632 		physical_freestart = physical_start +
    633 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
    634 		     KERNEL_BASE);
    635 		physical_freeend = physical_end;
    636 		free_pages =
    637 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
    638 	}
    639 
    640 	/* Switch tables */
    641 #ifdef VERBOSE_INIT_ARM
    642 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
    643 	       physical_freestart, free_pages, free_pages);
    644 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
    645 #endif
    646 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    647 	cpu_setttb(kernel_l1pt.pv_pa, true);
    648 	cpu_tlb_flushID();
    649 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    650 
    651 	/*
    652 	 * Moved from cpu_startup() as data_abort_handler() references
    653 	 * this during uvm init
    654 	 */
    655 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    656 
    657 #ifdef VERBOSE_INIT_ARM
    658 	printf("done!\n");
    659 #endif
    660 
    661 #ifdef VERBOSE_INIT_ARM
    662 	printf("bootstrap done.\n");
    663 #endif
    664 
    665 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
    666 
    667 	/*
    668 	 * Pages were allocated during the secondary bootstrap for the
    669 	 * stacks for different CPU modes.
    670 	 * We must now set the r13 registers in the different CPU modes to
    671 	 * point to these stacks.
    672 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
    673 	 * of the stack memory.
    674 	 */
    675 #ifdef VERBOSE_INIT_ARM
    676 	printf("init subsystems: stacks ");
    677 #endif
    678 
    679 	set_stackptr(PSR_IRQ32_MODE,
    680 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    681 	set_stackptr(PSR_ABT32_MODE,
    682 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    683 	set_stackptr(PSR_UND32_MODE,
    684 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    685 
    686 	/*
    687 	 * Well we should set a data abort handler.
    688 	 * Once things get going this will change as we will need a proper
    689 	 * handler.
    690 	 * Until then we will use a handler that just panics but tells us
    691 	 * why.
    692 	 * Initialisation of the vectors will just panic on a data abort.
    693 	 * This just fills in a slighly better one.
    694 	 */
    695 #ifdef VERBOSE_INIT_ARM
    696 	printf("vectors ");
    697 #endif
    698 	data_abort_handler_address = (u_int)data_abort_handler;
    699 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    700 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    701 
    702 	/* Initialise the undefined instruction handlers */
    703 #ifdef VERBOSE_INIT_ARM
    704 	printf("undefined ");
    705 #endif
    706 	undefined_init();
    707 
    708 	/* Load memory into UVM. */
    709 #ifdef VERBOSE_INIT_ARM
    710 	printf("page ");
    711 #endif
    712 	uvm_md_init();
    713 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    714 	    atop(physical_freestart), atop(physical_freeend),
    715 	    VM_FREELIST_DEFAULT);
    716 
    717 	/* Boot strap pmap telling it where managed kernel virtual memory is */
    718 #ifdef VERBOSE_INIT_ARM
    719 	printf("pmap ");
    720 #endif
    721 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    722 
    723 	/* Setup the IRQ system */
    724 #ifdef VERBOSE_INIT_ARM
    725 	printf("irq ");
    726 #endif
    727 	i80321_intr_init();
    728 
    729 #ifdef VERBOSE_INIT_ARM
    730 	printf("done.\n");
    731 #endif
    732 
    733 #ifdef BOOTHOWTO
    734 	boothowto = BOOTHOWTO;
    735 #endif
    736 
    737 #ifdef DDB
    738 	db_machine_init();
    739 	if (boothowto & RB_KDB)
    740 		Debugger();
    741 #endif
    742 
    743 	/* We return the new stack pointer address */
    744 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
    745 }
    746 
    747 void
    748 consinit(void)
    749 {
    750 	static const bus_addr_t comcnaddrs[] = {
    751 		IQ80321_UART1,		/* com0 */
    752 	};
    753 	static int consinit_called;
    754 
    755 	if (consinit_called != 0)
    756 		return;
    757 
    758 	consinit_called = 1;
    759 
    760 	/*
    761 	 * Console devices are mapped VA==PA.  Our devmap reflects
    762 	 * this, so register it now so drivers can map the console
    763 	 * device.
    764 	 */
    765 	pmap_devmap_register(iq80321_devmap);
    766 
    767 #if NCOM > 0
    768 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
    769 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
    770 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
    771 #else
    772 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
    773 #endif
    774 #if KGDB
    775 #if NCOM > 0
    776 	if (strcmp(kgdb_devname, "com") == 0) {
    777 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
    778 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
    779 	}
    780 #endif	/* NCOM > 0 */
    781 #endif	/* KGDB */
    782 }
    783