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