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