Home | History | Annotate | Line # | Download | only in isa
isadma_bounce.c revision 1.2.4.2
      1  1.2.4.2  minoura /* $NetBSD: isadma_bounce.c,v 1.2.4.2 2000/06/22 16:59:16 minoura Exp $ */
      2  1.2.4.2  minoura /* NetBSD: isadma_bounce.c,v 1.2 2000/06/01 05:49:36 thorpej Exp  */
      3  1.2.4.2  minoura 
      4  1.2.4.2  minoura /*-
      5  1.2.4.2  minoura  * Copyright (c) 1996, 1997, 1998, 2000 The NetBSD Foundation, Inc.
      6  1.2.4.2  minoura  * All rights reserved.
      7  1.2.4.2  minoura  *
      8  1.2.4.2  minoura  * This code is derived from software contributed to The NetBSD Foundation
      9  1.2.4.2  minoura  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  1.2.4.2  minoura  * NASA Ames Research Center.
     11  1.2.4.2  minoura  *
     12  1.2.4.2  minoura  * Redistribution and use in source and binary forms, with or without
     13  1.2.4.2  minoura  * modification, are permitted provided that the following conditions
     14  1.2.4.2  minoura  * are met:
     15  1.2.4.2  minoura  * 1. Redistributions of source code must retain the above copyright
     16  1.2.4.2  minoura  *    notice, this list of conditions and the following disclaimer.
     17  1.2.4.2  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.2.4.2  minoura  *    notice, this list of conditions and the following disclaimer in the
     19  1.2.4.2  minoura  *    documentation and/or other materials provided with the distribution.
     20  1.2.4.2  minoura  * 3. All advertising materials mentioning features or use of this software
     21  1.2.4.2  minoura  *    must display the following acknowledgement:
     22  1.2.4.2  minoura  *	This product includes software developed by the NetBSD
     23  1.2.4.2  minoura  *	Foundation, Inc. and its contributors.
     24  1.2.4.2  minoura  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  1.2.4.2  minoura  *    contributors may be used to endorse or promote products derived
     26  1.2.4.2  minoura  *    from this software without specific prior written permission.
     27  1.2.4.2  minoura  *
     28  1.2.4.2  minoura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  1.2.4.2  minoura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  1.2.4.2  minoura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  1.2.4.2  minoura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  1.2.4.2  minoura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  1.2.4.2  minoura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  1.2.4.2  minoura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  1.2.4.2  minoura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  1.2.4.2  minoura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  1.2.4.2  minoura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  1.2.4.2  minoura  * POSSIBILITY OF SUCH DAMAGE.
     39  1.2.4.2  minoura  */
     40  1.2.4.2  minoura 
     41  1.2.4.2  minoura #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     42  1.2.4.2  minoura 
     43  1.2.4.2  minoura __KERNEL_RCSID(0, "$NetBSD: isadma_bounce.c,v 1.2.4.2 2000/06/22 16:59:16 minoura Exp $");
     44  1.2.4.2  minoura 
     45  1.2.4.2  minoura #include <sys/param.h>
     46  1.2.4.2  minoura #include <sys/systm.h>
     47  1.2.4.2  minoura #include <sys/syslog.h>
     48  1.2.4.2  minoura #include <sys/device.h>
     49  1.2.4.2  minoura #include <sys/malloc.h>
     50  1.2.4.2  minoura #include <sys/proc.h>
     51  1.2.4.2  minoura #include <sys/mbuf.h>
     52  1.2.4.2  minoura 
     53  1.2.4.2  minoura #define _ARC_BUS_DMA_PRIVATE
     54  1.2.4.2  minoura #include <machine/bus.h>
     55  1.2.4.2  minoura 
     56  1.2.4.2  minoura #include <dev/isa/isareg.h>
     57  1.2.4.2  minoura #include <dev/isa/isavar.h>
     58  1.2.4.2  minoura 
     59  1.2.4.2  minoura #include <vm/vm.h>
     60  1.2.4.2  minoura 
     61  1.2.4.2  minoura extern paddr_t avail_end;	/* from pmap.c */
     62  1.2.4.2  minoura 
     63  1.2.4.2  minoura /*
     64  1.2.4.2  minoura  * ISA can only DMA to 0-16M.
     65  1.2.4.2  minoura  */
     66  1.2.4.2  minoura #define	ISA_DMA_BOUNCE_THRESHOLD	(16 * 1024 * 1024)
     67  1.2.4.2  minoura 
     68  1.2.4.2  minoura /*
     69  1.2.4.2  minoura  * Cookie used by bouncing ISA DMA.  A pointer to one of these is stashed
     70  1.2.4.2  minoura  * in the DMA map.
     71  1.2.4.2  minoura  */
     72  1.2.4.2  minoura struct isadma_bounce_cookie {
     73  1.2.4.2  minoura 	int	id_flags;		/* flags; see below */
     74  1.2.4.2  minoura 
     75  1.2.4.2  minoura 	/*
     76  1.2.4.2  minoura 	 * Information about the original buffer used during
     77  1.2.4.2  minoura 	 * DMA map syncs.  Note that origbuflen is only used
     78  1.2.4.2  minoura 	 * for ID_BUFTYPE_LINEAR.
     79  1.2.4.2  minoura 	 */
     80  1.2.4.2  minoura 	void	*id_origbuf;		/* pointer to orig buffer if
     81  1.2.4.2  minoura 					   bouncing */
     82  1.2.4.2  minoura 	bus_size_t id_origbuflen;	/* ...and size */
     83  1.2.4.2  minoura 	int	id_buftype;		/* type of buffer */
     84  1.2.4.2  minoura 
     85  1.2.4.2  minoura 	void	*id_bouncebuf;		/* pointer to the bounce buffer */
     86  1.2.4.2  minoura 	bus_size_t id_bouncebuflen;	/* ...and size */
     87  1.2.4.2  minoura 	int	id_nbouncesegs;		/* number of valid bounce segs */
     88  1.2.4.2  minoura 	bus_dma_segment_t id_bouncesegs[1]; /* array of bounce buffer
     89  1.2.4.2  minoura 					       physical memory segments */
     90  1.2.4.2  minoura };
     91  1.2.4.2  minoura 
     92  1.2.4.2  minoura /* id_flags */
     93  1.2.4.2  minoura #define	ID_MIGHT_NEED_BOUNCE	0x01	/* map could need bounce buffers */
     94  1.2.4.2  minoura #define	ID_HAS_BOUNCE		0x02	/* map currently has bounce buffers */
     95  1.2.4.2  minoura #define	ID_IS_BOUNCING		0x04	/* map is bouncing current xfer */
     96  1.2.4.2  minoura 
     97  1.2.4.2  minoura /* id_buftype */
     98  1.2.4.2  minoura #define	ID_BUFTYPE_INVALID	0
     99  1.2.4.2  minoura #define	ID_BUFTYPE_LINEAR	1
    100  1.2.4.2  minoura #define	ID_BUFTYPE_MBUF		2
    101  1.2.4.2  minoura #define	ID_BUFTYPE_UIO		3
    102  1.2.4.2  minoura #define	ID_BUFTYPE_RAW		4
    103  1.2.4.2  minoura 
    104  1.2.4.2  minoura int	isadma_bounce_dmamap_create(bus_dma_tag_t, bus_size_t, int,
    105  1.2.4.2  minoura 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
    106  1.2.4.2  minoura void	isadma_bounce_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
    107  1.2.4.2  minoura int	isadma_bounce_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
    108  1.2.4.2  minoura 	    bus_size_t, struct proc *, int);
    109  1.2.4.2  minoura int	isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
    110  1.2.4.2  minoura 	    struct mbuf *, int);
    111  1.2.4.2  minoura int	isadma_bounce_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
    112  1.2.4.2  minoura 	    struct uio *, int);
    113  1.2.4.2  minoura int	isadma_bounce_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
    114  1.2.4.2  minoura 	    bus_dma_segment_t *, int, bus_size_t, int);
    115  1.2.4.2  minoura void	isadma_bounce_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
    116  1.2.4.2  minoura void	isadma_bounce_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
    117  1.2.4.2  minoura 	    bus_addr_t, bus_size_t, int);
    118  1.2.4.2  minoura int	isadma_bounce_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
    119  1.2.4.2  minoura 	    bus_size_t, bus_dma_segment_t *, int, int *, int);
    120  1.2.4.2  minoura 
    121  1.2.4.2  minoura static int isadma_bounce_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
    122  1.2.4.2  minoura 	    bus_size_t, int));
    123  1.2.4.2  minoura static void isadma_bounce_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
    124  1.2.4.2  minoura 
    125  1.2.4.2  minoura void
    126  1.2.4.2  minoura isadma_bounce_tag_init(t)
    127  1.2.4.2  minoura 	bus_dma_tag_t t;
    128  1.2.4.2  minoura {
    129  1.2.4.2  minoura 	/*
    130  1.2.4.2  minoura 	 * Initialize the DMA tag used for ISA DMA.
    131  1.2.4.2  minoura 	 */
    132  1.2.4.2  minoura 
    133  1.2.4.2  minoura 	_bus_dma_tag_init(t);
    134  1.2.4.2  minoura 
    135  1.2.4.2  minoura 	t->_dmamap_create = isadma_bounce_dmamap_create;
    136  1.2.4.2  minoura 	t->_dmamap_destroy = isadma_bounce_dmamap_destroy;
    137  1.2.4.2  minoura 	t->_dmamap_load = isadma_bounce_dmamap_load;
    138  1.2.4.2  minoura 	t->_dmamap_load_mbuf = isadma_bounce_dmamap_load_mbuf;
    139  1.2.4.2  minoura 	t->_dmamap_load_uio = isadma_bounce_dmamap_load_uio;
    140  1.2.4.2  minoura 	t->_dmamap_load_raw = isadma_bounce_dmamap_load_raw;
    141  1.2.4.2  minoura 	t->_dmamap_unload = isadma_bounce_dmamap_unload;
    142  1.2.4.2  minoura 	t->_dmamap_sync = isadma_bounce_dmamap_sync;
    143  1.2.4.2  minoura 	t->_dmamem_alloc = isadma_bounce_dmamem_alloc;
    144  1.2.4.2  minoura }
    145  1.2.4.2  minoura 
    146  1.2.4.2  minoura /*
    147  1.2.4.2  minoura  * Create an ISA DMA map.
    148  1.2.4.2  minoura  */
    149  1.2.4.2  minoura int
    150  1.2.4.2  minoura isadma_bounce_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    151  1.2.4.2  minoura     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    152  1.2.4.2  minoura {
    153  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie;
    154  1.2.4.2  minoura 	bus_dmamap_t map;
    155  1.2.4.2  minoura 	int error, cookieflags;
    156  1.2.4.2  minoura 	void *cookiestore;
    157  1.2.4.2  minoura 	size_t cookiesize;
    158  1.2.4.2  minoura 
    159  1.2.4.2  minoura 	/* Call common function to create the basic map. */
    160  1.2.4.2  minoura 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    161  1.2.4.2  minoura 	    flags, dmamp);
    162  1.2.4.2  minoura 	if (error)
    163  1.2.4.2  minoura 		return (error);
    164  1.2.4.2  minoura 
    165  1.2.4.2  minoura 	map = *dmamp;
    166  1.2.4.2  minoura 	map->_dm_cookie = NULL;
    167  1.2.4.2  minoura 
    168  1.2.4.2  minoura 	cookiesize = sizeof(*cookie);
    169  1.2.4.2  minoura 
    170  1.2.4.2  minoura 	/*
    171  1.2.4.2  minoura 	 * ISA only has 24-bits of address space.  This means
    172  1.2.4.2  minoura 	 * we can't DMA to pages over 16M.  In order to DMA to
    173  1.2.4.2  minoura 	 * arbitrary buffers, we use "bounce buffers" - pages
    174  1.2.4.2  minoura 	 * in memory below the 16M boundary.  On DMA reads,
    175  1.2.4.2  minoura 	 * DMA happens to the bounce buffers, and is copied into
    176  1.2.4.2  minoura 	 * the caller's buffer.  On writes, data is copied into
    177  1.2.4.2  minoura 	 * but bounce buffer, and the DMA happens from those
    178  1.2.4.2  minoura 	 * pages.  To software using the DMA mapping interface,
    179  1.2.4.2  minoura 	 * this looks simply like a data cache.
    180  1.2.4.2  minoura 	 *
    181  1.2.4.2  minoura 	 * If we have more than 16M of RAM in the system, we may
    182  1.2.4.2  minoura 	 * need bounce buffers.  We check and remember that here.
    183  1.2.4.2  minoura 	 *
    184  1.2.4.2  minoura 	 * ...or, there is an opposite case.  The most segments
    185  1.2.4.2  minoura 	 * a transfer will require is (maxxfer / PAGE_SIZE) + 1.  If
    186  1.2.4.2  minoura 	 * the caller can't handle that many segments (e.g. the
    187  1.2.4.2  minoura 	 * ISA DMA controller), we may have to bounce it as well.
    188  1.2.4.2  minoura 	 */
    189  1.2.4.2  minoura 	cookieflags = 0;
    190  1.2.4.2  minoura 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD ||
    191  1.2.4.2  minoura 	    ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
    192  1.2.4.2  minoura 		cookieflags |= ID_MIGHT_NEED_BOUNCE;
    193  1.2.4.2  minoura 		cookiesize += (sizeof(bus_dma_segment_t) *
    194  1.2.4.2  minoura 		    (map->_dm_segcnt - 1));
    195  1.2.4.2  minoura 	}
    196  1.2.4.2  minoura 
    197  1.2.4.2  minoura 	/*
    198  1.2.4.2  minoura 	 * Allocate our cookie.
    199  1.2.4.2  minoura 	 */
    200  1.2.4.2  minoura 	if ((cookiestore = malloc(cookiesize, M_DMAMAP,
    201  1.2.4.2  minoura 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
    202  1.2.4.2  minoura 		error = ENOMEM;
    203  1.2.4.2  minoura 		goto out;
    204  1.2.4.2  minoura 	}
    205  1.2.4.2  minoura 	memset(cookiestore, 0, cookiesize);
    206  1.2.4.2  minoura 	cookie = (struct isadma_bounce_cookie *)cookiestore;
    207  1.2.4.2  minoura 	cookie->id_flags = cookieflags;
    208  1.2.4.2  minoura 	map->_dm_cookie = cookie;
    209  1.2.4.2  minoura 
    210  1.2.4.2  minoura 	if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
    211  1.2.4.2  minoura 		/*
    212  1.2.4.2  minoura 		 * Allocate the bounce pages now if the caller
    213  1.2.4.2  minoura 		 * wishes us to do so.
    214  1.2.4.2  minoura 		 */
    215  1.2.4.2  minoura 		if ((flags & BUS_DMA_ALLOCNOW) == 0)
    216  1.2.4.2  minoura 			goto out;
    217  1.2.4.2  minoura 
    218  1.2.4.2  minoura 		error = isadma_bounce_alloc_bouncebuf(t, map, size, flags);
    219  1.2.4.2  minoura 	}
    220  1.2.4.2  minoura 
    221  1.2.4.2  minoura  out:
    222  1.2.4.2  minoura 	if (error) {
    223  1.2.4.2  minoura 		if (map->_dm_cookie != NULL)
    224  1.2.4.2  minoura 			free(map->_dm_cookie, M_DMAMAP);
    225  1.2.4.2  minoura 		_bus_dmamap_destroy(t, map);
    226  1.2.4.2  minoura 	}
    227  1.2.4.2  minoura 	return (error);
    228  1.2.4.2  minoura }
    229  1.2.4.2  minoura 
    230  1.2.4.2  minoura /*
    231  1.2.4.2  minoura  * Destroy an ISA DMA map.
    232  1.2.4.2  minoura  */
    233  1.2.4.2  minoura void
    234  1.2.4.2  minoura isadma_bounce_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    235  1.2.4.2  minoura {
    236  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    237  1.2.4.2  minoura 
    238  1.2.4.2  minoura 	/*
    239  1.2.4.2  minoura 	 * Free any bounce pages this map might hold.
    240  1.2.4.2  minoura 	 */
    241  1.2.4.2  minoura 	if (cookie->id_flags & ID_HAS_BOUNCE)
    242  1.2.4.2  minoura 		isadma_bounce_free_bouncebuf(t, map);
    243  1.2.4.2  minoura 
    244  1.2.4.2  minoura 	free(cookie, M_DMAMAP);
    245  1.2.4.2  minoura 	_bus_dmamap_destroy(t, map);
    246  1.2.4.2  minoura }
    247  1.2.4.2  minoura 
    248  1.2.4.2  minoura /*
    249  1.2.4.2  minoura  * Load an ISA DMA map with a linear buffer.
    250  1.2.4.2  minoura  */
    251  1.2.4.2  minoura int
    252  1.2.4.2  minoura isadma_bounce_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    253  1.2.4.2  minoura     bus_size_t buflen, struct proc *p, int flags)
    254  1.2.4.2  minoura {
    255  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    256  1.2.4.2  minoura 	int error;
    257  1.2.4.2  minoura 
    258  1.2.4.2  minoura 	/*
    259  1.2.4.2  minoura 	 * Make sure that on error condition we return "no valid mappings."
    260  1.2.4.2  minoura 	 */
    261  1.2.4.2  minoura 	map->dm_mapsize = 0;
    262  1.2.4.2  minoura 	map->dm_nsegs = 0;
    263  1.2.4.2  minoura 
    264  1.2.4.2  minoura 	/*
    265  1.2.4.2  minoura 	 * Try to load the map the normal way.  If this errors out,
    266  1.2.4.2  minoura 	 * and we can bounce, we will.
    267  1.2.4.2  minoura 	 */
    268  1.2.4.2  minoura 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    269  1.2.4.2  minoura 	if (error == 0 ||
    270  1.2.4.2  minoura 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    271  1.2.4.2  minoura 		return (error);
    272  1.2.4.2  minoura 
    273  1.2.4.2  minoura 	/*
    274  1.2.4.2  minoura 	 * First attempt failed; bounce it.
    275  1.2.4.2  minoura 	 */
    276  1.2.4.2  minoura 
    277  1.2.4.2  minoura 	/*
    278  1.2.4.2  minoura 	 * Allocate bounce pages, if necessary.
    279  1.2.4.2  minoura 	 */
    280  1.2.4.2  minoura 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    281  1.2.4.2  minoura 		error = isadma_bounce_alloc_bouncebuf(t, map, buflen, flags);
    282  1.2.4.2  minoura 		if (error)
    283  1.2.4.2  minoura 			return (error);
    284  1.2.4.2  minoura 	}
    285  1.2.4.2  minoura 
    286  1.2.4.2  minoura 	/*
    287  1.2.4.2  minoura 	 * Cache a pointer to the caller's buffer and load the DMA map
    288  1.2.4.2  minoura 	 * with the bounce buffer.
    289  1.2.4.2  minoura 	 */
    290  1.2.4.2  minoura 	cookie->id_origbuf = buf;
    291  1.2.4.2  minoura 	cookie->id_origbuflen = buflen;
    292  1.2.4.2  minoura 	cookie->id_buftype = ID_BUFTYPE_LINEAR;
    293  1.2.4.2  minoura 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
    294  1.2.4.2  minoura 	    p, flags);
    295  1.2.4.2  minoura 	if (error) {
    296  1.2.4.2  minoura 		/*
    297  1.2.4.2  minoura 		 * Free the bounce pages, unless our resources
    298  1.2.4.2  minoura 		 * are reserved for our exclusive use.
    299  1.2.4.2  minoura 		 */
    300  1.2.4.2  minoura 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    301  1.2.4.2  minoura 			isadma_bounce_free_bouncebuf(t, map);
    302  1.2.4.2  minoura 		return (error);
    303  1.2.4.2  minoura 	}
    304  1.2.4.2  minoura 
    305  1.2.4.2  minoura 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    306  1.2.4.2  minoura 	cookie->id_flags |= ID_IS_BOUNCING;
    307  1.2.4.2  minoura 	return (0);
    308  1.2.4.2  minoura }
    309  1.2.4.2  minoura 
    310  1.2.4.2  minoura /*
    311  1.2.4.2  minoura  * Like isadma_bounce_dmamap_load(), but for mbufs.
    312  1.2.4.2  minoura  */
    313  1.2.4.2  minoura int
    314  1.2.4.2  minoura isadma_bounce_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map,
    315  1.2.4.2  minoura     struct mbuf *m0, int flags)
    316  1.2.4.2  minoura {
    317  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    318  1.2.4.2  minoura 	int error;
    319  1.2.4.2  minoura 
    320  1.2.4.2  minoura 	/*
    321  1.2.4.2  minoura 	 * Make sure on error condition we return "no valid mappings."
    322  1.2.4.2  minoura 	 */
    323  1.2.4.2  minoura 	map->dm_mapsize = 0;
    324  1.2.4.2  minoura 	map->dm_nsegs = 0;
    325  1.2.4.2  minoura 
    326  1.2.4.2  minoura #ifdef DIAGNOSTIC
    327  1.2.4.2  minoura 	if ((m0->m_flags & M_PKTHDR) == 0)
    328  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_load_mbuf: no packet header");
    329  1.2.4.2  minoura #endif
    330  1.2.4.2  minoura 
    331  1.2.4.2  minoura 	if (m0->m_pkthdr.len > map->_dm_size)
    332  1.2.4.2  minoura 		return (EINVAL);
    333  1.2.4.2  minoura 
    334  1.2.4.2  minoura 	/*
    335  1.2.4.2  minoura 	 * Try to load the map the normal way.  If this errors out,
    336  1.2.4.2  minoura 	 * and we can bounce, we will.
    337  1.2.4.2  minoura 	 */
    338  1.2.4.2  minoura 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
    339  1.2.4.2  minoura 	if (error == 0 ||
    340  1.2.4.2  minoura 	    (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
    341  1.2.4.2  minoura 		return (error);
    342  1.2.4.2  minoura 
    343  1.2.4.2  minoura 	/*
    344  1.2.4.2  minoura 	 * First attempt failed; bounce it.
    345  1.2.4.2  minoura 	 */
    346  1.2.4.2  minoura 
    347  1.2.4.2  minoura 	/*
    348  1.2.4.2  minoura 	 * Allocate bounce pages, if necessary.
    349  1.2.4.2  minoura 	 */
    350  1.2.4.2  minoura 	if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
    351  1.2.4.2  minoura 		error = isadma_bounce_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
    352  1.2.4.2  minoura 		    flags);
    353  1.2.4.2  minoura 		if (error)
    354  1.2.4.2  minoura 			return (error);
    355  1.2.4.2  minoura 	}
    356  1.2.4.2  minoura 
    357  1.2.4.2  minoura 	/*
    358  1.2.4.2  minoura 	 * Cache a pointer to the caller's buffer and load the DMA map
    359  1.2.4.2  minoura 	 * with the bounce buffer.
    360  1.2.4.2  minoura 	 */
    361  1.2.4.2  minoura 	cookie->id_origbuf = m0;
    362  1.2.4.2  minoura 	cookie->id_origbuflen = m0->m_pkthdr.len;	/* not really used */
    363  1.2.4.2  minoura 	cookie->id_buftype = ID_BUFTYPE_MBUF;
    364  1.2.4.2  minoura 	error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
    365  1.2.4.2  minoura 	    m0->m_pkthdr.len, NULL, flags);
    366  1.2.4.2  minoura 	if (error) {
    367  1.2.4.2  minoura 		/*
    368  1.2.4.2  minoura 		 * Free the bounce pages, unless our resources
    369  1.2.4.2  minoura 		 * are reserved for our exclusive use.
    370  1.2.4.2  minoura 		 */
    371  1.2.4.2  minoura 		if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    372  1.2.4.2  minoura 			isadma_bounce_free_bouncebuf(t, map);
    373  1.2.4.2  minoura 		return (error);
    374  1.2.4.2  minoura 	}
    375  1.2.4.2  minoura 
    376  1.2.4.2  minoura 	/* ...so isadma_bounce_dmamap_sync() knows we're bouncing */
    377  1.2.4.2  minoura 	cookie->id_flags |= ID_IS_BOUNCING;
    378  1.2.4.2  minoura 	return (0);
    379  1.2.4.2  minoura }
    380  1.2.4.2  minoura 
    381  1.2.4.2  minoura /*
    382  1.2.4.2  minoura  * Like isadma_bounce_dmamap_load(), but for uios.
    383  1.2.4.2  minoura  */
    384  1.2.4.2  minoura int
    385  1.2.4.2  minoura isadma_bounce_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map,
    386  1.2.4.2  minoura     struct uio *uio, int flags)
    387  1.2.4.2  minoura {
    388  1.2.4.2  minoura 
    389  1.2.4.2  minoura 	panic("isadma_bounce_dmamap_load_uio: not implemented");
    390  1.2.4.2  minoura }
    391  1.2.4.2  minoura 
    392  1.2.4.2  minoura /*
    393  1.2.4.2  minoura  * Like isadma_bounce_dmamap_load(), but for raw memory allocated with
    394  1.2.4.2  minoura  * bus_dmamem_alloc().
    395  1.2.4.2  minoura  */
    396  1.2.4.2  minoura int
    397  1.2.4.2  minoura isadma_bounce_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    398  1.2.4.2  minoura     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    399  1.2.4.2  minoura {
    400  1.2.4.2  minoura 
    401  1.2.4.2  minoura 	panic("isadma_bounce_dmamap_load_raw: not implemented");
    402  1.2.4.2  minoura }
    403  1.2.4.2  minoura 
    404  1.2.4.2  minoura /*
    405  1.2.4.2  minoura  * Unload an ISA DMA map.
    406  1.2.4.2  minoura  */
    407  1.2.4.2  minoura void
    408  1.2.4.2  minoura isadma_bounce_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    409  1.2.4.2  minoura {
    410  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    411  1.2.4.2  minoura 
    412  1.2.4.2  minoura 	/*
    413  1.2.4.2  minoura 	 * If we have bounce pages, free them, unless they're
    414  1.2.4.2  minoura 	 * reserved for our exclusive use.
    415  1.2.4.2  minoura 	 */
    416  1.2.4.2  minoura 	if ((cookie->id_flags & ID_HAS_BOUNCE) &&
    417  1.2.4.2  minoura 	    (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
    418  1.2.4.2  minoura 		isadma_bounce_free_bouncebuf(t, map);
    419  1.2.4.2  minoura 
    420  1.2.4.2  minoura 	cookie->id_flags &= ~ID_IS_BOUNCING;
    421  1.2.4.2  minoura 	cookie->id_buftype = ID_BUFTYPE_INVALID;
    422  1.2.4.2  minoura 
    423  1.2.4.2  minoura 	/*
    424  1.2.4.2  minoura 	 * Do the generic bits of the unload.
    425  1.2.4.2  minoura 	 */
    426  1.2.4.2  minoura 	_bus_dmamap_unload(t, map);
    427  1.2.4.2  minoura }
    428  1.2.4.2  minoura 
    429  1.2.4.2  minoura /*
    430  1.2.4.2  minoura  * Synchronize an ISA DMA map.
    431  1.2.4.2  minoura  */
    432  1.2.4.2  minoura void
    433  1.2.4.2  minoura isadma_bounce_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    434  1.2.4.2  minoura     bus_size_t len, int ops)
    435  1.2.4.2  minoura {
    436  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    437  1.2.4.2  minoura 	void (*sync)(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, bus_size_t, int);
    438  1.2.4.2  minoura 
    439  1.2.4.2  minoura #if defined(MIPS1) && defined(MIPS3)
    440  1.2.4.2  minoura 	sync = (CPUISMIPS3) ? _mips3_bus_dmamap_sync : _mips1_bus_dmamap_sync;
    441  1.2.4.2  minoura #elif defined(MIPS1)
    442  1.2.4.2  minoura 	sync = _mips1_bus_dmamap_sync;
    443  1.2.4.2  minoura #elif defined(MIPS3)
    444  1.2.4.2  minoura 	sync = _mips3_bus_dmamap_sync;
    445  1.2.4.2  minoura #else
    446  1.2.4.2  minoura #error neither MIPS1 nor MIPS3 is defined
    447  1.2.4.2  minoura #endif
    448  1.2.4.2  minoura 	/*
    449  1.2.4.2  minoura 	 * Mixing PRE and POST operations is not allowed.
    450  1.2.4.2  minoura 	 */
    451  1.2.4.2  minoura 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    452  1.2.4.2  minoura 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    453  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_sync: mix PRE and POST");
    454  1.2.4.2  minoura 
    455  1.2.4.2  minoura #ifdef DIAGNOSTIC
    456  1.2.4.2  minoura 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    457  1.2.4.2  minoura 		if (offset >= map->dm_mapsize)
    458  1.2.4.2  minoura 			panic("isadma_bounce_dmamap_sync: bad offset");
    459  1.2.4.2  minoura 		if (len == 0 || (offset + len) > map->dm_mapsize)
    460  1.2.4.2  minoura 			panic("isadma_bounce_dmamap_sync: bad length");
    461  1.2.4.2  minoura 	}
    462  1.2.4.2  minoura #endif
    463  1.2.4.2  minoura 
    464  1.2.4.2  minoura 	/*
    465  1.2.4.2  minoura 	 * If we're not bouncing, just drain the write buffer
    466  1.2.4.2  minoura 	 * and flush cache.
    467  1.2.4.2  minoura 	 */
    468  1.2.4.2  minoura 	if ((cookie->id_flags & ID_IS_BOUNCING) == 0) {
    469  1.2.4.2  minoura 		((*sync)(t, map, offset, len, ops));
    470  1.2.4.2  minoura 		return;
    471  1.2.4.2  minoura 	}
    472  1.2.4.2  minoura 
    473  1.2.4.2  minoura 	/*
    474  1.2.4.2  minoura 	 * XXX
    475  1.2.4.2  minoura 	 * This should be needed in BUS_DMASYNC_POSTREAD case only,
    476  1.2.4.2  minoura 	 * if _mips3_bus_dmamap_sync() used "Hit_Invalidate on POSTREAD",
    477  1.2.4.2  minoura 	 * rather than "Hit_Write_Back_Invalidate on PREREAD".
    478  1.2.4.2  minoura 	 */
    479  1.2.4.2  minoura 	if (ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_POSTREAD))
    480  1.2.4.2  minoura 		((*sync)(t, map, offset, len, ops));
    481  1.2.4.2  minoura 
    482  1.2.4.2  minoura 	switch (cookie->id_buftype) {
    483  1.2.4.2  minoura 	case ID_BUFTYPE_LINEAR:
    484  1.2.4.2  minoura 		/*
    485  1.2.4.2  minoura 		 * Nothing to do for pre-read.
    486  1.2.4.2  minoura 		 */
    487  1.2.4.2  minoura 
    488  1.2.4.2  minoura 		if (ops & BUS_DMASYNC_PREWRITE) {
    489  1.2.4.2  minoura 			/*
    490  1.2.4.2  minoura 			 * Copy the caller's buffer to the bounce buffer.
    491  1.2.4.2  minoura 			 */
    492  1.2.4.2  minoura 			memcpy((char *)cookie->id_bouncebuf + offset,
    493  1.2.4.2  minoura 			    (char *)cookie->id_origbuf + offset, len);
    494  1.2.4.2  minoura 		}
    495  1.2.4.2  minoura 
    496  1.2.4.2  minoura 		if (ops & BUS_DMASYNC_POSTREAD) {
    497  1.2.4.2  minoura 			/*
    498  1.2.4.2  minoura 			 * Copy the bounce buffer to the caller's buffer.
    499  1.2.4.2  minoura 			 */
    500  1.2.4.2  minoura 			memcpy((char *)cookie->id_origbuf + offset,
    501  1.2.4.2  minoura 			    (char *)cookie->id_bouncebuf + offset, len);
    502  1.2.4.2  minoura 		}
    503  1.2.4.2  minoura 
    504  1.2.4.2  minoura 		/*
    505  1.2.4.2  minoura 		 * Nothing to do for post-write.
    506  1.2.4.2  minoura 		 */
    507  1.2.4.2  minoura 		break;
    508  1.2.4.2  minoura 
    509  1.2.4.2  minoura 	case ID_BUFTYPE_MBUF:
    510  1.2.4.2  minoura 	    {
    511  1.2.4.2  minoura 		struct mbuf *m, *m0 = cookie->id_origbuf;
    512  1.2.4.2  minoura 		bus_size_t minlen, moff;
    513  1.2.4.2  minoura 
    514  1.2.4.2  minoura 		/*
    515  1.2.4.2  minoura 		 * Nothing to do for pre-read.
    516  1.2.4.2  minoura 		 */
    517  1.2.4.2  minoura 
    518  1.2.4.2  minoura 		if (ops & BUS_DMASYNC_PREWRITE) {
    519  1.2.4.2  minoura 			/*
    520  1.2.4.2  minoura 			 * Copy the caller's buffer to the bounce buffer.
    521  1.2.4.2  minoura 			 */
    522  1.2.4.2  minoura 			m_copydata(m0, offset, len,
    523  1.2.4.2  minoura 			    (char *)cookie->id_bouncebuf + offset);
    524  1.2.4.2  minoura 		}
    525  1.2.4.2  minoura 
    526  1.2.4.2  minoura 		if (ops & BUS_DMASYNC_POSTREAD) {
    527  1.2.4.2  minoura 			/*
    528  1.2.4.2  minoura 			 * Copy the bounce buffer to the caller's buffer.
    529  1.2.4.2  minoura 			 */
    530  1.2.4.2  minoura 			for (moff = offset, m = m0; m != NULL && len != 0;
    531  1.2.4.2  minoura 			     m = m->m_next) {
    532  1.2.4.2  minoura 				/* Find the beginning mbuf. */
    533  1.2.4.2  minoura 				if (moff >= m->m_len) {
    534  1.2.4.2  minoura 					moff -= m->m_len;
    535  1.2.4.2  minoura 					continue;
    536  1.2.4.2  minoura 				}
    537  1.2.4.2  minoura 
    538  1.2.4.2  minoura 				/*
    539  1.2.4.2  minoura 				 * Now at the first mbuf to sync; nail
    540  1.2.4.2  minoura 				 * each one until we have exhausted the
    541  1.2.4.2  minoura 				 * length.
    542  1.2.4.2  minoura 				 */
    543  1.2.4.2  minoura 				minlen = len < m->m_len - moff ?
    544  1.2.4.2  minoura 				    len : m->m_len - moff;
    545  1.2.4.2  minoura 
    546  1.2.4.2  minoura 				memcpy(mtod(m, caddr_t) + moff,
    547  1.2.4.2  minoura 				    (char *)cookie->id_bouncebuf + offset,
    548  1.2.4.2  minoura 				    minlen);
    549  1.2.4.2  minoura 
    550  1.2.4.2  minoura 				moff = 0;
    551  1.2.4.2  minoura 				len -= minlen;
    552  1.2.4.2  minoura 				offset += minlen;
    553  1.2.4.2  minoura 			}
    554  1.2.4.2  minoura 		}
    555  1.2.4.2  minoura 
    556  1.2.4.2  minoura 		/*
    557  1.2.4.2  minoura 		 * Nothing to do for post-write.
    558  1.2.4.2  minoura 		 */
    559  1.2.4.2  minoura 		break;
    560  1.2.4.2  minoura 	    }
    561  1.2.4.2  minoura 
    562  1.2.4.2  minoura 	case ID_BUFTYPE_UIO:
    563  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_UIO");
    564  1.2.4.2  minoura 		break;
    565  1.2.4.2  minoura 
    566  1.2.4.2  minoura 	case ID_BUFTYPE_RAW:
    567  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_RAW");
    568  1.2.4.2  minoura 		break;
    569  1.2.4.2  minoura 
    570  1.2.4.2  minoura 	case ID_BUFTYPE_INVALID:
    571  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_sync: ID_BUFTYPE_INVALID");
    572  1.2.4.2  minoura 		break;
    573  1.2.4.2  minoura 
    574  1.2.4.2  minoura 	default:
    575  1.2.4.2  minoura 		printf("unknown buffer type %d\n", cookie->id_buftype);
    576  1.2.4.2  minoura 		panic("isadma_bounce_dmamap_sync");
    577  1.2.4.2  minoura 	}
    578  1.2.4.2  minoura 
    579  1.2.4.2  minoura 	if (ops & BUS_DMASYNC_PREWRITE)
    580  1.2.4.2  minoura 		((*sync)(t, map, offset, len, ops));
    581  1.2.4.2  minoura }
    582  1.2.4.2  minoura 
    583  1.2.4.2  minoura /*
    584  1.2.4.2  minoura  * Allocate memory safe for ISA DMA.
    585  1.2.4.2  minoura  */
    586  1.2.4.2  minoura int
    587  1.2.4.2  minoura isadma_bounce_dmamem_alloc(bus_dma_tag_t t, bus_size_t size,
    588  1.2.4.2  minoura     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
    589  1.2.4.2  minoura     int nsegs, int *rsegs, int flags)
    590  1.2.4.2  minoura {
    591  1.2.4.2  minoura 	paddr_t high;
    592  1.2.4.2  minoura 
    593  1.2.4.2  minoura 	if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
    594  1.2.4.2  minoura 		high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
    595  1.2.4.2  minoura 	else
    596  1.2.4.2  minoura 		high = trunc_page(avail_end);
    597  1.2.4.2  minoura 
    598  1.2.4.2  minoura 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    599  1.2.4.2  minoura 	    segs, nsegs, rsegs, flags, 0, high));
    600  1.2.4.2  minoura }
    601  1.2.4.2  minoura 
    602  1.2.4.2  minoura /**********************************************************************
    603  1.2.4.2  minoura  * ISA DMA utility functions
    604  1.2.4.2  minoura  **********************************************************************/
    605  1.2.4.2  minoura 
    606  1.2.4.2  minoura static int
    607  1.2.4.2  minoura isadma_bounce_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
    608  1.2.4.2  minoura     bus_size_t size, int flags)
    609  1.2.4.2  minoura {
    610  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    611  1.2.4.2  minoura 	int error = 0;
    612  1.2.4.2  minoura 
    613  1.2.4.2  minoura 	cookie->id_bouncebuflen = round_page(size);
    614  1.2.4.2  minoura 	error = isadma_bounce_dmamem_alloc(t, cookie->id_bouncebuflen,
    615  1.2.4.2  minoura 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
    616  1.2.4.2  minoura 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
    617  1.2.4.2  minoura 	if (error)
    618  1.2.4.2  minoura 		goto out;
    619  1.2.4.2  minoura 	error = _bus_dmamem_map(t, cookie->id_bouncesegs,
    620  1.2.4.2  minoura 	    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
    621  1.2.4.2  minoura 	    (caddr_t *)&cookie->id_bouncebuf, flags);
    622  1.2.4.2  minoura 
    623  1.2.4.2  minoura  out:
    624  1.2.4.2  minoura 	if (error) {
    625  1.2.4.2  minoura 		_bus_dmamem_free(t, cookie->id_bouncesegs,
    626  1.2.4.2  minoura 		    cookie->id_nbouncesegs);
    627  1.2.4.2  minoura 		cookie->id_bouncebuflen = 0;
    628  1.2.4.2  minoura 		cookie->id_nbouncesegs = 0;
    629  1.2.4.2  minoura 	} else
    630  1.2.4.2  minoura 		cookie->id_flags |= ID_HAS_BOUNCE;
    631  1.2.4.2  minoura 
    632  1.2.4.2  minoura 	return (error);
    633  1.2.4.2  minoura }
    634  1.2.4.2  minoura 
    635  1.2.4.2  minoura static void
    636  1.2.4.2  minoura isadma_bounce_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
    637  1.2.4.2  minoura {
    638  1.2.4.2  minoura 	struct isadma_bounce_cookie *cookie = map->_dm_cookie;
    639  1.2.4.2  minoura 
    640  1.2.4.2  minoura 	_bus_dmamem_unmap(t, cookie->id_bouncebuf,
    641  1.2.4.2  minoura 	    cookie->id_bouncebuflen);
    642  1.2.4.2  minoura 	_bus_dmamem_free(t, cookie->id_bouncesegs,
    643  1.2.4.2  minoura 	    cookie->id_nbouncesegs);
    644  1.2.4.2  minoura 	cookie->id_bouncebuflen = 0;
    645  1.2.4.2  minoura 	cookie->id_nbouncesegs = 0;
    646  1.2.4.2  minoura 	cookie->id_flags &= ~ID_HAS_BOUNCE;
    647  1.2.4.2  minoura }
    648