1/*
2 * Copyright (C) 2017-2019 Alyssa Rosenzweig
3 * Copyright (C) 2017-2019 Connor Abbott
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include <panfrost-job.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <memory.h>
29#include <stdbool.h>
30#include <stdarg.h>
31#include "mmap.h"
32
33#include "../pan_pretty_print.h"
34#include "../midgard/disassemble.h"
35int pandecode_replay_jc(mali_ptr jc_gpu_va, bool bifrost);
36
37#define MEMORY_PROP(obj, p) {\
38	char *a = pointer_as_memory_reference(obj->p); \
39	pandecode_prop("%s = %s", #p, a); \
40	free(a); \
41}
42
43#define MEMORY_COMMENT(obj, p) {\
44	char *a = pointer_as_memory_reference(obj->p); \
45	pandecode_msg("%s = %s\n", #p, a); \
46	free(a); \
47}
48
49#define DYN_MEMORY_PROP(obj, no, p) { \
50	if (obj->p) \
51		pandecode_prop("%s = %s_%d_p", #p, #p, no); \
52}
53
54/* Semantic logging type.
55 *
56 * Raw: for raw messages to be printed as is.
57 * Message: for helpful information to be commented out in replays.
58 * Property: for properties of a struct
59 *
60 * Use one of pandecode_log, pandecode_msg, or pandecode_prop as syntax sugar.
61 */
62
63enum pandecode_log_type {
64        PANDECODE_RAW,
65        PANDECODE_MESSAGE,
66        PANDECODE_PROPERTY
67};
68
69#define pandecode_log(...)  pandecode_log_typed(PANDECODE_RAW,      __VA_ARGS__)
70#define pandecode_msg(...)  pandecode_log_typed(PANDECODE_MESSAGE,  __VA_ARGS__)
71#define pandecode_prop(...) pandecode_log_typed(PANDECODE_PROPERTY, __VA_ARGS__)
72
73unsigned pandecode_indent = 0;
74
75static void
76pandecode_make_indent(void)
77{
78        for (unsigned i = 0; i < pandecode_indent; ++i)
79                printf("    ");
80}
81
82static void
83pandecode_log_typed(enum pandecode_log_type type, const char *format, ...)
84{
85        va_list ap;
86
87        pandecode_make_indent();
88
89        if (type == PANDECODE_MESSAGE)
90                printf("// ");
91        else if (type == PANDECODE_PROPERTY)
92                printf(".");
93
94        va_start(ap, format);
95        vprintf(format, ap);
96        va_end(ap);
97
98        if (type == PANDECODE_PROPERTY)
99                printf(",\n");
100}
101
102static void
103pandecode_log_cont(const char *format, ...)
104{
105        va_list ap;
106
107        va_start(ap, format);
108        vprintf(format, ap);
109        va_end(ap);
110}
111
112struct pandecode_flag_info {
113        u64 flag;
114        const char *name;
115};
116
117static void
118pandecode_log_decoded_flags(const struct pandecode_flag_info *flag_info,
119                          u64 flags)
120{
121        bool decodable_flags_found = false;
122
123        for (int i = 0; flag_info[i].name; i++) {
124                if ((flags & flag_info[i].flag) != flag_info[i].flag)
125                        continue;
126
127                if (!decodable_flags_found) {
128                        decodable_flags_found = true;
129                } else {
130                        pandecode_log_cont(" | ");
131                }
132
133                pandecode_log_cont("%s", flag_info[i].name);
134
135                flags &= ~flag_info[i].flag;
136        }
137
138        if (decodable_flags_found) {
139                if (flags)
140                        pandecode_log_cont(" | 0x%" PRIx64, flags);
141        } else {
142                pandecode_log_cont("0x%" PRIx64, flags);
143        }
144}
145
146#define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
147static const struct pandecode_flag_info gl_enable_flag_info[] = {
148        FLAG_INFO(CULL_FACE_FRONT),
149        FLAG_INFO(CULL_FACE_BACK),
150        FLAG_INFO(OCCLUSION_QUERY),
151        FLAG_INFO(OCCLUSION_PRECISE),
152        {}
153};
154#undef FLAG_INFO
155
156#define FLAG_INFO(flag) { MALI_CLEAR_##flag, "MALI_CLEAR_" #flag }
157static const struct pandecode_flag_info clear_flag_info[] = {
158        FLAG_INFO(FAST),
159        FLAG_INFO(SLOW),
160        FLAG_INFO(SLOW_STENCIL),
161        {}
162};
163#undef FLAG_INFO
164
165#define FLAG_INFO(flag) { MALI_MASK_##flag, "MALI_MASK_" #flag }
166static const struct pandecode_flag_info mask_flag_info[] = {
167        FLAG_INFO(R),
168        FLAG_INFO(G),
169        FLAG_INFO(B),
170        FLAG_INFO(A),
171        {}
172};
173#undef FLAG_INFO
174
175#define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
176static const struct pandecode_flag_info u3_flag_info[] = {
177        FLAG_INFO(HAS_MSAA),
178        FLAG_INFO(CAN_DISCARD),
179        FLAG_INFO(HAS_BLEND_SHADER),
180        FLAG_INFO(DEPTH_TEST),
181        {}
182};
183
184static const struct pandecode_flag_info u4_flag_info[] = {
185        FLAG_INFO(NO_MSAA),
186        FLAG_INFO(NO_DITHER),
187        FLAG_INFO(DEPTH_RANGE_A),
188        FLAG_INFO(DEPTH_RANGE_B),
189        FLAG_INFO(STENCIL_TEST),
190        FLAG_INFO(SAMPLE_ALPHA_TO_COVERAGE_NO_BLEND_SHADER),
191        {}
192};
193#undef FLAG_INFO
194
195#define FLAG_INFO(flag) { MALI_FRAMEBUFFER_##flag, "MALI_FRAMEBUFFER_" #flag }
196static const struct pandecode_flag_info fb_fmt_flag_info[] = {
197        FLAG_INFO(MSAA_A),
198        FLAG_INFO(MSAA_B),
199        FLAG_INFO(MSAA_8),
200        {}
201};
202#undef FLAG_INFO
203
204#define FLAG_INFO(flag) { MALI_MFBD_FORMAT_##flag, "MALI_MFBD_FORMAT_" #flag }
205static const struct pandecode_flag_info mfbd_fmt_flag_info[] = {
206        FLAG_INFO(AFBC),
207        FLAG_INFO(MSAA),
208        {}
209};
210#undef FLAG_INFO
211
212#define FLAG_INFO(flag) { MALI_EXTRA_##flag, "MALI_EXTRA_" #flag }
213static const struct pandecode_flag_info mfbd_extra_flag_info[] = {
214        FLAG_INFO(PRESENT),
215        FLAG_INFO(AFBC),
216        FLAG_INFO(ZS),
217        {}
218};
219#undef FLAG_INFO
220
221#define FLAG_INFO(flag) { MALI_##flag, "MALI_" #flag }
222static const struct pandecode_flag_info shader_unknown1_flag_info [] = {
223        FLAG_INFO(NO_ALPHA_TO_COVERAGE),
224        FLAG_INFO(READS_TILEBUFFER),
225        FLAG_INFO(READS_ZS),
226        {}
227};
228#undef FLAG_INFO
229
230extern char *replace_fragment;
231extern char *replace_vertex;
232
233static char *
234pandecode_job_type_name(enum mali_job_type type)
235{
236#define DEFINE_CASE(name) case JOB_TYPE_ ## name: return "JOB_TYPE_" #name
237
238        switch (type) {
239                DEFINE_CASE(NULL);
240                DEFINE_CASE(SET_VALUE);
241                DEFINE_CASE(CACHE_FLUSH);
242                DEFINE_CASE(COMPUTE);
243                DEFINE_CASE(VERTEX);
244                DEFINE_CASE(TILER);
245                DEFINE_CASE(FUSED);
246                DEFINE_CASE(FRAGMENT);
247
248        case JOB_NOT_STARTED:
249                return "NOT_STARTED";
250
251        default:
252                pandecode_log("Warning! Unknown job type %x\n", type);
253                return "!?!?!?";
254        }
255
256#undef DEFINE_CASE
257}
258
259static char *
260pandecode_draw_mode_name(enum mali_draw_mode mode)
261{
262#define DEFINE_CASE(name) case MALI_ ## name: return "MALI_" #name
263
264        switch (mode) {
265                DEFINE_CASE(DRAW_NONE);
266                DEFINE_CASE(POINTS);
267                DEFINE_CASE(LINES);
268                DEFINE_CASE(TRIANGLES);
269                DEFINE_CASE(TRIANGLE_STRIP);
270                DEFINE_CASE(TRIANGLE_FAN);
271                DEFINE_CASE(LINE_STRIP);
272                DEFINE_CASE(LINE_LOOP);
273                DEFINE_CASE(POLYGON);
274                DEFINE_CASE(QUADS);
275                DEFINE_CASE(QUAD_STRIP);
276
277        default:
278                return "MALI_TRIANGLES /* XXX: Unknown GL mode, check dump */";
279        }
280
281#undef DEFINE_CASE
282}
283
284#define DEFINE_CASE(name) case MALI_FUNC_ ## name: return "MALI_FUNC_" #name
285static char *
286pandecode_func_name(enum mali_func mode)
287{
288        switch (mode) {
289                DEFINE_CASE(NEVER);
290                DEFINE_CASE(LESS);
291                DEFINE_CASE(EQUAL);
292                DEFINE_CASE(LEQUAL);
293                DEFINE_CASE(GREATER);
294                DEFINE_CASE(NOTEQUAL);
295                DEFINE_CASE(GEQUAL);
296                DEFINE_CASE(ALWAYS);
297
298        default:
299                return "MALI_FUNC_NEVER /* XXX: Unknown function, check dump */";
300        }
301}
302#undef DEFINE_CASE
303
304/* Why is this duplicated? Who knows... */
305#define DEFINE_CASE(name) case MALI_ALT_FUNC_ ## name: return "MALI_ALT_FUNC_" #name
306static char *
307pandecode_alt_func_name(enum mali_alt_func mode)
308{
309        switch (mode) {
310                DEFINE_CASE(NEVER);
311                DEFINE_CASE(LESS);
312                DEFINE_CASE(EQUAL);
313                DEFINE_CASE(LEQUAL);
314                DEFINE_CASE(GREATER);
315                DEFINE_CASE(NOTEQUAL);
316                DEFINE_CASE(GEQUAL);
317                DEFINE_CASE(ALWAYS);
318
319        default:
320                return "MALI_FUNC_NEVER /* XXX: Unknown function, check dump */";
321        }
322}
323#undef DEFINE_CASE
324
325#define DEFINE_CASE(name) case MALI_STENCIL_ ## name: return "MALI_STENCIL_" #name
326static char *
327pandecode_stencil_op_name(enum mali_stencil_op op)
328{
329        switch (op) {
330                DEFINE_CASE(KEEP);
331                DEFINE_CASE(REPLACE);
332                DEFINE_CASE(ZERO);
333                DEFINE_CASE(INVERT);
334                DEFINE_CASE(INCR_WRAP);
335                DEFINE_CASE(DECR_WRAP);
336                DEFINE_CASE(INCR);
337                DEFINE_CASE(DECR);
338
339        default:
340                return "MALI_STENCIL_KEEP /* XXX: Unknown stencil op, check dump */";
341        }
342}
343
344#undef DEFINE_CASE
345
346#define DEFINE_CASE(name) case MALI_ATTR_ ## name: return "MALI_ATTR_" #name
347static char *pandecode_attr_mode_name(enum mali_attr_mode mode)
348{
349	switch(mode) {
350	DEFINE_CASE(UNUSED);
351	DEFINE_CASE(LINEAR);
352	DEFINE_CASE(POT_DIVIDE);
353	DEFINE_CASE(MODULO);
354	DEFINE_CASE(NPOT_DIVIDE);
355	default: return "MALI_ATTR_UNUSED /* XXX: Unknown stencil op, check dump */";
356	}
357}
358
359#undef DEFINE_CASE
360
361#define DEFINE_CASE(name) case MALI_CHANNEL_## name: return "MALI_CHANNEL_" #name
362static char *
363pandecode_channel_name(enum mali_channel channel)
364{
365        switch (channel) {
366                DEFINE_CASE(RED);
367                DEFINE_CASE(GREEN);
368                DEFINE_CASE(BLUE);
369                DEFINE_CASE(ALPHA);
370                DEFINE_CASE(ZERO);
371                DEFINE_CASE(ONE);
372                DEFINE_CASE(RESERVED_0);
373                DEFINE_CASE(RESERVED_1);
374
375        default:
376                return "MALI_CHANNEL_ZERO /* XXX: Unknown channel, check dump */";
377        }
378}
379#undef DEFINE_CASE
380
381#define DEFINE_CASE(name) case MALI_WRAP_## name: return "MALI_WRAP_" #name
382static char *
383pandecode_wrap_mode_name(enum mali_wrap_mode op)
384{
385        switch (op) {
386                DEFINE_CASE(REPEAT);
387                DEFINE_CASE(CLAMP_TO_EDGE);
388                DEFINE_CASE(CLAMP_TO_BORDER);
389                DEFINE_CASE(MIRRORED_REPEAT);
390
391        default:
392                return "MALI_WRAP_REPEAT /* XXX: Unknown wrap mode, check dump */";
393        }
394}
395#undef DEFINE_CASE
396
397static inline char *
398pandecode_decode_fbd_type(enum mali_fbd_type type)
399{
400        if (type == MALI_SFBD)      return "SFBD";
401        else if (type == MALI_MFBD) return "MFBD";
402        else return "WATFBD /* XXX */";
403}
404
405static void
406pandecode_replay_sfbd(uint64_t gpu_va, int job_no)
407{
408        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
409        const struct mali_single_framebuffer *PANDECODE_PTR_VAR(s, mem, (mali_ptr) gpu_va);
410
411        pandecode_log("struct mali_single_framebuffer framebuffer_%d = {\n", job_no);
412        pandecode_indent++;
413
414        pandecode_prop("unknown1 = 0x%" PRIx32, s->unknown1);
415        pandecode_prop("unknown2 = 0x%" PRIx32, s->unknown2);
416
417        pandecode_log(".format = ");
418        pandecode_log_decoded_flags(fb_fmt_flag_info, s->format);
419        pandecode_log_cont(",\n");
420
421        pandecode_prop("width = MALI_POSITIVE(%" PRId16 ")", s->width + 1);
422        pandecode_prop("height = MALI_POSITIVE(%" PRId16 ")", s->height + 1);
423
424        MEMORY_PROP(s, framebuffer);
425        pandecode_prop("stride = %d", s->stride);
426
427        /* Earlier in the actual commandstream -- right before width -- but we
428         * delay to flow nicer */
429
430        pandecode_log(".clear_flags = ");
431        pandecode_log_decoded_flags(clear_flag_info, s->clear_flags);
432        pandecode_log_cont(",\n");
433
434        if (s->depth_buffer | s->depth_buffer_enable) {
435                MEMORY_PROP(s, depth_buffer);
436                pandecode_prop("depth_buffer_enable = %s", DS_ENABLE(s->depth_buffer_enable));
437        }
438
439        if (s->stencil_buffer | s->stencil_buffer_enable) {
440                MEMORY_PROP(s, stencil_buffer);
441                pandecode_prop("stencil_buffer_enable = %s", DS_ENABLE(s->stencil_buffer_enable));
442        }
443
444        if (s->clear_color_1 | s->clear_color_2 | s->clear_color_3 | s->clear_color_4) {
445                pandecode_prop("clear_color_1 = 0x%" PRIx32, s->clear_color_1);
446                pandecode_prop("clear_color_2 = 0x%" PRIx32, s->clear_color_2);
447                pandecode_prop("clear_color_3 = 0x%" PRIx32, s->clear_color_3);
448                pandecode_prop("clear_color_4 = 0x%" PRIx32, s->clear_color_4);
449        }
450
451        if (s->clear_depth_1 != 0 || s->clear_depth_2 != 0 || s->clear_depth_3 != 0 || s->clear_depth_4 != 0) {
452                pandecode_prop("clear_depth_1 = %f", s->clear_depth_1);
453                pandecode_prop("clear_depth_2 = %f", s->clear_depth_2);
454                pandecode_prop("clear_depth_3 = %f", s->clear_depth_3);
455                pandecode_prop("clear_depth_4 = %f", s->clear_depth_4);
456        }
457
458        if (s->clear_stencil) {
459                pandecode_prop("clear_stencil = 0x%x", s->clear_stencil);
460        }
461
462        MEMORY_PROP(s, unknown_address_0);
463        MEMORY_PROP(s, unknown_address_1);
464        MEMORY_PROP(s, unknown_address_2);
465
466        pandecode_prop("resolution_check = 0x%" PRIx32, s->resolution_check);
467        pandecode_prop("tiler_flags = 0x%" PRIx32, s->tiler_flags);
468
469        MEMORY_PROP(s, tiler_heap_free);
470        MEMORY_PROP(s, tiler_heap_end);
471
472        pandecode_indent--;
473        pandecode_log("};\n");
474
475        pandecode_prop("zero0 = 0x%" PRIx64, s->zero0);
476        pandecode_prop("zero1 = 0x%" PRIx64, s->zero1);
477        pandecode_prop("zero2 = 0x%" PRIx32, s->zero2);
478        pandecode_prop("zero4 = 0x%" PRIx32, s->zero4);
479
480        printf(".zero3 = {");
481
482        for (int i = 0; i < sizeof(s->zero3) / sizeof(s->zero3[0]); ++i)
483                printf("%X, ", s->zero3[i]);
484
485        printf("},\n");
486
487        printf(".zero6 = {");
488
489        for (int i = 0; i < sizeof(s->zero6) / sizeof(s->zero6[0]); ++i)
490                printf("%X, ", s->zero6[i]);
491
492        printf("},\n");
493}
494
495static void
496pandecode_replay_swizzle(unsigned swizzle)
497{
498	pandecode_prop("swizzle = %s | (%s << 3) | (%s << 6) | (%s << 9)",
499			pandecode_channel_name((swizzle >> 0) & 0x7),
500			pandecode_channel_name((swizzle >> 3) & 0x7),
501			pandecode_channel_name((swizzle >> 6) & 0x7),
502			pandecode_channel_name((swizzle >> 9) & 0x7));
503}
504
505static void
506pandecode_rt_format(struct mali_rt_format format)
507{
508        pandecode_log(".format = {\n");
509        pandecode_indent++;
510
511        pandecode_prop("unk1 = 0x%" PRIx32, format.unk1);
512        pandecode_prop("unk2 = 0x%" PRIx32, format.unk2);
513
514        pandecode_prop("nr_channels = MALI_POSITIVE(%d)",
515                        MALI_NEGATIVE(format.nr_channels));
516
517        pandecode_log(".flags = ");
518        pandecode_log_decoded_flags(mfbd_fmt_flag_info, format.flags);
519        pandecode_log_cont(",\n");
520
521        pandecode_replay_swizzle(format.swizzle);
522
523        pandecode_prop("unk4 = 0x%" PRIx32, format.unk4);
524
525        pandecode_indent--;
526        pandecode_log("},\n");
527}
528
529static void
530pandecode_replay_mfbd_bfr(uint64_t gpu_va, int job_no)
531{
532        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
533        const struct bifrost_framebuffer *PANDECODE_PTR_VAR(fb, mem, (mali_ptr) gpu_va);
534
535        if (fb->sample_locations) {
536                /* The blob stores all possible sample locations in a single buffer
537                 * allocated on startup, and just switches the pointer when switching
538                 * MSAA state. For now, we just put the data into the cmdstream, but we
539                 * should do something like what the blob does with a real driver.
540                 *
541                 * There seem to be 32 slots for sample locations, followed by another
542                 * 16. The second 16 is just the center location followed by 15 zeros
543                 * in all the cases I've identified (maybe shader vs. depth/color
544                 * samples?).
545                 */
546
547                struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(fb->sample_locations);
548
549                const u16 *PANDECODE_PTR_VAR(samples, smem, fb->sample_locations);
550
551                pandecode_log("uint16_t sample_locations_%d[] = {\n", job_no);
552                pandecode_indent++;
553
554                for (int i = 0; i < 32 + 16; i++) {
555                        pandecode_log("%d, %d,\n", samples[2 * i], samples[2 * i + 1]);
556                }
557
558                pandecode_indent--;
559                pandecode_log("};\n");
560        }
561
562        pandecode_log("struct bifrost_framebuffer framebuffer_%d = {\n", job_no);
563        pandecode_indent++;
564
565        pandecode_prop("unk0 = 0x%x", fb->unk0);
566
567        if (fb->sample_locations)
568                pandecode_prop("sample_locations = sample_locations_%d", job_no);
569
570        /* Assume that unknown1 and tiler_meta were emitted in the last job for
571         * now */
572        /*pandecode_prop("unknown1 = unknown1_%d_p", job_no - 1);
573        pandecode_prop("tiler_meta = tiler_meta_%d_p", job_no - 1);*/
574        MEMORY_PROP(fb, unknown1);
575        MEMORY_PROP(fb, tiler_meta);
576
577        pandecode_prop("width1 = MALI_POSITIVE(%d)", fb->width1 + 1);
578        pandecode_prop("height1 = MALI_POSITIVE(%d)", fb->height1 + 1);
579        pandecode_prop("width2 = MALI_POSITIVE(%d)", fb->width2 + 1);
580        pandecode_prop("height2 = MALI_POSITIVE(%d)", fb->height2 + 1);
581
582        pandecode_prop("unk1 = 0x%x", fb->unk1);
583        pandecode_prop("unk2 = 0x%x", fb->unk2);
584        pandecode_prop("rt_count_1 = MALI_POSITIVE(%d)", fb->rt_count_1 + 1);
585        pandecode_prop("rt_count_2 = %d", fb->rt_count_2);
586
587        pandecode_prop("unk3 = 0x%x", fb->unk3);
588        pandecode_prop("clear_stencil = 0x%x", fb->clear_stencil);
589        pandecode_prop("clear_depth = %f", fb->clear_depth);
590
591        pandecode_prop("unknown2 = 0x%x", fb->unknown2);
592        MEMORY_PROP(fb, scratchpad);
593        MEMORY_PROP(fb, tiler_scratch_start);
594        MEMORY_PROP(fb, tiler_scratch_middle);
595        MEMORY_PROP(fb, tiler_heap_start);
596        MEMORY_PROP(fb, tiler_heap_end);
597
598        if (fb->zero3 || fb->zero4 || fb->zero9 || fb->zero10 || fb->zero11 || fb->zero12) {
599                pandecode_msg("framebuffer zeros tripped\n");
600                pandecode_prop("zero3 = 0x%" PRIx32, fb->zero3);
601                pandecode_prop("zero4 = 0x%" PRIx32, fb->zero4);
602                pandecode_prop("zero9 = 0x%" PRIx64, fb->zero9);
603                pandecode_prop("zero10 = 0x%" PRIx64, fb->zero10);
604                pandecode_prop("zero11 = 0x%" PRIx64, fb->zero11);
605                pandecode_prop("zero12 = 0x%" PRIx64, fb->zero12);
606        }
607
608        pandecode_indent--;
609        pandecode_log("};\n");
610
611        gpu_va += sizeof(struct bifrost_framebuffer);
612
613        if (fb->unk3 & MALI_MFBD_EXTRA) {
614                mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
615                const struct bifrost_fb_extra *PANDECODE_PTR_VAR(fbx, mem, (mali_ptr) gpu_va);
616
617                pandecode_log("struct bifrost_fb_extra fb_extra_%d = {\n", job_no);
618                pandecode_indent++;
619
620                MEMORY_PROP(fbx, checksum);
621
622                if (fbx->checksum_stride)
623                        pandecode_prop("checksum_stride = %d", fbx->checksum_stride);
624
625                pandecode_log(".flags = ");
626                pandecode_log_decoded_flags(mfbd_extra_flag_info, fbx->flags);
627                pandecode_log_cont(",\n");
628
629                if (fbx->flags & MALI_EXTRA_AFBC_ZS) {
630                        pandecode_log(".ds_afbc = {\n");
631                        pandecode_indent++;
632
633                        MEMORY_PROP((&fbx->ds_afbc), depth_stencil_afbc_metadata);
634                        pandecode_prop("depth_stencil_afbc_stride = %d",
635                                     fbx->ds_afbc.depth_stencil_afbc_stride);
636                        MEMORY_PROP((&fbx->ds_afbc), depth_stencil);
637
638                        if (fbx->ds_afbc.zero1 || fbx->ds_afbc.padding) {
639                                pandecode_msg("Depth/stencil AFBC zeros tripped\n");
640                                pandecode_prop("zero1 = 0x%" PRIx32,
641                                             fbx->ds_afbc.zero1);
642                                pandecode_prop("padding = 0x%" PRIx64,
643                                             fbx->ds_afbc.padding);
644                        }
645
646                        pandecode_indent--;
647                        pandecode_log("},\n");
648                } else {
649                        pandecode_log(".ds_linear = {\n");
650                        pandecode_indent++;
651
652                        if (fbx->ds_linear.depth) {
653                                MEMORY_PROP((&fbx->ds_linear), depth);
654                                pandecode_prop("depth_stride = %d",
655                                             fbx->ds_linear.depth_stride);
656                        }
657
658                        if (fbx->ds_linear.stencil) {
659                                MEMORY_PROP((&fbx->ds_linear), stencil);
660                                pandecode_prop("stencil_stride = %d",
661                                             fbx->ds_linear.stencil_stride);
662                        }
663
664                        if (fbx->ds_linear.depth_stride_zero ||
665                                        fbx->ds_linear.stencil_stride_zero ||
666                                        fbx->ds_linear.zero1 || fbx->ds_linear.zero2) {
667                                pandecode_msg("Depth/stencil zeros tripped\n");
668                                pandecode_prop("depth_stride_zero = 0x%x",
669                                             fbx->ds_linear.depth_stride_zero);
670                                pandecode_prop("stencil_stride_zero = 0x%x",
671                                             fbx->ds_linear.stencil_stride_zero);
672                                pandecode_prop("zero1 = 0x%" PRIx32,
673                                             fbx->ds_linear.zero1);
674                                pandecode_prop("zero2 = 0x%" PRIx32,
675                                             fbx->ds_linear.zero2);
676                        }
677
678                        pandecode_indent--;
679                        pandecode_log("},\n");
680                }
681
682                if (fbx->zero3 || fbx->zero4) {
683                        pandecode_msg("fb_extra zeros tripped\n");
684                        pandecode_prop("zero3 = 0x%" PRIx64, fbx->zero3);
685                        pandecode_prop("zero4 = 0x%" PRIx64, fbx->zero4);
686                }
687
688                pandecode_indent--;
689                pandecode_log("};\n");
690
691                gpu_va += sizeof(struct bifrost_fb_extra);
692        }
693
694        pandecode_log("struct bifrost_render_target rts_list_%d[] = {\n", job_no);
695        pandecode_indent++;
696
697        for (int i = 0; i < MALI_NEGATIVE(fb->rt_count_1); i++) {
698                mali_ptr rt_va = gpu_va + i * sizeof(struct bifrost_render_target);
699                mem = pandecode_find_mapped_gpu_mem_containing(rt_va);
700                const struct bifrost_render_target *PANDECODE_PTR_VAR(rt, mem, (mali_ptr) rt_va);
701
702                pandecode_log("{\n");
703                pandecode_indent++;
704
705                pandecode_rt_format(rt->format);
706
707                /* TODO: How the actual heck does AFBC enabling work here? */
708                if (0) {
709                        pandecode_log(".afbc = {\n");
710                        pandecode_indent++;
711
712                        char *a = pointer_as_memory_reference(rt->afbc.metadata);
713                        pandecode_prop("metadata = %s", a);
714                        free(a);
715
716                        pandecode_prop("stride = %d", rt->afbc.stride);
717                        pandecode_prop("unk = 0x%" PRIx32, rt->afbc.unk);
718
719                        pandecode_indent--;
720                        pandecode_log("},\n");
721                } else {
722                        pandecode_log(".chunknown = {\n");
723                        pandecode_indent++;
724
725                        pandecode_prop("unk = 0x%" PRIx64, rt->chunknown.unk);
726
727                        char *a = pointer_as_memory_reference(rt->chunknown.pointer);
728                        pandecode_prop("pointer = %s", a);
729                        free(a);
730
731                        pandecode_indent--;
732                        pandecode_log("},\n");
733                }
734
735                MEMORY_PROP(rt, framebuffer);
736                pandecode_prop("framebuffer_stride = %d", rt->framebuffer_stride);
737
738                if (rt->clear_color_1 | rt->clear_color_2 | rt->clear_color_3 | rt->clear_color_4) {
739                        pandecode_prop("clear_color_1 = 0x%" PRIx32, rt->clear_color_1);
740                        pandecode_prop("clear_color_2 = 0x%" PRIx32, rt->clear_color_2);
741                        pandecode_prop("clear_color_3 = 0x%" PRIx32, rt->clear_color_3);
742                        pandecode_prop("clear_color_4 = 0x%" PRIx32, rt->clear_color_4);
743                }
744
745                if (rt->zero1 || rt->zero2 || rt->zero3) {
746                        pandecode_msg("render target zeros tripped\n");
747                        pandecode_prop("zero1 = 0x%" PRIx64, rt->zero1);
748                        pandecode_prop("zero2 = 0x%" PRIx32, rt->zero2);
749                        pandecode_prop("zero3 = 0x%" PRIx32, rt->zero3);
750                }
751
752                pandecode_indent--;
753                pandecode_log("},\n");
754        }
755
756        pandecode_indent--;
757        pandecode_log("};\n");
758}
759
760static void
761pandecode_replay_attributes(const struct pandecode_mapped_memory *mem,
762                          mali_ptr addr, int job_no, char *suffix,
763                          int count, bool varying)
764{
765        char *prefix = varying ? "varyings" : "attributes";
766
767        union mali_attr *attr = pandecode_fetch_gpu_mem(mem, addr, sizeof(union mali_attr) * count);
768
769        char base[128];
770        snprintf(base, sizeof(base), "%s_data_%d%s", prefix, job_no, suffix);
771
772        for (int i = 0; i < count; ++i) {
773		enum mali_attr_mode mode = attr[i].elements & 7;
774
775		if (mode == MALI_ATTR_UNUSED)
776			continue;
777
778		mali_ptr raw_elements = attr[i].elements & ~7;
779
780                /* TODO: Do we maybe want to dump the attribute values
781                 * themselves given the specified format? Or is that too hard?
782                 * */
783
784                char *a = pointer_as_memory_reference(raw_elements);
785                pandecode_log("mali_ptr %s_%d_p = %s;\n", base, i, a);
786                free(a);
787        }
788
789        pandecode_log("union mali_attr %s_%d[] = {\n", prefix, job_no);
790        pandecode_indent++;
791
792        for (int i = 0; i < count; ++i) {
793                pandecode_log("{\n");
794                pandecode_indent++;
795
796		pandecode_prop("elements = (%s_%d_p) | %s", base, i, pandecode_attr_mode_name(attr[i].elements & 7));
797		pandecode_prop("shift = %d", attr[i].shift);
798		pandecode_prop("extra_flags = %d", attr[i].extra_flags);
799                pandecode_prop("stride = 0x%" PRIx32, attr[i].stride);
800                pandecode_prop("size = 0x%" PRIx32, attr[i].size);
801                pandecode_indent--;
802                pandecode_log("}, \n");
803
804		if ((attr[i].elements & 7) == MALI_ATTR_NPOT_DIVIDE) {
805			i++;
806			pandecode_log("{\n");
807			pandecode_indent++;
808			pandecode_prop("unk = 0x%x", attr[i].unk);
809			pandecode_prop("magic_divisor = 0x%08x", attr[i].magic_divisor);
810			if (attr[i].zero != 0)
811				pandecode_prop("zero = 0x%x /* XXX zero tripped */", attr[i].zero);
812			pandecode_prop("divisor = %d", attr[i].divisor);
813			pandecode_indent--;
814			pandecode_log("}, \n");
815		}
816
817        }
818
819        pandecode_indent--;
820        pandecode_log("};\n");
821}
822
823static mali_ptr
824pandecode_replay_shader_address(const char *name, mali_ptr ptr)
825{
826        /* TODO: Decode flags */
827        mali_ptr shader_ptr = ptr & ~15;
828
829        char *a = pointer_as_memory_reference(shader_ptr);
830        pandecode_prop("%s = (%s) | %d", name, a, (int) (ptr & 15));
831        free(a);
832
833        return shader_ptr;
834}
835
836static void
837pandecode_replay_stencil(const char *name, const struct mali_stencil_test *stencil)
838{
839        const char *func = pandecode_func_name(stencil->func);
840        const char *sfail = pandecode_stencil_op_name(stencil->sfail);
841        const char *dpfail = pandecode_stencil_op_name(stencil->dpfail);
842        const char *dppass = pandecode_stencil_op_name(stencil->dppass);
843
844        if (stencil->zero)
845                pandecode_msg("Stencil zero tripped: %X\n", stencil->zero);
846
847        pandecode_log(".stencil_%s = {\n", name);
848        pandecode_indent++;
849        pandecode_prop("ref = %d", stencil->ref);
850        pandecode_prop("mask = 0x%02X", stencil->mask);
851        pandecode_prop("func = %s", func);
852        pandecode_prop("sfail = %s", sfail);
853        pandecode_prop("dpfail = %s", dpfail);
854        pandecode_prop("dppass = %s", dppass);
855        pandecode_indent--;
856        pandecode_log("},\n");
857}
858
859static void
860pandecode_replay_blend_equation(const struct mali_blend_equation *blend, const char *suffix)
861{
862        if (blend->zero1)
863                pandecode_msg("Blend zero tripped: %X\n", blend->zero1);
864
865        pandecode_log(".blend_equation%s = {\n", suffix);
866        pandecode_indent++;
867
868        pandecode_prop("rgb_mode = 0x%X", blend->rgb_mode);
869        pandecode_prop("alpha_mode = 0x%X", blend->alpha_mode);
870
871        pandecode_log(".color_mask = ");
872        pandecode_log_decoded_flags(mask_flag_info, blend->color_mask);
873        pandecode_log_cont(",\n");
874
875        pandecode_indent--;
876        pandecode_log("},\n");
877}
878
879static int
880pandecode_replay_attribute_meta(int job_no, int count, const struct mali_vertex_tiler_postfix *v, bool varying, char *suffix)
881{
882        char base[128];
883        char *prefix = varying ? "varying" : "attribute";
884	unsigned max_index = 0;
885        snprintf(base, sizeof(base), "%s_meta", prefix);
886
887        pandecode_log("struct mali_attr_meta %s_%d%s[] = {\n", base, job_no, suffix);
888        pandecode_indent++;
889
890        struct mali_attr_meta *attr_meta;
891        mali_ptr p = varying ? (v->varying_meta & ~0xF) : v->attribute_meta;
892
893        struct pandecode_mapped_memory *attr_mem = pandecode_find_mapped_gpu_mem_containing(p);
894
895        for (int i = 0; i < count; ++i, p += sizeof(struct mali_attr_meta)) {
896                attr_meta = pandecode_fetch_gpu_mem(attr_mem, p,
897                                                  sizeof(*attr_mem));
898
899                pandecode_log("{\n");
900                pandecode_indent++;
901                pandecode_prop("index = %d", attr_meta->index);
902
903		if (attr_meta->index > max_index)
904			max_index = attr_meta->index;
905		pandecode_replay_swizzle(attr_meta->swizzle);
906		pandecode_prop("format = %s", pandecode_format_name(attr_meta->format));
907
908                pandecode_prop("unknown1 = 0x%" PRIx64, (u64) attr_meta->unknown1);
909                pandecode_prop("unknown3 = 0x%" PRIx64, (u64) attr_meta->unknown3);
910                pandecode_prop("src_offset = 0x%" PRIx64, (u64) attr_meta->src_offset);
911                pandecode_indent--;
912                pandecode_log("},\n");
913
914        }
915
916        pandecode_indent--;
917        pandecode_log("};\n");
918
919        return max_index;
920}
921
922static void
923pandecode_replay_indices(uintptr_t pindices, uint32_t index_count, int job_no)
924{
925        struct pandecode_mapped_memory *imem = pandecode_find_mapped_gpu_mem_containing(pindices);
926
927        if (imem) {
928                /* Indices are literally just a u32 array :) */
929
930                uint32_t *PANDECODE_PTR_VAR(indices, imem, pindices);
931
932                pandecode_log("uint32_t indices_%d[] = {\n", job_no);
933                pandecode_indent++;
934
935                for (unsigned i = 0; i < (index_count + 1); i += 3)
936                        pandecode_log("%d, %d, %d,\n",
937                                    indices[i],
938                                    indices[i + 1],
939                                    indices[i + 2]);
940
941                pandecode_indent--;
942                pandecode_log("};\n");
943        }
944}
945
946/* return bits [lo, hi) of word */
947static u32
948bits(u32 word, u32 lo, u32 hi)
949{
950        if (hi - lo >= 32)
951                return word; // avoid undefined behavior with the shift
952
953        return (word >> lo) & ((1 << (hi - lo)) - 1);
954}
955
956static void
957pandecode_replay_vertex_tiler_prefix(struct mali_vertex_tiler_prefix *p, int job_no)
958{
959        pandecode_log_cont("{\n");
960        pandecode_indent++;
961
962        pandecode_prop("invocation_count = %" PRIx32, p->invocation_count);
963        pandecode_prop("size_y_shift = %d", p->size_y_shift);
964        pandecode_prop("size_z_shift = %d", p->size_z_shift);
965        pandecode_prop("workgroups_x_shift = %d", p->workgroups_x_shift);
966        pandecode_prop("workgroups_y_shift = %d", p->workgroups_y_shift);
967        pandecode_prop("workgroups_z_shift = %d", p->workgroups_z_shift);
968        pandecode_prop("workgroups_x_shift_2 = 0x%" PRIx32, p->workgroups_x_shift_2);
969
970        /* Decode invocation_count. See the comment before the definition of
971         * invocation_count for an explanation.
972         */
973        pandecode_msg("size: (%d, %d, %d)\n",
974                    bits(p->invocation_count, 0, p->size_y_shift) + 1,
975                    bits(p->invocation_count, p->size_y_shift, p->size_z_shift) + 1,
976                    bits(p->invocation_count, p->size_z_shift,
977                         p->workgroups_x_shift) + 1);
978        pandecode_msg("workgroups: (%d, %d, %d)\n",
979                    bits(p->invocation_count, p->workgroups_x_shift,
980                         p->workgroups_y_shift) + 1,
981                    bits(p->invocation_count, p->workgroups_y_shift,
982                         p->workgroups_z_shift) + 1,
983                    bits(p->invocation_count, p->workgroups_z_shift,
984                         32) + 1);
985
986        /* TODO: Decode */
987        pandecode_prop("unknown_draw = 0x%" PRIx32, p->unknown_draw);
988        pandecode_prop("workgroups_x_shift_3 = 0x%" PRIx32, p->workgroups_x_shift_3);
989
990        pandecode_prop("draw_mode = %s", pandecode_draw_mode_name(p->draw_mode));
991
992        /* Index count only exists for tiler jobs anyway */
993
994        if (p->index_count)
995                pandecode_prop("index_count = MALI_POSITIVE(%" PRId32 ")", p->index_count + 1);
996
997        pandecode_prop("negative_start = %d", p->negative_start);
998
999        DYN_MEMORY_PROP(p, job_no, indices);
1000
1001        if (p->zero1) {
1002                pandecode_msg("Zero tripped\n");
1003                pandecode_prop("zero1 = 0x%" PRIx32, p->zero1);
1004        }
1005
1006        pandecode_indent--;
1007        pandecode_log("},\n");
1008}
1009
1010static void
1011pandecode_replay_uniform_buffers(mali_ptr pubufs, int ubufs_count, int job_no)
1012{
1013        struct pandecode_mapped_memory *umem = pandecode_find_mapped_gpu_mem_containing(pubufs);
1014
1015        struct mali_uniform_buffer_meta *PANDECODE_PTR_VAR(ubufs, umem, pubufs);
1016
1017        for (int i = 0; i < ubufs_count; i++) {
1018                mali_ptr ptr = ubufs[i].ptr << 2;
1019                struct pandecode_mapped_memory *umem2 = pandecode_find_mapped_gpu_mem_containing(ptr);
1020                uint32_t *PANDECODE_PTR_VAR(ubuf, umem2, ptr);
1021                char name[50];
1022                snprintf(name, sizeof(name), "ubuf_%d", i);
1023                /* The blob uses ubuf 0 to upload internal stuff and
1024                 * uniforms that won't fit/are accessed indirectly, so
1025                 * it puts it in the batchbuffer.
1026                 */
1027                pandecode_log("uint32_t %s_%d[] = {\n", name, job_no);
1028                pandecode_indent++;
1029
1030                for (int j = 0; j <= ubufs[i].size; j++) {
1031                        for (int k = 0; k < 4; k++) {
1032                                if (k == 0)
1033                                        pandecode_log("0x%"PRIx32", ", ubuf[4 * j + k]);
1034                                else
1035                                        pandecode_log_cont("0x%"PRIx32", ", ubuf[4 * j + k]);
1036
1037                        }
1038
1039                        pandecode_log_cont("\n");
1040                }
1041
1042                pandecode_indent--;
1043                pandecode_log("};\n");
1044        }
1045
1046        pandecode_log("struct mali_uniform_buffer_meta uniform_buffers_%d[] = {\n",
1047                    job_no);
1048        pandecode_indent++;
1049
1050        for (int i = 0; i < ubufs_count; i++) {
1051                pandecode_log("{\n");
1052                pandecode_indent++;
1053                pandecode_prop("size = MALI_POSITIVE(%d)", ubufs[i].size + 1);
1054                pandecode_prop("ptr = ubuf_%d_%d_p >> 2", i, job_no);
1055                pandecode_indent--;
1056                pandecode_log("},\n");
1057        }
1058
1059        pandecode_indent--;
1060        pandecode_log("};\n");
1061}
1062
1063static void
1064pandecode_replay_scratchpad(uintptr_t pscratchpad, int job_no, char *suffix)
1065{
1066
1067        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(pscratchpad);
1068
1069        struct bifrost_scratchpad *PANDECODE_PTR_VAR(scratchpad, mem, pscratchpad);
1070
1071        if (scratchpad->zero)
1072                pandecode_msg("XXX scratchpad zero tripped");
1073
1074        pandecode_log("struct bifrost_scratchpad scratchpad_%d%s = {\n", job_no, suffix);
1075        pandecode_indent++;
1076
1077        pandecode_prop("flags = 0x%x", scratchpad->flags);
1078        MEMORY_PROP(scratchpad, gpu_scratchpad);
1079
1080        pandecode_indent--;
1081        pandecode_log("};\n");
1082}
1083
1084static void
1085pandecode_shader_disassemble(mali_ptr shader_ptr, int shader_no, int type,
1086                           bool is_bifrost)
1087{
1088        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(shader_ptr);
1089        uint8_t *PANDECODE_PTR_VAR(code, mem, shader_ptr);
1090
1091        /* Compute maximum possible size */
1092        size_t sz = mem->length - (shader_ptr - mem->gpu_va);
1093
1094        /* TODO: When Bifrost is upstreamed, disassemble that too */
1095        if (is_bifrost) {
1096                pandecode_msg("Bifrost disassembler not yet upstreamed");
1097                return;
1098        }
1099
1100        /* Print some boilerplate to clearly denote the assembly (which doesn't
1101         * obey indentation rules), and actually do the disassembly! */
1102
1103        printf("\n\n");
1104        disassemble_midgard(code, sz);
1105        printf("\n\n");
1106}
1107
1108static void
1109pandecode_replay_vertex_tiler_postfix_pre(const struct mali_vertex_tiler_postfix *p,
1110                                        int job_no, enum mali_job_type job_type,
1111                                        char *suffix, bool is_bifrost)
1112{
1113        mali_ptr shader_meta_ptr = (u64) (uintptr_t) (p->_shader_upper << 4);
1114        struct pandecode_mapped_memory *attr_mem;
1115
1116        /* On Bifrost, since the tiler heap (for tiler jobs) and the scratchpad
1117         * are the only things actually needed from the FBD, vertex/tiler jobs
1118         * no longer reference the FBD -- instead, this field points to some
1119         * info about the scratchpad.
1120         */
1121        if (is_bifrost)
1122                pandecode_replay_scratchpad(p->framebuffer & ~FBD_TYPE, job_no, suffix);
1123        else if (p->framebuffer & MALI_MFBD)
1124                pandecode_replay_mfbd_bfr((u64) ((uintptr_t) p->framebuffer) & FBD_MASK, job_no);
1125        else
1126                pandecode_replay_sfbd((u64) (uintptr_t) p->framebuffer, job_no);
1127
1128        int varying_count = 0, attribute_count = 0, uniform_count = 0, uniform_buffer_count = 0;
1129        int texture_count = 0, sampler_count = 0;
1130
1131        if (shader_meta_ptr) {
1132                struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(shader_meta_ptr);
1133                struct mali_shader_meta *PANDECODE_PTR_VAR(s, smem, shader_meta_ptr);
1134
1135                pandecode_log("struct mali_shader_meta shader_meta_%d%s = {\n", job_no, suffix);
1136                pandecode_indent++;
1137
1138                /* Save for dumps */
1139                attribute_count = s->attribute_count;
1140                varying_count = s->varying_count;
1141                texture_count = s->texture_count;
1142                sampler_count = s->sampler_count;
1143
1144                if (is_bifrost) {
1145                        uniform_count = s->bifrost2.uniform_count;
1146                        uniform_buffer_count = s->bifrost1.uniform_buffer_count;
1147                } else {
1148                        uniform_count = s->midgard1.uniform_count;
1149                        /* TODO figure this out */
1150                        uniform_buffer_count = 1;
1151                }
1152
1153                mali_ptr shader_ptr = pandecode_replay_shader_address("shader", s->shader);
1154
1155                pandecode_prop("texture_count = %" PRId16, s->texture_count);
1156                pandecode_prop("sampler_count = %" PRId16, s->sampler_count);
1157                pandecode_prop("attribute_count = %" PRId16, s->attribute_count);
1158                pandecode_prop("varying_count = %" PRId16, s->varying_count);
1159
1160                if (is_bifrost) {
1161                        pandecode_log(".bifrost1 = {\n");
1162                        pandecode_indent++;
1163
1164                        pandecode_prop("uniform_buffer_count = %" PRId32, s->bifrost1.uniform_buffer_count);
1165                        pandecode_prop("unk1 = 0x%" PRIx32, s->bifrost1.unk1);
1166
1167                        pandecode_indent--;
1168                        pandecode_log("},\n");
1169                } else {
1170                        pandecode_log(".midgard1 = {\n");
1171                        pandecode_indent++;
1172
1173                        pandecode_prop("uniform_count = %" PRId16, s->midgard1.uniform_count);
1174                        pandecode_prop("work_count = %" PRId16, s->midgard1.work_count);
1175
1176                        pandecode_log(".unknown1 = ");
1177                        pandecode_log_decoded_flags(shader_unknown1_flag_info, s->midgard1.unknown1);
1178                        pandecode_log_cont(",\n");
1179
1180                        pandecode_prop("unknown2 = 0x%" PRIx32, s->midgard1.unknown2);
1181
1182                        pandecode_indent--;
1183                        pandecode_log("},\n");
1184                }
1185
1186                if (s->depth_units || s->depth_factor) {
1187                        if (is_bifrost)
1188                                pandecode_prop("depth_units = %f", s->depth_units);
1189                        else
1190                                pandecode_prop("depth_units = MALI_NEGATIVE(%f)", s->depth_units - 1.0f);
1191
1192                        pandecode_prop("depth_factor = %f", s->depth_factor);
1193                }
1194
1195                bool invert_alpha_coverage = s->alpha_coverage & 0xFFF0;
1196                uint16_t inverted_coverage = invert_alpha_coverage ? ~s->alpha_coverage : s->alpha_coverage;
1197
1198                pandecode_prop("alpha_coverage = %sMALI_ALPHA_COVERAGE(%f)",
1199                             invert_alpha_coverage ? "~" : "",
1200                             MALI_GET_ALPHA_COVERAGE(inverted_coverage));
1201
1202                pandecode_log(".unknown2_3 = ");
1203
1204                int unknown2_3 = s->unknown2_3;
1205                int unknown2_4 = s->unknown2_4;
1206
1207                /* We're not quite sure what these flags mean without the depth test, if anything */
1208
1209                if (unknown2_3 & (MALI_DEPTH_TEST | MALI_DEPTH_FUNC_MASK)) {
1210                        const char *func = pandecode_func_name(MALI_GET_DEPTH_FUNC(unknown2_3));
1211                        unknown2_3 &= ~MALI_DEPTH_FUNC_MASK;
1212
1213                        pandecode_log_cont("MALI_DEPTH_FUNC(%s) | ", func);
1214                }
1215
1216                pandecode_log_decoded_flags(u3_flag_info, unknown2_3);
1217                pandecode_log_cont(",\n");
1218
1219                pandecode_prop("stencil_mask_front = 0x%02X", s->stencil_mask_front);
1220                pandecode_prop("stencil_mask_back = 0x%02X", s->stencil_mask_back);
1221
1222                pandecode_log(".unknown2_4 = ");
1223                pandecode_log_decoded_flags(u4_flag_info, unknown2_4);
1224                pandecode_log_cont(",\n");
1225
1226                pandecode_replay_stencil("front", &s->stencil_front);
1227                pandecode_replay_stencil("back", &s->stencil_back);
1228
1229                if (is_bifrost) {
1230                        pandecode_log(".bifrost2 = {\n");
1231                        pandecode_indent++;
1232
1233                        pandecode_prop("unk3 = 0x%" PRIx32, s->bifrost2.unk3);
1234                        pandecode_prop("preload_regs = 0x%" PRIx32, s->bifrost2.preload_regs);
1235                        pandecode_prop("uniform_count = %" PRId32, s->bifrost2.uniform_count);
1236                        pandecode_prop("unk4 = 0x%" PRIx32, s->bifrost2.unk4);
1237
1238                        pandecode_indent--;
1239                        pandecode_log("},\n");
1240                } else {
1241                        pandecode_log(".midgard2 = {\n");
1242                        pandecode_indent++;
1243
1244                        pandecode_prop("unknown2_7 = 0x%" PRIx32, s->midgard2.unknown2_7);
1245                        pandecode_indent--;
1246                        pandecode_log("},\n");
1247                }
1248
1249                pandecode_prop("unknown2_8 = 0x%" PRIx32, s->unknown2_8);
1250
1251                bool blend_shader = false;
1252
1253                if (!is_bifrost) {
1254                        if (s->unknown2_3 & MALI_HAS_BLEND_SHADER) {
1255                                blend_shader = true;
1256                                pandecode_replay_shader_address("blend_shader", s->blend_shader);
1257                        } else {
1258                                pandecode_replay_blend_equation(&s->blend_equation, "");
1259                        }
1260                }
1261
1262                pandecode_indent--;
1263                pandecode_log("};\n");
1264
1265                /* MRT blend fields are used whenever MFBD is used */
1266
1267                if (job_type == JOB_TYPE_TILER) {
1268                        pandecode_log("struct mali_blend_meta blend_meta_%d[] = {\n",
1269                                    job_no);
1270                        pandecode_indent++;
1271
1272                        int i;
1273
1274                        for (i = 0; i < 4; i++) {
1275                                const struct mali_blend_meta *b = &s->blend_meta[i];
1276                                pandecode_log("{\n");
1277                                pandecode_indent++;
1278
1279#ifndef BIFROST
1280                                pandecode_prop("unk1 = 0x%" PRIx64, b->unk1);
1281
1282                                /* Depending on unk1, we determine if there's a
1283                                 * blend shader */
1284
1285                                if ((b->unk1 & 0xF) >= 0x2) {
1286                                        blend_shader = true;
1287                                        pandecode_replay_shader_address("blend_shader", b->blend_shader);
1288                                } else {
1289                                        pandecode_replay_blend_equation(&b->blend_equation_1, "_1");
1290                                }
1291
1292                                /* This is always an equation, I think. If
1293                                 * there's a shader, it just defaults to
1294                                 * REPLACE (0x122) */
1295                                pandecode_replay_blend_equation(&b->blend_equation_2, "_2");
1296
1297                                if (b->zero2) {
1298                                        pandecode_msg("blend zero tripped\n");
1299                                        pandecode_prop("zero2 = 0x%x", b->zero2);
1300                                }
1301
1302#else
1303
1304                                pandecode_prop("unk1 = 0x%" PRIx32, b->unk1);
1305                                /* TODO figure out blend shader enable bit */
1306                                pandecode_replay_blend_equation(&b->blend_equation);
1307                                pandecode_prop("unk2 = 0x%" PRIx16, b->unk2);
1308                                pandecode_prop("index = 0x%" PRIx16, b->index);
1309                                pandecode_prop("unk3 = 0x%" PRIx32, b->unk3);
1310#endif
1311
1312                                pandecode_indent--;
1313                                pandecode_log("},\n");
1314
1315#ifdef BIFROST
1316                                if (b->unk2 == 3)
1317                                        break;
1318#else
1319                                /* TODO: What's this supposed to be? */
1320                                if (b->unk1 & 0x200)
1321                                        break;
1322#endif
1323
1324                        }
1325
1326                        pandecode_indent--;
1327                        pandecode_log("};\n");
1328
1329                        /* This needs to be uploaded right after the
1330                         * shader_meta since it's technically part of the same
1331                         * (variable-size) structure.
1332                         */
1333                }
1334
1335                pandecode_shader_disassemble(shader_ptr, job_no, job_type, is_bifrost);
1336
1337                if (!is_bifrost && blend_shader)
1338                        pandecode_shader_disassemble(s->blend_shader & ~0xF, job_no, job_type, false);
1339
1340        } else
1341                pandecode_msg("<no shader>\n");
1342
1343        if (p->viewport) {
1344                struct pandecode_mapped_memory *fmem = pandecode_find_mapped_gpu_mem_containing(p->viewport);
1345                struct mali_viewport *PANDECODE_PTR_VAR(f, fmem, p->viewport);
1346
1347                pandecode_log("struct mali_viewport viewport_%d%s = {\n", job_no, suffix);
1348                pandecode_indent++;
1349
1350                pandecode_prop("clip_minx = %f", f->clip_minx);
1351                pandecode_prop("clip_miny = %f", f->clip_miny);
1352                pandecode_prop("clip_minz = %f", f->clip_minz);
1353                pandecode_prop("clip_maxx = %f", f->clip_maxx);
1354                pandecode_prop("clip_maxy = %f", f->clip_maxy);
1355                pandecode_prop("clip_maxz = %f", f->clip_maxz);
1356
1357                /* Only the higher coordinates are MALI_POSITIVE scaled */
1358
1359                pandecode_prop("viewport0 = { %d, %d }",
1360                             f->viewport0[0], f->viewport0[1]);
1361
1362                pandecode_prop("viewport1 = { MALI_POSITIVE(%d), MALI_POSITIVE(%d) }",
1363                             f->viewport1[0] + 1, f->viewport1[1] + 1);
1364
1365                pandecode_indent--;
1366                pandecode_log("};\n");
1367        }
1368
1369        if (p->attribute_meta) {
1370                unsigned max_attr_index = pandecode_replay_attribute_meta(job_no, attribute_count, p, false, suffix);
1371
1372                attr_mem = pandecode_find_mapped_gpu_mem_containing(p->attributes);
1373                pandecode_replay_attributes(attr_mem, p->attributes, job_no, suffix, max_attr_index + 1, false);
1374        }
1375
1376        /* Varyings are encoded like attributes but not actually sent; we just
1377         * pass a zero buffer with the right stride/size set, (or whatever)
1378         * since the GPU will write to it itself */
1379
1380        if (p->varyings) {
1381                attr_mem = pandecode_find_mapped_gpu_mem_containing(p->varyings);
1382
1383                /* Number of descriptors depends on whether there are
1384                 * non-internal varyings */
1385
1386                pandecode_replay_attributes(attr_mem, p->varyings, job_no, suffix, varying_count > 1 ? 4 : 1, true);
1387        }
1388
1389        if (p->varying_meta) {
1390                pandecode_replay_attribute_meta(job_no, varying_count, p, true, suffix);
1391        }
1392
1393        if (p->uniforms) {
1394                int rows = uniform_count, width = 4;
1395                size_t sz = rows * width * sizeof(float);
1396
1397                struct pandecode_mapped_memory *uniform_mem = pandecode_find_mapped_gpu_mem_containing(p->uniforms);
1398                pandecode_fetch_gpu_mem(uniform_mem, p->uniforms, sz);
1399                float *PANDECODE_PTR_VAR(uniforms, uniform_mem, p->uniforms);
1400
1401                pandecode_log("float uniforms_%d%s[] = {\n", job_no, suffix);
1402
1403                pandecode_indent++;
1404
1405                for (int row = 0; row < rows; row++) {
1406                        for (int i = 0; i < width; i++)
1407                                pandecode_log_cont("%ff, ", uniforms[i]);
1408
1409                        pandecode_log_cont("\n");
1410
1411                        uniforms += width;
1412                }
1413
1414                pandecode_indent--;
1415                pandecode_log("};\n");
1416        }
1417
1418        if (p->uniform_buffers) {
1419                pandecode_replay_uniform_buffers(p->uniform_buffers, uniform_buffer_count, job_no);
1420        }
1421
1422        if (p->texture_trampoline) {
1423                struct pandecode_mapped_memory *mmem = pandecode_find_mapped_gpu_mem_containing(p->texture_trampoline);
1424
1425                if (mmem) {
1426                        mali_ptr *PANDECODE_PTR_VAR(u, mmem, p->texture_trampoline);
1427
1428                        pandecode_log("uint64_t texture_trampoline_%d[] = {\n", job_no);
1429                        pandecode_indent++;
1430
1431                        for (int tex = 0; tex < texture_count; ++tex) {
1432                                mali_ptr *PANDECODE_PTR_VAR(u, mmem, p->texture_trampoline + tex * sizeof(mali_ptr));
1433                                char *a = pointer_as_memory_reference(*u);
1434                                pandecode_log("%s,\n", a);
1435                                free(a);
1436                        }
1437
1438                        pandecode_indent--;
1439                        pandecode_log("};\n");
1440
1441                        /* Now, finally, descend down into the texture descriptor */
1442                        for (int tex = 0; tex < texture_count; ++tex) {
1443                                mali_ptr *PANDECODE_PTR_VAR(u, mmem, p->texture_trampoline + tex * sizeof(mali_ptr));
1444                                struct pandecode_mapped_memory *tmem = pandecode_find_mapped_gpu_mem_containing(*u);
1445
1446                                if (tmem) {
1447                                        struct mali_texture_descriptor *PANDECODE_PTR_VAR(t, tmem, *u);
1448
1449                                        pandecode_log("struct mali_texture_descriptor texture_descriptor_%d_%d = {\n", job_no, tex);
1450                                        pandecode_indent++;
1451
1452                                        pandecode_prop("width = MALI_POSITIVE(%" PRId16 ")", t->width + 1);
1453                                        pandecode_prop("height = MALI_POSITIVE(%" PRId16 ")", t->height + 1);
1454                                        pandecode_prop("depth = MALI_POSITIVE(%" PRId16 ")", t->depth + 1);
1455
1456                                        pandecode_prop("unknown3 = %" PRId16, t->unknown3);
1457                                        pandecode_prop("unknown3A = %" PRId8, t->unknown3A);
1458                                        pandecode_prop("nr_mipmap_levels = %" PRId8, t->nr_mipmap_levels);
1459
1460                                        struct mali_texture_format f = t->format;
1461
1462                                        pandecode_log(".format = {\n");
1463                                        pandecode_indent++;
1464
1465                                        pandecode_replay_swizzle(f.swizzle);
1466					pandecode_prop("format = %s", pandecode_format_name(f.format));
1467
1468                                        pandecode_prop("usage1 = 0x%" PRIx32, f.usage1);
1469                                        pandecode_prop("is_not_cubemap = %" PRId32, f.is_not_cubemap);
1470                                        pandecode_prop("usage2 = 0x%" PRIx32, f.usage2);
1471
1472                                        pandecode_indent--;
1473                                        pandecode_log("},\n");
1474
1475					pandecode_replay_swizzle(t->swizzle);
1476
1477                                        if (t->swizzle_zero) {
1478                                                /* Shouldn't happen */
1479                                                pandecode_msg("Swizzle zero tripped but replay will be fine anyway");
1480                                                pandecode_prop("swizzle_zero = %d", t->swizzle_zero);
1481                                        }
1482
1483                                        pandecode_prop("unknown3 = 0x%" PRIx32, t->unknown3);
1484
1485                                        pandecode_prop("unknown5 = 0x%" PRIx32, t->unknown5);
1486                                        pandecode_prop("unknown6 = 0x%" PRIx32, t->unknown6);
1487                                        pandecode_prop("unknown7 = 0x%" PRIx32, t->unknown7);
1488
1489                                        pandecode_log(".swizzled_bitmaps = {\n");
1490                                        pandecode_indent++;
1491
1492                                        int bitmap_count = MALI_NEGATIVE(t->nr_mipmap_levels);
1493
1494                                        if (!f.is_not_cubemap) {
1495                                                /* Miptree for each face */
1496                                                bitmap_count *= 6;
1497                                        }
1498
1499                                        int max_count = sizeof(t->swizzled_bitmaps) / sizeof(t->swizzled_bitmaps[0]);
1500
1501                                        if (bitmap_count > max_count) {
1502                                                pandecode_msg("XXX: bitmap count tripped");
1503                                                bitmap_count = max_count;
1504                                        }
1505
1506                                        for (int i = 0; i < bitmap_count; ++i) {
1507                                                char *a = pointer_as_memory_reference(t->swizzled_bitmaps[i]);
1508                                                pandecode_log("%s, \n", a);
1509                                                free(a);
1510                                        }
1511
1512                                        pandecode_indent--;
1513                                        pandecode_log("},\n");
1514
1515                                        pandecode_indent--;
1516                                        pandecode_log("};\n");
1517                                }
1518                        }
1519                }
1520        }
1521
1522        if (p->sampler_descriptor) {
1523                struct pandecode_mapped_memory *smem = pandecode_find_mapped_gpu_mem_containing(p->sampler_descriptor);
1524
1525                if (smem) {
1526                        struct mali_sampler_descriptor *s;
1527
1528                        mali_ptr d = p->sampler_descriptor;
1529
1530                        for (int i = 0; i < sampler_count; ++i) {
1531                                s = pandecode_fetch_gpu_mem(smem, d + sizeof(*s) * i, sizeof(*s));
1532
1533                                pandecode_log("struct mali_sampler_descriptor sampler_descriptor_%d_%d = {\n", job_no, i);
1534                                pandecode_indent++;
1535
1536                                /* Only the lower two bits are understood right now; the rest we display as hex */
1537                                pandecode_log(".filter_mode = MALI_TEX_MIN(%s) | MALI_TEX_MAG(%s) | 0x%" PRIx32",\n",
1538                                            MALI_FILTER_NAME(s->filter_mode & MALI_TEX_MIN_MASK),
1539                                            MALI_FILTER_NAME(s->filter_mode & MALI_TEX_MAG_MASK),
1540                                            s->filter_mode & ~3);
1541
1542                                pandecode_prop("min_lod = FIXED_16(%f)", DECODE_FIXED_16(s->min_lod));
1543                                pandecode_prop("max_lod = FIXED_16(%f)", DECODE_FIXED_16(s->max_lod));
1544
1545                                pandecode_prop("wrap_s = %s", pandecode_wrap_mode_name(s->wrap_s));
1546                                pandecode_prop("wrap_t = %s", pandecode_wrap_mode_name(s->wrap_t));
1547                                pandecode_prop("wrap_r = %s", pandecode_wrap_mode_name(s->wrap_r));
1548
1549                                pandecode_prop("compare_func = %s", pandecode_alt_func_name(s->compare_func));
1550
1551                                if (s->zero || s->zero2) {
1552                                        pandecode_msg("Zero tripped\n");
1553                                        pandecode_prop("zero = 0x%X, 0x%X\n", s->zero, s->zero2);
1554                                }
1555
1556                                pandecode_prop("unknown2 = %d", s->unknown2);
1557
1558                                pandecode_prop("border_color = { %f, %f, %f, %f }",
1559                                             s->border_color[0],
1560                                             s->border_color[1],
1561                                             s->border_color[2],
1562                                             s->border_color[3]);
1563
1564                                pandecode_indent--;
1565                                pandecode_log("};\n");
1566                        }
1567                }
1568        }
1569}
1570
1571static void
1572pandecode_replay_vertex_tiler_postfix(const struct mali_vertex_tiler_postfix *p, int job_no, bool is_bifrost)
1573{
1574        pandecode_log_cont("{\n");
1575        pandecode_indent++;
1576
1577        MEMORY_PROP(p, position_varying);
1578        MEMORY_COMMENT(p, position_varying);
1579        DYN_MEMORY_PROP(p, job_no, uniform_buffers);
1580        MEMORY_COMMENT(p, uniform_buffers);
1581        DYN_MEMORY_PROP(p, job_no, texture_trampoline);
1582        MEMORY_COMMENT(p, texture_trampoline);
1583        DYN_MEMORY_PROP(p, job_no, sampler_descriptor);
1584        MEMORY_COMMENT(p, sampler_descriptor);
1585        DYN_MEMORY_PROP(p, job_no, uniforms);
1586        MEMORY_COMMENT(p, uniforms);
1587        DYN_MEMORY_PROP(p, job_no, attributes);
1588        MEMORY_COMMENT(p, attributes);
1589        DYN_MEMORY_PROP(p, job_no, attribute_meta);
1590        MEMORY_COMMENT(p, attribute_meta);
1591        DYN_MEMORY_PROP(p, job_no, varyings);
1592        MEMORY_COMMENT(p, varyings);
1593        DYN_MEMORY_PROP(p, job_no, varying_meta);
1594        MEMORY_COMMENT(p, varying_meta);
1595        DYN_MEMORY_PROP(p, job_no, viewport);
1596        MEMORY_COMMENT(p, viewport);
1597        DYN_MEMORY_PROP(p, job_no, occlusion_counter);
1598        MEMORY_COMMENT(p, occlusion_counter);
1599        MEMORY_COMMENT(p, framebuffer & ~1);
1600        pandecode_msg("%" PRIx64 "\n", p->viewport);
1601        pandecode_msg("%" PRIx64 "\n", p->framebuffer);
1602
1603        if (is_bifrost)
1604                pandecode_prop("framebuffer = scratchpad_%d_p", job_no);
1605        else
1606                pandecode_prop("framebuffer = framebuffer_%d_p | %s", job_no, p->framebuffer & MALI_MFBD ? "MALI_MFBD" : "0");
1607
1608        pandecode_prop("_shader_upper = (shader_meta_%d_p) >> 4", job_no);
1609        pandecode_prop("flags = %d", p->flags);
1610
1611        pandecode_indent--;
1612        pandecode_log("},\n");
1613}
1614
1615static void
1616pandecode_replay_vertex_only_bfr(struct bifrost_vertex_only *v)
1617{
1618        pandecode_log_cont("{\n");
1619        pandecode_indent++;
1620
1621        pandecode_prop("unk2 = 0x%x", v->unk2);
1622
1623        if (v->zero0 || v->zero1) {
1624                pandecode_msg("vertex only zero tripped");
1625                pandecode_prop("zero0 = 0x%" PRIx32, v->zero0);
1626                pandecode_prop("zero1 = 0x%" PRIx64, v->zero1);
1627        }
1628
1629        pandecode_indent--;
1630        pandecode_log("}\n");
1631}
1632
1633static void
1634pandecode_replay_tiler_heap_meta(mali_ptr gpu_va, int job_no)
1635{
1636
1637        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
1638        const struct bifrost_tiler_heap_meta *PANDECODE_PTR_VAR(h, mem, gpu_va);
1639
1640        pandecode_log("struct mali_tiler_heap_meta tiler_heap_meta_%d = {\n", job_no);
1641        pandecode_indent++;
1642
1643        if (h->zero) {
1644                pandecode_msg("tiler heap zero tripped\n");
1645                pandecode_prop("zero = 0x%x", h->zero);
1646        }
1647
1648        for (int i = 0; i < 12; i++) {
1649                if (h->zeros[i] != 0) {
1650                        pandecode_msg("tiler heap zero %d tripped, value %x\n",
1651                                    i, h->zeros[i]);
1652                }
1653        }
1654
1655        pandecode_prop("heap_size = 0x%x", h->heap_size);
1656        MEMORY_PROP(h, tiler_heap_start);
1657        MEMORY_PROP(h, tiler_heap_free);
1658
1659        /* this might point to the beginning of another buffer, when it's
1660         * really the end of the tiler heap buffer, so we have to be careful
1661         * here.
1662         */
1663        char *a = pointer_as_memory_reference(h->tiler_heap_end - 1);
1664        pandecode_prop("tiler_heap_end = %s + 1", a);
1665        free(a);
1666
1667        pandecode_indent--;
1668        pandecode_log("};\n");
1669}
1670
1671static void
1672pandecode_replay_tiler_meta(mali_ptr gpu_va, int job_no)
1673{
1674        struct pandecode_mapped_memory *mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
1675        const struct bifrost_tiler_meta *PANDECODE_PTR_VAR(t, mem, gpu_va);
1676
1677        pandecode_replay_tiler_heap_meta(t->tiler_heap_meta, job_no);
1678
1679        pandecode_log("struct mali_tiler_meta tiler_meta_%d = {\n", job_no);
1680        pandecode_indent++;
1681
1682        if (t->zero0 || t->zero1) {
1683                pandecode_msg("tiler meta zero tripped");
1684                pandecode_prop("zero0 = 0x%" PRIx64, t->zero0);
1685                pandecode_prop("zero1 = 0x%" PRIx64, t->zero1);
1686        }
1687
1688        pandecode_prop("unk = 0x%x", t->unk);
1689        pandecode_prop("width = MALI_POSITIVE(%d)", t->width + 1);
1690        pandecode_prop("height = MALI_POSITIVE(%d)", t->height + 1);
1691        DYN_MEMORY_PROP(t, job_no, tiler_heap_meta);
1692
1693        for (int i = 0; i < 12; i++) {
1694                if (t->zeros[i] != 0) {
1695                        pandecode_msg("tiler heap zero %d tripped, value %" PRIx64 "\n",
1696                                    i, t->zeros[i]);
1697                }
1698        }
1699
1700        pandecode_indent--;
1701        pandecode_log("};\n");
1702}
1703
1704static void
1705pandecode_replay_gl_enables(uint32_t gl_enables, int job_type)
1706{
1707        pandecode_log(".gl_enables = ");
1708
1709        if (job_type == JOB_TYPE_TILER) {
1710                pandecode_log_cont("MALI_FRONT_FACE(MALI_%s) | ",
1711                                 gl_enables & MALI_FRONT_FACE(MALI_CW) ? "CW" : "CCW");
1712
1713                gl_enables &= ~(MALI_FRONT_FACE(1));
1714        }
1715
1716        pandecode_log_decoded_flags(gl_enable_flag_info, gl_enables);
1717
1718        pandecode_log_cont(",\n");
1719}
1720
1721static void
1722pandecode_replay_primitive_size(union midgard_primitive_size u, bool constant)
1723{
1724        pandecode_log(".primitive_size = {\n");
1725        pandecode_indent++;
1726
1727        if (constant) {
1728                pandecode_prop("constant = %f", u.constant);
1729        } else {
1730                MEMORY_PROP((&u), pointer);
1731        }
1732
1733        pandecode_indent--;
1734        pandecode_log("},\n");
1735}
1736
1737static void
1738pandecode_replay_tiler_only_bfr(const struct bifrost_tiler_only *t, int job_no)
1739{
1740        pandecode_log_cont("{\n");
1741        pandecode_indent++;
1742
1743        /* TODO: gl_PointSize on Bifrost */
1744        pandecode_replay_primitive_size(t->primitive_size, true);
1745
1746        DYN_MEMORY_PROP(t, job_no, tiler_meta);
1747        pandecode_replay_gl_enables(t->gl_enables, JOB_TYPE_TILER);
1748
1749        if (t->zero1 || t->zero2 || t->zero3 || t->zero4 || t->zero5
1750                        || t->zero6 || t->zero7 || t->zero8) {
1751                pandecode_msg("tiler only zero tripped");
1752                pandecode_prop("zero1 = 0x%" PRIx64, t->zero1);
1753                pandecode_prop("zero2 = 0x%" PRIx64, t->zero2);
1754                pandecode_prop("zero3 = 0x%" PRIx64, t->zero3);
1755                pandecode_prop("zero4 = 0x%" PRIx64, t->zero4);
1756                pandecode_prop("zero5 = 0x%" PRIx64, t->zero5);
1757                pandecode_prop("zero6 = 0x%" PRIx64, t->zero6);
1758                pandecode_prop("zero7 = 0x%" PRIx32, t->zero7);
1759                pandecode_prop("zero8 = 0x%" PRIx64, t->zero8);
1760        }
1761
1762        pandecode_indent--;
1763        pandecode_log("},\n");
1764}
1765
1766static int
1767pandecode_replay_vertex_job_bfr(const struct mali_job_descriptor_header *h,
1768                              const struct pandecode_mapped_memory *mem,
1769                              mali_ptr payload, int job_no)
1770{
1771        struct bifrost_payload_vertex *PANDECODE_PTR_VAR(v, mem, payload);
1772
1773        pandecode_replay_vertex_tiler_postfix_pre(&v->postfix, job_no, h->job_type, "", true);
1774
1775        pandecode_log("struct bifrost_payload_vertex payload_%d = {\n", job_no);
1776        pandecode_indent++;
1777
1778        pandecode_log(".prefix = ");
1779        pandecode_replay_vertex_tiler_prefix(&v->prefix, job_no);
1780
1781        pandecode_log(".vertex = ");
1782        pandecode_replay_vertex_only_bfr(&v->vertex);
1783
1784        pandecode_log(".postfix = ");
1785        pandecode_replay_vertex_tiler_postfix(&v->postfix, job_no, true);
1786
1787        pandecode_indent--;
1788        pandecode_log("};\n");
1789
1790        return sizeof(*v);
1791}
1792
1793static int
1794pandecode_replay_tiler_job_bfr(const struct mali_job_descriptor_header *h,
1795                             const struct pandecode_mapped_memory *mem,
1796                             mali_ptr payload, int job_no)
1797{
1798        struct bifrost_payload_tiler *PANDECODE_PTR_VAR(t, mem, payload);
1799
1800        pandecode_replay_vertex_tiler_postfix_pre(&t->postfix, job_no, h->job_type, "", true);
1801
1802        pandecode_replay_indices(t->prefix.indices, t->prefix.index_count, job_no);
1803        pandecode_replay_tiler_meta(t->tiler.tiler_meta, job_no);
1804
1805        pandecode_log("struct bifrost_payload_tiler payload_%d = {\n", job_no);
1806        pandecode_indent++;
1807
1808        pandecode_log(".prefix = ");
1809        pandecode_replay_vertex_tiler_prefix(&t->prefix, job_no);
1810
1811        pandecode_log(".tiler = ");
1812        pandecode_replay_tiler_only_bfr(&t->tiler, job_no);
1813
1814        pandecode_log(".postfix = ");
1815        pandecode_replay_vertex_tiler_postfix(&t->postfix, job_no, true);
1816
1817        pandecode_indent--;
1818        pandecode_log("};\n");
1819
1820        return sizeof(*t);
1821}
1822
1823static int
1824pandecode_replay_vertex_or_tiler_job_mdg(const struct mali_job_descriptor_header *h,
1825                                       const struct pandecode_mapped_memory *mem,
1826                                       mali_ptr payload, int job_no)
1827{
1828        struct midgard_payload_vertex_tiler *PANDECODE_PTR_VAR(v, mem, payload);
1829
1830        char *a = pointer_as_memory_reference(payload);
1831        pandecode_msg("vt payload: %s\n", a);
1832        free(a);
1833
1834        pandecode_replay_vertex_tiler_postfix_pre(&v->postfix, job_no, h->job_type, "", false);
1835
1836        pandecode_replay_indices(v->prefix.indices, v->prefix.index_count, job_no);
1837
1838        pandecode_log("struct midgard_payload_vertex_tiler payload_%d = {\n", job_no);
1839        pandecode_indent++;
1840
1841        bool has_primitive_pointer = v->prefix.unknown_draw & MALI_DRAW_VARYING_SIZE;
1842        pandecode_replay_primitive_size(v->primitive_size, !has_primitive_pointer);
1843
1844        pandecode_log(".prefix = ");
1845        pandecode_replay_vertex_tiler_prefix(&v->prefix, job_no);
1846
1847        pandecode_replay_gl_enables(v->gl_enables, h->job_type);
1848        pandecode_prop("draw_start = %d", v->draw_start);
1849
1850#ifndef __LP64__
1851
1852        if (v->zero3) {
1853                pandecode_msg("Zero tripped\n");
1854                pandecode_prop("zero3 = 0x%" PRIx32, v->zero3);
1855        }
1856
1857#endif
1858
1859        if (v->zero5) {
1860                pandecode_msg("Zero tripped\n");
1861                pandecode_prop("zero5 = 0x%" PRIx64, v->zero5);
1862        }
1863
1864        pandecode_log(".postfix = ");
1865        pandecode_replay_vertex_tiler_postfix(&v->postfix, job_no, false);
1866
1867        pandecode_indent--;
1868        pandecode_log("};\n");
1869
1870        return sizeof(*v);
1871}
1872
1873static int
1874pandecode_replay_fragment_job(const struct pandecode_mapped_memory *mem,
1875                            mali_ptr payload, int job_no,
1876                            bool is_bifrost)
1877{
1878        const struct mali_payload_fragment *PANDECODE_PTR_VAR(s, mem, payload);
1879
1880        bool fbd_dumped = false;
1881
1882        if (!is_bifrost && (s->framebuffer & FBD_TYPE) == MALI_SFBD) {
1883                /* Only SFBDs are understood, not MFBDs. We're speculating,
1884                 * based on the versioning, kernel code, etc, that the
1885                 * difference is between Single FrameBuffer Descriptor and
1886                 * Multiple FrmaeBuffer Descriptor; the change apparently lines
1887                 * up with multi-framebuffer support being added (T7xx onwards,
1888                 * including Gxx). In any event, there's some field shuffling
1889                 * that we haven't looked into yet. */
1890
1891                pandecode_replay_sfbd(s->framebuffer & FBD_MASK, job_no);
1892                fbd_dumped = true;
1893        } else if ((s->framebuffer & FBD_TYPE) == MALI_MFBD) {
1894                /* We don't know if Bifrost supports SFBD's at all, since the
1895                 * driver never uses them. And the format is different from
1896                 * Midgard anyways, due to the tiler heap and scratchpad being
1897                 * moved out into separate structures, so it's not clear what a
1898                 * Bifrost SFBD would even look like without getting an actual
1899                 * trace, which appears impossible.
1900                 */
1901
1902                pandecode_replay_mfbd_bfr(s->framebuffer & FBD_MASK, job_no);
1903                fbd_dumped = true;
1904        }
1905
1906        uintptr_t p = (uintptr_t) s->framebuffer & FBD_MASK;
1907
1908        pandecode_log("struct mali_payload_fragment payload_%d = {\n", job_no);
1909        pandecode_indent++;
1910
1911        /* See the comments by the macro definitions for mathematical context
1912         * on why this is so weird */
1913
1914        if (MALI_TILE_COORD_FLAGS(s->max_tile_coord) || MALI_TILE_COORD_FLAGS(s->min_tile_coord))
1915                pandecode_msg("Tile coordinate flag missed, replay wrong\n");
1916
1917        pandecode_prop("min_tile_coord = MALI_COORDINATE_TO_TILE_MIN(%d, %d)",
1918                     MALI_TILE_COORD_X(s->min_tile_coord) << MALI_TILE_SHIFT,
1919                     MALI_TILE_COORD_Y(s->min_tile_coord) << MALI_TILE_SHIFT);
1920
1921        pandecode_prop("max_tile_coord = MALI_COORDINATE_TO_TILE_MAX(%d, %d)",
1922                     (MALI_TILE_COORD_X(s->max_tile_coord) + 1) << MALI_TILE_SHIFT,
1923                     (MALI_TILE_COORD_Y(s->max_tile_coord) + 1) << MALI_TILE_SHIFT);
1924
1925        /* If the FBD was just decoded, we can refer to it by pointer. If not,
1926         * we have to fallback on offsets. */
1927
1928        const char *fbd_type = s->framebuffer & MALI_MFBD ? "MALI_MFBD" : "MALI_SFBD";
1929
1930        if (fbd_dumped)
1931                pandecode_prop("framebuffer = framebuffer_%d_p | %s", job_no, fbd_type);
1932        else
1933                pandecode_prop("framebuffer = %s | %s", pointer_as_memory_reference(p), fbd_type);
1934
1935        pandecode_indent--;
1936        pandecode_log("};\n");
1937
1938        return sizeof(*s);
1939}
1940
1941static int job_descriptor_number = 0;
1942
1943int
1944pandecode_replay_jc(mali_ptr jc_gpu_va, bool bifrost)
1945{
1946        struct mali_job_descriptor_header *h;
1947
1948        int start_number = 0;
1949
1950        bool first = true;
1951        bool last_size;
1952
1953        do {
1954                struct pandecode_mapped_memory *mem =
1955                        pandecode_find_mapped_gpu_mem_containing(jc_gpu_va);
1956
1957                void *payload;
1958
1959                h = PANDECODE_PTR(mem, jc_gpu_va, struct mali_job_descriptor_header);
1960
1961                /* On Midgard, for 32-bit jobs except for fragment jobs, the
1962                 * high 32-bits of the 64-bit pointer are reused to store
1963                 * something else.
1964                 */
1965                int offset = h->job_descriptor_size == MALI_JOB_32 &&
1966                             h->job_type != JOB_TYPE_FRAGMENT ? 4 : 0;
1967                mali_ptr payload_ptr = jc_gpu_va + sizeof(*h) - offset;
1968
1969                payload = pandecode_fetch_gpu_mem(mem, payload_ptr,
1970                                                MALI_PAYLOAD_SIZE);
1971
1972                int job_no = job_descriptor_number++;
1973
1974                if (first)
1975                        start_number = job_no;
1976
1977                pandecode_log("struct mali_job_descriptor_header job_%d = {\n", job_no);
1978                pandecode_indent++;
1979
1980                pandecode_prop("job_type = %s", pandecode_job_type_name(h->job_type));
1981
1982                /* Save for next job fixing */
1983                last_size = h->job_descriptor_size;
1984
1985                if (h->job_descriptor_size)
1986                        pandecode_prop("job_descriptor_size = %d", h->job_descriptor_size);
1987
1988                if (h->exception_status)
1989                        pandecode_prop("exception_status = %d", h->exception_status);
1990
1991                if (h->first_incomplete_task)
1992                        pandecode_prop("first_incomplete_task = %d", h->first_incomplete_task);
1993
1994                if (h->fault_pointer)
1995                        pandecode_prop("fault_pointer = 0x%" PRIx64, h->fault_pointer);
1996
1997                if (h->job_barrier)
1998                        pandecode_prop("job_barrier = %d", h->job_barrier);
1999
2000                pandecode_prop("job_index = %d", h->job_index);
2001
2002                if (h->unknown_flags)
2003                        pandecode_prop("unknown_flags = %d", h->unknown_flags);
2004
2005                if (h->job_dependency_index_1)
2006                        pandecode_prop("job_dependency_index_1 = %d", h->job_dependency_index_1);
2007
2008                if (h->job_dependency_index_2)
2009                        pandecode_prop("job_dependency_index_2 = %d", h->job_dependency_index_2);
2010
2011                pandecode_indent--;
2012                pandecode_log("};\n");
2013
2014                /* Do not touch the field yet -- decode the payload first, and
2015                 * don't touch that either. This is essential for the uploads
2016                 * to occur in sequence and therefore be dynamically allocated
2017                 * correctly. Do note the size, however, for that related
2018                 * reason. */
2019
2020                switch (h->job_type) {
2021                case JOB_TYPE_SET_VALUE: {
2022                        struct mali_payload_set_value *s = payload;
2023
2024                        pandecode_log("struct mali_payload_set_value payload_%d = {\n", job_no);
2025                        pandecode_indent++;
2026                        MEMORY_PROP(s, out);
2027                        pandecode_prop("unknown = 0x%" PRIX64, s->unknown);
2028                        pandecode_indent--;
2029                        pandecode_log("};\n");
2030
2031                        break;
2032                }
2033
2034                case JOB_TYPE_TILER:
2035                case JOB_TYPE_VERTEX:
2036                case JOB_TYPE_COMPUTE:
2037                        if (bifrost) {
2038                                if (h->job_type == JOB_TYPE_TILER)
2039                                        pandecode_replay_tiler_job_bfr(h, mem, payload_ptr, job_no);
2040                                else
2041                                        pandecode_replay_vertex_job_bfr(h, mem, payload_ptr, job_no);
2042                        } else
2043                                pandecode_replay_vertex_or_tiler_job_mdg(h, mem, payload_ptr, job_no);
2044
2045                        break;
2046
2047                case JOB_TYPE_FRAGMENT:
2048                        pandecode_replay_fragment_job(mem, payload_ptr, job_no, bifrost);
2049                        break;
2050
2051                default:
2052                        break;
2053                }
2054
2055                /* Handle linkage */
2056
2057                if (!first) {
2058                        pandecode_log("((struct mali_job_descriptor_header *) (uintptr_t) job_%d_p)->", job_no - 1);
2059
2060                        if (last_size)
2061                                pandecode_log_cont("next_job_64 = job_%d_p;\n\n", job_no);
2062                        else
2063                                pandecode_log_cont("next_job_32 = (u32) (uintptr_t) job_%d_p;\n\n", job_no);
2064                }
2065
2066                first = false;
2067
2068        } while ((jc_gpu_va = h->job_descriptor_size ? h->next_job_64 : h->next_job_32));
2069
2070        return start_number;
2071}
2072