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