162306a36Sopenharmony_ci============================================ 262306a36Sopenharmony_ciDynamic DMA mapping using the generic device 362306a36Sopenharmony_ci============================================ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci:Author: James E.J. Bottomley <James.Bottomley@HansenPartnership.com> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciThis document describes the DMA API. For a more gentle introduction 862306a36Sopenharmony_ciof the API (and actual examples), see Documentation/core-api/dma-api-howto.rst. 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ciThis API is split into two pieces. Part I describes the basic API. 1162306a36Sopenharmony_ciPart II describes extensions for supporting non-consistent memory 1262306a36Sopenharmony_cimachines. Unless you know that your driver absolutely has to support 1362306a36Sopenharmony_cinon-consistent platforms (this is usually only legacy platforms) you 1462306a36Sopenharmony_cishould only use the API described in part I. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciPart I - dma_API 1762306a36Sopenharmony_ci---------------- 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciTo get the dma_API, you must #include <linux/dma-mapping.h>. This 2062306a36Sopenharmony_ciprovides dma_addr_t and the interfaces described below. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciA dma_addr_t can hold any valid DMA address for the platform. It can be 2362306a36Sopenharmony_cigiven to a device to use as a DMA source or target. A CPU cannot reference 2462306a36Sopenharmony_cia dma_addr_t directly because there may be translation between its physical 2562306a36Sopenharmony_ciaddress space and the DMA address space. 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciPart Ia - Using large DMA-coherent buffers 2862306a36Sopenharmony_ci------------------------------------------ 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci:: 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci void * 3362306a36Sopenharmony_ci dma_alloc_coherent(struct device *dev, size_t size, 3462306a36Sopenharmony_ci dma_addr_t *dma_handle, gfp_t flag) 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ciConsistent memory is memory for which a write by either the device or 3762306a36Sopenharmony_cithe processor can immediately be read by the processor or device 3862306a36Sopenharmony_ciwithout having to worry about caching effects. (You may however need 3962306a36Sopenharmony_cito make sure to flush the processor's write buffers before telling 4062306a36Sopenharmony_cidevices to read that memory.) 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ciThis routine allocates a region of <size> bytes of consistent memory. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciIt returns a pointer to the allocated region (in the processor's virtual 4562306a36Sopenharmony_ciaddress space) or NULL if the allocation failed. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciIt also returns a <dma_handle> which may be cast to an unsigned integer the 4862306a36Sopenharmony_cisame width as the bus and given to the device as the DMA address base of 4962306a36Sopenharmony_cithe region. 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciNote: consistent memory can be expensive on some platforms, and the 5262306a36Sopenharmony_ciminimum allocation length may be as big as a page, so you should 5362306a36Sopenharmony_ciconsolidate your requests for consistent memory as much as possible. 5462306a36Sopenharmony_ciThe simplest way to do that is to use the dma_pool calls (see below). 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciThe flag parameter (dma_alloc_coherent() only) allows the caller to 5762306a36Sopenharmony_cispecify the ``GFP_`` flags (see kmalloc()) for the allocation (the 5862306a36Sopenharmony_ciimplementation may choose to ignore flags that affect the location of 5962306a36Sopenharmony_cithe returned memory, like GFP_DMA). 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci:: 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci void 6462306a36Sopenharmony_ci dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, 6562306a36Sopenharmony_ci dma_addr_t dma_handle) 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciFree a region of consistent memory you previously allocated. dev, 6862306a36Sopenharmony_cisize and dma_handle must all be the same as those passed into 6962306a36Sopenharmony_cidma_alloc_coherent(). cpu_addr must be the virtual address returned by 7062306a36Sopenharmony_cithe dma_alloc_coherent(). 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciNote that unlike their sibling allocation calls, these routines 7362306a36Sopenharmony_cimay only be called with IRQs enabled. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ciPart Ib - Using small DMA-coherent buffers 7762306a36Sopenharmony_ci------------------------------------------ 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ciTo get this part of the dma_API, you must #include <linux/dmapool.h> 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciMany drivers need lots of small DMA-coherent memory regions for DMA 8262306a36Sopenharmony_cidescriptors or I/O buffers. Rather than allocating in units of a page 8362306a36Sopenharmony_cior more using dma_alloc_coherent(), you can use DMA pools. These work 8462306a36Sopenharmony_cimuch like a struct kmem_cache, except that they use the DMA-coherent allocator, 8562306a36Sopenharmony_cinot __get_free_pages(). Also, they understand common hardware constraints 8662306a36Sopenharmony_cifor alignment, like queue heads needing to be aligned on N-byte boundaries. 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci:: 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci struct dma_pool * 9262306a36Sopenharmony_ci dma_pool_create(const char *name, struct device *dev, 9362306a36Sopenharmony_ci size_t size, size_t align, size_t alloc); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_cidma_pool_create() initializes a pool of DMA-coherent buffers 9662306a36Sopenharmony_cifor use with a given device. It must be called in a context which 9762306a36Sopenharmony_cican sleep. 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciThe "name" is for diagnostics (like a struct kmem_cache name); dev and size 10062306a36Sopenharmony_ciare like what you'd pass to dma_alloc_coherent(). The device's hardware 10162306a36Sopenharmony_cialignment requirement for this type of data is "align" (which is expressed 10262306a36Sopenharmony_ciin bytes, and must be a power of two). If your device has no boundary 10362306a36Sopenharmony_cicrossing restrictions, pass 0 for alloc; passing 4096 says memory allocated 10462306a36Sopenharmony_cifrom this pool must not cross 4KByte boundaries. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci:: 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci void * 10962306a36Sopenharmony_ci dma_pool_zalloc(struct dma_pool *pool, gfp_t mem_flags, 11062306a36Sopenharmony_ci dma_addr_t *handle) 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ciWraps dma_pool_alloc() and also zeroes the returned memory if the 11362306a36Sopenharmony_ciallocation attempt succeeded. 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci:: 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci void * 11962306a36Sopenharmony_ci dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags, 12062306a36Sopenharmony_ci dma_addr_t *dma_handle); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciThis allocates memory from the pool; the returned memory will meet the 12362306a36Sopenharmony_cisize and alignment requirements specified at creation time. Pass 12462306a36Sopenharmony_ciGFP_ATOMIC to prevent blocking, or if it's permitted (not 12562306a36Sopenharmony_ciin_interrupt, not holding SMP locks), pass GFP_KERNEL to allow 12662306a36Sopenharmony_ciblocking. Like dma_alloc_coherent(), this returns two values: an 12762306a36Sopenharmony_ciaddress usable by the CPU, and the DMA address usable by the pool's 12862306a36Sopenharmony_cidevice. 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci:: 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci void 13362306a36Sopenharmony_ci dma_pool_free(struct dma_pool *pool, void *vaddr, 13462306a36Sopenharmony_ci dma_addr_t addr); 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciThis puts memory back into the pool. The pool is what was passed to 13762306a36Sopenharmony_cidma_pool_alloc(); the CPU (vaddr) and DMA addresses are what 13862306a36Sopenharmony_ciwere returned when that routine allocated the memory being freed. 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci:: 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci void 14362306a36Sopenharmony_ci dma_pool_destroy(struct dma_pool *pool); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_cidma_pool_destroy() frees the resources of the pool. It must be 14662306a36Sopenharmony_cicalled in a context which can sleep. Make sure you've freed all allocated 14762306a36Sopenharmony_cimemory back to the pool before you destroy it. 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ciPart Ic - DMA addressing limitations 15162306a36Sopenharmony_ci------------------------------------ 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci:: 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci int 15662306a36Sopenharmony_ci dma_set_mask_and_coherent(struct device *dev, u64 mask) 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ciChecks to see if the mask is possible and updates the device 15962306a36Sopenharmony_cistreaming and coherent DMA mask parameters if it is. 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ciReturns: 0 if successful and a negative error if not. 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci:: 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci int 16662306a36Sopenharmony_ci dma_set_mask(struct device *dev, u64 mask) 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ciChecks to see if the mask is possible and updates the device 16962306a36Sopenharmony_ciparameters if it is. 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ciReturns: 0 if successful and a negative error if not. 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci:: 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci int 17662306a36Sopenharmony_ci dma_set_coherent_mask(struct device *dev, u64 mask) 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ciChecks to see if the mask is possible and updates the device 17962306a36Sopenharmony_ciparameters if it is. 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ciReturns: 0 if successful and a negative error if not. 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci:: 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci u64 18662306a36Sopenharmony_ci dma_get_required_mask(struct device *dev) 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ciThis API returns the mask that the platform requires to 18962306a36Sopenharmony_cioperate efficiently. Usually this means the returned mask 19062306a36Sopenharmony_ciis the minimum required to cover all of memory. Examining the 19162306a36Sopenharmony_cirequired mask gives drivers with variable descriptor sizes the 19262306a36Sopenharmony_ciopportunity to use smaller descriptors as necessary. 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ciRequesting the required mask does not alter the current mask. If you 19562306a36Sopenharmony_ciwish to take advantage of it, you should issue a dma_set_mask() 19662306a36Sopenharmony_cicall to set the mask to the value returned. 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci:: 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci size_t 20162306a36Sopenharmony_ci dma_max_mapping_size(struct device *dev); 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ciReturns the maximum size of a mapping for the device. The size parameter 20462306a36Sopenharmony_ciof the mapping functions like dma_map_single(), dma_map_page() and 20562306a36Sopenharmony_ciothers should not be larger than the returned value. 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci:: 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci size_t 21062306a36Sopenharmony_ci dma_opt_mapping_size(struct device *dev); 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ciReturns the maximum optimal size of a mapping for the device. 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ciMapping larger buffers may take much longer in certain scenarios. In 21562306a36Sopenharmony_ciaddition, for high-rate short-lived streaming mappings, the upfront time 21662306a36Sopenharmony_cispent on the mapping may account for an appreciable part of the total 21762306a36Sopenharmony_cirequest lifetime. As such, if splitting larger requests incurs no 21862306a36Sopenharmony_cisignificant performance penalty, then device drivers are advised to 21962306a36Sopenharmony_cilimit total DMA streaming mappings length to the returned value. 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci:: 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci bool 22462306a36Sopenharmony_ci dma_need_sync(struct device *dev, dma_addr_t dma_addr); 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ciReturns %true if dma_sync_single_for_{device,cpu} calls are required to 22762306a36Sopenharmony_citransfer memory ownership. Returns %false if those calls can be skipped. 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci:: 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci unsigned long 23262306a36Sopenharmony_ci dma_get_merge_boundary(struct device *dev); 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ciReturns the DMA merge boundary. If the device cannot merge any the DMA address 23562306a36Sopenharmony_cisegments, the function returns 0. 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ciPart Id - Streaming DMA mappings 23862306a36Sopenharmony_ci-------------------------------- 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci:: 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci dma_addr_t 24362306a36Sopenharmony_ci dma_map_single(struct device *dev, void *cpu_addr, size_t size, 24462306a36Sopenharmony_ci enum dma_data_direction direction) 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ciMaps a piece of processor virtual memory so it can be accessed by the 24762306a36Sopenharmony_cidevice and returns the DMA address of the memory. 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciThe direction for both APIs may be converted freely by casting. 25062306a36Sopenharmony_ciHowever the dma_API uses a strongly typed enumerator for its 25162306a36Sopenharmony_cidirection: 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_ci======================= ============================================= 25462306a36Sopenharmony_ciDMA_NONE no direction (used for debugging) 25562306a36Sopenharmony_ciDMA_TO_DEVICE data is going from the memory to the device 25662306a36Sopenharmony_ciDMA_FROM_DEVICE data is coming from the device to the memory 25762306a36Sopenharmony_ciDMA_BIDIRECTIONAL direction isn't known 25862306a36Sopenharmony_ci======================= ============================================= 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci.. note:: 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci Not all memory regions in a machine can be mapped by this API. 26362306a36Sopenharmony_ci Further, contiguous kernel virtual space may not be contiguous as 26462306a36Sopenharmony_ci physical memory. Since this API does not provide any scatter/gather 26562306a36Sopenharmony_ci capability, it will fail if the user tries to map a non-physically 26662306a36Sopenharmony_ci contiguous piece of memory. For this reason, memory to be mapped by 26762306a36Sopenharmony_ci this API should be obtained from sources which guarantee it to be 26862306a36Sopenharmony_ci physically contiguous (like kmalloc). 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci Further, the DMA address of the memory must be within the 27162306a36Sopenharmony_ci dma_mask of the device (the dma_mask is a bit mask of the 27262306a36Sopenharmony_ci addressable region for the device, i.e., if the DMA address of 27362306a36Sopenharmony_ci the memory ANDed with the dma_mask is still equal to the DMA 27462306a36Sopenharmony_ci address, then the device can perform DMA to the memory). To 27562306a36Sopenharmony_ci ensure that the memory allocated by kmalloc is within the dma_mask, 27662306a36Sopenharmony_ci the driver may specify various platform-dependent flags to restrict 27762306a36Sopenharmony_ci the DMA address range of the allocation (e.g., on x86, GFP_DMA 27862306a36Sopenharmony_ci guarantees to be within the first 16MB of available DMA addresses, 27962306a36Sopenharmony_ci as required by ISA devices). 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci Note also that the above constraints on physical contiguity and 28262306a36Sopenharmony_ci dma_mask may not apply if the platform has an IOMMU (a device which 28362306a36Sopenharmony_ci maps an I/O DMA address to a physical memory address). However, to be 28462306a36Sopenharmony_ci portable, device driver writers may *not* assume that such an IOMMU 28562306a36Sopenharmony_ci exists. 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ci.. warning:: 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ci Memory coherency operates at a granularity called the cache 29062306a36Sopenharmony_ci line width. In order for memory mapped by this API to operate 29162306a36Sopenharmony_ci correctly, the mapped region must begin exactly on a cache line 29262306a36Sopenharmony_ci boundary and end exactly on one (to prevent two separately mapped 29362306a36Sopenharmony_ci regions from sharing a single cache line). Since the cache line size 29462306a36Sopenharmony_ci may not be known at compile time, the API will not enforce this 29562306a36Sopenharmony_ci requirement. Therefore, it is recommended that driver writers who 29662306a36Sopenharmony_ci don't take special care to determine the cache line size at run time 29762306a36Sopenharmony_ci only map virtual regions that begin and end on page boundaries (which 29862306a36Sopenharmony_ci are guaranteed also to be cache line boundaries). 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci DMA_TO_DEVICE synchronisation must be done after the last modification 30162306a36Sopenharmony_ci of the memory region by the software and before it is handed off to 30262306a36Sopenharmony_ci the device. Once this primitive is used, memory covered by this 30362306a36Sopenharmony_ci primitive should be treated as read-only by the device. If the device 30462306a36Sopenharmony_ci may write to it at any point, it should be DMA_BIDIRECTIONAL (see 30562306a36Sopenharmony_ci below). 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci DMA_FROM_DEVICE synchronisation must be done before the driver 30862306a36Sopenharmony_ci accesses data that may be changed by the device. This memory should 30962306a36Sopenharmony_ci be treated as read-only by the driver. If the driver needs to write 31062306a36Sopenharmony_ci to it at any point, it should be DMA_BIDIRECTIONAL (see below). 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci DMA_BIDIRECTIONAL requires special handling: it means that the driver 31362306a36Sopenharmony_ci isn't sure if the memory was modified before being handed off to the 31462306a36Sopenharmony_ci device and also isn't sure if the device will also modify it. Thus, 31562306a36Sopenharmony_ci you must always sync bidirectional memory twice: once before the 31662306a36Sopenharmony_ci memory is handed off to the device (to make sure all memory changes 31762306a36Sopenharmony_ci are flushed from the processor) and once before the data may be 31862306a36Sopenharmony_ci accessed after being used by the device (to make sure any processor 31962306a36Sopenharmony_ci cache lines are updated with data that the device may have changed). 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci:: 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci void 32462306a36Sopenharmony_ci dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, 32562306a36Sopenharmony_ci enum dma_data_direction direction) 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ciUnmaps the region previously mapped. All the parameters passed in 32862306a36Sopenharmony_cimust be identical to those passed in (and returned) by the mapping 32962306a36Sopenharmony_ciAPI. 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci:: 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci dma_addr_t 33462306a36Sopenharmony_ci dma_map_page(struct device *dev, struct page *page, 33562306a36Sopenharmony_ci unsigned long offset, size_t size, 33662306a36Sopenharmony_ci enum dma_data_direction direction) 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci void 33962306a36Sopenharmony_ci dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size, 34062306a36Sopenharmony_ci enum dma_data_direction direction) 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ciAPI for mapping and unmapping for pages. All the notes and warnings 34362306a36Sopenharmony_cifor the other mapping APIs apply here. Also, although the <offset> 34462306a36Sopenharmony_ciand <size> parameters are provided to do partial page mapping, it is 34562306a36Sopenharmony_cirecommended that you never use these unless you really know what the 34662306a36Sopenharmony_cicache width is. 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci:: 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci dma_addr_t 35162306a36Sopenharmony_ci dma_map_resource(struct device *dev, phys_addr_t phys_addr, size_t size, 35262306a36Sopenharmony_ci enum dma_data_direction dir, unsigned long attrs) 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci void 35562306a36Sopenharmony_ci dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size, 35662306a36Sopenharmony_ci enum dma_data_direction dir, unsigned long attrs) 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ciAPI for mapping and unmapping for MMIO resources. All the notes and 35962306a36Sopenharmony_ciwarnings for the other mapping APIs apply here. The API should only be 36062306a36Sopenharmony_ciused to map device MMIO resources, mapping of RAM is not permitted. 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci:: 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci int 36562306a36Sopenharmony_ci dma_mapping_error(struct device *dev, dma_addr_t dma_addr) 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ciIn some circumstances dma_map_single(), dma_map_page() and dma_map_resource() 36862306a36Sopenharmony_ciwill fail to create a mapping. A driver can check for these errors by testing 36962306a36Sopenharmony_cithe returned DMA address with dma_mapping_error(). A non-zero return value 37062306a36Sopenharmony_cimeans the mapping could not be created and the driver should take appropriate 37162306a36Sopenharmony_ciaction (e.g. reduce current DMA mapping usage or delay and try again later). 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci:: 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci int 37662306a36Sopenharmony_ci dma_map_sg(struct device *dev, struct scatterlist *sg, 37762306a36Sopenharmony_ci int nents, enum dma_data_direction direction) 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ciReturns: the number of DMA address segments mapped (this may be shorter 38062306a36Sopenharmony_cithan <nents> passed in if some elements of the scatter/gather list are 38162306a36Sopenharmony_ciphysically or virtually adjacent and an IOMMU maps them with a single 38262306a36Sopenharmony_cientry). 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ciPlease note that the sg cannot be mapped again if it has been mapped once. 38562306a36Sopenharmony_ciThe mapping process is allowed to destroy information in the sg. 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ciAs with the other mapping interfaces, dma_map_sg() can fail. When it 38862306a36Sopenharmony_cidoes, 0 is returned and a driver must take appropriate action. It is 38962306a36Sopenharmony_cicritical that the driver do something, in the case of a block driver 39062306a36Sopenharmony_ciaborting the request or even oopsing is better than doing nothing and 39162306a36Sopenharmony_cicorrupting the filesystem. 39262306a36Sopenharmony_ci 39362306a36Sopenharmony_ciWith scatterlists, you use the resulting mapping like this:: 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci int i, count = dma_map_sg(dev, sglist, nents, direction); 39662306a36Sopenharmony_ci struct scatterlist *sg; 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci for_each_sg(sglist, sg, count, i) { 39962306a36Sopenharmony_ci hw_address[i] = sg_dma_address(sg); 40062306a36Sopenharmony_ci hw_len[i] = sg_dma_len(sg); 40162306a36Sopenharmony_ci } 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ciwhere nents is the number of entries in the sglist. 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ciThe implementation is free to merge several consecutive sglist entries 40662306a36Sopenharmony_ciinto one (e.g. with an IOMMU, or if several pages just happen to be 40762306a36Sopenharmony_ciphysically contiguous) and returns the actual number of sg entries it 40862306a36Sopenharmony_cimapped them to. On failure 0, is returned. 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ciThen you should loop count times (note: this can be less than nents times) 41162306a36Sopenharmony_ciand use sg_dma_address() and sg_dma_len() macros where you previously 41262306a36Sopenharmony_ciaccessed sg->address and sg->length as shown above. 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ci:: 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_ci void 41762306a36Sopenharmony_ci dma_unmap_sg(struct device *dev, struct scatterlist *sg, 41862306a36Sopenharmony_ci int nents, enum dma_data_direction direction) 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ciUnmap the previously mapped scatter/gather list. All the parameters 42162306a36Sopenharmony_cimust be the same as those and passed in to the scatter/gather mapping 42262306a36Sopenharmony_ciAPI. 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ciNote: <nents> must be the number you passed in, *not* the number of 42562306a36Sopenharmony_ciDMA address entries returned. 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci:: 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci void 43062306a36Sopenharmony_ci dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, 43162306a36Sopenharmony_ci size_t size, 43262306a36Sopenharmony_ci enum dma_data_direction direction) 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci void 43562306a36Sopenharmony_ci dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, 43662306a36Sopenharmony_ci size_t size, 43762306a36Sopenharmony_ci enum dma_data_direction direction) 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_ci void 44062306a36Sopenharmony_ci dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 44162306a36Sopenharmony_ci int nents, 44262306a36Sopenharmony_ci enum dma_data_direction direction) 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci void 44562306a36Sopenharmony_ci dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 44662306a36Sopenharmony_ci int nents, 44762306a36Sopenharmony_ci enum dma_data_direction direction) 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ciSynchronise a single contiguous or scatter/gather mapping for the CPU 45062306a36Sopenharmony_ciand device. With the sync_sg API, all the parameters must be the same 45162306a36Sopenharmony_cias those passed into the single mapping API. With the sync_single API, 45262306a36Sopenharmony_ciyou can use dma_handle and size parameters that aren't identical to 45362306a36Sopenharmony_cithose passed into the single mapping API to do a partial sync. 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci.. note:: 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci You must do this: 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci - Before reading values that have been written by DMA from the device 46162306a36Sopenharmony_ci (use the DMA_FROM_DEVICE direction) 46262306a36Sopenharmony_ci - After writing values that will be written to the device using DMA 46362306a36Sopenharmony_ci (use the DMA_TO_DEVICE) direction 46462306a36Sopenharmony_ci - before *and* after handing memory to the device if the memory is 46562306a36Sopenharmony_ci DMA_BIDIRECTIONAL 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ciSee also dma_map_single(). 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci:: 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_ci dma_addr_t 47262306a36Sopenharmony_ci dma_map_single_attrs(struct device *dev, void *cpu_addr, size_t size, 47362306a36Sopenharmony_ci enum dma_data_direction dir, 47462306a36Sopenharmony_ci unsigned long attrs) 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci void 47762306a36Sopenharmony_ci dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, 47862306a36Sopenharmony_ci size_t size, enum dma_data_direction dir, 47962306a36Sopenharmony_ci unsigned long attrs) 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci int 48262306a36Sopenharmony_ci dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, 48362306a36Sopenharmony_ci int nents, enum dma_data_direction dir, 48462306a36Sopenharmony_ci unsigned long attrs) 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci void 48762306a36Sopenharmony_ci dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl, 48862306a36Sopenharmony_ci int nents, enum dma_data_direction dir, 48962306a36Sopenharmony_ci unsigned long attrs) 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ciThe four functions above are just like the counterpart functions 49262306a36Sopenharmony_ciwithout the _attrs suffixes, except that they pass an optional 49362306a36Sopenharmony_cidma_attrs. 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_ciThe interpretation of DMA attributes is architecture-specific, and 49662306a36Sopenharmony_cieach attribute should be documented in 49762306a36Sopenharmony_ciDocumentation/core-api/dma-attributes.rst. 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ciIf dma_attrs are 0, the semantics of each of these functions 50062306a36Sopenharmony_ciis identical to those of the corresponding function 50162306a36Sopenharmony_ciwithout the _attrs suffix. As a result dma_map_single_attrs() 50262306a36Sopenharmony_cican generally replace dma_map_single(), etc. 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ciAs an example of the use of the ``*_attrs`` functions, here's how 50562306a36Sopenharmony_ciyou could pass an attribute DMA_ATTR_FOO when mapping memory 50662306a36Sopenharmony_cifor DMA:: 50762306a36Sopenharmony_ci 50862306a36Sopenharmony_ci #include <linux/dma-mapping.h> 50962306a36Sopenharmony_ci /* DMA_ATTR_FOO should be defined in linux/dma-mapping.h and 51062306a36Sopenharmony_ci * documented in Documentation/core-api/dma-attributes.rst */ 51162306a36Sopenharmony_ci ... 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci unsigned long attr; 51462306a36Sopenharmony_ci attr |= DMA_ATTR_FOO; 51562306a36Sopenharmony_ci .... 51662306a36Sopenharmony_ci n = dma_map_sg_attrs(dev, sg, nents, DMA_TO_DEVICE, attr); 51762306a36Sopenharmony_ci .... 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ciArchitectures that care about DMA_ATTR_FOO would check for its 52062306a36Sopenharmony_cipresence in their implementations of the mapping and unmapping 52162306a36Sopenharmony_ciroutines, e.g.::: 52262306a36Sopenharmony_ci 52362306a36Sopenharmony_ci void whizco_dma_map_sg_attrs(struct device *dev, dma_addr_t dma_addr, 52462306a36Sopenharmony_ci size_t size, enum dma_data_direction dir, 52562306a36Sopenharmony_ci unsigned long attrs) 52662306a36Sopenharmony_ci { 52762306a36Sopenharmony_ci .... 52862306a36Sopenharmony_ci if (attrs & DMA_ATTR_FOO) 52962306a36Sopenharmony_ci /* twizzle the frobnozzle */ 53062306a36Sopenharmony_ci .... 53162306a36Sopenharmony_ci } 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ciPart II - Non-coherent DMA allocations 53562306a36Sopenharmony_ci-------------------------------------- 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ciThese APIs allow to allocate pages that are guaranteed to be DMA addressable 53862306a36Sopenharmony_ciby the passed in device, but which need explicit management of memory ownership 53962306a36Sopenharmony_cifor the kernel vs the device. 54062306a36Sopenharmony_ci 54162306a36Sopenharmony_ciIf you don't understand how cache line coherency works between a processor and 54262306a36Sopenharmony_cian I/O device, you should not be using this part of the API. 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci:: 54562306a36Sopenharmony_ci 54662306a36Sopenharmony_ci struct page * 54762306a36Sopenharmony_ci dma_alloc_pages(struct device *dev, size_t size, dma_addr_t *dma_handle, 54862306a36Sopenharmony_ci enum dma_data_direction dir, gfp_t gfp) 54962306a36Sopenharmony_ci 55062306a36Sopenharmony_ciThis routine allocates a region of <size> bytes of non-coherent memory. It 55162306a36Sopenharmony_cireturns a pointer to first struct page for the region, or NULL if the 55262306a36Sopenharmony_ciallocation failed. The resulting struct page can be used for everything a 55362306a36Sopenharmony_cistruct page is suitable for. 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ciIt also returns a <dma_handle> which may be cast to an unsigned integer the 55662306a36Sopenharmony_cisame width as the bus and given to the device as the DMA address base of 55762306a36Sopenharmony_cithe region. 55862306a36Sopenharmony_ci 55962306a36Sopenharmony_ciThe dir parameter specified if data is read and/or written by the device, 56062306a36Sopenharmony_cisee dma_map_single() for details. 56162306a36Sopenharmony_ci 56262306a36Sopenharmony_ciThe gfp parameter allows the caller to specify the ``GFP_`` flags (see 56362306a36Sopenharmony_cikmalloc()) for the allocation, but rejects flags used to specify a memory 56462306a36Sopenharmony_cizone such as GFP_DMA or GFP_HIGHMEM. 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_ciBefore giving the memory to the device, dma_sync_single_for_device() needs 56762306a36Sopenharmony_cito be called, and before reading memory written by the device, 56862306a36Sopenharmony_cidma_sync_single_for_cpu(), just like for streaming DMA mappings that are 56962306a36Sopenharmony_cireused. 57062306a36Sopenharmony_ci 57162306a36Sopenharmony_ci:: 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ci void 57462306a36Sopenharmony_ci dma_free_pages(struct device *dev, size_t size, struct page *page, 57562306a36Sopenharmony_ci dma_addr_t dma_handle, enum dma_data_direction dir) 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ciFree a region of memory previously allocated using dma_alloc_pages(). 57862306a36Sopenharmony_cidev, size, dma_handle and dir must all be the same as those passed into 57962306a36Sopenharmony_cidma_alloc_pages(). page must be the pointer returned by dma_alloc_pages(). 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_ci:: 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci int 58462306a36Sopenharmony_ci dma_mmap_pages(struct device *dev, struct vm_area_struct *vma, 58562306a36Sopenharmony_ci size_t size, struct page *page) 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ciMap an allocation returned from dma_alloc_pages() into a user address space. 58862306a36Sopenharmony_cidev and size must be the same as those passed into dma_alloc_pages(). 58962306a36Sopenharmony_cipage must be the pointer returned by dma_alloc_pages(). 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci:: 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci void * 59462306a36Sopenharmony_ci dma_alloc_noncoherent(struct device *dev, size_t size, 59562306a36Sopenharmony_ci dma_addr_t *dma_handle, enum dma_data_direction dir, 59662306a36Sopenharmony_ci gfp_t gfp) 59762306a36Sopenharmony_ci 59862306a36Sopenharmony_ciThis routine is a convenient wrapper around dma_alloc_pages that returns the 59962306a36Sopenharmony_cikernel virtual address for the allocated memory instead of the page structure. 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_ci:: 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci void 60462306a36Sopenharmony_ci dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr, 60562306a36Sopenharmony_ci dma_addr_t dma_handle, enum dma_data_direction dir) 60662306a36Sopenharmony_ci 60762306a36Sopenharmony_ciFree a region of memory previously allocated using dma_alloc_noncoherent(). 60862306a36Sopenharmony_cidev, size, dma_handle and dir must all be the same as those passed into 60962306a36Sopenharmony_cidma_alloc_noncoherent(). cpu_addr must be the virtual address returned by 61062306a36Sopenharmony_cidma_alloc_noncoherent(). 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci:: 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci struct sg_table * 61562306a36Sopenharmony_ci dma_alloc_noncontiguous(struct device *dev, size_t size, 61662306a36Sopenharmony_ci enum dma_data_direction dir, gfp_t gfp, 61762306a36Sopenharmony_ci unsigned long attrs); 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ciThis routine allocates <size> bytes of non-coherent and possibly non-contiguous 62062306a36Sopenharmony_cimemory. It returns a pointer to struct sg_table that describes the allocated 62162306a36Sopenharmony_ciand DMA mapped memory, or NULL if the allocation failed. The resulting memory 62262306a36Sopenharmony_cican be used for struct page mapped into a scatterlist are suitable for. 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ciThe return sg_table is guaranteed to have 1 single DMA mapped segment as 62562306a36Sopenharmony_ciindicated by sgt->nents, but it might have multiple CPU side segments as 62662306a36Sopenharmony_ciindicated by sgt->orig_nents. 62762306a36Sopenharmony_ci 62862306a36Sopenharmony_ciThe dir parameter specified if data is read and/or written by the device, 62962306a36Sopenharmony_cisee dma_map_single() for details. 63062306a36Sopenharmony_ci 63162306a36Sopenharmony_ciThe gfp parameter allows the caller to specify the ``GFP_`` flags (see 63262306a36Sopenharmony_cikmalloc()) for the allocation, but rejects flags used to specify a memory 63362306a36Sopenharmony_cizone such as GFP_DMA or GFP_HIGHMEM. 63462306a36Sopenharmony_ci 63562306a36Sopenharmony_ciThe attrs argument must be either 0 or DMA_ATTR_ALLOC_SINGLE_PAGES. 63662306a36Sopenharmony_ci 63762306a36Sopenharmony_ciBefore giving the memory to the device, dma_sync_sgtable_for_device() needs 63862306a36Sopenharmony_cito be called, and before reading memory written by the device, 63962306a36Sopenharmony_cidma_sync_sgtable_for_cpu(), just like for streaming DMA mappings that are 64062306a36Sopenharmony_cireused. 64162306a36Sopenharmony_ci 64262306a36Sopenharmony_ci:: 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ci void 64562306a36Sopenharmony_ci dma_free_noncontiguous(struct device *dev, size_t size, 64662306a36Sopenharmony_ci struct sg_table *sgt, 64762306a36Sopenharmony_ci enum dma_data_direction dir) 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ciFree memory previously allocated using dma_alloc_noncontiguous(). dev, size, 65062306a36Sopenharmony_ciand dir must all be the same as those passed into dma_alloc_noncontiguous(). 65162306a36Sopenharmony_cisgt must be the pointer returned by dma_alloc_noncontiguous(). 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_ci:: 65462306a36Sopenharmony_ci 65562306a36Sopenharmony_ci void * 65662306a36Sopenharmony_ci dma_vmap_noncontiguous(struct device *dev, size_t size, 65762306a36Sopenharmony_ci struct sg_table *sgt) 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_ciReturn a contiguous kernel mapping for an allocation returned from 66062306a36Sopenharmony_cidma_alloc_noncontiguous(). dev and size must be the same as those passed into 66162306a36Sopenharmony_cidma_alloc_noncontiguous(). sgt must be the pointer returned by 66262306a36Sopenharmony_cidma_alloc_noncontiguous(). 66362306a36Sopenharmony_ci 66462306a36Sopenharmony_ciOnce a non-contiguous allocation is mapped using this function, the 66562306a36Sopenharmony_ciflush_kernel_vmap_range() and invalidate_kernel_vmap_range() APIs must be used 66662306a36Sopenharmony_cito manage the coherency between the kernel mapping, the device and user space 66762306a36Sopenharmony_cimappings (if any). 66862306a36Sopenharmony_ci 66962306a36Sopenharmony_ci:: 67062306a36Sopenharmony_ci 67162306a36Sopenharmony_ci void 67262306a36Sopenharmony_ci dma_vunmap_noncontiguous(struct device *dev, void *vaddr) 67362306a36Sopenharmony_ci 67462306a36Sopenharmony_ciUnmap a kernel mapping returned by dma_vmap_noncontiguous(). dev must be the 67562306a36Sopenharmony_cisame the one passed into dma_alloc_noncontiguous(). vaddr must be the pointer 67662306a36Sopenharmony_cireturned by dma_vmap_noncontiguous(). 67762306a36Sopenharmony_ci 67862306a36Sopenharmony_ci 67962306a36Sopenharmony_ci:: 68062306a36Sopenharmony_ci 68162306a36Sopenharmony_ci int 68262306a36Sopenharmony_ci dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, 68362306a36Sopenharmony_ci size_t size, struct sg_table *sgt) 68462306a36Sopenharmony_ci 68562306a36Sopenharmony_ciMap an allocation returned from dma_alloc_noncontiguous() into a user address 68662306a36Sopenharmony_cispace. dev and size must be the same as those passed into 68762306a36Sopenharmony_cidma_alloc_noncontiguous(). sgt must be the pointer returned by 68862306a36Sopenharmony_cidma_alloc_noncontiguous(). 68962306a36Sopenharmony_ci 69062306a36Sopenharmony_ci:: 69162306a36Sopenharmony_ci 69262306a36Sopenharmony_ci int 69362306a36Sopenharmony_ci dma_get_cache_alignment(void) 69462306a36Sopenharmony_ci 69562306a36Sopenharmony_ciReturns the processor cache alignment. This is the absolute minimum 69662306a36Sopenharmony_cialignment *and* width that you must observe when either mapping 69762306a36Sopenharmony_cimemory or doing partial flushes. 69862306a36Sopenharmony_ci 69962306a36Sopenharmony_ci.. note:: 70062306a36Sopenharmony_ci 70162306a36Sopenharmony_ci This API may return a number *larger* than the actual cache 70262306a36Sopenharmony_ci line, but it will guarantee that one or more cache lines fit exactly 70362306a36Sopenharmony_ci into the width returned by this call. It will also always be a power 70462306a36Sopenharmony_ci of two for easy alignment. 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci 70762306a36Sopenharmony_ciPart III - Debug drivers use of the DMA-API 70862306a36Sopenharmony_ci------------------------------------------- 70962306a36Sopenharmony_ci 71062306a36Sopenharmony_ciThe DMA-API as described above has some constraints. DMA addresses must be 71162306a36Sopenharmony_cireleased with the corresponding function with the same size for example. With 71262306a36Sopenharmony_cithe advent of hardware IOMMUs it becomes more and more important that drivers 71362306a36Sopenharmony_cido not violate those constraints. In the worst case such a violation can 71462306a36Sopenharmony_ciresult in data corruption up to destroyed filesystems. 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ciTo debug drivers and find bugs in the usage of the DMA-API checking code can 71762306a36Sopenharmony_cibe compiled into the kernel which will tell the developer about those 71862306a36Sopenharmony_civiolations. If your architecture supports it you can select the "Enable 71962306a36Sopenharmony_cidebugging of DMA-API usage" option in your kernel configuration. Enabling this 72062306a36Sopenharmony_cioption has a performance impact. Do not enable it in production kernels. 72162306a36Sopenharmony_ci 72262306a36Sopenharmony_ciIf you boot the resulting kernel will contain code which does some bookkeeping 72362306a36Sopenharmony_ciabout what DMA memory was allocated for which device. If this code detects an 72462306a36Sopenharmony_cierror it prints a warning message with some details into your kernel log. An 72562306a36Sopenharmony_ciexample warning message may look like this:: 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ci WARNING: at /data2/repos/linux-2.6-iommu/lib/dma-debug.c:448 72862306a36Sopenharmony_ci check_unmap+0x203/0x490() 72962306a36Sopenharmony_ci Hardware name: 73062306a36Sopenharmony_ci forcedeth 0000:00:08.0: DMA-API: device driver frees DMA memory with wrong 73162306a36Sopenharmony_ci function [device address=0x00000000640444be] [size=66 bytes] [mapped as 73262306a36Sopenharmony_ci single] [unmapped as page] 73362306a36Sopenharmony_ci Modules linked in: nfsd exportfs bridge stp llc r8169 73462306a36Sopenharmony_ci Pid: 0, comm: swapper Tainted: G W 2.6.28-dmatest-09289-g8bb99c0 #1 73562306a36Sopenharmony_ci Call Trace: 73662306a36Sopenharmony_ci <IRQ> [<ffffffff80240b22>] warn_slowpath+0xf2/0x130 73762306a36Sopenharmony_ci [<ffffffff80647b70>] _spin_unlock+0x10/0x30 73862306a36Sopenharmony_ci [<ffffffff80537e75>] usb_hcd_link_urb_to_ep+0x75/0xc0 73962306a36Sopenharmony_ci [<ffffffff80647c22>] _spin_unlock_irqrestore+0x12/0x40 74062306a36Sopenharmony_ci [<ffffffff8055347f>] ohci_urb_enqueue+0x19f/0x7c0 74162306a36Sopenharmony_ci [<ffffffff80252f96>] queue_work+0x56/0x60 74262306a36Sopenharmony_ci [<ffffffff80237e10>] enqueue_task_fair+0x20/0x50 74362306a36Sopenharmony_ci [<ffffffff80539279>] usb_hcd_submit_urb+0x379/0xbc0 74462306a36Sopenharmony_ci [<ffffffff803b78c3>] cpumask_next_and+0x23/0x40 74562306a36Sopenharmony_ci [<ffffffff80235177>] find_busiest_group+0x207/0x8a0 74662306a36Sopenharmony_ci [<ffffffff8064784f>] _spin_lock_irqsave+0x1f/0x50 74762306a36Sopenharmony_ci [<ffffffff803c7ea3>] check_unmap+0x203/0x490 74862306a36Sopenharmony_ci [<ffffffff803c8259>] debug_dma_unmap_page+0x49/0x50 74962306a36Sopenharmony_ci [<ffffffff80485f26>] nv_tx_done_optimized+0xc6/0x2c0 75062306a36Sopenharmony_ci [<ffffffff80486c13>] nv_nic_irq_optimized+0x73/0x2b0 75162306a36Sopenharmony_ci [<ffffffff8026df84>] handle_IRQ_event+0x34/0x70 75262306a36Sopenharmony_ci [<ffffffff8026ffe9>] handle_edge_irq+0xc9/0x150 75362306a36Sopenharmony_ci [<ffffffff8020e3ab>] do_IRQ+0xcb/0x1c0 75462306a36Sopenharmony_ci [<ffffffff8020c093>] ret_from_intr+0x0/0xa 75562306a36Sopenharmony_ci <EOI> <4>---[ end trace f6435a98e2a38c0e ]--- 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ciThe driver developer can find the driver and the device including a stacktrace 75862306a36Sopenharmony_ciof the DMA-API call which caused this warning. 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ciPer default only the first error will result in a warning message. All other 76162306a36Sopenharmony_cierrors will only silently counted. This limitation exist to prevent the code 76262306a36Sopenharmony_cifrom flooding your kernel log. To support debugging a device driver this can 76362306a36Sopenharmony_cibe disabled via debugfs. See the debugfs interface documentation below for 76462306a36Sopenharmony_cidetails. 76562306a36Sopenharmony_ci 76662306a36Sopenharmony_ciThe debugfs directory for the DMA-API debugging code is called dma-api/. In 76762306a36Sopenharmony_cithis directory the following files can currently be found: 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_ci=============================== =============================================== 77062306a36Sopenharmony_cidma-api/all_errors This file contains a numeric value. If this 77162306a36Sopenharmony_ci value is not equal to zero the debugging code 77262306a36Sopenharmony_ci will print a warning for every error it finds 77362306a36Sopenharmony_ci into the kernel log. Be careful with this 77462306a36Sopenharmony_ci option, as it can easily flood your logs. 77562306a36Sopenharmony_ci 77662306a36Sopenharmony_cidma-api/disabled This read-only file contains the character 'Y' 77762306a36Sopenharmony_ci if the debugging code is disabled. This can 77862306a36Sopenharmony_ci happen when it runs out of memory or if it was 77962306a36Sopenharmony_ci disabled at boot time 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_cidma-api/dump This read-only file contains current DMA 78262306a36Sopenharmony_ci mappings. 78362306a36Sopenharmony_ci 78462306a36Sopenharmony_cidma-api/error_count This file is read-only and shows the total 78562306a36Sopenharmony_ci numbers of errors found. 78662306a36Sopenharmony_ci 78762306a36Sopenharmony_cidma-api/num_errors The number in this file shows how many 78862306a36Sopenharmony_ci warnings will be printed to the kernel log 78962306a36Sopenharmony_ci before it stops. This number is initialized to 79062306a36Sopenharmony_ci one at system boot and be set by writing into 79162306a36Sopenharmony_ci this file 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_cidma-api/min_free_entries This read-only file can be read to get the 79462306a36Sopenharmony_ci minimum number of free dma_debug_entries the 79562306a36Sopenharmony_ci allocator has ever seen. If this value goes 79662306a36Sopenharmony_ci down to zero the code will attempt to increase 79762306a36Sopenharmony_ci nr_total_entries to compensate. 79862306a36Sopenharmony_ci 79962306a36Sopenharmony_cidma-api/num_free_entries The current number of free dma_debug_entries 80062306a36Sopenharmony_ci in the allocator. 80162306a36Sopenharmony_ci 80262306a36Sopenharmony_cidma-api/nr_total_entries The total number of dma_debug_entries in the 80362306a36Sopenharmony_ci allocator, both free and used. 80462306a36Sopenharmony_ci 80562306a36Sopenharmony_cidma-api/driver_filter You can write a name of a driver into this file 80662306a36Sopenharmony_ci to limit the debug output to requests from that 80762306a36Sopenharmony_ci particular driver. Write an empty string to 80862306a36Sopenharmony_ci that file to disable the filter and see 80962306a36Sopenharmony_ci all errors again. 81062306a36Sopenharmony_ci=============================== =============================================== 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ciIf you have this code compiled into your kernel it will be enabled by default. 81362306a36Sopenharmony_ciIf you want to boot without the bookkeeping anyway you can provide 81462306a36Sopenharmony_ci'dma_debug=off' as a boot parameter. This will disable DMA-API debugging. 81562306a36Sopenharmony_ciNotice that you can not enable it again at runtime. You have to reboot to do 81662306a36Sopenharmony_ciso. 81762306a36Sopenharmony_ci 81862306a36Sopenharmony_ciIf you want to see debug messages only for a special device driver you can 81962306a36Sopenharmony_cispecify the dma_debug_driver=<drivername> parameter. This will enable the 82062306a36Sopenharmony_cidriver filter at boot time. The debug code will only print errors for that 82162306a36Sopenharmony_cidriver afterwards. This filter can be disabled or changed later using debugfs. 82262306a36Sopenharmony_ci 82362306a36Sopenharmony_ciWhen the code disables itself at runtime this is most likely because it ran 82462306a36Sopenharmony_ciout of dma_debug_entries and was unable to allocate more on-demand. 65536 82562306a36Sopenharmony_cientries are preallocated at boot - if this is too low for you boot with 82662306a36Sopenharmony_ci'dma_debug_entries=<your_desired_number>' to overwrite the default. Note 82762306a36Sopenharmony_cithat the code allocates entries in batches, so the exact number of 82862306a36Sopenharmony_cipreallocated entries may be greater than the actual number requested. The 82962306a36Sopenharmony_cicode will print to the kernel log each time it has dynamically allocated 83062306a36Sopenharmony_cias many entries as were initially preallocated. This is to indicate that a 83162306a36Sopenharmony_cilarger preallocation size may be appropriate, or if it happens continually 83262306a36Sopenharmony_cithat a driver may be leaking mappings. 83362306a36Sopenharmony_ci 83462306a36Sopenharmony_ci:: 83562306a36Sopenharmony_ci 83662306a36Sopenharmony_ci void 83762306a36Sopenharmony_ci debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr); 83862306a36Sopenharmony_ci 83962306a36Sopenharmony_cidma-debug interface debug_dma_mapping_error() to debug drivers that fail 84062306a36Sopenharmony_cito check DMA mapping errors on addresses returned by dma_map_single() and 84162306a36Sopenharmony_cidma_map_page() interfaces. This interface clears a flag set by 84262306a36Sopenharmony_cidebug_dma_map_page() to indicate that dma_mapping_error() has been called by 84362306a36Sopenharmony_cithe driver. When driver does unmap, debug_dma_unmap() checks the flag and if 84462306a36Sopenharmony_cithis flag is still set, prints warning message that includes call trace that 84562306a36Sopenharmony_cileads up to the unmap. This interface can be called from dma_mapping_error() 84662306a36Sopenharmony_ciroutines to enable DMA mapping error check debugging. 847