radv/gfx9: allocate events from uncached VA space
[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
34 bool
35 radv_init_trace(struct radv_device *device)
36 {
37 struct radeon_winsys *ws = device->ws;
38
39 device->trace_bo = ws->buffer_create(ws, 4096, 8, RADEON_DOMAIN_VRAM,
40 RADEON_FLAG_CPU_ACCESS);
41 if (!device->trace_bo)
42 return false;
43
44 device->trace_id_ptr = ws->buffer_map(device->trace_bo);
45 if (!device->trace_id_ptr)
46 return false;
47
48 ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
49 &device->dmesg_timestamp, NULL);
50
51 return true;
52 }
53
54 static void
55 radv_dump_trace(struct radv_device *device, struct radeon_winsys_cs *cs)
56 {
57 const char *filename = getenv("RADV_TRACE_FILE");
58 FILE *f = fopen(filename, "w");
59
60 if (!f) {
61 fprintf(stderr, "Failed to write trace dump to %s\n", filename);
62 return;
63 }
64
65 fprintf(f, "Trace ID: %x\n", *device->trace_id_ptr);
66 device->ws->cs_dump(cs, f, (const int*)device->trace_id_ptr, 2);
67 fclose(f);
68 }
69
70 static bool
71 radv_gpu_hang_occured(struct radv_queue *queue)
72 {
73 struct radeon_winsys *ws = queue->device->ws;
74 enum ring_type ring;
75
76 ring = radv_queue_family_to_ring(queue->queue_family_index);
77
78 if (!ws->ctx_wait_idle(queue->hw_ctx, ring, queue->queue_idx))
79 return true;
80
81 return false;
82 }
83
84 void
85 radv_check_gpu_hangs(struct radv_queue *queue, struct radeon_winsys_cs *cs)
86 {
87 struct radv_device *device = queue->device;
88 uint64_t addr;
89
90 if (!radv_gpu_hang_occured(queue))
91 return;
92
93 if (ac_vm_fault_occured(device->physical_device->rad_info.chip_class,
94 &device->dmesg_timestamp, &addr)) {
95 fprintf(stderr, "VM fault report.\n\n");
96 fprintf(stderr, "Failing VM page: 0x%08"PRIx64"\n\n", addr);
97 }
98
99 radv_dump_trace(queue->device, cs);
100 abort();
101 }
102
103 void
104 radv_print_spirv(struct radv_shader_module *module, FILE *fp)
105 {
106 char path[] = "/tmp/fileXXXXXX";
107 char line[2048], command[128];
108 FILE *p;
109 int fd;
110
111 /* Dump the binary into a temporary file. */
112 fd = mkstemp(path);
113 if (fd < 0)
114 return;
115
116 if (write(fd, module->data, module->size) == -1)
117 goto fail;
118
119 sprintf(command, "spirv-dis %s", path);
120
121 /* Disassemble using spirv-dis if installed. */
122 p = popen(command, "r");
123 if (p) {
124 while (fgets(line, sizeof(line), p))
125 fprintf(fp, "%s", line);
126 pclose(p);
127 }
128
129 fail:
130 close(fd);
131 unlink(path);
132 }