Home | History | Annotate | Line # | Download | only in vax
bus_dma.c revision 1.13.4.2
      1  1.13.4.2  nathanw /*	$NetBSD: bus_dma.c,v 1.13.4.2 2002/06/20 03:42:18 nathanw Exp $	*/
      2  1.13.4.2  nathanw 
      3  1.13.4.2  nathanw /*-
      4  1.13.4.2  nathanw  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      5  1.13.4.2  nathanw  * All rights reserved.
      6  1.13.4.2  nathanw  *
      7  1.13.4.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13.4.2  nathanw  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.13.4.2  nathanw  * NASA Ames Research Center.
     10  1.13.4.2  nathanw  *
     11  1.13.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
     12  1.13.4.2  nathanw  * modification, are permitted provided that the following conditions
     13  1.13.4.2  nathanw  * are met:
     14  1.13.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     15  1.13.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     16  1.13.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.13.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     18  1.13.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     19  1.13.4.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     20  1.13.4.2  nathanw  *    must display the following acknowledgement:
     21  1.13.4.2  nathanw  *	This product includes software developed by the NetBSD
     22  1.13.4.2  nathanw  *	Foundation, Inc. and its contributors.
     23  1.13.4.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.13.4.2  nathanw  *    contributors may be used to endorse or promote products derived
     25  1.13.4.2  nathanw  *    from this software without specific prior written permission.
     26  1.13.4.2  nathanw  *
     27  1.13.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.13.4.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.13.4.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.13.4.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.13.4.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.13.4.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.13.4.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.13.4.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.13.4.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.13.4.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.13.4.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     38  1.13.4.2  nathanw  */
     39  1.13.4.2  nathanw /*
     40  1.13.4.2  nathanw  * bus_dma routines for vax. File copied from arm32/bus_dma.c.
     41  1.13.4.2  nathanw  * NetBSD: bus_dma.c,v 1.11 1998/09/21 22:53:35 thorpej Exp
     42  1.13.4.2  nathanw  */
     43  1.13.4.2  nathanw 
     44  1.13.4.2  nathanw #include <sys/param.h>
     45  1.13.4.2  nathanw #include <sys/systm.h>
     46  1.13.4.2  nathanw #include <sys/kernel.h>
     47  1.13.4.2  nathanw #include <sys/map.h>
     48  1.13.4.2  nathanw #include <sys/proc.h>
     49  1.13.4.2  nathanw #include <sys/buf.h>
     50  1.13.4.2  nathanw #include <sys/reboot.h>
     51  1.13.4.2  nathanw #include <sys/conf.h>
     52  1.13.4.2  nathanw #include <sys/file.h>
     53  1.13.4.2  nathanw #include <sys/malloc.h>
     54  1.13.4.2  nathanw #include <sys/mbuf.h>
     55  1.13.4.2  nathanw #include <sys/vnode.h>
     56  1.13.4.2  nathanw #include <sys/device.h>
     57  1.13.4.2  nathanw 
     58  1.13.4.2  nathanw #include <uvm/uvm_extern.h>
     59  1.13.4.2  nathanw 
     60  1.13.4.2  nathanw #define _VAX_BUS_DMA_PRIVATE
     61  1.13.4.2  nathanw #include <machine/bus.h>
     62  1.13.4.2  nathanw 
     63  1.13.4.2  nathanw #include <machine/ka43.h>
     64  1.13.4.2  nathanw #include <machine/sid.h>
     65  1.13.4.2  nathanw 
     66  1.13.4.2  nathanw extern	vaddr_t avail_start, avail_end, virtual_avail;
     67  1.13.4.2  nathanw 
     68  1.13.4.2  nathanw int	_bus_dmamap_load_buffer __P((bus_dma_tag_t, bus_dmamap_t, void *,
     69  1.13.4.2  nathanw 	    bus_size_t, struct proc *, int, vaddr_t *, int *, int));
     70  1.13.4.2  nathanw int	_bus_dma_inrange __P((bus_dma_segment_t *, int, bus_addr_t));
     71  1.13.4.2  nathanw int	_bus_dmamem_alloc_range __P((bus_dma_tag_t, bus_size_t, bus_size_t,
     72  1.13.4.2  nathanw 	    bus_size_t, bus_dma_segment_t*, int, int *, int, vaddr_t, vaddr_t));
     73  1.13.4.2  nathanw /*
     74  1.13.4.2  nathanw  * Common function for DMA map creation.  May be called by bus-specific
     75  1.13.4.2  nathanw  * DMA map creation functions.
     76  1.13.4.2  nathanw  */
     77  1.13.4.2  nathanw int
     78  1.13.4.2  nathanw _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
     79  1.13.4.2  nathanw 	bus_dma_tag_t t;
     80  1.13.4.2  nathanw 	bus_size_t size;
     81  1.13.4.2  nathanw 	int nsegments;
     82  1.13.4.2  nathanw 	bus_size_t maxsegsz;
     83  1.13.4.2  nathanw 	bus_size_t boundary;
     84  1.13.4.2  nathanw 	int flags;
     85  1.13.4.2  nathanw 	bus_dmamap_t *dmamp;
     86  1.13.4.2  nathanw {
     87  1.13.4.2  nathanw 	struct vax_bus_dmamap *map;
     88  1.13.4.2  nathanw 	void *mapstore;
     89  1.13.4.2  nathanw 	size_t mapsize;
     90  1.13.4.2  nathanw 
     91  1.13.4.2  nathanw #ifdef DEBUG_DMA
     92  1.13.4.2  nathanw 	printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
     93  1.13.4.2  nathanw 	    t, size, nsegments, maxsegsz, boundary, flags);
     94  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
     95  1.13.4.2  nathanw 
     96  1.13.4.2  nathanw 	/*
     97  1.13.4.2  nathanw 	 * Allocate and initialize the DMA map.  The end of the map
     98  1.13.4.2  nathanw 	 * is a variable-sized array of segments, so we allocate enough
     99  1.13.4.2  nathanw 	 * room for them in one shot.
    100  1.13.4.2  nathanw 	 *
    101  1.13.4.2  nathanw 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
    102  1.13.4.2  nathanw 	 * of ALLOCNOW notifies others that we've reserved these resources,
    103  1.13.4.2  nathanw 	 * and they are not to be freed.
    104  1.13.4.2  nathanw 	 *
    105  1.13.4.2  nathanw 	 * The bus_dmamap_t includes one bus_dma_segment_t, hence
    106  1.13.4.2  nathanw 	 * the (nsegments - 1).
    107  1.13.4.2  nathanw 	 */
    108  1.13.4.2  nathanw 	mapsize = sizeof(struct vax_bus_dmamap) +
    109  1.13.4.2  nathanw 	    (sizeof(bus_dma_segment_t) * (nsegments - 1));
    110  1.13.4.2  nathanw 	if ((mapstore = malloc(mapsize, M_DMAMAP,
    111  1.13.4.2  nathanw 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
    112  1.13.4.2  nathanw 		return (ENOMEM);
    113  1.13.4.2  nathanw 
    114  1.13.4.2  nathanw 	bzero(mapstore, mapsize);
    115  1.13.4.2  nathanw 	map = (struct vax_bus_dmamap *)mapstore;
    116  1.13.4.2  nathanw 	map->_dm_size = size;
    117  1.13.4.2  nathanw 	map->_dm_segcnt = nsegments;
    118  1.13.4.2  nathanw 	map->_dm_maxsegsz = maxsegsz;
    119  1.13.4.2  nathanw 	map->_dm_boundary = boundary;
    120  1.13.4.2  nathanw 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    121  1.13.4.2  nathanw 	map->dm_mapsize = 0;		/* no valid mappings */
    122  1.13.4.2  nathanw 	map->dm_nsegs = 0;
    123  1.13.4.2  nathanw 
    124  1.13.4.2  nathanw 	*dmamp = map;
    125  1.13.4.2  nathanw #ifdef DEBUG_DMA
    126  1.13.4.2  nathanw 	printf("dmamap_create:map=%p\n", map);
    127  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    128  1.13.4.2  nathanw 	return (0);
    129  1.13.4.2  nathanw }
    130  1.13.4.2  nathanw 
    131  1.13.4.2  nathanw /*
    132  1.13.4.2  nathanw  * Common function for DMA map destruction.  May be called by bus-specific
    133  1.13.4.2  nathanw  * DMA map destruction functions.
    134  1.13.4.2  nathanw  */
    135  1.13.4.2  nathanw void
    136  1.13.4.2  nathanw _bus_dmamap_destroy(t, map)
    137  1.13.4.2  nathanw 	bus_dma_tag_t t;
    138  1.13.4.2  nathanw 	bus_dmamap_t map;
    139  1.13.4.2  nathanw {
    140  1.13.4.2  nathanw 
    141  1.13.4.2  nathanw #ifdef DEBUG_DMA
    142  1.13.4.2  nathanw 	printf("dmamap_destroy: t=%p map=%p\n", t, map);
    143  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    144  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    145  1.13.4.2  nathanw 	if (map->dm_nsegs > 0)
    146  1.13.4.2  nathanw 		printf("bus_dmamap_destroy() called for map with valid mappings\n");
    147  1.13.4.2  nathanw #endif	/* DIAGNOSTIC */
    148  1.13.4.2  nathanw 	free(map, M_DEVBUF);
    149  1.13.4.2  nathanw }
    150  1.13.4.2  nathanw 
    151  1.13.4.2  nathanw /*
    152  1.13.4.2  nathanw  * Common function for loading a DMA map with a linear buffer.  May
    153  1.13.4.2  nathanw  * be called by bus-specific DMA map load functions.
    154  1.13.4.2  nathanw  */
    155  1.13.4.2  nathanw int
    156  1.13.4.2  nathanw _bus_dmamap_load(t, map, buf, buflen, p, flags)
    157  1.13.4.2  nathanw 	bus_dma_tag_t t;
    158  1.13.4.2  nathanw 	bus_dmamap_t map;
    159  1.13.4.2  nathanw 	void *buf;
    160  1.13.4.2  nathanw 	bus_size_t buflen;
    161  1.13.4.2  nathanw 	struct proc *p;
    162  1.13.4.2  nathanw 	int flags;
    163  1.13.4.2  nathanw {
    164  1.13.4.2  nathanw 	vaddr_t lastaddr;
    165  1.13.4.2  nathanw 	int seg, error;
    166  1.13.4.2  nathanw 
    167  1.13.4.2  nathanw #ifdef DEBUG_DMA
    168  1.13.4.2  nathanw 	printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
    169  1.13.4.2  nathanw 	    t, map, buf, buflen, p, flags);
    170  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    171  1.13.4.2  nathanw 
    172  1.13.4.2  nathanw 	/*
    173  1.13.4.2  nathanw 	 * Make sure that on error condition we return "no valid mappings".
    174  1.13.4.2  nathanw 	 */
    175  1.13.4.2  nathanw 	map->dm_mapsize = 0;
    176  1.13.4.2  nathanw 	map->dm_nsegs = 0;
    177  1.13.4.2  nathanw 
    178  1.13.4.2  nathanw 	if (buflen > map->_dm_size)
    179  1.13.4.2  nathanw 		return (EINVAL);
    180  1.13.4.2  nathanw 
    181  1.13.4.2  nathanw 	seg = 0;
    182  1.13.4.2  nathanw 	error = _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags,
    183  1.13.4.2  nathanw 	    &lastaddr, &seg, 1);
    184  1.13.4.2  nathanw 	if (error == 0) {
    185  1.13.4.2  nathanw 		map->dm_mapsize = buflen;
    186  1.13.4.2  nathanw 		map->dm_nsegs = seg + 1;
    187  1.13.4.2  nathanw 	}
    188  1.13.4.2  nathanw #ifdef DEBUG_DMA
    189  1.13.4.2  nathanw 	printf("dmamap_load: error=%d\n", error);
    190  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    191  1.13.4.2  nathanw 	return (error);
    192  1.13.4.2  nathanw }
    193  1.13.4.2  nathanw 
    194  1.13.4.2  nathanw /*
    195  1.13.4.2  nathanw  * Like _bus_dmamap_load(), but for mbufs.
    196  1.13.4.2  nathanw  */
    197  1.13.4.2  nathanw int
    198  1.13.4.2  nathanw _bus_dmamap_load_mbuf(t, map, m0, flags)
    199  1.13.4.2  nathanw 	bus_dma_tag_t t;
    200  1.13.4.2  nathanw 	bus_dmamap_t map;
    201  1.13.4.2  nathanw 	struct mbuf *m0;
    202  1.13.4.2  nathanw 	int flags;
    203  1.13.4.2  nathanw {
    204  1.13.4.2  nathanw 	vaddr_t lastaddr;
    205  1.13.4.2  nathanw 	int seg, error, first;
    206  1.13.4.2  nathanw 	struct mbuf *m;
    207  1.13.4.2  nathanw 
    208  1.13.4.2  nathanw #ifdef DEBUG_DMA
    209  1.13.4.2  nathanw 	printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
    210  1.13.4.2  nathanw 	    t, map, m0, flags);
    211  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    212  1.13.4.2  nathanw 
    213  1.13.4.2  nathanw 	/*
    214  1.13.4.2  nathanw 	 * Make sure that on error condition we return "no valid mappings."
    215  1.13.4.2  nathanw 	 */
    216  1.13.4.2  nathanw 	map->dm_mapsize = 0;
    217  1.13.4.2  nathanw 	map->dm_nsegs = 0;
    218  1.13.4.2  nathanw 
    219  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    220  1.13.4.2  nathanw 	if ((m0->m_flags & M_PKTHDR) == 0)
    221  1.13.4.2  nathanw 		panic("_bus_dmamap_load_mbuf: no packet header");
    222  1.13.4.2  nathanw #endif	/* DIAGNOSTIC */
    223  1.13.4.2  nathanw 
    224  1.13.4.2  nathanw 	if (m0->m_pkthdr.len > map->_dm_size)
    225  1.13.4.2  nathanw 		return (EINVAL);
    226  1.13.4.2  nathanw 
    227  1.13.4.2  nathanw 	first = 1;
    228  1.13.4.2  nathanw 	seg = 0;
    229  1.13.4.2  nathanw 	error = 0;
    230  1.13.4.2  nathanw 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
    231  1.13.4.2  nathanw 		error = _bus_dmamap_load_buffer(t, map, m->m_data, m->m_len,
    232  1.13.4.2  nathanw 		    NULL, flags, &lastaddr, &seg, first);
    233  1.13.4.2  nathanw 		first = 0;
    234  1.13.4.2  nathanw 	}
    235  1.13.4.2  nathanw 	if (error == 0) {
    236  1.13.4.2  nathanw 		map->dm_mapsize = m0->m_pkthdr.len;
    237  1.13.4.2  nathanw 		map->dm_nsegs = seg + 1;
    238  1.13.4.2  nathanw 	}
    239  1.13.4.2  nathanw #ifdef DEBUG_DMA
    240  1.13.4.2  nathanw 	printf("dmamap_load_mbuf: error=%d\n", error);
    241  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    242  1.13.4.2  nathanw 	return (error);
    243  1.13.4.2  nathanw }
    244  1.13.4.2  nathanw 
    245  1.13.4.2  nathanw /*
    246  1.13.4.2  nathanw  * Like _bus_dmamap_load(), but for uios.
    247  1.13.4.2  nathanw  */
    248  1.13.4.2  nathanw int
    249  1.13.4.2  nathanw _bus_dmamap_load_uio(t, map, uio, flags)
    250  1.13.4.2  nathanw 	bus_dma_tag_t t;
    251  1.13.4.2  nathanw 	bus_dmamap_t map;
    252  1.13.4.2  nathanw 	struct uio *uio;
    253  1.13.4.2  nathanw 	int flags;
    254  1.13.4.2  nathanw {
    255  1.13.4.2  nathanw 	vaddr_t lastaddr;
    256  1.13.4.2  nathanw 	int seg, i, error, first;
    257  1.13.4.2  nathanw 	bus_size_t minlen, resid;
    258  1.13.4.2  nathanw 	struct proc *p = NULL;
    259  1.13.4.2  nathanw 	struct iovec *iov;
    260  1.13.4.2  nathanw 	caddr_t addr;
    261  1.13.4.2  nathanw 
    262  1.13.4.2  nathanw 	/*
    263  1.13.4.2  nathanw 	 * Make sure that on error condition we return "no valid mappings."
    264  1.13.4.2  nathanw 	 */
    265  1.13.4.2  nathanw 	map->dm_mapsize = 0;
    266  1.13.4.2  nathanw 	map->dm_nsegs = 0;
    267  1.13.4.2  nathanw 
    268  1.13.4.2  nathanw 	resid = uio->uio_resid;
    269  1.13.4.2  nathanw 	iov = uio->uio_iov;
    270  1.13.4.2  nathanw 
    271  1.13.4.2  nathanw 	if (uio->uio_segflg == UIO_USERSPACE) {
    272  1.13.4.2  nathanw 		p = uio->uio_procp;
    273  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    274  1.13.4.2  nathanw 		if (p == NULL)
    275  1.13.4.2  nathanw 			panic("_bus_dmamap_load_uio: USERSPACE but no proc");
    276  1.13.4.2  nathanw #endif
    277  1.13.4.2  nathanw 	}
    278  1.13.4.2  nathanw 
    279  1.13.4.2  nathanw 	first = 1;
    280  1.13.4.2  nathanw 	seg = 0;
    281  1.13.4.2  nathanw 	error = 0;
    282  1.13.4.2  nathanw 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
    283  1.13.4.2  nathanw 		/*
    284  1.13.4.2  nathanw 		 * Now at the first iovec to load.  Load each iovec
    285  1.13.4.2  nathanw 		 * until we have exhausted the residual count.
    286  1.13.4.2  nathanw 		 */
    287  1.13.4.2  nathanw 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
    288  1.13.4.2  nathanw 		addr = (caddr_t)iov[i].iov_base;
    289  1.13.4.2  nathanw 
    290  1.13.4.2  nathanw 		error = _bus_dmamap_load_buffer(t, map, addr, minlen,
    291  1.13.4.2  nathanw 		    p, flags, &lastaddr, &seg, first);
    292  1.13.4.2  nathanw 		first = 0;
    293  1.13.4.2  nathanw 
    294  1.13.4.2  nathanw 		resid -= minlen;
    295  1.13.4.2  nathanw 	}
    296  1.13.4.2  nathanw 	if (error == 0) {
    297  1.13.4.2  nathanw 		map->dm_mapsize = uio->uio_resid;
    298  1.13.4.2  nathanw 		map->dm_nsegs = seg + 1;
    299  1.13.4.2  nathanw 	}
    300  1.13.4.2  nathanw 	return (error);
    301  1.13.4.2  nathanw }
    302  1.13.4.2  nathanw 
    303  1.13.4.2  nathanw /*
    304  1.13.4.2  nathanw  * Like _bus_dmamap_load(), but for raw memory allocated with
    305  1.13.4.2  nathanw  * bus_dmamem_alloc().
    306  1.13.4.2  nathanw  */
    307  1.13.4.2  nathanw int
    308  1.13.4.2  nathanw _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    309  1.13.4.2  nathanw 	bus_dma_tag_t t;
    310  1.13.4.2  nathanw 	bus_dmamap_t map;
    311  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    312  1.13.4.2  nathanw 	int nsegs;
    313  1.13.4.2  nathanw 	bus_size_t size;
    314  1.13.4.2  nathanw 	int flags;
    315  1.13.4.2  nathanw {
    316  1.13.4.2  nathanw 
    317  1.13.4.2  nathanw 	panic("_bus_dmamap_load_raw: not implemented");
    318  1.13.4.2  nathanw }
    319  1.13.4.2  nathanw 
    320  1.13.4.2  nathanw /*
    321  1.13.4.2  nathanw  * Common function for unloading a DMA map.  May be called by
    322  1.13.4.2  nathanw  * bus-specific DMA map unload functions.
    323  1.13.4.2  nathanw  */
    324  1.13.4.2  nathanw void
    325  1.13.4.2  nathanw _bus_dmamap_unload(t, map)
    326  1.13.4.2  nathanw 	bus_dma_tag_t t;
    327  1.13.4.2  nathanw 	bus_dmamap_t map;
    328  1.13.4.2  nathanw {
    329  1.13.4.2  nathanw 
    330  1.13.4.2  nathanw #ifdef DEBUG_DMA
    331  1.13.4.2  nathanw 	printf("dmamap_unload: t=%p map=%p\n", t, map);
    332  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    333  1.13.4.2  nathanw 
    334  1.13.4.2  nathanw 	/*
    335  1.13.4.2  nathanw 	 * No resources to free; just mark the mappings as
    336  1.13.4.2  nathanw 	 * invalid.
    337  1.13.4.2  nathanw 	 */
    338  1.13.4.2  nathanw 	map->dm_mapsize = 0;
    339  1.13.4.2  nathanw 	map->dm_nsegs = 0;
    340  1.13.4.2  nathanw }
    341  1.13.4.2  nathanw 
    342  1.13.4.2  nathanw /*
    343  1.13.4.2  nathanw  * Common function for DMA map synchronization.  May be called
    344  1.13.4.2  nathanw  * by bus-specific DMA map synchronization functions.
    345  1.13.4.2  nathanw  */
    346  1.13.4.2  nathanw void
    347  1.13.4.2  nathanw _bus_dmamap_sync(t, map, offset, len, ops)
    348  1.13.4.2  nathanw 	bus_dma_tag_t t;
    349  1.13.4.2  nathanw 	bus_dmamap_t map;
    350  1.13.4.2  nathanw 	bus_addr_t offset;
    351  1.13.4.2  nathanw 	bus_size_t len;
    352  1.13.4.2  nathanw 	int ops;
    353  1.13.4.2  nathanw {
    354  1.13.4.2  nathanw #ifdef DEBUG_DMA
    355  1.13.4.2  nathanw 	printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
    356  1.13.4.2  nathanw 	    t, map, offset, len, ops);
    357  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    358  1.13.4.2  nathanw 	/*
    359  1.13.4.2  nathanw 	 * A vax only has snoop-cache, so this routine is a no-op.
    360  1.13.4.2  nathanw 	 */
    361  1.13.4.2  nathanw 	return;
    362  1.13.4.2  nathanw }
    363  1.13.4.2  nathanw 
    364  1.13.4.2  nathanw /*
    365  1.13.4.2  nathanw  * Common function for DMA-safe memory allocation.  May be called
    366  1.13.4.2  nathanw  * by bus-specific DMA memory allocation functions.
    367  1.13.4.2  nathanw  */
    368  1.13.4.2  nathanw 
    369  1.13.4.2  nathanw int
    370  1.13.4.2  nathanw _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    371  1.13.4.2  nathanw 	bus_dma_tag_t t;
    372  1.13.4.2  nathanw 	bus_size_t size, alignment, boundary;
    373  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    374  1.13.4.2  nathanw 	int nsegs;
    375  1.13.4.2  nathanw 	int *rsegs;
    376  1.13.4.2  nathanw 	int flags;
    377  1.13.4.2  nathanw {
    378  1.13.4.2  nathanw 	int error;
    379  1.13.4.2  nathanw 
    380  1.13.4.2  nathanw 	error =  (_bus_dmamem_alloc_range(t, size, alignment, boundary,
    381  1.13.4.2  nathanw 	    segs, nsegs, rsegs, flags, round_page(avail_start),
    382  1.13.4.2  nathanw 	    trunc_page(avail_end)));
    383  1.13.4.2  nathanw 	return(error);
    384  1.13.4.2  nathanw }
    385  1.13.4.2  nathanw 
    386  1.13.4.2  nathanw /*
    387  1.13.4.2  nathanw  * Common function for freeing DMA-safe memory.  May be called by
    388  1.13.4.2  nathanw  * bus-specific DMA memory free functions.
    389  1.13.4.2  nathanw  */
    390  1.13.4.2  nathanw void
    391  1.13.4.2  nathanw _bus_dmamem_free(t, segs, nsegs)
    392  1.13.4.2  nathanw 	bus_dma_tag_t t;
    393  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    394  1.13.4.2  nathanw 	int nsegs;
    395  1.13.4.2  nathanw {
    396  1.13.4.2  nathanw 	struct vm_page *m;
    397  1.13.4.2  nathanw 	bus_addr_t addr;
    398  1.13.4.2  nathanw 	struct pglist mlist;
    399  1.13.4.2  nathanw 	int curseg;
    400  1.13.4.2  nathanw 
    401  1.13.4.2  nathanw #ifdef DEBUG_DMA
    402  1.13.4.2  nathanw 	printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
    403  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    404  1.13.4.2  nathanw 
    405  1.13.4.2  nathanw 	/*
    406  1.13.4.2  nathanw 	 * Build a list of pages to free back to the VM system.
    407  1.13.4.2  nathanw 	 */
    408  1.13.4.2  nathanw 	TAILQ_INIT(&mlist);
    409  1.13.4.2  nathanw 	for (curseg = 0; curseg < nsegs; curseg++) {
    410  1.13.4.2  nathanw 		for (addr = segs[curseg].ds_addr;
    411  1.13.4.2  nathanw 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    412  1.13.4.2  nathanw 		    addr += PAGE_SIZE) {
    413  1.13.4.2  nathanw 			m = PHYS_TO_VM_PAGE(addr);
    414  1.13.4.2  nathanw 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    415  1.13.4.2  nathanw 		}
    416  1.13.4.2  nathanw 	}
    417  1.13.4.2  nathanw 	uvm_pglistfree(&mlist);
    418  1.13.4.2  nathanw }
    419  1.13.4.2  nathanw 
    420  1.13.4.2  nathanw /*
    421  1.13.4.2  nathanw  * Common function for mapping DMA-safe memory.  May be called by
    422  1.13.4.2  nathanw  * bus-specific DMA memory map functions.
    423  1.13.4.2  nathanw  */
    424  1.13.4.2  nathanw int
    425  1.13.4.2  nathanw _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
    426  1.13.4.2  nathanw 	bus_dma_tag_t t;
    427  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    428  1.13.4.2  nathanw 	int nsegs;
    429  1.13.4.2  nathanw 	size_t size;
    430  1.13.4.2  nathanw 	caddr_t *kvap;
    431  1.13.4.2  nathanw 	int flags;
    432  1.13.4.2  nathanw {
    433  1.13.4.2  nathanw 	vaddr_t va;
    434  1.13.4.2  nathanw 	bus_addr_t addr;
    435  1.13.4.2  nathanw 	int curseg;
    436  1.13.4.2  nathanw 
    437  1.13.4.2  nathanw 	/*
    438  1.13.4.2  nathanw 	 * Special case (but common):
    439  1.13.4.2  nathanw 	 * If there is only one physical segment then the already-mapped
    440  1.13.4.2  nathanw 	 * virtual address is returned, since all physical memory is already
    441  1.13.4.2  nathanw 	 * in the beginning of kernel virtual memory.
    442  1.13.4.2  nathanw 	 */
    443  1.13.4.2  nathanw 	if (nsegs == 1) {
    444  1.13.4.2  nathanw 		*kvap = (caddr_t)(segs[0].ds_addr | KERNBASE);
    445  1.13.4.2  nathanw 		/*
    446  1.13.4.2  nathanw 		 * KA43 (3100/m76) must have its DMA-safe memory accessed
    447  1.13.4.2  nathanw 		 * through DIAGMEM. Remap it here.
    448  1.13.4.2  nathanw 		 */
    449  1.13.4.2  nathanw 		if (vax_boardtype == VAX_BTYP_43) {
    450  1.13.4.2  nathanw 			pmap_map((vaddr_t)*kvap, segs[0].ds_addr|KA43_DIAGMEM,
    451  1.13.4.2  nathanw 			    (segs[0].ds_addr|KA43_DIAGMEM) + size,
    452  1.13.4.2  nathanw 			    VM_PROT_READ|VM_PROT_WRITE);
    453  1.13.4.2  nathanw 		}
    454  1.13.4.2  nathanw 		return 0;
    455  1.13.4.2  nathanw 	}
    456  1.13.4.2  nathanw 	size = round_page(size);
    457  1.13.4.2  nathanw 	va = uvm_km_valloc(kernel_map, size);
    458  1.13.4.2  nathanw 
    459  1.13.4.2  nathanw 	if (va == 0)
    460  1.13.4.2  nathanw 		return (ENOMEM);
    461  1.13.4.2  nathanw 
    462  1.13.4.2  nathanw 	*kvap = (caddr_t)va;
    463  1.13.4.2  nathanw 
    464  1.13.4.2  nathanw 	for (curseg = 0; curseg < nsegs; curseg++) {
    465  1.13.4.2  nathanw 		for (addr = segs[curseg].ds_addr;
    466  1.13.4.2  nathanw 		    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
    467  1.13.4.2  nathanw 		    addr += NBPG, va += NBPG, size -= NBPG) {
    468  1.13.4.2  nathanw 			if (size == 0)
    469  1.13.4.2  nathanw 				panic("_bus_dmamem_map: size botch");
    470  1.13.4.2  nathanw 			if (vax_boardtype == VAX_BTYP_43)
    471  1.13.4.2  nathanw 				addr |= KA43_DIAGMEM;
    472  1.13.4.2  nathanw 			pmap_enter(pmap_kernel(), va, addr,
    473  1.13.4.2  nathanw 			    VM_PROT_READ | VM_PROT_WRITE,
    474  1.13.4.2  nathanw 			    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
    475  1.13.4.2  nathanw 		}
    476  1.13.4.2  nathanw 	}
    477  1.13.4.2  nathanw 	pmap_update(pmap_kernel());
    478  1.13.4.2  nathanw 	return (0);
    479  1.13.4.2  nathanw }
    480  1.13.4.2  nathanw 
    481  1.13.4.2  nathanw /*
    482  1.13.4.2  nathanw  * Common function for unmapping DMA-safe memory.  May be called by
    483  1.13.4.2  nathanw  * bus-specific DMA memory unmapping functions.
    484  1.13.4.2  nathanw  */
    485  1.13.4.2  nathanw void
    486  1.13.4.2  nathanw _bus_dmamem_unmap(t, kva, size)
    487  1.13.4.2  nathanw 	bus_dma_tag_t t;
    488  1.13.4.2  nathanw 	caddr_t kva;
    489  1.13.4.2  nathanw 	size_t size;
    490  1.13.4.2  nathanw {
    491  1.13.4.2  nathanw 
    492  1.13.4.2  nathanw #ifdef DEBUG_DMA
    493  1.13.4.2  nathanw 	printf("dmamem_unmap: t=%p kva=%p size=%x\n", t, kva, size);
    494  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    495  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    496  1.13.4.2  nathanw 	if ((u_long)kva & PGOFSET)
    497  1.13.4.2  nathanw 		panic("_bus_dmamem_unmap");
    498  1.13.4.2  nathanw #endif	/* DIAGNOSTIC */
    499  1.13.4.2  nathanw 
    500  1.13.4.2  nathanw 	/* Avoid free'ing if not mapped */
    501  1.13.4.2  nathanw 	if (kva >= (caddr_t)virtual_avail)
    502  1.13.4.2  nathanw 		uvm_km_free(kernel_map, (vaddr_t)kva, round_page(size));
    503  1.13.4.2  nathanw }
    504  1.13.4.2  nathanw 
    505  1.13.4.2  nathanw /*
    506  1.13.4.2  nathanw  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    507  1.13.4.2  nathanw  * bus-specific DMA mmap(2)'ing functions.
    508  1.13.4.2  nathanw  */
    509  1.13.4.2  nathanw paddr_t
    510  1.13.4.2  nathanw _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    511  1.13.4.2  nathanw 	bus_dma_tag_t t;
    512  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    513  1.13.4.2  nathanw 	int nsegs;
    514  1.13.4.2  nathanw 	off_t off;
    515  1.13.4.2  nathanw 	int prot, flags;
    516  1.13.4.2  nathanw {
    517  1.13.4.2  nathanw 	int i;
    518  1.13.4.2  nathanw 
    519  1.13.4.2  nathanw 	for (i = 0; i < nsegs; i++) {
    520  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    521  1.13.4.2  nathanw 		if (off & PGOFSET)
    522  1.13.4.2  nathanw 			panic("_bus_dmamem_mmap: offset unaligned");
    523  1.13.4.2  nathanw 		if (segs[i].ds_addr & PGOFSET)
    524  1.13.4.2  nathanw 			panic("_bus_dmamem_mmap: segment unaligned");
    525  1.13.4.2  nathanw 		if (segs[i].ds_len & PGOFSET)
    526  1.13.4.2  nathanw 			panic("_bus_dmamem_mmap: segment size not multiple"
    527  1.13.4.2  nathanw 			    " of page size");
    528  1.13.4.2  nathanw #endif	/* DIAGNOSTIC */
    529  1.13.4.2  nathanw 		if (off >= segs[i].ds_len) {
    530  1.13.4.2  nathanw 			off -= segs[i].ds_len;
    531  1.13.4.2  nathanw 			continue;
    532  1.13.4.2  nathanw 		}
    533  1.13.4.2  nathanw 
    534  1.13.4.2  nathanw 		return (btop((u_long)segs[i].ds_addr + off));
    535  1.13.4.2  nathanw 	}
    536  1.13.4.2  nathanw 
    537  1.13.4.2  nathanw 	/* Page not found. */
    538  1.13.4.2  nathanw 	return (-1);
    539  1.13.4.2  nathanw }
    540  1.13.4.2  nathanw 
    541  1.13.4.2  nathanw /**********************************************************************
    542  1.13.4.2  nathanw  * DMA utility functions
    543  1.13.4.2  nathanw  **********************************************************************/
    544  1.13.4.2  nathanw 
    545  1.13.4.2  nathanw /*
    546  1.13.4.2  nathanw  * Utility function to load a linear buffer.  lastaddrp holds state
    547  1.13.4.2  nathanw  * between invocations (for multiple-buffer loads).  segp contains
    548  1.13.4.2  nathanw  * the starting segment on entrace, and the ending segment on exit.
    549  1.13.4.2  nathanw  * first indicates if this is the first invocation of this function.
    550  1.13.4.2  nathanw  */
    551  1.13.4.2  nathanw int
    552  1.13.4.2  nathanw _bus_dmamap_load_buffer(t, map, buf, buflen, p, flags, lastaddrp, segp, first)
    553  1.13.4.2  nathanw 	bus_dma_tag_t t;
    554  1.13.4.2  nathanw 	bus_dmamap_t map;
    555  1.13.4.2  nathanw 	void *buf;
    556  1.13.4.2  nathanw 	bus_size_t buflen;
    557  1.13.4.2  nathanw 	struct proc *p;
    558  1.13.4.2  nathanw 	int flags;
    559  1.13.4.2  nathanw 	vaddr_t *lastaddrp;
    560  1.13.4.2  nathanw 	int *segp;
    561  1.13.4.2  nathanw 	int first;
    562  1.13.4.2  nathanw {
    563  1.13.4.2  nathanw 	bus_size_t sgsize;
    564  1.13.4.2  nathanw 	bus_addr_t curaddr, lastaddr, baddr, bmask;
    565  1.13.4.2  nathanw 	vaddr_t vaddr = (vaddr_t)buf;
    566  1.13.4.2  nathanw 	int seg;
    567  1.13.4.2  nathanw 	pmap_t pmap;
    568  1.13.4.2  nathanw 
    569  1.13.4.2  nathanw #ifdef DEBUG_DMA
    570  1.13.4.2  nathanw 	printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
    571  1.13.4.2  nathanw 	    buf, buflen, flags, first);
    572  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    573  1.13.4.2  nathanw 
    574  1.13.4.2  nathanw 	if (p != NULL)
    575  1.13.4.2  nathanw 		pmap = p->p_vmspace->vm_map.pmap;
    576  1.13.4.2  nathanw 	else
    577  1.13.4.2  nathanw 		pmap = pmap_kernel();
    578  1.13.4.2  nathanw 
    579  1.13.4.2  nathanw 	lastaddr = *lastaddrp;
    580  1.13.4.2  nathanw 	bmask  = ~(map->_dm_boundary - 1);
    581  1.13.4.2  nathanw 
    582  1.13.4.2  nathanw 	for (seg = *segp; buflen > 0; ) {
    583  1.13.4.2  nathanw 		/*
    584  1.13.4.2  nathanw 		 * Get the physical address for this segment.
    585  1.13.4.2  nathanw 		 */
    586  1.13.4.2  nathanw 		(void) pmap_extract(pmap, (vaddr_t)vaddr, &curaddr);
    587  1.13.4.2  nathanw 
    588  1.13.4.2  nathanw #if 0
    589  1.13.4.2  nathanw 		/*
    590  1.13.4.2  nathanw 		 * Make sure we're in an allowed DMA range.
    591  1.13.4.2  nathanw 		 */
    592  1.13.4.2  nathanw 		if (t->_ranges != NULL &&
    593  1.13.4.2  nathanw 		    _bus_dma_inrange(t->_ranges, t->_nranges, curaddr) == 0)
    594  1.13.4.2  nathanw 			return (EINVAL);
    595  1.13.4.2  nathanw #endif
    596  1.13.4.2  nathanw 
    597  1.13.4.2  nathanw 		/*
    598  1.13.4.2  nathanw 		 * Compute the segment size, and adjust counts.
    599  1.13.4.2  nathanw 		 */
    600  1.13.4.2  nathanw 		sgsize = NBPG - ((u_long)vaddr & PGOFSET);
    601  1.13.4.2  nathanw 		if (buflen < sgsize)
    602  1.13.4.2  nathanw 			sgsize = buflen;
    603  1.13.4.2  nathanw 
    604  1.13.4.2  nathanw 		/*
    605  1.13.4.2  nathanw 		 * Make sure we don't cross any boundaries.
    606  1.13.4.2  nathanw 		 */
    607  1.13.4.2  nathanw 		if (map->_dm_boundary > 0) {
    608  1.13.4.2  nathanw 			baddr = (curaddr + map->_dm_boundary) & bmask;
    609  1.13.4.2  nathanw 			if (sgsize > (baddr - curaddr))
    610  1.13.4.2  nathanw 				sgsize = (baddr - curaddr);
    611  1.13.4.2  nathanw 		}
    612  1.13.4.2  nathanw 
    613  1.13.4.2  nathanw 		/*
    614  1.13.4.2  nathanw 		 * Insert chunk into a segment, coalescing with
    615  1.13.4.2  nathanw 		 * previous segment if possible.
    616  1.13.4.2  nathanw 		 */
    617  1.13.4.2  nathanw 		if (first) {
    618  1.13.4.2  nathanw 			map->dm_segs[seg].ds_addr = curaddr;
    619  1.13.4.2  nathanw 			map->dm_segs[seg].ds_len = sgsize;
    620  1.13.4.2  nathanw 			first = 0;
    621  1.13.4.2  nathanw 		} else {
    622  1.13.4.2  nathanw 			if (curaddr == lastaddr &&
    623  1.13.4.2  nathanw 			    (map->dm_segs[seg].ds_len + sgsize) <=
    624  1.13.4.2  nathanw 			     map->_dm_maxsegsz &&
    625  1.13.4.2  nathanw 			    (map->_dm_boundary == 0 ||
    626  1.13.4.2  nathanw 			     (map->dm_segs[seg].ds_addr & bmask) ==
    627  1.13.4.2  nathanw 			     (curaddr & bmask)))
    628  1.13.4.2  nathanw 				map->dm_segs[seg].ds_len += sgsize;
    629  1.13.4.2  nathanw 			else {
    630  1.13.4.2  nathanw 				if (++seg >= map->_dm_segcnt)
    631  1.13.4.2  nathanw 					break;
    632  1.13.4.2  nathanw 				map->dm_segs[seg].ds_addr = curaddr;
    633  1.13.4.2  nathanw 				map->dm_segs[seg].ds_len = sgsize;
    634  1.13.4.2  nathanw 			}
    635  1.13.4.2  nathanw 		}
    636  1.13.4.2  nathanw 
    637  1.13.4.2  nathanw 		lastaddr = curaddr + sgsize;
    638  1.13.4.2  nathanw 		vaddr += sgsize;
    639  1.13.4.2  nathanw 		buflen -= sgsize;
    640  1.13.4.2  nathanw 	}
    641  1.13.4.2  nathanw 
    642  1.13.4.2  nathanw 	*segp = seg;
    643  1.13.4.2  nathanw 	*lastaddrp = lastaddr;
    644  1.13.4.2  nathanw 
    645  1.13.4.2  nathanw 	/*
    646  1.13.4.2  nathanw 	 * Did we fit?
    647  1.13.4.2  nathanw 	 */
    648  1.13.4.2  nathanw 	if (buflen != 0)
    649  1.13.4.2  nathanw 		return (EFBIG);		/* XXX better return value here? */
    650  1.13.4.2  nathanw 	return (0);
    651  1.13.4.2  nathanw }
    652  1.13.4.2  nathanw 
    653  1.13.4.2  nathanw /*
    654  1.13.4.2  nathanw  * Check to see if the specified page is in an allowed DMA range.
    655  1.13.4.2  nathanw  */
    656  1.13.4.2  nathanw int
    657  1.13.4.2  nathanw _bus_dma_inrange(ranges, nranges, curaddr)
    658  1.13.4.2  nathanw 	bus_dma_segment_t *ranges;
    659  1.13.4.2  nathanw 	int nranges;
    660  1.13.4.2  nathanw 	bus_addr_t curaddr;
    661  1.13.4.2  nathanw {
    662  1.13.4.2  nathanw 	bus_dma_segment_t *ds;
    663  1.13.4.2  nathanw 	int i;
    664  1.13.4.2  nathanw 
    665  1.13.4.2  nathanw 	for (i = 0, ds = ranges; i < nranges; i++, ds++) {
    666  1.13.4.2  nathanw 		if (curaddr >= ds->ds_addr &&
    667  1.13.4.2  nathanw 		    round_page(curaddr) <= (ds->ds_addr + ds->ds_len))
    668  1.13.4.2  nathanw 			return (1);
    669  1.13.4.2  nathanw 	}
    670  1.13.4.2  nathanw 
    671  1.13.4.2  nathanw 	return (0);
    672  1.13.4.2  nathanw }
    673  1.13.4.2  nathanw 
    674  1.13.4.2  nathanw /*
    675  1.13.4.2  nathanw  * Allocate physical memory from the given physical address range.
    676  1.13.4.2  nathanw  * Called by DMA-safe memory allocation methods.
    677  1.13.4.2  nathanw  */
    678  1.13.4.2  nathanw int
    679  1.13.4.2  nathanw _bus_dmamem_alloc_range(t, size, alignment, boundary, segs, nsegs, rsegs,
    680  1.13.4.2  nathanw     flags, low, high)
    681  1.13.4.2  nathanw 	bus_dma_tag_t t;
    682  1.13.4.2  nathanw 	bus_size_t size, alignment, boundary;
    683  1.13.4.2  nathanw 	bus_dma_segment_t *segs;
    684  1.13.4.2  nathanw 	int nsegs;
    685  1.13.4.2  nathanw 	int *rsegs;
    686  1.13.4.2  nathanw 	int flags;
    687  1.13.4.2  nathanw 	vaddr_t low;
    688  1.13.4.2  nathanw 	vaddr_t high;
    689  1.13.4.2  nathanw {
    690  1.13.4.2  nathanw 	vaddr_t curaddr, lastaddr;
    691  1.13.4.2  nathanw 	struct vm_page *m;
    692  1.13.4.2  nathanw 	struct pglist mlist;
    693  1.13.4.2  nathanw 	int curseg, error;
    694  1.13.4.2  nathanw 
    695  1.13.4.2  nathanw #ifdef DEBUG_DMA
    696  1.13.4.2  nathanw 	printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
    697  1.13.4.2  nathanw 	    t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
    698  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    699  1.13.4.2  nathanw 
    700  1.13.4.2  nathanw 	/* Always round the size. */
    701  1.13.4.2  nathanw 	size = round_page(size);
    702  1.13.4.2  nathanw 
    703  1.13.4.2  nathanw 	/*
    704  1.13.4.2  nathanw 	 * Allocate pages from the VM system.
    705  1.13.4.2  nathanw 	 */
    706  1.13.4.2  nathanw 	error = uvm_pglistalloc(size, low, high, alignment, boundary,
    707  1.13.4.2  nathanw 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    708  1.13.4.2  nathanw 	if (error)
    709  1.13.4.2  nathanw 		return (error);
    710  1.13.4.2  nathanw 
    711  1.13.4.2  nathanw 	/*
    712  1.13.4.2  nathanw 	 * Compute the location, size, and number of segments actually
    713  1.13.4.2  nathanw 	 * returned by the VM code.
    714  1.13.4.2  nathanw 	 */
    715  1.13.4.2  nathanw 	m = mlist.tqh_first;
    716  1.13.4.2  nathanw 	curseg = 0;
    717  1.13.4.2  nathanw 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    718  1.13.4.2  nathanw 	segs[curseg].ds_len = PAGE_SIZE;
    719  1.13.4.2  nathanw #ifdef DEBUG_DMA
    720  1.13.4.2  nathanw 		printf("alloc: page %lx\n", lastaddr);
    721  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    722  1.13.4.2  nathanw 	m = m->pageq.tqe_next;
    723  1.13.4.2  nathanw 
    724  1.13.4.2  nathanw 	for (; m != NULL; m = m->pageq.tqe_next) {
    725  1.13.4.2  nathanw 		curaddr = VM_PAGE_TO_PHYS(m);
    726  1.13.4.2  nathanw #ifdef DIAGNOSTIC
    727  1.13.4.2  nathanw 		if (curaddr < low || curaddr >= high) {
    728  1.13.4.2  nathanw 			printf("uvm_pglistalloc returned non-sensical"
    729  1.13.4.2  nathanw 			    " address 0x%lx\n", curaddr);
    730  1.13.4.2  nathanw 			panic("_bus_dmamem_alloc_range");
    731  1.13.4.2  nathanw 		}
    732  1.13.4.2  nathanw #endif	/* DIAGNOSTIC */
    733  1.13.4.2  nathanw #ifdef DEBUG_DMA
    734  1.13.4.2  nathanw 		printf("alloc: page %lx\n", curaddr);
    735  1.13.4.2  nathanw #endif	/* DEBUG_DMA */
    736  1.13.4.2  nathanw 		if (curaddr == (lastaddr + PAGE_SIZE))
    737  1.13.4.2  nathanw 			segs[curseg].ds_len += PAGE_SIZE;
    738  1.13.4.2  nathanw 		else {
    739  1.13.4.2  nathanw 			curseg++;
    740  1.13.4.2  nathanw 			segs[curseg].ds_addr = curaddr;
    741  1.13.4.2  nathanw 			segs[curseg].ds_len = PAGE_SIZE;
    742  1.13.4.2  nathanw 		}
    743  1.13.4.2  nathanw 		lastaddr = curaddr;
    744  1.13.4.2  nathanw 	}
    745  1.13.4.2  nathanw 
    746  1.13.4.2  nathanw 	*rsegs = curseg + 1;
    747  1.13.4.2  nathanw 
    748  1.13.4.2  nathanw 	return (0);
    749  1.13.4.2  nathanw }
    750  1.13.4.2  nathanw 
    751  1.13.4.2  nathanw /*
    752  1.13.4.2  nathanw  * "generic" DMA struct, nothing special.
    753  1.13.4.2  nathanw  */
    754  1.13.4.2  nathanw struct vax_bus_dma_tag vax_bus_dma_tag = {
    755  1.13.4.2  nathanw 	NULL,
    756  1.13.4.2  nathanw 	0,
    757  1.13.4.2  nathanw 	0,
    758  1.13.4.2  nathanw 	0,
    759  1.13.4.2  nathanw 	0,
    760  1.13.4.2  nathanw 	0,
    761  1.13.4.2  nathanw 	_bus_dmamap_create,
    762  1.13.4.2  nathanw 	_bus_dmamap_destroy,
    763  1.13.4.2  nathanw 	_bus_dmamap_load,
    764  1.13.4.2  nathanw 	_bus_dmamap_load_mbuf,
    765  1.13.4.2  nathanw 	_bus_dmamap_load_uio,
    766  1.13.4.2  nathanw 	_bus_dmamap_load_raw,
    767  1.13.4.2  nathanw 	_bus_dmamap_unload,
    768  1.13.4.2  nathanw 	_bus_dmamap_sync,
    769  1.13.4.2  nathanw 	_bus_dmamem_alloc,
    770  1.13.4.2  nathanw 	_bus_dmamem_free,
    771  1.13.4.2  nathanw 	_bus_dmamem_map,
    772  1.13.4.2  nathanw 	_bus_dmamem_unmap,
    773  1.13.4.2  nathanw 	_bus_dmamem_mmap,
    774  1.13.4.2  nathanw };
    775