Home | History | Annotate | Line # | Download | only in arm32
bus_dma.c revision 1.57
      1  1.57      matt /*	$NetBSD: bus_dma.c,v 1.57 2012/09/11 17:54:12 matt Exp $	*/
      2   1.1     chris 
      3   1.1     chris /*-
      4   1.1     chris  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1     chris  * All rights reserved.
      6   1.1     chris  *
      7   1.1     chris  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     chris  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1     chris  * NASA Ames Research Center.
     10   1.1     chris  *
     11   1.1     chris  * Redistribution and use in source and binary forms, with or without
     12   1.1     chris  * modification, are permitted provided that the following conditions
     13   1.1     chris  * are met:
     14   1.1     chris  * 1. Redistributions of source code must retain the above copyright
     15   1.1     chris  *    notice, this list of conditions and the following disclaimer.
     16   1.1     chris  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     chris  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     chris  *    documentation and/or other materials provided with the distribution.
     19   1.1     chris  *
     20   1.1     chris  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1     chris  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1     chris  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1     chris  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1     chris  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1     chris  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1     chris  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1     chris  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1     chris  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1     chris  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1     chris  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1     chris  */
     32  1.33     lukem 
     33  1.35  rearnsha #define _ARM32_BUS_DMA_PRIVATE
     34  1.35  rearnsha 
     35  1.33     lukem #include <sys/cdefs.h>
     36  1.57      matt __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.57 2012/09/11 17:54:12 matt Exp $");
     37   1.1     chris 
     38   1.1     chris #include <sys/param.h>
     39   1.1     chris #include <sys/systm.h>
     40   1.1     chris #include <sys/kernel.h>
     41   1.1     chris #include <sys/proc.h>
     42   1.1     chris #include <sys/buf.h>
     43   1.1     chris #include <sys/reboot.h>
     44   1.1     chris #include <sys/conf.h>
     45   1.1     chris #include <sys/file.h>
     46   1.1     chris #include <sys/malloc.h>
     47   1.1     chris #include <sys/mbuf.h>
     48   1.1     chris #include <sys/vnode.h>
     49   1.1     chris #include <sys/device.h>
     50   1.1     chris 
     51  1.53  uebayasi #include <uvm/uvm.h>
     52   1.1     chris 
     53  1.54    dyoung #include <sys/bus.h>
     54   1.1     chris #include <machine/cpu.h>
     55   1.4   thorpej 
     56   1.4   thorpej #include <arm/cpufunc.h>
     57   1.1     chris 
     58   1.7   thorpej int	_bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
     59  1.48      yamt 	    bus_size_t, struct vmspace *, int);
     60  1.15   thorpej struct arm32_dma_range *_bus_dma_inrange(struct arm32_dma_range *,
     61  1.15   thorpej 	    int, bus_addr_t);
     62   1.1     chris 
     63   1.1     chris /*
     64  1.19    briggs  * Check to see if the specified page is in an allowed DMA range.
     65  1.19    briggs  */
     66  1.47     perry inline struct arm32_dma_range *
     67  1.19    briggs _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
     68  1.19    briggs     bus_addr_t curaddr)
     69  1.19    briggs {
     70  1.19    briggs 	struct arm32_dma_range *dr;
     71  1.19    briggs 	int i;
     72  1.19    briggs 
     73  1.19    briggs 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
     74  1.19    briggs 		if (curaddr >= dr->dr_sysbase &&
     75  1.19    briggs 		    round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
     76  1.19    briggs 			return (dr);
     77  1.19    briggs 	}
     78  1.19    briggs 
     79  1.19    briggs 	return (NULL);
     80  1.19    briggs }
     81  1.19    briggs 
     82  1.19    briggs /*
     83  1.41   thorpej  * Common function to load the specified physical address into the
     84  1.41   thorpej  * DMA map, coalescing segments and boundary checking as necessary.
     85  1.41   thorpej  */
     86  1.41   thorpej static int
     87  1.41   thorpej _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
     88  1.41   thorpej     bus_addr_t paddr, bus_size_t size)
     89  1.41   thorpej {
     90  1.41   thorpej 	bus_dma_segment_t * const segs = map->dm_segs;
     91  1.41   thorpej 	int nseg = map->dm_nsegs;
     92  1.41   thorpej 	bus_addr_t lastaddr = 0xdead;	/* XXX gcc */
     93  1.41   thorpej 	bus_addr_t bmask = ~(map->_dm_boundary - 1);
     94  1.41   thorpej 	bus_addr_t curaddr;
     95  1.41   thorpej 	bus_size_t sgsize;
     96  1.41   thorpej 
     97  1.41   thorpej 	if (nseg > 0)
     98  1.41   thorpej 		lastaddr = segs[nseg-1].ds_addr + segs[nseg-1].ds_len;
     99  1.41   thorpej  again:
    100  1.41   thorpej 	sgsize = size;
    101  1.41   thorpej 
    102  1.41   thorpej 	/* Make sure we're in an allowed DMA range. */
    103  1.41   thorpej 	if (t->_ranges != NULL) {
    104  1.41   thorpej 		/* XXX cache last result? */
    105  1.41   thorpej 		const struct arm32_dma_range * const dr =
    106  1.41   thorpej 		    _bus_dma_inrange(t->_ranges, t->_nranges, paddr);
    107  1.41   thorpej 		if (dr == NULL)
    108  1.41   thorpej 			return (EINVAL);
    109  1.41   thorpej 
    110  1.41   thorpej 		/*
    111  1.41   thorpej 		 * In a valid DMA range.  Translate the physical
    112  1.41   thorpej 		 * memory address to an address in the DMA window.
    113  1.41   thorpej 		 */
    114  1.41   thorpej 		curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
    115  1.41   thorpej 	} else
    116  1.41   thorpej 		curaddr = paddr;
    117  1.41   thorpej 
    118  1.41   thorpej 	/*
    119  1.41   thorpej 	 * Make sure we don't cross any boundaries.
    120  1.41   thorpej 	 */
    121  1.41   thorpej 	if (map->_dm_boundary > 0) {
    122  1.41   thorpej 		bus_addr_t baddr;	/* next boundary address */
    123  1.41   thorpej 
    124  1.41   thorpej 		baddr = (curaddr + map->_dm_boundary) & bmask;
    125  1.41   thorpej 		if (sgsize > (baddr - curaddr))
    126  1.41   thorpej 			sgsize = (baddr - curaddr);
    127  1.41   thorpej 	}
    128  1.41   thorpej 
    129  1.41   thorpej 	/*
    130  1.41   thorpej 	 * Insert chunk into a segment, coalescing with the
    131  1.41   thorpej 	 * previous segment if possible.
    132  1.41   thorpej 	 */
    133  1.41   thorpej 	if (nseg > 0 && curaddr == lastaddr &&
    134  1.43      matt 	    segs[nseg-1].ds_len + sgsize <= map->dm_maxsegsz &&
    135  1.41   thorpej 	    (map->_dm_boundary == 0 ||
    136  1.41   thorpej 	     (segs[nseg-1].ds_addr & bmask) == (curaddr & bmask))) {
    137  1.41   thorpej 	     	/* coalesce */
    138  1.41   thorpej 		segs[nseg-1].ds_len += sgsize;
    139  1.41   thorpej 	} else if (nseg >= map->_dm_segcnt) {
    140  1.41   thorpej 		return (EFBIG);
    141  1.41   thorpej 	} else {
    142  1.41   thorpej 		/* new segment */
    143  1.41   thorpej 		segs[nseg].ds_addr = curaddr;
    144  1.41   thorpej 		segs[nseg].ds_len = sgsize;
    145  1.41   thorpej 		nseg++;
    146  1.41   thorpej 	}
    147  1.41   thorpej 
    148  1.41   thorpej 	lastaddr = curaddr + sgsize;
    149  1.41   thorpej 
    150  1.41   thorpej 	paddr += sgsize;
    151  1.41   thorpej 	size -= sgsize;
    152  1.41   thorpej 	if (size > 0)
    153  1.41   thorpej 		goto again;
    154  1.41   thorpej 
    155  1.41   thorpej 	map->dm_nsegs = nseg;
    156  1.41   thorpej 	return (0);
    157  1.41   thorpej }
    158  1.41   thorpej 
    159  1.41   thorpej /*
    160   1.1     chris  * Common function for DMA map creation.  May be called by bus-specific
    161   1.1     chris  * DMA map creation functions.
    162   1.1     chris  */
    163   1.1     chris int
    164   1.7   thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    165   1.7   thorpej     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    166   1.1     chris {
    167   1.1     chris 	struct arm32_bus_dmamap *map;
    168   1.1     chris 	void *mapstore;
    169   1.1     chris 	size_t mapsize;
    170   1.1     chris 
    171   1.1     chris #ifdef DEBUG_DMA
    172   1.1     chris 	printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
    173   1.1     chris 	    t, size, nsegments, maxsegsz, boundary, flags);
    174   1.1     chris #endif	/* DEBUG_DMA */
    175   1.1     chris 
    176   1.1     chris 	/*
    177   1.1     chris 	 * Allocate and initialize the DMA map.  The end of the map
    178   1.1     chris 	 * is a variable-sized array of segments, so we allocate enough
    179   1.1     chris 	 * room for them in one shot.
    180   1.1     chris 	 *
    181   1.1     chris 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
    182   1.1     chris 	 * of ALLOCNOW notifies others that we've reserved these resources,
    183   1.1     chris 	 * and they are not to be freed.
    184   1.1     chris 	 *
    185   1.1     chris 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
    186   1.1     chris 	 * the (nsegments - 1).
    187   1.1     chris 	 */
    188   1.1     chris 	mapsize = sizeof(struct arm32_bus_dmamap) +
    189   1.1     chris 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
    190   1.1     chris 	if ((mapstore = malloc(mapsize, M_DMAMAP,
    191   1.1     chris 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
    192   1.1     chris 		return (ENOMEM);
    193   1.1     chris 
    194   1.1     chris 	memset(mapstore, 0, mapsize);
    195   1.1     chris 	map = (struct arm32_bus_dmamap *)mapstore;
    196   1.1     chris 	map->_dm_size = size;
    197   1.1     chris 	map->_dm_segcnt = nsegments;
    198  1.43      matt 	map->_dm_maxmaxsegsz = maxsegsz;
    199   1.1     chris 	map->_dm_boundary = boundary;
    200   1.1     chris 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    201  1.14   thorpej 	map->_dm_origbuf = NULL;
    202  1.14   thorpej 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    203  1.48      yamt 	map->_dm_vmspace = vmspace_kernel();
    204  1.43      matt 	map->dm_maxsegsz = maxsegsz;
    205   1.1     chris 	map->dm_mapsize = 0;		/* no valid mappings */
    206   1.1     chris 	map->dm_nsegs = 0;
    207   1.1     chris 
    208   1.1     chris 	*dmamp = map;
    209   1.1     chris #ifdef DEBUG_DMA
    210   1.1     chris 	printf("dmamap_create:map=%p\n", map);
    211   1.1     chris #endif	/* DEBUG_DMA */
    212   1.1     chris 	return (0);
    213   1.1     chris }
    214   1.1     chris 
    215   1.1     chris /*
    216   1.1     chris  * Common function for DMA map destruction.  May be called by bus-specific
    217   1.1     chris  * DMA map destruction functions.
    218   1.1     chris  */
    219   1.1     chris void
    220   1.7   thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    221   1.1     chris {
    222   1.1     chris 
    223   1.1     chris #ifdef DEBUG_DMA
    224   1.1     chris 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
    225   1.1     chris #endif	/* DEBUG_DMA */
    226  1.13    briggs 
    227  1.13    briggs 	/*
    228  1.13    briggs 	 * Explicit unload.
    229  1.13    briggs 	 */
    230  1.43      matt 	map->dm_maxsegsz = map->_dm_maxmaxsegsz;
    231  1.13    briggs 	map->dm_mapsize = 0;
    232  1.13    briggs 	map->dm_nsegs = 0;
    233  1.14   thorpej 	map->_dm_origbuf = NULL;
    234  1.14   thorpej 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    235  1.48      yamt 	map->_dm_vmspace = NULL;
    236  1.13    briggs 
    237  1.25     chris 	free(map, M_DMAMAP);
    238   1.1     chris }
    239   1.1     chris 
    240   1.1     chris /*
    241   1.1     chris  * Common function for loading a DMA map with a linear buffer.  May
    242   1.1     chris  * be called by bus-specific DMA map load functions.
    243   1.1     chris  */
    244   1.1     chris int
    245   1.7   thorpej _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    246   1.7   thorpej     bus_size_t buflen, struct proc *p, int flags)
    247   1.1     chris {
    248  1.41   thorpej 	int error;
    249  1.48      yamt 	struct vmspace *vm;
    250   1.1     chris 
    251   1.1     chris #ifdef DEBUG_DMA
    252   1.1     chris 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
    253   1.1     chris 	    t, map, buf, buflen, p, flags);
    254   1.1     chris #endif	/* DEBUG_DMA */
    255   1.1     chris 
    256   1.1     chris 	/*
    257   1.1     chris 	 * Make sure that on error condition we return "no valid mappings".
    258   1.1     chris 	 */
    259   1.1     chris 	map->dm_mapsize = 0;
    260   1.1     chris 	map->dm_nsegs = 0;
    261  1.43      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    262   1.1     chris 
    263   1.1     chris 	if (buflen > map->_dm_size)
    264   1.1     chris 		return (EINVAL);
    265   1.1     chris 
    266  1.48      yamt 	if (p != NULL) {
    267  1.48      yamt 		vm = p->p_vmspace;
    268  1.48      yamt 	} else {
    269  1.48      yamt 		vm = vmspace_kernel();
    270  1.48      yamt 	}
    271  1.48      yamt 
    272  1.17   thorpej 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    273  1.17   thorpej 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
    274  1.17   thorpej 
    275  1.48      yamt 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
    276   1.1     chris 	if (error == 0) {
    277   1.1     chris 		map->dm_mapsize = buflen;
    278  1.14   thorpej 		map->_dm_origbuf = buf;
    279  1.14   thorpej 		map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
    280  1.48      yamt 		map->_dm_vmspace = vm;
    281   1.1     chris 	}
    282   1.1     chris #ifdef DEBUG_DMA
    283   1.1     chris 	printf("dmamap_load: error=%d\n", error);
    284   1.1     chris #endif	/* DEBUG_DMA */
    285   1.1     chris 	return (error);
    286   1.1     chris }
    287   1.1     chris 
    288   1.1     chris /*
    289   1.1     chris  * Like _bus_dmamap_load(), but for mbufs.
    290   1.1     chris  */
    291   1.1     chris int
    292   1.7   thorpej _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    293   1.7   thorpej     int flags)
    294   1.1     chris {
    295  1.41   thorpej 	int error;
    296   1.1     chris 	struct mbuf *m;
    297   1.1     chris 
    298   1.1     chris #ifdef DEBUG_DMA
    299   1.1     chris 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
    300   1.1     chris 	    t, map, m0, flags);
    301   1.1     chris #endif	/* DEBUG_DMA */
    302   1.1     chris 
    303   1.1     chris 	/*
    304   1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    305   1.1     chris 	 */
    306   1.1     chris 	map->dm_mapsize = 0;
    307   1.1     chris 	map->dm_nsegs = 0;
    308  1.43      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    309   1.1     chris 
    310   1.1     chris #ifdef DIAGNOSTIC
    311   1.1     chris 	if ((m0->m_flags & M_PKTHDR) == 0)
    312   1.1     chris 		panic("_bus_dmamap_load_mbuf: no packet header");
    313   1.1     chris #endif	/* DIAGNOSTIC */
    314   1.1     chris 
    315   1.1     chris 	if (m0->m_pkthdr.len > map->_dm_size)
    316   1.1     chris 		return (EINVAL);
    317   1.1     chris 
    318  1.28   thorpej 	/*
    319  1.28   thorpej 	 * Mbuf chains should almost never have coherent (i.e.
    320  1.28   thorpej 	 * un-cached) mappings, so clear that flag now.
    321  1.28   thorpej 	 */
    322  1.28   thorpej 	map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
    323  1.17   thorpej 
    324   1.1     chris 	error = 0;
    325   1.1     chris 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    326  1.41   thorpej 		int offset;
    327  1.41   thorpej 		int remainbytes;
    328  1.41   thorpej 		const struct vm_page * const *pgs;
    329  1.41   thorpej 		paddr_t paddr;
    330  1.41   thorpej 		int size;
    331  1.41   thorpej 
    332  1.28   thorpej 		if (m->m_len == 0)
    333  1.28   thorpej 			continue;
    334  1.57      matt 		/*
    335  1.57      matt 		 * Don't allow reads in read-only mbufs.
    336  1.57      matt 		 */
    337  1.57      matt 		if (M_ROMAP(m) && (flags & BUS_DMA_READ)) {
    338  1.57      matt 			error = EFAULT;
    339  1.57      matt 			break;
    340  1.57      matt 		}
    341  1.41   thorpej 		switch (m->m_flags & (M_EXT|M_CLUSTER|M_EXT_PAGES)) {
    342  1.28   thorpej 		case M_EXT|M_CLUSTER:
    343  1.28   thorpej 			/* XXX KDASSERT */
    344  1.28   thorpej 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
    345  1.41   thorpej 			paddr = m->m_ext.ext_paddr +
    346  1.28   thorpej 			    (m->m_data - m->m_ext.ext_buf);
    347  1.41   thorpej 			size = m->m_len;
    348  1.41   thorpej 			error = _bus_dmamap_load_paddr(t, map, paddr, size);
    349  1.41   thorpej 			break;
    350  1.41   thorpej 
    351  1.41   thorpej 		case M_EXT|M_EXT_PAGES:
    352  1.41   thorpej 			KASSERT(m->m_ext.ext_buf <= m->m_data);
    353  1.41   thorpej 			KASSERT(m->m_data <=
    354  1.41   thorpej 			    m->m_ext.ext_buf + m->m_ext.ext_size);
    355  1.41   thorpej 
    356  1.41   thorpej 			offset = (vaddr_t)m->m_data -
    357  1.41   thorpej 			    trunc_page((vaddr_t)m->m_ext.ext_buf);
    358  1.41   thorpej 			remainbytes = m->m_len;
    359  1.41   thorpej 
    360  1.41   thorpej 			/* skip uninteresting pages */
    361  1.41   thorpej 			pgs = (const struct vm_page * const *)
    362  1.41   thorpej 			    m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
    363  1.41   thorpej 
    364  1.41   thorpej 			offset &= PAGE_MASK;	/* offset in the first page */
    365  1.41   thorpej 
    366  1.41   thorpej 			/* load each page */
    367  1.41   thorpej 			while (remainbytes > 0) {
    368  1.41   thorpej 				const struct vm_page *pg;
    369  1.41   thorpej 
    370  1.41   thorpej 				size = MIN(remainbytes, PAGE_SIZE - offset);
    371  1.41   thorpej 
    372  1.41   thorpej 				pg = *pgs++;
    373  1.41   thorpej 				KASSERT(pg);
    374  1.41   thorpej 				paddr = VM_PAGE_TO_PHYS(pg) + offset;
    375  1.41   thorpej 
    376  1.41   thorpej 				error = _bus_dmamap_load_paddr(t, map,
    377  1.41   thorpej 				    paddr, size);
    378  1.41   thorpej 				if (error)
    379  1.28   thorpej 					break;
    380  1.41   thorpej 				offset = 0;
    381  1.41   thorpej 				remainbytes -= size;
    382  1.28   thorpej 			}
    383  1.28   thorpej 			break;
    384  1.28   thorpej 
    385  1.28   thorpej 		case 0:
    386  1.41   thorpej 			paddr = m->m_paddr + M_BUFOFFSET(m) +
    387  1.28   thorpej 			    (m->m_data - M_BUFADDR(m));
    388  1.41   thorpej 			size = m->m_len;
    389  1.41   thorpej 			error = _bus_dmamap_load_paddr(t, map, paddr, size);
    390  1.41   thorpej 			break;
    391  1.28   thorpej 
    392  1.28   thorpej 		default:
    393  1.28   thorpej 			error = _bus_dmamap_load_buffer(t, map, m->m_data,
    394  1.48      yamt 			    m->m_len, vmspace_kernel(), flags);
    395  1.28   thorpej 		}
    396   1.1     chris 	}
    397   1.1     chris 	if (error == 0) {
    398   1.1     chris 		map->dm_mapsize = m0->m_pkthdr.len;
    399  1.14   thorpej 		map->_dm_origbuf = m0;
    400  1.14   thorpej 		map->_dm_buftype = ARM32_BUFTYPE_MBUF;
    401  1.48      yamt 		map->_dm_vmspace = vmspace_kernel();	/* always kernel */
    402   1.1     chris 	}
    403   1.1     chris #ifdef DEBUG_DMA
    404   1.1     chris 	printf("dmamap_load_mbuf: error=%d\n", error);
    405   1.1     chris #endif	/* DEBUG_DMA */
    406   1.1     chris 	return (error);
    407   1.1     chris }
    408   1.1     chris 
    409   1.1     chris /*
    410   1.1     chris  * Like _bus_dmamap_load(), but for uios.
    411   1.1     chris  */
    412   1.1     chris int
    413   1.7   thorpej _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    414   1.7   thorpej     int flags)
    415   1.1     chris {
    416  1.41   thorpej 	int i, error;
    417   1.1     chris 	bus_size_t minlen, resid;
    418   1.1     chris 	struct iovec *iov;
    419  1.50  christos 	void *addr;
    420   1.1     chris 
    421   1.1     chris 	/*
    422   1.1     chris 	 * Make sure that on error condition we return "no valid mappings."
    423   1.1     chris 	 */
    424   1.1     chris 	map->dm_mapsize = 0;
    425   1.1     chris 	map->dm_nsegs = 0;
    426  1.43      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    427   1.1     chris 
    428   1.1     chris 	resid = uio->uio_resid;
    429   1.1     chris 	iov = uio->uio_iov;
    430   1.1     chris 
    431  1.17   thorpej 	/* _bus_dmamap_load_buffer() clears this if we're not... */
    432  1.17   thorpej 	map->_dm_flags |= ARM32_DMAMAP_COHERENT;
    433  1.17   thorpej 
    434   1.1     chris 	error = 0;
    435   1.1     chris 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    436   1.1     chris 		/*
    437   1.1     chris 		 * Now at the first iovec to load.  Load each iovec
    438   1.1     chris 		 * until we have exhausted the residual count.
    439   1.1     chris 		 */
    440   1.1     chris 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    441  1.50  christos 		addr = (void *)iov[i].iov_base;
    442   1.1     chris 
    443   1.1     chris 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
    444  1.48      yamt 		    uio->uio_vmspace, flags);
    445   1.1     chris 
    446   1.1     chris 		resid -= minlen;
    447   1.1     chris 	}
    448   1.1     chris 	if (error == 0) {
    449   1.1     chris 		map->dm_mapsize = uio->uio_resid;
    450  1.14   thorpej 		map->_dm_origbuf = uio;
    451  1.14   thorpej 		map->_dm_buftype = ARM32_BUFTYPE_UIO;
    452  1.48      yamt 		map->_dm_vmspace = uio->uio_vmspace;
    453   1.1     chris 	}
    454   1.1     chris 	return (error);
    455   1.1     chris }
    456   1.1     chris 
    457   1.1     chris /*
    458   1.1     chris  * Like _bus_dmamap_load(), but for raw memory allocated with
    459   1.1     chris  * bus_dmamem_alloc().
    460   1.1     chris  */
    461   1.1     chris int
    462   1.7   thorpej _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    463   1.7   thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    464   1.1     chris {
    465   1.1     chris 
    466   1.1     chris 	panic("_bus_dmamap_load_raw: not implemented");
    467   1.1     chris }
    468   1.1     chris 
    469   1.1     chris /*
    470   1.1     chris  * Common function for unloading a DMA map.  May be called by
    471   1.1     chris  * bus-specific DMA map unload functions.
    472   1.1     chris  */
    473   1.1     chris void
    474   1.7   thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    475   1.1     chris {
    476   1.1     chris 
    477   1.1     chris #ifdef DEBUG_DMA
    478   1.1     chris 	printf("dmamap_unload: t=%p map=%p\n", t, map);
    479   1.1     chris #endif	/* DEBUG_DMA */
    480   1.1     chris 
    481   1.1     chris 	/*
    482   1.1     chris 	 * No resources to free; just mark the mappings as
    483   1.1     chris 	 * invalid.
    484   1.1     chris 	 */
    485   1.1     chris 	map->dm_mapsize = 0;
    486   1.1     chris 	map->dm_nsegs = 0;
    487  1.14   thorpej 	map->_dm_origbuf = NULL;
    488  1.14   thorpej 	map->_dm_buftype = ARM32_BUFTYPE_INVALID;
    489  1.48      yamt 	map->_dm_vmspace = NULL;
    490   1.1     chris }
    491   1.1     chris 
    492  1.57      matt static void
    493  1.57      matt _bus_dmamap_sync_segment(vaddr_t va, paddr_t pa, vsize_t len, int ops, bool readonly_p)
    494  1.14   thorpej {
    495  1.57      matt 	KASSERT((va & PAGE_MASK) == (pa & PAGE_MASK));
    496  1.14   thorpej 
    497  1.14   thorpej 	switch (ops) {
    498  1.14   thorpej 	case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
    499  1.57      matt 		if (!readonly_p) {
    500  1.57      matt 			cpu_dcache_wbinv_range(va, len);
    501  1.57      matt 			cpu_sdcache_wbinv_range(va, pa, len);
    502  1.57      matt 			break;
    503  1.57      matt 		}
    504  1.57      matt 		/* FALLTHROUGH */
    505  1.14   thorpej 
    506  1.57      matt 	case BUS_DMASYNC_PREREAD: {
    507  1.57      matt 		vsize_t misalignment = va & arm_dcache_align_mask;
    508  1.57      matt 		if (misalignment) {
    509  1.57      matt 			va &= ~arm_dcache_align_mask;
    510  1.57      matt 			pa &= ~arm_dcache_align_mask;
    511  1.57      matt 			cpu_dcache_wbinv_range(va, arm_dcache_align);
    512  1.57      matt 			cpu_sdcache_wbinv_range(va, pa, arm_dcache_align);
    513  1.57      matt 			if (len <= arm_dcache_align - misalignment)
    514  1.57      matt 				break;
    515  1.57      matt 			len -= arm_dcache_align - misalignment;
    516  1.57      matt 			va += arm_dcache_align;
    517  1.57      matt 			pa += arm_dcache_align;
    518  1.57      matt 		}
    519  1.57      matt 		misalignment = len & arm_dcache_align_mask;
    520  1.57      matt 		len -= misalignment;
    521  1.57      matt 		cpu_dcache_inv_range(va, len);
    522  1.57      matt 		cpu_sdcache_inv_range(va, pa, len);
    523  1.57      matt 		if (misalignment) {
    524  1.57      matt 			va += len;
    525  1.57      matt 			pa += len;
    526  1.57      matt 			cpu_dcache_wbinv_range(va, arm_dcache_align);
    527  1.57      matt 			cpu_sdcache_wbinv_range(va, pa, arm_dcache_align);
    528  1.57      matt 		}
    529  1.14   thorpej 		break;
    530  1.57      matt 	}
    531  1.14   thorpej 
    532  1.14   thorpej 	case BUS_DMASYNC_PREWRITE:
    533  1.57      matt 		cpu_dcache_wb_range(va, len);
    534  1.57      matt 		cpu_sdcache_wb_range(va, pa, len);
    535  1.14   thorpej 		break;
    536  1.14   thorpej 	}
    537  1.14   thorpej }
    538  1.14   thorpej 
    539  1.47     perry static inline void
    540  1.57      matt _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    541  1.14   thorpej     bus_size_t len, int ops)
    542  1.14   thorpej {
    543  1.57      matt 	bus_dma_segment_t *ds = map->dm_segs;
    544  1.57      matt 	vaddr_t va = (vaddr_t) map->_dm_origbuf;
    545  1.57      matt 
    546  1.57      matt 	while (len > 0) {
    547  1.57      matt 		while (offset >= ds->ds_len) {
    548  1.57      matt 			offset -= ds->ds_len;
    549  1.57      matt 			va += ds->ds_len;
    550  1.57      matt 			ds++;
    551  1.57      matt 		}
    552  1.57      matt 
    553  1.57      matt 		paddr_t pa = ds->ds_addr + offset;
    554  1.57      matt 		size_t seglen = min(len, ds->ds_len - offset);
    555  1.57      matt 
    556  1.57      matt 		_bus_dmamap_sync_segment(va + offset, pa, seglen, ops, false);
    557  1.57      matt 
    558  1.57      matt 		offset += seglen;
    559  1.57      matt 		len -= seglen;
    560  1.57      matt 	}
    561  1.57      matt }
    562  1.57      matt 
    563  1.57      matt static inline void
    564  1.57      matt _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t offset,
    565  1.57      matt     bus_size_t len, int ops)
    566  1.57      matt {
    567  1.57      matt 	bus_dma_segment_t *ds = map->dm_segs;
    568  1.57      matt 	struct mbuf *m = map->_dm_origbuf;
    569  1.57      matt 	bus_size_t voff = offset;
    570  1.57      matt 	bus_size_t ds_off = offset;
    571  1.57      matt 
    572  1.57      matt 	while (len > 0) {
    573  1.57      matt 		/* Find the current dma segment */
    574  1.57      matt 		while (ds_off >= ds->ds_len) {
    575  1.57      matt 			ds_off -= ds->ds_len;
    576  1.57      matt 			ds++;
    577  1.57      matt 		}
    578  1.57      matt 		/* Find the current mbuf. */
    579  1.57      matt 		while (voff >= m->m_len) {
    580  1.57      matt 			voff -= m->m_len;
    581  1.57      matt 			m = m->m_next;
    582  1.14   thorpej 		}
    583  1.14   thorpej 
    584  1.14   thorpej 		/*
    585  1.14   thorpej 		 * Now at the first mbuf to sync; nail each one until
    586  1.14   thorpej 		 * we have exhausted the length.
    587  1.14   thorpej 		 */
    588  1.57      matt 		vsize_t seglen = min(len, min(m->m_len - voff, ds->ds_len - ds_off));
    589  1.57      matt 		vaddr_t va = mtod(m, vaddr_t) + voff;
    590  1.57      matt 		paddr_t pa = ds->ds_addr + ds_off;
    591  1.14   thorpej 
    592  1.28   thorpej 		/*
    593  1.28   thorpej 		 * We can save a lot of work here if we know the mapping
    594  1.28   thorpej 		 * is read-only at the MMU:
    595  1.28   thorpej 		 *
    596  1.28   thorpej 		 * If a mapping is read-only, no dirty cache blocks will
    597  1.28   thorpej 		 * exist for it.  If a writable mapping was made read-only,
    598  1.28   thorpej 		 * we know any dirty cache lines for the range will have
    599  1.28   thorpej 		 * been cleaned for us already.  Therefore, if the upper
    600  1.28   thorpej 		 * layer can tell us we have a read-only mapping, we can
    601  1.28   thorpej 		 * skip all cache cleaning.
    602  1.28   thorpej 		 *
    603  1.28   thorpej 		 * NOTE: This only works if we know the pmap cleans pages
    604  1.28   thorpej 		 * before making a read-write -> read-only transition.  If
    605  1.28   thorpej 		 * this ever becomes non-true (e.g. Physically Indexed
    606  1.28   thorpej 		 * cache), this will have to be revisited.
    607  1.28   thorpej 		 */
    608  1.14   thorpej 
    609  1.57      matt 		_bus_dmamap_sync_segment(va, pa, seglen, ops, M_ROMAP(m));
    610  1.57      matt 		voff += seglen;
    611  1.57      matt 		ds_off += seglen;
    612  1.57      matt 		len -= seglen;
    613  1.14   thorpej 	}
    614  1.14   thorpej }
    615  1.14   thorpej 
    616  1.47     perry static inline void
    617  1.14   thorpej _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    618  1.14   thorpej     bus_size_t len, int ops)
    619  1.14   thorpej {
    620  1.57      matt 	bus_dma_segment_t *ds = map->dm_segs;
    621  1.14   thorpej 	struct uio *uio = map->_dm_origbuf;
    622  1.57      matt 	struct iovec *iov = uio->uio_iov;
    623  1.57      matt 	bus_size_t voff = offset;
    624  1.57      matt 	bus_size_t ds_off = offset;
    625  1.57      matt 
    626  1.57      matt 	while (len > 0) {
    627  1.57      matt 		/* Find the current dma segment */
    628  1.57      matt 		while (ds_off >= ds->ds_len) {
    629  1.57      matt 			ds_off -= ds->ds_len;
    630  1.57      matt 			ds++;
    631  1.57      matt 		}
    632  1.14   thorpej 
    633  1.57      matt 		/* Find the current iovec. */
    634  1.57      matt 		while (voff >= iov->iov_len) {
    635  1.57      matt 			voff -= iov->iov_len;
    636  1.57      matt 			iov++;
    637  1.14   thorpej 		}
    638  1.14   thorpej 
    639  1.14   thorpej 		/*
    640  1.14   thorpej 		 * Now at the first iovec to sync; nail each one until
    641  1.14   thorpej 		 * we have exhausted the length.
    642  1.14   thorpej 		 */
    643  1.57      matt 		vsize_t seglen = min(len, min(iov->iov_len - voff, ds->ds_len - ds_off));
    644  1.57      matt 		vaddr_t va = (vaddr_t) iov->iov_base + voff;
    645  1.57      matt 		paddr_t pa = ds->ds_addr + ds_off;
    646  1.57      matt 
    647  1.57      matt 		_bus_dmamap_sync_segment(va, pa, seglen, ops, false);
    648  1.57      matt 
    649  1.57      matt 		voff += seglen;
    650  1.57      matt 		ds_off += seglen;
    651  1.57      matt 		len -= seglen;
    652  1.14   thorpej 	}
    653  1.14   thorpej }
    654  1.14   thorpej 
    655   1.1     chris /*
    656   1.1     chris  * Common function for DMA map synchronization.  May be called
    657   1.1     chris  * by bus-specific DMA map synchronization functions.
    658   1.8   thorpej  *
    659   1.8   thorpej  * This version works for the Virtually Indexed Virtually Tagged
    660   1.8   thorpej  * cache found on 32-bit ARM processors.
    661   1.8   thorpej  *
    662   1.8   thorpej  * XXX Should have separate versions for write-through vs.
    663   1.8   thorpej  * XXX write-back caches.  We currently assume write-back
    664   1.8   thorpej  * XXX here, which is not as efficient as it could be for
    665   1.8   thorpej  * XXX the write-through case.
    666   1.1     chris  */
    667   1.1     chris void
    668   1.7   thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    669   1.7   thorpej     bus_size_t len, int ops)
    670   1.1     chris {
    671   1.1     chris 
    672   1.1     chris #ifdef DEBUG_DMA
    673   1.1     chris 	printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
    674   1.1     chris 	    t, map, offset, len, ops);
    675   1.1     chris #endif	/* DEBUG_DMA */
    676   1.1     chris 
    677   1.8   thorpej 	/*
    678   1.8   thorpej 	 * Mixing of PRE and POST operations is not allowed.
    679   1.8   thorpej 	 */
    680   1.8   thorpej 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    681   1.8   thorpej 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    682   1.8   thorpej 		panic("_bus_dmamap_sync: mix PRE and POST");
    683   1.8   thorpej 
    684   1.8   thorpej #ifdef DIAGNOSTIC
    685   1.8   thorpej 	if (offset >= map->dm_mapsize)
    686   1.8   thorpej 		panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
    687   1.8   thorpej 		    offset, map->dm_mapsize);
    688   1.8   thorpej 	if (len == 0 || (offset + len) > map->dm_mapsize)
    689   1.8   thorpej 		panic("_bus_dmamap_sync: bad length");
    690   1.8   thorpej #endif
    691   1.8   thorpej 
    692   1.8   thorpej 	/*
    693   1.8   thorpej 	 * For a virtually-indexed write-back cache, we need
    694   1.8   thorpej 	 * to do the following things:
    695   1.8   thorpej 	 *
    696   1.8   thorpej 	 *	PREREAD -- Invalidate the D-cache.  We do this
    697   1.8   thorpej 	 *	here in case a write-back is required by the back-end.
    698   1.8   thorpej 	 *
    699   1.8   thorpej 	 *	PREWRITE -- Write-back the D-cache.  Note that if
    700   1.8   thorpej 	 *	we are doing a PREREAD|PREWRITE, we can collapse
    701   1.8   thorpej 	 *	the whole thing into a single Wb-Inv.
    702   1.8   thorpej 	 *
    703   1.8   thorpej 	 *	POSTREAD -- Nothing.
    704   1.8   thorpej 	 *
    705   1.8   thorpej 	 *	POSTWRITE -- Nothing.
    706   1.8   thorpej 	 */
    707   1.8   thorpej 
    708   1.8   thorpej 	ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    709   1.8   thorpej 	if (ops == 0)
    710   1.8   thorpej 		return;
    711   1.8   thorpej 
    712  1.17   thorpej 	/* Skip cache frobbing if mapping was COHERENT. */
    713  1.17   thorpej 	if (map->_dm_flags & ARM32_DMAMAP_COHERENT) {
    714  1.17   thorpej 		/* Drain the write buffer. */
    715  1.17   thorpej 		cpu_drain_writebuf();
    716  1.17   thorpej 		return;
    717  1.17   thorpej 	}
    718   1.8   thorpej 
    719   1.8   thorpej 	/*
    720  1.38       scw 	 * If the mapping belongs to a non-kernel vmspace, and the
    721  1.38       scw 	 * vmspace has not been active since the last time a full
    722  1.38       scw 	 * cache flush was performed, we don't need to do anything.
    723   1.8   thorpej 	 */
    724  1.48      yamt 	if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
    725  1.48      yamt 	    vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
    726   1.8   thorpej 		return;
    727   1.8   thorpej 
    728  1.14   thorpej 	switch (map->_dm_buftype) {
    729  1.14   thorpej 	case ARM32_BUFTYPE_LINEAR:
    730  1.14   thorpej 		_bus_dmamap_sync_linear(t, map, offset, len, ops);
    731  1.14   thorpej 		break;
    732  1.14   thorpej 
    733  1.14   thorpej 	case ARM32_BUFTYPE_MBUF:
    734  1.14   thorpej 		_bus_dmamap_sync_mbuf(t, map, offset, len, ops);
    735  1.14   thorpej 		break;
    736  1.14   thorpej 
    737  1.14   thorpej 	case ARM32_BUFTYPE_UIO:
    738  1.14   thorpej 		_bus_dmamap_sync_uio(t, map, offset, len, ops);
    739  1.14   thorpej 		break;
    740  1.14   thorpej 
    741  1.14   thorpej 	case ARM32_BUFTYPE_RAW:
    742  1.14   thorpej 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
    743  1.14   thorpej 		break;
    744  1.14   thorpej 
    745  1.14   thorpej 	case ARM32_BUFTYPE_INVALID:
    746  1.14   thorpej 		panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
    747  1.14   thorpej 		break;
    748  1.14   thorpej 
    749  1.14   thorpej 	default:
    750  1.14   thorpej 		printf("unknown buffer type %d\n", map->_dm_buftype);
    751  1.14   thorpej 		panic("_bus_dmamap_sync");
    752   1.8   thorpej 	}
    753   1.1     chris 
    754   1.8   thorpej 	/* Drain the write buffer. */
    755   1.8   thorpej 	cpu_drain_writebuf();
    756   1.1     chris }
    757   1.1     chris 
    758   1.1     chris /*
    759   1.1     chris  * Common function for DMA-safe memory allocation.  May be called
    760   1.1     chris  * by bus-specific DMA memory allocation functions.
    761   1.1     chris  */
    762   1.1     chris 
    763  1.11   thorpej extern paddr_t physical_start;
    764  1.11   thorpej extern paddr_t physical_end;
    765   1.1     chris 
    766   1.1     chris int
    767   1.7   thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    768   1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    769   1.7   thorpej     int flags)
    770   1.1     chris {
    771  1.15   thorpej 	struct arm32_dma_range *dr;
    772  1.37   mycroft 	int error, i;
    773  1.15   thorpej 
    774   1.1     chris #ifdef DEBUG_DMA
    775  1.15   thorpej 	printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
    776  1.15   thorpej 	    "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
    777  1.15   thorpej 	    boundary, segs, nsegs, rsegs, flags);
    778  1.15   thorpej #endif
    779  1.15   thorpej 
    780  1.15   thorpej 	if ((dr = t->_ranges) != NULL) {
    781  1.37   mycroft 		error = ENOMEM;
    782  1.15   thorpej 		for (i = 0; i < t->_nranges; i++, dr++) {
    783  1.37   mycroft 			if (dr->dr_len == 0)
    784  1.15   thorpej 				continue;
    785  1.15   thorpej 			error = _bus_dmamem_alloc_range(t, size, alignment,
    786  1.15   thorpej 			    boundary, segs, nsegs, rsegs, flags,
    787  1.15   thorpej 			    trunc_page(dr->dr_sysbase),
    788  1.15   thorpej 			    trunc_page(dr->dr_sysbase + dr->dr_len));
    789  1.15   thorpej 			if (error == 0)
    790  1.15   thorpej 				break;
    791  1.15   thorpej 		}
    792  1.15   thorpej 	} else {
    793  1.15   thorpej 		error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
    794  1.15   thorpej 		    segs, nsegs, rsegs, flags, trunc_page(physical_start),
    795  1.15   thorpej 		    trunc_page(physical_end));
    796  1.15   thorpej 	}
    797  1.15   thorpej 
    798   1.1     chris #ifdef DEBUG_DMA
    799   1.1     chris 	printf("dmamem_alloc: =%d\n", error);
    800  1.15   thorpej #endif
    801  1.15   thorpej 
    802   1.1     chris 	return(error);
    803   1.1     chris }
    804   1.1     chris 
    805   1.1     chris /*
    806   1.1     chris  * Common function for freeing DMA-safe memory.  May be called by
    807   1.1     chris  * bus-specific DMA memory free functions.
    808   1.1     chris  */
    809   1.1     chris void
    810   1.7   thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    811   1.1     chris {
    812   1.1     chris 	struct vm_page *m;
    813   1.1     chris 	bus_addr_t addr;
    814   1.1     chris 	struct pglist mlist;
    815   1.1     chris 	int curseg;
    816   1.1     chris 
    817   1.1     chris #ifdef DEBUG_DMA
    818   1.1     chris 	printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
    819   1.1     chris #endif	/* DEBUG_DMA */
    820   1.1     chris 
    821   1.1     chris 	/*
    822   1.1     chris 	 * Build a list of pages to free back to the VM system.
    823   1.1     chris 	 */
    824   1.1     chris 	TAILQ_INIT(&mlist);
    825   1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
    826   1.1     chris 		for (addr = segs[curseg].ds_addr;
    827   1.1     chris 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    828   1.1     chris 		    addr += PAGE_SIZE) {
    829   1.1     chris 			m = PHYS_TO_VM_PAGE(addr);
    830  1.52        ad 			TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
    831   1.1     chris 		}
    832   1.1     chris 	}
    833   1.1     chris 	uvm_pglistfree(&mlist);
    834   1.1     chris }
    835   1.1     chris 
    836   1.1     chris /*
    837   1.1     chris  * Common function for mapping DMA-safe memory.  May be called by
    838   1.1     chris  * bus-specific DMA memory map functions.
    839   1.1     chris  */
    840   1.1     chris int
    841   1.7   thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    842  1.50  christos     size_t size, void **kvap, int flags)
    843   1.1     chris {
    844  1.11   thorpej 	vaddr_t va;
    845  1.57      matt 	paddr_t pa;
    846   1.1     chris 	int curseg;
    847   1.1     chris 	pt_entry_t *ptep/*, pte*/;
    848  1.45      yamt 	const uvm_flag_t kmflags =
    849  1.45      yamt 	    (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
    850   1.1     chris 
    851   1.1     chris #ifdef DEBUG_DMA
    852   1.3  rearnsha 	printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
    853   1.3  rearnsha 	    segs, nsegs, (unsigned long)size, flags);
    854   1.1     chris #endif	/* DEBUG_DMA */
    855   1.1     chris 
    856   1.1     chris 	size = round_page(size);
    857  1.45      yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
    858   1.1     chris 
    859   1.1     chris 	if (va == 0)
    860   1.1     chris 		return (ENOMEM);
    861   1.1     chris 
    862  1.50  christos 	*kvap = (void *)va;
    863   1.1     chris 
    864   1.1     chris 	for (curseg = 0; curseg < nsegs; curseg++) {
    865  1.57      matt 		for (pa = segs[curseg].ds_addr;
    866  1.57      matt 		    pa < (segs[curseg].ds_addr + segs[curseg].ds_len);
    867  1.57      matt 		    pa += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    868   1.1     chris #ifdef DEBUG_DMA
    869  1.57      matt 			printf("wiring p%lx to v%lx", pa, va);
    870   1.1     chris #endif	/* DEBUG_DMA */
    871   1.1     chris 			if (size == 0)
    872   1.1     chris 				panic("_bus_dmamem_map: size botch");
    873  1.57      matt 			pmap_enter(pmap_kernel(), va, pa,
    874   1.1     chris 			    VM_PROT_READ | VM_PROT_WRITE,
    875   1.1     chris 			    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
    876  1.57      matt 
    877   1.1     chris 			/*
    878   1.1     chris 			 * If the memory must remain coherent with the
    879   1.1     chris 			 * cache then we must make the memory uncacheable
    880   1.1     chris 			 * in order to maintain virtual cache coherency.
    881  1.24       wiz 			 * We must also guarantee the cache does not already
    882   1.1     chris 			 * contain the virtal addresses we are making
    883   1.1     chris 			 * uncacheable.
    884   1.1     chris 			 */
    885   1.1     chris 			if (flags & BUS_DMA_COHERENT) {
    886  1.27   thorpej 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
    887  1.57      matt 				cpu_sdcache_wbinv_range(va, pa, PAGE_SIZE);
    888   1.1     chris 				cpu_drain_writebuf();
    889   1.1     chris 				ptep = vtopte(va);
    890  1.17   thorpej 				*ptep &= ~L2_S_CACHE_MASK;
    891  1.21   thorpej 				PTE_SYNC(ptep);
    892   1.1     chris 				tlb_flush();
    893   1.1     chris 			}
    894   1.1     chris #ifdef DEBUG_DMA
    895   1.1     chris 			ptep = vtopte(va);
    896   1.1     chris 			printf(" pte=v%p *pte=%x\n", ptep, *ptep);
    897   1.1     chris #endif	/* DEBUG_DMA */
    898   1.1     chris 		}
    899   1.1     chris 	}
    900   1.2     chris 	pmap_update(pmap_kernel());
    901   1.1     chris #ifdef DEBUG_DMA
    902   1.1     chris 	printf("dmamem_map: =%p\n", *kvap);
    903   1.1     chris #endif	/* DEBUG_DMA */
    904   1.1     chris 	return (0);
    905   1.1     chris }
    906   1.1     chris 
    907   1.1     chris /*
    908   1.1     chris  * Common function for unmapping DMA-safe memory.  May be called by
    909   1.1     chris  * bus-specific DMA memory unmapping functions.
    910   1.1     chris  */
    911   1.1     chris void
    912  1.50  christos _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
    913   1.1     chris {
    914   1.1     chris 
    915   1.1     chris #ifdef DEBUG_DMA
    916   1.3  rearnsha 	printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
    917   1.3  rearnsha 	    (unsigned long)size);
    918   1.1     chris #endif	/* DEBUG_DMA */
    919   1.1     chris #ifdef DIAGNOSTIC
    920   1.1     chris 	if ((u_long)kva & PGOFSET)
    921   1.1     chris 		panic("_bus_dmamem_unmap");
    922   1.1     chris #endif	/* DIAGNOSTIC */
    923   1.1     chris 
    924   1.1     chris 	size = round_page(size);
    925  1.44      yamt 	pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
    926  1.44      yamt 	pmap_update(pmap_kernel());
    927  1.44      yamt 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
    928   1.1     chris }
    929   1.1     chris 
    930   1.1     chris /*
    931   1.1     chris  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    932   1.1     chris  * bus-specific DMA mmap(2)'ing functions.
    933   1.1     chris  */
    934   1.1     chris paddr_t
    935   1.7   thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    936   1.7   thorpej     off_t off, int prot, int flags)
    937   1.1     chris {
    938   1.1     chris 	int i;
    939   1.1     chris 
    940   1.1     chris 	for (i = 0; i < nsegs; i++) {
    941   1.1     chris #ifdef DIAGNOSTIC
    942   1.1     chris 		if (off & PGOFSET)
    943   1.1     chris 			panic("_bus_dmamem_mmap: offset unaligned");
    944   1.1     chris 		if (segs[i].ds_addr & PGOFSET)
    945   1.1     chris 			panic("_bus_dmamem_mmap: segment unaligned");
    946   1.1     chris 		if (segs[i].ds_len & PGOFSET)
    947   1.1     chris 			panic("_bus_dmamem_mmap: segment size not multiple"
    948   1.1     chris 			    " of page size");
    949   1.1     chris #endif	/* DIAGNOSTIC */
    950   1.1     chris 		if (off >= segs[i].ds_len) {
    951   1.1     chris 			off -= segs[i].ds_len;
    952   1.1     chris 			continue;
    953   1.1     chris 		}
    954   1.1     chris 
    955   1.9   thorpej 		return (arm_btop((u_long)segs[i].ds_addr + off));
    956   1.1     chris 	}
    957   1.1     chris 
    958   1.1     chris 	/* Page not found. */
    959   1.1     chris 	return (-1);
    960   1.1     chris }
    961   1.1     chris 
    962   1.1     chris /**********************************************************************
    963   1.1     chris  * DMA utility functions
    964   1.1     chris  **********************************************************************/
    965   1.1     chris 
    966   1.1     chris /*
    967   1.1     chris  * Utility function to load a linear buffer.  lastaddrp holds state
    968   1.1     chris  * between invocations (for multiple-buffer loads).  segp contains
    969   1.1     chris  * the starting segment on entrace, and the ending segment on exit.
    970   1.1     chris  * first indicates if this is the first invocation of this function.
    971   1.1     chris  */
    972   1.1     chris int
    973   1.7   thorpej _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    974  1.48      yamt     bus_size_t buflen, struct vmspace *vm, int flags)
    975   1.1     chris {
    976   1.1     chris 	bus_size_t sgsize;
    977  1.41   thorpej 	bus_addr_t curaddr;
    978  1.11   thorpej 	vaddr_t vaddr = (vaddr_t)buf;
    979  1.17   thorpej 	pd_entry_t *pde;
    980  1.17   thorpej 	pt_entry_t pte;
    981  1.41   thorpej 	int error;
    982   1.1     chris 	pmap_t pmap;
    983  1.29       scw 	pt_entry_t *ptep;
    984   1.1     chris 
    985   1.1     chris #ifdef DEBUG_DMA
    986  1.40       scw 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
    987  1.40       scw 	    buf, buflen, flags);
    988   1.1     chris #endif	/* DEBUG_DMA */
    989   1.1     chris 
    990  1.48      yamt 	pmap = vm_map_pmap(&vm->vm_map);
    991   1.1     chris 
    992  1.41   thorpej 	while (buflen > 0) {
    993   1.1     chris 		/*
    994   1.1     chris 		 * Get the physical address for this segment.
    995  1.17   thorpej 		 *
    996  1.55      matt 		 * XXX Doesn't support checking for coherent mappings
    997  1.17   thorpej 		 * XXX in user address space.
    998   1.1     chris 		 */
    999  1.17   thorpej 		if (__predict_true(pmap == pmap_kernel())) {
   1000  1.29       scw 			(void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
   1001  1.17   thorpej 			if (__predict_false(pmap_pde_section(pde))) {
   1002  1.55      matt 				paddr_t s_frame = L1_S_FRAME;
   1003  1.55      matt 				paddr_t s_offset = L1_S_OFFSET;
   1004  1.56      matt #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
   1005  1.55      matt 				if (__predict_false(pmap_pde_supersection(pde))) {
   1006  1.55      matt 					s_frame = L1_SS_FRAME;
   1007  1.55      matt 					s_frame = L1_SS_OFFSET;
   1008  1.55      matt }
   1009  1.55      matt #endif
   1010  1.55      matt 				curaddr = (*pde & s_frame) | (vaddr & s_offset);
   1011  1.17   thorpej 				if (*pde & L1_S_CACHE_MASK) {
   1012  1.55      matt 					map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
   1013  1.17   thorpej 				}
   1014  1.17   thorpej 			} else {
   1015  1.29       scw 				pte = *ptep;
   1016  1.17   thorpej 				KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
   1017  1.17   thorpej 				if (__predict_false((pte & L2_TYPE_MASK)
   1018  1.17   thorpej 						    == L2_TYPE_L)) {
   1019  1.17   thorpej 					curaddr = (pte & L2_L_FRAME) |
   1020  1.17   thorpej 					    (vaddr & L2_L_OFFSET);
   1021  1.17   thorpej 					if (pte & L2_L_CACHE_MASK) {
   1022  1.17   thorpej 						map->_dm_flags &=
   1023  1.17   thorpej 						    ~ARM32_DMAMAP_COHERENT;
   1024  1.17   thorpej 					}
   1025  1.17   thorpej 				} else {
   1026  1.17   thorpej 					curaddr = (pte & L2_S_FRAME) |
   1027  1.17   thorpej 					    (vaddr & L2_S_OFFSET);
   1028  1.17   thorpej 					if (pte & L2_S_CACHE_MASK) {
   1029  1.17   thorpej 						map->_dm_flags &=
   1030  1.17   thorpej 						    ~ARM32_DMAMAP_COHERENT;
   1031  1.17   thorpej 					}
   1032  1.17   thorpej 				}
   1033  1.17   thorpej 			}
   1034  1.34    briggs 		} else {
   1035  1.17   thorpej 			(void) pmap_extract(pmap, vaddr, &curaddr);
   1036  1.34    briggs 			map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
   1037  1.34    briggs 		}
   1038   1.1     chris 
   1039   1.1     chris 		/*
   1040   1.1     chris 		 * Compute the segment size, and adjust counts.
   1041   1.1     chris 		 */
   1042  1.27   thorpej 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
   1043   1.1     chris 		if (buflen < sgsize)
   1044   1.1     chris 			sgsize = buflen;
   1045   1.1     chris 
   1046  1.41   thorpej 		error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize);
   1047  1.41   thorpej 		if (error)
   1048  1.41   thorpej 			return (error);
   1049   1.1     chris 
   1050   1.1     chris 		vaddr += sgsize;
   1051   1.1     chris 		buflen -= sgsize;
   1052   1.1     chris 	}
   1053   1.1     chris 
   1054   1.1     chris 	return (0);
   1055   1.1     chris }
   1056   1.1     chris 
   1057   1.1     chris /*
   1058   1.1     chris  * Allocate physical memory from the given physical address range.
   1059   1.1     chris  * Called by DMA-safe memory allocation methods.
   1060   1.1     chris  */
   1061   1.1     chris int
   1062   1.7   thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1063   1.7   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1064  1.11   thorpej     int flags, paddr_t low, paddr_t high)
   1065   1.1     chris {
   1066  1.11   thorpej 	paddr_t curaddr, lastaddr;
   1067   1.1     chris 	struct vm_page *m;
   1068   1.1     chris 	struct pglist mlist;
   1069   1.1     chris 	int curseg, error;
   1070   1.1     chris 
   1071   1.1     chris #ifdef DEBUG_DMA
   1072   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",
   1073   1.1     chris 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
   1074   1.1     chris #endif	/* DEBUG_DMA */
   1075   1.1     chris 
   1076   1.1     chris 	/* Always round the size. */
   1077   1.1     chris 	size = round_page(size);
   1078   1.1     chris 
   1079   1.1     chris 	/*
   1080   1.1     chris 	 * Allocate pages from the VM system.
   1081   1.1     chris 	 */
   1082   1.1     chris 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
   1083   1.1     chris 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
   1084   1.1     chris 	if (error)
   1085   1.1     chris 		return (error);
   1086   1.1     chris 
   1087   1.1     chris 	/*
   1088   1.1     chris 	 * Compute the location, size, and number of segments actually
   1089   1.1     chris 	 * returned by the VM code.
   1090   1.1     chris 	 */
   1091  1.42     chris 	m = TAILQ_FIRST(&mlist);
   1092   1.1     chris 	curseg = 0;
   1093   1.1     chris 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
   1094   1.1     chris 	segs[curseg].ds_len = PAGE_SIZE;
   1095   1.1     chris #ifdef DEBUG_DMA
   1096   1.1     chris 		printf("alloc: page %lx\n", lastaddr);
   1097   1.1     chris #endif	/* DEBUG_DMA */
   1098  1.52        ad 	m = TAILQ_NEXT(m, pageq.queue);
   1099   1.1     chris 
   1100  1.52        ad 	for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
   1101   1.1     chris 		curaddr = VM_PAGE_TO_PHYS(m);
   1102   1.1     chris #ifdef DIAGNOSTIC
   1103   1.1     chris 		if (curaddr < low || curaddr >= high) {
   1104   1.1     chris 			printf("uvm_pglistalloc returned non-sensical"
   1105   1.1     chris 			    " address 0x%lx\n", curaddr);
   1106   1.1     chris 			panic("_bus_dmamem_alloc_range");
   1107   1.1     chris 		}
   1108   1.1     chris #endif	/* DIAGNOSTIC */
   1109   1.1     chris #ifdef DEBUG_DMA
   1110   1.1     chris 		printf("alloc: page %lx\n", curaddr);
   1111   1.1     chris #endif	/* DEBUG_DMA */
   1112   1.1     chris 		if (curaddr == (lastaddr + PAGE_SIZE))
   1113   1.1     chris 			segs[curseg].ds_len += PAGE_SIZE;
   1114   1.1     chris 		else {
   1115   1.1     chris 			curseg++;
   1116   1.1     chris 			segs[curseg].ds_addr = curaddr;
   1117   1.1     chris 			segs[curseg].ds_len = PAGE_SIZE;
   1118   1.1     chris 		}
   1119   1.1     chris 		lastaddr = curaddr;
   1120   1.1     chris 	}
   1121   1.1     chris 
   1122   1.1     chris 	*rsegs = curseg + 1;
   1123   1.1     chris 
   1124  1.15   thorpej 	return (0);
   1125  1.15   thorpej }
   1126  1.15   thorpej 
   1127  1.15   thorpej /*
   1128  1.15   thorpej  * Check if a memory region intersects with a DMA range, and return the
   1129  1.15   thorpej  * page-rounded intersection if it does.
   1130  1.15   thorpej  */
   1131  1.15   thorpej int
   1132  1.15   thorpej arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
   1133  1.15   thorpej     paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
   1134  1.15   thorpej {
   1135  1.15   thorpej 	struct arm32_dma_range *dr;
   1136  1.15   thorpej 	int i;
   1137  1.15   thorpej 
   1138  1.15   thorpej 	if (ranges == NULL)
   1139  1.15   thorpej 		return (0);
   1140  1.15   thorpej 
   1141  1.15   thorpej 	for (i = 0, dr = ranges; i < nranges; i++, dr++) {
   1142  1.15   thorpej 		if (dr->dr_sysbase <= pa &&
   1143  1.15   thorpej 		    pa < (dr->dr_sysbase + dr->dr_len)) {
   1144  1.15   thorpej 			/*
   1145  1.15   thorpej 			 * Beginning of region intersects with this range.
   1146  1.15   thorpej 			 */
   1147  1.15   thorpej 			*pap = trunc_page(pa);
   1148  1.15   thorpej 			*sizep = round_page(min(pa + size,
   1149  1.15   thorpej 			    dr->dr_sysbase + dr->dr_len) - pa);
   1150  1.15   thorpej 			return (1);
   1151  1.15   thorpej 		}
   1152  1.15   thorpej 		if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
   1153  1.15   thorpej 			/*
   1154  1.15   thorpej 			 * End of region intersects with this range.
   1155  1.15   thorpej 			 */
   1156  1.15   thorpej 			*pap = trunc_page(dr->dr_sysbase);
   1157  1.15   thorpej 			*sizep = round_page(min((pa + size) - dr->dr_sysbase,
   1158  1.15   thorpej 			    dr->dr_len));
   1159  1.15   thorpej 			return (1);
   1160  1.15   thorpej 		}
   1161  1.15   thorpej 	}
   1162  1.15   thorpej 
   1163  1.15   thorpej 	/* No intersection found. */
   1164   1.1     chris 	return (0);
   1165   1.1     chris }
   1166