Home | History | Annotate | Line # | Download | only in isa
isa_machdep.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: isa_machdep.c,v 1.1 2003/02/27 00:28:08 fvdl Exp $	*/
      2  1.1  fvdl 
      3  1.1  fvdl /*-
      4  1.1  fvdl  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  1.1  fvdl  * All rights reserved.
      6  1.1  fvdl  *
      7  1.1  fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  fvdl  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  1.1  fvdl  * Simulation Facility, NASA Ames Research Center.
     10  1.1  fvdl  *
     11  1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     12  1.1  fvdl  * modification, are permitted provided that the following conditions
     13  1.1  fvdl  * are met:
     14  1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     15  1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     16  1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     19  1.1  fvdl  * 3. All advertising materials mentioning features or use of this software
     20  1.1  fvdl  *    must display the following acknowledgement:
     21  1.1  fvdl  *	This product includes software developed by the NetBSD
     22  1.1  fvdl  *	Foundation, Inc. and its contributors.
     23  1.1  fvdl  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  fvdl  *    contributors may be used to endorse or promote products derived
     25  1.1  fvdl  *    from this software without specific prior written permission.
     26  1.1  fvdl  *
     27  1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  fvdl  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  fvdl  */
     39  1.1  fvdl 
     40  1.1  fvdl /*-
     41  1.1  fvdl  * Copyright (c) 1991 The Regents of the University of California.
     42  1.1  fvdl  * All rights reserved.
     43  1.1  fvdl  *
     44  1.1  fvdl  * This code is derived from software contributed to Berkeley by
     45  1.1  fvdl  * William Jolitz.
     46  1.1  fvdl  *
     47  1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     48  1.1  fvdl  * modification, are permitted provided that the following conditions
     49  1.1  fvdl  * are met:
     50  1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     51  1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     52  1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     53  1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     54  1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     55  1.1  fvdl  * 3. All advertising materials mentioning features or use of this software
     56  1.1  fvdl  *    must display the following acknowledgement:
     57  1.1  fvdl  *	This product includes software developed by the University of
     58  1.1  fvdl  *	California, Berkeley and its contributors.
     59  1.1  fvdl  * 4. Neither the name of the University nor the names of its contributors
     60  1.1  fvdl  *    may be used to endorse or promote products derived from this software
     61  1.1  fvdl  *    without specific prior written permission.
     62  1.1  fvdl  *
     63  1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  1.1  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  1.1  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  1.1  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  1.1  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  1.1  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  1.1  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  1.1  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  1.1  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  1.1  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  1.1  fvdl  * SUCH DAMAGE.
     74  1.1  fvdl  *
     75  1.1  fvdl  *	@(#)isa.c	7.2 (Berkeley) 5/13/91
     76  1.1  fvdl  */
     77  1.1  fvdl 
     78  1.1  fvdl #include <sys/cdefs.h>
     79  1.1  fvdl __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.1 2003/02/27 00:28:08 fvdl Exp $");
     80  1.1  fvdl 
     81  1.1  fvdl #define ISA_DMA_STATS
     82  1.1  fvdl 
     83  1.1  fvdl #include <sys/param.h>
     84  1.1  fvdl #include <sys/systm.h>
     85  1.1  fvdl #include <sys/kernel.h>
     86  1.1  fvdl #include <sys/syslog.h>
     87  1.1  fvdl #include <sys/device.h>
     88  1.1  fvdl #include <sys/malloc.h>
     89  1.1  fvdl #include <sys/proc.h>
     90  1.1  fvdl #include <sys/mbuf.h>
     91  1.1  fvdl 
     92  1.1  fvdl #define _X86_BUS_DMA_PRIVATE
     93  1.1  fvdl #include <machine/bus.h>
     94  1.1  fvdl 
     95  1.1  fvdl #include <machine/pio.h>
     96  1.1  fvdl #include <machine/cpufunc.h>
     97  1.1  fvdl 
     98  1.1  fvdl #include <dev/isa/isareg.h>
     99  1.1  fvdl #include <dev/isa/isavar.h>
    100  1.1  fvdl 
    101  1.1  fvdl #include <uvm/uvm_extern.h>
    102  1.1  fvdl 
    103  1.1  fvdl #include "ioapic.h"
    104  1.1  fvdl 
    105  1.1  fvdl #if NIOAPIC > 0
    106  1.1  fvdl #include <machine/i82093var.h>
    107  1.1  fvdl #include <machine/mpbiosvar.h>
    108  1.1  fvdl #endif
    109  1.1  fvdl 
    110  1.1  fvdl #include "mca.h"
    111  1.1  fvdl #if NMCA > 0
    112  1.1  fvdl #include <machine/mca_machdep.h>		/* for MCA_system */
    113  1.1  fvdl #endif
    114  1.1  fvdl 
    115  1.1  fvdl #include "eisa.h"		/* XXX */
    116  1.1  fvdl 
    117  1.1  fvdl /*
    118  1.1  fvdl  * ISA can only DMA to 0-16M.
    119  1.1  fvdl  */
    120  1.1  fvdl #define	ISA_DMA_BOUNCE_THRESHOLD	(16 * 1024 * 1024)
    121  1.1  fvdl 
    122  1.1  fvdl extern	paddr_t avail_end;
    123  1.1  fvdl 
    124  1.1  fvdl #define	IDTVEC(name)	__CONCAT(X,name)
    125  1.1  fvdl typedef void (vector) __P((void));
    126  1.1  fvdl extern vector *IDTVEC(intr)[];
    127  1.1  fvdl 
    128  1.1  fvdl /*
    129  1.1  fvdl  * Cookie used by ISA dma.  A pointer to one of these it stashed in
    130  1.1  fvdl  * the DMA map.
    131  1.1  fvdl  */
    132  1.1  fvdl struct x86_isa_dma_cookie {
    133  1.1  fvdl 	int	id_flags;		/* flags; see below */
    134  1.1  fvdl 
    135  1.1  fvdl 	/*
    136  1.1  fvdl 	 * Information about the original buffer used during
    137  1.1  fvdl 	 * DMA map syncs.  Note that origibuflen is only used
    138  1.1  fvdl 	 * for ID_BUFTYPE_LINEAR.
    139  1.1  fvdl 	 */
    140  1.1  fvdl 	void	*id_origbuf;		/* pointer to orig buffer if
    141  1.1  fvdl 					   bouncing */
    142  1.1  fvdl 	bus_size_t id_origbuflen;	/* ...and size */
    143  1.1  fvdl 	int	id_buftype;		/* type of buffer */
    144  1.1  fvdl 
    145  1.1  fvdl 	void	*id_bouncebuf;		/* pointer to the bounce buffer */
    146  1.1  fvdl 	bus_size_t id_bouncebuflen;	/* ...and size */
    147  1.1  fvdl 	int	id_nbouncesegs;		/* number of valid bounce segs */
    148  1.1  fvdl 	bus_dma_segment_t id_bouncesegs[0]; /* array of bounce buffer
    149  1.1  fvdl 					       physical memory segments */
    150  1.1  fvdl };
    151  1.1  fvdl 
    152  1.1  fvdl /* id_flags */
    153  1.1  fvdl #define	ID_MIGHT_NEED_BOUNCE	0x01	/* map could need bounce buffers */
    154  1.1  fvdl #define	ID_HAS_BOUNCE		0x02	/* map currently has bounce buffers */
    155  1.1  fvdl #define	ID_IS_BOUNCING		0x04	/* map is bouncing current xfer */
    156  1.1  fvdl 
    157  1.1  fvdl /* id_buftype */
    158  1.1  fvdl #define	ID_BUFTYPE_INVALID	0
    159  1.1  fvdl #define	ID_BUFTYPE_LINEAR	1
    160  1.1  fvdl #define	ID_BUFTYPE_MBUF		2
    161  1.1  fvdl #define	ID_BUFTYPE_UIO		3
    162  1.1  fvdl #define	ID_BUFTYPE_RAW		4
    163  1.1  fvdl 
    164  1.1  fvdl int	_isa_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
    165  1.1  fvdl 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
    166  1.1  fvdl void	_isa_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
    167  1.1  fvdl int	_isa_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
    168  1.1  fvdl 	    bus_size_t, struct proc *, int));
    169  1.1  fvdl int	_isa_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
    170  1.1  fvdl 	    struct mbuf *, int));
    171  1.1  fvdl int	_isa_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
    172  1.1  fvdl 	    struct uio *, int));
    173  1.1  fvdl int	_isa_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
    174  1.1  fvdl 	    bus_dma_segment_t *, int, bus_size_t, int));
    175  1.1  fvdl void	_isa_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    176  1.1  fvdl void	_isa_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
    177  1.1  fvdl 	    bus_addr_t, bus_size_t, int));
    178  1.1  fvdl 
    179  1.1  fvdl int	_isa_bus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t,
    180  1.1  fvdl 	    bus_size_t, bus_dma_segment_t *, int, int *, int));
    181  1.1  fvdl 
    182  1.1  fvdl int	_isa_dma_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
    183  1.1  fvdl 	    bus_size_t, int));
    184  1.1  fvdl void	_isa_dma_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
    185  1.1  fvdl 
    186  1.1  fvdl /*
    187  1.1  fvdl  * Entry points for ISA DMA.  These are mostly wrappers around
    188  1.1  fvdl  * the generic functions that understand how to deal with bounce
    189  1.1  fvdl  * buffers, if necessary.
    190  1.1  fvdl  */
    191  1.1  fvdl struct x86_bus_dma_tag isa_bus_dma_tag = {
    192  1.1  fvdl 	ISA_DMA_BOUNCE_THRESHOLD,
    193  1.1  fvdl 	_isa_bus_dmamap_create,
    194  1.1  fvdl 	_isa_bus_dmamap_destroy,
    195  1.1  fvdl 	_isa_bus_dmamap_load,
    196  1.1  fvdl 	_isa_bus_dmamap_load_mbuf,
    197  1.1  fvdl 	_isa_bus_dmamap_load_uio,
    198  1.1  fvdl 	_isa_bus_dmamap_load_raw,
    199  1.1  fvdl 	_isa_bus_dmamap_unload,
    200  1.1  fvdl 	_isa_bus_dmamap_sync,
    201  1.1  fvdl 	_isa_bus_dmamem_alloc,
    202  1.1  fvdl 	_bus_dmamem_free,
    203  1.1  fvdl 	_bus_dmamem_map,
    204  1.1  fvdl 	_bus_dmamem_unmap,
    205  1.1  fvdl 	_bus_dmamem_mmap,
    206  1.1  fvdl };
    207  1.1  fvdl 
    208  1.1  fvdl #define	LEGAL_IRQ(x)	((x) >= 0 && (x) < NUM_LEGACY_IRQS && (x) != 2)
    209  1.1  fvdl 
    210  1.1  fvdl int
    211  1.1  fvdl isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
    212  1.1  fvdl {
    213  1.1  fvdl 	int i, tmp, bestirq, count;
    214  1.1  fvdl 	struct intrhand **p, *q;
    215  1.1  fvdl 	struct intrsource *isp;
    216  1.1  fvdl 	struct cpu_info *ci;
    217  1.1  fvdl 
    218  1.1  fvdl 	if (type == IST_NONE)
    219  1.1  fvdl 		panic("intr_alloc: bogus type");
    220  1.1  fvdl 
    221  1.1  fvdl 	ci = &cpu_info_primary;
    222  1.1  fvdl 
    223  1.1  fvdl 	bestirq = -1;
    224  1.1  fvdl 	count = -1;
    225  1.1  fvdl 
    226  1.1  fvdl 	/* some interrupts should never be dynamically allocated */
    227  1.1  fvdl 	mask &= 0xdef8;
    228  1.1  fvdl 
    229  1.1  fvdl 	/*
    230  1.1  fvdl 	 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
    231  1.1  fvdl 	 * the right answer is to do "breadth-first" searching of devices.
    232  1.1  fvdl 	 */
    233  1.1  fvdl 	mask &= 0xefbf;
    234  1.1  fvdl 
    235  1.1  fvdl 	simple_lock(&ci->ci_slock);
    236  1.1  fvdl 
    237  1.1  fvdl 	for (i = 0; i < NUM_LEGACY_IRQS; i++) {
    238  1.1  fvdl 		if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
    239  1.1  fvdl 			continue;
    240  1.1  fvdl 		isp = ci->ci_isources[i];
    241  1.1  fvdl 		if (isp == NULL) {
    242  1.1  fvdl 			/*
    243  1.1  fvdl 			 * if nothing's using the irq, just return it
    244  1.1  fvdl 			 */
    245  1.1  fvdl 			*irq = i;
    246  1.1  fvdl 			simple_unlock(&ci->ci_slock);
    247  1.1  fvdl 			return (0);
    248  1.1  fvdl 		}
    249  1.1  fvdl 
    250  1.1  fvdl 		switch(isp->is_type) {
    251  1.1  fvdl 		case IST_EDGE:
    252  1.1  fvdl 		case IST_LEVEL:
    253  1.1  fvdl 			if (type != isp->is_type)
    254  1.1  fvdl 				continue;
    255  1.1  fvdl 			/*
    256  1.1  fvdl 			 * if the irq is shareable, count the number of other
    257  1.1  fvdl 			 * handlers, and if it's smaller than the last irq like
    258  1.1  fvdl 			 * this, remember it
    259  1.1  fvdl 			 *
    260  1.1  fvdl 			 * XXX We should probably also consider the
    261  1.1  fvdl 			 * interrupt level and stick IPL_TTY with other
    262  1.1  fvdl 			 * IPL_TTY, etc.
    263  1.1  fvdl 			 */
    264  1.1  fvdl 			for (p = &isp->is_handlers, tmp = 0; (q = *p) != NULL;
    265  1.1  fvdl 			     p = &q->ih_next, tmp++)
    266  1.1  fvdl 				;
    267  1.1  fvdl 			if ((bestirq == -1) || (count > tmp)) {
    268  1.1  fvdl 				bestirq = i;
    269  1.1  fvdl 				count = tmp;
    270  1.1  fvdl 			}
    271  1.1  fvdl 			break;
    272  1.1  fvdl 
    273  1.1  fvdl 		case IST_PULSE:
    274  1.1  fvdl 			/* this just isn't shareable */
    275  1.1  fvdl 			continue;
    276  1.1  fvdl 		}
    277  1.1  fvdl 	}
    278  1.1  fvdl 
    279  1.1  fvdl 	simple_unlock(&ci->ci_slock);
    280  1.1  fvdl 
    281  1.1  fvdl 	if (bestirq == -1)
    282  1.1  fvdl 		return (1);
    283  1.1  fvdl 
    284  1.1  fvdl 	*irq = bestirq;
    285  1.1  fvdl 
    286  1.1  fvdl 	return (0);
    287  1.1  fvdl }
    288  1.1  fvdl 
    289  1.1  fvdl const struct evcnt *
    290  1.1  fvdl isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
    291  1.1  fvdl {
    292  1.1  fvdl 
    293  1.1  fvdl 	/* XXX for now, no evcnt parent reported */
    294  1.1  fvdl 	return NULL;
    295  1.1  fvdl }
    296  1.1  fvdl 
    297  1.1  fvdl void *
    298  1.1  fvdl isa_intr_establish(ic, irq, type, level, ih_fun, ih_arg)
    299  1.1  fvdl 	isa_chipset_tag_t ic;
    300  1.1  fvdl 	int irq;
    301  1.1  fvdl 	int type;
    302  1.1  fvdl 	int level;
    303  1.1  fvdl 	int (*ih_fun) __P((void *));
    304  1.1  fvdl 	void *ih_arg;
    305  1.1  fvdl {
    306  1.1  fvdl 	struct pic *pic;
    307  1.1  fvdl 	int pin;
    308  1.1  fvdl #if NIOAPIC > 0
    309  1.1  fvdl 	int mpih;
    310  1.1  fvdl #endif
    311  1.1  fvdl 
    312  1.1  fvdl 	pin = irq;
    313  1.1  fvdl 	pic = &i8259_pic;
    314  1.1  fvdl 
    315  1.1  fvdl #if NIOAPIC > 0
    316  1.1  fvdl 	if (mp_busses != NULL) {
    317  1.1  fvdl 		if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 ||
    318  1.1  fvdl 		    intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) {
    319  1.1  fvdl 			if (!APIC_IRQ_ISLEGACY(mpih)) {
    320  1.1  fvdl 				pin = APIC_IRQ_PIN(mpih);
    321  1.1  fvdl 				pic = (struct pic *)
    322  1.1  fvdl 				    ioapic_find(APIC_IRQ_APIC(mpih));
    323  1.1  fvdl 				if (pic == NULL) {
    324  1.1  fvdl 					printf("isa_intr_establish: "
    325  1.1  fvdl 					       "unknown apic %d\n",
    326  1.1  fvdl 					    APIC_IRQ_APIC(mpih));
    327  1.1  fvdl 					return NULL;
    328  1.1  fvdl 				}
    329  1.1  fvdl 			}
    330  1.1  fvdl 		} else
    331  1.1  fvdl 			printf("isa_intr_establish: no MP mapping found\n");
    332  1.1  fvdl 	}
    333  1.1  fvdl #endif
    334  1.1  fvdl 	return intr_establish(irq, pic, pin, type, level, ih_fun, ih_arg);
    335  1.1  fvdl }
    336  1.1  fvdl 
    337  1.1  fvdl /*
    338  1.1  fvdl  * Deregister an interrupt handler.
    339  1.1  fvdl  */
    340  1.1  fvdl void
    341  1.1  fvdl isa_intr_disestablish(ic, arg)
    342  1.1  fvdl 	isa_chipset_tag_t ic;
    343  1.1  fvdl 	void *arg;
    344  1.1  fvdl {
    345  1.1  fvdl 	struct intrhand *ih = arg;
    346  1.1  fvdl 
    347  1.1  fvdl 	if (!LEGAL_IRQ(ih->ih_pin))
    348  1.1  fvdl 		panic("intr_disestablish: bogus irq");
    349  1.1  fvdl 
    350  1.1  fvdl 	intr_disestablish(ih);
    351  1.1  fvdl }
    352  1.1  fvdl 
    353  1.1  fvdl void
    354  1.1  fvdl isa_attach_hook(parent, self, iba)
    355  1.1  fvdl 	struct device *parent, *self;
    356  1.1  fvdl 	struct isabus_attach_args *iba;
    357  1.1  fvdl {
    358  1.1  fvdl 	extern struct x86_isa_chipset i386_isa_chipset;
    359  1.1  fvdl 	extern int isa_has_been_seen;
    360  1.1  fvdl 
    361  1.1  fvdl 	/*
    362  1.1  fvdl 	 * Notify others that might need to know that the ISA bus
    363  1.1  fvdl 	 * has now been attached.
    364  1.1  fvdl 	 */
    365  1.1  fvdl 	if (isa_has_been_seen)
    366  1.1  fvdl 		panic("isaattach: ISA bus already seen!");
    367  1.1  fvdl 	isa_has_been_seen = 1;
    368  1.1  fvdl 
    369  1.1  fvdl 	/*
    370  1.1  fvdl 	 * Since we can only have one ISA bus, we just use a single
    371  1.1  fvdl 	 * statically allocated ISA chipset structure.  Pass it up
    372  1.1  fvdl 	 * now.
    373  1.1  fvdl 	 */
    374  1.1  fvdl 	iba->iba_ic = &i386_isa_chipset;
    375  1.1  fvdl }
    376  1.1  fvdl 
    377  1.1  fvdl int
    378  1.1  fvdl isa_mem_alloc(t, size, align, boundary, flags, addrp, bshp)
    379  1.1  fvdl 	bus_space_tag_t t;
    380  1.1  fvdl 	bus_size_t size, align;
    381  1.1  fvdl 	bus_addr_t boundary;
    382  1.1  fvdl 	int flags;
    383  1.1  fvdl 	bus_addr_t *addrp;
    384  1.1  fvdl 	bus_space_handle_t *bshp;
    385  1.1  fvdl {
    386  1.1  fvdl 
    387  1.1  fvdl 	/*
    388  1.1  fvdl 	 * Allocate physical address space in the ISA hole.
    389  1.1  fvdl 	 */
    390  1.1  fvdl 	return (bus_space_alloc(t, IOM_BEGIN, IOM_END - 1, size, align,
    391  1.1  fvdl 	    boundary, flags, addrp, bshp));
    392  1.1  fvdl }
    393  1.1  fvdl 
    394  1.1  fvdl void
    395  1.1  fvdl isa_mem_free(t, bsh, size)
    396  1.1  fvdl 	bus_space_tag_t t;
    397  1.1  fvdl 	bus_space_handle_t bsh;
    398  1.1  fvdl 	bus_size_t size;
    399  1.1  fvdl {
    400  1.1  fvdl 
    401  1.1  fvdl 	bus_space_free(t, bsh, size);
    402  1.1  fvdl }
    403  1.1  fvdl 
    404  1.1  fvdl /**********************************************************************
    405  1.1  fvdl  * bus.h dma interface entry points
    406  1.1  fvdl  **********************************************************************/
    407  1.1  fvdl 
    408  1.1  fvdl #ifdef ISA_DMA_STATS
    409  1.1  fvdl #define	STAT_INCR(v)	(v)++
    410  1.1  fvdl #define	STAT_DECR(v)	do { \
    411  1.1  fvdl 		if ((v) == 0) \
    412  1.1  fvdl 			printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
    413  1.1  fvdl 		else \
    414  1.1  fvdl 			(v)--; \
    415  1.1  fvdl 		} while (0)
    416  1.1  fvdl u_long	isa_dma_stats_loads;
    417  1.1  fvdl u_long	isa_dma_stats_bounces;
    418  1.1  fvdl u_long	isa_dma_stats_nbouncebufs;
    419  1.1  fvdl #else
    420  1.1  fvdl #define	STAT_INCR(v)
    421  1.1  fvdl #define	STAT_DECR(v)
    422  1.1  fvdl #endif
    423  1.1  fvdl 
    424  1.1  fvdl /*
    425  1.1  fvdl  * Create an ISA DMA map.
    426  1.1  fvdl  */
    427  1.1  fvdl int
    428  1.1  fvdl _isa_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
    429  1.1  fvdl 	bus_dma_tag_t t;
    430  1.1  fvdl 	bus_size_t size;
    431  1.1  fvdl 	int nsegments;
    432  1.1  fvdl 	bus_size_t maxsegsz;
    433  1.1  fvdl 	bus_size_t boundary;
    434  1.1  fvdl 	int flags;
    435  1.1  fvdl 	bus_dmamap_t *dmamp;
    436  1.1  fvdl {
    437  1.1  fvdl 	struct x86_isa_dma_cookie *cookie;
    438  1.1  fvdl 	bus_dmamap_t map;
    439  1.1  fvdl 	int error, cookieflags;
    440  1.1  fvdl 	void *cookiestore;
    441  1.1  fvdl 	size_t cookiesize;
    442  1.1  fvdl 
    443  1.1  fvdl 	/* Call common function to create the basic map. */
    444  1.1  fvdl 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    445  1.1  fvdl 	    flags, dmamp);
    446  1.1  fvdl 	if (error)
    447  1.1  fvdl 		return (error);
    448  1.1  fvdl 
    449  1.1  fvdl 	map = *dmamp;
    450  1.1  fvdl 	map->_dm_cookie = NULL;
    451  1.1  fvdl 
    452  1.1  fvdl 	cookiesize = sizeof(struct x86_isa_dma_cookie);
    453  1.1  fvdl 
    454  1.1  fvdl 	/*
    455  1.1  fvdl 	 * ISA only has 24-bits of address space.  This means
    456  1.1  fvdl 	 * we can't DMA to pages over 16M.  In order to DMA to
    457  1.1  fvdl 	 * arbitrary buffers, we use "bounce buffers" - pages
    458  1.1  fvdl 	 * in memory below the 16M boundary.  On DMA reads,
    459  1.1  fvdl 	 * DMA happens to the bounce buffers, and is copied into
    460  1.1  fvdl 	 * the caller's buffer.  On writes, data is copied into
    461  1.1  fvdl 	 * but bounce buffer, and the DMA happens from those
    462  1.1  fvdl 	 * pages.  To software using the DMA mapping interface,
    463  1.1  fvdl 	 * this looks simply like a data cache.
    464  1.1  fvdl 	 *
    465  1.1  fvdl 	 * If we have more than 16M of RAM in the system, we may
    466  1.1  fvdl 	 * need bounce buffers.  We check and remember that here.
    467  1.1  fvdl 	 *
    468  1.1  fvdl 	 * There are exceptions, however.  VLB devices can do
    469  1.1  fvdl 	 * 32-bit DMA, and indicate that here.
    470  1.1  fvdl 	 *
    471  1.1  fvdl 	 * ...or, there is an opposite case.  The most segments
    472  1.1  fvdl 	 * a transfer will require is (maxxfer / PAGE_SIZE) + 1.  If
    473  1.1  fvdl 	 * the caller can't handle that many segments (e.g. the
    474  1.1  fvdl 	 * ISA DMA controller), we may have to bounce it as well.
    475  1.1  fvdl 	 */
    476  1.1  fvdl 	if (avail_end <= t->_bounce_thresh ||
    477  1.1  fvdl 	    (flags & ISABUS_DMA_32BIT) != 0) {
    478  1.1  fvdl 		/* Bouncing not necessary due to memory size. */
    479  1.1  fvdl 		map->_dm_bounce_thresh = 0;
    480  1.1  fvdl 	}
    481  1.1  fvdl 	cookieflags = 0;
    482  1.1  fvdl 	if (map->_dm_bounce_thresh != 0 ||
    483  1.1  fvdl 	    ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
    484  1.1  fvdl 		cookieflags |= ID_MIGHT_NEED_BOUNCE;
    485  1.1  fvdl 		cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
    486  1.1  fvdl 	}
    487  1.1  fvdl 
    488  1.1  fvdl 	/*
    489  1.1  fvdl 	 * Allocate our cookie.
    490  1.1  fvdl 	 */
    491  1.1  fvdl 	if ((cookiestore = malloc(cookiesize, M_DMAMAP,
    492  1.1  fvdl 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
    493  1.1  fvdl 		error = ENOMEM;
    494  1.1  fvdl 		goto out;
    495  1.1  fvdl 	}
    496  1.1  fvdl 	memset(cookiestore, 0, cookiesize);
    497  1.1  fvdl 	cookie = (struct x86_isa_dma_cookie *)cookiestore;
    498  1.1  fvdl 	cookie->id_flags = cookieflags;
    499  1.1  fvdl 	map->_dm_cookie = cookie;
    500  1.1  fvdl 
    501  1.1  fvdl 	if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
    502  1.1  fvdl 		/*
    503  1.1  fvdl 		 * Allocate the bounce pages now if the caller
    504  1.1  fvdl 		 * wishes us to do so.
    505  1.1  fvdl 		 */
    506  1.1  fvdl 		if ((flags & BUS_DMA_ALLOCNOW) == 0)
    507  1.1  fvdl 			goto out;
    508  1.1  fvdl 
    509  1.1  fvdl 		error = _isa_dma_alloc_bouncebuf(t, map, size, flags);
    510  1.1  fvdl 	}
    511  1.1  fvdl 
    512  1.1  fvdl  out:
    513  1.1  fvdl 	if (error) {
    514  1.1  fvdl 		if (map->_dm_cookie != NULL)
    515  1.1  fvdl 			free(map->_dm_cookie, M_DMAMAP);
    516  1.1  fvdl 		_bus_dmamap_destroy(t, map);
    517  1.1  fvdl 	}
    518  1.1  fvdl 	return (error);
    519  1.1  fvdl }
    520  1.1  fvdl 
    521  1.1  fvdl /*
    522  1.1  fvdl  * Destroy an ISA DMA map.
    523  1.1  fvdl  */
    524  1.1  fvdl void
    525  1.1  fvdl _isa_bus_dmamap_destroy(t, map)
    526  1.1  fvdl 	bus_dma_tag_t t;
    527  1.1  fvdl 	bus_dmamap_t map;
    528  1.1  fvdl {
    529  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    530  1.1  fvdl 
    531  1.1  fvdl 	/*
    532  1.1  fvdl 	 * Free any bounce pages this map might hold.
    533  1.1  fvdl 	 */
    534  1.1  fvdl 	if (cookie->id_flags & ID_HAS_BOUNCE)
    535  1.1  fvdl 		_isa_dma_free_bouncebuf(t, map);
    536  1.1  fvdl 
    537  1.1  fvdl 	free(cookie, M_DMAMAP);
    538  1.1  fvdl 	_bus_dmamap_destroy(t, map);
    539  1.1  fvdl }
    540  1.1  fvdl 
    541  1.1  fvdl /*
    542  1.1  fvdl  * Load an ISA DMA map with a linear buffer.
    543  1.1  fvdl  */
    544  1.1  fvdl int
    545  1.1  fvdl _isa_bus_dmamap_load(t, map, buf, buflen, p, flags)
    546  1.1  fvdl 	bus_dma_tag_t t;
    547  1.1  fvdl 	bus_dmamap_t map;
    548  1.1  fvdl 	void *buf;
    549  1.1  fvdl 	bus_size_t buflen;
    550  1.1  fvdl 	struct proc *p;
    551  1.1  fvdl 	int flags;
    552  1.1  fvdl {
    553  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    554  1.1  fvdl 	int error;
    555  1.1  fvdl 
    556  1.1  fvdl 	STAT_INCR(isa_dma_stats_loads);
    557  1.1  fvdl 
    558  1.1  fvdl 	/*
    559  1.1  fvdl 	 * Make sure that on error condition we return "no valid mappings."
    560  1.1  fvdl 	 */
    561  1.1  fvdl 	map->dm_mapsize = 0;
    562  1.1  fvdl 	map->dm_nsegs = 0;
    563  1.1  fvdl 
    564  1.1  fvdl 	/*
    565  1.1  fvdl 	 * Try to load the map the normal way.  If this errors out,
    566  1.1  fvdl 	 * and we can bounce, we will.
    567  1.1  fvdl 	 */
    568  1.1  fvdl 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    569  1.1  fvdl 	if (error == 0 ||
    570  1.1  fvdl 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    571  1.1  fvdl 		return (error);
    572  1.1  fvdl 
    573  1.1  fvdl 	/*
    574  1.1  fvdl 	 * First attempt failed; bounce it.
    575  1.1  fvdl 	 */
    576  1.1  fvdl 
    577  1.1  fvdl 	STAT_INCR(isa_dma_stats_bounces);
    578  1.1  fvdl 
    579  1.1  fvdl 	/*
    580  1.1  fvdl 	 * Allocate bounce pages, if necessary.
    581  1.1  fvdl 	 */
    582  1.1  fvdl 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    583  1.1  fvdl 		error = _isa_dma_alloc_bouncebuf(t, map, buflen, flags);
    584  1.1  fvdl 		if (error)
    585  1.1  fvdl 			return (error);
    586  1.1  fvdl 	}
    587  1.1  fvdl 
    588  1.1  fvdl 	/*
    589  1.1  fvdl 	 * Cache a pointer to the caller's buffer and load the DMA map
    590  1.1  fvdl 	 * with the bounce buffer.
    591  1.1  fvdl 	 */
    592  1.1  fvdl 	cookie->id_origbuf = buf;
    593  1.1  fvdl 	cookie->id_origbuflen = buflen;
    594  1.1  fvdl 	cookie->id_buftype = ID_BUFTYPE_LINEAR;
    595  1.1  fvdl 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
    596  1.1  fvdl 	    p, flags);
    597  1.1  fvdl 	if (error) {
    598  1.1  fvdl 		/*
    599  1.1  fvdl 		 * Free the bounce pages, unless our resources
    600  1.1  fvdl 		 * are reserved for our exclusive use.
    601  1.1  fvdl 		 */
    602  1.1  fvdl 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    603  1.1  fvdl 			_isa_dma_free_bouncebuf(t, map);
    604  1.1  fvdl 		return (error);
    605  1.1  fvdl 	}
    606  1.1  fvdl 
    607  1.1  fvdl 	/* ...so _isa_bus_dmamap_sync() knows we're bouncing */
    608  1.1  fvdl 	cookie->id_flags |= ID_IS_BOUNCING;
    609  1.1  fvdl 	return (0);
    610  1.1  fvdl }
    611  1.1  fvdl 
    612  1.1  fvdl /*
    613  1.1  fvdl  * Like _isa_bus_dmamap_load(), but for mbufs.
    614  1.1  fvdl  */
    615  1.1  fvdl int
    616  1.1  fvdl _isa_bus_dmamap_load_mbuf(t, map, m0, flags)
    617  1.1  fvdl 	bus_dma_tag_t t;
    618  1.1  fvdl 	bus_dmamap_t map;
    619  1.1  fvdl 	struct mbuf *m0;
    620  1.1  fvdl 	int flags;
    621  1.1  fvdl {
    622  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    623  1.1  fvdl 	int error;
    624  1.1  fvdl 
    625  1.1  fvdl 	/*
    626  1.1  fvdl 	 * Make sure on error condition we return "no valid mappings."
    627  1.1  fvdl 	 */
    628  1.1  fvdl 	map->dm_mapsize = 0;
    629  1.1  fvdl 	map->dm_nsegs = 0;
    630  1.1  fvdl 
    631  1.1  fvdl #ifdef DIAGNOSTIC
    632  1.1  fvdl 	if ((m0->m_flags & M_PKTHDR) == 0)
    633  1.1  fvdl 		panic("_isa_bus_dmamap_load_mbuf: no packet header");
    634  1.1  fvdl #endif
    635  1.1  fvdl 
    636  1.1  fvdl 	if (m0->m_pkthdr.len > map->_dm_size)
    637  1.1  fvdl 		return (EINVAL);
    638  1.1  fvdl 
    639  1.1  fvdl 	/*
    640  1.1  fvdl 	 * Try to load the map the normal way.  If this errors out,
    641  1.1  fvdl 	 * and we can bounce, we will.
    642  1.1  fvdl 	 */
    643  1.1  fvdl 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
    644  1.1  fvdl 	if (error == 0 ||
    645  1.1  fvdl 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    646  1.1  fvdl 		return (error);
    647  1.1  fvdl 
    648  1.1  fvdl 	/*
    649  1.1  fvdl 	 * First attempt failed; bounce it.
    650  1.1  fvdl 	 */
    651  1.1  fvdl 
    652  1.1  fvdl 	STAT_INCR(isa_dma_stats_bounces);
    653  1.1  fvdl 
    654  1.1  fvdl 	/*
    655  1.1  fvdl 	 * Allocate bounce pages, if necessary.
    656  1.1  fvdl 	 */
    657  1.1  fvdl 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    658  1.1  fvdl 		error = _isa_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
    659  1.1  fvdl 		    flags);
    660  1.1  fvdl 		if (error)
    661  1.1  fvdl 			return (error);
    662  1.1  fvdl 	}
    663  1.1  fvdl 
    664  1.1  fvdl 	/*
    665  1.1  fvdl 	 * Cache a pointer to the caller's buffer and load the DMA map
    666  1.1  fvdl 	 * with the bounce buffer.
    667  1.1  fvdl 	 */
    668  1.1  fvdl 	cookie->id_origbuf = m0;
    669  1.1  fvdl 	cookie->id_origbuflen = m0->m_pkthdr.len;	/* not really used */
    670  1.1  fvdl 	cookie->id_buftype = ID_BUFTYPE_MBUF;
    671  1.1  fvdl 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
    672  1.1  fvdl 	    m0->m_pkthdr.len, NULL, flags);
    673  1.1  fvdl 	if (error) {
    674  1.1  fvdl 		/*
    675  1.1  fvdl 		 * Free the bounce pages, unless our resources
    676  1.1  fvdl 		 * are reserved for our exclusive use.
    677  1.1  fvdl 		 */
    678  1.1  fvdl 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    679  1.1  fvdl 			_isa_dma_free_bouncebuf(t, map);
    680  1.1  fvdl 		return (error);
    681  1.1  fvdl 	}
    682  1.1  fvdl 
    683  1.1  fvdl 	/* ...so _isa_bus_dmamap_sync() knows we're bouncing */
    684  1.1  fvdl 	cookie->id_flags |= ID_IS_BOUNCING;
    685  1.1  fvdl 	return (0);
    686  1.1  fvdl }
    687  1.1  fvdl 
    688  1.1  fvdl /*
    689  1.1  fvdl  * Like _isa_bus_dmamap_load(), but for uios.
    690  1.1  fvdl  */
    691  1.1  fvdl int
    692  1.1  fvdl _isa_bus_dmamap_load_uio(t, map, uio, flags)
    693  1.1  fvdl 	bus_dma_tag_t t;
    694  1.1  fvdl 	bus_dmamap_t map;
    695  1.1  fvdl 	struct uio *uio;
    696  1.1  fvdl 	int flags;
    697  1.1  fvdl {
    698  1.1  fvdl 
    699  1.1  fvdl 	panic("_isa_bus_dmamap_load_uio: not implemented");
    700  1.1  fvdl }
    701  1.1  fvdl 
    702  1.1  fvdl /*
    703  1.1  fvdl  * Like _isa_bus_dmamap_load(), but for raw memory allocated with
    704  1.1  fvdl  * bus_dmamem_alloc().
    705  1.1  fvdl  */
    706  1.1  fvdl int
    707  1.1  fvdl _isa_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    708  1.1  fvdl 	bus_dma_tag_t t;
    709  1.1  fvdl 	bus_dmamap_t map;
    710  1.1  fvdl 	bus_dma_segment_t *segs;
    711  1.1  fvdl 	int nsegs;
    712  1.1  fvdl 	bus_size_t size;
    713  1.1  fvdl 	int flags;
    714  1.1  fvdl {
    715  1.1  fvdl 
    716  1.1  fvdl 	panic("_isa_bus_dmamap_load_raw: not implemented");
    717  1.1  fvdl }
    718  1.1  fvdl 
    719  1.1  fvdl /*
    720  1.1  fvdl  * Unload an ISA DMA map.
    721  1.1  fvdl  */
    722  1.1  fvdl void
    723  1.1  fvdl _isa_bus_dmamap_unload(t, map)
    724  1.1  fvdl 	bus_dma_tag_t t;
    725  1.1  fvdl 	bus_dmamap_t map;
    726  1.1  fvdl {
    727  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    728  1.1  fvdl 
    729  1.1  fvdl 	/*
    730  1.1  fvdl 	 * If we have bounce pages, free them, unless they're
    731  1.1  fvdl 	 * reserved for our exclusive use.
    732  1.1  fvdl 	 */
    733  1.1  fvdl 	if ((cookie->id_flags & ID_HAS_BOUNCE) &&
    734  1.1  fvdl 	    (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    735  1.1  fvdl 		_isa_dma_free_bouncebuf(t, map);
    736  1.1  fvdl 
    737  1.1  fvdl 	cookie->id_flags &= ~ID_IS_BOUNCING;
    738  1.1  fvdl 	cookie->id_buftype = ID_BUFTYPE_INVALID;
    739  1.1  fvdl 
    740  1.1  fvdl 	/*
    741  1.1  fvdl 	 * Do the generic bits of the unload.
    742  1.1  fvdl 	 */
    743  1.1  fvdl 	_bus_dmamap_unload(t, map);
    744  1.1  fvdl }
    745  1.1  fvdl 
    746  1.1  fvdl /*
    747  1.1  fvdl  * Synchronize an ISA DMA map.
    748  1.1  fvdl  */
    749  1.1  fvdl void
    750  1.1  fvdl _isa_bus_dmamap_sync(t, map, offset, len, ops)
    751  1.1  fvdl 	bus_dma_tag_t t;
    752  1.1  fvdl 	bus_dmamap_t map;
    753  1.1  fvdl 	bus_addr_t offset;
    754  1.1  fvdl 	bus_size_t len;
    755  1.1  fvdl 	int ops;
    756  1.1  fvdl {
    757  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    758  1.1  fvdl 
    759  1.1  fvdl 	/*
    760  1.1  fvdl 	 * Mixing PRE and POST operations is not allowed.
    761  1.1  fvdl 	 */
    762  1.1  fvdl 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    763  1.1  fvdl 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    764  1.1  fvdl 		panic("_isa_bus_dmamap_sync: mix PRE and POST");
    765  1.1  fvdl 
    766  1.1  fvdl #ifdef DIAGNOSTIC
    767  1.1  fvdl 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    768  1.1  fvdl 		if (offset >= map->dm_mapsize)
    769  1.1  fvdl 			panic("_isa_bus_dmamap_sync: bad offset");
    770  1.1  fvdl 		if (len == 0 || (offset + len) > map->dm_mapsize)
    771  1.1  fvdl 			panic("_isa_bus_dmamap_sync: bad length");
    772  1.1  fvdl 	}
    773  1.1  fvdl #endif
    774  1.1  fvdl 
    775  1.1  fvdl 	/*
    776  1.1  fvdl 	 * If we're not bouncing, just return; nothing to do.
    777  1.1  fvdl 	 */
    778  1.1  fvdl 	if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
    779  1.1  fvdl 		return;
    780  1.1  fvdl 
    781  1.1  fvdl 	switch (cookie->id_buftype) {
    782  1.1  fvdl 	case ID_BUFTYPE_LINEAR:
    783  1.1  fvdl 		/*
    784  1.1  fvdl 		 * Nothing to do for pre-read.
    785  1.1  fvdl 		 */
    786  1.1  fvdl 
    787  1.1  fvdl 		if (ops & BUS_DMASYNC_PREWRITE) {
    788  1.1  fvdl 			/*
    789  1.1  fvdl 			 * Copy the caller's buffer to the bounce buffer.
    790  1.1  fvdl 			 */
    791  1.1  fvdl 			memcpy((char *)cookie->id_bouncebuf + offset,
    792  1.1  fvdl 			    (char *)cookie->id_origbuf + offset, len);
    793  1.1  fvdl 		}
    794  1.1  fvdl 
    795  1.1  fvdl 		if (ops & BUS_DMASYNC_POSTREAD) {
    796  1.1  fvdl 			/*
    797  1.1  fvdl 			 * Copy the bounce buffer to the caller's buffer.
    798  1.1  fvdl 			 */
    799  1.1  fvdl 			memcpy((char *)cookie->id_origbuf + offset,
    800  1.1  fvdl 			    (char *)cookie->id_bouncebuf + offset, len);
    801  1.1  fvdl 		}
    802  1.1  fvdl 
    803  1.1  fvdl 		/*
    804  1.1  fvdl 		 * Nothing to do for post-write.
    805  1.1  fvdl 		 */
    806  1.1  fvdl 		break;
    807  1.1  fvdl 
    808  1.1  fvdl 	case ID_BUFTYPE_MBUF:
    809  1.1  fvdl 	    {
    810  1.1  fvdl 		struct mbuf *m, *m0 = cookie->id_origbuf;
    811  1.1  fvdl 		bus_size_t minlen, moff;
    812  1.1  fvdl 
    813  1.1  fvdl 		/*
    814  1.1  fvdl 		 * Nothing to do for pre-read.
    815  1.1  fvdl 		 */
    816  1.1  fvdl 
    817  1.1  fvdl 		if (ops & BUS_DMASYNC_PREWRITE) {
    818  1.1  fvdl 			/*
    819  1.1  fvdl 			 * Copy the caller's buffer to the bounce buffer.
    820  1.1  fvdl 			 */
    821  1.1  fvdl 			m_copydata(m0, offset, len,
    822  1.1  fvdl 			    (char *)cookie->id_bouncebuf + offset);
    823  1.1  fvdl 		}
    824  1.1  fvdl 
    825  1.1  fvdl 		if (ops & BUS_DMASYNC_POSTREAD) {
    826  1.1  fvdl 			/*
    827  1.1  fvdl 			 * Copy the bounce buffer to the caller's buffer.
    828  1.1  fvdl 			 */
    829  1.1  fvdl 			for (moff = offset, m = m0; m != NULL && len != 0;
    830  1.1  fvdl 			     m = m->m_next) {
    831  1.1  fvdl 				/* Find the beginning mbuf. */
    832  1.1  fvdl 				if (moff >= m->m_len) {
    833  1.1  fvdl 					moff -= m->m_len;
    834  1.1  fvdl 					continue;
    835  1.1  fvdl 				}
    836  1.1  fvdl 
    837  1.1  fvdl 				/*
    838  1.1  fvdl 				 * Now at the first mbuf to sync; nail
    839  1.1  fvdl 				 * each one until we have exhausted the
    840  1.1  fvdl 				 * length.
    841  1.1  fvdl 				 */
    842  1.1  fvdl 				minlen = len < m->m_len - moff ?
    843  1.1  fvdl 				    len : m->m_len - moff;
    844  1.1  fvdl 
    845  1.1  fvdl 				memcpy(mtod(m, caddr_t) + moff,
    846  1.1  fvdl 				    (char *)cookie->id_bouncebuf + offset,
    847  1.1  fvdl 				    minlen);
    848  1.1  fvdl 
    849  1.1  fvdl 				moff = 0;
    850  1.1  fvdl 				len -= minlen;
    851  1.1  fvdl 				offset += minlen;
    852  1.1  fvdl 			}
    853  1.1  fvdl 		}
    854  1.1  fvdl 
    855  1.1  fvdl 		/*
    856  1.1  fvdl 		 * Nothing to do for post-write.
    857  1.1  fvdl 		 */
    858  1.1  fvdl 		break;
    859  1.1  fvdl 	    }
    860  1.1  fvdl 
    861  1.1  fvdl 	case ID_BUFTYPE_UIO:
    862  1.1  fvdl 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_UIO");
    863  1.1  fvdl 		break;
    864  1.1  fvdl 
    865  1.1  fvdl 	case ID_BUFTYPE_RAW:
    866  1.1  fvdl 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_RAW");
    867  1.1  fvdl 		break;
    868  1.1  fvdl 
    869  1.1  fvdl 	case ID_BUFTYPE_INVALID:
    870  1.1  fvdl 		panic("_isa_bus_dmamap_sync: ID_BUFTYPE_INVALID");
    871  1.1  fvdl 		break;
    872  1.1  fvdl 
    873  1.1  fvdl 	default:
    874  1.1  fvdl 		printf("unknown buffer type %d\n", cookie->id_buftype);
    875  1.1  fvdl 		panic("_isa_bus_dmamap_sync");
    876  1.1  fvdl 	}
    877  1.1  fvdl }
    878  1.1  fvdl 
    879  1.1  fvdl /*
    880  1.1  fvdl  * Allocate memory safe for ISA DMA.
    881  1.1  fvdl  */
    882  1.1  fvdl int
    883  1.1  fvdl _isa_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    884  1.1  fvdl 	bus_dma_tag_t t;
    885  1.1  fvdl 	bus_size_t size, alignment, boundary;
    886  1.1  fvdl 	bus_dma_segment_t *segs;
    887  1.1  fvdl 	int nsegs;
    888  1.1  fvdl 	int *rsegs;
    889  1.1  fvdl 	int flags;
    890  1.1  fvdl {
    891  1.1  fvdl 	paddr_t high;
    892  1.1  fvdl 
    893  1.1  fvdl 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
    894  1.1  fvdl 		high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
    895  1.1  fvdl 	else
    896  1.1  fvdl 		high = trunc_page(avail_end);
    897  1.1  fvdl 
    898  1.1  fvdl 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    899  1.1  fvdl 	    segs, nsegs, rsegs, flags, 0, high));
    900  1.1  fvdl }
    901  1.1  fvdl 
    902  1.1  fvdl /**********************************************************************
    903  1.1  fvdl  * ISA DMA utility functions
    904  1.1  fvdl  **********************************************************************/
    905  1.1  fvdl 
    906  1.1  fvdl int
    907  1.1  fvdl _isa_dma_alloc_bouncebuf(t, map, size, flags)
    908  1.1  fvdl 	bus_dma_tag_t t;
    909  1.1  fvdl 	bus_dmamap_t map;
    910  1.1  fvdl 	bus_size_t size;
    911  1.1  fvdl 	int flags;
    912  1.1  fvdl {
    913  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    914  1.1  fvdl 	int error = 0;
    915  1.1  fvdl 
    916  1.1  fvdl 	cookie->id_bouncebuflen = round_page(size);
    917  1.1  fvdl 	error = _isa_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
    918  1.1  fvdl 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
    919  1.1  fvdl 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
    920  1.1  fvdl 	if (error)
    921  1.1  fvdl 		goto out;
    922  1.1  fvdl 	error = _bus_dmamem_map(t, cookie->id_bouncesegs,
    923  1.1  fvdl 	    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
    924  1.1  fvdl 	    (caddr_t *)&cookie->id_bouncebuf, flags);
    925  1.1  fvdl 
    926  1.1  fvdl  out:
    927  1.1  fvdl 	if (error) {
    928  1.1  fvdl 		_bus_dmamem_free(t, cookie->id_bouncesegs,
    929  1.1  fvdl 		    cookie->id_nbouncesegs);
    930  1.1  fvdl 		cookie->id_bouncebuflen = 0;
    931  1.1  fvdl 		cookie->id_nbouncesegs = 0;
    932  1.1  fvdl 	} else {
    933  1.1  fvdl 		cookie->id_flags |= ID_HAS_BOUNCE;
    934  1.1  fvdl 		STAT_INCR(isa_dma_stats_nbouncebufs);
    935  1.1  fvdl 	}
    936  1.1  fvdl 
    937  1.1  fvdl 	return (error);
    938  1.1  fvdl }
    939  1.1  fvdl 
    940  1.1  fvdl void
    941  1.1  fvdl _isa_dma_free_bouncebuf(t, map)
    942  1.1  fvdl 	bus_dma_tag_t t;
    943  1.1  fvdl 	bus_dmamap_t map;
    944  1.1  fvdl {
    945  1.1  fvdl 	struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
    946  1.1  fvdl 
    947  1.1  fvdl 	STAT_DECR(isa_dma_stats_nbouncebufs);
    948  1.1  fvdl 
    949  1.1  fvdl 	_bus_dmamem_unmap(t, cookie->id_bouncebuf,
    950  1.1  fvdl 	    cookie->id_bouncebuflen);
    951  1.1  fvdl 	_bus_dmamem_free(t, cookie->id_bouncesegs,
    952  1.1  fvdl 	    cookie->id_nbouncesegs);
    953  1.1  fvdl 	cookie->id_bouncebuflen = 0;
    954  1.1  fvdl 	cookie->id_nbouncesegs = 0;
    955  1.1  fvdl 	cookie->id_flags &= ~ID_HAS_BOUNCE;
    956  1.1  fvdl }
    957