diff options
author | Bjorn Helgaas <bhelgaas@google.com> | 2014-04-30 13:20:53 -0400 |
---|---|---|
committer | Bjorn Helgaas <bhelgaas@google.com> | 2014-05-20 18:54:21 -0400 |
commit | 77f2ea2f8d0833f9e976368481fb9a0775acf9e7 (patch) | |
tree | 007a1fb1dcfa6f9cb0baa26548af80b90148fabd /Documentation/DMA-API.txt | |
parent | c9eaa447e77efe77b7fa4c953bd62de8297fd6c5 (diff) |
DMA-API: Clarify physical/bus address distinction
The DMA-API documentation sometimes refers to "physical addresses" when it
really means "bus addresses." Sometimes these are identical, but they may
be different if the bridge leading to the bus performs address translation.
Update the documentation to use "bus address" when appropriate.
Also, consistently capitalize "DMA", use parens with function names, use
dev_printk() in examples, and reword a few sections for clarity.
No functional change; documentation changes only.
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: James Bottomley <jbottomley@Parallels.com>
Acked-by: Randy Dunlap <rdunlap@infradead.org>
Diffstat (limited to 'Documentation/DMA-API.txt')
-rw-r--r-- | Documentation/DMA-API.txt | 139 |
1 files changed, 72 insertions, 67 deletions
diff --git a/Documentation/DMA-API.txt b/Documentation/DMA-API.txt index e865279cec58..1147eba43128 100644 --- a/Documentation/DMA-API.txt +++ b/Documentation/DMA-API.txt | |||
@@ -4,22 +4,26 @@ | |||
4 | James E.J. Bottomley <James.Bottomley@HansenPartnership.com> | 4 | James E.J. Bottomley <James.Bottomley@HansenPartnership.com> |
5 | 5 | ||
6 | This document describes the DMA API. For a more gentle introduction | 6 | This document describes the DMA API. For a more gentle introduction |
7 | of the API (and actual examples) see | 7 | of the API (and actual examples), see Documentation/DMA-API-HOWTO.txt. |
8 | Documentation/DMA-API-HOWTO.txt. | ||
9 | 8 | ||
10 | This API is split into two pieces. Part I describes the API. Part II | 9 | This API is split into two pieces. Part I describes the basic API. |
11 | describes the extensions to the API for supporting non-consistent | 10 | Part II describes extensions for supporting non-consistent memory |
12 | memory machines. Unless you know that your driver absolutely has to | 11 | machines. Unless you know that your driver absolutely has to support |
13 | support non-consistent platforms (this is usually only legacy | 12 | non-consistent platforms (this is usually only legacy platforms) you |
14 | platforms) you should only use the API described in part I. | 13 | should only use the API described in part I. |
15 | 14 | ||
16 | Part I - dma_ API | 15 | Part I - dma_ API |
17 | ------------------------------------- | 16 | ------------------------------------- |
18 | 17 | ||
19 | To get the dma_ API, you must #include <linux/dma-mapping.h> | 18 | To get the dma_ API, you must #include <linux/dma-mapping.h>. This |
19 | provides dma_addr_t and the interfaces described below. | ||
20 | 20 | ||
21 | A dma_addr_t can hold any valid DMA or bus address for the platform. It | ||
22 | can be given to a device to use as a DMA source or target. A cpu cannot | ||
23 | reference a dma_addr_t directly because there may be translation between | ||
24 | its physical address space and the bus address space. | ||
21 | 25 | ||
22 | Part Ia - Using large dma-coherent buffers | 26 | Part Ia - Using large DMA-coherent buffers |
23 | ------------------------------------------ | 27 | ------------------------------------------ |
24 | 28 | ||
25 | void * | 29 | void * |
@@ -33,20 +37,21 @@ to make sure to flush the processor's write buffers before telling | |||
33 | devices to read that memory.) | 37 | devices to read that memory.) |
34 | 38 | ||
35 | This routine allocates a region of <size> bytes of consistent memory. | 39 | This routine allocates a region of <size> bytes of consistent memory. |
36 | It also returns a <dma_handle> which may be cast to an unsigned | ||
37 | integer the same width as the bus and used as the physical address | ||
38 | base of the region. | ||
39 | 40 | ||
40 | Returns: a pointer to the allocated region (in the processor's virtual | 41 | It returns a pointer to the allocated region (in the processor's virtual |
41 | address space) or NULL if the allocation failed. | 42 | address space) or NULL if the allocation failed. |
42 | 43 | ||
44 | It also returns a <dma_handle> which may be cast to an unsigned integer the | ||
45 | same width as the bus and given to the device as the bus address base of | ||
46 | the region. | ||
47 | |||
43 | Note: consistent memory can be expensive on some platforms, and the | 48 | Note: consistent memory can be expensive on some platforms, and the |
44 | minimum allocation length may be as big as a page, so you should | 49 | minimum allocation length may be as big as a page, so you should |
45 | consolidate your requests for consistent memory as much as possible. | 50 | consolidate your requests for consistent memory as much as possible. |
46 | The simplest way to do that is to use the dma_pool calls (see below). | 51 | The simplest way to do that is to use the dma_pool calls (see below). |
47 | 52 | ||
48 | The flag parameter (dma_alloc_coherent only) allows the caller to | 53 | The flag parameter (dma_alloc_coherent() only) allows the caller to |
49 | specify the GFP_ flags (see kmalloc) for the allocation (the | 54 | specify the GFP_ flags (see kmalloc()) for the allocation (the |
50 | implementation may choose to ignore flags that affect the location of | 55 | implementation may choose to ignore flags that affect the location of |
51 | the returned memory, like GFP_DMA). | 56 | the returned memory, like GFP_DMA). |
52 | 57 | ||
@@ -61,24 +66,24 @@ void | |||
61 | dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, | 66 | dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, |
62 | dma_addr_t dma_handle) | 67 | dma_addr_t dma_handle) |
63 | 68 | ||
64 | Free the region of consistent memory you previously allocated. dev, | 69 | Free a region of consistent memory you previously allocated. dev, |
65 | size and dma_handle must all be the same as those passed into the | 70 | size and dma_handle must all be the same as those passed into |
66 | consistent allocate. cpu_addr must be the virtual address returned by | 71 | dma_alloc_coherent(). cpu_addr must be the virtual address returned by |
67 | the consistent allocate. | 72 | the dma_alloc_coherent(). |
68 | 73 | ||
69 | Note that unlike their sibling allocation calls, these routines | 74 | Note that unlike their sibling allocation calls, these routines |
70 | may only be called with IRQs enabled. | 75 | may only be called with IRQs enabled. |
71 | 76 | ||
72 | 77 | ||
73 | Part Ib - Using small dma-coherent buffers | 78 | Part Ib - Using small DMA-coherent buffers |
74 | ------------------------------------------ | 79 | ------------------------------------------ |
75 | 80 | ||
76 | To get this part of the dma_ API, you must #include <linux/dmapool.h> | 81 | To get this part of the dma_ API, you must #include <linux/dmapool.h> |
77 | 82 | ||
78 | Many drivers need lots of small dma-coherent memory regions for DMA | 83 | Many drivers need lots of small DMA-coherent memory regions for DMA |
79 | descriptors or I/O buffers. Rather than allocating in units of a page | 84 | descriptors or I/O buffers. Rather than allocating in units of a page |
80 | or more using dma_alloc_coherent(), you can use DMA pools. These work | 85 | or more using dma_alloc_coherent(), you can use DMA pools. These work |
81 | much like a struct kmem_cache, except that they use the dma-coherent allocator, | 86 | much like a struct kmem_cache, except that they use the DMA-coherent allocator, |
82 | not __get_free_pages(). Also, they understand common hardware constraints | 87 | not __get_free_pages(). Also, they understand common hardware constraints |
83 | for alignment, like queue heads needing to be aligned on N-byte boundaries. | 88 | for alignment, like queue heads needing to be aligned on N-byte boundaries. |
84 | 89 | ||
@@ -87,7 +92,7 @@ for alignment, like queue heads needing to be aligned on N-byte boundaries. | |||
87 | dma_pool_create(const char *name, struct device *dev, | 92 | dma_pool_create(const char *name, struct device *dev, |
88 | size_t size, size_t align, size_t alloc); | 93 | size_t size, size_t align, size_t alloc); |
89 | 94 | ||
90 | The pool create() routines initialize a pool of dma-coherent buffers | 95 | dma_pool_create() initializes a pool of DMA-coherent buffers |
91 | for use with a given device. It must be called in a context which | 96 | for use with a given device. It must be called in a context which |
92 | can sleep. | 97 | can sleep. |
93 | 98 | ||
@@ -102,25 +107,26 @@ from this pool must not cross 4KByte boundaries. | |||
102 | void *dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags, | 107 | void *dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags, |
103 | dma_addr_t *dma_handle); | 108 | dma_addr_t *dma_handle); |
104 | 109 | ||
105 | This allocates memory from the pool; the returned memory will meet the size | 110 | This allocates memory from the pool; the returned memory will meet the |
106 | and alignment requirements specified at creation time. Pass GFP_ATOMIC to | 111 | size and alignment requirements specified at creation time. Pass |
107 | prevent blocking, or if it's permitted (not in_interrupt, not holding SMP locks), | 112 | GFP_ATOMIC to prevent blocking, or if it's permitted (not |
108 | pass GFP_KERNEL to allow blocking. Like dma_alloc_coherent(), this returns | 113 | in_interrupt, not holding SMP locks), pass GFP_KERNEL to allow |
109 | two values: an address usable by the cpu, and the dma address usable by the | 114 | blocking. Like dma_alloc_coherent(), this returns two values: an |
110 | pool's device. | 115 | address usable by the cpu, and the DMA address usable by the pool's |
116 | device. | ||
111 | 117 | ||
112 | 118 | ||
113 | void dma_pool_free(struct dma_pool *pool, void *vaddr, | 119 | void dma_pool_free(struct dma_pool *pool, void *vaddr, |
114 | dma_addr_t addr); | 120 | dma_addr_t addr); |
115 | 121 | ||
116 | This puts memory back into the pool. The pool is what was passed to | 122 | This puts memory back into the pool. The pool is what was passed to |
117 | the pool allocation routine; the cpu (vaddr) and dma addresses are what | 123 | dma_pool_alloc(); the cpu (vaddr) and DMA addresses are what |
118 | were returned when that routine allocated the memory being freed. | 124 | were returned when that routine allocated the memory being freed. |
119 | 125 | ||
120 | 126 | ||
121 | void dma_pool_destroy(struct dma_pool *pool); | 127 | void dma_pool_destroy(struct dma_pool *pool); |
122 | 128 | ||
123 | The pool destroy() routines free the resources of the pool. They must be | 129 | dma_pool_destroy() frees the resources of the pool. It must be |
124 | called in a context which can sleep. Make sure you've freed all allocated | 130 | called in a context which can sleep. Make sure you've freed all allocated |
125 | memory back to the pool before you destroy it. | 131 | memory back to the pool before you destroy it. |
126 | 132 | ||
@@ -187,9 +193,9 @@ dma_map_single(struct device *dev, void *cpu_addr, size_t size, | |||
187 | enum dma_data_direction direction) | 193 | enum dma_data_direction direction) |
188 | 194 | ||
189 | Maps a piece of processor virtual memory so it can be accessed by the | 195 | Maps a piece of processor virtual memory so it can be accessed by the |
190 | device and returns the physical handle of the memory. | 196 | device and returns the bus address of the memory. |
191 | 197 | ||
192 | The direction for both api's may be converted freely by casting. | 198 | The direction for both APIs may be converted freely by casting. |
193 | However the dma_ API uses a strongly typed enumerator for its | 199 | However the dma_ API uses a strongly typed enumerator for its |
194 | direction: | 200 | direction: |
195 | 201 | ||
@@ -198,31 +204,30 @@ DMA_TO_DEVICE data is going from the memory to the device | |||
198 | DMA_FROM_DEVICE data is coming from the device to the memory | 204 | DMA_FROM_DEVICE data is coming from the device to the memory |
199 | DMA_BIDIRECTIONAL direction isn't known | 205 | DMA_BIDIRECTIONAL direction isn't known |
200 | 206 | ||
201 | Notes: Not all memory regions in a machine can be mapped by this | 207 | Notes: Not all memory regions in a machine can be mapped by this API. |
202 | API. Further, regions that appear to be physically contiguous in | 208 | Further, contiguous kernel virtual space may not be contiguous as |
203 | kernel virtual space may not be contiguous as physical memory. Since | 209 | physical memory. Since this API does not provide any scatter/gather |
204 | this API does not provide any scatter/gather capability, it will fail | 210 | capability, it will fail if the user tries to map a non-physically |
205 | if the user tries to map a non-physically contiguous piece of memory. | 211 | contiguous piece of memory. For this reason, memory to be mapped by |
206 | For this reason, it is recommended that memory mapped by this API be | 212 | this API should be obtained from sources which guarantee it to be |
207 | obtained only from sources which guarantee it to be physically contiguous | 213 | physically contiguous (like kmalloc). |
208 | (like kmalloc). | 214 | |
209 | 215 | Further, the bus address of the memory must be within the | |
210 | Further, the physical address of the memory must be within the | 216 | dma_mask of the device (the dma_mask is a bit mask of the |
211 | dma_mask of the device (the dma_mask represents a bit mask of the | 217 | addressable region for the device, i.e., if the bus address of |
212 | addressable region for the device. I.e., if the physical address of | 218 | the memory ANDed with the dma_mask is still equal to the bus |
213 | the memory anded with the dma_mask is still equal to the physical | 219 | address, then the device can perform DMA to the memory). To |
214 | address, then the device can perform DMA to the memory). In order to | ||
215 | ensure that the memory allocated by kmalloc is within the dma_mask, | 220 | ensure that the memory allocated by kmalloc is within the dma_mask, |
216 | the driver may specify various platform-dependent flags to restrict | 221 | the driver may specify various platform-dependent flags to restrict |
217 | the physical memory range of the allocation (e.g. on x86, GFP_DMA | 222 | the bus address range of the allocation (e.g., on x86, GFP_DMA |
218 | guarantees to be within the first 16Mb of available physical memory, | 223 | guarantees to be within the first 16MB of available bus addresses, |
219 | as required by ISA devices). | 224 | as required by ISA devices). |
220 | 225 | ||
221 | Note also that the above constraints on physical contiguity and | 226 | Note also that the above constraints on physical contiguity and |
222 | dma_mask may not apply if the platform has an IOMMU (a device which | 227 | dma_mask may not apply if the platform has an IOMMU (a device which |
223 | supplies a physical to virtual mapping between the I/O memory bus and | 228 | maps an I/O bus address to a physical memory address). However, to be |
224 | the device). However, to be portable, device driver writers may *not* | 229 | portable, device driver writers may *not* assume that such an IOMMU |
225 | assume that such an IOMMU exists. | 230 | exists. |
226 | 231 | ||
227 | Warnings: Memory coherency operates at a granularity called the cache | 232 | Warnings: Memory coherency operates at a granularity called the cache |
228 | line width. In order for memory mapped by this API to operate | 233 | line width. In order for memory mapped by this API to operate |
@@ -281,9 +286,9 @@ cache width is. | |||
281 | int | 286 | int |
282 | dma_mapping_error(struct device *dev, dma_addr_t dma_addr) | 287 | dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
283 | 288 | ||
284 | In some circumstances dma_map_single and dma_map_page will fail to create | 289 | In some circumstances dma_map_single() and dma_map_page() will fail to create |
285 | a mapping. A driver can check for these errors by testing the returned | 290 | a mapping. A driver can check for these errors by testing the returned |
286 | dma address with dma_mapping_error(). A non-zero return value means the mapping | 291 | DMA address with dma_mapping_error(). A non-zero return value means the mapping |
287 | could not be created and the driver should take appropriate action (e.g. | 292 | could not be created and the driver should take appropriate action (e.g. |
288 | reduce current DMA mapping usage or delay and try again later). | 293 | reduce current DMA mapping usage or delay and try again later). |
289 | 294 | ||
@@ -291,7 +296,7 @@ reduce current DMA mapping usage or delay and try again later). | |||
291 | dma_map_sg(struct device *dev, struct scatterlist *sg, | 296 | dma_map_sg(struct device *dev, struct scatterlist *sg, |
292 | int nents, enum dma_data_direction direction) | 297 | int nents, enum dma_data_direction direction) |
293 | 298 | ||
294 | Returns: the number of physical segments mapped (this may be shorter | 299 | Returns: the number of bus address segments mapped (this may be shorter |
295 | than <nents> passed in if some elements of the scatter/gather list are | 300 | than <nents> passed in if some elements of the scatter/gather list are |
296 | physically or virtually adjacent and an IOMMU maps them with a single | 301 | physically or virtually adjacent and an IOMMU maps them with a single |
297 | entry). | 302 | entry). |
@@ -299,7 +304,7 @@ entry). | |||
299 | Please note that the sg cannot be mapped again if it has been mapped once. | 304 | Please note that the sg cannot be mapped again if it has been mapped once. |
300 | The mapping process is allowed to destroy information in the sg. | 305 | The mapping process is allowed to destroy information in the sg. |
301 | 306 | ||
302 | As with the other mapping interfaces, dma_map_sg can fail. When it | 307 | As with the other mapping interfaces, dma_map_sg() can fail. When it |
303 | does, 0 is returned and a driver must take appropriate action. It is | 308 | does, 0 is returned and a driver must take appropriate action. It is |
304 | critical that the driver do something, in the case of a block driver | 309 | critical that the driver do something, in the case of a block driver |
305 | aborting the request or even oopsing is better than doing nothing and | 310 | aborting the request or even oopsing is better than doing nothing and |
@@ -335,7 +340,7 @@ must be the same as those and passed in to the scatter/gather mapping | |||
335 | API. | 340 | API. |
336 | 341 | ||
337 | Note: <nents> must be the number you passed in, *not* the number of | 342 | Note: <nents> must be the number you passed in, *not* the number of |
338 | physical entries returned. | 343 | bus address entries returned. |
339 | 344 | ||
340 | void | 345 | void |
341 | dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, | 346 | dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size, |
@@ -391,10 +396,10 @@ The four functions above are just like the counterpart functions | |||
391 | without the _attrs suffixes, except that they pass an optional | 396 | without the _attrs suffixes, except that they pass an optional |
392 | struct dma_attrs*. | 397 | struct dma_attrs*. |
393 | 398 | ||
394 | struct dma_attrs encapsulates a set of "dma attributes". For the | 399 | struct dma_attrs encapsulates a set of "DMA attributes". For the |
395 | definition of struct dma_attrs see linux/dma-attrs.h. | 400 | definition of struct dma_attrs see linux/dma-attrs.h. |
396 | 401 | ||
397 | The interpretation of dma attributes is architecture-specific, and | 402 | The interpretation of DMA attributes is architecture-specific, and |
398 | each attribute should be documented in Documentation/DMA-attributes.txt. | 403 | each attribute should be documented in Documentation/DMA-attributes.txt. |
399 | 404 | ||
400 | If struct dma_attrs* is NULL, the semantics of each of these | 405 | If struct dma_attrs* is NULL, the semantics of each of these |
@@ -458,7 +463,7 @@ Note: where the platform can return consistent memory, it will | |||
458 | guarantee that the sync points become nops. | 463 | guarantee that the sync points become nops. |
459 | 464 | ||
460 | Warning: Handling non-consistent memory is a real pain. You should | 465 | Warning: Handling non-consistent memory is a real pain. You should |
461 | only ever use this API if you positively know your driver will be | 466 | only use this API if you positively know your driver will be |
462 | required to work on one of the rare (usually non-PCI) architectures | 467 | required to work on one of the rare (usually non-PCI) architectures |
463 | that simply cannot make consistent memory. | 468 | that simply cannot make consistent memory. |
464 | 469 | ||
@@ -496,26 +501,26 @@ dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr, | |||
496 | dma_addr_t device_addr, size_t size, int | 501 | dma_addr_t device_addr, size_t size, int |
497 | flags) | 502 | flags) |
498 | 503 | ||
499 | Declare region of memory to be handed out by dma_alloc_coherent when | 504 | Declare region of memory to be handed out by dma_alloc_coherent() when |
500 | it's asked for coherent memory for this device. | 505 | it's asked for coherent memory for this device. |
501 | 506 | ||
502 | bus_addr is the physical address to which the memory is currently | 507 | bus_addr is the physical address to which the memory is currently |
503 | assigned in the bus responding region (this will be used by the | 508 | assigned in the bus responding region (this will be used by the |
504 | platform to perform the mapping). | 509 | platform to perform the mapping). |
505 | 510 | ||
506 | device_addr is the physical address the device needs to be programmed | 511 | device_addr is the bus address the device needs to be programmed |
507 | with actually to address this memory (this will be handed out as the | 512 | with actually to address this memory (this will be handed out as the |
508 | dma_addr_t in dma_alloc_coherent()). | 513 | dma_addr_t in dma_alloc_coherent()). |
509 | 514 | ||
510 | size is the size of the area (must be multiples of PAGE_SIZE). | 515 | size is the size of the area (must be multiples of PAGE_SIZE). |
511 | 516 | ||
512 | flags can be or'd together and are: | 517 | flags can be ORed together and are: |
513 | 518 | ||
514 | DMA_MEMORY_MAP - request that the memory returned from | 519 | DMA_MEMORY_MAP - request that the memory returned from |
515 | dma_alloc_coherent() be directly writable. | 520 | dma_alloc_coherent() be directly writable. |
516 | 521 | ||
517 | DMA_MEMORY_IO - request that the memory returned from | 522 | DMA_MEMORY_IO - request that the memory returned from |
518 | dma_alloc_coherent() be addressable using read/write/memcpy_toio etc. | 523 | dma_alloc_coherent() be addressable using read()/write()/memcpy_toio() etc. |
519 | 524 | ||
520 | One or both of these flags must be present. | 525 | One or both of these flags must be present. |
521 | 526 | ||
@@ -572,7 +577,7 @@ region is occupied. | |||
572 | Part III - Debug drivers use of the DMA-API | 577 | Part III - Debug drivers use of the DMA-API |
573 | ------------------------------------------- | 578 | ------------------------------------------- |
574 | 579 | ||
575 | The DMA-API as described above as some constraints. DMA addresses must be | 580 | The DMA-API as described above has some constraints. DMA addresses must be |
576 | released with the corresponding function with the same size for example. With | 581 | released with the corresponding function with the same size for example. With |
577 | the advent of hardware IOMMUs it becomes more and more important that drivers | 582 | the advent of hardware IOMMUs it becomes more and more important that drivers |
578 | do not violate those constraints. In the worst case such a violation can | 583 | do not violate those constraints. In the worst case such a violation can |
@@ -690,11 +695,11 @@ architectural default. | |||
690 | void debug_dmap_mapping_error(struct device *dev, dma_addr_t dma_addr); | 695 | void debug_dmap_mapping_error(struct device *dev, dma_addr_t dma_addr); |
691 | 696 | ||
692 | dma-debug interface debug_dma_mapping_error() to debug drivers that fail | 697 | dma-debug interface debug_dma_mapping_error() to debug drivers that fail |
693 | to check dma mapping errors on addresses returned by dma_map_single() and | 698 | to check DMA mapping errors on addresses returned by dma_map_single() and |
694 | dma_map_page() interfaces. This interface clears a flag set by | 699 | dma_map_page() interfaces. This interface clears a flag set by |
695 | debug_dma_map_page() to indicate that dma_mapping_error() has been called by | 700 | debug_dma_map_page() to indicate that dma_mapping_error() has been called by |
696 | the driver. When driver does unmap, debug_dma_unmap() checks the flag and if | 701 | the driver. When driver does unmap, debug_dma_unmap() checks the flag and if |
697 | this flag is still set, prints warning message that includes call trace that | 702 | this flag is still set, prints warning message that includes call trace that |
698 | leads up to the unmap. This interface can be called from dma_mapping_error() | 703 | leads up to the unmap. This interface can be called from dma_mapping_error() |
699 | routines to enable dma mapping error check debugging. | 704 | routines to enable DMA mapping error check debugging. |
700 | 705 | ||