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