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