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