busdma_machdep-v6.c revision 269206
1239268Sgonzo/*- 2244469Scognet * Copyright (c) 2012 Ian Lepore 3239268Sgonzo * Copyright (c) 2010 Mark Tinguely 4239268Sgonzo * Copyright (c) 2004 Olivier Houchard 5239268Sgonzo * Copyright (c) 2002 Peter Grehan 6239268Sgonzo * Copyright (c) 1997, 1998 Justin T. Gibbs. 7239268Sgonzo * All rights reserved. 8239268Sgonzo * 9239268Sgonzo * Redistribution and use in source and binary forms, with or without 10239268Sgonzo * modification, are permitted provided that the following conditions 11239268Sgonzo * are met: 12239268Sgonzo * 1. Redistributions of source code must retain the above copyright 13239268Sgonzo * notice, this list of conditions, and the following disclaimer, 14239268Sgonzo * without modification, immediately at the beginning of the file. 15239268Sgonzo * 2. The name of the author may not be used to endorse or promote products 16239268Sgonzo * derived from this software without specific prior written permission. 17239268Sgonzo * 18239268Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19239268Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20239268Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21239268Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22239268Sgonzo * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23239268Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24239268Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25239268Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26239268Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27239268Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28239268Sgonzo * SUCH DAMAGE. 29239268Sgonzo * 30239268Sgonzo * From i386/busdma_machdep.c 191438 2009-04-23 20:24:19Z jhb 31239268Sgonzo */ 32239268Sgonzo 33239268Sgonzo#include <sys/cdefs.h> 34239268Sgonzo__FBSDID("$FreeBSD: head/sys/arm/arm/busdma_machdep-v6.c 269206 2014-07-29 02:31:29Z ian $"); 35239268Sgonzo 36239268Sgonzo#define _ARM32_BUS_DMA_PRIVATE 37239268Sgonzo#include <sys/param.h> 38239268Sgonzo#include <sys/kdb.h> 39239268Sgonzo#include <ddb/ddb.h> 40239268Sgonzo#include <ddb/db_output.h> 41239268Sgonzo#include <sys/systm.h> 42239268Sgonzo#include <sys/malloc.h> 43239268Sgonzo#include <sys/bus.h> 44244469Scognet#include <sys/busdma_bufalloc.h> 45239268Sgonzo#include <sys/interrupt.h> 46239268Sgonzo#include <sys/kernel.h> 47239268Sgonzo#include <sys/ktr.h> 48239268Sgonzo#include <sys/lock.h> 49246713Skib#include <sys/memdesc.h> 50239268Sgonzo#include <sys/proc.h> 51239268Sgonzo#include <sys/mutex.h> 52246713Skib#include <sys/sysctl.h> 53239268Sgonzo#include <sys/uio.h> 54239268Sgonzo 55239268Sgonzo#include <vm/vm.h> 56239268Sgonzo#include <vm/vm_page.h> 57239268Sgonzo#include <vm/vm_map.h> 58244469Scognet#include <vm/vm_extern.h> 59244469Scognet#include <vm/vm_kern.h> 60239268Sgonzo 61239268Sgonzo#include <machine/atomic.h> 62239268Sgonzo#include <machine/bus.h> 63239268Sgonzo#include <machine/cpufunc.h> 64239268Sgonzo#include <machine/md_var.h> 65239268Sgonzo 66239268Sgonzo#define MAX_BPAGES 64 67239268Sgonzo#define BUS_DMA_COULD_BOUNCE BUS_DMA_BUS3 68239268Sgonzo#define BUS_DMA_MIN_ALLOC_COMP BUS_DMA_BUS4 69239268Sgonzo 70239268Sgonzostruct bounce_zone; 71239268Sgonzo 72239268Sgonzostruct bus_dma_tag { 73239268Sgonzo bus_dma_tag_t parent; 74239268Sgonzo bus_size_t alignment; 75239268Sgonzo bus_size_t boundary; 76239268Sgonzo bus_addr_t lowaddr; 77239268Sgonzo bus_addr_t highaddr; 78239268Sgonzo bus_dma_filter_t *filter; 79239268Sgonzo void *filterarg; 80239268Sgonzo bus_size_t maxsize; 81239268Sgonzo u_int nsegments; 82239268Sgonzo bus_size_t maxsegsz; 83239268Sgonzo int flags; 84239268Sgonzo int ref_count; 85239268Sgonzo int map_count; 86239268Sgonzo bus_dma_lock_t *lockfunc; 87239268Sgonzo void *lockfuncarg; 88239268Sgonzo struct bounce_zone *bounce_zone; 89239268Sgonzo /* 90239268Sgonzo * DMA range for this tag. If the page doesn't fall within 91239268Sgonzo * one of these ranges, an error is returned. The caller 92239268Sgonzo * may then decide what to do with the transfer. If the 93239268Sgonzo * range pointer is NULL, it is ignored. 94239268Sgonzo */ 95239268Sgonzo struct arm32_dma_range *ranges; 96239268Sgonzo int _nranges; 97244469Scognet /* 98244469Scognet * Most tags need one or two segments, and can use the local tagsegs 99244469Scognet * array. For tags with a larger limit, we'll allocate a bigger array 100244469Scognet * on first use. 101244469Scognet */ 102244469Scognet bus_dma_segment_t *segments; 103244469Scognet bus_dma_segment_t tagsegs[2]; 104239268Sgonzo 105244469Scognet 106239268Sgonzo}; 107239268Sgonzo 108239268Sgonzostruct bounce_page { 109239268Sgonzo vm_offset_t vaddr; /* kva of bounce buffer */ 110239268Sgonzo bus_addr_t busaddr; /* Physical address */ 111239268Sgonzo vm_offset_t datavaddr; /* kva of client data */ 112246713Skib bus_addr_t dataaddr; /* client physical address */ 113239268Sgonzo bus_size_t datacount; /* client data count */ 114239268Sgonzo STAILQ_ENTRY(bounce_page) links; 115239268Sgonzo}; 116239268Sgonzo 117239268Sgonzostruct sync_list { 118239268Sgonzo vm_offset_t vaddr; /* kva of bounce buffer */ 119239268Sgonzo bus_addr_t busaddr; /* Physical address */ 120239268Sgonzo bus_size_t datacount; /* client data count */ 121239268Sgonzo}; 122239268Sgonzo 123239268Sgonzoint busdma_swi_pending; 124239268Sgonzo 125239268Sgonzostruct bounce_zone { 126239268Sgonzo STAILQ_ENTRY(bounce_zone) links; 127239268Sgonzo STAILQ_HEAD(bp_list, bounce_page) bounce_page_list; 128239268Sgonzo int total_bpages; 129239268Sgonzo int free_bpages; 130239268Sgonzo int reserved_bpages; 131239268Sgonzo int active_bpages; 132239268Sgonzo int total_bounced; 133239268Sgonzo int total_deferred; 134239268Sgonzo int map_count; 135239268Sgonzo bus_size_t alignment; 136239268Sgonzo bus_addr_t lowaddr; 137239268Sgonzo char zoneid[8]; 138239268Sgonzo char lowaddrid[20]; 139239268Sgonzo struct sysctl_ctx_list sysctl_tree; 140239268Sgonzo struct sysctl_oid *sysctl_tree_top; 141239268Sgonzo}; 142239268Sgonzo 143239268Sgonzostatic struct mtx bounce_lock; 144239268Sgonzostatic int total_bpages; 145239268Sgonzostatic int busdma_zonecount; 146239268Sgonzostatic STAILQ_HEAD(, bounce_zone) bounce_zone_list; 147239268Sgonzo 148239268SgonzoSYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters"); 149239268SgonzoSYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0, 150239268Sgonzo "Total bounce pages"); 151239268Sgonzo 152239268Sgonzostruct bus_dmamap { 153239268Sgonzo struct bp_list bpages; 154239268Sgonzo int pagesneeded; 155239268Sgonzo int pagesreserved; 156239268Sgonzo bus_dma_tag_t dmat; 157246713Skib struct memdesc mem; 158239268Sgonzo pmap_t pmap; 159239268Sgonzo bus_dmamap_callback_t *callback; 160239268Sgonzo void *callback_arg; 161244469Scognet int flags; 162244469Scognet#define DMAMAP_COHERENT (1 << 0) 163239268Sgonzo STAILQ_ENTRY(bus_dmamap) links; 164246713Skib int sync_count; 165246713Skib struct sync_list slist[]; 166239268Sgonzo}; 167239268Sgonzo 168239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist; 169239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist; 170239268Sgonzo 171239268Sgonzostatic void init_bounce_pages(void *dummy); 172239268Sgonzostatic int alloc_bounce_zone(bus_dma_tag_t dmat); 173239268Sgonzostatic int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages); 174239268Sgonzostatic int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 175239268Sgonzo int commit); 176239268Sgonzostatic bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, 177246713Skib vm_offset_t vaddr, bus_addr_t addr, 178246713Skib bus_size_t size); 179239268Sgonzostatic void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage); 180254061Scognetint run_filter(bus_dma_tag_t dmat, bus_addr_t paddr, bus_size_t size, int coherent); 181246713Skibstatic void _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 182239268Sgonzo void *buf, bus_size_t buflen, int flags); 183246713Skibstatic void _bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, 184246713Skib vm_paddr_t buf, bus_size_t buflen, int flags); 185246713Skibstatic int _bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 186246713Skib int flags); 187239268Sgonzo 188244469Scognetstatic busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */ 189244469Scognetstatic busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */ 190244469Scognetstatic void 191244469Scognetbusdma_init(void *dummy) 192244469Scognet{ 193252652Sgonzo int uma_flags; 194244469Scognet 195252652Sgonzo uma_flags = 0; 196252652Sgonzo 197244469Scognet /* Create a cache of buffers in standard (cacheable) memory. */ 198244469Scognet standard_allocator = busdma_bufalloc_create("buffer", 199244469Scognet arm_dcache_align, /* minimum_alignment */ 200244469Scognet NULL, /* uma_alloc func */ 201244469Scognet NULL, /* uma_free func */ 202252652Sgonzo uma_flags); /* uma_zcreate_flags */ 203244469Scognet 204252652Sgonzo#ifdef INVARIANTS 205252652Sgonzo /* 206252652Sgonzo * Force UMA zone to allocate service structures like 207252652Sgonzo * slabs using own allocator. uma_debug code performs 208252652Sgonzo * atomic ops on uma_slab_t fields and safety of this 209252652Sgonzo * operation is not guaranteed for write-back caches 210252652Sgonzo */ 211252652Sgonzo uma_flags = UMA_ZONE_OFFPAGE; 212252652Sgonzo#endif 213244469Scognet /* 214244469Scognet * Create a cache of buffers in uncacheable memory, to implement the 215244469Scognet * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag. 216244469Scognet */ 217244469Scognet coherent_allocator = busdma_bufalloc_create("coherent", 218244469Scognet arm_dcache_align, /* minimum_alignment */ 219244469Scognet busdma_bufalloc_alloc_uncacheable, 220244469Scognet busdma_bufalloc_free_uncacheable, 221252652Sgonzo uma_flags); /* uma_zcreate_flags */ 222244469Scognet} 223244469Scognet 224244469Scognet/* 225244469Scognet * This init historically used SI_SUB_VM, but now the init code requires 226244469Scognet * malloc(9) using M_DEVBUF memory, which is set up later than SI_SUB_VM, by 227267992Shselasky * SI_SUB_KMEM and SI_ORDER_THIRD, so we'll go right after that by using 228267992Shselasky * SI_SUB_KMEM and SI_ORDER_FOURTH. 229244469Scognet */ 230267992ShselaskySYSINIT(busdma, SI_SUB_KMEM, SI_ORDER_FOURTH, busdma_init, NULL); 231244469Scognet 232269136Sian/* 233269136Sian * This routine checks the exclusion zone constraints from a tag against the 234269136Sian * physical RAM available on the machine. If a tag specifies an exclusion zone 235269136Sian * but there's no RAM in that zone, then we avoid allocating resources to bounce 236269136Sian * a request, and we can use any memory allocator (as opposed to needing 237269136Sian * kmem_alloc_contig() just because it can allocate pages in an address range). 238269136Sian * 239269136Sian * Most tags have BUS_SPACE_MAXADDR or BUS_SPACE_MAXADDR_32BIT (they are the 240269136Sian * same value on 32-bit architectures) as their lowaddr constraint, and we can't 241269136Sian * possibly have RAM at an address higher than the highest address we can 242269136Sian * express, so we take a fast out. 243269136Sian */ 244269206Sianstatic int 245269206Sianexclusion_bounce(vm_offset_t lowaddr, vm_offset_t highaddr) 246239268Sgonzo{ 247239268Sgonzo int i; 248269136Sian 249269136Sian if (lowaddr >= BUS_SPACE_MAXADDR) 250269136Sian return (0); 251269136Sian 252239268Sgonzo for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) { 253239268Sgonzo if ((lowaddr >= phys_avail[i] && lowaddr <= phys_avail[i + 1]) 254239268Sgonzo || (lowaddr < phys_avail[i] && 255239268Sgonzo highaddr > phys_avail[i])) 256239268Sgonzo return (1); 257239268Sgonzo } 258239268Sgonzo return (0); 259239268Sgonzo} 260239268Sgonzo 261269206Sian/* 262269206Sian * Return true if the given address does not fall on the alignment boundary. 263269206Sian */ 264269206Sianstatic __inline int 265269206Sianalignment_bounce(bus_dma_tag_t dmat, bus_addr_t addr) 266269206Sian{ 267269206Sian 268269206Sian return (addr & (dmat->alignment - 1)); 269269206Sian} 270269206Sian 271269206Sian/* 272269206Sian * Return true if the buffer start or end does not fall on a cacheline boundary. 273269206Sian */ 274269206Sianstatic __inline int 275269206Siancacheline_bounce(bus_addr_t addr, bus_size_t size) 276269206Sian{ 277269206Sian 278269206Sian return ((addr | size) & arm_dcache_align_mask); 279269206Sian} 280269206Sian 281239268Sgonzostatic __inline struct arm32_dma_range * 282239268Sgonzo_bus_dma_inrange(struct arm32_dma_range *ranges, int nranges, 283239268Sgonzo bus_addr_t curaddr) 284239268Sgonzo{ 285239268Sgonzo struct arm32_dma_range *dr; 286239268Sgonzo int i; 287239268Sgonzo 288239268Sgonzo for (i = 0, dr = ranges; i < nranges; i++, dr++) { 289239268Sgonzo if (curaddr >= dr->dr_sysbase && 290239268Sgonzo round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len)) 291239268Sgonzo return (dr); 292239268Sgonzo } 293239268Sgonzo 294239268Sgonzo return (NULL); 295239268Sgonzo} 296239268Sgonzo 297239268Sgonzo/* 298239268Sgonzo * Return true if a match is made. 299239268Sgonzo * 300239268Sgonzo * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'. 301239268Sgonzo * 302239268Sgonzo * If paddr is within the bounds of the dma tag then call the filter callback 303239268Sgonzo * to check for a match, if there is no filter callback then assume a match. 304239268Sgonzo */ 305239268Sgonzoint 306254061Scognetrun_filter(bus_dma_tag_t dmat, bus_addr_t paddr, bus_size_t size, int coherent) 307239268Sgonzo{ 308239268Sgonzo int retval; 309239268Sgonzo 310239268Sgonzo retval = 0; 311239268Sgonzo 312239268Sgonzo do { 313239268Sgonzo if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr) 314269206Sian || alignment_bounce(dmat, paddr) || 315269206Sian (!coherent && cacheline_bounce(paddr, size))) 316239268Sgonzo && (dmat->filter == NULL 317239268Sgonzo || (*dmat->filter)(dmat->filterarg, paddr) != 0)) 318239268Sgonzo retval = 1; 319239268Sgonzo 320239268Sgonzo dmat = dmat->parent; 321239268Sgonzo } while (retval == 0 && dmat != NULL); 322239268Sgonzo return (retval); 323239268Sgonzo} 324239268Sgonzo 325239268Sgonzo/* 326239268Sgonzo * Convenience function for manipulating driver locks from busdma (during 327239268Sgonzo * busdma_swi, for example). Drivers that don't provide their own locks 328239268Sgonzo * should specify &Giant to dmat->lockfuncarg. Drivers that use their own 329239268Sgonzo * non-mutex locking scheme don't have to use this at all. 330239268Sgonzo */ 331239268Sgonzovoid 332239268Sgonzobusdma_lock_mutex(void *arg, bus_dma_lock_op_t op) 333239268Sgonzo{ 334239268Sgonzo struct mtx *dmtx; 335239268Sgonzo 336239268Sgonzo dmtx = (struct mtx *)arg; 337239268Sgonzo switch (op) { 338239268Sgonzo case BUS_DMA_LOCK: 339239268Sgonzo mtx_lock(dmtx); 340239268Sgonzo break; 341239268Sgonzo case BUS_DMA_UNLOCK: 342239268Sgonzo mtx_unlock(dmtx); 343239268Sgonzo break; 344239268Sgonzo default: 345239268Sgonzo panic("Unknown operation 0x%x for busdma_lock_mutex!", op); 346239268Sgonzo } 347239268Sgonzo} 348239268Sgonzo 349239268Sgonzo/* 350239268Sgonzo * dflt_lock should never get called. It gets put into the dma tag when 351239268Sgonzo * lockfunc == NULL, which is only valid if the maps that are associated 352239268Sgonzo * with the tag are meant to never be defered. 353239268Sgonzo * XXX Should have a way to identify which driver is responsible here. 354239268Sgonzo */ 355239268Sgonzostatic void 356239268Sgonzodflt_lock(void *arg, bus_dma_lock_op_t op) 357239268Sgonzo{ 358239268Sgonzo panic("driver error: busdma dflt_lock called"); 359239268Sgonzo} 360239268Sgonzo 361239268Sgonzo/* 362239268Sgonzo * Allocate a device specific dma_tag. 363239268Sgonzo */ 364239268Sgonzoint 365239268Sgonzobus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment, 366239268Sgonzo bus_size_t boundary, bus_addr_t lowaddr, 367239268Sgonzo bus_addr_t highaddr, bus_dma_filter_t *filter, 368239268Sgonzo void *filterarg, bus_size_t maxsize, int nsegments, 369239268Sgonzo bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc, 370239268Sgonzo void *lockfuncarg, bus_dma_tag_t *dmat) 371239268Sgonzo{ 372239268Sgonzo bus_dma_tag_t newtag; 373239268Sgonzo int error = 0; 374239268Sgonzo 375239268Sgonzo#if 0 376239268Sgonzo if (!parent) 377239268Sgonzo parent = arm_root_dma_tag; 378239268Sgonzo#endif 379239268Sgonzo 380239268Sgonzo /* Basic sanity checking */ 381239268Sgonzo if (boundary != 0 && boundary < maxsegsz) 382239268Sgonzo maxsegsz = boundary; 383239268Sgonzo 384239268Sgonzo /* Return a NULL tag on failure */ 385239268Sgonzo *dmat = NULL; 386239268Sgonzo 387239268Sgonzo if (maxsegsz == 0) { 388239268Sgonzo return (EINVAL); 389239268Sgonzo } 390239268Sgonzo 391239268Sgonzo newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, 392239268Sgonzo M_ZERO | M_NOWAIT); 393239268Sgonzo if (newtag == NULL) { 394239268Sgonzo CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 395239268Sgonzo __func__, newtag, 0, error); 396239268Sgonzo return (ENOMEM); 397239268Sgonzo } 398239268Sgonzo 399239268Sgonzo newtag->parent = parent; 400239268Sgonzo newtag->alignment = alignment; 401239268Sgonzo newtag->boundary = boundary; 402239268Sgonzo newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1); 403239268Sgonzo newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + 404239268Sgonzo (PAGE_SIZE - 1); 405239268Sgonzo newtag->filter = filter; 406239268Sgonzo newtag->filterarg = filterarg; 407239268Sgonzo newtag->maxsize = maxsize; 408239268Sgonzo newtag->nsegments = nsegments; 409239268Sgonzo newtag->maxsegsz = maxsegsz; 410239268Sgonzo newtag->flags = flags; 411239268Sgonzo newtag->ref_count = 1; /* Count ourself */ 412239268Sgonzo newtag->map_count = 0; 413239268Sgonzo newtag->ranges = bus_dma_get_range(); 414239268Sgonzo newtag->_nranges = bus_dma_get_range_nb(); 415239268Sgonzo if (lockfunc != NULL) { 416239268Sgonzo newtag->lockfunc = lockfunc; 417239268Sgonzo newtag->lockfuncarg = lockfuncarg; 418239268Sgonzo } else { 419239268Sgonzo newtag->lockfunc = dflt_lock; 420239268Sgonzo newtag->lockfuncarg = NULL; 421239268Sgonzo } 422244469Scognet /* 423244469Scognet * If all the segments we need fit into the local tagsegs array, set the 424244469Scognet * pointer now. Otherwise NULL the pointer and an array of segments 425244469Scognet * will be allocated later, on first use. We don't pre-allocate now 426244469Scognet * because some tags exist just to pass contraints to children in the 427244469Scognet * device hierarchy, and they tend to use BUS_SPACE_UNRESTRICTED and we 428244469Scognet * sure don't want to try to allocate an array for that. 429244469Scognet */ 430244469Scognet if (newtag->nsegments <= nitems(newtag->tagsegs)) 431244469Scognet newtag->segments = newtag->tagsegs; 432244469Scognet else 433244469Scognet newtag->segments = NULL; 434239268Sgonzo 435239268Sgonzo /* Take into account any restrictions imposed by our parent tag */ 436239268Sgonzo if (parent != NULL) { 437239268Sgonzo newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr); 438239268Sgonzo newtag->highaddr = MAX(parent->highaddr, newtag->highaddr); 439239268Sgonzo if (newtag->boundary == 0) 440239268Sgonzo newtag->boundary = parent->boundary; 441239268Sgonzo else if (parent->boundary != 0) 442239268Sgonzo newtag->boundary = MIN(parent->boundary, 443239268Sgonzo newtag->boundary); 444239268Sgonzo if ((newtag->filter != NULL) || 445239268Sgonzo ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0)) 446239268Sgonzo newtag->flags |= BUS_DMA_COULD_BOUNCE; 447239268Sgonzo if (newtag->filter == NULL) { 448239268Sgonzo /* 449239268Sgonzo * Short circuit looking at our parent directly 450239268Sgonzo * since we have encapsulated all of its information 451239268Sgonzo */ 452239268Sgonzo newtag->filter = parent->filter; 453239268Sgonzo newtag->filterarg = parent->filterarg; 454239268Sgonzo newtag->parent = parent->parent; 455239268Sgonzo } 456239268Sgonzo if (newtag->parent != NULL) 457239268Sgonzo atomic_add_int(&parent->ref_count, 1); 458239268Sgonzo } 459239268Sgonzo 460269206Sian if (exclusion_bounce(newtag->lowaddr, newtag->highaddr) 461269206Sian || alignment_bounce(newtag, 1)) 462239268Sgonzo newtag->flags |= BUS_DMA_COULD_BOUNCE; 463239268Sgonzo 464256637Sian /* 465256637Sian * Any request can auto-bounce due to cacheline alignment, in addition 466256637Sian * to any alignment or boundary specifications in the tag, so if the 467256637Sian * ALLOCNOW flag is set, there's always work to do. 468256637Sian */ 469254061Scognet if ((flags & BUS_DMA_ALLOCNOW) != 0) { 470239268Sgonzo struct bounce_zone *bz; 471256637Sian /* 472256637Sian * Round size up to a full page, and add one more page because 473256637Sian * there can always be one more boundary crossing than the 474256637Sian * number of pages in a transfer. 475256637Sian */ 476256637Sian maxsize = roundup2(maxsize, PAGE_SIZE) + PAGE_SIZE; 477256637Sian 478239268Sgonzo if ((error = alloc_bounce_zone(newtag)) != 0) { 479239268Sgonzo free(newtag, M_DEVBUF); 480239268Sgonzo return (error); 481239268Sgonzo } 482239268Sgonzo bz = newtag->bounce_zone; 483239268Sgonzo 484239268Sgonzo if (ptoa(bz->total_bpages) < maxsize) { 485239268Sgonzo int pages; 486239268Sgonzo 487239268Sgonzo pages = atop(maxsize) - bz->total_bpages; 488239268Sgonzo 489239268Sgonzo /* Add pages to our bounce pool */ 490239268Sgonzo if (alloc_bounce_pages(newtag, pages) < pages) 491239268Sgonzo error = ENOMEM; 492239268Sgonzo } 493239268Sgonzo /* Performed initial allocation */ 494239268Sgonzo newtag->flags |= BUS_DMA_MIN_ALLOC_COMP; 495239268Sgonzo } else 496239268Sgonzo newtag->bounce_zone = NULL; 497239268Sgonzo 498239268Sgonzo if (error != 0) { 499239268Sgonzo free(newtag, M_DEVBUF); 500239268Sgonzo } else { 501239268Sgonzo *dmat = newtag; 502239268Sgonzo } 503239268Sgonzo CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 504239268Sgonzo __func__, newtag, (newtag != NULL ? newtag->flags : 0), error); 505239268Sgonzo return (error); 506239268Sgonzo} 507239268Sgonzo 508239268Sgonzoint 509239268Sgonzobus_dma_tag_destroy(bus_dma_tag_t dmat) 510239268Sgonzo{ 511239268Sgonzo bus_dma_tag_t dmat_copy; 512239268Sgonzo int error; 513239268Sgonzo 514239268Sgonzo error = 0; 515239268Sgonzo dmat_copy = dmat; 516239268Sgonzo 517239268Sgonzo if (dmat != NULL) { 518239268Sgonzo 519239268Sgonzo if (dmat->map_count != 0) { 520239268Sgonzo error = EBUSY; 521239268Sgonzo goto out; 522239268Sgonzo } 523239268Sgonzo 524239268Sgonzo while (dmat != NULL) { 525239268Sgonzo bus_dma_tag_t parent; 526239268Sgonzo 527239268Sgonzo parent = dmat->parent; 528239268Sgonzo atomic_subtract_int(&dmat->ref_count, 1); 529239268Sgonzo if (dmat->ref_count == 0) { 530244469Scognet if (dmat->segments != NULL && 531244469Scognet dmat->segments != dmat->tagsegs) 532239268Sgonzo free(dmat->segments, M_DEVBUF); 533239268Sgonzo free(dmat, M_DEVBUF); 534239268Sgonzo /* 535239268Sgonzo * Last reference count, so 536239268Sgonzo * release our reference 537239268Sgonzo * count on our parent. 538239268Sgonzo */ 539239268Sgonzo dmat = parent; 540239268Sgonzo } else 541239268Sgonzo dmat = NULL; 542239268Sgonzo } 543239268Sgonzo } 544239268Sgonzoout: 545239268Sgonzo CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error); 546239268Sgonzo return (error); 547239268Sgonzo} 548239268Sgonzo 549254061Scognetstatic int allocate_bz_and_pages(bus_dma_tag_t dmat, bus_dmamap_t mapp) 550254061Scognet{ 551254061Scognet struct bounce_zone *bz; 552254061Scognet int maxpages; 553254061Scognet int error; 554254061Scognet 555254061Scognet if (dmat->bounce_zone == NULL) 556254061Scognet if ((error = alloc_bounce_zone(dmat)) != 0) 557254061Scognet return (error); 558254061Scognet bz = dmat->bounce_zone; 559254061Scognet /* Initialize the new map */ 560254061Scognet STAILQ_INIT(&(mapp->bpages)); 561254061Scognet 562254061Scognet /* 563256637Sian * Attempt to add pages to our pool on a per-instance basis up to a sane 564256637Sian * limit. Even if the tag isn't flagged as COULD_BOUNCE due to 565256637Sian * alignment and boundary constraints, it could still auto-bounce due to 566256637Sian * cacheline alignment, which requires at most two bounce pages. 567254061Scognet */ 568254229Scognet if (dmat->flags & BUS_DMA_COULD_BOUNCE) 569254229Scognet maxpages = MAX_BPAGES; 570254229Scognet else 571256637Sian maxpages = 2 * bz->map_count; 572254061Scognet if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 573254061Scognet || (bz->map_count > 0 && bz->total_bpages < maxpages)) { 574254061Scognet int pages; 575254061Scognet 576256637Sian pages = atop(roundup2(dmat->maxsize, PAGE_SIZE)) + 1; 577254061Scognet pages = MIN(maxpages - bz->total_bpages, pages); 578256637Sian pages = MAX(pages, 2); 579254061Scognet if (alloc_bounce_pages(dmat, pages) < pages) 580254061Scognet return (ENOMEM); 581254061Scognet 582254061Scognet if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) 583254061Scognet dmat->flags |= BUS_DMA_MIN_ALLOC_COMP; 584254061Scognet } 585254061Scognet bz->map_count++; 586254061Scognet return (0); 587254061Scognet} 588254061Scognet 589239268Sgonzo/* 590239268Sgonzo * Allocate a handle for mapping from kva/uva/physical 591239268Sgonzo * address space into bus device space. 592239268Sgonzo */ 593239268Sgonzoint 594239268Sgonzobus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp) 595239268Sgonzo{ 596246713Skib int mapsize; 597254061Scognet int error = 0; 598239268Sgonzo 599246713Skib mapsize = sizeof(**mapp) + (sizeof(struct sync_list) * dmat->nsegments); 600246713Skib *mapp = (bus_dmamap_t)malloc(mapsize, M_DEVBUF, M_NOWAIT | M_ZERO); 601239268Sgonzo if (*mapp == NULL) { 602239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM); 603239268Sgonzo return (ENOMEM); 604239268Sgonzo } 605246713Skib (*mapp)->sync_count = 0; 606239268Sgonzo 607239268Sgonzo if (dmat->segments == NULL) { 608239268Sgonzo dmat->segments = (bus_dma_segment_t *)malloc( 609239268Sgonzo sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF, 610239268Sgonzo M_NOWAIT); 611239268Sgonzo if (dmat->segments == NULL) { 612239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", 613239268Sgonzo __func__, dmat, ENOMEM); 614239268Sgonzo free(*mapp, M_DEVBUF); 615239268Sgonzo *mapp = NULL; 616239268Sgonzo return (ENOMEM); 617239268Sgonzo } 618239268Sgonzo } 619239268Sgonzo /* 620239268Sgonzo * Bouncing might be required if the driver asks for an active 621239268Sgonzo * exclusion region, a data alignment that is stricter than 1, and/or 622239268Sgonzo * an active address boundary. 623239268Sgonzo */ 624254061Scognet error = allocate_bz_and_pages(dmat, *mapp); 625254061Scognet if (error != 0) { 626254061Scognet free(*mapp, M_DEVBUF); 627254061Scognet *mapp = NULL; 628254061Scognet return (error); 629239268Sgonzo } 630239268Sgonzo return (error); 631239268Sgonzo} 632239268Sgonzo 633239268Sgonzo/* 634239268Sgonzo * Destroy a handle for mapping from kva/uva/physical 635239268Sgonzo * address space into bus device space. 636239268Sgonzo */ 637239268Sgonzoint 638239268Sgonzobus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map) 639239268Sgonzo{ 640246713Skib if (STAILQ_FIRST(&map->bpages) != NULL || map->sync_count != 0) { 641239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", 642239268Sgonzo __func__, dmat, EBUSY); 643239268Sgonzo return (EBUSY); 644239268Sgonzo } 645239268Sgonzo if (dmat->bounce_zone) 646239268Sgonzo dmat->bounce_zone->map_count--; 647239268Sgonzo free(map, M_DEVBUF); 648239268Sgonzo dmat->map_count--; 649239268Sgonzo CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat); 650239268Sgonzo return (0); 651239268Sgonzo} 652239268Sgonzo 653239268Sgonzo 654239268Sgonzo/* 655239268Sgonzo * Allocate a piece of memory that can be efficiently mapped into 656239268Sgonzo * bus device space based on the constraints lited in the dma tag. 657239268Sgonzo * A dmamap to for use with dmamap_load is also allocated. 658239268Sgonzo */ 659239268Sgonzoint 660239268Sgonzobus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags, 661239268Sgonzo bus_dmamap_t *mapp) 662239268Sgonzo{ 663244469Scognet busdma_bufalloc_t ba; 664244469Scognet struct busdma_bufzone *bufzone; 665244469Scognet vm_memattr_t memattr; 666244469Scognet int mflags; 667246713Skib int mapsize; 668254061Scognet int error; 669239268Sgonzo 670239268Sgonzo if (flags & BUS_DMA_NOWAIT) 671239268Sgonzo mflags = M_NOWAIT; 672239268Sgonzo else 673239268Sgonzo mflags = M_WAITOK; 674239268Sgonzo 675239268Sgonzo /* ARM non-snooping caches need a map for the VA cache sync structure */ 676239268Sgonzo 677246713Skib mapsize = sizeof(**mapp) + (sizeof(struct sync_list) * dmat->nsegments); 678246713Skib *mapp = (bus_dmamap_t)malloc(mapsize, M_DEVBUF, M_NOWAIT | M_ZERO); 679239268Sgonzo if (*mapp == NULL) { 680239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 681239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 682239268Sgonzo return (ENOMEM); 683239268Sgonzo } 684239268Sgonzo 685246713Skib (*mapp)->sync_count = 0; 686254061Scognet /* We may need bounce pages, even for allocated memory */ 687254061Scognet error = allocate_bz_and_pages(dmat, *mapp); 688254061Scognet if (error != 0) { 689254061Scognet free(*mapp, M_DEVBUF); 690254061Scognet *mapp = NULL; 691254061Scognet return (error); 692254061Scognet } 693239268Sgonzo 694239268Sgonzo if (dmat->segments == NULL) { 695239268Sgonzo dmat->segments = (bus_dma_segment_t *)malloc( 696239268Sgonzo sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF, 697239268Sgonzo mflags); 698239268Sgonzo if (dmat->segments == NULL) { 699239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 700239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 701239268Sgonzo free(*mapp, M_DEVBUF); 702239268Sgonzo *mapp = NULL; 703239268Sgonzo return (ENOMEM); 704239268Sgonzo } 705239268Sgonzo } 706239268Sgonzo 707239268Sgonzo if (flags & BUS_DMA_ZERO) 708239268Sgonzo mflags |= M_ZERO; 709244469Scognet if (flags & BUS_DMA_COHERENT) { 710244469Scognet memattr = VM_MEMATTR_UNCACHEABLE; 711244469Scognet ba = coherent_allocator; 712244469Scognet (*mapp)->flags |= DMAMAP_COHERENT; 713244469Scognet } else { 714244469Scognet memattr = VM_MEMATTR_DEFAULT; 715244469Scognet ba = standard_allocator; 716244469Scognet (*mapp)->flags = 0; 717244469Scognet } 718239268Sgonzo 719244469Scognet /* 720244469Scognet * Try to find a bufzone in the allocator that holds a cache of buffers 721244469Scognet * of the right size for this request. If the buffer is too big to be 722244469Scognet * held in the allocator cache, this returns NULL. 723239268Sgonzo */ 724244469Scognet bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 725244469Scognet 726244469Scognet /* 727244469Scognet * Allocate the buffer from the uma(9) allocator if... 728244469Scognet * - It's small enough to be in the allocator (bufzone not NULL). 729244469Scognet * - The alignment constraint isn't larger than the allocation size 730244469Scognet * (the allocator aligns buffers to their size boundaries). 731244469Scognet * - There's no need to handle lowaddr/highaddr exclusion zones. 732244469Scognet * else allocate non-contiguous pages if... 733244469Scognet * - The page count that could get allocated doesn't exceed nsegments. 734244469Scognet * - The alignment constraint isn't larger than a page boundary. 735244469Scognet * - There are no boundary-crossing constraints. 736244469Scognet * else allocate a block of contiguous pages because one or more of the 737244469Scognet * constraints is something that only the contig allocator can fulfill. 738244469Scognet */ 739244469Scognet if (bufzone != NULL && dmat->alignment <= bufzone->size && 740269206Sian !exclusion_bounce(dmat->lowaddr, dmat->highaddr)) { 741244469Scognet *vaddr = uma_zalloc(bufzone->umazone, mflags); 742244469Scognet } else if (dmat->nsegments >= btoc(dmat->maxsize) && 743244469Scognet dmat->alignment <= PAGE_SIZE && dmat->boundary == 0) { 744254025Sjeff *vaddr = (void *)kmem_alloc_attr(kernel_arena, dmat->maxsize, 745244469Scognet mflags, 0, dmat->lowaddr, memattr); 746239268Sgonzo } else { 747254025Sjeff *vaddr = (void *)kmem_alloc_contig(kernel_arena, dmat->maxsize, 748244469Scognet mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary, 749244469Scognet memattr); 750239268Sgonzo } 751244469Scognet 752244469Scognet 753239268Sgonzo if (*vaddr == NULL) { 754239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 755239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 756239268Sgonzo free(*mapp, M_DEVBUF); 757239268Sgonzo *mapp = NULL; 758239268Sgonzo return (ENOMEM); 759239268Sgonzo } 760239268Sgonzo dmat->map_count++; 761239268Sgonzo 762239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 763239268Sgonzo __func__, dmat, dmat->flags, 0); 764239268Sgonzo return (0); 765239268Sgonzo} 766239268Sgonzo 767239268Sgonzo/* 768239268Sgonzo * Free a piece of memory and it's allociated dmamap, that was allocated 769239268Sgonzo * via bus_dmamem_alloc. Make the same choice for free/contigfree. 770239268Sgonzo */ 771239268Sgonzovoid 772239268Sgonzobus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map) 773239268Sgonzo{ 774244469Scognet struct busdma_bufzone *bufzone; 775244469Scognet busdma_bufalloc_t ba; 776239268Sgonzo 777244469Scognet if (map->flags & DMAMAP_COHERENT) 778244469Scognet ba = coherent_allocator; 779244469Scognet else 780244469Scognet ba = standard_allocator; 781244469Scognet 782244469Scognet /* Be careful not to access map from here on. */ 783244469Scognet 784244469Scognet bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 785244469Scognet 786244469Scognet if (bufzone != NULL && dmat->alignment <= bufzone->size && 787269206Sian !exclusion_bounce(dmat->lowaddr, dmat->highaddr)) 788244469Scognet uma_zfree(bufzone->umazone, vaddr); 789244469Scognet else 790254025Sjeff kmem_free(kernel_arena, (vm_offset_t)vaddr, dmat->maxsize); 791244469Scognet 792239268Sgonzo dmat->map_count--; 793239268Sgonzo free(map, M_DEVBUF); 794239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags); 795239268Sgonzo} 796239268Sgonzo 797246713Skibstatic void 798246713Skib_bus_dmamap_count_phys(bus_dma_tag_t dmat, bus_dmamap_t map, vm_paddr_t buf, 799246713Skib bus_size_t buflen, int flags) 800246713Skib{ 801246713Skib bus_addr_t curaddr; 802246713Skib bus_size_t sgsize; 803246713Skib 804246713Skib if (map->pagesneeded == 0) { 805246713Skib CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d" 806246713Skib " map= %p, pagesneeded= %d", 807246713Skib dmat->lowaddr, dmat->boundary, dmat->alignment, 808246713Skib map, map->pagesneeded); 809246713Skib /* 810246713Skib * Count the number of bounce pages 811246713Skib * needed in order to complete this transfer 812246713Skib */ 813246713Skib curaddr = buf; 814246713Skib while (buflen != 0) { 815246713Skib sgsize = MIN(buflen, dmat->maxsegsz); 816254061Scognet if (run_filter(dmat, curaddr, sgsize, 817254061Scognet map->flags & DMAMAP_COHERENT) != 0) { 818246713Skib sgsize = MIN(sgsize, PAGE_SIZE); 819246713Skib map->pagesneeded++; 820246713Skib } 821246713Skib curaddr += sgsize; 822246713Skib buflen -= sgsize; 823246713Skib } 824246713Skib CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded); 825246713Skib } 826246713Skib} 827246713Skib 828246713Skibstatic void 829239268Sgonzo_bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 830239268Sgonzo void *buf, bus_size_t buflen, int flags) 831239268Sgonzo{ 832239268Sgonzo vm_offset_t vaddr; 833239268Sgonzo vm_offset_t vendaddr; 834239268Sgonzo bus_addr_t paddr; 835239268Sgonzo 836239268Sgonzo if (map->pagesneeded == 0) { 837239268Sgonzo CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d" 838239268Sgonzo " map= %p, pagesneeded= %d", 839239268Sgonzo dmat->lowaddr, dmat->boundary, dmat->alignment, 840239268Sgonzo map, map->pagesneeded); 841239268Sgonzo /* 842239268Sgonzo * Count the number of bounce pages 843239268Sgonzo * needed in order to complete this transfer 844239268Sgonzo */ 845239268Sgonzo vaddr = (vm_offset_t)buf; 846239268Sgonzo vendaddr = (vm_offset_t)buf + buflen; 847239268Sgonzo 848239268Sgonzo while (vaddr < vendaddr) { 849246713Skib if (__predict_true(map->pmap == kernel_pmap)) 850239268Sgonzo paddr = pmap_kextract(vaddr); 851239268Sgonzo else 852239268Sgonzo paddr = pmap_extract(map->pmap, vaddr); 853254061Scognet if (run_filter(dmat, paddr, 854254061Scognet min(vendaddr - vaddr, 855254061Scognet (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK))), 856254061Scognet map->flags & DMAMAP_COHERENT) != 0) { 857239268Sgonzo map->pagesneeded++; 858239268Sgonzo } 859239268Sgonzo vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK)); 860239268Sgonzo 861239268Sgonzo } 862239268Sgonzo CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded); 863239268Sgonzo } 864246713Skib} 865239268Sgonzo 866246713Skibstatic int 867246713Skib_bus_dmamap_reserve_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int flags) 868246713Skib{ 869246713Skib 870239268Sgonzo /* Reserve Necessary Bounce Pages */ 871246713Skib mtx_lock(&bounce_lock); 872246713Skib if (flags & BUS_DMA_NOWAIT) { 873246713Skib if (reserve_bounce_pages(dmat, map, 0) != 0) { 874246713Skib map->pagesneeded = 0; 875246713Skib mtx_unlock(&bounce_lock); 876246713Skib return (ENOMEM); 877239268Sgonzo } 878246713Skib } else { 879246713Skib if (reserve_bounce_pages(dmat, map, 1) != 0) { 880246713Skib /* Queue us for resources */ 881246713Skib STAILQ_INSERT_TAIL(&bounce_map_waitinglist, map, links); 882246713Skib mtx_unlock(&bounce_lock); 883246713Skib return (EINPROGRESS); 884246713Skib } 885239268Sgonzo } 886246713Skib mtx_unlock(&bounce_lock); 887239268Sgonzo 888239268Sgonzo return (0); 889239268Sgonzo} 890239268Sgonzo 891239268Sgonzo/* 892246713Skib * Add a single contiguous physical range to the segment list. 893246713Skib */ 894246713Skibstatic int 895246713Skib_bus_dmamap_addseg(bus_dma_tag_t dmat, bus_dmamap_t map, bus_addr_t curaddr, 896246713Skib bus_size_t sgsize, bus_dma_segment_t *segs, int *segp) 897246713Skib{ 898246713Skib bus_addr_t baddr, bmask; 899246713Skib int seg; 900246713Skib 901246713Skib /* 902246713Skib * Make sure we don't cross any boundaries. 903246713Skib */ 904246713Skib bmask = ~(dmat->boundary - 1); 905246713Skib if (dmat->boundary > 0) { 906246713Skib baddr = (curaddr + dmat->boundary) & bmask; 907246713Skib if (sgsize > (baddr - curaddr)) 908246713Skib sgsize = (baddr - curaddr); 909246713Skib } 910246713Skib 911246713Skib if (dmat->ranges) { 912246713Skib struct arm32_dma_range *dr; 913246713Skib 914246713Skib dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges, 915246713Skib curaddr); 916246713Skib if (dr == NULL) { 917246713Skib _bus_dmamap_unload(dmat, map); 918246881Sian return (0); 919246713Skib } 920246713Skib /* 921246713Skib * In a valid DMA range. Translate the physical 922246713Skib * memory address to an address in the DMA window. 923246713Skib */ 924246713Skib curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase; 925246713Skib } 926246713Skib 927246713Skib /* 928246713Skib * Insert chunk into a segment, coalescing with 929246713Skib * previous segment if possible. 930246713Skib */ 931246713Skib seg = *segp; 932246713Skib if (seg == -1) { 933246713Skib seg = 0; 934246713Skib segs[seg].ds_addr = curaddr; 935246713Skib segs[seg].ds_len = sgsize; 936246713Skib } else { 937246713Skib if (curaddr == segs[seg].ds_addr + segs[seg].ds_len && 938246713Skib (segs[seg].ds_len + sgsize) <= dmat->maxsegsz && 939246713Skib (dmat->boundary == 0 || 940246713Skib (segs[seg].ds_addr & bmask) == (curaddr & bmask))) 941246713Skib segs[seg].ds_len += sgsize; 942246713Skib else { 943246713Skib if (++seg >= dmat->nsegments) 944246713Skib return (0); 945246713Skib segs[seg].ds_addr = curaddr; 946246713Skib segs[seg].ds_len = sgsize; 947246713Skib } 948246713Skib } 949246713Skib *segp = seg; 950246713Skib return (sgsize); 951246713Skib} 952246713Skib 953246713Skib/* 954246713Skib * Utility function to load a physical buffer. segp contains 955239268Sgonzo * the starting segment on entrace, and the ending segment on exit. 956239268Sgonzo */ 957246713Skibint 958246713Skib_bus_dmamap_load_phys(bus_dma_tag_t dmat, 959246713Skib bus_dmamap_t map, 960246713Skib vm_paddr_t buf, bus_size_t buflen, 961246713Skib int flags, 962246713Skib bus_dma_segment_t *segs, 963246713Skib int *segp) 964246713Skib{ 965246713Skib bus_addr_t curaddr; 966246713Skib bus_size_t sgsize; 967246713Skib int error; 968246713Skib 969246713Skib if (segs == NULL) 970246713Skib segs = dmat->segments; 971246713Skib 972254061Scognet if (((map->flags & DMAMAP_COHERENT) == 0) || 973254061Scognet (dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) { 974246713Skib _bus_dmamap_count_phys(dmat, map, buf, buflen, flags); 975246713Skib if (map->pagesneeded != 0) { 976246713Skib error = _bus_dmamap_reserve_pages(dmat, map, flags); 977246713Skib if (error) 978246713Skib return (error); 979246713Skib } 980246713Skib } 981246713Skib 982246713Skib while (buflen > 0) { 983246713Skib curaddr = buf; 984246713Skib sgsize = MIN(buflen, dmat->maxsegsz); 985254061Scognet if ((((map->flags & DMAMAP_COHERENT) == 0) || 986254061Scognet ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) && 987254061Scognet map->pagesneeded != 0 && run_filter(dmat, curaddr, 988254061Scognet sgsize, map->flags & DMAMAP_COHERENT)) { 989246713Skib sgsize = MIN(sgsize, PAGE_SIZE); 990246713Skib curaddr = add_bounce_page(dmat, map, 0, curaddr, 991246713Skib sgsize); 992246713Skib } 993246713Skib sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs, 994246713Skib segp); 995246713Skib if (sgsize == 0) 996246713Skib break; 997246713Skib buf += sgsize; 998246713Skib buflen -= sgsize; 999246713Skib } 1000246713Skib 1001246713Skib /* 1002246713Skib * Did we fit? 1003246713Skib */ 1004246713Skib if (buflen != 0) { 1005246713Skib _bus_dmamap_unload(dmat, map); 1006246713Skib return (EFBIG); /* XXX better return value here? */ 1007246713Skib } 1008246713Skib return (0); 1009246713Skib} 1010246713Skib 1011257228Skibint 1012257228Skib_bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map, 1013257228Skib struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags, 1014257228Skib bus_dma_segment_t *segs, int *segp) 1015257228Skib{ 1016257228Skib 1017257228Skib return (bus_dmamap_load_ma_triv(dmat, map, ma, tlen, ma_offs, flags, 1018257228Skib segs, segp)); 1019257228Skib} 1020257228Skib 1021246713Skib/* 1022246713Skib * Utility function to load a linear buffer. segp contains 1023246713Skib * the starting segment on entrace, and the ending segment on exit. 1024246713Skib */ 1025246713Skibint 1026239268Sgonzo_bus_dmamap_load_buffer(bus_dma_tag_t dmat, 1027239268Sgonzo bus_dmamap_t map, 1028239268Sgonzo void *buf, bus_size_t buflen, 1029246713Skib pmap_t pmap, 1030239268Sgonzo int flags, 1031239268Sgonzo bus_dma_segment_t *segs, 1032246713Skib int *segp) 1033239268Sgonzo{ 1034239268Sgonzo bus_size_t sgsize; 1035246713Skib bus_addr_t curaddr; 1036239268Sgonzo vm_offset_t vaddr; 1037239268Sgonzo struct sync_list *sl; 1038246713Skib int error; 1039239268Sgonzo 1040246713Skib if (segs == NULL) 1041246713Skib segs = dmat->segments; 1042246713Skib 1043246859Sian map->pmap = pmap; 1044246859Sian 1045254061Scognet if (!(map->flags & DMAMAP_COHERENT) || 1046254061Scognet (dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) { 1047246713Skib _bus_dmamap_count_pages(dmat, map, buf, buflen, flags); 1048246713Skib if (map->pagesneeded != 0) { 1049246713Skib error = _bus_dmamap_reserve_pages(dmat, map, flags); 1050246713Skib if (error) 1051246713Skib return (error); 1052246713Skib } 1053239268Sgonzo } 1054239268Sgonzo 1055239268Sgonzo sl = NULL; 1056239268Sgonzo vaddr = (vm_offset_t)buf; 1057239268Sgonzo 1058246713Skib while (buflen > 0) { 1059239268Sgonzo /* 1060239268Sgonzo * Get the physical address for this segment. 1061239268Sgonzo */ 1062246713Skib if (__predict_true(map->pmap == kernel_pmap)) 1063239268Sgonzo curaddr = pmap_kextract(vaddr); 1064239268Sgonzo else 1065239268Sgonzo curaddr = pmap_extract(map->pmap, vaddr); 1066239268Sgonzo 1067239268Sgonzo /* 1068239268Sgonzo * Compute the segment size, and adjust counts. 1069239268Sgonzo */ 1070239268Sgonzo sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK); 1071239268Sgonzo if (sgsize > dmat->maxsegsz) 1072239268Sgonzo sgsize = dmat->maxsegsz; 1073239268Sgonzo if (buflen < sgsize) 1074239268Sgonzo sgsize = buflen; 1075239268Sgonzo 1076254061Scognet if ((((map->flags & DMAMAP_COHERENT) == 0) || 1077254061Scognet ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0)) && 1078254061Scognet map->pagesneeded != 0 && run_filter(dmat, curaddr, 1079254061Scognet sgsize, map->flags & DMAMAP_COHERENT)) { 1080246713Skib curaddr = add_bounce_page(dmat, map, vaddr, curaddr, 1081246713Skib sgsize); 1082239268Sgonzo } else { 1083246713Skib sl = &map->slist[map->sync_count - 1]; 1084246713Skib if (map->sync_count == 0 || 1085247776Scognet#ifdef ARM_L2_PIPT 1086247776Scognet curaddr != sl->busaddr + sl->datacount || 1087247776Scognet#endif 1088246713Skib vaddr != sl->vaddr + sl->datacount) { 1089246713Skib if (++map->sync_count > dmat->nsegments) 1090246713Skib goto cleanup; 1091246713Skib sl++; 1092246713Skib sl->vaddr = vaddr; 1093246713Skib sl->datacount = sgsize; 1094246713Skib sl->busaddr = curaddr; 1095246713Skib } else 1096246713Skib sl->datacount += sgsize; 1097239268Sgonzo } 1098246713Skib sgsize = _bus_dmamap_addseg(dmat, map, curaddr, sgsize, segs, 1099246713Skib segp); 1100246713Skib if (sgsize == 0) 1101246713Skib break; 1102239268Sgonzo vaddr += sgsize; 1103239268Sgonzo buflen -= sgsize; 1104239268Sgonzo } 1105239268Sgonzo 1106239268Sgonzocleanup: 1107239268Sgonzo /* 1108239268Sgonzo * Did we fit? 1109239268Sgonzo */ 1110239268Sgonzo if (buflen != 0) { 1111239268Sgonzo _bus_dmamap_unload(dmat, map); 1112246713Skib return (EFBIG); /* XXX better return value here? */ 1113239268Sgonzo } 1114239268Sgonzo return (0); 1115239268Sgonzo} 1116239268Sgonzo 1117246713Skib 1118246713Skibvoid 1119246713Skib__bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map, 1120246713Skib struct memdesc *mem, bus_dmamap_callback_t *callback, 1121246713Skib void *callback_arg) 1122239268Sgonzo{ 1123239268Sgonzo 1124246713Skib map->mem = *mem; 1125246713Skib map->dmat = dmat; 1126239268Sgonzo map->callback = callback; 1127239268Sgonzo map->callback_arg = callback_arg; 1128239268Sgonzo} 1129239268Sgonzo 1130246713Skibbus_dma_segment_t * 1131246713Skib_bus_dmamap_complete(bus_dma_tag_t dmat, bus_dmamap_t map, 1132246713Skib bus_dma_segment_t *segs, int nsegs, int error) 1133239268Sgonzo{ 1134239268Sgonzo 1135246713Skib if (segs == NULL) 1136246713Skib segs = dmat->segments; 1137246713Skib return (segs); 1138239268Sgonzo} 1139239268Sgonzo 1140239268Sgonzo/* 1141239268Sgonzo * Release the mapping held by map. 1142239268Sgonzo */ 1143239268Sgonzovoid 1144239268Sgonzo_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map) 1145239268Sgonzo{ 1146239268Sgonzo struct bounce_page *bpage; 1147239268Sgonzo struct bounce_zone *bz; 1148239268Sgonzo 1149239268Sgonzo if ((bz = dmat->bounce_zone) != NULL) { 1150239268Sgonzo while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1151239268Sgonzo STAILQ_REMOVE_HEAD(&map->bpages, links); 1152239268Sgonzo free_bounce_page(dmat, bpage); 1153239268Sgonzo } 1154239268Sgonzo 1155239268Sgonzo bz = dmat->bounce_zone; 1156239268Sgonzo bz->free_bpages += map->pagesreserved; 1157239268Sgonzo bz->reserved_bpages -= map->pagesreserved; 1158239268Sgonzo map->pagesreserved = 0; 1159239268Sgonzo map->pagesneeded = 0; 1160239268Sgonzo } 1161246713Skib map->sync_count = 0; 1162239268Sgonzo} 1163239268Sgonzo 1164239268Sgonzo#ifdef notyetbounceuser 1165239268Sgonzo /* If busdma uses user pages, then the interrupt handler could 1166239268Sgonzo * be use the kernel vm mapping. Both bounce pages and sync list 1167239268Sgonzo * do not cross page boundaries. 1168239268Sgonzo * Below is a rough sequence that a person would do to fix the 1169239268Sgonzo * user page reference in the kernel vmspace. This would be 1170239268Sgonzo * done in the dma post routine. 1171239268Sgonzo */ 1172239268Sgonzovoid 1173239268Sgonzo_bus_dmamap_fix_user(vm_offset_t buf, bus_size_t len, 1174239268Sgonzo pmap_t pmap, int op) 1175239268Sgonzo{ 1176239268Sgonzo bus_size_t sgsize; 1177239268Sgonzo bus_addr_t curaddr; 1178239268Sgonzo vm_offset_t va; 1179239268Sgonzo 1180239268Sgonzo /* each synclist entry is contained within a single page. 1181239268Sgonzo * 1182239268Sgonzo * this would be needed if BUS_DMASYNC_POSTxxxx was implemented 1183239268Sgonzo */ 1184239268Sgonzo curaddr = pmap_extract(pmap, buf); 1185239268Sgonzo va = pmap_dma_map(curaddr); 1186239268Sgonzo switch (op) { 1187239268Sgonzo case SYNC_USER_INV: 1188239268Sgonzo cpu_dcache_wb_range(va, sgsize); 1189239268Sgonzo break; 1190239268Sgonzo 1191239268Sgonzo case SYNC_USER_COPYTO: 1192239268Sgonzo bcopy((void *)va, (void *)bounce, sgsize); 1193239268Sgonzo break; 1194239268Sgonzo 1195239268Sgonzo case SYNC_USER_COPYFROM: 1196239268Sgonzo bcopy((void *) bounce, (void *)va, sgsize); 1197239268Sgonzo break; 1198239268Sgonzo 1199239268Sgonzo default: 1200239268Sgonzo break; 1201239268Sgonzo } 1202239268Sgonzo 1203239268Sgonzo pmap_dma_unmap(va); 1204239268Sgonzo} 1205239268Sgonzo#endif 1206239268Sgonzo 1207239268Sgonzo#ifdef ARM_L2_PIPT 1208239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(pa, size) 1209239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(pa, size) 1210239268Sgonzo#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(pa, size) 1211239268Sgonzo#else 1212239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(va, size) 1213239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(va, size) 1214243909Scognet#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(va, size) 1215239268Sgonzo#endif 1216239268Sgonzo 1217239268Sgonzovoid 1218239268Sgonzo_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op) 1219239268Sgonzo{ 1220239268Sgonzo struct bounce_page *bpage; 1221246713Skib struct sync_list *sl, *end; 1222248655Sian /* 1223248655Sian * If the buffer was from user space, it is possible that this is not 1224248655Sian * the same vm map, especially on a POST operation. It's not clear that 1225248655Sian * dma on userland buffers can work at all right now, certainly not if a 1226248655Sian * partial cacheline flush has to be handled. To be safe, until we're 1227248655Sian * able to test direct userland dma, panic on a map mismatch. 1228248655Sian */ 1229239268Sgonzo if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1230248655Sian if (!pmap_dmap_iscurrent(map->pmap)) 1231248655Sian panic("_bus_dmamap_sync: wrong user map for bounce sync."); 1232239268Sgonzo /* Handle data bouncing. */ 1233239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1234239268Sgonzo "performing bounce", __func__, dmat, dmat->flags, op); 1235239268Sgonzo 1236239268Sgonzo if (op & BUS_DMASYNC_PREWRITE) { 1237239268Sgonzo while (bpage != NULL) { 1238246713Skib if (bpage->datavaddr != 0) 1239246713Skib bcopy((void *)bpage->datavaddr, 1240246713Skib (void *)bpage->vaddr, 1241246713Skib bpage->datacount); 1242246713Skib else 1243246713Skib physcopyout(bpage->dataaddr, 1244246713Skib (void *)bpage->vaddr, 1245246713Skib bpage->datacount); 1246239268Sgonzo cpu_dcache_wb_range((vm_offset_t)bpage->vaddr, 1247239268Sgonzo bpage->datacount); 1248239268Sgonzo l2cache_wb_range((vm_offset_t)bpage->vaddr, 1249239268Sgonzo (vm_offset_t)bpage->busaddr, 1250239268Sgonzo bpage->datacount); 1251239268Sgonzo bpage = STAILQ_NEXT(bpage, links); 1252239268Sgonzo } 1253239268Sgonzo dmat->bounce_zone->total_bounced++; 1254239268Sgonzo } 1255239268Sgonzo 1256261418Scognet if (op & BUS_DMASYNC_PREREAD) { 1257261418Scognet bpage = STAILQ_FIRST(&map->bpages); 1258261418Scognet while (bpage != NULL) { 1259261418Scognet cpu_dcache_inv_range((vm_offset_t)bpage->vaddr, 1260261418Scognet bpage->datacount); 1261261418Scognet l2cache_inv_range((vm_offset_t)bpage->vaddr, 1262261418Scognet (vm_offset_t)bpage->busaddr, 1263261418Scognet bpage->datacount); 1264261418Scognet bpage = STAILQ_NEXT(bpage, links); 1265261418Scognet } 1266261418Scognet } 1267239268Sgonzo if (op & BUS_DMASYNC_POSTREAD) { 1268239268Sgonzo while (bpage != NULL) { 1269239268Sgonzo vm_offset_t startv; 1270239268Sgonzo vm_paddr_t startp; 1271239268Sgonzo int len; 1272239268Sgonzo 1273239268Sgonzo startv = bpage->vaddr &~ arm_dcache_align_mask; 1274239268Sgonzo startp = bpage->busaddr &~ arm_dcache_align_mask; 1275239268Sgonzo len = bpage->datacount; 1276239268Sgonzo 1277239268Sgonzo if (startv != bpage->vaddr) 1278239268Sgonzo len += bpage->vaddr & arm_dcache_align_mask; 1279239268Sgonzo if (len & arm_dcache_align_mask) 1280239268Sgonzo len = (len - 1281239268Sgonzo (len & arm_dcache_align_mask)) + 1282239268Sgonzo arm_dcache_align; 1283239268Sgonzo cpu_dcache_inv_range(startv, len); 1284239268Sgonzo l2cache_inv_range(startv, startp, len); 1285246713Skib if (bpage->datavaddr != 0) 1286246713Skib bcopy((void *)bpage->vaddr, 1287246713Skib (void *)bpage->datavaddr, 1288246713Skib bpage->datacount); 1289246713Skib else 1290246713Skib physcopyin((void *)bpage->vaddr, 1291246713Skib bpage->dataaddr, 1292246713Skib bpage->datacount); 1293239268Sgonzo bpage = STAILQ_NEXT(bpage, links); 1294239268Sgonzo } 1295239268Sgonzo dmat->bounce_zone->total_bounced++; 1296239268Sgonzo } 1297239268Sgonzo } 1298244469Scognet if (map->flags & DMAMAP_COHERENT) 1299244469Scognet return; 1300239268Sgonzo 1301246713Skib if (map->sync_count != 0) { 1302248655Sian if (!pmap_dmap_iscurrent(map->pmap)) 1303248655Sian panic("_bus_dmamap_sync: wrong user map for sync."); 1304239268Sgonzo /* ARM caches are not self-snooping for dma */ 1305239268Sgonzo 1306246713Skib sl = &map->slist[0]; 1307246713Skib end = &map->slist[map->sync_count]; 1308239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1309239268Sgonzo "performing sync", __func__, dmat, dmat->flags, op); 1310239268Sgonzo 1311239268Sgonzo switch (op) { 1312239268Sgonzo case BUS_DMASYNC_PREWRITE: 1313246713Skib while (sl != end) { 1314239268Sgonzo cpu_dcache_wb_range(sl->vaddr, sl->datacount); 1315239268Sgonzo l2cache_wb_range(sl->vaddr, sl->busaddr, 1316239268Sgonzo sl->datacount); 1317246713Skib sl++; 1318239268Sgonzo } 1319239268Sgonzo break; 1320239268Sgonzo 1321239268Sgonzo case BUS_DMASYNC_PREREAD: 1322246713Skib while (sl != end) { 1323254061Scognet cpu_dcache_inv_range(sl->vaddr, sl->datacount); 1324254061Scognet l2cache_inv_range(sl->vaddr, sl->busaddr, 1325254061Scognet sl->datacount); 1326246713Skib sl++; 1327239268Sgonzo } 1328239268Sgonzo break; 1329239268Sgonzo 1330239268Sgonzo case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD: 1331246713Skib while (sl != end) { 1332239268Sgonzo cpu_dcache_wbinv_range(sl->vaddr, sl->datacount); 1333239268Sgonzo l2cache_wbinv_range(sl->vaddr, 1334239268Sgonzo sl->busaddr, sl->datacount); 1335246713Skib sl++; 1336239268Sgonzo } 1337239268Sgonzo break; 1338239268Sgonzo 1339256638Sian case BUS_DMASYNC_POSTREAD: 1340256638Sian case BUS_DMASYNC_POSTWRITE: 1341256638Sian case BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE: 1342256638Sian break; 1343239268Sgonzo default: 1344256638Sian panic("unsupported combination of sync operations: 0x%08x\n", op); 1345239268Sgonzo break; 1346239268Sgonzo } 1347239268Sgonzo } 1348239268Sgonzo} 1349239268Sgonzo 1350239268Sgonzostatic void 1351239268Sgonzoinit_bounce_pages(void *dummy __unused) 1352239268Sgonzo{ 1353239268Sgonzo 1354239268Sgonzo total_bpages = 0; 1355239268Sgonzo STAILQ_INIT(&bounce_zone_list); 1356239268Sgonzo STAILQ_INIT(&bounce_map_waitinglist); 1357239268Sgonzo STAILQ_INIT(&bounce_map_callbacklist); 1358239268Sgonzo mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF); 1359239268Sgonzo} 1360239268SgonzoSYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL); 1361239268Sgonzo 1362239268Sgonzostatic struct sysctl_ctx_list * 1363239268Sgonzobusdma_sysctl_tree(struct bounce_zone *bz) 1364239268Sgonzo{ 1365239268Sgonzo return (&bz->sysctl_tree); 1366239268Sgonzo} 1367239268Sgonzo 1368239268Sgonzostatic struct sysctl_oid * 1369239268Sgonzobusdma_sysctl_tree_top(struct bounce_zone *bz) 1370239268Sgonzo{ 1371239268Sgonzo return (bz->sysctl_tree_top); 1372239268Sgonzo} 1373239268Sgonzo 1374239268Sgonzostatic int 1375239268Sgonzoalloc_bounce_zone(bus_dma_tag_t dmat) 1376239268Sgonzo{ 1377239268Sgonzo struct bounce_zone *bz; 1378239268Sgonzo 1379239268Sgonzo /* Check to see if we already have a suitable zone */ 1380239268Sgonzo STAILQ_FOREACH(bz, &bounce_zone_list, links) { 1381239268Sgonzo if ((dmat->alignment <= bz->alignment) 1382239268Sgonzo && (dmat->lowaddr >= bz->lowaddr)) { 1383239268Sgonzo dmat->bounce_zone = bz; 1384239268Sgonzo return (0); 1385239268Sgonzo } 1386239268Sgonzo } 1387239268Sgonzo 1388239268Sgonzo if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF, 1389239268Sgonzo M_NOWAIT | M_ZERO)) == NULL) 1390239268Sgonzo return (ENOMEM); 1391239268Sgonzo 1392239268Sgonzo STAILQ_INIT(&bz->bounce_page_list); 1393239268Sgonzo bz->free_bpages = 0; 1394239268Sgonzo bz->reserved_bpages = 0; 1395239268Sgonzo bz->active_bpages = 0; 1396239268Sgonzo bz->lowaddr = dmat->lowaddr; 1397239268Sgonzo bz->alignment = MAX(dmat->alignment, PAGE_SIZE); 1398239268Sgonzo bz->map_count = 0; 1399239268Sgonzo snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount); 1400239268Sgonzo busdma_zonecount++; 1401239268Sgonzo snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr); 1402239268Sgonzo STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links); 1403239268Sgonzo dmat->bounce_zone = bz; 1404239268Sgonzo 1405239268Sgonzo sysctl_ctx_init(&bz->sysctl_tree); 1406239268Sgonzo bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree, 1407239268Sgonzo SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid, 1408239268Sgonzo CTLFLAG_RD, 0, ""); 1409239268Sgonzo if (bz->sysctl_tree_top == NULL) { 1410239268Sgonzo sysctl_ctx_free(&bz->sysctl_tree); 1411239268Sgonzo return (0); /* XXX error code? */ 1412239268Sgonzo } 1413239268Sgonzo 1414239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1415239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1416239268Sgonzo "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0, 1417239268Sgonzo "Total bounce pages"); 1418239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1419239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1420239268Sgonzo "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0, 1421239268Sgonzo "Free bounce pages"); 1422239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1423239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1424239268Sgonzo "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0, 1425239268Sgonzo "Reserved bounce pages"); 1426239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1427239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1428239268Sgonzo "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0, 1429239268Sgonzo "Active bounce pages"); 1430239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1431239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1432239268Sgonzo "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0, 1433239268Sgonzo "Total bounce requests"); 1434239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1435239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1436239268Sgonzo "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0, 1437239268Sgonzo "Total bounce requests that were deferred"); 1438239268Sgonzo SYSCTL_ADD_STRING(busdma_sysctl_tree(bz), 1439239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1440239268Sgonzo "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, ""); 1441239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1442239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1443239268Sgonzo "alignment", CTLFLAG_RD, &bz->alignment, 0, ""); 1444239268Sgonzo 1445239268Sgonzo return (0); 1446239268Sgonzo} 1447239268Sgonzo 1448239268Sgonzostatic int 1449239268Sgonzoalloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages) 1450239268Sgonzo{ 1451239268Sgonzo struct bounce_zone *bz; 1452239268Sgonzo int count; 1453239268Sgonzo 1454239268Sgonzo bz = dmat->bounce_zone; 1455239268Sgonzo count = 0; 1456239268Sgonzo while (numpages > 0) { 1457239268Sgonzo struct bounce_page *bpage; 1458239268Sgonzo 1459239268Sgonzo bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF, 1460239268Sgonzo M_NOWAIT | M_ZERO); 1461239268Sgonzo 1462239268Sgonzo if (bpage == NULL) 1463239268Sgonzo break; 1464239268Sgonzo bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF, 1465239268Sgonzo M_NOWAIT, 0ul, 1466239268Sgonzo bz->lowaddr, 1467239268Sgonzo PAGE_SIZE, 1468239268Sgonzo 0); 1469239268Sgonzo if (bpage->vaddr == 0) { 1470239268Sgonzo free(bpage, M_DEVBUF); 1471239268Sgonzo break; 1472239268Sgonzo } 1473239268Sgonzo bpage->busaddr = pmap_kextract(bpage->vaddr); 1474239268Sgonzo mtx_lock(&bounce_lock); 1475239268Sgonzo STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links); 1476239268Sgonzo total_bpages++; 1477239268Sgonzo bz->total_bpages++; 1478239268Sgonzo bz->free_bpages++; 1479239268Sgonzo mtx_unlock(&bounce_lock); 1480239268Sgonzo count++; 1481239268Sgonzo numpages--; 1482239268Sgonzo } 1483239268Sgonzo return (count); 1484239268Sgonzo} 1485239268Sgonzo 1486239268Sgonzostatic int 1487239268Sgonzoreserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit) 1488239268Sgonzo{ 1489239268Sgonzo struct bounce_zone *bz; 1490239268Sgonzo int pages; 1491239268Sgonzo 1492239268Sgonzo mtx_assert(&bounce_lock, MA_OWNED); 1493239268Sgonzo bz = dmat->bounce_zone; 1494239268Sgonzo pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved); 1495239268Sgonzo if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages)) 1496239268Sgonzo return (map->pagesneeded - (map->pagesreserved + pages)); 1497239268Sgonzo bz->free_bpages -= pages; 1498239268Sgonzo bz->reserved_bpages += pages; 1499239268Sgonzo map->pagesreserved += pages; 1500239268Sgonzo pages = map->pagesneeded - map->pagesreserved; 1501239268Sgonzo 1502239268Sgonzo return (pages); 1503239268Sgonzo} 1504239268Sgonzo 1505239268Sgonzostatic bus_addr_t 1506239268Sgonzoadd_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr, 1507246713Skib bus_addr_t addr, bus_size_t size) 1508239268Sgonzo{ 1509239268Sgonzo struct bounce_zone *bz; 1510239268Sgonzo struct bounce_page *bpage; 1511239268Sgonzo 1512239268Sgonzo KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag")); 1513239268Sgonzo KASSERT(map != NULL, 1514239268Sgonzo ("add_bounce_page: bad map %p", map)); 1515239268Sgonzo 1516239268Sgonzo bz = dmat->bounce_zone; 1517239268Sgonzo if (map->pagesneeded == 0) 1518239268Sgonzo panic("add_bounce_page: map doesn't need any pages"); 1519239268Sgonzo map->pagesneeded--; 1520239268Sgonzo 1521239268Sgonzo if (map->pagesreserved == 0) 1522239268Sgonzo panic("add_bounce_page: map doesn't need any pages"); 1523239268Sgonzo map->pagesreserved--; 1524239268Sgonzo 1525239268Sgonzo mtx_lock(&bounce_lock); 1526239268Sgonzo bpage = STAILQ_FIRST(&bz->bounce_page_list); 1527239268Sgonzo if (bpage == NULL) 1528239268Sgonzo panic("add_bounce_page: free page list is empty"); 1529239268Sgonzo 1530239268Sgonzo STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links); 1531239268Sgonzo bz->reserved_bpages--; 1532239268Sgonzo bz->active_bpages++; 1533239268Sgonzo mtx_unlock(&bounce_lock); 1534239268Sgonzo 1535239268Sgonzo if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1536239268Sgonzo /* Page offset needs to be preserved. */ 1537239268Sgonzo bpage->vaddr |= vaddr & PAGE_MASK; 1538239268Sgonzo bpage->busaddr |= vaddr & PAGE_MASK; 1539239268Sgonzo } 1540239268Sgonzo bpage->datavaddr = vaddr; 1541246713Skib bpage->dataaddr = addr; 1542239268Sgonzo bpage->datacount = size; 1543239268Sgonzo STAILQ_INSERT_TAIL(&(map->bpages), bpage, links); 1544239268Sgonzo return (bpage->busaddr); 1545239268Sgonzo} 1546239268Sgonzo 1547239268Sgonzostatic void 1548239268Sgonzofree_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage) 1549239268Sgonzo{ 1550239268Sgonzo struct bus_dmamap *map; 1551239268Sgonzo struct bounce_zone *bz; 1552239268Sgonzo 1553239268Sgonzo bz = dmat->bounce_zone; 1554239268Sgonzo bpage->datavaddr = 0; 1555239268Sgonzo bpage->datacount = 0; 1556239268Sgonzo if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1557239268Sgonzo /* 1558239268Sgonzo * Reset the bounce page to start at offset 0. Other uses 1559239268Sgonzo * of this bounce page may need to store a full page of 1560239268Sgonzo * data and/or assume it starts on a page boundary. 1561239268Sgonzo */ 1562239268Sgonzo bpage->vaddr &= ~PAGE_MASK; 1563239268Sgonzo bpage->busaddr &= ~PAGE_MASK; 1564239268Sgonzo } 1565239268Sgonzo 1566239268Sgonzo mtx_lock(&bounce_lock); 1567239268Sgonzo STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links); 1568239268Sgonzo bz->free_bpages++; 1569239268Sgonzo bz->active_bpages--; 1570239268Sgonzo if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) { 1571239268Sgonzo if (reserve_bounce_pages(map->dmat, map, 1) == 0) { 1572239268Sgonzo STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links); 1573239268Sgonzo STAILQ_INSERT_TAIL(&bounce_map_callbacklist, 1574239268Sgonzo map, links); 1575239268Sgonzo busdma_swi_pending = 1; 1576239268Sgonzo bz->total_deferred++; 1577239268Sgonzo swi_sched(vm_ih, 0); 1578239268Sgonzo } 1579239268Sgonzo } 1580239268Sgonzo mtx_unlock(&bounce_lock); 1581239268Sgonzo} 1582239268Sgonzo 1583239268Sgonzovoid 1584239268Sgonzobusdma_swi(void) 1585239268Sgonzo{ 1586239268Sgonzo bus_dma_tag_t dmat; 1587239268Sgonzo struct bus_dmamap *map; 1588239268Sgonzo 1589239268Sgonzo mtx_lock(&bounce_lock); 1590239268Sgonzo while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) { 1591239268Sgonzo STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links); 1592239268Sgonzo mtx_unlock(&bounce_lock); 1593239268Sgonzo dmat = map->dmat; 1594239268Sgonzo (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK); 1595246713Skib bus_dmamap_load_mem(map->dmat, map, &map->mem, map->callback, 1596246713Skib map->callback_arg, BUS_DMA_WAITOK); 1597239268Sgonzo (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK); 1598239268Sgonzo mtx_lock(&bounce_lock); 1599239268Sgonzo } 1600239268Sgonzo mtx_unlock(&bounce_lock); 1601239268Sgonzo} 1602