machdep.c revision 1.37
1/*	$NetBSD: machdep.c,v 1.37 2000/01/19 20:05:44 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1992, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department, The Mach Operating System project at
11 * Carnegie-Mellon University and Ralph Campbell.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed by the University of
24 *	California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
42 */
43
44#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
45
46__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.37 2000/01/19 20:05:44 thorpej Exp $");
47
48/* from: Utah Hdr: machdep.c 1.63 91/04/24 */
49
50#include "fs_mfs.h"
51#include "opt_ddb.h"
52#include "opt_execfmt.h"
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/signalvar.h>
57#include <sys/kernel.h>
58#include <sys/map.h>
59#include <sys/proc.h>
60#include <sys/buf.h>
61#include <sys/reboot.h>
62#include <sys/conf.h>
63#include <sys/file.h>
64#include <sys/malloc.h>
65#include <sys/mbuf.h>
66#include <sys/msgbuf.h>
67#include <sys/ioctl.h>
68#include <sys/device.h>
69#include <sys/user.h>
70#include <sys/exec.h>
71#include <vm/vm.h>
72#include <sys/sysctl.h>
73#include <sys/mount.h>
74#include <sys/syscallargs.h>
75#include <sys/kcore.h>
76
77#include <vm/vm_kern.h>
78
79#include <uvm/uvm_extern.h>
80
81#include <ufs/mfs/mfs_extern.h>		/* mfs_initminiroot() */
82
83#include <machine/cpu.h>
84#include <machine/reg.h>
85#include <machine/psl.h>
86#include <machine/pte.h>
87#include <machine/autoconf.h>
88#include <machine/bootinfo.h>
89#include <machine/apbus.h>
90#include <machine/apcall.h>
91#include <mips/locore.h>		/* wbflush() */
92
93#ifdef DDB
94#include <machine/db_machdep.h>
95#include <ddb/db_access.h>
96#include <ddb/db_extern.h>
97#include <ddb/db_sym.h>
98#endif
99
100#include <machine/adrsmap.h>
101#include <machine/machConst.h>
102#include <machine/intr.h>
103#include <newsmips/newsmips/clockreg.h>
104#include <newsmips/newsmips/machid.h>
105#include <dev/cons.h>
106
107/* the following is used externally (sysctl_hw) */
108char machine[] = MACHINE;	/* from <machine/param.h> */
109char machine_arch[] = MACHINE_ARCH;
110char cpu_model[30];
111
112/* maps for VM objects */
113
114vm_map_t exec_map = NULL;
115vm_map_t mb_map = NULL;
116vm_map_t phys_map = NULL;
117
118char *bootinfo = NULL;		/* pointer to bootinfo structure */
119int physmem;			/* max supported memory, changes to actual */
120int systype;			/* what type of NEWS we are */
121struct apbus_sysinfo *_sip = NULL;
122
123phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
124int mem_cluster_cnt;
125
126struct idrom idrom;
127void (*enable_intr) __P((void));
128void (*disable_intr) __P((void));
129
130/* System type dependent initializations. */
131extern void news3400_init __P((void));
132extern void news5000_init __P((void));
133
134/*
135 * Interrupt-blocking functions defined in locore. These names aren't used
136 * directly except here and in interrupt handlers.
137 */
138
139/* Block out nested interrupt-enable bits. */
140extern int	cpu_spl0 __P((void)), cpu_spl1 __P((void));
141extern int	cpu_spl2 __P((void)), cpu_spl3 __P((void));
142extern int	splhigh __P((void));
143
144void to_monitor __P((int)) __attribute__((__noreturn__));
145
146/*
147 *  Local functions.
148 */
149
150/* initialize bss, etc. from kernel start, before main() is called. */
151void mach_init __P((int, int, int, int));
152void prom_halt __P((int)) __attribute__((__noreturn__));
153static void newsmips_softintr __P((int));
154
155#ifdef DEBUG
156/* stacktrace code violates prototypes to get callee's registers */
157extern void stacktrace __P((void)); /*XXX*/
158#endif
159
160/*
161 * safepri is a safe priority for sleep to set for a spin-wait
162 * during autoconfiguration or after a panic.  Used as an argument to splx().
163 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also
164 * disables mips1 FPU interrupts.
165 */
166int safepri = MIPS3_PSL_LOWIPL;		/* XXX */
167
168extern struct user *proc0paddr;
169extern u_long bootdev;
170extern char edata[], end[];
171
172/*
173 * Do all the stuff that locore normally does before calling main().
174 * Process arguments passed to us by the prom monitor.
175 * Return the first page address following the system.
176 */
177void
178mach_init(x_boothowto, x_bootdev, x_bootname, x_maxmem)
179	int x_boothowto;
180	int x_bootdev;
181	int x_bootname;
182	int x_maxmem;
183{
184	register int i;
185	u_long first, last;
186	caddr_t kernend, v;
187	vsize_t size;
188	struct btinfo_magic *bi_magic;
189	struct btinfo_bootarg *bi_arg;
190	struct btinfo_systype *bi_systype;
191#ifdef DDB
192	struct btinfo_symtab *bi_sym;
193	int nsym = 0;
194	char *ssym, *esym;
195#endif
196
197	/* clear the BSS segment */
198	bzero(edata, end - edata);
199
200	systype = NEWS3400;			/* XXX compatibility */
201
202	bootinfo = (void *)BOOTINFO_ADDR;	/* XXX */
203	bi_magic = lookup_bootinfo(BTINFO_MAGIC);
204	if (bi_magic && bi_magic->magic == BOOTINFO_MAGIC) {
205		bi_arg = lookup_bootinfo(BTINFO_BOOTARG);
206		if (bi_arg) {
207			x_boothowto = bi_arg->howto;
208			x_bootdev = bi_arg->bootdev;
209			x_maxmem = bi_arg->maxmem;
210		}
211#ifdef DDB
212		bi_sym = lookup_bootinfo(BTINFO_SYMTAB);
213		if (bi_sym) {
214			nsym = bi_sym->nsym;
215			ssym = (void *)bi_sym->ssym;
216			esym = (void *)bi_sym->esym;
217		}
218#endif
219
220		bi_systype = lookup_bootinfo(BTINFO_SYSTYPE);
221		if (bi_systype)
222			systype = bi_systype->type;
223	}
224
225#ifdef news5000
226	if (systype == NEWS5000) {
227		_sip = (void *)bi_arg->sip;
228		x_maxmem = _sip->apbsi_memsize;
229		x_maxmem -= 0x00100000;	/* reserve 1MB for ROM monitor */
230
231		consinit();
232	}
233#endif
234
235	/*
236	 * Save parameters into kernel work area.
237	 */
238	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_MAXMEMSIZE_ADDR)) = x_maxmem;
239	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTDEV_ADDR)) = x_bootdev;
240	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTSW_ADDR)) = x_boothowto;
241
242	kernend = (caddr_t)mips_round_page(end);
243#ifdef DDB
244	if (nsym)
245		kernend = (caddr_t)mips_round_page(esym);
246#endif
247
248	/*
249	 * Set the VM page size.
250	 */
251	uvm_setpagesize();
252
253	boothowto = x_boothowto;
254	bootdev = x_bootdev;
255	physmem = btoc(x_maxmem);
256
257	/*
258	 * Now that we know how much memory we have, initialize the
259	 * mem cluster array.
260	 */
261	mem_clusters[0].start = 0;		/* XXX is this correct? */
262	mem_clusters[0].size  = ctob(physmem);
263	mem_cluster_cnt = 1;
264
265	/*
266	 * Copy exception-dispatch code down to exception vector.
267	 * Initialize locore-function vector.
268	 * Clear out the I and D caches.
269	 */
270	mips_vector_init();
271#if 0
272	if (systype == NEWS5000) {
273		mips_L2CacheSize = 1024 * 1024;		/* XXX to be safe */
274		mips3_FlushCache();
275	}
276#endif
277
278#ifdef DDB
279	/*
280	 * Initialize machine-dependent DDB commands, in case of early panic.
281	 */
282	db_machine_init();
283	if (nsym)
284		ddb_init(esym - ssym, ssym, esym);
285#endif
286
287	boothowto &= ~RB_ASKNAME;	/* for lack of cn_getc */
288#ifdef KADB
289	boothowto |= RB_KDB;
290#endif
291
292#ifdef MFS
293	/*
294	 * Check to see if a mini-root was loaded into memory. It resides
295	 * at the start of the next page just after the end of BSS.
296	 */
297	if (boothowto & RB_MINIROOT) {
298		boothowto |= RB_DFLTROOT;
299		kernend += round_page(mfs_initminiroot(kernend));
300	}
301#endif
302
303	/*
304	 * Alloc u pages for proc0 stealing KSEG0 memory.
305	 */
306	proc0.p_addr = proc0paddr = (struct user *)kernend;
307	proc0.p_md.md_regs =
308	    (struct frame *)((caddr_t)kernend + UPAGES * PAGE_SIZE) - 1;
309	curpcb = &proc0.p_addr->u_pcb;
310	memset(kernend, 0, UPAGES * PAGE_SIZE);
311
312	kernend += UPAGES * PAGE_SIZE;
313
314	/*
315	 * Load the rest of the available pages into the VM system.
316	 */
317	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
318	last = mem_clusters[0].start + mem_clusters[0].size;
319	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
320	    VM_FREELIST_DEFAULT);
321
322	/*
323	 * Initialize error message buffer (at end of core).
324	 */
325	mips_init_msgbuf();
326
327	/*
328	 * Allocate space for system data structures.  These data structures
329	 * are allocated here instead of cpu_startup() because physical
330	 * memory is directly addressable.  We don't have to map these into
331	 * virtual address space.
332	 */
333	size = (vsize_t)allocsys(NULL, NULL);
334	v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
335	if ((allocsys(v, NULL) - v) != size)
336		panic("mach_init: table size inconsistency");
337
338	/*
339	 * Determine what model of computer we are running on.
340	 */
341	switch (systype) {
342#ifdef news3400
343	case NEWS3400:
344		news3400_init();
345		break;
346#endif
347
348#ifdef news5000
349	case NEWS5000:
350		news5000_init();
351		break;
352#endif
353	}
354
355	i = idrom.id_modelid;
356
357	switch (i) {
358
359	default:
360		printf("kernel not configured for systype 0x%x\n", i);
361		/* cpu_reboot(RB_HALT | RB_NOSYNC, NULL); */
362
363#ifdef news5000
364	case 2: /* NWS-5000U/W */
365	case 5: /* NWS-5000R */
366	case 7: /* NWS-5000X */
367	case 9: /* NWS-5900X */
368	case 11:/* NWS-5000G */
369		/*
370		 * Set up interrupt handling and I/O addresses.
371		 */
372		mips_hardware_intr = news5000_intr;
373		mips_software_intr = newsmips_softintr;
374		strcpy(cpu_model, "news5000");
375		cpuspeed = 50;	/* ??? XXX */
376		break;
377#endif
378
379#ifdef news3400
380	case 3: /* NWS-3410 */
381	case 6: /* NWS-3470 */
382		/*
383		 * Set up interrupt handling and I/O addresses.
384		 */
385		mips_hardware_intr = news3400_intr;
386		mips_software_intr = newsmips_softintr;
387		strcpy(cpu_model, "news3400");
388		cpuspeed = 10;
389		break;
390#endif
391	}
392
393	/*
394	 * Initialize the virtual memory system.
395	 */
396	pmap_bootstrap();
397}
398
399/*
400 * cpu_startup: allocate memory for variable-sized tables,
401 * initialize cpu, and do autoconfiguration.
402 */
403void
404cpu_startup()
405{
406	register unsigned i;
407	int base, residual;
408	vaddr_t minaddr, maxaddr;
409	vsize_t size;
410	char pbuf[9];
411#ifdef DEBUG
412	extern int pmapdebug;
413	int opmapdebug = pmapdebug;
414
415	pmapdebug = 0;
416#endif
417
418	/*
419	 * Good {morning,afternoon,evening,night}.
420	 */
421	printf(version);
422	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
423	printf("total memory = %s\n", pbuf);
424
425	/*
426	 * Allocate virtual address space for file I/O buffers.
427	 * Note they are different than the array of headers, 'buf',
428	 * and usually occupy more virtual memory than physical.
429	 */
430	size = MAXBSIZE * nbuf;
431	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
432		    NULL, UVM_UNKNOWN_OFFSET,
433		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
434				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
435		panic("startup: cannot allocate VM for buffers");
436	minaddr = (vaddr_t)buffers;
437	base = bufpages / nbuf;
438	residual = bufpages % nbuf;
439	for (i = 0; i < nbuf; i++) {
440		vsize_t curbufsize;
441		vaddr_t curbuf;
442		struct vm_page *pg;
443
444		/*
445		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
446		 * that MAXBSIZE space, we allocate and map (base+1) pages
447		 * for the first "residual" buffers, and then we allocate
448		 * "base" pages for the rest.
449		 */
450		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
451		curbufsize = NBPG * ((i < residual) ? (base+1) : base);
452
453		while (curbufsize) {
454			pg = uvm_pagealloc(NULL, 0, NULL, 0);
455			if (pg == NULL)
456				panic("cpu_startup: not enough memory for "
457				    "buffer cache");
458			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
459				       VM_PROT_READ|VM_PROT_WRITE);
460			curbuf += PAGE_SIZE;
461			curbufsize -= PAGE_SIZE;
462		}
463	}
464	/*
465	 * Allocate a submap for exec arguments.  This map effectively
466	 * limits the number of processes exec'ing at any time.
467	 */
468	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
469				   16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
470	/*
471	 * Allocate a submap for physio
472	 */
473	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
474				   VM_PHYS_SIZE, 0, FALSE, NULL);
475
476	/*
477	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
478	 * are allocated via the pool allocator, and we use KSEG to
479	 * map those pages.
480	 */
481
482#ifdef DEBUG
483	pmapdebug = opmapdebug;
484#endif
485	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
486	printf("avail memory = %s\n", pbuf);
487	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
488	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
489
490	/*
491	 * Set up buffers, so they can be used to read disk labels.
492	 */
493	bufinit();
494}
495
496
497/*
498 * machine dependent system variables.
499 */
500int
501cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
502	int *name;
503	u_int namelen;
504	void *oldp;
505	size_t *oldlenp;
506	void *newp;
507	size_t newlen;
508	struct proc *p;
509{
510	/* all sysctl names at this level are terminal */
511	if (namelen != 1)
512		return (ENOTDIR);		/* overloaded */
513
514	switch (name[0]) {
515
516	default:
517		return (EOPNOTSUPP);
518	}
519	/* NOTREACHED */
520}
521
522/*
523 * lookup_bootinfo:
524 * Look up information in bootinfo of boot loader.
525 */
526void *
527lookup_bootinfo(type)
528	int type;
529{
530	struct btinfo_common *bt;
531	char *help = bootinfo;
532
533	/* Check for a bootinfo record first. */
534	if (help == NULL)
535		return (NULL);
536
537	do {
538		bt = (struct btinfo_common *)help;
539		if (bt->type == type)
540			return ((void *)help);
541		help += bt->next;
542	} while (bt->next != 0 &&
543		(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
544
545	return (NULL);
546}
547
548/*
549 * call PROM to halt or reboot.
550 */
551void
552prom_halt(howto)
553	int howto;
554
555{
556#ifdef news5000
557	if (systype == NEWS5000)
558		apcall_exit(howto);
559#endif
560#ifdef news3400
561	if (systype == NEWS3400)
562		to_monitor(howto);
563#endif
564	for (;;);
565}
566
567int	waittime = -1;
568
569void
570cpu_reboot(howto, bootstr)
571	volatile int howto;
572	char *bootstr;
573{
574
575	/* take a snap shot before clobbering any registers */
576	if (curproc)
577		savectx((struct user *)curpcb);
578
579#ifdef DEBUG
580	if (panicstr)
581		stacktrace();
582#endif
583
584	/* If system is cold, just halt. */
585	if (cold) {
586		howto |= RB_HALT;
587		goto haltsys;
588	}
589
590	/* If "always halt" was specified as a boot flag, obey. */
591	if ((boothowto & RB_HALT) != 0)
592		howto |= RB_HALT;
593
594	boothowto = howto;
595	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
596		/*
597		 * Synchronize the disks....
598		 */
599		waittime = 0;
600		vfs_shutdown();
601
602		/*
603		 * If we've been adjusting the clock, the todr
604		 * will be out of synch; adjust it now.
605		 */
606		resettodr();
607	}
608
609	/* Disable interrupts. */
610	disable_intr();
611
612	splhigh();
613
614	/* If rebooting and a dump is requested do it. */
615#if 0
616	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
617#else
618	if (howto & RB_DUMP)
619#endif
620		dumpsys();
621
622haltsys:
623
624	/* run any shutdown hooks */
625	doshutdownhooks();
626
627	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
628		prom_halt(0x80);	/* rom monitor RB_PWOFF */
629
630	/* Finally, halt/reboot the system. */
631	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
632	prom_halt(howto & RB_HALT);
633	/*NOTREACHED*/
634}
635
636/*
637 * Return the best possible estimate of the time in the timeval
638 * to which tvp points.  Unfortunately, we can't read the hardware registers.
639 * We guarantee that the time will be greater than the value obtained by a
640 * previous call.
641 */
642void
643microtime(tvp)
644	register struct timeval *tvp;
645{
646	int s = splclock();
647	static struct timeval lasttime;
648
649	*tvp = time;
650#if 0
651	tvp->tv_usec += clkread();
652	if (tvp->tv_usec >= 1000000) {
653		tvp->tv_usec -= 1000000;
654		tvp->tv_sec++;
655	}
656#endif
657
658	if (tvp->tv_sec == lasttime.tv_sec &&
659	    tvp->tv_usec <= lasttime.tv_usec &&
660	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
661		tvp->tv_sec++;
662		tvp->tv_usec -= 1000000;
663	}
664	lasttime = *tvp;
665	splx(s);
666}
667
668void
669delay(n)
670	int n;
671{
672	DELAY(n);
673}
674
675#ifdef EXEC_ECOFF
676#include <sys/exec_ecoff.h>
677
678int
679cpu_exec_ecoff_hook(p, epp)
680	struct proc *p;
681	struct exec_package *epp;
682{
683	extern struct emul emul_netbsd;
684
685	epp->ep_emul = &emul_netbsd;
686
687	return 0;
688}
689#endif
690
691#include "zsc.h"
692
693int zssoft __P((void));
694
695void
696newsmips_softintr(sisr)
697	int sisr;
698{
699#if NZSC > 0
700	if (sisr & SOFTISR_ZS)
701		zssoft();
702#endif
703}
704