bus_dma.c revision 1.48 1 /* $NetBSD: bus_dma.c,v 1.48 2006/03/01 12:38:10 yamt 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #define _ARM32_BUS_DMA_PRIVATE
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: bus_dma.c,v 1.48 2006/03/01 12:38:10 yamt Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/buf.h>
50 #include <sys/reboot.h>
51 #include <sys/conf.h>
52 #include <sys/file.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/vnode.h>
56 #include <sys/device.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <machine/bus.h>
61 #include <machine/cpu.h>
62
63 #include <arm/cpufunc.h>
64
65 int _bus_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
66 bus_size_t, struct vmspace *, int);
67 struct arm32_dma_range *_bus_dma_inrange(struct arm32_dma_range *,
68 int, bus_addr_t);
69
70 /*
71 * Check to see if the specified page is in an allowed DMA range.
72 */
73 inline struct arm32_dma_range *
74 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
75 bus_addr_t curaddr)
76 {
77 struct arm32_dma_range *dr;
78 int i;
79
80 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
81 if (curaddr >= dr->dr_sysbase &&
82 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
83 return (dr);
84 }
85
86 return (NULL);
87 }
88
89 /*
90 * Common function to load the specified physical address into the
91 * DMA map, coalescing segments and boundary checking as necessary.
92 */
93 static int
94 _bus_dmamap_load_paddr(bus_dma_tag_t t, bus_dmamap_t map,
95 bus_addr_t paddr, bus_size_t size)
96 {
97 bus_dma_segment_t * const segs = map->dm_segs;
98 int nseg = map->dm_nsegs;
99 bus_addr_t lastaddr = 0xdead; /* XXX gcc */
100 bus_addr_t bmask = ~(map->_dm_boundary - 1);
101 bus_addr_t curaddr;
102 bus_size_t sgsize;
103
104 if (nseg > 0)
105 lastaddr = segs[nseg-1].ds_addr + segs[nseg-1].ds_len;
106 again:
107 sgsize = size;
108
109 /* Make sure we're in an allowed DMA range. */
110 if (t->_ranges != NULL) {
111 /* XXX cache last result? */
112 const struct arm32_dma_range * const dr =
113 _bus_dma_inrange(t->_ranges, t->_nranges, paddr);
114 if (dr == NULL)
115 return (EINVAL);
116
117 /*
118 * In a valid DMA range. Translate the physical
119 * memory address to an address in the DMA window.
120 */
121 curaddr = (paddr - dr->dr_sysbase) + dr->dr_busbase;
122 } else
123 curaddr = paddr;
124
125 /*
126 * Make sure we don't cross any boundaries.
127 */
128 if (map->_dm_boundary > 0) {
129 bus_addr_t baddr; /* next boundary address */
130
131 baddr = (curaddr + map->_dm_boundary) & bmask;
132 if (sgsize > (baddr - curaddr))
133 sgsize = (baddr - curaddr);
134 }
135
136 /*
137 * Insert chunk into a segment, coalescing with the
138 * previous segment if possible.
139 */
140 if (nseg > 0 && curaddr == lastaddr &&
141 segs[nseg-1].ds_len + sgsize <= map->dm_maxsegsz &&
142 (map->_dm_boundary == 0 ||
143 (segs[nseg-1].ds_addr & bmask) == (curaddr & bmask))) {
144 /* coalesce */
145 segs[nseg-1].ds_len += sgsize;
146 } else if (nseg >= map->_dm_segcnt) {
147 return (EFBIG);
148 } else {
149 /* new segment */
150 segs[nseg].ds_addr = curaddr;
151 segs[nseg].ds_len = sgsize;
152 nseg++;
153 }
154
155 lastaddr = curaddr + sgsize;
156
157 paddr += sgsize;
158 size -= sgsize;
159 if (size > 0)
160 goto again;
161
162 map->dm_nsegs = nseg;
163 return (0);
164 }
165
166 /*
167 * Common function for DMA map creation. May be called by bus-specific
168 * DMA map creation functions.
169 */
170 int
171 _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
172 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
173 {
174 struct arm32_bus_dmamap *map;
175 void *mapstore;
176 size_t mapsize;
177
178 #ifdef DEBUG_DMA
179 printf("dmamap_create: t=%p size=%lx nseg=%x msegsz=%lx boundary=%lx flags=%x\n",
180 t, size, nsegments, maxsegsz, boundary, flags);
181 #endif /* DEBUG_DMA */
182
183 /*
184 * Allocate and initialize the DMA map. The end of the map
185 * is a variable-sized array of segments, so we allocate enough
186 * room for them in one shot.
187 *
188 * Note we don't preserve the WAITOK or NOWAIT flags. Preservation
189 * of ALLOCNOW notifies others that we've reserved these resources,
190 * and they are not to be freed.
191 *
192 * The bus_dmamap_t includes one bus_dma_segment_t, hence
193 * the (nsegments - 1).
194 */
195 mapsize = sizeof(struct arm32_bus_dmamap) +
196 (sizeof(bus_dma_segment_t) * (nsegments - 1));
197 if ((mapstore = malloc(mapsize, M_DMAMAP,
198 (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
199 return (ENOMEM);
200
201 memset(mapstore, 0, mapsize);
202 map = (struct arm32_bus_dmamap *)mapstore;
203 map->_dm_size = size;
204 map->_dm_segcnt = nsegments;
205 map->_dm_maxmaxsegsz = maxsegsz;
206 map->_dm_boundary = boundary;
207 map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
208 map->_dm_origbuf = NULL;
209 map->_dm_buftype = ARM32_BUFTYPE_INVALID;
210 map->_dm_vmspace = vmspace_kernel();
211 map->dm_maxsegsz = maxsegsz;
212 map->dm_mapsize = 0; /* no valid mappings */
213 map->dm_nsegs = 0;
214
215 *dmamp = map;
216 #ifdef DEBUG_DMA
217 printf("dmamap_create:map=%p\n", map);
218 #endif /* DEBUG_DMA */
219 return (0);
220 }
221
222 /*
223 * Common function for DMA map destruction. May be called by bus-specific
224 * DMA map destruction functions.
225 */
226 void
227 _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
228 {
229
230 #ifdef DEBUG_DMA
231 printf("dmamap_destroy: t=%p map=%p\n", t, map);
232 #endif /* DEBUG_DMA */
233
234 /*
235 * Explicit unload.
236 */
237 map->dm_maxsegsz = map->_dm_maxmaxsegsz;
238 map->dm_mapsize = 0;
239 map->dm_nsegs = 0;
240 map->_dm_origbuf = NULL;
241 map->_dm_buftype = ARM32_BUFTYPE_INVALID;
242 map->_dm_vmspace = NULL;
243
244 free(map, M_DMAMAP);
245 }
246
247 /*
248 * Common function for loading a DMA map with a linear buffer. May
249 * be called by bus-specific DMA map load functions.
250 */
251 int
252 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
253 bus_size_t buflen, struct proc *p, int flags)
254 {
255 int error;
256 struct vmspace *vm;
257
258 #ifdef DEBUG_DMA
259 printf("dmamap_load: t=%p map=%p buf=%p len=%lx p=%p f=%d\n",
260 t, map, buf, buflen, p, flags);
261 #endif /* DEBUG_DMA */
262
263 /*
264 * Make sure that on error condition we return "no valid mappings".
265 */
266 map->dm_mapsize = 0;
267 map->dm_nsegs = 0;
268 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
269
270 if (buflen > map->_dm_size)
271 return (EINVAL);
272
273 if (p != NULL) {
274 vm = p->p_vmspace;
275 } else {
276 vm = vmspace_kernel();
277 }
278
279 /* _bus_dmamap_load_buffer() clears this if we're not... */
280 map->_dm_flags |= ARM32_DMAMAP_COHERENT;
281
282 error = _bus_dmamap_load_buffer(t, map, buf, buflen, vm, flags);
283 if (error == 0) {
284 map->dm_mapsize = buflen;
285 map->_dm_origbuf = buf;
286 map->_dm_buftype = ARM32_BUFTYPE_LINEAR;
287 map->_dm_vmspace = vm;
288 }
289 #ifdef DEBUG_DMA
290 printf("dmamap_load: error=%d\n", error);
291 #endif /* DEBUG_DMA */
292 return (error);
293 }
294
295 /*
296 * Like _bus_dmamap_load(), but for mbufs.
297 */
298 int
299 _bus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
300 int flags)
301 {
302 int error;
303 struct mbuf *m;
304
305 #ifdef DEBUG_DMA
306 printf("dmamap_load_mbuf: t=%p map=%p m0=%p f=%d\n",
307 t, map, m0, flags);
308 #endif /* DEBUG_DMA */
309
310 /*
311 * Make sure that on error condition we return "no valid mappings."
312 */
313 map->dm_mapsize = 0;
314 map->dm_nsegs = 0;
315 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
316
317 #ifdef DIAGNOSTIC
318 if ((m0->m_flags & M_PKTHDR) == 0)
319 panic("_bus_dmamap_load_mbuf: no packet header");
320 #endif /* DIAGNOSTIC */
321
322 if (m0->m_pkthdr.len > map->_dm_size)
323 return (EINVAL);
324
325 /*
326 * Mbuf chains should almost never have coherent (i.e.
327 * un-cached) mappings, so clear that flag now.
328 */
329 map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
330
331 error = 0;
332 for (m = m0; m != NULL && error == 0; m = m->m_next) {
333 int offset;
334 int remainbytes;
335 const struct vm_page * const *pgs;
336 paddr_t paddr;
337 int size;
338
339 if (m->m_len == 0)
340 continue;
341 switch (m->m_flags & (M_EXT|M_CLUSTER|M_EXT_PAGES)) {
342 case M_EXT|M_CLUSTER:
343 /* XXX KDASSERT */
344 KASSERT(m->m_ext.ext_paddr != M_PADDR_INVALID);
345 paddr = m->m_ext.ext_paddr +
346 (m->m_data - m->m_ext.ext_buf);
347 size = m->m_len;
348 error = _bus_dmamap_load_paddr(t, map, paddr, size);
349 break;
350
351 case M_EXT|M_EXT_PAGES:
352 KASSERT(m->m_ext.ext_buf <= m->m_data);
353 KASSERT(m->m_data <=
354 m->m_ext.ext_buf + m->m_ext.ext_size);
355
356 offset = (vaddr_t)m->m_data -
357 trunc_page((vaddr_t)m->m_ext.ext_buf);
358 remainbytes = m->m_len;
359
360 /* skip uninteresting pages */
361 pgs = (const struct vm_page * const *)
362 m->m_ext.ext_pgs + (offset >> PAGE_SHIFT);
363
364 offset &= PAGE_MASK; /* offset in the first page */
365
366 /* load each page */
367 while (remainbytes > 0) {
368 const struct vm_page *pg;
369
370 size = MIN(remainbytes, PAGE_SIZE - offset);
371
372 pg = *pgs++;
373 KASSERT(pg);
374 paddr = VM_PAGE_TO_PHYS(pg) + offset;
375
376 error = _bus_dmamap_load_paddr(t, map,
377 paddr, size);
378 if (error)
379 break;
380 offset = 0;
381 remainbytes -= size;
382 }
383 break;
384
385 case 0:
386 paddr = m->m_paddr + M_BUFOFFSET(m) +
387 (m->m_data - M_BUFADDR(m));
388 size = m->m_len;
389 error = _bus_dmamap_load_paddr(t, map, paddr, size);
390 break;
391
392 default:
393 error = _bus_dmamap_load_buffer(t, map, m->m_data,
394 m->m_len, vmspace_kernel(), flags);
395 }
396 }
397 if (error == 0) {
398 map->dm_mapsize = m0->m_pkthdr.len;
399 map->_dm_origbuf = m0;
400 map->_dm_buftype = ARM32_BUFTYPE_MBUF;
401 map->_dm_vmspace = vmspace_kernel(); /* always kernel */
402 }
403 #ifdef DEBUG_DMA
404 printf("dmamap_load_mbuf: error=%d\n", error);
405 #endif /* DEBUG_DMA */
406 return (error);
407 }
408
409 /*
410 * Like _bus_dmamap_load(), but for uios.
411 */
412 int
413 _bus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
414 int flags)
415 {
416 int i, error;
417 bus_size_t minlen, resid;
418 struct iovec *iov;
419 caddr_t addr;
420
421 /*
422 * Make sure that on error condition we return "no valid mappings."
423 */
424 map->dm_mapsize = 0;
425 map->dm_nsegs = 0;
426 KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
427
428 resid = uio->uio_resid;
429 iov = uio->uio_iov;
430
431 /* _bus_dmamap_load_buffer() clears this if we're not... */
432 map->_dm_flags |= ARM32_DMAMAP_COHERENT;
433
434 error = 0;
435 for (i = 0; i < uio->uio_iovcnt && resid != 0 && error == 0; i++) {
436 /*
437 * Now at the first iovec to load. Load each iovec
438 * until we have exhausted the residual count.
439 */
440 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
441 addr = (caddr_t)iov[i].iov_base;
442
443 error = _bus_dmamap_load_buffer(t, map, addr, minlen,
444 uio->uio_vmspace, flags);
445
446 resid -= minlen;
447 }
448 if (error == 0) {
449 map->dm_mapsize = uio->uio_resid;
450 map->_dm_origbuf = uio;
451 map->_dm_buftype = ARM32_BUFTYPE_UIO;
452 map->_dm_vmspace = uio->uio_vmspace;
453 }
454 return (error);
455 }
456
457 /*
458 * Like _bus_dmamap_load(), but for raw memory allocated with
459 * bus_dmamem_alloc().
460 */
461 int
462 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
463 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
464 {
465
466 panic("_bus_dmamap_load_raw: not implemented");
467 }
468
469 /*
470 * Common function for unloading a DMA map. May be called by
471 * bus-specific DMA map unload functions.
472 */
473 void
474 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
475 {
476
477 #ifdef DEBUG_DMA
478 printf("dmamap_unload: t=%p map=%p\n", t, map);
479 #endif /* DEBUG_DMA */
480
481 /*
482 * No resources to free; just mark the mappings as
483 * invalid.
484 */
485 map->dm_mapsize = 0;
486 map->dm_nsegs = 0;
487 map->_dm_origbuf = NULL;
488 map->_dm_buftype = ARM32_BUFTYPE_INVALID;
489 map->_dm_vmspace = NULL;
490 }
491
492 static inline void
493 _bus_dmamap_sync_linear(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
494 bus_size_t len, int ops)
495 {
496 vaddr_t addr = (vaddr_t) map->_dm_origbuf;
497
498 addr += offset;
499
500 switch (ops) {
501 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
502 cpu_dcache_wbinv_range(addr, len);
503 break;
504
505 case BUS_DMASYNC_PREREAD:
506 if (((addr | len) & arm_dcache_align_mask) == 0)
507 cpu_dcache_inv_range(addr, len);
508 else
509 cpu_dcache_wbinv_range(addr, len);
510 break;
511
512 case BUS_DMASYNC_PREWRITE:
513 cpu_dcache_wb_range(addr, len);
514 break;
515 }
516 }
517
518 static inline void
519 _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
520 bus_size_t len, int ops)
521 {
522 struct mbuf *m, *m0 = map->_dm_origbuf;
523 bus_size_t minlen, moff;
524 vaddr_t maddr;
525
526 for (moff = offset, m = m0; m != NULL && len != 0;
527 m = m->m_next) {
528 /* Find the beginning mbuf. */
529 if (moff >= m->m_len) {
530 moff -= m->m_len;
531 continue;
532 }
533
534 /*
535 * Now at the first mbuf to sync; nail each one until
536 * we have exhausted the length.
537 */
538 minlen = m->m_len - moff;
539 if (len < minlen)
540 minlen = len;
541
542 maddr = mtod(m, vaddr_t);
543 maddr += moff;
544
545 /*
546 * We can save a lot of work here if we know the mapping
547 * is read-only at the MMU:
548 *
549 * If a mapping is read-only, no dirty cache blocks will
550 * exist for it. If a writable mapping was made read-only,
551 * we know any dirty cache lines for the range will have
552 * been cleaned for us already. Therefore, if the upper
553 * layer can tell us we have a read-only mapping, we can
554 * skip all cache cleaning.
555 *
556 * NOTE: This only works if we know the pmap cleans pages
557 * before making a read-write -> read-only transition. If
558 * this ever becomes non-true (e.g. Physically Indexed
559 * cache), this will have to be revisited.
560 */
561 switch (ops) {
562 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
563 if (! M_ROMAP(m)) {
564 cpu_dcache_wbinv_range(maddr, minlen);
565 break;
566 }
567 /* else FALLTHROUGH */
568
569 case BUS_DMASYNC_PREREAD:
570 if (((maddr | minlen) & arm_dcache_align_mask) == 0)
571 cpu_dcache_inv_range(maddr, minlen);
572 else
573 cpu_dcache_wbinv_range(maddr, minlen);
574 break;
575
576 case BUS_DMASYNC_PREWRITE:
577 if (! M_ROMAP(m))
578 cpu_dcache_wb_range(maddr, minlen);
579 break;
580 }
581 moff = 0;
582 len -= minlen;
583 }
584 }
585
586 static inline void
587 _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
588 bus_size_t len, int ops)
589 {
590 struct uio *uio = map->_dm_origbuf;
591 struct iovec *iov;
592 bus_size_t minlen, ioff;
593 vaddr_t addr;
594
595 for (iov = uio->uio_iov, ioff = offset; len != 0; iov++) {
596 /* Find the beginning iovec. */
597 if (ioff >= iov->iov_len) {
598 ioff -= iov->iov_len;
599 continue;
600 }
601
602 /*
603 * Now at the first iovec to sync; nail each one until
604 * we have exhausted the length.
605 */
606 minlen = iov->iov_len - ioff;
607 if (len < minlen)
608 minlen = len;
609
610 addr = (vaddr_t) iov->iov_base;
611 addr += ioff;
612
613 switch (ops) {
614 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
615 cpu_dcache_wbinv_range(addr, minlen);
616 break;
617
618 case BUS_DMASYNC_PREREAD:
619 if (((addr | minlen) & arm_dcache_align_mask) == 0)
620 cpu_dcache_inv_range(addr, minlen);
621 else
622 cpu_dcache_wbinv_range(addr, minlen);
623 break;
624
625 case BUS_DMASYNC_PREWRITE:
626 cpu_dcache_wb_range(addr, minlen);
627 break;
628 }
629 ioff = 0;
630 len -= minlen;
631 }
632 }
633
634 /*
635 * Common function for DMA map synchronization. May be called
636 * by bus-specific DMA map synchronization functions.
637 *
638 * This version works for the Virtually Indexed Virtually Tagged
639 * cache found on 32-bit ARM processors.
640 *
641 * XXX Should have separate versions for write-through vs.
642 * XXX write-back caches. We currently assume write-back
643 * XXX here, which is not as efficient as it could be for
644 * XXX the write-through case.
645 */
646 void
647 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
648 bus_size_t len, int ops)
649 {
650
651 #ifdef DEBUG_DMA
652 printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
653 t, map, offset, len, ops);
654 #endif /* DEBUG_DMA */
655
656 /*
657 * Mixing of PRE and POST operations is not allowed.
658 */
659 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
660 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
661 panic("_bus_dmamap_sync: mix PRE and POST");
662
663 #ifdef DIAGNOSTIC
664 if (offset >= map->dm_mapsize)
665 panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
666 offset, map->dm_mapsize);
667 if (len == 0 || (offset + len) > map->dm_mapsize)
668 panic("_bus_dmamap_sync: bad length");
669 #endif
670
671 /*
672 * For a virtually-indexed write-back cache, we need
673 * to do the following things:
674 *
675 * PREREAD -- Invalidate the D-cache. We do this
676 * here in case a write-back is required by the back-end.
677 *
678 * PREWRITE -- Write-back the D-cache. Note that if
679 * we are doing a PREREAD|PREWRITE, we can collapse
680 * the whole thing into a single Wb-Inv.
681 *
682 * POSTREAD -- Nothing.
683 *
684 * POSTWRITE -- Nothing.
685 */
686
687 ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
688 if (ops == 0)
689 return;
690
691 /* Skip cache frobbing if mapping was COHERENT. */
692 if (map->_dm_flags & ARM32_DMAMAP_COHERENT) {
693 /* Drain the write buffer. */
694 cpu_drain_writebuf();
695 return;
696 }
697
698 /*
699 * If the mapping belongs to a non-kernel vmspace, and the
700 * vmspace has not been active since the last time a full
701 * cache flush was performed, we don't need to do anything.
702 */
703 if (__predict_false(!VMSPACE_IS_KERNEL_P(map->_dm_vmspace) &&
704 vm_map_pmap(&map->_dm_vmspace->vm_map)->pm_cstate.cs_cache_d == 0))
705 return;
706
707 switch (map->_dm_buftype) {
708 case ARM32_BUFTYPE_LINEAR:
709 _bus_dmamap_sync_linear(t, map, offset, len, ops);
710 break;
711
712 case ARM32_BUFTYPE_MBUF:
713 _bus_dmamap_sync_mbuf(t, map, offset, len, ops);
714 break;
715
716 case ARM32_BUFTYPE_UIO:
717 _bus_dmamap_sync_uio(t, map, offset, len, ops);
718 break;
719
720 case ARM32_BUFTYPE_RAW:
721 panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
722 break;
723
724 case ARM32_BUFTYPE_INVALID:
725 panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
726 break;
727
728 default:
729 printf("unknown buffer type %d\n", map->_dm_buftype);
730 panic("_bus_dmamap_sync");
731 }
732
733 /* Drain the write buffer. */
734 cpu_drain_writebuf();
735 }
736
737 /*
738 * Common function for DMA-safe memory allocation. May be called
739 * by bus-specific DMA memory allocation functions.
740 */
741
742 extern paddr_t physical_start;
743 extern paddr_t physical_end;
744
745 int
746 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
747 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
748 int flags)
749 {
750 struct arm32_dma_range *dr;
751 int error, i;
752
753 #ifdef DEBUG_DMA
754 printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
755 "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
756 boundary, segs, nsegs, rsegs, flags);
757 #endif
758
759 if ((dr = t->_ranges) != NULL) {
760 error = ENOMEM;
761 for (i = 0; i < t->_nranges; i++, dr++) {
762 if (dr->dr_len == 0)
763 continue;
764 error = _bus_dmamem_alloc_range(t, size, alignment,
765 boundary, segs, nsegs, rsegs, flags,
766 trunc_page(dr->dr_sysbase),
767 trunc_page(dr->dr_sysbase + dr->dr_len));
768 if (error == 0)
769 break;
770 }
771 } else {
772 error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
773 segs, nsegs, rsegs, flags, trunc_page(physical_start),
774 trunc_page(physical_end));
775 }
776
777 #ifdef DEBUG_DMA
778 printf("dmamem_alloc: =%d\n", error);
779 #endif
780
781 return(error);
782 }
783
784 /*
785 * Common function for freeing DMA-safe memory. May be called by
786 * bus-specific DMA memory free functions.
787 */
788 void
789 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
790 {
791 struct vm_page *m;
792 bus_addr_t addr;
793 struct pglist mlist;
794 int curseg;
795
796 #ifdef DEBUG_DMA
797 printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
798 #endif /* DEBUG_DMA */
799
800 /*
801 * Build a list of pages to free back to the VM system.
802 */
803 TAILQ_INIT(&mlist);
804 for (curseg = 0; curseg < nsegs; curseg++) {
805 for (addr = segs[curseg].ds_addr;
806 addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
807 addr += PAGE_SIZE) {
808 m = PHYS_TO_VM_PAGE(addr);
809 TAILQ_INSERT_TAIL(&mlist, m, pageq);
810 }
811 }
812 uvm_pglistfree(&mlist);
813 }
814
815 /*
816 * Common function for mapping DMA-safe memory. May be called by
817 * bus-specific DMA memory map functions.
818 */
819 int
820 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
821 size_t size, caddr_t *kvap, int flags)
822 {
823 vaddr_t va;
824 bus_addr_t addr;
825 int curseg;
826 pt_entry_t *ptep/*, pte*/;
827 const uvm_flag_t kmflags =
828 (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
829
830 #ifdef DEBUG_DMA
831 printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
832 segs, nsegs, (unsigned long)size, flags);
833 #endif /* DEBUG_DMA */
834
835 size = round_page(size);
836 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
837
838 if (va == 0)
839 return (ENOMEM);
840
841 *kvap = (caddr_t)va;
842
843 for (curseg = 0; curseg < nsegs; curseg++) {
844 for (addr = segs[curseg].ds_addr;
845 addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
846 addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
847 #ifdef DEBUG_DMA
848 printf("wiring p%lx to v%lx", addr, va);
849 #endif /* DEBUG_DMA */
850 if (size == 0)
851 panic("_bus_dmamem_map: size botch");
852 pmap_enter(pmap_kernel(), va, addr,
853 VM_PROT_READ | VM_PROT_WRITE,
854 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
855 /*
856 * If the memory must remain coherent with the
857 * cache then we must make the memory uncacheable
858 * in order to maintain virtual cache coherency.
859 * We must also guarantee the cache does not already
860 * contain the virtal addresses we are making
861 * uncacheable.
862 */
863 if (flags & BUS_DMA_COHERENT) {
864 cpu_dcache_wbinv_range(va, PAGE_SIZE);
865 cpu_drain_writebuf();
866 ptep = vtopte(va);
867 *ptep &= ~L2_S_CACHE_MASK;
868 PTE_SYNC(ptep);
869 tlb_flush();
870 }
871 #ifdef DEBUG_DMA
872 ptep = vtopte(va);
873 printf(" pte=v%p *pte=%x\n", ptep, *ptep);
874 #endif /* DEBUG_DMA */
875 }
876 }
877 pmap_update(pmap_kernel());
878 #ifdef DEBUG_DMA
879 printf("dmamem_map: =%p\n", *kvap);
880 #endif /* DEBUG_DMA */
881 return (0);
882 }
883
884 /*
885 * Common function for unmapping DMA-safe memory. May be called by
886 * bus-specific DMA memory unmapping functions.
887 */
888 void
889 _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
890 {
891
892 #ifdef DEBUG_DMA
893 printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
894 (unsigned long)size);
895 #endif /* DEBUG_DMA */
896 #ifdef DIAGNOSTIC
897 if ((u_long)kva & PGOFSET)
898 panic("_bus_dmamem_unmap");
899 #endif /* DIAGNOSTIC */
900
901 size = round_page(size);
902 pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
903 pmap_update(pmap_kernel());
904 uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
905 }
906
907 /*
908 * Common functin for mmap(2)'ing DMA-safe memory. May be called by
909 * bus-specific DMA mmap(2)'ing functions.
910 */
911 paddr_t
912 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
913 off_t off, int prot, int flags)
914 {
915 int i;
916
917 for (i = 0; i < nsegs; i++) {
918 #ifdef DIAGNOSTIC
919 if (off & PGOFSET)
920 panic("_bus_dmamem_mmap: offset unaligned");
921 if (segs[i].ds_addr & PGOFSET)
922 panic("_bus_dmamem_mmap: segment unaligned");
923 if (segs[i].ds_len & PGOFSET)
924 panic("_bus_dmamem_mmap: segment size not multiple"
925 " of page size");
926 #endif /* DIAGNOSTIC */
927 if (off >= segs[i].ds_len) {
928 off -= segs[i].ds_len;
929 continue;
930 }
931
932 return (arm_btop((u_long)segs[i].ds_addr + off));
933 }
934
935 /* Page not found. */
936 return (-1);
937 }
938
939 /**********************************************************************
940 * DMA utility functions
941 **********************************************************************/
942
943 /*
944 * Utility function to load a linear buffer. lastaddrp holds state
945 * between invocations (for multiple-buffer loads). segp contains
946 * the starting segment on entrace, and the ending segment on exit.
947 * first indicates if this is the first invocation of this function.
948 */
949 int
950 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
951 bus_size_t buflen, struct vmspace *vm, int flags)
952 {
953 bus_size_t sgsize;
954 bus_addr_t curaddr;
955 vaddr_t vaddr = (vaddr_t)buf;
956 pd_entry_t *pde;
957 pt_entry_t pte;
958 int error;
959 pmap_t pmap;
960 pt_entry_t *ptep;
961
962 #ifdef DEBUG_DMA
963 printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d)\n",
964 buf, buflen, flags);
965 #endif /* DEBUG_DMA */
966
967 pmap = vm_map_pmap(&vm->vm_map);
968
969 while (buflen > 0) {
970 /*
971 * Get the physical address for this segment.
972 *
973 * XXX Don't support checking for coherent mappings
974 * XXX in user address space.
975 */
976 if (__predict_true(pmap == pmap_kernel())) {
977 (void) pmap_get_pde_pte(pmap, vaddr, &pde, &ptep);
978 if (__predict_false(pmap_pde_section(pde))) {
979 curaddr = (*pde & L1_S_FRAME) |
980 (vaddr & L1_S_OFFSET);
981 if (*pde & L1_S_CACHE_MASK) {
982 map->_dm_flags &=
983 ~ARM32_DMAMAP_COHERENT;
984 }
985 } else {
986 pte = *ptep;
987 KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
988 if (__predict_false((pte & L2_TYPE_MASK)
989 == L2_TYPE_L)) {
990 curaddr = (pte & L2_L_FRAME) |
991 (vaddr & L2_L_OFFSET);
992 if (pte & L2_L_CACHE_MASK) {
993 map->_dm_flags &=
994 ~ARM32_DMAMAP_COHERENT;
995 }
996 } else {
997 curaddr = (pte & L2_S_FRAME) |
998 (vaddr & L2_S_OFFSET);
999 if (pte & L2_S_CACHE_MASK) {
1000 map->_dm_flags &=
1001 ~ARM32_DMAMAP_COHERENT;
1002 }
1003 }
1004 }
1005 } else {
1006 (void) pmap_extract(pmap, vaddr, &curaddr);
1007 map->_dm_flags &= ~ARM32_DMAMAP_COHERENT;
1008 }
1009
1010 /*
1011 * Compute the segment size, and adjust counts.
1012 */
1013 sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
1014 if (buflen < sgsize)
1015 sgsize = buflen;
1016
1017 error = _bus_dmamap_load_paddr(t, map, curaddr, sgsize);
1018 if (error)
1019 return (error);
1020
1021 vaddr += sgsize;
1022 buflen -= sgsize;
1023 }
1024
1025 return (0);
1026 }
1027
1028 /*
1029 * Allocate physical memory from the given physical address range.
1030 * Called by DMA-safe memory allocation methods.
1031 */
1032 int
1033 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1034 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1035 int flags, paddr_t low, paddr_t high)
1036 {
1037 paddr_t curaddr, lastaddr;
1038 struct vm_page *m;
1039 struct pglist mlist;
1040 int curseg, error;
1041
1042 #ifdef DEBUG_DMA
1043 printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
1044 t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
1045 #endif /* DEBUG_DMA */
1046
1047 /* Always round the size. */
1048 size = round_page(size);
1049
1050 /*
1051 * Allocate pages from the VM system.
1052 */
1053 error = uvm_pglistalloc(size, low, high, alignment, boundary,
1054 &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
1055 if (error)
1056 return (error);
1057
1058 /*
1059 * Compute the location, size, and number of segments actually
1060 * returned by the VM code.
1061 */
1062 m = TAILQ_FIRST(&mlist);
1063 curseg = 0;
1064 lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
1065 segs[curseg].ds_len = PAGE_SIZE;
1066 #ifdef DEBUG_DMA
1067 printf("alloc: page %lx\n", lastaddr);
1068 #endif /* DEBUG_DMA */
1069 m = TAILQ_NEXT(m, pageq);
1070
1071 for (; m != NULL; m = TAILQ_NEXT(m, pageq)) {
1072 curaddr = VM_PAGE_TO_PHYS(m);
1073 #ifdef DIAGNOSTIC
1074 if (curaddr < low || curaddr >= high) {
1075 printf("uvm_pglistalloc returned non-sensical"
1076 " address 0x%lx\n", curaddr);
1077 panic("_bus_dmamem_alloc_range");
1078 }
1079 #endif /* DIAGNOSTIC */
1080 #ifdef DEBUG_DMA
1081 printf("alloc: page %lx\n", curaddr);
1082 #endif /* DEBUG_DMA */
1083 if (curaddr == (lastaddr + PAGE_SIZE))
1084 segs[curseg].ds_len += PAGE_SIZE;
1085 else {
1086 curseg++;
1087 segs[curseg].ds_addr = curaddr;
1088 segs[curseg].ds_len = PAGE_SIZE;
1089 }
1090 lastaddr = curaddr;
1091 }
1092
1093 *rsegs = curseg + 1;
1094
1095 return (0);
1096 }
1097
1098 /*
1099 * Check if a memory region intersects with a DMA range, and return the
1100 * page-rounded intersection if it does.
1101 */
1102 int
1103 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
1104 paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
1105 {
1106 struct arm32_dma_range *dr;
1107 int i;
1108
1109 if (ranges == NULL)
1110 return (0);
1111
1112 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
1113 if (dr->dr_sysbase <= pa &&
1114 pa < (dr->dr_sysbase + dr->dr_len)) {
1115 /*
1116 * Beginning of region intersects with this range.
1117 */
1118 *pap = trunc_page(pa);
1119 *sizep = round_page(min(pa + size,
1120 dr->dr_sysbase + dr->dr_len) - pa);
1121 return (1);
1122 }
1123 if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
1124 /*
1125 * End of region intersects with this range.
1126 */
1127 *pap = trunc_page(dr->dr_sysbase);
1128 *sizep = round_page(min((pa + size) - dr->dr_sysbase,
1129 dr->dr_len));
1130 return (1);
1131 }
1132 }
1133
1134 /* No intersection found. */
1135 return (0);
1136 }
1137