Home | History | Annotate | Line # | Download | only in common
      1  1.74    andvar /* $NetBSD: bus_dma.c,v 1.74 2022/07/26 20:08:54 andvar Exp $ */
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4   1.8   thorpej  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5   1.2   thorpej  * All rights reserved.
      6   1.2   thorpej  *
      7   1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.2   thorpej  * NASA Ames Research Center.
     10   1.2   thorpej  *
     11   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.2   thorpej  * modification, are permitted provided that the following conditions
     13   1.2   thorpej  * are met:
     14   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.2   thorpej  *
     20   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.2   thorpej  */
     32   1.2   thorpej 
     33   1.2   thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34   1.2   thorpej 
     35  1.74    andvar __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.74 2022/07/26 20:08:54 andvar Exp $");
     36   1.2   thorpej 
     37   1.2   thorpej #include <sys/param.h>
     38   1.2   thorpej #include <sys/systm.h>
     39   1.2   thorpej #include <sys/kernel.h>
     40   1.2   thorpej #include <sys/device.h>
     41  1.71   thorpej #include <sys/kmem.h>
     42   1.2   thorpej #include <sys/proc.h>
     43   1.9   thorpej #include <sys/mbuf.h>
     44  1.28       mrg 
     45  1.16   thorpej #include <uvm/uvm_extern.h>
     46   1.2   thorpej 
     47   1.2   thorpej #define _ALPHA_BUS_DMA_PRIVATE
     48  1.68    dyoung #include <sys/bus.h>
     49   1.3   thorpej #include <machine/intr.h>
     50   1.2   thorpej 
     51  1.69  christos #include <dev/bus_dma/bus_dmamem_common.h>
     52  1.67   thorpej 
     53  1.72   thorpej static int	_bus_dmamap_load_buffer_direct(bus_dma_tag_t,
     54  1.72   thorpej 		    bus_dmamap_t, void *, bus_size_t, struct vmspace *, int,
     55  1.72   thorpej 		    paddr_t *, int *, int);
     56   1.9   thorpej 
     57  1.34   thorpej extern paddr_t avail_start, avail_end;	/* from pmap.c */
     58  1.34   thorpej 
     59  1.70   thorpej #define	DMA_COUNT_DECL(cnt)	_DMA_COUNT_DECL(dma_direct, cnt)
     60  1.70   thorpej #define	DMA_COUNT(cnt)		_DMA_COUNT(dma_direct, cnt)
     61  1.70   thorpej 
     62  1.71   thorpej static size_t
     63  1.71   thorpej _bus_dmamap_mapsize(int const nsegments)
     64  1.71   thorpej {
     65  1.71   thorpej 	KASSERT(nsegments > 0);
     66  1.71   thorpej 	return sizeof(struct alpha_bus_dmamap) +
     67  1.71   thorpej 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
     68  1.71   thorpej }
     69  1.71   thorpej 
     70   1.2   thorpej /*
     71   1.2   thorpej  * Common function for DMA map creation.  May be called by bus-specific
     72   1.2   thorpej  * DMA map creation functions.
     73   1.2   thorpej  */
     74   1.2   thorpej int
     75  1.41   thorpej _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
     76  1.41   thorpej     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
     77   1.2   thorpej {
     78   1.2   thorpej 	struct alpha_bus_dmamap *map;
     79   1.2   thorpej 	void *mapstore;
     80   1.2   thorpej 
     81   1.2   thorpej 	/*
     82  1.35    mjacob 	 * Allocate and initialize the DMA map.  The end of the map
     83   1.2   thorpej 	 * is a variable-sized array of segments, so we allocate enough
     84   1.2   thorpej 	 * room for them in one shot.
     85   1.2   thorpej 	 *
     86   1.2   thorpej 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
     87  1.56    simonb 	 * of ALLOCNOW notifies others that we've reserved these resources,
     88   1.2   thorpej 	 * and they are not to be freed.
     89   1.2   thorpej 	 *
     90   1.2   thorpej 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
     91   1.2   thorpej 	 * the (nsegments - 1).
     92   1.2   thorpej 	 */
     93  1.71   thorpej 	if ((mapstore = kmem_zalloc(_bus_dmamap_mapsize(nsegments),
     94  1.71   thorpej 	    (flags & BUS_DMA_NOWAIT) ? KM_NOSLEEP : KM_SLEEP)) == NULL)
     95   1.2   thorpej 		return (ENOMEM);
     96   1.2   thorpej 
     97   1.2   thorpej 	map = (struct alpha_bus_dmamap *)mapstore;
     98   1.2   thorpej 	map->_dm_size = size;
     99   1.2   thorpej 	map->_dm_segcnt = nsegments;
    100  1.57      matt 	map->_dm_maxmaxsegsz = maxsegsz;
    101  1.23   thorpej 	if (t->_boundary != 0 && t->_boundary < boundary)
    102  1.23   thorpej 		map->_dm_boundary = t->_boundary;
    103  1.23   thorpej 	else
    104  1.23   thorpej 		map->_dm_boundary = boundary;
    105   1.2   thorpej 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    106  1.57      matt 	map->dm_maxsegsz = maxsegsz;
    107  1.10   thorpej 	map->dm_mapsize = 0;		/* no valid mappings */
    108  1.10   thorpej 	map->dm_nsegs = 0;
    109  1.49   thorpej 	map->_dm_window = NULL;
    110   1.2   thorpej 
    111   1.2   thorpej 	*dmamp = map;
    112   1.2   thorpej 	return (0);
    113   1.2   thorpej }
    114   1.2   thorpej 
    115   1.2   thorpej /*
    116   1.2   thorpej  * Common function for DMA map destruction.  May be called by bus-specific
    117   1.2   thorpej  * DMA map destruction functions.
    118   1.2   thorpej  */
    119   1.2   thorpej void
    120  1.41   thorpej _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    121   1.2   thorpej {
    122   1.2   thorpej 
    123  1.71   thorpej 	kmem_free(map, _bus_dmamap_mapsize(map->_dm_segcnt));
    124   1.2   thorpej }
    125   1.2   thorpej 
    126   1.2   thorpej /*
    127   1.9   thorpej  * Utility function to load a linear buffer.  lastaddrp holds state
    128   1.9   thorpej  * between invocations (for multiple-buffer loads).  segp contains
    129   1.9   thorpej  * the starting segment on entrance, and the ending segment on exit.
    130   1.9   thorpej  * first indicates if this is the first invocation of this function.
    131   1.2   thorpej  */
    132  1.72   thorpej static int
    133  1.43   thorpej _bus_dmamap_load_buffer_direct(bus_dma_tag_t t, bus_dmamap_t map,
    134  1.62      yamt     void *buf, size_t buflen, struct vmspace *vm, int flags, paddr_t *lastaddrp,
    135  1.41   thorpej     int *segp, int first)
    136   1.2   thorpej {
    137   1.2   thorpej 	bus_size_t sgsize;
    138  1.22   thorpej 	bus_addr_t curaddr, lastaddr, baddr, bmask;
    139  1.25   thorpej 	vaddr_t vaddr = (vaddr_t)buf;
    140   1.9   thorpej 	int seg;
    141  1.73   thorpej 	bool address_is_valid __diagused;
    142   1.2   thorpej 
    143   1.9   thorpej 	lastaddr = *lastaddrp;
    144  1.21      matt 	bmask = ~(map->_dm_boundary - 1);
    145   1.2   thorpej 
    146  1.20      matt 	for (seg = *segp; buflen > 0 ; ) {
    147   1.2   thorpej 		/*
    148   1.2   thorpej 		 * Get the physical address for this segment.
    149   1.2   thorpej 		 */
    150  1.73   thorpej 		address_is_valid =
    151  1.73   thorpej 		    pmap_extract(vm->vm_map.pmap, vaddr, &curaddr);
    152  1.73   thorpej 		KASSERT(address_is_valid);
    153   1.2   thorpej 
    154  1.19   thorpej 		/*
    155  1.19   thorpej 		 * If we're beyond the current DMA window, indicate
    156  1.19   thorpej 		 * that and try to fall back into SGMAPs.
    157  1.19   thorpej 		 */
    158  1.26   thorpej 		if (t->_wsize != 0 && curaddr >= t->_wsize)
    159  1.19   thorpej 			return (EINVAL);
    160  1.19   thorpej 
    161  1.26   thorpej 		curaddr |= t->_wbase;
    162   1.2   thorpej 
    163   1.2   thorpej 		/*
    164   1.2   thorpej 		 * Compute the segment size, and adjust counts.
    165   1.2   thorpej 		 */
    166  1.52   thorpej 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
    167   1.2   thorpej 		if (buflen < sgsize)
    168   1.2   thorpej 			sgsize = buflen;
    169  1.57      matt 		if (map->dm_maxsegsz < sgsize)
    170  1.57      matt 			sgsize = map->dm_maxsegsz;
    171  1.22   thorpej 
    172  1.20      matt 		/*
    173  1.22   thorpej 		 * Make sure we don't cross any boundaries.
    174  1.20      matt 		 */
    175  1.21      matt 		if (map->_dm_boundary > 0) {
    176  1.20      matt 			baddr = (curaddr + map->_dm_boundary) & bmask;
    177  1.22   thorpej 			if (sgsize > (baddr - curaddr))
    178  1.22   thorpej 				sgsize = (baddr - curaddr);
    179  1.20      matt 		}
    180   1.2   thorpej 
    181   1.2   thorpej 		/*
    182   1.2   thorpej 		 * Insert chunk into a segment, coalescing with
    183   1.2   thorpej 		 * the previous segment if possible.
    184   1.2   thorpej 		 */
    185   1.2   thorpej 		if (first) {
    186   1.2   thorpej 			map->dm_segs[seg].ds_addr = curaddr;
    187   1.2   thorpej 			map->dm_segs[seg].ds_len = sgsize;
    188   1.2   thorpej 			first = 0;
    189   1.2   thorpej 		} else {
    190  1.36   thorpej 			if ((map->_dm_flags & DMAMAP_NO_COALESCE) == 0 &&
    191  1.36   thorpej 			    curaddr == lastaddr &&
    192   1.2   thorpej 			    (map->dm_segs[seg].ds_len + sgsize) <=
    193  1.57      matt 			     map->dm_maxsegsz &&
    194  1.20      matt 			    (map->_dm_boundary == 0 ||
    195  1.20      matt 			     (map->dm_segs[seg].ds_addr & bmask) ==
    196  1.21      matt 			     (curaddr & bmask)))
    197   1.2   thorpej 				map->dm_segs[seg].ds_len += sgsize;
    198   1.2   thorpej 			else {
    199  1.20      matt 				if (++seg >= map->_dm_segcnt)
    200  1.20      matt 					break;
    201   1.2   thorpej 				map->dm_segs[seg].ds_addr = curaddr;
    202   1.2   thorpej 				map->dm_segs[seg].ds_len = sgsize;
    203   1.2   thorpej 			}
    204   1.2   thorpej 		}
    205   1.2   thorpej 
    206   1.2   thorpej 		lastaddr = curaddr + sgsize;
    207   1.2   thorpej 		vaddr += sgsize;
    208   1.2   thorpej 		buflen -= sgsize;
    209   1.2   thorpej 	}
    210   1.2   thorpej 
    211   1.9   thorpej 	*segp = seg;
    212   1.9   thorpej 	*lastaddrp = lastaddr;
    213   1.9   thorpej 
    214   1.2   thorpej 	/*
    215   1.2   thorpej 	 * Did we fit?
    216   1.2   thorpej 	 */
    217   1.2   thorpej 	if (buflen != 0) {
    218   1.2   thorpej 		/*
    219  1.19   thorpej 		 * If there is a chained window, we will automatically
    220  1.19   thorpej 		 * fall back to it.
    221   1.2   thorpej 		 */
    222   1.2   thorpej 		return (EFBIG);		/* XXX better return value here? */
    223   1.2   thorpej 	}
    224  1.19   thorpej 
    225   1.9   thorpej 	return (0);
    226   1.9   thorpej }
    227   1.9   thorpej 
    228  1.70   thorpej DMA_COUNT_DECL(load);
    229  1.70   thorpej DMA_COUNT_DECL(load_next_window);
    230  1.70   thorpej 
    231   1.9   thorpej /*
    232   1.9   thorpej  * Common function for loading a direct-mapped DMA map with a linear
    233   1.9   thorpej  * buffer.  Called by bus-specific DMA map load functions with the
    234   1.9   thorpej  * OR value appropriate for indicating "direct-mapped" for that
    235   1.9   thorpej  * chipset.
    236   1.9   thorpej  */
    237   1.9   thorpej int
    238  1.41   thorpej _bus_dmamap_load_direct(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    239  1.41   thorpej     bus_size_t buflen, struct proc *p, int flags)
    240   1.9   thorpej {
    241  1.25   thorpej 	paddr_t lastaddr;
    242   1.9   thorpej 	int seg, error;
    243  1.62      yamt 	struct vmspace *vm;
    244   1.9   thorpej 
    245   1.9   thorpej 	/*
    246   1.9   thorpej 	 * Make sure that on error condition we return "no valid mappings".
    247   1.9   thorpej 	 */
    248  1.10   thorpej 	map->dm_mapsize = 0;
    249   1.9   thorpej 	map->dm_nsegs = 0;
    250  1.57      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    251  1.59    mhitch 	KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
    252   1.2   thorpej 
    253   1.9   thorpej 	if (buflen > map->_dm_size)
    254   1.9   thorpej 		return (EINVAL);
    255   1.9   thorpej 
    256  1.62      yamt 	if (p != NULL) {
    257  1.62      yamt 		vm = p->p_vmspace;
    258  1.62      yamt 	} else {
    259  1.62      yamt 		vm = vmspace_kernel();
    260  1.62      yamt 	}
    261   1.9   thorpej 	seg = 0;
    262  1.43   thorpej 	error = _bus_dmamap_load_buffer_direct(t, map, buf, buflen,
    263  1.62      yamt 	    vm, flags, &lastaddr, &seg, 1);
    264  1.10   thorpej 	if (error == 0) {
    265  1.70   thorpej 		DMA_COUNT(load);
    266  1.10   thorpej 		map->dm_mapsize = buflen;
    267   1.9   thorpej 		map->dm_nsegs = seg + 1;
    268  1.49   thorpej 		map->_dm_window = t;
    269  1.19   thorpej 	} else if (t->_next_window != NULL) {
    270  1.19   thorpej 		/*
    271  1.19   thorpej 		 * Give the next window a chance.
    272  1.19   thorpej 		 */
    273  1.70   thorpej 		DMA_COUNT(load_next_window);
    274  1.19   thorpej 		error = bus_dmamap_load(t->_next_window, map, buf, buflen,
    275  1.19   thorpej 		    p, flags);
    276  1.10   thorpej 	}
    277   1.9   thorpej 	return (error);
    278   1.2   thorpej }
    279   1.2   thorpej 
    280  1.70   thorpej DMA_COUNT_DECL(load_mbuf);
    281  1.70   thorpej DMA_COUNT_DECL(load_mbuf_next_window);
    282  1.70   thorpej 
    283   1.2   thorpej /*
    284  1.42   thorpej  * Like _bus_dmamap_load_direct(), but for mbufs.
    285   1.2   thorpej  */
    286   1.2   thorpej int
    287  1.41   thorpej _bus_dmamap_load_mbuf_direct(bus_dma_tag_t t, bus_dmamap_t map,
    288  1.41   thorpej     struct mbuf *m0, int flags)
    289   1.2   thorpej {
    290  1.25   thorpej 	paddr_t lastaddr;
    291   1.9   thorpej 	int seg, error, first;
    292   1.9   thorpej 	struct mbuf *m;
    293   1.9   thorpej 
    294   1.9   thorpej 	/*
    295   1.9   thorpej 	 * Make sure that on error condition we return "no valid mappings."
    296   1.9   thorpej 	 */
    297  1.10   thorpej 	map->dm_mapsize = 0;
    298   1.9   thorpej 	map->dm_nsegs = 0;
    299  1.57      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    300  1.59    mhitch 	KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
    301   1.9   thorpej 
    302   1.9   thorpej #ifdef DIAGNOSTIC
    303   1.9   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    304  1.43   thorpej 		panic("_bus_dmamap_load_mbuf_direct: no packet header");
    305   1.9   thorpej #endif
    306   1.2   thorpej 
    307   1.9   thorpej 	if (m0->m_pkthdr.len > map->_dm_size)
    308   1.9   thorpej 		return (EINVAL);
    309   1.9   thorpej 
    310   1.9   thorpej 	first = 1;
    311   1.9   thorpej 	seg = 0;
    312   1.9   thorpej 	error = 0;
    313   1.9   thorpej 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    314  1.53   thorpej 		if (m->m_len == 0)
    315  1.53   thorpej 			continue;
    316  1.53   thorpej 		/* XXX Could be better about coalescing. */
    317  1.53   thorpej 		/* XXX Doesn't check boundaries. */
    318  1.53   thorpej 		switch (m->m_flags & (M_EXT|M_EXT_CLUSTER)) {
    319  1.53   thorpej 		case M_EXT|M_EXT_CLUSTER:
    320  1.53   thorpej 			/* XXX KDASSERT */
    321  1.53   thorpej 			KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
    322  1.53   thorpej 			lastaddr = m->m_ext.ext_paddr +
    323  1.53   thorpej 			    (m->m_data - m->m_ext.ext_buf);
    324  1.53   thorpej  have_addr:
    325  1.53   thorpej 			if (first == 0 &&
    326  1.53   thorpej 			    ++seg >= map->_dm_segcnt) {
    327  1.53   thorpej 				error = EFBIG;
    328  1.53   thorpej 				break;
    329  1.53   thorpej 			}
    330  1.53   thorpej 
    331  1.53   thorpej 			/*
    332  1.53   thorpej 			 * If we're beyond the current DMA window, indicate
    333  1.53   thorpej 			 * that and try to fall back into SGMAPs.
    334  1.53   thorpej 			 */
    335  1.53   thorpej 			if (t->_wsize != 0 && lastaddr >= t->_wsize) {
    336  1.53   thorpej 				error = EINVAL;
    337  1.53   thorpej 				break;
    338  1.53   thorpej 			}
    339  1.53   thorpej 			lastaddr |= t->_wbase;
    340  1.53   thorpej 
    341  1.53   thorpej 			map->dm_segs[seg].ds_addr = lastaddr;
    342  1.53   thorpej 			map->dm_segs[seg].ds_len = m->m_len;
    343  1.53   thorpej 			lastaddr += m->m_len;
    344  1.53   thorpej 			break;
    345  1.53   thorpej 
    346  1.53   thorpej 		case 0:
    347  1.53   thorpej 			lastaddr = m->m_paddr + M_BUFOFFSET(m) +
    348  1.53   thorpej 			    (m->m_data - M_BUFADDR(m));
    349  1.53   thorpej 			goto have_addr;
    350  1.53   thorpej 
    351  1.53   thorpej 		default:
    352  1.53   thorpej 			error = _bus_dmamap_load_buffer_direct(t, map,
    353  1.62      yamt 			    m->m_data, m->m_len, vmspace_kernel(), flags,
    354  1.62      yamt 			    &lastaddr, &seg, first);
    355  1.53   thorpej 		}
    356   1.9   thorpej 		first = 0;
    357   1.9   thorpej 	}
    358  1.10   thorpej 	if (error == 0) {
    359  1.70   thorpej 		DMA_COUNT(load_mbuf);
    360  1.10   thorpej 		map->dm_mapsize = m0->m_pkthdr.len;
    361   1.9   thorpej 		map->dm_nsegs = seg + 1;
    362  1.49   thorpej 		map->_dm_window = t;
    363  1.19   thorpej 	} else if (t->_next_window != NULL) {
    364  1.19   thorpej 		/*
    365  1.19   thorpej 		 * Give the next window a chance.
    366  1.19   thorpej 		 */
    367  1.70   thorpej 		DMA_COUNT(load_mbuf_next_window);
    368  1.19   thorpej 		error = bus_dmamap_load_mbuf(t->_next_window, map, m0, flags);
    369  1.10   thorpej 	}
    370   1.9   thorpej 	return (error);
    371   1.2   thorpej }
    372   1.2   thorpej 
    373  1.70   thorpej DMA_COUNT_DECL(load_uio);
    374  1.70   thorpej DMA_COUNT_DECL(load_uio_next_window);
    375  1.70   thorpej 
    376   1.2   thorpej /*
    377  1.42   thorpej  * Like _bus_dmamap_load_direct(), but for uios.
    378   1.2   thorpej  */
    379   1.2   thorpej int
    380  1.41   thorpej _bus_dmamap_load_uio_direct(bus_dma_tag_t t, bus_dmamap_t map,
    381  1.41   thorpej     struct uio *uio, int flags)
    382   1.2   thorpej {
    383  1.25   thorpej 	paddr_t lastaddr;
    384  1.24   thorpej 	int seg, i, error, first;
    385  1.24   thorpej 	bus_size_t minlen, resid;
    386  1.62      yamt 	struct vmspace *vm;
    387  1.24   thorpej 	struct iovec *iov;
    388  1.63  christos 	void *addr;
    389  1.24   thorpej 
    390  1.24   thorpej 	/*
    391  1.24   thorpej 	 * Make sure that on error condition we return "no valid mappings."
    392  1.24   thorpej 	 */
    393  1.24   thorpej 	map->dm_mapsize = 0;
    394  1.24   thorpej 	map->dm_nsegs = 0;
    395  1.57      matt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    396  1.59    mhitch 	KASSERT((map->_dm_flags & (BUS_DMA_READ|BUS_DMA_WRITE)) == 0);
    397  1.24   thorpej 
    398  1.24   thorpej 	resid = uio->uio_resid;
    399  1.24   thorpej 	iov = uio->uio_iov;
    400  1.24   thorpej 
    401  1.62      yamt 	vm = uio->uio_vmspace;
    402  1.24   thorpej 
    403  1.24   thorpej 	first = 1;
    404  1.24   thorpej 	seg = 0;
    405  1.24   thorpej 	error = 0;
    406  1.24   thorpej 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    407  1.24   thorpej 		/*
    408  1.24   thorpej 		 * Now at the first iovec to load.  Load each iovec
    409  1.24   thorpej 		 * until we have exhausted the residual count.
    410  1.24   thorpej 		 */
    411  1.27   thorpej 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    412  1.63  christos 		addr = (void *)iov[i].iov_base;
    413  1.24   thorpej 
    414  1.43   thorpej 		error = _bus_dmamap_load_buffer_direct(t, map,
    415  1.62      yamt 		    addr, minlen, vm, flags, &lastaddr, &seg, first);
    416  1.24   thorpej 		first = 0;
    417  1.24   thorpej 
    418  1.24   thorpej 		resid -= minlen;
    419  1.24   thorpej 	}
    420  1.24   thorpej 	if (error == 0) {
    421  1.70   thorpej 		DMA_COUNT(load_uio);
    422  1.24   thorpej 		map->dm_mapsize = uio->uio_resid;
    423  1.24   thorpej 		map->dm_nsegs = seg + 1;
    424  1.49   thorpej 		map->_dm_window = t;
    425  1.24   thorpej 	} else if (t->_next_window != NULL) {
    426  1.24   thorpej 		/*
    427  1.24   thorpej 		 * Give the next window a chance.
    428  1.24   thorpej 		 */
    429  1.70   thorpej 		DMA_COUNT(load_uio_next_window);
    430  1.24   thorpej 		error = bus_dmamap_load_uio(t->_next_window, map, uio, flags);
    431  1.24   thorpej 	}
    432  1.24   thorpej 	return (error);
    433   1.2   thorpej }
    434   1.2   thorpej 
    435   1.2   thorpej /*
    436  1.42   thorpej  * Like _bus_dmamap_load_direct(), but for raw memory.
    437   1.2   thorpej  */
    438   1.2   thorpej int
    439  1.41   thorpej _bus_dmamap_load_raw_direct(bus_dma_tag_t t, bus_dmamap_t map,
    440  1.41   thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    441   1.2   thorpej {
    442   1.2   thorpej 
    443  1.18   thorpej 	panic("_bus_dmamap_load_raw_direct: not implemented");
    444   1.2   thorpej }
    445   1.2   thorpej 
    446   1.2   thorpej /*
    447   1.2   thorpej  * Common function for unloading a DMA map.  May be called by
    448   1.2   thorpej  * chipset-specific DMA map unload functions.
    449   1.2   thorpej  */
    450   1.2   thorpej void
    451  1.70   thorpej _bus_dmamap_unload_common(bus_dma_tag_t t, bus_dmamap_t map)
    452   1.2   thorpej {
    453   1.2   thorpej 
    454   1.2   thorpej 	/*
    455   1.2   thorpej 	 * No resources to free; just mark the mappings as
    456   1.2   thorpej 	 * invalid.
    457   1.2   thorpej 	 */
    458  1.57      matt 	map->dm_maxsegsz = map->_dm_maxmaxsegsz;
    459  1.10   thorpej 	map->dm_mapsize = 0;
    460   1.2   thorpej 	map->dm_nsegs = 0;
    461  1.49   thorpej 	map->_dm_window = NULL;
    462  1.59    mhitch 	map->_dm_flags &= ~(BUS_DMA_READ|BUS_DMA_WRITE);
    463   1.2   thorpej }
    464   1.2   thorpej 
    465  1.70   thorpej DMA_COUNT_DECL(unload);
    466  1.70   thorpej 
    467  1.70   thorpej void
    468  1.70   thorpej _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    469  1.70   thorpej {
    470  1.70   thorpej 	KASSERT(map->_dm_window == t);
    471  1.70   thorpej 	DMA_COUNT(unload);
    472  1.70   thorpej 	_bus_dmamap_unload_common(t, map);
    473  1.70   thorpej }
    474  1.70   thorpej 
    475   1.2   thorpej /*
    476   1.2   thorpej  * Common function for DMA map synchronization.  May be called
    477   1.2   thorpej  * by chipset-specific DMA map synchronization functions.
    478   1.2   thorpej  */
    479   1.2   thorpej void
    480  1.41   thorpej _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    481  1.41   thorpej     bus_size_t len, int ops)
    482   1.2   thorpej {
    483   1.2   thorpej 
    484  1.13   thorpej 	alpha_mb();
    485   1.2   thorpej }
    486   1.2   thorpej 
    487   1.2   thorpej /*
    488   1.2   thorpej  * Common function for DMA-safe memory allocation.  May be called
    489   1.2   thorpej  * by bus-specific DMA memory allocation functions.
    490   1.2   thorpej  */
    491   1.2   thorpej int
    492  1.41   thorpej _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    493  1.41   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    494  1.41   thorpej     int flags)
    495   1.2   thorpej {
    496  1.34   thorpej 
    497  1.34   thorpej 	return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    498  1.34   thorpej 	    segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
    499  1.34   thorpej }
    500  1.34   thorpej 
    501  1.34   thorpej /*
    502  1.34   thorpej  * Allocate physical memory from the given physical address range.
    503  1.34   thorpej  * Called by DMA-safe memory allocation methods.
    504  1.34   thorpej  */
    505  1.34   thorpej int
    506  1.41   thorpej _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    507  1.41   thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    508  1.41   thorpej     int flags, paddr_t low, paddr_t high)
    509  1.34   thorpej {
    510   1.2   thorpej 
    511  1.67   thorpej 	return (_bus_dmamem_alloc_range_common(t, size, alignment, boundary,
    512  1.67   thorpej 					       segs, nsegs, rsegs, flags,
    513  1.67   thorpej 					       low, high));
    514   1.2   thorpej }
    515   1.2   thorpej 
    516   1.2   thorpej /*
    517   1.2   thorpej  * Common function for freeing DMA-safe memory.  May be called by
    518   1.2   thorpej  * bus-specific DMA memory free functions.
    519   1.2   thorpej  */
    520   1.2   thorpej void
    521  1.41   thorpej _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    522   1.2   thorpej {
    523   1.2   thorpej 
    524  1.67   thorpej 	_bus_dmamem_free_common(t, segs, nsegs);
    525   1.2   thorpej }
    526   1.2   thorpej 
    527   1.2   thorpej /*
    528   1.2   thorpej  * Common function for mapping DMA-safe memory.  May be called by
    529   1.2   thorpej  * bus-specific DMA memory map functions.
    530   1.2   thorpej  */
    531   1.2   thorpej int
    532  1.41   thorpej _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    533  1.63  christos     size_t size, void **kvap, int flags)
    534   1.2   thorpej {
    535   1.2   thorpej 
    536   1.8   thorpej 	/*
    537   1.8   thorpej 	 * If we're only mapping 1 segment, use K0SEG, to avoid
    538   1.8   thorpej 	 * TLB thrashing.
    539   1.8   thorpej 	 */
    540   1.8   thorpej 	if (nsegs == 1) {
    541  1.63  christos 		*kvap = (void *)ALPHA_PHYS_TO_K0SEG(segs[0].ds_addr);
    542   1.8   thorpej 		return (0);
    543   1.8   thorpej 	}
    544   1.8   thorpej 
    545  1.67   thorpej 	return (_bus_dmamem_map_common(t, segs, nsegs, size, kvap, flags, 0));
    546   1.2   thorpej }
    547   1.2   thorpej 
    548   1.2   thorpej /*
    549   1.2   thorpej  * Common function for unmapping DMA-safe memory.  May be called by
    550   1.2   thorpej  * bus-specific DMA memory unmapping functions.
    551   1.2   thorpej  */
    552   1.2   thorpej void
    553  1.63  christos _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
    554   1.2   thorpej {
    555   1.2   thorpej 
    556   1.8   thorpej 	/*
    557   1.8   thorpej 	 * Nothing to do if we mapped it with K0SEG.
    558   1.8   thorpej 	 */
    559  1.63  christos 	if (kva >= (void *)ALPHA_K0SEG_BASE &&
    560  1.63  christos 	    kva <= (void *)ALPHA_K0SEG_END)
    561   1.8   thorpej 		return;
    562   1.2   thorpej 
    563  1.67   thorpej 	_bus_dmamem_unmap_common(t, kva, size);
    564   1.2   thorpej }
    565   1.2   thorpej 
    566   1.2   thorpej /*
    567  1.74    andvar  * Common function for mmap(2)'ing DMA-safe memory.  May be called by
    568   1.2   thorpej  * bus-specific DMA mmap(2)'ing functions.
    569   1.2   thorpej  */
    570  1.37    simonb paddr_t
    571  1.41   thorpej _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    572  1.41   thorpej     off_t off, int prot, int flags)
    573   1.2   thorpej {
    574  1.67   thorpej 	bus_addr_t rv;
    575   1.2   thorpej 
    576  1.67   thorpej 	rv = _bus_dmamem_mmap_common(t, segs, nsegs, off, prot, flags);
    577  1.67   thorpej 	if (rv == (bus_addr_t)-1)
    578  1.67   thorpej 		return (-1);
    579   1.6   thorpej 
    580  1.67   thorpej 	return (alpha_btop((char *)rv));
    581   1.2   thorpej }
    582