#include <linux/module.h>
 #include <linux/console.h>
 #include <linux/pci.h>
+#include <linux/poll.h>
+#include <linux/wait.h>
 
 #include <drm/drm.h>
 #include <drm/drm_aperture.h>
        schedule_work(&vgdev->config_changed_work);
 }
 
+static __poll_t virtio_gpu_poll(struct file *filp,
+                               struct poll_table_struct *wait)
+{
+       struct drm_file *drm_file = filp->private_data;
+       struct virtio_gpu_fpriv *vfpriv = drm_file->driver_priv;
+       struct drm_device *dev = drm_file->minor->dev;
+       struct drm_pending_event *e = NULL;
+       __poll_t mask = 0;
+
+       if (!vfpriv->ring_idx_mask)
+               return drm_poll(filp, wait);
+
+       poll_wait(filp, &drm_file->event_wait, wait);
+
+       if (!list_empty(&drm_file->event_list)) {
+               spin_lock_irq(&dev->event_lock);
+               e = list_first_entry(&drm_file->event_list,
+                                    struct drm_pending_event, link);
+               drm_file->event_space += e->event->length;
+               list_del(&e->link);
+               spin_unlock_irq(&dev->event_lock);
+
+               kfree(e);
+               mask |= EPOLLIN | EPOLLRDNORM;
+       }
+
+       return mask;
+}
+
 static struct virtio_device_id id_table[] = {
        { VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
        { 0 },
 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
 MODULE_AUTHOR("Alon Levy");
 
-DEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
+static const struct file_operations virtio_gpu_driver_fops = {
+       .owner          = THIS_MODULE,
+       .open           = drm_open,
+       .release        = drm_release,
+       .unlocked_ioctl = drm_ioctl,
+       .compat_ioctl   = drm_compat_ioctl,
+       .poll           = virtio_gpu_poll,
+       .read           = drm_read,
+       .llseek         = noop_llseek,
+       .mmap           = drm_gem_mmap
+};
 
 static const struct drm_driver driver = {
        .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC,
 
                                    VIRTGPU_BLOB_FLAG_USE_SHAREABLE | \
                                    VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE)
 
+static int virtio_gpu_fence_event_create(struct drm_device *dev,
+                                        struct drm_file *file,
+                                        struct virtio_gpu_fence *fence,
+                                        uint32_t ring_idx)
+{
+       struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
+       struct virtio_gpu_fence_event *e = NULL;
+       int ret;
+
+       if (!(vfpriv->ring_idx_mask & (1 << ring_idx)))
+               return 0;
+
+       e = kzalloc(sizeof(*e), GFP_KERNEL);
+       if (!e)
+               return -ENOMEM;
+
+       e->event.type = VIRTGPU_EVENT_FENCE_SIGNALED_INTERNAL;
+       e->event.length = sizeof(e->event);
+
+       ret = drm_event_reserve_init(dev, file, &e->base, &e->event);
+       if (ret)
+               goto free;
+
+       fence->e = e;
+       return 0;
+free:
+       kfree(e);
+       return ret;
+}
+
 /* Must be called with &virtio_gpu_fpriv.struct_mutex held. */
 static void virtio_gpu_create_context_locked(struct virtio_gpu_device *vgdev,
                                             struct virtio_gpu_fpriv *vfpriv)
                goto out_unresv;
        }
 
+       ret = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
+       if (ret)
+               goto out_unresv;
+
        if (out_fence_fd >= 0) {
                sync_file = sync_file_create(&out_fence->f);
                if (!sync_file) {