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