pan/decode: Use a page table for tracking mmaps
[mesa.git] / src / panfrost / pandecode / common.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2017-2018 Lyude Paul
4 * Copyright (C) 2019 Collabora, Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <stdint.h>
30 #include <string.h>
31
32 #include "decode.h"
33 #include "util/macros.h"
34 #include "util/u_debug.h"
35 #include "util/hash_table.h"
36
37 /* Memory handling */
38
39 static struct hash_table_u64 *mmap_table;
40
41 struct pandecode_mapped_memory *
42 pandecode_find_mapped_gpu_mem_containing(uint64_t addr)
43 {
44 return _mesa_hash_table_u64_search(mmap_table, addr & ~(4096 - 1));
45 }
46
47 static void
48 pandecode_add_name(struct pandecode_mapped_memory *mem, uint64_t gpu_va, const char *name)
49 {
50 if (!name) {
51 /* If we don't have a name, assign one */
52
53 snprintf(mem->name, ARRAY_SIZE(mem->name) - 1,
54 "memory_%" PRIx64, gpu_va);
55 } else {
56 assert((strlen(name) + 1) < ARRAY_SIZE(mem->name));
57 memcpy(mem->name, name, strlen(name) + 1);
58 }
59 }
60
61 void
62 pandecode_inject_mmap(uint64_t gpu_va, void *cpu, unsigned sz, const char *name)
63 {
64 /* First, search if we already mapped this and are just updating an address */
65
66 struct pandecode_mapped_memory *existing =
67 pandecode_find_mapped_gpu_mem_containing(gpu_va);
68
69 if (existing && existing->gpu_va == gpu_va) {
70 existing->length = sz;
71 existing->addr = cpu;
72 pandecode_add_name(existing, gpu_va, name);
73 return;
74 }
75
76 /* Otherwise, add a fresh mapping */
77 struct pandecode_mapped_memory *mapped_mem = NULL;
78
79 mapped_mem = malloc(sizeof(*mapped_mem));
80 mapped_mem->gpu_va = gpu_va;
81 mapped_mem->length = sz;
82 mapped_mem->addr = cpu;
83 pandecode_add_name(mapped_mem, gpu_va, name);
84
85 /* Add it to the table */
86 assert((gpu_va & 4095) == 0);
87
88 for (unsigned i = 0; i < sz; i += 4096)
89 _mesa_hash_table_u64_insert(mmap_table, gpu_va + i, mapped_mem);
90 }
91
92 char *
93 pointer_as_memory_reference(uint64_t ptr)
94 {
95 struct pandecode_mapped_memory *mapped;
96 char *out = malloc(128);
97
98 /* Try to find the corresponding mapped zone */
99
100 mapped = pandecode_find_mapped_gpu_mem_containing(ptr);
101
102 if (mapped) {
103 snprintf(out, 128, "%s + %d", mapped->name, (int) (ptr - mapped->gpu_va));
104 return out;
105 }
106
107 /* Just use the raw address if other options are exhausted */
108
109 snprintf(out, 128, "0x%" PRIx64, ptr);
110 return out;
111
112 }
113
114 static int pandecode_dump_frame_count = 0;
115
116 static void
117 pandecode_dump_file_open(bool force_stderr)
118 {
119 if (pandecode_dump_stream)
120 return;
121
122 /* This does a getenv every frame, so it is possible to use
123 * setenv to change the base at runtime.
124 */
125 const char *dump_file_base = debug_get_option("PANDECODE_DUMP_FILE", "pandecode.dump");
126 if (force_stderr || !strcmp(dump_file_base, "stderr"))
127 pandecode_dump_stream = stderr;
128 else {
129 char buffer[1024];
130 snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base, pandecode_dump_frame_count);
131 printf("pandecode: dump command stream to file %s\n", buffer);
132 pandecode_dump_stream = fopen(buffer, "w");
133 if (!pandecode_dump_stream)
134 fprintf(stderr,
135 "pandecode: failed to open command stream log file %s\n",
136 buffer);
137 }
138 }
139
140 static void
141 pandecode_dump_file_close(void)
142 {
143 if (pandecode_dump_stream && pandecode_dump_stream != stderr) {
144 fclose(pandecode_dump_stream);
145 pandecode_dump_stream = NULL;
146 }
147 }
148
149 void
150 pandecode_initialize(bool to_stderr)
151 {
152 mmap_table = _mesa_hash_table_u64_create(NULL);
153 pandecode_dump_file_open(to_stderr);
154 }
155
156 void
157 pandecode_next_frame(void)
158 {
159 pandecode_dump_file_close();
160 pandecode_dump_frame_count++;
161 pandecode_dump_file_open(false);
162 }
163
164 void
165 pandecode_close(void)
166 {
167 _mesa_hash_table_u64_destroy(mmap_table, NULL);
168 pandecode_dump_file_close();
169 }