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