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