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