summaryrefslogtreecommitdiff
path: root/sys/arch/i386/isa/isa_machdep.c
blob: 93f22d78a731bd72af9c1eff64742dec6b1fe639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*	$NetBSD: isa_machdep.c,v 1.10 1996/02/09 02:26:00 mycroft Exp $	*/

/*-
 * Copyright (c) 1993, 1994 Charles Hannum.
 * Copyright (c) 1991 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)isa.c	7.2 (Berkeley) 5/13/91
 */

#include <sys/param.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <vm/vm.h>

#include <machine/pio.h>
#include <machine/cpufunc.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>
#include <i386/isa/isa_machdep.h>
#include <i386/isa/icu.h>

#define	IDTVEC(name)	__CONCAT(X,name)
/* default interrupt vector table entries */
typedef (*vector)();
extern vector IDTVEC(intr)[], IDTVEC(fast)[];
extern struct gate_descriptor idt[];

int isamatch __P((struct device *, void *, void *));
void isaattach __P((struct device *, struct device *, void *));

struct cfdriver isacd = {
	NULL, "isa", isamatch, isaattach, DV_DULL, sizeof(struct isa_softc), 1
};

int
isamatch(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{

	return (1);
}

void
isaattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct isa_softc *sc = (struct isa_softc *)self;

	printf("\n");

	TAILQ_INIT(&sc->sc_subdevs);
	config_scan(isascan, self);
}

/*
 * Fill in default interrupt table (in case of spuruious interrupt
 * during configuration of kernel, setup interrupt control unit
 */
void
isa_defaultirq()
{
	int i;

	/* icu vectors */
	for (i = 0; i < ICU_LEN; i++)
		setgate(&idt[ICU_OFFSET + i], IDTVEC(intr)[i], 0, SDT_SYS386IGT,
		    SEL_KPL);
  
	/* initialize 8259's */
	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
	outb(IO_ICU1+1, 1 << IRQ_SLAVE); /* slave on line 2 */
#ifdef AUTO_EOI_1
	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
#else
	outb(IO_ICU1+1, 1);		/* 8086 mode */
#endif
	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
	outb(IO_ICU1, 0x68);		/* special mask mode (if available) */
	outb(IO_ICU1, 0x0a);		/* Read IRR by default. */
#ifdef REORDER_IRQ
	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
#endif

	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
	outb(IO_ICU2+1, IRQ_SLAVE);
#ifdef AUTO_EOI_2
	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
#else
	outb(IO_ICU2+1, 1);		/* 8086 mode */
#endif
	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
	outb(IO_ICU2, 0x68);		/* special mask mode (if available) */
	outb(IO_ICU2, 0x0a);		/* Read IRR by default. */
}

/*
 * Handle a NMI, possibly a machine check.
 * return true to panic system, false to ignore.
 */
int
isa_nmi()
{

	log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
	return(0);
}

/*
 * Caught a stray interrupt, notify
 */
void
isa_strayintr(irq)
	int irq;
{
	static u_long strays;

        /*
         * Stray interrupts on irq 7 occur when an interrupt line is raised
         * and then lowered before the CPU acknowledges it.  This generally
         * means either the device is screwed or something is cli'ing too
         * long and it's timing out.
         */
	if (++strays <= 5)
		log(LOG_ERR, "stray interrupt %d%s\n", irq,
		    strays >= 5 ? "; stopped logging" : "");
}

int fastvec;
int intrtype[ICU_LEN], intrmask[ICU_LEN], intrlevel[ICU_LEN];
struct intrhand *intrhand[ICU_LEN];

/*
 * Recalculate the interrupt masks from scratch.
 * We could code special registry and deregistry versions of this function that
 * would be faster, but the code would be nastier, and we don't expect this to
 * happen very much anyway.
 */
