isa.c revision 25216
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.82 1997/04/27 21:18:58 fsmp 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#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 (mp)
466				INTRMASK(*mp, isdp->id_irq);
467			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
468				      isdp->id_ri_flags, isdp->id_intr,
469				      mp, isdp->id_unit);
470			INTREN(isdp->id_irq);
471		}
472	} else {
473		if (isdp->id_reconfig) {
474			(*dp->attach)(isdp); /* reconfiguration attach */
475		}
476		if (!last_alive) {
477			if (!isdp->id_reconfig) {
478				printf("%s%d not found",
479				       dp->name, isdp->id_unit);
480				if (isdp->id_iobase != -1)
481					printf(" at 0x%x", isdp->id_iobase);
482				printf("\n");
483			}
484		}
485		else {
486			/* This code has not been tested.... */
487			if (isdp->id_irq) {
488				INTRDIS(isdp->id_irq);
489				unregister_intr(ffs(isdp->id_irq) - 1,
490						isdp->id_intr);
491				if (mp)
492					INTRUNMASK(*mp, isdp->id_irq);
493			}
494		}
495	}
496}
497
498/*
499 * Fill in default interrupt table (in case of spuruious interrupt
500 * during configuration of kernel, setup interrupt control unit
501 */
502void
503isa_defaultirq()
504{
505	int i;
506
507	/* icu vectors */
508	for (i = 0; i < ICU_LEN; i++)
509		unregister_intr(i, (inthand2_t *)NULL);
510
511	/* initialize 8259's */
512	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
513	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
514	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
515#ifdef AUTO_EOI_1
516	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
517#else
518	outb(IO_ICU1+1, 1);		/* 8086 mode */
519#endif
520	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
521	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
522	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
523
524	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
525	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
526	outb(IO_ICU2+1,2);		/* my slave id is 2 */
527#ifdef AUTO_EOI_2
528	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
529#else
530	outb(IO_ICU2+1,1);		/* 8086 mode */
531#endif
532	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
533	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
534}
535
536static caddr_t	dma_bouncebuf[8];
537static u_int	dma_bouncebufsize[8];
538static u_int8_t	dma_bounced = 0;
539static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
540static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
541
542#define VALID_DMA_MASK (7)
543
544/* high byte of address is stored in this port for i-th dma channel */
545static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
546
547/*
548 * Setup a DMA channel's bounce buffer.
549 */
550void
551isa_dmainit(chan, bouncebufsize)
552	int chan;
553	u_int bouncebufsize;
554{
555	void *buf;
556
557#ifdef DIAGNOSTIC
558	if (chan & ~VALID_DMA_MASK)
559		panic("isa_dmainit: channel out of range");
560
561	if (dma_bouncebuf[chan] != NULL)
562		panic("isa_dmainit: impossible request");
563#endif
564
565	dma_bouncebufsize[chan] = bouncebufsize;
566
567	/* Try malloc() first.  It works better if it works. */
568	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
569	if (buf != NULL) {
570		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
571			dma_bouncebuf[chan] = buf;
572			return;
573		}
574		free(buf, M_DEVBUF);
575	}
576	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
577			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
578	if (buf == NULL)
579		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
580	else
581		dma_bouncebuf[chan] = buf;
582}
583
584/*
585 * Register a DMA channel's usage.  Usually called from a device driver
586 * in open() or during it's initialization.
587 */
588int
589isa_dma_acquire(chan)
590	int chan;
591{
592#ifdef DIAGNOSTIC
593	if (chan & ~VALID_DMA_MASK)
594		panic("isa_dma_acquire: channel out of range");
595#endif
596
597	if (dma_inuse & (1 << chan)) {
598		printf("isa_dma_acquire: channel %d already in use\n", chan);
599		return (EBUSY);
600	}
601	dma_inuse |= (1 << chan);
602
603	return (0);
604}
605
606/*
607 * Unregister a DMA channel's usage.  Usually called from a device driver
608 * during close() or during it's shutdown.
609 */
610void
611isa_dma_release(chan)
612	int chan;
613{
614#ifdef DIAGNOSTIC
615	if (chan & ~VALID_DMA_MASK)
616		panic("isa_dma_release: channel out of range");
617
618	if (dma_inuse & (1 << chan) == 0)
619		printf("isa_dma_release: channel %d not in use\n", chan);
620#endif
621
622	if (dma_busy & (1 << chan)) {
623		dma_busy &= ~(1 << chan);
624		/*
625		 * XXX We should also do "dma_bounced &= (1 << chan);"
626		 * because we are acting on behalf of isa_dmadone() which
627		 * was not called to end the last DMA operation.  This does
628		 * not matter now, but it may in the future.
629		 */
630	}
631
632	dma_inuse &= ~(1 << chan);
633}
634
635/*
636 * isa_dmacascade(): program 8237 DMA controller channel to accept
637 * external dma control by a board.
638 */
639void isa_dmacascade(chan)
640	int chan;
641{
642#ifdef DIAGNOSTIC
643	if (chan & ~VALID_DMA_MASK)
644		panic("isa_dmacascade: channel out of range");
645#endif
646
647	/* set dma channel mode, and set dma channel mode */
648	if ((chan & 4) == 0) {
649		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
650		outb(DMA1_SMSK, chan);
651	} else {
652		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
653		outb(DMA2_SMSK, chan & 3);
654	}
655}
656
657/*
658 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
659 * problems by using a bounce buffer.
660 */
661void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
662{
663	vm_offset_t phys;
664	int waport;
665	caddr_t newaddr;
666
667#ifdef DIAGNOSTIC
668	if (chan & ~VALID_DMA_MASK)
669		panic("isa_dmastart: channel out of range");
670
671	if ((chan < 4 && nbytes > (1<<16))
672	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
673		panic("isa_dmastart: impossible request");
674
675	if (dma_inuse & (1 << chan) == 0)
676		printf("isa_dmastart: channel %d not acquired\n", chan);
677#endif
678
679	if (dma_busy & (1 << chan))
680		printf("isa_dmastart: channel %d busy\n", chan);
681
682	dma_busy |= (1 << chan);
683
684	if (isa_dmarangecheck(addr, nbytes, chan)) {
685		if (dma_bouncebuf[chan] == NULL
686		    || dma_bouncebufsize[chan] < nbytes)
687			panic("isa_dmastart: bad bounce buffer");
688		dma_bounced |= (1 << chan);
689		newaddr = dma_bouncebuf[chan];
690
691		/* copy bounce buffer on write */
692		if (!(flags & B_READ))
693			bcopy(addr, newaddr, nbytes);
694		addr = newaddr;
695	}
696
697	/* translate to physical */
698	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
699
700	if ((chan & 4) == 0) {
701		/*
702		 * Program one of DMA channels 0..3.  These are
703		 * byte mode channels.
704		 */
705		/* set dma channel mode, and reset address ff */
706
707		/* If B_RAW flag is set, then use autoinitialise mode */
708		if (flags & B_RAW) {
709		  if (flags & B_READ)
710			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
711		  else
712			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
713		}
714		else
715		if (flags & B_READ)
716			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
717		else
718			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
719		outb(DMA1_FFC, 0);
720
721		/* send start address */
722		waport =  DMA1_CHN(chan);
723		outb(waport, phys);
724		outb(waport, phys>>8);
725		outb(dmapageport[chan], phys>>16);
726
727		/* send count */
728		outb(waport + 1, --nbytes);
729		outb(waport + 1, nbytes>>8);
730
731		/* unmask channel */
732		outb(DMA1_SMSK, chan);
733	} else {
734		/*
735		 * Program one of DMA channels 4..7.  These are
736		 * word mode channels.
737		 */
738		/* set dma channel mode, and reset address ff */
739
740		/* If B_RAW flag is set, then use autoinitialise mode */
741		if (flags & B_RAW) {
742		  if (flags & B_READ)
743			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
744		  else
745			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
746		}
747		else
748		if (flags & B_READ)
749			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
750		else
751			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
752		outb(DMA2_FFC, 0);
753
754		/* send start address */
755		waport = DMA2_CHN(chan - 4);
756		outb(waport, phys>>1);
757		outb(waport, phys>>9);
758		outb(dmapageport[chan], phys>>16);
759
760		/* send count */
761		nbytes >>= 1;
762		outb(waport + 2, --nbytes);
763		outb(waport + 2, nbytes>>8);
764
765		/* unmask channel */
766		outb(DMA2_SMSK, chan & 3);
767	}
768}
769
770void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
771{
772#ifdef DIAGNOSTIC
773	if (chan & ~VALID_DMA_MASK)
774		panic("isa_dmadone: channel out of range");
775
776	if (dma_inuse & (1 << chan) == 0)
777		printf("isa_dmadone: channel %d not acquired\n", chan);
778#endif
779
780#if 0
781	/*
782	 * XXX This should be checked, but drivers like ad1848 only call
783	 * isa_dmastart() once because they use Auto DMA mode.  If we
784	 * leave this in, drivers that do this will print this continuously.
785	 */
786	if (dma_busy & (1 << chan) == 0)
787		printf("isa_dmadone: channel %d not busy\n", chan);
788#endif
789
790	if (dma_bounced & (1 << chan)) {
791		/* copy bounce buffer on read */
792		if (flags & B_READ)
793			bcopy(dma_bouncebuf[chan], addr, nbytes);
794
795		dma_bounced &= ~(1 << chan);
796	}
797	dma_busy &= ~(1 << chan);
798}
799
800/*
801 * Check for problems with the address range of a DMA transfer
802 * (non-contiguous physical pages, outside of bus address space,
803 * crossing DMA page boundaries).
804 * Return true if special handling needed.
805 */
806
807static int
808isa_dmarangecheck(caddr_t va, u_int length, int chan) {
809	vm_offset_t phys, priorpage = 0, endva;
810	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
811
812	endva = (vm_offset_t)round_page(va + length);
813	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
814		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
815#define ISARAM_END	RAM_END
816		if (phys == 0)
817			panic("isa_dmacheck: no physical page present");
818		if (phys >= ISARAM_END)
819			return (1);
820		if (priorpage) {
821			if (priorpage + PAGE_SIZE != phys)
822				return (1);
823			/* check if crossing a DMA page boundary */
824			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
825				return (1);
826		}
827		priorpage = phys;
828	}
829	return (0);
830}
831
832#define NMI_PARITY (1 << 7)
833#define NMI_IOCHAN (1 << 6)
834#define ENMI_WATCHDOG (1 << 7)
835#define ENMI_BUSTIMER (1 << 6)
836#define ENMI_IOSTATUS (1 << 5)
837
838/*
839 * Handle a NMI, possibly a machine check.
840 * return true to panic system, false to ignore.
841 */
842int
843isa_nmi(cd)
844	int cd;
845{
846	int isa_port = inb(0x61);
847	int eisa_port = inb(0x461);
848	if(isa_port & NMI_PARITY) {
849		panic("RAM parity error, likely hardware failure.");
850	} else if(isa_port & NMI_IOCHAN) {
851		panic("I/O channel check, likely hardware failure.");
852	} else if(eisa_port & ENMI_WATCHDOG) {
853		panic("EISA watchdog timer expired, likely hardware failure.");
854	} else if(eisa_port & ENMI_BUSTIMER) {
855		panic("EISA bus timeout, likely hardware failure.");
856	} else if(eisa_port & ENMI_IOSTATUS) {
857		panic("EISA I/O port status error.");
858	} else {
859		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
860		return(0);
861	}
862}
863
864/*
865 * Caught a stray interrupt, notify
866 */
867static void
868isa_strayintr(d)
869	int d;
870{
871
872	/* DON'T BOTHER FOR NOW! */
873	/* for some reason, we get bursts of intr #7, even if not enabled! */
874	/*
875	 * Well the reason you got bursts of intr #7 is because someone
876	 * raised an interrupt line and dropped it before the 8259 could
877	 * prioritize it.  This is documented in the intel data book.  This
878	 * means you have BAD hardware!  I have changed this so that only
879	 * the first 5 get logged, then it quits logging them, and puts
880	 * out a special message. rgrimes 3/25/1993
881	 */
882	/*
883	 * XXX TODO print a different message for #7 if it is for a
884	 * glitch.  Glitches can be distinguished from real #7's by
885	 * testing that the in-service bit is _not_ set.  The test
886	 * must be done before sending an EOI so it can't be done if
887	 * we are using AUTO_EOI_1.
888	 */
889	if (intrcnt[NR_DEVICES + d] <= 5)
890		log(LOG_ERR, "stray irq %d\n", d);
891	if (intrcnt[NR_DEVICES + d] == 5)
892		log(LOG_CRIT,
893		    "too many stray irq %d's; not logging any more\n", d);
894}
895
896/*
897 * Find the highest priority enabled display device.  Since we can't
898 * distinguish display devices from ttys, depend on display devices
899 * being sensitive and before sensitive non-display devices (if any)
900 * in isa_devtab_tty.
901 *
902 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
903 */
904struct isa_device *
905find_display()
906{
907	struct isa_device *dvp;
908
909	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
910		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
911			return (dvp);
912	return (NULL);
913}
914
915/*
916 * find an ISA device in a given isa_devtab_* table, given
917 * the table to search, the expected id_driver entry, and the unit number.
918 *
919 * this function is defined in isa_device.h, and this location is debatable;
920 * i put it there because it's useless w/o, and directly operates on
921 * the other stuff in that file.
922 *
923 */
924
925struct isa_device *find_isadev(table, driverp, unit)
926     struct isa_device *table;
927     struct isa_driver *driverp;
928     int unit;
929{
930  if (driverp == NULL) /* sanity check */
931    return NULL;
932
933  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
934    if (table->id_driver == 0)
935      return NULL;
936
937    table++;
938  }
939
940  return table;
941}
942
943/*
944 * Return nonzero if a (masked) irq is pending for a given device.
945 */
946#if defined(APIC_IO)
947
948int
949isa_irq_pending(dvp)
950	struct isa_device *dvp;
951{
952	/* read APIC IRR containing the 16 ISA INTerrupts */
953	return ((apic_base[APIC_IRR1] & 0x00ffffff)
954		& (u_int32_t)dvp->id_irq) ? 1 : 0;
955}
956
957/*
958 * an 8259 specific routine,
959 * for use by boot probes in certain device drivers.
960 */
961int
962icu_irq_pending(dvp)
963	struct isa_device *dvp;
964{
965	unsigned id_irq;
966	id_irq = dvp->id_irq;
967	if (id_irq & 0xff)
968		return (inb(IO_ICU1) & id_irq);
969	return (inb(IO_ICU2) & (id_irq >> 8));
970}
971
972#else /* APIC_IO */
973
974int
975isa_irq_pending(dvp)
976	struct isa_device *dvp;
977{
978	unsigned id_irq;
979	id_irq = dvp->id_irq;
980	if (id_irq & 0xff)
981		return (inb(IO_ICU1) & id_irq);
982	return (inb(IO_ICU2) & (id_irq >> 8));
983}
984
985#endif /* APIC_IO */
986
987int
988update_intr_masks(void)
989{
990	int intr, n=0;
991	u_int mask,*maskptr;
992
993	for (intr=0; intr < ICU_LEN; intr ++) {
994#if defined(APIC_IO)
995		/* no 8259 SLAVE to ignore */
996#else
997		if (intr==2) continue;	/* ignore 8259 SLAVE output */
998#endif /* APIC_IO */
999		maskptr = intr_mptr[intr];
1000		if (!maskptr) continue;
1001		*maskptr |= 1 << intr;
1002		mask = *maskptr;
1003		if (mask != intr_mask[intr]) {
1004#if 0
1005			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
1006				intr, intr_mask[intr], mask, maskptr);
1007#endif
1008			intr_mask[intr]=mask;
1009			n++;
1010		}
1011
1012	}
1013	return (n);
1014}
1015
1016int
1017register_intr(intr, device_id, flags, handler, maskptr, unit)
1018	int	intr;
1019	int	device_id;
1020	u_int	flags;
1021	inthand2_t *handler;
1022	u_int	*maskptr;
1023	int	unit;
1024{
1025	char	*cp;
1026	u_long	ef;
1027	int	id;
1028	u_int	mask = (maskptr ? *maskptr : 0);
1029
1030#if defined(APIC_IO)
1031	if ((u_int)intr >= ICU_LEN	/* no 8259 SLAVE to ignore */
1032#else
1033	if ((u_int)intr >= ICU_LEN || intr == 2
1034#endif /* APIC_IO */
1035	    || (u_int)device_id >= NR_DEVICES)
1036		return (EINVAL);
1037	if (intr_handler[intr] != isa_strayintr)
1038		return (EBUSY);
1039	ef = read_eflags();
1040	disable_intr();
1041	intr_countp[intr] = &intrcnt[device_id];
1042	intr_handler[intr] = handler;
1043	intr_mptr[intr] = maskptr;
1044	intr_mask[intr] = mask | (1 << intr);
1045	intr_unit[intr] = unit;
1046	setidt(ICU_OFFSET + intr,
1047	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
1048	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1049	write_eflags(ef);
1050	for (cp = intrnames, id = 0; id <= device_id; id++)
1051		while (*cp++ != '\0')
1052			;
1053	if (cp > eintrnames)
1054		return (0);
1055	if (intr < 10) {
1056		cp[-3] = intr + '0';
1057		cp[-2] = ' ';
1058	} else if (intr < 20) {
1059		cp[-3] = '1';
1060		cp[-2] = intr - 10 + '0';
1061	} else {
1062		cp[-3] = '2';
1063		cp[-2] = intr - 20 + '0';
1064	}
1065	return (0);
1066}
1067
1068static void
1069register_imask(dvp, mask)
1070	struct isa_device *dvp;
1071	u_int	mask;
1072{
1073	if (dvp->id_alive && dvp->id_irq) {
1074		int	intr;
1075
1076		intr = ffs(dvp->id_irq) - 1;
1077		intr_mask[intr] = mask | (1 <<intr);
1078	}
1079	(void) update_intr_masks();
1080}
1081
1082int
1083unregister_intr(intr, handler)
1084	int	intr;
1085	inthand2_t *handler;
1086{
1087	u_long	ef;
1088
1089	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1090		return (EINVAL);
1091	ef = read_eflags();
1092	disable_intr();
1093	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1094	intr_handler[intr] = isa_strayintr;
1095	intr_mptr[intr] = NULL;
1096	intr_mask[intr] = HWI_MASK | SWI_MASK;
1097	intr_unit[intr] = intr;
1098	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
1099	    GSEL(GCODE_SEL, SEL_KPL));
1100	write_eflags(ef);
1101	return (0);
1102}
1103