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