radeonsi: fix dumping shader descriptors into ddebug logs
[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 typedef unsigned (*slot_remap_func)(unsigned);
384
385 static void si_dump_descriptor_list(struct si_descriptors *desc,
386 const char *shader_name,
387 const char *elem_name,
388 unsigned element_dw_size,
389 unsigned num_elements,
390 slot_remap_func slot_remap,
391 FILE *f)
392 {
393 unsigned i, j;
394
395 for (i = 0; i < num_elements; i++) {
396 unsigned dw_offset = slot_remap(i) * element_dw_size;
397 uint32_t *gpu_ptr = desc->gpu_list ? desc->gpu_list : desc->list;
398 const char *list_note = desc->gpu_list ? "GPU list" : "CPU list";
399 uint32_t *cpu_list = desc->list + dw_offset;
400 uint32_t *gpu_list = gpu_ptr + dw_offset;
401
402 fprintf(f, COLOR_GREEN "%s%s slot %u (%s):" COLOR_RESET "\n",
403 shader_name, elem_name, i, list_note);
404
405 switch (element_dw_size) {
406 case 4:
407 for (j = 0; j < 4; j++)
408 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
409 gpu_list[j], 0xffffffff);
410 break;
411 case 8:
412 for (j = 0; j < 8; j++)
413 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
414 gpu_list[j], 0xffffffff);
415
416 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
417 for (j = 0; j < 4; j++)
418 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
419 gpu_list[4+j], 0xffffffff);
420 break;
421 case 16:
422 for (j = 0; j < 8; j++)
423 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
424 gpu_list[j], 0xffffffff);
425
426 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
427 for (j = 0; j < 4; j++)
428 ac_dump_reg(f, R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
429 gpu_list[4+j], 0xffffffff);
430
431 fprintf(f, COLOR_CYAN " FMASK:" COLOR_RESET "\n");
432 for (j = 0; j < 8; j++)
433 ac_dump_reg(f, R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
434 gpu_list[8+j], 0xffffffff);
435
436 fprintf(f, COLOR_CYAN " Sampler state:" COLOR_RESET "\n");
437 for (j = 0; j < 4; j++)
438 ac_dump_reg(f, R_008F30_SQ_IMG_SAMP_WORD0 + j*4,
439 gpu_list[12+j], 0xffffffff);
440 break;
441 }
442
443 if (memcmp(gpu_list, cpu_list, desc->element_dw_size * 4) != 0) {
444 fprintf(f, COLOR_RED "!!!!! This slot was corrupted in GPU memory !!!!!"
445 COLOR_RESET "\n");
446 }
447
448 fprintf(f, "\n");
449 }
450 }
451
452 static unsigned si_identity(unsigned slot)
453 {
454 return slot;
455 }
456
457 static void si_dump_descriptors(struct si_context *sctx,
458 enum pipe_shader_type processor,
459 const struct tgsi_shader_info *info, FILE *f)
460 {
461 struct si_descriptors *descs =
462 &sctx->descriptors[SI_DESCS_FIRST_SHADER +
463 processor * SI_NUM_SHADER_DESCS];
464 static const char *shader_name[] = {"VS", "PS", "GS", "TCS", "TES", "CS"};
465 const char *name = shader_name[processor];
466 unsigned enabled_constbuf, enabled_shaderbuf, enabled_samplers;
467 unsigned enabled_images;
468
469 if (info) {
470 enabled_constbuf = info->const_buffers_declared;
471 enabled_shaderbuf = info->shader_buffers_declared;
472 enabled_samplers = info->samplers_declared;
473 enabled_images = info->images_declared;
474 } else {
475 enabled_constbuf = sctx->const_and_shader_buffers[processor].enabled_mask >>
476 SI_NUM_SHADER_BUFFERS;
477 enabled_shaderbuf = sctx->const_and_shader_buffers[processor].enabled_mask &
478 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS);
479 enabled_shaderbuf = util_bitreverse(enabled_shaderbuf) >>
480 (32 - SI_NUM_SHADER_BUFFERS);
481 enabled_samplers = sctx->samplers[processor].views.enabled_mask;
482 enabled_images = sctx->images[processor].enabled_mask;
483 }
484
485 if (processor == PIPE_SHADER_VERTEX) {
486 assert(info); /* only CS may not have an info struct */
487
488 si_dump_descriptor_list(&sctx->vertex_buffers, name,
489 " - Vertex buffer", 4, info->num_inputs,
490 si_identity, f);
491 }
492
493 si_dump_descriptor_list(&descs[SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS],
494 name, " - Constant buffer", 4,
495 util_last_bit(enabled_constbuf),
496 si_get_constbuf_slot, f);
497 si_dump_descriptor_list(&descs[SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS],
498 name, " - Shader buffer", 4,
499 util_last_bit(enabled_shaderbuf),
500 si_get_shaderbuf_slot, f);
501 si_dump_descriptor_list(&descs[SI_SHADER_DESCS_SAMPLERS_AND_IMAGES],
502 name, " - Sampler", 16,
503 util_last_bit(enabled_samplers),
504 si_get_sampler_slot, f);
505 si_dump_descriptor_list(&descs[SI_SHADER_DESCS_SAMPLERS_AND_IMAGES],
506 name, " - Image", 8,
507 util_last_bit(enabled_images),
508 si_get_image_slot, f);
509 }
510
511 static void si_dump_gfx_descriptors(struct si_context *sctx,
512 const struct si_shader_ctx_state *state,
513 FILE *f)
514 {
515 if (!state->cso || !state->current)
516 return;
517
518 si_dump_descriptors(sctx, state->cso->type, &state->cso->info, f);
519 }
520
521 static void si_dump_compute_descriptors(struct si_context *sctx, FILE *f)
522 {
523 if (!sctx->cs_shader_state.program ||
524 sctx->cs_shader_state.program != sctx->cs_shader_state.emitted_program)
525 return;
526
527 si_dump_descriptors(sctx, PIPE_SHADER_COMPUTE, NULL, f);
528 }
529
530 struct si_shader_inst {
531 char text[160]; /* one disasm line */
532 unsigned offset; /* instruction offset */
533 unsigned size; /* instruction size = 4 or 8 */
534 };
535
536 /* Split a disassembly string into lines and add them to the array pointed
537 * to by "instructions". */
538 static void si_add_split_disasm(const char *disasm,
539 uint64_t start_addr,
540 unsigned *num,
541 struct si_shader_inst *instructions)
542 {
543 struct si_shader_inst *last_inst = *num ? &instructions[*num - 1] : NULL;
544 char *next;
545
546 while ((next = strchr(disasm, '\n'))) {
547 struct si_shader_inst *inst = &instructions[*num];
548 unsigned len = next - disasm;
549
550 assert(len < ARRAY_SIZE(inst->text));
551 memcpy(inst->text, disasm, len);
552 inst->text[len] = 0;
553 inst->offset = last_inst ? last_inst->offset + last_inst->size : 0;
554
555 const char *semicolon = strchr(disasm, ';');
556 assert(semicolon);
557 /* More than 16 chars after ";" means the instruction is 8 bytes long. */
558 inst->size = next - semicolon > 16 ? 8 : 4;
559
560 snprintf(inst->text + len, ARRAY_SIZE(inst->text) - len,
561 " [PC=0x%"PRIx64", off=%u, size=%u]",
562 start_addr + inst->offset, inst->offset, inst->size);
563
564 last_inst = inst;
565 (*num)++;
566 disasm = next + 1;
567 }
568 }
569
570 #define MAX_WAVES_PER_CHIP (64 * 40)
571
572 struct si_wave_info {
573 unsigned se; /* shader engine */
574 unsigned sh; /* shader array */
575 unsigned cu; /* compute unit */
576 unsigned simd;
577 unsigned wave;
578 uint32_t status;
579 uint64_t pc; /* program counter */
580 uint32_t inst_dw0;
581 uint32_t inst_dw1;
582 uint64_t exec;
583 bool matched; /* whether the wave is used by a currently-bound shader */
584 };
585
586 static int compare_wave(const void *p1, const void *p2)
587 {
588 struct si_wave_info *w1 = (struct si_wave_info *)p1;
589 struct si_wave_info *w2 = (struct si_wave_info *)p2;
590
591 /* Sort waves according to PC and then SE, SH, CU, etc. */
592 if (w1->pc < w2->pc)
593 return -1;
594 if (w1->pc > w2->pc)
595 return 1;
596 if (w1->se < w2->se)
597 return -1;
598 if (w1->se > w2->se)
599 return 1;
600 if (w1->sh < w2->sh)
601 return -1;
602 if (w1->sh > w2->sh)
603 return 1;
604 if (w1->cu < w2->cu)
605 return -1;
606 if (w1->cu > w2->cu)
607 return 1;
608 if (w1->simd < w2->simd)
609 return -1;
610 if (w1->simd > w2->simd)
611 return 1;
612 if (w1->wave < w2->wave)
613 return -1;
614 if (w1->wave > w2->wave)
615 return 1;
616
617 return 0;
618 }
619
620 /* Return wave information. "waves" should be a large enough array. */
621 static unsigned si_get_wave_info(struct si_wave_info waves[MAX_WAVES_PER_CHIP])
622 {
623 char line[2000];
624 unsigned num_waves = 0;
625
626 FILE *p = popen("umr -wa", "r");
627 if (!p)
628 return 0;
629
630 if (!fgets(line, sizeof(line), p) ||
631 strncmp(line, "SE", 2) != 0) {
632 pclose(p);
633 return 0;
634 }
635
636 while (fgets(line, sizeof(line), p)) {
637 struct si_wave_info *w;
638 uint32_t pc_hi, pc_lo, exec_hi, exec_lo;
639
640 assert(num_waves < MAX_WAVES_PER_CHIP);
641 w = &waves[num_waves];
642
643 if (sscanf(line, "%u %u %u %u %u %x %x %x %x %x %x %x",
644 &w->se, &w->sh, &w->cu, &w->simd, &w->wave,
645 &w->status, &pc_hi, &pc_lo, &w->inst_dw0,
646 &w->inst_dw1, &exec_hi, &exec_lo) == 12) {
647 w->pc = ((uint64_t)pc_hi << 32) | pc_lo;
648 w->exec = ((uint64_t)exec_hi << 32) | exec_lo;
649 w->matched = false;
650 num_waves++;
651 }
652 }
653
654 qsort(waves, num_waves, sizeof(struct si_wave_info), compare_wave);
655
656 pclose(p);
657 return num_waves;
658 }
659
660 /* If the shader is being executed, print its asm instructions, and annotate
661 * those that are being executed right now with information about waves that
662 * execute them. This is most useful during a GPU hang.
663 */
664 static void si_print_annotated_shader(struct si_shader *shader,
665 struct si_wave_info *waves,
666 unsigned num_waves,
667 FILE *f)
668 {
669 if (!shader || !shader->binary.disasm_string)
670 return;
671
672 uint64_t start_addr = shader->bo->gpu_address;
673 uint64_t end_addr = start_addr + shader->bo->b.b.width0;
674 unsigned i;
675
676 /* See if any wave executes the shader. */
677 for (i = 0; i < num_waves; i++) {
678 if (start_addr <= waves[i].pc && waves[i].pc <= end_addr)
679 break;
680 }
681 if (i == num_waves)
682 return; /* the shader is not being executed */
683
684 /* Remember the first found wave. The waves are sorted according to PC. */
685 waves = &waves[i];
686 num_waves -= i;
687
688 /* Get the list of instructions.
689 * Buffer size / 4 is the upper bound of the instruction count.
690 */
691 unsigned num_inst = 0;
692 struct si_shader_inst *instructions =
693 calloc(shader->bo->b.b.width0 / 4, sizeof(struct si_shader_inst));
694
695 if (shader->prolog) {
696 si_add_split_disasm(shader->prolog->binary.disasm_string,
697 start_addr, &num_inst, instructions);
698 }
699 if (shader->previous_stage) {
700 si_add_split_disasm(shader->previous_stage->binary.disasm_string,
701 start_addr, &num_inst, instructions);
702 }
703 if (shader->prolog2) {
704 si_add_split_disasm(shader->prolog2->binary.disasm_string,
705 start_addr, &num_inst, instructions);
706 }
707 si_add_split_disasm(shader->binary.disasm_string,
708 start_addr, &num_inst, instructions);
709 if (shader->epilog) {
710 si_add_split_disasm(shader->epilog->binary.disasm_string,
711 start_addr, &num_inst, instructions);
712 }
713
714 fprintf(f, COLOR_YELLOW "%s - annotated disassembly:" COLOR_RESET "\n",
715 si_get_shader_name(shader, shader->selector->type));
716
717 /* Print instructions with annotations. */
718 for (i = 0; i < num_inst; i++) {
719 struct si_shader_inst *inst = &instructions[i];
720
721 fprintf(f, "%s\n", inst->text);
722
723 /* Print which waves execute the instruction right now. */
724 while (num_waves && start_addr + inst->offset == waves->pc) {
725 fprintf(f,
726 " " COLOR_GREEN "^ SE%u SH%u CU%u "
727 "SIMD%u WAVE%u EXEC=%016"PRIx64 " ",
728 waves->se, waves->sh, waves->cu, waves->simd,
729 waves->wave, waves->exec);
730
731 if (inst->size == 4) {
732 fprintf(f, "INST32=%08X" COLOR_RESET "\n",
733 waves->inst_dw0);
734 } else {
735 fprintf(f, "INST64=%08X %08X" COLOR_RESET "\n",
736 waves->inst_dw0, waves->inst_dw1);
737 }
738
739 waves->matched = true;
740 waves = &waves[1];
741 num_waves--;
742 }
743 }
744
745 fprintf(f, "\n\n");
746 free(instructions);
747 }
748
749 static void si_dump_annotated_shaders(struct si_context *sctx, FILE *f)
750 {
751 struct si_wave_info waves[MAX_WAVES_PER_CHIP];
752 unsigned num_waves = si_get_wave_info(waves);
753
754 fprintf(f, COLOR_CYAN "The number of active waves = %u" COLOR_RESET
755 "\n\n", num_waves);
756
757 si_print_annotated_shader(sctx->vs_shader.current, waves, num_waves, f);
758 si_print_annotated_shader(sctx->tcs_shader.current, waves, num_waves, f);
759 si_print_annotated_shader(sctx->tes_shader.current, waves, num_waves, f);
760 si_print_annotated_shader(sctx->gs_shader.current, waves, num_waves, f);
761 si_print_annotated_shader(sctx->ps_shader.current, waves, num_waves, f);
762
763 /* Print waves executing shaders that are not currently bound. */
764 unsigned i;
765 bool found = false;
766 for (i = 0; i < num_waves; i++) {
767 if (waves[i].matched)
768 continue;
769
770 if (!found) {
771 fprintf(f, COLOR_CYAN
772 "Waves not executing currently-bound shaders:"
773 COLOR_RESET "\n");
774 found = true;
775 }
776 fprintf(f, " SE%u SH%u CU%u SIMD%u WAVE%u EXEC=%016"PRIx64
777 " INST=%08X %08X PC=%"PRIx64"\n",
778 waves[i].se, waves[i].sh, waves[i].cu, waves[i].simd,
779 waves[i].wave, waves[i].exec, waves[i].inst_dw0,
780 waves[i].inst_dw1, waves[i].pc);
781 }
782 if (found)
783 fprintf(f, "\n\n");
784 }
785
786 static void si_dump_command(const char *title, const char *command, FILE *f)
787 {
788 char line[2000];
789
790 FILE *p = popen(command, "r");
791 if (!p)
792 return;
793
794 fprintf(f, COLOR_YELLOW "%s: " COLOR_RESET "\n", title);
795 while (fgets(line, sizeof(line), p))
796 fputs(line, f);
797 fprintf(f, "\n\n");
798 pclose(p);
799 }
800
801 static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
802 unsigned flags)
803 {
804 struct si_context *sctx = (struct si_context*)ctx;
805
806 if (flags & PIPE_DUMP_DEVICE_STATUS_REGISTERS)
807 si_dump_debug_registers(sctx, f);
808
809 if (flags & PIPE_DUMP_CURRENT_STATES)
810 si_dump_framebuffer(sctx, f);
811
812 if (flags & PIPE_DUMP_CURRENT_SHADERS) {
813 si_dump_gfx_shader(sctx->screen, &sctx->vs_shader, f);
814 si_dump_gfx_shader(sctx->screen, &sctx->tcs_shader, f);
815 si_dump_gfx_shader(sctx->screen, &sctx->tes_shader, f);
816 si_dump_gfx_shader(sctx->screen, &sctx->gs_shader, f);
817 si_dump_gfx_shader(sctx->screen, &sctx->ps_shader, f);
818 si_dump_compute_shader(sctx->screen, &sctx->cs_shader_state, f);
819
820 if (flags & PIPE_DUMP_DEVICE_STATUS_REGISTERS) {
821 si_dump_annotated_shaders(sctx, f);
822 si_dump_command("Active waves (raw data)", "umr -wa | column -t", f);
823 si_dump_command("Wave information", "umr -O bits -wa", f);
824 }
825
826 si_dump_descriptor_list(&sctx->descriptors[SI_DESCS_RW_BUFFERS],
827 "", "RW buffers", 4, SI_NUM_RW_BUFFERS,
828 si_identity, f);
829 si_dump_gfx_descriptors(sctx, &sctx->vs_shader, f);
830 si_dump_gfx_descriptors(sctx, &sctx->tcs_shader, f);
831 si_dump_gfx_descriptors(sctx, &sctx->tes_shader, f);
832 si_dump_gfx_descriptors(sctx, &sctx->gs_shader, f);
833 si_dump_gfx_descriptors(sctx, &sctx->ps_shader, f);
834 si_dump_compute_descriptors(sctx, f);
835 }
836
837 if (flags & PIPE_DUMP_LAST_COMMAND_BUFFER) {
838 si_dump_bo_list(sctx, &sctx->last_gfx, f);
839 si_dump_last_ib(sctx, f);
840
841 fprintf(f, "Done.\n");
842
843 /* dump only once */
844 radeon_clear_saved_cs(&sctx->last_gfx);
845 r600_resource_reference(&sctx->last_trace_buf, NULL);
846 }
847 }
848
849 static void si_dump_dma(struct si_context *sctx,
850 struct radeon_saved_cs *saved, FILE *f)
851 {
852 static const char ib_name[] = "sDMA IB";
853 unsigned i;
854
855 si_dump_bo_list(sctx, saved, f);
856
857 fprintf(f, "------------------ %s begin ------------------\n", ib_name);
858
859 for (i = 0; i < saved->num_dw; ++i) {
860 fprintf(f, " %08x\n", saved->ib[i]);
861 }
862
863 fprintf(f, "------------------- %s end -------------------\n", ib_name);
864 fprintf(f, "\n");
865
866 fprintf(f, "SDMA Dump Done.\n");
867 }
868
869 static bool si_vm_fault_occured(struct si_context *sctx, uint32_t *out_addr)
870 {
871 char line[2000];
872 unsigned sec, usec;
873 int progress = 0;
874 uint64_t timestamp = 0;
875 bool fault = false;
876
877 FILE *p = popen("dmesg", "r");
878 if (!p)
879 return false;
880
881 while (fgets(line, sizeof(line), p)) {
882 char *msg, len;
883
884 if (!line[0] || line[0] == '\n')
885 continue;
886
887 /* Get the timestamp. */
888 if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
889 static bool hit = false;
890 if (!hit) {
891 fprintf(stderr, "%s: failed to parse line '%s'\n",
892 __func__, line);
893 hit = true;
894 }
895 continue;
896 }
897 timestamp = sec * 1000000llu + usec;
898
899 /* If just updating the timestamp. */
900 if (!out_addr)
901 continue;
902
903 /* Process messages only if the timestamp is newer. */
904 if (timestamp <= sctx->dmesg_timestamp)
905 continue;
906
907 /* Only process the first VM fault. */
908 if (fault)
909 continue;
910
911 /* Remove trailing \n */
912 len = strlen(line);
913 if (len && line[len-1] == '\n')
914 line[len-1] = 0;
915
916 /* Get the message part. */
917 msg = strchr(line, ']');
918 if (!msg) {
919 assert(0);
920 continue;
921 }
922 msg++;
923
924 switch (progress) {
925 case 0:
926 if (strstr(msg, "GPU fault detected:"))
927 progress = 1;
928 break;
929 case 1:
930 msg = strstr(msg, "VM_CONTEXT1_PROTECTION_FAULT_ADDR");
931 if (msg) {
932 msg = strstr(msg, "0x");
933 if (msg) {
934 msg += 2;
935 if (sscanf(msg, "%X", out_addr) == 1)
936 fault = true;
937 }
938 }
939 progress = 0;
940 break;
941 default:
942 progress = 0;
943 }
944 }
945 pclose(p);
946
947 if (timestamp > sctx->dmesg_timestamp)
948 sctx->dmesg_timestamp = timestamp;
949 return fault;
950 }
951
952 void si_check_vm_faults(struct r600_common_context *ctx,
953 struct radeon_saved_cs *saved, enum ring_type ring)
954 {
955 struct si_context *sctx = (struct si_context *)ctx;
956 struct pipe_screen *screen = sctx->b.b.screen;
957 FILE *f;
958 uint32_t addr;
959 char cmd_line[4096];
960
961 if (!si_vm_fault_occured(sctx, &addr))
962 return;
963
964 f = dd_get_debug_file(false);
965 if (!f)
966 return;
967
968 fprintf(f, "VM fault report.\n\n");
969 if (os_get_command_line(cmd_line, sizeof(cmd_line)))
970 fprintf(f, "Command: %s\n", cmd_line);
971 fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
972 fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
973 fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
974 fprintf(f, "Failing VM page: 0x%08x\n\n", addr);
975
976 if (sctx->apitrace_call_number)
977 fprintf(f, "Last apitrace call: %u\n\n",
978 sctx->apitrace_call_number);
979
980 switch (ring) {
981 case RING_GFX:
982 si_dump_debug_state(&sctx->b.b, f,
983 PIPE_DUMP_CURRENT_STATES |
984 PIPE_DUMP_CURRENT_SHADERS |
985 PIPE_DUMP_LAST_COMMAND_BUFFER);
986 break;
987
988 case RING_DMA:
989 si_dump_dma(sctx, saved, f);
990 break;
991
992 default:
993 break;
994 }
995
996 fclose(f);
997
998 fprintf(stderr, "Detected a VM fault, exiting...\n");
999 exit(0);
1000 }
1001
1002 void si_init_debug_functions(struct si_context *sctx)
1003 {
1004 sctx->b.b.dump_debug_state = si_dump_debug_state;
1005 sctx->b.check_vm_faults = si_check_vm_faults;
1006
1007 /* Set the initial dmesg timestamp for this context, so that
1008 * only new messages will be checked for VM faults.
1009 */
1010 if (sctx->screen->b.debug_flags & DBG_CHECK_VM)
1011 si_vm_fault_occured(sctx, NULL);
1012 }