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