• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (clif->devinfo->ver >= 42)
110                 return v3d42_clif_dump_packet(clif, offset, cl, size, reloc_mode);
111         else if (clif->devinfo->ver >= 41)
112                 return v3d41_clif_dump_packet(clif, offset, cl, size, reloc_mode);
113         else
114                 return v3d33_clif_dump_packet(clif, offset, cl, size, reloc_mode);
115 }
116 
117 static uint32_t
clif_dump_cl(struct clif_dump * clif,uint32_t start,uint32_t end,bool reloc_mode)118 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end,
119              bool reloc_mode)
120 {
121         struct clif_bo *bo = clif_lookup_bo(clif, start);
122         if (!bo) {
123                 out(clif, "Failed to look up address 0x%08x\n",
124                     start);
125                 return 0;
126         }
127 
128         void *start_vaddr = bo->vaddr + start - bo->offset;
129 
130         /* The end address is optional (for example, a BRANCH instruction
131          * won't set an end), but is used for BCL/RCL termination.
132          */
133         void *end_vaddr = NULL;
134         if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
135                 out(clif, "Failed to look up address 0x%08x\n",
136                     end);
137                 return 0;
138         }
139 
140         if (!reloc_mode)
141                 out(clif, "@format ctrllist  /* [%s+0x%08x] */\n",
142                     bo->name, start - bo->offset);
143 
144         uint32_t size;
145         uint8_t *cl = start_vaddr;
146         while (clif_dump_packet(clif, start, cl, &size, reloc_mode)) {
147                 cl += size;
148                 start += size;
149 
150                 if (cl == end_vaddr)
151                         break;
152         }
153 
154         return (void *)cl - bo->vaddr;
155 }
156 
157 /* Walks the worklist, parsing the relocs for any memory regions that might
158  * themselves have additional relocations.
159  */
160 static uint32_t
clif_dump_gl_shader_state_record(struct clif_dump * clif,struct reloc_worklist_entry * reloc,void * vaddr)161 clif_dump_gl_shader_state_record(struct clif_dump *clif,
162                                  struct reloc_worklist_entry *reloc,
163                                  void *vaddr)
164 {
165         struct v3d_group *state = v3d_spec_find_struct(clif->spec,
166                                                        "GL Shader State Record");
167         struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
168                                                       "GL Shader State Attribute Record");
169         assert(state);
170         assert(attr);
171         uint32_t offset = 0;
172 
173         out(clif, "@format shadrec_gl_main\n");
174         v3d_print_group(clif, state, 0, vaddr + offset);
175         offset += v3d_group_get_length(state);
176 
177         for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
178                 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
179                 v3d_print_group(clif, attr, 0, vaddr + offset);
180                 offset += v3d_group_get_length(attr);
181         }
182 
183         return offset;
184 }
185 
186 static void
clif_process_worklist(struct clif_dump * clif)187 clif_process_worklist(struct clif_dump *clif)
188 {
189         list_for_each_entry_safe(struct reloc_worklist_entry, reloc,
190                                  &clif->worklist, link) {
191                 void *vaddr;
192                 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
193                         out(clif, "Failed to look up address 0x%08x\n",
194                             reloc->addr);
195                         continue;
196                 }
197 
198                 switch (reloc->type) {
199                 case reloc_cl:
200                         clif_dump_cl(clif, reloc->addr, reloc->cl.end, true);
201                         break;
202 
203                 case reloc_gl_shader_state:
204                         break;
205                 case reloc_generic_tile_list:
206                         clif_dump_cl(clif, reloc->addr,
207                                      reloc->generic_tile_list.end, true);
208                         break;
209                 }
210         }
211 }
212 
213 static int
worklist_entry_compare(const void * a,const void * b)214 worklist_entry_compare(const void *a, const void *b)
215 {
216         return ((*(struct reloc_worklist_entry **)a)->addr -
217                 (*(struct reloc_worklist_entry **)b)->addr);
218 }
219 
220 static bool
clif_dump_if_blank(struct clif_dump * clif,struct clif_bo * bo,uint32_t start,uint32_t end)221 clif_dump_if_blank(struct clif_dump *clif, struct clif_bo *bo,
222                    uint32_t start, uint32_t end)
223 {
224         for (int i = start; i < end; i++) {
225                 if (((uint8_t *)bo->vaddr)[i] != 0)
226                         return false;
227         }
228 
229         out(clif, "\n");
230         out(clif, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end - start,
231             bo->name, start, end - 1);
232         return true;
233 }
234 
235 /* Dumps the binary data in the BO from start to end (relative to the start of
236  * the BO).
237  */
238 static void
clif_dump_binary(struct clif_dump * clif,struct clif_bo * bo,uint32_t start,uint32_t end)239 clif_dump_binary(struct clif_dump *clif, struct clif_bo *bo,
240                  uint32_t start, uint32_t end)
241 {
242         if (clif->pretty && clif->nobin)
243                 return;
244 
245         if (start == end)
246                 return;
247 
248         if (clif_dump_if_blank(clif, bo, start, end))
249                 return;
250 
251         out(clif, "@format binary /* [%s+0x%08x] */\n",
252             bo->name, start);
253 
254         uint32_t offset = start;
255         int dumped_in_line = 0;
256         while (offset < end) {
257                 if (clif_dump_if_blank(clif, bo, offset, end))
258                         return;
259 
260                 if (end - offset >= 4) {
261                         out(clif, "0x%08x ", *(uint32_t *)(bo->vaddr + offset));
262                         offset += 4;
263                 } else {
264                         out(clif, "0x%02x ", *(uint8_t *)(bo->vaddr + offset));
265                         offset++;
266                 }
267 
268                 if (++dumped_in_line == 8) {
269                         out(clif, "\n");
270                         dumped_in_line = 0;
271                 }
272         }
273         if (dumped_in_line)
274                 out(clif, "\n");
275 }
276 
277 /* Walks the list of relocations, dumping each buffer's contents (using our
278  * codegenned dump routines for pretty printing, and most importantly proper
279  * address references so that the CLIF parser can relocate buffers).
280  */
281 static void
clif_dump_buffers(struct clif_dump * clif)282 clif_dump_buffers(struct clif_dump *clif)
283 {
284         int num_relocs = 0;
285         list_for_each_entry(struct reloc_worklist_entry, reloc,
286                             &clif->worklist, link) {
287                 num_relocs++;
288         }
289         struct reloc_worklist_entry **relocs =
290                 ralloc_array(clif, struct reloc_worklist_entry *, num_relocs);
291         int i = 0;
292         list_for_each_entry(struct reloc_worklist_entry, reloc,
293                             &clif->worklist, link) {
294                 relocs[i++] = reloc;
295         }
296         qsort(relocs, num_relocs, sizeof(*relocs), worklist_entry_compare);
297 
298         struct clif_bo *bo = NULL;
299         uint32_t offset = 0;
300 
301         for (i = 0; i < num_relocs; i++) {
302                 struct reloc_worklist_entry *reloc = relocs[i];
303                 struct clif_bo *new_bo = clif_lookup_bo(clif, reloc->addr);
304 
305                 if (!new_bo) {
306                         out(clif, "Failed to look up address 0x%08x\n",
307                             reloc->addr);
308                         continue;
309                 }
310 
311                 if (new_bo != bo) {
312                         if (bo) {
313                                 /* Finish out the last of the last BO. */
314                                 clif_dump_binary(clif, bo,
315                                                  offset,
316                                                  bo->size);
317                         }
318 
319                         out(clif, "\n");
320                         out(clif, "@buffer %s\n", new_bo->name);
321                         bo = new_bo;
322                         offset = 0;
323                         bo->dumped = true;
324                 }
325 
326                 int reloc_offset = reloc->addr - bo->offset;
327                 if (offset != reloc_offset)
328                         clif_dump_binary(clif, bo, offset, reloc_offset);
329                 offset = reloc_offset;
330 
331                 switch (reloc->type) {
332                 case reloc_cl:
333                         offset = clif_dump_cl(clif, reloc->addr, reloc->cl.end,
334                                               false);
335                         out(clif, "\n");
336                         break;
337 
338                 case reloc_gl_shader_state:
339                         offset += clif_dump_gl_shader_state_record(clif,
340                                                                    reloc,
341                                                                    bo->vaddr +
342                                                                    offset);
343                         break;
344                 case reloc_generic_tile_list:
345                         offset = clif_dump_cl(clif, reloc->addr,
346                                               reloc->generic_tile_list.end,
347                                               false);
348                         break;
349                 }
350                 out(clif, "\n");
351         }
352 
353         if (bo) {
354                 clif_dump_binary(clif, bo, offset, bo->size);
355         }
356 
357         /* For any BOs that didn't have relocations, just dump them raw. */
358         for (int i = 0; i < clif->bo_count; i++) {
359                 bo = &clif->bo[i];
360                 if (bo->dumped)
361                         continue;
362                 out(clif, "@buffer %s\n", bo->name);
363                 clif_dump_binary(clif, bo, 0, bo->size);
364                 out(clif, "\n");
365         }
366 }
367 
368 void
clif_dump_add_cl(struct clif_dump * clif,uint32_t start,uint32_t end)369 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
370 {
371         struct reloc_worklist_entry *entry =
372                 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
373 
374         entry->cl.end = end;
375 }
376 
377 static int
clif_bo_offset_compare(const void * a,const void * b)378 clif_bo_offset_compare(const void *a, const void *b)
379 {
380         return ((struct clif_bo *)a)->offset - ((struct clif_bo *)b)->offset;
381 }
382 
383 void
clif_dump(struct clif_dump * clif,const struct drm_v3d_submit_cl * submit)384 clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)
385 {
386         clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);
387         clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);
388 
389         qsort(clif->bo, clif->bo_count, sizeof(clif->bo[0]),
390               clif_bo_offset_compare);
391 
392         /* A buffer needs to be defined before we can emit a CLIF address
393          * referencing it, so emit them all now.
394          */
395         for (int i = 0; i < clif->bo_count; i++) {
396                 out(clif, "@createbuf_aligned 4096 %s\n", clif->bo[i].name);
397         }
398 
399         /* Walk the worklist figuring out the locations of structs based on
400          * the CL contents.
401          */
402         clif_process_worklist(clif);
403 
404         /* Dump the contents of the buffers using the relocations we found to
405          * pretty-print structures.
406          */
407         clif_dump_buffers(clif);
408 
409         out(clif, "@add_bin 0\n  ");
410         out_address(clif, submit->bcl_start);
411         out(clif, "\n  ");
412         out_address(clif, submit->bcl_end);
413         out(clif, "\n  ");
414         out_address(clif, submit->qma);
415         out(clif, "\n  %d\n  ", submit->qms);
416         out_address(clif, submit->qts);
417         out(clif, "\n");
418         out(clif, "@wait_bin_all_cores\n");
419 
420         out(clif, "@add_render 0\n  ");
421         out_address(clif, submit->rcl_start);
422         out(clif, "\n  ");
423         out_address(clif, submit->rcl_end);
424         out(clif, "\n  ");
425         out_address(clif, submit->qma);
426         out(clif, "\n");
427         out(clif, "@wait_render_all_cores\n");
428 }
429 
430 void
clif_dump_add_bo(struct clif_dump * clif,const char * name,uint32_t offset,uint32_t size,void * vaddr)431 clif_dump_add_bo(struct clif_dump *clif, const char *name,
432                  uint32_t offset, uint32_t size, void *vaddr)
433 {
434         if (clif->bo_count >= clif->bo_array_size) {
435                 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
436                 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
437                                     clif->bo_array_size);
438         }
439 
440         /* CLIF relocs use the buffer name, so make sure they're unique. */
441         for (int i = 0; i < clif->bo_count; i++)
442                 assert(strcmp(clif->bo[i].name, name) != 0);
443 
444         clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
445         clif->bo[clif->bo_count].offset = offset;
446         clif->bo[clif->bo_count].size = size;
447         clif->bo[clif->bo_count].vaddr = vaddr;
448         clif->bo[clif->bo_count].dumped = false;
449         clif->bo_count++;
450 }
451