isa.c revision 4109
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
37 *	$Id: isa.c,v 1.35 1994/11/01 17:26:29 ache Exp $
38 */
39
40/*
41 * code to manage AT bus
42 *
43 * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44 * Fixed uninitialized variable problem and added code to deal
45 * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46 * mode DMA count compution and reorganized DMA setup code in
47 * isa_dmastart()
48 */
49
50#include <sys/param.h>
51#include <sys/systm.h>		/* isn't it a joy */
52#include <sys/kernel.h>		/* to have three of these */
53#include <sys/proc.h>
54#include <sys/conf.h>
55#include <sys/file.h>
56#include <sys/buf.h>
57#include <sys/uio.h>
58#include <sys/syslog.h>
59#include <sys/malloc.h>
60#include <sys/rlist.h>
61#include <machine/segments.h>
62#include <vm/vm.h>
63#include <machine/spl.h>
64#include <i386/isa/isa_device.h>
65#include <i386/isa/isa.h>
66#include <i386/isa/icu.h>
67#include <i386/isa/ic/i8237.h>
68#include <i386/isa/ic/i8042.h>
69#include <sys/devconf.h>
70#include "vector.h"
71
72/*
73**  Register definitions for DMA controller 1 (channels 0..3):
74*/
75#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
76#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
77#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
78#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
79
80/*
81**  Register definitions for DMA controller 2 (channels 4..7):
82*/
83#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
84#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
85#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
86#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
87
88/*
89 * Bits to specify the type and amount of conflict checking.
90 */
91#define	CC_ATTACH	(1 << 0)
92#define	CC_DRQ		(1 << 1)
93#define	CC_IOADDR	(1 << 2)
94#define	CC_IRQ		(1 << 3)
95#define	CC_MEMADDR	(1 << 4)
96
97/*
98 * XXX these defines should be in a central place.
99 */
100#define	read_eflags()		({u_long ef; \
101				  __asm("pushfl; popl %0" : "=a" (ef)); \
102				  ef; })
103#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
104
105u_long	*intr_countp[ICU_LEN];
106inthand2_t *intr_handler[ICU_LEN];
107u_int	intr_mask[ICU_LEN];
108int	intr_unit[ICU_LEN];
109
110struct kern_devconf kdc_isa0 = {
111	0, 0, 0,		/* filled in by dev_attach */
112	"isa", 0, { MDDT_BUS, 0 },
113	0, 0, 0, BUS_EXTERNALLEN,
114	0,			/* no parent yet; parent should be CPU */
115	0,			/* no parentdata */
116	DC_BUSY,		/* busses are always busy */
117	"ISA or EISA bus"
118};
119
120static inthand_t *fastintr[ICU_LEN] = {
121	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
122	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
123	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
124	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
125	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
126	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
127	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
128	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
129};
130
131static inthand_t *slowintr[ICU_LEN] = {
132	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
133	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
134	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
135	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
136};
137
138static void config_isadev __P((struct isa_device *isdp, u_int *mp));
139static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
140			  int item, char const *whatnot, char const *reason,
141			  char const *format));
142static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
143			 u_int checkbits));
144static int haveseen_isadev __P((struct isa_device *dvp, u_int checkbits));
145static inthand2_t isa_strayintr;
146static void register_imask __P((struct isa_device *dvp, u_int mask));
147
148/*
149 * print a conflict message
150 */
151static void
152conflict(dvp, tmpdvp, item, whatnot, reason, format)
153	struct isa_device	*dvp;
154	struct isa_device	*tmpdvp;
155	int			item;
156	char const		*whatnot;
157	char const		*reason;
158	char const		*format;
159{
160	printf("%s%d not %sed due to %s conflict with %s%d at ",
161		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
162		tmpdvp->id_driver->name, tmpdvp->id_unit);
163	printf(format, item);
164	printf("\n");
165}
166
167/*
168 * Check to see if things are alread in use, like IRQ's, I/O addresses
169 * and Memory addresses.
170 */
171static int
172haveseen(dvp, tmpdvp, checkbits)
173	struct isa_device *dvp;
174	struct isa_device *tmpdvp;
175	u_int	checkbits;
176{
177	/*
178	 * Only check against devices that have already been found
179	 */
180	if (tmpdvp->id_alive) {
181		char const *whatnot;
182
183		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
184		/*
185		 * Check for I/O address conflict.  We can only check the
186		 * starting address of the device against the range of the
187		 * device that has already been probed since we do not
188		 * know how many I/O addresses this device uses.
189		 */
190		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
191			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
192			    (dvp->id_iobase <=
193				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
194				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
195					 "I/O address", "0x%x");
196				return 1;
197			}
198		}
199		/*
200		 * Check for Memory address conflict.  We can check for
201		 * range overlap, but it will not catch all cases since the
202		 * driver may adjust the msize paramater during probe, for
203		 * now we just check that the starting address does not
204		 * fall within any allocated region.
205		 * XXX could add a second check after the probe for overlap,
206		 * since at that time we would know the full range.
207		 * XXX KERNBASE is a hack, we should have vaddr in the table!
208		 */
209		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
210			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
211			    (KERNBASE + dvp->id_maddr <=
212			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
213				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
214					 whatnot, "maddr", "0x%x");
215				return 1;
216			}
217		}
218		/*
219		 * Check for IRQ conflicts.
220		 */
221		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
222			if (tmpdvp->id_irq == dvp->id_irq) {
223				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
224					 whatnot, "irq", "%d");
225				return 1;
226			}
227		}
228		/*
229		 * Check for DRQ conflicts.
230		 */
231		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
232			if (tmpdvp->id_drq == dvp->id_drq) {
233				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
234					 "drq", "%d");
235				return 1;
236			}
237		}
238	}
239	return 0;
240}
241
242/*
243 * Search through all the isa_devtab_* tables looking for anything that
244 * conflicts with the current device.
245 */
246static int
247haveseen_isadev(dvp, checkbits)
248	struct isa_device *dvp;
249	u_int	checkbits;
250{
251	struct isa_device *tmpdvp;
252	int	status = 0;
253
254	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
255		status |= haveseen(dvp, tmpdvp, checkbits);
256		if (status)
257			return status;
258	}
259	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
260		status |= haveseen(dvp, tmpdvp, checkbits);
261		if (status)
262			return status;
263	}
264	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
265		status |= haveseen(dvp, tmpdvp, checkbits);
266		if (status)
267			return status;
268	}
269	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
270		status |= haveseen(dvp, tmpdvp, checkbits);
271		if (status)
272			return status;
273	}
274	return(status);
275}
276
277/*
278 * Configure all ISA devices
279 */
280void
281isa_configure() {
282	struct isa_device *dvp;
283
284	dev_attach(&kdc_isa0);
285
286	splhigh();
287	enable_intr();
288	INTREN(IRQ_SLAVE);
289	printf("Probing for devices on the ISA bus:\n");
290	/* First probe all the sensitive probes */
291	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
292		if (dvp->id_driver->sensitive_hw)
293			config_isadev(dvp, &tty_imask);
294	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
295		if (dvp->id_driver->sensitive_hw)
296			config_isadev(dvp, &bio_imask);
297	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
298		if (dvp->id_driver->sensitive_hw)
299			config_isadev(dvp, &net_imask);
300	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
301		if (dvp->id_driver->sensitive_hw)
302			config_isadev(dvp, (u_int *)NULL);
303
304	/* Then all the bad ones */
305	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
306		if (!dvp->id_driver->sensitive_hw)
307			config_isadev(dvp, &tty_imask);
308	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
309		if (!dvp->id_driver->sensitive_hw)
310			config_isadev(dvp, &bio_imask);
311	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
312		if (!dvp->id_driver->sensitive_hw)
313			config_isadev(dvp, &net_imask);
314	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
315		if (!dvp->id_driver->sensitive_hw)
316			config_isadev(dvp, (u_int *)NULL);
317
318	bio_imask |= SWI_CLOCK_MASK;
319	net_imask |= SWI_NET_MASK;
320	tty_imask |= SWI_TTY_MASK;
321
322/*
323 * XXX we should really add the tty device to net_imask when the line is
324 * switched to SLIPDISC, and then remove it when it is switched away from
325 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
326 * of them is running slip.
327 *
328 * XXX actually, blocking all ttys during a splimp doesn't matter so much
329 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
330 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
331 * during spltty.
332 */
333#include "sl.h"
334#include "ppp.h"
335
336#if (NSL > 0) || (NPPP > 0)
337	net_imask |= tty_imask;
338	tty_imask = net_imask;
339#endif
340	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
341#ifdef DIAGNOSTIC
342	printf("bio_imask %x tty_imask %x net_imask %x\n",
343	       bio_imask, tty_imask, net_imask);
344#endif
345	/*
346	 * Finish initializing intr_mask[].  Note that the partly
347	 * constructed masks aren't actually used since we're at splhigh.
348	 * For fully dynamic initialization, register_intr() and
349	 * unregister_intr() will have to adjust the masks for _all_
350	 * interrupts and for tty_imask, etc.
351	 */
352	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
353		register_imask(dvp, tty_imask);
354	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
355		register_imask(dvp, bio_imask);
356	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
357		register_imask(dvp, net_imask);
358	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
359		register_imask(dvp, SWI_CLOCK_MASK);
360	spl0();
361}
362
363/*
364 * Configure an ISA device.
365 */
366
367
368static void config_isadev_c();
369
370static void
371config_isadev(isdp, mp)
372     struct isa_device *isdp;
373     u_int *mp;
374{
375	config_isadev_c(isdp, mp, 0);
376}
377
378void
379reconfig_isadev(isdp, mp)
380	struct isa_device *isdp;
381	u_int *mp;
382{
383	config_isadev_c(isdp, mp, 1);
384}
385
386static void
387config_isadev_c(isdp, mp, reconfig)
388	struct isa_device *isdp;
389	u_int *mp;
390	int reconfig;
391{
392	u_int checkbits;
393	int id_alive;
394	int last_alive;
395	struct isa_driver *dp = isdp->id_driver;
396
397 	checkbits = 0;
398#ifndef ALLOW_CONFLICT_DRQ
399	checkbits |= CC_DRQ;
400#endif
401#ifndef ALLOW_CONFLICT_IOADDR
402	checkbits |= CC_IOADDR;
403#endif
404#ifndef ALLOW_CONFLICT_MEMADDR
405	checkbits |= CC_MEMADDR;
406#endif
407	if (!isdp->id_enabled) {
408		printf("%s%d: disabled, not probed.\n",
409			dp->name, isdp->id_unit);
410		return;
411	}
412	if (!reconfig && haveseen_isadev(isdp, checkbits))
413		return;
414	if (!reconfig && isdp->id_maddr) {
415		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
416		isdp->id_maddr += atdevbase;
417	}
418	if (reconfig) {
419		last_alive = isdp->id_alive;
420	}
421	else {
422		last_alive = 0;
423	}
424	id_alive = (*dp->probe)(isdp);
425	if (id_alive) {
426		/*
427		 * Only print the I/O address range if id_alive != -1
428		 * Right now this is a temporary fix just for the new
429		 * NPX code so that if it finds a 486 that can use trap
430		 * 16 it will not report I/O addresses.
431		 * Rod Grimes 04/26/94
432		 */
433		if (!isdp->id_reconfig) {
434			printf("%s%d", dp->name, isdp->id_unit);
435			if (id_alive != -1) {
436 				printf(" at 0x%x", isdp->id_iobase);
437 				if ((isdp->id_iobase + id_alive - 1) !=
438 				     isdp->id_iobase) {
439 					printf("-0x%x",
440					       isdp->id_iobase + id_alive - 1);
441				}
442			}
443			if (isdp->id_irq)
444				printf(" irq %d", ffs(isdp->id_irq) - 1);
445			if (isdp->id_drq != -1)
446				printf(" drq %d", isdp->id_drq);
447			if (isdp->id_maddr)
448				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
449			if (isdp->id_msize)
450				printf(" msize %d", isdp->id_msize);
451			if (isdp->id_flags)
452				printf(" flags 0x%x", isdp->id_flags);
453			if (isdp->id_iobase) {
454				if (isdp->id_iobase < 0x100) {
455					printf(" on motherboard\n");
456				} else {
457					if (isdp->id_iobase >= 0x1000) {
458						printf (" on eisa\n");
459					} else {
460						printf (" on isa\n");
461					}
462				}
463			}
464			/*
465			 * Check for conflicts again.  The driver may have
466			 * changed *dvp.  We should weaken the early check
467			 * since the driver may have been able to change
468			 * *dvp to avoid conflicts if given a chance.  We
469			 * already skip the early check for IRQs and force
470			 * a check for IRQs in the next group of checks.
471		 	 */
472#ifndef ALLOW_CONFLICT_IRQ
473			checkbits |= CC_IRQ;
474#endif
475			if (haveseen_isadev(isdp, checkbits))
476				return;
477			isdp->id_alive = id_alive;
478		}
479		(*dp->attach)(isdp);
480		if (isdp->id_irq) {
481			if (mp)
482				INTRMASK(*mp, isdp->id_irq);
483			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
484				      isdp->id_ri_flags, isdp->id_intr,
485				      mp, isdp->id_unit);
486			INTREN(isdp->id_irq);
487		}
488	} else {
489		if (isdp->id_reconfig) {
490			(*dp->attach)(isdp); /* reconfiguration attach */
491		}
492		if (!last_alive) {
493			if (!isdp->id_reconfig) {
494				printf("%s%d not found", dp->name, isdp->id_unit);
495				if (isdp->id_iobase) {
496					printf(" at 0x%x", isdp->id_iobase);
497				}
498				printf("\n");
499			}
500		}
501		else {
502			/* This code has not been tested.... */
503			if (isdp->id_irq) {
504				INTRDIS(isdp->id_irq);
505				unregister_intr(ffs(isdp->id_irq) - 1,
506						isdp->id_intr);
507				if (mp)
508					INTRUNMASK(*mp, isdp->id_irq);
509			}
510		}
511	}
512}
513
514/*
515 * Provide ISA-specific device information to user programs using the
516 * hw.devconf interface.
517 */
518int
519isa_externalize(struct isa_device *id, void *userp, size_t *maxlen)
520{
521	if(*maxlen < sizeof *id) {
522		return ENOMEM;
523	}
524
525	*maxlen -= sizeof *id;
526	return copyout(id, userp, sizeof *id);
527}
528
529/*
530 * Do the same thing for EISA information.  EISA information is currently
531 * the same as ISA information plus a slot number, but could be extended in
532 * the future.
533 */
534int
535eisa_externalize(struct isa_device *id, int slot, void *userp, size_t *maxlen)
536{
537	int rv;
538
539	if(*maxlen < (sizeof *id) + (sizeof slot)) {
540		return ENOMEM;
541	}
542	*maxlen -= (sizeof *id) + (sizeof slot);
543
544	rv = copyout(id, userp, sizeof *id);
545	if(rv) return rv;
546
547	return copyout(&slot, (char *)userp + sizeof *id, sizeof slot);
548}
549
550/*
551 * This is used to forcibly reconfigure an ISA device.  It currently just
552 * returns an error 'cos you can't do that yet.  It is here to demonstrate
553 * what the `internalize' routine is supposed to do.
554 */
555int
556isa_internalize(struct isa_device *id, void **userpp, size_t *len)
557{
558	struct isa_device myid;
559	char *userp = *userpp;
560	int rv;
561
562	if(*len < sizeof *id) {
563		return EINVAL;
564	}
565
566	rv = copyin(userp, &myid, sizeof myid);
567	if(rv) return rv;
568	*userpp = userp + sizeof myid;
569	*len -= sizeof myid;
570
571	rv = EOPNOTSUPP;
572	/* code would go here to validate the configuration request */
573	/* code would go here to actually perform the reconfiguration */
574	return rv;
575}
576
577int
578isa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
579			void *userp, size_t l)
580{
581	return isa_externalize(kdc->kdc_isa, userp, &l);
582}
583
584int
585eisa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
586			 void *userp, size_t l)
587{
588	return eisa_externalize(kdc->kdc_isa, -1, userp, &l);
589}
590
591/*
592 * Fill in default interrupt table (in case of spuruious interrupt
593 * during configuration of kernel, setup interrupt control unit
594 */
595void
596isa_defaultirq()
597{
598	int i;
599
600	/* icu vectors */
601	for (i = 0; i < ICU_LEN; i++)
602		unregister_intr(i, (inthand2_t *)NULL);
603
604	/* initialize 8259's */
605	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
606	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
607	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
608#ifdef AUTO_EOI_1
609	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
610#else
611	outb(IO_ICU1+1, 1);		/* 8086 mode */
612#endif
613	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
614	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
615	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
616
617	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
618	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
619	outb(IO_ICU2+1,2);		/* my slave id is 2 */
620#ifdef AUTO_EOI_2
621	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
622#else
623	outb(IO_ICU2+1,1);		/* 8086 mode */
624#endif
625	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
626	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
627}
628
629/* region of physical memory known to be contiguous */
630vm_offset_t isaphysmem;
631static caddr_t dma_bounce[8];		/* XXX */
632static char bounced[8];		/* XXX */
633#define MAXDMASZ 512		/* XXX */
634
635/* high byte of address is stored in this port for i-th dma channel */
636static short dmapageport[8] =
637	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
638
639/*
640 * isa_dmacascade(): program 8237 DMA controller channel to accept
641 * external dma control by a board.
642 */
643void isa_dmacascade(unsigned chan)
644{
645	if (chan > 7)
646		panic("isa_dmacascade: impossible request");
647
648	/* set dma channel mode, and set dma channel mode */
649	if ((chan & 4) == 0) {
650		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
651		outb(DMA1_SMSK, chan);
652	} else {
653		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
654		outb(DMA2_SMSK, chan & 3);
655	}
656}
657
658static int
659isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
660
661/*
662 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
663 * problems by using a bounce buffer.
664 */
665void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
666{	vm_offset_t phys;
667	int waport;
668	caddr_t newaddr;
669
670	if (    chan > 7
671	    || (chan < 4 && nbytes > (1<<16))
672	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
673		panic("isa_dmastart: impossible request");
674
675	if (isa_dmarangecheck(addr, nbytes, chan)) {
676		if (dma_bounce[chan] == 0)
677			dma_bounce[chan] =
678				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
679				(caddr_t) isaphysmem + NBPG*chan;
680		bounced[chan] = 1;
681		newaddr = dma_bounce[chan];
682		*(int *) newaddr = 0;	/* XXX */
683
684		/* copy bounce buffer on write */
685		if (!(flags & B_READ))
686			bcopy(addr, newaddr, nbytes);
687		addr = newaddr;
688	}
689
690	/* translate to physical */
691	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
692
693	if ((chan & 4) == 0) {
694		/*
695		 * Program one of DMA channels 0..3.  These are
696		 * byte mode channels.
697		 */
698		/* set dma channel mode, and reset address ff */
699
700		/* If B_RAW flag is set, then use autoinitialise mode */
701		if (flags & B_RAW) {
702		  if (flags & B_READ)
703			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
704		  else
705			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
706		}
707		else
708		if (flags & B_READ)
709			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
710		else
711			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
712		outb(DMA1_FFC, 0);
713
714		/* send start address */
715		waport =  DMA1_CHN(chan);
716		outb(waport, phys);
717		outb(waport, phys>>8);
718		outb(dmapageport[chan], phys>>16);
719
720		/* send count */
721		outb(waport + 1, --nbytes);
722		outb(waport + 1, nbytes>>8);
723
724		/* unmask channel */
725		outb(DMA1_SMSK, chan);
726	} else {
727		/*
728		 * Program one of DMA channels 4..7.  These are
729		 * word mode channels.
730		 */
731		/* set dma channel mode, and reset address ff */
732
733		/* If B_RAW flag is set, then use autoinitialise mode */
734		if (flags & B_RAW) {
735		  if (flags & B_READ)
736			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
737		  else
738			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
739		}
740		else
741		if (flags & B_READ)
742			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
743		else
744			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
745		outb(DMA2_FFC, 0);
746
747		/* send start address */
748		waport = DMA2_CHN(chan - 4);
749		outb(waport, phys>>1);
750		outb(waport, phys>>9);
751		outb(dmapageport[chan], phys>>16);
752
753		/* send count */
754		nbytes >>= 1;
755		outb(waport + 2, --nbytes);
756		outb(waport + 2, nbytes>>8);
757
758		/* unmask channel */
759		outb(DMA2_SMSK, chan & 3);
760	}
761}
762
763void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
764{
765
766	/* copy bounce buffer on read */
767	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
768	if (bounced[chan]) {
769		bcopy(dma_bounce[chan], addr, nbytes);
770		bounced[chan] = 0;
771	}
772}
773
774/*
775 * Check for problems with the address range of a DMA transfer
776 * (non-contiguous physical pages, outside of bus address space,
777 * crossing DMA page boundaries).
778 * Return true if special handling needed.
779 */
780
781static int
782isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
783	vm_offset_t phys, priorpage = 0, endva;
784	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
785
786	endva = (vm_offset_t)round_page(va + length);
787	for (; va < (caddr_t) endva ; va += NBPG) {
788		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
789#define ISARAM_END	RAM_END
790		if (phys == 0)
791			panic("isa_dmacheck: no physical page present");
792		if (phys >= ISARAM_END)
793			return (1);
794		if (priorpage) {
795			if (priorpage + NBPG != phys)
796				return (1);
797			/* check if crossing a DMA page boundary */
798			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
799				return (1);
800		}
801		priorpage = phys;
802	}
803	return (0);
804}
805
806/* head of queue waiting for physmem to become available */
807struct buf isa_physmemq;
808
809/* blocked waiting for resource to become free for exclusive use */
810static isaphysmemflag;
811/* if waited for and call requested when free (B_CALL) */
812static void (*isaphysmemunblock)(); /* needs to be a list */
813
814/*
815 * Allocate contiguous physical memory for transfer, returning
816 * a *virtual* address to region. May block waiting for resource.
817 * (assumed to be called at splbio())
818 */
819caddr_t
820isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
821
822	isaphysmemunblock = func;
823	while (isaphysmemflag & B_BUSY) {
824		isaphysmemflag |= B_WANTED;
825		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
826	}
827	isaphysmemflag |= B_BUSY;
828
829	return((caddr_t)isaphysmem);
830}
831
832/*
833 * Free contiguous physical memory used for transfer.
834 * (assumed to be called at splbio())
835 */
836void
837isa_freephysmem(caddr_t va, unsigned length) {
838
839	isaphysmemflag &= ~B_BUSY;
840	if (isaphysmemflag & B_WANTED) {
841		isaphysmemflag &= B_WANTED;
842		wakeup((caddr_t)&isaphysmemflag);
843		if (isaphysmemunblock)
844			(*isaphysmemunblock)();
845	}
846}
847
848#define NMI_PARITY (1 << 7)
849#define NMI_IOCHAN (1 << 6)
850#define ENMI_WATCHDOG (1 << 7)
851#define ENMI_BUSTIMER (1 << 6)
852#define ENMI_IOSTATUS (1 << 5)
853
854/*
855 * Handle a NMI, possibly a machine check.
856 * return true to panic system, false to ignore.
857 */
858int
859isa_nmi(cd)
860	int cd;
861{
862	int isa_port = inb(0x61);
863	int eisa_port = inb(0x461);
864	if(isa_port & NMI_PARITY) {
865		panic("RAM parity error, likely hardware failure.");
866	} else if(isa_port & NMI_IOCHAN) {
867		panic("I/O channel check, likely hardware failure.");
868	} else if(eisa_port & ENMI_WATCHDOG) {
869		panic("EISA watchdog timer expired, likely hardware failure.");
870	} else if(eisa_port & ENMI_BUSTIMER) {
871		panic("EISA bus timeout, likely hardware failure.");
872	} else if(eisa_port & ENMI_IOSTATUS) {
873		panic("EISA I/O port status error.");
874	} else {
875		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
876		return(0);
877	}
878}
879
880/*
881 * Caught a stray interrupt, notify
882 */
883static void
884isa_strayintr(d)
885	int d;
886{
887
888	/* DON'T BOTHER FOR NOW! */
889	/* for some reason, we get bursts of intr #7, even if not enabled! */
890	/*
891	 * Well the reason you got bursts of intr #7 is because someone
892	 * raised an interrupt line and dropped it before the 8259 could
893	 * prioritize it.  This is documented in the intel data book.  This
894	 * means you have BAD hardware!  I have changed this so that only
895	 * the first 5 get logged, then it quits logging them, and puts
896	 * out a special message. rgrimes 3/25/1993
897	 */
898	/*
899	 * XXX TODO print a different message for #7 if it is for a
900	 * glitch.  Glitches can be distinguished from real #7's by
901	 * testing that the in-service bit is _not_ set.  The test
902	 * must be done before sending an EOI so it can't be done if
903	 * we are using AUTO_EOI_1.
904	 */
905	if (intrcnt[NR_DEVICES + d] <= 5)
906		log(LOG_ERR, "stray irq %d\n", d);
907	if (intrcnt[NR_DEVICES + d] == 5)
908		log(LOG_CRIT,
909		    "too many stray irq %d's; not logging any more\n", d);
910}
911
912/*
913 * find an ISA device in a given isa_devtab_* table, given
914 * the table to search, the expected id_driver entry, and the unit number.
915 *
916 * this function is defined in isa_device.h, and this location is debatable;
917 * i put it there because it's useless w/o, and directly operates on
918 * the other stuff in that file.
919 *
920 */
921
922struct isa_device *find_isadev(table, driverp, unit)
923     struct isa_device *table;
924     struct isa_driver *driverp;
925     int unit;
926{
927  if (driverp == NULL) /* sanity check */
928    return NULL;
929
930  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
931    if (table->id_driver == 0)
932      return NULL;
933
934    table++;
935  }
936
937  return table;
938}
939
940/*
941 * Return nonzero if a (masked) irq is pending for a given device.
942 */
943int
944isa_irq_pending(dvp)
945	struct isa_device *dvp;
946{
947	unsigned id_irq;
948
949	id_irq = dvp->id_irq;
950	if (id_irq & 0xff)
951		return (inb(IO_ICU1) & id_irq);
952	return (inb(IO_ICU2) & (id_irq >> 8));
953}
954
955int
956register_intr(intr, device_id, flags, handler, maskptr, unit)
957	int	intr;
958	int	device_id;
959	u_int	flags;
960	inthand2_t *handler;
961	u_int	*maskptr;
962	int	unit;
963{
964	char	*cp;
965	u_long	ef;
966	int	id;
967	u_int	mask = (maskptr ? *maskptr : 0);
968
969	if ((u_int)intr >= ICU_LEN || intr == 2
970	    || (u_int)device_id >= NR_DEVICES)
971		return (EINVAL);
972	if (intr_handler[intr] != isa_strayintr)
973		return (EBUSY);
974	ef = read_eflags();
975	disable_intr();
976	intr_countp[intr] = &intrcnt[device_id];
977	intr_handler[intr] = handler;
978	intr_mask[intr] = mask | (1 << intr);
979	intr_unit[intr] = unit;
980	setidt(ICU_OFFSET + intr,
981	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
982	       SDT_SYS386IGT, SEL_KPL);
983	write_eflags(ef);
984	for (cp = intrnames, id = 0; id <= device_id; id++)
985		while (*cp++ != '\0')
986			;
987	if (cp > eintrnames)
988		return (0);
989	if (intr < 10) {
990		cp[-3] = intr + '0';
991		cp[-2] = ' ';
992	} else {
993		cp[-3] = '1';
994		cp[-2] = intr - 10 + '0';
995	}
996	return (0);
997}
998
999static void
1000register_imask(dvp, mask)
1001	struct isa_device *dvp;
1002	u_int	mask;
1003{
1004	if (dvp->id_alive && dvp->id_irq) {
1005		int	intr;
1006
1007		intr = ffs(dvp->id_irq) - 1;
1008		intr_mask[intr] = mask | (1 <<intr);
1009	}
1010}
1011
1012int
1013unregister_intr(intr, handler)
1014	int	intr;
1015	inthand2_t *handler;
1016{
1017	u_long	ef;
1018
1019	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1020		return (EINVAL);
1021	ef = read_eflags();
1022	disable_intr();
1023	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1024	intr_handler[intr] = isa_strayintr;
1025	intr_mask[intr] = HWI_MASK | SWI_MASK;
1026	intr_unit[intr] = intr;
1027	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
1028	write_eflags(ef);
1029	return (0);
1030}
1031