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