isa.c revision 8014
1169689Skan/*-
2169689Skan * Copyright (c) 1991 The Regents of the University of California.
3169689Skan * All rights reserved.
4169689Skan *
5169689Skan * This code is derived from software contributed to Berkeley by
6169689Skan * William Jolitz.
7169689Skan *
8169689Skan * Redistribution and use in source and binary forms, with or without
9169689Skan * modification, are permitted provided that the following conditions
10169689Skan * are met:
11169689Skan * 1. Redistributions of source code must retain the above copyright
12169689Skan *    notice, this list of conditions and the following disclaimer.
13169689Skan * 2. Redistributions in binary form must reproduce the above copyright
14169689Skan *    notice, this list of conditions and the following disclaimer in the
15169689Skan *    documentation and/or other materials provided with the distribution.
16169689Skan * 3. All advertising materials mentioning features or use of this software
17169689Skan *    must display the following acknowledgement:
18169689Skan *	This product includes software developed by the University of
19169689Skan *	California, Berkeley and its contributors.
20169689Skan * 4. Neither the name of the University nor the names of its contributors
21169689Skan *    may be used to endorse or promote products derived from this software
22169689Skan *    without specific prior written permission.
23169689Skan *
24169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34169689Skan * SUCH DAMAGE.
35169689Skan *
36169689Skan *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
37169689Skan *	$Id: isa.c,v 1.45 1995/04/12 20:47:54 wollman Exp $
38169689Skan */
39169689Skan
40169689Skan/*
41169689Skan * code to manage AT bus
42169689Skan *
43169689Skan * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
44169689Skan * Fixed uninitialized variable problem and added code to deal
45169689Skan * with DMA page boundaries in isa_dmarangecheck().  Fixed word
46169689Skan * mode DMA count compution and reorganized DMA setup code in
47169689Skan * isa_dmastart()
48169689Skan */
49169689Skan
50169689Skan#include <sys/param.h>
51169689Skan#include <sys/systm.h>		/* isn't it a joy */
52169689Skan#include <sys/kernel.h>		/* to have three of these */
53169689Skan#include <sys/proc.h>
54169689Skan#include <sys/conf.h>
55169689Skan#include <sys/file.h>
56169689Skan#include <sys/buf.h>
57169689Skan#include <sys/uio.h>
58169689Skan#include <sys/syslog.h>
59169689Skan#include <sys/malloc.h>
60169689Skan#include <sys/rlist.h>
61169689Skan#include <machine/segments.h>
62169689Skan#include <vm/vm.h>
63169689Skan#include <machine/spl.h>
64169689Skan#include <i386/isa/isa_device.h>
65169689Skan#include <i386/isa/isa.h>
66169689Skan#include <i386/isa/icu.h>
67169689Skan#include <i386/isa/ic/i8237.h>
68169689Skan#include <i386/isa/ic/i8042.h>
69169689Skan#include <sys/devconf.h>
70169689Skan#include "vector.h"
71169689Skan
72169689Skan/*
73169689Skan**  Register definitions for DMA controller 1 (channels 0..3):
74169689Skan*/
75169689Skan#define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
76169689Skan#define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
77169689Skan#define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
78169689Skan#define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
79169689Skan
80169689Skan/*
81169689Skan**  Register definitions for DMA controller 2 (channels 4..7):
82169689Skan*/
83169689Skan#define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
84169689Skan#define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
85169689Skan#define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
86169689Skan#define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
87169689Skan
88169689Skan/*
89169689Skan * XXX these defines should be in a central place.
90169689Skan */
91169689Skan#define	read_eflags()		({u_long ef; \
92169689Skan				  __asm("pushfl; popl %0" : "=a" (ef)); \
93169689Skan				  ef; })
94169689Skan#define	write_eflags(ef)	__asm("pushl %0; popfl" : : "a" ((u_long)(ef)))
95169689Skan
96169689Skanu_long	*intr_countp[ICU_LEN];
97169689Skaninthand2_t *intr_handler[ICU_LEN];
98169689Skanu_int	intr_mask[ICU_LEN];
99169689Skanu_int*	intr_mptr[ICU_LEN];
100169689Skanint	intr_unit[ICU_LEN];
101169689Skan
102169689Skanextern struct kern_devconf kdc_cpu0;
103169689Skan
104169689Skanstruct kern_devconf kdc_isa0 = {
105169689Skan	0, 0, 0,		/* filled in by dev_attach */
106169689Skan	"isa", 0, { MDDT_BUS, 0 },
107169689Skan	0, 0, 0, BUS_EXTERNALLEN,
108169689Skan	&kdc_cpu0,		/* parent is the CPU */
109169689Skan	0,			/* no parentdata */
110169689Skan	DC_BUSY,		/* busses are always busy */
111169689Skan	"ISA or EISA bus",
112169689Skan	DC_CLS_BUS		/* class */
113169689Skan};
114169689Skan
115169689Skanstatic inthand_t *fastintr[ICU_LEN] = {
116169689Skan	&IDTVEC(fastintr0), &IDTVEC(fastintr1),
117169689Skan	&IDTVEC(fastintr2), &IDTVEC(fastintr3),
118169689Skan	&IDTVEC(fastintr4), &IDTVEC(fastintr5),
119169689Skan	&IDTVEC(fastintr6), &IDTVEC(fastintr7),
120169689Skan	&IDTVEC(fastintr8), &IDTVEC(fastintr9),
121169689Skan	&IDTVEC(fastintr10), &IDTVEC(fastintr11),
122169689Skan	&IDTVEC(fastintr12), &IDTVEC(fastintr13),
123169689Skan	&IDTVEC(fastintr14), &IDTVEC(fastintr15)
124169689Skan};
125169689Skan
126169689Skanstatic inthand_t *slowintr[ICU_LEN] = {
127169689Skan	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
128169689Skan	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
129169689Skan	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
130169689Skan	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15)
131169689Skan};
132169689Skan
133169689Skanstatic void config_isadev __P((struct isa_device *isdp, u_int *mp));
134169689Skanstatic void config_isadev_c __P((struct isa_device *isdp, u_int *mp,
135169689Skan				 int reconfig));
136169689Skanstatic void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp,
137169689Skan			  int item, char const *whatnot, char const *reason,
138169689Skan			  char const *format));
139169689Skanstatic int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp,
140169689Skan			 u_int checkbits));
141169689Skanstatic inthand2_t isa_strayintr;
142169689Skanstatic void register_imask __P((struct isa_device *dvp, u_int mask));
143169689Skan
144169689Skan/*
145169689Skan * print a conflict message
146169689Skan */
147169689Skanstatic void
148169689Skanconflict(dvp, tmpdvp, item, whatnot, reason, format)
149169689Skan	struct isa_device	*dvp;
150169689Skan	struct isa_device	*tmpdvp;
151169689Skan	int			item;
152169689Skan	char const		*whatnot;
153169689Skan	char const		*reason;
154169689Skan	char const		*format;
155169689Skan{
156169689Skan	printf("%s%d not %sed due to %s conflict with %s%d at ",
157169689Skan		dvp->id_driver->name, dvp->id_unit, whatnot, reason,
158169689Skan		tmpdvp->id_driver->name, tmpdvp->id_unit);
159169689Skan	printf(format, item);
160169689Skan	printf("\n");
161169689Skan}
162169689Skan
163169689Skan/*
164169689Skan * Check to see if things are alread in use, like IRQ's, I/O addresses
165169689Skan * and Memory addresses.
166 */
167static int
168haveseen(dvp, tmpdvp, checkbits)
169	struct isa_device *dvp;
170	struct isa_device *tmpdvp;
171	u_int	checkbits;
172{
173	/*
174	 * Only check against devices that have already been found
175	 */
176	if (tmpdvp->id_alive) {
177		char const *whatnot;
178
179		whatnot = checkbits & CC_ATTACH ? "attach" : "prob";
180		/*
181		 * Check for I/O address conflict.  We can only check the
182		 * starting address of the device against the range of the
183		 * device that has already been probed since we do not
184		 * know how many I/O addresses this device uses.
185		 */
186		if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) {
187			if ((dvp->id_iobase >= tmpdvp->id_iobase) &&
188			    (dvp->id_iobase <=
189				  (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) {
190				conflict(dvp, tmpdvp, dvp->id_iobase, whatnot,
191					 "I/O address", "0x%x");
192				return 1;
193			}
194		}
195		/*
196		 * Check for Memory address conflict.  We can check for
197		 * range overlap, but it will not catch all cases since the
198		 * driver may adjust the msize paramater during probe, for
199		 * now we just check that the starting address does not
200		 * fall within any allocated region.
201		 * XXX could add a second check after the probe for overlap,
202		 * since at that time we would know the full range.
203		 * XXX KERNBASE is a hack, we should have vaddr in the table!
204		 */
205		if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) {
206			if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) &&
207			    (KERNBASE + dvp->id_maddr <=
208			     (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) {
209				conflict(dvp, tmpdvp, (int)dvp->id_maddr,
210					 whatnot, "maddr", "0x%x");
211				return 1;
212			}
213		}
214		/*
215		 * Check for IRQ conflicts.
216		 */
217		if (checkbits & CC_IRQ && tmpdvp->id_irq) {
218			if (tmpdvp->id_irq == dvp->id_irq) {
219				conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1,
220					 whatnot, "irq", "%d");
221				return 1;
222			}
223		}
224		/*
225		 * Check for DRQ conflicts.
226		 */
227		if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) {
228			if (tmpdvp->id_drq == dvp->id_drq) {
229				conflict(dvp, tmpdvp, dvp->id_drq, whatnot,
230					 "drq", "%d");
231				return 1;
232			}
233		}
234	}
235	return 0;
236}
237
238/*
239 * Search through all the isa_devtab_* tables looking for anything that
240 * conflicts with the current device.
241 */
242#include "eisa.h"
243#if NEISA > 0
244extern struct isa_device isa_devtab_eisa[];
245#endif
246
247int
248haveseen_isadev(dvp, checkbits)
249	struct isa_device *dvp;
250	u_int	checkbits;
251{
252	struct isa_device *tmpdvp;
253	int	status = 0;
254
255	for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) {
256		status |= haveseen(dvp, tmpdvp, checkbits);
257		if (status)
258			return status;
259	}
260	for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) {
261		status |= haveseen(dvp, tmpdvp, checkbits);
262		if (status)
263			return status;
264	}
265	for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) {
266		status |= haveseen(dvp, tmpdvp, checkbits);
267		if (status)
268			return status;
269	}
270	for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) {
271		status |= haveseen(dvp, tmpdvp, checkbits);
272		if (status)
273			return status;
274	}
275#if NEISA > 0
276	for (tmpdvp = isa_devtab_eisa; tmpdvp->id_driver; tmpdvp++) {
277		status |= haveseen(dvp, tmpdvp, checkbits);
278		if (status)
279			return status;
280	}
281#endif
282	return(status);
283}
284
285/*
286 * Configure all ISA devices
287 */
288void
289isa_configure() {
290	struct isa_device *dvp;
291
292	dev_attach(&kdc_isa0);
293
294	splhigh();
295	enable_intr();
296	INTREN(IRQ_SLAVE);
297	printf("Probing for devices on the ISA bus:\n");
298	/* First probe all the sensitive probes */
299	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
300		if (dvp->id_driver->sensitive_hw)
301			config_isadev(dvp, &tty_imask);
302	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
303		if (dvp->id_driver->sensitive_hw)
304			config_isadev(dvp, &bio_imask);
305	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
306		if (dvp->id_driver->sensitive_hw)
307			config_isadev(dvp, &net_imask);
308	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
309		if (dvp->id_driver->sensitive_hw)
310			config_isadev(dvp, (u_int *)NULL);
311
312	/* Then all the bad ones */
313	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
314		if (!dvp->id_driver->sensitive_hw)
315			config_isadev(dvp, &tty_imask);
316	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
317		if (!dvp->id_driver->sensitive_hw)
318			config_isadev(dvp, &bio_imask);
319	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
320		if (!dvp->id_driver->sensitive_hw)
321			config_isadev(dvp, &net_imask);
322	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
323		if (!dvp->id_driver->sensitive_hw)
324			config_isadev(dvp, (u_int *)NULL);
325
326	bio_imask |= SWI_CLOCK_MASK;
327	net_imask |= SWI_NET_MASK;
328	tty_imask |= SWI_TTY_MASK;
329
330/*
331 * XXX we should really add the tty device to net_imask when the line is
332 * switched to SLIPDISC, and then remove it when it is switched away from
333 * SLIPDISC.  No need to block out ALL ttys during a splimp when only one
334 * of them is running slip.
335 *
336 * XXX actually, blocking all ttys during a splimp doesn't matter so much
337 * with sio because the serial interrupt layer doesn't use tty_imask.  Only
338 * non-serial ttys suffer.  It's more stupid that ALL 'net's are blocked
339 * during spltty.
340 */
341#include "sl.h"
342#include "ppp.h"
343
344#if (NSL > 0) || (NPPP > 0)
345	net_imask |= tty_imask;
346	tty_imask = net_imask;
347#endif
348	/* bio_imask |= tty_imask ;  can some tty devices use buffers? */
349#ifdef DIAGNOSTIC
350	printf("bio_imask %x tty_imask %x net_imask %x\n",
351	       bio_imask, tty_imask, net_imask);
352#endif
353	/*
354	 * Finish initializing intr_mask[].  Note that the partly
355	 * constructed masks aren't actually used since we're at splhigh.
356	 * For fully dynamic initialization, register_intr() and
357	 * unregister_intr() will have to adjust the masks for _all_
358	 * interrupts and for tty_imask, etc.
359	 */
360	for (dvp = isa_devtab_tty; dvp->id_driver; dvp++)
361		register_imask(dvp, tty_imask);
362	for (dvp = isa_devtab_bio; dvp->id_driver; dvp++)
363		register_imask(dvp, bio_imask);
364	for (dvp = isa_devtab_net; dvp->id_driver; dvp++)
365		register_imask(dvp, net_imask);
366	for (dvp = isa_devtab_null; dvp->id_driver; dvp++)
367		register_imask(dvp, SWI_CLOCK_MASK);
368	spl0();
369}
370
371/*
372 * Configure an ISA device.
373 */
374
375
376static void
377config_isadev(isdp, mp)
378     struct isa_device *isdp;
379     u_int *mp;
380{
381	config_isadev_c(isdp, mp, 0);
382}
383
384void
385reconfig_isadev(isdp, mp)
386	struct isa_device *isdp;
387	u_int *mp;
388{
389	config_isadev_c(isdp, mp, 1);
390}
391
392static void
393config_isadev_c(isdp, mp, reconfig)
394	struct isa_device *isdp;
395	u_int *mp;
396	int reconfig;
397{
398	u_int checkbits;
399	int id_alive;
400	int last_alive;
401	struct isa_driver *dp = isdp->id_driver;
402
403 	checkbits = 0;
404#ifndef ALLOW_CONFLICT_DRQ
405	checkbits |= CC_DRQ;
406#endif
407#ifndef ALLOW_CONFLICT_IOADDR
408	checkbits |= CC_IOADDR;
409#endif
410#ifndef ALLOW_CONFLICT_MEMADDR
411	checkbits |= CC_MEMADDR;
412#endif
413	if (!isdp->id_enabled) {
414		printf("%s%d: disabled, not probed.\n",
415			dp->name, isdp->id_unit);
416		return;
417	}
418	if (!reconfig && haveseen_isadev(isdp, checkbits))
419		return;
420	if (!reconfig && isdp->id_maddr) {
421		isdp->id_maddr -= 0xa0000; /* XXX should be a define */
422		isdp->id_maddr += atdevbase;
423	}
424	if (reconfig) {
425		last_alive = isdp->id_alive;
426		isdp->id_reconfig = 1;
427	}
428	else {
429		last_alive = 0;
430		isdp->id_reconfig = 0;
431	}
432	id_alive = (*dp->probe)(isdp);
433	if (id_alive) {
434		/*
435		 * Only print the I/O address range if id_alive != -1
436		 * Right now this is a temporary fix just for the new
437		 * NPX code so that if it finds a 486 that can use trap
438		 * 16 it will not report I/O addresses.
439		 * Rod Grimes 04/26/94
440		 */
441		if (!isdp->id_reconfig) {
442			printf("%s%d", dp->name, isdp->id_unit);
443			if (id_alive != -1) {
444 				printf(" at 0x%x", isdp->id_iobase);
445 				if ((isdp->id_iobase + id_alive - 1) !=
446 				     isdp->id_iobase) {
447 					printf("-0x%x",
448					       isdp->id_iobase + id_alive - 1);
449				}
450			}
451			if (isdp->id_irq)
452				printf(" irq %d", ffs(isdp->id_irq) - 1);
453			if (isdp->id_drq != -1)
454				printf(" drq %d", isdp->id_drq);
455			if (isdp->id_maddr)
456				printf(" maddr 0x%lx", kvtop(isdp->id_maddr));
457			if (isdp->id_msize)
458				printf(" msize %d", isdp->id_msize);
459			if (isdp->id_flags)
460				printf(" flags 0x%x", isdp->id_flags);
461			if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) {
462				printf(" on motherboard");
463			} else if (isdp->id_iobase >= 0x1000 &&
464				    !(isdp->id_iobase & 0x300)) {
465				printf (" on eisa slot %d",
466					isdp->id_iobase >> 12);
467			} else {
468				printf (" on isa");
469			}
470			printf("\n");
471			/*
472			 * Check for conflicts again.  The driver may have
473			 * changed *dvp.  We should weaken the early check
474			 * since the driver may have been able to change
475			 * *dvp to avoid conflicts if given a chance.  We
476			 * already skip the early check for IRQs and force
477			 * a check for IRQs in the next group of checks.
478		 	 */
479#ifndef ALLOW_CONFLICT_IRQ
480			checkbits |= CC_IRQ;
481#endif
482			if (haveseen_isadev(isdp, checkbits))
483				return;
484			isdp->id_alive = id_alive;
485		}
486		(*dp->attach)(isdp);
487		if (isdp->id_irq) {
488			if (mp)
489				INTRMASK(*mp, isdp->id_irq);
490			register_intr(ffs(isdp->id_irq) - 1, isdp->id_id,
491				      isdp->id_ri_flags, isdp->id_intr,
492				      mp, isdp->id_unit);
493			INTREN(isdp->id_irq);
494		}
495	} else {
496		if (isdp->id_reconfig) {
497			(*dp->attach)(isdp); /* reconfiguration attach */
498		}
499		if (!last_alive) {
500			if (!isdp->id_reconfig) {
501				printf("%s%d not found", dp->name, isdp->id_unit);
502				if (isdp->id_iobase) {
503					printf(" at 0x%x", isdp->id_iobase);
504				}
505				printf("\n");
506			}
507		}
508		else {
509			/* This code has not been tested.... */
510			if (isdp->id_irq) {
511				INTRDIS(isdp->id_irq);
512				unregister_intr(ffs(isdp->id_irq) - 1,
513						isdp->id_intr);
514				if (mp)
515					INTRUNMASK(*mp, isdp->id_irq);
516			}
517		}
518	}
519}
520
521/*
522 * Provide ISA-specific device information to user programs using the
523 * hw.devconf interface.
524 */
525int
526isa_externalize(struct isa_device *id, void *userp, size_t *maxlen)
527{
528	if(*maxlen < sizeof *id) {
529		return ENOMEM;
530	}
531
532	*maxlen -= sizeof *id;
533	return copyout(id, userp, sizeof *id);
534}
535
536/*
537 * Do the same thing for EISA information.  EISA information is currently
538 * the same as ISA information plus a slot number, but could be extended in
539 * the future.
540 */
541int
542eisa_externalize(struct isa_device *id, int slot, void *userp, size_t *maxlen)
543{
544	int rv;
545
546	if(*maxlen < (sizeof *id) + (sizeof slot)) {
547		return ENOMEM;
548	}
549	*maxlen -= (sizeof *id) + (sizeof slot);
550
551	rv = copyout(id, userp, sizeof *id);
552	if(rv) return rv;
553
554	return copyout(&slot, (char *)userp + sizeof *id, sizeof slot);
555}
556
557/*
558 * This is used to forcibly reconfigure an ISA device.  It currently just
559 * returns an error 'cos you can't do that yet.  It is here to demonstrate
560 * what the `internalize' routine is supposed to do.
561 */
562int
563isa_internalize(struct isa_device *id, void **userpp, size_t *len)
564{
565	struct isa_device myid;
566	char *userp = *userpp;
567	int rv;
568
569	if(*len < sizeof *id) {
570		return EINVAL;
571	}
572
573	rv = copyin(userp, &myid, sizeof myid);
574	if(rv) return rv;
575	*userpp = userp + sizeof myid;
576	*len -= sizeof myid;
577
578	rv = EOPNOTSUPP;
579	/* code would go here to validate the configuration request */
580	/* code would go here to actually perform the reconfiguration */
581	return rv;
582}
583
584int
585isa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
586			void *userp, size_t l)
587{
588	return isa_externalize(kdc->kdc_isa, userp, &l);
589}
590
591int
592eisa_generic_externalize(struct proc *p, struct kern_devconf *kdc,
593			 void *userp, size_t l)
594{
595	return eisa_externalize(kdc->kdc_isa, -1, userp, &l);
596}
597
598/*
599 * Fill in default interrupt table (in case of spuruious interrupt
600 * during configuration of kernel, setup interrupt control unit
601 */
602void
603isa_defaultirq()
604{
605	int i;
606
607	/* icu vectors */
608	for (i = 0; i < ICU_LEN; i++)
609		unregister_intr(i, (inthand2_t *)NULL);
610
611	/* initialize 8259's */
612	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
613	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
614	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
615#ifdef AUTO_EOI_1
616	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
617#else
618	outb(IO_ICU1+1, 1);		/* 8086 mode */
619#endif
620	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
621	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
622	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
623
624	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
625	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
626	outb(IO_ICU2+1,2);		/* my slave id is 2 */
627#ifdef AUTO_EOI_2
628	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
629#else
630	outb(IO_ICU2+1,1);		/* 8086 mode */
631#endif
632	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
633	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
634}
635
636/* region of physical memory known to be contiguous */
637vm_offset_t isaphysmem;
638static caddr_t dma_bounce[8];		/* XXX */
639static char bounced[8];		/* XXX */
640#define MAXDMASZ 512		/* XXX */
641
642/* high byte of address is stored in this port for i-th dma channel */
643static short dmapageport[8] =
644	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
645
646/*
647 * isa_dmacascade(): program 8237 DMA controller channel to accept
648 * external dma control by a board.
649 */
650void isa_dmacascade(unsigned chan)
651{
652	if (chan > 7)
653		panic("isa_dmacascade: impossible request");
654
655	/* set dma channel mode, and set dma channel mode */
656	if ((chan & 4) == 0) {
657		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
658		outb(DMA1_SMSK, chan);
659	} else {
660		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
661		outb(DMA2_SMSK, chan & 3);
662	}
663}
664
665static int
666isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan);
667
668/*
669 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
670 * problems by using a bounce buffer.
671 */
672void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
673{	vm_offset_t phys;
674	int waport;
675	caddr_t newaddr;
676
677	if (    chan > 7
678	    || (chan < 4 && nbytes > (1<<16))
679	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
680		panic("isa_dmastart: impossible request");
681
682	if (isa_dmarangecheck(addr, nbytes, chan)) {
683		if (dma_bounce[chan] == 0)
684			dma_bounce[chan] =
685				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
686				(caddr_t) isaphysmem + NBPG*chan;
687		bounced[chan] = 1;
688		newaddr = dma_bounce[chan];
689		*(int *) newaddr = 0;	/* XXX */
690
691		/* copy bounce buffer on write */
692		if (!(flags & B_READ))
693			bcopy(addr, newaddr, nbytes);
694		addr = newaddr;
695	}
696
697	/* translate to physical */
698	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
699
700	if ((chan & 4) == 0) {
701		/*
702		 * Program one of DMA channels 0..3.  These are
703		 * byte mode channels.
704		 */
705		/* set dma channel mode, and reset address ff */
706
707		/* If B_RAW flag is set, then use autoinitialise mode */
708		if (flags & B_RAW) {
709		  if (flags & B_READ)
710			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
711		  else
712			outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
713		}
714		else
715		if (flags & B_READ)
716			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
717		else
718			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
719		outb(DMA1_FFC, 0);
720
721		/* send start address */
722		waport =  DMA1_CHN(chan);
723		outb(waport, phys);
724		outb(waport, phys>>8);
725		outb(dmapageport[chan], phys>>16);
726
727		/* send count */
728		outb(waport + 1, --nbytes);
729		outb(waport + 1, nbytes>>8);
730
731		/* unmask channel */
732		outb(DMA1_SMSK, chan);
733	} else {
734		/*
735		 * Program one of DMA channels 4..7.  These are
736		 * word mode channels.
737		 */
738		/* set dma channel mode, and reset address ff */
739
740		/* If B_RAW flag is set, then use autoinitialise mode */
741		if (flags & B_RAW) {
742		  if (flags & B_READ)
743			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
744		  else
745			outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
746		}
747		else
748		if (flags & B_READ)
749			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
750		else
751			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
752		outb(DMA2_FFC, 0);
753
754		/* send start address */
755		waport = DMA2_CHN(chan - 4);
756		outb(waport, phys>>1);
757		outb(waport, phys>>9);
758		outb(dmapageport[chan], phys>>16);
759
760		/* send count */
761		nbytes >>= 1;
762		outb(waport + 2, --nbytes);
763		outb(waport + 2, nbytes>>8);
764
765		/* unmask channel */
766		outb(DMA2_SMSK, chan & 3);
767	}
768}
769
770void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
771{
772
773	/* copy bounce buffer on read */
774	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
775	if (bounced[chan]) {
776		bcopy(dma_bounce[chan], addr, nbytes);
777		bounced[chan] = 0;
778	}
779}
780
781/*
782 * Check for problems with the address range of a DMA transfer
783 * (non-contiguous physical pages, outside of bus address space,
784 * crossing DMA page boundaries).
785 * Return true if special handling needed.
786 */
787
788static int
789isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
790	vm_offset_t phys, priorpage = 0, endva;
791	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
792
793	endva = (vm_offset_t)round_page(va + length);
794	for (; va < (caddr_t) endva ; va += NBPG) {
795		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
796#define ISARAM_END	RAM_END
797		if (phys == 0)
798			panic("isa_dmacheck: no physical page present");
799		if (phys >= ISARAM_END)
800			return (1);
801		if (priorpage) {
802			if (priorpage + NBPG != phys)
803				return (1);
804			/* check if crossing a DMA page boundary */
805			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
806				return (1);
807		}
808		priorpage = phys;
809	}
810	return (0);
811}
812
813/* head of queue waiting for physmem to become available */
814struct buf isa_physmemq;
815
816/* blocked waiting for resource to become free for exclusive use */
817static isaphysmemflag;
818/* if waited for and call requested when free (B_CALL) */
819static void (*isaphysmemunblock)(); /* needs to be a list */
820
821/*
822 * Allocate contiguous physical memory for transfer, returning
823 * a *virtual* address to region. May block waiting for resource.
824 * (assumed to be called at splbio())
825 */
826caddr_t
827isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
828
829	isaphysmemunblock = func;
830	while (isaphysmemflag & B_BUSY) {
831		isaphysmemflag |= B_WANTED;
832		tsleep((caddr_t)&isaphysmemflag, PRIBIO, "isaphys", 0);
833	}
834	isaphysmemflag |= B_BUSY;
835
836	return((caddr_t)isaphysmem);
837}
838
839/*
840 * Free contiguous physical memory used for transfer.
841 * (assumed to be called at splbio())
842 */
843void
844isa_freephysmem(caddr_t va, unsigned length) {
845
846	isaphysmemflag &= ~B_BUSY;
847	if (isaphysmemflag & B_WANTED) {
848		isaphysmemflag &= B_WANTED;
849		wakeup((caddr_t)&isaphysmemflag);
850		if (isaphysmemunblock)
851			(*isaphysmemunblock)();
852	}
853}
854
855#define NMI_PARITY (1 << 7)
856#define NMI_IOCHAN (1 << 6)
857#define ENMI_WATCHDOG (1 << 7)
858#define ENMI_BUSTIMER (1 << 6)
859#define ENMI_IOSTATUS (1 << 5)
860
861/*
862 * Handle a NMI, possibly a machine check.
863 * return true to panic system, false to ignore.
864 */
865int
866isa_nmi(cd)
867	int cd;
868{
869	int isa_port = inb(0x61);
870	int eisa_port = inb(0x461);
871	if(isa_port & NMI_PARITY) {
872		panic("RAM parity error, likely hardware failure.");
873	} else if(isa_port & NMI_IOCHAN) {
874		panic("I/O channel check, likely hardware failure.");
875	} else if(eisa_port & ENMI_WATCHDOG) {
876		panic("EISA watchdog timer expired, likely hardware failure.");
877	} else if(eisa_port & ENMI_BUSTIMER) {
878		panic("EISA bus timeout, likely hardware failure.");
879	} else if(eisa_port & ENMI_IOSTATUS) {
880		panic("EISA I/O port status error.");
881	} else {
882		printf("\nNMI ISA %x, EISA %x\n", isa_port, eisa_port);
883		return(0);
884	}
885}
886
887/*
888 * Caught a stray interrupt, notify
889 */
890static void
891isa_strayintr(d)
892	int d;
893{
894
895	/* DON'T BOTHER FOR NOW! */
896	/* for some reason, we get bursts of intr #7, even if not enabled! */
897	/*
898	 * Well the reason you got bursts of intr #7 is because someone
899	 * raised an interrupt line and dropped it before the 8259 could
900	 * prioritize it.  This is documented in the intel data book.  This
901	 * means you have BAD hardware!  I have changed this so that only
902	 * the first 5 get logged, then it quits logging them, and puts
903	 * out a special message. rgrimes 3/25/1993
904	 */
905	/*
906	 * XXX TODO print a different message for #7 if it is for a
907	 * glitch.  Glitches can be distinguished from real #7's by
908	 * testing that the in-service bit is _not_ set.  The test
909	 * must be done before sending an EOI so it can't be done if
910	 * we are using AUTO_EOI_1.
911	 */
912	if (intrcnt[NR_DEVICES + d] <= 5)
913		log(LOG_ERR, "stray irq %d\n", d);
914	if (intrcnt[NR_DEVICES + d] == 5)
915		log(LOG_CRIT,
916		    "too many stray irq %d's; not logging any more\n", d);
917}
918
919/*
920 * find an ISA device in a given isa_devtab_* table, given
921 * the table to search, the expected id_driver entry, and the unit number.
922 *
923 * this function is defined in isa_device.h, and this location is debatable;
924 * i put it there because it's useless w/o, and directly operates on
925 * the other stuff in that file.
926 *
927 */
928
929struct isa_device *find_isadev(table, driverp, unit)
930     struct isa_device *table;
931     struct isa_driver *driverp;
932     int unit;
933{
934  if (driverp == NULL) /* sanity check */
935    return NULL;
936
937  while ((table->id_driver != driverp) || (table->id_unit != unit)) {
938    if (table->id_driver == 0)
939      return NULL;
940
941    table++;
942  }
943
944  return table;
945}
946
947/*
948 * Return nonzero if a (masked) irq is pending for a given device.
949 */
950int
951isa_irq_pending(dvp)
952	struct isa_device *dvp;
953{
954	unsigned id_irq;
955
956	id_irq = dvp->id_irq;
957	if (id_irq & 0xff)
958		return (inb(IO_ICU1) & id_irq);
959	return (inb(IO_ICU2) & (id_irq >> 8));
960}
961
962int
963update_intr_masks(void)
964{
965	int intr, n=0;
966	u_int mask,*maskptr;
967	for (intr=0; intr < ICU_LEN; intr ++) {
968		if (intr==2) continue;
969		maskptr = intr_mptr[intr];
970		if (!maskptr) continue;
971		mask = *maskptr;
972		if (mask != intr_mask[intr]) {
973#if 0
974			printf ("intr_mask[%2d] old=%08x new=%08x ptr=%p.\n",
975				intr, intr_mask[intr], mask, maskptr);
976#endif
977			intr_mask[intr]=mask;
978			n++;
979		}
980
981	}
982	return (n);
983}
984
985int
986register_intr(intr, device_id, flags, handler, maskptr, unit)
987	int	intr;
988	int	device_id;
989	u_int	flags;
990	inthand2_t *handler;
991	u_int	*maskptr;
992	int	unit;
993{
994	char	*cp;
995	u_long	ef;
996	int	id;
997	u_int	mask = (maskptr ? *maskptr : 0);
998
999	if ((u_int)intr >= ICU_LEN || intr == 2
1000	    || (u_int)device_id >= NR_DEVICES)
1001		return (EINVAL);
1002	if (intr_handler[intr] != isa_strayintr)
1003		return (EBUSY);
1004	ef = read_eflags();
1005	disable_intr();
1006	intr_countp[intr] = &intrcnt[device_id];
1007	intr_handler[intr] = handler;
1008	intr_mptr[intr] = maskptr;
1009	intr_mask[intr] = mask | (1 << intr);
1010	intr_unit[intr] = unit;
1011	setidt(ICU_OFFSET + intr,
1012	       flags & RI_FAST ? fastintr[intr] : slowintr[intr],
1013	       SDT_SYS386IGT, SEL_KPL);
1014	write_eflags(ef);
1015	for (cp = intrnames, id = 0; id <= device_id; id++)
1016		while (*cp++ != '\0')
1017			;
1018	if (cp > eintrnames)
1019		return (0);
1020	if (intr < 10) {
1021		cp[-3] = intr + '0';
1022		cp[-2] = ' ';
1023	} else {
1024		cp[-3] = '1';
1025		cp[-2] = intr - 10 + '0';
1026	}
1027	return (0);
1028}
1029
1030static void
1031register_imask(dvp, mask)
1032	struct isa_device *dvp;
1033	u_int	mask;
1034{
1035	if (dvp->id_alive && dvp->id_irq) {
1036		int	intr;
1037
1038		intr = ffs(dvp->id_irq) - 1;
1039		intr_mask[intr] = mask | (1 <<intr);
1040	}
1041	(void) update_intr_masks();
1042}
1043
1044int
1045unregister_intr(intr, handler)
1046	int	intr;
1047	inthand2_t *handler;
1048{
1049	u_long	ef;
1050
1051	if ((u_int)intr >= ICU_LEN || handler != intr_handler[intr])
1052		return (EINVAL);
1053	ef = read_eflags();
1054	disable_intr();
1055	intr_countp[intr] = &intrcnt[NR_DEVICES + intr];
1056	intr_handler[intr] = isa_strayintr;
1057	intr_mptr[intr] = NULL;
1058	intr_mask[intr] = HWI_MASK | SWI_MASK;
1059	intr_unit[intr] = intr;
1060	setidt(ICU_OFFSET + intr, slowintr[intr], SDT_SYS386IGT, SEL_KPL);
1061	write_eflags(ef);
1062	return (0);
1063}
1064