xref: /netbsd/sys/arch/vax/vsa/vsbus_dma.c (revision bf9ec67e)
1 /* $NetBSD: vsbus_dma.c,v 1.8 2000/11/16 19:25:42 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 
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/sid.h>
52 #include <machine/sgmap.h>
53 #include <machine/vsbus.h>
54 
55 static int vsbus_bus_dmamap_create_sgmap(bus_dma_tag_t, bus_size_t, int,
56 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
57 
58 static void vsbus_bus_dmamap_destroy_sgmap(bus_dma_tag_t, bus_dmamap_t);
59 
60 static int vsbus_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
61 	    bus_size_t, struct proc *, int);
62 
63 static int vsbus_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
64 	    struct mbuf *, int);
65 
66 static int vsbus_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
67 	    struct uio *, int);
68 
69 static int vsbus_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
70 	    bus_dma_segment_t *, int, bus_size_t, int);
71 
72 static void vsbus_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
73 
74 static void vsbus_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
75 	    bus_size_t, int);
76 
77 void
78 vsbus_dma_init(sc, ptecnt)
79 	struct vsbus_softc *sc;
80 	unsigned ptecnt;
81 {
82 	bus_dma_tag_t t;
83 	bus_dma_segment_t segs[1];
84 	struct pte *pte;
85 	int nsegs, error;
86 	unsigned mapsize = ptecnt * sizeof(struct pte);
87 
88 	/*
89 	 * Initialize the DMA tag used for sgmap-mapped DMA.
90 	 */
91 	t = &sc->sc_dmatag;
92 	t->_cookie = sc;
93 	t->_wbase = 0;
94 	t->_wsize = ptecnt * VAX_NBPG;
95 	t->_boundary = 0;
96 	t->_sgmap = &sc->sc_sgmap;
97 	t->_dmamap_create = vsbus_bus_dmamap_create_sgmap;
98 	t->_dmamap_destroy = vsbus_bus_dmamap_destroy_sgmap;
99 	t->_dmamap_load = vsbus_bus_dmamap_load_sgmap;
100 	t->_dmamap_load_mbuf = vsbus_bus_dmamap_load_mbuf_sgmap;
101 	t->_dmamap_load_uio = vsbus_bus_dmamap_load_uio_sgmap;
102 	t->_dmamap_load_raw = vsbus_bus_dmamap_load_raw_sgmap;
103 	t->_dmamap_unload = vsbus_bus_dmamap_unload_sgmap;
104 	t->_dmamap_sync = vsbus_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 	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 		   (caddr_t *) &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 			((u_int32_t *) pte)[--nsegs] = 0;
135 		}
136 		segs->ds_addr = KA49_SCSIMAP;
137 	}
138 	printf("%s: %uK entry DMA SGMAP at PA 0x%lx (VA %p)\n",
139 		sc->sc_dev.dv_xname, ptecnt / 1024, segs->ds_addr, pte);
140 
141 	/*
142 	 * Initialize the SGMAP.
143 	 */
144 	vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0);
145 
146 }
147 
148 /*
149  * Create a VSBUS SGMAP-mapped DMA map.
150  */
151 int
152 vsbus_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
153     flags, dmamp)
154 	bus_dma_tag_t t;
155 	bus_size_t size;
156 	int nsegments;
157 	bus_size_t maxsegsz;
158 	bus_size_t boundary;
159 	int flags;
160 	bus_dmamap_t *dmamp;
161 {
162 	bus_dmamap_t map;
163 	int error;
164 
165 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
166 	    boundary, flags, dmamp);
167 	if (error)
168 		return (error);
169 
170 	map = *dmamp;
171 
172 	if (flags & BUS_DMA_ALLOCNOW) {
173 		error = vax_sgmap_alloc(map, vax_round_page(size),
174 		    t->_sgmap, flags);
175 		if (error)
176 			vsbus_bus_dmamap_destroy_sgmap(t, map);
177 	}
178 
179 	return (error);
180 }
181 
182 /*
183  * Destroy a VSBUS SGMAP-mapped DMA map.
184  */
185 static void
186 vsbus_bus_dmamap_destroy_sgmap(t, map)
187 	bus_dma_tag_t t;
188 	bus_dmamap_t map;
189 {
190 
191 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
192 		vax_sgmap_free(map, t->_sgmap);
193 
194 	_bus_dmamap_destroy(t, map);
195 }
196 
197 /*
198  * Load a VSBUS SGMAP-mapped DMA map with a linear buffer.
199  */
200 static int
201 vsbus_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
202 	bus_dma_tag_t t;
203 	bus_dmamap_t map;
204 	void *buf;
205 	bus_size_t buflen;
206 	struct proc *p;
207 	int flags;
208 {
209 	return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
210 }
211 
212 /*
213  * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain.
214  */
215 static int
216 vsbus_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
217 	bus_dma_tag_t t;
218 	bus_dmamap_t map;
219 	struct mbuf *m;
220 	int flags;
221 {
222 	return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
223 }
224 
225 /*
226  * Load a VSBUS SGMAP-mapped DMA map with a uio.
227  */
228 static int
229 vsbus_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
230 	bus_dma_tag_t t;
231 	bus_dmamap_t map;
232 	struct uio *uio;
233 	int flags;
234 {
235 	return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
236 }
237 
238 /*
239  * Load a VSBUS SGMAP-mapped DMA map with raw memory.
240  */
241 static int
242 vsbus_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
243 	bus_dma_tag_t t;
244 	bus_dmamap_t map;
245 	bus_dma_segment_t *segs;
246 	int nsegs;
247 	bus_size_t size;
248 	int flags;
249 {
250 	return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap);
251 }
252 
253 /*
254  * Unload a VSBUS DMA map.
255  */
256 static void
257 vsbus_bus_dmamap_unload_sgmap(t, map)
258 	bus_dma_tag_t t;
259 	bus_dmamap_t map;
260 {
261 	/*
262 	 * Invalidate any SGMAP page table entries used by this
263 	 * mapping.
264 	 */
265 	vax_sgmap_unload(t, map, t->_sgmap);
266 
267 	/*
268 	 * Do the generic bits of the unload.
269 	 */
270 	_bus_dmamap_unload(t, map);
271 }
272 
273 /*
274  * Sync the bus map.
275  */
276 static void
277 vsbus_bus_dmamap_sync(tag, dmam, offset, len, ops)
278 	bus_dma_tag_t tag;
279 	bus_dmamap_t dmam;
280 	bus_addr_t offset;
281 	bus_size_t len;
282 	int ops;
283 {
284 	/* not needed */
285 }
286