isa.c revision 18233
155682Smarkm/*-
255682Smarkm * Copyright (c) 1991 The Regents of the University of California.
355682Smarkm * All rights reserved.
455682Smarkm *
555682Smarkm * This code is derived from software contributed to Berkeley by
655682Smarkm * William Jolitz.
755682Smarkm *
855682Smarkm * Redistribution and use in source and binary forms, with or without
955682Smarkm * modification, are permitted provided that the following conditions
1055682Smarkm * are met:
1155682Smarkm * 1. Redistributions of source code must retain the above copyright
1255682Smarkm *    notice, this list of conditions and the following disclaimer.
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm * 3. All advertising materials mentioning features or use of this software
1755682Smarkm *    must display the following acknowledgement:
1855682Smarkm *	This product includes software developed by the University of
1955682Smarkm *	California, Berkeley and its contributors.
2055682Smarkm * 4. Neither the name of the University nor the names of its contributors
2155682Smarkm *    may be used to endorse or promote products derived from this software
2255682Smarkm *    without specific prior written permission.
2355682Smarkm *
2455682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2555682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2655682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2755682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2855682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2955682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3055682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3155682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3255682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3355682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3455682Smarkm * SUCH DAMAGE.
3555682Smarkm *
3672445Sassar *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
3755682Smarkm *	$Id: isa.c,v 1.72 1996/09/06 23:07:45 phk Exp $
3855682Smarkm */
3955682Smarkm
4055682Smarkm/*
4155682Smarkm * code to manage AT bus
4255682Smarkm *
4355682Smarkm * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
4455682Smarkm * Fixed uninitialized variable problem and added code to deal
4555682Smarkm * with DMA page boundaries in isa_dmarangecheck().  Fixed word
4655682Smarkm * mode DMA count compution and reorganized DMA setup code in
4755682Smarkm * isa_dmastart()
4855682Smarkm */
4955682Smarkm
5055682Smarkm#include "opt_auto_eoi.h"
5155682Smarkm
5255682Smarkm#include <sys/param.h>
5355682Smarkm#include <sys/systm.h>
5455682Smarkm#include <sys/buf.h>
5555682Smarkm#include <sys/syslog.h>
5655682Smarkm#include <sys/malloc.h>
5772445Sassar#include <machine/md_var.h>
5872445Sassar#include <machine/segments.h>
5972445Sassar#include <vm/vm.h>
6072445Sassar#include <vm/vm_param.h>
6155682Smarkm#include <vm/pmap.h>
6255682Smarkm#include <i386/isa/isa_device.h>
6355682Smarkm#include <i386/isa/isa.h>
6455682Smarkm#include <i386/isa/icu.h>
6555682Smarkm#include <i386/isa/ic/i8237.h>
6655682Smarkm#include "vector.h"
6755682Smarkm
6855682Smarkm/*
6955682Smarkm**  Register definitions for DMA controller 1 (channels 0..3):
7055682Smarkm*/
7155682Smarkm#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
7255682Smarkm#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
7355682Smarkm#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
7455682Smarkm#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
7555682Smarkm
7655682Smarkm/*
7755682Smarkm**  Register definitions for DMA controller 2 (channels 4..7):
7855682Smarkm*/
7955682Smarkm#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
8055682Smarkm#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
8155682Smarkm#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
8255682Smarkm#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
8355682Smarkm
8455682Smarkmu_long	*intr_countp[ICU_LEN];
8555682Smarkminthand2_t *intr_handler[ICU_LEN];
8655682Smarkmu_int	intr_mask[ICU_LEN];
8755682Smarkmu_int*	intr_mptr[ICU_LEN];
8855682Smarkmint	intr_unit[ICU_LEN];
8955682Smarkm
9055682Smarkmstatic inthand_t *fastintr[ICU_LEN] = {
9155682Smarkm	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
9255682Smarkm	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
9355682Smarkm	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
9455682Smarkm	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
9555682Smarkm	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
9655682Smarkm	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
9755682Smarkm	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
9855682Smarkm	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
9955682Smarkm};
10055682Smarkm
10155682Smarkmstatic inthand_t *slowintr[ICU_LEN] = {
10255682Smarkm	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
10355682Smarkm	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
10455682Smarkm	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
10555682Smarkm	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
10655682Smarkm};
10755682Smarkm
10855682Smarkmstatic void config_isadev __P((struct isa_device *isdp, u_int *mp));
10955682Smarkmstatic void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
11055682Smarkm				 int reconfig));
11155682Smarkmstatic void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
11255682Smarkm			  int item, char const *whatnot, char const *reason,
11355682Smarkm			  char const *format));
11455682Smarkmstatic int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
11555682Smarkm			 u_int checkbits));
11655682Smarkmstatic int isa_dmarangecheck __P((caddr_t va, u_int length, int chan));
11755682Smarkmstatic inthand2_t isa_strayintr;
11855682Smarkmstatic void register_imask __P((struct isa_device *dvp, u_int mask));
11955682Smarkm
12055682Smarkm/*
12155682Smarkm * print a conflict message
12255682Smarkm */
12355682Smarkmstatic void
12455682Smarkmconflict(dvp, tmpdvp, item, whatnot, reason, format)
12555682Smarkm	struct isa_device	*dvp;
12655682Smarkm	struct isa_device	*tmpdvp;
12755682Smarkm	int			item;
12855682Smarkm	char const		*whatnot;
12955682Smarkm	char const		*reason;
13055682Smarkm	char const		*format;
13155682Smarkm{
13255682Smarkm	printf("%s%d not %sed due to %s conflict with %s%d at ",
13355682Smarkm		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
13455682Smarkm		tmpdvp->id_driver->name, tmpdvp->id_unit);
13555682Smarkm	printf(format, item);
13655682Smarkm	printf("\n");
13755682Smarkm}
13855682Smarkm
13955682Smarkm/*
14055682Smarkm * Check to see if things are already in use, like IRQ's, I/O addresses
14155682Smarkm * and Memory addresses.
14255682Smarkm */
14355682Smarkmstatic int
14455682Smarkmhaveseen(dvp, tmpdvp, checkbits)
14555682Smarkm	struct isa_device *dvp;
14655682Smarkm	struct isa_device *tmpdvp;
14755682Smarkm	u_int	checkbits;
14855682Smarkm{
14955682Smarkm	/*
15055682Smarkm	 * Only check against devices that have already been found and are not
15155682Smarkm	 * unilaterally allowed to conflict anyway.
15255682Smarkm	 */
15355682Smarkm	if (tmpdvp->id_alive && !dvp->id_conflicts) {
15455682Smarkm		char const *whatnot;
15555682Smarkm
15655682Smarkm		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
15755682Smarkm		/*
15855682Smarkm		 * Check for I/O address conflict.  We can only check the
15955682Smarkm		 * starting address of the device against the range of the
16055682Smarkm		 * device that has already been probed since we do not
16155682Smarkm		 * know how many I/O addresses this device uses.
16255682Smarkm		 */
16355682Smarkm		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
16455682Smarkm			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
16555682Smarkm			    (dvp->id_iobase <=
16655682Smarkm				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
16755682Smarkm				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
16855682Smarkm					 "I/O address", "0x%x");
16955682Smarkm				return 1;
17055682Smarkm			}
17155682Smarkm		}
17255682Smarkm		/*
17355682Smarkm		 * Check for Memory address conflict.  We can check for
17455682Smarkm		 * range overlap, but it will not catch all cases since the
17555682Smarkm		 * driver may adjust the msize paramater during probe, for
17655682Smarkm		 * now we just check that the starting address does not
17755682Smarkm		 * fall within any allocated region.
17855682Smarkm		 * XXX could add a second check after the probe for overlap,
17955682Smarkm		 * since at that time we would know the full range.
18055682Smarkm		 * XXX KERNBASE is a hack, we should have vaddr in the table!
18155682Smarkm		 */
18255682Smarkm		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
18355682Smarkm			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
18455682Smarkm			    (KERNBASE + dvp->id_maddr <=
18555682Smarkm			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
18655682Smarkm				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
18755682Smarkm					 whatnot, "maddr", "0x%x");
18855682Smarkm				return 1;
18955682Smarkm			}
19055682Smarkm		}
19155682Smarkm		/*
19255682Smarkm		 * Check for IRQ conflicts.
19355682Smarkm		 */
19455682Smarkm		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
19555682Smarkm			if (tmpdvp->id_irq == dvp->id_irq) {
19655682Smarkm				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
19755682Smarkm					 whatnot, "irq", "%d");
19855682Smarkm				return 1;
19955682Smarkm			}
20055682Smarkm		}
20155682Smarkm		/*
20255682Smarkm		 * Check for DRQ conflicts.
20355682Smarkm		 */
20455682Smarkm		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
20555682Smarkm			if (tmpdvp->id_drq == dvp->id_drq) {
20655682Smarkm				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
20755682Smarkm					 "drq", "%d");
20855682Smarkm				return 1;
20955682Smarkm			}
21055682Smarkm		}
21155682Smarkm	}
21255682Smarkm	return 0;
21355682Smarkm}
21455682Smarkm
21555682Smarkm/*
21655682Smarkm * Search through all the isa_devtab_* tables looking for anything that
21755682Smarkm * conflicts with the current device.
21855682Smarkm */
21955682Smarkmint
22055682Smarkmhaveseen_isadev(dvp, checkbits)
22155682Smarkm	struct isa_device *dvp;
22255682Smarkm	u_int	checkbits;
22355682Smarkm{
22455682Smarkm	struct isa_device *tmpdvp;
22555682Smarkm	int	status = 0;
22655682Smarkm
22755682Smarkm	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
22855682Smarkm		status |= haveseen(dvp, tmpdvp, checkbits);
22955682Smarkm		if (status)
23055682Smarkm			return status;
23155682Smarkm	}
23255682Smarkm	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
23355682Smarkm		status |= haveseen(dvp, tmpdvp, checkbits);
23455682Smarkm		if (status)
23555682Smarkm			return status;
23655682Smarkm	}
23755682Smarkm	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
23855682Smarkm		status |= haveseen(dvp, tmpdvp, checkbits);
23955682Smarkm		if (status)
24055682Smarkm			return status;
24155682Smarkm	}
24255682Smarkm	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
24355682Smarkm		status |= haveseen(dvp, tmpdvp, checkbits);
24472445Sassar		if (status)
24572445Sassar			return status;
24655682Smarkm	}
24755682Smarkm	return(status);
24855682Smarkm}
24955682Smarkm
25055682Smarkm/*
25155682Smarkm * Configure all ISA devices
25255682Smarkm */
25355682Smarkmvoid
25472445Sassarisa_configure() {
25572445Sassar	struct isa_device *dvp;
25672445Sassar
25772445Sassar	splhigh();
25872445Sassar	printf("Probing for devices on the ISA bus:\n");
25955682Smarkm	/* First probe all the sensitive probes */
26055682Smarkm	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
26155682Smarkm		if (dvp->id_driver->sensitive_hw)
26255682Smarkm			config_isadev(dvp, &tty_imask);
26355682Smarkm	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
26455682Smarkm		if (dvp->id_driver->sensitive_hw)
26555682Smarkm			config_isadev(dvp, &bio_imask);
26655682Smarkm	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
26772445Sassar		if (dvp->id_driver->sensitive_hw)
26872445Sassar			config_isadev(dvp, &net_imask);
26955682Smarkm	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
27072445Sassar		if (dvp->id_driver->sensitive_hw)
27155682Smarkm			config_isadev(dvp, (u_int *)NULL);
27255682Smarkm
27355682Smarkm	/* Then all the bad ones */
27455682Smarkm	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
27555682Smarkm		if (!dvp->id_driver->sensitive_hw)
27655682Smarkm			config_isadev(dvp, &tty_imask);
27755682Smarkm	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
27855682Smarkm		if (!dvp->id_driver->sensitive_hw)
27955682Smarkm			config_isadev(dvp, &bio_imask);
28055682Smarkm	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
28155682Smarkm		if (!dvp->id_driver->sensitive_hw)
28255682Smarkm			config_isadev(dvp, &net_imask);
28355682Smarkm	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
28455682Smarkm		if (!dvp->id_driver->sensitive_hw)
28555682Smarkm			config_isadev(dvp, (u_int *)NULL);
28655682Smarkm
28755682Smarkm	bio_imask |= SWI_CLOCK_MASK;
28855682Smarkm	net_imask |= SWI_NET_MASK;
28955682Smarkm	tty_imask |= SWI_TTY_MASK;
29055682Smarkm
29155682Smarkm/*
29255682Smarkm * XXX we should really add the tty device to net_imask when the line is
29355682Smarkm * switched to SLIPDISC, and then remove it when it is switched away from
29455682Smarkm * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
29555682Smarkm * of them is running slip.
29655682Smarkm *
29755682Smarkm * XXX actually, blocking all ttys during a splimp doesn't matter so much
29855682Smarkm * with sio because the serial interrupt layer doesn't use tty_imask.  Only
29955682Smarkm * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
30055682Smarkm * during spltty.
30155682Smarkm */
30255682Smarkm#include "sl.h"
30355682Smarkm#if NSL > 0
30455682Smarkm	net_imask |= tty_imask;
30555682Smarkm	tty_imask = net_imask;
30655682Smarkm#endif
30755682Smarkm
30872445Sassar	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
30972445Sassar
31072445Sassar	if (bootverbose)
31172445Sassar		printf("imasks: bio %x, tty %x, net %x\n",
31272445Sassar		       bio_imask, tty_imask, net_imask);
31355682Smarkm
31472445Sassar	/*
31572445Sassar	 * Finish initializing intr_mask[].  Note that the partly
31655682Smarkm	 * constructed masks aren't actually used since we're at splhigh.
31755682Smarkm	 * For fully dynamic initialization, register_intr() and
31855682Smarkm	 * unregister_intr() will have to adjust the masks for _all_
31955682Smarkm	 * interrupts and for tty_imask, etc.
32055682Smarkm	 */
32155682Smarkm	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
32255682Smarkm		register_imask(dvp, tty_imask);
32355682Smarkm	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
32455682Smarkm		register_imask(dvp, bio_imask);
32555682Smarkm	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
32655682Smarkm		register_imask(dvp, net_imask);
32755682Smarkm	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
32855682Smarkm		register_imask(dvp, SWI_CLOCK_MASK);
32955682Smarkm	spl0();
33055682Smarkm}
33155682Smarkm
33255682Smarkm/*
33355682Smarkm * Configure an ISA device.
33455682Smarkm */
33555682Smarkm
33655682Smarkm
33755682Smarkmstatic void
33855682Smarkmconfig_isadev(isdp, mp)
33955682Smarkm     struct isa_device *isdp;
34055682Smarkm     u_int *mp;
34155682Smarkm{
34255682Smarkm	config_isadev_c(isdp, mp, 0);
34355682Smarkm}
34455682Smarkm
34555682Smarkmvoid
34655682Smarkmreconfig_isadev(isdp, mp)
34755682Smarkm	struct isa_device *isdp;
34855682Smarkm	u_int *mp;
34955682Smarkm{
35055682Smarkm	config_isadev_c(isdp, mp, 1);
35155682Smarkm}
35255682Smarkm
35355682Smarkmstatic void
35455682Smarkmconfig_isadev_c(isdp, mp, reconfig)
35555682Smarkm	struct isa_device *isdp;
35655682Smarkm	u_int *mp;
35755682Smarkm	int reconfig;
35855682Smarkm{
35955682Smarkm	u_int checkbits;
36055682Smarkm	int id_alive;
36155682Smarkm	int last_alive;
36255682Smarkm	struct isa_driver *dp = isdp->id_driver;
36355682Smarkm
36455682Smarkm	if (!isdp->id_enabled) {
36555682Smarkm		printf("%s%d: disabled, not probed.\n",
36655682Smarkm			dp->name, isdp->id_unit);
36755682Smarkm		return;
36855682Smarkm	}
36955682Smarkm	checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR;
37055682Smarkm	if (!reconfig && haveseen_isadev(isdp, checkbits))
37155682Smarkm		return;
37255682Smarkm	if (!reconfig && isdp->id_maddr) {
37355682Smarkm		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
37455682Smarkm		isdp->id_maddr += atdevbase;
37555682Smarkm	}
37655682Smarkm	if (reconfig) {
37755682Smarkm		last_alive = isdp->id_alive;
37855682Smarkm		isdp->id_reconfig = 1;
37955682Smarkm	}
38055682Smarkm	else {
38155682Smarkm		last_alive = 0;
38255682Smarkm		isdp->id_reconfig = 0;
38355682Smarkm	}
38455682Smarkm	id_alive = (*dp->probe)(isdp);
38555682Smarkm	if (id_alive) {
38655682Smarkm		/*
38755682Smarkm		 * Only print the I/O address range if id_alive != -1
38855682Smarkm		 * Right now this is a temporary fix just for the new
38955682Smarkm		 * NPX code so that if it finds a 486 that can use trap
39055682Smarkm		 * 16 it will not report I/O addresses.
39155682Smarkm		 * Rod Grimes 04/26/94
39255682Smarkm		 */
39355682Smarkm		if (!isdp->id_reconfig) {
39455682Smarkm			printf("%s%d", dp->name, isdp->id_unit);
39555682Smarkm			if (id_alive != -1) {
39672445Sassar				printf(" at 0x%x", isdp->id_iobase);
39772445Sassar				if (isdp->id_iobase + id_alive - 1 !=
39872445Sassar				    isdp->id_iobase) {
39972445Sassar					printf("-0x%x",
40072445Sassar					       isdp->id_iobase + id_alive - 1);
40172445Sassar				}
40272445Sassar			}
40372445Sassar			if (isdp->id_irq)
40472445Sassar				printf(" irq %d", ffs(isdp->id_irq) - 1);
40572445Sassar			if (isdp->id_drq != -1)
40672445Sassar				printf(" drq %d", isdp->id_drq);
40772445Sassar			if (isdp->id_maddr)
40872445Sassar				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
40972445Sassar			if (isdp->id_msize)
41072445Sassar				printf(" msize %d", isdp->id_msize);
41172445Sassar			if (isdp->id_flags)
41272445Sassar				printf(" flags 0x%x", isdp->id_flags);
41372445Sassar			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
41472445Sassar				printf(" on motherboard");
41572445Sassar			} else if (isdp->id_iobase >= 0x1000 &&
41672445Sassar				    !(isdp->id_iobase & 0x300)) {
41772445Sassar				printf (" on eisa slot %d",
41872445Sassar					isdp->id_iobase >> 12);
41972445Sassar			} else {
42072445Sassar				printf (" on isa");
42172445Sassar			}
42272445Sassar			printf("\n");
42372445Sassar			/*
42472445Sassar			 * Check for conflicts again.  The driver may have
42572445Sassar			 * changed *dvp.  We should weaken the early check
42672445Sassar			 * since the driver may have been able to change
42772445Sassar			 * *dvp to avoid conflicts if given a chance.  We
42872445Sassar			 * already skip the early check for IRQs and force
429			 * a check for IRQs in the next group of checks.
430			 */
431			checkbits |= CC_IRQ;
432			if (haveseen_isadev(isdp, checkbits))
433				return;
434			isdp->id_alive = id_alive;
435		}
436		(*dp->attach)(isdp);
437		if (isdp->id_irq) {
438			if (mp)
439				INTRMASK(*mp, isdp->id_irq);
440			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
441				      isdp->id_ri_flags, isdp->id_intr,
442				      mp, isdp->id_unit);
443			INTREN(isdp->id_irq);
444		}
445	} else {
446		if (isdp->id_reconfig) {
447			(*dp->attach)(isdp); /* reconfiguration attach */
448		}
449		if (!last_alive) {
450			if (!isdp->id_reconfig) {
451				printf("%s%d not found",
452				       dp->name, isdp->id_unit);
453				if (isdp->id_iobase) {
454					printf(" at 0x%x", isdp->id_iobase);
455				}
456				printf("\n");
457			}
458		}
459		else {
460			/* This code has not been tested.... */
461			if (isdp->id_irq) {
462				INTRDIS(isdp->id_irq);
463				unregister_intr(ffs(isdp->id_irq) - 1,
464						isdp->id_intr);
465				if (mp)
466					INTRUNMASK(*mp, isdp->id_irq);
467			}
468		}
469	}
470}
471
472/*
473 * Fill in default interrupt table (in case of spuruious interrupt
474 * during configuration of kernel, setup interrupt control unit
475 */
476void
477isa_defaultirq()
478{
479	int i;
480
481	/* icu vectors */
482	for (i = 0; i < ICU_LEN; i++)
483		unregister_intr(i, (inthand2_t *)NULL);
484
485	/* initialize 8259's */
486	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
487	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
488	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
489#ifdef AUTO_EOI_1
490	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
491#else
492	outb(IO_ICU1+1, 1);		/* 8086 mode */
493#endif
494	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
495	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
496	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
497
498	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
499	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
500	outb(IO_ICU2+1,2);		/* my slave id is 2 */
501#ifdef AUTO_EOI_2
502	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
503#else
504	outb(IO_ICU2+1,1);		/* 8086 mode */
505#endif
506	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
507	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
508}
509
510static caddr_t	dma_bouncebuf[8];
511static u_int	dma_bouncebufsize[8];
512static u_int8_t	dma_bounced = 0;
513static u_int8_t	dma_busy = 0;		/* Used in isa_dmastart() */
514static u_int8_t	dma_inuse = 0;		/* User for acquire/release */
515
516#define VALID_DMA_MASK (7)
517
518/* high byte of address is stored in this port for i-th dma channel */
519static short dmapageport[8] =
520	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
521
522/*
523 * Setup a DMA channel's bounce buffer.
524 */
525void
526isa_dmainit(chan, bouncebufsize)
527	int chan;
528	u_int bouncebufsize;
529{
530	void *buf;
531
532#ifdef DIAGNOSTIC
533	if (chan & ~VALID_DMA_MASK)
534		panic("isa_dmainit: channel out of range");
535
536	if (dma_bouncebuf[chan] != NULL)
537		panic("isa_dmainit: impossible request");
538#endif
539
540	dma_bouncebufsize[chan] = bouncebufsize;
541
542	/* Try malloc() first.  It works better if it works. */
543	buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT);
544	if (buf != NULL) {
545		if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
546			dma_bouncebuf[chan] = buf;
547			return;
548		}
549		free(buf, M_DEVBUF);
550	}
551	buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful,
552			   1ul, chan & 4 ? 0x20000ul : 0x10000ul);
553	if (buf == NULL)
554		printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize);
555	else
556		dma_bouncebuf[chan] = buf;
557}
558
559/*
560 * Register a DMA channel's usage.  Usually called from a device driver
561 * in open() or during it's initialization.
562 */
563int
564isa_dma_acquire(chan)
565	int chan;
566{
567#ifdef DIAGNOSTIC
568	if (chan & ~VALID_DMA_MASK)
569		panic("isa_dma_acquire: channel out of range");
570#endif
571
572	if (dma_inuse & (1 << chan)) {
573		printf("isa_dma_acquire: channel %d already in use\n", chan);
574		return (EBUSY);
575	}
576	dma_inuse |= (1 << chan);
577
578	return (0);
579}
580
581/*
582 * Unregister a DMA channel's usage.  Usually called from a device driver
583 * during close() or during it's shutdown.
584 */
585void
586isa_dma_release(chan)
587	int chan;
588{
589#ifdef DIAGNOSTIC
590	if (chan & ~VALID_DMA_MASK)
591		panic("isa_dma_release: channel out of range");
592
593	if (dma_inuse & (1 << chan) == 0)
594		printf("isa_dma_release: channel %d not in use\n", chan);
595#endif
596
597	if (dma_busy & (1 << chan)) {
598		dma_busy &= ~(1 << chan);
599		/*
600		 * XXX We should also do "dma_bounced &= (1 << chan);"
601		 * because we are acting on behalf of isa_dmadone() which
602		 * was not called to end the last DMA operation.  This does
603		 * not matter now, but it may in the future.
604		 */
605	}
606
607	dma_inuse &= ~(1 << chan);
608}
609
610/*
611 * isa_dmacascade(): program 8237 DMA controller channel to accept
612 * external dma control by a board.
613 */
614void isa_dmacascade(chan)
615	int chan;
616{
617#ifdef DIAGNOSTIC
618	if (chan & ~VALID_DMA_MASK)
619		panic("isa_dmacascade: channel out of range");
620#endif
621
622	/* set dma channel mode, and set dma channel mode */
623	if ((chan & 4) == 0) {
624		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
625		outb(DMA1_SMSK, chan);
626	} else {
627		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
628		outb(DMA2_SMSK, chan & 3);
629	}
630}
631
632/*
633 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
634 * problems by using a bounce buffer.
635 */
636void isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
637{
638	vm_offset_t phys;
639	int waport;
640	caddr_t newaddr;
641
642#ifdef DIAGNOSTIC
643	if (chan & ~VALID_DMA_MASK)
644		panic("isa_dmastart: channel out of range");
645
646	if ((chan < 4 && nbytes > (1<<16))
647	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
648		panic("isa_dmastart: impossible request");
649
650	if (dma_inuse & (1 << chan) == 0)
651		printf("isa_dmastart: channel %d not acquired\n", chan);
652#endif
653
654	if (dma_busy & (1 << chan))
655		printf("isa_dmastart: channel %d busy\n", chan);
656
657	dma_busy |= (1 << chan);
658
659	if (isa_dmarangecheck(addr, nbytes, chan)) {
660		if (dma_bouncebuf[chan] == NULL
661		    || dma_bouncebufsize[chan] < nbytes)
662			panic("isa_dmastart: bad bounce buffer");
663		dma_bounced |= (1 << chan);
664		newaddr = dma_bouncebuf[chan];
665
666		/* copy bounce buffer on write */
667		if (!(flags & B_READ))
668			bcopy(addr, newaddr, nbytes);
669		addr = newaddr;
670	}
671
672	/* translate to physical */
673	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
674
675	if ((chan & 4) == 0) {
676		/*
677		 * Program one of DMA channels 0..3.  These are
678		 * byte mode channels.
679		 */
680		/* set dma channel mode, and reset address ff */
681
682		/* If B_RAW flag is set, then use autoinitialise mode */
683		if (flags & B_RAW) {
684		  if (flags & B_READ)
685			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
686		  else
687			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
688		}
689		else
690		if (flags & B_READ)
691			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
692		else
693			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
694		outb(DMA1_FFC, 0);
695
696		/* send start address */
697		waport =  DMA1_CHN(chan);
698		outb(waport, phys);
699		outb(waport, phys>>8);
700		outb(dmapageport[chan], phys>>16);
701
702		/* send count */
703		outb(waport + 1, --nbytes);
704		outb(waport + 1, nbytes>>8);
705
706		/* unmask channel */
707		outb(DMA1_SMSK, chan);
708	} else {
709		/*
710		 * Program one of DMA channels 4..7.  These are
711		 * word mode channels.
712		 */
713		/* set dma channel mode, and reset address ff */
714
715		/* If B_RAW flag is set, then use autoinitialise mode */
716		if (flags & B_RAW) {
717		  if (flags & B_READ)
718			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
719		  else
720			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
721		}
722		else
723		if (flags & B_READ)
724			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
725		else
726			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
727		outb(DMA2_FFC, 0);
728
729		/* send start address */
730		waport = DMA2_CHN(chan - 4);
731		outb(waport, phys>>1);
732		outb(waport, phys>>9);
733		outb(dmapageport[chan], phys>>16);
734
735		/* send count */
736		nbytes >>= 1;
737		outb(waport + 2, --nbytes);
738		outb(waport + 2, nbytes>>8);
739
740		/* unmask channel */
741		outb(DMA2_SMSK, chan & 3);
742	}
743}
744
745void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
746{
747#ifdef DIAGNOSTIC
748	if (chan & ~VALID_DMA_MASK)
749		panic("isa_dmadone: channel out of range");
750
751	if (dma_inuse & (1 << chan) == 0)
752		printf("isa_dmadone: channel %d not acquired\n", chan);
753#endif
754
755#if 0
756	/*
757	 * XXX This should be checked, but drivers like ad1848 only call
758	 * isa_dmastart() once because they use Auto DMA mode.  If we
759	 * leave this in, drivers that do this will print this continuously.
760	 */
761	if (dma_busy & (1 << chan) == 0)
762		printf("isa_dmadone: channel %d not busy\n", chan);
763#endif
764
765	if (dma_bounced & (1 << chan)) {
766		/* copy bounce buffer on read */
767		if (flags & B_READ)
768			bcopy(dma_bouncebuf[chan], addr, nbytes);
769
770		dma_bounced &= ~(1 << chan);
771	}
772	dma_busy &= ~(1 << chan);
773}
774
775/*
776 * Check for problems with the address range of a DMA transfer
777 * (non-contiguous physical pages, outside of bus address space,
778 * crossing DMA page boundaries).
779 * Return true if special handling needed.
780 */
781
782static int
783isa_dmarangecheck(caddr_t va, u_int length, int chan) {
784	vm_offset_t phys, priorpage = 0, endva;
785	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
786
787	endva = (vm_offset_t)round_page(va + length);
788	for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
789		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
790#define ISARAM_END	RAM_END
791		if (phys == 0)
792			panic("isa_dmacheck: no physical page present");
793		if (phys >= ISARAM_END)
794			return (1);
795		if (priorpage) {
796			if (priorpage + PAGE_SIZE != phys)
797				return (1);
798			/* check if crossing a DMA page boundary */
799			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
800				return (1);
801		}
802		priorpage = phys;
803	}
804	return (0);
805}
806
807#define NMI_PARITY (1 << 7)
808#define NMI_IOCHAN (1 << 6)
809#define ENMI_WATCHDOG (1 << 7)
810#define ENMI_BUSTIMER (1 << 6)
811#define ENMI_IOSTATUS (1 << 5)
812
813/*
814 * Handle a NMI, possibly a machine check.
815 * return true to panic system, false to ignore.
816 */
817int
818isa_nmi(cd)
819	int cd;
820{
821	int isa_port = inb(0x61);
822	int eisa_port = inb(0x461);
823	if(isa_port & NMI_PARITY) {
824		panic("RAM parity error, likely hardware failure.");
825	} else if(isa_port & NMI_IOCHAN) {
826		panic("I/O channel check, likely hardware failure.");
827	} else if(eisa_port & ENMI_WATCHDOG) {
828		panic("EISA watchdog timer expired, likely hardware failure.");
829	} else if(eisa_port & ENMI_BUSTIMER) {
830		panic("EISA bus timeout, likely hardware failure.");
831	} else if(eisa_port & ENMI_IOSTATUS) {
832		panic("EISA I/O port status error.");
833	} else {
834		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
835		return(0);
836	}
837}
838
839/*
840 * Caught a stray interrupt, notify
841 */
842static void
843isa_strayintr(d)
844	int d;
845{
846
847	/* DON'T BOTHER FOR NOW! */
848	/* for some reason, we get bursts of intr #7, even if not enabled! */
849	/*
850	 * Well the reason you got bursts of intr #7 is because someone
851	 * raised an interrupt line and dropped it before the 8259 could
852	 * prioritize it.  This is documented in the intel data book.  This
853	 * means you have BAD hardware!  I have changed this so that only
854	 * the first 5 get logged, then it quits logging them, and puts
855	 * out a special message. rgrimes 3/25/1993
856	 */
857	/*
858	 * XXX TODO print a different message for #7 if it is for a
859	 * glitch.  Glitches can be distinguished from real #7's by
860	 * testing that the in-service bit is _not_ set.  The test
861	 * must be done before sending an EOI so it can't be done if
862	 * we are using AUTO_EOI_1.
863	 */
864	if (intrcnt[NR_DEVICES + d] <= 5)
865		log(LOG_ERR, "stray irq %d\n", d);
866	if (intrcnt[NR_DEVICES + d] == 5)
867		log(LOG_CRIT,
868		    "too many stray irq %d's; not logging any more\n", d);
869}
870
871/*
872 * Find the highest priority enabled display device.  Since we can't
873 * distinguish display devices from ttys, depend on display devices
874 * being sensitive and before sensitive non-display devices (if any)
875 * in isa_devtab_tty.
876 *
877 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
878 */
879struct isa_device *
880find_display()
881{
882	struct isa_device *dvp;
883
884	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
885		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
886			return (dvp);
887	return (NULL);
888}
889
890/*
891 * find an ISA device in a given isa_devtab_* table, given
892 * the table to search, the expected id_driver entry, and the unit number.
893 *
894 * this function is defined in isa_device.h, and this location is debatable;
895 * i put it there because it's useless w/o, and directly operates on
896 * the other stuff in that file.
897 *
898 */
899
900struct isa_device *find_isadev(table, driverp, unit)
901     struct isa_device *table;
902     struct isa_driver *driverp;
903     int unit;
904{
905  if (driverp == NULL) /* sanity check */
906    return NULL;
907
908  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
909    if (table->id_driver == 0)
910      return NULL;
911
912    table++;
913  }
914
915  return table;
916}
917
918/*
919 * Return nonzero if a (masked) irq is pending for a given device.
920 */
921int
922isa_irq_pending(dvp)
923	struct isa_device *dvp;
924{
925	unsigned id_irq;
926
927	id_irq = dvp->id_irq;
928	if (id_irq & 0xff)
929		return (inb(IO_ICU1) & id_irq);
930	return (inb(IO_ICU2) & (id_irq >> 8));
931}
932
933int
934update_intr_masks(void)
935{
936	int intr, n=0;
937	u_int mask,*maskptr;
938
939	for (intr=0; intr < ICU_LEN; intr ++) {
940		if (intr==2) continue;
941		maskptr = intr_mptr[intr];
942		if (!maskptr) continue;
943		*maskptr |= 1 << intr;
944		mask = *maskptr;
945		if (mask != intr_mask[intr]) {
946#if 0
947			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
948				intr, intr_mask[intr], mask, maskptr);
949#endif
950			intr_mask[intr]=mask;
951			n++;
952		}
953
954	}
955	return (n);
956}
957
958int
959register_intr(intr, device_id, flags, handler, maskptr, unit)
960	int	intr;
961	int	device_id;
962	u_int	flags;
963	inthand2_t *handler;
964	u_int	*maskptr;
965	int	unit;
966{
967	char	*cp;
968	u_long	ef;
969	int	id;
970	u_int	mask = (maskptr ? *maskptr : 0);
971
972	if ((u_int)intr >= ICU_LEN || intr == 2
973	    || (u_int)device_id >= NR_DEVICES)
974		return (EINVAL);
975	if (intr_handler[intr] != isa_strayintr)
976		return (EBUSY);
977	ef = read_eflags();
978	disable_intr();
979	intr_countp[intr] = &intrcnt[device_id];
980	intr_handler[intr] = handler;
981	intr_mptr[intr] = maskptr;
982	intr_mask[intr] = mask | (1 << intr);
983	intr_unit[intr] = unit;
984	setidt(ICU_OFFSET + intr,
985	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
986	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
987	write_eflags(ef);
988	for (cp = intrnames, id = 0; id <= device_id; id++)
989		while (*cp++ != '\0')
990			;
991	if (cp > eintrnames)
992		return (0);
993	if (intr < 10) {
994		cp[-3] = intr + '0';
995		cp[-2] = ' ';
996	} else {
997		cp[-3] = '1';
998		cp[-2] = intr - 10 + '0';
999	}
1000	return (0);
1001}
1002
1003static void
1004register_imask(dvp, mask)
1005	struct isa_device *dvp;
1006	u_int	mask;
1007{
1008	if (dvp->id_alive && dvp->id_irq) {
1009		int	intr;
1010
1011		intr = ffs(dvp->id_irq) - 1;
1012		intr_mask[intr] = mask | (1 <<intr);
1013	}
1014	(void) update_intr_masks();
1015}
1016
1017int
1018unregister_intr(intr, handler)
1019	int	intr;
1020	inthand2_t *handler;
1021{
1022	u_long	ef;
1023
1024	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1025		return (EINVAL);
1026	ef = read_eflags();
1027	disable_intr();
1028	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1029	intr_handler[intr] = isa_strayintr;
1030	intr_mptr[intr] = NULL;
1031	intr_mask[intr] = HWI_MASK | SWI_MASK;
1032	intr_unit[intr] = intr;
1033	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
1034	    GSEL(GCODE_SEL, SEL_KPL));
1035	write_eflags(ef);
1036	return (0);
1037}
1038