summaryrefslogtreecommitdiffstats
path: root/src/broadcom/clif/clif_dump.c
blob: af72834e5dc56127a72d0e12d5ed5238ec70c4fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright © 2016 Broadcom
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clif_dump.h"
#include "util/list.h"
#include "util/ralloc.h"

#include "broadcom/cle/v3d_decoder.h"

#define __gen_user_data void
#define __gen_address_type uint32_t
#define __gen_address_offset(reloc) (*reloc)
#define __gen_emit_reloc(cl, reloc)
#define __gen_unpack_address(cl, s, e) (__gen_unpack_uint(cl, s, e) << (31 - (e - s)))

enum reloc_worklist_type {
        reloc_gl_shader_state,
};

struct reloc_worklist_entry {
        struct list_head link;

        enum reloc_worklist_type type;
        uint32_t addr;

        union {
                struct {
                        uint32_t num_attrs;
                } shader_state;
        };
};

struct clif_dump {
        const struct v3d_device_info *devinfo;
        bool (*lookup_vaddr)(void *data, uint32_t addr, void **vaddr);
        FILE *out;
        /* Opaque data from the caller that is passed to the callbacks. */
        void *data;

        struct v3d_spec *spec;

        /* List of struct reloc_worklist_entry */
        struct list_head worklist;
};

static void
out(struct clif_dump *clif, const char *fmt, ...)
{
        va_list args;

        va_start(args, fmt);
        vfprintf(clif->out, fmt, args);
        va_end(args);
}

#include "broadcom/cle/v3d_packet_v33_pack.h"

static struct reloc_worklist_entry *
clif_dump_add_address_to_worklist(struct clif_dump *clif,
                                  enum reloc_worklist_type type,
                                  uint32_t addr)
{
        struct reloc_worklist_entry *entry =
                rzalloc(clif, struct reloc_worklist_entry);
        if (!entry)
                return NULL;

        entry->type = type;
        entry->addr = addr;

        list_addtail(&entry->link, &clif->worklist);

        return entry;
}

struct clif_dump *
clif_dump_init(const struct v3d_device_info *devinfo,
               FILE *out,
               bool (*lookup_vaddr)(void *data, uint32_t addr, void **vaddr),
               void *data)
{
        struct clif_dump *clif = rzalloc(NULL, struct clif_dump);

        clif->devinfo = devinfo;
        clif->lookup_vaddr = lookup_vaddr;
        clif->out = out;
        clif->data = data;
        clif->spec = v3d_spec_load(devinfo);

        list_inithead(&clif->worklist);

        return clif;
}

void
clif_dump_destroy(struct clif_dump *clif)
{
        ralloc_free(clif);
}

#define out_uint(_clif, field) out(_clif, "    /* %s = */ %u\n",        \
                            #field,  values-> field);

