dma-buf: Add support for mapping buffers with DMA attributes
PATCH 1/1 dma-buf: Add support for mapping buffers with DMA attributes
From: Guangming Cao <[email protected]>
When mapping the memory represented by a dma-buf into a device's address space, it might be desireable to map the memory with certain DMA attributes. Thus, introduce the dma_mapping_attrs field in the dma_buf_attachment structure so that when the memory is mapped with dma_buf_map_attachment, it is mapped with the desired DMA attributes.
--- drivers/dma-buf/heaps/cma_heap.c | 6 ++++-- drivers/dma-buf/heaps/system_heap.c | 6 ++++-- include/linux/dma-buf.h | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-)
drivers/dma-buf/heaps/cma_heap.c +4 -2
@@ -99,9 +99,10 @@ static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachme {struct dma_heap_attachment *a = attachment->priv;struct sg_table *table = &a->table; +int attrs = attachment->dma_map_attrs;int ret; - ret =dma_map_sgtable (attachment->dev, table, direction,0 ); + ret =dma_map_sgtable (attachment->dev, table, direction, attrs);if (ret)return ERR_PTR (-ENOMEM ); a->mapped = true;
@@ -113,9 +114,10 @@ static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, enum dma_data_direction direction) {struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); } static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
drivers/dma-buf/heaps/system_heap.c +4 -2
@@ -130,9 +130,10 @@ static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attac {struct dma_heap_attachment *a = attachment->priv;struct sg_table *table = a->table; +int attrs = attachment->dma_map_attrs;int ret; - ret =dma_map_sgtable (attachment->dev, table, direction,0 ); + ret =dma_map_sgtable (attachment->dev, table, direction, attrs);if (ret)return ERR_PTR (ret);
@@ -145,9 +146,10 @@ static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, enum dma_data_direction direction) {struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); } static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
include/linux/dma-buf.h +3 -0
@@ -379,6 +379,8 @@ struct dma_buf_attach_ops {* @importer_ops: importer operations for this attachment, if provided * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held. * @importer_priv: importer specific attachment data. +* @dma_map_attrs: DMA attributes to be used when the exporter maps the buffer +* through dma_buf_map_attachment. * * This structure holds the attachment information between the dma_buf buffer * and its user device(s). The list contains one attachment struct per device
@@ -399,6 +401,7 @@ struct dma_buf_attachment {const struct dma_buf_attach_ops *importer_ops;void *importer_priv;void *priv; +unsigned long dma_map_attrs; };/**
Signature · 1 line
2.17.1
Quoted 9 lines · On Mon, Aug 30, 2021 at 10:39:11AM +0800, [email protected] wrote:
From: Guangming Cao <[email protected]>
When mapping the memory represented by a dma-buf into a device's address space, it might be desireable to map the memory with certain DMA attributes. Thus, introduce the
dma_mapping_attrsfield in thedma_buf_attachmentstructure so that when the memory is mapped withdma_buf_map_attachment, it is mapped with the desired DMA attributes.
Quoted 3 lines
Signed-off-by Isaac J. Manjarres <[email protected]>Signed-off-by Sandeep Patil <[email protected]>Signed-off-by Guangming Cao <[email protected]>
Can you pls include the code that's going to use this here too?
At a glance all the attributes you might want to set are supposed to be under the control of the exporter, not the importer.
-Daniel
Quoted 86 lines
--- drivers/dma-buf/heaps/cma_heap.c | 6 ++++-- drivers/dma-buf/heaps/system_heap.c | 6 ++++-- include/linux/dma-buf.h | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-)drivers/dma-buf/heaps/cma_heap.c +4
-2@@ -99,9 +99,10 @@ static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachme {struct dma_heap_attachment *a = attachment->priv;struct sg_table *table = &a->table; +int attrs = attachment->dma_map_attrs;int ret; - ret =dma_map_sgtable (attachment->dev, table, direction,0 ); + ret =dma_map_sgtable (attachment->dev, table, direction, attrs);if (ret)return ERR_PTR (-ENOMEM ); a->mapped = true;@@ -113,9 +114,10 @@ static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, enum dma_data_direction direction) {struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); } static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,drivers/dma-buf/heaps/system_heap.c +4
-2@@ -130,9 +130,10 @@ static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attac {struct dma_heap_attachment *a = attachment->priv;struct sg_table *table = a->table; +int attrs = attachment->dma_map_attrs;int ret; - ret =dma_map_sgtable (attachment->dev, table, direction,0 ); + ret =dma_map_sgtable (attachment->dev, table, direction, attrs);if (ret)return ERR_PTR (ret);@@ -145,9 +146,10 @@ static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, enum dma_data_direction direction) {struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); } static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,include/linux/dma-buf.h +3
-0@@ -379,6 +379,8 @@ struct dma_buf_attach_ops {* @importer_ops: importer operations for this attachment, if provided * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held. * @importer_priv: importer specific attachment data. +* @dma_map_attrs: DMA attributes to be used when the exporter maps the buffer +* through dma_buf_map_attachment. * * This structure holds the attachment information between the dma_buf buffer * and its user device(s). The list contains one attachment struct per device @@ -399,6 +401,7 @@ struct dma_buf_attachment {const struct dma_buf_attach_ops *importer_ops;void *importer_priv;void *priv; +unsigned long dma_map_attrs; };/** Signature · 1 line
2.17.1
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Quoted 15 lines · Daniel Vetter
Quoted 9 lines · On Mon, Aug 30, 2021 at 10:39:11AM +0800, [email protected] wrote:
From: Guangming Cao <[email protected]>
When mapping the memory represented by a dma-buf into a device's address space, it might be desireable to map the memory with certain DMA attributes. Thus, introduce the
dma_mapping_attrsfield in thedma_buf_attachmentstructure so that when the memory is mapped withdma_buf_map_attachment, it is mapped with the desired DMA attributes.Quoted 3 lines
Signed-off-by Isaac J. Manjarres <[email protected]>Signed-off-by Sandeep Patil <[email protected]>Signed-off-by Guangming Cao <[email protected]>
Quoted 7 lines
Can you pls include the code that's going to use this here too?
At a glance all the attributes you might want to set are supposed to be
under the control of the exporter, not the importer. -Daniel
Hi Daniel,
Sorry for late. Currently I couldn't upload the coding going to use this part because of some miscellaneous reasons.
Just as I said in commit message, for dma_heap, users can't skip cache sync or other opetrations when map iova[dma_buf_map_attachment] by fill (struct dma_buf_attachment).dma_map_attrs, it's not dma_map_attrs expected(link:
https://github.com/torvalds/linux/blob/master/Documentation/core-api/dma-attributes.rst#dma_attr_skip_cpu_sync
).
Correspondingly, if they use dma_buf to map iova[dma_buf_map_attrs], they will not meet this issue. So, I think it's a design flaw for dma_heap.
BRs!
Guangming
Quoted 104 lines
Quoted 102 lines
--- drivers/dma-buf/heaps/cma_heap.c | 6 ++++-- drivers/dma-buf/heaps/system_heap.c | 6 ++++-- include/linux/dma-buf.h | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-)drivers/dma- renamed from drivers/dma-buf/heaps/cma_heap.c +0
-0
buf/heaps/cma_heap.c
index0c05b79870f9..2c9feb3bfc3e100644drivers/dma-buf/heaps/cma_heap.c +2
-1@@ -99,9 +99,10 @@ static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachme { struct dma_heap_attachment *a = attachment->priv; struct sg_table *table = &a->table; + int attrs = attachment->dma_map_attrs; int ret; - ret = dma_map_sgtable(attachment->dev, table, direction, 0); + ret = dma_map_sgtable(attachment->dev, table, direction, attrs); if (ret)drivers/dma-buf/heaps/cma_heap.c +2
-1return ERR_PTR (-ENOMEM ); a->mapped = true;@@ -113,9 +114,10 @@ static void cma_heap_unmap_dma_buf(structdma_buf_attachment *attachment, enum dma_data_direction direction) { struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); }static int cma_heap_dma_buf_begin_cpu_access(struct
dma_buf
*dmabuf,drivers/dma- renamed from drivers/dma-buf/heaps/system_heap.c +0
-0
buf/heaps/system_heap.c
index23a7e74ef966..fc7b1e02988e100644drivers/dma-buf/heaps/system_heap.c +2
-1@@ -130,9 +130,10 @@ static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attac { struct dma_heap_attachment *a = attachment->priv; struct sg_table *table = a->table; + int attrs = attachment->dma_map_attrs; int ret; - ret = dma_map_sgtable(attachment->dev, table, direction, 0); + ret = dma_map_sgtable(attachment->dev, table, direction, attrs); if (ret)drivers/dma-buf/heaps/system_heap.c +2
-1return ERR_PTR (ret);@@ -145,9 +146,10 @@ static void system_heap_unmap_dma_buf(structdma_buf_attachment *attachment, enum dma_data_direction direction) { struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); }static int system_heap_dma_buf_begin_cpu_access(struct
dma_buf
*dmabuf,include/linux/dma-buf.h +0
-0@@ -379,6 +379,8 @@ struct dma_buf_attach_ops {* @importer_ops: importer operations for this attachment, if provided
* dma_buf_map/unmap_attachment() must be called with thedma_resv
lock held.
* @importer_priv: importer specific attachment data.
+ * @dma_map_attrs: DMA attributes to be used when the exporter maps the buffer
+ * throughdma_buf_map_attachment.
*
* This structure holds the attachment information between thedma_bufbuffer
* and its user device(s). The list contains one attachment struct
per deviceinclude/linux/dma-buf.h +1
-0@@ -399,6 +401,7 @@ struct dma_buf_attachment {const struct dma_buf_attach_ops *importer_ops;void *importer_priv;void *priv; +unsigned long dma_map_attrs; };/** Signature · 1 line
2.17.1
From: Guangming Cao <[email protected]>
Quoted 15 lines · Daniel Vetter
Quoted 9 lines · On Mon, Aug 30, 2021 at 10:39:11AM +0800, [email protected] wrote:
From: Guangming Cao <[email protected]>
When mapping the memory represented by a dma-buf into a device's address space, it might be desireable to map the memory with certain DMA attributes. Thus, introduce the
dma_mapping_attrsfield in thedma_buf_attachmentstructure so that when the memory is mapped withdma_buf_map_attachment, it is mapped with the desired DMA attributes.Quoted 3 lines
Signed-off-by Isaac J. Manjarres <[email protected]>Signed-off-by Sandeep Patil <[email protected]>Signed-off-by Guangming Cao <[email protected]>
Quoted 7 lines
Can you pls include the code that's going to use this here too?
At a glance all the attributes you might want to set are supposed to be
under the control of the exporter, not the importer. -Daniel
Hi Daniel,
Sorry for the late. Currently I couldn't upload the coding going to use this part because of some miscellaneous reasons.
Just as I said in commit message, for dma_heap, users can't skip cache sync or other opetrations when map iova[dma_buf_map_attachment] by fill (struct dma_buf_attachment).dma_map_attrs, it's not dma_map_attrs expected(link:
https://github.com/torvalds/linux/blob/master/Documentation/core-api/dma-attributes.rst#dma_attr_skip_cpu_sync
).
Correspondingly, if they use dma_buf to map iova[dma_buf_map_attrs], they will not meet this issue. So, I think it's a design flaw for dma_heap.
BRs!
Guangming
Quoted 104 lines
Quoted 102 lines
--- drivers/dma-buf/heaps/cma_heap.c | 6 ++++-- drivers/dma-buf/heaps/system_heap.c | 6 ++++-- include/linux/dma-buf.h | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-)drivers/dma- renamed from drivers/dma-buf/heaps/cma_heap.c +0
-0
buf/heaps/cma_heap.c
index0c05b79870f9..2c9feb3bfc3e100644drivers/dma-buf/heaps/cma_heap.c +2
-1@@ -99,9 +99,10 @@ static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachme { struct dma_heap_attachment *a = attachment->priv; struct sg_table *table = &a->table; + int attrs = attachment->dma_map_attrs; int ret; - ret = dma_map_sgtable(attachment->dev, table, direction, 0); + ret = dma_map_sgtable(attachment->dev, table, direction, attrs); if (ret)return ERR_PTR(-ENOMEM); a->mapped = true;drivers/dma-buf/heaps/cma_heap.c +2
-1@@ -113,9 +114,10 @@ static void cma_heap_unmap_dma_buf(structdma_buf_attachment *attachment, enum dma_data_direction direction) { struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); }static int cma_heap_dma_buf_begin_cpu_access(struct
dma_buf
*dmabuf,drivers/dma- renamed from drivers/dma-buf/heaps/system_heap.c +0
-0
buf/heaps/system_heap.c
index23a7e74ef966..fc7b1e02988e100644drivers/dma-buf/heaps/system_heap.c +2
-1@@ -130,9 +130,10 @@ static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attac { struct dma_heap_attachment *a = attachment->priv; struct sg_table *table = a->table; + int attrs = attachment->dma_map_attrs; int ret; - ret = dma_map_sgtable(attachment->dev, table, direction, 0); + ret = dma_map_sgtable(attachment->dev, table, direction, attrs); if (ret)return ERR_PTR(ret);drivers/dma-buf/heaps/system_heap.c +2
-1@@ -145,9 +146,10 @@ static void system_heap_unmap_dma_buf(structdma_buf_attachment *attachment, enum dma_data_direction direction) { struct dma_heap_attachment *a = attachment->priv; +int attrs = attachment->dma_map_attrs; a->mapped = false; -dma_unmap_sgtable (attachment->dev, table, direction,0 ); +dma_unmap_sgtable (attachment->dev, table, direction, attrs); }static int system_heap_dma_buf_begin_cpu_access(struct
dma_buf
*dmabuf,include/linux/dma-buf.h +0
-0@@ -379,6 +379,8 @@ struct dma_buf_attach_ops {* @importer_ops: importer operations for this attachment, if provided
* dma_buf_map/unmap_attachment() must be called with thedma_resv
lock held.
* @importer_priv: importer specific attachment data.
+ * @dma_map_attrs: DMA attributes to be used when the exporter maps the buffer
+ * throughdma_buf_map_attachment.
*
* This structure holds the attachment information between thedma_bufbuffer
* and its user device(s). The list contains one attachment struct
per deviceinclude/linux/dma-buf.h +1
-0@@ -399,6 +401,7 @@ struct dma_buf_attachment {const struct dma_buf_attach_ops *importer_ops;void *importer_priv;void *priv; +unsigned long dma_map_attrs; };/** Signature · 1 line
2.17.1