dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@collabora.com>
To: Faith Ekstrand <faith@gfxstrand.net>
Cc: dri-devel@lists.freedesktop.org,
	Faith Ekstrand <faith.ekstrand@collabora.com>
Subject: Re: [PATCH 1/7] drm/shmem: Add a drm_gem_shmem_sync_mmap() helper
Date: Fri, 22 Aug 2025 17:13:54 +0200	[thread overview]
Message-ID: <20250822171354.3c125883@fedora> (raw)
In-Reply-To: <20250822142954.902402-2-faith.ekstrand@collabora.com>

On Fri, 22 Aug 2025 10:29:10 -0400
Faith Ekstrand <faith@gfxstrand.net> wrote:

> 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.
> 
> Signed-off-by: Faith Ekstrand <faith.ekstrand@collabora.com>
> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 64 ++++++++++++++++++++++++++
>  include/drm/drm_gem_shmem_helper.h     |  3 ++
>  2 files changed, 67 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 8ac0b1fa5287..50907c1fa11f 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -658,6 +658,70 @@ 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_mmap - Sync memor-mapped data to/from the device

                                     ^ CPU-mapped

> + * @shmem: shmem GEM object
> + * @offset: Offset into the GEM object
> + * @size: Size of the area to sync
> + *
> + * Returns:
> + * 0 on success or a negative error code on failure.
> + */
> +int
> +drm_gem_shmem_sync_mmap(struct drm_gem_shmem_object *shmem,
> +			size_t offset, size_t size,
> +			enum dma_data_direction dir)

Feels weird to name this function sync_mmap when all it's being passed
is a region is a GEM, not a virtual address in the process address
space. I can also imagine this helper being used for kernel-mapped BOs,
making _mmap a bit of a lie. Maybe we should just go for _sync() or
_sync_cpu(). 

> +{
> +	struct drm_device *dev = shmem->base.dev;
> +	struct sg_table *sgt;
> +	struct scatterlist *sgl;
> +	unsigned int count;
> +
> +	if (dir == DMA_NONE)
> +		return 0;
> +
> +	/* Don't bother if it's WC-mapped */
> +	if (shmem->map_wc)
> +		return 0;
> +
> +	if (size == 0)
> +		return 0;
> +
> +	if (offset + size < offset || offset + size > shmem->base.size)
> +		return -EINVAL;
> +
> +	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;
> +
> +		if (dir == DMA_FROM_DEVICE)
> +			dma_sync_single_for_cpu(dev->dev, paddr, len, dir);
> +		else
> +			dma_sync_single_for_device(dev->dev, paddr, len, dir);
> +	}
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(drm_gem_shmem_sync_mmap);
> +
>  /**
>   * 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 92f5db84b9c2..8e057a8e6c83 100644
> --- a/include/drm/drm_gem_shmem_helper.h
> +++ b/include/drm/drm_gem_shmem_helper.h
> @@ -121,6 +121,9 @@ int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem,
>  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);
> +int drm_gem_shmem_sync_mmap(struct drm_gem_shmem_object *shmem,
> +			    size_t offset, size_t size,
> +			    enum dma_data_direction dir);
>  
>  int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem);
>  void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem);


  reply	other threads:[~2025-08-22 15:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22 14:29 [PATCH 0/7] panfrost,panthor: Cached maps and explicit flushing Faith Ekstrand
2025-08-22 14:29 ` [PATCH 1/7] drm/shmem: Add a drm_gem_shmem_sync_mmap() helper Faith Ekstrand
2025-08-22 15:13   ` Boris Brezillon [this message]
2025-08-23  5:36   ` kernel test robot
2025-09-01  8:18   ` Adrian Larumbe
2025-09-01  8:47     ` Boris Brezillon
2025-08-22 14:29 ` [PATCH 2/7] drm/panthor: Add flag to map GEM object Write-Back Cacheable Faith Ekstrand
2025-08-22 15:19   ` Boris Brezillon
2025-08-22 14:29 ` [PATCH 3/7] drm/panthor: Add a PANTHOR_BO_SYNC ioctl Faith Ekstrand
2025-08-22 15:28   ` Boris Brezillon
2025-08-22 14:29 ` [PATCH 4/7] drm/panthor: Bump the driver version to 1.6 Faith Ekstrand
2025-09-01  8:21   ` Adrian Larumbe
2025-08-22 14:29 ` [PATCH 5/7] drm/panfrost: Add flag to map GEM object Write-Back Cacheable Faith Ekstrand
2025-09-01  8:24   ` Adrian Larumbe
2025-08-22 14:29 ` [PATCH 6/7] drm/panfrost: Add a PANFROST_SYNC_BO ioctl Faith Ekstrand
2025-08-24  4:01   ` kernel test robot
2025-08-22 14:29 ` [PATCH 7/7] drm/panfrost: Bump the driver version to 1.5 Faith Ekstrand
2025-08-22 15:05 ` [PATCH 0/7] panfrost,panthor: Cached maps and explicit flushing 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=20250822171354.3c125883@fedora \
    --to=boris.brezillon@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=faith.ekstrand@collabora.com \
    --cc=faith@gfxstrand.net \
    /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