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