isa.c revision 7345
1191783Srmacklem/*-
2191783Srmacklem * Copyright (c) 1991 The Regents of the University of California.
3191783Srmacklem * All rights reserved.
4191783Srmacklem *
5191783Srmacklem * This code is derived from software contributed to Berkeley by
6191783Srmacklem * William Jolitz.
7191783Srmacklem *
8191783Srmacklem * Redistribution and use in source and binary forms, with or without
9191783Srmacklem * modification, are permitted provided that the following conditions
10191783Srmacklem * are met:
11191783Srmacklem * 1. Redistributions of source code must retain the above copyright
12191783Srmacklem *    notice, this list of conditions and the following disclaimer.
13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
14191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
15191783Srmacklem *    documentation and/or other materials provided with the distribution.
16191783Srmacklem * 3. All advertising materials mentioning features or use of this software
17191783Srmacklem *    must display the following acknowledgement:
18191783Srmacklem *	This product includes software developed by the University of
19191783Srmacklem *	California, Berkeley and its contributors.
20191783Srmacklem * 4. Neither the name of the University nor the names of its contributors
21191783Srmacklem *    may be used to endorse or promote products derived from this software
22191783Srmacklem *    without specific prior written permission.
23191783Srmacklem *
24191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27191783Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34191783Srmacklem * SUCH DAMAGE.
35191783Srmacklem *
36191783Srmacklem *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
37191783Srmacklem *	$Id: isa.c,v 1.40 1995/03/16 17:31:18 se Exp $
38191783Srmacklem */
39191783Srmacklem
40191783Srmacklem/*
41191783Srmacklem * code to manage AT bus
42191783Srmacklem *
43191783Srmacklem * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44191783Srmacklem * Fixed uninitialized variable problem and added code to deal
45191783Srmacklem * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46191783Srmacklem * mode DMA count compution and reorganized DMA setup code in
47191783Srmacklem * isa_dmastart()
48191783Srmacklem */
49191783Srmacklem
50191783Srmacklem#include <sys/param.h>
51191783Srmacklem#include <sys/systm.h>		/* isn't it a joy */
52191783Srmacklem#include <sys/kernel.h>		/* to have three of these */
53191783Srmacklem#include <sys/proc.h>
54191783Srmacklem#include <sys/conf.h>
55191783Srmacklem#include <sys/file.h>
56191783Srmacklem#include <sys/buf.h>
57191783Srmacklem#include <sys/uio.h>
58191783Srmacklem#include <sys/syslog.h>
59191783Srmacklem#include <sys/malloc.h>
60191783Srmacklem#include <sys/rlist.h>
61191783Srmacklem#include <machine/segments.h>
62191783Srmacklem#include <vm/vm.h>
63191783Srmacklem#include <machine/spl.h>
64191783Srmacklem#include <i386/isa/isa_device.h>
65191783Srmacklem#include <i386/isa/isa.h>
66191783Srmacklem#include <i386/isa/icu.h>
67192121Srmacklem#include <i386/isa/ic/i8237.h>
68191783Srmacklem#include <i386/isa/ic/i8042.h>
69191783Srmacklem#include <sys/devconf.h>
70191783Srmacklem#include "vector.h"
71191783Srmacklem
72191783Srmacklem/*
73191783Srmacklem**  Register definitions for DMA controller 1 (channels 0..3):
74191783Srmacklem*/
75191783Srmacklem#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
76191783Srmacklem#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
77191783Srmacklem#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
78191783Srmacklem#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
79191783Srmacklem
80191783Srmacklem/*
81191783Srmacklem**  Register definitions for DMA controller 2 (channels 4..7):
82191783Srmacklem*/
83191783Srmacklem#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
84191783Srmacklem#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
85191783Srmacklem#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
86191783Srmacklem#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
87191783Srmacklem
88191783Srmacklem/*
89191783Srmacklem * Bits to specify the type and amount of conflict checking.
90191783Srmacklem */
91191783Srmacklem#define	CC_ATTACH	(1 << 0)
92191783Srmacklem#define	CC_DRQ		(1 << 1)
93191783Srmacklem#define	CC_IOADDR	(1 << 2)
94191783Srmacklem#define	CC_IRQ		(1 << 3)
95192121Srmacklem#define	CC_MEMADDR	(1 << 4)
96191783Srmacklem
97191783Srmacklem/*
98191783Srmacklem * XXX these defines should be in a central place.
99191783Srmacklem */
100191783Srmacklem#define	read_eflags()		({u_long ef; \
101192121Srmacklem				  __asm("pushfl; popl %0" : "=a" (ef)); \
102192121Srmacklem				  ef; })
103191783Srmacklem#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
104191783Srmacklem
105191783Srmacklemu_long	*intr_countp[ICU_LEN];
106191783Srmackleminthand2_t *intr_handler[ICU_LEN];
107191783Srmacklemu_int	intr_mask[ICU_LEN];
108191783Srmacklemu_int*	intr_mptr[ICU_LEN];
109191783Srmacklemint	intr_unit[ICU_LEN];
110191783Srmacklem
111191783Srmacklemstruct kern_devconf kdc_isa0 = {
112191783Srmacklem	0, 0, 0,		/* filled in by dev_attach */
113191783Srmacklem	"isa", 0, { MDDT_BUS, 0 },
114191783Srmacklem	0, 0, 0, BUS_EXTERNALLEN,
115191783Srmacklem	0,			/* no parent yet; parent should be CPU */
116191783Srmacklem	0,			/* no parentdata */
117191783Srmacklem	DC_BUSY,		/* busses are always busy */
118191783Srmacklem	"ISA or EISA bus"
119191783Srmacklem};
120191783Srmacklem
121191783Srmacklemstatic inthand_t *fastintr[ICU_LEN] = {
122191783Srmacklem	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
123191783Srmacklem	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
124191783Srmacklem	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
125191783Srmacklem	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
126191783Srmacklem	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
127191783Srmacklem	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
128191783Srmacklem	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
129191783Srmacklem	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
130191783Srmacklem};
131191783Srmacklem
132191783Srmacklemstatic inthand_t *slowintr[ICU_LEN] = {
133191783Srmacklem	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
134191783Srmacklem	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
135191783Srmacklem	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
136191783Srmacklem	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
137191783Srmacklem};
138191783Srmacklem
139191783Srmacklemstatic void config_isadev __P((struct isa_device *isdp, u_int *mp));
140191783Srmacklemstatic void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
141191783Srmacklem			  int item, char const *whatnot, char const *reason,
142191783Srmacklem			  char const *format));
143191783Srmacklemstatic int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
144191783Srmacklem			 u_int checkbits));
145191783Srmacklemstatic int haveseen_isadev __P((struct isa_device *dvp, u_int checkbits));
146191783Srmacklemstatic inthand2_t isa_strayintr;
147191783Srmacklemstatic void register_imask __P((struct isa_device *dvp, u_int mask));
148191783Srmacklem
149191783Srmacklem/*
150191783Srmacklem * print a conflict message
151191783Srmacklem */
152191783Srmacklemstatic void
153191783Srmacklemconflict(dvp, tmpdvp, item, whatnot, reason, format)
154191783Srmacklem	struct isa_device	*dvp;
155191783Srmacklem	struct isa_device	*tmpdvp;
156191783Srmacklem	int			item;
157191783Srmacklem	char const		*whatnot;
158191783Srmacklem	char const		*reason;
159191783Srmacklem	char const		*format;
160191783Srmacklem{
161191783Srmacklem	printf("%s%d not %sed due to %s conflict with %s%d at ",
162191783Srmacklem		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
163191783Srmacklem		tmpdvp->id_driver->name, tmpdvp->id_unit);
164191783Srmacklem	printf(format, item);
165191783Srmacklem	printf("\n");
166191783Srmacklem}
167191783Srmacklem
168191783Srmacklem/*
169191783Srmacklem * Check to see if things are alread in use, like IRQ's, I/O addresses
170191783Srmacklem * and Memory addresses.
171191783Srmacklem */
172191783Srmacklemstatic int
173191783Srmacklemhaveseen(dvp, tmpdvp, checkbits)
174191783Srmacklem	struct isa_device *dvp;
175191783Srmacklem	struct isa_device *tmpdvp;
176191783Srmacklem	u_int	checkbits;
177191783Srmacklem{
178191783Srmacklem	/*
179191783Srmacklem	 * Only check against devices that have already been found
180191783Srmacklem	 */
181191783Srmacklem	if (tmpdvp->id_alive) {
182191783Srmacklem		char const *whatnot;
183191783Srmacklem
184191783Srmacklem		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
185191783Srmacklem		/*
186191783Srmacklem		 * Check for I/O address conflict.  We can only check the
187191783Srmacklem		 * starting address of the device against the range of the
188191783Srmacklem		 * device that has already been probed since we do not
189191783Srmacklem		 * know how many I/O addresses this device uses.
190191783Srmacklem		 */
191191783Srmacklem		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
192191783Srmacklem			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
193191783Srmacklem			    (dvp->id_iobase <=
194191783Srmacklem				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
195194408Srmacklem				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
196191783Srmacklem					 "I/O address", "0x%x");
197191783Srmacklem				return 1;
198191783Srmacklem			}
199191783Srmacklem		}
200191783Srmacklem		/*
201191783Srmacklem		 * Check for Memory address conflict.  We can check for
202191783Srmacklem		 * range overlap, but it will not catch all cases since the
203191783Srmacklem		 * driver may adjust the msize paramater during probe, for
204191783Srmacklem		 * now we just check that the starting address does not
205191783Srmacklem		 * fall within any allocated region.
206191783Srmacklem		 * XXX could add a second check after the probe for overlap,
207191783Srmacklem		 * since at that time we would know the full range.
208191783Srmacklem		 * XXX KERNBASE is a hack, we should have vaddr in the table!
209191783Srmacklem		 */
210191783Srmacklem		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
211191783Srmacklem			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
212191783Srmacklem			    (KERNBASE + dvp->id_maddr <=
213191783Srmacklem			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
214191783Srmacklem				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
215191783Srmacklem					 whatnot, "maddr", "0x%x");
216191783Srmacklem				return 1;
217191783Srmacklem			}
218191783Srmacklem		}
219191783Srmacklem		/*
220191783Srmacklem		 * Check for IRQ conflicts.
221191783Srmacklem		 */
222191783Srmacklem		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
223191783Srmacklem			if (tmpdvp->id_irq == dvp->id_irq) {
224191783Srmacklem				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
225191783Srmacklem					 whatnot, "irq", "%d");
226191783Srmacklem				return 1;
227191783Srmacklem			}
228191783Srmacklem		}
229191783Srmacklem		/*
230191783Srmacklem		 * Check for DRQ conflicts.
231191783Srmacklem		 */
232191783Srmacklem		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
233191783Srmacklem			if (tmpdvp->id_drq == dvp->id_drq) {
234191783Srmacklem				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
235191783Srmacklem					 "drq", "%d");
236191783Srmacklem				return 1;
237191783Srmacklem			}
238191783Srmacklem		}
239191783Srmacklem	}
240191783Srmacklem	return 0;
241191783Srmacklem}
242191783Srmacklem
243191783Srmacklem/*
244223309Srmacklem * Search through all the isa_devtab_* tables looking for anything that
245191783Srmacklem * conflicts with the current device.
246191783Srmacklem */
247191783Srmacklemstatic int
248191783Srmacklemhaveseen_isadev(dvp, checkbits)
249191783Srmacklem	struct isa_device *dvp;
250191783Srmacklem	u_int	checkbits;
251191783Srmacklem{
252223309Srmacklem	struct isa_device *tmpdvp;
253191783Srmacklem	int	status = 0;
254191783Srmacklem
255191783Srmacklem	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
256191783Srmacklem		status |= haveseen(dvp, tmpdvp, checkbits);
257191783Srmacklem		if (status)
258191783Srmacklem			return status;
259191783Srmacklem	}
260191783Srmacklem	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
261191783Srmacklem		status |= haveseen(dvp, tmpdvp, checkbits);
262191783Srmacklem		if (status)
263191783Srmacklem			return status;
264191783Srmacklem	}
265191783Srmacklem	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
266191783Srmacklem		status |= haveseen(dvp, tmpdvp, checkbits);
267191783Srmacklem		if (status)
268191783Srmacklem			return status;
269191783Srmacklem	}
270191783Srmacklem	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
271191783Srmacklem		status |= haveseen(dvp, tmpdvp, checkbits);
272191783Srmacklem		if (status)
273191783Srmacklem			return status;
274191783Srmacklem	}
275191783Srmacklem	return(status);
276191783Srmacklem}
277191783Srmacklem
278191783Srmacklem/*
279191783Srmacklem * Configure all ISA devices
280191783Srmacklem */
281191783Srmacklemvoid
282191783Srmacklemisa_configure() {
283192613Srmacklem	struct isa_device *dvp;
284191783Srmacklem
285191783Srmacklem	dev_attach(&kdc_isa0);
286191783Srmacklem
287191783Srmacklem	splhigh();
288191783Srmacklem	enable_intr();
289191783Srmacklem	INTREN(IRQ_SLAVE);
290191783Srmacklem	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");
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