1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 Oleksandr Tymoshenko
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * From i386/busdma_machdep.c,v 1.26 2002/04/19 22:58:09 alfred
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35 * MIPS bus dma support routines
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/bus.h>
42 #include <sys/busdma_bufalloc.h>
43 #include <sys/interrupt.h>
44 #include <sys/lock.h>
45 #include <sys/proc.h>
46 #include <sys/memdesc.h>
47 #include <sys/mutex.h>
48 #include <sys/ktr.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/uio.h>
52
53 #include <vm/uma.h>
54 #include <vm/vm.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_phys.h>
59 #include <vm/vm_map.h>
60
61 #include <machine/atomic.h>
62 #include <machine/bus.h>
63 #include <machine/cache.h>
64 #include <machine/cpufunc.h>
65 #include <machine/cpuinfo.h>
66 #include <machine/md_var.h>
67
68 #define MAX_BPAGES 64
69 #define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3
70 #define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
71
72 /*
73 * On XBurst cores from Ingenic, cache-line writeback is local
74 * only, unless accompanied by invalidation. Invalidations force
75 * dirty line writeout and invalidation requests forwarded to
76 * other cores if other cores have the cache line dirty.
77 */
78 #if defined(SMP) && defined(CPU_XBURST)
79 #define BUS_DMA_FORCE_WBINV
80 #endif
81
82 struct bounce_zone;
83
84 struct bus_dma_tag {
85 bus_dma_tag_t parent;
86 bus_size_t alignment;
87 bus_addr_t boundary;
88 bus_addr_t lowaddr;
89 bus_addr_t highaddr;
90 bus_dma_filter_t *filter;
91 void *filterarg;
92 bus_size_t maxsize;
93 u_int nsegments;
94 bus_size_t maxsegsz;
95 int flags;
96 int ref_count;
97 int map_count;
98 bus_dma_lock_t *lockfunc;
99 void *lockfuncarg;
100 bus_dma_segment_t *segments;
101 struct bounce_zone *bounce_zone;
102 };
103
104 struct bounce_page {
105 vm_offset_t vaddr; /* kva of bounce buffer */
106 vm_offset_t vaddr_nocache; /* kva of bounce buffer uncached */
107 bus_addr_t busaddr; /* Physical address */
108 vm_offset_t datavaddr; /* kva of client data */
109 bus_addr_t dataaddr; /* client physical address */
110 bus_size_t datacount; /* client data count */
111 STAILQ_ENTRY(bounce_page) links;
112 };
113
114 struct sync_list {
115 vm_offset_t vaddr; /* kva of bounce buffer */
116 bus_addr_t busaddr; /* Physical address */
117 bus_size_t datacount; /* client data count */
118 };
119
120 int busdma_swi_pending;
121
122 struct bounce_zone {
123 STAILQ_ENTRY(bounce_zone) links;
124 STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
125 int total_bpages;
126 int free_bpages;
127 int reserved_bpages;
128 int active_bpages;
129 int total_bounced;
130 int total_deferred;
131 int map_count;
132 bus_size_t alignment;
133 bus_addr_t lowaddr;
134 char zoneid[8];
135 char lowaddrid[20];
136 struct sysctl_ctx_list sysctl_tree;
137 struct sysctl_oid *sysctl_tree_top;
138 };
139
140 static struct mtx bounce_lock;
141 static int total_bpages;
142 static int busdma_zonecount;
143 static STAILQ_HEAD(, bounce_zone) bounce_zone_list;
144
145 static SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0,
146 "Busdma parameters");
147 SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0,
148 "Total bounce pages");
149
150 #define DMAMAP_UNCACHEABLE 0x08
151 #define DMAMAP_CACHE_ALIGNED 0x10
152
153 struct bus_dmamap {
154 struct bp_list bpages;
155 int pagesneeded;
156 int pagesreserved;
157 bus_dma_tag_t dmat;
158 struct memdesc mem;
159 int flags;
160 TAILQ_ENTRY(bus_dmamap) freelist;
161 STAILQ_ENTRY(bus_dmamap) links;
162 bus_dmamap_callback_t *callback;
163 void *callback_arg;
164 int sync_count;
165 struct sync_list *slist;
166 };
167
168 static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
169 static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
170
171 static void init_bounce_pages(void *dummy);
172 static int alloc_bounce_zone(bus_dma_tag_t dmat);
173 static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
174 static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map,
175 int commit);
176 static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
177 vm_offset_t vaddr, bus_addr_t addr,
178 bus_size_t size);
179 static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
180
181 /* Default tag, as most drivers provide no parent tag. */
182 bus_dma_tag_t mips_root_dma_tag;
183
184 static uma_zone_t dmamap_zone; /* Cache of struct bus_dmamap items */
185
186 static busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */
187 static busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */
188
189 MALLOC_DEFINE(M_BUSDMA, "busdma", "busdma metadata");
190 MALLOC_DEFINE(M_BOUNCE, "bounce", "busdma bounce pages");
191
192 /*
193 * This is the ctor function passed to uma_zcreate() for the pool of dma maps.
194 * It'll need platform-specific changes if this code is copied.
195 */
196 static int
dmamap_ctor(void * mem,int size,void * arg,int flags)197 dmamap_ctor(void *mem, int size, void *arg, int flags)
198 {
199 bus_dmamap_t map;
200 bus_dma_tag_t dmat;
201
202 map = (bus_dmamap_t)mem;
203 dmat = (bus_dma_tag_t)arg;
204
205 dmat->map_count++;
206
207 bzero(map, sizeof(*map));
208 map->dmat = dmat;
209 STAILQ_INIT(&map->bpages);
210
211 return (0);
212 }
213
214 /*
215 * This is the dtor function passed to uma_zcreate() for the pool of dma maps.
216 * It may need platform-specific changes if this code is copied .
217 */
218 static void
dmamap_dtor(void * mem,int size,void * arg)219 dmamap_dtor(void *mem, int size, void *arg)
220 {
221 bus_dmamap_t map;
222
223 map = (bus_dmamap_t)mem;
224
225 map->dmat->map_count--;
226 }
227
228 static void
busdma_init(void * dummy)229 busdma_init(void *dummy)
230 {
231
232 /* Create a cache of maps for bus_dmamap_create(). */
233 dmamap_zone = uma_zcreate("dma maps", sizeof(struct bus_dmamap),
234 dmamap_ctor, dmamap_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
235
236 /* Create a cache of buffers in standard (cacheable) memory. */
237 standard_allocator = busdma_bufalloc_create("buffer",
238 mips_dcache_max_linesize, /* minimum_alignment */
239 NULL, /* uma_alloc func */
240 NULL, /* uma_free func */
241 0); /* uma_zcreate_flags */
242
243 /*
244 * Create a cache of buffers in uncacheable memory, to implement the
245 * BUS_DMA_COHERENT flag.
246 */
247 coherent_allocator = busdma_bufalloc_create("coherent",
248 mips_dcache_max_linesize, /* minimum_alignment */
249 busdma_bufalloc_alloc_uncacheable,
250 busdma_bufalloc_free_uncacheable,
251 0); /* uma_zcreate_flags */
252 }
253 SYSINIT(busdma, SI_SUB_KMEM, SI_ORDER_FOURTH, busdma_init, NULL);
254
255 /*
256 * Return true if a match is made.
257 *
258 * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'.
259 *
260 * If paddr is within the bounds of the dma tag then call the filter callback
261 * to check for a match, if there is no filter callback then assume a match.
262 */
263 static int
run_filter(bus_dma_tag_t dmat,bus_addr_t paddr)264 run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
265 {
266 int retval;
267
268 retval = 0;
269
270 do {
271 if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr)
272 || ((paddr & (dmat->alignment - 1)) != 0))
273 && (dmat->filter == NULL
274 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
275 retval = 1;
276
277 dmat = dmat->parent;
278 } while (retval == 0 && dmat != NULL);
279 return (retval);
280 }
281
282 /*
283 * Check to see if the specified page is in an allowed DMA range.
284 */
285
286 static __inline int
_bus_dma_can_bounce(vm_offset_t lowaddr,vm_offset_t highaddr)287 _bus_dma_can_bounce(vm_offset_t lowaddr, vm_offset_t highaddr)
288 {
289 int i;
290 for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) {
291 if ((lowaddr >= phys_avail[i] && lowaddr <= phys_avail[i + 1])
292 || (lowaddr < phys_avail[i] &&
293 highaddr > phys_avail[i]))
294 return (1);
295 }
296 return (0);
297 }
298
299 /*
300 * Convenience function for manipulating driver locks from busdma (during
301 * busdma_swi, for example).
302 */
303 void
busdma_lock_mutex(void * arg,bus_dma_lock_op_t op)304 busdma_lock_mutex(void *arg, bus_dma_lock_op_t op)
305 {
306 struct mtx *dmtx;
307
308 dmtx = (struct mtx *)arg;
309 switch (op) {
310 case BUS_DMA_LOCK:
311 mtx_lock(dmtx);
312 break;
313 case BUS_DMA_UNLOCK:
314 mtx_unlock(dmtx);
315 break;
316 default:
317 panic("Unknown operation 0x%x for busdma_lock_mutex!", op);
318 }
319 }
320
321 /*
322 * dflt_lock should never get called. It gets put into the dma tag when
323 * lockfunc == NULL, which is only valid if the maps that are associated
324 * with the tag are meant to never be defered.
325 * XXX Should have a way to identify which driver is responsible here.
326 */
327 static void
dflt_lock(void * arg,bus_dma_lock_op_t op)328 dflt_lock(void *arg, bus_dma_lock_op_t op)
329 {
330 #ifdef INVARIANTS
331 panic("driver error: busdma dflt_lock called");
332 #else
333 printf("DRIVER_ERROR: busdma dflt_lock called\n");
334 #endif
335 }
336
337 static __inline bus_dmamap_t
_busdma_alloc_dmamap(bus_dma_tag_t dmat)338 _busdma_alloc_dmamap(bus_dma_tag_t dmat)
339 {
340 struct sync_list *slist;
341 bus_dmamap_t map;
342
343 slist = malloc(sizeof(*slist) * dmat->nsegments, M_BUSDMA, M_NOWAIT);
344 if (slist == NULL)
345 return (NULL);
346 map = uma_zalloc_arg(dmamap_zone, dmat, M_NOWAIT);
347 if (map != NULL)
348 map->slist = slist;
349 else
350 free(slist, M_BUSDMA);
351 return (map);
352 }
353
354 static __inline void
_busdma_free_dmamap(bus_dmamap_t map)355 _busdma_free_dmamap(bus_dmamap_t map)
356 {
357
358 free(map->slist, M_BUSDMA);
359 uma_zfree(dmamap_zone, map);
360 }
361
362 /*
363 * Allocate a device specific dma_tag.
364 */
365 #define SEG_NB 1024
366
367 int
bus_dma_tag_create(bus_dma_tag_t parent,bus_size_t alignment,bus_addr_t boundary,bus_addr_t lowaddr,bus_addr_t highaddr,bus_dma_filter_t * filter,void * filterarg,bus_size_t maxsize,int nsegments,bus_size_t maxsegsz,int flags,bus_dma_lock_t * lockfunc,void * lockfuncarg,bus_dma_tag_t * dmat)368 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
369 bus_addr_t boundary, bus_addr_t lowaddr,
370 bus_addr_t highaddr, bus_dma_filter_t *filter,
371 void *filterarg, bus_size_t maxsize, int nsegments,
372 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
373 void *lockfuncarg, bus_dma_tag_t *dmat)
374 {
375 bus_dma_tag_t newtag;
376 int error = 0;
377 /* Return a NULL tag on failure */
378 *dmat = NULL;
379 if (!parent)
380 parent = mips_root_dma_tag;
381
382 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_BUSDMA, M_NOWAIT);
383 if (newtag == NULL) {
384 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
385 __func__, newtag, 0, error);
386 return (ENOMEM);
387 }
388
389 newtag->parent = parent;
390 newtag->alignment = alignment;
391 newtag->boundary = boundary;
392 newtag->lowaddr = trunc_page((vm_offset_t)lowaddr) + (PAGE_SIZE - 1);
393 newtag->highaddr = trunc_page((vm_offset_t)highaddr) + (PAGE_SIZE - 1);
394 newtag->filter = filter;
395 newtag->filterarg = filterarg;
396 newtag->maxsize = maxsize;
397 newtag->nsegments = nsegments;
398 newtag->maxsegsz = maxsegsz;
399 newtag->flags = flags;
400 if (cpuinfo.cache_coherent_dma)
401 newtag->flags |= BUS_DMA_COHERENT;
402 newtag->ref_count = 1; /* Count ourself */
403 newtag->map_count = 0;
404 if (lockfunc != NULL) {
405 newtag->lockfunc = lockfunc;
406 newtag->lockfuncarg = lockfuncarg;
407 } else {
408 newtag->lockfunc = dflt_lock;
409 newtag->lockfuncarg = NULL;
410 }
411 newtag->segments = NULL;
412
413 /*
414 * Take into account any restrictions imposed by our parent tag
415 */
416 if (parent != NULL) {
417 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
418 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
419 if (newtag->boundary == 0)
420 newtag->boundary = parent->boundary;
421 else if (parent->boundary != 0)
422 newtag->boundary =
423 MIN(parent->boundary, newtag->boundary);
424 if ((newtag->filter != NULL) ||
425 ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0))
426 newtag->flags |= BUS_DMA_COULD_BOUNCE;
427 if (newtag->filter == NULL) {
428 /*
429 * Short circuit looking at our parent directly
430 * since we have encapsulated all of its information
431 */
432 newtag->filter = parent->filter;
433 newtag->filterarg = parent->filterarg;
434 newtag->parent = parent->parent;
435 }
436 if (newtag->parent != NULL)
437 atomic_add_int(&parent->ref_count, 1);
438 }
439 if (_bus_dma_can_bounce(newtag->lowaddr, newtag->highaddr)
440 || newtag->alignment > 1)
441 newtag->flags |= BUS_DMA_COULD_BOUNCE;
442
443 if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
444 (flags & BUS_DMA_ALLOCNOW) != 0) {
445 struct bounce_zone *bz;
446
447 /* Must bounce */
448
449 if ((error = alloc_bounce_zone(newtag)) != 0) {
450 free(newtag, M_BUSDMA);
451 return (error);
452 }
453 bz = newtag->bounce_zone;
454
455 if (ptoa(bz->total_bpages) < maxsize) {
456 int pages;
457
458 pages = atop(maxsize) - bz->total_bpages;
459
460 /* Add pages to our bounce pool */
461 if (alloc_bounce_pages(newtag, pages) < pages)
462 error = ENOMEM;
463 }
464 /* Performed initial allocation */
465 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
466 } else
467 newtag->bounce_zone = NULL;
468 if (error != 0)
469 free(newtag, M_BUSDMA);
470 else
471 *dmat = newtag;
472 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d",
473 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error);
474
475 return (error);
476 }
477
478 void
bus_dma_template_clone(bus_dma_template_t * t,bus_dma_tag_t dmat)479 bus_dma_template_clone(bus_dma_template_t *t, bus_dma_tag_t dmat)
480 {
481
482 if (t == NULL || dmat == NULL)
483 return;
484
485 t->parent = dmat->parent;
486 t->alignment = dmat->alignment;
487 t->boundary = dmat->boundary;
488 t->lowaddr = dmat->lowaddr;
489 t->highaddr = dmat->highaddr;
490 t->maxsize = dmat->maxsize;
491 t->nsegments = dmat->nsegments;
492 t->maxsegsize = dmat->maxsegsz;
493 t->flags = dmat->flags;
494 t->lockfunc = dmat->lockfunc;
495 t->lockfuncarg = dmat->lockfuncarg;
496 }
497
498 int
bus_dma_tag_set_domain(bus_dma_tag_t dmat,int domain)499 bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain)
500 {
501
502 return (0);
503 }
504
505 int
bus_dma_tag_destroy(bus_dma_tag_t dmat)506 bus_dma_tag_destroy(bus_dma_tag_t dmat)
507 {
508 #ifdef KTR
509 bus_dma_tag_t dmat_copy = dmat;
510 #endif
511
512 if (dmat != NULL) {
513 if (dmat->map_count != 0)
514 return (EBUSY);
515
516 while (dmat != NULL) {
517 bus_dma_tag_t parent;
518
519 parent = dmat->parent;
520 atomic_subtract_int(&dmat->ref_count, 1);
521 if (dmat->ref_count == 0) {
522 if (dmat->segments != NULL)
523 free(dmat->segments, M_BUSDMA);
524 free(dmat, M_BUSDMA);
525 /*
526 * Last reference count, so
527 * release our reference
528 * count on our parent.
529 */
530 dmat = parent;
531 } else
532 dmat = NULL;
533 }
534 }
535 CTR2(KTR_BUSDMA, "%s tag %p", __func__, dmat_copy);
536
537 return (0);
538 }
539
540 #include <sys/kdb.h>
541 /*
542 * Allocate a handle for mapping from kva/uva/physical
543 * address space into bus device space.
544 */
545 int
bus_dmamap_create(bus_dma_tag_t dmat,int flags,bus_dmamap_t * mapp)546 bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
547 {
548 bus_dmamap_t newmap;
549 int error = 0;
550
551 if (dmat->segments == NULL) {
552 dmat->segments = (bus_dma_segment_t *)malloc(
553 sizeof(bus_dma_segment_t) * dmat->nsegments, M_BUSDMA,
554 M_NOWAIT);
555 if (dmat->segments == NULL) {
556 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
557 __func__, dmat, ENOMEM);
558 return (ENOMEM);
559 }
560 }
561
562 newmap = _busdma_alloc_dmamap(dmat);
563 if (newmap == NULL) {
564 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM);
565 return (ENOMEM);
566 }
567 *mapp = newmap;
568
569 /*
570 * Bouncing might be required if the driver asks for an active
571 * exclusion region, a data alignment that is stricter than 1, and/or
572 * an active address boundary.
573 */
574 if (dmat->flags & BUS_DMA_COULD_BOUNCE) {
575 /* Must bounce */
576 struct bounce_zone *bz;
577 int maxpages;
578
579 if (dmat->bounce_zone == NULL) {
580 if ((error = alloc_bounce_zone(dmat)) != 0) {
581 _busdma_free_dmamap(newmap);
582 *mapp = NULL;
583 return (error);
584 }
585 }
586 bz = dmat->bounce_zone;
587
588 /* Initialize the new map */
589 STAILQ_INIT(&((*mapp)->bpages));
590
591 /*
592 * Attempt to add pages to our pool on a per-instance
593 * basis up to a sane limit.
594 */
595 maxpages = MAX_BPAGES;
596 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
597 || (bz->map_count > 0 && bz->total_bpages < maxpages)) {
598 int pages;
599
600 pages = MAX(atop(dmat->maxsize), 1);
601 pages = MIN(maxpages - bz->total_bpages, pages);
602 pages = MAX(pages, 1);
603 if (alloc_bounce_pages(dmat, pages) < pages)
604 error = ENOMEM;
605
606 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
607 if (error == 0)
608 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
609 } else {
610 error = 0;
611 }
612 }
613 bz->map_count++;
614 }
615
616 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
617 __func__, dmat, dmat->flags, error);
618
619 return (0);
620 }
621
622 /*
623 * Destroy a handle for mapping from kva/uva/physical
624 * address space into bus device space.
625 */
626 int
bus_dmamap_destroy(bus_dma_tag_t dmat,bus_dmamap_t map)627 bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
628 {
629
630 if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) {
631 CTR3(KTR_BUSDMA, "%s: tag %p error %d",
632 __func__, dmat, EBUSY);
633 return (EBUSY);
634 }
635 if (dmat->bounce_zone)
636 dmat->bounce_zone->map_count--;
637 _busdma_free_dmamap(map);
638 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat);
639 return (0);
640 }
641
642 /*
643 * Allocate a piece of memory that can be efficiently mapped into
644 * bus device space based on the constraints lited in the dma tag.
645 * A dmamap to for use with dmamap_load is also allocated.
646 */
647 int
bus_dmamem_alloc(bus_dma_tag_t dmat,void ** vaddrp,int flags,bus_dmamap_t * mapp)648 bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddrp, int flags,
649 bus_dmamap_t *mapp)
650 {
651 bus_dmamap_t newmap = NULL;
652 busdma_bufalloc_t ba;
653 struct busdma_bufzone *bufzone;
654 vm_memattr_t memattr;
655 void *vaddr;
656
657 int mflags;
658
659 if (flags & BUS_DMA_NOWAIT)
660 mflags = M_NOWAIT;
661 else
662 mflags = M_WAITOK;
663 if (dmat->segments == NULL) {
664 dmat->segments = (bus_dma_segment_t *)malloc(
665 sizeof(bus_dma_segment_t) * dmat->nsegments, M_BUSDMA,
666 mflags);
667 if (dmat->segments == NULL) {
668 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
669 __func__, dmat, dmat->flags, ENOMEM);
670 return (ENOMEM);
671 }
672 }
673
674 newmap = _busdma_alloc_dmamap(dmat);
675 if (newmap == NULL) {
676 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d",
677 __func__, dmat, dmat->flags, ENOMEM);
678 return (ENOMEM);
679 }
680
681 /*
682 * If all the memory is coherent with DMA then we don't need to
683 * do anything special for a coherent mapping request.
684 */
685 if (dmat->flags & BUS_DMA_COHERENT)
686 flags &= ~BUS_DMA_COHERENT;
687
688 if (flags & BUS_DMA_COHERENT) {
689 memattr = VM_MEMATTR_UNCACHEABLE;
690 ba = coherent_allocator;
691 newmap->flags |= DMAMAP_UNCACHEABLE;
692 } else {
693 memattr = VM_MEMATTR_DEFAULT;
694 ba = standard_allocator;
695 }
696 /* All buffers we allocate are cache-aligned. */
697 newmap->flags |= DMAMAP_CACHE_ALIGNED;
698
699 if (flags & BUS_DMA_ZERO)
700 mflags |= M_ZERO;
701
702 /*
703 * Try to find a bufzone in the allocator that holds a cache of buffers
704 * of the right size for this request. If the buffer is too big to be
705 * held in the allocator cache, this returns NULL.
706 */
707 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
708
709 /*
710 * Allocate the buffer from the uma(9) allocator if...
711 * - It's small enough to be in the allocator (bufzone not NULL).
712 * - The alignment constraint isn't larger than the allocation size
713 * (the allocator aligns buffers to their size boundaries).
714 * - There's no need to handle lowaddr/highaddr exclusion zones.
715 * else allocate non-contiguous pages if...
716 * - The page count that could get allocated doesn't exceed
717 * nsegments also when the maximum segment size is less
718 * than PAGE_SIZE.
719 * - The alignment constraint isn't larger than a page boundary.
720 * - There are no boundary-crossing constraints.
721 * else allocate a block of contiguous pages because one or more of the
722 * constraints is something that only the contig allocator can fulfill.
723 */
724 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
725 !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr)) {
726 vaddr = uma_zalloc(bufzone->umazone, mflags);
727 } else if (dmat->nsegments >=
728 howmany(dmat->maxsize, MIN(dmat->maxsegsz, PAGE_SIZE)) &&
729 dmat->alignment <= PAGE_SIZE &&
730 (dmat->boundary % PAGE_SIZE) == 0) {
731 vaddr = (void *)kmem_alloc_attr(dmat->maxsize, mflags, 0,
732 dmat->lowaddr, memattr);
733 } else {
734 vaddr = (void *)kmem_alloc_contig(dmat->maxsize, mflags, 0,
735 dmat->lowaddr, dmat->alignment, dmat->boundary, memattr);
736 }
737 if (vaddr == NULL) {
738 _busdma_free_dmamap(newmap);
739 newmap = NULL;
740 } else {
741 newmap->sync_count = 0;
742 }
743 *vaddrp = vaddr;
744 *mapp = newmap;
745
746 return (vaddr == NULL ? ENOMEM : 0);
747 }
748
749 /*
750 * Free a piece of memory and it's allocated dmamap, that was allocated
751 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
752 */
753 void
bus_dmamem_free(bus_dma_tag_t dmat,void * vaddr,bus_dmamap_t map)754 bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
755 {
756 struct busdma_bufzone *bufzone;
757 busdma_bufalloc_t ba;
758
759 if (map->flags & DMAMAP_UNCACHEABLE)
760 ba = coherent_allocator;
761 else
762 ba = standard_allocator;
763
764 free(map->slist, M_BUSDMA);
765 uma_zfree(dmamap_zone, map);
766
767 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize);
768
769 if (bufzone != NULL && dmat->alignment <= bufzone->size &&
770 !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr))
771 uma_zfree(bufzone->umazone, vaddr);
772 else
773 kmem_free((vm_offset_t)vaddr, dmat->maxsize);
774 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags);
775 }
776
777 static void
_bus_dmamap_count_phys(bus_dma_tag_t dmat,bus_dmamap_t map,vm_paddr_t buf,bus_size_t buflen,int flags)778 _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf,
779 bus_size_t buflen, int flags)
780 {
781 bus_addr_t curaddr;
782 bus_size_t sgsize;
783
784 if (map->pagesneeded == 0) {
785 CTR3(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d",
786 dmat->lowaddr, dmat->boundary, dmat->alignment);
787 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d",
788 map, map->pagesneeded);
789 /*
790 * Count the number of bounce pages
791 * needed in order to complete this transfer
792 */
793 curaddr = buf;
794 while (buflen != 0) {
795 sgsize = MIN(buflen, dmat->maxsegsz);
796 if (run_filter(dmat, curaddr) != 0) {
797 sgsize = MIN(sgsize, PAGE_SIZE);
798 map->pagesneeded++;
799 }
800 curaddr += sgsize;
801 buflen -= sgsize;
802 }
803 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
804 }
805 }
806
807 static void
_bus_dmamap_count_pages(bus_dma_tag_t dmat,bus_dmamap_t map,pmap_t pmap,void * buf,bus_size_t buflen,int flags)808 _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, pmap_t pmap,
809 void *buf, bus_size_t buflen, int flags)
810 {
811 vm_offset_t vaddr;
812 vm_offset_t vendaddr;
813 bus_addr_t paddr;
814
815 if (map->pagesneeded == 0) {
816 CTR3(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d",
817 dmat->lowaddr, dmat->boundary, dmat->alignment);
818 CTR2(KTR_BUSDMA, "map= %p, pagesneeded= %d",
819 map, map->pagesneeded);
820 /*
821 * Count the number of bounce pages
822 * needed in order to complete this transfer
823 */
824 vaddr = (vm_offset_t)buf;
825 vendaddr = (vm_offset_t)buf + buflen;
826
827 while (vaddr < vendaddr) {
828 bus_size_t sg_len;
829
830 KASSERT(kernel_pmap == pmap, ("pmap is not kernel pmap"));
831 sg_len = PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK);
832 paddr = pmap_kextract(vaddr);
833 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
834 run_filter(dmat, paddr) != 0) {
835 sg_len = roundup2(sg_len, dmat->alignment);
836 map->pagesneeded++;
837 }
838 vaddr += sg_len;
839 }
840 CTR1(KTR_BUSDMA, "pagesneeded= %d\n", map->pagesneeded);
841 }
842 }
843
844 static int
_bus_dmamap_reserve_pages(bus_dma_tag_t dmat,bus_dmamap_t map,int flags)845 _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map,int flags)
846 {
847
848 /* Reserve Necessary Bounce Pages */
849 mtx_lock(&bounce_lock);
850 if (flags & BUS_DMA_NOWAIT) {
851 if (reserve_bounce_pages(dmat, map, 0) != 0) {
852 mtx_unlock(&bounce_lock);
853 return (ENOMEM);
854 }
855 } else {
856 if (reserve_bounce_pages(dmat, map, 1) != 0) {
857 /* Queue us for resources */
858 STAILQ_INSERT_TAIL(&bounce_map_waitinglist,
859 map, links);
860 mtx_unlock(&bounce_lock);
861 return (EINPROGRESS);
862 }
863 }
864 mtx_unlock(&bounce_lock);
865
866 return (0);
867 }
868
869 /*
870 * Add a single contiguous physical range to the segment list.
871 */
872 static int
_bus_dmamap_addseg(bus_dma_tag_t dmat,bus_dmamap_t map,bus_addr_t curaddr,bus_size_t sgsize,bus_dma_segment_t * segs,int * segp)873 _bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr,
874 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp)
875 {
876 bus_addr_t baddr, bmask;
877 int seg;
878
879 /*
880 * Make sure we don't cross any boundaries.
881 */
882 bmask = ~(dmat->boundary - 1);
883 if (dmat->boundary > 0) {
884 baddr = (curaddr + dmat->boundary) & bmask;
885 if (sgsize > (baddr - curaddr))
886 sgsize = (baddr - curaddr);
887 }
888 /*
889 * Insert chunk into a segment, coalescing with
890 * the previous segment if possible.
891 */
892 seg = *segp;
893 if (seg >= 0 &&
894 curaddr == segs[seg].ds_addr + segs[seg].ds_len &&
895 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
896 (dmat->boundary == 0 ||
897 (segs[seg].ds_addr & bmask) == (curaddr & bmask))) {
898 segs[seg].ds_len += sgsize;
899 } else {
900 if (++seg >= dmat->nsegments)
901 return (0);
902 segs[seg].ds_addr = curaddr;
903 segs[seg].ds_len = sgsize;
904 }
905 *segp = seg;
906 return (sgsize);
907 }
908
909 /*
910 * Utility function to load a physical buffer. segp contains
911 * the starting segment on entrace, and the ending segment on exit.
912 */
913 int
_bus_dmamap_load_phys(bus_dma_tag_t dmat,bus_dmamap_t map,vm_paddr_t buf,bus_size_t buflen,int flags,bus_dma_segment_t * segs,int * segp)914 _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
915 vm_paddr_t buf, bus_size_t buflen, int flags, bus_dma_segment_t *segs,
916 int *segp)
917 {
918 bus_addr_t curaddr;
919 bus_size_t sgsize;
920 int error;
921
922 if (segs == NULL)
923 segs = dmat->segments;
924
925 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
926 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags);
927 if (map->pagesneeded != 0) {
928 error = _bus_dmamap_reserve_pages(dmat, map, flags);
929 if (error)
930 return (error);
931 }
932 }
933
934 while (buflen > 0) {
935 curaddr = buf;
936 sgsize = MIN(buflen, dmat->maxsegsz);
937 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
938 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
939 sgsize = MIN(sgsize, PAGE_SIZE);
940 curaddr = add_bounce_page(dmat, map, 0, curaddr,
941 sgsize);
942 }
943 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
944 segp);
945 if (sgsize == 0)
946 break;
947 buf += sgsize;
948 buflen -= sgsize;
949 }
950
951 /*
952 * Did we fit?
953 */
954 if (buflen != 0) {
955 bus_dmamap_unload(dmat, map);
956 return (EFBIG); /* XXX better return value here? */
957 }
958 return (0);
959 }
960
961 int
_bus_dmamap_load_ma(bus_dma_tag_t dmat,bus_dmamap_t map,struct vm_page ** ma,bus_size_t tlen,int ma_offs,int flags,bus_dma_segment_t * segs,int * segp)962 _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map,
963 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
964 bus_dma_segment_t *segs, int *segp)
965 {
966
967 return (bus_dmamap_load_ma_triv(dmat, map, ma, tlen, ma_offs, flags,
968 segs, segp));
969 }
970
971 /*
972 * Utility function to load a linear buffer. segp contains
973 * the starting segment on entrance, and the ending segment on exit.
974 * first indicates if this is the first invocation of this function.
975 */
976 int
_bus_dmamap_load_buffer(bus_dma_tag_t dmat,bus_dmamap_t map,void * buf,bus_size_t buflen,struct pmap * pmap,int flags,bus_dma_segment_t * segs,int * segp)977 _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
978 bus_size_t buflen, struct pmap *pmap, int flags, bus_dma_segment_t *segs,
979 int *segp)
980 {
981 bus_size_t sgsize;
982 bus_addr_t curaddr;
983 struct sync_list *sl;
984 vm_offset_t vaddr = (vm_offset_t)buf;
985 int error = 0;
986
987 if (segs == NULL)
988 segs = dmat->segments;
989 if ((flags & BUS_DMA_LOAD_MBUF) != 0)
990 map->flags |= DMAMAP_CACHE_ALIGNED;
991
992 if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) {
993 _bus_dmamap_count_pages(dmat, map, pmap, buf, buflen, flags);
994 if (map->pagesneeded != 0) {
995 error = _bus_dmamap_reserve_pages(dmat, map, flags);
996 if (error)
997 return (error);
998 }
999 }
1000 CTR3(KTR_BUSDMA, "lowaddr= %d boundary= %d, "
1001 "alignment= %d", dmat->lowaddr, dmat->boundary, dmat->alignment);
1002
1003 while (buflen > 0) {
1004 /*
1005 * Get the physical address for this segment.
1006 *
1007 * XXX Don't support checking for coherent mappings
1008 * XXX in user address space.
1009 */
1010 KASSERT(kernel_pmap == pmap, ("pmap is not kernel pmap"));
1011 curaddr = pmap_kextract(vaddr);
1012
1013 /*
1014 * Compute the segment size, and adjust counts.
1015 */
1016 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
1017 if (sgsize > dmat->maxsegsz)
1018 sgsize = dmat->maxsegsz;
1019 if (buflen < sgsize)
1020 sgsize = buflen;
1021
1022 if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) &&
1023 map->pagesneeded != 0 && run_filter(dmat, curaddr)) {
1024 curaddr = add_bounce_page(dmat, map, vaddr, curaddr,
1025 sgsize);
1026 } else {
1027 sl = &map->slist[map->sync_count - 1];
1028 if (map->sync_count == 0 ||
1029 vaddr != sl->vaddr + sl->datacount) {
1030 if (++map->sync_count > dmat->nsegments)
1031 goto cleanup;
1032 sl++;
1033 sl->vaddr = vaddr;
1034 sl->datacount = sgsize;
1035 sl->busaddr = curaddr;
1036 } else
1037 sl->datacount += sgsize;
1038 }
1039 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs,
1040 segp);
1041 if (sgsize == 0)
1042 break;
1043 vaddr += sgsize;
1044 buflen -= sgsize;
1045 }
1046
1047 cleanup:
1048 /*
1049 * Did we fit?
1050 */
1051 if (buflen != 0) {
1052 bus_dmamap_unload(dmat, map);
1053 error = EFBIG; /* XXX better return value here? */
1054 }
1055 return (error);
1056 }
1057
1058 void
_bus_dmamap_waitok(bus_dma_tag_t dmat,bus_dmamap_t map,struct memdesc * mem,bus_dmamap_callback_t * callback,void * callback_arg)1059 _bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
1060 struct memdesc *mem, bus_dmamap_callback_t *callback, void *callback_arg)
1061 {
1062
1063 KASSERT(dmat != NULL, ("dmatag is NULL"));
1064 KASSERT(map != NULL, ("dmamap is NULL"));
1065 map->mem = *mem;
1066 map->callback = callback;
1067 map->callback_arg = callback_arg;
1068 }
1069
1070 bus_dma_segment_t *
_bus_dmamap_complete(bus_dma_tag_t dmat,bus_dmamap_t map,bus_dma_segment_t * segs,int nsegs,int error)1071 _bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map,
1072 bus_dma_segment_t *segs, int nsegs, int error)
1073 {
1074
1075 if (segs == NULL)
1076 segs = dmat->segments;
1077 return (segs);
1078 }
1079
1080 /*
1081 * Release the mapping held by map.
1082 */
1083 void
bus_dmamap_unload(bus_dma_tag_t dmat,bus_dmamap_t map)1084 bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
1085 {
1086 struct bounce_page *bpage;
1087
1088 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
1089 STAILQ_REMOVE_HEAD(&map->bpages, links);
1090 free_bounce_page(dmat, bpage);
1091 }
1092 map->sync_count = 0;
1093 return;
1094 }
1095
1096 static void
bus_dmamap_sync_buf(vm_offset_t buf,int len,bus_dmasync_op_t op,int aligned)1097 bus_dmamap_sync_buf(vm_offset_t buf, int len, bus_dmasync_op_t op, int aligned)
1098 {
1099 char tmp_cl[mips_dcache_max_linesize], tmp_clend[mips_dcache_max_linesize];
1100 vm_offset_t buf_cl, buf_clend;
1101 vm_size_t size_cl, size_clend;
1102 int cache_linesize_mask = mips_dcache_max_linesize - 1;
1103
1104 /*
1105 * dcache invalidation operates on cache line aligned addresses
1106 * and could modify areas of memory that share the same cache line
1107 * at the beginning and the ending of the buffer. In order to
1108 * prevent a data loss we save these chunks in temporary buffer
1109 * before invalidation and restore them afer it.
1110 *
1111 * If the aligned flag is set the buffer is either an mbuf or came from
1112 * our allocator caches. In both cases they are always sized and
1113 * aligned to cacheline boundaries, so we can skip preserving nearby
1114 * data if a transfer appears to overlap cachelines. An mbuf in
1115 * particular will usually appear to be overlapped because of offsetting
1116 * within the buffer to align the L3 headers, but we know that the bytes
1117 * preceeding that offset are part of the same mbuf memory and are not
1118 * unrelated adjacent data (and a rule of mbuf handling is that the cpu
1119 * is not allowed to touch the mbuf while dma is in progress, including
1120 * header fields).
1121 */
1122 if (aligned) {
1123 size_cl = 0;
1124 size_clend = 0;
1125 } else {
1126 buf_cl = buf & ~cache_linesize_mask;
1127 size_cl = buf & cache_linesize_mask;
1128 buf_clend = buf + len;
1129 size_clend = (mips_dcache_max_linesize -
1130 (buf_clend & cache_linesize_mask)) & cache_linesize_mask;
1131 }
1132
1133 switch (op) {
1134 case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE:
1135 case BUS_DMASYNC_POSTREAD:
1136
1137 /*
1138 * Save buffers that might be modified by invalidation
1139 */
1140 if (size_cl)
1141 memcpy (tmp_cl, (void*)buf_cl, size_cl);
1142 if (size_clend)
1143 memcpy (tmp_clend, (void*)buf_clend, size_clend);
1144 mips_dcache_inv_range(buf, len);
1145 /*
1146 * Restore them
1147 */
1148 if (size_cl)
1149 memcpy ((void*)buf_cl, tmp_cl, size_cl);
1150 if (size_clend)
1151 memcpy ((void*)buf_clend, tmp_clend, size_clend);
1152 /*
1153 * Copies above have brought corresponding memory
1154 * cache lines back into dirty state. Write them back
1155 * out and invalidate affected cache lines again if
1156 * necessary.
1157 */
1158 if (size_cl)
1159 mips_dcache_wbinv_range(buf_cl, size_cl);
1160 if (size_clend && (size_cl == 0 ||
1161 buf_clend - buf_cl > mips_dcache_max_linesize))
1162 mips_dcache_wbinv_range(buf_clend, size_clend);
1163 break;
1164
1165 case BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE:
1166 mips_dcache_wbinv_range(buf, len);
1167 break;
1168
1169 case BUS_DMASYNC_PREREAD:
1170 /*
1171 * Save buffers that might be modified by invalidation
1172 */
1173 if (size_cl)
1174 memcpy (tmp_cl, (void *)buf_cl, size_cl);
1175 if (size_clend)
1176 memcpy (tmp_clend, (void *)buf_clend, size_clend);
1177 mips_dcache_inv_range(buf, len);
1178 /*
1179 * Restore them
1180 */
1181 if (size_cl)
1182 memcpy ((void *)buf_cl, tmp_cl, size_cl);
1183 if (size_clend)
1184 memcpy ((void *)buf_clend, tmp_clend, size_clend);
1185 /*
1186 * Copies above have brought corresponding memory
1187 * cache lines back into dirty state. Write them back
1188 * out and invalidate affected cache lines again if
1189 * necessary.
1190 */
1191 if (size_cl)
1192 mips_dcache_wbinv_range(buf_cl, size_cl);
1193 if (size_clend && (size_cl == 0 ||
1194 buf_clend - buf_cl > mips_dcache_max_linesize))
1195 mips_dcache_wbinv_range(buf_clend, size_clend);
1196 break;
1197
1198 case BUS_DMASYNC_PREWRITE:
1199 #ifdef BUS_DMA_FORCE_WBINV
1200 mips_dcache_wbinv_range(buf, len);
1201 #else
1202 mips_dcache_wb_range(buf, len);
1203 #endif
1204 break;
1205 }
1206 }
1207
1208 static void
_bus_dmamap_sync_bp(bus_dma_tag_t dmat,bus_dmamap_t map,bus_dmasync_op_t op)1209 _bus_dmamap_sync_bp(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1210 {
1211 struct bounce_page *bpage;
1212
1213 STAILQ_FOREACH(bpage, &map->bpages, links) {
1214 if (op & BUS_DMASYNC_PREWRITE) {
1215 if (bpage->datavaddr != 0)
1216 bcopy((void *)bpage->datavaddr,
1217 (void *)(bpage->vaddr_nocache != 0 ?
1218 bpage->vaddr_nocache :
1219 bpage->vaddr),
1220 bpage->datacount);
1221 else
1222 physcopyout(bpage->dataaddr,
1223 (void *)(bpage->vaddr_nocache != 0 ?
1224 bpage->vaddr_nocache :
1225 bpage->vaddr),
1226 bpage->datacount);
1227 if (bpage->vaddr_nocache == 0) {
1228 #ifdef BUS_DMA_FORCE_WBINV
1229 mips_dcache_wbinv_range(bpage->vaddr,
1230 bpage->datacount);
1231 #else
1232 mips_dcache_wb_range(bpage->vaddr,
1233 bpage->datacount);
1234 #endif
1235 }
1236 dmat->bounce_zone->total_bounced++;
1237 }
1238 if (op & BUS_DMASYNC_POSTREAD) {
1239 if (bpage->vaddr_nocache == 0) {
1240 mips_dcache_inv_range(bpage->vaddr,
1241 bpage->datacount);
1242 }
1243 if (bpage->datavaddr != 0)
1244 bcopy((void *)(bpage->vaddr_nocache != 0 ?
1245 bpage->vaddr_nocache : bpage->vaddr),
1246 (void *)bpage->datavaddr, bpage->datacount);
1247 else
1248 physcopyin((void *)(bpage->vaddr_nocache != 0 ?
1249 bpage->vaddr_nocache : bpage->vaddr),
1250 bpage->dataaddr, bpage->datacount);
1251 dmat->bounce_zone->total_bounced++;
1252 }
1253 }
1254 }
1255
1256 void
bus_dmamap_sync(bus_dma_tag_t dmat,bus_dmamap_t map,bus_dmasync_op_t op)1257 bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
1258 {
1259 struct sync_list *sl, *end;
1260 int aligned;
1261
1262 if (op == BUS_DMASYNC_POSTWRITE)
1263 return;
1264 if (STAILQ_FIRST(&map->bpages))
1265 _bus_dmamap_sync_bp(dmat, map, op);
1266
1267 if ((dmat->flags & BUS_DMA_COHERENT) ||
1268 (map->flags & DMAMAP_UNCACHEABLE)) {
1269 if (op & BUS_DMASYNC_PREWRITE)
1270 mips_sync();
1271 return;
1272 }
1273
1274 aligned = (map->flags & DMAMAP_CACHE_ALIGNED) ? 1 : 0;
1275
1276 CTR3(KTR_BUSDMA, "%s: op %x flags %x", __func__, op, map->flags);
1277 if (map->sync_count) {
1278 end = &map->slist[map->sync_count];
1279 for (sl = &map->slist[0]; sl != end; sl++)
1280 bus_dmamap_sync_buf(sl->vaddr, sl->datacount, op,
1281 aligned);
1282 }
1283 }
1284
1285 static void
init_bounce_pages(void * dummy __unused)1286 init_bounce_pages(void *dummy __unused)
1287 {
1288
1289 total_bpages = 0;
1290 STAILQ_INIT(&bounce_zone_list);
1291 STAILQ_INIT(&bounce_map_waitinglist);
1292 STAILQ_INIT(&bounce_map_callbacklist);
1293 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF);
1294 }
1295 SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL);
1296
1297 static struct sysctl_ctx_list *
busdma_sysctl_tree(struct bounce_zone * bz)1298 busdma_sysctl_tree(struct bounce_zone *bz)
1299 {
1300 return (&bz->sysctl_tree);
1301 }
1302
1303 static struct sysctl_oid *
busdma_sysctl_tree_top(struct bounce_zone * bz)1304 busdma_sysctl_tree_top(struct bounce_zone *bz)
1305 {
1306 return (bz->sysctl_tree_top);
1307 }
1308
1309 static int
alloc_bounce_zone(bus_dma_tag_t dmat)1310 alloc_bounce_zone(bus_dma_tag_t dmat)
1311 {
1312 struct bounce_zone *bz;
1313
1314 /* Check to see if we already have a suitable zone */
1315 STAILQ_FOREACH(bz, &bounce_zone_list, links) {
1316 if ((dmat->alignment <= bz->alignment)
1317 && (dmat->lowaddr >= bz->lowaddr)) {
1318 dmat->bounce_zone = bz;
1319 return (0);
1320 }
1321 }
1322
1323 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_BUSDMA,
1324 M_NOWAIT | M_ZERO)) == NULL)
1325 return (ENOMEM);
1326
1327 STAILQ_INIT(&bz->bounce_page_list);
1328 bz->free_bpages = 0;
1329 bz->reserved_bpages = 0;
1330 bz->active_bpages = 0;
1331 bz->lowaddr = dmat->lowaddr;
1332 bz->alignment = MAX(dmat->alignment, PAGE_SIZE);
1333 bz->map_count = 0;
1334 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount);
1335 busdma_zonecount++;
1336 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr);
1337 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links);
1338 dmat->bounce_zone = bz;
1339
1340 sysctl_ctx_init(&bz->sysctl_tree);
1341 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree,
1342 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid,
1343 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
1344 if (bz->sysctl_tree_top == NULL) {
1345 sysctl_ctx_free(&bz->sysctl_tree);
1346 return (0); /* XXX error code? */
1347 }
1348
1349 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1350 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1351 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0,
1352 "Total bounce pages");
1353 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1354 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1355 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0,
1356 "Free bounce pages");
1357 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1358 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1359 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0,
1360 "Reserved bounce pages");
1361 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1362 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1363 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0,
1364 "Active bounce pages");
1365 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1366 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1367 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0,
1368 "Total bounce requests");
1369 SYSCTL_ADD_INT(busdma_sysctl_tree(bz),
1370 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1371 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0,
1372 "Total bounce requests that were deferred");
1373 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz),
1374 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1375 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, "");
1376 SYSCTL_ADD_UAUTO(busdma_sysctl_tree(bz),
1377 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO,
1378 "alignment", CTLFLAG_RD, &bz->alignment, "");
1379
1380 return (0);
1381 }
1382
1383 static int
alloc_bounce_pages(bus_dma_tag_t dmat,u_int numpages)1384 alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
1385 {
1386 struct bounce_zone *bz;
1387 int count;
1388
1389 bz = dmat->bounce_zone;
1390 count = 0;
1391 while (numpages > 0) {
1392 struct bounce_page *bpage;
1393
1394 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_BUSDMA,
1395 M_NOWAIT | M_ZERO);
1396
1397 if (bpage == NULL)
1398 break;
1399 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_BOUNCE,
1400 M_NOWAIT, 0ul,
1401 bz->lowaddr,
1402 PAGE_SIZE,
1403 0);
1404 if (bpage->vaddr == 0) {
1405 free(bpage, M_BUSDMA);
1406 break;
1407 }
1408 bpage->busaddr = pmap_kextract(bpage->vaddr);
1409 bpage->vaddr_nocache =
1410 (vm_offset_t)pmap_mapdev(bpage->busaddr, PAGE_SIZE);
1411 mtx_lock(&bounce_lock);
1412 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links);
1413 total_bpages++;
1414 bz->total_bpages++;
1415 bz->free_bpages++;
1416 mtx_unlock(&bounce_lock);
1417 count++;
1418 numpages--;
1419 }
1420 return (count);
1421 }
1422
1423 static int
reserve_bounce_pages(bus_dma_tag_t dmat,bus_dmamap_t map,int commit)1424 reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit)
1425 {
1426 struct bounce_zone *bz;
1427 int pages;
1428
1429 mtx_assert(&bounce_lock, MA_OWNED);
1430 bz = dmat->bounce_zone;
1431 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved);
1432 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages))
1433 return (map->pagesneeded - (map->pagesreserved + pages));
1434 bz->free_bpages -= pages;
1435 bz->reserved_bpages += pages;
1436 map->pagesreserved += pages;
1437 pages = map->pagesneeded - map->pagesreserved;
1438
1439 return (pages);
1440 }
1441
1442 static bus_addr_t
add_bounce_page(bus_dma_tag_t dmat,bus_dmamap_t map,vm_offset_t vaddr,bus_addr_t addr,bus_size_t size)1443 add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
1444 bus_addr_t addr, bus_size_t size)
1445 {
1446 struct bounce_zone *bz;
1447 struct bounce_page *bpage;
1448
1449 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag"));
1450 KASSERT(map != NULL, ("add_bounce_page: bad map %p", map));
1451
1452 bz = dmat->bounce_zone;
1453 if (map->pagesneeded == 0)
1454 panic("add_bounce_page: map doesn't need any pages");
1455 map->pagesneeded--;
1456
1457 if (map->pagesreserved == 0)
1458 panic("add_bounce_page: map doesn't need any pages");
1459 map->pagesreserved--;
1460
1461 mtx_lock(&bounce_lock);
1462 bpage = STAILQ_FIRST(&bz->bounce_page_list);
1463 if (bpage == NULL)
1464 panic("add_bounce_page: free page list is empty");
1465
1466 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links);
1467 bz->reserved_bpages--;
1468 bz->active_bpages++;
1469 mtx_unlock(&bounce_lock);
1470
1471 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1472 /* Page offset needs to be preserved. */
1473 bpage->vaddr |= addr & PAGE_MASK;
1474 bpage->busaddr |= addr & PAGE_MASK;
1475 }
1476 bpage->datavaddr = vaddr;
1477 bpage->dataaddr = addr;
1478 bpage->datacount = size;
1479 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
1480 return (bpage->busaddr);
1481 }
1482
1483 static void
free_bounce_page(bus_dma_tag_t dmat,struct bounce_page * bpage)1484 free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
1485 {
1486 struct bus_dmamap *map;
1487 struct bounce_zone *bz;
1488
1489 bz = dmat->bounce_zone;
1490 bpage->datavaddr = 0;
1491 bpage->datacount = 0;
1492 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) {
1493 /*
1494 * Reset the bounce page to start at offset 0. Other uses
1495 * of this bounce page may need to store a full page of
1496 * data and/or assume it starts on a page boundary.
1497 */
1498 bpage->vaddr &= ~PAGE_MASK;
1499 bpage->busaddr &= ~PAGE_MASK;
1500 }
1501
1502 mtx_lock(&bounce_lock);
1503 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links);
1504 bz->free_bpages++;
1505 bz->active_bpages--;
1506 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
1507 if (reserve_bounce_pages(map->dmat, map, 1) == 0) {
1508 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
1509 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
1510 map, links);
1511 busdma_swi_pending = 1;
1512 bz->total_deferred++;
1513 swi_sched(vm_ih, 0);
1514 }
1515 }
1516 mtx_unlock(&bounce_lock);
1517 }
1518
1519 void
busdma_swi(void)1520 busdma_swi(void)
1521 {
1522 bus_dma_tag_t dmat;
1523 struct bus_dmamap *map;
1524
1525 mtx_lock(&bounce_lock);
1526 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
1527 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
1528 mtx_unlock(&bounce_lock);
1529 dmat = map->dmat;
1530 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK);
1531 bus_dmamap_load_mem(map->dmat, map, &map->mem, map->callback,
1532 map->callback_arg, BUS_DMA_WAITOK);
1533 (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK);
1534 mtx_lock(&bounce_lock);
1535 }
1536 mtx_unlock(&bounce_lock);
1537 }
1538