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>
26 #include "r600_pipe.h"
27 #include "evergreend.h"
29 #include "egd_tables.h"
31 #define AC_IS_TRACE_POINT(x) (((x) & 0xcafe0000) == 0xcafe0000)
32 #define AC_GET_TRACE_POINT_ID(x) ((x) & 0xffff)
34 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
35 * read them, or use "aha -b -f file" to convert them to html.
37 #define COLOR_RESET "\033[0m"
38 #define COLOR_RED "\033[31m"
39 #define COLOR_GREEN "\033[1;32m"
40 #define COLOR_YELLOW "\033[1;33m"
41 #define COLOR_CYAN "\033[1;36m"
45 typedef void *(*ac_debug_addr_callback
)(void *data
, uint64_t addr
);
46 static void print_spaces(FILE *f
, unsigned num
)
48 fprintf(f
, "%*s", num
, "");
51 static void print_value(FILE *file
, uint32_t value
, int bits
)
53 /* Guess if it's int or float */
54 if (value
<= (1 << 15)) {
56 fprintf(file
, "%u\n", value
);
58 fprintf(file
, "%u (0x%0*x)\n", value
, bits
/ 4, value
);
62 if (fabs(f
) < 100000 && f
*10 == floor(f
*10))
63 fprintf(file
, "%.1ff (0x%0*x)\n", f
, bits
/ 4, value
);
65 /* Don't print more leading zeros than there are bits. */
66 fprintf(file
, "0x%0*x\n", bits
/ 4, value
);
70 static void print_named_value(FILE *file
, const char *name
, uint32_t value
,
73 print_spaces(file
, INDENT_PKT
);
74 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ", name
);
75 print_value(file
, value
, bits
);
78 static void eg_dump_reg(FILE *file
, unsigned offset
, uint32_t value
,
83 for (r
= 0; r
< ARRAY_SIZE(egd_reg_table
); r
++) {
84 const struct eg_reg
*reg
= &egd_reg_table
[r
];
85 const char *reg_name
= egd_strings
+ reg
->name_offset
;
87 if (reg
->offset
== offset
) {
88 bool first_field
= true;
90 print_spaces(file
, INDENT_PKT
);
91 fprintf(file
, COLOR_YELLOW
"%s" COLOR_RESET
" <- ",
94 if (!reg
->num_fields
) {
95 print_value(file
, value
, 32);
99 for (f
= 0; f
< reg
->num_fields
; f
++) {
100 const struct eg_field
*field
= egd_fields_table
+ reg
->fields_offset
+ f
;
101 const int *values_offsets
= egd_strings_offsets
+ field
->values_offset
;
102 uint32_t val
= (value
& field
->mask
) >>
103 (ffs(field
->mask
) - 1);
105 if (!(field
->mask
& field_mask
))
108 /* Indent the field. */
111 INDENT_PKT
+ strlen(reg_name
) + 4);
113 /* Print the field. */
114 fprintf(file
, "%s = ", egd_strings
+ field
->name_offset
);
116 if (val
< field
->num_values
&& values_offsets
[val
] >= 0)
117 fprintf(file
, "%s\n", egd_strings
+ values_offsets
[val
]);
119 print_value(file
, val
,
120 util_bitcount(field
->mask
));
128 print_spaces(file
, INDENT_PKT
);
129 fprintf(file
, COLOR_YELLOW
"0x%05x" COLOR_RESET
" <- 0x%08x\n", offset
, value
);
133 static void ac_parse_set_reg_packet(FILE *f
, uint32_t *ib
, unsigned count
,
136 unsigned reg
= (ib
[1] << 2) + reg_offset
;
139 for (i
= 0; i
< count
; i
++)
140 eg_dump_reg(f
, reg
+ i
*4, ib
[2+i
], ~0);
143 static uint32_t *ac_parse_packet3(FILE *f
, uint32_t *ib
, int *num_dw
,
144 int trace_id
, enum chip_class chip_class
,
145 ac_debug_addr_callback addr_callback
,
146 void *addr_callback_data
)
148 unsigned count
= PKT_COUNT_G(ib
[0]);
149 unsigned op
= PKT3_IT_OPCODE_G(ib
[0]);
150 const char *predicate
= PKT3_PREDICATE(ib
[0]) ? "(predicate)" : "";
151 const char *compute_mode
= (ib
[0] & 0x2) ? "(C)" : "";
154 /* Print the name first. */
155 for (i
= 0; i
< ARRAY_SIZE(packet3_table
); i
++)
156 if (packet3_table
[i
].op
== op
)
159 if (i
< ARRAY_SIZE(packet3_table
)) {
160 const char *name
= egd_strings
+ packet3_table
[i
].name_offset
;
162 if (op
== PKT3_SET_CONTEXT_REG
||
163 op
== PKT3_SET_CONFIG_REG
||
164 op
== PKT3_SET_UCONFIG_REG
||
165 op
== PKT3_SET_SH_REG
)
166 fprintf(f
, COLOR_CYAN
"%s%s%s" COLOR_CYAN
":\n",
167 name
, compute_mode
, predicate
);
169 fprintf(f
, COLOR_GREEN
"%s%s%s" COLOR_RESET
":\n",
170 name
, compute_mode
, predicate
);
172 fprintf(f
, COLOR_RED
"PKT3_UNKNOWN 0x%x%s%s" COLOR_RESET
":\n",
173 op
, compute_mode
, predicate
);
175 /* Print the contents. */
177 case PKT3_SET_CONTEXT_REG
:
178 ac_parse_set_reg_packet(f
, ib
, count
, EVERGREEN_CONTEXT_REG_OFFSET
);
180 case PKT3_SET_CONFIG_REG
:
181 ac_parse_set_reg_packet(f
, ib
, count
, EVERGREEN_CONFIG_REG_OFFSET
);
183 case PKT3_SURFACE_SYNC
:
184 eg_dump_reg(f
, R_0085F0_CP_COHER_CNTL
, ib
[1], ~0);
185 eg_dump_reg(f
, R_0085F4_CP_COHER_SIZE
, ib
[2], ~0);
186 eg_dump_reg(f
, R_0085F8_CP_COHER_BASE
, ib
[3], ~0);
187 print_named_value(f
, "POLL_INTERVAL", ib
[4], 16);
189 case PKT3_EVENT_WRITE
:
190 /* TODO dump VGT_EVENT_INITIATOR */
192 eg_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, ib
[1],
193 S_028A90_EVENT_TYPE(~0));
195 print_named_value(f
, "EVENT_TYPE", ib
[1] & 0xff, 8);
196 print_named_value(f
, "EVENT_INDEX", (ib
[1] >> 8) & 0xf, 4);
197 print_named_value(f
, "INV_L2", (ib
[1] >> 20) & 0x1, 1);
199 print_named_value(f
, "ADDRESS_LO", ib
[2], 32);
200 print_named_value(f
, "ADDRESS_HI", ib
[3], 16);
203 case PKT3_DRAW_INDEX_AUTO
:
204 eg_dump_reg(f
, R_008970_VGT_NUM_INDICES
, ib
[1], ~0);
205 eg_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ib
[2], ~0);
207 case PKT3_DRAW_INDEX_2
:
208 eg_dump_reg(f
, R_028A78_VGT_DMA_MAX_SIZE
, ib
[1], ~0);
209 eg_dump_reg(f
, R_0287E8_VGT_DMA_BASE
, ib
[2], ~0);
210 eg_dump_reg(f
, R_0287E4_VGT_DMA_BASE_HI
, ib
[3], ~0);
211 eg_dump_reg(f
, R_008970_VGT_NUM_INDICES
, ib
[4], ~0);
212 eg_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ib
[5], ~0);
214 case PKT3_INDEX_TYPE
:
215 eg_dump_reg(f
, R_028A7C_VGT_DMA_INDEX_TYPE
, ib
[1], ~0);
217 case PKT3_NUM_INSTANCES
:
218 eg_dump_reg(f
, R_028A88_VGT_NUM_INSTANCES
, ib
[1], ~0);
220 case PKT3_INDIRECT_BUFFER
:
222 case PKT3_PFP_SYNC_ME
:
225 if (ib
[0] == 0xffff1000) {
226 count
= -1; /* One dword NOP. */
228 } else if (count
== 0 && AC_IS_TRACE_POINT(ib
[1])) {
229 unsigned packet_id
= AC_GET_TRACE_POINT_ID(ib
[1]);
231 print_spaces(f
, INDENT_PKT
);
232 fprintf(f
, COLOR_RED
"Trace point ID: %u\n", packet_id
);
235 break; /* tracing was disabled */
237 print_spaces(f
, INDENT_PKT
);
238 if (packet_id
< trace_id
)
240 "This trace point was reached by the CP."
242 else if (packet_id
== trace_id
)
244 "!!!!! This is the last trace point that "
245 "was reached by the CP !!!!!"
247 else if (packet_id
+1 == trace_id
)
249 "!!!!! This is the first trace point that "
250 "was NOT been reached by the CP !!!!!"
254 "!!!!! This trace point was NOT reached "
259 /* fallthrough *//* print all dwords */
261 for (i
= 0; i
< count
+1; i
++) {
262 print_spaces(f
, INDENT_PKT
);
263 fprintf(f
, "0x%08x\n", ib
[1+i
]);
268 *num_dw
-= count
+ 2;
273 * Parse and print an IB into a file.
277 * \param num_dw size of the IB
278 * \param chip_class chip class
279 * \param trace_id the last trace ID that is known to have been reached
280 * and executed by the CP, typically read from a buffer
281 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
283 * \param addr_callback_data user data for addr_callback
285 static void eg_parse_ib(FILE *f
, uint32_t *ib
, int num_dw
, int trace_id
,
286 const char *name
, enum chip_class chip_class
,
287 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
289 fprintf(f
, "------------------ %s begin ------------------\n", name
);
292 unsigned type
= PKT_TYPE_G(ib
[0]);
296 ib
= ac_parse_packet3(f
, ib
, &num_dw
, trace_id
,
297 chip_class
, addr_callback
,
302 if (ib
[0] == 0x80000000) {
303 fprintf(f
, COLOR_GREEN
"NOP (type 2)" COLOR_RESET
"\n");
310 fprintf(f
, "Unknown packet type %i\n", type
);
315 fprintf(f
, "------------------- %s end -------------------\n", name
);
317 printf("Packet ends after the end of IB.\n");
323 static void eg_dump_last_ib(struct r600_context
*rctx
, FILE *f
)
325 int last_trace_id
= -1;
327 if (!rctx
->last_gfx
.ib
)
330 if (rctx
->last_trace_buf
) {
331 /* We are expecting that the ddebug pipe has already
332 * waited for the context, so this buffer should be idle.
333 * If the GPU is hung, there is no point in waiting for it.
335 uint32_t *map
= rctx
->b
.ws
->buffer_map(rctx
->last_trace_buf
->buf
,
337 PIPE_TRANSFER_UNSYNCHRONIZED
|
340 last_trace_id
= *map
;
343 eg_parse_ib(f
, rctx
->last_gfx
.ib
, rctx
->last_gfx
.num_dw
,
344 last_trace_id
, "IB", rctx
->b
.chip_class
,
349 void eg_dump_debug_state(struct pipe_context
*ctx
, FILE *f
,
352 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
354 eg_dump_last_ib(rctx
, f
);
356 fprintf(f
, "Done.\n");
359 radeon_clear_saved_cs(&rctx
->last_gfx
);
360 r600_resource_reference(&rctx
->last_trace_buf
, NULL
);