Home | History | Annotate | Line # | Download | only in malta
machdep.c revision 1.11
      1 /*	$NetBSD: machdep.c,v 1.11 2003/06/14 17:01:11 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1988 University of Utah.
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * the Systems Programming Group of the University of Utah Computer
     45  * Science Department, The Mach Operating System project at
     46  * Carnegie-Mellon University and Ralph Campbell.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     77  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     78  */
     79 
     80 #include "opt_ddb.h"
     81 #include "opt_execfmt.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/buf.h>
     87 #include <sys/reboot.h>
     88 #include <sys/user.h>
     89 #include <sys/mount.h>
     90 #include <sys/kcore.h>
     91 #include <sys/boot_flag.h>
     92 #include <sys/termios.h>
     93 #include <sys/ksyms.h>
     94 
     95 #include <uvm/uvm_extern.h>
     96 
     97 #include <dev/cons.h>
     98 
     99 #include "ksyms.h"
    100 
    101 #if NKSYMS || defined(DDB) || defined(LKM)
    102 #include <machine/db_machdep.h>
    103 #include <ddb/db_extern.h>
    104 #endif
    105 
    106 #include <machine/cpu.h>
    107 #include <machine/psl.h>
    108 #include <machine/yamon.h>
    109 
    110 #include <evbmips/malta/autoconf.h>
    111 #include <evbmips/malta/maltareg.h>
    112 #include <evbmips/malta/maltavar.h>
    113 
    114 #include "com.h"
    115 #if NCOM > 0
    116 #include <dev/ic/comreg.h>
    117 #include <dev/ic/comvar.h>
    118 
    119 int	comcnrate = 38400;	/* XXX should be config option */
    120 #endif /* NCOM > 0 */
    121 
    122 
    123 #define REGVAL(x)       *((__volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
    124 
    125 struct malta_config malta_configuration;
    126 
    127 /* For sysctl_hw. */
    128 extern char cpu_model[];
    129 
    130 /* Our exported CPU info; we can have only one. */
    131 struct cpu_info cpu_info_store;
    132 
    133 /* Maps for VM objects. */
    134 struct vm_map *exec_map = NULL;
    135 struct vm_map *mb_map = NULL;
    136 struct vm_map *phys_map = NULL;
    137 
    138 int	physmem;		/* Total physical memory */
    139 
    140 int	netboot;		/* Are we netbooting? */
    141 
    142 yamon_env_var *yamon_envp;
    143 
    144 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    145 int mem_cluster_cnt;
    146 
    147 void	configure(void);
    148 void	mach_init(int, char **, yamon_env_var *, u_long);
    149 
    150 /*
    151  * safepri is a safe priority for sleep to set for a spin-wait during
    152  * autoconfiguration or after a panic.  Used as an argument to splx().
    153  */
    154 int	safepri = MIPS1_PSL_LOWIPL;
    155 
    156 extern struct user *proc0paddr;
    157 
    158 /*
    159  * Do all the stuff that locore normally does before calling main().
    160  */
    161 void
    162 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    163 {
    164 	struct malta_config *mcp = &malta_configuration;
    165 	bus_space_handle_t sh;
    166 	caddr_t kernend, v;
    167         u_long first, last;
    168 	vsize_t size;
    169 	char *cp;
    170 	int i, howto;
    171 	uint8_t *brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
    172 
    173 	extern char edata[], end[];
    174 
    175 	*brkres = 0;	/* Disable BREAK==reset on console */
    176 
    177 	/* Get the propaganda in early! */
    178 	led_display_str("NetBSD");
    179 
    180 	/*
    181 	 * Clear the BSS segment.
    182 	 */
    183 	kernend = (caddr_t)mips_round_page(end);
    184 	memset(edata, 0, kernend - edata);
    185 
    186 	/* save the yamon environment pointer */
    187 	yamon_envp = envp;
    188 
    189 	/* Use YAMON callbacks for early console I/O */
    190 	cn_tab = &yamon_promcd;
    191 
    192 	/*
    193 	 * Set up the exception vectors and cpu-specific function
    194 	 * vectors early on.  We need the wbflush() vector set up
    195 	 * before comcnattach() is called (or at least before the
    196 	 * first printf() after that is called).
    197 	 * Also clears the I+D caches.
    198 	 */
    199 	mips_vector_init();
    200 
    201 	/* set the VM page size */
    202 	uvm_setpagesize();
    203 
    204 	physmem = btoc(memsize);
    205 
    206 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
    207 	malta_bus_io_init(&mcp->mc_iot, mcp);
    208 	malta_bus_mem_init(&mcp->mc_memt, mcp);
    209 	malta_dma_init(mcp);
    210 
    211 	/*
    212 	 * Calibrate the timer, delay() relies on this.
    213 	 */
    214 	bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
    215 	malta_cal_timer(&mcp->mc_iot, sh);
    216 	bus_space_unmap(&mcp->mc_iot, sh, 2);
    217 
    218 #if NCOM > 0
    219 	/*
    220 	 * Delay to allow firmware putchars to complete.
    221 	 * FIFO depth * character time.
    222 	 * character time = (1000000 / (defaultrate / 10))
    223 	 */
    224 	delay(160000000 / comcnrate);
    225 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
    226 	    COM_FREQ, COM_TYPE_NORMAL,
    227 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    228 		panic("malta: unable to initialize serial console");
    229 #else
    230 	panic("malta: not configured to use serial console");
    231 #endif /* NCOM > 0 */
    232 
    233 	consinit();
    234 
    235 	mem_clusters[0].start = 0;
    236 	mem_clusters[0].size = ctob(physmem);
    237 	mem_cluster_cnt = 1;
    238 
    239 	strcpy(cpu_model, "MIPS Malta Evaluation Board");
    240 
    241 	/*
    242 	 * XXX: check argv[0] - do something if "gdb"???
    243 	 */
    244 
    245 	/*
    246 	 * Look at arguments passed to us and compute boothowto.
    247 	 */
    248 	boothowto = RB_AUTOBOOT;
    249 	for (i = 1; i < argc; i++) {
    250 		for (cp = argv[i]; *cp; cp++) {
    251 			/* Ignore superfluous '-', if there is one */
    252 			if (*cp == '-')
    253 				continue;
    254 
    255 			howto = 0;
    256 			BOOT_FLAG(*cp, howto);
    257 			if (! howto)
    258 				printf("bootflag '%c' not recognised\n", *cp);
    259 			else
    260 				boothowto |= howto;
    261 		}
    262 	}
    263 
    264 	/*
    265 	 * Load the rest of the available pages into the VM system.
    266 	 */
    267 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    268 	last = mem_clusters[0].start + mem_clusters[0].size;
    269 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    270 		VM_FREELIST_DEFAULT);
    271 
    272 	/*
    273 	 * Initialize error message buffer (at end of core).
    274 	 */
    275 	mips_init_msgbuf();
    276 
    277 	/*
    278 	 * Compute the size of system data structures.  pmap_bootstrap()
    279 	 * needs some of this information.
    280 	 */
    281 	size = (vsize_t)allocsys(NULL, NULL);
    282 
    283 	pmap_bootstrap();
    284 
    285 	/*
    286 	 * Allocate space for proc0's USPACE.
    287 	 */
    288 	v = (caddr_t)uvm_pageboot_alloc(USPACE);
    289 	lwp0.l_addr = proc0paddr = (struct user *)v;
    290 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    291 	curpcb = &lwp0.l_addr->u_pcb;
    292 	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    293 
    294 	/*
    295 	 * Allocate space for system data structures.  These data structures
    296 	 * are allocated here instead of cpu_startup() because physical
    297 	 * memory is directly addressable.  We don't have to map these into
    298 	 * the virtual address space.
    299 	 */
    300 	v = (caddr_t)uvm_pageboot_alloc(size);
    301 	if ((allocsys(v, NULL) - v) != size)
    302 		panic("mach_init: table size inconsistency");
    303 
    304 	/*
    305 	 * Initialize debuggers, and break into them, if appropriate.
    306 	 */
    307 #if NKSYMS || defined(DDB) || defined(LKM)
    308 	ksyms_init(0, 0, 0);
    309 #endif
    310 
    311 #if defined(DDB)
    312 	if (boothowto & RB_KDB)
    313 		Debugger();
    314 #endif
    315 }
    316 
    317 void
    318 consinit(void)
    319 {
    320 
    321 	/*
    322 	 * Everything related to console initialization is done
    323 	 * in mach_init().
    324 	 */
    325 }
    326 
    327 /*
    328  * Allocate memory for variable-sized tables,
    329  */
    330 void
    331 cpu_startup()
    332 {
    333 	u_int i, base, residual;
    334 	vaddr_t minaddr, maxaddr;
    335 	vsize_t size;
    336 	char pbuf[9];
    337 
    338 	/*
    339 	 * Good {morning,afternoon,evening,night}.
    340 	 */
    341 	printf(version);
    342 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    343 	printf("%s memory", pbuf);
    344 
    345 	/*
    346 	 * Virtual memory is bootstrapped -- notify the bus spaces
    347 	 * that memory allocation is now safe.
    348 	 */
    349 	malta_configuration.mc_mallocsafe = 1;
    350 
    351 	/*
    352 	 * Allocate virtual address space for file I/O buffers.
    353 	 * Note they are different than the array of headers, 'buf',
    354 	 * and usually occupy more virtual memory than physical.
    355 	 */
    356 	size = MAXBSIZE * nbuf;
    357 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
    358 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    359 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    360 		    UVM_ADV_NORMAL, 0)) != 0)
    361 		panic("startup: cannot allocate VM for buffers");
    362 	minaddr = (vaddr_t)buffers;
    363 	base = bufpages / nbuf;
    364 	residual = bufpages % nbuf;
    365 	for (i = 0; i < nbuf; i++) {
    366 		vsize_t curbufsize;
    367 		vaddr_t curbuf;
    368 		struct vm_page *pg;
    369 
    370 		/*
    371 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    372 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    373 		 * for the first "residual" buffers, and then we allocate
    374 		 * "base" pages for the rest.
    375 		 */
    376 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
    377 		curbufsize = PAGE_SIZE * ((i < residual) ? (base + 1) : base);
    378 
    379 		while (curbufsize) {
    380 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    381 			if (pg == NULL)
    382 				panic("cpu_startup: not enough memory for "
    383 					"buffer cache");
    384 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    385 				       VM_PROT_READ|VM_PROT_WRITE);
    386 			curbuf += PAGE_SIZE;
    387 			curbufsize -= PAGE_SIZE;
    388 		}
    389 	}
    390 	pmap_update(pmap_kernel());
    391 
    392 	/*
    393 	 * Allocate a submap for exec arguments.  This map effectively
    394 	 * limits the number of processes exec'ing at any time.
    395 	 */
    396 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    397 				    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    398 	/*
    399 	 * Allocate a submap for physio.
    400 	 */
    401 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    402 				    VM_PHYS_SIZE, 0, FALSE, NULL);
    403 
    404 	/*
    405 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
    406 	 * are allocated via the pool allocator, and we use KSEG to
    407 	 * map those pages.)
    408 	 */
    409 
    410 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    411 	printf(", %s free", pbuf);
    412 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    413 	printf(", %s in %u buffers\n", pbuf, nbuf);
    414 
    415 	/*
    416 	 * Set up buffers, so they can be used to read disk labels.
    417 	 */
    418 	bufinit();
    419 }
    420 
    421 int	waittime = -1;
    422 
    423 void
    424 cpu_reboot(howto, bootstr)
    425 	int howto;
    426 	char *bootstr;
    427 {
    428 
    429 	/* Take a snapshot before clobbering any registers. */
    430 	if (curproc)
    431 		savectx((struct user *)curpcb);
    432 
    433 	if (cold) {
    434 		howto |= RB_HALT;
    435 		goto haltsys;
    436 	}
    437 
    438 	/* If "always halt" was specified as a boot flag, obey. */
    439 	if (boothowto & RB_HALT)
    440 		howto |= RB_HALT;
    441 
    442 	boothowto = howto;
    443 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    444 		waittime = 0;
    445 		vfs_shutdown();
    446 
    447 		/*
    448 		 * If we've been adjusting the clock, the todr
    449 		 * will be out of synch; adjust it now.
    450 		 */
    451 		resettodr();
    452 	}
    453 
    454 	splhigh();
    455 
    456 	if (howto & RB_DUMP)
    457 		dumpsys();
    458 
    459 haltsys:
    460 	doshutdownhooks();
    461 
    462 	if (howto & RB_HALT) {
    463 		printf("\n");
    464 		printf("The operating system has halted.\n");
    465 		printf("Please press any key to reboot.\n\n");
    466 		cnpollc(1);	/* For proper keyboard command handling */
    467 		cngetc();
    468 		cnpollc(0);
    469 	}
    470 
    471 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    472 	yamon_exit(boothowto);
    473 	printf("Oops, back from yamon_exit()\n\nResetting...");
    474 
    475 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
    476 
    477 	/*
    478 	 * Need a small delay here, otherwise we see the first few characters of
    479 	 * the warning below.
    480 	 */
    481 	delay(80000);
    482 
    483 	printf("WARNING: reset failed!\nSpinning...");
    484 
    485 	for (;;)
    486 		/* spin forever */ ;	/* XXX */
    487 }
    488