void
intr_calculatemasks()
{
	int irq, level;
	struct intrhand *q;

	/* First, figure out which levels each IRQ uses. */
	for (irq = 0; irq < ICU_LEN; irq++) {
		register int levels = 0;
		for (q = intrhand[irq]; q; q = q->ih_next)
			if (q->ih_level != IPL_NONE)
				levels |= 1 << q->ih_level;
		intrlevel[irq] = levels;
	}

	/* Then figure out which IRQs use each level. */
	for (level = 0; level < 5; level++) {
		register int irqs = 0;
		for (irq = 0; irq < ICU_LEN; irq++)
			if (intrlevel[irq] & (1 << level))
				irqs |= 1 << irq;
		imask[level] = irqs | SIR_ALLMASK;
	}

	/*
	 * There are tty, network and disk drivers that use free() at interrupt
	 * time, so imp > (tty | net | bio).
	 */
	imask[IPL_IMP] |= imask[IPL_TTY] | imask[IPL_NET] | imask[IPL_BIO];

	/*
	 * Enforce a hierarchy that gives slow devices a better chance at not
	 * dropping data.
	 */
	imask[IPL_TTY] |= imask[IPL_NET] | imask[IPL_BIO];
	imask[IPL_NET] |= imask[IPL_BIO];

	/* And eventually calculate the complete masks. */
	for (irq = 0; irq < ICU_LEN; irq++) {
		register int irqs = 1 << irq;
		for (q = intrhand[irq]; q; q = q->ih_next)
			if (q->ih_level != IPL_NONE)
				irqs |= imask[q->ih_level];
		intrmask[irq] = irqs | SIR_ALLMASK;
	}

	/* Lastly, determine which IRQs are actually in use. */
	{
		register int irqs = 0;
		for (irq = 0; irq < ICU_LEN; irq++)
			if (intrhand[irq])
				irqs |= 1 << irq;
		if (irqs >= 0x100) /* any IRQs >= 8 in use */
			irqs |= 1 << IRQ_SLAVE;
		imen = ~irqs;
		SET_ICUS();
	}
}

int
fakeintr(arg)
	void *arg;
{

	return 0;
}

#define	LEGAL_IRQ(x)	((x) >= 0 && (x) < ICU_LEN && (x) != 2)

/*
 * Set up an interrupt handler to start being called.
 * XXX PRONE TO RACE CONDITIONS, UGLY, 'INTERESTING' INSERTION ALGORITHM.
 */
void *
isa_intr_establish(irq, type, level, ih_fun, ih_arg)
	int irq;
	int type;
	int level;
	int (*ih_fun) __P((void *));
	void *ih_arg;
{
	struct intrhand **p, *q, *ih;
	static struct intrhand fakehand = {fakeintr};
	extern int cold;

	/* no point in sleeping unless someone can free memory. */
	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
	if (ih == NULL)
		panic("isa_intr_establish: can't malloc handler info");

	if (!LEGAL_IRQ(irq) || type == IST_NONE)
		panic("intr_establish: bogus irq or type");

	switch (intrtype[irq]) {
	case IST_EDGE:
	case IST_LEVEL:
		if (type == intrtype[irq])
			break;
	case IST_PULSE:
		if (type != IST_NONE)
			panic("intr_establish: can't share %s with %s",
			    isa_intr_typename(intrtype[irq]),
			    isa_intr_typename(type));
		break;
	}

	/*
	 * Figure out where to put the handler.
	 * This is O(N^2), but we want to preserve the order, and N is
	 * generally small.
	 */
	for (p = &intrhand[irq]; (q = *p) != NULL; p = &q->ih_next)
		;

	/*
	 * Actually install a fake handler momentarily, since we might be doing
	 * this with interrupts enabled and don't want the real routine called
	 * until masking is set up.
	 */
	fakehand.ih_level = level;
	*p = &fakehand;

	intr_calculatemasks();

	/*
	 * Poke the real handler in now.
	 */
	ih->ih_fun = ih_fun;
	ih->ih_arg = ih_arg;
	ih->ih_count = 0;
	ih->ih_next = NULL;
	ih->ih_level = level;
	ih->ih_irq = irq;
	*p = ih;

	return (ih);
}

/*
 * Deregister an interrupt handler.
 */
void
isa_intr_disestablish(arg)
	void *arg;
{
	struct intrhand *ih = arg;
	int irq = ih->ih_irq;
	struct intrhand **p, *q;

	if (!LEGAL_IRQ(irq))
		panic("intr_disestablish: bogus irq");

	/*
	 * Remove the handler from the chain.
	 * This is O(n^2), too.
	 */
	for (p = &intrhand[irq]; (q = *p) != NULL && q != ih; p = &q->ih_next)
		;
	if (q)
		*p = q->ih_next;
	else
		panic("intr_disestablish: handler not registered");
	free(ih, M_DEVBUF);

	intr_calculatemasks();

	if (intrhand[irq] == NULL)
		intrtype[irq] = IST_NONE;
}

/*
 * ISA DMA and bounce buffer management
 */

#define MAX_CHUNK 256		/* number of low memory segments */

