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