Home | History | Annotate | Line # | Download | only in gemini
gemini_machdep.c revision 1.22.6.1
      1 /*	$NetBSD: gemini_machdep.c,v 1.22.6.1 2017/02/05 13:40:07 skrll Exp $	*/
      2 
      3 /* adapted from:
      4  *	NetBSD: sdp24xx_machdep.c,v 1.4 2008/08/27 11:03:10 matt Exp
      5  */
      6 
      7 /*
      8  * Machine dependent functions for kernel setup for TI OSK5912 board.
      9  * Based on lubbock_machdep.c which in turn was based on iq80310_machhdep.c
     10  *
     11  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
     12  * Written by Hiroyuki Bessho for Genetec Corporation.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. The name of Genetec Corporation may not be used to endorse or
     23  *    promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  *
     38  * Copyright (c) 2001 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     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 for the NetBSD Project by
     54  *	Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  *
     71  * Copyright (c) 1997,1998 Mark Brinicombe.
     72  * Copyright (c) 1997,1998 Causality Limited.
     73  * All rights reserved.
     74  *
     75  * Redistribution and use in source and binary forms, with or without
     76  * modification, are permitted provided that the following conditions
     77  * are met:
     78  * 1. Redistributions of source code must retain the above copyright
     79  *    notice, this list of conditions and the following disclaimer.
     80  * 2. Redistributions in binary form must reproduce the above copyright
     81  *    notice, this list of conditions and the following disclaimer in the
     82  *    documentation and/or other materials provided with the distribution.
     83  * 3. All advertising materials mentioning features or use of this software
     84  *    must display the following acknowledgement:
     85  *	This product includes software developed by Mark Brinicombe
     86  *	for the NetBSD Project.
     87  * 4. The name of the company nor the name of the author may be used to
     88  *    endorse or promote products derived from this software without specific
     89  *    prior written permission.
     90  *
     91  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     92  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     93  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     94  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     95  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     96  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     97  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    101  * SUCH DAMAGE.
    102  *
    103  * Copyright (c) 2007 Microsoft
    104  * All rights reserved.
    105  *
    106  * Redistribution and use in source and binary forms, with or without
    107  * modification, are permitted provided that the following conditions
    108  * are met:
    109  * 1. Redistributions of source code must retain the above copyright
    110  *    notice, this list of conditions and the following disclaimer.
    111  * 2. Redistributions in binary form must reproduce the above copyright
    112  *    notice, this list of conditions and the following disclaimer in the
    113  *    documentation and/or other materials provided with the distribution.
    114  * 3. All advertising materials mentioning features or use of this software
    115  *    must display the following acknowledgement:
    116  *	This product includes software developed by Microsoft
    117  *
    118  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
    119  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    120  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    121  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
    122  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    123  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    124  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    125  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    126  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    127  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    128  * SUCH DAMAGE.
    129  */
    130 
    131 #include <sys/cdefs.h>
    132 __KERNEL_RCSID(0, "$NetBSD: gemini_machdep.c,v 1.22.6.1 2017/02/05 13:40:07 skrll Exp $");
    133 
    134 #include "opt_machdep.h"
    135 #include "opt_ddb.h"
    136 #include "opt_kgdb.h"
    137 #include "opt_ipkdb.h"
    138 #include "opt_md.h"
    139 #include "opt_com.h"
    140 #include "opt_gemini.h"
    141 #include "geminiwdt.h"
    142 #include "geminiipm.h"
    143 
    144 #include <sys/param.h>
    145 #include <sys/device.h>
    146 #include <sys/systm.h>
    147 #include <sys/kernel.h>
    148 #include <sys/exec.h>
    149 #include <sys/proc.h>
    150 #include <sys/msgbuf.h>
    151 #include <sys/reboot.h>
    152 #include <sys/termios.h>
    153 #include <sys/ksyms.h>
    154 #include <sys/bus.h>
    155 #include <sys/cpu.h>
    156 #include <sys/conf.h>
    157 
    158 #include <uvm/uvm_extern.h>
    159 
    160 #include <dev/cons.h>
    161 #include <dev/md.h>
    162 
    163 #include <machine/db_machdep.h>
    164 #include <ddb/db_sym.h>
    165 #include <ddb/db_extern.h>
    166 #ifdef KGDB
    167 #include <sys/kgdb.h>
    168 #endif
    169 
    170 #include <arm/locore.h>
    171 #include <arm/undefined.h>
    172 
    173 #include <arm/arm32/machdep.h>
    174 
    175 #include <machine/bootconfig.h>
    176 
    177 #include <arm/gemini/gemini_reg.h>
    178 #include <arm/gemini/gemini_var.h>
    179 #include <arm/gemini/gemini_wdtvar.h>
    180 #include <arm/gemini/gemini_com.h>
    181 #include <arm/gemini/lpc_com.h>
    182 
    183 #include <evbarm/gemini/gemini.h>
    184 
    185 #if defined(VERBOSE_INIT_ARM)
    186 # define GEMINI_PUTCHAR(c)	gemini_putchar(c)
    187 # define GEMINI_PUTHEX(n)	gemini_puthex(n)
    188 #else	/* VERBOSE_INIT_ARM */
    189 # define GEMINI_PUTCHAR(c)
    190 # define GEMINI_PUTHEX(n)
    191 #endif	/* VERBOSE_INIT_ARM */
    192 
    193 BootConfig bootconfig;		/* Boot config storage */
    194 char *boot_args = NULL;
    195 char *boot_file = NULL;
    196 
    197 /* Physical address of the beginning of SDRAM. */
    198 paddr_t physical_start;
    199 /* Physical address of the first byte after the end of SDRAM. */
    200 paddr_t physical_end;
    201 
    202 /* Same things, but for the free (unused by the kernel) memory. */
    203 static paddr_t physical_freestart, physical_freeend;
    204 static u_int free_pages;
    205 
    206 /* Physical address of the message buffer. */
    207 paddr_t msgbufphys;
    208 
    209 extern char KERNEL_BASE_phys[];
    210 extern char KERNEL_BASE_virt[];
    211 extern char etext[], __data_start[], _edata[], __bss_start[], __bss_end__[];
    212 extern char _end[];
    213 
    214 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
    215 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
    216 #define	KERNEL_PT_KERNEL_NUM	4
    217 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
    218 				        /* Page tables for mapping kernel VM */
    219 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
    220 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
    221 
    222 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
    223 
    224 
    225 #if (NGEMINIIPM > 0)
    226 pv_addr_t ipmq_pt;		/* L2 Page table for mapping IPM queues */
    227 #if defined(DEBUG) || 1
    228 unsigned long gemini_ipmq_pbase = GEMINI_IPMQ_PBASE;
    229 unsigned long gemini_ipmq_vbase = GEMINI_IPMQ_VBASE;
    230 #endif	/* DEBUG */
    231 #endif	/* NGEMINIIPM > 0 */
    232 
    233 
    234 /*
    235  * Macros to translate between physical and virtual for a subset of the
    236  * kernel address space.  *Not* for general use.
    237  */
    238 #define KERNEL_BASE_PHYS ((paddr_t)&KERNEL_BASE_phys)
    239 
    240 #define KERN_VTOPHYS(va) \
    241 	((paddr_t)((vaddr_t)va - KERNEL_BASE + GEMINI_DRAM_BASE))
    242 #define KERN_PHYSTOV(pa) \
    243 	((vaddr_t)((paddr_t)pa - GEMINI_DRAM_BASE + KERNEL_BASE))
    244 
    245 /* Prototypes */
    246 
    247 void gemini_intr_init(bus_space_tag_t);
    248 void consinit(void);
    249 #ifdef KGDB
    250 static void kgdb_port_init(void);
    251 #endif
    252 
    253 static void setup_real_page_tables(void);
    254 static void init_clocks(void);
    255 
    256 bs_protos(bs_notimpl);
    257 
    258 #include "com.h"
    259 #if NCOM > 0
    260 #include <dev/ic/comreg.h>
    261 #include <dev/ic/comvar.h>
    262 #endif
    263 
    264 
    265 static void gemini_global_reset(void) __attribute__ ((noreturn));
    266 static void gemini_cpu1_start(void);
    267 static void gemini_memchk(void);
    268 
    269 static void
    270 gemini_global_reset(void)
    271 {
    272 #if defined(GEMINI_MASTER) || defined(GEMINI_SINGLE)
    273 	volatile uint32_t *rp;
    274 	uint32_t r;
    275 
    276 	rp = (volatile uint32_t *)
    277 		(GEMINI_GLOBAL_VBASE + GEMINI_GLOBAL_RESET_CTL);
    278 	r = *rp;
    279 	r |= GLOBAL_RESET_GLOBAL;
    280 	*rp = r;
    281 #endif
    282 	for(;;);
    283 	/* NOTREACHED */
    284 }
    285 
    286 static void
    287 gemini_cpu1_start(void)
    288 {
    289 #ifdef GEMINI_MASTER
    290 	volatile uint32_t *rp;
    291 	uint32_t r;
    292 
    293 	rp = (volatile uint32_t *)
    294 		(GEMINI_GLOBAL_VBASE + GEMINI_GLOBAL_RESET_CTL);
    295 	r = *rp;
    296 	r &= ~GLOBAL_RESET_CPU1;
    297 	*rp = r;
    298 #endif
    299 }
    300 
    301 static void
    302 gemini_memchk(void)
    303 {
    304 	volatile uint32_t *rp;
    305 	uint32_t r;
    306 	uint32_t base;
    307 	uint32_t size;
    308 
    309 	rp = (volatile uint32_t *)
    310 		(GEMINI_DRAMC_VBASE + GEMINI_DRAMC_RMCR);
    311 	r = *rp;
    312 	base = (r & DRAMC_RMCR_RMBAR) >> DRAMC_RMCR_RMBAR_SHFT;
    313 	size = (r & DRAMC_RMCR_RMSZR) >> DRAMC_RMCR_RMSZR_SHFT;
    314 #if defined(GEMINI_SINGLE)
    315 	if (r != 0)
    316 		panic("%s: RMCR %#x, MEMSIZE %d mismatch\n",
    317 			__FUNCTION__, r, MEMSIZE);
    318 #elif defined(GEMINI_MASTER)
    319 	if (base != MEMSIZE)
    320 		panic("%s: RMCR %#x, MEMSIZE %d mismatch\n",
    321 			__FUNCTION__, r, MEMSIZE);
    322 #elif defined(GEMINI_SLAVE)
    323 	if (size != MEMSIZE)
    324 		panic("%s: RMCR %#x, MEMSIZE %d mismatch\n",
    325 			__FUNCTION__, r, MEMSIZE);
    326 #endif
    327 #if defined(VERBOSE_INIT_ARM) || 1
    328 	printf("DRAM Remap: base=%dMB, size=%dMB\n", base, size);
    329 #endif
    330 }
    331 
    332 /*
    333  * void cpu_reboot(int howto, char *bootstr)
    334  *
    335  * Reboots the system
    336  *
    337  * Deal with any syncing, unmounting, dumping and shutdown hooks,
    338  * then reset the CPU.
    339  */
    340 void
    341 cpu_reboot(int howto, char *bootstr)
    342 {
    343 	extern struct geminitmr_softc *ref_sc;
    344 
    345 #ifdef DIAGNOSTIC
    346 	/* info */
    347 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
    348 #endif
    349 
    350 	/*
    351 	 * If we are still cold then hit the air brakes
    352 	 * and crash to earth fast
    353 	 */
    354 	if (cold) {
    355 		doshutdownhooks();
    356 		pmf_system_shutdown(boothowto);
    357 		printf("The operating system has halted.\n");
    358 		printf("Please press any key to reboot.\n\n");
    359 		cngetc();
    360 		printf("rebooting...\n");
    361 		if (ref_sc != NULL)
    362 			delay(2000);			/* cnflush(); */
    363 		gemini_global_reset();
    364 		/*NOTREACHED*/
    365 	}
    366 
    367 	/* Disable console buffering */
    368 	cnpollc(1);
    369 
    370 	/*
    371 	 * If RB_NOSYNC was not specified sync the discs.
    372 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    373 	 * unmount.  It looks like syslogd is getting woken up only to find
    374 	 * that it cannot page part of the binary in as the filesystem has
    375 	 * been unmounted.
    376 	 */
    377 	if (!(howto & RB_NOSYNC))
    378 		bootsync();
    379 
    380 	/* Say NO to interrupts */
    381 	splhigh();
    382 
    383 	/* Do a dump if requested. */
    384 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    385 		dumpsys();
    386 
    387 	/* Run any shutdown hooks */
    388 	doshutdownhooks();
    389 
    390 	pmf_system_shutdown(boothowto);
    391 
    392 	/* Make sure IRQ's are disabled */
    393 	IRQdisable;
    394 
    395 	if (howto & RB_HALT) {
    396 		printf("The operating system has halted.\n");
    397 		printf("Please press any key to reboot.\n\n");
    398 		cngetc();
    399 	}
    400 
    401 	printf("rebooting...\n");
    402 	if (ref_sc != NULL)
    403 		delay(2000);			/* cnflush(); */
    404 	gemini_global_reset();
    405 	/*NOTREACHED*/
    406 }
    407 
    408 /*
    409  * Static device mappings. These peripheral registers are mapped at
    410  * fixed virtual addresses very early in initarm() so that we can use
    411  * them while booting the kernel, and stay at the same address
    412  * throughout whole kernel's life time.
    413  *
    414  * We use this table twice; once with bootstrap page table, and once
    415  * with kernel's page table which we build up in initarm().
    416  *
    417  * Since we map these registers into the bootstrap page table using
    418  * pmap_devmap_bootstrap() which calls pmap_map_chunk(), we map
    419  * registers segment-aligned and segment-rounded in order to avoid
    420  * using the 2nd page tables.
    421  */
    422 
    423 #define	_A(a)	((a) & ~L1_S_OFFSET)
    424 #define	_S(s)	(((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
    425 
    426 static const struct pmap_devmap devmap[] = {
    427 	/* Global regs */
    428 	{
    429 		.pd_va = _A(GEMINI_GLOBAL_VBASE),
    430 		.pd_pa = _A(GEMINI_GLOBAL_BASE),
    431 		.pd_size = _S(L1_S_SIZE),
    432 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    433 		.pd_cache = PTE_NOCACHE
    434 	},
    435 
    436 	/* Watchdog */
    437 	{
    438 		.pd_va = _A(GEMINI_WATCHDOG_VBASE),
    439 		.pd_pa = _A(GEMINI_WATCHDOG_BASE),
    440 		.pd_size = _S(L1_S_SIZE),
    441 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    442 		.pd_cache = PTE_NOCACHE
    443 	},
    444 
    445 	/* UART */
    446 	{
    447 		.pd_va = _A(GEMINI_UART_VBASE),
    448 		.pd_pa = _A(GEMINI_UART_BASE),
    449 		.pd_size = _S(L1_S_SIZE),
    450 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    451 		.pd_cache = PTE_NOCACHE
    452 	},
    453 
    454 	/* LPCHC */
    455 	{
    456 		.pd_va = _A(GEMINI_LPCHC_VBASE),
    457 		.pd_pa = _A(GEMINI_LPCHC_BASE),
    458 		.pd_size = _S(L1_S_SIZE),
    459 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    460 		.pd_cache = PTE_NOCACHE
    461 	},
    462 
    463 	/* LPCIO */
    464 	{
    465 		.pd_va = _A(GEMINI_LPCIO_VBASE),
    466 		.pd_pa = _A(GEMINI_LPCIO_BASE),
    467 		.pd_size = _S(L1_S_SIZE),
    468 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    469 		.pd_cache = PTE_NOCACHE
    470 	},
    471 
    472 	/* Timers */
    473 	{
    474 		.pd_va = _A(GEMINI_TIMER_VBASE),
    475 		.pd_pa = _A(GEMINI_TIMER_BASE),
    476 		.pd_size = _S(L1_S_SIZE),
    477 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    478 		.pd_cache = PTE_NOCACHE
    479 	},
    480 
    481 	/* DRAM Controller */
    482 	{
    483 		.pd_va = _A(GEMINI_DRAMC_VBASE),
    484 		.pd_pa = _A(GEMINI_DRAMC_BASE),
    485 		.pd_size = _S(L1_S_SIZE),
    486 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    487 		.pd_cache = PTE_NOCACHE
    488 	},
    489 
    490 #if defined(MEMORY_DISK_DYNAMIC)
    491 	/* Ramdisk */
    492 	{
    493 		.pd_va = _A(GEMINI_RAMDISK_VBASE),
    494 		.pd_pa = _A(GEMINI_RAMDISK_PBASE),
    495 		.pd_size = _S(GEMINI_RAMDISK_SIZE),
    496 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,
    497 		.pd_cache = PTE_NOCACHE
    498 	},
    499 #endif
    500 
    501 	{0}	/* list terminator */
    502 };
    503 
    504 #undef	_A
    505 #undef	_S
    506 
    507 #ifdef DDB
    508 static void gemini_db_trap(int where)
    509 {
    510 #if  NGEMINIWDT > 0
    511 	static int oldwatchdogstate;
    512 
    513 	if (where) {
    514 		oldwatchdogstate = geminiwdt_enable(0);
    515 	} else {
    516 		geminiwdt_enable(oldwatchdogstate);
    517 	}
    518 #endif
    519 }
    520 #endif
    521 
    522 #if defined(VERBOSE_INIT_ARM) || 1
    523 void gemini_putchar(char c);
    524 void
    525 gemini_putchar(char c)
    526 {
    527 	unsigned char *com0addr = (unsigned char *)GEMINI_UART_VBASE;
    528 	int timo = 150000;
    529 
    530 	while ((com0addr[COM_REG_LSR * 4] & LSR_TXRDY) == 0)
    531 		if (--timo == 0)
    532 			break;
    533 
    534 	com0addr[COM_REG_TXDATA] = c;
    535 
    536 	while ((com0addr[COM_REG_LSR * 4] & LSR_TSRE) == 0)
    537 		if (--timo == 0)
    538 			break;
    539 }
    540 
    541 void gemini_puthex(unsigned int);
    542 void
    543 gemini_puthex(unsigned int val)
    544 {
    545 	char hexc[] = "0123456789abcdef";
    546 
    547 	gemini_putchar('0');
    548 	gemini_putchar('x');
    549 	gemini_putchar(hexc[(val >> 28) & 0xf]);
    550 	gemini_putchar(hexc[(val >> 24) & 0xf]);
    551 	gemini_putchar(hexc[(val >> 20) & 0xf]);
    552 	gemini_putchar(hexc[(val >> 16) & 0xf]);
    553 	gemini_putchar(hexc[(val >> 12) & 0xf]);
    554 	gemini_putchar(hexc[(val >> 8) & 0xf]);
    555 	gemini_putchar(hexc[(val >> 4) & 0xf]);
    556 	gemini_putchar(hexc[(val >> 0) & 0xf]);
    557 }
    558 #endif	/* VERBOSE_INIT_ARM */
    559 
    560 /*
    561  * u_int initarm(...)
    562  *
    563  * Initial entry point on startup. This gets called before main() is
    564  * entered.
    565  * It should be responsible for setting up everything that must be
    566  * in place when main is called.
    567  * This includes
    568  *   Taking a copy of the boot configuration structure.
    569  *   Initialising the physical console so characters can be printed.
    570  *   Setting up page tables for the kernel
    571  *   Relocating the kernel to the bottom of physical memory
    572  */
    573 u_int
    574 initarm(void *arg)
    575 {
    576 	GEMINI_PUTCHAR('0');
    577 
    578 	/*
    579 	 * start cpu#1 now
    580 	 */
    581 	gemini_cpu1_start();
    582 
    583 	/*
    584 	 * When we enter here, we are using a temporary first level
    585 	 * translation table with section entries in it to cover the OBIO
    586 	 * peripherals and SDRAM.  The temporary first level translation table
    587 	 * is at the end of SDRAM.
    588 	 */
    589 
    590 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    591 	GEMINI_PUTCHAR('1');
    592 	if (set_cpufuncs())
    593 		panic("cpu not recognized!");
    594 
    595 	GEMINI_PUTCHAR('2');
    596 	init_clocks();
    597 	GEMINI_PUTCHAR('3');
    598 
    599 	/* The console is going to try to map things.  Give pmap a devmap. */
    600 	pmap_devmap_register(devmap);
    601 	GEMINI_PUTCHAR('4');
    602 	consinit();
    603 	GEMINI_PUTCHAR('5');
    604 #ifdef KGDB
    605 	kgdb_port_init();
    606 #endif
    607 
    608 	/* Talk to the user */
    609 	printf("\nNetBSD/evbarm (gemini) booting ...\n");
    610 
    611 #ifdef BOOT_ARGS
    612 	char mi_bootargs[] = BOOT_ARGS;
    613 	parse_mi_bootargs(mi_bootargs);
    614 #endif
    615 
    616 #ifdef VERBOSE_INIT_ARM
    617 	printf("initarm: Configuring system ...\n");
    618 #endif
    619 
    620 	/*
    621 	 * Set up the variables that define the availability of physical
    622 	 * memory.
    623 	 */
    624 	gemini_memchk();
    625 	physical_start = GEMINI_DRAM_BASE;
    626 #define	MEMSIZE_BYTES 	(MEMSIZE * 1024 * 1024)
    627 	physical_end = (physical_start & ~(0x400000-1)) + MEMSIZE_BYTES;
    628 	physmem = (physical_end - physical_start) / PAGE_SIZE;
    629 
    630 	/* Fake bootconfig structure for the benefit of pmap.c. */
    631 	bootconfig.dramblocks = 1;
    632 	bootconfig.dram[0].address = physical_start;
    633 	bootconfig.dram[0].pages = physmem;
    634 
    635 	/*
    636 	 * Our kernel is at the beginning of memory, so set our free space to
    637 	 * all the memory after the kernel.
    638 	 */
    639 	physical_freestart = KERN_VTOPHYS(round_page((vaddr_t) _end));
    640 	physical_freeend = physical_end;
    641 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
    642 
    643 	/*
    644 	 * This is going to do all the hard work of setting up the first and
    645 	 * and second level page tables.  Pages of memory will be allocated
    646 	 * and mapped for other structures that are required for system
    647 	 * operation.  When it returns, physical_freestart and free_pages will
    648 	 * have been updated to reflect the allocations that were made.  In
    649 	 * addition, kernel_l1pt, kernel_pt_table[], systempage, irqstack,
    650 	 * abtstack, undstack, kernelstack, msgbufphys will be set to point to
    651 	 * the memory that was allocated for them.
    652 	 */
    653 	setup_real_page_tables();
    654 
    655 	/*
    656 	 * Moved from cpu_startup() as data_abort_handler() references
    657 	 * this during uvm init.
    658 	 */
    659 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
    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_FIQ32_MODE, fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
    680 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
    681 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
    682 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
    683 
    684 	/*
    685 	 * Well we should set a data abort handler.
    686 	 * Once things get going this will change as we will need a proper
    687 	 * handler.
    688 	 * Until then we will use a handler that just panics but tells us
    689 	 * why.
    690 	 * Initialisation of the vectors will just panic on a data abort.
    691 	 * This just fills in a slightly better one.
    692 	 */
    693 #ifdef VERBOSE_INIT_ARM
    694 	printf("vectors ");
    695 #endif
    696 	data_abort_handler_address = (u_int)data_abort_handler;
    697 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
    698 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
    699 
    700 	/* Initialise the undefined instruction handlers */
    701 #ifdef VERBOSE_INIT_ARM
    702 	printf("undefined ");
    703 #endif
    704 	undefined_init();
    705 
    706 	/* Load memory into UVM. */
    707 #ifdef VERBOSE_INIT_ARM
    708 	printf("page ");
    709 #endif
    710 	uvm_md_init();
    711 
    712 #if (GEMINI_RAM_RESV_PBASE != 0)
    713 	uvm_page_physload(atop(physical_freestart), atop(GEMINI_RAM_RESV_PBASE),
    714 	    atop(physical_freestart), atop(GEMINI_RAM_RESV_PBASE),
    715 	    VM_FREELIST_DEFAULT);
    716 	uvm_page_physload(atop(GEMINI_RAM_RESV_PEND), atop(physical_freeend),
    717 	    atop(GEMINI_RAM_RESV_PEND), atop(physical_freeend),
    718 	    VM_FREELIST_DEFAULT);
    719 #else
    720 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
    721 	    atop(physical_freestart), atop(physical_freeend),
    722 	    VM_FREELIST_DEFAULT);
    723 #endif
    724 	uvm_page_physload(atop(GEMINI_DRAM_BASE), atop(KERNEL_BASE_phys),
    725 	    atop(GEMINI_DRAM_BASE), atop(KERNEL_BASE_phys),
    726 	    VM_FREELIST_DEFAULT);
    727 
    728 	/* Boot strap pmap telling it where the kernel page table is */
    729 #ifdef VERBOSE_INIT_ARM
    730 	printf("pmap ");
    731 #endif
    732 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    733 
    734 #ifdef VERBOSE_INIT_ARM
    735 	printf("done.\n");
    736 #endif
    737 
    738 #ifdef IPKDB
    739 	/* Initialise ipkdb */
    740 	ipkdb_init();
    741 	if (boothowto & RB_KDB)
    742 		ipkdb_connect(0);
    743 #endif
    744 
    745 #if defined(MEMORY_DISK_DYNAMIC)
    746 	md_root_setconf((char *)GEMINI_RAMDISK_VBASE, GEMINI_RAMDISK_SIZE);
    747 #endif
    748 
    749 #ifdef KGDB
    750 	if (boothowto & RB_KDB) {
    751 		kgdb_debug_init = 1;
    752 		kgdb_connect(1);
    753 	}
    754 #endif
    755 
    756 #ifdef DDB
    757 	db_trap_callback = gemini_db_trap;
    758 	db_machine_init();
    759 
    760 	/* Firmware doesn't load symbols. */
    761 	ddb_init(0, NULL, NULL);
    762 
    763 	if (boothowto & RB_KDB)
    764 		Debugger();
    765 #endif
    766 	printf("initarm done.\n");
    767 
    768 	/* We return the new stack pointer address */
    769 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
    770 }
    771 
    772 static void
    773 init_clocks(void)
    774 {
    775 }
    776 
    777 #ifndef CONSADDR
    778 #error Specify the address of the console UART with the CONSADDR option.
    779 #endif
    780 #ifndef CONSPEED
    781 #define CONSPEED 19200
    782 #endif
    783 #ifndef CONMODE
    784 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    785 #endif
    786 
    787 static const bus_addr_t consaddr = CONSADDR;
    788 static const int conspeed = CONSPEED;
    789 static const int conmode = CONMODE;
    790 
    791 #if CONSADDR==0x42000000
    792 /*
    793  * console initialization for obio com console
    794  */
    795 void
    796 consinit(void)
    797 {
    798 	static int consinit_called = 0;
    799 
    800 	if (consinit_called != 0)
    801 		return;
    802 	consinit_called = 1;
    803 
    804 	if (comcnattach(&gemini_a4x_bs_tag, consaddr, conspeed,
    805 		GEMINI_COM_FREQ, COM_TYPE_16550_NOERS, conmode))
    806 			panic("Serial console can not be initialized.");
    807 }
    808 
    809 #elif CONSADDR==0x478003f8
    810 # include <arm/gemini/gemini_lpcvar.h>
    811 /*
    812  * console initialization for lpc com console
    813  */
    814 void
    815 consinit(void)
    816 {
    817 	static int consinit_called = 0;
    818 	bus_space_tag_t iot = &gemini_bs_tag;
    819 	bus_space_handle_t lpchc_ioh;
    820 	bus_space_handle_t lpcio_ioh;
    821 	bus_size_t sz = L1_S_SIZE;
    822 	gemini_lpc_softc_t lpcsoftc;
    823 	gemini_lpc_bus_ops_t *ops;
    824 	void *lpctag = &lpcsoftc;
    825 	uint32_t r;
    826 	extern gemini_lpc_bus_ops_t gemini_lpc_bus_ops;
    827 
    828 	ops = &gemini_lpc_bus_ops;
    829 
    830 	if (consinit_called != 0)
    831 		return;
    832 	consinit_called = 1;
    833 
    834 	if (bus_space_map(iot, GEMINI_LPCHC_BASE, sz, 0, &lpchc_ioh))
    835 		panic("consinit: LPCHC can not be mapped.");
    836 
    837 	if (bus_space_map(iot, GEMINI_LPCIO_BASE, sz, 0, &lpcio_ioh))
    838 		panic("consinit: LPCIO can not be mapped.");
    839 
    840 	/* enable the LPC bus */
    841 	r = bus_space_read_4(iot, lpchc_ioh, GEMINI_LPCHC_CSR);
    842 	r |= LPCHC_CSR_BEN;
    843 	bus_space_write_4(iot, lpchc_ioh, GEMINI_LPCHC_CSR, r);
    844 
    845 	memset(&lpcsoftc, 0, sizeof(lpcsoftc));
    846 	lpcsoftc.sc_iot = iot;
    847 	lpcsoftc.sc_ioh = lpcio_ioh;
    848 
    849 	/* activate Serial Port 1 */
    850 	(*ops->lpc_pnp_enter)(lpctag);
    851 	(*ops->lpc_pnp_write)(lpctag, 1, 0x30, 0x01);
    852 	(*ops->lpc_pnp_exit)(lpctag);
    853 
    854 	if (comcnattach(iot, consaddr, conspeed,
    855 		IT8712F_COM_FREQ, COM_TYPE_NORMAL, conmode)) {
    856 			panic("Serial console can not be initialized.");
    857 	}
    858 
    859 	bus_space_unmap(iot, lpcio_ioh, sz);
    860 	bus_space_unmap(iot, lpchc_ioh, sz);
    861 }
    862 #else
    863 # error unknown console
    864 #endif
    865 
    866 #ifdef KGDB
    867 #ifndef KGDB_DEVADDR
    868 #error Specify the address of the kgdb UART with the KGDB_DEVADDR option.
    869 #endif
    870 #ifndef KGDB_DEVRATE
    871 #define KGDB_DEVRATE 19200
    872 #endif
    873 
    874 #ifndef KGDB_DEVMODE
    875 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    876 #endif
    877 static const vaddr_t comkgdbaddr = KGDB_DEVADDR;
    878 static const int comkgdbspeed = KGDB_DEVRATE;
    879 static const int comkgdbmode = KGDB_DEVMODE;
    880 
    881 void
    882 static kgdb_port_init(void)
    883 {
    884 	static int kgdbsinit_called = 0;
    885 
    886 	if (kgdbsinit_called != 0)
    887 		return;
    888 
    889 	kgdbsinit_called = 1;
    890 
    891 	bus_space_handle_t bh;
    892 	if (bus_space_map(&gemini_a4x_bs_tag, comkgdbaddr,
    893 		GEMINI_UART_SIZE, 0, &bh))
    894 			panic("kgdb port can not be mapped.");
    895 
    896 	if (com_kgdb_attach(&gemini_a4x_bs_tag, comkgdbaddr, comkgdbspeed,
    897 		GEMINI_UART_SIZE, COM_TYPE_16550_NOERS, comkgdbmode))
    898 			panic("KGDB uart can not be initialized.");
    899 
    900 	bus_space_unmap(&gemini_a4x_bs_tag, bh, GEMINI_UART_SIZE);
    901 }
    902 #endif
    903 
    904 static void
    905 setup_real_page_tables(void)
    906 {
    907 	/*
    908 	 * We need to allocate some fixed page tables to get the kernel going.
    909 	 *
    910 	 * We are going to allocate our bootstrap pages from the beginning of
    911 	 * the free space that we just calculated.  We allocate one page
    912 	 * directory and a number of page tables and store the physical
    913 	 * addresses in the kernel_pt_table array.
    914 	 *
    915 	 * The kernel page directory must be on a 16K boundary.  The page
    916 	 * tables must be on 4K boundaries.  What we do is allocate the
    917 	 * page directory on the first 16K boundary that we encounter, and
    918 	 * the page tables on 4K boundaries otherwise.  Since we allocate
    919 	 * at least 3 L2 page tables, we are guaranteed to encounter at
    920 	 * least one 16K aligned region.
    921 	 */
    922 
    923 #ifdef VERBOSE_INIT_ARM
    924 	printf("Allocating page tables\n");
    925 #endif
    926 
    927 	/*
    928 	 * Define a macro to simplify memory allocation.  As we allocate the
    929 	 * memory, make sure that we don't walk over our temporary first level
    930 	 * translation table.
    931 	 */
    932 #define valloc_pages(var, np)						\
    933 	(var).pv_pa = physical_freestart;				\
    934 	physical_freestart += ((np) * PAGE_SIZE);			\
    935 	if (physical_freestart > (physical_freeend - L1_TABLE_SIZE))	\
    936 		panic("initarm: out of memory");			\
    937 	free_pages -= (np);						\
    938 	(var).pv_va = KERN_PHYSTOV((var).pv_pa);			\
    939 	memset((char *)(var).pv_va, 0, ((np) * PAGE_SIZE));
    940 
    941 	int loop, pt_index;
    942 
    943 	pt_index = 0;
    944 	kernel_l1pt.pv_pa = 0;
    945 	kernel_l1pt.pv_va = 0;
    946 #ifdef VERBOSE_INIT_ARM
    947 	printf("%s: physical_freestart %#lx\n", __func__, physical_freestart);
    948 #endif
    949 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
    950 		/* Are we 16KB aligned for an L1 ? */
    951 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
    952 		    && kernel_l1pt.pv_pa == 0) {
    953 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
    954 		} else {
    955 			valloc_pages(kernel_pt_table[pt_index],
    956 			    L2_TABLE_SIZE / PAGE_SIZE);
    957 			++pt_index;
    958 		}
    959 	}
    960 
    961 #if (NGEMINIIPM > 0)
    962 	valloc_pages(ipmq_pt, L2_TABLE_SIZE / PAGE_SIZE);
    963 #endif
    964 
    965 #ifdef VERBOSE_INIT_ARM
    966 	pt_index=0;
    967 	printf("%s: kernel_l1pt: %#lx:%#lx\n",
    968 		__func__, kernel_l1pt.pv_va, kernel_l1pt.pv_pa);
    969 	printf("%s: kernel_pt_table:\n", __func__);
    970 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
    971 		printf("\t%#lx:%#lx\n", kernel_pt_table[pt_index].pv_va,
    972 			kernel_pt_table[pt_index].pv_pa);
    973 		++pt_index;
    974 	}
    975 #if (NGEMINIIPM > 0)
    976 	printf("%s: ipmq_pt:\n", __func__);
    977 	printf("\t%#lx:%#lx\n", ipmq_pt.pv_va, ipmq_pt.pv_pa);
    978 #endif
    979 #endif
    980 
    981 	/* This should never be able to happen but better confirm that. */
    982 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
    983 		panic("initarm: Failed to align the kernel page directory");
    984 
    985 	/*
    986 	 * Allocate a page for the system page mapped to V0x00000000
    987 	 * This page will just contain the system vectors and can be
    988 	 * shared by all processes.
    989 	 */
    990 	valloc_pages(systempage, 1);
    991 	systempage.pv_va = ARM_VECTORS_HIGH;
    992 
    993 	/* Allocate stacks for all modes */
    994 	valloc_pages(fiqstack, FIQ_STACK_SIZE);
    995 	valloc_pages(irqstack, IRQ_STACK_SIZE);
    996 	valloc_pages(abtstack, ABT_STACK_SIZE);
    997 	valloc_pages(undstack, UND_STACK_SIZE);
    998 	valloc_pages(kernelstack, UPAGES);
    999 
   1000 	/* Allocate the message buffer. */
   1001 	pv_addr_t msgbuf;
   1002 	int msgbuf_pgs = round_page(MSGBUFSIZE) / PAGE_SIZE;
   1003 	valloc_pages(msgbuf, msgbuf_pgs);
   1004 	msgbufphys = msgbuf.pv_pa;
   1005 
   1006 	/*
   1007 	 * Ok we have allocated physical pages for the primary kernel
   1008 	 * page tables
   1009 	 */
   1010 
   1011 #ifdef VERBOSE_INIT_ARM
   1012 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
   1013 #endif
   1014 
   1015 	/*
   1016 	 * Now we start construction of the L1 page table
   1017 	 * We start by mapping the L2 page tables into the L1.
   1018 	 * This means that we can replace L1 mappings later on if necessary
   1019 	 */
   1020 	vaddr_t l1_va = kernel_l1pt.pv_va;
   1021 	paddr_t l1_pa = kernel_l1pt.pv_pa;
   1022 
   1023 	/* Map the L2 pages tables in the L1 page table */
   1024 	pmap_link_l2pt(l1_va, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
   1025 		       &kernel_pt_table[KERNEL_PT_SYS]);
   1026 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
   1027 		pmap_link_l2pt(l1_va, KERNEL_BASE + loop * 0x00400000,
   1028 			       &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
   1029 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
   1030 		pmap_link_l2pt(l1_va, KERNEL_VM_BASE + loop * 0x00400000,
   1031 			       &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
   1032 
   1033 	/* update the top of the kernel VM */
   1034 	pmap_curmaxkvaddr =
   1035 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
   1036 
   1037 #if (NGEMINIIPM > 0)
   1038 printf("%s:%d: pmap_link_l2pt ipmq_pt\n", __FUNCTION__, __LINE__);
   1039 	pmap_link_l2pt(l1_va, GEMINI_IPMQ_VBASE, &ipmq_pt);
   1040 #endif
   1041 
   1042 #ifdef VERBOSE_INIT_ARM
   1043 	printf("Mapping kernel\n");
   1044 #endif
   1045 
   1046 	/* Now we fill in the L2 pagetable for the kernel static code/data */
   1047 #define round_L_page(x) (((x) + L2_L_OFFSET) & L2_L_FRAME)
   1048 	size_t textsize = round_L_page(etext - KERNEL_BASE_virt);
   1049 	size_t totalsize = round_L_page(_end - KERNEL_BASE_virt);
   1050 	/* offset of kernel in RAM */
   1051 	u_int offset = (u_int)KERNEL_BASE_virt - KERNEL_BASE;
   1052 
   1053 #ifdef DDB
   1054 	/* Map text section read-write. */
   1055 	offset += pmap_map_chunk(l1_va,
   1056 				(vaddr_t)KERNEL_BASE + offset,
   1057 				 physical_start + offset, textsize,
   1058 				 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE,
   1059 				 PTE_CACHE);
   1060 #else
   1061 	/* Map text section read-only. */
   1062 	offset += pmap_map_chunk(l1_va,
   1063 				(vaddr_t)KERNEL_BASE + offset,
   1064 				 physical_start + offset, textsize,
   1065 				 VM_PROT_READ|VM_PROT_EXECUTE, PTE_CACHE);
   1066 #endif
   1067 	/* Map data and bss sections read-write. */
   1068 	offset += pmap_map_chunk(l1_va,
   1069 				(vaddr_t)KERNEL_BASE + offset,
   1070 				 physical_start + offset, totalsize - textsize,
   1071 				 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1072 
   1073 #ifdef VERBOSE_INIT_ARM
   1074 	printf("Constructing L2 page tables\n");
   1075 #endif
   1076 
   1077 	/* Map the stack pages */
   1078 	pmap_map_chunk(l1_va, fiqstack.pv_va, fiqstack.pv_pa,
   1079 	    FIQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1080 	pmap_map_chunk(l1_va, irqstack.pv_va, irqstack.pv_pa,
   1081 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1082 	pmap_map_chunk(l1_va, abtstack.pv_va, abtstack.pv_pa,
   1083 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1084 	pmap_map_chunk(l1_va, undstack.pv_va, undstack.pv_pa,
   1085 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1086 	pmap_map_chunk(l1_va, kernelstack.pv_va, kernelstack.pv_pa,
   1087 	    UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
   1088 
   1089 	pmap_map_chunk(l1_va, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
   1090 	    L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
   1091 
   1092 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
   1093 		pmap_map_chunk(l1_va, kernel_pt_table[loop].pv_va,
   1094 			       kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
   1095 			       VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
   1096 	}
   1097 
   1098 	/* Map the vector page. */
   1099 	pmap_map_entry(l1_va, ARM_VECTORS_HIGH, systempage.pv_pa,
   1100 		       VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1101 
   1102 #if (NGEMINIIPM > 0)
   1103 	/* Map the IPM queue l2pt */
   1104 	pmap_map_chunk(l1_va, ipmq_pt.pv_va, ipmq_pt.pv_pa,
   1105 		L2_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
   1106 
   1107 	/* Map the IPM queue pages */
   1108 	pmap_map_chunk(l1_va, GEMINI_IPMQ_VBASE, GEMINI_IPMQ_PBASE,
   1109 	    GEMINI_IPMQ_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
   1110 
   1111 #ifdef GEMINI_SLAVE
   1112 	/*
   1113 	 * Map all memory, incluuding that owned by other core
   1114 	 * take into account the RAM remap, so view in this region
   1115 	 * is consistent with MASTER
   1116 	 */
   1117 	pmap_map_chunk(l1_va,
   1118 	    GEMINI_ALLMEM_VBASE,
   1119 	    GEMINI_ALLMEM_PBASE + ((GEMINI_ALLMEM_SIZE - MEMSIZE) * 1024 * 1024),
   1120 	    (GEMINI_ALLMEM_SIZE - MEMSIZE) * 1024 * 1024,
   1121 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1122 	pmap_map_chunk(l1_va,
   1123 	    GEMINI_ALLMEM_VBASE + GEMINI_BUSBASE * 1024 * 1024,
   1124 	    GEMINI_ALLMEM_PBASE,
   1125 	    (MEMSIZE * 1024 * 1024),
   1126 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1127 #else
   1128 	/* Map all memory, incluuding that owned by other core */
   1129 	pmap_map_chunk(l1_va, GEMINI_ALLMEM_VBASE, GEMINI_ALLMEM_PBASE,
   1130 	    GEMINI_ALLMEM_SIZE * 1024 * 1024, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
   1131 #endif	/* GEMINI_SLAVE */
   1132 #endif	/* NGEMINIIPM */
   1133 
   1134 	/*
   1135 	 * Map integrated peripherals at same address in first level page
   1136 	 * table so that we can continue to use console.
   1137 	 */
   1138 	pmap_devmap_bootstrap(l1_va, devmap);
   1139 
   1140 
   1141 #ifdef VERBOSE_INIT_ARM
   1142 	/* Tell the user about where all the bits and pieces live. */
   1143 	printf("%22s       Physical              Virtual        Num\n", " ");
   1144 	printf("%22s Starting    Ending    Starting    Ending   Pages\n", " ");
   1145 
   1146 	static const char mem_fmt[] =
   1147 	    "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %d\n";
   1148 	static const char mem_fmt_nov[] =
   1149 	    "%20s: 0x%08lx 0x%08lx                       %d\n";
   1150 
   1151 	printf(mem_fmt, "SDRAM", physical_start, physical_end-1,
   1152 	    KERN_PHYSTOV(physical_start), KERN_PHYSTOV(physical_end-1),
   1153 	    (int)physmem);
   1154 	printf(mem_fmt, "text section",
   1155 	       KERN_VTOPHYS(KERNEL_BASE_virt), KERN_VTOPHYS(etext-1),
   1156 	       (vaddr_t)KERNEL_BASE_virt, (vaddr_t)etext-1,
   1157 	       (int)(textsize / PAGE_SIZE));
   1158 	printf(mem_fmt, "data section",
   1159 	       KERN_VTOPHYS(__data_start), KERN_VTOPHYS(_edata),
   1160 	       (vaddr_t)__data_start, (vaddr_t)_edata,
   1161 	       (int)((round_page((vaddr_t)_edata)
   1162 		      - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
   1163 	printf(mem_fmt, "bss section",
   1164 	       KERN_VTOPHYS(__bss_start), KERN_VTOPHYS(__bss_end__),
   1165 	       (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
   1166 	       (int)((round_page((vaddr_t)__bss_end__)
   1167 		      - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
   1168 	printf(mem_fmt, "L1 page directory",
   1169 	    kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
   1170 	    kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
   1171 	    L1_TABLE_SIZE / PAGE_SIZE);
   1172 	printf(mem_fmt, "Exception Vectors",
   1173 	    systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
   1174 	    (vaddr_t)ARM_VECTORS_HIGH, (vaddr_t)ARM_VECTORS_HIGH + PAGE_SIZE - 1,
   1175 	    1);
   1176 	printf(mem_fmt, "FIQ stack",
   1177 	    fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
   1178 	    fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
   1179 	    FIQ_STACK_SIZE);
   1180 	printf(mem_fmt, "IRQ stack",
   1181 	    irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
   1182 	    irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
   1183 	    IRQ_STACK_SIZE);
   1184 	printf(mem_fmt, "ABT stack",
   1185 	    abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
   1186 	    abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
   1187 	    ABT_STACK_SIZE);
   1188 	printf(mem_fmt, "UND stack",
   1189 	    undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
   1190 	    undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
   1191 	    UND_STACK_SIZE);
   1192 	printf(mem_fmt, "SVC stack",
   1193 	    kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
   1194 	    kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
   1195 	    UPAGES);
   1196 	printf(mem_fmt_nov, "Message Buffer",
   1197 	    msgbufphys, msgbufphys + msgbuf_pgs * PAGE_SIZE - 1, msgbuf_pgs);
   1198 	printf(mem_fmt, "Free Memory", physical_freestart, physical_freeend-1,
   1199 	    KERN_PHYSTOV(physical_freestart), KERN_PHYSTOV(physical_freeend-1),
   1200 	    free_pages);
   1201 #endif
   1202 
   1203 	/*
   1204 	 * Now we have the real page tables in place so we can switch to them.
   1205 	 * Once this is done we will be running with the REAL kernel page
   1206 	 * tables.
   1207 	 */
   1208 
   1209 	/* Switch tables */
   1210 #ifdef VERBOSE_INIT_ARM
   1211 	printf("switching to new L1 page table  @%#lx...", l1_pa);
   1212 #endif
   1213 
   1214 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
   1215 	cpu_setttb(l1_pa, true);
   1216 	cpu_tlb_flushID();
   1217 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
   1218 
   1219 #ifdef VERBOSE_INIT_ARM
   1220 	printf("OK.\n");
   1221 #endif
   1222 }
   1223