vsbus_dma.c revision 1.13
1/* $NetBSD: vsbus_dma.c,v 1.13 2008/02/03 08:41:22 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: vsbus_dma.c,v 1.13 2008/02/03 08:41:22 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/sid.h>
54#include <machine/sgmap.h>
55#include <machine/vsbus.h>
56
57static int sgmap_bus_dmamap_create_sgmap(bus_dma_tag_t, bus_size_t, int,
58	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
59
60static void sgmap_bus_dmamap_destroy_sgmap(bus_dma_tag_t, bus_dmamap_t);
61
62static int sgmap_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
63	    bus_size_t, struct proc *, int);
64
65static int sgmap_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
66	    struct mbuf *, int);
67
68static int sgmap_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
69	    struct uio *, int);
70
71static int sgmap_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
72	    bus_dma_segment_t *, int, bus_size_t, int);
73
74static void sgmap_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
75
76static void sgmap_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
77	    bus_size_t, int);
78
79void
80vax_sgmap_dmatag_init(bus_dma_tag_t t, void *cookie, size_t ptecnt)
81{
82	/*
83	 * Initialize the DMA tag used for sgmap-mapped DMA.
84	 */
85	t->_cookie = cookie;
86	t->_wbase = 0;
87	t->_wsize = ptecnt * VAX_NBPG;
88	t->_boundary = 0;
89	t->_dmamap_create = sgmap_bus_dmamap_create_sgmap;
90	t->_dmamap_destroy = sgmap_bus_dmamap_destroy_sgmap;
91	t->_dmamap_load = sgmap_bus_dmamap_load_sgmap;
92	t->_dmamap_load_mbuf = sgmap_bus_dmamap_load_mbuf_sgmap;
93	t->_dmamap_load_uio = sgmap_bus_dmamap_load_uio_sgmap;
94	t->_dmamap_load_raw = sgmap_bus_dmamap_load_raw_sgmap;
95	t->_dmamap_unload = sgmap_bus_dmamap_unload_sgmap;
96	t->_dmamap_sync = sgmap_bus_dmamap_sync;
97
98	t->_dmamem_alloc = _bus_dmamem_alloc;
99	t->_dmamem_free = _bus_dmamem_free;
100	t->_dmamem_map = _bus_dmamem_map;
101	t->_dmamem_unmap = _bus_dmamem_unmap;
102	t->_dmamem_mmap = _bus_dmamem_mmap;
103}
104
105void
106vsbus_dma_init(struct vsbus_softc *sc, unsigned ptecnt)
107{
108	bus_dma_tag_t t = &sc->sc_dmatag;
109	bus_dma_segment_t segs[1];
110	struct pte *pte;
111	int nsegs;
112	int error;
113	size_t mapsize = ptecnt * sizeof(struct pte);
114
115	vax_sgmap_dmatag_init(t, sc, ptecnt);
116
117	t->_sgmap = &sc->sc_sgmap;
118
119	if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
120		/*
121		 * Allocate and map the VS4000 scatter gather map.
122		 */
123		error = bus_dmamem_alloc(t, mapsize, mapsize, mapsize,
124		    segs, 1, &nsegs, BUS_DMA_NOWAIT);
125		if (error) {
126			panic("vsbus_dma_init: error allocating memory for "
127			    "hw sgmap: error=%d", error);
128		}
129
130		error = bus_dmamem_map(t, segs, nsegs, mapsize,
131		   (void **)(void *) &pte, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
132		if (error) {
133			panic("vsbus_dma_init: error mapping memory for "
134			    "hw sgmap: error=%d", error);
135		}
136		memset(pte, 0, mapsize);
137		*(int *) (sc->sc_vsregs + 8) = segs->ds_addr;	/* set MAP BASE 0x2008008 */
138	} else {
139		pte = (struct pte *) vax_map_physmem(KA49_SCSIMAP, mapsize / VAX_NBPG);
140		for (nsegs = ptecnt; nsegs > 0; ) {
141			((u_int32_t *) pte)[--nsegs] = 0;
142		}
143		segs->ds_addr = KA49_SCSIMAP;
144	}
145	printf("%s: %uK entry DMA SGMAP at PA 0x%lx (VA %p)\n",
146		sc->sc_dev.dv_xname, ptecnt / 1024, segs->ds_addr, pte);
147
148	/*
149	 * Initialize the SGMAP.
150	 */
151	vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0);
152
153}
154
155/*
156 * Create a VSBUS SGMAP-mapped DMA map.
157 */
158int
159sgmap_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
160    flags, dmamp)
161	bus_dma_tag_t t;
162	bus_size_t size;
163	int nsegments;
164	bus_size_t maxsegsz;
165	bus_size_t boundary;
166	int flags;
167	bus_dmamap_t *dmamp;
168{
169	bus_dmamap_t map;
170	int error;
171
172	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
173	    boundary, flags, dmamp);
174	if (error)
175		return (error);
176
177	map = *dmamp;
178
179	if (flags & BUS_DMA_ALLOCNOW) {
180		error = vax_sgmap_alloc(map, vax_round_page(size),
181		    t->_sgmap, flags);
182		if (error)
183			sgmap_bus_dmamap_destroy_sgmap(t, map);
184	}
185
186	return (error);
187}
188
189/*
190 * Destroy a VSBUS SGMAP-mapped DMA map.
191 */
192static void
193sgmap_bus_dmamap_destroy_sgmap(t, map)
194	bus_dma_tag_t t;
195	bus_dmamap_t map;
196{
197
198	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
199		vax_sgmap_free(map, t->_sgmap);
200
201	_bus_dmamap_destroy(t, map);
202}
203
204/*
205 * Load a VSBUS SGMAP-mapped DMA map with a linear buffer.
206 */
207static int
208sgmap_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
209	bus_dma_tag_t t;
210	bus_dmamap_t map;
211	void *buf;
212	bus_size_t buflen;
213	struct proc *p;
214	int flags;
215{
216	return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
217}
218
219/*
220 * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain.
221 */
222static int
223sgmap_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
224	bus_dma_tag_t t;
225	bus_dmamap_t map;
226	struct mbuf *m;
227	int flags;
228{
229	return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
230}
231
232/*
233 * Load a VSBUS SGMAP-mapped DMA map with a uio.
234 */
235static int
236sgmap_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
237	bus_dma_tag_t t;
238	bus_dmamap_t map;
239	struct uio *uio;
240	int flags;
241{
242	return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
243}
244
245/*
246 * Load a VSBUS SGMAP-mapped DMA map with raw memory.
247 */
248static int
249sgmap_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
250	bus_dma_tag_t t;
251	bus_dmamap_t map;
252	bus_dma_segment_t *segs;
253	int nsegs;
254	bus_size_t size;
255	int flags;
256{
257	return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap);
258}
259
260/*
261 * Unload a VSBUS DMA map.
262 */
263static void
264sgmap_bus_dmamap_unload_sgmap(t, map)
265	bus_dma_tag_t t;
266	bus_dmamap_t map;
267{
268	/*
269	 * Invalidate any SGMAP page table entries used by this
270	 * mapping.
271	 */
272	vax_sgmap_unload(t, map, t->_sgmap);
273
274	/*
275	 * Do the generic bits of the unload.
276	 */
277	_bus_dmamap_unload(t, map);
278}
279
280/*
281 * Sync the bus map.
282 */
283static void
284sgmap_bus_dmamap_sync(tag, dmam, offset, len, ops)
285	bus_dma_tag_t tag;
286	bus_dmamap_t dmam;
287	bus_addr_t offset;
288	bus_size_t len;
289	int ops;
290{
291	/* not needed */
292}
293