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