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