Home | History | Annotate | Line # | Download | only in arm32
bus_dma.c revision 1.52.16.1
      1  1.52.16.1      matt /*	$NetBSD: bus_dma.c,v 1.52.16.1 2014/02/15 16:18:36 matt Exp $	*/
      2        1.1     chris 
      3        1.1     chris /*-
      4        1.1     chris  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5        1.1     chris  * All rights reserved.
      6        1.1     chris  *
      7        1.1     chris  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1     chris  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9        1.1     chris  * NASA Ames Research Center.
     10        1.1     chris  *
     11        1.1     chris  * Redistribution and use in source and binary forms, with or without
     12        1.1     chris  * modification, are permitted provided that the following conditions
     13        1.1     chris  * are met:
     14        1.1     chris  * 1. Redistributions of source code must retain the above copyright
     15        1.1     chris  *    notice, this list of conditions and the following disclaimer.
     16        1.1     chris  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1     chris  *    notice, this list of conditions and the following disclaimer in the
     18        1.1     chris  *    documentation and/or other materials provided with the distribution.
     19        1.1     chris  *
     20        1.1     chris  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1     chris  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1     chris  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1     chris  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1     chris  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1     chris  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1     chris  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1     chris  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1     chris  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1     chris  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1     chris  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1     chris  */
     32       1.33     lukem 
     33       1.35  rearnsha #define _ARM32_BUS_DMA_PRIVATE
     34       1.35  rearnsha 
     35       1.33     lukem #include <sys/cdefs.h>
     36  1.52.16.1      matt __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.52.16.1 2014/02/15 16:18:36 matt Exp $");
     37        1.1     chris 
     38        1.1     chris #include <sys/param.h>
     39        1.1     chris #include <sys/systm.h>
     40        1.1     chris #include <sys/kernel.h>
     41        1.1     chris #include <sys/proc.h>
     42        1.1     chris #include <sys/buf.h>
     43        1.1     chris #include <sys/reboot.h>
     44        1.1     chris #include <sys/conf.h>
     45        1.1     chris #include <sys/file.h>
     46        1.1     chris #include <sys/malloc.h>
     47        1.1     chris #include <sys/mbuf.h>
     48        1.1     chris #include <sys/vnode.h>
     49        1.1     chris #include <sys/device.h>
     50        1.1     chris 
     51  1.52.16.1      matt #include <uvm/uvm.h>
     52        1.1     chris 
     53  1.52.16.1      matt #include <sys/bus.h>
     54        1.1     chris #include <machine/cpu.h>
     55        1.4   thorpej 
     56        1.4   thorpej #include <arm/cpufunc.h>
     57        1.1     chris 
     58  1.52.16.1      matt #ifdef BUSDMA_COUNTERS
     59  1.52.16.1      matt static struct evcnt bus_dma_creates =
     60  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "creates");
     61  1.52.16.1      matt static struct evcnt bus_dma_bounced_creates =
     62  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced creates");
     63  1.52.16.1      matt static struct evcnt bus_dma_loads =
     64  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "loads");
     65  1.52.16.1      matt static struct evcnt bus_dma_bounced_loads =
     66  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced loads");
     67  1.52.16.1      matt static struct evcnt bus_dma_read_bounces =
     68  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "read bounces");
     69  1.52.16.1      matt static struct evcnt bus_dma_write_bounces =
     70  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "write bounces");
     71  1.52.16.1      matt static struct evcnt bus_dma_bounced_unloads =
     72  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced unloads");
     73  1.52.16.1      matt static struct evcnt bus_dma_unloads =
     74  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "unloads");
     75  1.52.16.1      matt static struct evcnt bus_dma_bounced_destroys =
     76  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced destroys");
     77  1.52.16.1      matt static struct evcnt bus_dma_destroys =
     78  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "destroys");
     79  1.52.16.1      matt static struct evcnt bus_dma_sync_prereadwrite =
     80  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prereadwrite");
     81  1.52.16.1      matt static struct evcnt bus_dma_sync_preread_begin =
     82  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread begin");
     83  1.52.16.1      matt static struct evcnt bus_dma_sync_preread =
     84  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread");
     85  1.52.16.1      matt static struct evcnt bus_dma_sync_preread_tail =
     86  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread tail");
     87  1.52.16.1      matt static struct evcnt bus_dma_sync_prewrite =
     88  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prewrite");
     89  1.52.16.1      matt static struct evcnt bus_dma_sync_postread =
     90  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postread");
     91  1.52.16.1      matt static struct evcnt bus_dma_sync_postreadwrite =
     92  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postreadwrite");
     93  1.52.16.1      matt static struct evcnt bus_dma_sync_postwrite =
     94  1.52.16.1      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postwrite");
     95  1.52.16.1      matt 
     96  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_creates);
     97  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_bounced_creates);
     98  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_loads);
     99  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_bounced_loads);
    100  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_read_bounces);
    101  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_write_bounces);
    102  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_unloads);
    103  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_bounced_unloads);
    104  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_destroys);
    105  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_bounced_destroys);
    106  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_prereadwrite);
    107  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_preread_begin);
    108  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_preread);
    109  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_preread_tail);
    110  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_prewrite);
    111  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_postread);
    112  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_postreadwrite);
    113  1.52.16.1      matt EVCNT_ATTACH_STATIC(bus_dma_sync_postwrite);
    114  1.52.16.1      matt 
    115  1.52.16.1      matt #define	STAT_INCR(x)	(bus_dma_ ## x.ev_count++)
    116  1.52.16.1      matt #else
    117  1.52.16.1      matt #define	STAT_INCR(x)	/*(bus_dma_ ## x.ev_count++)*/
    118  1.52.16.1      matt #endif
    119  1.52.16.1      matt 
    120        1.7   thorpej int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
    121       1.48      yamt 	    bus_size_t, struct vmspace *, int);
    122  1.52.16.1      matt static struct arm32_dma_range *
    123  1.52.16.1      matt 	_bus_dma_paddr_inrange(struct arm32_dma_range *, int, paddr_t);
    124        1.1     chris 
    125        1.1     chris /*
    126       1.19    briggs  * Check to see if the specified page is in an allowed DMA range.
    127       1.19    briggs  */
    128       1.47     perry inline struct arm32_dma_range *
    129  1.52.16.1      matt _bus_dma_paddr_inrange(struct arm32_dma_range *ranges, int nranges,
    130       1.19    briggs     bus_addr_t curaddr)
    131       1.19    briggs {
    132       1.19    briggs 	struct arm32_dma_range *dr;
    133       1.19    briggs 	int i;
    134       1.19    briggs 
    135       1.19    briggs 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
    136       1.19    briggs 		if (curaddr >= dr->dr_sysbase &&
    137       1.19    briggs 		    round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
    138       1.19    briggs 			return (dr);
    139       1.19    briggs 	}
    140       1.19    briggs 
    141       1.19    briggs 	return (NULL);
    142       1.19    briggs }
    143       1.19    briggs 
    144       1.19    briggs /*
    145  1.52.16.1      matt  * Check to see if the specified busaddr is in an allowed DMA range.
    146  1.52.16.1      matt  */
    147  1.52.16.1      matt static inline paddr_t
    148  1.52.16.1      matt _bus_dma_busaddr_to_paddr(bus_dma_tag_t t, bus_addr_t curaddr)
    149  1.52.16.1      matt {
    150  1.52.16.1      matt 	struct arm32_dma_range *dr;
    151  1.52.16.1      matt 	u_int i;
    152  1.52.16.1      matt 
    153  1.52.16.1      matt 	if (t->_nranges == 0)
    154  1.52.16.1      matt 		return curaddr;
    155  1.52.16.1      matt 
    156  1.52.16.1      matt 	for (i = 0, dr = t->_ranges; i < t->_nranges; i++, dr++) {
    157  1.52.16.1      matt 		if (dr->dr_busbase <= curaddr
    158  1.52.16.1      matt 		    && round_page(curaddr) <= dr->dr_busbase + dr->dr_len)
    159  1.52.16.1      matt 			return curaddr - dr->dr_busbase + dr->dr_sysbase;
    160  1.52.16.1      matt 	}
    161  1.52.16.1      matt 	panic("%s: curaddr %#lx not in range", __func__, curaddr);
    162  1.52.16.1      matt }
    163  1.52.16.1      matt 
    164  1.52.16.1      matt /*
    165       1.41   thorpej  * Common function to load the specified physical address into the
    166       1.41   thorpej  * DMA map, coalescing segments and boundary checking as necessary.
    167       1.41   thorpej  */
    168       1.41   thorpej static int
    169       1.41   thorpej _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
    170  1.52.16.1      matt     bus_addr_t paddr, bus_size_t size, bool coherent)
    171       1.41   thorpej {
    172       1.41   thorpej 	bus_dma_segment_t * const segs = map->dm_segs;
    173       1.41   thorpej 	int nseg = map->dm_nsegs;
    174  1.52.16.1      matt 	bus_addr_t lastaddr;
    175       1.41   thorpej 	bus_addr_t bmask = ~(map->_dm_boundary - 1);
    176       1.41   thorpej 	bus_addr_t curaddr;
    177       1.41   thorpej 	bus_size_t sgsize;
    178  1.52.16.1      matt 	uint32_t _ds_flags = coherent ? _BUS_DMAMAP_COHERENT : 0;
    179       1.41   thorpej 
    180       1.41   thorpej 	if (nseg > 0)
    181       1.41   thorpej 		lastaddr = segs[nseg-1].ds_addr + segs[nseg-1].ds_len;
    182  1.52.16.1      matt 	else
    183  1.52.16.1      matt 		lastaddr = 0xdead;
    184  1.52.16.1      matt 
    185       1.41   thorpej  again:
    186       1.41   thorpej 	sgsize = size;
    187       1.41   thorpej 
    188       1.41   thorpej 	/* Make sure we're in an allowed DMA range. */
    189       1.41   thorpej 	if (t->_ranges != NULL) {
    190       1.41   thorpej 		/* XXX cache last result? */
    191       1.41   thorpej 		const struct arm32_dma_range * const dr =
    192  1.52.16.1      matt 		    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, paddr);
    193       1.41   thorpej 		if (dr == NULL)
    194       1.41   thorpej 			return (EINVAL);
    195  1.52.16.1      matt 
    196  1.52.16.1      matt 		/*
    197  1.52.16.1      matt 		 * If this region is coherent, mark the segment as coherent.
    198  1.52.16.1      matt 		 */
    199  1.52.16.1      matt 		_ds_flags |= dr->dr_flags & _BUS_DMAMAP_COHERENT;
    200  1.52.16.1      matt 
    201       1.41   thorpej 		/*
    202       1.41   thorpej 		 * In a valid DMA range.  Translate the physical
    203       1.41   thorpej 		 * memory address to an address in the DMA window.
    204       1.41   thorpej 		 */
    205       1.41   thorpej 		curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
    206  1.52.16.1      matt #if 0
    207  1.52.16.1      matt 		printf("%p: %#lx: range %#lx/%#lx/%#lx/%#x: %#x <-- %#lx\n",
    208  1.52.16.1      matt 		    t, paddr, dr->dr_sysbase, dr->dr_busbase,
    209  1.52.16.1      matt 		    dr->dr_len, dr->dr_flags, _ds_flags, curaddr);
    210  1.52.16.1      matt #endif
    211       1.41   thorpej 	} else
    212       1.41   thorpej 		curaddr = paddr;
    213       1.41   thorpej 
    214       1.41   thorpej 	/*
    215       1.41   thorpej 	 * Make sure we don't cross any boundaries.
    216       1.41   thorpej 	 */
    217       1.41   thorpej 	if (map->_dm_boundary > 0) {
    218       1.41   thorpej 		bus_addr_t baddr;	/* next boundary address */
    219       1.41   thorpej 
    220       1.41   thorpej 		baddr = (curaddr + map->_dm_boundary) & bmask;
    221       1.41   thorpej 		if (sgsize > (baddr - curaddr))
    222       1.41   thorpej 			sgsize = (baddr - curaddr);
    223       1.41   thorpej 	}
    224       1.41   thorpej 
    225       1.41   thorpej 	/*
    226       1.41   thorpej 	 * Insert chunk into a segment, coalescing with the
    227       1.41   thorpej 	 * previous segment if possible.
    228       1.41   thorpej 	 */
    229       1.41   thorpej 	if (nseg > 0 && curaddr == lastaddr &&
    230       1.43      matt 	    segs[nseg-1].ds_len + sgsize <= map->dm_maxsegsz &&
    231  1.52.16.1      matt 	    ((segs[nseg-1]._ds_flags ^ _ds_flags) & _BUS_DMAMAP_COHERENT) == 0 &&
    232       1.41   thorpej 	    (map->_dm_boundary == 0 ||
    233       1.41   thorpej 	     (segs[nseg-1].ds_addr & bmask) == (curaddr & bmask))) {
    234       1.41   thorpej 	     	/* coalesce */
    235       1.41   thorpej 		segs[nseg-1].ds_len += sgsize;
    236       1.41   thorpej 	} else if (nseg >= map->_dm_segcnt) {
    237       1.41   thorpej 		return (EFBIG);
    238       1.41   thorpej 	} else {
    239       1.41   thorpej 		/* new segment */
    240       1.41   thorpej 		segs[nseg].ds_addr = curaddr;
    241       1.41   thorpej 		segs[nseg].ds_len = sgsize;
    242  1.52.16.1      matt 		segs[nseg]._ds_flags = _ds_flags;
    243       1.41   thorpej 		nseg++;
    244       1.41   thorpej 	}
    245       1.41   thorpej 
    246       1.41   thorpej 	lastaddr = curaddr + sgsize;
    247       1.41   thorpej 
    248       1.41   thorpej 	paddr += sgsize;
    249       1.41   thorpej 	size -= sgsize;
    250       1.41   thorpej 	if (size > 0)
    251       1.41   thorpej 		goto again;
    252  1.52.16.1      matt 
    253  1.52.16.1      matt 	map->_dm_flags &= (_ds_flags & _BUS_DMAMAP_COHERENT);
    254       1.41   thorpej 	map->dm_nsegs = nseg;
    255       1.41   thorpej 	return (0);
    256       1.41   thorpej }
    257       1.41   thorpej 
    258  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    259  1.52.16.1      matt static int _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
    260  1.52.16.1      matt 	    bus_size_t size, int flags);
    261  1.52.16.1      matt static void _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map);
    262  1.52.16.1      matt static int _bus_dma_uiomove(void *buf, struct uio *uio, size_t n,
    263  1.52.16.1      matt 	    int direction);
    264  1.52.16.1      matt 
    265  1.52.16.1      matt static int
    266  1.52.16.1      matt _bus_dma_load_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    267  1.52.16.1      matt 	size_t buflen, int buftype, int flags)
    268  1.52.16.1      matt {
    269  1.52.16.1      matt 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
    270  1.52.16.1      matt 	struct vmspace * const vm = vmspace_kernel();
    271  1.52.16.1      matt 	int error;
    272  1.52.16.1      matt 
    273  1.52.16.1      matt 	KASSERT(cookie != NULL);
    274  1.52.16.1      matt 	KASSERT(cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE);
    275  1.52.16.1      matt 
    276  1.52.16.1      matt 	/*
    277  1.52.16.1      matt 	 * Allocate bounce pages, if necessary.
    278  1.52.16.1      matt 	 */
    279  1.52.16.1      matt 	if ((cookie->id_flags & _BUS_DMA_HAS_BOUNCE) == 0) {
    280  1.52.16.1      matt 		error = _bus_dma_alloc_bouncebuf(t, map, buflen, flags);
    281  1.52.16.1      matt 		if (error)
    282  1.52.16.1      matt 			return (error);
    283  1.52.16.1      matt 	}
    284  1.52.16.1      matt 
    285  1.52.16.1      matt 	/*
    286  1.52.16.1      matt 	 * Cache a pointer to the caller's buffer and load the DMA map
    287  1.52.16.1      matt 	 * with the bounce buffer.
    288  1.52.16.1      matt 	 */
    289  1.52.16.1      matt 	cookie->id_origbuf = buf;
    290  1.52.16.1      matt 	cookie->id_origbuflen = buflen;
    291  1.52.16.1      matt 	error = _bus_dmamap_load_buffer(t, map, cookie->id_bouncebuf,
    292  1.52.16.1      matt 	    buflen, vm, flags);
    293  1.52.16.1      matt 	if (error)
    294  1.52.16.1      matt 		return (error);
    295  1.52.16.1      matt 
    296  1.52.16.1      matt 	STAT_INCR(bounced_loads);
    297  1.52.16.1      matt 	map->dm_mapsize = buflen;
    298  1.52.16.1      matt 	map->_dm_vmspace = vm;
    299  1.52.16.1      matt 	map->_dm_buftype = buftype;
    300  1.52.16.1      matt 
    301  1.52.16.1      matt 	/* ...so _bus_dmamap_sync() knows we're bouncing */
    302  1.52.16.1      matt 	map->_dm_flags |= _BUS_DMAMAP_IS_BOUNCING;
    303  1.52.16.1      matt 	cookie->id_flags |= _BUS_DMA_IS_BOUNCING;
    304  1.52.16.1      matt 	return 0;
    305  1.52.16.1      matt }
    306  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
    307  1.52.16.1      matt 
    308       1.41   thorpej /*
    309        1.1     chris  * Common function for DMA map creation.  May be called by bus-specific
    310        1.1     chris  * DMA map creation functions.
    311        1.1     chris  */
    312        1.1     chris int
    313        1.7   thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    314        1.7   thorpej     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    315        1.1     chris {
    316        1.1     chris 	struct arm32_bus_dmamap *map;
    317        1.1     chris 	void *mapstore;
    318        1.1     chris 	size_t mapsize;
    319        1.1     chris 
    320        1.1     chris #ifdef DEBUG_DMA
    321        1.1     chris 	printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
    322        1.1     chris 	    t, size, nsegments, maxsegsz, boundary, flags);
    323        1.1     chris #endif	/* DEBUG_DMA */
    324        1.1     chris 
    325        1.1     chris 	/*
    326        1.1     chris 	 * Allocate and initialize the DMA map.  The end of the map
    327        1.1     chris 	 * is a variable-sized array of segments, so we allocate enough
    328        1.1     chris 	 * room for them in one shot.
    329        1.1     chris 	 *
    330        1.1     chris 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
    331        1.1     chris 	 * of ALLOCNOW notifies others that we've reserved these resources,
    332        1.1     chris 	 * and they are not to be freed.
    333        1.1     chris 	 *
    334        1.1     chris 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
    335        1.1     chris 	 * the (nsegments - 1).
    336        1.1     chris 	 */
    337        1.1     chris 	mapsize = sizeof(struct arm32_bus_dmamap) +
    338        1.1     chris 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
    339  1.52.16.1      matt 	const int mallocflags = M_ZERO|(flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK;
    340  1.52.16.1      matt 	if ((mapstore = malloc(mapsize, M_DMAMAP, mallocflags)) == NULL)
    341        1.1     chris 		return (ENOMEM);
    342        1.1     chris 
    343        1.1     chris 	map = (struct arm32_bus_dmamap *)mapstore;
    344        1.1     chris 	map->_dm_size = size;
    345        1.1     chris 	map->_dm_segcnt = nsegments;
    346       1.43      matt 	map->_dm_maxmaxsegsz = maxsegsz;
    347        1.1     chris 	map->_dm_boundary = boundary;
    348        1.1     chris 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    349       1.14   thorpej 	map->_dm_origbuf = NULL;
    350  1.52.16.1      matt 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
    351       1.48      yamt 	map->_dm_vmspace = vmspace_kernel();
    352  1.52.16.1      matt 	map->_dm_cookie = NULL;
    353       1.43      matt 	map->dm_maxsegsz = maxsegsz;
    354        1.1     chris 	map->dm_mapsize = 0;		/* no valid mappings */
    355        1.1     chris 	map->dm_nsegs = 0;
    356        1.1     chris 
    357        1.1     chris 	*dmamp = map;
    358  1.52.16.1      matt 
    359  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    360  1.52.16.1      matt 	struct arm32_bus_dma_cookie *cookie;
    361  1.52.16.1      matt 	int cookieflags;
    362  1.52.16.1      matt 	void *cookiestore;
    363  1.52.16.1      matt 	size_t cookiesize;
    364  1.52.16.1      matt 	int error;
    365  1.52.16.1      matt 
    366  1.52.16.1      matt 	cookieflags = 0;
    367  1.52.16.1      matt 
    368  1.52.16.1      matt 	if (t->_may_bounce != NULL) {
    369  1.52.16.1      matt 		error = (*t->_may_bounce)(t, map, flags, &cookieflags);
    370  1.52.16.1      matt 		if (error != 0)
    371  1.52.16.1      matt 			goto out;
    372  1.52.16.1      matt 	}
    373  1.52.16.1      matt 
    374  1.52.16.1      matt 	if (t->_ranges != NULL)
    375  1.52.16.1      matt 		cookieflags |= _BUS_DMA_MIGHT_NEED_BOUNCE;
    376  1.52.16.1      matt 
    377  1.52.16.1      matt 	if ((cookieflags & _BUS_DMA_MIGHT_NEED_BOUNCE) == 0) {
    378  1.52.16.1      matt 		STAT_INCR(creates);
    379  1.52.16.1      matt 		return 0;
    380  1.52.16.1      matt 	}
    381  1.52.16.1      matt 
    382  1.52.16.1      matt 	cookiesize = sizeof(struct arm32_bus_dma_cookie) +
    383  1.52.16.1      matt 	    (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
    384  1.52.16.1      matt 
    385  1.52.16.1      matt 	/*
    386  1.52.16.1      matt 	 * Allocate our cookie.
    387  1.52.16.1      matt 	 */
    388  1.52.16.1      matt 	if ((cookiestore = malloc(cookiesize, M_DMAMAP, mallocflags)) == NULL) {
    389  1.52.16.1      matt 		error = ENOMEM;
    390  1.52.16.1      matt 		goto out;
    391  1.52.16.1      matt 	}
    392  1.52.16.1      matt 	cookie = (struct arm32_bus_dma_cookie *)cookiestore;
    393  1.52.16.1      matt 	cookie->id_flags = cookieflags;
    394  1.52.16.1      matt 	map->_dm_cookie = cookie;
    395  1.52.16.1      matt 	STAT_INCR(bounced_creates);
    396  1.52.16.1      matt 
    397  1.52.16.1      matt 	error = _bus_dma_alloc_bouncebuf(t, map, size, flags);
    398  1.52.16.1      matt  out:
    399  1.52.16.1      matt 	if (error)
    400  1.52.16.1      matt 		_bus_dmamap_destroy(t, map);
    401  1.52.16.1      matt #else
    402  1.52.16.1      matt 	STAT_INCR(creates);
    403  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
    404  1.52.16.1      matt 
    405        1.1     chris #ifdef DEBUG_DMA
    406        1.1     chris 	printf("dmamap_create:map=%p\n", map);
    407        1.1     chris #endif	/* DEBUG_DMA */
    408        1.1     chris 	return (0);
    409        1.1     chris }
    410        1.1     chris 
    411        1.1     chris /*
    412        1.1     chris  * Common function for DMA map destruction.  May be called by bus-specific
    413        1.1     chris  * DMA map destruction functions.
    414        1.1     chris  */
    415        1.1     chris void
    416        1.7   thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    417        1.1     chris {
    418        1.1     chris 
    419        1.1     chris #ifdef DEBUG_DMA
    420        1.1     chris 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
    421        1.1     chris #endif	/* DEBUG_DMA */
    422  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    423  1.52.16.1      matt 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
    424       1.13    briggs 
    425       1.13    briggs 	/*
    426  1.52.16.1      matt 	 * Free any bounce pages this map might hold.
    427       1.13    briggs 	 */
    428  1.52.16.1      matt 	if (cookie != NULL) {
    429  1.52.16.1      matt 		if (cookie->id_flags & _BUS_DMA_IS_BOUNCING)
    430  1.52.16.1      matt 			STAT_INCR(bounced_unloads);
    431  1.52.16.1      matt 		map->dm_nsegs = 0;
    432  1.52.16.1      matt 		if (cookie->id_flags & _BUS_DMA_HAS_BOUNCE)
    433  1.52.16.1      matt 			_bus_dma_free_bouncebuf(t, map);
    434  1.52.16.1      matt 		STAT_INCR(bounced_destroys);
    435  1.52.16.1      matt 		free(cookie, M_DMAMAP);
    436  1.52.16.1      matt 	} else
    437  1.52.16.1      matt #endif
    438  1.52.16.1      matt 	STAT_INCR(destroys);
    439  1.52.16.1      matt 
    440  1.52.16.1      matt 	if (map->dm_nsegs > 0)
    441  1.52.16.1      matt 		STAT_INCR(unloads);
    442       1.13    briggs 
    443       1.25     chris 	free(map, M_DMAMAP);
    444        1.1     chris }
    445        1.1     chris 
    446        1.1     chris /*
    447        1.1     chris  * Common function for loading a DMA map with a linear buffer.  May
    448        1.1     chris  * be called by bus-specific DMA map load functions.
    449        1.1     chris  */
    450        1.1     chris int
    451        1.7   thorpej _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    452        1.7   thorpej     bus_size_t buflen, struct proc *p, int flags)
    453        1.1     chris {
    454       1.48      yamt 	struct vmspace *vm;
    455  1.52.16.1      matt 	int error;
    456        1.1     chris 
    457        1.1     chris #ifdef DEBUG_DMA
    458        1.1     chris 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
    459        1.1     chris 	    t, map, buf, buflen, p, flags);
    460        1.1     chris #endif	/* DEBUG_DMA */
    461        1.1     chris 
    462  1.52.16.1      matt 	if (map->dm_nsegs > 0) {
    463  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    464  1.52.16.1      matt 		struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
    465  1.52.16.1      matt 		if (cookie != NULL) {
    466  1.52.16.1      matt 			if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
    467  1.52.16.1      matt 				STAT_INCR(bounced_unloads);
    468  1.52.16.1      matt 				cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
    469  1.52.16.1      matt 				map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
    470  1.52.16.1      matt 			}
    471  1.52.16.1      matt 		} else
    472  1.52.16.1      matt #endif
    473  1.52.16.1      matt 		STAT_INCR(unloads);
    474  1.52.16.1      matt 	}
    475  1.52.16.1      matt 
    476        1.1     chris 	/*
    477        1.1     chris 	 * Make sure that on error condition we return "no valid mappings".
    478        1.1     chris 	 */
    479        1.1     chris 	map->dm_mapsize = 0;
    480        1.1     chris 	map->dm_nsegs = 0;
    481  1.52.16.1      matt 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
    482  1.52.16.1      matt 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
    483  1.52.16.1      matt 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
    484  1.52.16.1      matt 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
    485        1.1     chris 
    486        1.1     chris 	if (buflen > map->_dm_size)
    487        1.1     chris 		return (EINVAL);
    488        1.1     chris 
    489       1.48      yamt 	if (p != NULL) {
    490       1.48      yamt 		vm = p->p_vmspace;
    491       1.48      yamt 	} else {
    492       1.48      yamt 		vm = vmspace_kernel();
    493       1.48      yamt 	}
    494       1.48      yamt 
    495       1.17   thorpej 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    496  1.52.16.1      matt 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
    497       1.17   thorpej 
    498       1.48      yamt 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
    499        1.1     chris 	if (error == 0) {
    500        1.1     chris 		map->dm_mapsize = buflen;
    501       1.48      yamt 		map->_dm_vmspace = vm;
    502  1.52.16.1      matt 		map->_dm_origbuf = buf;
    503  1.52.16.1      matt 		map->_dm_buftype = _BUS_DMA_BUFTYPE_LINEAR;
    504  1.52.16.1      matt 		return 0;
    505        1.1     chris 	}
    506  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    507  1.52.16.1      matt 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
    508  1.52.16.1      matt 	if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
    509  1.52.16.1      matt 		error = _bus_dma_load_bouncebuf(t, map, buf, buflen,
    510  1.52.16.1      matt 		    _BUS_DMA_BUFTYPE_LINEAR, flags);
    511  1.52.16.1      matt 	}
    512  1.52.16.1      matt #endif
    513        1.1     chris 	return (error);
    514        1.1     chris }
    515        1.1     chris 
    516        1.1     chris /*
    517        1.1     chris  * Like _bus_dmamap_load(), but for mbufs.
    518        1.1     chris  */
    519        1.1     chris int
    520        1.7   thorpej _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    521        1.7   thorpej     int flags)
    522        1.1     chris {
    523       1.41   thorpej 	int error;
    524        1.1     chris 	struct mbuf *m;
    525        1.1     chris 
    526        1.1     chris #ifdef DEBUG_DMA
    527        1.1     chris 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
    528        1.1     chris 	    t, map, m0, flags);
    529        1.1     chris #endif	/* DEBUG_DMA */
    530        1.1     chris 
    531  1.52.16.1      matt 	if (map->dm_nsegs > 0) {
    532  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    533  1.52.16.1      matt 		struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
    534  1.52.16.1      matt 		if (cookie != NULL) {
    535  1.52.16.1      matt 			if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
    536  1.52.16.1      matt 				STAT_INCR(bounced_unloads);
    537  1.52.16.1      matt 				cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
    538  1.52.16.1      matt 				map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
    539  1.52.16.1      matt 			}
    540  1.52.16.1      matt 		} else
    541  1.52.16.1      matt #endif
    542  1.52.16.1      matt 		STAT_INCR(unloads);
    543  1.52.16.1      matt 	}
    544  1.52.16.1      matt 
    545        1.1     chris 	/*
    546        1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    547        1.1     chris 	 */
    548        1.1     chris 	map->dm_mapsize = 0;
    549        1.1     chris 	map->dm_nsegs = 0;
    550  1.52.16.1      matt 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
    551  1.52.16.1      matt 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
    552  1.52.16.1      matt 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
    553  1.52.16.1      matt 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
    554        1.1     chris 
    555  1.52.16.1      matt 	KASSERT(m0->m_flags & M_PKTHDR);
    556        1.1     chris 
    557        1.1     chris 	if (m0->m_pkthdr.len > map->_dm_size)
    558        1.1     chris 		return (EINVAL);
    559        1.1     chris 
    560  1.52.16.1      matt 	/* _bus_dmamap_load_paddr() clears this if we're not... */
    561  1.52.16.1      matt 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
    562       1.17   thorpej 
    563        1.1     chris 	error = 0;
    564        1.1     chris 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    565       1.41   thorpej 		int offset;
    566       1.41   thorpej 		int remainbytes;
    567       1.41   thorpej 		const struct vm_page * const *pgs;
    568       1.41   thorpej 		paddr_t paddr;
    569       1.41   thorpej 		int size;
    570       1.41   thorpej 
    571       1.28   thorpej 		if (m->m_len == 0)
    572       1.28   thorpej 			continue;
    573  1.52.16.1      matt 		/*
    574  1.52.16.1      matt 		 * Don't allow reads in read-only mbufs.
    575  1.52.16.1      matt 		 */
    576  1.52.16.1      matt 		if (M_ROMAP(m) && (flags & BUS_DMA_READ)) {
    577  1.52.16.1      matt 			error = EFAULT;
    578  1.52.16.1      matt 			break;
    579  1.52.16.1      matt 		}
    580       1.41   thorpej 		switch (m->m_flags & (M_EXT|M_CLUSTER|M_EXT_PAGES)) {
    581       1.28   thorpej 		case M_EXT|M_CLUSTER:
    582       1.28   thorpej 			/* XXX KDASSERT */
    583       1.28   thorpej 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
    584       1.41   thorpej 			paddr = m->m_ext.ext_paddr +
    585       1.28   thorpej 			    (m->m_data - m->m_ext.ext_buf);
    586       1.41   thorpej 			size = m->m_len;
    587  1.52.16.1      matt 			error = _bus_dmamap_load_paddr(t, map, paddr, size,
    588  1.52.16.1      matt 			    false);
    589       1.41   thorpej 			break;
    590       1.41   thorpej 
    591       1.41   thorpej 		case M_EXT|M_EXT_PAGES:
    592       1.41   thorpej 			KASSERT(m->m_ext.ext_buf <= m->m_data);
    593       1.41   thorpej 			KASSERT(m->m_data <=
    594       1.41   thorpej 			    m->m_ext.ext_buf + m->m_ext.ext_size);
    595       1.41   thorpej 
    596       1.41   thorpej 			offset = (vaddr_t)m->m_data -
    597       1.41   thorpej 			    trunc_page((vaddr_t)m->m_ext.ext_buf);
    598       1.41   thorpej 			remainbytes = m->m_len;
    599       1.41   thorpej 
    600       1.41   thorpej 			/* skip uninteresting pages */
    601       1.41   thorpej 			pgs = (const struct vm_page * const *)
    602       1.41   thorpej 			    m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
    603       1.41   thorpej 
    604       1.41   thorpej 			offset &= PAGE_MASK;	/* offset in the first page */
    605       1.41   thorpej 
    606       1.41   thorpej 			/* load each page */
    607       1.41   thorpej 			while (remainbytes > 0) {
    608       1.41   thorpej 				const struct vm_page *pg;
    609       1.41   thorpej 
    610       1.41   thorpej 				size = MIN(remainbytes, PAGE_SIZE - offset);
    611       1.41   thorpej 
    612       1.41   thorpej 				pg = *pgs++;
    613       1.41   thorpej 				KASSERT(pg);
    614       1.41   thorpej 				paddr = VM_PAGE_TO_PHYS(pg) + offset;
    615       1.41   thorpej 
    616       1.41   thorpej 				error = _bus_dmamap_load_paddr(t, map,
    617  1.52.16.1      matt 				    paddr, size, false);
    618       1.41   thorpej 				if (error)
    619       1.28   thorpej 					break;
    620       1.41   thorpej 				offset = 0;
    621       1.41   thorpej 				remainbytes -= size;
    622       1.28   thorpej 			}
    623       1.28   thorpej 			break;
    624       1.28   thorpej 
    625       1.28   thorpej 		case 0:
    626       1.41   thorpej 			paddr = m->m_paddr + M_BUFOFFSET(m) +
    627       1.28   thorpej 			    (m->m_data - M_BUFADDR(m));
    628       1.41   thorpej 			size = m->m_len;
    629  1.52.16.1      matt 			error = _bus_dmamap_load_paddr(t, map, paddr, size,
    630  1.52.16.1      matt 			    false);
    631       1.41   thorpej 			break;
    632       1.28   thorpej 
    633       1.28   thorpej 		default:
    634       1.28   thorpej 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
    635       1.48      yamt 			    m->m_len, vmspace_kernel(), flags);
    636       1.28   thorpej 		}
    637        1.1     chris 	}
    638        1.1     chris 	if (error == 0) {
    639        1.1     chris 		map->dm_mapsize = m0->m_pkthdr.len;
    640       1.14   thorpej 		map->_dm_origbuf = m0;
    641  1.52.16.1      matt 		map->_dm_buftype = _BUS_DMA_BUFTYPE_MBUF;
    642       1.48      yamt 		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
    643  1.52.16.1      matt 		return 0;
    644        1.1     chris 	}
    645  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    646  1.52.16.1      matt 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
    647  1.52.16.1      matt 	if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
    648  1.52.16.1      matt 		error = _bus_dma_load_bouncebuf(t, map, m0, m0->m_pkthdr.len,
    649  1.52.16.1      matt 		    _BUS_DMA_BUFTYPE_MBUF, flags);
    650  1.52.16.1      matt 	}
    651  1.52.16.1      matt #endif
    652        1.1     chris 	return (error);
    653        1.1     chris }
    654        1.1     chris 
    655        1.1     chris /*
    656        1.1     chris  * Like _bus_dmamap_load(), but for uios.
    657        1.1     chris  */
    658        1.1     chris int
    659        1.7   thorpej _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    660        1.7   thorpej     int flags)
    661        1.1     chris {
    662       1.41   thorpej 	int i, error;
    663        1.1     chris 	bus_size_t minlen, resid;
    664        1.1     chris 	struct iovec *iov;
    665       1.50  christos 	void *addr;
    666        1.1     chris 
    667        1.1     chris 	/*
    668        1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    669        1.1     chris 	 */
    670        1.1     chris 	map->dm_mapsize = 0;
    671        1.1     chris 	map->dm_nsegs = 0;
    672  1.52.16.1      matt 	KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
    673  1.52.16.1      matt 	    "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
    674  1.52.16.1      matt 	    map->dm_maxsegsz, map->_dm_maxmaxsegsz);
    675        1.1     chris 
    676        1.1     chris 	resid = uio->uio_resid;
    677        1.1     chris 	iov = uio->uio_iov;
    678        1.1     chris 
    679       1.17   thorpej 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    680  1.52.16.1      matt 	map->_dm_flags |= _BUS_DMAMAP_COHERENT;
    681       1.17   thorpej 
    682        1.1     chris 	error = 0;
    683        1.1     chris 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    684        1.1     chris 		/*
    685        1.1     chris 		 * Now at the first iovec to load.  Load each iovec
    686        1.1     chris 		 * until we have exhausted the residual count.
    687        1.1     chris 		 */
    688        1.1     chris 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    689       1.50  christos 		addr = (void *)iov[i].iov_base;
    690        1.1     chris 
    691        1.1     chris 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
    692       1.48      yamt 		    uio->uio_vmspace, flags);
    693        1.1     chris 
    694        1.1     chris 		resid -= minlen;
    695        1.1     chris 	}
    696        1.1     chris 	if (error == 0) {
    697        1.1     chris 		map->dm_mapsize = uio->uio_resid;
    698       1.14   thorpej 		map->_dm_origbuf = uio;
    699  1.52.16.1      matt 		map->_dm_buftype = _BUS_DMA_BUFTYPE_UIO;
    700       1.48      yamt 		map->_dm_vmspace = uio->uio_vmspace;
    701        1.1     chris 	}
    702        1.1     chris 	return (error);
    703        1.1     chris }
    704        1.1     chris 
    705        1.1     chris /*
    706        1.1     chris  * Like _bus_dmamap_load(), but for raw memory allocated with
    707        1.1     chris  * bus_dmamem_alloc().
    708        1.1     chris  */
    709        1.1     chris int
    710        1.7   thorpej _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    711        1.7   thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    712        1.1     chris {
    713        1.1     chris 
    714        1.1     chris 	panic("_bus_dmamap_load_raw: not implemented");
    715        1.1     chris }
    716        1.1     chris 
    717        1.1     chris /*
    718        1.1     chris  * Common function for unloading a DMA map.  May be called by
    719        1.1     chris  * bus-specific DMA map unload functions.
    720        1.1     chris  */
    721        1.1     chris void
    722        1.7   thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    723        1.1     chris {
    724        1.1     chris 
    725        1.1     chris #ifdef DEBUG_DMA
    726        1.1     chris 	printf("dmamap_unload: t=%p map=%p\n", t, map);
    727        1.1     chris #endif	/* DEBUG_DMA */
    728        1.1     chris 
    729        1.1     chris 	/*
    730        1.1     chris 	 * No resources to free; just mark the mappings as
    731        1.1     chris 	 * invalid.
    732        1.1     chris 	 */
    733        1.1     chris 	map->dm_mapsize = 0;
    734        1.1     chris 	map->dm_nsegs = 0;
    735       1.14   thorpej 	map->_dm_origbuf = NULL;
    736  1.52.16.1      matt 	map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
    737       1.48      yamt 	map->_dm_vmspace = NULL;
    738        1.1     chris }
    739        1.1     chris 
    740  1.52.16.1      matt static void
    741  1.52.16.1      matt _bus_dmamap_sync_segment(vaddr_t va, paddr_t pa, vsize_t len, int ops, bool readonly_p)
    742       1.14   thorpej {
    743  1.52.16.1      matt 	KASSERT((va & PAGE_MASK) == (pa & PAGE_MASK));
    744  1.52.16.1      matt #if 0
    745  1.52.16.1      matt 	printf("sync_segment: va=%#lx pa=%#lx len=%#lx ops=%#x ro=%d\n",
    746  1.52.16.1      matt 	    va, pa, len, ops, readonly_p);
    747  1.52.16.1      matt #endif
    748       1.14   thorpej 
    749       1.14   thorpej 	switch (ops) {
    750       1.14   thorpej 	case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    751  1.52.16.1      matt 		if (!readonly_p) {
    752  1.52.16.1      matt 			STAT_INCR(sync_prereadwrite);
    753  1.52.16.1      matt 			cpu_dcache_wbinv_range(va, len);
    754  1.52.16.1      matt 			cpu_sdcache_wbinv_range(va, pa, len);
    755  1.52.16.1      matt 			break;
    756  1.52.16.1      matt 		}
    757  1.52.16.1      matt 		/* FALLTHROUGH */
    758       1.14   thorpej 
    759  1.52.16.1      matt 	case BUS_DMASYNC_PREREAD: {
    760  1.52.16.1      matt 		const size_t line_size = arm_dcache_align;
    761  1.52.16.1      matt 		const size_t line_mask = arm_dcache_align_mask;
    762  1.52.16.1      matt 		vsize_t misalignment = va & line_mask;
    763  1.52.16.1      matt 		if (misalignment) {
    764  1.52.16.1      matt 			va -= misalignment;
    765  1.52.16.1      matt 			pa -= misalignment;
    766  1.52.16.1      matt 			len += misalignment;
    767  1.52.16.1      matt 			STAT_INCR(sync_preread_begin);
    768  1.52.16.1      matt 			cpu_dcache_wbinv_range(va, line_size);
    769  1.52.16.1      matt 			cpu_sdcache_wbinv_range(va, pa, line_size);
    770  1.52.16.1      matt 			if (len <= line_size)
    771  1.52.16.1      matt 				break;
    772  1.52.16.1      matt 			va += line_size;
    773  1.52.16.1      matt 			pa += line_size;
    774  1.52.16.1      matt 			len -= line_size;
    775  1.52.16.1      matt 		}
    776  1.52.16.1      matt 		misalignment = len & line_mask;
    777  1.52.16.1      matt 		len -= misalignment;
    778  1.52.16.1      matt 		if (len > 0) {
    779  1.52.16.1      matt 			STAT_INCR(sync_preread);
    780  1.52.16.1      matt 			cpu_dcache_inv_range(va, len);
    781  1.52.16.1      matt 			cpu_sdcache_inv_range(va, pa, len);
    782  1.52.16.1      matt 		}
    783  1.52.16.1      matt 		if (misalignment) {
    784  1.52.16.1      matt 			va += len;
    785  1.52.16.1      matt 			pa += len;
    786  1.52.16.1      matt 			STAT_INCR(sync_preread_tail);
    787  1.52.16.1      matt 			cpu_dcache_wbinv_range(va, line_size);
    788  1.52.16.1      matt 			cpu_sdcache_wbinv_range(va, pa, line_size);
    789  1.52.16.1      matt 		}
    790       1.14   thorpej 		break;
    791  1.52.16.1      matt 	}
    792       1.14   thorpej 
    793       1.14   thorpej 	case BUS_DMASYNC_PREWRITE:
    794  1.52.16.1      matt 		STAT_INCR(sync_prewrite);
    795  1.52.16.1      matt 		cpu_dcache_wb_range(va, len);
    796  1.52.16.1      matt 		cpu_sdcache_wb_range(va, pa, len);
    797  1.52.16.1      matt 		break;
    798  1.52.16.1      matt 
    799  1.52.16.1      matt #ifdef CPU_CORTEX
    800  1.52.16.1      matt 	/*
    801  1.52.16.1      matt 	 * Cortex CPUs can do speculative loads so we need to clean the cache
    802  1.52.16.1      matt 	 * after a DMA read to deal with any speculatively loaded cache lines.
    803  1.52.16.1      matt 	 * Since these can't be dirty, we can just invalidate them and don't
    804  1.52.16.1      matt 	 * have to worry about having to write back their contents.
    805  1.52.16.1      matt 	 */
    806  1.52.16.1      matt 	case BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE:
    807  1.52.16.1      matt 		STAT_INCR(sync_postreadwrite);
    808  1.52.16.1      matt 		cpu_dcache_inv_range(va, len);
    809  1.52.16.1      matt 		cpu_sdcache_inv_range(va, pa, len);
    810       1.14   thorpej 		break;
    811  1.52.16.1      matt 	case BUS_DMASYNC_POSTREAD:
    812  1.52.16.1      matt 		STAT_INCR(sync_postread);
    813  1.52.16.1      matt 		cpu_dcache_inv_range(va, len);
    814  1.52.16.1      matt 		cpu_sdcache_inv_range(va, pa, len);
    815  1.52.16.1      matt 		break;
    816  1.52.16.1      matt #endif
    817       1.14   thorpej 	}
    818       1.14   thorpej }
    819       1.14   thorpej 
    820       1.47     perry static inline void
    821  1.52.16.1      matt _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    822       1.14   thorpej     bus_size_t len, int ops)
    823       1.14   thorpej {
    824  1.52.16.1      matt 	bus_dma_segment_t *ds = map->dm_segs;
    825  1.52.16.1      matt 	vaddr_t va = (vaddr_t) map->_dm_origbuf;
    826  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
    827  1.52.16.1      matt 	if (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING) {
    828  1.52.16.1      matt 		struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
    829  1.52.16.1      matt 		va = (vaddr_t) cookie->id_bouncebuf;
    830  1.52.16.1      matt 	}
    831  1.52.16.1      matt #endif
    832  1.52.16.1      matt 
    833  1.52.16.1      matt 	while (len > 0) {
    834  1.52.16.1      matt 		while (offset >= ds->ds_len) {
    835  1.52.16.1      matt 			offset -= ds->ds_len;
    836  1.52.16.1      matt 			va += ds->ds_len;
    837  1.52.16.1      matt 			ds++;
    838  1.52.16.1      matt 		}
    839  1.52.16.1      matt 
    840  1.52.16.1      matt 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + offset);
    841  1.52.16.1      matt 		size_t seglen = min(len, ds->ds_len - offset);
    842  1.52.16.1      matt 
    843  1.52.16.1      matt 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
    844  1.52.16.1      matt 			_bus_dmamap_sync_segment(va + offset, pa, seglen, ops,
    845  1.52.16.1      matt 			    false);
    846  1.52.16.1      matt 
    847  1.52.16.1      matt 		offset += seglen;
    848  1.52.16.1      matt 		len -= seglen;
    849  1.52.16.1      matt 	}
    850  1.52.16.1      matt }
    851  1.52.16.1      matt 
    852  1.52.16.1      matt static inline void
    853  1.52.16.1      matt _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t offset,
    854  1.52.16.1      matt     bus_size_t len, int ops)
    855  1.52.16.1      matt {
    856  1.52.16.1      matt 	bus_dma_segment_t *ds = map->dm_segs;
    857  1.52.16.1      matt 	struct mbuf *m = map->_dm_origbuf;
    858  1.52.16.1      matt 	bus_size_t voff = offset;
    859  1.52.16.1      matt 	bus_size_t ds_off = offset;
    860  1.52.16.1      matt 
    861  1.52.16.1      matt 	while (len > 0) {
    862  1.52.16.1      matt 		/* Find the current dma segment */
    863  1.52.16.1      matt 		while (ds_off >= ds->ds_len) {
    864  1.52.16.1      matt 			ds_off -= ds->ds_len;
    865  1.52.16.1      matt 			ds++;
    866  1.52.16.1      matt 		}
    867  1.52.16.1      matt 		/* Find the current mbuf. */
    868  1.52.16.1      matt 		while (voff >= m->m_len) {
    869  1.52.16.1      matt 			voff -= m->m_len;
    870  1.52.16.1      matt 			m = m->m_next;
    871       1.14   thorpej 		}
    872       1.14   thorpej 
    873       1.14   thorpej 		/*
    874       1.14   thorpej 		 * Now at the first mbuf to sync; nail each one until
    875       1.14   thorpej 		 * we have exhausted the length.
    876       1.14   thorpej 		 */
    877  1.52.16.1      matt 		vsize_t seglen = min(len, min(m->m_len - voff, ds->ds_len - ds_off));
    878  1.52.16.1      matt 		vaddr_t va = mtod(m, vaddr_t) + voff;
    879  1.52.16.1      matt 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
    880       1.14   thorpej 
    881       1.28   thorpej 		/*
    882       1.28   thorpej 		 * We can save a lot of work here if we know the mapping
    883       1.28   thorpej 		 * is read-only at the MMU:
    884       1.28   thorpej 		 *
    885       1.28   thorpej 		 * If a mapping is read-only, no dirty cache blocks will
    886       1.28   thorpej 		 * exist for it.  If a writable mapping was made read-only,
    887       1.28   thorpej 		 * we know any dirty cache lines for the range will have
    888       1.28   thorpej 		 * been cleaned for us already.  Therefore, if the upper
    889       1.28   thorpej 		 * layer can tell us we have a read-only mapping, we can
    890       1.28   thorpej 		 * skip all cache cleaning.
    891       1.28   thorpej 		 *
    892       1.28   thorpej 		 * NOTE: This only works if we know the pmap cleans pages
    893       1.28   thorpej 		 * before making a read-write -> read-only transition.  If
    894       1.28   thorpej 		 * this ever becomes non-true (e.g. Physically Indexed
    895       1.28   thorpej 		 * cache), this will have to be revisited.
    896       1.28   thorpej 		 */
    897       1.14   thorpej 
    898  1.52.16.1      matt 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
    899  1.52.16.1      matt 			_bus_dmamap_sync_segment(va, pa, seglen, ops,
    900  1.52.16.1      matt 			    M_ROMAP(m));
    901  1.52.16.1      matt 		voff += seglen;
    902  1.52.16.1      matt 		ds_off += seglen;
    903  1.52.16.1      matt 		len -= seglen;
    904       1.14   thorpej 	}
    905       1.14   thorpej }
    906       1.14   thorpej 
    907       1.47     perry static inline void
    908       1.14   thorpej _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    909       1.14   thorpej     bus_size_t len, int ops)
    910       1.14   thorpej {
    911  1.52.16.1      matt 	bus_dma_segment_t *ds = map->dm_segs;
    912       1.14   thorpej 	struct uio *uio = map->_dm_origbuf;
    913  1.52.16.1      matt 	struct iovec *iov = uio->uio_iov;
    914  1.52.16.1      matt 	bus_size_t voff = offset;
    915  1.52.16.1      matt 	bus_size_t ds_off = offset;
    916  1.52.16.1      matt 
    917  1.52.16.1      matt 	while (len > 0) {
    918  1.52.16.1      matt 		/* Find the current dma segment */
    919  1.52.16.1      matt 		while (ds_off >= ds->ds_len) {
    920  1.52.16.1      matt 			ds_off -= ds->ds_len;
    921  1.52.16.1      matt 			ds++;
    922  1.52.16.1      matt 		}
    923       1.14   thorpej 
    924  1.52.16.1      matt 		/* Find the current iovec. */
    925  1.52.16.1      matt 		while (voff >= iov->iov_len) {
    926  1.52.16.1      matt 			voff -= iov->iov_len;
    927  1.52.16.1      matt 			iov++;
    928       1.14   thorpej 		}
    929       1.14   thorpej 
    930       1.14   thorpej 		/*
    931       1.14   thorpej 		 * Now at the first iovec to sync; nail each one until
    932       1.14   thorpej 		 * we have exhausted the length.
    933       1.14   thorpej 		 */
    934  1.52.16.1      matt 		vsize_t seglen = min(len, min(iov->iov_len - voff, ds->ds_len - ds_off));
    935  1.52.16.1      matt 		vaddr_t va = (vaddr_t) iov->iov_base + voff;
    936  1.52.16.1      matt 		paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
    937  1.52.16.1      matt 
    938  1.52.16.1      matt 		if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
    939  1.52.16.1      matt 			_bus_dmamap_sync_segment(va, pa, seglen, ops, false);
    940  1.52.16.1      matt 
    941  1.52.16.1      matt 		voff += seglen;
    942  1.52.16.1      matt 		ds_off += seglen;
    943  1.52.16.1      matt 		len -= seglen;
    944       1.14   thorpej 	}
    945       1.14   thorpej }
    946       1.14   thorpej 
    947        1.1     chris /*
    948        1.1     chris  * Common function for DMA map synchronization.  May be called
    949        1.1     chris  * by bus-specific DMA map synchronization functions.
    950        1.8   thorpej  *
    951        1.8   thorpej  * This version works for the Virtually Indexed Virtually Tagged
    952        1.8   thorpej  * cache found on 32-bit ARM processors.
    953        1.8   thorpej  *
    954        1.8   thorpej  * XXX Should have separate versions for write-through vs.
    955        1.8   thorpej  * XXX write-back caches.  We currently assume write-back
    956        1.8   thorpej  * XXX here, which is not as efficient as it could be for
    957        1.8   thorpej  * XXX the write-through case.
    958        1.1     chris  */
    959        1.1     chris void
    960        1.7   thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    961        1.7   thorpej     bus_size_t len, int ops)
    962        1.1     chris {
    963        1.1     chris #ifdef DEBUG_DMA
    964        1.1     chris 	printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
    965        1.1     chris 	    t, map, offset, len, ops);
    966        1.1     chris #endif	/* DEBUG_DMA */
    967        1.1     chris 
    968        1.8   thorpej 	/*
    969        1.8   thorpej 	 * Mixing of PRE and POST operations is not allowed.
    970        1.8   thorpej 	 */
    971        1.8   thorpej 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    972        1.8   thorpej 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    973        1.8   thorpej 		panic("_bus_dmamap_sync: mix PRE and POST");
    974        1.8   thorpej 
    975  1.52.16.1      matt 	KASSERTMSG(offset < map->dm_mapsize,
    976  1.52.16.1      matt 	    "offset %lu mapsize %lu",
    977  1.52.16.1      matt 	    offset, map->dm_mapsize);
    978  1.52.16.1      matt 	KASSERTMSG(len > 0 && offset + len <= map->dm_mapsize,
    979  1.52.16.1      matt 	    "len %lu offset %lu mapsize %lu",
    980  1.52.16.1      matt 	    len, offset, map->dm_mapsize);
    981        1.8   thorpej 
    982        1.8   thorpej 	/*
    983        1.8   thorpej 	 * For a virtually-indexed write-back cache, we need
    984        1.8   thorpej 	 * to do the following things:
    985        1.8   thorpej 	 *
    986        1.8   thorpej 	 *	PREREAD -- Invalidate the D-cache.  We do this
    987        1.8   thorpej 	 *	here in case a write-back is required by the back-end.
    988        1.8   thorpej 	 *
    989        1.8   thorpej 	 *	PREWRITE -- Write-back the D-cache.  Note that if
    990        1.8   thorpej 	 *	we are doing a PREREAD|PREWRITE, we can collapse
    991        1.8   thorpej 	 *	the whole thing into a single Wb-Inv.
    992        1.8   thorpej 	 *
    993  1.52.16.1      matt 	 *	POSTREAD -- Re-invalidate the D-cache in case speculative
    994  1.52.16.1      matt 	 *	memory accesses caused cachelines to become valid with now
    995  1.52.16.1      matt 	 *	invalid data.
    996        1.8   thorpej 	 *
    997        1.8   thorpej 	 *	POSTWRITE -- Nothing.
    998        1.8   thorpej 	 */
    999  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1000  1.52.16.1      matt 	const bool bouncing = (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING);
   1001  1.52.16.1      matt #else
   1002  1.52.16.1      matt 	const bool bouncing = false;
   1003  1.52.16.1      matt #endif
   1004        1.8   thorpej 
   1005  1.52.16.1      matt 	const int pre_ops = ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1006  1.52.16.1      matt #ifdef CPU_CORTEX
   1007  1.52.16.1      matt 	const int post_ops = ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1008  1.52.16.1      matt #else
   1009  1.52.16.1      matt 	const int post_ops = 0;
   1010  1.52.16.1      matt #endif
   1011  1.52.16.1      matt 	if (!bouncing && pre_ops == 0 && post_ops == BUS_DMASYNC_POSTWRITE) {
   1012  1.52.16.1      matt 		STAT_INCR(sync_postwrite);
   1013        1.8   thorpej 		return;
   1014  1.52.16.1      matt 	}
   1015  1.52.16.1      matt 	KASSERTMSG(bouncing || pre_ops != 0 || (post_ops & BUS_DMASYNC_POSTREAD),
   1016  1.52.16.1      matt 	    "pre_ops %#x post_ops %#x", pre_ops, post_ops);
   1017  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1018  1.52.16.1      matt 	if (bouncing && (ops & BUS_DMASYNC_PREWRITE)) {
   1019  1.52.16.1      matt 		struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
   1020  1.52.16.1      matt 		STAT_INCR(write_bounces);
   1021  1.52.16.1      matt 		char * const dataptr = (char *)cookie->id_bouncebuf + offset;
   1022  1.52.16.1      matt 		/*
   1023  1.52.16.1      matt 		 * Copy the caller's buffer to the bounce buffer.
   1024  1.52.16.1      matt 		 */
   1025  1.52.16.1      matt 		switch (map->_dm_buftype) {
   1026  1.52.16.1      matt 		case _BUS_DMA_BUFTYPE_LINEAR:
   1027  1.52.16.1      matt 			memcpy(dataptr, cookie->id_origlinearbuf + offset, len);
   1028  1.52.16.1      matt 			break;
   1029  1.52.16.1      matt 		case _BUS_DMA_BUFTYPE_MBUF:
   1030  1.52.16.1      matt 			m_copydata(cookie->id_origmbuf, offset, len, dataptr);
   1031  1.52.16.1      matt 			break;
   1032  1.52.16.1      matt 		case _BUS_DMA_BUFTYPE_UIO:
   1033  1.52.16.1      matt 			_bus_dma_uiomove(dataptr, cookie->id_origuio, len, UIO_WRITE);
   1034  1.52.16.1      matt 			break;
   1035  1.52.16.1      matt #ifdef DIAGNOSTIC
   1036  1.52.16.1      matt 		case _BUS_DMA_BUFTYPE_RAW:
   1037  1.52.16.1      matt 			panic("_bus_dmamap_sync(pre): _BUS_DMA_BUFTYPE_RAW");
   1038  1.52.16.1      matt 			break;
   1039  1.52.16.1      matt 
   1040  1.52.16.1      matt 		case _BUS_DMA_BUFTYPE_INVALID:
   1041  1.52.16.1      matt 			panic("_bus_dmamap_sync(pre): _BUS_DMA_BUFTYPE_INVALID");
   1042  1.52.16.1      matt 			break;
   1043  1.52.16.1      matt 
   1044  1.52.16.1      matt 		default:
   1045  1.52.16.1      matt 			panic("_bus_dmamap_sync(pre): map %p: unknown buffer type %d\n",
   1046  1.52.16.1      matt 			    map, map->_dm_buftype);
   1047  1.52.16.1      matt 			break;
   1048  1.52.16.1      matt #endif /* DIAGNOSTIC */
   1049  1.52.16.1      matt 		}
   1050  1.52.16.1      matt 	}
   1051  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
   1052        1.8   thorpej 
   1053       1.17   thorpej 	/* Skip cache frobbing if mapping was COHERENT. */
   1054  1.52.16.1      matt 	if (!bouncing && (map->_dm_flags & _BUS_DMAMAP_COHERENT)) {
   1055       1.17   thorpej 		/* Drain the write buffer. */
   1056  1.52.16.1      matt 		if (pre_ops & BUS_DMASYNC_PREWRITE)
   1057  1.52.16.1      matt 			cpu_drain_writebuf();
   1058       1.17   thorpej 		return;
   1059       1.17   thorpej 	}
   1060        1.8   thorpej 
   1061  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1062  1.52.16.1      matt 	if (bouncing && ((map->_dm_flags & _BUS_DMAMAP_COHERENT) || pre_ops == 0)) {
   1063  1.52.16.1      matt 		goto bounce_it;
   1064  1.52.16.1      matt 	}
   1065  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
   1066  1.52.16.1      matt 
   1067  1.52.16.1      matt #ifndef ARM_MMU_EXTENDED
   1068        1.8   thorpej 	/*
   1069       1.38       scw 	 * If the mapping belongs to a non-kernel vmspace, and the
   1070       1.38       scw 	 * vmspace has not been active since the last time a full
   1071       1.38       scw 	 * cache flush was performed, we don't need to do anything.
   1072        1.8   thorpej 	 */
   1073       1.48      yamt 	if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
   1074       1.48      yamt 	    vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
   1075        1.8   thorpej 		return;
   1076  1.52.16.1      matt #endif
   1077        1.8   thorpej 
   1078  1.52.16.1      matt 	int buftype = map->_dm_buftype;
   1079  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1080  1.52.16.1      matt 	if (bouncing) {
   1081  1.52.16.1      matt 		buftype = _BUS_DMA_BUFTYPE_LINEAR;
   1082  1.52.16.1      matt 	}
   1083  1.52.16.1      matt #endif
   1084  1.52.16.1      matt 
   1085  1.52.16.1      matt 	switch (buftype) {
   1086  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_LINEAR:
   1087       1.14   thorpej 		_bus_dmamap_sync_linear(t, map, offset, len, ops);
   1088       1.14   thorpej 		break;
   1089       1.14   thorpej 
   1090  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_MBUF:
   1091       1.14   thorpej 		_bus_dmamap_sync_mbuf(t, map, offset, len, ops);
   1092       1.14   thorpej 		break;
   1093       1.14   thorpej 
   1094  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_UIO:
   1095       1.14   thorpej 		_bus_dmamap_sync_uio(t, map, offset, len, ops);
   1096       1.14   thorpej 		break;
   1097       1.14   thorpej 
   1098  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_RAW:
   1099  1.52.16.1      matt 		panic("_bus_dmamap_sync: _BUS_DMA_BUFTYPE_RAW");
   1100       1.14   thorpej 		break;
   1101       1.14   thorpej 
   1102  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_INVALID:
   1103  1.52.16.1      matt 		panic("_bus_dmamap_sync: _BUS_DMA_BUFTYPE_INVALID");
   1104       1.14   thorpej 		break;
   1105       1.14   thorpej 
   1106       1.14   thorpej 	default:
   1107  1.52.16.1      matt 		panic("_bus_dmamap_sync: map %p: unknown buffer type %d\n",
   1108  1.52.16.1      matt 		    map, map->_dm_buftype);
   1109        1.8   thorpej 	}
   1110        1.1     chris 
   1111        1.8   thorpej 	/* Drain the write buffer. */
   1112        1.8   thorpej 	cpu_drain_writebuf();
   1113  1.52.16.1      matt 
   1114  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1115  1.52.16.1      matt   bounce_it:
   1116  1.52.16.1      matt 	if (!bouncing || (ops & BUS_DMASYNC_POSTREAD) == 0)
   1117  1.52.16.1      matt 		return;
   1118  1.52.16.1      matt 
   1119  1.52.16.1      matt 	struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
   1120  1.52.16.1      matt 	char * const dataptr = (char *)cookie->id_bouncebuf + offset;
   1121  1.52.16.1      matt 	STAT_INCR(read_bounces);
   1122  1.52.16.1      matt 	/*
   1123  1.52.16.1      matt 	 * Copy the bounce buffer to the caller's buffer.
   1124  1.52.16.1      matt 	 */
   1125  1.52.16.1      matt 	switch (map->_dm_buftype) {
   1126  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_LINEAR:
   1127  1.52.16.1      matt 		memcpy(cookie->id_origlinearbuf + offset, dataptr, len);
   1128  1.52.16.1      matt 		break;
   1129  1.52.16.1      matt 
   1130  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_MBUF:
   1131  1.52.16.1      matt 		m_copyback(cookie->id_origmbuf, offset, len, dataptr);
   1132  1.52.16.1      matt 		break;
   1133  1.52.16.1      matt 
   1134  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_UIO:
   1135  1.52.16.1      matt 		_bus_dma_uiomove(dataptr, cookie->id_origuio, len, UIO_READ);
   1136  1.52.16.1      matt 		break;
   1137  1.52.16.1      matt #ifdef DIAGNOSTIC
   1138  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_RAW:
   1139  1.52.16.1      matt 		panic("_bus_dmamap_sync(post): _BUS_DMA_BUFTYPE_RAW");
   1140  1.52.16.1      matt 		break;
   1141  1.52.16.1      matt 
   1142  1.52.16.1      matt 	case _BUS_DMA_BUFTYPE_INVALID:
   1143  1.52.16.1      matt 		panic("_bus_dmamap_sync(post): _BUS_DMA_BUFTYPE_INVALID");
   1144  1.52.16.1      matt 		break;
   1145  1.52.16.1      matt 
   1146  1.52.16.1      matt 	default:
   1147  1.52.16.1      matt 		panic("_bus_dmamap_sync(post): map %p: unknown buffer type %d\n",
   1148  1.52.16.1      matt 		    map, map->_dm_buftype);
   1149  1.52.16.1      matt 		break;
   1150  1.52.16.1      matt #endif
   1151  1.52.16.1      matt 	}
   1152  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
   1153        1.1     chris }
   1154        1.1     chris 
   1155        1.1     chris /*
   1156        1.1     chris  * Common function for DMA-safe memory allocation.  May be called
   1157        1.1     chris  * by bus-specific DMA memory allocation functions.
   1158        1.1     chris  */
   1159        1.1     chris 
   1160       1.11   thorpej extern paddr_t physical_start;
   1161       1.11   thorpej extern paddr_t physical_end;
   1162        1.1     chris 
   1163        1.1     chris int
   1164        1.7   thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1165        1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1166        1.7   thorpej     int flags)
   1167        1.1     chris {
   1168       1.15   thorpej 	struct arm32_dma_range *dr;
   1169       1.37   mycroft 	int error, i;
   1170       1.15   thorpej 
   1171        1.1     chris #ifdef DEBUG_DMA
   1172       1.15   thorpej 	printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
   1173       1.15   thorpej 	    "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
   1174       1.15   thorpej 	    boundary, segs, nsegs, rsegs, flags);
   1175       1.15   thorpej #endif
   1176       1.15   thorpej 
   1177       1.15   thorpej 	if ((dr = t->_ranges) != NULL) {
   1178       1.37   mycroft 		error = ENOMEM;
   1179       1.15   thorpej 		for (i = 0; i < t->_nranges; i++, dr++) {
   1180  1.52.16.1      matt 			if (dr->dr_len == 0
   1181  1.52.16.1      matt 			    || (dr->dr_flags & _BUS_DMAMAP_NOALLOC))
   1182       1.15   thorpej 				continue;
   1183       1.15   thorpej 			error = _bus_dmamem_alloc_range(t, size, alignment,
   1184       1.15   thorpej 			    boundary, segs, nsegs, rsegs, flags,
   1185       1.15   thorpej 			    trunc_page(dr->dr_sysbase),
   1186       1.15   thorpej 			    trunc_page(dr->dr_sysbase + dr->dr_len));
   1187       1.15   thorpej 			if (error == 0)
   1188       1.15   thorpej 				break;
   1189       1.15   thorpej 		}
   1190       1.15   thorpej 	} else {
   1191       1.15   thorpej 		error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
   1192       1.15   thorpej 		    segs, nsegs, rsegs, flags, trunc_page(physical_start),
   1193       1.15   thorpej 		    trunc_page(physical_end));
   1194       1.15   thorpej 	}
   1195       1.15   thorpej 
   1196        1.1     chris #ifdef DEBUG_DMA
   1197        1.1     chris 	printf("dmamem_alloc: =%d\n", error);
   1198       1.15   thorpej #endif
   1199       1.15   thorpej 
   1200        1.1     chris 	return(error);
   1201        1.1     chris }
   1202        1.1     chris 
   1203        1.1     chris /*
   1204        1.1     chris  * Common function for freeing DMA-safe memory.  May be called by
   1205        1.1     chris  * bus-specific DMA memory free functions.
   1206        1.1     chris  */
   1207        1.1     chris void
   1208        1.7   thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
   1209        1.1     chris {
   1210        1.1     chris 	struct vm_page *m;
   1211        1.1     chris 	bus_addr_t addr;
   1212        1.1     chris 	struct pglist mlist;
   1213        1.1     chris 	int curseg;
   1214        1.1     chris 
   1215        1.1     chris #ifdef DEBUG_DMA
   1216        1.1     chris 	printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
   1217        1.1     chris #endif	/* DEBUG_DMA */
   1218        1.1     chris 
   1219        1.1     chris 	/*
   1220        1.1     chris 	 * Build a list of pages to free back to the VM system.
   1221        1.1     chris 	 */
   1222        1.1     chris 	TAILQ_INIT(&mlist);
   1223        1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
   1224        1.1     chris 		for (addr = segs[curseg].ds_addr;
   1225        1.1     chris 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
   1226        1.1     chris 		    addr += PAGE_SIZE) {
   1227        1.1     chris 			m = PHYS_TO_VM_PAGE(addr);
   1228       1.52        ad 			TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
   1229        1.1     chris 		}
   1230        1.1     chris 	}
   1231        1.1     chris 	uvm_pglistfree(&mlist);
   1232        1.1     chris }
   1233        1.1     chris 
   1234        1.1     chris /*
   1235        1.1     chris  * Common function for mapping DMA-safe memory.  May be called by
   1236        1.1     chris  * bus-specific DMA memory map functions.
   1237        1.1     chris  */
   1238        1.1     chris int
   1239        1.7   thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
   1240       1.50  christos     size_t size, void **kvap, int flags)
   1241        1.1     chris {
   1242       1.11   thorpej 	vaddr_t va;
   1243  1.52.16.1      matt 	paddr_t pa;
   1244        1.1     chris 	int curseg;
   1245  1.52.16.1      matt 	pt_entry_t *ptep;
   1246  1.52.16.1      matt 	const uvm_flag_t kmflags = UVM_KMF_VAONLY
   1247  1.52.16.1      matt 	    | ((flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0);
   1248  1.52.16.1      matt 	vsize_t align = 0;
   1249        1.1     chris 
   1250        1.1     chris #ifdef DEBUG_DMA
   1251        1.3  rearnsha 	printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
   1252        1.3  rearnsha 	    segs, nsegs, (unsigned long)size, flags);
   1253        1.1     chris #endif	/* DEBUG_DMA */
   1254        1.1     chris 
   1255  1.52.16.1      matt #ifdef PMAP_MAP_POOLPAGE
   1256  1.52.16.1      matt 	/*
   1257  1.52.16.1      matt 	 * If all of memory is mapped, and we are mapping a single physically
   1258  1.52.16.1      matt 	 * contiguous area then this area is already mapped.  Let's see if we
   1259  1.52.16.1      matt 	 * avoid having a separate mapping for it.
   1260  1.52.16.1      matt 	 */
   1261  1.52.16.1      matt 	if (nsegs == 1) {
   1262  1.52.16.1      matt 		/*
   1263  1.52.16.1      matt 		 * If this is a non-COHERENT mapping, then the existing kernel
   1264  1.52.16.1      matt 		 * mapping is already compatible with it.
   1265  1.52.16.1      matt 		 */
   1266  1.52.16.1      matt 		bool direct_mapable = (flags & BUS_DMA_COHERENT) == 0;
   1267  1.52.16.1      matt 		pa = segs[0].ds_addr;
   1268  1.52.16.1      matt 
   1269  1.52.16.1      matt 		/*
   1270  1.52.16.1      matt 		 * This is a COHERENT mapping which, unless this address is in
   1271  1.52.16.1      matt 		 * a COHERENT dma range, will not be compatible.
   1272  1.52.16.1      matt 		 */
   1273  1.52.16.1      matt 		if (t->_ranges != NULL) {
   1274  1.52.16.1      matt 			const struct arm32_dma_range * const dr =
   1275  1.52.16.1      matt 			    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
   1276  1.52.16.1      matt 			if (dr != NULL
   1277  1.52.16.1      matt 			    && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
   1278  1.52.16.1      matt 				direct_mapable = true;
   1279  1.52.16.1      matt 			}
   1280  1.52.16.1      matt 		}
   1281  1.52.16.1      matt 
   1282  1.52.16.1      matt 		if (direct_mapable) {
   1283  1.52.16.1      matt 			*kvap = (void *)PMAP_MAP_POOLPAGE(pa);
   1284  1.52.16.1      matt #ifdef DEBUG_DMA
   1285  1.52.16.1      matt 			printf("dmamem_map: =%p\n", *kvap);
   1286  1.52.16.1      matt #endif	/* DEBUG_DMA */
   1287  1.52.16.1      matt 			return 0;
   1288  1.52.16.1      matt 		}
   1289  1.52.16.1      matt 	}
   1290  1.52.16.1      matt #endif
   1291  1.52.16.1      matt 
   1292        1.1     chris 	size = round_page(size);
   1293  1.52.16.1      matt 	if (__predict_true(size > L2_L_SIZE)) {
   1294  1.52.16.1      matt #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   1295  1.52.16.1      matt 		if (size >= L1_SS_SIZE)
   1296  1.52.16.1      matt 			align = L1_SS_SIZE;
   1297  1.52.16.1      matt 		else
   1298  1.52.16.1      matt #endif
   1299  1.52.16.1      matt 		if (size >= L1_S_SIZE)
   1300  1.52.16.1      matt 			align = L1_S_SIZE;
   1301  1.52.16.1      matt 		else
   1302  1.52.16.1      matt 			align = L2_S_SIZE;
   1303  1.52.16.1      matt 	}
   1304  1.52.16.1      matt 
   1305  1.52.16.1      matt 	va = uvm_km_alloc(kernel_map, size, align, kmflags);
   1306  1.52.16.1      matt 	if (__predict_false(va == 0 && align > 0)) {
   1307  1.52.16.1      matt 		align = 0;
   1308  1.52.16.1      matt 		va = uvm_km_alloc(kernel_map, size, 0, kmflags);
   1309  1.52.16.1      matt 	}
   1310        1.1     chris 
   1311        1.1     chris 	if (va == 0)
   1312        1.1     chris 		return (ENOMEM);
   1313        1.1     chris 
   1314       1.50  christos 	*kvap = (void *)va;
   1315        1.1     chris 
   1316        1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
   1317  1.52.16.1      matt 		for (pa = segs[curseg].ds_addr;
   1318  1.52.16.1      matt 		    pa < (segs[curseg].ds_addr + segs[curseg].ds_len);
   1319  1.52.16.1      matt 		    pa += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
   1320  1.52.16.1      matt 			bool uncached = (flags & BUS_DMA_COHERENT);
   1321        1.1     chris #ifdef DEBUG_DMA
   1322  1.52.16.1      matt 			printf("wiring p%lx to v%lx", pa, va);
   1323        1.1     chris #endif	/* DEBUG_DMA */
   1324        1.1     chris 			if (size == 0)
   1325        1.1     chris 				panic("_bus_dmamem_map: size botch");
   1326  1.52.16.1      matt 
   1327  1.52.16.1      matt 			const struct arm32_dma_range * const dr =
   1328  1.52.16.1      matt 			    _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
   1329  1.52.16.1      matt 			/*
   1330  1.52.16.1      matt 			 * If this dma region is coherent then there is
   1331  1.52.16.1      matt 			 * no need for an uncached mapping.
   1332  1.52.16.1      matt 			 */
   1333  1.52.16.1      matt 			if (dr != NULL
   1334  1.52.16.1      matt 			    && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
   1335  1.52.16.1      matt 				uncached = false;
   1336  1.52.16.1      matt 			}
   1337  1.52.16.1      matt 
   1338  1.52.16.1      matt 			pmap_kenter_pa(va, pa,
   1339        1.1     chris 			    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
   1340  1.52.16.1      matt 
   1341        1.1     chris 			/*
   1342        1.1     chris 			 * If the memory must remain coherent with the
   1343        1.1     chris 			 * cache then we must make the memory uncacheable
   1344        1.1     chris 			 * in order to maintain virtual cache coherency.
   1345       1.24       wiz 			 * We must also guarantee the cache does not already
   1346        1.1     chris 			 * contain the virtal addresses we are making
   1347        1.1     chris 			 * uncacheable.
   1348        1.1     chris 			 */
   1349  1.52.16.1      matt 			if (uncached) {
   1350       1.27   thorpej 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   1351  1.52.16.1      matt 				cpu_sdcache_wbinv_range(va, pa, PAGE_SIZE);
   1352        1.1     chris 				cpu_drain_writebuf();
   1353        1.1     chris 				ptep = vtopte(va);
   1354       1.17   thorpej 				*ptep &= ~L2_S_CACHE_MASK;
   1355       1.21   thorpej 				PTE_SYNC(ptep);
   1356        1.1     chris 				tlb_flush();
   1357        1.1     chris 			}
   1358        1.1     chris #ifdef DEBUG_DMA
   1359        1.1     chris 			ptep = vtopte(va);
   1360        1.1     chris 			printf(" pte=v%p *pte=%x\n", ptep, *ptep);
   1361        1.1     chris #endif	/* DEBUG_DMA */
   1362        1.1     chris 		}
   1363        1.1     chris 	}
   1364        1.2     chris 	pmap_update(pmap_kernel());
   1365        1.1     chris #ifdef DEBUG_DMA
   1366        1.1     chris 	printf("dmamem_map: =%p\n", *kvap);
   1367        1.1     chris #endif	/* DEBUG_DMA */
   1368        1.1     chris 	return (0);
   1369        1.1     chris }
   1370        1.1     chris 
   1371        1.1     chris /*
   1372        1.1     chris  * Common function for unmapping DMA-safe memory.  May be called by
   1373        1.1     chris  * bus-specific DMA memory unmapping functions.
   1374        1.1     chris  */
   1375        1.1     chris void
   1376       1.50  christos _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
   1377        1.1     chris {
   1378        1.1     chris 
   1379        1.1     chris #ifdef DEBUG_DMA
   1380  1.52.16.1      matt 	printf("dmamem_unmap: t=%p kva=%p size=%zx\n", t, kva, size);
   1381        1.1     chris #endif	/* DEBUG_DMA */
   1382  1.52.16.1      matt 	KASSERTMSG(((uintptr_t)kva & PAGE_MASK) == 0,
   1383  1.52.16.1      matt 	    "kva %p (%#"PRIxPTR")", kva, (uintptr_t)kva & PAGE_MASK);
   1384        1.1     chris 
   1385        1.1     chris 	size = round_page(size);
   1386  1.52.16.1      matt 	pmap_kremove((vaddr_t)kva, size);
   1387       1.44      yamt 	pmap_update(pmap_kernel());
   1388       1.44      yamt 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
   1389        1.1     chris }
   1390        1.1     chris 
   1391        1.1     chris /*
   1392        1.1     chris  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
   1393        1.1     chris  * bus-specific DMA mmap(2)'ing functions.
   1394        1.1     chris  */
   1395        1.1     chris paddr_t
   1396        1.7   thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
   1397        1.7   thorpej     off_t off, int prot, int flags)
   1398        1.1     chris {
   1399  1.52.16.1      matt 	paddr_t map_flags;
   1400        1.1     chris 	int i;
   1401        1.1     chris 
   1402        1.1     chris 	for (i = 0; i < nsegs; i++) {
   1403  1.52.16.1      matt 		KASSERTMSG((off & PAGE_MASK) == 0,
   1404  1.52.16.1      matt 		    "off %#qx (%#x)", off, (int)off & PAGE_MASK);
   1405  1.52.16.1      matt 		KASSERTMSG((segs[i].ds_addr & PAGE_MASK) == 0,
   1406  1.52.16.1      matt 		    "ds_addr %#lx (%#x)", segs[i].ds_addr,
   1407  1.52.16.1      matt 		    (int)segs[i].ds_addr & PAGE_MASK);
   1408  1.52.16.1      matt 		KASSERTMSG((segs[i].ds_len & PAGE_MASK) == 0,
   1409  1.52.16.1      matt 		    "ds_len %#lx (%#x)", segs[i].ds_addr,
   1410  1.52.16.1      matt 		    (int)segs[i].ds_addr & PAGE_MASK);
   1411        1.1     chris 		if (off >= segs[i].ds_len) {
   1412        1.1     chris 			off -= segs[i].ds_len;
   1413        1.1     chris 			continue;
   1414        1.1     chris 		}
   1415        1.1     chris 
   1416  1.52.16.1      matt 		map_flags = 0;
   1417  1.52.16.1      matt 		if (flags & BUS_DMA_PREFETCHABLE)
   1418  1.52.16.1      matt 			map_flags |= ARM32_MMAP_WRITECOMBINE;
   1419  1.52.16.1      matt 
   1420  1.52.16.1      matt 		return (arm_btop((u_long)segs[i].ds_addr + off) | map_flags);
   1421  1.52.16.1      matt 
   1422        1.1     chris 	}
   1423        1.1     chris 
   1424        1.1     chris 	/* Page not found. */
   1425        1.1     chris 	return (-1);
   1426        1.1     chris }
   1427        1.1     chris 
   1428        1.1     chris /**********************************************************************
   1429        1.1     chris  * DMA utility functions
   1430        1.1     chris  **********************************************************************/
   1431        1.1     chris 
   1432        1.1     chris /*
   1433        1.1     chris  * Utility function to load a linear buffer.  lastaddrp holds state
   1434        1.1     chris  * between invocations (for multiple-buffer loads).  segp contains
   1435        1.1     chris  * the starting segment on entrace, and the ending segment on exit.
   1436        1.1     chris  * first indicates if this is the first invocation of this function.
   1437        1.1     chris  */
   1438        1.1     chris int
   1439        1.7   thorpej _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
   1440       1.48      yamt     bus_size_t buflen, struct vmspace *vm, int flags)
   1441        1.1     chris {
   1442        1.1     chris 	bus_size_t sgsize;
   1443       1.41   thorpej 	bus_addr_t curaddr;
   1444       1.11   thorpej 	vaddr_t vaddr = (vaddr_t)buf;
   1445       1.41   thorpej 	int error;
   1446        1.1     chris 	pmap_t pmap;
   1447        1.1     chris 
   1448        1.1     chris #ifdef DEBUG_DMA
   1449       1.40       scw 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
   1450       1.40       scw 	    buf, buflen, flags);
   1451        1.1     chris #endif	/* DEBUG_DMA */
   1452        1.1     chris 
   1453       1.48      yamt 	pmap = vm_map_pmap(&vm->vm_map);
   1454        1.1     chris 
   1455       1.41   thorpej 	while (buflen > 0) {
   1456        1.1     chris 		/*
   1457        1.1     chris 		 * Get the physical address for this segment.
   1458       1.17   thorpej 		 *
   1459  1.52.16.1      matt 		 * XXX Doesn't support checking for coherent mappings
   1460       1.17   thorpej 		 * XXX in user address space.
   1461        1.1     chris 		 */
   1462  1.52.16.1      matt 		bool coherent;
   1463       1.17   thorpej 		if (__predict_true(pmap == pmap_kernel())) {
   1464  1.52.16.1      matt 			pd_entry_t *pde;
   1465  1.52.16.1      matt 			pt_entry_t *ptep;
   1466       1.29       scw 			(void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
   1467       1.17   thorpej 			if (__predict_false(pmap_pde_section(pde))) {
   1468  1.52.16.1      matt 				paddr_t s_frame = L1_S_FRAME;
   1469  1.52.16.1      matt 				paddr_t s_offset = L1_S_OFFSET;
   1470  1.52.16.1      matt #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   1471  1.52.16.1      matt 				if (__predict_false(pmap_pde_supersection(pde))) {
   1472  1.52.16.1      matt 					s_frame = L1_SS_FRAME;
   1473  1.52.16.1      matt 					s_offset = L1_SS_OFFSET;
   1474       1.17   thorpej 				}
   1475  1.52.16.1      matt #endif
   1476  1.52.16.1      matt 				curaddr = (*pde & s_frame) | (vaddr & s_offset);
   1477  1.52.16.1      matt 				coherent = (*pde & L1_S_CACHE_MASK) == 0;
   1478       1.17   thorpej 			} else {
   1479  1.52.16.1      matt 				pt_entry_t pte = *ptep;
   1480  1.52.16.1      matt 				KDASSERTMSG((pte & L2_TYPE_MASK) != L2_TYPE_INV,
   1481  1.52.16.1      matt 				    "va=%#"PRIxVADDR" pde=%#x ptep=%p pte=%#x",
   1482  1.52.16.1      matt 				    vaddr, *pde, ptep, pte);
   1483       1.17   thorpej 				if (__predict_false((pte & L2_TYPE_MASK)
   1484       1.17   thorpej 						    == L2_TYPE_L)) {
   1485       1.17   thorpej 					curaddr = (pte & L2_L_FRAME) |
   1486       1.17   thorpej 					    (vaddr & L2_L_OFFSET);
   1487  1.52.16.1      matt 					coherent = (pte & L2_L_CACHE_MASK) == 0;
   1488       1.17   thorpej 				} else {
   1489       1.17   thorpej 					curaddr = (pte & L2_S_FRAME) |
   1490       1.17   thorpej 					    (vaddr & L2_S_OFFSET);
   1491  1.52.16.1      matt 					coherent = (pte & L2_S_CACHE_MASK) == 0;
   1492       1.17   thorpej 				}
   1493       1.17   thorpej 			}
   1494       1.34    briggs 		} else {
   1495       1.17   thorpej 			(void) pmap_extract(pmap, vaddr, &curaddr);
   1496  1.52.16.1      matt 			coherent = false;
   1497       1.34    briggs 		}
   1498        1.1     chris 
   1499        1.1     chris 		/*
   1500        1.1     chris 		 * Compute the segment size, and adjust counts.
   1501        1.1     chris 		 */
   1502       1.27   thorpej 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
   1503        1.1     chris 		if (buflen < sgsize)
   1504        1.1     chris 			sgsize = buflen;
   1505        1.1     chris 
   1506  1.52.16.1      matt 		error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize,
   1507  1.52.16.1      matt 		    coherent);
   1508       1.41   thorpej 		if (error)
   1509       1.41   thorpej 			return (error);
   1510        1.1     chris 
   1511        1.1     chris 		vaddr += sgsize;
   1512        1.1     chris 		buflen -= sgsize;
   1513        1.1     chris 	}
   1514        1.1     chris 
   1515        1.1     chris 	return (0);
   1516        1.1     chris }
   1517        1.1     chris 
   1518        1.1     chris /*
   1519        1.1     chris  * Allocate physical memory from the given physical address range.
   1520        1.1     chris  * Called by DMA-safe memory allocation methods.
   1521        1.1     chris  */
   1522        1.1     chris int
   1523        1.7   thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1524        1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1525       1.11   thorpej     int flags, paddr_t low, paddr_t high)
   1526        1.1     chris {
   1527       1.11   thorpej 	paddr_t curaddr, lastaddr;
   1528        1.1     chris 	struct vm_page *m;
   1529        1.1     chris 	struct pglist mlist;
   1530        1.1     chris 	int curseg, error;
   1531        1.1     chris 
   1532  1.52.16.1      matt 	KASSERTMSG(boundary == 0 || (boundary & (boundary-1)) == 0,
   1533  1.52.16.1      matt 	    "invalid boundary %#lx", boundary);
   1534  1.52.16.1      matt 
   1535        1.1     chris #ifdef DEBUG_DMA
   1536        1.1     chris 	printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
   1537        1.1     chris 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
   1538        1.1     chris #endif	/* DEBUG_DMA */
   1539        1.1     chris 
   1540        1.1     chris 	/* Always round the size. */
   1541        1.1     chris 	size = round_page(size);
   1542        1.1     chris 
   1543        1.1     chris 	/*
   1544  1.52.16.1      matt 	 * We accept boundaries < size, splitting in multiple segments
   1545  1.52.16.1      matt 	 * if needed. uvm_pglistalloc does not, so compute an appropriate
   1546  1.52.16.1      matt 	 * boundary: next power of 2 >= size
   1547  1.52.16.1      matt 	 */
   1548  1.52.16.1      matt 	bus_size_t uboundary = boundary;
   1549  1.52.16.1      matt 	if (uboundary <= PAGE_SIZE) {
   1550  1.52.16.1      matt 		uboundary = 0;
   1551  1.52.16.1      matt 	} else {
   1552  1.52.16.1      matt 		while (uboundary < size) {
   1553  1.52.16.1      matt 			uboundary <<= 1;
   1554  1.52.16.1      matt 		}
   1555  1.52.16.1      matt 	}
   1556  1.52.16.1      matt 
   1557  1.52.16.1      matt 	/*
   1558        1.1     chris 	 * Allocate pages from the VM system.
   1559        1.1     chris 	 */
   1560  1.52.16.1      matt 	error = uvm_pglistalloc(size, low, high, alignment, uboundary,
   1561        1.1     chris 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
   1562        1.1     chris 	if (error)
   1563        1.1     chris 		return (error);
   1564        1.1     chris 
   1565        1.1     chris 	/*
   1566        1.1     chris 	 * Compute the location, size, and number of segments actually
   1567        1.1     chris 	 * returned by the VM code.
   1568        1.1     chris 	 */
   1569       1.42     chris 	m = TAILQ_FIRST(&mlist);
   1570        1.1     chris 	curseg = 0;
   1571        1.1     chris 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
   1572        1.1     chris 	segs[curseg].ds_len = PAGE_SIZE;
   1573        1.1     chris #ifdef DEBUG_DMA
   1574        1.1     chris 		printf("alloc: page %lx\n", lastaddr);
   1575        1.1     chris #endif	/* DEBUG_DMA */
   1576       1.52        ad 	m = TAILQ_NEXT(m, pageq.queue);
   1577        1.1     chris 
   1578       1.52        ad 	for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
   1579        1.1     chris 		curaddr = VM_PAGE_TO_PHYS(m);
   1580  1.52.16.1      matt 		KASSERTMSG(low <= curaddr && curaddr < high,
   1581  1.52.16.1      matt 		    "uvm_pglistalloc returned non-sensicaladdress %#lx "
   1582  1.52.16.1      matt 		    "(low=%#lx, high=%#lx\n", curaddr, low, high);
   1583        1.1     chris #ifdef DEBUG_DMA
   1584        1.1     chris 		printf("alloc: page %lx\n", curaddr);
   1585        1.1     chris #endif	/* DEBUG_DMA */
   1586  1.52.16.1      matt 		if (curaddr == lastaddr + PAGE_SIZE
   1587  1.52.16.1      matt 		    && (lastaddr & boundary) == (curaddr & boundary))
   1588        1.1     chris 			segs[curseg].ds_len += PAGE_SIZE;
   1589        1.1     chris 		else {
   1590        1.1     chris 			curseg++;
   1591  1.52.16.1      matt 			if (curseg >= nsegs) {
   1592  1.52.16.1      matt 				uvm_pglistfree(&mlist);
   1593  1.52.16.1      matt 				return EFBIG;
   1594  1.52.16.1      matt 			}
   1595        1.1     chris 			segs[curseg].ds_addr = curaddr;
   1596        1.1     chris 			segs[curseg].ds_len = PAGE_SIZE;
   1597        1.1     chris 		}
   1598        1.1     chris 		lastaddr = curaddr;
   1599        1.1     chris 	}
   1600        1.1     chris 
   1601        1.1     chris 	*rsegs = curseg + 1;
   1602        1.1     chris 
   1603       1.15   thorpej 	return (0);
   1604       1.15   thorpej }
   1605       1.15   thorpej 
   1606       1.15   thorpej /*
   1607       1.15   thorpej  * Check if a memory region intersects with a DMA range, and return the
   1608       1.15   thorpej  * page-rounded intersection if it does.
   1609       1.15   thorpej  */
   1610       1.15   thorpej int
   1611       1.15   thorpej arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
   1612       1.15   thorpej     paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
   1613       1.15   thorpej {
   1614       1.15   thorpej 	struct arm32_dma_range *dr;
   1615       1.15   thorpej 	int i;
   1616       1.15   thorpej 
   1617       1.15   thorpej 	if (ranges == NULL)
   1618       1.15   thorpej 		return (0);
   1619       1.15   thorpej 
   1620       1.15   thorpej 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
   1621       1.15   thorpej 		if (dr->dr_sysbase <= pa &&
   1622       1.15   thorpej 		    pa < (dr->dr_sysbase + dr->dr_len)) {
   1623       1.15   thorpej 			/*
   1624       1.15   thorpej 			 * Beginning of region intersects with this range.
   1625       1.15   thorpej 			 */
   1626       1.15   thorpej 			*pap = trunc_page(pa);
   1627       1.15   thorpej 			*sizep = round_page(min(pa + size,
   1628       1.15   thorpej 			    dr->dr_sysbase + dr->dr_len) - pa);
   1629       1.15   thorpej 			return (1);
   1630       1.15   thorpej 		}
   1631       1.15   thorpej 		if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
   1632       1.15   thorpej 			/*
   1633       1.15   thorpej 			 * End of region intersects with this range.
   1634       1.15   thorpej 			 */
   1635       1.15   thorpej 			*pap = trunc_page(dr->dr_sysbase);
   1636       1.15   thorpej 			*sizep = round_page(min((pa + size) - dr->dr_sysbase,
   1637       1.15   thorpej 			    dr->dr_len));
   1638       1.15   thorpej 			return (1);
   1639       1.15   thorpej 		}
   1640       1.15   thorpej 	}
   1641       1.15   thorpej 
   1642       1.15   thorpej 	/* No intersection found. */
   1643        1.1     chris 	return (0);
   1644        1.1     chris }
   1645  1.52.16.1      matt 
   1646  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1647  1.52.16.1      matt static int
   1648  1.52.16.1      matt _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
   1649  1.52.16.1      matt     bus_size_t size, int flags)
   1650  1.52.16.1      matt {
   1651  1.52.16.1      matt 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
   1652  1.52.16.1      matt 	int error = 0;
   1653  1.52.16.1      matt 
   1654  1.52.16.1      matt 	KASSERT(cookie != NULL);
   1655  1.52.16.1      matt 
   1656  1.52.16.1      matt 	cookie->id_bouncebuflen = round_page(size);
   1657  1.52.16.1      matt 	error = _bus_dmamem_alloc(t, cookie->id_bouncebuflen,
   1658  1.52.16.1      matt 	    PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
   1659  1.52.16.1      matt 	    map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
   1660  1.52.16.1      matt 	if (error == 0) {
   1661  1.52.16.1      matt 		error = _bus_dmamem_map(t, cookie->id_bouncesegs,
   1662  1.52.16.1      matt 		    cookie->id_nbouncesegs, cookie->id_bouncebuflen,
   1663  1.52.16.1      matt 		    (void **)&cookie->id_bouncebuf, flags);
   1664  1.52.16.1      matt 		if (error) {
   1665  1.52.16.1      matt 			_bus_dmamem_free(t, cookie->id_bouncesegs,
   1666  1.52.16.1      matt 			    cookie->id_nbouncesegs);
   1667  1.52.16.1      matt 			cookie->id_bouncebuflen = 0;
   1668  1.52.16.1      matt 			cookie->id_nbouncesegs = 0;
   1669  1.52.16.1      matt 		} else {
   1670  1.52.16.1      matt 			cookie->id_flags |= _BUS_DMA_HAS_BOUNCE;
   1671  1.52.16.1      matt 		}
   1672  1.52.16.1      matt 	} else {
   1673  1.52.16.1      matt 		cookie->id_bouncebuflen = 0;
   1674  1.52.16.1      matt 		cookie->id_nbouncesegs = 0;
   1675  1.52.16.1      matt 	}
   1676  1.52.16.1      matt 
   1677  1.52.16.1      matt 	return (error);
   1678  1.52.16.1      matt }
   1679  1.52.16.1      matt 
   1680  1.52.16.1      matt static void
   1681  1.52.16.1      matt _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
   1682  1.52.16.1      matt {
   1683  1.52.16.1      matt 	struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
   1684  1.52.16.1      matt 
   1685  1.52.16.1      matt 	KASSERT(cookie != NULL);
   1686  1.52.16.1      matt 
   1687  1.52.16.1      matt 	_bus_dmamem_unmap(t, cookie->id_bouncebuf, cookie->id_bouncebuflen);
   1688  1.52.16.1      matt 	_bus_dmamem_free(t, cookie->id_bouncesegs, cookie->id_nbouncesegs);
   1689  1.52.16.1      matt 	cookie->id_bouncebuflen = 0;
   1690  1.52.16.1      matt 	cookie->id_nbouncesegs = 0;
   1691  1.52.16.1      matt 	cookie->id_flags &= ~_BUS_DMA_HAS_BOUNCE;
   1692  1.52.16.1      matt }
   1693  1.52.16.1      matt 
   1694  1.52.16.1      matt /*
   1695  1.52.16.1      matt  * This function does the same as uiomove, but takes an explicit
   1696  1.52.16.1      matt  * direction, and does not update the uio structure.
   1697  1.52.16.1      matt  */
   1698  1.52.16.1      matt static int
   1699  1.52.16.1      matt _bus_dma_uiomove(void *buf, struct uio *uio, size_t n, int direction)
   1700  1.52.16.1      matt {
   1701  1.52.16.1      matt 	struct iovec *iov;
   1702  1.52.16.1      matt 	int error;
   1703  1.52.16.1      matt 	struct vmspace *vm;
   1704  1.52.16.1      matt 	char *cp;
   1705  1.52.16.1      matt 	size_t resid, cnt;
   1706  1.52.16.1      matt 	int i;
   1707  1.52.16.1      matt 
   1708  1.52.16.1      matt 	iov = uio->uio_iov;
   1709  1.52.16.1      matt 	vm = uio->uio_vmspace;
   1710  1.52.16.1      matt 	cp = buf;
   1711  1.52.16.1      matt 	resid = n;
   1712  1.52.16.1      matt 
   1713  1.52.16.1      matt 	for (i = 0; i < uio->uio_iovcnt && resid > 0; i++) {
   1714  1.52.16.1      matt 		iov = &uio->uio_iov[i];
   1715  1.52.16.1      matt 		if (iov->iov_len == 0)
   1716  1.52.16.1      matt 			continue;
   1717  1.52.16.1      matt 		cnt = MIN(resid, iov->iov_len);
   1718  1.52.16.1      matt 
   1719  1.52.16.1      matt 		if (!VMSPACE_IS_KERNEL_P(vm) &&
   1720  1.52.16.1      matt 		    (curlwp->l_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
   1721  1.52.16.1      matt 		    != 0) {
   1722  1.52.16.1      matt 			preempt();
   1723  1.52.16.1      matt 		}
   1724  1.52.16.1      matt 		if (direction == UIO_READ) {
   1725  1.52.16.1      matt 			error = copyout_vmspace(vm, cp, iov->iov_base, cnt);
   1726  1.52.16.1      matt 		} else {
   1727  1.52.16.1      matt 			error = copyin_vmspace(vm, iov->iov_base, cp, cnt);
   1728  1.52.16.1      matt 		}
   1729  1.52.16.1      matt 		if (error)
   1730  1.52.16.1      matt 			return (error);
   1731  1.52.16.1      matt 		cp += cnt;
   1732  1.52.16.1      matt 		resid -= cnt;
   1733  1.52.16.1      matt 	}
   1734  1.52.16.1      matt 	return (0);
   1735  1.52.16.1      matt }
   1736  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
   1737  1.52.16.1      matt 
   1738  1.52.16.1      matt int
   1739  1.52.16.1      matt _bus_dmatag_subregion(bus_dma_tag_t tag, bus_addr_t min_addr,
   1740  1.52.16.1      matt     bus_addr_t max_addr, bus_dma_tag_t *newtag, int flags)
   1741  1.52.16.1      matt {
   1742  1.52.16.1      matt 
   1743  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1744  1.52.16.1      matt 	struct arm32_dma_range *dr;
   1745  1.52.16.1      matt 	bool subset = false;
   1746  1.52.16.1      matt 	size_t nranges = 0;
   1747  1.52.16.1      matt 	size_t i;
   1748  1.52.16.1      matt 	for (i = 0, dr = tag->_ranges; i < tag->_nranges; i++, dr++) {
   1749  1.52.16.1      matt 		if (dr->dr_sysbase <= min_addr
   1750  1.52.16.1      matt 		    && max_addr <= dr->dr_sysbase + dr->dr_len - 1) {
   1751  1.52.16.1      matt 			subset = true;
   1752  1.52.16.1      matt 		}
   1753  1.52.16.1      matt 		if (min_addr <= dr->dr_sysbase + dr->dr_len
   1754  1.52.16.1      matt 		    && max_addr >= dr->dr_sysbase) {
   1755  1.52.16.1      matt 			nranges++;
   1756  1.52.16.1      matt 		}
   1757  1.52.16.1      matt 	}
   1758  1.52.16.1      matt 	if (subset) {
   1759  1.52.16.1      matt 		*newtag = tag;
   1760  1.52.16.1      matt 		/* if the tag must be freed, add a reference */
   1761  1.52.16.1      matt 		if (tag->_tag_needs_free)
   1762  1.52.16.1      matt 			(tag->_tag_needs_free)++;
   1763  1.52.16.1      matt 		return 0;
   1764  1.52.16.1      matt 	}
   1765  1.52.16.1      matt 	if (nranges == 0) {
   1766  1.52.16.1      matt 		nranges = 1;
   1767  1.52.16.1      matt 	}
   1768  1.52.16.1      matt 
   1769  1.52.16.1      matt 	size_t mallocsize = sizeof(*tag) + nranges * sizeof(*dr);
   1770  1.52.16.1      matt 	if ((*newtag = malloc(mallocsize, M_DMAMAP,
   1771  1.52.16.1      matt 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
   1772  1.52.16.1      matt 		return ENOMEM;
   1773  1.52.16.1      matt 
   1774  1.52.16.1      matt 	dr = (void *)(*newtag + 1);
   1775  1.52.16.1      matt 	**newtag = *tag;
   1776  1.52.16.1      matt 	(*newtag)->_tag_needs_free = 1;
   1777  1.52.16.1      matt 	(*newtag)->_ranges = dr;
   1778  1.52.16.1      matt 	(*newtag)->_nranges = nranges;
   1779  1.52.16.1      matt 
   1780  1.52.16.1      matt 	if (tag->_ranges == NULL) {
   1781  1.52.16.1      matt 		dr->dr_sysbase = min_addr;
   1782  1.52.16.1      matt 		dr->dr_busbase = min_addr;
   1783  1.52.16.1      matt 		dr->dr_len = max_addr + 1 - min_addr;
   1784  1.52.16.1      matt 	} else {
   1785  1.52.16.1      matt 		for (i = 0; i < nranges; i++) {
   1786  1.52.16.1      matt 			if (min_addr > dr->dr_sysbase + dr->dr_len
   1787  1.52.16.1      matt 			    || max_addr < dr->dr_sysbase)
   1788  1.52.16.1      matt 				continue;
   1789  1.52.16.1      matt 			dr[0] = tag->_ranges[i];
   1790  1.52.16.1      matt 			if (dr->dr_sysbase < min_addr) {
   1791  1.52.16.1      matt 				psize_t diff = min_addr - dr->dr_sysbase;
   1792  1.52.16.1      matt 				dr->dr_busbase += diff;
   1793  1.52.16.1      matt 				dr->dr_len -= diff;
   1794  1.52.16.1      matt 				dr->dr_sysbase += diff;
   1795  1.52.16.1      matt 			}
   1796  1.52.16.1      matt 			if (max_addr != 0xffffffff
   1797  1.52.16.1      matt 			    && max_addr + 1 < dr->dr_sysbase + dr->dr_len) {
   1798  1.52.16.1      matt 				dr->dr_len = max_addr + 1 - dr->dr_sysbase;
   1799  1.52.16.1      matt 			}
   1800  1.52.16.1      matt 			dr++;
   1801  1.52.16.1      matt 		}
   1802  1.52.16.1      matt 	}
   1803  1.52.16.1      matt 
   1804  1.52.16.1      matt 	return 0;
   1805  1.52.16.1      matt #else
   1806  1.52.16.1      matt 	return EOPNOTSUPP;
   1807  1.52.16.1      matt #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
   1808  1.52.16.1      matt }
   1809  1.52.16.1      matt 
   1810  1.52.16.1      matt void
   1811  1.52.16.1      matt _bus_dmatag_destroy(bus_dma_tag_t tag)
   1812  1.52.16.1      matt {
   1813  1.52.16.1      matt #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
   1814  1.52.16.1      matt 	switch (tag->_tag_needs_free) {
   1815  1.52.16.1      matt 	case 0:
   1816  1.52.16.1      matt 		break;				/* not allocated with malloc */
   1817  1.52.16.1      matt 	case 1:
   1818  1.52.16.1      matt 		free(tag, M_DMAMAP);		/* last reference to tag */
   1819  1.52.16.1      matt 		break;
   1820  1.52.16.1      matt 	default:
   1821  1.52.16.1      matt 		(tag->_tag_needs_free)--;	/* one less reference */
   1822  1.52.16.1      matt 	}
   1823  1.52.16.1      matt #endif
   1824  1.52.16.1      matt }
   1825