OSDN Git Service

v3d: Emit commands to switch CLIF parser to CL/shader/attr input mode.
[android-x86/external-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)
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
62         list_inithead(&clif->worklist);
63
64         return clif;
65 }
66
67 void
68 clif_dump_destroy(struct clif_dump *clif)
69 {
70         ralloc_free(clif);
71 }
72
73 struct clif_bo *
74 clif_lookup_bo(struct clif_dump *clif, uint32_t addr)
75 {
76         for (int i = 0; i < clif->bo_count; i++) {
77                 struct clif_bo *bo = &clif->bo[i];
78
79                 if (addr >= bo->offset &&
80                     addr < bo->offset + bo->size) {
81                         return bo;
82                 }
83         }
84
85         return NULL;
86 }
87
88 static bool
89 clif_lookup_vaddr(struct clif_dump *clif, uint32_t addr, void **vaddr)
90 {
91         struct clif_bo *bo = clif_lookup_bo(clif, addr);
92         if (!bo)
93                 return false;
94
95         *vaddr = bo->vaddr + addr - bo->offset;
96         return true;
97 }
98
99 #define out_uint(_clif, field) out(_clif, "    /* %s = */ %u\n",        \
100                             #field,  values-> field);
101
102 static bool
103 clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
104                  uint32_t *size)
105 {
106         if (clif->devinfo->ver >= 41)
107                 return v3d41_clif_dump_packet(clif, offset, cl, size);
108         else
109                 return v3d33_clif_dump_packet(clif, offset, cl, size);
110 }
111
112 static void
113 clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
114 {
115         void *start_vaddr;
116         if (!clif_lookup_vaddr(clif, start, &start_vaddr)) {
117                 out(clif, "Failed to look up address 0x%08x\n",
118                     start);
119                 return;
120         }
121
122         /* The end address is optional (for example, a BRANCH instruction
123          * won't set an end), but is used for BCL/RCL termination.
124          */
125         void *end_vaddr = NULL;
126         if (end && !clif_lookup_vaddr(clif, end, &end_vaddr)) {
127                 out(clif, "Failed to look up address 0x%08x\n",
128                     end);
129                 return;
130         }
131
132         out(clif, "@format ctrllist\n");
133
134         uint32_t size;
135         uint8_t *cl = start_vaddr;
136         while (clif_dump_packet(clif, start, cl, &size)) {
137                 cl += size;
138                 start += size;
139
140                 if (cl == end_vaddr)
141                         break;
142         }
143 }
144
145 static void
146 clif_dump_gl_shader_state_record(struct clif_dump *clif,
147                                  struct reloc_worklist_entry *reloc,
148                                  void *vaddr)
149 {
150         struct v3d_group *state = v3d_spec_find_struct(clif->spec,
151                                                        "GL Shader State Record");
152         struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
153                                                       "GL Shader State Attribute Record");
154         assert(state);
155         assert(attr);
156
157         out(clif, "@format shadrec_gl_main\n");
158         v3d_print_group(clif, state, 0, vaddr);
159         vaddr += v3d_group_get_length(state);
160
161         for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
162                 out(clif, "@format shadrec_gl_attr /* %d */\n", i);
163                 v3d_print_group(clif, attr, 0, vaddr);
164                 vaddr += v3d_group_get_length(attr);
165         }
166 }
167
168 static void
169 clif_process_worklist(struct clif_dump *clif)
170 {
171         while (!list_empty(&clif->worklist)) {
172                 struct reloc_worklist_entry *reloc =
173                         list_first_entry(&clif->worklist,
174                                          struct reloc_worklist_entry, link);
175                 list_del(&reloc->link);
176
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                         out(clif, "\n");
188                         break;
189
190                 case reloc_gl_shader_state:
191                         clif_dump_gl_shader_state_record(clif,
192                                                          reloc,
193                                                          vaddr);
194                         break;
195                 case reloc_generic_tile_list:
196                         clif_dump_cl(clif, reloc->addr,
197                                      reloc->generic_tile_list.end);
198                         break;
199                 }
200                 out(clif, "\n");
201         }
202 }
203
204 void clif_dump(struct clif_dump *clif)
205 {
206         clif_process_worklist(clif);
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_add_bo(struct clif_dump *clif, const char *name,
220                  uint32_t offset, uint32_t size, void *vaddr)
221 {
222         if (clif->bo_count >= clif->bo_array_size) {
223                 clif->bo_array_size = MAX2(4, clif->bo_array_size * 2);
224                 clif->bo = reralloc(clif, clif->bo, struct clif_bo,
225                                     clif->bo_array_size);
226         }
227
228         clif->bo[clif->bo_count].name = ralloc_strdup(clif, name);
229         clif->bo[clif->bo_count].offset = offset;
230         clif->bo[clif->bo_count].size = size;
231         clif->bo[clif->bo_count].vaddr = vaddr;
232         clif->bo_count++;
233 }