static unsigned long bitmap[MAX_CHUNK / 32 + 1];

#define set(i) (bitmap[(i) >> 5] |= (1 << (i)))
#define clr(i) (bitmap[(i) >> 5] &= ~(1 << (i)))
#define bit(i) ((bitmap[(i) >> 5] & (1 << (i))) != 0)

static int bit_ptr = -1;	/* last segment visited */
static int chunk_size = 0;	/* size (bytes) of one low mem segment */
static int chunk_num = 0;	/* actual number of low mem segments */

vm_offset_t isaphysmem;		/* base address of low mem arena */
int isaphysmempgs;		/* number of pages of low mem arena */

/*
 * check if addr is from our low mem arena
 */

static int
bounce_isbounced(addr)
	vm_offset_t addr;
{
	return(addr >= vtophys(isaphysmem) &&
	       addr < vtophys(isaphysmem)+isaphysmempgs*NBPG);
}

/*
 * return the virtual address of addr. addr must be from low mem arena
 */

static caddr_t
bounce_to_virt(addr)
	vm_offset_t addr;
{
	return((caddr_t)(isaphysmem+(addr-vtophys(isaphysmem))));
}

/*
 * alloc a low mem segment of size nbytes. Alignment constraint is:
 *   (addr & pmask) == ((addr+size-1) & pmask)
 * if waitok, call may wait for memory to become available.
 * returns 0 on failure
 */

static vm_offset_t
bounce_alloc(nbytes, pmask, waitok)
	vm_size_t nbytes;
	vm_offset_t pmask;
	int waitok;
{
	int i, l;
	vm_offset_t a, b, c, r;
	vm_size_t n;
	int nunits, opri;

	opri = splbio();

	if (bit_ptr < 0) {	/* initialize low mem arena */
		if ((chunk_size = isaphysmempgs*NBPG/MAX_CHUNK) & 1)
			chunk_size--;
		chunk_num =  (isaphysmempgs*NBPG) / chunk_size;
		for(i = 0; i < chunk_num; i++)
			set(i);
		bit_ptr = 0;
	}

	nunits = (nbytes+chunk_size-1)/chunk_size;

	/*
	 * set a=start, b=start with address constraints, c=end
	 * check if this request may ever succeed.
	 */

	a = isaphysmem;
	b = (isaphysmem + ~pmask) & pmask;
	c = isaphysmem + chunk_num*chunk_size;
	n = nunits*chunk_size;
	if (a + n >= c || pmask != 0 && a + n >= b && b + n >= c) {
		splx(opri);
		return(0);
	}

	for (;;) {
		i = bit_ptr;
		l = -1;
		do{
			if (bit(i) && l >= 0 && (i - l + 1) >= nunits){
				r = vtophys(isaphysmem+(i-nunits+1)*chunk_size);
				if (((r ^ (r+nbytes-1)) & pmask) == 0) {
					for (l = i - nunits + 1; l <= i; l++)
						clr(l);
					bit_ptr = i;
					splx(opri);
					return(r);
				}
			} else if (bit(i) && l < 0)
				l = i;
			else if (!bit(i))
				l = -1;
			if (++i == chunk_num) {
				i = 0;
				l = -1;
			}
		} while(i != bit_ptr);

		if (waitok)
			tsleep((caddr_t) &bit_ptr, PRIBIO, "physmem", 0);
		else {
			splx(opri);
			return(0);
		}
	}
}

/* 
 * return a segent of the low mem arena to the free pool
 */

static void
bounce_free(addr, nbytes)
	vm_offset_t addr;
	vm_size_t nbytes;
{
	int i, j, opri;
	vm_offset_t vaddr;

	opri = splbio();

	vaddr = (vm_offset_t)bounce_to_virt(addr);
	if (vaddr < isaphysmem || vaddr >= isaphysmem+chunk_num*chunk_size ||
	    ((i = (int)(vaddr-isaphysmem)) % chunk_size) != 0)
		panic("bounce_free: bad address");

	i /= chunk_size;
	j = i + (nbytes+chunk_size-1)/chunk_size;

	while (i < j) {
		if (bit(i))
			panic("bounce_free: already free");
		set(i);
		i++;
	}

	wakeup((caddr_t)&bit_ptr);
	splx(opri);
}

