1/*
2 * Copyright © 2016-2018 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 <ctype.h>
25#include <stdlib.h>
26#include <string.h>
27#include "util/macros.h"
28#include "broadcom/cle/v3d_decoder.h"
29#include "clif_dump.h"
30#include "clif_private.h"
31
32#define __gen_user_data void
33#define __gen_address_type uint32_t
34#define __gen_address_offset(reloc) (*reloc)
35#define __gen_emit_reloc(cl, reloc)
36#define __gen_unpack_address(cl, s, e) (__gen_unpack_uint(cl, s, e) << (31 - (e - s)))
37#include "broadcom/cle/v3dx_pack.h"
38#include "broadcom/common/v3d_macros.h"
39
40static char *
41clif_name(const char *xml_name)
42{
43        char *name = malloc(strlen(xml_name) + 1);
44
45        int j = 0;
46        for (int i = 0; i < strlen(xml_name); i++) {
47                if (xml_name[i] == ' ') {
48                        name[j++] = '_';
49                } else if (xml_name[i] == '(' || xml_name[i] == ')') {
50                        /* skip */
51                } else {
52                        name[j++] = toupper(xml_name[i]);
53                }
54        }
55        name[j++] = 0;
56
57        return name;
58}
59
60bool
61v3dX(clif_dump_packet)(struct clif_dump *clif, uint32_t offset,
62                       const uint8_t *cl, uint32_t *size, bool reloc_mode)
63{
64        struct v3d_group *inst = v3d_spec_find_instruction(clif->spec, cl);
65        if (!inst) {
66                out(clif, "0x%08x: Unknown packet %d!\n", offset, *cl);
67                return false;
68        }
69
70        *size = v3d_group_get_length(inst);
71
72        if (!reloc_mode) {
73                char *name = clif_name(v3d_group_get_name(inst));
74                out(clif, "%s\n", name);
75                free(name);
76                v3d_print_group(clif, inst, 0, cl);
77        }
78
79        switch (*cl) {
80        case V3DX(GL_SHADER_STATE_opcode): {
81                struct V3DX(GL_SHADER_STATE) values;
82                V3DX(GL_SHADER_STATE_unpack)(cl, &values);
83
84                if (reloc_mode) {
85                        struct reloc_worklist_entry *reloc =
86                                clif_dump_add_address_to_worklist(clif,
87                                                                  reloc_gl_shader_state,
88                                                                  values.address);
89                        if (reloc) {
90                                reloc->shader_state.num_attrs =
91                                        values.number_of_attribute_arrays;
92                        }
93                }
94                return true;
95        }
96
97#if V3D_VERSION >= 41
98        case V3DX(GL_SHADER_STATE_INCLUDING_GS_opcode): {
99                struct V3DX(GL_SHADER_STATE_INCLUDING_GS) values;
100                V3DX(GL_SHADER_STATE_INCLUDING_GS_unpack)(cl, &values);
101
102                if (reloc_mode) {
103                        struct reloc_worklist_entry *reloc =
104                                clif_dump_add_address_to_worklist(clif,
105                                                                  reloc_gl_including_gs_shader_state,
106                                                                  values.address);
107                        if (reloc) {
108                                reloc->shader_state.num_attrs =
109                                        values.number_of_attribute_arrays;
110                        }
111                }
112                return true;
113        }
114#endif /* V3D_VERSION >= 41 */
115
116#if V3D_VERSION < 40
117        case V3DX(STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED_opcode): {
118                struct V3DX(STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED) values;
119                V3DX(STORE_MULTI_SAMPLE_RESOLVED_TILE_COLOR_BUFFER_EXTENDED_unpack)(cl, &values);
120
121                if (values.last_tile_of_frame)
122                        return false;
123                break;
124        }
125#endif /* V3D_VERSION < 40 */
126
127#if V3D_VERSION > 40
128        case V3DX(TRANSFORM_FEEDBACK_SPECS_opcode): {
129                struct V3DX(TRANSFORM_FEEDBACK_SPECS) values;
130                V3DX(TRANSFORM_FEEDBACK_SPECS_unpack)(cl, &values);
131                struct v3d_group *spec = v3d_spec_find_struct(clif->spec,
132                                                              "Transform Feedback Output Data Spec");
133                assert(spec);
134
135                cl += *size;
136
137                for (int i = 0; i < values.number_of_16_bit_output_data_specs_following; i++) {
138                        if (!reloc_mode)
139                                v3d_print_group(clif, spec, 0, cl);
140                        cl += v3d_group_get_length(spec);
141                        *size += v3d_group_get_length(spec);
142                }
143                if (!reloc_mode)
144                        out(clif, "@format ctrllist\n");
145                break;
146        }
147#else /* V3D_VERSION < 40 */
148        case V3DX(TRANSFORM_FEEDBACK_ENABLE_opcode): {
149                struct V3DX(TRANSFORM_FEEDBACK_ENABLE) values;
150                V3DX(TRANSFORM_FEEDBACK_ENABLE_unpack)(cl, &values);
151                struct v3d_group *spec = v3d_spec_find_struct(clif->spec,
152                                                              "Transform Feedback Output Data Spec");
153                struct v3d_group *addr = v3d_spec_find_struct(clif->spec,
154                                                              "Transform Feedback Output Address");
155                assert(spec);
156                assert(addr);
157
158                cl += *size;
159
160                for (int i = 0; i < values.number_of_16_bit_output_data_specs_following; i++) {
161                        if (!reloc_mode)
162                                v3d_print_group(clif, spec, 0, cl);
163                        cl += v3d_group_get_length(spec);
164                        *size += v3d_group_get_length(spec);
165                }
166
167                for (int i = 0; i < values.number_of_32_bit_output_buffer_address_following; i++) {
168                        if (!reloc_mode)
169                                v3d_print_group(clif, addr, 0, cl);
170                        cl += v3d_group_get_length(addr);
171                        *size += v3d_group_get_length(addr);
172                }
173                break;
174        }
175#endif /* V3D_VERSION < 40 */
176
177        case V3DX(START_ADDRESS_OF_GENERIC_TILE_LIST_opcode): {
178                struct V3DX(START_ADDRESS_OF_GENERIC_TILE_LIST) values;
179                V3DX(START_ADDRESS_OF_GENERIC_TILE_LIST_unpack)(cl, &values);
180                struct reloc_worklist_entry *reloc =
181                        clif_dump_add_address_to_worklist(clif,
182                                                          reloc_generic_tile_list,
183                                                          values.start);
184                reloc->generic_tile_list.end = values.end;
185                break;
186        }
187
188        case V3DX(HALT_opcode):
189                return false;
190        }
191
192        return true;
193}
194