Currently exporters must create an sg_table even if they can't use the
DMA API to create them. For example vfio-pci and uverbs export MMIO
which don't have struct page and call dma_buf_phys_vec_to_sgt() from
their map_dma_buf() to create it for the importing device. iommufd,
which needs to program domains itself, reaches it through a private vfio
interface since no dma-buf API hands out the address of a buffer.

Add a second type of dma_buf_op - {un}map_dma_shared_buf() - which
allows exporters to return a dma_shared_buf describing the memory to
be mapped instead of a sg_table. The dma-shared-buf core can then be
used to map that correctly for the given device and convert it into a
list of dma addresses for importers to program.

This will allow for the eventual removal of dma_buf_{un}map_sgt() and
the associated sg_table handling in dma-buf exporters and importers once
all have been converted to use the new operations.

Co-developed-by: Jason Gunthorpe <[email protected]>
Signed-off-by: Jason Gunthorpe <[email protected]>
Signed-off-by: Alistair Popple <[email protected]>
---
 drivers/dma-buf/dma-buf.c | 134 ++++++++++++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h   |  30 +++++++++--
 2 files changed, 156 insertions(+), 8 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 0b8da0c0bc5f..70058509c397 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -714,11 +714,17 @@ struct dma_buf *dma_buf_export(const struct 
dma_buf_export_info *exp_info)
        int ret;
 
        if (WARN_ON(!exp_info->priv || !exp_info->ops
-                   || !exp_info->ops->map_dma_buf
-                   || !exp_info->ops->unmap_dma_buf
                    || !exp_info->ops->release))
                return ERR_PTR(-EINVAL);
 
+       /* Exactly one of the two map and unmap pairs */
+       if (WARN_ON(!exp_info->ops->map_dma_buf != 
!exp_info->ops->unmap_dma_buf ||
+                   !exp_info->ops->map_dma_shared_buf !=
+                   !exp_info->ops->unmap_dma_shared_buf ||
+                   !exp_info->ops->map_dma_buf ==
+                   !exp_info->ops->map_dma_shared_buf))
+               return ERR_PTR(-EINVAL);
+
        if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
                return ERR_PTR(-EINVAL);
 
@@ -933,6 +939,122 @@ static void dma_buf_unwrap_sg_table(struct sg_table 
**sg_table)
        kfree(copy);
 }
 
