drm/ttm: inline ttm_bo_reserve and related calls
authorFrançois Tigeot <ftigeot@wolfpond.org>
Tue, 12 Mar 2019 18:09:34 +0000 (19:09 +0100)
committerFrançois Tigeot <ftigeot@wolfpond.org>
Tue, 12 Mar 2019 18:10:43 +0000 (19:10 +0100)
sys/conf/kern.pre.mk
sys/dev/drm/drm/Makefile
sys/dev/drm/include/drm/ttm/ttm_bo_driver.h
sys/dev/drm/include/drm/ttm/ttm_execbuf_util.h
sys/dev/drm/include/drm/ttm/ttm_object.h
sys/dev/drm/radeon/Makefile
sys/dev/drm/ttm/ttm_bo.c
sys/dev/drm/ttm/ttm_execbuf_util.c

index 39a01f8..a68b726 100644 (file)
@@ -58,7 +58,9 @@ INCLUDES+= -I${.OBJDIR} -I"$S/${OSACPI_MI_DIR}" -I"$S/${ACPICA_DIR}/include"
 INCLUDES+= -I$S/dev/netif/ath/ath_hal -I$S/contrib/dev/ath/ath_hal
 
 # Same thing for drm includes
-INCLUDES+= -I$S/dev/drm/include -I$S/dev/drm/include/uapi
+INCLUDES+= -I$S/dev/drm/include
+INCLUDES+= -I$S/dev/drm/include/drm
+INCLUDES+= -I$S/dev/drm/include/uapi
 
 COPTS= ${INCLUDES} ${IDENT} -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h
 CFLAGS=        ${COPTFLAGS} ${KCFLAGS} ${CWARNFLAGS} ${DEBUG} ${COPTS}
index efb2b4d..f99fc42 100644 (file)
@@ -83,6 +83,7 @@ SRCS   += device_if.h bus_if.h pci_if.h device_if.h iicbus_if.h opt_drm.h \
          opt_vm.h
 
 KCFLAGS+= -I${SYSDIR}/dev/drm/include
+KCFLAGS+= -I${SYSDIR}/dev/drm/include/drm
 KCFLAGS+= -I${SYSDIR}/dev/drm/include/uapi
 KCFLAGS+= -include ${SYSDIR}/dev/drm/kconfig.h
 
index 5e3f836..89b3489 100644 (file)
@@ -771,6 +771,54 @@ extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
                           bool interruptible);
 extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
 
+extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
+
+/**
+ * ttm_bo_reserve_nolru:
+ *
+ * @bo: A pointer to a struct ttm_buffer_object.
+ * @interruptible: Sleep interruptible if waiting.
+ * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
+ * @use_ticket: If @bo is already reserved, Only sleep waiting for
+ * it to become unreserved if @ticket->stamp is older.
+ *
+ * Will not remove reserved buffers from the lru lists.
+ * Otherwise identical to ttm_bo_reserve.
+ *
+ * Returns:
+ * -EDEADLK: The reservation may cause a deadlock.
+ * Release all buffer reservations, wait for @bo to become unreserved and
+ * try again. (only if use_sequence == 1).
+ * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
+ * a signal. Release all buffer reservations and return to user-space.
+ * -EBUSY: The function needed to sleep, but @no_wait was true
+ * -EALREADY: Bo already reserved using @ticket. This error code will only
+ * be returned if @use_ticket is set to true.
+ */
+static inline int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
+                                      bool interruptible,
+                                      bool no_wait, bool use_ticket,
+                                      struct ww_acquire_ctx *ticket)
+{
+       int ret = 0;
+
+       if (no_wait) {
+               bool success;
+               if (WARN_ON(ticket))
+                       return -EBUSY;
+
+               success = ww_mutex_trylock(&bo->resv->lock);
+               return success ? 0 : -EBUSY;
+       }
+
+       if (interruptible)
+               ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
+       else
+               ret = ww_mutex_lock(&bo->resv->lock, ticket);
+       if (ret == -EINTR)
+               return -ERESTARTSYS;
+       return ret;
+}
 
 /**
  * ttm_bo_reserve:
@@ -779,7 +827,7 @@ extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  * @interruptible: Sleep interruptible if waiting.
  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
  * @use_ticket: If @bo is already reserved, Only sleep waiting for
- * it to become unreserved if @sequence < (@bo)->sequence.
+ * it to become unreserved if @ticket->stamp is older.
  *
  * Locks a buffer object for validation. (Or prevents other processes from
  * locking it for validation) and removes it from lru lists, while taking
@@ -793,7 +841,7 @@ extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  * Processes attempting to reserve multiple buffers other than for eviction,
  * (typically execbuf), should first obtain a unique 32-bit
  * validation sequence number,
- * and call this function with @use_sequence == 1 and @sequence == the unique
+ * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
  * sequence number. If upon call of this function, the buffer object is already
  * reserved, the validation sequence is checked against the validation
  * sequence of the process currently reserving the buffer,
@@ -808,37 +856,31 @@ extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
  * will eventually succeed, preventing both deadlocks and starvation.
  *
  * Returns:
- * -EAGAIN: The reservation may cause a deadlock.
+ * -EDEADLK: The reservation may cause a deadlock.
  * Release all buffer reservations, wait for @bo to become unreserved and
  * try again. (only if use_sequence == 1).
  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
  * a signal. Release all buffer reservations and return to user-space.
  * -EBUSY: The function needed to sleep, but @no_wait was true
- * -EDEADLK: Bo already reserved using @sequence. This error code will only
- * be returned if @use_sequence is set to true.
+ * -EALREADY: Bo already reserved using @ticket. This error code will only
+ * be returned if @use_ticket is set to true.
  */
