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