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