-extern int ttm_bo_reserve(struct ttm_buffer_object *bo,
-                         bool interruptible,
-                         bool no_wait, bool use_ticket,
-                         struct ww_acquire_ctx *ticket);
+static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
+                                bool interruptible,
+                                bool no_wait, bool use_ticket,
+                                struct ww_acquire_ctx *ticket)
+{
+       int ret;
 
-/**
- * ttm_bo_reserve_slowpath_nolru:
- * @bo: A pointer to a struct ttm_buffer_object.
- * @interruptible: Sleep interruptible if waiting.
- * @sequence: Set (@bo)->sequence to this value after lock
- *
- * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
- * from all our other reservations. Because there are no other reservations
- * held by us, this function cannot deadlock any more.
- *
- * Will not remove reserved buffers from the lru lists.
- * Otherwise identical to ttm_bo_reserve_slowpath.
- */
-extern int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
-                                        bool interruptible,
-                                        struct ww_acquire_ctx *ticket);
+       WARN_ON(!atomic_read(&bo->kref.refcount));
+
+       ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
+                                   ticket);
+       if (likely(ret == 0))
+               ttm_bo_del_sub_from_lru(bo);
 
+       return ret;
+}
 
 /**
  * ttm_bo_reserve_slowpath:
@@ -850,45 +892,27 @@ extern int ttm_bo_reserve_slowpath_nolru(struct ttm_buffer_object *bo,
  * from all our other reservations. Because there are no other reservations
  * held by us, this function cannot deadlock any more.
  */
-extern int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
-                                  bool interruptible,
-                                  struct ww_acquire_ctx *ticket);
+static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
+                                         bool interruptible,
+                                         struct ww_acquire_ctx *ticket)
+{
+       int ret = 0;
 
-/**
- * ttm_bo_reserve_nolru:
- *
- * @bo: A pointer to a struct ttm_buffer_object.
- * @interruptible: Sleep interruptible if waiting.
- * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
- * @use_sequence: If @bo is already reserved, Only sleep waiting for
- * it to become unreserved if @sequence < (@bo)->sequence.
- *
- * Will not remove reserved buffers from the lru lists.
- * Otherwise identical to ttm_bo_reserve.
- *
- * Returns:
- * -EAGAIN: The reservation may cause a deadlock.
- * Release all buffer reservations, wait for @bo to become unreserved and
- * try again. (only if use_sequence == 1).
- * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
- * a signal. Release all buffer reservations and return to user-space.
- * -EBUSY: The function needed to sleep, but @no_wait was true
- * -EDEADLK: Bo already reserved using @sequence. This error code will only
- * be returned if @use_sequence is set to true.
- */
-extern int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
-                                bool interruptible,
-                                bool no_wait, bool use_ticket,
-                                struct ww_acquire_ctx *ticket);
+       WARN_ON(!atomic_read(&bo->kref.refcount));
 
-/**
- * ttm_bo_unreserve
- *
- * @bo: A pointer to a struct ttm_buffer_object.
- *
- * Unreserve a previous reservation of @bo.
- */
-extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
+       if (interruptible)
+               ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
+                                                      ticket);
+       else
+               ww_mutex_lock_slow(&bo->resv->lock, ticket);
+
+       if (likely(ret == 0))
+               ttm_bo_del_sub_from_lru(bo);
+       else if (ret == -EINTR)
+               ret = -ERESTARTSYS;
+
+       return ret;
+}
 
 /**
  * ttm_bo_unreserve_ticket
@@ -897,19 +921,28 @@ extern void ttm_bo_unreserve(struct ttm_buffer_object *bo);
  *
  * Unreserve a previous reservation of @bo made with @ticket.
  */
