From: Steven Price <steven.price@arm.com>
To: Boris Brezillon <boris.brezillon@collabora.com>
Cc: dri-devel@lists.freedesktop.org,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Faith Ekstrand" <faith.ekstrand@collabora.com>,
"Thierry Reding" <thierry.reding@gmail.com>,
"Mikko Perttunen" <mperttunen@nvidia.com>,
"Melissa Wen" <mwen@igalia.com>,
"Maíra Canal" <mcanal@igalia.com>,
"Lucas De Marchi" <lucas.demarchi@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
"Frank Binns" <frank.binns@imgtec.com>,
"Matt Coster" <matt.coster@imgtec.com>,
"Rob Clark" <robin.clark@oss.qualcomm.com>,
"Dmitry Baryshkov" <lumag@kernel.org>,
"Abhinav Kumar" <abhinav.kumar@linux.dev>,
"Jessica Zhang" <jessica.zhang@oss.qualcomm.com>,
"Sean Paul" <sean@poorly.run>,
"Marijn Suijten" <marijn.suijten@somainline.org>,
"Alex Deucher" <alexander.deucher@amd.com>,
"Christian König" <christian.koenig@amd.com>,
amd-gfx@lists.freedesktop.org, kernel@collabora.com
Subject: Re: [PATCH v5 03/16] drm/shmem: Add a drm_gem_shmem_sync() helper
Date: Fri, 14 Nov 2025 15:02:28 +0000 [thread overview]
Message-ID: <5d511f84-5703-4992-b57d-9475dbce185d@arm.com> (raw)
In-Reply-To: <20251030140525.366636-4-boris.brezillon@collabora.com>
On 30/10/2025 14:05, Boris Brezillon wrote:
> From: Faith Ekstrand <faith.ekstrand@collabora.com>
>
> This enables syncing mapped GEM objects between the CPU and GPU via calls
> to dma_sync_*(). It's a bit annoying as it requires walking the sg_table
> so it's best if every driver doesn't hand-roll it.
>
> v2:
> - s/drm_gem_shmem_sync_mmap/drm_gem_shmem_sync/
> - Change the prototype to match drm_gem_object_funcs::sync()
> - Add a wrapper for drm_gem_object_funcs::sync()
>
> v3:
> - No changes
>
> v4:
> - Add Steve's R-b
>
> v5:
> - Change the semantics of the drm_gem_shmem_sync() helper to better
> reflect the UMD cache flush/flush+invalidate semantics (discussed
> with Faith)
> - Drop R-bs
>
> Signed-off-by: Faith Ekstrand <faith.ekstrand@collabora.com>
> Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Steven Price <steven.price@arm.com>
> ---
> drivers/gpu/drm/drm_gem_shmem_helper.c | 93 ++++++++++++++++++++++++++
> include/drm/drm_gem_shmem_helper.h | 14 ++++
> 2 files changed, 107 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index e49c75739c20..d9266e22a0dc 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -690,6 +690,99 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
> }
> EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
>
> +/**
> + * drm_gem_shmem_sync - Sync CPU-mapped data
> + * @shmem: shmem GEM object
> + * @offset: Offset into the GEM object
> + * @size: Size of the area to sync
> + * @type: Type of synchronization
> + *
> + * Returns:
> + * 0 on success or a negative error code on failure.
> + */
> +int drm_gem_shmem_sync(struct drm_gem_shmem_object *shmem, size_t offset,
> + size_t size, enum drm_gem_shmem_sync_type type)
> +{
> + const struct drm_device *dev = shmem->base.dev;
> + struct sg_table *sgt;
> + struct scatterlist *sgl;
> + unsigned int count;
> +
> + /* Make sure the range is in bounds. */
> + if (offset + size < offset || offset + size > shmem->base.size)
> + return -EINVAL;
> +
> + /* Disallow CPU-cache maintenance on imported buffers. */
> + if (drm_gem_is_imported(&shmem->base))
> + return -EINVAL;
> +
> + switch (type) {
> + case DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH:
> + case DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE:
> + break;
> +
> + default:
> + return -EINVAL;
> + }
> +
> + /* Don't bother if it's WC-mapped */
> + if (shmem->map_wc)
> + return 0;
> +
> + /* Nothing to do if the size is zero. */
> + if (size == 0)
> + return 0;
> +
> + sgt = drm_gem_shmem_get_pages_sgt(shmem);
> + if (IS_ERR(sgt))
> + return PTR_ERR(sgt);
> +
> + for_each_sgtable_dma_sg(sgt, sgl, count) {
> + if (size == 0)
> + break;
> +
> + dma_addr_t paddr = sg_dma_address(sgl);
> + size_t len = sg_dma_len(sgl);
> +
> + if (len <= offset) {
> + offset -= len;
> + continue;
> + }
> +
> + paddr += offset;
> + len -= offset;
> + len = min_t(size_t, len, size);
> + size -= len;
> + offset = 0;
> +
> + /* It's unclear whether dma_sync_xxx() is the right API to do CPU
> + * cache maintenance given an IOMMU can register their own
> + * implementation doing more than just CPU cache flushes/invalidation,
> + * and what we really care about here is CPU caches only, but that's
> + * the best we have that is both arch-agnostic and does at least the
> + * CPU cache maintenance on a <page,offset,size> tuple.
> + *
> + * Also, I wish we could do a single
> + *
> + * dma_sync_single_for_device(BIDIR)
> + *
> + * and get a flush+invalidate, but that's not how it's implemented
> + * in practice (at least on arm64), so we have to make it
> + *
> + * dma_sync_single_for_device(TO_DEVICE)
> + * dma_sync_single_for_cpu(FROM_DEVICE)
> + *
> + * for the flush+invalidate case.
> + */
> + dma_sync_single_for_device(dev->dev, paddr, len, DMA_TO_DEVICE);
> + if (type == DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE)
> + dma_sync_single_for_cpu(dev->dev, paddr, len, DMA_FROM_DEVICE);
> + }
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(drm_gem_shmem_sync);
> +
> /**
> * drm_gem_shmem_print_info() - Print &drm_gem_shmem_object info for debugfs
> * @shmem: shmem GEM object
> diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h
> index 075275d6b2fd..b0b6d0104a9a 100644
> --- a/include/drm/drm_gem_shmem_helper.h
> +++ b/include/drm/drm_gem_shmem_helper.h
> @@ -124,6 +124,20 @@ void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem,
> struct iosys_map *map);
> int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
>
> +/**
> + * enum enum drm_gem_shmem_sync_type - Type of synchronization
> + */
> +enum drm_gem_shmem_sync_type {
> + /** DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH: Flush CPU caches */
> + DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH = 0,
> +
> + /** DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE: Flush and invalidate CPU caches */
> + DRM_GEM_SHMEM_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE,
> +};
> +
> +int drm_gem_shmem_sync(struct drm_gem_shmem_object *shmem, size_t offset,
> + size_t size, enum drm_gem_shmem_sync_type type);
> +
> int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem);
> void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem);
>
next prev parent reply other threads:[~2025-11-14 15:02 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-30 14:05 [PATCH v5 00/16] drm/panfrost, panthor: Cached maps and explicit flushing Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 01/16] drm/prime: Simplify life of drivers needing custom dma_buf_ops Boris Brezillon
2025-10-30 14:25 ` Christian König
2025-10-30 14:35 ` Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 02/16] drm/shmem: Provide a generic {begin, end}_cpu_access() implementation Boris Brezillon
2025-10-30 14:31 ` [PATCH v5 02/16] drm/shmem: Provide a generic {begin,end}_cpu_access() implementation Christian König
2025-11-04 8:08 ` Boris Brezillon
2025-11-03 20:34 ` [PATCH v5 02/16] drm/shmem: Provide a generic {begin, end}_cpu_access() implementation Akash Goel
2025-11-04 7:42 ` Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 03/16] drm/shmem: Add a drm_gem_shmem_sync() helper Boris Brezillon
2025-11-14 15:02 ` Steven Price [this message]
2025-10-30 14:05 ` [PATCH v5 04/16] drm/panthor: Provide a custom dma_buf implementation Boris Brezillon
2025-11-14 15:02 ` Steven Price
2025-10-30 14:05 ` [PATCH v5 05/16] drm/panthor: Fix panthor_gpu_coherency_set() Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 06/16] drm/panthor: Expose the selected coherency protocol to the UMD Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 07/16] drm/panthor: Add a PANTHOR_BO_SYNC ioctl Boris Brezillon
2025-10-31 7:25 ` Marcin Ślusarz
2025-11-03 20:42 ` Akash Goel
2025-11-04 7:41 ` Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 08/16] drm/panthor: Add an ioctl to query BO flags Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 09/16] drm/panthor: Add flag to map GEM object Write-Back Cacheable Boris Brezillon
2025-11-03 16:43 ` Akash Goel
2025-11-03 17:13 ` Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 10/16] drm/panthor: Bump the driver version to 1.6 Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 11/16] drm/panfrost: Provide a custom dma_buf implementation Boris Brezillon
2025-11-14 16:17 ` Steven Price
2025-10-30 14:05 ` [PATCH v5 12/16] drm/panfrost: Expose the selected coherency protocol to the UMD Boris Brezillon
2025-11-14 16:19 ` Steven Price
2025-10-30 14:05 ` [PATCH v5 13/16] drm/panfrost: Add a PANFROST_SYNC_BO ioctl Boris Brezillon
2025-10-31 7:08 ` Marcin Ślusarz
2025-10-31 8:49 ` Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 14/16] drm/panfrost: Add an ioctl to query BO flags Boris Brezillon
2025-10-30 14:05 ` [PATCH v5 15/16] drm/panfrost: Add flag to map GEM object Write-Back Cacheable Boris Brezillon
2025-11-14 16:22 ` Steven Price
2025-10-30 14:05 ` [PATCH v5 16/16] drm/panfrost: Bump the driver version to 1.6 Boris Brezillon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5d511f84-5703-4992-b57d-9475dbce185d@arm.com \
--to=steven.price@arm.com \
--cc=abhinav.kumar@linux.dev \
--cc=airlied@gmail.com \
--cc=alexander.deucher@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=boris.brezillon@collabora.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=faith.ekstrand@collabora.com \
--cc=frank.binns@imgtec.com \
--cc=jessica.zhang@oss.qualcomm.com \
--cc=kernel@collabora.com \
--cc=lucas.demarchi@intel.com \
--cc=lumag@kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=marijn.suijten@somainline.org \
--cc=matt.coster@imgtec.com \
--cc=mcanal@igalia.com \
--cc=mperttunen@nvidia.com \
--cc=mripard@kernel.org \
--cc=mwen@igalia.com \
--cc=robin.clark@oss.qualcomm.com \
--cc=rodrigo.vivi@intel.com \
--cc=sean@poorly.run \
--cc=simona@ffwll.ch \
--cc=thierry.reding@gmail.com \
--cc=thomas.hellstrom@linux.intel.com \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox