* Allocate a handle for mapping from kva/uva/physical * address space into bus device space. */
| 545 | * address space into bus device space. |
| 546 | */ |
| 547 | int |
| 548 | bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp) |
| 549 | { |
| 550 | bus_dmamap_t newmap; |
| 551 | int error = 0; |
| 552 | |
| 553 | if (dmat->segments == NULL) { |
| 554 | dmat->segments = (bus_dma_segment_t *)malloc( |
| 555 | sizeof(bus_dma_segment_t) * dmat->nsegments, M_BUSDMA, |
| 556 | M_NOWAIT); |
| 557 | if (dmat->segments == NULL) { |
| 558 | CTR3(KTR_BUSDMA, "%s: tag %p error %d", |
| 559 | __func__, dmat, ENOMEM); |
| 560 | return (ENOMEM); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | newmap = _busdma_alloc_dmamap(dmat); |
| 565 | if (newmap == NULL) { |
| 566 | CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM); |
| 567 | return (ENOMEM); |
| 568 | } |
| 569 | *mapp = newmap; |
| 570 | |
| 571 | /* |
| 572 | * Bouncing might be required if the driver asks for an active |
| 573 | * exclusion region, a data alignment that is stricter than 1, and/or |
| 574 | * an active address boundary. |
| 575 | */ |
| 576 | if (dmat->flags & BUS_DMA_COULD_BOUNCE) { |
| 577 | /* Must bounce */ |
| 578 | struct bounce_zone *bz; |
| 579 | int maxpages; |
| 580 | |
| 581 | if (dmat->bounce_zone == NULL) { |
| 582 | if ((error = alloc_bounce_zone(dmat)) != 0) { |
| 583 | _busdma_free_dmamap(newmap); |
| 584 | *mapp = NULL; |
| 585 | return (error); |
| 586 | } |
| 587 | } |
| 588 | bz = dmat->bounce_zone; |
| 589 | |
| 590 | /* Initialize the new map */ |
| 591 | STAILQ_INIT(&((*mapp)->bpages)); |
| 592 | |
| 593 | /* |
| 594 | * Attempt to add pages to our pool on a per-instance |
| 595 | * basis up to a sane limit. |
| 596 | */ |
| 597 | maxpages = MAX_BPAGES; |
| 598 | if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 |
| 599 | || (bz->map_count > 0 && bz->total_bpages < maxpages)) { |
| 600 | int pages; |
| 601 | |
| 602 | pages = MAX(atop(dmat->maxsize), 1); |
| 603 | pages = MIN(maxpages - bz->total_bpages, pages); |
| 604 | pages = MAX(pages, 1); |
no test coverage detected