blob: 0fc5728ed487e9735e7293e345d6949057ee3a00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001 Dynamic DMA mapping using the generic device
2 ============================================
3
4 James E.J. Bottomley <James.Bottomley@HansenPartnership.com>
5
6This document describes the DMA API. For a more gentle introduction
7phrased in terms of the pci_ equivalents (and actual examples) see
Randy Dunlap5872fb92009-01-29 16:28:02 -08008Documentation/PCI/PCI-DMA-mapping.txt.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10This API is split into two pieces. Part I describes the API and the
11corresponding pci_ API. Part II describes the extensions to the API
12for supporting non-consistent memory machines. Unless you know that
13your driver absolutely has to support non-consistent platforms (this
14is usually only legacy platforms) you should only use the API
15described in part I.
16
17Part I - pci_ and dma_ Equivalent API
18-------------------------------------
19
20To get the pci_ API, you must #include <linux/pci.h>
21To get the dma_ API, you must #include <linux/dma-mapping.h>
22
23
24Part Ia - Using large dma-coherent buffers
25------------------------------------------
26
27void *
28dma_alloc_coherent(struct device *dev, size_t size,
Randy Dunlapa12e2c62007-07-31 00:38:17 -070029 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030void *
31pci_alloc_consistent(struct pci_dev *dev, size_t size,
32 dma_addr_t *dma_handle)
33
34Consistent memory is memory for which a write by either the device or
35the processor can immediately be read by the processor or device
David Brownell21440d32006-04-01 10:21:52 -080036without having to worry about caching effects. (You may however need
37to make sure to flush the processor's write buffers before telling
38devices to read that memory.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40This routine allocates a region of <size> bytes of consistent memory.
Randy Dunlapa12e2c62007-07-31 00:38:17 -070041It also returns a <dma_handle> which may be cast to an unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -070042integer the same width as the bus and used as the physical address
43base of the region.
44
45Returns: a pointer to the allocated region (in the processor's virtual
46address space) or NULL if the allocation failed.
47
48Note: consistent memory can be expensive on some platforms, and the
49minimum allocation length may be as big as a page, so you should
50consolidate your requests for consistent memory as much as possible.
51The simplest way to do that is to use the dma_pool calls (see below).
52
53The flag parameter (dma_alloc_coherent only) allows the caller to
54specify the GFP_ flags (see kmalloc) for the allocation (the
Randy Dunlapa12e2c62007-07-31 00:38:17 -070055implementation may choose to ignore flags that affect the location of
Linus Torvalds1da177e2005-04-16 15:20:36 -070056the returned memory, like GFP_DMA). For pci_alloc_consistent, you
57must assume GFP_ATOMIC behaviour.
58
59void
Randy Dunlapa12e2c62007-07-31 00:38:17 -070060dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 dma_addr_t dma_handle)
62void
Randy Dunlapa12e2c62007-07-31 00:38:17 -070063pci_free_consistent(struct pci_dev *dev, size_t size, void *cpu_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 dma_addr_t dma_handle)
65
66Free the region of consistent memory you previously allocated. dev,
67size and dma_handle must all be the same as those passed into the
68consistent allocate. cpu_addr must be the virtual address returned by
Randy Dunlapa12e2c62007-07-31 00:38:17 -070069the consistent allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Brownellaa248862007-08-10 13:10:27 -070071Note that unlike their sibling allocation calls, these routines
72may only be called with IRQs enabled.
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75Part Ib - Using small dma-coherent buffers
76------------------------------------------
77
78To get this part of the dma_ API, you must #include <linux/dmapool.h>
79
80Many drivers need lots of small dma-coherent memory regions for DMA
81descriptors or I/O buffers. Rather than allocating in units of a page
82or more using dma_alloc_coherent(), you can use DMA pools. These work
Randy Dunlapa12e2c62007-07-31 00:38:17 -070083much like a struct kmem_cache, except that they use the dma-coherent allocator,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084not __get_free_pages(). Also, they understand common hardware constraints
Randy Dunlapa12e2c62007-07-31 00:38:17 -070085for alignment, like queue heads needing to be aligned on N-byte boundaries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88 struct dma_pool *
89 dma_pool_create(const char *name, struct device *dev,
90 size_t size, size_t align, size_t alloc);
91
92 struct pci_pool *
93 pci_pool_create(const char *name, struct pci_device *dev,
94 size_t size, size_t align, size_t alloc);
95
96The pool create() routines initialize a pool of dma-coherent buffers
97for use with a given device. It must be called in a context which
98can sleep.
99
Christoph Lametere18b8902006-12-06 20:33:20 -0800100The "name" is for diagnostics (like a struct kmem_cache name); dev and size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101are like what you'd pass to dma_alloc_coherent(). The device's hardware
102alignment requirement for this type of data is "align" (which is expressed
103in bytes, and must be a power of two). If your device has no boundary
104crossing restrictions, pass 0 for alloc; passing 4096 says memory allocated
105from this pool must not cross 4KByte boundaries.
106
107
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700108 void *dma_pool_alloc(struct dma_pool *pool, gfp_t gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 dma_addr_t *dma_handle);
110
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700111 void *pci_pool_alloc(struct pci_pool *pool, gfp_t gfp_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dma_addr_t *dma_handle);
113
114This allocates memory from the pool; the returned memory will meet the size
115and alignment requirements specified at creation time. Pass GFP_ATOMIC to
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700116prevent blocking, or if it's permitted (not in_interrupt, not holding SMP locks),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117pass GFP_KERNEL to allow blocking. Like dma_alloc_coherent(), this returns
118two values: an address usable by the cpu, and the dma address usable by the
119pool's device.
120
121
122 void dma_pool_free(struct dma_pool *pool, void *vaddr,
123 dma_addr_t addr);
124
125 void pci_pool_free(struct pci_pool *pool, void *vaddr,
126 dma_addr_t addr);
127
128This puts memory back into the pool. The pool is what was passed to
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700129the pool allocation routine; the cpu (vaddr) and dma addresses are what
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130were returned when that routine allocated the memory being freed.
131
132
133 void dma_pool_destroy(struct dma_pool *pool);
134
135 void pci_pool_destroy(struct pci_pool *pool);
136
137The pool destroy() routines free the resources of the pool. They must be
138called in a context which can sleep. Make sure you've freed all allocated
139memory back to the pool before you destroy it.
140
141
142Part Ic - DMA addressing limitations
143------------------------------------
144
145int
146dma_supported(struct device *dev, u64 mask)
147int
Randy Dunlap02d15c42008-04-29 01:00:35 -0700148pci_dma_supported(struct pci_dev *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150Checks to see if the device can support DMA to the memory described by
151mask.
152
153Returns: 1 if it can and 0 if it can't.
154
155Notes: This routine merely tests to see if the mask is possible. It
156won't change the current mask settings. It is more intended as an
157internal API for use by the platform than an external API for use by
158driver writers.
159
160int
161dma_set_mask(struct device *dev, u64 mask)
162int
163pci_set_dma_mask(struct pci_device *dev, u64 mask)
164
165Checks to see if the mask is possible and updates the device
166parameters if it is.
167
168Returns: 0 if successful and a negative error if not.
169
FUJITA Tomonori6a1961f2010-03-10 15:23:39 -0800170int
171dma_set_coherent_mask(struct device *dev, u64 mask)
172int
173pci_set_consistent_dma_mask(struct pci_device *dev, u64 mask)
174
175Checks to see if the mask is possible and updates the device
176parameters if it is.
177
178Returns: 0 if successful and a negative error if not.
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180u64
181dma_get_required_mask(struct device *dev)
182
John Keller175add12008-11-24 16:47:17 -0600183This API returns the mask that the platform requires to
184operate efficiently. Usually this means the returned mask
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185is the minimum required to cover all of memory. Examining the
186required mask gives drivers with variable descriptor sizes the
187opportunity to use smaller descriptors as necessary.
188
189Requesting the required mask does not alter the current mask. If you
John Keller175add12008-11-24 16:47:17 -0600190wish to take advantage of it, you should issue a dma_set_mask()
191call to set the mask to the value returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193
194Part Id - Streaming DMA mappings
195--------------------------------
196
197dma_addr_t
198dma_map_single(struct device *dev, void *cpu_addr, size_t size,
199 enum dma_data_direction direction)
200dma_addr_t
Randy Dunlap02d15c42008-04-29 01:00:35 -0700201pci_map_single(struct pci_dev *hwdev, void *cpu_addr, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int direction)
203
204Maps a piece of processor virtual memory so it can be accessed by the
205device and returns the physical handle of the memory.
206
207The direction for both api's may be converted freely by casting.
208However the dma_ API uses a strongly typed enumerator for its
209direction:
210
211DMA_NONE = PCI_DMA_NONE no direction (used for
212 debugging)
213DMA_TO_DEVICE = PCI_DMA_TODEVICE data is going from the
214 memory to the device
215DMA_FROM_DEVICE = PCI_DMA_FROMDEVICE data is coming from
216 the device to the
217 memory
218DMA_BIDIRECTIONAL = PCI_DMA_BIDIRECTIONAL direction isn't known
219
220Notes: Not all memory regions in a machine can be mapped by this
221API. Further, regions that appear to be physically contiguous in
222kernel virtual space may not be contiguous as physical memory. Since
223this API does not provide any scatter/gather capability, it will fail
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700224if the user tries to map a non-physically contiguous piece of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225For this reason, it is recommended that memory mapped by this API be
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700226obtained only from sources which guarantee it to be physically contiguous
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227(like kmalloc).
228
229Further, the physical address of the memory must be within the
230dma_mask of the device (the dma_mask represents a bit mask of the
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700231addressable region for the device. I.e., if the physical address of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232the memory anded with the dma_mask is still equal to the physical
233address, then the device can perform DMA to the memory). In order to
234ensure that the memory allocated by kmalloc is within the dma_mask,
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700235the driver may specify various platform-dependent flags to restrict
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236the physical memory range of the allocation (e.g. on x86, GFP_DMA
237guarantees to be within the first 16Mb of available physical memory,
238as required by ISA devices).
239
240Note also that the above constraints on physical contiguity and
241dma_mask may not apply if the platform has an IOMMU (a device which
242supplies a physical to virtual mapping between the I/O memory bus and
243the device). However, to be portable, device driver writers may *not*
244assume that such an IOMMU exists.
245
246Warnings: Memory coherency operates at a granularity called the cache
247line width. In order for memory mapped by this API to operate
248correctly, the mapped region must begin exactly on a cache line
249boundary and end exactly on one (to prevent two separately mapped
250regions from sharing a single cache line). Since the cache line size
251may not be known at compile time, the API will not enforce this
252requirement. Therefore, it is recommended that driver writers who
253don't take special care to determine the cache line size at run time
254only map virtual regions that begin and end on page boundaries (which
255are guaranteed also to be cache line boundaries).
256
257DMA_TO_DEVICE synchronisation must be done after the last modification
258of the memory region by the software and before it is handed off to
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700259the driver. Once this primitive is used, memory covered by this
260primitive should be treated as read-only by the device. If the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261may write to it at any point, it should be DMA_BIDIRECTIONAL (see
262below).
263
264DMA_FROM_DEVICE synchronisation must be done before the driver
265accesses data that may be changed by the device. This memory should
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700266be treated as read-only by the driver. If the driver needs to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267to it at any point, it should be DMA_BIDIRECTIONAL (see below).
268
269DMA_BIDIRECTIONAL requires special handling: it means that the driver
270isn't sure if the memory was modified before being handed off to the
271device and also isn't sure if the device will also modify it. Thus,
272you must always sync bidirectional memory twice: once before the
273memory is handed off to the device (to make sure all memory changes
274are flushed from the processor) and once before the data may be
275accessed after being used by the device (to make sure any processor
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700276cache lines are updated with data that the device may have changed).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278void
279dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
280 enum dma_data_direction direction)
281void
282pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr,
283 size_t size, int direction)
284
285Unmaps the region previously mapped. All the parameters passed in
286must be identical to those passed in (and returned) by the mapping
287API.
288
289dma_addr_t
290dma_map_page(struct device *dev, struct page *page,
291 unsigned long offset, size_t size,
292 enum dma_data_direction direction)
293dma_addr_t
294pci_map_page(struct pci_dev *hwdev, struct page *page,
295 unsigned long offset, size_t size, int direction)
296void
297dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
298 enum dma_data_direction direction)
299void
300pci_unmap_page(struct pci_dev *hwdev, dma_addr_t dma_address,
301 size_t size, int direction)
302
303API for mapping and unmapping for pages. All the notes and warnings
304for the other mapping APIs apply here. Also, although the <offset>
305and <size> parameters are provided to do partial page mapping, it is
306recommended that you never use these unless you really know what the
307cache width is.
308
309int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700310dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700313pci_dma_mapping_error(struct pci_dev *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315In some circumstances dma_map_single and dma_map_page will fail to create
316a mapping. A driver can check for these errors by testing the returned
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700317dma address with dma_mapping_error(). A non-zero return value means the mapping
318could not be created and the driver should take appropriate action (e.g.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319reduce current DMA mapping usage or delay and try again later).
320
David Brownell21440d32006-04-01 10:21:52 -0800321 int
322 dma_map_sg(struct device *dev, struct scatterlist *sg,
323 int nents, enum dma_data_direction direction)
324 int
325 pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg,
326 int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700328Returns: the number of physical segments mapped (this may be shorter
FUJITA Tomonori1d678f32008-12-01 13:14:01 -0800329than <nents> passed in if some elements of the scatter/gather list are
330physically or virtually adjacent and an IOMMU maps them with a single
331entry).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333Please note that the sg cannot be mapped again if it has been mapped once.
334The mapping process is allowed to destroy information in the sg.
335
336As with the other mapping interfaces, dma_map_sg can fail. When it
337does, 0 is returned and a driver must take appropriate action. It is
338critical that the driver do something, in the case of a block driver
339aborting the request or even oopsing is better than doing nothing and
340corrupting the filesystem.
341
David Brownell21440d32006-04-01 10:21:52 -0800342With scatterlists, you use the resulting mapping like this:
343
344 int i, count = dma_map_sg(dev, sglist, nents, direction);
345 struct scatterlist *sg;
346
FUJITA Tomonori79eb0142008-09-18 09:35:28 -0700347 for_each_sg(sglist, sg, count, i) {
David Brownell21440d32006-04-01 10:21:52 -0800348 hw_address[i] = sg_dma_address(sg);
349 hw_len[i] = sg_dma_len(sg);
350 }
351
352where nents is the number of entries in the sglist.
353
354The implementation is free to merge several consecutive sglist entries
355into one (e.g. with an IOMMU, or if several pages just happen to be
356physically contiguous) and returns the actual number of sg entries it
357mapped them to. On failure 0, is returned.
358
359Then you should loop count times (note: this can be less than nents times)
360and use sg_dma_address() and sg_dma_len() macros where you previously
361accessed sg->address and sg->length as shown above.
362
363 void
364 dma_unmap_sg(struct device *dev, struct scatterlist *sg,
365 int nhwentries, enum dma_data_direction direction)
366 void
367 pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg,
368 int nents, int direction)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700370Unmap the previously mapped scatter/gather list. All the parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371must be the same as those and passed in to the scatter/gather mapping
372API.
373
374Note: <nents> must be the number you passed in, *not* the number of
375physical entries returned.
376
FUJITA Tomonori9705ef72010-03-10 15:23:17 -0800377void
378dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
379 enum dma_data_direction direction)
380void
381pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t dma_handle,
382 size_t size, int direction)
383void
384dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, size_t size,
385 enum dma_data_direction direction)
386void
387pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t dma_handle,
388 size_t size, int direction)
389void
390dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
391 enum dma_data_direction direction)
392void
393pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg,
394 int nelems, int direction)
395void
396dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
397 enum dma_data_direction direction)
398void
399pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg,
400 int nelems, int direction)
401
402Synchronise a single contiguous or scatter/gather mapping for the cpu
403and device. With the sync_sg API, all the parameters must be the same
404as those passed into the single mapping API. With the sync_single API,
405you can use dma_handle and size parameters that aren't identical to
406those passed into the single mapping API to do a partial sync.
407
408Notes: You must do this:
409
410- Before reading values that have been written by DMA from the device
411 (use the DMA_FROM_DEVICE direction)
412- After writing values that will be written to the device using DMA
413 (use the DMA_TO_DEVICE) direction
414- before *and* after handing memory to the device if the memory is
415 DMA_BIDIRECTIONAL
416
417See also dma_map_single().
418
Arthur Kepnera75b0a22008-04-29 01:00:31 -0700419dma_addr_t
420dma_map_single_attrs(struct device *dev, void *cpu_addr, size_t size,
421 enum dma_data_direction dir,
422 struct dma_attrs *attrs)
423
424void
425dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr,
426 size_t size, enum dma_data_direction dir,
427 struct dma_attrs *attrs)
428
429int
430dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
431 int nents, enum dma_data_direction dir,
432 struct dma_attrs *attrs)
433
434void
435dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
436 int nents, enum dma_data_direction dir,
437 struct dma_attrs *attrs)
438
439The four functions above are just like the counterpart functions
440without the _attrs suffixes, except that they pass an optional
441struct dma_attrs*.
442
443struct dma_attrs encapsulates a set of "dma attributes". For the
444definition of struct dma_attrs see linux/dma-attrs.h.
445
446The interpretation of dma attributes is architecture-specific, and
447each attribute should be documented in Documentation/DMA-attributes.txt.
448
449If struct dma_attrs* is NULL, the semantics of each of these
450functions is identical to those of the corresponding function
451without the _attrs suffix. As a result dma_map_single_attrs()
452can generally replace dma_map_single(), etc.
453
454As an example of the use of the *_attrs functions, here's how
455you could pass an attribute DMA_ATTR_FOO when mapping memory
456for DMA:
457
458#include <linux/dma-attrs.h>
459/* DMA_ATTR_FOO should be defined in linux/dma-attrs.h and
460 * documented in Documentation/DMA-attributes.txt */
461...
462
463 DEFINE_DMA_ATTRS(attrs);
464 dma_set_attr(DMA_ATTR_FOO, &attrs);
465 ....
466 n = dma_map_sg_attrs(dev, sg, nents, DMA_TO_DEVICE, &attr);
467 ....
468
469Architectures that care about DMA_ATTR_FOO would check for its
470presence in their implementations of the mapping and unmapping
471routines, e.g.:
472
473void whizco_dma_map_sg_attrs(struct device *dev, dma_addr_t dma_addr,
474 size_t size, enum dma_data_direction dir,
475 struct dma_attrs *attrs)
476{
477 ....
478 int foo = dma_get_attr(DMA_ATTR_FOO, attrs);
479 ....
480 if (foo)
481 /* twizzle the frobnozzle */
482 ....
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
FUJITA Tomonori0acedc12010-03-10 15:23:31 -0800485Part Ie - Optimizing Unmap State Space Consumption
486--------------------------------
487
488On some platforms, dma_unmap_{single,page}() is simply a nop.
489Therefore, keeping track of the mapping address and length is a waste
490of space. Instead of filling your drivers up with ifdefs and the like
491to "work around" this (which would defeat the whole purpose of a
492portable API) the following facilities are provided.
493
494Actually, instead of describing the macros one by one, we'll
495transform some example code.
496
4971) Use DEFINE_DMA_UNMAP_{ADDR,LEN} in state saving structures.
498 Example, before:
499
500 struct ring_state {
501 struct sk_buff *skb;
502 dma_addr_t mapping;
503 __u32 len;
504 };
505
506 after:
507
508 struct ring_state {
509 struct sk_buff *skb;
510 DEFINE_DMA_UNMAP_ADDR(mapping);
511 DEFINE_DMA_UNMAP_LEN(len);
512 };
513
5142) Use dma_unmap_{addr,len}_set to set these values.
515 Example, before:
516
517 ringp->mapping = FOO;
518 ringp->len = BAR;
519
520 after:
521
522 dma_unmap_addr_set(ringp, mapping, FOO);
523 dma_unmap_len_set(ringp, len, BAR);
524
5253) Use dma_unmap_{addr,len} to access these values.
526 Example, before:
527
528 dma_unmap_single(dev, ringp->mapping, ringp->len,
529 DMA_FROM_DEVICE);
530
531 after:
532
533 dma_unmap_single(dev,
534 dma_unmap_addr(ringp, mapping),
535 dma_unmap_len(ringp, len),
536 DMA_FROM_DEVICE);
537
538It really should be self-explanatory. We treat the ADDR and LEN
539separately, because it is possible for an implementation to only
540need the address in order to perform the unmap operation.
541
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543Part II - Advanced dma_ usage
544-----------------------------
545
546Warning: These pieces of the DMA API have no PCI equivalent. They
547should also not be used in the majority of cases, since they cater for
548unlikely corner cases that don't belong in usual drivers.
549
550If you don't understand how cache line coherency works between a
551processor and an I/O device, you should not be using this part of the
552API at all.
553
554void *
555dma_alloc_noncoherent(struct device *dev, size_t size,
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700556 dma_addr_t *dma_handle, gfp_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558Identical to dma_alloc_coherent() except that the platform will
559choose to return either consistent or non-consistent memory as it sees
560fit. By using this API, you are guaranteeing to the platform that you
561have all the correct and necessary sync points for this memory in the
562driver should it choose to return non-consistent memory.
563
564Note: where the platform can return consistent memory, it will
565guarantee that the sync points become nops.
566
567Warning: Handling non-consistent memory is a real pain. You should
568only ever use this API if you positively know your driver will be
569required to work on one of the rare (usually non-PCI) architectures
570that simply cannot make consistent memory.
571
572void
573dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr,
574 dma_addr_t dma_handle)
575
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700576Free memory allocated by the nonconsistent API. All parameters must
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577be identical to those passed in (and returned by
578dma_alloc_noncoherent()).
579
580int
Ralf Baechlef67637e2006-12-06 20:38:54 -0800581dma_is_consistent(struct device *dev, dma_addr_t dma_handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700583Returns true if the device dev is performing consistent DMA on the memory
Ralf Baechlef67637e2006-12-06 20:38:54 -0800584area pointed to by the dma_handle.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586int
587dma_get_cache_alignment(void)
588
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700589Returns the processor cache alignment. This is the absolute minimum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590alignment *and* width that you must observe when either mapping
591memory or doing partial flushes.
592
593Notes: This API may return a number *larger* than the actual cache
594line, but it will guarantee that one or more cache lines fit exactly
595into the width returned by this call. It will also always be a power
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700596of two for easy alignment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598void
Ralf Baechled3fa72e2006-12-06 20:38:56 -0800599dma_cache_sync(struct device *dev, void *vaddr, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 enum dma_data_direction direction)
601
602Do a partial sync of memory that was allocated by
603dma_alloc_noncoherent(), starting at virtual address vaddr and
604continuing on for size. Again, you *must* observe the cache line
605boundaries when doing this.
606
607int
608dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
609 dma_addr_t device_addr, size_t size, int
610 flags)
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612Declare region of memory to be handed out by dma_alloc_coherent when
613it's asked for coherent memory for this device.
614
615bus_addr is the physical address to which the memory is currently
616assigned in the bus responding region (this will be used by the
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700617platform to perform the mapping).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619device_addr is the physical address the device needs to be programmed
620with actually to address this memory (this will be handed out as the
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700621dma_addr_t in dma_alloc_coherent()).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623size is the size of the area (must be multiples of PAGE_SIZE).
624
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700625flags can be or'd together and are:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627DMA_MEMORY_MAP - request that the memory returned from
Matt LaPlante4ae0edc2006-11-30 04:58:40 +0100628dma_alloc_coherent() be directly writable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630DMA_MEMORY_IO - request that the memory returned from
631dma_alloc_coherent() be addressable using read/write/memcpy_toio etc.
632
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700633One or both of these flags must be present.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635DMA_MEMORY_INCLUDES_CHILDREN - make the declared memory be allocated by
636dma_alloc_coherent of any child devices of this one (for memory residing
637on a bridge).
638
639DMA_MEMORY_EXCLUSIVE - only allocate memory from the declared regions.
640Do not allow dma_alloc_coherent() to fall back to system memory when
641it's out of memory in the declared region.
642
643The return value will be either DMA_MEMORY_MAP or DMA_MEMORY_IO and
644must correspond to a passed in flag (i.e. no returning DMA_MEMORY_IO
645if only DMA_MEMORY_MAP were passed in) for success or zero for
646failure.
647
648Note, for DMA_MEMORY_IO returns, all subsequent memory returned by
649dma_alloc_coherent() may no longer be accessed directly, but instead
650must be accessed using the correct bus functions. If your driver
651isn't prepared to handle this contingency, it should not specify
652DMA_MEMORY_IO in the input flags.
653
654As a simplification for the platforms, only *one* such region of
655memory may be declared per device.
656
657For reasons of efficiency, most platforms choose to track the declared
658region only at the granularity of a page. For smaller allocations,
659you should use the dma_pool() API.
660
661void
662dma_release_declared_memory(struct device *dev)
663
664Remove the memory region previously declared from the system. This
665API performs *no* in-use checking for this region and will return
666unconditionally having removed all the required structures. It is the
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700667driver's job to ensure that no parts of this memory region are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668currently in use.
669
670void *
671dma_mark_declared_memory_occupied(struct device *dev,
672 dma_addr_t device_addr, size_t size)
673
674This is used to occupy specific regions of the declared space
675(dma_alloc_coherent() will hand out the first free region it finds).
676
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700677device_addr is the *device* address of the region requested.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Randy Dunlapa12e2c62007-07-31 00:38:17 -0700679size is the size (and should be a page-sized multiple).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681The return value will be either a pointer to the processor virtual
682address of the memory, or an error (via PTR_ERR()) if any part of the
683region is occupied.
Joerg Roedel187f9c32009-01-09 16:28:07 +0100684
685Part III - Debug drivers use of the DMA-API
686-------------------------------------------
687
688The DMA-API as described above as some constraints. DMA addresses must be
689released with the corresponding function with the same size for example. With
690the advent of hardware IOMMUs it becomes more and more important that drivers
691do not violate those constraints. In the worst case such a violation can
692result in data corruption up to destroyed filesystems.
693
694To debug drivers and find bugs in the usage of the DMA-API checking code can
695be compiled into the kernel which will tell the developer about those
696violations. If your architecture supports it you can select the "Enable
697debugging of DMA-API usage" option in your kernel configuration. Enabling this
698option has a performance impact. Do not enable it in production kernels.
699
700If you boot the resulting kernel will contain code which does some bookkeeping
701about what DMA memory was allocated for which device. If this code detects an
702error it prints a warning message with some details into your kernel log. An
703example warning message may look like this:
704
705------------[ cut here ]------------
706WARNING: at /data2/repos/linux-2.6-iommu/lib/dma-debug.c:448
707 check_unmap+0x203/0x490()
708Hardware name:
709forcedeth 0000:00:08.0: DMA-API: device driver frees DMA memory with wrong
710 function [device address=0x00000000640444be] [size=66 bytes] [mapped as
711single] [unmapped as page]
712Modules linked in: nfsd exportfs bridge stp llc r8169
713Pid: 0, comm: swapper Tainted: G W 2.6.28-dmatest-09289-g8bb99c0 #1
714Call Trace:
715 <IRQ> [<ffffffff80240b22>] warn_slowpath+0xf2/0x130
716 [<ffffffff80647b70>] _spin_unlock+0x10/0x30
717 [<ffffffff80537e75>] usb_hcd_link_urb_to_ep+0x75/0xc0
718 [<ffffffff80647c22>] _spin_unlock_irqrestore+0x12/0x40
719 [<ffffffff8055347f>] ohci_urb_enqueue+0x19f/0x7c0
720 [<ffffffff80252f96>] queue_work+0x56/0x60
721 [<ffffffff80237e10>] enqueue_task_fair+0x20/0x50
722 [<ffffffff80539279>] usb_hcd_submit_urb+0x379/0xbc0
723 [<ffffffff803b78c3>] cpumask_next_and+0x23/0x40
724 [<ffffffff80235177>] find_busiest_group+0x207/0x8a0
725 [<ffffffff8064784f>] _spin_lock_irqsave+0x1f/0x50
726 [<ffffffff803c7ea3>] check_unmap+0x203/0x490
727 [<ffffffff803c8259>] debug_dma_unmap_page+0x49/0x50
728 [<ffffffff80485f26>] nv_tx_done_optimized+0xc6/0x2c0
729 [<ffffffff80486c13>] nv_nic_irq_optimized+0x73/0x2b0
730 [<ffffffff8026df84>] handle_IRQ_event+0x34/0x70
731 [<ffffffff8026ffe9>] handle_edge_irq+0xc9/0x150
732 [<ffffffff8020e3ab>] do_IRQ+0xcb/0x1c0
733 [<ffffffff8020c093>] ret_from_intr+0x0/0xa
734 <EOI> <4>---[ end trace f6435a98e2a38c0e ]---
735
736The driver developer can find the driver and the device including a stacktrace
737of the DMA-API call which caused this warning.
738
739Per default only the first error will result in a warning message. All other
740errors will only silently counted. This limitation exist to prevent the code
741from flooding your kernel log. To support debugging a device driver this can
742be disabled via debugfs. See the debugfs interface documentation below for
743details.
744
745The debugfs directory for the DMA-API debugging code is called dma-api/. In
746this directory the following files can currently be found:
747
748 dma-api/all_errors This file contains a numeric value. If this
749 value is not equal to zero the debugging code
750 will print a warning for every error it finds
Matt LaPlante19f59462009-04-27 15:06:31 +0200751 into the kernel log. Be careful with this
752 option, as it can easily flood your logs.
Joerg Roedel187f9c32009-01-09 16:28:07 +0100753
754 dma-api/disabled This read-only file contains the character 'Y'
755 if the debugging code is disabled. This can
756 happen when it runs out of memory or if it was
757 disabled at boot time
758
759 dma-api/error_count This file is read-only and shows the total
760 numbers of errors found.
761
762 dma-api/num_errors The number in this file shows how many
763 warnings will be printed to the kernel log
764 before it stops. This number is initialized to
765 one at system boot and be set by writing into
766 this file
767
768 dma-api/min_free_entries
769 This read-only file can be read to get the
770 minimum number of free dma_debug_entries the
771 allocator has ever seen. If this value goes
772 down to zero the code will disable itself
773 because it is not longer reliable.
774
775 dma-api/num_free_entries
776 The current number of free dma_debug_entries
777 in the allocator.
778
Joerg Roedel016ea682009-05-22 21:57:23 +0200779 dma-api/driver-filter
780 You can write a name of a driver into this file
781 to limit the debug output to requests from that
782 particular driver. Write an empty string to
783 that file to disable the filter and see
784 all errors again.
785
Joerg Roedel187f9c32009-01-09 16:28:07 +0100786If you have this code compiled into your kernel it will be enabled by default.
787If you want to boot without the bookkeeping anyway you can provide
788'dma_debug=off' as a boot parameter. This will disable DMA-API debugging.
789Notice that you can not enable it again at runtime. You have to reboot to do
790so.
791
Joerg Roedel016ea682009-05-22 21:57:23 +0200792If you want to see debug messages only for a special device driver you can
793specify the dma_debug_driver=<drivername> parameter. This will enable the
794driver filter at boot time. The debug code will only print errors for that
795driver afterwards. This filter can be disabled or changed later using debugfs.
796
Joerg Roedel187f9c32009-01-09 16:28:07 +0100797When the code disables itself at runtime this is most likely because it ran
798out of dma_debug_entries. These entries are preallocated at boot. The number
799of preallocated entries is defined per architecture. If it is too low for you
800boot with 'dma_debug_entries=<your_desired_number>' to overwrite the
801architectural default.