blob: 59d5aee89e37e9dfd5a3b3019035fb3d1af2e5ed [file] [log] [blame]
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -03001USB DMA
2~~~~~~~
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004In Linux 2.5 kernels (and later), USB device drivers have additional control
5over how DMA may be used to perform I/O operations. The APIs are detailed
6in the kernel usb programming guide (kerneldoc, from the source code).
7
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -03008API overview
9============
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11The big picture is that USB drivers can continue to ignore most DMA issues,
Randy Dunlap5872fb92009-01-29 16:28:02 -080012though they still must provide DMA-ready buffers (see
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030013``Documentation/DMA-API-HOWTO.txt``). That's how they've worked through
14the 2.4 (and earlier) kernels, or they can now be DMA-aware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030016DMA-aware usb drivers:
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
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
Alan Stern85bcb5e2010-04-30 16:35:37 -040022 bit saying if it's valid. (Control requests also have "setup_dma", but
23 drivers must not use it.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alan Stern85bcb5e2010-04-30 16:35:37 -040025- "usbcore" will map this DMA address, if a DMA-aware driver didn't do
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030026 it first and set ``URB_NO_TRANSFER_DMA_MAP``. HCDs
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 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
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030033Eliminating copies
34==================
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36It's good to avoid making CPUs copy data needlessly. The costs can add up,
37and effects like cache-trashing can impose subtle penalties.
38
David Brownellfbf54dd2007-07-01 23:33:12 -070039- 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.
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030047 You'd also set ``URB_NO_TRANSFER_DMA_MAP`` in urb->transfer_flags::
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Daniel Mack997ea582010-04-12 13:17:25 +020049 void *usb_alloc_coherent (struct usb_device *dev, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int mem_flags, dma_addr_t *dma);
51
Daniel Mack997ea582010-04-12 13:17:25 +020052 void usb_free_coherent (struct usb_device *dev, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 void *addr, dma_addr_t dma);
54
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030055 Most drivers should **NOT** be using these primitives; they don't need
David Brownellfbf54dd2007-07-01 23:33:12 -070056 to use this type of memory ("dma-coherent"), and memory returned from
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030057 :c:func:`kmalloc` will work just fine.
David Brownellfbf54dd2007-07-01 23:33:12 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
David Brownellfbf54dd2007-07-01 23:33:12 -070062 systems where the I/O would otherwise thrash an IOMMU mapping. (See
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030063 ``Documentation/DMA-API-HOWTO.txt`` for definitions of "coherent" and
Randy Dunlap5872fb92009-01-29 16:28:02 -080064 "streaming" DMA mappings.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
67 space-efficient.
68
David Brownellfbf54dd2007-07-01 23:33:12 -070069 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070075- Devices on some EHCI controllers could handle DMA to/from high memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
David Brownellfbf54dd2007-07-01 23:33:12 -070077 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
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030081 behavior. Just don't override it; e.g. with ``NETIF_F_HIGHDMA``.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
David Brownellfbf54dd2007-07-01 23:33:12 -070083 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030088Working with existing buffers
89=============================
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91Existing buffers aren't usable for DMA without first being mapped into the
David Brownellfbf54dd2007-07-01 23:33:12 -070092DMA address space of the device. However, most buffers passed to your
93driver can safely be used with such DMA mapping. (See the first section
Paul Bolle395cf962011-08-15 02:02:26 +020094of Documentation/DMA-API-HOWTO.txt, titled "What memory is DMA-able?")
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96- When you're using scatterlists, you can map everything at once. On some
97 systems, this kicks in an IOMMU and turns the scatterlists into single
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -030098 DMA transactions::
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
101 struct scatterlist *sg, int nents);
102
103 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
104 struct scatterlist *sg, int n_hw_ents);
105
106 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
107 struct scatterlist *sg, int n_hw_ents);
108
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -0300109 It's probably easier to use the new ``usb_sg_*()`` calls, which do the DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mapping and apply other tweaks to make scatterlist i/o be fast.
111
112- Some drivers may prefer to work with the model that they're mapping large
113 buffers, synchronizing their safe re-use. (If there's no re-use, then let
114 usbcore do the map/unmap.) Large periodic transfers make good examples
115 here, since it's cheaper to just synchronize the buffer than to unmap it
116 each time an urb completes and then re-map it on during resubmission.
117
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -0300118 These calls all work with initialized urbs: ``urb->dev``, ``urb->pipe``,
119 ``urb->transfer_buffer``, and ``urb->transfer_buffer_length`` must all be
120 valid when these calls are used (``urb->setup_packet`` must be valid too
121 if urb is a control request)::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 struct urb *usb_buffer_map (struct urb *urb);
124
125 void usb_buffer_dmasync (struct urb *urb);
126
127 void usb_buffer_unmap (struct urb *urb);
128
Mauro Carvalho Chehab2a373332017-04-05 10:23:06 -0300129 The calls manage ``urb->transfer_dma`` for you, and set
130 ``URB_NO_TRANSFER_DMA_MAP`` so that usbcore won't map or unmap the buffer.
131 They cannot be used for setup_packet buffers in control requests.
David Brownellfbf54dd2007-07-01 23:33:12 -0700132
133Note that several of those interfaces are currently commented out, since
134they don't have current users. See the source code. Other than the dmasync
135calls (where the underlying DMA primitives have changed), most of them can
136easily be commented back in if you want to use them.