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