Implement 'map_peer_resource' for the Intel IOMMU driver. Simply translate
the resource to a physical address and route it to the same handlers used
by the 'map_page' API.

This allows a device to map another's resource, to enable peer-to-peer
transactions.

Add behind CONFIG_HAS_DMA_P2P guards, since the dma_map_ops members are
behind them as well.

Signed-off-by: Will Davis <wda...@nvidia.com>
Reviewed-by: Terence Ripperda <trippe...@nvidia.com>
Reviewed-by: John Hubbard <jhubb...@nvidia.com>
---
 drivers/iommu/intel-iommu.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index a98a7b2..0d3746f 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -3544,6 +3544,40 @@ static void intel_unmap_page(struct device *dev, 
dma_addr_t dev_addr,
        intel_unmap(dev, dev_addr);
 }
 
+#ifdef CONFIG_HAS_DMA_P2P
+static dma_peer_addr_t intel_map_peer_resource(struct device *dev,
+                                              struct device *peer,
+                                              struct resource *res,
+                                              unsigned long offset,
+                                              size_t size,
+                                              enum dma_data_direction dir,
+                                              struct dma_attrs *attrs)
+{
+       struct pci_dev *pdev;
+       struct pci_dev *ppeer;
+
+       if (!dev_is_pci(dev) || !dev_is_pci(peer))
+               return 0;
+
+       pdev = to_pci_dev(dev);
+       ppeer = to_pci_dev(peer);
+
+       if (!pci_peer_traffic_supported(pdev, ppeer))
+               return 0;
+
+       return __intel_map_single(dev, res->start + offset, size,
+                                 dir, *dev->dma_mask);
+}
+
+static void intel_unmap_peer_resource(struct device *dev,
+                                     dma_peer_addr_t dev_addr,
+                                     size_t size, enum dma_data_direction dir,
+                                     struct dma_attrs *attrs)
+{
+       intel_unmap(dev, dev_addr);
+}
+#endif
+
 static void *intel_alloc_coherent(struct device *dev, size_t size,
                                  dma_addr_t *dma_handle, gfp_t flags,
                                  struct dma_attrs *attrs)
@@ -3700,6 +3734,10 @@ struct dma_map_ops intel_dma_ops = {
        .unmap_sg = intel_unmap_sg,
        .map_page = intel_map_page,
        .unmap_page = intel_unmap_page,
+#ifdef CONFIG_HAS_DMA_P2P
+       .map_peer_resource = intel_map_peer_resource,
+       .unmap_peer_resource = intel_unmap_peer_resource,
+#endif
        .mapping_error = intel_mapping_error,
 };
 
-- 
2.5.1

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

Reply via email to