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