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