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