isa.c revision 12092
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.54 1995/10/31 21:03:57 peter 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
245int
246haveseen_isadev(dvp, checkbits)
247	struct isa_device *dvp;
248	u_int	checkbits;
249{
250	struct isa_device *tmpdvp;
251	int	status = 0;
252
253	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
254		status |= haveseen(dvp, tmpdvp, checkbits);
255		if (status)
256			return status;
257	}
258	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
259		status |= haveseen(dvp, tmpdvp, checkbits);
260		if (status)
261			return status;
262	}
263	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
264		status |= haveseen(dvp, tmpdvp, checkbits);
265		if (status)
266			return status;
267	}
268	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
269		status |= haveseen(dvp, tmpdvp, checkbits);
270		if (status)
271			return status;
272	}
273	return(status);
274}
275
276/*
277 * Configure all ISA devices
278 */
279void
280isa_configure() {
281	struct isa_device *dvp;
282
283	dev_attach(&kdc_isa0);
284
285	splhigh();
286	enable_intr();
287	INTREN(IRQ_SLAVE);
288	printf("Probing for devices on the ISA bus:\n");
289	/* First probe all the sensitive probes */
290	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
291		if (dvp->id_driver->sensitive_hw)
292			config_isadev(dvp, &tty_imask);
293	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
294		if (dvp->id_driver->sensitive_hw)
295			config_isadev(dvp, &bio_imask);
296	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
297		if (dvp->id_driver->sensitive_hw)
298			config_isadev(dvp, &net_imask);
299	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
300		if (dvp->id_driver->sensitive_hw)
301			config_isadev(dvp, (u_int *)NULL);
302
303	/* Then all the bad ones */
304	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
305		if (!dvp->id_driver->sensitive_hw)
306			config_isadev(dvp, &tty_imask);
307	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
308		if (!dvp->id_driver->sensitive_hw)
309			config_isadev(dvp, &bio_imask);
310	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
311		if (!dvp->id_driver->sensitive_hw)
312			config_isadev(dvp, &net_imask);
313	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
314		if (!dvp->id_driver->sensitive_hw)
315			config_isadev(dvp, (u_int *)NULL);
316
317	bio_imask |= SWI_CLOCK_MASK;
318	net_imask |= SWI_NET_MASK;
319	tty_imask |= SWI_TTY_MASK;
320
321/*
322 * XXX we should really add the tty device to net_imask when the line is
323 * switched to SLIPDISC, and then remove it when it is switched away from
324 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
325 * of them is running slip.
326 *
327 * XXX actually, blocking all ttys during a splimp doesn't matter so much
328 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
329 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
330 * during spltty.
331 */
332#include "sl.h"
333#include "ppp.h"
334
335#if (NSL > 0)
336	net_imask |= tty_imask;
337	tty_imask = net_imask;
338#endif
339
340	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
341
342	if (bootverbose)
343		printf("imasks: bio %x, tty %x, net %x\n",
344			bio_imask, tty_imask, net_imask);
345
346	/*
347	 * Finish initializing intr_mask[].  Note that the partly
348	 * constructed masks aren't actually used since we're at splhigh.
349	 * For fully dynamic initialization, register_intr() and
350	 * unregister_intr() will have to adjust the masks for _all_
351	 * interrupts and for tty_imask, etc.
352	 */
353	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
354		register_imask(dvp, tty_imask);
355	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
356		register_imask(dvp, bio_imask);
357	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
358		register_imask(dvp, net_imask);
359	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
360		register_imask(dvp, SWI_CLOCK_MASK);
361	spl0();
362}
363
364/*
365 * Configure an ISA device.
366 */
367
368
369static void
370config_isadev(isdp, mp)
371     struct isa_device *isdp;
372     u_int *mp;
373{
374	config_isadev_c(isdp, mp, 0);
375}
376
377void
378reconfig_isadev(isdp, mp)
379	struct isa_device *isdp;
380	u_int *mp;
381{
382	config_isadev_c(isdp, mp, 1);
383}
384
385static void
386config_isadev_c(isdp, mp, reconfig)
387	struct isa_device *isdp;
388	u_int *mp;
389	int reconfig;
390{
391	u_int checkbits;
392	int id_alive;
393	int last_alive;
394	struct isa_driver *dp = isdp->id_driver;
395
396 	checkbits = 0;
397	checkbits |= CC_DRQ;
398	checkbits |= CC_IOADDR;
399	checkbits |= CC_MEMADDR;
400	if (!isdp->id_enabled) {
401		printf("%s%d: disabled, not probed.\n",
402			dp->name, isdp->id_unit);
403		return;
404	}
405	if (!reconfig && haveseen_isadev(isdp, checkbits))
406		return;
407	if (!reconfig && isdp->id_maddr) {
408		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
409		isdp->id_maddr += atdevbase;
410	}
411	if (reconfig) {
412		last_alive = isdp->id_alive;
413		isdp->id_reconfig = 1;
414	}
415	else {
416		last_alive = 0;
417		isdp->id_reconfig = 0;
418	}
419	id_alive = (*dp->probe)(isdp);
420	if (id_alive) {
421		/*
422		 * Only print the I/O address range if id_alive != -1
423		 * Right now this is a temporary fix just for the new
424		 * NPX code so that if it finds a 486 that can use trap
425		 * 16 it will not report I/O addresses.
426		 * Rod Grimes 04/26/94
427		 */
428		if (!isdp->id_reconfig) {
429			printf("%s%d", dp->name, isdp->id_unit);
430			if (id_alive != -1) {
431 				printf(" at 0x%x", isdp->id_iobase);
432 				if ((isdp->id_iobase + id_alive - 1) !=
433 				     isdp->id_iobase) {
434 					printf("-0x%x",
435					       isdp->id_iobase + id_alive - 1);
436				}
437			}
438			if (isdp->id_irq)
439				printf(" irq %d", ffs(isdp->id_irq) - 1);
440			if (isdp->id_drq != -1)
441				printf(" drq %d", isdp->id_drq);
442			if (isdp->id_maddr)
443				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
444			if (isdp->id_msize)
445				printf(" msize %d", isdp->id_msize);
446			if (isdp->id_flags)
447				printf(" flags 0x%x", isdp->id_flags);
448			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
449				printf(" on motherboard");
450			} else if (isdp->id_iobase >= 0x1000 &&
451				    !(isdp->id_iobase & 0x300)) {
452				printf (" on eisa slot %d",
453					isdp->id_iobase >> 12);
454			} else {
455				printf (" on isa");
456			}
457			printf("\n");
458			/*
459			 * Check for conflicts again.  The driver may have
460			 * changed *dvp.  We should weaken the early check
461			 * since the driver may have been able to change
462			 * *dvp to avoid conflicts if given a chance.  We
463			 * already skip the early check for IRQs and force
464			 * a check for IRQs in the next group of checks.
465		 	 */
466			checkbits |= CC_IRQ;
467			if (haveseen_isadev(isdp, checkbits))
468				return;
469			isdp->id_alive = id_alive;
470		}
471		(*dp->attach)(isdp);
472		if (isdp->id_irq) {
473			if (mp)
474				INTRMASK(*mp, isdp->id_irq);
475			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
476				      isdp->id_ri_flags, isdp->id_intr,
477				      mp, isdp->id_unit);
478			INTREN(isdp->id_irq);
479		}
480	} else {
481		if (isdp->id_reconfig) {
482			(*dp->attach)(isdp); /* reconfiguration attach */
483		}
484		if (!last_alive) {
485			if (!isdp->id_reconfig) {
486				printf("%s%d not found", dp->name, isdp->id_unit);
487				if (isdp->id_iobase) {
488					printf(" at 0x%x", isdp->id_iobase);
489				}
490				printf("\n");
491			}
492		}
493		else {
494			/* This code has not been tested.... */
495			if (isdp->id_irq) {
496				INTRDIS(isdp->id_irq);
497				unregister_intr(ffs(isdp->id_irq) - 1,
498						isdp->id_intr);
499				if (mp)
500					INTRUNMASK(*mp, isdp->id_irq);
501			}
502		}
503	}
504}
505
506/*
507 * Provide ISA-specific device information to user programs using the
508 * hw.devconf interface.
509 */
510int
511isa_externalize(struct isa_device *id, void *userp, size_t *maxlen)
512{
513	if(*maxlen < sizeof *id) {
514		return ENOMEM;
515	}
516
517	*maxlen -= sizeof *id;
518	return copyout(id, userp, sizeof *id);
519}
520
521/*
522 * This is used to forcibly reconfigure an ISA device.  It currently just
523 * returns an error 'cos you can't do that yet.  It is here to demonstrate
524 * what the `internalize' routine is supposed to do.
525 */
526int
527isa_internalize(struct isa_device *id, void **userpp, size_t *len)
528{
529	struct isa_device myid;
530	char *userp = *userpp;
531	int rv;
532
533	if(*len < sizeof *id) {
534		return EINVAL;
535	}
536
537	rv = copyin(userp, &myid, sizeof myid);
538	if(rv) return rv;
539	*userpp = userp + sizeof myid;
540	*len -= sizeof myid;
541
542	rv = EOPNOTSUPP;
543	/* code would go here to validate the configuration request */
544	/* code would go here to actually perform the reconfiguration */
545	return rv;
546}
547
548int
549isa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
550			void *userp, size_t l)
551{
552	return isa_externalize(kdc->kdc_isa, userp, &l);
553}
554
555/*
556 * Fill in default interrupt table (in case of spuruious interrupt
557 * during configuration of kernel, setup interrupt control unit
558 */
559void
560isa_defaultirq()
561{
562	int i;
563
564	/* icu vectors */
565	for (i = 0; i < ICU_LEN; i++)
566		unregister_intr(i, (inthand2_t *)NULL);
567
568	/* initialize 8259's */
569	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
570	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
571	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
572#ifdef AUTO_EOI_1
573	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
574#else
575	outb(IO_ICU1+1, 1);		/* 8086 mode */
576#endif
577	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
578	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
579	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
580
581	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
582	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
583	outb(IO_ICU2+1,2);		/* my slave id is 2 */
584#ifdef AUTO_EOI_2
585	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
586#else
587	outb(IO_ICU2+1,1);		/* 8086 mode */
588#endif
589	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
590	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
591}
592
593/* region of physical memory known to be contiguous */
594vm_offset_t isaphysmem;
595static caddr_t dma_bounce[8];		/* XXX */
596static char bounced[8];		/* XXX */
597#define MAXDMASZ 512		/* XXX */
598
599/* high byte of address is stored in this port for i-th dma channel */
600static short dmapageport[8] =
601	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
602
603/*
604 * isa_dmacascade(): program 8237 DMA controller channel to accept
605 * external dma control by a board.
606 */
607void isa_dmacascade(unsigned chan)
608{
609	if (chan > 7)
610		panic("isa_dmacascade: impossible request");
611
612	/* set dma channel mode, and set dma channel mode */
613	if ((chan & 4) == 0) {
614		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
615		outb(DMA1_SMSK, chan);
616	} else {
617		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
618		outb(DMA2_SMSK, chan & 3);
619	}
620}
621
622static int
623isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
624
625/*
626 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
627 * problems by using a bounce buffer.
628 */
629void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
630{	vm_offset_t phys;
631	int waport;
632	caddr_t newaddr;
633
634	if (    chan > 7
635	    || (chan < 4 && nbytes > (1<<16))
636	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
637		panic("isa_dmastart: impossible request");
638
639	if (isa_dmarangecheck(addr, nbytes, chan)) {
640		if (dma_bounce[chan] == 0)
641			dma_bounce[chan] =
642				(caddr_t) isaphysmem + NBPG*chan;
643		bounced[chan] = 1;
644		newaddr = dma_bounce[chan];
645		*(int *) newaddr = 0;	/* XXX */
646
647		/* copy bounce buffer on write */
648		if (!(flags & B_READ))
649			bcopy(addr, newaddr, nbytes);
650		addr = newaddr;
651	}
652
653	/* translate to physical */
654	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
655
656	if ((chan & 4) == 0) {
657		/*
658		 * Program one of DMA channels 0..3.  These are
659		 * byte mode channels.
660		 */
661		/* set dma channel mode, and reset address ff */
662
663		/* If B_RAW flag is set, then use autoinitialise mode */
664		if (flags & B_RAW) {
665		  if (flags & B_READ)
666			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
667		  else
668			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
669		}
670		else
671		if (flags & B_READ)
672			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
673		else
674			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
675		outb(DMA1_FFC, 0);
676
677		/* send start address */
678		waport =  DMA1_CHN(chan);
679		outb(waport, phys);
680		outb(waport, phys>>8);
681		outb(dmapageport[chan], phys>>16);
682
683		/* send count */
684		outb(waport + 1, --nbytes);
685		outb(waport + 1, nbytes>>8);
686
687		/* unmask channel */
688		outb(DMA1_SMSK, chan);
689	} else {
690		/*
691		 * Program one of DMA channels 4..7.  These are
692		 * word mode channels.
693		 */
694		/* set dma channel mode, and reset address ff */
695
696		/* If B_RAW flag is set, then use autoinitialise mode */
697		if (flags & B_RAW) {
698		  if (flags & B_READ)
699			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
700		  else
701			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
702		}
703		else
704		if (flags & B_READ)
705			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
706		else
707			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
708		outb(DMA2_FFC, 0);
709
710		/* send start address */
711		waport = DMA2_CHN(chan - 4);
712		outb(waport, phys>>1);
713		outb(waport, phys>>9);
714		outb(dmapageport[chan], phys>>16);
715
716		/* send count */
717		nbytes >>= 1;
718		outb(waport + 2, --nbytes);
719		outb(waport + 2, nbytes>>8);
720
721		/* unmask channel */
722		outb(DMA2_SMSK, chan & 3);
723	}
724}
725
726void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
727{
728
729	/* copy bounce buffer on read */
730	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
731	if (bounced[chan]) {
732		bcopy(dma_bounce[chan], addr, nbytes);
733		bounced[chan] = 0;
734	}
735}
736
737/*
738 * Check for problems with the address range of a DMA transfer
739 * (non-contiguous physical pages, outside of bus address space,
740 * crossing DMA page boundaries).
741 * Return true if special handling needed.
742 */
743
744static int
745isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
746	vm_offset_t phys, priorpage = 0, endva;
747	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
748
749	endva = (vm_offset_t)round_page(va + length);
750	for (; va < (caddr_t) endva ; va += NBPG) {
751		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
752#define ISARAM_END	RAM_END
753		if (phys == 0)
754			panic("isa_dmacheck: no physical page present");
755		if (phys >= ISARAM_END)
756			return (1);
757		if (priorpage) {
758			if (priorpage + NBPG != phys)
759				return (1);
760			/* check if crossing a DMA page boundary */
761			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
762				return (1);
763		}
764		priorpage = phys;
765	}
766	return (0);
767}
768
769#define NMI_PARITY (1 << 7)
770#define NMI_IOCHAN (1 << 6)
771#define ENMI_WATCHDOG (1 << 7)
772#define ENMI_BUSTIMER (1 << 6)
773#define ENMI_IOSTATUS (1 << 5)
774
775/*
776 * Handle a NMI, possibly a machine check.
777 * return true to panic system, false to ignore.
778 */
779int
780isa_nmi(cd)
781	int cd;
782{
783	int isa_port = inb(0x61);
784	int eisa_port = inb(0x461);
785	if(isa_port & NMI_PARITY) {
786		panic("RAM parity error, likely hardware failure.");
787	} else if(isa_port & NMI_IOCHAN) {
788		panic("I/O channel check, likely hardware failure.");
789	} else if(eisa_port & ENMI_WATCHDOG) {
790		panic("EISA watchdog timer expired, likely hardware failure.");
791	} else if(eisa_port & ENMI_BUSTIMER) {
792		panic("EISA bus timeout, likely hardware failure.");
793	} else if(eisa_port & ENMI_IOSTATUS) {
794		panic("EISA I/O port status error.");
795	} else {
796		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
797		return(0);
798	}
799}
800
801/*
802 * Caught a stray interrupt, notify
803 */
804static void
805isa_strayintr(d)
806	int d;
807{
808
809	/* DON'T BOTHER FOR NOW! */
810	/* for some reason, we get bursts of intr #7, even if not enabled! */
811	/*
812	 * Well the reason you got bursts of intr #7 is because someone
813	 * raised an interrupt line and dropped it before the 8259 could
814	 * prioritize it.  This is documented in the intel data book.  This
815	 * means you have BAD hardware!  I have changed this so that only
816	 * the first 5 get logged, then it quits logging them, and puts
817	 * out a special message. rgrimes 3/25/1993
818	 */
819	/*
820	 * XXX TODO print a different message for #7 if it is for a
821	 * glitch.  Glitches can be distinguished from real #7's by
822	 * testing that the in-service bit is _not_ set.  The test
823	 * must be done before sending an EOI so it can't be done if
824	 * we are using AUTO_EOI_1.
825	 */
826	if (intrcnt[NR_DEVICES + d] <= 5)
827		log(LOG_ERR, "stray irq %d\n", d);
828	if (intrcnt[NR_DEVICES + d] == 5)
829		log(LOG_CRIT,
830		    "too many stray irq %d's; not logging any more\n", d);
831}
832
833/*
834 * Find the highest priority enabled display device.  Since we can't
835 * distinguish display devices from ttys, depend on display devices
836 * being sensitive and before sensitive non-display devices (if any)
837 * in isa_devtab_tty.
838 *
839 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES.
840 */
841struct isa_device *
842find_display()
843{
844	struct isa_device *dvp;
845
846	for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++)
847		if (dvp->id_driver->sensitive_hw && dvp->id_enabled)
848			return (dvp);
849	return (NULL);
850}
851
852/*
853 * find an ISA device in a given isa_devtab_* table, given
854 * the table to search, the expected id_driver entry, and the unit number.
855 *
856 * this function is defined in isa_device.h, and this location is debatable;
857 * i put it there because it's useless w/o, and directly operates on
858 * the other stuff in that file.
859 *
860 */
861
862struct isa_device *find_isadev(table, driverp, unit)
863     struct isa_device *table;
864     struct isa_driver *driverp;
865     int unit;
866{
867  if (driverp == NULL) /* sanity check */
868    return NULL;
869
870  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
871    if (table->id_driver == 0)
872      return NULL;
873
874    table++;
875  }
876
877  return table;
878}
879
880/*
881 * Return nonzero if a (masked) irq is pending for a given device.
882 */
883int
884isa_irq_pending(dvp)
885	struct isa_device *dvp;
886{
887	unsigned id_irq;
888
889	id_irq = dvp->id_irq;
890	if (id_irq & 0xff)
891		return (inb(IO_ICU1) & id_irq);
892	return (inb(IO_ICU2) & (id_irq >> 8));
893}
894
895int
896update_intr_masks(void)
897{
898	int intr, n=0;
899	u_int mask,*maskptr;
900
901	for (intr=0; intr < ICU_LEN; intr ++) {
902		if (intr==2) continue;
903		maskptr = intr_mptr[intr];
904		if (!maskptr) continue;
905		mask = *maskptr;
906		if (mask != intr_mask[intr]) {
907#if 0
908			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
909				intr, intr_mask[intr], mask, maskptr);
910#endif
911			intr_mask[intr]=mask;
912			n++;
913		}
914
915	}
916	return (n);
917}
918
919int
920register_intr(intr, device_id, flags, handler, maskptr, unit)
921	int	intr;
922	int	device_id;
923	u_int	flags;
924	inthand2_t *handler;
925	u_int	*maskptr;
926	int	unit;
927{
928	char	*cp;
929	u_long	ef;
930	int	id;
931	u_int	mask = (maskptr ? *maskptr : 0);
932
933	if ((u_int)intr >= ICU_LEN || intr == 2
934	    || (u_int)device_id >= NR_DEVICES)
935		return (EINVAL);
936	if (intr_handler[intr] != isa_strayintr)
937		return (EBUSY);
938	ef = read_eflags();
939	disable_intr();
940	intr_countp[intr] = &intrcnt[device_id];
941	intr_handler[intr] = handler;
942	intr_mptr[intr] = maskptr;
943	intr_mask[intr] = mask | (1 << intr);
944	intr_unit[intr] = unit;
945	setidt(ICU_OFFSET + intr,
946	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
947	       SDT_SYS386IGT, SEL_KPL);
948	write_eflags(ef);
949	for (cp = intrnames, id = 0; id <= device_id; id++)
950		while (*cp++ != '\0')
951			;
952	if (cp > eintrnames)
953		return (0);
954	if (intr < 10) {
955		cp[-3] = intr + '0';
956		cp[-2] = ' ';
957	} else {
958		cp[-3] = '1';
959		cp[-2] = intr - 10 + '0';
960	}
961	return (0);
962}
963
964static void
965register_imask(dvp, mask)
966	struct isa_device *dvp;
967	u_int	mask;
968{
969	if (dvp->id_alive && dvp->id_irq) {
970		int	intr;
971
972		intr = ffs(dvp->id_irq) - 1;
973		intr_mask[intr] = mask | (1 <<intr);
974	}
975	(void) update_intr_masks();
976}
977
978int
979unregister_intr(intr, handler)
980	int	intr;
981	inthand2_t *handler;
982{
983	u_long	ef;
984
985	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
986		return (EINVAL);
987	ef = read_eflags();
988	disable_intr();
989	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
990	intr_handler[intr] = isa_strayintr;
991	intr_mptr[intr] = NULL;
992	intr_mask[intr] = HWI_MASK | SWI_MASK;
993	intr_unit[intr] = intr;
994	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
995	write_eflags(ef);
996	return (0);
997}
998