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