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