Home | History | Annotate | Line # | Download | only in uba
uba_dma.c revision 1.9
      1 /* $NetBSD: uba_dma.c,v 1.9 2008/03/11 05:34:02 matt 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.9 2008/03/11 05:34:02 matt 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(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(bus_dma_tag_t, bus_dmamap_t);
     63 
     64 int	uba_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     65 	    bus_size_t, struct proc *, int);
     66 
     67 int	uba_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     68 	    struct mbuf *, int);
     69 
     70 int	uba_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     71 	    struct uio *, int);
     72 
     73 int	uba_bus_dmamap_load_raw_sgmap(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(bus_dma_tag_t, bus_dmamap_t);
     77 
     78 void	uba_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     79 	    bus_size_t, int);
     80 
     81 void
     82 uba_dma_init(struct uba_vsoftc *sc)
     83 {
     84 	bus_dma_tag_t t;
     85 	struct pte *pte;
     86 
     87 	/*
     88 	 * Initialize the DMA tag used for sgmap-mapped DMA.
     89 	 */
     90 	t = &sc->uv_dmat;
     91 	t->_cookie = sc;
     92 	t->_wbase = 0;
     93 	t->_wsize = sc->uv_size;
     94 	t->_boundary = 0;
     95 	t->_sgmap = &sc->uv_sgmap;
     96 	t->_dmamap_create = uba_bus_dmamap_create_sgmap;
     97 	t->_dmamap_destroy = uba_bus_dmamap_destroy_sgmap;
     98 	t->_dmamap_load = uba_bus_dmamap_load_sgmap;
     99 	t->_dmamap_load_mbuf = uba_bus_dmamap_load_mbuf_sgmap;
    100 	t->_dmamap_load_uio = uba_bus_dmamap_load_uio_sgmap;
    101 	t->_dmamap_load_raw = uba_bus_dmamap_load_raw_sgmap;
    102 	t->_dmamap_unload = uba_bus_dmamap_unload_sgmap;
    103 	t->_dmamap_sync = uba_bus_dmamap_sync;
    104 
    105 	t->_dmamem_alloc = _bus_dmamem_alloc;
    106 	t->_dmamem_free = _bus_dmamem_free;
    107 	t->_dmamem_map = _bus_dmamem_map;
    108 	t->_dmamem_unmap = _bus_dmamem_unmap;
    109 	t->_dmamem_mmap = _bus_dmamem_mmap;
    110 
    111 	/*
    112 	 * Map in Unibus map registers, if not mapped in already.
    113 	 */
    114 	if (sc->uv_uba) {
    115 		pte = sc->uv_uba->uba_map;
    116 	} else {
    117 		pte = (struct pte *)vax_map_physmem(sc->uv_addr,
    118 		    vax_btoc(vax_btoc(sc->uv_size) * sizeof(struct pte)));
    119 		if (pte == 0)
    120 			panic("uba_dma_init");
    121 	}
    122 	/*
    123 	 * Initialize the SGMAP.
    124 	 */
    125 	vax_sgmap_init(t, &sc->uv_sgmap, "uba_sgmap", 0, sc->uv_size, pte, 0);
    126 
    127 }
    128 
    129 /*
    130  * Create a UBA SGMAP-mapped DMA map.
    131  */
    132 int
    133 uba_bus_dmamap_create_sgmap(bus_dma_tag_t t, bus_size_t size,  int nsegments,
    134 	bus_size_t maxsegsz, bus_size_t boundary, 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(bus_dma_tag_t t, bus_dmamap_t map)
    162 {
    163 
    164 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    165 		vax_sgmap_free(map, t->_sgmap);
    166 
    167 	_bus_dmamap_destroy(t, map);
    168 }
    169 
    170 /*
    171  * Load a UBA SGMAP-mapped DMA map with a linear buffer.
    172  */
    173 int
    174 uba_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    175 	bus_size_t buflen, struct proc *p, int flags)
    176 {
    177 	int error;
    178 
    179 	error = vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
    180 	/*
    181 	 * XXX - Set up BDPs.
    182 	 */
    183 
    184 	return (error);
    185 }
    186 
    187 /*
    188  * Load a UBA SGMAP-mapped DMA map with an mbuf chain.
    189  */
    190 int
    191 uba_bus_dmamap_load_mbuf_sgmap( bus_dma_tag_t t, bus_dmamap_t map,
    192 	struct mbuf *m, int flags)
    193 {
    194 	int error;
    195 
    196 	error = vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
    197 
    198 	return (error);
    199 }
    200 
    201 /*
    202  * Load a UBA SGMAP-mapped DMA map with a uio.
    203  */
    204 int
    205 uba_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    206 	struct uio *uio, int flags)
    207 {
    208 	int error;
    209 
    210 	error = vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
    211 
    212 	return (error);
    213 }
    214 
    215 /*
    216  * Load a UBA SGMAP-mapped DMA map with raw memory.
    217  */
    218 int
    219 uba_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    220 	bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    221 {
    222 	int error;
    223 
    224 	error = vax_sgmap_load_raw(t, map, segs, nsegs, size, flags,
    225 	    t->_sgmap);
    226 
    227 	return (error);
    228 }
    229 
    230 /*
    231  * Unload a UBA DMA map.
    232  */
    233 void
    234 uba_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    235 {
    236 	/*
    237 	 * Invalidate any SGMAP page table entries used by this
    238 	 * mapping.
    239 	 */
    240 	vax_sgmap_unload(t, map, t->_sgmap);
    241 
    242 	/*
    243 	 * Do the generic bits of the unload.
    244 	 */
    245 	_bus_dmamap_unload(t, map);
    246 }
    247 
    248 /*
    249  * Sync the bus map. This is only needed if BDP's are used.
    250  */
    251 void
    252 uba_bus_dmamap_sync(bus_dma_tag_t tag, bus_dmamap_t dmam, bus_addr_t offset,
    253 	bus_size_t len, int ops)
    254 {
    255 	/* Only BDP handling, but not yet. */
    256 }
    257