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