-extern void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
-                                   struct ww_acquire_ctx *ticket);
+static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
+                                          struct ww_acquire_ctx *t)
+{
+       if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
+               lockmgr(&bo->glob->lru_lock, LK_EXCLUSIVE);
+               ttm_bo_add_to_lru(bo);
+               lockmgr(&bo->glob->lru_lock, LK_RELEASE);
+       }
+       ww_mutex_unlock(&bo->resv->lock);
+}
 
 /**
- * ttm_bo_unreserve_locked
+ * ttm_bo_unreserve
+ *
  * @bo: A pointer to a struct ttm_buffer_object.
- * @ticket: ww_acquire_ctx used for reserving, or NULL
  *
- * Unreserve a previous reservation of @bo made with @ticket.
- * Needs to be called with struct ttm_bo_global::lru_lock held.
+ * Unreserve a previous reservation of @bo.
  */
-extern void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo,
-                                          struct ww_acquire_ctx *ticket);
+static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
+{
+       ttm_bo_unreserve_ticket(bo, NULL);
+}
 
 /*
  * ttm_bo_util.c
index a7d1937..ec8a1d3 100644 (file)
@@ -31,7 +31,7 @@
 #ifndef _TTM_EXECBUF_UTIL_H_
 #define _TTM_EXECBUF_UTIL_H_
 
-#include <drm/ttm/ttm_bo_api.h>
+#include <ttm/ttm_bo_api.h>
 #include <linux/list.h>
 
 /**
index f2d13b7..fc0cf06 100644 (file)
@@ -27,7 +27,6 @@
 /*
  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  */
-/* $FreeBSD: head/sys/dev/drm2/ttm/ttm_object.h 247835 2013-03-05 09:49:34Z kib $ */
 /** @file ttm_object.h
  *
  * Base- and reference object implementation for the various
 #ifndef _TTM_OBJECT_H_
 #define _TTM_OBJECT_H_
 
+#include <linux/list.h>
 #include <drm/drm_hashtab.h>
-#include <drm/ttm/ttm_memory.h>
 #include <linux/kref.h>
+#include <linux/rcupdate.h>
+#include <ttm/ttm_memory.h>
 
 /**
  * enum ttm_ref_type
@@ -120,7 +121,7 @@ struct ttm_object_device;
  */
 
 struct ttm_base_object {
-       /* struct rcu_head rhead;XXXKIB */
+       struct rcu_head rhead;
        struct drm_hash_item hash;
        enum ttm_object_type object_type;
        bool shareable;
index 64ce13c..fb3f5e7 100644 (file)
@@ -132,6 +132,7 @@ SRCS        +=                                                              \
        pci_if.h
 
 KCFLAGS+= -I${SYSDIR}/dev/drm/include
+KCFLAGS+= -I${SYSDIR}/dev/drm/include/drm
 KCFLAGS+= -I${SYSDIR}/dev/drm/include/uapi
 KCFLAGS+= -I${SYSDIR}/contrib/dev/acpica/source/include
 KCFLAGS+= -include ${SYSDIR}/dev/drm/kconfig.h
index 7c42f2c..8c2d058 100644 (file)
@@ -182,6 +182,7 @@ void ttm_bo_add_to_lru(struct ttm_buffer_object *bo)
                }
        }
 }
+EXPORT_SYMBOL(ttm_bo_add_to_lru);
 
 int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
 {
@@ -204,35 +205,6 @@ int ttm_bo_del_from_lru(struct ttm_buffer_object *bo)
        return put_count;
 }
 
-int ttm_bo_reserve_nolru(struct ttm_buffer_object *bo,
-                         bool interruptible,
-                         bool no_wait, bool use_ticket,
-                         struct ww_acquire_ctx *ticket)
-{
-       int ret = 0;
-
-       if (no_wait) {
-               bool success;
-
-               /* not valid any more, fix your locking! */
-               if (WARN_ON(ticket))
-                       return -EBUSY;
-
-               success = ww_mutex_trylock(&bo->resv->lock);
-               return success ? 0 : -EBUSY;
-       }
-
-       if (interruptible)
-               ret = ww_mutex_lock_interruptible(&bo->resv->lock,
-                                                 ticket);
-       else
-               ret = ww_mutex_lock(&bo->resv->lock, ticket);
-       if (ret == -EINTR)
-               return -ERESTARTSYS;
-       return ret;
-}
-EXPORT_SYMBOL(ttm_bo_reserve);
-
 static void ttm_bo_ref_bug(struct kref *list_kref)
 {
        BUG();
@@ -245,77 +217,16 @@ void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
                 (never_free) ? ttm_bo_ref_bug : ttm_bo_release_list);
 }
 
