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