+/*
+ * Get a list of DMA addresses to program into importing device hardware from 
an
+ * exporter that provides a dma_shared_buf.
+ */
+static struct dma_addr_list *
+dma_buf_map_list(struct dma_buf_attachment *attach,
+                enum dma_data_direction dir)
+{
+       struct dma_shared_buf *shared_buf;
+       struct dma_addr_list *addrs;
+
+       dma_resv_assert_held(attach->dmabuf->resv);
+
+       shared_buf = attach->dmabuf->ops->map_dma_shared_buf(attach);
+       if (!shared_buf)
+               return ERR_PTR(-ENOMEM);
+       if (IS_ERR(shared_buf))
+               return ERR_CAST(shared_buf);
+
+       /*
+        * MMIO goes only to importers prepared for memory without struct
+        * pages.
+        */
+       if (shared_buf->provider && !attach->peer2peer) {
+               addrs = ERR_PTR(-EOPNOTSUPP);
+               goto err_unmap_list;
+       }
+
+       addrs = dma_shared_buf_map(attach->dev, shared_buf, dir);
+       if (IS_ERR(addrs))
+               goto err_unmap_list;
+
+       addrs->priv = shared_buf;
+       return addrs;
+
+err_unmap_list:
+       attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+       return addrs;
+}
+
+static void dma_buf_unmap_list(struct dma_buf_attachment *attach,
+                              struct dma_addr_list *addrs,
+                              enum dma_data_direction dir)
+{
+       struct dma_shared_buf *shared_buf = addrs->priv;
+
+       dma_shared_buf_unmap(attach->dev, addrs, dir);
+       attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+}
+
+/**
+ * struct dma_buf_dma - the sg_table the core builds for an exporter that hands
+ *                      out a dma_shared_buf
+ * @sgt: A DMA-only scatterlist over the ranges of @addrs, built by
+ *       dma_addr_list_to_sgt()
+ * @addrs: The DMA addresses the exporter's dma_shared_buf was mapped to
+ */
+struct dma_buf_dma {
+       struct sg_table sgt;
+       struct dma_addr_list *addrs;
+};
+
+/*
+ * The sg_table of an attachment: made by the exporter's map_dma_buf(), or by
+ * the core from the dma_shared_buf its map_dma_shared_buf() hands out.
+ * Importers see no difference.
+ */
+static struct sg_table *
+dma_buf_map_sg_table(struct dma_buf_attachment *attach,
+                    enum dma_data_direction dir)
+{
+       struct dma_buf_dma *dma;
+       int ret;
+
+       if (attach->dmabuf->ops->map_dma_buf)
+               return attach->dmabuf->ops->map_dma_buf(attach, dir);
+
+       dma = kzalloc_obj(*dma);
+       if (!dma)
+               return ERR_PTR(-ENOMEM);
+
+       dma->addrs = dma_buf_map_list(attach, dir);
+       if (IS_ERR(dma->addrs)) {
+               ret = PTR_ERR(dma->addrs);
+               goto err_free;
+       }
+
+       ret = dma_addr_list_to_sgt(&dma->sgt, dma->addrs);
+       if (ret)
+               goto err_unmap;
+       return &dma->sgt;
+
+err_unmap:
+       dma_buf_unmap_list(attach, dma->addrs, dir);
+err_free:
+       kfree(dma);
+       return ERR_PTR(ret);
+}
+
+static void dma_buf_unmap_sg_table(struct dma_buf_attachment *attach,
+                                  struct sg_table *sg_table,
+                                  enum dma_data_direction dir)
+{
+       struct dma_buf_dma *dma;
+
+       if (attach->dmabuf->ops->unmap_dma_buf) {
+               attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, dir);
+               return;
+       }
+
+       dma = container_of(sg_table, struct dma_buf_dma, sgt);
+       dma_addr_list_free_sgt(&dma->sgt);
+       dma_buf_unmap_list(attach, dma->addrs, dir);
+       kfree(dma);
+}
+
 static inline bool
 dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
 {
@@ -1001,6 +1123,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
  *     - &dma_buf_ops.unpin()
  *     - &dma_buf_ops.map_dma_buf()
  *     - &dma_buf_ops.unmap_dma_buf()
+ *     - &dma_buf_ops.map_dma_shared_buf()
+ *     - &dma_buf_ops.unmap_dma_shared_buf()
  *     - &dma_buf_ops.vmap()
  *     - &dma_buf_ops.vunmap()
  *
@@ -1213,7 +1337,7 @@ struct sg_table *dma_buf_map_sgt(struct 
dma_buf_attachment *attach,
                        return ERR_PTR(ret);
        }
 
-       sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
+       sg_table = dma_buf_map_sg_table(attach, direction);
        if (!sg_table)
                sg_table = ERR_PTR(-ENOMEM);
        if (IS_ERR(sg_table))
@@ -1252,7 +1376,7 @@ struct sg_table *dma_buf_map_sgt(struct 
dma_buf_attachment *attach,
        return sg_table;
 
 error_unmap:
-       attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+       dma_buf_unmap_sg_table(attach, sg_table, direction);
        sg_table = ERR_PTR(ret);
 
 error_unpin:
@@ -1313,7 +1437,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
        dma_resv_assert_held(attach->dmabuf->resv);
 
        dma_buf_unwrap_sg_table(&sg_table);
-       attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+       dma_buf_unmap_sg_table(attach, sg_table, direction);
 
        if (dma_buf_pin_on_map(attach))
                attach->dmabuf->ops->unpin(attach);
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index b138b77212c9..719296a3d47e 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -19,6 +19,7 @@
 #include <linux/scatterlist.h>
 #include <linux/list.h>
 #include <linux/dma-mapping.h>
+#include <linux/dma-shared-buf.h>
 #include <linux/fs.h>
 #include <linux/dma-fence.h>
 #include <linux/wait.h>
@@ -117,8 +118,10 @@ struct dma_buf_ops {
         * @map_dma_buf:
         *
         * This is called by dma_buf_map_sgt() and is used to map a
-        * shared &dma_buf into device address space, and it is mandatory. It
-        * can only be called if @attach has been called successfully.
+        * shared &dma_buf into device address space. It can only be called if
+        * @attach has been called successfully. An exporter provides either
+        * this and @unmap_dma_buf or @map_dma_shared_buf and
+        * @unmap_dma_shared_buf, see there.
         *
         * This call may sleep, e.g. when the backing storage first needs to be
         * allocated, or moved to a location suitable for all currently attached
@@ -174,7 +177,7 @@ struct dma_buf_ops {
         * @unmap_dma_buf:
         *
         * This is called by dma_buf_unmap_sgt() and should unmap and
-        * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
+        * release the &sg_table allocated in @map_dma_buf, and goes with it.
         * For static dma_buf handling this might also unpin the backing
         * storage if this is the last mapping of the DMA buffer.
         */
@@ -182,6 +185,27 @@ struct dma_buf_ops {
                              struct sg_table *,
                              enum dma_data_direction);
 
+       /**
+        * @map_dma_shared_buf:
+        *
+        * The alternative to @map_dma_buf for an exporter that provides a
+        * dma_shared_buf instead of a sg_table.
+        *
+        * Returns the dma_shared_buf, or a negative error value wrapped into a
+        * pointer.
+        */
+       struct dma_shared_buf *
+       (*map_dma_shared_buf)(struct dma_buf_attachment *attach);
+
+       /**
+        * @unmap_dma_shared_buf:
+        *
+        * Release a dma_shared_buf returned by @map_dma_shared_buf. The core
+        * holds none of its addresses once this is called.
+        */
+       void (*unmap_dma_shared_buf)(struct dma_buf_attachment *attach,
+                                    struct dma_shared_buf *shared_buf);
+
        /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
         * if the call would block.
         */

-- 
2.54.0

Reply via email to