drm/gem-dma: Support dedicated DMA device for allocation
Subsystems (7)
cover drm/gem-dma: Support dedicated DMA device for allocation
Hi folks,
This series expands the "dedicated DMA device" support in DRM to the GEM DMA helpers, and converts the MediaTek DRM driver to setting the DMA device and dropping the custom GEM helpers that implemented this function.
Various display drivers implement the "dedicated DMA device" support with custom GEM helpers. These include Exynos, MediaTek, and Rockchip to name a few. Allwinner does something entirely different, calling of_dma_configure() on the virtual display device using the OF node of the actual DMA device. Recently this causes a warning if IOMMUs are involved.
This series intends to allow the core helpers to deal with it, and not have every driver implement it in slightly different ways, duplicating code.
Patch 1 adds dedicated DMA device support to drm_prime_pages_to_sg(). I believe this was missing from the original change that added dedicated DMA devices for PRIME.
Patch 2 adds support for dedicated DMA device to the GEM DMA helpers for GEM buffer allocation and mmap.
Patch 3 converts the MediaTek DRM driver to use the dedicated DMA device support, and drop all the remaining custom GEM callbacks that deal with it.
Patch 4 converts the Allwinner sun4i DRM driver to use the dedicated DMA device support, instead of the of_dma_configure() hack it currently has.
The series should be merged through drm-misc-next so that other drivers can take advantage of the change.
I also intend to try to convert the Exynos and Rockchip drivers, however both also have options to set DMA_ATTR_NO_KERNEL_MAPPING when using dma_alloc_attrs() to allocate memory for the buffers. I intend to resurrect the DRM_MODE_DUMB_KERNEL_MAP work from Rob Herring [1] to handle this. The Rockchip driver also has custom IOMMU attachment that I'm still trying to understand.
Thanks
ChenYu
Chen-Yu Tsai (4):
drm/prime: Limit scatter list size with dedicated DMA device drm/gem-dma: Support dedicated DMA device for allocation and mapping drm/mediatek: Set dedicated DMA device and drop custom GEM callbacks drm/sun4i: Use backend/mixer as dedicated DMA device
drivers/gpu/drm/drm_gem_dma_helper.c | 21 ++- drivers/gpu/drm/drm_prime.c | 2 +- drivers/gpu/drm/mediatek/mtk_crtc.c | 1 - drivers/gpu/drm/mediatek/mtk_drm_drv.c | 21 +-- drivers/gpu/drm/mediatek/mtk_drm_drv.h | 1 - drivers/gpu/drm/mediatek/mtk_gem.c | 231 ------------------------- drivers/gpu/drm/mediatek/mtk_gem.h | 17 -- drivers/gpu/drm/sun4i/sun4i_backend.c | 27 +-- drivers/gpu/drm/sun4i/sun8i_mixer.c | 27 +-- 9 files changed, 46 insertions(+), 302 deletions(-) delete mode 100644 drivers/gpu/drm/mediatek/mtk_gem.c delete mode 100644 drivers/gpu/drm/mediatek/mtk_gem.h
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 1/4 drm/prime: Limit scatter list size with dedicated DMA device
If a dedicated DMA device is specified for the DRM device, then the scatter list size limit should pertain to the DMA device.
Use the dedicated DMA device, if given, to limit the scatter list size. This only applies to drivers that have called drm_dev_set_dma_dev() and are using drm_prime_pages_to_sg() either directly or through the SHMEM helpers. At the time of this writing, the former case only includes the Rockchip DRM driver, while the latter case includes the gud, udl, and the tiny appletbdrm and gm12u320 drivers.
--- drivers/gpu/drm/drm_prime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +1 -1
@@ -859,7 +859,7 @@ struct sg_table *drm_prime_pages_to_sg(struct drm_device *dev,return ERR_PTR (-ENOMEM );if (dev) - max_segment =dma_max_mapping_size (dev->dev); + max_segment =dma_max_mapping_size (drm_dev_dma_dev (dev));if (max_segment ==0 ) max_segment =UINT_MAX ; err = sg_alloc_table_from_pages_segment(sg, pages, nr_pages, 0,
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 2/4 drm/gem-dma: Support dedicated DMA device for allocation and mapping
In reply to Chen-Yu TsaiSupport for a dedicated DMA device for prime imports was added in commit 143ec8d3f939 ("drm/prime: Support dedicated DMA device for dma-buf imports"). This allowed the DRM driver to provide a dedicated DMA device when its own underlying device was not capable of DMA, for example when it is a USB device (the original target) or a virtual device. The latter case is common on embedded SoCs, on which the display pipeline is composed of various fixed function blocks, and the DRM device is simply a made-up device, an address space managing the routing between the blocks, or whichever block the implementor thought made sense at the time. The point is that the chosen device is often not the actual device doing the DMA. Various drivers have used workarounds or reimplemented the GEM DMA helpers to get the DMA addresses and IOMMUs to work correctly.
Add support for the dedicated DMA device to the GEM DMA helpers.
No existing driver currently uses the GEM DMA helpers and calls drm_dev_set_dma_dev() to set a dedicated DMA device, so no existing users should be affected.
--- drivers/gpu/drm/drm_gem_dma_helper.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem_dma_helper.c +12 -9
@@ -146,12 +146,13 @@ struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,return dma_obj;if (dma_obj->map_noncoherent) { - dma_obj->vaddr =dma_alloc_noncoherent (drm->dev, size, + dma_obj->vaddr =dma_alloc_noncoherent (drm_dev_dma_dev (drm), + size, &dma_obj->dma_addr,DMA_TO_DEVICE ,GFP_KERNEL | __GFP_NOWARN); }else { - dma_obj->vaddr =dma_alloc_wc (drm->dev, size, + dma_obj->vaddr =dma_alloc_wc (drm_dev_dma_dev (drm), size, &dma_obj->dma_addr,GFP_KERNEL | __GFP_NOWARN); }
@@ -236,12 +237,14 @@ void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj)drm_prime_gem_destroy (gem_obj, dma_obj->sgt); }else if (dma_obj->vaddr) {if (dma_obj->map_noncoherent) -dma_free_noncoherent (gem_obj->dev->dev, dma_obj->base.size, +dma_free_noncoherent (drm_dev_dma_dev (gem_obj->dev), + dma_obj->base.size, dma_obj->vaddr, dma_obj->dma_addr,DMA_TO_DEVICE );else -dma_free_wc (gem_obj->dev->dev, dma_obj->base.size, - dma_obj->vaddr, dma_obj->dma_addr); +dma_free_wc (drm_dev_dma_dev (gem_obj->dev), + dma_obj->base.size, dma_obj->vaddr, + dma_obj->dma_addr); }drm_gem_object_release (gem_obj);
@@ -432,7 +435,7 @@ 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 (obj->dev->dev, sgt, dma_obj->vaddr, + ret =dma_get_sgtable (drm_dev_dma_dev (obj->dev), sgt, dma_obj->vaddr, dma_obj->dma_addr, obj->size);if (ret <0 ) goto out;
@@ -539,12 +542,12 @@ int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *if (dma_obj->map_noncoherent) { vma->vm_page_prot =vm_get_page_prot (vma->vm_flags); - ret =dma_mmap_pages (dma_obj->base.dev->dev, + ret =dma_mmap_pages (drm_dev_dma_dev (dma_obj->base.dev), vma, vma->vm_end - vma->vm_start,virt_to_page (dma_obj->vaddr)); }else { - ret =dma_mmap_wc (dma_obj->base.dev->dev, vma, dma_obj->vaddr, - dma_obj->dma_addr, + 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); }if (ret)
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 3/4 drm/mediatek: Set dedicated DMA device and drop custom GEM callbacks
In reply to Chen-Yu TsaiIn commit 9b54a32c7c6a ("drm/mediatek: mtk_gem: Partial refactor and use drm_gem_dma_object") the MediaTek DRM driver was refactored to use drm_gem_dma_object, but custom callbacks were still needed to deal with using the first device of the pipeline as the DMA device, instead of the MMSYS device that the DRM driver binds to.
Turns out there is already partial support for dedicated DMA devices in the DRM subsystem for PRIME imports. The preceding patches add support for dedicated DMA devices to the GEM DMA helpers.
This allows us to just set the dedicated DMA device for the DRM device, and drop all the custom GEM callbacks. Also drop the .dma_dev field from the driver private data as it is no longer needed.
There are slight differences in the mmap helper: the VM_DONTDUMP and VM_IO flags are no longer set. Both were lifted from drm_gem_mmap_obj(). VM_IO probably doesn't make sense since the buffer is allocated using dma_alloc_attrs().
--- drivers/gpu/drm/mediatek/mtk_crtc.c | 1 - drivers/gpu/drm/mediatek/mtk_drm_drv.c | 21 +-- drivers/gpu/drm/mediatek/mtk_drm_drv.h | 1 - drivers/gpu/drm/mediatek/mtk_gem.c | 231 ------------------------- drivers/gpu/drm/mediatek/mtk_gem.h | 17 -- 5 files changed, 3 insertions(+), 268 deletions(-) delete mode 100644 drivers/gpu/drm/mediatek/mtk_gem.c delete mode 100644 drivers/gpu/drm/mediatek/mtk_gem.h
drivers/gpu/drm/mediatek/mtk_crtc.c +0 -1
@@ -23,7 +23,6 @@#include "mtk_crtc.h" #include "mtk_ddp_comp.h" #include "mtk_drm_drv.h" -#include "mtk_gem.h" #include "mtk_plane.h" /*
drivers/gpu/drm/mediatek/mtk_drm_drv.c +3 -18
@@ -19,6 +19,7 @@#include <drm/drm_fbdev_dma.h> #include <drm/drm_fourcc.h> #include <drm/drm_gem.h> +#include <drm/drm_gem_dma_helper.h> #include <drm/drm_gem_framebuffer_helper.h> #include <drm/drm_ioctl.h> #include <drm/drm_of.h>
@@ -29,7 +30,6 @@#include "mtk_ddp_comp.h" #include "mtk_disp_drv.h" #include "mtk_drm_drv.h" -#include "mtk_gem.h" #define DRIVER_NAME "mediatek"#define DRIVER_DESC "Mediatek SoC DRM"
@@ -565,8 +565,7 @@ static int mtk_drm_kms_init(struct drm_device *drm) goto err_component_unbind; } -for (i =0 ; i < private->data->mmsys_dev_num; i++) - private->all_drm_private[i]->dma_dev = dma_dev; +drm_dev_set_dma_dev (drm, dma_dev);/* * Configure the DMA segment size to make sure we get contiguous IOVA
@@ -600,26 +599,12 @@ static void mtk_drm_kms_deinit(struct drm_device *drm)DEFINE_DRM_GEM_FOPS (mtk_drm_fops); -/* -* We need to override this because the device used to import the memory is -* not dev->dev, as drm_gem_prime_import() expects. -*/ -static struct drm_gem_object *mtk_gem_prime_import (struct drm_device *dev, -struct dma_buf *dma_buf) -{ -struct mtk_drm_private *private = dev->dev_private; - -return drm_gem_prime_import_dev (dev, dma_buf, private->dma_dev); -} -static const struct drm_driver mtk_drm_driver = { .driver_features =DRIVER_MODESET |DRIVER_GEM |DRIVER_ATOMIC , - .dumb_create = mtk_gem_dumb_create, +DRM_GEM_DMA_DRIVER_OPS ,DRM_FBDEV_DMA_DRIVER_OPS , - .gem_prime_import = mtk_gem_prime_import, - .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table, .fops = &mtk_drm_fops, .name =DRIVER_NAME ,
drivers/gpu/drm/mediatek/mtk_drm_drv.h +0 -1
@@ -54,7 +54,6 @@ struct mtk_mmsys_driver_data {struct mtk_drm_private {struct drm_device *drm; -struct device *dma_dev;bool mtk_drm_bound;bool drm_master;struct device *dev;
drivers/gpu/drm/mediatek/mtk_gem.c deleted +0 -231
231 lines not on this page · raw message
drivers/gpu/drm/mediatek/mtk_gem.h deleted +0 -17
@@ -1,17 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* -* Copyright (c) 2015 MediaTek Inc. -*/ - -#ifndef _MTK_GEM_H_ -#define _MTK_GEM_H_ - -#include <drm/drm_gem.h> -#include <drm/drm_gem_dma_helper.h> - -int mtk_gem_dumb_create (struct drm_file *file_priv,struct drm_device *dev, -struct drm_mode_create_dumb *args); -struct drm_gem_object *mtk_gem_prime_import_sg_table (struct drm_device *dev, -struct dma_buf_attachment *attach,struct sg_table *sg); - -#endif
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH 4/4 drm/sun4i: Use backend/mixer as dedicated DMA device
In reply to Chen-Yu TsaiThe sun4i DRM driver deals with DMA constraints in a peculiar way. Instead of using the actual DMA device in various helpers, it justs reconfigures the DMA constraints of the virtual display device using the DMA device's device tree node by calling of_dma_configure().
Turns out of_dma_configure() should only be called from bus code. Lately this also triggers a big warning through of_iommu_configure() and ultimately __iommu_probe_device():
late IOMMU probe at driver bind, something fishy here!
Now that the GEM DMA helpers have proper support for allocating and mapping buffers with a dedicated DMA device, switch over to it as the proper solution.
The mixer change was tested on a Pine H64 model B. The backend change was only compile tested. Though I don't expect any issues, help testing on an older device would be appreciated.
--- drivers/gpu/drm/sun4i/sun4i_backend.c | 27 +++++++++++++++------------ drivers/gpu/drm/sun4i/sun8i_mixer.c | 27 +++++++++++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-)
drivers/gpu/drm/sun4i/sun4i_backend.c +15 -12
@@ -798,18 +798,21 @@ static int sun4i_backend_bind(struct device *dev, struct device *master,dev_set_drvdata (dev, backend);spin_lock_init (&backend->frontend_lock); -if (of_property_present (dev->of_node,"interconnects" )) { -/* -* This assume we have the same DMA constraints for all our the -* devices in our pipeline (all the backends, but also the -* frontends). This sounds bad, but it has always been the case -* for us, and DRM doesn't do per-device allocation either, so -* we would need to fix DRM first... -*/ - ret =of_dma_configure (drm->dev, dev->of_node, true); -if (ret) -return ret; - } +/* +* This assume we have the same DMA constraints for all our the +* devices in our pipeline (all the backends, but also the +* frontends). This sounds bad, but it has always been the case +* for us, and DRM doesn't do per-device allocation either, so +* we would need to fix DRM first... +* +* Always use the first bound backend as the DMA device. While +* our device trees always have all backends enabled, some in +* the wild may actually have the first one disabled. If both +* are enabled, the order in which they are bound is guaranteed +* since the driver adds components in order. +*/ +if (drm_dev_dma_dev (drm) == drm->dev) +drm_dev_set_dma_dev (drm, dev); backend->engine.node = dev->of_node; backend->engine.ops = &sun4i_backend_engine_ops;
drivers/gpu/drm/sun4i/sun8i_mixer.c +15 -12
@@ -536,18 +536,21 @@ static int sun8i_mixer_bind(struct device *dev, struct device *master, mixer->engine.ops = &sun8i_engine_ops; mixer->engine.node = dev->of_node; -if (of_property_present (dev->of_node,"iommus" )) { -/* -* This assume we have the same DMA constraints for -* all our the mixers in our pipeline. This sounds -* bad, but it has always been the case for us, and -* DRM doesn't do per-device allocation either, so we -* would need to fix DRM first... -*/ - ret =of_dma_configure (drm->dev, dev->of_node, true); -if (ret) -return ret; - } +/* +* This assume we have the same DMA constraints for all our the +* devices in our pipeline (all the backends, but also the +* frontends). This sounds bad, but it has always been the case +* for us, and DRM doesn't do per-device allocation either, so +* we would need to fix DRM first... +* +* Always use the first bound backend as the DMA device. While +* our device trees always have all backends enabled, some in +* the wild may actually have the first one disabled. If both +* are enabled, the order in which they are bound is guaranteed +* since the driver adds components in order. +*/ +if (drm_dev_dma_dev (drm) == drm->dev) +drm_dev_set_dma_dev (drm, dev);/* * While this function can fail, we shouldn't do anything
Signature · 1 line
2.53.0.473.g4a7958ca14-goog