dma.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. In Linux 2.5 kernels (and later), USB device drivers have additional control
  2. over how DMA may be used to perform I/O operations. The APIs are detailed
  3. in the kernel usb programming guide (kerneldoc, from the source code).
  4. API OVERVIEW
  5. The big picture is that USB drivers can continue to ignore most DMA issues,
  6. though they still must provide DMA-ready buffers (see
  7. Documentation/PCI/PCI-DMA-mapping.txt). That's how they've worked through
  8. the 2.4 (and earlier) kernels.
  9. OR: they can now be DMA-aware.
  10. - New calls enable DMA-aware drivers, letting them allocate dma buffers and
  11. manage dma mappings for existing dma-ready buffers (see below).
  12. - URBs have an additional "transfer_dma" field, as well as a transfer_flags
  13. bit saying if it's valid. (Control requests also have "setup_dma" and a
  14. corresponding transfer_flags bit.)
  15. - "usbcore" will map those DMA addresses, if a DMA-aware driver didn't do
  16. it first and set URB_NO_TRANSFER_DMA_MAP or URB_NO_SETUP_DMA_MAP. HCDs
  17. don't manage dma mappings for URBs.
  18. - There's a new "generic DMA API", parts of which are usable by USB device
  19. drivers. Never use dma_set_mask() on any USB interface or device; that
  20. would potentially break all devices sharing that bus.
  21. ELIMINATING COPIES
  22. It's good to avoid making CPUs copy data needlessly. The costs can add up,
  23. and effects like cache-trashing can impose subtle penalties.
  24. - If you're doing lots of small data transfers from the same buffer all
  25. the time, that can really burn up resources on systems which use an
  26. IOMMU to manage the DMA mappings. It can cost MUCH more to set up and
  27. tear down the IOMMU mappings with each request than perform the I/O!
  28. For those specific cases, USB has primitives to allocate less expensive
  29. memory. They work like kmalloc and kfree versions that give you the right
  30. kind of addresses to store in urb->transfer_buffer and urb->transfer_dma.
  31. You'd also set URB_NO_TRANSFER_DMA_MAP in urb->transfer_flags:
  32. void *usb_buffer_alloc (struct usb_device *dev, size_t size,
  33. int mem_flags, dma_addr_t *dma);
  34. void usb_buffer_free (struct usb_device *dev, size_t size,
  35. void *addr, dma_addr_t dma);
  36. Most drivers should *NOT* be using these primitives; they don't need
  37. to use this type of memory ("dma-coherent"), and memory returned from
  38. kmalloc() will work just fine.
  39. For control transfers you can use the buffer primitives or not for each
  40. of the transfer buffer and setup buffer independently. Set the flag bits
  41. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP to indicate which
  42. buffers you have prepared. For non-control transfers URB_NO_SETUP_DMA_MAP
  43. is ignored.
  44. The memory buffer returned is "dma-coherent"; sometimes you might need to
  45. force a consistent memory access ordering by using memory barriers. It's
  46. not using a streaming DMA mapping, so it's good for small transfers on
  47. systems where the I/O would otherwise thrash an IOMMU mapping. (See
  48. Documentation/PCI/PCI-DMA-mapping.txt for definitions of "coherent" and
  49. "streaming" DMA mappings.)
  50. Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
  51. space-efficient.
  52. On most systems the memory returned will be uncached, because the
  53. semantics of dma-coherent memory require either bypassing CPU caches
  54. or using cache hardware with bus-snooping support. While x86 hardware
  55. has such bus-snooping, many other systems use software to flush cache
  56. lines to prevent DMA conflicts.
  57. - Devices on some EHCI controllers could handle DMA to/from high memory.
  58. Unfortunately, the current Linux DMA infrastructure doesn't have a sane
  59. way to expose these capabilities ... and in any case, HIGHMEM is mostly a
  60. design wart specific to x86_32. So your best bet is to ensure you never
  61. pass a highmem buffer into a USB driver. That's easy; it's the default
  62. behavior. Just don't override it; e.g. with NETIF_F_HIGHDMA.
  63. This may force your callers to do some bounce buffering, copying from
  64. high memory to "normal" DMA memory. If you can come up with a good way
  65. to fix this issue (for x86_32 machines with over 1 GByte of memory),
  66. feel free to submit patches.
  67. WORKING WITH EXISTING BUFFERS
  68. Existing buffers aren't usable for DMA without first being mapped into the
  69. DMA address space of the device. However, most buffers passed to your
  70. driver can safely be used with such DMA mapping. (See the first section
  71. of Documentation/PCI/PCI-DMA-mapping.txt, titled "What memory is DMA-able?")
  72. - When you're using scatterlists, you can map everything at once. On some
  73. systems, this kicks in an IOMMU and turns the scatterlists into single
  74. DMA transactions:
  75. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  76. struct scatterlist *sg, int nents);
  77. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  78. struct scatterlist *sg, int n_hw_ents);
  79. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  80. struct scatterlist *sg, int n_hw_ents);
  81. It's probably easier to use the new usb_sg_*() calls, which do the DMA
  82. mapping and apply other tweaks to make scatterlist i/o be fast.
  83. - Some drivers may prefer to work with the model that they're mapping large
  84. buffers, synchronizing their safe re-use. (If there's no re-use, then let
  85. usbcore do the map/unmap.) Large periodic transfers make good examples
  86. here, since it's cheaper to just synchronize the buffer than to unmap it
  87. each time an urb completes and then re-map it on during resubmission.
  88. These calls all work with initialized urbs: urb->dev, urb->pipe,
  89. urb->transfer_buffer, and urb->transfer_buffer_length must all be
  90. valid when these calls are used (urb->setup_packet must be valid too
  91. if urb is a control request):
  92. struct urb *usb_buffer_map (struct urb *urb);
  93. void usb_buffer_dmasync (struct urb *urb);
  94. void usb_buffer_unmap (struct urb *urb);
  95. The calls manage urb->transfer_dma for you, and set URB_NO_TRANSFER_DMA_MAP
  96. so that usbcore won't map or unmap the buffer. The same goes for
  97. urb->setup_dma and URB_NO_SETUP_DMA_MAP for control requests.
  98. Note that several of those interfaces are currently commented out, since
  99. they don't have current users. See the source code. Other than the dmasync
  100. calls (where the underlying DMA primitives have changed), most of them can
  101. easily be commented back in if you want to use them.