xref: /aosp_15_r20/external/mesa3d/src/panfrost/lib/genxml/decode.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "decode.h"
27 #include <ctype.h>
28 #include <errno.h>
29 #include <memory.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <genxml/gen_macros.h>
35 #include <sys/mman.h>
36 
37 #include "compiler/bifrost/disassemble.h"
38 #include "compiler/valhall/disassemble.h"
39 #include "midgard/disassemble.h"
40 #include "util/set.h"
41 #include "pan_format.h"
42 
43 #if PAN_ARCH <= 5
44 /* Midgard's tiler descriptor is embedded within the
45  * larger FBD */
46 
47 static void
pandecode_midgard_tiler_descriptor(struct pandecode_context * ctx,const struct mali_tiler_context_packed * tp,const struct mali_tiler_weights_packed * wp)48 pandecode_midgard_tiler_descriptor(struct pandecode_context *ctx,
49                                    const struct mali_tiler_context_packed *tp,
50                                    const struct mali_tiler_weights_packed *wp)
51 {
52    pan_unpack(tp, TILER_CONTEXT, t);
53    DUMP_UNPACKED(ctx, TILER_CONTEXT, t, "Tiler:\n");
54 
55    /* We've never seen weights used in practice, but they exist */
56    pan_unpack(wp, TILER_WEIGHTS, w);
57    bool nonzero_weights = false;
58 
59    nonzero_weights |= w.weight0 != 0x0;
60    nonzero_weights |= w.weight1 != 0x0;
61    nonzero_weights |= w.weight2 != 0x0;
62    nonzero_weights |= w.weight3 != 0x0;
63    nonzero_weights |= w.weight4 != 0x0;
64    nonzero_weights |= w.weight5 != 0x0;
65    nonzero_weights |= w.weight6 != 0x0;
66    nonzero_weights |= w.weight7 != 0x0;
67 
68    if (nonzero_weights)
69       DUMP_UNPACKED(ctx, TILER_WEIGHTS, w, "Tiler Weights:\n");
70 }
71 #endif
72 
73 #if PAN_ARCH >= 5
74 static void
pandecode_render_target(struct pandecode_context * ctx,uint64_t gpu_va,unsigned gpu_id,const struct MALI_FRAMEBUFFER_PARAMETERS * fb)75 pandecode_render_target(struct pandecode_context *ctx, uint64_t gpu_va,
76                         unsigned gpu_id,
77                         const struct MALI_FRAMEBUFFER_PARAMETERS *fb)
78 {
79    pandecode_log(ctx, "Color Render Targets @%" PRIx64 ":\n", gpu_va);
80    ctx->indent++;
81 
82    for (int i = 0; i < (fb->render_target_count); i++) {
83       mali_ptr rt_va = gpu_va + i * pan_size(RENDER_TARGET);
84       const struct mali_render_target_packed *PANDECODE_PTR_VAR(
85          ctx, rtp, (mali_ptr)rt_va);
86       DUMP_CL(ctx, RENDER_TARGET, rtp, "Color Render Target %d:\n", i);
87    }
88 
89    ctx->indent--;
90    pandecode_log(ctx, "\n");
91 }
92 #endif
93 
94 #if PAN_ARCH >= 6
95 static void
pandecode_sample_locations(struct pandecode_context * ctx,const void * fb)96 pandecode_sample_locations(struct pandecode_context *ctx, const void *fb)
97 {
98    pan_section_unpack(fb, FRAMEBUFFER, PARAMETERS, params);
99 
100    const u16 *PANDECODE_PTR_VAR(ctx, samples, params.sample_locations);
101 
102    pandecode_log(ctx, "Sample locations @%" PRIx64 ":\n",
103                  params.sample_locations);
104    for (int i = 0; i < 33; i++) {
105       pandecode_log(ctx, "  (%d, %d),\n", samples[2 * i] - 128,
106                     samples[2 * i + 1] - 128);
107    }
108 }
109 #endif
110 
111 struct pandecode_fbd
GENX(pandecode_fbd)112 GENX(pandecode_fbd)(struct pandecode_context *ctx, uint64_t gpu_va,
113                     bool is_fragment, unsigned gpu_id)
114 {
115    const void *PANDECODE_PTR_VAR(ctx, fb, (mali_ptr)gpu_va);
116    pan_section_unpack(fb, FRAMEBUFFER, PARAMETERS, params);
117    DUMP_UNPACKED(ctx, FRAMEBUFFER_PARAMETERS, params, "Parameters:\n");
118 
119 #if PAN_ARCH >= 6
120    pandecode_sample_locations(ctx, fb);
121 
122    unsigned dcd_size = pan_size(DRAW);
123    unsigned job_type_param = 0;
124 
125 #if PAN_ARCH <= 9
126    job_type_param = MALI_JOB_TYPE_FRAGMENT;
127 #endif
128 
129    if (params.pre_frame_0 != MALI_PRE_POST_FRAME_SHADER_MODE_NEVER) {
130       const void *PANDECODE_PTR_VAR(ctx, dcd,
131                                     params.frame_shader_dcds + (0 * dcd_size));
132       pan_unpack(dcd, DRAW, draw);
133       pandecode_log(ctx, "Pre frame 0 @%" PRIx64 " (mode=%d):\n",
134                     params.frame_shader_dcds, params.pre_frame_0);
135       GENX(pandecode_dcd)(ctx, &draw, job_type_param, gpu_id);
136    }
137 
138    if (params.pre_frame_1 != MALI_PRE_POST_FRAME_SHADER_MODE_NEVER) {
139       const void *PANDECODE_PTR_VAR(ctx, dcd,
140                                     params.frame_shader_dcds + (1 * dcd_size));
141       pan_unpack(dcd, DRAW, draw);
142       pandecode_log(ctx, "Pre frame 1 @%" PRIx64 ":\n",
143                     params.frame_shader_dcds + (1 * dcd_size));
144       GENX(pandecode_dcd)(ctx, &draw, job_type_param, gpu_id);
145    }
146 
147    if (params.post_frame != MALI_PRE_POST_FRAME_SHADER_MODE_NEVER) {
148       const void *PANDECODE_PTR_VAR(ctx, dcd,
149                                     params.frame_shader_dcds + (2 * dcd_size));
150       pan_unpack(dcd, DRAW, draw);
151       pandecode_log(ctx, "Post frame:\n");
152       GENX(pandecode_dcd)(ctx, &draw, job_type_param, gpu_id);
153    }
154 #else
155    DUMP_SECTION(ctx, FRAMEBUFFER, LOCAL_STORAGE, fb, "Local Storage:\n");
156 
157    const void *t = pan_section_ptr(fb, FRAMEBUFFER, TILER);
158    const void *w = pan_section_ptr(fb, FRAMEBUFFER, TILER_WEIGHTS);
159    pandecode_midgard_tiler_descriptor(ctx, t, w);
160 #endif
161 
162    pandecode_log(ctx, "Framebuffer @%" PRIx64 ":\n", gpu_va);
163    ctx->indent++;
164 
165    DUMP_UNPACKED(ctx, FRAMEBUFFER_PARAMETERS, params, "Parameters:\n");
166 #if PAN_ARCH >= 6
167    if (params.tiler)
168       GENX(pandecode_tiler)(ctx, params.tiler, gpu_id);
169 #endif
170 
171    ctx->indent--;
172    pandecode_log(ctx, "\n");
173 
174 #if PAN_ARCH >= 5
175    gpu_va += pan_size(FRAMEBUFFER);
176 
177    if (params.has_zs_crc_extension) {
178       const struct mali_zs_crc_extension_packed *PANDECODE_PTR_VAR(
179          ctx, zs_crc, (mali_ptr)gpu_va);
180       DUMP_CL(ctx, ZS_CRC_EXTENSION, zs_crc, "ZS CRC Extension:\n");
181       pandecode_log(ctx, "\n");
182 
183       gpu_va += pan_size(ZS_CRC_EXTENSION);
184    }
185 
186    if (is_fragment)
187       pandecode_render_target(ctx, gpu_va, gpu_id, &params);
188 
189    return (struct pandecode_fbd){
190       .rt_count = params.render_target_count,
191       .has_extra = params.has_zs_crc_extension,
192    };
193 #else
194    /* Dummy unpack of the padding section to make sure all words are 0.
195     * No need to call print here since the section is supposed to be empty.
196     */
197    pan_section_unpack(fb, FRAMEBUFFER, PADDING_1, padding1);
198    pan_section_unpack(fb, FRAMEBUFFER, PADDING_2, padding2);
199 
200    return (struct pandecode_fbd){
201       .rt_count = 1,
202    };
203 #endif
204 }
205 
206 #if PAN_ARCH >= 5
207 mali_ptr
GENX(pandecode_blend)208 GENX(pandecode_blend)(struct pandecode_context *ctx, void *descs, int rt_no,
209                       mali_ptr frag_shader)
210 {
211    pan_unpack(descs + (rt_no * pan_size(BLEND)), BLEND, b);
212    DUMP_UNPACKED(ctx, BLEND, b, "Blend RT %d:\n", rt_no);
213 #if PAN_ARCH >= 6
214    if (b.internal.mode != MALI_BLEND_MODE_SHADER)
215       return 0;
216    /* If we don't have a frag shader, we can't extract the LSB of the blend
217     * shader so return NULL in that case. It doesn't matter, because the
218     * blend shader won't be executed anyway, so disassembling is not
219     * super useful. */
220    if (!frag_shader)
221       return 0;
222 
223    return (frag_shader & 0xFFFFFFFF00000000ULL) | b.internal.shader.pc;
224 #else
225    return b.blend_shader ? (b.shader_pc & ~0xf) : 0;
226 #endif
227 }
228 #endif
229 
230 #if PAN_ARCH <= 7
231 static bool
panfrost_is_yuv_format(uint32_t packed)232 panfrost_is_yuv_format(uint32_t packed)
233 {
234 #if PAN_ARCH == 7
235    enum mali_format mali_fmt = packed >> 12;
236    return mali_fmt >= MALI_YUV8 && mali_fmt <= MALI_CUSTOM_YUV_5;
237 #else
238    /* Currently only supported by panfrost on v7 */
239    assert(0);
240    return false;
241 #endif
242 }
243 
244 static void
pandecode_texture_payload(struct pandecode_context * ctx,mali_ptr payload,const struct MALI_TEXTURE * tex)245 pandecode_texture_payload(struct pandecode_context *ctx, mali_ptr payload,
246                           const struct MALI_TEXTURE *tex)
247 {
248    unsigned nr_samples =
249       tex->dimension == MALI_TEXTURE_DIMENSION_3D ? 1 : tex->sample_count;
250 
251    if (!payload)
252       return;
253 
254    /* A bunch of bitmap pointers follow.
255     * We work out the correct number,
256     * based on the mipmap/cubemap
257     * properties, but dump extra
258     * possibilities to futureproof */
259 
260    int bitmap_count = tex->levels;
261 
262    /* Miptree for each face */
263    if (tex->dimension == MALI_TEXTURE_DIMENSION_CUBE)
264       bitmap_count *= 6;
265 
266    /* Array of layers */
267    bitmap_count *= nr_samples;
268 
269    /* Array of textures */
270    bitmap_count *= tex->array_size;
271 
272 #define PANDECODE_EMIT_TEX_PAYLOAD_DESC(T, msg)                                \
273    for (int i = 0; i < bitmap_count; ++i) {                                    \
274       uint64_t addr = payload + pan_size(T) * i;                               \
275       pan_unpack(PANDECODE_PTR(ctx, addr, void), T, s);                        \
276       DUMP_UNPACKED(ctx, T, s, msg " @%" PRIx64 ":\n", addr)                   \
277    }
278 
279 #if PAN_ARCH <= 5
280    switch (tex->surface_type) {
281    case MALI_SURFACE_TYPE_32:
282       PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_32, "Surface 32");
283       break;
284    case MALI_SURFACE_TYPE_64:
285       PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE, "Surface");
286       break;
287    case MALI_SURFACE_TYPE_32_WITH_ROW_STRIDE:
288       PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_32, "Surface 32 With Row Stride");
289       break;
290    case MALI_SURFACE_TYPE_64_WITH_STRIDES:
291       PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE,
292                                       "Surface With Stride");
293       break;
294    default:
295       fprintf(ctx->dump_stream, "Unknown surface descriptor type %X\n",
296               tex->surface_type);
297       break;
298    }
299 #elif PAN_ARCH == 6
300    PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE, "Surface With Stride");
301 #else
302    STATIC_ASSERT(PAN_ARCH == 7);
303    if (panfrost_is_yuv_format(tex->format)) {
304       PANDECODE_EMIT_TEX_PAYLOAD_DESC(MULTIPLANAR_SURFACE, "Surface YUV");
305    } else {
306       PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE,
307                                       "Surface With Stride");
308    }
309 #endif
310 
311 #undef PANDECODE_EMIT_TEX_PAYLOAD_DESC
312 }
313 #endif
314 
315 #if PAN_ARCH <= 5
316 void
GENX(pandecode_texture)317 GENX(pandecode_texture)(struct pandecode_context *ctx, mali_ptr u, unsigned tex)
318 {
319    const uint8_t *cl = pandecode_fetch_gpu_mem(ctx, u, pan_size(TEXTURE));
320 
321    pan_unpack(cl, TEXTURE, temp);
322    DUMP_UNPACKED(ctx, TEXTURE, temp, "Texture:\n")
323 
324    ctx->indent++;
325    pandecode_texture_payload(ctx, u + pan_size(TEXTURE), &temp);
326    ctx->indent--;
327 }
328 #else
329 void
GENX(pandecode_texture)330 GENX(pandecode_texture)(struct pandecode_context *ctx, const void *cl,
331                         unsigned tex)
332 {
333    pan_unpack(cl, TEXTURE, temp);
334    DUMP_UNPACKED(ctx, TEXTURE, temp, "Texture:\n")
335 
336    ctx->indent++;
337 
338 #if PAN_ARCH >= 9
339    int plane_count = temp.levels * temp.array_size;
340 
341    /* Miptree for each face */
342    if (temp.dimension == MALI_TEXTURE_DIMENSION_CUBE)
343       plane_count *= 6;
344 
345    for (unsigned i = 0; i < plane_count; ++i)
346       DUMP_ADDR(ctx, PLANE, temp.surfaces + i * pan_size(PLANE), "Plane %u:\n",
347                 i);
348 #else
349    pandecode_texture_payload(ctx, temp.surfaces, &temp);
350 #endif
351    ctx->indent--;
352 }
353 #endif
354 
355 #if PAN_ARCH >= 6
356 void
GENX(pandecode_tiler)357 GENX(pandecode_tiler)(struct pandecode_context *ctx, mali_ptr gpu_va,
358                       unsigned gpu_id)
359 {
360    pan_unpack(PANDECODE_PTR(ctx, gpu_va, void), TILER_CONTEXT, t);
361 
362    if (t.heap) {
363       pan_unpack(PANDECODE_PTR(ctx, t.heap, void), TILER_HEAP, h);
364       DUMP_UNPACKED(ctx, TILER_HEAP, h, "Tiler Heap:\n");
365    }
366 
367    DUMP_UNPACKED(ctx, TILER_CONTEXT, t, "Tiler Context @%" PRIx64 ":\n",
368                  gpu_va);
369 }
370 #endif
371 
372 #if PAN_ARCH >= 9
373 void
GENX(pandecode_fau)374 GENX(pandecode_fau)(struct pandecode_context *ctx, mali_ptr addr,
375                     unsigned count, const char *name)
376 {
377    if (count == 0)
378       return;
379 
380    const uint32_t *PANDECODE_PTR_VAR(ctx, raw, addr);
381 
382    pandecode_validate_buffer(ctx, addr, count * 8);
383 
384    fprintf(ctx->dump_stream, "%s @%" PRIx64 ":\n", name, addr);
385    for (unsigned i = 0; i < count; ++i) {
386       fprintf(ctx->dump_stream, "  %08X %08X\n", raw[2 * i], raw[2 * i + 1]);
387    }
388    fprintf(ctx->dump_stream, "\n");
389 }
390 
391 mali_ptr
GENX(pandecode_shader)392 GENX(pandecode_shader)(struct pandecode_context *ctx, mali_ptr addr,
393                        const char *label, unsigned gpu_id)
394 {
395    MAP_ADDR(ctx, SHADER_PROGRAM, addr, cl);
396    pan_unpack(cl, SHADER_PROGRAM, desc);
397 
398    assert(desc.type == 8);
399 
400    DUMP_UNPACKED(ctx, SHADER_PROGRAM, desc, "%s Shader @%" PRIx64 ":\n", label,
401                  addr);
402    pandecode_shader_disassemble(ctx, desc.binary, gpu_id);
403    return desc.binary;
404 }
405 
406 static void
pandecode_resources(struct pandecode_context * ctx,mali_ptr addr,unsigned size)407 pandecode_resources(struct pandecode_context *ctx, mali_ptr addr, unsigned size)
408 {
409    const uint8_t *cl = pandecode_fetch_gpu_mem(ctx, addr, size);
410    assert((size % 0x20) == 0);
411 
412    for (unsigned i = 0; i < size; i += 0x20) {
413       unsigned type = (cl[i] & 0xF);
414 
415       switch (type) {
416       case MALI_DESCRIPTOR_TYPE_SAMPLER:
417          DUMP_CL(ctx, SAMPLER, cl + i, "Sampler @%" PRIx64 ":\n", addr + i);
418          break;
419       case MALI_DESCRIPTOR_TYPE_TEXTURE:
420          pandecode_log(ctx, "Texture @%" PRIx64 "\n", addr + i);
421          GENX(pandecode_texture)(ctx, cl + i, i);
422          break;
423       case MALI_DESCRIPTOR_TYPE_ATTRIBUTE:
424          DUMP_CL(ctx, ATTRIBUTE, cl + i, "Attribute @%" PRIx64 ":\n", addr + i);
425          break;
426       case MALI_DESCRIPTOR_TYPE_BUFFER:
427          DUMP_CL(ctx, BUFFER, cl + i, "Buffer @%" PRIx64 ":\n", addr + i);
428          break;
429       default:
430          fprintf(ctx->dump_stream, "Unknown descriptor type %X\n", type);
431          break;
432       }
433    }
434 }
435 
436 void
GENX(pandecode_resource_tables)437 GENX(pandecode_resource_tables)(struct pandecode_context *ctx, mali_ptr addr,
438                                 const char *label)
439 {
440    unsigned count = addr & 0x3F;
441    addr = addr & ~0x3F;
442 
443    const uint8_t *cl =
444       pandecode_fetch_gpu_mem(ctx, addr, MALI_RESOURCE_LENGTH * count);
445 
446    pandecode_log(ctx, "%s resource table @%" PRIx64 "\n", label, addr);
447    ctx->indent += 2;
448    for (unsigned i = 0; i < count; ++i) {
449       pan_unpack(cl + i * MALI_RESOURCE_LENGTH, RESOURCE, entry);
450       DUMP_UNPACKED(ctx, RESOURCE, entry, "Entry %u @%" PRIx64 ":\n", i,
451                     addr + i * MALI_RESOURCE_LENGTH);
452 
453       ctx->indent += 2;
454       if (entry.address)
455          pandecode_resources(ctx, entry.address, entry.size);
456       ctx->indent -= 2;
457    }
458    ctx->indent -= 2;
459 }
460 
461 void
GENX(pandecode_depth_stencil)462 GENX(pandecode_depth_stencil)(struct pandecode_context *ctx, mali_ptr addr)
463 {
464    MAP_ADDR(ctx, DEPTH_STENCIL, addr, cl);
465    pan_unpack(cl, DEPTH_STENCIL, desc);
466    DUMP_UNPACKED(ctx, DEPTH_STENCIL, desc, "Depth/stencil");
467 }
468 
469 void
GENX(pandecode_shader_environment)470 GENX(pandecode_shader_environment)(struct pandecode_context *ctx,
471                                    const struct MALI_SHADER_ENVIRONMENT *p,
472                                    unsigned gpu_id)
473 {
474    if (p->shader)
475       GENX(pandecode_shader)(ctx, p->shader, "Shader", gpu_id);
476 
477    if (p->resources)
478       GENX(pandecode_resource_tables)(ctx, p->resources, "Resources");
479 
480    if (p->thread_storage)
481       DUMP_ADDR(ctx, LOCAL_STORAGE, p->thread_storage, "Local Storage:\n");
482 
483    if (p->fau)
484       GENX(pandecode_fau)(ctx, p->fau, p->fau_count, "FAU");
485 }
486 
487 void
GENX(pandecode_blend_descs)488 GENX(pandecode_blend_descs)(struct pandecode_context *ctx, mali_ptr blend,
489                             unsigned count, mali_ptr frag_shader,
490                             unsigned gpu_id)
491 {
492    for (unsigned i = 0; i < count; ++i) {
493       struct mali_blend_packed *PANDECODE_PTR_VAR(ctx, blend_descs, blend);
494 
495       mali_ptr blend_shader =
496          GENX(pandecode_blend)(ctx, blend_descs, i, frag_shader);
497       if (blend_shader) {
498          fprintf(ctx->dump_stream, "Blend shader %u @%" PRIx64 "", i,
499                  blend_shader);
500          pandecode_shader_disassemble(ctx, blend_shader, gpu_id);
501       }
502    }
503 }
504 
505 void
GENX(pandecode_dcd)506 GENX(pandecode_dcd)(struct pandecode_context *ctx, const struct MALI_DRAW *p,
507                     unsigned unused, unsigned gpu_id)
508 {
509    mali_ptr frag_shader = 0;
510 
511    GENX(pandecode_depth_stencil)(ctx, p->depth_stencil);
512    GENX(pandecode_blend_descs)
513    (ctx, p->blend, p->blend_count, frag_shader, gpu_id);
514    GENX(pandecode_shader_environment)(ctx, &p->shader, gpu_id);
515    DUMP_UNPACKED(ctx, DRAW, *p, "Draw:\n");
516 }
517 #endif
518