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