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