vsbus_dma.c revision 1.2
1/* $NetBSD: vsbus_dma.c,v 1.2 2000/03/04 00:22:37 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	vs_regs = vax_map_physmem(VS_REGS, 1);
129	*(int *) (vs_regs + 8) = segs->ds_addr;	/* set MAP BASE 0x2008008 */
130	vax_unmap_physmem(vs_regs, 1);
131
132	/*
133	 * Initialize the SGMAP.
134	 */
135	vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0);
136
137}
138
139/*
140 * Create a VSBUS SGMAP-mapped DMA map.
141 */
142int
143vsbus_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
144    flags, dmamp)
145	bus_dma_tag_t t;
146	bus_size_t size;
147	int nsegments;
148	bus_size_t maxsegsz;
149	bus_size_t boundary;
150	int flags;
151	bus_dmamap_t *dmamp;
152{
153	bus_dmamap_t map;
154	int error;
155
156	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
157	    boundary, flags, dmamp);
158	if (error)
159		return (error);
160
161	map = *dmamp;
162
163	if (flags & BUS_DMA_ALLOCNOW) {
164		error = vax_sgmap_alloc(map, vax_round_page(size),
165		    t->_sgmap, flags);
166		if (error)
167			vsbus_bus_dmamap_destroy_sgmap(t, map);
168	}
169
170	return (error);
171}
172
173/*
174 * Destroy a VSBUS SGMAP-mapped DMA map.
175 */
176static void
177vsbus_bus_dmamap_destroy_sgmap(t, map)
178	bus_dma_tag_t t;
179	bus_dmamap_t map;
180{
181
182	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
183		vax_sgmap_free(map, t->_sgmap);
184
185	_bus_dmamap_destroy(t, map);
186}
187
188/*
189 * Load a VSBUS SGMAP-mapped DMA map with a linear buffer.
190 */
191static int
192vsbus_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
193	bus_dma_tag_t t;
194	bus_dmamap_t map;
195	void *buf;
196	bus_size_t buflen;
197	struct proc *p;
198	int flags;
199{
200	return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
201}
202
203/*
204 * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain.
205 */
206static int
207vsbus_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
208	bus_dma_tag_t t;
209	bus_dmamap_t map;
210	struct mbuf *m;
211	int flags;
212{
213	return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
214}
215
216/*
217 * Load a VSBUS SGMAP-mapped DMA map with a uio.
218 */
219static int
220vsbus_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	return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
227}
228
229/*
230 * Load a VSBUS SGMAP-mapped DMA map with raw memory.
231 */
232static int
233vsbus_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
234	bus_dma_tag_t t;
235	bus_dmamap_t map;
236	bus_dma_segment_t *segs;
237	int nsegs;
238	bus_size_t size;
239	int flags;
240{
241	return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap);
242}
243
244/*
245 * Unload a VSBUS DMA map.
246 */
247static void
248vsbus_bus_dmamap_unload_sgmap(t, map)
249	bus_dma_tag_t t;
250	bus_dmamap_t map;
251{
252	/*
253	 * Invalidate any SGMAP page table entries used by this
254	 * mapping.
255	 */
256	vax_sgmap_unload(t, map, t->_sgmap);
257
258	/*
259	 * Do the generic bits of the unload.
260	 */
261	_bus_dmamap_unload(t, map);
262}
263
264/*
265 * Sync the bus map.
266 */
267static void
268vsbus_bus_dmamap_sync(tag, dmam, offset, len, ops)
269	bus_dma_tag_t tag;
270	bus_dmamap_t dmam;
271	bus_addr_t offset;
272	bus_size_t len;
273	int ops;
274{
275	/* not needed */
276}
277