radeonsi: dump compute descriptor lists
[mesa.git] / src / gallium / drivers / radeonsi / si_debug.c
1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Marek Olšák <maraeo@gmail.com>
25 */
26
27 #include "si_pipe.h"
28 #include "si_compute.h"
29 #include "sid.h"
30 #include "gfx9d.h"
31 #include "sid_tables.h"
32 #include "ddebug/dd_util.h"
33 #include "util/u_memory.h"
34 #include "ac_debug.h"
35
36 DEBUG_GET_ONCE_OPTION(replace_shaders, "RADEON_REPLACE_SHADERS", NULL)
37
38 static void si_dump_shader(struct si_screen *sscreen,
39 enum pipe_shader_type processor,
40 const struct si_shader *shader, FILE *f)
41 {
42 if (shader->shader_log)
43 fwrite(shader->shader_log, shader->shader_log_size, 1, f);
44 else
45 si_shader_dump(sscreen, shader, NULL, processor, f, false);
46 }
47
48 static void si_dump_gfx_shader(struct si_screen *sscreen,
49 const struct si_shader_ctx_state *state, FILE *f)
50 {
51 const struct si_shader *current = state->current;
52
53 if (!state->cso || !current)
54 return;
55
56 si_dump_shader(sscreen, state->cso->info.processor, current, f);
57 }
58
59 static void si_dump_compute_shader(struct si_screen *sscreen,
60 const struct si_cs_shader_state *state, FILE *f)
61 {
62 if (!state->program || state->program != state->emitted_program)
63 return;
64
65 si_dump_shader(sscreen, PIPE_SHADER_COMPUTE, &state->program->shader, f);
66 }
67
68 /**
69 * Shader compiles can be overridden with arbitrary ELF objects by setting
70 * the environment variable RADEON_REPLACE_SHADERS=num1:filename1[;num2:filename2]
71 */
72 bool si_replace_shader(unsigned num, struct ac_shader_binary *binary)
73 {
74 const char *p = debug_get_option_replace_shaders();
75 const char *semicolon;
76 char *copy = NULL;
77 FILE *f;
78 long filesize, nread;
79 char *buf = NULL;
80 bool replaced = false;
81
82 if (!p)
83 return false;
84
85 while (*p) {
86 unsigned long i;
87 char *endp;
88 i = strtoul(p, &endp, 0);
89
90 p = endp;
91 if (*p != ':') {
92 fprintf(stderr, "RADEON_REPLACE_SHADERS formatted badly.\n");
93 exit(1);
94 }
95 ++p;
96
97 if (i == num)
98 break;
99
100 p = strchr(p, ';');
101 if (!p)
102 return false;
103 ++p;
104 }
105 if (!*p)
106 return false;
107
108 semicolon = strchr(p, ';');
109 if (semicolon) {
110 p = copy = strndup(p, semicolon - p);
111 if (!copy) {
112 fprintf(stderr, "out of memory\n");
113 return false;
114 }
115 }
116
117 fprintf(stderr, "radeonsi: replace shader %u by %s\n", num, p);
118
119 f = fopen(p, "r");
120 if (!f) {
121 perror("radeonsi: failed to open file");
122 goto out_free;
123 }
124
125 if (fseek(f, 0, SEEK_END) != 0)
126 goto file_error;
127
128 filesize = ftell(f);
129 if (filesize < 0)
130 goto file_error;
131
132 if (fseek(f, 0, SEEK_SET) != 0)
133 goto file_error;
134
135 buf = MALLOC(filesize);
136 if (!buf) {
137 fprintf(stderr, "out of memory\n");
138 goto out_close;
139 }
140
141 nread = fread(buf, 1, filesize, f);
142 if (nread != filesize)
143 goto file_error;
144
145 ac_elf_read(buf, filesize, binary);
146 replaced = true;
147
148 out_close:
149 fclose(f);
150 out_free:
151 FREE(buf);
152 free(copy);
153 return replaced;
154
155 file_error:
156 perror("radeonsi: reading shader");
157 goto out_close;
158 }
159
160 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
161 * read them, or use "aha -b -f file" to convert them to html.
162 */
163 #define COLOR_RESET "\033[0m"
164 #define COLOR_RED "\033[31m"
165 #define COLOR_GREEN "\033[1;32m"
166 #define COLOR_YELLOW "\033[1;33m"
167 #define COLOR_CYAN "\033[1;36m"
168
169 static void si_dump_mmapped_reg(struct si_context *sctx, FILE *f,
170 unsigned offset)
171 {
172 struct radeon_winsys *ws = sctx->b.ws;
173 uint32_t value;
174
175 if (ws->read_registers(ws, offset, 1, &value))
176 ac_dump_reg(f, offset, value, ~0);
177 }
178
179 static void si_dump_debug_registers(struct si_context *sctx, FILE *f)
180 {
181 if (sctx->screen->b.info.drm_major == 2 &&
182 sctx->screen->b.info.drm_minor < 42)
183 return; /* no radeon support */
184
185 fprintf(f, "Memory-mapped registers:\n");
186 si_dump_mmapped_reg(sctx, f, R_008010_GRBM_STATUS);
187
188 /* No other registers can be read on DRM < 3.1.0. */
189 if (sctx->screen->b.info.drm_major < 3 ||
190 sctx->screen->b.info.drm_minor < 1) {
191 fprintf(f, "\n");
192 return;
193 }
194
195 si_dump_mmapped_reg(sctx, f, R_008008_GRBM_STATUS2);
196 si_dump_mmapped_reg(sctx, f, R_008014_GRBM_STATUS_SE0);
197 si_dump_mmapped_reg(sctx, f, R_008018_GRBM_STATUS_SE1);
198 si_dump_mmapped_reg(sctx, f, R_008038_GRBM_STATUS_SE2);
199 si_dump_mmapped_reg(sctx, f, R_00803C_GRBM_STATUS_SE3);
200 si_dump_mmapped_reg(sctx, f, R_00D034_SDMA0_STATUS_REG);
201 si_dump_mmapped_reg(sctx, f, R_00D834_SDMA1_STATUS_REG);
202 if (sctx->b.chip_class <= VI) {
203 si_dump_mmapped_reg(sctx, f, R_000E50_SRBM_STATUS);
204 si_dump_mmapped_reg(sctx, f, R_000E4C_SRBM_STATUS2);
205 si_dump_mmapped_reg(sctx, f, R_000E54_SRBM_STATUS3);
206 }
207 si_dump_mmapped_reg(sctx, f, R_008680_CP_STAT);
208 si_dump_mmapped_reg(sctx, f, R_008674_CP_STALLED_STAT1);
209 si_dump_mmapped_reg(sctx, f, R_008678_CP_STALLED_STAT2);
210 si_dump_mmapped_reg(sctx, f, R_008670_CP_STALLED_STAT3);
211 si_dump_mmapped_reg(sctx, f, R_008210_CP_CPC_STATUS);
212 si_dump_mmapped_reg(sctx, f, R_008214_CP_CPC_BUSY_STAT);
213 si_dump_mmapped_reg(sctx, f, R_008218_CP_CPC_STALLED_STAT1);
214 si_dump_mmapped_reg(sctx, f, R_00821C_CP_CPF_STATUS);
215 si_dump_mmapped_reg(sctx, f, R_008220_CP_CPF_BUSY_STAT);
216 si_dump_mmapped_reg(sctx, f, R_008224_CP_CPF_STALLED_STAT1);
217 fprintf(f, "\n");
218 }
219
220 static void si_dump_last_ib(struct si_context *sctx, FILE *f)
221 {
222 int last_trace_id = -1;
223
224 if (!sctx->last_gfx.ib)
225 return;
226
227 if (sctx->last_trace_buf) {
228 /* We are expecting that the ddebug pipe has already
229 * waited for the context, so this buffer should be idle.
230 * If the GPU is hung, there is no point in waiting for it.
231 */
232 uint32_t *map = sctx->b.ws->buffer_map(sctx->last_trace_buf->buf,
233 NULL,
234 PIPE_TRANSFER_UNSYNCHRONIZED |
235 PIPE_TRANSFER_READ);
236 if (map)
237 last_trace_id = *map;
238 }
239
240 if (sctx->init_config)
241 ac_parse_ib(f, sctx->init_config->pm4, sctx->init_config->ndw,
242 -1, "IB2: Init config", sctx->b.chip_class,
243 NULL, NULL);
244
245 if (sctx->init_config_gs_rings)
246 ac_parse_ib(f, sctx->init_config_gs_rings->pm4,
247 sctx->init_config_gs_rings->ndw,
248 -1, "IB2: Init GS rings", sctx->b.chip_class,
249 NULL, NULL);
250
251 ac_parse_ib(f, sctx->last_gfx.ib, sctx->last_gfx.num_dw,
252 last_trace_id, "IB", sctx->b.chip_class,
253 NULL, NULL);
254 }
255
256 static const char *priority_to_string(enum radeon_bo_priority priority)
257 {
258 #define ITEM(x) [RADEON_PRIO_##x] = #x
259 static const char *table[64] = {
260 ITEM(FENCE),
261 ITEM(TRACE),
262 ITEM(SO_FILLED_SIZE),
263 ITEM(QUERY),
264 ITEM(IB1),
265 ITEM(IB2),
266 ITEM(DRAW_INDIRECT),
267 ITEM(INDEX_BUFFER),
268 ITEM(VCE),
269 ITEM(UVD),
270 ITEM(SDMA_BUFFER),
271 ITEM(SDMA_TEXTURE),
272 ITEM(CP_DMA),
273 ITEM(CONST_BUFFER),
274 ITEM(DESCRIPTORS),
275 ITEM(BORDER_COLORS),
276 ITEM(SAMPLER_BUFFER),
277 ITEM(VERTEX_BUFFER),
278 ITEM(SHADER_RW_BUFFER),
279 ITEM(COMPUTE_GLOBAL),
280 ITEM(SAMPLER_TEXTURE),
281 ITEM(SHADER_RW_IMAGE),
282 ITEM(SAMPLER_TEXTURE_MSAA),
283 ITEM(COLOR_BUFFER),
284 ITEM(DEPTH_BUFFER),
285 ITEM(COLOR_BUFFER_MSAA),
286 ITEM(DEPTH_BUFFER_MSAA),
287 ITEM(CMASK),
288 ITEM(DCC),
289 ITEM(HTILE),
290 ITEM(SHADER_BINARY),
291 ITEM(SHADER_RINGS),
292 ITEM(SCRATCH_BUFFER),
293 };
294 #undef ITEM
295
296 assert(priority < ARRAY_SIZE(table));
297 return table[priority];
298 }
299
300 static int bo_list_compare_va(const struct radeon_bo_list_item *a,
301 const struct radeon_bo_list_item *b)
302 {
303 return a->vm_address < b->vm_address ? -1 :
304 a->vm_address > b->vm_address ? 1 : 0;
305 }
306
307 static void si_dump_bo_list(struct si_context *sctx,
308 const struct radeon_saved_cs *saved, FILE *f)
309 {
310 unsigned i,j;
311
312 if (!saved->bo_list)
313 return;
314
315 /* Sort the list according to VM adddresses first. */
316 qsort(saved->bo_list, saved->bo_count,
317 sizeof(saved->bo_list[0]), (void*)bo_list_compare_va);
318
319 fprintf(f, "Buffer list (in units of pages = 4kB):\n"
320 COLOR_YELLOW " Size VM start page "
321 "VM end page Usage" COLOR_RESET "\n");
322
323 for (i = 0; i < saved->bo_count; i++) {
324 /* Note: Buffer sizes are expected to be aligned to 4k by the winsys. */
325 const unsigned page_size = sctx->b.screen->info.gart_page_size;
326 uint64_t va = saved->bo_list[i].vm_address;
327 uint64_t size = saved->bo_list[i].bo_size;
328 bool hit = false;
329
330 /* If there's unused virtual memory between 2 buffers, print it. */
331 if (i) {
332 uint64_t previous_va_end = saved->bo_list[i-1].vm_address +
333 saved->bo_list[i-1].bo_size;
334
335 if (va > previous_va_end) {
336 fprintf(f, " %10"PRIu64" -- hole --\n",
337 (va - previous_va_end) / page_size);
338 }
339 }
340
341 /* Print the buffer. */
342 fprintf(f, " %10"PRIu64" 0x%013"PRIX64" 0x%013"PRIX64" ",
343 size / page_size, va / page_size, (va + size) / page_size);
344
345 /* Print the usage. */
346 for (j = 0; j < 64; j++) {
347 if (!(saved->bo_list[i].priority_usage & (1llu << j)))
348 continue;
349
350 fprintf(f, "%s%s", !hit ? "" : ", ", priority_to_string(j));
351 hit = true;
352 }
353 fprintf(f, "\n");
354 }
355 fprintf(f, "\nNote: The holes represent memory not used by the IB.\n"
356 " Other buffers can still be allocated there.\n\n");
357 }
358
359 static void si_dump_framebuffer(struct si_context *sctx, FILE *f)
360 {
361 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
362 struct r600_texture *rtex;
363 int i;
364
365 for (i = 0; i < state->nr_cbufs; i++) {
366 if (!state->cbufs[i])
367 continue;
368
369 rtex = (struct r600_texture*)state->cbufs[i]->texture;
370 fprintf(f, COLOR_YELLOW "Color buffer %i:" COLOR_RESET "\n", i);
371 r600_print_texture_info(sctx->b.screen, rtex, f);
372 fprintf(f, "\n");
373 }
374
375 if (state->zsbuf) {
376 rtex = (struct r600_texture*)state->zsbuf->texture;
377 fprintf(f, COLOR_YELLOW "Depth-stencil buffer:" COLOR_RESET "\n");
378 r600_print_texture_info(sctx->b.screen, rtex, f);
379 fprintf(f, "\n");
380 }
381 }
382
383 static void si_dump_descriptor_list(struct si_descriptors *desc,
384 const char *shader_name,
385 const char *elem_name,
386 unsigned num_elements,
387 FILE *f)
388 {
389 unsigned i, j;
390 uint32_t *cpu_list = desc->list;
391 uint32_t *gpu_list = desc->gpu_list;
392 const char *list_note = "GPU list";
393
394 if (!gpu_list) {
395 gpu_list = cpu_list;
396 list_note = "CPU list";
397 }
398
399 for (i = 0; i < num_elements; i++) {
400 fprintf(f, COLOR_GREEN "%s%s slot %u (%s):" COLOR_RESET "\n",
401 shader_name, elem_name, i, list_note);
402
403 switch (desc->element_dw_size) {
404 case 4:
405 for (j = 0; j < 4; j++)
406 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
407 gpu_list[j], 0xffffffff);
408 break;
409 case 8:
410 for (j = 0; j < 8; j++)
411 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
412 gpu_list[j], 0xffffffff);
413
414 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
415 for (j = 0; j < 4; j++)
416 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
417 gpu_list[4+j], 0xffffffff);
418 break;
419 case 16:
420 for (j = 0; j < 8; j++)
421 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
422 gpu_list[j], 0xffffffff);
423
424 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
425 for (j = 0; j < 4; j++)
426 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
427 gpu_list[4+j], 0xffffffff);
428
429 fprintf(f, COLOR_CYAN " FMASK:" COLOR_RESET "\n");
430 for (j = 0; j < 8; j++)
431 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
432 gpu_list[8+j], 0xffffffff);
433
434 fprintf(f, COLOR_CYAN " Sampler state:" COLOR_RESET "\n");
435 for (j = 0; j < 4; j++)
436 ac_dump_reg(f, R_008F30_SQ_IMG_SAMP_WORD0 + j*4,
437 gpu_list[12+j], 0xffffffff);
438 break;
439 }
440
441 if (memcmp(gpu_list, cpu_list, desc->element_dw_size * 4) != 0) {
442 fprintf(f, COLOR_RED "!!!!! This slot was corrupted in GPU memory !!!!!"
443 COLOR_RESET "\n");
444 }
445
446 fprintf(f, "\n");
447 gpu_list += desc->element_dw_size;
448 cpu_list += desc->element_dw_size;
449 }
450 }
451
452 static void si_dump_descriptors(struct si_context *sctx,
453 enum pipe_shader_type processor,
454 const struct tgsi_shader_info *info, FILE *f)
455 {
456 struct si_descriptors *descs =
457 &sctx->descriptors[SI_DESCS_FIRST_SHADER +
458 processor * SI_NUM_SHADER_DESCS];
459 static const char *shader_name[] = {"VS", "PS", "GS", "TCS", "TES", "CS"};
460
461 static const char *elem_name[] = {
462 " - Constant buffer",
463 " - Shader buffer",
464 " - Sampler",
465 " - Image",
466 };
467 unsigned enabled_slots[] = {
468 sctx->const_buffers[processor].enabled_mask,
469 sctx->shader_buffers[processor].enabled_mask,
470 sctx->samplers[processor].views.enabled_mask,
471 sctx->images[processor].enabled_mask,
472 };
473 unsigned required_slots[] = {
474 info ? info->const_buffers_declared : 0,
475 info ? info->shader_buffers_declared : 0,
476 info ? info->samplers_declared : 0,
477 info ? info->images_declared : 0,
478 };
479
480 if (processor == PIPE_SHADER_VERTEX) {
481 si_dump_descriptor_list(&sctx->vertex_buffers, shader_name[processor],
482 " - Vertex buffer", info->num_inputs, f);
483 }
484
485 for (unsigned i = 0; i < SI_NUM_SHADER_DESCS; ++i, ++descs)
486 si_dump_descriptor_list(descs, shader_name[processor], elem_name[i],
487 util_last_bit(enabled_slots[i] | required_slots[i]), f);
488 }
489
490 static void si_dump_gfx_descriptors(struct si_context *sctx,
491 const struct si_shader_ctx_state *state,
492 FILE *f)
493 {
494 if (!state->cso || !state->current)
495 return;
496
497 si_dump_descriptors(sctx, state->cso->type, &state->cso->info, f);
498 }
499
500 static void si_dump_compute_descriptors(struct si_context *sctx, FILE *f)
501 {
502 if (!sctx->cs_shader_state.program ||
503 sctx->cs_shader_state.program != sctx->cs_shader_state.emitted_program)
504 return;
505
506 si_dump_descriptors(sctx, PIPE_SHADER_COMPUTE, NULL, f);
507 }
508
509 struct si_shader_inst {
510 char text[160]; /* one disasm line */
511 unsigned offset; /* instruction offset */
512 unsigned size; /* instruction size = 4 or 8 */
513 };
514
515 /* Split a disassembly string into lines and add them to the array pointed
516 * to by "instructions". */
517 static void si_add_split_disasm(const char *disasm,
518 uint64_t start_addr,
519 unsigned *num,
520 struct si_shader_inst *instructions)
521 {
522 struct si_shader_inst *last_inst = *num ? &instructions[*num - 1] : NULL;
523 char *next;
524
525 while ((next = strchr(disasm, '\n'))) {
526 struct si_shader_inst *inst = &instructions[*num];
527 unsigned len = next - disasm;
528
529 assert(len < ARRAY_SIZE(inst->text));
530 memcpy(inst->text, disasm, len);
531 inst->text[len] = 0;
532 inst->offset = last_inst ? last_inst->offset + last_inst->size : 0;
533
534 const char *semicolon = strchr(disasm, ';');
535 assert(semicolon);
536 /* More than 16 chars after ";" means the instruction is 8 bytes long. */
537 inst->size = next - semicolon > 16 ? 8 : 4;
538
539 snprintf(inst->text + len, ARRAY_SIZE(inst->text) - len,
540 " [PC=0x%"PRIx64", off=%u, size=%u]",
541 start_addr + inst->offset, inst->offset, inst->size);
542
543 last_inst = inst;
544 (*num)++;
545 disasm = next + 1;
546 }
547 }
548
549 #define MAX_WAVES_PER_CHIP (64 * 40)
550
551 struct si_wave_info {
552 unsigned se; /* shader engine */
553 unsigned sh; /* shader array */
554 unsigned cu; /* compute unit */
555 unsigned simd;
556 unsigned wave;
557 uint32_t status;
558 uint64_t pc; /* program counter */
559 uint32_t inst_dw0;
560 uint32_t inst_dw1;
561 uint64_t exec;
562 bool matched; /* whether the wave is used by a currently-bound shader */
563 };
564
565 static int compare_wave(const void *p1, const void *p2)
566 {
567 struct si_wave_info *w1 = (struct si_wave_info *)p1;
568 struct si_wave_info *w2 = (struct si_wave_info *)p2;
569
570 /* Sort waves according to PC and then SE, SH, CU, etc. */
571 if (w1->pc < w2->pc)
572 return -1;
573 if (w1->pc > w2->pc)
574 return 1;
575 if (w1->se < w2->se)
576 return -1;
577 if (w1->se > w2->se)
578 return 1;
579 if (w1->sh < w2->sh)
580 return -1;
581 if (w1->sh > w2->sh)
582 return 1;
583 if (w1->cu < w2->cu)
584 return -1;
585 if (w1->cu > w2->cu)
586 return 1;
587 if (w1->simd < w2->simd)
588 return -1;
589 if (w1->simd > w2->simd)
590 return 1;
591 if (w1->wave < w2->wave)
592 return -1;
593 if (w1->wave > w2->wave)
594 return 1;
595
596 return 0;
597 }
598
599 /* Return wave information. "waves" should be a large enough array. */
600 static unsigned si_get_wave_info(struct si_wave_info waves[MAX_WAVES_PER_CHIP])
601 {
602 char line[2000];
603 unsigned num_waves = 0;
604
605 FILE *p = popen("umr -wa", "r");
606 if (!p)
607 return 0;
608
609 if (!fgets(line, sizeof(line), p) ||
610 strncmp(line, "SE", 2) != 0) {
611 pclose(p);
612 return 0;
613 }
614
615 while (fgets(line, sizeof(line), p)) {
616 struct si_wave_info *w;
617 uint32_t pc_hi, pc_lo, exec_hi, exec_lo;
618
619 assert(num_waves < MAX_WAVES_PER_CHIP);
620 w = &waves[num_waves];
621
622 if (sscanf(line, "%u %u %u %u %u %x %x %x %x %x %x %x",
623 &w->se, &w->sh, &w->cu, &w->simd, &w->wave,
624 &w->status, &pc_hi, &pc_lo, &w->inst_dw0,
625 &w->inst_dw1, &exec_hi, &exec_lo) == 12) {
626 w->pc = ((uint64_t)pc_hi << 32) | pc_lo;
627 w->exec = ((uint64_t)exec_hi << 32) | exec_lo;
628 w->matched = false;
629 num_waves++;
630 }
631 }
632
633 qsort(waves, num_waves, sizeof(struct si_wave_info), compare_wave);
634
635 pclose(p);
636 return num_waves;
637 }
638
639 /* If the shader is being executed, print its asm instructions, and annotate
640 * those that are being executed right now with information about waves that
641 * execute them. This is most useful during a GPU hang.
642 */
643 static void si_print_annotated_shader(struct si_shader *shader,
644 struct si_wave_info *waves,
645 unsigned num_waves,
646 FILE *f)
647 {
648 if (!shader || !shader->binary.disasm_string)
649 return;
650
651 uint64_t start_addr = shader->bo->gpu_address;
652 uint64_t end_addr = start_addr + shader->bo->b.b.width0;
653 unsigned i;
654
655 /* See if any wave executes the shader. */
656 for (i = 0; i < num_waves; i++) {
657 if (start_addr <= waves[i].pc && waves[i].pc <= end_addr)
658 break;
659 }
660 if (i == num_waves)
661 return; /* the shader is not being executed */
662
663 /* Remember the first found wave. The waves are sorted according to PC. */
664 waves = &waves[i];
665 num_waves -= i;
666
667 /* Get the list of instructions.
668 * Buffer size / 4 is the upper bound of the instruction count.
669 */
670 unsigned num_inst = 0;
671 struct si_shader_inst *instructions =
672 calloc(shader->bo->b.b.width0 / 4, sizeof(struct si_shader_inst));
673
674 if (shader->prolog) {
675 si_add_split_disasm(shader->prolog->binary.disasm_string,
676 start_addr, &num_inst, instructions);
677 }
678 if (shader->previous_stage) {
679 si_add_split_disasm(shader->previous_stage->binary.disasm_string,
680 start_addr, &num_inst, instructions);
681 }
682 if (shader->prolog2) {
683 si_add_split_disasm(shader->prolog2->binary.disasm_string,
684 start_addr, &num_inst, instructions);
685 }
686 si_add_split_disasm(shader->binary.disasm_string,
687 start_addr, &num_inst, instructions);
688 if (shader->epilog) {
689 si_add_split_disasm(shader->epilog->binary.disasm_string,
690 start_addr, &num_inst, instructions);
691 }
692
693 fprintf(f, COLOR_YELLOW "%s - annotated disassembly:" COLOR_RESET "\n",
694 si_get_shader_name(shader, shader->selector->type));
695
696 /* Print instructions with annotations. */
697 for (i = 0; i < num_inst; i++) {
698 struct si_shader_inst *inst = &instructions[i];
699
700 fprintf(f, "%s\n", inst->text);
701
702 /* Print which waves execute the instruction right now. */
703 while (num_waves && start_addr + inst->offset == waves->pc) {
704 fprintf(f,
705 " " COLOR_GREEN "^ SE%u SH%u CU%u "
706 "SIMD%u WAVE%u EXEC=%016"PRIx64 " ",
707 waves->se, waves->sh, waves->cu, waves->simd,
708 waves->wave, waves->exec);
709
710 if (inst->size == 4) {
711 fprintf(f, "INST32=%08X" COLOR_RESET "\n",
712 waves->inst_dw0);
713 } else {
714 fprintf(f, "INST64=%08X %08X" COLOR_RESET "\n",
715 waves->inst_dw0, waves->inst_dw1);
716 }
717
718 waves->matched = true;
719 waves = &waves[1];
720 num_waves--;
721 }
722 }
723
724 fprintf(f, "\n\n");
725 free(instructions);
726 }
727
728 static void si_dump_annotated_shaders(struct si_context *sctx, FILE *f)
729 {
730 struct si_wave_info waves[MAX_WAVES_PER_CHIP];
731 unsigned num_waves = si_get_wave_info(waves);
732
733 fprintf(f, COLOR_CYAN "The number of active waves = %u" COLOR_RESET
734 "\n\n", num_waves);
735
736 si_print_annotated_shader(sctx->vs_shader.current, waves, num_waves, f);
737 si_print_annotated_shader(sctx->tcs_shader.current, waves, num_waves, f);
738 si_print_annotated_shader(sctx->tes_shader.current, waves, num_waves, f);
739 si_print_annotated_shader(sctx->gs_shader.current, waves, num_waves, f);
740 si_print_annotated_shader(sctx->ps_shader.current, waves, num_waves, f);
741
742 /* Print waves executing shaders that are not currently bound. */
743 unsigned i;
744 bool found = false;
745 for (i = 0; i < num_waves; i++) {
746 if (waves[i].matched)
747 continue;
748
749 if (!found) {
750 fprintf(f, COLOR_CYAN
751 "Waves not executing currently-bound shaders:"
752 COLOR_RESET "\n");
753 found = true;
754 }
755 fprintf(f, " SE%u SH%u CU%u SIMD%u WAVE%u EXEC=%016"PRIx64
756 " INST=%08X %08X PC=%"PRIx64"\n",
757 waves[i].se, waves[i].sh, waves[i].cu, waves[i].simd,
758 waves[i].wave, waves[i].exec, waves[i].inst_dw0,
759 waves[i].inst_dw1, waves[i].pc);
760 }
761 if (found)
762 fprintf(f, "\n\n");
763 }
764
765 static void si_dump_command(const char *title, const char *command, FILE *f)
766 {
767 char line[2000];
768
769 FILE *p = popen(command, "r");
770 if (!p)
771 return;
772
773 fprintf(f, COLOR_YELLOW "%s: " COLOR_RESET "\n", title);
774 while (fgets(line, sizeof(line), p))
775 fputs(line, f);
776 fprintf(f, "\n\n");
777 pclose(p);
778 }
779
780 static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
781 unsigned flags)
782 {
783 struct si_context *sctx = (struct si_context*)ctx;
784
785 if (flags & PIPE_DUMP_DEVICE_STATUS_REGISTERS)
786 si_dump_debug_registers(sctx, f);
787
788 if (flags & PIPE_DUMP_CURRENT_STATES)
789 si_dump_framebuffer(sctx, f);
790
791 if (flags & PIPE_DUMP_CURRENT_SHADERS) {
792 si_dump_gfx_shader(sctx->screen, &sctx->vs_shader, f);
793 si_dump_gfx_shader(sctx->screen, &sctx->tcs_shader, f);
794 si_dump_gfx_shader(sctx->screen, &sctx->tes_shader, f);
795 si_dump_gfx_shader(sctx->screen, &sctx->gs_shader, f);
796 si_dump_gfx_shader(sctx->screen, &sctx->ps_shader, f);
797 si_dump_compute_shader(sctx->screen, &sctx->cs_shader_state, f);
798
799 if (flags & PIPE_DUMP_DEVICE_STATUS_REGISTERS) {
800 si_dump_annotated_shaders(sctx, f);
801 si_dump_command("Active waves (raw data)", "umr -wa | column -t", f);
802 si_dump_command("Wave information", "umr -O bits -wa", f);
803 }
804
805 si_dump_descriptor_list(&sctx->descriptors[SI_DESCS_RW_BUFFERS],
806 "", "RW buffers", SI_NUM_RW_BUFFERS, f);
807 si_dump_gfx_descriptors(sctx, &sctx->vs_shader, f);
808 si_dump_gfx_descriptors(sctx, &sctx->tcs_shader, f);
809 si_dump_gfx_descriptors(sctx, &sctx->tes_shader, f);
810 si_dump_gfx_descriptors(sctx, &sctx->gs_shader, f);
811 si_dump_gfx_descriptors(sctx, &sctx->ps_shader, f);
812 si_dump_compute_descriptors(sctx, f);
813 }
814
815 if (flags & PIPE_DUMP_LAST_COMMAND_BUFFER) {
816 si_dump_bo_list(sctx, &sctx->last_gfx, f);
817 si_dump_last_ib(sctx, f);
818
819 fprintf(f, "Done.\n");
820
821 /* dump only once */
822 radeon_clear_saved_cs(&sctx->last_gfx);
823 r600_resource_reference(&sctx->last_trace_buf, NULL);
824 }
825 }
826
827 static void si_dump_dma(struct si_context *sctx,
828 struct radeon_saved_cs *saved, FILE *f)
829 {
830 static const char ib_name[] = "sDMA IB";
831 unsigned i;
832
833 si_dump_bo_list(sctx, saved, f);
834
835 fprintf(f, "------------------ %s begin ------------------\n", ib_name);
836
837 for (i = 0; i < saved->num_dw; ++i) {
838 fprintf(f, " %08x\n", saved->ib[i]);
839 }
840
841 fprintf(f, "------------------- %s end -------------------\n", ib_name);
842 fprintf(f, "\n");
843
844 fprintf(f, "SDMA Dump Done.\n");
845 }
846
847 static bool si_vm_fault_occured(struct si_context *sctx, uint32_t *out_addr)
848 {
849 char line[2000];
850 unsigned sec, usec;
851 int progress = 0;
852 uint64_t timestamp = 0;
853 bool fault = false;
854
855 FILE *p = popen("dmesg", "r");
856 if (!p)
857 return false;
858
859 while (fgets(line, sizeof(line), p)) {
860 char *msg, len;
861
862 if (!line[0] || line[0] == '\n')
863 continue;
864
865 /* Get the timestamp. */
866 if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
867 static bool hit = false;
868 if (!hit) {
869 fprintf(stderr, "%s: failed to parse line '%s'\n",
870 __func__, line);
871 hit = true;
872 }
873 continue;
874 }
875 timestamp = sec * 1000000llu + usec;
876
877 /* If just updating the timestamp. */
878 if (!out_addr)
879 continue;
880
881 /* Process messages only if the timestamp is newer. */
882 if (timestamp <= sctx->dmesg_timestamp)
883 continue;
884
885 /* Only process the first VM fault. */
886 if (fault)
887 continue;
888
889 /* Remove trailing \n */
890 len = strlen(line);
891 if (len && line[len-1] == '\n')
892 line[len-1] = 0;
893
894 /* Get the message part. */
895 msg = strchr(line, ']');
896 if (!msg) {
897 assert(0);
898 continue;
899 }
900 msg++;
901
902 switch (progress) {
903 case 0:
904 if (strstr(msg, "GPU fault detected:"))
905 progress = 1;
906 break;
907 case 1:
908 msg = strstr(msg, "VM_CONTEXT1_PROTECTION_FAULT_ADDR");
909 if (msg) {
910 msg = strstr(msg, "0x");
911 if (msg) {
912 msg += 2;
913 if (sscanf(msg, "%X", out_addr) == 1)
914 fault = true;
915 }
916 }
917 progress = 0;
918 break;
919 default:
920 progress = 0;
921 }
922 }
923 pclose(p);
924
925 if (timestamp > sctx->dmesg_timestamp)
926 sctx->dmesg_timestamp = timestamp;
927 return fault;
928 }
929
930 void si_check_vm_faults(struct r600_common_context *ctx,
931 struct radeon_saved_cs *saved, enum ring_type ring)
932 {
933 struct si_context *sctx = (struct si_context *)ctx;
934 struct pipe_screen *screen = sctx->b.b.screen;
935 FILE *f;
936 uint32_t addr;
937 char cmd_line[4096];
938
939 if (!si_vm_fault_occured(sctx, &addr))
940 return;
941
942 f = dd_get_debug_file(false);
943 if (!f)
944 return;
945
946 fprintf(f, "VM fault report.\n\n");
947 if (os_get_command_line(cmd_line, sizeof(cmd_line)))
948 fprintf(f, "Command: %s\n", cmd_line);
949 fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
950 fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
951 fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
952 fprintf(f, "Failing VM page: 0x%08x\n\n", addr);
953
954 if (sctx->apitrace_call_number)
955 fprintf(f, "Last apitrace call: %u\n\n",
956 sctx->apitrace_call_number);
957
958 switch (ring) {
959 case RING_GFX:
960 si_dump_debug_state(&sctx->b.b, f,
961 PIPE_DUMP_CURRENT_STATES |
962 PIPE_DUMP_CURRENT_SHADERS |
963 PIPE_DUMP_LAST_COMMAND_BUFFER);
964 break;
965
966 case RING_DMA:
967 si_dump_dma(sctx, saved, f);
968 break;
969
970 default:
971 break;
972 }
973
974 fclose(f);
975
976 fprintf(stderr, "Detected a VM fault, exiting...\n");
977 exit(0);
978 }
979
980 void si_init_debug_functions(struct si_context *sctx)
981 {
982 sctx->b.b.dump_debug_state = si_dump_debug_state;
983 sctx->b.check_vm_faults = si_check_vm_faults;
984
985 /* Set the initial dmesg timestamp for this context, so that
986 * only new messages will be checked for VM faults.
987 */
988 if (sctx->screen->b.debug_flags & DBG_CHECK_VM)
989 si_vm_fault_occured(sctx, NULL);
990 }