static bool
clif_dump_packet(struct clif_dump *clif, uint32_t offset, const uint8_t *cl,
                 uint32_t *size)
{
        struct v3d_group *inst = v3d_spec_find_instruction(clif->spec, cl);
        if (!inst) {
                out(clif, "0x%08x: Unknown packet %d!\n", offset, *cl);
                return false;
        }

        *size = v3d_group_get_length(inst);

        out(clif, "%s\n", v3d_group_get_name(inst));
        v3d_print_group(clif->out, inst, 0, cl, "");

        switch (*cl) {
        case V3D33_GL_SHADER_STATE_opcode: {
                struct V3D33_GL_SHADER_STATE values;
                V3D33_GL_SHADER_STATE_unpack(cl, &values);

                struct reloc_worklist_entry *reloc =
                        clif_dump_add_address_to_worklist(clif,
                                                          reloc_gl_shader_state,
                                                          values.address);
                if (reloc) {
                        reloc->shader_state.num_attrs =
                                values.number_of_attribute_arrays;
                }
                return true;
        }

        case V3D33_STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED_opcode: {
                struct V3D33_STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED values;
                V3D33_STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED_unpack(cl, &values);

                if (values.last_tile_of_frame)
                        return false;
                break;
        }

        case V3D33_TRANSFORM_FEEDBACK_ENABLE_opcode: {
                struct V3D33_TRANSFORM_FEEDBACK_ENABLE values;
                V3D33_TRANSFORM_FEEDBACK_ENABLE_unpack(cl, &values);
                struct v3d_group *spec = v3d_spec_find_struct(clif->spec,
                                                              "Transform Feedback Output Data Spec");
                struct v3d_group *addr = v3d_spec_find_struct(clif->spec,
                                                              "Transform Feedback Output Address");
                assert(spec);
                assert(addr);

                cl += *size;

                for (int i = 0; i < values.number_of_16_bit_output_data_specs_following; i++) {
                        v3d_print_group(clif->out, spec, 0, cl, "");
                        cl += v3d_group_get_length(spec);
                        *size += v3d_group_get_length(spec);
                }

                for (int i = 0; i < values.number_of_32_bit_output_buffer_address_following; i++) {
                        v3d_print_group(clif->out, addr, 0, cl, "");
                        cl += v3d_group_get_length(addr);
                        *size += v3d_group_get_length(addr);
                }
                break;
        }

        case V3D33_HALT_opcode:
                return false;
        }

        return true;
}

static void
clif_dump_gl_shader_state_record(struct clif_dump *clif,
                                 struct reloc_worklist_entry *reloc, void *vaddr)
{
        struct v3d_group *state = v3d_spec_find_struct(clif->spec,
                                                       "GL Shader State Record");
        struct v3d_group *attr = v3d_spec_find_struct(clif->spec,
                                                      "GL Shader State Attribute Record");
        assert(state);
        assert(attr);

        out(clif, "GL Shader State Record at 0x%08x\n", reloc->addr);
        v3d_print_group(clif->out, state, 0, vaddr, "");
        vaddr += v3d_group_get_length(state);

        for (int i = 0; i < reloc->shader_state.num_attrs; i++) {
                out(clif, "  Attribute %d\n", i);
                v3d_print_group(clif->out, attr, 0, vaddr, "");
                vaddr += v3d_group_get_length(attr);
        }
}

static void
clif_dump_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
{
        void *start_vaddr;
        if (!clif->lookup_vaddr(clif->data, start, &start_vaddr)) {
                out(clif, "Failed to look up address 0x%08x\n",
                    start);
                return;
        }

        /* The end address is optional (for example, a BRANCH instruction
         * won't set an end), but is used for BCL/RCL termination.
         */
        void *end_vaddr = NULL;
        if (end && !clif->lookup_vaddr(clif->data, end, &end_vaddr)) {
                out(clif, "Failed to look up address 0x%08x\n",
                    end);
                return;
        }

        uint32_t size;
        uint8_t *cl = start_vaddr;
        while (clif_dump_packet(clif, start, cl, &size)) {
                cl += size;
                start += size;

                if (cl == end_vaddr)
                        break;
        }
}

static void
clif_process_worklist(struct clif_dump *clif)
{
        while (!list_empty(&clif->worklist)) {
                struct reloc_worklist_entry *reloc =
                        list_first_entry(&clif->worklist,
                                         struct reloc_worklist_entry, link);
                list_del(&reloc->link);

                void *vaddr;
                if (!clif->lookup_vaddr(clif->data, reloc->addr, &vaddr)) {
                        out(clif, "Failed to look up address 0x%08x\n",
                            reloc->addr);
                        continue;
                }

                switch (reloc->type) {
                case reloc_gl_shader_state:
                        clif_dump_gl_shader_state_record(clif, reloc, vaddr);
                        break;
                }
                out(clif, "\n");
        }
}

void
clif_dump_add_cl(struct clif_dump *clif, uint32_t start, uint32_t end)
{
        clif_dump_cl(clif, start, end);
        out(clif, "\n");

        clif_process_worklist(clif);
}