USB DMA¶
In Linux 2.5 kernels (and later), USB device drivers have additional controlover how DMA may be used to perform I/O operations. The APIs are detailedin the kernel usb programming guide (kerneldoc, from the source code).
API overview¶
The big picture is that USB drivers can continue to ignore most DMA issues,though they still must provide DMA-ready buffers (seeDynamic DMA mapping Guide). That’s how they’ve worked throughthe 2.4 (and earlier) kernels, or they can now be DMA-aware.
DMA-aware usb drivers:
- New calls enable DMA-aware drivers, letting them allocate dma buffers andmanage dma mappings for existing dma-ready buffers (see below).
- URBs have an additional “transfer_dma” field, as well as a transfer_flagsbit saying if it’s valid. (Control requests also have “setup_dma”, butdrivers must not use it.)
- “usbcore” will map this DMA address, if a DMA-aware driver didn’t doit first and set
URB_NO_TRANSFER_DMA_MAP. HCDsdon’t manage dma mappings for URBs. - There’s a new “generic DMA API”, parts of which are usable by USB devicedrivers. Never use dma_set_mask() on any USB interface or device; thatwould potentially break all devices sharing that bus.
Eliminating copies¶
It’s good to avoid making CPUs copy data needlessly. The costs can add up,and effects like cache-trashing can impose subtle penalties.
If you’re doing lots of small data transfers from the same buffer allthe time, that can really burn up resources on systems which use anIOMMU to manage the DMA mappings. It can cost MUCH more to set up andtear down the IOMMU mappings with each request than perform the I/O!
For those specific cases, USB has primitives to allocate less expensivememory. They work like kmalloc and kfree versions that give you the rightkind of addresses to store in urb->transfer_buffer and urb->transfer_dma.You’d also set
URB_NO_TRANSFER_DMA_MAPin urb->transfer_flags:void *usb_alloc_coherent (struct usb_device *dev, size_t size, int mem_flags, dma_addr_t *dma);void usb_free_coherent (struct usb_device *dev, size_t size, void *addr, dma_addr_t dma);
Most drivers shouldNOT be using these primitives; they don’t needto use this type of memory (“dma-coherent”), and memory returned from
kmalloc()will work just fine.The memory buffer returned is “dma-coherent”; sometimes you might need toforce a consistent memory access ordering by using memory barriers. It’snot using a streaming DMA mapping, so it’s good for small transfers onsystems where the I/O would otherwise thrash an IOMMU mapping. (SeeDynamic DMA mapping Guide for definitions of “coherent” and“streaming” DMA mappings.)
Asking for 1/Nth of a page (as well as asking for N pages) is reasonablyspace-efficient.
On most systems the memory returned will be uncached, because thesemantics of dma-coherent memory require either bypassing CPU cachesor using cache hardware with bus-snooping support. While x86 hardwarehas such bus-snooping, many other systems use software to flush cachelines to prevent DMA conflicts.
Devices on some EHCI controllers could handle DMA to/from high memory.
Unfortunately, the current Linux DMA infrastructure doesn’t have a saneway to expose these capabilities … and in any case, HIGHMEM is mostly adesign wart specific to x86_32. So your best bet is to ensure you neverpass a highmem buffer into a USB driver. That’s easy; it’s the defaultbehavior. Just don’t override it; e.g. with
NETIF_F_HIGHDMA.This may force your callers to do some bounce buffering, copying fromhigh memory to “normal” DMA memory. If you can come up with a good wayto fix this issue (for x86_32 machines with over 1 GByte of memory),feel free to submit patches.
Working with existing buffers¶
Existing buffers aren’t usable for DMA without first being mapped into theDMA address space of the device. However, most buffers passed to yourdriver can safely be used with such DMA mapping. (See the first sectionofDynamic DMA mapping Guide, titled “What memory is DMA-able?”)
When you’re using scatterlists, you can map everything at once. On somesystems, this kicks in an IOMMU and turns the scatterlists into singleDMA transactions:
int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, struct scatterlist *sg, int nents);void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, struct scatterlist *sg, int n_hw_ents);void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, struct scatterlist *sg, int n_hw_ents);
It’s probably easier to use the new
usb_sg_*()calls, which do the DMAmapping and apply other tweaks to make scatterlist i/o be fast.Some drivers may prefer to work with the model that they’re mapping largebuffers, synchronizing their safe re-use. (If there’s no re-use, then letusbcore do the map/unmap.) Large periodic transfers make good exampleshere, since it’s cheaper to just synchronize the buffer than to unmap iteach time an urb completes and then re-map it on during resubmission.
These calls all work with initialized urbs:
urb->dev,urb->pipe,urb->transfer_buffer, andurb->transfer_buffer_lengthmust all bevalid when these calls are used (urb->setup_packetmust be valid tooif urb is a control request):struct urb *usb_buffer_map (struct urb *urb);void usb_buffer_dmasync (struct urb *urb);void usb_buffer_unmap (struct urb *urb);
The calls manage
urb->transfer_dmafor you, and setURB_NO_TRANSFER_DMA_MAPso that usbcore won’t map or unmap the buffer.They cannot be used for setup_packet buffers in control requests.
Note that several of those interfaces are currently commented out, sincethey don’t have current users. See the source code. Other than the dmasynccalls (where the underlying DMA primitives have changed), most of them caneasily be commented back in if you want to use them.