isa.c revision 24334
1235368Sgnn/*-
2235368Sgnn * Copyright (c) 1991 The Regents of the University of California.
3235368Sgnn * 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.79 1997/03/25 03:29:40 ache 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 "opt_auto_eoi.h"
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/buf.h>
55#include <sys/syslog.h>
56#include <sys/malloc.h>
57#include <machine/md_var.h>
58#include <machine/segments.h>
59#include <vm/vm.h>
60#include <vm/vm_param.h>
61#include <vm/pmap.h>
62#include <i386/isa/isa_device.h>
63#include <i386/isa/isa.h>
64#include <i386/isa/icu.h>
65#include <i386/isa/ic/i8237.h>
66#include "vector.h"
67
68/*
69**  Register definitions for DMA controller 1 (channels 0..3):
70*/
71#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
72#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
73#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
74#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
75
76/*
77**  Register definitions for DMA controller 2 (channels 4..7):
78*/
79#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
80#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
81#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
82#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
83
84u_long	*intr_countp[ICU_LEN];
85inthand2_t *intr_handler[ICU_LEN];
86u_int	intr_mask[ICU_LEN];
87u_int*	intr_mptr[ICU_LEN];
88int	intr_unit[ICU_LEN];
89
90static inthand_t *fastintr[ICU_LEN] = {
91	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
92	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
93	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
94	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
95	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
96	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
97	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
98	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
99};
100
101static inthand_t *slowintr[ICU_LEN] = {
102	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
103	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
104	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
105	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
106};
107
108static void config_isadev __P((struct isa_device *isdp, u_int *mp));
109static void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
110				 int reconfig));
111static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
112			  int item, char const *whatnot, char const *reason,
113			  char const *format));
114static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
115			 u_int checkbits));
116static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
117static inthand2_t isa_strayintr;
118static void register_imask __P((struct isa_device *dvp, u_int mask));
119
120/*
121 * print a conflict message
122 */
123static void
124conflict(dvp, tmpdvp, item, whatnot, reason, format)
125	struct isa_device	*dvp;
126	struct isa_device	*tmpdvp;
127	int			item;
128	char const		*whatnot;
129	char const		*reason;
130	char const		*format;
131{
132	printf("%s%d not %sed due to %s conflict with %s%d at ",
133		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
134		tmpdvp->id_driver->name, tmpdvp->id_unit);
135	printf(format, item);
136	printf("\n");
137}
138
139/*
140 * Check to see if things are already in use, like IRQ's, I/O addresses
141 * and Memory addresses.
142 */
143static int
144haveseen(dvp, tmpdvp, checkbits)
145	struct isa_device *dvp;
146	struct isa_device *tmpdvp;
147	u_int	checkbits;
148{
149	/*
150	 * Only check against devices that have already been found and are not
151	 * unilaterally allowed to conflict anyway.
152	 */
153	if (tmpdvp->id_alive && !dvp->id_conflicts) {
154		char const *whatnot;
155
156		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
157		/*
158		 * Check for I/O address conflict.  We can only check the
159		 * starting address of the device against the range of the
160		 * device that has already been probed since we do not
161		 * know how many I/O addresses this device uses.
162		 */
163		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
164			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
165			    (dvp->id_iobase <=
166				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
167				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
168					 "I/O address", "0x%x");
169				return 1;
170			}
171		}
172		/*
173		 * Check for Memory address conflict.  We can check for
174		 * range overlap, but it will not catch all cases since the
175		 * driver may adjust the msize paramater during probe, for
176		 * now we just check that the starting address does not
177		 * fall within any allocated region.
178		 * XXX could add a second check after the probe for overlap,
179		 * since at that time we would know the full range.
180		 * XXX KERNBASE is a hack, we should have vaddr in the table!
181		 */
182		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
183			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
184			    (KERNBASE + dvp->id_maddr <=
185			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
186				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
187					 whatnot, "maddr", "0x%x");
188				return 1;
189			}
190		}
191		/*
192		 * Check for IRQ conflicts.
193		 */
194		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
195			if (tmpdvp->id_irq == dvp->id_irq) {
196				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
197					 whatnot, "irq", "%d");
198				return 1;
199			}
200		}
201		/*
202		 * Check for DRQ conflicts.
203		 */
204		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
205			if (tmpdvp->id_drq == dvp->id_drq) {
206				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
207					 "drq", "%d");
208				return 1;
209			}
210		}
211	}
212	return 0;
213}
214
215/*
216 * Search through all the isa_devtab_* tables looking for anything that
217 * conflicts with the current device.
218 */
219int
220haveseen_isadev(dvp, checkbits)
221	struct isa_device *dvp;
222	u_int	checkbits;
223{
224	struct isa_device *tmpdvp;
225	int	status = 0;
226
227	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
228		status |= haveseen(dvp, tmpdvp, checkbits);
229		if (status)
230			return status;
231	}
232	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
233		status |= haveseen(dvp, tmpdvp, checkbits);
234		if (status)
235			return status;
236	}
237	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
238		status |= haveseen(dvp, tmpdvp, checkbits);
239		if (status)
240			return status;
241	}
242	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
243		status |= haveseen(dvp, tmpdvp, checkbits);
244		if (status)
245			return status;
246	}
247	return(status);
248}
249
250/*
251 * Configure all ISA devices
252 */
253void
254isa_configure() {
255	struct isa_device *dvp;
256
257	splhigh();
258	printf("Probing for devices on the ISA bus:\n");
259	/* First probe all the sensitive probes */
260	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
261		if (dvp->id_driver->sensitive_hw)
262			config_isadev(dvp, &tty_imask);
263	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
264		if (dvp->id_driver->sensitive_hw)
265			config_isadev(dvp, &bio_imask);
266	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
267		if (dvp->id_driver->sensitive_hw)
268			config_isadev(dvp, &net_imask);
269	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
270		if (dvp->id_driver->sensitive_hw)
271			config_isadev(dvp, (u_int *)NULL);
272
273	/* Then all the bad ones */
274	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
275		if (!dvp->id_driver->sensitive_hw)
276			config_isadev(dvp, &tty_imask);
277	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
278		if (!dvp->id_driver->sensitive_hw)
279			config_isadev(dvp, &bio_imask);
280	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
281		if (!dvp->id_driver->sensitive_hw)
282			config_isadev(dvp, &net_imask);
283	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
284		if (!dvp->id_driver->sensitive_hw)
285			config_isadev(dvp, (u_int *)NULL);
286
287	bio_imask |= SWI_CLOCK_MASK;
288	net_imask |= SWI_NET_MASK;
289	tty_imask |= SWI_TTY_MASK;
290
291/*
292 * XXX we should really add the tty device to net_imask when the line is
293 * switched to SLIPDISC, and then remove it when it is switched away from
294 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
295 * of them is running slip.
296 *
297 * XXX actually, blocking all ttys during a splimp doesn't matter so much
298 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
299 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
300 * during spltty.
301 */
302#include "sl.h"
303#if NSL > 0
304	net_imask |= tty_imask;
305	tty_imask = net_imask;
306#endif
307
308	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
309
310	if (bootverbose)
311		printf("imasks: bio %x, tty %x, net %x\n",
312		       bio_imask, tty_imask, net_imask);
313
314	/*
315	 * Finish initializing intr_mask[].  Note that the partly
316	 * constructed masks aren't actually used since we're at splhigh.
317	 * For fully dynamic initialization, register_intr() and
318	 * unregister_intr() will have to adjust the masks for _all_
319	 * interrupts and for tty_imask, etc.
320	 */
321	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
322		register_imask(dvp, tty_imask);
323	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
324		register_imask(dvp, bio_imask);
325	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
326		register_imask(dvp, net_imask);
327	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
328		register_imask(dvp, SWI_CLOCK_MASK);
329	spl0();
330}
331
332/*
333 * Configure an ISA device.
334 */
335
336
337static void
338config_isadev(isdp, mp)
339     struct isa_device *isdp;
340     u_int *mp;
341{
342	config_isadev_c(isdp, mp, 0);
343}
344
345void
346reconfig_isadev(isdp, mp)
347	struct isa_device *isdp;
348	u_int *mp;
349{
350	config_isadev_c(isdp, mp, 1);
351}
352
353static void
354config_isadev_c(isdp, mp, reconfig)
355	struct isa_device *isdp;
356	u_int *mp;
357	int reconfig;
358{
359	u_int checkbits;
360	int id_alive;
361	int last_alive;
362	struct isa_driver *dp = isdp->id_driver;
363
364	if (!isdp->id_enabled) {
365		printf("%s%d: disabled, not probed.\n",
366			dp->name, isdp->id_unit);
367		return;
368	}
369	checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR;
370	if (!reconfig && haveseen_isadev(isdp, checkbits))
371		return;
372	if (!reconfig && isdp->id_maddr) {
373		isdp->id_maddr -= ISA_HOLE_START;
374		isdp->id_maddr += atdevbase;
375	}
376	if (reconfig) {
377		last_alive = isdp->id_alive;
378		isdp->id_reconfig = 1;
379	}
380	else {
381		last_alive = 0;
382		isdp->id_reconfig = 0;
383	}
384	id_alive = (*dp->probe)(isdp);
385	if (id_alive) {
386		/*
387		 * Only print the I/O address range if id_alive != -1
388		 * Right now this is a temporary fix just for the new
389		 * NPX code so that if it finds a 486 that can use trap
390		 * 16 it will not report I/O addresses.
391		 * Rod Grimes 04/26/94
392		 */
393		if (!isdp->id_reconfig) {
394			printf("%s%d", dp->name, isdp->id_unit);
395			if (id_alive != -1) {
396				if (isdp->id_iobase == -1)
397					printf(" at ?");
398				else {
399					printf(" at 0x%x", isdp->id_iobase);
400					if (isdp->id_iobase + id_alive - 1 !=
401					    isdp->id_iobase) {
402						printf("-0x%x",
403						       isdp->id_iobase + id_alive - 1);
404					}
405				}
406			}
407			if (isdp->id_irq)
408				printf(" irq %d", ffs(isdp->id_irq) - 1);
409			if (isdp->id_drq != -1)
410				printf(" drq %d", isdp->id_drq);
411			if (isdp->id_maddr)
412				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
413			if (isdp->id_msize)
414				printf(" msize %d", isdp->id_msize);
415			if (isdp->id_flags)
416				printf(" flags 0x%x", isdp->id_flags);
417			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
418				printf(" on motherboard");
419			} else if (isdp->id_iobase >= 0x1000 &&
420				    !(isdp->id_iobase & 0x300)) {
421				printf (" on eisa slot %d",
422					isdp->id_iobase >> 12);
423			} else {
424				printf (" on isa");
425			}
426			printf("\n");
427			/*
428			 * Check for conflicts again.  The driver may have
429			 * changed *dvp.  We should weaken the early check
430			 * since the driver may have been able to change
431			 * *dvp to avoid conflicts if given a chance.  We
432			 * already skip the early check for IRQs and force
433			 * a check for IRQs in the next group of checks.
434			 */
435			checkbits |= CC_IRQ;
436			if (haveseen_isadev(isdp, checkbits))
437				return;
438			isdp->id_alive = id_alive;
439		}
440		(*dp->attach)(isdp);
441		if (isdp->id_irq) {
442			if (mp)
443				INTRMASK(*mp, isdp->id_irq);
444			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
445				      isdp->id_ri_flags, isdp->id_intr,
446				      mp, isdp->id_unit);
447			INTREN(isdp->id_irq);
448		}
449	} else {
450		if (isdp->id_reconfig) {
451			(*dp->attach)(isdp); /* reconfiguration attach */
452		}
453		if (!last_alive) {
454			if (!isdp->id_reconfig) {
455				printf("%s%d not found",
456				       dp->name, isdp->id_unit);
457				if (isdp->id_iobase != -1)
458					printf(" at 0x%x", isdp->id_iobase);
459				printf("\n");
460			}
461		}
462		else {
463			/* This code has not been tested.... */
464			if (isdp->id_irq) {
465				INTRDIS(isdp->id_irq);
466				unregister_intr(ffs(isdp->id_irq) - 1,
467						isdp->id_intr);
468				if (mp)
469					INTRUNMASK(*mp, isdp->id_irq);
470			}
471		}
472	}
473}
474
475/*
476 * Fill in default interrupt table (in case of spuruious interrupt
477 * during configuration of kernel, setup interrupt control unit
478 */
479void
480isa_defaultirq()
481{
482	int i;
483
484	/* icu vectors */
485	for (i = 0; i < ICU_LEN; i++)
486		unregister_intr(i, (inthand2_t *)NULL);
487
488	/* initialize 8259's */
489	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
490	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
491	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
492#ifdef AUTO_EOI_1
493	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
494#else
495	outb(IO_ICU1+1, 1);		/* 8086 mode */
496#endif
497	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
498	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
499	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
500
501	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
502	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
503	outb(IO_ICU2+1,2);		/* my slave id is 2 */
504#ifdef AUTO_EOI_2
505	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
506#else
507	outb(IO_ICU2+1,1);		/* 8086 mode */
508#endif
509	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
510	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
511}
512
513static caddr_t	dma_bouncebuf[8];
514static u_int	dma_bouncebufsize[8];
515static u_int8_t	dma_bounced = 0;
516static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
517static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
518
519#define VALID_DMA_MASK (7)
520
521/* high byte of address is stored in this port for i-th dma channel */
522static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
523
524/*
525 * Setup a DMA channel's bounce buffer.
526 */
527void
528isa_dmainit(chan, bouncebufsize)
529	int chan;
530	u_int bouncebufsize;
531{
532	void *buf;
533
534#ifdef DIAGNOSTIC
535	if (chan & ~VALID_DMA_MASK)
536		panic("isa_dmainit: channel out of range");
537
538	if (dma_bouncebuf[chan] != NULL)
539		panic("isa_dmainit: impossible request");
540#endif
541
542	dma_bouncebufsize[chan] = bouncebufsize;
543
544	/* Try malloc() first.  It works better if it works. */
545	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
546	if (buf != NULL) {
547		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
548			dma_bouncebuf[chan] = buf;
549			return;
550		}
551		free(buf, M_DEVBUF);
552	}
553	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
554			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
555	if (buf == NULL)
556		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
557	else
558		dma_bouncebuf[chan] = buf;
559}
560
561/*
562 * Register a DMA channel's usage.  Usually called from a device driver
563 * in open() or during it's initialization.
564 */
565int
566isa_dma_acquire(chan)
567	int chan;
568{
569#ifdef DIAGNOSTIC
570	if (chan & ~VALID_DMA_MASK)
571		panic("isa_dma_acquire: channel out of range");
572#endif
573
574	if (dma_inuse & (1 << chan)) {
575		printf("isa_dma_acquire: channel %d already in use\n", chan);
576		return (EBUSY);
577	}
578	dma_inuse |= (1 << chan);
579
580	return (0);
581}
582
583/*
584 * Unregister a DMA channel's usage.  Usually called from a device driver
585 * during close() or during it's shutdown.
586 */
587void
588isa_dma_release(chan)
589	int chan;
590{
591#ifdef DIAGNOSTIC
592	if (chan & ~VALID_DMA_MASK)
593		panic("isa_dma_release: channel out of range");
594
595	if (dma_inuse & (1 << chan) == 0)
596		printf("isa_dma_release: channel %d not in use\n", chan);
597#endif
598
599	if (dma_busy & (1 << chan)) {
600		dma_busy &= ~(1 << chan);
601		/*
602		 * XXX We should also do "dma_bounced &= (1 << chan);"
603		 * because we are acting on behalf of isa_dmadone() which
604		 * was not called to end the last DMA operation.  This does
605		 * not matter now, but it may in the future.
606		 */
607	}
608
609	dma_inuse &= ~(1 << chan);
610}
611
612/*
613 * isa_dmacascade(): program 8237 DMA controller channel to accept
614 * external dma control by a board.
615 */
616void isa_dmacascade(chan)
617	int chan;
618{
619#ifdef DIAGNOSTIC
620	if (chan & ~VALID_DMA_MASK)
621		panic("isa_dmacascade: channel out of range");
622#endif
623
624	/* set dma channel mode, and set dma channel mode */
625	if ((chan & 4) == 0) {
626		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
627		outb(DMA1_SMSK, chan);
628	} else {
629		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
630		outb(DMA2_SMSK, chan & 3);
631	}
632}
633
634/*
635 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
636 * problems by using a bounce buffer.
637 */
638void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
639{
640	vm_offset_t phys;
641	int waport;
642	caddr_t newaddr;
643
644#ifdef DIAGNOSTIC
645	if (chan & ~VALID_DMA_MASK)
646		panic("isa_dmastart: channel out of range");
647
648	if ((chan < 4 && nbytes > (1<<16))
649	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
650		panic("isa_dmastart: impossible request");
651
652	if (dma_inuse & (1 << chan) == 0)
653		printf("isa_dmastart: channel %d not acquired\n", chan);
654#endif
655
656	if (dma_busy & (1 << chan))
657		printf("isa_dmastart: channel %d busy\n", chan);
658
659	dma_busy |= (1 << chan);
660
661	if (isa_dmarangecheck(addr, nbytes, chan)) {
662		if (dma_bouncebuf[chan] == NULL
663		    || dma_bouncebufsize[chan] < nbytes)
664			panic("isa_dmastart: bad bounce buffer");
665		dma_bounced |= (1 << chan);
666		newaddr = dma_bouncebuf[chan];
667
668		/* copy bounce buffer on write */
669		if (!(flags & B_READ))
670			bcopy(addr, newaddr, nbytes);
671		addr = newaddr;
672	}
673
674	/* translate to physical */
675	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
676
677	if ((chan & 4) == 0) {
678		/*
679		 * Program one of DMA channels 0..3.  These are
680		 * byte mode channels.
681		 */
682		/* set dma channel mode, and reset address ff */
683
684		/* If B_RAW flag is set, then use autoinitialise mode */
685		if (flags & B_RAW) {
686		  if (flags & B_READ)
687			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
688		  else
689			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
690		}
691		else
692		if (flags & B_READ)
693			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
694		else
695			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
696		outb(DMA1_FFC, 0);
697
698		/* send start address */
699		waport =  DMA1_CHN(chan);
700		outb(waport, phys);
701		outb(waport, phys>>8);
702		outb(dmapageport[chan], phys>>16);
703
704		/* send count */
705		outb(waport + 1, --nbytes);
706		outb(waport + 1, nbytes>>8);
707
708		/* unmask channel */
709		outb(DMA1_SMSK, chan);
710	} else {
711		/*
712		 * Program one of DMA channels 4..7.  These are
713		 * word mode channels.
714		 */
715		/* set dma channel mode, and reset address ff */
716
717		/* If B_RAW flag is set, then use autoinitialise mode */
718		if (flags & B_RAW) {
719		  if (flags & B_READ)
720			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
721		  else
722			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
723		}
724		else
725		if (flags & B_READ)
726			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
727		else
728			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
729		outb(DMA2_FFC, 0);
730
731		/* send start address */
732		waport = DMA2_CHN(chan - 4);
733		outb(waport, phys>>1);
734		outb(waport, phys>>9);
735		outb(dmapageport[chan], phys>>16);
736
737		/* send count */
738		nbytes >>= 1;
739		outb(waport + 2, --nbytes);
740		outb(waport + 2, nbytes>>8);
741
742		/* unmask channel */
743		outb(DMA2_SMSK, chan & 3);
744	}
745}
746
747void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
748{
749#ifdef DIAGNOSTIC
750	if (chan & ~VALID_DMA_MASK)
751		panic("isa_dmadone: channel out of range");
752
753	if (dma_inuse & (1 << chan) == 0)
754		printf("isa_dmadone: channel %d not acquired\n", chan);
755#endif
756
757#if 0
758	/*
759	 * XXX This should be checked, but drivers like ad1848 only call
760	 * isa_dmastart() once because they use Auto DMA mode.  If we
761	 * leave this in, drivers that do this will print this continuously.
762	 */
763	if (dma_busy & (1 << chan) == 0)
764		printf("isa_dmadone: channel %d not busy\n", chan);
765#endif
766
767	if (dma_bounced & (1 << chan)) {
768		/* copy bounce buffer on read */
769		if (flags & B_READ)
770			bcopy(dma_bouncebuf[chan], addr, nbytes);
771
772		dma_bounced &= ~(1 << chan);
773	}
774	dma_busy &= ~(1 << chan);
775}
776
777/*
778 * Check for problems with the address range of a DMA transfer
779 * (non-contiguous physical pages, outside of bus address space,
780 * crossing DMA page boundaries).
781 * Return true if special handling needed.
782 */
783
784static int
785isa_dmarangecheck(caddr_t va, u_int length, int chan) {
786	vm_offset_t phys, priorpage = 0, endva;
787	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
788
789	endva = (vm_offset_t)round_page(va + length);
790	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
791		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
792#define ISARAM_END	RAM_END
793		if (phys == 0)
794			panic("isa_dmacheck: no physical page present");
795		if (phys >= ISARAM_END)
796			return (1);
797		if (priorpage) {
798			if (priorpage + PAGE_SIZE != phys)
799				return (1);
800			/* check if crossing a DMA page boundary */
801			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
802				return (1);
803		}
804		priorpage = phys;
805	}
806	return (0);
807}
808
809#define NMI_PARITY (1 << 7)
810#define NMI_IOCHAN (1 << 6)
811#define ENMI_WATCHDOG (1 << 7)
812#define ENMI_BUSTIMER (1 << 6)
813#define ENMI_IOSTATUS (1 << 5)
814
815/*
816 * Handle a NMI, possibly a machine check.
817 * return true to panic system, false to ignore.
818 */
819int
820isa_nmi(cd)
821	int cd;
822{
823	int isa_port = inb(0x61);
824	int eisa_port = inb(0x461);
825	if(isa_port & NMI_PARITY) {
826		panic("RAM parity error, likely hardware failure.");
827	} else if(isa_port & NMI_IOCHAN) {
828		panic("I/O channel check, likely hardware failure.");
829	} else if(eisa_port & ENMI_WATCHDOG) {
830		panic("EISA watchdog timer expired, likely hardware failure.");
831	} else if(eisa_port & ENMI_BUSTIMER) {
832		panic("EISA bus timeout, likely hardware failure.");
833	} else if(eisa_port & ENMI_IOSTATUS) {
834		panic("EISA I/O port status error.");
835	} else {
836		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
837		return(0);
838	}
839}
840
841/*
842 * Caught a stray interrupt, notify
843 */
844static void
845isa_strayintr(d)
846	int d;
847{
848
849	/* DON'T BOTHER FOR NOW! */
850	/* for some reason, we get bursts of intr #7, even if not enabled! */
851	/*
852	 * Well the reason you got bursts of intr #7 is because someone
853	 * raised an interrupt line and dropped it before the 8259 could
854	 * prioritize it.  This is documented in the intel data book.  This
855	 * means you have BAD hardware!  I have changed this so that only
856	 * the first 5 get logged, then it quits logging them, and puts
857	 * out a special message. rgrimes 3/25/1993
858	 */
859	/*
860	 * XXX TODO print a different message for #7 if it is for a
861	 * glitch.  Glitches can be distinguished from real #7's by
862	 * testing that the in-service bit is _not_ set.  The test
863	 * must be done before sending an EOI so it can't be done if
864	 * we are using AUTO_EOI_1.
865	 */
866	if (intrcnt[NR_DEVICES + d] <= 5)
867		log(LOG_ERR, "stray irq %d\n", d);
868	if (intrcnt[NR_DEVICES + d] == 5)
869		log(LOG_CRIT,
870		    "too many stray irq %d's; not logging any more\n", d);
871}
872
873/*
874 * Find the highest priority enabled display device.  Since we can't
875 * distinguish display devices from ttys, depend on display devices
876 * being sensitive and before sensitive non-display devices (if any)
877 * in isa_devtab_tty.
878 *
879 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
880 */
881struct isa_device *
882find_display()
883{
884	struct isa_device *dvp;
885
886	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
887		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
888			return (dvp);
889	return (NULL);
890}
891
892/*
893 * find an ISA device in a given isa_devtab_* table, given
894 * the table to search, the expected id_driver entry, and the unit number.
895 *
896 * this function is defined in isa_device.h, and this location is debatable;
897 * i put it there because it's useless w/o, and directly operates on
898 * the other stuff in that file.
899 *
900 */
901
902struct isa_device *find_isadev(table, driverp, unit)
903     struct isa_device *table;
904     struct isa_driver *driverp;
905     int unit;
906{
907  if (driverp == NULL) /* sanity check */
908    return NULL;
909
910  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
911    if (table->id_driver == 0)
912      return NULL;
913
914    table++;
915  }
916
917  return table;
918}
919
920/*
921 * Return nonzero if a (masked) irq is pending for a given device.
922 */
923int
924isa_irq_pending(dvp)
925	struct isa_device *dvp;
926{
927	unsigned id_irq;
928
929	id_irq = dvp->id_irq;
930	if (id_irq & 0xff)
931		return (inb(IO_ICU1) & id_irq);
932	return (inb(IO_ICU2) & (id_irq >> 8));
933}
934
935int
936update_intr_masks(void)
937{
938	int intr, n=0;
939	u_int mask,*maskptr;
940
941	for (intr=0; intr < ICU_LEN; intr ++) {
942		if (intr==2) continue;
943		maskptr = intr_mptr[intr];
944		if (!maskptr) continue;
945		*maskptr |= 1 << intr;
946		mask = *maskptr;
947		if (mask != intr_mask[intr]) {
948#if 0
949			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
950				intr, intr_mask[intr], mask, maskptr);
951#endif
952			intr_mask[intr]=mask;
953			n++;
954		}
955
956	}
957	return (n);
958}
959
960int
961register_intr(intr, device_id, flags, handler, maskptr, unit)
962	int	intr;
963	int	device_id;
964	u_int	flags;
965	inthand2_t *handler;
966	u_int	*maskptr;
967	int	unit;
968{
969	char	*cp;
970	u_long	ef;
971	int	id;
972	u_int	mask = (maskptr ? *maskptr : 0);
973
974	if ((u_int)intr >= ICU_LEN || intr == 2
975	    || (u_int)device_id >= NR_DEVICES)
976		return (EINVAL);
977	if (intr_handler[intr] != isa_strayintr)
978		return (EBUSY);
979	ef = read_eflags();
980	disable_intr();
981	intr_countp[intr] = &intrcnt[device_id];
982	intr_handler[intr] = handler;
983	intr_mptr[intr] = maskptr;
984	intr_mask[intr] = mask | (1 << intr);
985	intr_unit[intr] = unit;
986	setidt(ICU_OFFSET + intr,
987	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
988	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
989	write_eflags(ef);
990	for (cp = intrnames, id = 0; id <= device_id; id++)
991		while (*cp++ != '\0')
992			;
993	if (cp > eintrnames)
994		return (0);
995	if (intr < 10) {
996		cp[-3] = intr + '0';
997		cp[-2] = ' ';
998	} else {
999		cp[-3] = '1';
1000		cp[-2] = intr - 10 + '0';
1001	}
1002	return (0);
1003}
1004
1005static void
1006register_imask(dvp, mask)
1007	struct isa_device *dvp;
1008	u_int	mask;
1009{
1010	if (dvp->id_alive && dvp->id_irq) {
1011		int	intr;
1012
1013		intr = ffs(dvp->id_irq) - 1;
1014		intr_mask[intr] = mask | (1 <<intr);
1015	}
1016	(void) update_intr_masks();
1017}
1018
1019int
1020unregister_intr(intr, handler)
1021	int	intr;
1022	inthand2_t *handler;
1023{
1024	u_long	ef;
1025
1026	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1027		return (EINVAL);
1028	ef = read_eflags();
1029	disable_intr();
1030	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1031	intr_handler[intr] = isa_strayintr;
1032	intr_mptr[intr] = NULL;
1033	intr_mask[intr] = HWI_MASK | SWI_MASK;
1034	intr_unit[intr] = intr;
1035	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
1036	    GSEL(GCODE_SEL, SEL_KPL));
1037	write_eflags(ef);
1038	return (0);
1039}
1040