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"
43 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
44 * read them, or use "aha -b -f file" to convert them to html.
46 #define COLOR_RESET "\033[0m"
47 #define COLOR_RED "\033[31m"
48 #define COLOR_GREEN "\033[1;32m"
49 #define COLOR_YELLOW "\033[1;33m"
50 #define COLOR_CYAN "\033[1;36m"
59 enum chip_class chip_class
;
60 ac_debug_addr_callback addr_callback
;
61 void *addr_callback_data
;
66 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
);
68 static void print_spaces(FILE *f
, unsigned num
)
70 fprintf(f
, "%*s", num
, "");
73 static void print_value(FILE *file
, uint32_t value
, int bits
)
75 /* Guess if it's int or float */
76 if (value
<= (1 << 15)) {
78 fprintf(file
, "%u\n", value
);
80 fprintf(file
, "%u (0x%0*x)\n", value
, bits
/ 4, value
);
84 if (fabs(f
) < 100000 && f
*10 == floor(f
*10))
85 fprintf(file
, "%.1ff (0x%0*x)\n", f
, bits
/ 4, value
);
87 /* Don't print more leading zeros than there are bits. */
88 fprintf(file
, "0x%0*x\n", bits
/ 4, value
);
92 static void print_named_value(FILE *file
, const char *name
, uint32_t value
,
95 print_spaces(file
, INDENT_PKT
);
96 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ", name
);
97 print_value(file
, value
, bits
);
100 void ac_dump_reg(FILE *file
, unsigned offset
, uint32_t value
,
105 for (r
= 0; r
< ARRAY_SIZE(sid_reg_table
); r
++) {
106 const struct si_reg
*reg
= &sid_reg_table
[r
];
107 const char *reg_name
= sid_strings
+ reg
->name_offset
;
109 if (reg
->offset
== offset
) {
110 bool first_field
= true;
112 print_spaces(file
, INDENT_PKT
);
113 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ",
116 if (!reg
->num_fields
) {
117 print_value(file
, value
, 32);
121 for (f
= 0; f
< reg
->num_fields
; f
++) {
122 const struct si_field
*field
= sid_fields_table
+ reg
->fields_offset
+ f
;
123 const int *values_offsets
= sid_strings_offsets
+ field
->values_offset
;
124 uint32_t val
= (value
& field
->mask
) >>
125 (ffs(field
->mask
) - 1);
127 if (!(field
->mask
& field_mask
))
130 /* Indent the field. */
133 INDENT_PKT
+ strlen(reg_name
) + 4);
135 /* Print the field. */
136 fprintf(file
, "%s = ", sid_strings
+ field
->name_offset
);
138 if (val
< field
->num_values
&& values_offsets
[val
] >= 0)
139 fprintf(file
, "%s\n", sid_strings
+ values_offsets
[val
]);
141 print_value(file
, val
,
142 util_bitcount(field
->mask
));
150 print_spaces(file
, INDENT_PKT
);
151 fprintf(file
, COLOR_YELLOW
"0x%05x" COLOR_RESET
" <- 0x%08x\n", offset
, value
);
154 static uint32_t ac_ib_get(struct ac_ib_parser
*ib
)
158 if (ib
->cur_dw
< ib
->num_dw
) {
159 v
= ib
->ib
[ib
->cur_dw
];
161 /* Help figure out where garbage data is written to IBs.
163 * Arguably we should do this already when the IBs are written,
164 * see RADEON_VALGRIND. The problem is that client-requests to
165 * Valgrind have an overhead even when Valgrind isn't running,
166 * and radeon_emit is performance sensitive...
168 if (VALGRIND_CHECK_VALUE_IS_DEFINED(v
))
169 fprintf(ib
->f
, COLOR_RED
"Valgrind: The next DWORD is garbage"
172 fprintf(ib
->f
, "\n\035#%08x ", v
);
174 fprintf(ib
->f
, "\n\035#???????? ");
181 static void ac_parse_set_reg_packet(FILE *f
, unsigned count
, unsigned reg_offset
,
182 struct ac_ib_parser
*ib
)
184 unsigned reg_dw
= ac_ib_get(ib
);
185 unsigned reg
= ((reg_dw
& 0xFFFF) << 2) + reg_offset
;
186 unsigned index
= reg_dw
>> 28;
190 print_spaces(f
, INDENT_PKT
);
191 fprintf(f
, "INDEX = %u\n", index
);
194 for (i
= 0; i
< count
; i
++)
195 ac_dump_reg(f
, reg
+ i
*4, ac_ib_get(ib
), ~0);
198 static void ac_parse_packet3(FILE *f
, uint32_t header
, struct ac_ib_parser
*ib
)
200 unsigned first_dw
= ib
->cur_dw
;
201 int count
= PKT_COUNT_G(header
);
202 unsigned op
= PKT3_IT_OPCODE_G(header
);
203 const char *predicate
= PKT3_PREDICATE(header
) ? "(predicate)" : "";
206 /* Print the name first. */
207 for (i
= 0; i
< ARRAY_SIZE(packet3_table
); i
++)
208 if (packet3_table
[i
].op
== op
)
211 if (i
< ARRAY_SIZE(packet3_table
)) {
212 const char *name
= sid_strings
+ packet3_table
[i
].name_offset
;
214 if (op
== PKT3_SET_CONTEXT_REG
||
215 op
== PKT3_SET_CONFIG_REG
||
216 op
== PKT3_SET_UCONFIG_REG
||
217 op
== PKT3_SET_SH_REG
)
218 fprintf(f
, COLOR_CYAN
"%s%s" COLOR_CYAN
":\n",
221 fprintf(f
, COLOR_GREEN
"%s%s" COLOR_RESET
":\n",
224 fprintf(f
, COLOR_RED
"PKT3_UNKNOWN 0x%x%s" COLOR_RESET
":\n",
227 /* Print the contents. */
229 case PKT3_SET_CONTEXT_REG
:
230 ac_parse_set_reg_packet(f
, count
, SI_CONTEXT_REG_OFFSET
, ib
);
232 case PKT3_SET_CONFIG_REG
:
233 ac_parse_set_reg_packet(f
, count
, SI_CONFIG_REG_OFFSET
, ib
);
235 case PKT3_SET_UCONFIG_REG
:
236 ac_parse_set_reg_packet(f
, count
, CIK_UCONFIG_REG_OFFSET
, ib
);
238 case PKT3_SET_SH_REG
:
239 ac_parse_set_reg_packet(f
, count
, SI_SH_REG_OFFSET
, ib
);
241 case PKT3_ACQUIRE_MEM
:
242 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
243 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
244 ac_dump_reg(f
, R_030230_CP_COHER_SIZE_HI
, ac_ib_get(ib
), ~0);
245 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
246 ac_dump_reg(f
, R_0301E4_CP_COHER_BASE_HI
, ac_ib_get(ib
), ~0);
247 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
249 case PKT3_SURFACE_SYNC
:
250 if (ib
->chip_class
>= CIK
) {
251 ac_dump_reg(f
, R_0301F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
252 ac_dump_reg(f
, R_0301F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
253 ac_dump_reg(f
, R_0301F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
255 ac_dump_reg(f
, R_0085F0_CP_COHER_CNTL
, ac_ib_get(ib
), ~0);
256 ac_dump_reg(f
, R_0085F4_CP_COHER_SIZE
, ac_ib_get(ib
), ~0);
257 ac_dump_reg(f
, R_0085F8_CP_COHER_BASE
, ac_ib_get(ib
), ~0);
259 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
261 case PKT3_EVENT_WRITE
: {
262 uint32_t event_dw
= ac_ib_get(ib
);
263 ac_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, event_dw
,
264 S_028A90_EVENT_TYPE(~0));
265 print_named_value(f
, "EVENT_INDEX", (event_dw
>> 8) & 0xf, 4);
266 print_named_value(f
, "INV_L2", (event_dw
>> 20) & 0x1, 1);
268 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
269 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 16);
273 case PKT3_EVENT_WRITE_EOP
: {
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
, "ADDRESS_LO", ac_ib_get(ib
), 32);
284 uint32_t addr_hi_dw
= ac_ib_get(ib
);
285 print_named_value(f
, "ADDRESS_HI", addr_hi_dw
, 16);
286 print_named_value(f
, "DST_SEL", (addr_hi_dw
>> 16) & 0x3, 2);
287 print_named_value(f
, "INT_SEL", (addr_hi_dw
>> 24) & 0x7, 3);
288 print_named_value(f
, "DATA_SEL", addr_hi_dw
>> 29, 3);
289 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
290 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
293 case PKT3_RELEASE_MEM
: {
294 uint32_t event_dw
= ac_ib_get(ib
);
295 ac_dump_reg(f
, 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
, "TC_NC_ACTION_ENA", (event_dw
>> 19) & 0x1, 1);
304 print_named_value(f
, "TC_WC_ACTION_ENA", (event_dw
>> 20) & 0x1, 1);
305 print_named_value(f
, "TC_MD_ACTION_ENA", (event_dw
>> 21) & 0x1, 1);
306 uint32_t sel_dw
= ac_ib_get(ib
);
307 print_named_value(f
, "DST_SEL", (sel_dw
>> 16) & 0x3, 2);
308 print_named_value(f
, "INT_SEL", (sel_dw
>> 24) & 0x7, 3);
309 print_named_value(f
, "DATA_SEL", sel_dw
>> 29, 3);
310 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
311 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
312 print_named_value(f
, "DATA_LO", ac_ib_get(ib
), 32);
313 print_named_value(f
, "DATA_HI", ac_ib_get(ib
), 32);
314 print_named_value(f
, "CTXID", ac_ib_get(ib
), 32);
317 case PKT3_WAIT_REG_MEM
:
318 print_named_value(f
, "OP", ac_ib_get(ib
), 32);
319 print_named_value(f
, "ADDRESS_LO", ac_ib_get(ib
), 32);
320 print_named_value(f
, "ADDRESS_HI", ac_ib_get(ib
), 32);
321 print_named_value(f
, "REF", ac_ib_get(ib
), 32);
322 print_named_value(f
, "MASK", ac_ib_get(ib
), 32);
323 print_named_value(f
, "POLL_INTERVAL", ac_ib_get(ib
), 16);
325 case PKT3_DRAW_INDEX_AUTO
:
326 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
327 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
329 case PKT3_DRAW_INDEX_2
:
330 ac_dump_reg(f
, R_028A78_VGT_DMA_MAX_SIZE
, ac_ib_get(ib
), ~0);
331 ac_dump_reg(f
, R_0287E8_VGT_DMA_BASE
, ac_ib_get(ib
), ~0);
332 ac_dump_reg(f
, R_0287E4_VGT_DMA_BASE_HI
, ac_ib_get(ib
), ~0);
333 ac_dump_reg(f
, R_030930_VGT_NUM_INDICES
, ac_ib_get(ib
), ~0);
334 ac_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ac_ib_get(ib
), ~0);
336 case PKT3_INDEX_TYPE
:
337 ac_dump_reg(f
, R_028A7C_VGT_DMA_INDEX_TYPE
, ac_ib_get(ib
), ~0);
339 case PKT3_NUM_INSTANCES
:
340 ac_dump_reg(f
, R_030934_VGT_NUM_INSTANCES
, ac_ib_get(ib
), ~0);
342 case PKT3_WRITE_DATA
:
343 ac_dump_reg(f
, R_370_CONTROL
, ac_ib_get(ib
), ~0);
344 ac_dump_reg(f
, R_371_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
345 ac_dump_reg(f
, R_372_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
346 /* The payload is written automatically */
349 ac_dump_reg(f
, R_410_CP_DMA_WORD0
, ac_ib_get(ib
), ~0);
350 ac_dump_reg(f
, R_411_CP_DMA_WORD1
, ac_ib_get(ib
), ~0);
351 ac_dump_reg(f
, R_412_CP_DMA_WORD2
, ac_ib_get(ib
), ~0);
352 ac_dump_reg(f
, R_413_CP_DMA_WORD3
, ac_ib_get(ib
), ~0);
353 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
356 ac_dump_reg(f
, R_500_DMA_DATA_WORD0
, ac_ib_get(ib
), ~0);
357 ac_dump_reg(f
, R_501_SRC_ADDR_LO
, ac_ib_get(ib
), ~0);
358 ac_dump_reg(f
, R_502_SRC_ADDR_HI
, ac_ib_get(ib
), ~0);
359 ac_dump_reg(f
, R_503_DST_ADDR_LO
, ac_ib_get(ib
), ~0);
360 ac_dump_reg(f
, R_504_DST_ADDR_HI
, ac_ib_get(ib
), ~0);
361 ac_dump_reg(f
, R_414_COMMAND
, ac_ib_get(ib
), ~0);
363 case PKT3_INDIRECT_BUFFER_SI
:
364 case PKT3_INDIRECT_BUFFER_CONST
:
365 case PKT3_INDIRECT_BUFFER_CIK
: {
366 uint32_t base_lo_dw
= ac_ib_get(ib
);
367 ac_dump_reg(f
, R_3F0_IB_BASE_LO
, base_lo_dw
, ~0);
368 uint32_t base_hi_dw
= ac_ib_get(ib
);
369 ac_dump_reg(f
, R_3F1_IB_BASE_HI
, base_hi_dw
, ~0);
370 uint32_t control_dw
= ac_ib_get(ib
);
371 ac_dump_reg(f
, R_3F2_CONTROL
, control_dw
, ~0);
373 if (!ib
->addr_callback
)
376 uint64_t addr
= ((uint64_t)base_hi_dw
<< 32) | base_lo_dw
;
377 void *data
= ib
->addr_callback(ib
->addr_callback_data
, addr
);
381 if (G_3F2_CHAIN(control_dw
)) {
383 ib
->num_dw
= G_3F2_IB_SIZE(control_dw
);
388 struct ac_ib_parser ib_recurse
;
389 memcpy(&ib_recurse
, ib
, sizeof(ib_recurse
));
390 ib_recurse
.ib
= data
;
391 ib_recurse
.num_dw
= G_3F2_IB_SIZE(control_dw
);
392 ib_recurse
.cur_dw
= 0;
394 fprintf(f
, "\n\035>------------------ nested begin ------------------\n");
395 ac_do_parse_ib(f
, &ib_recurse
);
396 fprintf(f
, "\n\035<------------------- nested end -------------------\n");
399 case PKT3_CLEAR_STATE
:
400 case PKT3_INCREMENT_DE_COUNTER
:
401 case PKT3_PFP_SYNC_ME
:
404 if (header
== 0xffff1000) {
405 count
= -1; /* One dword NOP. */
406 } else if (count
== 0 && ib
->cur_dw
< ib
->num_dw
&&
407 AC_IS_TRACE_POINT(ib
->ib
[ib
->cur_dw
])) {
408 unsigned packet_id
= AC_GET_TRACE_POINT_ID(ib
->ib
[ib
->cur_dw
]);
410 print_spaces(f
, INDENT_PKT
);
411 fprintf(f
, COLOR_RED
"Trace point ID: %u\n", packet_id
);
413 if (ib
->trace_id
== -1)
414 break; /* tracing was disabled */
416 print_spaces(f
, INDENT_PKT
);
417 if (packet_id
< ib
->trace_id
)
419 "This trace point was reached by the CP."
421 else if (packet_id
== ib
->trace_id
)
423 "!!!!! This is the last trace point that "
424 "was reached by the CP !!!!!"
426 else if (packet_id
+1 == ib
->trace_id
)
428 "!!!!! This is the first trace point that "
429 "was NOT been reached by the CP !!!!!"
433 "!!!!! This trace point was NOT reached "
441 /* print additional dwords */
442 while (ib
->cur_dw
<= first_dw
+ count
)
445 if (ib
->cur_dw
> first_dw
+ count
+ 1)
446 fprintf(f
, COLOR_RED
"\n!!!!! count in header too low !!!!!"
451 * Parse and print an IB into a file.
453 static void ac_do_parse_ib(FILE *f
, struct ac_ib_parser
*ib
)
455 while (ib
->cur_dw
< ib
->num_dw
) {
456 uint32_t header
= ac_ib_get(ib
);
457 unsigned type
= PKT_TYPE_G(header
);
461 ac_parse_packet3(f
, header
, ib
);
465 if (header
== 0x80000000) {
466 fprintf(f
, COLOR_GREEN
"NOP (type 2)" COLOR_RESET
"\n");
471 fprintf(f
, "Unknown packet type %i\n", type
);
477 static void format_ib_output(FILE *f
, char *out
)
484 if (out
[0] == '\n' && out
[1] == '\035')
486 if (out
[0] == '\035') {
494 unsigned indent
= 4 * depth
;
499 print_spaces(f
, indent
);
501 char *end
= strchrnul(out
, '\n');
502 fwrite(out
, end
- out
, 1, f
);
503 fputc('\n', f
); /* always end with a new line */
515 * Parse and print an IB into a file.
519 * \param num_dw size of the IB
520 * \param chip_class chip class
521 * \param trace_id the last trace ID that is known to have been reached
522 * and executed by the CP, typically read from a buffer
523 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
525 * \param addr_callback_data user data for addr_callback
527 void ac_parse_ib_chunk(FILE *f
, uint32_t *ib_ptr
, int num_dw
, int trace_id
,
528 enum chip_class chip_class
,
529 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
531 struct ac_ib_parser ib
= {};
534 ib
.trace_id
= trace_id
;
535 ib
.chip_class
= chip_class
;
536 ib
.addr_callback
= addr_callback
;
537 ib
.addr_callback_data
= addr_callback_data
;
541 FILE *memf
= open_memstream(&out
, &outsize
);
543 ac_do_parse_ib(memf
, &ib
);
547 format_ib_output(f
, out
);
551 if (ib
.cur_dw
> ib
.num_dw
) {
552 printf("\nPacket ends after the end of IB.\n");
558 * Parse and print an IB into a file.
562 * \param num_dw size of the IB
563 * \param chip_class chip class
564 * \param trace_id the last trace ID that is known to have been reached
565 * and executed by the CP, typically read from a buffer
566 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
568 * \param addr_callback_data user data for addr_callback
570 void ac_parse_ib(FILE *f
, uint32_t *ib
, int num_dw
, int trace_id
,
571 const char *name
, enum chip_class chip_class
,
572 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
574 fprintf(f
, "------------------ %s begin ------------------\n", name
);
576 ac_parse_ib_chunk(f
, ib
, num_dw
, trace_id
, chip_class
, addr_callback
,
579 fprintf(f
, "------------------- %s end -------------------\n\n", name
);