2 * Copyright © 2016 Broadcom
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:
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
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
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"
33 #include "broadcom/cle/v3d_decoder.h"
35 struct reloc_worklist_entry
*
36 clif_dump_add_address_to_worklist(struct clif_dump
*clif
,
37 enum reloc_worklist_type type
,
40 struct reloc_worklist_entry
*entry
=
41 rzalloc(clif
, struct reloc_worklist_entry
);
48 list_addtail(&entry
->link
, &clif
->worklist
);
54 clif_dump_init(const struct v3d_device_info
*devinfo
,
55 FILE *out
, bool pretty
)
57 struct clif_dump
*clif
= rzalloc(NULL
, struct clif_dump
);
59 clif
->devinfo
= devinfo
;
61 clif
->spec
= v3d_spec_load(devinfo
);
62 clif
->pretty
= pretty
;
64 list_inithead(&clif
->worklist
);
70 clif_dump_destroy(struct clif_dump
*clif
)
76 clif_lookup_bo(struct clif_dump
*clif
, uint32_t addr
)
78 for (int i
= 0; i
< clif
->bo_count
; i
++) {
79 struct clif_bo
*bo
= &clif
->bo
[i
];
81 if (addr
>= bo
->offset
&&
82 addr
< bo
->offset
+ bo
->size
) {
91 clif_lookup_vaddr(struct clif_dump
*clif
, uint32_t addr
, void **vaddr
)
93 struct clif_bo
*bo
= clif_lookup_bo(clif
, addr
);
97 *vaddr
= bo
->vaddr
+ addr
- bo
->offset
;
101 #define out_uint(_clif, field) out(_clif, " /* %s = */ %u\n", \
102 #field, values-> field);
105 clif_dump_packet(struct clif_dump
*clif
, uint32_t offset
, const uint8_t *cl
,
106 uint32_t *size
, bool reloc_mode
)
108 if (clif
->devinfo
->ver
>= 41)
109 return v3d41_clif_dump_packet(clif
, offset
, cl
, size
, reloc_mode
);
111 return v3d33_clif_dump_packet(clif
, offset
, cl
, size
, reloc_mode
);
115 clif_dump_cl(struct clif_dump
*clif
, uint32_t start
, uint32_t end
,
118 struct clif_bo
*bo
= clif_lookup_bo(clif
, start
);
120 out(clif
, "Failed to look up address 0x%08x\n",
125 void *start_vaddr
= bo
->vaddr
+ start
- bo
->offset
;
127 /* The end address is optional (for example, a BRANCH instruction
128 * won't set an end), but is used for BCL/RCL termination.
130 void *end_vaddr
= NULL
;
131 if (end
&& !clif_lookup_vaddr(clif
, end
, &end_vaddr
)) {
132 out(clif
, "Failed to look up address 0x%08x\n",
138 out(clif
, "@format ctrllist /* [%s+0x%08x] */\n",
139 bo
->name
, start
- bo
->offset
);
142 uint8_t *cl
= start_vaddr
;
143 while (clif_dump_packet(clif
, start
, cl
, &size
, reloc_mode
)) {
151 return (void *)cl
- bo
->vaddr
;
154 /* Walks the worklist, parsing the relocs for any memory regions that might
155 * themselves have additional relocations.
158 clif_dump_gl_shader_state_record(struct clif_dump
*clif
,
159 struct reloc_worklist_entry
*reloc
,
162 struct v3d_group
*state
= v3d_spec_find_struct(clif
->spec
,
163 "GL Shader State Record");
164 struct v3d_group
*attr
= v3d_spec_find_struct(clif
->spec
,
165 "GL Shader State Attribute Record");
170 out(clif
, "@format shadrec_gl_main\n");
171 v3d_print_group(clif
, state
, 0, vaddr
+ offset
);
172 offset
+= v3d_group_get_length(state
);
174 for (int i
= 0; i
< reloc
->shader_state
.num_attrs
; i
++) {
175 out(clif
, "@format shadrec_gl_attr /* %d */\n", i
);
176 v3d_print_group(clif
, attr
, 0, vaddr
+ offset
);
177 offset
+= v3d_group_get_length(attr
);
184 clif_process_worklist(struct clif_dump
*clif
)
186 list_for_each_entry_safe(struct reloc_worklist_entry
, reloc
,
187 &clif
->worklist
, link
) {
189 if (!clif_lookup_vaddr(clif
, reloc
->addr
, &vaddr
)) {
190 out(clif
, "Failed to look up address 0x%08x\n",
195 switch (reloc
->type
) {
197 clif_dump_cl(clif
, reloc
->addr
, reloc
->cl
.end
, true);
200 case reloc_gl_shader_state
:
202 case reloc_generic_tile_list
:
203 clif_dump_cl(clif
, reloc
->addr
,
204 reloc
->generic_tile_list
.end
, true);
211 worklist_entry_compare(const void *a
, const void *b
)
213 return ((*(struct reloc_worklist_entry
**)a
)->addr
-
214 (*(struct reloc_worklist_entry
**)b
)->addr
);
218 clif_dump_if_blank(struct clif_dump
*clif
, struct clif_bo
*bo
,
219 uint32_t start
, uint32_t end
)
221 for (int i
= start
; i
< end
; i
++) {
222 if (((uint8_t *)bo
->vaddr
)[i
] != 0)
227 out(clif
, "@format blank %d /* [%s+0x%08x..0x%08x] */\n", end
- start
,
228 bo
->name
, start
, end
- 1);
232 /* Dumps the binary data in the BO from start to end (relative to the start of
236 clif_dump_binary(struct clif_dump
*clif
, struct clif_bo
*bo
,
237 uint32_t start
, uint32_t end
)
242 if (clif_dump_if_blank(clif
, bo
, start
, end
))
245 out(clif
, "@format binary /* [%s+0x%08x] */\n",
248 uint32_t offset
= start
;
249 int dumped_in_line
= 0;
250 while (offset
< end
) {
251 if (clif_dump_if_blank(clif
, bo
, offset
, end
))
254 if (end
- offset
>= 4) {
255 out(clif
, "0x%08x ", *(uint32_t *)(bo
->vaddr
+ offset
));
258 out(clif
, "0x%02x ", *(uint8_t *)(bo
->vaddr
+ offset
));
262 if (++dumped_in_line
== 8) {
271 /* Walks the list of relocations, dumping each buffer's contents (using our
272 * codegenned dump routines for pretty printing, and most importantly proper
273 * address references so that the CLIF parser can relocate buffers).
276 clif_dump_buffers(struct clif_dump
*clif
)
279 list_for_each_entry(struct reloc_worklist_entry
, reloc
,
280 &clif
->worklist
, link
) {
283 struct reloc_worklist_entry
**relocs
=
284 ralloc_array(clif
, struct reloc_worklist_entry
*, num_relocs
);
286 list_for_each_entry(struct reloc_worklist_entry
, reloc
,
287 &clif
->worklist
, link
) {
290 qsort(relocs
, num_relocs
, sizeof(*relocs
), worklist_entry_compare
);
292 struct clif_bo
*bo
= NULL
;
295 for (i
= 0; i
< num_relocs
; i
++) {
296 struct reloc_worklist_entry
*reloc
= relocs
[i
];
297 struct clif_bo
*new_bo
= clif_lookup_bo(clif
, reloc
->addr
);
300 out(clif
, "Failed to look up address 0x%08x\n",
307 /* Finish out the last of the last BO. */
308 clif_dump_binary(clif
, bo
,
314 out(clif
, "@buffer %s\n", new_bo
->name
);
320 int reloc_offset
= reloc
->addr
- bo
->offset
;
321 if (offset
!= reloc_offset
)
322 clif_dump_binary(clif
, bo
, offset
, reloc_offset
);
323 offset
= reloc_offset
;
325 switch (reloc
->type
) {
327 offset
= clif_dump_cl(clif
, reloc
->addr
, reloc
->cl
.end
,
332 case reloc_gl_shader_state
:
333 offset
+= clif_dump_gl_shader_state_record(clif
,
338 case reloc_generic_tile_list
:
339 offset
= clif_dump_cl(clif
, reloc
->addr
,
340 reloc
->generic_tile_list
.end
,
348 clif_dump_binary(clif
, bo
, offset
, bo
->size
);
351 /* For any BOs that didn't have relocations, just dump them raw. */
352 for (int i
= 0; i
< clif
->bo_count
; i
++) {
356 out(clif
, "@buffer %s\n", bo
->name
);
357 clif_dump_binary(clif
, bo
, 0, bo
->size
);
363 clif_dump_add_cl(struct clif_dump
*clif
, uint32_t start
, uint32_t end
)
365 struct reloc_worklist_entry
*entry
=
366 clif_dump_add_address_to_worklist(clif
, reloc_cl
, start
);
372 clif_bo_offset_compare(const void *a
, const void *b
)
374 return ((struct clif_bo
*)a
)->offset
- ((struct clif_bo
*)b
)->offset
;
378 clif_dump(struct clif_dump
*clif
, const struct drm_v3d_submit_cl
*submit
)
380 clif_dump_add_cl(clif
, submit
->bcl_start
, submit
->bcl_end
);
381 clif_dump_add_cl(clif
, submit
->rcl_start
, submit
->rcl_end
);
383 qsort(clif
->bo
, clif
->bo_count
, sizeof(clif
->bo
[0]),
384 clif_bo_offset_compare
);
386 /* A buffer needs to be defined before we can emit a CLIF address
387 * referencing it, so emit them all now.
389 for (int i
= 0; i
< clif
->bo_count
; i
++) {
390 out(clif
, "@createbuf_aligned 4096 %s\n", clif
->bo
[i
].name
);
393 /* Walk the worklist figuring out the locations of structs based on
396 clif_process_worklist(clif
);
398 /* Dump the contents of the buffers using the relocations we found to
399 * pretty-print structures.
401 clif_dump_buffers(clif
);
403 out(clif
, "@add_bin 0\n ");
404 out_address(clif
, submit
->bcl_start
);
406 out_address(clif
, submit
->bcl_end
);
408 out_address(clif
, submit
->qma
);
409 out(clif
, "\n %d\n ", submit
->qms
);
410 out_address(clif
, submit
->qts
);
412 out(clif
, "@wait_bin_all_cores\n");
414 out(clif
, "@add_render 0\n ");
415 out_address(clif
, submit
->rcl_start
);
417 out_address(clif
, submit
->rcl_end
);
419 out_address(clif
, submit
->qma
);
421 out(clif
, "@wait_render_all_cores\n");
425 clif_dump_add_bo(struct clif_dump
*clif
, const char *name
,
426 uint32_t offset
, uint32_t size
, void *vaddr
)
428 if (clif
->bo_count
>= clif
->bo_array_size
) {
429 clif
->bo_array_size
= MAX2(4, clif
->bo_array_size
* 2);
430 clif
->bo
= reralloc(clif
, clif
->bo
, struct clif_bo
,
431 clif
->bo_array_size
);
434 /* CLIF relocs use the buffer name, so make sure they're unique. */
435 for (int i
= 0; i
< clif
->bo_count
; i
++)
436 assert(strcmp(clif
->bo
[i
].name
, name
) != 0);
438 clif
->bo
[clif
->bo_count
].name
= ralloc_strdup(clif
, name
);
439 clif
->bo
[clif
->bo_count
].offset
= offset
;
440 clif
->bo
[clif
->bo_count
].size
= size
;
441 clif
->bo
[clif
->bo_count
].vaddr
= vaddr
;
442 clif
->bo
[clif
->bo_count
].dumped
= false;