machdep.c revision 1.1.2.3       1 /*	$NetBSD: machdep.c,v 1.1.2.3 2009/09/15 02:46:43 cliff Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department, The Mach Operating System project at
     80  * Carnegie-Mellon University and Ralph Campbell.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by the University of
     93  *	California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
    111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.1.2.3 2009/09/15 02:46:43 cliff Exp $");
    116 
    117 #include "opt_ddb.h"
    118 #include "opt_com.h"
    119 #include "opt_execfmt.h"
    120 #include "opt_memsize.h"
    121 
    122 #include <sys/param.h>
    123 #include <sys/systm.h>
    124 #include <sys/kernel.h>
    125 #include <sys/buf.h>
    126 #include <sys/reboot.h>
    127 #include <sys/user.h>
    128 #include <sys/mount.h>
    129 #include <sys/kcore.h>
    130 #include <sys/boot_flag.h>
    131 #include <sys/termios.h>
    132 #include <sys/ksyms.h>
    133 #include <sys/bus.h>
    134 #include <sys/device.h>
    135 
    136 #include <uvm/uvm_extern.h>
    137 
    138 #include <dev/cons.h>
    139 
    140 #include "ksyms.h"
    141 
    142 #if NKSYMS || defined(DDB) || defined(LKM)
    143 #include <machine/db_machdep.h>
    144 #include <ddb/db_extern.h>
    145 #endif
    146 
    147 #include <machine/cpu.h>
    148 #include <machine/psl.h>
    149 
    150 #include "com.h"
    151 #if NCOM == 0
    152 #error no serial console
    153 #endif
    154 
    155 #include <dev/ic/comreg.h>
    156 #include <dev/ic/comvar.h>
    157 
    158 #include <mips/rmi/rmixl_comvar.h>
    159 #include <mips/rmi/rmixlvar.h>
    160 #include <mips/rmi/rmixl_firmware.h>
    161 #include <mips/rmi/rmixlreg.h>
    162 
    163 #ifndef CONSPEED
    164 # define CONSPEED 38400
    165 #endif
    166 #ifndef CONMODE
    167 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    168 #endif
    169 #ifndef CONSADDR
    170 # define CONSADDR RMIXL_IO_DEV_UART_1
    171 #endif
    172 
    173 int		comcnfreq = -1;
    174 uint		comcnspeed = CONSPEED;
    175 uint		comcnmode  = CONMODE;
    176 bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    177 
    178 struct rmixl_config rmixl_configuration;
    179 
    180 
    181 /*
    182  * array of tested firmware versions
    183  * if you fiund new ones and they work
    184  * please add them
    185  */
    186 static uint64_t rmiclfw_psb_versions[] = {
    187 	0x4958d4fb00000056,
    188 };
    189 #define RMICLFW_PSB_VERSIONS_LEN \
    190 	(sizeof(rmiclfw_psb_versions)/sizeof(rmiclfw_psb_versions[0]))
    191 
    192 /*
    193  * kernel copies of firmware info
    194  */
    195 static rmixlfw_info_t rmixlfw_info;
    196 static rmixlfw_mmap_t rmixlfw_phys_mmap;
    197 static rmixlfw_mmap_t rmixlfw_avail_mmap;
    198 
    199 
    200 /* For sysctl_hw. */
    201 extern char cpu_model[];
    202 
    203 /* Our exported CPU info; we can have only one. */
    204 struct cpu_info cpu_info_store;
    205 
    206 /* Maps for VM objects. */
    207 struct vm_map *mb_map = NULL;
    208 struct vm_map *phys_map = NULL;
    209 
    210 int	physmem;		/* Total physical memory */
    211 
    212 int	netboot;		/* Are we netbooting? */
    213 
    214 
    215 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    216 int mem_cluster_cnt;
    217 
    218 void configure(void);
    219 void mach_init(int, int32_t *, void *, void *);
    220 static u_long rmixlfw_init(void *);
    221 static void __attribute__((__noreturn__)) rmixl_exit(int);
    222 
    223 
    224 /*
    225  * safepri is a safe priority for sleep to set for a spin-wait during
    226  * autoconfiguration or after a panic.  Used as an argument to splx().
    227  */
    228 int	safepri = MIPS1_PSL_LOWIPL;
    229 
    230 extern struct user *proc0paddr;
    231 
    232 /*
    233  * Do all the stuff that locore normally does before calling main().
    234  */
    235 void
    236 mach_init(int argc, int32_t *argv, void *envp, void *infop)
    237 {
    238 	struct rmixl_config *rcp;
    239 	void *kernend, *v;
    240         size_t first, last;
    241 	u_long memsize;
    242 	extern char edata[], end[];
    243 
    244 	/*
    245 	 * Clear the BSS segment.
    246 	 */
    247 	kernend = (void *)mips_round_page(end);
    248 	memset(edata, 0, (char *)kernend - edata);
    249 
    250 	/*
    251 	 * Set up the exception vectors and CPU-specific function
    252 	 * vectors early on.  We need the wbflush() vector set up
    253 	 * before comcnattach() is called (or at least before the
    254 	 * first printf() after that is called).
    255 	 * Also clears the I+D caches.
    256 	 */
    257 	mips_vector_init();
    258 
    259 	memsize = rmixlfw_init(infop);
    260 
    261 	/* set the VM page size */
    262 	uvm_setpagesize();
    263 
    264 	physmem = btoc(memsize);
    265 
    266 	rcp = &rmixl_configuration;
    267 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rmixlfw_info.io_base);
    268 	rmixl_eb_bus_mem_init(&rcp->rc_eb_memt, rcp); /* need for console */
    269 	rmixl_el_bus_mem_init(&rcp->rc_el_memt, rcp); /* XXX defer ? */
    270 
    271 #if NCOM > 0
    272 	rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
    273 		COM_TYPE_NORMAL, comcnmode);
    274 #endif
    275 
    276 	printf("\nNetBSD/rmixl\n");
    277 	printf("memsize = %#lx\n", memsize);
    278 
    279 	/*
    280 	 * Obtain the cpu frequency
    281 	 * Compute the number of ticks for hz.
    282 	 * Compute the delay divisor.
    283 	 * Double the Hz if this CPU runs at twice the
    284          *  external/cp0-count frequency
    285 	 */
    286 	curcpu()->ci_cpu_freq = rmixlfw_info.cpu_frequency;
    287 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    288 	curcpu()->ci_divisor_delay =
    289 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    290         if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    291 		curcpu()->ci_cpu_freq *= 2;
    292 
    293 	/*
    294 	 * Look at arguments passed to us and compute boothowto.
    295 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    296 	 *   by forcing sign extension in cast to (char *)
    297 	 */
    298 	boothowto = RB_AUTOBOOT;
    299 	for (int i = 1; i < argc; i++) {
    300 		for (char *cp = (char *)(uint64_t)argv[i]; *cp; cp++) {
    301 			int howto;
    302 			/* Ignore superfluous '-', if there is one */
    303 			if (*cp == '-')
    304 				continue;
    305 
    306 			howto = 0;
    307 			BOOT_FLAG(*cp, howto);
    308 			if (! howto)
    309 				printf("bootflag '%c' not recognised\n", *cp);
    310 			else
    311 				boothowto |= howto;
    312 		}
    313 	}
    314 	printf("boothowto %#x\n", boothowto);
    315 
    316 	/*
    317 	 * Load the rest of the available pages into the VM system.
    318 	 */
    319 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    320 	last = mem_clusters[0].start + mem_clusters[0].size;
    321 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    322 		VM_FREELIST_DEFAULT);
    323 	for (int i = 1; i < mem_cluster_cnt; i++) {
    324 		first = round_page(mem_clusters[i].start);
    325 		last = mem_clusters[i].start + mem_clusters[i].size;
    326 		uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    327 			VM_FREELIST_DEFAULT);
    328 	}
    329 
    330 	/*
    331 	 * Initialize error message buffer (at end of core).
    332 	 */
    333 	mips_init_msgbuf();
    334 
    335 	pmap_bootstrap();
    336 
    337 	/*
    338 	 * Allocate space for proc0's USPACE.
    339 	 */
    340 	v = (void *)uvm_pageboot_alloc(USPACE);
    341 	lwp0.l_addr = proc0paddr = (struct user *)v;
    342 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    343 #ifdef _LP64
    344         lwp0.l_md.md_regs->f_regs[_R_SR] = MIPS_SR_KX;
    345 #endif
    346         proc0paddr->u_pcb.pcb_context.val[_L_SR] =
    347 #ifdef _LP64
    348             MIPS_SR_KX |
    349 #endif
    350             MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    351 
    352 
    353 	/*
    354 	 * Initialize debuggers, and break into them, if appropriate.
    355 	 */
    356 #if NKSYMS || defined(DDB) || defined(LKM)
    357 	ksyms_init(0, 0, 0);
    358 #endif
    359 
    360 #if defined(DDB)
    361 	if (boothowto & RB_KDB)
    362 		Debugger();
    363 #endif
    364 
    365 }
    366 
    367 static u_long
    368 rmixlfw_init(void *infop)
    369 {
    370 	uint64_t tmp;
    371 	uint64_t sz;
    372 	uint64_t sum;
    373 #ifdef MEMSIZE
    374 	u_long memsize = MEMSIZE;
    375 #endif
    376 
    377 	strcpy(cpu_model, "RMI XLS616ATX VIIA");	/* XXX */
    378 
    379 	tmp = (int64_t)infop;
    380 	tmp |= 0xffffffffULL << 32;
    381 	infop = (void *)tmp;
    382 	rmixlfw_info = *(rmixlfw_info_t *)infop;
    383 
    384 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    385 		if (rmiclfw_psb_versions[i] == rmixlfw_info.psb_version)
    386 			goto found;
    387 	}
    388 
    389 	rmixl_putchar_init(MIPS_KSEG1_TO_PHYS(rmixlfw_info.io_base));
    390 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    391 	rmixl_puthex64(rmixlfw_info.psb_version);
    392 	rmixl_puts("\r\n");
    393  found:
    394 
    395 	rmixlfw_phys_mmap  = *(rmixlfw_mmap_t *)rmixlfw_info.psb_physaddr_map;
    396 	rmixlfw_avail_mmap = *(rmixlfw_mmap_t *)rmixlfw_info.avail_mem_map;
    397 
    398 	sum = 0;
    399 	mem_cluster_cnt = 0;
    400 	for (uint32_t i=0; i < rmixlfw_avail_mmap.nmmaps; i++) {
    401 		if (rmixlfw_avail_mmap.entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    402 			continue;
    403 		mem_clusters[i].start = rmixlfw_avail_mmap.entry[i].start;
    404 		sz = rmixlfw_avail_mmap.entry[i].size;
    405 		sum += sz;
    406 		mem_clusters[i].size = sz;
    407 		mem_cluster_cnt++;
    408 #ifdef DEBUG
    409 		rmixl_puthex32(i);
    410 		rmixl_puts(": ");
    411 		rmixl_puthex64(mem_clusters[i].start);
    412 		rmixl_puts(": ");
    413 		rmixl_puthex64(sz);
    414 		rmixl_puts(": ");
    415 		rmixl_puthex64(sum);
    416 		rmixl_puts("\r\n");
    417 #endif
    418 #ifdef MEMSIZE
    419 		/*
    420 		 * configurably limit memsize
    421 		 */
    422 		if (sum == memsize)
    423 			break;
    424 		if (sum > memsize) {
    425 			tmp = sum - memsize;
    426 			sz -= tmp;
    427 			sum -= tmp;
    428 			mem_clusters[i].size = sz;
    429 			break;
    430 		}
    431 #endif
    432 	}
    433 	return sum;
    434 }
    435 
    436 void
    437 consinit(void)
    438 {
    439 
    440 	/*
    441 	 * Everything related to console initialization is done
    442 	 * in mach_init().
    443 	 */
    444 }
    445 
    446 /*
    447  * Allocate memory for variable-sized tables,
    448  */
    449 void
    450 cpu_startup()
    451 {
    452 	vaddr_t minaddr, maxaddr;
    453 	char pbuf[9];
    454 
    455 	/*
    456 	 * Good {morning,afternoon,evening,night}.
    457 	 */
    458 	printf("%s%s", copyright, version);
    459 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    460 	printf("total memory = %s\n", pbuf);
    461 
    462 	/*
    463 	 * Virtual memory is bootstrapped -- notify the bus spaces
    464 	 * that memory allocation is now safe.
    465 	 */
    466 	rmixl_configuration.rc_mallocsafe = 1;
    467 
    468 	minaddr = 0;
    469 	/*
    470 	 * Allocate a submap for physio.
    471 	 */
    472 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    473 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    474 
    475 	/*
    476 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    477 	 * are allocated via the pool allocator, and we use KSEG to
    478 	 * map those pages.)
    479 	 */
    480 
    481 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    482 	printf("avail memory = %s\n", pbuf);
    483 }
    484 
    485 int	waittime = -1;
    486 
    487 void
    488 cpu_reboot(howto, bootstr)
    489 	int howto;
    490 	char *bootstr;
    491 {
    492 
    493 	/* Take a snapshot before clobbering any registers. */
    494 	if (curproc)
    495 		savectx((struct user *)curpcb);
    496 
    497 	if (cold) {
    498 		howto |= RB_HALT;
    499 		goto haltsys;
    500 	}
    501 
    502 	/* If "always halt" was specified as a boot flag, obey. */
    503 	if (boothowto & RB_HALT)
    504 		howto |= RB_HALT;
    505 
    506 	boothowto = howto;
    507 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    508 		waittime = 0;
    509 		vfs_shutdown();
    510 
    511 		/*
    512 		 * If we've been adjusting the clock, the todr
    513 		 * will be out of synch; adjust it now.
    514 		 */
    515 		resettodr();
    516 	}
    517 
    518 	splhigh();
    519 
    520 	if (howto & RB_DUMP)
    521 		dumpsys();
    522 
    523 haltsys:
    524 	doshutdownhooks();
    525 
    526 	if (howto & RB_HALT) {
    527 		printf("\n");
    528 		printf("The operating system has halted.\n");
    529 		printf("Please press any key to reboot.\n\n");
    530 		cnpollc(1);	/* For proper keyboard command handling */
    531 		cngetc();
    532 		cnpollc(0);
    533 	}
    534 
    535 	printf("rebooting...\n\n");
    536 
    537 	rmixl_exit(0);
    538 }
    539 
    540 /*
    541  * goodbye world
    542  */
    543 #define GPIO_CPU_RST 0xa0			/* XXX TMP */
    544 void __attribute__((__noreturn__))
    545 rmixl_exit(int howto)
    546 {
    547 	/* use firmware callbak to reboot */
    548 	void (*reset)(void) = (void *)rmixlfw_info.warm_reset;
    549 	(*reset)();
    550 	printf("warm reset callback failed, spinning...\n");
    551 	for (;;);
    552 }
    553