machdep.c revision 1.42
1/*	$NetBSD: machdep.c,v 1.42 2000/06/26 14:20:51 mrg 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.42 2000/06/26 14:20:51 mrg 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 <uvm/uvm_extern.h>
78
79#include <ufs/mfs/mfs_extern.h>		/* mfs_initminiroot() */
80
81#include <machine/cpu.h>
82#include <machine/reg.h>
83#include <machine/psl.h>
84#include <machine/pte.h>
85#include <machine/autoconf.h>
86#include <machine/bootinfo.h>
87#include <machine/apbus.h>
88#include <machine/apcall.h>
89#include <mips/locore.h>		/* wbflush() */
90
91#ifdef DDB
92#include <machine/db_machdep.h>
93#include <ddb/db_access.h>
94#include <ddb/db_extern.h>
95#include <ddb/db_sym.h>
96#endif
97
98#include <machine/adrsmap.h>
99#include <machine/machConst.h>
100#include <machine/intr.h>
101#include <newsmips/newsmips/clockreg.h>
102#include <newsmips/newsmips/machid.h>
103#include <dev/cons.h>
104
105/* the following is used externally (sysctl_hw) */
106char machine[] = MACHINE;	/* from <machine/param.h> */
107char machine_arch[] = MACHINE_ARCH;
108char cpu_model[30];
109
110/* Our exported CPU info; we can have only one. */
111struct cpu_info cpu_info_store;
112
113/* maps for VM objects */
114
115vm_map_t exec_map = NULL;
116vm_map_t mb_map = NULL;
117vm_map_t phys_map = NULL;
118
119char *bootinfo = NULL;		/* pointer to bootinfo structure */
120int physmem;			/* max supported memory, changes to actual */
121int systype;			/* what type of NEWS we are */
122struct apbus_sysinfo *_sip = NULL;
123
124phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
125int mem_cluster_cnt;
126
127struct idrom idrom;
128void (*enable_intr) __P((void));
129void (*disable_intr) __P((void));
130
131/* System type dependent initializations. */
132extern void news3400_init __P((void));
133extern void news5000_init __P((void));
134
135static void (*hardware_intr) __P((u_int, u_int, u_int, u_int));
136u_int ssir;
137
138/*
139 *  Local functions.
140 */
141
142/* initialize bss, etc. from kernel start, before main() is called. */
143void mach_init __P((int, int, int, int));
144
145void cpu_intr  __P((u_int32_t, u_int32_t, u_int32_t, u_int32_t));
146
147void prom_halt __P((int)) __attribute__((__noreturn__));
148void to_monitor __P((int)) __attribute__((__noreturn__));
149
150#ifdef DEBUG
151/* stacktrace code violates prototypes to get callee's registers */
152extern void stacktrace __P((void)); /*XXX*/
153#endif
154
155/*
156 * safepri is a safe priority for sleep to set for a spin-wait
157 * during autoconfiguration or after a panic.  Used as an argument to splx().
158 * XXX disables interrupt 5 to disable mips3 on-chip clock, which also
159 * disables mips1 FPU interrupts.
160 */
161int safepri = MIPS3_PSL_LOWIPL;		/* XXX */
162
163extern struct user *proc0paddr;
164extern u_long bootdev;
165extern char edata[], end[];
166
167/*
168 * Do all the stuff that locore normally does before calling main().
169 * Process arguments passed to us by the prom monitor.
170 * Return the first page address following the system.
171 */
172void
173mach_init(x_boothowto, x_bootdev, x_bootname, x_maxmem)
174	int x_boothowto;
175	int x_bootdev;
176	int x_bootname;
177	int x_maxmem;
178{
179	register int i;
180	u_long first, last;
181	caddr_t kernend, v;
182	vsize_t size;
183	struct btinfo_magic *bi_magic;
184	struct btinfo_bootarg *bi_arg;
185	struct btinfo_systype *bi_systype;
186#ifdef DDB
187	struct btinfo_symtab *bi_sym;
188	int nsym = 0;
189	char *ssym, *esym;
190#endif
191
192	/* clear the BSS segment */
193	bzero(edata, end - edata);
194
195	systype = NEWS3400;			/* XXX compatibility */
196
197	bootinfo = (void *)BOOTINFO_ADDR;	/* XXX */
198	bi_magic = lookup_bootinfo(BTINFO_MAGIC);
199	if (bi_magic && bi_magic->magic == BOOTINFO_MAGIC) {
200		bi_arg = lookup_bootinfo(BTINFO_BOOTARG);
201		if (bi_arg) {
202			x_boothowto = bi_arg->howto;
203			x_bootdev = bi_arg->bootdev;
204			x_maxmem = bi_arg->maxmem;
205		}
206#ifdef DDB
207		bi_sym = lookup_bootinfo(BTINFO_SYMTAB);
208		if (bi_sym) {
209			nsym = bi_sym->nsym;
210			ssym = (void *)bi_sym->ssym;
211			esym = (void *)bi_sym->esym;
212		}
213#endif
214
215		bi_systype = lookup_bootinfo(BTINFO_SYSTYPE);
216		if (bi_systype)
217			systype = bi_systype->type;
218	}
219
220#ifdef news5000
221	if (systype == NEWS5000) {
222		_sip = (void *)bi_arg->sip;
223		x_maxmem = _sip->apbsi_memsize;
224		x_maxmem -= 0x00100000;	/* reserve 1MB for ROM monitor */
225
226		consinit();
227	}
228#endif
229
230	/*
231	 * Save parameters into kernel work area.
232	 */
233	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_MAXMEMSIZE_ADDR)) = x_maxmem;
234	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTDEV_ADDR)) = x_bootdev;
235	*(int *)(MIPS_PHYS_TO_KSEG1(MACH_BOOTSW_ADDR)) = x_boothowto;
236
237	kernend = (caddr_t)mips_round_page(end);
238#ifdef DDB
239	if (nsym)
240		kernend = (caddr_t)mips_round_page(esym);
241#endif
242
243	/*
244	 * Set the VM page size.
245	 */
246	uvm_setpagesize();
247
248	boothowto = x_boothowto;
249	bootdev = x_bootdev;
250	physmem = btoc(x_maxmem);
251
252	/*
253	 * Now that we know how much memory we have, initialize the
254	 * mem cluster array.
255	 */
256	mem_clusters[0].start = 0;		/* XXX is this correct? */
257	mem_clusters[0].size  = ctob(physmem);
258	mem_cluster_cnt = 1;
259
260	/*
261	 * Copy exception-dispatch code down to exception vector.
262	 * Initialize locore-function vector.
263	 * Clear out the I and D caches.
264	 */
265	mips_vector_init();
266#if 0
267	if (systype == NEWS5000) {
268		mips_L2CacheSize = 1024 * 1024;		/* XXX to be safe */
269		mips3_FlushCache();
270	}
271#endif
272
273#ifdef DDB
274	/*
275	 * Initialize machine-dependent DDB commands, in case of early panic.
276	 */
277	db_machine_init();
278	if (nsym)
279		ddb_init(esym - ssym, ssym, esym);
280#endif
281
282	boothowto &= ~RB_ASKNAME;	/* for lack of cn_getc */
283#ifdef KADB
284	boothowto |= RB_KDB;
285#endif
286
287#ifdef MFS
288	/*
289	 * Check to see if a mini-root was loaded into memory. It resides
290	 * at the start of the next page just after the end of BSS.
291	 */
292	if (boothowto & RB_MINIROOT) {
293		boothowto |= RB_DFLTROOT;
294		kernend += round_page(mfs_initminiroot(kernend));
295	}
296#endif
297
298	/*
299	 * Alloc u pages for proc0 stealing KSEG0 memory.
300	 */
301	proc0.p_addr = proc0paddr = (struct user *)kernend;
302	proc0.p_md.md_regs = (struct frame *)(kernend + USPACE) - 1;
303	memset(proc0.p_addr, 0, USPACE);
304	curpcb = &proc0.p_addr->u_pcb;
305	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
306
307	kernend += USPACE;
308
309	/*
310	 * Load the rest of the available pages into the VM system.
311	 */
312	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
313	last = mem_clusters[0].start + mem_clusters[0].size;
314	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
315	    VM_FREELIST_DEFAULT);
316
317	/*
318	 * Initialize error message buffer (at end of core).
319	 */
320	mips_init_msgbuf();
321
322	/*
323	 * Allocate space for system data structures.  These data structures
324	 * are allocated here instead of cpu_startup() because physical
325	 * memory is directly addressable.  We don't have to map these into
326	 * virtual address space.
327	 */
328	size = (vsize_t)allocsys(NULL, NULL);
329	v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
330	if ((allocsys(v, NULL) - v) != size)
331		panic("mach_init: table size inconsistency");
332
333	/*
334	 * Determine what model of computer we are running on.
335	 */
336	switch (systype) {
337#ifdef news3400
338	case NEWS3400:
339		news3400_init();
340		break;
341#endif
342
343#ifdef news5000
344	case NEWS5000:
345		news5000_init();
346		break;
347#endif
348	}
349
350	i = idrom.id_modelid;
351
352	switch (i) {
353
354	default:
355		printf("kernel not configured for systype 0x%x\n", i);
356		/* cpu_reboot(RB_HALT | RB_NOSYNC, NULL); */
357
358#ifdef news5000
359	case 2: /* NWS-5000U/W */
360	case 5: /* NWS-5000R */
361	case 7: /* NWS-5000X */
362	case 9: /* NWS-5900X */
363	case 11:/* NWS-5000G */
364		/*
365		 * Set up interrupt handling and I/O addresses.
366		 */
367		hardware_intr = news5000_intr;
368		strcpy(cpu_model, "news5000");
369		cpuspeed = 50;	/* ??? XXX */
370		break;
371#endif
372
373#ifdef news3400
374	case 3: /* NWS-3410 */
375	case 6: /* NWS-3470 */
376		/*
377		 * Set up interrupt handling and I/O addresses.
378		 */
379		hardware_intr = news3400_intr;
380		strcpy(cpu_model, "news3400");
381		cpuspeed = 10;
382		break;
383#endif
384	}
385
386	/*
387	 * Initialize the virtual memory system.
388	 */
389	pmap_bootstrap();
390}
391
392/*
393 * cpu_startup: allocate memory for variable-sized tables,
394 * initialize cpu, and do autoconfiguration.
395 */
396void
397cpu_startup()
398{
399	register unsigned i;
400	int base, residual;
401	vaddr_t minaddr, maxaddr;
402	vsize_t size;
403	char pbuf[9];
404#ifdef DEBUG
405	extern int pmapdebug;
406	int opmapdebug = pmapdebug;
407
408	pmapdebug = 0;
409#endif
410
411	/*
412	 * Good {morning,afternoon,evening,night}.
413	 */
414	printf(version);
415	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
416	printf("total memory = %s\n", pbuf);
417
418	/*
419	 * Allocate virtual address space for file I/O buffers.
420	 * Note they are different than the array of headers, 'buf',
421	 * and usually occupy more virtual memory than physical.
422	 */
423	size = MAXBSIZE * nbuf;
424	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(size),
425		    NULL, UVM_UNKNOWN_OFFSET,
426		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
427				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
428		panic("startup: cannot allocate VM for buffers");
429	minaddr = (vaddr_t)buffers;
430	base = bufpages / nbuf;
431	residual = bufpages % nbuf;
432	for (i = 0; i < nbuf; i++) {
433		vsize_t curbufsize;
434		vaddr_t curbuf;
435		struct vm_page *pg;
436
437		/*
438		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
439		 * that MAXBSIZE space, we allocate and map (base+1) pages
440		 * for the first "residual" buffers, and then we allocate
441		 * "base" pages for the rest.
442		 */
443		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
444		curbufsize = NBPG * ((i < residual) ? (base+1) : base);
445
446		while (curbufsize) {
447			pg = uvm_pagealloc(NULL, 0, NULL, 0);
448			if (pg == NULL)
449				panic("cpu_startup: not enough memory for "
450				    "buffer cache");
451			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
452				       VM_PROT_READ|VM_PROT_WRITE);
453			curbuf += PAGE_SIZE;
454			curbufsize -= PAGE_SIZE;
455		}
456	}
457	/*
458	 * Allocate a submap for exec arguments.  This map effectively
459	 * limits the number of processes exec'ing at any time.
460	 */
461	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
462				   16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
463	/*
464	 * Allocate a submap for physio
465	 */
466	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
467				   VM_PHYS_SIZE, 0, FALSE, NULL);
468
469	/*
470	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
471	 * are allocated via the pool allocator, and we use KSEG to
472	 * map those pages.
473	 */
474
475#ifdef DEBUG
476	pmapdebug = opmapdebug;
477#endif
478	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
479	printf("avail memory = %s\n", pbuf);
480	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
481	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
482
483	/*
484	 * Set up buffers, so they can be used to read disk labels.
485	 */
486	bufinit();
487}
488
489
490/*
491 * machine dependent system variables.
492 */
493int
494cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
495	int *name;
496	u_int namelen;
497	void *oldp;
498	size_t *oldlenp;
499	void *newp;
500	size_t newlen;
501	struct proc *p;
502{
503	/* all sysctl names at this level are terminal */
504	if (namelen != 1)
505		return (ENOTDIR);		/* overloaded */
506
507	switch (name[0]) {
508
509	default:
510		return (EOPNOTSUPP);
511	}
512	/* NOTREACHED */
513}
514
515/*
516 * lookup_bootinfo:
517 * Look up information in bootinfo of boot loader.
518 */
519void *
520lookup_bootinfo(type)
521	int type;
522{
523	struct btinfo_common *bt;
524	char *help = bootinfo;
525
526	/* Check for a bootinfo record first. */
527	if (help == NULL)
528		return (NULL);
529
530	do {
531		bt = (struct btinfo_common *)help;
532		if (bt->type == type)
533			return ((void *)help);
534		help += bt->next;
535	} while (bt->next != 0 &&
536		(size_t)help < (size_t)bootinfo + BOOTINFO_SIZE);
537
538	return (NULL);
539}
540
541/*
542 * call PROM to halt or reboot.
543 */
544void
545prom_halt(howto)
546	int howto;
547
548{
549#ifdef news5000
550	if (systype == NEWS5000)
551		apcall_exit(howto);
552#endif
553#ifdef news3400
554	if (systype == NEWS3400)
555		to_monitor(howto);
556#endif
557	for (;;);
558}
559
560int	waittime = -1;
561
562void
563cpu_reboot(howto, bootstr)
564	volatile int howto;
565	char *bootstr;
566{
567
568	/* take a snap shot before clobbering any registers */
569	if (curproc)
570		savectx((struct user *)curpcb);
571
572#ifdef DEBUG
573	if (panicstr)
574		stacktrace();
575#endif
576
577	/* If system is cold, just halt. */
578	if (cold) {
579		howto |= RB_HALT;
580		goto haltsys;
581	}
582
583	/* If "always halt" was specified as a boot flag, obey. */
584	if ((boothowto & RB_HALT) != 0)
585		howto |= RB_HALT;
586
587	boothowto = howto;
588	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
589		/*
590		 * Synchronize the disks....
591		 */
592		waittime = 0;
593		vfs_shutdown();
594
595		/*
596		 * If we've been adjusting the clock, the todr
597		 * will be out of synch; adjust it now.
598		 */
599		resettodr();
600	}
601
602	/* Disable interrupts. */
603	disable_intr();
604
605	splhigh();
606
607	/* If rebooting and a dump is requested do it. */
608#if 0
609	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
610#else
611	if (howto & RB_DUMP)
612#endif
613		dumpsys();
614
615haltsys:
616
617	/* run any shutdown hooks */
618	doshutdownhooks();
619
620	if ((howto & RB_POWERDOWN) == RB_POWERDOWN)
621		prom_halt(0x80);	/* rom monitor RB_PWOFF */
622
623	/* Finally, halt/reboot the system. */
624	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
625	prom_halt(howto & RB_HALT);
626	/*NOTREACHED*/
627}
628
629/*
630 * Return the best possible estimate of the time in the timeval
631 * to which tvp points.  Unfortunately, we can't read the hardware registers.
632 * We guarantee that the time will be greater than the value obtained by a
633 * previous call.
634 */
635void
636microtime(tvp)
637	register struct timeval *tvp;
638{
639	int s = splclock();
640	static struct timeval lasttime;
641
642	*tvp = time;
643#if 0
644	tvp->tv_usec += clkread();
645	if (tvp->tv_usec >= 1000000) {
646		tvp->tv_usec -= 1000000;
647		tvp->tv_sec++;
648	}
649#endif
650
651	if (tvp->tv_sec == lasttime.tv_sec &&
652	    tvp->tv_usec <= lasttime.tv_usec &&
653	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
654		tvp->tv_sec++;
655		tvp->tv_usec -= 1000000;
656	}
657	lasttime = *tvp;
658	splx(s);
659}
660
661void
662delay(n)
663	int n;
664{
665	DELAY(n);
666}
667
668#include "zsc.h"
669
670int zssoft __P((void));
671
672void
673cpu_intr(status, cause, pc, ipending)
674	u_int32_t status;
675	u_int32_t cause;
676	u_int32_t pc;
677	u_int32_t ipending;
678{
679	uvmexp.intrs++;
680
681	/* device interrupts */
682	(*hardware_intr)(status, cause, pc, ipending);
683
684	/* software simulated interrupt */
685	if ((ipending & MIPS_SOFT_INT_MASK_1) ||
686	    (ssir && (status & MIPS_SOFT_INT_MASK_1))) {
687
688#define DO_SIR(bit, fn)						\
689	do {							\
690		if (n & (bit)) {				\
691			uvmexp.softs++;				\
692			fn;					\
693		}						\
694	} while (0)
695
696		unsigned n;
697		n = ssir; ssir = 0;
698		_clrsoftintr(MIPS_SOFT_INT_MASK_1);
699
700#if NZSC > 0
701		DO_SIR(SIR_SERIAL, zssoft());
702#endif
703		DO_SIR(SIR_NET, netintr());
704#undef DO_SIR
705	}
706
707	/* 'softclock' interrupt */
708	if (ipending & MIPS_SOFT_INT_MASK_0) {
709		_clrsoftintr(MIPS_SOFT_INT_MASK_0);
710		uvmexp.softs++;
711		intrcnt[SOFTCLOCK_INTR]++;
712		softclock();
713	}
714}
715
716#ifdef EXEC_ECOFF
717#include <sys/exec_ecoff.h>
718
719int
720cpu_exec_ecoff_hook(p, epp)
721	struct proc *p;
722	struct exec_package *epp;
723{
724	extern struct emul emul_netbsd;
725
726	epp->ep_emul = &emul_netbsd;
727
728	return 0;
729}
730#endif
731