Just a quick resend of the existing vgem patches, all 3 have been acked, but only the first 2 have reviews. The third involves both new ioctl and dma-buf/fences, so perhaps people have been reluctant... But now is a good time! These patches are exercised by intel-gpu-tools (or will be once the new ioctls are ratified). -Chris
The vGEM mmap code has bitrotted slightly and now immediately BUGs. Since vGEM was last updated, there are new core GEM facilities to provide more common functions, so let's use those here.
v2: drm_gem_free_mmap_offset() is performed from drm_gem_object_release() so we can remove the redundant call.
Testcase: igt/vgem_basic/mmap Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=96603 Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Cc: Matthew Auld matthew.auld@intel.com Tested-by: Humberto Israel Perez Rodriguez humberto.i.perez.rodriguez@intel.com Reviewed-by: Matthew Auld matthew.auld@intel.com Acked-by: Zach Reizner zachr@google.com --- drivers/gpu/drm/vgem/vgem_drv.c | 164 +++++++++++++++------------------------- drivers/gpu/drm/vgem/vgem_drv.h | 6 -- 2 files changed, 61 insertions(+), 109 deletions(-)
diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index 35ea5d02a827..c161b6d7e427 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -42,81 +42,38 @@ #define DRIVER_MAJOR 1 #define DRIVER_MINOR 0
-void vgem_gem_put_pages(struct drm_vgem_gem_object *obj) -{ - drm_gem_put_pages(&obj->base, obj->pages, false, false); - obj->pages = NULL; -} - static void vgem_gem_free_object(struct drm_gem_object *obj) { struct drm_vgem_gem_object *vgem_obj = to_vgem_bo(obj);
- drm_gem_free_mmap_offset(obj); - - if (vgem_obj->use_dma_buf && obj->dma_buf) { - dma_buf_put(obj->dma_buf); - obj->dma_buf = NULL; - } - drm_gem_object_release(obj); - - if (vgem_obj->pages) - vgem_gem_put_pages(vgem_obj); - - vgem_obj->pages = NULL; - kfree(vgem_obj); }
-int vgem_gem_get_pages(struct drm_vgem_gem_object *obj) -{ - struct page **pages; - - if (obj->pages || obj->use_dma_buf) - return 0; - - pages = drm_gem_get_pages(&obj->base); - if (IS_ERR(pages)) { - return PTR_ERR(pages); - } - - obj->pages = pages; - - return 0; -} - static int vgem_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) { struct drm_vgem_gem_object *obj = vma->vm_private_data; - loff_t num_pages; - pgoff_t page_offset; - int ret; - /* We don't use vmf->pgoff since that has the fake offset */ - page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> - PAGE_SHIFT; - - num_pages = DIV_ROUND_UP(obj->base.size, PAGE_SIZE); - - if (page_offset > num_pages) - return VM_FAULT_SIGBUS; - - ret = vm_insert_page(vma, (unsigned long)vmf->virtual_address, - obj->pages[page_offset]); - switch (ret) { - case 0: - return VM_FAULT_NOPAGE; - case -ENOMEM: - return VM_FAULT_OOM; - case -EBUSY: - return VM_FAULT_RETRY; - case -EFAULT: - case -EINVAL: - return VM_FAULT_SIGBUS; - default: - WARN_ON(1); - return VM_FAULT_SIGBUS; + unsigned long vaddr = (unsigned long)vmf->virtual_address; + struct page *page; + + page = shmem_read_mapping_page(file_inode(obj->base.filp)->i_mapping, + (vaddr - vma->vm_start) >> PAGE_SHIFT); + if (!IS_ERR(page)) { + vmf->page = page; + return 0; + } else switch (PTR_ERR(page)) { + case -ENOSPC: + case -ENOMEM: + return VM_FAULT_OOM; + case -EBUSY: + return VM_FAULT_RETRY; + case -EFAULT: + case -EINVAL: + return VM_FAULT_SIGBUS; + default: + WARN_ON_ONCE(PTR_ERR(page)); + return VM_FAULT_SIGBUS; } }
@@ -134,57 +91,43 @@ static struct drm_gem_object *vgem_gem_create(struct drm_device *dev, unsigned long size) { struct drm_vgem_gem_object *obj; - struct drm_gem_object *gem_object; - int err; - - size = roundup(size, PAGE_SIZE); + int ret;
obj = kzalloc(sizeof(*obj), GFP_KERNEL); if (!obj) return ERR_PTR(-ENOMEM);
- gem_object = &obj->base; - - err = drm_gem_object_init(dev, gem_object, size); - if (err) - goto out; - - err = vgem_gem_get_pages(obj); - if (err) - goto out; - - err = drm_gem_handle_create(file, gem_object, handle); - if (err) - goto handle_out; + ret = drm_gem_object_init(dev, &obj->base, roundup(size, PAGE_SIZE)); + if (ret) + goto err_free;
- drm_gem_object_unreference_unlocked(gem_object); + ret = drm_gem_handle_create(file, &obj->base, handle); + drm_gem_object_unreference_unlocked(&obj->base); + if (ret) + goto err;
- return gem_object; + return &obj->base;
-handle_out: - drm_gem_object_release(gem_object); -out: +err_free: kfree(obj); - return ERR_PTR(err); +err: + return ERR_PTR(ret); }
static int vgem_gem_dumb_create(struct drm_file *file, struct drm_device *dev, struct drm_mode_create_dumb *args) { struct drm_gem_object *gem_object; - uint64_t size; - uint64_t pitch = args->width * DIV_ROUND_UP(args->bpp, 8); + u64 pitch, size;
+ pitch = args->width * DIV_ROUND_UP(args->bpp, 8); size = args->height * pitch; if (size == 0) return -EINVAL;
gem_object = vgem_gem_create(dev, file, &args->handle, size); - - if (IS_ERR(gem_object)) { - DRM_DEBUG_DRIVER("object creation failed\n"); + if (IS_ERR(gem_object)) return PTR_ERR(gem_object); - }
args->size = gem_object->size; args->pitch = pitch; @@ -194,26 +137,26 @@ static int vgem_gem_dumb_create(struct drm_file *file, struct drm_device *dev, return 0; }
-int vgem_gem_dumb_map(struct drm_file *file, struct drm_device *dev, - uint32_t handle, uint64_t *offset) +static int vgem_gem_dumb_map(struct drm_file *file, struct drm_device *dev, + uint32_t handle, uint64_t *offset) { - int ret = 0; struct drm_gem_object *obj; + int ret;
obj = drm_gem_object_lookup(file, handle); if (!obj) return -ENOENT;
+ if (!obj->filp) { + ret = -EINVAL; + goto unref; + } + ret = drm_gem_create_mmap_offset(obj); if (ret) goto unref;
- BUG_ON(!obj->filp); - - obj->filp->private_data = obj; - *offset = drm_vma_node_offset_addr(&obj->vma_node); - unref: drm_gem_object_unreference_unlocked(obj);
@@ -223,10 +166,26 @@ unref: static struct drm_ioctl_desc vgem_ioctls[] = { };
+static int vgem_mmap(struct file *filp, struct vm_area_struct *vma) +{ + unsigned long flags = vma->vm_flags; + int ret; + + ret = drm_gem_mmap(filp, vma); + if (ret) + return ret; + + /* Keep the WC mmaping set by drm_gem_mmap() but our pages + * are ordinary and not special. + */ + vma->vm_flags = flags | VM_DONTEXPAND | VM_DONTDUMP; + return 0; +} + static const struct file_operations vgem_driver_fops = { .owner = THIS_MODULE, .open = drm_open, - .mmap = drm_gem_mmap, + .mmap = vgem_mmap, .poll = drm_poll, .read = drm_read, .unlocked_ioctl = drm_ioctl, @@ -248,7 +207,7 @@ static struct drm_driver vgem_driver = { .minor = DRIVER_MINOR, };
-struct drm_device *vgem_device; +static struct drm_device *vgem_device;
static int __init vgem_init(void) { @@ -261,7 +220,6 @@ static int __init vgem_init(void) }
ret = drm_dev_register(vgem_device, 0); - if (ret) goto out_unref;
diff --git a/drivers/gpu/drm/vgem/vgem_drv.h b/drivers/gpu/drm/vgem/vgem_drv.h index e9f92f7ee275..988cbaae7588 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.h +++ b/drivers/gpu/drm/vgem/vgem_drv.h @@ -35,12 +35,6 @@ #define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base) struct drm_vgem_gem_object { struct drm_gem_object base; - struct page **pages; - bool use_dma_buf; };
-/* vgem_drv.c */ -extern void vgem_gem_put_pages(struct drm_vgem_gem_object *obj); -extern int vgem_gem_get_pages(struct drm_vgem_gem_object *obj); - #endif
Enable the standard GEM dma-buf interface provided by the DRM core, but only for exporting the VGEM object. This allows passing around the VGEM objects created from the dumb interface and using them as sources elsewhere. Creating a VGEM object for a foriegn handle is not supported.
v2: With additional completeness. v3: Need to clear the CPU cache upon exporting the dma-addresses. v4: Use drm_gem_put_pages() as well. v5: Use drm_prime_pages_to_sg()
Testcase: igt/vgem_basic/dmabuf-* Testcase: igt/prime_vgem Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Acked-by: Zach Reizner zachr@google.com Reviewed-by: Matthew Auld matthew.auld@intel.com --- drivers/gpu/drm/vgem/vgem_drv.c | 89 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index c161b6d7e427..b5fb968d2d5c 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -192,14 +192,101 @@ static const struct file_operations vgem_driver_fops = { .release = drm_release, };
+static int vgem_prime_pin(struct drm_gem_object *obj) +{ + long n_pages = obj->size >> PAGE_SHIFT; + struct page **pages; + + /* Flush the object from the CPU cache so that importers can rely + * on coherent indirect access via the exported dma-address. + */ + pages = drm_gem_get_pages(obj); + if (IS_ERR(pages)) + return PTR_ERR(pages); + + drm_clflush_pages(pages, n_pages); + drm_gem_put_pages(obj, pages, true, false); + + return 0; +} + +static struct sg_table *vgem_prime_get_sg_table(struct drm_gem_object *obj) +{ + struct sg_table *st; + struct page **pages; + + pages = drm_gem_get_pages(obj); + if (IS_ERR(pages)) + return ERR_CAST(pages); + + st = drm_prime_pages_to_sg(pages, obj->size >> PAGE_SHIFT); + drm_gem_put_pages(obj, pages, false, false); + + return st; +} + +static void *vgem_prime_vmap(struct drm_gem_object *obj) +{ + long n_pages = obj->size >> PAGE_SHIFT; + struct page **pages; + void *addr; + + pages = drm_gem_get_pages(obj); + if (IS_ERR(pages)) + return NULL; + + addr = vmap(pages, n_pages, 0, pgprot_writecombine(PAGE_KERNEL_IO)); + drm_gem_put_pages(obj, pages, false, false); + + return addr; +} + +static void vgem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) +{ + vunmap(vaddr); +} + +static int vgem_prime_mmap(struct drm_gem_object *obj, + struct vm_area_struct *vma) +{ + int ret; + + if (obj->size < vma->vm_end - vma->vm_start) + return -EINVAL; + + if (!obj->filp) + return -ENODEV; + + ret = obj->filp->f_op->mmap(obj->filp, vma); + if (ret) + return ret; + + fput(vma->vm_file); + vma->vm_file = get_file(obj->filp); + vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; + vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); + + return 0; +} + static struct drm_driver vgem_driver = { - .driver_features = DRIVER_GEM, + .driver_features = DRIVER_GEM | DRIVER_PRIME, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls, .fops = &vgem_driver_fops, + .dumb_create = vgem_gem_dumb_create, .dumb_map_offset = vgem_gem_dumb_map, + + .prime_handle_to_fd = drm_gem_prime_handle_to_fd, + .gem_prime_pin = vgem_prime_pin, + .gem_prime_export = drm_gem_prime_export, + .gem_prime_get_sg_table = vgem_prime_get_sg_table, + .gem_prime_vmap = vgem_prime_vmap, + .gem_prime_vunmap = vgem_prime_vunmap, + .gem_prime_mmap = vgem_prime_mmap, + .name = DRIVER_NAME, .desc = DRIVER_DESC, .date = DRIVER_DATE,
On Mon, Jul 11, 2016 at 02:08:07PM +0100, Chris Wilson wrote:
Enable the standard GEM dma-buf interface provided by the DRM core, but only for exporting the VGEM object. This allows passing around the VGEM objects created from the dumb interface and using them as sources elsewhere. Creating a VGEM object for a foriegn handle is not supported.
v2: With additional completeness. v3: Need to clear the CPU cache upon exporting the dma-addresses. v4: Use drm_gem_put_pages() as well. v5: Use drm_prime_pages_to_sg()
Testcase: igt/vgem_basic/dmabuf-* Testcase: igt/prime_vgem Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Acked-by: Zach Reizner zachr@google.com Reviewed-by: Matthew Auld matthew.auld@intel.com
Merged the first 2 patches from this series. -Daniel
drivers/gpu/drm/vgem/vgem_drv.c | 89 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index c161b6d7e427..b5fb968d2d5c 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -192,14 +192,101 @@ static const struct file_operations vgem_driver_fops = { .release = drm_release, };
+static int vgem_prime_pin(struct drm_gem_object *obj) +{
- long n_pages = obj->size >> PAGE_SHIFT;
- struct page **pages;
- /* Flush the object from the CPU cache so that importers can rely
* on coherent indirect access via the exported dma-address.
*/
- pages = drm_gem_get_pages(obj);
- if (IS_ERR(pages))
return PTR_ERR(pages);
- drm_clflush_pages(pages, n_pages);
- drm_gem_put_pages(obj, pages, true, false);
- return 0;
+}
+static struct sg_table *vgem_prime_get_sg_table(struct drm_gem_object *obj) +{
- struct sg_table *st;
- struct page **pages;
- pages = drm_gem_get_pages(obj);
- if (IS_ERR(pages))
return ERR_CAST(pages);
- st = drm_prime_pages_to_sg(pages, obj->size >> PAGE_SHIFT);
- drm_gem_put_pages(obj, pages, false, false);
- return st;
+}
+static void *vgem_prime_vmap(struct drm_gem_object *obj) +{
- long n_pages = obj->size >> PAGE_SHIFT;
- struct page **pages;
- void *addr;
- pages = drm_gem_get_pages(obj);
- if (IS_ERR(pages))
return NULL;
- addr = vmap(pages, n_pages, 0, pgprot_writecombine(PAGE_KERNEL_IO));
- drm_gem_put_pages(obj, pages, false, false);
- return addr;
+}
+static void vgem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) +{
- vunmap(vaddr);
+}
+static int vgem_prime_mmap(struct drm_gem_object *obj,
struct vm_area_struct *vma)
+{
- int ret;
- if (obj->size < vma->vm_end - vma->vm_start)
return -EINVAL;
- if (!obj->filp)
return -ENODEV;
- ret = obj->filp->f_op->mmap(obj->filp, vma);
- if (ret)
return ret;
- fput(vma->vm_file);
- vma->vm_file = get_file(obj->filp);
- vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
- vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
- return 0;
+}
static struct drm_driver vgem_driver = {
- .driver_features = DRIVER_GEM,
- .driver_features = DRIVER_GEM | DRIVER_PRIME, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls, .fops = &vgem_driver_fops,
- .dumb_create = vgem_gem_dumb_create, .dumb_map_offset = vgem_gem_dumb_map,
- .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
- .gem_prime_pin = vgem_prime_pin,
- .gem_prime_export = drm_gem_prime_export,
- .gem_prime_get_sg_table = vgem_prime_get_sg_table,
- .gem_prime_vmap = vgem_prime_vmap,
- .gem_prime_vunmap = vgem_prime_vunmap,
- .gem_prime_mmap = vgem_prime_mmap,
- .name = DRIVER_NAME, .desc = DRIVER_DESC, .date = DRIVER_DATE,
-- 2.8.1
vGEM buffers are useful for passing data between software clients and hardware renders. By allowing the user to create and attach fences to the exported vGEM buffers (on the dma-buf), the user can implement a deferred renderer and queue hardware operations like flipping and then signal the buffer readiness (i.e. this allows the user to schedule operations out-of-order, but have them complete in-order).
This also makes it much easier to write tightly controlled testcases for dma-buf fencing and signaling between hardware drivers.
Testcase: igt/vgem_basic/dmabuf-fence Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Cc: Gustavo Padovan gustavo.padovan@collabora.co.uk Acked-by: Zach Reizner zachr@google.com --- drivers/gpu/drm/vgem/Makefile | 2 +- drivers/gpu/drm/vgem/vgem_drv.c | 34 ++++++ drivers/gpu/drm/vgem/vgem_drv.h | 18 ++++ drivers/gpu/drm/vgem/vgem_fence.c | 220 ++++++++++++++++++++++++++++++++++++++ include/uapi/drm/vgem_drm.h | 62 +++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/vgem/vgem_fence.c create mode 100644 include/uapi/drm/vgem_drm.h
diff --git a/drivers/gpu/drm/vgem/Makefile b/drivers/gpu/drm/vgem/Makefile index 3f4c7b842028..bfcdea1330e6 100644 --- a/drivers/gpu/drm/vgem/Makefile +++ b/drivers/gpu/drm/vgem/Makefile @@ -1,4 +1,4 @@ ccflags-y := -Iinclude/drm -vgem-y := vgem_drv.o +vgem-y := vgem_drv.o vgem_fence.o
obj-$(CONFIG_DRM_VGEM) += vgem.o diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index b5fb968d2d5c..2659e5cda857 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -83,6 +83,34 @@ static const struct vm_operations_struct vgem_gem_vm_ops = { .close = drm_gem_vm_close, };
+static int vgem_open(struct drm_device *dev, struct drm_file *file) +{ + struct vgem_file *vfile; + int ret; + + vfile = kzalloc(sizeof(*vfile), GFP_KERNEL); + if (!vfile) + return -ENOMEM; + + file->driver_priv = vfile; + + ret = vgem_fence_open(vfile); + if (ret) { + kfree(vfile); + return ret; + } + + return 0; +} + +static void vgem_preclose(struct drm_device *dev, struct drm_file *file) +{ + struct vgem_file *vfile = file->driver_priv; + + vgem_fence_close(vfile); + kfree(vfile); +} + /* ioctls */
static struct drm_gem_object *vgem_gem_create(struct drm_device *dev, @@ -164,6 +192,8 @@ unref: }
static struct drm_ioctl_desc vgem_ioctls[] = { + DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), + DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), };
static int vgem_mmap(struct file *filp, struct vm_area_struct *vma) @@ -271,9 +301,12 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
static struct drm_driver vgem_driver = { .driver_features = DRIVER_GEM | DRIVER_PRIME, + .open = vgem_open, + .preclose = vgem_preclose, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls, + .num_ioctls = ARRAY_SIZE(vgem_ioctls), .fops = &vgem_driver_fops,
.dumb_create = vgem_gem_dumb_create, @@ -328,5 +361,6 @@ module_init(vgem_init); module_exit(vgem_exit);
MODULE_AUTHOR("Red Hat, Inc."); +MODULE_AUTHOR("Intel Corporation"); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL and additional rights"); diff --git a/drivers/gpu/drm/vgem/vgem_drv.h b/drivers/gpu/drm/vgem/vgem_drv.h index 988cbaae7588..88ce21010e28 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.h +++ b/drivers/gpu/drm/vgem/vgem_drv.h @@ -32,9 +32,27 @@ #include <drm/drmP.h> #include <drm/drm_gem.h>
+#include <uapi/drm/vgem_drm.h> + +struct vgem_file { + struct idr fence_idr; + struct mutex fence_mutex; + u64 fence_context; + atomic_t fence_seqno; +}; + #define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base) struct drm_vgem_gem_object { struct drm_gem_object base; };
+int vgem_fence_open(struct vgem_file *file); +int vgem_fence_attach_ioctl(struct drm_device *dev, + void *data, + struct drm_file *file); +int vgem_fence_signal_ioctl(struct drm_device *dev, + void *data, + struct drm_file *file); +void vgem_fence_close(struct vgem_file *file); + #endif diff --git a/drivers/gpu/drm/vgem/vgem_fence.c b/drivers/gpu/drm/vgem/vgem_fence.c new file mode 100644 index 000000000000..649e9e1cee35 --- /dev/null +++ b/drivers/gpu/drm/vgem/vgem_fence.c @@ -0,0 +1,220 @@ +/* + * Copyright 2016 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software") + * to deal in the software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * them Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include <linux/dma-buf.h> +#include <linux/reservation.h> + +#include "vgem_drv.h" + +struct vgem_fence { + struct fence base; + struct spinlock lock; +}; + +static const char *vgem_fence_get_driver_name(struct fence *fence) +{ + return "vgem"; +} + +static const char *vgem_fence_get_timeline_name(struct fence *fence) +{ + return "file"; +} + +static bool vgem_fence_signaled(struct fence *fence) +{ + return false; +} + +static bool vgem_fence_enable_signaling(struct fence *fence) +{ + return true; +} + +static void vgem_fence_value_str(struct fence *fence, char *str, int size) +{ + snprintf(str, size, "%u", fence->seqno); +} + +static void vgem_fence_timeline_value_str(struct fence *fence, char *str, + int size) +{ + snprintf(str, size, "%u", 0); +} + +const struct fence_ops vgem_fence_ops = { + .get_driver_name = vgem_fence_get_driver_name, + .get_timeline_name = vgem_fence_get_timeline_name, + .enable_signaling = vgem_fence_enable_signaling, + .signaled = vgem_fence_signaled, + .wait = fence_default_wait, + .fence_value_str = vgem_fence_value_str, + .timeline_value_str = vgem_fence_timeline_value_str, +}; + +static u32 vgem_fence_next_seqno(struct vgem_file *vfile) +{ + u32 seqno; + + seqno = atomic_inc_return(&vfile->fence_seqno); + if (seqno == 0) + seqno = atomic_inc_return(&vfile->fence_seqno); + + return seqno; +} + +static struct fence *vgem_fence_create(struct vgem_file *vfile) +{ + struct vgem_fence *fence; + + fence = kzalloc(sizeof(*fence), GFP_KERNEL); + if (!fence) + return NULL; + + spin_lock_init(&fence->lock); + fence_init(&fence->base, + &vgem_fence_ops, + &fence->lock, + vfile->fence_context, + vgem_fence_next_seqno(vfile)); + + return &fence->base; +} + +static int attach_dmabuf(struct drm_device *dev, + struct drm_gem_object *obj) +{ + struct dma_buf *dmabuf; + + if (obj->dma_buf) + return 0; + + dmabuf = dev->driver->gem_prime_export(dev, obj, 0); + if (IS_ERR(dmabuf)) + return PTR_ERR(dmabuf); + + obj->dma_buf = dmabuf; + drm_gem_object_reference(obj); + return 0; +} + +int vgem_fence_attach_ioctl(struct drm_device *dev, + void *data, + struct drm_file *file) +{ + struct drm_vgem_fence_attach *arg = data; + struct vgem_file *vfile = file->driver_priv; + struct reservation_object *resv; + struct drm_gem_object *obj; + struct fence *fence; + int ret; + + if (arg->flags & ~VGEM_FENCE_WRITE) + return -EINVAL; + + if (arg->pad) + return -EINVAL; + + obj = drm_gem_object_lookup(file, arg->handle); + if (!obj) + return -ENOENT; + + ret = attach_dmabuf(dev, obj); + if (ret) + goto out; + + fence = vgem_fence_create(vfile); + if (!fence) { + ret = -ENOMEM; + goto out; + } + + ret = 0; + resv = obj->dma_buf->resv; + mutex_lock(&resv->lock.base); + if (arg->flags & VGEM_FENCE_WRITE) + reservation_object_add_excl_fence(resv, fence); + else if ((ret = reservation_object_reserve_shared(resv)) == 0) + reservation_object_add_shared_fence(resv, fence); + mutex_unlock(&resv->lock.base); + + if (ret == 0) { + mutex_lock(&vfile->fence_mutex); + ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL); + mutex_unlock(&vfile->fence_mutex); + if (ret > 0) { + arg->out_fence = ret; + ret = 0; + } + } + if (ret) + fence_put(fence); +out: + drm_gem_object_unreference_unlocked(obj); + return ret; +} + +int vgem_fence_signal_ioctl(struct drm_device *dev, + void *data, + struct drm_file *file) +{ + struct vgem_file *vfile = file->driver_priv; + struct drm_vgem_fence_signal *arg = data; + struct fence *fence; + + if (arg->flags) + return -EINVAL; + + mutex_lock(&vfile->fence_mutex); + fence = idr_replace(&vfile->fence_idr, NULL, arg->fence); + mutex_unlock(&vfile->fence_mutex); + if (!fence) + return -ENOENT; + if (IS_ERR(fence)) + return PTR_ERR(fence); + + fence_signal(fence); + fence_put(fence); + return 0; +} + +int vgem_fence_open(struct vgem_file *vfile) +{ + mutex_init(&vfile->fence_mutex); + idr_init(&vfile->fence_idr); + vfile->fence_context = fence_context_alloc(1); + + return 0; +} + +static int __vgem_fence_idr_fini(int id, void *p, void *data) +{ + fence_signal(p); + fence_put(p); + return 0; +} + +void vgem_fence_close(struct vgem_file *vfile) +{ + idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile); + idr_destroy(&vfile->fence_idr); +} diff --git a/include/uapi/drm/vgem_drm.h b/include/uapi/drm/vgem_drm.h new file mode 100644 index 000000000000..352d2fae8de9 --- /dev/null +++ b/include/uapi/drm/vgem_drm.h @@ -0,0 +1,62 @@ +/* + * Copyright 2016 Intel Corporation + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef _UAPI_VGEM_DRM_H_ +#define _UAPI_VGEM_DRM_H_ + +#include "drm.h" + +#if defined(__cplusplus) +extern "C" { +#endif + +/* Please note that modifications to all structs defined here are + * subject to backwards-compatibility constraints. + */ +#define DRM_VGEM_FENCE_ATTACH 0x1 +#define DRM_VGEM_FENCE_SIGNAL 0x2 + +#define DRM_IOCTL_VGEM_FENCE_ATTACH DRM_IOWR( DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach) +#define DRM_IOCTL_VGEM_FENCE_SIGNAL DRM_IOW( DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal) + +struct drm_vgem_fence_attach { + __u32 handle; + __u32 flags; +#define VGEM_FENCE_WRITE 0x1 + __u32 out_fence; + __u32 pad; +}; + +struct drm_vgem_fence_signal { + __u32 fence; + __u32 flags; +}; + +#if defined(__cplusplus) +} +#endif + +#endif /* _UAPI_VGEM_DRM_H_ */
2016-07-11 Chris Wilson chris@chris-wilson.co.uk:
vGEM buffers are useful for passing data between software clients and hardware renders. By allowing the user to create and attach fences to the exported vGEM buffers (on the dma-buf), the user can implement a deferred renderer and queue hardware operations like flipping and then signal the buffer readiness (i.e. this allows the user to schedule operations out-of-order, but have them complete in-order).
This also makes it much easier to write tightly controlled testcases for dma-buf fencing and signaling between hardware drivers.
Testcase: igt/vgem_basic/dmabuf-fence Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Cc: Gustavo Padovan gustavo.padovan@collabora.co.uk Acked-by: Zach Reizner zachr@google.com
drivers/gpu/drm/vgem/Makefile | 2 +- drivers/gpu/drm/vgem/vgem_drv.c | 34 ++++++ drivers/gpu/drm/vgem/vgem_drv.h | 18 ++++ drivers/gpu/drm/vgem/vgem_fence.c | 220 ++++++++++++++++++++++++++++++++++++++ include/uapi/drm/vgem_drm.h | 62 +++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/vgem/vgem_fence.c create mode 100644 include/uapi/drm/vgem_drm.h
diff --git a/drivers/gpu/drm/vgem/Makefile b/drivers/gpu/drm/vgem/Makefile index 3f4c7b842028..bfcdea1330e6 100644 --- a/drivers/gpu/drm/vgem/Makefile +++ b/drivers/gpu/drm/vgem/Makefile @@ -1,4 +1,4 @@ ccflags-y := -Iinclude/drm -vgem-y := vgem_drv.o +vgem-y := vgem_drv.o vgem_fence.o
obj-$(CONFIG_DRM_VGEM) += vgem.o diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index b5fb968d2d5c..2659e5cda857 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -83,6 +83,34 @@ static const struct vm_operations_struct vgem_gem_vm_ops = { .close = drm_gem_vm_close, };
+static int vgem_open(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile;
- int ret;
- vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
- if (!vfile)
return -ENOMEM;
- file->driver_priv = vfile;
- ret = vgem_fence_open(vfile);
- if (ret) {
kfree(vfile);
return ret;
- }
- return 0;
+}
+static void vgem_preclose(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile = file->driver_priv;
- vgem_fence_close(vfile);
- kfree(vfile);
+}
/* ioctls */
static struct drm_gem_object *vgem_gem_create(struct drm_device *dev, @@ -164,6 +192,8 @@ unref: }
static struct drm_ioctl_desc vgem_ioctls[] = {
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
};
static int vgem_mmap(struct file *filp, struct vm_area_struct *vma) @@ -271,9 +301,12 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
static struct drm_driver vgem_driver = { .driver_features = DRIVER_GEM | DRIVER_PRIME,
.open = vgem_open,
.preclose = vgem_preclose, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls,
.num_ioctls = ARRAY_SIZE(vgem_ioctls), .fops = &vgem_driver_fops,
.dumb_create = vgem_gem_dumb_create,
@@ -328,5 +361,6 @@ module_init(vgem_init); module_exit(vgem_exit);
MODULE_AUTHOR("Red Hat, Inc."); +MODULE_AUTHOR("Intel Corporation"); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL and additional rights"); diff --git a/drivers/gpu/drm/vgem/vgem_drv.h b/drivers/gpu/drm/vgem/vgem_drv.h index 988cbaae7588..88ce21010e28 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.h +++ b/drivers/gpu/drm/vgem/vgem_drv.h @@ -32,9 +32,27 @@ #include <drm/drmP.h> #include <drm/drm_gem.h>
+#include <uapi/drm/vgem_drm.h>
+struct vgem_file {
- struct idr fence_idr;
- struct mutex fence_mutex;
- u64 fence_context;
- atomic_t fence_seqno;
+};
#define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base) struct drm_vgem_gem_object { struct drm_gem_object base; };
+int vgem_fence_open(struct vgem_file *file); +int vgem_fence_attach_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+int vgem_fence_signal_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+void vgem_fence_close(struct vgem_file *file);
#endif diff --git a/drivers/gpu/drm/vgem/vgem_fence.c b/drivers/gpu/drm/vgem/vgem_fence.c new file mode 100644 index 000000000000..649e9e1cee35 --- /dev/null +++ b/drivers/gpu/drm/vgem/vgem_fence.c @@ -0,0 +1,220 @@ +/*
- Copyright 2016 Intel Corporation
- Permission is hereby granted, free of charge, to any person obtaining a
- copy of this software and associated documentation files (the "Software")
- to deal in the software without restriction, including without limitation
- on the rights to use, copy, modify, merge, publish, distribute, sub
- license, and/or sell copies of the Software, and to permit persons to whom
- them Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice (including the next
- paragraph) shall be included in all copies or substantial portions of the
- Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
- IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+#include <linux/dma-buf.h> +#include <linux/reservation.h>
+#include "vgem_drv.h"
+struct vgem_fence {
- struct fence base;
- struct spinlock lock;
+};
+static const char *vgem_fence_get_driver_name(struct fence *fence) +{
- return "vgem";
+}
+static const char *vgem_fence_get_timeline_name(struct fence *fence) +{
- return "file";
+}
+static bool vgem_fence_signaled(struct fence *fence) +{
- return false;
+}
+static bool vgem_fence_enable_signaling(struct fence *fence) +{
- return true;
+}
+static void vgem_fence_value_str(struct fence *fence, char *str, int size) +{
- snprintf(str, size, "%u", fence->seqno);
+}
+static void vgem_fence_timeline_value_str(struct fence *fence, char *str,
int size)
+{
- snprintf(str, size, "%u", 0);
+}
I think this would be mainly used for debug purposes but it would be nice to actually return the current timeline value here, i.e., the seqno of the last signalled fence.
This would also allow to return if the fence was signaled or not in vgem_fence_signaled() instead of just return false. But if the fence_put() after fence_signal() releases the last reference to the fence vgem_fence_signaled() would be never be called after it signalled.
+const struct fence_ops vgem_fence_ops = {
- .get_driver_name = vgem_fence_get_driver_name,
- .get_timeline_name = vgem_fence_get_timeline_name,
- .enable_signaling = vgem_fence_enable_signaling,
- .signaled = vgem_fence_signaled,
- .wait = fence_default_wait,
- .fence_value_str = vgem_fence_value_str,
- .timeline_value_str = vgem_fence_timeline_value_str,
+};
+static u32 vgem_fence_next_seqno(struct vgem_file *vfile) +{
- u32 seqno;
- seqno = atomic_inc_return(&vfile->fence_seqno);
- if (seqno == 0)
seqno = atomic_inc_return(&vfile->fence_seqno);
- return seqno;
+}
+static struct fence *vgem_fence_create(struct vgem_file *vfile) +{
- struct vgem_fence *fence;
- fence = kzalloc(sizeof(*fence), GFP_KERNEL);
- if (!fence)
return NULL;
- spin_lock_init(&fence->lock);
- fence_init(&fence->base,
&vgem_fence_ops,
&fence->lock,
vfile->fence_context,
vgem_fence_next_seqno(vfile));
Shall we make seqno u64 too?
Gustavo
On Mon, Jul 11, 2016 at 12:10:40PM -0300, Gustavo Padovan wrote:
2016-07-11 Chris Wilson chris@chris-wilson.co.uk:
vGEM buffers are useful for passing data between software clients and hardware renders. By allowing the user to create and attach fences to the exported vGEM buffers (on the dma-buf), the user can implement a deferred renderer and queue hardware operations like flipping and then signal the buffer readiness (i.e. this allows the user to schedule operations out-of-order, but have them complete in-order).
This also makes it much easier to write tightly controlled testcases for dma-buf fencing and signaling between hardware drivers.
Testcase: igt/vgem_basic/dmabuf-fence Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Cc: Gustavo Padovan gustavo.padovan@collabora.co.uk Acked-by: Zach Reizner zachr@google.com
drivers/gpu/drm/vgem/Makefile | 2 +- drivers/gpu/drm/vgem/vgem_drv.c | 34 ++++++ drivers/gpu/drm/vgem/vgem_drv.h | 18 ++++ drivers/gpu/drm/vgem/vgem_fence.c | 220 ++++++++++++++++++++++++++++++++++++++ include/uapi/drm/vgem_drm.h | 62 +++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/vgem/vgem_fence.c create mode 100644 include/uapi/drm/vgem_drm.h
diff --git a/drivers/gpu/drm/vgem/Makefile b/drivers/gpu/drm/vgem/Makefile index 3f4c7b842028..bfcdea1330e6 100644 --- a/drivers/gpu/drm/vgem/Makefile +++ b/drivers/gpu/drm/vgem/Makefile @@ -1,4 +1,4 @@ ccflags-y := -Iinclude/drm -vgem-y := vgem_drv.o +vgem-y := vgem_drv.o vgem_fence.o
obj-$(CONFIG_DRM_VGEM) += vgem.o diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index b5fb968d2d5c..2659e5cda857 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -83,6 +83,34 @@ static const struct vm_operations_struct vgem_gem_vm_ops = { .close = drm_gem_vm_close, };
+static int vgem_open(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile;
- int ret;
- vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
- if (!vfile)
return -ENOMEM;
- file->driver_priv = vfile;
- ret = vgem_fence_open(vfile);
- if (ret) {
kfree(vfile);
return ret;
- }
- return 0;
+}
+static void vgem_preclose(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile = file->driver_priv;
- vgem_fence_close(vfile);
- kfree(vfile);
+}
/* ioctls */
static struct drm_gem_object *vgem_gem_create(struct drm_device *dev, @@ -164,6 +192,8 @@ unref: }
static struct drm_ioctl_desc vgem_ioctls[] = {
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
};
static int vgem_mmap(struct file *filp, struct vm_area_struct *vma) @@ -271,9 +301,12 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
static struct drm_driver vgem_driver = { .driver_features = DRIVER_GEM | DRIVER_PRIME,
.open = vgem_open,
.preclose = vgem_preclose, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls,
.num_ioctls = ARRAY_SIZE(vgem_ioctls), .fops = &vgem_driver_fops,
.dumb_create = vgem_gem_dumb_create,
@@ -328,5 +361,6 @@ module_init(vgem_init); module_exit(vgem_exit);
MODULE_AUTHOR("Red Hat, Inc."); +MODULE_AUTHOR("Intel Corporation"); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL and additional rights"); diff --git a/drivers/gpu/drm/vgem/vgem_drv.h b/drivers/gpu/drm/vgem/vgem_drv.h index 988cbaae7588..88ce21010e28 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.h +++ b/drivers/gpu/drm/vgem/vgem_drv.h @@ -32,9 +32,27 @@ #include <drm/drmP.h> #include <drm/drm_gem.h>
+#include <uapi/drm/vgem_drm.h>
+struct vgem_file {
- struct idr fence_idr;
- struct mutex fence_mutex;
- u64 fence_context;
- atomic_t fence_seqno;
+};
#define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base) struct drm_vgem_gem_object { struct drm_gem_object base; };
+int vgem_fence_open(struct vgem_file *file); +int vgem_fence_attach_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+int vgem_fence_signal_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+void vgem_fence_close(struct vgem_file *file);
#endif diff --git a/drivers/gpu/drm/vgem/vgem_fence.c b/drivers/gpu/drm/vgem/vgem_fence.c new file mode 100644 index 000000000000..649e9e1cee35 --- /dev/null +++ b/drivers/gpu/drm/vgem/vgem_fence.c @@ -0,0 +1,220 @@ +/*
- Copyright 2016 Intel Corporation
- Permission is hereby granted, free of charge, to any person obtaining a
- copy of this software and associated documentation files (the "Software")
- to deal in the software without restriction, including without limitation
- on the rights to use, copy, modify, merge, publish, distribute, sub
- license, and/or sell copies of the Software, and to permit persons to whom
- them Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice (including the next
- paragraph) shall be included in all copies or substantial portions of the
- Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
- IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+#include <linux/dma-buf.h> +#include <linux/reservation.h>
+#include "vgem_drv.h"
+struct vgem_fence {
- struct fence base;
- struct spinlock lock;
+};
+static const char *vgem_fence_get_driver_name(struct fence *fence) +{
- return "vgem";
+}
+static const char *vgem_fence_get_timeline_name(struct fence *fence) +{
- return "file";
+}
+static bool vgem_fence_signaled(struct fence *fence) +{
- return false;
+}
+static bool vgem_fence_enable_signaling(struct fence *fence) +{
- return true;
+}
+static void vgem_fence_value_str(struct fence *fence, char *str, int size) +{
- snprintf(str, size, "%u", fence->seqno);
+}
+static void vgem_fence_timeline_value_str(struct fence *fence, char *str,
int size)
+{
- snprintf(str, size, "%u", 0);
+}
I think this would be mainly used for debug purposes but it would be nice to actually return the current timeline value here, i.e., the seqno of the last signalled fence.
The fences can and will be signaled in any order, there is no timeline here under the control of userspace, we only give them fences.
This would also allow to return if the fence was signaled or not in vgem_fence_signaled() instead of just return false.
That doesn't fit the out-of-order unbound nature of the interface. The interface is just a collection of fences that userspace associates with the buffer that it may signal at any time. (Having no strict timeline is an advantage!) -Chris
On Mon, Jul 11, 2016 at 04:24:45PM +0100, Chris Wilson wrote:
On Mon, Jul 11, 2016 at 12:10:40PM -0300, Gustavo Padovan wrote:
2016-07-11 Chris Wilson chris@chris-wilson.co.uk:
vGEM buffers are useful for passing data between software clients and hardware renders. By allowing the user to create and attach fences to the exported vGEM buffers (on the dma-buf), the user can implement a deferred renderer and queue hardware operations like flipping and then signal the buffer readiness (i.e. this allows the user to schedule operations out-of-order, but have them complete in-order).
This also makes it much easier to write tightly controlled testcases for dma-buf fencing and signaling between hardware drivers.
Testcase: igt/vgem_basic/dmabuf-fence Signed-off-by: Chris Wilson chris@chris-wilson.co.uk Cc: Sean Paul seanpaul@chromium.org Cc: Zach Reizner zachr@google.com Cc: Gustavo Padovan gustavo.padovan@collabora.co.uk Acked-by: Zach Reizner zachr@google.com
drivers/gpu/drm/vgem/Makefile | 2 +- drivers/gpu/drm/vgem/vgem_drv.c | 34 ++++++ drivers/gpu/drm/vgem/vgem_drv.h | 18 ++++ drivers/gpu/drm/vgem/vgem_fence.c | 220 ++++++++++++++++++++++++++++++++++++++ include/uapi/drm/vgem_drm.h | 62 +++++++++++ 5 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/vgem/vgem_fence.c create mode 100644 include/uapi/drm/vgem_drm.h
diff --git a/drivers/gpu/drm/vgem/Makefile b/drivers/gpu/drm/vgem/Makefile index 3f4c7b842028..bfcdea1330e6 100644 --- a/drivers/gpu/drm/vgem/Makefile +++ b/drivers/gpu/drm/vgem/Makefile @@ -1,4 +1,4 @@ ccflags-y := -Iinclude/drm -vgem-y := vgem_drv.o +vgem-y := vgem_drv.o vgem_fence.o
obj-$(CONFIG_DRM_VGEM) += vgem.o diff --git a/drivers/gpu/drm/vgem/vgem_drv.c b/drivers/gpu/drm/vgem/vgem_drv.c index b5fb968d2d5c..2659e5cda857 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.c +++ b/drivers/gpu/drm/vgem/vgem_drv.c @@ -83,6 +83,34 @@ static const struct vm_operations_struct vgem_gem_vm_ops = { .close = drm_gem_vm_close, };
+static int vgem_open(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile;
- int ret;
- vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
- if (!vfile)
return -ENOMEM;
- file->driver_priv = vfile;
- ret = vgem_fence_open(vfile);
- if (ret) {
kfree(vfile);
return ret;
- }
- return 0;
+}
+static void vgem_preclose(struct drm_device *dev, struct drm_file *file) +{
- struct vgem_file *vfile = file->driver_priv;
- vgem_fence_close(vfile);
- kfree(vfile);
+}
/* ioctls */
static struct drm_gem_object *vgem_gem_create(struct drm_device *dev, @@ -164,6 +192,8 @@ unref: }
static struct drm_ioctl_desc vgem_ioctls[] = {
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
- DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
};
static int vgem_mmap(struct file *filp, struct vm_area_struct *vma) @@ -271,9 +301,12 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
static struct drm_driver vgem_driver = { .driver_features = DRIVER_GEM | DRIVER_PRIME,
.open = vgem_open,
.preclose = vgem_preclose, .gem_free_object_unlocked = vgem_gem_free_object, .gem_vm_ops = &vgem_gem_vm_ops, .ioctls = vgem_ioctls,
.num_ioctls = ARRAY_SIZE(vgem_ioctls), .fops = &vgem_driver_fops,
.dumb_create = vgem_gem_dumb_create,
@@ -328,5 +361,6 @@ module_init(vgem_init); module_exit(vgem_exit);
MODULE_AUTHOR("Red Hat, Inc."); +MODULE_AUTHOR("Intel Corporation"); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL and additional rights"); diff --git a/drivers/gpu/drm/vgem/vgem_drv.h b/drivers/gpu/drm/vgem/vgem_drv.h index 988cbaae7588..88ce21010e28 100644 --- a/drivers/gpu/drm/vgem/vgem_drv.h +++ b/drivers/gpu/drm/vgem/vgem_drv.h @@ -32,9 +32,27 @@ #include <drm/drmP.h> #include <drm/drm_gem.h>
+#include <uapi/drm/vgem_drm.h>
+struct vgem_file {
- struct idr fence_idr;
- struct mutex fence_mutex;
- u64 fence_context;
- atomic_t fence_seqno;
+};
#define to_vgem_bo(x) container_of(x, struct drm_vgem_gem_object, base) struct drm_vgem_gem_object { struct drm_gem_object base; };
+int vgem_fence_open(struct vgem_file *file); +int vgem_fence_attach_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+int vgem_fence_signal_ioctl(struct drm_device *dev,
void *data,
struct drm_file *file);
+void vgem_fence_close(struct vgem_file *file);
#endif diff --git a/drivers/gpu/drm/vgem/vgem_fence.c b/drivers/gpu/drm/vgem/vgem_fence.c new file mode 100644 index 000000000000..649e9e1cee35 --- /dev/null +++ b/drivers/gpu/drm/vgem/vgem_fence.c @@ -0,0 +1,220 @@ +/*
- Copyright 2016 Intel Corporation
- Permission is hereby granted, free of charge, to any person obtaining a
- copy of this software and associated documentation files (the "Software")
- to deal in the software without restriction, including without limitation
- on the rights to use, copy, modify, merge, publish, distribute, sub
- license, and/or sell copies of the Software, and to permit persons to whom
- them Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice (including the next
- paragraph) shall be included in all copies or substantial portions of the
- Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
- IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+#include <linux/dma-buf.h> +#include <linux/reservation.h>
+#include "vgem_drv.h"
+struct vgem_fence {
- struct fence base;
- struct spinlock lock;
+};
+static const char *vgem_fence_get_driver_name(struct fence *fence) +{
- return "vgem";
+}
+static const char *vgem_fence_get_timeline_name(struct fence *fence) +{
- return "file";
+}
+static bool vgem_fence_signaled(struct fence *fence) +{
- return false;
+}
+static bool vgem_fence_enable_signaling(struct fence *fence) +{
- return true;
+}
+static void vgem_fence_value_str(struct fence *fence, char *str, int size) +{
- snprintf(str, size, "%u", fence->seqno);
+}
+static void vgem_fence_timeline_value_str(struct fence *fence, char *str,
int size)
+{
- snprintf(str, size, "%u", 0);
+}
I think this would be mainly used for debug purposes but it would be nice to actually return the current timeline value here, i.e., the seqno of the last signalled fence.
The fences can and will be signaled in any order, there is no timeline here under the control of userspace, we only give them fences.
This would also allow to return if the fence was signaled or not in vgem_fence_signaled() instead of just return false.
That doesn't fit the out-of-order unbound nature of the interface. The interface is just a collection of fences that userspace associates with the buffer that it may signal at any time. (Having no strict timeline is an advantage!)
Fences on the same timeline are supposed to be signalled in-order. If you want full out-of-order fences then you need to grab a new timeline number for each one. Drivers can and do merge fences on the same timeline and just carry the one with the largest seqno around. -Daniel
On Tue, Jul 12, 2016 at 12:44:17PM +0200, Daniel Vetter wrote:
On Mon, Jul 11, 2016 at 04:24:45PM +0100, Chris Wilson wrote:
That doesn't fit the out-of-order unbound nature of the interface. The interface is just a collection of fences that userspace associates with the buffer that it may signal at any time. (Having no strict timeline is an advantage!)
Fences on the same timeline are supposed to be signalled in-order. If you want full out-of-order fences then you need to grab a new timeline number for each one. Drivers can and do merge fences on the same timeline and just carry the one with the largest seqno around.
Ugh. Timelines simply don't mean anything everywhere - a very leaky abstration.
Nevertheless, a fence_context per vgem_fence would do the trick. -Chris
On Tue, Jul 12, 2016 at 12:04:03PM +0100, Chris Wilson wrote:
On Tue, Jul 12, 2016 at 12:44:17PM +0200, Daniel Vetter wrote:
On Mon, Jul 11, 2016 at 04:24:45PM +0100, Chris Wilson wrote:
That doesn't fit the out-of-order unbound nature of the interface. The interface is just a collection of fences that userspace associates with the buffer that it may signal at any time. (Having no strict timeline is an advantage!)
Fences on the same timeline are supposed to be signalled in-order. If you want full out-of-order fences then you need to grab a new timeline number for each one. Drivers can and do merge fences on the same timeline and just carry the one with the largest seqno around.
Ugh. Timelines simply don't mean anything everywhere - a very leaky abstration.
Nevertheless, a fence_context per vgem_fence would do the trick.
Yeah it's a bit meh, but allocating plenty of them is how we currently cope with it. I suggested that we have a special FENCE_TIMELINE_UNORDERED flag (we need it for fence_array too), but that wasn't popular. I still expect it to happen eventually ;-) -Daniel
dri-devel@lists.freedesktop.org