bus_dma_tag fixes and enhancements.
[dragonfly.git] / sys / platform / pc32 / i386 / busdma_machdep.c
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  * $FreeBSD: src/sys/i386/i386/busdma_machdep.c,v 1.94 2008/08/15 20:51:31 kmacy Exp $
27  * $DragonFly: src/sys/platform/pc32/i386/busdma_machdep.c,v 1.23 2008/06/05 18:06:32 swildner Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/uio.h>
35 #include <sys/thread2.h>
36 #include <sys/bus_dma.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/lock.h>
40 #include <sys/spinlock2.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_page.h>
44
45 /* XXX needed for to access pmap to convert per-proc virtual to physical */
46 #include <sys/proc.h>
47 #include <sys/lock.h>
48 #include <vm/vm_map.h>
49
50 #include <machine/md_var.h>
51
52 #define MAX_BPAGES      1024
53
54 /*
55  * 16 x N declared on stack.
56  */
57 #define BUS_DMA_CACHE_SEGMENTS  8
58
59 struct bounce_zone;
60 struct bus_dmamap;
61
62 struct bus_dma_tag {
63         bus_dma_tag_t   parent;
64         bus_size_t      alignment;
65         bus_size_t      boundary;
66         bus_addr_t      lowaddr;
67         bus_addr_t      highaddr;
68         bus_dma_filter_t *filter;
69         void            *filterarg;
70         bus_size_t      maxsize;
71         u_int           nsegments;
72         bus_size_t      maxsegsz;
73         int             flags;
74         int             ref_count;
75         int             map_count;
76         bus_dma_segment_t *segments;
77         struct bounce_zone *bounce_zone;
78 #ifdef SMP
79         struct spinlock spin;
80 #else
81         int             unused0;
82 #endif
83 };
84
85 /*
86  * bus_dma_tag private flags
87  */
88 #define BUS_DMA_BOUNCE_ALIGN    BUS_DMA_BUS2
89 #define BUS_DMA_BOUNCE_LOWADDR  BUS_DMA_BUS3
90 #define BUS_DMA_MIN_ALLOC_COMP  BUS_DMA_BUS4
91
92 #define BUS_DMA_COULD_BOUNCE    (BUS_DMA_BOUNCE_LOWADDR | BUS_DMA_BOUNCE_ALIGN)
93
94 #define BUS_DMAMEM_KMALLOC(dmat) \
95         ((dmat)->maxsize <= PAGE_SIZE && \
96          (dmat)->alignment <= PAGE_SIZE && \
97          (dmat)->lowaddr >= ptoa(Maxmem))
98
99 struct bounce_page {
100         vm_offset_t     vaddr;          /* kva of bounce buffer */
101         bus_addr_t      busaddr;        /* Physical address */
102         vm_offset_t     datavaddr;      /* kva of client data */
103         bus_size_t      datacount;      /* client data count */
104         STAILQ_ENTRY(bounce_page) links;
105 };
106
107 struct bounce_zone {
108         STAILQ_ENTRY(bounce_zone) links;
109         STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
110         STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
111 #ifdef SMP
112         struct spinlock spin;
113 #else
114         int             unused0;
115 #endif
116         int             total_bpages;
117         int             free_bpages;
118         int             reserved_bpages;
119         int             active_bpages;
120         int             total_bounced;
121         int             total_deferred;
122         int             reserve_failed;
123         bus_size_t      alignment;
124         bus_addr_t      lowaddr;
125         char            zoneid[8];
126         char            lowaddrid[20];
127         struct sysctl_ctx_list sysctl_ctx;
128         struct sysctl_oid *sysctl_tree;
129 };
130
131 #ifdef SMP
132 #define BZ_LOCK(bz)     spin_lock_wr(&(bz)->spin)
133 #define BZ_UNLOCK(bz)   spin_unlock_wr(&(bz)->spin)
134 #else
135 #define BZ_LOCK(bz)     crit_enter()
136 #define BZ_UNLOCK(bz)   crit_exit()
137 #endif
138
139 static struct lwkt_token bounce_zone_tok =
140         LWKT_TOKEN_INITIALIZER(bounce_zone_tok);
141 static int busdma_zonecount;
142 static STAILQ_HEAD(, bounce_zone) bounce_zone_list =
143         STAILQ_HEAD_INITIALIZER(bounce_zone_list);
144
145 int busdma_swi_pending;
146 static int total_bounce_pages;
147 static int max_bounce_pages = MAX_BPAGES;
148 static int bounce_alignment = 1; /* XXX temporary */
149
150 TUNABLE_INT("hw.busdma.max_bpages", &max_bounce_pages);
151 TUNABLE_INT("hw.busdma.bounce_alignment", &bounce_alignment);
152
153 struct bus_dmamap {
154         struct bp_list  bpages;
155         int             pagesneeded;
156         int             pagesreserved;
157         bus_dma_tag_t   dmat;
158         void            *buf;           /* unmapped buffer pointer */
159         bus_size_t      buflen;         /* unmapped buffer length */
160         bus_dmamap_callback_t *callback;
161         void            *callback_arg;
162         STAILQ_ENTRY(bus_dmamap) links;
163 };
164
165 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist =
166         STAILQ_HEAD_INITIALIZER(bounce_map_callbacklist);
167
168 static struct bus_dmamap nobounce_dmamap;
169
170 static int              alloc_bounce_zone(bus_dma_tag_t);
171 static int              alloc_bounce_pages(bus_dma_tag_t, u_int, int);
172 static int              reserve_bounce_pages(bus_dma_tag_t, bus_dmamap_t, int);
173 static void             return_bounce_pages(bus_dma_tag_t, bus_dmamap_t);
174 static bus_addr_t       add_bounce_page(bus_dma_tag_t, bus_dmamap_t,
175                             vm_offset_t, bus_size_t);
176 static void             free_bounce_page(bus_dma_tag_t, struct bounce_page *);
177
178 static bus_dmamap_t     get_map_waiting(bus_dma_tag_t);
179 static void             add_map_callback(bus_dmamap_t);
180
181 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
182 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bounce_pages,
183            0, "Total bounce pages");
184 SYSCTL_INT(_hw_busdma, OID_AUTO, max_bpages, CTLFLAG_RD, &max_bounce_pages,
185            0, "Max bounce pages per bounce zone");
186 SYSCTL_INT(_hw_busdma, OID_AUTO, bounce_alignment, CTLFLAG_RD,
187            &bounce_alignment, 0, "Obey alignment constraint");
188
189 static __inline int
190 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
191 {
192         int retval;
193
194         retval = 0;
195         do {
196                 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr) ||
197                      (bounce_alignment && (paddr & (dmat->alignment - 1)) != 0))
198                  && (dmat->filter == NULL ||
199                      dmat->filter(dmat->filterarg, paddr) != 0))
200                         retval = 1;
201
202                 dmat = dmat->parent;
203         } while (retval == 0 && dmat != NULL);
204         return (retval);
205 }
206
207 static __inline
208 bus_dma_segment_t *
209 bus_dma_tag_lock(bus_dma_tag_t tag, bus_dma_segment_t *cache)
210 {
211         if (tag->nsegments <= BUS_DMA_CACHE_SEGMENTS)
212                 return(cache);
213 #ifdef SMP
214         spin_lock_wr(&tag->spin);
215 #endif
216         return(tag->segments);
217 }
218
219 static __inline
220 void
221 bus_dma_tag_unlock(bus_dma_tag_t tag)
222 {
223 #ifdef SMP
224         if (tag->nsegments > BUS_DMA_CACHE_SEGMENTS)
225                 spin_unlock_wr(&tag->spin);
226 #endif
227 }
228
229 /*
230  * Allocate a device specific dma_tag.
231  */
232 int
233 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
234                    bus_size_t boundary, bus_addr_t lowaddr,
235                    bus_addr_t highaddr, bus_dma_filter_t *filter,
236                    void *filterarg, bus_size_t maxsize, int nsegments,
237                    bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
238 {
239         bus_dma_tag_t newtag;
240         int error = 0;
241
242         /*
243          * Sanity checks
244          */
245
246         if (alignment == 0)
247                 alignment = 1;
248         if (alignment & (alignment - 1))
249                 panic("alignment must be power of 2\n");
250
251         if (boundary != 0) {
252                 if (boundary & (boundary - 1))
253                         panic("boundary must be power of 2\n");
254                 if (boundary < maxsegsz) {
255                         kprintf("boundary < maxsegsz:\n");
256                         print_backtrace();
257                         maxsegsz = boundary;
258                 }
259         }
260
261         /* Return a NULL tag on failure */
262         *dmat = NULL;
263
264         newtag = kmalloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT | M_ZERO);
265
266         spin_init(&newtag->spin);
267         newtag->parent = parent;
268         newtag->alignment = alignment;
269         newtag->boundary = boundary;
270         newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
271         newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
272         newtag->filter = filter;
273         newtag->filterarg = filterarg;
274         newtag->maxsize = maxsize;
275         newtag->nsegments = nsegments;
276         newtag->maxsegsz = maxsegsz;
277         newtag->flags = flags;
278         newtag->ref_count = 1; /* Count ourself */
279         newtag->map_count = 0;
280         newtag->segments = NULL;
281         newtag->bounce_zone = NULL;
282
283         /* Take into account any restrictions imposed by our parent tag */
284         if (parent != NULL) {
285                 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
286                 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
287
288                 if (newtag->boundary == 0) {
289                         newtag->boundary = parent->boundary;
290                 } else if (parent->boundary != 0) {
291                         newtag->boundary = MIN(parent->boundary,
292                                                newtag->boundary);
293                 }
294
295 #ifdef notyet
296                 newtag->alignment = MAX(parent->alignment, newtag->alignment);
297 #endif
298
299                 if (newtag->filter == NULL) {
300                         /*
301                          * Short circuit looking at our parent directly
302                          * since we have encapsulated all of its information
303                          */
304                         newtag->filter = parent->filter;
305                         newtag->filterarg = parent->filterarg;
306                         newtag->parent = parent->parent;
307                 }
308                 if (newtag->parent != NULL)
309                         parent->ref_count++;
310         }
311
312         if (newtag->lowaddr < ptoa(Maxmem))
313                 newtag->flags |= BUS_DMA_BOUNCE_LOWADDR;
314         if (bounce_alignment && newtag->alignment > 1 &&
315             !(newtag->flags & BUS_DMA_ALIGNED))
316                 newtag->flags |= BUS_DMA_BOUNCE_ALIGN;
317
318         if ((newtag->flags & BUS_DMA_COULD_BOUNCE) &&
319             (flags & BUS_DMA_ALLOCNOW) != 0) {
320                 struct bounce_zone *bz;
321
322                 /* Must bounce */
323
324                 error = alloc_bounce_zone(newtag);
325                 if (error)
326                         goto back;
327                 bz = newtag->bounce_zone;
328
329                 if (ptoa(bz->total_bpages) < maxsize) {
330                         int pages;
331
332                         if (flags & BUS_DMA_ONEBPAGE) {
333                                 pages = 1;
334                         } else {
335                                 pages = atop(round_page(maxsize)) -
336                                         bz->total_bpages;
337                                 pages = MAX(pages, 1);
338                         }
339
340                         /* Add pages to our bounce pool */
341                         if (alloc_bounce_pages(newtag, pages, flags) < pages)
342                                 error = ENOMEM;
343
344                         /* Performed initial allocation */
345                         newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
346                 }
347         }
348 back:
349         if (error)
350                 kfree(newtag, M_DEVBUF);
351         else
352                 *dmat = newtag;
353         return error;
354 }
355
356 int
357 bus_dma_tag_destroy(bus_dma_tag_t dmat)
358 {
359         if (dmat != NULL) {
360                 if (dmat->map_count != 0)
361                         return (EBUSY);
362
363                 while (dmat != NULL) {
364                         bus_dma_tag_t parent;
365
366                         parent = dmat->parent;
367                         dmat->ref_count--;
368                         if (dmat->ref_count == 0) {
369                                 if (dmat->segments != NULL)
370                                         kfree(dmat->segments, M_DEVBUF);
371                                 kfree(dmat, M_DEVBUF);
372                                 /*
373                                  * Last reference count, so
374                                  * release our reference
375                                  * count on our parent.
376                                  */
377                                 dmat = parent;
378                         } else
379                                 dmat = NULL;
380                 }
381         }
382         return (0);
383 }
384
385 bus_size_t
386 bus_dma_tag_getmaxsize(bus_dma_tag_t tag)
387 {
388         return(tag->maxsize);
389 }
390
391 /*
392  * Allocate a handle for mapping from kva/uva/physical
393  * address space into bus device space.
394  */
395 int
396 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
397 {
398         int error;
399
400         error = 0;
401
402         if (dmat->segments == NULL) {
403                 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
404                 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) * 
405                                         dmat->nsegments, M_DEVBUF, M_INTWAIT);
406         }
407
408         if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
409                 struct bounce_zone *bz;
410                 int maxpages;
411
412                 /* Must bounce */
413
414                 if (dmat->bounce_zone == NULL) {
415                         error = alloc_bounce_zone(dmat);
416                         if (error)
417                                 return error;
418                 }
419                 bz = dmat->bounce_zone;
420
421                 *mapp = kmalloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT | M_ZERO);
422
423                 /* Initialize the new map */
424                 STAILQ_INIT(&((*mapp)->bpages));
425
426                 /*
427                  * Attempt to add pages to our pool on a per-instance
428                  * basis up to a sane limit.
429                  */
430                 if (dmat->flags & BUS_DMA_BOUNCE_ALIGN) {
431                         maxpages = max_bounce_pages;
432                 } else {
433                         maxpages = MIN(max_bounce_pages,
434                                        Maxmem - atop(dmat->lowaddr));
435                 }
436                 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
437                  || (dmat->map_count > 0
438                   && bz->total_bpages < maxpages)) {
439                         int pages;
440
441                         if (flags & BUS_DMA_ONEBPAGE) {
442                                 pages = 1;
443                         } else {
444                                 pages = atop(round_page(dmat->maxsize));
445                                 pages = MIN(maxpages - bz->total_bpages, pages);
446                                 pages = MAX(pages, 1);
447                         }
448                         if (alloc_bounce_pages(dmat, pages, flags) < pages)
449                                 error = ENOMEM;
450
451                         if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
452                                 if (!error)
453                                         dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
454                         } else {
455                                 error = 0;
456                         }
457                 }
458         } else {
459                 *mapp = NULL;
460         }
461         if (!error)
462                 dmat->map_count++;
463         return error;
464 }
465
466 /*
467  * Destroy a handle for mapping from kva/uva/physical
468  * address space into bus device space.
469  */
470 int
471 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
472 {
473         if (map != NULL) {
474                 if (STAILQ_FIRST(&map->bpages) != NULL)
475                         return (EBUSY);
476                 kfree(map, M_DEVBUF);
477         }
478         dmat->map_count--;
479         return (0);
480 }
481
482 static __inline bus_size_t
483 check_kmalloc(bus_dma_tag_t dmat, const void *vaddr0, int verify)
484 {
485         bus_size_t maxsize = 0;
486         uintptr_t vaddr = (uintptr_t)vaddr0;
487
488         if ((vaddr ^ (vaddr + dmat->maxsize - 1)) & ~PAGE_MASK) {
489                 kprintf("boundary check failed\n");
490                 if (verify)
491                         print_backtrace(); /* XXX panic */
492                 maxsize = dmat->maxsize;
493         }
494         if (vaddr & (dmat->alignment - 1)) {
495                 kprintf("alignment check failed\n");
496                 if (verify)
497                         print_backtrace(); /* XXX panic */
498                 if (dmat->maxsize < dmat->alignment)
499                         maxsize = dmat->alignment;
500                 else
501                         maxsize = dmat->maxsize;
502         }
503         return maxsize;
504 }
505
506 /*
507  * Allocate a piece of memory that can be efficiently mapped into
508  * bus device space based on the constraints lited in the dma tag.
509  *
510  * mapp is degenerate.  By definition this allocation should not require
511  * bounce buffers so do not allocate a dma map.
512  */
513 int
514 bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
515                  bus_dmamap_t *mapp)
516 {
517         int mflags;
518
519         /* If we succeed, no mapping/bouncing will be required */
520         *mapp = NULL;
521
522         if (dmat->segments == NULL) {
523                 KKASSERT(dmat->nsegments < 16384);
524                 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) * 
525                                         dmat->nsegments, M_DEVBUF, M_INTWAIT);
526         }
527
528         if (flags & BUS_DMA_NOWAIT)
529                 mflags = M_NOWAIT;
530         else
531                 mflags = M_WAITOK;
532         if (flags & BUS_DMA_ZERO)
533                 mflags |= M_ZERO;
534
535         if (BUS_DMAMEM_KMALLOC(dmat)) {
536                 bus_size_t maxsize;
537
538                 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
539
540                 /*
541                  * XXX
542                  * Check whether the allocation
543                  * - crossed a page boundary
544                  * - was not aligned
545                  * Retry with power-of-2 alignment in the above cases.
546                  */
547                 maxsize = check_kmalloc(dmat, *vaddr, 0);
548                 if (maxsize) {
549                         size_t size;
550
551                         kfree(*vaddr, M_DEVBUF);
552                         /* XXX check for overflow? */
553                         for (size = 1; size <= maxsize; size <<= 1)
554                                 ;
555                         *vaddr = kmalloc(size, M_DEVBUF, mflags);
556                         check_kmalloc(dmat, *vaddr, 1);
557                 }
558         } else {
559                 /*
560                  * XXX Use Contigmalloc until it is merged into this facility
561                  *     and handles multi-seg allocations.  Nobody is doing
562                  *     multi-seg allocations yet though.
563                  */
564                 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
565                     0ul, dmat->lowaddr, dmat->alignment, dmat->boundary);
566         }
567         if (*vaddr == NULL)
568                 return (ENOMEM);
569         return (0);
570 }
571
572 /*
573  * Free a piece of memory and it's allociated dmamap, that was allocated
574  * via bus_dmamem_alloc.  Make the same choice for free/contigfree.
575  */
576 void
577 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
578 {
579         /*
580          * dmamem does not need to be bounced, so the map should be
581          * NULL
582          */
583         if (map != NULL)
584                 panic("bus_dmamem_free: Invalid map freed\n");
585         if (BUS_DMAMEM_KMALLOC(dmat))
586                 kfree(vaddr, M_DEVBUF);
587         else
588                 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
589 }
590
591 static __inline vm_paddr_t
592 _bus_dma_extract(pmap_t pmap, vm_offset_t vaddr)
593 {
594         if (pmap)
595                 return pmap_extract(pmap, vaddr);
596         else
597                 return pmap_kextract(vaddr);
598 }
599
600 /*
601  * Utility function to load a linear buffer.  lastaddrp holds state
602  * between invocations (for multiple-buffer loads).  segp contains
603  * the segment following the starting one on entrace, and the ending
604  * segment on exit.  first indicates if this is the first invocation
605  * of this function.
606  */
607 static int
608 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
609                         bus_dmamap_t map,
610                         void *buf, bus_size_t buflen,
611                         bus_dma_segment_t *segments,
612                         int nsegments,
613                         pmap_t pmap,
614                         int flags,
615                         vm_paddr_t *lastpaddrp,
616                         int *segp,
617                         int first)
618 {
619         vm_offset_t vaddr;
620         vm_paddr_t paddr, nextpaddr;
621         bus_dma_segment_t *sg;
622         bus_addr_t bmask;
623         int seg, error = 0;
624
625         if (map == NULL)
626                 map = &nobounce_dmamap;
627
628 #ifdef INVARIANTS
629         if (dmat->flags & BUS_DMA_ALIGNED)
630                 KKASSERT(((uintptr_t)buf & (dmat->alignment - 1)) == 0);
631 #endif
632
633         /*
634          * If we are being called during a callback, pagesneeded will
635          * be non-zero, so we can avoid doing the work twice.
636          */
637         if ((dmat->flags & BUS_DMA_COULD_BOUNCE) &&
638             map != &nobounce_dmamap && map->pagesneeded == 0) {
639                 vm_offset_t vendaddr;
640
641                 /*
642                  * Count the number of bounce pages
643                  * needed in order to complete this transfer
644                  */
645                 vaddr = (vm_offset_t)buf;
646                 vendaddr = (vm_offset_t)buf + buflen;
647
648                 while (vaddr < vendaddr) {
649                         paddr = _bus_dma_extract(pmap, vaddr);
650                         if (run_filter(dmat, paddr) != 0)
651                                 map->pagesneeded++;
652                         vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
653                 }
654         }
655
656         /* Reserve Necessary Bounce Pages */
657         if (map->pagesneeded != 0) {
658                 struct bounce_zone *bz;
659
660                 bz = dmat->bounce_zone;
661                 BZ_LOCK(bz);
662                 if (flags & BUS_DMA_NOWAIT) {
663                         if (reserve_bounce_pages(dmat, map, 0) != 0) {
664                                 BZ_UNLOCK(bz);
665                                 error = ENOMEM;
666                                 goto free_bounce;
667                         }
668                 } else {
669                         if (reserve_bounce_pages(dmat, map, 1) != 0) {
670                                 /* Queue us for resources */
671                                 map->dmat = dmat;
672                                 map->buf = buf;
673                                 map->buflen = buflen;
674
675                                 STAILQ_INSERT_TAIL(
676                                     &dmat->bounce_zone->bounce_map_waitinglist,
677                                     map, links);
678                                 BZ_UNLOCK(bz);
679
680                                 return (EINPROGRESS);
681                         }
682                 }
683                 BZ_UNLOCK(bz);
684         }
685
686         KKASSERT(*segp >= 1 && *segp <= nsegments);
687         seg = *segp;
688         sg = &segments[seg - 1];
689
690         vaddr = (vm_offset_t)buf;
691         nextpaddr = *lastpaddrp;
692         bmask = ~(dmat->boundary - 1);  /* note: will be 0 if boundary is 0 */
693
694         /* force at least one segment */
695         do {
696                 bus_size_t size;
697
698                 /*
699                  * Per-page main loop
700                  */
701                 paddr = _bus_dma_extract(pmap, vaddr);
702                 size = PAGE_SIZE - (paddr & PAGE_MASK);
703                 if (size > buflen)
704                         size = buflen;
705                 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
706                         /*
707                          * note: this paddr has the same in-page offset
708                          * as vaddr and thus the paddr above, so the
709                          * size does not have to be recalculated
710                          */
711                         paddr = add_bounce_page(dmat, map, vaddr, size);
712                 }
713
714                 /*
715                  * Fill in the bus_dma_segment
716                  */
717                 if (first) {
718                         sg->ds_addr = paddr;
719                         sg->ds_len = size;
720                         first = 0;
721                 } else if (paddr == nextpaddr) {
722                         sg->ds_len += size;
723                 } else {
724                         sg++;
725                         seg++;
726                         if (seg > nsegments)
727                                 break;
728                         sg->ds_addr = paddr;
729                         sg->ds_len = size;
730                 }
731                 nextpaddr = paddr + size;
732
733                 /*
734                  * Handle maxsegsz and boundary issues with a nested loop
735                  */
736                 for (;;) {
737                         bus_size_t tmpsize;
738
739                         /*
740                          * Limit to the boundary and maximum segment size
741                          */
742                         if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
743                                 tmpsize = dmat->boundary -
744                                           (sg->ds_addr & ~bmask);
745                                 if (tmpsize > dmat->maxsegsz)
746                                         tmpsize = dmat->maxsegsz;
747                                 KKASSERT(tmpsize < sg->ds_len);
748                         } else if (sg->ds_len > dmat->maxsegsz) {
749                                 tmpsize = dmat->maxsegsz;
750                         } else {
751                                 break;
752                         }
753
754                         /*
755                          * Futz, split the data into a new segment.
756                          */
757                         if (seg >= nsegments)
758                                 goto fail;
759                         sg[1].ds_len = sg[0].ds_len - tmpsize;
760                         sg[1].ds_addr = sg[0].ds_addr + tmpsize;
761                         sg[0].ds_len = tmpsize;
762                         sg++;
763                         seg++;
764                 }
765
766                 /*
767                  * Adjust for loop
768                  */
769                 buflen -= size;
770                 vaddr += size;
771         } while (buflen > 0);
772 fail:
773         if (buflen != 0)
774                 error = EFBIG;
775
776         *segp = seg;
777         *lastpaddrp = nextpaddr;
778
779 free_bounce:
780         if (error && (dmat->flags & BUS_DMA_COULD_BOUNCE) &&
781             map != &nobounce_dmamap) {
782                 _bus_dmamap_unload(dmat, map);
783                 return_bounce_pages(dmat, map);
784         }
785         return error;
786 }
787
788 /*
789  * Map the buffer buf into bus space using the dmamap map.
790  */
791 int
792 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
793                 bus_size_t buflen, bus_dmamap_callback_t *callback,
794                 void *callback_arg, int flags)
795 {
796         bus_dma_segment_t cache_segments[BUS_DMA_CACHE_SEGMENTS];
797         bus_dma_segment_t *segments;
798         vm_paddr_t lastaddr = 0;
799         int error, nsegs = 1;
800
801         if (map != NULL) {
802                 /*
803                  * XXX
804                  * Follow old semantics.  Once all of the callers are fixed,
805                  * we should get rid of these internal flag "adjustment".
806                  */
807                 flags &= ~BUS_DMA_NOWAIT;
808                 flags |= BUS_DMA_WAITOK;
809
810                 map->callback = callback;
811                 map->callback_arg = callback_arg;
812         }
813
814         segments = bus_dma_tag_lock(dmat, cache_segments);
815         error = _bus_dmamap_load_buffer(dmat, map, buf, buflen,
816                         segments, dmat->nsegments,
817                         NULL, flags, &lastaddr, &nsegs, 1);
818         if (error == EINPROGRESS) {
819                 bus_dma_tag_unlock(dmat);
820                 return error;
821         }
822         callback(callback_arg, segments, nsegs, error);
823         bus_dma_tag_unlock(dmat);
824         return 0;
825 }
826
827 /*
828  * Like _bus_dmamap_load(), but for mbufs.
829  */
830 int
831 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
832                      struct mbuf *m0,
833                      bus_dmamap_callback2_t *callback, void *callback_arg,
834                      int flags)
835 {
836         bus_dma_segment_t cache_segments[BUS_DMA_CACHE_SEGMENTS];
837         bus_dma_segment_t *segments;
838         int nsegs, error;
839
840         /*
841          * XXX
842          * Follow old semantics.  Once all of the callers are fixed,
843          * we should get rid of these internal flag "adjustment".
844          */
845         flags &= ~BUS_DMA_WAITOK;
846         flags |= BUS_DMA_NOWAIT;
847
848         segments = bus_dma_tag_lock(dmat, cache_segments);
849         error = bus_dmamap_load_mbuf_segment(dmat, map, m0,
850                         segments, dmat->nsegments, &nsegs, flags);
851         if (error) {
852                 /* force "no valid mappings" in callback */
853                 callback(callback_arg, segments, 0,
854                          0, error);
855         } else {
856                 callback(callback_arg, segments, nsegs,
857                          m0->m_pkthdr.len, error);
858         }
859         bus_dma_tag_unlock(dmat);
860         return error;
861 }
862
863 int
864 bus_dmamap_load_mbuf_segment(bus_dma_tag_t dmat, bus_dmamap_t map,
865                              struct mbuf *m0,
866                              bus_dma_segment_t *segs, int maxsegs,
867                              int *nsegs, int flags)
868 {
869         int error;
870
871         M_ASSERTPKTHDR(m0);
872
873         KASSERT(maxsegs >= 1, ("invalid maxsegs %d\n", maxsegs));
874         KASSERT(maxsegs <= dmat->nsegments,
875                 ("%d too many segments, dmat only support %d segments\n",
876                  maxsegs, dmat->nsegments));
877         KASSERT(flags & BUS_DMA_NOWAIT,
878                 ("only BUS_DMA_NOWAIT is supported\n"));
879
880         if (m0->m_pkthdr.len <= dmat->maxsize) {
881                 int first = 1;
882                 vm_paddr_t lastaddr = 0;
883                 struct mbuf *m;
884
885                 *nsegs = 1;
886                 error = 0;
887                 for (m = m0; m != NULL && error == 0; m = m->m_next) {
888                         if (m->m_len == 0)
889                                 continue;
890
891                         error = _bus_dmamap_load_buffer(dmat, map,
892                                         m->m_data, m->m_len,
893                                         segs, maxsegs,
894                                         NULL, flags, &lastaddr,
895                                         nsegs, first);
896                         if (error == ENOMEM && !first) {
897                                 /*
898                                  * Out of bounce pages due to too many
899                                  * fragments in the mbuf chain; return
900                                  * EFBIG instead.
901                                  */
902                                 error = EFBIG;
903                         }
904                         first = 0;
905                 }
906 #ifdef INVARIANTS
907                 if (!error)
908                         KKASSERT(*nsegs <= maxsegs && *nsegs >= 1);
909 #endif
910         } else {
911                 *nsegs = 0;
912                 error = EINVAL;
913         }
914         KKASSERT(error != EINPROGRESS);
915         return error;
916 }
917
918 /*
919  * Like _bus_dmamap_load(), but for uios.
920  */
921 int
922 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
923                     struct uio *uio,
924                     bus_dmamap_callback2_t *callback, void *callback_arg,
925                     int flags)
926 {
927         vm_paddr_t lastaddr;
928         int nsegs, error, first, i;
929         bus_size_t resid;
930         struct iovec *iov;
931         pmap_t pmap;
932         bus_dma_segment_t cache_segments[BUS_DMA_CACHE_SEGMENTS];
933         bus_dma_segment_t *segments;
934         bus_dma_segment_t *segs;
935         int nsegs_left;
936
937         if (dmat->nsegments <= BUS_DMA_CACHE_SEGMENTS)
938                 segments = cache_segments;
939         else
940                 segments = kmalloc(sizeof(bus_dma_segment_t) * dmat->nsegments,
941                                    M_DEVBUF, M_WAITOK | M_ZERO);
942
943         /*
944          * XXX
945          * Follow old semantics.  Once all of the callers are fixed,
946          * we should get rid of these internal flag "adjustment".
947          */
948         flags &= ~BUS_DMA_WAITOK;
949         flags |= BUS_DMA_NOWAIT;
950
951         resid = uio->uio_resid;
952         iov = uio->uio_iov;
953
954         segs = segments;
955         nsegs_left = dmat->nsegments;
956
957         if (uio->uio_segflg == UIO_USERSPACE) {
958                 struct thread *td;
959
960                 td = uio->uio_td;
961                 KASSERT(td != NULL && td->td_proc != NULL,
962                         ("bus_dmamap_load_uio: USERSPACE but no proc"));
963                 pmap = vmspace_pmap(td->td_proc->p_vmspace);
964         } else {
965                 pmap = NULL;
966         }
967
968         error = 0;
969         nsegs = 1;
970         first = 1;
971         lastaddr = 0;
972         for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
973                 /*
974                  * Now at the first iovec to load.  Load each iovec
975                  * until we have exhausted the residual count.
976                  */
977                 bus_size_t minlen =
978                         resid < iov[i].iov_len ? resid : iov[i].iov_len;
979                 caddr_t addr = (caddr_t) iov[i].iov_base;
980
981                 error = _bus_dmamap_load_buffer(dmat, map, addr, minlen,
982                                 segs, nsegs_left,
983                                 pmap, flags, &lastaddr, &nsegs, first);
984                 first = 0;
985
986                 resid -= minlen;
987                 if (error == 0) {
988                         nsegs_left -= nsegs;
989                         segs += nsegs;
990                 }
991         }
992
993         /*
994          * Minimum one DMA segment, even if 0-length buffer.
995          */
996         if (nsegs_left == dmat->nsegments)
997                 --nsegs_left;
998
999         if (error) {
1000                 /* force "no valid mappings" in callback */
1001                 callback(callback_arg, segments, 0, 0, error);
1002         } else {
1003                 callback(callback_arg, segments, dmat->nsegments - nsegs_left,
1004                          uio->uio_resid, error);
1005         }
1006         if (dmat->nsegments > BUS_DMA_CACHE_SEGMENTS)
1007                 kfree(segments, M_DEVBUF);
1008         return error;
1009 }
1010
1011 /*
1012  * Release the mapping held by map.
1013  */
1014 void
1015 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1016 {
1017         struct bounce_page *bpage;
1018
1019         while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1020                 STAILQ_REMOVE_HEAD(&map->bpages, links);
1021                 free_bounce_page(dmat, bpage);
1022         }
1023 }
1024
1025 void
1026 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1027 {
1028         struct bounce_page *bpage;
1029
1030         if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1031                 /*
1032                  * Handle data bouncing.  We might also
1033                  * want to add support for invalidating
1034                  * the caches on broken hardware
1035                  */
1036                 switch (op) {
1037                 case BUS_DMASYNC_PREWRITE:
1038                         while (bpage != NULL) {
1039                                 bcopy((void *)bpage->datavaddr,
1040                                       (void *)bpage->vaddr,
1041                                       bpage->datacount);
1042                                 bpage = STAILQ_NEXT(bpage, links);
1043                         }
1044                         dmat->bounce_zone->total_bounced++;
1045                         break;
1046
1047                 case BUS_DMASYNC_POSTREAD:
1048                         while (bpage != NULL) {
1049                                 bcopy((void *)bpage->vaddr,
1050                                       (void *)bpage->datavaddr,
1051                                       bpage->datacount);
1052                                 bpage = STAILQ_NEXT(bpage, links);
1053                         }
1054                         dmat->bounce_zone->total_bounced++;
1055                         break;
1056
1057                 case BUS_DMASYNC_PREREAD:
1058                 case BUS_DMASYNC_POSTWRITE:
1059                         /* No-ops */
1060                         break;
1061                 }
1062         }
1063 }
1064
1065 static int
1066 alloc_bounce_zone(bus_dma_tag_t dmat)
1067 {
1068         struct bounce_zone *bz, *new_bz;
1069         lwkt_tokref ref;
1070
1071         KASSERT(dmat->bounce_zone == NULL,
1072                 ("bounce zone was already assigned\n"));
1073
1074         new_bz = kmalloc(sizeof(*new_bz), M_DEVBUF, M_INTWAIT | M_ZERO);
1075
1076         lwkt_gettoken(&ref, &bounce_zone_tok);
1077
1078         /* Check to see if we already have a suitable zone */
1079         STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1080                 if (dmat->alignment <= bz->alignment &&
1081                     dmat->lowaddr >= bz->lowaddr) {
1082                         lwkt_reltoken(&ref);
1083
1084                         dmat->bounce_zone = bz;
1085                         kfree(new_bz, M_DEVBUF);
1086                         return 0;
1087                 }
1088         }
1089         bz = new_bz;
1090
1091 #ifdef SMP
1092         spin_init(&bz->spin);
1093 #endif
1094         STAILQ_INIT(&bz->bounce_page_list);
1095         STAILQ_INIT(&bz->bounce_map_waitinglist);
1096         bz->free_bpages = 0;
1097         bz->reserved_bpages = 0;
1098         bz->active_bpages = 0;
1099         bz->lowaddr = dmat->lowaddr;
1100         bz->alignment = round_page(dmat->alignment);
1101         ksnprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1102         busdma_zonecount++;
1103         ksnprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1104         STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1105
1106         lwkt_reltoken(&ref);
1107
1108         dmat->bounce_zone = bz;
1109
1110         sysctl_ctx_init(&bz->sysctl_ctx);
1111         bz->sysctl_tree = SYSCTL_ADD_NODE(&bz->sysctl_ctx,
1112             SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1113             CTLFLAG_RD, 0, "");
1114         if (bz->sysctl_tree == NULL) {
1115                 sysctl_ctx_free(&bz->sysctl_ctx);
1116                 return 0;       /* XXX error code? */
1117         }
1118
1119         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1120             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1121             "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1122             "Total bounce pages");
1123         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1124             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1125             "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1126             "Free bounce pages");
1127         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1128             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1129             "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1130             "Reserved bounce pages");
1131         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1132             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1133             "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1134             "Active bounce pages");
1135         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1136             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1137             "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1138             "Total bounce requests");
1139         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1140             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1141             "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1142             "Total bounce requests that were deferred");
1143         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1144             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1145             "reserve_failed", CTLFLAG_RD, &bz->reserve_failed, 0,
1146             "Total bounce page reservations that were failed");
1147         SYSCTL_ADD_STRING(&bz->sysctl_ctx,
1148             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1149             "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1150         SYSCTL_ADD_INT(&bz->sysctl_ctx,
1151             SYSCTL_CHILDREN(bz->sysctl_tree), OID_AUTO,
1152             "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1153
1154         return 0;
1155 }
1156
1157 static int
1158 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages, int flags)
1159 {
1160         struct bounce_zone *bz = dmat->bounce_zone;
1161         int count = 0, mflags;
1162
1163         if (flags & BUS_DMA_NOWAIT)
1164                 mflags = M_NOWAIT;
1165         else
1166                 mflags = M_WAITOK;
1167
1168         while (numpages > 0) {
1169                 struct bounce_page *bpage;
1170
1171                 bpage = kmalloc(sizeof(*bpage), M_DEVBUF, M_INTWAIT | M_ZERO);
1172
1173                 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1174                                                          mflags, 0ul,
1175                                                          bz->lowaddr,
1176                                                          bz->alignment, 0);
1177                 if (bpage->vaddr == 0) {
1178                         kfree(bpage, M_DEVBUF);
1179                         break;
1180                 }
1181                 bpage->busaddr = pmap_kextract(bpage->vaddr);
1182
1183                 BZ_LOCK(bz);
1184                 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1185                 total_bounce_pages++;
1186                 bz->total_bpages++;
1187                 bz->free_bpages++;
1188                 BZ_UNLOCK(bz);
1189
1190                 count++;
1191                 numpages--;
1192         }
1193         return count;
1194 }
1195
1196 /* Assume caller holds bounce zone spinlock */
1197 static int
1198 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1199 {
1200         struct bounce_zone *bz = dmat->bounce_zone;
1201         int pages;
1202
1203         pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1204         if (!commit && map->pagesneeded > (map->pagesreserved + pages)) {
1205                 bz->reserve_failed++;
1206                 return (map->pagesneeded - (map->pagesreserved + pages));
1207         }
1208
1209         bz->free_bpages -= pages;
1210
1211         bz->reserved_bpages += pages;
1212         KKASSERT(bz->reserved_bpages <= bz->total_bpages);
1213
1214         map->pagesreserved += pages;
1215         pages = map->pagesneeded - map->pagesreserved;
1216
1217         return pages;
1218 }
1219
1220 static void
1221 return_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
1222 {
1223         struct bounce_zone *bz = dmat->bounce_zone;
1224         int reserved = map->pagesreserved;
1225         bus_dmamap_t wait_map;
1226
1227         map->pagesreserved = 0;
1228         map->pagesneeded = 0;
1229
1230         if (reserved == 0)
1231                 return;
1232
1233         BZ_LOCK(bz);
1234
1235         bz->free_bpages += reserved;
1236         KKASSERT(bz->free_bpages <= bz->total_bpages);
1237
1238         KKASSERT(bz->reserved_bpages >= reserved);
1239         bz->reserved_bpages -= reserved;
1240
1241         wait_map = get_map_waiting(dmat);
1242
1243         BZ_UNLOCK(bz);
1244
1245         if (wait_map != NULL)
1246                 add_map_callback(map);
1247 }
1248
1249 static bus_addr_t
1250 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1251                 bus_size_t size)
1252 {
1253         struct bounce_zone *bz = dmat->bounce_zone;
1254         struct bounce_page *bpage;
1255
1256         KASSERT(map->pagesneeded > 0, ("map doesn't need any pages"));
1257         map->pagesneeded--;
1258
1259         KASSERT(map->pagesreserved > 0, ("map doesn't reserve any pages"));
1260         map->pagesreserved--;
1261
1262         BZ_LOCK(bz);
1263
1264         bpage = STAILQ_FIRST(&bz->bounce_page_list);
1265         KASSERT(bpage != NULL, ("free page list is empty"));
1266         STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1267
1268         KKASSERT(bz->reserved_bpages > 0);
1269         bz->reserved_bpages--;
1270
1271         bz->active_bpages++;
1272         KKASSERT(bz->active_bpages <= bz->total_bpages);
1273
1274         BZ_UNLOCK(bz);
1275
1276         bpage->datavaddr = vaddr;
1277         bpage->datacount = size;
1278         STAILQ_INSERT_TAIL(&map->bpages, bpage, links);
1279         return bpage->busaddr;
1280 }
1281
1282 static void
1283 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1284 {
1285         struct bounce_zone *bz = dmat->bounce_zone;
1286         bus_dmamap_t map;
1287
1288         bpage->datavaddr = 0;
1289         bpage->datacount = 0;
1290
1291         BZ_LOCK(bz);
1292
1293         STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1294
1295         bz->free_bpages++;
1296         KKASSERT(bz->free_bpages <= bz->total_bpages);
1297
1298         KKASSERT(bz->active_bpages > 0);
1299         bz->active_bpages--;
1300
1301         map = get_map_waiting(dmat);
1302
1303         BZ_UNLOCK(bz);
1304
1305         if (map != NULL)
1306                 add_map_callback(map);
1307 }
1308
1309 /* Assume caller holds bounce zone spinlock */
1310 static bus_dmamap_t
1311 get_map_waiting(bus_dma_tag_t dmat)
1312 {
1313         struct bounce_zone *bz = dmat->bounce_zone;
1314         bus_dmamap_t map;
1315
1316         map = STAILQ_FIRST(&bz->bounce_map_waitinglist);
1317         if (map != NULL) {
1318                 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1319                         STAILQ_REMOVE_HEAD(&bz->bounce_map_waitinglist, links);
1320                         bz->total_deferred++;
1321                 } else {
1322                         map = NULL;
1323                 }
1324         }
1325         return map;
1326 }
1327
1328 static void
1329 add_map_callback(bus_dmamap_t map)
1330 {
1331         /* XXX callbacklist is not MPSAFE */
1332         crit_enter();
1333         get_mplock();
1334         STAILQ_INSERT_TAIL(&bounce_map_callbacklist, map, links);
1335         busdma_swi_pending = 1;
1336         setsoftvm();
1337         rel_mplock();
1338         crit_exit();
1339 }
1340
1341 void
1342 busdma_swi(void)
1343 {
1344         bus_dmamap_t map;
1345
1346         crit_enter();
1347         while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1348                 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1349                 crit_exit();
1350                 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1351                                 map->callback, map->callback_arg, /*flags*/0);
1352                 crit_enter();
1353         }
1354         crit_exit();
1355 }