xref: /netbsd-src/sys/arch/sun2/sun2/machdep.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: machdep.c,v 1.17 2002/09/06 13:18:43 gehenna Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Matthew Fredette.
5  * Copyright (c) 1994, 1995 Gordon W. Ross
6  * Copyright (c) 1993 Adam Glass
7  * Copyright (c) 1988 University of Utah.
8  * Copyright (c) 1982, 1986, 1990, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * the Systems Programming Group of the University of Utah Computer
13  * Science Department.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *	from: Utah Hdr: machdep.c 1.74 92/12/20
44  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
45  */
46 
47 /*-
48  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
49  * All rights reserved.
50  *
51  * This code is derived from software contributed to The NetBSD Foundation
52  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
53  * NASA Ames Research Center.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  *    must display the following acknowledgement:
65  *	This product includes software developed by the NetBSD
66  *	Foundation, Inc. and its contributors.
67  * 4. Neither the name of The NetBSD Foundation nor the names of its
68  *    contributors may be used to endorse or promote products derived
69  *    from this software without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
72  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
73  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
75  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
77  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
79  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
80  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
81  * POSSIBILITY OF SUCH DAMAGE.
82  */
83 
84 /*
85  * Copyright (c) 1992, 1993
86  *	The Regents of the University of California.  All rights reserved.
87  *
88  * This software was developed by the Computer Systems Engineering group
89  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
90  * contributed to Berkeley.
91  *
92  * All advertising materials mentioning features or use of this software
93  * must display the following acknowledgement:
94  *	This product includes software developed by the University of
95  *	California, Lawrence Berkeley Laboratory.
96  *
97  * Redistribution and use in source and binary forms, with or without
98  * modification, are permitted provided that the following conditions
99  * are met:
100  * 1. Redistributions of source code must retain the above copyright
101  *    notice, this list of conditions and the following disclaimer.
102  * 2. Redistributions in binary form must reproduce the above copyright
103  *    notice, this list of conditions and the following disclaimer in the
104  *    documentation and/or other materials provided with the distribution.
105  * 3. All advertising materials mentioning features or use of this software
106  *    must display the following acknowledgement:
107  *	This product includes software developed by the University of
108  *	California, Berkeley and its contributors.
109  * 4. Neither the name of the University nor the names of its contributors
110  *    may be used to endorse or promote products derived from this software
111  *    without specific prior written permission.
112  *
113  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
114  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
115  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
116  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
117  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
118  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
119  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
120  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
121  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
122  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
123  * SUCH DAMAGE.
124  *
125  *	@(#)machdep.c	8.6 (Berkeley) 1/14/94
126  */
127 
128 #include "opt_ddb.h"
129 #include "opt_kgdb.h"
130 
131 #include <sys/param.h>
132 #include <sys/systm.h>
133 #include <sys/kernel.h>
134 #include <sys/map.h>
135 #include <sys/proc.h>
136 #include <sys/buf.h>
137 #include <sys/reboot.h>
138 #include <sys/conf.h>
139 #include <sys/file.h>
140 #include <sys/clist.h>
141 #include <sys/device.h>
142 #include <sys/malloc.h>
143 #include <sys/extent.h>
144 #include <sys/mbuf.h>
145 #include <sys/msgbuf.h>
146 #include <sys/ioctl.h>
147 #include <sys/tty.h>
148 #include <sys/mount.h>
149 #include <sys/user.h>
150 #include <sys/exec.h>
151 #include <sys/core.h>
152 #include <sys/kcore.h>
153 #include <sys/vnode.h>
154 #include <sys/syscallargs.h>
155 #ifdef	KGDB
156 #include <sys/kgdb.h>
157 #endif
158 
159 #include <uvm/uvm.h> /* XXX: not _extern ... need vm_map_create */
160 
161 #include <sys/sysctl.h>
162 
163 #include <dev/cons.h>
164 
165 #include <machine/promlib.h>
166 #include <machine/cpu.h>
167 #include <machine/dvma.h>
168 #include <machine/idprom.h>
169 #include <machine/kcore.h>
170 #include <machine/reg.h>
171 #include <machine/psl.h>
172 #include <machine/pte.h>
173 #define _SUN68K_BUS_DMA_PRIVATE
174 #include <machine/autoconf.h>
175 #include <machine/bus.h>
176 #include <machine/intr.h>
177 #include <machine/pmap.h>
178 
179 #if defined(DDB)
180 #include <machine/db_machdep.h>
181 #include <ddb/db_sym.h>
182 #include <ddb/db_extern.h>
183 #endif
184 
185 #include <dev/vme/vmereg.h>
186 #include <dev/vme/vmevar.h>
187 
188 #include <sun2/sun2/control.h>
189 #include <sun2/sun2/enable.h>
190 #include <sun2/sun2/machdep.h>
191 
192 #include <sun68k/sun68k/vme_sun68k.h>
193 
194 /* Defined in locore.s */
195 extern char kernel_text[];
196 /* Defined by the linker */
197 extern char etext[];
198 
199 /* Our exported CPU info; we can have only one. */
200 struct cpu_info cpu_info_store;
201 
202 struct vm_map *exec_map = NULL;
203 struct vm_map *mb_map = NULL;
204 struct vm_map *phys_map = NULL;
205 
206 int	physmem;
207 int	fputype;
208 caddr_t	msgbufaddr;
209 
210 /* Virtual page frame for /dev/mem (see mem.c) */
211 vaddr_t vmmap;
212 
213 /*
214  * safepri is a safe priority for sleep to set for a spin-wait
215  * during autoconfiguration or after a panic.
216  */
217 int	safepri = PSL_LOWIPL;
218 
219 /* Soft copy of the enable register. */
220 __volatile u_short enable_reg_soft = ENABLE_REG_SOFT_UNDEF;
221 
222 /*
223  * Our no-fault fault handler.
224  */
225 label_t *nofault;
226 
227 /*
228  * dvmamap is used to manage DVMA memory.
229  */
230 static struct extent *dvmamap;
231 
232 /* Our private scratch page for dumping the MMU. */
233 static vaddr_t dumppage;
234 
235 static void identifycpu __P((void));
236 static void initcpu __P((void));
237 
238 /*
239  * cpu_startup: allocate memory for variable-sized tables,
240  * initialize cpu, and do autoconfiguration.
241  *
242  * This is called early in init_main.c:main(), after the
243  * kernel memory allocator is ready for use, but before
244  * the creation of processes 1,2, and mountroot, etc.
245  */
246 void
247 cpu_startup()
248 {
249 	caddr_t v;
250 	vsize_t size;
251 	u_int sz, i, base, residual;
252 	vaddr_t minaddr, maxaddr;
253 	char pbuf[9];
254 
255 	/*
256 	 * Initialize message buffer (for kernel printf).
257 	 * This is put in physical pages four through seven
258 	 * so it will always be in the same place after a
259 	 * reboot. (physical pages 0-3 are reserved by the PROM
260 	 * for its vector table and other stuff.)
261 	 * Its mapping was prepared in pmap_bootstrap().
262 	 * Also, offset some to avoid PROM scribbles.
263 	 */
264 	v = (caddr_t) (NBPG * 4);
265 	msgbufaddr = (caddr_t)(v + MSGBUFOFF);
266 	initmsgbuf(msgbufaddr, MSGBUFSIZE);
267 
268 #ifdef DDB
269 	{
270 		extern int end[];
271 		extern char *esym;
272 
273 		ddb_init(end[0], end + 1, (int*)esym);
274 	}
275 #endif /* DDB */
276 
277 	/*
278 	 * Good {morning,afternoon,evening,night}.
279 	 */
280 	printf(version);
281 	identifycpu();
282 	fputype = FPU_NONE;
283 #ifdef  FPU_EMULATE
284 	printf("fpu: emulator\n");
285 #else
286 	printf("fpu: no math support\n");
287 #endif
288 
289 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
290 	printf("total memory = %s\n", pbuf);
291 
292 	/*
293 	 * XXX fredette - we force a small number of buffers
294 	 * to help me debug this on my low-memory machine.
295 	 * this should go away at some point, allowing the
296 	 * normal automatic buffer-sizing to happen.
297 	 */
298 	bufpages = 37;
299 
300 	/*
301 	 * Get scratch page for dumpsys().
302 	 */
303 	if ((dumppage = uvm_km_alloc(kernel_map, NBPG)) == 0)
304 		panic("startup: alloc dumppage");
305 
306 	/*
307 	 * Find out how much space we need, allocate it,
308 	 * and then give everything true virtual addresses.
309 	 */
310 	sz = (u_int)allocsys(NULL, NULL);
311 	if ((v = (caddr_t)uvm_km_alloc(kernel_map, round_page(sz))) == 0)
312 		panic("startup: no room for tables");
313 	if (allocsys(v, NULL) - v != sz)
314 		panic("startup: table size inconsistency");
315 
316 	/*
317 	 * Now allocate buffers proper.  They are different than the above
318 	 * in that they usually occupy more virtual memory than physical.
319 	 */
320 	size = MAXBSIZE * nbuf;
321 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
322 		    NULL, UVM_UNKNOWN_OFFSET, 0,
323 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
324 				UVM_ADV_NORMAL, 0)) != 0)
325 		panic("startup: cannot allocate VM for buffers");
326 	minaddr = (vaddr_t)buffers;
327 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
328 		/* don't want to alloc more physical mem than needed */
329 		bufpages = btoc(MAXBSIZE) * nbuf;
330 	}
331 	base = bufpages / nbuf;
332 	residual = bufpages % nbuf;
333 	for (i = 0; i < nbuf; i++) {
334 		vsize_t curbufsize;
335 		vaddr_t curbuf;
336 		struct vm_page *pg;
337 
338 		/*
339 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
340 		 * that MAXBSIZE space, we allocate and map (base+1) pages
341 		 * for the first "residual" buffers, and then we allocate
342 		 * "base" pages for the rest.
343 		 */
344 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
345 		curbufsize = NBPG * ((i < residual) ? (base+1) : base);
346 
347 		while (curbufsize) {
348 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
349 			if (pg == NULL)
350 				panic("cpu_startup: not enough memory for "
351 				    "buffer cache");
352 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
353 				       VM_PROT_READ|VM_PROT_WRITE);
354 			curbuf += PAGE_SIZE;
355 			curbufsize -= PAGE_SIZE;
356 		}
357 	}
358 	pmap_update(pmap_kernel());
359 
360 	/*
361 	 * Allocate a submap for exec arguments.  This map effectively
362 	 * limits the number of processes exec'ing at any time.
363 	 */
364 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
365 				   NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
366 
367 	/*
368 	 * We don't use a submap for physio, and use a separate map
369 	 * for DVMA allocations.  Our vmapbuf just maps pages into
370 	 * the kernel map (any kernel mapping is OK) and then the
371 	 * device drivers clone the kernel mappings into DVMA space.
372 	 */
373 
374 	/*
375 	 * Finally, allocate mbuf cluster submap.
376 	 */
377 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
378 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
379 				 FALSE, NULL);
380 
381 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
382 	printf("avail memory = %s\n", pbuf);
383 	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
384 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
385 
386 	/*
387 	 * Allocate a virtual page (for use by /dev/mem)
388 	 * This page is handed to pmap_enter() therefore
389 	 * it has to be in the normal kernel VA range.
390 	 */
391 	vmmap = uvm_km_valloc_wait(kernel_map, NBPG);
392 
393 	/*
394 	 * Allocate dma map for devices on the bus.
395 	 */
396 	dvmamap = extent_create("dvmamap",
397 	    DVMA_MAP_BASE, DVMA_MAP_BASE + DVMA_MAP_AVAIL,
398 	    M_DEVBUF, 0, 0, EX_NOWAIT);
399 	if (dvmamap == NULL)
400 		panic("unable to allocate DVMA map");
401 
402 	/*
403 	 * Set up CPU-specific registers, cache, etc.
404 	 */
405 	initcpu();
406 
407 	/*
408 	 * Set up buffers, so they can be used to read disk labels.
409 	 */
410 	bufinit();
411 }
412 
413 /*
414  * Set registers on exec.
415  */
416 void
417 setregs(p, pack, stack)
418 	struct proc *p;
419 	struct exec_package *pack;
420 	u_long stack;
421 {
422 	struct trapframe *tf = (struct trapframe *)p->p_md.md_regs;
423 
424 	tf->tf_sr = PSL_USERSET;
425 	tf->tf_pc = pack->ep_entry & ~1;
426 	tf->tf_regs[D0] = 0;
427 	tf->tf_regs[D1] = 0;
428 	tf->tf_regs[D2] = 0;
429 	tf->tf_regs[D3] = 0;
430 	tf->tf_regs[D4] = 0;
431 	tf->tf_regs[D5] = 0;
432 	tf->tf_regs[D6] = 0;
433 	tf->tf_regs[D7] = 0;
434 	tf->tf_regs[A0] = 0;
435 	tf->tf_regs[A1] = 0;
436 	tf->tf_regs[A2] = (int)p->p_psstr;
437 	tf->tf_regs[A3] = 0;
438 	tf->tf_regs[A4] = 0;
439 	tf->tf_regs[A5] = 0;
440 	tf->tf_regs[A6] = 0;
441 	tf->tf_regs[SP] = stack;
442 
443 	/* restore a null state frame */
444 	p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
445 
446 	p->p_md.md_flags = 0;
447 }
448 
449 /*
450  * Info for CTL_HW
451  */
452 char	machine[16] = MACHINE;		/* from <machine/param.h> */
453 char	kernel_arch[16] = "sun2";	/* XXX needs a sysctl node */
454 char	cpu_model[120];
455 
456 /*
457  * Determine which Sun2 model we are running on.
458  */
459 void
460 identifycpu()
461 {
462 	extern char *cpu_string;	/* XXX */
463 
464 	/* Other stuff? (VAC, mc6888x version, etc.) */
465 	/* Note: miniroot cares about the kernel_arch part. */
466 	sprintf(cpu_model, "%s %s", kernel_arch, cpu_string);
467 
468 	printf("Model: %s\n", cpu_model);
469 }
470 
471 /*
472  * machine dependent system variables.
473  */
474 int
475 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
476 	int *name;
477 	u_int namelen;
478 	void *oldp;
479 	size_t *oldlenp;
480 	void *newp;
481 	size_t newlen;
482 	struct proc *p;
483 {
484 	int error;
485 	dev_t consdev;
486 	char *cp;
487 
488 	/* all sysctl names at this level are terminal */
489 	if (namelen != 1)
490 		return (ENOTDIR);		/* overloaded */
491 
492 	switch (name[0]) {
493 	case CPU_CONSDEV:
494 		if (cn_tab != NULL)
495 			consdev = cn_tab->cn_dev;
496 		else
497 			consdev = NODEV;
498 		error = sysctl_rdstruct(oldp, oldlenp, newp,
499 		    &consdev, sizeof consdev);
500 		break;
501 
502 #if 0	/* XXX - Not yet... */
503 	case CPU_ROOT_DEVICE:
504 		error = sysctl_rdstring(oldp, oldlenp, newp, root_device);
505 		break;
506 
507 #endif
508 	case CPU_BOOTED_KERNEL:
509 		cp = prom_getbootfile();
510 		if (cp == NULL || cp[0] == '\0')
511 			return (ENOENT);
512 		return (sysctl_rdstring(oldp, oldlenp, newp, cp));
513 
514 	default:
515 		error = EOPNOTSUPP;
516 	}
517 	return (error);
518 }
519 
520 /* See: sig_machdep.c */
521 
522 /*
523  * Do a sync in preparation for a reboot.
524  * XXX - This could probably be common code.
525  * XXX - And now, most of it is in vfs_shutdown()
526  * XXX - Put waittime checks in there too?
527  */
528 int waittime = -1;	/* XXX - Who else looks at this? -gwr */
529 static void
530 reboot_sync __P((void))
531 {
532 
533 	/* Check waittime here to localize its use to this function. */
534 	if (waittime >= 0)
535 		return;
536 	waittime = 0;
537 	vfs_shutdown();
538 }
539 
540 /*
541  * Common part of the BSD and SunOS reboot system calls.
542  */
543 __dead void
544 cpu_reboot(howto, user_boot_string)
545 	int howto;
546 	char *user_boot_string;
547 {
548 	char *bs, *p;
549 	char default_boot_string[8];
550 
551 	/* If system is cold, just halt. (early panic?) */
552 	if (cold)
553 		goto haltsys;
554 
555 	/* Un-blank the screen if appropriate. */
556 	cnpollc(1);
557 
558 	if ((howto & RB_NOSYNC) == 0) {
559 		reboot_sync();
560 		/*
561 		 * If we've been adjusting the clock, the todr
562 		 * will be out of synch; adjust it now.
563 		 *
564 		 * XXX - However, if the kernel has been sitting in ddb,
565 		 * the time will be way off, so don't set the HW clock!
566 		 * XXX - Should do sanity check against HW clock. -gwr
567 		 */
568 		/* resettodr(); */
569 	}
570 
571 	/* Disable interrupts. */
572 	splhigh();
573 
574 	/* Write out a crash dump if asked. */
575 	if (howto & RB_DUMP)
576 		dumpsys();
577 
578 	/* run any shutdown hooks */
579 	doshutdownhooks();
580 
581 	if (howto & RB_HALT) {
582 	haltsys:
583 		printf("halted.\n");
584 		prom_halt();
585 	}
586 
587 	/*
588 	 * Automatic reboot.
589 	 */
590 	bs = user_boot_string;
591 	if (bs == NULL) {
592 		/*
593 		 * Build our own boot string with an empty
594 		 * boot device/file and (maybe) some flags.
595 		 * The PROM will supply the device/file name.
596 		 */
597 		bs = default_boot_string;
598 		*bs = '\0';
599 		if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
600 			/* Append the boot flags. */
601 			p = bs;
602 			*p++ = ' ';
603 			*p++ = '-';
604 			if (howto & RB_KDB)
605 				*p++ = 'd';
606 			if (howto & RB_ASKNAME)
607 				*p++ = 'a';
608 			if (howto & RB_SINGLE)
609 				*p++ = 's';
610 			*p = '\0';
611 		}
612 	}
613 	printf("rebooting...\n");
614 	prom_boot(bs);
615 	for (;;) ;
616 	/*NOTREACHED*/
617 }
618 
619 /*
620  * These variables are needed by /sbin/savecore
621  */
622 u_int32_t dumpmag = 0x8fca0101;	/* magic number */
623 int 	dumpsize = 0;		/* pages */
624 long	dumplo = 0; 		/* blocks */
625 
626 #define	DUMP_EXTRA 	3	/* CPU-dependent extra pages */
627 
628 /*
629  * This is called by main to set dumplo, dumpsize.
630  * Dumps always skip the first NBPG of disk space
631  * in case there might be a disk label stored there.
632  * If there is extra space, put dump at the end to
633  * reduce the chance that swapping trashes it.
634  */
635 void
636 cpu_dumpconf()
637 {
638 	const struct bdevsw *bdev;
639 	int devblks;	/* size of dump device in blocks */
640 	int dumpblks;	/* size of dump image in blocks */
641 	int (*getsize)__P((dev_t));
642 
643 	if (dumpdev == NODEV)
644 		return;
645 
646 	bdev = bdevsw_lookup(dumpdev);
647 	if (bdev == NULL)
648 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
649 	getsize = bdev->d_psize;
650 	if (getsize == NULL)
651 		return;
652 	devblks = (*getsize)(dumpdev);
653 	if (devblks <= ctod(1))
654 		return;
655 	devblks &= ~(ctod(1)-1);
656 
657 	/*
658 	 * Note: savecore expects dumpsize to be the
659 	 * number of pages AFTER the dump header.
660 	 */
661 	dumpsize = physmem;
662 
663 	/* Position dump image near end of space, page aligned. */
664 	dumpblks = ctod(physmem + DUMP_EXTRA);
665 	dumplo = devblks - dumpblks;
666 
667 	/* If it does not fit, truncate it by moving dumplo. */
668 	/* Note: Must force signed comparison. */
669 	if (dumplo < ((long)ctod(1))) {
670 		dumplo = ctod(1);
671 		dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA;
672 	}
673 }
674 
675 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
676 struct pcb dumppcb;
677 extern paddr_t avail_start;
678 
679 /*
680  * Write a crash dump.  The format while in swap is:
681  *   kcore_seg_t cpu_hdr;
682  *   cpu_kcore_hdr_t cpu_data;
683  *   padding (NBPG-sizeof(kcore_seg_t))
684  *   pagemap (2*NBPG)
685  *   physical memory...
686  */
687 void
688 dumpsys()
689 {
690 	const struct bdevsw *dsw;
691 	kcore_seg_t	*kseg_p;
692 	cpu_kcore_hdr_t *chdr_p;
693 	struct sun2_kcore_hdr *sh;
694 	char *vaddr;
695 	paddr_t paddr;
696 	int psize, todo, chunk;
697 	daddr_t blkno;
698 	int error = 0;
699 
700 	if (dumpdev == NODEV)
701 		return;
702 	dsw = bdevsw_lookup(dumpdev);
703 	if (dsw == NULL || dsw->d_psize == NULL)
704 		return;
705 	if (dumppage == 0)
706 		return;
707 
708 	/*
709 	 * For dumps during autoconfiguration,
710 	 * if dump device has already configured...
711 	 */
712 	if (dumpsize == 0)
713 		cpu_dumpconf();
714 	if (dumplo <= 0) {
715 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
716 		    minor(dumpdev));
717 		return;
718 	}
719 	savectx(&dumppcb);
720 
721 	psize = (*(dsw->d_psize))(dumpdev);
722 	if (psize == -1) {
723 		printf("dump area unavailable\n");
724 		return;
725 	}
726 
727 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
728 	    minor(dumpdev), dumplo);
729 
730 	/*
731 	 * Prepare the dump header, including MMU state.
732 	 */
733 	blkno = dumplo;
734 	todo = dumpsize;	/* pages */
735 	vaddr = (char*)dumppage;
736 	memset(vaddr, 0, NBPG);
737 
738 	/* Set pointers to all three parts. */
739 	kseg_p = (kcore_seg_t *)vaddr;
740 	chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1);
741 	sh = &chdr_p->un._sun2;
742 
743 	/* Fill in kcore_seg_t part. */
744 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
745 	kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p));
746 
747 	/* Fill in cpu_kcore_hdr_t part. */
748 	strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name));
749 	chdr_p->page_size = NBPG;
750 	chdr_p->kernbase = KERNBASE;
751 
752 	/* Fill in the sun2_kcore_hdr part (MMU state). */
753 	pmap_kcore_hdr(sh);
754 
755 	/* Write out the dump header. */
756 	error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
757 	if (error)
758 		goto fail;
759 	blkno += btodb(NBPG);
760 
761 	/* translation RAM (pages zero through seven) */
762 	for(chunk = 0; chunk < (NBPG * 8); chunk += NBPG) {
763 		pmap_get_pagemap((int*)vaddr, chunk);
764 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
765 		if (error)
766 			goto fail;
767 		blkno += btodb(NBPG);
768 	}
769 
770 	/*
771 	 * Now dump physical memory.  Have to do it in two chunks.
772 	 * The first chunk is "unmanaged" (by the VM code) and its
773 	 * range of physical addresses is not allow in pmap_enter.
774 	 * However, that segment is mapped linearly, so we can just
775 	 * use the virtual mappings already in place.  The second
776 	 * chunk is done the normal way, using pmap_enter.
777 	 *
778 	 * Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext.
779 	 */
780 
781 	/* Do the first chunk (0 <= PA < avail_start) */
782 	paddr = 0;
783 	chunk = btoc(avail_start);
784 	if (chunk > todo)
785 		chunk = todo;
786 	do {
787 		if ((todo & 0xf) == 0)
788 			printf("\r%4d", todo);
789 		vaddr = (char*)(paddr + KERNBASE);
790 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
791 		if (error)
792 			goto fail;
793 		paddr += NBPG;
794 		blkno += btodb(NBPG);
795 		--todo;
796 	} while (--chunk > 0);
797 
798 	/* Do the second chunk (avail_start <= PA < dumpsize) */
799 	vaddr = (char*)vmmap;	/* Borrow /dev/mem VA */
800 	do {
801 		if ((todo & 0xf) == 0)
802 			printf("\r%4d", todo);
803 		pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ);
804 		pmap_update(pmap_kernel());
805 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
806 		pmap_kremove(vmmap, NBPG);
807 		pmap_update(pmap_kernel());
808 		if (error)
809 			goto fail;
810 		paddr += NBPG;
811 		blkno += btodb(NBPG);
812 	} while (--todo > 0);
813 
814 	printf("\rdump succeeded\n");
815 	return;
816 fail:
817 	printf(" dump error=%d\n", error);
818 }
819 
820 static void
821 initcpu()
822 {
823 	/* XXX: Enable RAM parity/ECC checking? */
824 	/* XXX: parityenable(); */
825 
826 }
827 
828 /* straptrap() in trap.c */
829 
830 /* from hp300: badaddr() */
831 
832 /* XXX: parityenable() ? */
833 /* regdump() moved to regdump.c */
834 
835 /*
836  * cpu_exec_aout_makecmds():
837  *	cpu-dependent a.out format hook for execve().
838  *
839  * Determine if the given exec package refers to something which we
840  * understand and, if so, set up the vmcmds for it.
841  */
842 int
843 cpu_exec_aout_makecmds(p, epp)
844 	struct proc *p;
845 	struct exec_package *epp;
846 {
847 	return ENOEXEC;
848 }
849 
850 /*
851  * Soft interrupt support.
852  */
853 void isr_soft_request(level)
854 	int level;
855 {
856 	u_char bit;
857 
858 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
859 		return;
860 
861 	bit = 1 << level;
862 	enable_reg_or(bit);
863 }
864 
865 void isr_soft_clear(level)
866 	int level;
867 {
868 	u_char bit;
869 
870 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
871 		return;
872 
873 	bit = 1 << level;
874 	enable_reg_and(~bit);
875 }
876 
877 /*
878  * Like _bus_dmamap_load(), but for raw memory allocated with
879  * bus_dmamem_alloc().
880  */
881 int
882 _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
883 	bus_dma_tag_t t;
884 	bus_dmamap_t map;
885 	bus_dma_segment_t *segs;
886 	int nsegs;
887 	bus_size_t size;
888 	int flags;
889 {
890 	struct vm_page *m;
891 	paddr_t pa;
892 	bus_addr_t dva;
893 	bus_size_t sgsize;
894 	struct pglist *mlist;
895 	int pagesz = PAGE_SIZE;
896 	int error;
897 
898 	/*
899 	 * Make sure that on error condition we return "no valid mappings".
900 	 */
901 	map->dm_nsegs = 0;
902 	map->dm_mapsize = 0;
903 
904 	/* Allocate DVMA addresses */
905 	sgsize = (size + pagesz - 1) & -pagesz;
906 
907 	/*
908 	 * If the device can see our entire 24-bit address space,
909 	 * we can use any properly aligned virtual addresses.
910 	 */
911 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0) {
912 		dva = _bus_dma_valloc_skewed(sgsize, map->_dm_boundary,
913 					     pagesz, 0);
914 		if (dva == 0)
915 			return (ENOMEM);
916 	}
917 
918 	/*
919 	 * Otherwise, we need virtual addresses in DVMA space.
920 	 */
921 	else {
922 		error = extent_alloc(dvmamap, sgsize, pagesz,
923 					map->_dm_boundary,
924 					(flags & BUS_DMA_NOWAIT) == 0
925 						? EX_WAITOK : EX_NOWAIT,
926 					(u_long *)&dva);
927 		if (error)
928 			return (error);
929 	}
930 
931 	/* Fill in the segment. */
932 	map->dm_segs[0].ds_addr = dva;
933 	map->dm_segs[0].ds_len = size;
934 	map->dm_segs[0]._ds_va = dva;
935 	map->dm_segs[0]._ds_sgsize = sgsize;
936 
937 	/* Map physical pages into MMU */
938 	mlist = segs[0]._ds_mlist;
939 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
940 		if (sgsize == 0)
941 			panic("_bus_dmamap_load_raw: size botch");
942 		pa = VM_PAGE_TO_PHYS(m);
943 		pmap_enter(pmap_kernel(), dva,
944 			   (pa & -pagesz) | PMAP_NC,
945 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
946 
947 		dva += pagesz;
948 		sgsize -= pagesz;
949 	}
950 	pmap_update(pmap_kernel());
951 
952 	/* Make the map truly valid. */
953 	map->dm_nsegs = 1;
954 	map->dm_mapsize = size;
955 
956 	return (0);
957 }
958 
959 /*
960  * load DMA map with a linear buffer.
961  */
962 int
963 _bus_dmamap_load(t, map, buf, buflen, p, flags)
964 	bus_dma_tag_t t;
965 	bus_dmamap_t map;
966 	void *buf;
967 	bus_size_t buflen;
968 	struct proc *p;
969 	int flags;
970 {
971 	bus_size_t sgsize;
972 	vaddr_t va = (vaddr_t)buf;
973 	int pagesz = PAGE_SIZE;
974 	bus_addr_t dva;
975 	pmap_t pmap;
976 	int rv;
977 
978 	/*
979 	 * Make sure that on error condition we return "no valid mappings".
980 	 */
981 	map->dm_nsegs = 0;
982 	map->dm_mapsize = 0;
983 
984 	if (buflen > map->_dm_size)
985 		return (EINVAL);
986 
987 	/*
988 	 * A 24-bit device can see all of our kernel address space, so
989 	 * if we have KVAs, we can just load them as-is, no mapping
990 	 * necessary.
991 	 */
992 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0 && p == NULL) {
993 		/*
994 		 * XXX Need to implement "don't dma across this boundry".
995 		 */
996 		if (map->_dm_boundary != 0)
997 			panic("bus_dmamap_load: boundaries not implemented");
998 		map->dm_mapsize = buflen;
999 		map->dm_nsegs = 1;
1000 		map->dm_segs[0].ds_addr = (bus_addr_t)va;
1001 		map->dm_segs[0].ds_len = buflen;
1002 		map->_dm_flags |= _BUS_DMA_DIRECTMAP;
1003 		return (0);
1004 	}
1005 
1006 	/*
1007 	 * Allocate a region in DVMA space.
1008 	 */
1009 	sgsize = m68k_round_page(buflen + (va & (pagesz - 1)));
1010 
1011 	if (extent_alloc(dvmamap, sgsize, pagesz, map->_dm_boundary,
1012 			 (flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT,
1013 			 (u_long *)&dva) != 0) {
1014 		return (ENOMEM);
1015 	}
1016 
1017 	/* Fill in the segment. */
1018 	map->dm_segs[0].ds_addr = dva + (va & (pagesz - 1));
1019 	map->dm_segs[0].ds_len = buflen;
1020 	map->dm_segs[0]._ds_va = dva;
1021 	map->dm_segs[0]._ds_sgsize = sgsize;
1022 
1023 	/*
1024 	 * Now map the DVMA addresses we allocated to point to the
1025 	 * pages of the caller's buffer.
1026 	 */
1027 	if (p != NULL)
1028 		pmap = p->p_vmspace->vm_map.pmap;
1029 	else
1030 		pmap = pmap_kernel();
1031 
1032 	for (; buflen > 0; ) {
1033 		paddr_t pa;
1034 		/*
1035 		 * Get the physical address for this page.
1036 		 */
1037 		rv = pmap_extract(pmap, va, &pa);
1038 #ifdef	DIAGNOSTIC
1039 		if (!rv)
1040 			panic("_bus_dmamap_load: no page");
1041 #endif	/* DIAGNOSTIC */
1042 
1043 		/*
1044 		 * Compute the segment size, and adjust counts.
1045 		 */
1046 		sgsize = pagesz - (va & (pagesz - 1));
1047 		if (buflen < sgsize)
1048 			sgsize = buflen;
1049 
1050 		pmap_enter(pmap_kernel(), dva,
1051 			   (pa & -pagesz) | PMAP_NC,
1052 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
1053 
1054 		dva += pagesz;
1055 		va += sgsize;
1056 		buflen -= sgsize;
1057 	}
1058 	pmap_update(pmap_kernel());
1059 
1060 	/* Make the map truly valid. */
1061 	map->dm_nsegs = 1;
1062 	map->dm_mapsize = map->dm_segs[0].ds_len;
1063 
1064 	return (0);
1065 }
1066 
1067 /*
1068  * unload a DMA map.
1069  */
1070 void
1071 _bus_dmamap_unload(t, map)
1072 	bus_dma_tag_t t;
1073 	bus_dmamap_t map;
1074 {
1075 	bus_dma_segment_t *segs = map->dm_segs;
1076 	int nsegs = map->dm_nsegs;
1077 	int flags = map->_dm_flags;
1078 	bus_addr_t dva;
1079 	bus_size_t len;
1080 	int s, error;
1081 
1082 	if (nsegs != 1)
1083 		panic("_bus_dmamem_unload: nsegs = %d", nsegs);
1084 
1085 	/*
1086 	 * _BUS_DMA_DIRECTMAP is set iff this map was loaded using
1087 	 * _bus_dmamap_load for a 24-bit device.
1088 	 */
1089 	if ((flags & _BUS_DMA_DIRECTMAP) != 0) {
1090 		/* Nothing to release */
1091 		map->_dm_flags &= ~_BUS_DMA_DIRECTMAP;
1092 	}
1093 
1094 	/*
1095 	 * Otherwise, this map was loaded using _bus_dmamap_load for a
1096 	 * non-24-bit device, or using _bus_dmamap_load_raw.
1097 	 */
1098 	else {
1099 		dva = segs[0]._ds_va & -PAGE_SIZE;
1100 		len = segs[0]._ds_sgsize;
1101 
1102 		/*
1103 		 * Unmap the DVMA addresses.
1104 		 */
1105 		pmap_remove(pmap_kernel(), dva, dva + len);
1106 		pmap_update(pmap_kernel());
1107 
1108 		/*
1109 		 * Free the DVMA addresses.
1110 		 */
1111 		if ((flags & BUS_DMA_24BIT) != 0) {
1112 			/*
1113 			 * This map was loaded using _bus_dmamap_load_raw
1114 			 * for a 24-bit device.
1115 			 */
1116 			uvm_unmap(kernel_map, dva, dva + len);
1117 		} else {
1118 			/*
1119 			 * This map was loaded using _bus_dmamap_load or
1120 			 * _bus_dmamap_load_raw for a non-24-bit device.
1121 			 */
1122 			s = splhigh();
1123 			error = extent_free(dvmamap, dva, len, EX_NOWAIT);
1124 			splx(s);
1125 			if (error != 0)
1126 				printf("warning: %ld of DVMA space lost\n", len);
1127 		}
1128 	}
1129 
1130 	/* Mark the mappings as invalid. */
1131 	map->dm_mapsize = 0;
1132 	map->dm_nsegs = 0;
1133 }
1134 
1135 /*
1136  * Translate a VME address and address modifier
1137  * into a CPU physical address and page type.
1138  */
1139 int
1140 vmebus_translate(mod, addr, btp, bap)
1141 	vme_am_t	mod;
1142 	vme_addr_t	addr;
1143 	bus_type_t	*btp;
1144 	bus_addr_t	*bap;
1145 {
1146 	bus_addr_t base;
1147 
1148 	switch(mod) {
1149 #define _DS (VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA)
1150 
1151 	case (VME_AM_A16|_DS):
1152 		base = 0x00ff0000;
1153 		break;
1154 
1155 	case (VME_AM_A24|_DS):
1156 		base = 0;
1157 		break;
1158 
1159 	default:
1160 		return (ENOENT);
1161 #undef _DS
1162 	}
1163 
1164 	*bap = base | addr;
1165 	*btp = (*bap & 0x800000 ? PMAP_VME8 : PMAP_VME0);
1166 	return (0);
1167 }
1168