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