isa.c revision 3670
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.27 1994/10/01 02:56:14 davidg 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	/*
166	 * Only check against devices that have already been found
167	 */
168	if (tmpdvp->id_alive) {
169		char const *whatnot;
170
171		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
172		/*
173		 * Check for I/O address conflict.  We can only check the
174		 * starting address of the device against the range of the
175		 * device that has already been probed since we do not
176		 * know how many I/O addresses this device uses.
177		 */
178		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
179			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
180			    (dvp->id_iobase <=
181				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
182				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
183					 "I/O address", "0x%x");
184				return 1;
185			}
186		}
187		/*
188		 * Check for Memory address conflict.  We can check for
189		 * range overlap, but it will not catch all cases since the
190		 * driver may adjust the msize paramater during probe, for
191		 * now we just check that the starting address does not
192		 * fall within any allocated region.
193		 * XXX could add a second check after the probe for overlap,
194		 * since at that time we would know the full range.
195		 * XXX KERNBASE is a hack, we should have vaddr in the table!
196		 */
197		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
198			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
199			    (KERNBASE + dvp->id_maddr <=
200			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
201				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
202					 whatnot, "maddr", "0x%x");
203				return 1;
204			}
205		}
206		/*
207		 * Check for IRQ conflicts.
208		 */
209		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
210			if (tmpdvp->id_irq == dvp->id_irq) {
211				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
212					 whatnot, "irq", "%d");
213				return 1;
214			}
215		}
216		/*
217		 * Check for DRQ conflicts.
218		 */
219		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
220			if (tmpdvp->id_drq == dvp->id_drq) {
221				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
222					 "drq", "%d");
223				return 1;
224			}
225		}
226	}
227	return 0;
228}
229
230/*
231 * Search through all the isa_devtab_* tables looking for anything that
232 * conflicts with the current device.
233 */
234static int
235haveseen_isadev(dvp, checkbits)
236	struct isa_device *dvp;
237	u_int	checkbits;
238{
239	struct isa_device *tmpdvp;
240	int	status = 0;
241
242	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
243		status |= haveseen(dvp, tmpdvp, checkbits);
244		if (status)
245			return status;
246	}
247	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
248		status |= haveseen(dvp, tmpdvp, checkbits);
249		if (status)
250			return status;
251	}
252	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
253		status |= haveseen(dvp, tmpdvp, checkbits);
254		if (status)
255			return status;
256	}
257	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
258		status |= haveseen(dvp, tmpdvp, checkbits);
259		if (status)
260			return status;
261	}
262	return(status);
263}
264
265/*
266 * Configure all ISA devices
267 */
268void
269isa_configure() {
270	struct isa_device *dvp;
271
272	splhigh();
273	enable_intr();
274	INTREN(IRQ_SLAVE);
275	printf("Probing for devices on the ISA bus:\n");
276	/* First probe all the sensitive probes */
277	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
278		if (dvp->id_driver->sensitive_hw)
279			config_isadev(dvp, &tty_imask);
280	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
281		if (dvp->id_driver->sensitive_hw)
282			config_isadev(dvp, &bio_imask);
283	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
284		if (dvp->id_driver->sensitive_hw)
285			config_isadev(dvp, &net_imask);
286	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
287		if (dvp->id_driver->sensitive_hw)
288			config_isadev(dvp, (u_int *)NULL);
289
290	/* Then all the bad ones */
291	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
292		if (!dvp->id_driver->sensitive_hw)
293			config_isadev(dvp, &tty_imask);
294	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
295		if (!dvp->id_driver->sensitive_hw)
296			config_isadev(dvp, &bio_imask);
297	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
298		if (!dvp->id_driver->sensitive_hw)
299			config_isadev(dvp, &net_imask);
300	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
301		if (!dvp->id_driver->sensitive_hw)
302			config_isadev(dvp, (u_int *)NULL);
303
304	bio_imask |= SWI_CLOCK_MASK;
305	net_imask |= SWI_NET_MASK;
306	tty_imask |= SWI_TTY_MASK;
307
308/*
309 * XXX we should really add the tty device to net_imask when the line is
310 * switched to SLIPDISC, and then remove it when it is switched away from
311 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
312 * of them is running slip.
313 *
314 * XXX actually, blocking all ttys during a splimp doesn't matter so much
315 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
316 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
317 * during spltty.
318 */
319#include "sl.h"
320#if NSL > 0
321	net_imask |= tty_imask;
322	tty_imask = net_imask;
323#endif
324	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
325#ifdef DIAGNOSTIC
326	printf("bio_imask %x tty_imask %x net_imask %x\n",
327	       bio_imask, tty_imask, net_imask);
328#endif
329	/*
330	 * Finish initializing intr_mask[].  Note that the partly
331	 * constructed masks aren't actually used since we're at splhigh.
332	 * For fully dynamic initialization, register_intr() and
333	 * unregister_intr() will have to adjust the masks for _all_
334	 * interrupts and for tty_imask, etc.
335	 */
336	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
337		register_imask(dvp, tty_imask);
338	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
339		register_imask(dvp, bio_imask);
340	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
341		register_imask(dvp, net_imask);
342	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
343		register_imask(dvp, SWI_CLOCK_MASK);
344	spl0();
345}
346
347/*
348 * Configure an ISA device.
349 */
350
351
352static void config_isadev_c();
353
354static void
355config_isadev(isdp, mp)
356     struct isa_device *isdp;
357     u_int *mp;
358{
359	config_isadev_c(isdp, mp, 0);
360}
361
362void
363reconfig_isadev(isdp, mp)
364	struct isa_device *isdp;
365	u_int *mp;
366{
367	config_isadev_c(isdp, mp, 1);
368}
369
370static void
371config_isadev_c(isdp, mp, reconfig)
372	struct isa_device *isdp;
373	u_int *mp;
374	int reconfig;
375{
376	u_int checkbits;
377	int id_alive;
378	int last_alive;
379	struct isa_driver *dp = isdp->id_driver;
380
381 	checkbits = 0;
382#ifndef ALLOW_CONFLICT_IRQ
383	checkbits |= CC_IRQ;
384#endif
385#ifndef ALLOW_CONFLICT_DRQ
386	checkbits |= CC_DRQ;
387#endif
388#ifndef ALLOW_CONFLICT_IOADDR
389	checkbits |= CC_IOADDR;
390#endif
391#ifndef ALLOW_CONFLICT_MEMADDR
392	checkbits |= CC_MEMADDR;
393#endif
394	if (!reconfig && haveseen_isadev(isdp, checkbits))
395		return;
396	if (!reconfig && isdp->id_maddr) {
397		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
398		isdp->id_maddr += atdevbase;
399	}
400	if (reconfig) {
401		last_alive = isdp->id_alive;
402	}
403	else {
404		last_alive = 0;
405	}
406	id_alive = (*dp->probe)(isdp);
407	if (id_alive) {
408		/*
409		 * Only print the I/O address range if id_alive != -1
410		 * Right now this is a temporary fix just for the new
411		 * NPX code so that if it finds a 486 that can use trap
412		 * 16 it will not report I/O addresses.
413		 * Rod Grimes 04/26/94
414		 */
415		if (!isdp->id_reconfig) {
416			printf("%s%d", dp->name, isdp->id_unit);
417			if (id_alive != -1) {
418 				printf(" at 0x%x", isdp->id_iobase);
419 				if ((isdp->id_iobase + id_alive - 1) !=
420 				     isdp->id_iobase) {
421 					printf("-0x%x",
422					       isdp->id_iobase + id_alive - 1);
423				}
424			}
425			if (isdp->id_irq)
426				printf(" irq %d", ffs(isdp->id_irq) - 1);
427			if (isdp->id_drq != -1)
428				printf(" drq %d", isdp->id_drq);
429			if (isdp->id_maddr)
430				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
431			if (isdp->id_msize)
432				printf(" msize %d", isdp->id_msize);
433			if (isdp->id_flags)
434				printf(" flags 0x%x", isdp->id_flags);
435			if (isdp->id_iobase) {
436				if (isdp->id_iobase < 0x100) {
437					printf(" on motherboard\n");
438				} else {
439					if (isdp->id_iobase >= 0x1000) {
440						printf (" on eisa\n");
441					} else {
442						printf (" on isa\n");
443					}
444				}
445			}
446			/*
447			 * Check for conflicts again.  The driver may have
448			 * changed *dvp.  We should weaken the early check
449			 * since the driver may have been able to change
450			 * *dvp to avoid conflicts if given a chance.  We
451			 * already skip the early check for IRQs and force
452			 * a check for IRQs in the next group of checks.
453		 	 */
454#ifndef ALLOW_CONFLICT_IRQ
455			checkbits |= CC_IRQ;
456#endif
457			if (haveseen_isadev(isdp, checkbits))
458				return;
459			isdp->id_alive = id_alive;
460		}
461		(*dp->attach)(isdp);
462		if (isdp->id_irq) {
463			if (mp)
464				INTRMASK(*mp, isdp->id_irq);
465			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
466				      isdp->id_ri_flags, isdp->id_intr,
467				      mp ? *mp : 0, isdp->id_unit);
468			INTREN(isdp->id_irq);
469		}
470	} else {
471		if (isdp->id_reconfig) {
472			(*dp->attach)(isdp); /* reconfiguration attach */
473		}
474		if (!last_alive) {
475			if (!isdp->id_reconfig) {
476				printf("%s%d not found", dp->name, isdp->id_unit);
477				if (isdp->id_iobase) {
478					printf(" at 0x%x", isdp->id_iobase);
479				}
480				printf("\n");
481			}
482		}
483		else {
484			/* This code has not been tested.... */
485			if (isdp->id_irq) {
486				INTRDIS(isdp->id_irq);
487				unregister_intr(ffs(isdp->id_irq) - 1,
488						isdp->id_intr);
489				if (mp)
490					INTRUNMASK(*mp, isdp->id_irq);
491			}
492		}
493	}
494}
495
496/*
497 * Fill in default interrupt table (in case of spuruious interrupt
498 * during configuration of kernel, setup interrupt control unit
499 */
500void
501isa_defaultirq()
502{
503	int i;
504
505	/* icu vectors */
506	for (i = 0; i < ICU_LEN; i++)
507		unregister_intr(i, (inthand2_t *)NULL);
508
509	/* initialize 8259's */
510	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
511	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
512	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
513#ifdef AUTO_EOI_1
514	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
515#else
516	outb(IO_ICU1+1, 1);		/* 8086 mode */
517#endif
518	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
519	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
520	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
521
522	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
523	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
524	outb(IO_ICU2+1,2);		/* my slave id is 2 */
525#ifdef AUTO_EOI_2
526	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
527#else
528	outb(IO_ICU2+1,1);		/* 8086 mode */
529#endif
530	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
531	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
532}
533
534/* region of physical memory known to be contiguous */
535vm_offset_t isaphysmem;
536static caddr_t dma_bounce[8];		/* XXX */
537static char bounced[8];		/* XXX */
538#define MAXDMASZ 512		/* XXX */
539
540/* high byte of address is stored in this port for i-th dma channel */
541static short dmapageport[8] =
542	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
543
544/*
545 * isa_dmacascade(): program 8237 DMA controller channel to accept
546 * external dma control by a board.
547 */
548void isa_dmacascade(unsigned chan)
549{
550	if (chan > 7)
551		panic("isa_dmacascade: impossible request");
552
553	/* set dma channel mode, and set dma channel mode */
554	if ((chan & 4) == 0) {
555		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
556		outb(DMA1_SMSK, chan);
557	} else {
558		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
559		outb(DMA2_SMSK, chan & 3);
560	}
561}
562
563static int
564isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
565
566/*
567 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
568 * problems by using a bounce buffer.
569 */
570void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
571{	vm_offset_t phys;
572	int waport;
573	caddr_t newaddr;
574
575	if (    chan > 7
576	    || (chan < 4 && nbytes > (1<<16))
577	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
578		panic("isa_dmastart: impossible request");
579
580	if (isa_dmarangecheck(addr, nbytes, chan)) {
581		if (dma_bounce[chan] == 0)
582			dma_bounce[chan] =
583				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
584				(caddr_t) isaphysmem + NBPG*chan;
585		bounced[chan] = 1;
586		newaddr = dma_bounce[chan];
587		*(int *) newaddr = 0;	/* XXX */
588
589		/* copy bounce buffer on write */
590		if (!(flags & B_READ))
591			bcopy(addr, newaddr, nbytes);
592		addr = newaddr;
593	}
594
595	/* translate to physical */
596	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
597
598	if ((chan & 4) == 0) {
599		/*
600		 * Program one of DMA channels 0..3.  These are
601		 * byte mode channels.
602		 */
603		/* set dma channel mode, and reset address ff */
604		if (flags & B_READ)
605			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
606		else
607			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
608		outb(DMA1_FFC, 0);
609
610		/* send start address */
611		waport =  DMA1_CHN(chan);
612		outb(waport, phys);
613		outb(waport, phys>>8);
614		outb(dmapageport[chan], phys>>16);
615
616		/* send count */
617		outb(waport + 1, --nbytes);
618		outb(waport + 1, nbytes>>8);
619
620		/* unmask channel */
621		outb(DMA1_SMSK, chan);
622	} else {
623		/*
624		 * Program one of DMA channels 4..7.  These are
625		 * word mode channels.
626		 */
627		/* set dma channel mode, and reset address ff */
628		if (flags & B_READ)
629			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
630		else
631			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
632		outb(DMA2_FFC, 0);
633
634		/* send start address */
635		waport = DMA2_CHN(chan - 4);
636		outb(waport, phys>>1);
637		outb(waport, phys>>9);
638		outb(dmapageport[chan], phys>>16);
639
640		/* send count */
641		nbytes >>= 1;
642		outb(waport + 2, --nbytes);
643		outb(waport + 2, nbytes>>8);
644
645		/* unmask channel */
646		outb(DMA2_SMSK, chan & 3);
647	}
648}
649
650void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
651{
652
653	/* copy bounce buffer on read */
654	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
655	if (bounced[chan]) {
656		bcopy(dma_bounce[chan], addr, nbytes);
657		bounced[chan] = 0;
658	}
659}
660
661/*
662 * Check for problems with the address range of a DMA transfer
663 * (non-contiguous physical pages, outside of bus address space,
664 * crossing DMA page boundaries).
665 * Return true if special handling needed.
666 */
667
668static int
669isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
670	vm_offset_t phys, priorpage = 0, endva;
671	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
672
673	endva = (vm_offset_t)round_page(va + length);
674	for (; va < (caddr_t) endva ; va += NBPG) {
675		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
676#define ISARAM_END	RAM_END
677		if (phys == 0)
678			panic("isa_dmacheck: no physical page present");
679		if (phys >= ISARAM_END)
680			return (1);
681		if (priorpage) {
682			if (priorpage + NBPG != phys)
683				return (1);
684			/* check if crossing a DMA page boundary */
685			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
686				return (1);
687		}
688		priorpage = phys;
689	}
690	return (0);
691}
692
693/* head of queue waiting for physmem to become available */
694struct buf isa_physmemq;
695
696/* blocked waiting for resource to become free for exclusive use */
697static isaphysmemflag;
698/* if waited for and call requested when free (B_CALL) */
699static void (*isaphysmemunblock)(); /* needs to be a list */
700
701/*
702 * Allocate contiguous physical memory for transfer, returning
703 * a *virtual* address to region. May block waiting for resource.
704 * (assumed to be called at splbio())
705 */
706caddr_t
707isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
708
709	isaphysmemunblock = func;
710	while (isaphysmemflag & B_BUSY) {
711		isaphysmemflag |= B_WANTED;
712		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
713	}
714	isaphysmemflag |= B_BUSY;
715
716	return((caddr_t)isaphysmem);
717}
718
719/*
720 * Free contiguous physical memory used for transfer.
721 * (assumed to be called at splbio())
722 */
723void
724isa_freephysmem(caddr_t va, unsigned length) {
725
726	isaphysmemflag &= ~B_BUSY;
727	if (isaphysmemflag & B_WANTED) {
728		isaphysmemflag &= B_WANTED;
729		wakeup((caddr_t)&isaphysmemflag);
730		if (isaphysmemunblock)
731			(*isaphysmemunblock)();
732	}
733}
734
735#define NMI_PARITY (1 << 7)
736#define NMI_IOCHAN (1 << 6)
737#define ENMI_WATCHDOG (1 << 7)
738#define ENMI_BUSTIMER (1 << 6)
739#define ENMI_IOSTATUS (1 << 5)
740
741/*
742 * Handle a NMI, possibly a machine check.
743 * return true to panic system, false to ignore.
744 */
745int
746isa_nmi(cd)
747	int cd;
748{
749	int isa_port = inb(0x61);
750	int eisa_port = inb(0x461);
751	if(isa_port & NMI_PARITY) {
752		panic("RAM parity error, likely hardware failure.");
753	} else if(isa_port & NMI_IOCHAN) {
754		panic("I/O channel check, likely hardware failure.");
755	} else if(eisa_port & ENMI_WATCHDOG) {
756		panic("EISA watchdog timer expired, likely hardware failure.");
757	} else if(eisa_port & ENMI_BUSTIMER) {
758		panic("EISA bus timeout, likely hardware failure.");
759	} else if(eisa_port & ENMI_IOSTATUS) {
760		panic("EISA I/O port status error.");
761	} else {
762		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
763		return(0);
764	}
765}
766
767/*
768 * Caught a stray interrupt, notify
769 */
770static void
771isa_strayintr(d)
772	int d;
773{
774
775	/* DON'T BOTHER FOR NOW! */
776	/* for some reason, we get bursts of intr #7, even if not enabled! */
777	/*
778	 * Well the reason you got bursts of intr #7 is because someone
779	 * raised an interrupt line and dropped it before the 8259 could
780	 * prioritize it.  This is documented in the intel data book.  This
781	 * means you have BAD hardware!  I have changed this so that only
782	 * the first 5 get logged, then it quits logging them, and puts
783	 * out a special message. rgrimes 3/25/1993
784	 */
785	/*
786	 * XXX TODO print a different message for #7 if it is for a
787	 * glitch.  Glitches can be distinguished from real #7's by
788	 * testing that the in-service bit is _not_ set.  The test
789	 * must be done before sending an EOI so it can't be done if
790	 * we are using AUTO_EOI_1.
791	 */
792	if (intrcnt[NR_DEVICES + d] <= 5)
793		log(LOG_ERR, "stray irq %d\n", d);
794	if (intrcnt[NR_DEVICES + d] == 5)
795		log(LOG_CRIT,
796		    "too many stray irq %d's; not logging any more\n", d);
797}
798
799/*
800 * find an ISA device in a given isa_devtab_* table, given
801 * the table to search, the expected id_driver entry, and the unit number.
802 *
803 * this function is defined in isa_device.h, and this location is debatable;
804 * i put it there because it's useless w/o, and directly operates on
805 * the other stuff in that file.
806 *
807 */
808
809struct isa_device *find_isadev(table, driverp, unit)
810     struct isa_device *table;
811     struct isa_driver *driverp;
812     int unit;
813{
814  if (driverp == NULL) /* sanity check */
815    return NULL;
816
817  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
818    if (table->id_driver == 0)
819      return NULL;
820
821    table++;
822  }
823
824  return table;
825}
826
827/*
828 * Return nonzero if a (masked) irq is pending for a given device.
829 */
830int
831isa_irq_pending(dvp)
832	struct isa_device *dvp;
833{
834	unsigned id_irq;
835
836	id_irq = dvp->id_irq;
837	if (id_irq & 0xff)
838		return (inb(IO_ICU1) & id_irq);
839	return (inb(IO_ICU2) & (id_irq >> 8));
840}
841
842int
843register_intr(intr, device_id, flags, handler, mask, unit)
844	int	intr;
845	int	device_id;
846	u_int	flags;
847	inthand2_t *handler;
848	u_int	mask;
849	int	unit;
850{
851	char	*cp;
852	u_long	ef;
853	int	id;
854
855	if ((u_int)intr >= ICU_LEN || intr == 2
856	    || (u_int)device_id >= NR_DEVICES)
857		return (EINVAL);
858	if (intr_handler[intr] != isa_strayintr)
859		return (EBUSY);
860	ef = read_eflags();
861	disable_intr();
862	intr_countp[intr] = &intrcnt[device_id];
863	intr_handler[intr] = handler;
864	intr_mask[intr] = mask | (1 << intr);
865	intr_unit[intr] = unit;
866	setidt(ICU_OFFSET + intr,
867	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
868	       SDT_SYS386IGT, SEL_KPL);
869	write_eflags(ef);
870	for (cp = intrnames, id = 0; id <= device_id; id++)
871		while (*cp++ != '\0')
872			;
873	if (cp > eintrnames)
874		return (0);
875	if (intr < 10) {
876		cp[-3] = intr + '0';
877		cp[-2] = ' ';
878	} else {
879		cp[-3] = '1';
880		cp[-2] = intr - 10 + '0';
881	}
882	return (0);
883}
884
885static void
886register_imask(dvp, mask)
887	struct isa_device *dvp;
888	u_int	mask;
889{
890	if (dvp->id_alive && dvp->id_irq) {
891		int	intr;
892
893		intr = ffs(dvp->id_irq) - 1;
894		intr_mask[intr] = mask | (1 <<intr);
895	}
896}
897
898int
899unregister_intr(intr, handler)
900	int	intr;
901	inthand2_t *handler;
902{
903	u_long	ef;
904
905	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
906		return (EINVAL);
907	ef = read_eflags();
908	disable_intr();
909	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
910	intr_handler[intr] = isa_strayintr;
911	intr_mask[intr] = HWI_MASK | SWI_MASK;
912	intr_unit[intr] = intr;
913	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
914	write_eflags(ef);
915	return (0);
916}
917