v3d: Use a short, underscored name for packets in CLIF/CL dumping.
[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 "clif_dump.h"
28 #include "clif_private.h"
29 #include "util/list.h"
30 #include "util/ralloc.h"
31
32 #include "broadcom/cle/v3d_decoder.h"
33
34 struct reloc_worklist_entry *
35 clif_dump_add_address_to_worklist(struct clif_dump *clif,
36 enum reloc_worklist_type type,
37 uint32_t addr)
38 {
39 struct reloc_worklist_entry *entry =
40 rzalloc(clif, struct reloc_worklist_entry);
41 if (!entry)
42 return NULL;
43
44 entry->type = type;
45 entry->addr = addr;
46
47 list_addtail(&entry->link, &clif->worklist);
48
49 return entry;
50 }
51
52 struct clif_dump *
53 clif_dump_init(const struct v3d_device_info *devinfo,
54 FILE *out, bool pretty)
55 {
56 struct clif_dump *clif = rzalloc(NULL, struct clif_dump);
57
58 clif->devinfo = devinfo;
59 clif->out = out;
60 clif->spec = v3d_spec_load(devinfo);
61 clif->pretty = pretty;
62
63 list_inithead(&clif->worklist);
64
65 return clif;
66 }
67
68 void
69 clif_dump_destroy(struct clif_dump *clif)
70 {
71 ralloc_free(clif);
72 }
73
74 struct clif_bo *
75 clif_lookup_bo(struct clif_dump *clif, uint32_t addr)
76 {
77 for (int i = 0; i < clif->bo_count; i++) {
78 struct clif_bo *bo = &clif->bo[i];
79
80 if (addr >= bo->offset &&
81 addr < bo->offset + bo->size) {
82 return bo;
83 }
84 }
85
86 return NULL;
87 }
88
89 static bool
90 clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)
91 {
92 struct clif_bo *bo = clif_lookup_bo(clif, addr);
93 if (!bo)
94 return false;
95
96 *vaddr = bo->vaddr + addr - bo->offset;
97 return true;
98 }
99
100 #define out_uint(_clif, field) out(_clif, " /* %s = */ %u\n", \
101 #field, values-> field);
102
103 static bool
104 clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
105 uint32_t *size)
106 {
107 if (clif->devinfo->ver >= 41)
108 return v3d41_clif_dump_packet(clif, offset, cl, size);
109 else
110 return v3d33_clif_dump_packet(clif, offset, cl, size);
111 }
112
113 static void
114 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
115 {
116 void *start_vaddr;
117 if (!clif_lookup_vaddr(clif, start, &start_vaddr)) {
118 out(clif, "Failed to look up address 0x%08x\n",
119 start);
120 return;
121 }
122
123 /* The end address is optional (for example, a BRANCH instruction
124 * won't set an end), but is used for BCL/RCL termination.
125 */
126 void *end_vaddr = NULL;
127 if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
128 out(clif, "Failed to look up address 0x%08x\n",
129 end);
130 return;
131 }
132
133 out(clif, "@format ctrllist\n");
134
135 uint32_t size;
136 uint8_t *cl = start_vaddr;
137 while (clif_dump_packet(clif, start, cl, &size)) {
138 cl += size;
139 start += size;
140
141 if (cl == end_vaddr)
142 break;
143 }
144 }
145
146 static void
147 clif_dump_gl_shader_state_record(struct clif_dump *clif,
148 struct reloc_worklist_entry *reloc,
149 void *vaddr)
150 {
151 struct v3d_group *state = v3d_spec_find_struct(clif->spec,
152 "GL Shader State Record");
153 struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
154 "GL Shader State Attribute Record");
155 assert(state);
156 assert(attr);
157
158 out(clif, "@format shadrec_gl_main\n");
159 v3d_print_group(clif, state, 0, vaddr);
160 vaddr += v3d_group_get_length(state);
161
162 for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
163 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
164 v3d_print_group(clif, attr, 0, vaddr);
165 vaddr += v3d_group_get_length(attr);
166 }
167 }
168
169 static void
170 clif_process_worklist(struct clif_dump *clif)
171 {
172 while (!list_empty(&clif->worklist)) {
173 struct reloc_worklist_entry *reloc =
174 list_first_entry(&clif->worklist,
175 struct reloc_worklist_entry, link);
176 list_del(&reloc->link);
177
178 void *vaddr;
179 if (!clif_lookup_vaddr(clif, reloc->addr, &vaddr)) {
180 out(clif, "Failed to look up address 0x%08x\n",
181 reloc->addr);
182 continue;
183 }
184
185 switch (reloc->type) {
186 case reloc_cl:
187 clif_dump_cl(clif, reloc->addr, reloc->cl.end);
188 out(clif, "\n");
189 break;
190
191 case reloc_gl_shader_state:
192 clif_dump_gl_shader_state_record(clif,
193 reloc,
194 vaddr);
195 break;
196 case reloc_generic_tile_list:
197 clif_dump_cl(clif, reloc->addr,
198 reloc->generic_tile_list.end);
199 break;
200 }
201 out(clif, "\n");
202 }
203 }
204
205 void clif_dump(struct clif_dump *clif)
206 {
207 clif_process_worklist(clif);
208 }
209
210 void
211 clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
212 {
213 struct reloc_worklist_entry *entry =
214 clif_dump_add_address_to_worklist(clif, reloc_cl, start);
215
216 entry->cl.end = end;
217 }
218
219 void
220 clif_dump_add_bo(struct clif_dump *clif, const char *name,
221 uint32_t offset, uint32_t size, void *vaddr)
222 {
223 if (clif->bo_count >= clif->bo_array_size) {
224 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
225 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
226 clif->bo_array_size);
227 }
228
229 clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
230 clif->bo[clif->bo_count].offset = offset;
231 clif->bo[clif->bo_count].size = size;
232 clif->bo[clif->bo_count].vaddr = vaddr;
233 clif->bo_count++;
234 }