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