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