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