dma_shared_buf_map() maps a dma_shared_buf for a device through the DMA
API. However a caller such as iommufd manages IOMMU domains itself and
has no device to map for. It needs the memory a dma_shared_buf describes
programmed into a domain at an IOVA of its choosing.

Add dma_shared_buf_iommu_map() to map a window of a dma_shared_buf into
an IOMMU domain. The window is mapped IOMMU_MMIO when the dma_shared_buf
has a p2pdma provider. Nothing is left mapped if it fails. There is no
matching unmap function as nothing is kept for the mapping, so the
caller undoes it with iommu_unmap().

Signed-off-by: Alistair Popple <[email protected]>
---
 drivers/dma-buf/dma-shared-buf.c | 71 ++++++++++++++++++++++++++++++++++++++++
 include/linux/dma-shared-buf.h   |  4 +++
 2 files changed, 75 insertions(+)

diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index 75ab08828ee8..d5d179b771f2 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -4,6 +4,7 @@
  * be mapped for whichever device needs to access it.
  */
 #include <linux/dma-shared-buf.h>
+#include <linux/iommu.h>
 #include <linux/overflow.h>
 #include <linux/pci-p2pdma.h>
 #include <linux/slab.h>
@@ -136,3 +137,73 @@ void dma_shared_buf_unmap(struct device *dev, struct 
dma_addr_list *addrs,
        }
        kvfree(addrs);
 }
+
+/**
+ * dma_shared_buf_iommu_map - Map a window of a dma_shared_buf into an IOMMU
+ *                            domain
+ * @shared_buf: The memory to map
+ * @domain: The domain to program
+ * @iova: Where in @domain the window starts
+ * @offset: Where in @shared_buf the window starts
+ * @size: Length of the window
+ * @prot: IOMMU_READ/IOMMU_WRITE/... for the mapping. IOMMU_MMIO is added
+ *        and IOMMU_CACHE removed when @shared_buf is MMIO.
+ * @gfp: Allocation flags for the page tables
+ *
+ * For a caller managing an IOMMU domain itself rather than mapping for a
+ * device. Nothing is left mapped on failure and nothing of @shared_buf is
+ * referenced once this returns. The caller undoes the mapping with
+ * iommu_unmap().
+ *
+ * Returns: 0 on success, -EINVAL if the window is outside @shared_buf, or the
+ * error from iommu_map().
+ */
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+                            struct iommu_domain *domain, unsigned long iova,
+                            size_t offset, size_t size, int prot, gfp_t gfp)
+{
+       unsigned long start_iova = iova;
+       const struct phys_vec *vec;
+       size_t i;
+       int ret;
+
+       if (offset >= shared_buf->size || size > shared_buf->size - offset)
+               return -EINVAL;
+
+       if (shared_buf->provider) {
+               prot &= ~IOMMU_CACHE;
+               prot |= IOMMU_MMIO;
+       }
+
+       /* Seek to the range holding @offset, then map up to @size bytes */
+       for (i = 0, vec = shared_buf->ranges; i < shared_buf->nr_ranges;
+            i++, vec++) {
+               size_t len;
+
+               if (offset >= vec->len) {
+                       offset -= vec->len;
+                       continue;
+               }
+               if (!size)
+                       break;
+
+               len = min(size, vec->len - offset);
+               ret = iommu_map(domain, iova, vec->paddr + offset, len, prot,
+                               gfp);
+               if (ret)
+                       goto err_unmap;
+               iova += len;
+               size -= len;
+               offset = 0;
+       }
+       if (WARN_ON(size)) {
+               ret = -EINVAL;
+               goto err_unmap;
+       }
+       return 0;
+
+err_unmap:
+       if (iova != start_iova)
+               iommu_unmap(domain, start_iova, iova - start_iova);
+       return ret;
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 53f506e0cc85..2a3f6297d23f 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -9,6 +9,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/types.h>
 
+struct iommu_domain;
 struct p2pdma_provider;
 
 /**
@@ -73,5 +74,8 @@ dma_shared_buf_map(struct device *dev, const struct 
dma_shared_buf *shared_buf,
                   enum dma_data_direction dir);
 void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
                          enum dma_data_direction dir);
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+                            struct iommu_domain *domain, unsigned long iova,
+                            size_t offset, size_t size, int prot, gfp_t gfp);
 
 #endif /* _LINUX_DMA_SHARED_BUF_H */

-- 
2.54.0

Reply via email to