diff mbox series

[v5,2/5] drm/prime: Add drm_gem_prime_mmap()

Message ID 20181017130454.44292-3-noralf@tronnes.org (mailing list archive)
State New, archived
Headers show
Series drm: Add shmem GEM library | expand

Commit Message

Noralf Trønnes Oct. 17, 2018, 1:04 p.m. UTC
Add a generic PRIME GEM mmap function.

Suggested-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
---
 drivers/gpu/drm/drm_prime.c | 37 +++++++++++++++++++++++++++++++++++++
 include/drm/drm_prime.h     |  1 +
 2 files changed, 38 insertions(+)

Comments

Daniel Vetter Oct. 17, 2018, 3:22 p.m. UTC | #1
On Wed, Oct 17, 2018 at 03:04:51PM +0200, Noralf Trønnes wrote:
> Add a generic PRIME GEM mmap function.
> 
> Suggested-by: Daniel Vetter <daniel.vetter@ffwll.ch>
> Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
> ---
>  drivers/gpu/drm/drm_prime.c | 37 +++++++++++++++++++++++++++++++++++++
>  include/drm/drm_prime.h     |  1 +
>  2 files changed, 38 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
> index ba6c7e02a2ae..42abf98c1d4a 100644
> --- a/drivers/gpu/drm/drm_prime.c
> +++ b/drivers/gpu/drm/drm_prime.c
> @@ -651,6 +651,43 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev,
>  }
>  EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
>  
> +/**
> + * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
> + * @obj: GEM object
> + * @vma: Virtual address range
> + *
> + * This function sets up a userspace mapping for PRIME exported buffers using
> + * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
> + * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
> + * called to set up the mapping.
> + *
> + * Drivers can use this as their &drm_driver->gem_prime_mmap callback.

s/->/./ for the kerneldoc.

Otherwise looks good to me, Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

We indeed can't make this the default because only dumb buffers are
guaranteed to be coherent, other mmap implementations might not be. And
for those the driver must provide the right begin/end_cpu_access callbacks
in the dma_buf_ops structure.

Also, would be neat if we could slightly demidlayer the helpers here, so
that it's less of an all-or-nothing decisions. But that's for another
time.
-Daniel


> + */
> +int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
> +{
> +	/* Used by drm_gem_mmap() to lookup the GEM object */
> +	struct drm_file priv = {
> +		.minor = obj->dev->primary,
> +	};
> +	struct file fil = {
> +		.private_data = &priv,
> +	};
> +	int ret;
> +
> +	ret = drm_vma_node_allow(&obj->vma_node, &priv);
> +	if (ret)
> +		return ret;
> +
> +	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
> +
> +	ret = obj->dev->driver->fops->mmap(&fil, vma);
> +
> +	drm_vma_node_revoke(&obj->vma_node, &priv);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL(drm_gem_prime_mmap);
> +
>  /**
>   * drm_gem_prime_import_dev - core implementation of the import callback
>   * @dev: drm_device to import into
> diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h
> index e2032fbc0f08..b03731a3f079 100644
> --- a/include/drm/drm_prime.h
> +++ b/include/drm/drm_prime.h
> @@ -70,6 +70,7 @@ struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
>  int drm_gem_prime_handle_to_fd(struct drm_device *dev,
>  			       struct drm_file *file_priv, uint32_t handle, uint32_t flags,
>  			       int *prime_fd);
> +int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
>  struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
>  					    struct dma_buf *dma_buf);
>  
> -- 
> 2.15.1
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index ba6c7e02a2ae..42abf98c1d4a 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -651,6 +651,43 @@  int drm_gem_prime_handle_to_fd(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
 
+/**
+ * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
+ * @obj: GEM object
+ * @vma: Virtual address range
+ *
+ * This function sets up a userspace mapping for PRIME exported buffers using
+ * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
+ * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
+ * called to set up the mapping.
+ *
+ * Drivers can use this as their &drm_driver->gem_prime_mmap callback.
+ */
+int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
+{
+	/* Used by drm_gem_mmap() to lookup the GEM object */
+	struct drm_file priv = {
+		.minor = obj->dev->primary,
+	};
+	struct file fil = {
+		.private_data = &priv,
+	};
+	int ret;
+
+	ret = drm_vma_node_allow(&obj->vma_node, &priv);
+	if (ret)
+		return ret;
+
+	vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
+
+	ret = obj->dev->driver->fops->mmap(&fil, vma);
+
+	drm_vma_node_revoke(&obj->vma_node, &priv);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_prime_mmap);
+
 /**
  * drm_gem_prime_import_dev - core implementation of the import callback
  * @dev: drm_device to import into
diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h
index e2032fbc0f08..b03731a3f079 100644
--- a/include/drm/drm_prime.h
+++ b/include/drm/drm_prime.h
@@ -70,6 +70,7 @@  struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
 int drm_gem_prime_handle_to_fd(struct drm_device *dev,
 			       struct drm_file *file_priv, uint32_t handle, uint32_t flags,
 			       int *prime_fd);
+int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
 struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
 					    struct dma_buf *dma_buf);