9562686c6d34dde3c2d4419b04b8f061b550fe0d
[mesa.git] / src / broadcom / clif / clif_dump.c
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 *
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 *
54 clif_dump_init(const struct v3d_device_info *devinfo,
55 FILE *out, bool pretty)
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
64 list_inithead(&clif->worklist);
65
66 return clif;
67 }
68
69 void
70 clif_dump_destroy(struct clif_dump *clif)
71 {
72 ralloc_free(clif);
73 }
74
75 struct clif_bo *
76 clif_lookup_bo(struct clif_dump *clif, uint32_t addr)
77 {
78 for (int i = 0; i < clif->bo_count; i++) {
79 struct clif_bo *bo = &clif->bo[i];
80
81 if (addr >= bo->offset &&
82 addr < bo->offset + bo->size) {
83 return bo;
84 }
85 }
86
87 return NULL;
88 }
89
90 static bool
91 clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)
92 {
93 struct clif_bo *bo = clif_lookup_bo(clif, addr);
94 if (!bo)
95 return false;
96
97 *vaddr = bo->vaddr + addr - bo->offset;
98 return true;
99 }
100
101 #define out_uint(_clif, field) out(_clif, " /* %s = */ %u\n", \
102 #field, values-> field);
103
104 static bool
105 clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
106 uint32_t *size)
107 {
108 if (clif->devinfo->ver >= 41)
109 return v3d41_clif_dump_packet(clif, offset, cl, size);
110 else
111 return v3d33_clif_dump_packet(clif, offset, cl, size);
112 }
113
114 static void
115 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
116 {
117 void *start_vaddr;
118 if (!clif_lookup_vaddr(clif, start, &start_vaddr)) {
119 out(clif, "Failed to look up address 0x%08x\n",
120 start);
121 return;
122 }
123
124 /* The end address is optional (for example, a BRANCH instruction
125 * won't set an end), but is used for BCL/RCL termination.
126 */
127 void *end_vaddr = NULL;
128 if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
129 out(clif, "Failed to look up address 0x%08x\n",
130 end);
131 return;
132 }
133
134 out(clif, "@format ctrllist\n");
135
136 uint32_t size;
137 uint8_t *cl = start_vaddr;
138 while (clif_dump_packet(clif, start, cl, &size)) {
139 cl += size;
140 start += size;
141
142 if (cl == end_vaddr)
143 break;
144 }
145 }
146
147 static void
148 clif_dump_gl_shader_state_record(struct clif_dump *clif,
149 struct reloc_worklist_entry *reloc,
150 void *vaddr)
151 {
152 struct v3d_group *state = v3d_spec_find_struct(clif->spec,
153 "GL Shader State Record");
154 struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
155 "GL Shader State Attribute Record");
156 assert(state);
157 assert(attr);
158
159 out(clif, "@format shadrec_gl_main\n");
160 v3d_print_group(clif, state, 0, vaddr);
161 vaddr += v3d_group_get_length(state);
162
163 for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
164 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
165 v3d_print_group(clif, attr, 0, vaddr);
166 vaddr += v3d_group_get_length(attr);
167 }
168 }
169
170 static void
171 clif_process_worklist(struct clif_dump *clif)
172 {
173 while (!list_empty(&clif->worklist)) {
174 struct reloc_worklist_entry *reloc =
175 list_first_entry(&clif->worklist,
176 struct reloc_worklist_entry, link);
177 list_del(&reloc->link);
178
179 void *vaddr;
180 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
181 out(clif, "Failed to look up address 0x%08x\n",
182 reloc->addr);
183 continue;
184 }
185
186 switch (reloc->type) {
187 case reloc_cl:
188 clif_dump_cl(clif, reloc->addr, reloc->cl.end);
189 out(clif, "\n");
190 break;
191
192 case reloc_gl_shader_state:
193 clif_dump_gl_shader_state_record(clif,
194 reloc,
195 vaddr);
196 break;
197 case reloc_generic_tile_list:
198 clif_dump_cl(clif, reloc->addr,
199 reloc->generic_tile_list.end);
200 break;
201 }
202 out(clif, "\n");
203 }
204 }
205
206 void
207 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
208 {
209 struct reloc_worklist_entry *entry =
210 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
211
212 entry->cl.end = end;
213 }
214
215 void
216 clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)
217 {
218 clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);
219 clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);
220
221 clif_process_worklist(clif);
222
223 out(clif, "@add_bin 0\n ");
224 out_address(clif, submit->bcl_start);
225 out(clif, "\n ");
226 out_address(clif, submit->bcl_end);
227 out(clif, "\n ");
228 out_address(clif, submit->qma);
229 out(clif, "\n %d\n ", submit->qms);
230 out_address(clif, submit->qts);
231 out(clif, "\n");
232 out(clif, "@wait_bin_all_cores\n");
233
234 out(clif, "@add_render 0\n ");
235 out_address(clif, submit->rcl_start);
236 out(clif, "\n ");
237 out_address(clif, submit->rcl_end);
238 out(clif, "\n ");
239 out_address(clif, submit->qma);
240 out(clif, "\n");
241 out(clif, "@wait_render_all_cores\n");
242 }
243
244 void
245 clif_dump_add_bo(struct clif_dump *clif, const char *name,
246 uint32_t offset, uint32_t size, void *vaddr)
247 {
248 if (clif->bo_count >= clif->bo_array_size) {
249 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
250 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
251 clif->bo_array_size);
252 }
253
254 clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
255 clif->bo[clif->bo_count].offset = offset;
256 clif->bo[clif->bo_count].size = size;
257 clif->bo[clif->bo_count].vaddr = vaddr;
258 clif->bo_count++;
259 }