radeonsi: also dump shaders on a VM fault
[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_shader.h"
29 #include "sid.h"
30 #include "sid_tables.h"
31 #include "radeon/radeon_elf_util.h"
32 #include "ddebug/dd_util.h"
33 #include "util/u_memory.h"
34
35 DEBUG_GET_ONCE_OPTION(replace_shaders, "RADEON_REPLACE_SHADERS", NULL)
36
37 static void si_dump_shader(struct si_screen *sscreen,
38 struct si_shader_ctx_state *state, FILE *f)
39 {
40 if (!state->cso || !state->current)
41 return;
42
43 si_dump_shader_key(state->cso->type, &state->current->key, f);
44 si_shader_dump(sscreen, state->current, NULL,
45 state->cso->info.processor, f);
46 }
47
48 /**
49 * Shader compiles can be overridden with arbitrary ELF objects by setting
50 * the environment variable RADEON_REPLACE_SHADERS=num1:filename1[;num2:filename2]
51 */
52 bool si_replace_shader(unsigned num, struct radeon_shader_binary *binary)
53 {
54 const char *p = debug_get_option_replace_shaders();
55 const char *semicolon;
56 char *copy = NULL;
57 FILE *f;
58 long filesize, nread;
59 char *buf = NULL;
60 bool replaced = false;
61
62 if (!p)
63 return false;
64
65 while (*p) {
66 unsigned long i;
67 char *endp;
68 i = strtoul(p, &endp, 0);
69
70 p = endp;
71 if (*p != ':') {
72 fprintf(stderr, "RADEON_REPLACE_SHADERS formatted badly.\n");
73 exit(1);
74 }
75 ++p;
76
77 if (i == num)
78 break;
79
80 p = strchr(p, ';');
81 if (!p)
82 return false;
83 ++p;
84 }
85 if (!*p)
86 return false;
87
88 semicolon = strchr(p, ';');
89 if (semicolon) {
90 p = copy = strndup(p, semicolon - p);
91 if (!copy) {
92 fprintf(stderr, "out of memory\n");
93 return false;
94 }
95 }
96
97 fprintf(stderr, "radeonsi: replace shader %u by %s\n", num, p);
98
99 f = fopen(p, "r");
100 if (!f) {
101 perror("radeonsi: failed to open file");
102 goto out_free;
103 }
104
105 if (fseek(f, 0, SEEK_END) != 0)
106 goto file_error;
107
108 filesize = ftell(f);
109 if (filesize < 0)
110 goto file_error;
111
112 if (fseek(f, 0, SEEK_SET) != 0)
113 goto file_error;
114
115 buf = MALLOC(filesize);
116 if (!buf) {
117 fprintf(stderr, "out of memory\n");
118 goto out_close;
119 }
120
121 nread = fread(buf, 1, filesize, f);
122 if (nread != filesize)
123 goto file_error;
124
125 radeon_elf_read(buf, filesize, binary);
126 replaced = true;
127
128 out_close:
129 fclose(f);
130 out_free:
131 FREE(buf);
132 free(copy);
133 return replaced;
134
135 file_error:
136 perror("radeonsi: reading shader");
137 goto out_close;
138 }
139
140 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
141 * read them, or use "aha -b -f file" to convert them to html.
142 */
143 #define COLOR_RESET "\033[0m"
144 #define COLOR_RED "\033[31m"
145 #define COLOR_GREEN "\033[1;32m"
146 #define COLOR_YELLOW "\033[1;33m"
147 #define COLOR_CYAN "\033[1;36m"
148
149 #define INDENT_PKT 8
150
151 static void print_spaces(FILE *f, unsigned num)
152 {
153 fprintf(f, "%*s", num, "");
154 }
155
156 static void print_value(FILE *file, uint32_t value, int bits)
157 {
158 /* Guess if it's int or float */
159 if (value <= (1 << 15)) {
160 if (value <= 9)
161 fprintf(file, "%u\n", value);
162 else
163 fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
164 } else {
165 float f = uif(value);
166
167 if (fabs(f) < 100000 && f*10 == floor(f*10))
168 fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
169 else
170 /* Don't print more leading zeros than there are bits. */
171 fprintf(file, "0x%0*x\n", bits / 4, value);
172 }
173 }
174
175 static void print_named_value(FILE *file, const char *name, uint32_t value,
176 int bits)
177 {
178 print_spaces(file, INDENT_PKT);
179 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ", name);
180 print_value(file, value, bits);
181 }
182
183 static void si_dump_reg(FILE *file, unsigned offset, uint32_t value,
184 uint32_t field_mask)
185 {
186 int r, f;
187
188 for (r = 0; r < ARRAY_SIZE(reg_table); r++) {
189 const struct si_reg *reg = &reg_table[r];
190
191 if (reg->offset == offset) {
192 bool first_field = true;
193
194 print_spaces(file, INDENT_PKT);
195 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
196 reg->name);
197
198 if (!reg->num_fields) {
199 print_value(file, value, 32);
200 return;
201 }
202
203 for (f = 0; f < reg->num_fields; f++) {
204 const struct si_field *field = &reg->fields[f];
205 uint32_t val = (value & field->mask) >>
206 (ffs(field->mask) - 1);
207
208 if (!(field->mask & field_mask))
209 continue;
210
211 /* Indent the field. */
212 if (!first_field)
213 print_spaces(file,
214 INDENT_PKT + strlen(reg->name) + 4);
215
216 /* Print the field. */
217 fprintf(file, "%s = ", field->name);
218
219 if (val < field->num_values && field->values[val])
220 fprintf(file, "%s\n", field->values[val]);
221 else
222 print_value(file, val,
223 util_bitcount(field->mask));
224
225 first_field = false;
226 }
227 return;
228 }
229 }
230
231 fprintf(file, COLOR_YELLOW "0x%05x" COLOR_RESET " = 0x%08x", offset, value);
232 }
233
234 static void si_parse_set_reg_packet(FILE *f, uint32_t *ib, unsigned count,
235 unsigned reg_offset)
236 {
237 unsigned reg = (ib[1] << 2) + reg_offset;
238 int i;
239
240 for (i = 0; i < count; i++)
241 si_dump_reg(f, reg + i*4, ib[2+i], ~0);
242 }
243
244 static uint32_t *si_parse_packet3(FILE *f, uint32_t *ib, int *num_dw,
245 int trace_id)
246 {
247 unsigned count = PKT_COUNT_G(ib[0]);
248 unsigned op = PKT3_IT_OPCODE_G(ib[0]);
249 const char *predicate = PKT3_PREDICATE(ib[0]) ? "(predicate)" : "";
250 int i;
251
252 /* Print the name first. */
253 for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
254 if (packet3_table[i].op == op)
255 break;
256
257 if (i < ARRAY_SIZE(packet3_table))
258 if (op == PKT3_SET_CONTEXT_REG ||
259 op == PKT3_SET_CONFIG_REG ||
260 op == PKT3_SET_UCONFIG_REG ||
261 op == PKT3_SET_SH_REG)
262 fprintf(f, COLOR_CYAN "%s%s" COLOR_CYAN ":\n",
263 packet3_table[i].name, predicate);
264 else
265 fprintf(f, COLOR_GREEN "%s%s" COLOR_RESET ":\n",
266 packet3_table[i].name, predicate);
267 else
268 fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s" COLOR_RESET ":\n",
269 op, predicate);
270
271 /* Print the contents. */
272 switch (op) {
273 case PKT3_SET_CONTEXT_REG:
274 si_parse_set_reg_packet(f, ib, count, SI_CONTEXT_REG_OFFSET);
275 break;
276 case PKT3_SET_CONFIG_REG:
277 si_parse_set_reg_packet(f, ib, count, SI_CONFIG_REG_OFFSET);
278 break;
279 case PKT3_SET_UCONFIG_REG:
280 si_parse_set_reg_packet(f, ib, count, CIK_UCONFIG_REG_OFFSET);
281 break;
282 case PKT3_SET_SH_REG:
283 si_parse_set_reg_packet(f, ib, count, SI_SH_REG_OFFSET);
284 break;
285 case PKT3_DRAW_PREAMBLE:
286 si_dump_reg(f, R_030908_VGT_PRIMITIVE_TYPE, ib[1], ~0);
287 si_dump_reg(f, R_028AA8_IA_MULTI_VGT_PARAM, ib[2], ~0);
288 si_dump_reg(f, R_028B58_VGT_LS_HS_CONFIG, ib[3], ~0);
289 break;
290 case PKT3_ACQUIRE_MEM:
291 si_dump_reg(f, R_0301F0_CP_COHER_CNTL, ib[1], ~0);
292 si_dump_reg(f, R_0301F4_CP_COHER_SIZE, ib[2], ~0);
293 si_dump_reg(f, R_030230_CP_COHER_SIZE_HI, ib[3], ~0);
294 si_dump_reg(f, R_0301F8_CP_COHER_BASE, ib[4], ~0);
295 si_dump_reg(f, R_0301E4_CP_COHER_BASE_HI, ib[5], ~0);
296 print_named_value(f, "POLL_INTERVAL", ib[6], 16);
297 break;
298 case PKT3_SURFACE_SYNC:
299 si_dump_reg(f, R_0085F0_CP_COHER_CNTL, ib[1], ~0);
300 si_dump_reg(f, R_0085F4_CP_COHER_SIZE, ib[2], ~0);
301 si_dump_reg(f, R_0085F8_CP_COHER_BASE, ib[3], ~0);
302 print_named_value(f, "POLL_INTERVAL", ib[4], 16);
303 break;
304 case PKT3_EVENT_WRITE:
305 si_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, ib[1],
306 S_028A90_EVENT_TYPE(~0));
307 print_named_value(f, "EVENT_INDEX", (ib[1] >> 8) & 0xf, 4);
308 print_named_value(f, "INV_L2", (ib[1] >> 20) & 0x1, 1);
309 if (count > 0) {
310 print_named_value(f, "ADDRESS_LO", ib[2], 32);
311 print_named_value(f, "ADDRESS_HI", ib[3], 16);
312 }
313 break;
314 case PKT3_DRAW_INDEX_AUTO:
315 si_dump_reg(f, R_030930_VGT_NUM_INDICES, ib[1], ~0);
316 si_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[2], ~0);
317 break;
318 case PKT3_DRAW_INDEX_2:
319 si_dump_reg(f, R_028A78_VGT_DMA_MAX_SIZE, ib[1], ~0);
320 si_dump_reg(f, R_0287E8_VGT_DMA_BASE, ib[2], ~0);
321 si_dump_reg(f, R_0287E4_VGT_DMA_BASE_HI, ib[3], ~0);
322 si_dump_reg(f, R_030930_VGT_NUM_INDICES, ib[4], ~0);
323 si_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[5], ~0);
324 break;
325 case PKT3_INDEX_TYPE:
326 si_dump_reg(f, R_028A7C_VGT_DMA_INDEX_TYPE, ib[1], ~0);
327 break;
328 case PKT3_NUM_INSTANCES:
329 si_dump_reg(f, R_030934_VGT_NUM_INSTANCES, ib[1], ~0);
330 break;
331 case PKT3_WRITE_DATA:
332 si_dump_reg(f, R_370_CONTROL, ib[1], ~0);
333 si_dump_reg(f, R_371_DST_ADDR_LO, ib[2], ~0);
334 si_dump_reg(f, R_372_DST_ADDR_HI, ib[3], ~0);
335 for (i = 2; i < count; i++) {
336 print_spaces(f, INDENT_PKT);
337 fprintf(f, "0x%08x\n", ib[2+i]);
338 }
339 break;
340 case PKT3_CP_DMA:
341 si_dump_reg(f, R_410_CP_DMA_WORD0, ib[1], ~0);
342 si_dump_reg(f, R_411_CP_DMA_WORD1, ib[2], ~0);
343 si_dump_reg(f, R_412_CP_DMA_WORD2, ib[3], ~0);
344 si_dump_reg(f, R_413_CP_DMA_WORD3, ib[4], ~0);
345 si_dump_reg(f, R_414_COMMAND, ib[5], ~0);
346 break;
347 case PKT3_DMA_DATA:
348 si_dump_reg(f, R_500_DMA_DATA_WORD0, ib[1], ~0);
349 si_dump_reg(f, R_501_SRC_ADDR_LO, ib[2], ~0);
350 si_dump_reg(f, R_502_SRC_ADDR_HI, ib[3], ~0);
351 si_dump_reg(f, R_503_DST_ADDR_LO, ib[4], ~0);
352 si_dump_reg(f, R_504_DST_ADDR_HI, ib[5], ~0);
353 si_dump_reg(f, R_414_COMMAND, ib[6], ~0);
354 break;
355 case PKT3_NOP:
356 if (ib[0] == 0xffff1000) {
357 count = -1; /* One dword NOP. */
358 break;
359 } else if (count == 0 && SI_IS_TRACE_POINT(ib[1])) {
360 unsigned packet_id = SI_GET_TRACE_POINT_ID(ib[1]);
361
362 print_spaces(f, INDENT_PKT);
363 fprintf(f, COLOR_RED "Trace point ID: %u\n", packet_id);
364
365 if (trace_id == -1)
366 break; /* tracing was disabled */
367
368 print_spaces(f, INDENT_PKT);
369 if (packet_id < trace_id)
370 fprintf(f, COLOR_RED
371 "This trace point was reached by the CP."
372 COLOR_RESET "\n");
373 else if (packet_id == trace_id)
374 fprintf(f, COLOR_RED
375 "!!!!! This is the last trace point that "
376 "was reached by the CP !!!!!"
377 COLOR_RESET "\n");
378 else if (packet_id+1 == trace_id)
379 fprintf(f, COLOR_RED
380 "!!!!! This is the first trace point that "
381 "was NOT been reached by the CP !!!!!"
382 COLOR_RESET "\n");
383 else
384 fprintf(f, COLOR_RED
385 "!!!!! This trace point was NOT reached "
386 "by the CP !!!!!"
387 COLOR_RESET "\n");
388 break;
389 }
390 /* fall through, print all dwords */
391 default:
392 for (i = 0; i < count+1; i++) {
393 print_spaces(f, INDENT_PKT);
394 fprintf(f, "0x%08x\n", ib[1+i]);
395 }
396 }
397
398 ib += count + 2;
399 *num_dw -= count + 2;
400 return ib;
401 }
402
403 /**
404 * Parse and print an IB into a file.
405 *
406 * \param f file
407 * \param ib IB
408 * \param num_dw size of the IB
409 * \param chip_class chip class
410 * \param trace_id the last trace ID that is known to have been reached
411 * and executed by the CP, typically read from a buffer
412 */
413 static void si_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id,
414 const char *name)
415 {
416 fprintf(f, "------------------ %s begin ------------------\n", name);
417
418 while (num_dw > 0) {
419 unsigned type = PKT_TYPE_G(ib[0]);
420
421 switch (type) {
422 case 3:
423 ib = si_parse_packet3(f, ib, &num_dw, trace_id);
424 break;
425 case 2:
426 /* type-2 nop */
427 if (ib[0] == 0x80000000) {
428 fprintf(f, COLOR_GREEN "NOP (type 2)" COLOR_RESET "\n");
429 ib++;
430 break;
431 }
432 /* fall through */
433 default:
434 fprintf(f, "Unknown packet type %i\n", type);
435 return;
436 }
437 }
438
439 fprintf(f, "------------------- %s end -------------------\n", name);
440 if (num_dw < 0) {
441 printf("Packet ends after the end of IB.\n");
442 exit(0);
443 }
444 fprintf(f, "\n");
445 }
446
447 static void si_dump_mmapped_reg(struct si_context *sctx, FILE *f,
448 unsigned offset)
449 {
450 struct radeon_winsys *ws = sctx->b.ws;
451 uint32_t value;
452
453 if (ws->read_registers(ws, offset, 1, &value))
454 si_dump_reg(f, offset, value, ~0);
455 }
456
457 static void si_dump_debug_registers(struct si_context *sctx, FILE *f)
458 {
459 if (sctx->screen->b.info.drm_major == 2 &&
460 sctx->screen->b.info.drm_minor < 42)
461 return; /* no radeon support */
462
463 fprintf(f, "Memory-mapped registers:\n");
464 si_dump_mmapped_reg(sctx, f, R_008010_GRBM_STATUS);
465
466 /* No other registers can be read on DRM < 3.1.0. */
467 if (sctx->screen->b.info.drm_major < 3 ||
468 sctx->screen->b.info.drm_minor < 1) {
469 fprintf(f, "\n");
470 return;
471 }
472
473 si_dump_mmapped_reg(sctx, f, R_008008_GRBM_STATUS2);
474 si_dump_mmapped_reg(sctx, f, R_008014_GRBM_STATUS_SE0);
475 si_dump_mmapped_reg(sctx, f, R_008018_GRBM_STATUS_SE1);
476 si_dump_mmapped_reg(sctx, f, R_008038_GRBM_STATUS_SE2);
477 si_dump_mmapped_reg(sctx, f, R_00803C_GRBM_STATUS_SE3);
478 si_dump_mmapped_reg(sctx, f, R_00D034_SDMA0_STATUS_REG);
479 si_dump_mmapped_reg(sctx, f, R_00D834_SDMA1_STATUS_REG);
480 si_dump_mmapped_reg(sctx, f, R_000E50_SRBM_STATUS);
481 si_dump_mmapped_reg(sctx, f, R_000E4C_SRBM_STATUS2);
482 si_dump_mmapped_reg(sctx, f, R_000E54_SRBM_STATUS3);
483 si_dump_mmapped_reg(sctx, f, R_008680_CP_STAT);
484 si_dump_mmapped_reg(sctx, f, R_008674_CP_STALLED_STAT1);
485 si_dump_mmapped_reg(sctx, f, R_008678_CP_STALLED_STAT2);
486 si_dump_mmapped_reg(sctx, f, R_008670_CP_STALLED_STAT3);
487 si_dump_mmapped_reg(sctx, f, R_008210_CP_CPC_STATUS);
488 si_dump_mmapped_reg(sctx, f, R_008214_CP_CPC_BUSY_STAT);
489 si_dump_mmapped_reg(sctx, f, R_008218_CP_CPC_STALLED_STAT1);
490 si_dump_mmapped_reg(sctx, f, R_00821C_CP_CPF_STATUS);
491 si_dump_mmapped_reg(sctx, f, R_008220_CP_CPF_BUSY_STAT);
492 si_dump_mmapped_reg(sctx, f, R_008224_CP_CPF_STALLED_STAT1);
493 fprintf(f, "\n");
494 }
495
496 static void si_dump_last_ib(struct si_context *sctx, FILE *f)
497 {
498 int last_trace_id = -1;
499
500 if (!sctx->last_ib)
501 return;
502
503 if (sctx->last_trace_buf) {
504 /* We are expecting that the ddebug pipe has already
505 * waited for the context, so this buffer should be idle.
506 * If the GPU is hung, there is no point in waiting for it.
507 */
508 uint32_t *map = sctx->b.ws->buffer_map(sctx->last_trace_buf->buf,
509 NULL,
510 PIPE_TRANSFER_UNSYNCHRONIZED |
511 PIPE_TRANSFER_READ);
512 if (map)
513 last_trace_id = *map;
514 }
515
516 if (sctx->init_config)
517 si_parse_ib(f, sctx->init_config->pm4, sctx->init_config->ndw,
518 -1, "IB2: Init config");
519
520 if (sctx->init_config_gs_rings)
521 si_parse_ib(f, sctx->init_config_gs_rings->pm4,
522 sctx->init_config_gs_rings->ndw,
523 -1, "IB2: Init GS rings");
524
525 si_parse_ib(f, sctx->last_ib, sctx->last_ib_dw_size,
526 last_trace_id, "IB");
527 free(sctx->last_ib); /* dump only once */
528 sctx->last_ib = NULL;
529 r600_resource_reference(&sctx->last_trace_buf, NULL);
530 }
531
532 static const char *priority_to_string(enum radeon_bo_priority priority)
533 {
534 #define ITEM(x) [RADEON_PRIO_##x] = #x
535 static const char *table[64] = {
536 ITEM(FENCE),
537 ITEM(TRACE),
538 ITEM(SO_FILLED_SIZE),
539 ITEM(QUERY),
540 ITEM(IB1),
541 ITEM(IB2),
542 ITEM(DRAW_INDIRECT),
543 ITEM(INDEX_BUFFER),
544 ITEM(CP_DMA),
545 ITEM(VCE),
546 ITEM(UVD),
547 ITEM(SDMA_BUFFER),
548 ITEM(SDMA_TEXTURE),
549 ITEM(USER_SHADER),
550 ITEM(INTERNAL_SHADER),
551 ITEM(CONST_BUFFER),
552 ITEM(DESCRIPTORS),
553 ITEM(BORDER_COLORS),
554 ITEM(SAMPLER_BUFFER),
555 ITEM(VERTEX_BUFFER),
556 ITEM(SHADER_RW_BUFFER),
557 ITEM(RINGS_STREAMOUT),
558 ITEM(SCRATCH_BUFFER),
559 ITEM(COMPUTE_GLOBAL),
560 ITEM(SAMPLER_TEXTURE),
561 ITEM(SHADER_RW_IMAGE),
562 ITEM(SAMPLER_TEXTURE_MSAA),
563 ITEM(COLOR_BUFFER),
564 ITEM(DEPTH_BUFFER),
565 ITEM(COLOR_BUFFER_MSAA),
566 ITEM(DEPTH_BUFFER_MSAA),
567 ITEM(CMASK),
568 ITEM(DCC),
569 ITEM(HTILE),
570 };
571 #undef ITEM
572
573 assert(priority < ARRAY_SIZE(table));
574 return table[priority];
575 }
576
577 static int bo_list_compare_va(const struct radeon_bo_list_item *a,
578 const struct radeon_bo_list_item *b)
579 {
580 return a->vm_address < b->vm_address ? -1 :
581 a->vm_address > b->vm_address ? 1 : 0;
582 }
583
584 static void si_dump_last_bo_list(struct si_context *sctx, FILE *f)
585 {
586 unsigned i,j;
587
588 if (!sctx->last_bo_list)
589 return;
590
591 /* Sort the list according to VM adddresses first. */
592 qsort(sctx->last_bo_list, sctx->last_bo_count,
593 sizeof(sctx->last_bo_list[0]), (void*)bo_list_compare_va);
594
595 fprintf(f, "Buffer list (in units of pages = 4kB):\n"
596 COLOR_YELLOW " Size VM start page "
597 "VM end page Usage" COLOR_RESET "\n");
598
599 for (i = 0; i < sctx->last_bo_count; i++) {
600 /* Note: Buffer sizes are expected to be aligned to 4k by the winsys. */
601 const unsigned page_size = 4096;
602 uint64_t va = sctx->last_bo_list[i].vm_address;
603 uint64_t size = sctx->last_bo_list[i].buf->size;
604 bool hit = false;
605
606 /* If there's unused virtual memory between 2 buffers, print it. */
607 if (i) {
608 uint64_t previous_va_end = sctx->last_bo_list[i-1].vm_address +
609 sctx->last_bo_list[i-1].buf->size;
610
611 if (va > previous_va_end) {
612 fprintf(f, " %10"PRIu64" -- hole --\n",
613 (va - previous_va_end) / page_size);
614 }
615 }
616
617 /* Print the buffer. */
618 fprintf(f, " %10"PRIu64" 0x%013"PRIx64" 0x%013"PRIx64" ",
619 size / page_size, va / page_size, (va + size) / page_size);
620
621 /* Print the usage. */
622 for (j = 0; j < 64; j++) {
623 if (!(sctx->last_bo_list[i].priority_usage & (1llu << j)))
624 continue;
625
626 fprintf(f, "%s%s", !hit ? "" : ", ", priority_to_string(j));
627 hit = true;
628 }
629 fprintf(f, "\n");
630 }
631 fprintf(f, "\nNote: The holes represent memory not used by the IB.\n"
632 " Other buffers can still be allocated there.\n\n");
633
634 for (i = 0; i < sctx->last_bo_count; i++)
635 pb_reference(&sctx->last_bo_list[i].buf, NULL);
636 free(sctx->last_bo_list);
637 sctx->last_bo_list = NULL;
638 }
639
640 static void si_dump_framebuffer(struct si_context *sctx, FILE *f)
641 {
642 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
643 struct r600_texture *rtex;
644 int i;
645
646 for (i = 0; i < state->nr_cbufs; i++) {
647 if (!state->cbufs[i])
648 continue;
649
650 rtex = (struct r600_texture*)state->cbufs[i]->texture;
651 fprintf(f, COLOR_YELLOW "Color buffer %i:" COLOR_RESET "\n", i);
652 r600_print_texture_info(rtex, f);
653 fprintf(f, "\n");
654 }
655
656 if (state->zsbuf) {
657 rtex = (struct r600_texture*)state->zsbuf->texture;
658 fprintf(f, COLOR_YELLOW "Depth-stencil buffer:" COLOR_RESET "\n");
659 r600_print_texture_info(rtex, f);
660 fprintf(f, "\n");
661 }
662 }
663
664 static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
665 unsigned flags)
666 {
667 struct si_context *sctx = (struct si_context*)ctx;
668
669 if (flags & PIPE_DEBUG_DEVICE_IS_HUNG)
670 si_dump_debug_registers(sctx, f);
671
672 si_dump_framebuffer(sctx, f);
673 si_dump_shader(sctx->screen, &sctx->vs_shader, f);
674 si_dump_shader(sctx->screen, &sctx->tcs_shader, f);
675 si_dump_shader(sctx->screen, &sctx->tes_shader, f);
676 si_dump_shader(sctx->screen, &sctx->gs_shader, f);
677 si_dump_shader(sctx->screen, &sctx->ps_shader, f);
678
679 si_dump_last_bo_list(sctx, f);
680 si_dump_last_ib(sctx, f);
681
682 fprintf(f, "Done.\n");
683 }
684
685 static bool si_vm_fault_occured(struct si_context *sctx, uint32_t *out_addr)
686 {
687 char line[2000];
688 unsigned sec, usec;
689 int progress = 0;
690 uint64_t timestamp = 0;
691 bool fault = false;
692
693 FILE *p = popen("dmesg", "r");
694 if (!p)
695 return false;
696
697 while (fgets(line, sizeof(line), p)) {
698 char *msg, len;
699
700 /* Get the timestamp. */
701 if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
702 assert(0);
703 continue;
704 }
705 timestamp = sec * 1000000llu + usec;
706
707 /* If just updating the timestamp. */
708 if (!out_addr)
709 continue;
710
711 /* Process messages only if the timestamp is newer. */
712 if (timestamp <= sctx->dmesg_timestamp)
713 continue;
714
715 /* Only process the first VM fault. */
716 if (fault)
717 continue;
718
719 /* Remove trailing \n */
720 len = strlen(line);
721 if (len && line[len-1] == '\n')
722 line[len-1] = 0;
723
724 /* Get the message part. */
725 msg = strchr(line, ']');
726 if (!msg) {
727 assert(0);
728 continue;
729 }
730 msg++;
731
732 switch (progress) {
733 case 0:
734 if (strstr(msg, "GPU fault detected:"))
735 progress = 1;
736 break;
737 case 1:
738 msg = strstr(msg, "VM_CONTEXT1_PROTECTION_FAULT_ADDR");
739 if (msg) {
740 msg = strstr(msg, "0x");
741 if (msg) {
742 msg += 2;
743 if (sscanf(msg, "%X", out_addr) == 1)
744 fault = true;
745 }
746 }
747 progress = 0;
748 break;
749 default:
750 progress = 0;
751 }
752 }
753 pclose(p);
754
755 if (timestamp > sctx->dmesg_timestamp)
756 sctx->dmesg_timestamp = timestamp;
757 return fault;
758 }
759
760 void si_check_vm_faults(struct si_context *sctx)
761 {
762 struct pipe_screen *screen = sctx->b.b.screen;
763 FILE *f;
764 uint32_t addr;
765
766 /* Use conservative timeout 800ms, after which we won't wait any
767 * longer and assume the GPU is hung.
768 */
769 sctx->b.ws->fence_wait(sctx->b.ws, sctx->last_gfx_fence, 800*1000*1000);
770
771 if (!si_vm_fault_occured(sctx, &addr))
772 return;
773
774 f = dd_get_debug_file(false);
775 if (!f)
776 return;
777
778 fprintf(f, "VM fault report.\n\n");
779 fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
780 fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
781 fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
782 fprintf(f, "Failing VM page: 0x%08x\n\n", addr);
783
784 si_dump_debug_state(&sctx->b.b, f, 0);
785 fclose(f);
786
787 fprintf(stderr, "Detected a VM fault, exiting...\n");
788 exit(0);
789 }
790
791 void si_init_debug_functions(struct si_context *sctx)
792 {
793 sctx->b.b.dump_debug_state = si_dump_debug_state;
794
795 /* Set the initial dmesg timestamp for this context, so that
796 * only new messages will be checked for VM faults.
797 */
798 if (sctx->screen->b.debug_flags & DBG_CHECK_VM)
799 si_vm_fault_occured(sctx, NULL);
800 }