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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/kdb.h>
32 #include <ddb/ddb.h>
33 #include <ddb/db_output.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/bus.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/proc.h>
42 #include <sys/mutex.h>
43 #include <sys/mbuf.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
50
51 #include <machine/atomic.h>
52 #include <machine/bus.h>
53 #include <machine/md_var.h>
54 #include <machine/specialreg.h>
55
56 #define MAX_BPAGES 512
57
58 struct bounce_zone;
59
60 struct bus_dma_tag {
61 bus_dma_tag_t parent;
62 bus_size_t alignment;
63 bus_size_t boundary;
64 bus_addr_t lowaddr;
65 bus_addr_t highaddr;
66 bus_dma_filter_t *filter;
67 void *filterarg;
68 bus_size_t maxsize;
69 u_int nsegments;
70 bus_size_t maxsegsz;
71 int flags;
72 int ref_count;
73 int map_count;
74 bus_dma_lock_t *lockfunc;
75 void *lockfuncarg;
76 bus_dma_segment_t *segments;
77 struct bounce_zone *bounce_zone;
78 };
79
80 struct bounce_page {
81 vm_offset_t vaddr; /* kva of bounce buffer */
82 bus_addr_t busaddr; /* Physical address */
83 vm_offset_t datavaddr; /* kva of client data */
84 bus_size_t datacount; /* client data count */
85 STAILQ_ENTRY(bounce_page) links;
86 };
87
88 int busdma_swi_pending;
89
90 struct bounce_zone {
91 STAILQ_ENTRY(bounce_zone) links;
92 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
93 int total_bpages;
94 int free_bpages;
95 int reserved_bpages;
96 int active_bpages;
97 int total_bounced;
98 int total_deferred;
99 bus_size_t alignment;
100 bus_size_t boundary;
101 bus_addr_t lowaddr;
102 char zoneid[8];
103 char lowaddrid[20];
104 struct sysctl_ctx_list sysctl_tree;
105 struct sysctl_oid *sysctl_tree_top;
106 };
107
108 static struct mtx bounce_lock;
109 static int total_bpages;
110 static int busdma_zonecount;
111 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
112
113 SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters");
114 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
115 "Total bounce pages");
116
117 struct bus_dmamap {
118 struct bp_list bpages;
119 int pagesneeded;
120 int pagesreserved;
121 bus_dma_tag_t dmat;
122 void *buf; /* unmapped buffer pointer */
123 bus_size_t buflen; /* unmapped buffer length */
124 bus_dmamap_callback_t *callback;
125 void *callback_arg;
126 STAILQ_ENTRY(bus_dmamap) links;
127 };
128
129 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
130 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
131 static struct bus_dmamap nobounce_dmamap;
132
133 static void init_bounce_pages(void *dummy);
134 static int alloc_bounce_zone(bus_dma_tag_t dmat);
135 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
136 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
137 int commit);
138 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
139 vm_offset_t vaddr, bus_size_t size);
140 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
141 static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
142
143 /*
144 * Return true if a match is made.
145 *
146 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
147 *
148 * If paddr is within the bounds of the dma tag then call the filter callback
149 * to check for a match, if there is no filter callback then assume a match.
150 */
151 static __inline int
152 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
153 {
154 int retval;
155
156 retval = 0;
157
158 do {
159 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
160 || ((paddr & (dmat->alignment - 1)) != 0))
161 && (dmat->filter == NULL
162 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
163 retval = 1;
164
165 dmat = dmat->parent;
166 } while (retval == 0 && dmat != NULL);
167 return (retval);
168 }
169
170 /*
171 * Convenience function for manipulating driver locks from busdma (during
172 * busdma_swi, for example). Drivers that don't provide their own locks
173 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own
174 * non-mutex locking scheme don't have to use this at all.
175 */
176 void
177 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
178 {
179 struct mtx *dmtx;
180
181 dmtx = (struct mtx *)arg;
182 switch (op) {
183 case BUS_DMA_LOCK:
184 mtx_lock(dmtx);
185 break;
186 case BUS_DMA_UNLOCK:
187 mtx_unlock(dmtx);
188 break;
189 default:
190 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
191 }
192 }
193
194 /*
195 * dflt_lock should never get called. It gets put into the dma tag when
196 * lockfunc == NULL, which is only valid if the maps that are associated
197 * with the tag are meant to never be defered.
198 * XXX Should have a way to identify which driver is responsible here.
199 */
200 static void
201 dflt_lock(void *arg, bus_dma_lock_op_t op)
202 {
203 panic("driver error: busdma dflt_lock called");
204 }
205
206 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
207 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
208 /*
209 * Allocate a device specific dma_tag.
210 */
211 int
212 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
213 bus_size_t boundary, bus_addr_t lowaddr,
214 bus_addr_t highaddr, bus_dma_filter_t *filter,
215 void *filterarg, bus_size_t maxsize, int nsegments,
216 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
217 void *lockfuncarg, bus_dma_tag_t *dmat)
218 {
219 bus_dma_tag_t newtag;
220 int error = 0;
221
222 #if defined(PAE)
223 /* Need at least a 4GB boundary, PAE limitations require 2GB */
224 if (boundary == 0 || boundary > ((bus_addr_t)1 << 31))
225 boundary = (bus_size_t)1 << 31;
226 #endif
227
228 /* Basic sanity checking */
229 if (boundary != 0 && boundary < maxsegsz)
230 maxsegsz = boundary;
231
232 /* Return a NULL tag on failure */
233 *dmat = NULL;
234
235 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF,
236 M_ZERO | M_NOWAIT);
237 if (newtag == NULL) {
238 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
239 __func__, newtag, 0, error);
240 return (ENOMEM);
241 }
242
243 newtag->parent = parent;
244 newtag->alignment = alignment;
245 newtag->boundary = boundary;
246 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
247 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) +
248 (PAGE_SIZE - 1);
249 newtag->filter = filter;
250 newtag->filterarg = filterarg;
251 newtag->maxsize = maxsize;
252 newtag->nsegments = nsegments;
253 newtag->maxsegsz = maxsegsz;
254 newtag->flags = flags;
255 newtag->ref_count = 1; /* Count ourself */
256 newtag->map_count = 0;
257 if (lockfunc != NULL) {
258 newtag->lockfunc = lockfunc;
259 newtag->lockfuncarg = lockfuncarg;
260 } else {
261 newtag->lockfunc = dflt_lock;
262 newtag->lockfuncarg = NULL;
263 }
264 newtag->segments = NULL;
265
266 /* Take into account any restrictions imposed by our parent tag */
267 if (parent != NULL) {
268 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
269 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
270 if (newtag->boundary == 0)
271 newtag->boundary = parent->boundary;
272 else if (parent->boundary != 0)
273 newtag->boundary = MIN(parent->boundary,
274 newtag->boundary);
275 if (newtag->filter == NULL) {
276 /*
277 * Short circuit looking at our parent directly
278 * since we have encapsulated all of its information
279 */
280 newtag->filter = parent->filter;
281 newtag->filterarg = parent->filterarg;
282 newtag->parent = parent->parent;
283 }
284 if (newtag->parent != NULL)
285 atomic_add_int(&parent->ref_count, 1);
286 }
287
288 if (newtag->lowaddr < ptoa((vm_paddr_t)Maxmem)
289 || newtag->alignment > 1)
290 newtag->flags |= BUS_DMA_COULD_BOUNCE;
291
292 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
293 (flags & BUS_DMA_ALLOCNOW) != 0) {
294 struct bounce_zone *bz;
295
296 /* Must bounce */
297
298 if ((error = alloc_bounce_zone(newtag)) != 0) {
299 free(newtag, M_DEVBUF);
300 return (error);
301 }
302 bz = newtag->bounce_zone;
303
304 if (ptoa(bz->total_bpages) < maxsize) {
305 int pages;
306
307 pages = atop(maxsize) - bz->total_bpages;
308
309 /* Add pages to our bounce pool */
310 if (alloc_bounce_pages(newtag, pages) < pages)
311 error = ENOMEM;
312 }
313 /* Performed initial allocation */
314 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
315 }
316
317 if (error != 0) {
318 free(newtag, M_DEVBUF);
319 } else {
320 *dmat = newtag;
321 }
322 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
323 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
324 return (error);
325 }
326
327 int
328 bus_dma_tag_destroy(bus_dma_tag_t dmat)
329 {
330 bus_dma_tag_t dmat_copy;
331 int error;
332
333 error = 0;
334 dmat_copy = dmat;
335
336 if (dmat != NULL) {
337
338 if (dmat->map_count != 0) {
339 error = EBUSY;
340 goto out;
341 }
342
343 while (dmat != NULL) {
344 bus_dma_tag_t parent;
345
346 parent = dmat->parent;
347 atomic_subtract_int(&dmat->ref_count, 1);
348 if (dmat->ref_count == 0) {
349 if (dmat->segments != NULL)
350 free(dmat->segments, M_DEVBUF);
351 free(dmat, M_DEVBUF);
352 /*
353 * Last reference count, so
354 * release our reference
355 * count on our parent.
356 */
357 dmat = parent;
358 } else
359 dmat = NULL;
360 }
361 }
362 out:
363 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error);
364 return (error);
365 }
366
367 /*
368 * Allocate a handle for mapping from kva/uva/physical
369 * address space into bus device space.
370 */
371 int
372 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
373 {
374 int error;
375
376 error = 0;
377
378 if (dmat->segments == NULL) {
379 dmat->segments = (bus_dma_segment_t *)malloc(
380 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
381 M_NOWAIT);
382 if (dmat->segments == NULL) {
383 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
384 __func__, dmat, ENOMEM);
385 return (ENOMEM);
386 }
387 }
388
389 /*
390 * Bouncing might be required if the driver asks for an active
391 * exclusion region, a data alignment that is stricter than 1, and/or
392 * an active address boundary.
393 */
394 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
395
396 /* Must bounce */
397 struct bounce_zone *bz;
398 int maxpages;
399
400 if (dmat->bounce_zone == NULL) {
401 if ((error = alloc_bounce_zone(dmat)) != 0)
402 return (error);
403 }
404 bz = dmat->bounce_zone;
405
406 *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF,
407 M_NOWAIT | M_ZERO);
408 if (*mapp == NULL) {
409 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
410 __func__, dmat, ENOMEM);
411 return (ENOMEM);
412 }
413
414 /* Initialize the new map */
415 STAILQ_INIT(&((*mapp)->bpages));
416
417 /*
418 * Attempt to add pages to our pool on a per-instance
419 * basis up to a sane limit.
420 */
421 if (dmat->alignment > 1)
422 maxpages = MAX_BPAGES;
423 else
424 maxpages = MIN(MAX_BPAGES, Maxmem -atop(dmat->lowaddr));
425 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
426 || (dmat->map_count > 0 && bz->total_bpages < maxpages)) {
427 int pages;
428
429 pages = MAX(atop(dmat->maxsize), 1);
430 pages = MIN(maxpages - bz->total_bpages, pages);
431 pages = MAX(pages, 1);
432 if (alloc_bounce_pages(dmat, pages) < pages)
433 error = ENOMEM;
434
435 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
436 if (error == 0)
437 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
438 } else {
439 error = 0;
440 }
441 }
442 } else {
443 *mapp = NULL;
444 }
445 if (error == 0)
446 dmat->map_count++;
447 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
448 __func__, dmat, dmat->flags, error);
449 return (error);
450 }
451
452 /*
453 * Destroy a handle for mapping from kva/uva/physical
454 * address space into bus device space.
455 */
456 int
457 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
458 {
459 if (map != NULL && map != &nobounce_dmamap) {
460 if (STAILQ_FIRST(&map->bpages) != NULL) {
461 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
462 __func__, dmat, EBUSY);
463 return (EBUSY);
464 }
465 free(map, M_DEVBUF);
466 }
467 dmat->map_count--;
468 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
469 return (0);
470 }
471
472
473 /*
474 * Allocate a piece of memory that can be efficiently mapped into
475 * bus device space based on the constraints lited in the dma tag.
476 * A dmamap to for use with dmamap_load is also allocated.
477 */
478 int
479 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
480 bus_dmamap_t *mapp)
481 {
482 int mflags;
483
484 if (flags & BUS_DMA_NOWAIT)
485 mflags = M_NOWAIT;
486 else
487 mflags = M_WAITOK;
488 if (flags & BUS_DMA_ZERO)
489 mflags |= M_ZERO;
490
491 /* If we succeed, no mapping/bouncing will be required */
492 *mapp = NULL;
493
494 if (dmat->segments == NULL) {
495 dmat->segments = (bus_dma_segment_t *)malloc(
496 sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF,
497 M_NOWAIT);
498 if (dmat->segments == NULL) {
499 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
500 __func__, dmat, dmat->flags, ENOMEM);
501 return (ENOMEM);
502 }
503 }
504
505 /*
506 * XXX:
507 * (dmat->alignment < dmat->maxsize) is just a quick hack; the exact
508 * alignment guarantees of malloc need to be nailed down, and the
509 * code below should be rewritten to take that into account.
510 *
511 * In the meantime, we'll warn the user if malloc gets it wrong.
512 */
513 if ((dmat->maxsize <= PAGE_SIZE) &&
514 (dmat->alignment < dmat->maxsize) &&
515 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem)) {
516 *vaddr = malloc(dmat->maxsize, M_DEVBUF, mflags);
517 } else {
518 /*
519 * XXX Use Contigmalloc until it is merged into this facility
520 * and handles multi-seg allocations. Nobody is doing
521 * multi-seg allocations yet though.
522 * XXX Certain AGP hardware does.
523 */
524 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
525 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
526 dmat->boundary);
527 }
528 if (*vaddr == NULL) {
529 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
530 __func__, dmat, dmat->flags, ENOMEM);
531 return (ENOMEM);
532 } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) {
533 printf("bus_dmamem_alloc failed to align memory properly.\n");
534 }
535 if (flags & BUS_DMA_NOCACHE)
536 pmap_change_attr((vm_offset_t)*vaddr, dmat->maxsize,
537 PAT_UNCACHEABLE);
538 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
539 __func__, dmat, dmat->flags, ENOMEM);
540 return (0);
541 }
542
543 /*
544 * Free a piece of memory and it's allociated dmamap, that was allocated
545 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
546 */
547 void
548 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
549 {
550 /*
551 * dmamem does not need to be bounced, so the map should be
552 * NULL
553 */
554 if (map != NULL)
555 panic("bus_dmamem_free: Invalid map freed\n");
556 pmap_change_attr((vm_offset_t)vaddr, dmat->maxsize, PAT_WRITE_BACK);
557 if ((dmat->maxsize <= PAGE_SIZE) &&
558 (dmat->alignment < dmat->maxsize) &&
559 dmat->lowaddr >= ptoa((vm_paddr_t)Maxmem))
560 free(vaddr, M_DEVBUF);
561 else {
562 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
563 }
564 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
565 }
566
567 /*
568 * Utility function to load a linear buffer. lastaddrp holds state
569 * between invocations (for multiple-buffer loads). segp contains
570 * the starting segment on entrace, and the ending segment on exit.
571 * first indicates if this is the first invocation of this function.
572 */
573 static __inline int
574 _bus_dmamap_load_buffer(bus_dma_tag_t dmat,
575 bus_dmamap_t map,
576 void *buf, bus_size_t buflen,
577 pmap_t pmap,
578 int flags,
579 bus_addr_t *lastaddrp,
580 bus_dma_segment_t *segs,
581 int *segp,
582 int first)
583 {
584 bus_size_t sgsize;
585 bus_addr_t curaddr, lastaddr, baddr, bmask;
586 vm_offset_t vaddr;
587 bus_addr_t paddr;
588 int seg;
589
590 if (map == NULL)
591 map = &nobounce_dmamap;
592
593 if ((map != &nobounce_dmamap && map->pagesneeded == 0)
594 && ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) {
595 vm_offset_t vendaddr;
596
597 CTR4(KTR_BUSDMA, "lowaddr= %d Maxmem= %d, boundary= %d, "
598 "alignment= %d", dmat->lowaddr, ptoa((vm_paddr_t)Maxmem),
599 dmat->boundary, dmat->alignment);
600 CTR3(KTR_BUSDMA, "map= %p, nobouncemap= %p, pagesneeded= %d",
601 map, &nobounce_dmamap, map->pagesneeded);
602 /*
603 * Count the number of bounce pages
604 * needed in order to complete this transfer
605 */
606 vaddr = (vm_offset_t)buf;
607 vendaddr = (vm_offset_t)buf + buflen;
608
609 while (vaddr < vendaddr) {
610 paddr = pmap_kextract(vaddr);
611 if (run_filter(dmat, paddr) != 0)
612 map->pagesneeded++;
613 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK));
614 }
615 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
616 }
617
618 /* Reserve Necessary Bounce Pages */
619 if (map->pagesneeded != 0) {
620 mtx_lock(&bounce_lock);
621 if (flags & BUS_DMA_NOWAIT) {
622 if (reserve_bounce_pages(dmat, map, 0) != 0) {
623 mtx_unlock(&bounce_lock);
624 return (ENOMEM);
625 }
626 } else {
627 if (reserve_bounce_pages(dmat, map, 1) != 0) {
628 /* Queue us for resources */
629 map->dmat = dmat;
630 map->buf = buf;
631 map->buflen = buflen;
632 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
633 map, links);
634 mtx_unlock(&bounce_lock);
635 return (EINPROGRESS);
636 }
637 }
638 mtx_unlock(&bounce_lock);
639 }
640
641 vaddr = (vm_offset_t)buf;
642 lastaddr = *lastaddrp;
643 bmask = ~(dmat->boundary - 1);
644
645 for (seg = *segp; buflen > 0 ; ) {
646 /*
647 * Get the physical address for this segment.
648 */
649 if (pmap)
650 curaddr = pmap_extract(pmap, vaddr);
651 else
652 curaddr = pmap_kextract(vaddr);
653
654 /*
655 * Compute the segment size, and adjust counts.
656 */
657 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
658 if (buflen < sgsize)
659 sgsize = buflen;
660
661 /*
662 * Make sure we don't cross any boundaries.
663 */
664 if (dmat->boundary > 0) {
665 baddr = (curaddr + dmat->boundary) & bmask;
666 if (sgsize > (baddr - curaddr))
667 sgsize = (baddr - curaddr);
668 }
669
670 if (map->pagesneeded != 0 && run_filter(dmat, curaddr))
671 curaddr = add_bounce_page(dmat, map, vaddr, sgsize);
672
673 /*
674 * Insert chunk into a segment, coalescing with
675 * previous segment if possible.
676 */
677 if (first) {
678 segs[seg].ds_addr = curaddr;
679 segs[seg].ds_len = sgsize;
680 first = 0;
681 } else {
682 if (curaddr == lastaddr &&
683 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
684 (dmat->boundary == 0 ||
685 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
686 segs[seg].ds_len += sgsize;
687 else {
688 if (++seg >= dmat->nsegments)
689 break;
690 segs[seg].ds_addr = curaddr;
691 segs[seg].ds_len = sgsize;
692 }
693 }
694
695 lastaddr = curaddr + sgsize;
696 vaddr += sgsize;
697 buflen -= sgsize;
698 }
699
700 *segp = seg;
701 *lastaddrp = lastaddr;
702
703 /*
704 * Did we fit?
705 */
706 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
707 }
708
709 /*
710 * Map the buffer buf into bus space using the dmamap map.
711 */
712 int
713 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
714 bus_size_t buflen, bus_dmamap_callback_t *callback,
715 void *callback_arg, int flags)
716 {
717 bus_addr_t lastaddr = 0;
718 int error, nsegs = 0;
719
720 if (map != NULL) {
721 flags |= BUS_DMA_WAITOK;
722 map->callback = callback;
723 map->callback_arg = callback_arg;
724 }
725
726 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, NULL, flags,
727 &lastaddr, dmat->segments, &nsegs, 1);
728
729 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
730 __func__, dmat, dmat->flags, error, nsegs + 1);
731
732 if (error == EINPROGRESS) {
733 return (error);
734 }
735
736 if (error)
737 (*callback)(callback_arg, dmat->segments, 0, error);
738 else
739 (*callback)(callback_arg, dmat->segments, nsegs + 1, 0);
740
741 /*
742 * Return ENOMEM to the caller so that it can pass it up the stack.
743 * This error only happens when NOWAIT is set, so deferal is disabled.
744 */
745 if (error == ENOMEM)
746 return (error);
747
748 return (0);
749 }
750
751
752 /*
753 * Like _bus_dmamap_load(), but for mbufs.
754 */
755 int
756 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
757 struct mbuf *m0,
758 bus_dmamap_callback2_t *callback, void *callback_arg,
759 int flags)
760 {
761 int nsegs, error;
762
763 M_ASSERTPKTHDR(m0);
764
765 flags |= BUS_DMA_NOWAIT;
766 nsegs = 0;
767 error = 0;
768 if (m0->m_pkthdr.len <= dmat->maxsize) {
769 int first = 1;
770 bus_addr_t lastaddr = 0;
771 struct mbuf *m;
772
773 for (m = m0; m != NULL && error == 0; m = m->m_next) {
774 if (m->m_len > 0) {
775 error = _bus_dmamap_load_buffer(dmat, map,
776 m->m_data, m->m_len,
777 NULL, flags, &lastaddr,
778 dmat->segments, &nsegs, first);
779 first = 0;
780 }
781 }
782 } else {
783 error = EINVAL;
784 }
785
786 if (error) {
787 /* force "no valid mappings" in callback */
788 (*callback)(callback_arg, dmat->segments, 0, 0, error);
789 } else {
790 (*callback)(callback_arg, dmat->segments,
791 nsegs+1, m0->m_pkthdr.len, error);
792 }
793 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
794 __func__, dmat, dmat->flags, error, nsegs + 1);
795 return (error);
796 }
797
798 int
799 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
800 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs,
801 int flags)
802 {
803 int error;
804
805 M_ASSERTPKTHDR(m0);
806
807 flags |= BUS_DMA_NOWAIT;
808 *nsegs = 0;
809 error = 0;
810 if (m0->m_pkthdr.len <= dmat->maxsize) {
811 int first = 1;
812 bus_addr_t lastaddr = 0;
813 struct mbuf *m;
814
815 for (m = m0; m != NULL && error == 0; m = m->m_next) {
816 if (m->m_len > 0) {
817 error = _bus_dmamap_load_buffer(dmat, map,
818 m->m_data, m->m_len,
819 NULL, flags, &lastaddr,
820 segs, nsegs, first);
821 first = 0;
822 }
823 }
824 } else {
825 error = EINVAL;
826 }
827
828 /* XXX FIXME: Having to increment nsegs is really annoying */
829 ++*nsegs;
830 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
831 __func__, dmat, dmat->flags, error, *nsegs);
832 return (error);
833 }
834
835 /*
836 * Like _bus_dmamap_load(), but for uios.
837 */
838 int
839 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
840 struct uio *uio,
841 bus_dmamap_callback2_t *callback, void *callback_arg,
842 int flags)
843 {
844 bus_addr_t lastaddr;
845 int nsegs, error, first, i;
846 bus_size_t resid;
847 struct iovec *iov;
848 pmap_t pmap;
849
850 flags |= BUS_DMA_NOWAIT;
851 resid = uio->uio_resid;
852 iov = uio->uio_iov;
853
854 if (uio->uio_segflg == UIO_USERSPACE) {
855 KASSERT(uio->uio_td != NULL,
856 ("bus_dmamap_load_uio: USERSPACE but no proc"));
857 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
858 } else
859 pmap = NULL;
860
861 nsegs = 0;
862 error = 0;
863 first = 1;
864 lastaddr = (bus_addr_t) 0;
865 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
866 /*
867 * Now at the first iovec to load. Load each iovec
868 * until we have exhausted the residual count.
869 */
870 bus_size_t minlen =
871 resid < iov[i].iov_len ? resid : iov[i].iov_len;
872 caddr_t addr = (caddr_t) iov[i].iov_base;
873
874 if (minlen > 0) {
875 error = _bus_dmamap_load_buffer(dmat, map,
876 addr, minlen, pmap, flags, &lastaddr,
877 dmat->segments, &nsegs, first);
878 first = 0;
879
880 resid -= minlen;
881 }
882 }
883
884 if (error) {
885 /* force "no valid mappings" in callback */
886 (*callback)(callback_arg, dmat->segments, 0, 0, error);
887 } else {
888 (*callback)(callback_arg, dmat->segments,
889 nsegs+1, uio->uio_resid, error);
890 }
891 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
892 __func__, dmat, dmat->flags, error, nsegs + 1);
893 return (error);
894 }
895
896 /*
897 * Release the mapping held by map.
898 */
899 void
900 _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
901 {
902 struct bounce_page *bpage;
903
904 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
905 STAILQ_REMOVE_HEAD(&map->bpages, links);
906 free_bounce_page(dmat, bpage);
907 }
908 }
909
910 void
911 _bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
912 {
913 struct bounce_page *bpage;
914
915 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
916 /*
917 * Handle data bouncing. We might also
918 * want to add support for invalidating
919 * the caches on broken hardware
920 */
921 dmat->bounce_zone->total_bounced++;
922 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x "
923 "performing bounce", __func__, op, dmat, dmat->flags);
924
925 if (op & BUS_DMASYNC_PREWRITE) {
926 while (bpage != NULL) {
927 bcopy((void *)bpage->datavaddr,
928 (void *)bpage->vaddr,
929 bpage->datacount);
930 bpage = STAILQ_NEXT(bpage, links);
931 }
932 }
933
934 if (op & BUS_DMASYNC_POSTREAD) {
935 while (bpage != NULL) {
936 bcopy((void *)bpage->vaddr,
937 (void *)bpage->datavaddr,
938 bpage->datacount);
939 bpage = STAILQ_NEXT(bpage, links);
940 }
941 }
942 }
943 }
944
945 static void
946 init_bounce_pages(void *dummy __unused)
947 {
948
949 total_bpages = 0;
950 STAILQ_INIT(&bounce_zone_list);
951 STAILQ_INIT(&bounce_map_waitinglist);
952 STAILQ_INIT(&bounce_map_callbacklist);
953 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
954 }
955 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
956
957 static struct sysctl_ctx_list *
958 busdma_sysctl_tree(struct bounce_zone *bz)
959 {
960 return (&bz->sysctl_tree);
961 }
962
963 static struct sysctl_oid *
964 busdma_sysctl_tree_top(struct bounce_zone *bz)
965 {
966 return (bz->sysctl_tree_top);
967 }
968
969 static int
970 alloc_bounce_zone(bus_dma_tag_t dmat)
971 {
972 struct bounce_zone *bz;
973
974 /* Check to see if we already have a suitable zone */
975 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
976 if ((dmat->alignment <= bz->alignment)
977 && (dmat->boundary <= bz->boundary)
978 && (dmat->lowaddr >= bz->lowaddr)) {
979 dmat->bounce_zone = bz;
980 return (0);
981 }
982 }
983
984 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF,
985 M_NOWAIT | M_ZERO)) == NULL)
986 return (ENOMEM);
987
988 STAILQ_INIT(&bz->bounce_page_list);
989 bz->free_bpages = 0;
990 bz->reserved_bpages = 0;
991 bz->active_bpages = 0;
992 bz->lowaddr = dmat->lowaddr;
993 bz->alignment = dmat->alignment;
994 bz->boundary = dmat->boundary;
995 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
996 busdma_zonecount++;
997 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
998 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
999 dmat->bounce_zone = bz;
1000
1001 sysctl_ctx_init(&bz->sysctl_tree);
1002 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1003 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1004 CTLFLAG_RD, 0, "");
1005 if (bz->sysctl_tree_top == NULL) {
1006 sysctl_ctx_free(&bz->sysctl_tree);
1007 return (0); /* XXX error code? */
1008 }
1009
1010 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1011 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1012 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1013 "Total bounce pages");
1014 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1015 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1016 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1017 "Free bounce pages");
1018 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1019 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1020 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1021 "Reserved bounce pages");
1022 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1023 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1024 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1025 "Active bounce pages");
1026 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1027 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1028 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1029 "Total bounce requests");
1030 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1031 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1032 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1033 "Total bounce requests that were deferred");
1034 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1035 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1036 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1037 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1038 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1039 "alignment", CTLFLAG_RD, &bz->alignment, 0, "");
1040 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1041 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1042 "boundary", CTLFLAG_RD, &bz->boundary, 0, "");
1043
1044 return (0);
1045 }
1046
1047 static int
1048 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1049 {
1050 struct bounce_zone *bz;
1051 int count;
1052
1053 bz = dmat->bounce_zone;
1054 count = 0;
1055 while (numpages > 0) {
1056 struct bounce_page *bpage;
1057
1058 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF,
1059 M_NOWAIT | M_ZERO);
1060
1061 if (bpage == NULL)
1062 break;
1063 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
1064 M_NOWAIT, 0ul,
1065 bz->lowaddr,
1066 PAGE_SIZE,
1067 bz->boundary);
1068 if (bpage->vaddr == 0) {
1069 free(bpage, M_DEVBUF);
1070 break;
1071 }
1072 bpage->busaddr = pmap_kextract(bpage->vaddr);
1073 mtx_lock(&bounce_lock);
1074 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1075 total_bpages++;
1076 bz->total_bpages++;
1077 bz->free_bpages++;
1078 mtx_unlock(&bounce_lock);
1079 count++;
1080 numpages--;
1081 }
1082 return (count);
1083 }
1084
1085 static int
1086 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1087 {
1088 struct bounce_zone *bz;
1089 int pages;
1090
1091 mtx_assert(&bounce_lock, MA_OWNED);
1092 bz = dmat->bounce_zone;
1093 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1094 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1095 return (map->pagesneeded - (map->pagesreserved + pages));
1096 bz->free_bpages -= pages;
1097 bz->reserved_bpages += pages;
1098 map->pagesreserved += pages;
1099 pages = map->pagesneeded - map->pagesreserved;
1100
1101 return (pages);
1102 }
1103
1104 static bus_addr_t
1105 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1106 bus_size_t size)
1107 {
1108 struct bounce_zone *bz;
1109 struct bounce_page *bpage;
1110
1111 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1112 KASSERT(map != NULL && map != &nobounce_dmamap,
1113 ("add_bounce_page: bad map %p", map));
1114
1115 bz = dmat->bounce_zone;
1116 if (map->pagesneeded == 0)
1117 panic("add_bounce_page: map doesn't need any pages");
1118 map->pagesneeded--;
1119
1120 if (map->pagesreserved == 0)
1121 panic("add_bounce_page: map doesn't need any pages");
1122 map->pagesreserved--;
1123
1124 mtx_lock(&bounce_lock);
1125 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1126 if (bpage == NULL)
1127 panic("add_bounce_page: free page list is empty");
1128
1129 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1130 bz->reserved_bpages--;
1131 bz->active_bpages++;
1132 mtx_unlock(&bounce_lock);
1133
1134 bpage->datavaddr = vaddr;
1135 bpage->datacount = size;
1136 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1137 return (bpage->busaddr);
1138 }
1139
1140 static void
1141 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1142 {
1143 struct bus_dmamap *map;
1144 struct bounce_zone *bz;
1145
1146 bz = dmat->bounce_zone;
1147 bpage->datavaddr = 0;
1148 bpage->datacount = 0;
1149
1150 mtx_lock(&bounce_lock);
1151 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1152 bz->free_bpages++;
1153 bz->active_bpages--;
1154 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1155 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1156 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1157 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1158 map, links);
1159 busdma_swi_pending = 1;
1160 bz->total_deferred++;
1161 swi_sched(vm_ih, 0);
1162 }
1163 }
1164 mtx_unlock(&bounce_lock);
1165 }
1166
1167 void
1168 busdma_swi(void)
1169 {
1170 bus_dma_tag_t dmat;
1171 struct bus_dmamap *map;
1172
1173 mtx_lock(&bounce_lock);
1174 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1175 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1176 mtx_unlock(&bounce_lock);
1177 dmat = map->dmat;
1178 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1179 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
1180 map->callback, map->callback_arg, /*flags*/0);
1181 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1182 mtx_lock(&bounce_lock);
1183 }
1184 mtx_unlock(&bounce_lock);
1185 }
Cache object: ca02985454b29821bc6d03a8138fc5a8
|