vsbus_dma.c revision 1.15
1/* $NetBSD: vsbus_dma.c,v 1.15 2008/04/28 20:23:39 martin 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: vsbus_dma.c,v 1.15 2008/04/28 20:23:39 martin Exp $"); 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/device.h> 40#include <sys/malloc.h> 41#include <uvm/uvm_extern.h> 42 43#define _VAX_BUS_DMA_PRIVATE 44#include <machine/bus.h> 45#include <machine/cpu.h> 46#include <machine/sid.h> 47#include <machine/sgmap.h> 48#include <machine/vsbus.h> 49 50static int sgmap_bus_dmamap_create_sgmap(bus_dma_tag_t, bus_size_t, int, 51 bus_size_t, bus_size_t, int, bus_dmamap_t *); 52 53static void sgmap_bus_dmamap_destroy_sgmap(bus_dma_tag_t, bus_dmamap_t); 54 55static int sgmap_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *, 56 bus_size_t, struct proc *, int); 57 58static int sgmap_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t, 59 struct mbuf *, int); 60 61static int sgmap_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t, 62 struct uio *, int); 63 64static int sgmap_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t, 65 bus_dma_segment_t *, int, bus_size_t, int); 66 67static void sgmap_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t); 68 69static void sgmap_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t, 70 bus_size_t, int); 71 72void 73vax_sgmap_dmatag_init(bus_dma_tag_t t, void *cookie, size_t ptecnt) 74{ 75 /* 76 * Initialize the DMA tag used for sgmap-mapped DMA. 77 */ 78 t->_cookie = cookie; 79 t->_wbase = 0; 80 t->_wsize = ptecnt * VAX_NBPG; 81 t->_boundary = 0; 82 t->_dmamap_create = sgmap_bus_dmamap_create_sgmap; 83 t->_dmamap_destroy = sgmap_bus_dmamap_destroy_sgmap; 84 t->_dmamap_load = sgmap_bus_dmamap_load_sgmap; 85 t->_dmamap_load_mbuf = sgmap_bus_dmamap_load_mbuf_sgmap; 86 t->_dmamap_load_uio = sgmap_bus_dmamap_load_uio_sgmap; 87 t->_dmamap_load_raw = sgmap_bus_dmamap_load_raw_sgmap; 88 t->_dmamap_unload = sgmap_bus_dmamap_unload_sgmap; 89 t->_dmamap_sync = sgmap_bus_dmamap_sync; 90 91 t->_dmamem_alloc = _bus_dmamem_alloc; 92 t->_dmamem_free = _bus_dmamem_free; 93 t->_dmamem_map = _bus_dmamem_map; 94 t->_dmamem_unmap = _bus_dmamem_unmap; 95 t->_dmamem_mmap = _bus_dmamem_mmap; 96} 97 98void 99vsbus_dma_init(struct vsbus_softc *sc, unsigned ptecnt) 100{ 101 bus_dma_tag_t t = &sc->sc_dmatag; 102 bus_dma_segment_t segs[1]; 103 struct pte *pte; 104 int nsegs; 105 int error; 106 size_t mapsize = ptecnt * sizeof(struct pte); 107 108 vax_sgmap_dmatag_init(t, sc, ptecnt); 109 110 t->_sgmap = &sc->sc_sgmap; 111 112 if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) { 113 /* 114 * Allocate and map the VS4000 scatter gather map. 115 */ 116 error = bus_dmamem_alloc(t, mapsize, mapsize, mapsize, 117 segs, 1, &nsegs, BUS_DMA_NOWAIT); 118 if (error) { 119 panic("vsbus_dma_init: error allocating memory for " 120 "hw sgmap: error=%d", error); 121 } 122 123 error = bus_dmamem_map(t, segs, nsegs, mapsize, 124 (void **)(void *) &pte, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 125 if (error) { 126 panic("vsbus_dma_init: error mapping memory for " 127 "hw sgmap: error=%d", error); 128 } 129 memset(pte, 0, mapsize); 130 *(int *) (sc->sc_vsregs + 8) = segs->ds_addr; /* set MAP BASE 0x2008008 */ 131 } else { 132 pte = (struct pte *) vax_map_physmem(KA49_SCSIMAP, mapsize / VAX_NBPG); 133 for (nsegs = ptecnt; nsegs > 0; ) { 134 ((uint32_t *) pte)[--nsegs] = 0; 135 } 136 segs->ds_addr = KA49_SCSIMAP; 137 } 138 aprint_normal_dev(sc->sc_dev, 139 "%uK entry DMA SGMAP at PA 0x%lx (VA %p)\n", 140 ptecnt / 1024, segs->ds_addr, pte); 141 142 /* 143 * Initialize the SGMAP. 144 */ 145 vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0); 146 147} 148 149/* 150 * Create a VSBUS SGMAP-mapped DMA map. 151 */ 152int 153sgmap_bus_dmamap_create_sgmap(bus_dma_tag_t t, bus_size_t size, int nsegments, 154 bus_size_t maxsegsz, bus_size_t boundary, int flags, 155 bus_dmamap_t *dmamp) 156{ 157 bus_dmamap_t map; 158 int error; 159 160 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, 161 boundary, flags, dmamp); 162 if (error) 163 return (error); 164 165 map = *dmamp; 166 167 if (flags & BUS_DMA_ALLOCNOW) { 168 error = vax_sgmap_alloc(map, vax_round_page(size), 169 t->_sgmap, flags); 170 if (error) 171 sgmap_bus_dmamap_destroy_sgmap(t, map); 172 } 173 174 return (error); 175} 176 177/* 178 * Destroy a VSBUS SGMAP-mapped DMA map. 179 */ 180static void 181sgmap_bus_dmamap_destroy_sgmap(bus_dma_tag_t t, bus_dmamap_t map) 182{ 183 184 if (map->_dm_flags & DMAMAP_HAS_SGMAP) 185 vax_sgmap_free(map, t->_sgmap); 186 187 _bus_dmamap_destroy(t, map); 188} 189 190/* 191 * Load a VSBUS SGMAP-mapped DMA map with a linear buffer. 192 */ 193static int 194sgmap_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 195 bus_size_t buflen, struct proc *p, int flags) 196{ 197 return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap); 198} 199 200/* 201 * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain. 202 */ 203static int 204sgmap_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 205 struct mbuf *m, int flags) 206{ 207 return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap); 208} 209 210/* 211 * Load a VSBUS SGMAP-mapped DMA map with a uio. 212 */ 213static int 214sgmap_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 215 struct uio *uio, int flags) 216{ 217 return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap); 218} 219 220/* 221 * Load a VSBUS SGMAP-mapped DMA map with raw memory. 222 */ 223static int 224sgmap_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map, 225 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 226{ 227 return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap); 228} 229 230/* 231 * Unload a VSBUS DMA map. 232 */ 233static void 234sgmap_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. 250 */ 251static void 252sgmap_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 /* not needed */ 256} 257