/*
 * setup (addr,nbytes) for an ISA dma transfer.
 * flags&ISADMA_MAP_WAITOK	may wait
 * flags&ISADMA_MAP_BOUNCE	may use a bounce buffer if necessary
 * flags&ISADMA_MAP_CONTIG	result must be physically contiguous
 * flags&ISADMA_MAP_8BIT	must not cross 64k boundary
 * flags&ISADMA_MAP_16BIT	must not cross 128k boundary
 *
 * returns the number of used phys entries, 0 on failure.
 * if flags&ISADMA_MAP_CONTIG result is 1 on sucess!
 */

int
isadma_map(addr, nbytes, phys, flags)
	caddr_t addr;
	vm_size_t nbytes;
	struct isadma_seg *phys;
	int flags;
{
	vm_offset_t pmask, thiskv, thisphys, nextphys;
	vm_size_t datalen;
	int seg, waitok, i;

	if (flags & ISADMA_MAP_8BIT)
		pmask = ~((64*1024)-1);
	else if (flags & ISADMA_MAP_16BIT)
		pmask = ~((128*1024)-1);
	else
		pmask = 0;

	waitok = (flags & ISADMA_MAP_WAITOK) != 0;

	thiskv = (vm_offset_t)addr;
	datalen = nbytes;
	thisphys = vtophys(thiskv);
	seg = 0;

	while (datalen > 0 && (seg == 0 || (flags & ISADMA_MAP_CONTIG) == 0)) {
		if (thisphys == 0)
			panic("isadma_map: no physical page present");

		phys[seg].length = 0;
		phys[seg].addr = thisphys;

		nextphys = thisphys;
		while (datalen > 0 && thisphys == nextphys) {
			nextphys = trunc_page(thisphys) + NBPG;
			phys[seg].length += min(nextphys-thisphys, datalen);
			datalen -= min(nextphys-thisphys, datalen);
			thiskv = trunc_page(thiskv) + NBPG;
			if (datalen)
				thisphys = vtophys(thiskv);
		}

		if (phys[seg].addr+phys[seg].length > 0xffffff) {
			if (flags & ISADMA_MAP_CONTIG) {
				phys[seg].length = nbytes;
				datalen = 0;
			}
			if ((flags & ISADMA_MAP_BOUNCE) == 0)
				phys[seg].addr = 0;
			else
				phys[seg].addr =
				    bounce_alloc(phys[seg].length, pmask,
						 waitok);
			if (phys[seg].addr == 0) {
				for (i = 0; i < seg; i++)
					if (bounce_isbounced(phys[i].addr))
						bounce_free(phys[i].addr,
							    phys[i].length);
				return 0;
			}
		}

		seg++;
	}

	/* check all constraints */
	if (datalen ||
	    ((phys[0].addr ^ (phys[0].addr+phys[0].length-1)) & pmask) != 0 ||
	    ((phys[0].addr & 1) && (flags & ISADMA_MAP_16BIT))) {
		if ((flags & ISADMA_MAP_BOUNCE) == 0)
			return 0;
		if ((phys[0].addr = bounce_alloc(nbytes, pmask, waitok)) == 0)
			return 0;
		phys[0].length = nbytes;
	}

	return seg;
}

/*
 * undo a ISA dma mapping. Simply return the bounced segments to the pool.
 */

void
isadma_unmap(addr, nbytes, nphys, phys)
	caddr_t addr;
	vm_size_t nbytes;
	int nphys;
	struct isadma_seg *phys;
{
	int i;

	for (i = 0; i < nphys; i++)
		if (bounce_isbounced(phys[i].addr))
			bounce_free(phys[i].addr, phys[i].length);
}

/*
 * copy bounce buffer to buffer where needed
 */

void
isadma_copyfrombuf(addr, nbytes, nphys, phys)
	caddr_t addr;
	vm_size_t nbytes;
	int nphys;
	struct isadma_seg *phys;
{
	int i;

	for (i = 0; i < nphys; i++) {
		if (bounce_isbounced(phys[i].addr)) {
			bcopy(bounce_to_virt(phys[i].addr), addr,
			    phys[i].length);
		}
		addr += phys[i].length;
	}
}

/*
 * copy buffer to bounce buffer where needed
 */

void
isadma_copytobuf(addr, nbytes, nphys, phys)
	caddr_t addr;
	vm_size_t nbytes;
	int nphys;
	struct isadma_seg *phys;
{
	int i;

	for (i = 0; i < nphys; i++) {
		if (bounce_isbounced(phys[i].addr)) {
			bcopy(addr, bounce_to_virt(phys[i].addr),
			    phys[i].length);
		}
		addr += phys[i].length;
	}
}