xref: /aosp_15_r20/external/mesa3d/src/broadcom/clif/clif_dump.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2016 Broadcom
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "drm-uapi/v3d_drm.h"
28 #include "clif_dump.h"
29 #include "clif_private.h"
30 #include "util/list.h"
31 #include "util/ralloc.h"
32 
33 #include "broadcom/cle/v3d_decoder.h"
34 
35 struct reloc_worklist_entry *
clif_dump_add_address_to_worklist(struct clif_dump * clif,enum reloc_worklist_type type,uint32_t addr)36 clif_dump_add_address_to_worklist(struct clif_dump *clif,
37                                   enum reloc_worklist_type type,
38                                   uint32_t addr)
39 {
40         struct reloc_worklist_entry *entry =
41                 rzalloc(clif, struct reloc_worklist_entry);
42         if (!entry)
43                 return NULL;
44 
45         entry->type = type;
46         entry->addr = addr;
47 
48         list_addtail(&entry->link, &clif->worklist);
49 
50         return entry;
51 }
52 
53 struct clif_dump *
clif_dump_init(const struct v3d_device_info * devinfo,FILE * out,bool pretty,bool nobin)54 clif_dump_init(const struct v3d_device_info *devinfo,
55                FILE *out, bool pretty, bool nobin)
56 {
57         struct clif_dump *clif = rzalloc(NULL, struct clif_dump);
58 
59         clif->devinfo = devinfo;
60         clif->out = out;
61         clif->spec = v3d_spec_load(devinfo);
62         clif->pretty = pretty;
63         clif->nobin = nobin;
64 
65         list_inithead(&clif->worklist);
66 
67         return clif;
68 }
69 
70 void
clif_dump_destroy(struct clif_dump * clif)71 clif_dump_destroy(struct clif_dump *clif)
72 {
73         ralloc_free(clif);
74 }
75 
76 struct clif_bo *
clif_lookup_bo(struct clif_dump * clif,uint32_t addr)77 clif_lookup_bo(struct clif_dump *clif, uint32_t addr)
78 {
79         for (int i = 0; i < clif->bo_count; i++) {
80                 struct clif_bo *bo = &clif->bo[i];
81 
82                 if (addr >= bo->offset &&
83                     addr < bo->offset + bo->size) {
84                         return bo;
85                 }
86         }
87 
88         return NULL;
89 }
90 
91 static bool
clif_lookup_vaddr(struct clif_dump * clif,uint32_t addr,void ** vaddr)92 clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)
93 {
94         struct clif_bo *bo = clif_lookup_bo(clif, addr);
95         if (!bo)
96                 return false;
97 
98         *vaddr = bo->vaddr + addr - bo->offset;
99         return true;
100 }
101 
102 #define out_uint(_clif, field) out(_clif, "    /* %s = */ %u\n",        \
103                             #field,  values-> field);
104 
105 static bool
clif_dump_packet(struct clif_dump * clif,uint32_t offset,const uint8_t * cl,uint32_t * size,bool reloc_mode)106 clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
107                  uint32_t *size, bool reloc_mode)
108 {
109 
110         switch (clif->devinfo->ver) {
111         case 42:
112                 return v3d42_clif_dump_packet(clif, offset, cl, size, reloc_mode);
113         case 71:
114                 return v3d71_clif_dump_packet(clif, offset, cl, size, reloc_mode);
115         default:
116                 break;
117         };
118         unreachable("Unknown HW version");
119 }
120 
121 static uint32_t
clif_dump_cl(struct clif_dump * clif,uint32_t start,uint32_t end,bool reloc_mode)122 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end,
123              bool reloc_mode)
124 {
125         struct clif_bo *bo = clif_lookup_bo(clif, start);
126         if (!bo) {
127                 out(clif, "Failed to look up address 0x%08x\n",
128                     start);
129                 return 0;
130         }
131 
132         void *start_vaddr = bo->vaddr + start - bo->offset;
133 
134         /* The end address is optional (for example, a BRANCH instruction
135          * won't set an end), but is used for BCL/RCL termination.
136          */
137         void *end_vaddr = NULL;
138         if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
139                 out(clif, "Failed to look up address 0x%08x\n",
140                     end);
141                 return 0;
142         }
143 
144         if (!reloc_mode)
145                 out(clif, "@format ctrllist  /* [%s+0x%08x] */\n",
146                     bo->name, start - bo->offset);
147 
148         uint32_t size;
149         uint8_t *cl = start_vaddr;
150         while (clif_dump_packet(clif, start, cl, &size, reloc_mode)) {
151                 cl += size;
152                 start += size;
153 
154                 if (cl == end_vaddr)
155                         break;
156         }
157 
158         return (void *)cl - bo->vaddr;
159 }
160 
161 /* Walks the worklist, parsing the relocs for any memory regions that might
162  * themselves have additional relocations.
163  */
164 static uint32_t
clif_dump_gl_shader_state_record(struct clif_dump * clif,struct reloc_worklist_entry * reloc,void * vaddr,bool including_gs)165 clif_dump_gl_shader_state_record(struct clif_dump *clif,
166                                  struct reloc_worklist_entry *reloc,
167                                  void *vaddr,
168                                  bool including_gs)
169 {
170         struct v3d_group *state = v3d_spec_find_struct(clif->spec,
171                                                        "GL Shader State Record");
172         struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
173                                                       "GL Shader State Attribute Record");
174         assert(state);
175         assert(attr);
176         uint32_t offset = 0;
177 
178         if (including_gs) {
179                 struct v3d_group *gs_state = v3d_spec_find_struct(clif->spec,
180                                                                   "Geometry Shader State Record");
181                 assert(gs_state);
182                 out(clif, "@format shadrec_gl_geom\n");
183                 v3d_print_group(clif, gs_state, 0, vaddr + offset);
184                 offset += v3d_group_get_length(gs_state);
185                 /* Extra pad when geometry/tessellation shader is present */
186                 offset += 20;
187         }
188         out(clif, "@format shadrec_gl_main\n");
189         v3d_print_group(clif, state, 0, vaddr + offset);
190         offset += v3d_group_get_length(state);
191 
192         for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
193                 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
194                 v3d_print_group(clif, attr, 0, vaddr + offset);
195                 offset += v3d_group_get_length(attr);
196         }
197 
198         return offset;
199 }
200 
201 static void
clif_process_worklist(struct clif_dump * clif)202 clif_process_worklist(struct clif_dump *clif)
203 {
204         list_for_each_entry_safe(struct reloc_worklist_entry, reloc,
205                                  &clif->worklist, link) {
206                 void *vaddr;
207                 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
208                         out(clif, "Failed to look up address 0x%08x\n",
209                             reloc->addr);
210                         continue;
211                 }
212 
213                 switch (reloc->type) {
214                 case reloc_cl:
215                         clif_dump_cl(clif, reloc->addr, reloc->cl.end, true);
216                         break;
217 
218                 case reloc_gl_shader_state:
219                 case reloc_gl_including_gs_shader_state:
220                         break;
221                 case reloc_generic_tile_list:
222                         clif_dump_cl(clif, reloc->addr,
223                                      reloc->generic_tile_list.end, true);
224                         break;
225                 }
226         }
227 }
228 
229 static int
worklist_entry_compare(const void * a,const void * b)230 worklist_entry_compare(const void *a, const void *b)
231 {
232         return ((*(struct reloc_worklist_entry **)a)->addr -
233                 (*(struct reloc_worklist_entry **)b)->addr);
234 }
235 
236 static bool
clif_dump_if_blank(struct clif_dump * clif,struct clif_bo * bo,uint32_t start,uint32_t end)237 clif_dump_if_blank(struct clif_dump *clif, struct clif_bo *bo,
238                    uint32_t start, uint32_t end)
239 {
240         for (int i = start; i < end; i++) {
241                 if (((uint8_t *)bo->vaddr)[i] != 0)
242                         return false;
243         }
244 
245         out(clif, "\n");
246         out(clif, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end - start,
247             bo->name, start, end - 1);
248         return true;
249 }
250 
251 /* Dumps the binary data in the BO from start to end (relative to the start of
252  * the BO).
253  */
254 static void
clif_dump_binary(struct clif_dump * clif,struct clif_bo * bo,uint32_t start,uint32_t end)255 clif_dump_binary(struct clif_dump *clif, struct clif_bo *bo,
256                  uint32_t start, uint32_t end)
257 {
258         if (clif->pretty && clif->nobin)
259                 return;
260 
261         if (start == end)
262                 return;
263 
264         if (clif_dump_if_blank(clif, bo, start, end))
265                 return;
266 
267         out(clif, "@format binary /* [%s+0x%08x] */\n",
268             bo->name, start);
269 
270         uint32_t offset = start;
271         int dumped_in_line = 0;
272         while (offset < end) {
273                 if (clif_dump_if_blank(clif, bo, offset, end))
274                         return;
275 
276                 if (end - offset >= 4) {
277                         out(clif, "0x%08x ", *(uint32_t *)(bo->vaddr + offset));
278                         offset += 4;
279                 } else {
280                         out(clif, "0x%02x ", *(uint8_t *)(bo->vaddr + offset));
281                         offset++;
282                 }
283 
284                 if (++dumped_in_line == 8) {
285                         out(clif, "\n");
286                         dumped_in_line = 0;
287                 }
288         }
289         if (dumped_in_line)
290                 out(clif, "\n");
291 }
292 
293 /* Walks the list of relocations, dumping each buffer's contents (using our
294  * codegenned dump routines for pretty printing, and most importantly proper
295  * address references so that the CLIF parser can relocate buffers).
296  */
297 static void
clif_dump_buffers(struct clif_dump * clif)298 clif_dump_buffers(struct clif_dump *clif)
299 {
300         int num_relocs = 0;
301         list_for_each_entry(struct reloc_worklist_entry, reloc,
302                             &clif->worklist, link) {
303                 num_relocs++;
304         }
305         struct reloc_worklist_entry **relocs =
306                 ralloc_array(clif, struct reloc_worklist_entry *, num_relocs);
307         int i = 0;
308         list_for_each_entry(struct reloc_worklist_entry, reloc,
309                             &clif->worklist, link) {
310                 relocs[i++] = reloc;
311         }
312         qsort(relocs, num_relocs, sizeof(*relocs), worklist_entry_compare);
313 
314         struct clif_bo *bo = NULL;
315         uint32_t offset = 0;
316 
317         for (i = 0; i < num_relocs; i++) {
318                 struct reloc_worklist_entry *reloc = relocs[i];
319                 struct clif_bo *new_bo = clif_lookup_bo(clif, reloc->addr);
320 
321                 if (!new_bo) {
322                         out(clif, "Failed to look up address 0x%08x\n",
323                             reloc->addr);
324                         continue;
325                 }
326 
327                 if (new_bo != bo) {
328                         if (bo) {
329                                 /* Finish out the last of the last BO. */
330                                 clif_dump_binary(clif, bo,
331                                                  offset,
332                                                  bo->size);
333                         }
334 
335                         out(clif, "\n");
336                         out(clif, "@buffer %s\n", new_bo->name);
337                         bo = new_bo;
338                         offset = 0;
339                         bo->dumped = true;
340                 }
341 
342                 int reloc_offset = reloc->addr - bo->offset;
343                 if (offset != reloc_offset)
344                         clif_dump_binary(clif, bo, offset, reloc_offset);
345                 offset = reloc_offset;
346 
347                 switch (reloc->type) {
348                 case reloc_cl:
349                         offset = clif_dump_cl(clif, reloc->addr, reloc->cl.end,
350                                               false);
351                         out(clif, "\n");
352                         break;
353 
354                 case reloc_gl_shader_state:
355                 case reloc_gl_including_gs_shader_state:
356                         offset += clif_dump_gl_shader_state_record(clif,
357                                                                    reloc,
358                                                                    bo->vaddr +
359                                                                    offset,
360                                                                    reloc->type == reloc_gl_including_gs_shader_state);
361                         break;
362                 case reloc_generic_tile_list:
363                         offset = clif_dump_cl(clif, reloc->addr,
364                                               reloc->generic_tile_list.end,
365                                               false);
366                         break;
367                 }
368                 out(clif, "\n");
369         }
370 
371         if (bo) {
372                 clif_dump_binary(clif, bo, offset, bo->size);
373         }
374 
375         /* For any BOs that didn't have relocations, just dump them raw. */
376         for (int i = 0; i < clif->bo_count; i++) {
377                 bo = &clif->bo[i];
378                 if (bo->dumped)
379                         continue;
380                 out(clif, "@buffer %s\n", bo->name);
381                 clif_dump_binary(clif, bo, 0, bo->size);
382                 out(clif, "\n");
383         }
384 }
385 
386 void
clif_dump_add_cl(struct clif_dump * clif,uint32_t start,uint32_t end)387 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
388 {
389         struct reloc_worklist_entry *entry =
390                 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
391 
392         entry->cl.end = end;
393 }
394 
395 static int
clif_bo_offset_compare(const void * a,const void * b)396 clif_bo_offset_compare(const void *a, const void *b)
397 {
398         return ((struct clif_bo *)a)->offset - ((struct clif_bo *)b)->offset;
399 }
400 
401 void
clif_dump(struct clif_dump * clif,const struct drm_v3d_submit_cl * submit)402 clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)
403 {
404         clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);
405         clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);
406 
407         qsort(clif->bo, clif->bo_count, sizeof(clif->bo[0]),
408               clif_bo_offset_compare);
409 
410         /* A buffer needs to be defined before we can emit a CLIF address
411          * referencing it, so emit them all now.
412          */
413         for (int i = 0; i < clif->bo_count; i++) {
414                 out(clif, "@createbuf_aligned 4096 %s\n", clif->bo[i].name);
415         }
416 
417         /* Walk the worklist figuring out the locations of structs based on
418          * the CL contents.
419          */
420         clif_process_worklist(clif);
421 
422         /* Dump the contents of the buffers using the relocations we found to
423          * pretty-print structures.
424          */
425         clif_dump_buffers(clif);
426 
427         out(clif, "@add_bin 0\n  ");
428         out_address(clif, submit->bcl_start);
429         out(clif, "\n  ");
430         out_address(clif, submit->bcl_end);
431         out(clif, "\n  ");
432         out_address(clif, submit->qma);
433         out(clif, "\n  %d\n  ", submit->qms);
434         out_address(clif, submit->qts);
435         out(clif, "\n");
436         out(clif, "@wait_bin_all_cores\n");
437 
438         out(clif, "@add_render 0\n  ");
439         out_address(clif, submit->rcl_start);
440         out(clif, "\n  ");
441         out_address(clif, submit->rcl_end);
442         out(clif, "\n  ");
443         out_address(clif, submit->qma);
444         out(clif, "\n");
445         out(clif, "@wait_render_all_cores\n");
446 }
447 
448 void
clif_dump_add_bo(struct clif_dump * clif,const char * name,uint32_t offset,uint32_t size,void * vaddr)449 clif_dump_add_bo(struct clif_dump *clif, const char *name,
450                  uint32_t offset, uint32_t size, void *vaddr)
451 {
452         if (clif->bo_count >= clif->bo_array_size) {
453                 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
454                 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
455                                     clif->bo_array_size);
456         }
457 
458         /* CLIF relocs use the buffer name, so make sure they're unique. */
459         for (int i = 0; i < clif->bo_count; i++)
460                 assert(strcmp(clif->bo[i].name, name) != 0);
461 
462         clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
463         clif->bo[clif->bo_count].offset = offset;
464         clif->bo[clif->bo_count].size = size;
465         clif->bo[clif->bo_count].vaddr = vaddr;
466         clif->bo[clif->bo_count].dumped = false;
467         clif->bo_count++;
468 }
469