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