Home | History | Annotate | Line # | Download | only in uba
uba_dma.c revision 1.7
      1 /* $NetBSD: uba_dma.c,v 1.7 2003/07/15 02:15:02 lukem Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: uba_dma.c,v 1.7 2003/07/15 02:15:02 lukem Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <uvm/uvm_extern.h>
     49 
     50 #define _VAX_BUS_DMA_PRIVATE
     51 #include <machine/bus.h>
     52 #include <machine/cpu.h>
     53 #include <machine/sgmap.h>
     54 
     55 #include <dev/qbus/ubavar.h>
     56 
     57 #include <arch/vax/uba/uba_common.h>
     58 
     59 int	uba_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     60 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     61 
     62 void	uba_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     63 
     64 int	uba_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     65 	    bus_size_t, struct proc *, int));
     66 
     67 int	uba_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     68 	    struct mbuf *, int));
     69 
     70 int	uba_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     71 	    struct uio *, int));
     72 
     73 int	uba_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     74 	    bus_dma_segment_t *, int, bus_size_t, int));
     75 
     76 void	uba_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     77 
     78 void	uba_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     79 	    bus_size_t, int));
     80 
     81 void
     82 uba_dma_init(sc)
     83 	struct uba_vsoftc *sc;
     84 {
     85 	bus_dma_tag_t t;
     86 	struct pte *pte;
     87 
     88 	/*
     89 	 * Initialize the DMA tag used for sgmap-mapped DMA.
     90 	 */
     91 	t = &sc->uv_dmat;
     92 	t->_cookie = sc;
     93 	t->_wbase = 0;
     94 	t->_wsize = sc->uv_size;
     95 	t->_boundary = 0;
     96 	t->_sgmap = &sc->uv_sgmap;
     97 	t->_dmamap_create = uba_bus_dmamap_create_sgmap;
     98 	t->_dmamap_destroy = uba_bus_dmamap_destroy_sgmap;
     99 	t->_dmamap_load = uba_bus_dmamap_load_sgmap;
    100 	t->_dmamap_load_mbuf = uba_bus_dmamap_load_mbuf_sgmap;
    101 	t->_dmamap_load_uio = uba_bus_dmamap_load_uio_sgmap;
    102 	t->_dmamap_load_raw = uba_bus_dmamap_load_raw_sgmap;
    103 	t->_dmamap_unload = uba_bus_dmamap_unload_sgmap;
    104 	t->_dmamap_sync = uba_bus_dmamap_sync;
    105 
    106 	t->_dmamem_alloc = _bus_dmamem_alloc;
    107 	t->_dmamem_free = _bus_dmamem_free;
    108 	t->_dmamem_map = _bus_dmamem_map;
    109 	t->_dmamem_unmap = _bus_dmamem_unmap;
    110 	t->_dmamem_mmap = _bus_dmamem_mmap;
    111 
    112 	/*
    113 	 * Map in Unibus map registers, if not mapped in already.
    114 	 */
    115 	if (sc->uv_uba) {
    116 		pte = sc->uv_uba->uba_map;
    117 	} else {
    118 		pte = (struct pte *)vax_map_physmem(sc->uv_addr,
    119 		    vax_btoc(vax_btoc(sc->uv_size) * sizeof(struct pte)));
    120 		if (pte == 0)
    121 			panic("uba_dma_init");
    122 	}
    123 	/*
    124 	 * Initialize the SGMAP.
    125 	 */
    126 	vax_sgmap_init(t, &sc->uv_sgmap, "uba_sgmap", 0, sc->uv_size, pte, 0);
    127 
    128 }
    129 
    130 /*
    131  * Create a UBA SGMAP-mapped DMA map.
    132  */
    133 int
    134 uba_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    135     flags, dmamp)
    136 	bus_dma_tag_t t;
    137 	bus_size_t size;
    138 	int nsegments;
    139 	bus_size_t maxsegsz;
    140 	bus_size_t boundary;
    141 	int flags;
    142 	bus_dmamap_t *dmamp;
    143 {
    144 	bus_dmamap_t map;
    145 	int error;
    146 
    147 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    148 	    boundary, flags, dmamp);
    149 	if (error)
    150 		return (error);
    151 
    152 	map = *dmamp;
    153 
    154 	if (flags & BUS_DMA_ALLOCNOW) {
    155 		error = vax_sgmap_alloc(map, vax_round_page(size),
    156 		    t->_sgmap, flags);
    157 		if (error)
    158 			uba_bus_dmamap_destroy_sgmap(t, map);
    159 	}
    160 
    161 	return (error);
    162 }
    163 
    164 /*
    165  * Destroy a UBA SGMAP-mapped DMA map.
    166  */
    167 void
    168 uba_bus_dmamap_destroy_sgmap(t, map)
    169 	bus_dma_tag_t t;
    170 	bus_dmamap_t map;
    171 {
    172 
    173 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    174 		vax_sgmap_free(map, t->_sgmap);
    175 
    176 	_bus_dmamap_destroy(t, map);
    177 }
    178 
    179 /*
    180  * Load a UBA SGMAP-mapped DMA map with a linear buffer.
    181  */
    182 int
    183 uba_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    184 	bus_dma_tag_t t;
    185 	bus_dmamap_t map;
    186 	void *buf;
    187 	bus_size_t buflen;
    188 	struct proc *p;
    189 	int flags;
    190 {
    191 	int error;
    192 
    193 	error = vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
    194 	/*
    195 	 * XXX - Set up BDPs.
    196 	 */
    197 
    198 	return (error);
    199 }
    200 
    201 /*
    202  * Load a UBA SGMAP-mapped DMA map with an mbuf chain.
    203  */
    204 int
    205 uba_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    206 	bus_dma_tag_t t;
    207 	bus_dmamap_t map;
    208 	struct mbuf *m;
    209 	int flags;
    210 {
    211 	int error;
    212 
    213 	error = vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
    214 
    215 	return (error);
    216 }
    217 
    218 /*
    219  * Load a UBA SGMAP-mapped DMA map with a uio.
    220  */
    221 int
    222 uba_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    223 	bus_dma_tag_t t;
    224 	bus_dmamap_t map;
    225 	struct uio *uio;
    226 	int flags;
    227 {
    228 	int error;
    229 
    230 	error = vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
    231 
    232 	return (error);
    233 }
    234 
    235 /*
    236  * Load a UBA SGMAP-mapped DMA map with raw memory.
    237  */
    238 int
    239 uba_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    240 	bus_dma_tag_t t;
    241 	bus_dmamap_t map;
    242 	bus_dma_segment_t *segs;
    243 	int nsegs;
    244 	bus_size_t size;
    245 	int flags;
    246 {
    247 	int error;
    248 
    249 	error = vax_sgmap_load_raw(t, map, segs, nsegs, size, flags,
    250 	    t->_sgmap);
    251 
    252 	return (error);
    253 }
    254 
    255 /*
    256  * Unload a UBA DMA map.
    257  */
    258 void
    259 uba_bus_dmamap_unload_sgmap(t, map)
    260 	bus_dma_tag_t t;
    261 	bus_dmamap_t map;
    262 {
    263 
    264 	/*
    265 	 * Invalidate any SGMAP page table entries used by this
    266 	 * mapping.
    267 	 */
    268 	vax_sgmap_unload(t, map, t->_sgmap);
    269 
    270 	/*
    271 	 * Do the generic bits of the unload.
    272 	 */
    273 	_bus_dmamap_unload(t, map);
    274 }
    275 
    276 /*
    277  * Sync the bus map. This is only needed if BDP's are used.
    278  */
    279 void
    280 uba_bus_dmamap_sync(tag, dmam, offset, len, ops)
    281 	bus_dma_tag_t tag;
    282 	bus_dmamap_t dmam;
    283 	bus_addr_t offset;
    284 	bus_size_t len;
    285 	int ops;
    286 {
    287 	/* Only BDP handling, but not yet. */
    288 }
    289