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