v3d: Dump the contents off all the buffers in CLIF mode.
[mesa.git] / src / broadcom / clif / clif_dump.c
1 /*
2 * Copyright © 2016 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "drm-uapi/v3d_drm.h"
28 #include "clif_dump.h"
29 #include "clif_private.h"
30 #include "util/list.h"
31 #include "util/ralloc.h"
32
33 #include "broadcom/cle/v3d_decoder.h"
34
35 struct reloc_worklist_entry *
36 clif_dump_add_address_to_worklist(struct clif_dump *clif,
37 enum reloc_worklist_type type,
38 uint32_t addr)
39 {
40 struct reloc_worklist_entry *entry =
41 rzalloc(clif, struct reloc_worklist_entry);
42 if (!entry)
43 return NULL;
44
45 entry->type = type;
46 entry->addr = addr;
47
48 list_addtail(&entry->link, &clif->worklist);
49
50 return entry;
51 }
52
53 struct clif_dump *
54 clif_dump_init(const struct v3d_device_info *devinfo,
55 FILE *out, bool pretty)
56 {
57 struct clif_dump *clif = rzalloc(NULL, struct clif_dump);
58
59 clif->devinfo = devinfo;
60 clif->out = out;
61 clif->spec = v3d_spec_load(devinfo);
62 clif->pretty = pretty;
63
64 list_inithead(&clif->worklist);
65
66 return clif;
67 }
68
69 void
70 clif_dump_destroy(struct clif_dump *clif)
71 {
72 ralloc_free(clif);
73 }
74
75 struct clif_bo *
76 clif_lookup_bo(struct clif_dump *clif, uint32_t addr)
77 {
78 for (int i = 0; i < clif->bo_count; i++) {
79 struct clif_bo *bo = &clif->bo[i];
80
81 if (addr >= bo->offset &&
82 addr < bo->offset + bo->size) {
83 return bo;
84 }
85 }
86
87 return NULL;
88 }
89
90 static bool
91 clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)
92 {
93 struct clif_bo *bo = clif_lookup_bo(clif, addr);
94 if (!bo)
95 return false;
96
97 *vaddr = bo->vaddr + addr - bo->offset;
98 return true;
99 }
100
101 #define out_uint(_clif, field) out(_clif, " /* %s = */ %u\n", \
102 #field, values-> field);
103
104 static bool
105 clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
106 uint32_t *size, bool reloc_mode)
107 {
108 if (clif->devinfo->ver >= 41)
109 return v3d41_clif_dump_packet(clif, offset, cl, size, reloc_mode);
110 else
111 return v3d33_clif_dump_packet(clif, offset, cl, size, reloc_mode);
112 }
113
114 static uint32_t
115 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end,
116 bool reloc_mode)
117 {
118 struct clif_bo *bo = clif_lookup_bo(clif, start);
119 if (!bo) {
120 out(clif, "Failed to look up address 0x%08x\n",
121 start);
122 return 0;
123 }
124
125 void *start_vaddr = bo->vaddr + start - bo->offset;
126
127 /* The end address is optional (for example, a BRANCH instruction
128 * won't set an end), but is used for BCL/RCL termination.
129 */
130 void *end_vaddr = NULL;
131 if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
132 out(clif, "Failed to look up address 0x%08x\n",
133 end);
134 return 0;
135 }
136
137 if (!reloc_mode)
138 out(clif, "@format ctrllist /* [%s+0x%08x] */\n",
139 bo->name, start - bo->offset);
140
141 uint32_t size;
142 uint8_t *cl = start_vaddr;
143 while (clif_dump_packet(clif, start, cl, &size, reloc_mode)) {
144 cl += size;
145 start += size;
146
147 if (cl == end_vaddr)
148 break;
149 }
150
151 return (void *)cl - bo->vaddr;
152 }
153
154 /* Walks the worklist, parsing the relocs for any memory regions that might
155 * themselves have additional relocations.
156 */
157 static uint32_t
158 clif_dump_gl_shader_state_record(struct clif_dump *clif,
159 struct reloc_worklist_entry *reloc,
160 void *vaddr)
161 {
162 struct v3d_group *state = v3d_spec_find_struct(clif->spec,
163 "GL Shader State Record");
164 struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
165 "GL Shader State Attribute Record");
166 assert(state);
167 assert(attr);
168 uint32_t offset = 0;
169
170 out(clif, "@format shadrec_gl_main\n");
171 v3d_print_group(clif, state, 0, vaddr + offset);
172 offset += v3d_group_get_length(state);
173
174 for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
175 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
176 v3d_print_group(clif, attr, 0, vaddr + offset);
177 offset += v3d_group_get_length(attr);
178 }
179
180 return offset;
181 }
182
183 static void
184 clif_process_worklist(struct clif_dump *clif)
185 {
186 list_for_each_entry_safe(struct reloc_worklist_entry, reloc,
187 &clif->worklist, link) {
188 void *vaddr;
189 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
190 out(clif, "Failed to look up address 0x%08x\n",
191 reloc->addr);
192 continue;
193 }
194
195 switch (reloc->type) {
196 case reloc_cl:
197 clif_dump_cl(clif, reloc->addr, reloc->cl.end, true);
198 break;
199
200 case reloc_gl_shader_state:
201 break;
202 case reloc_generic_tile_list:
203 clif_dump_cl(clif, reloc->addr,
204 reloc->generic_tile_list.end, true);
205 break;
206 }
207 }
208 }
209
210 static int
211 worklist_entry_compare(const void *a, const void *b)
212 {
213 return ((*(struct reloc_worklist_entry **)a)->addr -
214 (*(struct reloc_worklist_entry **)b)->addr);
215 }
216
217 static bool
218 clif_dump_if_blank(struct clif_dump *clif, struct clif_bo *bo,
219 uint32_t start, uint32_t end)
220 {
221 for (int i = start; i < end; i++) {
222 if (((uint8_t *)bo->vaddr)[i] != 0)
223 return false;
224 }
225
226 out(clif, "\n");
227 out(clif, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end - start,
228 bo->name, start, end - 1);
229 return true;
230 }
231
232 /* Dumps the binary data in the BO from start to end (relative to the start of
233 * the BO).
234 */
235 static void
236 clif_dump_binary(struct clif_dump *clif, struct clif_bo *bo,
237 uint32_t start, uint32_t end)
238 {
239 if (start == end)
240 return;
241
242 if (clif_dump_if_blank(clif, bo, start, end))
243 return;
244
245 out(clif, "@format binary /* [%s+0x%08x] */\n",
246 bo->name, start);
247
248 uint32_t offset = start;
249 int dumped_in_line = 0;
250 while (offset < end) {
251 if (clif_dump_if_blank(clif, bo, offset, end))
252 return;
253
254 if (end - offset >= 4) {
255 out(clif, "0x%08x ", *(uint32_t *)(bo->vaddr + offset));
256 offset += 4;
257 } else {
258 out(clif, "0x%02x ", *(uint8_t *)(bo->vaddr + offset));
259 offset++;
260 }
261
262 if (++dumped_in_line == 8) {
263 out(clif, "\n");
264 dumped_in_line = 0;
265 }
266 }
267 if (dumped_in_line)
268 out(clif, "\n");
269 }
270
271 /* Walks the list of relocations, dumping each buffer's contents (using our
272 * codegenned dump routines for pretty printing, and most importantly proper
273 * address references so that the CLIF parser can relocate buffers).
274 */
275 static void
276 clif_dump_buffers(struct clif_dump *clif)
277 {
278 int num_relocs = 0;
279 list_for_each_entry(struct reloc_worklist_entry, reloc,
280 &clif->worklist, link) {
281 num_relocs++;
282 }
283 struct reloc_worklist_entry **relocs =
284 ralloc_array(clif, struct reloc_worklist_entry *, num_relocs);
285 int i = 0;
286 list_for_each_entry(struct reloc_worklist_entry, reloc,
287 &clif->worklist, link) {
288 relocs[i++] = reloc;
289 }
290 qsort(relocs, num_relocs, sizeof(*relocs), worklist_entry_compare);
291
292 struct clif_bo *bo = NULL;
293 uint32_t offset = 0;
294
295 for (i = 0; i < num_relocs; i++) {
296 struct reloc_worklist_entry *reloc = relocs[i];
297 struct clif_bo *new_bo = clif_lookup_bo(clif, reloc->addr);
298
299 if (!new_bo) {
300 out(clif, "Failed to look up address 0x%08x\n",
301 reloc->addr);
302 continue;
303 }
304
305 if (new_bo != bo) {
306 if (bo) {
307 /* Finish out the last of the last BO. */
308 clif_dump_binary(clif, bo,
309 offset,
310 bo->size);
311 }
312
313 out(clif, "\n");
314 out(clif, "@buffer %s\n", new_bo->name);
315 bo = new_bo;
316 offset = 0;
317 bo->dumped = true;
318 }
319
320 int reloc_offset = reloc->addr - bo->offset;
321 if (offset != reloc_offset)
322 clif_dump_binary(clif, bo, offset, reloc_offset);
323 offset = reloc_offset;
324
325 switch (reloc->type) {
326 case reloc_cl:
327 offset = clif_dump_cl(clif, reloc->addr, reloc->cl.end,
328 false);
329 out(clif, "\n");
330 break;
331
332 case reloc_gl_shader_state:
333 offset += clif_dump_gl_shader_state_record(clif,
334 reloc,
335 bo->vaddr +
336 offset);
337 break;
338 case reloc_generic_tile_list:
339 offset = clif_dump_cl(clif, reloc->addr,
340 reloc->generic_tile_list.end,
341 false);
342 break;
343 }
344 out(clif, "\n");
345 }
346
347 if (bo) {
348 clif_dump_binary(clif, bo, offset, bo->size);
349 }
350
351 /* For any BOs that didn't have relocations, just dump them raw. */
352 for (int i = 0; i < clif->bo_count; i++) {
353 bo = &clif->bo[i];
354 if (bo->dumped)
355 continue;
356 out(clif, "@buffer %s\n", bo->name);
357 clif_dump_binary(clif, bo, 0, bo->size);
358 out(clif, "\n");
359 }
360 }
361
362 void
363 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
364 {
365 struct reloc_worklist_entry *entry =
366 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
367
368 entry->cl.end = end;
369 }
370
371 static int
372 clif_bo_offset_compare(const void *a, const void *b)
373 {
374 return ((struct clif_bo *)a)->offset - ((struct clif_bo *)b)->offset;
375 }
376
377 void
378 clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)
379 {
380 clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);
381 clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);
382
383 qsort(clif->bo, clif->bo_count, sizeof(clif->bo[0]),
384 clif_bo_offset_compare);
385
386 /* A buffer needs to be defined before we can emit a CLIF address
387 * referencing it, so emit them all now.
388 */
389 for (int i = 0; i < clif->bo_count; i++) {
390 out(clif, "@createbuf_aligned 4096 %s\n", clif->bo[i].name);
391 }
392
393 /* Walk the worklist figuring out the locations of structs based on
394 * the CL contents.
395 */
396 clif_process_worklist(clif);
397
398 /* Dump the contents of the buffers using the relocations we found to
399 * pretty-print structures.
400 */
401 clif_dump_buffers(clif);
402
403 out(clif, "@add_bin 0\n ");
404 out_address(clif, submit->bcl_start);
405 out(clif, "\n ");
406 out_address(clif, submit->bcl_end);
407 out(clif, "\n ");
408 out_address(clif, submit->qma);
409 out(clif, "\n %d\n ", submit->qms);
410 out_address(clif, submit->qts);
411 out(clif, "\n");
412 out(clif, "@wait_bin_all_cores\n");
413
414 out(clif, "@add_render 0\n ");
415 out_address(clif, submit->rcl_start);
416 out(clif, "\n ");
417 out_address(clif, submit->rcl_end);
418 out(clif, "\n ");
419 out_address(clif, submit->qma);
420 out(clif, "\n");
421 out(clif, "@wait_render_all_cores\n");
422 }
423
424 void
425 clif_dump_add_bo(struct clif_dump *clif, const char *name,
426 uint32_t offset, uint32_t size, void *vaddr)
427 {
428 if (clif->bo_count >= clif->bo_array_size) {
429 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
430 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
431 clif->bo_array_size);
432 }
433
434 /* CLIF relocs use the buffer name, so make sure they're unique. */
435 for (int i = 0; i < clif->bo_count; i++)
436 assert(strcmp(clif->bo[i].name, name) != 0);
437
438 clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
439 clif->bo[clif->bo_count].offset = offset;
440 clif->bo[clif->bo_count].size = size;
441 clif->bo[clif->bo_count].vaddr = vaddr;
442 clif->bo[clif->bo_count].dumped = false;
443 clif->bo_count++;
444 }