radeonsi: use si_get_indirect_index for TEMP indexing
[mesa.git] / src / gallium / drivers / vc4 / vc4_bufmgr.c
index 9cff4e68f427aa608df8f34eb90739ba74d9e846..b300fa5bd025e43d91e2e1ff48637a2332d50d12 100644 (file)
@@ -48,6 +48,32 @@ static bool dump_stats = false;
 static void
 vc4_bo_cache_free_all(struct vc4_bo_cache *cache);
 
+void
+vc4_bo_label(struct vc4_screen *screen, struct vc4_bo *bo, const char *fmt, ...)
+{
+        /* Perform BO labeling by default on debug builds (so that you get
+         * whole-system allocation information), or if VC4_DEBUG=surf is set
+         * (for debugging a single app's allocation).
+         */
+#ifndef DEBUG
+        if (!(vc4_debug & VC4_DEBUG_SURFACE))
+                return;
+#endif
+        va_list va;
+        va_start(va, fmt);
+        char *name = ralloc_vasprintf(NULL, fmt, va);
+        va_end(va);
+
+        struct drm_vc4_label_bo label = {
+                .handle = bo->handle,
+                .len = strlen(name),
+                .name = (uintptr_t)name,
+        };
+        drmIoctl(screen->fd, DRM_IOCTL_VC4_LABEL_BO, &label);
+
+        ralloc_free(name);
+}
+
 static void
 vc4_bo_dump_stats(struct vc4_screen *screen)
 {
@@ -97,7 +123,7 @@ vc4_bo_from_cache(struct vc4_screen *screen, uint32_t size, const char *name)
                 return NULL;
 
         struct vc4_bo *bo = NULL;
-        pipe_mutex_lock(cache->lock);
+        mtx_lock(&cache->lock);
         if (!list_empty(&cache->size_list[page_index])) {
                 bo = LIST_ENTRY(struct vc4_bo, cache->size_list[page_index].next,
                                 size_list);
@@ -107,22 +133,25 @@ vc4_bo_from_cache(struct vc4_screen *screen, uint32_t size, const char *name)
                  * user will proceed to CPU map it and fill it with stuff.
                  */
                 if (!vc4_bo_wait(bo, 0, NULL)) {
-                        pipe_mutex_unlock(cache->lock);
+                        mtx_unlock(&cache->lock);
                         return NULL;
                 }
 
                 pipe_reference_init(&bo->reference, 1);
                 vc4_bo_remove_from_cache(cache, bo);
 
+                vc4_bo_label(screen, bo, "%s", name);
                 bo->name = name;
         }
-        pipe_mutex_unlock(cache->lock);
+        mtx_unlock(&cache->lock);
         return bo;
 }
 
 struct vc4_bo *
 vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
 {
+        bool cleared_and_retried = false;
+        struct drm_vc4_create_bo create;
         struct vc4_bo *bo;
         int ret;
 
@@ -149,12 +178,8 @@ vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
         bo->private = true;
 
  retry:
-        ;
-
-        bool cleared_and_retried = false;
-        struct drm_vc4_create_bo create = {
-                .size = size
-        };
+        memset(&create, 0, sizeof(create));
+        create.size = size;
 
         ret = vc4_ioctl(screen->fd, DRM_IOCTL_VC4_CREATE_BO, &create);
         bo->handle = create.handle;
@@ -178,6 +203,8 @@ vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
                 vc4_bo_dump_stats(screen);
         }
 
+        vc4_bo_label(screen, bo, "%s", name);
+
         return bo;
 }
 
@@ -188,9 +215,9 @@ vc4_bo_last_unreference(struct vc4_bo *bo)
 
         struct timespec time;
         clock_gettime(CLOCK_MONOTONIC, &time);
-        pipe_mutex_lock(screen->bo_cache.lock);
+        mtx_lock(&screen->bo_cache.lock);
         vc4_bo_last_unreference_locked_timed(bo, time.tv_sec);
-        pipe_mutex_unlock(screen->bo_cache.lock);
+        mtx_unlock(&screen->bo_cache.lock);
 }
 
 static void
@@ -261,13 +288,13 @@ free_stale_bos(struct vc4_screen *screen, time_t time)
 static void
 vc4_bo_cache_free_all(struct vc4_bo_cache *cache)
 {
-        pipe_mutex_lock(cache->lock);
+        mtx_lock(&cache->lock);
         list_for_each_entry_safe(struct vc4_bo, bo, &cache->time_list,
                                  time_list) {
                 vc4_bo_remove_from_cache(cache, bo);
                 vc4_bo_free(bo);
         }
-        pipe_mutex_unlock(cache->lock);
+        mtx_unlock(&cache->lock);
 }
 
 void
@@ -289,17 +316,8 @@ vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time)
                 /* Move old list contents over (since the array has moved, and
                  * therefore the pointers to the list heads have to change).
                  */
-                for (int i = 0; i < cache->size_list_size; i++) {
-                        struct list_head *old_head = &cache->size_list[i];
-                        if (list_empty(old_head))
-                                list_inithead(&new_list[i]);
-                        else {
-                                new_list[i].next = old_head->next;
-                                new_list[i].prev = old_head->prev;
-                                new_list[i].next->prev = &new_list[i];
-                                new_list[i].prev->next = &new_list[i];
-                        }
-                }
+                for (int i = 0; i < cache->size_list_size; i++)
+                        list_replace(&cache->size_list[i], &new_list[i]);
                 for (int i = cache->size_list_size; i < page_index + 1; i++)
                         list_inithead(&new_list[i]);
 
@@ -318,6 +336,7 @@ vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time)
                 vc4_bo_dump_stats(screen);
         }
         bo->name = NULL;
+        vc4_bo_label(screen, bo, "mesa cache");
 
         free_stale_bos(screen, time);
 }
@@ -331,7 +350,7 @@ vc4_bo_open_handle(struct vc4_screen *screen,
 
         assert(size);
 
-        pipe_mutex_lock(screen->bo_handles_mutex);
+        mtx_lock(&screen->bo_handles_mutex);
 
         bo = util_hash_table_get(screen->bo_handles, (void*)(uintptr_t)handle);
         if (bo) {
@@ -356,7 +375,7 @@ vc4_bo_open_handle(struct vc4_screen *screen,
         util_hash_table_set(screen->bo_handles, (void *)(uintptr_t)handle, bo);
 
 done:
-        pipe_mutex_unlock(screen->bo_handles_mutex);
+        mtx_unlock(&screen->bo_handles_mutex);
         return bo;
 }
 
@@ -410,10 +429,10 @@ vc4_bo_get_dmabuf(struct vc4_bo *bo)
                 return -1;
         }
 
-        pipe_mutex_lock(bo->screen->bo_handles_mutex);
+        mtx_lock(&bo->screen->bo_handles_mutex);
         bo->private = false;
         util_hash_table_set(bo->screen->bo_handles, (void *)(uintptr_t)bo->handle, bo);
-        pipe_mutex_unlock(bo->screen->bo_handles_mutex);
+        mtx_unlock(&bo->screen->bo_handles_mutex);
 
         return fd;
 }