v3d: Split walking the CLs to generate relocs from walking CLs to dump.
[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, bool reloc_mode)
107 {
108 if (clif->devinfo->ver >= 41)
109 return v3d41_clif_dump_packet(clif, offset, cl, size, reloc_mode);
110 else
111 return v3d33_clif_dump_packet(clif, offset, cl, size, reloc_mode);
112 }
113
114 static void
115 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end,
116 bool reloc_mode)
117 {
118 void *start_vaddr;
119 if (!clif_lookup_vaddr(clif, start, &start_vaddr)) {
120 out(clif, "Failed to look up address 0x%08x\n",
121 start);
122 return;
123 }
124
125 /* The end address is optional (for example, a BRANCH instruction
126 * won't set an end), but is used for BCL/RCL termination.
127 */
128 void *end_vaddr = NULL;
129 if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
130 out(clif, "Failed to look up address 0x%08x\n",
131 end);
132 return;
133 }
134
135 if (!reloc_mode)
136 out(clif, "@format ctrllist\n");
137
138 uint32_t size;
139 uint8_t *cl = start_vaddr;
140 while (clif_dump_packet(clif, start, cl, &size, reloc_mode)) {
141 cl += size;
142 start += size;
143
144 if (cl == end_vaddr)
145 break;
146 }
147 }
148
149 static void
150 clif_dump_gl_shader_state_record(struct clif_dump *clif,
151 struct reloc_worklist_entry *reloc,
152 void *vaddr)
153 {
154 struct v3d_group *state = v3d_spec_find_struct(clif->spec,
155 "GL Shader State Record");
156 struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
157 "GL Shader State Attribute Record");
158 assert(state);
159 assert(attr);
160
161 out(clif, "@format shadrec_gl_main\n");
162 v3d_print_group(clif, state, 0, vaddr);
163 vaddr += v3d_group_get_length(state);
164
165 for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
166 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
167 v3d_print_group(clif, attr, 0, vaddr);
168 vaddr += v3d_group_get_length(attr);
169 }
170 }
171
172 static void
173 clif_process_worklist(struct clif_dump *clif, bool reloc_mode)
174 {
175 list_for_each_entry_safe(struct reloc_worklist_entry, reloc,
176 &clif->worklist, link) {
177 void *vaddr;
178 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
179 out(clif, "Failed to look up address 0x%08x\n",
180 reloc->addr);
181 continue;
182 }
183
184 switch (reloc->type) {
185 case reloc_cl:
186 clif_dump_cl(clif, reloc->addr, reloc->cl.end,
187 reloc_mode);
188 if (!reloc_mode)
189 out(clif, "\n");
190 break;
191
192 case reloc_gl_shader_state:
193 if (reloc_mode)
194 continue;
195 clif_dump_gl_shader_state_record(clif,
196 reloc,
197 vaddr);
198 break;
199 case reloc_generic_tile_list:
200 clif_dump_cl(clif, reloc->addr,
201 reloc->generic_tile_list.end, reloc_mode);
202 break;
203 }
204 if (!reloc_mode)
205 out(clif, "\n");
206 }
207 }
208
209 void
210 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
211 {
212 struct reloc_worklist_entry *entry =
213 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
214
215 entry->cl.end = end;
216 }
217
218 void
219 clif_dump(struct clif_dump *clif, const struct drm_v3d_submit_cl *submit)
220 {
221 clif_dump_add_cl(clif, submit->bcl_start, submit->bcl_end);
222 clif_dump_add_cl(clif, submit->rcl_start, submit->rcl_end);
223
224 clif_process_worklist(clif, true);
225 clif_process_worklist(clif, false);
226
227 out(clif, "@add_bin 0\n ");
228 out_address(clif, submit->bcl_start);
229 out(clif, "\n ");
230 out_address(clif, submit->bcl_end);
231 out(clif, "\n ");
232 out_address(clif, submit->qma);
233 out(clif, "\n %d\n ", submit->qms);
234 out_address(clif, submit->qts);
235 out(clif, "\n");
236 out(clif, "@wait_bin_all_cores\n");
237
238 out(clif, "@add_render 0\n ");
239 out_address(clif, submit->rcl_start);
240 out(clif, "\n ");
241 out_address(clif, submit->rcl_end);
242 out(clif, "\n ");
243 out_address(clif, submit->qma);
244 out(clif, "\n");
245 out(clif, "@wait_render_all_cores\n");
246 }
247
248 void
249 clif_dump_add_bo(struct clif_dump *clif, const char *name,
250 uint32_t offset, uint32_t size, void *vaddr)
251 {
252 if (clif->bo_count >= clif->bo_array_size) {
253 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
254 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
255 clif->bo_array_size);
256 }
257
258 clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
259 clif->bo[clif->bo_count].offset = offset;
260 clif->bo[clif->bo_count].size = size;
261 clif->bo[clif->bo_count].vaddr = vaddr;
262 clif->bo_count++;
263 }