Home | History | Annotate | Line # | Download | only in rmixl
machdep.c revision 1.9.8.2
      1 /*	$NetBSD: machdep.c,v 1.9.8.2 2012/03/04 00:46:06 mrg 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) 1988 University of Utah.
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department, The Mach Operating System project at
     46  * Carnegie-Mellon University and Ralph Campbell.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     73  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.9.8.2 2012/03/04 00:46:06 mrg Exp $");
     78 
     79 #define __INTR_PRIVATE
     80 
     81 #include "opt_multiprocessor.h"
     82 #include "opt_ddb.h"
     83 #include "opt_com.h"
     84 #include "opt_execfmt.h"
     85 #include "opt_memsize.h"
     86 #include "rmixl_pcix.h"
     87 #include "rmixl_pcie.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/systm.h>
     91 #include <sys/kernel.h>
     92 #include <sys/buf.h>
     93 #include <sys/reboot.h>
     94 #include <sys/mount.h>
     95 #include <sys/kcore.h>
     96 #include <sys/boot_flag.h>
     97 #include <sys/termios.h>
     98 #include <sys/ksyms.h>
     99 #include <sys/bus.h>
    100 #include <sys/device.h>
    101 #include <sys/extent.h>
    102 #include <sys/malloc.h>
    103 
    104 #include <uvm/uvm_extern.h>
    105 
    106 #include <dev/cons.h>
    107 
    108 #include "ksyms.h"
    109 
    110 #if NKSYMS || defined(DDB) || defined(LKM)
    111 #include <mips/db_machdep.h>
    112 #include <ddb/db_extern.h>
    113 #endif
    114 
    115 #include <mips/cpu.h>
    116 #include <mips/psl.h>
    117 #include <mips/cache.h>
    118 #include <mips/mips_opcode.h>
    119 
    120 #include "com.h"
    121 #if NCOM == 0
    122 #error no serial console
    123 #endif
    124 
    125 #include <dev/ic/comreg.h>
    126 #include <dev/ic/comvar.h>
    127 
    128 #include <mips/include/intr.h>
    129 
    130 #include <mips/rmi/rmixlreg.h>
    131 #include <mips/rmi/rmixlvar.h>
    132 #include <mips/rmi/rmixl_intr.h>
    133 #include <mips/rmi/rmixl_firmware.h>
    134 #include <mips/rmi/rmixl_comvar.h>
    135 #include <mips/rmi/rmixl_pcievar.h>
    136 #include <mips/rmi/rmixl_pcixvar.h>
    137 
    138 #ifdef MACHDEP_DEBUG
    139 int machdep_debug=MACHDEP_DEBUG;
    140 # define DPRINTF(x)	do { if (machdep_debug) printf x ; } while(0)
    141 #else
    142 # define DPRINTF(x)
    143 #endif
    144 
    145 #ifndef CONSFREQ
    146 # define CONSFREQ 66000000
    147 #endif
    148 #ifndef CONSPEED
    149 # define CONSPEED 38400
    150 #endif
    151 #ifndef CONMODE
    152 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    153 #endif
    154 #ifndef CONSADDR
    155 # define CONSADDR RMIXL_IO_DEV_UART_1
    156 #endif
    157 
    158 int		comcnfreq  = CONSFREQ;
    159 int		comcnspeed = CONSPEED;
    160 tcflag_t	comcnmode  = CONMODE;
    161 bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    162 
    163 struct rmixl_config rmixl_configuration;
    164 
    165 
    166 /*
    167  * array of tested firmware versions
    168  * if you find new ones and they work
    169  * please add them
    170  */
    171 typedef struct rmiclfw_psb_id {
    172 	uint64_t		psb_version;
    173 	rmixlfw_psb_type_t	psb_type;
    174 } rmiclfw_psb_id_t;
    175 static rmiclfw_psb_id_t rmiclfw_psb_id[] = {
    176 	{	0x4958d4fb00000056ULL, PSB_TYPE_RMI  },
    177 	{	0x4aacdb6a00000056ULL, PSB_TYPE_RMI  },
    178 	{	0x4b67d03200000056ULL, PSB_TYPE_RMI  },
    179 	{	0x4c17058b00000056ULL, PSB_TYPE_RMI  },
    180 	{	0x49a5a8fa00000056ULL, PSB_TYPE_DELL },
    181 	{	0x4b8ead3100000056ULL, PSB_TYPE_DELL },
    182 };
    183 #define RMICLFW_PSB_VERSIONS_LEN \
    184 	(sizeof(rmiclfw_psb_id)/sizeof(rmiclfw_psb_id[0]))
    185 
    186 /*
    187  * storage for fixed extent used to allocate physical address regions
    188  * because extent(9) start and end values are u_long, they are only
    189  * 32 bits on a 32 bit kernel, which is insuffucuent since XLS physical
    190  * address is 40 bits wide.  So the "physaddr" map stores regions
    191  * in units of megabytes.
    192  */
    193 static u_long rmixl_physaddr_storage[
    194 	EXTENT_FIXED_STORAGE_SIZE(32)/sizeof(u_long)
    195 ];
    196 
    197 /* For sysctl_hw. */
    198 extern char cpu_model[];
    199 
    200 /* Maps for VM objects. */
    201 struct vm_map *phys_map = NULL;
    202 
    203 int	netboot;		/* Are we netbooting? */
    204 
    205 
    206 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    207 u_quad_t mem_cluster_maxaddr;
    208 u_int mem_cluster_cnt;
    209 
    210 
    211 void configure(void);
    212 void mach_init(int, int32_t *, void *, int64_t);
    213 static uint64_t rmixlfw_init(int64_t);
    214 static uint64_t mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
    215 static void __attribute__((__noreturn__)) rmixl_reset(void);
    216 static void rmixl_physaddr_init(void);
    217 static u_int ram_seg_resv(phys_ram_seg_t *, u_int, u_quad_t, u_quad_t);
    218 void rmixlfw_mmap_print(rmixlfw_mmap_t *);
    219 
    220 
    221 #ifdef MULTIPROCESSOR
    222 static bool rmixl_fixup_cop0_oscratch(int32_t, uint32_t [2]);
    223 void rmixl_get_wakeup_info(struct rmixl_config *);
    224 #ifdef MACHDEP_DEBUG
    225 static void rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *);
    226 #endif	/* MACHDEP_DEBUG */
    227 #endif	/* MULTIPROCESSOR */
    228 static void rmixl_fixup_curcpu(void);
    229 
    230 /*
    231  * Do all the stuff that locore normally does before calling main().
    232  */
    233 void
    234 mach_init(int argc, int32_t *argv, void *envp, int64_t infop)
    235 {
    236 	struct rmixl_config *rcp = &rmixl_configuration;
    237 	void *kernend;
    238 	uint64_t memsize;
    239 	extern char edata[], end[];
    240 
    241 	rmixl_pcr_init_core();
    242 
    243 	/*
    244 	 * Clear the BSS segment.
    245 	 */
    246 	kernend = (void *)mips_round_page(end);
    247 	memset(edata, 0, (char *)kernend - edata);
    248 
    249 	/*
    250 	 * Set up the exception vectors and CPU-specific function
    251 	 * vectors early on.  We need the wbflush() vector set up
    252 	 * before comcnattach() is called (or at least before the
    253 	 * first printf() after that is called).
    254 	 * Also clears the I+D caches.
    255 	 *
    256 	 * specify chip-specific EIRR/EIMR based spl functions
    257 	 */
    258 #ifdef MULTIPROCESSOR
    259 	mips_vector_init(&rmixl_splsw, true);
    260 #else
    261 	mips_vector_init(&rmixl_splsw, false);
    262 #endif
    263 
    264 	/* mips_vector_init initialized mips_options */
    265 	strcpy(cpu_model, mips_options.mips_cpu->cpu_name);
    266 
    267 	/* get system info from firmware */
    268 	memsize = rmixlfw_init(infop);
    269 
    270 	/* set the VM page size */
    271 	uvm_setpagesize();
    272 
    273 	physmem = btoc(memsize);
    274 
    275 	rmixl_obio_eb_bus_mem_init(&rcp->rc_obio_eb_memt, rcp);
    276 
    277 #if NCOM > 0
    278 	rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
    279 		COM_TYPE_NORMAL, comcnmode);
    280 #endif
    281 
    282 	printf("\nNetBSD/rmixl\n");
    283 	printf("memsize = %#"PRIx64"\n", memsize);
    284 #ifdef MEMLIMIT
    285 	printf("memlimit = %#"PRIx64"\n", (uint64_t)MEMLIMIT);
    286 #endif
    287 
    288 #if defined(MULTIPROCESSOR) && defined(MACHDEP_DEBUG)
    289 	rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info);
    290 	rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info + 1);
    291 	printf("cpu_wakeup_info %p, cpu_wakeup_end %p\n",
    292 		rcp->rc_cpu_wakeup_info,
    293 		rcp->rc_cpu_wakeup_end);
    294 	printf("userapp_cpu_map: %#"PRIx64"\n",
    295 		rcp->rc_psb_info.userapp_cpu_map);
    296 	printf("wakeup: %#"PRIx64"\n", rcp->rc_psb_info.wakeup);
    297 {
    298 	register_t sp;
    299 	asm volatile ("move	%0, $sp\n" : "=r"(sp));
    300 	printf("sp: %#"PRIx64"\n", sp);
    301 }
    302 #endif
    303 
    304 	rmixl_physaddr_init();
    305 
    306 	/*
    307 	 * Obtain the cpu frequency
    308 	 * Compute the number of ticks for hz.
    309 	 * Compute the delay divisor.
    310 	 * Double the Hz if this CPU runs at twice the
    311          *  external/cp0-count frequency
    312 	 */
    313 	curcpu()->ci_cpu_freq = rcp->rc_psb_info.cpu_frequency;
    314 	curcpu()->ci_cctr_freq = curcpu()->ci_cpu_freq;
    315 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    316 	curcpu()->ci_divisor_delay =
    317 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    318         if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    319 		curcpu()->ci_cpu_freq *= 2;
    320 
    321 	/*
    322 	 * Look at arguments passed to us and compute boothowto.
    323 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    324 	 *   by forcing sign extension in cast to (char *)
    325 	 */
    326 	boothowto = RB_AUTOBOOT;
    327 	for (int i = 1; i < argc; i++) {
    328 		for (char *cp = (char *)(intptr_t)argv[i]; *cp; cp++) {
    329 			int howto;
    330 			/* Ignore superfluous '-', if there is one */
    331 			if (*cp == '-')
    332 				continue;
    333 
    334 			howto = 0;
    335 			BOOT_FLAG(*cp, howto);
    336 			if (howto != 0)
    337 				boothowto |= howto;
    338 #ifdef DIAGNOSTIC
    339 			else
    340 				printf("bootflag '%c' not recognised\n", *cp);
    341 #endif
    342 		}
    343 	}
    344 #ifdef DIAGNOSTIC
    345 	printf("boothowto %#x\n", boothowto);
    346 #endif
    347 
    348 	/*
    349 	 * Reserve pages from the VM system.
    350 	 */
    351 
    352 	/* reserve 0..start..kernend pages */
    353 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    354 		0, round_page(MIPS_KSEG0_TO_PHYS(kernend)));
    355 
    356 	/* reserve reset exception vector page */
    357 	/* should never be in our clusters anyway... */
    358 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    359 		0x1FC00000, 0x1FC00000+NBPG);
    360 
    361 #ifdef MULTIPROCEESOR
    362 	/* reserve the cpu_wakeup_info area */
    363 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    364 		(u_quad_t)trunc_page(rcp->rc_cpu_wakeup_info),
    365 		(u_quad_t)round_page(rcp->rc_cpu_wakeup_end));
    366 #endif
    367 
    368 #ifdef MEMLIMIT
    369 	/* reserve everything >= MEMLIMIT */
    370 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    371 		(u_quad_t)MEMLIMIT, (u_quad_t)~0);
    372 #endif
    373 
    374 	/* get maximum RAM address from the VM clusters */
    375 	mem_cluster_maxaddr = 0;
    376 	for (u_int i=0; i < mem_cluster_cnt; i++) {
    377 		u_quad_t tmp = round_page(
    378 			mem_clusters[i].start + mem_clusters[i].size);
    379 		if (tmp > mem_cluster_maxaddr)
    380 			mem_cluster_maxaddr = tmp;
    381 	}
    382 	DPRINTF(("mem_cluster_maxaddr %#"PRIx64"\n", mem_cluster_maxaddr));
    383 
    384 	/*
    385 	 * Load mem_clusters[] into the VM system.
    386 	 */
    387 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
    388 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    389 
    390 	/*
    391 	 * Initialize error message buffer (at end of core).
    392 	 */
    393 	mips_init_msgbuf();
    394 
    395 	pmap_bootstrap();
    396 
    397 	/*
    398 	 * Allocate uarea page for lwp0 and set it.
    399 	 */
    400 	mips_init_lwp0_uarea();
    401 
    402 #if defined(DDB)
    403 	if (boothowto & RB_KDB)
    404 		Debugger();
    405 #endif
    406 	/*
    407 	 * store (cpu#0) curcpu in COP0 OSSCRATCH0
    408 	 * used in exception vector
    409 	 */
    410 	__asm __volatile("dmtc0 %0,$%1"
    411 		:: "r"(&cpu_info_store), "n"(MIPS_COP_0_OSSCRATCH));
    412 #ifdef MULTIPROCESSOR
    413 	mips_fixup_exceptions(rmixl_fixup_cop0_oscratch);
    414 #endif
    415 	rmixl_fixup_curcpu();
    416 }
    417 
    418 /*
    419  * set up Processor Control Regs for this core
    420  */
    421 void
    422 rmixl_pcr_init_core(void)
    423 {
    424 	uint32_t r;
    425 
    426 #ifdef MULTIPROCESSOR
    427 	rmixl_mtcr(RMIXL_PCR_MMU_SETUP, __BITS(2,0));
    428 						/* enable MMU clock gating */
    429 						/* 4 threads active -- why needed if Global? */
    430 						/* enable global TLB mode */
    431 #else
    432 	rmixl_mtcr(RMIXL_PCR_THREADEN, 1);	/* disable all threads except #0 */
    433 	rmixl_mtcr(RMIXL_PCR_MMU_SETUP, 0);	/* enable MMU clock gating */
    434 						/* set single MMU Thread Mode */
    435 						/* TLB is partitioned (1 partition) */
    436 #endif
    437 
    438 	r = rmixl_mfcr(RMIXL_PCR_L1D_CONFIG0);
    439 	r &= ~__BIT(14);			/* disable Unaligned Access */
    440 	rmixl_mtcr(RMIXL_PCR_L1D_CONFIG0, r);
    441 
    442 #if defined(DDB) && defined(MIPS_DDB_WATCH)
    443 	/*
    444 	 * clear IEU_DEFEATURE[DBE]
    445 	 * this enables COP0 watchpoint to trigger T_WATCH exception
    446 	 * instead of signaling JTAG.
    447 	 */
    448 	r = rmixl_mfcr(RMIXL_PCR_IEU_DEFEATURE);
    449 	r &= ~__BIT(7);
    450 	rmixl_mtcr(RMIXL_PCR_IEU_DEFEATURE, r);
    451 #endif
    452 }
    453 
    454 #ifdef MULTIPROCESSOR
    455 static bool
    456 rmixl_fixup_cop0_oscratch(int32_t load_addr, uint32_t new_insns[2])
    457 {
    458 	size_t offset = load_addr - (intptr_t)&cpu_info_store;
    459 
    460 	KASSERT(MIPS_KSEG0_P(load_addr));
    461 	KASSERT(offset < sizeof(struct cpu_info));
    462 
    463 	/*
    464 	 * Fixup this direct load cpu_info_store to actually get the current
    465 	 * CPU's cpu_info from COP0 OSSCRATCH0 and then fix the load to be
    466 	 * relative from the start of struct cpu_info.
    467 	 */
    468 
    469 	/* [0] = [d]mfc0 rX, $22 (OSScratch) */
    470 	new_insns[0] = (020 << 26)
    471 #ifdef _LP64
    472 	    | (1 << 21)		/* double move */
    473 #endif
    474 	    | (new_insns[0] & 0x001f0000)
    475 	    | (MIPS_COP_0_OSSCRATCH << 11) | (0 << 0);
    476 
    477 	/* [1] = [ls][dw] rX, offset(rX) */
    478 	new_insns[1] = (new_insns[1] & 0xffff0000) | offset;
    479 
    480 	return true;
    481 }
    482 #endif /* MULTIPROCESSOR */
    483 
    484 /*
    485  * The following changes all	lX	rN, L_CPU(MIPS_CURLWP) [curlwp->l_cpu]
    486  * to			     	[d]mfc0	rN, $22 [MIPS_COP_0_OSSCRATCH]
    487  *
    488  * the mfc0 is 3 cycles shorter than the load.
    489  */
    490 #define	LOAD_CURCPU_0	((MIPS_CURLWP_REG << 21) | offsetof(lwp_t, l_cpu))
    491 #define	MFC0_CURCPU_0	((OP_COP0 << 26) | (MIPS_COP_0_OSSCRATCH << 11))
    492 #ifdef _LP64
    493 #define	LOAD_CURCPU	((uint32_t)(OP_LD << 26) | LOAD_CURCPU_0)
    494 #define	MFC0_CURCPU	((uint32_t)(OP_DMF << 21) | MFC0_CURCPU_0)
    495 #else
    496 #define	LOAD_CURCPU	((uint32_t)(OP_LW << 26) | LOAD_CURCPU_0)
    497 #define	MFC0_CURCPU	((uint32_t)(OP_MF << 21) | MFC0_CURCPU_0)
    498 #endif
    499 #define	LOAD_CURCPU_MASK	0xffe0ffff
    500 
    501 static void
    502 rmixl_fixup_curcpu(void)
    503 {
    504 	extern uint32_t _ftext[];
    505 	extern uint32_t _etext[];
    506 
    507 	for (uint32_t *insnp = _ftext; insnp < _etext; insnp++) {
    508 		const uint32_t insn = *insnp;
    509 		if (__predict_false((insn & LOAD_CURCPU_MASK) == LOAD_CURCPU)) {
    510 			/*
    511 			 * Since the register to loaded is located in bits
    512 			 * 16-20 for the mfc0 and the load instruction we can
    513 			 * just change the instruction bits around it.
    514 			 */
    515 			*insnp = insn ^ LOAD_CURCPU ^ MFC0_CURCPU;
    516 			mips_icache_sync_range((vaddr_t)insnp, 4);
    517 		}
    518 	}
    519 }
    520 
    521 /*
    522  * ram_seg_resv - cut reserved regions out of segs, fragmenting as needed
    523  *
    524  * we simply build a new table of segs, then copy it back over the given one
    525  * this is inefficient but simple and called only a few times
    526  *
    527  * note: 'last' here means 1st addr past the end of the segment (start+size)
    528  */
    529 static u_int
    530 ram_seg_resv(phys_ram_seg_t *segs, u_int nsegs,
    531 	u_quad_t resv_first, u_quad_t resv_last)
    532 {
    533         u_quad_t first, last;
    534 	int new_nsegs=0;
    535 	int resv_flag;
    536 	phys_ram_seg_t new_segs[VM_PHYSSEG_MAX];
    537 
    538 	for (u_int i=0; i < nsegs; i++) {
    539 		resv_flag = 0;
    540 		first = trunc_page(segs[i].start);
    541 		last = round_page(segs[i].start + segs[i].size);
    542 
    543 		KASSERT(new_nsegs < VM_PHYSSEG_MAX);
    544 		if ((resv_first <= first) && (resv_last >= last)) {
    545 			/* whole segment is resverved */
    546 			continue;
    547 		}
    548 		if ((resv_first > first) && (resv_first < last)) {
    549 			u_quad_t new_last;
    550 
    551 			/*
    552 			 * reserved start in segment
    553 			 * salvage the leading fragment
    554 			 */
    555 			resv_flag = 1;
    556 			new_last = last - (last - resv_first);
    557 			KASSERT (new_last > first);
    558 			new_segs[new_nsegs].start = first;
    559 			new_segs[new_nsegs].size = new_last - first;
    560 			new_nsegs++;
    561 		}
    562 		if ((resv_last > first) && (resv_last < last)) {
    563 			u_quad_t new_first;
    564 
    565 			/*
    566 			 * reserved end in segment
    567 			 * salvage the trailing fragment
    568 			 */
    569 			resv_flag = 1;
    570 			new_first = first + (resv_last - first);
    571 			KASSERT (last > (new_first + NBPG));
    572 			new_segs[new_nsegs].start = new_first;
    573 			new_segs[new_nsegs].size = last - new_first;
    574 			new_nsegs++;
    575 		}
    576 		if (resv_flag == 0) {
    577 			/*
    578 			 * nothing reserved here, take it all
    579 			 */
    580 			new_segs[new_nsegs].start = first;
    581 			new_segs[new_nsegs].size = last - first;
    582 			new_nsegs++;
    583 		}
    584 
    585 	}
    586 
    587 	memcpy(segs, new_segs, sizeof(new_segs));
    588 
    589 	return new_nsegs;
    590 }
    591 
    592 /*
    593  * create an extent for physical address space
    594  * these are in units of MB for sake of compression (for sake of 32 bit kernels)
    595  * allocate the regions where we have known functions (DRAM, IO, etc)
    596  * what remains can be allocated as needed for other stuff
    597  * e.g. to configure BARs that are not already initialized and enabled.
    598  */
    599 static void
    600 rmixl_physaddr_init(void)
    601 {
    602 	struct extent *ext;
    603 	unsigned long start = 0UL;
    604 	unsigned long end = (__BIT(40) / (1024 * 1024)) -1;
    605 	u_long base;
    606 	u_long size;
    607 	uint32_t r;
    608 
    609 	ext = extent_create("physaddr", start, end,
    610 		(void *)rmixl_physaddr_storage, sizeof(rmixl_physaddr_storage),
    611 		EX_NOWAIT | EX_NOCOALESCE);
    612 
    613 	if (ext == NULL)
    614 		panic("%s: extent_create failed", __func__);
    615 
    616 	/*
    617 	 * grab regions per DRAM BARs
    618 	 */
    619 	for (u_int i=0; i < RMIXL_SBC_DRAM_NBARS; i++) {
    620 		r = RMIXL_IOREG_READ(RMIXL_SBC_DRAM_BAR(i));
    621 		if ((r & RMIXL_DRAM_BAR_STATUS) == 0)
    622 			continue;	/* not enabled */
    623 		base = (u_long)(DRAM_BAR_TO_BASE((uint64_t)r) / (1024 * 1024));
    624 		size = (u_long)(DRAM_BAR_TO_SIZE((uint64_t)r) / (1024 * 1024));
    625 
    626 		DPRINTF(("%s: %d: %d: 0x%08x -- 0x%010lx:%lu MB\n",
    627 			__func__, __LINE__, i, r, base * (1024 * 1024), size));
    628 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    629 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    630 				"failed", __func__, ext, base, size, EX_NOWAIT);
    631 	}
    632 
    633 	/*
    634 	 * get chip-dependent physaddr regions
    635 	 */
    636 	switch(cpu_rmixl_chip_type(mips_options.mips_cpu)) {
    637 	case CIDFL_RMI_TYPE_XLR:
    638 #if NRMIXL_PCIX
    639 		rmixl_physaddr_init_pcix(ext);
    640 #endif
    641 		break;
    642 	case CIDFL_RMI_TYPE_XLS:
    643 #if NRMIXL_PCIE
    644 		rmixl_physaddr_init_pcie(ext);
    645 #endif
    646 		break;
    647 	case CIDFL_RMI_TYPE_XLP:
    648 		/* XXX TBD */
    649 		panic("%s: RMI XLP not yet supported", __func__);
    650 	}
    651 
    652 	/*
    653 	 *  at this point all regions left in "physaddr" extent
    654 	 *  are unused holes in the physical adress space
    655 	 *  available for use as needed.
    656 	 */
    657 	rmixl_configuration.rc_phys_ex = ext;
    658 #ifdef MACHDEP_DEBUG
    659 	extent_print(ext);
    660 #endif
    661 }
    662 
    663 static uint64_t
    664 rmixlfw_init(int64_t infop)
    665 {
    666 	struct rmixl_config *rcp = &rmixl_configuration;
    667 
    668 #ifdef MULTIPROCESSOR
    669 	rmixl_get_wakeup_info(rcp);
    670 #endif
    671 
    672 	infop |= MIPS_KSEG0_START;
    673 	rcp->rc_psb_info = *(rmixlfw_info_t *)(intptr_t)infop;
    674 
    675 	rcp->rc_psb_type = PSB_TYPE_UNKNOWN;
    676 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    677 		if (rmiclfw_psb_id[i].psb_version ==
    678 		    rcp->rc_psb_info.psb_version) {
    679 			rcp->rc_psb_type = rmiclfw_psb_id[i].psb_type;
    680 			goto found;
    681 		}
    682 	}
    683 
    684 	rcp->rc_io_pbase = RMIXL_IO_DEV_PBASE;
    685 	rmixl_putchar_init(rcp->rc_io_pbase);
    686 
    687 #ifdef DIAGNOSTIC
    688 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    689 	rmixl_puthex64(rcp->rc_psb_info.psb_version);
    690 	rmixl_puts("\r\n");
    691 #endif
    692 
    693 #ifdef MEMSIZE
    694 	/* XXX trust and use MEMSIZE */
    695 	mem_clusters[0].start = 0;
    696 	mem_clusters[0].size = MEMSIZE;
    697 	mem_cluster_cnt = 1;
    698 	return MEMSIZE;
    699 #else
    700 	rmixl_puts("\r\nERROR: configure MEMSIZE\r\n");
    701 	cpu_reboot(RB_HALT, NULL);
    702 	/* NOTREACHED */
    703 #endif
    704 
    705  found:
    706 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rcp->rc_psb_info.io_base);
    707 	rmixl_putchar_init(rcp->rc_io_pbase);
    708 #ifdef MACHDEP_DEBUG
    709 	rmixl_puts("\r\ninfop: ");
    710 	rmixl_puthex64((uint64_t)(intptr_t)infop);
    711 #endif
    712 #ifdef DIAGNOSTIC
    713 	rmixl_puts("\r\nrecognized psb_version=");
    714 	rmixl_puthex64(rcp->rc_psb_info.psb_version);
    715 	rmixl_puts(", psb_type=");
    716 	rmixl_puts(rmixlfw_psb_type_name(rcp->rc_psb_type));
    717 	rmixl_puts("\r\n");
    718 #endif
    719 
    720 	return mem_clusters_init(
    721 		(rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.psb_physaddr_map,
    722 		(rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.avail_mem_map);
    723 }
    724 
    725 void
    726 rmixlfw_mmap_print(rmixlfw_mmap_t *map)
    727 {
    728 #ifdef MACHDEP_DEBUG
    729 	for (uint32_t i=0; i < map->nmmaps; i++) {
    730 		rmixl_puthex32(i);
    731 		rmixl_puts(", ");
    732 		rmixl_puthex64(map->entry[i].start);
    733 		rmixl_puts(", ");
    734 		rmixl_puthex64(map->entry[i].size);
    735 		rmixl_puts(", ");
    736 		rmixl_puthex32(map->entry[i].type);
    737 		rmixl_puts("\r\n");
    738 	}
    739 #endif
    740 }
    741 
    742 /*
    743  * mem_clusters_init
    744  *
    745  * initialize mem_clusters[] table based on memory address mapping
    746  * provided by boot firmware.
    747  *
    748  * prefer avail_mem_map if we can, otherwise use psb_physaddr_map.
    749  * these will be limited by MEMSIZE if it is configured.
    750  * if neither are available, just use MEMSIZE.
    751  */
    752 static uint64_t
    753 mem_clusters_init(
    754 	rmixlfw_mmap_t *psb_physaddr_map,
    755 	rmixlfw_mmap_t *avail_mem_map)
    756 {
    757 	rmixlfw_mmap_t *map = NULL;
    758 	const char *mapname;
    759 	uint64_t sz;
    760 	uint64_t sum;
    761 	u_int cnt;
    762 #ifdef MEMSIZE
    763 	uint64_t memsize = MEMSIZE;
    764 #endif
    765 
    766 #ifdef MACHDEP_DEBUG
    767 	rmixl_puts("psb_physaddr_map: ");
    768 	rmixl_puthex64((uint64_t)(intptr_t)psb_physaddr_map);
    769 	rmixl_puts("\r\n");
    770 #endif
    771 	if (psb_physaddr_map != NULL) {
    772 		map = psb_physaddr_map;
    773 		mapname = "psb_physaddr_map";
    774 		rmixlfw_mmap_print(map);
    775 	}
    776 #ifdef DIAGNOSTIC
    777 	else {
    778 		rmixl_puts("WARNING: no psb_physaddr_map\r\n");
    779 	}
    780 #endif
    781 
    782 #ifdef MACHDEP_DEBUG
    783 	rmixl_puts("avail_mem_map: ");
    784 	rmixl_puthex64((uint64_t)(intptr_t)avail_mem_map);
    785 	rmixl_puts("\r\n");
    786 #endif
    787 	if (avail_mem_map != NULL) {
    788 		map = avail_mem_map;
    789 		mapname = "avail_mem_map";
    790 		rmixlfw_mmap_print(map);
    791 	}
    792 #ifdef DIAGNOSTIC
    793 	else {
    794 		rmixl_puts("WARNING: no avail_mem_map\r\n");
    795 	}
    796 #endif
    797 
    798 	if (map == NULL) {
    799 #ifndef MEMSIZE
    800 		rmixl_puts("panic: no firmware memory map, "
    801 			"must configure MEMSIZE\r\n");
    802 		for(;;);	/* XXX */
    803 #else
    804 #ifdef DIAGNOSTIC
    805 		rmixl_puts("WARNING: no avail_mem_map, "
    806 			"using MEMSIZE\r\n");
    807 #endif
    808 
    809 		mem_clusters[0].start = 0;
    810 		mem_clusters[0].size = MEMSIZE;
    811 		mem_cluster_cnt = 1;
    812 		return MEMSIZE;
    813 #endif	/* MEMSIZE */
    814 	}
    815 
    816 #ifdef DIAGNOSTIC
    817 	rmixl_puts("using ");
    818 	rmixl_puts(mapname);
    819 	rmixl_puts("\r\n");
    820 #endif
    821 #ifdef MACHDEP_DEBUG
    822 	rmixl_puts("memory clusters:\r\n");
    823 #endif
    824 	sum = 0;
    825 	cnt = 0;
    826 	for (uint32_t i=0; i < map->nmmaps; i++) {
    827 		if (map->entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    828 			continue;
    829 		mem_clusters[cnt].start = map->entry[i].start;
    830 		sz = map->entry[i].size;
    831 		sum += sz;
    832 		mem_clusters[cnt].size = sz;
    833 #ifdef MACHDEP_DEBUG
    834 		rmixl_puthex32(i);
    835 		rmixl_puts(": ");
    836 		rmixl_puthex64(mem_clusters[cnt].start);
    837 		rmixl_puts(", ");
    838 		rmixl_puthex64(sz);
    839 		rmixl_puts(": ");
    840 		rmixl_puthex64(sum);
    841 		rmixl_puts("\r\n");
    842 #endif
    843 #ifdef MEMSIZE
    844 		/*
    845 		 * configurably limit memsize
    846 		 */
    847 		if (sum == memsize)
    848 			break;
    849 		if (sum > memsize) {
    850 			uint64_t tmp;
    851 
    852 			tmp = sum - memsize;
    853 			sz -= tmp;
    854 			sum -= tmp;
    855 			mem_clusters[cnt].size = sz;
    856 			cnt++;
    857 			break;
    858 		}
    859 #endif
    860 		cnt++;
    861 	}
    862 	mem_cluster_cnt = cnt;
    863 	return sum;
    864 }
    865 
    866 #ifdef MULTIPROCESSOR
    867 /*
    868  * RMI firmware passes wakeup info structure in CP0 OS Scratch reg #7
    869  * they do not explicitly give us the size of the wakeup area.
    870  * we "know" that firmware loader sets wip->gp thusly:
    871  *   gp = stack_start[vcpu] = round_page(wakeup_end) + (vcpu * (PAGE_SIZE * 2))
    872  * so
    873  *   round_page(wakeup_end) == gp - (vcpu * (PAGE_SIZE * 2))
    874  * Only the "master" cpu runs this function, so
    875  *   vcpu = wip->master_cpu
    876  */
    877 void
    878 rmixl_get_wakeup_info(struct rmixl_config *rcp)
    879 {
    880 	volatile rmixlfw_cpu_wakeup_info_t *wip;
    881 	int32_t scratch_7;
    882 	intptr_t end;
    883 
    884 	__asm__ volatile(
    885 		".set push"				"\n"
    886 		".set noreorder"			"\n"
    887 		".set mips64"				"\n"
    888 		"dmfc0	%0, $22, 7"			"\n"
    889 		".set pop"				"\n"
    890 			: "=r"(scratch_7));
    891 
    892 	wip = (volatile rmixlfw_cpu_wakeup_info_t *)
    893 			(intptr_t)scratch_7;
    894 	end = wip->entry.gp - (wip->master_cpu & (PAGE_SIZE * 2));;
    895 
    896 	if (wip->valid == 1) {
    897 		rcp->rc_cpu_wakeup_end = (const void *)end;
    898 		rcp->rc_cpu_wakeup_info = wip;
    899 	}
    900 };
    901 
    902 #ifdef MACHDEP_DEBUG
    903 static void
    904 rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *wip)
    905 {
    906 	int i;
    907 
    908 	printf("%s: wip %p, size %lu\n", __func__, wip, sizeof(*wip));
    909 
    910 	printf("cpu_status %#x\n",  wip->cpu_status);
    911 	printf("valid: %d\n", wip->valid);
    912 	printf("entry: addr %#x, args %#x, sp %#"PRIx64", gp %#"PRIx64"\n",
    913 		wip->entry.addr,
    914 		wip->entry.args,
    915 		wip->entry.sp,
    916 		wip->entry.gp);
    917 	printf("master_cpu %d\n", wip->master_cpu);
    918 	printf("master_cpu_mask %#x\n", wip->master_cpu_mask);
    919 	printf("buddy_cpu_mask %#x\n", wip->buddy_cpu_mask);
    920 	printf("psb_os_cpu_map %#x\n", wip->psb_os_cpu_map);
    921 	printf("argc %d\n", wip->argc);
    922 	printf("argv:");
    923 	for (i=0; i < wip->argc; i++)
    924 		printf(" %#x", wip->argv[i]);
    925 	printf("\n");
    926 	printf("valid_tlb_entries %d\n", wip->valid_tlb_entries);
    927 	printf("tlb_map:\n");
    928 	for (i=0; i < wip->valid_tlb_entries; i++) {
    929 		volatile const struct lib_cpu_tlb_mapping *m =
    930 			&wip->tlb_map[i];
    931 		printf(" %d", m->page_size);
    932 		printf(", %d", m->asid);
    933 		printf(", %d", m->coherency);
    934 		printf(", %d", m->coherency);
    935 		printf(", %d", m->attr);
    936 		printf(", %#x", m->virt);
    937 		printf(", %#"PRIx64"\n", m->phys);
    938 	}
    939 	printf("elf segs:\n");
    940 	for (i=0; i < MAX_ELF_SEGMENTS; i++) {
    941 		volatile const struct core_segment_info *e =
    942 			&wip->seg_info[i];
    943 		printf(" %#"PRIx64"", e->vaddr);
    944 		printf(", %#"PRIx64"", e->memsz);
    945 		printf(", %#x\n", e->flags);
    946 	}
    947 	printf("envc %d\n", wip->envc);
    948 	for (i=0; i < wip->envc; i++)
    949 		printf(" %#x \"%s\"", wip->envs[i],
    950 			(char *)(intptr_t)(int32_t)(wip->envs[i]));
    951 	printf("\n");
    952 	printf("app_mode %d\n", wip->app_mode);
    953 	printf("printk_lock %#x\n", wip->printk_lock);
    954 	printf("kseg_master %d\n", wip->kseg_master);
    955 	printf("kuseg_reentry_function %#x\n", wip->kuseg_reentry_function);
    956 	printf("kuseg_reentry_args %#x\n", wip->kuseg_reentry_args);
    957 	printf("app_shared_mem_addr %#"PRIx64"\n", wip->app_shared_mem_addr);
    958 	printf("app_shared_mem_size %#"PRIx64"\n", wip->app_shared_mem_size);
    959 	printf("app_shared_mem_orig %#"PRIx64"\n", wip->app_shared_mem_orig);
    960 	printf("loader_lock %#x\n", wip->loader_lock);
    961 	printf("global_wakeup_mask %#x\n", wip->global_wakeup_mask);
    962 	printf("unused_0 %#x\n", wip->unused_0);
    963 }
    964 #endif	/* MACHDEP_DEBUG */
    965 #endif 	/* MULTIPROCESSOR */
    966 
    967 void
    968 consinit(void)
    969 {
    970 
    971 	/*
    972 	 * Everything related to console initialization is done
    973 	 * in mach_init().
    974 	 */
    975 }
    976 
    977 /*
    978  * Allocate memory for variable-sized tables,
    979  */
    980 void
    981 cpu_startup(void)
    982 {
    983 	vaddr_t minaddr, maxaddr;
    984 	char pbuf[9];
    985 
    986 	/*
    987 	 * Good {morning,afternoon,evening,night}.
    988 	 */
    989 	printf("%s%s", copyright, version);
    990 	format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem));
    991 	printf("total memory = %s\n", pbuf);
    992 
    993 	/*
    994 	 * Virtual memory is bootstrapped -- notify the bus spaces
    995 	 * that memory allocation is now safe.
    996 	 */
    997 	rmixl_configuration.rc_mallocsafe = 1;
    998 
    999 	minaddr = 0;
   1000 	/*
   1001 	 * Allocate a submap for physio.
   1002 	 */
   1003 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1004 				    VM_PHYS_SIZE, 0, FALSE, NULL);
   1005 
   1006 	/*
   1007 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
   1008 	 * are allocated via the pool allocator, and we use XKSEG to
   1009 	 * map those pages.)
   1010 	 */
   1011 
   1012 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
   1013 	printf("avail memory = %s\n", pbuf);
   1014 }
   1015 
   1016 int	waittime = -1;
   1017 
   1018 void
   1019 cpu_reboot(int howto, char *bootstr)
   1020 {
   1021 
   1022 	/* Take a snapshot before clobbering any registers. */
   1023 	savectx(curpcb);
   1024 
   1025 	if (cold) {
   1026 		howto |= RB_HALT;
   1027 		goto haltsys;
   1028 	}
   1029 
   1030 	/* If "always halt" was specified as a boot flag, obey. */
   1031 	if (boothowto & RB_HALT)
   1032 		howto |= RB_HALT;
   1033 
   1034 	boothowto = howto;
   1035 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
   1036 		waittime = 0;
   1037 		vfs_shutdown();
   1038 
   1039 		/*
   1040 		 * If we've been adjusting the clock, the todr
   1041 		 * will be out of synch; adjust it now.
   1042 		 */
   1043 		resettodr();
   1044 	}
   1045 
   1046 	splhigh();
   1047 
   1048 	if (howto & RB_DUMP)
   1049 		dumpsys();
   1050 
   1051 haltsys:
   1052 	doshutdownhooks();
   1053 
   1054 	if (howto & RB_HALT) {
   1055 		printf("\n");
   1056 		printf("The operating system has halted.\n");
   1057 		printf("Please press any key to reboot.\n\n");
   1058 		cnpollc(1);	/* For proper keyboard command handling */
   1059 		cngetc();
   1060 		cnpollc(0);
   1061 	}
   1062 
   1063 	printf("rebooting...\n\n");
   1064 
   1065 	rmixl_reset();
   1066 }
   1067 
   1068 /*
   1069  * goodbye world
   1070  */
   1071 void __attribute__((__noreturn__))
   1072 rmixl_reset(void)
   1073 {
   1074 	uint32_t r;
   1075 
   1076 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET);
   1077 	r |= RMIXL_GPIO_RESET_RESET;
   1078 	RMIXL_IOREG_WRITE(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET, r);
   1079 
   1080 	printf("soft reset failed, spinning...\n");
   1081 	for (;;);
   1082 }
   1083