isa.c revision 12991
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.59 1995/12/19 14:30:48 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	outb(IO_ICU1+1, 2 | 1);		/* (master) auto EOI, 8086 mode */
564	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
565	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
566	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
567
568	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
569	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
570	outb(IO_ICU2+1,2);		/* my slave id is 2 */
571#ifdef AUTO_EOI_2
572	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
573#else
574	outb(IO_ICU2+1,1);		/* 8086 mode */
575#endif
576	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
577	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
578}
579
580/* region of physical memory known to be contiguous */
581vm_offset_t isaphysmem;
582static caddr_t dma_bounce[8];		/* XXX */
583static char bounced[8];		/* XXX */
584#define MAXDMASZ 512		/* XXX */
585
586/* high byte of address is stored in this port for i-th dma channel */
587static short dmapageport[8] =
588	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
589
590/*
591 * isa_dmacascade(): program 8237 DMA controller channel to accept
592 * external dma control by a board.
593 */
594void isa_dmacascade(unsigned chan)
595{
596	if (chan > 7)
597		panic("isa_dmacascade: impossible request");
598
599	/* set dma channel mode, and set dma channel mode */
600	if ((chan & 4) == 0) {
601		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
602		outb(DMA1_SMSK, chan);
603	} else {
604		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
605		outb(DMA2_SMSK, chan & 3);
606	}
607}
608
609static int
610isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
611
612/*
613 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
614 * problems by using a bounce buffer.
615 */
616void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
617{	vm_offset_t phys;
618	int waport;
619	caddr_t newaddr;
620
621	if (    chan > 7
622	    || (chan < 4 && nbytes > (1<<16))
623	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
624		panic("isa_dmastart: impossible request");
625
626	if (isa_dmarangecheck(addr, nbytes, chan)) {
627		if (dma_bounce[chan] == 0)
628			dma_bounce[chan] =
629				(caddr_t) isaphysmem + NBPG*chan;
630		bounced[chan] = 1;
631		newaddr = dma_bounce[chan];
632		*(int *) newaddr = 0;	/* XXX */
633
634		/* copy bounce buffer on write */
635		if (!(flags & B_READ))
636			bcopy(addr, newaddr, nbytes);
637		addr = newaddr;
638	}
639
640	/* translate to physical */
641	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
642
643	if ((chan & 4) == 0) {
644		/*
645		 * Program one of DMA channels 0..3.  These are
646		 * byte mode channels.
647		 */
648		/* set dma channel mode, and reset address ff */
649
650		/* If B_RAW flag is set, then use autoinitialise mode */
651		if (flags & B_RAW) {
652		  if (flags & B_READ)
653			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
654		  else
655			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
656		}
657		else
658		if (flags & B_READ)
659			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
660		else
661			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
662		outb(DMA1_FFC, 0);
663
664		/* send start address */
665		waport =  DMA1_CHN(chan);
666		outb(waport, phys);
667		outb(waport, phys>>8);
668		outb(dmapageport[chan], phys>>16);
669
670		/* send count */
671		outb(waport + 1, --nbytes);
672		outb(waport + 1, nbytes>>8);
673
674		/* unmask channel */
675		outb(DMA1_SMSK, chan);
676	} else {
677		/*
678		 * Program one of DMA channels 4..7.  These are
679		 * word mode channels.
680		 */
681		/* set dma channel mode, and reset address ff */
682
683		/* If B_RAW flag is set, then use autoinitialise mode */
684		if (flags & B_RAW) {
685		  if (flags & B_READ)
686			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
687		  else
688			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
689		}
690		else
691		if (flags & B_READ)
692			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
693		else
694			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
695		outb(DMA2_FFC, 0);
696
697		/* send start address */
698		waport = DMA2_CHN(chan - 4);
699		outb(waport, phys>>1);
700		outb(waport, phys>>9);
701		outb(dmapageport[chan], phys>>16);
702
703		/* send count */
704		nbytes >>= 1;
705		outb(waport + 2, --nbytes);
706		outb(waport + 2, nbytes>>8);
707
708		/* unmask channel */
709		outb(DMA2_SMSK, chan & 3);
710	}
711}
712
713void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
714{
715
716	/* copy bounce buffer on read */
717	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
718	if (bounced[chan]) {
719		bcopy(dma_bounce[chan], addr, nbytes);
720		bounced[chan] = 0;
721	}
722}
723
724/*
725 * Check for problems with the address range of a DMA transfer
726 * (non-contiguous physical pages, outside of bus address space,
727 * crossing DMA page boundaries).
728 * Return true if special handling needed.
729 */
730
731static int
732isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
733	vm_offset_t phys, priorpage = 0, endva;
734	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
735
736	endva = (vm_offset_t)round_page(va + length);
737	for (; va < (caddr_t) endva ; va += NBPG) {
738		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
739#define ISARAM_END	RAM_END
740		if (phys == 0)
741			panic("isa_dmacheck: no physical page present");
742		if (phys >= ISARAM_END)
743			return (1);
744		if (priorpage) {
745			if (priorpage + NBPG != phys)
746				return (1);
747			/* check if crossing a DMA page boundary */
748			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
749				return (1);
750		}
751		priorpage = phys;
752	}
753	return (0);
754}
755
756#define NMI_PARITY (1 << 7)
757#define NMI_IOCHAN (1 << 6)
758#define ENMI_WATCHDOG (1 << 7)
759#define ENMI_BUSTIMER (1 << 6)
760#define ENMI_IOSTATUS (1 << 5)
761
762/*
763 * Handle a NMI, possibly a machine check.
764 * return true to panic system, false to ignore.
765 */
766int
767isa_nmi(cd)
768	int cd;
769{
770	int isa_port = inb(0x61);
771	int eisa_port = inb(0x461);
772	if(isa_port & NMI_PARITY) {
773		panic("RAM parity error, likely hardware failure.");
774	} else if(isa_port & NMI_IOCHAN) {
775		panic("I/O channel check, likely hardware failure.");
776	} else if(eisa_port & ENMI_WATCHDOG) {
777		panic("EISA watchdog timer expired, likely hardware failure.");
778	} else if(eisa_port & ENMI_BUSTIMER) {
779		panic("EISA bus timeout, likely hardware failure.");
780	} else if(eisa_port & ENMI_IOSTATUS) {
781		panic("EISA I/O port status error.");
782	} else {
783		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
784		return(0);
785	}
786}
787
788/*
789 * Caught a stray interrupt, notify
790 */
791static void
792isa_strayintr(d)
793	int d;
794{
795
796	/* DON'T BOTHER FOR NOW! */
797	/* for some reason, we get bursts of intr #7, even if not enabled! */
798	/*
799	 * Well the reason you got bursts of intr #7 is because someone
800	 * raised an interrupt line and dropped it before the 8259 could
801	 * prioritize it.  This is documented in the intel data book.  This
802	 * means you have BAD hardware!  I have changed this so that only
803	 * the first 5 get logged, then it quits logging them, and puts
804	 * out a special message. rgrimes 3/25/1993
805	 */
806	/*
807	 * XXX TODO print a different message for #7 if it is for a
808	 * glitch.  Glitches can be distinguished from real #7's by
809	 * testing that the in-service bit is _not_ set.  The test
810	 * must be done before sending an EOI so it can't be done if
811	 * we are using AUTO_EOI_1.
812	 *
813	 * XXX AUTO_EOI_1 is now standard.
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		*maskptr |= 1 << intr;
895		mask = *maskptr;
896		if (mask != intr_mask[intr]) {
897#if 0
898			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
899				intr, intr_mask[intr], mask, maskptr);
900#endif
901			intr_mask[intr]=mask;
902			n++;
903		}
904
905	}
906	return (n);
907}
908
909int
910register_intr(intr, device_id, flags, handler, maskptr, unit)
911	int	intr;
912	int	device_id;
913	u_int	flags;
914	inthand2_t *handler;
915	u_int	*maskptr;
916	int	unit;
917{
918	char	*cp;
919	u_long	ef;
920	int	id;
921	u_int	mask = (maskptr ? *maskptr : 0);
922
923	if ((u_int)intr >= ICU_LEN || intr == 2
924	    || (u_int)device_id >= NR_DEVICES)
925		return (EINVAL);
926	if (intr_handler[intr] != isa_strayintr)
927		return (EBUSY);
928	ef = read_eflags();
929	disable_intr();
930	intr_countp[intr] = &intrcnt[device_id];
931	intr_handler[intr] = handler;
932	intr_mptr[intr] = maskptr;
933	intr_mask[intr] = mask | (1 << intr);
934	intr_unit[intr] = unit;
935	setidt(ICU_OFFSET + intr,
936	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
937	       SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
938	write_eflags(ef);
939	for (cp = intrnames, id = 0; id <= device_id; id++)
940		while (*cp++ != '\0')
941			;
942	if (cp > eintrnames)
943		return (0);
944	if (intr < 10) {
945		cp[-3] = intr + '0';
946		cp[-2] = ' ';
947	} else {
948		cp[-3] = '1';
949		cp[-2] = intr - 10 + '0';
950	}
951	return (0);
952}
953
954static void
955register_imask(dvp, mask)
956	struct isa_device *dvp;
957	u_int	mask;
958{
959	if (dvp->id_alive && dvp->id_irq) {
960		int	intr;
961
962		intr = ffs(dvp->id_irq) - 1;
963		intr_mask[intr] = mask | (1 <<intr);
964	}
965	(void) update_intr_masks();
966}
967
968int
969unregister_intr(intr, handler)
970	int	intr;
971	inthand2_t *handler;
972{
973	u_long	ef;
974
975	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
976		return (EINVAL);
977	ef = read_eflags();
978	disable_intr();
979	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
980	intr_handler[intr] = isa_strayintr;
981	intr_mptr[intr] = NULL;
982	intr_mask[intr] = HWI_MASK | SWI_MASK;
983	intr_unit[intr] = intr;
984	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL,
985	    GSEL(GCODE_SEL, SEL_KPL));
986	write_eflags(ef);
987	return (0);
988}
989