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