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