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