p2pdma.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============================
  3. PCI Peer-to-Peer DMA Support
  4. ============================
  5. The PCI bus has pretty decent support for performing DMA transfers
  6. between two devices on the bus. This type of transaction is henceforth
  7. called Peer-to-Peer (or P2P). However, there are a number of issues that
  8. make P2P transactions tricky to do in a perfectly safe way.
  9. One of the biggest issues is that PCI doesn't require forwarding
  10. transactions between hierarchy domains, and in PCIe, each Root Port
  11. defines a separate hierarchy domain. To make things worse, there is no
  12. simple way to determine if a given Root Complex supports this or not.
  13. (See PCIe r4.0, sec 1.3.1). Therefore, as of this writing, the kernel
  14. only supports doing P2P when the endpoints involved are all behind the
  15. same PCI bridge, as such devices are all in the same PCI hierarchy
  16. domain, and the spec guarantees that all transactions within the
  17. hierarchy will be routable, but it does not require routing
  18. between hierarchies.
  19. The second issue is that to make use of existing interfaces in Linux,
  20. memory that is used for P2P transactions needs to be backed by struct
  21. pages. However, PCI BARs are not typically cache coherent so there are
  22. a few corner case gotchas with these pages so developers need to
  23. be careful about what they do with them.
  24. Driver Writer's Guide
  25. =====================
  26. In a given P2P implementation there may be three or more different
  27. types of kernel drivers in play:
  28. * Provider - A driver which provides or publishes P2P resources like
  29. memory or doorbell registers to other drivers.
  30. * Client - A driver which makes use of a resource by setting up a
  31. DMA transaction to or from it.
  32. * Orchestrator - A driver which orchestrates the flow of data between
  33. clients and providers.
  34. In many cases there could be overlap between these three types (i.e.,
  35. it may be typical for a driver to be both a provider and a client).
  36. For example, in the NVMe Target Copy Offload implementation:
  37. * The NVMe PCI driver is both a client, provider and orchestrator
  38. in that it exposes any CMB (Controller Memory Buffer) as a P2P memory
  39. resource (provider), it accepts P2P memory pages as buffers in requests
  40. to be used directly (client) and it can also make use of the CMB as
  41. submission queue entries (orchestrator).
  42. * The RDMA driver is a client in this arrangement so that an RNIC
  43. can DMA directly to the memory exposed by the NVMe device.
  44. * The NVMe Target driver (nvmet) can orchestrate the data from the RNIC
  45. to the P2P memory (CMB) and then to the NVMe device (and vice versa).
  46. This is currently the only arrangement supported by the kernel but
  47. one could imagine slight tweaks to this that would allow for the same
  48. functionality. For example, if a specific RNIC added a BAR with some
  49. memory behind it, its driver could add support as a P2P provider and
  50. then the NVMe Target could use the RNIC's memory instead of the CMB
  51. in cases where the NVMe cards in use do not have CMB support.
  52. Provider Drivers
  53. ----------------
  54. A provider simply needs to register a BAR (or a portion of a BAR)
  55. as a P2P DMA resource using :c:func:`pci_p2pdma_add_resource()`.
  56. This will register struct pages for all the specified memory.
  57. After that it may optionally publish all of its resources as
  58. P2P memory using :c:func:`pci_p2pmem_publish()`. This will allow
  59. any orchestrator drivers to find and use the memory. When marked in
  60. this way, the resource must be regular memory with no side effects.
  61. For the time being this is fairly rudimentary in that all resources
  62. are typically going to be P2P memory. Future work will likely expand
  63. this to include other types of resources like doorbells.
  64. Client Drivers
  65. --------------
  66. A client driver only has to use the mapping API :c:func:`dma_map_sg()`
  67. and :c:func:`dma_unmap_sg()` functions as usual, and the implementation
  68. will do the right thing for the P2P capable memory.
  69. Orchestrator Drivers
  70. --------------------
  71. The first task an orchestrator driver must do is compile a list of
  72. all client devices that will be involved in a given transaction. For
  73. example, the NVMe Target driver creates a list including the namespace
  74. block device and the RNIC in use. If the orchestrator has access to
  75. a specific P2P provider to use it may check compatibility using
  76. :c:func:`pci_p2pdma_distance()` otherwise it may find a memory provider
  77. that's compatible with all clients using :c:func:`pci_p2pmem_find()`.
  78. If more than one provider is supported, the one nearest to all the clients will
  79. be chosen first. If more than one provider is an equal distance away, the
  80. one returned will be chosen at random (it is not an arbitrary but
  81. truly random). This function returns the PCI device to use for the provider
  82. with a reference taken and therefore when it's no longer needed it should be
  83. returned with pci_dev_put().
  84. Once a provider is selected, the orchestrator can then use
  85. :c:func:`pci_alloc_p2pmem()` and :c:func:`pci_free_p2pmem()` to
  86. allocate P2P memory from the provider. :c:func:`pci_p2pmem_alloc_sgl()`
  87. and :c:func:`pci_p2pmem_free_sgl()` are convenience functions for
  88. allocating scatter-gather lists with P2P memory.
  89. Struct Page Caveats
  90. -------------------
  91. Driver writers should be very careful about not passing these special
  92. struct pages to code that isn't prepared for it. At this time, the kernel
  93. interfaces do not have any checks for ensuring this. This obviously
  94. precludes passing these pages to userspace.
  95. P2P memory is also technically IO memory but should never have any side
  96. effects behind it. Thus, the order of loads and stores should not be important
  97. and ioreadX(), iowriteX() and friends should not be necessary.
  98. P2P DMA Support Library
  99. =======================
  100. .. kernel-doc:: drivers/pci/p2pdma.c
  101. :export: