vsbus_dma.c revision 1.1
1/* $NetBSD: vsbus_dma.c,v 1.1 2000/03/03 22:57:49 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	struct pte *pte;
82
83	/*
84	 * Initialize the DMA tag used for sgmap-mapped DMA.
85	 */
86	t = &sc->sc_dmatag;
87	t->_cookie = sc;
88	t->_wbase = 0;
89	t->_wsize = 16*1024*1024;
90	t->_boundary = 0;
91	t->_sgmap = &sc->sc_sgmap;
92	t->_dmamap_create = vsbus_bus_dmamap_create_sgmap;
93	t->_dmamap_destroy = vsbus_bus_dmamap_destroy_sgmap;
94	t->_dmamap_load = vsbus_bus_dmamap_load_sgmap;
95	t->_dmamap_load_mbuf = vsbus_bus_dmamap_load_mbuf_sgmap;
96	t->_dmamap_load_uio = vsbus_bus_dmamap_load_uio_sgmap;
97	t->_dm7	t->_dmamap_load_uio = vsbus_bus_dmamap_loa
98	t->_dmamap_load_raw = vsbus_bus_dmamap_load_raw_sgmap;
99	t->_dmamap_unload = vsbus_bus_dmamap_unload_sgmap;
100	t->_dmamap_sync = vsbus_bus_dmamap_sync;
101
102	t->_dmamem_alloc = _bus_dmamem_alloc;
103	t->_dmamem_free = _bus_dmamem_free;
104	t->_dmamem_map = _bus_dmamem_map;
105	t->_dmamem_unmap = _bus_dmamem_unmap;
106	t->_dmamem_mmap = _bus_dmamem_mmap;
107
108	/*
109	 * Allocate and map the VS4000 scatter gather map.
110	 */
111
112	pte = (struct pte *)vax_map_physmem(sc->uv_addr, sc->uv_size/VAX_NBPG);
113	if (pte == 0)
114		panic("uba_dma_init");
115	/*
116	 * Initialize the SGMAP.
117	 */
118	vax_sgmap_init(t, &sc->sc_sgmap, "vsbus_sgmap", t->_wbase, t->_wsize, pte, 0);
119
120}
121
122/*
123 * Create a VSBUS SGMAP-mapped DMA map.
124 */
125int
126vsbus_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
127    flags, dmamp)
128	bus_dma_tag_t t;
129	bus_size_t size;
130	int nsegments;
131	bus_size_t maxsegsz;
132	bus_size_t boundary;
133	int flags;
134	bus_dmamap_t *dmamp;
135{
136	bus_dmamap_t map;
137	int error;
138
139	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
140	    boundary, flags, dmamp);
141	if (error)
142		return (error);
143
144	map = *dmamp;
145
146	if (flags & BUS_DMA_ALLOCNOW) {
147		error = vax_sgmap_alloc(map, vax_round_page(size),
148		    t->_sgmap, flags);
149		if (error)
150			vsbus_bus_dmamap_destroy_sgmap(t, map);
151	}
152
153	return (error);
154}
155
156/*
157 * Destroy a VSBUS SGMAP-mapped DMA map.
158 */
159static void
160vsbus_bus_dmamap_destroy_sgmap(t, map)
161	bus_dma_tag_t t;
162	bus_dmamap_t map;
163{
164
165	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
166		vax_sgmap_free(map, t->_sgmap);
167
168	_bus_dmamap_destroy(t, map);
169}
170
171/*
172 * Load a VSBUS SGMAP-mapped DMA map with a linear buffer.
173 */
174static int
175vsbus_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
176	bus_dma_tag_t t;
177	bus_dmamap_t map;
178	void *buf;
179	bus_size_t buflen;
180	struct proc *p;
181	int flags;
182{
183	return vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
184}
185
186/*
187 * Load a VSBUS SGMAP-mapped DMA map with an mbuf chain.
188 */
189static int
190vsbus_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
191	bus_dma_tag_t t;
192	bus_dmamap_t map;
193	struct mbuf *m;
194	int flags;
195{
196	return vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
197}
198
199/*
200 * Load a VSBUS SGMAP-mapped DMA map with a uio.
201 */
202static int
203vsbus_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
204	bus_dma_tag_t t;
205	bus_dmamap_t map;
206	struct uio *uio;
207	int flags;
208{
209	return vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
210}
211
212/*
213 * Load a VSBUS SGMAP-mapped DMA map with raw memory.
214 */
215static int
216vsbus_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
217	bus_dma_tag_t t;
218	bus_dmamap_t map;
219	bus_dma_segment_t *segs;
220	int nsegs;
221	bus_size_t size;
222	int flags;
223{
224	return vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, t->_sgmap);
225}
226
227/*
228 * Unload a VSBUS DMA map.
229 */
230static void
231vsbus_bus_dmamap_unload_sgmap(t, map)
232	bus_dma_tag_t t;
233	bus_dmamap_t map;
234{
235	/*
236	 * Invalidate any SGMAP page table entries used by this
237	 * mapping.
238	 */
239	vax_sgmap_unload(t, map, t->_sgmap);
240
241	/*
242	 * Do the generic bits of the unload.
243	 */
244	_bus_dmamap_unload(t, map);
245}
246
247/*
248 * Sync the bus map.
249 */
250static void
251vsbus_bus_dmamap_sync(tag, dmam, offset, len, ops)
252	bus_dma_tag_t tag;
253	bus_dmamap_t dmam;
254	bus_addr_t offset;
255	bus_size_t len;
256	int ops;
257{
258	/* not needed */
259}
260