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)" : "";
153 /* Print the name first. */
154 for (i
= 0; i
< ARRAY_SIZE(packet3_table
); i
++)
155 if (packet3_table
[i
].op
== op
)
158 if (i
< ARRAY_SIZE(packet3_table
)) {
159 const char *name
= egd_strings
+ packet3_table
[i
].name_offset
;
161 if (op
== PKT3_SET_CONTEXT_REG
||
162 op
== PKT3_SET_CONFIG_REG
||
163 op
== PKT3_SET_UCONFIG_REG
||
164 op
== PKT3_SET_SH_REG
)
165 fprintf(f
, COLOR_CYAN
"%s%s" COLOR_CYAN
":\n",
168 fprintf(f
, COLOR_GREEN
"%s%s" COLOR_RESET
":\n",
171 fprintf(f
, COLOR_RED
"PKT3_UNKNOWN 0x%x%s" COLOR_RESET
":\n",
174 /* Print the contents. */
176 case PKT3_SET_CONTEXT_REG
:
177 ac_parse_set_reg_packet(f
, ib
, count
, EVERGREEN_CONTEXT_REG_OFFSET
);
179 case PKT3_SET_CONFIG_REG
:
180 ac_parse_set_reg_packet(f
, ib
, count
, EVERGREEN_CONFIG_REG_OFFSET
);
182 case PKT3_SURFACE_SYNC
:
183 eg_dump_reg(f
, R_0085F0_CP_COHER_CNTL
, ib
[1], ~0);
184 eg_dump_reg(f
, R_0085F4_CP_COHER_SIZE
, ib
[2], ~0);
185 eg_dump_reg(f
, R_0085F8_CP_COHER_BASE
, ib
[3], ~0);
186 print_named_value(f
, "POLL_INTERVAL", ib
[4], 16);
188 case PKT3_EVENT_WRITE
:
189 /* TODO dump VGT_EVENT_INITIATOR */
191 eg_dump_reg(f
, R_028A90_VGT_EVENT_INITIATOR
, ib
[1],
192 S_028A90_EVENT_TYPE(~0));
194 print_named_value(f
, "EVENT_INDEX", (ib
[1] >> 8) & 0xf, 4);
195 print_named_value(f
, "INV_L2", (ib
[1] >> 20) & 0x1, 1);
197 print_named_value(f
, "ADDRESS_LO", ib
[2], 32);
198 print_named_value(f
, "ADDRESS_HI", ib
[3], 16);
201 case PKT3_DRAW_INDEX_AUTO
:
202 eg_dump_reg(f
, R_008970_VGT_NUM_INDICES
, ib
[1], ~0);
203 eg_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ib
[2], ~0);
205 case PKT3_DRAW_INDEX_2
:
206 eg_dump_reg(f
, R_028A78_VGT_DMA_MAX_SIZE
, ib
[1], ~0);
207 eg_dump_reg(f
, R_0287E8_VGT_DMA_BASE
, ib
[2], ~0);
208 eg_dump_reg(f
, R_0287E4_VGT_DMA_BASE_HI
, ib
[3], ~0);
209 eg_dump_reg(f
, R_008970_VGT_NUM_INDICES
, ib
[4], ~0);
210 eg_dump_reg(f
, R_0287F0_VGT_DRAW_INITIATOR
, ib
[5], ~0);
212 case PKT3_INDEX_TYPE
:
213 eg_dump_reg(f
, R_028A7C_VGT_DMA_INDEX_TYPE
, ib
[1], ~0);
215 case PKT3_NUM_INSTANCES
:
216 eg_dump_reg(f
, R_028A88_VGT_NUM_INSTANCES
, ib
[1], ~0);
218 case PKT3_INDIRECT_BUFFER
:
220 case PKT3_PFP_SYNC_ME
:
223 if (ib
[0] == 0xffff1000) {
224 count
= -1; /* One dword NOP. */
226 } else if (count
== 0 && AC_IS_TRACE_POINT(ib
[1])) {
227 unsigned packet_id
= AC_GET_TRACE_POINT_ID(ib
[1]);
229 print_spaces(f
, INDENT_PKT
);
230 fprintf(f
, COLOR_RED
"Trace point ID: %u\n", packet_id
);
233 break; /* tracing was disabled */
235 print_spaces(f
, INDENT_PKT
);
236 if (packet_id
< trace_id
)
238 "This trace point was reached by the CP."
240 else if (packet_id
== trace_id
)
242 "!!!!! This is the last trace point that "
243 "was reached by the CP !!!!!"
245 else if (packet_id
+1 == trace_id
)
247 "!!!!! This is the first trace point that "
248 "was NOT been reached by the CP !!!!!"
252 "!!!!! This trace point was NOT reached "
257 /* fall through, print all dwords */
259 for (i
= 0; i
< count
+1; i
++) {
260 print_spaces(f
, INDENT_PKT
);
261 fprintf(f
, "0x%08x\n", ib
[1+i
]);
266 *num_dw
-= count
+ 2;
271 * Parse and print an IB into a file.
275 * \param num_dw size of the IB
276 * \param chip_class chip class
277 * \param trace_id the last trace ID that is known to have been reached
278 * and executed by the CP, typically read from a buffer
279 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
281 * \param addr_callback_data user data for addr_callback
283 static void eg_parse_ib(FILE *f
, uint32_t *ib
, int num_dw
, int trace_id
,
284 const char *name
, enum chip_class chip_class
,
285 ac_debug_addr_callback addr_callback
, void *addr_callback_data
)
287 fprintf(f
, "------------------ %s begin ------------------\n", name
);
290 unsigned type
= PKT_TYPE_G(ib
[0]);
294 ib
= ac_parse_packet3(f
, ib
, &num_dw
, trace_id
,
295 chip_class
, addr_callback
,
300 if (ib
[0] == 0x80000000) {
301 fprintf(f
, COLOR_GREEN
"NOP (type 2)" COLOR_RESET
"\n");
308 fprintf(f
, "Unknown packet type %i\n", type
);
313 fprintf(f
, "------------------- %s end -------------------\n", name
);
315 printf("Packet ends after the end of IB.\n");
321 static void eg_dump_last_ib(struct r600_context
*rctx
, FILE *f
)
323 int last_trace_id
= -1;
325 if (!rctx
->last_gfx
.ib
)
328 if (rctx
->last_trace_buf
) {
329 /* We are expecting that the ddebug pipe has already
330 * waited for the context, so this buffer should be idle.
331 * If the GPU is hung, there is no point in waiting for it.
333 uint32_t *map
= rctx
->b
.ws
->buffer_map(rctx
->last_trace_buf
->buf
,
335 PIPE_TRANSFER_UNSYNCHRONIZED
|
338 last_trace_id
= *map
;
341 eg_parse_ib(f
, rctx
->last_gfx
.ib
, rctx
->last_gfx
.num_dw
,
342 last_trace_id
, "IB", rctx
->b
.chip_class
,
347 void eg_dump_debug_state(struct pipe_context
*ctx
, FILE *f
,
350 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
352 eg_dump_last_ib(rctx
, f
);
354 fprintf(f
, "Done.\n");
357 radeon_clear_saved_cs(&rctx
->last_gfx
);
358 r600_resource_reference(&rctx
->last_trace_buf
, NULL
);