162306a36Sopenharmony_ciUSB DMA
262306a36Sopenharmony_ci~~~~~~~
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciIn Linux 2.5 kernels (and later), USB device drivers have additional control
562306a36Sopenharmony_ciover how DMA may be used to perform I/O operations.  The APIs are detailed
662306a36Sopenharmony_ciin the kernel usb programming guide (kerneldoc, from the source code).
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciAPI overview
962306a36Sopenharmony_ci============
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThe big picture is that USB drivers can continue to ignore most DMA issues,
1262306a36Sopenharmony_cithough they still must provide DMA-ready buffers (see
1362306a36Sopenharmony_ciDocumentation/core-api/dma-api-howto.rst).  That's how they've worked through
1462306a36Sopenharmony_cithe 2.4 (and earlier) kernels, or they can now be DMA-aware.
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciDMA-aware usb drivers:
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci- New calls enable DMA-aware drivers, letting them allocate dma buffers and
1962306a36Sopenharmony_ci  manage dma mappings for existing dma-ready buffers (see below).
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci- URBs have an additional "transfer_dma" field, as well as a transfer_flags
2262306a36Sopenharmony_ci  bit saying if it's valid.  (Control requests also have "setup_dma", but
2362306a36Sopenharmony_ci  drivers must not use it.)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci- "usbcore" will map this DMA address, if a DMA-aware driver didn't do
2662306a36Sopenharmony_ci  it first and set ``URB_NO_TRANSFER_DMA_MAP``.  HCDs
2762306a36Sopenharmony_ci  don't manage dma mappings for URBs.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci- There's a new "generic DMA API", parts of which are usable by USB device
3062306a36Sopenharmony_ci  drivers.  Never use dma_set_mask() on any USB interface or device; that
3162306a36Sopenharmony_ci  would potentially break all devices sharing that bus.
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciEliminating copies
3462306a36Sopenharmony_ci==================
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciIt's good to avoid making CPUs copy data needlessly.  The costs can add up,
3762306a36Sopenharmony_ciand effects like cache-trashing can impose subtle penalties.
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci- If you're doing lots of small data transfers from the same buffer all
4062306a36Sopenharmony_ci  the time, that can really burn up resources on systems which use an
4162306a36Sopenharmony_ci  IOMMU to manage the DMA mappings.  It can cost MUCH more to set up and
4262306a36Sopenharmony_ci  tear down the IOMMU mappings with each request than perform the I/O!
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci  For those specific cases, USB has primitives to allocate less expensive
4562306a36Sopenharmony_ci  memory.  They work like kmalloc and kfree versions that give you the right
4662306a36Sopenharmony_ci  kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
4762306a36Sopenharmony_ci  You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	void *usb_alloc_coherent (struct usb_device *dev, size_t size,
5062306a36Sopenharmony_ci		int mem_flags, dma_addr_t *dma);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	void usb_free_coherent (struct usb_device *dev, size_t size,
5362306a36Sopenharmony_ci		void *addr, dma_addr_t dma);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci  Most drivers should **NOT** be using these primitives; they don't need
5662306a36Sopenharmony_ci  to use this type of memory ("dma-coherent"), and memory returned from
5762306a36Sopenharmony_ci  :c:func:`kmalloc` will work just fine.
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci  The memory buffer returned is "dma-coherent"; sometimes you might need to
6062306a36Sopenharmony_ci  force a consistent memory access ordering by using memory barriers.  It's
6162306a36Sopenharmony_ci  not using a streaming DMA mapping, so it's good for small transfers on
6262306a36Sopenharmony_ci  systems where the I/O would otherwise thrash an IOMMU mapping.  (See
6362306a36Sopenharmony_ci  Documentation/core-api/dma-api-howto.rst for definitions of "coherent" and
6462306a36Sopenharmony_ci  "streaming" DMA mappings.)
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci  Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
6762306a36Sopenharmony_ci  space-efficient.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci  On most systems the memory returned will be uncached, because the
7062306a36Sopenharmony_ci  semantics of dma-coherent memory require either bypassing CPU caches
7162306a36Sopenharmony_ci  or using cache hardware with bus-snooping support.  While x86 hardware
7262306a36Sopenharmony_ci  has such bus-snooping, many other systems use software to flush cache
7362306a36Sopenharmony_ci  lines to prevent DMA conflicts.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci- Devices on some EHCI controllers could handle DMA to/from high memory.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci  Unfortunately, the current Linux DMA infrastructure doesn't have a sane
7862306a36Sopenharmony_ci  way to expose these capabilities ... and in any case, HIGHMEM is mostly a
7962306a36Sopenharmony_ci  design wart specific to x86_32.  So your best bet is to ensure you never
8062306a36Sopenharmony_ci  pass a highmem buffer into a USB driver.  That's easy; it's the default
8162306a36Sopenharmony_ci  behavior.  Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci  This may force your callers to do some bounce buffering, copying from
8462306a36Sopenharmony_ci  high memory to "normal" DMA memory.  If you can come up with a good way
8562306a36Sopenharmony_ci  to fix this issue (for x86_32 machines with over 1 GByte of memory),
8662306a36Sopenharmony_ci  feel free to submit patches.
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciWorking with existing buffers
8962306a36Sopenharmony_ci=============================
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ciExisting buffers aren't usable for DMA without first being mapped into the
9262306a36Sopenharmony_ciDMA address space of the device.  However, most buffers passed to your
9362306a36Sopenharmony_cidriver can safely be used with such DMA mapping.  (See the first section
9462306a36Sopenharmony_ciof Documentation/core-api/dma-api-howto.rst, titled "What memory is DMA-able?")
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci- When you're using scatterlists, you can map everything at once.  On some
9762306a36Sopenharmony_ci  systems, this kicks in an IOMMU and turns the scatterlists into single
9862306a36Sopenharmony_ci  DMA transactions::
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
10162306a36Sopenharmony_ci		struct scatterlist *sg, int nents);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
10462306a36Sopenharmony_ci		struct scatterlist *sg, int n_hw_ents);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
10762306a36Sopenharmony_ci		struct scatterlist *sg, int n_hw_ents);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci  It's probably easier to use the new ``usb_sg_*()`` calls, which do the DMA
11062306a36Sopenharmony_ci  mapping and apply other tweaks to make scatterlist i/o be fast.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci- Some drivers may prefer to work with the model that they're mapping large
11362306a36Sopenharmony_ci  buffers, synchronizing their safe re-use.  (If there's no re-use, then let
11462306a36Sopenharmony_ci  usbcore do the map/unmap.)  Large periodic transfers make good examples
11562306a36Sopenharmony_ci  here, since it's cheaper to just synchronize the buffer than to unmap it
11662306a36Sopenharmony_ci  each time an urb completes and then re-map it on during resubmission.
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci  These calls all work with initialized urbs:  ``urb->dev``, ``urb->pipe``,
11962306a36Sopenharmony_ci  ``urb->transfer_buffer``, and ``urb->transfer_buffer_length`` must all be
12062306a36Sopenharmony_ci  valid when these calls are used (``urb->setup_packet`` must be valid too
12162306a36Sopenharmony_ci  if urb is a control request)::
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	struct urb *usb_buffer_map (struct urb *urb);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	void usb_buffer_dmasync (struct urb *urb);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	void usb_buffer_unmap (struct urb *urb);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci  The calls manage ``urb->transfer_dma`` for you, and set
13062306a36Sopenharmony_ci  ``URB_NO_TRANSFER_DMA_MAP`` so that usbcore won't map or unmap the buffer.
13162306a36Sopenharmony_ci  They cannot be used for setup_packet buffers in control requests.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciNote that several of those interfaces are currently commented out, since
13462306a36Sopenharmony_cithey don't have current users.  See the source code.  Other than the dmasync
13562306a36Sopenharmony_cicalls (where the underlying DMA primitives have changed), most of them can
13662306a36Sopenharmony_cieasily be commented back in if you want to use them.
137