Debian Codegraph @

src/asahi/lib/decode.c

raw
1 /*
2 * Copyright 2017-2019 Alyssa Rosenzweig
3 * Copyright 2017-2019 Connor Abbott
4 * Copyright 2019 Collabora, Ltd.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include <ctype.h>
9 #include <memory.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "util/os_misc.h"
15 #include "util/u_dynarray.h"
16 #include "util/u_math.h"
17 #include <sys/mman.h>
18 #include <agx_pack.h>
19
20 #include "asahi/isa/disasm.h"
21 #include "util/u_hexdump.h"
22 #include "decode.h"
23
24 struct libagxdecode_config lib_config;
25
26 static void
27 agx_disassemble(void *_code, size_t maxlen, FILE *fp)
28 {
29 bool errors = agx2_disassemble(_code, maxlen, fp);
30 assert(!errors);
31 }
32
33 FILE *agxdecode_dump_stream;
34
35 struct agxdecode_ctx {
36 struct util_dynarray mmap_array;
37 uint64_t shader_base;
38 };
39
40 static uint64_t
41 decode_usc(struct agxdecode_ctx *ctx, uint64_t addr)
42 {
43 return ctx->shader_base + addr;
44 }
45
46 struct agxdecode_ctx *
47 agxdecode_new_context(uint64_t shader_base)
48 {
49 struct agxdecode_ctx *ctx = calloc(1, sizeof(struct agxdecode_ctx));
50 ctx->shader_base = shader_base;
51 return ctx;
52 }
53
54 void
55 agxdecode_destroy_context(struct agxdecode_ctx *ctx)
56 {
57 free(ctx);
58 }
59
60 static struct agx_bo *
61 agxdecode_find_mapped_gpu_mem_containing(struct agxdecode_ctx *ctx,
62 uint64_t addr)
63 {
64 util_dynarray_foreach(&ctx->mmap_array, struct agx_bo, it) {
65 if (it->va && addr >= it->va->addr && (addr - it->va->addr) < it->size)
66 return it;
67 }
68
69 return NULL;
70 }
71
72 static struct agx_bo *
73 agxdecode_find_handle(struct agxdecode_ctx *ctx, unsigned handle)
74 {
75 util_dynarray_foreach(&ctx->mmap_array, struct agx_bo, it) {
76 if (it->handle == handle)
77 return it;
78 }
79
80 return NULL;
81 }
82
83 static size_t
84 _agxdecode_grab_mapped(struct agxdecode_ctx *ctx, uint64_t gpu_va, void **buf,
85 int line, const char *filename)
86 {
87 if (lib_config.read_gpu_mem)
88 UNREACHABLE("you'll have to figure it out.");
89
90 const struct agx_bo *mem =
91 agxdecode_find_mapped_gpu_mem_containing(ctx, gpu_va);
92
93 if (!mem) {
94 fprintf(stderr, "Access to unknown memory %" PRIx64 " in %s:%d\n", gpu_va,
95 filename, line);
96 fflush(agxdecode_dump_stream);
97 assert(0);
98 }
99
100 uint32_t offset = gpu_va - mem->va->addr;
101
102 *buf = mem->_map + offset;
103 return mem->size - offset;
104 }
105
106 static size_t
107 __agxdecode_fetch_gpu_mem(struct agxdecode_ctx *ctx, const struct agx_bo *mem,
108 uint64_t gpu_va, size_t size, void *buf, int line,
109 const char *filename)
110 {
111 if (lib_config.read_gpu_mem)
112 return lib_config.read_gpu_mem(gpu_va, size, buf);
113
114 if (!mem)
115 mem = agxdecode_find_mapped_gpu_mem_containing(ctx, gpu_va);
116
117 if (!mem) {
118 fprintf(stderr, "Access to unknown memory %" PRIx64 " in %s:%d\n", gpu_va,
119 filename, line);
120 fflush(agxdecode_dump_stream);
121 assert(0);
122 }
123
124 assert(mem);
125
126 if (size + (gpu_va - mem->va->addr) > mem->size) {
127 fprintf(stderr,
128 "Overflowing to unknown memory %" PRIx64
129 " of size %zu (max size %zu) in %s:%d\n",
130 gpu_va, size, (size_t)(mem->size - (gpu_va - mem->va->addr)),
131 filename, line);
132 fflush(agxdecode_dump_stream);
133 assert(0);
134 }
135
136 memcpy(buf, mem->_map + gpu_va - mem->va->addr, size);
137
138 return size;
139 }
140
141 #define agxdecode_fetch_gpu_mem(ctx, gpu_va, size, buf) \
142 __agxdecode_fetch_gpu_mem(ctx, NULL, gpu_va, size, buf, __LINE__, __FILE__)
143
144 #define agxdecode_grab_mapped(ctx, gpu_va, buf) \
145 _agxdecode_grab_mapped(ctx, gpu_va, buf, __LINE__, __FILE__)
146
147 #define agxdecode_fetch_gpu_array(ctx, gpu_va, buf) \
148 agxdecode_fetch_gpu_mem(ctx, gpu_va, sizeof(buf), buf)
149
150 /* Helpers for parsing the cmdstream */
151
152 #define DUMP_UNPACKED(T, var, str) \
153 { \
154 agxdecode_log(str); \
155 agx_print(agxdecode_dump_stream, T, var, 2); \
156 }
157
158 #define DUMP_CL(T, cl, str) \
159 { \
160 agx_unpack(agxdecode_dump_stream, cl, T, temp); \
161 DUMP_UNPACKED(T, temp, str "\n"); \
162 }
163
164 #define DUMP_FIELD(struct, fmt, field) \
165 { \
166 fprintf(agxdecode_dump_stream, #field " = " fmt "\n", struct->field); \
167 }
168
169 #define agxdecode_log(str) fputs(str, agxdecode_dump_stream)
170 #define agxdecode_msg(str) fprintf(agxdecode_dump_stream, "// %s", str)
171
172 typedef struct drm_asahi_params_global decoder_params;
173
174 /* Abstraction for command stream parsing */
175 typedef unsigned (*decode_cmd)(struct agxdecode_ctx *ctx, const uint8_t *map,
176 uint64_t *link, bool verbose,
177 decoder_params *params, void *data);
178
179 #define STATE_DONE (0xFFFFFFFFu)
180 #define STATE_LINK (0xFFFFFFFEu)
181 #define STATE_CALL (0xFFFFFFFDu)
182 #define STATE_RET (0xFFFFFFFCu)
183
184 static void
185 agxdecode_stateful(struct agxdecode_ctx *ctx, uint64_t va, const char *label,
186 decode_cmd decoder, bool verbose, decoder_params *params,
187 void *data)
188 {
189 uint64_t stack[16];
190 unsigned sp = 0;
191
192 uint8_t buf[1024];
193 size_t size = sizeof(buf);
194 if (!lib_config.read_gpu_mem) {
195 struct agx_bo *alloc = agxdecode_find_mapped_gpu_mem_containing(ctx, va);
196 assert(alloc != NULL && "nonexistent object");
197 fprintf(agxdecode_dump_stream, "%s (%" PRIx64 ", handle %u)\n", label, va,
198 alloc->handle);
199 size = MIN2(size, alloc->size - (va - alloc->va->addr));
200 } else {
201 fprintf(agxdecode_dump_stream, "%s (%" PRIx64 ")\n", label, va);
202 }
203 fflush(agxdecode_dump_stream);
204
205 int len = agxdecode_fetch_gpu_mem(ctx, va, size, buf);
206
207 int left = len;
208 uint8_t *map = buf;
209 uint64_t link = 0;
210
211 fflush(agxdecode_dump_stream);
212
213 while (left) {
214 if (len <= 0) {
215 fprintf(agxdecode_dump_stream, "!! Failed to read GPU memory\n");
216 fflush(agxdecode_dump_stream);
217 return;
218 }
219
220 unsigned count = decoder(ctx, map, &link, verbose, params, data);
221
222 /* If we fail to decode, default to a hexdump (don't hang) */
223 if (count == 0) {
224 u_hexdump(agxdecode_dump_stream, map, 8, false);
225 count = 8;
226 }
227
228 fflush(agxdecode_dump_stream);
229 if (count == STATE_DONE) {
230 break;
231 } else if (count == STATE_LINK) {
232 fprintf(agxdecode_dump_stream, "Linking to 0x%" PRIx64 "\n\n", link);
233 va = link;
234 left = len = agxdecode_fetch_gpu_array(ctx, va, buf);
235 map = buf;
236 } else if (count == STATE_CALL) {
237 fprintf(agxdecode_dump_stream,
238 "Calling 0x%" PRIx64 " (return = 0x%" PRIx64 ")\n\n", link,
239 va + 8);
240 assert(sp < ARRAY_SIZE(stack));
241 stack[sp++] = va + 8;
242 va = link;
243 left = len = agxdecode_fetch_gpu_array(ctx, va, buf);
244 map = buf;
245 } else if (count == STATE_RET) {
246 assert(sp > 0);
247 va = stack[--sp];
248 fprintf(agxdecode_dump_stream, "Returning to 0x%" PRIx64 "\n\n", va);
249 left = len = agxdecode_fetch_gpu_array(ctx, va, buf);
250 map = buf;
251 } else {
252 va += count;
253 map += count;
254 left -= count;
255
256 if (left < 512 && len == sizeof(buf)) {
257 left = len = agxdecode_fetch_gpu_array(ctx, va, buf);
258 map = buf;
259 }
260 }
261 }
262 }
263
264 static void
265 agxdecode_texture_pbe(struct agxdecode_ctx *ctx, const void *map)
266 {
267 struct AGX_TEXTURE tex;
268 struct AGX_PBE pbe;
269
270 bool valid_texture = AGX_TEXTURE_unpack(NULL, map, &tex);
271 bool valid_pbe = AGX_PBE_unpack(NULL, map, &pbe);
272
273 /* Try to guess if it's texture or PBE */
274 valid_texture &=
275 tex.swizzle_r <= AGX_CHANNEL_0 && tex.swizzle_g <= AGX_CHANNEL_0 &&
276 tex.swizzle_b <= AGX_CHANNEL_0 && tex.swizzle_a <= AGX_CHANNEL_0;
277
278 if (valid_texture && !valid_pbe) {
279 DUMP_CL(TEXTURE, map, "Texture");
280 } else if (valid_pbe && !valid_texture) {
281 DUMP_CL(PBE, map, "PBE");
282 } else {
283 if (!valid_texture) {
284 assert(!valid_pbe);
285 fprintf(agxdecode_dump_stream, "XXX: invalid texture/PBE\n");
286 }
287
288 DUMP_CL(TEXTURE, map, "Texture");
289 DUMP_CL(PBE, map, "PBE");
290 }
291 }
292
293 static unsigned
294 agxdecode_usc(struct agxdecode_ctx *ctx, const uint8_t *map,
295 UNUSED uint64_t *link, UNUSED bool verbose,
296 decoder_params *params, UNUSED void *data)
297 {
298 enum agx_sampler_states *sampler_states = data;
299 enum agx_usc_control type = map[0];
300
301 bool extended_samplers =
302 (sampler_states != NULL) &&
303 (((*sampler_states) == AGX_SAMPLER_STATES_8_EXTENDED) ||
304 ((*sampler_states) == AGX_SAMPLER_STATES_16_EXTENDED));
305
306 #define USC_CASE(name, human) \
307 case AGX_USC_CONTROL_##name: { \
308 DUMP_CL(USC_##name, map, human); \
309 return AGX_USC_##name##_LENGTH; \
310 }
311
312 switch (type) {
313 case AGX_USC_CONTROL_NO_PRESHADER: {
314 DUMP_CL(USC_NO_PRESHADER, map, "No preshader");
315 return STATE_DONE;
316 }
317
318 case AGX_USC_CONTROL_PRESHADER: {
319 agx_unpack(agxdecode_dump_stream, map, USC_PRESHADER, ctrl);
320 DUMP_UNPACKED(USC_PRESHADER, ctrl, "Preshader\n");
321
322 void *buf;
323 size_t size =
324 agxdecode_grab_mapped(ctx, decode_usc(ctx, ctrl.code), &buf);
325
326 agx_disassemble(buf, size, agxdecode_dump_stream);
327 return STATE_DONE;
328 }
329
330 case AGX_USC_CONTROL_SHADER: {
331 agx_unpack(agxdecode_dump_stream, map, USC_SHADER, ctrl);
332 DUMP_UNPACKED(USC_SHADER, ctrl, "Shader\n");
333
334 agxdecode_log("\n");
335 void *buf;
336 size_t size =
337 agxdecode_grab_mapped(ctx, decode_usc(ctx, ctrl.code), &buf);
338
339 agx_disassemble(buf, size, agxdecode_dump_stream);
340 agxdecode_log("\n");
341
342 return AGX_USC_SHADER_LENGTH;
343 }
344
345 case AGX_USC_CONTROL_SAMPLER: {
346 agx_unpack(agxdecode_dump_stream, map, USC_SAMPLER, temp);
347 DUMP_UNPACKED(USC_SAMPLER, temp, "Sampler state\n");
348
349 size_t stride =
350 AGX_SAMPLER_LENGTH + (extended_samplers ? AGX_BORDER_LENGTH : 0);
351 uint8_t *samp = alloca(stride * temp.count);
352
353 agxdecode_fetch_gpu_mem(ctx, temp.buffer, stride * temp.count, samp);
354
355 for (unsigned i = 0; i < temp.count; ++i) {
356 DUMP_CL(SAMPLER, samp, "Sampler");
357 samp += AGX_SAMPLER_LENGTH;
358
359 if (extended_samplers) {
360 DUMP_CL(BORDER, samp, "Border");
361 samp += AGX_BORDER_LENGTH;
362 }
363 }
364
365 return AGX_USC_SAMPLER_LENGTH;
366 }
367
368 case AGX_USC_CONTROL_TEXTURE: {
369 agx_unpack(agxdecode_dump_stream, map, USC_TEXTURE, temp);
370 DUMP_UNPACKED(USC_TEXTURE, temp, "Texture state\n");
371
372 uint8_t buf[AGX_TEXTURE_LENGTH * temp.count];
373 uint8_t *tex = buf;
374
375 agxdecode_fetch_gpu_array(ctx, temp.buffer, buf);
376
377 /* Note: samplers only need 8 byte alignment? */
378 for (unsigned i = 0; i < temp.count; ++i) {
379 fprintf(agxdecode_dump_stream, "ts%u: \n", temp.start + i);
380 agxdecode_texture_pbe(ctx, tex);
381
382 tex += AGX_TEXTURE_LENGTH;
383 }
384
385 return AGX_USC_TEXTURE_LENGTH;
386 }
387
388 case AGX_USC_CONTROL_UNIFORM: {
389 agx_unpack(agxdecode_dump_stream, map, USC_UNIFORM, temp);
390 DUMP_UNPACKED(USC_UNIFORM, temp, "Uniform\n");
391
392 uint8_t buf[2 * temp.size_halfs];
393 agxdecode_fetch_gpu_array(ctx, temp.buffer, buf);
394 u_hexdump(agxdecode_dump_stream, buf, 2 * temp.size_halfs, false);
395
396 return AGX_USC_UNIFORM_LENGTH;
397 }
398
399 case AGX_USC_CONTROL_UNIFORM_HIGH: {
400 agx_unpack(agxdecode_dump_stream, map, USC_UNIFORM_HIGH, temp);
401 DUMP_UNPACKED(USC_UNIFORM_HIGH, temp, "Uniform (high)\n");
402
403 uint8_t buf[2 * temp.size_halfs];
404 agxdecode_fetch_gpu_array(ctx, temp.buffer, buf);
405 u_hexdump(agxdecode_dump_stream, buf, 2 * temp.size_halfs, false);
406
407 return AGX_USC_UNIFORM_HIGH_LENGTH;
408 }
409
410 USC_CASE(FRAGMENT_PROPERTIES, "Fragment properties");
411 USC_CASE(SHARED, "Shared");
412 USC_CASE(REGISTERS, "Registers");
413
414 default:
415 fprintf(agxdecode_dump_stream, "Unknown USC control type: %u\n", type);
416 u_hexdump(agxdecode_dump_stream, map, 8, false);
417 return 8;
418 }
419
420 #undef USC_CASE
421 }
422
423 #define PPP_PRINT(map, header_name, struct_name, human) \
424 if (hdr.header_name) { \
425 if (((map + AGX_##struct_name##_LENGTH) > (base + size))) { \
426 fprintf(agxdecode_dump_stream, "Buffer overrun in PPP update\n"); \
427 return; \
428 } \
429 DUMP_CL(struct_name, map, human); \
430 map += AGX_##struct_name##_LENGTH; \
431 fflush(agxdecode_dump_stream); \
432 }
433
434 static void
435 agxdecode_record(struct agxdecode_ctx *ctx, uint64_t va, size_t size,
436 bool verbose, decoder_params *params)
437 {
438 uint8_t buf[size];
439 uint8_t *base = buf;
440 uint8_t *map = base;
441
442 agxdecode_fetch_gpu_array(ctx, va, buf);
443
444 agx_unpack(agxdecode_dump_stream, map, PPP_HEADER, hdr);
445 map += AGX_PPP_HEADER_LENGTH;
446
447 PPP_PRINT(map, fragment_control, FRAGMENT_CONTROL, "Fragment control");
448 PPP_PRINT(map, fragment_control_2, FRAGMENT_CONTROL, "Fragment control 2");
449 PPP_PRINT(map, fragment_front_face, FRAGMENT_FACE, "Front face");
450 PPP_PRINT(map, fragment_front_face_2, FRAGMENT_FACE_2, "Front face 2");
451 PPP_PRINT(map, fragment_front_stencil, FRAGMENT_STENCIL, "Front stencil");
452 PPP_PRINT(map, fragment_back_face, FRAGMENT_FACE, "Back face");
453 PPP_PRINT(map, fragment_back_face_2, FRAGMENT_FACE_2, "Back face 2");
454 PPP_PRINT(map, fragment_back_stencil, FRAGMENT_STENCIL, "Back stencil");
455 PPP_PRINT(map, depth_bias_scissor, DEPTH_BIAS_SCISSOR, "Depth bias/scissor");
456
457 if (hdr.region_clip) {
458 if (((map + (AGX_REGION_CLIP_LENGTH * hdr.viewport_count)) >
459 (base + size))) {
460 fprintf(agxdecode_dump_stream, "Buffer overrun in PPP update\n");
461 return;
462 }
463
464 for (unsigned i = 0; i < hdr.viewport_count; ++i) {
465 DUMP_CL(REGION_CLIP, map, "Region clip");
466 map += AGX_REGION_CLIP_LENGTH;
467 fflush(agxdecode_dump_stream);
468 }
469 }
470
471 if (hdr.viewport) {
472 if (((map + AGX_VIEWPORT_CONTROL_LENGTH +
473 (AGX_VIEWPORT_LENGTH * hdr.viewport_count)) > (base + size))) {
474 fprintf(agxdecode_dump_stream, "Buffer overrun in PPP update\n");
475 return;
476 }
477
478 DUMP_CL(VIEWPORT_CONTROL, map, "Viewport control");
479 map += AGX_VIEWPORT_CONTROL_LENGTH;
480
481 for (unsigned i = 0; i < hdr.viewport_count; ++i) {
482 DUMP_CL(VIEWPORT, map, "Viewport");
483 map += AGX_VIEWPORT_LENGTH;
484 fflush(agxdecode_dump_stream);
485 }
486 }
487
488 PPP_PRINT(map, w_clamp, W_CLAMP, "W clamp");
489 PPP_PRINT(map, output_select, OUTPUT_SELECT, "Output select");
490 PPP_PRINT(map, varying_counts_32, VARYING_COUNTS, "Varying counts 32");
491 PPP_PRINT(map, varying_counts_16, VARYING_COUNTS, "Varying counts 16");
492 PPP_PRINT(map, cull, CULL, "Cull");
493 PPP_PRINT(map, cull_2, CULL_2, "Cull 2");
494
495 if (hdr.fragment_shader) {
496 agx_unpack(agxdecode_dump_stream, map, FRAGMENT_SHADER_WORD_0, frag_0);
497 agx_unpack(agxdecode_dump_stream, map + 4, FRAGMENT_SHADER_WORD_1,
498 frag_1);
499 agx_unpack(agxdecode_dump_stream, map + 8, FRAGMENT_SHADER_WORD_2,
500 frag_2);
501 agxdecode_stateful(ctx, decode_usc(ctx, frag_1.pipeline),
502 "Fragment pipeline", agxdecode_usc, verbose, params,
503 &frag_0.sampler_state_register_count);
504
505 if (frag_2.cf_bindings) {
506 uint8_t buf[128];
507 uint8_t *cf = buf;
508
509 agxdecode_fetch_gpu_array(ctx, decode_usc(ctx, frag_2.cf_bindings),
510 buf);
511 u_hexdump(agxdecode_dump_stream, cf, 128, false);
512
513 DUMP_CL(CF_BINDING_HEADER, cf, "Coefficient binding header:");
514 cf += AGX_CF_BINDING_HEADER_LENGTH;
515
516 for (unsigned i = 0; i < frag_0.cf_binding_count; ++i) {
517 DUMP_CL(CF_BINDING, cf, "Coefficient binding:");
518 cf += AGX_CF_BINDING_LENGTH;
519 }
520 }
521
522 DUMP_CL(FRAGMENT_SHADER_WORD_0, map, "Fragment shader word 0");
523 DUMP_CL(FRAGMENT_SHADER_WORD_1, map + 4, "Fragment shader word 1");
524 DUMP_CL(FRAGMENT_SHADER_WORD_2, map + 8, "Fragment shader word 2");
525 DUMP_CL(FRAGMENT_SHADER_WORD_3, map + 12, "Fragment shader word 3");
526 map += 16;
527 }
528
529 PPP_PRINT(map, occlusion_query, FRAGMENT_OCCLUSION_QUERY, "Occlusion query");
530 PPP_PRINT(map, occlusion_query_2, FRAGMENT_OCCLUSION_QUERY_2,
531 "Occlusion query 2");
532 PPP_PRINT(map, output_unknown, OUTPUT_UNKNOWN, "Output unknown");
533 PPP_PRINT(map, output_size, OUTPUT_SIZE, "Output size");
534 PPP_PRINT(map, varying_word_2, VARYING_2, "Varying word 2");
535
536 /* PPP print checks we don't read too much, now check we read enough */
537 assert(map == (base + size) && "invalid size of PPP update");
538 }
539
540 static unsigned
541 agxdecode_cdm(struct agxdecode_ctx *ctx, const uint8_t *map, uint64_t *link,
542 bool verbose, decoder_params *params, UNUSED void *data)
543 {
544 /* Bits 29-31 contain the block type */
545 enum agx_cdm_block_type block_type = (map[3] >> 5);
546
547 switch (block_type) {
548 case AGX_CDM_BLOCK_TYPE_LAUNCH: {
549 size_t length =
550 AGX_CDM_LAUNCH_WORD_0_LENGTH + AGX_CDM_LAUNCH_WORD_1_LENGTH;
551
552 #define CDM_PRINT(STRUCT_NAME, human) \
553 do { \
554 DUMP_CL(CDM_##STRUCT_NAME, map, human); \
555 map += AGX_CDM_##STRUCT_NAME##_LENGTH; \
556 length += AGX_CDM_##STRUCT_NAME##_LENGTH; \
557 } while (0);
558
559 agx_unpack(agxdecode_dump_stream, map + 0, CDM_LAUNCH_WORD_0, hdr0);
560 agx_unpack(agxdecode_dump_stream, map + 4, CDM_LAUNCH_WORD_1, hdr1);
561
562 agxdecode_stateful(ctx, decode_usc(ctx, hdr1.pipeline), "Pipeline",
563 agxdecode_usc, verbose, params,
564 &hdr0.sampler_state_register_count);
565 DUMP_UNPACKED(CDM_LAUNCH_WORD_0, hdr0, "Compute\n");
566 DUMP_UNPACKED(CDM_LAUNCH_WORD_1, hdr1, "Compute\n");
567 map += 8;
568
569 /* Added in G14X */
570 if (params->gpu_generation >= 14 && params->num_clusters_total > 1)
571 CDM_PRINT(UNK_G14X, "Unknown G14X");
572
573 switch (hdr0.mode) {
574 case AGX_CDM_MODE_DIRECT:
575 CDM_PRINT(GLOBAL_SIZE, "Global size");
576 CDM_PRINT(LOCAL_SIZE, "Local size");
577 break;
578 case AGX_CDM_MODE_INDIRECT_GLOBAL:
579 CDM_PRINT(INDIRECT, "Indirect buffer");
580 CDM_PRINT(LOCAL_SIZE, "Local size");
581 break;
582 case AGX_CDM_MODE_INDIRECT_LOCAL:
583 CDM_PRINT(INDIRECT, "Indirect buffer");
584 break;
585 default:
586 fprintf(agxdecode_dump_stream, "Unknown CDM mode: %u\n", hdr0.mode);
587 break;
588 }
589
590 return length;
591 }
592
593 case AGX_CDM_BLOCK_TYPE_STREAM_LINK: {
594 agx_unpack(agxdecode_dump_stream, map, CDM_STREAM_LINK, hdr);
595 DUMP_UNPACKED(CDM_STREAM_LINK, hdr, "Stream Link\n");
596 *link = hdr.target_lo | (((uint64_t)hdr.target_hi) << 32);
597 return hdr.with_return ? STATE_CALL : STATE_LINK;
598 }
599
600 case AGX_CDM_BLOCK_TYPE_STREAM_TERMINATE: {
601 DUMP_CL(CDM_STREAM_TERMINATE, map, "Stream Terminate");
602 return STATE_DONE;
603 }
604
605 case AGX_CDM_BLOCK_TYPE_STREAM_RETURN: {
606 DUMP_CL(CDM_STREAM_RETURN, map, "Stream Return");
607 return STATE_RET;
608 }
609
610 case AGX_CDM_BLOCK_TYPE_BARRIER: {
611 DUMP_CL(CDM_BARRIER, map, "Barrier");
612 return AGX_CDM_BARRIER_LENGTH;
613 }
614
615 default:
616 fprintf(agxdecode_dump_stream, "Unknown CDM block type: %u\n",
617 block_type);
618 u_hexdump(agxdecode_dump_stream, map, 8, false);
619 return 8;
620 }
621 }
622
623 static unsigned
624 agxdecode_vdm(struct agxdecode_ctx *ctx, const uint8_t *map, uint64_t *link,
625 bool verbose, decoder_params *params, UNUSED void *data)
626 {
627 /* Bits 29-31 contain the block type */
628 enum agx_vdm_block_type block_type = (map[3] >> 5);
629
630 switch (block_type) {
631 case AGX_VDM_BLOCK_TYPE_BARRIER: {
632 agx_unpack(agxdecode_dump_stream, map, VDM_BARRIER, hdr);
633 DUMP_UNPACKED(VDM_BARRIER, hdr, "Barrier\n");
634 return hdr.returns ? STATE_RET : AGX_VDM_BARRIER_LENGTH;
635 }
636
637 case AGX_VDM_BLOCK_TYPE_PPP_STATE_UPDATE: {
638 agx_unpack(agxdecode_dump_stream, map, PPP_STATE, cmd);
639
640 uint64_t address = (((uint64_t)cmd.pointer_hi) << 32) | cmd.pointer_lo;
641
642 if (!lib_config.read_gpu_mem) {
643 struct agx_bo *mem =
644 agxdecode_find_mapped_gpu_mem_containing(ctx, address);
645
646 if (!mem) {
647 DUMP_UNPACKED(PPP_STATE, cmd, "Non-existent record (XXX)\n");
648 return AGX_PPP_STATE_LENGTH;
649 }
650 }
651
652 agxdecode_record(ctx, address, cmd.size_words * 4, verbose, params);
653 return AGX_PPP_STATE_LENGTH;
654 }
655
656 case AGX_VDM_BLOCK_TYPE_VDM_STATE_UPDATE: {
657 size_t length = AGX_VDM_STATE_LENGTH;
658 agx_unpack(agxdecode_dump_stream, map, VDM_STATE, hdr);
659 map += AGX_VDM_STATE_LENGTH;
660
661 #define VDM_PRINT(header_name, STRUCT_NAME, human) \
662 if (hdr.header_name##_present) { \
663 DUMP_CL(VDM_STATE_##STRUCT_NAME, map, human); \
664 map += AGX_VDM_STATE_##STRUCT_NAME##_LENGTH; \
665 length += AGX_VDM_STATE_##STRUCT_NAME##_LENGTH; \
666 }
667
668 VDM_PRINT(restart_index, RESTART_INDEX, "Restart index");
669
670 /* If word 1 is present but word 0 is not, fallback to compact samplers */
671 enum agx_sampler_states sampler_states = 0;
672
673 if (hdr.vertex_shader_word_0_present) {
674 agx_unpack(agxdecode_dump_stream, map, VDM_STATE_VERTEX_SHADER_WORD_0,
675 word_0);
676 sampler_states = word_0.sampler_state_register_count;
677 }
678
679 VDM_PRINT(vertex_shader_word_0, VERTEX_SHADER_WORD_0,
680 "Vertex shader word 0");
681
682 if (hdr.vertex_shader_word_1_present) {
683 agx_unpack(agxdecode_dump_stream, map, VDM_STATE_VERTEX_SHADER_WORD_1,
684 word_1);
685 fprintf(agxdecode_dump_stream, "Pipeline %X\n",
686 (uint32_t)word_1.pipeline);
687 agxdecode_stateful(ctx, decode_usc(ctx, word_1.pipeline), "Pipeline",
688 agxdecode_usc, verbose, params, &sampler_states);
689 }
690
691 VDM_PRINT(vertex_shader_word_1, VERTEX_SHADER_WORD_1,
692 "Vertex shader word 1");
693 VDM_PRINT(vertex_outputs, VERTEX_OUTPUTS, "Vertex outputs");
694 VDM_PRINT(tessellation, TESSELLATION, "Tessellation");
695 VDM_PRINT(vertex_unknown, VERTEX_UNKNOWN, "Vertex unknown");
696 VDM_PRINT(tessellation_scale, TESSELLATION_SCALE, "Tessellation scale");
697
698 #undef VDM_PRINT
699 return hdr.tessellation_scale_present ? length : ALIGN_POT(length, 8);
700 }
701
702 case AGX_VDM_BLOCK_TYPE_INDEX_LIST: {
703 size_t length = AGX_INDEX_LIST_LENGTH;
704 agx_unpack(agxdecode_dump_stream, map, INDEX_LIST, hdr);
705 DUMP_UNPACKED(INDEX_LIST, hdr, "Index List\n");
706 map += AGX_INDEX_LIST_LENGTH;
707
708 #define IDX_PRINT(header_name, STRUCT_NAME, human) \
709 if (hdr.header_name##_present) { \
710 DUMP_CL(INDEX_LIST_##STRUCT_NAME, map, human); \
711 map += AGX_INDEX_LIST_##STRUCT_NAME##_LENGTH; \
712 length += AGX_INDEX_LIST_##STRUCT_NAME##_LENGTH; \
713 }
714
715 IDX_PRINT(index_buffer, BUFFER_LO, "Index buffer");
716 IDX_PRINT(index_count, COUNT, "Index count");
717 IDX_PRINT(instance_count, INSTANCES, "Instance count");
718 IDX_PRINT(start, START, "Start");
719 IDX_PRINT(indirect_buffer, INDIRECT_BUFFER, "Indirect buffer");
720 IDX_PRINT(index_buffer_size, BUFFER_SIZE, "Index buffer size");
721
722 #undef IDX_PRINT
723 return length;
724 }
725
726 case AGX_VDM_BLOCK_TYPE_STREAM_LINK: {
727 agx_unpack(agxdecode_dump_stream, map, VDM_STREAM_LINK, hdr);
728 DUMP_UNPACKED(VDM_STREAM_LINK, hdr, "Stream Link\n");
729 *link = hdr.target_lo | (((uint64_t)hdr.target_hi) << 32);
730 return hdr.with_return ? STATE_CALL : STATE_LINK;
731 }
732
733 case AGX_VDM_BLOCK_TYPE_STREAM_TERMINATE: {
734 DUMP_CL(VDM_STREAM_TERMINATE, map, "Stream Terminate");
735 return STATE_DONE;
736 }
737
738 case AGX_VDM_BLOCK_TYPE_TESSELLATE: {
739 size_t length = AGX_VDM_TESSELLATE_LENGTH;
740 agx_unpack(agxdecode_dump_stream, map, VDM_TESSELLATE, hdr);
741 DUMP_UNPACKED(VDM_TESSELLATE, hdr, "Tessellate List\n");
742 map += AGX_VDM_TESSELLATE_LENGTH;
743
744 #define TESS_PRINT(header_name, STRUCT_NAME, human) \
745 if (hdr.header_name##_present) { \
746 DUMP_CL(VDM_TESSELLATE_##STRUCT_NAME, map, human); \
747 map += AGX_VDM_TESSELLATE_##STRUCT_NAME##_LENGTH; \
748 length += AGX_VDM_TESSELLATE_##STRUCT_NAME##_LENGTH; \
749 }
750
751 TESS_PRINT(factor_buffer, FACTOR_BUFFER, "Factor buffer");
752 TESS_PRINT(patch_count, PATCH_COUNT, "Patch");
753 TESS_PRINT(instance_count, INSTANCE_COUNT, "Instance count");
754 TESS_PRINT(base_patch, BASE_PATCH, "Base patch");
755 TESS_PRINT(base_instance, BASE_INSTANCE, "Base instance");
756 TESS_PRINT(instance_stride, INSTANCE_STRIDE, "Instance stride");
757 TESS_PRINT(indirect, INDIRECT, "Indirect");
758 TESS_PRINT(factor_buffer_size, FACTOR_BUFFER_SIZE, "Factor buffer size");
759
760 #undef TESS_PRINT
761 return length;
762 }
763
764 default:
765 fprintf(agxdecode_dump_stream, "Unknown VDM block type: %u\n",
766 block_type);
767 u_hexdump(agxdecode_dump_stream, map, 8, false);
768 return 8;
769 }
770 }
771
772 static void
773 agxdecode_sampler_heap(struct agxdecode_ctx *ctx, uint64_t heap, unsigned count)
774 {
775 if (!heap)
776 return;
777
778 struct agx_sampler_packed samp[1024];
779 agxdecode_fetch_gpu_array(ctx, heap, samp);
780
781 for (unsigned i = 0; i < count; ++i) {
782 bool nonzero = false;
783 for (unsigned j = 0; j < ARRAY_SIZE(samp[i].opaque); ++j) {
784 nonzero |= samp[i].opaque[j] != 0;
785 }
786
787 if (nonzero) {
788 fprintf(agxdecode_dump_stream, "Heap sampler %u\n", i);
789
790 agx_unpack(agxdecode_dump_stream, samp + i, SAMPLER, temp);
791 agx_print(agxdecode_dump_stream, SAMPLER, temp, 2);
792 }
793 }
794 }
795
796 static void
797 agxdecode_helper(struct agxdecode_ctx *ctx, const char *prefix, uint64_t helper)
798 {
799 if (helper & 1) {
800 fprintf(agxdecode_dump_stream, "%s helper program:\n", prefix);
801 uint8_t buf[1024];
802 agx_disassemble(
803 buf, agxdecode_fetch_gpu_array(ctx, decode_usc(ctx, helper & ~1), buf),
804 agxdecode_dump_stream);
805 }
806 }
807
808 static void
809 agxdecode_drm_cmd_render(struct agxdecode_ctx *ctx,
810 struct drm_asahi_params_global *params,
811 struct drm_asahi_cmd_render *c, bool verbose)
812 {
813 DUMP_FIELD(c, "%x", flags);
814 DUMP_FIELD(c, "0x%llx", vdm_ctrl_stream_base);
815 agxdecode_stateful(ctx, c->vdm_ctrl_stream_base, "Encoder", agxdecode_vdm,
816 verbose, params, NULL);
817 DUMP_FIELD(c, "0x%x", ppp_ctrl);
818 DUMP_FIELD(c, "0x%llx", ppp_multisamplectl);
819 DUMP_CL(ZLS_CONTROL, &c->zls_ctrl, "ZLS Control");
820 DUMP_FIELD(c, "0x%llx", depth.base);
821 DUMP_FIELD(c, "0x%llx", depth.comp_base);
822 DUMP_FIELD(c, "%u", depth.stride);
823 DUMP_FIELD(c, "%u", depth.comp_stride);
824 DUMP_FIELD(c, "0x%llx", stencil.base);
825 DUMP_FIELD(c, "0x%llx", stencil.comp_base);
826 DUMP_FIELD(c, "%u", stencil.stride);
827 DUMP_FIELD(c, "%u", stencil.comp_stride);
828 DUMP_FIELD(c, "0x%llx", isp_scissor_base);
829 DUMP_FIELD(c, "0x%llx", isp_dbias_base);
830 DUMP_FIELD(c, "%d", width_px);
831 DUMP_FIELD(c, "%d", height_px);
832 DUMP_FIELD(c, "%d", layers);
833 DUMP_FIELD(c, "%d", samples);
834 DUMP_FIELD(c, "%d", sample_size_B);
835 DUMP_FIELD(c, "%d", utile_width_px);
836 DUMP_FIELD(c, "%d", utile_height_px);
837 DUMP_FIELD(c, "0x%x", bg.usc);
838 DUMP_FIELD(c, "0x%x", bg.rsrc_spec);
839 agxdecode_stateful(ctx, decode_usc(ctx, c->bg.usc & ~0x7), "Load pipeline",
840 agxdecode_usc, verbose, params, NULL);
841 DUMP_FIELD(c, "0x%x", eot.usc);
842 DUMP_FIELD(c, "0x%x", eot.rsrc_spec);
843 agxdecode_stateful(ctx, decode_usc(ctx, c->eot.usc & ~0x7), "Store pipeline",
844 agxdecode_usc, verbose, params, NULL);
845 DUMP_FIELD(c, "0x%x", partial_bg.usc);
846 DUMP_FIELD(c, "0x%x", partial_bg.rsrc_spec);
847 agxdecode_stateful(ctx, decode_usc(ctx, c->partial_bg.usc & ~0x7),
848 "Partial reload pipeline", agxdecode_usc, verbose, params,
849 NULL);
850 DUMP_FIELD(c, "0x%x", partial_eot.usc);
851 DUMP_FIELD(c, "0x%x", partial_eot.rsrc_spec);
852 agxdecode_stateful(ctx, decode_usc(ctx, c->partial_eot.usc & ~0x7),
853 "Partial store pipeline", agxdecode_usc, verbose, params,
854 NULL);
855
856 DUMP_FIELD(c, "0x%x", isp_zls_pixels);
857 DUMP_FIELD(c, "0x%x", isp_bgobjdepth);
858 DUMP_FIELD(c, "0x%x", isp_bgobjvals);
859
860 agxdecode_sampler_heap(ctx, c->sampler_heap, c->sampler_count);
861
862 agxdecode_helper(ctx, "Vertex", c->vertex_helper.binary);
863 agxdecode_helper(ctx, "Fragment", c->fragment_helper.binary);
864 }
865
866 static void
867 agxdecode_drm_cmd_compute(struct agxdecode_ctx *ctx,
868 struct drm_asahi_params_global *params,
869 struct drm_asahi_cmd_compute *c, bool verbose)
870 {
871 DUMP_FIELD(c, "%x", flags);
872 DUMP_FIELD(c, "0x%llx", cdm_ctrl_stream_base);
873 agxdecode_stateful(ctx, c->cdm_ctrl_stream_base, "Encoder", agxdecode_cdm,
874 verbose, params, NULL);
875
876 agxdecode_sampler_heap(ctx, c->sampler_heap, c->sampler_count);
877 agxdecode_helper(ctx, "Compute", c->helper.binary);
878 }
879
880 static void
881 agxdecode_drm_attachments(const char *name, struct drm_asahi_attachment *atts,
882 size_t size)
883 {
884 fprintf(agxdecode_dump_stream, "%s attachments:\n", name);
885 unsigned count = size / sizeof(struct drm_asahi_attachment);
886 for (unsigned i = 0; i < count; i++) {
887 DUMP_FIELD((&atts[i]), "0x%llx", size);
888 DUMP_FIELD((&atts[i]), "0x%llx", pointer);
889 }
890 }
891
892 void
893 agxdecode_drm_cmdbuf(struct agxdecode_ctx *ctx,
894 struct drm_asahi_params_global *params,
895 struct util_dynarray *cmdbuf, bool verbose)
896 {
897 agxdecode_dump_file_open();
898
899 for (unsigned offs = 0; offs < cmdbuf->size;) {
900 struct drm_asahi_cmd_header *header =
901 (void *)((uint8_t *)cmdbuf->data) + offs;
902 offs += sizeof(*header);
903 void *data = (void *)((uint8_t *)cmdbuf->data) + offs;
904
905 if (header->cmd_type == DRM_ASAHI_CMD_RENDER) {
906 agxdecode_drm_cmd_render(ctx, params, data, verbose);
907 } else if (header->cmd_type == DRM_ASAHI_CMD_COMPUTE) {
908 agxdecode_drm_cmd_compute(ctx, params, data, verbose);
909 } else if (header->cmd_type == DRM_ASAHI_SET_VERTEX_ATTACHMENTS) {
910 agxdecode_drm_attachments("Vertex", data, header->size);
911 } else if (header->cmd_type == DRM_ASAHI_SET_FRAGMENT_ATTACHMENTS) {
912 agxdecode_drm_attachments("Fragment", data, header->size);
913 } else if (header->cmd_type == DRM_ASAHI_SET_COMPUTE_ATTACHMENTS) {
914 agxdecode_drm_attachments("Compute", data, header->size);
915 } else {
916 UNREACHABLE("Invalid command type");
917 }
918
919 offs += header->size;
920 }
921 }
922
923 static void
924 chip_id_to_params(decoder_params *params, uint32_t chip_id)
925 {
926 switch (chip_id) {
927 case 0x6000 ... 0x6002:
928 *params = (decoder_params){
929 .gpu_generation = 13,
930 .gpu_variant = "SCD"[chip_id & 15],
931 .chip_id = chip_id,
932 .num_clusters_total = 2 << (chip_id & 15),
933 };
934 break;
935 case 0x6020 ... 0x6022:
936 *params = (decoder_params){
937 .gpu_generation = 14,
938 .gpu_variant = "SCD"[chip_id & 15],
939 .chip_id = chip_id,
940 .num_clusters_total = 2 << (chip_id & 15),
941 };
942 break;
943 case 0x8112:
944 *params = (decoder_params){
945 .gpu_generation = 14,
946 .gpu_variant = 'G',
947 .chip_id = chip_id,
948 .num_clusters_total = 1,
949 };
950 break;
951 case 0x8103:
952 default:
953 *params = (decoder_params){
954 .gpu_generation = 13,
955 .gpu_variant = 'G',
956 .chip_id = chip_id,
957 .num_clusters_total = 1,
958 };
959 break;
960 }
961 }
962
963 void
964 agxdecode_cmdstream(struct agxdecode_ctx *ctx, unsigned cmdbuf_handle,
965 unsigned map_handle, bool verbose)
966 {
967 agxdecode_dump_file_open();
968
969 struct agx_bo *cmdbuf = agxdecode_find_handle(ctx, cmdbuf_handle);
970 assert(cmdbuf != NULL && "nonexistent command buffer");
971
972 struct drm_asahi_params_global params;
973
974 chip_id_to_params(&params, 0x8103);
975
976 uint32_t *map = cmdbuf->_map;
977 uint64_t encoder = (uint64_t)map[14] | ((uint64_t)(map[15]) << 32);
978
979 if (map[13] == 3) {
980 agxdecode_stateful(ctx, encoder, "Encoder", agxdecode_cdm, verbose,
981 &params, NULL);
982 } else {
983 agxdecode_stateful(ctx, encoder, "Encoder", agxdecode_vdm, verbose,
984 &params, NULL);
985 }
986 }
987
988 void
989 agxdecode_track_alloc(struct agxdecode_ctx *ctx, struct agx_bo *alloc)
990 {
991 util_dynarray_foreach(&ctx->mmap_array, struct agx_bo, it) {
992 bool match = (it->handle == alloc->handle);
993 assert(!match && "tried to alloc already allocated BO");
994 }
995
996 util_dynarray_append(&ctx->mmap_array, *alloc);
997 }
998
999 void
1000 agxdecode_track_free(struct agxdecode_ctx *ctx, struct agx_bo *bo)
1001 {
1002 bool found = false;
1003
1004 util_dynarray_foreach(&ctx->mmap_array, struct agx_bo, it) {
1005 if (it->handle == bo->handle) {
1006 assert(!found && "mapped multiple times!");
1007 found = true;
1008
1009 memset(it, 0, sizeof(*it));
1010 }
1011 }
1012
1013 assert(found && "freed unmapped memory");
1014 }
1015
1016 static int agxdecode_dump_frame_count = 0;
1017
1018 void
1019 agxdecode_dump_file_open(void)
1020 {
1021 if (agxdecode_dump_stream)
1022 return;
1023
1024 /* This does a os_get_option every frame, so it is possible to use
1025 * os_set_option to change the base at runtime.
1026 */
1027 const char *dump_file_base =
1028 os_get_option("AGXDECODE_DUMP_FILE") ?: "agxdecode.dump";
1029 if (!strcmp(dump_file_base, "stderr"))
1030 agxdecode_dump_stream = stderr;
1031 else {
1032 char buffer[1024];
1033 snprintf(buffer, sizeof(buffer), "%s.%04d", dump_file_base,
1034 agxdecode_dump_frame_count);
1035 printf("agxdecode: dump command stream to file %s\n", buffer);
1036 agxdecode_dump_stream = fopen(buffer, "w");
1037 if (!agxdecode_dump_stream) {
1038 fprintf(stderr,
1039 "agxdecode: failed to open command stream log file %s\n",
1040 buffer);
1041 }
1042 }
1043 }
1044
1045 static void
1046 agxdecode_dump_file_close(void)
1047 {
1048 if (agxdecode_dump_stream && agxdecode_dump_stream != stderr) {
1049 fclose(agxdecode_dump_stream);
1050 agxdecode_dump_stream = NULL;
1051 }
1052 }
1053
1054 void
1055 agxdecode_next_frame(void)
1056 {
1057 agxdecode_dump_file_close();
1058 agxdecode_dump_frame_count++;
1059 }
1060
1061 void
1062 agxdecode_close(void)
1063 {
1064 agxdecode_dump_file_close();
1065 }
1066
1067 static ssize_t
1068 libagxdecode_writer(void *cookie, const char *buffer, size_t size)
1069 {
1070 return lib_config.stream_write(buffer, size);
1071 }
1072
1073 #if defined(_GNU_SOURCE) && !DETECT_OS_ANDROID
1074 static cookie_io_functions_t funcs = {.write = libagxdecode_writer};
1075 #endif
1076
1077 static decoder_params lib_params;
1078
1079 void
1080 libagxdecode_init(struct libagxdecode_config *config)
1081 {
1082 #if defined(_GNU_SOURCE) && !DETECT_OS_ANDROID
1083 lib_config = *config;
1084 agxdecode_dump_stream = fopencookie(NULL, "w", funcs);
1085
1086 chip_id_to_params(&lib_params, config->chip_id);
1087 #else
1088 /* fopencookie is a glibc extension */
1089 UNREACHABLE("libagxdecode only available with glibc");
1090 #endif
1091 }
1092
1093 void
1094 libagxdecode_vdm(struct agxdecode_ctx *ctx, uint64_t addr, const char *label,
1095 bool verbose)
1096 {
1097 agxdecode_stateful(ctx, addr, label, agxdecode_vdm, verbose, &lib_params,
1098 NULL);
1099 }
1100
1101 void
1102 libagxdecode_cdm(struct agxdecode_ctx *ctx, uint64_t addr, const char *label,
1103 bool verbose)
1104 {
1105 agxdecode_stateful(ctx, addr, label, agxdecode_cdm, verbose, &lib_params,
1106 NULL);
1107 }
1108 void
1109 libagxdecode_usc(struct agxdecode_ctx *ctx, uint64_t addr, const char *label,
1110 bool verbose)
1111 {
1112 agxdecode_stateful(ctx, addr, label, agxdecode_usc, verbose, &lib_params,
1113 NULL);
1114 }
1115 void
1116 libagxdecode_shutdown(void)
1117 {
1118 agxdecode_dump_file_close();
1119 }