radv: Allow triggering thread traces by file.
[mesa.git] / src / amd / common / ac_debug.c
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
24 #include "ac_debug.h"
25
26 #ifdef HAVE_VALGRIND
27 #include <valgrind.h>
28 #include <memcheck.h>
29 #define VG(x) x
30 #else
31 #define VG(x) ((void)0)
32 #endif
33
34 #include <inttypes.h>
35
36 #include "sid.h"
37 #include "sid_tables.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "util/u_string.h"
41
42 #include <assert.h>
43
44 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
45 * read them, or use "aha -b -f file" to convert them to html.
46 */
47 #define COLOR_RESET "\033[0m"
48 #define COLOR_RED "\033[31m"
49 #define COLOR_GREEN "\033[1;32m"
50 #define COLOR_YELLOW "\033[1;33m"
51 #define COLOR_CYAN "\033[1;36m"
52
53 #define INDENT_PKT 8
54
55 struct ac_ib_parser {
56 FILE *f;
57 uint32_t *ib;
58 unsigned num_dw;
59 const int *trace_ids;
60 unsigned trace_id_count;
61 enum chip_class chip_class;
62 ac_debug_addr_callback addr_callback;
63 void *addr_callback_data;
64
65 unsigned cur_dw;
66 };
67
68 static void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib);
69
70 static void print_spaces(FILE *f, unsigned num)
71 {
72 fprintf(f, "%*s", num, "");
73 }
74
75 static void print_value(FILE *file, uint32_t value, int bits)
76 {
77 /* Guess if it's int or float */
78 if (value <= (1 << 15)) {
79 if (value <= 9)
80 fprintf(file, "%u\n", value);
81 else
82 fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
83 } else {
84 float f = uif(value);
85
86 if (fabs(f) < 100000 && f*10 == floor(f*10))
87 fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
88 else
89 /* Don't print more leading zeros than there are bits. */
90 fprintf(file, "0x%0*x\n", bits / 4, value);
91 }
92 }
93
94 static void print_named_value(FILE *file, const char *name, uint32_t value,
95 int bits)
96 {
97 print_spaces(file, INDENT_PKT);
98 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ", name);
99 print_value(file, value, bits);
100 }
101
102 static const struct si_reg *find_register(enum chip_class chip_class, unsigned offset)
103 {
104 const struct si_reg *table;
105 unsigned table_size;
106
107 switch (chip_class) {
108 case GFX10_3:
109 case GFX10:
110 table = gfx10_reg_table;
111 table_size = ARRAY_SIZE(gfx10_reg_table);
112 break;
113 case GFX9:
114 table = gfx9_reg_table;
115 table_size = ARRAY_SIZE(gfx9_reg_table);
116 break;
117 case GFX8:
118 table = gfx8_reg_table;
119 table_size = ARRAY_SIZE(gfx8_reg_table);
120 break;
121 case GFX7:
122 table = gfx7_reg_table;
123 table_size = ARRAY_SIZE(gfx7_reg_table);
124 break;
125 case GFX6:
126 table = gfx6_reg_table;
127 table_size = ARRAY_SIZE(gfx6_reg_table);
128 break;
129 default:
130 return NULL;
131 }
132
133 for (unsigned i = 0; i < table_size; i++) {
134 const struct si_reg *reg = &table[i];
135
136 if (reg->offset == offset)
137 return reg;
138 }
139
140 return NULL;
141 }
142
143 const char *ac_get_register_name(enum chip_class chip_class, unsigned offset)
144 {
145 const struct si_reg *reg = find_register(chip_class, offset);
146
147 return reg ? sid_strings + reg->name_offset : "(no name)";
148 }
149
150 void ac_dump_reg(FILE *file, enum chip_class chip_class, unsigned offset,
151 uint32_t value, uint32_t field_mask)
152 {
153 const struct si_reg *reg = find_register(chip_class, offset);
154
155 if (reg) {
156 const char *reg_name = sid_strings + reg->name_offset;
157 bool first_field = true;
158
159 print_spaces(file, INDENT_PKT);
160 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
161 reg_name);
162
163 if (!reg->num_fields) {
164 print_value(file, value, 32);
165 return;
166 }
167
168 for (unsigned f = 0; f < reg->num_fields; f++) {
169 const struct si_field *field = sid_fields_table + reg->fields_offset + f;
170 const int *values_offsets = sid_strings_offsets + field->values_offset;
171 uint32_t val = (value & field->mask) >>
172 (ffs(field->mask) - 1);
173
174 if (!(field->mask & field_mask))
175 continue;
176
177 /* Indent the field. */
178 if (!first_field)
179 print_spaces(file,
180 INDENT_PKT + strlen(reg_name) + 4);
181
182 /* Print the field. */
183 fprintf(file, "%s = ", sid_strings + field->name_offset);
184
185 if (val < field->num_values && values_offsets[val] >= 0)
186 fprintf(file, "%s\n", sid_strings + values_offsets[val]);
187 else
188 print_value(file, val,
189 util_bitcount(field->mask));
190
191 first_field = false;
192 }
193 return;
194 }
195
196 print_spaces(file, INDENT_PKT);
197 fprintf(file, COLOR_YELLOW "0x%05x" COLOR_RESET " <- 0x%08x\n", offset, value);
198 }
199
200 static uint32_t ac_ib_get(struct ac_ib_parser *ib)
201 {
202 uint32_t v = 0;
203
204 if (ib->cur_dw < ib->num_dw) {
205 v = ib->ib[ib->cur_dw];
206 #ifdef HAVE_VALGRIND
207 /* Help figure out where garbage data is written to IBs.
208 *
209 * Arguably we should do this already when the IBs are written,
210 * see RADEON_VALGRIND. The problem is that client-requests to
211 * Valgrind have an overhead even when Valgrind isn't running,
212 * and radeon_emit is performance sensitive...
213 */
214 if (VALGRIND_CHECK_VALUE_IS_DEFINED(v))
215 fprintf(ib->f, COLOR_RED "Valgrind: The next DWORD is garbage"
216 COLOR_RESET "\n");
217 #endif
218 fprintf(ib->f, "\n\035#%08x ", v);
219 } else {
220 fprintf(ib->f, "\n\035#???????? ");
221 }
222
223 ib->cur_dw++;
224 return v;
225 }
226
227 static void ac_parse_set_reg_packet(FILE *f, unsigned count, unsigned reg_offset,
228 struct ac_ib_parser *ib)
229 {
230 unsigned reg_dw = ac_ib_get(ib);
231 unsigned reg = ((reg_dw & 0xFFFF) << 2) + reg_offset;
232 unsigned index = reg_dw >> 28;
233 int i;
234
235 if (index != 0) {
236 print_spaces(f, INDENT_PKT);
237 fprintf(f, "INDEX = %u\n", index);
238 }
239
240 for (i = 0; i < count; i++)
241 ac_dump_reg(f, ib->chip_class, reg + i*4, ac_ib_get(ib), ~0);
242 }
243
244 static void ac_parse_packet3(FILE *f, uint32_t header, struct ac_ib_parser *ib,
245 int *current_trace_id)
246 {
247 unsigned first_dw = ib->cur_dw;
248 int count = PKT_COUNT_G(header);
249 unsigned op = PKT3_IT_OPCODE_G(header);
250 const char *predicate = PKT3_PREDICATE(header) ? "(predicate)" : "";
251 int i;
252
253 /* Print the name first. */
254 for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
255 if (packet3_table[i].op == op)
256 break;
257
258 if (i < ARRAY_SIZE(packet3_table)) {
259 const char *name = sid_strings + packet3_table[i].name_offset;
260
261 if (op == PKT3_SET_CONTEXT_REG ||
262 op == PKT3_SET_CONFIG_REG ||
263 op == PKT3_SET_UCONFIG_REG ||
264 op == PKT3_SET_UCONFIG_REG_INDEX ||
265 op == PKT3_SET_SH_REG)
266 fprintf(f, COLOR_CYAN "%s%s" COLOR_CYAN ":\n",
267 name, predicate);
268 else
269 fprintf(f, COLOR_GREEN "%s%s" COLOR_RESET ":\n",
270 name, predicate);
271 } else
272 fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s" COLOR_RESET ":\n",
273 op, predicate);
274
275 /* Print the contents. */
276 switch (op) {
277 case PKT3_SET_CONTEXT_REG:
278 ac_parse_set_reg_packet(f, count, SI_CONTEXT_REG_OFFSET, ib);
279 break;
280 case PKT3_SET_CONFIG_REG:
281 ac_parse_set_reg_packet(f, count, SI_CONFIG_REG_OFFSET, ib);
282 break;
283 case PKT3_SET_UCONFIG_REG:
284 case PKT3_SET_UCONFIG_REG_INDEX:
285 ac_parse_set_reg_packet(f, count, CIK_UCONFIG_REG_OFFSET, ib);
286 break;
287 case PKT3_SET_SH_REG:
288 ac_parse_set_reg_packet(f, count, SI_SH_REG_OFFSET, ib);
289 break;
290 case PKT3_ACQUIRE_MEM:
291 ac_dump_reg(f, ib->chip_class, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
292 ac_dump_reg(f, ib->chip_class, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
293 ac_dump_reg(f, ib->chip_class, R_030230_CP_COHER_SIZE_HI, ac_ib_get(ib), ~0);
294 ac_dump_reg(f, ib->chip_class, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
295 ac_dump_reg(f, ib->chip_class, R_0301E4_CP_COHER_BASE_HI, ac_ib_get(ib), ~0);
296 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
297 if (ib->chip_class >= GFX10)
298 ac_dump_reg(f, ib->chip_class, R_586_GCR_CNTL, ac_ib_get(ib), ~0);
299 break;
300 case PKT3_SURFACE_SYNC:
301 if (ib->chip_class >= GFX7) {
302 ac_dump_reg(f, ib->chip_class, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
303 ac_dump_reg(f, ib->chip_class, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
304 ac_dump_reg(f, ib->chip_class, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
305 } else {
306 ac_dump_reg(f, ib->chip_class, R_0085F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
307 ac_dump_reg(f, ib->chip_class, R_0085F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
308 ac_dump_reg(f, ib->chip_class, R_0085F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
309 }
310 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
311 break;
312 case PKT3_EVENT_WRITE: {
313 uint32_t event_dw = ac_ib_get(ib);
314 ac_dump_reg(f, ib->chip_class, R_028A90_VGT_EVENT_INITIATOR, event_dw,
315 S_028A90_EVENT_TYPE(~0));
316 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
317 print_named_value(f, "INV_L2", (event_dw >> 20) & 0x1, 1);
318 if (count > 0) {
319 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
320 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 16);
321 }
322 break;
323 }
324 case PKT3_EVENT_WRITE_EOP: {
325 uint32_t event_dw = ac_ib_get(ib);
326 ac_dump_reg(f, ib->chip_class, R_028A90_VGT_EVENT_INITIATOR, event_dw,
327 S_028A90_EVENT_TYPE(~0));
328 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
329 print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
330 print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
331 print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
332 print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
333 print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
334 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
335 uint32_t addr_hi_dw = ac_ib_get(ib);
336 print_named_value(f, "ADDRESS_HI", addr_hi_dw, 16);
337 print_named_value(f, "DST_SEL", (addr_hi_dw >> 16) & 0x3, 2);
338 print_named_value(f, "INT_SEL", (addr_hi_dw >> 24) & 0x7, 3);
339 print_named_value(f, "DATA_SEL", addr_hi_dw >> 29, 3);
340 print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
341 print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
342 break;
343 }
344 case PKT3_RELEASE_MEM: {
345 uint32_t event_dw = ac_ib_get(ib);
346 if (ib->chip_class >= GFX10) {
347 ac_dump_reg(f, ib->chip_class, R_490_RELEASE_MEM_OP, event_dw, ~0u);
348 } else {
349 ac_dump_reg(f, ib->chip_class, R_028A90_VGT_EVENT_INITIATOR, event_dw,
350 S_028A90_EVENT_TYPE(~0));
351 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
352 print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
353 print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
354 print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
355 print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
356 print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
357 print_named_value(f, "TC_NC_ACTION_ENA", (event_dw >> 19) & 0x1, 1);
358 print_named_value(f, "TC_WC_ACTION_ENA", (event_dw >> 20) & 0x1, 1);
359 print_named_value(f, "TC_MD_ACTION_ENA", (event_dw >> 21) & 0x1, 1);
360 }
361 uint32_t sel_dw = ac_ib_get(ib);
362 print_named_value(f, "DST_SEL", (sel_dw >> 16) & 0x3, 2);
363 print_named_value(f, "INT_SEL", (sel_dw >> 24) & 0x7, 3);
364 print_named_value(f, "DATA_SEL", sel_dw >> 29, 3);
365 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
366 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
367 print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
368 print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
369 print_named_value(f, "CTXID", ac_ib_get(ib), 32);
370 break;
371 }
372 case PKT3_WAIT_REG_MEM:
373 print_named_value(f, "OP", ac_ib_get(ib), 32);
374 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
375 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
376 print_named_value(f, "REF", ac_ib_get(ib), 32);
377 print_named_value(f, "MASK", ac_ib_get(ib), 32);
378 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
379 break;
380 case PKT3_DRAW_INDEX_AUTO:
381 ac_dump_reg(f, ib->chip_class, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
382 ac_dump_reg(f, ib->chip_class, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
383 break;
384 case PKT3_DRAW_INDEX_2:
385 ac_dump_reg(f, ib->chip_class, R_028A78_VGT_DMA_MAX_SIZE, ac_ib_get(ib), ~0);
386 ac_dump_reg(f, ib->chip_class, R_0287E8_VGT_DMA_BASE, ac_ib_get(ib), ~0);
387 ac_dump_reg(f, ib->chip_class, R_0287E4_VGT_DMA_BASE_HI, ac_ib_get(ib), ~0);
388 ac_dump_reg(f, ib->chip_class, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
389 ac_dump_reg(f, ib->chip_class, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
390 break;
391 case PKT3_INDEX_TYPE:
392 ac_dump_reg(f, ib->chip_class, R_028A7C_VGT_DMA_INDEX_TYPE, ac_ib_get(ib), ~0);
393 break;
394 case PKT3_NUM_INSTANCES:
395 ac_dump_reg(f, ib->chip_class, R_030934_VGT_NUM_INSTANCES, ac_ib_get(ib), ~0);
396 break;
397 case PKT3_WRITE_DATA:
398 ac_dump_reg(f, ib->chip_class, R_370_CONTROL, ac_ib_get(ib), ~0);
399 ac_dump_reg(f, ib->chip_class, R_371_DST_ADDR_LO, ac_ib_get(ib), ~0);
400 ac_dump_reg(f, ib->chip_class, R_372_DST_ADDR_HI, ac_ib_get(ib), ~0);
401 /* The payload is written automatically */
402 break;
403 case PKT3_CP_DMA:
404 ac_dump_reg(f, ib->chip_class, R_410_CP_DMA_WORD0, ac_ib_get(ib), ~0);
405 ac_dump_reg(f, ib->chip_class, R_411_CP_DMA_WORD1, ac_ib_get(ib), ~0);
406 ac_dump_reg(f, ib->chip_class, R_412_CP_DMA_WORD2, ac_ib_get(ib), ~0);
407 ac_dump_reg(f, ib->chip_class, R_413_CP_DMA_WORD3, ac_ib_get(ib), ~0);
408 ac_dump_reg(f, ib->chip_class, R_414_COMMAND, ac_ib_get(ib), ~0);
409 break;
410 case PKT3_DMA_DATA:
411 ac_dump_reg(f, ib->chip_class, R_500_DMA_DATA_WORD0, ac_ib_get(ib), ~0);
412 ac_dump_reg(f, ib->chip_class, R_501_SRC_ADDR_LO, ac_ib_get(ib), ~0);
413 ac_dump_reg(f, ib->chip_class, R_502_SRC_ADDR_HI, ac_ib_get(ib), ~0);
414 ac_dump_reg(f, ib->chip_class, R_503_DST_ADDR_LO, ac_ib_get(ib), ~0);
415 ac_dump_reg(f, ib->chip_class, R_504_DST_ADDR_HI, ac_ib_get(ib), ~0);
416 ac_dump_reg(f, ib->chip_class, R_414_COMMAND, ac_ib_get(ib), ~0);
417 break;
418 case PKT3_INDIRECT_BUFFER_SI:
419 case PKT3_INDIRECT_BUFFER_CONST:
420 case PKT3_INDIRECT_BUFFER_CIK: {
421 uint32_t base_lo_dw = ac_ib_get(ib);
422 ac_dump_reg(f, ib->chip_class, R_3F0_IB_BASE_LO, base_lo_dw, ~0);
423 uint32_t base_hi_dw = ac_ib_get(ib);
424 ac_dump_reg(f, ib->chip_class, R_3F1_IB_BASE_HI, base_hi_dw, ~0);
425 uint32_t control_dw = ac_ib_get(ib);
426 ac_dump_reg(f, ib->chip_class, R_3F2_IB_CONTROL, control_dw, ~0);
427
428 if (!ib->addr_callback)
429 break;
430
431 uint64_t addr = ((uint64_t)base_hi_dw << 32) | base_lo_dw;
432 void *data = ib->addr_callback(ib->addr_callback_data, addr);
433 if (!data)
434 break;
435
436 if (G_3F2_CHAIN(control_dw)) {
437 ib->ib = data;
438 ib->num_dw = G_3F2_IB_SIZE(control_dw);
439 ib->cur_dw = 0;
440 return;
441 }
442
443 struct ac_ib_parser ib_recurse;
444 memcpy(&ib_recurse, ib, sizeof(ib_recurse));
445 ib_recurse.ib = data;
446 ib_recurse.num_dw = G_3F2_IB_SIZE(control_dw);
447 ib_recurse.cur_dw = 0;
448 if(ib_recurse.trace_id_count) {
449 if (*current_trace_id == *ib->trace_ids) {
450 ++ib_recurse.trace_ids;
451 --ib_recurse.trace_id_count;
452 } else {
453 ib_recurse.trace_id_count = 0;
454 }
455 }
456
457 fprintf(f, "\n\035>------------------ nested begin ------------------\n");
458 ac_do_parse_ib(f, &ib_recurse);
459 fprintf(f, "\n\035<------------------- nested end -------------------\n");
460 break;
461 }
462 case PKT3_CLEAR_STATE:
463 case PKT3_INCREMENT_DE_COUNTER:
464 case PKT3_PFP_SYNC_ME:
465 break;
466 case PKT3_NOP:
467 if (header == PKT3_NOP_PAD) {
468 count = -1; /* One dword NOP. */
469 } else if (count == 0 && ib->cur_dw < ib->num_dw &&
470 AC_IS_TRACE_POINT(ib->ib[ib->cur_dw])) {
471 unsigned packet_id = AC_GET_TRACE_POINT_ID(ib->ib[ib->cur_dw]);
472
473 print_spaces(f, INDENT_PKT);
474 fprintf(f, COLOR_RED "Trace point ID: %u\n", packet_id);
475
476 if (!ib->trace_id_count)
477 break; /* tracing was disabled */
478
479 *current_trace_id = packet_id;
480
481 print_spaces(f, INDENT_PKT);
482 if (packet_id < *ib->trace_ids)
483 fprintf(f, COLOR_RED
484 "This trace point was reached by the CP."
485 COLOR_RESET "\n");
486 else if (packet_id == *ib->trace_ids)
487 fprintf(f, COLOR_RED
488 "!!!!! This is the last trace point that "
489 "was reached by the CP !!!!!"
490 COLOR_RESET "\n");
491 else if (packet_id+1 == *ib->trace_ids)
492 fprintf(f, COLOR_RED
493 "!!!!! This is the first trace point that "
494 "was NOT been reached by the CP !!!!!"
495 COLOR_RESET "\n");
496 else
497 fprintf(f, COLOR_RED
498 "!!!!! This trace point was NOT reached "
499 "by the CP !!!!!"
500 COLOR_RESET "\n");
501 break;
502 }
503 break;
504 }
505
506 /* print additional dwords */
507 while (ib->cur_dw <= first_dw + count)
508 ac_ib_get(ib);
509
510 if (ib->cur_dw > first_dw + count + 1)
511 fprintf(f, COLOR_RED "\n!!!!! count in header too low !!!!!"
512 COLOR_RESET "\n");
513 }
514
515 /**
516 * Parse and print an IB into a file.
517 */
518 static void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib)
519 {
520 int current_trace_id = -1;
521
522 while (ib->cur_dw < ib->num_dw) {
523 uint32_t header = ac_ib_get(ib);
524 unsigned type = PKT_TYPE_G(header);
525
526 switch (type) {
527 case 3:
528 ac_parse_packet3(f, header, ib, &current_trace_id);
529 break;
530 case 2:
531 /* type-2 nop */
532 if (header == 0x80000000) {
533 fprintf(f, COLOR_GREEN "NOP (type 2)" COLOR_RESET "\n");
534 break;
535 }
536 /* fall through */
537 default:
538 fprintf(f, "Unknown packet type %i\n", type);
539 break;
540 }
541 }
542 }
543
544 static void format_ib_output(FILE *f, char *out)
545 {
546 unsigned depth = 0;
547
548 for (;;) {
549 char op = 0;
550
551 if (out[0] == '\n' && out[1] == '\035')
552 out++;
553 if (out[0] == '\035') {
554 op = out[1];
555 out += 2;
556 }
557
558 if (op == '<')
559 depth--;
560
561 unsigned indent = 4 * depth;
562 if (op != '#')
563 indent += 9;
564
565 if (indent)
566 print_spaces(f, indent);
567
568 char *end = strchrnul(out, '\n');
569 fwrite(out, end - out, 1, f);
570 fputc('\n', f); /* always end with a new line */
571 if (!*end)
572 break;
573
574 out = end + 1;
575
576 if (op == '>')
577 depth++;
578 }
579 }
580
581 /**
582 * Parse and print an IB into a file.
583 *
584 * \param f file
585 * \param ib_ptr IB
586 * \param num_dw size of the IB
587 * \param chip_class chip class
588 * \param trace_ids the last trace IDs that are known to have been reached
589 * and executed by the CP, typically read from a buffer
590 * \param trace_id_count The number of entries in the trace_ids array.
591 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
592 * be NULL.
593 * \param addr_callback_data user data for addr_callback
594 */
595 void ac_parse_ib_chunk(FILE *f, uint32_t *ib_ptr, int num_dw, const int *trace_ids,
596 unsigned trace_id_count, enum chip_class chip_class,
597 ac_debug_addr_callback addr_callback, void *addr_callback_data)
598 {
599 struct ac_ib_parser ib = {};
600 ib.ib = ib_ptr;
601 ib.num_dw = num_dw;
602 ib.trace_ids = trace_ids;
603 ib.trace_id_count = trace_id_count;
604 ib.chip_class = chip_class;
605 ib.addr_callback = addr_callback;
606 ib.addr_callback_data = addr_callback_data;
607
608 char *out;
609 size_t outsize;
610 FILE *memf = open_memstream(&out, &outsize);
611 ib.f = memf;
612 ac_do_parse_ib(memf, &ib);
613 fclose(memf);
614
615 if (out) {
616 format_ib_output(f, out);
617 free(out);
618 }
619
620 if (ib.cur_dw > ib.num_dw) {
621 printf("\nPacket ends after the end of IB.\n");
622 exit(1);
623 }
624 }
625
626 /**
627 * Parse and print an IB into a file.
628 *
629 * \param f file
630 * \param ib IB
631 * \param num_dw size of the IB
632 * \param chip_class chip class
633 * \param trace_ids the last trace IDs that are known to have been reached
634 * and executed by the CP, typically read from a buffer
635 * \param trace_id_count The number of entries in the trace_ids array.
636 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
637 * be NULL.
638 * \param addr_callback_data user data for addr_callback
639 */
640 void ac_parse_ib(FILE *f, uint32_t *ib, int num_dw, const int *trace_ids,
641 unsigned trace_id_count, const char *name,
642 enum chip_class chip_class, ac_debug_addr_callback addr_callback,
643 void *addr_callback_data)
644 {
645 fprintf(f, "------------------ %s begin ------------------\n", name);
646
647 ac_parse_ib_chunk(f, ib, num_dw, trace_ids, trace_id_count,
648 chip_class, addr_callback, addr_callback_data);
649
650 fprintf(f, "------------------- %s end -------------------\n\n", name);
651 }
652
653 /**
654 * Parse dmesg and return TRUE if a VM fault has been detected.
655 *
656 * \param chip_class chip class
657 * \param old_dmesg_timestamp previous dmesg timestamp parsed at init time
658 * \param out_addr detected VM fault addr
659 */
660 bool ac_vm_fault_occured(enum chip_class chip_class,
661 uint64_t *old_dmesg_timestamp, uint64_t *out_addr)
662 {
663 char line[2000];
664 unsigned sec, usec;
665 int progress = 0;
666 uint64_t dmesg_timestamp = 0;
667 bool fault = false;
668
669 FILE *p = popen("dmesg", "r");
670 if (!p)
671 return false;
672
673 while (fgets(line, sizeof(line), p)) {
674 char *msg, len;
675
676 if (!line[0] || line[0] == '\n')
677 continue;
678
679 /* Get the timestamp. */
680 if (sscanf(line, "[%u.%u]", &sec, &usec) != 2) {
681 static bool hit = false;
682 if (!hit) {
683 fprintf(stderr, "%s: failed to parse line '%s'\n",
684 __func__, line);
685 hit = true;
686 }
687 continue;
688 }
689 dmesg_timestamp = sec * 1000000ull + usec;
690
691 /* If just updating the timestamp. */
692 if (!out_addr)
693 continue;
694
695 /* Process messages only if the timestamp is newer. */
696 if (dmesg_timestamp <= *old_dmesg_timestamp)
697 continue;
698
699 /* Only process the first VM fault. */
700 if (fault)
701 continue;
702
703 /* Remove trailing \n */
704 len = strlen(line);
705 if (len && line[len-1] == '\n')
706 line[len-1] = 0;
707
708 /* Get the message part. */
709 msg = strchr(line, ']');
710 if (!msg)
711 continue;
712 msg++;
713
714 const char *header_line, *addr_line_prefix, *addr_line_format;
715
716 if (chip_class >= GFX9) {
717 /* Match this:
718 * ..: [gfxhub] VMC page fault (src_id:0 ring:158 vm_id:2 pas_id:0)
719 * ..: at page 0x0000000219f8f000 from 27
720 * ..: VM_L2_PROTECTION_FAULT_STATUS:0x0020113C
721 */
722 header_line = "VMC page fault";
723 addr_line_prefix = " at page";
724 addr_line_format = "%"PRIx64;
725 } else {
726 header_line = "GPU fault detected:";
727 addr_line_prefix = "VM_CONTEXT1_PROTECTION_FAULT_ADDR";
728 addr_line_format = "%"PRIX64;
729 }
730
731 switch (progress) {
732 case 0:
733 if (strstr(msg, header_line))
734 progress = 1;
735 break;
736 case 1:
737 msg = strstr(msg, addr_line_prefix);
738 if (msg) {
739 msg = strstr(msg, "0x");
740 if (msg) {
741 msg += 2;
742 if (sscanf(msg, addr_line_format, out_addr) == 1)
743 fault = true;
744 }
745 }
746 progress = 0;
747 break;
748 default:
749 progress = 0;
750 }
751 }
752 pclose(p);
753
754 if (dmesg_timestamp > *old_dmesg_timestamp)
755 *old_dmesg_timestamp = dmesg_timestamp;
756
757 return fault;
758 }
759
760 static int compare_wave(const void *p1, const void *p2)
761 {
762 struct ac_wave_info *w1 = (struct ac_wave_info *)p1;
763 struct ac_wave_info *w2 = (struct ac_wave_info *)p2;
764
765 /* Sort waves according to PC and then SE, SH, CU, etc. */
766 if (w1->pc < w2->pc)
767 return -1;
768 if (w1->pc > w2->pc)
769 return 1;
770 if (w1->se < w2->se)
771 return -1;
772 if (w1->se > w2->se)
773 return 1;
774 if (w1->sh < w2->sh)
775 return -1;
776 if (w1->sh > w2->sh)
777 return 1;
778 if (w1->cu < w2->cu)
779 return -1;
780 if (w1->cu > w2->cu)
781 return 1;
782 if (w1->simd < w2->simd)
783 return -1;
784 if (w1->simd > w2->simd)
785 return 1;
786 if (w1->wave < w2->wave)
787 return -1;
788 if (w1->wave > w2->wave)
789 return 1;
790
791 return 0;
792 }
793
794 /* Return wave information. "waves" should be a large enough array. */
795 unsigned ac_get_wave_info(enum chip_class chip_class,
796 struct ac_wave_info waves[AC_MAX_WAVES_PER_CHIP])
797 {
798 char line[2000], cmd[128];
799 unsigned num_waves = 0;
800
801 sprintf(cmd, "umr -O halt_waves -wa %s", chip_class >= GFX10 ? "gfx_0.0.0" : "gfx");
802
803 FILE *p = popen(cmd, "r");
804 if (!p)
805 return 0;
806
807 if (!fgets(line, sizeof(line), p) ||
808 strncmp(line, "SE", 2) != 0) {
809 pclose(p);
810 return 0;
811 }
812
813 while (fgets(line, sizeof(line), p)) {
814 struct ac_wave_info *w;
815 uint32_t pc_hi, pc_lo, exec_hi, exec_lo;
816
817 assert(num_waves < AC_MAX_WAVES_PER_CHIP);
818 w = &waves[num_waves];
819
820 if (sscanf(line, "%u %u %u %u %u %x %x %x %x %x %x %x",
821 &w->se, &w->sh, &w->cu, &w->simd, &w->wave,
822 &w->status, &pc_hi, &pc_lo, &w->inst_dw0,
823 &w->inst_dw1, &exec_hi, &exec_lo) == 12) {
824 w->pc = ((uint64_t)pc_hi << 32) | pc_lo;
825 w->exec = ((uint64_t)exec_hi << 32) | exec_lo;
826 w->matched = false;
827 num_waves++;
828 }
829 }
830
831 qsort(waves, num_waves, sizeof(struct ac_wave_info), compare_wave);
832
833 pclose(p);
834 return num_waves;
835 }