#include "si_shader.h"
#include "sid.h"
#include "sid_tables.h"
+#include "radeon/radeon_elf_util.h"
+#include "ddebug/dd_util.h"
+#include "util/u_memory.h"
+DEBUG_GET_ONCE_OPTION(replace_shaders, "RADEON_REPLACE_SHADERS", NULL)
-static void si_dump_shader(struct si_shader_selector *sel, const char *name,
- FILE *f)
+static void si_dump_shader(struct si_screen *sscreen,
+ struct si_shader_ctx_state *state, FILE *f)
{
- if (!sel || !sel->current)
+ if (!state->cso || !state->current)
return;
- fprintf(f, "%s shader disassembly:\n", name);
- si_dump_shader_key(sel->type, &sel->current->key, f);
- fprintf(f, "%s\n\n", sel->current->binary.disasm_string);
+ si_dump_shader_key(state->cso->type, &state->current->key, f);
+ si_shader_dump(sscreen, state->current, NULL,
+ state->cso->info.processor, f);
+}
+
+/**
+ * Shader compiles can be overridden with arbitrary ELF objects by setting
+ * the environment variable RADEON_REPLACE_SHADERS=num1:filename1[;num2:filename2]
+ */
+bool si_replace_shader(unsigned num, struct radeon_shader_binary *binary)
+{
+ const char *p = debug_get_option_replace_shaders();
+ const char *semicolon;
+ char *copy = NULL;
+ FILE *f;
+ long filesize, nread;
+ char *buf = NULL;
+ bool replaced = false;
+
+ if (!p)
+ return false;
+
+ while (*p) {
+ unsigned long i;
+ char *endp;
+ i = strtoul(p, &endp, 0);
+
+ p = endp;
+ if (*p != ':') {
+ fprintf(stderr, "RADEON_REPLACE_SHADERS formatted badly.\n");
+ exit(1);
+ }
+ ++p;
+
+ if (i == num)
+ break;
+
+ p = strchr(p, ';');
+ if (!p)
+ return false;
+ ++p;
+ }
+ if (!*p)
+ return false;
+
+ semicolon = strchr(p, ';');
+ if (semicolon) {
+ p = copy = strndup(p, semicolon - p);
+ if (!copy) {
+ fprintf(stderr, "out of memory\n");
+ return false;
+ }
+ }
+
+ fprintf(stderr, "radeonsi: replace shader %u by %s\n", num, p);
+
+ f = fopen(p, "r");
+ if (!f) {
+ perror("radeonsi: failed to open file");
+ goto out_free;
+ }
+
+ if (fseek(f, 0, SEEK_END) != 0)
+ goto file_error;
+
+ filesize = ftell(f);
+ if (filesize < 0)
+ goto file_error;
+
+ if (fseek(f, 0, SEEK_SET) != 0)
+ goto file_error;
+
+ buf = MALLOC(filesize);
+ if (!buf) {
+ fprintf(stderr, "out of memory\n");
+ goto out_close;
+ }
+
+ nread = fread(buf, 1, filesize, f);
+ if (nread != filesize)
+ goto file_error;
+
+ radeon_elf_read(buf, filesize, binary);
+ replaced = true;
+
+out_close:
+ fclose(f);
+out_free:
+ FREE(buf);
+ free(copy);
+ return replaced;
+
+file_error:
+ perror("radeonsi: reading shader");
+ goto out_close;
}
/* Parsed IBs are difficult to read without colors. Use "less -R file" to
static void print_value(FILE *file, uint32_t value, int bits)
{
/* Guess if it's int or float */
- if (value <= (1 << 15))
- fprintf(file, "%u\n", value);
- else {
+ if (value <= (1 << 15)) {
+ if (value <= 9)
+ fprintf(file, "%u\n", value);
+ else
+ fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
+ } else {
float f = uif(value);
if (fabs(f) < 100000 && f*10 == floor(f*10))
- fprintf(file, "%.1ff\n", f);
+ fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
else
/* Don't print more leading zeros than there are bits. */
fprintf(file, "0x%0*x\n", bits / 4, value);
{
int r, f;
- for (r = 0; r < ARRAY_SIZE(reg_table); r++) {
- const struct si_reg *reg = ®_table[r];
+ for (r = 0; r < ARRAY_SIZE(sid_reg_table); r++) {
+ const struct si_reg *reg = &sid_reg_table[r];
+ const char *reg_name = sid_strings + reg->name_offset;
if (reg->offset == offset) {
bool first_field = true;
print_spaces(file, INDENT_PKT);
fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
- reg->name);
+ reg_name);
if (!reg->num_fields) {
print_value(file, value, 32);
}
for (f = 0; f < reg->num_fields; f++) {
- const struct si_field *field = ®->fields[f];
+ const struct si_field *field = sid_fields_table + reg->fields_offset + f;
+ const int *values_offsets = sid_strings_offsets + field->values_offset;
uint32_t val = (value & field->mask) >>
(ffs(field->mask) - 1);
/* Indent the field. */
if (!first_field)
print_spaces(file,
- INDENT_PKT + strlen(reg->name) + 4);
+ INDENT_PKT + strlen(reg_name) + 4);
/* Print the field. */
- fprintf(file, "%s = ", field->name);
+ fprintf(file, "%s = ", sid_strings + field->name_offset);
- if (val < field->num_values && field->values[val])
- fprintf(file, "%s\n", field->values[val]);
+ if (val < field->num_values && values_offsets[val] >= 0)
+ fprintf(file, "%s\n", sid_strings + values_offsets[val]);
else
print_value(file, val,
util_bitcount(field->mask));
if (packet3_table[i].op == op)
break;
- if (i < ARRAY_SIZE(packet3_table))
+ if (i < ARRAY_SIZE(packet3_table)) {
+ const char *name = sid_strings + packet3_table[i].name_offset;
+
if (op == PKT3_SET_CONTEXT_REG ||
op == PKT3_SET_CONFIG_REG ||
op == PKT3_SET_UCONFIG_REG ||
op == PKT3_SET_SH_REG)
fprintf(f, COLOR_CYAN "%s%s" COLOR_CYAN ":\n",
- packet3_table[i].name, predicate);
+ name, predicate);
else
fprintf(f, COLOR_GREEN "%s%s" COLOR_RESET ":\n",
- packet3_table[i].name, predicate);
- else
+ name, predicate);
+ } else
fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s" COLOR_RESET ":\n",
op, predicate);
si_dump_reg(f, R_504_DST_ADDR_HI, ib[5], ~0);
si_dump_reg(f, R_414_COMMAND, ib[6], ~0);
break;
+ case PKT3_INDIRECT_BUFFER_SI:
+ case PKT3_INDIRECT_BUFFER_CONST:
+ case PKT3_INDIRECT_BUFFER_CIK:
+ si_dump_reg(f, R_3F0_IB_BASE_LO, ib[1], ~0);
+ si_dump_reg(f, R_3F1_IB_BASE_HI, ib[2], ~0);
+ si_dump_reg(f, R_3F2_CONTROL, ib[3], ~0);
+ break;
case PKT3_NOP:
if (ib[0] == 0xffff1000) {
count = -1; /* One dword NOP. */
* \param trace_id the last trace ID that is known to have been reached
* and executed by the CP, typically read from a buffer
*/
-static void si_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id)
+static void si_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id,
+ const char *name)
{
- fprintf(f, "------------------ IB begin ------------------\n");
+ fprintf(f, "------------------ %s begin ------------------\n", name);
while (num_dw > 0) {
unsigned type = PKT_TYPE_G(ib[0]);
}
}
- fprintf(f, "------------------- IB end -------------------\n");
+ fprintf(f, "------------------- %s end -------------------\n", name);
if (num_dw < 0) {
printf("Packet ends after the end of IB.\n");
exit(0);
}
+ fprintf(f, "\n");
}
static void si_dump_mmapped_reg(struct si_context *sctx, FILE *f,
fprintf(f, "\n");
}
+static void si_dump_last_ib(struct si_context *sctx, FILE *f)
+{
+ int last_trace_id = -1;
+
+ if (!sctx->last_gfx.ib)
+ return;
+
+ if (sctx->last_trace_buf) {
+ /* We are expecting that the ddebug pipe has already
+ * waited for the context, so this buffer should be idle.
+ * If the GPU is hung, there is no point in waiting for it.
+ */
+ uint32_t *map = sctx->b.ws->buffer_map(sctx->last_trace_buf->buf,
+ NULL,
+ PIPE_TRANSFER_UNSYNCHRONIZED |
+ PIPE_TRANSFER_READ);
+ if (map)
+ last_trace_id = *map;
+ }
+
+ if (sctx->init_config)
+ si_parse_ib(f, sctx->init_config->pm4, sctx->init_config->ndw,
+ -1, "IB2: Init config");
+
+ if (sctx->init_config_gs_rings)
+ si_parse_ib(f, sctx->init_config_gs_rings->pm4,
+ sctx->init_config_gs_rings->ndw,
+ -1, "IB2: Init GS rings");
+
+ si_parse_ib(f, sctx->last_gfx.ib, sctx->last_gfx.num_dw,
+ last_trace_id, "IB");
+}
+
+static const char *priority_to_string(enum radeon_bo_priority priority)
+{
+#define ITEM(x) [RADEON_PRIO_##x] = #x
+ static const char *table[64] = {
+ ITEM(FENCE),
+ ITEM(TRACE),
+ ITEM(SO_FILLED_SIZE),
+ ITEM(QUERY),
+ ITEM(IB1),
+ ITEM(IB2),
+ ITEM(DRAW_INDIRECT),
+ ITEM(INDEX_BUFFER),
+ ITEM(CP_DMA),
+ ITEM(VCE),
+ ITEM(UVD),
+ ITEM(SDMA_BUFFER),
+ ITEM(SDMA_TEXTURE),
+ ITEM(USER_SHADER),
+ ITEM(INTERNAL_SHADER),
+ ITEM(CONST_BUFFER),
+ ITEM(DESCRIPTORS),
+ ITEM(BORDER_COLORS),
+ ITEM(SAMPLER_BUFFER),
+ ITEM(VERTEX_BUFFER),
+ ITEM(SHADER_RW_BUFFER),
+ ITEM(RINGS_STREAMOUT),
+ ITEM(SCRATCH_BUFFER),
+ ITEM(COMPUTE_GLOBAL),
+ ITEM(SAMPLER_TEXTURE),
+ ITEM(SHADER_RW_IMAGE),
+ ITEM(SAMPLER_TEXTURE_MSAA),
+ ITEM(COLOR_BUFFER),
+ ITEM(DEPTH_BUFFER),
+ ITEM(COLOR_BUFFER_MSAA),
+ ITEM(DEPTH_BUFFER_MSAA),
+ ITEM(CMASK),
+ ITEM(DCC),
+ ITEM(HTILE),
+ };
+#undef ITEM
+
+ assert(priority < ARRAY_SIZE(table));
+ return table[priority];
+}
+
+static int bo_list_compare_va(const struct radeon_bo_list_item *a,
+ const struct radeon_bo_list_item *b)
+{
+ return a->vm_address < b->vm_address ? -1 :
+ a->vm_address > b->vm_address ? 1 : 0;
+}
+
+static void si_dump_bo_list(struct si_context *sctx,
+ const struct radeon_saved_cs *saved, FILE *f)
+{
+ unsigned i,j;
+
+ if (!saved->bo_list)
+ return;
+
+ /* Sort the list according to VM adddresses first. */
+ qsort(saved->bo_list, saved->bo_count,
+ sizeof(saved->bo_list[0]), (void*)bo_list_compare_va);
+
+ fprintf(f, "Buffer list (in units of pages = 4kB):\n"
+ COLOR_YELLOW " Size VM start page "
+ "VM end page Usage" COLOR_RESET "\n");
+
+ for (i = 0; i < saved->bo_count; i++) {
+ /* Note: Buffer sizes are expected to be aligned to 4k by the winsys. */
+ const unsigned page_size = sctx->b.screen->info.gart_page_size;
+ uint64_t va = saved->bo_list[i].vm_address;
+ uint64_t size = saved->bo_list[i].buf->size;
+ bool hit = false;
+
+ /* If there's unused virtual memory between 2 buffers, print it. */
+ if (i) {
+ uint64_t previous_va_end = saved->bo_list[i-1].vm_address +
+ saved->bo_list[i-1].buf->size;
+
+ if (va > previous_va_end) {
+ fprintf(f, " %10"PRIu64" -- hole --\n",
+ (va - previous_va_end) / page_size);
+ }
+ }
+
+ /* Print the buffer. */
+ fprintf(f, " %10"PRIu64" 0x%013"PRIx64" 0x%013"PRIx64" ",
+ size / page_size, va / page_size, (va + size) / page_size);
+
+ /* Print the usage. */
+ for (j = 0; j < 64; j++) {
+ if (!(saved->bo_list[i].priority_usage & (1llu << j)))
+ continue;
+
+ fprintf(f, "%s%s", !hit ? "" : ", ", priority_to_string(j));
+ hit = true;
+ }
+ fprintf(f, "\n");
+ }
+ fprintf(f, "\nNote: The holes represent memory not used by the IB.\n"
+ " Other buffers can still be allocated there.\n\n");
+}
+
+static void si_dump_framebuffer(struct si_context *sctx, FILE *f)
+{
+ struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
+ struct r600_texture *rtex;
+ int i;
+
+ for (i = 0; i < state->nr_cbufs; i++) {
+ if (!state->cbufs[i])
+ continue;
+
+ rtex = (struct r600_texture*)state->cbufs[i]->texture;
+ fprintf(f, COLOR_YELLOW "Color buffer %i:" COLOR_RESET "\n", i);
+ r600_print_texture_info(rtex, f);
+ fprintf(f, "\n");
+ }
+
+ if (state->zsbuf) {
+ rtex = (struct r600_texture*)state->zsbuf->texture;
+ fprintf(f, COLOR_YELLOW "Depth-stencil buffer:" COLOR_RESET "\n");
+ r600_print_texture_info(rtex, f);
+ fprintf(f, "\n");
+ }
+}
+
static void si_dump_debug_state(struct pipe_context *ctx, FILE *f,
unsigned flags)
{
if (flags & PIPE_DEBUG_DEVICE_IS_HUNG)
si_dump_debug_registers(sctx, f);
- si_dump_shader(sctx->vs_shader, "Vertex", f);
- si_dump_shader(sctx->tcs_shader, "Tessellation control", f);
- si_dump_shader(sctx->tes_shader, "Tessellation evaluation", f);
- si_dump_shader(sctx->gs_shader, "Geometry", f);
- si_dump_shader(sctx->ps_shader, "Fragment", f);
-
- if (sctx->last_ib) {
- int last_trace_id = -1;
-
- if (sctx->last_trace_buf) {
- /* We are expecting that the ddebug pipe has already
- * waited for the context, so this buffer should be idle.
- * If the GPU is hung, there is no point in waiting for it.
- */
- uint32_t *map =
- sctx->b.ws->buffer_map(sctx->last_trace_buf->cs_buf,
- NULL,
- PIPE_TRANSFER_UNSYNCHRONIZED |
- PIPE_TRANSFER_READ);
- if (map)
- last_trace_id = *map;
+ si_dump_framebuffer(sctx, f);
+ si_dump_shader(sctx->screen, &sctx->vs_shader, f);
+ si_dump_shader(sctx->screen, &sctx->tcs_shader, f);
+ si_dump_shader(sctx->screen, &sctx->tes_shader, f);
+ si_dump_shader(sctx->screen, &sctx->gs_shader, f);
+ si_dump_shader(sctx->screen, &sctx->ps_shader, f);
+
+ si_dump_bo_list(sctx, &sctx->last_gfx, f);
+ si_dump_last_ib(sctx, f);
+
+ fprintf(f, "Done.\n");
+
+ /* dump only once */
+ radeon_clear_saved_cs(&sctx->last_gfx);
+ r600_resource_reference(&sctx->last_trace_buf, NULL);
+}
+
+static bool si_vm_fault_occured(struct si_context *sctx, uint32_t *out_addr)
+{
+ char line[2000];
+ unsigned sec, usec;
+ int progress = 0;
+ uint64_t timestamp = 0;
+ bool fault = false;
+
+ FILE *p = popen("dmesg", "r");
+ if (!p)
+ return false;
+
+ while (fgets(line, sizeof(line), p)) {
+ char *msg, len;
+
+ if (!line[0] || line[0] == '\n')
+ continue;
+
+ /* Get the timestamp. */
+ if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
+ assert(0);
+ continue;
}
+ timestamp = sec * 1000000llu + usec;
+
+ /* If just updating the timestamp. */
+ if (!out_addr)
+ continue;
+
+ /* Process messages only if the timestamp is newer. */
+ if (timestamp <= sctx->dmesg_timestamp)
+ continue;
+
+ /* Only process the first VM fault. */
+ if (fault)
+ continue;
+
+ /* Remove trailing \n */
+ len = strlen(line);
+ if (len && line[len-1] == '\n')
+ line[len-1] = 0;
+
+ /* Get the message part. */
+ msg = strchr(line, ']');
+ if (!msg) {
+ assert(0);
+ continue;
+ }
+ msg++;
- si_parse_ib(f, sctx->last_ib, sctx->last_ib_dw_size,
- last_trace_id);
- free(sctx->last_ib); /* dump only once */
- sctx->last_ib = NULL;
- r600_resource_reference(&sctx->last_trace_buf, NULL);
+ switch (progress) {
+ case 0:
+ if (strstr(msg, "GPU fault detected:"))
+ progress = 1;
+ break;
+ case 1:
+ msg = strstr(msg, "VM_CONTEXT1_PROTECTION_FAULT_ADDR");
+ if (msg) {
+ msg = strstr(msg, "0x");
+ if (msg) {
+ msg += 2;
+ if (sscanf(msg, "%X", out_addr) == 1)
+ fault = true;
+ }
+ }
+ progress = 0;
+ break;
+ default:
+ progress = 0;
+ }
}
+ pclose(p);
- fprintf(f, "Done.\n");
+ if (timestamp > sctx->dmesg_timestamp)
+ sctx->dmesg_timestamp = timestamp;
+ return fault;
+}
+
+void si_check_vm_faults(struct si_context *sctx)
+{
+ struct pipe_screen *screen = sctx->b.b.screen;
+ FILE *f;
+ uint32_t addr;
+
+ if (!si_vm_fault_occured(sctx, &addr))
+ return;
+
+ f = dd_get_debug_file(false);
+ if (!f)
+ return;
+
+ fprintf(f, "VM fault report.\n\n");
+ fprintf(f, "Driver vendor: %s\n", screen->get_vendor(screen));
+ fprintf(f, "Device vendor: %s\n", screen->get_device_vendor(screen));
+ fprintf(f, "Device name: %s\n\n", screen->get_name(screen));
+ fprintf(f, "Failing VM page: 0x%08x\n\n", addr);
+
+ si_dump_debug_state(&sctx->b.b, f, 0);
+ fclose(f);
+
+ fprintf(stderr, "Detected a VM fault, exiting...\n");
+ exit(0);
}
void si_init_debug_functions(struct si_context *sctx)
{
sctx->b.b.dump_debug_state = si_dump_debug_state;
+
+ /* Set the initial dmesg timestamp for this context, so that
+ * only new messages will be checked for VM faults.
+ */
+ if (sctx->screen->b.debug_flags & DBG_CHECK_VM)
+ si_vm_fault_occured(sctx, NULL);
}