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