int_bus_dma.c revision 1.11.6.1 1 1.11.6.1 skrll /* $NetBSD: int_bus_dma.c,v 1.11.6.1 2004/08/03 10:34:01 skrll Exp $ */
2 1.1 rearnsha
3 1.10 thorpej /*
4 1.10 thorpej * Copyright (c) 2002 Wasabi Systems, Inc.
5 1.1 rearnsha * All rights reserved.
6 1.1 rearnsha *
7 1.10 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 rearnsha *
9 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
10 1.1 rearnsha * modification, are permitted provided that the following conditions
11 1.1 rearnsha * are met:
12 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
13 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
14 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
16 1.1 rearnsha * documentation and/or other materials provided with the distribution.
17 1.1 rearnsha * 3. All advertising materials mentioning features or use of this software
18 1.1 rearnsha * must display the following acknowledgement:
19 1.10 thorpej * This product includes software developed for the NetBSD Project by
20 1.10 thorpej * Wasabi Systems, Inc.
21 1.10 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.10 thorpej * or promote products derived from this software without specific prior
23 1.10 thorpej * written permission.
24 1.1 rearnsha *
25 1.10 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.10 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 rearnsha * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.10 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 rearnsha * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 rearnsha * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 rearnsha * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 rearnsha * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 rearnsha * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 rearnsha * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 rearnsha * POSSIBILITY OF SUCH DAMAGE.
36 1.1 rearnsha */
37 1.10 thorpej
38 1.10 thorpej /*
39 1.10 thorpej * PCI DMA support for the ARM Integrator.
40 1.1 rearnsha */
41 1.1 rearnsha
42 1.11.6.1 skrll #define _ARM32_BUS_DMA_PRIVATE
43 1.11.6.1 skrll
44 1.11.6.1 skrll #include <sys/cdefs.h>
45 1.11.6.1 skrll __KERNEL_RCSID(0, "$NetBSD: int_bus_dma.c,v 1.11.6.1 2004/08/03 10:34:01 skrll Exp $");
46 1.11.6.1 skrll
47 1.1 rearnsha #include <sys/param.h>
48 1.1 rearnsha #include <sys/systm.h>
49 1.10 thorpej #include <sys/device.h>
50 1.1 rearnsha #include <sys/malloc.h>
51 1.1 rearnsha #include <sys/mbuf.h>
52 1.1 rearnsha
53 1.1 rearnsha #include <uvm/uvm_extern.h>
54 1.1 rearnsha
55 1.11.6.1 skrll #include <machine/bootconfig.h>
56 1.11.6.1 skrll
57 1.1 rearnsha #include <evbarm/integrator/int_bus_dma.h>
58 1.1 rearnsha
59 1.11.6.1 skrll struct integrator_dma_cookie {
60 1.11.6.1 skrll int id_flags; /* flags; see below */
61 1.11.6.1 skrll
62 1.11.6.1 skrll /*
63 1.11.6.1 skrll * Information about the original buffer used during
64 1.11.6.1 skrll * DMA map syncs. Note that origbuflen is only used
65 1.11.6.1 skrll * for ID_BUFTYPE_LINEAR.
66 1.11.6.1 skrll */
67 1.11.6.1 skrll void *id_origbuf; /* pointer to orig buffer if
68 1.11.6.1 skrll bouncing */
69 1.11.6.1 skrll bus_size_t id_origbuflen; /* ...and size */
70 1.11.6.1 skrll int id_buftype; /* type of buffer */
71 1.11.6.1 skrll
72 1.11.6.1 skrll void *id_bouncebuf; /* pointer to the bounce buffer */
73 1.11.6.1 skrll bus_size_t id_bouncebuflen; /* ...and size */
74 1.11.6.1 skrll int id_nbouncesegs; /* number of valid bounce segs */
75 1.11.6.1 skrll bus_dma_segment_t id_bouncesegs[0]; /* array of bounce buffer
76 1.11.6.1 skrll physical memory segments */
77 1.11.6.1 skrll };
78 1.11.6.1 skrll /* id_flags */
79 1.11.6.1 skrll #define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */
80 1.11.6.1 skrll #define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */
81 1.11.6.1 skrll #define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */
82 1.11.6.1 skrll
83 1.11.6.1 skrll /* id_buftype */
84 1.11.6.1 skrll #define ID_BUFTYPE_INVALID 0
85 1.11.6.1 skrll #define ID_BUFTYPE_LINEAR 1
86 1.11.6.1 skrll #define ID_BUFTYPE_MBUF 2
87 1.11.6.1 skrll #define ID_BUFTYPE_UIO 3
88 1.11.6.1 skrll #define ID_BUFTYPE_RAW 4
89 1.11.6.1 skrll
90 1.11.6.1 skrll #define DEBUG(x)
91 1.11.6.1 skrll
92 1.11.6.1 skrll static struct arm32_dma_range integrator_dma_ranges[DRAM_BLOCKS];
93 1.11.6.1 skrll
94 1.11.6.1 skrll extern BootConfig bootconfig;
95 1.11.6.1 skrll
96 1.11.6.1 skrll static int integrator_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int,
97 1.11.6.1 skrll bus_size_t, bus_size_t, int, bus_dmamap_t *);
98 1.11.6.1 skrll static void integrator_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
99 1.11.6.1 skrll static int integrator_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
100 1.11.6.1 skrll bus_size_t, struct proc *, int);
101 1.11.6.1 skrll static int integrator_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
102 1.11.6.1 skrll struct mbuf *, int);
103 1.11.6.1 skrll static int integrator_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
104 1.11.6.1 skrll struct uio *, int);
105 1.11.6.1 skrll static int integrator_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
106 1.11.6.1 skrll bus_dma_segment_t *, int, bus_size_t, int);
107 1.11.6.1 skrll static void integrator_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
108 1.11.6.1 skrll static void integrator_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t,
109 1.11.6.1 skrll bus_addr_t, bus_size_t, int);
110 1.11.6.1 skrll static int integrator_bus_dmamem_alloc(bus_dma_tag_t, bus_size_t,
111 1.11.6.1 skrll bus_size_t, bus_size_t, bus_dma_segment_t *, int, int *, int);
112 1.11.6.1 skrll static int integrator_dma_alloc_bouncebuf(bus_dma_tag_t, bus_dmamap_t,
113 1.11.6.1 skrll bus_size_t, int);
114 1.11.6.1 skrll static void integrator_dma_free_bouncebuf(bus_dma_tag_t, bus_dmamap_t);
115 1.11.6.1 skrll
116 1.11.6.1 skrll
117 1.11.6.1 skrll /*
118 1.11.6.1 skrll * Create an Integrator DMA map.
119 1.11.6.1 skrll */
120 1.11.6.1 skrll static int
121 1.11.6.1 skrll integrator_bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
122 1.11.6.1 skrll bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
123 1.11.6.1 skrll {
124 1.11.6.1 skrll struct integrator_dma_cookie *cookie;
125 1.11.6.1 skrll bus_dmamap_t map;
126 1.11.6.1 skrll int error, cookieflags;
127 1.11.6.1 skrll void *cookiestore;
128 1.11.6.1 skrll size_t cookiesize;
129 1.11.6.1 skrll
130 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_create(tag %x, size %x, nseg %d, max %x,"
131 1.11.6.1 skrll " boundary %x, flags %x, dmamap %p)\n", (unsigned) t,
132 1.11.6.1 skrll (unsigned) size, nsegments, (unsigned) maxsegsz,
133 1.11.6.1 skrll (unsigned)boundary, flags, dmamp));
134 1.11.6.1 skrll
135 1.11.6.1 skrll /* Call common function to create the basic map. */
136 1.11.6.1 skrll error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
137 1.11.6.1 skrll flags, dmamp);
138 1.11.6.1 skrll if (error)
139 1.11.6.1 skrll return (error);
140 1.11.6.1 skrll
141 1.11.6.1 skrll map = *dmamp;
142 1.11.6.1 skrll map->_dm_cookie = NULL;
143 1.11.6.1 skrll
144 1.11.6.1 skrll cookiesize = sizeof(struct integrator_dma_cookie);
145 1.11.6.1 skrll
146 1.11.6.1 skrll /*
147 1.11.6.1 skrll * Some CM boards have private memory which is significantly
148 1.11.6.1 skrll * faster than the normal memory stick. To support this
149 1.11.6.1 skrll * memory we have to bounce any DMA transfers.
150 1.11.6.1 skrll *
151 1.11.6.1 skrll * In order to DMA to arbitrary buffers, we use "bounce
152 1.11.6.1 skrll * buffers" - pages in in the main PCI visible memory. On DMA
153 1.11.6.1 skrll * reads, DMA happens to the bounce buffers, and is copied
154 1.11.6.1 skrll * into the caller's buffer. On writes, data is copied into
155 1.11.6.1 skrll * but bounce buffer, and the DMA happens from those pages.
156 1.11.6.1 skrll * To software using the DMA mapping interface, this looks
157 1.11.6.1 skrll * simply like a data cache.
158 1.11.6.1 skrll *
159 1.11.6.1 skrll * If we have private RAM in the system, we may need bounce
160 1.11.6.1 skrll * buffers. We check and remember that here.
161 1.11.6.1 skrll */
162 1.11.6.1 skrll #if 0
163 1.11.6.1 skrll cookieflags = ID_MIGHT_NEED_BOUNCE;
164 1.11.6.1 skrll #else
165 1.11.6.1 skrll cookieflags = 0;
166 1.11.6.1 skrll #endif
167 1.11.6.1 skrll cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
168 1.11.6.1 skrll
169 1.11.6.1 skrll /*
170 1.11.6.1 skrll * Allocate our cookie.
171 1.11.6.1 skrll */
172 1.11.6.1 skrll if ((cookiestore = malloc(cookiesize, M_DMAMAP,
173 1.11.6.1 skrll (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
174 1.11.6.1 skrll error = ENOMEM;
175 1.11.6.1 skrll goto out;
176 1.11.6.1 skrll }
177 1.11.6.1 skrll memset(cookiestore, 0, cookiesize);
178 1.11.6.1 skrll cookie = (struct integrator_dma_cookie *)cookiestore;
179 1.11.6.1 skrll cookie->id_flags = cookieflags;
180 1.11.6.1 skrll map->_dm_cookie = cookie;
181 1.11.6.1 skrll
182 1.11.6.1 skrll if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
183 1.11.6.1 skrll /*
184 1.11.6.1 skrll * Allocate the bounce pages now if the caller
185 1.11.6.1 skrll * wishes us to do so.
186 1.11.6.1 skrll */
187 1.11.6.1 skrll if ((flags & BUS_DMA_ALLOCNOW) == 0)
188 1.11.6.1 skrll goto out;
189 1.11.6.1 skrll
190 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_create bouncebuf alloc\n"));
191 1.11.6.1 skrll error = integrator_dma_alloc_bouncebuf(t, map, size, flags);
192 1.11.6.1 skrll }
193 1.11.6.1 skrll
194 1.11.6.1 skrll out:
195 1.11.6.1 skrll if (error) {
196 1.11.6.1 skrll if (map->_dm_cookie != NULL)
197 1.11.6.1 skrll free(map->_dm_cookie, M_DMAMAP);
198 1.11.6.1 skrll _bus_dmamap_destroy(t, map);
199 1.11.6.1 skrll printf("I_bus_dmamap_create failed (%d)\n", error);
200 1.11.6.1 skrll }
201 1.11.6.1 skrll return (error);
202 1.11.6.1 skrll }
203 1.11.6.1 skrll
204 1.11.6.1 skrll /*
205 1.11.6.1 skrll * Destroy an ISA DMA map.
206 1.11.6.1 skrll */
207 1.11.6.1 skrll static void
208 1.11.6.1 skrll integrator_bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
209 1.11.6.1 skrll {
210 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
211 1.11.6.1 skrll
212 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_destroy (tag %x, map %x)\n", (unsigned) t,
213 1.11.6.1 skrll (unsigned) map));
214 1.11.6.1 skrll /*
215 1.11.6.1 skrll * Free any bounce pages this map might hold.
216 1.11.6.1 skrll */
217 1.11.6.1 skrll if (cookie->id_flags & ID_HAS_BOUNCE) {
218 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_destroy bouncebuf\n"));
219 1.11.6.1 skrll integrator_dma_free_bouncebuf(t, map);
220 1.11.6.1 skrll }
221 1.11.6.1 skrll
222 1.11.6.1 skrll free(cookie, M_DMAMAP);
223 1.11.6.1 skrll _bus_dmamap_destroy(t, map);
224 1.11.6.1 skrll }
225 1.11.6.1 skrll
226 1.11.6.1 skrll /*
227 1.11.6.1 skrll * Load an Integrator DMA map with a linear buffer.
228 1.11.6.1 skrll */
229 1.11.6.1 skrll static int
230 1.11.6.1 skrll integrator_bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
231 1.11.6.1 skrll bus_size_t buflen, struct proc *p, int flags)
232 1.11.6.1 skrll {
233 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
234 1.11.6.1 skrll int error;
235 1.11.6.1 skrll
236 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_load (tag %x, map %x, buf %p, len %u,"
237 1.11.6.1 skrll " proc %p, flags %d)\n", (unsigned) t, (unsigned) map, buf,
238 1.11.6.1 skrll (unsigned) buflen, p, flags));
239 1.11.6.1 skrll /*
240 1.11.6.1 skrll * Make sure that on error condition we return "no valid mappings."
241 1.11.6.1 skrll */
242 1.11.6.1 skrll map->dm_mapsize = 0;
243 1.11.6.1 skrll map->dm_nsegs = 0;
244 1.11.6.1 skrll
245 1.11.6.1 skrll /*
246 1.11.6.1 skrll * Try to load the map the normal way. If this errors out,
247 1.11.6.1 skrll * and we can bounce, we will.
248 1.11.6.1 skrll */
249 1.11.6.1 skrll error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
250 1.11.6.1 skrll if (error == 0 ||
251 1.11.6.1 skrll (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
252 1.11.6.1 skrll return (error);
253 1.11.6.1 skrll
254 1.11.6.1 skrll /*
255 1.11.6.1 skrll * First attempt failed; bounce it.
256 1.11.6.1 skrll */
257 1.11.6.1 skrll
258 1.11.6.1 skrll /*
259 1.11.6.1 skrll * Allocate bounce pages, if necessary.
260 1.11.6.1 skrll */
261 1.11.6.1 skrll if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
262 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_load alloc bouncebuf\n"));
263 1.11.6.1 skrll error = integrator_dma_alloc_bouncebuf(t, map, buflen, flags);
264 1.11.6.1 skrll if (error)
265 1.11.6.1 skrll return (error);
266 1.11.6.1 skrll }
267 1.11.6.1 skrll
268 1.11.6.1 skrll /*
269 1.11.6.1 skrll * Cache a pointer to the caller's buffer and load the DMA map
270 1.11.6.1 skrll * with the bounce buffer.
271 1.11.6.1 skrll */
272 1.11.6.1 skrll cookie->id_origbuf = buf;
273 1.11.6.1 skrll cookie->id_origbuflen = buflen;
274 1.11.6.1 skrll cookie->id_buftype = ID_BUFTYPE_LINEAR;
275 1.11.6.1 skrll error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
276 1.11.6.1 skrll NULL, flags);
277 1.11.6.1 skrll if (error) {
278 1.11.6.1 skrll /*
279 1.11.6.1 skrll * Free the bounce pages, unless our resources
280 1.11.6.1 skrll * are reserved for our exclusive use.
281 1.11.6.1 skrll */
282 1.11.6.1 skrll if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
283 1.11.6.1 skrll integrator_dma_free_bouncebuf(t, map);
284 1.11.6.1 skrll return (error);
285 1.11.6.1 skrll }
286 1.11.6.1 skrll
287 1.11.6.1 skrll /* ...so integrator_bus_dmamap_sync() knows we're bouncing */
288 1.11.6.1 skrll cookie->id_flags |= ID_IS_BOUNCING;
289 1.11.6.1 skrll return (0);
290 1.11.6.1 skrll }
291 1.11.6.1 skrll
292 1.11.6.1 skrll /*
293 1.11.6.1 skrll * Like integrator_bus_dmamap_load(), but for mbufs.
294 1.11.6.1 skrll */
295 1.11.6.1 skrll static int
296 1.11.6.1 skrll integrator_bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map,
297 1.11.6.1 skrll struct mbuf *m0, int flags)
298 1.11.6.1 skrll {
299 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
300 1.11.6.1 skrll int error;
301 1.11.6.1 skrll
302 1.11.6.1 skrll /*
303 1.11.6.1 skrll * Make sure that on error condition we return "no valid mappings."
304 1.11.6.1 skrll */
305 1.11.6.1 skrll map->dm_mapsize = 0;
306 1.11.6.1 skrll map->dm_nsegs = 0;
307 1.11.6.1 skrll
308 1.11.6.1 skrll #ifdef DIAGNOSTIC
309 1.11.6.1 skrll if ((m0->m_flags & M_PKTHDR) == 0)
310 1.11.6.1 skrll panic("integrator_bus_dmamap_load_mbuf: no packet header");
311 1.11.6.1 skrll #endif
312 1.11.6.1 skrll
313 1.11.6.1 skrll if (m0->m_pkthdr.len > map->_dm_size)
314 1.11.6.1 skrll return (EINVAL);
315 1.11.6.1 skrll
316 1.11.6.1 skrll /*
317 1.11.6.1 skrll * Try to load the map the normal way. If this errors out,
318 1.11.6.1 skrll * and we can bounce, we will.
319 1.11.6.1 skrll */
320 1.11.6.1 skrll error = _bus_dmamap_load_mbuf(t, map, m0, flags);
321 1.11.6.1 skrll if (error == 0 ||
322 1.11.6.1 skrll (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
323 1.11.6.1 skrll return (error);
324 1.11.6.1 skrll
325 1.11.6.1 skrll /*
326 1.11.6.1 skrll * First attempt failed; bounce it.
327 1.11.6.1 skrll *
328 1.11.6.1 skrll * Allocate bounce pages, if necessary.
329 1.11.6.1 skrll */
330 1.11.6.1 skrll if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
331 1.11.6.1 skrll error = integrator_dma_alloc_bouncebuf(t, map,
332 1.11.6.1 skrll m0->m_pkthdr.len, flags);
333 1.11.6.1 skrll if (error)
334 1.11.6.1 skrll return (error);
335 1.11.6.1 skrll }
336 1.11.6.1 skrll
337 1.11.6.1 skrll /*
338 1.11.6.1 skrll * Cache a pointer to the caller's buffer and load the DMA map
339 1.11.6.1 skrll * with the bounce buffer.
340 1.11.6.1 skrll */
341 1.11.6.1 skrll cookie->id_origbuf = m0;
342 1.11.6.1 skrll cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
343 1.11.6.1 skrll cookie->id_buftype = ID_BUFTYPE_MBUF;
344 1.11.6.1 skrll error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
345 1.11.6.1 skrll m0->m_pkthdr.len, NULL, flags);
346 1.11.6.1 skrll if (error) {
347 1.11.6.1 skrll /*
348 1.11.6.1 skrll * Free the bounce pages, unless our resources
349 1.11.6.1 skrll * are reserved for our exclusive use.
350 1.11.6.1 skrll */
351 1.11.6.1 skrll if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
352 1.11.6.1 skrll integrator_dma_free_bouncebuf(t, map);
353 1.11.6.1 skrll return (error);
354 1.11.6.1 skrll }
355 1.11.6.1 skrll
356 1.11.6.1 skrll /* ...so integrator_bus_dmamap_sync() knows we're bouncing */
357 1.11.6.1 skrll cookie->id_flags |= ID_IS_BOUNCING;
358 1.11.6.1 skrll return (0);
359 1.11.6.1 skrll }
360 1.11.6.1 skrll
361 1.11.6.1 skrll /*
362 1.11.6.1 skrll * Like integrator_bus_dmamap_load(), but for uios.
363 1.11.6.1 skrll */
364 1.11.6.1 skrll static int
365 1.11.6.1 skrll integrator_bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map,
366 1.11.6.1 skrll struct uio *uio, int flags)
367 1.11.6.1 skrll {
368 1.11.6.1 skrll
369 1.11.6.1 skrll panic("integrator_bus_dmamap_load_uio: not implemented");
370 1.11.6.1 skrll }
371 1.11.6.1 skrll
372 1.11.6.1 skrll /*
373 1.11.6.1 skrll * Like intgrator_bus_dmamap_load(), but for raw memory allocated with
374 1.11.6.1 skrll * bus_dmamem_alloc().
375 1.11.6.1 skrll */
376 1.11.6.1 skrll static int
377 1.11.6.1 skrll integrator_bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
378 1.11.6.1 skrll bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
379 1.11.6.1 skrll {
380 1.11.6.1 skrll
381 1.11.6.1 skrll panic("integrator_bus_dmamap_load_raw: not implemented");
382 1.11.6.1 skrll }
383 1.11.6.1 skrll
384 1.11.6.1 skrll /*
385 1.11.6.1 skrll * Unload an Integrator DMA map.
386 1.11.6.1 skrll */
387 1.11.6.1 skrll static void
388 1.11.6.1 skrll integrator_bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
389 1.11.6.1 skrll {
390 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
391 1.11.6.1 skrll
392 1.11.6.1 skrll /*
393 1.11.6.1 skrll * If we have bounce pages, free them, unless they're
394 1.11.6.1 skrll * reserved for our exclusive use.
395 1.11.6.1 skrll */
396 1.11.6.1 skrll if ((cookie->id_flags & ID_HAS_BOUNCE) &&
397 1.11.6.1 skrll (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
398 1.11.6.1 skrll integrator_dma_free_bouncebuf(t, map);
399 1.11.6.1 skrll
400 1.11.6.1 skrll cookie->id_flags &= ~ID_IS_BOUNCING;
401 1.11.6.1 skrll cookie->id_buftype = ID_BUFTYPE_INVALID;
402 1.11.6.1 skrll
403 1.11.6.1 skrll /*
404 1.11.6.1 skrll * Do the generic bits of the unload.
405 1.11.6.1 skrll */
406 1.11.6.1 skrll _bus_dmamap_unload(t, map);
407 1.11.6.1 skrll }
408 1.11.6.1 skrll
409 1.11.6.1 skrll /*
410 1.11.6.1 skrll * Synchronize an Integrator DMA map.
411 1.11.6.1 skrll */
412 1.11.6.1 skrll static void
413 1.11.6.1 skrll integrator_bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map,
414 1.11.6.1 skrll bus_addr_t offset, bus_size_t len, int ops)
415 1.11.6.1 skrll {
416 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
417 1.11.6.1 skrll
418 1.11.6.1 skrll DEBUG(printf("I_bus_dmamap_sync (tag %x, map %x, offset %x, size %u,"
419 1.11.6.1 skrll " ops %d\n", (unsigned)t, (unsigned)map, (unsigned)offset ,
420 1.11.6.1 skrll (unsigned)len, ops));
421 1.11.6.1 skrll /*
422 1.11.6.1 skrll * Mixing PRE and POST operations is not allowed.
423 1.11.6.1 skrll */
424 1.11.6.1 skrll if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
425 1.11.6.1 skrll (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
426 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: mix PRE and POST");
427 1.11.6.1 skrll
428 1.11.6.1 skrll #ifdef DIAGNOSTIC
429 1.11.6.1 skrll if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
430 1.11.6.1 skrll if (offset >= map->dm_mapsize)
431 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: bad offset");
432 1.11.6.1 skrll if (len == 0 || (offset + len) > map->dm_mapsize)
433 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: bad length");
434 1.11.6.1 skrll }
435 1.11.6.1 skrll #endif
436 1.11.6.1 skrll
437 1.11.6.1 skrll /*
438 1.11.6.1 skrll * If we're not bouncing then use the standard code.
439 1.11.6.1 skrll */
440 1.11.6.1 skrll if ((cookie->id_flags & ID_IS_BOUNCING) == 0) {
441 1.11.6.1 skrll _bus_dmamap_sync(t, map, offset, len, ops);
442 1.11.6.1 skrll return;
443 1.11.6.1 skrll }
444 1.11.6.1 skrll
445 1.11.6.1 skrll DEBUG(printf("dmamap_sync(");
446 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREREAD)
447 1.11.6.1 skrll printf("preread ");
448 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREWRITE)
449 1.11.6.1 skrll printf("prewrite ");
450 1.11.6.1 skrll if (ops & BUS_DMASYNC_POSTREAD)
451 1.11.6.1 skrll printf("postread ");
452 1.11.6.1 skrll if (ops & BUS_DMASYNC_POSTWRITE)
453 1.11.6.1 skrll printf("postwrite ");)
454 1.11.6.1 skrll
455 1.11.6.1 skrll switch (cookie->id_buftype) {
456 1.11.6.1 skrll case ID_BUFTYPE_LINEAR:
457 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREWRITE) {
458 1.11.6.1 skrll /*
459 1.11.6.1 skrll * Copy the caller's buffer to the bounce buffer.
460 1.11.6.1 skrll */
461 1.11.6.1 skrll memcpy((char *)cookie->id_bouncebuf + offset,
462 1.11.6.1 skrll (char *)cookie->id_origbuf + offset, len);
463 1.11.6.1 skrll cpu_dcache_wbinv_range((vaddr_t)cookie->id_bouncebuf +
464 1.11.6.1 skrll offset, len);
465 1.11.6.1 skrll }
466 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREREAD) {
467 1.11.6.1 skrll cpu_dcache_wbinv_range((vaddr_t)cookie->id_bouncebuf +
468 1.11.6.1 skrll offset, len);
469 1.11.6.1 skrll }
470 1.11.6.1 skrll if (ops & BUS_DMASYNC_POSTREAD) {
471 1.11.6.1 skrll /*
472 1.11.6.1 skrll * Copy the bounce buffer to the caller's buffer.
473 1.11.6.1 skrll */
474 1.11.6.1 skrll memcpy((char *)cookie->id_origbuf + offset,
475 1.11.6.1 skrll (char *)cookie->id_bouncebuf + offset, len);
476 1.11.6.1 skrll }
477 1.11.6.1 skrll
478 1.11.6.1 skrll /*
479 1.11.6.1 skrll * Nothing to do for post-write.
480 1.11.6.1 skrll */
481 1.11.6.1 skrll break;
482 1.11.6.1 skrll
483 1.11.6.1 skrll case ID_BUFTYPE_MBUF:
484 1.11.6.1 skrll {
485 1.11.6.1 skrll struct mbuf *m, *m0 = cookie->id_origbuf;
486 1.11.6.1 skrll bus_size_t minlen, moff;
487 1.11.6.1 skrll
488 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREWRITE) {
489 1.11.6.1 skrll /*
490 1.11.6.1 skrll * Copy the caller's buffer to the bounce buffer.
491 1.11.6.1 skrll */
492 1.11.6.1 skrll m_copydata(m0, offset, len,
493 1.11.6.1 skrll (char *)cookie->id_bouncebuf + offset);
494 1.11.6.1 skrll cpu_dcache_wb_range((vaddr_t)cookie->id_bouncebuf +
495 1.11.6.1 skrll offset, len);
496 1.11.6.1 skrll }
497 1.11.6.1 skrll if (ops & BUS_DMASYNC_PREREAD) {
498 1.11.6.1 skrll cpu_dcache_wbinv_range ((vaddr_t)cookie->id_bouncebuf +
499 1.11.6.1 skrll offset, len);
500 1.11.6.1 skrll }
501 1.11.6.1 skrll if (ops & BUS_DMASYNC_POSTREAD) {
502 1.11.6.1 skrll /*
503 1.11.6.1 skrll * Copy the bounce buffer to the caller's buffer.
504 1.11.6.1 skrll */
505 1.11.6.1 skrll for (moff = offset, m = m0; m != NULL && len != 0;
506 1.11.6.1 skrll m = m->m_next) {
507 1.11.6.1 skrll /* Find the beginning mbuf. */
508 1.11.6.1 skrll if (moff >= m->m_len) {
509 1.11.6.1 skrll moff -= m->m_len;
510 1.11.6.1 skrll continue;
511 1.11.6.1 skrll }
512 1.11.6.1 skrll
513 1.11.6.1 skrll /*
514 1.11.6.1 skrll * Now at the first mbuf to sync; nail
515 1.11.6.1 skrll * each one until we have exhausted the
516 1.11.6.1 skrll * length.
517 1.11.6.1 skrll */
518 1.11.6.1 skrll minlen = len < m->m_len - moff ?
519 1.11.6.1 skrll len : m->m_len - moff;
520 1.11.6.1 skrll
521 1.11.6.1 skrll memcpy(mtod(m, caddr_t) + moff,
522 1.11.6.1 skrll (char *)cookie->id_bouncebuf + offset,
523 1.11.6.1 skrll minlen);
524 1.11.6.1 skrll
525 1.11.6.1 skrll moff = 0;
526 1.11.6.1 skrll len -= minlen;
527 1.11.6.1 skrll offset += minlen;
528 1.11.6.1 skrll }
529 1.11.6.1 skrll }
530 1.11.6.1 skrll /*
531 1.11.6.1 skrll * Nothing to do for post-write.
532 1.11.6.1 skrll */
533 1.11.6.1 skrll break;
534 1.11.6.1 skrll }
535 1.11.6.1 skrll
536 1.11.6.1 skrll case ID_BUFTYPE_UIO:
537 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: ID_BUFTYPE_UIO");
538 1.11.6.1 skrll break;
539 1.11.6.1 skrll
540 1.11.6.1 skrll case ID_BUFTYPE_RAW:
541 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: ID_BUFTYPE_RAW");
542 1.11.6.1 skrll break;
543 1.11.6.1 skrll
544 1.11.6.1 skrll case ID_BUFTYPE_INVALID:
545 1.11.6.1 skrll panic("integrator_bus_dmamap_sync: ID_BUFTYPE_INVALID");
546 1.11.6.1 skrll break;
547 1.11.6.1 skrll
548 1.11.6.1 skrll default:
549 1.11.6.1 skrll printf("unknown buffer type %d\n", cookie->id_buftype);
550 1.11.6.1 skrll panic("integrator_bus_dmamap_sync");
551 1.11.6.1 skrll }
552 1.11.6.1 skrll }
553 1.11.6.1 skrll
554 1.11.6.1 skrll /*
555 1.11.6.1 skrll * Allocate memory safe for Integrator DMA.
556 1.11.6.1 skrll */
557 1.11.6.1 skrll static int
558 1.11.6.1 skrll integrator_bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size,
559 1.11.6.1 skrll bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
560 1.11.6.1 skrll int nsegs, int *rsegs, int flags)
561 1.11.6.1 skrll {
562 1.11.6.1 skrll
563 1.11.6.1 skrll if (t->_ranges == NULL)
564 1.11.6.1 skrll return (ENOMEM);
565 1.11.6.1 skrll
566 1.11.6.1 skrll /* _bus_dmamem_alloc() does the range checks for us. */
567 1.11.6.1 skrll return (_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs,
568 1.11.6.1 skrll rsegs, flags));
569 1.11.6.1 skrll }
570 1.11.6.1 skrll
571 1.11.6.1 skrll /**********************************************************************
572 1.11.6.1 skrll * Integrator DMA utility functions
573 1.11.6.1 skrll **********************************************************************/
574 1.11.6.1 skrll
575 1.11.6.1 skrll static int
576 1.11.6.1 skrll integrator_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
577 1.11.6.1 skrll bus_size_t size, int flags)
578 1.11.6.1 skrll {
579 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
580 1.11.6.1 skrll int error = 0;
581 1.11.6.1 skrll
582 1.11.6.1 skrll DEBUG(printf("Alloc bouncebuf\n"));
583 1.11.6.1 skrll cookie->id_bouncebuflen = round_page(size);
584 1.11.6.1 skrll error = integrator_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
585 1.11.6.1 skrll NBPG, map->_dm_boundary, cookie->id_bouncesegs,
586 1.11.6.1 skrll map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
587 1.11.6.1 skrll if (error)
588 1.11.6.1 skrll goto out;
589 1.11.6.1 skrll {
590 1.11.6.1 skrll int seg;
591 1.11.6.1 skrll
592 1.11.6.1 skrll for (seg = 0; seg < cookie->id_nbouncesegs; seg++)
593 1.11.6.1 skrll DEBUG(printf("Seg %d @ PA 0x%08x+0x%x\n", seg,
594 1.11.6.1 skrll (unsigned) cookie->id_bouncesegs[seg].ds_addr,
595 1.11.6.1 skrll (unsigned) cookie->id_bouncesegs[seg].ds_len));
596 1.11.6.1 skrll }
597 1.11.6.1 skrll error = _bus_dmamem_map(t, cookie->id_bouncesegs,
598 1.11.6.1 skrll cookie->id_nbouncesegs, cookie->id_bouncebuflen,
599 1.11.6.1 skrll (caddr_t *)&cookie->id_bouncebuf, flags);
600 1.11.6.1 skrll
601 1.11.6.1 skrll out:
602 1.11.6.1 skrll if (error) {
603 1.11.6.1 skrll _bus_dmamem_free(t, cookie->id_bouncesegs,
604 1.11.6.1 skrll cookie->id_nbouncesegs);
605 1.11.6.1 skrll cookie->id_bouncebuflen = 0;
606 1.11.6.1 skrll cookie->id_nbouncesegs = 0;
607 1.11.6.1 skrll } else {
608 1.11.6.1 skrll DEBUG(printf("Alloc bouncebuf OK\n"));
609 1.11.6.1 skrll cookie->id_flags |= ID_HAS_BOUNCE;
610 1.11.6.1 skrll }
611 1.11.6.1 skrll
612 1.11.6.1 skrll return (error);
613 1.11.6.1 skrll }
614 1.11.6.1 skrll
615 1.11.6.1 skrll static void
616 1.11.6.1 skrll integrator_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
617 1.11.6.1 skrll {
618 1.11.6.1 skrll struct integrator_dma_cookie *cookie = map->_dm_cookie;
619 1.11.6.1 skrll
620 1.11.6.1 skrll _bus_dmamem_unmap(t, cookie->id_bouncebuf,
621 1.11.6.1 skrll cookie->id_bouncebuflen);
622 1.11.6.1 skrll _bus_dmamem_free(t, cookie->id_bouncesegs,
623 1.11.6.1 skrll cookie->id_nbouncesegs);
624 1.11.6.1 skrll cookie->id_bouncebuflen = 0;
625 1.11.6.1 skrll cookie->id_nbouncesegs = 0;
626 1.11.6.1 skrll cookie->id_flags &= ~ID_HAS_BOUNCE;
627 1.11.6.1 skrll }
628 1.1 rearnsha
629 1.1 rearnsha void
630 1.10 thorpej integrator_pci_dma_init(bus_dma_tag_t dmat)
631 1.1 rearnsha {
632 1.10 thorpej struct arm32_dma_range *dr = integrator_dma_ranges;
633 1.11.6.1 skrll int i;
634 1.11.6.1 skrll int nranges = 0;
635 1.11.6.1 skrll
636 1.11.6.1 skrll for (i = 0; i < bootconfig.dramblocks; i++)
637 1.11.6.1 skrll if (bootconfig.dram[i].flags & BOOT_DRAM_CAN_DMA) {
638 1.11.6.1 skrll dr[nranges].dr_sysbase = bootconfig.dram[i].address;
639 1.11.6.1 skrll dr[nranges].dr_busbase =
640 1.11.6.1 skrll LOCAL_TO_CM_ALIAS(dr[nranges].dr_sysbase);
641 1.11.6.1 skrll dr[nranges].dr_len = bootconfig.dram[i].pages * NBPG;
642 1.11.6.1 skrll nranges++;
643 1.11.6.1 skrll }
644 1.1 rearnsha
645 1.11.6.1 skrll if (nranges == 0)
646 1.11.6.1 skrll panic ("integrator_pci_dma_init: No DMA capable memory");
647 1.10 thorpej
648 1.10 thorpej dmat->_ranges = dr;
649 1.11.6.1 skrll dmat->_nranges = nranges;
650 1.10 thorpej
651 1.11.6.1 skrll dmat->_dmamap_create = integrator_bus_dmamap_create;
652 1.11.6.1 skrll dmat->_dmamap_destroy = integrator_bus_dmamap_destroy;
653 1.11.6.1 skrll dmat->_dmamap_load = integrator_bus_dmamap_load;
654 1.11.6.1 skrll dmat->_dmamap_load_mbuf = integrator_bus_dmamap_load_mbuf;
655 1.11.6.1 skrll dmat->_dmamap_load_uio = integrator_bus_dmamap_load_uio;
656 1.11.6.1 skrll dmat->_dmamap_load_raw = integrator_bus_dmamap_load_raw;
657 1.11.6.1 skrll dmat->_dmamap_unload = integrator_bus_dmamap_unload;
658 1.11.6.1 skrll dmat->_dmamap_sync_pre = integrator_bus_dmamap_sync;
659 1.11.6.1 skrll dmat->_dmamap_sync_post = integrator_bus_dmamap_sync;
660 1.10 thorpej
661 1.11.6.1 skrll dmat->_dmamem_alloc = integrator_bus_dmamem_alloc;
662 1.10 thorpej dmat->_dmamem_free = _bus_dmamem_free;
663 1.10 thorpej dmat->_dmamem_map = _bus_dmamem_map;
664 1.10 thorpej dmat->_dmamem_unmap = _bus_dmamem_unmap;
665 1.10 thorpej dmat->_dmamem_mmap = _bus_dmamem_mmap;
666 1.1 rearnsha }
667