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