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