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