~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/driver-api/usb/dma.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 USB DMA
  2 ~~~~~~~
  3 
  4 In Linux 2.5 kernels (and later), USB device drivers have additional control
  5 over how DMA may be used to perform I/O operations.  The APIs are detailed
  6 in the kernel usb programming guide (kerneldoc, from the source code).
  7 
  8 API overview
  9 ============
 10 
 11 The big picture is that USB drivers can continue to ignore most DMA issues,
 12 though they still must provide DMA-ready buffers (see
 13 Documentation/core-api/dma-api-howto.rst).  That's how they've worked through
 14 the 2.4 (and earlier) kernels, or they can now be DMA-aware.
 15 
 16 DMA-aware usb drivers:
 17 
 18 - New calls enable DMA-aware drivers, letting them allocate dma buffers and
 19   manage dma mappings for existing dma-ready buffers (see below).
 20 
 21 - URBs have an additional "transfer_dma" field, as well as a transfer_flags
 22   bit saying if it's valid.  (Control requests also have "setup_dma", but
 23   drivers must not use it.)
 24 
 25 - "usbcore" will map this DMA address, if a DMA-aware driver didn't do
 26   it first and set ``URB_NO_TRANSFER_DMA_MAP``.  HCDs
 27   don't manage dma mappings for URBs.
 28 
 29 - There's a new "generic DMA API", parts of which are usable by USB device
 30   drivers.  Never use dma_set_mask() on any USB interface or device; that
 31   would potentially break all devices sharing that bus.
 32 
 33 Eliminating copies
 34 ==================
 35 
 36 It's good to avoid making CPUs copy data needlessly.  The costs can add up,
 37 and effects like cache-trashing can impose subtle penalties.
 38 
 39 - If you're doing lots of small data transfers from the same buffer all
 40   the time, that can really burn up resources on systems which use an
 41   IOMMU to manage the DMA mappings.  It can cost MUCH more to set up and
 42   tear down the IOMMU mappings with each request than perform the I/O!
 43 
 44   For those specific cases, USB has primitives to allocate less expensive
 45   memory.  They work like kmalloc and kfree versions that give you the right
 46   kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
 47   You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::
 48 
 49         void *usb_alloc_coherent (struct usb_device *dev, size_t size,
 50                 int mem_flags, dma_addr_t *dma);
 51 
 52         void usb_free_coherent (struct usb_device *dev, size_t size,
 53                 void *addr, dma_addr_t dma);
 54 
 55   Most drivers should **NOT** be using these primitives; they don't need
 56   to use this type of memory ("dma-coherent"), and memory returned from
 57   :c:func:`kmalloc` will work just fine.
 58 
 59   The memory buffer returned is "dma-coherent"; sometimes you might need to
 60   force a consistent memory access ordering by using memory barriers.  It's
 61   not using a streaming DMA mapping, so it's good for small transfers on
 62   systems where the I/O would otherwise thrash an IOMMU mapping.  (See
 63   Documentation/core-api/dma-api-howto.rst for definitions of "coherent" and
 64   "streaming" DMA mappings.)
 65 
 66   Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
 67   space-efficient.
 68 
 69   On most systems the memory returned will be uncached, because the
 70   semantics of dma-coherent memory require either bypassing CPU caches
 71   or using cache hardware with bus-snooping support.  While x86 hardware
 72   has such bus-snooping, many other systems use software to flush cache
 73   lines to prevent DMA conflicts.
 74 
 75 - Devices on some EHCI controllers could handle DMA to/from high memory.
 76 
 77   Unfortunately, the current Linux DMA infrastructure doesn't have a sane
 78   way to expose these capabilities ... and in any case, HIGHMEM is mostly a
 79   design wart specific to x86_32.  So your best bet is to ensure you never
 80   pass a highmem buffer into a USB driver.  That's easy; it's the default
 81   behavior.  Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.
 82 
 83   This may force your callers to do some bounce buffering, copying from
 84   high memory to "normal" DMA memory.  If you can come up with a good way
 85   to fix this issue (for x86_32 machines with over 1 GByte of memory),
 86   feel free to submit patches.
 87 
 88 Working with existing buffers
 89 =============================
 90 
 91 Existing buffers aren't usable for DMA without first being mapped into the
 92 DMA address space of the device.  However, most buffers passed to your
 93 driver can safely be used with such DMA mapping.  (See the first section
 94 of Documentation/core-api/dma-api-howto.rst, titled "What memory is DMA-able?")
 95 
 96 - When you have the scatterlists which have been mapped for the USB controller,
 97   you could use the new ``usb_sg_*()`` calls, which would turn scatterlist
 98   into URBs::
 99 
100         int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
101                 unsigned pipe, unsigned period, struct scatterlist *sg,
102                 int nents, size_t length, gfp_t mem_flags);
103 
104         void usb_sg_wait(struct usb_sg_request *io);
105 
106         void usb_sg_cancel(struct usb_sg_request *io);
107 
108   When the USB controller doesn't support DMA, the ``usb_sg_init()`` would try
109   to submit URBs in PIO way as long as the page in scatterlists is not in the
110   Highmem, which could be very rare in modern architectures.

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php