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