Home | History | Annotate | Line # | Download | only in rmixl
machdep.c revision 1.1.2.1
      1 /*	$NetBSD: machdep.c,v 1.1.2.1 2009/09/13 03:27:38 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.1 2009/09/13 03:27:38 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_base = rmixlfw_info.io_base;
    268 	rmixl_bus_mem_init(&rcp->rc_memt, rcp);
    269 #ifdef NOTYET
    270 	rmixl_bus_io_init(&rcp->rc_iot, rcp);
    271 	rmixl_dma_init(rcp);
    272 #endif
    273 #if NPCI > 0
    274 	rmixl_pci_init(&rcp->rc_pc, &rcp->rc_gt);
    275 #endif
    276 
    277 #if NCOM > 0
    278 	rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
    279 		COM_TYPE_NORMAL, comcnmode);
    280 #endif
    281 
    282 	printf("\nNetBSD/rmixl\n");
    283 	printf("memsize = %#lx\n", memsize);
    284 
    285 	/*
    286 	 * Obtain the cpu frequency
    287 	 * Compute the number of ticks for hz.
    288 	 * Compute the delay divisor.
    289 	 * Double the Hz if this CPU runs at twice the
    290          *  external/cp0-count frequency
    291 	 */
    292 	curcpu()->ci_cpu_freq = rmixlfw_info.cpu_frequency;
    293 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    294 	curcpu()->ci_divisor_delay =
    295 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    296         if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    297 		curcpu()->ci_cpu_freq *= 2;
    298 
    299 	/*
    300 	 * Look at arguments passed to us and compute boothowto.
    301 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    302 	 *   by forcing sign extension in cast to (char *)
    303 	 */
    304 	boothowto = RB_AUTOBOOT;
    305 	for (int i = 1; i < argc; i++) {
    306 		for (char *cp = (char *)(uint64_t)argv[i]; *cp; cp++) {
    307 			int howto;
    308 			/* Ignore superfluous '-', if there is one */
    309 			if (*cp == '-')
    310 				continue;
    311 
    312 			howto = 0;
    313 			BOOT_FLAG(*cp, howto);
    314 			if (! howto)
    315 				printf("bootflag '%c' not recognised\n", *cp);
    316 			else
    317 				boothowto |= howto;
    318 		}
    319 	}
    320 	printf("boothowto %#x\n", boothowto);
    321 
    322 	/*
    323 	 * Load the rest of the available pages into the VM system.
    324 	 */
    325 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    326 	last = mem_clusters[0].start + mem_clusters[0].size;
    327 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    328 		VM_FREELIST_DEFAULT);
    329 	for (int i = 1; i < mem_cluster_cnt; i++) {
    330 		first = round_page(mem_clusters[i].start);
    331 		last = mem_clusters[i].start + mem_clusters[i].size;
    332 		uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    333 			VM_FREELIST_DEFAULT);
    334 	}
    335 
    336 	/*
    337 	 * Initialize error message buffer (at end of core).
    338 	 */
    339 	mips_init_msgbuf();
    340 
    341 	pmap_bootstrap();
    342 
    343 	/*
    344 	 * Allocate space for proc0's USPACE.
    345 	 */
    346 	v = (void *)uvm_pageboot_alloc(USPACE);
    347 	lwp0.l_addr = proc0paddr = (struct user *)v;
    348 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    349 #ifdef _LP64
    350         lwp0.l_md.md_regs->f_regs[_R_SR] = MIPS_SR_KX;
    351 #endif
    352         proc0paddr->u_pcb.pcb_context.val[_L_SR] =
    353 #ifdef _LP64
    354             MIPS_SR_KX |
    355 #endif
    356             MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    357 
    358 
    359 	/*
    360 	 * Initialize debuggers, and break into them, if appropriate.
    361 	 */
    362 #if NKSYMS || defined(DDB) || defined(LKM)
    363 	ksyms_init(0, 0, 0);
    364 #endif
    365 
    366 #if defined(DDB)
    367 	if (boothowto & RB_KDB)
    368 		Debugger();
    369 #endif
    370 
    371 }
    372 
    373 static u_long
    374 rmixlfw_init(void *infop)
    375 {
    376 	uint64_t tmp;
    377 	uint64_t sz;
    378 	uint64_t sum;
    379 #ifdef MEMSIZE
    380 	u_long memsize = MEMSIZE;
    381 #endif
    382 
    383 	strcpy(cpu_model, "RMI XLS616ATX VIIA");	/* XXX */
    384 
    385 	tmp = (int64_t)infop;
    386 	tmp |= 0xffffffffULL << 32;
    387 	infop = (void *)tmp;
    388 	rmixlfw_info = *(rmixlfw_info_t *)infop;
    389 
    390 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    391 		if (rmiclfw_psb_versions[i] == rmixlfw_info.psb_version)
    392 			goto found;
    393 	}
    394 
    395 	rmixl_putchar_init(rmixlfw_info.io_base);
    396 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    397 	rmixl_puthex64(rmixlfw_info.psb_version);
    398 	rmixl_puts("\r\n");
    399  found:
    400 
    401 	rmixlfw_phys_mmap  = *(rmixlfw_mmap_t *)rmixlfw_info.psb_physaddr_map;
    402 	rmixlfw_avail_mmap = *(rmixlfw_mmap_t *)rmixlfw_info.avail_mem_map;
    403 
    404 	sum = 0;
    405 	mem_cluster_cnt = 0;
    406 	for (uint32_t i=0; i < rmixlfw_avail_mmap.nmmaps; i++) {
    407 		if (rmixlfw_avail_mmap.entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    408 			continue;
    409 		mem_clusters[i].start = rmixlfw_avail_mmap.entry[i].start;
    410 		sz = rmixlfw_avail_mmap.entry[i].size;
    411 		sum += sz;
    412 		mem_clusters[i].size = sz;
    413 		mem_cluster_cnt++;
    414 #ifdef DEBUG
    415 		rmixl_puthex32(i);
    416 		rmixl_puts(": ");
    417 		rmixl_puthex64(mem_clusters[i].start);
    418 		rmixl_puts(": ");
    419 		rmixl_puthex64(sz);
    420 		rmixl_puts(": ");
    421 		rmixl_puthex64(sum);
    422 		rmixl_puts("\r\n");
    423 #endif
    424 #ifdef MEMSIZE
    425 		/*
    426 		 * configurably limit memsize
    427 		 */
    428 		if (sum == memsize)
    429 			break;
    430 		if (sum > memsize) {
    431 			tmp = sum - memsize;
    432 			sz -= tmp;
    433 			sum -= tmp;
    434 			mem_clusters[i].size = sz;
    435 			break;
    436 		}
    437 #endif
    438 	}
    439 	return sum;
    440 }
    441 
    442 void
    443 consinit(void)
    444 {
    445 
    446 	/*
    447 	 * Everything related to console initialization is done
    448 	 * in mach_init().
    449 	 */
    450 }
    451 
    452 /*
    453  * Allocate memory for variable-sized tables,
    454  */
    455 void
    456 cpu_startup()
    457 {
    458 	vaddr_t minaddr, maxaddr;
    459 	char pbuf[9];
    460 
    461 	/*
    462 	 * Good {morning,afternoon,evening,night}.
    463 	 */
    464 	printf("%s%s", copyright, version);
    465 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    466 	printf("total memory = %s\n", pbuf);
    467 
    468 	/*
    469 	 * Virtual memory is bootstrapped -- notify the bus spaces
    470 	 * that memory allocation is now safe.
    471 	 */
    472 	rmixl_configuration.rc_mallocsafe = 1;
    473 
    474 	minaddr = 0;
    475 	/*
    476 	 * Allocate a submap for physio.
    477 	 */
    478 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    479 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    480 
    481 	/*
    482 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    483 	 * are allocated via the pool allocator, and we use KSEG to
    484 	 * map those pages.)
    485 	 */
    486 
    487 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    488 	printf("avail memory = %s\n", pbuf);
    489 }
    490 
    491 int	waittime = -1;
    492 
    493 void
    494 cpu_reboot(howto, bootstr)
    495 	int howto;
    496 	char *bootstr;
    497 {
    498 
    499 	/* Take a snapshot before clobbering any registers. */
    500 	if (curproc)
    501 		savectx((struct user *)curpcb);
    502 
    503 	if (cold) {
    504 		howto |= RB_HALT;
    505 		goto haltsys;
    506 	}
    507 
    508 	/* If "always halt" was specified as a boot flag, obey. */
    509 	if (boothowto & RB_HALT)
    510 		howto |= RB_HALT;
    511 
    512 	boothowto = howto;
    513 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    514 		waittime = 0;
    515 		vfs_shutdown();
    516 
    517 		/*
    518 		 * If we've been adjusting the clock, the todr
    519 		 * will be out of synch; adjust it now.
    520 		 */
    521 		resettodr();
    522 	}
    523 
    524 	splhigh();
    525 
    526 	if (howto & RB_DUMP)
    527 		dumpsys();
    528 
    529 haltsys:
    530 	doshutdownhooks();
    531 
    532 	if (howto & RB_HALT) {
    533 		printf("\n");
    534 		printf("The operating system has halted.\n");
    535 		printf("Please press any key to reboot.\n\n");
    536 		cnpollc(1);	/* For proper keyboard command handling */
    537 		cngetc();
    538 		cnpollc(0);
    539 	}
    540 
    541 	printf("rebooting...\n\n");
    542 
    543 	rmixl_exit(0);
    544 }
    545 
    546 /*
    547  * goodbye world
    548  */
    549 #define GPIO_CPU_RST 0xa0			/* XXX TMP */
    550 void __attribute__((__noreturn__))
    551 rmixl_exit(int howto)
    552 {
    553 	/* use firmware callbak to reboot */
    554 	void (*reset)(void) = (void *)rmixlfw_info.warm_reset;
    555 	(*reset)();
    556 	printf("warm reset callback failed, spinning...\n");
    557 	for (;;);
    558 }
    559