2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
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
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.
24 * Marek Olšák <maraeo@gmail.com>
39 #include "sid_tables.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "util/u_string.h"
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.
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"
60 enum chip_class chip_class
;
61 ac_debug_addr_callback addr_callback
;
62 void *addr_callback_data
;
67 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
);
69 static void print_spaces(FILE *f
, unsigned num
)
71 fprintf(f
, "%*s", num
, "");
74 static void print_value(FILE *file
, uint32_t value
, int bits
)
76 /* Guess if it's int or float */
77 if (value
<= (1 << 15)) {
79 fprintf(file
, "%u\n", value
);
81 fprintf(file
, "%u (0x%0*x)\n", value
, bits
/ 4, value
);
85 if (fabs(f
) < 100000 && f
*10 == floor(f
*10))
86 fprintf(file
, "%.1ff (0x%0*x)\n", f
, bits
/ 4, value
);
88 /* Don't print more leading zeros than there are bits. */
89 fprintf(file
, "0x%0*x\n", bits
/ 4, value
);
93 static void print_named_value(FILE *file
, const char *name
, uint32_t value
,
96 print_spaces(file
, INDENT_PKT
);
97 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ", name
);
98 print_value(file
, value
, bits
);
101 void ac_dump_reg(FILE *file
, unsigned offset
, uint32_t value
,
106 for (r
= 0; r
< ARRAY_SIZE(sid_reg_table
); r
++) {
107 const struct si_reg
*reg
= &sid_reg_table
[r
];
108 const char *reg_name
= sid_strings
+ reg
->name_offset
;
110 if (reg
->offset
== offset
) {
111 bool first_field
= true;
113 print_spaces(file
, INDENT_PKT
);
114 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ",
117 if (!reg
->num_fields
) {
118 print_value(file
, value
, 32);
122 for (f
= 0; f
< reg
->num_fields
; f
++) {
123 const struct si_field
*field
= sid_fields_table
+ reg
->fields_offset
+ f
;
124 const int *values_offsets
= sid_strings_offsets
+ field
->values_offset
;
125 uint32_t val
= (value
& field
->mask
) >>
126 (ffs(field
->mask
) - 1);
128 if (!(field
->mask
& field_mask
))
131 /* Indent the field. */
134 INDENT_PKT
+ strlen(reg_name
) + 4);
136 /* Print the field. */
137 fprintf(file
, "%s = ", sid_strings
+ field
->name_offset
);
139 if (val
< field
->num_values
&& values_offsets
[val
] >= 0)
140 fprintf(file
, "%s\n", sid_strings
+ values_offsets
[val
]);
142 print_value(file
, val
,
143 util_bitcount(field
->mask
));
151 print_spaces(file
, INDENT_PKT
);
152 fprintf(file
, COLOR_YELLOW
"0x%05x" COLOR_RESET
" <- 0x%08x\n", offset
, value
);
155 static uint32_t ac_ib_get(struct ac_ib_parser
*ib
)
159 if (ib
->cur_dw
< ib
->num_dw
) {
160 v
= ib
->ib
[ib
->cur_dw
];
162 /* Help figure out where garbage data is written to IBs.
164 * Arguably we should do this already when the IBs are written,
165 * see RADEON_VALGRIND. The problem is that client-requests to
166 * Valgrind have an overhead even when Valgrind isn't running,
167 * and radeon_emit is performance sensitive...
169 if (VALGRIND_CHECK_VALUE_IS_DEFINED(v
))
170 fprintf(ib
->f
, COLOR_RED
"Valgrind: The next DWORD is garbage"
173 fprintf(ib
->f
, "\n\035#%08x ", v
);
175 fprintf(ib
->f
, "\n\035#???????? ");
182 static void ac_parse_set_reg_packet(FILE *f
, unsigned count
, unsigned reg_offset
,
183 struct ac_ib_parser
*ib
)
185 unsigned reg_dw
= ac_ib_get(ib
);
186 unsigned reg
= ((reg_dw
& 0xFFFF) << 2) + reg_offset
;
187 unsigned index
= reg_dw
>> 28;
191 print_spaces(f
, INDENT_PKT
);
192 fprintf(f
, "INDEX = %u\n", index
);
195 for (i
= 0; i
< count
; i
++)
196 ac_dump_reg(f
, reg
+ i
*4, ac_ib_get(ib
), ~0);
199 static void ac_parse_packet3(FILE *f
, uint32_t header
, struct ac_ib_parser
*ib
)
201 unsigned first_dw
= ib
->cur_dw
;
202 int count
= PKT_COUNT_G(header
);
203 unsigned op
= PKT3_IT_OPCODE_G(header
);
204 const char *predicate
= PKT3_PREDICATE(header
) ? "(predicate)" : "";
207 /* Print the name first. */
208 for (i
= 0; i
< ARRAY_SIZE(packet3_table
); i
++)
209 if (packet3_table
[i
].op
== op
)
212 if (i
< ARRAY_SIZE(packet3_table
)) {
213 const char *name
= sid_strings
+ packet3_table
[i
].name_offset
;
215 if (op
== PKT3_SET_CONTEXT_REG
||
216 op
== PKT3_SET_CONFIG_REG
||
217 op
== PKT3_SET_UCONFIG_REG
||
218 op
== PKT3_SET_SH_REG
)
219 fprintf(f
, COLOR_CYAN
"%s%s" COLOR_CYAN
":\n",
222 fprintf(f
, COLOR_GREEN
"%s%s" COLOR_RESET
":\n",
225 fprintf(f
, COLOR_RED
"PKT3_UNKNOWN 0x%x%s" COLOR_RESET
":\n",
228 /* Print the contents. */
230 case PKT3_SET_CONTEXT_REG
:
231 ac_parse_set_reg_packet(f
, count
, SI_CONTEXT_REG_OFFSET
, ib
);
233 case PKT3_SET_CONFIG_REG
:
234 ac_parse_set_reg_packet(f
, count
, SI_CONFIG_REG_OFFSET
, ib
);
236 case PKT3_SET_UCONFIG_REG
:
237 ac_parse_set_reg_packet(f
, count
, CIK_UCONFIG_REG_OFFSET
, ib
);
239 case PKT3_SET_SH_REG
:
240 ac_parse_set_reg_packet(f
, count
, SI_SH_REG_OFFSET
, ib
);
242 case PKT3_ACQUIRE_MEM
:
243 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
244 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
245 ac_dump_reg(f
, R_030230_CP_COHER_SIZE_HI
, ac_ib_get(ib
), ~0);
246 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
247 ac_dump_reg(f
, R_0301E4_CP_COHER_BASE_HI
, ac_ib_get(ib
), ~0);
248 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
250 case PKT3_SURFACE_SYNC
:
251 if (ib
->chip_class
>= CIK
) {
252 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
253 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
254 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
256 ac_dump_reg(f
, R_0085F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
257 ac_dump_reg(f
, R_0085F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
258 ac_dump_reg(f
, R_0085F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
260 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
262 case PKT3_EVENT_WRITE
: {
263 uint32_t event_dw
= ac_ib_get(ib
);
264 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
265 S_028A90_EVENT_TYPE(~0));
266 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
267 print_named_value(f
, "INV_L2", (event_dw
>> 20) & 0x1, 1);
269 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
270 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 16);
274 case PKT3_EVENT_WRITE_EOP
: {
275 uint32_t event_dw
= ac_ib_get(ib
);
276 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
277 S_028A90_EVENT_TYPE(~0));
278 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
279 print_named_value(f
, "TCL1_VOL_ACTION_ENA", (event_dw
>> 12) & 0x1, 1);
280 print_named_value(f
, "TC_VOL_ACTION_ENA", (event_dw
>> 13) & 0x1, 1);
281 print_named_value(f
, "TC_WB_ACTION_ENA", (event_dw
>> 15) & 0x1, 1);
282 print_named_value(f
, "TCL1_ACTION_ENA", (event_dw
>> 16) & 0x1, 1);
283 print_named_value(f
, "TC_ACTION_ENA", (event_dw
>> 17) & 0x1, 1);
284 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
285 uint32_t addr_hi_dw
= ac_ib_get(ib
);
286 print_named_value(f
, "ADDRESS_HI", addr_hi_dw
, 16);
287 print_named_value(f
, "DST_SEL", (addr_hi_dw
>> 16) & 0x3, 2);
288 print_named_value(f
, "INT_SEL", (addr_hi_dw
>> 24) & 0x7, 3);
289 print_named_value(f
, "DATA_SEL", addr_hi_dw
>> 29, 3);
290 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
291 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
294 case PKT3_RELEASE_MEM
: {
295 uint32_t event_dw
= ac_ib_get(ib
);
296 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
297 S_028A90_EVENT_TYPE(~0));
298 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
299 print_named_value(f
, "TCL1_VOL_ACTION_ENA", (event_dw
>> 12) & 0x1, 1);
300 print_named_value(f
, "TC_VOL_ACTION_ENA", (event_dw
>> 13) & 0x1, 1);
301 print_named_value(f
, "TC_WB_ACTION_ENA", (event_dw
>> 15) & 0x1, 1);
302 print_named_value(f
, "TCL1_ACTION_ENA", (event_dw
>> 16) & 0x1, 1);
303 print_named_value(f
, "TC_ACTION_ENA", (event_dw
>> 17) & 0x1, 1);
304 print_named_value(f
, "TC_NC_ACTION_ENA", (event_dw
>> 19) & 0x1, 1);
305 print_named_value(f
, "TC_WC_ACTION_ENA", (event_dw
>> 20) & 0x1, 1);
306 print_named_value(f
, "TC_MD_ACTION_ENA", (event_dw
>> 21) & 0x1, 1);
307 uint32_t sel_dw
= ac_ib_get(ib
);
308 print_named_value(f
, "DST_SEL", (sel_dw
>> 16) & 0x3, 2);
309 print_named_value(f
, "INT_SEL", (sel_dw
>> 24) & 0x7, 3);
310 print_named_value(f
, "DATA_SEL", sel_dw
>> 29, 3);
311 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
312 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
313 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
314 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
315 print_named_value(f
, "CTXID", ac_ib_get(ib
), 32);
318 case PKT3_WAIT_REG_MEM
:
319 print_named_value(f
, "OP", ac_ib_get(ib
), 32);
320 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
321 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
322 print_named_value(f
, "REF", ac_ib_get(ib
), 32);
323 print_named_value(f
, "MASK", ac_ib_get(ib
), 32);
324 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
326 case PKT3_DRAW_INDEX_AUTO
:
327 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
328 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
330 case PKT3_DRAW_INDEX_2
:
331 ac_dump_reg(f
, R_028A78_VGT_DMA_MAX_SIZE
, ac_ib_get(ib
), ~0);
332 ac_dump_reg(f
, R_0287E8_VGT_DMA_BASE
, ac_ib_get(ib
), ~0);
333 ac_dump_reg(f
, R_0287E4_VGT_DMA_BASE_HI
, ac_ib_get(ib
), ~0);
334 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
335 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
337 case PKT3_INDEX_TYPE
:
338 ac_dump_reg(f
, R_028A7C_VGT_DMA_INDEX_TYPE
, ac_ib_get(ib
), ~0);
340 case PKT3_NUM_INSTANCES
:
341 ac_dump_reg(f
, R_030934_VGT_NUM_INSTANCES
, ac_ib_get(ib
), ~0);
343 case PKT3_WRITE_DATA
:
344 ac_dump_reg(f
, R_370_CONTROL
, ac_ib_get(ib
), ~0);
345 ac_dump_reg(f
, R_371_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
346 ac_dump_reg(f
, R_372_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
347 /* The payload is written automatically */
350 ac_dump_reg(f
, R_410_CP_DMA_WORD0
, ac_ib_get(ib
), ~0);
351 ac_dump_reg(f
, R_411_CP_DMA_WORD1
, ac_ib_get(ib
), ~0);
352 ac_dump_reg(f
, R_412_CP_DMA_WORD2
, ac_ib_get(ib
), ~0);
353 ac_dump_reg(f
, R_413_CP_DMA_WORD3
, ac_ib_get(ib
), ~0);
354 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
357 ac_dump_reg(f
, R_500_DMA_DATA_WORD0
, ac_ib_get(ib
), ~0);
358 ac_dump_reg(f
, R_501_SRC_ADDR_LO
, ac_ib_get(ib
), ~0);
359 ac_dump_reg(f
, R_502_SRC_ADDR_HI
, ac_ib_get(ib
), ~0);
360 ac_dump_reg(f
, R_503_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
361 ac_dump_reg(f
, R_504_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
362 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
364 case PKT3_INDIRECT_BUFFER_SI
:
365 case PKT3_INDIRECT_BUFFER_CONST
:
366 case PKT3_INDIRECT_BUFFER_CIK
: {
367 uint32_t base_lo_dw
= ac_ib_get(ib
);
368 ac_dump_reg(f
, R_3F0_IB_BASE_LO
, base_lo_dw
, ~0);
369 uint32_t base_hi_dw
= ac_ib_get(ib
);
370 ac_dump_reg(f
, R_3F1_IB_BASE_HI
, base_hi_dw
, ~0);
371 uint32_t control_dw
= ac_ib_get(ib
);
372 ac_dump_reg(f
, R_3F2_CONTROL
, control_dw
, ~0);
374 if (!ib
->addr_callback
)
377 uint64_t addr
= ((uint64_t)base_hi_dw
<< 32) | base_lo_dw
;
378 void *data
= ib
->addr_callback(ib
->addr_callback_data
, addr
);
382 if (G_3F2_CHAIN(control_dw
)) {
384 ib
->num_dw
= G_3F2_IB_SIZE(control_dw
);
389 struct ac_ib_parser ib_recurse
;
390 memcpy(&ib_recurse
, ib
, sizeof(ib_recurse
));
391 ib_recurse
.ib
= data
;
392 ib_recurse
.num_dw
= G_3F2_IB_SIZE(control_dw
);
393 ib_recurse
.cur_dw
= 0;
395 fprintf(f
, "\n\035>------------------ nested begin ------------------\n");
396 ac_do_parse_ib(f
, &ib_recurse
);
397 fprintf(f
, "\n\035<------------------- nested end -------------------\n");
400 case PKT3_CLEAR_STATE
:
401 case PKT3_INCREMENT_DE_COUNTER
:
402 case PKT3_PFP_SYNC_ME
:
405 if (header
== 0xffff1000) {
406 count
= -1; /* One dword NOP. */
407 } else if (count
== 0 && ib
->cur_dw
< ib
->num_dw
&&
408 AC_IS_TRACE_POINT(ib
->ib
[ib
->cur_dw
])) {
409 unsigned packet_id
= AC_GET_TRACE_POINT_ID(ib
->ib
[ib
->cur_dw
]);
411 print_spaces(f
, INDENT_PKT
);
412 fprintf(f
, COLOR_RED
"Trace point ID: %u\n", packet_id
);
414 if (ib
->trace_id
== -1)
415 break; /* tracing was disabled */
417 print_spaces(f
, INDENT_PKT
);
418 if (packet_id
< ib
->trace_id
)
420 "This trace point was reached by the CP."
422 else if (packet_id
== ib
->trace_id
)
424 "!!!!! This is the last trace point that "
425 "was reached by the CP !!!!!"
427 else if (packet_id
+1 == ib
->trace_id
)
429 "!!!!! This is the first trace point that "
430 "was NOT been reached by the CP !!!!!"
434 "!!!!! This trace point was NOT reached "
442 /* print additional dwords */
443 while (ib
->cur_dw
<= first_dw
+ count
)
446 if (ib
->cur_dw
> first_dw
+ count
+ 1)
447 fprintf(f
, COLOR_RED
"\n!!!!! count in header too low !!!!!"
452 * Parse and print an IB into a file.
454 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
)
456 while (ib
->cur_dw
< ib
->num_dw
) {
457 uint32_t header
= ac_ib_get(ib
);
458 unsigned type
= PKT_TYPE_G(header
);
462 ac_parse_packet3(f
, header
, ib
);
466 if (header
== 0x80000000) {
467 fprintf(f
, COLOR_GREEN
"NOP (type 2)" COLOR_RESET
"\n");
472 fprintf(f
, "Unknown packet type %i\n", type
);
478 static void format_ib_output(FILE *f
, char *out
)
485 if (out
[0] == '\n' && out
[1] == '\035')
487 if (out
[0] == '\035') {
495 unsigned indent
= 4 * depth
;
500 print_spaces(f
, indent
);
502 char *end
= util_strchrnul(out
, '\n');
503 fwrite(out
, end
- out
, 1, f
);
504 fputc('\n', f
); /* always end with a new line */
516 * Parse and print an IB into a file.
520 * \param num_dw size of the IB
521 * \param chip_class chip class
522 * \param trace_id the last trace ID that is known to have been reached
523 * and executed by the CP, typically read from a buffer
524 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
526 * \param addr_callback_data user data for addr_callback
528 void ac_parse_ib_chunk(FILE *f
, uint32_t *ib_ptr
, int num_dw
, int trace_id
,
529 enum chip_class chip_class
,
530 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
532 struct ac_ib_parser ib
= {};
535 ib
.trace_id
= trace_id
;
536 ib
.chip_class
= chip_class
;
537 ib
.addr_callback
= addr_callback
;
538 ib
.addr_callback_data
= addr_callback_data
;
542 FILE *memf
= open_memstream(&out
, &outsize
);
544 ac_do_parse_ib(memf
, &ib
);
548 format_ib_output(f
, out
);
552 if (ib
.cur_dw
> ib
.num_dw
) {
553 printf("\nPacket ends after the end of IB.\n");
559 * Parse and print an IB into a file.
563 * \param num_dw size of the IB
564 * \param chip_class chip class
565 * \param trace_id the last trace ID that is known to have been reached
566 * and executed by the CP, typically read from a buffer
567 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
569 * \param addr_callback_data user data for addr_callback
571 void ac_parse_ib(FILE *f
, uint32_t *ib
, int num_dw
, int trace_id
,
572 const char *name
, enum chip_class chip_class
,
573 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
575 fprintf(f
, "------------------ %s begin ------------------\n", name
);
577 ac_parse_ib_chunk(f
, ib
, num_dw
, trace_id
, chip_class
, addr_callback
,
580 fprintf(f
, "------------------- %s end -------------------\n\n", name
);