isa.c revision 2103
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.20 1994/08/13 03:50:07 wollman 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" : "probe";
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	splnone();
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_DRQ
331	checkbits |= CC_DRQ;
332#endif
333#ifndef ALLOW_CONFLICT_IOADDR
334	checkbits |= CC_IOADDR;
335#endif
336#ifndef ALLOW_CONFLICT_MEMADDR
337	checkbits |= CC_MEMADDR;
338#endif
339	if (haveseen_isadev(isdp, checkbits))
340		return;
341	if (isdp->id_maddr) {
342		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
343		isdp->id_maddr += atdevbase;
344	}
345	id_alive = (*dp->probe)(isdp);
346	if (id_alive) {
347		/*
348		 * Only print the I/O address range if id_alive != -1
349		 * Right now this is a temporary fix just for the new
350		 * NPX code so that if it finds a 486 that can use trap
351		 * 16 it will not report I/O addresses.
352		 * Rod Grimes 04/26/94
353		 */
354		printf("%s%d", dp->name, isdp->id_unit);
355		if (id_alive != -1) {
356 			printf(" at 0x%x", isdp->id_iobase);
357 			if ((isdp->id_iobase + id_alive - 1) !=
358 			     isdp->id_iobase) {
359 				printf("-0x%x",
360				       isdp->id_iobase + id_alive - 1);
361			}
362		}
363		if (isdp->id_irq)
364			printf(" irq %d", ffs(isdp->id_irq) - 1);
365		if (isdp->id_drq != -1)
366			printf(" drq %d", isdp->id_drq);
367		if (isdp->id_maddr)
368			printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
369		if (isdp->id_msize)
370			printf(" msize %d", isdp->id_msize);
371		if (isdp->id_flags)
372			printf(" flags 0x%x", isdp->id_flags);
373		if (isdp->id_iobase) {
374			if (isdp->id_iobase < 0x100) {
375				printf(" on motherboard\n");
376			} else {
377				if (isdp->id_iobase >= 0x1000) {
378					printf (" on eisa\n");
379				} else {
380					printf (" on isa\n");
381				}
382			}
383		}
384		/*
385		 * Check for conflicts again.  The driver may have changed
386		 * *dvp.  We should weaken the early check since the
387		 * driver may have been able to change *dvp to avoid
388		 * conflicts if given a chance.  We already skip the early
389		 * check for IRQs and force a check for IRQs in the next
390		 * group of checks.
391		 */
392		checkbits |= CC_IRQ;
393		if (haveseen_isadev(isdp, checkbits))
394			return;
395		isdp->id_alive = id_alive;
396		(*dp->attach)(isdp);
397		if (isdp->id_irq) {
398			if (mp)
399				INTRMASK(*mp, isdp->id_irq);
400			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
401				      isdp->id_ri_flags, isdp->id_intr,
402				      mp ? *mp : 0, isdp->id_unit);
403			INTREN(isdp->id_irq);
404		}
405	} else {
406		printf("%s%d not found", dp->name, isdp->id_unit);
407		if (isdp->id_iobase) {
408			printf(" at 0x%x", isdp->id_iobase);
409		}
410		printf("\n");
411	}
412}
413
414/*
415 * Fill in default interrupt table (in case of spuruious interrupt
416 * during configuration of kernel, setup interrupt control unit
417 */
418void
419isa_defaultirq()
420{
421	int i;
422
423	/* icu vectors */
424	for (i = 0; i < ICU_LEN; i++)
425		unregister_intr(i, (inthand2_t *)NULL);
426
427	/* initialize 8259's */
428	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
429	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
430	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
431#ifdef AUTO_EOI_1
432	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
433#else
434	outb(IO_ICU1+1, 1);		/* 8086 mode */
435#endif
436	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
437	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
438	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
439
440	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
441	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
442	outb(IO_ICU2+1,2);		/* my slave id is 2 */
443#ifdef AUTO_EOI_2
444	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
445#else
446	outb(IO_ICU2+1,1);		/* 8086 mode */
447#endif
448	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
449	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
450}
451
452/* region of physical memory known to be contiguous */
453vm_offset_t isaphysmem;
454static caddr_t dma_bounce[8];		/* XXX */
455static char bounced[8];		/* XXX */
456#define MAXDMASZ 512		/* XXX */
457
458/* high byte of address is stored in this port for i-th dma channel */
459static short dmapageport[8] =
460	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
461
462/*
463 * isa_dmacascade(): program 8237 DMA controller channel to accept
464 * external dma control by a board.
465 */
466void isa_dmacascade(unsigned chan)
467{
468	if (chan > 7)
469		panic("isa_dmacascade: impossible request");
470
471	/* set dma channel mode, and set dma channel mode */
472	if ((chan & 4) == 0) {
473		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
474		outb(DMA1_SMSK, chan);
475	} else {
476		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
477		outb(DMA2_SMSK, chan & 3);
478	}
479}
480
481static int
482isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
483
484/*
485 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
486 * problems by using a bounce buffer.
487 */
488void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
489{	vm_offset_t phys;
490	int waport;
491	caddr_t newaddr;
492
493	if (    chan > 7
494	    || (chan < 4 && nbytes > (1<<16))
495	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
496		panic("isa_dmastart: impossible request");
497
498	if (isa_dmarangecheck(addr, nbytes, chan)) {
499		if (dma_bounce[chan] == 0)
500			dma_bounce[chan] =
501				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
502				(caddr_t) isaphysmem + NBPG*chan;
503		bounced[chan] = 1;
504		newaddr = dma_bounce[chan];
505		*(int *) newaddr = 0;	/* XXX */
506
507		/* copy bounce buffer on write */
508		if (!(flags & B_READ))
509			bcopy(addr, newaddr, nbytes);
510		addr = newaddr;
511	}
512
513	/* translate to physical */
514	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
515
516	if ((chan & 4) == 0) {
517		/*
518		 * Program one of DMA channels 0..3.  These are
519		 * byte mode channels.
520		 */
521		/* set dma channel mode, and reset address ff */
522		if (flags & B_READ)
523			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
524		else
525			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
526		outb(DMA1_FFC, 0);
527
528		/* send start address */
529		waport =  DMA1_CHN(chan);
530		outb(waport, phys);
531		outb(waport, phys>>8);
532		outb(dmapageport[chan], phys>>16);
533
534		/* send count */
535		outb(waport + 1, --nbytes);
536		outb(waport + 1, nbytes>>8);
537
538		/* unmask channel */
539		outb(DMA1_SMSK, chan);
540	} else {
541		/*
542		 * Program one of DMA channels 4..7.  These are
543		 * word mode channels.
544		 */
545		/* set dma channel mode, and reset address ff */
546		if (flags & B_READ)
547			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
548		else
549			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
550		outb(DMA2_FFC, 0);
551
552		/* send start address */
553		waport = DMA2_CHN(chan - 4);
554		outb(waport, phys>>1);
555		outb(waport, phys>>9);
556		outb(dmapageport[chan], phys>>16);
557
558		/* send count */
559		nbytes >>= 1;
560		outb(waport + 2, --nbytes);
561		outb(waport + 2, nbytes>>8);
562
563		/* unmask channel */
564		outb(DMA2_SMSK, chan & 3);
565	}
566}
567
568void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
569{
570
571	/* copy bounce buffer on read */
572	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
573	if (bounced[chan]) {
574		bcopy(dma_bounce[chan], addr, nbytes);
575		bounced[chan] = 0;
576	}
577}
578
579/*
580 * Check for problems with the address range of a DMA transfer
581 * (non-contiguous physical pages, outside of bus address space,
582 * crossing DMA page boundaries).
583 * Return true if special handling needed.
584 */
585
586static int
587isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
588	vm_offset_t phys, priorpage = 0, endva;
589	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
590
591	endva = (vm_offset_t)round_page(va + length);
592	for (; va < (caddr_t) endva ; va += NBPG) {
593		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
594#define ISARAM_END	RAM_END
595		if (phys == 0)
596			panic("isa_dmacheck: no physical page present");
597		if (phys >= ISARAM_END)
598			return (1);
599		if (priorpage) {
600			if (priorpage + NBPG != phys)
601				return (1);
602			/* check if crossing a DMA page boundary */
603			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
604				return (1);
605		}
606		priorpage = phys;
607	}
608	return (0);
609}
610
611/* head of queue waiting for physmem to become available */
612struct buf isa_physmemq;
613
614/* blocked waiting for resource to become free for exclusive use */
615static isaphysmemflag;
616/* if waited for and call requested when free (B_CALL) */
617static void (*isaphysmemunblock)(); /* needs to be a list */
618
619/*
620 * Allocate contiguous physical memory for transfer, returning
621 * a *virtual* address to region. May block waiting for resource.
622 * (assumed to be called at splbio())
623 */
624caddr_t
625isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
626
627	isaphysmemunblock = func;
628	while (isaphysmemflag & B_BUSY) {
629		isaphysmemflag |= B_WANTED;
630		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
631	}
632	isaphysmemflag |= B_BUSY;
633
634	return((caddr_t)isaphysmem);
635}
636
637/*
638 * Free contiguous physical memory used for transfer.
639 * (assumed to be called at splbio())
640 */
641void
642isa_freephysmem(caddr_t va, unsigned length) {
643
644	isaphysmemflag &= ~B_BUSY;
645	if (isaphysmemflag & B_WANTED) {
646		isaphysmemflag &= B_WANTED;
647		wakeup((caddr_t)&isaphysmemflag);
648		if (isaphysmemunblock)
649			(*isaphysmemunblock)();
650	}
651}
652
653#define NMI_PARITY (1 << 7)
654#define NMI_IOCHAN (1 << 6)
655#define ENMI_WATCHDOG (1 << 7)
656#define ENMI_BUSTIMER (1 << 6)
657#define ENMI_IOSTATUS (1 << 5)
658
659/*
660 * Handle a NMI, possibly a machine check.
661 * return true to panic system, false to ignore.
662 */
663int
664isa_nmi(cd)
665	int cd;
666{
667	int isa_port = inb(0x61);
668	int eisa_port = inb(0x461);
669	if(isa_port & NMI_PARITY) {
670		panic("RAM parity error, likely hardware failure.");
671	} else if(isa_port & NMI_IOCHAN) {
672		panic("I/O channel check, likely hardware failure.");
673	} else if(eisa_port & ENMI_WATCHDOG) {
674		panic("EISA watchdog timer expired, likely hardware failure.");
675	} else if(eisa_port & ENMI_BUSTIMER) {
676		panic("EISA bus timeout, likely hardware failure.");
677	} else if(eisa_port & ENMI_IOSTATUS) {
678		panic("EISA I/O port status error.");
679	} else {
680		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
681		return(0);
682	}
683}
684
685/*
686 * Caught a stray interrupt, notify
687 */
688static void
689isa_strayintr(d)
690	int d;
691{
692
693	/* DON'T BOTHER FOR NOW! */
694	/* for some reason, we get bursts of intr #7, even if not enabled! */
695	/*
696	 * Well the reason you got bursts of intr #7 is because someone
697	 * raised an interrupt line and dropped it before the 8259 could
698	 * prioritize it.  This is documented in the intel data book.  This
699	 * means you have BAD hardware!  I have changed this so that only
700	 * the first 5 get logged, then it quits logging them, and puts
701	 * out a special message. rgrimes 3/25/1993
702	 */
703	/*
704	 * XXX TODO print a different message for #7 if it is for a
705	 * glitch.  Glitches can be distinguished from real #7's by
706	 * testing that the in-service bit is _not_ set.  The test
707	 * must be done before sending an EOI so it can't be done if
708	 * we are using AUTO_EOI_1.
709	 */
710	if (intrcnt[NR_DEVICES + d] <= 5)
711		log(LOG_ERR, "stray irq %d\n", d);
712	if (intrcnt[NR_DEVICES + d] == 5)
713		log(LOG_CRIT,
714		    "too many stray irq %d's; not logging any more\n", d);
715}
716
717/*
718 * find an ISA device in a given isa_devtab_* table, given
719 * the table to search, the expected id_driver entry, and the unit number.
720 *
721 * this function is defined in isa_device.h, and this location is debatable;
722 * i put it there because it's useless w/o, and directly operates on
723 * the other stuff in that file.
724 *
725 */
726
727struct isa_device *find_isadev(table, driverp, unit)
728     struct isa_device *table;
729     struct isa_driver *driverp;
730     int unit;
731{
732  if (driverp == NULL) /* sanity check */
733    return NULL;
734
735  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
736    if (table->id_driver == 0)
737      return NULL;
738
739    table++;
740  }
741
742  return table;
743}
744
745/*
746 * Return nonzero if a (masked) irq is pending for a given device.
747 */
748int
749isa_irq_pending(dvp)
750	struct isa_device *dvp;
751{
752	unsigned id_irq;
753
754	id_irq = dvp->id_irq;
755	if (id_irq & 0xff)
756		return (inb(IO_ICU1) & id_irq);
757	return (inb(IO_ICU2) & (id_irq >> 8));
758}
759
760int
761register_intr(intr, device_id, flags, handler, mask, unit)
762	int	intr;
763	int	device_id;
764	u_int	flags;
765	inthand2_t *handler;
766	u_int	mask;
767	int	unit;
768{
769	char	*cp;
770	u_long	ef;
771	int	id;
772
773	if ((u_int)intr >= ICU_LEN || intr == 2
774	    || (u_int)device_id >= NR_DEVICES)
775		return (EINVAL);
776	if (intr_handler[intr] != isa_strayintr)
777		return (EBUSY);
778	ef = read_eflags();
779	disable_intr();
780	intr_countp[intr] = &intrcnt[device_id];
781	intr_handler[intr] = handler;
782	intr_mask[intr] = mask | (1 << intr);
783	intr_unit[intr] = unit;
784	setidt(ICU_OFFSET + intr,
785	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
786	       SDT_SYS386IGT, SEL_KPL);
787	write_eflags(ef);
788	for (cp = intrnames, id = 0; id <= device_id; id++)
789		while (*cp++ != '\0')
790			;
791	if (cp > eintrnames)
792		return (0);
793	if (intr < 10) {
794		cp[-3] = intr + '0';
795		cp[-2] = ' ';
796	} else {
797		cp[-3] = '1';
798		cp[-2] = intr - 10 + '0';
799	}
800	return (0);
801}
802
803static void
804register_imask(dvp, mask)
805	struct isa_device *dvp;
806	u_int	mask;
807{
808	if (dvp->id_alive && dvp->id_irq) {
809		int	intr;
810
811		intr = ffs(dvp->id_irq) - 1;
812		intr_mask[intr] = mask | (1 <<intr);
813	}
814}
815
816int
817unregister_intr(intr, handler)
818	int	intr;
819	inthand2_t *handler;
820{
821	u_long	ef;
822
823	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
824		return (EINVAL);
825	ef = read_eflags();
826	disable_intr();
827	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
828	intr_handler[intr] = isa_strayintr;
829	intr_mask[intr] = HWI_MASK | SWI_MASK;
830	intr_unit[intr] = intr;
831	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
832	write_eflags(ef);
833	return (0);
834}
835