162306a36Sopenharmony_ci=========================
262306a36Sopenharmony_ciDynamic DMA mapping Guide
362306a36Sopenharmony_ci=========================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Author: David S. Miller <davem@redhat.com>
662306a36Sopenharmony_ci:Author: Richard Henderson <rth@cygnus.com>
762306a36Sopenharmony_ci:Author: Jakub Jelinek <jakub@redhat.com>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciThis is a guide to device driver writers on how to use the DMA API
1062306a36Sopenharmony_ciwith example pseudo-code.  For a concise description of the API, see
1162306a36Sopenharmony_ciDMA-API.txt.
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciCPU and DMA addresses
1462306a36Sopenharmony_ci=====================
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciThere are several kinds of addresses involved in the DMA API, and it's
1762306a36Sopenharmony_ciimportant to understand the differences.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciThe kernel normally uses virtual addresses.  Any address returned by
2062306a36Sopenharmony_cikmalloc(), vmalloc(), and similar interfaces is a virtual address and can
2162306a36Sopenharmony_cibe stored in a ``void *``.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciThe virtual memory system (TLB, page tables, etc.) translates virtual
2462306a36Sopenharmony_ciaddresses to CPU physical addresses, which are stored as "phys_addr_t" or
2562306a36Sopenharmony_ci"resource_size_t".  The kernel manages device resources like registers as
2662306a36Sopenharmony_ciphysical addresses.  These are the addresses in /proc/iomem.  The physical
2762306a36Sopenharmony_ciaddress is not directly useful to a driver; it must use ioremap() to map
2862306a36Sopenharmony_cithe space and produce a virtual address.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciI/O devices use a third kind of address: a "bus address".  If a device has
3162306a36Sopenharmony_ciregisters at an MMIO address, or if it performs DMA to read or write system
3262306a36Sopenharmony_cimemory, the addresses used by the device are bus addresses.  In some
3362306a36Sopenharmony_cisystems, bus addresses are identical to CPU physical addresses, but in
3462306a36Sopenharmony_cigeneral they are not.  IOMMUs and host bridges can produce arbitrary
3562306a36Sopenharmony_cimappings between physical and bus addresses.
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciFrom a device's point of view, DMA uses the bus address space, but it may
3862306a36Sopenharmony_cibe restricted to a subset of that space.  For example, even if a system
3962306a36Sopenharmony_cisupports 64-bit addresses for main memory and PCI BARs, it may use an IOMMU
4062306a36Sopenharmony_ciso devices only need to use 32-bit DMA addresses.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciHere's a picture and some examples::
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci               CPU                  CPU                  Bus
4562306a36Sopenharmony_ci             Virtual              Physical             Address
4662306a36Sopenharmony_ci             Address              Address               Space
4762306a36Sopenharmony_ci              Space                Space
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci            +-------+             +------+             +------+
5062306a36Sopenharmony_ci            |       |             |MMIO  |   Offset    |      |
5162306a36Sopenharmony_ci            |       |  Virtual    |Space |   applied   |      |
5262306a36Sopenharmony_ci          C +-------+ --------> B +------+ ----------> +------+ A
5362306a36Sopenharmony_ci            |       |  mapping    |      |   by host   |      |
5462306a36Sopenharmony_ci  +-----+   |       |             |      |   bridge    |      |   +--------+
5562306a36Sopenharmony_ci  |     |   |       |             +------+             |      |   |        |
5662306a36Sopenharmony_ci  | CPU |   |       |             | RAM  |             |      |   | Device |
5762306a36Sopenharmony_ci  |     |   |       |             |      |             |      |   |        |
5862306a36Sopenharmony_ci  +-----+   +-------+             +------+             +------+   +--------+
5962306a36Sopenharmony_ci            |       |  Virtual    |Buffer|   Mapping   |      |
6062306a36Sopenharmony_ci          X +-------+ --------> Y +------+ <---------- +------+ Z
6162306a36Sopenharmony_ci            |       |  mapping    | RAM  |   by IOMMU
6262306a36Sopenharmony_ci            |       |             |      |
6362306a36Sopenharmony_ci            |       |             |      |
6462306a36Sopenharmony_ci            +-------+             +------+
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciDuring the enumeration process, the kernel learns about I/O devices and
6762306a36Sopenharmony_citheir MMIO space and the host bridges that connect them to the system.  For
6862306a36Sopenharmony_ciexample, if a PCI device has a BAR, the kernel reads the bus address (A)
6962306a36Sopenharmony_cifrom the BAR and converts it to a CPU physical address (B).  The address B
7062306a36Sopenharmony_ciis stored in a struct resource and usually exposed via /proc/iomem.  When a
7162306a36Sopenharmony_cidriver claims a device, it typically uses ioremap() to map physical address
7262306a36Sopenharmony_ciB at a virtual address (C).  It can then use, e.g., ioread32(C), to access
7362306a36Sopenharmony_cithe device registers at bus address A.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciIf the device supports DMA, the driver sets up a buffer using kmalloc() or
7662306a36Sopenharmony_cia similar interface, which returns a virtual address (X).  The virtual
7762306a36Sopenharmony_cimemory system maps X to a physical address (Y) in system RAM.  The driver
7862306a36Sopenharmony_cican use virtual address X to access the buffer, but the device itself
7962306a36Sopenharmony_cicannot because DMA doesn't go through the CPU virtual memory system.
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciIn some simple systems, the device can do DMA directly to physical address
8262306a36Sopenharmony_ciY.  But in many others, there is IOMMU hardware that translates DMA
8362306a36Sopenharmony_ciaddresses to physical addresses, e.g., it translates Z to Y.  This is part
8462306a36Sopenharmony_ciof the reason for the DMA API: the driver can give a virtual address X to
8562306a36Sopenharmony_cian interface like dma_map_single(), which sets up any required IOMMU
8662306a36Sopenharmony_cimapping and returns the DMA address Z.  The driver then tells the device to
8762306a36Sopenharmony_cido DMA to Z, and the IOMMU maps it to the buffer at address Y in system
8862306a36Sopenharmony_ciRAM.
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciSo that Linux can use the dynamic DMA mapping, it needs some help from the
9162306a36Sopenharmony_cidrivers, namely it has to take into account that DMA addresses should be
9262306a36Sopenharmony_cimapped only for the time they are actually used and unmapped after the DMA
9362306a36Sopenharmony_citransfer.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciThe following API will work of course even on platforms where no such
9662306a36Sopenharmony_cihardware exists.
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciNote that the DMA API works with any bus independent of the underlying
9962306a36Sopenharmony_cimicroprocessor architecture. You should use the DMA API rather than the
10062306a36Sopenharmony_cibus-specific DMA API, i.e., use the dma_map_*() interfaces rather than the
10162306a36Sopenharmony_cipci_map_*() interfaces.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciFirst of all, you should make sure::
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	#include <linux/dma-mapping.h>
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciis in your driver, which provides the definition of dma_addr_t.  This type
10862306a36Sopenharmony_cican hold any valid DMA address for the platform and should be used
10962306a36Sopenharmony_cieverywhere you hold a DMA address returned from the DMA mapping functions.
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciWhat memory is DMA'able?
11262306a36Sopenharmony_ci========================
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciThe first piece of information you must know is what kernel memory can
11562306a36Sopenharmony_cibe used with the DMA mapping facilities.  There has been an unwritten
11662306a36Sopenharmony_ciset of rules regarding this, and this text is an attempt to finally
11762306a36Sopenharmony_ciwrite them down.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciIf you acquired your memory via the page allocator
12062306a36Sopenharmony_ci(i.e. __get_free_page*()) or the generic memory allocators
12162306a36Sopenharmony_ci(i.e. kmalloc() or kmem_cache_alloc()) then you may DMA to/from
12262306a36Sopenharmony_cithat memory using the addresses returned from those routines.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciThis means specifically that you may _not_ use the memory/addresses
12562306a36Sopenharmony_cireturned from vmalloc() for DMA.  It is possible to DMA to the
12662306a36Sopenharmony_ci_underlying_ memory mapped into a vmalloc() area, but this requires
12762306a36Sopenharmony_ciwalking page tables to get the physical addresses, and then
12862306a36Sopenharmony_citranslating each of those pages back to a kernel address using
12962306a36Sopenharmony_cisomething like __va().  [ EDIT: Update this when we integrate
13062306a36Sopenharmony_ciGerd Knorr's generic code which does this. ]
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciThis rule also means that you may use neither kernel image addresses
13362306a36Sopenharmony_ci(items in data/text/bss segments), nor module image addresses, nor
13462306a36Sopenharmony_cistack addresses for DMA.  These could all be mapped somewhere entirely
13562306a36Sopenharmony_cidifferent than the rest of physical memory.  Even if those classes of
13662306a36Sopenharmony_cimemory could physically work with DMA, you'd need to ensure the I/O
13762306a36Sopenharmony_cibuffers were cacheline-aligned.  Without that, you'd see cacheline
13862306a36Sopenharmony_cisharing problems (data corruption) on CPUs with DMA-incoherent caches.
13962306a36Sopenharmony_ci(The CPU could write to one word, DMA would write to a different one
14062306a36Sopenharmony_ciin the same cache line, and one of them could be overwritten.)
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ciAlso, this means that you cannot take the return of a kmap()
14362306a36Sopenharmony_cicall and DMA to/from that.  This is similar to vmalloc().
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciWhat about block I/O and networking buffers?  The block I/O and
14662306a36Sopenharmony_cinetworking subsystems make sure that the buffers they use are valid
14762306a36Sopenharmony_cifor you to DMA from/to.
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ciDMA addressing capabilities
15062306a36Sopenharmony_ci===========================
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciBy default, the kernel assumes that your device can address 32-bits of DMA
15362306a36Sopenharmony_ciaddressing.  For a 64-bit capable device, this needs to be increased, and for
15462306a36Sopenharmony_cia device with limitations, it needs to be decreased.
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciSpecial note about PCI: PCI-X specification requires PCI-X devices to support
15762306a36Sopenharmony_ci64-bit addressing (DAC) for all transactions.  And at least one platform (SGI
15862306a36Sopenharmony_ciSN2) requires 64-bit consistent allocations to operate correctly when the IO
15962306a36Sopenharmony_cibus is in PCI-X mode.
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciFor correct operation, you must set the DMA mask to inform the kernel about
16262306a36Sopenharmony_ciyour devices DMA addressing capabilities.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ciThis is performed via a call to dma_set_mask_and_coherent()::
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	int dma_set_mask_and_coherent(struct device *dev, u64 mask);
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ciwhich will set the mask for both streaming and coherent APIs together.  If you
16962306a36Sopenharmony_cihave some special requirements, then the following two separate calls can be
17062306a36Sopenharmony_ciused instead:
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	The setup for streaming mappings is performed via a call to
17362306a36Sopenharmony_ci	dma_set_mask()::
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci		int dma_set_mask(struct device *dev, u64 mask);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	The setup for consistent allocations is performed via a call
17862306a36Sopenharmony_ci	to dma_set_coherent_mask()::
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci		int dma_set_coherent_mask(struct device *dev, u64 mask);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciHere, dev is a pointer to the device struct of your device, and mask is a bit
18362306a36Sopenharmony_cimask describing which bits of an address your device supports.  Often the
18462306a36Sopenharmony_cidevice struct of your device is embedded in the bus-specific device struct of
18562306a36Sopenharmony_ciyour device.  For example, &pdev->dev is a pointer to the device struct of a
18662306a36Sopenharmony_ciPCI device (pdev is a pointer to the PCI device struct of your device).
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciThese calls usually return zero to indicate your device can perform DMA
18962306a36Sopenharmony_ciproperly on the machine given the address mask you provided, but they might
19062306a36Sopenharmony_cireturn an error if the mask is too small to be supportable on the given
19162306a36Sopenharmony_cisystem.  If it returns non-zero, your device cannot perform DMA properly on
19262306a36Sopenharmony_cithis platform, and attempting to do so will result in undefined behavior.
19362306a36Sopenharmony_ciYou must not use DMA on this device unless the dma_set_mask family of
19462306a36Sopenharmony_cifunctions has returned success.
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciThis means that in the failure case, you have two options:
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci1) Use some non-DMA mode for data transfer, if possible.
19962306a36Sopenharmony_ci2) Ignore this device and do not initialize it.
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ciIt is recommended that your driver print a kernel KERN_WARNING message when
20262306a36Sopenharmony_cisetting the DMA mask fails.  In this manner, if a user of your driver reports
20362306a36Sopenharmony_cithat performance is bad or that the device is not even detected, you can ask
20462306a36Sopenharmony_cithem for the kernel messages to find out exactly why.
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciThe standard 64-bit addressing device would do something like this::
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
20962306a36Sopenharmony_ci		dev_warn(dev, "mydev: No suitable DMA available\n");
21062306a36Sopenharmony_ci		goto ignore_this_device;
21162306a36Sopenharmony_ci	}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciIf the device only supports 32-bit addressing for descriptors in the
21462306a36Sopenharmony_cicoherent allocations, but supports full 64-bits for streaming mappings
21562306a36Sopenharmony_ciit would look like this::
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	if (dma_set_mask(dev, DMA_BIT_MASK(64))) {
21862306a36Sopenharmony_ci		dev_warn(dev, "mydev: No suitable DMA available\n");
21962306a36Sopenharmony_ci		goto ignore_this_device;
22062306a36Sopenharmony_ci	}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ciThe coherent mask will always be able to set the same or a smaller mask as
22362306a36Sopenharmony_cithe streaming mask. However for the rare case that a device driver only
22462306a36Sopenharmony_ciuses consistent allocations, one would have to check the return value from
22562306a36Sopenharmony_cidma_set_coherent_mask().
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciFinally, if your device can only drive the low 24-bits of
22862306a36Sopenharmony_ciaddress you might do something like::
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	if (dma_set_mask(dev, DMA_BIT_MASK(24))) {
23162306a36Sopenharmony_ci		dev_warn(dev, "mydev: 24-bit DMA addressing not available\n");
23262306a36Sopenharmony_ci		goto ignore_this_device;
23362306a36Sopenharmony_ci	}
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciWhen dma_set_mask() or dma_set_mask_and_coherent() is successful, and
23662306a36Sopenharmony_cireturns zero, the kernel saves away this mask you have provided.  The
23762306a36Sopenharmony_cikernel will use this information later when you make DMA mappings.
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ciThere is a case which we are aware of at this time, which is worth
24062306a36Sopenharmony_cimentioning in this documentation.  If your device supports multiple
24162306a36Sopenharmony_cifunctions (for example a sound card provides playback and record
24262306a36Sopenharmony_cifunctions) and the various different functions have _different_
24362306a36Sopenharmony_ciDMA addressing limitations, you may wish to probe each mask and
24462306a36Sopenharmony_cionly provide the functionality which the machine can handle.  It
24562306a36Sopenharmony_ciis important that the last call to dma_set_mask() be for the
24662306a36Sopenharmony_cimost specific mask.
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ciHere is pseudo-code showing how this might be done::
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	#define PLAYBACK_ADDRESS_BITS	DMA_BIT_MASK(32)
25162306a36Sopenharmony_ci	#define RECORD_ADDRESS_BITS	DMA_BIT_MASK(24)
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	struct my_sound_card *card;
25462306a36Sopenharmony_ci	struct device *dev;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	...
25762306a36Sopenharmony_ci	if (!dma_set_mask(dev, PLAYBACK_ADDRESS_BITS)) {
25862306a36Sopenharmony_ci		card->playback_enabled = 1;
25962306a36Sopenharmony_ci	} else {
26062306a36Sopenharmony_ci		card->playback_enabled = 0;
26162306a36Sopenharmony_ci		dev_warn(dev, "%s: Playback disabled due to DMA limitations\n",
26262306a36Sopenharmony_ci		       card->name);
26362306a36Sopenharmony_ci	}
26462306a36Sopenharmony_ci	if (!dma_set_mask(dev, RECORD_ADDRESS_BITS)) {
26562306a36Sopenharmony_ci		card->record_enabled = 1;
26662306a36Sopenharmony_ci	} else {
26762306a36Sopenharmony_ci		card->record_enabled = 0;
26862306a36Sopenharmony_ci		dev_warn(dev, "%s: Record disabled due to DMA limitations\n",
26962306a36Sopenharmony_ci		       card->name);
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ciA sound card was used as an example here because this genre of PCI
27362306a36Sopenharmony_cidevices seems to be littered with ISA chips given a PCI front end,
27462306a36Sopenharmony_ciand thus retaining the 16MB DMA addressing limitations of ISA.
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciTypes of DMA mappings
27762306a36Sopenharmony_ci=====================
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ciThere are two types of DMA mappings:
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci- Consistent DMA mappings which are usually mapped at driver
28262306a36Sopenharmony_ci  initialization, unmapped at the end and for which the hardware should
28362306a36Sopenharmony_ci  guarantee that the device and the CPU can access the data
28462306a36Sopenharmony_ci  in parallel and will see updates made by each other without any
28562306a36Sopenharmony_ci  explicit software flushing.
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci  Think of "consistent" as "synchronous" or "coherent".
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci  The current default is to return consistent memory in the low 32
29062306a36Sopenharmony_ci  bits of the DMA space.  However, for future compatibility you should
29162306a36Sopenharmony_ci  set the consistent mask even if this default is fine for your
29262306a36Sopenharmony_ci  driver.
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci  Good examples of what to use consistent mappings for are:
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	- Network card DMA ring descriptors.
29762306a36Sopenharmony_ci	- SCSI adapter mailbox command data structures.
29862306a36Sopenharmony_ci	- Device firmware microcode executed out of
29962306a36Sopenharmony_ci	  main memory.
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci  The invariant these examples all require is that any CPU store
30262306a36Sopenharmony_ci  to memory is immediately visible to the device, and vice
30362306a36Sopenharmony_ci  versa.  Consistent mappings guarantee this.
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci  .. important::
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	     Consistent DMA memory does not preclude the usage of
30862306a36Sopenharmony_ci	     proper memory barriers.  The CPU may reorder stores to
30962306a36Sopenharmony_ci	     consistent memory just as it may normal memory.  Example:
31062306a36Sopenharmony_ci	     if it is important for the device to see the first word
31162306a36Sopenharmony_ci	     of a descriptor updated before the second, you must do
31262306a36Sopenharmony_ci	     something like::
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci		desc->word0 = address;
31562306a36Sopenharmony_ci		wmb();
31662306a36Sopenharmony_ci		desc->word1 = DESC_VALID;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci             in order to get correct behavior on all platforms.
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	     Also, on some platforms your driver may need to flush CPU write
32162306a36Sopenharmony_ci	     buffers in much the same way as it needs to flush write buffers
32262306a36Sopenharmony_ci	     found in PCI bridges (such as by reading a register's value
32362306a36Sopenharmony_ci	     after writing it).
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci- Streaming DMA mappings which are usually mapped for one DMA
32662306a36Sopenharmony_ci  transfer, unmapped right after it (unless you use dma_sync_* below)
32762306a36Sopenharmony_ci  and for which hardware can optimize for sequential accesses.
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci  Think of "streaming" as "asynchronous" or "outside the coherency
33062306a36Sopenharmony_ci  domain".
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci  Good examples of what to use streaming mappings for are:
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	- Networking buffers transmitted/received by a device.
33562306a36Sopenharmony_ci	- Filesystem buffers written/read by a SCSI device.
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci  The interfaces for using this type of mapping were designed in
33862306a36Sopenharmony_ci  such a way that an implementation can make whatever performance
33962306a36Sopenharmony_ci  optimizations the hardware allows.  To this end, when using
34062306a36Sopenharmony_ci  such mappings you must be explicit about what you want to happen.
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ciNeither type of DMA mapping has alignment restrictions that come from
34362306a36Sopenharmony_cithe underlying bus, although some devices may have such restrictions.
34462306a36Sopenharmony_ciAlso, systems with caches that aren't DMA-coherent will work better
34562306a36Sopenharmony_ciwhen the underlying buffers don't share cache lines with other data.
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ciUsing Consistent DMA mappings
34962306a36Sopenharmony_ci=============================
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ciTo allocate and map large (PAGE_SIZE or so) consistent DMA regions,
35262306a36Sopenharmony_ciyou should do::
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	dma_addr_t dma_handle;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	cpu_addr = dma_alloc_coherent(dev, size, &dma_handle, gfp);
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ciwhere device is a ``struct device *``. This may be called in interrupt
35962306a36Sopenharmony_cicontext with the GFP_ATOMIC flag.
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ciSize is the length of the region you want to allocate, in bytes.
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ciThis routine will allocate RAM for that region, so it acts similarly to
36462306a36Sopenharmony_ci__get_free_pages() (but takes size instead of a page order).  If your
36562306a36Sopenharmony_cidriver needs regions sized smaller than a page, you may prefer using
36662306a36Sopenharmony_cithe dma_pool interface, described below.
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ciThe consistent DMA mapping interfaces, will by default return a DMA address
36962306a36Sopenharmony_ciwhich is 32-bit addressable.  Even if the device indicates (via the DMA mask)
37062306a36Sopenharmony_cithat it may address the upper 32-bits, consistent allocation will only
37162306a36Sopenharmony_cireturn > 32-bit addresses for DMA if the consistent DMA mask has been
37262306a36Sopenharmony_ciexplicitly changed via dma_set_coherent_mask().  This is true of the
37362306a36Sopenharmony_cidma_pool interface as well.
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_cidma_alloc_coherent() returns two values: the virtual address which you
37662306a36Sopenharmony_cican use to access it from the CPU and dma_handle which you pass to the
37762306a36Sopenharmony_cicard.
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ciThe CPU virtual address and the DMA address are both
38062306a36Sopenharmony_ciguaranteed to be aligned to the smallest PAGE_SIZE order which
38162306a36Sopenharmony_ciis greater than or equal to the requested size.  This invariant
38262306a36Sopenharmony_ciexists (for example) to guarantee that if you allocate a chunk
38362306a36Sopenharmony_ciwhich is smaller than or equal to 64 kilobytes, the extent of the
38462306a36Sopenharmony_cibuffer you receive will not cross a 64K boundary.
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ciTo unmap and free such a DMA region, you call::
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	dma_free_coherent(dev, size, cpu_addr, dma_handle);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ciwhere dev, size are the same as in the above call and cpu_addr and
39162306a36Sopenharmony_cidma_handle are the values dma_alloc_coherent() returned to you.
39262306a36Sopenharmony_ciThis function may not be called in interrupt context.
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ciIf your driver needs lots of smaller memory regions, you can write
39562306a36Sopenharmony_cicustom code to subdivide pages returned by dma_alloc_coherent(),
39662306a36Sopenharmony_cior you can use the dma_pool API to do that.  A dma_pool is like
39762306a36Sopenharmony_cia kmem_cache, but it uses dma_alloc_coherent(), not __get_free_pages().
39862306a36Sopenharmony_ciAlso, it understands common hardware constraints for alignment,
39962306a36Sopenharmony_cilike queue heads needing to be aligned on N byte boundaries.
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ciCreate a dma_pool like this::
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	struct dma_pool *pool;
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	pool = dma_pool_create(name, dev, size, align, boundary);
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ciThe "name" is for diagnostics (like a kmem_cache name); dev and size
40862306a36Sopenharmony_ciare as above.  The device's hardware alignment requirement for this
40962306a36Sopenharmony_citype of data is "align" (which is expressed in bytes, and must be a
41062306a36Sopenharmony_cipower of two).  If your device has no boundary crossing restrictions,
41162306a36Sopenharmony_cipass 0 for boundary; passing 4096 says memory allocated from this pool
41262306a36Sopenharmony_cimust not cross 4KByte boundaries (but at that time it may be better to
41362306a36Sopenharmony_ciuse dma_alloc_coherent() directly instead).
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ciAllocate memory from a DMA pool like this::
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	cpu_addr = dma_pool_alloc(pool, flags, &dma_handle);
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ciflags are GFP_KERNEL if blocking is permitted (not in_interrupt nor
42062306a36Sopenharmony_ciholding SMP locks), GFP_ATOMIC otherwise.  Like dma_alloc_coherent(),
42162306a36Sopenharmony_cithis returns two values, cpu_addr and dma_handle.
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ciFree memory that was allocated from a dma_pool like this::
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	dma_pool_free(pool, cpu_addr, dma_handle);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciwhere pool is what you passed to dma_pool_alloc(), and cpu_addr and
42862306a36Sopenharmony_cidma_handle are the values dma_pool_alloc() returned. This function
42962306a36Sopenharmony_cimay be called in interrupt context.
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ciDestroy a dma_pool by calling::
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	dma_pool_destroy(pool);
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ciMake sure you've called dma_pool_free() for all memory allocated
43662306a36Sopenharmony_cifrom a pool before you destroy the pool. This function may not
43762306a36Sopenharmony_cibe called in interrupt context.
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ciDMA Direction
44062306a36Sopenharmony_ci=============
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ciThe interfaces described in subsequent portions of this document
44362306a36Sopenharmony_citake a DMA direction argument, which is an integer and takes on
44462306a36Sopenharmony_cione of the following values::
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci DMA_BIDIRECTIONAL
44762306a36Sopenharmony_ci DMA_TO_DEVICE
44862306a36Sopenharmony_ci DMA_FROM_DEVICE
44962306a36Sopenharmony_ci DMA_NONE
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ciYou should provide the exact DMA direction if you know it.
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ciDMA_TO_DEVICE means "from main memory to the device"
45462306a36Sopenharmony_ciDMA_FROM_DEVICE means "from the device to main memory"
45562306a36Sopenharmony_ciIt is the direction in which the data moves during the DMA
45662306a36Sopenharmony_citransfer.
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ciYou are _strongly_ encouraged to specify this as precisely
45962306a36Sopenharmony_cias you possibly can.
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ciIf you absolutely cannot know the direction of the DMA transfer,
46262306a36Sopenharmony_cispecify DMA_BIDIRECTIONAL.  It means that the DMA can go in
46362306a36Sopenharmony_cieither direction.  The platform guarantees that you may legally
46462306a36Sopenharmony_cispecify this, and that it will work, but this may be at the
46562306a36Sopenharmony_cicost of performance for example.
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ciThe value DMA_NONE is to be used for debugging.  One can
46862306a36Sopenharmony_cihold this in a data structure before you come to know the
46962306a36Sopenharmony_ciprecise direction, and this will help catch cases where your
47062306a36Sopenharmony_cidirection tracking logic has failed to set things up properly.
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ciAnother advantage of specifying this value precisely (outside of
47362306a36Sopenharmony_cipotential platform-specific optimizations of such) is for debugging.
47462306a36Sopenharmony_ciSome platforms actually have a write permission boolean which DMA
47562306a36Sopenharmony_cimappings can be marked with, much like page protections in the user
47662306a36Sopenharmony_ciprogram address space.  Such platforms can and do report errors in the
47762306a36Sopenharmony_cikernel logs when the DMA controller hardware detects violation of the
47862306a36Sopenharmony_cipermission setting.
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ciOnly streaming mappings specify a direction, consistent mappings
48162306a36Sopenharmony_ciimplicitly have a direction attribute setting of
48262306a36Sopenharmony_ciDMA_BIDIRECTIONAL.
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ciThe SCSI subsystem tells you the direction to use in the
48562306a36Sopenharmony_ci'sc_data_direction' member of the SCSI command your driver is
48662306a36Sopenharmony_ciworking on.
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ciFor Networking drivers, it's a rather simple affair.  For transmit
48962306a36Sopenharmony_cipackets, map/unmap them with the DMA_TO_DEVICE direction
49062306a36Sopenharmony_cispecifier.  For receive packets, just the opposite, map/unmap them
49162306a36Sopenharmony_ciwith the DMA_FROM_DEVICE direction specifier.
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ciUsing Streaming DMA mappings
49462306a36Sopenharmony_ci============================
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ciThe streaming DMA mapping routines can be called from interrupt
49762306a36Sopenharmony_cicontext.  There are two versions of each map/unmap, one which will
49862306a36Sopenharmony_cimap/unmap a single memory region, and one which will map/unmap a
49962306a36Sopenharmony_ciscatterlist.
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ciTo map a single region, you do::
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	struct device *dev = &my_dev->dev;
50462306a36Sopenharmony_ci	dma_addr_t dma_handle;
50562306a36Sopenharmony_ci	void *addr = buffer->ptr;
50662306a36Sopenharmony_ci	size_t size = buffer->len;
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	dma_handle = dma_map_single(dev, addr, size, direction);
50962306a36Sopenharmony_ci	if (dma_mapping_error(dev, dma_handle)) {
51062306a36Sopenharmony_ci		/*
51162306a36Sopenharmony_ci		 * reduce current DMA mapping usage,
51262306a36Sopenharmony_ci		 * delay and try again later or
51362306a36Sopenharmony_ci		 * reset driver.
51462306a36Sopenharmony_ci		 */
51562306a36Sopenharmony_ci		goto map_error_handling;
51662306a36Sopenharmony_ci	}
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ciand to unmap it::
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	dma_unmap_single(dev, dma_handle, size, direction);
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ciYou should call dma_mapping_error() as dma_map_single() could fail and return
52362306a36Sopenharmony_cierror.  Doing so will ensure that the mapping code will work correctly on all
52462306a36Sopenharmony_ciDMA implementations without any dependency on the specifics of the underlying
52562306a36Sopenharmony_ciimplementation. Using the returned address without checking for errors could
52662306a36Sopenharmony_ciresult in failures ranging from panics to silent data corruption.  The same
52762306a36Sopenharmony_ciapplies to dma_map_page() as well.
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ciYou should call dma_unmap_single() when the DMA activity is finished, e.g.,
53062306a36Sopenharmony_cifrom the interrupt which told you that the DMA transfer is done.
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_ciUsing CPU pointers like this for single mappings has a disadvantage:
53362306a36Sopenharmony_ciyou cannot reference HIGHMEM memory in this way.  Thus, there is a
53462306a36Sopenharmony_cimap/unmap interface pair akin to dma_{map,unmap}_single().  These
53562306a36Sopenharmony_ciinterfaces deal with page/offset pairs instead of CPU pointers.
53662306a36Sopenharmony_ciSpecifically::
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	struct device *dev = &my_dev->dev;
53962306a36Sopenharmony_ci	dma_addr_t dma_handle;
54062306a36Sopenharmony_ci	struct page *page = buffer->page;
54162306a36Sopenharmony_ci	unsigned long offset = buffer->offset;
54262306a36Sopenharmony_ci	size_t size = buffer->len;
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	dma_handle = dma_map_page(dev, page, offset, size, direction);
54562306a36Sopenharmony_ci	if (dma_mapping_error(dev, dma_handle)) {
54662306a36Sopenharmony_ci		/*
54762306a36Sopenharmony_ci		 * reduce current DMA mapping usage,
54862306a36Sopenharmony_ci		 * delay and try again later or
54962306a36Sopenharmony_ci		 * reset driver.
55062306a36Sopenharmony_ci		 */
55162306a36Sopenharmony_ci		goto map_error_handling;
55262306a36Sopenharmony_ci	}
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	...
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	dma_unmap_page(dev, dma_handle, size, direction);
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ciHere, "offset" means byte offset within the given page.
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ciYou should call dma_mapping_error() as dma_map_page() could fail and return
56162306a36Sopenharmony_cierror as outlined under the dma_map_single() discussion.
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ciYou should call dma_unmap_page() when the DMA activity is finished, e.g.,
56462306a36Sopenharmony_cifrom the interrupt which told you that the DMA transfer is done.
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ciWith scatterlists, you map a region gathered from several regions by::
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci	int i, count = dma_map_sg(dev, sglist, nents, direction);
56962306a36Sopenharmony_ci	struct scatterlist *sg;
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	for_each_sg(sglist, sg, count, i) {
57262306a36Sopenharmony_ci		hw_address[i] = sg_dma_address(sg);
57362306a36Sopenharmony_ci		hw_len[i] = sg_dma_len(sg);
57462306a36Sopenharmony_ci	}
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ciwhere nents is the number of entries in the sglist.
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ciThe implementation is free to merge several consecutive sglist entries
57962306a36Sopenharmony_ciinto one (e.g. if DMA mapping is done with PAGE_SIZE granularity, any
58062306a36Sopenharmony_ciconsecutive sglist entries can be merged into one provided the first one
58162306a36Sopenharmony_ciends and the second one starts on a page boundary - in fact this is a huge
58262306a36Sopenharmony_ciadvantage for cards which either cannot do scatter-gather or have very
58362306a36Sopenharmony_cilimited number of scatter-gather entries) and returns the actual number
58462306a36Sopenharmony_ciof sg entries it mapped them to. On failure 0 is returned.
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ciThen you should loop count times (note: this can be less than nents times)
58762306a36Sopenharmony_ciand use sg_dma_address() and sg_dma_len() macros where you previously
58862306a36Sopenharmony_ciaccessed sg->address and sg->length as shown above.
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ciTo unmap a scatterlist, just call::
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci	dma_unmap_sg(dev, sglist, nents, direction);
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ciAgain, make sure DMA activity has already finished.
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci.. note::
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	The 'nents' argument to the dma_unmap_sg call must be
59962306a36Sopenharmony_ci	the _same_ one you passed into the dma_map_sg call,
60062306a36Sopenharmony_ci	it should _NOT_ be the 'count' value _returned_ from the
60162306a36Sopenharmony_ci	dma_map_sg call.
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ciEvery dma_map_{single,sg}() call should have its dma_unmap_{single,sg}()
60462306a36Sopenharmony_cicounterpart, because the DMA address space is a shared resource and
60562306a36Sopenharmony_ciyou could render the machine unusable by consuming all DMA addresses.
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ciIf you need to use the same streaming DMA region multiple times and touch
60862306a36Sopenharmony_cithe data in between the DMA transfers, the buffer needs to be synced
60962306a36Sopenharmony_ciproperly in order for the CPU and device to see the most up-to-date and
61062306a36Sopenharmony_cicorrect copy of the DMA buffer.
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ciSo, firstly, just map it with dma_map_{single,sg}(), and after each DMA
61362306a36Sopenharmony_citransfer call either::
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci	dma_sync_single_for_cpu(dev, dma_handle, size, direction);
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_cior::
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ci	dma_sync_sg_for_cpu(dev, sglist, nents, direction);
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_cias appropriate.
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ciThen, if you wish to let the device get at the DMA area again,
62462306a36Sopenharmony_cifinish accessing the data with the CPU, and then before actually
62562306a36Sopenharmony_cigiving the buffer to the hardware call either::
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci	dma_sync_single_for_device(dev, dma_handle, size, direction);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_cior::
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	dma_sync_sg_for_device(dev, sglist, nents, direction);
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_cias appropriate.
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci.. note::
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci	      The 'nents' argument to dma_sync_sg_for_cpu() and
63862306a36Sopenharmony_ci	      dma_sync_sg_for_device() must be the same passed to
63962306a36Sopenharmony_ci	      dma_map_sg(). It is _NOT_ the count returned by
64062306a36Sopenharmony_ci	      dma_map_sg().
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ciAfter the last DMA transfer call one of the DMA unmap routines
64362306a36Sopenharmony_cidma_unmap_{single,sg}(). If you don't touch the data from the first
64462306a36Sopenharmony_cidma_map_*() call till dma_unmap_*(), then you don't have to call the
64562306a36Sopenharmony_cidma_sync_*() routines at all.
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ciHere is pseudo code which shows a situation in which you would need
64862306a36Sopenharmony_cito use the dma_sync_*() interfaces::
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci	my_card_setup_receive_buffer(struct my_card *cp, char *buffer, int len)
65162306a36Sopenharmony_ci	{
65262306a36Sopenharmony_ci		dma_addr_t mapping;
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci		mapping = dma_map_single(cp->dev, buffer, len, DMA_FROM_DEVICE);
65562306a36Sopenharmony_ci		if (dma_mapping_error(cp->dev, mapping)) {
65662306a36Sopenharmony_ci			/*
65762306a36Sopenharmony_ci			 * reduce current DMA mapping usage,
65862306a36Sopenharmony_ci			 * delay and try again later or
65962306a36Sopenharmony_ci			 * reset driver.
66062306a36Sopenharmony_ci			 */
66162306a36Sopenharmony_ci			goto map_error_handling;
66262306a36Sopenharmony_ci		}
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci		cp->rx_buf = buffer;
66562306a36Sopenharmony_ci		cp->rx_len = len;
66662306a36Sopenharmony_ci		cp->rx_dma = mapping;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci		give_rx_buf_to_card(cp);
66962306a36Sopenharmony_ci	}
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	...
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	my_card_interrupt_handler(int irq, void *devid, struct pt_regs *regs)
67462306a36Sopenharmony_ci	{
67562306a36Sopenharmony_ci		struct my_card *cp = devid;
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci		...
67862306a36Sopenharmony_ci		if (read_card_status(cp) == RX_BUF_TRANSFERRED) {
67962306a36Sopenharmony_ci			struct my_card_header *hp;
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_ci			/* Examine the header to see if we wish
68262306a36Sopenharmony_ci			 * to accept the data.  But synchronize
68362306a36Sopenharmony_ci			 * the DMA transfer with the CPU first
68462306a36Sopenharmony_ci			 * so that we see updated contents.
68562306a36Sopenharmony_ci			 */
68662306a36Sopenharmony_ci			dma_sync_single_for_cpu(&cp->dev, cp->rx_dma,
68762306a36Sopenharmony_ci						cp->rx_len,
68862306a36Sopenharmony_ci						DMA_FROM_DEVICE);
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci			/* Now it is safe to examine the buffer. */
69162306a36Sopenharmony_ci			hp = (struct my_card_header *) cp->rx_buf;
69262306a36Sopenharmony_ci			if (header_is_ok(hp)) {
69362306a36Sopenharmony_ci				dma_unmap_single(&cp->dev, cp->rx_dma, cp->rx_len,
69462306a36Sopenharmony_ci						 DMA_FROM_DEVICE);
69562306a36Sopenharmony_ci				pass_to_upper_layers(cp->rx_buf);
69662306a36Sopenharmony_ci				make_and_setup_new_rx_buf(cp);
69762306a36Sopenharmony_ci			} else {
69862306a36Sopenharmony_ci				/* CPU should not write to
69962306a36Sopenharmony_ci				 * DMA_FROM_DEVICE-mapped area,
70062306a36Sopenharmony_ci				 * so dma_sync_single_for_device() is
70162306a36Sopenharmony_ci				 * not needed here. It would be required
70262306a36Sopenharmony_ci				 * for DMA_BIDIRECTIONAL mapping if
70362306a36Sopenharmony_ci				 * the memory was modified.
70462306a36Sopenharmony_ci				 */
70562306a36Sopenharmony_ci				give_rx_buf_to_card(cp);
70662306a36Sopenharmony_ci			}
70762306a36Sopenharmony_ci		}
70862306a36Sopenharmony_ci	}
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ciHandling Errors
71162306a36Sopenharmony_ci===============
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ciDMA address space is limited on some architectures and an allocation
71462306a36Sopenharmony_cifailure can be determined by:
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci- checking if dma_alloc_coherent() returns NULL or dma_map_sg returns 0
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci- checking the dma_addr_t returned from dma_map_single() and dma_map_page()
71962306a36Sopenharmony_ci  by using dma_mapping_error()::
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci	dma_addr_t dma_handle;
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	dma_handle = dma_map_single(dev, addr, size, direction);
72462306a36Sopenharmony_ci	if (dma_mapping_error(dev, dma_handle)) {
72562306a36Sopenharmony_ci		/*
72662306a36Sopenharmony_ci		 * reduce current DMA mapping usage,
72762306a36Sopenharmony_ci		 * delay and try again later or
72862306a36Sopenharmony_ci		 * reset driver.
72962306a36Sopenharmony_ci		 */
73062306a36Sopenharmony_ci		goto map_error_handling;
73162306a36Sopenharmony_ci	}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci- unmap pages that are already mapped, when mapping error occurs in the middle
73462306a36Sopenharmony_ci  of a multiple page mapping attempt. These example are applicable to
73562306a36Sopenharmony_ci  dma_map_page() as well.
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ciExample 1::
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci	dma_addr_t dma_handle1;
74062306a36Sopenharmony_ci	dma_addr_t dma_handle2;
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci	dma_handle1 = dma_map_single(dev, addr, size, direction);
74362306a36Sopenharmony_ci	if (dma_mapping_error(dev, dma_handle1)) {
74462306a36Sopenharmony_ci		/*
74562306a36Sopenharmony_ci		 * reduce current DMA mapping usage,
74662306a36Sopenharmony_ci		 * delay and try again later or
74762306a36Sopenharmony_ci		 * reset driver.
74862306a36Sopenharmony_ci		 */
74962306a36Sopenharmony_ci		goto map_error_handling1;
75062306a36Sopenharmony_ci	}
75162306a36Sopenharmony_ci	dma_handle2 = dma_map_single(dev, addr, size, direction);
75262306a36Sopenharmony_ci	if (dma_mapping_error(dev, dma_handle2)) {
75362306a36Sopenharmony_ci		/*
75462306a36Sopenharmony_ci		 * reduce current DMA mapping usage,
75562306a36Sopenharmony_ci		 * delay and try again later or
75662306a36Sopenharmony_ci		 * reset driver.
75762306a36Sopenharmony_ci		 */
75862306a36Sopenharmony_ci		goto map_error_handling2;
75962306a36Sopenharmony_ci	}
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	...
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci	map_error_handling2:
76462306a36Sopenharmony_ci		dma_unmap_single(dma_handle1);
76562306a36Sopenharmony_ci	map_error_handling1:
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ciExample 2::
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci	/*
77062306a36Sopenharmony_ci	 * if buffers are allocated in a loop, unmap all mapped buffers when
77162306a36Sopenharmony_ci	 * mapping error is detected in the middle
77262306a36Sopenharmony_ci	 */
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	dma_addr_t dma_addr;
77562306a36Sopenharmony_ci	dma_addr_t array[DMA_BUFFERS];
77662306a36Sopenharmony_ci	int save_index = 0;
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_ci	for (i = 0; i < DMA_BUFFERS; i++) {
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci		...
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci		dma_addr = dma_map_single(dev, addr, size, direction);
78362306a36Sopenharmony_ci		if (dma_mapping_error(dev, dma_addr)) {
78462306a36Sopenharmony_ci			/*
78562306a36Sopenharmony_ci			 * reduce current DMA mapping usage,
78662306a36Sopenharmony_ci			 * delay and try again later or
78762306a36Sopenharmony_ci			 * reset driver.
78862306a36Sopenharmony_ci			 */
78962306a36Sopenharmony_ci			goto map_error_handling;
79062306a36Sopenharmony_ci		}
79162306a36Sopenharmony_ci		array[i].dma_addr = dma_addr;
79262306a36Sopenharmony_ci		save_index++;
79362306a36Sopenharmony_ci	}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	...
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ci	map_error_handling:
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci	for (i = 0; i < save_index; i++) {
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci		...
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci		dma_unmap_single(array[i].dma_addr);
80462306a36Sopenharmony_ci	}
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ciNetworking drivers must call dev_kfree_skb() to free the socket buffer
80762306a36Sopenharmony_ciand return NETDEV_TX_OK if the DMA mapping fails on the transmit hook
80862306a36Sopenharmony_ci(ndo_start_xmit). This means that the socket buffer is just dropped in
80962306a36Sopenharmony_cithe failure case.
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ciSCSI drivers must return SCSI_MLQUEUE_HOST_BUSY if the DMA mapping
81262306a36Sopenharmony_cifails in the queuecommand hook. This means that the SCSI subsystem
81362306a36Sopenharmony_cipasses the command to the driver again later.
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ciOptimizing Unmap State Space Consumption
81662306a36Sopenharmony_ci========================================
81762306a36Sopenharmony_ci
81862306a36Sopenharmony_ciOn many platforms, dma_unmap_{single,page}() is simply a nop.
81962306a36Sopenharmony_ciTherefore, keeping track of the mapping address and length is a waste
82062306a36Sopenharmony_ciof space.  Instead of filling your drivers up with ifdefs and the like
82162306a36Sopenharmony_cito "work around" this (which would defeat the whole purpose of a
82262306a36Sopenharmony_ciportable API) the following facilities are provided.
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ciActually, instead of describing the macros one by one, we'll
82562306a36Sopenharmony_citransform some example code.
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci1) Use DEFINE_DMA_UNMAP_{ADDR,LEN} in state saving structures.
82862306a36Sopenharmony_ci   Example, before::
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci	struct ring_state {
83162306a36Sopenharmony_ci		struct sk_buff *skb;
83262306a36Sopenharmony_ci		dma_addr_t mapping;
83362306a36Sopenharmony_ci		__u32 len;
83462306a36Sopenharmony_ci	};
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci   after::
83762306a36Sopenharmony_ci
83862306a36Sopenharmony_ci	struct ring_state {
83962306a36Sopenharmony_ci		struct sk_buff *skb;
84062306a36Sopenharmony_ci		DEFINE_DMA_UNMAP_ADDR(mapping);
84162306a36Sopenharmony_ci		DEFINE_DMA_UNMAP_LEN(len);
84262306a36Sopenharmony_ci	};
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci2) Use dma_unmap_{addr,len}_set() to set these values.
84562306a36Sopenharmony_ci   Example, before::
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	ringp->mapping = FOO;
84862306a36Sopenharmony_ci	ringp->len = BAR;
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_ci   after::
85162306a36Sopenharmony_ci
85262306a36Sopenharmony_ci	dma_unmap_addr_set(ringp, mapping, FOO);
85362306a36Sopenharmony_ci	dma_unmap_len_set(ringp, len, BAR);
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci3) Use dma_unmap_{addr,len}() to access these values.
85662306a36Sopenharmony_ci   Example, before::
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci	dma_unmap_single(dev, ringp->mapping, ringp->len,
85962306a36Sopenharmony_ci			 DMA_FROM_DEVICE);
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci   after::
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci	dma_unmap_single(dev,
86462306a36Sopenharmony_ci			 dma_unmap_addr(ringp, mapping),
86562306a36Sopenharmony_ci			 dma_unmap_len(ringp, len),
86662306a36Sopenharmony_ci			 DMA_FROM_DEVICE);
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ciIt really should be self-explanatory.  We treat the ADDR and LEN
86962306a36Sopenharmony_ciseparately, because it is possible for an implementation to only
87062306a36Sopenharmony_cineed the address in order to perform the unmap operation.
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ciPlatform Issues
87362306a36Sopenharmony_ci===============
87462306a36Sopenharmony_ci
87562306a36Sopenharmony_ciIf you are just writing drivers for Linux and do not maintain
87662306a36Sopenharmony_cian architecture port for the kernel, you can safely skip down
87762306a36Sopenharmony_cito "Closing".
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci1) Struct scatterlist requirements.
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ci   You need to enable CONFIG_NEED_SG_DMA_LENGTH if the architecture
88262306a36Sopenharmony_ci   supports IOMMUs (including software IOMMU).
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci2) ARCH_DMA_MINALIGN
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci   Architectures must ensure that kmalloc'ed buffer is
88762306a36Sopenharmony_ci   DMA-safe. Drivers and subsystems depend on it. If an architecture
88862306a36Sopenharmony_ci   isn't fully DMA-coherent (i.e. hardware doesn't ensure that data in
88962306a36Sopenharmony_ci   the CPU cache is identical to data in main memory),
89062306a36Sopenharmony_ci   ARCH_DMA_MINALIGN must be set so that the memory allocator
89162306a36Sopenharmony_ci   makes sure that kmalloc'ed buffer doesn't share a cache line with
89262306a36Sopenharmony_ci   the others. See arch/arm/include/asm/cache.h as an example.
89362306a36Sopenharmony_ci
89462306a36Sopenharmony_ci   Note that ARCH_DMA_MINALIGN is about DMA memory alignment
89562306a36Sopenharmony_ci   constraints. You don't need to worry about the architecture data
89662306a36Sopenharmony_ci   alignment constraints (e.g. the alignment constraints about 64-bit
89762306a36Sopenharmony_ci   objects).
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ciClosing
90062306a36Sopenharmony_ci=======
90162306a36Sopenharmony_ci
90262306a36Sopenharmony_ciThis document, and the API itself, would not be in its current
90362306a36Sopenharmony_ciform without the feedback and suggestions from numerous individuals.
90462306a36Sopenharmony_ciWe would like to specifically mention, in no particular order, the
90562306a36Sopenharmony_cifollowing people::
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	Russell King <rmk@arm.linux.org.uk>
90862306a36Sopenharmony_ci	Leo Dagum <dagum@barrel.engr.sgi.com>
90962306a36Sopenharmony_ci	Ralf Baechle <ralf@oss.sgi.com>
91062306a36Sopenharmony_ci	Grant Grundler <grundler@cup.hp.com>
91162306a36Sopenharmony_ci	Jay Estabrook <Jay.Estabrook@compaq.com>
91262306a36Sopenharmony_ci	Thomas Sailer <sailer@ife.ee.ethz.ch>
91362306a36Sopenharmony_ci	Andrea Arcangeli <andrea@suse.de>
91462306a36Sopenharmony_ci	Jens Axboe <jens.axboe@oracle.com>
91562306a36Sopenharmony_ci	David Mosberger-Tang <davidm@hpl.hp.com>
916