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