intel: aubinator_viewer: store urb state during decoding
[mesa.git] / src / intel / tools / aubinator_viewer_decoder.cpp
1 /*
2 * Copyright © 2017 Intel Corporation
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 <string.h>
25
26 #include "util/macros.h"
27
28 #include "aubinator_viewer.h"
29
30 void
31 aub_viewer_decode_ctx_init(struct aub_viewer_decode_ctx *ctx,
32 struct aub_viewer_cfg *cfg,
33 struct aub_viewer_decode_cfg *decode_cfg,
34 struct gen_spec *spec,
35 struct gen_disasm *disasm,
36 struct gen_batch_decode_bo (*get_bo)(void *, uint64_t),
37 unsigned (*get_state_size)(void *, uint32_t),
38 void *user_data)
39 {
40 memset(ctx, 0, sizeof(*ctx));
41
42 ctx->get_bo = get_bo;
43 ctx->get_state_size = get_state_size;
44 ctx->user_data = user_data;
45
46 ctx->cfg = cfg;
47 ctx->decode_cfg = decode_cfg;
48 ctx->spec = spec;
49 ctx->disasm = disasm;
50 }
51
52 static void
53 aub_viewer_print_group(struct aub_viewer_decode_ctx *ctx,
54 struct gen_group *group,
55 uint64_t address, const void *map)
56 {
57 struct gen_field_iterator iter;
58 int last_dword = -1;
59 const uint32_t *p = (const uint32_t *) map;
60
61 gen_field_iterator_init(&iter, group, p, 0, false);
62 while (gen_field_iterator_next(&iter)) {
63 if (ctx->decode_cfg->show_dwords) {
64 int iter_dword = iter.end_bit / 32;
65 if (last_dword != iter_dword) {
66 for (int i = last_dword + 1; i <= iter_dword; i++) {
67 ImGui::TextColored(ctx->cfg->dwords_color,
68 "0x%08" PRIx64 ": 0x%08x : Dword %d",
69 address + 4 * i, iter.p[i], i);
70 }
71 last_dword = iter_dword;
72 }
73 }
74 if (!gen_field_is_header(iter.field)) {
75 if (ctx->decode_cfg->field_filter.PassFilter(iter.name)) {
76 ImGui::Text("%s: %s", iter.name, iter.value);
77 if (iter.struct_desc) {
78 int struct_dword = iter.start_bit / 32;
79 uint64_t struct_address = address + 4 * struct_dword;
80 aub_viewer_print_group(ctx, iter.struct_desc, struct_address,
81 &p[struct_dword]);
82 }
83 }
84 }
85 }
86 }
87
88 static struct gen_batch_decode_bo
89 ctx_get_bo(struct aub_viewer_decode_ctx *ctx, uint64_t addr)
90 {
91 if (gen_spec_get_gen(ctx->spec) >= gen_make_gen(8,0)) {
92 /* On Broadwell and above, we have 48-bit addresses which consume two
93 * dwords. Some packets require that these get stored in a "canonical
94 * form" which means that bit 47 is sign-extended through the upper
95 * bits. In order to correctly handle those aub dumps, we need to mask
96 * off the top 16 bits.
97 */
98 addr &= (~0ull >> 16);
99 }
100
101 struct gen_batch_decode_bo bo = ctx->get_bo(ctx->user_data, addr);
102
103 if (gen_spec_get_gen(ctx->spec) >= gen_make_gen(8,0))
104 bo.addr &= (~0ull >> 16);
105
106 /* We may actually have an offset into the bo */
107 if (bo.map != NULL) {
108 assert(bo.addr <= addr);
109 uint64_t offset = addr - bo.addr;
110 bo.map = (const uint8_t *)bo.map + offset;
111 bo.addr += offset;
112 bo.size -= offset;
113 }
114
115 return bo;
116 }
117
118 static int
119 update_count(struct aub_viewer_decode_ctx *ctx,
120 uint32_t offset_from_dsba,
121 unsigned element_dwords,
122 unsigned guess)
123 {
124 unsigned size = 0;
125
126 if (ctx->get_state_size)
127 size = ctx->get_state_size(ctx->user_data, offset_from_dsba);
128
129 if (size > 0)
130 return size / (sizeof(uint32_t) * element_dwords);
131
132 /* In the absence of any information, just guess arbitrarily. */
133 return guess;
134 }
135
136 static void
137 ctx_disassemble_program(struct aub_viewer_decode_ctx *ctx,
138 uint32_t ksp, const char *type)
139 {
140 uint64_t addr = ctx->instruction_base + ksp;
141 struct gen_batch_decode_bo bo = ctx_get_bo(ctx, addr);
142 if (!bo.map) {
143 ImGui::TextColored(ctx->cfg->missing_color, "Shader unavailable");
144 return;
145 }
146
147 ImGui::PushID(addr);
148 if (ImGui::Button(type) && ctx->display_shader)
149 ctx->display_shader(ctx->user_data, type, addr);
150 ImGui::PopID();
151 }
152
153 static void
154 handle_state_base_address(struct aub_viewer_decode_ctx *ctx,
155 struct gen_group *inst,
156 const uint32_t *p)
157 {
158 struct gen_field_iterator iter;
159 gen_field_iterator_init(&iter, inst, p, 0, false);
160
161 while (gen_field_iterator_next(&iter)) {
162 if (strcmp(iter.name, "Surface State Base Address") == 0) {
163 ctx->surface_base = iter.raw_value;
164 } else if (strcmp(iter.name, "Dynamic State Base Address") == 0) {
165 ctx->dynamic_base = iter.raw_value;
166 } else if (strcmp(iter.name, "Instruction Base Address") == 0) {
167 ctx->instruction_base = iter.raw_value;
168 }
169 }
170 }
171
172 static void
173 dump_binding_table(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)
174 {
175 struct gen_group *strct =
176 gen_spec_find_struct(ctx->spec, "RENDER_SURFACE_STATE");
177 if (strct == NULL) {
178 ImGui::TextColored(ctx->cfg->missing_color, "did not find RENDER_SURFACE_STATE info");
179 return;
180 }
181
182 if (count < 0)
183 count = update_count(ctx, offset, 1, 8);
184
185 if (offset % 32 != 0 || offset >= UINT16_MAX) {
186 ImGui::TextColored(ctx->cfg->missing_color, "invalid binding table pointer");
187 return;
188 }
189
190 struct gen_batch_decode_bo bind_bo =
191 ctx_get_bo(ctx, ctx->surface_base + offset);
192
193 if (bind_bo.map == NULL) {
194 ImGui::TextColored(ctx->cfg->missing_color,
195 "binding table unavailable addr=0x%08" PRIx64,
196 ctx->surface_base + offset);
197 return;
198 }
199
200 const uint32_t *pointers = (const uint32_t *) bind_bo.map;
201 for (int i = 0; i < count; i++) {
202 if (pointers[i] == 0)
203 continue;
204
205 uint64_t addr = ctx->surface_base + pointers[i];
206 struct gen_batch_decode_bo bo = ctx_get_bo(ctx, addr);
207 uint32_t size = strct->dw_length * 4;
208
209 if (pointers[i] % 32 != 0 ||
210 addr < bo.addr || addr + size >= bo.addr + bo.size) {
211 ImGui::TextColored(ctx->cfg->missing_color,
212 "pointer %u: %08x <not valid>", i, pointers[i]);
213 continue;
214 }
215
216 ImGui::Text("pointer %u: %08x", i, pointers[i]);
217 aub_viewer_print_group(ctx, strct, addr, (const uint8_t *) bo.map + (addr - bo.addr));
218 }
219 }
220
221 static void
222 dump_samplers(struct aub_viewer_decode_ctx *ctx, uint32_t offset, int count)
223 {
224 struct gen_group *strct = gen_spec_find_struct(ctx->spec, "SAMPLER_STATE");
225
226 if (count < 0)
227 count = update_count(ctx, offset, strct->dw_length, 4);
228
229 uint64_t state_addr = ctx->dynamic_base + offset;
230 struct gen_batch_decode_bo bo = ctx_get_bo(ctx, state_addr);
231 const uint8_t *state_map = (const uint8_t *) bo.map;
232
233 if (state_map == NULL) {
234 ImGui::TextColored(ctx->cfg->missing_color,
235 "samplers unavailable addr=0x%08" PRIx64, state_addr);
236 return;
237 }
238
239 if (offset % 32 != 0 || state_addr - bo.addr >= bo.size) {
240 ImGui::TextColored(ctx->cfg->missing_color, "invalid sampler state pointer");
241 return;
242 }
243
244 for (int i = 0; i < count; i++) {
245 ImGui::Text("sampler state %d", i);
246 aub_viewer_print_group(ctx, strct, state_addr, state_map);
247 state_addr += 16;
248 state_map += 16;
249 }
250 }
251
252 static void
253 handle_media_interface_descriptor_load(struct aub_viewer_decode_ctx *ctx,
254 struct gen_group *inst,
255 const uint32_t *p)
256 {
257 struct gen_group *desc =
258 gen_spec_find_struct(ctx->spec, "INTERFACE_DESCRIPTOR_DATA");
259
260 struct gen_field_iterator iter;
261 gen_field_iterator_init(&iter, inst, p, 0, false);
262 uint32_t descriptor_offset = 0;
263 int descriptor_count = 0;
264 while (gen_field_iterator_next(&iter)) {
265 if (strcmp(iter.name, "Interface Descriptor Data Start Address") == 0) {
266 descriptor_offset = strtol(iter.value, NULL, 16);
267 } else if (strcmp(iter.name, "Interface Descriptor Total Length") == 0) {
268 descriptor_count =
269 strtol(iter.value, NULL, 16) / (desc->dw_length * 4);
270 }
271 }
272
273 uint64_t desc_addr = ctx->dynamic_base + descriptor_offset;
274 struct gen_batch_decode_bo bo = ctx_get_bo(ctx, desc_addr);
275 const uint32_t *desc_map = (const uint32_t *) bo.map;
276
277 if (desc_map == NULL) {
278 ImGui::TextColored(ctx->cfg->missing_color,
279 "interface descriptors unavailable addr=0x%08" PRIx64, desc_addr);
280 return;
281 }
282
283 for (int i = 0; i < descriptor_count; i++) {
284 ImGui::Text("descriptor %d: %08x", i, descriptor_offset);
285
286 aub_viewer_print_group(ctx, desc, desc_addr, desc_map);
287
288 gen_field_iterator_init(&iter, desc, desc_map, 0, false);
289 uint64_t ksp = 0;
290 uint32_t sampler_offset = 0, sampler_count = 0;
291 uint32_t binding_table_offset = 0, binding_entry_count = 0;
292 while (gen_field_iterator_next(&iter)) {
293 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
294 ksp = strtoll(iter.value, NULL, 16);
295 } else if (strcmp(iter.name, "Sampler State Pointer") == 0) {
296 sampler_offset = strtol(iter.value, NULL, 16);
297 } else if (strcmp(iter.name, "Sampler Count") == 0) {
298 sampler_count = strtol(iter.value, NULL, 10);
299 } else if (strcmp(iter.name, "Binding Table Pointer") == 0) {
300 binding_table_offset = strtol(iter.value, NULL, 16);
301 } else if (strcmp(iter.name, "Binding Table Entry Count") == 0) {
302 binding_entry_count = strtol(iter.value, NULL, 10);
303 }
304 }
305
306 ctx_disassemble_program(ctx, ksp, "compute shader");
307
308 dump_samplers(ctx, sampler_offset, sampler_count);
309 dump_binding_table(ctx, binding_table_offset, binding_entry_count);
310
311 desc_map += desc->dw_length;
312 desc_addr += desc->dw_length * 4;
313 }
314 }
315
316 static void
317 handle_3dstate_vertex_buffers(struct aub_viewer_decode_ctx *ctx,
318 struct gen_group *inst,
319 const uint32_t *p)
320 {
321 struct gen_group *vbs = gen_spec_find_struct(ctx->spec, "VERTEX_BUFFER_STATE");
322
323 struct gen_batch_decode_bo vb = {};
324 uint32_t vb_size = 0;
325 int index = -1;
326 int pitch = -1;
327 bool ready = false;
328
329 struct gen_field_iterator iter;
330 gen_field_iterator_init(&iter, inst, p, 0, false);
331 while (gen_field_iterator_next(&iter)) {
332 if (iter.struct_desc != vbs)
333 continue;
334
335 uint64_t buffer_addr;
336
337 struct gen_field_iterator vbs_iter;
338 gen_field_iterator_init(&vbs_iter, vbs, &iter.p[iter.start_bit / 32], 0, false);
339 while (gen_field_iterator_next(&vbs_iter)) {
340 if (strcmp(vbs_iter.name, "Vertex Buffer Index") == 0) {
341 index = vbs_iter.raw_value;
342 } else if (strcmp(vbs_iter.name, "Buffer Pitch") == 0) {
343 pitch = vbs_iter.raw_value;
344 } else if (strcmp(vbs_iter.name, "Buffer Starting Address") == 0) {
345 buffer_addr = vbs_iter.raw_value;
346 vb = ctx_get_bo(ctx, buffer_addr);
347 } else if (strcmp(vbs_iter.name, "Buffer Size") == 0) {
348 vb_size = vbs_iter.raw_value;
349 ready = true;
350 } else if (strcmp(vbs_iter.name, "End Address") == 0) {
351 if (vb.map && vbs_iter.raw_value >= vb.addr)
352 vb_size = vbs_iter.raw_value - vb.addr;
353 else
354 vb_size = 0;
355 ready = true;
356 }
357
358 if (!ready)
359 continue;
360
361 ImGui::Text("vertex buffer %d, size %d", index, vb_size);
362
363 if (vb.map == NULL) {
364 ImGui::TextColored(ctx->cfg->missing_color,
365 "buffer contents unavailable addr=0x%08" PRIx64, buffer_addr);
366 continue;
367 }
368
369 if (vb.map == 0 || vb_size == 0)
370 continue;
371
372 vb.map = NULL;
373 vb_size = 0;
374 index = -1;
375 pitch = -1;
376 ready = false;
377 }
378 }
379 }
380
381 static void
382 handle_3dstate_index_buffer(struct aub_viewer_decode_ctx *ctx,
383 struct gen_group *inst,
384 const uint32_t *p)
385 {
386 struct gen_batch_decode_bo ib = {};
387 uint64_t buffer_addr = 0;
388 uint32_t ib_size = 0;
389 uint32_t format = 0;
390
391 struct gen_field_iterator iter;
392 gen_field_iterator_init(&iter, inst, p, 0, false);
393 while (gen_field_iterator_next(&iter)) {
394 if (strcmp(iter.name, "Index Format") == 0) {
395 format = iter.raw_value;
396 } else if (strcmp(iter.name, "Buffer Starting Address") == 0) {
397 buffer_addr = iter.raw_value;
398 ib = ctx_get_bo(ctx, buffer_addr);
399 } else if (strcmp(iter.name, "Buffer Size") == 0) {
400 ib_size = iter.raw_value;
401 }
402 }
403
404 if (ib.map == NULL) {
405 ImGui::TextColored(ctx->cfg->missing_color,
406 "buffer contents unavailable addr=0x%08" PRIx64,
407 buffer_addr);
408 return;
409 }
410
411 const uint8_t *m = (const uint8_t *) ib.map;
412 const uint8_t *ib_end = m + MIN2(ib.size, ib_size);
413 for (int i = 0; m < ib_end && i < 10; i++) {
414 switch (format) {
415 case 0:
416 m += 1;
417 break;
418 case 1:
419 m += 2;
420 break;
421 case 2:
422 m += 4;
423 break;
424 }
425 }
426 }
427
428 static void
429 decode_single_ksp(struct aub_viewer_decode_ctx *ctx,
430 struct gen_group *inst,
431 const uint32_t *p)
432 {
433 uint64_t ksp = 0;
434 bool is_simd8 = false; /* vertex shaders on Gen8+ only */
435 bool is_enabled = true;
436
437 struct gen_field_iterator iter;
438 gen_field_iterator_init(&iter, inst, p, 0, false);
439 while (gen_field_iterator_next(&iter)) {
440 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
441 ksp = iter.raw_value;
442 } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
443 is_simd8 = iter.raw_value;
444 } else if (strcmp(iter.name, "Dispatch Mode") == 0) {
445 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
446 } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
447 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
448 } else if (strcmp(iter.name, "Enable") == 0) {
449 is_enabled = iter.raw_value;
450 }
451 }
452
453 const char *type =
454 strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :
455 strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :
456 strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :
457 strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
458 strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation evaluation shader" :
459 strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation control shader" :
460 strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
461 strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
462 NULL;
463
464 if (is_enabled)
465 ctx_disassemble_program(ctx, ksp, type);
466 }
467
468 static void
469 decode_ps_kernels(struct aub_viewer_decode_ctx *ctx,
470 struct gen_group *inst,
471 const uint32_t *p)
472 {
473 uint64_t ksp[3] = {0, 0, 0};
474 bool enabled[3] = {false, false, false};
475
476 struct gen_field_iterator iter;
477 gen_field_iterator_init(&iter, inst, p, 0, false);
478 while (gen_field_iterator_next(&iter)) {
479 if (strncmp(iter.name, "Kernel Start Pointer ",
480 strlen("Kernel Start Pointer ")) == 0) {
481 int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
482 ksp[idx] = strtol(iter.value, NULL, 16);
483 } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
484 enabled[0] = strcmp(iter.value, "true") == 0;
485 } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
486 enabled[1] = strcmp(iter.value, "true") == 0;
487 } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
488 enabled[2] = strcmp(iter.value, "true") == 0;
489 }
490 }
491
492 /* Reorder KSPs to be [8, 16, 32] instead of the hardware order. */
493 if (enabled[0] + enabled[1] + enabled[2] == 1) {
494 if (enabled[1]) {
495 ksp[1] = ksp[0];
496 ksp[0] = 0;
497 } else if (enabled[2]) {
498 ksp[2] = ksp[0];
499 ksp[0] = 0;
500 }
501 } else {
502 uint64_t tmp = ksp[1];
503 ksp[1] = ksp[2];
504 ksp[2] = tmp;
505 }
506
507 if (enabled[0])
508 ctx_disassemble_program(ctx, ksp[0], "SIMD8 fragment shader");
509 if (enabled[1])
510 ctx_disassemble_program(ctx, ksp[1], "SIMD16 fragment shader");
511 if (enabled[2])
512 ctx_disassemble_program(ctx, ksp[2], "SIMD32 fragment shader");
513 }
514
515 static void
516 decode_3dstate_constant(struct aub_viewer_decode_ctx *ctx,
517 struct gen_group *inst,
518 const uint32_t *p)
519 {
520 struct gen_group *body =
521 gen_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");
522
523 uint32_t read_length[4] = {0};
524 uint64_t read_addr[4];
525
526 struct gen_field_iterator outer;
527 gen_field_iterator_init(&outer, inst, p, 0, false);
528 while (gen_field_iterator_next(&outer)) {
529 if (outer.struct_desc != body)
530 continue;
531
532 struct gen_field_iterator iter;
533 gen_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
534 0, false);
535
536 while (gen_field_iterator_next(&iter)) {
537 int idx;
538 if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {
539 read_length[idx] = iter.raw_value;
540 } else if (sscanf(iter.name, "Buffer[%d]", &idx) == 1) {
541 read_addr[idx] = iter.raw_value;
542 }
543 }
544
545 for (int i = 0; i < 4; i++) {
546 if (read_length[i] == 0)
547 continue;
548
549 struct gen_batch_decode_bo buffer = ctx_get_bo(ctx, read_addr[i]);
550 if (!buffer.map) {
551 ImGui::TextColored(ctx->cfg->missing_color,
552 "constant buffer %d unavailable addr=0x%08" PRIx64,
553 i, read_addr[i]);
554 continue;
555 }
556
557 unsigned size = read_length[i] * 32;
558 ImGui::Text("constant buffer %d, size %u", i, size);
559
560 if (ctx->edit_address) {
561 if (ImGui::Button("Show/Edit buffer"))
562 ctx->edit_address(ctx->user_data, read_addr[i], size);
563 }
564 }
565 }
566 }
567
568 static void
569 decode_3dstate_binding_table_pointers(struct aub_viewer_decode_ctx *ctx,
570 struct gen_group *inst,
571 const uint32_t *p)
572 {
573 dump_binding_table(ctx, p[1], -1);
574 }
575
576 static void
577 decode_3dstate_sampler_state_pointers(struct aub_viewer_decode_ctx *ctx,
578 struct gen_group *inst,
579 const uint32_t *p)
580 {
581 dump_samplers(ctx, p[1], -1);
582 }
583
584 static void
585 decode_3dstate_sampler_state_pointers_gen6(struct aub_viewer_decode_ctx *ctx,
586 struct gen_group *inst,
587 const uint32_t *p)
588 {
589 dump_samplers(ctx, p[1], -1);
590 dump_samplers(ctx, p[2], -1);
591 dump_samplers(ctx, p[3], -1);
592 }
593
594 static bool
595 str_ends_with(const char *str, const char *end)
596 {
597 int offset = strlen(str) - strlen(end);
598 if (offset < 0)
599 return false;
600
601 return strcmp(str + offset, end) == 0;
602 }
603
604 static void
605 decode_dynamic_state_pointers(struct aub_viewer_decode_ctx *ctx,
606 struct gen_group *inst, const uint32_t *p,
607 const char *struct_type, int count)
608 {
609 struct gen_group *state = gen_spec_find_struct(ctx->spec, struct_type);
610
611 uint32_t state_offset = 0;
612
613 struct gen_field_iterator iter;
614 gen_field_iterator_init(&iter, inst, p, 0, false);
615 while (gen_field_iterator_next(&iter)) {
616 if (str_ends_with(iter.name, "Pointer")) {
617 state_offset = iter.raw_value;
618 break;
619 }
620 }
621
622 uint64_t state_addr = ctx->dynamic_base + state_offset;
623 struct gen_batch_decode_bo bo = ctx_get_bo(ctx, state_addr);
624 const uint8_t *state_map = (const uint8_t *) bo.map;
625
626 if (state_map == NULL) {
627 ImGui::TextColored(ctx->cfg->missing_color,
628 "dynamic %s state unavailable addr=0x%08" PRIx64,
629 struct_type, state_addr);
630 return;
631 }
632
633 for (int i = 0; i < count; i++) {
634 ImGui::Text("%s %d", struct_type, i);
635 aub_viewer_print_group(ctx, state, state_offset, state_map);
636
637 state_addr += state->dw_length * 4;
638 state_map += state->dw_length;
639 }
640 }
641
642 static void
643 decode_3dstate_viewport_state_pointers_cc(struct aub_viewer_decode_ctx *ctx,
644 struct gen_group *inst,
645 const uint32_t *p)
646 {
647 decode_dynamic_state_pointers(ctx, inst, p, "CC_VIEWPORT", 4);
648 }
649
650 static void
651 decode_3dstate_viewport_state_pointers_sf_clip(struct aub_viewer_decode_ctx *ctx,
652 struct gen_group *inst,
653 const uint32_t *p)
654 {
655 decode_dynamic_state_pointers(ctx, inst, p, "SF_CLIP_VIEWPORT", 4);
656 }
657
658 static void
659 decode_3dstate_blend_state_pointers(struct aub_viewer_decode_ctx *ctx,
660 struct gen_group *inst,
661 const uint32_t *p)
662 {
663 decode_dynamic_state_pointers(ctx, inst, p, "BLEND_STATE", 1);
664 }
665
666 static void
667 decode_3dstate_cc_state_pointers(struct aub_viewer_decode_ctx *ctx,
668 struct gen_group *inst,
669 const uint32_t *p)
670 {
671 decode_dynamic_state_pointers(ctx, inst, p, "COLOR_CALC_STATE", 1);
672 }
673
674 static void
675 decode_3dstate_scissor_state_pointers(struct aub_viewer_decode_ctx *ctx,
676 struct gen_group *inst,
677 const uint32_t *p)
678 {
679 decode_dynamic_state_pointers(ctx, inst, p, "SCISSOR_RECT", 1);
680 }
681
682 static void
683 decode_load_register_imm(struct aub_viewer_decode_ctx *ctx,
684 struct gen_group *inst,
685 const uint32_t *p)
686 {
687 struct gen_group *reg = gen_spec_find_register(ctx->spec, p[1]);
688
689 if (reg != NULL &&
690 ImGui::TreeNodeEx(&p[1], ImGuiTreeNodeFlags_Framed,
691 "%s (0x%x) = 0x%x",
692 reg->name, reg->register_offset, p[2])) {
693 aub_viewer_print_group(ctx, reg, reg->register_offset, &p[2]);
694 ImGui::TreePop();
695 }
696 }
697
698 static void
699 decode_3dprimitive(struct aub_viewer_decode_ctx *ctx,
700 struct gen_group *inst,
701 const uint32_t *p)
702 {
703 if (ctx->display_urb) {
704 if (ImGui::Button("Show URB"))
705 ctx->display_urb(ctx->user_data, ctx->urb_stages);
706 }
707 }
708
709 static void
710 handle_urb(struct aub_viewer_decode_ctx *ctx,
711 struct gen_group *inst,
712 const uint32_t *p)
713 {
714 struct gen_field_iterator iter;
715 gen_field_iterator_init(&iter, inst, p, 0, false);
716 while (gen_field_iterator_next(&iter)) {
717 if (strstr(iter.name, "URB Starting Address")) {
718 ctx->urb_stages[ctx->stage].start = iter.raw_value * 8192;
719 } else if (strstr(iter.name, "URB Entry Allocation Size")) {
720 ctx->urb_stages[ctx->stage].size = (iter.raw_value + 1) * 64;
721 } else if (strstr(iter.name, "Number of URB Entries")) {
722 ctx->urb_stages[ctx->stage].n_entries = iter.raw_value;
723 }
724 }
725
726 ctx->end_urb_offset = MAX2(ctx->urb_stages[ctx->stage].start +
727 ctx->urb_stages[ctx->stage].n_entries *
728 ctx->urb_stages[ctx->stage].size,
729 ctx->end_urb_offset);
730 }
731
732 static void
733 handle_urb_read(struct aub_viewer_decode_ctx *ctx,
734 struct gen_group *inst,
735 const uint32_t *p)
736 {
737 struct gen_field_iterator iter;
738 gen_field_iterator_init(&iter, inst, p, 0, false);
739 while (gen_field_iterator_next(&iter)) {
740 /* Workaround the "Force * URB Entry Read Length" fields */
741 if (iter.end_bit - iter.start_bit < 2)
742 continue;
743
744 if (strstr(iter.name, "URB Entry Read Offset")) {
745 ctx->urb_stages[ctx->stage].rd_offset = iter.raw_value * 32;
746 } else if (strstr(iter.name, "URB Entry Read Length")) {
747 ctx->urb_stages[ctx->stage].rd_length = iter.raw_value * 32;
748 } else if (strstr(iter.name, "URB Entry Output Read Offset")) {
749 ctx->urb_stages[ctx->stage].wr_offset = iter.raw_value * 32;
750 } else if (strstr(iter.name, "URB Entry Output Length")) {
751 ctx->urb_stages[ctx->stage].wr_length = iter.raw_value * 32;
752 }
753 }
754 }
755
756 static void
757 handle_urb_constant(struct aub_viewer_decode_ctx *ctx,
758 struct gen_group *inst,
759 const uint32_t *p)
760 {
761 struct gen_group *body =
762 gen_spec_find_struct(ctx->spec, "3DSTATE_CONSTANT_BODY");
763
764 struct gen_field_iterator outer;
765 gen_field_iterator_init(&outer, inst, p, 0, false);
766 while (gen_field_iterator_next(&outer)) {
767 if (outer.struct_desc != body)
768 continue;
769
770 struct gen_field_iterator iter;
771 gen_field_iterator_init(&iter, body, &outer.p[outer.start_bit / 32],
772 0, false);
773
774 ctx->urb_stages[ctx->stage].const_rd_length = 0;
775 while (gen_field_iterator_next(&iter)) {
776 int idx;
777 if (sscanf(iter.name, "Read Length[%d]", &idx) == 1) {
778 ctx->urb_stages[ctx->stage].const_rd_length += iter.raw_value * 32;
779 }
780 }
781 }
782 }
783
784 struct custom_decoder {
785 const char *cmd_name;
786 void (*decode)(struct aub_viewer_decode_ctx *ctx,
787 struct gen_group *inst,
788 const uint32_t *p);
789 enum aub_decode_stage stage;
790 } display_decoders[] = {
791 { "STATE_BASE_ADDRESS", handle_state_base_address },
792 { "MEDIA_INTERFACE_DESCRIPTOR_LOAD", handle_media_interface_descriptor_load },
793 { "3DSTATE_VERTEX_BUFFERS", handle_3dstate_vertex_buffers },
794 { "3DSTATE_INDEX_BUFFER", handle_3dstate_index_buffer },
795 { "3DSTATE_VS", decode_single_ksp, AUB_DECODE_STAGE_VS, },
796 { "3DSTATE_GS", decode_single_ksp, AUB_DECODE_STAGE_GS, },
797 { "3DSTATE_DS", decode_single_ksp, AUB_DECODE_STAGE_DS, },
798 { "3DSTATE_HS", decode_single_ksp, AUB_DECODE_STAGE_HS, },
799 { "3DSTATE_PS", decode_ps_kernels, AUB_DECODE_STAGE_PS, },
800 { "3DSTATE_CONSTANT_VS", decode_3dstate_constant, AUB_DECODE_STAGE_VS, },
801 { "3DSTATE_CONSTANT_GS", decode_3dstate_constant, AUB_DECODE_STAGE_GS, },
802 { "3DSTATE_CONSTANT_DS", decode_3dstate_constant, AUB_DECODE_STAGE_DS, },
803 { "3DSTATE_CONSTANT_HS", decode_3dstate_constant, AUB_DECODE_STAGE_HS, },
804 { "3DSTATE_CONSTANT_PS", decode_3dstate_constant, AUB_DECODE_STAGE_PS, },
805
806 { "3DSTATE_BINDING_TABLE_POINTERS_VS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_VS, },
807 { "3DSTATE_BINDING_TABLE_POINTERS_GS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_GS, },
808 { "3DSTATE_BINDING_TABLE_POINTERS_HS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_HS, },
809 { "3DSTATE_BINDING_TABLE_POINTERS_DS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_DS, },
810 { "3DSTATE_BINDING_TABLE_POINTERS_PS", decode_3dstate_binding_table_pointers, AUB_DECODE_STAGE_PS, },
811
812 { "3DSTATE_SAMPLER_STATE_POINTERS_VS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_VS, },
813 { "3DSTATE_SAMPLER_STATE_POINTERS_GS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_GS, },
814 { "3DSTATE_SAMPLER_STATE_POINTERS_DS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_DS, },
815 { "3DSTATE_SAMPLER_STATE_POINTERS_HS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_HS, },
816 { "3DSTATE_SAMPLER_STATE_POINTERS_PS", decode_3dstate_sampler_state_pointers, AUB_DECODE_STAGE_PS, },
817 { "3DSTATE_SAMPLER_STATE_POINTERS", decode_3dstate_sampler_state_pointers_gen6 },
818
819 { "3DSTATE_VIEWPORT_STATE_POINTERS_CC", decode_3dstate_viewport_state_pointers_cc },
820 { "3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP", decode_3dstate_viewport_state_pointers_sf_clip },
821 { "3DSTATE_BLEND_STATE_POINTERS", decode_3dstate_blend_state_pointers },
822 { "3DSTATE_CC_STATE_POINTERS", decode_3dstate_cc_state_pointers },
823 { "3DSTATE_SCISSOR_STATE_POINTERS", decode_3dstate_scissor_state_pointers },
824 { "MI_LOAD_REGISTER_IMM", decode_load_register_imm },
825 { "3DPRIMITIVE", decode_3dprimitive },
826 };
827
828 struct custom_decoder info_decoders[] = {
829 { "STATE_BASE_ADDRESS", handle_state_base_address },
830 { "3DSTATE_URB_VS", handle_urb, AUB_DECODE_STAGE_VS, },
831 { "3DSTATE_URB_GS", handle_urb, AUB_DECODE_STAGE_GS, },
832 { "3DSTATE_URB_DS", handle_urb, AUB_DECODE_STAGE_DS, },
833 { "3DSTATE_URB_HS", handle_urb, AUB_DECODE_STAGE_HS, },
834 { "3DSTATE_VS", handle_urb_read, AUB_DECODE_STAGE_VS, },
835 { "3DSTATE_GS", handle_urb_read, AUB_DECODE_STAGE_GS, },
836 { "3DSTATE_DS", handle_urb_read, AUB_DECODE_STAGE_DS, },
837 { "3DSTATE_HS", handle_urb_read, AUB_DECODE_STAGE_HS, },
838 { "3DSTATE_PS", handle_urb_read, AUB_DECODE_STAGE_PS, },
839 { "3DSTATE_CONSTANT_VS", handle_urb_constant, AUB_DECODE_STAGE_VS, },
840 { "3DSTATE_CONSTANT_GS", handle_urb_constant, AUB_DECODE_STAGE_GS, },
841 { "3DSTATE_CONSTANT_DS", handle_urb_constant, AUB_DECODE_STAGE_DS, },
842 { "3DSTATE_CONSTANT_HS", handle_urb_constant, AUB_DECODE_STAGE_HS, },
843 { "3DSTATE_CONSTANT_PS", handle_urb_constant, AUB_DECODE_STAGE_PS, },
844 };
845
846 static inline uint64_t
847 get_address(struct gen_spec *spec, const uint32_t *p)
848 {
849 /* Addresses are always guaranteed to be page-aligned and sometimes
850 * hardware packets have extra stuff stuffed in the bottom 12 bits.
851 */
852 uint64_t addr = p[0] & ~0xfffu;
853
854 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0)) {
855 /* On Broadwell and above, we have 48-bit addresses which consume two
856 * dwords. Some packets require that these get stored in a "canonical
857 * form" which means that bit 47 is sign-extended through the upper
858 * bits. In order to correctly handle those aub dumps, we need to mask
859 * off the top 16 bits.
860 */
861 addr |= ((uint64_t)p[1] & 0xffff) << 32;
862 }
863
864 return addr;
865 }
866
867 void
868 aub_viewer_render_batch(struct aub_viewer_decode_ctx *ctx,
869 const void *_batch, uint32_t batch_size,
870 uint64_t batch_addr)
871 {
872 struct gen_group *inst;
873 const uint32_t *p, *batch = (const uint32_t *) _batch, *end = batch + batch_size;
874 int length;
875
876 for (p = batch; p < end; p += length) {
877 inst = gen_spec_find_instruction(ctx->spec, p);
878 length = gen_group_get_length(inst, p);
879 assert(inst == NULL || length > 0);
880 length = MAX2(1, length);
881
882 uint64_t offset = batch_addr + ((char *)p - (char *)batch);
883
884 if (inst == NULL) {
885 ImGui::TextColored(ctx->cfg->error_color,
886 "x%08" PRIx64 ": unknown instruction %08x",
887 offset, p[0]);
888 continue;
889 }
890
891 const char *inst_name = gen_group_get_name(inst);
892
893 for (unsigned i = 0; i < ARRAY_SIZE(info_decoders); i++) {
894 if (strcmp(inst_name, info_decoders[i].cmd_name) == 0) {
895 ctx->stage = info_decoders[i].stage;
896 info_decoders[i].decode(ctx, inst, p);
897 break;
898 }
899 }
900
901 if (ctx->decode_cfg->command_filter.PassFilter(inst->name) &&
902 ImGui::TreeNodeEx(p,
903 ImGuiTreeNodeFlags_Framed,
904 "0x%08" PRIx64 ": %s",
905 offset, inst->name)) {
906 aub_viewer_print_group(ctx, inst, offset, p);
907
908 for (unsigned i = 0; i < ARRAY_SIZE(display_decoders); i++) {
909 if (strcmp(inst_name, display_decoders[i].cmd_name) == 0) {
910 ctx->stage = display_decoders[i].stage;
911 display_decoders[i].decode(ctx, inst, p);
912 break;
913 }
914 }
915
916 if (ctx->edit_address) {
917 if (ImGui::Button("Edit instruction"))
918 ctx->edit_address(ctx->user_data, offset, length * 4);
919 }
920
921 ImGui::TreePop();
922 }
923
924 if (strcmp(inst_name, "MI_BATCH_BUFFER_START") == 0) {
925 struct gen_batch_decode_bo next_batch = {};
926 bool second_level;
927 struct gen_field_iterator iter;
928 gen_field_iterator_init(&iter, inst, p, 0, false);
929 while (gen_field_iterator_next(&iter)) {
930 if (strcmp(iter.name, "Batch Buffer Start Address") == 0) {
931 next_batch = ctx_get_bo(ctx, iter.raw_value);
932 } else if (strcmp(iter.name, "Second Level Batch Buffer") == 0) {
933 second_level = iter.raw_value;
934 }
935 }
936
937 if (next_batch.map == NULL) {
938 ImGui::TextColored(ctx->cfg->missing_color,
939 "Secondary batch at 0x%08" PRIx64 " unavailable",
940 next_batch.addr);
941 } else {
942 aub_viewer_render_batch(ctx, next_batch.map, next_batch.size,
943 next_batch.addr);
944 }
945 if (second_level) {
946 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
947 * like a subroutine call. Commands that come afterwards get
948 * processed once the 2nd level batch buffer returns with
949 * MI_BATCH_BUFFER_END.
950 */
951 continue;
952 } else {
953 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
954 * like a goto. Nothing after it will ever get processed. In
955 * order to prevent the recursion from growing, we just reset the
956 * loop and continue;
957 */
958 break;
959 }
960 } else if (strcmp(inst_name, "MI_BATCH_BUFFER_END") == 0) {
961 break;
962 }
963 }
964 }