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