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