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