bus_dma.c revision 1.76 1 /* $NetBSD: bus_dma.c,v 1.76 2013/02/14 08:07:35 matt Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #define _ARM32_BUS_DMA_PRIVATE
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.76 2013/02/14 08:07:35 matt Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/buf.h>
43 #include <sys/reboot.h>
44 #include <sys/conf.h>
45 #include <sys/file.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/vnode.h>
49 #include <sys/device.h>
50
51 #include <uvm/uvm.h>
52
53 #include <sys/bus.h>
54 #include <machine/cpu.h>
55
56 #include <arm/cpufunc.h>
57
58 #ifdef BUSDMA_COUNTERS
59 static struct evcnt bus_dma_creates =
60 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "creates");
61 static struct evcnt bus_dma_bounced_creates =
62 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced creates");
63 static struct evcnt bus_dma_loads =
64 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "loads");
65 static struct evcnt bus_dma_bounced_loads =
66 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced loads");
67 static struct evcnt bus_dma_read_bounces =
68 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "read bounces");
69 static struct evcnt bus_dma_write_bounces =
70 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "write bounces");
71 static struct evcnt bus_dma_bounced_unloads =
72 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced unloads");
73 static struct evcnt bus_dma_unloads =
74 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "unloads");
75 static struct evcnt bus_dma_bounced_destroys =
76 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "bounced destroys");
77 static struct evcnt bus_dma_destroys =
78 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "destroys");
79 static struct evcnt bus_dma_sync_prereadwrite =
80 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prereadwrite");
81 static struct evcnt bus_dma_sync_preread_begin =
82 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread begin");
83 static struct evcnt bus_dma_sync_preread =
84 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread");
85 static struct evcnt bus_dma_sync_preread_tail =
86 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync preread tail");
87 static struct evcnt bus_dma_sync_prewrite =
88 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync prewrite");
89 static struct evcnt bus_dma_sync_postread =
90 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postread");
91 static struct evcnt bus_dma_sync_postreadwrite =
92 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postreadwrite");
93 static struct evcnt bus_dma_sync_postwrite =
94 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "busdma", "sync postwrite");
95
96 EVCNT_ATTACH_STATIC(bus_dma_creates);
97 EVCNT_ATTACH_STATIC(bus_dma_bounced_creates);
98 EVCNT_ATTACH_STATIC(bus_dma_loads);
99 EVCNT_ATTACH_STATIC(bus_dma_bounced_loads);
100 EVCNT_ATTACH_STATIC(bus_dma_read_bounces);
101 EVCNT_ATTACH_STATIC(bus_dma_write_bounces);
102 EVCNT_ATTACH_STATIC(bus_dma_unloads);
103 EVCNT_ATTACH_STATIC(bus_dma_bounced_unloads);
104 EVCNT_ATTACH_STATIC(bus_dma_destroys);
105 EVCNT_ATTACH_STATIC(bus_dma_bounced_destroys);
106 EVCNT_ATTACH_STATIC(bus_dma_sync_prereadwrite);
107 EVCNT_ATTACH_STATIC(bus_dma_sync_preread_begin);
108 EVCNT_ATTACH_STATIC(bus_dma_sync_preread);
109 EVCNT_ATTACH_STATIC(bus_dma_sync_preread_tail);
110 EVCNT_ATTACH_STATIC(bus_dma_sync_prewrite);
111 EVCNT_ATTACH_STATIC(bus_dma_sync_postread);
112 EVCNT_ATTACH_STATIC(bus_dma_sync_postreadwrite);
113 EVCNT_ATTACH_STATIC(bus_dma_sync_postwrite);
114
115 #define STAT_INCR(x) (bus_dma_ ## x.ev_count++)
116 #else
117 #define STAT_INCR(x) /*(bus_dma_ ## x.ev_count++)*/
118 #endif
119
120 int _bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
121 bus_size_t, struct vmspace *, int);
122 static struct arm32_dma_range *
123 _bus_dma_paddr_inrange(struct arm32_dma_range *, int, paddr_t);
124
125 /*
126 * Check to see if the specified page is in an allowed DMA range.
127 */
128 inline struct arm32_dma_range *
129 _bus_dma_paddr_inrange(struct arm32_dma_range *ranges, int nranges,
130 bus_addr_t curaddr)
131 {
132 struct arm32_dma_range *dr;
133 int i;
134
135 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
136 if (curaddr >= dr->dr_sysbase &&
137 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
138 return (dr);
139 }
140
141 return (NULL);
142 }
143
144 /*
145 * Check to see if the specified busaddr is in an allowed DMA range.
146 */
147 static inline paddr_t
148 _bus_dma_busaddr_to_paddr(bus_dma_tag_t t, bus_addr_t curaddr)
149 {
150 struct arm32_dma_range *dr;
151 u_int i;
152
153 if (t->_nranges == 0)
154 return curaddr;
155
156 for (i = 0, dr = t->_ranges; i < t->_nranges; i++, dr++) {
157 if (dr->dr_busbase <= curaddr
158 && round_page(curaddr) <= dr->dr_busbase + dr->dr_len)
159 return curaddr - dr->dr_busbase + dr->dr_sysbase;
160 }
161 panic("%s: curaddr %#lx not in range", __func__, curaddr);
162 }
163
164 /*
165 * Common function to load the specified physical address into the
166 * DMA map, coalescing segments and boundary checking as necessary.
167 */
168 static int
169 _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
170 bus_addr_t paddr, bus_size_t size, bool coherent)
171 {
172 bus_dma_segment_t * const segs = map->dm_segs;
173 int nseg = map->dm_nsegs;
174 bus_addr_t lastaddr;
175 bus_addr_t bmask = ~(map->_dm_boundary - 1);
176 bus_addr_t curaddr;
177 bus_size_t sgsize;
178 uint32_t _ds_flags = coherent ? _BUS_DMAMAP_COHERENT : 0;
179
180 if (nseg > 0)
181 lastaddr = segs[nseg-1].ds_addr + segs[nseg-1].ds_len;
182 else
183 lastaddr = 0xdead;
184
185 again:
186 sgsize = size;
187
188 /* Make sure we're in an allowed DMA range. */
189 if (t->_ranges != NULL) {
190 /* XXX cache last result? */
191 const struct arm32_dma_range * const dr =
192 _bus_dma_paddr_inrange(t->_ranges, t->_nranges, paddr);
193 if (dr == NULL)
194 return (EINVAL);
195
196 /*
197 * If this region is coherent, mark the segment as coherent.
198 */
199 _ds_flags |= dr->dr_flags & _BUS_DMAMAP_COHERENT;
200
201 /*
202 * In a valid DMA range. Translate the physical
203 * memory address to an address in the DMA window.
204 */
205 curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
206 #if 0
207 printf("%p: %#lx: range %#lx/%#lx/%#lx/%#x: %#x <-- %#lx\n",
208 t, paddr, dr->dr_sysbase, dr->dr_busbase,
209 dr->dr_len, dr->dr_flags, _ds_flags, curaddr);
210 #endif
211 } else
212 curaddr = paddr;
213
214 /*
215 * Make sure we don't cross any boundaries.
216 */
217 if (map->_dm_boundary > 0) {
218 bus_addr_t baddr; /* next boundary address */
219
220 baddr = (curaddr + map->_dm_boundary) & bmask;
221 if (sgsize > (baddr - curaddr))
222 sgsize = (baddr - curaddr);
223 }
224
225 /*
226 * Insert chunk into a segment, coalescing with the
227 * previous segment if possible.
228 */
229 if (nseg > 0 && curaddr == lastaddr &&
230 segs[nseg-1].ds_len + sgsize <= map->dm_maxsegsz &&
231 ((segs[nseg-1]._ds_flags ^ _ds_flags) & _BUS_DMAMAP_COHERENT) == 0 &&
232 (map->_dm_boundary == 0 ||
233 (segs[nseg-1].ds_addr & bmask) == (curaddr & bmask))) {
234 /* coalesce */
235 segs[nseg-1].ds_len += sgsize;
236 } else if (nseg >= map->_dm_segcnt) {
237 return (EFBIG);
238 } else {
239 /* new segment */
240 segs[nseg].ds_addr = curaddr;
241 segs[nseg].ds_len = sgsize;
242 segs[nseg]._ds_flags = _ds_flags;
243 nseg++;
244 }
245
246 lastaddr = curaddr + sgsize;
247
248 paddr += sgsize;
249 size -= sgsize;
250 if (size > 0)
251 goto again;
252
253 map->_dm_flags &= (_ds_flags & _BUS_DMAMAP_COHERENT);
254 map->dm_nsegs = nseg;
255 return (0);
256 }
257
258 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
259 static int _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
260 bus_size_t size, int flags);
261 static void _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map);
262 static int _bus_dma_uiomove(void *buf, struct uio *uio, size_t n,
263 int direction);
264
265 static int
266 _bus_dma_load_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
267 size_t buflen, int buftype, int flags)
268 {
269 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
270 struct vmspace * const vm = vmspace_kernel();
271 int error;
272
273 KASSERT(cookie != NULL);
274 KASSERT(cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE);
275
276 /*
277 * Allocate bounce pages, if necessary.
278 */
279 if ((cookie->id_flags & _BUS_DMA_HAS_BOUNCE) == 0) {
280 error = _bus_dma_alloc_bouncebuf(t, map, buflen, flags);
281 if (error)
282 return (error);
283 }
284
285 /*
286 * Cache a pointer to the caller's buffer and load the DMA map
287 * with the bounce buffer.
288 */
289 cookie->id_origbuf = buf;
290 cookie->id_origbuflen = buflen;
291 error = _bus_dmamap_load_buffer(t, map, cookie->id_bouncebuf,
292 buflen, vm, flags);
293 if (error)
294 return (error);
295
296 STAT_INCR(bounced_loads);
297 map->dm_mapsize = buflen;
298 map->_dm_vmspace = vm;
299 map->_dm_buftype = buftype;
300
301 /* ...so _bus_dmamap_sync() knows we're bouncing */
302 map->_dm_flags |= _BUS_DMAMAP_IS_BOUNCING;
303 cookie->id_flags |= _BUS_DMA_IS_BOUNCING;
304 return 0;
305 }
306 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
307
308 /*
309 * Common function for DMA map creation. May be called by bus-specific
310 * DMA map creation functions.
311 */
312 int
313 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
314 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
315 {
316 struct arm32_bus_dmamap *map;
317 void *mapstore;
318 size_t mapsize;
319
320 #ifdef DEBUG_DMA
321 printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
322 t, size, nsegments, maxsegsz, boundary, flags);
323 #endif /* DEBUG_DMA */
324
325 /*
326 * Allocate and initialize the DMA map. The end of the map
327 * is a variable-sized array of segments, so we allocate enough
328 * room for them in one shot.
329 *
330 * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
331 * of ALLOCNOW notifies others that we've reserved these resources,
332 * and they are not to be freed.
333 *
334 * The bus_dmamap_t includes one bus_dma_segment_t, hence
335 * the (nsegments - 1).
336 */
337 mapsize = sizeof(struct arm32_bus_dmamap) +
338 (sizeof(bus_dma_segment_t) * (nsegments - 1));
339 const int mallocflags = M_ZERO|(flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK;
340 if ((mapstore = malloc(mapsize, M_DMAMAP, mallocflags)) == NULL)
341 return (ENOMEM);
342
343 map = (struct arm32_bus_dmamap *)mapstore;
344 map->_dm_size = size;
345 map->_dm_segcnt = nsegments;
346 map->_dm_maxmaxsegsz = maxsegsz;
347 map->_dm_boundary = boundary;
348 map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
349 map->_dm_origbuf = NULL;
350 map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
351 map->_dm_vmspace = vmspace_kernel();
352 map->_dm_cookie = NULL;
353 map->dm_maxsegsz = maxsegsz;
354 map->dm_mapsize = 0; /* no valid mappings */
355 map->dm_nsegs = 0;
356
357 *dmamp = map;
358
359 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
360 struct arm32_bus_dma_cookie *cookie;
361 int cookieflags;
362 void *cookiestore;
363 size_t cookiesize;
364 int error;
365
366 cookieflags = 0;
367
368 if (t->_may_bounce != NULL) {
369 error = (*t->_may_bounce)(t, map, flags, &cookieflags);
370 if (error != 0)
371 goto out;
372 }
373
374 if (t->_ranges != NULL)
375 cookieflags |= _BUS_DMA_MIGHT_NEED_BOUNCE;
376
377 if ((cookieflags & _BUS_DMA_MIGHT_NEED_BOUNCE) == 0) {
378 STAT_INCR(creates);
379 return 0;
380 }
381
382 cookiesize = sizeof(struct arm32_bus_dma_cookie) +
383 (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
384
385 /*
386 * Allocate our cookie.
387 */
388 if ((cookiestore = malloc(cookiesize, M_DMAMAP, mallocflags)) == NULL) {
389 error = ENOMEM;
390 goto out;
391 }
392 cookie = (struct arm32_bus_dma_cookie *)cookiestore;
393 cookie->id_flags = cookieflags;
394 map->_dm_cookie = cookie;
395 STAT_INCR(bounced_creates);
396
397 error = _bus_dma_alloc_bouncebuf(t, map, size, flags);
398 out:
399 if (error)
400 _bus_dmamap_destroy(t, map);
401 #else
402 STAT_INCR(creates);
403 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
404
405 #ifdef DEBUG_DMA
406 printf("dmamap_create:map=%p\n", map);
407 #endif /* DEBUG_DMA */
408 return (0);
409 }
410
411 /*
412 * Common function for DMA map destruction. May be called by bus-specific
413 * DMA map destruction functions.
414 */
415 void
416 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
417 {
418
419 #ifdef DEBUG_DMA
420 printf("dmamap_destroy: t=%p map=%p\n", t, map);
421 #endif /* DEBUG_DMA */
422 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
423 struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
424
425 /*
426 * Free any bounce pages this map might hold.
427 */
428 if (cookie != NULL) {
429 if (cookie->id_flags & _BUS_DMA_IS_BOUNCING)
430 STAT_INCR(bounced_unloads);
431 map->dm_nsegs = 0;
432 if (cookie->id_flags & _BUS_DMA_HAS_BOUNCE)
433 _bus_dma_free_bouncebuf(t, map);
434 STAT_INCR(bounced_destroys);
435 free(cookie, M_DMAMAP);
436 } else
437 #endif
438 STAT_INCR(destroys);
439
440 if (map->dm_nsegs > 0)
441 STAT_INCR(unloads);
442
443 free(map, M_DMAMAP);
444 }
445
446 /*
447 * Common function for loading a DMA map with a linear buffer. May
448 * be called by bus-specific DMA map load functions.
449 */
450 int
451 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
452 bus_size_t buflen, struct proc *p, int flags)
453 {
454 struct vmspace *vm;
455 int error;
456
457 #ifdef DEBUG_DMA
458 printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
459 t, map, buf, buflen, p, flags);
460 #endif /* DEBUG_DMA */
461
462 if (map->dm_nsegs > 0) {
463 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
464 struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
465 if (cookie != NULL) {
466 if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
467 STAT_INCR(bounced_unloads);
468 cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
469 map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
470 }
471 } else
472 #endif
473 STAT_INCR(unloads);
474 }
475
476 /*
477 * Make sure that on error condition we return "no valid mappings".
478 */
479 map->dm_mapsize = 0;
480 map->dm_nsegs = 0;
481 map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
482 KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
483 "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
484 map->dm_maxsegsz, map->_dm_maxmaxsegsz);
485
486 if (buflen > map->_dm_size)
487 return (EINVAL);
488
489 if (p != NULL) {
490 vm = p->p_vmspace;
491 } else {
492 vm = vmspace_kernel();
493 }
494
495 /* _bus_dmamap_load_buffer() clears this if we're not... */
496 map->_dm_flags |= _BUS_DMAMAP_COHERENT;
497
498 error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
499 if (error == 0) {
500 map->dm_mapsize = buflen;
501 map->_dm_vmspace = vm;
502 map->_dm_origbuf = buf;
503 map->_dm_buftype = _BUS_DMA_BUFTYPE_LINEAR;
504 return 0;
505 }
506 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
507 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
508 if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
509 error = _bus_dma_load_bouncebuf(t, map, buf, buflen,
510 _BUS_DMA_BUFTYPE_LINEAR, flags);
511 }
512 #endif
513 return (error);
514 }
515
516 /*
517 * Like _bus_dmamap_load(), but for mbufs.
518 */
519 int
520 _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
521 int flags)
522 {
523 int error;
524 struct mbuf *m;
525
526 #ifdef DEBUG_DMA
527 printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
528 t, map, m0, flags);
529 #endif /* DEBUG_DMA */
530
531 if (map->dm_nsegs > 0) {
532 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
533 struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
534 if (cookie != NULL) {
535 if (cookie->id_flags & _BUS_DMA_IS_BOUNCING) {
536 STAT_INCR(bounced_unloads);
537 cookie->id_flags &= ~_BUS_DMA_IS_BOUNCING;
538 map->_dm_flags &= ~_BUS_DMAMAP_IS_BOUNCING;
539 }
540 } else
541 #endif
542 STAT_INCR(unloads);
543 }
544
545 /*
546 * Make sure that on error condition we return "no valid mappings."
547 */
548 map->dm_mapsize = 0;
549 map->dm_nsegs = 0;
550 map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
551 KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
552 "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
553 map->dm_maxsegsz, map->_dm_maxmaxsegsz);
554
555 #ifdef DIAGNOSTIC
556 if ((m0->m_flags & M_PKTHDR) == 0)
557 panic("_bus_dmamap_load_mbuf: no packet header");
558 #endif /* DIAGNOSTIC */
559
560 if (m0->m_pkthdr.len > map->_dm_size)
561 return (EINVAL);
562
563 /* _bus_dmamap_load_paddr() clears this if we're not... */
564 map->_dm_flags |= _BUS_DMAMAP_COHERENT;
565
566 error = 0;
567 for (m = m0; m != NULL && error == 0; m = m->m_next) {
568 int offset;
569 int remainbytes;
570 const struct vm_page * const *pgs;
571 paddr_t paddr;
572 int size;
573
574 if (m->m_len == 0)
575 continue;
576 /*
577 * Don't allow reads in read-only mbufs.
578 */
579 if (M_ROMAP(m) && (flags & BUS_DMA_READ)) {
580 error = EFAULT;
581 break;
582 }
583 switch (m->m_flags & (M_EXT|M_CLUSTER|M_EXT_PAGES)) {
584 case M_EXT|M_CLUSTER:
585 /* XXX KDASSERT */
586 KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
587 paddr = m->m_ext.ext_paddr +
588 (m->m_data - m->m_ext.ext_buf);
589 size = m->m_len;
590 error = _bus_dmamap_load_paddr(t, map, paddr, size,
591 false);
592 break;
593
594 case M_EXT|M_EXT_PAGES:
595 KASSERT(m->m_ext.ext_buf <= m->m_data);
596 KASSERT(m->m_data <=
597 m->m_ext.ext_buf + m->m_ext.ext_size);
598
599 offset = (vaddr_t)m->m_data -
600 trunc_page((vaddr_t)m->m_ext.ext_buf);
601 remainbytes = m->m_len;
602
603 /* skip uninteresting pages */
604 pgs = (const struct vm_page * const *)
605 m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
606
607 offset &= PAGE_MASK; /* offset in the first page */
608
609 /* load each page */
610 while (remainbytes > 0) {
611 const struct vm_page *pg;
612
613 size = MIN(remainbytes, PAGE_SIZE - offset);
614
615 pg = *pgs++;
616 KASSERT(pg);
617 paddr = VM_PAGE_TO_PHYS(pg) + offset;
618
619 error = _bus_dmamap_load_paddr(t, map,
620 paddr, size, false);
621 if (error)
622 break;
623 offset = 0;
624 remainbytes -= size;
625 }
626 break;
627
628 case 0:
629 paddr = m->m_paddr + M_BUFOFFSET(m) +
630 (m->m_data - M_BUFADDR(m));
631 size = m->m_len;
632 error = _bus_dmamap_load_paddr(t, map, paddr, size,
633 false);
634 break;
635
636 default:
637 error = _bus_dmamap_load_buffer(t, map, m->m_data,
638 m->m_len, vmspace_kernel(), flags);
639 }
640 }
641 if (error == 0) {
642 map->dm_mapsize = m0->m_pkthdr.len;
643 map->_dm_origbuf = m0;
644 map->_dm_buftype = _BUS_DMA_BUFTYPE_MBUF;
645 map->_dm_vmspace = vmspace_kernel(); /* always kernel */
646 return 0;
647 }
648 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
649 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
650 if (cookie != NULL && (cookie->id_flags & _BUS_DMA_MIGHT_NEED_BOUNCE)) {
651 error = _bus_dma_load_bouncebuf(t, map, m0, m0->m_pkthdr.len,
652 _BUS_DMA_BUFTYPE_MBUF, flags);
653 }
654 #endif
655 return (error);
656 }
657
658 /*
659 * Like _bus_dmamap_load(), but for uios.
660 */
661 int
662 _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
663 int flags)
664 {
665 int i, error;
666 bus_size_t minlen, resid;
667 struct iovec *iov;
668 void *addr;
669
670 /*
671 * Make sure that on error condition we return "no valid mappings."
672 */
673 map->dm_mapsize = 0;
674 map->dm_nsegs = 0;
675 KASSERTMSG(map->dm_maxsegsz <= map->_dm_maxmaxsegsz,
676 "dm_maxsegsz %lu _dm_maxmaxsegsz %lu",
677 map->dm_maxsegsz, map->_dm_maxmaxsegsz);
678
679 resid = uio->uio_resid;
680 iov = uio->uio_iov;
681
682 /* _bus_dmamap_load_buffer() clears this if we're not... */
683 map->_dm_flags |= _BUS_DMAMAP_COHERENT;
684
685 error = 0;
686 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
687 /*
688 * Now at the first iovec to load. Load each iovec
689 * until we have exhausted the residual count.
690 */
691 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
692 addr = (void *)iov[i].iov_base;
693
694 error = _bus_dmamap_load_buffer(t, map, addr, minlen,
695 uio->uio_vmspace, flags);
696
697 resid -= minlen;
698 }
699 if (error == 0) {
700 map->dm_mapsize = uio->uio_resid;
701 map->_dm_origbuf = uio;
702 map->_dm_buftype = _BUS_DMA_BUFTYPE_UIO;
703 map->_dm_vmspace = uio->uio_vmspace;
704 }
705 return (error);
706 }
707
708 /*
709 * Like _bus_dmamap_load(), but for raw memory allocated with
710 * bus_dmamem_alloc().
711 */
712 int
713 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
714 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
715 {
716
717 panic("_bus_dmamap_load_raw: not implemented");
718 }
719
720 /*
721 * Common function for unloading a DMA map. May be called by
722 * bus-specific DMA map unload functions.
723 */
724 void
725 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
726 {
727
728 #ifdef DEBUG_DMA
729 printf("dmamap_unload: t=%p map=%p\n", t, map);
730 #endif /* DEBUG_DMA */
731
732 /*
733 * No resources to free; just mark the mappings as
734 * invalid.
735 */
736 map->dm_mapsize = 0;
737 map->dm_nsegs = 0;
738 map->_dm_origbuf = NULL;
739 map->_dm_buftype = _BUS_DMA_BUFTYPE_INVALID;
740 map->_dm_vmspace = NULL;
741 }
742
743 static void
744 _bus_dmamap_sync_segment(vaddr_t va, paddr_t pa, vsize_t len, int ops, bool readonly_p)
745 {
746 KASSERT((va & PAGE_MASK) == (pa & PAGE_MASK));
747 #if 0
748 printf("sync_segment: va=%#lx pa=%#lx len=%#lx ops=%#x ro=%d\n",
749 va, pa, len, ops, readonly_p);
750 #endif
751
752 switch (ops) {
753 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
754 if (!readonly_p) {
755 STAT_INCR(sync_prereadwrite);
756 cpu_dcache_wbinv_range(va, len);
757 cpu_sdcache_wbinv_range(va, pa, len);
758 break;
759 }
760 /* FALLTHROUGH */
761
762 case BUS_DMASYNC_PREREAD: {
763 const size_t line_size = arm_dcache_align;
764 const size_t line_mask = arm_dcache_align_mask;
765 vsize_t misalignment = va & line_mask;
766 STAT_INCR(sync_preread);
767 if (misalignment) {
768 va -= misalignment;
769 pa -= misalignment;
770 len += misalignment;
771 cpu_dcache_wbinv_range(va, line_size);
772 cpu_sdcache_wbinv_range(va, pa, line_size);
773 if (len <= line_size)
774 break;
775 va += line_size;
776 pa += line_size;
777 len -= line_size;
778 }
779 misalignment = len & line_mask;
780 len -= misalignment;
781 if (len > 0) {
782 cpu_dcache_inv_range(va, len);
783 cpu_sdcache_inv_range(va, pa, len);
784 }
785 if (misalignment) {
786 va += len;
787 pa += len;
788 cpu_dcache_wbinv_range(va, line_size);
789 cpu_sdcache_wbinv_range(va, pa, line_size);
790 }
791 break;
792 }
793
794 case BUS_DMASYNC_PREWRITE:
795 STAT_INCR(sync_prewrite);
796 cpu_dcache_wb_range(va, len);
797 cpu_sdcache_wb_range(va, pa, len);
798 break;
799
800 #ifdef CPU_CORTEX
801 /*
802 * Cortex CPUs can do speculative loads so we need to clean the cache
803 * after a DMA read to deal with any speculatively loaded cache lines.
804 * Since these can't be dirty, we can just invalidate them and don't
805 * have to worry about having to write back their contents.
806 */
807 case BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE:
808 STAT_INCR(sync_postreadwrite);
809 cpu_dcache_inv_range(va, len);
810 cpu_sdcache_inv_range(va, pa, len);
811 break;
812 case BUS_DMASYNC_POSTREAD:
813 STAT_INCR(sync_postread);
814 cpu_dcache_inv_range(va, len);
815 cpu_sdcache_inv_range(va, pa, len);
816 break;
817 #endif
818 }
819 }
820
821 static inline void
822 _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
823 bus_size_t len, int ops)
824 {
825 bus_dma_segment_t *ds = map->dm_segs;
826 vaddr_t va = (vaddr_t) map->_dm_origbuf;
827 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
828 if (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING) {
829 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
830 va = (vaddr_t) cookie->id_bouncebuf;
831 }
832 #endif
833
834 while (len > 0) {
835 while (offset >= ds->ds_len) {
836 offset -= ds->ds_len;
837 va += ds->ds_len;
838 ds++;
839 }
840
841 paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + offset);
842 size_t seglen = min(len, ds->ds_len - offset);
843
844 if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
845 _bus_dmamap_sync_segment(va + offset, pa, seglen, ops,
846 false);
847
848 offset += seglen;
849 len -= seglen;
850 }
851 }
852
853 static inline void
854 _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_size_t offset,
855 bus_size_t len, int ops)
856 {
857 bus_dma_segment_t *ds = map->dm_segs;
858 struct mbuf *m = map->_dm_origbuf;
859 bus_size_t voff = offset;
860 bus_size_t ds_off = offset;
861
862 while (len > 0) {
863 /* Find the current dma segment */
864 while (ds_off >= ds->ds_len) {
865 ds_off -= ds->ds_len;
866 ds++;
867 }
868 /* Find the current mbuf. */
869 while (voff >= m->m_len) {
870 voff -= m->m_len;
871 m = m->m_next;
872 }
873
874 /*
875 * Now at the first mbuf to sync; nail each one until
876 * we have exhausted the length.
877 */
878 vsize_t seglen = min(len, min(m->m_len - voff, ds->ds_len - ds_off));
879 vaddr_t va = mtod(m, vaddr_t) + voff;
880 paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
881
882 /*
883 * We can save a lot of work here if we know the mapping
884 * is read-only at the MMU:
885 *
886 * If a mapping is read-only, no dirty cache blocks will
887 * exist for it. If a writable mapping was made read-only,
888 * we know any dirty cache lines for the range will have
889 * been cleaned for us already. Therefore, if the upper
890 * layer can tell us we have a read-only mapping, we can
891 * skip all cache cleaning.
892 *
893 * NOTE: This only works if we know the pmap cleans pages
894 * before making a read-write -> read-only transition. If
895 * this ever becomes non-true (e.g. Physically Indexed
896 * cache), this will have to be revisited.
897 */
898
899 if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
900 _bus_dmamap_sync_segment(va, pa, seglen, ops,
901 M_ROMAP(m));
902 voff += seglen;
903 ds_off += seglen;
904 len -= seglen;
905 }
906 }
907
908 static inline void
909 _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
910 bus_size_t len, int ops)
911 {
912 bus_dma_segment_t *ds = map->dm_segs;
913 struct uio *uio = map->_dm_origbuf;
914 struct iovec *iov = uio->uio_iov;
915 bus_size_t voff = offset;
916 bus_size_t ds_off = offset;
917
918 while (len > 0) {
919 /* Find the current dma segment */
920 while (ds_off >= ds->ds_len) {
921 ds_off -= ds->ds_len;
922 ds++;
923 }
924
925 /* Find the current iovec. */
926 while (voff >= iov->iov_len) {
927 voff -= iov->iov_len;
928 iov++;
929 }
930
931 /*
932 * Now at the first iovec to sync; nail each one until
933 * we have exhausted the length.
934 */
935 vsize_t seglen = min(len, min(iov->iov_len - voff, ds->ds_len - ds_off));
936 vaddr_t va = (vaddr_t) iov->iov_base + voff;
937 paddr_t pa = _bus_dma_busaddr_to_paddr(t, ds->ds_addr + ds_off);
938
939 if ((ds->_ds_flags & _BUS_DMAMAP_COHERENT) == 0)
940 _bus_dmamap_sync_segment(va, pa, seglen, ops, false);
941
942 voff += seglen;
943 ds_off += seglen;
944 len -= seglen;
945 }
946 }
947
948 /*
949 * Common function for DMA map synchronization. May be called
950 * by bus-specific DMA map synchronization functions.
951 *
952 * This version works for the Virtually Indexed Virtually Tagged
953 * cache found on 32-bit ARM processors.
954 *
955 * XXX Should have separate versions for write-through vs.
956 * XXX write-back caches. We currently assume write-back
957 * XXX here, which is not as efficient as it could be for
958 * XXX the write-through case.
959 */
960 void
961 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
962 bus_size_t len, int ops)
963 {
964 #ifdef DEBUG_DMA
965 printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
966 t, map, offset, len, ops);
967 #endif /* DEBUG_DMA */
968
969 /*
970 * Mixing of PRE and POST operations is not allowed.
971 */
972 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
973 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
974 panic("_bus_dmamap_sync: mix PRE and POST");
975
976 #ifdef DIAGNOSTIC
977 if (offset >= map->dm_mapsize)
978 panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
979 offset, map->dm_mapsize);
980 if (len == 0 || (offset + len) > map->dm_mapsize)
981 panic("_bus_dmamap_sync: bad length");
982 #endif
983
984 /*
985 * For a virtually-indexed write-back cache, we need
986 * to do the following things:
987 *
988 * PREREAD -- Invalidate the D-cache. We do this
989 * here in case a write-back is required by the back-end.
990 *
991 * PREWRITE -- Write-back the D-cache. Note that if
992 * we are doing a PREREAD|PREWRITE, we can collapse
993 * the whole thing into a single Wb-Inv.
994 *
995 * POSTREAD -- Re-invalidate the D-cache in case speculative
996 * memory accesses caused cachelines to become valid with now
997 * invalid data.
998 *
999 * POSTWRITE -- Nothing.
1000 */
1001 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1002 const bool bouncing = (map->_dm_flags & _BUS_DMAMAP_IS_BOUNCING);
1003 #else
1004 const bool bouncing = false;
1005 #endif
1006
1007 const int pre_ops = ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1008 #ifdef CPU_CORTEX
1009 const int post_ops = ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1010 #else
1011 const int post_ops = 0;
1012 #endif
1013 if (!bouncing && pre_ops == 0 && post_ops == BUS_DMASYNC_POSTWRITE) {
1014 STAT_INCR(sync_postwrite);
1015 return;
1016 }
1017 KASSERTMSG(bouncing || pre_ops != 0 || (post_ops & BUS_DMASYNC_POSTREAD),
1018 "pre_ops %#x post_ops %#x", pre_ops, post_ops);
1019 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1020 if (bouncing && (ops & BUS_DMASYNC_PREWRITE)) {
1021 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
1022 STAT_INCR(write_bounces);
1023 char * const dataptr = (char *)cookie->id_bouncebuf + offset;
1024 /*
1025 * Copy the caller's buffer to the bounce buffer.
1026 */
1027 switch (map->_dm_buftype) {
1028 case _BUS_DMA_BUFTYPE_LINEAR:
1029 memcpy(dataptr, cookie->id_origlinearbuf + offset, len);
1030 break;
1031 case _BUS_DMA_BUFTYPE_MBUF:
1032 m_copydata(cookie->id_origmbuf, offset, len, dataptr);
1033 break;
1034 case _BUS_DMA_BUFTYPE_UIO:
1035 _bus_dma_uiomove(dataptr, cookie->id_origuio, len, UIO_WRITE);
1036 break;
1037 #ifdef DIAGNOSTIC
1038 case _BUS_DMA_BUFTYPE_RAW:
1039 panic("_bus_dmamap_sync(pre): _BUS_DMA_BUFTYPE_RAW");
1040 break;
1041
1042 case _BUS_DMA_BUFTYPE_INVALID:
1043 panic("_bus_dmamap_sync(pre): _BUS_DMA_BUFTYPE_INVALID");
1044 break;
1045
1046 default:
1047 panic("_bus_dmamap_sync(pre): map %p: unknown buffer type %d\n",
1048 map, map->_dm_buftype);
1049 break;
1050 #endif /* DIAGNOSTIC */
1051 }
1052 }
1053 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1054
1055 /* Skip cache frobbing if mapping was COHERENT. */
1056 if (!bouncing && (map->_dm_flags & _BUS_DMAMAP_COHERENT)) {
1057 /* Drain the write buffer. */
1058 if (pre_ops & BUS_DMASYNC_PREWRITE)
1059 cpu_drain_writebuf();
1060 return;
1061 }
1062
1063 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1064 if (bouncing && ((map->_dm_flags & _BUS_DMAMAP_COHERENT) || pre_ops == 0)) {
1065 goto bounce_it;
1066 }
1067 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1068
1069 /*
1070 * If the mapping belongs to a non-kernel vmspace, and the
1071 * vmspace has not been active since the last time a full
1072 * cache flush was performed, we don't need to do anything.
1073 */
1074 if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
1075 vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
1076 return;
1077
1078 int buftype = map->_dm_buftype;
1079 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1080 if (bouncing) {
1081 buftype = _BUS_DMA_BUFTYPE_LINEAR;
1082 }
1083 #endif
1084
1085 switch (buftype) {
1086 case _BUS_DMA_BUFTYPE_LINEAR:
1087 _bus_dmamap_sync_linear(t, map, offset, len, ops);
1088 break;
1089
1090 case _BUS_DMA_BUFTYPE_MBUF:
1091 _bus_dmamap_sync_mbuf(t, map, offset, len, ops);
1092 break;
1093
1094 case _BUS_DMA_BUFTYPE_UIO:
1095 _bus_dmamap_sync_uio(t, map, offset, len, ops);
1096 break;
1097
1098 case _BUS_DMA_BUFTYPE_RAW:
1099 panic("_bus_dmamap_sync: _BUS_DMA_BUFTYPE_RAW");
1100 break;
1101
1102 case _BUS_DMA_BUFTYPE_INVALID:
1103 panic("_bus_dmamap_sync: _BUS_DMA_BUFTYPE_INVALID");
1104 break;
1105
1106 default:
1107 panic("_bus_dmamap_sync: map %p: unknown buffer type %d\n",
1108 map, map->_dm_buftype);
1109 }
1110
1111 /* Drain the write buffer. */
1112 cpu_drain_writebuf();
1113
1114 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1115 bounce_it:
1116 if (!bouncing || (ops & BUS_DMASYNC_POSTREAD) == 0)
1117 return;
1118
1119 struct arm32_bus_dma_cookie * const cookie = map->_dm_cookie;
1120 char * const dataptr = (char *)cookie->id_bouncebuf + offset;
1121 STAT_INCR(read_bounces);
1122 /*
1123 * Copy the bounce buffer to the caller's buffer.
1124 */
1125 switch (map->_dm_buftype) {
1126 case _BUS_DMA_BUFTYPE_LINEAR:
1127 memcpy(cookie->id_origlinearbuf + offset, dataptr, len);
1128 break;
1129
1130 case _BUS_DMA_BUFTYPE_MBUF:
1131 m_copyback(cookie->id_origmbuf, offset, len, dataptr);
1132 break;
1133
1134 case _BUS_DMA_BUFTYPE_UIO:
1135 _bus_dma_uiomove(dataptr, cookie->id_origuio, len, UIO_READ);
1136 break;
1137 #ifdef DIAGNOSTIC
1138 case _BUS_DMA_BUFTYPE_RAW:
1139 panic("_bus_dmamap_sync(post): _BUS_DMA_BUFTYPE_RAW");
1140 break;
1141
1142 case _BUS_DMA_BUFTYPE_INVALID:
1143 panic("_bus_dmamap_sync(post): _BUS_DMA_BUFTYPE_INVALID");
1144 break;
1145
1146 default:
1147 panic("_bus_dmamap_sync(post): map %p: unknown buffer type %d\n",
1148 map, map->_dm_buftype);
1149 break;
1150 #endif
1151 }
1152 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1153 }
1154
1155 /*
1156 * Common function for DMA-safe memory allocation. May be called
1157 * by bus-specific DMA memory allocation functions.
1158 */
1159
1160 extern paddr_t physical_start;
1161 extern paddr_t physical_end;
1162
1163 int
1164 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1165 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1166 int flags)
1167 {
1168 struct arm32_dma_range *dr;
1169 int error, i;
1170
1171 #ifdef DEBUG_DMA
1172 printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
1173 "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
1174 boundary, segs, nsegs, rsegs, flags);
1175 #endif
1176
1177 if ((dr = t->_ranges) != NULL) {
1178 error = ENOMEM;
1179 for (i = 0; i < t->_nranges; i++, dr++) {
1180 if (dr->dr_len == 0
1181 || (dr->dr_flags & _BUS_DMAMAP_NOALLOC))
1182 continue;
1183 error = _bus_dmamem_alloc_range(t, size, alignment,
1184 boundary, segs, nsegs, rsegs, flags,
1185 trunc_page(dr->dr_sysbase),
1186 trunc_page(dr->dr_sysbase + dr->dr_len));
1187 if (error == 0)
1188 break;
1189 }
1190 } else {
1191 error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
1192 segs, nsegs, rsegs, flags, trunc_page(physical_start),
1193 trunc_page(physical_end));
1194 }
1195
1196 #ifdef DEBUG_DMA
1197 printf("dmamem_alloc: =%d\n", error);
1198 #endif
1199
1200 return(error);
1201 }
1202
1203 /*
1204 * Common function for freeing DMA-safe memory. May be called by
1205 * bus-specific DMA memory free functions.
1206 */
1207 void
1208 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
1209 {
1210 struct vm_page *m;
1211 bus_addr_t addr;
1212 struct pglist mlist;
1213 int curseg;
1214
1215 #ifdef DEBUG_DMA
1216 printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
1217 #endif /* DEBUG_DMA */
1218
1219 /*
1220 * Build a list of pages to free back to the VM system.
1221 */
1222 TAILQ_INIT(&mlist);
1223 for (curseg = 0; curseg < nsegs; curseg++) {
1224 for (addr = segs[curseg].ds_addr;
1225 addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
1226 addr += PAGE_SIZE) {
1227 m = PHYS_TO_VM_PAGE(addr);
1228 TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
1229 }
1230 }
1231 uvm_pglistfree(&mlist);
1232 }
1233
1234 /*
1235 * Common function for mapping DMA-safe memory. May be called by
1236 * bus-specific DMA memory map functions.
1237 */
1238 int
1239 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1240 size_t size, void **kvap, int flags)
1241 {
1242 vaddr_t va;
1243 paddr_t pa;
1244 int curseg;
1245 pt_entry_t *ptep;
1246 const uvm_flag_t kmflags = UVM_KMF_VAONLY
1247 | ((flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0);
1248 vsize_t align = 0;
1249
1250 #ifdef DEBUG_DMA
1251 printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
1252 segs, nsegs, (unsigned long)size, flags);
1253 #endif /* DEBUG_DMA */
1254
1255 #ifdef PMAP_MAP_POOLPAGE
1256 /*
1257 * If all of memory is mapped, and we are mapping a single physically
1258 * contiguous area then this area is already mapped. Let's see if we
1259 * avoid having a separate mapping for it.
1260 */
1261 if (nsegs == 1) {
1262 /*
1263 * If this is a non-COHERENT mapping, then the existing kernel
1264 * mapping is already compatible with it.
1265 */
1266 bool direct_mapable = (flags & BUS_DMA_COHERENT) == 0;
1267 pa = segs[0].ds_addr;
1268
1269 /*
1270 * This is a COHERENT mapping which, unless this address is in
1271 * a COHERENT dma range, will not be compatible.
1272 */
1273 if (t->_ranges != NULL) {
1274 const struct arm32_dma_range * const dr =
1275 _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
1276 if (dr != NULL
1277 && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
1278 direct_mapable = true;
1279 }
1280 }
1281
1282 if (direct_mapable) {
1283 *kvap = (void *)PMAP_MAP_POOLPAGE(pa);
1284 #ifdef DEBUG_DMA
1285 printf("dmamem_map: =%p\n", *kvap);
1286 #endif /* DEBUG_DMA */
1287 return 0;
1288 }
1289 }
1290 #endif
1291
1292 size = round_page(size);
1293 if (__predict_true(size > L2_L_SIZE)) {
1294 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
1295 if (size >= L1_SS_SIZE)
1296 align = L1_SS_SIZE;
1297 else
1298 #endif
1299 if (size >= L1_S_SIZE)
1300 align = L1_S_SIZE;
1301 else
1302 align = L2_S_SIZE;
1303 }
1304
1305 va = uvm_km_alloc(kernel_map, size, align, kmflags);
1306 if (__predict_false(va == 0 && align > 0)) {
1307 align = 0;
1308 va = uvm_km_alloc(kernel_map, size, 0, kmflags);
1309 }
1310
1311 if (va == 0)
1312 return (ENOMEM);
1313
1314 *kvap = (void *)va;
1315
1316 for (curseg = 0; curseg < nsegs; curseg++) {
1317 for (pa = segs[curseg].ds_addr;
1318 pa < (segs[curseg].ds_addr + segs[curseg].ds_len);
1319 pa += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
1320 bool uncached = (flags & BUS_DMA_COHERENT);
1321 #ifdef DEBUG_DMA
1322 printf("wiring p%lx to v%lx", pa, va);
1323 #endif /* DEBUG_DMA */
1324 if (size == 0)
1325 panic("_bus_dmamem_map: size botch");
1326
1327 const struct arm32_dma_range * const dr =
1328 _bus_dma_paddr_inrange(t->_ranges, t->_nranges, pa);
1329 /*
1330 * If this dma region is coherent then there is
1331 * no need for an uncached mapping.
1332 */
1333 if (dr != NULL
1334 && (dr->dr_flags & _BUS_DMAMAP_COHERENT)) {
1335 uncached = false;
1336 }
1337
1338 pmap_kenter_pa(va, pa,
1339 VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
1340
1341 /*
1342 * If the memory must remain coherent with the
1343 * cache then we must make the memory uncacheable
1344 * in order to maintain virtual cache coherency.
1345 * We must also guarantee the cache does not already
1346 * contain the virtal addresses we are making
1347 * uncacheable.
1348 */
1349 if (uncached) {
1350 cpu_dcache_wbinv_range(va, PAGE_SIZE);
1351 cpu_sdcache_wbinv_range(va, pa, PAGE_SIZE);
1352 cpu_drain_writebuf();
1353 ptep = vtopte(va);
1354 *ptep &= ~L2_S_CACHE_MASK;
1355 PTE_SYNC(ptep);
1356 tlb_flush();
1357 }
1358 #ifdef DEBUG_DMA
1359 ptep = vtopte(va);
1360 printf(" pte=v%p *pte=%x\n", ptep, *ptep);
1361 #endif /* DEBUG_DMA */
1362 }
1363 }
1364 pmap_update(pmap_kernel());
1365 #ifdef DEBUG_DMA
1366 printf("dmamem_map: =%p\n", *kvap);
1367 #endif /* DEBUG_DMA */
1368 return (0);
1369 }
1370
1371 /*
1372 * Common function for unmapping DMA-safe memory. May be called by
1373 * bus-specific DMA memory unmapping functions.
1374 */
1375 void
1376 _bus_dmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
1377 {
1378
1379 #ifdef DEBUG_DMA
1380 printf("dmamem_unmap: t=%p kva=%p size=%zx\n", t, kva, size);
1381 #endif /* DEBUG_DMA */
1382 #ifdef DIAGNOSTIC
1383 if ((u_long)kva & PGOFSET)
1384 panic("_bus_dmamem_unmap");
1385 #endif /* DIAGNOSTIC */
1386
1387 size = round_page(size);
1388 pmap_kremove((vaddr_t)kva, size);
1389 pmap_update(pmap_kernel());
1390 uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
1391 }
1392
1393 /*
1394 * Common functin for mmap(2)'ing DMA-safe memory. May be called by
1395 * bus-specific DMA mmap(2)'ing functions.
1396 */
1397 paddr_t
1398 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1399 off_t off, int prot, int flags)
1400 {
1401 paddr_t map_flags;
1402 int i;
1403
1404 for (i = 0; i < nsegs; i++) {
1405 #ifdef DIAGNOSTIC
1406 if (off & PGOFSET)
1407 panic("_bus_dmamem_mmap: offset unaligned");
1408 if (segs[i].ds_addr & PGOFSET)
1409 panic("_bus_dmamem_mmap: segment unaligned");
1410 if (segs[i].ds_len & PGOFSET)
1411 panic("_bus_dmamem_mmap: segment size not multiple"
1412 " of page size");
1413 #endif /* DIAGNOSTIC */
1414 if (off >= segs[i].ds_len) {
1415 off -= segs[i].ds_len;
1416 continue;
1417 }
1418
1419 map_flags = 0;
1420 if (flags & BUS_DMA_PREFETCHABLE)
1421 map_flags |= ARM32_MMAP_WRITECOMBINE;
1422
1423 return (arm_btop((u_long)segs[i].ds_addr + off) | map_flags);
1424
1425 }
1426
1427 /* Page not found. */
1428 return (-1);
1429 }
1430
1431 /**********************************************************************
1432 * DMA utility functions
1433 **********************************************************************/
1434
1435 /*
1436 * Utility function to load a linear buffer. lastaddrp holds state
1437 * between invocations (for multiple-buffer loads). segp contains
1438 * the starting segment on entrace, and the ending segment on exit.
1439 * first indicates if this is the first invocation of this function.
1440 */
1441 int
1442 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
1443 bus_size_t buflen, struct vmspace *vm, int flags)
1444 {
1445 bus_size_t sgsize;
1446 bus_addr_t curaddr;
1447 vaddr_t vaddr = (vaddr_t)buf;
1448 int error;
1449 pmap_t pmap;
1450
1451 #ifdef DEBUG_DMA
1452 printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
1453 buf, buflen, flags);
1454 #endif /* DEBUG_DMA */
1455
1456 pmap = vm_map_pmap(&vm->vm_map);
1457
1458 while (buflen > 0) {
1459 /*
1460 * Get the physical address for this segment.
1461 *
1462 * XXX Doesn't support checking for coherent mappings
1463 * XXX in user address space.
1464 */
1465 bool coherent;
1466 if (__predict_true(pmap == pmap_kernel())) {
1467 pd_entry_t *pde;
1468 pt_entry_t *ptep;
1469 (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
1470 if (__predict_false(pmap_pde_section(pde))) {
1471 paddr_t s_frame = L1_S_FRAME;
1472 paddr_t s_offset = L1_S_OFFSET;
1473 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
1474 if (__predict_false(pmap_pde_supersection(pde))) {
1475 s_frame = L1_SS_FRAME;
1476 s_offset = L1_SS_OFFSET;
1477 }
1478 #endif
1479 curaddr = (*pde & s_frame) | (vaddr & s_offset);
1480 coherent = (*pde & L1_S_CACHE_MASK) == 0;
1481 } else {
1482 pt_entry_t pte = *ptep;
1483 KDASSERTMSG((pte & L2_TYPE_MASK) != L2_TYPE_INV,
1484 "va=%#"PRIxVADDR" pde=%#x ptep=%p pte=%#x",
1485 vaddr, *pde, ptep, pte);
1486 if (__predict_false((pte & L2_TYPE_MASK)
1487 == L2_TYPE_L)) {
1488 curaddr = (pte & L2_L_FRAME) |
1489 (vaddr & L2_L_OFFSET);
1490 coherent = (pte & L2_L_CACHE_MASK) == 0;
1491 } else {
1492 curaddr = (pte & L2_S_FRAME) |
1493 (vaddr & L2_S_OFFSET);
1494 coherent = (pte & L2_S_CACHE_MASK) == 0;
1495 }
1496 }
1497 } else {
1498 (void) pmap_extract(pmap, vaddr, &curaddr);
1499 coherent = false;
1500 }
1501
1502 /*
1503 * Compute the segment size, and adjust counts.
1504 */
1505 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
1506 if (buflen < sgsize)
1507 sgsize = buflen;
1508
1509 error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize,
1510 coherent);
1511 if (error)
1512 return (error);
1513
1514 vaddr += sgsize;
1515 buflen -= sgsize;
1516 }
1517
1518 return (0);
1519 }
1520
1521 /*
1522 * Allocate physical memory from the given physical address range.
1523 * Called by DMA-safe memory allocation methods.
1524 */
1525 int
1526 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1527 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1528 int flags, paddr_t low, paddr_t high)
1529 {
1530 paddr_t curaddr, lastaddr;
1531 struct vm_page *m;
1532 struct pglist mlist;
1533 int curseg, error;
1534
1535 KASSERTMSG(boundary == 0 || (boundary & (boundary-1)) == 0,
1536 "invalid boundary %#lx", boundary);
1537
1538 #ifdef DEBUG_DMA
1539 printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
1540 t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
1541 #endif /* DEBUG_DMA */
1542
1543 /* Always round the size. */
1544 size = round_page(size);
1545
1546 /*
1547 * We accept boundaries < size, splitting in multiple segments
1548 * if needed. uvm_pglistalloc does not, so compute an appropriate
1549 * boundary: next power of 2 >= size
1550 */
1551 bus_size_t uboundary = boundary;
1552 if (uboundary <= PAGE_SIZE) {
1553 uboundary = 0;
1554 } else {
1555 while (uboundary < size) {
1556 uboundary <<= 1;
1557 }
1558 }
1559
1560 /*
1561 * Allocate pages from the VM system.
1562 */
1563 error = uvm_pglistalloc(size, low, high, alignment, boundary,
1564 &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
1565 if (error)
1566 return (error);
1567
1568 /*
1569 * Compute the location, size, and number of segments actually
1570 * returned by the VM code.
1571 */
1572 m = TAILQ_FIRST(&mlist);
1573 curseg = 0;
1574 lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
1575 segs[curseg].ds_len = PAGE_SIZE;
1576 #ifdef DEBUG_DMA
1577 printf("alloc: page %lx\n", lastaddr);
1578 #endif /* DEBUG_DMA */
1579 m = TAILQ_NEXT(m, pageq.queue);
1580
1581 for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
1582 curaddr = VM_PAGE_TO_PHYS(m);
1583 KASSERTMSG(low <= curaddr && curaddr < high,
1584 "uvm_pglistalloc returned non-sensicaladdress %#lx "
1585 "(low=%#lx, high=%#lx\n", curaddr, low, high);
1586 #ifdef DEBUG_DMA
1587 printf("alloc: page %lx\n", curaddr);
1588 #endif /* DEBUG_DMA */
1589 if (curaddr == lastaddr + PAGE_SIZE
1590 && (lastaddr & boundary) == (curaddr & boundary))
1591 segs[curseg].ds_len += PAGE_SIZE;
1592 else {
1593 curseg++;
1594 if (curseg >= nsegs) {
1595 uvm_pglistfree(&mlist);
1596 return EFBIG;
1597 }
1598 segs[curseg].ds_addr = curaddr;
1599 segs[curseg].ds_len = PAGE_SIZE;
1600 }
1601 lastaddr = curaddr;
1602 }
1603
1604 *rsegs = curseg + 1;
1605
1606 return (0);
1607 }
1608
1609 /*
1610 * Check if a memory region intersects with a DMA range, and return the
1611 * page-rounded intersection if it does.
1612 */
1613 int
1614 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
1615 paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
1616 {
1617 struct arm32_dma_range *dr;
1618 int i;
1619
1620 if (ranges == NULL)
1621 return (0);
1622
1623 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
1624 if (dr->dr_sysbase <= pa &&
1625 pa < (dr->dr_sysbase + dr->dr_len)) {
1626 /*
1627 * Beginning of region intersects with this range.
1628 */
1629 *pap = trunc_page(pa);
1630 *sizep = round_page(min(pa + size,
1631 dr->dr_sysbase + dr->dr_len) - pa);
1632 return (1);
1633 }
1634 if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
1635 /*
1636 * End of region intersects with this range.
1637 */
1638 *pap = trunc_page(dr->dr_sysbase);
1639 *sizep = round_page(min((pa + size) - dr->dr_sysbase,
1640 dr->dr_len));
1641 return (1);
1642 }
1643 }
1644
1645 /* No intersection found. */
1646 return (0);
1647 }
1648
1649 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1650 static int
1651 _bus_dma_alloc_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map,
1652 bus_size_t size, int flags)
1653 {
1654 struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
1655 int error = 0;
1656
1657 #ifdef DIAGNOSTIC
1658 if (cookie == NULL)
1659 panic("_bus_dma_alloc_bouncebuf: no cookie");
1660 #endif
1661
1662 cookie->id_bouncebuflen = round_page(size);
1663 error = _bus_dmamem_alloc(t, cookie->id_bouncebuflen,
1664 PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
1665 map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
1666 if (error == 0) {
1667 error = _bus_dmamem_map(t, cookie->id_bouncesegs,
1668 cookie->id_nbouncesegs, cookie->id_bouncebuflen,
1669 (void **)&cookie->id_bouncebuf, flags);
1670 if (error) {
1671 _bus_dmamem_free(t, cookie->id_bouncesegs,
1672 cookie->id_nbouncesegs);
1673 cookie->id_bouncebuflen = 0;
1674 cookie->id_nbouncesegs = 0;
1675 } else {
1676 cookie->id_flags |= _BUS_DMA_HAS_BOUNCE;
1677 }
1678 } else {
1679 cookie->id_bouncebuflen = 0;
1680 cookie->id_nbouncesegs = 0;
1681 }
1682
1683 return (error);
1684 }
1685
1686 static void
1687 _bus_dma_free_bouncebuf(bus_dma_tag_t t, bus_dmamap_t map)
1688 {
1689 struct arm32_bus_dma_cookie *cookie = map->_dm_cookie;
1690
1691 #ifdef DIAGNOSTIC
1692 if (cookie == NULL)
1693 panic("_bus_dma_alloc_bouncebuf: no cookie");
1694 #endif
1695
1696 _bus_dmamem_unmap(t, cookie->id_bouncebuf, cookie->id_bouncebuflen);
1697 _bus_dmamem_free(t, cookie->id_bouncesegs,
1698 cookie->id_nbouncesegs);
1699 cookie->id_bouncebuflen = 0;
1700 cookie->id_nbouncesegs = 0;
1701 cookie->id_flags &= ~_BUS_DMA_HAS_BOUNCE;
1702 }
1703
1704 /*
1705 * This function does the same as uiomove, but takes an explicit
1706 * direction, and does not update the uio structure.
1707 */
1708 static int
1709 _bus_dma_uiomove(void *buf, struct uio *uio, size_t n, int direction)
1710 {
1711 struct iovec *iov;
1712 int error;
1713 struct vmspace *vm;
1714 char *cp;
1715 size_t resid, cnt;
1716 int i;
1717
1718 iov = uio->uio_iov;
1719 vm = uio->uio_vmspace;
1720 cp = buf;
1721 resid = n;
1722
1723 for (i = 0; i < uio->uio_iovcnt && resid > 0; i++) {
1724 iov = &uio->uio_iov[i];
1725 if (iov->iov_len == 0)
1726 continue;
1727 cnt = MIN(resid, iov->iov_len);
1728
1729 if (!VMSPACE_IS_KERNEL_P(vm) &&
1730 (curlwp->l_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
1731 != 0) {
1732 preempt();
1733 }
1734 if (direction == UIO_READ) {
1735 error = copyout_vmspace(vm, cp, iov->iov_base, cnt);
1736 } else {
1737 error = copyin_vmspace(vm, iov->iov_base, cp, cnt);
1738 }
1739 if (error)
1740 return (error);
1741 cp += cnt;
1742 resid -= cnt;
1743 }
1744 return (0);
1745 }
1746 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1747
1748 int
1749 _bus_dmatag_subregion(bus_dma_tag_t tag, bus_addr_t min_addr,
1750 bus_addr_t max_addr, bus_dma_tag_t *newtag, int flags)
1751 {
1752
1753 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1754 struct arm32_dma_range *dr;
1755 bool subset = false;
1756 size_t nranges = 0;
1757 size_t i;
1758 for (i = 0, dr = tag->_ranges; i < tag->_nranges; i++, dr++) {
1759 if (dr->dr_sysbase <= min_addr
1760 && max_addr <= dr->dr_sysbase + dr->dr_len - 1) {
1761 subset = true;
1762 }
1763 if (min_addr <= dr->dr_sysbase + dr->dr_len
1764 && max_addr >= dr->dr_sysbase) {
1765 nranges++;
1766 }
1767 }
1768 if (subset) {
1769 *newtag = tag;
1770 /* if the tag must be freed, add a reference */
1771 if (tag->_tag_needs_free)
1772 (tag->_tag_needs_free)++;
1773 return 0;
1774 }
1775 if (nranges == 0) {
1776 nranges = 1;
1777 }
1778
1779 size_t mallocsize = sizeof(*tag) + nranges * sizeof(*dr);
1780 if ((*newtag = malloc(mallocsize, M_DMAMAP,
1781 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
1782 return ENOMEM;
1783
1784 dr = (void *)(*newtag + 1);
1785 **newtag = *tag;
1786 (*newtag)->_tag_needs_free = 1;
1787 (*newtag)->_ranges = dr;
1788 (*newtag)->_nranges = nranges;
1789
1790 if (tag->_ranges == NULL) {
1791 dr->dr_sysbase = min_addr;
1792 dr->dr_busbase = min_addr;
1793 dr->dr_len = max_addr + 1 - min_addr;
1794 } else {
1795 for (i = 0; i < nranges; i++) {
1796 if (min_addr > dr->dr_sysbase + dr->dr_len
1797 || max_addr < dr->dr_sysbase)
1798 continue;
1799 dr[0] = tag->_ranges[i];
1800 if (dr->dr_sysbase < min_addr) {
1801 psize_t diff = min_addr - dr->dr_sysbase;
1802 dr->dr_busbase += diff;
1803 dr->dr_len -= diff;
1804 dr->dr_sysbase += diff;
1805 }
1806 if (max_addr != 0xffffffff
1807 && max_addr + 1 < dr->dr_sysbase + dr->dr_len) {
1808 dr->dr_len = max_addr + 1 - dr->dr_sysbase;
1809 }
1810 dr++;
1811 }
1812 }
1813
1814 return 0;
1815 #else
1816 return EOPNOTSUPP;
1817 #endif /* _ARM32_NEED_BUS_DMA_BOUNCE */
1818 }
1819
1820 void
1821 _bus_dmatag_destroy(bus_dma_tag_t tag)
1822 {
1823 #ifdef _ARM32_NEED_BUS_DMA_BOUNCE
1824 switch (tag->_tag_needs_free) {
1825 case 0:
1826 break; /* not allocated with malloc */
1827 case 1:
1828 free(tag, M_DMAMAP); /* last reference to tag */
1829 break;
1830 default:
1831 (tag->_tag_needs_free)--; /* one less reference */
1832 }
1833 #endif
1834 }
1835