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