Home | History | Annotate | Line # | Download | only in arm32
bus_dma.c revision 1.11.4.1
      1  1.11.4.1        he /*	$NetBSD: bus_dma.c,v 1.11.4.1 2002/12/07 19:23:21 he 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  * 3. All advertising materials mentioning features or use of this software
     20       1.1     chris  *    must display the following acknowledgement:
     21       1.1     chris  *	This product includes software developed by the NetBSD
     22       1.1     chris  *	Foundation, Inc. and its contributors.
     23       1.1     chris  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1     chris  *    contributors may be used to endorse or promote products derived
     25       1.1     chris  *    from this software without specific prior written permission.
     26       1.1     chris  *
     27       1.1     chris  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1     chris  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1     chris  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1     chris  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1     chris  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1     chris  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1     chris  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1     chris  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1     chris  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1     chris  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1     chris  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1     chris  */
     39       1.1     chris 
     40       1.1     chris #include <sys/param.h>
     41       1.1     chris #include <sys/systm.h>
     42       1.1     chris #include <sys/kernel.h>
     43       1.1     chris #include <sys/map.h>
     44       1.1     chris #include <sys/proc.h>
     45       1.1     chris #include <sys/buf.h>
     46       1.1     chris #include <sys/reboot.h>
     47       1.1     chris #include <sys/conf.h>
     48       1.1     chris #include <sys/file.h>
     49       1.1     chris #include <sys/malloc.h>
     50       1.1     chris #include <sys/mbuf.h>
     51       1.1     chris #include <sys/vnode.h>
     52       1.1     chris #include <sys/device.h>
     53       1.1     chris 
     54       1.1     chris #include <uvm/uvm_extern.h>
     55       1.1     chris 
     56       1.1     chris #define _ARM32_BUS_DMA_PRIVATE
     57       1.1     chris #include <machine/bus.h>
     58       1.1     chris 
     59       1.1     chris #include <machine/cpu.h>
     60       1.4   thorpej 
     61       1.4   thorpej #include <arm/cpufunc.h>
     62       1.1     chris 
     63       1.7   thorpej int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
     64      1.11   thorpej 	    bus_size_t, struct proc *, int, paddr_t *, int *, int);
     65       1.7   thorpej int	_bus_dma_inrange(bus_dma_segment_t *, int, bus_addr_t);
     66       1.1     chris 
     67       1.1     chris /*
     68       1.1     chris  * Common function for DMA map creation.  May be called by bus-specific
     69       1.1     chris  * DMA map creation functions.
     70       1.1     chris  */
     71       1.1     chris int
     72       1.7   thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
     73       1.7   thorpej     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
     74       1.1     chris {
     75       1.1     chris 	struct arm32_bus_dmamap *map;
     76       1.1     chris 	void *mapstore;
     77       1.1     chris 	size_t mapsize;
     78       1.1     chris 
     79       1.1     chris #ifdef DEBUG_DMA
     80       1.1     chris 	printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
     81       1.1     chris 	    t, size, nsegments, maxsegsz, boundary, flags);
     82       1.1     chris #endif	/* DEBUG_DMA */
     83       1.1     chris 
     84       1.1     chris 	/*
     85       1.1     chris 	 * Allocate and initialize the DMA map.  The end of the map
     86       1.1     chris 	 * is a variable-sized array of segments, so we allocate enough
     87       1.1     chris 	 * room for them in one shot.
     88       1.1     chris 	 *
     89       1.1     chris 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
     90       1.1     chris 	 * of ALLOCNOW notifies others that we've reserved these resources,
     91       1.1     chris 	 * and they are not to be freed.
     92       1.1     chris 	 *
     93       1.1     chris 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
     94       1.1     chris 	 * the (nsegments - 1).
     95       1.1     chris 	 */
     96       1.1     chris 	mapsize = sizeof(struct arm32_bus_dmamap) +
     97       1.1     chris 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
     98       1.1     chris 	if ((mapstore = malloc(mapsize, M_DMAMAP,
     99       1.1     chris 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
    100       1.1     chris 		return (ENOMEM);
    101       1.1     chris 
    102       1.1     chris 	memset(mapstore, 0, mapsize);
    103       1.1     chris 	map = (struct arm32_bus_dmamap *)mapstore;
    104       1.1     chris 	map->_dm_size = size;
    105       1.1     chris 	map->_dm_segcnt = nsegments;
    106       1.1     chris 	map->_dm_maxsegsz = maxsegsz;
    107       1.1     chris 	map->_dm_boundary = boundary;
    108       1.1     chris 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    109  1.11.4.1        he 	map->_dm_origbuf = NULL;
    110  1.11.4.1        he 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    111       1.8   thorpej 	map->_dm_proc = NULL;
    112       1.1     chris 	map->dm_mapsize = 0;		/* no valid mappings */
    113       1.1     chris 	map->dm_nsegs = 0;
    114       1.1     chris 
    115       1.1     chris 	*dmamp = map;
    116       1.1     chris #ifdef DEBUG_DMA
    117       1.1     chris 	printf("dmamap_create:map=%p\n", map);
    118       1.1     chris #endif	/* DEBUG_DMA */
    119       1.1     chris 	return (0);
    120       1.1     chris }
    121       1.1     chris 
    122       1.1     chris /*
    123       1.1     chris  * Common function for DMA map destruction.  May be called by bus-specific
    124       1.1     chris  * DMA map destruction functions.
    125       1.1     chris  */
    126       1.1     chris void
    127       1.7   thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    128       1.1     chris {
    129       1.1     chris 
    130       1.1     chris #ifdef DEBUG_DMA
    131       1.1     chris 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
    132       1.1     chris #endif	/* DEBUG_DMA */
    133  1.11.4.1        he 
    134  1.11.4.1        he 	/*
    135  1.11.4.1        he 	 * Explicit unload.
    136  1.11.4.1        he 	 */
    137  1.11.4.1        he 	map->dm_mapsize = 0;
    138  1.11.4.1        he 	map->dm_nsegs = 0;
    139  1.11.4.1        he 	map->_dm_origbuf = NULL;
    140  1.11.4.1        he 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    141  1.11.4.1        he 	map->_dm_proc = NULL;
    142  1.11.4.1        he 
    143       1.1     chris 	free(map, M_DEVBUF);
    144       1.1     chris }
    145       1.1     chris 
    146       1.1     chris /*
    147       1.1     chris  * Common function for loading a DMA map with a linear buffer.  May
    148       1.1     chris  * be called by bus-specific DMA map load functions.
    149       1.1     chris  */
    150       1.1     chris int
    151       1.7   thorpej _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    152       1.7   thorpej     bus_size_t buflen, struct proc *p, int flags)
    153       1.1     chris {
    154      1.11   thorpej 	paddr_t lastaddr;
    155       1.1     chris 	int seg, error;
    156       1.1     chris 
    157       1.1     chris #ifdef DEBUG_DMA
    158       1.1     chris 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
    159       1.1     chris 	    t, map, buf, buflen, p, flags);
    160       1.1     chris #endif	/* DEBUG_DMA */
    161       1.1     chris 
    162       1.1     chris 	/*
    163       1.1     chris 	 * Make sure that on error condition we return "no valid mappings".
    164       1.1     chris 	 */
    165       1.1     chris 	map->dm_mapsize = 0;
    166       1.1     chris 	map->dm_nsegs = 0;
    167       1.1     chris 
    168       1.1     chris 	if (buflen > map->_dm_size)
    169       1.1     chris 		return (EINVAL);
    170       1.1     chris 
    171       1.1     chris 	seg = 0;
    172       1.1     chris 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
    173       1.1     chris 	    &lastaddr, &seg, 1);
    174       1.1     chris 	if (error == 0) {
    175       1.1     chris 		map->dm_mapsize = buflen;
    176       1.1     chris 		map->dm_nsegs = seg + 1;
    177  1.11.4.1        he 		map->_dm_origbuf = buf;
    178  1.11.4.1        he 		map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
    179       1.8   thorpej 		map->_dm_proc = p;
    180       1.1     chris 	}
    181       1.1     chris #ifdef DEBUG_DMA
    182       1.1     chris 	printf("dmamap_load: error=%d\n", error);
    183       1.1     chris #endif	/* DEBUG_DMA */
    184       1.1     chris 	return (error);
    185       1.1     chris }
    186       1.1     chris 
    187       1.1     chris /*
    188       1.1     chris  * Like _bus_dmamap_load(), but for mbufs.
    189       1.1     chris  */
    190       1.1     chris int
    191       1.7   thorpej _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    192       1.7   thorpej     int flags)
    193       1.1     chris {
    194      1.11   thorpej 	paddr_t lastaddr;
    195       1.1     chris 	int seg, error, first;
    196       1.1     chris 	struct mbuf *m;
    197       1.1     chris 
    198       1.1     chris #ifdef DEBUG_DMA
    199       1.1     chris 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
    200       1.1     chris 	    t, map, m0, flags);
    201       1.1     chris #endif	/* DEBUG_DMA */
    202       1.1     chris 
    203       1.1     chris 	/*
    204       1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    205       1.1     chris 	 */
    206       1.1     chris 	map->dm_mapsize = 0;
    207       1.1     chris 	map->dm_nsegs = 0;
    208       1.1     chris 
    209       1.1     chris #ifdef DIAGNOSTIC
    210       1.1     chris 	if ((m0->m_flags & M_PKTHDR) == 0)
    211       1.1     chris 		panic("_bus_dmamap_load_mbuf: no packet header");
    212       1.1     chris #endif	/* DIAGNOSTIC */
    213       1.1     chris 
    214       1.1     chris 	if (m0->m_pkthdr.len > map->_dm_size)
    215       1.1     chris 		return (EINVAL);
    216       1.1     chris 
    217       1.1     chris 	first = 1;
    218       1.1     chris 	seg = 0;
    219       1.1     chris 	error = 0;
    220       1.1     chris 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    221       1.1     chris 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
    222       1.1     chris 		    NULL, flags, &lastaddr, &seg, first);
    223       1.1     chris 		first = 0;
    224       1.1     chris 	}
    225       1.1     chris 	if (error == 0) {
    226       1.1     chris 		map->dm_mapsize = m0->m_pkthdr.len;
    227       1.1     chris 		map->dm_nsegs = seg + 1;
    228  1.11.4.1        he 		map->_dm_origbuf = m0;
    229  1.11.4.1        he 		map->_dm_buftype = ARM32_BUFTYPE_MBUF;
    230       1.8   thorpej 		map->_dm_proc = NULL;	/* always kernel */
    231       1.1     chris 	}
    232       1.1     chris #ifdef DEBUG_DMA
    233       1.1     chris 	printf("dmamap_load_mbuf: error=%d\n", error);
    234       1.1     chris #endif	/* DEBUG_DMA */
    235       1.1     chris 	return (error);
    236       1.1     chris }
    237       1.1     chris 
    238       1.1     chris /*
    239       1.1     chris  * Like _bus_dmamap_load(), but for uios.
    240       1.1     chris  */
    241       1.1     chris int
    242       1.7   thorpej _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    243       1.7   thorpej     int flags)
    244       1.1     chris {
    245      1.11   thorpej 	paddr_t lastaddr;
    246       1.1     chris 	int seg, i, error, first;
    247       1.1     chris 	bus_size_t minlen, resid;
    248       1.1     chris 	struct proc *p = NULL;
    249       1.1     chris 	struct iovec *iov;
    250       1.1     chris 	caddr_t addr;
    251       1.1     chris 
    252       1.1     chris 	/*
    253       1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    254       1.1     chris 	 */
    255       1.1     chris 	map->dm_mapsize = 0;
    256       1.1     chris 	map->dm_nsegs = 0;
    257       1.1     chris 
    258       1.1     chris 	resid = uio->uio_resid;
    259       1.1     chris 	iov = uio->uio_iov;
    260       1.1     chris 
    261       1.1     chris 	if (uio->uio_segflg == UIO_USERSPACE) {
    262       1.1     chris 		p = uio->uio_procp;
    263       1.1     chris #ifdef DIAGNOSTIC
    264       1.1     chris 		if (p == NULL)
    265       1.1     chris 			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
    266       1.1     chris #endif
    267       1.1     chris 	}
    268       1.1     chris 
    269       1.1     chris 	first = 1;
    270       1.1     chris 	seg = 0;
    271       1.1     chris 	error = 0;
    272       1.1     chris 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    273       1.1     chris 		/*
    274       1.1     chris 		 * Now at the first iovec to load.  Load each iovec
    275       1.1     chris 		 * until we have exhausted the residual count.
    276       1.1     chris 		 */
    277       1.1     chris 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    278       1.1     chris 		addr = (caddr_t)iov[i].iov_base;
    279       1.1     chris 
    280       1.1     chris 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
    281       1.1     chris 		    p, flags, &lastaddr, &seg, first);
    282       1.1     chris 		first = 0;
    283       1.1     chris 
    284       1.1     chris 		resid -= minlen;
    285       1.1     chris 	}
    286       1.1     chris 	if (error == 0) {
    287       1.1     chris 		map->dm_mapsize = uio->uio_resid;
    288       1.1     chris 		map->dm_nsegs = seg + 1;
    289  1.11.4.1        he 		map->_dm_origbuf = uio;
    290  1.11.4.1        he 		map->_dm_buftype = ARM32_BUFTYPE_UIO;
    291       1.8   thorpej 		map->_dm_proc = p;
    292       1.1     chris 	}
    293       1.1     chris 	return (error);
    294       1.1     chris }
    295       1.1     chris 
    296       1.1     chris /*
    297       1.1     chris  * Like _bus_dmamap_load(), but for raw memory allocated with
    298       1.1     chris  * bus_dmamem_alloc().
    299       1.1     chris  */
    300       1.1     chris int
    301       1.7   thorpej _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    302       1.7   thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    303       1.1     chris {
    304       1.1     chris 
    305       1.1     chris 	panic("_bus_dmamap_load_raw: not implemented");
    306       1.1     chris }
    307       1.1     chris 
    308       1.1     chris /*
    309       1.1     chris  * Common function for unloading a DMA map.  May be called by
    310       1.1     chris  * bus-specific DMA map unload functions.
    311       1.1     chris  */
    312       1.1     chris void
    313       1.7   thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    314       1.1     chris {
    315       1.1     chris 
    316       1.1     chris #ifdef DEBUG_DMA
    317       1.1     chris 	printf("dmamap_unload: t=%p map=%p\n", t, map);
    318       1.1     chris #endif	/* DEBUG_DMA */
    319       1.1     chris 
    320       1.1     chris 	/*
    321       1.1     chris 	 * No resources to free; just mark the mappings as
    322       1.1     chris 	 * invalid.
    323       1.1     chris 	 */
    324       1.1     chris 	map->dm_mapsize = 0;
    325       1.1     chris 	map->dm_nsegs = 0;
    326  1.11.4.1        he 	map->_dm_origbuf = NULL;
    327  1.11.4.1        he 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    328       1.8   thorpej 	map->_dm_proc = NULL;
    329       1.1     chris }
    330       1.1     chris 
    331  1.11.4.1        he static void
    332  1.11.4.1        he _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    333  1.11.4.1        he     bus_size_t len, int ops)
    334  1.11.4.1        he {
    335  1.11.4.1        he 	vaddr_t addr = (vaddr_t) map->_dm_origbuf;
    336  1.11.4.1        he 
    337  1.11.4.1        he 	addr += offset;
    338  1.11.4.1        he 	len -= offset;
    339  1.11.4.1        he 
    340  1.11.4.1        he 	switch (ops) {
    341  1.11.4.1        he 	case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    342  1.11.4.1        he 		cpu_dcache_wbinv_range(addr, len);
    343  1.11.4.1        he 		break;
    344  1.11.4.1        he 
    345  1.11.4.1        he 	case BUS_DMASYNC_PREREAD:
    346  1.11.4.1        he #if 1
    347  1.11.4.1        he 		cpu_dcache_wbinv_range(addr, len);
    348  1.11.4.1        he #else
    349  1.11.4.1        he 		cpu_dcache_inv_range(addr, len);
    350  1.11.4.1        he #endif
    351  1.11.4.1        he 		break;
    352  1.11.4.1        he 
    353  1.11.4.1        he 	case BUS_DMASYNC_PREWRITE:
    354  1.11.4.1        he 		cpu_dcache_wb_range(addr, len);
    355  1.11.4.1        he 		break;
    356  1.11.4.1        he 	}
    357  1.11.4.1        he }
    358  1.11.4.1        he 
    359  1.11.4.1        he static void
    360  1.11.4.1        he _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    361  1.11.4.1        he     bus_size_t len, int ops)
    362  1.11.4.1        he {
    363  1.11.4.1        he 	struct mbuf *m, *m0 = map->_dm_origbuf;
    364  1.11.4.1        he 	bus_size_t minlen, moff;
    365  1.11.4.1        he 	vaddr_t maddr;
    366  1.11.4.1        he 
    367  1.11.4.1        he 	for (moff = offset, m = m0; m != NULL && len != 0;
    368  1.11.4.1        he 	     m = m->m_next) {
    369  1.11.4.1        he 		/* Find the beginning mbuf. */
    370  1.11.4.1        he 		if (moff >= m->m_len) {
    371  1.11.4.1        he 			moff -= m->m_len;
    372  1.11.4.1        he 			continue;
    373  1.11.4.1        he 		}
    374  1.11.4.1        he 
    375  1.11.4.1        he 		/*
    376  1.11.4.1        he 		 * Now at the first mbuf to sync; nail each one until
    377  1.11.4.1        he 		 * we have exhausted the length.
    378  1.11.4.1        he 		 */
    379  1.11.4.1        he 		minlen = m->m_len - moff;
    380  1.11.4.1        he 		if (len < minlen)
    381  1.11.4.1        he 			minlen = len;
    382  1.11.4.1        he 
    383  1.11.4.1        he 		maddr = mtod(m, vaddr_t);
    384  1.11.4.1        he 		maddr += moff;
    385  1.11.4.1        he 
    386  1.11.4.1        he 		switch (ops) {
    387  1.11.4.1        he 		case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    388  1.11.4.1        he 			cpu_dcache_wbinv_range(maddr, minlen);
    389  1.11.4.1        he 			break;
    390  1.11.4.1        he 
    391  1.11.4.1        he 		case BUS_DMASYNC_PREREAD:
    392  1.11.4.1        he #if 1
    393  1.11.4.1        he 			cpu_dcache_wbinv_range(maddr, minlen);
    394  1.11.4.1        he #else
    395  1.11.4.1        he 			cpu_dcache_inv_range(maddr, minlen);
    396  1.11.4.1        he #endif
    397  1.11.4.1        he 			break;
    398  1.11.4.1        he 
    399  1.11.4.1        he 		case BUS_DMASYNC_PREWRITE:
    400  1.11.4.1        he 			cpu_dcache_wb_range(maddr, minlen);
    401  1.11.4.1        he 			break;
    402  1.11.4.1        he 		}
    403  1.11.4.1        he 		moff = 0;
    404  1.11.4.1        he 		len -= minlen;
    405  1.11.4.1        he 	}
    406  1.11.4.1        he }
    407  1.11.4.1        he 
    408  1.11.4.1        he static void
    409  1.11.4.1        he _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    410  1.11.4.1        he     bus_size_t len, int ops)
    411  1.11.4.1        he {
    412  1.11.4.1        he 	struct uio *uio = map->_dm_origbuf;
    413  1.11.4.1        he 	struct iovec *iov;
    414  1.11.4.1        he 	bus_size_t minlen, ioff;
    415  1.11.4.1        he 	vaddr_t addr;
    416  1.11.4.1        he 
    417  1.11.4.1        he 	for (iov = uio->uio_iov, ioff = offset; len != 0; iov++) {
    418  1.11.4.1        he 		/* Find the beginning iovec. */
    419  1.11.4.1        he 		if (ioff >= iov->iov_len) {
    420  1.11.4.1        he 			ioff -= iov->iov_len;
    421  1.11.4.1        he 			continue;
    422  1.11.4.1        he 		}
    423  1.11.4.1        he 
    424  1.11.4.1        he 		/*
    425  1.11.4.1        he 		 * Now at the first iovec to sync; nail each one until
    426  1.11.4.1        he 		 * we have exhausted the length.
    427  1.11.4.1        he 		 */
    428  1.11.4.1        he 		minlen = iov->iov_len - ioff;
    429  1.11.4.1        he 		if (len < minlen)
    430  1.11.4.1        he 			minlen = len;
    431  1.11.4.1        he 
    432  1.11.4.1        he 		addr = (vaddr_t) iov->iov_base;
    433  1.11.4.1        he 		addr += ioff;
    434  1.11.4.1        he 
    435  1.11.4.1        he 		switch (ops) {
    436  1.11.4.1        he 		case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    437  1.11.4.1        he 			cpu_dcache_wbinv_range(addr, minlen);
    438  1.11.4.1        he 			break;
    439  1.11.4.1        he 
    440  1.11.4.1        he 		case BUS_DMASYNC_PREREAD:
    441  1.11.4.1        he #if 1
    442  1.11.4.1        he 			cpu_dcache_wbinv_range(addr, minlen);
    443  1.11.4.1        he #else
    444  1.11.4.1        he 			cpu_dcache_inv_range(addr, minlen);
    445  1.11.4.1        he #endif
    446  1.11.4.1        he 			break;
    447  1.11.4.1        he 
    448  1.11.4.1        he 		case BUS_DMASYNC_PREWRITE:
    449  1.11.4.1        he 			cpu_dcache_wb_range(addr, minlen);
    450  1.11.4.1        he 			break;
    451  1.11.4.1        he 		}
    452  1.11.4.1        he 		ioff = 0;
    453  1.11.4.1        he 		len -= minlen;
    454  1.11.4.1        he 	}
    455  1.11.4.1        he }
    456  1.11.4.1        he 
    457       1.1     chris /*
    458       1.1     chris  * Common function for DMA map synchronization.  May be called
    459       1.1     chris  * by bus-specific DMA map synchronization functions.
    460       1.8   thorpej  *
    461       1.8   thorpej  * This version works for the Virtually Indexed Virtually Tagged
    462       1.8   thorpej  * cache found on 32-bit ARM processors.
    463       1.8   thorpej  *
    464       1.8   thorpej  * XXX Should have separate versions for write-through vs.
    465       1.8   thorpej  * XXX write-back caches.  We currently assume write-back
    466       1.8   thorpej  * XXX here, which is not as efficient as it could be for
    467       1.8   thorpej  * XXX the write-through case.
    468       1.1     chris  */
    469       1.1     chris void
    470       1.7   thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    471       1.7   thorpej     bus_size_t len, int ops)
    472       1.1     chris {
    473       1.1     chris 
    474       1.1     chris #ifdef DEBUG_DMA
    475       1.1     chris 	printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
    476       1.1     chris 	    t, map, offset, len, ops);
    477       1.1     chris #endif	/* DEBUG_DMA */
    478       1.1     chris 
    479       1.8   thorpej 	/*
    480       1.8   thorpej 	 * Mixing of PRE and POST operations is not allowed.
    481       1.8   thorpej 	 */
    482       1.8   thorpej 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    483       1.8   thorpej 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    484       1.8   thorpej 		panic("_bus_dmamap_sync: mix PRE and POST");
    485       1.8   thorpej 
    486       1.8   thorpej #ifdef DIAGNOSTIC
    487       1.8   thorpej 	if (offset >= map->dm_mapsize)
    488       1.8   thorpej 		panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
    489       1.8   thorpej 		    offset, map->dm_mapsize);
    490       1.8   thorpej 	if (len == 0 || (offset + len) > map->dm_mapsize)
    491       1.8   thorpej 		panic("_bus_dmamap_sync: bad length");
    492       1.8   thorpej #endif
    493       1.8   thorpej 
    494       1.8   thorpej 	/*
    495       1.8   thorpej 	 * For a virtually-indexed write-back cache, we need
    496       1.8   thorpej 	 * to do the following things:
    497       1.8   thorpej 	 *
    498       1.8   thorpej 	 *	PREREAD -- Invalidate the D-cache.  We do this
    499       1.8   thorpej 	 *	here in case a write-back is required by the back-end.
    500       1.8   thorpej 	 *
    501       1.8   thorpej 	 *	PREWRITE -- Write-back the D-cache.  Note that if
    502       1.8   thorpej 	 *	we are doing a PREREAD|PREWRITE, we can collapse
    503       1.8   thorpej 	 *	the whole thing into a single Wb-Inv.
    504       1.8   thorpej 	 *
    505       1.8   thorpej 	 *	POSTREAD -- Nothing.
    506       1.8   thorpej 	 *
    507       1.8   thorpej 	 *	POSTWRITE -- Nothing.
    508       1.8   thorpej 	 */
    509       1.8   thorpej 
    510       1.8   thorpej 	ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    511       1.8   thorpej 	if (ops == 0)
    512       1.8   thorpej 		return;
    513       1.8   thorpej 
    514       1.8   thorpej 	/*
    515       1.8   thorpej 	 * XXX Skip cache frobbing if mapping was COHERENT.
    516       1.8   thorpej 	 */
    517       1.8   thorpej 
    518       1.8   thorpej 	/*
    519       1.8   thorpej 	 * If the mapping is not the kernel's and also not the
    520       1.8   thorpej 	 * current process's (XXX actually, vmspace), then we
    521       1.8   thorpej 	 * don't have anything to do, since the cache is Wb-Inv'd
    522       1.8   thorpej 	 * on context switch.
    523       1.8   thorpej 	 *
    524       1.8   thorpej 	 * XXX REVISIT WHEN WE DO FCSE!
    525       1.8   thorpej 	 */
    526       1.8   thorpej 	if (__predict_false(map->_dm_proc != NULL && map->_dm_proc != curproc))
    527       1.8   thorpej 		return;
    528       1.8   thorpej 
    529  1.11.4.1        he 	switch (map->_dm_buftype) {
    530  1.11.4.1        he 	case ARM32_BUFTYPE_LINEAR:
    531  1.11.4.1        he 		_bus_dmamap_sync_linear(t, map, offset, len, ops);
    532  1.11.4.1        he 		break;
    533  1.11.4.1        he 
    534  1.11.4.1        he 	case ARM32_BUFTYPE_MBUF:
    535  1.11.4.1        he 		_bus_dmamap_sync_mbuf(t, map, offset, len, ops);
    536  1.11.4.1        he 		break;
    537  1.11.4.1        he 
    538  1.11.4.1        he 	case ARM32_BUFTYPE_UIO:
    539  1.11.4.1        he 		_bus_dmamap_sync_uio(t, map, offset, len, ops);
    540  1.11.4.1        he 		break;
    541  1.11.4.1        he 
    542  1.11.4.1        he 	case ARM32_BUFTYPE_RAW:
    543  1.11.4.1        he 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
    544  1.11.4.1        he 		break;
    545  1.11.4.1        he 
    546  1.11.4.1        he 	case ARM32_BUFTYPE_INVALID:
    547  1.11.4.1        he 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
    548  1.11.4.1        he 		break;
    549  1.11.4.1        he 
    550  1.11.4.1        he 	default:
    551  1.11.4.1        he 		printf("unknown buffer type %d\n", map->_dm_buftype);
    552  1.11.4.1        he 		panic("_bus_dmamap_sync");
    553       1.8   thorpej 	}
    554       1.1     chris 
    555       1.8   thorpej 	/* Drain the write buffer. */
    556       1.8   thorpej 	cpu_drain_writebuf();
    557       1.1     chris }
    558       1.1     chris 
    559       1.1     chris /*
    560       1.1     chris  * Common function for DMA-safe memory allocation.  May be called
    561       1.1     chris  * by bus-specific DMA memory allocation functions.
    562       1.1     chris  */
    563       1.1     chris 
    564      1.11   thorpej extern paddr_t physical_start;
    565      1.11   thorpej extern paddr_t physical_freestart;
    566      1.11   thorpej extern paddr_t physical_freeend;
    567      1.11   thorpej extern paddr_t physical_end;
    568       1.1     chris 
    569       1.1     chris int
    570       1.7   thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    571       1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    572       1.7   thorpej     int flags)
    573       1.1     chris {
    574       1.1     chris 	int error;
    575       1.1     chris #ifdef DEBUG_DMA
    576       1.1     chris 	printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x\n",
    577       1.1     chris 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags);
    578       1.1     chris #endif	/* DEBUG_DMA */
    579       1.1     chris 	error =  (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    580       1.1     chris 	    segs, nsegs, rsegs, flags, trunc_page(physical_start), trunc_page(physical_end)));
    581       1.1     chris #ifdef DEBUG_DMA
    582       1.1     chris 	printf("dmamem_alloc: =%d\n", error);
    583       1.1     chris #endif	/* DEBUG_DMA */
    584       1.1     chris 	return(error);
    585       1.1     chris }
    586       1.1     chris 
    587       1.1     chris /*
    588       1.1     chris  * Common function for freeing DMA-safe memory.  May be called by
    589       1.1     chris  * bus-specific DMA memory free functions.
    590       1.1     chris  */
    591       1.1     chris void
    592       1.7   thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    593       1.1     chris {
    594       1.1     chris 	struct vm_page *m;
    595       1.1     chris 	bus_addr_t addr;
    596       1.1     chris 	struct pglist mlist;
    597       1.1     chris 	int curseg;
    598       1.1     chris 
    599       1.1     chris #ifdef DEBUG_DMA
    600       1.1     chris 	printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
    601       1.1     chris #endif	/* DEBUG_DMA */
    602       1.1     chris 
    603       1.1     chris 	/*
    604       1.1     chris 	 * Build a list of pages to free back to the VM system.
    605       1.1     chris 	 */
    606       1.1     chris 	TAILQ_INIT(&mlist);
    607       1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
    608       1.1     chris 		for (addr = segs[curseg].ds_addr;
    609       1.1     chris 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    610       1.1     chris 		    addr += PAGE_SIZE) {
    611       1.1     chris 			m = PHYS_TO_VM_PAGE(addr);
    612       1.1     chris 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    613       1.1     chris 		}
    614       1.1     chris 	}
    615       1.1     chris 	uvm_pglistfree(&mlist);
    616       1.1     chris }
    617       1.1     chris 
    618       1.1     chris /*
    619       1.1     chris  * Common function for mapping DMA-safe memory.  May be called by
    620       1.1     chris  * bus-specific DMA memory map functions.
    621       1.1     chris  */
    622       1.1     chris int
    623       1.7   thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    624       1.7   thorpej     size_t size, caddr_t *kvap, int flags)
    625       1.1     chris {
    626      1.11   thorpej 	vaddr_t va;
    627       1.1     chris 	bus_addr_t addr;
    628       1.1     chris 	int curseg;
    629       1.1     chris 	pt_entry_t *ptep/*, pte*/;
    630       1.1     chris 
    631       1.1     chris #ifdef DEBUG_DMA
    632       1.3  rearnsha 	printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
    633       1.3  rearnsha 	    segs, nsegs, (unsigned long)size, flags);
    634       1.1     chris #endif	/* DEBUG_DMA */
    635       1.1     chris 
    636       1.1     chris 	size = round_page(size);
    637       1.1     chris 	va = uvm_km_valloc(kernel_map, size);
    638       1.1     chris 
    639       1.1     chris 	if (va == 0)
    640       1.1     chris 		return (ENOMEM);
    641       1.1     chris 
    642       1.1     chris 	*kvap = (caddr_t)va;
    643       1.1     chris 
    644       1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
    645       1.1     chris 		for (addr = segs[curseg].ds_addr;
    646       1.1     chris 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    647       1.1     chris 		    addr += NBPG, va += NBPG, size -= NBPG) {
    648       1.1     chris #ifdef DEBUG_DMA
    649       1.1     chris 			printf("wiring p%lx to v%lx", addr, va);
    650       1.1     chris #endif	/* DEBUG_DMA */
    651       1.1     chris 			if (size == 0)
    652       1.1     chris 				panic("_bus_dmamem_map: size botch");
    653       1.1     chris 			pmap_enter(pmap_kernel(), va, addr,
    654       1.1     chris 			    VM_PROT_READ | VM_PROT_WRITE,
    655       1.1     chris 			    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
    656       1.1     chris 			/*
    657       1.1     chris 			 * If the memory must remain coherent with the
    658       1.1     chris 			 * cache then we must make the memory uncacheable
    659       1.1     chris 			 * in order to maintain virtual cache coherency.
    660       1.1     chris 			 * We must also guarentee the cache does not already
    661       1.1     chris 			 * contain the virtal addresses we are making
    662       1.1     chris 			 * uncacheable.
    663       1.1     chris 			 */
    664       1.1     chris 			if (flags & BUS_DMA_COHERENT) {
    665       1.6   thorpej 				cpu_dcache_wbinv_range(va, NBPG);
    666       1.1     chris 				cpu_drain_writebuf();
    667       1.1     chris 				ptep = vtopte(va);
    668      1.10   thorpej 				*ptep &= ~(L2_B | L2_C);
    669       1.1     chris 				tlb_flush();
    670       1.1     chris 			}
    671       1.1     chris #ifdef DEBUG_DMA
    672       1.1     chris 			ptep = vtopte(va);
    673       1.1     chris 			printf(" pte=v%p *pte=%x\n", ptep, *ptep);
    674       1.1     chris #endif	/* DEBUG_DMA */
    675       1.1     chris 		}
    676       1.1     chris 	}
    677       1.2     chris 	pmap_update(pmap_kernel());
    678       1.1     chris #ifdef DEBUG_DMA
    679       1.1     chris 	printf("dmamem_map: =%p\n", *kvap);
    680       1.1     chris #endif	/* DEBUG_DMA */
    681       1.1     chris 	return (0);
    682       1.1     chris }
    683       1.1     chris 
    684       1.1     chris /*
    685       1.1     chris  * Common function for unmapping DMA-safe memory.  May be called by
    686       1.1     chris  * bus-specific DMA memory unmapping functions.
    687       1.1     chris  */
    688       1.1     chris void
    689       1.7   thorpej _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    690       1.1     chris {
    691       1.1     chris 
    692       1.1     chris #ifdef DEBUG_DMA
    693       1.3  rearnsha 	printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
    694       1.3  rearnsha 	    (unsigned long)size);
    695       1.1     chris #endif	/* DEBUG_DMA */
    696       1.1     chris #ifdef DIAGNOSTIC
    697       1.1     chris 	if ((u_long)kva & PGOFSET)
    698       1.1     chris 		panic("_bus_dmamem_unmap");
    699       1.1     chris #endif	/* DIAGNOSTIC */
    700       1.1     chris 
    701       1.1     chris 	size = round_page(size);
    702      1.11   thorpej 	uvm_km_free(kernel_map, (vaddr_t)kva, size);
    703       1.1     chris }
    704       1.1     chris 
    705       1.1     chris /*
    706       1.1     chris  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    707       1.1     chris  * bus-specific DMA mmap(2)'ing functions.
    708       1.1     chris  */
    709       1.1     chris paddr_t
    710       1.7   thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    711       1.7   thorpej     off_t off, int prot, int flags)
    712       1.1     chris {
    713       1.1     chris 	int i;
    714       1.1     chris 
    715       1.1     chris 	for (i = 0; i < nsegs; i++) {
    716       1.1     chris #ifdef DIAGNOSTIC
    717       1.1     chris 		if (off & PGOFSET)
    718       1.1     chris 			panic("_bus_dmamem_mmap: offset unaligned");
    719       1.1     chris 		if (segs[i].ds_addr & PGOFSET)
    720       1.1     chris 			panic("_bus_dmamem_mmap: segment unaligned");
    721       1.1     chris 		if (segs[i].ds_len & PGOFSET)
    722       1.1     chris 			panic("_bus_dmamem_mmap: segment size not multiple"
    723       1.1     chris 			    " of page size");
    724       1.1     chris #endif	/* DIAGNOSTIC */
    725       1.1     chris 		if (off >= segs[i].ds_len) {
    726       1.1     chris 			off -= segs[i].ds_len;
    727       1.1     chris 			continue;
    728       1.1     chris 		}
    729       1.1     chris 
    730       1.9   thorpej 		return (arm_btop((u_long)segs[i].ds_addr + off));
    731       1.1     chris 	}
    732       1.1     chris 
    733       1.1     chris 	/* Page not found. */
    734       1.1     chris 	return (-1);
    735       1.1     chris }
    736       1.1     chris 
    737       1.1     chris /**********************************************************************
    738       1.1     chris  * DMA utility functions
    739       1.1     chris  **********************************************************************/
    740       1.1     chris 
    741       1.1     chris /*
    742       1.1     chris  * Utility function to load a linear buffer.  lastaddrp holds state
    743       1.1     chris  * between invocations (for multiple-buffer loads).  segp contains
    744       1.1     chris  * the starting segment on entrace, and the ending segment on exit.
    745       1.1     chris  * first indicates if this is the first invocation of this function.
    746       1.1     chris  */
    747       1.1     chris int
    748       1.7   thorpej _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    749      1.11   thorpej     bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
    750       1.7   thorpej     int *segp, int first)
    751       1.1     chris {
    752       1.1     chris 	bus_size_t sgsize;
    753       1.1     chris 	bus_addr_t curaddr, lastaddr, baddr, bmask;
    754      1.11   thorpej 	vaddr_t vaddr = (vaddr_t)buf;
    755       1.1     chris 	int seg;
    756       1.1     chris 	pmap_t pmap;
    757       1.1     chris 
    758       1.1     chris #ifdef DEBUG_DMA
    759       1.1     chris 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
    760       1.1     chris 	    buf, buflen, flags, first);
    761       1.1     chris #endif	/* DEBUG_DMA */
    762       1.1     chris 
    763       1.1     chris 	if (p != NULL)
    764       1.1     chris 		pmap = p->p_vmspace->vm_map.pmap;
    765       1.1     chris 	else
    766       1.1     chris 		pmap = pmap_kernel();
    767       1.1     chris 
    768       1.1     chris 	lastaddr = *lastaddrp;
    769       1.1     chris 	bmask  = ~(map->_dm_boundary - 1);
    770       1.1     chris 
    771       1.1     chris 	for (seg = *segp; buflen > 0; ) {
    772       1.1     chris 		/*
    773       1.1     chris 		 * Get the physical address for this segment.
    774       1.1     chris 		 */
    775       1.1     chris 		(void) pmap_extract(pmap, (vaddr_t)vaddr, &curaddr);
    776       1.1     chris 
    777       1.1     chris 		/*
    778       1.1     chris 		 * Make sure we're in an allowed DMA range.
    779       1.1     chris 		 */
    780       1.1     chris 		if (t->_ranges != NULL &&
    781       1.1     chris 		    _bus_dma_inrange(t->_ranges, t->_nranges, curaddr) == 0)
    782       1.1     chris 			return (EINVAL);
    783       1.1     chris 
    784       1.1     chris 		/*
    785       1.1     chris 		 * Compute the segment size, and adjust counts.
    786       1.1     chris 		 */
    787       1.1     chris 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    788       1.1     chris 		if (buflen < sgsize)
    789       1.1     chris 			sgsize = buflen;
    790       1.1     chris 
    791       1.1     chris 		/*
    792       1.1     chris 		 * Make sure we don't cross any boundaries.
    793       1.1     chris 		 */
    794       1.1     chris 		if (map->_dm_boundary > 0) {
    795       1.1     chris 			baddr = (curaddr + map->_dm_boundary) & bmask;
    796       1.1     chris 			if (sgsize > (baddr - curaddr))
    797       1.1     chris 				sgsize = (baddr - curaddr);
    798       1.1     chris 		}
    799       1.1     chris 
    800       1.1     chris 		/*
    801       1.1     chris 		 * Insert chunk into a segment, coalescing with
    802       1.1     chris 		 * previous segment if possible.
    803       1.1     chris 		 */
    804       1.1     chris 		if (first) {
    805       1.1     chris 			map->dm_segs[seg].ds_addr = curaddr;
    806       1.1     chris 			map->dm_segs[seg].ds_len = sgsize;
    807       1.1     chris 			first = 0;
    808       1.1     chris 		} else {
    809       1.1     chris 			if (curaddr == lastaddr &&
    810       1.1     chris 			    (map->dm_segs[seg].ds_len + sgsize) <=
    811       1.1     chris 			     map->_dm_maxsegsz &&
    812       1.1     chris 			    (map->_dm_boundary == 0 ||
    813       1.1     chris 			     (map->dm_segs[seg].ds_addr & bmask) ==
    814       1.1     chris 			     (curaddr & bmask)))
    815       1.1     chris 				map->dm_segs[seg].ds_len += sgsize;
    816       1.1     chris 			else {
    817       1.1     chris 				if (++seg >= map->_dm_segcnt)
    818       1.1     chris 					break;
    819       1.1     chris 				map->dm_segs[seg].ds_addr = curaddr;
    820       1.1     chris 				map->dm_segs[seg].ds_len = sgsize;
    821       1.1     chris 			}
    822       1.1     chris 		}
    823       1.1     chris 
    824       1.1     chris 		lastaddr = curaddr + sgsize;
    825       1.1     chris 		vaddr += sgsize;
    826       1.1     chris 		buflen -= sgsize;
    827       1.1     chris 	}
    828       1.1     chris 
    829       1.1     chris 	*segp = seg;
    830       1.1     chris 	*lastaddrp = lastaddr;
    831       1.1     chris 
    832       1.1     chris 	/*
    833       1.1     chris 	 * Did we fit?
    834       1.1     chris 	 */
    835       1.1     chris 	if (buflen != 0)
    836       1.1     chris 		return (EFBIG);		/* XXX better return value here? */
    837       1.1     chris 	return (0);
    838       1.1     chris }
    839       1.1     chris 
    840       1.1     chris /*
    841       1.1     chris  * Check to see if the specified page is in an allowed DMA range.
    842       1.1     chris  */
    843       1.1     chris int
    844       1.7   thorpej _bus_dma_inrange(bus_dma_segment_t *ranges, int nranges, bus_addr_t curaddr)
    845       1.1     chris {
    846       1.1     chris 	bus_dma_segment_t *ds;
    847       1.1     chris 	int i;
    848       1.1     chris 
    849       1.1     chris 	for (i = 0, ds = ranges; i < nranges; i++, ds++) {
    850       1.1     chris 		if (curaddr >= ds->ds_addr &&
    851       1.1     chris 		    round_page(curaddr) <= (ds->ds_addr + ds->ds_len))
    852       1.1     chris 			return (1);
    853       1.1     chris 	}
    854       1.1     chris 
    855       1.1     chris 	return (0);
    856       1.1     chris }
    857       1.1     chris 
    858       1.1     chris /*
    859       1.1     chris  * Allocate physical memory from the given physical address range.
    860       1.1     chris  * Called by DMA-safe memory allocation methods.
    861       1.1     chris  */
    862       1.1     chris int
    863       1.7   thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    864       1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    865      1.11   thorpej     int flags, paddr_t low, paddr_t high)
    866       1.1     chris {
    867      1.11   thorpej 	paddr_t curaddr, lastaddr;
    868       1.1     chris 	struct vm_page *m;
    869       1.1     chris 	struct pglist mlist;
    870       1.1     chris 	int curseg, error;
    871       1.1     chris 
    872       1.1     chris #ifdef DEBUG_DMA
    873       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",
    874       1.1     chris 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
    875       1.1     chris #endif	/* DEBUG_DMA */
    876       1.1     chris 
    877       1.1     chris 	/* Always round the size. */
    878       1.1     chris 	size = round_page(size);
    879       1.1     chris 
    880       1.1     chris 	/*
    881       1.1     chris 	 * Allocate pages from the VM system.
    882       1.1     chris 	 */
    883       1.1     chris 	TAILQ_INIT(&mlist);
    884       1.1     chris 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
    885       1.1     chris 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    886       1.1     chris 	if (error)
    887       1.1     chris 		return (error);
    888       1.1     chris 
    889       1.1     chris 	/*
    890       1.1     chris 	 * Compute the location, size, and number of segments actually
    891       1.1     chris 	 * returned by the VM code.
    892       1.1     chris 	 */
    893       1.1     chris 	m = mlist.tqh_first;
    894       1.1     chris 	curseg = 0;
    895       1.1     chris 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    896       1.1     chris 	segs[curseg].ds_len = PAGE_SIZE;
    897       1.1     chris #ifdef DEBUG_DMA
    898       1.1     chris 		printf("alloc: page %lx\n", lastaddr);
    899       1.1     chris #endif	/* DEBUG_DMA */
    900       1.1     chris 	m = m->pageq.tqe_next;
    901       1.1     chris 
    902       1.1     chris 	for (; m != NULL; m = m->pageq.tqe_next) {
    903       1.1     chris 		curaddr = VM_PAGE_TO_PHYS(m);
    904       1.1     chris #ifdef DIAGNOSTIC
    905       1.1     chris 		if (curaddr < low || curaddr >= high) {
    906       1.1     chris 			printf("uvm_pglistalloc returned non-sensical"
    907       1.1     chris 			    " address 0x%lx\n", curaddr);
    908       1.1     chris 			panic("_bus_dmamem_alloc_range");
    909       1.1     chris 		}
    910       1.1     chris #endif	/* DIAGNOSTIC */
    911       1.1     chris #ifdef DEBUG_DMA
    912       1.1     chris 		printf("alloc: page %lx\n", curaddr);
    913       1.1     chris #endif	/* DEBUG_DMA */
    914       1.1     chris 		if (curaddr == (lastaddr + PAGE_SIZE))
    915       1.1     chris 			segs[curseg].ds_len += PAGE_SIZE;
    916       1.1     chris 		else {
    917       1.1     chris 			curseg++;
    918       1.1     chris 			segs[curseg].ds_addr = curaddr;
    919       1.1     chris 			segs[curseg].ds_len = PAGE_SIZE;
    920       1.1     chris 		}
    921       1.1     chris 		lastaddr = curaddr;
    922       1.1     chris 	}
    923       1.1     chris 
    924       1.1     chris 	*rsegs = curseg + 1;
    925       1.1     chris 
    926       1.1     chris 	return (0);
    927       1.1     chris }
    928