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