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