Home | History | Annotate | Line # | Download | only in rmixl
machdep.c revision 1.2
      1 /*	$NetBSD: machdep.c,v 1.2 2009/12/14 00:46:03 matt 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.2 2009/12/14 00:46:03 matt 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 #define MACHDEP_DEBUG 1
    166 #ifdef MACHDEP_DEBUG
    167 int machdep_debug=MACHDEP_DEBUG;
    168 # define DPRINTF(x)	do { if (machdep_debug) printf x ; } while(0)
    169 #else
    170 # define DPRINTF(x)
    171 #endif
    172 
    173 #ifndef CONSFREQ
    174 # define CONSFREQ -1		/* inherit from firmware */
    175 #endif
    176 #ifndef CONSPEED
    177 # define CONSPEED 38400
    178 #endif
    179 #ifndef CONMODE
    180 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    181 #endif
    182 #ifndef CONSADDR
    183 # define CONSADDR RMIXL_IO_DEV_UART_1
    184 #endif
    185 
    186 int		comcnfreq  = CONSFREQ;
    187 int		comcnspeed = CONSPEED;
    188 tcflag_t	comcnmode  = CONMODE;
    189 bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    190 
    191 struct rmixl_config rmixl_configuration;
    192 
    193 
    194 /*
    195  * array of tested firmware versions
    196  * if you find new ones and they work
    197  * please add them
    198  */
    199 static uint64_t rmiclfw_psb_versions[] = {
    200 	0x4958d4fb00000056ULL,
    201 	0x49a5a8fa00000056ULL,
    202 	0x4aacdb6a00000056ULL,
    203 };
    204 #define RMICLFW_PSB_VERSIONS_LEN \
    205 	(sizeof(rmiclfw_psb_versions)/sizeof(rmiclfw_psb_versions[0]))
    206 
    207 /*
    208  * kernel copies of firmware info
    209  */
    210 static rmixlfw_info_t rmixlfw_info;
    211 static rmixlfw_mmap_t rmixlfw_phys_mmap;
    212 static rmixlfw_mmap_t rmixlfw_avail_mmap;
    213 #define RMIXLFW_INFOP_LEGAL	0x8c000000
    214 
    215 
    216 /*
    217  * storage for fixed extent used to allocate physical address regions
    218  * because extent(9) start and end values are u_long, they are only
    219  * 32 bits on a 32 bit kernel, which is insuffucuent since XLS physical
    220  * address is 40 bits wide.  So the "physaddr" map stores regions
    221  * in units of megabytes.
    222  */
    223 static u_long rmixl_physaddr_storage[
    224 	EXTENT_FIXED_STORAGE_SIZE(32)/sizeof(u_long)
    225 ];
    226 
    227 /* For sysctl_hw. */
    228 extern char cpu_model[];
    229 
    230 /* Our exported CPU info; we can have only one. */
    231 struct cpu_info cpu_info_store;
    232 
    233 /* Maps for VM objects. */
    234 struct vm_map *mb_map = NULL;
    235 struct vm_map *phys_map = NULL;
    236 
    237 int	physmem;		/* Total physical memory */
    238 
    239 int	netboot;		/* Are we netbooting? */
    240 
    241 
    242 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    243 u_int mem_cluster_cnt;
    244 
    245 
    246 void configure(void);
    247 void mach_init(int, int32_t *, void *, int64_t);
    248 static u_long rmixlfw_init(int64_t);
    249 static u_long mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
    250 static void __attribute__((__noreturn__)) rmixl_exit(int);
    251 static void rmixl_physaddr_init(void);
    252 static u_int ram_seg_resv(phys_ram_seg_t *, u_int, u_quad_t, u_quad_t);
    253 void rmixlfw_mmap_print(rmixlfw_mmap_t *);
    254 
    255 
    256 /*
    257  * safepri is a safe priority for sleep to set for a spin-wait during
    258  * autoconfiguration or after a panic.  Used as an argument to splx().
    259  */
    260 int	safepri = MIPS1_PSL_LOWIPL;
    261 
    262 extern struct user *proc0paddr;
    263 
    264 /*
    265  * Do all the stuff that locore normally does before calling main().
    266  */
    267 void
    268 mach_init(int argc, int32_t *argv, void *envp, int64_t infop)
    269 {
    270 	struct rmixl_config *rcp = &rmixl_configuration;
    271 	void *kernend, *v;
    272 	u_long memsize;
    273 	u_int vm_cluster_cnt;
    274 	uint32_t r;
    275 	phys_ram_seg_t vm_clusters[VM_PHYSSEG_MAX];
    276 	extern char edata[], end[];
    277 
    278 	rmixl_mtcr(0, 1);		/* disable all threads except #0 */
    279 
    280 	r = rmixl_mfcr(0x300);
    281 	r &= ~__BIT(14);		/* disabled Unaligned Access */
    282 	rmixl_mtcr(0x300, r);
    283 
    284 	rmixl_mtcr(0x400, 0);		/* enable MMU clock gating */
    285 					/* set single MMU Thread Mode */
    286 					/* TLB is partitioned (1 partition) */
    287 
    288 	/*
    289 	 * Clear the BSS segment.
    290 	 */
    291 	kernend = (void *)mips_round_page(end);
    292 	memset(edata, 0, (char *)kernend - edata);
    293 
    294 	/*
    295 	 * Set up the exception vectors and CPU-specific function
    296 	 * vectors early on.  We need the wbflush() vector set up
    297 	 * before comcnattach() is called (or at least before the
    298 	 * first printf() after that is called).
    299 	 * Also clears the I+D caches.
    300 	 */
    301 	mips_vector_init();
    302 
    303 	memsize = rmixlfw_init(infop);
    304 
    305 	/* set the VM page size */
    306 	uvm_setpagesize();
    307 
    308 	physmem = btoc(memsize);
    309 
    310 	rmixl_obio_bus_mem_init(&rcp->rc_obio_memt, rcp); /* need for console */
    311 
    312 #if NCOM > 0
    313 	rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
    314 		COM_TYPE_NORMAL, comcnmode);
    315 #endif
    316 
    317 	printf("\nNetBSD/rmixl\n");
    318 	printf("memsize = %#lx\n", memsize);
    319 
    320 	rmixl_physaddr_init();
    321 
    322 	/*
    323 	 * Obtain the cpu frequency
    324 	 * Compute the number of ticks for hz.
    325 	 * Compute the delay divisor.
    326 	 * Double the Hz if this CPU runs at twice the
    327          *  external/cp0-count frequency
    328 	 */
    329 	curcpu()->ci_cpu_freq = rmixlfw_info.cpu_frequency;
    330 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    331 	curcpu()->ci_divisor_delay =
    332 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    333         if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    334 		curcpu()->ci_cpu_freq *= 2;
    335 
    336 	/*
    337 	 * Look at arguments passed to us and compute boothowto.
    338 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    339 	 *   by forcing sign extension in cast to (char *)
    340 	 */
    341 	boothowto = RB_AUTOBOOT;
    342 	for (int i = 1; i < argc; i++) {
    343 		for (char *cp = (char *)(intptr_t)argv[i]; *cp; cp++) {
    344 			int howto;
    345 			/* Ignore superfluous '-', if there is one */
    346 			if (*cp == '-')
    347 				continue;
    348 
    349 			howto = 0;
    350 			BOOT_FLAG(*cp, howto);
    351 			if (howto != 0)
    352 				boothowto |= howto;
    353 #ifdef DIAGNOSTIC
    354 			else
    355 				printf("bootflag '%c' not recognised\n", *cp);
    356 #endif
    357 		}
    358 	}
    359 #ifdef DIAGNOSTIC
    360 	printf("boothowto %#x\n", boothowto);
    361 #endif
    362 
    363 	/*
    364 	 * Reserve pages from the VM system.
    365 	 * to maintain mem_clusters[] as a map of raw ram,
    366 	 * copy into temporary table vm_clusters[]
    367 	 * work on that and use it to feed vm_physload()
    368 	 */
    369 	KASSERT(sizeof(mem_clusters) == sizeof(vm_clusters));
    370 	memcpy(&vm_clusters, &mem_clusters, sizeof(vm_clusters));
    371 	vm_cluster_cnt = mem_cluster_cnt;
    372 
    373 	/* reserve 0..start..kernend pages */
    374 	vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
    375 		0, round_page(MIPS_KSEG0_TO_PHYS(kernend)));
    376 
    377 	/* reserve reset exception vector page */
    378 	/* should never be in our clusters anyway... */
    379 	vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
    380 		MIPS_RESET_EXC_VEC, MIPS_RESET_EXC_VEC+NBPG);
    381 
    382 	/*
    383 	 * Load vm_clusters[] into the VM system.
    384 	 */
    385 	for (u_int i=0; i < vm_cluster_cnt; i++) {
    386 		u_quad_t first, last;
    387 
    388 		first = trunc_page(vm_clusters[i].start);
    389 		last = round_page(vm_clusters[i].start + vm_clusters[i].size);
    390 		DPRINTF(("%s: %d: %#"PRIx64", %#"PRIx64"\n",
    391 			__func__, i, first, last));
    392 
    393 		uvm_page_physload(atop(first), atop(last), atop(first),
    394 			atop(last), VM_FREELIST_DEFAULT);
    395 	}
    396 
    397 	/*
    398 	 * Initialize error message buffer (at end of core).
    399 	 */
    400 	mips_init_msgbuf();
    401 
    402 	pmap_bootstrap();
    403 
    404 	/*
    405 	 * Allocate space for proc0's USPACE.
    406 	 */
    407 	v = (void *)uvm_pageboot_alloc(USPACE);
    408 	lwp0.l_addr = proc0paddr = (struct user *)v;
    409 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    410 #ifdef _LP64
    411         lwp0.l_md.md_regs->f_regs[_R_SR] = MIPS_SR_KX;
    412 #endif
    413         proc0paddr->u_pcb.pcb_context.val[_L_SR] =
    414 #ifdef _LP64
    415             MIPS_SR_KX |
    416 #endif
    417             MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    418 
    419 
    420 	/*
    421 	 * Initialize debuggers, and break into them, if appropriate.
    422 	 */
    423 #if NKSYMS || defined(DDB) || defined(LKM)
    424 	ksyms_init(0, 0, 0);
    425 #endif
    426 
    427 #if defined(DDB)
    428 	if (boothowto & RB_KDB)
    429 		Debugger();
    430 #endif
    431 }
    432 
    433 /*
    434  * ram_seg_resv - cut reserved regions out of segs, fragmenting as needed
    435  *
    436  * we simply build a new table of segs, then copy it back over the given one
    437  * this is inefficient but simple and called only a few times
    438  *
    439  * note: 'last' here means 1st addr past the end of the segment (start+size)
    440  */
    441 static u_int
    442 ram_seg_resv(phys_ram_seg_t *segs, u_int nsegs,
    443 	u_quad_t resv_first, u_quad_t resv_last)
    444 {
    445         u_quad_t first, last;
    446 	int new_nsegs=0;
    447 	int resv_flag;
    448 	phys_ram_seg_t new_segs[VM_PHYSSEG_MAX];
    449 
    450 	for (u_int i=0; i < nsegs; i++) {
    451 		resv_flag = 0;
    452 		first = trunc_page(segs[i].start);
    453 		last = round_page(segs[i].start + segs[i].size);
    454 
    455 		KASSERT(new_nsegs < VM_PHYSSEG_MAX);
    456 		if ((resv_first <= first) && (resv_last >= last)) {
    457 			/* whole segment is resverved */
    458 			continue;
    459 		}
    460 		if ((resv_first > first) && (resv_first < last)) {
    461 			u_quad_t new_last;
    462 
    463 			/*
    464 			 * reserved start in segment
    465 			 * salvage the leading fragment
    466 			 */
    467 			resv_flag = 1;
    468 			new_last = last - (last - resv_first);
    469 			KASSERT (new_last > first);
    470 			new_segs[new_nsegs].start = first;
    471 			new_segs[new_nsegs].size = new_last - first;
    472 			new_nsegs++;
    473 		}
    474 		if ((resv_last > first) && (resv_last < last)) {
    475 			u_quad_t new_first;
    476 
    477 			/*
    478 			 * reserved end in segment
    479 			 * salvage the trailing fragment
    480 			 */
    481 			resv_flag = 1;
    482 			new_first = first + (resv_last - first);
    483 			KASSERT (last > (new_first + NBPG));
    484 			new_segs[new_nsegs].start = new_first;
    485 			new_segs[new_nsegs].size = last - new_first;
    486 			new_nsegs++;
    487 		}
    488 		if (resv_flag == 0) {
    489 			/*
    490 			 * nothing reserved here, take it all
    491 			 */
    492 			new_segs[new_nsegs].start = first;
    493 			new_segs[new_nsegs].size = last - first;
    494 			new_nsegs++;
    495 		}
    496 
    497 	}
    498 
    499 	memcpy(segs, new_segs, sizeof(new_segs));
    500 
    501 	return new_nsegs;
    502 }
    503 
    504 /*
    505  * create an extent for physical address space
    506  * these are in units of MB for sake of compression (for sake of 32 bit kernels)
    507  * allocate the regions where we have known functions (DRAM, IO, etc)
    508  * what remains can be allocated as needed for other stuff
    509  * e.g. to configure BARs that are not already initialized and enabled.
    510  */
    511 static void
    512 rmixl_physaddr_init(void)
    513 {
    514 	struct extent *ext;
    515 	unsigned long start = 0UL;
    516 	unsigned long end = (__BIT(40) / (1024 * 1024)) -1;
    517 	u_long base;
    518 	u_long size;
    519 	uint32_t r;
    520 
    521 	ext = extent_create("physaddr", start, end, M_DEVBUF,
    522 		(void *)rmixl_physaddr_storage, sizeof(rmixl_physaddr_storage),
    523 		EX_NOWAIT | EX_NOCOALESCE);
    524 
    525 	if (ext == NULL)
    526 		panic("%s: extent_create failed", __func__);
    527 
    528 	/*
    529 	 * grab regions per DRAM BARs
    530 	 */
    531 	for (u_int i=0; i < RMIXL_SBC_DRAM_NBARS; i++) {
    532 		r = RMIXL_IOREG_READ(RMIXL_SBC_DRAM_BAR(i));
    533 		if ((r & RMIXL_DRAM_BAR_STATUS) == 0)
    534 			continue;	/* not enabled */
    535 		base = (u_long)(DRAM_BAR_TO_BASE((uint64_t)r) / (1024 * 1024));
    536 		size = (u_long)(DRAM_BAR_TO_SIZE((uint64_t)r) / (1024 * 1024));
    537 
    538 		DPRINTF(("%s: %d: %d: 0x%08x -- 0x%010lx:%lu MB\n",
    539 			__func__, __LINE__, i, r, base * (1024 * 1024), size));
    540 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    541 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    542 				"failed", __func__, ext, base, size, EX_NOWAIT);
    543 	}
    544 
    545 	/*
    546 	 * grab regions per PCIe CFG, ECFG, IO, MEM BARs
    547 	 */
    548 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_CFG_BAR);
    549 	if ((r & RMIXL_PCIE_CFG_BAR_ENB) != 0) {
    550 		base = (u_long)(RMIXL_PCIE_CFG_BAR_TO_BA((uint64_t)r)
    551 			/ (1024 * 1024));
    552 		size = (u_long)RMIXL_PCIE_CFG_SIZE / (1024 * 1024);
    553 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    554 			__LINE__, "CFG", r, base * 1024 * 1024, size));
    555 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    556 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    557 				"failed", __func__, ext, base, size, EX_NOWAIT);
    558 	}
    559 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_ECFG_BAR);
    560 	if ((r & RMIXL_PCIE_ECFG_BAR_ENB) != 0) {
    561 		base = (u_long)(RMIXL_PCIE_ECFG_BAR_TO_BA((uint64_t)r)
    562 			/ (1024 * 1024));
    563 		size = (u_long)RMIXL_PCIE_ECFG_SIZE / (1024 * 1024);
    564 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    565 			__LINE__, "ECFG", r, base * 1024 * 1024, size));
    566 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    567 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    568 				"failed", __func__, ext, base, size, EX_NOWAIT);
    569 	}
    570 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_MEM_BAR);
    571 	if ((r & RMIXL_PCIE_MEM_BAR_ENB) != 0) {
    572 		base = (u_long)(RMIXL_PCIE_MEM_BAR_TO_BA((uint64_t)r)
    573 			/ (1024 * 1024));
    574 		size = (u_long)(RMIXL_PCIE_MEM_BAR_TO_SIZE((uint64_t)r)
    575 			/ (1024 * 1024));
    576 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    577 			__LINE__, "MEM", r, base * 1024 * 1024, size));
    578 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    579 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    580 				"failed", __func__, ext, base, size, EX_NOWAIT);
    581 	}
    582 	r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_IO_BAR);
    583 	if ((r & RMIXL_PCIE_IO_BAR_ENB) != 0) {
    584 		base = (u_long)(RMIXL_PCIE_IO_BAR_TO_BA((uint64_t)r)
    585 			/ (1024 * 1024));
    586 		size = (u_long)(RMIXL_PCIE_IO_BAR_TO_SIZE((uint64_t)r)
    587 			/ (1024 * 1024));
    588 		DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
    589 			__LINE__, "IO", r, base * 1024 * 1024, size));
    590 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    591 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    592 				"failed", __func__, ext, base, size, EX_NOWAIT);
    593 	}
    594 
    595 	/*
    596 	 *  at this point all regions left in "physaddr" extent
    597 	 *  are unused holes in the physical adress space
    598 	 *  available for use as needed.
    599 	 */
    600 	rmixl_configuration.rc_phys_ex = ext;
    601 #ifdef MACHDEP_DEBUG
    602 	extent_print(ext);
    603 #endif
    604 }
    605 
    606 static u_long
    607 rmixlfw_init(int64_t infop)
    608 {
    609 	struct rmixl_config *rcp = &rmixl_configuration;
    610 
    611 	strcpy(cpu_model, "RMI XLS616ATX VIIA");	/* XXX */
    612 
    613 	infop |= MIPS_KSEG0_START;
    614 	rmixlfw_info = *(rmixlfw_info_t *)(intptr_t)infop;
    615 
    616 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    617 		if (rmiclfw_psb_versions[i] == rmixlfw_info.psb_version)
    618 			goto found;
    619 	}
    620 
    621 	rcp->rc_io_pbase = RMIXL_IO_DEV_PBASE;
    622 	rmixl_putchar_init(rcp->rc_io_pbase);
    623 
    624 #ifdef DIAGNOSTIC
    625 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    626 	rmixl_puthex64(rmixlfw_info.psb_version);
    627 	rmixl_puts("\r\n");
    628 #endif
    629 
    630 	/* XXX trust and use MEMSIZE */
    631 	mem_clusters[0].start = 0;
    632 	mem_clusters[0].size = MEMSIZE;
    633 	mem_cluster_cnt = 1;
    634 	return MEMSIZE;
    635 
    636  found:
    637 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rmixlfw_info.io_base);
    638 	rmixl_putchar_init(rcp->rc_io_pbase);
    639 #ifdef MACHDEP_DEBUG
    640 	rmixl_puts("\r\ninfop: ");
    641 	rmixl_puthex64((uint64_t)(intptr_t)infop);
    642 #endif
    643 #ifdef DIAGNOSTIC
    644 	rmixl_puts("\r\nrecognized psb_version: ");
    645 	rmixl_puthex64(rmixlfw_info.psb_version);
    646 	rmixl_puts("\r\n");
    647 #endif
    648 
    649 	return mem_clusters_init(
    650 		(rmixlfw_mmap_t *)(intptr_t)rmixlfw_info.psb_physaddr_map,
    651 		(rmixlfw_mmap_t *)(intptr_t)rmixlfw_info.avail_mem_map);
    652 }
    653 
    654 void
    655 rmixlfw_mmap_print(rmixlfw_mmap_t *map)
    656 {
    657 #ifdef MACHDEP_DEBUG
    658 	for (uint32_t i=0; i < map->nmmaps; i++) {
    659 		rmixl_puthex32(i);
    660 		rmixl_puts(", ");
    661 		rmixl_puthex64(map->entry[i].start);
    662 		rmixl_puts(", ");
    663 		rmixl_puthex64(map->entry[i].size);
    664 		rmixl_puts(", ");
    665 		rmixl_puthex32(map->entry[i].type);
    666 		rmixl_puts("\r\n");
    667 	}
    668 #endif
    669 }
    670 
    671 /*
    672  * mem_clusters_init
    673  *
    674  * initialize mem_clusters[] table based on memory address mapping
    675  * provided by boot firmware.
    676  *
    677  * prefer avail_mem_map if we can, otherwise use psb_physaddr_map.
    678  * these will be limited by MEMSIZE if it is configured.
    679  * if neither are available, just use MEMSIZE.
    680  */
    681 static u_long
    682 mem_clusters_init(
    683 	rmixlfw_mmap_t *psb_physaddr_map,
    684 	rmixlfw_mmap_t *avail_mem_map)
    685 {
    686 	rmixlfw_mmap_t *map = NULL;
    687 	const char *mapname;
    688 	uint64_t tmp;
    689 	uint64_t sz;
    690 	uint64_t sum;
    691 	u_int cnt;
    692 #ifdef MEMSIZE
    693 	u_long memsize = MEMSIZE;
    694 #endif
    695 
    696 #ifdef MACHDEP_DEBUG
    697 	rmixl_puts("psb_physaddr_map: ");
    698 	rmixl_puthex64((uint64_t)(intptr_t)psb_physaddr_map);
    699 	rmixl_puts("\r\n");
    700 #endif
    701 	if (psb_physaddr_map != NULL) {
    702 		rmixlfw_phys_mmap = *psb_physaddr_map;
    703 		map = &rmixlfw_phys_mmap;
    704 		mapname = "psb_physaddr_map";
    705 		rmixlfw_mmap_print(map);
    706 	}
    707 #ifdef DIAGNOSTIC
    708 	else {
    709 		rmixl_puts("WARNING: no psb_physaddr_map\r\n");
    710 	}
    711 #endif
    712 
    713 #ifdef MACHDEP_DEBUG
    714 	rmixl_puts("avail_mem_map: ");
    715 	rmixl_puthex64((uint64_t)(intptr_t)avail_mem_map);
    716 	rmixl_puts("\r\n");
    717 #endif
    718 	if (avail_mem_map != NULL) {
    719 		rmixlfw_avail_mmap = *avail_mem_map;
    720 		map = &rmixlfw_avail_mmap;
    721 		mapname = "avail_mem_map";
    722 		rmixlfw_mmap_print(map);
    723 	}
    724 #ifdef DIAGNOSTIC
    725 	else {
    726 		rmixl_puts("WARNING: no avail_mem_map\r\n");
    727 	}
    728 #endif
    729 
    730 	if (map == NULL) {
    731 #ifndef MEMSIZE
    732 		rmixl_puts("panic: no firmware memory map, "
    733 			"must configure MEMSIZE\r\n");
    734 		for(;;);	/* XXX */
    735 #else
    736 #ifdef DIAGNOSTIC
    737 		rmixl_puts("WARNING: no avail_mem_map, "
    738 			"using MEMSIZE\r\n");
    739 #endif
    740 
    741 		mem_clusters[0].start = 0;
    742 		mem_clusters[0].size = MEMSIZE;
    743 		mem_cluster_cnt = 1;
    744 		return MEMSIZE;
    745 #endif	/* MEMSIZE */
    746 	}
    747 
    748 #ifdef DIAGNOSTIC
    749 	rmixl_puts("using ");
    750 	rmixl_puts(mapname);
    751 	rmixl_puts("\r\n");
    752 #endif
    753 #ifdef MACHDEP_DEBUG
    754 	rmixl_puts("memory clusters:\r\n");
    755 #endif
    756 	sum = 0;
    757 	cnt = 0;
    758 	for (uint32_t i=0; i < map->nmmaps; i++) {
    759 		if (map->entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    760 			continue;
    761 		mem_clusters[cnt].start = map->entry[i].start;
    762 		sz = map->entry[i].size;
    763 		sum += sz;
    764 		mem_clusters[cnt].size = sz;
    765 #ifdef MACHDEP_DEBUG
    766 		rmixl_puthex32(i);
    767 		rmixl_puts(": ");
    768 		rmixl_puthex64(mem_clusters[cnt].start);
    769 		rmixl_puts(", ");
    770 		rmixl_puthex64(sz);
    771 		rmixl_puts(": ");
    772 		rmixl_puthex64(sum);
    773 		rmixl_puts("\r\n");
    774 #endif
    775 #ifdef MEMSIZE
    776 		/*
    777 		 * configurably limit memsize
    778 		 */
    779 		if (sum == memsize)
    780 			break;
    781 		if (sum > memsize) {
    782 			tmp = sum - memsize;
    783 			sz -= tmp;
    784 			sum -= tmp;
    785 			mem_clusters[cnt].size = sz;
    786 			break;
    787 		}
    788 #endif
    789 		cnt++;
    790 	}
    791 	mem_cluster_cnt = cnt;
    792 	return sum;
    793 }
    794 
    795 void
    796 consinit(void)
    797 {
    798 
    799 	/*
    800 	 * Everything related to console initialization is done
    801 	 * in mach_init().
    802 	 */
    803 }
    804 
    805 /*
    806  * Allocate memory for variable-sized tables,
    807  */
    808 void
    809 cpu_startup()
    810 {
    811 	vaddr_t minaddr, maxaddr;
    812 	char pbuf[9];
    813 
    814 	/*
    815 	 * Good {morning,afternoon,evening,night}.
    816 	 */
    817 	printf("%s%s", copyright, version);
    818 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    819 	printf("total memory = %s\n", pbuf);
    820 
    821 	/*
    822 	 * Virtual memory is bootstrapped -- notify the bus spaces
    823 	 * that memory allocation is now safe.
    824 	 */
    825 	rmixl_configuration.rc_mallocsafe = 1;
    826 
    827 	minaddr = 0;
    828 	/*
    829 	 * Allocate a submap for physio.
    830 	 */
    831 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    832 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    833 
    834 	/*
    835 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    836 	 * are allocated via the pool allocator, and we use XKSEG to
    837 	 * map those pages.)
    838 	 */
    839 
    840 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    841 	printf("avail memory = %s\n", pbuf);
    842 }
    843 
    844 int	waittime = -1;
    845 
    846 void
    847 cpu_reboot(howto, bootstr)
    848 	int howto;
    849 	char *bootstr;
    850 {
    851 
    852 	/* Take a snapshot before clobbering any registers. */
    853 	if (curproc)
    854 		savectx((struct user *)curpcb);
    855 
    856 	if (cold) {
    857 		howto |= RB_HALT;
    858 		goto haltsys;
    859 	}
    860 
    861 	/* If "always halt" was specified as a boot flag, obey. */
    862 	if (boothowto & RB_HALT)
    863 		howto |= RB_HALT;
    864 
    865 	boothowto = howto;
    866 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    867 		waittime = 0;
    868 		vfs_shutdown();
    869 
    870 		/*
    871 		 * If we've been adjusting the clock, the todr
    872 		 * will be out of synch; adjust it now.
    873 		 */
    874 		resettodr();
    875 	}
    876 
    877 	splhigh();
    878 
    879 	if (howto & RB_DUMP)
    880 		dumpsys();
    881 
    882 haltsys:
    883 	doshutdownhooks();
    884 
    885 	if (howto & RB_HALT) {
    886 		printf("\n");
    887 		printf("The operating system has halted.\n");
    888 		printf("Please press any key to reboot.\n\n");
    889 		cnpollc(1);	/* For proper keyboard command handling */
    890 		cngetc();
    891 		cnpollc(0);
    892 	}
    893 
    894 	printf("rebooting...\n\n");
    895 
    896 	rmixl_exit(0);
    897 }
    898 
    899 /*
    900  * goodbye world
    901  */
    902 #define GPIO_CPU_RST 0xa0			/* XXX TMP */
    903 void __attribute__((__noreturn__))
    904 rmixl_exit(int howto)
    905 {
    906 	/* use firmware callbak to reboot */
    907 	void (*reset)(void) = (void *)(intptr_t)rmixlfw_info.warm_reset;
    908 	if (reset != 0) {
    909 		(*reset)();
    910 		printf("warm reset callback failed, spinning...\n");
    911 	} else {
    912 		printf("warm reset callback absent, spinning...\n");
    913 	}
    914 	for (;;);
    915 }
    916