Home | History | Annotate | Line # | Download | only in rmixl
machdep.c revision 1.1.2.6
      1 /*	$NetBSD: machdep.c,v 1.1.2.6 2009/11/09 09:55:11 cliff Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge 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) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR 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  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department, The Mach Operating System project at
     80  * Carnegie-Mellon University and Ralph Campbell.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by the University of
     93  *	California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
    111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.1.2.6 2009/11/09 09:55:11 cliff Exp $");
    116 
    117 #include "opt_ddb.h"
    118 #include "opt_com.h"
    119 #include "opt_execfmt.h"
    120 #include "opt_memsize.h"
    121 
    122 #include <sys/param.h>
    123 #include <sys/systm.h>
    124 #include <sys/kernel.h>
    125 #include <sys/buf.h>
    126 #include <sys/reboot.h>
    127 #include <sys/user.h>
    128 #include <sys/mount.h>
    129 #include <sys/kcore.h>
    130 #include <sys/boot_flag.h>
    131 #include <sys/termios.h>
    132 #include <sys/ksyms.h>
    133 #include <sys/bus.h>
    134 #include <sys/device.h>
    135 #include <sys/extent.h>
    136 #include <sys/malloc.h>
    137 
    138 #include <uvm/uvm_extern.h>
    139 
    140 #include <dev/cons.h>
    141 
    142 #include "ksyms.h"
    143 
    144 #if NKSYMS || defined(DDB) || defined(LKM)
    145 #include <machine/db_machdep.h>
    146 #include <ddb/db_extern.h>
    147 #endif
    148 
    149 #include <machine/cpu.h>
    150 #include <machine/psl.h>
    151 
    152 #include "com.h"
    153 #if NCOM == 0
    154 #error no serial console
    155 #endif
    156 
    157 #include <dev/ic/comreg.h>
    158 #include <dev/ic/comvar.h>
    159 
    160 #include <mips/rmi/rmixl_comvar.h>
    161 #include <mips/rmi/rmixlvar.h>
    162 #include <mips/rmi/rmixl_firmware.h>
    163 #include <mips/rmi/rmixlreg.h>
    164 
    165 #ifdef MACHDEP_DEBUG
    166 int machdep_debug=MACHDEP_DEBUG;
    167 # define DPRINTF(x)	do { if (machdep_debug) printf x ; } while(0)
    168 #else
    169 # define DPRINTF(x)
    170 #endif
    171 
    172 #ifndef CONSFREQ
    173 # define CONSFREQ -1		/* inherit from firmware */
    174 #endif
    175 #ifndef CONSPEED
    176 # define CONSPEED 38400
    177 #endif
    178 #ifndef CONMODE
    179 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    180 #endif
    181 #ifndef CONSADDR
    182 # define CONSADDR RMIXL_IO_DEV_UART_1
    183 #endif
    184 
    185 int		comcnfreq  = CONSFREQ;
    186 int		comcnspeed = CONSPEED;
    187 tcflag_t	comcnmode  = CONMODE;
    188 bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    189 
    190 struct rmixl_config rmixl_configuration;
    191 
    192 
    193 /*
    194  * array of tested firmware versions
    195  * if you find new ones and they work
    196  * please add them
    197  */
    198 static uint64_t rmiclfw_psb_versions[] = {
    199 	0x4958d4fb00000056,
    200 	0x49a5a8fa00000056,
    201 };
    202 #define RMICLFW_PSB_VERSIONS_LEN \
    203 	(sizeof(rmiclfw_psb_versions)/sizeof(rmiclfw_psb_versions[0]))
    204 
    205 /*
    206  * kernel copies of firmware info
    207  */
    208 static rmixlfw_info_t rmixlfw_info;
    209 static rmixlfw_mmap_t rmixlfw_phys_mmap;
    210 static rmixlfw_mmap_t rmixlfw_avail_mmap;
    211 #define RMIXLFW_INFOP_LEGAL	0x8c000000
    212 
    213 
    214 /*
    215  * storage for fixed extent used to allocate physical address regions
    216  * because extent(9) start and end values are u_long, they are only
    217  * 32 bits on a 32 bit kernel, which is insuffucuent since XLS physical
    218  * address is 40 bits wide.  So the "physaddr" map stores regions
    219  * in units of megabytes.
    220  */
    221 static u_long rmixl_physaddr_storage[
    222 	EXTENT_FIXED_STORAGE_SIZE(32)/sizeof(u_long)
    223 ];
    224 
    225 /* For sysctl_hw. */
    226 extern char cpu_model[];
    227 
    228 /* Our exported CPU info; we can have only one. */
    229 struct cpu_info cpu_info_store;
    230 
    231 /* Maps for VM objects. */
    232 struct vm_map *mb_map = NULL;
    233 struct vm_map *phys_map = NULL;
    234 
    235 int	physmem;		/* Total physical memory */
    236 
    237 int	netboot;		/* Are we netbooting? */
    238 
    239 
    240 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    241 u_int mem_cluster_cnt;
    242 
    243 
    244 void configure(void);
    245 void mach_init(int, int32_t *, void *, void *);
    246 static u_long rmixlfw_init(void *);
    247 static u_long mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
    248 static void __attribute__((__noreturn__)) rmixl_exit(int);
    249 static void rmixl_physaddr_init(void);
    250 static u_int ram_seg_resv(phys_ram_seg_t *, u_int, u_quad_t, u_quad_t);
    251 void rmixlfw_mmap_print(rmixlfw_mmap_t *);
    252 
    253 
    254 /*
    255  * safepri is a safe priority for sleep to set for a spin-wait during
    256  * autoconfiguration or after a panic.  Used as an argument to splx().
    257  */
    258 int	safepri = MIPS1_PSL_LOWIPL;
    259 
    260 extern struct user *proc0paddr;
    261 
    262 /*
    263  * Do all the stuff that locore normally does before calling main().
    264  */
    265 void
    266 mach_init(int argc, int32_t *argv, void *envp, void *infop)
    267 {
    268 	struct rmixl_config *rcp = &rmixl_configuration;
    269 	void *kernend, *v;
    270 	u_long memsize;
    271 	u_int vm_cluster_cnt;
    272 	phys_ram_seg_t vm_clusters[VM_PHYSSEG_MAX];
    273 	extern char edata[], end[];
    274 
    275 	/*
    276 	 * Clear the BSS segment.
    277 	 */
    278 	kernend = (void *)mips_round_page(end);
    279 	memset(edata, 0, (char *)kernend - edata);
    280 
    281 	/*
    282 	 * Set up the exception vectors and CPU-specific function
    283 	 * vectors early on.  We need the wbflush() vector set up
    284 	 * before comcnattach() is called (or at least before the
    285 	 * first printf() after that is called).
    286 	 * Also clears the I+D caches.
    287 	 */
    288 	mips_vector_init();
    289 
    290 	memsize = rmixlfw_init(infop);
    291 
    292 	/* set the VM page size */
    293 	uvm_setpagesize();
    294 
    295 	physmem = btoc(memsize);
    296 
    297 	rmixl_eb_bus_mem_init(&rcp->rc_eb_memt, rcp); /* need for console */
    298 	rmixl_el_bus_mem_init(&rcp->rc_el_memt, rcp); /* XXX defer ? */
    299 
    300 #if NCOM > 0
    301 	rmixl_com_cnattach(comcnaddr, comcnspeed, -1,
    302 		COM_TYPE_NORMAL, comcnmode);
    303 #endif
    304 
    305 	printf("\nNetBSD/rmixl\n");
    306 	printf("memsize = %#lx\n", memsize);
    307 
    308 	rmixl_physaddr_init();
    309 
    310 	/*
    311 	 * Obtain the cpu frequency
    312 	 * Compute the number of ticks for hz.
    313 	 * Compute the delay divisor.
    314 	 * Double the Hz if this CPU runs at twice the
    315          *  external/cp0-count frequency
    316 	 */
    317 	curcpu()->ci_cpu_freq = rmixlfw_info.cpu_frequency;
    318 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    319 	curcpu()->ci_divisor_delay =
    320 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    321         if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    322 		curcpu()->ci_cpu_freq *= 2;
    323 
    324 	/*
    325 	 * Look at arguments passed to us and compute boothowto.
    326 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    327 	 *   by forcing sign extension in cast to (char *)
    328 	 */
    329 	boothowto = RB_AUTOBOOT;
    330 	for (int i = 1; i < argc; i++) {
    331 		for (char *cp = (char *)(uint64_t)argv[i]; *cp; cp++) {
    332 			int howto;
    333 			/* Ignore superfluous '-', if there is one */
    334 			if (*cp == '-')
    335 				continue;
    336 
    337 			howto = 0;
    338 			BOOT_FLAG(*cp, howto);
    339 			if (howto != 0)
    340 				boothowto |= howto;
    341 #ifdef DIAGNOSTIC
    342 			else
    343 				printf("bootflag '%c' not recognised\n", *cp);
    344 #endif
    345 		}
    346 	}
    347 #ifdef DIAGNOSTIC
    348 	printf("boothowto %#x\n", boothowto);
    349 #endif
    350 
    351 	/*
    352 	 * Reserve pages from the VM system.
    353 	 * to maintain mem_clusters[] as a map of raw ram,
    354 	 * copy into temporary table vm_clusters[]
    355 	 * work on that and use it to feed vm_physload()
    356 	 */
    357 	KASSERT(sizeof(mem_clusters) == sizeof(vm_clusters));
    358 	memcpy(&vm_clusters, &mem_clusters, sizeof(vm_clusters));
    359 	vm_cluster_cnt = mem_cluster_cnt;
    360 
    361 	/* reserve 0..start..kernend pages */
    362 	vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
    363 		0, round_page(MIPS_KSEG0_TO_PHYS(kernend)));
    364 
    365 	/* reserve reset exception vector page */
    366 	vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
    367 		MIPS_KSEG1_TO_PHYS(MIPS_RESET_EXC_VEC),
    368 		MIPS_KSEG1_TO_PHYS(MIPS_RESET_EXC_VEC+NBPG));
    369 
    370 	/*
    371 	 * Load vm_clusters[] into the VM system.
    372 	 */
    373 	for (u_int i=0; i < vm_cluster_cnt; i++) {
    374 		u_quad_t first, last;
    375 
    376 		first = trunc_page(vm_clusters[i].start);
    377 		last = round_page(vm_clusters[i].start + vm_clusters[i].size);
    378 		DPRINTF(("%s: %d: %#lx, %#lx\n", __func__, i, first, last));
    379 		uvm_page_physload(atop(first), atop(last), atop(first),
    380 			atop(last), VM_FREELIST_DEFAULT);
    381 	}
    382 
    383 	/*
    384 	 * Initialize error message buffer (at end of core).
    385 	 */
    386 	mips_init_msgbuf();
    387 
    388 	pmap_bootstrap();
    389 
    390 	/*
    391 	 * Allocate space for proc0's USPACE.
    392 	 */
    393 	v = (void *)uvm_pageboot_alloc(USPACE);
    394 	lwp0.l_addr = proc0paddr = (struct user *)v;
    395 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    396 #ifdef _LP64
    397         lwp0.l_md.md_regs->f_regs[_R_SR] = MIPS_SR_KX;
    398 #endif
    399         proc0paddr->u_pcb.pcb_context.val[_L_SR] =
    400 #ifdef _LP64
    401             MIPS_SR_KX |
    402 #endif
    403             MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    404 
    405 
    406 	/*
    407 	 * Initialize debuggers, and break into them, if appropriate.
    408 	 */
    409 #if NKSYMS || defined(DDB) || defined(LKM)
    410 	ksyms_init(0, 0, 0);
    411 #endif
    412 
    413 #if defined(DDB)
    414 	if (boothowto & RB_KDB)
    415 		Debugger();
    416 #endif
    417 }
    418 
    419 
    420 /*
    421  * ram_seg_resv - cut reserved regions out of segs, fragmenting as needed
    422  *
    423  * we simply build a new table of segs, then copy it back over the given one
    424  * this is inefficient but simple and called only a few times
    425  *
    426  * note: 'last' here means 1st addr past the end of the segment (start+size)
    427  */
    428 static u_int
    429 ram_seg_resv(phys_ram_seg_t *segs, u_int nsegs,
    430 	u_quad_t resv_first, u_quad_t resv_last)
    431 {
    432         u_quad_t first, last;
    433 	int new_nsegs=0;
    434 	int resv_flag;
    435 	phys_ram_seg_t new_segs[VM_PHYSSEG_MAX];
    436 
    437 	for (u_int i=0; i < nsegs; i++) {
    438 		resv_flag = 0;
    439 		first = trunc_page(segs[i].start);
    440 		last = round_page(segs[i].start + segs[i].size);
    441 
    442 		KASSERT(new_nsegs < VM_PHYSSEG_MAX);
    443 		if ((resv_first <= first) && (resv_last >= last)) {
    444 			/* whole segment is resverved */
    445 			continue;
    446 		}
    447 		if ((resv_first > first) && (resv_first < last)) {
    448 			u_quad_t new_last;
    449 
    450 			/*
    451 			 * reserved start in segment
    452 			 * salvage the leading fragment
    453 			 */
    454 			resv_flag = 1;
    455 			new_last = last - (last - resv_first);
    456 			KASSERT (new_last > first);
    457 			new_segs[new_nsegs].start = first;
    458 			new_segs[new_nsegs].size = new_last - first;
    459 			new_nsegs++;
    460 		}
    461 		if ((resv_last > first) && (resv_last < last)) {
    462 			u_quad_t new_first;
    463 
    464 			/*
    465 			 * reserved end in segment
    466 			 * salvage the trailing fragment
    467 			 */
    468 			resv_flag = 1;
    469 			new_first = first + (resv_last - first);
    470 			KASSERT (last > (new_first + NBPG));
    471 			new_segs[new_nsegs].start = new_first;
    472 			new_segs[new_nsegs].size = last - new_first;
    473 			new_nsegs++;
    474 		}
    475 		if (resv_flag == 0) {
    476 			/*
    477 			 * nothing reserved here, take it all
    478 			 */
    479 			new_segs[new_nsegs].start = first;
    480 			new_segs[new_nsegs].size = last - first;
    481 			new_nsegs++;
    482 		}
    483 
    484 	}
    485 
    486 	memcpy(segs, new_segs, sizeof(new_segs));
    487 
    488 	return new_nsegs;
    489 }
    490 
    491 /*
    492  * create an extent for physical address space
    493  * these are in units of MB for sake of compression (for sake of 32 bit kernels)
    494  * allocate the regions where we have known functions (DRAM, IO, etc)
    495  * what remains can be allocated as needed for other stuff
    496  * e.g. to configure BARs that are not already initialized and enabled.
    497  */
    498 static void
    499 rmixl_physaddr_init(void)
    500 {
    501 	struct extent *ext;
    502 	unsigned long start = 0UL;
    503 	unsigned long end = (__BIT(40) / (1024 * 1024)) -1;
    504 	u_long base;
    505 	u_long size;
    506 	uint32_t r;
    507 
    508 	ext = extent_create("physaddr", start, end, M_DEVBUF,
    509 		(void *)rmixl_physaddr_storage, sizeof(rmixl_physaddr_storage),
    510 		EX_NOWAIT | EX_NOCOALESCE);
    511 
    512 	if (ext == NULL)
    513 		panic("%s: extent_create failed", __func__);
    514 
    515 	/*
    516 	 * grab regions per DRAM BARs
    517 	 */
    518 	for (u_int i=0; i < RMIXL_SBC_DRAM_NBARS; i++) {
    519 		r = RMIXL_IOREG_READ(RMIXL_SBC_DRAM_BAR(i));
    520 		if ((r & RMIXL_DRAM_BAR_STATUS) == 0)
    521 			continue;	/* not enabled */
    522 		base = (u_long)(DRAM_BAR_TO_BASE((uint64_t)r) / (1024 * 1024));
    523 		size = (u_long)(DRAM_BAR_TO_SIZE((uint64_t)r) / (1024 * 1024));
    524 
    525 		DPRINTF(("%s: %d: %d: 0x%08x -- 0x%010lx:%lu MB\n",
    526 			__func__, __LINE__, i, r, base * (1024 * 1024), size));
    527 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    528 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    529 				"failed", __func__, ext, base, size, EX_NOWAIT);
    530 	}
    531 
    532 	/*
    533 	 * grab regions per PCIe CFG, ECFG, IO, MEM BARs
    534 	 */
    535 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_CFG_BAR);
    536 	if ((r & RMIXL_PCIE_CFG_BAR_ENB) != 0) {
    537 		base = (u_long)(RMIXL_PCIE_CFG_BAR_TO_BA((uint64_t)r)
    538 			/ (1024 * 1024));
    539 		size = (u_long)RMIXL_PCIE_CFG_SIZE / (1024 * 1024);
    540 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    541 			__LINE__, "CFG", r, base * 1024 * 1024, size));
    542 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    543 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    544 				"failed", __func__, ext, base, size, EX_NOWAIT);
    545 	}
    546 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_ECFG_BAR);
    547 	if ((r & RMIXL_PCIE_ECFG_BAR_ENB) != 0) {
    548 		base = (u_long)(RMIXL_PCIE_ECFG_BAR_TO_BA((uint64_t)r)
    549 			/ (1024 * 1024));
    550 		size = (u_long)RMIXL_PCIE_ECFG_SIZE / (1024 * 1024);
    551 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    552 			__LINE__, "ECFG", r, base * 1024 * 1024, size));
    553 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    554 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    555 				"failed", __func__, ext, base, size, EX_NOWAIT);
    556 	}
    557 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_MEM_BAR);
    558 	if ((r & RMIXL_PCIE_MEM_BAR_ENB) != 0) {
    559 		base = (u_long)(RMIXL_PCIE_MEM_BAR_TO_BA((uint64_t)r)
    560 			/ (1024 * 1024));
    561 		size = (u_long)(RMIXL_PCIE_MEM_BAR_TO_SIZE((uint64_t)r)
    562 			/ (1024 * 1024));
    563 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    564 			__LINE__, "MEM", r, base * 1024 * 1024, size));
    565 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    566 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    567 				"failed", __func__, ext, base, size, EX_NOWAIT);
    568 	}
    569 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_IO_BAR);
    570 	if ((r & RMIXL_PCIE_IO_BAR_ENB) != 0) {
    571 		base = (u_long)(RMIXL_PCIE_IO_BAR_TO_BA((uint64_t)r)
    572 			/ (1024 * 1024));
    573 		size = (u_long)(RMIXL_PCIE_IO_BAR_TO_SIZE((uint64_t)r)
    574 			/ (1024 * 1024));
    575 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    576 			__LINE__, "IO", r, base * 1024 * 1024, size));
    577 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    578 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    579 				"failed", __func__, ext, base, size, EX_NOWAIT);
    580 	}
    581 
    582 	/*
    583 	 *  at this point all regions left in "physaddr" extent
    584 	 *  are unused holes in the physical adress space
    585 	 *  available for use as needed.
    586 	 */
    587 	rmixl_configuration.rc_phys_ex = ext;
    588 #ifdef MACHDEP_DEBUG
    589 	extent_print(ext);
    590 #endif
    591 }
    592 
    593 static u_long
    594 rmixlfw_init(void *infop)
    595 {
    596 	struct rmixl_config *rcp = &rmixl_configuration;
    597 	uint64_t tmp;
    598 
    599 	strcpy(cpu_model, "RMI XLS616ATX VIIA");	/* XXX */
    600 
    601 	tmp = (int64_t)infop;
    602 	tmp |= 0xffffffffULL << 32;
    603 	infop = (void *)tmp;
    604 	rmixlfw_info = *(rmixlfw_info_t *)infop;
    605 
    606 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    607 		if (rmiclfw_psb_versions[i] == rmixlfw_info.psb_version)
    608 			goto found;
    609 	}
    610 
    611 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(RMIXL_IO_DEV_PBASE);
    612 	rmixl_putchar_init(rcp->rc_io_pbase);
    613 
    614 #ifdef DIAGNOSTIC
    615 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    616 	rmixl_puthex64(rmixlfw_info.psb_version);
    617 	rmixl_puts("\r\n");
    618 #endif
    619 
    620  found:
    621 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rmixlfw_info.io_base);
    622 	rmixl_putchar_init(rcp->rc_io_pbase);
    623 #ifdef MACHDEP_DEBUG
    624 	rmixl_puts("\r\ninfop: ");
    625 	rmixl_puthex64((uint64_t)infop);
    626 #endif
    627 #ifdef DIAGNOSTIC
    628 	rmixl_puts("\r\nrecognized psb_version: ");
    629 	rmixl_puthex64(rmixlfw_info.psb_version);
    630 	rmixl_puts("\r\n");
    631 #endif
    632 
    633 	return mem_clusters_init(
    634 		(rmixlfw_mmap_t *)rmixlfw_info.psb_physaddr_map,
    635 		(rmixlfw_mmap_t *)rmixlfw_info.avail_mem_map);
    636 }
    637 
    638 void
    639 rmixlfw_mmap_print(rmixlfw_mmap_t *map)
    640 {
    641 #ifdef MACHDEP_DEBUG
    642 	for (uint32_t i=0; i < map->nmmaps; i++) {
    643 		rmixl_puthex32(i);
    644 		rmixl_puts(", ");
    645 		rmixl_puthex64(map->entry[i].start);
    646 		rmixl_puts(", ");
    647 		rmixl_puthex64(map->entry[i].size);
    648 		rmixl_puts(", ");
    649 		rmixl_puthex32(map->entry[i].type);
    650 		rmixl_puts("\r\n");
    651 	}
    652 #endif
    653 }
    654 
    655 /*
    656  * mem_clusters_init
    657  *
    658  * initialize mem_clusters[] table based on memory address mapping
    659  * provided by boot firmware.
    660  *
    661  * prefer avail_mem_map if we can, otherwise use psb_physaddr_map.
    662  * these will be limited by MEMSIZE if it is configured.
    663  * if neither are available, just use MEMSIZE.
    664  */
    665 static u_long
    666 mem_clusters_init(
    667 	rmixlfw_mmap_t *psb_physaddr_map,
    668 	rmixlfw_mmap_t *avail_mem_map)
    669 {
    670 	rmixlfw_mmap_t *map = NULL;
    671 	const char *mapname;
    672 	uint64_t tmp;
    673 	uint64_t sz;
    674 	uint64_t sum;
    675 	u_int cnt;
    676 #ifdef MEMSIZE
    677 	u_long memsize = MEMSIZE;
    678 #endif
    679 
    680 #ifdef MACHDEP_DEBUG
    681 	rmixl_puts("psb_physaddr_map: ");
    682 	rmixl_puthex64((uint64_t)psb_physaddr_map);
    683 	rmixl_puts("\r\n");
    684 #endif
    685 	if (psb_physaddr_map != NULL) {
    686 		rmixlfw_phys_mmap = *psb_physaddr_map;
    687 		map = &rmixlfw_phys_mmap;
    688 		mapname = "psb_physaddr_map";
    689 		rmixlfw_mmap_print(map);
    690 	}
    691 #ifdef DIAGNOSTIC
    692 	else {
    693 		rmixl_puts("WARNING: no psb_physaddr_map\r\n");
    694 	}
    695 #endif
    696 
    697 #ifdef MACHDEP_DEBUG
    698 	rmixl_puts("avail_mem_map: ");
    699 	rmixl_puthex64((uint64_t)avail_mem_map);
    700 	rmixl_puts("\r\n");
    701 #endif
    702 	if (avail_mem_map != NULL) {
    703 		rmixlfw_avail_mmap = *avail_mem_map;
    704 		map = &rmixlfw_avail_mmap;
    705 		mapname = "avail_mem_map";
    706 		rmixlfw_mmap_print(map);
    707 	}
    708 #ifdef DIAGNOSTIC
    709 	else {
    710 		rmixl_puts("WARNING: no avail_mem_map\r\n");
    711 	}
    712 #endif
    713 
    714 	if (map == NULL) {
    715 #ifndef MEMSIZE
    716 		rmixl_puts("panic: no firmware memory map, "
    717 			"must configure MEMSIZE\r\n");
    718 		for(;;);	/* XXX */
    719 #else
    720 #ifdef DIAGNOSTIC
    721 		rmixl_puts("WARNING: no avail_mem_map, "
    722 			"using MEMSIZE\r\n");
    723 #endif
    724 
    725 		mem_clusters[0].start = 0;
    726 		mem_clusters[0].size = MEMSIZE;
    727 		mem_cluster_cnt = 1;
    728 		return MEMSIZE;
    729 #endif	/* MEMSIZE */
    730 	}
    731 
    732 #ifdef DIAGNOSTIC
    733 	rmixl_puts("using ");
    734 	rmixl_puts(mapname);
    735 	rmixl_puts("\r\n");
    736 #endif
    737 #ifdef MACHDEP_DEBUG
    738 	rmixl_puts("memory clusters:\r\n");
    739 #endif
    740 	sum = 0;
    741 	cnt = 0;
    742 	for (uint32_t i=0; i < map->nmmaps; i++) {
    743 		if (map->entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    744 			continue;
    745 		mem_clusters[cnt].start = map->entry[i].start;
    746 		sz = map->entry[i].size;
    747 		sum += sz;
    748 		mem_clusters[cnt].size = sz;
    749 #ifdef MACHDEP_DEBUG
    750 		rmixl_puthex32(i);
    751 		rmixl_puts(": ");
    752 		rmixl_puthex64(mem_clusters[cnt].start);
    753 		rmixl_puts(", ");
    754 		rmixl_puthex64(sz);
    755 		rmixl_puts(": ");
    756 		rmixl_puthex64(sum);
    757 		rmixl_puts("\r\n");
    758 #endif
    759 #ifdef MEMSIZE
    760 		/*
    761 		 * configurably limit memsize
    762 		 */
    763 		if (sum == memsize)
    764 			break;
    765 		if (sum > memsize) {
    766 			tmp = sum - memsize;
    767 			sz -= tmp;
    768 			sum -= tmp;
    769 			mem_clusters[cnt].size = sz;
    770 			break;
    771 		}
    772 #endif
    773 		cnt++;
    774 	}
    775 	mem_cluster_cnt = cnt;
    776 	return sum;
    777 }
    778 
    779 void
    780 consinit(void)
    781 {
    782 
    783 	/*
    784 	 * Everything related to console initialization is done
    785 	 * in mach_init().
    786 	 */
    787 }
    788 
    789 /*
    790  * Allocate memory for variable-sized tables,
    791  */
    792 void
    793 cpu_startup()
    794 {
    795 	vaddr_t minaddr, maxaddr;
    796 	char pbuf[9];
    797 
    798 	/*
    799 	 * Good {morning,afternoon,evening,night}.
    800 	 */
    801 	printf("%s%s", copyright, version);
    802 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    803 	printf("total memory = %s\n", pbuf);
    804 
    805 	/*
    806 	 * Virtual memory is bootstrapped -- notify the bus spaces
    807 	 * that memory allocation is now safe.
    808 	 */
    809 	rmixl_configuration.rc_mallocsafe = 1;
    810 
    811 	minaddr = 0;
    812 	/*
    813 	 * Allocate a submap for physio.
    814 	 */
    815 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    816 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    817 
    818 	/*
    819 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    820 	 * are allocated via the pool allocator, and we use XKSEG to
    821 	 * map those pages.)
    822 	 */
    823 
    824 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    825 	printf("avail memory = %s\n", pbuf);
    826 }
    827 
    828 int	waittime = -1;
    829 
    830 void
    831 cpu_reboot(howto, bootstr)
    832 	int howto;
    833 	char *bootstr;
    834 {
    835 
    836 	/* Take a snapshot before clobbering any registers. */
    837 	if (curproc)
    838 		savectx((struct user *)curpcb);
    839 
    840 	if (cold) {
    841 		howto |= RB_HALT;
    842 		goto haltsys;
    843 	}
    844 
    845 	/* If "always halt" was specified as a boot flag, obey. */
    846 	if (boothowto & RB_HALT)
    847 		howto |= RB_HALT;
    848 
    849 	boothowto = howto;
    850 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    851 		waittime = 0;
    852 		vfs_shutdown();
    853 
    854 		/*
    855 		 * If we've been adjusting the clock, the todr
    856 		 * will be out of synch; adjust it now.
    857 		 */
    858 		resettodr();
    859 	}
    860 
    861 	splhigh();
    862 
    863 	if (howto & RB_DUMP)
    864 		dumpsys();
    865 
    866 haltsys:
    867 	doshutdownhooks();
    868 
    869 	if (howto & RB_HALT) {
    870 		printf("\n");
    871 		printf("The operating system has halted.\n");
    872 		printf("Please press any key to reboot.\n\n");
    873 		cnpollc(1);	/* For proper keyboard command handling */
    874 		cngetc();
    875 		cnpollc(0);
    876 	}
    877 
    878 	printf("rebooting...\n\n");
    879 
    880 	rmixl_exit(0);
    881 }
    882 
    883 /*
    884  * goodbye world
    885  */
    886 #define GPIO_CPU_RST 0xa0			/* XXX TMP */
    887 void __attribute__((__noreturn__))
    888 rmixl_exit(int howto)
    889 {
    890 	/* use firmware callbak to reboot */
    891 	void (*reset)(void) = (void *)rmixlfw_info.warm_reset;
    892 	if (reset != 0) {
    893 		(*reset)();
    894 		printf("warm reset callback failed, spinning...\n");
    895 	} else {
    896 		printf("warm reset callback absent, spinning...\n");
    897 	}
    898 	for (;;);
    899 }
    900