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 #include "r600_pipe.h"
27 #include "evergreend.h"
28
29 #include "egd_tables.h"
30
31 #define AC_IS_TRACE_POINT(x) (((x) & 0xcafe0000) == 0xcafe0000)
32 #define AC_GET_TRACE_POINT_ID(x) ((x) & 0xffff)
33
34 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
35 * read them, or use "aha -b -f file" to convert them to html.
36 */
37 #define COLOR_RESET "\033[0m"
38 #define COLOR_RED "\033[31m"
39 #define COLOR_GREEN "\033[1;32m"
40 #define COLOR_YELLOW "\033[1;33m"
41 #define COLOR_CYAN "\033[1;36m"
42
43 #define INDENT_PKT 8
44
45 typedef void *(*ac_debug_addr_callback)(void *data, uint64_t addr);
print_spaces(FILE * f,unsigned num)46 static void print_spaces(FILE *f, unsigned num)
47 {
48 fprintf(f, "%*s", num, "");
49 }
50
print_value(FILE * file,uint32_t value,int bits)51 static void print_value(FILE *file, uint32_t value, int bits)
52 {
53 /* Guess if it's int or float */
54 if (value <= (1 << 15)) {
55 if (value <= 9)
56 fprintf(file, "%u\n", value);
57 else
58 fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
59 } else {
60 float f = uif(value);
61
62 if (fabs(f) < 100000 && f*10 == floor(f*10))
63 fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
64 else
65 /* Don't print more leading zeros than there are bits. */
66 fprintf(file, "0x%0*x\n", bits / 4, value);
67 }
68 }
69
print_named_value(FILE * file,const char * name,uint32_t value,int bits)70 static void print_named_value(FILE *file, const char *name, uint32_t value,
71 int bits)
72 {
73 print_spaces(file, INDENT_PKT);
74 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ", name);
75 print_value(file, value, bits);
76 }
77
eg_dump_reg(FILE * file,unsigned offset,uint32_t value,uint32_t field_mask)78 static void eg_dump_reg(FILE *file, unsigned offset, uint32_t value,
79 uint32_t field_mask)
80 {
81 unsigned r, f;
82
83 for (r = 0; r < ARRAY_SIZE(egd_reg_table); r++) {
84 const struct eg_reg *reg = &egd_reg_table[r];
85 const char *reg_name = egd_strings + reg->name_offset;
86
87 if (reg->offset == offset) {
88 bool first_field = true;
89
90 print_spaces(file, INDENT_PKT);
91 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
92 reg_name);
93
94 if (!reg->num_fields) {
95 print_value(file, value, 32);
96 return;
97 }
98
99 for (f = 0; f < reg->num_fields; f++) {
100 const struct eg_field *field = egd_fields_table + reg->fields_offset + f;
101 const int *values_offsets = egd_strings_offsets + field->values_offset;
102 uint32_t val = (value & field->mask) >>
103 (ffs(field->mask) - 1);
104
105 if (!(field->mask & field_mask))
106 continue;
107
108 /* Indent the field. */
109 if (!first_field)
110 print_spaces(file,
111 INDENT_PKT + strlen(reg_name) + 4);
112
113 /* Print the field. */
114 fprintf(file, "%s = ", egd_strings + field->name_offset);
115
116 if (val < field->num_values && values_offsets[val] >= 0)
117 fprintf(file, "%s\n", egd_strings + values_offsets[val]);
118 else
119 print_value(file, val,
120 util_bitcount(field->mask));
121
122 first_field = false;
123 }
124 return;
125 }
126 }
127
128 print_spaces(file, INDENT_PKT);
129 fprintf(file, COLOR_YELLOW "0x%05x" COLOR_RESET " <- 0x%08x\n", offset, value);
130 }
131
132
ac_parse_set_reg_packet(FILE * f,uint32_t * ib,unsigned count,unsigned reg_offset)133 static void ac_parse_set_reg_packet(FILE *f, uint32_t *ib, unsigned count,
134 unsigned reg_offset)
135 {
136 unsigned reg = (ib[1] << 2) + reg_offset;
137 unsigned i;
138
139 for (i = 0; i < count; i++)
140 eg_dump_reg(f, reg + i*4, ib[2+i], ~0);
141 }
142
ac_parse_packet3(FILE * f,uint32_t * ib,int * num_dw,int trace_id,enum chip_class chip_class,ac_debug_addr_callback addr_callback,void * addr_callback_data)143 static uint32_t *ac_parse_packet3(FILE *f, uint32_t *ib, int *num_dw,
144 int trace_id, enum chip_class chip_class,
145 ac_debug_addr_callback addr_callback,
146 void *addr_callback_data)
147 {
148 unsigned count = PKT_COUNT_G(ib[0]);
149 unsigned op = PKT3_IT_OPCODE_G(ib[0]);
150 const char *predicate = PKT3_PREDICATE(ib[0]) ? "(predicate)" : "";
151 const char *compute_mode = (ib[0] & 0x2) ? "(C)" : "";
152 unsigned i;
153
154 /* Print the name first. */
155 for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
156 if (packet3_table[i].op == op)
157 break;
158
159 if (i < ARRAY_SIZE(packet3_table)) {
160 const char *name = egd_strings + packet3_table[i].name_offset;
161
162 if (op == PKT3_SET_CONTEXT_REG ||
163 op == PKT3_SET_CONFIG_REG ||
164 op == PKT3_SET_UCONFIG_REG ||
165 op == PKT3_SET_SH_REG)
166 fprintf(f, COLOR_CYAN "%s%s%s" COLOR_CYAN ":\n",
167 name, compute_mode, predicate);
168 else
169 fprintf(f, COLOR_GREEN "%s%s%s" COLOR_RESET ":\n",
170 name, compute_mode, predicate);
171 } else
172 fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s%s" COLOR_RESET ":\n",
173 op, compute_mode, predicate);
174
175 /* Print the contents. */
176 switch (op) {
177 case PKT3_SET_CONTEXT_REG:
178 ac_parse_set_reg_packet(f, ib, count, EVERGREEN_CONTEXT_REG_OFFSET);
179 break;
180 case PKT3_SET_CONFIG_REG:
181 ac_parse_set_reg_packet(f, ib, count, EVERGREEN_CONFIG_REG_OFFSET);
182 break;
183 case PKT3_SURFACE_SYNC:
184 eg_dump_reg(f, R_0085F0_CP_COHER_CNTL, ib[1], ~0);
185 eg_dump_reg(f, R_0085F4_CP_COHER_SIZE, ib[2], ~0);
186 eg_dump_reg(f, R_0085F8_CP_COHER_BASE, ib[3], ~0);
187 print_named_value(f, "POLL_INTERVAL", ib[4], 16);
188 break;
189 case PKT3_EVENT_WRITE:
190 /* TODO dump VGT_EVENT_INITIATOR */
191 #if 0
192 eg_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, ib[1],
193 S_028A90_EVENT_TYPE(~0));
194 #endif
195 print_named_value(f, "EVENT_TYPE", ib[1] & 0xff, 8);
196 print_named_value(f, "EVENT_INDEX", (ib[1] >> 8) & 0xf, 4);
197 print_named_value(f, "INV_L2", (ib[1] >> 20) & 0x1, 1);
198 if (count > 0) {
199 print_named_value(f, "ADDRESS_LO", ib[2], 32);
200 print_named_value(f, "ADDRESS_HI", ib[3], 16);
201 }
202 break;
203 case PKT3_DRAW_INDEX_AUTO:
204 eg_dump_reg(f, R_008970_VGT_NUM_INDICES, ib[1], ~0);
205 eg_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[2], ~0);
206 break;
207 case PKT3_DRAW_INDEX_2:
208 eg_dump_reg(f, R_028A78_VGT_DMA_MAX_SIZE, ib[1], ~0);
209 eg_dump_reg(f, R_0287E8_VGT_DMA_BASE, ib[2], ~0);
210 eg_dump_reg(f, R_0287E4_VGT_DMA_BASE_HI, ib[3], ~0);
211 eg_dump_reg(f, R_008970_VGT_NUM_INDICES, ib[4], ~0);
212 eg_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[5], ~0);
213 break;
214 case PKT3_INDEX_TYPE:
215 eg_dump_reg(f, R_028A7C_VGT_DMA_INDEX_TYPE, ib[1], ~0);
216 break;
217 case PKT3_NUM_INSTANCES:
218 eg_dump_reg(f, R_028A88_VGT_NUM_INSTANCES, ib[1], ~0);
219 break;
220 case PKT3_INDIRECT_BUFFER:
221 break;
222 case PKT3_PFP_SYNC_ME:
223 break;
224 case PKT3_NOP:
225 if (ib[0] == 0xffff1000) {
226 count = -1; /* One dword NOP. */
227 break;
228 } else if (count == 0 && AC_IS_TRACE_POINT(ib[1])) {
229 unsigned packet_id = AC_GET_TRACE_POINT_ID(ib[1]);
230
231 print_spaces(f, INDENT_PKT);
232 fprintf(f, COLOR_RED "Trace point ID: %u\n", packet_id);
233
234 if (trace_id == -1)
235 break; /* tracing was disabled */
236
237 print_spaces(f, INDENT_PKT);
238 if (packet_id < trace_id)
239 fprintf(f, COLOR_RED
240 "This trace point was reached by the CP."
241 COLOR_RESET "\n");
242 else if (packet_id == trace_id)
243 fprintf(f, COLOR_RED
244 "!!!!! This is the last trace point that "
245 "was reached by the CP !!!!!"
246 COLOR_RESET "\n");
247 else if (packet_id+1 == trace_id)
248 fprintf(f, COLOR_RED
249 "!!!!! This is the first trace point that "
250 "was NOT been reached by the CP !!!!!"
251 COLOR_RESET "\n");
252 else
253 fprintf(f, COLOR_RED
254 "!!!!! This trace point was NOT reached "
255 "by the CP !!!!!"
256 COLOR_RESET "\n");
257 break;
258 }
259 /* fallthrough *//* print all dwords */
260 default:
261 for (i = 0; i < count+1; i++) {
262 print_spaces(f, INDENT_PKT);
263 fprintf(f, "0x%08x\n", ib[1+i]);
264 }
265 }
266
267 ib += count + 2;
268 *num_dw -= count + 2;
269 return ib;
270 }
271
272 /**
273 * Parse and print an IB into a file.
274 *
275 * \param f file
276 * \param ib IB
277 * \param num_dw size of the IB
278 * \param chip_class chip class
279 * \param trace_id the last trace ID that is known to have been reached
280 * and executed by the CP, typically read from a buffer
281 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
282 * be NULL.
283 * \param addr_callback_data user data for addr_callback
284 */
eg_parse_ib(FILE * f,uint32_t * ib,int num_dw,int trace_id,const char * name,enum chip_class chip_class,ac_debug_addr_callback addr_callback,void * addr_callback_data)285 static void eg_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id,
286 const char *name, enum chip_class chip_class,
287 ac_debug_addr_callback addr_callback, void *addr_callback_data)
288 {
289 fprintf(f, "------------------ %s begin ------------------\n", name);
290
291 while (num_dw > 0) {
292 unsigned type = PKT_TYPE_G(ib[0]);
293
294 switch (type) {
295 case 3:
296 ib = ac_parse_packet3(f, ib, &num_dw, trace_id,
297 chip_class, addr_callback,
298 addr_callback_data);
299 break;
300 case 2:
301 /* type-2 nop */
302 if (ib[0] == 0x80000000) {
303 fprintf(f, COLOR_GREEN "NOP (type 2)" COLOR_RESET "\n");
304 ib++;
305 num_dw--;
306 break;
307 }
308 /* fall through */
309 default:
310 fprintf(f, "Unknown packet type %i\n", type);
311 return;
312 }
313 }
314
315 fprintf(f, "------------------- %s end -------------------\n", name);
316 if (num_dw < 0) {
317 printf("Packet ends after the end of IB.\n");
318 exit(0);
319 }
320 fprintf(f, "\n");
321 }
322
eg_dump_last_ib(struct r600_context * rctx,FILE * f)323 static void eg_dump_last_ib(struct r600_context *rctx, FILE *f)
324 {
325 int last_trace_id = -1;
326
327 if (!rctx->last_gfx.ib)
328 return;
329
330 if (rctx->last_trace_buf) {
331 /* We are expecting that the ddebug pipe has already
332 * waited for the context, so this buffer should be idle.
333 * If the GPU is hung, there is no point in waiting for it.
334 */
335 uint32_t *map = rctx->b.ws->buffer_map(rctx->last_trace_buf->buf,
336 NULL,
337 PIPE_MAP_UNSYNCHRONIZED |
338 PIPE_MAP_READ);
339 if (map)
340 last_trace_id = *map;
341 }
342
343 eg_parse_ib(f, rctx->last_gfx.ib, rctx->last_gfx.num_dw,
344 last_trace_id, "IB", rctx->b.chip_class,
345 NULL, NULL);
346 }
347
348
eg_dump_debug_state(struct pipe_context * ctx,FILE * f,unsigned flags)349 void eg_dump_debug_state(struct pipe_context *ctx, FILE *f,
350 unsigned flags)
351 {
352 struct r600_context *rctx = (struct r600_context*)ctx;
353
354 eg_dump_last_ib(rctx, f);
355
356 fprintf(f, "Done.\n");
357
358 /* dump only once */
359 radeon_clear_saved_cs(&rctx->last_gfx);
360 r600_resource_reference(&rctx->last_trace_buf, NULL);
361 }
362