1 /*-
2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/6.1/sys/i386/i386/busdma_machdep.c 158179 2006-04-30 16:44:43Z cvs2svn $");
29
30 #include <sys/param.h>
31 #include <sys/kdb.h>
32 #include <ddb/ddb.h>
33 #include <ddb/db_output.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/bus.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/proc.h>
42 #include <sys/mutex.h>
43 #include <sys/mbuf.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
50
51 #include <machine/atomic.h>
52 #include <machine/bus.h>
53 #include <machine/md_var.h>
54
55 #define MAX_BPAGES 512
56
57 struct bounce_zone;
58
59 struct bus_dma_tag {
60 bus_dma_tag_t parent;
61 bus_size_t alignment;
62 bus_size_t boundary;
63 bus_addr_t lowaddr;
64 bus_addr_t highaddr;
65 bus_dma_filter_t *filter;
66 void *filterarg;
67 bus_size_t maxsize;
68 u_int nsegments;
69 bus_size_t maxsegsz;
70 int flags;
71 int ref_count;
72 int map_count;
73 bus_dma_lock_t *lockfunc;
74 void *lockfuncarg;
75 bus_dma_segment_t *segments;
76 struct bounce_zone *bounce_zone;
77 };
78
79 struct bounce_page {
80 vm_offset_t vaddr; /* kva of bounce buffer */
81 bus_addr_t busaddr; /* Physical address */
82 vm_offset_t datavaddr; /* kva of client data */
83 bus_size_t datacount; /* client data count */
84 STAILQ_ENTRY(bounce_page) links;
85 };
86
87 int busdma_swi_pending;
88
89 struct bounce_zone {
90 STAILQ_ENTRY(bounce_zone) links;
91 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
92 int total_bpages;
93 int free_bpages;
94 int reserved_bpages;
95 int active_bpages;
96 int total_bounced;
97 int total_deferred;
98 bus_size_t alignment;
99 bus_size_t boundary;
100 bus_addr_t lowaddr;
101 char zoneid[8];
102 char lowaddrid[20];
103 struct sysctl_ctx_list sysctl_tree;
104 struct sysctl_oid *sysctl_tree_top;
105 };
106
107 static struct mtx bounce_lock;
108 static int total_bpages;
109 static int busdma_zonecount;
110 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
111
112 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
113 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
114 "Total bounce pages");
115
116 struct bus_dmamap {
117 struct bp_list bpages;
118 int pagesneeded;
119 int pagesreserved;
120 bus_dma_tag_t dmat;
121 void *buf; /* unmapped buffer pointer */
122 bus_size_t buflen; /* unmapped buffer length */
123 bus_dmamap_callback_t *callback;
124 void *callback_arg;
125 STAILQ_ENTRY(bus_dmamap) links;
126 };
127
128 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
129 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
130 static struct bus_dmamap nobounce_dmamap;
131
132 static void init_bounce_pages(void *dummy);
133 static int alloc_bounce_zone(bus_dma_tag_t dmat);
134 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
135 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
136 int commit);
137 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
138 vm_offset_t vaddr, bus_size_t size);
139 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
140 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
141
142 /*
143 * Return true if a match is made.
144 *
145 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
146 *
147 * If paddr is within the bounds of the dma tag then call the filter callback
148 * to check for a match, if there is no filter callback then assume a match.
149 */
150 static __inline int
151 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
152 {
153 int retval;
154
155 retval = 0;
156
157 do {
158 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
159 || ((paddr & (dmat->alignment - 1)) != 0))
160 && (dmat->filter == NULL
161 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
162 retval = 1;
163
164 dmat = dmat->parent;
165 } while (retval == 0 && dmat != NULL);
166 return (retval);
167 }
168
169 /*
170 * Convenience function for manipulating driver locks from busdma (during
171 * busdma_swi, for example). Drivers that don't provide their own locks
172 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
173 * non-mutex locking scheme don't have to use this at all.
174 */
175 void
176 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
177 {
178 struct mtx *dmtx;
179
180 dmtx = (struct mtx *)arg;
181 switch (op) {
182 case BUS_DMA_LOCK:
183 mtx_lock(dmtx);
184 break;
185 case BUS_DMA_UNLOCK:
186 mtx_unlock(dmtx);
187 break;
188 default:
189 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
190 }
191 }
192
193 /*
194 * dflt_lock should never get called. It gets put into the dma tag when
195 * lockfunc == NULL, which is only valid if the maps that are associated
196 * with the tag are meant to never be defered.
197 * XXX Should have a way to identify which driver is responsible here.
198 */
199 static void
200 dflt_lock(void *arg, bus_dma_lock_op_t op)
201 {
202 panic("driver error: busdma dflt_lock called");
203 }
204
205 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
206 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
207 /*
208 * Allocate a device specific dma_tag.
209 */
210 int
211 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
212 bus_size_t boundary, bus_addr_t lowaddr,
213 bus_addr_t highaddr, bus_dma_filter_t *filter,
214 void *filterarg, bus_size_t maxsize, int nsegments,
215 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
216 void *lockfuncarg, bus_dma_tag_t *dmat)
217 {
218 bus_dma_tag_t newtag;
219 int error = 0;
220
221 /* Basic sanity checking */
222 if (boundary != 0 && boundary < maxsegsz)
223 maxsegsz = boundary;
224
225 /* Return a NULL tag on failure */
226 *dmat = NULL;
227
228 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
229 M_ZERO | M_NOWAIT);
230 if (newtag == NULL) {
231 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
232 __func__, newtag, 0, error);
233 return (ENOMEM);
234 }
235
236 newtag->parent = parent;
237 newtag->alignment = alignment;
238 newtag->boundary = boundary;
239 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
240 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
241 (PAGE_SIZE - 1);
242 newtag->filter = filter;
243 newtag->filterarg = filterarg;
244 newtag->maxsize = maxsize;
245 newtag->nsegments = nsegments;
246 newtag->maxsegsz = maxsegsz;
247 newtag->flags = flags;
248 newtag->ref_count = 1; /* Count ourself */
249 newtag->map_count = 0;
250 if (lockfunc != NULL) {
251 newtag->lockfunc = lockfunc;
252 newtag->lockfuncarg = lockfuncarg;
253 } else {
254 newtag->lockfunc = dflt_lock;
255 newtag->lockfuncarg = NULL;
256 }
257 newtag->segments = NULL;
258
259 /* Take into account any restrictions imposed by our parent tag */
260 if (parent != NULL) {
261 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
262 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
263 if (newtag->boundary == 0)
264 newtag->boundary = parent->boundary;
265 else if (parent->boundary != 0)
266 newtag->boundary = MIN(parent->boundary,
267 newtag->boundary);
268 if (newtag->filter == NULL) {
269 /*
270 * Short circuit looking at our parent directly
271 * since we have encapsulated all of its information
272 */
273 newtag->filter = parent->filter;
274 newtag->filterarg = parent->filterarg;
275 newtag->parent = parent->parent;
276 }
277 if (newtag->parent != NULL)
278 atomic_add_int(&parent->ref_count, 1);
279 }
280
281 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
282 || newtag->alignment > 1)
283 newtag->flags |= BUS_DMA_COULD_BOUNCE;
284
285 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
286 (flags & BUS_DMA_ALLOCNOW) != 0) {
287 struct bounce_zone *bz;
288
289 /* Must bounce */
290
291 if ((error = alloc_bounce_zone(newtag)) != 0) {
292 free(newtag, M_DEVBUF);
293 return (error);
294 }
295 bz = newtag->bounce_zone;
296
297 if (ptoa(bz->total_bpages) < maxsize) {
298 int pages;
299
300 pages = atop(maxsize) - bz->total_bpages;
301
302 /* Add pages to our bounce pool */
303 if (alloc_bounce_pages(newtag, pages) < pages)
304 error = ENOMEM;
305 }
306 /* Performed initial allocation */
307 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
308 }
309
310 if (error != 0) {
311 free(newtag, M_DEVBUF);
312 } else {
313 *dmat = newtag;
314 }
315 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
316 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
317 return (error);
318 }
319
320 int
321 bus_dma_tag_destroy(bus_dma_tag_t dmat)
322 {
323 bus_dma_tag_t dmat_copy;
324 int error;
325
326 error = 0;
327 dmat_copy = dmat;
328
329 if (dmat != NULL) {
330
331 if (dmat->map_count != 0) {
332 error = EBUSY;
333 goto out;
334 }
335
336 while (dmat != NULL) {
337 bus_dma_tag_t parent;
338
339 parent = dmat->parent;
340 atomic_subtract_int(&dmat->ref_count, 1);
341 if (dmat->ref_count == 0) {
342 if (dmat->segments != NULL)
343 free(dmat->segments, M_DEVBUF);
344 free(dmat, M_DEVBUF);
345 /*
346 * Last reference count, so
347 * release our reference
348 * count on our parent.
349 */
350 dmat = parent;
351 } else
352 dmat = NULL;
353 }
354 }
355 out:
356 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
357 return (error);
358 }
359
360 /*
361 * Allocate a handle for mapping from kva/uva/physical
362 * address space into bus device space.
363 */
364 int
365 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
366 {
367 int error;
368
369 error = 0;
370
371 if (dmat->segments == NULL) {
372 dmat->segments = (bus_dma_segment_t *)malloc(
373 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
374 M_NOWAIT);
375 if (dmat->segments == NULL) {
376 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
377 __func__, dmat, ENOMEM);
378 return (ENOMEM);
379 }
380 }
381
382 /*
383 * Bouncing might be required if the driver asks for an active
384 * exclusion region, a data alignment that is stricter than 1, and/or
385 * an active address boundary.
386 */
387 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
388
389 /* Must bounce */
390 struct bounce_zone *bz;
391 int maxpages;
392
393 if (dmat->bounce_zone == NULL) {
394 if ((error = alloc_bounce_zone(dmat)) != 0)
395 return (error);
396 }
397 bz = dmat->bounce_zone;
398
399 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
400 M_NOWAIT | M_ZERO);
401 if (*mapp == NULL) {
402 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
403 __func__, dmat, ENOMEM);
404 return (ENOMEM);
405 }
406
407 /* Initialize the new map */
408 STAILQ_INIT(&((*mapp)->bpages));
409
410 /*
411 * Attempt to add pages to our pool on a per-instance
412 * basis up to a sane limit.
413 */
414 if (dmat->alignment > 1)
415 maxpages = MAX_BPAGES;
416 else
417 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
418 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
419 || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
420 int pages;
421
422 pages = MAX(atop(dmat->maxsize), 1);
423 pages = MIN(maxpages - bz->total_bpages, pages);
424 pages = MAX(pages, 1);
425 if (alloc_bounce_pages(dmat, pages) < pages)
426 error = ENOMEM;
427
428 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
429 if (error == 0)
430 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
431 } else {
432 error = 0;
433 }
434 }
435 } else {
436 *mapp = NULL;
437 }
438 if (error == 0)
439 dmat->map_count++;
440 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
441 __func__, dmat, dmat->flags, error);
442 return (error);
443 }
444
445 /*
446 * Destroy a handle for mapping from kva/uva/physical
447 * address space into bus device space.
448 */
449 int
450 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
451 {
452 if (map != NULL && map != &nobounce_dmamap) {
453 if (STAILQ_FIRST(&map->bpages) != NULL) {
454 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
455 __func__, dmat, EBUSY);
456 return (EBUSY);
457 }
458 free(map, M_DEVBUF);
459 }
460 dmat->map_count--;
461 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
462 return (0);
463 }
464
465
466 /*
467 * Allocate a piece of memory that can be efficiently mapped into
468 * bus device space based on the constraints lited in the dma tag.
469 * A dmamap to for use with dmamap_load is also allocated.
470 */
471 int
472 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
473 bus_dmamap_t *mapp)
474 {
475 int mflags;
476
477 if (flags & BUS_DMA_NOWAIT)
478 mflags = M_NOWAIT;
479 else
480 mflags = M_WAITOK;
481 if (flags & BUS_DMA_ZERO)
482 mflags |= M_ZERO;
483
484 /* If we succeed, no mapping/bouncing will be required */
485 *mapp = NULL;
486
487 if (dmat->segments == NULL) {
488 dmat->segments = (bus_dma_segment_t *)malloc(
489 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
490 M_NOWAIT);
491 if (dmat->segments == NULL) {
492 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
493 __func__, dmat, dmat->flags, ENOMEM);
494 return (ENOMEM);
495 }
496 }
497
498 if ((dmat->maxsize <= PAGE_SIZE) &&
499 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
500 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
501 } else {
502 /*
503 * XXX Use Contigmalloc until it is merged into this facility
504 * and handles multi-seg allocations. Nobody is doing
505 * multi-seg allocations yet though.
506 * XXX Certain AGP hardware does.
507 */
508 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
509 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
510 dmat->boundary);
511 }
512 if (*vaddr == NULL) {
513 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
514 __func__, dmat, dmat->flags, ENOMEM);
515 return (ENOMEM);
516 }
517 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
518 __func__, dmat, dmat->flags, ENOMEM);
519 return (0);
520 }
521
522 /*
523 * Free a piece of memory and it's allociated dmamap, that was allocated
524 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
525 */
526 void
527 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
528 {
529 /*
530 * dmamem does not need to be bounced, so the map should be
531 * NULL
532 */
533 if (map != NULL)
534 panic("bus_dmamem_free: Invalid map freed\n");
535 if ((dmat->maxsize <= PAGE_SIZE)
536 && dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
537 free(vaddr, M_DEVBUF);
538 else {
539 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
540 }
541 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
542 }
543
544 /*
545 * Utility function to load a linear buffer. lastaddrp holds state
546 * between invocations (for multiple-buffer loads). segp contains
547 * the starting segment on entrace, and the ending segment on exit.
548 * first indicates if this is the first invocation of this function.
549 */
550 static __inline int
551 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
552 bus_dmamap_t map,
553 void *buf, bus_size_t buflen,
554 pmap_t pmap,
555 int flags,
556 bus_addr_t *lastaddrp,
557 bus_dma_segment_t *segs,
558 int *segp,
559 int first)
560 {
561 bus_size_t sgsize;
562 bus_addr_t curaddr, lastaddr, baddr, bmask;
563 vm_offset_t vaddr;
564 bus_addr_t paddr;
565 int needbounce = 0;
566 int seg;
567
568 if (map == NULL)
569 map = &nobounce_dmamap;
570
571 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
572 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
573 vm_offset_t vendaddr;
574
575 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
576 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
577 dmat->boundary, dmat->alignment);
578 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
579 map, &nobounce_dmamap, map->pagesneeded);
580 /*
581 * Count the number of bounce pages
582 * needed in order to complete this transfer
583 */
584 vaddr = trunc_page((vm_offset_t)buf);
585 vendaddr = (vm_offset_t)buf + buflen;
586
587 while (vaddr < vendaddr) {
588 paddr = pmap_kextract(vaddr);
589 if (run_filter(dmat, paddr) != 0) {
590 needbounce = 1;
591 map->pagesneeded++;
592 }
593 vaddr += PAGE_SIZE;
594 }
595 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
596 }
597
598 /* Reserve Necessary Bounce Pages */
599 if (map->pagesneeded != 0) {
600 mtx_lock(&bounce_lock);
601 if (flags & BUS_DMA_NOWAIT) {
602 if (reserve_bounce_pages(dmat, map, 0) != 0) {
603 mtx_unlock(&bounce_lock);
604 return (ENOMEM);
605 }
606 } else {
607 if (reserve_bounce_pages(dmat, map, 1) != 0) {
608 /* Queue us for resources */
609 map->dmat = dmat;
610 map->buf = buf;
611 map->buflen = buflen;
612 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
613 map, links);
614 mtx_unlock(&bounce_lock);
615 return (EINPROGRESS);
616 }
617 }
618 mtx_unlock(&bounce_lock);
619 }
620
621 vaddr = (vm_offset_t)buf;
622 lastaddr = *lastaddrp;
623 bmask = ~(dmat->boundary - 1);
624
625 for (seg = *segp; buflen > 0 ; ) {
626 /*
627 * Get the physical address for this segment.
628 */
629 if (pmap)
630 curaddr = pmap_extract(pmap, vaddr);
631 else
632 curaddr = pmap_kextract(vaddr);
633
634 /*
635 * Compute the segment size, and adjust counts.
636 */
637 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
638 if (buflen < sgsize)
639 sgsize = buflen;
640
641 /*
642 * Make sure we don't cross any boundaries.
643 */
644 if (dmat->boundary > 0) {
645 baddr = (curaddr + dmat->boundary) & bmask;
646 if (sgsize > (baddr - curaddr))
647 sgsize = (baddr - curaddr);
648 }
649
650 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
651 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
652
653 /*
654 * Insert chunk into a segment, coalescing with
655 * previous segment if possible.
656 */
657 if (first) {
658 segs[seg].ds_addr = curaddr;
659 segs[seg].ds_len = sgsize;
660 first = 0;
661 } else {
662 if (needbounce == 0 && curaddr == lastaddr &&
663 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
664 (dmat->boundary == 0 ||
665 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
666 segs[seg].ds_len += sgsize;
667 else {
668 if (++seg >= dmat->nsegments)
669 break;
670 segs[seg].ds_addr = curaddr;
671 segs[seg].ds_len = sgsize;
672 }
673 }
674
675 lastaddr = curaddr + sgsize;
676 vaddr += sgsize;
677 buflen -= sgsize;
678 }
679
680 *segp = seg;
681 *lastaddrp = lastaddr;
682
683 /*
684 * Did we fit?
685 */
686 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
687 }
688
689 /*
690 * Map the buffer buf into bus space using the dmamap map.
691 */
692 int
693 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
694 bus_size_t buflen, bus_dmamap_callback_t *callback,
695 void *callback_arg, int flags)
696 {
697 bus_addr_t lastaddr = 0;
698 int error, nsegs = 0;
699
700 if (map != NULL) {
701 flags |= BUS_DMA_WAITOK;
702 map->callback = callback;
703 map->callback_arg = callback_arg;
704 }
705
706 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
707 &lastaddr, dmat->segments, &nsegs, 1);
708
709 if (error == EINPROGRESS) {
710 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
711 __func__, dmat, dmat->flags, error);
712 return (error);
713 }
714
715 if (error)
716 (*callback)(callback_arg, dmat->segments, 0, error);
717 else
718 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
719
720 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error 0 nsegs %d",
721 __func__, dmat, dmat->flags, nsegs + 1);
722 return (0);
723 }
724
725
726 /*
727 * Like _bus_dmamap_load(), but for mbufs.
728 */
729 int
730 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
731 struct mbuf *m0,
732 bus_dmamap_callback2_t *callback, void *callback_arg,
733 int flags)
734 {
735 int nsegs, error;
736
737 M_ASSERTPKTHDR(m0);
738
739 flags |= BUS_DMA_NOWAIT;
740 nsegs = 0;
741 error = 0;
742 if (m0->m_pkthdr.len <= dmat->maxsize) {
743 int first = 1;
744 bus_addr_t lastaddr = 0;
745 struct mbuf *m;
746
747 for (m = m0; m != NULL && error == 0; m = m->m_next) {
748 if (m->m_len > 0) {
749 error = _bus_dmamap_load_buffer(dmat, map,
750 m->m_data, m->m_len,
751 NULL, flags, &lastaddr,
752 dmat->segments, &nsegs, first);
753 first = 0;
754 }
755 }
756 } else {
757 error = EINVAL;
758 }
759
760 if (error) {
761 /* force "no valid mappings" in callback */
762 (*callback)(callback_arg, dmat->segments, 0, 0, error);
763 } else {
764 (*callback)(callback_arg, dmat->segments,
765 nsegs+1, m0->m_pkthdr.len, error);
766 }
767 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
768 __func__, dmat, dmat->flags, error, nsegs + 1);
769 return (error);
770 }
771
772 int
773 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
774 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
775 int flags)
776 {
777 int error;
778
779 M_ASSERTPKTHDR(m0);
780
781 flags |= BUS_DMA_NOWAIT;
782 *nsegs = 0;
783 error = 0;
784 if (m0->m_pkthdr.len <= dmat->maxsize) {
785 int first = 1;
786 bus_addr_t lastaddr = 0;
787 struct mbuf *m;
788
789 for (m = m0; m != NULL && error == 0; m = m->m_next) {
790 if (m->m_len > 0) {
791 error = _bus_dmamap_load_buffer(dmat, map,
792 m->m_data, m->m_len,
793 NULL, flags, &lastaddr,
794 segs, nsegs, first);
795 first = 0;
796 }
797 }
798 } else {
799 error = EINVAL;
800 }
801
802 /* XXX FIXME: Having to increment nsegs is really annoying */
803 ++*nsegs;
804 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
805 __func__, dmat, dmat->flags, error, *nsegs);
806 return (error);
807 }
808
809 /*
810 * Like _bus_dmamap_load(), but for uios.
811 */
812 int
813 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
814 struct uio *uio,
815 bus_dmamap_callback2_t *callback, void *callback_arg,
816 int flags)
817 {
818 bus_addr_t lastaddr;
819 int nsegs, error, first, i;
820 bus_size_t resid;
821 struct iovec *iov;
822 pmap_t pmap;
823
824 flags |= BUS_DMA_NOWAIT;
825 resid = uio->uio_resid;
826 iov = uio->uio_iov;
827
828 if (uio->uio_segflg == UIO_USERSPACE) {
829 KASSERT(uio->uio_td != NULL,
830 ("bus_dmamap_load_uio: USERSPACE but no proc"));
831 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
832 } else
833 pmap = NULL;
834
835 nsegs = 0;
836 error = 0;
837 first = 1;
838 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
839 /*
840 * Now at the first iovec to load. Load each iovec
841 * until we have exhausted the residual count.
842 */
843 bus_size_t minlen =
844 resid < iov[i].iov_len ? resid : iov[i].iov_len;
845 caddr_t addr = (caddr_t) iov[i].iov_base;
846
847 if (minlen > 0) {
848 error = _bus_dmamap_load_buffer(dmat, map,
849 addr, minlen, pmap, flags, &lastaddr,
850 dmat->segments, &nsegs, first);
851 first = 0;
852
853 resid -= minlen;
854 }
855 }
856
857 if (error) {
858 /* force "no valid mappings" in callback */
859 (*callback)(callback_arg, dmat->segments, 0, 0, error);
860 } else {
861 (*callback)(callback_arg, dmat->segments,
862 nsegs+1, uio->uio_resid, error);
863 }
864 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
865 __func__, dmat, dmat->flags, error, nsegs + 1);
866 return (error);
867 }
868
869 /*
870 * Release the mapping held by map.
871 */
872 void
873 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
874 {
875 struct bounce_page *bpage;
876
877 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
878 STAILQ_REMOVE_HEAD(&map->bpages, links);
879 free_bounce_page(dmat, bpage);
880 }
881 }
882
883 void
884 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
885 {
886 struct bounce_page *bpage;
887
888 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
889 /*
890 * Handle data bouncing. We might also
891 * want to add support for invalidating
892 * the caches on broken hardware
893 */
894 dmat->bounce_zone->total_bounced++;
895 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
896 "performing bounce", __func__, op, dmat, dmat->flags);
897
898 if (op & BUS_DMASYNC_PREWRITE) {
899 while (bpage != NULL) {
900 bcopy((void *)bpage->datavaddr,
901 (void *)bpage->vaddr,
902 bpage->datacount);
903 bpage = STAILQ_NEXT(bpage, links);
904 }
905 }
906
907 if (op & BUS_DMASYNC_POSTREAD) {
908 while (bpage != NULL) {
909 bcopy((void *)bpage->vaddr,
910 (void *)bpage->datavaddr,
911 bpage->datacount);
912 bpage = STAILQ_NEXT(bpage, links);
913 }
914 }
915 }
916 }
917
918 static void
919 init_bounce_pages(void *dummy __unused)
920 {
921
922 total_bpages = 0;
923 STAILQ_INIT(&bounce_zone_list);
924 STAILQ_INIT(&bounce_map_waitinglist);
925 STAILQ_INIT(&bounce_map_callbacklist);
926 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
927 }
928 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
929
930 static struct sysctl_ctx_list *
931 busdma_sysctl_tree(struct bounce_zone *bz)
932 {
933 return (&bz->sysctl_tree);
934 }
935
936 static struct sysctl_oid *
937 busdma_sysctl_tree_top(struct bounce_zone *bz)
938 {
939 return (bz->sysctl_tree_top);
940 }
941
942 static int
943 alloc_bounce_zone(bus_dma_tag_t dmat)
944 {
945 struct bounce_zone *bz;
946
947 /* Check to see if we already have a suitable zone */
948 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
949 if ((dmat->alignment <= bz->alignment)
950 && (dmat->boundary <= bz->boundary)
951 && (dmat->lowaddr >= bz->lowaddr)) {
952 dmat->bounce_zone = bz;
953 return (0);
954 }
955 }
956
957 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
958 M_NOWAIT | M_ZERO)) == NULL)
959 return (ENOMEM);
960
961 STAILQ_INIT(&bz->bounce_page_list);
962 bz->free_bpages = 0;
963 bz->reserved_bpages = 0;
964 bz->active_bpages = 0;
965 bz->lowaddr = dmat->lowaddr;
966 bz->alignment = dmat->alignment;
967 bz->boundary = dmat->boundary;
968 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
969 busdma_zonecount++;
970 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
971 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
972 dmat->bounce_zone = bz;
973
974 sysctl_ctx_init(&bz->sysctl_tree);
975 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
976 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
977 CTLFLAG_RD, 0, "");
978 if (bz->sysctl_tree_top == NULL) {
979 sysctl_ctx_free(&bz->sysctl_tree);
980 return (0); /* XXX error code? */
981 }
982
983 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
984 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
985 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
986 "Total bounce pages");
987 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
988 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
989 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
990 "Free bounce pages");
991 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
992 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
993 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
994 "Reserved bounce pages");
995 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
996 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
997 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
998 "Active bounce pages");
999 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1000 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1001 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1002 "Total bounce requests");
1003 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1004 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1005 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1006 "Total bounce requests that were deferred");
1007 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1008 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1009 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1010 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1011 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1012 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1013 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1014 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1015 "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
1016
1017 return (0);
1018 }
1019
1020 static int
1021 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1022 {
1023 struct bounce_zone *bz;
1024 int count;
1025
1026 bz = dmat->bounce_zone;
1027 count = 0;
1028 while (numpages > 0) {
1029 struct bounce_page *bpage;
1030
1031 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1032 M_NOWAIT | M_ZERO);
1033
1034 if (bpage == NULL)
1035 break;
1036 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1037 M_NOWAIT, 0ul,
1038 bz->lowaddr,
1039 PAGE_SIZE,
1040 bz->boundary);
1041 if (bpage->vaddr == 0) {
1042 free(bpage, M_DEVBUF);
1043 break;
1044 }
1045 bpage->busaddr = pmap_kextract(bpage->vaddr);
1046 mtx_lock(&bounce_lock);
1047 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1048 total_bpages++;
1049 bz->total_bpages++;
1050 bz->free_bpages++;
1051 mtx_unlock(&bounce_lock);
1052 count++;
1053 numpages--;
1054 }
1055 return (count);
1056 }
1057
1058 static int
1059 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1060 {
1061 struct bounce_zone *bz;
1062 int pages;
1063
1064 mtx_assert(&bounce_lock, MA_OWNED);
1065 bz = dmat->bounce_zone;
1066 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1067 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1068 return (map->pagesneeded - (map->pagesreserved + pages));
1069 bz->free_bpages -= pages;
1070 bz->reserved_bpages += pages;
1071 map->pagesreserved += pages;
1072 pages = map->pagesneeded - map->pagesreserved;
1073
1074 return (pages);
1075 }
1076
1077 static bus_addr_t
1078 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1079 bus_size_t size)
1080 {
1081 struct bounce_zone *bz;
1082 struct bounce_page *bpage;
1083
1084 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1085 KASSERT(map != NULL && map != &nobounce_dmamap,
1086 ("add_bounce_page: bad map %p", map));
1087
1088 bz = dmat->bounce_zone;
1089 if (map->pagesneeded == 0)
1090 panic("add_bounce_page: map doesn't need any pages");
1091 map->pagesneeded--;
1092
1093 if (map->pagesreserved == 0)
1094 panic("add_bounce_page: map doesn't need any pages");
1095 map->pagesreserved--;
1096
1097 mtx_lock(&bounce_lock);
1098 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1099 if (bpage == NULL)
1100 panic("add_bounce_page: free page list is empty");
1101
1102 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1103 bz->reserved_bpages--;
1104 bz->active_bpages++;
1105 mtx_unlock(&bounce_lock);
1106
1107 bpage->datavaddr = vaddr;
1108 bpage->datacount = size;
1109 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1110 return (bpage->busaddr);
1111 }
1112
1113 static void
1114 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1115 {
1116 struct bus_dmamap *map;
1117 struct bounce_zone *bz;
1118
1119 bz = dmat->bounce_zone;
1120 bpage->datavaddr = 0;
1121 bpage->datacount = 0;
1122
1123 mtx_lock(&bounce_lock);
1124 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1125 bz->free_bpages++;
1126 bz->active_bpages--;
1127 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1128 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1129 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1130 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1131 map, links);
1132 busdma_swi_pending = 1;
1133 bz->total_deferred++;
1134 swi_sched(vm_ih, 0);
1135 }
1136 }
1137 mtx_unlock(&bounce_lock);
1138 }
1139
1140 void
1141 busdma_swi(void)
1142 {
1143 bus_dma_tag_t dmat;
1144 struct bus_dmamap *map;
1145
1146 mtx_lock(&bounce_lock);
1147 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1148 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1149 mtx_unlock(&bounce_lock);
1150 dmat = map->dmat;
1151 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1152 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1153 map->callback, map->callback_arg, /*flags*/0);
1154 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1155 mtx_lock(&bounce_lock);
1156 }
1157 mtx_unlock(&bounce_lock);
1158 }
Cache object: 6f60d3417d1b7785c85fb2c94f9f5668
|