Home | History | Annotate | Line # | Download | only in isa
isa_machdep.c revision 1.16
      1 /*	$NetBSD: isa_machdep.c,v 1.16 2007/02/21 20:41:26 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*-
     41  * Copyright (c) 1991 The Regents of the University of California.
     42  * All rights reserved.
     43  *
     44  * This code is derived from software contributed to Berkeley by
     45  * William Jolitz.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)isa.c	7.2 (Berkeley) 5/13/91
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.16 2007/02/21 20:41:26 mrg Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/syslog.h>
     81 #include <sys/device.h>
     82 #include <sys/malloc.h>
     83 #include <sys/proc.h>
     84 #include <sys/mbuf.h>
     85 
     86 #include <machine/bus.h>
     87 #include <machine/bus_private.h>
     88 
     89 #include <machine/pio.h>
     90 #include <machine/cpufunc.h>
     91 
     92 #include <dev/isa/isareg.h>
     93 #include <dev/isa/isavar.h>
     94 
     95 #include <uvm/uvm_extern.h>
     96 
     97 #include "ioapic.h"
     98 
     99 #if NIOAPIC > 0
    100 #include <machine/i82093var.h>
    101 #include <machine/mpbiosvar.h>
    102 #endif
    103 
    104 static int _isa_dma_may_bounce(bus_dma_tag_t, bus_dmamap_t, int, int *);
    105 
    106 struct x86_bus_dma_tag isa_bus_dma_tag = {
    107 	0,				/* _tag_needs_free */
    108 	ISA_DMA_BOUNCE_THRESHOLD,	/* _bounce_thresh */
    109 	0,				/* _bounce_alloc_lo */
    110 	ISA_DMA_BOUNCE_THRESHOLD,	/* _bounce_alloc_hi */
    111 	_isa_dma_may_bounce,
    112 	_bus_dmamap_create,
    113 	_bus_dmamap_destroy,
    114 	_bus_dmamap_load,
    115 	_bus_dmamap_load_mbuf,
    116 	_bus_dmamap_load_uio,
    117 	_bus_dmamap_load_raw,
    118 	_bus_dmamap_unload,
    119 	_bus_dmamap_sync,
    120 	_bus_dmamem_alloc,
    121 	_bus_dmamem_free,
    122 	_bus_dmamem_map,
    123 	_bus_dmamem_unmap,
    124 	_bus_dmamem_mmap,
    125 	_bus_dmatag_subregion,
    126 	_bus_dmatag_destroy,
    127 };
    128 
    129 #define	IDTVEC(name)	__CONCAT(X,name)
    130 typedef void (vector) __P((void));
    131 extern vector *IDTVEC(intr)[];
    132 
    133 #define	LEGAL_IRQ(x)	((x) >= 0 && (x) < NUM_LEGACY_IRQS && (x) != 2)
    134 
    135 int
    136 isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
    137 {
    138 	int i, tmp, bestirq, count;
    139 	struct intrhand **p, *q;
    140 	struct intrsource *isp;
    141 	struct cpu_info *ci;
    142 
    143 	if (type == IST_NONE)
    144 		panic("intr_alloc: bogus type");
    145 
    146 	ci = &cpu_info_primary;
    147 
    148 	bestirq = -1;
    149 	count = -1;
    150 
    151 	/* some interrupts should never be dynamically allocated */
    152 	mask &= 0xdef8;
    153 
    154 	/*
    155 	 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
    156 	 * the right answer is to do "breadth-first" searching of devices.
    157 	 */
    158 	mask &= 0xefbf;
    159 
    160 	simple_lock(&ci->ci_slock);
    161 
    162 	for (i = 0; i < NUM_LEGACY_IRQS; i++) {
    163 		if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
    164 			continue;
    165 		isp = ci->ci_isources[i];
    166 		if (isp == NULL) {
    167 			/*
    168 			 * if nothing's using the irq, just return it
    169 			 */
    170 			*irq = i;
    171 			simple_unlock(&ci->ci_slock);
    172 			return (0);
    173 		}
    174 
    175 		switch(isp->is_type) {
    176 		case IST_EDGE:
    177 		case IST_LEVEL:
    178 			if (type != isp->is_type)
    179 				continue;
    180 			/*
    181 			 * if the irq is shareable, count the number of other
    182 			 * handlers, and if it's smaller than the last irq like
    183 			 * this, remember it
    184 			 *
    185 			 * XXX We should probably also consider the
    186 			 * interrupt level and stick IPL_TTY with other
    187 			 * IPL_TTY, etc.
    188 			 */
    189 			for (p = &isp->is_handlers, tmp = 0; (q = *p) != NULL;
    190 			     p = &q->ih_next, tmp++)
    191 				;
    192 			if ((bestirq == -1) || (count > tmp)) {
    193 				bestirq = i;
    194 				count = tmp;
    195 			}
    196 			break;
    197 
    198 		case IST_PULSE:
    199 			/* this just isn't shareable */
    200 			continue;
    201 		}
    202 	}
    203 
    204 	simple_unlock(&ci->ci_slock);
    205 
    206 	if (bestirq == -1)
    207 		return (1);
    208 
    209 	*irq = bestirq;
    210 
    211 	return (0);
    212 }
    213 
    214 const struct evcnt *
    215 isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
    216 {
    217 
    218 	/* XXX for now, no evcnt parent reported */
    219 	return NULL;
    220 }
    221 
    222 void *
    223 isa_intr_establish(
    224     isa_chipset_tag_t ic,
    225     int irq,
    226     int type,
    227     int level,
    228     int (*ih_fun)(void *),
    229     void *ih_arg
    230 )
    231 {
    232 	struct pic *pic;
    233 	int pin;
    234 #if NIOAPIC > 0
    235 	int mpih;
    236 #endif
    237 
    238 	pin = irq;
    239 	pic = &i8259_pic;
    240 
    241 #if NIOAPIC > 0
    242 	if (mp_busses != NULL) {
    243 		if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 ||
    244 		    intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) {
    245 			if (!APIC_IRQ_ISLEGACY(mpih)) {
    246 				pin = APIC_IRQ_PIN(mpih);
    247 				pic = (struct pic *)
    248 				    ioapic_find(APIC_IRQ_APIC(mpih));
    249 				if (pic == NULL) {
    250 					printf("isa_intr_establish: "
    251 					       "unknown apic %d\n",
    252 					    APIC_IRQ_APIC(mpih));
    253 					return NULL;
    254 				}
    255 			}
    256 		} else
    257 			printf("isa_intr_establish: no MP mapping found\n");
    258 	}
    259 #endif
    260 	return intr_establish(irq, pic, pin, type, level, ih_fun, ih_arg);
    261 }
    262 
    263 /*
    264  * Deregister an interrupt handler.
    265  */
    266 void
    267 isa_intr_disestablish(isa_chipset_tag_t ic, void *arg)
    268 {
    269 	struct intrhand *ih = arg;
    270 
    271 	if (!LEGAL_IRQ(ih->ih_pin))
    272 		panic("intr_disestablish: bogus irq");
    273 
    274 	intr_disestablish(ih);
    275 }
    276 
    277 void
    278 isa_attach_hook(struct device *parent, struct device *self,
    279     struct isabus_attach_args *iba)
    280 {
    281 	extern struct x86_isa_chipset x86_isa_chipset;
    282 	extern int isa_has_been_seen;
    283 
    284 	/*
    285 	 * Notify others that might need to know that the ISA bus
    286 	 * has now been attached.
    287 	 */
    288 	if (isa_has_been_seen)
    289 		panic("isaattach: ISA bus already seen!");
    290 	isa_has_been_seen = 1;
    291 
    292 	/*
    293 	 * Since we can only have one ISA bus, we just use a single
    294 	 * statically allocated ISA chipset structure.  Pass it up
    295 	 * now.
    296 	 */
    297 	iba->iba_ic = &x86_isa_chipset;
    298 }
    299 
    300 int
    301 isa_mem_alloc(t, size, align, boundary, flags, addrp, bshp)
    302 	bus_space_tag_t t;
    303 	bus_size_t size, align;
    304 	bus_addr_t boundary;
    305 	int flags;
    306 	bus_addr_t *addrp;
    307 	bus_space_handle_t *bshp;
    308 {
    309 
    310 	/*
    311 	 * Allocate physical address space in the ISA hole.
    312 	 */
    313 	return (bus_space_alloc(t, IOM_BEGIN, IOM_END - 1, size, align,
    314 	    boundary, flags, addrp, bshp));
    315 }
    316 
    317 void
    318 isa_mem_free(t, bsh, size)
    319 	bus_space_tag_t t;
    320 	bus_space_handle_t bsh;
    321 	bus_size_t size;
    322 {
    323 
    324 	bus_space_free(t, bsh, size);
    325 }
    326 
    327 /*
    328  * ISA only has 24-bits of address space.  This means
    329  * we can't DMA to pages over 16M.  In order to DMA to
    330  * arbitrary buffers, we use "bounce buffers" - pages
    331  * in memory below the 16M boundary.  On DMA reads,
    332  * DMA happens to the bounce buffers, and is copied into
    333  * the caller's buffer.  On writes, data is copied into
    334  * but bounce buffer, and the DMA happens from those
    335  * pages.  To software using the DMA mapping interface,
    336  * this looks simply like a data cache.
    337  *
    338  * If we have more than 16M of RAM in the system, we may
    339  * need bounce buffers.  We check and remember that here.
    340  *
    341  * There are exceptions, however.  VLB devices can do
    342  * 32-bit DMA, and indicate that here.
    343  *
    344  * ...or, there is an opposite case.  The most segments
    345  * a transfer will require is (maxxfer / PAGE_SIZE) + 1.  If
    346  * the caller can't handle that many segments (e.g. the
    347  * ISA DMA controller), we may have to bounce it as well.
    348  */
    349 static int
    350 _isa_dma_may_bounce(bus_dma_tag_t t, bus_dmamap_t map, int flags,
    351     int *cookieflagsp)
    352 {
    353 	if ((flags & ISABUS_DMA_32BIT) != 0)
    354 		map->_dm_bounce_thresh = 0;
    355 
    356 	if (((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt)
    357 		*cookieflagsp |= X86_DMA_MIGHT_NEED_BOUNCE;
    358 	return 0;
    359 }
    360