18c2ecf20Sopenharmony_ci========================= 28c2ecf20Sopenharmony_ciDynamic DMA mapping Guide 38c2ecf20Sopenharmony_ci========================= 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci:Author: David S. Miller <davem@redhat.com> 68c2ecf20Sopenharmony_ci:Author: Richard Henderson <rth@cygnus.com> 78c2ecf20Sopenharmony_ci:Author: Jakub Jelinek <jakub@redhat.com> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ciThis is a guide to device driver writers on how to use the DMA API 108c2ecf20Sopenharmony_ciwith example pseudo-code. For a concise description of the API, see 118c2ecf20Sopenharmony_ciDMA-API.txt. 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciCPU and DMA addresses 148c2ecf20Sopenharmony_ci===================== 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciThere are several kinds of addresses involved in the DMA API, and it's 178c2ecf20Sopenharmony_ciimportant to understand the differences. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciThe kernel normally uses virtual addresses. Any address returned by 208c2ecf20Sopenharmony_cikmalloc(), vmalloc(), and similar interfaces is a virtual address and can 218c2ecf20Sopenharmony_cibe stored in a ``void *``. 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciThe virtual memory system (TLB, page tables, etc.) translates virtual 248c2ecf20Sopenharmony_ciaddresses to CPU physical addresses, which are stored as "phys_addr_t" or 258c2ecf20Sopenharmony_ci"resource_size_t". The kernel manages device resources like registers as 268c2ecf20Sopenharmony_ciphysical addresses. These are the addresses in /proc/iomem. The physical 278c2ecf20Sopenharmony_ciaddress is not directly useful to a driver; it must use ioremap() to map 288c2ecf20Sopenharmony_cithe space and produce a virtual address. 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciI/O devices use a third kind of address: a "bus address". If a device has 318c2ecf20Sopenharmony_ciregisters at an MMIO address, or if it performs DMA to read or write system 328c2ecf20Sopenharmony_cimemory, the addresses used by the device are bus addresses. In some 338c2ecf20Sopenharmony_cisystems, bus addresses are identical to CPU physical addresses, but in 348c2ecf20Sopenharmony_cigeneral they are not. IOMMUs and host bridges can produce arbitrary 358c2ecf20Sopenharmony_cimappings between physical and bus addresses. 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciFrom a device's point of view, DMA uses the bus address space, but it may 388c2ecf20Sopenharmony_cibe restricted to a subset of that space. For example, even if a system 398c2ecf20Sopenharmony_cisupports 64-bit addresses for main memory and PCI BARs, it may use an IOMMU 408c2ecf20Sopenharmony_ciso devices only need to use 32-bit DMA addresses. 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ciHere's a picture and some examples:: 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci CPU CPU Bus 458c2ecf20Sopenharmony_ci Virtual Physical Address 468c2ecf20Sopenharmony_ci Address Address Space 478c2ecf20Sopenharmony_ci Space Space 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci +-------+ +------+ +------+ 508c2ecf20Sopenharmony_ci | | |MMIO | Offset | | 518c2ecf20Sopenharmony_ci | | Virtual |Space | applied | | 528c2ecf20Sopenharmony_ci C +-------+ --------> B +------+ ----------> +------+ A 538c2ecf20Sopenharmony_ci | | mapping | | by host | | 548c2ecf20Sopenharmony_ci +-----+ | | | | bridge | | +--------+ 558c2ecf20Sopenharmony_ci | | | | +------+ | | | | 568c2ecf20Sopenharmony_ci | CPU | | | | RAM | | | | Device | 578c2ecf20Sopenharmony_ci | | | | | | | | | | 588c2ecf20Sopenharmony_ci +-----+ +-------+ +------+ +------+ +--------+ 598c2ecf20Sopenharmony_ci | | Virtual |Buffer| Mapping | | 608c2ecf20Sopenharmony_ci X +-------+ --------> Y +------+ <---------- +------+ Z 618c2ecf20Sopenharmony_ci | | mapping | RAM | by IOMMU 628c2ecf20Sopenharmony_ci | | | | 638c2ecf20Sopenharmony_ci | | | | 648c2ecf20Sopenharmony_ci +-------+ +------+ 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ciDuring the enumeration process, the kernel learns about I/O devices and 678c2ecf20Sopenharmony_citheir MMIO space and the host bridges that connect them to the system. For 688c2ecf20Sopenharmony_ciexample, if a PCI device has a BAR, the kernel reads the bus address (A) 698c2ecf20Sopenharmony_cifrom the BAR and converts it to a CPU physical address (B). The address B 708c2ecf20Sopenharmony_ciis stored in a struct resource and usually exposed via /proc/iomem. When a 718c2ecf20Sopenharmony_cidriver claims a device, it typically uses ioremap() to map physical address 728c2ecf20Sopenharmony_ciB at a virtual address (C). It can then use, e.g., ioread32(C), to access 738c2ecf20Sopenharmony_cithe device registers at bus address A. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciIf the device supports DMA, the driver sets up a buffer using kmalloc() or 768c2ecf20Sopenharmony_cia similar interface, which returns a virtual address (X). The virtual 778c2ecf20Sopenharmony_cimemory system maps X to a physical address (Y) in system RAM. The driver 788c2ecf20Sopenharmony_cican use virtual address X to access the buffer, but the device itself 798c2ecf20Sopenharmony_cicannot because DMA doesn't go through the CPU virtual memory system. 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ciIn some simple systems, the device can do DMA directly to physical address 828c2ecf20Sopenharmony_ciY. But in many others, there is IOMMU hardware that translates DMA 838c2ecf20Sopenharmony_ciaddresses to physical addresses, e.g., it translates Z to Y. This is part 848c2ecf20Sopenharmony_ciof the reason for the DMA API: the driver can give a virtual address X to 858c2ecf20Sopenharmony_cian interface like dma_map_single(), which sets up any required IOMMU 868c2ecf20Sopenharmony_cimapping and returns the DMA address Z. The driver then tells the device to 878c2ecf20Sopenharmony_cido DMA to Z, and the IOMMU maps it to the buffer at address Y in system 888c2ecf20Sopenharmony_ciRAM. 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciSo that Linux can use the dynamic DMA mapping, it needs some help from the 918c2ecf20Sopenharmony_cidrivers, namely it has to take into account that DMA addresses should be 928c2ecf20Sopenharmony_cimapped only for the time they are actually used and unmapped after the DMA 938c2ecf20Sopenharmony_citransfer. 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ciThe following API will work of course even on platforms where no such 968c2ecf20Sopenharmony_cihardware exists. 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciNote that the DMA API works with any bus independent of the underlying 998c2ecf20Sopenharmony_cimicroprocessor architecture. You should use the DMA API rather than the 1008c2ecf20Sopenharmony_cibus-specific DMA API, i.e., use the dma_map_*() interfaces rather than the 1018c2ecf20Sopenharmony_cipci_map_*() interfaces. 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciFirst of all, you should make sure:: 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci #include <linux/dma-mapping.h> 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciis in your driver, which provides the definition of dma_addr_t. This type 1088c2ecf20Sopenharmony_cican hold any valid DMA address for the platform and should be used 1098c2ecf20Sopenharmony_cieverywhere you hold a DMA address returned from the DMA mapping functions. 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ciWhat memory is DMA'able? 1128c2ecf20Sopenharmony_ci======================== 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ciThe first piece of information you must know is what kernel memory can 1158c2ecf20Sopenharmony_cibe used with the DMA mapping facilities. There has been an unwritten 1168c2ecf20Sopenharmony_ciset of rules regarding this, and this text is an attempt to finally 1178c2ecf20Sopenharmony_ciwrite them down. 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciIf you acquired your memory via the page allocator 1208c2ecf20Sopenharmony_ci(i.e. __get_free_page*()) or the generic memory allocators 1218c2ecf20Sopenharmony_ci(i.e. kmalloc() or kmem_cache_alloc()) then you may DMA to/from 1228c2ecf20Sopenharmony_cithat memory using the addresses returned from those routines. 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ciThis means specifically that you may _not_ use the memory/addresses 1258c2ecf20Sopenharmony_cireturned from vmalloc() for DMA. It is possible to DMA to the 1268c2ecf20Sopenharmony_ci_underlying_ memory mapped into a vmalloc() area, but this requires 1278c2ecf20Sopenharmony_ciwalking page tables to get the physical addresses, and then 1288c2ecf20Sopenharmony_citranslating each of those pages back to a kernel address using 1298c2ecf20Sopenharmony_cisomething like __va(). [ EDIT: Update this when we integrate 1308c2ecf20Sopenharmony_ciGerd Knorr's generic code which does this. ] 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ciThis rule also means that you may use neither kernel image addresses 1338c2ecf20Sopenharmony_ci(items in data/text/bss segments), nor module image addresses, nor 1348c2ecf20Sopenharmony_cistack addresses for DMA. These could all be mapped somewhere entirely 1358c2ecf20Sopenharmony_cidifferent than the rest of physical memory. Even if those classes of 1368c2ecf20Sopenharmony_cimemory could physically work with DMA, you'd need to ensure the I/O 1378c2ecf20Sopenharmony_cibuffers were cacheline-aligned. Without that, you'd see cacheline 1388c2ecf20Sopenharmony_cisharing problems (data corruption) on CPUs with DMA-incoherent caches. 1398c2ecf20Sopenharmony_ci(The CPU could write to one word, DMA would write to a different one 1408c2ecf20Sopenharmony_ciin the same cache line, and one of them could be overwritten.) 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciAlso, this means that you cannot take the return of a kmap() 1438c2ecf20Sopenharmony_cicall and DMA to/from that. This is similar to vmalloc(). 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciWhat about block I/O and networking buffers? The block I/O and 1468c2ecf20Sopenharmony_cinetworking subsystems make sure that the buffers they use are valid 1478c2ecf20Sopenharmony_cifor you to DMA from/to. 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciDMA addressing capabilities 1508c2ecf20Sopenharmony_ci=========================== 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ciBy default, the kernel assumes that your device can address 32-bits of DMA 1538c2ecf20Sopenharmony_ciaddressing. For a 64-bit capable device, this needs to be increased, and for 1548c2ecf20Sopenharmony_cia device with limitations, it needs to be decreased. 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ciSpecial note about PCI: PCI-X specification requires PCI-X devices to support 1578c2ecf20Sopenharmony_ci64-bit addressing (DAC) for all transactions. And at least one platform (SGI 1588c2ecf20Sopenharmony_ciSN2) requires 64-bit consistent allocations to operate correctly when the IO 1598c2ecf20Sopenharmony_cibus is in PCI-X mode. 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ciFor correct operation, you must set the DMA mask to inform the kernel about 1628c2ecf20Sopenharmony_ciyour devices DMA addressing capabilities. 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ciThis is performed via a call to dma_set_mask_and_coherent():: 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci int dma_set_mask_and_coherent(struct device *dev, u64 mask); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ciwhich will set the mask for both streaming and coherent APIs together. If you 1698c2ecf20Sopenharmony_cihave some special requirements, then the following two separate calls can be 1708c2ecf20Sopenharmony_ciused instead: 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci The setup for streaming mappings is performed via a call to 1738c2ecf20Sopenharmony_ci dma_set_mask():: 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci int dma_set_mask(struct device *dev, u64 mask); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci The setup for consistent allocations is performed via a call 1788c2ecf20Sopenharmony_ci to dma_set_coherent_mask():: 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci int dma_set_coherent_mask(struct device *dev, u64 mask); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ciHere, dev is a pointer to the device struct of your device, and mask is a bit 1838c2ecf20Sopenharmony_cimask describing which bits of an address your device supports. Often the 1848c2ecf20Sopenharmony_cidevice struct of your device is embedded in the bus-specific device struct of 1858c2ecf20Sopenharmony_ciyour device. For example, &pdev->dev is a pointer to the device struct of a 1868c2ecf20Sopenharmony_ciPCI device (pdev is a pointer to the PCI device struct of your device). 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ciThese calls usually return zero to indicated your device can perform DMA 1898c2ecf20Sopenharmony_ciproperly on the machine given the address mask you provided, but they might 1908c2ecf20Sopenharmony_cireturn an error if the mask is too small to be supportable on the given 1918c2ecf20Sopenharmony_cisystem. If it returns non-zero, your device cannot perform DMA properly on 1928c2ecf20Sopenharmony_cithis platform, and attempting to do so will result in undefined behavior. 1938c2ecf20Sopenharmony_ciYou must not use DMA on this device unless the dma_set_mask family of 1948c2ecf20Sopenharmony_cifunctions has returned success. 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ciThis means that in the failure case, you have two options: 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci1) Use some non-DMA mode for data transfer, if possible. 1998c2ecf20Sopenharmony_ci2) Ignore this device and do not initialize it. 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ciIt is recommended that your driver print a kernel KERN_WARNING message when 2028c2ecf20Sopenharmony_cisetting the DMA mask fails. In this manner, if a user of your driver reports 2038c2ecf20Sopenharmony_cithat performance is bad or that the device is not even detected, you can ask 2048c2ecf20Sopenharmony_cithem for the kernel messages to find out exactly why. 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ciThe standard 64-bit addressing device would do something like this:: 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) { 2098c2ecf20Sopenharmony_ci dev_warn(dev, "mydev: No suitable DMA available\n"); 2108c2ecf20Sopenharmony_ci goto ignore_this_device; 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciIf the device only supports 32-bit addressing for descriptors in the 2148c2ecf20Sopenharmony_cicoherent allocations, but supports full 64-bits for streaming mappings 2158c2ecf20Sopenharmony_ciit would look like this:: 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci if (dma_set_mask(dev, DMA_BIT_MASK(64))) { 2188c2ecf20Sopenharmony_ci dev_warn(dev, "mydev: No suitable DMA available\n"); 2198c2ecf20Sopenharmony_ci goto ignore_this_device; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ciThe coherent mask will always be able to set the same or a smaller mask as 2238c2ecf20Sopenharmony_cithe streaming mask. However for the rare case that a device driver only 2248c2ecf20Sopenharmony_ciuses consistent allocations, one would have to check the return value from 2258c2ecf20Sopenharmony_cidma_set_coherent_mask(). 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ciFinally, if your device can only drive the low 24-bits of 2288c2ecf20Sopenharmony_ciaddress you might do something like:: 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci if (dma_set_mask(dev, DMA_BIT_MASK(24))) { 2318c2ecf20Sopenharmony_ci dev_warn(dev, "mydev: 24-bit DMA addressing not available\n"); 2328c2ecf20Sopenharmony_ci goto ignore_this_device; 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ciWhen dma_set_mask() or dma_set_mask_and_coherent() is successful, and 2368c2ecf20Sopenharmony_cireturns zero, the kernel saves away this mask you have provided. The 2378c2ecf20Sopenharmony_cikernel will use this information later when you make DMA mappings. 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ciThere is a case which we are aware of at this time, which is worth 2408c2ecf20Sopenharmony_cimentioning in this documentation. If your device supports multiple 2418c2ecf20Sopenharmony_cifunctions (for example a sound card provides playback and record 2428c2ecf20Sopenharmony_cifunctions) and the various different functions have _different_ 2438c2ecf20Sopenharmony_ciDMA addressing limitations, you may wish to probe each mask and 2448c2ecf20Sopenharmony_cionly provide the functionality which the machine can handle. It 2458c2ecf20Sopenharmony_ciis important that the last call to dma_set_mask() be for the 2468c2ecf20Sopenharmony_cimost specific mask. 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ciHere is pseudo-code showing how this might be done:: 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci #define PLAYBACK_ADDRESS_BITS DMA_BIT_MASK(32) 2518c2ecf20Sopenharmony_ci #define RECORD_ADDRESS_BITS DMA_BIT_MASK(24) 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci struct my_sound_card *card; 2548c2ecf20Sopenharmony_ci struct device *dev; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci ... 2578c2ecf20Sopenharmony_ci if (!dma_set_mask(dev, PLAYBACK_ADDRESS_BITS)) { 2588c2ecf20Sopenharmony_ci card->playback_enabled = 1; 2598c2ecf20Sopenharmony_ci } else { 2608c2ecf20Sopenharmony_ci card->playback_enabled = 0; 2618c2ecf20Sopenharmony_ci dev_warn(dev, "%s: Playback disabled due to DMA limitations\n", 2628c2ecf20Sopenharmony_ci card->name); 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci if (!dma_set_mask(dev, RECORD_ADDRESS_BITS)) { 2658c2ecf20Sopenharmony_ci card->record_enabled = 1; 2668c2ecf20Sopenharmony_ci } else { 2678c2ecf20Sopenharmony_ci card->record_enabled = 0; 2688c2ecf20Sopenharmony_ci dev_warn(dev, "%s: Record disabled due to DMA limitations\n", 2698c2ecf20Sopenharmony_ci card->name); 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ciA sound card was used as an example here because this genre of PCI 2738c2ecf20Sopenharmony_cidevices seems to be littered with ISA chips given a PCI front end, 2748c2ecf20Sopenharmony_ciand thus retaining the 16MB DMA addressing limitations of ISA. 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ciTypes of DMA mappings 2778c2ecf20Sopenharmony_ci===================== 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciThere are two types of DMA mappings: 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci- Consistent DMA mappings which are usually mapped at driver 2828c2ecf20Sopenharmony_ci initialization, unmapped at the end and for which the hardware should 2838c2ecf20Sopenharmony_ci guarantee that the device and the CPU can access the data 2848c2ecf20Sopenharmony_ci in parallel and will see updates made by each other without any 2858c2ecf20Sopenharmony_ci explicit software flushing. 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci Think of "consistent" as "synchronous" or "coherent". 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci The current default is to return consistent memory in the low 32 2908c2ecf20Sopenharmony_ci bits of the DMA space. However, for future compatibility you should 2918c2ecf20Sopenharmony_ci set the consistent mask even if this default is fine for your 2928c2ecf20Sopenharmony_ci driver. 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci Good examples of what to use consistent mappings for are: 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci - Network card DMA ring descriptors. 2978c2ecf20Sopenharmony_ci - SCSI adapter mailbox command data structures. 2988c2ecf20Sopenharmony_ci - Device firmware microcode executed out of 2998c2ecf20Sopenharmony_ci main memory. 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci The invariant these examples all require is that any CPU store 3028c2ecf20Sopenharmony_ci to memory is immediately visible to the device, and vice 3038c2ecf20Sopenharmony_ci versa. Consistent mappings guarantee this. 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci .. important:: 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci Consistent DMA memory does not preclude the usage of 3088c2ecf20Sopenharmony_ci proper memory barriers. The CPU may reorder stores to 3098c2ecf20Sopenharmony_ci consistent memory just as it may normal memory. Example: 3108c2ecf20Sopenharmony_ci if it is important for the device to see the first word 3118c2ecf20Sopenharmony_ci of a descriptor updated before the second, you must do 3128c2ecf20Sopenharmony_ci something like:: 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci desc->word0 = address; 3158c2ecf20Sopenharmony_ci wmb(); 3168c2ecf20Sopenharmony_ci desc->word1 = DESC_VALID; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci in order to get correct behavior on all platforms. 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci Also, on some platforms your driver may need to flush CPU write 3218c2ecf20Sopenharmony_ci buffers in much the same way as it needs to flush write buffers 3228c2ecf20Sopenharmony_ci found in PCI bridges (such as by reading a register's value 3238c2ecf20Sopenharmony_ci after writing it). 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci- Streaming DMA mappings which are usually mapped for one DMA 3268c2ecf20Sopenharmony_ci transfer, unmapped right after it (unless you use dma_sync_* below) 3278c2ecf20Sopenharmony_ci and for which hardware can optimize for sequential accesses. 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci Think of "streaming" as "asynchronous" or "outside the coherency 3308c2ecf20Sopenharmony_ci domain". 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci Good examples of what to use streaming mappings for are: 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci - Networking buffers transmitted/received by a device. 3358c2ecf20Sopenharmony_ci - Filesystem buffers written/read by a SCSI device. 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci The interfaces for using this type of mapping were designed in 3388c2ecf20Sopenharmony_ci such a way that an implementation can make whatever performance 3398c2ecf20Sopenharmony_ci optimizations the hardware allows. To this end, when using 3408c2ecf20Sopenharmony_ci such mappings you must be explicit about what you want to happen. 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ciNeither type of DMA mapping has alignment restrictions that come from 3438c2ecf20Sopenharmony_cithe underlying bus, although some devices may have such restrictions. 3448c2ecf20Sopenharmony_ciAlso, systems with caches that aren't DMA-coherent will work better 3458c2ecf20Sopenharmony_ciwhen the underlying buffers don't share cache lines with other data. 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ciUsing Consistent DMA mappings 3498c2ecf20Sopenharmony_ci============================= 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ciTo allocate and map large (PAGE_SIZE or so) consistent DMA regions, 3528c2ecf20Sopenharmony_ciyou should do:: 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci dma_addr_t dma_handle; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci cpu_addr = dma_alloc_coherent(dev, size, &dma_handle, gfp); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ciwhere device is a ``struct device *``. This may be called in interrupt 3598c2ecf20Sopenharmony_cicontext with the GFP_ATOMIC flag. 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ciSize is the length of the region you want to allocate, in bytes. 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ciThis routine will allocate RAM for that region, so it acts similarly to 3648c2ecf20Sopenharmony_ci__get_free_pages() (but takes size instead of a page order). If your 3658c2ecf20Sopenharmony_cidriver needs regions sized smaller than a page, you may prefer using 3668c2ecf20Sopenharmony_cithe dma_pool interface, described below. 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ciThe consistent DMA mapping interfaces, will by default return a DMA address 3698c2ecf20Sopenharmony_ciwhich is 32-bit addressable. Even if the device indicates (via the DMA mask) 3708c2ecf20Sopenharmony_cithat it may address the upper 32-bits, consistent allocation will only 3718c2ecf20Sopenharmony_cireturn > 32-bit addresses for DMA if the consistent DMA mask has been 3728c2ecf20Sopenharmony_ciexplicitly changed via dma_set_coherent_mask(). This is true of the 3738c2ecf20Sopenharmony_cidma_pool interface as well. 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_cidma_alloc_coherent() returns two values: the virtual address which you 3768c2ecf20Sopenharmony_cican use to access it from the CPU and dma_handle which you pass to the 3778c2ecf20Sopenharmony_cicard. 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ciThe CPU virtual address and the DMA address are both 3808c2ecf20Sopenharmony_ciguaranteed to be aligned to the smallest PAGE_SIZE order which 3818c2ecf20Sopenharmony_ciis greater than or equal to the requested size. This invariant 3828c2ecf20Sopenharmony_ciexists (for example) to guarantee that if you allocate a chunk 3838c2ecf20Sopenharmony_ciwhich is smaller than or equal to 64 kilobytes, the extent of the 3848c2ecf20Sopenharmony_cibuffer you receive will not cross a 64K boundary. 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ciTo unmap and free such a DMA region, you call:: 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci dma_free_coherent(dev, size, cpu_addr, dma_handle); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ciwhere dev, size are the same as in the above call and cpu_addr and 3918c2ecf20Sopenharmony_cidma_handle are the values dma_alloc_coherent() returned to you. 3928c2ecf20Sopenharmony_ciThis function may not be called in interrupt context. 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ciIf your driver needs lots of smaller memory regions, you can write 3958c2ecf20Sopenharmony_cicustom code to subdivide pages returned by dma_alloc_coherent(), 3968c2ecf20Sopenharmony_cior you can use the dma_pool API to do that. A dma_pool is like 3978c2ecf20Sopenharmony_cia kmem_cache, but it uses dma_alloc_coherent(), not __get_free_pages(). 3988c2ecf20Sopenharmony_ciAlso, it understands common hardware constraints for alignment, 3998c2ecf20Sopenharmony_cilike queue heads needing to be aligned on N byte boundaries. 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ciCreate a dma_pool like this:: 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci struct dma_pool *pool; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci pool = dma_pool_create(name, dev, size, align, boundary); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ciThe "name" is for diagnostics (like a kmem_cache name); dev and size 4088c2ecf20Sopenharmony_ciare as above. The device's hardware alignment requirement for this 4098c2ecf20Sopenharmony_citype of data is "align" (which is expressed in bytes, and must be a 4108c2ecf20Sopenharmony_cipower of two). If your device has no boundary crossing restrictions, 4118c2ecf20Sopenharmony_cipass 0 for boundary; passing 4096 says memory allocated from this pool 4128c2ecf20Sopenharmony_cimust not cross 4KByte boundaries (but at that time it may be better to 4138c2ecf20Sopenharmony_ciuse dma_alloc_coherent() directly instead). 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ciAllocate memory from a DMA pool like this:: 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci cpu_addr = dma_pool_alloc(pool, flags, &dma_handle); 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ciflags are GFP_KERNEL if blocking is permitted (not in_interrupt nor 4208c2ecf20Sopenharmony_ciholding SMP locks), GFP_ATOMIC otherwise. Like dma_alloc_coherent(), 4218c2ecf20Sopenharmony_cithis returns two values, cpu_addr and dma_handle. 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ciFree memory that was allocated from a dma_pool like this:: 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci dma_pool_free(pool, cpu_addr, dma_handle); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ciwhere pool is what you passed to dma_pool_alloc(), and cpu_addr and 4288c2ecf20Sopenharmony_cidma_handle are the values dma_pool_alloc() returned. This function 4298c2ecf20Sopenharmony_cimay be called in interrupt context. 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ciDestroy a dma_pool by calling:: 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci dma_pool_destroy(pool); 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ciMake sure you've called dma_pool_free() for all memory allocated 4368c2ecf20Sopenharmony_cifrom a pool before you destroy the pool. This function may not 4378c2ecf20Sopenharmony_cibe called in interrupt context. 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ciDMA Direction 4408c2ecf20Sopenharmony_ci============= 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ciThe interfaces described in subsequent portions of this document 4438c2ecf20Sopenharmony_citake a DMA direction argument, which is an integer and takes on 4448c2ecf20Sopenharmony_cione of the following values:: 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci DMA_BIDIRECTIONAL 4478c2ecf20Sopenharmony_ci DMA_TO_DEVICE 4488c2ecf20Sopenharmony_ci DMA_FROM_DEVICE 4498c2ecf20Sopenharmony_ci DMA_NONE 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ciYou should provide the exact DMA direction if you know it. 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ciDMA_TO_DEVICE means "from main memory to the device" 4548c2ecf20Sopenharmony_ciDMA_FROM_DEVICE means "from the device to main memory" 4558c2ecf20Sopenharmony_ciIt is the direction in which the data moves during the DMA 4568c2ecf20Sopenharmony_citransfer. 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ciYou are _strongly_ encouraged to specify this as precisely 4598c2ecf20Sopenharmony_cias you possibly can. 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ciIf you absolutely cannot know the direction of the DMA transfer, 4628c2ecf20Sopenharmony_cispecify DMA_BIDIRECTIONAL. It means that the DMA can go in 4638c2ecf20Sopenharmony_cieither direction. The platform guarantees that you may legally 4648c2ecf20Sopenharmony_cispecify this, and that it will work, but this may be at the 4658c2ecf20Sopenharmony_cicost of performance for example. 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ciThe value DMA_NONE is to be used for debugging. One can 4688c2ecf20Sopenharmony_cihold this in a data structure before you come to know the 4698c2ecf20Sopenharmony_ciprecise direction, and this will help catch cases where your 4708c2ecf20Sopenharmony_cidirection tracking logic has failed to set things up properly. 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ciAnother advantage of specifying this value precisely (outside of 4738c2ecf20Sopenharmony_cipotential platform-specific optimizations of such) is for debugging. 4748c2ecf20Sopenharmony_ciSome platforms actually have a write permission boolean which DMA 4758c2ecf20Sopenharmony_cimappings can be marked with, much like page protections in the user 4768c2ecf20Sopenharmony_ciprogram address space. Such platforms can and do report errors in the 4778c2ecf20Sopenharmony_cikernel logs when the DMA controller hardware detects violation of the 4788c2ecf20Sopenharmony_cipermission setting. 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ciOnly streaming mappings specify a direction, consistent mappings 4818c2ecf20Sopenharmony_ciimplicitly have a direction attribute setting of 4828c2ecf20Sopenharmony_ciDMA_BIDIRECTIONAL. 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ciThe SCSI subsystem tells you the direction to use in the 4858c2ecf20Sopenharmony_ci'sc_data_direction' member of the SCSI command your driver is 4868c2ecf20Sopenharmony_ciworking on. 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ciFor Networking drivers, it's a rather simple affair. For transmit 4898c2ecf20Sopenharmony_cipackets, map/unmap them with the DMA_TO_DEVICE direction 4908c2ecf20Sopenharmony_cispecifier. For receive packets, just the opposite, map/unmap them 4918c2ecf20Sopenharmony_ciwith the DMA_FROM_DEVICE direction specifier. 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ciUsing Streaming DMA mappings 4948c2ecf20Sopenharmony_ci============================ 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ciThe streaming DMA mapping routines can be called from interrupt 4978c2ecf20Sopenharmony_cicontext. There are two versions of each map/unmap, one which will 4988c2ecf20Sopenharmony_cimap/unmap a single memory region, and one which will map/unmap a 4998c2ecf20Sopenharmony_ciscatterlist. 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ciTo map a single region, you do:: 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci struct device *dev = &my_dev->dev; 5048c2ecf20Sopenharmony_ci dma_addr_t dma_handle; 5058c2ecf20Sopenharmony_ci void *addr = buffer->ptr; 5068c2ecf20Sopenharmony_ci size_t size = buffer->len; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci dma_handle = dma_map_single(dev, addr, size, direction); 5098c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_handle)) { 5108c2ecf20Sopenharmony_ci /* 5118c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 5128c2ecf20Sopenharmony_ci * delay and try again later or 5138c2ecf20Sopenharmony_ci * reset driver. 5148c2ecf20Sopenharmony_ci */ 5158c2ecf20Sopenharmony_ci goto map_error_handling; 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ciand to unmap it:: 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci dma_unmap_single(dev, dma_handle, size, direction); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ciYou should call dma_mapping_error() as dma_map_single() could fail and return 5238c2ecf20Sopenharmony_cierror. Doing so will ensure that the mapping code will work correctly on all 5248c2ecf20Sopenharmony_ciDMA implementations without any dependency on the specifics of the underlying 5258c2ecf20Sopenharmony_ciimplementation. Using the returned address without checking for errors could 5268c2ecf20Sopenharmony_ciresult in failures ranging from panics to silent data corruption. The same 5278c2ecf20Sopenharmony_ciapplies to dma_map_page() as well. 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ciYou should call dma_unmap_single() when the DMA activity is finished, e.g., 5308c2ecf20Sopenharmony_cifrom the interrupt which told you that the DMA transfer is done. 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ciUsing CPU pointers like this for single mappings has a disadvantage: 5338c2ecf20Sopenharmony_ciyou cannot reference HIGHMEM memory in this way. Thus, there is a 5348c2ecf20Sopenharmony_cimap/unmap interface pair akin to dma_{map,unmap}_single(). These 5358c2ecf20Sopenharmony_ciinterfaces deal with page/offset pairs instead of CPU pointers. 5368c2ecf20Sopenharmony_ciSpecifically:: 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci struct device *dev = &my_dev->dev; 5398c2ecf20Sopenharmony_ci dma_addr_t dma_handle; 5408c2ecf20Sopenharmony_ci struct page *page = buffer->page; 5418c2ecf20Sopenharmony_ci unsigned long offset = buffer->offset; 5428c2ecf20Sopenharmony_ci size_t size = buffer->len; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci dma_handle = dma_map_page(dev, page, offset, size, direction); 5458c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_handle)) { 5468c2ecf20Sopenharmony_ci /* 5478c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 5488c2ecf20Sopenharmony_ci * delay and try again later or 5498c2ecf20Sopenharmony_ci * reset driver. 5508c2ecf20Sopenharmony_ci */ 5518c2ecf20Sopenharmony_ci goto map_error_handling; 5528c2ecf20Sopenharmony_ci } 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci ... 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci dma_unmap_page(dev, dma_handle, size, direction); 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ciHere, "offset" means byte offset within the given page. 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ciYou should call dma_mapping_error() as dma_map_page() could fail and return 5618c2ecf20Sopenharmony_cierror as outlined under the dma_map_single() discussion. 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ciYou should call dma_unmap_page() when the DMA activity is finished, e.g., 5648c2ecf20Sopenharmony_cifrom the interrupt which told you that the DMA transfer is done. 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ciWith scatterlists, you map a region gathered from several regions by:: 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci int i, count = dma_map_sg(dev, sglist, nents, direction); 5698c2ecf20Sopenharmony_ci struct scatterlist *sg; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci for_each_sg(sglist, sg, count, i) { 5728c2ecf20Sopenharmony_ci hw_address[i] = sg_dma_address(sg); 5738c2ecf20Sopenharmony_ci hw_len[i] = sg_dma_len(sg); 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ciwhere nents is the number of entries in the sglist. 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ciThe implementation is free to merge several consecutive sglist entries 5798c2ecf20Sopenharmony_ciinto one (e.g. if DMA mapping is done with PAGE_SIZE granularity, any 5808c2ecf20Sopenharmony_ciconsecutive sglist entries can be merged into one provided the first one 5818c2ecf20Sopenharmony_ciends and the second one starts on a page boundary - in fact this is a huge 5828c2ecf20Sopenharmony_ciadvantage for cards which either cannot do scatter-gather or have very 5838c2ecf20Sopenharmony_cilimited number of scatter-gather entries) and returns the actual number 5848c2ecf20Sopenharmony_ciof sg entries it mapped them to. On failure 0 is returned. 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ciThen you should loop count times (note: this can be less than nents times) 5878c2ecf20Sopenharmony_ciand use sg_dma_address() and sg_dma_len() macros where you previously 5888c2ecf20Sopenharmony_ciaccessed sg->address and sg->length as shown above. 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ciTo unmap a scatterlist, just call:: 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci dma_unmap_sg(dev, sglist, nents, direction); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ciAgain, make sure DMA activity has already finished. 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci.. note:: 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci The 'nents' argument to the dma_unmap_sg call must be 5998c2ecf20Sopenharmony_ci the _same_ one you passed into the dma_map_sg call, 6008c2ecf20Sopenharmony_ci it should _NOT_ be the 'count' value _returned_ from the 6018c2ecf20Sopenharmony_ci dma_map_sg call. 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ciEvery dma_map_{single,sg}() call should have its dma_unmap_{single,sg}() 6048c2ecf20Sopenharmony_cicounterpart, because the DMA address space is a shared resource and 6058c2ecf20Sopenharmony_ciyou could render the machine unusable by consuming all DMA addresses. 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ciIf you need to use the same streaming DMA region multiple times and touch 6088c2ecf20Sopenharmony_cithe data in between the DMA transfers, the buffer needs to be synced 6098c2ecf20Sopenharmony_ciproperly in order for the CPU and device to see the most up-to-date and 6108c2ecf20Sopenharmony_cicorrect copy of the DMA buffer. 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ciSo, firstly, just map it with dma_map_{single,sg}(), and after each DMA 6138c2ecf20Sopenharmony_citransfer call either:: 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci dma_sync_single_for_cpu(dev, dma_handle, size, direction); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_cior:: 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci dma_sync_sg_for_cpu(dev, sglist, nents, direction); 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_cias appropriate. 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ciThen, if you wish to let the device get at the DMA area again, 6248c2ecf20Sopenharmony_cifinish accessing the data with the CPU, and then before actually 6258c2ecf20Sopenharmony_cigiving the buffer to the hardware call either:: 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci dma_sync_single_for_device(dev, dma_handle, size, direction); 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_cior:: 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci dma_sync_sg_for_device(dev, sglist, nents, direction); 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_cias appropriate. 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci.. note:: 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci The 'nents' argument to dma_sync_sg_for_cpu() and 6388c2ecf20Sopenharmony_ci dma_sync_sg_for_device() must be the same passed to 6398c2ecf20Sopenharmony_ci dma_map_sg(). It is _NOT_ the count returned by 6408c2ecf20Sopenharmony_ci dma_map_sg(). 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ciAfter the last DMA transfer call one of the DMA unmap routines 6438c2ecf20Sopenharmony_cidma_unmap_{single,sg}(). If you don't touch the data from the first 6448c2ecf20Sopenharmony_cidma_map_*() call till dma_unmap_*(), then you don't have to call the 6458c2ecf20Sopenharmony_cidma_sync_*() routines at all. 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ciHere is pseudo code which shows a situation in which you would need 6488c2ecf20Sopenharmony_cito use the dma_sync_*() interfaces:: 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci my_card_setup_receive_buffer(struct my_card *cp, char *buffer, int len) 6518c2ecf20Sopenharmony_ci { 6528c2ecf20Sopenharmony_ci dma_addr_t mapping; 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci mapping = dma_map_single(cp->dev, buffer, len, DMA_FROM_DEVICE); 6558c2ecf20Sopenharmony_ci if (dma_mapping_error(cp->dev, mapping)) { 6568c2ecf20Sopenharmony_ci /* 6578c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 6588c2ecf20Sopenharmony_ci * delay and try again later or 6598c2ecf20Sopenharmony_ci * reset driver. 6608c2ecf20Sopenharmony_ci */ 6618c2ecf20Sopenharmony_ci goto map_error_handling; 6628c2ecf20Sopenharmony_ci } 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci cp->rx_buf = buffer; 6658c2ecf20Sopenharmony_ci cp->rx_len = len; 6668c2ecf20Sopenharmony_ci cp->rx_dma = mapping; 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci give_rx_buf_to_card(cp); 6698c2ecf20Sopenharmony_ci } 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci ... 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci my_card_interrupt_handler(int irq, void *devid, struct pt_regs *regs) 6748c2ecf20Sopenharmony_ci { 6758c2ecf20Sopenharmony_ci struct my_card *cp = devid; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci ... 6788c2ecf20Sopenharmony_ci if (read_card_status(cp) == RX_BUF_TRANSFERRED) { 6798c2ecf20Sopenharmony_ci struct my_card_header *hp; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci /* Examine the header to see if we wish 6828c2ecf20Sopenharmony_ci * to accept the data. But synchronize 6838c2ecf20Sopenharmony_ci * the DMA transfer with the CPU first 6848c2ecf20Sopenharmony_ci * so that we see updated contents. 6858c2ecf20Sopenharmony_ci */ 6868c2ecf20Sopenharmony_ci dma_sync_single_for_cpu(&cp->dev, cp->rx_dma, 6878c2ecf20Sopenharmony_ci cp->rx_len, 6888c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci /* Now it is safe to examine the buffer. */ 6918c2ecf20Sopenharmony_ci hp = (struct my_card_header *) cp->rx_buf; 6928c2ecf20Sopenharmony_ci if (header_is_ok(hp)) { 6938c2ecf20Sopenharmony_ci dma_unmap_single(&cp->dev, cp->rx_dma, cp->rx_len, 6948c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 6958c2ecf20Sopenharmony_ci pass_to_upper_layers(cp->rx_buf); 6968c2ecf20Sopenharmony_ci make_and_setup_new_rx_buf(cp); 6978c2ecf20Sopenharmony_ci } else { 6988c2ecf20Sopenharmony_ci /* CPU should not write to 6998c2ecf20Sopenharmony_ci * DMA_FROM_DEVICE-mapped area, 7008c2ecf20Sopenharmony_ci * so dma_sync_single_for_device() is 7018c2ecf20Sopenharmony_ci * not needed here. It would be required 7028c2ecf20Sopenharmony_ci * for DMA_BIDIRECTIONAL mapping if 7038c2ecf20Sopenharmony_ci * the memory was modified. 7048c2ecf20Sopenharmony_ci */ 7058c2ecf20Sopenharmony_ci give_rx_buf_to_card(cp); 7068c2ecf20Sopenharmony_ci } 7078c2ecf20Sopenharmony_ci } 7088c2ecf20Sopenharmony_ci } 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ciDrivers converted fully to this interface should not use virt_to_bus() any 7118c2ecf20Sopenharmony_cilonger, nor should they use bus_to_virt(). Some drivers have to be changed a 7128c2ecf20Sopenharmony_cilittle bit, because there is no longer an equivalent to bus_to_virt() in the 7138c2ecf20Sopenharmony_cidynamic DMA mapping scheme - you have to always store the DMA addresses 7148c2ecf20Sopenharmony_cireturned by the dma_alloc_coherent(), dma_pool_alloc(), and dma_map_single() 7158c2ecf20Sopenharmony_cicalls (dma_map_sg() stores them in the scatterlist itself if the platform 7168c2ecf20Sopenharmony_cisupports dynamic DMA mapping in hardware) in your driver structures and/or 7178c2ecf20Sopenharmony_ciin the card registers. 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ciAll drivers should be using these interfaces with no exceptions. It 7208c2ecf20Sopenharmony_ciis planned to completely remove virt_to_bus() and bus_to_virt() as 7218c2ecf20Sopenharmony_cithey are entirely deprecated. Some ports already do not provide these 7228c2ecf20Sopenharmony_cias it is impossible to correctly support them. 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ciHandling Errors 7258c2ecf20Sopenharmony_ci=============== 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ciDMA address space is limited on some architectures and an allocation 7288c2ecf20Sopenharmony_cifailure can be determined by: 7298c2ecf20Sopenharmony_ci 7308c2ecf20Sopenharmony_ci- checking if dma_alloc_coherent() returns NULL or dma_map_sg returns 0 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci- checking the dma_addr_t returned from dma_map_single() and dma_map_page() 7338c2ecf20Sopenharmony_ci by using dma_mapping_error():: 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ci dma_addr_t dma_handle; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci dma_handle = dma_map_single(dev, addr, size, direction); 7388c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_handle)) { 7398c2ecf20Sopenharmony_ci /* 7408c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 7418c2ecf20Sopenharmony_ci * delay and try again later or 7428c2ecf20Sopenharmony_ci * reset driver. 7438c2ecf20Sopenharmony_ci */ 7448c2ecf20Sopenharmony_ci goto map_error_handling; 7458c2ecf20Sopenharmony_ci } 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_ci- unmap pages that are already mapped, when mapping error occurs in the middle 7488c2ecf20Sopenharmony_ci of a multiple page mapping attempt. These example are applicable to 7498c2ecf20Sopenharmony_ci dma_map_page() as well. 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ciExample 1:: 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci dma_addr_t dma_handle1; 7548c2ecf20Sopenharmony_ci dma_addr_t dma_handle2; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci dma_handle1 = dma_map_single(dev, addr, size, direction); 7578c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_handle1)) { 7588c2ecf20Sopenharmony_ci /* 7598c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 7608c2ecf20Sopenharmony_ci * delay and try again later or 7618c2ecf20Sopenharmony_ci * reset driver. 7628c2ecf20Sopenharmony_ci */ 7638c2ecf20Sopenharmony_ci goto map_error_handling1; 7648c2ecf20Sopenharmony_ci } 7658c2ecf20Sopenharmony_ci dma_handle2 = dma_map_single(dev, addr, size, direction); 7668c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_handle2)) { 7678c2ecf20Sopenharmony_ci /* 7688c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 7698c2ecf20Sopenharmony_ci * delay and try again later or 7708c2ecf20Sopenharmony_ci * reset driver. 7718c2ecf20Sopenharmony_ci */ 7728c2ecf20Sopenharmony_ci goto map_error_handling2; 7738c2ecf20Sopenharmony_ci } 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_ci ... 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci map_error_handling2: 7788c2ecf20Sopenharmony_ci dma_unmap_single(dma_handle1); 7798c2ecf20Sopenharmony_ci map_error_handling1: 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ciExample 2:: 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci /* 7848c2ecf20Sopenharmony_ci * if buffers are allocated in a loop, unmap all mapped buffers when 7858c2ecf20Sopenharmony_ci * mapping error is detected in the middle 7868c2ecf20Sopenharmony_ci */ 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci dma_addr_t dma_addr; 7898c2ecf20Sopenharmony_ci dma_addr_t array[DMA_BUFFERS]; 7908c2ecf20Sopenharmony_ci int save_index = 0; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci for (i = 0; i < DMA_BUFFERS; i++) { 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci ... 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci dma_addr = dma_map_single(dev, addr, size, direction); 7978c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_addr)) { 7988c2ecf20Sopenharmony_ci /* 7998c2ecf20Sopenharmony_ci * reduce current DMA mapping usage, 8008c2ecf20Sopenharmony_ci * delay and try again later or 8018c2ecf20Sopenharmony_ci * reset driver. 8028c2ecf20Sopenharmony_ci */ 8038c2ecf20Sopenharmony_ci goto map_error_handling; 8048c2ecf20Sopenharmony_ci } 8058c2ecf20Sopenharmony_ci array[i].dma_addr = dma_addr; 8068c2ecf20Sopenharmony_ci save_index++; 8078c2ecf20Sopenharmony_ci } 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci ... 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci map_error_handling: 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci for (i = 0; i < save_index; i++) { 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci ... 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci dma_unmap_single(array[i].dma_addr); 8188c2ecf20Sopenharmony_ci } 8198c2ecf20Sopenharmony_ci 8208c2ecf20Sopenharmony_ciNetworking drivers must call dev_kfree_skb() to free the socket buffer 8218c2ecf20Sopenharmony_ciand return NETDEV_TX_OK if the DMA mapping fails on the transmit hook 8228c2ecf20Sopenharmony_ci(ndo_start_xmit). This means that the socket buffer is just dropped in 8238c2ecf20Sopenharmony_cithe failure case. 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ciSCSI drivers must return SCSI_MLQUEUE_HOST_BUSY if the DMA mapping 8268c2ecf20Sopenharmony_cifails in the queuecommand hook. This means that the SCSI subsystem 8278c2ecf20Sopenharmony_cipasses the command to the driver again later. 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ciOptimizing Unmap State Space Consumption 8308c2ecf20Sopenharmony_ci======================================== 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ciOn many platforms, dma_unmap_{single,page}() is simply a nop. 8338c2ecf20Sopenharmony_ciTherefore, keeping track of the mapping address and length is a waste 8348c2ecf20Sopenharmony_ciof space. Instead of filling your drivers up with ifdefs and the like 8358c2ecf20Sopenharmony_cito "work around" this (which would defeat the whole purpose of a 8368c2ecf20Sopenharmony_ciportable API) the following facilities are provided. 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ciActually, instead of describing the macros one by one, we'll 8398c2ecf20Sopenharmony_citransform some example code. 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci1) Use DEFINE_DMA_UNMAP_{ADDR,LEN} in state saving structures. 8428c2ecf20Sopenharmony_ci Example, before:: 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci struct ring_state { 8458c2ecf20Sopenharmony_ci struct sk_buff *skb; 8468c2ecf20Sopenharmony_ci dma_addr_t mapping; 8478c2ecf20Sopenharmony_ci __u32 len; 8488c2ecf20Sopenharmony_ci }; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci after:: 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci struct ring_state { 8538c2ecf20Sopenharmony_ci struct sk_buff *skb; 8548c2ecf20Sopenharmony_ci DEFINE_DMA_UNMAP_ADDR(mapping); 8558c2ecf20Sopenharmony_ci DEFINE_DMA_UNMAP_LEN(len); 8568c2ecf20Sopenharmony_ci }; 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ci2) Use dma_unmap_{addr,len}_set() to set these values. 8598c2ecf20Sopenharmony_ci Example, before:: 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci ringp->mapping = FOO; 8628c2ecf20Sopenharmony_ci ringp->len = BAR; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci after:: 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci dma_unmap_addr_set(ringp, mapping, FOO); 8678c2ecf20Sopenharmony_ci dma_unmap_len_set(ringp, len, BAR); 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci3) Use dma_unmap_{addr,len}() to access these values. 8708c2ecf20Sopenharmony_ci Example, before:: 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci dma_unmap_single(dev, ringp->mapping, ringp->len, 8738c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci after:: 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci dma_unmap_single(dev, 8788c2ecf20Sopenharmony_ci dma_unmap_addr(ringp, mapping), 8798c2ecf20Sopenharmony_ci dma_unmap_len(ringp, len), 8808c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_ciIt really should be self-explanatory. We treat the ADDR and LEN 8838c2ecf20Sopenharmony_ciseparately, because it is possible for an implementation to only 8848c2ecf20Sopenharmony_cineed the address in order to perform the unmap operation. 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ciPlatform Issues 8878c2ecf20Sopenharmony_ci=============== 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ciIf you are just writing drivers for Linux and do not maintain 8908c2ecf20Sopenharmony_cian architecture port for the kernel, you can safely skip down 8918c2ecf20Sopenharmony_cito "Closing". 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci1) Struct scatterlist requirements. 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci You need to enable CONFIG_NEED_SG_DMA_LENGTH if the architecture 8968c2ecf20Sopenharmony_ci supports IOMMUs (including software IOMMU). 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci2) ARCH_DMA_MINALIGN 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci Architectures must ensure that kmalloc'ed buffer is 9018c2ecf20Sopenharmony_ci DMA-safe. Drivers and subsystems depend on it. If an architecture 9028c2ecf20Sopenharmony_ci isn't fully DMA-coherent (i.e. hardware doesn't ensure that data in 9038c2ecf20Sopenharmony_ci the CPU cache is identical to data in main memory), 9048c2ecf20Sopenharmony_ci ARCH_DMA_MINALIGN must be set so that the memory allocator 9058c2ecf20Sopenharmony_ci makes sure that kmalloc'ed buffer doesn't share a cache line with 9068c2ecf20Sopenharmony_ci the others. See arch/arm/include/asm/cache.h as an example. 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_ci Note that ARCH_DMA_MINALIGN is about DMA memory alignment 9098c2ecf20Sopenharmony_ci constraints. You don't need to worry about the architecture data 9108c2ecf20Sopenharmony_ci alignment constraints (e.g. the alignment constraints about 64-bit 9118c2ecf20Sopenharmony_ci objects). 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ciClosing 9148c2ecf20Sopenharmony_ci======= 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_ciThis document, and the API itself, would not be in its current 9178c2ecf20Sopenharmony_ciform without the feedback and suggestions from numerous individuals. 9188c2ecf20Sopenharmony_ciWe would like to specifically mention, in no particular order, the 9198c2ecf20Sopenharmony_cifollowing people:: 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci Russell King <rmk@arm.linux.org.uk> 9228c2ecf20Sopenharmony_ci Leo Dagum <dagum@barrel.engr.sgi.com> 9238c2ecf20Sopenharmony_ci Ralf Baechle <ralf@oss.sgi.com> 9248c2ecf20Sopenharmony_ci Grant Grundler <grundler@cup.hp.com> 9258c2ecf20Sopenharmony_ci Jay Estabrook <Jay.Estabrook@compaq.com> 9268c2ecf20Sopenharmony_ci Thomas Sailer <sailer@ife.ee.ethz.ch> 9278c2ecf20Sopenharmony_ci Andrea Arcangeli <andrea@suse.de> 9288c2ecf20Sopenharmony_ci Jens Axboe <jens.axboe@oracle.com> 9298c2ecf20Sopenharmony_ci David Mosberger-Tang <davidm@hpl.hp.com> 930