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