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