Prime helpers
Subsystems (7)
cover Prime helpers
This series adds helper functions that abstract the core parts of .gem_prime_import and .gem_prime_export so that drivers don't have to worry about the low-level details. These helpers are optional. A driver can use them by plugging in drm_gem_prime_import and drm_gem_prime_export into the drm_driver structure, or it can bypass them by plugging in its own functions. The first patch adds these helpers, and the later patches switch three drivers over to using them.
Jerome, I tried to address your concerns about code sharing by making the helpers available without making them required.
I haven't yet compile-tested the Exynos change since I don't have a toolchain set up for that, but I'll try to do that today.
Aaron Plattner (4):
drm: add prime helpers
drm/nouveau: use prime helpers
drm/radeon: use prime helpers
drm/exynos: use prime helpers
drivers/gpu/drm/drm_prime.c | 190 ++++++++++++++++++++++++++++- drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 151 ++--------------------- drivers/gpu/drm/exynos/exynos_drm_dmabuf.h | 13 +- drivers/gpu/drm/exynos/exynos_drm_drv.c | 2 + drivers/gpu/drm/nouveau/nouveau_bo.h | 1 - drivers/gpu/drm/nouveau/nouveau_drm.c | 9 +- drivers/gpu/drm/nouveau/nouveau_gem.c | 2 - drivers/gpu/drm/nouveau/nouveau_gem.h | 11 +- drivers/gpu/drm/nouveau/nouveau_prime.c | 172 ++++---------------------- drivers/gpu/drm/radeon/radeon_drv.c | 17 +-- drivers/gpu/drm/radeon/radeon_prime.c | 169 ++++--------------------- include/drm/drmP.h | 15 +++ 12 files changed, 288 insertions(+), 464 deletions(-)
Signature · 1 line
1.8.0.1
PATCH 1/4 drm: add prime helpers
Instead of reimplementing all of the dma_buf functionality in every driver, create helpers drm_prime_import and drm_prime_export that implement them in terms of new, lower-level hook functions:
gem_prime_pin: callback when a buffer is created, used to pin buffers into GTT
gem_prime_get_pages: convert a drm_gem_object to an sg_table for export
gem_prime_import_sg: convert an sg_table into a drm_gem_object gem_prime_vmap, gem_prime_vunmap: map and unmap an object
These hooks are optional; drivers can opt in by using drm_gem_prime_import and drm_gem_prime_export as the .gem_prime_import and .gem_prime_export fields of struct drm_driver.
--- drivers/gpu/drm/drm_prime.c | 190 +++++++++++++++++++++++++++++++++++++++++++- include/drm/drmP.h | 15 ++++ 2 files changed, 204 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +189 -1
@@ -53,7 +53,8 @@* Self-importing: if userspace is using PRIME as a replacement for flink * then it will get a fd->handle request for a GEM object that it created. * Drivers should detect this situation and return back the gem object -* from the dma-buf private. +* from the dma-buf private. Prime will do this automatically for drivers that +* use the drm_gem_prime_{import,export} helpers. */ struct drm_prime_member {
@@ -62,6 +63,146 @@ struct drm_prime_member {uint32_t handle; }; +static struct sg_table *drm_gem_map_dma_buf (struct dma_buf_attachment *attach, +enum dma_data_direction dir) +{ +struct drm_gem_object *obj = attach->dmabuf->priv; +struct sg_table *st; + +mutex_lock_interruptible (&obj->dev->struct_mutex); + + st = obj->dev->driver->gem_prime_get_pages (obj); + +if (!IS_ERR_OR_NULL (st)) +dma_map_sg (attach->dev, st->sgl, st->nents, dir); + +mutex_unlock (&obj->dev->struct_mutex); +return st; +} + +static void drm_gem_unmap_dma_buf (struct dma_buf_attachment *attach, +struct sg_table *sg,enum dma_data_direction dir) +{ +dma_unmap_sg (attach->dev, sg->sgl, sg->nents, dir); +sg_free_table (sg); +kfree (sg); +} + +static void drm_gem_dmabuf_release (struct dma_buf *dma_buf) +{ +struct drm_gem_object *obj = dma_buf->priv; + +if (obj->export_dma_buf == dma_buf) { +/* drop the reference on the export fd holds */ + obj->export_dma_buf =NULL ; +drm_gem_object_unreference_unlocked (obj); + } +} + +static void *drm_gem_dmabuf_vmap (struct dma_buf *dma_buf) +{ +struct drm_gem_object *obj = dma_buf->priv; +struct drm_device *dev = obj->dev; +void *virtual =ERR_PTR (-EINVAL ); + +if (!dev->driver->gem_prime_vmap) +return ERR_PTR (-EINVAL ); + +mutex_lock (&dev->struct_mutex); +if (obj->vmapping_count) { + obj->vmapping_count++; + virtual = obj->vmapping_ptr; + goto out_unlock; + } + + virtual = dev->driver->gem_prime_vmap (obj); +if (IS_ERR (virtual)) { +mutex_unlock (&dev->struct_mutex); +return virtual; + } + obj->vmapping_count =1 ; + obj->vmapping_ptr = virtual; +out_unlock: +mutex_unlock (&dev->struct_mutex); +return obj->vmapping_ptr; +} + +static void drm_gem_dmabuf_vunmap (struct dma_buf *dma_buf,void *vaddr) +{ +struct drm_gem_object *obj = dma_buf->priv; +struct drm_device *dev = obj->dev; + +mutex_lock (&dev->struct_mutex); + obj->vmapping_count--; +if (obj->vmapping_count ==0 ) { + dev->driver->gem_prime_vunmap (obj); + obj->vmapping_ptr =NULL ; + } +mutex_unlock (&dev->struct_mutex); +} + +static void *drm_gem_dmabuf_kmap_atomic (struct dma_buf *dma_buf, +unsigned long page_num) +{ +return NULL ; +} + +static void drm_gem_dmabuf_kunmap_atomic (struct dma_buf *dma_buf, +unsigned long page_num,void *addr) +{ + +} +static void *drm_gem_dmabuf_kmap (struct dma_buf *dma_buf, +unsigned long page_num) +{ +return NULL ; +} + +static void drm_gem_dmabuf_kunmap (struct dma_buf *dma_buf, +unsigned long page_num,void *addr) +{ + +} + +static int drm_gem_dmabuf_mmap (struct dma_buf *dma_buf, +struct vm_area_struct *vma) +{ +return -EINVAL ; +} + +static int drm_gem_begin_cpu_access (struct dma_buf *dma_buf, +size_t start,size_t length,enum dma_data_direction direction) +{ +return -EINVAL ; +} + +static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = { + .map_dma_buf = drm_gem_map_dma_buf, + .unmap_dma_buf = drm_gem_unmap_dma_buf, + .release = drm_gem_dmabuf_release, + .kmap = drm_gem_dmabuf_kmap, + .kmap_atomic = drm_gem_dmabuf_kmap_atomic, + .kunmap = drm_gem_dmabuf_kunmap, + .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic, + .mmap = drm_gem_dmabuf_mmap, + .vmap = drm_gem_dmabuf_vmap, + .vunmap = drm_gem_dmabuf_vunmap, + .begin_cpu_access = drm_gem_begin_cpu_access, +}; + +struct dma_buf *drm_gem_prime_export (struct drm_device *dev, +struct drm_gem_object *obj,int flags) +{ +if (dev->driver->gem_prime_pin) { +int ret = dev->driver->gem_prime_pin (obj); +if (ret) +return ERR_PTR (ret); + } +return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, +0600 ); +} +EXPORT_SYMBOL (drm_gem_prime_export); +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)
@@ -117,6 +258,53 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, }EXPORT_SYMBOL (drm_gem_prime_handle_to_fd); +struct drm_gem_object *drm_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf) +{ +struct dma_buf_attachment *attach; +struct sg_table *sg; +struct drm_gem_object *obj; +int ret; + +if (!dev->driver->gem_prime_import_sg) +return ERR_PTR (-EINVAL ); + +if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) { + obj = dma_buf->priv; +if (obj->dev == dev) { +drm_gem_object_reference (obj); +return obj; + } + } + + attach =dma_buf_attach (dma_buf, dev->dev); +if (IS_ERR (attach)) +return ERR_PTR (PTR_ERR (attach)); + + sg =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); +if (IS_ERR_OR_NULL (sg)) { + ret =PTR_ERR (sg); + goto fail_detach; + } + + obj = dev->driver->gem_prime_import_sg (dev, dma_buf->size, sg); +if (IS_ERR (obj)) { + ret =PTR_ERR (obj); + goto fail_unmap; + } + + obj->import_attach = attach; + +return obj; + +fail_unmap: +dma_buf_unmap_attachment (attach, sg,DMA_BIDIRECTIONAL ); +fail_detach: +dma_buf_detach (dma_buf, attach); +return ERR_PTR (ret); +} +EXPORT_SYMBOL (drm_gem_prime_import); +int drm_gem_prime_fd_to_handle (struct drm_device *dev,struct drm_file *file_priv,int prime_fd,uint32_t *handle) {
include/drm/drmP.h +15 -0
@@ -674,6 +674,10 @@ struct drm_gem_object {/* dma buf attachment backing this object */ struct dma_buf_attachment *import_attach; + +/* vmap information */ +int vmapping_count; +void *vmapping_ptr; };#include <drm/drm_crtc.h>
@@ -919,6 +923,13 @@ struct drm_driver {/* import dmabuf -> GEM */ struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,struct dma_buf *dma_buf); +/* low-level interface used by drm_gem_prime_{import,export} */ +int (*gem_prime_pin)(struct drm_gem_object *obj); +struct sg_table *(*gem_prime_get_pages)(struct drm_gem_object *obj); +struct drm_gem_object *(*gem_prime_import_sg)(struct drm_device *dev, +size_t size,struct sg_table *sg); +void *(*gem_prime_vmap)(struct drm_gem_object *obj); +void (*gem_prime_vunmap)(struct drm_gem_object *obj);/* vga arb irq handler */ void (*vgaarb_irq)(struct drm_device *dev,bool state);
@@ -1540,9 +1551,13 @@ extern int drm_clients_info(struct seq_file *m, void* data);extern int drm_gem_name_info (struct seq_file *m,void *data); +extern struct dma_buf *drm_gem_prime_export (struct drm_device *dev, +struct drm_gem_object *obj,int flags);extern 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); +extern struct drm_gem_object *drm_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf);extern int drm_gem_prime_fd_to_handle (struct drm_device *dev,struct drm_file *file_priv,int prime_fd,uint32_t *handle);
Signature · 1 line
1.8.0.1
PATCH 3/4 drm/radeon: use prime helpers
In reply to Aaron PlattnerSimplify the Radeon prime implementation by using the default behavior provided by drm_gem_prime_import and drm_gem_prime_export.
--- drivers/gpu/drm/radeon/radeon_drv.c | 17 ++-- drivers/gpu/drm/radeon/radeon_prime.c | 169 +++++----------------------------- 2 files changed, 31 insertions(+), 155 deletions(-)
drivers/gpu/drm/radeon/radeon_drv.c +10 -7
@@ -113,11 +113,11 @@ int radeon_mode_dumb_create(struct drm_file *file_priv,int radeon_mode_dumb_destroy (struct drm_file *file_priv,struct drm_device *dev,uint32_t handle); -struct dma_buf *radeon_gem_prime_export (struct drm_device *dev, -struct drm_gem_object *obj, -int flags); -struct drm_gem_object *radeon_gem_prime_import (struct drm_device *dev, -struct dma_buf *dma_buf); +struct sg_table *radeon_gem_prime_get_pages (struct drm_gem_object *obj); +struct drm_gem_object *radeon_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sg); +int radeon_gem_prime_pin (struct drm_gem_object *obj);#if defined(CONFIG_DEBUG_FS )int radeon_debugfs_init (struct drm_minor *minor);
@@ -392,8 +392,11 @@ static struct drm_driver kms_driver = { .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, - .gem_prime_export = radeon_gem_prime_export, - .gem_prime_import = radeon_gem_prime_import, + .gem_prime_export = drm_gem_prime_export, + .gem_prime_import = drm_gem_prime_import, + .gem_prime_pin = radeon_gem_prime_pin, + .gem_prime_get_pages = radeon_gem_prime_get_pages, + .gem_prime_import_sg = radeon_gem_prime_import_sg, .name = DRIVER_NAME, .desc = DRIVER_DESC,
drivers/gpu/drm/radeon/radeon_prime.c +21 -148
219 lines not on this page · raw message
Signature · 1 line
1.8.0.1
PATCH 2/4 drm/nouveau: use prime helpers
In reply to Aaron PlattnerSimplify the Nouveau prime implementation by using the default behavior provided by drm_gem_prime_import and drm_gem_prime_export.
--- drivers/gpu/drm/nouveau/nouveau_bo.h | 1 - drivers/gpu/drm/nouveau/nouveau_drm.c | 9 +- drivers/gpu/drm/nouveau/nouveau_gem.c | 2 - drivers/gpu/drm/nouveau/nouveau_gem.h | 11 +- drivers/gpu/drm/nouveau/nouveau_prime.c | 172 ++++---------------------------- 5 files changed, 35 insertions(+), 160 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.h +0 -1
@@ -31,7 +31,6 @@ struct nouveau_bo {int pin_refcnt;struct ttm_bo_kmap_obj dma_buf_vmap; -int vmapping_count; }; static inline struct nouveau_bo *
drivers/gpu/drm/nouveau/nouveau_drm.c +7 -2
@@ -644,8 +644,13 @@ driver = { .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, - .gem_prime_export = nouveau_gem_prime_export, - .gem_prime_import = nouveau_gem_prime_import, + .gem_prime_export = drm_gem_prime_export, + .gem_prime_import = drm_gem_prime_import, + .gem_prime_pin = nouveau_gem_prime_pin, + .gem_prime_get_pages = nouveau_gem_prime_get_pages, + .gem_prime_import_sg = nouveau_gem_prime_import_sg, + .gem_prime_vmap = nouveau_gem_prime_vmap, + .gem_prime_vunmap = nouveau_gem_prime_vunmap, .gem_init_object = nouveau_gem_object_new, .gem_free_object = nouveau_gem_object_del,
drivers/gpu/drm/nouveau/nouveau_gem.c +0 -2
@@ -24,8 +24,6 @@* */ -#include <linux/dma-buf.h> -#include <subdev/fb.h> #include "nouveau_drm.h"
drivers/gpu/drm/nouveau/nouveau_gem.h +7 -4
@@ -35,9 +35,12 @@ extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *,extern int nouveau_gem_ioctl_info (struct drm_device *,void *,struct drm_file *); -extern struct dma_buf *nouveau_gem_prime_export (struct drm_device *dev, -struct drm_gem_object *obj,int flags); -extern struct drm_gem_object *nouveau_gem_prime_import (struct drm_device *dev, -struct dma_buf *dma_buf); +extern int nouveau_gem_prime_pin (struct drm_gem_object *); +extern struct sg_table *nouveau_gem_prime_get_pages (struct drm_gem_object *); +extern struct drm_gem_object *nouveau_gem_prime_import_sg (struct drm_device *, +size_t size, +struct sg_table *); +extern void *nouveau_gem_prime_vmap (struct drm_gem_object *); +extern void nouveau_gem_prime_vunmap (struct drm_gem_object *);#endif
drivers/gpu/drm/nouveau/nouveau_prime.c +21 -151
222 lines not on this page · raw message
Signature · 1 line
1.8.0.1
PATCH 4/4 drm/exynos: use prime helpers
In reply to Aaron PlattnerSimplify the Exynos prime implementation by using the default behavior provided by drm_gem_prime_import and drm_gem_prime_export.
---
I still need to find a system to test this.
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 151 ++--------------------------- drivers/gpu/drm/exynos/exynos_drm_dmabuf.h | 13 ++- drivers/gpu/drm/exynos/exynos_drm_drv.c | 2 + 3 files changed, 18 insertions(+), 148 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +8 -143
@@ -28,8 +28,6 @@#include "exynos_drm_drv.h" #include "exynos_drm_gem.h" -#include <linux/dma-buf.h> -static struct sg_table *exynos_get_sgt (struct drm_device *drm_dev,struct exynos_drm_gem_buf *buf) {
@@ -56,15 +54,12 @@ out:return NULL ; } -static struct sg_table * -exynos_gem_map_dma_buf (struct dma_buf_attachment *attach, -enum dma_data_direction dir) +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj) { -struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv; +struct exynos_drm_gem_obj *gem_obj =to_exynos_gem_obj (obj);struct drm_device *dev = gem_obj->base.dev;struct exynos_drm_gem_buf *buf;struct sg_table *sgt =NULL ; -int nents;DRM_DEBUG_PRIME ("%s\n" , __FILE__);
@@ -74,119 +69,20 @@ static struct sg_table *return sgt; } -mutex_lock (&dev->struct_mutex); - sgt =exynos_get_sgt (dev, buf);if (!sgt) - goto err_unlock; - - nents =dma_map_sg (attach->dev, sgt->sgl, sgt->nents, dir); -if (!nents) { -DRM_ERROR ("failed to map sgl with iommu.\n" ); - sgt =NULL ; - goto err_unlock; - } + goto err;DRM_DEBUG_PRIME ("buffer size = 0x%lx\n" , buf->size); -err_unlock: -mutex_unlock (&dev->struct_mutex); +err:return sgt; } -static void exynos_gem_unmap_dma_buf (struct dma_buf_attachment *attach, -struct sg_table *sgt, -enum dma_data_direction dir) -{ -dma_unmap_sg (attach->dev, sgt->sgl, sgt->nents, dir); - -sg_free_table (sgt); -kfree (sgt); - sgt =NULL ; -} - -static void exynos_dmabuf_release (struct dma_buf *dmabuf) +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sg) { -struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv; - -DRM_DEBUG_PRIME ("%s\n" , __FILE__); - -/* -* exynos_dmabuf_release() call means that file object's -* f_count is 0 and it calls drm_gem_object_handle_unreference() -* to drop the references that these values had been increased -* at drm_prime_handle_to_fd() -*/ -if (exynos_gem_obj->base.export_dma_buf == dmabuf) { - exynos_gem_obj->base.export_dma_buf =NULL ; - -/* -* drop this gem object refcount to release allocated buffer -* and resources. -*/ -drm_gem_object_unreference_unlocked (&exynos_gem_obj->base); - } -} - -static void *exynos_gem_dmabuf_kmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num, -void *addr) -{ -/* TODO */ -} - -static void *exynos_gem_dmabuf_kmap (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap (struct dma_buf *dma_buf, -unsigned long page_num,void *addr) -{ -/* TODO */ -} - -static int exynos_gem_dmabuf_mmap (struct dma_buf *dma_buf, -struct vm_area_struct *vma) -{ -return -ENOTTY ; -} - -static struct dma_buf_ops exynos_dmabuf_ops = { - .map_dma_buf = exynos_gem_map_dma_buf, - .unmap_dma_buf = exynos_gem_unmap_dma_buf, - .kmap = exynos_gem_dmabuf_kmap, - .kmap_atomic = exynos_gem_dmabuf_kmap_atomic, - .kunmap = exynos_gem_dmabuf_kunmap, - .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic, - .mmap = exynos_gem_dmabuf_mmap, - .release = exynos_dmabuf_release, -}; - -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags) -{ -struct exynos_drm_gem_obj *exynos_gem_obj =to_exynos_gem_obj (obj); - -return dma_buf_export (exynos_gem_obj, &exynos_dmabuf_ops, - exynos_gem_obj->base.size,0600 ); -} - -struct drm_gem_object *exynos_dmabuf_prime_import (struct drm_device *drm_dev, -struct dma_buf *dma_buf) -{ -struct dma_buf_attachment *attach;struct sg_table *sgt;struct scatterlist *sgl;struct exynos_drm_gem_obj *exynos_gem_obj;
@@ -195,36 +91,10 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,DRM_DEBUG_PRIME ("%s\n" , __FILE__); -/* is this one of own objects? */ -if (dma_buf->ops == &exynos_dmabuf_ops) { -struct drm_gem_object *obj; - - exynos_gem_obj = dma_buf->priv; - obj = &exynos_gem_obj->base; - -/* is it from our device? */ -if (obj->dev == drm_dev) { -drm_gem_object_reference (obj); -return obj; - } - } - - attach =dma_buf_attach (dma_buf, drm_dev->dev); -if (IS_ERR (attach)) -return ERR_PTR (-EINVAL ); - - - sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { - ret =PTR_ERR (sgt); - goto err_buf_detach; - } - buffer =kzalloc (sizeof (*buffer),GFP_KERNEL );if (!buffer) {DRM_ERROR ("failed to allocate exynos_drm_gem_buf.\n" ); - ret = -ENOMEM ; - goto err_unmap_attach; +return ERR_PTR (-ENOMEM ); } exynos_gem_obj =exynos_drm_gem_init (drm_dev, dma_buf->size);
@@ -253,7 +123,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, exynos_gem_obj->buffer = buffer; buffer->sgt = sgt; - exynos_gem_obj->base.import_attach = attach;DRM_DEBUG_PRIME ("dma_addr = 0x%x, size = 0x%lx\n" , buffer->dma_addr, buffer->size);
@@ -263,10 +132,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, err_free_buffer:kfree (buffer); buffer =NULL ; -err_unmap_attach: -dma_buf_unmap_attachment (attach, sgt,DMA_BIDIRECTIONAL ); -err_buf_detach: -dma_buf_detach (dma_buf, attach);return ERR_PTR (ret); }
drivers/gpu/drm/exynos/exynos_drm_dmabuf.h +8 -5
@@ -27,13 +27,16 @@#define _EXYNOS_DRM_DMABUF_H_#ifdef CONFIG_DRM_EXYNOS_DMABUF -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags); - -struct drm_gem_object *exynos_dmabuf_prime_import (struct drm_device *drm_dev, -struct dma_buf *dma_buf); +#define exynos_dmabuf_prime_export drm_gem_prime_export +#define exynos_dmabuf_prime_import drm_gem_prime_import +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj); +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sg);#else #define exynos_dmabuf_prime_export NULL#define exynos_dmabuf_prime_import NULL +#define exynos_gem_prime_get_pages NULL +#define exynos_gem_prime_import_sg NULL#endif #endif
drivers/gpu/drm/exynos/exynos_drm_drv.c +2 -0
@@ -283,6 +283,8 @@ static struct drm_driver exynos_drm_driver = { .prime_fd_to_handle = drm_gem_prime_fd_to_handle, .gem_prime_export = exynos_dmabuf_prime_export, .gem_prime_import = exynos_dmabuf_prime_import, + .gem_prime_get_pages = exynos_gem_prime_get_pages, + .gem_prime_import_sg = exynos_gem_prime_import_sg, .ioctls = exynos_ioctls, .fops = &exynos_drm_driver_fops, .name = DRIVER_NAME,
Signature · 1 line
1.8.0.1
PATCH v2 1/1 drm/exynos: use prime helpers
Simplify the Exynos prime implementation by using the default behavior provided by drm_gem_prime_import and drm_gem_prime_export.
---
v2: fix dumb mistakes now that I have a toolchain that can compile-test this
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 156 ++--------------------------- drivers/gpu/drm/exynos/exynos_drm_dmabuf.h | 13 ++- drivers/gpu/drm/exynos/exynos_drm_drv.c | 2 + 3 files changed, 20 insertions(+), 151 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +10 -146
@@ -28,8 +28,6 @@#include "exynos_drm_drv.h" #include "exynos_drm_gem.h" -#include <linux/dma-buf.h> -static struct sg_table *exynos_get_sgt (struct drm_device *drm_dev,struct exynos_drm_gem_buf *buf) {
@@ -56,15 +54,12 @@ out:return NULL ; } -static struct sg_table * -exynos_gem_map_dma_buf (struct dma_buf_attachment *attach, -enum dma_data_direction dir) +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj) { -struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv; +struct exynos_drm_gem_obj *gem_obj =to_exynos_gem_obj (obj);struct drm_device *dev = gem_obj->base.dev;struct exynos_drm_gem_buf *buf;struct sg_table *sgt =NULL ; -int nents;DRM_DEBUG_PRIME ("%s\n" , __FILE__);
@@ -74,120 +69,20 @@ static struct sg_table *return sgt; } -mutex_lock (&dev->struct_mutex); - sgt =exynos_get_sgt (dev, buf);if (!sgt) - goto err_unlock; - - nents =dma_map_sg (attach->dev, sgt->sgl, sgt->nents, dir); -if (!nents) { -DRM_ERROR ("failed to map sgl with iommu.\n" ); - sgt =NULL ; - goto err_unlock; - } + goto err;DRM_DEBUG_PRIME ("buffer size = 0x%lx\n" , buf->size); -err_unlock: -mutex_unlock (&dev->struct_mutex); +err:return sgt; } -static void exynos_gem_unmap_dma_buf (struct dma_buf_attachment *attach, -struct sg_table *sgt, -enum dma_data_direction dir) -{ -dma_unmap_sg (attach->dev, sgt->sgl, sgt->nents, dir); - -sg_free_table (sgt); -kfree (sgt); - sgt =NULL ; -} - -static void exynos_dmabuf_release (struct dma_buf *dmabuf) +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sgt) { -struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv; - -DRM_DEBUG_PRIME ("%s\n" , __FILE__); - -/* -* exynos_dmabuf_release() call means that file object's -* f_count is 0 and it calls drm_gem_object_handle_unreference() -* to drop the references that these values had been increased -* at drm_prime_handle_to_fd() -*/ -if (exynos_gem_obj->base.export_dma_buf == dmabuf) { - exynos_gem_obj->base.export_dma_buf =NULL ; - -/* -* drop this gem object refcount to release allocated buffer -* and resources. -*/ -drm_gem_object_unreference_unlocked (&exynos_gem_obj->base); - } -} - -static void *exynos_gem_dmabuf_kmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num, -void *addr) -{ -/* TODO */ -} - -static void *exynos_gem_dmabuf_kmap (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap (struct dma_buf *dma_buf, -unsigned long page_num,void *addr) -{ -/* TODO */ -} - -static int exynos_gem_dmabuf_mmap (struct dma_buf *dma_buf, -struct vm_area_struct *vma) -{ -return -ENOTTY ; -} - -static struct dma_buf_ops exynos_dmabuf_ops = { - .map_dma_buf = exynos_gem_map_dma_buf, - .unmap_dma_buf = exynos_gem_unmap_dma_buf, - .kmap = exynos_gem_dmabuf_kmap, - .kmap_atomic = exynos_gem_dmabuf_kmap_atomic, - .kunmap = exynos_gem_dmabuf_kunmap, - .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic, - .mmap = exynos_gem_dmabuf_mmap, - .release = exynos_dmabuf_release, -}; - -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags) -{ -struct exynos_drm_gem_obj *exynos_gem_obj =to_exynos_gem_obj (obj); - -return dma_buf_export (exynos_gem_obj, &exynos_dmabuf_ops, - exynos_gem_obj->base.size,0600 ); -} - -struct drm_gem_object *exynos_dmabuf_prime_import (struct drm_device *drm_dev, -struct dma_buf *dma_buf) -{ -struct dma_buf_attachment *attach; -struct sg_table *sgt;struct scatterlist *sgl;struct exynos_drm_gem_obj *exynos_gem_obj;struct exynos_drm_gem_buf *buffer;
@@ -195,39 +90,13 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,DRM_DEBUG_PRIME ("%s\n" , __FILE__); -/* is this one of own objects? */ -if (dma_buf->ops == &exynos_dmabuf_ops) { -struct drm_gem_object *obj; - - exynos_gem_obj = dma_buf->priv; - obj = &exynos_gem_obj->base; - -/* is it from our device? */ -if (obj->dev == drm_dev) { -drm_gem_object_reference (obj); -return obj; - } - } - - attach =dma_buf_attach (dma_buf, drm_dev->dev); -if (IS_ERR (attach)) -return ERR_PTR (-EINVAL ); - - - sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { - ret =PTR_ERR (sgt); - goto err_buf_detach; - } - buffer =kzalloc (sizeof (*buffer),GFP_KERNEL );if (!buffer) {DRM_ERROR ("failed to allocate exynos_drm_gem_buf.\n" ); - ret = -ENOMEM ; - goto err_unmap_attach; +return ERR_PTR (-ENOMEM ); } - exynos_gem_obj =exynos_drm_gem_init (drm_dev, dma_buf->size); + exynos_gem_obj =exynos_drm_gem_init (dev, size);if (!exynos_gem_obj) { ret = -ENOMEM ; goto err_free_buffer;
@@ -235,7 +104,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, sgl = sgt->sgl; - buffer->size = dma_buf->size; + buffer->size = size; buffer->dma_addr =sg_dma_address (sgl);if (sgt->nents ==1 ) {
@@ -253,7 +122,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, exynos_gem_obj->buffer = buffer; buffer->sgt = sgt; - exynos_gem_obj->base.import_attach = attach;DRM_DEBUG_PRIME ("dma_addr = 0x%x, size = 0x%lx\n" , buffer->dma_addr, buffer->size);
@@ -263,10 +131,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, err_free_buffer:kfree (buffer); buffer =NULL ; -err_unmap_attach: -dma_buf_unmap_attachment (attach, sgt,DMA_BIDIRECTIONAL ); -err_buf_detach: -dma_buf_detach (dma_buf, attach);return ERR_PTR (ret); }
drivers/gpu/drm/exynos/exynos_drm_dmabuf.h +8 -5
@@ -27,13 +27,16 @@#define _EXYNOS_DRM_DMABUF_H_#ifdef CONFIG_DRM_EXYNOS_DMABUF -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags); - -struct drm_gem_object *exynos_dmabuf_prime_import (struct drm_device *drm_dev, -struct dma_buf *dma_buf); +#define exynos_dmabuf_prime_export drm_gem_prime_export +#define exynos_dmabuf_prime_import drm_gem_prime_import +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj); +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sg);#else #define exynos_dmabuf_prime_export NULL#define exynos_dmabuf_prime_import NULL +#define exynos_gem_prime_get_pages NULL +#define exynos_gem_prime_import_sg NULL#endif #endif
drivers/gpu/drm/exynos/exynos_drm_drv.c +2 -0
@@ -283,6 +283,8 @@ static struct drm_driver exynos_drm_driver = { .prime_fd_to_handle = drm_gem_prime_fd_to_handle, .gem_prime_export = exynos_dmabuf_prime_export, .gem_prime_import = exynos_dmabuf_prime_import, + .gem_prime_get_pages = exynos_gem_prime_get_pages, + .gem_prime_import_sg = exynos_gem_prime_import_sg, .ioctls = exynos_ioctls, .fops = &exynos_drm_driver_fops, .name = DRIVER_NAME,
Signature · 1 line
1.8.0.1
Re: [PATCH 1/4] drm: add prime helpers
In reply to Aaron PlattnerQuoted 9 lines · Aaron Plattner
Instead of reimplementing all of the
dma_buffunctionality in every driver, create helpersdrm_prime_importanddrm_prime_exportthat implement them in terms of new, lower-level hook functions:
gem_prime_pin: callback when a buffer is created, used to pin buffers into GTT
gem_prime_get_pages: convert adrm_gem_objectto ansg_tablefor export
gem_prime_import_sg: convert ansg_tableinto adrm_gem_objectgem_prime_vmap,gem_prime_vunmap: map and unmap an object
Quoted 5 lines
These hooks are optional; drivers can opt in by using
drm_gem_prime_importanddrm_gem_prime_exportas the .gem_prime_import and .gem_prime_export fields of structdrm_driver.Signed-off-by Aaron Plattner <[email protected]>
A few comments:
- Can you please add kerneldoc entries for these new helpers? Laurent Pinchart started a nice drm kerneldoc as an overview. And since then we've at least integrated the kerneldoc api reference at a nice place there and brushed it up a bit when touching drm core or helpers in a bigger patch series. See e.g. my recent dp helper series for what I'm looking for. I think we should have kerneldoc at least for the exported functions.
- Just an idea for all the essential noop cpu helpers: Maybe just call them dma_buf*noop and shovel them as convenience helpers into the dma-buf.c code in the core, for drivers that don't want/can't/won't bother to implement the cpu access support. Related: Exporting the functions in general so that drivers could pick&choose
- s/gem_prime_get_pages/gem_prime_get_sg/ drm/i915 now uses sg_tables everywhere to manage backing storage, and we're starting to use the stolen memory, too. Stolen memory is not struct page backed, so better make this clear in the function calls. I know that the current prime/dma_buf code from Dave doesn't really work too well (*cough*) if the backing storage isn't struct page backed, at least on the ttm import side. This also links in with my 2nd comment above - dma_buf requires that exporter map the sg into the importers device space to support fake subdevices which share the exporters iommu/gart, hence such incetious exporter might want to overwrite some of the functions.
- To make it a first-class helper and remove any and all midlayer stints from this (I truly loath midlayers ...) maybe shovel this into a drm_prime_helper.c file. Also, adding functions to the core vtable for pure helpers is usually not too neat, since it makes it way too damn easy to mix things up and transform the helper into a midlayer by accident. E.g. the crtc helper functions all just use a generic void * pointer for their vtables, other helpers either subclass an existing struct or use their completely independent structs (which the driver can both embed into it's own object struct and then do the right upclassing in the callbacks). tbh haven't looked to closely at the series to asses what would make sense, but we have way too much gunk in the drm vtable already ...
Dunno whether this aligns with what you want to achieve here. But on a quick hunch this code feels rather unflexible and just refactors the identical copypasta code back into one copy. Anyway, just figured I'll drop my 2 cents here, feel free to ignore (maybe safe for the last one).
Cheers, Daniel
Quoted 270 lines
--- drivers/gpu/drm/drm_prime.c | 190 +++++++++++++++++++++++++++++++++++++++++++- include/drm/drmP.h | 15 ++++ 2 files changed, 204 insertions(+), 1 deletion(-)drivers/gpu/drm/drm_prime.c +189
-1@@ -53,7 +53,8 @@* Self-importing: if userspace is using PRIME as a replacement for flink * then it will get a fd->handle request for a GEM object that it created. * Drivers should detect this situation and return back the gem object -* from the dma-buf private. +* from the dma-buf private. Prime will do this automatically for drivers that +* use the drm_gem_prime_{import,export} helpers. */ struct drm_prime_member {@@ -62,6 +63,146 @@ struct drm_prime_member {uint32_t handle; }; +static struct sg_table *drm_gem_map_dma_buf (struct dma_buf_attachment *attach, +enum dma_data_direction dir) +{ +struct drm_gem_object *obj = attach->dmabuf->priv; +struct sg_table *st; + +mutex_lock_interruptible (&obj->dev->struct_mutex); + + st = obj->dev->driver->gem_prime_get_pages (obj); + +if (!IS_ERR_OR_NULL (st)) +dma_map_sg (attach->dev, st->sgl, st->nents, dir); + +mutex_unlock (&obj->dev->struct_mutex); +return st; +} + +static void drm_gem_unmap_dma_buf (struct dma_buf_attachment *attach, +struct sg_table *sg,enum dma_data_direction dir) +{ +dma_unmap_sg (attach->dev, sg->sgl, sg->nents, dir); +sg_free_table (sg); +kfree (sg); +} + +static void drm_gem_dmabuf_release (struct dma_buf *dma_buf) +{ +struct drm_gem_object *obj = dma_buf->priv; + +if (obj->export_dma_buf == dma_buf) { +/* drop the reference on the export fd holds */ + obj->export_dma_buf =NULL ; +drm_gem_object_unreference_unlocked (obj); + } +} + +static void *drm_gem_dmabuf_vmap (struct dma_buf *dma_buf) +{ +struct drm_gem_object *obj = dma_buf->priv; +struct drm_device *dev = obj->dev; +void *virtual =ERR_PTR (-EINVAL ); + +if (!dev->driver->gem_prime_vmap) +return ERR_PTR (-EINVAL ); + +mutex_lock (&dev->struct_mutex); +if (obj->vmapping_count) { + obj->vmapping_count++; + virtual = obj->vmapping_ptr; + goto out_unlock; + } + + virtual = dev->driver->gem_prime_vmap (obj); +if (IS_ERR (virtual)) { +mutex_unlock (&dev->struct_mutex); +return virtual; + } + obj->vmapping_count =1 ; + obj->vmapping_ptr = virtual; +out_unlock: +mutex_unlock (&dev->struct_mutex); +return obj->vmapping_ptr; +} + +static void drm_gem_dmabuf_vunmap (struct dma_buf *dma_buf,void *vaddr) +{ +struct drm_gem_object *obj = dma_buf->priv; +struct drm_device *dev = obj->dev; + +mutex_lock (&dev->struct_mutex); + obj->vmapping_count--; +if (obj->vmapping_count ==0 ) { + dev->driver->gem_prime_vunmap (obj); + obj->vmapping_ptr =NULL ; + } +mutex_unlock (&dev->struct_mutex); +} + +static void *drm_gem_dmabuf_kmap_atomic (struct dma_buf *dma_buf, +unsigned long page_num) +{ +return NULL ; +} + +static void drm_gem_dmabuf_kunmap_atomic (struct dma_buf *dma_buf, +unsigned long page_num,void *addr) +{ + +} +static void *drm_gem_dmabuf_kmap (struct dma_buf *dma_buf, +unsigned long page_num) +{ +return NULL ; +} + +static void drm_gem_dmabuf_kunmap (struct dma_buf *dma_buf, +unsigned long page_num,void *addr) +{ + +} + +static int drm_gem_dmabuf_mmap (struct dma_buf *dma_buf, +struct vm_area_struct *vma) +{ +return -EINVAL ; +} + +static int drm_gem_begin_cpu_access (struct dma_buf *dma_buf, +size_t start,size_t length,enum dma_data_direction direction) +{ +return -EINVAL ; +} + +static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = { + .map_dma_buf = drm_gem_map_dma_buf, + .unmap_dma_buf = drm_gem_unmap_dma_buf, + .release = drm_gem_dmabuf_release, + .kmap = drm_gem_dmabuf_kmap, + .kmap_atomic = drm_gem_dmabuf_kmap_atomic, + .kunmap = drm_gem_dmabuf_kunmap, + .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic, + .mmap = drm_gem_dmabuf_mmap, + .vmap = drm_gem_dmabuf_vmap, + .vunmap = drm_gem_dmabuf_vunmap, + .begin_cpu_access = drm_gem_begin_cpu_access, +}; + +struct dma_buf *drm_gem_prime_export (struct drm_device *dev, +struct drm_gem_object *obj,int flags) +{ +if (dev->driver->gem_prime_pin) { +int ret = dev->driver->gem_prime_pin (obj); +if (ret) +return ERR_PTR (ret); + } +return dma_buf_export (obj, &drm_gem_prime_dmabuf_ops, obj->size, +0600 ); +} +EXPORT_SYMBOL (drm_gem_prime_export); +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)@@ -117,6 +258,53 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, }EXPORT_SYMBOL (drm_gem_prime_handle_to_fd); +struct drm_gem_object *drm_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf) +{ +struct dma_buf_attachment *attach; +struct sg_table *sg; +struct drm_gem_object *obj; +int ret; + +if (!dev->driver->gem_prime_import_sg) +return ERR_PTR (-EINVAL ); + +if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) { + obj = dma_buf->priv; +if (obj->dev == dev) { +drm_gem_object_reference (obj); +return obj; + } + } + + attach =dma_buf_attach (dma_buf, dev->dev); +if (IS_ERR (attach)) +return ERR_PTR (PTR_ERR (attach)); + + sg =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); +if (IS_ERR_OR_NULL (sg)) { + ret =PTR_ERR (sg); + goto fail_detach; + } + + obj = dev->driver->gem_prime_import_sg (dev, dma_buf->size, sg); +if (IS_ERR (obj)) { + ret =PTR_ERR (obj); + goto fail_unmap; + } + + obj->import_attach = attach; + +return obj; + +fail_unmap: +dma_buf_unmap_attachment (attach, sg,DMA_BIDIRECTIONAL ); +fail_detach: +dma_buf_detach (dma_buf, attach); +return ERR_PTR (ret); +} +EXPORT_SYMBOL (drm_gem_prime_import); +int drm_gem_prime_fd_to_handle (struct drm_device *dev,struct drm_file *file_priv,int prime_fd,uint32_t *handle) {include/drm/drmP.h +15
-0@@ -674,6 +674,10 @@ struct drm_gem_object {/* dma buf attachment backing this object */ struct dma_buf_attachment *import_attach; + +/* vmap information */ +int vmapping_count; +void *vmapping_ptr; };#include <drm/drm_crtc.h> @@ -919,6 +923,13 @@ struct drm_driver {/* import dmabuf -> GEM */ struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,struct dma_buf *dma_buf); +/* low-level interface used by drm_gem_prime_{import,export} */ +int (*gem_prime_pin)(struct drm_gem_object *obj); +struct sg_table *(*gem_prime_get_pages)(struct drm_gem_object *obj); +struct drm_gem_object *(*gem_prime_import_sg)(struct drm_device *dev, +size_t size,struct sg_table *sg); +void *(*gem_prime_vmap)(struct drm_gem_object *obj); +void (*gem_prime_vunmap)(struct drm_gem_object *obj);/* vga arb irq handler */ void (*vgaarb_irq)(struct drm_device *dev,bool state);@@ -1540,9 +1551,13 @@ extern int drm_clients_info(struct seq_file *m, void* data);extern int drm_gem_name_info (struct seq_file *m,void *data); +extern struct dma_buf *drm_gem_prime_export (struct drm_device *dev, +struct drm_gem_object *obj,int flags);extern 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); +extern struct drm_gem_object *drm_gem_prime_import (struct drm_device *dev, +struct dma_buf *dma_buf);extern int drm_gem_prime_fd_to_handle (struct drm_device *dev,struct drm_file *file_priv,int prime_fd,uint32_t *handle);Signature · 6 lines
1.8.0.1 _______________________________________________ dri-devel mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 1/4] drm: add prime helpers
Quoted 26 lines · Daniel Vetter
Quoted 9 lines · Aaron Plattner
Instead of reimplementing all of the
dma_buffunctionality in every driver, create helpersdrm_prime_importanddrm_prime_exportthat implement them in terms of new, lower-level hook functions:
gem_prime_pin: callback when a buffer is created, used to pin buffers into GTT
gem_prime_get_pages: convert adrm_gem_objectto ansg_tablefor export
gem_prime_import_sg: convert ansg_tableinto adrm_gem_objectgem_prime_vmap,gem_prime_vunmap: map and unmap an objectQuoted 5 lines
These hooks are optional; drivers can opt in by using
drm_gem_prime_importanddrm_gem_prime_exportas the .gem_prime_import and .gem_prime_export fields of structdrm_driver.Signed-off-by Aaron Plattner <[email protected]>A few comments:
- Can you please add kerneldoc entries for these new helpers? Laurent Pinchart started a nice drm kerneldoc as an overview. And since then we've at least integrated the kerneldoc api reference at a nice place there and brushed it up a bit when touching drm core or helpers in a bigger patch series. See e.g. my recent dp helper series for what I'm looking for. I think we should have kerneldoc at least for the exported functions.
Quoted 5 lines
- Just an idea for all the essential noop cpu helpers: Maybe just call them dma_buf*noop and shovel them as convenience helpers into the
dma-buf.ccode in the core, for drivers that don't want/can't/won't bother to implement the cpu access support. Related: Exporting the functions in general so that drivers could pick&choose
One more: For the cpu access noop helpers I'd vote for -ENOTTY as the more canonical "not implemented error, you're talking to the wrong thing" error instead of -EINVAL, which an exporter could throw back to the importer if e.g. the range is outside of the size of the dma_buf. With a quick dma_buf doc update we could then bless this as the official way to denounce cpu access support.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Inki Dae
maintainerRe: [PATCH v2] drm/exynos: use prime helpers
In reply to Aaron PlattnerHi,
CCing media guys.
I agree with you but we should consider one issue released to v4l2.
As you may know, V4L2-based driver uses vb2 as buffer manager and the vb2 includes dmabuf feature>(import and export) And v4l2 uses streaming concept>(qbuf and dqbuf)
With dmabuf and iommu, generally qbuf imports a fd into its own buffer and maps it with its own iommu table calling dma_buf_map_attachment(). And dqbuf calls dma_buf_unmap_attachment() to unmap that buffer from its own iommu table.
But now vb2's unmap_dma_buf callback is nothing to do. I think that the reason is the below issue,
If qbuf maps buffer with iomm table and dqbuf unmaps it from iommu table then it has performance deterioration because qbuf and dqbuf are called repeatedly.
And this means map/unmap are repeated also. So I think media guys moved dma_unmap_sg call from its own unmap_dma_buf callback to detach callback instead.
For this, you can refer to vb2_dc_dmabuf_ops_unmap and vb2_dc_dmabuf_ops_detach function.
So I added the below patch to avoid that performance deterioration and am testing it now.(this patch is derived from videobuf2-dma-contig.c)
Thus, I'm not sure that your common set could cover all the cases including other frameworks. Please give me any opinions.
Thanks,
Inki Dae
2012/12/7 Aaron Plattner <[email protected]>
Quoted 297 lines
Simplify the Exynos prime implementation by using the default behavior provided
bydrm_gem_prime_importanddrm_gem_prime_export.Signed-off-by Aaron Plattner <[email protected]>---
v2: fix dumb mistakes now that I have a toolchain that can compile-test thisdrivers/gpu/drm/exynos/exynos_drm_dmabuf.c | 156 ++--------------------------- drivers/gpu/drm/exynos/exynos_drm_dmabuf.h | 13 ++- drivers/gpu/drm/exynos/exynos_drm_drv.c | 2 + 3 files changed, 20 insertions(+), 151 deletions(-)drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +7
-46@@ -28,8 +28,6 @@#include "exynos_drm_drv.h" #include "exynos_drm_gem.h" -#include <linux/dma-buf.h> -static struct sg_table *exynos_get_sgt (struct drm_device *drm_dev,struct exynos_drm_gem_buf *buf) {@@ -56,15 +54,12 @@ out:return NULL ; } -static struct sg_table * -exynos_gem_map_dma_buf (struct dma_buf_attachment *attach, -enum dma_data_direction dir) +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj) { -struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv; +struct exynos_drm_gem_obj *gem_obj =to_exynos_gem_obj (obj);struct drm_device *dev = gem_obj->base.dev;struct exynos_drm_gem_buf *buf;struct sg_table *sgt =NULL ; -int nents;DRM_DEBUG_PRIME ("%s\n" , __FILE__);@@ -74,120 +69,20 @@ static struct sg_table *return sgt; } -mutex_lock (&dev->struct_mutex); - sgt =exynos_get_sgt (dev, buf);if (!sgt) - goto err_unlock; - - nents =dma_map_sg (attach->dev, sgt->sgl, sgt->nents, dir); -if (!nents) { -DRM_ERROR ("failed to map sgl with iommu.\n" ); - sgt =NULL ; - goto err_unlock; - } + goto err;DRM_DEBUG_PRIME ("buffer size = 0x%lx\n" , buf->size); -err_unlock: -mutex_unlock (&dev->struct_mutex); +err:return sgt; } -static void exynos_gem_unmap_dma_buf (struct dma_buf_attachment *attach, -struct sg_table *sgt, -enum dma_data_direction dir) -{ -dma_unmap_sg (attach->dev, sgt->sgl, sgt->nents, dir); - -sg_free_table (sgt); -kfree (sgt); - sgt =NULL ; -} - -static void exynos_dmabuf_release (struct dma_buf *dmabuf) +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sgt) { -struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv; - -DRM_DEBUG_PRIME ("%s\n" , __FILE__); - -/* -* exynos_dmabuf_release() call means that file object's -* f_count is 0 and it calls drm_gem_object_handle_unreference() -* to drop the references that these values had been increased -* at drm_prime_handle_to_fd() -*/ -if (exynos_gem_obj->base.export_dma_buf == dmabuf) { - exynos_gem_obj->base.export_dma_buf =NULL ; - -/* -* drop this gem object refcount to release allocated buffer
drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +0
-62-* and resources. -*/ -drm_gem_object_unreference_unlocked (&exynos_gem_obj->base); - } -} - -static void *exynos_gem_dmabuf_kmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap_atomic (struct dma_buf *dma_buf, -unsigned long page_num, -void *addr) -{ -/* TODO */ -} - -static void *exynos_gem_dmabuf_kmap (struct dma_buf *dma_buf, -unsigned long page_num) -{ -/* TODO */ - -return NULL ; -} - -static void exynos_gem_dmabuf_kunmap (struct dma_buf *dma_buf, -unsigned long page_num,void *addr) -{ -/* TODO */ -} - -static int exynos_gem_dmabuf_mmap (struct dma_buf *dma_buf, -struct vm_area_struct *vma) -{ -return -ENOTTY ; -} - -static struct dma_buf_ops exynos_dmabuf_ops = { - .map_dma_buf = exynos_gem_map_dma_buf, - .unmap_dma_buf = exynos_gem_unmap_dma_buf, - .kmap = exynos_gem_dmabuf_kmap, - .kmap_atomic = exynos_gem_dmabuf_kmap_atomic, - .kunmap = exynos_gem_dmabuf_kunmap, - .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic, - .mmap = exynos_gem_dmabuf_mmap, - .release = exynos_dmabuf_release, -}; - -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags) -{ -struct exynos_drm_gem_obj *exynos_gem_obj =to_exynos_gem_obj (obj); - -return dma_buf_export (exynos_gem_obj, &exynos_dmabuf_ops, - exynos_gem_obj->base.size,0600 ); -} - -struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device
*drm_dev,drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +2
-32- struct dma_buf *dma_buf) -{ -struct dma_buf_attachment *attach; -struct sg_table *sgt;struct scatterlist *sgl;struct exynos_drm_gem_obj *exynos_gem_obj;struct exynos_drm_gem_buf *buffer;@@ -195,39 +90,13 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, DRM_DEBUG_PRIME("%s\n", __FILE__); -/* is this one of own objects? */ -if (dma_buf->ops == &exynos_dmabuf_ops) { -struct drm_gem_object *obj; - - exynos_gem_obj = dma_buf->priv; - obj = &exynos_gem_obj->base; - -/* is it from our device? */ -if (obj->dev == drm_dev) { -drm_gem_object_reference (obj); -return obj; - } - } - - attach =dma_buf_attach (dma_buf, drm_dev->dev); -if (IS_ERR (attach)) -return ERR_PTR (-EINVAL ); - - - sgt =dma_buf_map_attachment (attach,DMA_BIDIRECTIONAL ); -if (IS_ERR_OR_NULL (sgt)) { - ret =PTR_ERR (sgt); - goto err_buf_detach; - } - buffer =kzalloc (sizeof (*buffer),GFP_KERNEL );if (!buffer) {DRM_ERROR ("failed to allocate exynos_drm_gem_buf.\n" ); - ret = -ENOMEM ; - goto err_unmap_attach; +return ERR_PTR (-ENOMEM ); } - exynos_gem_obj =exynos_drm_gem_init (drm_dev, dma_buf->size); + exynos_gem_obj =exynos_drm_gem_init (dev, size);if (!exynos_gem_obj) { ret = -ENOMEM ;goto err_free_buffer;drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +1
-1@@ -235,7 +104,7 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, sgl = sgt->sgl; - buffer->size = dma_buf->size; + buffer->size = size; buffer->dma_addr =sg_dma_address (sgl);if (sgt->nents == 1) {drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +0
-1@@ -253,7 +122,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, exynos_gem_obj->buffer = buffer; buffer->sgt = sgt; - exynos_gem_obj->base.import_attach = attach; DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n",
buffer->dma_addr,
buffer->size);drivers/gpu/drm/exynos/exynos_drm_dmabuf.c +0
-4@@ -263,10 +131,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, err_free_buffer: kfree(buffer); buffer =NULL ; -err_unmap_attach: -dma_buf_unmap_attachment (attach, sgt,DMA_BIDIRECTIONAL ); -err_buf_detach: -dma_buf_detach (dma_buf, attach);return ERR_PTR (ret); }drivers/gpu/drm/exynos/exynos_drm_dmabuf.h +8
-5@@ -27,13 +27,16 @@#define _EXYNOS_DRM_DMABUF_H_#ifdef CONFIG_DRM_EXYNOS_DMABUF -struct dma_buf *exynos_dmabuf_prime_export (struct drm_device *drm_dev, -struct drm_gem_object *obj,int flags); - -struct drm_gem_object *exynos_dmabuf_prime_import (struct drm_device *drm_dev, -struct dma_buf *dma_buf); +#define exynos_dmabuf_prime_export drm_gem_prime_export +#define exynos_dmabuf_prime_import drm_gem_prime_import +struct sg_table *exynos_gem_prime_get_pages (struct drm_gem_object *obj); +struct drm_gem_object *exynos_gem_prime_import_sg (struct drm_device *dev, +size_t size, +struct sg_table *sg);#else #define exynos_dmabuf_prime_export NULL#define exynos_dmabuf_prime_import NULL +#define exynos_gem_prime_get_pages NULL +#define exynos_gem_prime_import_sg NULL#endif #endif
drivers/gpu/drm/exynos/exynos_drm_drv.c +2
-1@@ -283,6 +283,8 @@ static struct drm_driver exynos_drm_driver = { .prime_fd_to_handle = drm_gem_prime_fd_to_handle, .gem_prime_export = exynos_dmabuf_prime_export, .gem_prime_import = exynos_dmabuf_prime_import, + .gem_prime_get_pages = exynos_gem_prime_get_pages, + .gem_prime_import_sg = exynos_gem_prime_import_sg, .ioctls = exynos_ioctls, .fops = &exynos_drm_driver_fops, .name = DRIVER_NAME, --1.8.0.1
Quoted 5 lines
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v2] drm/exynos: use prime helpers
Quoted 2 lines · Inki Dae
Thus, I'm not sure that your common set could cover all the cases including other frameworks. Please give me any opinions.
I don't think that's required - as long as it is fairly useable by many drivers a helper library is good enough. And since it's no midlayer there's no requirement to use it, you're always free to do your own special sauce in your driver. But I think the current rfc could be made a bit more flexible so that more drivers could use at least parts of it, see my other mail.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 1/4] drm: add prime helpers
In reply to Daniel VetterOp 06-12-12 22:50, Daniel Vetter schreef:
Quoted 31 lines · Daniel Vetter
Quoted 25 lines · Aaron Plattner
Quoted 9 lines
Instead of reimplementing all of the
dma_buffunctionality in every driver, create helpersdrm_prime_importanddrm_prime_exportthat implement them in terms of new, lower-level hook functions:
gem_prime_pin: callback when a buffer is created, used to pin buffers into GTT
gem_prime_get_pages: convert adrm_gem_objectto ansg_tablefor export
gem_prime_import_sg: convert ansg_tableinto adrm_gem_objectgem_prime_vmap,gem_prime_vunmap: map and unmap an objectQuoted 5 lines
These hooks are optional; drivers can opt in by using
drm_gem_prime_importanddrm_gem_prime_exportas the .gem_prime_import and .gem_prime_export fields of structdrm_driver.Signed-off-by Aaron Plattner <[email protected]>A few comments:
- Can you please add kerneldoc entries for these new helpers? Laurent Pinchart started a nice drm kerneldoc as an overview. And since then we've at least integrated the kerneldoc api reference at a nice place there and brushed it up a bit when touching drm core or helpers in a bigger patch series. See e.g. my recent dp helper series for what I'm looking for. I think we should have kerneldoc at least for the exported functions.
Quoted 5 lines
- Just an idea for all the essential noop cpu helpers: Maybe just call them dma_buf*noop and shovel them as convenience helpers into the
dma-buf.ccode in the core, for drivers that don't want/can't/won't bother to implement the cpu access support. Related: Exporting the functions in general so that drivers could pick&choose
Quoted 7 lines
One more: For the cpu access noop helpers I'd vote for
-ENOTTYas the more canonical "not implemented error, you're talking to the wrong thing" error instead of-EINVAL, which an exporter could throw back to the importer if e.g. the range is outside of the size of thedma_buf. With a quickdma_bufdoc update we could then bless this as the official way to denounce cpu access support.
Yeah lets reinvent a new error to return, and make it not a typewriter to confuse users, instead of using -ENODEV which would actually be valid and most descriptive here if you read the mmap page:
-ENODEV The underlying file system of the specified file does not support memory mapping.
~Maarten
Re: [PATCH v2] drm/exynos: use prime helpers
In reply to Inki DaeQuoted 26 lines · Inki Dae
Hi,
CCing media guys.
I agree with you but we should consider one issue released to v4l2.
As you may know, V4L2-based driver uses vb2 as buffer manager and the vb2 includes dmabuf feature>(import and export) And v4l2 uses streaming concept>(qbuf and dqbuf)
With dmabuf and iommu, generally qbuf imports a fd into its own buffer and maps it with its own iommu table callingdma_buf_map_attachment(). And dqbuf callsdma_buf_unmap_attachment()to unmap that buffer from its own iommu table.
But now vb2'sunmap_dma_bufcallback is nothing to do. I think that the reason is the below issue,If qbuf maps buffer with iomm table and dqbuf unmaps it from iommu table then it has performance deterioration because qbuf and dqbuf are called repeatedly.
And this means map/unmap are repeated also. So I think media guys moveddma_unmap_sgcall from its ownunmap_dma_bufcallback to detach callback instead.
For this, you can refer tovb2_dc_dmabuf_ops_unmapandvb2_dc_dmabuf_ops_detachfunction.
Quoted 6 lines
So I added the below patch to avoid that performance deterioration and am testing it now.(this patch is derived from
videobuf2-dma-contig.c)
http://git.kernel.org/?p=linux/kernel/git/daeinki/drm-exynos.git;a=commit;h=576b1c3de8b90cf1570b8418b60afd1edaae4e30Thus, I'm not sure that your common set could cover all the cases including other frameworks. Please give me any opinions.
It seems like this adjustment would make perfect sense to add to the helper layer I suggested. E.g., instead of having an exynos_attach structure that caches the sgt, there'd be a struct drm_gem_prime_attach that would do the same thing, and save the sgt it gets from driver->gem_prime_get_sg. Then it would benefit nouveau and radeon, too.
Alternatively, patch #4 could be dropped and Exynos can continue to reimplement all of this core functionality, since the helpers are optional, but I don't see anything about this change that should make it Exynos-specific, unless I'm missing something.
--
Aaron
Re: [PATCH 1/4] drm: add prime helpers
In reply to Daniel VetterQuoted 18 lines · Daniel Vetter
Quoted 9 lines · Aaron Plattner
Instead of reimplementing all of the
dma_buffunctionality in every driver, create helpersdrm_prime_importanddrm_prime_exportthat implement them in terms of new, lower-level hook functions:
gem_prime_pin: callback when a buffer is created, used to pin buffers into GTT
gem_prime_get_pages: convert adrm_gem_objectto ansg_tablefor export
gem_prime_import_sg: convert ansg_tableinto adrm_gem_objectgem_prime_vmap,gem_prime_vunmap: map and unmap an objectQuoted 5 lines
These hooks are optional; drivers can opt in by using
drm_gem_prime_importanddrm_gem_prime_exportas the .gem_prime_import and .gem_prime_export fields of structdrm_driver.Signed-off-by Aaron Plattner <[email protected]>A few comments:
Quoted 7 lines
- Can you please add kerneldoc entries for these new helpers? Laurent Pinchart started a nice drm kerneldoc as an overview. And since then we've at least integrated the kerneldoc api reference at a nice place there and brushed it up a bit when touching drm core or helpers in a bigger patch series. See e.g. my recent dp helper series for what I'm looking for. I think we should have kerneldoc at least for the exported functions.
Good call, I'll look into that.
Quoted 5 lines
- Just an idea for all the essential noop cpu helpers: Maybe just call them dma_buf*noop and shovel them as convenience helpers into the
dma-buf.ccode in the core, for drivers that don't want/can't/won't bother to implement the cpu access support. Related: Exporting the functions in general so that drivers could pick&choose
Seems like a good idea as a follow-on change. Do you think it would make more sense to have noop helpers, or allow them to be NULL in dma-buf.c?
Quoted 10 lines
- s/gem_prime_get_pages/gem_prime_get_sg/ drm/i915 now uses
sg_tableseverywhere to manage backing storage, and we're starting to use the stolen memory, too. Stolen memory is not struct page backed, so better make this clear in the function calls. I know that the current prime/dma_buf code from Dave doesn't really work too well (*cough*) if the backing storage isn't struct page backed, at least on the ttm import side. This also links in with my 2nd comment above -dma_bufrequires that exporter map the sg into the importers device space to support fake subdevices which share the exporters iommu/gart, hence such incetious exporter might want to overwrite some of the functions.
Will do in a v2 set.
Quoted 4 lines
- To make it a first-class helper and remove any and all midlayer stints from this (I truly loath midlayers ...) maybe shovel this into a
drm_prime_helper.cfile. Also, adding functions to the core vtable for pure helpers is usually not too neat, since it makes it way too damn
Quoted 8 lines
easy to mix things up and transform the helper into a midlayer by accident. E.g. the crtc helper functions all just use a generic void * pointer for their vtables, other helpers either subclass an existing struct or use their completely independent structs (which the driver can both embed into it's own object struct and then do the right upclassing in the callbacks). tbh haven't looked to closely at the series to asses what would make sense, but we have way too much gunk in the drm vtable already ...
I'm not sure I fully understand what you mean by this. Are you suggesting creating a separate struct for these functions and having a void* pointer to that in struct drm_driver?
Quoted 4 lines
Dunno whether this aligns with what you want to achieve here. But on a quick hunch this code feels rather unflexible and just refactors the identical copypasta code back into one copy. Anyway, just figured I'll drop my 2 cents here, feel free to ignore (maybe safe for the last one).
I think uncopypasta-ing the current code is beneficial on its own. Do you think doing this now would make it harder to do the further refactoring you suggest later?
--
Aaron
Re: [PATCH 1/4] drm: add prime helpers
Quoted 1 line · Aaron Plattner
On 12/06/2012 01:46 PM, Daniel Vetter wrote:
[snip]
Quoted 9 lines
Quoted 5 lines
- Just an idea for all the essential noop cpu helpers: Maybe just call them dma_buf*noop and shovel them as convenience helpers into the
dma-buf.ccode in the core, for drivers that don't want/can't/won't bother to implement the cpu access support. Related: Exporting the functions in general so that drivers could pick&chooseSeems like a good idea as a follow-on change. Do you think it would make more sense to have noop helpers, or allow them to be NULL in
dma-buf.c?
Yeah, sounds good. Just thought that drm drivers aren't the only ones not caring about cpu access support too much, so an officially blessed way with documentation and unified errno would be good
[snip]
Quoted 13 lines
Quoted 4 lines
- To make it a first-class helper and remove any and all midlayer stints from this (I truly loath midlayers ...) maybe shovel this into a
drm_prime_helper.cfile. Also, adding functions to the core vtable for pure helpers is usually not too neat, since it makes it way too damnQuoted 8 lines
easy to mix things up and transform the helper into a midlayer by accident. E.g. the crtc helper functions all just use a generic void * pointer for their vtables, other helpers either subclass an existing struct or use their completely independent structs (which the driver can both embed into it's own object struct and then do the right upclassing in the callbacks). tbh haven't looked to closely at the series to asses what would make sense, but we have way too much gunk in the drm vtable already ...
Quoted 3 lines
I'm not sure I fully understand what you mean by this. Are you suggesting creating a separate struct for these functions and having a void* pointer to that in struct
drm_driver?
Create a new struct like
struct drm_prime_helper_funcs {
int (*gem_prime_pin)(struct drm_gem_object *obj);
struct sg_table *(*gem_prime_get_pages)(struct drm_gem_object *obj);
struct drm_gem_object *(*gem_prime_import_sg)(struct drm_device *dev,
size_t size, struct sg_table *sg);
void *(*gem_prime_vmap)(struct drm_gem_object *obj);
void (*gem_prime_vunmap)(struct drm_gem_object *obj);
}struct drm_prime_helper_obj {
/* vmap information */
int vmapping_count;
void *vmapping_ptr;
/* any other data the helpers need ... */
struct drm_prime_helper_funcs *funcs;
}Drivers then fill out a func vtable and embedded the helper obj into their own gpu bo struct, i.e.
struct radeon_bo {
struct ttm_bo ttm_bo;
struct gem_buffer_object gem_bo;
struct drm_prime_helper_obj prime_bo;
/* other stuff */
}In the prime helper callbacks in the driver then upcast the prime_bo to the radeon_bo instead of the gem_bo to the radeon bo.
Upsides: Guaranteed to not interfere with drivers not using it, with an imo higher chance that the helper is cleanly separate from core stuff (no guarantee ofc) and so easier to refactor in the future. And drm is full with legacy stuff used by very few drivers, but highly intermingled with drm core used by other drivers (my little holy war here is to slowly refactor those things out and shovel them into drivers), hence why I prefer to have an as clean separation of optional stuff as possible ...
Also, this way allows different drivers to use completely different helper libraries for the same task, without those two helper libraries ever interfering.
Downside: Adds a pointer to each bo struct for drivers using the helpers. Given how big those tend to be, not too onerous. Given And maybe a bit of confusion in driver callbacks, but since the linux container_of is typechecked by the compiler, not too onerous either.
Quoted 8 lines
Quoted 4 lines
Dunno whether this aligns with what you want to achieve here. But on a quick hunch this code feels rather unflexible and just refactors the identical copypasta code back into one copy. Anyway, just figured I'll drop my 2 cents here, feel free to ignore (maybe safe for the last one).
I think uncopypasta-ing the current code is beneficial on its own. Do you think doing this now would make it harder to do the further refactoring you suggest later?
If we later on go with something like the above, it'll require going over all drivers again, doing similar mechanic changes. If no one yet managed to intermingle the helpers with the core in the meantime, that is ;-)
Cheers, Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 1/4] drm: add prime helpers
Quoted 30 lines · Daniel Vetter
Quoted 14 lines · Aaron Plattner
Quoted 4 lines · Daniel Vetter
- To make it a first-class helper and remove any and all midlayer stints from this (I truly loath midlayers ...) maybe shovel this into a
drm_prime_helper.cfile. Also, adding functions to the core vtable for pure helpers is usually not too neat, since it makes it way too damnQuoted 8 lines
easy to mix things up and transform the helper into a midlayer by accident. E.g. the crtc helper functions all just use a generic void * pointer for their vtables, other helpers either subclass an existing struct or use their completely independent structs (which the driver can both embed into it's own object struct and then do the right upclassing in the callbacks). tbh haven't looked to closely at the series to asses what would make sense, but we have way too much gunk in the drm vtable already ...
Quoted 3 lines
I'm not sure I fully understand what you mean by this. Are you suggesting creating a separate struct for these functions and having a void* pointer to that in struct
drm_driver?Create a new struct like
struct drm_prime_helper_funcs { int (*gem_prime_pin)(struct drm_gem_object *obj); struct sg_table *(*gem_prime_get_pages)(struct drm_gem_object *obj); struct drm_gem_object *(*gem_prime_import_sg)(struct drm_device *dev, size_t size, struct sg_table *sg); void *(*gem_prime_vmap)(struct drm_gem_object *obj); void (*gem_prime_vunmap)(struct drm_gem_object *obj); }
Quoted 8 lines
struct drm_prime_helper_obj {
/* vmap information */
int vmapping_count;
void *vmapping_ptr;
/* any other data the helpers need ... */
struct drm_prime_helper_funcs *funcs;
}Ah, I see what you mean. This would also need either a pointer to the drv directly so the helpers can lock drv->struct_mutex, or a helper function to convert from a drm_prime_helper_obj* to a gem_buffer_object* in a driver-specific way since the helper doesn't know the layout of the driver's bo structure.
So it would be something like
struct drm_prime_helper_obj *helper_obj = dma_buf->priv; struct drm_prime_helper_funcs *funcs = helper_obj->funcs; struct drm_gem_object *obj = funcs->get_gem_obj(helper_obj); mutex_lock(&obj->dev->struct_mutex); funcs->whatever(obj); mutex_unlock&obj->dev->struct_mutex);
and then for example, radeon_drm_prime_get_gem_obj would be
struct radeon_bo *bo = container_of(helper_obj, struct radeon_bo,
prime_helper);
return &bo->gem_base;
?
That seems a little over-engineered to me, but I can code it up.
--
Aaron
Quoted 46 lines
Drivers then fill out a func vtable and embedded the helper obj into their own gpu bo struct, i.e.
struct radeon_bo { struct ttm_bo ttm_bo; struct gem_buffer_object gem_bo; struct drm_prime_helper_obj prime_bo; /* other stuff */ }In the prime helper callbacks in the driver then upcast the
prime_boto theradeon_boinstead of thegem_boto the radeon bo.Upsides: Guaranteed to not interfere with drivers not using it, with an imo higher chance that the helper is cleanly separate from core stuff (no guarantee ofc) and so easier to refactor in the future. And drm is full with legacy stuff used by very few drivers, but highly intermingled with drm core used by other drivers (my little holy war here is to slowly refactor those things out and shovel them into drivers), hence why I prefer to have an as clean separation of optional stuff as possible ...
Also, this way allows different drivers to use completely different helper libraries for the same task, without those two helper libraries ever interfering.
Downside: Adds a pointer to each bo struct for drivers using the helpers. Given how big those tend to be, not too onerous. Given And maybe a bit of confusion in driver callbacks, but since the linux
container_ofis typechecked by the compiler, not too onerous either.Quoted 8 lines
Quoted 4 lines
Dunno whether this aligns with what you want to achieve here. But on a quick hunch this code feels rather unflexible and just refactors the identical copypasta code back into one copy. Anyway, just figured I'll drop my 2 cents here, feel free to ignore (maybe safe for the last one).
I think uncopypasta-ing the current code is beneficial on its own. Do you think doing this now would make it harder to do the further refactoring you suggest later?
If we later on go with something like the above, it'll require going over all drivers again, doing similar mechanic changes. If no one yet managed to intermingle the helpers with the core in the meantime, that is ;-)
Cheers, Daniel
Re: [PATCH 1/4] drm: add prime helpers
Quoted 5 lines · Aaron Plattner
Ah, I see what you mean. This would also need either a pointer to the drv directly so the helpers can lock
drv->struct_mutex, or a helper function to convert from a drm_prime_helper_obj* to a gem_buffer_object* in a driver-specific way since the helper doesn't know the layout of the driver's bo structure.
Ah, locking, and it involves dev->struct_mutex. Another pet peeve of mine ;-) Tbh I haven't looked at locking issues when stitching together my quick proposal.
The problem with the dev->struct_mutex lock is that it's everywhere and hence it's way to easy to create deadlocks with it. Especially with prime, where a simple rule like "take this lock first, always" are suddenly really more complicated since gem drivers can assume both the importer and exporter role ... I haven't done a full locking review, but I expect current prime to deadlock (through driver calls) when userspace starts doing funny things.
So imo it's best to move dev->struct_mutex as far away as possible from helper functions and think really hard whether we really need it. I've noticed three functions:
drm_gem_map_dma_buf: We can remove the locking here imo, if drivers need dev->struct_mutex for internal consistency, they can take it in their callbacks. The dma_buf_attachment is very much like a fancy sg list + backing storage pointer. If multiple callers concurrently interact with the same attachment, havoc might ensue. Importers should add their own locking if concurrent access is possible (e.g. similar to how filling in the same sg table concurrently and then calling dma_map_sg concurrently would lead to chaos). Otoh creating/destroying attachments with attach/detach is protected by the dma_buf->lock.
I've checked dma-buf docs, and that this is not specced in the docs, but was very much the intention. So I think we can solve this with a simple doc update ;-)
drm_gem_dmabuf_v(un)map: Beside that drivers might need to lock for internal consistency the lock only protects the vmapping_counter and pointer and avoids that we race concurrent vmap/vunmap calls to the driver. Now vmap/vunmap is very much like an attachment, just that we don't have an explicit struct for each client since there's only one cpu address space.
So pretty much every driver is forced to reinvent vmap refcounting, which feels like an interface bug. What about moving this code to the dma-buf.c interface shim and protecting it with dma_buf->lock? Atm we only take that lock for attach/detach, and drivers using vmap place the vmap call at the same spot hw drivers would place the attach call, so this shouldn't introduce any nefarious locking issues. So I think this would be the right way to move forward.
And with that we've weaned the prime helpers off their dependency of dev->struct_mutex, which should make them a notch more flexible and so useful (since fighting locking inversions is a royal pain best avoided).
Comments?
Cheers, Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 1/4] drm: add prime helpers
Quoted 52 lines · Daniel Vetter
Quoted 5 lines · Aaron Plattner
Ah, I see what you mean. This would also need either a pointer to the drv directly so the helpers can lock
drv->struct_mutex, or a helper function to convert from a drm_prime_helper_obj* to a gem_buffer_object* in a driver-specific way since the helper doesn't know the layout of the driver's bo structure.Ah, locking, and it involves
dev->struct_mutex. Another pet peeve of mine ;-) Tbh I haven't looked at locking issues when stitching together my quick proposal.The problem with the
dev->struct_mutexlock is that it's everywhere and hence it's way to easy to create deadlocks with it. Especially with prime, where a simple rule like "take this lock first, always" are suddenly really more complicated since gem drivers can assume both the importer and exporter role ... I haven't done a full locking review, but I expect current prime to deadlock (through driver calls) when userspace starts doing funny things.So imo it's best to move
dev->struct_mutexas far away as possible from helper functions and think really hard whether we really need it. I've noticed three functions:
drm_gem_map_dma_buf: We can remove the locking here imo, if drivers needdev->struct_mutexfor internal consistency, they can take it in their callbacks. Thedma_buf_attachmentis very much like a fancy sg list + backing storage pointer. If multiple callers concurrently interact with the same attachment, havoc might ensue. Importers should add their own locking if concurrent access is possible (e.g. similar to how filling in the same sg table concurrently and then callingdma_map_sgconcurrently would lead to chaos). Otoh creating/destroying attachments with attach/detach is protected by thedma_buf->lock.I've checked dma-buf docs, and that this is not specced in the docs, but was very much the intention. So I think we can solve this with a simple doc update ;-)
drm_gem_dmabuf_v(un)map: Beside that drivers might need to lock for internal consistency the lock only protects the
vmapping_counterand pointer and avoids that we race concurrent vmap/vunmap calls to the driver. Now vmap/vunmap is very much like an attachment, just that we don't have an explicit struct for each client since there's only one cpu address space.So pretty much every driver is forced to reinvent vmap refcounting, which feels like an interface bug. What about moving this code to the
dma-buf.cinterface shim and protecting it withdma_buf->lock? Atm we only take that lock for attach/detach, and drivers using vmap place the vmap call at the same spot hw drivers would place the attach call, so this shouldn't introduce any nefarious locking issues. So I think this would be the right way to move forward.
Quoted 6 lines
And with that we've weaned the prime helpers off their dependency of
dev->struct_mutex, which should make them a notch more flexible and so useful (since fighting locking inversions is a royal pain best avoided).Comments?
This all sounds good, but it's getting well outside the realm of what I'm comfortable doing in the short term as a kernel noob. Would you object to going with a version of these changes that leaves the new hooks where they are now and then applying these suggestions later? I don't think the risk of the helpers turning into a required mid-layer is very high given that i915 won't use them and it's one of the most-tested drivers.
--
Aaron
Inki Dae
maintainerRe: [PATCH v2] drm/exynos: use prime helpers
In reply to Aaron Plattner2012/12/8 Aaron Plattner <[email protected]>
Quoted 37 lines · Inki Dae
Quoted 27 lines
Hi,
CCing media guys.
I agree with you but we should consider one issue released to v4l2.
As you may know, V4L2-based driver uses vb2 as buffer manager and the vb2 includes dmabuf feature>(import and export) And v4l2 uses streaming concept>(qbuf and dqbuf)
With dmabuf and iommu, generally qbuf imports a fd into its own buffer and maps it with its own iommu table callingdma_buf_map_attachment(). And dqbuf callsdma_buf_unmap_attachment()to unmap that buffer from its own iommu table.
But now vb2'sunmap_dma_bufcallback is nothing to do. I think that the reason is the below issue,If qbuf maps buffer with iomm table and dqbuf unmaps it from iommu table then it has performance deterioration because qbuf and dqbuf are called repeatedly.
And this means map/unmap are repeated also. So I think media guys moveddma_unmap_sgcall from its ownunmap_dma_bufcallback to detach callback instead.
For this, you can refer tovb2_dc_dmabuf_ops_unmapandvb2_dc_dmabuf_ops_detachfunction.Quoted 8 lines
So I added the below patch to avoid that performance deterioration and
am testing it now.(this patch is derived fromvideobuf2-dma-contig.c)
http://git.kernel.org/?p=**linux/kernel/git/daeinki/drm-**
exynos.git;a=commit;h=**576b1c3de8b90cf1570b8418b60afd**1edaae4e30<http://git.kernel.org/?p=linux/kernel/git/daeinki/drm-exynos.git;a=commit;h=576b1c3de8b90cf1570b8418b60afd1edaae4e30>Thus, I'm not sure that your common set could cover all the cases including other frameworks. Please give me any opinions.
Quoted 6 lines
It seems like this adjustment would make perfect sense to add to the helper layer I suggested. E.g., instead of having an
exynos_attachstructure that caches the sgt, there'd be a structdrm_gem_prime_attachthat would do the same thing, and save the sgt it gets fromdriver->gem_prime_get_sg. Then it would benefit nouveau and radeon, too.
If this change is applied to common helper and also that could be accepted by other maintainers then I think it's better to use this common helper instead of specific one.
Quoted 5 lines
Alternatively, patch #4 could be dropped and Exynos can continue to reimplement all of this core functionality, since the helpers are optional, but I don't see anything about this change that should make it Exynos-specific,
I agree with you. I also think this change isn't specific to Exynos. But you need to check if this is a reasonable change for other drivers also.
Thanks,
Inki Dae
Quoted 3 lines
unless I'm missing something.
--
Quoted 7 lines
Aaron
______________________________**_________________
dri-devel mailing list
[email protected].**org <[email protected]>
http://lists.freedesktop.org/**mailman/listinfo/dri-devel<http://lists.freedesktop.org/mailman/listinfo/dri-devel>
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel