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