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>
31 #include "sid_tables.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
35 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
36 * read them, or use "aha -b -f file" to convert them to html.
38 #define COLOR_RESET "\033[0m"
39 #define COLOR_RED "\033[31m"
40 #define COLOR_GREEN "\033[1;32m"
41 #define COLOR_YELLOW "\033[1;33m"
42 #define COLOR_CYAN "\033[1;36m"
51 enum chip_class chip_class
;
52 ac_debug_addr_callback addr_callback
;
53 void *addr_callback_data
;
58 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
);
60 static void print_spaces(FILE *f
, unsigned num
)
62 fprintf(f
, "%*s", num
, "");
65 static void print_value(FILE *file
, uint32_t value
, int bits
)
67 /* Guess if it's int or float */
68 if (value
<= (1 << 15)) {
70 fprintf(file
, "%u\n", value
);
72 fprintf(file
, "%u (0x%0*x)\n", value
, bits
/ 4, value
);
76 if (fabs(f
) < 100000 && f
*10 == floor(f
*10))
77 fprintf(file
, "%.1ff (0x%0*x)\n", f
, bits
/ 4, value
);
79 /* Don't print more leading zeros than there are bits. */
80 fprintf(file
, "0x%0*x\n", bits
/ 4, value
);
84 static void print_named_value(FILE *file
, const char *name
, uint32_t value
,
87 print_spaces(file
, INDENT_PKT
);
88 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ", name
);
89 print_value(file
, value
, bits
);
92 void ac_dump_reg(FILE *file
, unsigned offset
, uint32_t value
,
97 for (r
= 0; r
< ARRAY_SIZE(sid_reg_table
); r
++) {
98 const struct si_reg
*reg
= &sid_reg_table
[r
];
99 const char *reg_name
= sid_strings
+ reg
->name_offset
;
101 if (reg
->offset
== offset
) {
102 bool first_field
= true;
104 print_spaces(file
, INDENT_PKT
);
105 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ",
108 if (!reg
->num_fields
) {
109 print_value(file
, value
, 32);
113 for (f
= 0; f
< reg
->num_fields
; f
++) {
114 const struct si_field
*field
= sid_fields_table
+ reg
->fields_offset
+ f
;
115 const int *values_offsets
= sid_strings_offsets
+ field
->values_offset
;
116 uint32_t val
= (value
& field
->mask
) >>
117 (ffs(field
->mask
) - 1);
119 if (!(field
->mask
& field_mask
))
122 /* Indent the field. */
125 INDENT_PKT
+ strlen(reg_name
) + 4);
127 /* Print the field. */
128 fprintf(file
, "%s = ", sid_strings
+ field
->name_offset
);
130 if (val
< field
->num_values
&& values_offsets
[val
] >= 0)
131 fprintf(file
, "%s\n", sid_strings
+ values_offsets
[val
]);
133 print_value(file
, val
,
134 util_bitcount(field
->mask
));
142 print_spaces(file
, INDENT_PKT
);
143 fprintf(file
, COLOR_YELLOW
"0x%05x" COLOR_RESET
" <- 0x%08x\n", offset
, value
);
146 static uint32_t ac_ib_get(struct ac_ib_parser
*ib
)
150 if (ib
->cur_dw
< ib
->num_dw
) {
151 v
= ib
->ib
[ib
->cur_dw
];
152 fprintf(ib
->f
, "\n\035#%08x ", v
);
154 fprintf(ib
->f
, "\n\035#???????? ");
161 static void ac_parse_set_reg_packet(FILE *f
, unsigned count
, unsigned reg_offset
,
162 struct ac_ib_parser
*ib
)
164 unsigned reg_dw
= ac_ib_get(ib
);
165 unsigned reg
= ((reg_dw
& 0xFFFF) << 2) + reg_offset
;
166 unsigned index
= reg_dw
>> 28;
170 print_spaces(f
, INDENT_PKT
);
171 fprintf(f
, "INDEX = %u\n", index
);
174 for (i
= 0; i
< count
; i
++)
175 ac_dump_reg(f
, reg
+ i
*4, ac_ib_get(ib
), ~0);
178 static void ac_parse_packet3(FILE *f
, uint32_t header
, struct ac_ib_parser
*ib
)
180 unsigned first_dw
= ib
->cur_dw
;
181 int count
= PKT_COUNT_G(header
);
182 unsigned op
= PKT3_IT_OPCODE_G(header
);
183 const char *predicate
= PKT3_PREDICATE(header
) ? "(predicate)" : "";
186 /* Print the name first. */
187 for (i
= 0; i
< ARRAY_SIZE(packet3_table
); i
++)
188 if (packet3_table
[i
].op
== op
)
191 if (i
< ARRAY_SIZE(packet3_table
)) {
192 const char *name
= sid_strings
+ packet3_table
[i
].name_offset
;
194 if (op
== PKT3_SET_CONTEXT_REG
||
195 op
== PKT3_SET_CONFIG_REG
||
196 op
== PKT3_SET_UCONFIG_REG
||
197 op
== PKT3_SET_SH_REG
)
198 fprintf(f
, COLOR_CYAN
"%s%s" COLOR_CYAN
":\n",
201 fprintf(f
, COLOR_GREEN
"%s%s" COLOR_RESET
":\n",
204 fprintf(f
, COLOR_RED
"PKT3_UNKNOWN 0x%x%s" COLOR_RESET
":\n",
207 /* Print the contents. */
209 case PKT3_SET_CONTEXT_REG
:
210 ac_parse_set_reg_packet(f
, count
, SI_CONTEXT_REG_OFFSET
, ib
);
212 case PKT3_SET_CONFIG_REG
:
213 ac_parse_set_reg_packet(f
, count
, SI_CONFIG_REG_OFFSET
, ib
);
215 case PKT3_SET_UCONFIG_REG
:
216 ac_parse_set_reg_packet(f
, count
, CIK_UCONFIG_REG_OFFSET
, ib
);
218 case PKT3_SET_SH_REG
:
219 ac_parse_set_reg_packet(f
, count
, SI_SH_REG_OFFSET
, ib
);
221 case PKT3_ACQUIRE_MEM
:
222 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
223 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
224 ac_dump_reg(f
, R_030230_CP_COHER_SIZE_HI
, ac_ib_get(ib
), ~0);
225 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
226 ac_dump_reg(f
, R_0301E4_CP_COHER_BASE_HI
, ac_ib_get(ib
), ~0);
227 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
229 case PKT3_SURFACE_SYNC
:
230 if (ib
->chip_class
>= CIK
) {
231 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
232 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
233 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
235 ac_dump_reg(f
, R_0085F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
236 ac_dump_reg(f
, R_0085F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
237 ac_dump_reg(f
, R_0085F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
239 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
241 case PKT3_EVENT_WRITE
: {
242 uint32_t event_dw
= ac_ib_get(ib
);
243 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
244 S_028A90_EVENT_TYPE(~0));
245 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
246 print_named_value(f
, "INV_L2", (event_dw
>> 20) & 0x1, 1);
248 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
249 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 16);
253 case PKT3_EVENT_WRITE_EOP
: {
254 uint32_t event_dw
= ac_ib_get(ib
);
255 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
256 S_028A90_EVENT_TYPE(~0));
257 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
258 print_named_value(f
, "TCL1_VOL_ACTION_ENA", (event_dw
>> 12) & 0x1, 1);
259 print_named_value(f
, "TC_VOL_ACTION_ENA", (event_dw
>> 13) & 0x1, 1);
260 print_named_value(f
, "TC_WB_ACTION_ENA", (event_dw
>> 15) & 0x1, 1);
261 print_named_value(f
, "TCL1_ACTION_ENA", (event_dw
>> 16) & 0x1, 1);
262 print_named_value(f
, "TC_ACTION_ENA", (event_dw
>> 17) & 0x1, 1);
263 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
264 uint32_t addr_hi_dw
= ac_ib_get(ib
);
265 print_named_value(f
, "ADDRESS_HI", addr_hi_dw
, 16);
266 print_named_value(f
, "DST_SEL", (addr_hi_dw
>> 16) & 0x3, 2);
267 print_named_value(f
, "INT_SEL", (addr_hi_dw
>> 24) & 0x7, 3);
268 print_named_value(f
, "DATA_SEL", addr_hi_dw
>> 29, 3);
269 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
270 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
273 case PKT3_RELEASE_MEM
: {
274 uint32_t event_dw
= ac_ib_get(ib
);
275 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
276 S_028A90_EVENT_TYPE(~0));
277 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
278 print_named_value(f
, "TCL1_VOL_ACTION_ENA", (event_dw
>> 12) & 0x1, 1);
279 print_named_value(f
, "TC_VOL_ACTION_ENA", (event_dw
>> 13) & 0x1, 1);
280 print_named_value(f
, "TC_WB_ACTION_ENA", (event_dw
>> 15) & 0x1, 1);
281 print_named_value(f
, "TCL1_ACTION_ENA", (event_dw
>> 16) & 0x1, 1);
282 print_named_value(f
, "TC_ACTION_ENA", (event_dw
>> 17) & 0x1, 1);
283 print_named_value(f
, "TC_NC_ACTION_ENA", (event_dw
>> 19) & 0x1, 1);
284 print_named_value(f
, "TC_WC_ACTION_ENA", (event_dw
>> 20) & 0x1, 1);
285 print_named_value(f
, "TC_MD_ACTION_ENA", (event_dw
>> 21) & 0x1, 1);
286 uint32_t sel_dw
= ac_ib_get(ib
);
287 print_named_value(f
, "DST_SEL", (sel_dw
>> 16) & 0x3, 2);
288 print_named_value(f
, "INT_SEL", (sel_dw
>> 24) & 0x7, 3);
289 print_named_value(f
, "DATA_SEL", sel_dw
>> 29, 3);
290 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
291 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
292 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
293 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
294 print_named_value(f
, "CTXID", ac_ib_get(ib
), 32);
297 case PKT3_WAIT_REG_MEM
:
298 print_named_value(f
, "OP", ac_ib_get(ib
), 32);
299 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
300 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
301 print_named_value(f
, "REF", ac_ib_get(ib
), 32);
302 print_named_value(f
, "MASK", ac_ib_get(ib
), 32);
303 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
305 case PKT3_DRAW_INDEX_AUTO
:
306 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
307 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
309 case PKT3_DRAW_INDEX_2
:
310 ac_dump_reg(f
, R_028A78_VGT_DMA_MAX_SIZE
, ac_ib_get(ib
), ~0);
311 ac_dump_reg(f
, R_0287E8_VGT_DMA_BASE
, ac_ib_get(ib
), ~0);
312 ac_dump_reg(f
, R_0287E4_VGT_DMA_BASE_HI
, ac_ib_get(ib
), ~0);
313 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
314 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
316 case PKT3_INDEX_TYPE
:
317 ac_dump_reg(f
, R_028A7C_VGT_DMA_INDEX_TYPE
, ac_ib_get(ib
), ~0);
319 case PKT3_NUM_INSTANCES
:
320 ac_dump_reg(f
, R_030934_VGT_NUM_INSTANCES
, ac_ib_get(ib
), ~0);
322 case PKT3_WRITE_DATA
:
323 ac_dump_reg(f
, R_370_CONTROL
, ac_ib_get(ib
), ~0);
324 ac_dump_reg(f
, R_371_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
325 ac_dump_reg(f
, R_372_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
326 /* The payload is written automatically */
329 ac_dump_reg(f
, R_410_CP_DMA_WORD0
, ac_ib_get(ib
), ~0);
330 ac_dump_reg(f
, R_411_CP_DMA_WORD1
, ac_ib_get(ib
), ~0);
331 ac_dump_reg(f
, R_412_CP_DMA_WORD2
, ac_ib_get(ib
), ~0);
332 ac_dump_reg(f
, R_413_CP_DMA_WORD3
, ac_ib_get(ib
), ~0);
333 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
336 ac_dump_reg(f
, R_500_DMA_DATA_WORD0
, ac_ib_get(ib
), ~0);
337 ac_dump_reg(f
, R_501_SRC_ADDR_LO
, ac_ib_get(ib
), ~0);
338 ac_dump_reg(f
, R_502_SRC_ADDR_HI
, ac_ib_get(ib
), ~0);
339 ac_dump_reg(f
, R_503_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
340 ac_dump_reg(f
, R_504_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
341 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
343 case PKT3_INDIRECT_BUFFER_SI
:
344 case PKT3_INDIRECT_BUFFER_CONST
:
345 case PKT3_INDIRECT_BUFFER_CIK
: {
346 uint32_t base_lo_dw
= ac_ib_get(ib
);
347 ac_dump_reg(f
, R_3F0_IB_BASE_LO
, base_lo_dw
, ~0);
348 uint32_t base_hi_dw
= ac_ib_get(ib
);
349 ac_dump_reg(f
, R_3F1_IB_BASE_HI
, base_hi_dw
, ~0);
350 uint32_t control_dw
= ac_ib_get(ib
);
351 ac_dump_reg(f
, R_3F2_CONTROL
, control_dw
, ~0);
353 if (!ib
->addr_callback
)
356 uint64_t addr
= ((uint64_t)base_hi_dw
<< 32) | base_lo_dw
;
357 void *data
= ib
->addr_callback(ib
->addr_callback_data
, addr
);
361 if (G_3F2_CHAIN(control_dw
)) {
363 ib
->num_dw
= G_3F2_IB_SIZE(control_dw
);
368 struct ac_ib_parser ib_recurse
;
369 memcpy(&ib_recurse
, ib
, sizeof(ib_recurse
));
370 ib_recurse
.ib
= data
;
371 ib_recurse
.num_dw
= G_3F2_IB_SIZE(control_dw
);
372 ib_recurse
.cur_dw
= 0;
374 fprintf(f
, "\n\035>------------------ nested begin ------------------\n");
375 ac_do_parse_ib(f
, &ib_recurse
);
376 fprintf(f
, "\n\035<------------------- nested end -------------------\n");
379 case PKT3_CLEAR_STATE
:
380 case PKT3_INCREMENT_DE_COUNTER
:
381 case PKT3_PFP_SYNC_ME
:
384 if (header
== 0xffff1000) {
385 count
= -1; /* One dword NOP. */
386 } else if (count
== 0 && ib
->cur_dw
< ib
->num_dw
&&
387 AC_IS_TRACE_POINT(ib
->ib
[ib
->cur_dw
])) {
388 unsigned packet_id
= AC_GET_TRACE_POINT_ID(ib
->ib
[ib
->cur_dw
]);
390 print_spaces(f
, INDENT_PKT
);
391 fprintf(f
, COLOR_RED
"Trace point ID: %u\n", packet_id
);
393 if (ib
->trace_id
== -1)
394 break; /* tracing was disabled */
396 print_spaces(f
, INDENT_PKT
);
397 if (packet_id
< ib
->trace_id
)
399 "This trace point was reached by the CP."
401 else if (packet_id
== ib
->trace_id
)
403 "!!!!! This is the last trace point that "
404 "was reached by the CP !!!!!"
406 else if (packet_id
+1 == ib
->trace_id
)
408 "!!!!! This is the first trace point that "
409 "was NOT been reached by the CP !!!!!"
413 "!!!!! This trace point was NOT reached "
421 /* print additional dwords */
422 while (ib
->cur_dw
<= first_dw
+ count
)
425 if (ib
->cur_dw
> first_dw
+ count
+ 1)
426 fprintf(f
, COLOR_RED
"\n!!!!! count in header too low !!!!!"
431 * Parse and print an IB into a file.
433 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
)
435 while (ib
->cur_dw
< ib
->num_dw
) {
436 uint32_t header
= ac_ib_get(ib
);
437 unsigned type
= PKT_TYPE_G(header
);
441 ac_parse_packet3(f
, header
, ib
);
445 if (header
== 0x80000000) {
446 fprintf(f
, COLOR_GREEN
"NOP (type 2)" COLOR_RESET
"\n");
451 fprintf(f
, "Unknown packet type %i\n", type
);
457 static void format_ib_output(FILE *f
, char *out
)
464 if (out
[0] == '\n' && out
[1] == '\035')
466 if (out
[0] == '\035') {
474 unsigned indent
= 4 * depth
;
479 print_spaces(f
, indent
);
481 char *end
= strchrnul(out
, '\n');
482 fwrite(out
, end
- out
, 1, f
);
483 fputc('\n', f
); /* always end with a new line */
495 * Parse and print an IB into a file.
499 * \param num_dw size of the IB
500 * \param chip_class chip class
501 * \param trace_id the last trace ID that is known to have been reached
502 * and executed by the CP, typically read from a buffer
503 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
505 * \param addr_callback_data user data for addr_callback
507 void ac_parse_ib_chunk(FILE *f
, uint32_t *ib_ptr
, int num_dw
, int trace_id
,
508 enum chip_class chip_class
,
509 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
511 struct ac_ib_parser ib
= {};
514 ib
.trace_id
= trace_id
;
515 ib
.chip_class
= chip_class
;
516 ib
.addr_callback
= addr_callback
;
517 ib
.addr_callback_data
= addr_callback_data
;
521 FILE *memf
= open_memstream(&out
, &outsize
);
523 ac_do_parse_ib(memf
, &ib
);
527 format_ib_output(f
, out
);
531 if (ib
.cur_dw
> ib
.num_dw
) {
532 printf("\nPacket ends after the end of IB.\n");
538 * Parse and print an IB into a file.
542 * \param num_dw size of the IB
543 * \param chip_class chip class
544 * \param trace_id the last trace ID that is known to have been reached
545 * and executed by the CP, typically read from a buffer
546 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
548 * \param addr_callback_data user data for addr_callback
550 void ac_parse_ib(FILE *f
, uint32_t *ib
, int num_dw
, int trace_id
,
551 const char *name
, enum chip_class chip_class
,
552 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
554 fprintf(f
, "------------------ %s begin ------------------\n", name
);
556 ac_parse_ib_chunk(f
, ib
, num_dw
, trace_id
, chip_class
, addr_callback
,
559 fprintf(f
, "------------------- %s end -------------------\n\n", name
);