panfrost: XMLify wrap modes
[mesa.git] / src / panfrost / lib / decode.c
1 /*
2 * Copyright (C) 2017-2019 Alyssa Rosenzweig
3 * Copyright (C) 2017-2019 Connor Abbott
4 * Copyright (C) 2019 Collabora, Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <midgard_pack.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <memory.h>
30 #include <stdbool.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33 #include "decode.h"
34 #include "util/macros.h"
35 #include "util/u_math.h"
36
37 #include "midgard/disassemble.h"
38 #include "bifrost/disassemble.h"
39
40 #include "pan_encoder.h"
41
42 static void pandecode_swizzle(unsigned swizzle, enum mali_format format);
43
44 #define MEMORY_PROP(obj, p) {\
45 if (obj->p) { \
46 char *a = pointer_as_memory_reference(obj->p); \
47 pandecode_prop("%s = %s", #p, a); \
48 free(a); \
49 } \
50 }
51
52 #define MEMORY_PROP_DIR(obj, p) {\
53 if (obj.p) { \
54 char *a = pointer_as_memory_reference(obj.p); \
55 pandecode_prop("%s = %s", #p, a); \
56 free(a); \
57 } \
58 }
59
60 #define DUMP_CL(title, T, cl, indent) {\
61 fprintf(pandecode_dump_stream, "%s\n", title); \
62 struct MALI_ ## T temp; \
63 MALI_ ## T ## _unpack((const uint8_t *) cl, &temp); \
64 MALI_ ## T ## _print(pandecode_dump_stream, &temp, 0); \
65 }
66
67 #define DUMP_ADDR(title, T, addr, indent) {\
68 struct pandecode_mapped_memory *mapped_mem = pandecode_find_mapped_gpu_mem_containing(addr); \
69 const uint8_t *cl = pandecode_fetch_gpu_mem(mapped_mem, addr, MALI_ ## T ## _LENGTH); \
70 DUMP_CL(title, T, cl, indent); \
71 }
72
73 FILE *pandecode_dump_stream;
74
75 /* Semantic logging type.
76 *
77 * Raw: for raw messages to be printed as is.
78 * Message: for helpful information to be commented out in replays.
79 * Property: for properties of a struct
80 *
81 * Use one of pandecode_log, pandecode_msg, or pandecode_prop as syntax sugar.
82 */
83
84 enum pandecode_log_type {
85 PANDECODE_RAW,
86 PANDECODE_MESSAGE,
87 PANDECODE_PROPERTY
88 };
89
90 #define pandecode_log(...) pandecode_log_typed(PANDECODE_RAW, __VA_ARGS__)
91 #define pandecode_msg(...) pandecode_log_typed(PANDECODE_MESSAGE, __VA_ARGS__)
92 #define pandecode_prop(...) pandecode_log_typed(PANDECODE_PROPERTY, __VA_ARGS__)
93
94 unsigned pandecode_indent = 0;
95
96 static void
97 pandecode_make_indent(void)
98 {
99 for (unsigned i = 0; i < pandecode_indent; ++i)
100 fprintf(pandecode_dump_stream, " ");
101 }
102
103 static void
104 pandecode_log_typed(enum pandecode_log_type type, const char *format, ...)
105 {
106 va_list ap;
107
108 pandecode_make_indent();
109
110 if (type == PANDECODE_MESSAGE)
111 fprintf(pandecode_dump_stream, "// ");
112 else if (type == PANDECODE_PROPERTY)
113 fprintf(pandecode_dump_stream, ".");
114
115 va_start(ap, format);
116 vfprintf(pandecode_dump_stream, format, ap);
117 va_end(ap);
118
119 if (type == PANDECODE_PROPERTY)
120 fprintf(pandecode_dump_stream, ",\n");
121 }
122
123 static void
124 pandecode_log_cont(const char *format, ...)
125 {
126 va_list ap;
127
128 va_start(ap, format);
129 vfprintf(pandecode_dump_stream, format, ap);
130 va_end(ap);
131 }
132
133 /* To check for memory safety issues, validates that the given pointer in GPU
134 * memory is valid, containing at least sz bytes. The goal is to eliminate
135 * GPU-side memory bugs (NULL pointer dereferences, buffer overflows, or buffer
136 * overruns) by statically validating pointers.
137 */
138
139 static void
140 pandecode_validate_buffer(mali_ptr addr, size_t sz)
141 {
142 if (!addr) {
143 pandecode_msg("XXX: null pointer deref");
144 return;
145 }
146
147 /* Find a BO */
148
149 struct pandecode_mapped_memory *bo =
150 pandecode_find_mapped_gpu_mem_containing(addr);
151
152 if (!bo) {
153 pandecode_msg("XXX: invalid memory dereference\n");
154 return;
155 }
156
157 /* Bounds check */
158
159 unsigned offset = addr - bo->gpu_va;
160 unsigned total = offset + sz;
161
162 if (total > bo->length) {
163 pandecode_msg("XXX: buffer overrun. "
164 "Chunk of size %zu at offset %d in buffer of size %zu. "
165 "Overrun by %zu bytes. \n",
166 sz, offset, bo->length, total - bo->length);
167 return;
168 }
169 }
170
171 struct pandecode_flag_info {
172 u64 flag;
173 const char *name;
174 };
175
176 static void
177 pandecode_log_decoded_flags(const struct pandecode_flag_info *flag_info,
178 u64 flags)
179 {
180 bool decodable_flags_found = false;
181
182 for (int i = 0; flag_info[i].name; i++) {
183 if ((flags & flag_info[i].flag) != flag_info[i].flag)
184 continue;
185
186 if (!decodable_flags_found) {
187 decodable_flags_found = true;
188 } else {
189 pandecode_log_cont(" | ");
190 }
191
192 pandecode_log_cont("%s", flag_info[i].name);
193
194 flags &= ~flag_info[i].flag;
195 }
196
197 if (decodable_flags_found) {
198 if (flags)
199 pandecode_log_cont(" | 0x%" PRIx64, flags);
200 } else {
201 pandecode_log_cont("0x%" PRIx64, flags);
202 }
203 }
204
205 #define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
206 static const struct pandecode_flag_info gl_enable_flag_info[] = {
207 FLAG_INFO(OCCLUSION_QUERY),
208 FLAG_INFO(OCCLUSION_PRECISE),
209 FLAG_INFO(FRONT_CCW_TOP),
210 FLAG_INFO(CULL_FACE_FRONT),
211 FLAG_INFO(CULL_FACE_BACK),
212 {}
213 };
214 #undef FLAG_INFO
215
216 #define FLAG_INFO(flag) { MALI_CLEAR_##flag, "MALI_CLEAR_" #flag }
217 static const struct pandecode_flag_info clear_flag_info[] = {
218 FLAG_INFO(FAST),
219 FLAG_INFO(SLOW),
220 FLAG_INFO(SLOW_STENCIL),
221 {}
222 };
223 #undef FLAG_INFO
224
225 #define FLAG_INFO(flag) { MALI_MASK_##flag, "MALI_MASK_" #flag }
226 static const struct pandecode_flag_info mask_flag_info[] = {
227 FLAG_INFO(R),
228 FLAG_INFO(G),
229 FLAG_INFO(B),
230 FLAG_INFO(A),
231 {}
232 };
233 #undef FLAG_INFO
234
235 #define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
236 static const struct pandecode_flag_info u3_flag_info[] = {
237 FLAG_INFO(HAS_MSAA),
238 FLAG_INFO(PER_SAMPLE),
239 FLAG_INFO(CAN_DISCARD),
240 FLAG_INFO(HAS_BLEND_SHADER),
241 FLAG_INFO(DEPTH_WRITEMASK),
242 FLAG_INFO(DEPTH_CLIP_NEAR),
243 FLAG_INFO(DEPTH_CLIP_FAR),
244 {}
245 };
246
247 static const struct pandecode_flag_info u4_flag_info[] = {
248 FLAG_INFO(NO_MSAA),
249 FLAG_INFO(NO_DITHER),
250 FLAG_INFO(DEPTH_RANGE_A),
251 FLAG_INFO(DEPTH_RANGE_B),
252 FLAG_INFO(STENCIL_TEST),
253 FLAG_INFO(ALPHA_TO_COVERAGE),
254 {}
255 };
256 #undef FLAG_INFO
257
258 #define FLAG_INFO(flag) { MALI_MFBD_FORMAT_##flag, "MALI_MFBD_FORMAT_" #flag }
259 static const struct pandecode_flag_info mfbd_fmt_flag_info[] = {
260 FLAG_INFO(SRGB),
261 {}
262 };
263 #undef FLAG_INFO
264
265 #define FLAG_INFO(flag) { MALI_AFBC_##flag, "MALI_AFBC_" #flag }
266 static const struct pandecode_flag_info afbc_fmt_flag_info[] = {
267 FLAG_INFO(YTR),
268 {}
269 };
270 #undef FLAG_INFO
271
272 #define FLAG_INFO(flag) { MALI_EXTRA_##flag, "MALI_EXTRA_" #flag }
273 static const struct pandecode_flag_info mfbd_extra_flag_hi_info[] = {
274 FLAG_INFO(PRESENT),
275 {}
276 };
277 #undef FLAG_INFO
278
279 #define FLAG_INFO(flag) { MALI_EXTRA_##flag, "MALI_EXTRA_" #flag }
280 static const struct pandecode_flag_info mfbd_extra_flag_lo_info[] = {
281 FLAG_INFO(ZS),
282 {}
283 };
284 #undef FLAG_INFO
285
286 #define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
287 static const struct pandecode_flag_info shader_midgard1_flag_lo_info [] = {
288 FLAG_INFO(WRITES_Z),
289 FLAG_INFO(EARLY_Z),
290 FLAG_INFO(READS_TILEBUFFER),
291 FLAG_INFO(WRITES_GLOBAL),
292 FLAG_INFO(READS_ZS),
293 {}
294 };
295
296 static const struct pandecode_flag_info shader_midgard1_flag_hi_info [] = {
297 FLAG_INFO(WRITES_S),
298 FLAG_INFO(SUPPRESS_INF_NAN),
299 {}
300 };
301 #undef FLAG_INFO
302
303 #define FLAG_INFO(flag) { MALI_BIFROST_##flag, "MALI_BIFROST_" #flag }
304 static const struct pandecode_flag_info shader_bifrost_info [] = {
305 FLAG_INFO(FULL_THREAD),
306 FLAG_INFO(EARLY_Z),
307 FLAG_INFO(FIRST_ATEST),
308 {}
309 };
310
311 #undef FLAG_INFO
312
313 #define FLAG_INFO(flag) { MALI_MFBD_##flag, "MALI_MFBD_" #flag }
314 static const struct pandecode_flag_info mfbd_flag_info [] = {
315 FLAG_INFO(DEPTH_WRITE),
316 FLAG_INFO(EXTRA),
317 {}
318 };
319 #undef FLAG_INFO
320
321 #define FLAG_INFO(flag) { MALI_SAMP_##flag, "MALI_SAMP_" #flag }
322 static const struct pandecode_flag_info sampler_flag_info [] = {
323 FLAG_INFO(MAG_NEAREST),
324 FLAG_INFO(MIN_NEAREST),
325 FLAG_INFO(MIP_LINEAR_1),
326 FLAG_INFO(MIP_LINEAR_2),
327 FLAG_INFO(NORM_COORDS),
328 {}
329 };
330 #undef FLAG_INFO
331
332 #define FLAG_INFO(flag) { MALI_SFBD_FORMAT_##flag, "MALI_SFBD_FORMAT_" #flag }
333 static const struct pandecode_flag_info sfbd_unk1_info [] = {
334 FLAG_INFO(MSAA_8),
335 FLAG_INFO(MSAA_A),
336 {}
337 };
338 #undef FLAG_INFO
339
340 #define FLAG_INFO(flag) { MALI_SFBD_FORMAT_##flag, "MALI_SFBD_FORMAT_" #flag }
341 static const struct pandecode_flag_info sfbd_unk2_info [] = {
342 FLAG_INFO(MSAA_B),
343 FLAG_INFO(SRGB),
344 {}
345 };
346 #undef FLAG_INFO
347
348 #define DEFINE_CASE(name) case MALI_## name: return "MALI_" #name
349 static char *pandecode_format(enum mali_format format)
350 {
351 static char unk_format_str[10];
352
353 switch (format) {
354 DEFINE_CASE(ETC2_RGB8);
355 DEFINE_CASE(ETC2_R11_UNORM);
356 DEFINE_CASE(ETC2_RGBA8);
357 DEFINE_CASE(ETC2_RG11_UNORM);
358 DEFINE_CASE(ETC2_R11_SNORM);
359 DEFINE_CASE(ETC2_RG11_SNORM);
360 DEFINE_CASE(ETC2_RGB8A1);
361 DEFINE_CASE(NXR);
362 DEFINE_CASE(BC1_UNORM);
363 DEFINE_CASE(BC2_UNORM);
364 DEFINE_CASE(BC3_UNORM);
365 DEFINE_CASE(BC4_UNORM);
366 DEFINE_CASE(BC4_SNORM);
367 DEFINE_CASE(BC5_UNORM);
368 DEFINE_CASE(BC5_SNORM);
369 DEFINE_CASE(BC6H_UF16);
370 DEFINE_CASE(BC6H_SF16);
371 DEFINE_CASE(BC7_UNORM);
372 DEFINE_CASE(ASTC_3D_LDR);
373 DEFINE_CASE(ASTC_3D_HDR);
374 DEFINE_CASE(ASTC_2D_LDR);
375 DEFINE_CASE(ASTC_2D_HDR);
376 DEFINE_CASE(RGB565);
377 DEFINE_CASE(RGB5_X1_UNORM);
378 DEFINE_CASE(RGB5_A1_UNORM);
379 DEFINE_CASE(RGB10_A2_UNORM);
380 DEFINE_CASE(RGB10_A2_SNORM);
381 DEFINE_CASE(RGB10_A2UI);
382 DEFINE_CASE(RGB10_A2I);
383 DEFINE_CASE(RGB332_UNORM);
384 DEFINE_CASE(RGB233_UNORM);
385 DEFINE_CASE(Z24X8_UNORM);
386 DEFINE_CASE(R32_FIXED);
387 DEFINE_CASE(RG32_FIXED);
388 DEFINE_CASE(RGB32_FIXED);
389 DEFINE_CASE(RGBA32_FIXED);
390 DEFINE_CASE(R11F_G11F_B10F);
391 DEFINE_CASE(R9F_G9F_B9F_E5F);
392 DEFINE_CASE(VARYING_POS);
393 DEFINE_CASE(VARYING_DISCARD);
394
395 DEFINE_CASE(R8_SNORM);
396 DEFINE_CASE(R16_SNORM);
397 DEFINE_CASE(R32_SNORM);
398 DEFINE_CASE(RG8_SNORM);
399 DEFINE_CASE(RG16_SNORM);
400 DEFINE_CASE(RG32_SNORM);
401 DEFINE_CASE(RGB8_SNORM);
402 DEFINE_CASE(RGB16_SNORM);
403 DEFINE_CASE(RGB32_SNORM);
404 DEFINE_CASE(RGBA8_SNORM);
405 DEFINE_CASE(RGBA16_SNORM);
406 DEFINE_CASE(RGBA32_SNORM);
407
408 DEFINE_CASE(R8UI);
409 DEFINE_CASE(R16UI);
410 DEFINE_CASE(R32UI);
411 DEFINE_CASE(RG8UI);
412 DEFINE_CASE(RG16UI);
413 DEFINE_CASE(RG32UI);
414 DEFINE_CASE(RGB8UI);
415 DEFINE_CASE(RGB16UI);
416 DEFINE_CASE(RGB32UI);
417 DEFINE_CASE(RGBA8UI);
418 DEFINE_CASE(RGBA16UI);
419 DEFINE_CASE(RGBA32UI);
420
421 DEFINE_CASE(R8_UNORM);
422 DEFINE_CASE(R16_UNORM);
423 DEFINE_CASE(R32_UNORM);
424 DEFINE_CASE(R32F);
425 DEFINE_CASE(RG8_UNORM);
426 DEFINE_CASE(RG16_UNORM);
427 DEFINE_CASE(RG32_UNORM);
428 DEFINE_CASE(RG32F);
429 DEFINE_CASE(RGB8_UNORM);
430 DEFINE_CASE(RGB16_UNORM);
431 DEFINE_CASE(RGB32_UNORM);
432 DEFINE_CASE(RGB32F);
433 DEFINE_CASE(RGBA4_UNORM);
434 DEFINE_CASE(RGBA8_UNORM);
435 DEFINE_CASE(RGBA16_UNORM);
436 DEFINE_CASE(RGBA32_UNORM);
437 DEFINE_CASE(RGBA32F);
438
439 DEFINE_CASE(R8I);
440 DEFINE_CASE(R16I);
441 DEFINE_CASE(R32I);
442 DEFINE_CASE(RG8I);
443 DEFINE_CASE(R16F);
444 DEFINE_CASE(RG16I);
445 DEFINE_CASE(RG32I);
446 DEFINE_CASE(RG16F);
447 DEFINE_CASE(RGB8I);
448 DEFINE_CASE(RGB16I);
449 DEFINE_CASE(RGB32I);
450 DEFINE_CASE(RGB16F);
451 DEFINE_CASE(RGBA8I);
452 DEFINE_CASE(RGBA16I);
453 DEFINE_CASE(RGBA32I);
454 DEFINE_CASE(RGBA16F);
455
456 DEFINE_CASE(RGBA4);
457 DEFINE_CASE(RGBA8_2);
458 DEFINE_CASE(RGB10_A2_2);
459 default:
460 snprintf(unk_format_str, sizeof(unk_format_str), "MALI_0x%02x", format);
461 return unk_format_str;
462 }
463 }
464
465 #undef DEFINE_CASE
466
467 #define DEFINE_CASE(name) case MALI_MSAA_ ## name: return "MALI_MSAA_" #name
468 static char *
469 pandecode_msaa_mode(enum mali_msaa_mode mode)
470 {
471 switch (mode) {
472 DEFINE_CASE(SINGLE);
473 DEFINE_CASE(AVERAGE);
474 DEFINE_CASE(MULTIPLE);
475 DEFINE_CASE(LAYERED);
476 default:
477 unreachable("Impossible");
478 return "";
479 }
480 }
481 #undef DEFINE_CASE
482
483 static char *pandecode_attr_mode_short(enum mali_attr_mode mode)
484 {
485 switch(mode) {
486 /* TODO: Combine to just "instanced" once this can be done
487 * unambiguously in all known cases */
488 case MALI_ATTR_POT_DIVIDE:
489 return "instanced_pot";
490 case MALI_ATTR_MODULO:
491 return "instanced_mod";
492 case MALI_ATTR_NPOT_DIVIDE:
493 return "instanced_npot";
494 case MALI_ATTR_IMAGE:
495 return "image";
496 default:
497 pandecode_msg("XXX: invalid attribute mode %X\n", mode);
498 return "";
499 }
500 }
501
502 static const char *
503 pandecode_special_record(uint64_t v, bool* attribute)
504 {
505 switch(v) {
506 case MALI_ATTR_VERTEXID:
507 *attribute = true;
508 return "gl_VertexID";
509 case MALI_ATTR_INSTANCEID:
510 *attribute = true;
511 return "gl_InstanceID";
512 case MALI_VARYING_FRAG_COORD:
513 return "gl_FragCoord";
514 case MALI_VARYING_FRONT_FACING:
515 return "gl_FrontFacing";
516 case MALI_VARYING_POINT_COORD:
517 return "gl_PointCoord";
518 default:
519 pandecode_msg("XXX: invalid special record %" PRIx64 "\n", v);
520 return "";
521 }
522 }
523
524 #define DEFINE_CASE(name) case MALI_BLOCK_## name: return "MALI_BLOCK_" #name
525 static char *
526 pandecode_block_format(enum mali_block_format fmt)
527 {
528 switch (fmt) {
529 DEFINE_CASE(TILED);
530 DEFINE_CASE(UNKNOWN);
531 DEFINE_CASE(LINEAR);
532 DEFINE_CASE(AFBC);
533
534 default:
535 unreachable("Invalid case");
536 }
537 }
538 #undef DEFINE_CASE
539
540 #define DEFINE_CASE(name) case MALI_EXCEPTION_ACCESS_## name: return ""#name
541 static char *
542 pandecode_exception_access(unsigned access)
543 {
544 switch (access) {
545 DEFINE_CASE(NONE);
546 DEFINE_CASE(EXECUTE);
547 DEFINE_CASE(READ);
548 DEFINE_CASE(WRITE);
549
550 default:
551 unreachable("Invalid case");
552 }
553 }
554 #undef DEFINE_CASE
555
556 /* Midgard's tiler descriptor is embedded within the
557 * larger FBD */
558
559 static void
560 pandecode_midgard_tiler_descriptor(
561 const struct midgard_tiler_descriptor *t,
562 unsigned width,
563 unsigned height,
564 bool is_fragment,
565 bool has_hierarchy)
566 {
567 pandecode_log(".tiler = {\n");
568 pandecode_indent++;
569
570 if (t->hierarchy_mask == MALI_TILER_DISABLED)
571 pandecode_prop("hierarchy_mask = MALI_TILER_DISABLED");
572 else
573 pandecode_prop("hierarchy_mask = 0x%" PRIx16, t->hierarchy_mask);
574
575 /* We know this name from the kernel, but we never see it nonzero */
576
577 if (t->flags)
578 pandecode_msg("XXX: unexpected tiler flags 0x%" PRIx16, t->flags);
579
580 MEMORY_PROP(t, polygon_list);
581
582 /* The body is offset from the base of the polygon list */
583 //assert(t->polygon_list_body > t->polygon_list);
584 unsigned body_offset = t->polygon_list_body - t->polygon_list;
585
586 /* It needs to fit inside the reported size */
587 //assert(t->polygon_list_size >= body_offset);
588
589 /* Now that we've sanity checked, we'll try to calculate the sizes
590 * ourselves for comparison */
591
592 unsigned ref_header = panfrost_tiler_header_size(width, height, t->hierarchy_mask, has_hierarchy);
593 unsigned ref_size = panfrost_tiler_full_size(width, height, t->hierarchy_mask, has_hierarchy);
594
595 if (!((ref_header == body_offset) && (ref_size == t->polygon_list_size))) {
596 pandecode_msg("XXX: bad polygon list size (expected %d / 0x%x)\n",
597 ref_header, ref_size);
598 pandecode_prop("polygon_list_size = 0x%x", t->polygon_list_size);
599 pandecode_msg("body offset %d\n", body_offset);
600 }
601
602 /* The tiler heap has a start and end specified -- it should be
603 * identical to what we have in the BO. The exception is if tiling is
604 * disabled. */
605
606 MEMORY_PROP(t, heap_start);
607 assert(t->heap_end >= t->heap_start);
608
609 struct pandecode_mapped_memory *heap =
610 pandecode_find_mapped_gpu_mem_containing(t->heap_start);
611
612 unsigned heap_size = t->heap_end - t->heap_start;
613
614 /* Tiling is enabled with a special flag */
615 unsigned hierarchy_mask = t->hierarchy_mask & MALI_HIERARCHY_MASK;
616 unsigned tiler_flags = t->hierarchy_mask ^ hierarchy_mask;
617
618 bool tiling_enabled = hierarchy_mask;
619
620 if (tiling_enabled) {
621 /* When tiling is enabled, the heap should be a tight fit */
622 unsigned heap_offset = t->heap_start - heap->gpu_va;
623 if ((heap_offset + heap_size) != heap->length) {
624 pandecode_msg("XXX: heap size %u (expected %zu)\n",
625 heap_size, heap->length - heap_offset);
626 }
627
628 /* We should also have no other flags */
629 if (tiler_flags)
630 pandecode_msg("XXX: unexpected tiler %X\n", tiler_flags);
631 } else {
632 /* When tiling is disabled, we should have that flag and no others */
633
634 if (tiler_flags != MALI_TILER_DISABLED) {
635 pandecode_msg("XXX: unexpected tiler flag %X, expected MALI_TILER_DISABLED\n",
636 tiler_flags);
637 }
638
639 /* We should also have an empty heap */
640 if (heap_size) {
641 pandecode_msg("XXX: tiler heap size %d given, expected empty\n",
642 heap_size);
643 }
644
645 /* Disabled tiling is used only for clear-only jobs, which are
646 * purely FRAGMENT, so we should never see this for
647 * non-FRAGMENT descriptors. */
648
649 if (!is_fragment)
650 pandecode_msg("XXX: tiler disabled for non-FRAGMENT job\n");
651 }
652
653 /* We've never seen weights used in practice, but we know from the
654 * kernel these fields is there */
655
656 bool nonzero_weights = false;
657
658 for (unsigned w = 0; w < ARRAY_SIZE(t->weights); ++w) {
659 nonzero_weights |= t->weights[w] != 0x0;
660 }
661
662 if (nonzero_weights) {
663 pandecode_log(".weights = { ");
664
665 for (unsigned w = 0; w < ARRAY_SIZE(t->weights); ++w) {
666 pandecode_log_cont("%d, ", t->weights[w]);
667 }
668
669 pandecode_log("},");
670 }
671
672 pandecode_indent--;
673 pandecode_log("}\n");
674 }
675
676 /* TODO: The Bifrost tiler is not understood at all yet */
677
678 static void
679 pandecode_bifrost_tiler_descriptor(const struct mali_framebuffer *fb)
680 {
681 pandecode_log(".tiler = {\n");
682 pandecode_indent++;
683
684 MEMORY_PROP(fb, tiler_meta);
685
686 for (int i = 0; i < 16; i++) {
687 if (fb->zeros[i] != 0) {
688 pandecode_msg("XXX: tiler descriptor zero %d tripped, value %x\n",
689 i, fb->zeros[i]);
690 }
691 }
692
693 pandecode_log("},\n");
694
695 pandecode_indent--;
696 pandecode_log("}\n");
697
698 }
699
700 /* Information about the framebuffer passed back for
701 * additional analysis */
702
703 struct pandecode_fbd {
704 unsigned width;
705 unsigned height;
706 unsigned rt_count;
707 bool has_extra;
708 };
709
710 static void
711 pandecode_sfbd_format(struct mali_sfbd_format format)
712 {
713 pandecode_log(".format = {\n");
714 pandecode_indent++;
715
716 pandecode_log(".unk1 = ");
717 pandecode_log_decoded_flags(sfbd_unk1_info, format.unk1);
718 pandecode_log_cont(",\n");
719
720 /* TODO: Map formats so we can check swizzles and print nicely */
721 pandecode_log("swizzle");
722 pandecode_swizzle(format.swizzle, MALI_RGBA8_UNORM);
723 pandecode_log_cont(",\n");
724
725 pandecode_prop("nr_channels = MALI_POSITIVE(%d)",
726 (format.nr_channels + 1));
727
728 pandecode_log(".unk2 = ");
729 pandecode_log_decoded_flags(sfbd_unk2_info, format.unk2);
730 pandecode_log_cont(",\n");
731
732 pandecode_prop("block = %s", pandecode_block_format(format.block));
733
734 pandecode_prop("unk3 = 0x%" PRIx32, format.unk3);
735
736 pandecode_indent--;
737 pandecode_log("},\n");
738 }
739
740 static void
741 pandecode_shared_memory(const struct mali_shared_memory *desc, bool is_compute)
742 {
743 pandecode_prop("stack_shift = 0x%x", desc->stack_shift);
744
745 if (desc->unk0)
746 pandecode_prop("unk0 = 0x%x", desc->unk0);
747
748 if (desc->shared_workgroup_count != 0x1F) {
749 pandecode_prop("shared_workgroup_count = %d", desc->shared_workgroup_count);
750 if (!is_compute)
751 pandecode_msg("XXX: wrong workgroup count for noncompute\n");
752 }
753
754 if (desc->shared_unk1 || desc->shared_shift) {
755 pandecode_prop("shared_unk1 = %X", desc->shared_unk1);
756 pandecode_prop("shared_shift = %X", desc->shared_shift);
757
758 if (!is_compute)
759 pandecode_msg("XXX: shared memory configured in noncompute shader");
760 }
761
762 if (desc->shared_zero) {
763 pandecode_msg("XXX: shared memory zero tripped\n");
764 pandecode_prop("shared_zero = 0x%" PRIx32, desc->shared_zero);
765 }
766
767 if (desc->shared_memory && !is_compute)
768 pandecode_msg("XXX: shared memory used in noncompute shader\n");
769
770 MEMORY_PROP(desc, scratchpad);
771 MEMORY_PROP(desc, shared_memory);
772 MEMORY_PROP(desc, unknown1);
773
774 if (desc->scratchpad) {
775 struct pandecode_mapped_memory *smem =
776 pandecode_find_mapped_gpu_mem_containing(desc->scratchpad);
777
778 pandecode_msg("scratchpad size %u\n", smem->length);
779 }
780
781 }
782
783 static struct pandecode_fbd
784 pandecode_sfbd(uint64_t gpu_va, int job_no, bool is_fragment, unsigned gpu_id)
785 {
786 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
787 const struct mali_single_framebuffer *PANDECODE_PTR_VAR(s, mem, (mali_ptr) gpu_va);
788
789 struct pandecode_fbd info = {
790 .has_extra = false,
791 .rt_count = 1
792 };
793
794 pandecode_log("struct mali_single_framebuffer framebuffer_%"PRIx64"_%d = {\n", gpu_va, job_no);
795 pandecode_indent++;
796
797 pandecode_log(".shared_memory = {\n");
798 pandecode_indent++;
799 pandecode_shared_memory(&s->shared_memory, false);
800 pandecode_indent--;
801 pandecode_log("},\n");
802
803 pandecode_sfbd_format(s->format);
804
805 info.width = s->width + 1;
806 info.height = s->height + 1;
807
808 pandecode_prop("width = MALI_POSITIVE(%" PRId16 ")", info.width);
809 pandecode_prop("height = MALI_POSITIVE(%" PRId16 ")", info.height);
810
811 MEMORY_PROP(s, checksum);
812
813 if (s->checksum_stride)
814 pandecode_prop("checksum_stride = %d", s->checksum_stride);
815
816 MEMORY_PROP(s, framebuffer);
817 pandecode_prop("stride = %d", s->stride);
818
819 /* Earlier in the actual commandstream -- right before width -- but we
820 * delay to flow nicer */
821
822 pandecode_log(".clear_flags = ");
823 pandecode_log_decoded_flags(clear_flag_info, s->clear_flags);
824 pandecode_log_cont(",\n");
825
826 if (s->depth_buffer) {
827 MEMORY_PROP(s, depth_buffer);
828 pandecode_prop("depth_stride = %d", s->depth_stride);
829 }
830
831 if (s->stencil_buffer) {
832 MEMORY_PROP(s, stencil_buffer);
833 pandecode_prop("stencil_stride = %d", s->stencil_stride);
834 }
835
836 if (s->depth_stride_zero ||
837 s->stencil_stride_zero ||
838 s->zero7 || s->zero8) {
839 pandecode_msg("XXX: Depth/stencil zeros tripped\n");
840 pandecode_prop("depth_stride_zero = 0x%x",
841 s->depth_stride_zero);
842 pandecode_prop("stencil_stride_zero = 0x%x",
843 s->stencil_stride_zero);
844 pandecode_prop("zero7 = 0x%" PRIx32,
845 s->zero7);
846 pandecode_prop("zero8 = 0x%" PRIx32,
847 s->zero8);
848 }
849
850 if (s->clear_color_1 | s->clear_color_2 | s->clear_color_3 | s->clear_color_4) {
851 pandecode_prop("clear_color_1 = 0x%" PRIx32, s->clear_color_1);
852 pandecode_prop("clear_color_2 = 0x%" PRIx32, s->clear_color_2);
853 pandecode_prop("clear_color_3 = 0x%" PRIx32, s->clear_color_3);
854 pandecode_prop("clear_color_4 = 0x%" PRIx32, s->clear_color_4);
855 }
856
857 if (s->clear_depth_1 != 0 || s->clear_depth_2 != 0 || s->clear_depth_3 != 0 || s->clear_depth_4 != 0) {
858 pandecode_prop("clear_depth_1 = %f", s->clear_depth_1);
859 pandecode_prop("clear_depth_2 = %f", s->clear_depth_2);
860 pandecode_prop("clear_depth_3 = %f", s->clear_depth_3);
861 pandecode_prop("clear_depth_4 = %f", s->clear_depth_4);
862 }
863
864 if (s->clear_stencil) {
865 pandecode_prop("clear_stencil = 0x%x", s->clear_stencil);
866 }
867
868 const struct midgard_tiler_descriptor t = s->tiler;
869
870 bool has_hierarchy = !(gpu_id == 0x0720 || gpu_id == 0x0820 || gpu_id == 0x0830);
871 pandecode_midgard_tiler_descriptor(&t, s->width + 1, s->height + 1, is_fragment, has_hierarchy);
872
873 pandecode_indent--;
874 pandecode_log("};\n");
875
876 pandecode_prop("zero2 = 0x%" PRIx32, s->zero2);
877 pandecode_prop("zero4 = 0x%" PRIx32, s->zero4);
878 pandecode_prop("zero5 = 0x%" PRIx32, s->zero5);
879
880 pandecode_log_cont(".zero3 = {");
881
882 for (int i = 0; i < sizeof(s->zero3) / sizeof(s->zero3[0]); ++i)
883 pandecode_log_cont("%X, ", s->zero3[i]);
884
885 pandecode_log_cont("},\n");
886
887 pandecode_log_cont(".zero6 = {");
888
889 for (int i = 0; i < sizeof(s->zero6) / sizeof(s->zero6[0]); ++i)
890 pandecode_log_cont("%X, ", s->zero6[i]);
891
892 pandecode_log_cont("},\n");
893
894 return info;
895 }
896
897 static void
898 pandecode_compute_fbd(uint64_t gpu_va, int job_no)
899 {
900 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
901 const struct mali_shared_memory *PANDECODE_PTR_VAR(s, mem, (mali_ptr) gpu_va);
902
903 pandecode_log("struct mali_shared_memory shared_%"PRIx64"_%d = {\n", gpu_va, job_no);
904 pandecode_indent++;
905 pandecode_shared_memory(s, true);
906 pandecode_indent--;
907 pandecode_log("},\n");
908 }
909
910 /* Extracts the number of components associated with a Mali format */
911
912 static unsigned
913 pandecode_format_component_count(enum mali_format fmt)
914 {
915 /* Mask out the format class */
916 unsigned top = fmt & 0b11100000;
917
918 switch (top) {
919 case MALI_FORMAT_SNORM:
920 case MALI_FORMAT_UINT:
921 case MALI_FORMAT_UNORM:
922 case MALI_FORMAT_SINT:
923 return ((fmt >> 3) & 3) + 1;
924 default:
925 /* TODO: Validate */
926 return 4;
927 }
928 }
929
930 /* Extracts a mask of accessed components from a 12-bit Mali swizzle */
931
932 static unsigned
933 pandecode_access_mask_from_channel_swizzle(unsigned swizzle)
934 {
935 unsigned mask = 0;
936 assert(MALI_CHANNEL_RED == 0);
937
938 for (unsigned c = 0; c < 4; ++c) {
939 enum mali_channel chan = (swizzle >> (3*c)) & 0x7;
940
941 if (chan <= MALI_CHANNEL_ALPHA)
942 mask |= (1 << chan);
943 }
944
945 return mask;
946 }
947
948 /* Validates that a (format, swizzle) pair is valid, in the sense that the
949 * swizzle doesn't access any components that are undefined in the format.
950 * Returns whether the swizzle is trivial (doesn't do any swizzling) and can be
951 * omitted */
952
953 static bool
954 pandecode_validate_format_swizzle(enum mali_format fmt, unsigned swizzle)
955 {
956 unsigned nr_comp = pandecode_format_component_count(fmt);
957 unsigned access_mask = pandecode_access_mask_from_channel_swizzle(swizzle);
958 unsigned valid_mask = (1 << nr_comp) - 1;
959 unsigned invalid_mask = ~valid_mask;
960
961 if (access_mask & invalid_mask) {
962 pandecode_msg("XXX: invalid components accessed\n");
963 return false;
964 }
965
966 /* Check for the default non-swizzling swizzle so we can suppress
967 * useless printing for the defaults */
968
969 unsigned default_swizzles[4] = {
970 MALI_CHANNEL_RED | (MALI_CHANNEL_ZERO << 3) | (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9),
971 MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_ZERO << 6) | (MALI_CHANNEL_ONE << 9),
972 MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ONE << 9),
973 MALI_CHANNEL_RED | (MALI_CHANNEL_GREEN << 3) | (MALI_CHANNEL_BLUE << 6) | (MALI_CHANNEL_ALPHA << 9)
974 };
975
976 return (swizzle == default_swizzles[nr_comp - 1]);
977 }
978
979 /* Maps MALI_RGBA32F to rgba32f, etc */
980
981 static void
982 pandecode_format_short(enum mali_format fmt, bool srgb)
983 {
984 /* We want a type-like format, so cut off the initial MALI_ */
985 char *format = pandecode_format(fmt);
986 format += strlen("MALI_");
987
988 unsigned len = strlen(format);
989 char *lower_format = calloc(1, len + 1);
990
991 for (unsigned i = 0; i < len; ++i)
992 lower_format[i] = tolower(format[i]);
993
994 /* Sanity check sRGB flag is applied to RGB, per the name */
995 if (srgb && lower_format[0] != 'r')
996 pandecode_msg("XXX: sRGB applied to non-colour format\n");
997
998 /* Just prefix with an s, so you get formats like srgba8_unorm */
999 if (srgb)
1000 pandecode_log_cont("s");
1001
1002 pandecode_log_cont("%s", lower_format);
1003 free(lower_format);
1004 }
1005
1006 static void
1007 pandecode_swizzle(unsigned swizzle, enum mali_format format)
1008 {
1009 /* First, do some validation */
1010 bool trivial_swizzle = pandecode_validate_format_swizzle(
1011 format, swizzle);
1012
1013 if (trivial_swizzle)
1014 return;
1015
1016 /* Next, print the swizzle */
1017 pandecode_log_cont(".");
1018
1019 static const char components[] = "rgba01";
1020
1021 for (unsigned c = 0; c < 4; ++c) {
1022 enum mali_channel chan = (swizzle >> (3 * c)) & 0x7;
1023
1024 if (chan >= MALI_CHANNEL_RESERVED_0) {
1025 pandecode_log("XXX: invalid swizzle channel %d\n", chan);
1026 continue;
1027 }
1028 pandecode_log_cont("%c", components[chan]);
1029 }
1030 }
1031
1032 static void
1033 pandecode_rt_format(struct mali_rt_format format)
1034 {
1035 pandecode_log(".format = {\n");
1036 pandecode_indent++;
1037
1038 pandecode_prop("unk1 = 0x%" PRIx32, format.unk1);
1039 pandecode_prop("unk2 = 0x%" PRIx32, format.unk2);
1040 pandecode_prop("unk3 = 0x%" PRIx32, format.unk3);
1041 pandecode_prop("unk4 = 0x%" PRIx32, format.unk4);
1042
1043 pandecode_prop("block = %s", pandecode_block_format(format.block));
1044
1045 /* TODO: Map formats so we can check swizzles and print nicely */
1046 pandecode_log("swizzle");
1047 pandecode_swizzle(format.swizzle, MALI_RGBA8_UNORM);
1048 pandecode_log_cont(",\n");
1049
1050 pandecode_prop("nr_channels = MALI_POSITIVE(%d)",
1051 (format.nr_channels + 1));
1052
1053 pandecode_log(".flags = ");
1054 pandecode_log_decoded_flags(mfbd_fmt_flag_info, format.flags);
1055 pandecode_log_cont(",\n");
1056
1057 pandecode_prop("msaa = %s", pandecode_msaa_mode(format.msaa));
1058
1059 /* In theory, the no_preload bit can be cleared to enable MFBD preload,
1060 * which is a faster hardware-based alternative to the wallpaper method
1061 * to preserve framebuffer contents across frames. In practice, MFBD
1062 * preload is buggy on Midgard, and so this is a chicken bit. If this
1063 * bit isn't set, most likely something broke unrelated to preload */
1064
1065 if (!format.no_preload) {
1066 pandecode_msg("XXX: buggy MFBD preload enabled - chicken bit should be clear\n");
1067 pandecode_prop("no_preload = 0x%" PRIx32, format.no_preload);
1068 }
1069
1070 if (format.zero)
1071 pandecode_prop("zero = 0x%" PRIx32, format.zero);
1072
1073 pandecode_indent--;
1074 pandecode_log("},\n");
1075 }
1076
1077 static void
1078 pandecode_render_target(uint64_t gpu_va, unsigned job_no, const struct mali_framebuffer *fb)
1079 {
1080 pandecode_log("struct mali_render_target rts_list_%"PRIx64"_%d[] = {\n", gpu_va, job_no);
1081 pandecode_indent++;
1082
1083 for (int i = 0; i < (fb->rt_count_1 + 1); i++) {
1084 mali_ptr rt_va = gpu_va + i * sizeof(struct mali_render_target);
1085 struct pandecode_mapped_memory *mem =
1086 pandecode_find_mapped_gpu_mem_containing(rt_va);
1087 const struct mali_render_target *PANDECODE_PTR_VAR(rt, mem, (mali_ptr) rt_va);
1088
1089 pandecode_log("{\n");
1090 pandecode_indent++;
1091
1092 pandecode_rt_format(rt->format);
1093
1094 if (rt->format.block == MALI_BLOCK_AFBC) {
1095 pandecode_log(".afbc = {\n");
1096 pandecode_indent++;
1097
1098 char *a = pointer_as_memory_reference(rt->afbc.metadata);
1099 pandecode_prop("metadata = %s", a);
1100 free(a);
1101
1102 pandecode_prop("stride = %d", rt->afbc.stride);
1103
1104 pandecode_log(".flags = ");
1105 pandecode_log_decoded_flags(afbc_fmt_flag_info, rt->afbc.flags);
1106 pandecode_log_cont(",\n");
1107
1108 pandecode_indent--;
1109 pandecode_log("},\n");
1110 } else if (rt->afbc.metadata || rt->afbc.stride || rt->afbc.flags) {
1111 pandecode_msg("XXX: AFBC disabled but AFBC field set (0x%lX, 0x%x, 0x%x)\n",
1112 rt->afbc.metadata,
1113 rt->afbc.stride,
1114 rt->afbc.flags);
1115 }
1116
1117 MEMORY_PROP(rt, framebuffer);
1118 pandecode_prop("framebuffer_stride = %d", rt->framebuffer_stride);
1119
1120 if (rt->layer_stride)
1121 pandecode_prop("layer_stride = %d", rt->layer_stride);
1122
1123 if (rt->clear_color_1 | rt->clear_color_2 | rt->clear_color_3 | rt->clear_color_4) {
1124 pandecode_prop("clear_color_1 = 0x%" PRIx32, rt->clear_color_1);
1125 pandecode_prop("clear_color_2 = 0x%" PRIx32, rt->clear_color_2);
1126 pandecode_prop("clear_color_3 = 0x%" PRIx32, rt->clear_color_3);
1127 pandecode_prop("clear_color_4 = 0x%" PRIx32, rt->clear_color_4);
1128 }
1129
1130 if (rt->zero1 || rt->zero2) {
1131 pandecode_msg("XXX: render target zeros tripped\n");
1132 pandecode_prop("zero1 = 0x%" PRIx64, rt->zero1);
1133 pandecode_prop("zero2 = 0x%" PRIx32, rt->zero2);
1134 }
1135
1136 pandecode_indent--;
1137 pandecode_log("},\n");
1138 }
1139
1140 pandecode_indent--;
1141 pandecode_log("};\n");
1142 }
1143
1144 static struct pandecode_fbd
1145 pandecode_mfbd_bfr(uint64_t gpu_va, int job_no, bool is_fragment, bool is_compute, bool is_bifrost)
1146 {
1147 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
1148 const struct mali_framebuffer *PANDECODE_PTR_VAR(fb, mem, (mali_ptr) gpu_va);
1149
1150 struct pandecode_fbd info;
1151
1152 if (is_bifrost && fb->msaa.sample_locations) {
1153 /* The blob stores all possible sample locations in a single buffer
1154 * allocated on startup, and just switches the pointer when switching
1155 * MSAA state. For now, we just put the data into the cmdstream, but we
1156 * should do something like what the blob does with a real driver.
1157 *
1158 * There seem to be 32 slots for sample locations, followed by another
1159 * 16. The second 16 is just the center location followed by 15 zeros
1160 * in all the cases I've identified (maybe shader vs. depth/color
1161 * samples?).
1162 */
1163
1164 struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(fb->msaa.sample_locations);
1165
1166 const u16 *PANDECODE_PTR_VAR(samples, smem, fb->msaa.sample_locations);
1167
1168 pandecode_log("uint16_t sample_locations_%d[] = {\n", job_no);
1169 pandecode_indent++;
1170
1171 for (int i = 0; i < 32 + 16; i++) {
1172 pandecode_log("%d, %d,\n", samples[2 * i], samples[2 * i + 1]);
1173 }
1174
1175 pandecode_indent--;
1176 pandecode_log("};\n");
1177 }
1178
1179 pandecode_log("struct mali_framebuffer framebuffer_%"PRIx64"_%d = {\n", gpu_va, job_no);
1180 pandecode_indent++;
1181
1182 if (is_bifrost) {
1183 pandecode_log(".msaa = {\n");
1184 pandecode_indent++;
1185
1186 if (fb->msaa.sample_locations)
1187 pandecode_prop("sample_locations = sample_locations_%d", job_no);
1188 else
1189 pandecode_msg("XXX: sample_locations missing\n");
1190
1191 if (fb->msaa.zero1 || fb->msaa.zero2 || fb->msaa.zero4) {
1192 pandecode_msg("XXX: multisampling zero tripped\n");
1193 pandecode_prop("zero1 = %" PRIx64, fb->msaa.zero1);
1194 pandecode_prop("zero2 = %" PRIx64, fb->msaa.zero2);
1195 pandecode_prop("zero4 = %" PRIx64, fb->msaa.zero4);
1196 }
1197
1198 pandecode_indent--;
1199 pandecode_log("},\n");
1200 } else {
1201 pandecode_log(".shared_memory = {\n");
1202 pandecode_indent++;
1203 pandecode_shared_memory(&fb->shared_memory, is_compute);
1204 pandecode_indent--;
1205 pandecode_log("},\n");
1206 }
1207
1208 info.width = fb->width1 + 1;
1209 info.height = fb->height1 + 1;
1210 info.rt_count = fb->rt_count_1 + 1;
1211
1212 pandecode_prop("width1 = MALI_POSITIVE(%d)", fb->width1 + 1);
1213 pandecode_prop("height1 = MALI_POSITIVE(%d)", fb->height1 + 1);
1214 pandecode_prop("width2 = MALI_POSITIVE(%d)", fb->width2 + 1);
1215 pandecode_prop("height2 = MALI_POSITIVE(%d)", fb->height2 + 1);
1216
1217 pandecode_prop("unk1 = 0x%x", fb->unk1);
1218 pandecode_prop("unk2 = 0x%x", fb->unk2);
1219 pandecode_prop("rt_count_1 = MALI_POSITIVE(%d)", fb->rt_count_1 + 1);
1220 pandecode_prop("rt_count_2 = %d", fb->rt_count_2);
1221
1222 pandecode_log(".mfbd_flags = ");
1223 pandecode_log_decoded_flags(mfbd_flag_info, fb->mfbd_flags);
1224 pandecode_log_cont(",\n");
1225
1226 if (fb->clear_stencil)
1227 pandecode_prop("clear_stencil = 0x%x", fb->clear_stencil);
1228
1229 if (fb->clear_depth)
1230 pandecode_prop("clear_depth = %f", fb->clear_depth);
1231
1232 if (!is_compute)
1233 if (is_bifrost)
1234 pandecode_bifrost_tiler_descriptor(fb);
1235 else {
1236 const struct midgard_tiler_descriptor t = fb->tiler;
1237 pandecode_midgard_tiler_descriptor(&t, fb->width1 + 1, fb->height1 + 1, is_fragment, true);
1238 }
1239 else
1240 pandecode_msg("XXX: skipping compute MFBD, fixme\n");
1241
1242 if (fb->zero3 || fb->zero4) {
1243 pandecode_msg("XXX: framebuffer zeros tripped\n");
1244 pandecode_prop("zero3 = 0x%" PRIx32, fb->zero3);
1245 pandecode_prop("zero4 = 0x%" PRIx32, fb->zero4);
1246 }
1247
1248 pandecode_indent--;
1249 pandecode_log("};\n");
1250
1251 gpu_va += sizeof(struct mali_framebuffer);
1252
1253 info.has_extra = (fb->mfbd_flags & MALI_MFBD_EXTRA) && is_fragment;
1254
1255 if (info.has_extra) {
1256 mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
1257 const struct mali_framebuffer_extra *PANDECODE_PTR_VAR(fbx, mem, (mali_ptr) gpu_va);
1258
1259 pandecode_log("struct mali_framebuffer_extra fb_extra_%"PRIx64"_%d = {\n", gpu_va, job_no);
1260 pandecode_indent++;
1261
1262 MEMORY_PROP(fbx, checksum);
1263
1264 if (fbx->checksum_stride)
1265 pandecode_prop("checksum_stride = %d", fbx->checksum_stride);
1266
1267 pandecode_log(".flags_hi = ");
1268 pandecode_log_decoded_flags(mfbd_extra_flag_hi_info, fbx->flags_hi);
1269 pandecode_log_cont(",\n");
1270
1271 pandecode_log(".flags_lo = ");
1272 pandecode_log_decoded_flags(mfbd_extra_flag_lo_info, fbx->flags_lo);
1273 pandecode_log_cont(",\n");
1274
1275 pandecode_prop("zs_block = %s", pandecode_block_format(fbx->zs_block));
1276 pandecode_prop("zs_samples = MALI_POSITIVE(%u)", fbx->zs_samples + 1);
1277
1278 if (fbx->zs_block == MALI_BLOCK_AFBC) {
1279 pandecode_log(".ds_afbc = {\n");
1280 pandecode_indent++;
1281
1282 MEMORY_PROP_DIR(fbx->ds_afbc, depth_stencil_afbc_metadata);
1283 pandecode_prop("depth_stencil_afbc_stride = %d",
1284 fbx->ds_afbc.depth_stencil_afbc_stride);
1285 MEMORY_PROP_DIR(fbx->ds_afbc, depth_stencil);
1286
1287 pandecode_log(".flags = ");
1288 pandecode_log_decoded_flags(afbc_fmt_flag_info, fbx->ds_afbc.flags);
1289 pandecode_log_cont(",\n");
1290
1291 if (fbx->ds_afbc.padding) {
1292 pandecode_msg("XXX: Depth/stencil AFBC zeros tripped\n");
1293 pandecode_prop("padding = 0x%" PRIx64, fbx->ds_afbc.padding);
1294 }
1295
1296 pandecode_indent--;
1297 pandecode_log("},\n");
1298 } else {
1299 pandecode_log(".ds_linear = {\n");
1300 pandecode_indent++;
1301
1302 if (fbx->ds_linear.depth) {
1303 MEMORY_PROP_DIR(fbx->ds_linear, depth);
1304 pandecode_prop("depth_stride = %d",
1305 fbx->ds_linear.depth_stride);
1306 pandecode_prop("depth_layer_stride = %d",
1307 fbx->ds_linear.depth_layer_stride);
1308 } else if (fbx->ds_linear.depth_stride || fbx->ds_linear.depth_layer_stride) {
1309 pandecode_msg("XXX: depth stride zero tripped %d %d\n", fbx->ds_linear.depth_stride, fbx->ds_linear.depth_layer_stride);
1310 }
1311
1312 if (fbx->ds_linear.stencil) {
1313 MEMORY_PROP_DIR(fbx->ds_linear, stencil);
1314 pandecode_prop("stencil_stride = %d",
1315 fbx->ds_linear.stencil_stride);
1316 pandecode_prop("stencil_layer_stride = %d",
1317 fbx->ds_linear.stencil_layer_stride);
1318 } else if (fbx->ds_linear.stencil_stride || fbx->ds_linear.stencil_layer_stride) {
1319 pandecode_msg("XXX: stencil stride zero tripped %d %d\n", fbx->ds_linear.stencil_stride, fbx->ds_linear.stencil_layer_stride);
1320 }
1321
1322 if (fbx->ds_linear.depth_stride_zero ||
1323 fbx->ds_linear.stencil_stride_zero) {
1324 pandecode_msg("XXX: Depth/stencil zeros tripped\n");
1325 pandecode_prop("depth_stride_zero = 0x%x",
1326 fbx->ds_linear.depth_stride_zero);
1327 pandecode_prop("stencil_stride_zero = 0x%x",
1328 fbx->ds_linear.stencil_stride_zero);
1329 }
1330
1331 pandecode_indent--;
1332 pandecode_log("},\n");
1333 }
1334
1335 if (fbx->clear_color_1 | fbx->clear_color_2) {
1336 pandecode_prop("clear_color_1 = 0x%" PRIx32, fbx->clear_color_1);
1337 pandecode_prop("clear_color_2 = 0x%" PRIx32, fbx->clear_color_2);
1338 }
1339
1340 if (fbx->zero3) {
1341 pandecode_msg("XXX: fb_extra zeros tripped\n");
1342 pandecode_prop("zero3 = 0x%" PRIx64, fbx->zero3);
1343 }
1344
1345 pandecode_indent--;
1346 pandecode_log("};\n");
1347
1348 gpu_va += sizeof(struct mali_framebuffer_extra);
1349 }
1350
1351 if (is_fragment)
1352 pandecode_render_target(gpu_va, job_no, fb);
1353
1354 return info;
1355 }
1356
1357 /* Just add a comment decoding the shift/odd fields forming the padded vertices
1358 * count */
1359
1360 static void
1361 pandecode_padded_vertices(unsigned shift, unsigned k)
1362 {
1363 unsigned odd = 2*k + 1;
1364 unsigned pot = 1 << shift;
1365 pandecode_msg("padded_num_vertices = %d\n", odd * pot);
1366 }
1367
1368 /* Given a magic divisor, recover what we were trying to divide by.
1369 *
1370 * Let m represent the magic divisor. By definition, m is an element on Z, whre
1371 * 0 <= m < 2^N, for N bits in m.
1372 *
1373 * Let q represent the number we would like to divide by.
1374 *
1375 * By definition of a magic divisor for N-bit unsigned integers (a number you
1376 * multiply by to magically get division), m is a number such that:
1377 *
1378 * (m * x) & (2^N - 1) = floor(x/q).
1379 * for all x on Z where 0 <= x < 2^N
1380 *
1381 * Ignore the case where any of the above values equals zero; it is irrelevant
1382 * for our purposes (instanced arrays).
1383 *
1384 * Choose x = q. Then:
1385 *
1386 * (m * x) & (2^N - 1) = floor(x/q).
1387 * (m * q) & (2^N - 1) = floor(q/q).
1388 *
1389 * floor(q/q) = floor(1) = 1, therefore:
1390 *
1391 * (m * q) & (2^N - 1) = 1
1392 *
1393 * Recall the identity that the bitwise AND of one less than a power-of-two
1394 * equals the modulo with that power of two, i.e. for all x:
1395 *
1396 * x & (2^N - 1) = x % N
1397 *
1398 * Therefore:
1399 *
1400 * mq % (2^N) = 1
1401 *
1402 * By definition, a modular multiplicative inverse of a number m is the number
1403 * q such that with respect to a modulos M:
1404 *
1405 * mq % M = 1
1406 *
1407 * Therefore, q is the modular multiplicative inverse of m with modulus 2^N.
1408 *
1409 */
1410
1411 static void
1412 pandecode_magic_divisor(uint32_t magic, unsigned shift, unsigned orig_divisor, unsigned extra)
1413 {
1414 #if 0
1415 /* Compute the modular inverse of `magic` with respect to 2^(32 -
1416 * shift) the most lame way possible... just repeatedly add.
1417 * Asymptoptically slow but nobody cares in practice, unless you have
1418 * massive numbers of vertices or high divisors. */
1419
1420 unsigned inverse = 0;
1421
1422 /* Magic implicitly has the highest bit set */
1423 magic |= (1 << 31);
1424
1425 /* Depending on rounding direction */
1426 if (extra)
1427 magic++;
1428
1429 for (;;) {
1430 uint32_t product = magic * inverse;
1431
1432 if (shift) {
1433 product >>= shift;
1434 }
1435
1436 if (product == 1)
1437 break;
1438
1439 ++inverse;
1440 }
1441
1442 pandecode_msg("dividing by %d (maybe off by two)\n", inverse);
1443
1444 /* Recall we're supposed to divide by (gl_level_divisor *
1445 * padded_num_vertices) */
1446
1447 unsigned padded_num_vertices = inverse / orig_divisor;
1448
1449 pandecode_msg("padded_num_vertices = %d\n", padded_num_vertices);
1450 #endif
1451 }
1452
1453 static void
1454 pandecode_attributes(const struct pandecode_mapped_memory *mem,
1455 mali_ptr addr, int job_no, char *suffix,
1456 int count, bool varying, enum mali_job_type job_type)
1457 {
1458 char *prefix = varying ? "varying" : "attribute";
1459 assert(addr);
1460
1461 if (!count) {
1462 pandecode_msg("warn: No %s records\n", prefix);
1463 return;
1464 }
1465
1466 union mali_attr *attr = pandecode_fetch_gpu_mem(mem, addr, sizeof(union mali_attr) * count);
1467
1468 for (int i = 0; i < count; ++i) {
1469 /* First, check for special records */
1470 if (attr[i].elements < MALI_RECORD_SPECIAL) {
1471 if (attr[i].size)
1472 pandecode_msg("XXX: tripped size=%d\n", attr[i].size);
1473
1474 if (attr[i].stride) {
1475 /* gl_InstanceID passes a magic divisor in the
1476 * stride field to divide by the padded vertex
1477 * count. No other records should do so, so
1478 * stride should otherwise be zero. Note that
1479 * stride in the usual attribute sense doesn't
1480 * apply to special records. */
1481
1482 bool has_divisor = attr[i].elements == MALI_ATTR_INSTANCEID;
1483
1484 pandecode_log_cont("/* %smagic divisor = %X */ ",
1485 has_divisor ? "" : "XXX: ", attr[i].stride);
1486 }
1487
1488 if (attr[i].shift || attr[i].extra_flags) {
1489 /* Attributes use these fields for
1490 * instancing/padding/etc type issues, but
1491 * varyings don't */
1492
1493 pandecode_log_cont("/* %sshift=%d, extra=%d */ ",
1494 varying ? "XXX: " : "",
1495 attr[i].shift, attr[i].extra_flags);
1496 }
1497
1498 /* Print the special record name */
1499 bool attribute = false;
1500 pandecode_log("%s_%d = %s;\n", prefix, i, pandecode_special_record(attr[i].elements, &attribute));
1501
1502 /* Sanity check */
1503 if (attribute == varying)
1504 pandecode_msg("XXX: mismatched special record\n");
1505
1506 continue;
1507 }
1508
1509 enum mali_attr_mode mode = attr[i].elements & 7;
1510
1511 if (mode == MALI_ATTR_UNUSED)
1512 pandecode_msg("XXX: unused attribute record\n");
1513
1514 /* For non-linear records, we need to print the type of record */
1515 if (mode != MALI_ATTR_LINEAR)
1516 pandecode_log_cont("%s ", pandecode_attr_mode_short(mode));
1517
1518 /* Print the name to link with attr_meta */
1519 pandecode_log_cont("%s_%d", prefix, i);
1520
1521 /* Print the stride and size */
1522 pandecode_log_cont("<%u>[%u]", attr[i].stride, attr[i].size);
1523
1524 /* TODO: Sanity check the quotient itself. It must be equal to
1525 * (or be greater than, if the driver added padding) the padded
1526 * vertex count. */
1527
1528 /* Finally, print the pointer */
1529 mali_ptr raw_elements = attr[i].elements & ~7;
1530 char *a = pointer_as_memory_reference(raw_elements);
1531 pandecode_log_cont(" = (%s);\n", a);
1532 free(a);
1533
1534 /* Check the pointer */
1535 pandecode_validate_buffer(raw_elements, attr[i].size);
1536
1537 /* shift/extra_flags exist only for instanced */
1538 if (attr[i].shift | attr[i].extra_flags) {
1539 /* These are set to random values by the blob for
1540 * varyings, most likely a symptom of uninitialized
1541 * memory where the hardware masked the bug. As such we
1542 * put this at a warning, not an error. */
1543
1544 if (mode == MALI_ATTR_LINEAR)
1545 pandecode_msg("warn: instancing fields set for linear\n");
1546
1547 pandecode_prop("shift = %d", attr[i].shift);
1548 pandecode_prop("extra_flags = %d", attr[i].extra_flags);
1549 }
1550
1551 /* Decode further where possible */
1552
1553 if (mode == MALI_ATTR_MODULO) {
1554 pandecode_padded_vertices(
1555 attr[i].shift,
1556 attr[i].extra_flags);
1557 }
1558
1559 if (mode == MALI_ATTR_NPOT_DIVIDE) {
1560 i++;
1561 pandecode_log("{\n");
1562 pandecode_indent++;
1563 pandecode_prop("unk = 0x%x", attr[i].unk);
1564 pandecode_prop("magic_divisor = 0x%08x", attr[i].magic_divisor);
1565 if (attr[i].zero != 0)
1566 pandecode_prop("XXX: zero tripped (0x%x)\n", attr[i].zero);
1567 pandecode_prop("divisor = %d", attr[i].divisor);
1568 pandecode_magic_divisor(attr[i].magic_divisor, attr[i - 1].shift, attr[i].divisor, attr[i - 1].extra_flags);
1569 pandecode_indent--;
1570 pandecode_log("}, \n");
1571 }
1572
1573 }
1574
1575 pandecode_log("\n");
1576 }
1577
1578 static mali_ptr
1579 pandecode_shader_address(const char *name, mali_ptr ptr)
1580 {
1581 /* TODO: Decode flags */
1582 mali_ptr shader_ptr = ptr & ~15;
1583
1584 char *a = pointer_as_memory_reference(shader_ptr);
1585 pandecode_prop("%s = (%s) | %d", name, a, (int) (ptr & 15));
1586 free(a);
1587
1588 return shader_ptr;
1589 }
1590
1591 static void
1592 pandecode_stencil(const char *name, const struct mali_stencil_test *stencil)
1593 {
1594 unsigned any_nonzero =
1595 stencil->ref | stencil->mask | stencil->func |
1596 stencil->sfail | stencil->dpfail | stencil->dppass;
1597
1598 if (any_nonzero == 0)
1599 return;
1600
1601 const char *func = mali_func_as_str(stencil->func);
1602 const char *sfail = mali_stencil_op_as_str(stencil->sfail);
1603 const char *dpfail = mali_stencil_op_as_str(stencil->dpfail);
1604 const char *dppass = mali_stencil_op_as_str(stencil->dppass);
1605
1606 if (stencil->zero)
1607 pandecode_msg("XXX: stencil zero tripped: %X\n", stencil->zero);
1608
1609 pandecode_log(".stencil_%s = {\n", name);
1610 pandecode_indent++;
1611 pandecode_prop("ref = %d", stencil->ref);
1612 pandecode_prop("mask = 0x%02X", stencil->mask);
1613 pandecode_prop("func = %s", func);
1614 pandecode_prop("sfail = %s", sfail);
1615 pandecode_prop("dpfail = %s", dpfail);
1616 pandecode_prop("dppass = %s", dppass);
1617 pandecode_indent--;
1618 pandecode_log("},\n");
1619 }
1620
1621 static void
1622 pandecode_blend_equation(const struct mali_blend_equation *blend)
1623 {
1624 if (blend->zero1)
1625 pandecode_msg("XXX: blend zero tripped: %X\n", blend->zero1);
1626
1627 pandecode_log(".equation = {\n");
1628 pandecode_indent++;
1629
1630 pandecode_prop("rgb_mode = 0x%X", blend->rgb_mode);
1631 pandecode_prop("alpha_mode = 0x%X", blend->alpha_mode);
1632
1633 pandecode_log(".color_mask = ");
1634 pandecode_log_decoded_flags(mask_flag_info, blend->color_mask);
1635 pandecode_log_cont(",\n");
1636
1637 pandecode_indent--;
1638 pandecode_log("},\n");
1639 }
1640
1641 /* Decodes a Bifrost blend constant. See the notes in bifrost_blend_rt */
1642
1643 static unsigned
1644 decode_bifrost_constant(u16 constant)
1645 {
1646 float lo = (float) (constant & 0xFF);
1647 float hi = (float) (constant >> 8);
1648
1649 return (hi / 255.0) + (lo / 65535.0);
1650 }
1651
1652 static mali_ptr
1653 pandecode_bifrost_blend(void *descs, int job_no, int rt_no)
1654 {
1655 struct bifrost_blend_rt *b =
1656 ((struct bifrost_blend_rt *) descs) + rt_no;
1657
1658 pandecode_log("struct bifrost_blend_rt blend_rt_%d_%d = {\n", job_no, rt_no);
1659 pandecode_indent++;
1660
1661 pandecode_prop("flags = 0x%" PRIx16, b->flags);
1662 pandecode_prop("constant = 0x%" PRIx8 " /* %f */",
1663 b->constant, decode_bifrost_constant(b->constant));
1664
1665 /* TODO figure out blend shader enable bit */
1666 pandecode_blend_equation(&b->equation);
1667
1668 pandecode_prop("unk2 = 0x%" PRIx16, b->unk2);
1669 pandecode_prop("index = 0x%" PRIx16, b->index);
1670
1671 pandecode_log(".format = ");
1672 pandecode_format_short(b->format, false);
1673 pandecode_swizzle(b->swizzle, b->format);
1674 pandecode_log_cont(",\n");
1675
1676 pandecode_prop("swizzle = 0x%" PRIx32, b->swizzle);
1677 pandecode_prop("format = 0x%" PRIx32, b->format);
1678
1679 if (b->zero1) {
1680 pandecode_msg("XXX: pandecode_bifrost_blend zero1 tripped\n");
1681 pandecode_prop("zero1 = 0x%" PRIx32, b->zero1);
1682 }
1683
1684 pandecode_log(".shader_type = ");
1685 switch(b->shader_type) {
1686 case BIFROST_BLEND_F16:
1687 pandecode_log_cont("BIFROST_BLEND_F16");
1688 break;
1689 case BIFROST_BLEND_F32:
1690 pandecode_log_cont("BIFROST_BLEND_F32");
1691 break;
1692 case BIFROST_BLEND_I32:
1693 pandecode_log_cont("BIFROST_BLEND_I32");
1694 break;
1695 case BIFROST_BLEND_U32:
1696 pandecode_log_cont("BIFROST_BLEND_U32");
1697 break;
1698 case BIFROST_BLEND_I16:
1699 pandecode_log_cont("BIFROST_BLEND_I16");
1700 break;
1701 case BIFROST_BLEND_U16:
1702 pandecode_log_cont("BIFROST_BLEND_U16");
1703 break;
1704 }
1705 pandecode_log_cont(",\n");
1706
1707 if (b->zero2) {
1708 pandecode_msg("XXX: pandecode_bifrost_blend zero2 tripped\n");
1709 pandecode_prop("zero2 = 0x%" PRIx32, b->zero2);
1710 }
1711
1712 pandecode_prop("shader = 0x%" PRIx32, b->shader);
1713
1714 pandecode_indent--;
1715 pandecode_log("},\n");
1716
1717 return 0;
1718 }
1719
1720 static mali_ptr
1721 pandecode_midgard_blend(union midgard_blend *blend, bool is_shader)
1722 {
1723 /* constant/equation is in a union */
1724 if (!blend->shader)
1725 return 0;
1726
1727 pandecode_log(".blend = {\n");
1728 pandecode_indent++;
1729
1730 if (is_shader) {
1731 pandecode_shader_address("shader", blend->shader);
1732 } else {
1733 pandecode_blend_equation(&blend->equation);
1734 pandecode_prop("constant = %f", blend->constant);
1735 }
1736
1737 pandecode_indent--;
1738 pandecode_log("},\n");
1739
1740 /* Return blend shader to disassemble if present */
1741 return is_shader ? (blend->shader & ~0xF) : 0;
1742 }
1743
1744 static mali_ptr
1745 pandecode_midgard_blend_mrt(void *descs, int job_no, int rt_no)
1746 {
1747 struct midgard_blend_rt *b =
1748 ((struct midgard_blend_rt *) descs) + rt_no;
1749
1750 /* Flags determine presence of blend shader */
1751 bool is_shader = (b->flags & 0xF) >= 0x2;
1752
1753 pandecode_log("struct midgard_blend_rt blend_rt_%d_%d = {\n", job_no, rt_no);
1754 pandecode_indent++;
1755
1756 pandecode_prop("flags = 0x%" PRIx64, b->flags);
1757
1758 union midgard_blend blend = b->blend;
1759 mali_ptr shader = pandecode_midgard_blend(&blend, is_shader);
1760
1761 pandecode_indent--;
1762 pandecode_log("};\n");
1763
1764 return shader;
1765 }
1766
1767 /* Attributes and varyings have descriptor records, which contain information
1768 * about their format and ordering with the attribute/varying buffers. We'll
1769 * want to validate that the combinations specified are self-consistent.
1770 */
1771
1772 static int
1773 pandecode_attribute_meta(int job_no, int count, const struct mali_vertex_tiler_postfix *v, bool varying, char *suffix)
1774 {
1775 char base[128];
1776 char *prefix = varying ? "varying" : "attribute";
1777 unsigned max_index = 0;
1778 snprintf(base, sizeof(base), "%s_meta", prefix);
1779
1780 struct mali_attr_meta *attr_meta;
1781 mali_ptr p = varying ? v->varying_meta : v->attribute_meta;
1782
1783 struct pandecode_mapped_memory *attr_mem = pandecode_find_mapped_gpu_mem_containing(p);
1784
1785 for (int i = 0; i < count; ++i, p += sizeof(struct mali_attr_meta)) {
1786 attr_meta = pandecode_fetch_gpu_mem(attr_mem, p,
1787 sizeof(*attr_mem));
1788
1789 /* If the record is discard, it should be zero for everything else */
1790
1791 if (attr_meta->format == MALI_VARYING_DISCARD) {
1792 uint64_t zero =
1793 attr_meta->index |
1794 attr_meta->unknown1 |
1795 attr_meta->unknown3 |
1796 attr_meta->src_offset;
1797
1798 if (zero)
1799 pandecode_msg("XXX: expected empty record for varying discard\n");
1800
1801 /* We want to look for a literal 0000 swizzle -- this
1802 * is not encoded with all zeroes, however */
1803
1804 enum mali_channel z = MALI_CHANNEL_ZERO;
1805 unsigned zero_swizzle = z | (z << 3) | (z << 6) | (z << 9);
1806 bool good_swizzle = attr_meta->swizzle == zero_swizzle;
1807
1808 if (!good_swizzle)
1809 pandecode_msg("XXX: expected zero swizzle for discard\n");
1810
1811 if (!varying)
1812 pandecode_msg("XXX: cannot discard attribute\n");
1813
1814 /* If we're all good, omit the record */
1815 if (!zero && varying && good_swizzle) {
1816 pandecode_log("/* discarded varying */\n");
1817 continue;
1818 }
1819 }
1820
1821 if (attr_meta->index > max_index)
1822 max_index = attr_meta->index;
1823
1824 if (attr_meta->unknown1 != 0x2) {
1825 pandecode_msg("XXX: expected unknown1 = 0x2\n");
1826 pandecode_prop("unknown1 = 0x%" PRIx64, (u64) attr_meta->unknown1);
1827 }
1828
1829 if (attr_meta->unknown3) {
1830 pandecode_msg("XXX: unexpected unknown3 set\n");
1831 pandecode_prop("unknown3 = 0x%" PRIx64, (u64) attr_meta->unknown3);
1832 }
1833
1834 pandecode_format_short(attr_meta->format, false);
1835 pandecode_log_cont(" %s_%u", prefix, attr_meta->index);
1836
1837 if (attr_meta->src_offset)
1838 pandecode_log_cont("[%u]", attr_meta->src_offset);
1839
1840 pandecode_swizzle(attr_meta->swizzle, attr_meta->format);
1841
1842 pandecode_log_cont(";\n");
1843 }
1844
1845 pandecode_log("\n");
1846
1847 return count ? (max_index + 1) : 0;
1848 }
1849
1850 /* return bits [lo, hi) of word */
1851 static u32
1852 bits(u32 word, u32 lo, u32 hi)
1853 {
1854 if (hi - lo >= 32)
1855 return word; // avoid undefined behavior with the shift
1856
1857 return (word >> lo) & ((1 << (hi - lo)) - 1);
1858 }
1859
1860 static void
1861 pandecode_vertex_tiler_prefix(struct mali_vertex_tiler_prefix *p, int job_no, bool graphics)
1862 {
1863 pandecode_log(".prefix = {\n");
1864 pandecode_indent++;
1865
1866 /* Decode invocation_count. See the comment before the definition of
1867 * invocation_count for an explanation.
1868 */
1869
1870 unsigned size_y_shift = bits(p->invocation_shifts, 0, 5);
1871 unsigned size_z_shift = bits(p->invocation_shifts, 5, 10);
1872 unsigned workgroups_x_shift = bits(p->invocation_shifts, 10, 16);
1873 unsigned workgroups_y_shift = bits(p->invocation_shifts, 16, 22);
1874 unsigned workgroups_z_shift = bits(p->invocation_shifts, 22, 28);
1875 unsigned workgroups_x_shift_2 = bits(p->invocation_shifts, 28, 32);
1876
1877 unsigned size_x = bits(p->invocation_count, 0, size_y_shift) + 1;
1878 unsigned size_y = bits(p->invocation_count, size_y_shift, size_z_shift) + 1;
1879 unsigned size_z = bits(p->invocation_count, size_z_shift, workgroups_x_shift) + 1;
1880
1881 unsigned groups_x = bits(p->invocation_count, workgroups_x_shift, workgroups_y_shift) + 1;
1882 unsigned groups_y = bits(p->invocation_count, workgroups_y_shift, workgroups_z_shift) + 1;
1883 unsigned groups_z = bits(p->invocation_count, workgroups_z_shift, 32) + 1;
1884
1885 /* Even though we have this decoded, we want to ensure that the
1886 * representation is "unique" so we don't lose anything by printing only
1887 * the final result. More specifically, we need to check that we were
1888 * passed something in canonical form, since the definition per the
1889 * hardware is inherently not unique. How? Well, take the resulting
1890 * decode and pack it ourselves! If it is bit exact with what we
1891 * decoded, we're good to go. */
1892
1893 struct mali_vertex_tiler_prefix ref;
1894 panfrost_pack_work_groups_compute(&ref, groups_x, groups_y, groups_z, size_x, size_y, size_z, graphics);
1895
1896 bool canonical =
1897 (p->invocation_count == ref.invocation_count) &&
1898 (p->invocation_shifts == ref.invocation_shifts);
1899
1900 if (!canonical) {
1901 pandecode_msg("XXX: non-canonical workgroups packing\n");
1902 pandecode_msg("expected: %X, %X",
1903 ref.invocation_count,
1904 ref.invocation_shifts);
1905
1906 pandecode_prop("invocation_count = 0x%" PRIx32, p->invocation_count);
1907 pandecode_prop("size_y_shift = %d", size_y_shift);
1908 pandecode_prop("size_z_shift = %d", size_z_shift);
1909 pandecode_prop("workgroups_x_shift = %d", workgroups_x_shift);
1910 pandecode_prop("workgroups_y_shift = %d", workgroups_y_shift);
1911 pandecode_prop("workgroups_z_shift = %d", workgroups_z_shift);
1912 pandecode_prop("workgroups_x_shift_2 = %d", workgroups_x_shift_2);
1913 }
1914
1915 /* Regardless, print the decode */
1916 pandecode_msg("size (%d, %d, %d), count (%d, %d, %d)\n",
1917 size_x, size_y, size_z,
1918 groups_x, groups_y, groups_z);
1919
1920 /* TODO: Decode */
1921 if (p->unknown_draw)
1922 pandecode_prop("unknown_draw = 0x%" PRIx32, p->unknown_draw);
1923
1924 pandecode_prop("workgroups_x_shift_3 = 0x%" PRIx32, p->workgroups_x_shift_3);
1925
1926 if (p->draw_mode != MALI_DRAW_MODE_NONE)
1927 pandecode_prop("draw_mode = %s", mali_draw_mode_as_str(p->draw_mode));
1928
1929 /* Index count only exists for tiler jobs anyway */
1930
1931 if (p->index_count)
1932 pandecode_prop("index_count = MALI_POSITIVE(%" PRId32 ")", p->index_count + 1);
1933
1934
1935 unsigned index_raw_size = (p->unknown_draw & MALI_DRAW_INDEXED_SIZE);
1936 index_raw_size >>= MALI_DRAW_INDEXED_SHIFT;
1937
1938 /* Validate an index buffer is present if we need one. TODO: verify
1939 * relationship between invocation_count and index_count */
1940
1941 if (p->indices) {
1942 unsigned count = p->index_count;
1943
1944 /* Grab the size */
1945 unsigned size = (index_raw_size == 0x3) ? 4 : index_raw_size;
1946
1947 /* Ensure we got a size, and if so, validate the index buffer
1948 * is large enough to hold a full set of indices of the given
1949 * size */
1950
1951 if (!index_raw_size)
1952 pandecode_msg("XXX: index size missing\n");
1953 else
1954 pandecode_validate_buffer(p->indices, count * size);
1955 } else if (index_raw_size)
1956 pandecode_msg("XXX: unexpected index size %u\n", index_raw_size);
1957
1958 if (p->offset_bias_correction)
1959 pandecode_prop("offset_bias_correction = %d", p->offset_bias_correction);
1960
1961 /* TODO: Figure out what this is. It's not zero */
1962 pandecode_prop("zero1 = 0x%" PRIx32, p->zero1);
1963
1964 pandecode_indent--;
1965 pandecode_log("},\n");
1966 }
1967
1968 static void
1969 pandecode_uniform_buffers(mali_ptr pubufs, int ubufs_count, int job_no)
1970 {
1971 struct pandecode_mapped_memory *umem = pandecode_find_mapped_gpu_mem_containing(pubufs);
1972 uint64_t *PANDECODE_PTR_VAR(ubufs, umem, pubufs);
1973
1974 for (int i = 0; i < ubufs_count; i++) {
1975 unsigned size = (ubufs[i] & ((1 << 10) - 1)) * 16;
1976 mali_ptr addr = (ubufs[i] >> 10) << 2;
1977
1978 pandecode_validate_buffer(addr, size);
1979
1980 char *ptr = pointer_as_memory_reference(addr);
1981 pandecode_log("ubuf_%d[%u] = %s;\n", i, size, ptr);
1982 free(ptr);
1983 }
1984
1985 pandecode_log("\n");
1986 }
1987
1988 static void
1989 pandecode_uniforms(mali_ptr uniforms, unsigned uniform_count)
1990 {
1991 pandecode_validate_buffer(uniforms, uniform_count * 16);
1992
1993 char *ptr = pointer_as_memory_reference(uniforms);
1994 pandecode_log("vec4 uniforms[%u] = %s;\n", uniform_count, ptr);
1995 free(ptr);
1996 }
1997
1998 static const char *
1999 shader_type_for_job(unsigned type)
2000 {
2001 switch (type) {
2002 case MALI_JOB_TYPE_VERTEX: return "VERTEX";
2003 case MALI_JOB_TYPE_TILER: return "FRAGMENT";
2004 case MALI_JOB_TYPE_COMPUTE: return "COMPUTE";
2005 default:
2006 return "UNKNOWN";
2007 }
2008 }
2009
2010 static unsigned shader_id = 0;
2011
2012 static struct midgard_disasm_stats
2013 pandecode_shader_disassemble(mali_ptr shader_ptr, int shader_no, int type,
2014 bool is_bifrost, unsigned gpu_id)
2015 {
2016 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(shader_ptr);
2017 uint8_t *PANDECODE_PTR_VAR(code, mem, shader_ptr);
2018
2019 /* Compute maximum possible size */
2020 size_t sz = mem->length - (shader_ptr - mem->gpu_va);
2021
2022 /* Print some boilerplate to clearly denote the assembly (which doesn't
2023 * obey indentation rules), and actually do the disassembly! */
2024
2025 pandecode_log_cont("\n\n");
2026
2027 struct midgard_disasm_stats stats;
2028
2029 if (is_bifrost) {
2030 disassemble_bifrost(pandecode_dump_stream, code, sz, true);
2031
2032 /* TODO: Extend stats to Bifrost */
2033 stats.texture_count = -128;
2034 stats.sampler_count = -128;
2035 stats.attribute_count = -128;
2036 stats.varying_count = -128;
2037 stats.uniform_count = -128;
2038 stats.uniform_buffer_count = -128;
2039 stats.work_count = -128;
2040
2041 stats.instruction_count = 0;
2042 stats.bundle_count = 0;
2043 stats.quadword_count = 0;
2044 stats.helper_invocations = false;
2045 } else {
2046 stats = disassemble_midgard(pandecode_dump_stream,
2047 code, sz, gpu_id,
2048 type == MALI_JOB_TYPE_TILER ?
2049 MESA_SHADER_FRAGMENT : MESA_SHADER_VERTEX);
2050 }
2051
2052 /* Print shader-db stats. Skip COMPUTE jobs since they are used for
2053 * driver-internal purposes with the blob and interfere */
2054
2055 bool should_shaderdb = type != MALI_JOB_TYPE_COMPUTE;
2056
2057 if (should_shaderdb) {
2058 unsigned nr_threads =
2059 (stats.work_count <= 4) ? 4 :
2060 (stats.work_count <= 8) ? 2 :
2061 1;
2062
2063 pandecode_log_cont("shader%d - MESA_SHADER_%s shader: "
2064 "%u inst, %u bundles, %u quadwords, "
2065 "%u registers, %u threads, 0 loops, 0:0 spills:fills\n\n\n",
2066 shader_id++,
2067 shader_type_for_job(type),
2068 stats.instruction_count, stats.bundle_count, stats.quadword_count,
2069 stats.work_count, nr_threads);
2070 }
2071
2072
2073 return stats;
2074 }
2075
2076 static void
2077 pandecode_texture_payload(mali_ptr payload,
2078 enum mali_texture_type type,
2079 enum mali_texture_layout layout,
2080 bool manual_stride,
2081 uint8_t levels,
2082 uint16_t depth,
2083 uint16_t array_size,
2084 struct pandecode_mapped_memory *tmem)
2085 {
2086 pandecode_log(".payload = {\n");
2087 pandecode_indent++;
2088
2089 /* A bunch of bitmap pointers follow.
2090 * We work out the correct number,
2091 * based on the mipmap/cubemap
2092 * properties, but dump extra
2093 * possibilities to futureproof */
2094
2095 int bitmap_count = levels + 1;
2096
2097 /* Miptree for each face */
2098 if (type == MALI_TEX_CUBE)
2099 bitmap_count *= 6;
2100
2101 /* Array of layers */
2102 bitmap_count *= (depth + 1);
2103
2104 /* Array of textures */
2105 bitmap_count *= (array_size + 1);
2106
2107 /* Stride for each element */
2108 if (manual_stride)
2109 bitmap_count *= 2;
2110
2111 mali_ptr *pointers_and_strides = pandecode_fetch_gpu_mem(tmem,
2112 payload, sizeof(mali_ptr) * bitmap_count);
2113 for (int i = 0; i < bitmap_count; ++i) {
2114 /* How we dump depends if this is a stride or a pointer */
2115
2116 if (manual_stride && (i & 1)) {
2117 /* signed 32-bit snuck in as a 64-bit pointer */
2118 uint64_t stride_set = pointers_and_strides[i];
2119 uint32_t clamped_stride = stride_set;
2120 int32_t stride = clamped_stride;
2121 assert(stride_set == clamped_stride);
2122 pandecode_log("(mali_ptr) %d /* stride */, \n", stride);
2123 } else {
2124 char *a = pointer_as_memory_reference(pointers_and_strides[i]);
2125 pandecode_log("%s, \n", a);
2126 free(a);
2127 }
2128 }
2129
2130 pandecode_indent--;
2131 pandecode_log("},\n");
2132 }
2133
2134 static void
2135 pandecode_texture(mali_ptr u,
2136 struct pandecode_mapped_memory *tmem,
2137 unsigned job_no, unsigned tex)
2138 {
2139 struct mali_texture_descriptor *PANDECODE_PTR_VAR(t, tmem, u);
2140
2141 pandecode_log("struct mali_texture_descriptor texture_descriptor_%"PRIx64"_%d_%d = {\n", u, job_no, tex);
2142 pandecode_indent++;
2143
2144 pandecode_prop("width = %" PRId32, t->width);
2145 pandecode_prop("height = %" PRId32, t->height);
2146 pandecode_prop("depth = %" PRId32, t->depth);
2147 pandecode_prop("array_size = %" PRId32, t->array_size);
2148
2149 pandecode_log("\n");
2150 pandecode_prop("f.swizzle = 0x%" PRIx32, t->format.swizzle);
2151 pandecode_prop("f.format = 0x%" PRIx32, t->format.format);
2152 pandecode_prop("f.srgb = 0x%" PRIx32, t->format.srgb);
2153 pandecode_prop("f.unknown1 = 0x%" PRIx32, t->format.unknown1);
2154 pandecode_prop("f.type = %" PRId32, t->format.type);
2155 pandecode_prop("f.layout = %" PRId32, t->format.layout);
2156 pandecode_prop("f.unknown2 = 0x%" PRIx32, t->format.unknown2);
2157 pandecode_prop("f.manual_stride = %" PRId32, t->format.manual_stride);
2158 pandecode_prop("f.zero = 0x%" PRIx32, t->format.zero);
2159 pandecode_log("\n");
2160
2161 pandecode_prop("unknown3 = 0x%" PRIx32, t->unknown3);
2162 pandecode_prop("unknown3A = 0x%" PRIx32, t->unknown3A);
2163 pandecode_prop("levels = %" PRId32, t->levels);
2164 pandecode_prop("swizzle = 0x%" PRIx32, t->swizzle);
2165 pandecode_prop("swizzle_zero = 0x%" PRIx32, t->swizzle_zero);
2166
2167 pandecode_prop("unknown5 = 0x%" PRIx32, t->unknown5);
2168 pandecode_prop("unknown6 = 0x%" PRIx32, t->unknown6);
2169 pandecode_prop("unknown7 = 0x%" PRIx32, t->unknown7);
2170 pandecode_log("\n");
2171
2172 struct mali_texture_format f = t->format;
2173
2174 /* See the definiton of enum mali_texture_type */
2175
2176 bool is_cube = f.type == MALI_TEX_CUBE;
2177 unsigned dimension = is_cube ? 2 : f.type;
2178
2179 pandecode_make_indent();
2180
2181 /* Print the layout. Default is linear; a modifier can denote AFBC or
2182 * u-interleaved/tiled modes */
2183
2184 if (f.layout == MALI_TEXTURE_AFBC)
2185 pandecode_log_cont("afbc");
2186 else if (f.layout == MALI_TEXTURE_TILED)
2187 pandecode_log_cont("tiled");
2188 else if (f.layout == MALI_TEXTURE_LINEAR)
2189 pandecode_log_cont("linear");
2190 else
2191 pandecode_msg("XXX: invalid texture layout 0x%X\n", f.layout);
2192
2193 pandecode_swizzle(t->swizzle, f.format);
2194 pandecode_log_cont(" ");
2195
2196 /* Distinguish cube/2D with modifier */
2197
2198 if (is_cube)
2199 pandecode_log_cont("cube ");
2200
2201 pandecode_format_short(f.format, f.srgb);
2202 pandecode_swizzle(f.swizzle, f.format);
2203
2204 /* All four width/height/depth/array_size dimensions are present
2205 * regardless of the type of texture, but it is an error to have
2206 * non-zero dimensions for unused dimensions. Verify this. array_size
2207 * can always be set, as can width. Depth used for MSAA. */
2208
2209 if (t->height && dimension < 2)
2210 pandecode_msg("XXX: nonzero height for <2D texture\n");
2211
2212 /* Print only the dimensions that are actually there */
2213
2214 pandecode_log_cont(": %d", t->width + 1);
2215
2216 if (t->height || t->depth)
2217 pandecode_log_cont("x%u", t->height + 1);
2218
2219 if (t->depth)
2220 pandecode_log_cont("x%u", t->depth + 1);
2221
2222 if (t->array_size)
2223 pandecode_log_cont("[%u]", t->array_size + 1);
2224
2225 if (t->levels)
2226 pandecode_log_cont(" mip %u", t->levels);
2227
2228 pandecode_log_cont("\n");
2229
2230 if (f.unknown1 | f.zero) {
2231 pandecode_msg("XXX: texture format zero tripped\n");
2232 pandecode_prop("unknown1 = %" PRId32, f.unknown1);
2233 pandecode_prop("zero = %" PRId32, f.zero);
2234 }
2235
2236 if (!f.unknown2) {
2237 pandecode_msg("XXX: expected unknown texture bit set\n");
2238 pandecode_prop("unknown2 = %" PRId32, f.unknown2);
2239 }
2240
2241 if (t->swizzle_zero) {
2242 pandecode_msg("XXX: swizzle zero tripped\n");
2243 pandecode_prop("swizzle_zero = %d", t->swizzle_zero);
2244 }
2245
2246 if (t->unknown3 | t->unknown3A | t->unknown5 | t->unknown6 | t->unknown7) {
2247 pandecode_msg("XXX: texture zero tripped\n");
2248 pandecode_prop("unknown3 = %" PRId16, t->unknown3);
2249 pandecode_prop("unknown3A = %" PRId8, t->unknown3A);
2250 pandecode_prop("unknown5 = 0x%" PRIx32, t->unknown5);
2251 pandecode_prop("unknown6 = 0x%" PRIx32, t->unknown6);
2252 pandecode_prop("unknown7 = 0x%" PRIx32, t->unknown7);
2253 }
2254
2255 pandecode_texture_payload(u + sizeof(*t), f.type, f.layout, f.manual_stride, t->levels, t->depth, t->array_size, tmem);
2256
2257 pandecode_indent--;
2258 pandecode_log("};\n");
2259 }
2260
2261 static void
2262 pandecode_bifrost_texture(
2263 const struct bifrost_texture_descriptor *t,
2264 unsigned job_no,
2265 unsigned tex)
2266 {
2267 pandecode_log("struct bifrost_texture_descriptor texture_descriptor_%d_%d = {\n", job_no, tex);
2268 pandecode_indent++;
2269
2270 pandecode_prop("format_unk = 0x%" PRIx32, t->format_unk);
2271 pandecode_prop("type = %" PRId32, t->type);
2272
2273 if (t->zero) {
2274 pandecode_msg("XXX: zero tripped\n");
2275 pandecode_prop("zero = 0x%" PRIx32, t->zero);
2276 }
2277
2278 pandecode_prop("format_swizzle = 0x%" PRIx32, t->format_swizzle);
2279 pandecode_prop("format = 0x%" PRIx32, t->format);
2280 pandecode_prop("srgb = 0x%" PRIx32, t->srgb);
2281 pandecode_prop("format_unk3 = 0x%" PRIx32, t->format_unk3);
2282 pandecode_prop("width = %" PRId32, t->width);
2283 pandecode_prop("height = %" PRId32, t->height);
2284 pandecode_prop("swizzle = 0x%" PRIx32, t->swizzle);
2285 pandecode_prop("levels = %" PRId32, t->levels);
2286 pandecode_prop("unk1 = 0x%" PRIx32, t->unk1);
2287 pandecode_prop("levels_unk = %" PRId32, t->levels_unk);
2288 pandecode_prop("level_2 = %" PRId32, t->level_2);
2289 pandecode_prop("payload = 0x%" PRIx64, t->payload);
2290 pandecode_prop("array_size = %" PRId32, t->array_size);
2291 pandecode_prop("unk4 = 0x%" PRIx32, t->unk4);
2292 pandecode_prop("depth = %" PRId32, t->depth);
2293 pandecode_prop("unk5 = 0x%" PRIx32, t->unk5);
2294 pandecode_log("\n");
2295
2296 /* See the definiton of enum mali_texture_type */
2297
2298 bool is_cube = t->type == MALI_TEX_CUBE;
2299 unsigned dimension = is_cube ? 2 : t->type;
2300
2301 /* Print the layout. Default is linear; a modifier can denote AFBC or
2302 * u-interleaved/tiled modes */
2303
2304 if (t->layout == MALI_TEXTURE_AFBC)
2305 pandecode_log_cont("afbc");
2306 else if (t->layout == MALI_TEXTURE_TILED)
2307 pandecode_log_cont("tiled");
2308 else if (t->layout == MALI_TEXTURE_LINEAR)
2309 pandecode_log_cont("linear");
2310 else
2311 pandecode_msg("XXX: invalid texture layout 0x%X\n", t->layout);
2312
2313 pandecode_swizzle(t->swizzle, t->format);
2314 pandecode_log_cont(" ");
2315
2316 /* Distinguish cube/2D with modifier */
2317
2318 if (is_cube)
2319 pandecode_log_cont("cube ");
2320
2321 pandecode_format_short(t->format, t->srgb);
2322
2323 /* All four width/height/depth/array_size dimensions are present
2324 * regardless of the type of texture, but it is an error to have
2325 * non-zero dimensions for unused dimensions. Verify this. array_size
2326 * can always be set, as can width. */
2327
2328 if (t->height && dimension < 2)
2329 pandecode_msg("XXX: nonzero height for <2D texture\n");
2330
2331 if (t->depth && dimension < 3)
2332 pandecode_msg("XXX: nonzero depth for <2D texture\n");
2333
2334 /* Print only the dimensions that are actually there */
2335
2336 pandecode_log_cont(": %d", t->width + 1);
2337
2338 if (dimension >= 2)
2339 pandecode_log_cont("x%u", t->height + 1);
2340
2341 if (dimension >= 3)
2342 pandecode_log_cont("x%u", t->depth + 1);
2343
2344 if (t->array_size)
2345 pandecode_log_cont("[%u]", t->array_size + 1);
2346
2347 if (t->levels)
2348 pandecode_log_cont(" mip %u", t->levels);
2349
2350 pandecode_log_cont("\n");
2351
2352 struct pandecode_mapped_memory *tmem = pandecode_find_mapped_gpu_mem_containing(t->payload);
2353 if (t->payload) {
2354 pandecode_texture_payload(t->payload, t->type, t->layout,
2355 true, t->levels, t->depth,
2356 t->array_size, tmem);
2357 }
2358
2359 pandecode_indent--;
2360 pandecode_log("};\n");
2361 }
2362
2363 /* For shader properties like texture_count, we have a claimed property in the shader_meta, and the actual Truth from static analysis (this may just be an upper limit). We validate accordingly */
2364
2365 static void
2366 pandecode_shader_prop(const char *name, unsigned claim, signed truth, bool fuzzy)
2367 {
2368 /* Nothing to do */
2369 if (claim == truth)
2370 return;
2371
2372 if (fuzzy && (truth < 0))
2373 pandecode_msg("XXX: fuzzy %s, claimed %d, expected %d\n", name, claim, truth);
2374
2375 if ((truth >= 0) && !fuzzy) {
2376 pandecode_msg("%s: expected %s = %d, claimed %u\n",
2377 (truth < claim) ? "warn" : "XXX",
2378 name, truth, claim);
2379 } else if ((claim > -truth) && !fuzzy) {
2380 pandecode_msg("XXX: expected %s <= %u, claimed %u\n",
2381 name, -truth, claim);
2382 } else if (fuzzy && (claim < truth))
2383 pandecode_msg("XXX: expected %s >= %u, claimed %u\n",
2384 name, truth, claim);
2385
2386 pandecode_log(".%s = %" PRId16, name, claim);
2387
2388 if (fuzzy)
2389 pandecode_log_cont(" /* %u used */", truth);
2390
2391 pandecode_log_cont(",\n");
2392 }
2393
2394 static void
2395 pandecode_blend_shader_disassemble(mali_ptr shader, int job_no, int job_type,
2396 bool is_bifrost, unsigned gpu_id)
2397 {
2398 struct midgard_disasm_stats stats =
2399 pandecode_shader_disassemble(shader, job_no, job_type, is_bifrost, gpu_id);
2400
2401 bool has_texture = (stats.texture_count > 0);
2402 bool has_sampler = (stats.sampler_count > 0);
2403 bool has_attribute = (stats.attribute_count > 0);
2404 bool has_varying = (stats.varying_count > 0);
2405 bool has_uniform = (stats.uniform_count > 0);
2406 bool has_ubo = (stats.uniform_buffer_count > 0);
2407
2408 if (has_texture || has_sampler)
2409 pandecode_msg("XXX: blend shader accessing textures\n");
2410
2411 if (has_attribute || has_varying)
2412 pandecode_msg("XXX: blend shader accessing interstage\n");
2413
2414 if (has_uniform || has_ubo)
2415 pandecode_msg("XXX: blend shader accessing uniforms\n");
2416 }
2417
2418 static void
2419 pandecode_textures(mali_ptr textures, unsigned texture_count, int job_no, bool is_bifrost)
2420 {
2421 struct pandecode_mapped_memory *mmem = pandecode_find_mapped_gpu_mem_containing(textures);
2422
2423 if (!mmem)
2424 return;
2425
2426 if (is_bifrost) {
2427 const struct bifrost_texture_descriptor *PANDECODE_PTR_VAR(t, mmem, textures);
2428
2429 pandecode_log("uint64_t textures_%"PRIx64"_%d[] = {\n", textures, job_no);
2430 pandecode_indent++;
2431
2432 for (unsigned tex = 0; tex < texture_count; ++tex)
2433 pandecode_bifrost_texture(&t[tex], job_no, tex);
2434
2435 pandecode_indent--;
2436 pandecode_log("};\n");
2437 } else {
2438 mali_ptr *PANDECODE_PTR_VAR(u, mmem, textures);
2439
2440 pandecode_log("uint64_t textures_%"PRIx64"_%d[] = {\n", textures, job_no);
2441 pandecode_indent++;
2442
2443 for (int tex = 0; tex < texture_count; ++tex) {
2444 mali_ptr *PANDECODE_PTR_VAR(u, mmem, textures + tex * sizeof(mali_ptr));
2445 char *a = pointer_as_memory_reference(*u);
2446 pandecode_log("%s,\n", a);
2447 free(a);
2448 }
2449
2450 pandecode_indent--;
2451 pandecode_log("};\n");
2452
2453 /* Now, finally, descend down into the texture descriptor */
2454 for (unsigned tex = 0; tex < texture_count; ++tex) {
2455 mali_ptr *PANDECODE_PTR_VAR(u, mmem, textures + tex * sizeof(mali_ptr));
2456 struct pandecode_mapped_memory *tmem = pandecode_find_mapped_gpu_mem_containing(*u);
2457 if (tmem)
2458 pandecode_texture(*u, tmem, job_no, tex);
2459 }
2460 }
2461 }
2462
2463 static void
2464 pandecode_samplers(mali_ptr samplers, unsigned sampler_count, int job_no, bool is_bifrost)
2465 {
2466 struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(samplers);
2467
2468 if (!smem)
2469 return;
2470
2471 if (is_bifrost) {
2472 struct bifrost_sampler_descriptor *s;
2473
2474 for (int i = 0; i < sampler_count; ++i) {
2475 s = pandecode_fetch_gpu_mem(smem, samplers + sizeof(*s) * i, sizeof(*s));
2476
2477 pandecode_log("struct bifrost_sampler_descriptor sampler_descriptor_%"PRIx64"_%d_%d = {\n", samplers + sizeof(*s) * i, job_no, i);
2478 pandecode_indent++;
2479
2480 if (s->unk1 != 1) {
2481 pandecode_msg("XXX: unk1 tripped\n");
2482 pandecode_prop("unk1 = 0x%x", s->unk1);
2483 }
2484
2485 pandecode_prop("wrap_s = %s", mali_wrap_mode_as_str(s->wrap_s));
2486 pandecode_prop("wrap_t = %s", mali_wrap_mode_as_str(s->wrap_t));
2487 pandecode_prop("wrap_r = %s", mali_wrap_mode_as_str(s->wrap_r));
2488
2489 if (s->unk8 != 0x8) {
2490 pandecode_msg("XXX: unk8 tripped\n");
2491 pandecode_prop("unk8 = 0x%x", s->unk8);
2492 }
2493
2494 pandecode_prop("unk2 = 0x%x", s->unk2);
2495 pandecode_prop("unk3 = 0x%x", s->unk3);
2496 pandecode_prop("min_filter = %s", s->min_filter ? "nearest" : "linear");
2497 pandecode_prop("norm_coords = 0x%x", s->norm_coords & 0x1);
2498 pandecode_prop("zero1 = 0x%x", s->zero1 & 0x1);
2499 pandecode_prop("mip_filter = %s", s->mip_filter ? "linear" : "nearest");
2500 pandecode_prop("mag_filter = %s", s->mag_filter ? "linear" : "nearest");
2501
2502 pandecode_prop("min_lod = FIXED_16(%f)", DECODE_FIXED_16(s->min_lod));
2503 pandecode_prop("max_lod = FIXED_16(%f)", DECODE_FIXED_16(s->max_lod));
2504
2505 if (s->zero1 || s->zero2 || s->zero3 || s->zero4) {
2506 pandecode_msg("XXX: sampler zero tripped\n");
2507 pandecode_prop("zero = 0x%" PRIx8 ", 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 "\n", s->zero1, s->zero2, s->zero3, s->zero4);
2508 }
2509
2510 pandecode_indent--;
2511 pandecode_log("};\n");
2512 }
2513 } else {
2514 struct mali_sampler_descriptor *s;
2515
2516 for (int i = 0; i < sampler_count; ++i) {
2517 s = pandecode_fetch_gpu_mem(smem, samplers + sizeof(*s) * i, sizeof(*s));
2518
2519 pandecode_log("struct mali_sampler_descriptor sampler_descriptor_%"PRIx64"_%d_%d = {\n", samplers + sizeof(*s) * i, job_no, i);
2520 pandecode_indent++;
2521
2522 pandecode_log(".filter_mode = ");
2523 pandecode_log_decoded_flags(sampler_flag_info, s->filter_mode);
2524 pandecode_log_cont(",\n");
2525
2526 pandecode_prop("min_lod = FIXED_16(%f)", DECODE_FIXED_16(s->min_lod));
2527 pandecode_prop("max_lod = FIXED_16(%f)", DECODE_FIXED_16(s->max_lod));
2528
2529 if (s->lod_bias)
2530 pandecode_prop("lod_bias = FIXED_16(%f)", DECODE_FIXED_16(s->lod_bias));
2531
2532 pandecode_prop("wrap_s = %s", mali_wrap_mode_as_str(s->wrap_s));
2533 pandecode_prop("wrap_t = %s", mali_wrap_mode_as_str(s->wrap_t));
2534 pandecode_prop("wrap_r = %s", mali_wrap_mode_as_str(s->wrap_r));
2535
2536 pandecode_prop("compare_func = %s", mali_func_as_str(s->compare_func));
2537
2538 if (s->zero || s->zero2) {
2539 pandecode_msg("XXX: sampler zero tripped\n");
2540 pandecode_prop("zero = 0x%X, 0x%X\n", s->zero, s->zero2);
2541 }
2542
2543 pandecode_prop("seamless_cube_map = %d", s->seamless_cube_map);
2544
2545 pandecode_prop("border_color = { %f, %f, %f, %f }",
2546 s->border_color[0],
2547 s->border_color[1],
2548 s->border_color[2],
2549 s->border_color[3]);
2550
2551 pandecode_indent--;
2552 pandecode_log("};\n");
2553 }
2554 }
2555 }
2556
2557 static void
2558 pandecode_vertex_tiler_postfix_pre(
2559 const struct mali_vertex_tiler_postfix *p,
2560 int job_no, enum mali_job_type job_type,
2561 char *suffix, bool is_bifrost, unsigned gpu_id)
2562 {
2563 struct pandecode_mapped_memory *attr_mem;
2564
2565 /* On Bifrost, since the tiler heap (for tiler jobs) and the scratchpad
2566 * are the only things actually needed from the FBD, vertex/tiler jobs
2567 * no longer reference the FBD -- instead, this field points to some
2568 * info about the scratchpad.
2569 */
2570
2571 struct pandecode_fbd fbd_info = {
2572 /* Default for Bifrost */
2573 .rt_count = 1
2574 };
2575
2576 if (is_bifrost) {
2577 pandecode_log_cont("\t/* %X %/\n", p->shared_memory & 1);
2578 pandecode_compute_fbd(p->shared_memory & ~1, job_no);
2579 } else if (p->shared_memory & MALI_MFBD)
2580 fbd_info = pandecode_mfbd_bfr((u64) ((uintptr_t) p->shared_memory) & FBD_MASK, job_no, false, job_type == MALI_JOB_TYPE_COMPUTE, false);
2581 else if (job_type == MALI_JOB_TYPE_COMPUTE)
2582 pandecode_compute_fbd((u64) (uintptr_t) p->shared_memory, job_no);
2583 else
2584 fbd_info = pandecode_sfbd((u64) (uintptr_t) p->shared_memory, job_no, false, gpu_id);
2585
2586 int varying_count = 0, attribute_count = 0, uniform_count = 0, uniform_buffer_count = 0;
2587 int texture_count = 0, sampler_count = 0;
2588
2589 if (p->shader) {
2590 struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(p->shader);
2591 struct mali_shader_meta *PANDECODE_PTR_VAR(s, smem, p->shader);
2592
2593 /* Disassemble ahead-of-time to get stats. Initialize with
2594 * stats for the missing-shader case so we get validation
2595 * there, too */
2596
2597 struct midgard_disasm_stats info = {
2598 .texture_count = 0,
2599 .sampler_count = 0,
2600 .attribute_count = 0,
2601 .varying_count = 0,
2602 .work_count = 1,
2603
2604 .uniform_count = -128,
2605 .uniform_buffer_count = 0
2606 };
2607
2608 if (s->shader & ~0xF)
2609 info = pandecode_shader_disassemble(s->shader & ~0xF, job_no, job_type, is_bifrost, gpu_id);
2610
2611 pandecode_log("struct mali_shader_meta shader_meta_%"PRIx64"_%d%s = {\n", p->shader, job_no, suffix);
2612 pandecode_indent++;
2613
2614 /* Save for dumps */
2615 attribute_count = s->attribute_count;
2616 varying_count = s->varying_count;
2617 texture_count = s->texture_count;
2618 sampler_count = s->sampler_count;
2619
2620 if (is_bifrost) {
2621 uniform_count = s->bifrost2.uniform_count;
2622 uniform_buffer_count = s->bifrost1.uniform_buffer_count;
2623 } else {
2624 uniform_count = s->midgard1.uniform_count;
2625 uniform_buffer_count = s->midgard1.uniform_buffer_count;
2626 }
2627
2628 pandecode_shader_address("shader", s->shader);
2629
2630 pandecode_shader_prop("texture_count", s->texture_count, info.texture_count, false);
2631 pandecode_shader_prop("sampler_count", s->sampler_count, info.sampler_count, false);
2632 pandecode_shader_prop("attribute_count", s->attribute_count, info.attribute_count, false);
2633 pandecode_shader_prop("varying_count", s->varying_count, info.varying_count, false);
2634 pandecode_shader_prop("uniform_buffer_count",
2635 uniform_buffer_count,
2636 info.uniform_buffer_count, true);
2637
2638 if (!is_bifrost) {
2639 pandecode_shader_prop("uniform_count",
2640 uniform_count,
2641 info.uniform_count, false);
2642
2643 pandecode_shader_prop("work_count",
2644 s->midgard1.work_count, info.work_count, false);
2645 }
2646
2647 if (is_bifrost) {
2648 pandecode_log("bifrost1.unk1 = ");
2649 pandecode_log_decoded_flags(shader_bifrost_info, s->bifrost1.unk1);
2650 pandecode_log_cont(",\n");
2651 } else {
2652 bool helpers = s->midgard1.flags_lo & MALI_HELPER_INVOCATIONS;
2653
2654 if (helpers != info.helper_invocations) {
2655 pandecode_msg("XXX: expected helpers %u but got %u\n",
2656 info.helper_invocations, helpers);
2657 }
2658
2659 pandecode_log(".midgard1.flags_lo = ");
2660 pandecode_log_decoded_flags(shader_midgard1_flag_lo_info,
2661 s->midgard1.flags_lo & ~MALI_HELPER_INVOCATIONS);
2662 pandecode_log_cont(",\n");
2663
2664 pandecode_log(".midgard1.flags_hi = ");
2665 pandecode_log_decoded_flags(shader_midgard1_flag_hi_info, s->midgard1.flags_hi);
2666 pandecode_log_cont(",\n");
2667 }
2668
2669 if (s->depth_units || s->depth_factor) {
2670 pandecode_prop("depth_factor = %f", s->depth_factor);
2671 pandecode_prop("depth_units = %f", s->depth_units);
2672 }
2673
2674 if (s->coverage_mask)
2675 pandecode_prop("coverage_mask = 0x%X", s->coverage_mask);
2676
2677 if (s->unknown2_2)
2678 pandecode_prop(".unknown2_2 = %X", s->unknown2_2);
2679
2680 if (s->unknown2_3 || s->unknown2_4) {
2681 pandecode_log(".unknown2_3 = ");
2682
2683 int unknown2_3 = s->unknown2_3;
2684 int unknown2_4 = s->unknown2_4;
2685
2686 /* We're not quite sure what these flags mean without the depth test, if anything */
2687
2688 if (unknown2_3 & (MALI_DEPTH_WRITEMASK | MALI_DEPTH_FUNC_MASK)) {
2689 const char *func = mali_func_as_str(MALI_GET_DEPTH_FUNC(unknown2_3));
2690 unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
2691
2692 pandecode_log_cont("MALI_DEPTH_FUNC(%s) | ", func);
2693 }
2694
2695 pandecode_log_decoded_flags(u3_flag_info, unknown2_3);
2696 pandecode_log_cont(",\n");
2697
2698 pandecode_log(".unknown2_4 = ");
2699 pandecode_log_decoded_flags(u4_flag_info, unknown2_4);
2700 pandecode_log_cont(",\n");
2701 }
2702
2703 if (s->stencil_mask_front || s->stencil_mask_back) {
2704 pandecode_prop("stencil_mask_front = 0x%02X", s->stencil_mask_front);
2705 pandecode_prop("stencil_mask_back = 0x%02X", s->stencil_mask_back);
2706 }
2707
2708 pandecode_stencil("front", &s->stencil_front);
2709 pandecode_stencil("back", &s->stencil_back);
2710
2711 if (is_bifrost) {
2712 pandecode_log(".bifrost2 = {\n");
2713 pandecode_indent++;
2714
2715 pandecode_prop("unk3 = 0x%" PRIx32, s->bifrost2.unk3);
2716 pandecode_prop("preload_regs = 0x%" PRIx32, s->bifrost2.preload_regs);
2717 pandecode_prop("uniform_count = %" PRId32, s->bifrost2.uniform_count);
2718 pandecode_prop("unk4 = 0x%" PRIx32, s->bifrost2.unk4);
2719
2720 pandecode_indent--;
2721 pandecode_log("},\n");
2722 } else if (s->midgard2.unknown2_7) {
2723 pandecode_log(".midgard2 = {\n");
2724 pandecode_indent++;
2725
2726 pandecode_prop("unknown2_7 = 0x%" PRIx32, s->midgard2.unknown2_7);
2727 pandecode_indent--;
2728 pandecode_log("},\n");
2729 }
2730
2731 if (s->padding) {
2732 pandecode_msg("XXX: shader padding tripped\n");
2733 pandecode_prop("padding = 0x%" PRIx32, s->padding);
2734 }
2735
2736 if (!is_bifrost) {
2737 /* TODO: Blend shaders routing/disasm */
2738 union midgard_blend blend = s->blend;
2739 mali_ptr shader = pandecode_midgard_blend(&blend, s->unknown2_3 & MALI_HAS_BLEND_SHADER);
2740 if (shader & ~0xF)
2741 pandecode_blend_shader_disassemble(shader, job_no, job_type, false, gpu_id);
2742 } else {
2743 pandecode_msg("mdg_blend = %" PRIx64 "\n", s->blend.shader);
2744 }
2745
2746 pandecode_indent--;
2747 pandecode_log("};\n");
2748
2749 /* MRT blend fields are used whenever MFBD is used, with
2750 * per-RT descriptors */
2751
2752 if (job_type == MALI_JOB_TYPE_TILER && (is_bifrost || p->shared_memory & MALI_MFBD)) {
2753 void* blend_base = (void *) (s + 1);
2754
2755 for (unsigned i = 0; i < fbd_info.rt_count; i++) {
2756 mali_ptr shader = 0;
2757
2758 if (is_bifrost)
2759 shader = pandecode_bifrost_blend(blend_base, job_no, i);
2760 else
2761 shader = pandecode_midgard_blend_mrt(blend_base, job_no, i);
2762
2763 if (shader & ~0xF)
2764 pandecode_blend_shader_disassemble(shader, job_no, job_type, false, gpu_id);
2765
2766 }
2767 }
2768 } else
2769 pandecode_msg("XXX: missing shader descriptor\n");
2770
2771 if (p->viewport) {
2772 struct pandecode_mapped_memory *fmem = pandecode_find_mapped_gpu_mem_containing(p->viewport);
2773 struct mali_viewport *PANDECODE_PTR_VAR(f, fmem, p->viewport);
2774
2775 pandecode_log("struct mali_viewport viewport_%"PRIx64"_%d%s = {\n", p->viewport, job_no, suffix);
2776 pandecode_indent++;
2777
2778 pandecode_prop("clip_minx = %f", f->clip_minx);
2779 pandecode_prop("clip_miny = %f", f->clip_miny);
2780 pandecode_prop("clip_minz = %f", f->clip_minz);
2781 pandecode_prop("clip_maxx = %f", f->clip_maxx);
2782 pandecode_prop("clip_maxy = %f", f->clip_maxy);
2783 pandecode_prop("clip_maxz = %f", f->clip_maxz);
2784
2785 /* Only the higher coordinates are MALI_POSITIVE scaled */
2786
2787 pandecode_prop("viewport0 = { %d, %d }",
2788 f->viewport0[0], f->viewport0[1]);
2789
2790 pandecode_prop("viewport1 = { MALI_POSITIVE(%d), MALI_POSITIVE(%d) }",
2791 f->viewport1[0] + 1, f->viewport1[1] + 1);
2792
2793 pandecode_indent--;
2794 pandecode_log("};\n");
2795 }
2796
2797 unsigned max_attr_index = 0;
2798
2799 if (p->attribute_meta)
2800 max_attr_index = pandecode_attribute_meta(job_no, attribute_count, p, false, suffix);
2801
2802 if (p->attributes) {
2803 attr_mem = pandecode_find_mapped_gpu_mem_containing(p->attributes);
2804 pandecode_attributes(attr_mem, p->attributes, job_no, suffix, max_attr_index, false, job_type);
2805 }
2806
2807 /* Varyings are encoded like attributes but not actually sent; we just
2808 * pass a zero buffer with the right stride/size set, (or whatever)
2809 * since the GPU will write to it itself */
2810
2811 if (p->varying_meta) {
2812 varying_count = pandecode_attribute_meta(job_no, varying_count, p, true, suffix);
2813 }
2814
2815 if (p->varyings) {
2816 attr_mem = pandecode_find_mapped_gpu_mem_containing(p->varyings);
2817
2818 /* Number of descriptors depends on whether there are
2819 * non-internal varyings */
2820
2821 pandecode_attributes(attr_mem, p->varyings, job_no, suffix, varying_count, true, job_type);
2822 }
2823
2824 if (p->uniform_buffers) {
2825 if (uniform_buffer_count)
2826 pandecode_uniform_buffers(p->uniform_buffers, uniform_buffer_count, job_no);
2827 else
2828 pandecode_msg("warn: UBOs specified but not referenced\n");
2829 } else if (uniform_buffer_count)
2830 pandecode_msg("XXX: UBOs referenced but not specified\n");
2831
2832 /* We don't want to actually dump uniforms, but we do need to validate
2833 * that the counts we were given are sane */
2834
2835 if (p->uniforms) {
2836 if (uniform_count)
2837 pandecode_uniforms(p->uniforms, uniform_count);
2838 else
2839 pandecode_msg("warn: Uniforms specified but not referenced\n");
2840 } else if (uniform_count)
2841 pandecode_msg("XXX: Uniforms referenced but not specified\n");
2842
2843 if (p->textures)
2844 pandecode_textures(p->textures, texture_count, job_no, is_bifrost);
2845
2846 if (p->sampler_descriptor)
2847 pandecode_samplers(p->sampler_descriptor, sampler_count, job_no, is_bifrost);
2848 }
2849
2850 static void
2851 pandecode_gl_enables(uint32_t gl_enables, int job_type)
2852 {
2853 pandecode_log(".gl_enables = ");
2854
2855 pandecode_log_decoded_flags(gl_enable_flag_info, gl_enables);
2856
2857 pandecode_log_cont(",\n");
2858 }
2859
2860 static void
2861 pandecode_vertex_tiler_postfix(const struct mali_vertex_tiler_postfix *p, int job_no, bool is_bifrost)
2862 {
2863 if (p->shader & 0xF)
2864 pandecode_msg("warn: shader tagged %X\n", (unsigned) (p->shader & 0xF));
2865
2866 pandecode_log(".postfix = {\n");
2867 pandecode_indent++;
2868
2869 pandecode_gl_enables(p->gl_enables, MALI_JOB_TYPE_TILER);
2870 pandecode_prop("instance_shift = 0x%x", p->instance_shift);
2871 pandecode_prop("instance_odd = 0x%x", p->instance_odd);
2872
2873 if (p->zero4) {
2874 pandecode_msg("XXX: vertex only zero tripped");
2875 pandecode_prop("zero4 = 0x%" PRIx32, p->zero4);
2876 }
2877
2878 pandecode_prop("offset_start = 0x%x", p->offset_start);
2879
2880 if (p->zero5) {
2881 pandecode_msg("XXX: vertex only zero tripped");
2882 pandecode_prop("zero5 = 0x%" PRIx64, p->zero5);
2883 }
2884
2885 MEMORY_PROP(p, position_varying);
2886 MEMORY_PROP(p, occlusion_counter);
2887
2888 pandecode_indent--;
2889 pandecode_log("},\n");
2890 }
2891
2892 static void
2893 pandecode_tiler_heap_meta(mali_ptr gpu_va, int job_no)
2894 {
2895 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
2896 const struct bifrost_tiler_heap_meta *PANDECODE_PTR_VAR(h, mem, gpu_va);
2897
2898 pandecode_log("struct bifrost_tiler_heap_meta tiler_heap_meta_%"PRIx64"_%d = {\n", gpu_va, job_no);
2899 pandecode_indent++;
2900
2901 if (h->zero) {
2902 pandecode_msg("XXX: tiler heap zero tripped\n");
2903 pandecode_prop("zero = 0x%x", h->zero);
2904 }
2905
2906 pandecode_prop("heap_size = 0x%x", h->heap_size);
2907 MEMORY_PROP(h, tiler_heap_start);
2908 MEMORY_PROP(h, tiler_heap_free);
2909
2910 /* this might point to the beginning of another buffer, when it's
2911 * really the end of the tiler heap buffer, so we have to be careful
2912 * here. but for zero length, we need the same pointer.
2913 */
2914
2915 if (h->tiler_heap_end == h->tiler_heap_start) {
2916 MEMORY_PROP(h, tiler_heap_start);
2917 } else {
2918 char *a = pointer_as_memory_reference(h->tiler_heap_end - 1);
2919 pandecode_prop("tiler_heap_end = %s + 1", a);
2920 free(a);
2921 }
2922
2923 for (int i = 0; i < 10; i++) {
2924 if (h->zeros[i] != 0) {
2925 pandecode_msg("XXX: tiler heap zero %d tripped, value %x\n",
2926 i, h->zeros[i]);
2927 }
2928 }
2929
2930 if (h->unk1 != 0x1) {
2931 pandecode_msg("XXX: tiler heap unk1 tripped\n");
2932 pandecode_prop("unk1 = 0x%x", h->unk1);
2933 }
2934
2935 if (h->unk7e007e != 0x7e007e) {
2936 pandecode_msg("XXX: tiler heap unk7e007e tripped\n");
2937 pandecode_prop("unk7e007e = 0x%x", h->unk7e007e);
2938 }
2939
2940 pandecode_indent--;
2941 pandecode_log("};\n");
2942 }
2943
2944 static void
2945 pandecode_tiler_meta(mali_ptr gpu_va, int job_no)
2946 {
2947 struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
2948 const struct bifrost_tiler_meta *PANDECODE_PTR_VAR(t, mem, gpu_va);
2949
2950 pandecode_tiler_heap_meta(t->tiler_heap_meta, job_no);
2951
2952 pandecode_log("struct bifrost_tiler_meta tiler_meta_%"PRIx64"_%d = {\n", gpu_va, job_no);
2953 pandecode_indent++;
2954
2955 pandecode_prop("tiler_heap_next_start = 0x%" PRIx32, t->tiler_heap_next_start);
2956 pandecode_prop("used_hierarchy_mask = 0x%" PRIx32, t->used_hierarchy_mask);
2957
2958 if (t->hierarchy_mask != 0xa &&
2959 t->hierarchy_mask != 0x14 &&
2960 t->hierarchy_mask != 0x28 &&
2961 t->hierarchy_mask != 0x50 &&
2962 t->hierarchy_mask != 0xa0)
2963 pandecode_prop("XXX: Unexpected hierarchy_mask (not 0xa, 0x14, 0x28, 0x50 or 0xa0)!");
2964
2965 pandecode_prop("hierarchy_mask = 0x%" PRIx16, t->hierarchy_mask);
2966
2967 pandecode_prop("flags = 0x%" PRIx16, t->flags);
2968
2969 pandecode_prop("width = MALI_POSITIVE(%d)", t->width + 1);
2970 pandecode_prop("height = MALI_POSITIVE(%d)", t->height + 1);
2971
2972 if (t->zero0) {
2973 pandecode_msg("XXX: tiler meta zero tripped\n");
2974 pandecode_prop("zero0 = 0x%" PRIx64, t->zero0);
2975 }
2976
2977 for (int i = 0; i < 12; i++) {
2978 if (t->zeros[i] != 0) {
2979 pandecode_msg("XXX: tiler heap zero %d tripped, value %" PRIx64 "\n",
2980 i, t->zeros[i]);
2981 }
2982 }
2983
2984 pandecode_indent--;
2985 pandecode_log("};\n");
2986 }
2987
2988 static void
2989 pandecode_primitive_size(union midgard_primitive_size u, bool constant)
2990 {
2991 if (u.pointer == 0x0)
2992 return;
2993
2994 pandecode_log(".primitive_size = {\n");
2995 pandecode_indent++;
2996
2997 if (constant) {
2998 pandecode_prop("constant = %f", u.constant);
2999 } else {
3000 MEMORY_PROP((&u), pointer);
3001 }
3002
3003 pandecode_indent--;
3004 pandecode_log("},\n");
3005 }
3006
3007 static void
3008 pandecode_tiler_only_bfr(const struct bifrost_tiler_only *t, int job_no)
3009 {
3010 pandecode_log_cont("{\n");
3011 pandecode_indent++;
3012
3013 /* TODO: gl_PointSize on Bifrost */
3014 pandecode_primitive_size(t->primitive_size, true);
3015
3016 if (t->zero1 || t->zero2 || t->zero3 || t->zero4 || t->zero5
3017 || t->zero6) {
3018 pandecode_msg("XXX: tiler only zero tripped\n");
3019 pandecode_prop("zero1 = 0x%" PRIx64, t->zero1);
3020 pandecode_prop("zero2 = 0x%" PRIx64, t->zero2);
3021 pandecode_prop("zero3 = 0x%" PRIx64, t->zero3);
3022 pandecode_prop("zero4 = 0x%" PRIx64, t->zero4);
3023 pandecode_prop("zero5 = 0x%" PRIx64, t->zero5);
3024 pandecode_prop("zero6 = 0x%" PRIx64, t->zero6);
3025 }
3026
3027 pandecode_indent--;
3028 pandecode_log("},\n");
3029 }
3030
3031 static int
3032 pandecode_vertex_job_bfr(const struct mali_job_descriptor_header *h,
3033 const struct pandecode_mapped_memory *mem,
3034 mali_ptr payload, int job_no, unsigned gpu_id)
3035 {
3036 struct bifrost_payload_vertex *PANDECODE_PTR_VAR(v, mem, payload);
3037
3038 pandecode_vertex_tiler_postfix_pre(&v->postfix, job_no, h->job_type, "", true, gpu_id);
3039
3040 pandecode_log("struct bifrost_payload_vertex payload_%"PRIx64"_%d = {\n", payload, job_no);
3041 pandecode_indent++;
3042
3043 pandecode_vertex_tiler_prefix(&v->prefix, job_no, false);
3044 pandecode_vertex_tiler_postfix(&v->postfix, job_no, true);
3045
3046 pandecode_indent--;
3047 pandecode_log("};\n");
3048
3049 return sizeof(*v);
3050 }
3051
3052 static int
3053 pandecode_tiler_job_bfr(const struct mali_job_descriptor_header *h,
3054 const struct pandecode_mapped_memory *mem,
3055 mali_ptr payload, int job_no, unsigned gpu_id)
3056 {
3057 struct bifrost_payload_tiler *PANDECODE_PTR_VAR(t, mem, payload);
3058
3059 pandecode_vertex_tiler_postfix_pre(&t->postfix, job_no, h->job_type, "", true, gpu_id);
3060 pandecode_tiler_meta(t->tiler.tiler_meta, job_no);
3061
3062 pandecode_log("struct bifrost_payload_tiler payload_%"PRIx64"_%d = {\n", payload, job_no);
3063 pandecode_indent++;
3064
3065 pandecode_vertex_tiler_prefix(&t->prefix, job_no, false);
3066
3067 pandecode_log(".tiler = ");
3068 pandecode_tiler_only_bfr(&t->tiler, job_no);
3069
3070 pandecode_vertex_tiler_postfix(&t->postfix, job_no, true);
3071
3072 pandecode_indent--;
3073 pandecode_log("};\n");
3074
3075 return sizeof(*t);
3076 }
3077
3078 static int
3079 pandecode_vertex_or_tiler_job_mdg(const struct mali_job_descriptor_header *h,
3080 const struct pandecode_mapped_memory *mem,
3081 mali_ptr payload, int job_no, unsigned gpu_id)
3082 {
3083 struct midgard_payload_vertex_tiler *PANDECODE_PTR_VAR(v, mem, payload);
3084 bool is_graphics = (h->job_type == MALI_JOB_TYPE_VERTEX) || (h->job_type == MALI_JOB_TYPE_TILER);
3085
3086 pandecode_vertex_tiler_postfix_pre(&v->postfix, job_no, h->job_type, "", false, gpu_id);
3087
3088 pandecode_log("struct midgard_payload_vertex_tiler payload_%d = {\n", job_no);
3089 pandecode_indent++;
3090
3091 pandecode_vertex_tiler_prefix(&v->prefix, job_no, is_graphics);
3092 pandecode_vertex_tiler_postfix(&v->postfix, job_no, false);
3093
3094 bool has_primitive_pointer = v->prefix.unknown_draw & MALI_DRAW_VARYING_SIZE;
3095 pandecode_primitive_size(v->primitive_size, !has_primitive_pointer);
3096
3097 pandecode_indent--;
3098 pandecode_log("};\n");
3099
3100 return sizeof(*v);
3101 }
3102
3103 static int
3104 pandecode_fragment_job(const struct pandecode_mapped_memory *mem,
3105 mali_ptr payload, int job_no,
3106 bool is_bifrost, unsigned gpu_id)
3107 {
3108 const struct mali_payload_fragment *PANDECODE_PTR_VAR(s, mem, payload);
3109
3110 bool is_mfbd = s->framebuffer & MALI_MFBD;
3111
3112 if (!is_mfbd && is_bifrost)
3113 pandecode_msg("XXX: Bifrost fragment must use MFBD\n");
3114
3115 struct pandecode_fbd info;
3116
3117 if (is_mfbd)
3118 info = pandecode_mfbd_bfr(s->framebuffer & FBD_MASK, job_no, true, false, is_bifrost);
3119 else
3120 info = pandecode_sfbd(s->framebuffer & FBD_MASK, job_no, true, gpu_id);
3121
3122 /* Compute the tag for the tagged pointer. This contains the type of
3123 * FBD (MFBD/SFBD), and in the case of an MFBD, information about which
3124 * additional structures follow the MFBD header (an extra payload or
3125 * not, as well as a count of render targets) */
3126
3127 unsigned expected_tag = is_mfbd ? MALI_MFBD : 0;
3128
3129 if (is_mfbd) {
3130 if (info.has_extra)
3131 expected_tag |= MALI_MFBD_TAG_EXTRA;
3132
3133 expected_tag |= (MALI_POSITIVE(info.rt_count) << 2);
3134 }
3135
3136 if ((s->min_tile_coord | s->max_tile_coord) & ~(MALI_X_COORD_MASK | MALI_Y_COORD_MASK)) {
3137 pandecode_msg("XXX: unexpected tile coordinate bits\n");
3138 pandecode_prop("min_tile_coord = 0x%X\n", s->min_tile_coord);
3139 pandecode_prop("max_tile_coord = 0x%X\n", s->max_tile_coord);
3140 }
3141
3142 /* Extract tile coordinates */
3143
3144 unsigned min_x = MALI_TILE_COORD_X(s->min_tile_coord) << MALI_TILE_SHIFT;
3145 unsigned min_y = MALI_TILE_COORD_Y(s->min_tile_coord) << MALI_TILE_SHIFT;
3146
3147 unsigned max_x = (MALI_TILE_COORD_X(s->max_tile_coord) + 1) << MALI_TILE_SHIFT;
3148 unsigned max_y = (MALI_TILE_COORD_Y(s->max_tile_coord) + 1) << MALI_TILE_SHIFT;
3149
3150 /* For the max, we also want the floored (rather than ceiled) version for checking */
3151
3152 unsigned max_x_f = (MALI_TILE_COORD_X(s->max_tile_coord)) << MALI_TILE_SHIFT;
3153 unsigned max_y_f = (MALI_TILE_COORD_Y(s->max_tile_coord)) << MALI_TILE_SHIFT;
3154
3155 /* Validate the coordinates are well-ordered */
3156
3157 if (min_x == max_x)
3158 pandecode_msg("XXX: empty X coordinates (%u = %u)\n", min_x, max_x);
3159 else if (min_x > max_x)
3160 pandecode_msg("XXX: misordered X coordinates (%u > %u)\n", min_x, max_x);
3161
3162 if (min_y == max_y)
3163 pandecode_msg("XXX: empty X coordinates (%u = %u)\n", min_x, max_x);
3164 else if (min_y > max_y)
3165 pandecode_msg("XXX: misordered X coordinates (%u > %u)\n", min_x, max_x);
3166
3167 /* Validate the coordinates fit inside the framebuffer. We use floor,
3168 * rather than ceil, for the max coordinates, since the tile
3169 * coordinates for something like an 800x600 framebuffer will actually
3170 * resolve to 800x608, which would otherwise trigger a Y-overflow */
3171
3172 if ((min_x > info.width) || (max_x_f > info.width))
3173 pandecode_msg("XXX: tile coordinates overflow in X direction\n");
3174
3175 if ((min_y > info.height) || (max_y_f > info.height))
3176 pandecode_msg("XXX: tile coordinates overflow in Y direction\n");
3177
3178 /* After validation, we print */
3179
3180 pandecode_log("fragment (%u, %u) ... (%u, %u)\n\n", min_x, min_y, max_x, max_y);
3181
3182 /* The FBD is a tagged pointer */
3183
3184 unsigned tag = (s->framebuffer & ~FBD_MASK);
3185
3186 if (tag != expected_tag)
3187 pandecode_msg("XXX: expected FBD tag %X but got %X\n", expected_tag, tag);
3188
3189 return sizeof(*s);
3190 }
3191
3192 /* Entrypoint to start tracing. jc_gpu_va is the GPU address for the first job
3193 * in the chain; later jobs are found by walking the chain. Bifrost is, well,
3194 * if it's bifrost or not. GPU ID is the more finegrained ID (at some point, we
3195 * might wish to combine this with the bifrost parameter) because some details
3196 * are model-specific even within a particular architecture. Minimal traces
3197 * *only* examine the job descriptors, skipping printing entirely if there is
3198 * no faults, and only descends into the payload if there are faults. This is
3199 * useful for looking for faults without the overhead of invasive traces. */
3200
3201 void
3202 pandecode_jc(mali_ptr jc_gpu_va, bool bifrost, unsigned gpu_id, bool minimal)
3203 {
3204 pandecode_dump_file_open();
3205
3206 struct mali_job_descriptor_header *h;
3207 unsigned job_descriptor_number = 0;
3208
3209 do {
3210 struct pandecode_mapped_memory *mem =
3211 pandecode_find_mapped_gpu_mem_containing(jc_gpu_va);
3212
3213 void *payload;
3214
3215 h = PANDECODE_PTR(mem, jc_gpu_va, struct mali_job_descriptor_header);
3216
3217 /* On Midgard, for 32-bit jobs except for fragment jobs, the
3218 * high 32-bits of the 64-bit pointer are reused to store
3219 * something else.
3220 */
3221 int offset = h->job_descriptor_size == MALI_JOB_32 &&
3222 h->job_type != MALI_JOB_TYPE_FRAGMENT ? 4 : 0;
3223 mali_ptr payload_ptr = jc_gpu_va + sizeof(*h) - offset;
3224
3225 payload = pandecode_fetch_gpu_mem(mem, payload_ptr, 256);
3226
3227 int job_no = job_descriptor_number++;
3228
3229 /* If the job is good to go, skip it in minimal mode */
3230 if (minimal && (h->exception_status == 0x0 || h->exception_status == 0x1))
3231 continue;
3232
3233 pandecode_log("struct mali_job_descriptor_header job_%"PRIx64"_%d = {\n", jc_gpu_va, job_no);
3234 pandecode_indent++;
3235
3236 pandecode_prop("job_type = %s", mali_job_type_as_str(h->job_type));
3237
3238 if (h->job_descriptor_size)
3239 pandecode_prop("job_descriptor_size = %d", h->job_descriptor_size);
3240
3241 if (h->exception_status && h->exception_status != 0x1)
3242 pandecode_prop("exception_status = %x (source ID: 0x%x access: %s exception: 0x%x)",
3243 h->exception_status,
3244 (h->exception_status >> 16) & 0xFFFF,
3245 pandecode_exception_access((h->exception_status >> 8) & 0x3),
3246 h->exception_status & 0xFF);
3247
3248 if (h->first_incomplete_task)
3249 pandecode_prop("first_incomplete_task = %d", h->first_incomplete_task);
3250
3251 if (h->fault_pointer)
3252 pandecode_prop("fault_pointer = 0x%" PRIx64, h->fault_pointer);
3253
3254 if (h->job_barrier)
3255 pandecode_prop("job_barrier = %d", h->job_barrier);
3256
3257 pandecode_prop("job_index = %d", h->job_index);
3258
3259 if (h->unknown_flags)
3260 pandecode_prop("unknown_flags = %d", h->unknown_flags);
3261
3262 if (h->job_dependency_index_1)
3263 pandecode_prop("job_dependency_index_1 = %d", h->job_dependency_index_1);
3264
3265 if (h->job_dependency_index_2)
3266 pandecode_prop("job_dependency_index_2 = %d", h->job_dependency_index_2);
3267
3268 pandecode_indent--;
3269 pandecode_log("};\n");
3270
3271 switch (h->job_type) {
3272 case MALI_JOB_TYPE_WRITE_VALUE: {
3273 struct mali_payload_write_value *s = payload;
3274 pandecode_log("struct mali_payload_write_value payload_%"PRIx64"_%d = {\n", payload_ptr, job_no);
3275 pandecode_indent++;
3276 MEMORY_PROP(s, address);
3277
3278 if (s->value_descriptor != MALI_WRITE_VALUE_ZERO) {
3279 pandecode_msg("XXX: unknown value descriptor\n");
3280 pandecode_prop("value_descriptor = 0x%" PRIX32, s->value_descriptor);
3281 }
3282
3283 if (s->reserved) {
3284 pandecode_msg("XXX: set value tripped\n");
3285 pandecode_prop("reserved = 0x%" PRIX32, s->reserved);
3286 }
3287
3288 pandecode_prop("immediate = 0x%" PRIX64, s->immediate);
3289 pandecode_indent--;
3290 pandecode_log("};\n");
3291
3292 break;
3293 }
3294
3295 case MALI_JOB_TYPE_TILER:
3296 case MALI_JOB_TYPE_VERTEX:
3297 case MALI_JOB_TYPE_COMPUTE:
3298 if (bifrost) {
3299 if (h->job_type == MALI_JOB_TYPE_TILER)
3300 pandecode_tiler_job_bfr(h, mem, payload_ptr, job_no, gpu_id);
3301 else
3302 pandecode_vertex_job_bfr(h, mem, payload_ptr, job_no, gpu_id);
3303 } else
3304 pandecode_vertex_or_tiler_job_mdg(h, mem, payload_ptr, job_no, gpu_id);
3305
3306 break;
3307
3308 case MALI_JOB_TYPE_FRAGMENT:
3309 pandecode_fragment_job(mem, payload_ptr, job_no, bifrost, gpu_id);
3310 break;
3311
3312 default:
3313 break;
3314 }
3315 } while ((jc_gpu_va = h->next_job));
3316
3317 pandecode_map_read_write();
3318 }