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