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