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