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