Home | History | Annotate | Line # | Download | only in uba
uba_dma.c revision 1.2
      1 /* $NetBSD: uba_dma.c,v 1.2 1999/06/20 00:59:55 ragge 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, SPEUBAL, 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 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/device.h>
     45 #include <sys/malloc.h>
     46 #include <vm/vm.h>
     47 
     48 #define _VAX_BUS_DMA_PRIVATE
     49 #include <machine/bus.h>
     50 #include <machine/cpu.h>
     51 #include <machine/sgmap.h>
     52 
     53 #include <dev/qbus/ubavar.h>
     54 
     55 #include <arch/vax/uba/uba_common.h>
     56 
     57 int	uba_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     58 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     59 
     60 void	uba_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     61 
     62 int	uba_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     63 	    bus_size_t, struct proc *, int));
     64 
     65 int	uba_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     66 	    struct mbuf *, int));
     67 
     68 int	uba_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     69 	    struct uio *, int));
     70 
     71 int	uba_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     72 	    bus_dma_segment_t *, int, bus_size_t, int));
     73 
     74 void	uba_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     75 
     76 void	uba_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     77 	    bus_size_t, int));
     78 
     79 void
     80 uba_dma_init(sc)
     81 	struct uba_vsoftc *sc;
     82 {
     83 	bus_dma_tag_t t;
     84 	struct pte *pte;
     85 
     86 	/*
     87 	 * Initialize the DMA tag used for sgmap-mapped DMA.
     88 	 */
     89 	t = &sc->uv_dmat;
     90 	t->_cookie = sc;
     91 	t->_wbase = 0;
     92 	t->_wsize = sc->uv_size;
     93 	t->_boundary = 0;
     94 	t->_sgmap = &sc->uv_sgmap;
     95 	t->_dmamap_create = uba_bus_dmamap_create_sgmap;
     96 	t->_dmamap_destroy = uba_bus_dmamap_destroy_sgmap;
     97 	t->_dmamap_load = uba_bus_dmamap_load_sgmap;
     98 	t->_dmamap_load_mbuf = uba_bus_dmamap_load_mbuf_sgmap;
     99 	t->_dmamap_load_uio = uba_bus_dmamap_load_uio_sgmap;
    100 	t->_dmamap_load_raw = uba_bus_dmamap_load_raw_sgmap;
    101 	t->_dmamap_unload = uba_bus_dmamap_unload_sgmap;
    102 	t->_dmamap_sync = uba_bus_dmamap_sync;
    103 
    104 	t->_dmamem_alloc = _bus_dmamem_alloc;
    105 	t->_dmamem_free = _bus_dmamem_free;
    106 	t->_dmamem_map = _bus_dmamem_map;
    107 	t->_dmamem_unmap = _bus_dmamem_unmap;
    108 	t->_dmamem_mmap = _bus_dmamem_mmap;
    109 
    110 	/*
    111 	 * Map in Unibus map registers.
    112 	 */
    113 	pte = (struct pte *)vax_map_physmem(sc->uv_addr, sc->uv_size/VAX_NBPG);
    114 	if (pte == 0)
    115 		panic("uba_dma_init");
    116 	/*
    117 	 * Initialize the SGMAP.
    118 	 */
    119 	vax_sgmap_init(t, &sc->uv_sgmap, "uba_sgmap", 0, sc->uv_size, pte, 0);
    120 
    121 }
    122 
    123 /*
    124  * Create a UBA SGMAP-mapped DMA map.
    125  */
    126 int
    127 uba_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    128     flags, dmamp)
    129 	bus_dma_tag_t t;
    130 	bus_size_t size;
    131 	int nsegments;
    132 	bus_size_t maxsegsz;
    133 	bus_size_t boundary;
    134 	int flags;
    135 	bus_dmamap_t *dmamp;
    136 {
    137 	bus_dmamap_t map;
    138 	int error;
    139 
    140 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    141 	    boundary, flags, dmamp);
    142 	if (error)
    143 		return (error);
    144 
    145 	map = *dmamp;
    146 
    147 	if (flags & BUS_DMA_ALLOCNOW) {
    148 		error = vax_sgmap_alloc(map, vax_round_page(size),
    149 		    t->_sgmap, flags);
    150 		if (error)
    151 			uba_bus_dmamap_destroy_sgmap(t, map);
    152 	}
    153 
    154 	return (error);
    155 }
    156 
    157 /*
    158  * Destroy a UBA SGMAP-mapped DMA map.
    159  */
    160 void
    161 uba_bus_dmamap_destroy_sgmap(t, map)
    162 	bus_dma_tag_t t;
    163 	bus_dmamap_t map;
    164 {
    165 
    166 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    167 		vax_sgmap_free(map, t->_sgmap);
    168 
    169 	_bus_dmamap_destroy(t, map);
    170 }
    171 
    172 /*
    173  * Load a UBA SGMAP-mapped DMA map with a linear buffer.
    174  */
    175 int
    176 uba_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    177 	bus_dma_tag_t t;
    178 	bus_dmamap_t map;
    179 	void *buf;
    180 	bus_size_t buflen;
    181 	struct proc *p;
    182 	int flags;
    183 {
    184 	int error;
    185 
    186 	error = vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
    187 	/*
    188 	 * XXX - Set up BDPs.
    189 	 */
    190 
    191 	return (error);
    192 }
    193 
    194 /*
    195  * Load a UBA SGMAP-mapped DMA map with an mbuf chain.
    196  */
    197 int
    198 uba_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    199 	bus_dma_tag_t t;
    200 	bus_dmamap_t map;
    201 	struct mbuf *m;
    202 	int flags;
    203 {
    204 	int error;
    205 
    206 	error = vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
    207 
    208 	return (error);
    209 }
    210 
    211 /*
    212  * Load a UBA SGMAP-mapped DMA map with a uio.
    213  */
    214 int
    215 uba_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    216 	bus_dma_tag_t t;
    217 	bus_dmamap_t map;
    218 	struct uio *uio;
    219 	int flags;
    220 {
    221 	int error;
    222 
    223 	error = vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
    224 
    225 	return (error);
    226 }
    227 
    228 /*
    229  * Load a UBA SGMAP-mapped DMA map with raw memory.
    230  */
    231 int
    232 uba_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    233 	bus_dma_tag_t t;
    234 	bus_dmamap_t map;
    235 	bus_dma_segment_t *segs;
    236 	int nsegs;
    237 	bus_size_t size;
    238 	int flags;
    239 {
    240 	int error;
    241 
    242 	error = vax_sgmap_load_raw(t, map, segs, nsegs, size, flags,
    243 	    t->_sgmap);
    244 
    245 	return (error);
    246 }
    247 
    248 /*
    249  * Unload a UBA DMA map.
    250  */
    251 void
    252 uba_bus_dmamap_unload_sgmap(t, map)
    253 	bus_dma_tag_t t;
    254 	bus_dmamap_t map;
    255 {
    256 
    257 	/*
    258 	 * Invalidate any SGMAP page table entries used by this
    259 	 * mapping.
    260 	 */
    261 	vax_sgmap_unload(t, map, t->_sgmap);
    262 
    263 	/*
    264 	 * Do the generic bits of the unload.
    265 	 */
    266 	_bus_dmamap_unload(t, map);
    267 }
    268 
    269 /*
    270  * Sync the bus map. This is only needed if BDP's are used.
    271  */
    272 void
    273 uba_bus_dmamap_sync(tag, dmam, offset, len, ops)
    274 	bus_dma_tag_t tag;
    275 	bus_dmamap_t dmam;
    276 	bus_addr_t offset;
    277 	bus_size_t len;
    278 	int ops;
    279 {
    280 	/* Only BDP handling, but not yet. */
    281 }
    282