For kmalloc(), MALLOC() and contigmalloc(), use M_ZERO instead of
[dragonfly.git] / sys / platform / pc32 / i386 / busdma_machdep.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997, 1998 Justin T. Gibbs.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/i386/i386/busdma_machdep.c,v 1.16.2.2 2003/01/23 00:55:27 scottl Exp $
e7b4468c 27 * $DragonFly: src/sys/platform/pc32/i386/busdma_machdep.c,v 1.22 2008/01/05 14:02:41 swildner Exp $
984263bc
MD
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/mbuf.h>
34#include <sys/uio.h>
9acd5bbb 35#include <sys/thread2.h>
1f7ab7c9 36#include <sys/bus_dma.h>
984263bc
MD
37
38#include <vm/vm.h>
39#include <vm/vm_page.h>
40
41/* XXX needed for to access pmap to convert per-proc virtual to physical */
42#include <sys/proc.h>
43#include <sys/lock.h>
44#include <vm/vm_map.h>
45
984263bc
MD
46#include <machine/md_var.h>
47
4343fdc9 48#define MAX_BPAGES 1024
984263bc
MD
49
50struct bus_dma_tag {
51 bus_dma_tag_t parent;
52 bus_size_t alignment;
53 bus_size_t boundary;
54 bus_addr_t lowaddr;
55 bus_addr_t highaddr;
56 bus_dma_filter_t *filter;
57 void *filterarg;
58 bus_size_t maxsize;
59 u_int nsegments;
60 bus_size_t maxsegsz;
61 int flags;
62 int ref_count;
63 int map_count;
06577778 64 bus_dma_segment_t *segments;
984263bc
MD
65};
66
67struct bounce_page {
68 vm_offset_t vaddr; /* kva of bounce buffer */
69 bus_addr_t busaddr; /* Physical address */
70 vm_offset_t datavaddr; /* kva of client data */
71 bus_size_t datacount; /* client data count */
72 STAILQ_ENTRY(bounce_page) links;
73};
74
75int busdma_swi_pending;
76
77static STAILQ_HEAD(bp_list, bounce_page) bounce_page_list;
78static int free_bpages;
79static int reserved_bpages;
80static int active_bpages;
81static int total_bpages;
82static bus_addr_t bounce_lowaddr = BUS_SPACE_MAXADDR;
83
84struct bus_dmamap {
85 struct bp_list bpages;
86 int pagesneeded;
87 int pagesreserved;
88 bus_dma_tag_t dmat;
89 void *buf; /* unmapped buffer pointer */
90 bus_size_t buflen; /* unmapped buffer length */
91 bus_dmamap_callback_t *callback;
92 void *callback_arg;
93 STAILQ_ENTRY(bus_dmamap) links;
94};
95
96static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist;
97static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist;
98static struct bus_dmamap nobounce_dmamap;
99
100static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages);
101static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map);
6ef943a3 102static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map,
984263bc
MD
103 vm_offset_t vaddr, bus_size_t size);
104static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage);
105static __inline int run_filter(bus_dma_tag_t dmat, bus_addr_t paddr);
106
107static __inline int
108run_filter(bus_dma_tag_t dmat, bus_addr_t paddr)
109{
110 int retval;
111
112 retval = 0;
113 do {
114 if (paddr > dmat->lowaddr
115 && paddr <= dmat->highaddr
116 && (dmat->filter == NULL
117 || (*dmat->filter)(dmat->filterarg, paddr) != 0))
118 retval = 1;
119
120 dmat = dmat->parent;
121 } while (retval == 0 && dmat != NULL);
122 return (retval);
123}
124
125#define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4
126/*
127 * Allocate a device specific dma_tag.
128 */
129int
130bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
131 bus_size_t boundary, bus_addr_t lowaddr,
132 bus_addr_t highaddr, bus_dma_filter_t *filter,
133 void *filterarg, bus_size_t maxsize, int nsegments,
134 bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat)
135{
136 bus_dma_tag_t newtag;
137 int error = 0;
138
139 /* Return a NULL tag on failure */
140 *dmat = NULL;
141
efda3bd0 142 newtag = kmalloc(sizeof(*newtag), M_DEVBUF, M_INTWAIT);
984263bc
MD
143
144 newtag->parent = parent;
145 newtag->alignment = alignment;
146 newtag->boundary = boundary;
6ef943a3
MD
147 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1);
148 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + (PAGE_SIZE - 1);
984263bc
MD
149 newtag->filter = filter;
150 newtag->filterarg = filterarg;
151 newtag->maxsize = maxsize;
152 newtag->nsegments = nsegments;
153 newtag->maxsegsz = maxsegsz;
154 newtag->flags = flags;
155 newtag->ref_count = 1; /* Count ourself */
156 newtag->map_count = 0;
3a2114c4 157 newtag->segments = NULL;
984263bc
MD
158
159 /* Take into account any restrictions imposed by our parent tag */
160 if (parent != NULL) {
161 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr);
162 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr);
163 /*
164 * XXX Not really correct??? Probably need to honor boundary
165 * all the way up the inheritence chain.
166 */
167 newtag->boundary = MAX(parent->boundary, newtag->boundary);
168 if (newtag->filter == NULL) {
169 /*
170 * Short circuit looking at our parent directly
171 * since we have encapsulated all of its information
172 */
173 newtag->filter = parent->filter;
174 newtag->filterarg = parent->filterarg;
175 newtag->parent = parent->parent;
176 }
177 if (newtag->parent != NULL) {
178 parent->ref_count++;
179 }
180 }
181
6ef943a3
MD
182 if (newtag->lowaddr < ptoa(Maxmem) &&
183 (flags & BUS_DMA_ALLOCNOW) != 0) {
984263bc
MD
184 /* Must bounce */
185
186 if (lowaddr > bounce_lowaddr) {
187 /*
188 * Go through the pool and kill any pages
189 * that don't reside below lowaddr.
190 */
191 panic("bus_dma_tag_create: page reallocation "
192 "not implemented");
193 }
194 if (ptoa(total_bpages) < maxsize) {
195 int pages;
196
197 pages = atop(maxsize) - total_bpages;
198
199 /* Add pages to our bounce pool */
200 if (alloc_bounce_pages(newtag, pages) < pages)
201 error = ENOMEM;
202 }
203 /* Performed initial allocation */
204 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP;
205 }
206
207 if (error != 0) {
efda3bd0 208 kfree(newtag, M_DEVBUF);
984263bc
MD
209 } else {
210 *dmat = newtag;
211 }
212 return (error);
213}
214
215int
216bus_dma_tag_destroy(bus_dma_tag_t dmat)
217{
218 if (dmat != NULL) {
219
220 if (dmat->map_count != 0)
221 return (EBUSY);
222
223 while (dmat != NULL) {
224 bus_dma_tag_t parent;
225
226 parent = dmat->parent;
227 dmat->ref_count--;
228 if (dmat->ref_count == 0) {
06577778 229 if (dmat->segments != NULL)
efda3bd0
MD
230 kfree(dmat->segments, M_DEVBUF);
231 kfree(dmat, M_DEVBUF);
984263bc
MD
232 /*
233 * Last reference count, so
234 * release our reference
235 * count on our parent.
236 */
237 dmat = parent;
238 } else
239 dmat = NULL;
240 }
241 }
242 return (0);
243}
244
245/*
246 * Allocate a handle for mapping from kva/uva/physical
247 * address space into bus device space.
248 */
249int
250bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp)
251{
252 int error;
253
254 error = 0;
255
3a2114c4
MD
256 if (dmat->segments == NULL) {
257 KKASSERT(dmat->nsegments && dmat->nsegments < 16384);
77652cad 258 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
3a2114c4
MD
259 dmat->nsegments, M_DEVBUF, M_INTWAIT);
260 }
261
984263bc
MD
262 if (dmat->lowaddr < ptoa(Maxmem)) {
263 /* Must bounce */
264 int maxpages;
265
e7b4468c
SW
266 *mapp = kmalloc(sizeof(**mapp), M_DEVBUF, M_INTWAIT | M_ZERO);
267 if (*mapp == NULL)
984263bc 268 return (ENOMEM);
e7b4468c
SW
269 /* Initialize the new map */
270 STAILQ_INIT(&((*mapp)->bpages));
984263bc
MD
271 /*
272 * Attempt to add pages to our pool on a per-instance
273 * basis up to a sane limit.
274 */
275 maxpages = MIN(MAX_BPAGES, Maxmem - atop(dmat->lowaddr));
276 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0
277 || (dmat->map_count > 0
278 && total_bpages < maxpages)) {
279 int pages;
280
281 if (dmat->lowaddr > bounce_lowaddr) {
282 /*
283 * Go through the pool and kill any pages
284 * that don't reside below lowaddr.
285 */
286 panic("bus_dmamap_create: page reallocation "
287 "not implemented");
288 }
4343fdc9
SZ
289
290 pages = MAX(atop(dmat->maxsize), 1);
984263bc 291 pages = MIN(maxpages - total_bpages, pages);
4343fdc9
SZ
292 pages = MAX(pages, 1);
293 if (alloc_bounce_pages(dmat, pages) < pages)
294 error = ENOMEM;
984263bc
MD
295
296 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) {
297 if (error == 0)
298 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP;
299 } else {
300 error = 0;
301 }
302 }
303 } else {
304 *mapp = NULL;
305 }
306 if (error == 0)
307 dmat->map_count++;
308 return (error);
309}
310
311/*
312 * Destroy a handle for mapping from kva/uva/physical
313 * address space into bus device space.
314 */
315int
316bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map)
317{
318 if (map != NULL) {
319 if (STAILQ_FIRST(&map->bpages) != NULL)
320 return (EBUSY);
efda3bd0 321 kfree(map, M_DEVBUF);
984263bc
MD
322 }
323 dmat->map_count--;
324 return (0);
325}
326
327
328/*
329 * Allocate a piece of memory that can be efficiently mapped into
330 * bus device space based on the constraints lited in the dma tag.
ff8e0d00
MD
331 *
332 * mapp is degenerate. By definition this allocation should not require
333 * bounce buffers so do not allocate a dma map.
984263bc
MD
334 */
335int
336bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
337 bus_dmamap_t *mapp)
338{
ba0fefd4 339 int mflags;
984263bc
MD
340 /* If we succeed, no mapping/bouncing will be required */
341 *mapp = NULL;
342
3a2114c4
MD
343 if (dmat->segments == NULL) {
344 KKASSERT(dmat->nsegments < 16384);
77652cad 345 dmat->segments = kmalloc(sizeof(bus_dma_segment_t) *
3a2114c4
MD
346 dmat->nsegments, M_DEVBUF, M_INTWAIT);
347 }
348
ba0fefd4
JS
349 if (flags & BUS_DMA_NOWAIT)
350 mflags = M_NOWAIT;
351 else
352 mflags = M_WAITOK;
353 if (flags & BUS_DMA_ZERO)
354 mflags |= M_ZERO;
355
6ef943a3
MD
356 if ((dmat->maxsize <= PAGE_SIZE) &&
357 dmat->lowaddr >= ptoa(Maxmem)) {
efda3bd0 358 *vaddr = kmalloc(dmat->maxsize, M_DEVBUF, mflags);
68aa5c00 359 /*
ff8e0d00 360 * XXX Check whether the allocation crossed a page boundary
68aa5c00
JS
361 * and retry with power-of-2 alignment in that case.
362 */
363 if ((((intptr_t)*vaddr) & PAGE_MASK) !=
364 (((intptr_t)*vaddr + dmat->maxsize) & PAGE_MASK)) {
365 size_t size;
efda3bd0 366 kfree(*vaddr, M_DEVBUF);
68aa5c00
JS
367 /* XXX check for overflow? */
368 for (size = 1; size <= dmat->maxsize; size <<= 1)
369 ;
efda3bd0 370 *vaddr = kmalloc(size, M_DEVBUF, mflags);
68aa5c00 371 }
984263bc
MD
372 } else {
373 /*
374 * XXX Use Contigmalloc until it is merged into this facility
375 * and handles multi-seg allocations. Nobody is doing
376 * multi-seg allocations yet though.
377 */
ba0fefd4 378 *vaddr = contigmalloc(dmat->maxsize, M_DEVBUF, mflags,
984263bc
MD
379 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment : 1ul,
380 dmat->boundary);
381 }
382 if (*vaddr == NULL)
383 return (ENOMEM);
384 return (0);
385}
386
387/*
388 * Free a piece of memory and it's allociated dmamap, that was allocated
389 * via bus_dmamem_alloc. Make the same choice for free/contigfree.
390 */
391void
392bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map)
393{
394 /*
395 * dmamem does not need to be bounced, so the map should be
396 * NULL
397 */
398 if (map != NULL)
399 panic("bus_dmamem_free: Invalid map freed\n");
6ef943a3
MD
400 if ((dmat->maxsize <= PAGE_SIZE) &&
401 dmat->lowaddr >= ptoa(Maxmem))
efda3bd0 402 kfree(vaddr, M_DEVBUF);
984263bc
MD
403 else
404 contigfree(vaddr, dmat->maxsize, M_DEVBUF);
405}
406
407#define BUS_DMAMAP_NSEGS ((BUS_SPACE_MAXSIZE / PAGE_SIZE) + 1)
408
409/*
410 * Map the buffer buf into bus space using the dmamap map.
411 */
412int
413bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
414 bus_size_t buflen, bus_dmamap_callback_t *callback,
415 void *callback_arg, int flags)
416{
417 vm_offset_t vaddr;
6ef943a3 418 vm_paddr_t paddr;
984263bc
MD
419 bus_dma_segment_t *sg;
420 int seg;
421 int error;
6ef943a3 422 vm_paddr_t nextpaddr;
270d2794 423 bus_addr_t bmask;
984263bc
MD
424
425 if (map == NULL)
426 map = &nobounce_dmamap;
427
428 error = 0;
429 /*
430 * If we are being called during a callback, pagesneeded will
431 * be non-zero, so we can avoid doing the work twice.
432 */
6ef943a3
MD
433 if (dmat->lowaddr < ptoa(Maxmem) &&
434 map->pagesneeded == 0) {
984263bc
MD
435 vm_offset_t vendaddr;
436
437 /*
438 * Count the number of bounce pages
439 * needed in order to complete this transfer
440 */
441 vaddr = trunc_page((vm_offset_t)buf);
442 vendaddr = (vm_offset_t)buf + buflen;
443
444 while (vaddr < vendaddr) {
445 paddr = pmap_kextract(vaddr);
446 if (run_filter(dmat, paddr) != 0) {
447
448 map->pagesneeded++;
449 }
450 vaddr += PAGE_SIZE;
451 }
452 }
453
454 /* Reserve Necessary Bounce Pages */
455 if (map->pagesneeded != 0) {
9acd5bbb 456 crit_enter();
984263bc
MD
457 if (reserve_bounce_pages(dmat, map) != 0) {
458
459 /* Queue us for resources */
460 map->dmat = dmat;
461 map->buf = buf;
462 map->buflen = buflen;
463 map->callback = callback;
464 map->callback_arg = callback_arg;
465
466 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links);
9acd5bbb 467 crit_exit();
984263bc
MD
468
469 return (EINPROGRESS);
470 }
9acd5bbb 471 crit_exit();
984263bc
MD
472 }
473
474 vaddr = (vm_offset_t)buf;
06577778 475 sg = dmat->segments;
984263bc
MD
476 seg = 1;
477 sg->ds_len = 0;
984263bc 478 nextpaddr = 0;
270d2794
MD
479 bmask = ~(dmat->boundary - 1); /* note: will be 0 if boundary is 0 */
480
481 /* force at least one segment */
984263bc
MD
482 do {
483 bus_size_t size;
484
270d2794
MD
485 /*
486 * Per-page main loop
487 */
984263bc
MD
488 paddr = pmap_kextract(vaddr);
489 size = PAGE_SIZE - (paddr & PAGE_MASK);
490 if (size > buflen)
491 size = buflen;
984263bc 492 if (map->pagesneeded != 0 && run_filter(dmat, paddr)) {
270d2794
MD
493 /*
494 * note: this paddr has the same in-page offset
495 * as vaddr and thus the paddr above, so the
496 * size does not have to be recalculated
497 */
984263bc
MD
498 paddr = add_bounce_page(dmat, map, vaddr, size);
499 }
500
270d2794
MD
501 /*
502 * Fill in the bus_dma_segment
503 */
984263bc
MD
504 if (sg->ds_len == 0) {
505 sg->ds_addr = paddr;
506 sg->ds_len = size;
507 } else if (paddr == nextpaddr) {
508 sg->ds_len += size;
509 } else {
984263bc
MD
510 sg++;
511 seg++;
512 if (seg > dmat->nsegments)
513 break;
514 sg->ds_addr = paddr;
515 sg->ds_len = size;
516 }
984263bc 517 nextpaddr = paddr + size;
270d2794
MD
518
519 /*
520 * Handle maxsegsz and boundary issues with a nested loop
521 */
522 for (;;) {
523 bus_size_t tmpsize;
524
525 /*
526 * Limit to the boundary and maximum segment size
527 */
6b02d9de 528 if (((nextpaddr - 1) ^ sg->ds_addr) & bmask) {
270d2794
MD
529 tmpsize = dmat->boundary -
530 (sg->ds_addr & ~bmask);
531 if (tmpsize > dmat->maxsegsz)
532 tmpsize = dmat->maxsegsz;
533 KKASSERT(tmpsize < sg->ds_len);
534 } else if (sg->ds_len > dmat->maxsegsz) {
535 tmpsize = dmat->maxsegsz;
536 } else {
537 break;
538 }
539
540 /*
541 * Futz, split the data into a new segment.
542 */
543 if (seg >= dmat->nsegments)
544 goto fail;
545 sg[1].ds_len = sg[0].ds_len - tmpsize;
546 sg[1].ds_addr = sg[0].ds_addr + tmpsize;
547 sg[0].ds_len = tmpsize;
548 sg++;
549 seg++;
550 }
551
552 /*
553 * Adjust for loop
554 */
984263bc 555 buflen -= size;
270d2794 556 vaddr += size;
984263bc
MD
557 } while (buflen > 0);
558
270d2794 559fail:
984263bc 560 if (buflen != 0) {
26be20a0 561 kprintf("bus_dmamap_load: Too many segs! buf_len = 0x%lx\n",
984263bc
MD
562 (u_long)buflen);
563 error = EFBIG;
564 }
565
06577778 566 (*callback)(callback_arg, dmat->segments, seg, error);
984263bc
MD
567
568 return (0);
569}
570
571/*
572 * Utility function to load a linear buffer. lastaddrp holds state
573 * between invocations (for multiple-buffer loads). segp contains
574 * the starting segment on entrace, and the ending segment on exit.
575 * first indicates if this is the first invocation of this function.
576 */
577static int
578_bus_dmamap_load_buffer(bus_dma_tag_t dmat,
984263bc 579 void *buf, bus_size_t buflen,
dadab5e9 580 struct thread *td,
984263bc
MD
581 int flags,
582 vm_offset_t *lastaddrp,
583 int *segp,
584 int first)
585{
06577778 586 bus_dma_segment_t *segs;
984263bc
MD
587 bus_size_t sgsize;
588 bus_addr_t curaddr, lastaddr, baddr, bmask;
589 vm_offset_t vaddr = (vm_offset_t)buf;
590 int seg;
591 pmap_t pmap;
592
dadab5e9
MD
593 if (td->td_proc != NULL)
594 pmap = vmspace_pmap(td->td_proc->p_vmspace);
984263bc
MD
595 else
596 pmap = NULL;
597
06577778 598 segs = dmat->segments;
984263bc
MD
599 lastaddr = *lastaddrp;
600 bmask = ~(dmat->boundary - 1);
601
602 for (seg = *segp; buflen > 0 ; ) {
603 /*
604 * Get the physical address for this segment.
605 */
606 if (pmap)
607 curaddr = pmap_extract(pmap, vaddr);
608 else
609 curaddr = pmap_kextract(vaddr);
610
611 /*
612 * Compute the segment size, and adjust counts.
613 */
614 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK);
615 if (buflen < sgsize)
616 sgsize = buflen;
617
618 /*
619 * Make sure we don't cross any boundaries.
620 */
621 if (dmat->boundary > 0) {
622 baddr = (curaddr + dmat->boundary) & bmask;
623 if (sgsize > (baddr - curaddr))
624 sgsize = (baddr - curaddr);
625 }
626
627 /*
628 * Insert chunk into a segment, coalescing with
629 * previous segment if possible.
630 */
631 if (first) {
632 segs[seg].ds_addr = curaddr;
633 segs[seg].ds_len = sgsize;
634 first = 0;
635 } else {
636 if (curaddr == lastaddr &&
637 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz &&
638 (dmat->boundary == 0 ||
639 (segs[seg].ds_addr & bmask) == (curaddr & bmask)))
640 segs[seg].ds_len += sgsize;
641 else {
642 if (++seg >= dmat->nsegments)
643 break;
644 segs[seg].ds_addr = curaddr;
645 segs[seg].ds_len = sgsize;
646 }
647 }
648
649 lastaddr = curaddr + sgsize;
650 vaddr += sgsize;
651 buflen -= sgsize;
652 }
653
654 *segp = seg;
655 *lastaddrp = lastaddr;
656
657 /*
658 * Did we fit?
659 */
660 return (buflen != 0 ? EFBIG : 0); /* XXX better return value here? */
661}
662
663/*
664 * Like _bus_dmamap_load(), but for mbufs.
665 */
666int
667bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
668 struct mbuf *m0,
669 bus_dmamap_callback2_t *callback, void *callback_arg,
670 int flags)
671{
984263bc
MD
672 int nsegs, error;
673
674 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
675 ("bus_dmamap_load_mbuf: No support for bounce pages!"));
676 KASSERT(m0->m_flags & M_PKTHDR,
677 ("bus_dmamap_load_mbuf: no packet header"));
678
679 nsegs = 0;
680 error = 0;
681 if (m0->m_pkthdr.len <= dmat->maxsize) {
682 int first = 1;
683 vm_offset_t lastaddr = 0;
684 struct mbuf *m;
685
686 for (m = m0; m != NULL && error == 0; m = m->m_next) {
485dcc96
JS
687 if ( m->m_len == 0 )
688 continue;
984263bc 689 error = _bus_dmamap_load_buffer(dmat,
984263bc 690 m->m_data, m->m_len,
6b9fef06
MD
691 curthread, flags, &lastaddr,
692 &nsegs, first);
984263bc
MD
693 first = 0;
694 }
695 } else {
696 error = EINVAL;
697 }
698
699 if (error) {
700 /* force "no valid mappings" in callback */
06577778 701 (*callback)(callback_arg, dmat->segments, 0, 0, error);
984263bc 702 } else {
06577778 703 (*callback)(callback_arg, dmat->segments,
984263bc
MD
704 nsegs+1, m0->m_pkthdr.len, error);
705 }
706 return (error);
707}
708
709/*
710 * Like _bus_dmamap_load(), but for uios.
711 */
712int
713bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
714 struct uio *uio,
715 bus_dmamap_callback2_t *callback, void *callback_arg,
716 int flags)
717{
718 vm_offset_t lastaddr;
984263bc
MD
719 int nsegs, error, first, i;
720 bus_size_t resid;
721 struct iovec *iov;
dadab5e9 722 struct thread *td = NULL;
984263bc
MD
723
724 KASSERT(dmat->lowaddr >= ptoa(Maxmem) || map != NULL,
725 ("bus_dmamap_load_uio: No support for bounce pages!"));
726
727 resid = uio->uio_resid;
728 iov = uio->uio_iov;
729
730 if (uio->uio_segflg == UIO_USERSPACE) {
dadab5e9
MD
731 td = uio->uio_td;
732 KASSERT(td != NULL && td->td_proc != NULL,
984263bc
MD
733 ("bus_dmamap_load_uio: USERSPACE but no proc"));
734 }
735
736 nsegs = 0;
737 error = 0;
738 first = 1;
739 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
740 /*
741 * Now at the first iovec to load. Load each iovec
742 * until we have exhausted the residual count.
743 */
744 bus_size_t minlen =
745 resid < iov[i].iov_len ? resid : iov[i].iov_len;
746 caddr_t addr = (caddr_t) iov[i].iov_base;
747
748 error = _bus_dmamap_load_buffer(dmat,
984263bc 749 addr, minlen,
dadab5e9 750 td, flags, &lastaddr, &nsegs, first);
984263bc
MD
751 first = 0;
752
753 resid -= minlen;
754 }
755
756 if (error) {
757 /* force "no valid mappings" in callback */
06577778 758 (*callback)(callback_arg, dmat->segments, 0, 0, error);
984263bc 759 } else {
06577778 760 (*callback)(callback_arg, dmat->segments,
984263bc
MD
761 nsegs+1, uio->uio_resid, error);
762 }
763 return (error);
764}
765
766/*
767 * Release the mapping held by map.
768 */
769void
770_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map)
771{
772 struct bounce_page *bpage;
773
774 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
775 STAILQ_REMOVE_HEAD(&map->bpages, links);
776 free_bounce_page(dmat, bpage);
777 }
778}
779
780void
781_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op)
782{
783 struct bounce_page *bpage;
784
785 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) {
786
787 /*
788 * Handle data bouncing. We might also
789 * want to add support for invalidating
790 * the caches on broken hardware
791 */
792 switch (op) {
793 case BUS_DMASYNC_PREWRITE:
794 while (bpage != NULL) {
795 bcopy((void *)bpage->datavaddr,
796 (void *)bpage->vaddr,
797 bpage->datacount);
798 bpage = STAILQ_NEXT(bpage, links);
799 }
800 break;
801
802 case BUS_DMASYNC_POSTREAD:
803 while (bpage != NULL) {
804 bcopy((void *)bpage->vaddr,
805 (void *)bpage->datavaddr,
806 bpage->datacount);
807 bpage = STAILQ_NEXT(bpage, links);
808 }
809 break;
810 case BUS_DMASYNC_PREREAD:
811 case BUS_DMASYNC_POSTWRITE:
812 /* No-ops */
813 break;
814 }
815 }
816}
817
818static int
819alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages)
820{
821 int count;
822
823 count = 0;
824 if (total_bpages == 0) {
825 STAILQ_INIT(&bounce_page_list);
826 STAILQ_INIT(&bounce_map_waitinglist);
827 STAILQ_INIT(&bounce_map_callbacklist);
828 }
829
830 while (numpages > 0) {
831 struct bounce_page *bpage;
984263bc 832
efda3bd0 833 bpage = (struct bounce_page *)kmalloc(sizeof(*bpage), M_DEVBUF,
e7b4468c 834 M_INTWAIT | M_ZERO);
984263bc
MD
835
836 if (bpage == NULL)
837 break;
984263bc
MD
838 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF,
839 M_NOWAIT, 0ul,
840 dmat->lowaddr,
841 PAGE_SIZE,
842 0);
843 if (bpage->vaddr == NULL) {
efda3bd0 844 kfree(bpage, M_DEVBUF);
984263bc
MD
845 break;
846 }
847 bpage->busaddr = pmap_kextract(bpage->vaddr);
9acd5bbb 848 crit_enter();
984263bc
MD
849 STAILQ_INSERT_TAIL(&bounce_page_list, bpage, links);
850 total_bpages++;
851 free_bpages++;
9acd5bbb 852 crit_exit();
984263bc
MD
853 count++;
854 numpages--;
855 }
856 return (count);
857}
858
859static int
860reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map)
861{
862 int pages;
863
864 pages = MIN(free_bpages, map->pagesneeded - map->pagesreserved);
865 free_bpages -= pages;
866 reserved_bpages += pages;
867 map->pagesreserved += pages;
868 pages = map->pagesneeded - map->pagesreserved;
869
870 return (pages);
871}
872
6ef943a3 873static bus_addr_t
984263bc
MD
874add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr,
875 bus_size_t size)
876{
984263bc
MD
877 struct bounce_page *bpage;
878
879 if (map->pagesneeded == 0)
880 panic("add_bounce_page: map doesn't need any pages");
881 map->pagesneeded--;
882
883 if (map->pagesreserved == 0)
884 panic("add_bounce_page: map doesn't need any pages");
885 map->pagesreserved--;
886
9acd5bbb 887 crit_enter();
984263bc
MD
888 bpage = STAILQ_FIRST(&bounce_page_list);
889 if (bpage == NULL)
890 panic("add_bounce_page: free page list is empty");
891
892 STAILQ_REMOVE_HEAD(&bounce_page_list, links);
893 reserved_bpages--;
894 active_bpages++;
9acd5bbb 895 crit_exit();
984263bc
MD
896
897 bpage->datavaddr = vaddr;
898 bpage->datacount = size;
899 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links);
900 return (bpage->busaddr);
901}
902
903static void
904free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage)
905{
984263bc
MD
906 struct bus_dmamap *map;
907
908 bpage->datavaddr = 0;
909 bpage->datacount = 0;
910
9acd5bbb 911 crit_enter();
984263bc
MD
912 STAILQ_INSERT_HEAD(&bounce_page_list, bpage, links);
913 free_bpages++;
914 active_bpages--;
915 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) {
916 if (reserve_bounce_pages(map->dmat, map) == 0) {
917 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links);
918 STAILQ_INSERT_TAIL(&bounce_map_callbacklist,
919 map, links);
920 busdma_swi_pending = 1;
921 setsoftvm();
922 }
923 }
9acd5bbb 924 crit_exit();
984263bc
MD
925}
926
927void
f123d5a1 928busdma_swi(void)
984263bc 929{
984263bc
MD
930 struct bus_dmamap *map;
931
9acd5bbb 932 crit_enter();
984263bc
MD
933 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) {
934 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links);
9acd5bbb 935 crit_exit();
984263bc
MD
936 bus_dmamap_load(map->dmat, map, map->buf, map->buflen,
937 map->callback, map->callback_arg, /*flags*/0);
9acd5bbb 938 crit_enter();
984263bc 939 }
9acd5bbb 940 crit_exit();
984263bc 941}