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