busdma_machdep-v6.c revision 274191
1/*- 2 * Copyright (c) 2012 Ian Lepore 3 * Copyright (c) 2010 Mark Tinguely 4 * Copyright (c) 2004 Olivier Houchard 5 * Copyright (c) 2002 Peter Grehan 6 * Copyright (c) 1997, 1998 Justin T. Gibbs. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification, immediately at the beginning of the file. 15 * 2. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * From i386/busdma_machdep.c 191438 2009-04-23 20:24:19Z jhb 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/arm/arm/busdma_machdep-v6.c 274191 2014-11-06 19:14:58Z ian $"); 35 36#define _ARM32_BUS_DMA_PRIVATE 37#include <sys/param.h> 38#include <sys/kdb.h> 39#include <ddb/ddb.h> 40#include <ddb/db_output.h> 41#include <sys/systm.h> 42#include <sys/malloc.h> 43#include <sys/bus.h> 44#include <sys/busdma_bufalloc.h> 45#include <sys/counter.h> 46#include <sys/interrupt.h> 47#include <sys/kernel.h> 48#include <sys/ktr.h> 49#include <sys/lock.h> 50#include <sys/memdesc.h> 51#include <sys/proc.h> 52#include <sys/mutex.h> 53#include <sys/sysctl.h> 54#include <sys/uio.h> 55 56#include <vm/vm.h> 57#include <vm/vm_page.h> 58#include <vm/vm_map.h> 59#include <vm/vm_extern.h> 60#include <vm/vm_kern.h> 61 62#include <machine/atomic.h> 63#include <machine/bus.h> 64#include <machine/cpufunc.h> 65#include <machine/md_var.h> 66 67#define IS_POWER_OF_2(val) (((val) & ((val) - 1)) == 0) 68 69#define MAX_BPAGES 64 70#define MAX_DMA_SEGMENTS 4096 71#define BUS_DMA_EXCL_BOUNCE BUS_DMA_BUS2 72#define BUS_DMA_ALIGN_BOUNCE BUS_DMA_BUS3 73#define BUS_DMA_COULD_BOUNCE (BUS_DMA_EXCL_BOUNCE | BUS_DMA_ALIGN_BOUNCE) 74#define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4 75 76struct bounce_zone; 77 78struct bus_dma_tag { 79 bus_dma_tag_t parent; 80 bus_size_t alignment; 81 bus_size_t boundary; 82 bus_addr_t lowaddr; 83 bus_addr_t highaddr; 84 bus_dma_filter_t *filter; 85 void *filterarg; 86 bus_size_t maxsize; 87 u_int nsegments; 88 bus_size_t maxsegsz; 89 int flags; 90 int ref_count; 91 int map_count; 92 bus_dma_lock_t *lockfunc; 93 void *lockfuncarg; 94 struct bounce_zone *bounce_zone; 95 /* 96 * DMA range for this tag. If the page doesn't fall within 97 * one of these ranges, an error is returned. The caller 98 * may then decide what to do with the transfer. If the 99 * range pointer is NULL, it is ignored. 100 */ 101 struct arm32_dma_range *ranges; 102 int _nranges; 103}; 104 105struct bounce_page { 106 vm_offset_t vaddr; /* kva of bounce buffer */ 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 114struct 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 120int busdma_swi_pending; 121 122struct 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 140static struct mtx bounce_lock; 141static int total_bpages; 142static int busdma_zonecount; 143static uint32_t tags_total; 144static uint32_t maps_total; 145static uint32_t maps_dmamem; 146static uint32_t maps_coherent; 147static counter_u64_t maploads_total; 148static counter_u64_t maploads_bounced; 149static counter_u64_t maploads_coherent; 150static counter_u64_t maploads_dmamem; 151static counter_u64_t maploads_mbuf; 152static counter_u64_t maploads_physmem; 153 154static STAILQ_HEAD(, bounce_zone) bounce_zone_list; 155 156SYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters"); 157SYSCTL_UINT(_hw_busdma, OID_AUTO, tags_total, CTLFLAG_RD, &tags_total, 0, 158 "Number of active tags"); 159SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_total, CTLFLAG_RD, &maps_total, 0, 160 "Number of active maps"); 161SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_dmamem, CTLFLAG_RD, &maps_dmamem, 0, 162 "Number of active maps for bus_dmamem_alloc buffers"); 163SYSCTL_UINT(_hw_busdma, OID_AUTO, maps_coherent, CTLFLAG_RD, &maps_coherent, 0, 164 "Number of active maps with BUS_DMA_COHERENT flag set"); 165SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_total, CTLFLAG_RD, 166 &maploads_total, "Number of load operations performed"); 167SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_bounced, CTLFLAG_RD, 168 &maploads_bounced, "Number of load operations that used bounce buffers"); 169SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_coherent, CTLFLAG_RD, 170 &maploads_dmamem, "Number of load operations on BUS_DMA_COHERENT memory"); 171SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_dmamem, CTLFLAG_RD, 172 &maploads_dmamem, "Number of load operations on bus_dmamem_alloc buffers"); 173SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_mbuf, CTLFLAG_RD, 174 &maploads_mbuf, "Number of load operations for mbufs"); 175SYSCTL_COUNTER_U64(_hw_busdma, OID_AUTO, maploads_physmem, CTLFLAG_RD, 176 &maploads_physmem, "Number of load operations on physical buffers"); 177SYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0, 178 "Total bounce pages"); 179 180struct bus_dmamap { 181 struct bp_list bpages; 182 int pagesneeded; 183 int pagesreserved; 184 bus_dma_tag_t dmat; 185 struct memdesc mem; 186 pmap_t pmap; 187 bus_dmamap_callback_t *callback; 188 void *callback_arg; 189 int flags; 190#define DMAMAP_COHERENT (1 << 0) 191#define DMAMAP_DMAMEM_ALLOC (1 << 1) 192#define DMAMAP_MBUF (1 << 2) 193 STAILQ_ENTRY(bus_dmamap) links; 194 bus_dma_segment_t *segments; 195 int sync_count; 196 struct sync_list slist[]; 197}; 198 199static STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist; 200static STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist; 201 202static void init_bounce_pages(void *dummy); 203static int alloc_bounce_zone(bus_dma_tag_t dmat); 204static int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages); 205static int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 206 int commit); 207static bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, 208 vm_offset_t vaddr, bus_addr_t addr, 209 bus_size_t size); 210static void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage); 211static void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 212 void *buf, bus_size_t buflen, int flags); 213static void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, 214 vm_paddr_t buf, bus_size_t buflen, int flags); 215static int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 216 int flags); 217 218static busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */ 219static busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */ 220static void 221busdma_init(void *dummy) 222{ 223 int uma_flags; 224 225 maploads_total = counter_u64_alloc(M_WAITOK); 226 maploads_bounced = counter_u64_alloc(M_WAITOK); 227 maploads_coherent = counter_u64_alloc(M_WAITOK); 228 maploads_dmamem = counter_u64_alloc(M_WAITOK); 229 maploads_mbuf = counter_u64_alloc(M_WAITOK); 230 maploads_physmem = counter_u64_alloc(M_WAITOK); 231 232 uma_flags = 0; 233 234 /* Create a cache of buffers in standard (cacheable) memory. */ 235 standard_allocator = busdma_bufalloc_create("buffer", 236 arm_dcache_align, /* minimum_alignment */ 237 NULL, /* uma_alloc func */ 238 NULL, /* uma_free func */ 239 uma_flags); /* uma_zcreate_flags */ 240 241#ifdef INVARIANTS 242 /* 243 * Force UMA zone to allocate service structures like 244 * slabs using own allocator. uma_debug code performs 245 * atomic ops on uma_slab_t fields and safety of this 246 * operation is not guaranteed for write-back caches 247 */ 248 uma_flags = UMA_ZONE_OFFPAGE; 249#endif 250 /* 251 * Create a cache of buffers in uncacheable memory, to implement the 252 * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag. 253 */ 254 coherent_allocator = busdma_bufalloc_create("coherent", 255 arm_dcache_align, /* minimum_alignment */ 256 busdma_bufalloc_alloc_uncacheable, 257 busdma_bufalloc_free_uncacheable, 258 uma_flags); /* uma_zcreate_flags */ 259} 260 261/* 262 * This init historically used SI_SUB_VM, but now the init code requires 263 * malloc(9) using M_DEVBUF memory and the pcpu zones for counter(9), which get 264 * set up by SI_SUB_KMEM and SI_ORDER_LAST, so we'll go right after that by 265 * using SI_SUB_KMEM+1. 266 */ 267SYSINIT(busdma, SI_SUB_KMEM+1, SI_ORDER_FIRST, busdma_init, NULL); 268 269/* 270 * This routine checks the exclusion zone constraints from a tag against the 271 * physical RAM available on the machine. If a tag specifies an exclusion zone 272 * but there's no RAM in that zone, then we avoid allocating resources to bounce 273 * a request, and we can use any memory allocator (as opposed to needing 274 * kmem_alloc_contig() just because it can allocate pages in an address range). 275 * 276 * Most tags have BUS_SPACE_MAXADDR or BUS_SPACE_MAXADDR_32BIT (they are the 277 * same value on 32-bit architectures) as their lowaddr constraint, and we can't 278 * possibly have RAM at an address higher than the highest address we can 279 * express, so we take a fast out. 280 */ 281static int 282exclusion_bounce_check(vm_offset_t lowaddr, vm_offset_t highaddr) 283{ 284 int i; 285 286 if (lowaddr >= BUS_SPACE_MAXADDR) 287 return (0); 288 289 for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) { 290 if ((lowaddr >= phys_avail[i] && lowaddr < phys_avail[i + 1]) || 291 (lowaddr < phys_avail[i] && highaddr >= phys_avail[i])) 292 return (1); 293 } 294 return (0); 295} 296 297/* 298 * Return true if the tag has an exclusion zone that could lead to bouncing. 299 */ 300static __inline int 301exclusion_bounce(bus_dma_tag_t dmat) 302{ 303 304 return (dmat->flags & BUS_DMA_EXCL_BOUNCE); 305} 306 307/* 308 * Return true if the given address does not fall on the alignment boundary. 309 */ 310static __inline int 311alignment_bounce(bus_dma_tag_t dmat, bus_addr_t addr) 312{ 313 314 return (addr & (dmat->alignment - 1)); 315} 316 317/* 318 * Return true if the DMA should bounce because the start or end does not fall 319 * on a cacheline boundary (which would require a partial cacheline flush). 320 * COHERENT memory doesn't trigger cacheline flushes. Memory allocated by 321 * bus_dmamem_alloc() is always aligned to cacheline boundaries, and there's a 322 * strict rule that such memory cannot be accessed by the CPU while DMA is in 323 * progress (or by multiple DMA engines at once), so that it's always safe to do 324 * full cacheline flushes even if that affects memory outside the range of a 325 * given DMA operation that doesn't involve the full allocated buffer. If we're 326 * mapping an mbuf, that follows the same rules as a buffer we allocated. 327 */ 328static __inline int 329cacheline_bounce(bus_dmamap_t map, bus_addr_t addr, bus_size_t size) 330{ 331 332 if (map->flags & (DMAMAP_DMAMEM_ALLOC | DMAMAP_COHERENT | DMAMAP_MBUF)) 333 return (0); 334 return ((addr | size) & arm_dcache_align_mask); 335} 336 337/* 338 * Return true if we might need to bounce the DMA described by addr and size. 339 * 340 * This is used to quick-check whether we need to do the more expensive work of 341 * checking the DMA page-by-page looking for alignment and exclusion bounces. 342 * 343 * Note that the addr argument might be either virtual or physical. It doesn't 344 * matter because we only look at the low-order bits, which are the same in both 345 * address spaces. 346 */ 347static __inline int 348might_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t addr, 349 bus_size_t size) 350{ 351 return ((dmat->flags & BUS_DMA_EXCL_BOUNCE) || 352 alignment_bounce(dmat, addr) || 353 cacheline_bounce(map, addr, size)); 354} 355 356/* 357 * Return true if we must bounce the DMA described by paddr and size. 358 * 359 * Bouncing can be triggered by DMA that doesn't begin and end on cacheline 360 * boundaries, or doesn't begin on an alignment boundary, or falls within the 361 * exclusion zone of any tag in the ancestry chain. 362 * 363 * For exclusions, walk the chain of tags comparing paddr to the exclusion zone 364 * within each tag. If the tag has a filter function, use it to decide whether 365 * the DMA needs to bounce, otherwise any DMA within the zone bounces. 366 */ 367static int 368must_bounce(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t paddr, 369 bus_size_t size) 370{ 371 372 if (cacheline_bounce(map, paddr, size)) 373 return (1); 374 375 /* 376 * The tag already contains ancestors' alignment restrictions so this 377 * check doesn't need to be inside the loop. 378 */ 379 if (alignment_bounce(dmat, paddr)) 380 return (1); 381 382 /* 383 * Even though each tag has an exclusion zone that is a superset of its 384 * own and all its ancestors' exclusions, the exclusion zone of each tag 385 * up the chain must be checked within the loop, because the busdma 386 * rules say the filter function is called only when the address lies 387 * within the low-highaddr range of the tag that filterfunc belongs to. 388 */ 389 while (dmat != NULL && exclusion_bounce(dmat)) { 390 if ((paddr >= dmat->lowaddr && paddr <= dmat->highaddr) && 391 (dmat->filter == NULL || 392 dmat->filter(dmat->filterarg, paddr) != 0)) 393 return (1); 394 dmat = dmat->parent; 395 } 396 397 return (0); 398} 399 400static __inline struct arm32_dma_range * 401_bus_dma_inrange(struct arm32_dma_range *ranges, int nranges, 402 bus_addr_t curaddr) 403{ 404 struct arm32_dma_range *dr; 405 int i; 406 407 for (i = 0, dr = ranges; i < nranges; i++, dr++) { 408 if (curaddr >= dr->dr_sysbase && 409 round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len)) 410 return (dr); 411 } 412 413 return (NULL); 414} 415 416/* 417 * Convenience function for manipulating driver locks from busdma (during 418 * busdma_swi, for example). Drivers that don't provide their own locks 419 * should specify &Giant to dmat->lockfuncarg. Drivers that use their own 420 * non-mutex locking scheme don't have to use this at all. 421 */ 422void 423busdma_lock_mutex(void *arg, bus_dma_lock_op_t op) 424{ 425 struct mtx *dmtx; 426 427 dmtx = (struct mtx *)arg; 428 switch (op) { 429 case BUS_DMA_LOCK: 430 mtx_lock(dmtx); 431 break; 432 case BUS_DMA_UNLOCK: 433 mtx_unlock(dmtx); 434 break; 435 default: 436 panic("Unknown operation 0x%x for busdma_lock_mutex!", op); 437 } 438} 439 440/* 441 * dflt_lock should never get called. It gets put into the dma tag when 442 * lockfunc == NULL, which is only valid if the maps that are associated 443 * with the tag are meant to never be defered. 444 * XXX Should have a way to identify which driver is responsible here. 445 */ 446static void 447dflt_lock(void *arg, bus_dma_lock_op_t op) 448{ 449 panic("driver error: busdma dflt_lock called"); 450} 451 452/* 453 * Allocate a device specific dma_tag. 454 */ 455int 456bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment, 457 bus_size_t boundary, bus_addr_t lowaddr, 458 bus_addr_t highaddr, bus_dma_filter_t *filter, 459 void *filterarg, bus_size_t maxsize, int nsegments, 460 bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc, 461 void *lockfuncarg, bus_dma_tag_t *dmat) 462{ 463 bus_dma_tag_t newtag; 464 int error = 0; 465 466#if 0 467 if (!parent) 468 parent = arm_root_dma_tag; 469#endif 470 471 /* Basic sanity checking. */ 472 KASSERT(boundary == 0 || IS_POWER_OF_2(boundary), 473 ("dma tag boundary %lu, must be a power of 2", boundary)); 474 KASSERT(boundary == 0 || boundary >= maxsegsz, 475 ("dma tag boundary %lu is < maxsegsz %lu\n", boundary, maxsegsz)); 476 KASSERT(alignment != 0 && IS_POWER_OF_2(alignment), 477 ("dma tag alignment %lu, must be non-zero power of 2", alignment)); 478 KASSERT(maxsegsz != 0, ("dma tag maxsegsz must not be zero")); 479 480 /* Return a NULL tag on failure */ 481 *dmat = NULL; 482 483 newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, 484 M_ZERO | M_NOWAIT); 485 if (newtag == NULL) { 486 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 487 __func__, newtag, 0, error); 488 return (ENOMEM); 489 } 490 491 newtag->parent = parent; 492 newtag->alignment = alignment; 493 newtag->boundary = boundary; 494 newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1); 495 newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + 496 (PAGE_SIZE - 1); 497 newtag->filter = filter; 498 newtag->filterarg = filterarg; 499 newtag->maxsize = maxsize; 500 newtag->nsegments = nsegments; 501 newtag->maxsegsz = maxsegsz; 502 newtag->flags = flags; 503 newtag->ref_count = 1; /* Count ourself */ 504 newtag->map_count = 0; 505 newtag->ranges = bus_dma_get_range(); 506 newtag->_nranges = bus_dma_get_range_nb(); 507 if (lockfunc != NULL) { 508 newtag->lockfunc = lockfunc; 509 newtag->lockfuncarg = lockfuncarg; 510 } else { 511 newtag->lockfunc = dflt_lock; 512 newtag->lockfuncarg = NULL; 513 } 514 515 /* Take into account any restrictions imposed by our parent tag */ 516 if (parent != NULL) { 517 newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr); 518 newtag->highaddr = MAX(parent->highaddr, newtag->highaddr); 519 newtag->alignment = MAX(parent->alignment, newtag->alignment); 520 newtag->flags |= parent->flags & BUS_DMA_COULD_BOUNCE; 521 if (newtag->boundary == 0) 522 newtag->boundary = parent->boundary; 523 else if (parent->boundary != 0) 524 newtag->boundary = MIN(parent->boundary, 525 newtag->boundary); 526 if (newtag->filter == NULL) { 527 /* 528 * Short circuit to looking at our parent directly 529 * since we have encapsulated all of its information 530 */ 531 newtag->filter = parent->filter; 532 newtag->filterarg = parent->filterarg; 533 newtag->parent = parent->parent; 534 } 535 if (newtag->parent != NULL) 536 atomic_add_int(&parent->ref_count, 1); 537 } 538 539 if (exclusion_bounce_check(newtag->lowaddr, newtag->highaddr)) 540 newtag->flags |= BUS_DMA_EXCL_BOUNCE; 541 if (alignment_bounce(newtag, 1)) 542 newtag->flags |= BUS_DMA_ALIGN_BOUNCE; 543 544 /* 545 * Any request can auto-bounce due to cacheline alignment, in addition 546 * to any alignment or boundary specifications in the tag, so if the 547 * ALLOCNOW flag is set, there's always work to do. 548 */ 549 if ((flags & BUS_DMA_ALLOCNOW) != 0) { 550 struct bounce_zone *bz; 551 /* 552 * Round size up to a full page, and add one more page because 553 * there can always be one more boundary crossing than the 554 * number of pages in a transfer. 555 */ 556 maxsize = roundup2(maxsize, PAGE_SIZE) + PAGE_SIZE; 557 558 if ((error = alloc_bounce_zone(newtag)) != 0) { 559 free(newtag, M_DEVBUF); 560 return (error); 561 } 562 bz = newtag->bounce_zone; 563 564 if (ptoa(bz->total_bpages) < maxsize) { 565 int pages; 566 567 pages = atop(maxsize) - bz->total_bpages; 568 569 /* Add pages to our bounce pool */ 570 if (alloc_bounce_pages(newtag, pages) < pages) 571 error = ENOMEM; 572 } 573 /* Performed initial allocation */ 574 newtag->flags |= BUS_DMA_MIN_ALLOC_COMP; 575 } else 576 newtag->bounce_zone = NULL; 577 578 if (error != 0) { 579 free(newtag, M_DEVBUF); 580 } else { 581 atomic_add_32(&tags_total, 1); 582 *dmat = newtag; 583 } 584 CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 585 __func__, newtag, (newtag != NULL ? newtag->flags : 0), error); 586 return (error); 587} 588 589int 590bus_dma_tag_destroy(bus_dma_tag_t dmat) 591{ 592 bus_dma_tag_t dmat_copy; 593 int error; 594 595 error = 0; 596 dmat_copy = dmat; 597 598 if (dmat != NULL) { 599 600 if (dmat->map_count != 0) { 601 error = EBUSY; 602 goto out; 603 } 604 605 while (dmat != NULL) { 606 bus_dma_tag_t parent; 607 608 parent = dmat->parent; 609 atomic_subtract_int(&dmat->ref_count, 1); 610 if (dmat->ref_count == 0) { 611 atomic_subtract_32(&tags_total, 1); 612 free(dmat, M_DEVBUF); 613 /* 614 * Last reference count, so 615 * release our reference 616 * count on our parent. 617 */ 618 dmat = parent; 619 } else 620 dmat = NULL; 621 } 622 } 623out: 624 CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error); 625 return (error); 626} 627 628static int allocate_bz_and_pages(bus_dma_tag_t dmat, bus_dmamap_t mapp) 629{ 630 struct bounce_zone *bz; 631 int maxpages; 632 int error; 633 634 if (dmat->bounce_zone == NULL) 635 if ((error = alloc_bounce_zone(dmat)) != 0) 636 return (error); 637 bz = dmat->bounce_zone; 638 /* Initialize the new map */ 639 STAILQ_INIT(&(mapp->bpages)); 640 641 /* 642 * Attempt to add pages to our pool on a per-instance basis up to a sane 643 * limit. Even if the tag isn't flagged as COULD_BOUNCE due to 644 * alignment and boundary constraints, it could still auto-bounce due to 645 * cacheline alignment, which requires at most two bounce pages. 646 */ 647 if (dmat->flags & BUS_DMA_COULD_BOUNCE) 648 maxpages = MAX_BPAGES; 649 else 650 maxpages = 2 * bz->map_count; 651 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 || 652 (bz->map_count > 0 && bz->total_bpages < maxpages)) { 653 int pages; 654 655 pages = atop(roundup2(dmat->maxsize, PAGE_SIZE)) + 1; 656 pages = MIN(maxpages - bz->total_bpages, pages); 657 pages = MAX(pages, 2); 658 if (alloc_bounce_pages(dmat, pages) < pages) 659 return (ENOMEM); 660 661 if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) 662 dmat->flags |= BUS_DMA_MIN_ALLOC_COMP; 663 } 664 bz->map_count++; 665 return (0); 666} 667 668static bus_dmamap_t 669allocate_map(bus_dma_tag_t dmat, int mflags) 670{ 671 int mapsize, segsize; 672 bus_dmamap_t map; 673 674 /* 675 * Allocate the map. The map structure ends with an embedded 676 * variable-sized array of sync_list structures. Following that 677 * we allocate enough extra space to hold the array of bus_dma_segments. 678 */ 679 KASSERT(dmat->nsegments <= MAX_DMA_SEGMENTS, 680 ("cannot allocate %u dma segments (max is %u)", 681 dmat->nsegments, MAX_DMA_SEGMENTS)); 682 segsize = sizeof(struct bus_dma_segment) * dmat->nsegments; 683 mapsize = sizeof(*map) + sizeof(struct sync_list) * dmat->nsegments; 684 map = malloc(mapsize + segsize, M_DEVBUF, mflags | M_ZERO); 685 if (map == NULL) { 686 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM); 687 return (NULL); 688 } 689 map->segments = (bus_dma_segment_t *)((uintptr_t)map + mapsize); 690 return (map); 691} 692 693/* 694 * Allocate a handle for mapping from kva/uva/physical 695 * address space into bus device space. 696 */ 697int 698bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp) 699{ 700 bus_dmamap_t map; 701 int error = 0; 702 703 *mapp = map = allocate_map(dmat, M_NOWAIT); 704 if (map == NULL) { 705 CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM); 706 return (ENOMEM); 707 } 708 709 /* 710 * Bouncing might be required if the driver asks for an exclusion 711 * region, a data alignment that is stricter than 1, or DMA that begins 712 * or ends with a partial cacheline. Whether bouncing will actually 713 * happen can't be known until mapping time, but we need to pre-allocate 714 * resources now because we might not be allowed to at mapping time. 715 */ 716 error = allocate_bz_and_pages(dmat, map); 717 if (error != 0) { 718 free(map, M_DEVBUF); 719 *mapp = NULL; 720 return (error); 721 } 722 if (map->flags & DMAMAP_COHERENT) 723 atomic_add_32(&maps_coherent, 1); 724 atomic_add_32(&maps_total, 1); 725 dmat->map_count++; 726 727 return (0); 728} 729 730/* 731 * Destroy a handle for mapping from kva/uva/physical 732 * address space into bus device space. 733 */ 734int 735bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map) 736{ 737 if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) { 738 CTR3(KTR_BUSDMA, "%s: tag %p error %d", 739 __func__, dmat, EBUSY); 740 return (EBUSY); 741 } 742 if (dmat->bounce_zone) 743 dmat->bounce_zone->map_count--; 744 if (map->flags & DMAMAP_COHERENT) 745 atomic_subtract_32(&maps_coherent, 1); 746 atomic_subtract_32(&maps_total, 1); 747 free(map, M_DEVBUF); 748 dmat->map_count--; 749 CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat); 750 return (0); 751} 752 753 754/* 755 * Allocate a piece of memory that can be efficiently mapped into 756 * bus device space based on the constraints lited in the dma tag. 757 * A dmamap to for use with dmamap_load is also allocated. 758 */ 759int 760bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags, 761 bus_dmamap_t *mapp) 762{ 763 busdma_bufalloc_t ba; 764 struct busdma_bufzone *bufzone; 765 bus_dmamap_t map; 766 vm_memattr_t memattr; 767 int mflags; 768 769 if (flags & BUS_DMA_NOWAIT) 770 mflags = M_NOWAIT; 771 else 772 mflags = M_WAITOK; 773 if (flags & BUS_DMA_ZERO) 774 mflags |= M_ZERO; 775 776 *mapp = map = allocate_map(dmat, mflags); 777 if (map == NULL) { 778 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 779 __func__, dmat, dmat->flags, ENOMEM); 780 return (ENOMEM); 781 } 782 map->flags = DMAMAP_DMAMEM_ALLOC; 783 784 /* Choose a busdma buffer allocator based on memory type flags. */ 785 if (flags & BUS_DMA_COHERENT) { 786 memattr = VM_MEMATTR_UNCACHEABLE; 787 ba = coherent_allocator; 788 map->flags |= DMAMAP_COHERENT; 789 } else { 790 memattr = VM_MEMATTR_DEFAULT; 791 ba = standard_allocator; 792 } 793 794 /* 795 * Try to find a bufzone in the allocator that holds a cache of buffers 796 * of the right size for this request. If the buffer is too big to be 797 * held in the allocator cache, this returns NULL. 798 */ 799 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 800 801 /* 802 * Allocate the buffer from the uma(9) allocator if... 803 * - It's small enough to be in the allocator (bufzone not NULL). 804 * - The alignment constraint isn't larger than the allocation size 805 * (the allocator aligns buffers to their size boundaries). 806 * - There's no need to handle lowaddr/highaddr exclusion zones. 807 * else allocate non-contiguous pages if... 808 * - The page count that could get allocated doesn't exceed nsegments. 809 * - The alignment constraint isn't larger than a page boundary. 810 * - There are no boundary-crossing constraints. 811 * else allocate a block of contiguous pages because one or more of the 812 * constraints is something that only the contig allocator can fulfill. 813 */ 814 if (bufzone != NULL && dmat->alignment <= bufzone->size && 815 !exclusion_bounce(dmat)) { 816 *vaddr = uma_zalloc(bufzone->umazone, mflags); 817 } else if (dmat->nsegments >= btoc(dmat->maxsize) && 818 dmat->alignment <= PAGE_SIZE && dmat->boundary == 0) { 819 *vaddr = (void *)kmem_alloc_attr(kernel_arena, dmat->maxsize, 820 mflags, 0, dmat->lowaddr, memattr); 821 } else { 822 *vaddr = (void *)kmem_alloc_contig(kernel_arena, dmat->maxsize, 823 mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary, 824 memattr); 825 } 826 827 828 if (*vaddr == NULL) { 829 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 830 __func__, dmat, dmat->flags, ENOMEM); 831 free(map, M_DEVBUF); 832 *mapp = NULL; 833 return (ENOMEM); 834 } 835 if (map->flags & DMAMAP_COHERENT) 836 atomic_add_32(&maps_coherent, 1); 837 atomic_add_32(&maps_dmamem, 1); 838 atomic_add_32(&maps_total, 1); 839 dmat->map_count++; 840 841 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 842 __func__, dmat, dmat->flags, 0); 843 return (0); 844} 845 846/* 847 * Free a piece of memory and it's allociated dmamap, that was allocated 848 * via bus_dmamem_alloc. Make the same choice for free/contigfree. 849 */ 850void 851bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map) 852{ 853 struct busdma_bufzone *bufzone; 854 busdma_bufalloc_t ba; 855 856 if (map->flags & DMAMAP_COHERENT) 857 ba = coherent_allocator; 858 else 859 ba = standard_allocator; 860 861 /* Be careful not to access map from here on. */ 862 863 bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 864 865 if (bufzone != NULL && dmat->alignment <= bufzone->size && 866 !exclusion_bounce(dmat)) 867 uma_zfree(bufzone->umazone, vaddr); 868 else 869 kmem_free(kernel_arena, (vm_offset_t)vaddr, dmat->maxsize); 870 871 dmat->map_count--; 872 if (map->flags & DMAMAP_COHERENT) 873 atomic_subtract_32(&maps_coherent, 1); 874 atomic_subtract_32(&maps_total, 1); 875 atomic_subtract_32(&maps_dmamem, 1); 876 free(map, M_DEVBUF); 877 CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags); 878} 879 880static void 881_bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf, 882 bus_size_t buflen, int flags) 883{ 884 bus_addr_t curaddr; 885 bus_size_t sgsize; 886 887 if (map->pagesneeded == 0) { 888 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d" 889 " map= %p, pagesneeded= %d", 890 dmat->lowaddr, dmat->boundary, dmat->alignment, 891 map, map->pagesneeded); 892 /* 893 * Count the number of bounce pages 894 * needed in order to complete this transfer 895 */ 896 curaddr = buf; 897 while (buflen != 0) { 898 sgsize = MIN(buflen, dmat->maxsegsz); 899 if (must_bounce(dmat, map, curaddr, sgsize) != 0) { 900 sgsize = MIN(sgsize, PAGE_SIZE); 901 map->pagesneeded++; 902 } 903 curaddr += sgsize; 904 buflen -= sgsize; 905 } 906 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded); 907 } 908} 909 910static void 911_bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 912 void *buf, bus_size_t buflen, int flags) 913{ 914 vm_offset_t vaddr; 915 vm_offset_t vendaddr; 916 bus_addr_t paddr; 917 918 if (map->pagesneeded == 0) { 919 CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d" 920 " map= %p, pagesneeded= %d", 921 dmat->lowaddr, dmat->boundary, dmat->alignment, 922 map, map->pagesneeded); 923 /* 924 * Count the number of bounce pages 925 * needed in order to complete this transfer 926 */ 927 vaddr = (vm_offset_t)buf; 928 vendaddr = (vm_offset_t)buf + buflen; 929 930 while (vaddr < vendaddr) { 931 if (__predict_true(map->pmap == kernel_pmap)) 932 paddr = pmap_kextract(vaddr); 933 else 934 paddr = pmap_extract(map->pmap, vaddr); 935 if (must_bounce(dmat, map, paddr, 936 min(vendaddr - vaddr, (PAGE_SIZE - ((vm_offset_t)vaddr & 937 PAGE_MASK)))) != 0) { 938 map->pagesneeded++; 939 } 940 vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK)); 941 942 } 943 CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded); 944 } 945} 946 947static int 948_bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags) 949{ 950 951 /* Reserve Necessary Bounce Pages */ 952 mtx_lock(&bounce_lock); 953 if (flags & BUS_DMA_NOWAIT) { 954 if (reserve_bounce_pages(dmat, map, 0) != 0) { 955 map->pagesneeded = 0; 956 mtx_unlock(&bounce_lock); 957 return (ENOMEM); 958 } 959 } else { 960 if (reserve_bounce_pages(dmat, map, 1) != 0) { 961 /* Queue us for resources */ 962 STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links); 963 mtx_unlock(&bounce_lock); 964 return (EINPROGRESS); 965 } 966 } 967 mtx_unlock(&bounce_lock); 968 969 return (0); 970} 971 972/* 973 * Add a single contiguous physical range to the segment list. 974 */ 975static int 976_bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr, 977 bus_size_t sgsize, bus_dma_segment_t *segs, int *segp) 978{ 979 bus_addr_t baddr, bmask; 980 int seg; 981 982 /* 983 * Make sure we don't cross any boundaries. 984 */ 985 bmask = ~(dmat->boundary - 1); 986 if (dmat->boundary > 0) { 987 baddr = (curaddr + dmat->boundary) & bmask; 988 if (sgsize > (baddr - curaddr)) 989 sgsize = (baddr - curaddr); 990 } 991 992 if (dmat->ranges) { 993 struct arm32_dma_range *dr; 994 995 dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges, 996 curaddr); 997 if (dr == NULL) { 998 _bus_dmamap_unload(dmat, map); 999 return (0); 1000 } 1001 /* 1002 * In a valid DMA range. Translate the physical 1003 * memory address to an address in the DMA window. 1004 */ 1005 curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase; 1006 } 1007 1008 /* 1009 * Insert chunk into a segment, coalescing with 1010 * previous segment if possible. 1011 */ 1012 seg = *segp; 1013 if (seg == -1) { 1014 seg = 0; 1015 segs[seg].ds_addr = curaddr; 1016 segs[seg].ds_len = sgsize; 1017 } else { 1018 if (curaddr == segs[seg].ds_addr + segs[seg].ds_len && 1019 (segs[seg].ds_len + sgsize) <= dmat->maxsegsz && 1020 (dmat->boundary == 0 || 1021 (segs[seg].ds_addr & bmask) == (curaddr & bmask))) 1022 segs[seg].ds_len += sgsize; 1023 else { 1024 if (++seg >= dmat->nsegments) 1025 return (0); 1026 segs[seg].ds_addr = curaddr; 1027 segs[seg].ds_len = sgsize; 1028 } 1029 } 1030 *segp = seg; 1031 return (sgsize); 1032} 1033 1034/* 1035 * Utility function to load a physical buffer. segp contains 1036 * the starting segment on entrace, and the ending segment on exit. 1037 */ 1038int 1039_bus_dmamap_load_phys(bus_dma_tag_t dmat, 1040 bus_dmamap_t map, 1041 vm_paddr_t buf, bus_size_t buflen, 1042 int flags, 1043 bus_dma_segment_t *segs, 1044 int *segp) 1045{ 1046 bus_addr_t curaddr; 1047 bus_size_t sgsize; 1048 int error; 1049 1050 if (segs == NULL) 1051 segs = map->segments; 1052 1053 counter_u64_add(maploads_total, 1); 1054 counter_u64_add(maploads_physmem, 1); 1055 1056 if (might_bounce(dmat, map, buflen, buflen)) { 1057 _bus_dmamap_count_phys(dmat, map, buf, buflen, flags); 1058 if (map->pagesneeded != 0) { 1059 counter_u64_add(maploads_bounced, 1); 1060 error = _bus_dmamap_reserve_pages(dmat, map, flags); 1061 if (error) 1062 return (error); 1063 } 1064 } 1065 1066 while (buflen > 0) { 1067 curaddr = buf; 1068 sgsize = MIN(buflen, dmat->maxsegsz); 1069 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr, 1070 sgsize)) { 1071 sgsize = MIN(sgsize, PAGE_SIZE); 1072 curaddr = add_bounce_page(dmat, map, 0, curaddr, 1073 sgsize); 1074 } 1075 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs, 1076 segp); 1077 if (sgsize == 0) 1078 break; 1079 buf += sgsize; 1080 buflen -= sgsize; 1081 } 1082 1083 /* 1084 * Did we fit? 1085 */ 1086 if (buflen != 0) { 1087 _bus_dmamap_unload(dmat, map); 1088 return (EFBIG); /* XXX better return value here? */ 1089 } 1090 return (0); 1091} 1092 1093int 1094_bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map, 1095 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags, 1096 bus_dma_segment_t *segs, int *segp) 1097{ 1098 1099 return (bus_dmamap_load_ma_triv(dmat, map, ma, tlen, ma_offs, flags, 1100 segs, segp)); 1101} 1102 1103/* 1104 * Utility function to load a linear buffer. segp contains 1105 * the starting segment on entrace, and the ending segment on exit. 1106 */ 1107int 1108_bus_dmamap_load_buffer(bus_dma_tag_t dmat, 1109 bus_dmamap_t map, 1110 void *buf, bus_size_t buflen, 1111 pmap_t pmap, 1112 int flags, 1113 bus_dma_segment_t *segs, 1114 int *segp) 1115{ 1116 bus_size_t sgsize; 1117 bus_addr_t curaddr; 1118 vm_offset_t vaddr; 1119 struct sync_list *sl; 1120 int error; 1121 1122 counter_u64_add(maploads_total, 1); 1123 if (map->flags & DMAMAP_COHERENT) 1124 counter_u64_add(maploads_coherent, 1); 1125 if (map->flags & DMAMAP_DMAMEM_ALLOC) 1126 counter_u64_add(maploads_dmamem, 1); 1127 1128 if (segs == NULL) 1129 segs = map->segments; 1130 1131 if (flags & BUS_DMA_LOAD_MBUF) { 1132 counter_u64_add(maploads_mbuf, 1); 1133 map->flags |= DMAMAP_MBUF; 1134 } 1135 1136 map->pmap = pmap; 1137 1138 if (might_bounce(dmat, map, (bus_addr_t)buf, buflen)) { 1139 _bus_dmamap_count_pages(dmat, map, buf, buflen, flags); 1140 if (map->pagesneeded != 0) { 1141 counter_u64_add(maploads_bounced, 1); 1142 error = _bus_dmamap_reserve_pages(dmat, map, flags); 1143 if (error) 1144 return (error); 1145 } 1146 } 1147 1148 sl = NULL; 1149 vaddr = (vm_offset_t)buf; 1150 1151 while (buflen > 0) { 1152 /* 1153 * Get the physical address for this segment. 1154 */ 1155 if (__predict_true(map->pmap == kernel_pmap)) 1156 curaddr = pmap_kextract(vaddr); 1157 else 1158 curaddr = pmap_extract(map->pmap, vaddr); 1159 1160 /* 1161 * Compute the segment size, and adjust counts. 1162 */ 1163 sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK); 1164 if (sgsize > dmat->maxsegsz) 1165 sgsize = dmat->maxsegsz; 1166 if (buflen < sgsize) 1167 sgsize = buflen; 1168 1169 if (map->pagesneeded != 0 && must_bounce(dmat, map, curaddr, 1170 sgsize)) { 1171 curaddr = add_bounce_page(dmat, map, vaddr, curaddr, 1172 sgsize); 1173 } else { 1174 sl = &map->slist[map->sync_count - 1]; 1175 if (map->sync_count == 0 || 1176#ifdef ARM_L2_PIPT 1177 curaddr != sl->busaddr + sl->datacount || 1178#endif 1179 vaddr != sl->vaddr + sl->datacount) { 1180 if (++map->sync_count > dmat->nsegments) 1181 goto cleanup; 1182 sl++; 1183 sl->vaddr = vaddr; 1184 sl->datacount = sgsize; 1185 sl->busaddr = curaddr; 1186 } else 1187 sl->datacount += sgsize; 1188 } 1189 sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs, 1190 segp); 1191 if (sgsize == 0) 1192 break; 1193 vaddr += sgsize; 1194 buflen -= sgsize; 1195 } 1196 1197cleanup: 1198 /* 1199 * Did we fit? 1200 */ 1201 if (buflen != 0) { 1202 _bus_dmamap_unload(dmat, map); 1203 return (EFBIG); /* XXX better return value here? */ 1204 } 1205 return (0); 1206} 1207 1208 1209void 1210__bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map, 1211 struct memdesc *mem, bus_dmamap_callback_t *callback, 1212 void *callback_arg) 1213{ 1214 1215 map->mem = *mem; 1216 map->dmat = dmat; 1217 map->callback = callback; 1218 map->callback_arg = callback_arg; 1219} 1220 1221bus_dma_segment_t * 1222_bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map, 1223 bus_dma_segment_t *segs, int nsegs, int error) 1224{ 1225 1226 if (segs == NULL) 1227 segs = map->segments; 1228 return (segs); 1229} 1230 1231/* 1232 * Release the mapping held by map. 1233 */ 1234void 1235_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map) 1236{ 1237 struct bounce_page *bpage; 1238 struct bounce_zone *bz; 1239 1240 if ((bz = dmat->bounce_zone) != NULL) { 1241 while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1242 STAILQ_REMOVE_HEAD(&map->bpages, links); 1243 free_bounce_page(dmat, bpage); 1244 } 1245 1246 bz = dmat->bounce_zone; 1247 bz->free_bpages += map->pagesreserved; 1248 bz->reserved_bpages -= map->pagesreserved; 1249 map->pagesreserved = 0; 1250 map->pagesneeded = 0; 1251 } 1252 map->sync_count = 0; 1253 map->flags &= ~DMAMAP_MBUF; 1254} 1255 1256#ifdef notyetbounceuser 1257 /* If busdma uses user pages, then the interrupt handler could 1258 * be use the kernel vm mapping. Both bounce pages and sync list 1259 * do not cross page boundaries. 1260 * Below is a rough sequence that a person would do to fix the 1261 * user page reference in the kernel vmspace. This would be 1262 * done in the dma post routine. 1263 */ 1264void 1265_bus_dmamap_fix_user(vm_offset_t buf, bus_size_t len, 1266 pmap_t pmap, int op) 1267{ 1268 bus_size_t sgsize; 1269 bus_addr_t curaddr; 1270 vm_offset_t va; 1271 1272 /* each synclist entry is contained within a single page. 1273 * 1274 * this would be needed if BUS_DMASYNC_POSTxxxx was implemented 1275 */ 1276 curaddr = pmap_extract(pmap, buf); 1277 va = pmap_dma_map(curaddr); 1278 switch (op) { 1279 case SYNC_USER_INV: 1280 cpu_dcache_wb_range(va, sgsize); 1281 break; 1282 1283 case SYNC_USER_COPYTO: 1284 bcopy((void *)va, (void *)bounce, sgsize); 1285 break; 1286 1287 case SYNC_USER_COPYFROM: 1288 bcopy((void *) bounce, (void *)va, sgsize); 1289 break; 1290 1291 default: 1292 break; 1293 } 1294 1295 pmap_dma_unmap(va); 1296} 1297#endif 1298 1299#ifdef ARM_L2_PIPT 1300#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(pa, size) 1301#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(pa, size) 1302#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(pa, size) 1303#else 1304#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(va, size) 1305#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(va, size) 1306#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(va, size) 1307#endif 1308 1309void 1310_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op) 1311{ 1312 struct bounce_page *bpage; 1313 struct sync_list *sl, *end; 1314 /* 1315 * If the buffer was from user space, it is possible that this is not 1316 * the same vm map, especially on a POST operation. It's not clear that 1317 * dma on userland buffers can work at all right now, certainly not if a 1318 * partial cacheline flush has to be handled. To be safe, until we're 1319 * able to test direct userland dma, panic on a map mismatch. 1320 */ 1321 if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1322 if (!pmap_dmap_iscurrent(map->pmap)) 1323 panic("_bus_dmamap_sync: wrong user map for bounce sync."); 1324 /* Handle data bouncing. */ 1325 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1326 "performing bounce", __func__, dmat, dmat->flags, op); 1327 1328 if (op & BUS_DMASYNC_PREWRITE) { 1329 while (bpage != NULL) { 1330 if (bpage->datavaddr != 0) 1331 bcopy((void *)bpage->datavaddr, 1332 (void *)bpage->vaddr, 1333 bpage->datacount); 1334 else 1335 physcopyout(bpage->dataaddr, 1336 (void *)bpage->vaddr, 1337 bpage->datacount); 1338 cpu_dcache_wb_range((vm_offset_t)bpage->vaddr, 1339 bpage->datacount); 1340 l2cache_wb_range((vm_offset_t)bpage->vaddr, 1341 (vm_offset_t)bpage->busaddr, 1342 bpage->datacount); 1343 bpage = STAILQ_NEXT(bpage, links); 1344 } 1345 dmat->bounce_zone->total_bounced++; 1346 } 1347 1348 if (op & BUS_DMASYNC_PREREAD) { 1349 bpage = STAILQ_FIRST(&map->bpages); 1350 while (bpage != NULL) { 1351 cpu_dcache_inv_range((vm_offset_t)bpage->vaddr, 1352 bpage->datacount); 1353 l2cache_inv_range((vm_offset_t)bpage->vaddr, 1354 (vm_offset_t)bpage->busaddr, 1355 bpage->datacount); 1356 bpage = STAILQ_NEXT(bpage, links); 1357 } 1358 } 1359 if (op & BUS_DMASYNC_POSTREAD) { 1360 while (bpage != NULL) { 1361 vm_offset_t startv; 1362 vm_paddr_t startp; 1363 int len; 1364 1365 startv = bpage->vaddr &~ arm_dcache_align_mask; 1366 startp = bpage->busaddr &~ arm_dcache_align_mask; 1367 len = bpage->datacount; 1368 1369 if (startv != bpage->vaddr) 1370 len += bpage->vaddr & arm_dcache_align_mask; 1371 if (len & arm_dcache_align_mask) 1372 len = (len - 1373 (len & arm_dcache_align_mask)) + 1374 arm_dcache_align; 1375 cpu_dcache_inv_range(startv, len); 1376 l2cache_inv_range(startv, startp, len); 1377 if (bpage->datavaddr != 0) 1378 bcopy((void *)bpage->vaddr, 1379 (void *)bpage->datavaddr, 1380 bpage->datacount); 1381 else 1382 physcopyin((void *)bpage->vaddr, 1383 bpage->dataaddr, 1384 bpage->datacount); 1385 bpage = STAILQ_NEXT(bpage, links); 1386 } 1387 dmat->bounce_zone->total_bounced++; 1388 } 1389 } 1390 if (map->flags & DMAMAP_COHERENT) 1391 return; 1392 1393 if (map->sync_count != 0) { 1394 if (!pmap_dmap_iscurrent(map->pmap)) 1395 panic("_bus_dmamap_sync: wrong user map for sync."); 1396 /* ARM caches are not self-snooping for dma */ 1397 1398 sl = &map->slist[0]; 1399 end = &map->slist[map->sync_count]; 1400 CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1401 "performing sync", __func__, dmat, dmat->flags, op); 1402 1403 switch (op) { 1404 case BUS_DMASYNC_PREWRITE: 1405 while (sl != end) { 1406 cpu_dcache_wb_range(sl->vaddr, sl->datacount); 1407 l2cache_wb_range(sl->vaddr, sl->busaddr, 1408 sl->datacount); 1409 sl++; 1410 } 1411 break; 1412 1413 case BUS_DMASYNC_PREREAD: 1414 while (sl != end) { 1415 cpu_dcache_inv_range(sl->vaddr, sl->datacount); 1416 l2cache_inv_range(sl->vaddr, sl->busaddr, 1417 sl->datacount); 1418 sl++; 1419 } 1420 break; 1421 1422 case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD: 1423 while (sl != end) { 1424 cpu_dcache_wbinv_range(sl->vaddr, sl->datacount); 1425 l2cache_wbinv_range(sl->vaddr, 1426 sl->busaddr, sl->datacount); 1427 sl++; 1428 } 1429 break; 1430 1431 case BUS_DMASYNC_POSTREAD: 1432 case BUS_DMASYNC_POSTWRITE: 1433 case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE: 1434 break; 1435 default: 1436 panic("unsupported combination of sync operations: 0x%08x\n", op); 1437 break; 1438 } 1439 } 1440} 1441 1442static void 1443init_bounce_pages(void *dummy __unused) 1444{ 1445 1446 total_bpages = 0; 1447 STAILQ_INIT(&bounce_zone_list); 1448 STAILQ_INIT(&bounce_map_waitinglist); 1449 STAILQ_INIT(&bounce_map_callbacklist); 1450 mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF); 1451} 1452SYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL); 1453 1454static struct sysctl_ctx_list * 1455busdma_sysctl_tree(struct bounce_zone *bz) 1456{ 1457 return (&bz->sysctl_tree); 1458} 1459 1460static struct sysctl_oid * 1461busdma_sysctl_tree_top(struct bounce_zone *bz) 1462{ 1463 return (bz->sysctl_tree_top); 1464} 1465 1466static int 1467alloc_bounce_zone(bus_dma_tag_t dmat) 1468{ 1469 struct bounce_zone *bz; 1470 1471 /* Check to see if we already have a suitable zone */ 1472 STAILQ_FOREACH(bz, &bounce_zone_list, links) { 1473 if ((dmat->alignment <= bz->alignment) && 1474 (dmat->lowaddr >= bz->lowaddr)) { 1475 dmat->bounce_zone = bz; 1476 return (0); 1477 } 1478 } 1479 1480 if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF, 1481 M_NOWAIT | M_ZERO)) == NULL) 1482 return (ENOMEM); 1483 1484 STAILQ_INIT(&bz->bounce_page_list); 1485 bz->free_bpages = 0; 1486 bz->reserved_bpages = 0; 1487 bz->active_bpages = 0; 1488 bz->lowaddr = dmat->lowaddr; 1489 bz->alignment = MAX(dmat->alignment, PAGE_SIZE); 1490 bz->map_count = 0; 1491 snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount); 1492 busdma_zonecount++; 1493 snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr); 1494 STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links); 1495 dmat->bounce_zone = bz; 1496 1497 sysctl_ctx_init(&bz->sysctl_tree); 1498 bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree, 1499 SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid, 1500 CTLFLAG_RD, 0, ""); 1501 if (bz->sysctl_tree_top == NULL) { 1502 sysctl_ctx_free(&bz->sysctl_tree); 1503 return (0); /* XXX error code? */ 1504 } 1505 1506 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1507 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1508 "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0, 1509 "Total bounce pages"); 1510 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1511 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1512 "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0, 1513 "Free bounce pages"); 1514 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1515 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1516 "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0, 1517 "Reserved bounce pages"); 1518 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1519 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1520 "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0, 1521 "Active bounce pages"); 1522 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1523 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1524 "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0, 1525 "Total bounce requests (pages bounced)"); 1526 SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1527 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1528 "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0, 1529 "Total bounce requests that were deferred"); 1530 SYSCTL_ADD_STRING(busdma_sysctl_tree(bz), 1531 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1532 "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, ""); 1533 SYSCTL_ADD_ULONG(busdma_sysctl_tree(bz), 1534 SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1535 "alignment", CTLFLAG_RD, &bz->alignment, ""); 1536 1537 return (0); 1538} 1539 1540static int 1541alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages) 1542{ 1543 struct bounce_zone *bz; 1544 int count; 1545 1546 bz = dmat->bounce_zone; 1547 count = 0; 1548 while (numpages > 0) { 1549 struct bounce_page *bpage; 1550 1551 bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF, 1552 M_NOWAIT | M_ZERO); 1553 1554 if (bpage == NULL) 1555 break; 1556 bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF, 1557 M_NOWAIT, 0ul, bz->lowaddr, PAGE_SIZE, 0); 1558 if (bpage->vaddr == 0) { 1559 free(bpage, M_DEVBUF); 1560 break; 1561 } 1562 bpage->busaddr = pmap_kextract(bpage->vaddr); 1563 mtx_lock(&bounce_lock); 1564 STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links); 1565 total_bpages++; 1566 bz->total_bpages++; 1567 bz->free_bpages++; 1568 mtx_unlock(&bounce_lock); 1569 count++; 1570 numpages--; 1571 } 1572 return (count); 1573} 1574 1575static int 1576reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit) 1577{ 1578 struct bounce_zone *bz; 1579 int pages; 1580 1581 mtx_assert(&bounce_lock, MA_OWNED); 1582 bz = dmat->bounce_zone; 1583 pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved); 1584 if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages)) 1585 return (map->pagesneeded - (map->pagesreserved + pages)); 1586 bz->free_bpages -= pages; 1587 bz->reserved_bpages += pages; 1588 map->pagesreserved += pages; 1589 pages = map->pagesneeded - map->pagesreserved; 1590 1591 return (pages); 1592} 1593 1594static bus_addr_t 1595add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr, 1596 bus_addr_t addr, bus_size_t size) 1597{ 1598 struct bounce_zone *bz; 1599 struct bounce_page *bpage; 1600 1601 KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag")); 1602 KASSERT(map != NULL, 1603 ("add_bounce_page: bad map %p", map)); 1604 1605 bz = dmat->bounce_zone; 1606 if (map->pagesneeded == 0) 1607 panic("add_bounce_page: map doesn't need any pages"); 1608 map->pagesneeded--; 1609 1610 if (map->pagesreserved == 0) 1611 panic("add_bounce_page: map doesn't need any pages"); 1612 map->pagesreserved--; 1613 1614 mtx_lock(&bounce_lock); 1615 bpage = STAILQ_FIRST(&bz->bounce_page_list); 1616 if (bpage == NULL) 1617 panic("add_bounce_page: free page list is empty"); 1618 1619 STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links); 1620 bz->reserved_bpages--; 1621 bz->active_bpages++; 1622 mtx_unlock(&bounce_lock); 1623 1624 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1625 /* Page offset needs to be preserved. */ 1626 bpage->vaddr |= vaddr & PAGE_MASK; 1627 bpage->busaddr |= vaddr & PAGE_MASK; 1628 } 1629 bpage->datavaddr = vaddr; 1630 bpage->dataaddr = addr; 1631 bpage->datacount = size; 1632 STAILQ_INSERT_TAIL(&(map->bpages), bpage, links); 1633 return (bpage->busaddr); 1634} 1635 1636static void 1637free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage) 1638{ 1639 struct bus_dmamap *map; 1640 struct bounce_zone *bz; 1641 1642 bz = dmat->bounce_zone; 1643 bpage->datavaddr = 0; 1644 bpage->datacount = 0; 1645 if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1646 /* 1647 * Reset the bounce page to start at offset 0. Other uses 1648 * of this bounce page may need to store a full page of 1649 * data and/or assume it starts on a page boundary. 1650 */ 1651 bpage->vaddr &= ~PAGE_MASK; 1652 bpage->busaddr &= ~PAGE_MASK; 1653 } 1654 1655 mtx_lock(&bounce_lock); 1656 STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links); 1657 bz->free_bpages++; 1658 bz->active_bpages--; 1659 if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) { 1660 if (reserve_bounce_pages(map->dmat, map, 1) == 0) { 1661 STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links); 1662 STAILQ_INSERT_TAIL(&bounce_map_callbacklist, 1663 map, links); 1664 busdma_swi_pending = 1; 1665 bz->total_deferred++; 1666 swi_sched(vm_ih, 0); 1667 } 1668 } 1669 mtx_unlock(&bounce_lock); 1670} 1671 1672void 1673busdma_swi(void) 1674{ 1675 bus_dma_tag_t dmat; 1676 struct bus_dmamap *map; 1677 1678 mtx_lock(&bounce_lock); 1679 while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) { 1680 STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links); 1681 mtx_unlock(&bounce_lock); 1682 dmat = map->dmat; 1683 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_LOCK); 1684 bus_dmamap_load_mem(map->dmat, map, &map->mem, map->callback, 1685 map->callback_arg, BUS_DMA_WAITOK); 1686 dmat->lockfunc(dmat->lockfuncarg, BUS_DMA_UNLOCK); 1687 mtx_lock(&bounce_lock); 1688 } 1689 mtx_unlock(&bounce_lock); 1690} 1691