isa.c revision 3869
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.32 1994/10/23 21:27:26 wollman 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/conf.h>
54#include <sys/file.h>
55#include <sys/buf.h>
56#include <sys/uio.h>
57#include <sys/syslog.h>
58#include <sys/malloc.h>
59#include <sys/rlist.h>
60#include <machine/segments.h>
61#include <vm/vm.h>
62#include <machine/spl.h>
63#include <i386/isa/isa_device.h>
64#include <i386/isa/isa.h>
65#include <i386/isa/icu.h>
66#include <i386/isa/ic/i8237.h>
67#include <i386/isa/ic/i8042.h>
68#include <sys/devconf.h>
69#include "vector.h"
70
71/*
72**  Register definitions for DMA controller 1 (channels 0..3):
73*/
74#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
75#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
76#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
77#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
78
79/*
80**  Register definitions for DMA controller 2 (channels 4..7):
81*/
82#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
83#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
84#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
85#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
86
87/*
88 * Bits to specify the type and amount of conflict checking.
89 */
90#define	CC_ATTACH	(1 << 0)
91#define	CC_DRQ		(1 << 1)
92#define	CC_IOADDR	(1 << 2)
93#define	CC_IRQ		(1 << 3)
94#define	CC_MEMADDR	(1 << 4)
95
96/*
97 * XXX these defines should be in a central place.
98 */
99#define	read_eflags()		({u_long ef; \
100				  __asm("pushfl; popl %0" : "=a" (ef)); \
101				  ef; })
102#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
103
104u_long	*intr_countp[ICU_LEN];
105inthand2_t *intr_handler[ICU_LEN];
106u_int	intr_mask[ICU_LEN];
107int	intr_unit[ICU_LEN];
108
109struct kern_devconf kdc_isa0 = {
110	0, 0, 0,		/* filled in by dev_attach */
111	"isa", 0, { MDDT_BUS, 0 },
112	0, 0, 0, BUS_EXTERNALLEN,
113	0,			/* no parent yet; parent should be CPU */
114	0,			/* no parentdata */
115	DC_BUSY,		/* busses are always busy */
116	"ISA or EISA bus"
117};
118
119static inthand_t *fastintr[ICU_LEN] = {
120	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
121	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
122	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
123	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
124	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
125	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
126	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
127	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
128};
129
130static inthand_t *slowintr[ICU_LEN] = {
131	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
132	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
133	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
134	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
135};
136
137static void config_isadev __P((struct isa_device *isdp, u_int *mp));
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 int haveseen_isadev __P((struct isa_device *dvp, u_int checkbits));
144static inthand2_t isa_strayintr;
145static void register_imask __P((struct isa_device *dvp, u_int mask));
146
147/*
148 * print a conflict message
149 */
150static void
151conflict(dvp, tmpdvp, item, whatnot, reason, format)
152	struct isa_device	*dvp;
153	struct isa_device	*tmpdvp;
154	int			item;
155	char const		*whatnot;
156	char const		*reason;
157	char const		*format;
158{
159	printf("%s%d not %sed due to %s conflict with %s%d at ",
160		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
161		tmpdvp->id_driver->name, tmpdvp->id_unit);
162	printf(format, item);
163	printf("\n");
164}
165
166/*
167 * Check to see if things are alread in use, like IRQ's, I/O addresses
168 * and Memory addresses.
169 */
170static int
171haveseen(dvp, tmpdvp, checkbits)
172	struct isa_device *dvp;
173	struct isa_device *tmpdvp;
174	u_int	checkbits;
175{
176	/*
177	 * Only check against devices that have already been found
178	 */
179	if (tmpdvp->id_alive) {
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 */
245static int
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) || (NPPP > 0)
336	net_imask |= tty_imask;
337	tty_imask = net_imask;
338#endif
339	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
340#ifdef DIAGNOSTIC
341	printf("bio_imask %x tty_imask %x net_imask %x\n",
342	       bio_imask, tty_imask, net_imask);
343#endif
344	/*
345	 * Finish initializing intr_mask[].  Note that the partly
346	 * constructed masks aren't actually used since we're at splhigh.
347	 * For fully dynamic initialization, register_intr() and
348	 * unregister_intr() will have to adjust the masks for _all_
349	 * interrupts and for tty_imask, etc.
350	 */
351	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
352		register_imask(dvp, tty_imask);
353	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
354		register_imask(dvp, bio_imask);
355	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
356		register_imask(dvp, net_imask);
357	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
358		register_imask(dvp, SWI_CLOCK_MASK);
359	spl0();
360}
361
362/*
363 * Configure an ISA device.
364 */
365
366
367static void config_isadev_c();
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#ifndef ALLOW_CONFLICT_DRQ
398	checkbits |= CC_DRQ;
399#endif
400#ifndef ALLOW_CONFLICT_IOADDR
401	checkbits |= CC_IOADDR;
402#endif
403#ifndef ALLOW_CONFLICT_MEMADDR
404	checkbits |= CC_MEMADDR;
405#endif
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	}
415	else {
416		last_alive = 0;
417	}
418	id_alive = (*dp->probe)(isdp);
419	if (id_alive) {
420		/*
421		 * Only print the I/O address range if id_alive != -1
422		 * Right now this is a temporary fix just for the new
423		 * NPX code so that if it finds a 486 that can use trap
424		 * 16 it will not report I/O addresses.
425		 * Rod Grimes 04/26/94
426		 */
427		if (!isdp->id_reconfig) {
428			printf("%s%d", dp->name, isdp->id_unit);
429			if (id_alive != -1) {
430 				printf(" at 0x%x", isdp->id_iobase);
431 				if ((isdp->id_iobase + id_alive - 1) !=
432 				     isdp->id_iobase) {
433 					printf("-0x%x",
434					       isdp->id_iobase + id_alive - 1);
435				}
436			}
437			if (isdp->id_irq)
438				printf(" irq %d", ffs(isdp->id_irq) - 1);
439			if (isdp->id_drq != -1)
440				printf(" drq %d", isdp->id_drq);
441			if (isdp->id_maddr)
442				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
443			if (isdp->id_msize)
444				printf(" msize %d", isdp->id_msize);
445			if (isdp->id_flags)
446				printf(" flags 0x%x", isdp->id_flags);
447			if (isdp->id_iobase) {
448				if (isdp->id_iobase < 0x100) {
449					printf(" on motherboard\n");
450				} else {
451					if (isdp->id_iobase >= 0x1000) {
452						printf (" on eisa\n");
453					} else {
454						printf (" on isa\n");
455					}
456				}
457			}
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#ifndef ALLOW_CONFLICT_IRQ
467			checkbits |= CC_IRQ;
468#endif
469			if (haveseen_isadev(isdp, checkbits))
470				return;
471			isdp->id_alive = id_alive;
472		}
473		(*dp->attach)(isdp);
474		if (isdp->id_irq) {
475			if (mp)
476				INTRMASK(*mp, isdp->id_irq);
477			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
478				      isdp->id_ri_flags, isdp->id_intr,
479				      mp, isdp->id_unit);
480			INTREN(isdp->id_irq);
481		}
482	} else {
483		if (isdp->id_reconfig) {
484			(*dp->attach)(isdp); /* reconfiguration attach */
485		}
486		if (!last_alive) {
487			if (!isdp->id_reconfig) {
488				printf("%s%d not found", dp->name, isdp->id_unit);
489				if (isdp->id_iobase) {
490					printf(" at 0x%x", isdp->id_iobase);
491				}
492				printf("\n");
493			}
494		}
495		else {
496			/* This code has not been tested.... */
497			if (isdp->id_irq) {
498				INTRDIS(isdp->id_irq);
499				unregister_intr(ffs(isdp->id_irq) - 1,
500						isdp->id_intr);
501				if (mp)
502					INTRUNMASK(*mp, isdp->id_irq);
503			}
504		}
505	}
506}
507
508/*
509 * Provide ISA-specific device information to user programs using the
510 * hw.devconf interface.
511 */
512int
513isa_externalize(struct isa_device *id, void *userp, size_t *maxlen)
514{
515	if(*maxlen < sizeof *id) {
516		return ENOMEM;
517	}
518
519	*maxlen -= sizeof *id;
520	return copyout(id, userp, sizeof *id);
521}
522
523/*
524 * Do the same thing for EISA information.  EISA information is currently
525 * the same as ISA information plus a slot number, but could be extended in
526 * the future.
527 */
528int
529eisa_externalize(struct isa_device *id, int slot, void *userp, size_t *maxlen)
530{
531	int rv;
532
533	if(*maxlen < (sizeof *id) + (sizeof slot)) {
534		return ENOMEM;
535	}
536	*maxlen -= (sizeof *id) + (sizeof slot);
537
538	rv = copyout(id, userp, sizeof *id);
539	if(rv) return rv;
540
541	return copyout(&slot, (char *)userp + sizeof *id, sizeof slot);
542}
543
544/*
545 * This is used to forcibly reconfigure an ISA device.  It currently just
546 * returns an error 'cos you can't do that yet.  It is here to demonstrate
547 * what the `internalize' routine is supposed to do.
548 */
549int
550isa_internalize(struct isa_device *id, void **userpp, size_t *len)
551{
552	struct isa_device myid;
553	char *userp = *userpp;
554	int rv;
555
556	if(*len < sizeof *id) {
557		return EINVAL;
558	}
559
560	rv = copyin(userp, &myid, sizeof myid);
561	if(rv) return rv;
562	*userpp = userp + sizeof myid;
563	*len -= sizeof myid;
564
565	rv = EOPNOTSUPP;
566	/* code would go here to validate the configuration request */
567	/* code would go here to actually perform the reconfiguration */
568	return rv;
569}
570
571int
572isa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
573			void *userp, size_t l)
574{
575	return isa_externalize(kdc->kdc_isa, userp, &l);
576}
577
578int
579eisa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
580			 void *userp, size_t l)
581{
582	return eisa_externalize(kdc->kdc_isa, -1, userp, &l);
583}
584
585/*
586 * Fill in default interrupt table (in case of spuruious interrupt
587 * during configuration of kernel, setup interrupt control unit
588 */
589void
590isa_defaultirq()
591{
592	int i;
593
594	/* icu vectors */
595	for (i = 0; i < ICU_LEN; i++)
596		unregister_intr(i, (inthand2_t *)NULL);
597
598	/* initialize 8259's */
599	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
600	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
601	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
602#ifdef AUTO_EOI_1
603	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
604#else
605	outb(IO_ICU1+1, 1);		/* 8086 mode */
606#endif
607	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
608	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
609	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
610
611	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
612	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
613	outb(IO_ICU2+1,2);		/* my slave id is 2 */
614#ifdef AUTO_EOI_2
615	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
616#else
617	outb(IO_ICU2+1,1);		/* 8086 mode */
618#endif
619	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
620	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
621}
622
623/* region of physical memory known to be contiguous */
624vm_offset_t isaphysmem;
625static caddr_t dma_bounce[8];		/* XXX */
626static char bounced[8];		/* XXX */
627#define MAXDMASZ 512		/* XXX */
628
629/* high byte of address is stored in this port for i-th dma channel */
630static short dmapageport[8] =
631	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
632
633/*
634 * isa_dmacascade(): program 8237 DMA controller channel to accept
635 * external dma control by a board.
636 */
637void isa_dmacascade(unsigned chan)
638{
639	if (chan > 7)
640		panic("isa_dmacascade: impossible request");
641
642	/* set dma channel mode, and set dma channel mode */
643	if ((chan & 4) == 0) {
644		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
645		outb(DMA1_SMSK, chan);
646	} else {
647		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
648		outb(DMA2_SMSK, chan & 3);
649	}
650}
651
652static int
653isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
654
655/*
656 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
657 * problems by using a bounce buffer.
658 */
659void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
660{	vm_offset_t phys;
661	int waport;
662	caddr_t newaddr;
663
664	if (    chan > 7
665	    || (chan < 4 && nbytes > (1<<16))
666	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
667		panic("isa_dmastart: impossible request");
668
669	if (isa_dmarangecheck(addr, nbytes, chan)) {
670		if (dma_bounce[chan] == 0)
671			dma_bounce[chan] =
672				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
673				(caddr_t) isaphysmem + NBPG*chan;
674		bounced[chan] = 1;
675		newaddr = dma_bounce[chan];
676		*(int *) newaddr = 0;	/* XXX */
677
678		/* copy bounce buffer on write */
679		if (!(flags & B_READ))
680			bcopy(addr, newaddr, nbytes);
681		addr = newaddr;
682	}
683
684	/* translate to physical */
685	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
686
687	if ((chan & 4) == 0) {
688		/*
689		 * Program one of DMA channels 0..3.  These are
690		 * byte mode channels.
691		 */
692		/* set dma channel mode, and reset address ff */
693		if (flags & B_READ)
694			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
695		else
696			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
697		outb(DMA1_FFC, 0);
698
699		/* send start address */
700		waport =  DMA1_CHN(chan);
701		outb(waport, phys);
702		outb(waport, phys>>8);
703		outb(dmapageport[chan], phys>>16);
704
705		/* send count */
706		outb(waport + 1, --nbytes);
707		outb(waport + 1, nbytes>>8);
708
709		/* unmask channel */
710		outb(DMA1_SMSK, chan);
711	} else {
712		/*
713		 * Program one of DMA channels 4..7.  These are
714		 * word mode channels.
715		 */
716		/* set dma channel mode, and reset address ff */
717		if (flags & B_READ)
718			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
719		else
720			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
721		outb(DMA2_FFC, 0);
722
723		/* send start address */
724		waport = DMA2_CHN(chan - 4);
725		outb(waport, phys>>1);
726		outb(waport, phys>>9);
727		outb(dmapageport[chan], phys>>16);
728
729		/* send count */
730		nbytes >>= 1;
731		outb(waport + 2, --nbytes);
732		outb(waport + 2, nbytes>>8);
733
734		/* unmask channel */
735		outb(DMA2_SMSK, chan & 3);
736	}
737}
738
739void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
740{
741
742	/* copy bounce buffer on read */
743	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
744	if (bounced[chan]) {
745		bcopy(dma_bounce[chan], addr, nbytes);
746		bounced[chan] = 0;
747	}
748}
749
750/*
751 * Check for problems with the address range of a DMA transfer
752 * (non-contiguous physical pages, outside of bus address space,
753 * crossing DMA page boundaries).
754 * Return true if special handling needed.
755 */
756
757static int
758isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
759	vm_offset_t phys, priorpage = 0, endva;
760	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
761
762	endva = (vm_offset_t)round_page(va + length);
763	for (; va < (caddr_t) endva ; va += NBPG) {
764		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
765#define ISARAM_END	RAM_END
766		if (phys == 0)
767			panic("isa_dmacheck: no physical page present");
768		if (phys >= ISARAM_END)
769			return (1);
770		if (priorpage) {
771			if (priorpage + NBPG != phys)
772				return (1);
773			/* check if crossing a DMA page boundary */
774			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
775				return (1);
776		}
777		priorpage = phys;
778	}
779	return (0);
780}
781
782/* head of queue waiting for physmem to become available */
783struct buf isa_physmemq;
784
785/* blocked waiting for resource to become free for exclusive use */
786static isaphysmemflag;
787/* if waited for and call requested when free (B_CALL) */
788static void (*isaphysmemunblock)(); /* needs to be a list */
789
790/*
791 * Allocate contiguous physical memory for transfer, returning
792 * a *virtual* address to region. May block waiting for resource.
793 * (assumed to be called at splbio())
794 */
795caddr_t
796isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
797
798	isaphysmemunblock = func;
799	while (isaphysmemflag & B_BUSY) {
800		isaphysmemflag |= B_WANTED;
801		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
802	}
803	isaphysmemflag |= B_BUSY;
804
805	return((caddr_t)isaphysmem);
806}
807
808/*
809 * Free contiguous physical memory used for transfer.
810 * (assumed to be called at splbio())
811 */
812void
813isa_freephysmem(caddr_t va, unsigned length) {
814
815	isaphysmemflag &= ~B_BUSY;
816	if (isaphysmemflag & B_WANTED) {
817		isaphysmemflag &= B_WANTED;
818		wakeup((caddr_t)&isaphysmemflag);
819		if (isaphysmemunblock)
820			(*isaphysmemunblock)();
821	}
822}
823
824#define NMI_PARITY (1 << 7)
825#define NMI_IOCHAN (1 << 6)
826#define ENMI_WATCHDOG (1 << 7)
827#define ENMI_BUSTIMER (1 << 6)
828#define ENMI_IOSTATUS (1 << 5)
829
830/*
831 * Handle a NMI, possibly a machine check.
832 * return true to panic system, false to ignore.
833 */
834int
835isa_nmi(cd)
836	int cd;
837{
838	int isa_port = inb(0x61);
839	int eisa_port = inb(0x461);
840	if(isa_port & NMI_PARITY) {
841		panic("RAM parity error, likely hardware failure.");
842	} else if(isa_port & NMI_IOCHAN) {
843		panic("I/O channel check, likely hardware failure.");
844	} else if(eisa_port & ENMI_WATCHDOG) {
845		panic("EISA watchdog timer expired, likely hardware failure.");
846	} else if(eisa_port & ENMI_BUSTIMER) {
847		panic("EISA bus timeout, likely hardware failure.");
848	} else if(eisa_port & ENMI_IOSTATUS) {
849		panic("EISA I/O port status error.");
850	} else {
851		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
852		return(0);
853	}
854}
855
856/*
857 * Caught a stray interrupt, notify
858 */
859static void
860isa_strayintr(d)
861	int d;
862{
863
864	/* DON'T BOTHER FOR NOW! */
865	/* for some reason, we get bursts of intr #7, even if not enabled! */
866	/*
867	 * Well the reason you got bursts of intr #7 is because someone
868	 * raised an interrupt line and dropped it before the 8259 could
869	 * prioritize it.  This is documented in the intel data book.  This
870	 * means you have BAD hardware!  I have changed this so that only
871	 * the first 5 get logged, then it quits logging them, and puts
872	 * out a special message. rgrimes 3/25/1993
873	 */
874	/*
875	 * XXX TODO print a different message for #7 if it is for a
876	 * glitch.  Glitches can be distinguished from real #7's by
877	 * testing that the in-service bit is _not_ set.  The test
878	 * must be done before sending an EOI so it can't be done if
879	 * we are using AUTO_EOI_1.
880	 */
881	if (intrcnt[NR_DEVICES + d] <= 5)
882		log(LOG_ERR, "stray irq %d\n", d);
883	if (intrcnt[NR_DEVICES + d] == 5)
884		log(LOG_CRIT,
885		    "too many stray irq %d's; not logging any more\n", d);
886}
887
888/*
889 * find an ISA device in a given isa_devtab_* table, given
890 * the table to search, the expected id_driver entry, and the unit number.
891 *
892 * this function is defined in isa_device.h, and this location is debatable;
893 * i put it there because it's useless w/o, and directly operates on
894 * the other stuff in that file.
895 *
896 */
897
898struct isa_device *find_isadev(table, driverp, unit)
899     struct isa_device *table;
900     struct isa_driver *driverp;
901     int unit;
902{
903  if (driverp == NULL) /* sanity check */
904    return NULL;
905
906  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
907    if (table->id_driver == 0)
908      return NULL;
909
910    table++;
911  }
912
913  return table;
914}
915
916/*
917 * Return nonzero if a (masked) irq is pending for a given device.
918 */
919int
920isa_irq_pending(dvp)
921	struct isa_device *dvp;
922{
923	unsigned id_irq;
924
925	id_irq = dvp->id_irq;
926	if (id_irq & 0xff)
927		return (inb(IO_ICU1) & id_irq);
928	return (inb(IO_ICU2) & (id_irq >> 8));
929}
930
931int
932register_intr(intr, device_id, flags, handler, maskptr, unit)
933	int	intr;
934	int	device_id;
935	u_int	flags;
936	inthand2_t *handler;
937	u_int	*maskptr;
938	int	unit;
939{
940	char	*cp;
941	u_long	ef;
942	int	id;
943	u_int	mask = (maskptr ? *maskptr : 0);
944
945	if ((u_int)intr >= ICU_LEN || intr == 2
946	    || (u_int)device_id >= NR_DEVICES)
947		return (EINVAL);
948	if (intr_handler[intr] != isa_strayintr)
949		return (EBUSY);
950	ef = read_eflags();
951	disable_intr();
952	intr_countp[intr] = &intrcnt[device_id];
953	intr_handler[intr] = handler;
954	intr_mask[intr] = mask | (1 << intr);
955	intr_unit[intr] = unit;
956	setidt(ICU_OFFSET + intr,
957	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
958	       SDT_SYS386IGT, SEL_KPL);
959	write_eflags(ef);
960	for (cp = intrnames, id = 0; id <= device_id; id++)
961		while (*cp++ != '\0')
962			;
963	if (cp > eintrnames)
964		return (0);
965	if (intr < 10) {
966		cp[-3] = intr + '0';
967		cp[-2] = ' ';
968	} else {
969		cp[-3] = '1';
970		cp[-2] = intr - 10 + '0';
971	}
972	return (0);
973}
974
975static void
976register_imask(dvp, mask)
977	struct isa_device *dvp;
978	u_int	mask;
979{
980	if (dvp->id_alive && dvp->id_irq) {
981		int	intr;
982
983		intr = ffs(dvp->id_irq) - 1;
984		intr_mask[intr] = mask | (1 <<intr);
985	}
986}
987
988int
989unregister_intr(intr, handler)
990	int	intr;
991	inthand2_t *handler;
992{
993	u_long	ef;
994
995	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
996		return (EINVAL);
997	ef = read_eflags();
998	disable_intr();
999	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1000	intr_handler[intr] = isa_strayintr;
1001	intr_mask[intr] = HWI_MASK | SWI_MASK;
1002	intr_unit[intr] = intr;
1003	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
1004	write_eflags(ef);
1005	return (0);
1006}
1007