Home | History | Annotate | Line # | Download | only in malta
machdep.c revision 1.32
      1 /*	$NetBSD: machdep.c,v 1.32 2009/02/13 22:41:01 apb 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.32 2009/02/13 22:41:01 apb Exp $");
    116 
    117 #include "opt_ddb.h"
    118 #include "opt_execfmt.h"
    119 #include "opt_modular.h"
    120 
    121 #include <sys/param.h>
    122 #include <sys/systm.h>
    123 #include <sys/kernel.h>
    124 #include <sys/buf.h>
    125 #include <sys/reboot.h>
    126 #include <sys/user.h>
    127 #include <sys/mount.h>
    128 #include <sys/kcore.h>
    129 #include <sys/boot_flag.h>
    130 #include <sys/termios.h>
    131 #include <sys/ksyms.h>
    132 #include <sys/device.h>
    133 
    134 #include <uvm/uvm_extern.h>
    135 
    136 #include <dev/cons.h>
    137 
    138 #include "ksyms.h"
    139 
    140 #if NKSYMS || defined(DDB) || defined(MODULAR)
    141 #include <machine/db_machdep.h>
    142 #include <ddb/db_extern.h>
    143 #endif
    144 
    145 #include <machine/cpu.h>
    146 #include <machine/psl.h>
    147 #include <machine/yamon.h>
    148 
    149 #include <evbmips/malta/autoconf.h>
    150 #include <evbmips/malta/maltareg.h>
    151 #include <evbmips/malta/maltavar.h>
    152 
    153 #include "com.h"
    154 #if NCOM > 0
    155 #include <dev/ic/comreg.h>
    156 #include <dev/ic/comvar.h>
    157 
    158 int	comcnrate = 38400;	/* XXX should be config option */
    159 #endif /* NCOM > 0 */
    160 
    161 
    162 #define REGVAL(x)       *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
    163 
    164 struct malta_config malta_configuration;
    165 
    166 /* For sysctl_hw. */
    167 extern char cpu_model[];
    168 
    169 /* Our exported CPU info; we can have only one. */
    170 struct cpu_info cpu_info_store;
    171 
    172 /* Maps for VM objects. */
    173 struct vm_map *mb_map = NULL;
    174 struct vm_map *phys_map = NULL;
    175 
    176 int	physmem;		/* Total physical memory */
    177 
    178 int	netboot;		/* Are we netbooting? */
    179 
    180 yamon_env_var *yamon_envp;
    181 
    182 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    183 int mem_cluster_cnt;
    184 
    185 void	configure(void);
    186 void	mach_init(int, char **, yamon_env_var *, u_long);
    187 
    188 /*
    189  * safepri is a safe priority for sleep to set for a spin-wait during
    190  * autoconfiguration or after a panic.  Used as an argument to splx().
    191  */
    192 int	safepri = MIPS1_PSL_LOWIPL;
    193 
    194 extern struct user *proc0paddr;
    195 
    196 /*
    197  * Do all the stuff that locore normally does before calling main().
    198  */
    199 void
    200 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    201 {
    202 	struct malta_config *mcp = &malta_configuration;
    203 	bus_space_handle_t sh;
    204 	void *kernend, *v;
    205         u_long first, last;
    206 	char *cp;
    207 	int freqok, i, howto;
    208 	uint8_t *brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
    209 
    210 	extern char edata[], end[];
    211 
    212 	*brkres = 0;	/* Disable BREAK==reset on console */
    213 
    214 	/* Get the propaganda in early! */
    215 	led_display_str("NetBSD");
    216 
    217 	/*
    218 	 * Clear the BSS segment.
    219 	 */
    220 	kernend = (void *)mips_round_page(end);
    221 	memset(edata, 0, (char *)kernend - edata);
    222 
    223 	/* save the yamon environment pointer */
    224 	yamon_envp = envp;
    225 
    226 	/* Use YAMON callbacks for early console I/O */
    227 	cn_tab = &yamon_promcd;
    228 
    229 	/*
    230 	 * Set up the exception vectors and CPU-specific function
    231 	 * vectors early on.  We need the wbflush() vector set up
    232 	 * before comcnattach() is called (or at least before the
    233 	 * first printf() after that is called).
    234 	 * Also clears the I+D caches.
    235 	 */
    236 	mips_vector_init();
    237 
    238 	/* set the VM page size */
    239 	uvm_setpagesize();
    240 
    241 	physmem = btoc(memsize);
    242 
    243 	/*
    244 	 * Use YAMON's CPU frequency if available.
    245 	 */
    246 	freqok = yamon_setcpufreq(1);
    247 
    248 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
    249 	malta_bus_io_init(&mcp->mc_iot, mcp);
    250 	malta_bus_mem_init(&mcp->mc_memt, mcp);
    251 	malta_dma_init(mcp);
    252 
    253 	/*
    254 	 * Calibrate the timer if YAMON failed to tell us.
    255 	 */
    256 	if (!freqok) {
    257 		bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
    258 		malta_cal_timer(&mcp->mc_iot, sh);
    259 		bus_space_unmap(&mcp->mc_iot, sh, 2);
    260 	}
    261 
    262 #if NCOM > 0
    263 	/*
    264 	 * Delay to allow firmware putchars to complete.
    265 	 * FIFO depth * character time.
    266 	 * character time = (1000000 / (defaultrate / 10))
    267 	 */
    268 	delay(160000000 / comcnrate);
    269 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
    270 	    COM_FREQ, COM_TYPE_NORMAL,
    271 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    272 		panic("malta: unable to initialize serial console");
    273 #else
    274 	panic("malta: not configured to use serial console");
    275 #endif /* NCOM > 0 */
    276 
    277 	mem_clusters[0].start = 0;
    278 	mem_clusters[0].size = ctob(physmem);
    279 	mem_cluster_cnt = 1;
    280 
    281 	strcpy(cpu_model, "MIPS Malta Evaluation Board");
    282 
    283 	/*
    284 	 * XXX: check argv[0] - do something if "gdb"???
    285 	 */
    286 
    287 	/*
    288 	 * Look at arguments passed to us and compute boothowto.
    289 	 */
    290 	boothowto = RB_AUTOBOOT;
    291 	for (i = 1; i < argc; i++) {
    292 		for (cp = argv[i]; *cp; cp++) {
    293 			/* Ignore superfluous '-', if there is one */
    294 			if (*cp == '-')
    295 				continue;
    296 
    297 			howto = 0;
    298 			BOOT_FLAG(*cp, howto);
    299 			if (! howto)
    300 				printf("bootflag '%c' not recognised\n", *cp);
    301 			else
    302 				boothowto |= howto;
    303 		}
    304 	}
    305 
    306 	/*
    307 	 * Load the rest of the available pages into the VM system.
    308 	 */
    309 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    310 	last = mem_clusters[0].start + mem_clusters[0].size;
    311 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    312 		VM_FREELIST_DEFAULT);
    313 
    314 	/*
    315 	 * Initialize error message buffer (at end of core).
    316 	 */
    317 	mips_init_msgbuf();
    318 
    319 	pmap_bootstrap();
    320 
    321 	/*
    322 	 * Allocate space for proc0's USPACE.
    323 	 */
    324 	v = (void *)uvm_pageboot_alloc(USPACE);
    325 	lwp0.l_addr = proc0paddr = (struct user *)v;
    326 	lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
    327 	proc0paddr->u_pcb.pcb_context[11] =
    328 	    MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    329 
    330 	/*
    331 	 * Initialize debuggers, and break into them, if appropriate.
    332 	 */
    333 #if defined(DDB)
    334 	if (boothowto & RB_KDB)
    335 		Debugger();
    336 #endif
    337 }
    338 
    339 void
    340 consinit(void)
    341 {
    342 
    343 	/*
    344 	 * Everything related to console initialization is done
    345 	 * in mach_init().
    346 	 */
    347 }
    348 
    349 /*
    350  * Allocate memory for variable-sized tables,
    351  */
    352 void
    353 cpu_startup()
    354 {
    355 	vaddr_t minaddr, maxaddr;
    356 	char pbuf[9];
    357 
    358 	/*
    359 	 * Good {morning,afternoon,evening,night}.
    360 	 */
    361 	printf("%s%s", copyright, version);
    362 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    363 	printf("total memory = %s\n", pbuf);
    364 
    365 	/*
    366 	 * Virtual memory is bootstrapped -- notify the bus spaces
    367 	 * that memory allocation is now safe.
    368 	 */
    369 	malta_configuration.mc_mallocsafe = 1;
    370 
    371 	minaddr = 0;
    372 	/*
    373 	 * Allocate a submap for physio.
    374 	 */
    375 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    376 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    377 
    378 	/*
    379 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    380 	 * are allocated via the pool allocator, and we use KSEG to
    381 	 * map those pages.)
    382 	 */
    383 
    384 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    385 	printf("avail memory = %s\n", pbuf);
    386 }
    387 
    388 int	waittime = -1;
    389 
    390 void
    391 cpu_reboot(howto, bootstr)
    392 	int howto;
    393 	char *bootstr;
    394 {
    395 
    396 	/* Take a snapshot before clobbering any registers. */
    397 	if (curproc)
    398 		savectx((struct user *)curpcb);
    399 
    400 	if (cold) {
    401 		howto |= RB_HALT;
    402 		goto haltsys;
    403 	}
    404 
    405 	/* If "always halt" was specified as a boot flag, obey. */
    406 	if (boothowto & RB_HALT)
    407 		howto |= RB_HALT;
    408 
    409 	boothowto = howto;
    410 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    411 		waittime = 0;
    412 		vfs_shutdown();
    413 
    414 		/*
    415 		 * If we've been adjusting the clock, the todr
    416 		 * will be out of synch; adjust it now.
    417 		 */
    418 		resettodr();
    419 	}
    420 
    421 	splhigh();
    422 
    423 	if (howto & RB_DUMP)
    424 		dumpsys();
    425 
    426 haltsys:
    427 	doshutdownhooks();
    428 
    429 	pmf_system_shutdown(boothowto);
    430 
    431 	if (howto & RB_HALT) {
    432 		printf("\n");
    433 		printf("The operating system has halted.\n");
    434 		printf("Please press any key to reboot.\n\n");
    435 		cnpollc(1);	/* For proper keyboard command handling */
    436 		cngetc();
    437 		cnpollc(0);
    438 	}
    439 
    440 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    441 	yamon_exit(boothowto);
    442 	printf("Oops, back from yamon_exit()\n\nResetting...");
    443 
    444 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
    445 
    446 	/*
    447 	 * Need a small delay here, otherwise we see the first few characters of
    448 	 * the warning below.
    449 	 */
    450 	delay(80000);
    451 
    452 	printf("WARNING: reset failed!\nSpinning...");
    453 
    454 	for (;;)
    455 		/* spin forever */ ;	/* XXX */
    456 }
    457