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