xref: /openbmc/linux/Documentation/driver-api/usb/dma.rst (revision 762f99f4f3cb41a775b5157dd761217beba65873)
12a373331SMauro Carvalho ChehabUSB DMA
22a373331SMauro Carvalho Chehab~~~~~~~
32a373331SMauro Carvalho Chehab
42a373331SMauro Carvalho ChehabIn Linux 2.5 kernels (and later), USB device drivers have additional control
52a373331SMauro Carvalho Chehabover how DMA may be used to perform I/O operations.  The APIs are detailed
62a373331SMauro Carvalho Chehabin the kernel usb programming guide (kerneldoc, from the source code).
72a373331SMauro Carvalho Chehab
82a373331SMauro Carvalho ChehabAPI overview
92a373331SMauro Carvalho Chehab============
102a373331SMauro Carvalho Chehab
112a373331SMauro Carvalho ChehabThe big picture is that USB drivers can continue to ignore most DMA issues,
122a373331SMauro Carvalho Chehabthough they still must provide DMA-ready buffers (see
13*ab8e8da6SMauro Carvalho ChehabDocumentation/core-api/dma-api-howto.rst).  That's how they've worked through
142a373331SMauro Carvalho Chehabthe 2.4 (and earlier) kernels, or they can now be DMA-aware.
152a373331SMauro Carvalho Chehab
162a373331SMauro Carvalho ChehabDMA-aware usb drivers:
172a373331SMauro Carvalho Chehab
182a373331SMauro Carvalho Chehab- New calls enable DMA-aware drivers, letting them allocate dma buffers and
192a373331SMauro Carvalho Chehab  manage dma mappings for existing dma-ready buffers (see below).
202a373331SMauro Carvalho Chehab
212a373331SMauro Carvalho Chehab- URBs have an additional "transfer_dma" field, as well as a transfer_flags
222a373331SMauro Carvalho Chehab  bit saying if it's valid.  (Control requests also have "setup_dma", but
232a373331SMauro Carvalho Chehab  drivers must not use it.)
242a373331SMauro Carvalho Chehab
252a373331SMauro Carvalho Chehab- "usbcore" will map this DMA address, if a DMA-aware driver didn't do
262a373331SMauro Carvalho Chehab  it first and set ``URB_NO_TRANSFER_DMA_MAP``.  HCDs
272a373331SMauro Carvalho Chehab  don't manage dma mappings for URBs.
282a373331SMauro Carvalho Chehab
292a373331SMauro Carvalho Chehab- There's a new "generic DMA API", parts of which are usable by USB device
302a373331SMauro Carvalho Chehab  drivers.  Never use dma_set_mask() on any USB interface or device; that
312a373331SMauro Carvalho Chehab  would potentially break all devices sharing that bus.
322a373331SMauro Carvalho Chehab
332a373331SMauro Carvalho ChehabEliminating copies
342a373331SMauro Carvalho Chehab==================
352a373331SMauro Carvalho Chehab
362a373331SMauro Carvalho ChehabIt's good to avoid making CPUs copy data needlessly.  The costs can add up,
372a373331SMauro Carvalho Chehaband effects like cache-trashing can impose subtle penalties.
382a373331SMauro Carvalho Chehab
392a373331SMauro Carvalho Chehab- If you're doing lots of small data transfers from the same buffer all
402a373331SMauro Carvalho Chehab  the time, that can really burn up resources on systems which use an
412a373331SMauro Carvalho Chehab  IOMMU to manage the DMA mappings.  It can cost MUCH more to set up and
422a373331SMauro Carvalho Chehab  tear down the IOMMU mappings with each request than perform the I/O!
432a373331SMauro Carvalho Chehab
442a373331SMauro Carvalho Chehab  For those specific cases, USB has primitives to allocate less expensive
452a373331SMauro Carvalho Chehab  memory.  They work like kmalloc and kfree versions that give you the right
462a373331SMauro Carvalho Chehab  kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
472a373331SMauro Carvalho Chehab  You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::
482a373331SMauro Carvalho Chehab
492a373331SMauro Carvalho Chehab	void *usb_alloc_coherent (struct usb_device *dev, size_t size,
502a373331SMauro Carvalho Chehab		int mem_flags, dma_addr_t *dma);
512a373331SMauro Carvalho Chehab
522a373331SMauro Carvalho Chehab	void usb_free_coherent (struct usb_device *dev, size_t size,
532a373331SMauro Carvalho Chehab		void *addr, dma_addr_t dma);
542a373331SMauro Carvalho Chehab
552a373331SMauro Carvalho Chehab  Most drivers should **NOT** be using these primitives; they don't need
562a373331SMauro Carvalho Chehab  to use this type of memory ("dma-coherent"), and memory returned from
572a373331SMauro Carvalho Chehab  :c:func:`kmalloc` will work just fine.
582a373331SMauro Carvalho Chehab
592a373331SMauro Carvalho Chehab  The memory buffer returned is "dma-coherent"; sometimes you might need to
602a373331SMauro Carvalho Chehab  force a consistent memory access ordering by using memory barriers.  It's
612a373331SMauro Carvalho Chehab  not using a streaming DMA mapping, so it's good for small transfers on
622a373331SMauro Carvalho Chehab  systems where the I/O would otherwise thrash an IOMMU mapping.  (See
63*ab8e8da6SMauro Carvalho Chehab  Documentation/core-api/dma-api-howto.rst for definitions of "coherent" and
642a373331SMauro Carvalho Chehab  "streaming" DMA mappings.)
652a373331SMauro Carvalho Chehab
662a373331SMauro Carvalho Chehab  Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
672a373331SMauro Carvalho Chehab  space-efficient.
682a373331SMauro Carvalho Chehab
692a373331SMauro Carvalho Chehab  On most systems the memory returned will be uncached, because the
702a373331SMauro Carvalho Chehab  semantics of dma-coherent memory require either bypassing CPU caches
712a373331SMauro Carvalho Chehab  or using cache hardware with bus-snooping support.  While x86 hardware
722a373331SMauro Carvalho Chehab  has such bus-snooping, many other systems use software to flush cache
732a373331SMauro Carvalho Chehab  lines to prevent DMA conflicts.
742a373331SMauro Carvalho Chehab
752a373331SMauro Carvalho Chehab- Devices on some EHCI controllers could handle DMA to/from high memory.
762a373331SMauro Carvalho Chehab
772a373331SMauro Carvalho Chehab  Unfortunately, the current Linux DMA infrastructure doesn't have a sane
782a373331SMauro Carvalho Chehab  way to expose these capabilities ... and in any case, HIGHMEM is mostly a
792a373331SMauro Carvalho Chehab  design wart specific to x86_32.  So your best bet is to ensure you never
802a373331SMauro Carvalho Chehab  pass a highmem buffer into a USB driver.  That's easy; it's the default
812a373331SMauro Carvalho Chehab  behavior.  Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.
822a373331SMauro Carvalho Chehab
832a373331SMauro Carvalho Chehab  This may force your callers to do some bounce buffering, copying from
842a373331SMauro Carvalho Chehab  high memory to "normal" DMA memory.  If you can come up with a good way
852a373331SMauro Carvalho Chehab  to fix this issue (for x86_32 machines with over 1 GByte of memory),
862a373331SMauro Carvalho Chehab  feel free to submit patches.
872a373331SMauro Carvalho Chehab
882a373331SMauro Carvalho ChehabWorking with existing buffers
892a373331SMauro Carvalho Chehab=============================
902a373331SMauro Carvalho Chehab
912a373331SMauro Carvalho ChehabExisting buffers aren't usable for DMA without first being mapped into the
922a373331SMauro Carvalho ChehabDMA address space of the device.  However, most buffers passed to your
932a373331SMauro Carvalho Chehabdriver can safely be used with such DMA mapping.  (See the first section
94*ab8e8da6SMauro Carvalho Chehabof Documentation/core-api/dma-api-howto.rst, titled "What memory is DMA-able?")
952a373331SMauro Carvalho Chehab
962a373331SMauro Carvalho Chehab- When you're using scatterlists, you can map everything at once.  On some
972a373331SMauro Carvalho Chehab  systems, this kicks in an IOMMU and turns the scatterlists into single
982a373331SMauro Carvalho Chehab  DMA transactions::
992a373331SMauro Carvalho Chehab
1002a373331SMauro Carvalho Chehab	int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1012a373331SMauro Carvalho Chehab		struct scatterlist *sg, int nents);
1022a373331SMauro Carvalho Chehab
1032a373331SMauro Carvalho Chehab	void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1042a373331SMauro Carvalho Chehab		struct scatterlist *sg, int n_hw_ents);
1052a373331SMauro Carvalho Chehab
1062a373331SMauro Carvalho Chehab	void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1072a373331SMauro Carvalho Chehab		struct scatterlist *sg, int n_hw_ents);
1082a373331SMauro Carvalho Chehab
1092a373331SMauro Carvalho Chehab  It's probably easier to use the new ``usb_sg_*()`` calls, which do the DMA
1102a373331SMauro Carvalho Chehab  mapping and apply other tweaks to make scatterlist i/o be fast.
1112a373331SMauro Carvalho Chehab
1122a373331SMauro Carvalho Chehab- Some drivers may prefer to work with the model that they're mapping large
1132a373331SMauro Carvalho Chehab  buffers, synchronizing their safe re-use.  (If there's no re-use, then let
1142a373331SMauro Carvalho Chehab  usbcore do the map/unmap.)  Large periodic transfers make good examples
1152a373331SMauro Carvalho Chehab  here, since it's cheaper to just synchronize the buffer than to unmap it
1162a373331SMauro Carvalho Chehab  each time an urb completes and then re-map it on during resubmission.
1172a373331SMauro Carvalho Chehab
1182a373331SMauro Carvalho Chehab  These calls all work with initialized urbs:  ``urb->dev``, ``urb->pipe``,
1192a373331SMauro Carvalho Chehab  ``urb->transfer_buffer``, and ``urb->transfer_buffer_length`` must all be
1202a373331SMauro Carvalho Chehab  valid when these calls are used (``urb->setup_packet`` must be valid too
1212a373331SMauro Carvalho Chehab  if urb is a control request)::
1222a373331SMauro Carvalho Chehab
1232a373331SMauro Carvalho Chehab	struct urb *usb_buffer_map (struct urb *urb);
1242a373331SMauro Carvalho Chehab
1252a373331SMauro Carvalho Chehab	void usb_buffer_dmasync (struct urb *urb);
1262a373331SMauro Carvalho Chehab
1272a373331SMauro Carvalho Chehab	void usb_buffer_unmap (struct urb *urb);
1282a373331SMauro Carvalho Chehab
1292a373331SMauro Carvalho Chehab  The calls manage ``urb->transfer_dma`` for you, and set
1302a373331SMauro Carvalho Chehab  ``URB_NO_TRANSFER_DMA_MAP`` so that usbcore won't map or unmap the buffer.
1312a373331SMauro Carvalho Chehab  They cannot be used for setup_packet buffers in control requests.
1322a373331SMauro Carvalho Chehab
1332a373331SMauro Carvalho ChehabNote that several of those interfaces are currently commented out, since
1342a373331SMauro Carvalho Chehabthey don't have current users.  See the source code.  Other than the dmasync
1352a373331SMauro Carvalho Chehabcalls (where the underlying DMA primitives have changed), most of them can
1362a373331SMauro Carvalho Chehabeasily be commented back in if you want to use them.
137