dma.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 DMA-mapping.txt).
  7. That's how they've worked through the 2.4 (and earlier) kernels.
  8. OR: they can now be DMA-aware.
  9. - New calls enable DMA-aware drivers, letting them allocate dma buffers and
  10. manage dma mappings for existing dma-ready buffers (see below).
  11. - URBs have an additional "transfer_dma" field, as well as a transfer_flags
  12. bit saying if it's valid. (Control requests also have "setup_dma" and a
  13. corresponding transfer_flags bit.)
  14. - "usbcore" will map those DMA addresses, if a DMA-aware driver didn't do
  15. it first and set URB_NO_TRANSFER_DMA_MAP or URB_NO_SETUP_DMA_MAP. HCDs
  16. don't manage dma mappings for URBs.
  17. - There's a new "generic DMA API", parts of which are usable by USB device
  18. drivers. Never use dma_set_mask() on any USB interface or device; that
  19. would potentially break all devices sharing that bus.
  20. ELIMINATING COPIES
  21. It's good to avoid making CPUs copy data needlessly. The costs can add up,
  22. and effects like cache-trashing can impose subtle penalties.
  23. - When you're allocating a buffer for DMA purposes anyway, use the buffer
  24. primitives. Think of them as kmalloc and kfree that give you the right
  25. kind of addresses to store in urb->transfer_buffer and urb->transfer_dma,
  26. while guaranteeing that no hidden copies through DMA "bounce" buffers will
  27. slow things down. You'd also set URB_NO_TRANSFER_DMA_MAP in
  28. urb->transfer_flags:
  29. void *usb_buffer_alloc (struct usb_device *dev, size_t size,
  30. int mem_flags, dma_addr_t *dma);
  31. void usb_buffer_free (struct usb_device *dev, size_t size,
  32. void *addr, dma_addr_t dma);
  33. For control transfers you can use the buffer primitives or not for each
  34. of the transfer buffer and setup buffer independently. Set the flag bits
  35. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP to indicate which
  36. buffers you have prepared. For non-control transfers URB_NO_SETUP_DMA_MAP
  37. is ignored.
  38. The memory buffer returned is "dma-coherent"; sometimes you might need to
  39. force a consistent memory access ordering by using memory barriers. It's
  40. not using a streaming DMA mapping, so it's good for small transfers on
  41. systems where the I/O would otherwise tie up an IOMMU mapping. (See
  42. Documentation/DMA-mapping.txt for definitions of "coherent" and "streaming"
  43. DMA mappings.)
  44. Asking for 1/Nth of a page (as well as asking for N pages) is reasonably
  45. space-efficient.
  46. - Devices on some EHCI controllers could handle DMA to/from high memory.
  47. Driver probe() routines can notice this using a generic DMA call, then
  48. tell higher level code (network, scsi, etc) about it like this:
  49. if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
  50. net->features |= NETIF_F_HIGHDMA;
  51. That can eliminate dma bounce buffering of requests that originate (or
  52. terminate) in high memory, in cases where the buffers aren't allocated
  53. with usb_buffer_alloc() but instead are dma-mapped.
  54. WORKING WITH EXISTING BUFFERS
  55. Existing buffers aren't usable for DMA without first being mapped into the
  56. DMA address space of the device.
  57. - When you're using scatterlists, you can map everything at once. On some
  58. systems, this kicks in an IOMMU and turns the scatterlists into single
  59. DMA transactions:
  60. int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
  61. struct scatterlist *sg, int nents);
  62. void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
  63. struct scatterlist *sg, int n_hw_ents);
  64. void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
  65. struct scatterlist *sg, int n_hw_ents);
  66. It's probably easier to use the new usb_sg_*() calls, which do the DMA
  67. mapping and apply other tweaks to make scatterlist i/o be fast.
  68. - Some drivers may prefer to work with the model that they're mapping large
  69. buffers, synchronizing their safe re-use. (If there's no re-use, then let
  70. usbcore do the map/unmap.) Large periodic transfers make good examples
  71. here, since it's cheaper to just synchronize the buffer than to unmap it
  72. each time an urb completes and then re-map it on during resubmission.
  73. These calls all work with initialized urbs: urb->dev, urb->pipe,
  74. urb->transfer_buffer, and urb->transfer_buffer_length must all be
  75. valid when these calls are used (urb->setup_packet must be valid too
  76. if urb is a control request):
  77. struct urb *usb_buffer_map (struct urb *urb);
  78. void usb_buffer_dmasync (struct urb *urb);
  79. void usb_buffer_unmap (struct urb *urb);
  80. The calls manage urb->transfer_dma for you, and set URB_NO_TRANSFER_DMA_MAP
  81. so that usbcore won't map or unmap the buffer. The same goes for
  82. urb->setup_dma and URB_NO_SETUP_DMA_MAP for control requests.