ac/debug: Move IB decode to common code.
[mesa.git] / src / amd / common / ac_debug.c
1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * Marek Olšák <maraeo@gmail.com>
25 */
26
27 #include "ac_debug.h"
28
29 #include "sid.h"
30 #include "sid_tables.h"
31 #include "util/u_math.h"
32 #include "util/u_memory.h"
33
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.
36 */
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"
42
43 #define INDENT_PKT 8
44
45 static void print_spaces(FILE *f, unsigned num)
46 {
47 fprintf(f, "%*s", num, "");
48 }
49
50 static void print_value(FILE *file, uint32_t value, int bits)
51 {
52 /* Guess if it's int or float */
53 if (value <= (1 << 15)) {
54 if (value <= 9)
55 fprintf(file, "%u\n", value);
56 else
57 fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
58 } else {
59 float f = uif(value);
60
61 if (fabs(f) < 100000 && f*10 == floor(f*10))
62 fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
63 else
64 /* Don't print more leading zeros than there are bits. */
65 fprintf(file, "0x%0*x\n", bits / 4, value);
66 }
67 }
68
69 static void print_named_value(FILE *file, const char *name, uint32_t value,
70 int bits)
71 {
72 print_spaces(file, INDENT_PKT);
73 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ", name);
74 print_value(file, value, bits);
75 }
76
77 void ac_dump_reg(FILE *file, unsigned offset, uint32_t value,
78 uint32_t field_mask)
79 {
80 int r, f;
81
82 for (r = 0; r < ARRAY_SIZE(sid_reg_table); r++) {
83 const struct si_reg *reg = &sid_reg_table[r];
84 const char *reg_name = sid_strings + reg->name_offset;
85
86 if (reg->offset == offset) {
87 bool first_field = true;
88
89 print_spaces(file, INDENT_PKT);
90 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
91 reg_name);
92
93 if (!reg->num_fields) {
94 print_value(file, value, 32);
95 return;
96 }
97
98 for (f = 0; f < reg->num_fields; f++) {
99 const struct si_field *field = sid_fields_table + reg->fields_offset + f;
100 const int *values_offsets = sid_strings_offsets + field->values_offset;
101 uint32_t val = (value & field->mask) >>
102 (ffs(field->mask) - 1);
103
104 if (!(field->mask & field_mask))
105 continue;
106
107 /* Indent the field. */
108 if (!first_field)
109 print_spaces(file,
110 INDENT_PKT + strlen(reg_name) + 4);
111
112 /* Print the field. */
113 fprintf(file, "%s = ", sid_strings + field->name_offset);
114
115 if (val < field->num_values && values_offsets[val] >= 0)
116 fprintf(file, "%s\n", sid_strings + values_offsets[val]);
117 else
118 print_value(file, val,
119 util_bitcount(field->mask));
120
121 first_field = false;
122 }
123 return;
124 }
125 }
126
127 print_spaces(file, INDENT_PKT);
128 fprintf(file, COLOR_YELLOW "0x%05x" COLOR_RESET " <- 0x%08x\n", offset, value);
129 }
130
131 static void ac_parse_set_reg_packet(FILE *f, uint32_t *ib, unsigned count,
132 unsigned reg_offset)
133 {
134 unsigned reg = (ib[1] << 2) + reg_offset;
135 int i;
136
137 for (i = 0; i < count; i++)
138 ac_dump_reg(f, reg + i*4, ib[2+i], ~0);
139 }
140
141 static uint32_t *ac_parse_packet3(FILE *f, uint32_t *ib, int *num_dw,
142 int trace_id, enum chip_class chip_class)
143 {
144 unsigned count = PKT_COUNT_G(ib[0]);
145 unsigned op = PKT3_IT_OPCODE_G(ib[0]);
146 const char *predicate = PKT3_PREDICATE(ib[0]) ? "(predicate)" : "";
147 int i;
148
149 /* Print the name first. */
150 for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
151 if (packet3_table[i].op == op)
152 break;
153
154 if (i < ARRAY_SIZE(packet3_table)) {
155 const char *name = sid_strings + packet3_table[i].name_offset;
156
157 if (op == PKT3_SET_CONTEXT_REG ||
158 op == PKT3_SET_CONFIG_REG ||
159 op == PKT3_SET_UCONFIG_REG ||
160 op == PKT3_SET_SH_REG)
161 fprintf(f, COLOR_CYAN "%s%s" COLOR_CYAN ":\n",
162 name, predicate);
163 else
164 fprintf(f, COLOR_GREEN "%s%s" COLOR_RESET ":\n",
165 name, predicate);
166 } else
167 fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s" COLOR_RESET ":\n",
168 op, predicate);
169
170 /* Print the contents. */
171 switch (op) {
172 case PKT3_SET_CONTEXT_REG:
173 ac_parse_set_reg_packet(f, ib, count, SI_CONTEXT_REG_OFFSET);
174 break;
175 case PKT3_SET_CONFIG_REG:
176 ac_parse_set_reg_packet(f, ib, count, SI_CONFIG_REG_OFFSET);
177 break;
178 case PKT3_SET_UCONFIG_REG:
179 ac_parse_set_reg_packet(f, ib, count, CIK_UCONFIG_REG_OFFSET);
180 break;
181 case PKT3_SET_SH_REG:
182 ac_parse_set_reg_packet(f, ib, count, SI_SH_REG_OFFSET);
183 break;
184 case PKT3_ACQUIRE_MEM:
185 ac_dump_reg(f, R_0301F0_CP_COHER_CNTL, ib[1], ~0);
186 ac_dump_reg(f, R_0301F4_CP_COHER_SIZE, ib[2], ~0);
187 ac_dump_reg(f, R_030230_CP_COHER_SIZE_HI, ib[3], ~0);
188 ac_dump_reg(f, R_0301F8_CP_COHER_BASE, ib[4], ~0);
189 ac_dump_reg(f, R_0301E4_CP_COHER_BASE_HI, ib[5], ~0);
190 print_named_value(f, "POLL_INTERVAL", ib[6], 16);
191 break;
192 case PKT3_SURFACE_SYNC:
193 if (chip_class >= CIK) {
194 ac_dump_reg(f, R_0301F0_CP_COHER_CNTL, ib[1], ~0);
195 ac_dump_reg(f, R_0301F4_CP_COHER_SIZE, ib[2], ~0);
196 ac_dump_reg(f, R_0301F8_CP_COHER_BASE, ib[3], ~0);
197 } else {
198 ac_dump_reg(f, R_0085F0_CP_COHER_CNTL, ib[1], ~0);
199 ac_dump_reg(f, R_0085F4_CP_COHER_SIZE, ib[2], ~0);
200 ac_dump_reg(f, R_0085F8_CP_COHER_BASE, ib[3], ~0);
201 }
202 print_named_value(f, "POLL_INTERVAL", ib[4], 16);
203 break;
204 case PKT3_EVENT_WRITE:
205 ac_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, ib[1],
206 S_028A90_EVENT_TYPE(~0));
207 print_named_value(f, "EVENT_INDEX", (ib[1] >> 8) & 0xf, 4);
208 print_named_value(f, "INV_L2", (ib[1] >> 20) & 0x1, 1);
209 if (count > 0) {
210 print_named_value(f, "ADDRESS_LO", ib[2], 32);
211 print_named_value(f, "ADDRESS_HI", ib[3], 16);
212 }
213 break;
214 case PKT3_DRAW_INDEX_AUTO:
215 ac_dump_reg(f, R_030930_VGT_NUM_INDICES, ib[1], ~0);
216 ac_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[2], ~0);
217 break;
218 case PKT3_DRAW_INDEX_2:
219 ac_dump_reg(f, R_028A78_VGT_DMA_MAX_SIZE, ib[1], ~0);
220 ac_dump_reg(f, R_0287E8_VGT_DMA_BASE, ib[2], ~0);
221 ac_dump_reg(f, R_0287E4_VGT_DMA_BASE_HI, ib[3], ~0);
222 ac_dump_reg(f, R_030930_VGT_NUM_INDICES, ib[4], ~0);
223 ac_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ib[5], ~0);
224 break;
225 case PKT3_INDEX_TYPE:
226 ac_dump_reg(f, R_028A7C_VGT_DMA_INDEX_TYPE, ib[1], ~0);
227 break;
228 case PKT3_NUM_INSTANCES:
229 ac_dump_reg(f, R_030934_VGT_NUM_INSTANCES, ib[1], ~0);
230 break;
231 case PKT3_WRITE_DATA:
232 ac_dump_reg(f, R_370_CONTROL, ib[1], ~0);
233 ac_dump_reg(f, R_371_DST_ADDR_LO, ib[2], ~0);
234 ac_dump_reg(f, R_372_DST_ADDR_HI, ib[3], ~0);
235 for (i = 2; i < count; i++) {
236 print_spaces(f, INDENT_PKT);
237 fprintf(f, "0x%08x\n", ib[2+i]);
238 }
239 break;
240 case PKT3_CP_DMA:
241 ac_dump_reg(f, R_410_CP_DMA_WORD0, ib[1], ~0);
242 ac_dump_reg(f, R_411_CP_DMA_WORD1, ib[2], ~0);
243 ac_dump_reg(f, R_412_CP_DMA_WORD2, ib[3], ~0);
244 ac_dump_reg(f, R_413_CP_DMA_WORD3, ib[4], ~0);
245 ac_dump_reg(f, R_414_COMMAND, ib[5], ~0);
246 break;
247 case PKT3_DMA_DATA:
248 ac_dump_reg(f, R_500_DMA_DATA_WORD0, ib[1], ~0);
249 ac_dump_reg(f, R_501_SRC_ADDR_LO, ib[2], ~0);
250 ac_dump_reg(f, R_502_SRC_ADDR_HI, ib[3], ~0);
251 ac_dump_reg(f, R_503_DST_ADDR_LO, ib[4], ~0);
252 ac_dump_reg(f, R_504_DST_ADDR_HI, ib[5], ~0);
253 ac_dump_reg(f, R_414_COMMAND, ib[6], ~0);
254 break;
255 case PKT3_INDIRECT_BUFFER_SI:
256 case PKT3_INDIRECT_BUFFER_CONST:
257 case PKT3_INDIRECT_BUFFER_CIK:
258 ac_dump_reg(f, R_3F0_IB_BASE_LO, ib[1], ~0);
259 ac_dump_reg(f, R_3F1_IB_BASE_HI, ib[2], ~0);
260 ac_dump_reg(f, R_3F2_CONTROL, ib[3], ~0);
261 break;
262 case PKT3_CLEAR_STATE:
263 case PKT3_INCREMENT_DE_COUNTER:
264 case PKT3_PFP_SYNC_ME:
265 break;
266 case PKT3_NOP:
267 if (ib[0] == 0xffff1000) {
268 count = -1; /* One dword NOP. */
269 break;
270 } else if (count == 0 && AC_IS_TRACE_POINT(ib[1])) {
271 unsigned packet_id = AC_GET_TRACE_POINT_ID(ib[1]);
272
273 print_spaces(f, INDENT_PKT);
274 fprintf(f, COLOR_RED "Trace point ID: %u\n", packet_id);
275
276 if (trace_id == -1)
277 break; /* tracing was disabled */
278
279 print_spaces(f, INDENT_PKT);
280 if (packet_id < trace_id)
281 fprintf(f, COLOR_RED
282 "This trace point was reached by the CP."
283 COLOR_RESET "\n");
284 else if (packet_id == trace_id)
285 fprintf(f, COLOR_RED
286 "!!!!! This is the last trace point that "
287 "was reached by the CP !!!!!"
288 COLOR_RESET "\n");
289 else if (packet_id+1 == trace_id)
290 fprintf(f, COLOR_RED
291 "!!!!! This is the first trace point that "
292 "was NOT been reached by the CP !!!!!"
293 COLOR_RESET "\n");
294 else
295 fprintf(f, COLOR_RED
296 "!!!!! This trace point was NOT reached "
297 "by the CP !!!!!"
298 COLOR_RESET "\n");
299 break;
300 }
301 /* fall through, print all dwords */
302 default:
303 for (i = 0; i < count+1; i++) {
304 print_spaces(f, INDENT_PKT);
305 fprintf(f, "0x%08x\n", ib[1+i]);
306 }
307 }
308
309 ib += count + 2;
310 *num_dw -= count + 2;
311 return ib;
312 }
313
314 /**
315 * Parse and print an IB into a file.
316 *
317 * \param f file
318 * \param ib IB
319 * \param num_dw size of the IB
320 * \param chip_class chip class
321 * \param trace_id the last trace ID that is known to have been reached
322 * and executed by the CP, typically read from a buffer
323 */
324 void ac_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id,
325 const char *name, enum chip_class chip_class)
326 {
327 fprintf(f, "------------------ %s begin ------------------\n", name);
328
329 while (num_dw > 0) {
330 unsigned type = PKT_TYPE_G(ib[0]);
331
332 switch (type) {
333 case 3:
334 ib = ac_parse_packet3(f, ib, &num_dw, trace_id,
335 chip_class);
336 break;
337 case 2:
338 /* type-2 nop */
339 if (ib[0] == 0x80000000) {
340 fprintf(f, COLOR_GREEN "NOP (type 2)" COLOR_RESET "\n");
341 ib++;
342 break;
343 }
344 /* fall through */
345 default:
346 fprintf(f, "Unknown packet type %i\n", type);
347 return;
348 }
349 }
350
351 fprintf(f, "------------------- %s end -------------------\n", name);
352 if (num_dw < 0) {
353 printf("Packet ends after the end of IB.\n");
354 exit(0);
355 }
356 fprintf(f, "\n");
357 }