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, ¶ms);
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
217 return (frag_shader & 0xFFFFFFFF00000000ULL) | b.internal.shader.pc;
218 #else
219 return b.blend_shader ? (b.shader_pc & ~0xf) : 0;
220 #endif
221 }
222 #endif
223
224 #if PAN_ARCH <= 7
225 static bool
panfrost_is_yuv_format(uint32_t packed)226 panfrost_is_yuv_format(uint32_t packed)
227 {
228 #if PAN_ARCH == 7
229 enum mali_format mali_fmt = packed >> 12;
230 return mali_fmt >= MALI_YUV8 && mali_fmt <= MALI_CUSTOM_YUV_5;
231 #else
232 /* Currently only supported by panfrost on v7 */
233 assert(0);
234 return false;
235 #endif
236 }
237
238 static void
pandecode_texture_payload(struct pandecode_context * ctx,mali_ptr payload,const struct MALI_TEXTURE * tex)239 pandecode_texture_payload(struct pandecode_context *ctx, mali_ptr payload,
240 const struct MALI_TEXTURE *tex)
241 {
242 unsigned nr_samples =
243 tex->dimension == MALI_TEXTURE_DIMENSION_3D ? 1 : tex->sample_count;
244
245 /* A bunch of bitmap pointers follow.
246 * We work out the correct number,
247 * based on the mipmap/cubemap
248 * properties, but dump extra
249 * possibilities to futureproof */
250
251 int bitmap_count = tex->levels;
252
253 /* Miptree for each face */
254 if (tex->dimension == MALI_TEXTURE_DIMENSION_CUBE)
255 bitmap_count *= 6;
256
257 /* Array of layers */
258 bitmap_count *= nr_samples;
259
260 /* Array of textures */
261 bitmap_count *= tex->array_size;
262
263 #define PANDECODE_EMIT_TEX_PAYLOAD_DESC(T, msg) \
264 for (int i = 0; i < bitmap_count; ++i) { \
265 uint64_t addr = payload + pan_size(T) * i; \
266 pan_unpack(PANDECODE_PTR(ctx, addr, void), T, s); \
267 DUMP_UNPACKED(ctx, T, s, msg " @%" PRIx64 ":\n", addr) \
268 }
269
270 #if PAN_ARCH <= 5
271 switch (tex->surface_type) {
272 case MALI_SURFACE_TYPE_32:
273 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_32, "Surface 32");
274 break;
275 case MALI_SURFACE_TYPE_64:
276 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE, "Surface");
277 break;
278 case MALI_SURFACE_TYPE_32_WITH_ROW_STRIDE:
279 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_32, "Surface 32 With Row Stride");
280 break;
281 case MALI_SURFACE_TYPE_64_WITH_STRIDES:
282 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE,
283 "Surface With Stride");
284 break;
285 default:
286 fprintf(ctx->dump_stream, "Unknown surface descriptor type %X\n",
287 tex->surface_type);
288 break;
289 }
290 #elif PAN_ARCH == 6
291 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE, "Surface With Stride");
292 #else
293 STATIC_ASSERT(PAN_ARCH == 7);
294 if (panfrost_is_yuv_format(tex->format)) {
295 PANDECODE_EMIT_TEX_PAYLOAD_DESC(MULTIPLANAR_SURFACE, "Surface YUV");
296 } else {
297 PANDECODE_EMIT_TEX_PAYLOAD_DESC(SURFACE_WITH_STRIDE,
298 "Surface With Stride");
299 }
300 #endif
301
302 #undef PANDECODE_EMIT_TEX_PAYLOAD_DESC
303 }
304 #endif
305
306 #if PAN_ARCH <= 5
307 void
GENX(pandecode_texture)308 GENX(pandecode_texture)(struct pandecode_context *ctx, mali_ptr u, unsigned tex)
309 {
310 const uint8_t *cl = pandecode_fetch_gpu_mem(ctx, u, pan_size(TEXTURE));
311
312 pan_unpack(cl, TEXTURE, temp);
313 DUMP_UNPACKED(ctx, TEXTURE, temp, "Texture:\n")
314
315 ctx->indent++;
316 pandecode_texture_payload(ctx, u + pan_size(TEXTURE), &temp);
317 ctx->indent--;
318 }
319 #else
320 void
GENX(pandecode_texture)321 GENX(pandecode_texture)(struct pandecode_context *ctx, const void *cl,
322 unsigned tex)
323 {
324 pan_unpack(cl, TEXTURE, temp);
325 DUMP_UNPACKED(ctx, TEXTURE, temp, "Texture:\n")
326
327 ctx->indent++;
328
329 #if PAN_ARCH >= 9
330 int plane_count = temp.levels * temp.array_size;
331
332 /* Miptree for each face */
333 if (temp.dimension == MALI_TEXTURE_DIMENSION_CUBE)
334 plane_count *= 6;
335
336 for (unsigned i = 0; i < plane_count; ++i)
337 DUMP_ADDR(ctx, PLANE, temp.surfaces + i * pan_size(PLANE), "Plane %u:\n",
338 i);
339 #else
340 pandecode_texture_payload(ctx, temp.surfaces, &temp);
341 #endif
342 ctx->indent--;
343 }
344 #endif
345
346 #if PAN_ARCH >= 6
347 void
GENX(pandecode_tiler)348 GENX(pandecode_tiler)(struct pandecode_context *ctx, mali_ptr gpu_va,
349 unsigned gpu_id)
350 {
351 pan_unpack(PANDECODE_PTR(ctx, gpu_va, void), TILER_CONTEXT, t);
352
353 if (t.heap) {
354 pan_unpack(PANDECODE_PTR(ctx, t.heap, void), TILER_HEAP, h);
355 DUMP_UNPACKED(ctx, TILER_HEAP, h, "Tiler Heap:\n");
356 }
357
358 DUMP_UNPACKED(ctx, TILER_CONTEXT, t, "Tiler Context @%" PRIx64 ":\n",
359 gpu_va);
360 }
361 #endif
362
363 #if PAN_ARCH >= 9
364 void
GENX(pandecode_fau)365 GENX(pandecode_fau)(struct pandecode_context *ctx, mali_ptr addr,
366 unsigned count, const char *name)
367 {
368 if (count == 0)
369 return;
370
371 const uint32_t *PANDECODE_PTR_VAR(ctx, raw, addr);
372
373 pandecode_validate_buffer(ctx, addr, count * 8);
374
375 fprintf(ctx->dump_stream, "%s @%" PRIx64 ":\n", name, addr);
376 for (unsigned i = 0; i < count; ++i) {
377 fprintf(ctx->dump_stream, " %08X %08X\n", raw[2 * i], raw[2 * i + 1]);
378 }
379 fprintf(ctx->dump_stream, "\n");
380 }
381
382 mali_ptr
GENX(pandecode_shader)383 GENX(pandecode_shader)(struct pandecode_context *ctx, mali_ptr addr,
384 const char *label, unsigned gpu_id)
385 {
386 MAP_ADDR(ctx, SHADER_PROGRAM, addr, cl);
387 pan_unpack(cl, SHADER_PROGRAM, desc);
388
389 assert(desc.type == 8);
390
391 DUMP_UNPACKED(ctx, SHADER_PROGRAM, desc, "%s Shader @%" PRIx64 ":\n", label,
392 addr);
393 pandecode_shader_disassemble(ctx, desc.binary, gpu_id);
394 return desc.binary;
395 }
396
397 static void
pandecode_resources(struct pandecode_context * ctx,mali_ptr addr,unsigned size)398 pandecode_resources(struct pandecode_context *ctx, mali_ptr addr, unsigned size)
399 {
400 const uint8_t *cl = pandecode_fetch_gpu_mem(ctx, addr, size);
401 assert((size % 0x20) == 0);
402
403 for (unsigned i = 0; i < size; i += 0x20) {
404 unsigned type = (cl[i] & 0xF);
405
406 switch (type) {
407 case MALI_DESCRIPTOR_TYPE_SAMPLER:
408 DUMP_CL(ctx, SAMPLER, cl + i, "Sampler @%" PRIx64 ":\n", addr + i);
409 break;
410 case MALI_DESCRIPTOR_TYPE_TEXTURE:
411 pandecode_log(ctx, "Texture @%" PRIx64 "\n", addr + i);
412 GENX(pandecode_texture)(ctx, cl + i, i);
413 break;
414 case MALI_DESCRIPTOR_TYPE_ATTRIBUTE:
415 DUMP_CL(ctx, ATTRIBUTE, cl + i, "Attribute @%" PRIx64 ":\n", addr + i);
416 break;
417 case MALI_DESCRIPTOR_TYPE_BUFFER:
418 DUMP_CL(ctx, BUFFER, cl + i, "Buffer @%" PRIx64 ":\n", addr + i);
419 break;
420 default:
421 fprintf(ctx->dump_stream, "Unknown descriptor type %X\n", type);
422 break;
423 }
424 }
425 }
426
427 void
GENX(pandecode_resource_tables)428 GENX(pandecode_resource_tables)(struct pandecode_context *ctx, mali_ptr addr,
429 const char *label)
430 {
431 unsigned count = addr & 0x3F;
432 addr = addr & ~0x3F;
433
434 const uint8_t *cl =
435 pandecode_fetch_gpu_mem(ctx, addr, MALI_RESOURCE_LENGTH * count);
436
437 pandecode_log(ctx, "%s resource table @%" PRIx64 "\n", label, addr);
438 ctx->indent += 2;
439 for (unsigned i = 0; i < count; ++i) {
440 pan_unpack(cl + i * MALI_RESOURCE_LENGTH, RESOURCE, entry);
441 DUMP_UNPACKED(ctx, RESOURCE, entry, "Entry %u @%" PRIx64 ":\n", i,
442 addr + i * MALI_RESOURCE_LENGTH);
443
444 ctx->indent += 2;
445 if (entry.address)
446 pandecode_resources(ctx, entry.address, entry.size);
447 ctx->indent -= 2;
448 }
449 ctx->indent -= 2;
450 }
451
452 void
GENX(pandecode_depth_stencil)453 GENX(pandecode_depth_stencil)(struct pandecode_context *ctx, mali_ptr addr)
454 {
455 MAP_ADDR(ctx, DEPTH_STENCIL, addr, cl);
456 pan_unpack(cl, DEPTH_STENCIL, desc);
457 DUMP_UNPACKED(ctx, DEPTH_STENCIL, desc, "Depth/stencil");
458 }
459
460 void
GENX(pandecode_shader_environment)461 GENX(pandecode_shader_environment)(struct pandecode_context *ctx,
462 const struct MALI_SHADER_ENVIRONMENT *p,
463 unsigned gpu_id)
464 {
465 if (p->shader)
466 GENX(pandecode_shader)(ctx, p->shader, "Shader", gpu_id);
467
468 if (p->resources)
469 GENX(pandecode_resource_tables)(ctx, p->resources, "Resources");
470
471 if (p->thread_storage)
472 DUMP_ADDR(ctx, LOCAL_STORAGE, p->thread_storage, "Local Storage:\n");
473
474 if (p->fau)
475 GENX(pandecode_fau)(ctx, p->fau, p->fau_count, "FAU");
476 }
477
478 void
GENX(pandecode_blend_descs)479 GENX(pandecode_blend_descs)(struct pandecode_context *ctx, mali_ptr blend,
480 unsigned count, mali_ptr frag_shader,
481 unsigned gpu_id)
482 {
483 for (unsigned i = 0; i < count; ++i) {
484 struct mali_blend_packed *PANDECODE_PTR_VAR(ctx, blend_descs, blend);
485
486 mali_ptr blend_shader =
487 GENX(pandecode_blend)(ctx, blend_descs, i, frag_shader);
488 if (blend_shader) {
489 fprintf(ctx->dump_stream, "Blend shader %u @%" PRIx64 "", i,
490 blend_shader);
491 pandecode_shader_disassemble(ctx, blend_shader, gpu_id);
492 }
493 }
494 }
495
496 void
GENX(pandecode_dcd)497 GENX(pandecode_dcd)(struct pandecode_context *ctx, const struct MALI_DRAW *p,
498 unsigned unused, unsigned gpu_id)
499 {
500 mali_ptr frag_shader = 0;
501
502 GENX(pandecode_depth_stencil)(ctx, p->depth_stencil);
503 GENX(pandecode_blend_descs)
504 (ctx, p->blend, p->blend_count, frag_shader, gpu_id);
505 GENX(pandecode_shader_environment)(ctx, &p->shader, gpu_id);
506 DUMP_UNPACKED(ctx, DRAW, *p, "Draw:\n");
507 }
508 #endif
509