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