virgl: fence fd support
Subsystems (4)
v5 cover virgl: fence fd support
This series implements fence support for drm/virtio and has been tested using qemu, kmscube and the below branches.
Rob Herring solved a reference counting issue and suggested a context check for the execbuf ioctl, his changes have been included in the below commits to keep the tree working at all commits.
The linux series can be found here:
https://gitlab.collabora.com/robertfoss/linux/commits/virtio_fences_v5
As for mesa, the branch can be found here:
https://gitlab.collabora.com/robertfoss/mesa/commits/virtio_fences_v3
Changes since v4:
- drm/virtio: add uapi for in and out explicit fences
- Emil/Gerd: Improved commit message and fence_fd comment
Changes since v3:
- Rebased on drm-misc-next
- drm/virtio: add virtio_gpu_alloc_fence()
- Gerd: Clarified and extended commit message
- Emil: Fixed whitespace issue
- Emil: Changed label name from fail_fence to fail_backoff
- Emil: Remove special case for !fence->drv in virtio_gpu_fence_cleanup()
- drm/virtio: add uapi for in and out explicit fences
- Emil: Added r-b
- Emil: Move fence_fd assignment to after sanity checks
- drm/virtio: add in-fences support for explicit synchronization
- Move all in_fence handling to the same VIRTGPU_EXECBUF_FENCE_FD_IN block
- Emil: Make sure to always call dma_fence_put()
- Emil: Added r-b
- drm/virtio: add out-fences support for explicit synchronization
- Emil: Combine with in-fences patch
- drm/virtio: bump driver version after explicit synchronization addition
- Emil: Added r-b
Changes since v2:
- drm/virtio: add virtio_gpu_alloc_fence()
- Forward port and fix compilation issues
- drm/virtio: add uapi for in and out explicit fences
- Check exbuf->flags for unsupported flags
- drm/virtio: add in-fences support for explicit synchronization
Gustavo Padovan (1):
drm/virtio: bump driver version after explicit synchronization
addition
Robert Foss (3):
drm/virtio: add virtio_gpu_alloc_fence()
drm/virtio: add uapi for in and out explicit fences drm/virtio: add in/out fence support for explicit synchronization
drivers/gpu/drm/virtio/virtgpu_drv.h | 8 +- drivers/gpu/drm/virtio/virtgpu_fence.c | 29 +++++-- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 108 +++++++++++++++++++++---- drivers/gpu/drm/virtio/virtgpu_plane.c | 46 +++++++++-- drivers/gpu/drm/virtio/virtgpu_vq.c | 2 +- include/uapi/drm/virtgpu_drm.h | 13 ++- 6 files changed, 173 insertions(+), 33 deletions(-)
Signature · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v5 1/4 drm/virtio: add virtio_gpu_alloc_fence()
Refactor fence creation, add fences to relevant GPU operations and add cursor helper functions.
This removes the potential for allocation failures from the cmd_submit and atomic_commit paths.
Now a fence will be allocated first and only after that will we proceed with the rest of the execution.
---
Changes since v3:
- Gerd: Clarified and extended commit message
- Emil: Fixed whitespace issue
- Emil: Changed label name from fail_fence to fail_backoff
- Emil: Remove special case for !fence->drv in virtio_gpu_fence_cleanup()
drivers/gpu/drm/virtio/virtgpu_drv.h | 4 +++ drivers/gpu/drm/virtio/virtgpu_fence.c | 29 ++++++++++++---- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 30 +++++++++++++++-- drivers/gpu/drm/virtio/virtgpu_plane.c | 46 +++++++++++++++++++++++--- drivers/gpu/drm/virtio/virtgpu_vq.c | 2 +- 5 files changed, 96 insertions(+), 15 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +4 -0
@@ -131,6 +131,7 @@ struct virtio_gpu_framebuffer {int x1, y1, x2, y2;/* dirty rect */ spinlock_t dirty_lock;uint32_t hw_res_handle; +struct virtio_gpu_fence *fence; };#define to_virtio_gpu_framebuffer (x) \ container_of(x, struct virtio_gpu_framebuffer, base)
@@ -349,6 +350,9 @@ void virtio_gpu_ttm_fini(struct virtio_gpu_device *vgdev);int virtio_gpu_mmap (struct file *filp,struct vm_area_struct *vma);/* virtio_gpu_fence.c */ +struct virtio_gpu_fence *virtio_gpu_fence_alloc ( +struct virtio_gpu_device *vgdev); +void virtio_gpu_fence_cleanup (struct virtio_gpu_fence *fence);int virtio_gpu_fence_emit (struct virtio_gpu_device *vgdev,struct virtio_gpu_ctrl_hdr *cmd_hdr,struct virtio_gpu_fence **fence);
drivers/gpu/drm/virtio/virtgpu_fence.c +22 -7
@@ -67,6 +67,28 @@ static const struct dma_fence_ops virtio_fence_ops = { .timeline_value_str = virtio_timeline_value_str, }; +struct virtio_gpu_fence *virtio_gpu_fence_alloc (struct virtio_gpu_device *vgdev) +{ +struct virtio_gpu_fence_driver *drv = &vgdev->fence_drv; +struct virtio_gpu_fence *fence =kzalloc (sizeof (struct virtio_gpu_fence ), +GFP_ATOMIC ); +if (!fence) +return fence; + + fence->drv = drv; +dma_fence_init (&fence->f, &virtio_fence_ops, &drv->lock, drv->context,0 ); + +return fence; +} + +void virtio_gpu_fence_cleanup (struct virtio_gpu_fence *fence) +{ +if (!fence) +return ; + +dma_fence_put (&fence->f); +} +int virtio_gpu_fence_emit (struct virtio_gpu_device *vgdev,struct virtio_gpu_ctrl_hdr *cmd_hdr,struct virtio_gpu_fence **fence)
@@ -74,15 +96,8 @@ int virtio_gpu_fence_emit(struct virtio_gpu_device *vgdev,struct virtio_gpu_fence_driver *drv = &vgdev->fence_drv;unsigned long irq_flags; - *fence =kmalloc (sizeof (struct virtio_gpu_fence ),GFP_ATOMIC ); -if ((*fence) ==NULL ) -return -ENOMEM ; -spin_lock_irqsave (&drv->lock, irq_flags); - (*fence)->drv = drv; (*fence)->seq = ++drv->sync_seq; -dma_fence_init (&(*fence)->f, &virtio_fence_ops, &drv->lock, - drv->context, (*fence)->seq);dma_fence_get (&(*fence)->f);list_add_tail (&(*fence)->node, &drv->fences);spin_unlock_irqrestore (&drv->lock, irq_flags);
drivers/gpu/drm/virtio/virtgpu_ioctl.c +28 -2
@@ -168,6 +168,13 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, ret =PTR_ERR (buf); goto out_unresv; } + + fence =virtio_gpu_fence_alloc (vgdev); +if (!fence) { +kfree (buf); + ret = -ENOMEM ; + goto out_unresv; + }virtio_gpu_cmd_submit (vgdev, buf, exbuf->size, vfpriv->ctx_id, &fence);
@@ -283,11 +290,17 @@ static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data, rc_3d.nr_samples =cpu_to_le32 (rc->nr_samples); rc_3d.flags =cpu_to_le32 (rc->flags); + fence =virtio_gpu_fence_alloc (vgdev); +if (!fence) { + ret = -ENOMEM ; + goto fail_backoff; + } +virtio_gpu_cmd_resource_create_3d (vgdev, qobj, &rc_3d,NULL ); ret =virtio_gpu_object_attach (vgdev, qobj, &fence);if (ret) { -ttm_eu_backoff_reservation (&ticket, &validate_list); - goto fail_unref; +virtio_gpu_fence_cleanup (fence); + goto fail_backoff; }ttm_eu_fence_buffer_objects (&ticket, &validate_list, &fence->f); }
@@ -312,6 +325,8 @@ static int virtio_gpu_resource_create_ioctl(struct drm_device *dev, void *data,dma_fence_put (&fence->f); }return 0 ; +fail_backoff: +ttm_eu_backoff_reservation (&ticket, &validate_list); fail_unref:if (vgdev->has_virgl_3d) {virtio_gpu_unref_list (&validate_list);
@@ -374,6 +389,12 @@ static int virtio_gpu_transfer_from_host_ioctl(struct drm_device *dev, goto out_unres;convert_to_hw_box (&box, &args->box); + + fence =virtio_gpu_fence_alloc (vgdev); +if (!fence) { + ret = -ENOMEM ; + goto out_unres; + } virtio_gpu_cmd_transfer_from_host_3d (vgdev, qobj->hw_res_handle, vfpriv->ctx_id, offset, args->level,
@@ -423,6 +444,11 @@ static int virtio_gpu_transfer_to_host_ioctl(struct drm_device *dev, void *data, (vgdev, qobj, offset, box.w, box.h, box.x, box.y,NULL ); }else { + fence =virtio_gpu_fence_alloc (vgdev); +if (!fence) { + ret = -ENOMEM ; + goto out_unres; + } virtio_gpu_cmd_transfer_to_host_3d (vgdev, qobj, vfpriv ? vfpriv->ctx_id : 0, offset,
drivers/gpu/drm/virtio/virtgpu_plane.c +41 -5
73 lines not on this page · raw message
drivers/gpu/drm/virtio/virtgpu_vq.c +1 -1
@@ -896,9 +896,9 @@ void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev, struct virtio_gpu_object *obj) {bool use_dma_api = !virtio_has_iommu_quirk (vgdev->vdev); -struct virtio_gpu_fence *fence;if (use_dma_api && obj->mapped) { +struct virtio_gpu_fence *fence =virtio_gpu_fence_alloc (vgdev);/* detach backing and wait for the host process it ... */ virtio_gpu_cmd_resource_inval_backing (vgdev, obj->hw_res_handle, &fence);dma_fence_wait (&fence->f, true);
Signature · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v5 2/4 drm/virtio: add uapi for in and out explicit fences
In reply to Robert FossAdd a new field called fence_fd that will be used by userspace to send in-fences to the kernel and receive out-fences created by the kernel.
This uapi enables virtio to take advantage of explicit synchronization of dma-bufs.
There are two new flags:
* VIRTGPU_EXECBUF_FENCE_FD_IN to be used when passing an in-fence fd.
* VIRTGPU_EXECBUF_FENCE_FD_OUT to be used when requesting an out-fence fd
The execbuffer IOCTL is now read-write to allow the userspace to read the out-fence.
On error -1 should be returned in the fence_fd field.
---
Changes since v4:
- Emil/Gerd: Improved commit message and fence_fd comment
Changes since v3:
- Emil: Move fence_fd assignment to after sanity checks
- Emil: Added r-b
drivers/gpu/drm/virtio/virtgpu_ioctl.c | 5 +++++ include/uapi/drm/virtgpu_drm.h | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +5 -0
@@ -119,6 +119,11 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,if (vgdev->has_virgl_3d == false)return -ENOSYS ; +if ((exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS )) +return -EINVAL ; + + exbuf->fence_fd =-1 ; +INIT_LIST_HEAD (&validate_list);if (exbuf->num_bo_handles) {
include/uapi/drm/virtgpu_drm.h +10 -3
@@ -47,6 +47,13 @@ extern "C" {#define DRM_VIRTGPU_WAIT 0x08#define DRM_VIRTGPU_GET_CAPS 0x09 +#define VIRTGPU_EXECBUF_FENCE_FD_IN 0x01 +#define VIRTGPU_EXECBUF_FENCE_FD_OUT 0x02 +#define VIRTGPU_EXECBUF_FLAGS (\ + VIRTGPU_EXECBUF_FENCE_FD_IN |\ + VIRTGPU_EXECBUF_FENCE_FD_OUT |\ + 0) +struct drm_virtgpu_map {__u64 offset;/* use for mmap system call */ __u32 handle;
@@ -54,12 +61,12 @@ struct drm_virtgpu_map { };struct drm_virtgpu_execbuffer { -__u32 flags;/* for future use */ +__u32 flags;__u32 size;__u64 command;/* void* */ __u64 bo_handles;__u32 num_bo_handles; -__u32 pad; +__s32 fence_fd; };#define VIRTGPU_PARAM_3D_FEATURES 1/* do we have 3D features in the hw */
@@ -137,7 +144,7 @@ struct drm_virtgpu_get_caps { DRM_IOWR(DRM_COMMAND_BASE + DRM_VIRTGPU_MAP, struct drm_virtgpu_map)#define DRM_IOCTL_VIRTGPU_EXECBUFFER \ - DRM_IOW(DRM_COMMAND_BASE + DRM_VIRTGPU_EXECBUFFER,\ + DRM_IOWR(DRM_COMMAND_BASE + DRM_VIRTGPU_EXECBUFFER,\ struct drm_virtgpu_execbuffer)#define DRM_IOCTL_VIRTGPU_GETPARAM \
Signature · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v5 3/4 drm/virtio: add in/out fence support for explicit synchronization
In reply to Robert FossWhen the execbuf call receives an in-fence it will get the dma_fence related to that fence fd and wait on it before submitting the draw call.
On the out-fence side we get fence returned by the submitted draw call and attach it to a sync_file and send the sync_file fd to userspace. On error -1 is returned to userspace.
VIRTGPU_EXECBUF_FENCE_FD_IN & VIRTGPU_EXECBUF_FENCE_FD_OUT are supported at the simultaneously and can be flagged for simultaneously.
---
Changes since v3:
- Move all in_fence handling to the same VIRTGPU_EXECBUF_FENCE_FD_IN block
- Emil: Make sure to always call dma_fence_put()
- Emil: Added r-b
drivers/gpu/drm/virtio/virtgpu_ioctl.c | 81 ++++++++++++++++++++------ include/uapi/drm/virtgpu_drm.h | 2 +- 2 files changed, 65 insertions(+), 18 deletions(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +64 -17
@@ -28,6 +28,7 @@#include <drm/drmP.h> #include <drm/virtgpu_drm.h> #include <drm/ttm/ttm_execbuf_util.h> +#include <linux/sync_file.h> #include "virtgpu_drv.h"
@@ -105,7 +106,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,struct virtio_gpu_device *vgdev = dev->dev_private;struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv;struct drm_gem_object *gobj; -struct virtio_gpu_fence *fence; +struct virtio_gpu_fence *out_fence;struct virtio_gpu_object *qobj;int ret;uint32_t *bo_handles =NULL ;
@@ -114,6 +115,9 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,struct ttm_validate_buffer *buflist =NULL ;int i;struct ww_acquire_ctx ticket; +struct sync_file *sync_file; +int in_fence_fd = exbuf->fence_fd; +int out_fence_fd =-1 ;void *buf;if (vgdev->has_virgl_3d == false)
@@ -124,6 +128,33 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, exbuf->fence_fd =-1 ; +if (exbuf->flags &VIRTGPU_EXECBUF_FENCE_FD_IN ) { +struct dma_fence *in_fence; + + in_fence =sync_file_get_fence (in_fence_fd); + +if (!in_fence) +return -EINVAL ; + +/* +* Wait if the fence is from a foreign context, or if the fence +* array contains any fence from a foreign context. +*/ + ret =0 ; +if (!dma_fence_match_context (in_fence, vgdev->fence_drv.context)) + ret =dma_fence_wait (in_fence, true); + +dma_fence_put (in_fence); +if (ret) +return ret; + } + +if (exbuf->flags &VIRTGPU_EXECBUF_FENCE_FD_OUT ) { + out_fence_fd =get_unused_fd_flags (O_CLOEXEC ); +if (out_fence_fd <0 ) +return out_fence_fd; + } +INIT_LIST_HEAD (&validate_list);if (exbuf->num_bo_handles) {
@@ -133,26 +164,22 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,sizeof (struct ttm_validate_buffer ), GFP_KERNEL | __GFP_ZERO);if (!bo_handles || !buflist) { -kvfree (bo_handles); -kvfree (buflist); -return -ENOMEM ; + ret = -ENOMEM ; + goto out_unused_fd; } user_bo_handles = (void __user *)(uintptr_t )exbuf->bo_handles;if (copy_from_user (bo_handles, user_bo_handles, exbuf->num_bo_handles *sizeof (uint32_t ))) { ret = -EFAULT ; -kvfree (bo_handles); -kvfree (buflist); -return ret; + goto out_unused_fd; }for (i =0 ; i < exbuf->num_bo_handles; i++) { gobj =drm_gem_object_lookup (drm_file, bo_handles[i]);if (!gobj) { -kvfree (bo_handles); -kvfree (buflist); -return -ENOENT ; + ret = -ENOENT ; + goto out_unused_fd; } qobj =gem_to_virtio_gpu_obj (gobj);
@@ -161,6 +188,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,list_add (&buflist[i].head, &validate_list); }kvfree (bo_handles); + bo_handles =NULL ; } ret =virtio_gpu_object_list_validate (&ticket, &validate_list);
@@ -174,28 +202,47 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, goto out_unresv; } - fence =virtio_gpu_fence_alloc (vgdev); -if (!fence) { -kfree (buf); + out_fence =virtio_gpu_fence_alloc (vgdev); +if (!out_fence) { ret = -ENOMEM ; - goto out_unresv; + goto out_memdup; + } + +if (out_fence_fd >=0 ) { + sync_file =sync_file_create (&out_fence->f); +if (!sync_file) { +dma_fence_put (&out_fence->f); + ret = -ENOMEM ; + goto out_memdup; + } + + exbuf->fence_fd = out_fence_fd; +fd_install (out_fence_fd, sync_file->file); } +virtio_gpu_cmd_submit (vgdev, buf, exbuf->size, - vfpriv->ctx_id, &fence); + vfpriv->ctx_id, &out_fence); -ttm_eu_fence_buffer_objects (&ticket, &validate_list, &fence->f); +ttm_eu_fence_buffer_objects (&ticket, &validate_list, &out_fence->f);/* fence the command bo */ virtio_gpu_unref_list (&validate_list);kvfree (buflist); -dma_fence_put (&fence->f);return 0 ; +out_memdup: +kfree (buf); out_unresv:ttm_eu_backoff_reservation (&ticket, &validate_list); out_free:virtio_gpu_unref_list (&validate_list); +out_unused_fd: +kvfree (bo_handles);kvfree (buflist); + +if (out_fence_fd >=0 ) +put_unused_fd (out_fence_fd); +return ret; }
include/uapi/drm/virtgpu_drm.h +1 -1
@@ -66,7 +66,7 @@ struct drm_virtgpu_execbuffer {__u64 command;/* void* */ __u64 bo_handles;__u32 num_bo_handles; -__s32 fence_fd; +__s32 fence_fd;/* in/out fence fd (see VIRTGPU_EXECBUF_FENCE_FD_IN/OUT) */ };#define VIRTGPU_PARAM_3D_FEATURES 1/* do we have 3D features in the hw */
Signature · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
PATCH v5 4/4 drm/virtio: bump driver version after explicit synchronization addition
In reply to Robert FossFrom: Gustavo Padovan <[email protected]>
To reflect the (backward compatible) changes in the uabi we are bumping the driver's version.
---
Changes since v3:
- Emil: Added r-b
drivers/gpu/drm/virtio/virtgpu_drv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +2 -2
@@ -47,8 +47,8 @@#define DRIVER_DATE "0"#define DRIVER_MAJOR 0 -#define DRIVER_MINOR 0 -#define DRIVER_PATCHLEVEL 1 +#define DRIVER_MINOR 1 +#define DRIVER_PATCHLEVEL 0/* virtgpu_drm_bus.c */ int drm_virtio_init (struct drm_driver *driver,struct virtio_device *vdev);
Signature · 1 line
2.17.1
Gerd Hoffmann
maintainerQuoted 8 lines · Robert Foss
This series implements fence support for drm/virtio and has been tested using qemu, kmscube and the below branches.
Rob Herring solved a reference counting issue and suggested a context check for the execbuf ioctl, his changes have been included in the below commits to keep the tree working at all commits.
Patches added to qemu queue, should land in drm-misc-next soon.
thanks,
Gerd
Gerd Hoffmann
maintainerQuoted 8 lines · Robert Foss
This series implements fence support for drm/virtio and has been tested using qemu, kmscube and the below branches.
Rob Herring solved a reference counting issue and suggested a context check for the execbuf ioctl, his changes have been included in the below commits to keep the tree working at all commits.
Patches added to qemu queue, should land in drm-misc-next soon.
thanks,
Gerd