1 /* $NetBSD: uba_dma.c,v 1.6 2001/07/27 12:57:20 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, 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 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 <uvm/uvm_extern.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, if not mapped in already. 112 */ 113 if (sc->uv_uba) { 114 pte = sc->uv_uba->uba_map; 115 } else { 116 pte = (struct pte *)vax_map_physmem(sc->uv_addr, 117 vax_btoc(vax_btoc(sc->uv_size) * sizeof(struct pte))); 118 if (pte == 0) 119 panic("uba_dma_init"); 120 } 121 /* 122 * Initialize the SGMAP. 123 */ 124 vax_sgmap_init(t, &sc->uv_sgmap, "uba_sgmap", 0, sc->uv_size, pte, 0); 125 126 } 127 128 /* 129 * Create a UBA SGMAP-mapped DMA map. 130 */ 131 int 132 uba_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary, 133 flags, dmamp) 134 bus_dma_tag_t t; 135 bus_size_t size; 136 int nsegments; 137 bus_size_t maxsegsz; 138 bus_size_t boundary; 139 int flags; 140 bus_dmamap_t *dmamp; 141 { 142 bus_dmamap_t map; 143 int error; 144 145 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, 146 boundary, flags, dmamp); 147 if (error) 148 return (error); 149 150 map = *dmamp; 151 152 if (flags & BUS_DMA_ALLOCNOW) { 153 error = vax_sgmap_alloc(map, vax_round_page(size), 154 t->_sgmap, flags); 155 if (error) 156 uba_bus_dmamap_destroy_sgmap(t, map); 157 } 158 159 return (error); 160 } 161 162 /* 163 * Destroy a UBA SGMAP-mapped DMA map. 164 */ 165 void 166 uba_bus_dmamap_destroy_sgmap(t, map) 167 bus_dma_tag_t t; 168 bus_dmamap_t map; 169 { 170 171 if (map->_dm_flags & DMAMAP_HAS_SGMAP) 172 vax_sgmap_free(map, t->_sgmap); 173 174 _bus_dmamap_destroy(t, map); 175 } 176 177 /* 178 * Load a UBA SGMAP-mapped DMA map with a linear buffer. 179 */ 180 int 181 uba_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags) 182 bus_dma_tag_t t; 183 bus_dmamap_t map; 184 void *buf; 185 bus_size_t buflen; 186 struct proc *p; 187 int flags; 188 { 189 int error; 190 191 error = vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap); 192 /* 193 * XXX - Set up BDPs. 194 */ 195 196 return (error); 197 } 198 199 /* 200 * Load a UBA SGMAP-mapped DMA map with an mbuf chain. 201 */ 202 int 203 uba_bus_dmamap_load_mbuf_sgmap(t, map, m, flags) 204 bus_dma_tag_t t; 205 bus_dmamap_t map; 206 struct mbuf *m; 207 int flags; 208 { 209 int error; 210 211 error = vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap); 212 213 return (error); 214 } 215 216 /* 217 * Load a UBA SGMAP-mapped DMA map with a uio. 218 */ 219 int 220 uba_bus_dmamap_load_uio_sgmap(t, map, uio, flags) 221 bus_dma_tag_t t; 222 bus_dmamap_t map; 223 struct uio *uio; 224 int flags; 225 { 226 int error; 227 228 error = vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap); 229 230 return (error); 231 } 232 233 /* 234 * Load a UBA SGMAP-mapped DMA map with raw memory. 235 */ 236 int 237 uba_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags) 238 bus_dma_tag_t t; 239 bus_dmamap_t map; 240 bus_dma_segment_t *segs; 241 int nsegs; 242 bus_size_t size; 243 int flags; 244 { 245 int error; 246 247 error = vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, 248 t->_sgmap); 249 250 return (error); 251 } 252 253 /* 254 * Unload a UBA DMA map. 255 */ 256 void 257 uba_bus_dmamap_unload_sgmap(t, map) 258 bus_dma_tag_t t; 259 bus_dmamap_t map; 260 { 261 262 /* 263 * Invalidate any SGMAP page table entries used by this 264 * mapping. 265 */ 266 vax_sgmap_unload(t, map, t->_sgmap); 267 268 /* 269 * Do the generic bits of the unload. 270 */ 271 _bus_dmamap_unload(t, map); 272 } 273 274 /* 275 * Sync the bus map. This is only needed if BDP's are used. 276 */ 277 void 278 uba_bus_dmamap_sync(tag, dmam, offset, len, ops) 279 bus_dma_tag_t tag; 280 bus_dmamap_t dmam; 281 bus_addr_t offset; 282 bus_size_t len; 283 int ops; 284 { 285 /* Only BDP handling, but not yet. */ 286 } 287