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