radv: initialize the trace BO to 0
[mesa.git] / src / amd / vulkan / radv_debug.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "ac_debug.h"
32 #include "radv_debug.h"
33 #include "radv_shader.h"
34
35 #define TRACE_BO_SIZE 4096
36
37 bool
38 radv_init_trace(struct radv_device *device)
39 {
40 struct radeon_winsys *ws = device->ws;
41
42 device->trace_bo = ws->buffer_create(ws, TRACE_BO_SIZE, 8,
43 RADEON_DOMAIN_VRAM,
44 RADEON_FLAG_CPU_ACCESS);
45 if (!device->trace_bo)
46 return false;
47
48 device->trace_id_ptr = ws->buffer_map(device->trace_bo);
49 if (!device->trace_id_ptr)
50 return false;
51
52 memset(device->trace_id_ptr, 0, TRACE_BO_SIZE);
53
54 ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
55 &device->dmesg_timestamp, NULL);
56
57 return true;
58 }
59
60 static void
61 radv_dump_trace(struct radv_device *device, struct radeon_winsys_cs *cs)
62 {
63 const char *filename = getenv("RADV_TRACE_FILE");
64 FILE *f = fopen(filename, "w");
65
66 if (!f) {
67 fprintf(stderr, "Failed to write trace dump to %s\n", filename);
68 return;
69 }
70
71 fprintf(f, "Trace ID: %x\n", *device->trace_id_ptr);
72 device->ws->cs_dump(cs, f, (const int*)device->trace_id_ptr, 2);
73 fclose(f);
74 }
75
76 static bool
77 radv_gpu_hang_occured(struct radv_queue *queue)
78 {
79 struct radeon_winsys *ws = queue->device->ws;
80 enum ring_type ring;
81
82 ring = radv_queue_family_to_ring(queue->queue_family_index);
83
84 if (!ws->ctx_wait_idle(queue->hw_ctx, ring, queue->queue_idx))
85 return true;
86
87 return false;
88 }
89
90 void
91 radv_check_gpu_hangs(struct radv_queue *queue, struct radeon_winsys_cs *cs)
92 {
93 struct radv_device *device = queue->device;
94 uint64_t addr;
95
96 bool hang_occurred = radv_gpu_hang_occured(queue);
97 bool vm_fault_occurred = false;
98 if (queue->device->instance->debug_flags & RADV_DEBUG_VM_FAULTS)
99 vm_fault_occurred = ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
100 &device->dmesg_timestamp, &addr);
101 if (!hang_occurred && !vm_fault_occurred)
102 return;
103
104 if (vm_fault_occurred) {
105 fprintf(stderr, "VM fault report.\n\n");
106 fprintf(stderr, "Failing VM page: 0x%08"PRIx64"\n\n", addr);
107 }
108
109 radv_dump_trace(queue->device, cs);
110 abort();
111 }
112
113 void
114 radv_print_spirv(struct radv_shader_module *module, FILE *fp)
115 {
116 char path[] = "/tmp/fileXXXXXX";
117 char line[2048], command[128];
118 FILE *p;
119 int fd;
120
121 /* Dump the binary into a temporary file. */
122 fd = mkstemp(path);
123 if (fd < 0)
124 return;
125
126 if (write(fd, module->data, module->size) == -1)
127 goto fail;
128
129 sprintf(command, "spirv-dis %s", path);
130
131 /* Disassemble using spirv-dis if installed. */
132 p = popen(command, "r");
133 if (p) {
134 while (fgets(line, sizeof(line), p))
135 fprintf(fp, "%s", line);
136 pclose(p);
137 }
138
139 fail:
140 close(fd);
141 unlink(path);
142 }