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