diff options
Diffstat (limited to 'Documentation/DMA-API-HOWTO.txt')
| -rw-r--r-- | Documentation/DMA-API-HOWTO.txt | 758 |
1 files changed, 758 insertions, 0 deletions
diff --git a/Documentation/DMA-API-HOWTO.txt b/Documentation/DMA-API-HOWTO.txt new file mode 100644 index 000000000000..52618ab069ad --- /dev/null +++ b/Documentation/DMA-API-HOWTO.txt | |||
| @@ -0,0 +1,758 @@ | |||
| 1 | Dynamic DMA mapping Guide | ||
| 2 | ========================= | ||
| 3 | |||
| 4 | David S. Miller <davem@redhat.com> | ||
| 5 | Richard Henderson <rth@cygnus.com> | ||
| 6 | Jakub Jelinek <jakub@redhat.com> | ||
| 7 | |||
| 8 | This is a guide to device driver writers on how to use the DMA API | ||
| 9 | with example pseudo-code. For a concise description of the API, see | ||
| 10 | DMA-API.txt. | ||
| 11 | |||
| 12 | Most of the 64bit platforms have special hardware that translates bus | ||
| 13 | addresses (DMA addresses) into physical addresses. This is similar to | ||
| 14 | how page tables and/or a TLB translates virtual addresses to physical | ||
| 15 | addresses on a CPU. This is needed so that e.g. PCI devices can | ||
| 16 | access with a Single Address Cycle (32bit DMA address) any page in the | ||
| 17 | 64bit physical address space. Previously in Linux those 64bit | ||
| 18 | platforms had to set artificial limits on the maximum RAM size in the | ||
| 19 | system, so that the virt_to_bus() static scheme works (the DMA address | ||
| 20 | translation tables were simply filled on bootup to map each bus | ||
| 21 | address to the physical page __pa(bus_to_virt())). | ||
| 22 | |||
| 23 | So that Linux can use the dynamic DMA mapping, it needs some help from the | ||
| 24 | drivers, namely it has to take into account that DMA addresses should be | ||
| 25 | mapped only for the time they are actually used and unmapped after the DMA | ||
| 26 | transfer. | ||
| 27 | |||
| 28 | The following API will work of course even on platforms where no such | ||
| 29 | hardware exists. | ||
| 30 | |||
| 31 | Note that the DMA API works with any bus independent of the underlying | ||
| 32 | microprocessor architecture. You should use the DMA API rather than | ||
| 33 | the bus specific DMA API (e.g. pci_dma_*). | ||
| 34 | |||
| 35 | First of all, you should make sure | ||
| 36 | |||
| 37 | #include <linux/dma-mapping.h> | ||
| 38 | |||
| 39 | is in your driver. This file will obtain for you the definition of the | ||
| 40 | dma_addr_t (which can hold any valid DMA address for the platform) | ||
| 41 | type which should be used everywhere you hold a DMA (bus) address | ||
| 42 | returned from the DMA mapping functions. | ||
| 43 | |||
| 44 | What memory is DMA'able? | ||
| 45 | |||
| 46 | The first piece of information you must know is what kernel memory can | ||
| 47 | be used with the DMA mapping facilities. There has been an unwritten | ||
| 48 | set of rules regarding this, and this text is an attempt to finally | ||
| 49 | write them down. | ||
| 50 | |||
| 51 | If you acquired your memory via the page allocator | ||
| 52 | (i.e. __get_free_page*()) or the generic memory allocators | ||
| 53 | (i.e. kmalloc() or kmem_cache_alloc()) then you may DMA to/from | ||
| 54 | that memory using the addresses returned from those routines. | ||
| 55 | |||
| 56 | This means specifically that you may _not_ use the memory/addresses | ||
| 57 | returned from vmalloc() for DMA. It is possible to DMA to the | ||
| 58 | _underlying_ memory mapped into a vmalloc() area, but this requires | ||
| 59 | walking page tables to get the physical addresses, and then | ||
| 60 | translating each of those pages back to a kernel address using | ||
| 61 | something like __va(). [ EDIT: Update this when we integrate | ||
| 62 | Gerd Knorr's generic code which does this. ] | ||
| 63 | |||
| 64 | This rule also means that you may use neither kernel image addresses | ||
| 65 | (items in data/text/bss segments), nor module image addresses, nor | ||
| 66 | stack addresses for DMA. These could all be mapped somewhere entirely | ||
| 67 | different than the rest of physical memory. Even if those classes of | ||
| 68 | memory could physically work with DMA, you'd need to ensure the I/O | ||
| 69 | buffers were cacheline-aligned. Without that, you'd see cacheline | ||
| 70 | sharing problems (data corruption) on CPUs with DMA-incoherent caches. | ||
| 71 | (The CPU could write to one word, DMA would write to a different one | ||
| 72 | in the same cache line, and one of them could be overwritten.) | ||
| 73 | |||
| 74 | Also, this means that you cannot take the return of a kmap() | ||
| 75 | call and DMA to/from that. This is similar to vmalloc(). | ||
| 76 | |||
| 77 | What about block I/O and networking buffers? The block I/O and | ||
| 78 | networking subsystems make sure that the buffers they use are valid | ||
| 79 | for you to DMA from/to. | ||
| 80 | |||
| 81 | DMA addressing limitations | ||
| 82 | |||
| 83 | Does your device have any DMA addressing limitations? For example, is | ||
| 84 | your device only capable of driving the low order 24-bits of address? | ||
| 85 | If so, you need to inform the kernel of this fact. | ||
| 86 | |||
| 87 | By default, the kernel assumes that your device can address the full | ||
| 88 | 32-bits. For a 64-bit capable device, this needs to be increased. | ||
| 89 | And for a device with limitations, as discussed in the previous | ||
| 90 | paragraph, it needs to be decreased. | ||
| 91 | |||
| 92 | Special note about PCI: PCI-X specification requires PCI-X devices to | ||
| 93 | support 64-bit addressing (DAC) for all transactions. And at least | ||
| 94 | one platform (SGI SN2) requires 64-bit consistent allocations to | ||
| 95 | operate correctly when the IO bus is in PCI-X mode. | ||
| 96 | |||
| 97 | For correct operation, you must interrogate the kernel in your device | ||
| 98 | probe routine to see if the DMA controller on the machine can properly | ||
| 99 | support the DMA addressing limitation your device has. It is good | ||
| 100 | style to do this even if your device holds the default setting, | ||
| 101 | because this shows that you did think about these issues wrt. your | ||
| 102 | device. | ||
| 103 | |||
| 104 | The query is performed via a call to dma_set_mask(): | ||
| 105 | |||
| 106 | int dma_set_mask(struct device *dev, u64 mask); | ||
| 107 | |||
| 108 | The query for consistent allocations is performed via a call to | ||
| 109 | dma_set_coherent_mask(): | ||
| 110 | |||
| 111 | int dma_set_coherent_mask(struct device *dev, u64 mask); | ||
| 112 | |||
| 113 | Here, dev is a pointer to the device struct of your device, and mask | ||
| 114 | is a bit mask describing which bits of an address your device | ||
| 115 | supports. It returns zero if your card can perform DMA properly on | ||
| 116 | the machine given the address mask you provided. In general, the | ||
| 117 | device struct of your device is embedded in the bus specific device | ||
| 118 | struct of your device. For example, a pointer to the device struct of | ||
| 119 | your PCI device is pdev->dev (pdev is a pointer to the PCI device | ||
| 120 | struct of your device). | ||
| 121 | |||
| 122 | If it returns non-zero, your device cannot perform DMA properly on | ||
| 123 | this platform, and attempting to do so will result in undefined | ||
| 124 | behavior. You must either use a different mask, or not use DMA. | ||
| 125 | |||
| 126 | This means that in the failure case, you have three options: | ||
| 127 | |||
| 128 | 1) Use another DMA mask, if possible (see below). | ||
| 129 | 2) Use some non-DMA mode for data transfer, if possible. | ||
| 130 | 3) Ignore this device and do not initialize it. | ||
| 131 | |||
| 132 | It is recommended that your driver print a kernel KERN_WARNING message | ||
| 133 | when you end up performing either #2 or #3. In this manner, if a user | ||
| 134 | of your driver reports that performance is bad or that the device is not | ||
| 135 | even detected, you can ask them for the kernel messages to find out | ||
| 136 | exactly why. | ||
| 137 | |||
| 138 | The standard 32-bit addressing device would do something like this: | ||
| 139 | |||
| 140 | if (dma_set_mask(dev, DMA_BIT_MASK(32))) { | ||
| 141 | printk(KERN_WARNING | ||
| 142 | "mydev: No suitable DMA available.\n"); | ||
| 143 | goto ignore_this_device; | ||
| 144 | } | ||
| 145 | |||
| 146 | Another common scenario is a 64-bit capable device. The approach here | ||
| 147 | is to try for 64-bit addressing, but back down to a 32-bit mask that | ||
| 148 | should not fail. The kernel may fail the 64-bit mask not because the | ||
| 149 | platform is not capable of 64-bit addressing. Rather, it may fail in | ||
| 150 | this case simply because 32-bit addressing is done more efficiently | ||
| 151 | than 64-bit addressing. For example, Sparc64 PCI SAC addressing is | ||
| 152 | more efficient than DAC addressing. | ||
| 153 | |||
| 154 | Here is how you would handle a 64-bit capable device which can drive | ||
| 155 | all 64-bits when accessing streaming DMA: | ||
| 156 | |||
| 157 | int using_dac; | ||
| 158 | |||
| 159 | if (!dma_set_mask(dev, DMA_BIT_MASK(64))) { | ||
| 160 | using_dac = 1; | ||
| 161 | } else if (!dma_set_mask(dev, DMA_BIT_MASK(32))) { | ||
| 162 | using_dac = 0; | ||
| 163 | } else { | ||
| 164 | printk(KERN_WARNING | ||
| 165 | "mydev: No suitable DMA available.\n"); | ||
| 166 | goto ignore_this_device; | ||
| 167 | } | ||
| 168 | |||
| 169 | If a card is capable of using 64-bit consistent allocations as well, | ||
| 170 | the case would look like this: | ||
| 171 | |||
| 172 | int using_dac, consistent_using_dac; | ||
| 173 | |||
| 174 | if (!dma_set_mask(dev, DMA_BIT_MASK(64))) { | ||
| 175 | using_dac = 1; | ||
| 176 | consistent_using_dac = 1; | ||
| 177 | dma_set_coherent_mask(dev, DMA_BIT_MASK(64)); | ||
| 178 | } else if (!dma_set_mask(dev, DMA_BIT_MASK(32))) { | ||
| 179 | using_dac = 0; | ||
| 180 | consistent_using_dac = 0; | ||
| 181 | dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); | ||
| 182 | } else { | ||
| 183 | printk(KERN_WARNING | ||
| 184 | "mydev: No suitable DMA available.\n"); | ||
| 185 | goto ignore_this_device; | ||
| 186 | } | ||
| 187 | |||
| 188 | dma_set_coherent_mask() will always be able to set the same or a | ||
| 189 | smaller mask as dma_set_mask(). However for the rare case that a | ||
| 190 | device driver only uses consistent allocations, one would have to | ||
| 191 | check the return value from dma_set_coherent_mask(). | ||
| 192 | |||
| 193 | Finally, if your device can only drive the low 24-bits of | ||
| 194 | address you might do something like: | ||
| 195 | |||
| 196 | if (dma_set_mask(dev, DMA_BIT_MASK(24))) { | ||
| 197 | printk(KERN_WARNING | ||
| 198 | "mydev: 24-bit DMA addressing not available.\n"); | ||
| 199 | goto ignore_this_device; | ||
| 200 | } | ||
| 201 | |||
| 202 | When dma_set_mask() is successful, and returns zero, the kernel saves | ||
| 203 | away this mask you have provided. The kernel will use this | ||
| 204 | information later when you make DMA mappings. | ||
