busdma_machdep-v6.c revision 244469
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 244469 2012-12-20 00:35:26Z cognet $"); 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> 49239268Sgonzo#include <sys/proc.h> 50239268Sgonzo#include <sys/mutex.h> 51239268Sgonzo#include <sys/mbuf.h> 52239268Sgonzo#include <sys/uio.h> 53239268Sgonzo#include <sys/sysctl.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 70239268Sgonzo#define FIX_DMAP_BUS_DMASYNC_POSTREAD 71239268Sgonzo 72239268Sgonzostruct bounce_zone; 73239268Sgonzo 74239268Sgonzostruct bus_dma_tag { 75239268Sgonzo bus_dma_tag_t parent; 76239268Sgonzo bus_size_t alignment; 77239268Sgonzo bus_size_t boundary; 78239268Sgonzo bus_addr_t lowaddr; 79239268Sgonzo bus_addr_t highaddr; 80239268Sgonzo bus_dma_filter_t *filter; 81239268Sgonzo void *filterarg; 82239268Sgonzo bus_size_t maxsize; 83239268Sgonzo u_int nsegments; 84239268Sgonzo bus_size_t maxsegsz; 85239268Sgonzo int flags; 86239268Sgonzo int ref_count; 87239268Sgonzo int map_count; 88239268Sgonzo bus_dma_lock_t *lockfunc; 89239268Sgonzo void *lockfuncarg; 90239268Sgonzo struct bounce_zone *bounce_zone; 91239268Sgonzo /* 92239268Sgonzo * DMA range for this tag. If the page doesn't fall within 93239268Sgonzo * one of these ranges, an error is returned. The caller 94239268Sgonzo * may then decide what to do with the transfer. If the 95239268Sgonzo * range pointer is NULL, it is ignored. 96239268Sgonzo */ 97239268Sgonzo struct arm32_dma_range *ranges; 98239268Sgonzo int _nranges; 99244469Scognet /* 100244469Scognet * Most tags need one or two segments, and can use the local tagsegs 101244469Scognet * array. For tags with a larger limit, we'll allocate a bigger array 102244469Scognet * on first use. 103244469Scognet */ 104244469Scognet bus_dma_segment_t *segments; 105244469Scognet bus_dma_segment_t tagsegs[2]; 106239268Sgonzo 107244469Scognet 108239268Sgonzo}; 109239268Sgonzo 110239268Sgonzostruct bounce_page { 111239268Sgonzo vm_offset_t vaddr; /* kva of bounce buffer */ 112239268Sgonzo bus_addr_t busaddr; /* Physical address */ 113239268Sgonzo vm_offset_t datavaddr; /* kva of client data */ 114239268Sgonzo bus_size_t datacount; /* client data count */ 115239268Sgonzo STAILQ_ENTRY(bounce_page) links; 116239268Sgonzo}; 117239268Sgonzo 118239268Sgonzostruct sync_list { 119239268Sgonzo vm_offset_t vaddr; /* kva of bounce buffer */ 120239268Sgonzo bus_addr_t busaddr; /* Physical address */ 121239268Sgonzo bus_size_t datacount; /* client data count */ 122239268Sgonzo STAILQ_ENTRY(sync_list) slinks; 123239268Sgonzo}; 124239268Sgonzo 125239268Sgonzoint busdma_swi_pending; 126239268Sgonzo 127239268Sgonzostruct bounce_zone { 128239268Sgonzo STAILQ_ENTRY(bounce_zone) links; 129239268Sgonzo STAILQ_HEAD(bp_list, bounce_page) bounce_page_list; 130239268Sgonzo int total_bpages; 131239268Sgonzo int free_bpages; 132239268Sgonzo int reserved_bpages; 133239268Sgonzo int active_bpages; 134239268Sgonzo int total_bounced; 135239268Sgonzo int total_deferred; 136239268Sgonzo int map_count; 137239268Sgonzo bus_size_t alignment; 138239268Sgonzo bus_addr_t lowaddr; 139239268Sgonzo char zoneid[8]; 140239268Sgonzo char lowaddrid[20]; 141239268Sgonzo struct sysctl_ctx_list sysctl_tree; 142239268Sgonzo struct sysctl_oid *sysctl_tree_top; 143239268Sgonzo}; 144239268Sgonzo 145239268Sgonzostatic struct mtx bounce_lock; 146239268Sgonzostatic int total_bpages; 147239268Sgonzostatic int busdma_zonecount; 148239268Sgonzostatic STAILQ_HEAD(, bounce_zone) bounce_zone_list; 149239268Sgonzo 150239268SgonzoSYSCTL_NODE(_hw, OID_AUTO, busdma, CTLFLAG_RD, 0, "Busdma parameters"); 151239268SgonzoSYSCTL_INT(_hw_busdma, OID_AUTO, total_bpages, CTLFLAG_RD, &total_bpages, 0, 152239268Sgonzo "Total bounce pages"); 153239268Sgonzo 154239268Sgonzostruct bus_dmamap { 155239268Sgonzo struct bp_list bpages; 156239268Sgonzo int pagesneeded; 157239268Sgonzo int pagesreserved; 158239268Sgonzo bus_dma_tag_t dmat; 159239268Sgonzo void *buf; /* unmapped buffer pointer */ 160239268Sgonzo bus_size_t buflen; /* unmapped buffer length */ 161239268Sgonzo pmap_t pmap; 162239268Sgonzo bus_dmamap_callback_t *callback; 163239268Sgonzo void *callback_arg; 164244469Scognet int flags; 165244469Scognet#define DMAMAP_COHERENT (1 << 0) 166239268Sgonzo STAILQ_ENTRY(bus_dmamap) links; 167239268Sgonzo STAILQ_HEAD(,sync_list) slist; 168239268Sgonzo}; 169239268Sgonzo 170239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_waitinglist; 171239268Sgonzostatic STAILQ_HEAD(, bus_dmamap) bounce_map_callbacklist; 172239268Sgonzo 173239268Sgonzostatic void init_bounce_pages(void *dummy); 174239268Sgonzostatic int alloc_bounce_zone(bus_dma_tag_t dmat); 175239268Sgonzostatic int alloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages); 176239268Sgonzostatic int reserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 177239268Sgonzo int commit); 178239268Sgonzostatic bus_addr_t add_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, 179239268Sgonzo vm_offset_t vaddr, bus_size_t size); 180239268Sgonzostatic void free_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage); 181239268Sgonzoint run_filter(bus_dma_tag_t dmat, bus_addr_t paddr); 182239268Sgonzostatic int _bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 183239268Sgonzo void *buf, bus_size_t buflen, int flags); 184239268Sgonzo 185244469Scognetstatic busdma_bufalloc_t coherent_allocator; /* Cache of coherent buffers */ 186244469Scognetstatic busdma_bufalloc_t standard_allocator; /* Cache of standard buffers */ 187244469Scognetstatic void 188244469Scognetbusdma_init(void *dummy) 189244469Scognet{ 190244469Scognet 191244469Scognet /* Create a cache of buffers in standard (cacheable) memory. */ 192244469Scognet standard_allocator = busdma_bufalloc_create("buffer", 193244469Scognet arm_dcache_align, /* minimum_alignment */ 194244469Scognet NULL, /* uma_alloc func */ 195244469Scognet NULL, /* uma_free func */ 196244469Scognet 0); /* uma_zcreate_flags */ 197244469Scognet 198244469Scognet /* 199244469Scognet * Create a cache of buffers in uncacheable memory, to implement the 200244469Scognet * BUS_DMA_COHERENT (and potentially BUS_DMA_NOCACHE) flag. 201244469Scognet */ 202244469Scognet coherent_allocator = busdma_bufalloc_create("coherent", 203244469Scognet arm_dcache_align, /* minimum_alignment */ 204244469Scognet busdma_bufalloc_alloc_uncacheable, 205244469Scognet busdma_bufalloc_free_uncacheable, 206244469Scognet 0); /* uma_zcreate_flags */ 207244469Scognet} 208244469Scognet 209244469Scognet/* 210244469Scognet * This init historically used SI_SUB_VM, but now the init code requires 211244469Scognet * malloc(9) using M_DEVBUF memory, which is set up later than SI_SUB_VM, by 212244469Scognet * SI_SUB_KMEM and SI_ORDER_SECOND, so we'll go right after that by using 213244469Scognet * SI_SUB_KMEM and SI_ORDER_THIRD. 214244469Scognet */ 215244469ScognetSYSINIT(busdma, SI_SUB_KMEM, SI_ORDER_THIRD, busdma_init, NULL); 216244469Scognet 217239268Sgonzostatic __inline int 218239268Sgonzo_bus_dma_can_bounce(vm_offset_t lowaddr, vm_offset_t highaddr) 219239268Sgonzo{ 220239268Sgonzo int i; 221239268Sgonzo for (i = 0; phys_avail[i] && phys_avail[i + 1]; i += 2) { 222239268Sgonzo if ((lowaddr >= phys_avail[i] && lowaddr <= phys_avail[i + 1]) 223239268Sgonzo || (lowaddr < phys_avail[i] && 224239268Sgonzo highaddr > phys_avail[i])) 225239268Sgonzo return (1); 226239268Sgonzo } 227239268Sgonzo return (0); 228239268Sgonzo} 229239268Sgonzo 230239268Sgonzostatic __inline struct arm32_dma_range * 231239268Sgonzo_bus_dma_inrange(struct arm32_dma_range *ranges, int nranges, 232239268Sgonzo bus_addr_t curaddr) 233239268Sgonzo{ 234239268Sgonzo struct arm32_dma_range *dr; 235239268Sgonzo int i; 236239268Sgonzo 237239268Sgonzo for (i = 0, dr = ranges; i < nranges; i++, dr++) { 238239268Sgonzo if (curaddr >= dr->dr_sysbase && 239239268Sgonzo round_page(curaddr) <= (dr->dr_sysbase + dr->dr_len)) 240239268Sgonzo return (dr); 241239268Sgonzo } 242239268Sgonzo 243239268Sgonzo return (NULL); 244239268Sgonzo} 245239268Sgonzo 246239268Sgonzo/* 247239268Sgonzo * Return true if a match is made. 248239268Sgonzo * 249239268Sgonzo * To find a match walk the chain of bus_dma_tag_t's looking for 'paddr'. 250239268Sgonzo * 251239268Sgonzo * If paddr is within the bounds of the dma tag then call the filter callback 252239268Sgonzo * to check for a match, if there is no filter callback then assume a match. 253239268Sgonzo */ 254239268Sgonzoint 255239268Sgonzorun_filter(bus_dma_tag_t dmat, bus_addr_t paddr) 256239268Sgonzo{ 257239268Sgonzo int retval; 258239268Sgonzo 259239268Sgonzo retval = 0; 260239268Sgonzo 261239268Sgonzo do { 262239268Sgonzo if (((paddr > dmat->lowaddr && paddr <= dmat->highaddr) 263239268Sgonzo || ((paddr & (dmat->alignment - 1)) != 0)) 264239268Sgonzo && (dmat->filter == NULL 265239268Sgonzo || (*dmat->filter)(dmat->filterarg, paddr) != 0)) 266239268Sgonzo retval = 1; 267239268Sgonzo 268239268Sgonzo dmat = dmat->parent; 269239268Sgonzo } while (retval == 0 && dmat != NULL); 270239268Sgonzo return (retval); 271239268Sgonzo} 272239268Sgonzo 273239268Sgonzo/* 274239268Sgonzo * Convenience function for manipulating driver locks from busdma (during 275239268Sgonzo * busdma_swi, for example). Drivers that don't provide their own locks 276239268Sgonzo * should specify &Giant to dmat->lockfuncarg. Drivers that use their own 277239268Sgonzo * non-mutex locking scheme don't have to use this at all. 278239268Sgonzo */ 279239268Sgonzovoid 280239268Sgonzobusdma_lock_mutex(void *arg, bus_dma_lock_op_t op) 281239268Sgonzo{ 282239268Sgonzo struct mtx *dmtx; 283239268Sgonzo 284239268Sgonzo dmtx = (struct mtx *)arg; 285239268Sgonzo switch (op) { 286239268Sgonzo case BUS_DMA_LOCK: 287239268Sgonzo mtx_lock(dmtx); 288239268Sgonzo break; 289239268Sgonzo case BUS_DMA_UNLOCK: 290239268Sgonzo mtx_unlock(dmtx); 291239268Sgonzo break; 292239268Sgonzo default: 293239268Sgonzo panic("Unknown operation 0x%x for busdma_lock_mutex!", op); 294239268Sgonzo } 295239268Sgonzo} 296239268Sgonzo 297239268Sgonzo/* 298239268Sgonzo * dflt_lock should never get called. It gets put into the dma tag when 299239268Sgonzo * lockfunc == NULL, which is only valid if the maps that are associated 300239268Sgonzo * with the tag are meant to never be defered. 301239268Sgonzo * XXX Should have a way to identify which driver is responsible here. 302239268Sgonzo */ 303239268Sgonzostatic void 304239268Sgonzodflt_lock(void *arg, bus_dma_lock_op_t op) 305239268Sgonzo{ 306239268Sgonzo panic("driver error: busdma dflt_lock called"); 307239268Sgonzo} 308239268Sgonzo 309239268Sgonzo/* 310239268Sgonzo * Allocate a device specific dma_tag. 311239268Sgonzo */ 312239268Sgonzoint 313239268Sgonzobus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment, 314239268Sgonzo bus_size_t boundary, bus_addr_t lowaddr, 315239268Sgonzo bus_addr_t highaddr, bus_dma_filter_t *filter, 316239268Sgonzo void *filterarg, bus_size_t maxsize, int nsegments, 317239268Sgonzo bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc, 318239268Sgonzo void *lockfuncarg, bus_dma_tag_t *dmat) 319239268Sgonzo{ 320239268Sgonzo bus_dma_tag_t newtag; 321239268Sgonzo int error = 0; 322239268Sgonzo 323239268Sgonzo#if 0 324239268Sgonzo if (!parent) 325239268Sgonzo parent = arm_root_dma_tag; 326239268Sgonzo#endif 327239268Sgonzo 328239268Sgonzo /* Basic sanity checking */ 329239268Sgonzo if (boundary != 0 && boundary < maxsegsz) 330239268Sgonzo maxsegsz = boundary; 331239268Sgonzo 332239268Sgonzo /* Return a NULL tag on failure */ 333239268Sgonzo *dmat = NULL; 334239268Sgonzo 335239268Sgonzo if (maxsegsz == 0) { 336239268Sgonzo return (EINVAL); 337239268Sgonzo } 338239268Sgonzo 339239268Sgonzo newtag = (bus_dma_tag_t)malloc(sizeof(*newtag), M_DEVBUF, 340239268Sgonzo M_ZERO | M_NOWAIT); 341239268Sgonzo if (newtag == NULL) { 342239268Sgonzo CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 343239268Sgonzo __func__, newtag, 0, error); 344239268Sgonzo return (ENOMEM); 345239268Sgonzo } 346239268Sgonzo 347239268Sgonzo newtag->parent = parent; 348239268Sgonzo newtag->alignment = alignment; 349239268Sgonzo newtag->boundary = boundary; 350239268Sgonzo newtag->lowaddr = trunc_page((vm_paddr_t)lowaddr) + (PAGE_SIZE - 1); 351239268Sgonzo newtag->highaddr = trunc_page((vm_paddr_t)highaddr) + 352239268Sgonzo (PAGE_SIZE - 1); 353239268Sgonzo newtag->filter = filter; 354239268Sgonzo newtag->filterarg = filterarg; 355239268Sgonzo newtag->maxsize = maxsize; 356239268Sgonzo newtag->nsegments = nsegments; 357239268Sgonzo newtag->maxsegsz = maxsegsz; 358239268Sgonzo newtag->flags = flags; 359239268Sgonzo newtag->ref_count = 1; /* Count ourself */ 360239268Sgonzo newtag->map_count = 0; 361239268Sgonzo newtag->ranges = bus_dma_get_range(); 362239268Sgonzo newtag->_nranges = bus_dma_get_range_nb(); 363239268Sgonzo if (lockfunc != NULL) { 364239268Sgonzo newtag->lockfunc = lockfunc; 365239268Sgonzo newtag->lockfuncarg = lockfuncarg; 366239268Sgonzo } else { 367239268Sgonzo newtag->lockfunc = dflt_lock; 368239268Sgonzo newtag->lockfuncarg = NULL; 369239268Sgonzo } 370244469Scognet /* 371244469Scognet * If all the segments we need fit into the local tagsegs array, set the 372244469Scognet * pointer now. Otherwise NULL the pointer and an array of segments 373244469Scognet * will be allocated later, on first use. We don't pre-allocate now 374244469Scognet * because some tags exist just to pass contraints to children in the 375244469Scognet * device hierarchy, and they tend to use BUS_SPACE_UNRESTRICTED and we 376244469Scognet * sure don't want to try to allocate an array for that. 377244469Scognet */ 378244469Scognet if (newtag->nsegments <= nitems(newtag->tagsegs)) 379244469Scognet newtag->segments = newtag->tagsegs; 380244469Scognet else 381244469Scognet newtag->segments = NULL; 382239268Sgonzo 383239268Sgonzo /* Take into account any restrictions imposed by our parent tag */ 384239268Sgonzo if (parent != NULL) { 385239268Sgonzo newtag->lowaddr = MIN(parent->lowaddr, newtag->lowaddr); 386239268Sgonzo newtag->highaddr = MAX(parent->highaddr, newtag->highaddr); 387239268Sgonzo if (newtag->boundary == 0) 388239268Sgonzo newtag->boundary = parent->boundary; 389239268Sgonzo else if (parent->boundary != 0) 390239268Sgonzo newtag->boundary = MIN(parent->boundary, 391239268Sgonzo newtag->boundary); 392239268Sgonzo if ((newtag->filter != NULL) || 393239268Sgonzo ((parent->flags & BUS_DMA_COULD_BOUNCE) != 0)) 394239268Sgonzo newtag->flags |= BUS_DMA_COULD_BOUNCE; 395239268Sgonzo if (newtag->filter == NULL) { 396239268Sgonzo /* 397239268Sgonzo * Short circuit looking at our parent directly 398239268Sgonzo * since we have encapsulated all of its information 399239268Sgonzo */ 400239268Sgonzo newtag->filter = parent->filter; 401239268Sgonzo newtag->filterarg = parent->filterarg; 402239268Sgonzo newtag->parent = parent->parent; 403239268Sgonzo } 404239268Sgonzo if (newtag->parent != NULL) 405239268Sgonzo atomic_add_int(&parent->ref_count, 1); 406239268Sgonzo } 407239268Sgonzo 408239268Sgonzo if (_bus_dma_can_bounce(newtag->lowaddr, newtag->highaddr) 409239268Sgonzo || newtag->alignment > 1) 410239268Sgonzo newtag->flags |= BUS_DMA_COULD_BOUNCE; 411239268Sgonzo 412239268Sgonzo if (((newtag->flags & BUS_DMA_COULD_BOUNCE) != 0) && 413239268Sgonzo (flags & BUS_DMA_ALLOCNOW) != 0) { 414239268Sgonzo struct bounce_zone *bz; 415239268Sgonzo 416239268Sgonzo /* Must bounce */ 417239268Sgonzo 418239268Sgonzo if ((error = alloc_bounce_zone(newtag)) != 0) { 419239268Sgonzo free(newtag, M_DEVBUF); 420239268Sgonzo return (error); 421239268Sgonzo } 422239268Sgonzo bz = newtag->bounce_zone; 423239268Sgonzo 424239268Sgonzo if (ptoa(bz->total_bpages) < maxsize) { 425239268Sgonzo int pages; 426239268Sgonzo 427239268Sgonzo pages = atop(maxsize) - bz->total_bpages; 428239268Sgonzo 429239268Sgonzo /* Add pages to our bounce pool */ 430239268Sgonzo if (alloc_bounce_pages(newtag, pages) < pages) 431239268Sgonzo error = ENOMEM; 432239268Sgonzo } 433239268Sgonzo /* Performed initial allocation */ 434239268Sgonzo newtag->flags |= BUS_DMA_MIN_ALLOC_COMP; 435239268Sgonzo } else 436239268Sgonzo newtag->bounce_zone = NULL; 437239268Sgonzo 438239268Sgonzo if (error != 0) { 439239268Sgonzo free(newtag, M_DEVBUF); 440239268Sgonzo } else { 441239268Sgonzo *dmat = newtag; 442239268Sgonzo } 443239268Sgonzo CTR4(KTR_BUSDMA, "%s returned tag %p tag flags 0x%x error %d", 444239268Sgonzo __func__, newtag, (newtag != NULL ? newtag->flags : 0), error); 445239268Sgonzo return (error); 446239268Sgonzo} 447239268Sgonzo 448239268Sgonzoint 449239268Sgonzobus_dma_tag_destroy(bus_dma_tag_t dmat) 450239268Sgonzo{ 451239268Sgonzo bus_dma_tag_t dmat_copy; 452239268Sgonzo int error; 453239268Sgonzo 454239268Sgonzo error = 0; 455239268Sgonzo dmat_copy = dmat; 456239268Sgonzo 457239268Sgonzo if (dmat != NULL) { 458239268Sgonzo 459239268Sgonzo if (dmat->map_count != 0) { 460239268Sgonzo error = EBUSY; 461239268Sgonzo goto out; 462239268Sgonzo } 463239268Sgonzo 464239268Sgonzo while (dmat != NULL) { 465239268Sgonzo bus_dma_tag_t parent; 466239268Sgonzo 467239268Sgonzo parent = dmat->parent; 468239268Sgonzo atomic_subtract_int(&dmat->ref_count, 1); 469239268Sgonzo if (dmat->ref_count == 0) { 470244469Scognet if (dmat->segments != NULL && 471244469Scognet dmat->segments != dmat->tagsegs) 472239268Sgonzo free(dmat->segments, M_DEVBUF); 473239268Sgonzo free(dmat, M_DEVBUF); 474239268Sgonzo /* 475239268Sgonzo * Last reference count, so 476239268Sgonzo * release our reference 477239268Sgonzo * count on our parent. 478239268Sgonzo */ 479239268Sgonzo dmat = parent; 480239268Sgonzo } else 481239268Sgonzo dmat = NULL; 482239268Sgonzo } 483239268Sgonzo } 484239268Sgonzoout: 485239268Sgonzo CTR3(KTR_BUSDMA, "%s tag %p error %d", __func__, dmat_copy, error); 486239268Sgonzo return (error); 487239268Sgonzo} 488239268Sgonzo 489239268Sgonzo/* 490239268Sgonzo * Allocate a handle for mapping from kva/uva/physical 491239268Sgonzo * address space into bus device space. 492239268Sgonzo */ 493239268Sgonzoint 494239268Sgonzobus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp) 495239268Sgonzo{ 496239268Sgonzo int error; 497239268Sgonzo 498239268Sgonzo error = 0; 499239268Sgonzo 500239268Sgonzo *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF, 501239268Sgonzo M_NOWAIT | M_ZERO); 502239268Sgonzo if (*mapp == NULL) { 503239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", __func__, dmat, ENOMEM); 504239268Sgonzo return (ENOMEM); 505239268Sgonzo } 506239268Sgonzo STAILQ_INIT(&((*mapp)->slist)); 507239268Sgonzo 508239268Sgonzo if (dmat->segments == NULL) { 509239268Sgonzo dmat->segments = (bus_dma_segment_t *)malloc( 510239268Sgonzo sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF, 511239268Sgonzo M_NOWAIT); 512239268Sgonzo if (dmat->segments == NULL) { 513239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", 514239268Sgonzo __func__, dmat, ENOMEM); 515239268Sgonzo free(*mapp, M_DEVBUF); 516239268Sgonzo *mapp = NULL; 517239268Sgonzo return (ENOMEM); 518239268Sgonzo } 519239268Sgonzo } 520239268Sgonzo /* 521239268Sgonzo * Bouncing might be required if the driver asks for an active 522239268Sgonzo * exclusion region, a data alignment that is stricter than 1, and/or 523239268Sgonzo * an active address boundary. 524239268Sgonzo */ 525239268Sgonzo if (dmat->flags & BUS_DMA_COULD_BOUNCE) { 526239268Sgonzo 527239268Sgonzo /* Must bounce */ 528239268Sgonzo struct bounce_zone *bz; 529239268Sgonzo int maxpages; 530239268Sgonzo 531239268Sgonzo if (dmat->bounce_zone == NULL) { 532239268Sgonzo if ((error = alloc_bounce_zone(dmat)) != 0) { 533239268Sgonzo free(*mapp, M_DEVBUF); 534239268Sgonzo *mapp = NULL; 535239268Sgonzo return (error); 536239268Sgonzo } 537239268Sgonzo } 538239268Sgonzo bz = dmat->bounce_zone; 539239268Sgonzo 540239268Sgonzo /* Initialize the new map */ 541239268Sgonzo STAILQ_INIT(&((*mapp)->bpages)); 542239268Sgonzo 543239268Sgonzo /* 544239268Sgonzo * Attempt to add pages to our pool on a per-instance 545239268Sgonzo * basis up to a sane limit. 546239268Sgonzo */ 547239268Sgonzo maxpages = MAX_BPAGES; 548239268Sgonzo if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0 549239268Sgonzo || (bz->map_count > 0 && bz->total_bpages < maxpages)) { 550239268Sgonzo int pages; 551239268Sgonzo 552239268Sgonzo pages = MAX(atop(dmat->maxsize), 1); 553239268Sgonzo pages = MIN(maxpages - bz->total_bpages, pages); 554239268Sgonzo pages = MAX(pages, 1); 555239268Sgonzo if (alloc_bounce_pages(dmat, pages) < pages) 556239268Sgonzo error = ENOMEM; 557239268Sgonzo 558239268Sgonzo if ((dmat->flags & BUS_DMA_MIN_ALLOC_COMP) == 0) { 559239268Sgonzo if (error == 0) 560239268Sgonzo dmat->flags |= BUS_DMA_MIN_ALLOC_COMP; 561239268Sgonzo } else { 562239268Sgonzo error = 0; 563239268Sgonzo } 564239268Sgonzo } 565239268Sgonzo bz->map_count++; 566239268Sgonzo } 567239268Sgonzo if (error == 0) 568239268Sgonzo dmat->map_count++; 569239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 570239268Sgonzo __func__, dmat, dmat->flags, error); 571239268Sgonzo return (error); 572239268Sgonzo} 573239268Sgonzo 574239268Sgonzo/* 575239268Sgonzo * Destroy a handle for mapping from kva/uva/physical 576239268Sgonzo * address space into bus device space. 577239268Sgonzo */ 578239268Sgonzoint 579239268Sgonzobus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map) 580239268Sgonzo{ 581239268Sgonzo if (STAILQ_FIRST(&map->bpages) != NULL || 582239268Sgonzo STAILQ_FIRST(&map->slist) != NULL) { 583239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p error %d", 584239268Sgonzo __func__, dmat, EBUSY); 585239268Sgonzo return (EBUSY); 586239268Sgonzo } 587239268Sgonzo if (dmat->bounce_zone) 588239268Sgonzo dmat->bounce_zone->map_count--; 589239268Sgonzo free(map, M_DEVBUF); 590239268Sgonzo dmat->map_count--; 591239268Sgonzo CTR2(KTR_BUSDMA, "%s: tag %p error 0", __func__, dmat); 592239268Sgonzo return (0); 593239268Sgonzo} 594239268Sgonzo 595239268Sgonzo 596239268Sgonzo/* 597239268Sgonzo * Allocate a piece of memory that can be efficiently mapped into 598239268Sgonzo * bus device space based on the constraints lited in the dma tag. 599239268Sgonzo * A dmamap to for use with dmamap_load is also allocated. 600239268Sgonzo */ 601239268Sgonzoint 602239268Sgonzobus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags, 603239268Sgonzo bus_dmamap_t *mapp) 604239268Sgonzo{ 605244469Scognet busdma_bufalloc_t ba; 606244469Scognet struct busdma_bufzone *bufzone; 607244469Scognet vm_memattr_t memattr; 608244469Scognet int mflags; 609239268Sgonzo 610239268Sgonzo if (flags & BUS_DMA_NOWAIT) 611239268Sgonzo mflags = M_NOWAIT; 612239268Sgonzo else 613239268Sgonzo mflags = M_WAITOK; 614239268Sgonzo 615239268Sgonzo /* ARM non-snooping caches need a map for the VA cache sync structure */ 616239268Sgonzo 617239268Sgonzo *mapp = (bus_dmamap_t)malloc(sizeof(**mapp), M_DEVBUF, 618239268Sgonzo M_NOWAIT | M_ZERO); 619239268Sgonzo if (*mapp == NULL) { 620239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 621239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 622239268Sgonzo return (ENOMEM); 623239268Sgonzo } 624239268Sgonzo 625239268Sgonzo STAILQ_INIT(&((*mapp)->slist)); 626239268Sgonzo 627239268Sgonzo if (dmat->segments == NULL) { 628239268Sgonzo dmat->segments = (bus_dma_segment_t *)malloc( 629239268Sgonzo sizeof(bus_dma_segment_t) * dmat->nsegments, M_DEVBUF, 630239268Sgonzo mflags); 631239268Sgonzo if (dmat->segments == NULL) { 632239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 633239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 634239268Sgonzo free(*mapp, M_DEVBUF); 635239268Sgonzo *mapp = NULL; 636239268Sgonzo return (ENOMEM); 637239268Sgonzo } 638239268Sgonzo } 639239268Sgonzo 640239268Sgonzo if (flags & BUS_DMA_ZERO) 641239268Sgonzo mflags |= M_ZERO; 642244469Scognet if (flags & BUS_DMA_COHERENT) { 643244469Scognet memattr = VM_MEMATTR_UNCACHEABLE; 644244469Scognet ba = coherent_allocator; 645244469Scognet (*mapp)->flags |= DMAMAP_COHERENT; 646244469Scognet } else { 647244469Scognet memattr = VM_MEMATTR_DEFAULT; 648244469Scognet ba = standard_allocator; 649244469Scognet (*mapp)->flags = 0; 650244469Scognet } 651244469Scognet#ifdef notyet 652244469Scognet /* All buffers we allocate are cache-aligned. */ 653244469Scognet map->flags |= DMAMAP_CACHE_ALIGNED; 654244469Scognet#endif 655239268Sgonzo 656244469Scognet /* 657244469Scognet * Try to find a bufzone in the allocator that holds a cache of buffers 658244469Scognet * of the right size for this request. If the buffer is too big to be 659244469Scognet * held in the allocator cache, this returns NULL. 660239268Sgonzo */ 661244469Scognet bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 662244469Scognet 663244469Scognet /* 664244469Scognet * Allocate the buffer from the uma(9) allocator if... 665244469Scognet * - It's small enough to be in the allocator (bufzone not NULL). 666244469Scognet * - The alignment constraint isn't larger than the allocation size 667244469Scognet * (the allocator aligns buffers to their size boundaries). 668244469Scognet * - There's no need to handle lowaddr/highaddr exclusion zones. 669244469Scognet * else allocate non-contiguous pages if... 670244469Scognet * - The page count that could get allocated doesn't exceed nsegments. 671244469Scognet * - The alignment constraint isn't larger than a page boundary. 672244469Scognet * - There are no boundary-crossing constraints. 673244469Scognet * else allocate a block of contiguous pages because one or more of the 674244469Scognet * constraints is something that only the contig allocator can fulfill. 675244469Scognet */ 676244469Scognet if (bufzone != NULL && dmat->alignment <= bufzone->size && 677244469Scognet !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr)) { 678244469Scognet *vaddr = uma_zalloc(bufzone->umazone, mflags); 679244469Scognet } else if (dmat->nsegments >= btoc(dmat->maxsize) && 680244469Scognet dmat->alignment <= PAGE_SIZE && dmat->boundary == 0) { 681244469Scognet *vaddr = (void *)kmem_alloc_attr(kernel_map, dmat->maxsize, 682244469Scognet mflags, 0, dmat->lowaddr, memattr); 683239268Sgonzo } else { 684244469Scognet *vaddr = (void *)kmem_alloc_contig(kernel_map, dmat->maxsize, 685244469Scognet mflags, 0, dmat->lowaddr, dmat->alignment, dmat->boundary, 686244469Scognet memattr); 687239268Sgonzo } 688244469Scognet 689244469Scognet 690239268Sgonzo if (*vaddr == NULL) { 691239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 692239268Sgonzo __func__, dmat, dmat->flags, ENOMEM); 693239268Sgonzo free(*mapp, M_DEVBUF); 694239268Sgonzo *mapp = NULL; 695239268Sgonzo return (ENOMEM); 696239268Sgonzo } else if ((uintptr_t)*vaddr & (dmat->alignment - 1)) { 697239268Sgonzo printf("bus_dmamem_alloc failed to align memory properly.\n"); 698239268Sgonzo } 699239268Sgonzo dmat->map_count++; 700239268Sgonzo 701239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", 702239268Sgonzo __func__, dmat, dmat->flags, 0); 703239268Sgonzo return (0); 704239268Sgonzo} 705239268Sgonzo 706239268Sgonzo/* 707239268Sgonzo * Free a piece of memory and it's allociated dmamap, that was allocated 708239268Sgonzo * via bus_dmamem_alloc. Make the same choice for free/contigfree. 709239268Sgonzo */ 710239268Sgonzovoid 711239268Sgonzobus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map) 712239268Sgonzo{ 713244469Scognet struct busdma_bufzone *bufzone; 714244469Scognet busdma_bufalloc_t ba; 715239268Sgonzo 716244469Scognet if (map->flags & DMAMAP_COHERENT) 717244469Scognet ba = coherent_allocator; 718244469Scognet else 719244469Scognet ba = standard_allocator; 720244469Scognet 721244469Scognet /* Be careful not to access map from here on. */ 722244469Scognet 723244469Scognet bufzone = busdma_bufalloc_findzone(ba, dmat->maxsize); 724244469Scognet 725244469Scognet if (bufzone != NULL && dmat->alignment <= bufzone->size && 726244469Scognet !_bus_dma_can_bounce(dmat->lowaddr, dmat->highaddr)) 727244469Scognet uma_zfree(bufzone->umazone, vaddr); 728244469Scognet else 729244469Scognet kmem_free(kernel_map, (vm_offset_t)vaddr, dmat->maxsize); 730244469Scognet 731239268Sgonzo dmat->map_count--; 732239268Sgonzo free(map, M_DEVBUF); 733239268Sgonzo CTR3(KTR_BUSDMA, "%s: tag %p flags 0x%x", __func__, dmat, dmat->flags); 734239268Sgonzo} 735239268Sgonzo 736239268Sgonzostatic int 737239268Sgonzo_bus_dmamap_count_pages(bus_dma_tag_t dmat, bus_dmamap_t map, 738239268Sgonzo void *buf, bus_size_t buflen, int flags) 739239268Sgonzo{ 740239268Sgonzo vm_offset_t vaddr; 741239268Sgonzo vm_offset_t vendaddr; 742239268Sgonzo bus_addr_t paddr; 743239268Sgonzo 744239268Sgonzo if (map->pagesneeded == 0) { 745239268Sgonzo CTR5(KTR_BUSDMA, "lowaddr= %d, boundary= %d, alignment= %d" 746239268Sgonzo " map= %p, pagesneeded= %d", 747239268Sgonzo dmat->lowaddr, dmat->boundary, dmat->alignment, 748239268Sgonzo map, map->pagesneeded); 749239268Sgonzo /* 750239268Sgonzo * Count the number of bounce pages 751239268Sgonzo * needed in order to complete this transfer 752239268Sgonzo */ 753239268Sgonzo vaddr = (vm_offset_t)buf; 754239268Sgonzo vendaddr = (vm_offset_t)buf + buflen; 755239268Sgonzo 756239268Sgonzo while (vaddr < vendaddr) { 757239268Sgonzo if (__predict_true(map->pmap == pmap_kernel())) 758239268Sgonzo paddr = pmap_kextract(vaddr); 759239268Sgonzo else 760239268Sgonzo paddr = pmap_extract(map->pmap, vaddr); 761239268Sgonzo if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) && 762239268Sgonzo run_filter(dmat, paddr) != 0) { 763239268Sgonzo map->pagesneeded++; 764239268Sgonzo } 765239268Sgonzo vaddr += (PAGE_SIZE - ((vm_offset_t)vaddr & PAGE_MASK)); 766239268Sgonzo 767239268Sgonzo } 768239268Sgonzo CTR1(KTR_BUSDMA, "pagesneeded= %d", map->pagesneeded); 769239268Sgonzo } 770239268Sgonzo 771239268Sgonzo /* Reserve Necessary Bounce Pages */ 772239268Sgonzo if (map->pagesneeded != 0) { 773239268Sgonzo mtx_lock(&bounce_lock); 774239268Sgonzo if (flags & BUS_DMA_NOWAIT) { 775239268Sgonzo if (reserve_bounce_pages(dmat, map, 0) != 0) { 776239268Sgonzo map->pagesneeded = 0; 777239268Sgonzo mtx_unlock(&bounce_lock); 778239268Sgonzo return (ENOMEM); 779239268Sgonzo } 780239268Sgonzo } else { 781239268Sgonzo if (reserve_bounce_pages(dmat, map, 1) != 0) { 782239268Sgonzo /* Queue us for resources */ 783239268Sgonzo map->dmat = dmat; 784239268Sgonzo map->buf = buf; 785239268Sgonzo map->buflen = buflen; 786239268Sgonzo STAILQ_INSERT_TAIL(&bounce_map_waitinglist, 787239268Sgonzo map, links); 788239268Sgonzo mtx_unlock(&bounce_lock); 789239268Sgonzo return (EINPROGRESS); 790239268Sgonzo } 791239268Sgonzo } 792239268Sgonzo mtx_unlock(&bounce_lock); 793239268Sgonzo } 794239268Sgonzo 795239268Sgonzo return (0); 796239268Sgonzo} 797239268Sgonzo 798239268Sgonzo/* 799239268Sgonzo * Utility function to load a linear buffer. lastaddrp holds state 800239268Sgonzo * between invocations (for multiple-buffer loads). segp contains 801239268Sgonzo * the starting segment on entrace, and the ending segment on exit. 802239268Sgonzo * first indicates if this is the first invocation of this function. 803239268Sgonzo */ 804239268Sgonzostatic __inline int 805239268Sgonzo_bus_dmamap_load_buffer(bus_dma_tag_t dmat, 806239268Sgonzo bus_dmamap_t map, 807239268Sgonzo void *buf, bus_size_t buflen, 808239268Sgonzo int flags, 809239268Sgonzo bus_addr_t *lastaddrp, 810239268Sgonzo bus_dma_segment_t *segs, 811239268Sgonzo int *segp, 812239268Sgonzo int first) 813239268Sgonzo{ 814239268Sgonzo bus_size_t sgsize; 815239268Sgonzo bus_addr_t curaddr, lastaddr, baddr, bmask; 816239268Sgonzo vm_offset_t vaddr; 817239268Sgonzo struct sync_list *sl; 818239268Sgonzo int seg, error; 819239268Sgonzo 820239268Sgonzo if ((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) { 821239268Sgonzo error = _bus_dmamap_count_pages(dmat, map, buf, buflen, flags); 822239268Sgonzo if (error) 823239268Sgonzo return (error); 824239268Sgonzo } 825239268Sgonzo 826239268Sgonzo sl = NULL; 827239268Sgonzo vaddr = (vm_offset_t)buf; 828239268Sgonzo lastaddr = *lastaddrp; 829239268Sgonzo bmask = ~(dmat->boundary - 1); 830239268Sgonzo 831239268Sgonzo for (seg = *segp; buflen > 0 ; ) { 832239268Sgonzo /* 833239268Sgonzo * Get the physical address for this segment. 834239268Sgonzo */ 835239268Sgonzo if (__predict_true(map->pmap == pmap_kernel())) 836239268Sgonzo curaddr = pmap_kextract(vaddr); 837239268Sgonzo else 838239268Sgonzo curaddr = pmap_extract(map->pmap, vaddr); 839239268Sgonzo 840239268Sgonzo /* 841239268Sgonzo * Compute the segment size, and adjust counts. 842239268Sgonzo */ 843239268Sgonzo sgsize = PAGE_SIZE - ((u_long)curaddr & PAGE_MASK); 844239268Sgonzo if (sgsize > dmat->maxsegsz) 845239268Sgonzo sgsize = dmat->maxsegsz; 846239268Sgonzo if (buflen < sgsize) 847239268Sgonzo sgsize = buflen; 848239268Sgonzo 849239268Sgonzo /* 850239268Sgonzo * Make sure we don't cross any boundaries. 851239268Sgonzo */ 852239268Sgonzo if (dmat->boundary > 0) { 853239268Sgonzo baddr = (curaddr + dmat->boundary) & bmask; 854239268Sgonzo if (sgsize > (baddr - curaddr)) 855239268Sgonzo sgsize = (baddr - curaddr); 856239268Sgonzo } 857239268Sgonzo 858239268Sgonzo if (((dmat->flags & BUS_DMA_COULD_BOUNCE) != 0) && 859239268Sgonzo map->pagesneeded != 0 && run_filter(dmat, curaddr)) { 860239268Sgonzo curaddr = add_bounce_page(dmat, map, vaddr, sgsize); 861239268Sgonzo } else { 862239268Sgonzo /* add_sync_list(dmat, map, vaddr, sgsize, cflag); */ 863239268Sgonzo sl = (struct sync_list *)malloc(sizeof(struct sync_list), 864239268Sgonzo M_DEVBUF, M_NOWAIT | M_ZERO); 865239268Sgonzo if (sl == NULL) 866239268Sgonzo goto cleanup; 867239268Sgonzo STAILQ_INSERT_TAIL(&(map->slist), sl, slinks); 868239268Sgonzo sl->vaddr = vaddr; 869239268Sgonzo sl->datacount = sgsize; 870239268Sgonzo sl->busaddr = curaddr; 871239268Sgonzo } 872239268Sgonzo 873239268Sgonzo 874239268Sgonzo if (dmat->ranges) { 875239268Sgonzo struct arm32_dma_range *dr; 876239268Sgonzo 877239268Sgonzo dr = _bus_dma_inrange(dmat->ranges, dmat->_nranges, 878239268Sgonzo curaddr); 879239268Sgonzo if (dr == NULL) { 880239268Sgonzo _bus_dmamap_unload(dmat, map); 881239268Sgonzo return (EINVAL); 882239268Sgonzo } 883239268Sgonzo /* 884239268Sgonzo * In a valid DMA range. Translate the physical 885239268Sgonzo * memory address to an address in the DMA window. 886239268Sgonzo */ 887239268Sgonzo curaddr = (curaddr - dr->dr_sysbase) + dr->dr_busbase; 888239268Sgonzo } 889239268Sgonzo 890239268Sgonzo /* 891239268Sgonzo * Insert chunk into a segment, coalescing with 892239268Sgonzo * previous segment if possible. 893239268Sgonzo */ 894239268Sgonzo if (first) { 895239268Sgonzo segs[seg].ds_addr = curaddr; 896239268Sgonzo segs[seg].ds_len = sgsize; 897239268Sgonzo first = 0; 898239268Sgonzo } else { 899239268Sgonzo if (curaddr == lastaddr && 900239268Sgonzo (segs[seg].ds_len + sgsize) <= dmat->maxsegsz && 901239268Sgonzo (dmat->boundary == 0 || 902239268Sgonzo (segs[seg].ds_addr & bmask) == (curaddr & bmask))) 903239268Sgonzo segs[seg].ds_len += sgsize; 904239268Sgonzo else { 905239268Sgonzo if (++seg >= dmat->nsegments) 906239268Sgonzo break; 907239268Sgonzo segs[seg].ds_addr = curaddr; 908239268Sgonzo segs[seg].ds_len = sgsize; 909239268Sgonzo } 910239268Sgonzo } 911239268Sgonzo 912239268Sgonzo lastaddr = curaddr + sgsize; 913239268Sgonzo vaddr += sgsize; 914239268Sgonzo buflen -= sgsize; 915239268Sgonzo } 916239268Sgonzo 917239268Sgonzo *segp = seg; 918239268Sgonzo *lastaddrp = lastaddr; 919239268Sgonzocleanup: 920239268Sgonzo /* 921239268Sgonzo * Did we fit? 922239268Sgonzo */ 923239268Sgonzo if (buflen != 0) { 924239268Sgonzo _bus_dmamap_unload(dmat, map); 925239268Sgonzo return(EFBIG); /* XXX better return value here? */ 926239268Sgonzo } 927239268Sgonzo return (0); 928239268Sgonzo} 929239268Sgonzo 930239268Sgonzo/* 931239268Sgonzo * Map the buffer buf into bus space using the dmamap map. 932239268Sgonzo */ 933239268Sgonzoint 934239268Sgonzobus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf, 935239268Sgonzo bus_size_t buflen, bus_dmamap_callback_t *callback, 936239268Sgonzo void *callback_arg, int flags) 937239268Sgonzo{ 938239268Sgonzo bus_addr_t lastaddr = 0; 939239268Sgonzo int error, nsegs = 0; 940239268Sgonzo 941239268Sgonzo flags |= BUS_DMA_WAITOK; 942239268Sgonzo map->callback = callback; 943239268Sgonzo map->callback_arg = callback_arg; 944239268Sgonzo map->pmap = kernel_pmap; 945239268Sgonzo 946239268Sgonzo error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, flags, 947239268Sgonzo &lastaddr, dmat->segments, &nsegs, 1); 948239268Sgonzo 949239268Sgonzo CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 950239268Sgonzo __func__, dmat, dmat->flags, error, nsegs + 1); 951239268Sgonzo 952239268Sgonzo if (error == EINPROGRESS) { 953239268Sgonzo return (error); 954239268Sgonzo } 955239268Sgonzo 956239268Sgonzo if (error) 957239268Sgonzo (*callback)(callback_arg, dmat->segments, 0, error); 958239268Sgonzo else 959239268Sgonzo (*callback)(callback_arg, dmat->segments, nsegs + 1, 0); 960239268Sgonzo 961239268Sgonzo /* 962239268Sgonzo * Return ENOMEM to the caller so that it can pass it up the stack. 963239268Sgonzo * This error only happens when NOWAIT is set, so deferal is disabled. 964239268Sgonzo */ 965239268Sgonzo if (error == ENOMEM) 966239268Sgonzo return (error); 967239268Sgonzo 968239268Sgonzo return (0); 969239268Sgonzo} 970239268Sgonzo 971239268Sgonzo 972239268Sgonzo/* 973239268Sgonzo * Like _bus_dmamap_load(), but for mbufs. 974239268Sgonzo */ 975239268Sgonzostatic __inline int 976239268Sgonzo_bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, 977239268Sgonzo struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs, 978239268Sgonzo int flags) 979239268Sgonzo{ 980239268Sgonzo int error; 981239268Sgonzo 982239268Sgonzo M_ASSERTPKTHDR(m0); 983239268Sgonzo map->pmap = kernel_pmap; 984239268Sgonzo 985239268Sgonzo flags |= BUS_DMA_NOWAIT; 986239268Sgonzo *nsegs = 0; 987239268Sgonzo error = 0; 988239268Sgonzo if (m0->m_pkthdr.len <= dmat->maxsize) { 989239268Sgonzo int first = 1; 990239268Sgonzo bus_addr_t lastaddr = 0; 991239268Sgonzo struct mbuf *m; 992239268Sgonzo 993239268Sgonzo for (m = m0; m != NULL && error == 0; m = m->m_next) { 994239268Sgonzo if (m->m_len > 0) { 995239268Sgonzo error = _bus_dmamap_load_buffer(dmat, map, 996239268Sgonzo m->m_data, m->m_len, 997239268Sgonzo flags, &lastaddr, 998239268Sgonzo segs, nsegs, first); 999239268Sgonzo first = 0; 1000239268Sgonzo } 1001239268Sgonzo } 1002239268Sgonzo } else { 1003239268Sgonzo error = EINVAL; 1004239268Sgonzo } 1005239268Sgonzo 1006239268Sgonzo /* XXX FIXME: Having to increment nsegs is really annoying */ 1007239268Sgonzo ++*nsegs; 1008239268Sgonzo CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 1009239268Sgonzo __func__, dmat, dmat->flags, error, *nsegs); 1010239268Sgonzo return (error); 1011239268Sgonzo} 1012239268Sgonzo 1013239268Sgonzoint 1014239268Sgonzobus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, 1015239268Sgonzo struct mbuf *m0, 1016239268Sgonzo bus_dmamap_callback2_t *callback, void *callback_arg, 1017239268Sgonzo int flags) 1018239268Sgonzo{ 1019239268Sgonzo int nsegs, error; 1020239268Sgonzo 1021239268Sgonzo error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, dmat->segments, &nsegs, 1022239268Sgonzo flags); 1023239268Sgonzo 1024239268Sgonzo if (error) { 1025239268Sgonzo /* force "no valid mappings" in callback */ 1026239268Sgonzo (*callback)(callback_arg, dmat->segments, 0, 0, error); 1027239268Sgonzo } else { 1028239268Sgonzo (*callback)(callback_arg, dmat->segments, 1029239268Sgonzo nsegs, m0->m_pkthdr.len, error); 1030239268Sgonzo } 1031239268Sgonzo CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 1032239268Sgonzo __func__, dmat, dmat->flags, error, nsegs); 1033239268Sgonzo 1034239268Sgonzo return (error); 1035239268Sgonzo} 1036239268Sgonzo 1037239268Sgonzoint 1038239268Sgonzobus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, 1039239268Sgonzo struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs, 1040239268Sgonzo int flags) 1041239268Sgonzo{ 1042239268Sgonzo return (_bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags)); 1043239268Sgonzo} 1044239268Sgonzo 1045239268Sgonzo/* 1046239268Sgonzo * Like _bus_dmamap_load(), but for uios. 1047239268Sgonzo */ 1048239268Sgonzoint 1049239268Sgonzobus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, 1050239268Sgonzo struct uio *uio, 1051239268Sgonzo bus_dmamap_callback2_t *callback, void *callback_arg, 1052239268Sgonzo int flags) 1053239268Sgonzo{ 1054239268Sgonzo bus_addr_t lastaddr; 1055239268Sgonzo int nsegs, error, first, i; 1056239268Sgonzo bus_size_t resid; 1057239268Sgonzo struct iovec *iov; 1058239268Sgonzo 1059239268Sgonzo flags |= BUS_DMA_NOWAIT; 1060239268Sgonzo resid = uio->uio_resid; 1061239268Sgonzo iov = uio->uio_iov; 1062239268Sgonzo 1063239268Sgonzo if (uio->uio_segflg == UIO_USERSPACE) { 1064239268Sgonzo KASSERT(uio->uio_td != NULL, 1065239268Sgonzo ("bus_dmamap_load_uio: USERSPACE but no proc")); 1066239268Sgonzo map->pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace); 1067239268Sgonzo } else 1068239268Sgonzo map->pmap = kernel_pmap; 1069239268Sgonzo 1070239268Sgonzo nsegs = 0; 1071239268Sgonzo error = 0; 1072239268Sgonzo first = 1; 1073239268Sgonzo lastaddr = (bus_addr_t) 0; 1074239268Sgonzo for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) { 1075239268Sgonzo /* 1076239268Sgonzo * Now at the first iovec to load. Load each iovec 1077239268Sgonzo * until we have exhausted the residual count. 1078239268Sgonzo */ 1079239268Sgonzo bus_size_t minlen = 1080239268Sgonzo resid < iov[i].iov_len ? resid : iov[i].iov_len; 1081239268Sgonzo caddr_t addr = (caddr_t) iov[i].iov_base; 1082239268Sgonzo 1083239268Sgonzo if (minlen > 0) { 1084239268Sgonzo error = _bus_dmamap_load_buffer(dmat, map, 1085239268Sgonzo addr, minlen, flags, &lastaddr, 1086239268Sgonzo dmat->segments, &nsegs, first); 1087239268Sgonzo first = 0; 1088239268Sgonzo resid -= minlen; 1089239268Sgonzo } 1090239268Sgonzo } 1091239268Sgonzo 1092239268Sgonzo if (error) { 1093239268Sgonzo /* force "no valid mappings" in callback */ 1094239268Sgonzo (*callback)(callback_arg, dmat->segments, 0, 0, error); 1095239268Sgonzo } else { 1096239268Sgonzo (*callback)(callback_arg, dmat->segments, 1097239268Sgonzo nsegs+1, uio->uio_resid, error); 1098239268Sgonzo } 1099239268Sgonzo CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 1100239268Sgonzo __func__, dmat, dmat->flags, error, nsegs + 1); 1101239268Sgonzo return (error); 1102239268Sgonzo} 1103239268Sgonzo 1104239268Sgonzo/* 1105239268Sgonzo * Release the mapping held by map. 1106239268Sgonzo */ 1107239268Sgonzovoid 1108239268Sgonzo_bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map) 1109239268Sgonzo{ 1110239268Sgonzo struct bounce_page *bpage; 1111239268Sgonzo struct bounce_zone *bz; 1112239268Sgonzo struct sync_list *sl; 1113239268Sgonzo 1114239268Sgonzo while ((sl = STAILQ_FIRST(&map->slist)) != NULL) { 1115239268Sgonzo STAILQ_REMOVE_HEAD(&map->slist, slinks); 1116239268Sgonzo free(sl, M_DEVBUF); 1117239268Sgonzo } 1118239268Sgonzo 1119239268Sgonzo if ((bz = dmat->bounce_zone) != NULL) { 1120239268Sgonzo while ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1121239268Sgonzo STAILQ_REMOVE_HEAD(&map->bpages, links); 1122239268Sgonzo free_bounce_page(dmat, bpage); 1123239268Sgonzo } 1124239268Sgonzo 1125239268Sgonzo bz = dmat->bounce_zone; 1126239268Sgonzo bz->free_bpages += map->pagesreserved; 1127239268Sgonzo bz->reserved_bpages -= map->pagesreserved; 1128239268Sgonzo map->pagesreserved = 0; 1129239268Sgonzo map->pagesneeded = 0; 1130239268Sgonzo } 1131239268Sgonzo} 1132239268Sgonzo 1133239268Sgonzo#ifdef notyetbounceuser 1134239268Sgonzo /* If busdma uses user pages, then the interrupt handler could 1135239268Sgonzo * be use the kernel vm mapping. Both bounce pages and sync list 1136239268Sgonzo * do not cross page boundaries. 1137239268Sgonzo * Below is a rough sequence that a person would do to fix the 1138239268Sgonzo * user page reference in the kernel vmspace. This would be 1139239268Sgonzo * done in the dma post routine. 1140239268Sgonzo */ 1141239268Sgonzovoid 1142239268Sgonzo_bus_dmamap_fix_user(vm_offset_t buf, bus_size_t len, 1143239268Sgonzo pmap_t pmap, int op) 1144239268Sgonzo{ 1145239268Sgonzo bus_size_t sgsize; 1146239268Sgonzo bus_addr_t curaddr; 1147239268Sgonzo vm_offset_t va; 1148239268Sgonzo 1149239268Sgonzo /* each synclist entry is contained within a single page. 1150239268Sgonzo * 1151239268Sgonzo * this would be needed if BUS_DMASYNC_POSTxxxx was implemented 1152239268Sgonzo */ 1153239268Sgonzo curaddr = pmap_extract(pmap, buf); 1154239268Sgonzo va = pmap_dma_map(curaddr); 1155239268Sgonzo switch (op) { 1156239268Sgonzo case SYNC_USER_INV: 1157239268Sgonzo cpu_dcache_wb_range(va, sgsize); 1158239268Sgonzo break; 1159239268Sgonzo 1160239268Sgonzo case SYNC_USER_COPYTO: 1161239268Sgonzo bcopy((void *)va, (void *)bounce, sgsize); 1162239268Sgonzo break; 1163239268Sgonzo 1164239268Sgonzo case SYNC_USER_COPYFROM: 1165239268Sgonzo bcopy((void *) bounce, (void *)va, sgsize); 1166239268Sgonzo break; 1167239268Sgonzo 1168239268Sgonzo default: 1169239268Sgonzo break; 1170239268Sgonzo } 1171239268Sgonzo 1172239268Sgonzo pmap_dma_unmap(va); 1173239268Sgonzo} 1174239268Sgonzo#endif 1175239268Sgonzo 1176239268Sgonzo#ifdef ARM_L2_PIPT 1177239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(pa, size) 1178239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(pa, size) 1179239268Sgonzo#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(pa, size) 1180239268Sgonzo#else 1181239268Sgonzo#define l2cache_wb_range(va, pa, size) cpu_l2cache_wb_range(va, size) 1182239268Sgonzo#define l2cache_wbinv_range(va, pa, size) cpu_l2cache_wbinv_range(va, size) 1183243909Scognet#define l2cache_inv_range(va, pa, size) cpu_l2cache_inv_range(va, size) 1184239268Sgonzo#endif 1185239268Sgonzo 1186239268Sgonzovoid 1187239268Sgonzo_bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, bus_dmasync_op_t op) 1188239268Sgonzo{ 1189239268Sgonzo struct bounce_page *bpage; 1190239268Sgonzo struct sync_list *sl; 1191239268Sgonzo bus_size_t len, unalign; 1192239268Sgonzo vm_offset_t buf, ebuf; 1193239268Sgonzo#ifdef FIX_DMAP_BUS_DMASYNC_POSTREAD 1194239268Sgonzo vm_offset_t bbuf; 1195239268Sgonzo char _tmp_cl[arm_dcache_align], _tmp_clend[arm_dcache_align]; 1196239268Sgonzo#endif 1197239268Sgonzo int listcount = 0; 1198239268Sgonzo 1199239268Sgonzo /* if buffer was from user space, it it possible that this 1200239268Sgonzo * is not the same vm map. The fix is to map each page in 1201239268Sgonzo * the buffer into the current address space (KVM) and then 1202239268Sgonzo * do the bounce copy or sync list cache operation. 1203239268Sgonzo * 1204239268Sgonzo * The sync list entries are already broken into 1205239268Sgonzo * their respective physical pages. 1206239268Sgonzo */ 1207239268Sgonzo if (!pmap_dmap_iscurrent(map->pmap)) 1208239268Sgonzo printf("_bus_dmamap_sync: wrong user map: %p %x\n", map->pmap, op); 1209239268Sgonzo 1210239268Sgonzo if ((bpage = STAILQ_FIRST(&map->bpages)) != NULL) { 1211239268Sgonzo 1212239268Sgonzo /* Handle data bouncing. */ 1213239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1214239268Sgonzo "performing bounce", __func__, dmat, dmat->flags, op); 1215239268Sgonzo 1216239268Sgonzo if (op & BUS_DMASYNC_PREWRITE) { 1217239268Sgonzo while (bpage != NULL) { 1218239268Sgonzo bcopy((void *)bpage->datavaddr, 1219239268Sgonzo (void *)bpage->vaddr, 1220239268Sgonzo bpage->datacount); 1221239268Sgonzo cpu_dcache_wb_range((vm_offset_t)bpage->vaddr, 1222239268Sgonzo bpage->datacount); 1223239268Sgonzo l2cache_wb_range((vm_offset_t)bpage->vaddr, 1224239268Sgonzo (vm_offset_t)bpage->busaddr, 1225239268Sgonzo bpage->datacount); 1226239268Sgonzo bpage = STAILQ_NEXT(bpage, links); 1227239268Sgonzo } 1228239268Sgonzo dmat->bounce_zone->total_bounced++; 1229239268Sgonzo } 1230239268Sgonzo 1231239268Sgonzo if (op & BUS_DMASYNC_POSTREAD) { 1232239268Sgonzo if (!pmap_dmap_iscurrent(map->pmap)) 1233239268Sgonzo panic("_bus_dmamap_sync: wrong user map. apply fix"); 1234239268Sgonzo 1235239268Sgonzo cpu_dcache_inv_range((vm_offset_t)bpage->vaddr, 1236239268Sgonzo bpage->datacount); 1237239268Sgonzo l2cache_inv_range((vm_offset_t)bpage->vaddr, 1238239268Sgonzo (vm_offset_t)bpage->busaddr, 1239239268Sgonzo bpage->datacount); 1240239268Sgonzo while (bpage != NULL) { 1241239268Sgonzo vm_offset_t startv; 1242239268Sgonzo vm_paddr_t startp; 1243239268Sgonzo int len; 1244239268Sgonzo 1245239268Sgonzo startv = bpage->vaddr &~ arm_dcache_align_mask; 1246239268Sgonzo startp = bpage->busaddr &~ arm_dcache_align_mask; 1247239268Sgonzo len = bpage->datacount; 1248239268Sgonzo 1249239268Sgonzo if (startv != bpage->vaddr) 1250239268Sgonzo len += bpage->vaddr & arm_dcache_align_mask; 1251239268Sgonzo if (len & arm_dcache_align_mask) 1252239268Sgonzo len = (len - 1253239268Sgonzo (len & arm_dcache_align_mask)) + 1254239268Sgonzo arm_dcache_align; 1255239268Sgonzo cpu_dcache_inv_range(startv, len); 1256239268Sgonzo l2cache_inv_range(startv, startp, len); 1257239268Sgonzo bcopy((void *)bpage->vaddr, 1258239268Sgonzo (void *)bpage->datavaddr, 1259239268Sgonzo bpage->datacount); 1260239268Sgonzo bpage = STAILQ_NEXT(bpage, links); 1261239268Sgonzo } 1262239268Sgonzo dmat->bounce_zone->total_bounced++; 1263239268Sgonzo } 1264239268Sgonzo } 1265244469Scognet if (map->flags & DMAMAP_COHERENT) 1266244469Scognet return; 1267239268Sgonzo 1268239268Sgonzo sl = STAILQ_FIRST(&map->slist); 1269239268Sgonzo while (sl) { 1270239268Sgonzo listcount++; 1271239268Sgonzo sl = STAILQ_NEXT(sl, slinks); 1272239268Sgonzo } 1273239268Sgonzo if ((sl = STAILQ_FIRST(&map->slist)) != NULL) { 1274239268Sgonzo /* ARM caches are not self-snooping for dma */ 1275239268Sgonzo 1276239268Sgonzo CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x op 0x%x " 1277239268Sgonzo "performing sync", __func__, dmat, dmat->flags, op); 1278239268Sgonzo 1279239268Sgonzo switch (op) { 1280239268Sgonzo case BUS_DMASYNC_PREWRITE: 1281239268Sgonzo while (sl != NULL) { 1282239268Sgonzo cpu_dcache_wb_range(sl->vaddr, sl->datacount); 1283239268Sgonzo l2cache_wb_range(sl->vaddr, sl->busaddr, 1284239268Sgonzo sl->datacount); 1285239268Sgonzo sl = STAILQ_NEXT(sl, slinks); 1286239268Sgonzo } 1287239268Sgonzo break; 1288239268Sgonzo 1289239268Sgonzo case BUS_DMASYNC_PREREAD: 1290239268Sgonzo while (sl != NULL) { 1291239268Sgonzo /* write back the unaligned portions */ 1292239268Sgonzo vm_paddr_t physaddr = sl->busaddr, ephysaddr; 1293239268Sgonzo buf = sl->vaddr; 1294239268Sgonzo len = sl->datacount; 1295239268Sgonzo ebuf = buf + len; /* end of buffer */ 1296239268Sgonzo ephysaddr = physaddr + len; 1297239268Sgonzo unalign = buf & arm_dcache_align_mask; 1298239268Sgonzo if (unalign) { 1299239268Sgonzo /* wbinv leading fragment */ 1300239268Sgonzo buf &= ~arm_dcache_align_mask; 1301239268Sgonzo physaddr &= ~arm_dcache_align_mask; 1302239268Sgonzo cpu_dcache_wbinv_range(buf, 1303239268Sgonzo arm_dcache_align); 1304239268Sgonzo l2cache_wbinv_range(buf, physaddr, 1305239268Sgonzo arm_dcache_align); 1306239268Sgonzo buf += arm_dcache_align; 1307239268Sgonzo physaddr += arm_dcache_align; 1308239268Sgonzo /* number byte in buffer wbinv */ 1309239268Sgonzo unalign = arm_dcache_align - unalign; 1310239268Sgonzo if (len > unalign) 1311239268Sgonzo len -= unalign; 1312239268Sgonzo else 1313239268Sgonzo len = 0; 1314239268Sgonzo } 1315239268Sgonzo unalign = ebuf & arm_dcache_align_mask; 1316239268Sgonzo if (ebuf > buf && unalign) { 1317239268Sgonzo /* wbinv trailing fragment */ 1318239268Sgonzo len -= unalign; 1319239268Sgonzo ebuf -= unalign; 1320239268Sgonzo ephysaddr -= unalign; 1321239268Sgonzo cpu_dcache_wbinv_range(ebuf, 1322239268Sgonzo arm_dcache_align); 1323239268Sgonzo l2cache_wbinv_range(ebuf, ephysaddr, 1324239268Sgonzo arm_dcache_align); 1325239268Sgonzo } 1326239268Sgonzo if (ebuf > buf) { 1327239268Sgonzo cpu_dcache_inv_range(buf, len); 1328239268Sgonzo l2cache_inv_range(buf, physaddr, len); 1329239268Sgonzo } 1330239268Sgonzo sl = STAILQ_NEXT(sl, slinks); 1331239268Sgonzo } 1332239268Sgonzo break; 1333239268Sgonzo 1334239268Sgonzo case BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD: 1335239268Sgonzo while (sl != NULL) { 1336239268Sgonzo cpu_dcache_wbinv_range(sl->vaddr, sl->datacount); 1337239268Sgonzo l2cache_wbinv_range(sl->vaddr, 1338239268Sgonzo sl->busaddr, sl->datacount); 1339239268Sgonzo sl = STAILQ_NEXT(sl, slinks); 1340239268Sgonzo } 1341239268Sgonzo break; 1342239268Sgonzo 1343239268Sgonzo#ifdef FIX_DMAP_BUS_DMASYNC_POSTREAD 1344239268Sgonzo case BUS_DMASYNC_POSTREAD: 1345239268Sgonzo if (!pmap_dmap_iscurrent(map->pmap)) 1346239268Sgonzo panic("_bus_dmamap_sync: wrong user map. apply fix"); 1347239268Sgonzo while (sl != NULL) { 1348239268Sgonzo /* write back the unaligned portions */ 1349239268Sgonzo vm_paddr_t physaddr; 1350239268Sgonzo buf = sl->vaddr; 1351239268Sgonzo len = sl->datacount; 1352239268Sgonzo physaddr = sl->busaddr; 1353239268Sgonzo bbuf = buf & ~arm_dcache_align_mask; 1354239268Sgonzo ebuf = buf + len; 1355239268Sgonzo physaddr = physaddr & ~arm_dcache_align_mask; 1356239268Sgonzo unalign = buf & arm_dcache_align_mask; 1357239268Sgonzo if (unalign) { 1358239268Sgonzo memcpy(_tmp_cl, (void *)bbuf, unalign); 1359239268Sgonzo len += unalign; /* inv entire cache line */ 1360239268Sgonzo } 1361239268Sgonzo unalign = ebuf & arm_dcache_align_mask; 1362239268Sgonzo if (unalign) { 1363239268Sgonzo unalign = arm_dcache_align - unalign; 1364239268Sgonzo memcpy(_tmp_clend, (void *)ebuf, unalign); 1365239268Sgonzo len += unalign; /* inv entire cache line */ 1366239268Sgonzo } 1367239268Sgonzo /* inv are cache length aligned */ 1368239268Sgonzo cpu_dcache_inv_range(bbuf, len); 1369239268Sgonzo l2cache_inv_range(bbuf, physaddr, len); 1370239268Sgonzo 1371239268Sgonzo unalign = (vm_offset_t)buf & arm_dcache_align_mask; 1372239268Sgonzo if (unalign) { 1373239268Sgonzo memcpy((void *)bbuf, _tmp_cl, unalign); 1374239268Sgonzo } 1375239268Sgonzo unalign = ebuf & arm_dcache_align_mask; 1376239268Sgonzo if (unalign) { 1377239268Sgonzo unalign = arm_dcache_align - unalign; 1378239268Sgonzo memcpy((void *)ebuf, _tmp_clend, unalign); 1379239268Sgonzo } 1380239268Sgonzo sl = STAILQ_NEXT(sl, slinks); 1381239268Sgonzo } 1382239268Sgonzo break; 1383239268Sgonzo#endif /* FIX_DMAP_BUS_DMASYNC_POSTREAD */ 1384239268Sgonzo 1385239268Sgonzo default: 1386239268Sgonzo break; 1387239268Sgonzo } 1388239268Sgonzo } 1389239268Sgonzo} 1390239268Sgonzo 1391239268Sgonzostatic void 1392239268Sgonzoinit_bounce_pages(void *dummy __unused) 1393239268Sgonzo{ 1394239268Sgonzo 1395239268Sgonzo total_bpages = 0; 1396239268Sgonzo STAILQ_INIT(&bounce_zone_list); 1397239268Sgonzo STAILQ_INIT(&bounce_map_waitinglist); 1398239268Sgonzo STAILQ_INIT(&bounce_map_callbacklist); 1399239268Sgonzo mtx_init(&bounce_lock, "bounce pages lock", NULL, MTX_DEF); 1400239268Sgonzo} 1401239268SgonzoSYSINIT(bpages, SI_SUB_LOCK, SI_ORDER_ANY, init_bounce_pages, NULL); 1402239268Sgonzo 1403239268Sgonzostatic struct sysctl_ctx_list * 1404239268Sgonzobusdma_sysctl_tree(struct bounce_zone *bz) 1405239268Sgonzo{ 1406239268Sgonzo return (&bz->sysctl_tree); 1407239268Sgonzo} 1408239268Sgonzo 1409239268Sgonzostatic struct sysctl_oid * 1410239268Sgonzobusdma_sysctl_tree_top(struct bounce_zone *bz) 1411239268Sgonzo{ 1412239268Sgonzo return (bz->sysctl_tree_top); 1413239268Sgonzo} 1414239268Sgonzo 1415239268Sgonzostatic int 1416239268Sgonzoalloc_bounce_zone(bus_dma_tag_t dmat) 1417239268Sgonzo{ 1418239268Sgonzo struct bounce_zone *bz; 1419239268Sgonzo 1420239268Sgonzo /* Check to see if we already have a suitable zone */ 1421239268Sgonzo STAILQ_FOREACH(bz, &bounce_zone_list, links) { 1422239268Sgonzo if ((dmat->alignment <= bz->alignment) 1423239268Sgonzo && (dmat->lowaddr >= bz->lowaddr)) { 1424239268Sgonzo dmat->bounce_zone = bz; 1425239268Sgonzo return (0); 1426239268Sgonzo } 1427239268Sgonzo } 1428239268Sgonzo 1429239268Sgonzo if ((bz = (struct bounce_zone *)malloc(sizeof(*bz), M_DEVBUF, 1430239268Sgonzo M_NOWAIT | M_ZERO)) == NULL) 1431239268Sgonzo return (ENOMEM); 1432239268Sgonzo 1433239268Sgonzo STAILQ_INIT(&bz->bounce_page_list); 1434239268Sgonzo bz->free_bpages = 0; 1435239268Sgonzo bz->reserved_bpages = 0; 1436239268Sgonzo bz->active_bpages = 0; 1437239268Sgonzo bz->lowaddr = dmat->lowaddr; 1438239268Sgonzo bz->alignment = MAX(dmat->alignment, PAGE_SIZE); 1439239268Sgonzo bz->map_count = 0; 1440239268Sgonzo snprintf(bz->zoneid, 8, "zone%d", busdma_zonecount); 1441239268Sgonzo busdma_zonecount++; 1442239268Sgonzo snprintf(bz->lowaddrid, 18, "%#jx", (uintmax_t)bz->lowaddr); 1443239268Sgonzo STAILQ_INSERT_TAIL(&bounce_zone_list, bz, links); 1444239268Sgonzo dmat->bounce_zone = bz; 1445239268Sgonzo 1446239268Sgonzo sysctl_ctx_init(&bz->sysctl_tree); 1447239268Sgonzo bz->sysctl_tree_top = SYSCTL_ADD_NODE(&bz->sysctl_tree, 1448239268Sgonzo SYSCTL_STATIC_CHILDREN(_hw_busdma), OID_AUTO, bz->zoneid, 1449239268Sgonzo CTLFLAG_RD, 0, ""); 1450239268Sgonzo if (bz->sysctl_tree_top == NULL) { 1451239268Sgonzo sysctl_ctx_free(&bz->sysctl_tree); 1452239268Sgonzo return (0); /* XXX error code? */ 1453239268Sgonzo } 1454239268Sgonzo 1455239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1456239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1457239268Sgonzo "total_bpages", CTLFLAG_RD, &bz->total_bpages, 0, 1458239268Sgonzo "Total bounce pages"); 1459239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1460239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1461239268Sgonzo "free_bpages", CTLFLAG_RD, &bz->free_bpages, 0, 1462239268Sgonzo "Free bounce pages"); 1463239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1464239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1465239268Sgonzo "reserved_bpages", CTLFLAG_RD, &bz->reserved_bpages, 0, 1466239268Sgonzo "Reserved bounce pages"); 1467239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1468239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1469239268Sgonzo "active_bpages", CTLFLAG_RD, &bz->active_bpages, 0, 1470239268Sgonzo "Active bounce pages"); 1471239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1472239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1473239268Sgonzo "total_bounced", CTLFLAG_RD, &bz->total_bounced, 0, 1474239268Sgonzo "Total bounce requests"); 1475239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1476239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1477239268Sgonzo "total_deferred", CTLFLAG_RD, &bz->total_deferred, 0, 1478239268Sgonzo "Total bounce requests that were deferred"); 1479239268Sgonzo SYSCTL_ADD_STRING(busdma_sysctl_tree(bz), 1480239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1481239268Sgonzo "lowaddr", CTLFLAG_RD, bz->lowaddrid, 0, ""); 1482239268Sgonzo SYSCTL_ADD_INT(busdma_sysctl_tree(bz), 1483239268Sgonzo SYSCTL_CHILDREN(busdma_sysctl_tree_top(bz)), OID_AUTO, 1484239268Sgonzo "alignment", CTLFLAG_RD, &bz->alignment, 0, ""); 1485239268Sgonzo 1486239268Sgonzo return (0); 1487239268Sgonzo} 1488239268Sgonzo 1489239268Sgonzostatic int 1490239268Sgonzoalloc_bounce_pages(bus_dma_tag_t dmat, u_int numpages) 1491239268Sgonzo{ 1492239268Sgonzo struct bounce_zone *bz; 1493239268Sgonzo int count; 1494239268Sgonzo 1495239268Sgonzo bz = dmat->bounce_zone; 1496239268Sgonzo count = 0; 1497239268Sgonzo while (numpages > 0) { 1498239268Sgonzo struct bounce_page *bpage; 1499239268Sgonzo 1500239268Sgonzo bpage = (struct bounce_page *)malloc(sizeof(*bpage), M_DEVBUF, 1501239268Sgonzo M_NOWAIT | M_ZERO); 1502239268Sgonzo 1503239268Sgonzo if (bpage == NULL) 1504239268Sgonzo break; 1505239268Sgonzo bpage->vaddr = (vm_offset_t)contigmalloc(PAGE_SIZE, M_DEVBUF, 1506239268Sgonzo M_NOWAIT, 0ul, 1507239268Sgonzo bz->lowaddr, 1508239268Sgonzo PAGE_SIZE, 1509239268Sgonzo 0); 1510239268Sgonzo if (bpage->vaddr == 0) { 1511239268Sgonzo free(bpage, M_DEVBUF); 1512239268Sgonzo break; 1513239268Sgonzo } 1514239268Sgonzo bpage->busaddr = pmap_kextract(bpage->vaddr); 1515239268Sgonzo mtx_lock(&bounce_lock); 1516239268Sgonzo STAILQ_INSERT_TAIL(&bz->bounce_page_list, bpage, links); 1517239268Sgonzo total_bpages++; 1518239268Sgonzo bz->total_bpages++; 1519239268Sgonzo bz->free_bpages++; 1520239268Sgonzo mtx_unlock(&bounce_lock); 1521239268Sgonzo count++; 1522239268Sgonzo numpages--; 1523239268Sgonzo } 1524239268Sgonzo return (count); 1525239268Sgonzo} 1526239268Sgonzo 1527239268Sgonzostatic int 1528239268Sgonzoreserve_bounce_pages(bus_dma_tag_t dmat, bus_dmamap_t map, int commit) 1529239268Sgonzo{ 1530239268Sgonzo struct bounce_zone *bz; 1531239268Sgonzo int pages; 1532239268Sgonzo 1533239268Sgonzo mtx_assert(&bounce_lock, MA_OWNED); 1534239268Sgonzo bz = dmat->bounce_zone; 1535239268Sgonzo pages = MIN(bz->free_bpages, map->pagesneeded - map->pagesreserved); 1536239268Sgonzo if (commit == 0 && map->pagesneeded > (map->pagesreserved + pages)) 1537239268Sgonzo return (map->pagesneeded - (map->pagesreserved + pages)); 1538239268Sgonzo bz->free_bpages -= pages; 1539239268Sgonzo bz->reserved_bpages += pages; 1540239268Sgonzo map->pagesreserved += pages; 1541239268Sgonzo pages = map->pagesneeded - map->pagesreserved; 1542239268Sgonzo 1543239268Sgonzo return (pages); 1544239268Sgonzo} 1545239268Sgonzo 1546239268Sgonzostatic bus_addr_t 1547239268Sgonzoadd_bounce_page(bus_dma_tag_t dmat, bus_dmamap_t map, vm_offset_t vaddr, 1548239268Sgonzo bus_size_t size) 1549239268Sgonzo{ 1550239268Sgonzo struct bounce_zone *bz; 1551239268Sgonzo struct bounce_page *bpage; 1552239268Sgonzo 1553239268Sgonzo KASSERT(dmat->bounce_zone != NULL, ("no bounce zone in dma tag")); 1554239268Sgonzo KASSERT(map != NULL, 1555239268Sgonzo ("add_bounce_page: bad map %p", map)); 1556239268Sgonzo 1557239268Sgonzo bz = dmat->bounce_zone; 1558239268Sgonzo if (map->pagesneeded == 0) 1559239268Sgonzo panic("add_bounce_page: map doesn't need any pages"); 1560239268Sgonzo map->pagesneeded--; 1561239268Sgonzo 1562239268Sgonzo if (map->pagesreserved == 0) 1563239268Sgonzo panic("add_bounce_page: map doesn't need any pages"); 1564239268Sgonzo map->pagesreserved--; 1565239268Sgonzo 1566239268Sgonzo mtx_lock(&bounce_lock); 1567239268Sgonzo bpage = STAILQ_FIRST(&bz->bounce_page_list); 1568239268Sgonzo if (bpage == NULL) 1569239268Sgonzo panic("add_bounce_page: free page list is empty"); 1570239268Sgonzo 1571239268Sgonzo STAILQ_REMOVE_HEAD(&bz->bounce_page_list, links); 1572239268Sgonzo bz->reserved_bpages--; 1573239268Sgonzo bz->active_bpages++; 1574239268Sgonzo mtx_unlock(&bounce_lock); 1575239268Sgonzo 1576239268Sgonzo if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1577239268Sgonzo /* Page offset needs to be preserved. */ 1578239268Sgonzo bpage->vaddr |= vaddr & PAGE_MASK; 1579239268Sgonzo bpage->busaddr |= vaddr & PAGE_MASK; 1580239268Sgonzo } 1581239268Sgonzo bpage->datavaddr = vaddr; 1582239268Sgonzo bpage->datacount = size; 1583239268Sgonzo STAILQ_INSERT_TAIL(&(map->bpages), bpage, links); 1584239268Sgonzo return (bpage->busaddr); 1585239268Sgonzo} 1586239268Sgonzo 1587239268Sgonzostatic void 1588239268Sgonzofree_bounce_page(bus_dma_tag_t dmat, struct bounce_page *bpage) 1589239268Sgonzo{ 1590239268Sgonzo struct bus_dmamap *map; 1591239268Sgonzo struct bounce_zone *bz; 1592239268Sgonzo 1593239268Sgonzo bz = dmat->bounce_zone; 1594239268Sgonzo bpage->datavaddr = 0; 1595239268Sgonzo bpage->datacount = 0; 1596239268Sgonzo if (dmat->flags & BUS_DMA_KEEP_PG_OFFSET) { 1597239268Sgonzo /* 1598239268Sgonzo * Reset the bounce page to start at offset 0. Other uses 1599239268Sgonzo * of this bounce page may need to store a full page of 1600239268Sgonzo * data and/or assume it starts on a page boundary. 1601239268Sgonzo */ 1602239268Sgonzo bpage->vaddr &= ~PAGE_MASK; 1603239268Sgonzo bpage->busaddr &= ~PAGE_MASK; 1604239268Sgonzo } 1605239268Sgonzo 1606239268Sgonzo mtx_lock(&bounce_lock); 1607239268Sgonzo STAILQ_INSERT_HEAD(&bz->bounce_page_list, bpage, links); 1608239268Sgonzo bz->free_bpages++; 1609239268Sgonzo bz->active_bpages--; 1610239268Sgonzo if ((map = STAILQ_FIRST(&bounce_map_waitinglist)) != NULL) { 1611239268Sgonzo if (reserve_bounce_pages(map->dmat, map, 1) == 0) { 1612239268Sgonzo STAILQ_REMOVE_HEAD(&bounce_map_waitinglist, links); 1613239268Sgonzo STAILQ_INSERT_TAIL(&bounce_map_callbacklist, 1614239268Sgonzo map, links); 1615239268Sgonzo busdma_swi_pending = 1; 1616239268Sgonzo bz->total_deferred++; 1617239268Sgonzo swi_sched(vm_ih, 0); 1618239268Sgonzo } 1619239268Sgonzo } 1620239268Sgonzo mtx_unlock(&bounce_lock); 1621239268Sgonzo} 1622239268Sgonzo 1623239268Sgonzovoid 1624239268Sgonzobusdma_swi(void) 1625239268Sgonzo{ 1626239268Sgonzo bus_dma_tag_t dmat; 1627239268Sgonzo struct bus_dmamap *map; 1628239268Sgonzo 1629239268Sgonzo mtx_lock(&bounce_lock); 1630239268Sgonzo while ((map = STAILQ_FIRST(&bounce_map_callbacklist)) != NULL) { 1631239268Sgonzo STAILQ_REMOVE_HEAD(&bounce_map_callbacklist, links); 1632239268Sgonzo mtx_unlock(&bounce_lock); 1633239268Sgonzo dmat = map->dmat; 1634239268Sgonzo (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_LOCK); 1635239268Sgonzo bus_dmamap_load(map->dmat, map, map->buf, map->buflen, 1636239268Sgonzo map->callback, map->callback_arg, /*flags*/0); 1637239268Sgonzo (dmat->lockfunc)(dmat->lockfuncarg, BUS_DMA_UNLOCK); 1638239268Sgonzo mtx_lock(&bounce_lock); 1639239268Sgonzo } 1640239268Sgonzo mtx_unlock(&bounce_lock); 1641239268Sgonzo} 1642