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