isa.c revision 15330
153451Speter/*-
253451Speter * Copyright (c) 1991 The Regents of the University of California.
353451Speter * All rights reserved.
453451Speter *
553451Speter * This code is derived from software contributed to Berkeley by
653451Speter * William Jolitz.
753451Speter *
853451Speter * Redistribution and use in source and binary forms, with or without
953451Speter * modification, are permitted provided that the following conditions
1053451Speter * are met:
1153451Speter * 1. Redistributions of source code must retain the above copyright
1253451Speter *    notice, this list of conditions and the following disclaimer.
1353451Speter * 2. Redistributions in binary form must reproduce the above copyright
1453451Speter *    notice, this list of conditions and the following disclaimer in the
1553451Speter *    documentation and/or other materials provided with the distribution.
1653451Speter * 3. All advertising materials mentioning features or use of this software
1753451Speter *    must display the following acknowledgement:
1853451Speter *	This product includes software developed by the University of
1953451Speter *	California, Berkeley and its contributors.
2053451Speter * 4. Neither the name of the University nor the names of its contributors
2153451Speter *    may be used to endorse or promote products derived from this software
2253451Speter *    without specific prior written permission.
2353451Speter *
2453451Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2553451Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2653451Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2753451Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2853451Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2953451Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3053451Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3153451Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3253451Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3353451Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3453451Speter * SUCH DAMAGE.
3553451Speter *
3653451Speter *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
3753451Speter *	$Id: isa.c,v 1.68 1996/04/11 21:18:43 bde Exp $
3853451Speter */
3953451Speter
4053451Speter/*
4153451Speter * code to manage AT bus
4253451Speter *
4353451Speter * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
4453451Speter * Fixed uninitialized variable problem and added code to deal
4553451Speter * with DMA page boundaries in isa_dmarangecheck().  Fixed word
4653451Speter * mode DMA count compution and reorganized DMA setup code in
4753451Speter * isa_dmastart()
4853451Speter */
4953451Speter
5053451Speter#include "opt_auto_eoi.h"
5153451Speter
5253451Speter#include <sys/param.h>
5353451Speter#include <sys/systm.h>
5453451Speter#include <sys/sysctl.h>
5553451Speter#include <sys/buf.h>
5653451Speter#include <sys/syslog.h>
5753451Speter#include <sys/malloc.h>
5853451Speter#include <machine/segments.h>
5953451Speter#include <vm/vm.h>
6053451Speter#include <vm/vm_param.h>
6153451Speter#include <vm/pmap.h>
6253451Speter#include <i386/isa/isa_device.h>
6353451Speter#include <i386/isa/isa.h>
6453451Speter#include <i386/isa/icu.h>
6553451Speter#include <i386/isa/ic/i8237.h>
6653451Speter#include <sys/devconf.h>
6753451Speter#include "vector.h"
6853451Speter
6953451Speter/*
7053451Speter**  Register definitions for DMA controller 1 (channels 0..3):
7153451Speter*/
7253451Speter#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
7353451Speter#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
7453451Speter#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
7553451Speter#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
7653451Speter
7753451Speter/*
7853451Speter**  Register definitions for DMA controller 2 (channels 4..7):
7953451Speter*/
8053451Speter#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
8153451Speter#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
8253451Speter#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
8353451Speter#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
8453451Speter
8553451Speteru_long	*intr_countp[ICU_LEN];
8653451Speterinthand2_t *intr_handler[ICU_LEN];
8753451Speteru_int	intr_mask[ICU_LEN];
8853451Speteru_int*	intr_mptr[ICU_LEN];
8953451Speterint	intr_unit[ICU_LEN];
9053451Speter
9153451Speterextern struct kern_devconf kdc_cpu0;
9253451Speter
9353451Speterstruct kern_devconf kdc_isa0 = {
9453451Speter	0, 0, 0,		/* filled in by dev_attach */
9553451Speter	"isa", 0, { MDDT_BUS, 0 },
9653451Speter	0, 0, 0, BUS_EXTERNALLEN,
9753451Speter	&kdc_cpu0,		/* parent is the CPU */
9853451Speter	0,			/* no parentdata */
9953451Speter	DC_BUSY,		/* busses are always busy */
10053451Speter	"ISA bus",
10153451Speter	DC_CLS_BUS		/* class */
10253451Speter};
10353451Speter
10453451Speterstatic inthand_t *fastintr[ICU_LEN] = {
10553451Speter	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
10653451Speter	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
10753451Speter	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
10853451Speter	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
10953451Speter	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
11053451Speter	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
11153451Speter	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
11253451Speter	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
11353451Speter};
11453451Speter
11553451Speterstatic inthand_t *slowintr[ICU_LEN] = {
11653451Speter	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
11753451Speter	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
11853451Speter	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
11953451Speter	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
12053451Speter};
12153451Speter
12253451Speterstatic void config_isadev __P((struct isa_device *isdp, u_int *mp));
12353451Speterstatic void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
12453451Speter				 int reconfig));
12553451Speterstatic void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
12653451Speter			  int item, char const *whatnot, char const *reason,
12753451Speter			  char const *format));
12853451Speterstatic int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
12953451Speter			 u_int checkbits));
13053451Speterstatic int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
13153451Speterstatic inthand2_t isa_strayintr;
13253451Speterstatic void register_imask __P((struct isa_device *dvp, u_int mask));
13353451Speter
13453451Speter/*
13553451Speter * print a conflict message
13653451Speter */
13753451Speterstatic void
13853451Speterconflict(dvp, tmpdvp, item, whatnot, reason, format)
13953451Speter	struct isa_device	*dvp;
14053451Speter	struct isa_device	*tmpdvp;
14153451Speter	int			item;
14253451Speter	char const		*whatnot;
14353451Speter	char const		*reason;
14453451Speter	char const		*format;
14553451Speter{
14653451Speter	printf("%s%d not %sed due to %s conflict with %s%d at ",
14753451Speter		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
14853451Speter		tmpdvp->id_driver->name, tmpdvp->id_unit);
14953451Speter	printf(format, item);
15053451Speter	printf("\n");
15153451Speter}
15253451Speter
15353451Speter/*
15453451Speter * Check to see if things are already in use, like IRQ's, I/O addresses
15553451Speter * and Memory addresses.
15653451Speter */
15753451Speterstatic int
15853451Speterhaveseen(dvp, tmpdvp, checkbits)
15953451Speter	struct isa_device *dvp;
16053451Speter	struct isa_device *tmpdvp;
16153451Speter	u_int	checkbits;
16253451Speter{
16353451Speter	/*
16453451Speter	 * Only check against devices that have already been found and are not
16553451Speter	 * unilaterally allowed to conflict anyway.
16653451Speter	 */
16753451Speter	if (tmpdvp->id_alive && !dvp->id_conflicts) {
16853451Speter		char const *whatnot;
16953451Speter
17053451Speter		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
17153451Speter		/*
17253451Speter		 * Check for I/O address conflict.  We can only check the
17353451Speter		 * starting address of the device against the range of the
17453451Speter		 * device that has already been probed since we do not
17553451Speter		 * know how many I/O addresses this device uses.
17653451Speter		 */
17753451Speter		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
17853451Speter			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
17953451Speter			    (dvp->id_iobase <=
18053451Speter				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
18153451Speter				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
18253451Speter					 "I/O address", "0x%x");
18353451Speter				return 1;
18453451Speter			}
18553451Speter		}
18653451Speter		/*
18753451Speter		 * Check for Memory address conflict.  We can check for
18853451Speter		 * range overlap, but it will not catch all cases since the
18953451Speter		 * driver may adjust the msize paramater during probe, for
19053451Speter		 * now we just check that the starting address does not
19153451Speter		 * fall within any allocated region.
19253451Speter		 * XXX could add a second check after the probe for overlap,
19353451Speter		 * since at that time we would know the full range.
19453451Speter		 * XXX KERNBASE is a hack, we should have vaddr in the table!
19553451Speter		 */
19653451Speter		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
19753451Speter			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
19853451Speter			    (KERNBASE + dvp->id_maddr <=
19953451Speter			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
20053451Speter				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
20153451Speter					 whatnot, "maddr", "0x%x");
20253451Speter				return 1;
20353451Speter			}
20453451Speter		}
20553451Speter		/*
20653451Speter		 * Check for IRQ conflicts.
20753451Speter		 */
20853451Speter		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
20953451Speter			if (tmpdvp->id_irq == dvp->id_irq) {
21053451Speter				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
21153451Speter					 whatnot, "irq", "%d");
21253451Speter				return 1;
21353451Speter			}
21453451Speter		}
21553451Speter		/*
21653451Speter		 * Check for DRQ conflicts.
21753451Speter		 */
21853451Speter		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
21953451Speter			if (tmpdvp->id_drq == dvp->id_drq) {
22053451Speter				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
22153451Speter					 "drq", "%d");
22253451Speter				return 1;
22353451Speter			}
22453451Speter		}
22553451Speter	}
22653451Speter	return 0;
22753451Speter}
22853451Speter
22953451Speter/*
23053451Speter * Search through all the isa_devtab_* tables looking for anything that
23153451Speter * conflicts with the current device.
23253451Speter */
23353451Speterint
23453451Speterhaveseen_isadev(dvp, checkbits)
23553451Speter	struct isa_device *dvp;
23653451Speter	u_int	checkbits;
23753451Speter{
23853451Speter	struct isa_device *tmpdvp;
23953451Speter	int	status = 0;
24053451Speter
24153451Speter	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
24253451Speter		status |= haveseen(dvp, tmpdvp, checkbits);
24353451Speter		if (status)
24453451Speter			return status;
24553451Speter	}
24653451Speter	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
24753451Speter		status |= haveseen(dvp, tmpdvp, checkbits);
24853451Speter		if (status)
24953451Speter			return status;
25053451Speter	}
25153451Speter	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
25253451Speter		status |= haveseen(dvp, tmpdvp, checkbits);
25353451Speter		if (status)
25453451Speter			return status;
25553451Speter	}
25653451Speter	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
25753451Speter		status |= haveseen(dvp, tmpdvp, checkbits);
25853451Speter		if (status)
25953451Speter			return status;
26053451Speter	}
26153451Speter	return(status);
26253451Speter}
26353451Speter
26453451Speter/*
26553451Speter * Configure all ISA devices
26653451Speter */
26753451Spetervoid
26853451Speterisa_configure() {
26953451Speter	struct isa_device *dvp;
27053451Speter
27153451Speter	dev_attach(&kdc_isa0);
27253451Speter
27353451Speter	splhigh();
27453451Speter	printf("Probing for devices on the ISA bus:\n");
27553451Speter	/* First probe all the sensitive probes */
27653451Speter	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
27753451Speter		if (dvp->id_driver->sensitive_hw)
27853451Speter			config_isadev(dvp, &tty_imask);
27953451Speter	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
28053451Speter		if (dvp->id_driver->sensitive_hw)
28153451Speter			config_isadev(dvp, &bio_imask);
28253451Speter	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
28353451Speter		if (dvp->id_driver->sensitive_hw)
28453451Speter			config_isadev(dvp, &net_imask);
28553451Speter	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
28653451Speter		if (dvp->id_driver->sensitive_hw)
28753451Speter			config_isadev(dvp, (u_int *)NULL);
28853451Speter
28953451Speter	/* Then all the bad ones */
29053451Speter	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
29153451Speter		if (!dvp->id_driver->sensitive_hw)
29253451Speter			config_isadev(dvp, &tty_imask);
29353451Speter	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
29453451Speter		if (!dvp->id_driver->sensitive_hw)
29553451Speter			config_isadev(dvp, &bio_imask);
29653451Speter	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
29753451Speter		if (!dvp->id_driver->sensitive_hw)
29853451Speter			config_isadev(dvp, &net_imask);
29953451Speter	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
30053451Speter		if (!dvp->id_driver->sensitive_hw)
30153451Speter			config_isadev(dvp, (u_int *)NULL);
30253451Speter
30353451Speter	bio_imask |= SWI_CLOCK_MASK;
30453451Speter	net_imask |= SWI_NET_MASK;
30553451Speter	tty_imask |= SWI_TTY_MASK;
30653451Speter
30753451Speter/*
30853451Speter * XXX we should really add the tty device to net_imask when the line is
30953451Speter * switched to SLIPDISC, and then remove it when it is switched away from
31053451Speter * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
31153451Speter * of them is running slip.
31253451Speter *
31353451Speter * XXX actually, blocking all ttys during a splimp doesn't matter so much
31453451Speter * with sio because the serial interrupt layer doesn't use tty_imask.  Only
31553451Speter * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
31653451Speter * during spltty.
31753451Speter */
31853451Speter#include "sl.h"
31953451Speter#if NSL > 0
32053451Speter	net_imask |= tty_imask;
32153451Speter	tty_imask = net_imask;
32253451Speter#endif
32353451Speter
32453451Speter	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
32553451Speter
32653451Speter	if (bootverbose)
32753451Speter		printf("imasks: bio %x, tty %x, net %x\n",
32853451Speter		       bio_imask, tty_imask, net_imask);
32953451Speter
33053451Speter	/*
33153451Speter	 * Finish initializing intr_mask[].  Note that the partly
33253451Speter	 * constructed masks aren't actually used since we're at splhigh.
33353451Speter	 * For fully dynamic initialization, register_intr() and
33453451Speter	 * unregister_intr() will have to adjust the masks for _all_
33553451Speter	 * interrupts and for tty_imask, etc.
33653451Speter	 */
33753451Speter	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
33853451Speter		register_imask(dvp, tty_imask);
33953451Speter	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
34053451Speter		register_imask(dvp, bio_imask);
34153451Speter	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
34253451Speter		register_imask(dvp, net_imask);
34353451Speter	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
34453451Speter		register_imask(dvp, SWI_CLOCK_MASK);
34553451Speter	spl0();
34653451Speter}
34753451Speter
34853451Speter/*
34953451Speter * Configure an ISA device.
35053451Speter */
35153451Speter
35253451Speter
35353451Speterstatic void
35453451Speterconfig_isadev(isdp, mp)
35553451Speter     struct isa_device *isdp;
35653451Speter     u_int *mp;
35753451Speter{
35853451Speter	config_isadev_c(isdp, mp, 0);
35953451Speter}
36053451Speter
36153451Spetervoid
36253451Speterreconfig_isadev(isdp, mp)
36353451Speter	struct isa_device *isdp;
36453451Speter	u_int *mp;
36553451Speter{
36653451Speter	config_isadev_c(isdp, mp, 1);
36753451Speter}
36853451Speter
36953451Speterstatic void
37053451Speterconfig_isadev_c(isdp, mp, reconfig)
37153451Speter	struct isa_device *isdp;
37253451Speter	u_int *mp;
37353451Speter	int reconfig;
37453451Speter{
37553451Speter	u_int checkbits;
37653451Speter	int id_alive;
37753451Speter	int last_alive;
37853451Speter	struct isa_driver *dp = isdp->id_driver;
37953451Speter
38053451Speter	if (!isdp->id_enabled) {
38153451Speter		printf("%s%d: disabled, not probed.\n",
38253451Speter			dp->name, isdp->id_unit);
38353451Speter		return;
38453451Speter	}
38553451Speter	checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR;
38653451Speter	if (!reconfig && haveseen_isadev(isdp, checkbits))
38753451Speter		return;
38853451Speter	if (!reconfig && isdp->id_maddr) {
38953451Speter		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
39053451Speter		isdp->id_maddr += atdevbase;
39153451Speter	}
39253451Speter	if (reconfig) {
39353451Speter		last_alive = isdp->id_alive;
39453451Speter		isdp->id_reconfig = 1;
39553451Speter	}
39653451Speter	else {
39753451Speter		last_alive = 0;
39853451Speter		isdp->id_reconfig = 0;
39953451Speter	}
40053451Speter	id_alive = (*dp->probe)(isdp);
40153451Speter	if (id_alive) {
40253451Speter		/*
40353451Speter		 * Only print the I/O address range if id_alive != -1
40453451Speter		 * Right now this is a temporary fix just for the new
40553451Speter		 * NPX code so that if it finds a 486 that can use trap
40653451Speter		 * 16 it will not report I/O addresses.
40753451Speter		 * Rod Grimes 04/26/94
40853451Speter		 */
40953451Speter		if (!isdp->id_reconfig) {
41053451Speter			printf("%s%d", dp->name, isdp->id_unit);
41153451Speter			if (id_alive != -1) {
41253451Speter				printf(" at 0x%x", isdp->id_iobase);
41353451Speter				if (isdp->id_iobase + id_alive - 1 !=
41453451Speter				    isdp->id_iobase) {
41553451Speter					printf("-0x%x",
41653451Speter					       isdp->id_iobase + id_alive - 1);
41753451Speter				}
41853451Speter			}
41953451Speter			if (isdp->id_irq)
42053451Speter				printf(" irq %d", ffs(isdp->id_irq) - 1);
42153451Speter			if (isdp->id_drq != -1)
42253451Speter				printf(" drq %d", isdp->id_drq);
42353451Speter			if (isdp->id_maddr)
42453451Speter				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
42553451Speter			if (isdp->id_msize)
42653451Speter				printf(" msize %d", isdp->id_msize);
42753451Speter			if (isdp->id_flags)
42853451Speter				printf(" flags 0x%x", isdp->id_flags);
42953451Speter			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
43053451Speter				printf(" on motherboard");
43153451Speter			} else if (isdp->id_iobase >= 0x1000 &&
43253451Speter				    !(isdp->id_iobase & 0x300)) {
43353451Speter				printf (" on eisa slot %d",
43453451Speter					isdp->id_iobase >> 12);
43553451Speter			} else {
43653451Speter				printf (" on isa");
43753451Speter			}
43853451Speter			printf("\n");
43953451Speter			/*
44053451Speter			 * Check for conflicts again.  The driver may have
44153451Speter			 * changed *dvp.  We should weaken the early check
44253451Speter			 * since the driver may have been able to change
44353451Speter			 * *dvp to avoid conflicts if given a chance.  We
44453451Speter			 * already skip the early check for IRQs and force
44553451Speter			 * a check for IRQs in the next group of checks.
44653451Speter			 */
44753451Speter			checkbits |= CC_IRQ;
44853451Speter			if (haveseen_isadev(isdp, checkbits))
44953451Speter				return;
45053451Speter			isdp->id_alive = id_alive;
45153451Speter		}
45253451Speter		(*dp->attach)(isdp);
45353451Speter		if (isdp->id_irq) {
45453451Speter			if (mp)
455				INTRMASK(*mp, isdp->id_irq);
456			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
457				      isdp->id_ri_flags, isdp->id_intr,
458				      mp, isdp->id_unit);
459			INTREN(isdp->id_irq);
460		}
461	} else {
462		if (isdp->id_reconfig) {
463			(*dp->attach)(isdp); /* reconfiguration attach */
464		}
465		if (!last_alive) {
466			if (!isdp->id_reconfig) {
467				printf("%s%d not found",
468				       dp->name, isdp->id_unit);
469				if (isdp->id_iobase) {
470					printf(" at 0x%x", isdp->id_iobase);
471				}
472				printf("\n");
473			}
474		}
475		else {
476			/* This code has not been tested.... */
477			if (isdp->id_irq) {
478				INTRDIS(isdp->id_irq);
479				unregister_intr(ffs(isdp->id_irq) - 1,
480						isdp->id_intr);
481				if (mp)
482					INTRUNMASK(*mp, isdp->id_irq);
483			}
484		}
485	}
486}
487
488/*
489 * Provide ISA-specific device information to user programs using the
490 * hw.devconf interface.
491 */
492int
493isa_externalize(struct isa_device *id, struct sysctl_req *req)
494{
495	return (SYSCTL_OUT(req, id, sizeof *id));
496}
497
498/*
499 * This is used to forcibly reconfigure an ISA device.  It currently just
500 * returns an error 'cos you can't do that yet.  It is here to demonstrate
501 * what the `internalize' routine is supposed to do.
502 */
503int
504isa_internalize(struct isa_device *id, struct sysctl_req *req)
505{
506	struct isa_device myid;
507	int rv;
508
509	rv = SYSCTL_IN(req, &myid, sizeof *id);
510	if(rv)
511		return rv;
512
513	rv = EOPNOTSUPP;
514	/* code would go here to validate the configuration request */
515	/* code would go here to actually perform the reconfiguration */
516	return rv;
517}
518
519int
520isa_generic_externalize(struct kern_devconf *kdc, struct sysctl_req *req)
521{
522	return isa_externalize(kdc->kdc_isa, req);
523}
524
525/*
526 * Fill in default interrupt table (in case of spuruious interrupt
527 * during configuration of kernel, setup interrupt control unit
528 */
529void
530isa_defaultirq()
531{
532	int i;
533
534	/* icu vectors */
535	for (i = 0; i < ICU_LEN; i++)
536		unregister_intr(i, (inthand2_t *)NULL);
537
538	/* initialize 8259's */
539	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
540	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
541	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
542#ifdef AUTO_EOI_1
543	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
544#else
545	outb(IO_ICU1+1, 1);		/* 8086 mode */
546#endif
547	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
548	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
549	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
550
551	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
552	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
553	outb(IO_ICU2+1,2);		/* my slave id is 2 */
554#ifdef AUTO_EOI_2
555	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
556#else
557	outb(IO_ICU2+1,1);		/* 8086 mode */
558#endif
559	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
560	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
561}
562
563static caddr_t	dma_bouncebuf[8];
564static u_int	dma_bouncebufsize[8];
565static u_int8_t	dma_bounced = 0;
566static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
567static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
568
569#define VALID_DMA_MASK (7)
570
571/* high byte of address is stored in this port for i-th dma channel */
572static short dmapageport[8] =
573	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
574
575/*
576 * Setup a DMA channel's bounce buffer.
577 */
578void
579isa_dmainit(chan, bouncebufsize)
580	int chan;
581	u_int bouncebufsize;
582{
583	void *buf;
584
585#ifdef DIAGNOSTIC
586	if (chan & ~VALID_DMA_MASK)
587		panic("isa_dmainit: channel out of range");
588
589	if (dma_bouncebuf[chan] != NULL)
590		panic("isa_dmainit: impossible request");
591#endif
592
593	dma_bouncebufsize[chan] = bouncebufsize;
594
595	/* Try malloc() first.  It works better if it works. */
596	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
597	if (buf != NULL) {
598		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
599			dma_bouncebuf[chan] = buf;
600			return;
601		}
602		free(buf, M_DEVBUF);
603	}
604	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
605			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
606	if (buf == NULL)
607		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
608	else
609		dma_bouncebuf[chan] = buf;
610}
611
612/*
613 * Register a DMA channel's usage.  Usually called from a device driver
614 * in open() or during it's initialization.
615 */
616int
617isa_dma_acquire(chan)
618	int chan;
619{
620#ifdef DIAGNOSTIC
621	if (chan & ~VALID_DMA_MASK)
622		panic("isa_dma_acquire: channel out of range");
623#endif
624
625	if (dma_inuse & (1 << chan)) {
626		printf("isa_dma_acquire: channel %d already in use\n", chan);
627		return (EBUSY);
628	}
629	dma_inuse |= (1 << chan);
630
631	return (0);
632}
633
634/*
635 * Unregister a DMA channel's usage.  Usually called from a device driver
636 * during close() or during it's shutdown.
637 */
638void
639isa_dma_release(chan)
640	int chan;
641{
642#ifdef DIAGNOSTIC
643	if (chan & ~VALID_DMA_MASK)
644		panic("isa_dma_release: channel out of range");
645
646	if (dma_inuse & (1 << chan) == 0)
647		printf("isa_dma_release: channel %d not in use\n", chan);
648#endif
649
650	if (dma_busy & (1 << chan)) {
651		dma_busy &= ~(1 << chan);
652		/*
653		 * XXX We should also do "dma_bounced &= (1 << chan);"
654		 * because we are acting on behalf of isa_dmadone() which
655		 * was not called to end the last DMA operation.  This does
656		 * not matter now, but it may in the future.
657		 */
658	}
659
660	dma_inuse &= ~(1 << chan);
661}
662
663/*
664 * isa_dmacascade(): program 8237 DMA controller channel to accept
665 * external dma control by a board.
666 */
667void isa_dmacascade(chan)
668	int chan;
669{
670#ifdef DIAGNOSTIC
671	if (chan & ~VALID_DMA_MASK)
672		panic("isa_dmacascade: channel out of range");
673#endif
674
675	/* set dma channel mode, and set dma channel mode */
676	if ((chan & 4) == 0) {
677		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
678		outb(DMA1_SMSK, chan);
679	} else {
680		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
681		outb(DMA2_SMSK, chan & 3);
682	}
683}
684
685/*
686 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
687 * problems by using a bounce buffer.
688 */
689void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
690{
691	vm_offset_t phys;
692	int waport;
693	caddr_t newaddr;
694
695#ifdef DIAGNOSTIC
696	if (chan & ~VALID_DMA_MASK)
697		panic("isa_dmastart: channel out of range");
698
699	if ((chan < 4 && nbytes > (1<<16))
700	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
701		panic("isa_dmastart: impossible request");
702
703	if (dma_inuse & (1 << chan) == 0)
704		printf("isa_dmastart: channel %d not acquired\n", chan);
705#endif
706
707	if (dma_busy & (1 << chan))
708		printf("isa_dmastart: channel %d busy\n", chan);
709
710	dma_busy |= (1 << chan);
711
712	if (isa_dmarangecheck(addr, nbytes, chan)) {
713		if (dma_bouncebuf[chan] == NULL
714		    || dma_bouncebufsize[chan] < nbytes)
715			panic("isa_dmastart: bad bounce buffer");
716		dma_bounced |= (1 << chan);
717		newaddr = dma_bouncebuf[chan];
718
719		/* copy bounce buffer on write */
720		if (!(flags & B_READ))
721			bcopy(addr, newaddr, nbytes);
722		addr = newaddr;
723	}
724
725	/* translate to physical */
726	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
727
728	if ((chan & 4) == 0) {
729		/*
730		 * Program one of DMA channels 0..3.  These are
731		 * byte mode channels.
732		 */
733		/* set dma channel mode, and reset address ff */
734
735		/* If B_RAW flag is set, then use autoinitialise mode */
736		if (flags & B_RAW) {
737		  if (flags & B_READ)
738			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
739		  else
740			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
741		}
742		else
743		if (flags & B_READ)
744			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
745		else
746			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
747		outb(DMA1_FFC, 0);
748
749		/* send start address */
750		waport =  DMA1_CHN(chan);
751		outb(waport, phys);
752		outb(waport, phys>>8);
753		outb(dmapageport[chan], phys>>16);
754
755		/* send count */
756		outb(waport + 1, --nbytes);
757		outb(waport + 1, nbytes>>8);
758
759		/* unmask channel */
760		outb(DMA1_SMSK, chan);
761	} else {
762		/*
763		 * Program one of DMA channels 4..7.  These are
764		 * word mode channels.
765		 */
766		/* set dma channel mode, and reset address ff */
767
768		/* If B_RAW flag is set, then use autoinitialise mode */
769		if (flags & B_RAW) {
770		  if (flags & B_READ)
771			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
772		  else
773			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
774		}
775		else
776		if (flags & B_READ)
777			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
778		else
779			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
780		outb(DMA2_FFC, 0);
781
782		/* send start address */
783		waport = DMA2_CHN(chan - 4);
784		outb(waport, phys>>1);
785		outb(waport, phys>>9);
786		outb(dmapageport[chan], phys>>16);
787
788		/* send count */
789		nbytes >>= 1;
790		outb(waport + 2, --nbytes);
791		outb(waport + 2, nbytes>>8);
792
793		/* unmask channel */
794		outb(DMA2_SMSK, chan & 3);
795	}
796}
797
798void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
799{
800#ifdef DIAGNOSTIC
801	if (chan & ~VALID_DMA_MASK)
802		panic("isa_dmadone: channel out of range");
803
804	if (dma_inuse & (1 << chan) == 0)
805		printf("isa_dmadone: channel %d not acquired\n", chan);
806#endif
807
808#if 0
809	/*
810	 * XXX This should be checked, but drivers like ad1848 only call
811	 * isa_dmastart() once because they use Auto DMA mode.  If we
812	 * leave this in, drivers that do this will print this continuously.
813	 */
814	if (dma_busy & (1 << chan) == 0)
815		printf("isa_dmadone: channel %d not busy\n", chan);
816#endif
817
818	if (dma_bounced & (1 << chan)) {
819		/* copy bounce buffer on read */
820		if (flags & B_READ)
821			bcopy(dma_bouncebuf[chan], addr, nbytes);
822
823		dma_bounced &= ~(1 << chan);
824	}
825	dma_busy &= ~(1 << chan);
826}
827
828/*
829 * Check for problems with the address range of a DMA transfer
830 * (non-contiguous physical pages, outside of bus address space,
831 * crossing DMA page boundaries).
832 * Return true if special handling needed.
833 */
834
835static int
836isa_dmarangecheck(caddr_t va, u_int length, int chan) {
837	vm_offset_t phys, priorpage = 0, endva;
838	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
839
840	endva = (vm_offset_t)round_page(va + length);
841	for (; va < (caddr_t) endva ; va += NBPG) {
842		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
843#define ISARAM_END	RAM_END
844		if (phys == 0)
845			panic("isa_dmacheck: no physical page present");
846		if (phys >= ISARAM_END)
847			return (1);
848		if (priorpage) {
849			if (priorpage + NBPG != phys)
850				return (1);
851			/* check if crossing a DMA page boundary */
852			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
853				return (1);
854		}
855		priorpage = phys;
856	}
857	return (0);
858}
859
860#define NMI_PARITY (1 << 7)
861#define NMI_IOCHAN (1 << 6)
862#define ENMI_WATCHDOG (1 << 7)
863#define ENMI_BUSTIMER (1 << 6)
864#define ENMI_IOSTATUS (1 << 5)
865
866/*
867 * Handle a NMI, possibly a machine check.
868 * return true to panic system, false to ignore.
869 */
870int
871isa_nmi(cd)
872	int cd;
873{
874	int isa_port = inb(0x61);
875	int eisa_port = inb(0x461);
876	if(isa_port & NMI_PARITY) {
877		panic("RAM parity error, likely hardware failure.");
878	} else if(isa_port & NMI_IOCHAN) {
879		panic("I/O channel check, likely hardware failure.");
880	} else if(eisa_port & ENMI_WATCHDOG) {
881		panic("EISA watchdog timer expired, likely hardware failure.");
882	} else if(eisa_port & ENMI_BUSTIMER) {
883		panic("EISA bus timeout, likely hardware failure.");
884	} else if(eisa_port & ENMI_IOSTATUS) {
885		panic("EISA I/O port status error.");
886	} else {
887		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
888		return(0);
889	}
890}
891
892/*
893 * Caught a stray interrupt, notify
894 */
895static void
896isa_strayintr(d)
897	int d;
898{
899
900	/* DON'T BOTHER FOR NOW! */
901	/* for some reason, we get bursts of intr #7, even if not enabled! */
902	/*
903	 * Well the reason you got bursts of intr #7 is because someone
904	 * raised an interrupt line and dropped it before the 8259 could
905	 * prioritize it.  This is documented in the intel data book.  This
906	 * means you have BAD hardware!  I have changed this so that only
907	 * the first 5 get logged, then it quits logging them, and puts
908	 * out a special message. rgrimes 3/25/1993
909	 */
910	/*
911	 * XXX TODO print a different message for #7 if it is for a
912	 * glitch.  Glitches can be distinguished from real #7's by
913	 * testing that the in-service bit is _not_ set.  The test
914	 * must be done before sending an EOI so it can't be done if
915	 * we are using AUTO_EOI_1.
916	 */
917	if (intrcnt[NR_DEVICES + d] <= 5)
918		log(LOG_ERR, "stray irq %d\n", d);
919	if (intrcnt[NR_DEVICES + d] == 5)
920		log(LOG_CRIT,
921		    "too many stray irq %d's; not logging any more\n", d);
922}
923
924/*
925 * Find the highest priority enabled display device.  Since we can't
926 * distinguish display devices from ttys, depend on display devices
927 * being sensitive and before sensitive non-display devices (if any)
928 * in isa_devtab_tty.
929 *
930 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
931 */
932struct isa_device *
933find_display()
934{
935	struct isa_device *dvp;
936
937	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
938		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
939			return (dvp);
940	return (NULL);
941}
942
943/*
944 * find an ISA device in a given isa_devtab_* table, given
945 * the table to search, the expected id_driver entry, and the unit number.
946 *
947 * this function is defined in isa_device.h, and this location is debatable;
948 * i put it there because it's useless w/o, and directly operates on
949 * the other stuff in that file.
950 *
951 */
952
953struct isa_device *find_isadev(table, driverp, unit)
954     struct isa_device *table;
955     struct isa_driver *driverp;
956     int unit;
957{
958  if (driverp == NULL) /* sanity check */
959    return NULL;
960
961  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
962    if (table->id_driver == 0)
963      return NULL;
964
965    table++;
966  }
967
968  return table;
969}
970
971/*
972 * Return nonzero if a (masked) irq is pending for a given device.
973 */
974int
975isa_irq_pending(dvp)
976	struct isa_device *dvp;
977{
978	unsigned id_irq;
979
980	id_irq = dvp->id_irq;
981	if (id_irq & 0xff)
982		return (inb(IO_ICU1) & id_irq);
983	return (inb(IO_ICU2) & (id_irq >> 8));
984}
985
986int
987update_intr_masks(void)
988{
989	int intr, n=0;
990	u_int mask,*maskptr;
991
992	for (intr=0; intr < ICU_LEN; intr ++) {
993		if (intr==2) continue;
994		maskptr = intr_mptr[intr];
995		if (!maskptr) continue;
996		*maskptr |= 1 << intr;
997		mask = *maskptr;
998		if (mask != intr_mask[intr]) {
999#if 0
1000			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
1001				intr, intr_mask[intr], mask, maskptr);
1002#endif
1003			intr_mask[intr]=mask;
1004			n++;
1005		}
1006
1007	}
1008	return (n);
1009}
1010
1011int
1012register_intr(intr, device_id, flags, handler, maskptr, unit)
1013	int	intr;
1014	int	device_id;
1015	u_int	flags;
1016	inthand2_t *handler;
1017	u_int	*maskptr;
1018	int	unit;
1019{
1020	char	*cp;
1021	u_long	ef;
1022	int	id;
1023	u_int	mask = (maskptr ? *maskptr : 0);
1024
1025	if ((u_int)intr >= ICU_LEN || intr == 2
1026	    || (u_int)device_id >= NR_DEVICES)
1027		return (EINVAL);
1028	if (intr_handler[intr] != isa_strayintr)
1029		return (EBUSY);
1030	ef = read_eflags();
1031	disable_intr();
1032	intr_countp[intr] = &intrcnt[device_id];
1033	intr_handler[intr] = handler;
1034	intr_mptr[intr] = maskptr;
1035	intr_mask[intr] = mask | (1 << intr);
1036	intr_unit[intr] = unit;
1037	setidt(ICU_OFFSET + intr,
1038	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
1039	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1040	write_eflags(ef);
1041	for (cp = intrnames, id = 0; id <= device_id; id++)
1042		while (*cp++ != '\0')
1043			;
1044	if (cp > eintrnames)
1045		return (0);
1046	if (intr < 10) {
1047		cp[-3] = intr + '0';
1048		cp[-2] = ' ';
1049	} else {
1050		cp[-3] = '1';
1051		cp[-2] = intr - 10 + '0';
1052	}
1053	return (0);
1054}
1055
1056static void
1057register_imask(dvp, mask)
1058	struct isa_device *dvp;
1059	u_int	mask;
1060{
1061	if (dvp->id_alive && dvp->id_irq) {
1062		int	intr;
1063
1064		intr = ffs(dvp->id_irq) - 1;
1065		intr_mask[intr] = mask | (1 <<intr);
1066	}
1067	(void) update_intr_masks();
1068}
1069
1070int
1071unregister_intr(intr, handler)
1072	int	intr;
1073	inthand2_t *handler;
1074{
1075	u_long	ef;
1076
1077	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1078		return (EINVAL);
1079	ef = read_eflags();
1080	disable_intr();
1081	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1082	intr_handler[intr] = isa_strayintr;
1083	intr_mptr[intr] = NULL;
1084	intr_mask[intr] = HWI_MASK | SWI_MASK;
1085	intr_unit[intr] = intr;
1086	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
1087	    GSEL(GCODE_SEL, SEL_KPL));
1088	write_eflags(ef);
1089	return (0);
1090}
1091