busdma: Make sure bounce zone's sysctl tree is created before destroying it
[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
MD
141static struct lwkt_token bounce_zone_tok =
142 LWKT_TOKEN_MP_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 }
5efcff53 483 if (!error)
984263bc 484 dmat->map_count++;
5efcff53 485 return error;
984263bc
MD
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);
efda3bd0 498 kfree(map, M_DEVBUF);
984263bc
MD
499 }
500 dmat->map_count--;
501 return (0);
502}
503
46fbe6c5
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) {
e94a9b8c
SZ
511 if (verify || bootverbose)
512 kprintf("boundary check failed\n");
46fbe6c5 513 if (verify)
7ce2998e 514 print_backtrace(-1); /* XXX panic */
46fbe6c5
SZ
515 maxsize = dmat->maxsize;
516 }
517 if (vaddr & (dmat->alignment - 1)) {
e94a9b8c
SZ
518 if (verify || bootverbose)
519 kprintf("alignment check failed\n");
46fbe6c5 520 if (verify)
7ce2998e 521 print_backtrace(-1); /* XXX panic */
46fbe6c5
SZ
522 if (dmat->maxsize < dmat->alignment)
523 maxsize = dmat->alignment;
524 else
525 maxsize = dmat->maxsize;
526 }
527 return maxsize;
528}
529
984263bc
MD
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.
ff8e0d00
MD
533 *
534 * mapp is degenerate. By definition this allocation should not require
535 * bounce buffers so do not allocate a dma map.
984263bc
MD
536 */
537int
46fbe6c5 538bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
984263bc
MD
539 bus_dmamap_t *mapp)
540{
ba0fefd4 541 int mflags;
141ebb24 542
984263bc
MD
543 /* If we succeed, no mapping/bouncing will be required */
544 *mapp = NULL;
545
3a2114c4
MD
546 if (dmat->segments == NULL) {
547 KKASSERT(dmat->nsegments < 16384);
77652cad 548 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
3a2114c4
MD
549 dmat->nsegments, M_DEVBUF, M_INTWAIT);
550 }
551
ba0fefd4
JS
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
46fbe6c5
SZ
559 if (BUS_DMAMEM_KMALLOC(dmat)) {
560 bus_size_t maxsize;
561
efda3bd0 562 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
46fbe6c5 563
68aa5c00 564 /*
46fbe6c5
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.
68aa5c00 570 */
46fbe6c5
SZ
571 maxsize = check_kmalloc(dmat, *vaddr, 0);
572 if (maxsize) {
68aa5c00 573 size_t size;
9d9878b7 574
efda3bd0 575 kfree(*vaddr, M_DEVBUF);
68aa5c00 576 /* XXX check for overflow? */
46fbe6c5 577 for (size = 1; size <= maxsize; size <<= 1)
68aa5c00 578 ;
efda3bd0 579 *vaddr = kmalloc(size, M_DEVBUF, mflags);
46fbe6c5 580 check_kmalloc(dmat, *vaddr, 1);
68aa5c00 581 }
984263bc
MD
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 */
ba0fefd4 588 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
24d9d29b 589 0ul, dmat->lowaddr, dmat->alignment, dmat->boundary);
984263bc
MD
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");
46fbe6c5 609 if (BUS_DMAMEM_KMALLOC(dmat))
efda3bd0 610 kfree(vaddr, M_DEVBUF);
984263bc
MD
611 else
612 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
613}
614
070a7c5f
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}
623
624/*
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.
630 */
b2436634 631static int
1884d7c3 632_bus_dmamap_load_buffer(bus_dma_tag_t dmat,
b2436634
SZ
633 bus_dmamap_t map,
634 void *buf, bus_size_t buflen,
aa2d9ae8
SZ
635 bus_dma_segment_t *segments,
636 int nsegments,
070a7c5f 637 pmap_t pmap,
b2436634
SZ
638 int flags,
639 vm_paddr_t *lastpaddrp,
640 int *segp,
641 int first)
984263bc 642{
9d9878b7
SZ
643 vm_offset_t vaddr;
644 vm_paddr_t paddr, nextpaddr;
645 bus_dma_segment_t *sg;
646 bus_addr_t bmask;
b2436634 647 int seg, error = 0;
984263bc
MD
648
649 if (map == NULL)
650 map = &nobounce_dmamap;
651
46fbe6c5
SZ
652#ifdef INVARIANTS
653 if (dmat->flags & BUS_DMA_ALIGNED)
654 KKASSERT(((uintptr_t)buf & (dmat->alignment - 1)) == 0);
655#endif
656
984263bc
MD
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 */
e7029048 661 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) &&
60405791 662 map != &nobounce_dmamap && map->pagesneeded == 0) {
9d9878b7 663 vm_offset_t vendaddr;
984263bc
MD
664
665 /*
666 * Count the number of bounce pages
667 * needed in order to complete this transfer
668 */
60405791 669 vaddr = (vm_offset_t)buf;
984263bc
MD
670 vendaddr = (vm_offset_t)buf + buflen;
671
672 while (vaddr < vendaddr) {
070a7c5f 673 paddr = _bus_dma_extract(pmap, vaddr);
141ebb24 674 if (run_filter(dmat, paddr) != 0)
984263bc 675 map->pagesneeded++;
60405791 676 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
984263bc
MD
677 }
678 }
679
680 /* Reserve Necessary Bounce Pages */
681 if (map->pagesneeded != 0) {
78c79c21
SZ
682 struct bounce_zone *bz;
683
684 bz = dmat->bounce_zone;
685 BZ_LOCK(bz);
46cbb173
SZ
686 if (flags & BUS_DMA_NOWAIT) {
687 if (reserve_bounce_pages(dmat, map, 0) != 0) {
78c79c21 688 BZ_UNLOCK(bz);
eda6a747
SZ
689 error = ENOMEM;
690 goto free_bounce;
46cbb173
SZ
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;
984263bc 698
eec44d94
SZ
699 STAILQ_INSERT_TAIL(
700 &dmat->bounce_zone->bounce_map_waitinglist,
701 map, links);
78c79c21 702 BZ_UNLOCK(bz);
984263bc 703
46cbb173
SZ
704 return (EINPROGRESS);
705 }
984263bc 706 }
78c79c21 707 BZ_UNLOCK(bz);
984263bc
MD
708 }
709
aa2d9ae8 710 KKASSERT(*segp >= 1 && *segp <= nsegments);
b2436634 711 seg = *segp;
aa2d9ae8 712 sg = &segments[seg - 1];
b2436634 713
984263bc 714 vaddr = (vm_offset_t)buf;
b2436634 715 nextpaddr = *lastpaddrp;
270d2794
MD
716 bmask = ~(dmat->boundary - 1); /* note: will be 0 if boundary is 0 */
717
718 /* force at least one segment */
984263bc 719 do {
9d9878b7 720 bus_size_t size;
984263bc 721
270d2794
MD
722 /*
723 * Per-page main loop
724 */
070a7c5f 725 paddr = _bus_dma_extract(pmap, vaddr);
984263bc
MD
726 size = PAGE_SIZE - (paddr & PAGE_MASK);
727 if (size > buflen)
728 size = buflen;
984263bc 729 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
b2436634 730 /*
270d2794
MD
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 */
984263bc
MD
735 paddr = add_bounce_page(dmat, map, vaddr, size);
736 }
737
270d2794
MD
738 /*
739 * Fill in the bus_dma_segment
740 */
b2436634 741 if (first) {
984263bc
MD
742 sg->ds_addr = paddr;
743 sg->ds_len = size;
b2436634 744 first = 0;
984263bc
MD
745 } else if (paddr == nextpaddr) {
746 sg->ds_len += size;
747 } else {
984263bc
MD
748 sg++;
749 seg++;
aa2d9ae8 750 if (seg > nsegments)
984263bc
MD
751 break;
752 sg->ds_addr = paddr;
753 sg->ds_len = size;
754 }
984263bc 755 nextpaddr = paddr + size;
270d2794
MD
756
757 /*
758 * Handle maxsegsz and boundary issues with a nested loop
759 */
760 for (;;) {
9d9878b7 761 bus_size_t tmpsize;
270d2794
MD
762
763 /*
764 * Limit to the boundary and maximum segment size
765 */
6b02d9de 766 if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
270d2794
MD
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 */
aa2d9ae8 781 if (seg >= nsegments)
270d2794
MD
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 */
984263bc 793 buflen -= size;
270d2794 794 vaddr += size;
984263bc 795 } while (buflen > 0);
270d2794 796fail:
65cf4e39 797 if (buflen != 0)
984263bc 798 error = EFBIG;
984263bc 799
b2436634
SZ
800 *segp = seg;
801 *lastpaddrp = nextpaddr;
984263bc 802
eda6a747 803free_bounce:
65cf4e39
SZ
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 }
b2436634
SZ
809 return error;
810}
811
812/*
813 * Map the buffer buf into bus space using the dmamap map.
814 */
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)
819{
6eed46e7
MD
820 bus_dma_segment_t cache_segments[BUS_DMA_CACHE_SEGMENTS];
821 bus_dma_segment_t *segments;
b2436634
SZ
822 vm_paddr_t lastaddr = 0;
823 int error, nsegs = 1;
824
825 if (map != NULL) {
46cbb173
SZ
826 /*
827 * XXX
828 * Follow old semantics. Once all of the callers are fixed,
829 * we should get rid of these internal flag "adjustment".
830 */
831 flags &= ~BUS_DMA_NOWAIT;
b2436634 832 flags |= BUS_DMA_WAITOK;
46cbb173 833
b2436634
SZ
834 map->callback = callback;
835 map->callback_arg = callback_arg;
836 }
837
6eed46e7 838 segments = bus_dma_tag_lock(dmat, cache_segments);
1884d7c3 839 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen,
6eed46e7 840 segments, dmat->nsegments,
1884d7c3 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);
b2436634 851 return error;
6eed46e7
MD
852 }
853 callback(callback_arg, segments, nsegs, error);
854 bus_dma_tag_unlock(dmat);
b2436634 855 return 0;
984263bc
MD
856}
857
984263bc
MD
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;
984263bc
MD
869 int nsegs, error;
870
18d561ae
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;
984263bc 878
6eed46e7 879 segments = bus_dma_tag_lock(dmat, cache_segments);
aa2d9ae8 880 error = bus_dmamap_load_mbuf_segment(dmat, map, m0,
6eed46e7 881 segments, dmat->nsegments, &nsegs, flags);
aa2d9ae8
SZ
882 if (error) {
883 /* force "no valid mappings" in callback */
6eed46e7
MD
884 callback(callback_arg, segments, 0,
885 0, error);
aa2d9ae8 886 } else {
6eed46e7 887 callback(callback_arg, segments, nsegs,
aa2d9ae8
SZ
888 m0->m_pkthdr.len, error);
889 }
6eed46e7 890 bus_dma_tag_unlock(dmat);
aa2d9ae8
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"));
910
984263bc
MD
911 if (m0->m_pkthdr.len <= dmat->maxsize) {
912 int first = 1;
18d561ae 913 vm_paddr_t lastaddr = 0;
984263bc
MD
914 struct mbuf *m;
915
aa2d9ae8 916 *nsegs = 1;
18d561ae 917 error = 0;
984263bc 918 for (m = m0; m != NULL && error == 0; m = m->m_next) {
18d561ae 919 if (m->m_len == 0)
485dcc96 920 continue;
18d561ae 921
1884d7c3 922 error = _bus_dmamap_load_buffer(dmat, map,
984263bc 923 m->m_data, m->m_len,
aa2d9ae8 924 segs, maxsegs,
18d561ae 925 NULL, flags, &lastaddr,
aa2d9ae8 926 nsegs, first);
eda6a747
SZ
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 }
984263bc
MD
935 first = 0;
936 }
aa2d9ae8
SZ
937#ifdef INVARIANTS
938 if (!error)
939 KKASSERT(*nsegs <= maxsegs && *nsegs >= 1);
940#endif
984263bc 941 } else {
aa2d9ae8 942 *nsegs = 0;
984263bc
MD
943 error = EINVAL;
944 }
aa2d9ae8 945 KKASSERT(error != EINPROGRESS);
18d561ae 946 return error;
984263bc
MD
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{
070a7c5f 958 vm_paddr_t lastaddr;
984263bc
MD
959 int nsegs, error, first, i;
960 bus_size_t resid;
961 struct iovec *iov;
070a7c5f 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);
984263bc 973
070a7c5f
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;
984263bc 981
e54488bb 982 resid = (bus_size_t)uio->uio_resid;
984263bc
MD
983 iov = uio->uio_iov;
984
6eed46e7
MD
985 segs = segments;
986 nsegs_left = dmat->nsegments;
987
984263bc 988 if (uio->uio_segflg == UIO_USERSPACE) {
070a7c5f
SZ
989 struct thread *td;
990
dadab5e9
MD
991 td = uio->uio_td;
992 KASSERT(td != NULL && td->td_proc != NULL,
984263bc 993 ("bus_dmamap_load_uio: USERSPACE but no proc"));
070a7c5f
SZ
994 pmap = vmspace_pmap(td->td_proc->p_vmspace);
995 } else {
996 pmap = NULL;
984263bc
MD
997 }
998
984263bc 999 error = 0;
070a7c5f 1000 nsegs = 1;
984263bc 1001 first = 1;
070a7c5f 1002 lastaddr = 0;
984263bc
MD
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
1884d7c3 1012 error = _bus_dmamap_load_buffer(dmat, map, addr, minlen,
6eed46e7 1013 segs, nsegs_left,
070a7c5f 1014 pmap, flags, &lastaddr, &nsegs, first);
984263bc
MD
1015 first = 0;
1016
1017 resid -= minlen;
6eed46e7
MD
1018 if (error == 0) {
1019 nsegs_left -= nsegs;
1020 segs += nsegs;
1021 }
984263bc
MD
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
984263bc
MD
1030 if (error) {
1031 /* force "no valid mappings" in callback */
bc2cd3f9
MD
1032 callback(callback_arg, segments, 0,
1033 0, error);
984263bc 1034 } else {
6eed46e7 1035 callback(callback_arg, segments, dmat->nsegments - nsegs_left,
e54488bb 1036 (bus_size_t)uio->uio_resid, error);
984263bc 1037 }
6eed46e7
MD
1038 if (dmat->nsegments > BUS_DMA_CACHE_SEGMENTS)
1039 kfree(segments, M_DEVBUF);
070a7c5f 1040 return error;
984263bc
MD
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) {
984263bc
MD
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 }
5efcff53 1076 dmat->bounce_zone->total_bounced++;
984263bc
MD
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 }
5efcff53 1086 dmat->bounce_zone->total_bounced++;
984263bc 1087 break;
141ebb24 1088
984263bc
MD
1089 case BUS_DMASYNC_PREREAD:
1090 case BUS_DMASYNC_POSTWRITE:
1091 /* No-ops */
1092 break;
1093 }
1094 }
1095}
1096
1097static int
5efcff53 1098alloc_bounce_zone(bus_dma_tag_t dmat)
984263bc 1099{
5a162f17 1100 struct bounce_zone *bz, *new_bz;
5efcff53
SZ
1101
1102 KASSERT(dmat->bounce_zone == NULL,
1103 ("bounce zone was already assigned\n"));
1104
5a162f17
SZ
1105 new_bz = kmalloc(sizeof(*new_bz), M_DEVBUF, M_INTWAIT | M_ZERO);
1106
3b998fa9 1107 lwkt_gettoken(&bounce_zone_tok);
5a162f17 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 }
5efcff53
SZ
1123 }
1124 }
5a162f17 1125 bz = new_bz;
5efcff53 1126
78c79c21
SZ
1127#ifdef SMP
1128 spin_init(&bz->spin);
1129#endif
5efcff53 1130 STAILQ_INIT(&bz->bounce_page_list);
eec44d94 1131 STAILQ_INIT(&bz->bounce_map_waitinglist);
5efcff53
SZ
1132 bz->free_bpages = 0;
1133 bz->reserved_bpages = 0;
1134 bz->active_bpages = 0;
1135 bz->lowaddr = dmat->lowaddr;
f3f55019 1136 bz->alignment = round_page(dmat->alignment);
5efcff53 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 }
5a162f17 1147
3b998fa9 1148 lwkt_reltoken(&bounce_zone_tok);
5a162f17 1149
5efcff53
SZ
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? */
984263bc 1159 }
141ebb24 1160
5efcff53
SZ
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");
63fbdfd2
SZ
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");
5efcff53
SZ
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, "");
5efcff53
SZ
1195
1196 return 0;
1197}
1198
1199static int
f3f55019 1200alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages, int flags)
5efcff53
SZ
1201{
1202 struct bounce_zone *bz = dmat->bounce_zone;
f3f55019
SZ
1203 int count = 0, mflags;
1204
1205 if (flags & BUS_DMA_NOWAIT)
1206 mflags = M_NOWAIT;
1207 else
1208 mflags = M_WAITOK;
5efcff53 1209
984263bc
MD
1210 while (numpages > 0) {
1211 struct bounce_page *bpage;
984263bc 1212
58d9c764 1213 bpage = kmalloc(sizeof(*bpage), M_DEVBUF, M_INTWAIT | M_ZERO);
984263bc 1214
984263bc 1215 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
f3f55019
SZ
1216 mflags, 0ul,
1217 bz->lowaddr,
ca611aa8 1218 bz->alignment, 0);
3641b7ca 1219 if (bpage->vaddr == 0) {
efda3bd0 1220 kfree(bpage, M_DEVBUF);
984263bc
MD
1221 break;
1222 }
1223 bpage->busaddr = pmap_kextract(bpage->vaddr);
5efcff53 1224
78c79c21 1225 BZ_LOCK(bz);
5efcff53
SZ
1226 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1227 total_bounce_pages++;
1228 bz->total_bpages++;
1229 bz->free_bpages++;
78c79c21 1230 BZ_UNLOCK(bz);
5efcff53 1231
984263bc
MD
1232 count++;
1233 numpages--;
1234 }
5efcff53 1235 return count;
984263bc
MD
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
65cf4e39 1289/* Assume caller holds bounce zone spinlock */
984263bc 1290static int
46cbb173 1291reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
984263bc 1292{
5efcff53 1293 struct bounce_zone *bz = dmat->bounce_zone;
984263bc
MD
1294 int pages;
1295
5efcff53 1296 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
63fbdfd2
SZ
1297 if (!commit && map->pagesneeded > (map->pagesreserved + pages)) {
1298 bz->reserve_failed++;
46cbb173 1299 return (map->pagesneeded - (map->pagesreserved + pages));
63fbdfd2 1300 }
ad196c46 1301
5efcff53 1302 bz->free_bpages -= pages;
ad196c46 1303
5efcff53 1304 bz->reserved_bpages += pages;
ad196c46
SZ
1305 KKASSERT(bz->reserved_bpages <= bz->total_bpages);
1306
984263bc
MD
1307 map->pagesreserved += pages;
1308 pages = map->pagesneeded - map->pagesreserved;
1309
5efcff53 1310 return pages;
984263bc
MD
1311}
1312
65cf4e39
SZ
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);
ad196c46 1327
65cf4e39 1328 bz->free_bpages += reserved;
ad196c46
SZ
1329 KKASSERT(bz->free_bpages <= bz->total_bpages);
1330
1331 KKASSERT(bz->reserved_bpages >= reserved);
65cf4e39 1332 bz->reserved_bpages -= reserved;
ad196c46 1333
65cf4e39 1334 wait_map = get_map_waiting(dmat);
ad196c46 1335
65cf4e39
SZ
1336 BZ_UNLOCK(bz);
1337
1338 if (wait_map != NULL)
1339 add_map_callback(map);
1340}
1341
6ef943a3 1342static bus_addr_t
984263bc
MD
1343add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1344 bus_size_t size)
1345{
5efcff53 1346 struct bounce_zone *bz = dmat->bounce_zone;
984263bc
MD
1347 struct bounce_page *bpage;
1348
ad196c46 1349 KASSERT(map->pagesneeded > 0, ("map doesn't need any pages"));
984263bc
MD
1350 map->pagesneeded--;
1351
ad196c46 1352 KASSERT(map->pagesreserved > 0, ("map doesn't reserve any pages"));
984263bc
MD
1353 map->pagesreserved--;
1354
78c79c21 1355 BZ_LOCK(bz);
984263bc 1356
ad196c46
SZ
1357 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1358 KASSERT(bpage != NULL, ("free page list is empty"));
5efcff53 1359 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
ad196c46
SZ
1360
1361 KKASSERT(bz->reserved_bpages > 0);
5efcff53 1362 bz->reserved_bpages--;
ad196c46 1363
5efcff53 1364 bz->active_bpages++;
ad196c46
SZ
1365 KKASSERT(bz->active_bpages <= bz->total_bpages);
1366
78c79c21 1367 BZ_UNLOCK(bz);
984263bc
MD
1368
1369 bpage->datavaddr = vaddr;
1370 bpage->datacount = size;
5efcff53
SZ
1371 STAILQ_INSERT_TAIL(&map->bpages, bpage, links);
1372 return bpage->busaddr;
984263bc
MD
1373}
1374
1375static void
1376free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1377{
5efcff53 1378 struct bounce_zone *bz = dmat->bounce_zone;
65cf4e39 1379 bus_dmamap_t map;
984263bc
MD
1380
1381 bpage->datavaddr = 0;
1382 bpage->datacount = 0;
1383
78c79c21 1384 BZ_LOCK(bz);
ad196c46 1385
5efcff53 1386 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
ad196c46 1387
5efcff53 1388 bz->free_bpages++;
ad196c46
SZ
1389 KKASSERT(bz->free_bpages <= bz->total_bpages);
1390
1391 KKASSERT(bz->active_bpages > 0);
5efcff53 1392 bz->active_bpages--;
ad196c46 1393
65cf4e39 1394 map = get_map_waiting(dmat);
ad196c46 1395
65cf4e39
SZ
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) {
46cbb173 1411 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
eec44d94 1412 STAILQ_REMOVE_HEAD(&bz->bounce_map_waitinglist, links);
5efcff53 1413 bz->total_deferred++;
78c79c21
SZ
1414 } else {
1415 map = NULL;
984263bc
MD
1416 }
1417 }
65cf4e39
SZ
1418 return map;
1419}
78c79c21 1420
65cf4e39
SZ
1421static void
1422add_map_callback(bus_dmamap_t map)
1423{
0d4f82f8 1424 spin_lock(&bounce_map_list_spin);
65cf4e39
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);
984263bc
MD
1429}
1430
1431void
f123d5a1 1432busdma_swi(void)
984263bc 1433{
65cf4e39 1434 bus_dmamap_t map;
984263bc 1435
0d4f82f8 1436 spin_lock(&bounce_map_list_spin);
984263bc
MD
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);
984263bc
MD
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);
984263bc 1443 }
0d4f82f8 1444 spin_unlock(&bounce_map_list_spin);
984263bc 1445}