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