xref: /netbsd/sys/arch/vax/uba/uba_dma.c (revision b57cbd6a)
1 /* $NetBSD: uba_dma.c,v 1.11 2010/12/14 23:38:30 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: uba_dma.c,v 1.11 2010/12/14 23:38:30 matt Exp $");
35 
36 #define _VAX_BUS_DMA_PRIVATE
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/device.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #include <machine/sgmap.h>
49 
50 #include <dev/qbus/ubavar.h>
51 
52 #include <arch/vax/uba/uba_common.h>
53 
54 int	uba_bus_dmamap_create_sgmap(bus_dma_tag_t, bus_size_t, int,
55 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
56 
57 void	uba_bus_dmamap_destroy_sgmap(bus_dma_tag_t, bus_dmamap_t);
58 
59 int	uba_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
60 	    bus_size_t, struct proc *, int);
61 
62 int	uba_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
63 	    struct mbuf *, int);
64 
65 int	uba_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
66 	    struct uio *, int);
67 
68 int	uba_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
69 	    bus_dma_segment_t *, int, bus_size_t, int);
70 
71 void	uba_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
72 
73 void	uba_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
74 	    bus_size_t, int);
75 
76 void
uba_dma_init(struct uba_vsoftc * sc)77 uba_dma_init(struct uba_vsoftc *sc)
78 {
79 	bus_dma_tag_t t;
80 	struct pte *pte;
81 
82 	/*
83 	 * Initialize the DMA tag used for sgmap-mapped DMA.
84 	 */
85 	t = &sc->uv_dmat;
86 	t->_cookie = sc;
87 	t->_wbase = 0;
88 	t->_wsize = sc->uv_size;
89 	t->_boundary = 0;
90 	t->_sgmap = &sc->uv_sgmap;
91 	t->_dmamap_create = uba_bus_dmamap_create_sgmap;
92 	t->_dmamap_destroy = uba_bus_dmamap_destroy_sgmap;
93 	t->_dmamap_load = uba_bus_dmamap_load_sgmap;
94 	t->_dmamap_load_mbuf = uba_bus_dmamap_load_mbuf_sgmap;
95 	t->_dmamap_load_uio = uba_bus_dmamap_load_uio_sgmap;
96 	t->_dmamap_load_raw = uba_bus_dmamap_load_raw_sgmap;
97 	t->_dmamap_unload = uba_bus_dmamap_unload_sgmap;
98 	t->_dmamap_sync = uba_bus_dmamap_sync;
99 
100 	t->_dmamem_alloc = _bus_dmamem_alloc;
101 	t->_dmamem_free = _bus_dmamem_free;
102 	t->_dmamem_map = _bus_dmamem_map;
103 	t->_dmamem_unmap = _bus_dmamem_unmap;
104 	t->_dmamem_mmap = _bus_dmamem_mmap;
105 
106 	/*
107 	 * Map in Unibus map registers, if not mapped in already.
108 	 */
109 	if (sc->uv_uba) {
110 		pte = sc->uv_uba->uba_map;
111 	} else {
112 		pte = (struct pte *)vax_map_physmem(sc->uv_addr,
113 		    vax_btoc(vax_btoc(sc->uv_size) * sizeof(struct pte)));
114 		if (pte == 0)
115 			panic("uba_dma_init");
116 	}
117 	/*
118 	 * Initialize the SGMAP.
119 	 */
120 	vax_sgmap_init(t, &sc->uv_sgmap, "uba_sgmap", 0, sc->uv_size, pte, 0);
121 
122 }
123 
124 /*
125  * Create a UBA SGMAP-mapped DMA map.
126  */
127 int
uba_bus_dmamap_create_sgmap(bus_dma_tag_t t,bus_size_t size,int nsegments,bus_size_t maxsegsz,bus_size_t boundary,int flags,bus_dmamap_t * dmamp)128 uba_bus_dmamap_create_sgmap(bus_dma_tag_t t, bus_size_t size,  int nsegments,
129 	bus_size_t maxsegsz, bus_size_t boundary, int flags,
130 	bus_dmamap_t *dmamp)
131 {
132 	bus_dmamap_t map;
133 	int error;
134 
135 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
136 	    boundary, flags, dmamp);
137 	if (error)
138 		return (error);
139 
140 	map = *dmamp;
141 
142 	if (flags & BUS_DMA_ALLOCNOW) {
143 		error = vax_sgmap_alloc(map, vax_round_page(size),
144 		    t->_sgmap, flags);
145 		if (error)
146 			uba_bus_dmamap_destroy_sgmap(t, map);
147 	}
148 
149 	return (error);
150 }
151 
152 /*
153  * Destroy a UBA SGMAP-mapped DMA map.
154  */
155 void
uba_bus_dmamap_destroy_sgmap(bus_dma_tag_t t,bus_dmamap_t map)156 uba_bus_dmamap_destroy_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
157 {
158 
159 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
160 		vax_sgmap_free(map, t->_sgmap);
161 
162 	_bus_dmamap_destroy(t, map);
163 }
164 
165 /*
166  * Load a UBA SGMAP-mapped DMA map with a linear buffer.
167  */
168 int
uba_bus_dmamap_load_sgmap(bus_dma_tag_t t,bus_dmamap_t map,void * buf,bus_size_t buflen,struct proc * p,int flags)169 uba_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
170 	bus_size_t buflen, struct proc *p, int flags)
171 {
172 	int error;
173 
174 	error = vax_sgmap_load(t, map, buf, buflen, p, flags, t->_sgmap);
175 	/*
176 	 * XXX - Set up BDPs.
177 	 */
178 
179 	return (error);
180 }
181 
182 /*
183  * Load a UBA SGMAP-mapped DMA map with an mbuf chain.
184  */
185 int
uba_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t,bus_dmamap_t map,struct mbuf * m,int flags)186 uba_bus_dmamap_load_mbuf_sgmap( bus_dma_tag_t t, bus_dmamap_t map,
187 	struct mbuf *m, int flags)
188 {
189 	int error;
190 
191 	error = vax_sgmap_load_mbuf(t, map, m, flags, t->_sgmap);
192 
193 	return (error);
194 }
195 
196 /*
197  * Load a UBA SGMAP-mapped DMA map with a uio.
198  */
199 int
uba_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t,bus_dmamap_t map,struct uio * uio,int flags)200 uba_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
201 	struct uio *uio, int flags)
202 {
203 	int error;
204 
205 	error = vax_sgmap_load_uio(t, map, uio, flags, t->_sgmap);
206 
207 	return (error);
208 }
209 
210 /*
211  * Load a UBA SGMAP-mapped DMA map with raw memory.
212  */
213 int
uba_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t,bus_dmamap_t map,bus_dma_segment_t * segs,int nsegs,bus_size_t size,int flags)214 uba_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
215 	bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
216 {
217 	int error;
218 
219 	error = vax_sgmap_load_raw(t, map, segs, nsegs, size, flags,
220 	    t->_sgmap);
221 
222 	return (error);
223 }
224 
225 /*
226  * Unload a UBA DMA map.
227  */
228 void
uba_bus_dmamap_unload_sgmap(bus_dma_tag_t t,bus_dmamap_t map)229 uba_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
230 {
231 	/*
232 	 * Invalidate any SGMAP page table entries used by this
233 	 * mapping.
234 	 */
235 	vax_sgmap_unload(t, map, t->_sgmap);
236 
237 	/*
238 	 * Do the generic bits of the unload.
239 	 */
240 	_bus_dmamap_unload(t, map);
241 }
242 
243 /*
244  * Sync the bus map. This is only needed if BDP's are used.
245  */
246 void
uba_bus_dmamap_sync(bus_dma_tag_t tag,bus_dmamap_t dmam,bus_addr_t offset,bus_size_t len,int ops)247 uba_bus_dmamap_sync(bus_dma_tag_t tag, bus_dmamap_t dmam, bus_addr_t offset,
248 	bus_size_t len, int ops)
249 {
250 	/* Only BDP handling, but not yet. */
251 }
252