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