isa.c revision 3224
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
37 *	$Id: isa.c,v 1.25 1994/09/20 05:07:11 bde Exp $
38 */
39
40/*
41 * code to manage AT bus
42 *
43 * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44 * Fixed uninitialized variable problem and added code to deal
45 * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46 * mode DMA count compution and reorganized DMA setup code in
47 * isa_dmastart()
48 */
49
50#include <sys/param.h>
51#include <sys/systm.h>		/* isn't it a joy */
52#include <sys/kernel.h>		/* to have three of these */
53#include <sys/conf.h>
54#include <sys/file.h>
55#include <sys/buf.h>
56#include <sys/uio.h>
57#include <sys/syslog.h>
58#include <sys/malloc.h>
59#include <sys/rlist.h>
60#include <machine/segments.h>
61#include <vm/vm.h>
62#include <machine/spl.h>
63#include <i386/isa/isa_device.h>
64#include <i386/isa/isa.h>
65#include <i386/isa/icu.h>
66#include <i386/isa/ic/i8237.h>
67#include <i386/isa/ic/i8042.h>
68#include "vector.h"
69
70/*
71**  Register definitions for DMA controller 1 (channels 0..3):
72*/
73#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
74#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
75#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
76#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
77
78/*
79**  Register definitions for DMA controller 2 (channels 4..7):
80*/
81#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
82#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
83#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
84#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
85
86/*
87 * Bits to specify the type and amount of conflict checking.
88 */
89#define	CC_ATTACH	(1 << 0)
90#define	CC_DRQ		(1 << 1)
91#define	CC_IOADDR	(1 << 2)
92#define	CC_IRQ		(1 << 3)
93#define	CC_MEMADDR	(1 << 4)
94
95/*
96 * XXX these defines should be in a central place.
97 */
98#define	read_eflags()		({u_long ef; \
99				  __asm("pushfl; popl %0" : "=a" (ef)); \
100				  ef; })
101#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
102
103u_long	*intr_countp[ICU_LEN];
104inthand2_t *intr_handler[ICU_LEN];
105u_int	intr_mask[ICU_LEN];
106int	intr_unit[ICU_LEN];
107
108static inthand_t *fastintr[ICU_LEN] = {
109	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
110	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
111	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
112	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
113	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
114	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
115	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
116	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
117};
118
119static inthand_t *slowintr[ICU_LEN] = {
120	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
121	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
122	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
123	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
124};
125
126static void config_isadev __P((struct isa_device *isdp, u_int *mp));
127static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
128			  int item, char const *whatnot, char const *reason,
129			  char const *format));
130static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
131			 u_int checkbits));
132static int haveseen_isadev __P((struct isa_device *dvp, u_int checkbits));
133static inthand2_t isa_strayintr;
134static void register_imask __P((struct isa_device *dvp, u_int mask));
135
136/*
137 * print a conflict message
138 */
139static void
140conflict(dvp, tmpdvp, item, whatnot, reason, format)
141	struct isa_device	*dvp;
142	struct isa_device	*tmpdvp;
143	int			item;
144	char const		*whatnot;
145	char const		*reason;
146	char const		*format;
147{
148	printf("%s%d not %sed due to %s conflict with %s%d at ",
149		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
150		tmpdvp->id_driver->name, tmpdvp->id_unit);
151	printf(format, item);
152	printf("\n");
153}
154
155/*
156 * Check to see if things are alread in use, like IRQ's, I/O addresses
157 * and Memory addresses.
158 */
159static int
160haveseen(dvp, tmpdvp, checkbits)
161	struct isa_device *dvp;
162	struct isa_device *tmpdvp;
163	u_int	checkbits;
164{
165	int	status = 0;
166
167	/*
168	 * Only check against devices that have already been found
169	 */
170	if (tmpdvp->id_alive) {
171		char const *whatnot;
172
173		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
174		/*
175		 * Check for I/O address conflict.  We can only check the
176		 * starting address of the device against the range of the
177		 * device that has already been probed since we do not
178		 * know how many I/O addresses this device uses.
179		 */
180		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
181			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
182			    (dvp->id_iobase <=
183				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
184				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
185					 "I/O address", "0x%x");
186				status = 1;
187			}
188		}
189		/*
190		 * Check for Memory address conflict.  We can check for
191		 * range overlap, but it will not catch all cases since the
192		 * driver may adjust the msize paramater during probe, for
193		 * now we just check that the starting address does not
194		 * fall within any allocated region.
195		 * XXX could add a second check after the probe for overlap,
196		 * since at that time we would know the full range.
197		 * XXX KERNBASE is a hack, we should have vaddr in the table!
198		 */
199		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
200			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
201			    (KERNBASE + dvp->id_maddr <=
202			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
203				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
204					 whatnot, "maddr", "0x%x");
205				status = 1;
206			}
207		}
208		/*
209		 * Check for IRQ conflicts.
210		 */
211		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
212			if (tmpdvp->id_irq == dvp->id_irq) {
213				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
214					 whatnot, "irq", "%d");
215				status = 1;
216			}
217		}
218		/*
219		 * Check for DRQ conflicts.
220		 */
221		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
222			if (tmpdvp->id_drq == dvp->id_drq) {
223				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
224					 "drq", "%d");
225				status = 1;
226			}
227		}
228	}
229	return (status);
230}
231
232/*
233 * Search through all the isa_devtab_* tables looking for anything that
234 * conflicts with the current device.
235 */
236static int
237haveseen_isadev(dvp, checkbits)
238	struct isa_device *dvp;
239	u_int	checkbits;
240{
241	struct isa_device *tmpdvp;
242	int	status = 0;
243
244	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++)
245		status |= haveseen(dvp, tmpdvp, checkbits);
246	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++)
247		status |= haveseen(dvp, tmpdvp, checkbits);
248	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++)
249		status |= haveseen(dvp, tmpdvp, checkbits);
250	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++)
251		status |= haveseen(dvp, tmpdvp, checkbits);
252	return(status);
253}
254
255/*
256 * Configure all ISA devices
257 */
258void
259isa_configure() {
260	struct isa_device *dvp;
261
262	splhigh();
263	enable_intr();
264	INTREN(IRQ_SLAVE);
265	printf("Probing for devices on the ISA bus:\n");
266	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
267		config_isadev(dvp, &tty_imask);
268	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
269		config_isadev(dvp, &bio_imask);
270	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
271		config_isadev(dvp, &net_imask);
272	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
273		config_isadev(dvp, (u_int *)NULL);
274	bio_imask |= SWI_CLOCK_MASK;
275	net_imask |= SWI_NET_MASK;
276	tty_imask |= SWI_TTY_MASK;
277
278/*
279 * XXX we should really add the tty device to net_imask when the line is
280 * switched to SLIPDISC, and then remove it when it is switched away from
281 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
282 * of them is running slip.
283 *
284 * XXX actually, blocking all ttys during a splimp doesn't matter so much
285 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
286 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
287 * during spltty.
288 */
289#include "sl.h"
290#if NSL > 0
291	net_imask |= tty_imask;
292	tty_imask = net_imask;
293#endif
294	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
295#ifdef DIAGNOSTIC
296	printf("bio_imask %x tty_imask %x net_imask %x\n",
297	       bio_imask, tty_imask, net_imask);
298#endif
299	/*
300	 * Finish initializing intr_mask[].  Note that the partly
301	 * constructed masks aren't actually used since we're at splhigh.
302	 * For fully dynamic initialization, register_intr() and
303	 * unregister_intr() will have to adjust the masks for _all_
304	 * interrupts and for tty_imask, etc.
305	 */
306	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
307		register_imask(dvp, tty_imask);
308	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
309		register_imask(dvp, bio_imask);
310	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
311		register_imask(dvp, net_imask);
312	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
313		register_imask(dvp, SWI_CLOCK_MASK);
314	spl0();
315}
316
317/*
318 * Configure an ISA device.
319 */
320static void
321config_isadev(isdp, mp)
322	struct isa_device *isdp;
323	u_int *mp;
324{
325	u_int checkbits;
326	int id_alive;
327	struct isa_driver *dp = isdp->id_driver;
328
329 	checkbits = 0;
330#ifndef ALLOW_CONFLICT_IRQ
331	checkbits |= CC_IRQ;
332#endif
333#ifndef ALLOW_CONFLICT_DRQ
334	checkbits |= CC_DRQ;
335#endif
336#ifndef ALLOW_CONFLICT_IOADDR
337	checkbits |= CC_IOADDR;
338#endif
339#ifndef ALLOW_CONFLICT_MEMADDR
340	checkbits |= CC_MEMADDR;
341#endif
342	if (haveseen_isadev(isdp, checkbits))
343		return;
344	if (isdp->id_maddr) {
345		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
346		isdp->id_maddr += atdevbase;
347	}
348	id_alive = (*dp->probe)(isdp);
349	if (id_alive) {
350		/*
351		 * Only print the I/O address range if id_alive != -1
352		 * Right now this is a temporary fix just for the new
353		 * NPX code so that if it finds a 486 that can use trap
354		 * 16 it will not report I/O addresses.
355		 * Rod Grimes 04/26/94
356		 */
357		printf("%s%d", dp->name, isdp->id_unit);
358		if (id_alive != -1) {
359 			printf(" at 0x%x", isdp->id_iobase);
360 			if ((isdp->id_iobase + id_alive - 1) !=
361 			     isdp->id_iobase) {
362 				printf("-0x%x",
363				       isdp->id_iobase + id_alive - 1);
364			}
365		}
366		if (isdp->id_irq)
367			printf(" irq %d", ffs(isdp->id_irq) - 1);
368		if (isdp->id_drq != -1)
369			printf(" drq %d", isdp->id_drq);
370		if (isdp->id_maddr)
371			printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
372		if (isdp->id_msize)
373			printf(" msize %d", isdp->id_msize);
374		if (isdp->id_flags)
375			printf(" flags 0x%x", isdp->id_flags);
376		if (isdp->id_iobase) {
377			if (isdp->id_iobase < 0x100) {
378				printf(" on motherboard\n");
379			} else {
380				if (isdp->id_iobase >= 0x1000) {
381					printf (" on eisa\n");
382				} else {
383					printf (" on isa\n");
384				}
385			}
386		}
387		/*
388		 * Check for conflicts again.  The driver may have changed
389		 * *dvp.  We should weaken the early check since the
390		 * driver may have been able to change *dvp to avoid
391		 * conflicts if given a chance.  We already skip the early
392		 * check for IRQs and force a check for IRQs in the next
393		 * group of checks.
394		 */
395#ifndef ALLOW_CONFLICT_IRQ
396		checkbits |= CC_IRQ;
397#endif
398		if (haveseen_isadev(isdp, checkbits))
399			return;
400		isdp->id_alive = id_alive;
401		(*dp->attach)(isdp);
402		if (isdp->id_irq) {
403			if (mp)
404				INTRMASK(*mp, isdp->id_irq);
405			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
406				      isdp->id_ri_flags, isdp->id_intr,
407				      mp ? *mp : 0, isdp->id_unit);
408			INTREN(isdp->id_irq);
409		}
410	} else {
411		printf("%s%d not found", dp->name, isdp->id_unit);
412		if (isdp->id_iobase) {
413			printf(" at 0x%x", isdp->id_iobase);
414		}
415		printf("\n");
416	}
417}
418
419/*
420 * Fill in default interrupt table (in case of spuruious interrupt
421 * during configuration of kernel, setup interrupt control unit
422 */
423void
424isa_defaultirq()
425{
426	int i;
427
428	/* icu vectors */
429	for (i = 0; i < ICU_LEN; i++)
430		unregister_intr(i, (inthand2_t *)NULL);
431
432	/* initialize 8259's */
433	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
434	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
435	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
436#ifdef AUTO_EOI_1
437	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
438#else
439	outb(IO_ICU1+1, 1);		/* 8086 mode */
440#endif
441	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
442	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
443	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
444
445	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
446	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
447	outb(IO_ICU2+1,2);		/* my slave id is 2 */
448#ifdef AUTO_EOI_2
449	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
450#else
451	outb(IO_ICU2+1,1);		/* 8086 mode */
452#endif
453	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
454	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
455}
456
457/* region of physical memory known to be contiguous */
458vm_offset_t isaphysmem;
459static caddr_t dma_bounce[8];		/* XXX */
460static char bounced[8];		/* XXX */
461#define MAXDMASZ 512		/* XXX */
462
463/* high byte of address is stored in this port for i-th dma channel */
464static short dmapageport[8] =
465	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
466
467/*
468 * isa_dmacascade(): program 8237 DMA controller channel to accept
469 * external dma control by a board.
470 */
471void isa_dmacascade(unsigned chan)
472{
473	if (chan > 7)
474		panic("isa_dmacascade: impossible request");
475
476	/* set dma channel mode, and set dma channel mode */
477	if ((chan & 4) == 0) {
478		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
479		outb(DMA1_SMSK, chan);
480	} else {
481		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
482		outb(DMA2_SMSK, chan & 3);
483	}
484}
485
486static int
487isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
488
489/*
490 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
491 * problems by using a bounce buffer.
492 */
493void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
494{	vm_offset_t phys;
495	int waport;
496	caddr_t newaddr;
497
498	if (    chan > 7
499	    || (chan < 4 && nbytes > (1<<16))
500	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
501		panic("isa_dmastart: impossible request");
502
503	if (isa_dmarangecheck(addr, nbytes, chan)) {
504		if (dma_bounce[chan] == 0)
505			dma_bounce[chan] =
506				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
507				(caddr_t) isaphysmem + NBPG*chan;
508		bounced[chan] = 1;
509		newaddr = dma_bounce[chan];
510		*(int *) newaddr = 0;	/* XXX */
511
512		/* copy bounce buffer on write */
513		if (!(flags & B_READ))
514			bcopy(addr, newaddr, nbytes);
515		addr = newaddr;
516	}
517
518	/* translate to physical */
519	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
520
521	if ((chan & 4) == 0) {
522		/*
523		 * Program one of DMA channels 0..3.  These are
524		 * byte mode channels.
525		 */
526		/* set dma channel mode, and reset address ff */
527		if (flags & B_READ)
528			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
529		else
530			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
531		outb(DMA1_FFC, 0);
532
533		/* send start address */
534		waport =  DMA1_CHN(chan);
535		outb(waport, phys);
536		outb(waport, phys>>8);
537		outb(dmapageport[chan], phys>>16);
538
539		/* send count */
540		outb(waport + 1, --nbytes);
541		outb(waport + 1, nbytes>>8);
542
543		/* unmask channel */
544		outb(DMA1_SMSK, chan);
545	} else {
546		/*
547		 * Program one of DMA channels 4..7.  These are
548		 * word mode channels.
549		 */
550		/* set dma channel mode, and reset address ff */
551		if (flags & B_READ)
552			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
553		else
554			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
555		outb(DMA2_FFC, 0);
556
557		/* send start address */
558		waport = DMA2_CHN(chan - 4);
559		outb(waport, phys>>1);
560		outb(waport, phys>>9);
561		outb(dmapageport[chan], phys>>16);
562
563		/* send count */
564		nbytes >>= 1;
565		outb(waport + 2, --nbytes);
566		outb(waport + 2, nbytes>>8);
567
568		/* unmask channel */
569		outb(DMA2_SMSK, chan & 3);
570	}
571}
572
573void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
574{
575
576	/* copy bounce buffer on read */
577	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
578	if (bounced[chan]) {
579		bcopy(dma_bounce[chan], addr, nbytes);
580		bounced[chan] = 0;
581	}
582}
583
584/*
585 * Check for problems with the address range of a DMA transfer
586 * (non-contiguous physical pages, outside of bus address space,
587 * crossing DMA page boundaries).
588 * Return true if special handling needed.
589 */
590
591static int
592isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
593	vm_offset_t phys, priorpage = 0, endva;
594	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
595
596	endva = (vm_offset_t)round_page(va + length);
597	for (; va < (caddr_t) endva ; va += NBPG) {
598		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
599#define ISARAM_END	RAM_END
600		if (phys == 0)
601			panic("isa_dmacheck: no physical page present");
602		if (phys >= ISARAM_END)
603			return (1);
604		if (priorpage) {
605			if (priorpage + NBPG != phys)
606				return (1);
607			/* check if crossing a DMA page boundary */
608			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
609				return (1);
610		}
611		priorpage = phys;
612	}
613	return (0);
614}
615
616/* head of queue waiting for physmem to become available */
617struct buf isa_physmemq;
618
619/* blocked waiting for resource to become free for exclusive use */
620static isaphysmemflag;
621/* if waited for and call requested when free (B_CALL) */
622static void (*isaphysmemunblock)(); /* needs to be a list */
623
624/*
625 * Allocate contiguous physical memory for transfer, returning
626 * a *virtual* address to region. May block waiting for resource.
627 * (assumed to be called at splbio())
628 */
629caddr_t
630isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
631
632	isaphysmemunblock = func;
633	while (isaphysmemflag & B_BUSY) {
634		isaphysmemflag |= B_WANTED;
635		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
636	}
637	isaphysmemflag |= B_BUSY;
638
639	return((caddr_t)isaphysmem);
640}
641
642/*
643 * Free contiguous physical memory used for transfer.
644 * (assumed to be called at splbio())
645 */
646void
647isa_freephysmem(caddr_t va, unsigned length) {
648
649	isaphysmemflag &= ~B_BUSY;
650	if (isaphysmemflag & B_WANTED) {
651		isaphysmemflag &= B_WANTED;
652		wakeup((caddr_t)&isaphysmemflag);
653		if (isaphysmemunblock)
654			(*isaphysmemunblock)();
655	}
656}
657
658#define NMI_PARITY (1 << 7)
659#define NMI_IOCHAN (1 << 6)
660#define ENMI_WATCHDOG (1 << 7)
661#define ENMI_BUSTIMER (1 << 6)
662#define ENMI_IOSTATUS (1 << 5)
663
664/*
665 * Handle a NMI, possibly a machine check.
666 * return true to panic system, false to ignore.
667 */
668int
669isa_nmi(cd)
670	int cd;
671{
672	int isa_port = inb(0x61);
673	int eisa_port = inb(0x461);
674	if(isa_port & NMI_PARITY) {
675		panic("RAM parity error, likely hardware failure.");
676	} else if(isa_port & NMI_IOCHAN) {
677		panic("I/O channel check, likely hardware failure.");
678	} else if(eisa_port & ENMI_WATCHDOG) {
679		panic("EISA watchdog timer expired, likely hardware failure.");
680	} else if(eisa_port & ENMI_BUSTIMER) {
681		panic("EISA bus timeout, likely hardware failure.");
682	} else if(eisa_port & ENMI_IOSTATUS) {
683		panic("EISA I/O port status error.");
684	} else {
685		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
686		return(0);
687	}
688}
689
690/*
691 * Caught a stray interrupt, notify
692 */
693static void
694isa_strayintr(d)
695	int d;
696{
697
698	/* DON'T BOTHER FOR NOW! */
699	/* for some reason, we get bursts of intr #7, even if not enabled! */
700	/*
701	 * Well the reason you got bursts of intr #7 is because someone
702	 * raised an interrupt line and dropped it before the 8259 could
703	 * prioritize it.  This is documented in the intel data book.  This
704	 * means you have BAD hardware!  I have changed this so that only
705	 * the first 5 get logged, then it quits logging them, and puts
706	 * out a special message. rgrimes 3/25/1993
707	 */
708	/*
709	 * XXX TODO print a different message for #7 if it is for a
710	 * glitch.  Glitches can be distinguished from real #7's by
711	 * testing that the in-service bit is _not_ set.  The test
712	 * must be done before sending an EOI so it can't be done if
713	 * we are using AUTO_EOI_1.
714	 */
715	if (intrcnt[NR_DEVICES + d] <= 5)
716		log(LOG_ERR, "stray irq %d\n", d);
717	if (intrcnt[NR_DEVICES + d] == 5)
718		log(LOG_CRIT,
719		    "too many stray irq %d's; not logging any more\n", d);
720}
721
722/*
723 * find an ISA device in a given isa_devtab_* table, given
724 * the table to search, the expected id_driver entry, and the unit number.
725 *
726 * this function is defined in isa_device.h, and this location is debatable;
727 * i put it there because it's useless w/o, and directly operates on
728 * the other stuff in that file.
729 *
730 */
731
732struct isa_device *find_isadev(table, driverp, unit)
733     struct isa_device *table;
734     struct isa_driver *driverp;
735     int unit;
736{
737  if (driverp == NULL) /* sanity check */
738    return NULL;
739
740  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
741    if (table->id_driver == 0)
742      return NULL;
743
744    table++;
745  }
746
747  return table;
748}
749
750/*
751 * Return nonzero if a (masked) irq is pending for a given device.
752 */
753int
754isa_irq_pending(dvp)
755	struct isa_device *dvp;
756{
757	unsigned id_irq;
758
759	id_irq = dvp->id_irq;
760	if (id_irq & 0xff)
761		return (inb(IO_ICU1) & id_irq);
762	return (inb(IO_ICU2) & (id_irq >> 8));
763}
764
765int
766register_intr(intr, device_id, flags, handler, mask, unit)
767	int	intr;
768	int	device_id;
769	u_int	flags;
770	inthand2_t *handler;
771	u_int	mask;
772	int	unit;
773{
774	char	*cp;
775	u_long	ef;
776	int	id;
777
778	if ((u_int)intr >= ICU_LEN || intr == 2
779	    || (u_int)device_id >= NR_DEVICES)
780		return (EINVAL);
781	if (intr_handler[intr] != isa_strayintr)
782		return (EBUSY);
783	ef = read_eflags();
784	disable_intr();
785	intr_countp[intr] = &intrcnt[device_id];
786	intr_handler[intr] = handler;
787	intr_mask[intr] = mask | (1 << intr);
788	intr_unit[intr] = unit;
789	setidt(ICU_OFFSET + intr,
790	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
791	       SDT_SYS386IGT, SEL_KPL);
792	write_eflags(ef);
793	for (cp = intrnames, id = 0; id <= device_id; id++)
794		while (*cp++ != '\0')
795			;
796	if (cp > eintrnames)
797		return (0);
798	if (intr < 10) {
799		cp[-3] = intr + '0';
800		cp[-2] = ' ';
801	} else {
802		cp[-3] = '1';
803		cp[-2] = intr - 10 + '0';
804	}
805	return (0);
806}
807
808static void
809register_imask(dvp, mask)
810	struct isa_device *dvp;
811	u_int	mask;
812{
813	if (dvp->id_alive && dvp->id_irq) {
814		int	intr;
815
816		intr = ffs(dvp->id_irq) - 1;
817		intr_mask[intr] = mask | (1 <<intr);
818	}
819}
820
821int
822unregister_intr(intr, handler)
823	int	intr;
824	inthand2_t *handler;
825{
826	u_long	ef;
827
828	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
829		return (EINVAL);
830	ef = read_eflags();
831	disable_intr();
832	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
833	intr_handler[intr] = isa_strayintr;
834	intr_mask[intr] = HWI_MASK | SWI_MASK;
835	intr_unit[intr] = intr;
836	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
837	write_eflags(ef);
838	return (0);
839}
840