panfrost: Identify layer_stride
[mesa.git] / src / panfrost / pandecode / common.c
index 2f2249efe955e82e11c32597988ba4288eb6d2fc..65890e44955a11ec2d80574a0f33ffe2aec90a7c 100644 (file)
 #include "decode.h"
 #include "util/macros.h"
 #include "util/u_debug.h"
+#include "util/hash_table.h"
 
 /* Memory handling */
 
-static struct pandecode_mapped_memory mmaps;
+static struct hash_table_u64 *mmap_table;
 
 struct pandecode_mapped_memory *
 pandecode_find_mapped_gpu_mem_containing(uint64_t addr)
 {
-        list_for_each_entry(struct pandecode_mapped_memory, pos, &mmaps.node, node) {
-                if (addr >= pos->gpu_va && addr < pos->gpu_va + pos->length)
-                        return pos;
-        }
-
-        return NULL;
+        return _mesa_hash_table_u64_search(mmap_table, addr & ~(4096 - 1));
 }
 
 static void
@@ -67,31 +63,30 @@ pandecode_inject_mmap(uint64_t gpu_va, void *cpu, unsigned sz, const char *name)
 {
         /* First, search if we already mapped this and are just updating an address */
 
-        list_for_each_entry(struct pandecode_mapped_memory, pos, &mmaps.node, node) {
-                if (pos->gpu_va == gpu_va) {
-                        /* TODO: Resizing weirdness. Only applies to tracing
-                         * the legacy driver, not for native traces */
+        struct pandecode_mapped_memory *existing =
+                pandecode_find_mapped_gpu_mem_containing(gpu_va);
 
-                        pos->length = sz;
-                        pos->addr = cpu;
-                        pandecode_add_name(pos, gpu_va, name);
-
-                        return;
-                }
+        if (existing && existing->gpu_va == gpu_va) {
+                existing->length = sz;
+                existing->addr = cpu;
+                pandecode_add_name(existing, gpu_va, name);
+                return;
         }
 
         /* Otherwise, add a fresh mapping */
         struct pandecode_mapped_memory *mapped_mem = NULL;
 
         mapped_mem = malloc(sizeof(*mapped_mem));
-        list_inithead(&mapped_mem->node);
-
         mapped_mem->gpu_va = gpu_va;
         mapped_mem->length = sz;
         mapped_mem->addr = cpu;
         pandecode_add_name(mapped_mem, gpu_va, name);
 
-        list_add(&mapped_mem->node, &mmaps.node);
+        /* Add it to the table */
+        assert((gpu_va & 4095) == 0);
+
+        for (unsigned i = 0; i < sz; i += 4096)
+                _mesa_hash_table_u64_insert(mmap_table, gpu_va + i, mapped_mem);
 }
 
 char *
@@ -119,31 +114,33 @@ pointer_as_memory_reference(uint64_t ptr)
 static int pandecode_dump_frame_count = 0;
 
 static void
-pandecode_dump_file_open(void)
+pandecode_dump_file_open(bool force_stderr)
 {
         if (pandecode_dump_stream)
                 return;
 
-        char buffer[1024];
-
         /* This does a getenv every frame, so it is possible to use
          * setenv to change the base at runtime.
          */
         const char *dump_file_base = debug_get_option("PANDECODE_DUMP_FILE", "pandecode.dump");
-        snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base, pandecode_dump_frame_count);
-
-        printf("pandecode: dump command stream to file %s\n", buffer);
-        pandecode_dump_stream = fopen(buffer, "w");
-
-        if (!pandecode_dump_stream)
-                fprintf(stderr,"pandecode: failed to open command stream log file %s\n",
-                        buffer);
+        if (force_stderr || !strcmp(dump_file_base, "stderr"))
+                pandecode_dump_stream = stderr;
+        else {
+                char buffer[1024];
+                snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base, pandecode_dump_frame_count);
+                printf("pandecode: dump command stream to file %s\n", buffer);
+                pandecode_dump_stream = fopen(buffer, "w");
+                if (!pandecode_dump_stream)
+                        fprintf(stderr,
+                                "pandecode: failed to open command stream log file %s\n",
+                                buffer);
+        }
 }
 
 static void
 pandecode_dump_file_close(void)
 {
-        if (pandecode_dump_stream) {
+        if (pandecode_dump_stream && pandecode_dump_stream != stderr) {
                 fclose(pandecode_dump_stream);
                 pandecode_dump_stream = NULL;
         }
@@ -152,12 +149,8 @@ pandecode_dump_file_close(void)
 void
 pandecode_initialize(bool to_stderr)
 {
-        list_inithead(&mmaps.node);
-
-        if (to_stderr)
-                pandecode_dump_stream = stderr;
-        else
-                pandecode_dump_file_open();
+        mmap_table = _mesa_hash_table_u64_create(NULL);
+        pandecode_dump_file_open(to_stderr);
 }
 
 void
@@ -165,11 +158,12 @@ pandecode_next_frame(void)
 {
         pandecode_dump_file_close();
         pandecode_dump_frame_count++;
-        pandecode_dump_file_open();
+        pandecode_dump_file_open(false);
 }
 
 void
 pandecode_close(void)
 {
+        _mesa_hash_table_u64_destroy(mmap_table, NULL);
         pandecode_dump_file_close();
 }