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