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