-int ttm_bo_reserve(struct ttm_buffer_object *bo,
-                  bool interruptible,
-                  bool no_wait, bool use_ticket,
-                  struct ww_acquire_ctx *ticket)
-{
-       struct ttm_bo_global *glob = bo->glob;
-       int put_count = 0;
-       int ret;
-
-       ret = ttm_bo_reserve_nolru(bo, interruptible, no_wait, use_ticket,
-                                   ticket);
-       if (likely(ret == 0)) {
-               lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
-               put_count = ttm_bo_del_from_lru(bo);
-               lockmgr(&glob->lru_lock, LK_RELEASE);
-               ttm_bo_list_ref_sub(bo, put_count, true);
-       }
-
-       return ret;
-}
-
-int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
-                           bool interruptible, struct ww_acquire_ctx *ticket)
-{
-       struct ttm_bo_global *glob = bo->glob;
-       int put_count = 0;
-       int ret = 0;
-
-       if (interruptible)
-               ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
-                                                      ticket);
-       else
-               ww_mutex_lock_slow(&bo->resv->lock, ticket);
-
-       if (likely(ret == 0)) {
-               lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
-               put_count = ttm_bo_del_from_lru(bo);
-               lockmgr(&glob->lru_lock, LK_RELEASE);
-               ttm_bo_list_ref_sub(bo, put_count, true);
-       } else if (ret == -EINTR)
-               ret = -ERESTARTSYS;
-
-       return ret;
-}
-EXPORT_SYMBOL(ttm_bo_reserve_slowpath);
-
-void ttm_bo_unreserve_ticket_locked(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
-{
-       ttm_bo_add_to_lru(bo);
-       ww_mutex_unlock(&bo->resv->lock);
-}
-
-void ttm_bo_unreserve(struct ttm_buffer_object *bo)
-{
-       struct ttm_bo_global *glob = bo->glob;
-
-       lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
-       ttm_bo_unreserve_ticket_locked(bo, NULL);
-       lockmgr(&glob->lru_lock, LK_RELEASE);
-}
-EXPORT_SYMBOL(ttm_bo_unreserve);
-
-void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo, struct ww_acquire_ctx *ticket)
+void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo)
 {
-       struct ttm_bo_global *glob = bo->glob;
+       int put_count;
 
-       lockmgr(&glob->lru_lock, LK_EXCLUSIVE);
-       ttm_bo_unreserve_ticket_locked(bo, ticket);
-       lockmgr(&glob->lru_lock, LK_RELEASE);
+       lockmgr(&bo->glob->lru_lock, LK_EXCLUSIVE);
+        put_count = ttm_bo_del_from_lru(bo);
+        lockmgr(&bo->glob->lru_lock, LK_RELEASE);
+        ttm_bo_list_ref_sub(bo, put_count, true);
 }
-EXPORT_SYMBOL(ttm_bo_unreserve_ticket);
+EXPORT_SYMBOL(ttm_bo_del_sub_from_lru);
 
 /*
  * Call bo->mutex locked.
index b5cb11f..1ec137d 100644 (file)
@@ -44,12 +44,10 @@ static void ttm_eu_backoff_reservation_locked(struct list_head *list,
 
                entry->reserved = false;
                if (entry->removed) {
-                       ttm_bo_unreserve_ticket_locked(bo, ticket);
+                       ttm_bo_add_to_lru(bo);
                        entry->removed = false;
-
-               } else {
-                       ww_mutex_unlock(&bo->resv->lock);
                }
+               ww_mutex_unlock(&bo->resv->lock);
        }
 }
 
@@ -221,7 +219,8 @@ void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
                bo = entry->bo;
                entry->old_sync_obj = bo->sync_obj;
                bo->sync_obj = driver->sync_obj_ref(sync_obj);
-               ttm_bo_unreserve_ticket_locked(bo, ticket);
+               ttm_bo_add_to_lru(bo);
+               ww_mutex_unlock(&bo->resv->lock);
                entry->reserved = false;
        }
        lockmgr(&bdev->fence_lock, LK_RELEASE);