isa.c revision 8488
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.48 1995/05/11 05:20:43 jkh 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 * XXX these defines should be in a central place.
90 */
91#define	read_eflags()		({u_long ef; \
92				  __asm("pushfl; popl %0" : "=a" (ef)); \
93				  ef; })
94#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
95
96u_long	*intr_countp[ICU_LEN];
97inthand2_t *intr_handler[ICU_LEN];
98u_int	intr_mask[ICU_LEN];
99u_int*	intr_mptr[ICU_LEN];
100int	intr_unit[ICU_LEN];
101
102extern struct kern_devconf kdc_cpu0;
103
104struct kern_devconf kdc_isa0 = {
105	0, 0, 0,		/* filled in by dev_attach */
106	"isa", 0, { MDDT_BUS, 0 },
107	0, 0, 0, BUS_EXTERNALLEN,
108	&kdc_cpu0,		/* parent is the CPU */
109	0,			/* no parentdata */
110	DC_BUSY,		/* busses are always busy */
111	"ISA or EISA bus",
112	DC_CLS_BUS		/* class */
113};
114
115static inthand_t *fastintr[ICU_LEN] = {
116	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
117	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
118	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
119	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
120	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
121	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
122	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
123	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
124};
125
126static inthand_t *slowintr[ICU_LEN] = {
127	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
128	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
129	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
130	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
131};
132
133static void config_isadev __P((struct isa_device *isdp, u_int *mp));
134static void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
135				 int reconfig));
136static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
137			  int item, char const *whatnot, char const *reason,
138			  char const *format));
139static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
140			 u_int checkbits));
141static inthand2_t isa_strayintr;
142static void register_imask __P((struct isa_device *dvp, u_int mask));
143
144/*
145 * print a conflict message
146 */
147static void
148conflict(dvp, tmpdvp, item, whatnot, reason, format)
149	struct isa_device	*dvp;
150	struct isa_device	*tmpdvp;
151	int			item;
152	char const		*whatnot;
153	char const		*reason;
154	char const		*format;
155{
156	printf("%s%d not %sed due to %s conflict with %s%d at ",
157		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
158		tmpdvp->id_driver->name, tmpdvp->id_unit);
159	printf(format, item);
160	printf("\n");
161}
162
163/*
164 * Check to see if things are already in use, like IRQ's, I/O addresses
165 * and Memory addresses.
166 */
167static int
168haveseen(dvp, tmpdvp, checkbits)
169	struct isa_device *dvp;
170	struct isa_device *tmpdvp;
171	u_int	checkbits;
172{
173	/*
174	 * Only check against devices that have already been found and are not
175	 * unilaterally allowed to conflict anyway.
176	 */
177	if (tmpdvp->id_alive && !dvp->id_conflicts) {
178		char const *whatnot;
179
180		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
181		/*
182		 * Check for I/O address conflict.  We can only check the
183		 * starting address of the device against the range of the
184		 * device that has already been probed since we do not
185		 * know how many I/O addresses this device uses.
186		 */
187		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
188			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
189			    (dvp->id_iobase <=
190				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
191				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
192					 "I/O address", "0x%x");
193				return 1;
194			}
195		}
196		/*
197		 * Check for Memory address conflict.  We can check for
198		 * range overlap, but it will not catch all cases since the
199		 * driver may adjust the msize paramater during probe, for
200		 * now we just check that the starting address does not
201		 * fall within any allocated region.
202		 * XXX could add a second check after the probe for overlap,
203		 * since at that time we would know the full range.
204		 * XXX KERNBASE is a hack, we should have vaddr in the table!
205		 */
206		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
207			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
208			    (KERNBASE + dvp->id_maddr <=
209			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
210				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
211					 whatnot, "maddr", "0x%x");
212				return 1;
213			}
214		}
215		/*
216		 * Check for IRQ conflicts.
217		 */
218		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
219			if (tmpdvp->id_irq == dvp->id_irq) {
220				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
221					 whatnot, "irq", "%d");
222				return 1;
223			}
224		}
225		/*
226		 * Check for DRQ conflicts.
227		 */
228		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
229			if (tmpdvp->id_drq == dvp->id_drq) {
230				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
231					 "drq", "%d");
232				return 1;
233			}
234		}
235	}
236	return 0;
237}
238
239/*
240 * Search through all the isa_devtab_* tables looking for anything that
241 * conflicts with the current device.
242 */
243#include "eisa.h"
244#if NEISA > 0
245extern struct isa_device isa_devtab_eisa[];
246#endif
247
248int
249haveseen_isadev(dvp, checkbits)
250	struct isa_device *dvp;
251	u_int	checkbits;
252{
253	struct isa_device *tmpdvp;
254	int	status = 0;
255
256	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
257		status |= haveseen(dvp, tmpdvp, checkbits);
258		if (status)
259			return status;
260	}
261	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
262		status |= haveseen(dvp, tmpdvp, checkbits);
263		if (status)
264			return status;
265	}
266	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
267		status |= haveseen(dvp, tmpdvp, checkbits);
268		if (status)
269			return status;
270	}
271	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
272		status |= haveseen(dvp, tmpdvp, checkbits);
273		if (status)
274			return status;
275	}
276#if NEISA > 0
277	for (tmpdvp = isa_devtab_eisa; tmpdvp->id_driver; tmpdvp++) {
278		status |= haveseen(dvp, tmpdvp, checkbits);
279		if (status)
280			return status;
281	}
282#endif
283	return(status);
284}
285
286/*
287 * Configure all ISA devices
288 */
289void
290isa_configure() {
291	struct isa_device *dvp;
292
293	dev_attach(&kdc_isa0);
294
295	splhigh();
296	enable_intr();
297	INTREN(IRQ_SLAVE);
298	printf("Probing for devices on the ISA bus:\n");
299	/* First probe all the sensitive probes */
300	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
301		if (dvp->id_driver->sensitive_hw)
302			config_isadev(dvp, &tty_imask);
303	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
304		if (dvp->id_driver->sensitive_hw)
305			config_isadev(dvp, &bio_imask);
306	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
307		if (dvp->id_driver->sensitive_hw)
308			config_isadev(dvp, &net_imask);
309	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
310		if (dvp->id_driver->sensitive_hw)
311			config_isadev(dvp, (u_int *)NULL);
312
313	/* Then all the bad ones */
314	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
315		if (!dvp->id_driver->sensitive_hw)
316			config_isadev(dvp, &tty_imask);
317	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
318		if (!dvp->id_driver->sensitive_hw)
319			config_isadev(dvp, &bio_imask);
320	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
321		if (!dvp->id_driver->sensitive_hw)
322			config_isadev(dvp, &net_imask);
323	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
324		if (!dvp->id_driver->sensitive_hw)
325			config_isadev(dvp, (u_int *)NULL);
326
327	bio_imask |= SWI_CLOCK_MASK;
328	net_imask |= SWI_NET_MASK;
329	tty_imask |= SWI_TTY_MASK;
330
331/*
332 * XXX we should really add the tty device to net_imask when the line is
333 * switched to SLIPDISC, and then remove it when it is switched away from
334 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
335 * of them is running slip.
336 *
337 * XXX actually, blocking all ttys during a splimp doesn't matter so much
338 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
339 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
340 * during spltty.
341 */
342#include "sl.h"
343#include "ppp.h"
344
345#if (NSL > 0) || (NPPP > 0)
346	net_imask |= tty_imask;
347	tty_imask = net_imask;
348#endif
349	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
350#ifdef DIAGNOSTIC
351	printf("bio_imask %x tty_imask %x net_imask %x\n",
352	       bio_imask, tty_imask, net_imask);
353#endif
354	/*
355	 * Finish initializing intr_mask[].  Note that the partly
356	 * constructed masks aren't actually used since we're at splhigh.
357	 * For fully dynamic initialization, register_intr() and
358	 * unregister_intr() will have to adjust the masks for _all_
359	 * interrupts and for tty_imask, etc.
360	 */
361	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
362		register_imask(dvp, tty_imask);
363	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
364		register_imask(dvp, bio_imask);
365	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
366		register_imask(dvp, net_imask);
367	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
368		register_imask(dvp, SWI_CLOCK_MASK);
369	spl0();
370}
371
372/*
373 * Configure an ISA device.
374 */
375
376
377static void
378config_isadev(isdp, mp)
379     struct isa_device *isdp;
380     u_int *mp;
381{
382	config_isadev_c(isdp, mp, 0);
383}
384
385void
386reconfig_isadev(isdp, mp)
387	struct isa_device *isdp;
388	u_int *mp;
389{
390	config_isadev_c(isdp, mp, 1);
391}
392
393static void
394config_isadev_c(isdp, mp, reconfig)
395	struct isa_device *isdp;
396	u_int *mp;
397	int reconfig;
398{
399	u_int checkbits;
400	int id_alive;
401	int last_alive;
402	struct isa_driver *dp = isdp->id_driver;
403
404 	checkbits = 0;
405	checkbits |= CC_DRQ;
406	checkbits |= CC_IOADDR;
407	checkbits |= CC_MEMADDR;
408	if (!isdp->id_enabled) {
409		printf("%s%d: disabled, not probed.\n",
410			dp->name, isdp->id_unit);
411		return;
412	}
413	if (!reconfig && haveseen_isadev(isdp, checkbits))
414		return;
415	if (!reconfig && isdp->id_maddr) {
416		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
417		isdp->id_maddr += atdevbase;
418	}
419	if (reconfig) {
420		last_alive = isdp->id_alive;
421		isdp->id_reconfig = 1;
422	}
423	else {
424		last_alive = 0;
425		isdp->id_reconfig = 0;
426	}
427	id_alive = (*dp->probe)(isdp);
428	if (id_alive) {
429		/*
430		 * Only print the I/O address range if id_alive != -1
431		 * Right now this is a temporary fix just for the new
432		 * NPX code so that if it finds a 486 that can use trap
433		 * 16 it will not report I/O addresses.
434		 * Rod Grimes 04/26/94
435		 */
436		if (!isdp->id_reconfig) {
437			printf("%s%d", dp->name, isdp->id_unit);
438			if (id_alive != -1) {
439 				printf(" at 0x%x", isdp->id_iobase);
440 				if ((isdp->id_iobase + id_alive - 1) !=
441 				     isdp->id_iobase) {
442 					printf("-0x%x",
443					       isdp->id_iobase + id_alive - 1);
444				}
445			}
446			if (isdp->id_irq)
447				printf(" irq %d", ffs(isdp->id_irq) - 1);
448			if (isdp->id_drq != -1)
449				printf(" drq %d", isdp->id_drq);
450			if (isdp->id_maddr)
451				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
452			if (isdp->id_msize)
453				printf(" msize %d", isdp->id_msize);
454			if (isdp->id_flags)
455				printf(" flags 0x%x", isdp->id_flags);
456			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
457				printf(" on motherboard");
458			} else if (isdp->id_iobase >= 0x1000 &&
459				    !(isdp->id_iobase & 0x300)) {
460				printf (" on eisa slot %d",
461					isdp->id_iobase >> 12);
462			} else {
463				printf (" on isa");
464			}
465			printf("\n");
466			/*
467			 * Check for conflicts again.  The driver may have
468			 * changed *dvp.  We should weaken the early check
469			 * since the driver may have been able to change
470			 * *dvp to avoid conflicts if given a chance.  We
471			 * already skip the early check for IRQs and force
472			 * a check for IRQs in the next group of checks.
473		 	 */
474			checkbits |= CC_IRQ;
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
956update_intr_masks(void)
957{
958	int intr, n=0;
959	u_int mask,*maskptr;
960	for (intr=0; intr < ICU_LEN; intr ++) {
961		if (intr==2) continue;
962		maskptr = intr_mptr[intr];
963		if (!maskptr) continue;
964		mask = *maskptr;
965		if (mask != intr_mask[intr]) {
966#if 0
967			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
968				intr, intr_mask[intr], mask, maskptr);
969#endif
970			intr_mask[intr]=mask;
971			n++;
972		}
973
974	}
975	return (n);
976}
977
978int
979register_intr(intr, device_id, flags, handler, maskptr, unit)
980	int	intr;
981	int	device_id;
982	u_int	flags;
983	inthand2_t *handler;
984	u_int	*maskptr;
985	int	unit;
986{
987	char	*cp;
988	u_long	ef;
989	int	id;
990	u_int	mask = (maskptr ? *maskptr : 0);
991
992	if ((u_int)intr >= ICU_LEN || intr == 2
993	    || (u_int)device_id >= NR_DEVICES)
994		return (EINVAL);
995	if (intr_handler[intr] != isa_strayintr)
996		return (EBUSY);
997	ef = read_eflags();
998	disable_intr();
999	intr_countp[intr] = &intrcnt[device_id];
1000	intr_handler[intr] = handler;
1001	intr_mptr[intr] = maskptr;
1002	intr_mask[intr] = mask | (1 << intr);
1003	intr_unit[intr] = unit;
1004	setidt(ICU_OFFSET + intr,
1005	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
1006	       SDT_SYS386IGT, SEL_KPL);
1007	write_eflags(ef);
1008	for (cp = intrnames, id = 0; id <= device_id; id++)
1009		while (*cp++ != '\0')
1010			;
1011	if (cp > eintrnames)
1012		return (0);
1013	if (intr < 10) {
1014		cp[-3] = intr + '0';
1015		cp[-2] = ' ';
1016	} else {
1017		cp[-3] = '1';
1018		cp[-2] = intr - 10 + '0';
1019	}
1020	return (0);
1021}
1022
1023static void
1024register_imask(dvp, mask)
1025	struct isa_device *dvp;
1026	u_int	mask;
1027{
1028	if (dvp->id_alive && dvp->id_irq) {
1029		int	intr;
1030
1031		intr = ffs(dvp->id_irq) - 1;
1032		intr_mask[intr] = mask | (1 <<intr);
1033	}
1034	(void) update_intr_masks();
1035}
1036
1037int
1038unregister_intr(intr, handler)
1039	int	intr;
1040	inthand2_t *handler;
1041{
1042	u_long	ef;
1043
1044	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1045		return (EINVAL);
1046	ef = read_eflags();
1047	disable_intr();
1048	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1049	intr_handler[intr] = isa_strayintr;
1050	intr_mptr[intr] = NULL;
1051	intr_mask[intr] = HWI_MASK | SWI_MASK;
1052	intr_unit[intr] = intr;
1053	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
1054	write_eflags(ef);
1055	return (0);
1056}
1057