[PATCH v2 3/4] drm/gem-dma: Use the dma_*_attr API variant

Chen-Yu Tsai
2026-03-17T06:41:28Z

From: Rob Herring <robh@kernel.org>

In preparation to allow DRM DMA users to adjust the DMA_ATTR_* flags,
convert the DMA helper code to use the dma_*_attr APIs instead of the
dma_*_wc variants.

Only the DMA_ATTR_WRITE_COMBINE and DMA_ATTR_NO_WARN attributes are set
in this commit, so there's no functional change.

Also change the dma_free_wc() call in vc4_bo_purge() in the vc4 driver
to use dma_free_attrs() to match. vc4_bo is a sub-class of
drm_gem_dma_object.

Sub-classes of |struct drm_gem_dma_object| can also set additional
DMA_ATTR_* flags if they choose so. For example a sub-class could
set DMA_ATTR_FORCE_CONTIGUOUS in certain cases.

Signed-off-by: Rob Herring <robh@kernel.org>
[wenst@chromium.org: Rebase onto renamed DMA helpers]
[wenst@chromium.org: Make vc4_bo_purge() use matching dma_free_attrs()]
[wenst@chromium.org: Expand commit message to mention that sub-classes
can set extra DMA_ATTR_* flags]
Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>

---
Changes since v1:
- Rebased onto renamed DMA helpers
- Made vc4_bo_purge() use matching dma_free_attrs()
- Expanded commit message to mention that sub-classes can set extra
DMA_ATTR_* flags
---
drivers/gpu/drm/drm_gem_dma_helper.c | 26 +++++++++++++++-----------
drivers/gpu/drm/vc4/vc4_bo.c | 2 +-
include/drm/drm_gem_dma_helper.h | 3 +++
3 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/drm_gem_dma_helper.c b/drivers/gpu/drm/drm_gem_dma_helper.c
index 1c00a71ab3c9..9722c9fc86f3 100644
--- a/drivers/gpu/drm/drm_gem_dma_helper.c
+++ b/drivers/gpu/drm/drm_gem_dma_helper.c
@@ -108,6 +108,7 @@ __drm_gem_dma_create(struct drm_device *drm, size_t size, bool private)
goto error;
}

+ dma_obj->dma_attrs |= DMA_ATTR_NO_WARN | DMA_ATTR_WRITE_COMBINE;
return dma_obj;

error:
@@ -152,9 +153,10 @@ struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,
DMA_TO_DEVICE,
GFP_KERNEL | __GFP_NOWARN);
} else {
- dma_obj->vaddr = dma_alloc_wc(drm_dev_dma_dev(drm), size,
- &dma_obj->dma_addr,
- GFP_KERNEL | __GFP_NOWARN);
+ dma_obj->vaddr = dma_alloc_attrs(drm_dev_dma_dev(drm), size,
+ &dma_obj->dma_addr,
+ GFP_KERNEL | __GFP_NOWARN,
+ dma_obj->dma_attrs);
}
if (!dma_obj->vaddr) {
drm_dbg(drm, "failed to allocate buffer with size %zu\n",
@@ -242,9 +244,9 @@ void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj)
dma_obj->vaddr, dma_obj->dma_addr,
DMA_TO_DEVICE);
else
- dma_free_wc(drm_dev_dma_dev(gem_obj->dev),
- dma_obj->base.size, dma_obj->vaddr,
- dma_obj->dma_addr);
+ dma_free_attrs(drm_dev_dma_dev(gem_obj->dev),
+ dma_obj->base.size, dma_obj->vaddr,
+ dma_obj->dma_addr, dma_obj->dma_attrs);
}

drm_gem_object_release(gem_obj);
@@ -435,8 +437,9 @@ struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj)
if (!sgt)
return ERR_PTR(-ENOMEM);

- ret = dma_get_sgtable(drm_dev_dma_dev(obj->dev), sgt, dma_obj->vaddr,
- dma_obj->dma_addr, obj->size);
+ ret = dma_get_sgtable_attrs(drm_dev_dma_dev(obj->dev), sgt,
+ dma_obj->vaddr, dma_obj->dma_addr,
+ obj->size, dma_obj->dma_attrs);
if (ret < 0)
goto out;

@@ -546,9 +549,10 @@ int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *
vma, vma->vm_end - vma->vm_start,
virt_to_page(dma_obj->vaddr));
} else {
- ret = dma_mmap_wc(drm_dev_dma_dev(dma_obj->base.dev), vma,
- dma_obj->vaddr, dma_obj->dma_addr,
- vma->vm_end - vma->vm_start);
+ ret = dma_mmap_attrs(drm_dev_dma_dev(dma_obj->base.dev), vma,
+ dma_obj->vaddr, dma_obj->dma_addr,
+ vma->vm_end - vma->vm_start,
+ dma_obj->dma_attrs);
}
if (ret)
drm_gem_vm_close(vma);
diff --git a/drivers/gpu/drm/vc4/vc4_bo.c b/drivers/gpu/drm/vc4/vc4_bo.c
index f45ba47b4ba8..981593739a0f 100644
--- a/drivers/gpu/drm/vc4/vc4_bo.c
+++ b/drivers/gpu/drm/vc4/vc4_bo.c
@@ -304,7 +304,7 @@ static void vc4_bo_purge(struct drm_gem_object *obj)

drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);

- dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.dma_addr);
+ dma_free_attrs(dev->dev, obj->size, bo->base.vaddr, bo->base.dma_addr, bo->base.dma_attrs);
bo->base.vaddr = NULL;
bo->madv = __VC4_MADV_PURGED;
}
diff --git a/include/drm/drm_gem_dma_helper.h b/include/drm/drm_gem_dma_helper.h
index f2678e7ecb98..e815ff121e23 100644
--- a/include/drm/drm_gem_dma_helper.h
+++ b/include/drm/drm_gem_dma_helper.h
@@ -16,6 +16,8 @@ struct drm_mode_create_dumb;
* more than one entry but they are guaranteed to have contiguous
* DMA addresses.
* @vaddr: kernel virtual address of the backing memory
+ * @dma_attrs: DMA attributes used when allocating backing memory.
+ * Only applies to coherent memory.
* @map_noncoherent: if true, the GEM object is backed by non-coherent memory
*/
struct drm_gem_dma_object {
@@ -25,6 +27,7 @@ struct drm_gem_dma_object {

/* For objects with DMA memory allocated by GEM DMA */
void *vaddr;
+ unsigned long dma_attrs;

bool map_noncoherent;
};
--
2.53.0.851.ga537e3e6e9-goog