pan/midgard: Add units for more instructions
[mesa.git] / src / panfrost / midgard / helpers.h
1 /* Copyright (c) 2018-2019 Alyssa Rosenzweig (alyssa@rosenzweig.io)
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22 #ifndef __MDG_HELPERS_H
23 #define __MDG_HELPERS_H
24
25 #include "util/macros.h"
26 #include <string.h>
27
28 #define OP_IS_LOAD_VARY_F(op) (\
29 op == midgard_op_ld_vary_16 || \
30 op == midgard_op_ld_vary_32 \
31 )
32
33 #define OP_IS_STORE_VARY(op) (\
34 op == midgard_op_st_vary_16 || \
35 op == midgard_op_st_vary_32 || \
36 op == midgard_op_st_vary_32u || \
37 op == midgard_op_st_vary_32i \
38 )
39
40 #define OP_IS_STORE_R26(op) (\
41 OP_IS_STORE_VARY(op) || \
42 op == midgard_op_st_char || \
43 op == midgard_op_st_char2 || \
44 op == midgard_op_st_char4 || \
45 op == midgard_op_st_short4 || \
46 op == midgard_op_st_int4 \
47 )
48
49 #define OP_IS_STORE(op) (\
50 OP_IS_STORE_VARY(op) || \
51 op == midgard_op_st_cubemap_coords \
52 )
53
54 #define OP_IS_PROJECTION(op) ( \
55 op == midgard_op_ldst_perspective_division_z || \
56 op == midgard_op_ldst_perspective_division_w \
57 )
58
59 #define OP_IS_R27_ONLY(op) ( \
60 OP_IS_PROJECTION(op) || \
61 op == midgard_op_st_cubemap_coords \
62 )
63
64 #define OP_IS_MOVE(op) ( \
65 op == midgard_alu_op_fmov || \
66 op == midgard_alu_op_imov \
67 )
68
69 #define OP_IS_UBO_READ(op) ( \
70 op == midgard_op_ld_uniform_32 || \
71 op == midgard_op_ld_uniform_16 || \
72 op == midgard_op_ld_uniform_32i \
73 )
74
75 #define OP_IS_CSEL(op) ( \
76 op == midgard_alu_op_icsel || \
77 op == midgard_alu_op_icsel_v || \
78 op == midgard_alu_op_fcsel_v || \
79 op == midgard_alu_op_fcsel \
80 )
81
82 #define OP_IS_DERIVATIVE(op) ( \
83 op == TEXTURE_OP_DFDX || \
84 op == TEXTURE_OP_DFDY \
85 )
86
87 /* ALU control words are single bit fields with a lot of space */
88
89 #define ALU_ENAB_VEC_MUL (1 << 17)
90 #define ALU_ENAB_SCAL_ADD (1 << 19)
91 #define ALU_ENAB_VEC_ADD (1 << 21)
92 #define ALU_ENAB_SCAL_MUL (1 << 23)
93 #define ALU_ENAB_VEC_LUT (1 << 25)
94 #define ALU_ENAB_BR_COMPACT (1 << 26)
95 #define ALU_ENAB_BRANCH (1 << 27)
96
97 /* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */
98
99 /* Denotes an opcode that takes a vector input with a fixed-number of
100 * channels, but outputs to only a single output channel, like dot products.
101 * For these, to determine the effective mask, this quirk can be set. We have
102 * an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no
103 * sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't
104 * make sense (since then why are we quirked?), so that corresponds to "no
105 * count set" */
106
107 #define OP_CHANNEL_COUNT(c) ((c - 1) << 0)
108 #define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)
109
110 /* For instructions that take a single argument, normally the first argument
111 * slot is used for the argument and the second slot is a dummy #0 constant.
112 * However, there are exceptions: instructions like fmov store their argument
113 * in the _second_ slot and store a dummy r24 in the first slot, designated by
114 * QUIRK_FLIPPED_R24 */
115
116 #define QUIRK_FLIPPED_R24 (1 << 2)
117
118 /* Is the op commutative? */
119 #define OP_COMMUTES (1 << 3)
120
121 /* Does the op convert types between int- and float- space (i2f/f2u/etc) */
122 #define OP_TYPE_CONVERT (1 << 4)
123
124 /* Vector-independant shorthands for the above; these numbers are arbitrary and
125 * not from the ISA. Convert to the above with unit_enum_to_midgard */
126
127 #define UNIT_MUL 0
128 #define UNIT_ADD 1
129 #define UNIT_LUT 2
130
131 /* 4-bit type tags */
132
133 #define TAG_TEXTURE_4_VTX 0x2
134 #define TAG_TEXTURE_4 0x3
135 #define TAG_LOAD_STORE_4 0x5
136 #define TAG_ALU_4 0x8
137 #define TAG_ALU_8 0x9
138 #define TAG_ALU_12 0xA
139 #define TAG_ALU_16 0xB
140
141 static inline int
142 quadword_size(int tag)
143 {
144 switch (tag) {
145 case TAG_ALU_4:
146 case TAG_LOAD_STORE_4:
147 case TAG_TEXTURE_4:
148 case TAG_TEXTURE_4_VTX:
149 return 1;
150 case TAG_ALU_8:
151 return 2;
152 case TAG_ALU_12:
153 return 3;
154 case TAG_ALU_16:
155 return 4;
156 default:
157 unreachable("Unknown tag");
158 }
159 }
160
161 #define IS_ALU(tag) (tag == TAG_ALU_4 || tag == TAG_ALU_8 || \
162 tag == TAG_ALU_12 || tag == TAG_ALU_16)
163
164 /* Special register aliases */
165
166 #define MAX_WORK_REGISTERS 16
167
168 /* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */
169 #define REGISTER_UNIFORMS 24
170
171 #define REGISTER_UNUSED 24
172 #define REGISTER_CONSTANT 26
173 #define REGISTER_VARYING_BASE 26
174 #define REGISTER_OFFSET 27
175 #define REGISTER_TEXTURE_BASE 28
176 #define REGISTER_SELECT 31
177
178 /* SSA helper aliases to mimic the registers. UNUSED_0 encoded as an inline
179 * constant. UNUSED_1 encoded as REGISTER_UNUSED */
180
181 #define SSA_UNUSED_0 0
182 #define SSA_UNUSED_1 -2
183
184 #define SSA_FIXED_SHIFT 24
185 #define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1)
186 #define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1)
187 #define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)
188
189 /* Swizzle support */
190
191 #define SWIZZLE(A, B, C, D) ((D << 6) | (C << 4) | (B << 2) | (A << 0))
192 #define SWIZZLE_FROM_ARRAY(r) SWIZZLE(r[0], r[1], r[2], r[3])
193 #define COMPONENT_X 0x0
194 #define COMPONENT_Y 0x1
195 #define COMPONENT_Z 0x2
196 #define COMPONENT_W 0x3
197
198 #define SWIZZLE_XXXX SWIZZLE(COMPONENT_X, COMPONENT_X, COMPONENT_X, COMPONENT_X)
199 #define SWIZZLE_XYXX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_X)
200 #define SWIZZLE_XYZX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_X)
201 #define SWIZZLE_XYZW SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W)
202 #define SWIZZLE_XYXZ SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_Z)
203 #define SWIZZLE_XYZZ SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_Z)
204 #define SWIZZLE_XXXY SWIZZLE(COMPONENT_X, COMPONENT_X, COMPONENT_X, COMPONENT_Y)
205 #define SWIZZLE_ZZZW SWIZZLE(COMPONENT_Z, COMPONENT_Z, COMPONENT_Z, COMPONENT_W)
206 #define SWIZZLE_ZWWW SWIZZLE(COMPONENT_Z, COMPONENT_W, COMPONENT_W, COMPONENT_W)
207 #define SWIZZLE_WWWW SWIZZLE(COMPONENT_W, COMPONENT_W, COMPONENT_W, COMPONENT_W)
208
209 static inline unsigned
210 swizzle_of(unsigned comp)
211 {
212 switch (comp) {
213 case 1:
214 return SWIZZLE_XXXX;
215 case 2:
216 return SWIZZLE_XYXX;
217 case 3:
218 return SWIZZLE_XYZX;
219 case 4:
220 return SWIZZLE_XYZW;
221 default:
222 unreachable("Invalid component count");
223 }
224 }
225
226 static inline unsigned
227 mask_of(unsigned nr_comp)
228 {
229 return (1 << nr_comp) - 1;
230 }
231
232
233 /* See ISA notes */
234
235 #define LDST_NOP (3)
236
237 /* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is
238 * implemented on some subset of these units (or occassionally all of them).
239 * This table encodes a bit mask of valid units for each opcode, so the
240 * scheduler can figure where to plonk the instruction. */
241
242 /* Shorthands for each unit */
243 #define UNIT_VMUL ALU_ENAB_VEC_MUL
244 #define UNIT_SADD ALU_ENAB_SCAL_ADD
245 #define UNIT_VADD ALU_ENAB_VEC_ADD
246 #define UNIT_SMUL ALU_ENAB_SCAL_MUL
247 #define UNIT_VLUT ALU_ENAB_VEC_LUT
248
249 /* Shorthands for usual combinations of units */
250
251 #define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)
252 #define UNITS_ADD (UNIT_VADD | UNIT_SADD)
253 #define UNITS_MOST (UNITS_MUL | UNITS_ADD)
254 #define UNITS_ALL (UNITS_MOST | UNIT_VLUT)
255 #define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)
256 #define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)
257 #define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)
258
259 struct mir_op_props {
260 const char *name;
261 unsigned props;
262 };
263
264 /* This file is common, so don't define the tables themselves. #include
265 * midgard_op.h if you need that, or edit midgard_ops.c directly */
266
267 /* Duplicate bits to convert a 4-bit writemask to duplicated 8-bit format,
268 * which is used for 32-bit vector units */
269
270 static inline unsigned
271 expand_writemask_32(unsigned mask)
272 {
273 unsigned o = 0;
274
275 for (int i = 0; i < 4; ++i)
276 if (mask & (1 << i))
277 o |= (3 << (2 * i));
278
279 return o;
280 }
281
282 /* Coerce structs to integer */
283
284 static inline unsigned
285 vector_alu_srco_unsigned(midgard_vector_alu_src src)
286 {
287 unsigned u;
288 memcpy(&u, &src, sizeof(src));
289 return u;
290 }
291
292 static inline midgard_vector_alu_src
293 vector_alu_from_unsigned(unsigned u)
294 {
295 midgard_vector_alu_src s;
296 memcpy(&s, &u, sizeof(s));
297 return s;
298 }
299
300 /* Composes two swizzles */
301 static inline unsigned
302 pan_compose_swizzle(unsigned left, unsigned right)
303 {
304 unsigned out = 0;
305
306 for (unsigned c = 0; c < 4; ++c) {
307 unsigned s = (left >> (2*c)) & 0x3;
308 unsigned q = (right >> (2*s)) & 0x3;
309
310 out |= (q << (2*c));
311 }
312
313 return out;
314 }
315
316 /* Applies a swizzle to an ALU source */
317
318 static inline unsigned
319 vector_alu_apply_swizzle(unsigned src, unsigned swizzle)
320 {
321 midgard_vector_alu_src s =
322 vector_alu_from_unsigned(src);
323
324 s.swizzle = pan_compose_swizzle(s.swizzle, swizzle);
325
326 return vector_alu_srco_unsigned(s);
327 }
328
329 /* Checks for an xyzw.. swizzle, given a mask */
330
331 static inline bool
332 mir_is_simple_swizzle(unsigned swizzle, unsigned mask)
333 {
334 for (unsigned i = 0; i < 16; ++i) {
335 if (!(mask & (1 << i))) continue;
336
337 if (((swizzle >> (2 * i)) & 0x3) != i)
338 return false;
339 }
340
341 return true;
342 }
343
344 #endif