panfrost: Inline reference counting routines
authorAlyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Mon, 23 Mar 2020 23:17:49 +0000 (19:17 -0400)
committerMarge Bot <eric+marge@anholt.net>
Tue, 31 Mar 2020 01:12:26 +0000 (01:12 +0000)
We use only a very small subset of the capabilities of
pipe_reference (just wrappers for atomic ints..). Let's inline it and
drop the dependency on Gallium from pan_bo.

Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4382>

src/gallium/drivers/panfrost/pan_bo.c
src/gallium/drivers/panfrost/pan_bo.h

index d7bcae5b024ca06e3562da438977fbc25aa66dca..62e14a5bafd1868a6d67bf1d66535a966a3f9ccb 100644 (file)
@@ -409,7 +409,7 @@ panfrost_bo_create(struct panfrost_device *dev, size_t size,
                         pandecode_inject_mmap(bo->gpu, NULL, bo->size, NULL);
         }
 
-        pipe_reference_init(&bo->reference, 1);
+        p_atomic_set(&bo->refcnt, 1);
 
         pthread_mutex_lock(&dev->active_bos_lock);
         _mesa_set_add(bo->dev->active_bos, bo);
@@ -421,8 +421,10 @@ panfrost_bo_create(struct panfrost_device *dev, size_t size,
 void
 panfrost_bo_reference(struct panfrost_bo *bo)
 {
-        if (bo)
-                pipe_reference(NULL, &bo->reference);
+        if (bo) {
+                ASSERTED int count = p_atomic_inc_return(&bo->refcnt);
+                assert(count != 1);
+        }
 }
 
 void
@@ -431,7 +433,8 @@ panfrost_bo_unreference(struct panfrost_bo *bo)
         if (!bo)
                 return;
 
-        if (!pipe_reference(&bo->reference, NULL))
+        /* Don't return to cache if there are still references */
+        if (p_atomic_dec_return(&bo->refcnt))
                 return;
 
         struct panfrost_device *dev = bo->dev;
@@ -440,7 +443,7 @@ panfrost_bo_unreference(struct panfrost_bo *bo)
         /* Someone might have imported this BO while we were waiting for the
          * lock, let's make sure it's still not referenced before freeing it.
          */
-        if (!pipe_is_referenced(&bo->reference)) {
+        if (p_atomic_read(&bo->refcnt) == 0) {
                 _mesa_set_remove_key(bo->dev->active_bos, bo);
 
                 /* When the reference count goes to zero, we need to cleanup */
@@ -484,23 +487,23 @@ panfrost_bo_import(struct panfrost_device *dev, int fd)
                 newbo->size = lseek(fd, 0, SEEK_END);
                 newbo->flags |= PAN_BO_DONT_REUSE | PAN_BO_IMPORTED;
                 assert(newbo->size > 0);
-                pipe_reference_init(&newbo->reference, 1);
+                p_atomic_set(&newbo->refcnt, 1);
                 // TODO map and unmap on demand?
                 panfrost_bo_mmap(newbo);
         } else {
                 ralloc_free(newbo);
-                /* !pipe_is_referenced(&bo->reference) can happen if the BO
+                /* bo->refcnt != 0 can happen if the BO
                  * was being released but panfrost_bo_import() acquired the
                  * lock before panfrost_bo_unreference(). In that case, refcnt
                  * is 0 and we can't use panfrost_bo_reference() directly, we
-                 * have to re-initialize it with pipe_reference_init().
+                 * have to re-initialize the refcnt().
                  * Note that panfrost_bo_unreference() checks
-                 * pipe_is_referenced() value just after acquiring the lock to
+                 * refcnt value just after acquiring the lock to
                  * make sure the object is not freed if panfrost_bo_import()
                  * acquired it in the meantime.
                  */
-                if (!pipe_is_referenced(&bo->reference))
-                        pipe_reference_init(&newbo->reference, 1);
+                if (p_atomic_read(&bo->refcnt))
+                        p_atomic_set(&newbo->refcnt, 1);
                 else
                         panfrost_bo_reference(bo);
                 assert(bo->cpu);
index ef9e7033a1c4a3ccadff904434fbed506d131014..fc20ceed1d47f9bbed6ac540a9b8706d8a7b61ef 100644 (file)
@@ -27,7 +27,6 @@
 #define __PAN_BO_H__
 
 #include <panfrost-misc.h>
-#include "pipe/p_state.h"
 #include "util/list.h"
 #include "pan_device.h"
 
@@ -91,7 +90,8 @@ struct panfrost_bo {
          */
         time_t last_used;
 
-        struct pipe_reference reference;
+        /* Atomic reference count */
+        int32_t refcnt;
 
         struct panfrost_device *dev;