2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
32 #include "radv_debug.h"
33 #include "radv_shader.h"
35 #define TRACE_BO_SIZE 4096
37 /* Trace BO layout (offsets are 4 bytes):
39 * [0]: primary trace ID
40 * [1]: secondary trace ID
41 * [2-3]: 64-bit GFX pipeline pointer
42 * [4-5]: 64-bit COMPUTE pipeline pointer
46 radv_init_trace(struct radv_device
*device
)
48 struct radeon_winsys
*ws
= device
->ws
;
50 device
->trace_bo
= ws
->buffer_create(ws
, TRACE_BO_SIZE
, 8,
52 RADEON_FLAG_CPU_ACCESS
);
53 if (!device
->trace_bo
)
56 device
->trace_id_ptr
= ws
->buffer_map(device
->trace_bo
);
57 if (!device
->trace_id_ptr
)
60 memset(device
->trace_id_ptr
, 0, TRACE_BO_SIZE
);
62 ac_vm_fault_occured(device
->physical_device
->rad_info
.chip_class
,
63 &device
->dmesg_timestamp
, NULL
);
69 radv_dump_trace(struct radv_device
*device
, struct radeon_winsys_cs
*cs
)
71 const char *filename
= getenv("RADV_TRACE_FILE");
72 FILE *f
= fopen(filename
, "w");
75 fprintf(stderr
, "Failed to write trace dump to %s\n", filename
);
79 fprintf(f
, "Trace ID: %x\n", *device
->trace_id_ptr
);
80 device
->ws
->cs_dump(cs
, f
, (const int*)device
->trace_id_ptr
, 2);
85 radv_dump_shader(struct radv_shader_variant
*shader
, gl_shader_stage stage
,
91 fprintf(f
, "%s:\n%s\n\n", radv_get_shader_name(shader
, stage
),
92 shader
->disasm_string
);
96 radv_dump_shaders(struct radv_pipeline
*pipeline
,
97 struct radv_shader_variant
*compute_shader
, FILE *f
)
101 /* Dump active graphics shaders. */
102 mask
= pipeline
->active_stages
;
104 int stage
= u_bit_scan(&mask
);
106 radv_dump_shader(pipeline
->shaders
[stage
], stage
, f
);
109 radv_dump_shader(compute_shader
, MESA_SHADER_COMPUTE
, f
);
113 radv_dump_graphics_state(struct radv_pipeline
*graphics_pipeline
,
114 struct radv_pipeline
*compute_pipeline
, FILE *f
)
116 struct radv_shader_variant
*compute_shader
=
117 compute_pipeline
? compute_pipeline
->shaders
[MESA_SHADER_COMPUTE
] : NULL
;
119 if (!graphics_pipeline
)
122 radv_dump_shaders(graphics_pipeline
, compute_shader
, f
);
126 radv_dump_compute_state(struct radv_pipeline
*compute_pipeline
, FILE *f
)
128 if (!compute_pipeline
)
131 radv_dump_shaders(compute_pipeline
,
132 compute_pipeline
->shaders
[MESA_SHADER_COMPUTE
], f
);
135 static struct radv_pipeline
*
136 radv_get_saved_graphics_pipeline(struct radv_device
*device
)
138 uint64_t *ptr
= (uint64_t *)device
->trace_id_ptr
;
140 return (struct radv_pipeline
*)ptr
[1];
143 static struct radv_pipeline
*
144 radv_get_saved_compute_pipeline(struct radv_device
*device
)
146 uint64_t *ptr
= (uint64_t *)device
->trace_id_ptr
;
148 return (struct radv_pipeline
*)ptr
[2];
152 radv_gpu_hang_occured(struct radv_queue
*queue
, enum ring_type ring
)
154 struct radeon_winsys
*ws
= queue
->device
->ws
;
156 if (!ws
->ctx_wait_idle(queue
->hw_ctx
, ring
, queue
->queue_idx
))
163 radv_check_gpu_hangs(struct radv_queue
*queue
, struct radeon_winsys_cs
*cs
)
165 struct radv_pipeline
*graphics_pipeline
, *compute_pipeline
;
166 struct radv_device
*device
= queue
->device
;
170 ring
= radv_queue_family_to_ring(queue
->queue_family_index
);
172 bool hang_occurred
= radv_gpu_hang_occured(queue
, ring
);
173 bool vm_fault_occurred
= false;
174 if (queue
->device
->instance
->debug_flags
& RADV_DEBUG_VM_FAULTS
)
175 vm_fault_occurred
= ac_vm_fault_occured(device
->physical_device
->rad_info
.chip_class
,
176 &device
->dmesg_timestamp
, &addr
);
177 if (!hang_occurred
&& !vm_fault_occurred
)
180 graphics_pipeline
= radv_get_saved_graphics_pipeline(device
);
181 compute_pipeline
= radv_get_saved_compute_pipeline(device
);
183 if (vm_fault_occurred
) {
184 fprintf(stderr
, "VM fault report.\n\n");
185 fprintf(stderr
, "Failing VM page: 0x%08"PRIx64
"\n\n", addr
);
190 radv_dump_graphics_state(graphics_pipeline
, compute_pipeline
,
194 radv_dump_compute_state(compute_pipeline
, stderr
);
201 radv_dump_trace(queue
->device
, cs
);
206 radv_print_spirv(struct radv_shader_module
*module
, FILE *fp
)
208 char path
[] = "/tmp/fileXXXXXX";
209 char line
[2048], command
[128];
213 /* Dump the binary into a temporary file. */
218 if (write(fd
, module
->data
, module
->size
) == -1)
221 sprintf(command
, "spirv-dis %s", path
);
223 /* Disassemble using spirv-dis if installed. */
224 p
= popen(command
, "r");
226 while (fgets(line
, sizeof(line
), p
))
227 fprintf(fp
, "%s", line
);