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