radeonsi: update copyrights
[mesa.git] / src / gallium / drivers / radeonsi / si_debug.c
1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "si_pipe.h"
26 #include "si_compute.h"
27 #include "sid.h"
28 #include "gfx9d.h"
29 #include "sid_tables.h"
30 #include "ddebug/dd_util.h"
31 #include "util/u_dump.h"
32 #include "util/u_log.h"
33 #include "util/u_memory.h"
34 #include "ac_debug.h"
35
36 static void si_dump_bo_list(struct si_context *sctx,
37 const struct radeon_saved_cs *saved, FILE *f);
38
39 DEBUG_GET_ONCE_OPTION(replace_shaders, "RADEON_REPLACE_SHADERS", NULL)
40
41 /**
42 * Store a linearized copy of all chunks of \p cs together with the buffer
43 * list in \p saved.
44 */
45 void si_save_cs(struct radeon_winsys *ws, struct radeon_winsys_cs *cs,
46 struct radeon_saved_cs *saved, bool get_buffer_list)
47 {
48 uint32_t *buf;
49 unsigned i;
50
51 /* Save the IB chunks. */
52 saved->num_dw = cs->prev_dw + cs->current.cdw;
53 saved->ib = MALLOC(4 * saved->num_dw);
54 if (!saved->ib)
55 goto oom;
56
57 buf = saved->ib;
58 for (i = 0; i < cs->num_prev; ++i) {
59 memcpy(buf, cs->prev[i].buf, cs->prev[i].cdw * 4);
60 buf += cs->prev[i].cdw;
61 }
62 memcpy(buf, cs->current.buf, cs->current.cdw * 4);
63
64 if (!get_buffer_list)
65 return;
66
67 /* Save the buffer list. */
68 saved->bo_count = ws->cs_get_buffer_list(cs, NULL);
69 saved->bo_list = CALLOC(saved->bo_count,
70 sizeof(saved->bo_list[0]));
71 if (!saved->bo_list) {
72 FREE(saved->ib);
73 goto oom;
74 }
75 ws->cs_get_buffer_list(cs, saved->bo_list);
76
77 return;
78
79 oom:
80 fprintf(stderr, "%s: out of memory\n", __func__);
81 memset(saved, 0, sizeof(*saved));
82 }
83
84 void si_clear_saved_cs(struct radeon_saved_cs *saved)
85 {
86 FREE(saved->ib);
87 FREE(saved->bo_list);
88
89 memset(saved, 0, sizeof(*saved));
90 }
91
92 void si_destroy_saved_cs(struct si_saved_cs *scs)
93 {
94 si_clear_saved_cs(&scs->gfx);
95 r600_resource_reference(&scs->trace_buf, NULL);
96 free(scs);
97 }
98
99 static void si_dump_shader(struct si_screen *sscreen,
100 enum pipe_shader_type processor,
101 const struct si_shader *shader, FILE *f)
102 {
103 if (shader->shader_log)
104 fwrite(shader->shader_log, shader->shader_log_size, 1, f);
105 else
106 si_shader_dump(sscreen, shader, NULL, processor, f, false);
107 }
108
109 struct si_log_chunk_shader {
110 /* The shader destroy code assumes a current context for unlinking of
111 * PM4 packets etc.
112 *
113 * While we should be able to destroy shaders without a context, doing
114 * so would happen only very rarely and be therefore likely to fail
115 * just when you're trying to debug something. Let's just remember the
116 * current context in the chunk.
117 */
118 struct si_context *ctx;
119 struct si_shader *shader;
120 enum pipe_shader_type processor;
121
122 /* For keep-alive reference counts */
123 struct si_shader_selector *sel;
124 struct si_compute *program;
125 };
126
127 static void
128 si_log_chunk_shader_destroy(void *data)
129 {
130 struct si_log_chunk_shader *chunk = data;
131 si_shader_selector_reference(chunk->ctx, &chunk->sel, NULL);
132 si_compute_reference(&chunk->program, NULL);
133 FREE(chunk);
134 }
135
136 static void
137 si_log_chunk_shader_print(void *data, FILE *f)
138 {
139 struct si_log_chunk_shader *chunk = data;
140 struct si_screen *sscreen = chunk->ctx->screen;
141 si_dump_shader(sscreen, chunk->processor,
142 chunk->shader, f);
143 }
144
145 static struct u_log_chunk_type si_log_chunk_type_shader = {
146 .destroy = si_log_chunk_shader_destroy,
147 .print = si_log_chunk_shader_print,
148 };
149
150 static void si_dump_gfx_shader(struct si_context *ctx,
151 const struct si_shader_ctx_state *state,
152 struct u_log_context *log)
153 {
154 struct si_shader *current = state->current;
155
156 if (!state->cso || !current)
157 return;
158
159 struct si_log_chunk_shader *chunk = CALLOC_STRUCT(si_log_chunk_shader);
160 chunk->ctx = ctx;
161 chunk->processor = state->cso->info.processor;
162 chunk->shader = current;
163 si_shader_selector_reference(ctx, &chunk->sel, current->selector);
164 u_log_chunk(log, &si_log_chunk_type_shader, chunk);
165 }
166
167 static void si_dump_compute_shader(struct si_context *ctx,
168 struct u_log_context *log)
169 {
170 const struct si_cs_shader_state *state = &ctx->cs_shader_state;
171
172 if (!state->program)
173 return;
174
175 struct si_log_chunk_shader *chunk = CALLOC_STRUCT(si_log_chunk_shader);
176 chunk->ctx = ctx;
177 chunk->processor = PIPE_SHADER_COMPUTE;
178 chunk->shader = &state->program->shader;
179 si_compute_reference(&chunk->program, state->program);
180 u_log_chunk(log, &si_log_chunk_type_shader, chunk);
181 }
182
183 /**
184 * Shader compiles can be overridden with arbitrary ELF objects by setting
185 * the environment variable RADEON_REPLACE_SHADERS=num1:filename1[;num2:filename2]
186 */
187 bool si_replace_shader(unsigned num, struct ac_shader_binary *binary)
188 {
189 const char *p = debug_get_option_replace_shaders();
190 const char *semicolon;
191 char *copy = NULL;
192 FILE *f;
193 long filesize, nread;
194 char *buf = NULL;
195 bool replaced = false;
196
197 if (!p)
198 return false;
199
200 while (*p) {
201 unsigned long i;
202 char *endp;
203 i = strtoul(p, &endp, 0);
204
205 p = endp;
206 if (*p != ':') {
207 fprintf(stderr, "RADEON_REPLACE_SHADERS formatted badly.\n");
208 exit(1);
209 }
210 ++p;
211
212 if (i == num)
213 break;
214
215 p = strchr(p, ';');
216 if (!p)
217 return false;
218 ++p;
219 }
220 if (!*p)
221 return false;
222
223 semicolon = strchr(p, ';');
224 if (semicolon) {
225 p = copy = strndup(p, semicolon - p);
226 if (!copy) {
227 fprintf(stderr, "out of memory\n");
228 return false;
229 }
230 }
231
232 fprintf(stderr, "radeonsi: replace shader %u by %s\n", num, p);
233
234 f = fopen(p, "r");
235 if (!f) {
236 perror("radeonsi: failed to open file");
237 goto out_free;
238 }
239
240 if (fseek(f, 0, SEEK_END) != 0)
241 goto file_error;
242
243 filesize = ftell(f);
244 if (filesize < 0)
245 goto file_error;
246
247 if (fseek(f, 0, SEEK_SET) != 0)
248 goto file_error;
249
250 buf = MALLOC(filesize);
251 if (!buf) {
252 fprintf(stderr, "out of memory\n");
253 goto out_close;
254 }
255
256 nread = fread(buf, 1, filesize, f);
257 if (nread != filesize)
258 goto file_error;
259
260 ac_elf_read(buf, filesize, binary);
261 replaced = true;
262
263 out_close:
264 fclose(f);
265 out_free:
266 FREE(buf);
267 free(copy);
268 return replaced;
269
270 file_error:
271 perror("radeonsi: reading shader");
272 goto out_close;
273 }
274
275 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
276 * read them, or use "aha -b -f file" to convert them to html.
277 */
278 #define COLOR_RESET "\033[0m"
279 #define COLOR_RED "\033[31m"
280 #define COLOR_GREEN "\033[1;32m"
281 #define COLOR_YELLOW "\033[1;33m"
282 #define COLOR_CYAN "\033[1;36m"
283
284 static void si_dump_mmapped_reg(struct si_context *sctx, FILE *f,
285 unsigned offset)
286 {
287 struct radeon_winsys *ws = sctx->b.ws;
288 uint32_t value;
289
290 if (ws->read_registers(ws, offset, 1, &value))
291 ac_dump_reg(f, sctx->b.chip_class, offset, value, ~0);
292 }
293
294 static void si_dump_debug_registers(struct si_context *sctx, FILE *f)
295 {
296 if (sctx->screen->info.drm_major == 2 &&
297 sctx->screen->info.drm_minor < 42)
298 return; /* no radeon support */
299
300 fprintf(f, "Memory-mapped registers:\n");
301 si_dump_mmapped_reg(sctx, f, R_008010_GRBM_STATUS);
302
303 /* No other registers can be read on DRM < 3.1.0. */
304 if (sctx->screen->info.drm_major < 3 ||
305 sctx->screen->info.drm_minor < 1) {
306 fprintf(f, "\n");
307 return;
308 }
309
310 si_dump_mmapped_reg(sctx, f, R_008008_GRBM_STATUS2);
311 si_dump_mmapped_reg(sctx, f, R_008014_GRBM_STATUS_SE0);
312 si_dump_mmapped_reg(sctx, f, R_008018_GRBM_STATUS_SE1);
313 si_dump_mmapped_reg(sctx, f, R_008038_GRBM_STATUS_SE2);
314 si_dump_mmapped_reg(sctx, f, R_00803C_GRBM_STATUS_SE3);
315 si_dump_mmapped_reg(sctx, f, R_00D034_SDMA0_STATUS_REG);
316 si_dump_mmapped_reg(sctx, f, R_00D834_SDMA1_STATUS_REG);
317 if (sctx->b.chip_class <= VI) {
318 si_dump_mmapped_reg(sctx, f, R_000E50_SRBM_STATUS);
319 si_dump_mmapped_reg(sctx, f, R_000E4C_SRBM_STATUS2);
320 si_dump_mmapped_reg(sctx, f, R_000E54_SRBM_STATUS3);
321 }
322 si_dump_mmapped_reg(sctx, f, R_008680_CP_STAT);
323 si_dump_mmapped_reg(sctx, f, R_008674_CP_STALLED_STAT1);
324 si_dump_mmapped_reg(sctx, f, R_008678_CP_STALLED_STAT2);
325 si_dump_mmapped_reg(sctx, f, R_008670_CP_STALLED_STAT3);
326 si_dump_mmapped_reg(sctx, f, R_008210_CP_CPC_STATUS);
327 si_dump_mmapped_reg(sctx, f, R_008214_CP_CPC_BUSY_STAT);
328 si_dump_mmapped_reg(sctx, f, R_008218_CP_CPC_STALLED_STAT1);
329 si_dump_mmapped_reg(sctx, f, R_00821C_CP_CPF_STATUS);
330 si_dump_mmapped_reg(sctx, f, R_008220_CP_CPF_BUSY_STAT);
331 si_dump_mmapped_reg(sctx, f, R_008224_CP_CPF_STALLED_STAT1);
332 fprintf(f, "\n");
333 }
334
335 struct si_log_chunk_cs {
336 struct si_context *ctx;
337 struct si_saved_cs *cs;
338 bool dump_bo_list;
339 unsigned gfx_begin, gfx_end;
340 };
341
342 static void si_log_chunk_type_cs_destroy(void *data)
343 {
344 struct si_log_chunk_cs *chunk = data;
345 si_saved_cs_reference(&chunk->cs, NULL);
346 free(chunk);
347 }
348
349 static void si_parse_current_ib(FILE *f, struct radeon_winsys_cs *cs,
350 unsigned begin, unsigned end,
351 int *last_trace_id, unsigned trace_id_count,
352 const char *name, enum chip_class chip_class)
353 {
354 unsigned orig_end = end;
355
356 assert(begin <= end);
357
358 fprintf(f, "------------------ %s begin (dw = %u) ------------------\n",
359 name, begin);
360
361 for (unsigned prev_idx = 0; prev_idx < cs->num_prev; ++prev_idx) {
362 struct radeon_winsys_cs_chunk *chunk = &cs->prev[prev_idx];
363
364 if (begin < chunk->cdw) {
365 ac_parse_ib_chunk(f, chunk->buf + begin,
366 MIN2(end, chunk->cdw) - begin,
367 last_trace_id, trace_id_count,
368 chip_class, NULL, NULL);
369 }
370
371 if (end <= chunk->cdw)
372 return;
373
374 if (begin < chunk->cdw)
375 fprintf(f, "\n---------- Next %s Chunk ----------\n\n",
376 name);
377
378 begin -= MIN2(begin, chunk->cdw);
379 end -= chunk->cdw;
380 }
381
382 assert(end <= cs->current.cdw);
383
384 ac_parse_ib_chunk(f, cs->current.buf + begin, end - begin, last_trace_id,
385 trace_id_count, chip_class, NULL, NULL);
386
387 fprintf(f, "------------------- %s end (dw = %u) -------------------\n\n",
388 name, orig_end);
389 }
390
391 static void si_log_chunk_type_cs_print(void *data, FILE *f)
392 {
393 struct si_log_chunk_cs *chunk = data;
394 struct si_context *ctx = chunk->ctx;
395 struct si_saved_cs *scs = chunk->cs;
396 int last_trace_id = -1;
397
398 /* We are expecting that the ddebug pipe has already
399 * waited for the context, so this buffer should be idle.
400 * If the GPU is hung, there is no point in waiting for it.
401 */
402 uint32_t *map = ctx->b.ws->buffer_map(scs->trace_buf->buf,
403 NULL,
404 PIPE_TRANSFER_UNSYNCHRONIZED |
405 PIPE_TRANSFER_READ);
406 if (map)
407 last_trace_id = map[0];
408
409 if (chunk->gfx_end != chunk->gfx_begin) {
410 if (chunk->gfx_begin == 0) {
411 if (ctx->init_config)
412 ac_parse_ib(f, ctx->init_config->pm4, ctx->init_config->ndw,
413 NULL, 0, "IB2: Init config", ctx->b.chip_class,
414 NULL, NULL);
415
416 if (ctx->init_config_gs_rings)
417 ac_parse_ib(f, ctx->init_config_gs_rings->pm4,
418 ctx->init_config_gs_rings->ndw,
419 NULL, 0, "IB2: Init GS rings", ctx->b.chip_class,
420 NULL, NULL);
421 }
422
423 if (scs->flushed) {
424 ac_parse_ib(f, scs->gfx.ib + chunk->gfx_begin,
425 chunk->gfx_end - chunk->gfx_begin,
426 &last_trace_id, map ? 1 : 0, "IB", ctx->b.chip_class,
427 NULL, NULL);
428 } else {
429 si_parse_current_ib(f, ctx->b.gfx_cs, chunk->gfx_begin,
430 chunk->gfx_end, &last_trace_id, map ? 1 : 0,
431 "IB", ctx->b.chip_class);
432 }
433 }
434
435 if (chunk->dump_bo_list) {
436 fprintf(f, "Flushing. Time: ");
437 util_dump_ns(f, scs->time_flush);
438 fprintf(f, "\n\n");
439 si_dump_bo_list(ctx, &scs->gfx, f);
440 }
441 }
442
443 static const struct u_log_chunk_type si_log_chunk_type_cs = {
444 .destroy = si_log_chunk_type_cs_destroy,
445 .print = si_log_chunk_type_cs_print,
446 };
447
448 static void si_log_cs(struct si_context *ctx, struct u_log_context *log,
449 bool dump_bo_list)
450 {
451 assert(ctx->current_saved_cs);
452
453 struct si_saved_cs *scs = ctx->current_saved_cs;
454 unsigned gfx_cur = ctx->b.gfx_cs->prev_dw + ctx->b.gfx_cs->current.cdw;
455
456 if (!dump_bo_list &&
457 gfx_cur == scs->gfx_last_dw)
458 return;
459
460 struct si_log_chunk_cs *chunk = calloc(1, sizeof(*chunk));
461
462 chunk->ctx = ctx;
463 si_saved_cs_reference(&chunk->cs, scs);
464 chunk->dump_bo_list = dump_bo_list;
465
466 chunk->gfx_begin = scs->gfx_last_dw;
467 chunk->gfx_end = gfx_cur;
468 scs->gfx_last_dw = gfx_cur;
469
470 u_log_chunk(log, &si_log_chunk_type_cs, chunk);
471 }
472
473 void si_auto_log_cs(void *data, struct u_log_context *log)
474 {
475 struct si_context *ctx = (struct si_context *)data;
476 si_log_cs(ctx, log, false);
477 }
478
479 void si_log_hw_flush(struct si_context *sctx)
480 {
481 if (!sctx->b.log)
482 return;
483
484 si_log_cs(sctx, sctx->b.log, true);
485 }
486
487 static const char *priority_to_string(enum radeon_bo_priority priority)
488 {
489 #define ITEM(x) [RADEON_PRIO_##x] = #x
490 static const char *table[64] = {
491 ITEM(FENCE),
492 ITEM(TRACE),
493 ITEM(SO_FILLED_SIZE),
494 ITEM(QUERY),
495 ITEM(IB1),
496 ITEM(IB2),
497 ITEM(DRAW_INDIRECT),
498 ITEM(INDEX_BUFFER),
499 ITEM(VCE),
500 ITEM(UVD),
501 ITEM(SDMA_BUFFER),
502 ITEM(SDMA_TEXTURE),
503 ITEM(CP_DMA),
504 ITEM(CONST_BUFFER),
505 ITEM(DESCRIPTORS),
506 ITEM(BORDER_COLORS),
507 ITEM(SAMPLER_BUFFER),
508 ITEM(VERTEX_BUFFER),
509 ITEM(SHADER_RW_BUFFER),
510 ITEM(COMPUTE_GLOBAL),
511 ITEM(SAMPLER_TEXTURE),
512 ITEM(SHADER_RW_IMAGE),
513 ITEM(SAMPLER_TEXTURE_MSAA),
514 ITEM(COLOR_BUFFER),
515 ITEM(DEPTH_BUFFER),
516 ITEM(COLOR_BUFFER_MSAA),
517 ITEM(DEPTH_BUFFER_MSAA),
518 ITEM(CMASK),
519 ITEM(DCC),
520 ITEM(HTILE),
521 ITEM(SHADER_BINARY),
522 ITEM(SHADER_RINGS),
523 ITEM(SCRATCH_BUFFER),
524 };
525 #undef ITEM
526
527 assert(priority < ARRAY_SIZE(table));
528 return table[priority];
529 }
530
531 static int bo_list_compare_va(const struct radeon_bo_list_item *a,
532 const struct radeon_bo_list_item *b)
533 {
534 return a->vm_address < b->vm_address ? -1 :
535 a->vm_address > b->vm_address ? 1 : 0;
536 }
537
538 static void si_dump_bo_list(struct si_context *sctx,
539 const struct radeon_saved_cs *saved, FILE *f)
540 {
541 unsigned i,j;
542
543 if (!saved->bo_list)
544 return;
545
546 /* Sort the list according to VM adddresses first. */
547 qsort(saved->bo_list, saved->bo_count,
548 sizeof(saved->bo_list[0]), (void*)bo_list_compare_va);
549
550 fprintf(f, "Buffer list (in units of pages = 4kB):\n"
551 COLOR_YELLOW " Size VM start page "
552 "VM end page Usage" COLOR_RESET "\n");
553
554 for (i = 0; i < saved->bo_count; i++) {
555 /* Note: Buffer sizes are expected to be aligned to 4k by the winsys. */
556 const unsigned page_size = sctx->b.screen->info.gart_page_size;
557 uint64_t va = saved->bo_list[i].vm_address;
558 uint64_t size = saved->bo_list[i].bo_size;
559 bool hit = false;
560
561 /* If there's unused virtual memory between 2 buffers, print it. */
562 if (i) {
563 uint64_t previous_va_end = saved->bo_list[i-1].vm_address +
564 saved->bo_list[i-1].bo_size;
565
566 if (va > previous_va_end) {
567 fprintf(f, " %10"PRIu64" -- hole --\n",
568 (va - previous_va_end) / page_size);
569 }
570 }
571
572 /* Print the buffer. */
573 fprintf(f, " %10"PRIu64" 0x%013"PRIX64" 0x%013"PRIX64" ",
574 size / page_size, va / page_size, (va + size) / page_size);
575
576 /* Print the usage. */
577 for (j = 0; j < 64; j++) {
578 if (!(saved->bo_list[i].priority_usage & (1ull << j)))
579 continue;
580
581 fprintf(f, "%s%s", !hit ? "" : ", ", priority_to_string(j));
582 hit = true;
583 }
584 fprintf(f, "\n");
585 }
586 fprintf(f, "\nNote: The holes represent memory not used by the IB.\n"
587 " Other buffers can still be allocated there.\n\n");
588 }
589
590 static void si_dump_framebuffer(struct si_context *sctx, struct u_log_context *log)
591 {
592 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
593 struct r600_texture *rtex;
594 int i;
595
596 for (i = 0; i < state->nr_cbufs; i++) {
597 if (!state->cbufs[i])
598 continue;
599
600 rtex = (struct r600_texture*)state->cbufs[i]->texture;
601 u_log_printf(log, COLOR_YELLOW "Color buffer %i:" COLOR_RESET "\n", i);
602 si_print_texture_info(sctx->b.screen, rtex, log);
603 u_log_printf(log, "\n");
604 }
605
606 if (state->zsbuf) {
607 rtex = (struct r600_texture*)state->zsbuf->texture;
608 u_log_printf(log, COLOR_YELLOW "Depth-stencil buffer:" COLOR_RESET "\n");
609 si_print_texture_info(sctx->b.screen, rtex, log);
610 u_log_printf(log, "\n");
611 }
612 }
613
614 typedef unsigned (*slot_remap_func)(unsigned);
615
616 struct si_log_chunk_desc_list {
617 /** Pointer to memory map of buffer where the list is uploader */
618 uint32_t *gpu_list;
619 /** Reference of buffer where the list is uploaded, so that gpu_list
620 * is kept live. */
621 struct r600_resource *buf;
622
623 const char *shader_name;
624 const char *elem_name;
625 slot_remap_func slot_remap;
626 enum chip_class chip_class;
627 unsigned element_dw_size;
628 unsigned num_elements;
629
630 uint32_t list[0];
631 };
632
633 static void
634 si_log_chunk_desc_list_destroy(void *data)
635 {
636 struct si_log_chunk_desc_list *chunk = data;
637 r600_resource_reference(&chunk->buf, NULL);
638 FREE(chunk);
639 }
640
641 static void
642 si_log_chunk_desc_list_print(void *data, FILE *f)
643 {
644 struct si_log_chunk_desc_list *chunk = data;
645
646 for (unsigned i = 0; i < chunk->num_elements; i++) {
647 unsigned cpu_dw_offset = i * chunk->element_dw_size;
648 unsigned gpu_dw_offset = chunk->slot_remap(i) * chunk->element_dw_size;
649 const char *list_note = chunk->gpu_list ? "GPU list" : "CPU list";
650 uint32_t *cpu_list = chunk->list + cpu_dw_offset;
651 uint32_t *gpu_list = chunk->gpu_list ? chunk->gpu_list + gpu_dw_offset : cpu_list;
652
653 fprintf(f, COLOR_GREEN "%s%s slot %u (%s):" COLOR_RESET "\n",
654 chunk->shader_name, chunk->elem_name, i, list_note);
655
656 switch (chunk->element_dw_size) {
657 case 4:
658 for (unsigned j = 0; j < 4; j++)
659 ac_dump_reg(f, chunk->chip_class,
660 R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
661 gpu_list[j], 0xffffffff);
662 break;
663 case 8:
664 for (unsigned j = 0; j < 8; j++)
665 ac_dump_reg(f, chunk->chip_class,
666 R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
667 gpu_list[j], 0xffffffff);
668
669 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
670 for (unsigned j = 0; j < 4; j++)
671 ac_dump_reg(f, chunk->chip_class,
672 R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
673 gpu_list[4+j], 0xffffffff);
674 break;
675 case 16:
676 for (unsigned j = 0; j < 8; j++)
677 ac_dump_reg(f, chunk->chip_class,
678 R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
679 gpu_list[j], 0xffffffff);
680
681 fprintf(f, COLOR_CYAN " Buffer:" COLOR_RESET "\n");
682 for (unsigned j = 0; j < 4; j++)
683 ac_dump_reg(f, chunk->chip_class,
684 R_008F00_SQ_BUF_RSRC_WORD0 + j*4,
685 gpu_list[4+j], 0xffffffff);
686
687 fprintf(f, COLOR_CYAN " FMASK:" COLOR_RESET "\n");
688 for (unsigned j = 0; j < 8; j++)
689 ac_dump_reg(f, chunk->chip_class,
690 R_008F10_SQ_IMG_RSRC_WORD0 + j*4,
691 gpu_list[8+j], 0xffffffff);
692
693 fprintf(f, COLOR_CYAN " Sampler state:" COLOR_RESET "\n");
694 for (unsigned j = 0; j < 4; j++)
695 ac_dump_reg(f, chunk->chip_class,
696 R_008F30_SQ_IMG_SAMP_WORD0 + j*4,
697 gpu_list[12+j], 0xffffffff);
698 break;
699 }
700
701 if (memcmp(gpu_list, cpu_list, chunk->element_dw_size * 4) != 0) {
702 fprintf(f, COLOR_RED "!!!!! This slot was corrupted in GPU memory !!!!!"
703 COLOR_RESET "\n");
704 }
705
706 fprintf(f, "\n");
707 }
708
709 }
710
711 static const struct u_log_chunk_type si_log_chunk_type_descriptor_list = {
712 .destroy = si_log_chunk_desc_list_destroy,
713 .print = si_log_chunk_desc_list_print,
714 };
715
716 static void si_dump_descriptor_list(struct si_screen *screen,
717 struct si_descriptors *desc,
718 const char *shader_name,
719 const char *elem_name,
720 unsigned element_dw_size,
721 unsigned num_elements,
722 slot_remap_func slot_remap,
723 struct u_log_context *log)
724 {
725 if (!desc->list)
726 return;
727
728 /* In some cases, the caller doesn't know how many elements are really
729 * uploaded. Reduce num_elements to fit in the range of active slots. */
730 unsigned active_range_dw_begin =
731 desc->first_active_slot * desc->element_dw_size;
732 unsigned active_range_dw_end =
733 active_range_dw_begin + desc->num_active_slots * desc->element_dw_size;
734
735 while (num_elements > 0) {
736 int i = slot_remap(num_elements - 1);
737 unsigned dw_begin = i * element_dw_size;
738 unsigned dw_end = dw_begin + element_dw_size;
739
740 if (dw_begin >= active_range_dw_begin && dw_end <= active_range_dw_end)
741 break;
742
743 num_elements--;
744 }
745
746 struct si_log_chunk_desc_list *chunk =
747 CALLOC_VARIANT_LENGTH_STRUCT(si_log_chunk_desc_list,
748 4 * element_dw_size * num_elements);
749 chunk->shader_name = shader_name;
750 chunk->elem_name = elem_name;
751 chunk->element_dw_size = element_dw_size;
752 chunk->num_elements = num_elements;
753 chunk->slot_remap = slot_remap;
754 chunk->chip_class = screen->info.chip_class;
755
756 r600_resource_reference(&chunk->buf, desc->buffer);
757 chunk->gpu_list = desc->gpu_list;
758
759 for (unsigned i = 0; i < num_elements; ++i) {
760 memcpy(&chunk->list[i * element_dw_size],
761 &desc->list[slot_remap(i) * element_dw_size],
762 4 * element_dw_size);
763 }
764
765 u_log_chunk(log, &si_log_chunk_type_descriptor_list, chunk);
766 }
767
768 static unsigned si_identity(unsigned slot)
769 {
770 return slot;
771 }
772
773 static void si_dump_descriptors(struct si_context *sctx,
774 enum pipe_shader_type processor,
775 const struct tgsi_shader_info *info,
776 struct u_log_context *log)
777 {
778 struct si_descriptors *descs =
779 &sctx->descriptors[SI_DESCS_FIRST_SHADER +
780 processor * SI_NUM_SHADER_DESCS];
781 static const char *shader_name[] = {"VS", "PS", "GS", "TCS", "TES", "CS"};
782 const char *name = shader_name[processor];
783 unsigned enabled_constbuf, enabled_shaderbuf, enabled_samplers;
784 unsigned enabled_images;
785
786 if (info) {
787 enabled_constbuf = info->const_buffers_declared;
788 enabled_shaderbuf = info->shader_buffers_declared;
789 enabled_samplers = info->samplers_declared;
790 enabled_images = info->images_declared;
791 } else {
792 enabled_constbuf = sctx->const_and_shader_buffers[processor].enabled_mask >>
793 SI_NUM_SHADER_BUFFERS;
794 enabled_shaderbuf = sctx->const_and_shader_buffers[processor].enabled_mask &
795 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS);
796 enabled_shaderbuf = util_bitreverse(enabled_shaderbuf) >>
797 (32 - SI_NUM_SHADER_BUFFERS);
798 enabled_samplers = sctx->samplers[processor].enabled_mask;
799 enabled_images = sctx->images[processor].enabled_mask;
800 }
801
802 if (processor == PIPE_SHADER_VERTEX &&
803 sctx->vb_descriptors_buffer &&
804 sctx->vb_descriptors_gpu_list &&
805 sctx->vertex_elements) {
806 assert(info); /* only CS may not have an info struct */
807 struct si_descriptors desc = {};
808
809 desc.buffer = sctx->vb_descriptors_buffer;
810 desc.list = sctx->vb_descriptors_gpu_list;
811 desc.gpu_list = sctx->vb_descriptors_gpu_list;
812 desc.element_dw_size = 4;
813 desc.num_active_slots = sctx->vertex_elements->desc_list_byte_size / 16;
814
815 si_dump_descriptor_list(sctx->screen, &desc, name,
816 " - Vertex buffer", 4, info->num_inputs,
817 si_identity, log);
818 }
819
820 si_dump_descriptor_list(sctx->screen,
821 &descs[SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS],
822 name, " - Constant buffer", 4,
823 util_last_bit(enabled_constbuf),
824 si_get_constbuf_slot, log);
825 si_dump_descriptor_list(sctx->screen,
826 &descs[SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS],
827 name, " - Shader buffer", 4,
828 util_last_bit(enabled_shaderbuf),
829 si_get_shaderbuf_slot, log);
830 si_dump_descriptor_list(sctx->screen,
831 &descs[SI_SHADER_DESCS_SAMPLERS_AND_IMAGES],
832 name, " - Sampler", 16,
833 util_last_bit(enabled_samplers),
834 si_get_sampler_slot, log);
835 si_dump_descriptor_list(sctx->screen,
836 &descs[SI_SHADER_DESCS_SAMPLERS_AND_IMAGES],
837 name, " - Image", 8,
838 util_last_bit(enabled_images),
839 si_get_image_slot, log);
840 }
841
842 static void si_dump_gfx_descriptors(struct si_context *sctx,
843 const struct si_shader_ctx_state *state,
844 struct u_log_context *log)
845 {
846 if (!state->cso || !state->current)
847 return;
848
849 si_dump_descriptors(sctx, state->cso->type, &state->cso->info, log);
850 }
851
852 static void si_dump_compute_descriptors(struct si_context *sctx,
853 struct u_log_context *log)
854 {
855 if (!sctx->cs_shader_state.program)
856 return;
857
858 si_dump_descriptors(sctx, PIPE_SHADER_COMPUTE, NULL, log);
859 }
860
861 struct si_shader_inst {
862 char text[160]; /* one disasm line */
863 unsigned offset; /* instruction offset */
864 unsigned size; /* instruction size = 4 or 8 */
865 };
866
867 /* Split a disassembly string into lines and add them to the array pointed
868 * to by "instructions". */
869 static void si_add_split_disasm(const char *disasm,
870 uint64_t start_addr,
871 unsigned *num,
872 struct si_shader_inst *instructions)
873 {
874 struct si_shader_inst *last_inst = *num ? &instructions[*num - 1] : NULL;
875 char *next;
876
877 while ((next = strchr(disasm, '\n'))) {
878 struct si_shader_inst *inst = &instructions[*num];
879 unsigned len = next - disasm;
880
881 assert(len < ARRAY_SIZE(inst->text));
882 memcpy(inst->text, disasm, len);
883 inst->text[len] = 0;
884 inst->offset = last_inst ? last_inst->offset + last_inst->size : 0;
885
886 const char *semicolon = strchr(disasm, ';');
887 assert(semicolon);
888 /* More than 16 chars after ";" means the instruction is 8 bytes long. */
889 inst->size = next - semicolon > 16 ? 8 : 4;
890
891 snprintf(inst->text + len, ARRAY_SIZE(inst->text) - len,
892 " [PC=0x%"PRIx64", off=%u, size=%u]",
893 start_addr + inst->offset, inst->offset, inst->size);
894
895 last_inst = inst;
896 (*num)++;
897 disasm = next + 1;
898 }
899 }
900
901 /* If the shader is being executed, print its asm instructions, and annotate
902 * those that are being executed right now with information about waves that
903 * execute them. This is most useful during a GPU hang.
904 */
905 static void si_print_annotated_shader(struct si_shader *shader,
906 struct ac_wave_info *waves,
907 unsigned num_waves,
908 FILE *f)
909 {
910 if (!shader || !shader->binary.disasm_string)
911 return;
912
913 uint64_t start_addr = shader->bo->gpu_address;
914 uint64_t end_addr = start_addr + shader->bo->b.b.width0;
915 unsigned i;
916
917 /* See if any wave executes the shader. */
918 for (i = 0; i < num_waves; i++) {
919 if (start_addr <= waves[i].pc && waves[i].pc <= end_addr)
920 break;
921 }
922 if (i == num_waves)
923 return; /* the shader is not being executed */
924
925 /* Remember the first found wave. The waves are sorted according to PC. */
926 waves = &waves[i];
927 num_waves -= i;
928
929 /* Get the list of instructions.
930 * Buffer size / 4 is the upper bound of the instruction count.
931 */
932 unsigned num_inst = 0;
933 struct si_shader_inst *instructions =
934 calloc(shader->bo->b.b.width0 / 4, sizeof(struct si_shader_inst));
935
936 if (shader->prolog) {
937 si_add_split_disasm(shader->prolog->binary.disasm_string,
938 start_addr, &num_inst, instructions);
939 }
940 if (shader->previous_stage) {
941 si_add_split_disasm(shader->previous_stage->binary.disasm_string,
942 start_addr, &num_inst, instructions);
943 }
944 if (shader->prolog2) {
945 si_add_split_disasm(shader->prolog2->binary.disasm_string,
946 start_addr, &num_inst, instructions);
947 }
948 si_add_split_disasm(shader->binary.disasm_string,
949 start_addr, &num_inst, instructions);
950 if (shader->epilog) {
951 si_add_split_disasm(shader->epilog->binary.disasm_string,
952 start_addr, &num_inst, instructions);
953 }
954
955 fprintf(f, COLOR_YELLOW "%s - annotated disassembly:" COLOR_RESET "\n",
956 si_get_shader_name(shader, shader->selector->type));
957
958 /* Print instructions with annotations. */
959 for (i = 0; i < num_inst; i++) {
960 struct si_shader_inst *inst = &instructions[i];
961
962 fprintf(f, "%s\n", inst->text);
963
964 /* Print which waves execute the instruction right now. */
965 while (num_waves && start_addr + inst->offset == waves->pc) {
966 fprintf(f,
967 " " COLOR_GREEN "^ SE%u SH%u CU%u "
968 "SIMD%u WAVE%u EXEC=%016"PRIx64 " ",
969 waves->se, waves->sh, waves->cu, waves->simd,
970 waves->wave, waves->exec);
971
972 if (inst->size == 4) {
973 fprintf(f, "INST32=%08X" COLOR_RESET "\n",
974 waves->inst_dw0);
975 } else {
976 fprintf(f, "INST64=%08X %08X" COLOR_RESET "\n",
977 waves->inst_dw0, waves->inst_dw1);
978 }
979
980 waves->matched = true;
981 waves = &waves[1];
982 num_waves--;
983 }
984 }
985
986 fprintf(f, "\n\n");
987 free(instructions);
988 }
989
990 static void si_dump_annotated_shaders(struct si_context *sctx, FILE *f)
991 {
992 struct ac_wave_info waves[AC_MAX_WAVES_PER_CHIP];
993 unsigned num_waves = ac_get_wave_info(waves);
994
995 fprintf(f, COLOR_CYAN "The number of active waves = %u" COLOR_RESET
996 "\n\n", num_waves);
997
998 si_print_annotated_shader(sctx->vs_shader.current, waves, num_waves, f);
999 si_print_annotated_shader(sctx->tcs_shader.current, waves, num_waves, f);
1000 si_print_annotated_shader(sctx->tes_shader.current, waves, num_waves, f);
1001 si_print_annotated_shader(sctx->gs_shader.current, waves, num_waves, f);
1002 si_print_annotated_shader(sctx->ps_shader.current, waves, num_waves, f);
1003
1004 /* Print waves executing shaders that are not currently bound. */
1005 unsigned i;
1006 bool found = false;
1007 for (i = 0; i < num_waves; i++) {
1008 if (waves[i].matched)
1009 continue;
1010
1011 if (!found) {
1012 fprintf(f, COLOR_CYAN
1013 "Waves not executing currently-bound shaders:"
1014 COLOR_RESET "\n");
1015 found = true;
1016 }
1017 fprintf(f, " SE%u SH%u CU%u SIMD%u WAVE%u EXEC=%016"PRIx64
1018 " INST=%08X %08X PC=%"PRIx64"\n",
1019 waves[i].se, waves[i].sh, waves[i].cu, waves[i].simd,
1020 waves[i].wave, waves[i].exec, waves[i].inst_dw0,
1021 waves[i].inst_dw1, waves[i].pc);
1022 }
1023 if (found)
1024 fprintf(f, "\n\n");
1025 }
1026
1027 static void si_dump_command(const char *title, const char *command, FILE *f)
1028 {
1029 char line[2000];
1030
1031 FILE *p = popen(command, "r");
1032 if (!p)
1033 return;
1034
1035 fprintf(f, COLOR_YELLOW "%s: " COLOR_RESET "\n", title);
1036 while (fgets(line, sizeof(line), p))
1037 fputs(line, f);
1038 fprintf(f, "\n\n");
1039 pclose(p);
1040 }
1041
1042 static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
1043 unsigned flags)
1044 {
1045 struct si_context *sctx = (struct si_context*)ctx;
1046
1047 if (sctx->b.log)
1048 u_log_flush(sctx->b.log);
1049
1050 if (flags & PIPE_DUMP_DEVICE_STATUS_REGISTERS) {
1051 si_dump_debug_registers(sctx, f);
1052
1053 si_dump_annotated_shaders(sctx, f);
1054 si_dump_command("Active waves (raw data)", "umr -wa | column -t", f);
1055 si_dump_command("Wave information", "umr -O bits -wa", f);
1056 }
1057 }
1058
1059 void si_log_draw_state(struct si_context *sctx, struct u_log_context *log)
1060 {
1061 if (!log)
1062 return;
1063
1064 si_dump_framebuffer(sctx, log);
1065
1066 si_dump_gfx_shader(sctx, &sctx->vs_shader, log);
1067 si_dump_gfx_shader(sctx, &sctx->tcs_shader, log);
1068 si_dump_gfx_shader(sctx, &sctx->tes_shader, log);
1069 si_dump_gfx_shader(sctx, &sctx->gs_shader, log);
1070 si_dump_gfx_shader(sctx, &sctx->ps_shader, log);
1071
1072 si_dump_descriptor_list(sctx->screen,
1073 &sctx->descriptors[SI_DESCS_RW_BUFFERS],
1074 "", "RW buffers", 4, SI_NUM_RW_BUFFERS,
1075 si_identity, log);
1076 si_dump_gfx_descriptors(sctx, &sctx->vs_shader, log);
1077 si_dump_gfx_descriptors(sctx, &sctx->tcs_shader, log);
1078 si_dump_gfx_descriptors(sctx, &sctx->tes_shader, log);
1079 si_dump_gfx_descriptors(sctx, &sctx->gs_shader, log);
1080 si_dump_gfx_descriptors(sctx, &sctx->ps_shader, log);
1081 }
1082
1083 void si_log_compute_state(struct si_context *sctx, struct u_log_context *log)
1084 {
1085 if (!log)
1086 return;
1087
1088 si_dump_compute_shader(sctx, log);
1089 si_dump_compute_descriptors(sctx, log);
1090 }
1091
1092 static void si_dump_dma(struct si_context *sctx,
1093 struct radeon_saved_cs *saved, FILE *f)
1094 {
1095 static const char ib_name[] = "sDMA IB";
1096 unsigned i;
1097
1098 si_dump_bo_list(sctx, saved, f);
1099
1100 fprintf(f, "------------------ %s begin ------------------\n", ib_name);
1101
1102 for (i = 0; i < saved->num_dw; ++i) {
1103 fprintf(f, " %08x\n", saved->ib[i]);
1104 }
1105
1106 fprintf(f, "------------------- %s end -------------------\n", ib_name);
1107 fprintf(f, "\n");
1108
1109 fprintf(f, "SDMA Dump Done.\n");
1110 }
1111
1112 void si_check_vm_faults(struct si_context *sctx,
1113 struct radeon_saved_cs *saved, enum ring_type ring)
1114 {
1115 struct pipe_screen *screen = sctx->b.b.screen;
1116 FILE *f;
1117 uint64_t addr;
1118 char cmd_line[4096];
1119
1120 if (!ac_vm_fault_occured(sctx->b.chip_class,
1121 &sctx->dmesg_timestamp, &addr))
1122 return;
1123
1124 f = dd_get_debug_file(false);
1125 if (!f)
1126 return;
1127
1128 fprintf(f, "VM fault report.\n\n");
1129 if (os_get_command_line(cmd_line, sizeof(cmd_line)))
1130 fprintf(f, "Command: %s\n", cmd_line);
1131 fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
1132 fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
1133 fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
1134 fprintf(f, "Failing VM page: 0x%08"PRIx64"\n\n", addr);
1135
1136 if (sctx->apitrace_call_number)
1137 fprintf(f, "Last apitrace call: %u\n\n",
1138 sctx->apitrace_call_number);
1139
1140 switch (ring) {
1141 case RING_GFX: {
1142 struct u_log_context log;
1143 u_log_context_init(&log);
1144
1145 si_log_draw_state(sctx, &log);
1146 si_log_compute_state(sctx, &log);
1147 si_log_cs(sctx, &log, true);
1148
1149 u_log_new_page_print(&log, f);
1150 u_log_context_destroy(&log);
1151 break;
1152 }
1153 case RING_DMA:
1154 si_dump_dma(sctx, saved, f);
1155 break;
1156
1157 default:
1158 break;
1159 }
1160
1161 fclose(f);
1162
1163 fprintf(stderr, "Detected a VM fault, exiting...\n");
1164 exit(0);
1165 }
1166
1167 void si_init_debug_functions(struct si_context *sctx)
1168 {
1169 sctx->b.b.dump_debug_state = si_dump_debug_state;
1170
1171 /* Set the initial dmesg timestamp for this context, so that
1172 * only new messages will be checked for VM faults.
1173 */
1174 if (sctx->screen->debug_flags & DBG(CHECK_VM))
1175 ac_vm_fault_occured(sctx->b.chip_class,
1176 &sctx->dmesg_timestamp, NULL);
1177 }