bus_dma.c revision 1.18 1 /* $NetBSD: bus_dma.c,v 1.18 2002/08/14 22:56:55 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 (((addr | len) & arm_dcache_align_mask) == 0)
357 cpu_dcache_inv_range(addr, len);
358 else
359 cpu_dcache_wbinv_range(addr, len);
360 break;
361
362 case BUS_DMASYNC_PREWRITE:
363 cpu_dcache_wb_range(addr, len);
364 break;
365 }
366 }
367
368 static void
369 _bus_dmamap_sync_mbuf(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
370 bus_size_t len, int ops)
371 {
372 struct mbuf *m, *m0 = map->_dm_origbuf;
373 bus_size_t minlen, moff;
374 vaddr_t maddr;
375
376 for (moff = offset, m = m0; m != NULL && len != 0;
377 m = m->m_next) {
378 /* Find the beginning mbuf. */
379 if (moff >= m->m_len) {
380 moff -= m->m_len;
381 continue;
382 }
383
384 /*
385 * Now at the first mbuf to sync; nail each one until
386 * we have exhausted the length.
387 */
388 minlen = m->m_len - moff;
389 if (len < minlen)
390 minlen = len;
391
392 maddr = mtod(m, vaddr_t);
393 maddr += moff;
394
395 switch (ops) {
396 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
397 cpu_dcache_wbinv_range(maddr, minlen);
398 break;
399
400 case BUS_DMASYNC_PREREAD:
401 if (((maddr | minlen) & arm_dcache_align_mask) == 0)
402 cpu_dcache_inv_range(maddr, minlen);
403 else
404 cpu_dcache_wbinv_range(maddr, minlen);
405 break;
406
407 case BUS_DMASYNC_PREWRITE:
408 cpu_dcache_wb_range(maddr, minlen);
409 break;
410 }
411 moff = 0;
412 len -= minlen;
413 }
414 }
415
416 static void
417 _bus_dmamap_sync_uio(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
418 bus_size_t len, int ops)
419 {
420 struct uio *uio = map->_dm_origbuf;
421 struct iovec *iov;
422 bus_size_t minlen, ioff;
423 vaddr_t addr;
424
425 for (iov = uio->uio_iov, ioff = offset; len != 0; iov++) {
426 /* Find the beginning iovec. */
427 if (ioff >= iov->iov_len) {
428 ioff -= iov->iov_len;
429 continue;
430 }
431
432 /*
433 * Now at the first iovec to sync; nail each one until
434 * we have exhausted the length.
435 */
436 minlen = iov->iov_len - ioff;
437 if (len < minlen)
438 minlen = len;
439
440 addr = (vaddr_t) iov->iov_base;
441 addr += ioff;
442
443 switch (ops) {
444 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
445 cpu_dcache_wbinv_range(addr, minlen);
446 break;
447
448 case BUS_DMASYNC_PREREAD:
449 if (((addr | minlen) & arm_dcache_align_mask) == 0)
450 cpu_dcache_inv_range(addr, minlen);
451 else
452 cpu_dcache_wbinv_range(addr, minlen);
453 break;
454
455 case BUS_DMASYNC_PREWRITE:
456 cpu_dcache_wb_range(addr, minlen);
457 break;
458 }
459 ioff = 0;
460 len -= minlen;
461 }
462 }
463
464 /*
465 * Common function for DMA map synchronization. May be called
466 * by bus-specific DMA map synchronization functions.
467 *
468 * This version works for the Virtually Indexed Virtually Tagged
469 * cache found on 32-bit ARM processors.
470 *
471 * XXX Should have separate versions for write-through vs.
472 * XXX write-back caches. We currently assume write-back
473 * XXX here, which is not as efficient as it could be for
474 * XXX the write-through case.
475 */
476 void
477 _bus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
478 bus_size_t len, int ops)
479 {
480
481 #ifdef DEBUG_DMA
482 printf("dmamap_sync: t=%p map=%p offset=%lx len=%lx ops=%x\n",
483 t, map, offset, len, ops);
484 #endif /* DEBUG_DMA */
485
486 /*
487 * Mixing of PRE and POST operations is not allowed.
488 */
489 if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
490 (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
491 panic("_bus_dmamap_sync: mix PRE and POST");
492
493 #ifdef DIAGNOSTIC
494 if (offset >= map->dm_mapsize)
495 panic("_bus_dmamap_sync: bad offset %lu (map size is %lu)",
496 offset, map->dm_mapsize);
497 if (len == 0 || (offset + len) > map->dm_mapsize)
498 panic("_bus_dmamap_sync: bad length");
499 #endif
500
501 /*
502 * For a virtually-indexed write-back cache, we need
503 * to do the following things:
504 *
505 * PREREAD -- Invalidate the D-cache. We do this
506 * here in case a write-back is required by the back-end.
507 *
508 * PREWRITE -- Write-back the D-cache. Note that if
509 * we are doing a PREREAD|PREWRITE, we can collapse
510 * the whole thing into a single Wb-Inv.
511 *
512 * POSTREAD -- Nothing.
513 *
514 * POSTWRITE -- Nothing.
515 */
516
517 ops &= (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
518 if (ops == 0)
519 return;
520
521 /* Skip cache frobbing if mapping was COHERENT. */
522 if (map->_dm_flags & ARM32_DMAMAP_COHERENT) {
523 /* Drain the write buffer. */
524 cpu_drain_writebuf();
525 return;
526 }
527
528 /*
529 * If the mapping is not the kernel's and also not the
530 * current process's (XXX actually, vmspace), then we
531 * don't have anything to do, since the cache is Wb-Inv'd
532 * on context switch.
533 *
534 * XXX REVISIT WHEN WE DO FCSE!
535 */
536 if (__predict_false(map->_dm_proc != NULL && map->_dm_proc != curproc))
537 return;
538
539 switch (map->_dm_buftype) {
540 case ARM32_BUFTYPE_LINEAR:
541 _bus_dmamap_sync_linear(t, map, offset, len, ops);
542 break;
543
544 case ARM32_BUFTYPE_MBUF:
545 _bus_dmamap_sync_mbuf(t, map, offset, len, ops);
546 break;
547
548 case ARM32_BUFTYPE_UIO:
549 _bus_dmamap_sync_uio(t, map, offset, len, ops);
550 break;
551
552 case ARM32_BUFTYPE_RAW:
553 panic("_bus_dmamap_sync: ARM32_BUFTYPE_RAW");
554 break;
555
556 case ARM32_BUFTYPE_INVALID:
557 panic("_bus_dmamap_sync: ARM32_BUFTYPE_INVALID");
558 break;
559
560 default:
561 printf("unknown buffer type %d\n", map->_dm_buftype);
562 panic("_bus_dmamap_sync");
563 }
564
565 /* Drain the write buffer. */
566 cpu_drain_writebuf();
567 }
568
569 /*
570 * Common function for DMA-safe memory allocation. May be called
571 * by bus-specific DMA memory allocation functions.
572 */
573
574 extern paddr_t physical_start;
575 extern paddr_t physical_freestart;
576 extern paddr_t physical_freeend;
577 extern paddr_t physical_end;
578
579 int
580 _bus_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
581 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
582 int flags)
583 {
584 struct arm32_dma_range *dr;
585 int error, i;
586
587 #ifdef DEBUG_DMA
588 printf("dmamem_alloc t=%p size=%lx align=%lx boundary=%lx "
589 "segs=%p nsegs=%x rsegs=%p flags=%x\n", t, size, alignment,
590 boundary, segs, nsegs, rsegs, flags);
591 #endif
592
593 if ((dr = t->_ranges) != NULL) {
594 for (i = 0; i < t->_nranges; i++, dr++) {
595 if (dr->dr_len == 0) {
596 error = ENOMEM;
597 continue;
598 }
599 error = _bus_dmamem_alloc_range(t, size, alignment,
600 boundary, segs, nsegs, rsegs, flags,
601 trunc_page(dr->dr_sysbase),
602 trunc_page(dr->dr_sysbase + dr->dr_len));
603 if (error == 0)
604 break;
605 }
606 } else {
607 error = _bus_dmamem_alloc_range(t, size, alignment, boundary,
608 segs, nsegs, rsegs, flags, trunc_page(physical_start),
609 trunc_page(physical_end));
610 }
611
612 #ifdef DEBUG_DMA
613 printf("dmamem_alloc: =%d\n", error);
614 #endif
615
616 return(error);
617 }
618
619 /*
620 * Common function for freeing DMA-safe memory. May be called by
621 * bus-specific DMA memory free functions.
622 */
623 void
624 _bus_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
625 {
626 struct vm_page *m;
627 bus_addr_t addr;
628 struct pglist mlist;
629 int curseg;
630
631 #ifdef DEBUG_DMA
632 printf("dmamem_free: t=%p segs=%p nsegs=%x\n", t, segs, nsegs);
633 #endif /* DEBUG_DMA */
634
635 /*
636 * Build a list of pages to free back to the VM system.
637 */
638 TAILQ_INIT(&mlist);
639 for (curseg = 0; curseg < nsegs; curseg++) {
640 for (addr = segs[curseg].ds_addr;
641 addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
642 addr += PAGE_SIZE) {
643 m = PHYS_TO_VM_PAGE(addr);
644 TAILQ_INSERT_TAIL(&mlist, m, pageq);
645 }
646 }
647 uvm_pglistfree(&mlist);
648 }
649
650 /*
651 * Common function for mapping DMA-safe memory. May be called by
652 * bus-specific DMA memory map functions.
653 */
654 int
655 _bus_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
656 size_t size, caddr_t *kvap, int flags)
657 {
658 vaddr_t va;
659 bus_addr_t addr;
660 int curseg;
661 pt_entry_t *ptep/*, pte*/;
662
663 #ifdef DEBUG_DMA
664 printf("dmamem_map: t=%p segs=%p nsegs=%x size=%lx flags=%x\n", t,
665 segs, nsegs, (unsigned long)size, flags);
666 #endif /* DEBUG_DMA */
667
668 size = round_page(size);
669 va = uvm_km_valloc(kernel_map, size);
670
671 if (va == 0)
672 return (ENOMEM);
673
674 *kvap = (caddr_t)va;
675
676 for (curseg = 0; curseg < nsegs; curseg++) {
677 for (addr = segs[curseg].ds_addr;
678 addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
679 addr += NBPG, va += NBPG, size -= NBPG) {
680 #ifdef DEBUG_DMA
681 printf("wiring p%lx to v%lx", addr, va);
682 #endif /* DEBUG_DMA */
683 if (size == 0)
684 panic("_bus_dmamem_map: size botch");
685 pmap_enter(pmap_kernel(), va, addr,
686 VM_PROT_READ | VM_PROT_WRITE,
687 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
688 /*
689 * If the memory must remain coherent with the
690 * cache then we must make the memory uncacheable
691 * in order to maintain virtual cache coherency.
692 * We must also guarentee the cache does not already
693 * contain the virtal addresses we are making
694 * uncacheable.
695 */
696 if (flags & BUS_DMA_COHERENT) {
697 cpu_dcache_wbinv_range(va, NBPG);
698 cpu_drain_writebuf();
699 ptep = vtopte(va);
700 *ptep &= ~L2_S_CACHE_MASK;
701 tlb_flush();
702 }
703 #ifdef DEBUG_DMA
704 ptep = vtopte(va);
705 printf(" pte=v%p *pte=%x\n", ptep, *ptep);
706 #endif /* DEBUG_DMA */
707 }
708 }
709 pmap_update(pmap_kernel());
710 #ifdef DEBUG_DMA
711 printf("dmamem_map: =%p\n", *kvap);
712 #endif /* DEBUG_DMA */
713 return (0);
714 }
715
716 /*
717 * Common function for unmapping DMA-safe memory. May be called by
718 * bus-specific DMA memory unmapping functions.
719 */
720 void
721 _bus_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
722 {
723
724 #ifdef DEBUG_DMA
725 printf("dmamem_unmap: t=%p kva=%p size=%lx\n", t, kva,
726 (unsigned long)size);
727 #endif /* DEBUG_DMA */
728 #ifdef DIAGNOSTIC
729 if ((u_long)kva & PGOFSET)
730 panic("_bus_dmamem_unmap");
731 #endif /* DIAGNOSTIC */
732
733 size = round_page(size);
734 uvm_km_free(kernel_map, (vaddr_t)kva, size);
735 }
736
737 /*
738 * Common functin for mmap(2)'ing DMA-safe memory. May be called by
739 * bus-specific DMA mmap(2)'ing functions.
740 */
741 paddr_t
742 _bus_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
743 off_t off, int prot, int flags)
744 {
745 int i;
746
747 for (i = 0; i < nsegs; i++) {
748 #ifdef DIAGNOSTIC
749 if (off & PGOFSET)
750 panic("_bus_dmamem_mmap: offset unaligned");
751 if (segs[i].ds_addr & PGOFSET)
752 panic("_bus_dmamem_mmap: segment unaligned");
753 if (segs[i].ds_len & PGOFSET)
754 panic("_bus_dmamem_mmap: segment size not multiple"
755 " of page size");
756 #endif /* DIAGNOSTIC */
757 if (off >= segs[i].ds_len) {
758 off -= segs[i].ds_len;
759 continue;
760 }
761
762 return (arm_btop((u_long)segs[i].ds_addr + off));
763 }
764
765 /* Page not found. */
766 return (-1);
767 }
768
769 /**********************************************************************
770 * DMA utility functions
771 **********************************************************************/
772
773 /*
774 * Utility function to load a linear buffer. lastaddrp holds state
775 * between invocations (for multiple-buffer loads). segp contains
776 * the starting segment on entrace, and the ending segment on exit.
777 * first indicates if this is the first invocation of this function.
778 */
779 int
780 _bus_dmamap_load_buffer(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
781 bus_size_t buflen, struct proc *p, int flags, paddr_t *lastaddrp,
782 int *segp, int first)
783 {
784 struct arm32_dma_range *dr;
785 bus_size_t sgsize;
786 bus_addr_t curaddr, lastaddr, baddr, bmask;
787 vaddr_t vaddr = (vaddr_t)buf;
788 pd_entry_t *pde;
789 pt_entry_t pte;
790 int seg;
791 pmap_t pmap;
792
793 #ifdef DEBUG_DMA
794 printf("_bus_dmamem_load_buffer(buf=%p, len=%lx, flags=%d, 1st=%d)\n",
795 buf, buflen, flags, first);
796 #endif /* DEBUG_DMA */
797
798 if (p != NULL)
799 pmap = p->p_vmspace->vm_map.pmap;
800 else
801 pmap = pmap_kernel();
802
803 lastaddr = *lastaddrp;
804 bmask = ~(map->_dm_boundary - 1);
805
806 for (seg = *segp; buflen > 0; ) {
807 /*
808 * Get the physical address for this segment.
809 *
810 * XXX Don't support checking for coherent mappings
811 * XXX in user address space.
812 */
813 if (__predict_true(pmap == pmap_kernel())) {
814 pde = pmap_pde(pmap, vaddr);
815 if (__predict_false(pmap_pde_section(pde))) {
816 curaddr = (*pde & L1_S_FRAME) |
817 (vaddr & L1_S_OFFSET);
818 if (*pde & L1_S_CACHE_MASK) {
819 map->_dm_flags &=
820 ~ARM32_DMAMAP_COHERENT;
821 }
822 } else {
823 pte = *vtopte(vaddr);
824 KDASSERT((pte & L2_TYPE_MASK) != L2_TYPE_INV);
825 if (__predict_false((pte & L2_TYPE_MASK)
826 == L2_TYPE_L)) {
827 curaddr = (pte & L2_L_FRAME) |
828 (vaddr & L2_L_OFFSET);
829 if (pte & L2_L_CACHE_MASK) {
830 map->_dm_flags &=
831 ~ARM32_DMAMAP_COHERENT;
832 }
833 } else {
834 curaddr = (pte & L2_S_FRAME) |
835 (vaddr & L2_S_OFFSET);
836 if (pte & L2_S_CACHE_MASK) {
837 map->_dm_flags &=
838 ~ARM32_DMAMAP_COHERENT;
839 }
840 }
841 }
842 } else
843 (void) pmap_extract(pmap, vaddr, &curaddr);
844
845 /*
846 * Make sure we're in an allowed DMA range.
847 */
848 if (t->_ranges != NULL) {
849 /* XXX cache last result? */
850 dr = _bus_dma_inrange(t->_ranges, t->_nranges,
851 curaddr);
852 if (dr == NULL)
853 return (EINVAL);
854
855 /*
856 * In a valid DMA range. Translate the physical
857 * memory address to an address in the DMA window.
858 */
859 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase;
860 }
861
862 /*
863 * Compute the segment size, and adjust counts.
864 */
865 sgsize = NBPG - ((u_long)vaddr & PGOFSET);
866 if (buflen < sgsize)
867 sgsize = buflen;
868
869 /*
870 * Make sure we don't cross any boundaries.
871 */
872 if (map->_dm_boundary > 0) {
873 baddr = (curaddr + map->_dm_boundary) & bmask;
874 if (sgsize > (baddr - curaddr))
875 sgsize = (baddr - curaddr);
876 }
877
878 /*
879 * Insert chunk into a segment, coalescing with
880 * previous segment if possible.
881 */
882 if (first) {
883 map->dm_segs[seg].ds_addr = curaddr;
884 map->dm_segs[seg].ds_len = sgsize;
885 first = 0;
886 } else {
887 if (curaddr == lastaddr &&
888 (map->dm_segs[seg].ds_len + sgsize) <=
889 map->_dm_maxsegsz &&
890 (map->_dm_boundary == 0 ||
891 (map->dm_segs[seg].ds_addr & bmask) ==
892 (curaddr & bmask)))
893 map->dm_segs[seg].ds_len += sgsize;
894 else {
895 if (++seg >= map->_dm_segcnt)
896 break;
897 map->dm_segs[seg].ds_addr = curaddr;
898 map->dm_segs[seg].ds_len = sgsize;
899 }
900 }
901
902 lastaddr = curaddr + sgsize;
903 vaddr += sgsize;
904 buflen -= sgsize;
905 }
906
907 *segp = seg;
908 *lastaddrp = lastaddr;
909
910 /*
911 * Did we fit?
912 */
913 if (buflen != 0)
914 return (EFBIG); /* XXX better return value here? */
915 return (0);
916 }
917
918 /*
919 * Check to see if the specified page is in an allowed DMA range.
920 */
921 struct arm32_dma_range *
922 _bus_dma_inrange(struct arm32_dma_range *ranges, int nranges,
923 bus_addr_t curaddr)
924 {
925 struct arm32_dma_range *dr;
926 int i;
927
928 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
929 if (curaddr >= dr->dr_sysbase &&
930 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len))
931 return (dr);
932 }
933
934 return (NULL);
935 }
936
937 /*
938 * Allocate physical memory from the given physical address range.
939 * Called by DMA-safe memory allocation methods.
940 */
941 int
942 _bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
943 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
944 int flags, paddr_t low, paddr_t high)
945 {
946 paddr_t curaddr, lastaddr;
947 struct vm_page *m;
948 struct pglist mlist;
949 int curseg, error;
950
951 #ifdef DEBUG_DMA
952 printf("alloc_range: t=%p size=%lx align=%lx boundary=%lx segs=%p nsegs=%x rsegs=%p flags=%x lo=%lx hi=%lx\n",
953 t, size, alignment, boundary, segs, nsegs, rsegs, flags, low, high);
954 #endif /* DEBUG_DMA */
955
956 /* Always round the size. */
957 size = round_page(size);
958
959 /*
960 * Allocate pages from the VM system.
961 */
962 error = uvm_pglistalloc(size, low, high, alignment, boundary,
963 &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
964 if (error)
965 return (error);
966
967 /*
968 * Compute the location, size, and number of segments actually
969 * returned by the VM code.
970 */
971 m = mlist.tqh_first;
972 curseg = 0;
973 lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
974 segs[curseg].ds_len = PAGE_SIZE;
975 #ifdef DEBUG_DMA
976 printf("alloc: page %lx\n", lastaddr);
977 #endif /* DEBUG_DMA */
978 m = m->pageq.tqe_next;
979
980 for (; m != NULL; m = m->pageq.tqe_next) {
981 curaddr = VM_PAGE_TO_PHYS(m);
982 #ifdef DIAGNOSTIC
983 if (curaddr < low || curaddr >= high) {
984 printf("uvm_pglistalloc returned non-sensical"
985 " address 0x%lx\n", curaddr);
986 panic("_bus_dmamem_alloc_range");
987 }
988 #endif /* DIAGNOSTIC */
989 #ifdef DEBUG_DMA
990 printf("alloc: page %lx\n", curaddr);
991 #endif /* DEBUG_DMA */
992 if (curaddr == (lastaddr + PAGE_SIZE))
993 segs[curseg].ds_len += PAGE_SIZE;
994 else {
995 curseg++;
996 segs[curseg].ds_addr = curaddr;
997 segs[curseg].ds_len = PAGE_SIZE;
998 }
999 lastaddr = curaddr;
1000 }
1001
1002 *rsegs = curseg + 1;
1003
1004 return (0);
1005 }
1006
1007 /*
1008 * Check if a memory region intersects with a DMA range, and return the
1009 * page-rounded intersection if it does.
1010 */
1011 int
1012 arm32_dma_range_intersect(struct arm32_dma_range *ranges, int nranges,
1013 paddr_t pa, psize_t size, paddr_t *pap, psize_t *sizep)
1014 {
1015 struct arm32_dma_range *dr;
1016 int i;
1017
1018 if (ranges == NULL)
1019 return (0);
1020
1021 for (i = 0, dr = ranges; i < nranges; i++, dr++) {
1022 if (dr->dr_sysbase <= pa &&
1023 pa < (dr->dr_sysbase + dr->dr_len)) {
1024 /*
1025 * Beginning of region intersects with this range.
1026 */
1027 *pap = trunc_page(pa);
1028 *sizep = round_page(min(pa + size,
1029 dr->dr_sysbase + dr->dr_len) - pa);
1030 return (1);
1031 }
1032 if (pa < dr->dr_sysbase && dr->dr_sysbase < (pa + size)) {
1033 /*
1034 * End of region intersects with this range.
1035 */
1036 *pap = trunc_page(dr->dr_sysbase);
1037 *sizep = round_page(min((pa + size) - dr->dr_sysbase,
1038 dr->dr_len));
1039 return (1);
1040 }
1041 }
1042
1043 /* No intersection found. */
1044 return (0);
1045 }
1046