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