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