isa.c revision 11978
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.53 1995/10/22 15:07:43 bde 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 <machine/cpu.h>
65#include <i386/isa/isa_device.h>
66#include <i386/isa/isa.h>
67#include <i386/isa/icu.h>
68#include <i386/isa/ic/i8237.h>
69#include <i386/isa/ic/i8042.h>
70#include <sys/devconf.h>
71#include "vector.h"
72
73/*
74**  Register definitions for DMA controller 1 (channels 0..3):
75*/
76#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
77#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
78#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
79#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
80
81/*
82**  Register definitions for DMA controller 2 (channels 4..7):
83*/
84#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
85#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
86#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
87#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
88
89/*
90 * XXX these defines should be in a central place.
91 */
92#define	read_eflags()		({u_long ef; \
93				  __asm("pushfl; popl %0" : "=a" (ef)); \
94				  ef; })
95#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
96
97u_long	*intr_countp[ICU_LEN];
98inthand2_t *intr_handler[ICU_LEN];
99u_int	intr_mask[ICU_LEN];
100u_int*	intr_mptr[ICU_LEN];
101int	intr_unit[ICU_LEN];
102
103extern struct kern_devconf kdc_cpu0;
104
105struct kern_devconf kdc_isa0 = {
106	0, 0, 0,		/* filled in by dev_attach */
107	"isa", 0, { MDDT_BUS, 0 },
108	0, 0, 0, BUS_EXTERNALLEN,
109	&kdc_cpu0,		/* parent is the CPU */
110	0,			/* no parentdata */
111	DC_BUSY,		/* busses are always busy */
112	"ISA or EISA bus",
113	DC_CLS_BUS		/* class */
114};
115
116static inthand_t *fastintr[ICU_LEN] = {
117	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
118	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
119	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
120	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
121	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
122	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
123	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
124	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
125};
126
127static inthand_t *slowintr[ICU_LEN] = {
128	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
129	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
130	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
131	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
132};
133
134static void config_isadev __P((struct isa_device *isdp, u_int *mp));
135static void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
136				 int reconfig));
137static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
138			  int item, char const *whatnot, char const *reason,
139			  char const *format));
140static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
141			 u_int checkbits));
142static inthand2_t isa_strayintr;
143static void register_imask __P((struct isa_device *dvp, u_int mask));
144
145/*
146 * print a conflict message
147 */
148static void
149conflict(dvp, tmpdvp, item, whatnot, reason, format)
150	struct isa_device	*dvp;
151	struct isa_device	*tmpdvp;
152	int			item;
153	char const		*whatnot;
154	char const		*reason;
155	char const		*format;
156{
157	printf("%s%d not %sed due to %s conflict with %s%d at ",
158		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
159		tmpdvp->id_driver->name, tmpdvp->id_unit);
160	printf(format, item);
161	printf("\n");
162}
163
164/*
165 * Check to see if things are already in use, like IRQ's, I/O addresses
166 * and Memory addresses.
167 */
168static int
169haveseen(dvp, tmpdvp, checkbits)
170	struct isa_device *dvp;
171	struct isa_device *tmpdvp;
172	u_int	checkbits;
173{
174	/*
175	 * Only check against devices that have already been found and are not
176	 * unilaterally allowed to conflict anyway.
177	 */
178	if (tmpdvp->id_alive && !dvp->id_conflicts) {
179		char const *whatnot;
180
181		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
182		/*
183		 * Check for I/O address conflict.  We can only check the
184		 * starting address of the device against the range of the
185		 * device that has already been probed since we do not
186		 * know how many I/O addresses this device uses.
187		 */
188		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
189			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
190			    (dvp->id_iobase <=
191				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
192				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
193					 "I/O address", "0x%x");
194				return 1;
195			}
196		}
197		/*
198		 * Check for Memory address conflict.  We can check for
199		 * range overlap, but it will not catch all cases since the
200		 * driver may adjust the msize paramater during probe, for
201		 * now we just check that the starting address does not
202		 * fall within any allocated region.
203		 * XXX could add a second check after the probe for overlap,
204		 * since at that time we would know the full range.
205		 * XXX KERNBASE is a hack, we should have vaddr in the table!
206		 */
207		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
208			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
209			    (KERNBASE + dvp->id_maddr <=
210			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
211				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
212					 whatnot, "maddr", "0x%x");
213				return 1;
214			}
215		}
216		/*
217		 * Check for IRQ conflicts.
218		 */
219		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
220			if (tmpdvp->id_irq == dvp->id_irq) {
221				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
222					 whatnot, "irq", "%d");
223				return 1;
224			}
225		}
226		/*
227		 * Check for DRQ conflicts.
228		 */
229		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
230			if (tmpdvp->id_drq == dvp->id_drq) {
231				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
232					 "drq", "%d");
233				return 1;
234			}
235		}
236	}
237	return 0;
238}
239
240/*
241 * Search through all the isa_devtab_* tables looking for anything that
242 * conflicts with the current device.
243 */
244#include "eisa.h"
245#if NEISA > 0
246extern struct isa_device isa_devtab_eisa[];
247#endif
248
249int
250haveseen_isadev(dvp, checkbits)
251	struct isa_device *dvp;
252	u_int	checkbits;
253{
254	struct isa_device *tmpdvp;
255	int	status = 0;
256
257	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
258		status |= haveseen(dvp, tmpdvp, checkbits);
259		if (status)
260			return status;
261	}
262	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
263		status |= haveseen(dvp, tmpdvp, checkbits);
264		if (status)
265			return status;
266	}
267	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
268		status |= haveseen(dvp, tmpdvp, checkbits);
269		if (status)
270			return status;
271	}
272	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
273		status |= haveseen(dvp, tmpdvp, checkbits);
274		if (status)
275			return status;
276	}
277#if NEISA > 0
278	for (tmpdvp = isa_devtab_eisa; tmpdvp->id_driver; tmpdvp++) {
279		status |= haveseen(dvp, tmpdvp, checkbits);
280		if (status)
281			return status;
282	}
283#endif
284	return(status);
285}
286
287/*
288 * Configure all ISA devices
289 */
290void
291isa_configure() {
292	struct isa_device *dvp;
293
294	dev_attach(&kdc_isa0);
295
296	splhigh();
297	enable_intr();
298	INTREN(IRQ_SLAVE);
299	printf("Probing for devices on the ISA bus:\n");
300	/* First probe all the sensitive probes */
301	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
302		if (dvp->id_driver->sensitive_hw)
303			config_isadev(dvp, &tty_imask);
304	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
305		if (dvp->id_driver->sensitive_hw)
306			config_isadev(dvp, &bio_imask);
307	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
308		if (dvp->id_driver->sensitive_hw)
309			config_isadev(dvp, &net_imask);
310	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
311		if (dvp->id_driver->sensitive_hw)
312			config_isadev(dvp, (u_int *)NULL);
313
314	/* Then all the bad ones */
315	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
316		if (!dvp->id_driver->sensitive_hw)
317			config_isadev(dvp, &tty_imask);
318	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
319		if (!dvp->id_driver->sensitive_hw)
320			config_isadev(dvp, &bio_imask);
321	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
322		if (!dvp->id_driver->sensitive_hw)
323			config_isadev(dvp, &net_imask);
324	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
325		if (!dvp->id_driver->sensitive_hw)
326			config_isadev(dvp, (u_int *)NULL);
327
328	bio_imask |= SWI_CLOCK_MASK;
329	net_imask |= SWI_NET_MASK;
330	tty_imask |= SWI_TTY_MASK;
331
332/*
333 * XXX we should really add the tty device to net_imask when the line is
334 * switched to SLIPDISC, and then remove it when it is switched away from
335 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
336 * of them is running slip.
337 *
338 * XXX actually, blocking all ttys during a splimp doesn't matter so much
339 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
340 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
341 * during spltty.
342 */
343#include "sl.h"
344#include "ppp.h"
345
346#if (NSL > 0)
347	net_imask |= tty_imask;
348	tty_imask = net_imask;
349#endif
350
351	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
352
353	if (bootverbose)
354		printf("imasks: bio %x, tty %x, net %x\n",
355			bio_imask, tty_imask, net_imask);
356
357	/*
358	 * Finish initializing intr_mask[].  Note that the partly
359	 * constructed masks aren't actually used since we're at splhigh.
360	 * For fully dynamic initialization, register_intr() and
361	 * unregister_intr() will have to adjust the masks for _all_
362	 * interrupts and for tty_imask, etc.
363	 */
364	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
365		register_imask(dvp, tty_imask);
366	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
367		register_imask(dvp, bio_imask);
368	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
369		register_imask(dvp, net_imask);
370	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
371		register_imask(dvp, SWI_CLOCK_MASK);
372	spl0();
373}
374
375/*
376 * Configure an ISA device.
377 */
378
379
380static void
381config_isadev(isdp, mp)
382     struct isa_device *isdp;
383     u_int *mp;
384{
385	config_isadev_c(isdp, mp, 0);
386}
387
388void
389reconfig_isadev(isdp, mp)
390	struct isa_device *isdp;
391	u_int *mp;
392{
393	config_isadev_c(isdp, mp, 1);
394}
395
396static void
397config_isadev_c(isdp, mp, reconfig)
398	struct isa_device *isdp;
399	u_int *mp;
400	int reconfig;
401{
402	u_int checkbits;
403	int id_alive;
404	int last_alive;
405	struct isa_driver *dp = isdp->id_driver;
406
407 	checkbits = 0;
408	checkbits |= CC_DRQ;
409	checkbits |= CC_IOADDR;
410	checkbits |= CC_MEMADDR;
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			checkbits |= CC_IRQ;
478			if (haveseen_isadev(isdp, checkbits))
479				return;
480			isdp->id_alive = id_alive;
481		}
482		(*dp->attach)(isdp);
483		if (isdp->id_irq) {
484			if (mp)
485				INTRMASK(*mp, isdp->id_irq);
486			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
487				      isdp->id_ri_flags, isdp->id_intr,
488				      mp, isdp->id_unit);
489			INTREN(isdp->id_irq);
490		}
491	} else {
492		if (isdp->id_reconfig) {
493			(*dp->attach)(isdp); /* reconfiguration attach */
494		}
495		if (!last_alive) {
496			if (!isdp->id_reconfig) {
497				printf("%s%d not found", dp->name, isdp->id_unit);
498				if (isdp->id_iobase) {
499					printf(" at 0x%x", isdp->id_iobase);
500				}
501				printf("\n");
502			}
503		}
504		else {
505			/* This code has not been tested.... */
506			if (isdp->id_irq) {
507				INTRDIS(isdp->id_irq);
508				unregister_intr(ffs(isdp->id_irq) - 1,
509						isdp->id_intr);
510				if (mp)
511					INTRUNMASK(*mp, isdp->id_irq);
512			}
513		}
514	}
515}
516
517/*
518 * Provide ISA-specific device information to user programs using the
519 * hw.devconf interface.
520 */
521int
522isa_externalize(struct isa_device *id, void *userp, size_t *maxlen)
523{
524	if(*maxlen < sizeof *id) {
525		return ENOMEM;
526	}
527
528	*maxlen -= sizeof *id;
529	return copyout(id, userp, sizeof *id);
530}
531
532/*
533 * Do the same thing for EISA information.  EISA information is currently
534 * the same as ISA information plus a slot number, but could be extended in
535 * the future.
536 */
537int
538eisa_externalize(struct isa_device *id, int slot, void *userp, size_t *maxlen)
539{
540	int rv;
541
542	if(*maxlen < (sizeof *id) + (sizeof slot)) {
543		return ENOMEM;
544	}
545	*maxlen -= (sizeof *id) + (sizeof slot);
546
547	rv = copyout(id, userp, sizeof *id);
548	if(rv) return rv;
549
550	return copyout(&slot, (char *)userp + sizeof *id, sizeof slot);
551}
552
553/*
554 * This is used to forcibly reconfigure an ISA device.  It currently just
555 * returns an error 'cos you can't do that yet.  It is here to demonstrate
556 * what the `internalize' routine is supposed to do.
557 */
558int
559isa_internalize(struct isa_device *id, void **userpp, size_t *len)
560{
561	struct isa_device myid;
562	char *userp = *userpp;
563	int rv;
564
565	if(*len < sizeof *id) {
566		return EINVAL;
567	}
568
569	rv = copyin(userp, &myid, sizeof myid);
570	if(rv) return rv;
571	*userpp = userp + sizeof myid;
572	*len -= sizeof myid;
573
574	rv = EOPNOTSUPP;
575	/* code would go here to validate the configuration request */
576	/* code would go here to actually perform the reconfiguration */
577	return rv;
578}
579
580int
581isa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
582			void *userp, size_t l)
583{
584	return isa_externalize(kdc->kdc_isa, userp, &l);
585}
586
587int
588eisa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
589			 void *userp, size_t l)
590{
591	return eisa_externalize(kdc->kdc_isa, -1, userp, &l);
592}
593
594/*
595 * Fill in default interrupt table (in case of spuruious interrupt
596 * during configuration of kernel, setup interrupt control unit
597 */
598void
599isa_defaultirq()
600{
601	int i;
602
603	/* icu vectors */
604	for (i = 0; i < ICU_LEN; i++)
605		unregister_intr(i, (inthand2_t *)NULL);
606
607	/* initialize 8259's */
608	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
609	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
610	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
611#ifdef AUTO_EOI_1
612	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
613#else
614	outb(IO_ICU1+1, 1);		/* 8086 mode */
615#endif
616	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
617	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
618	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
619
620	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
621	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
622	outb(IO_ICU2+1,2);		/* my slave id is 2 */
623#ifdef AUTO_EOI_2
624	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
625#else
626	outb(IO_ICU2+1,1);		/* 8086 mode */
627#endif
628	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
629	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
630}
631
632/* region of physical memory known to be contiguous */
633vm_offset_t isaphysmem;
634static caddr_t dma_bounce[8];		/* XXX */
635static char bounced[8];		/* XXX */
636#define MAXDMASZ 512		/* XXX */
637
638/* high byte of address is stored in this port for i-th dma channel */
639static short dmapageport[8] =
640	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
641
642/*
643 * isa_dmacascade(): program 8237 DMA controller channel to accept
644 * external dma control by a board.
645 */
646void isa_dmacascade(unsigned chan)
647{
648	if (chan > 7)
649		panic("isa_dmacascade: impossible request");
650
651	/* set dma channel mode, and set dma channel mode */
652	if ((chan & 4) == 0) {
653		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
654		outb(DMA1_SMSK, chan);
655	} else {
656		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
657		outb(DMA2_SMSK, chan & 3);
658	}
659}
660
661static int
662isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
663
664/*
665 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
666 * problems by using a bounce buffer.
667 */
668void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
669{	vm_offset_t phys;
670	int waport;
671	caddr_t newaddr;
672
673	if (    chan > 7
674	    || (chan < 4 && nbytes > (1<<16))
675	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
676		panic("isa_dmastart: impossible request");
677
678	if (isa_dmarangecheck(addr, nbytes, chan)) {
679		if (dma_bounce[chan] == 0)
680			dma_bounce[chan] =
681				(caddr_t) isaphysmem + NBPG*chan;
682		bounced[chan] = 1;
683		newaddr = dma_bounce[chan];
684		*(int *) newaddr = 0;	/* XXX */
685
686		/* copy bounce buffer on write */
687		if (!(flags & B_READ))
688			bcopy(addr, newaddr, nbytes);
689		addr = newaddr;
690	}
691
692	/* translate to physical */
693	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
694
695	if ((chan & 4) == 0) {
696		/*
697		 * Program one of DMA channels 0..3.  These are
698		 * byte mode channels.
699		 */
700		/* set dma channel mode, and reset address ff */
701
702		/* If B_RAW flag is set, then use autoinitialise mode */
703		if (flags & B_RAW) {
704		  if (flags & B_READ)
705			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
706		  else
707			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
708		}
709		else
710		if (flags & B_READ)
711			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
712		else
713			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
714		outb(DMA1_FFC, 0);
715
716		/* send start address */
717		waport =  DMA1_CHN(chan);
718		outb(waport, phys);
719		outb(waport, phys>>8);
720		outb(dmapageport[chan], phys>>16);
721
722		/* send count */
723		outb(waport + 1, --nbytes);
724		outb(waport + 1, nbytes>>8);
725
726		/* unmask channel */
727		outb(DMA1_SMSK, chan);
728	} else {
729		/*
730		 * Program one of DMA channels 4..7.  These are
731		 * word mode channels.
732		 */
733		/* set dma channel mode, and reset address ff */
734
735		/* If B_RAW flag is set, then use autoinitialise mode */
736		if (flags & B_RAW) {
737		  if (flags & B_READ)
738			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
739		  else
740			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
741		}
742		else
743		if (flags & B_READ)
744			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
745		else
746			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
747		outb(DMA2_FFC, 0);
748
749		/* send start address */
750		waport = DMA2_CHN(chan - 4);
751		outb(waport, phys>>1);
752		outb(waport, phys>>9);
753		outb(dmapageport[chan], phys>>16);
754
755		/* send count */
756		nbytes >>= 1;
757		outb(waport + 2, --nbytes);
758		outb(waport + 2, nbytes>>8);
759
760		/* unmask channel */
761		outb(DMA2_SMSK, chan & 3);
762	}
763}
764
765void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
766{
767
768	/* copy bounce buffer on read */
769	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
770	if (bounced[chan]) {
771		bcopy(dma_bounce[chan], addr, nbytes);
772		bounced[chan] = 0;
773	}
774}
775
776/*
777 * Check for problems with the address range of a DMA transfer
778 * (non-contiguous physical pages, outside of bus address space,
779 * crossing DMA page boundaries).
780 * Return true if special handling needed.
781 */
782
783static int
784isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
785	vm_offset_t phys, priorpage = 0, endva;
786	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
787
788	endva = (vm_offset_t)round_page(va + length);
789	for (; va < (caddr_t) endva ; va += NBPG) {
790		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
791#define ISARAM_END	RAM_END
792		if (phys == 0)
793			panic("isa_dmacheck: no physical page present");
794		if (phys >= ISARAM_END)
795			return (1);
796		if (priorpage) {
797			if (priorpage + NBPG != phys)
798				return (1);
799			/* check if crossing a DMA page boundary */
800			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
801				return (1);
802		}
803		priorpage = phys;
804	}
805	return (0);
806}
807
808#define NMI_PARITY (1 << 7)
809#define NMI_IOCHAN (1 << 6)
810#define ENMI_WATCHDOG (1 << 7)
811#define ENMI_BUSTIMER (1 << 6)
812#define ENMI_IOSTATUS (1 << 5)
813
814/*
815 * Handle a NMI, possibly a machine check.
816 * return true to panic system, false to ignore.
817 */
818int
819isa_nmi(cd)
820	int cd;
821{
822	int isa_port = inb(0x61);
823	int eisa_port = inb(0x461);
824	if(isa_port & NMI_PARITY) {
825		panic("RAM parity error, likely hardware failure.");
826	} else if(isa_port & NMI_IOCHAN) {
827		panic("I/O channel check, likely hardware failure.");
828	} else if(eisa_port & ENMI_WATCHDOG) {
829		panic("EISA watchdog timer expired, likely hardware failure.");
830	} else if(eisa_port & ENMI_BUSTIMER) {
831		panic("EISA bus timeout, likely hardware failure.");
832	} else if(eisa_port & ENMI_IOSTATUS) {
833		panic("EISA I/O port status error.");
834	} else {
835		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
836		return(0);
837	}
838}
839
840/*
841 * Caught a stray interrupt, notify
842 */
843static void
844isa_strayintr(d)
845	int d;
846{
847
848	/* DON'T BOTHER FOR NOW! */
849	/* for some reason, we get bursts of intr #7, even if not enabled! */
850	/*
851	 * Well the reason you got bursts of intr #7 is because someone
852	 * raised an interrupt line and dropped it before the 8259 could
853	 * prioritize it.  This is documented in the intel data book.  This
854	 * means you have BAD hardware!  I have changed this so that only
855	 * the first 5 get logged, then it quits logging them, and puts
856	 * out a special message. rgrimes 3/25/1993
857	 */
858	/*
859	 * XXX TODO print a different message for #7 if it is for a
860	 * glitch.  Glitches can be distinguished from real #7's by
861	 * testing that the in-service bit is _not_ set.  The test
862	 * must be done before sending an EOI so it can't be done if
863	 * we are using AUTO_EOI_1.
864	 */
865	if (intrcnt[NR_DEVICES + d] <= 5)
866		log(LOG_ERR, "stray irq %d\n", d);
867	if (intrcnt[NR_DEVICES + d] == 5)
868		log(LOG_CRIT,
869		    "too many stray irq %d's; not logging any more\n", d);
870}
871
872/*
873 * Find the highest priority enabled display device.  Since we can't
874 * distinguish display devices from ttys, depend on display devices
875 * being sensitive and before sensitive non-display devices (if any)
876 * in isa_devtab_tty.
877 *
878 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
879 */
880struct isa_device *
881find_display()
882{
883	struct isa_device *dvp;
884
885	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
886		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
887			return (dvp);
888	return (NULL);
889}
890
891/*
892 * find an ISA device in a given isa_devtab_* table, given
893 * the table to search, the expected id_driver entry, and the unit number.
894 *
895 * this function is defined in isa_device.h, and this location is debatable;
896 * i put it there because it's useless w/o, and directly operates on
897 * the other stuff in that file.
898 *
899 */
900
901struct isa_device *find_isadev(table, driverp, unit)
902     struct isa_device *table;
903     struct isa_driver *driverp;
904     int unit;
905{
906  if (driverp == NULL) /* sanity check */
907    return NULL;
908
909  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
910    if (table->id_driver == 0)
911      return NULL;
912
913    table++;
914  }
915
916  return table;
917}
918
919/*
920 * Return nonzero if a (masked) irq is pending for a given device.
921 */
922int
923isa_irq_pending(dvp)
924	struct isa_device *dvp;
925{
926	unsigned id_irq;
927
928	id_irq = dvp->id_irq;
929	if (id_irq & 0xff)
930		return (inb(IO_ICU1) & id_irq);
931	return (inb(IO_ICU2) & (id_irq >> 8));
932}
933
934int
935update_intr_masks(void)
936{
937	int intr, n=0;
938	u_int mask,*maskptr;
939
940	for (intr=0; intr < ICU_LEN; intr ++) {
941		if (intr==2) continue;
942		maskptr = intr_mptr[intr];
943		if (!maskptr) continue;
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);
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	write_eflags(ef);
1035	return (0);
1036}
1037