The sg_table is a poor fit for what DMA-buf exists to exchange. Every
importer has to walk a scatterlist to get at the addresses it actually
wants. Replace it with a simple list of DMA address regions.

Add dma_buf_map_dma_list() and dma_buf_unmap_dma_list() as the
alternative to dma_buf_map_sgt(). They return an array of DMA address
ranges, valid in the address space of the attached device, and the
total size. The importer holds the list until unmap. Pin-on-map and the
fence wait for static importers work the same as for dma_buf_map_sgt().

No exporter changes are required for compatibility with importers
using dma_buf_map_dma_list(). A converted exporter already returning a
dma_shared_buf uses that to create a dma_addr_list directly, otherwise
the sg_table is converted to a dma_addr_list.

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

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 799be8cad3ca..395903fea45e 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1001,6 +1001,51 @@ struct dma_buf_dma {
        struct dma_addr_list *addrs;
 };
 
+/*
+ * The DMA address list of an exporter that maps an sg_table itself: the
+ * table's DMA side copied out, the table kept until unmap. The exporter
+ * cannot tell a list importer from a table one.
+ */
+static struct dma_addr_list *
+dma_buf_sgt_map_dma_list(struct dma_buf_attachment *attach,
+                        enum dma_data_direction dir)
+{
+       const struct dma_buf_ops *ops = attach->dmabuf->ops;
+       struct dma_addr_list *addrs;
+       struct scatterlist *sg;
+       struct sg_table *sgt;
+       int i;
+
+       sgt = ops->map_dma_buf(attach, dir);
+       if (!sgt)
+               return ERR_PTR(-ENOMEM);
+       if (IS_ERR(sgt))
+               return ERR_CAST(sgt);
+
+       addrs = kvzalloc(struct_size(addrs, ranges, sgt->nents), GFP_KERNEL);
+       if (!addrs) {
+               ops->unmap_dma_buf(attach, sgt, dir);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       addrs->nr_ranges = sgt->nents;
+       for_each_sgtable_dma_sg(sgt, sg, i) {
+               addrs->ranges[i].addr = sg_dma_address(sg);
+               addrs->ranges[i].len = sg_dma_len(sg);
+               addrs->size += sg_dma_len(sg);
+       }
+       addrs->priv = sgt;
+       return addrs;
+}
+
+static void dma_buf_sgt_unmap_dma_list(struct dma_buf_attachment *attach,
+                                      struct dma_addr_list *addrs,
+                                      enum dma_data_direction dir)
+{
+       attach->dmabuf->ops->unmap_dma_buf(attach, addrs->priv, dir);
+       kvfree(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.
@@ -1117,6 +1162,8 @@ static long dma_buf_map_wait(struct dma_buf_attachment 
*attach)
  *     - dma_buf_unpin()
  *     - dma_buf_map_sgt()
  *     - dma_buf_unmap_sgt()
+ *     - dma_buf_map_dma_list()
+ *     - dma_buf_unmap_dma_list()
  *     - dma_buf_vmap()
  *     - dma_buf_vunmap()
  *
@@ -1135,6 +1182,8 @@ static long dma_buf_map_wait(struct dma_buf_attachment 
*attach)
  *     - dma_buf_end_cpu_access()
  *     - dma_buf_map_sgt_unlocked()
  *     - dma_buf_unmap_sgt_unlocked()
+ *     - dma_buf_map_dma_list_unlocked()
+ *     - dma_buf_unmap_dma_list_unlocked()
  *     - dma_buf_vmap_unlocked()
  *     - dma_buf_vunmap_unlocked()
  *
@@ -1486,6 +1535,148 @@ void dma_buf_unmap_sgt_unlocked(struct 
dma_buf_attachment *attach,
 }
 EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt_unlocked, "DMA_BUF");
 
+/**
+ * dma_buf_map_dma_list - Returns the buffer's DMA addresses for the 
attachment's
+ * device, without a scatterlist
+ * @attach:    [in]    attachment whose DMA addresses are to be returned
+ * @direction: [in]    direction of DMA transfer
+ *
+ * The alternative to dma_buf_map_sgt() for importers that want the DMA
+ * addresses and nothing else: an array of ranges, valid in the DMA address
+ * space of the attached device, with no struct pages and no scatterlist behind
+ * them. The importer reads the size and the ranges of the list and nothing
+ * else, and nothing in it is the importer's to free. The same rules apply as
+ * for dma_buf_map_sgt(): the dma_resv lock is held, the backing storage stays
+ * pinned for as long as the mapping exists, and dynamic importers must wait 
for
+ * the exclusive fence of the buffer's &dma_resv first.
+ *
+ * Returns: the list, valid until dma_buf_unmap_dma_list(), or an ERR_PTR on
+ * error. May return -EINTR if it is interrupted by a signal.
+ */
+const struct dma_addr_list *
+dma_buf_map_dma_list(struct dma_buf_attachment *attach,
+                    enum dma_data_direction direction)
+{
+       struct dma_addr_list *list;
+       long ret;
+
+       might_sleep();
+
+       if (WARN_ON(!attach || !attach->dmabuf))
+               return ERR_PTR(-EINVAL);
+
+       dma_resv_assert_held(attach->dmabuf->resv);
+
+       ret = dma_buf_map_pin(attach);
+       if (ret)
+               return ERR_PTR(ret);
+
+       if (attach->dmabuf->ops->map_dma_shared_buf)
+               list = dma_buf_map_list(attach, direction);
+       else
+               list = dma_buf_sgt_map_dma_list(attach, direction);
+       if (IS_ERR(list))
+               goto error_unpin;
+
+       ret = dma_buf_map_wait(attach);
+       if (ret < 0) {
+               if (attach->dmabuf->ops->map_dma_shared_buf)
+                       dma_buf_unmap_list(attach, list, direction);
+               else
+                       dma_buf_sgt_unmap_dma_list(attach, list, direction);
+               list = ERR_PTR(ret);
+               goto error_unpin;
+       }
+       return list;
+
+error_unpin:
+       dma_buf_map_unpin(attach);
+       return list;
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_dma_list, "DMA_BUF");
+
+/**
+ * dma_buf_map_dma_list_unlocked - Returns the buffer's DMA addresses for the
+ * attachment's device, without a scatterlist
+ * @attach:    [in]    attachment whose DMA addresses are to be returned
+ * @direction: [in]    direction of DMA transfer
+ *
+ * Unlocked variant of dma_buf_map_dma_list().
+ *
+ * Returns: the list, valid until dma_buf_unmap_dma_list_unlocked(), or an
+ * ERR_PTR on error.
+ */
+const struct dma_addr_list *
+dma_buf_map_dma_list_unlocked(struct dma_buf_attachment *attach,
+                             enum dma_data_direction direction)
+{
+       const struct dma_addr_list *list;
+
+       might_sleep();
+
+       if (WARN_ON(!attach || !attach->dmabuf))
+               return ERR_PTR(-EINVAL);
+
+       dma_resv_lock(attach->dmabuf->resv, NULL);
+       list = dma_buf_map_dma_list(attach, direction);
+       dma_resv_unlock(attach->dmabuf->resv);
+
+       return list;
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_map_dma_list_unlocked, "DMA_BUF");
+
+/**
+ * dma_buf_unmap_dma_list - Release a mapping from dma_buf_map_dma_list()
+ * @attach:    [in]    attachment to unmap buffer from
+ * @list:      [in]    the list dma_buf_map_dma_list() returned
+ * @direction: [in]    direction of DMA transfer
+ */
+void dma_buf_unmap_dma_list(struct dma_buf_attachment *attach,
+                           const struct dma_addr_list *list,
+                           enum dma_data_direction direction)
+{
+       /* The list is the core's own: only the importer's view is const */
+       struct dma_addr_list *addrs = (struct dma_addr_list *)list;
+
+       might_sleep();
+
+       if (WARN_ON(!attach || !attach->dmabuf || !list))
+               return;
+
+       dma_resv_assert_held(attach->dmabuf->resv);
+
+       if (attach->dmabuf->ops->map_dma_shared_buf)
+               dma_buf_unmap_list(attach, addrs, direction);
+       else
+               dma_buf_sgt_unmap_dma_list(attach, addrs, direction);
+       dma_buf_map_unpin(attach);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_dma_list, "DMA_BUF");
+
+/**
+ * dma_buf_unmap_dma_list_unlocked - Release a mapping from
+ * dma_buf_map_dma_list_unlocked()
+ * @attach:    [in]    attachment to unmap buffer from
+ * @list:      [in]    the list dma_buf_map_dma_list_unlocked() returned
+ * @direction: [in]    direction of DMA transfer
+ *
+ * Unlocked variant of dma_buf_unmap_dma_list().
+ */
+void dma_buf_unmap_dma_list_unlocked(struct dma_buf_attachment *attach,
+                                    const struct dma_addr_list *list,
+                                    enum dma_data_direction direction)
+{
+       might_sleep();
+
+       if (WARN_ON(!attach || !attach->dmabuf || !list))
+               return;
+
+       dma_resv_lock(attach->dmabuf->resv, NULL);
+       dma_buf_unmap_dma_list(attach, list, direction);
+       dma_resv_unlock(attach->dmabuf->resv);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_dma_list_unlocked, "DMA_BUF");
+
 /**
  * dma_buf_attach_revocable - check if a DMA-buf importer implements
  * revoke semantics.
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 719296a3d47e..c20dae88bf05 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -612,6 +612,18 @@ dma_buf_map_sgt_unlocked(struct dma_buf_attachment *attach,
 void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
                                struct sg_table *sg_table,
                                enum dma_data_direction direction);
+const struct dma_addr_list *
+dma_buf_map_dma_list(struct dma_buf_attachment *attach,
+                    enum dma_data_direction direction);
+void dma_buf_unmap_dma_list(struct dma_buf_attachment *attach,
+                           const struct dma_addr_list *list,
+                           enum dma_data_direction direction);
+const struct dma_addr_list *
+dma_buf_map_dma_list_unlocked(struct dma_buf_attachment *attach,
+                             enum dma_data_direction direction);
+void dma_buf_unmap_dma_list_unlocked(struct dma_buf_attachment *attach,
+                                    const struct dma_addr_list *list,
+                                    enum dma_data_direction direction);
 
 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
                 unsigned long);

-- 
2.54.0

Reply via email to