drm: Add reservation_object to drm_gem_object
authorRob Herring <robh@kernel.org>
Sat, 2 Feb 2019 15:41:54 +0000 (09:41 -0600)
committerMaxime Ripard <maxime.ripard@bootlin.com>
Tue, 19 Feb 2019 10:08:40 +0000 (11:08 +0100)
Many users of drm_gem_object embed a struct reservation_object into
their subclassed struct, so let's add one to struct drm_gem_object.
This will allow removing the reservation object from the subclasses
and removing the ->gem_prime_res_obj callback.

With the addition, add a drm_gem_reservation_object_wait() helper
function for drivers to use in wait ioctls.

Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <maxime.ripard@bootlin.com>
Cc: Sean Paul <sean@poorly.run>
Cc: David Airlie <airlied@linux.ie>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Rob Herring <robh@kernel.org>
Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Reviewed-by: Eric Anholt <eric@anholt.net>
Reviewed-by: Christian Gmeiner <christian.gmeiner@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190202154158.10443-2-robh@kernel.org
Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
Documentation/gpu/todo.rst
drivers/gpu/drm/drm_gem.c
drivers/gpu/drm/drm_prime.c
include/drm/drm_gem.h

index 159a4aba49e6eec9053434ba1c89dbe264d93f01..4afbc186f119cf94ee700a06b9eb58f8df9559f3 100644 (file)
@@ -215,12 +215,12 @@ Might be good to also have some igt testcases for this.
 
 Contact: Daniel Vetter, Noralf Tronnes
 
-Put a reservation_object into drm_gem_object
+Remove the ->gem_prime_res_obj callback
 --------------------------------------------
 
-This would remove the need for the ->gem_prime_res_obj callback. It would also
-allow us to implement generic helpers for waiting for a bo, allowing for quite a
-bit of refactoring in the various wait ioctl implementations.
+The ->gem_prime_res_obj callback can be removed from drivers by using the
+reservation_object in the drm_gem_object. It may also be possible to use the
+generic drm_gem_reservation_object_wait helper for waiting for a bo.
 
 Contact: Daniel Vetter
 
index d0b9f6a9953f38c61a6d0770a42bf6d575515413..ad124f5a6f4dae6574762cff61bd48e13e310195 100644 (file)
@@ -171,6 +171,10 @@ void drm_gem_private_object_init(struct drm_device *dev,
        kref_init(&obj->refcount);
        obj->handle_count = 0;
        obj->size = size;
+       reservation_object_init(&obj->_resv);
+       if (!obj->resv)
+               obj->resv = &obj->_resv;
+
        drm_vma_node_reset(&obj->vma_node);
 }
 EXPORT_SYMBOL(drm_gem_private_object_init);
@@ -687,6 +691,44 @@ drm_gem_object_lookup(struct drm_file *filp, u32 handle)
 }
 EXPORT_SYMBOL(drm_gem_object_lookup);
 
+/**
+ * drm_gem_reservation_object_wait - Wait on GEM object's reservation's objects
+ * shared and/or exclusive fences.
+ * @filep: DRM file private date
+ * @handle: userspace handle
+ * @wait_all: if true, wait on all fences, else wait on just exclusive fence
+ * @timeout: timeout value in jiffies or zero to return immediately
+ *
+ * Returns:
+ *
+ * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
+ * greater than 0 on success.
+ */
+long drm_gem_reservation_object_wait(struct drm_file *filep, u32 handle,
+                                   bool wait_all, unsigned long timeout)
+{
+       long ret;
+       struct drm_gem_object *obj;
+
+       obj = drm_gem_object_lookup(filep, handle);
+       if (!obj) {
+               DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
+               return -EINVAL;
+       }
+
+       ret = reservation_object_wait_timeout_rcu(obj->resv, wait_all,
+                                                 true, timeout);
+       if (ret == 0)
+               ret = -ETIME;
+       else if (ret > 0)
+               ret = 0;
+
+       drm_gem_object_put_unlocked(obj);
+
+       return ret;
+}
+EXPORT_SYMBOL(drm_gem_reservation_object_wait);
+
 /**
  * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  * @dev: drm_device
@@ -851,6 +893,7 @@ drm_gem_object_release(struct drm_gem_object *obj)
        if (obj->filp)
                fput(obj->filp);
 
+       reservation_object_fini(&obj->_resv);
        drm_gem_free_mmap_offset(obj);
 }
 EXPORT_SYMBOL(drm_gem_object_release);
index 231e3f6d5f4162c243c19c04811b71727f7c50cb..dc079efb3b0fcc99ff36856187ee50aac0ef7322 100644 (file)
@@ -504,6 +504,7 @@ struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
                .size = obj->size,
                .flags = flags,
                .priv = obj,
+               .resv = obj->resv,
        };
 
        if (dev->driver->gem_prime_res_obj)
index c957274252844fc1d332bc04908a803161ed4c4c..25f1ff2df464a5e0f261d813492f611333787b05 100644 (file)
@@ -35,6 +35,7 @@
  */
 
 #include <linux/kref.h>
+#include <linux/reservation.h>
 
 #include <drm/drm_vma_manager.h>
 
@@ -262,6 +263,24 @@ struct drm_gem_object {
         */
        struct dma_buf_attachment *import_attach;
 
+       /**
+        * @resv:
+        *
+        * Pointer to reservation object associated with the this GEM object.
+        *
+        * Normally (@resv == &@_resv) except for imported GEM objects.
+        */
+       struct reservation_object *resv;
+
+       /**
+        * @_resv:
+        *
+        * A reservation object for this GEM object.
+        *
+        * This is unused for imported GEM objects.
+        */
+       struct reservation_object _resv;
+
        /**
         * @funcs:
         *
@@ -363,6 +382,8 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
                bool dirty, bool accessed);
 
 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
+long drm_gem_reservation_object_wait(struct drm_file *filep, u32 handle,
+                                   bool wait_all, unsigned long timeout);
 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
                            u32 handle, u64 *offset);
 int drm_gem_dumb_destroy(struct drm_file *file,