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