0001 USB DMA
0002 ~~~~~~~
0003
0004 In Linux 2.5 kernels (and later), USB device drivers have additional control
0005 over how DMA may be used to perform I/O operations. The APIs are detailed
0006 in the kernel usb programming guide (kerneldoc, from the source code).
0007
0008 API overview
0009 ============
0010
0011 The big picture is that USB drivers can continue to ignore most DMA issues,
0012 though they still must provide DMA-ready buffers (see
0013 Documentation/core-api/dma-api-howto.rst). That's how they've worked through
0014 the 2.4 (and earlier) kernels, or they can now be DMA-aware.
0015
0016 DMA-aware usb drivers:
0017
0018 - New calls enable DMA-aware drivers, letting them allocate dma buffers and
0019 manage dma mappings for existing dma-ready buffers (see below).
0020
0021 - URBs have an additional "transfer_dma" field, as well as a transfer_flags
0022 bit saying if it's valid. (Control requests also have "setup_dma", but
0023 drivers must not use it.)
0024
0025 - "usbcore" will map this DMA address, if a DMA-aware driver didn't do
0026 it first and set ``URB_NO_TRANSFER_DMA_MAP``. HCDs
0027 don't manage dma mappings for URBs.
0028
0029 - There's a new "generic DMA API", parts of which are usable by USB device
0030 drivers. Never use dma_set_mask() on any USB interface or device; that
0031 would potentially break all devices sharing that bus.
0032
0033 Eliminating copies
0034 ==================
0035
0036 It's good to avoid making CPUs copy data needlessly. The costs can add up,
0037 and effects like cache-trashing can impose subtle penalties.
0038
0039 - If you're doing lots of small data transfers from the same buffer all
0040 the time, that can really burn up resources on systems which use an
0041 IOMMU to manage the DMA mappings. It can cost MUCH more to set up and
0042 tear down the IOMMU mappings with each request than perform the I/O!
0043
0044 For those specific cases, USB has primitives to allocate less expensive
0045 memory. They work like kmalloc and kfree versions that give you the right
0046 kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
0047 You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::
0048
0049 void *usb_alloc_coherent (struct usb_device *dev, size_t size,
0050 int mem_flags, dma_addr_t *dma);
0051
0052 void usb_free_coherent (struct usb_device *dev, size_t size,
0053 void *addr, dma_addr_t dma);
0054
0055 Most drivers should **NOT** be using these primitives; they don't need
0056 to use this type of memory ("dma-coherent"), and memory returned from
0057 :c:func:`kmalloc` will work just fine.
0058
0059 The memory buffer returned is "dma-coherent"; sometimes you might need to
0060 force a consistent memory access ordering by using memory barriers. It's
0061 not using a streaming DMA mapping, so it's good for small transfers on
0062 systems where the I/O would otherwise thrash an IOMMU mapping. (See
0063 Documentation/core-api/dma-api-howto.rst for definitions of "coherent" and
0064 "streaming" DMA mappings.)
0065
0066 Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
0067 space-efficient.
0068
0069 On most systems the memory returned will be uncached, because the
0070 semantics of dma-coherent memory require either bypassing CPU caches
0071 or using cache hardware with bus-snooping support. While x86 hardware
0072 has such bus-snooping, many other systems use software to flush cache
0073 lines to prevent DMA conflicts.
0074
0075 - Devices on some EHCI controllers could handle DMA to/from high memory.
0076
0077 Unfortunately, the current Linux DMA infrastructure doesn't have a sane
0078 way to expose these capabilities ... and in any case, HIGHMEM is mostly a
0079 design wart specific to x86_32. So your best bet is to ensure you never
0080 pass a highmem buffer into a USB driver. That's easy; it's the default
0081 behavior. Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.
0082
0083 This may force your callers to do some bounce buffering, copying from
0084 high memory to "normal" DMA memory. If you can come up with a good way
0085 to fix this issue (for x86_32 machines with over 1 GByte of memory),
0086 feel free to submit patches.
0087
0088 Working with existing buffers
0089 =============================
0090
0091 Existing buffers aren't usable for DMA without first being mapped into the
0092 DMA address space of the device. However, most buffers passed to your
0093 driver can safely be used with such DMA mapping. (See the first section
0094 of Documentation/core-api/dma-api-howto.rst, titled "What memory is DMA-able?")
0095
0096 - When you're using scatterlists, you can map everything at once. On some
0097 systems, this kicks in an IOMMU and turns the scatterlists into single
0098 DMA transactions::
0099
0100 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
0101 struct scatterlist *sg, int nents);
0102
0103 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
0104 struct scatterlist *sg, int n_hw_ents);
0105
0106 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
0107 struct scatterlist *sg, int n_hw_ents);
0108
0109 It's probably easier to use the new ``usb_sg_*()`` calls, which do the DMA
0110 mapping and apply other tweaks to make scatterlist i/o be fast.
0111
0112 - Some drivers may prefer to work with the model that they're mapping large
0113 buffers, synchronizing their safe re-use. (If there's no re-use, then let
0114 usbcore do the map/unmap.) Large periodic transfers make good examples
0115 here, since it's cheaper to just synchronize the buffer than to unmap it
0116 each time an urb completes and then re-map it on during resubmission.
0117
0118 These calls all work with initialized urbs: ``urb->dev``, ``urb->pipe``,
0119 ``urb->transfer_buffer``, and ``urb->transfer_buffer_length`` must all be
0120 valid when these calls are used (``urb->setup_packet`` must be valid too
0121 if urb is a control request)::
0122
0123 struct urb *usb_buffer_map (struct urb *urb);
0124
0125 void usb_buffer_dmasync (struct urb *urb);
0126
0127 void usb_buffer_unmap (struct urb *urb);
0128
0129 The calls manage ``urb->transfer_dma`` for you, and set
0130 ``URB_NO_TRANSFER_DMA_MAP`` so that usbcore won't map or unmap the buffer.
0131 They cannot be used for setup_packet buffers in control requests.
0132
0133 Note that several of those interfaces are currently commented out, since
0134 they don't have current users. See the source code. Other than the dmasync
0135 calls (where the underlying DMA primitives have changed), most of them can
0136 easily be commented back in if you want to use them.