pan/bit: Add CSEL to interpreter
[mesa.git] / src / panfrost / bifrost / bifrost.h
1 /*
2 * Copyright (C) 2019 Connor Abbott <cwabbott0@gmail.com>
3 * Copyright (C) 2019 Lyude Paul <thatslyude@gmail.com>
4 * Copyright (C) 2019 Ryan Houdek <Sonicadvance1@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #ifndef __bifrost_h__
27 #define __bifrost_h__
28
29 #include <stdint.h>
30 #include <stdbool.h>
31
32 enum bifrost_clause_type {
33 BIFROST_CLAUSE_NONE = 0,
34 BIFROST_CLAUSE_LOAD_VARY = 1,
35 BIFROST_CLAUSE_UBO = 2,
36 BIFROST_CLAUSE_TEX = 3,
37 BIFROST_CLAUSE_SSBO_LOAD = 5,
38 BIFROST_CLAUSE_SSBO_STORE = 6,
39 BIFROST_CLAUSE_BLEND = 9,
40 BIFROST_CLAUSE_ATEST = 13,
41 BIFROST_CLAUSE_64BIT = 15
42 };
43
44 struct bifrost_header {
45 unsigned unk0 : 7;
46 // If true, convert any infinite result of any floating-point operation to
47 // the biggest representable number.
48 unsigned suppress_inf: 1;
49 // Convert any NaN results to 0.
50 unsigned suppress_nan : 1;
51 unsigned unk1 : 2;
52 // true if the execution mask of the next clause is the same as the mask of
53 // the current clause.
54 unsigned back_to_back : 1;
55 unsigned no_end_of_shader: 1;
56 unsigned unk2 : 2;
57 // Set to true for fragment shaders, to implement this bit of spec text
58 // from section 7.1.5 of the GLSL ES spec:
59 //
60 // "Stores to image and buffer variables performed by helper invocations
61 // have no effect on the underlying image or buffer memory."
62 //
63 // Helper invocations are threads (invocations) corresponding to pixels in
64 // a quad that aren't actually part of the triangle, but are included to
65 // make derivatives work correctly. They're usually turned on, but they
66 // need to be masked off for GLSL-level stores. This bit seems to be the
67 // only bit that's actually different between fragment shaders and other
68 // shaders, so this is probably what it's doing.
69 unsigned elide_writes : 1;
70 // If backToBack is off:
71 // - true for conditional branches and fallthrough
72 // - false for unconditional branches
73 // The blob seems to always set it to true if back-to-back is on.
74 unsigned branch_cond : 1;
75 // This bit is set when the next clause writes to the data register of some
76 // previous clause.
77 unsigned datareg_writebarrier: 1;
78 unsigned datareg : 6;
79 unsigned scoreboard_deps: 8;
80 unsigned scoreboard_index: 3;
81 enum bifrost_clause_type clause_type: 4;
82 unsigned unk3 : 1; // part of clauseType?
83 enum bifrost_clause_type next_clause_type: 4;
84 unsigned unk4 : 1; // part of nextClauseType?
85 } __attribute__((packed));
86
87 enum bifrost_packed_src {
88 BIFROST_SRC_PORT0 = 0,
89 BIFROST_SRC_PORT1 = 1,
90 BIFROST_SRC_PORT3 = 2,
91 BIFROST_SRC_STAGE = 3,
92 BIFROST_SRC_CONST_LO = 4,
93 BIFROST_SRC_CONST_HI = 5,
94 BIFROST_SRC_PASS_FMA = 6,
95 BIFROST_SRC_PASS_ADD = 7,
96 };
97
98 #define BIFROST_FMA_EXT (0xe0000)
99 #define BIFROST_FMA_OP_MOV BIFROST_FMA_EXT | (0x32d)
100
101 struct bifrost_fma_inst {
102 unsigned src0 : 3;
103 unsigned op : 20;
104 } __attribute__((packed));
105
106 struct bifrost_fma_2src {
107 unsigned src0 : 3;
108 unsigned src1 : 3;
109 unsigned op : 17;
110 } __attribute__((packed));
111
112 #define BIFROST_ADD_OP_BLEND (0x1952c)
113
114 struct bifrost_add_inst {
115 unsigned src0 : 3;
116 unsigned op : 17;
117 } __attribute__((packed));
118
119 #define BIFROST_ADD_OP_LD_UBO_1 (0x0c1a0 >> 3)
120 #define BIFROST_ADD_OP_LD_UBO_2 (0x0c1e0 >> 3)
121 #define BIFROST_ADD_OP_LD_UBO_3 (0x0caa0 >> 3)
122 #define BIFROST_ADD_OP_LD_UBO_4 (0x0c220 >> 3)
123
124 struct bifrost_add_2src {
125 unsigned src0 : 3;
126 unsigned src1 : 3;
127 unsigned op : 14;
128 } __attribute__((packed));
129
130 #define BIFROST_ADD_OP_ST_VAR (0x19300 >> 8)
131
132 struct bifrost_st_vary {
133 unsigned src0 : 3;
134 unsigned src1 : 3;
135 unsigned src2 : 3;
136 unsigned channels : 2;
137 unsigned op : 9;
138 } __attribute__((packed));
139
140 #define BIFROST_ADD_OP_ATEST (0xc8f)
141
142 struct bifrost_add_atest {
143 /* gl_SampleMask (R60) */
144 unsigned src0 : 3;
145
146 /* Alpha value */
147 unsigned src1 : 3;
148
149 /* If half, X/Y select. If !half, always set */
150 unsigned component : 1;
151 unsigned half : 1;
152
153 unsigned op : 12;
154 } __attribute__((packed));
155
156 enum bifrost_outmod {
157 BIFROST_NONE = 0x0,
158 BIFROST_POS = 0x1,
159 BIFROST_SAT_SIGNED = 0x2,
160 BIFROST_SAT = 0x3,
161 };
162
163 enum bifrost_roundmode {
164 BIFROST_RTE = 0x0, /* round to even */
165 BIFROST_RTP = 0x1, /* round to positive */
166 BIFROST_RTN = 0x2, /* round to negative */
167 BIFROST_RTZ = 0x3 /* round to zero */
168 };
169
170 /* NONE: Same as fmax() and fmin() -- return the other
171 * number if any number is NaN. Also always return +0 if
172 * one argument is +0 and the other is -0.
173 *
174 * NAN_WINS: Instead of never returning a NaN, always return
175 * one. The "greater"/"lesser" NaN is always returned, first
176 * by checking the sign and then the mantissa bits.
177 *
178 * SRC1_WINS: For max, implement src0 > src1 ? src0 : src1.
179 * For min, implement src0 < src1 ? src0 : src1. This
180 * includes handling NaN's and signedness of 0 differently
181 * from above, since +0 and -0 compare equal and comparisons
182 * always return false for NaN's. As a result, this mode is
183 * *not* commutative.
184 *
185 * SRC0_WINS: For max, implement src0 < src1 ? src1 : src0
186 * For min, implement src0 > src1 ? src1 : src0
187 */
188
189
190 enum bifrost_minmax_mode {
191 BIFROST_MINMAX_NONE = 0x0,
192 BIFROST_NAN_WINS = 0x1,
193 BIFROST_SRC1_WINS = 0x2,
194 BIFROST_SRC0_WINS = 0x3,
195 };
196
197 #define BIFROST_FMA_OP_FADD32 (0x58 >> 2)
198
199 struct bifrost_fma_add {
200 unsigned src0 : 3;
201 unsigned src1 : 3;
202 unsigned src1_abs : 1;
203 unsigned src0_neg : 1;
204 unsigned src1_neg : 1;
205 unsigned unk : 3;
206 unsigned src0_abs : 1;
207 enum bifrost_roundmode roundmode : 2;
208 enum bifrost_outmod outmod : 2;
209 unsigned op : 6;
210 } __attribute__((packed));
211
212 #define BIFROST_FMA_OP_FMAX16 (0xC0 >> 2)
213 #define BIFROST_FMA_OP_FMIN16 (0xCC >> 2)
214 #define BIFROST_FMA_OP_FADD16 (0xD8 >> 2)
215
216 struct bifrost_fma_add_minmax16 {
217 unsigned src0 : 3;
218 unsigned src1 : 3;
219 /* abs2 inferred as (src1 < src0) */
220 unsigned abs1 : 1;
221 unsigned src0_neg : 1;
222 unsigned src1_neg : 1;
223 unsigned src0_swizzle : 2;
224 unsigned src1_swizzle : 2;
225 unsigned mode : 2;
226 enum bifrost_outmod outmod : 2;
227 /* roundmode for add, min/max mode for min/max */
228 unsigned op : 6;
229 } __attribute__((packed));
230
231 #define BIFROST_FMA_OP_FMA (0x00)
232
233 struct bifrost_fma_fma {
234 unsigned src0 : 3;
235 unsigned src1 : 3;
236 unsigned src2 : 3;
237 unsigned src_expand : 3;
238 unsigned src0_abs : 1;
239 enum bifrost_roundmode roundmode : 2;
240 enum bifrost_outmod outmod : 2;
241 unsigned src0_neg : 1; /* 14 */
242 unsigned src2_neg : 1;
243 unsigned src1_abs : 1;
244 unsigned src2_abs : 1; /* 17 */
245 unsigned op : 2;
246 } __attribute__((packed));
247
248 #define BIFROST_FMA_OP_FMA16 (0x2)
249
250 struct bifrost_fma_fma16 {
251 unsigned src0 : 3;
252 unsigned src1 : 3;
253 unsigned src2 : 3;
254 unsigned swizzle_0 : 2;
255 unsigned swizzle_1 : 2;
256 enum bifrost_roundmode roundmode : 2;
257 enum bifrost_outmod outmod : 2;
258 unsigned src0_neg : 1;
259 unsigned src2_neg : 1;
260 unsigned swizzle_2 : 2;
261 unsigned op : 2;
262 } __attribute__((packed));
263
264 enum bifrost_csel_cond {
265 BIFROST_FEQ_F = 0x0,
266 BIFROST_FGT_F = 0x1,
267 BIFROST_FGE_F = 0x2,
268 BIFROST_IEQ_F = 0x3,
269 BIFROST_IGT_I = 0x4,
270 BIFROST_IGE_I = 0x5,
271 BIFROST_UGT_I = 0x6,
272 BIFROST_UGE_I = 0x7
273 };
274
275 #define BIFROST_FMA_OP_CSEL4 (0x5c)
276 #define BIFROST_FMA_OP_CSEL4_V16 (0xdc)
277
278 struct bifrost_csel4 {
279 unsigned src0 : 3;
280 unsigned src1 : 3;
281 unsigned src2 : 3;
282 unsigned src3 : 3;
283 enum bifrost_csel_cond cond : 3;
284 unsigned op : 8;
285 } __attribute__((packed));
286
287 struct bifrost_shift_fma {
288 unsigned src0 : 3;
289 unsigned src1 : 3;
290 unsigned src2 : 3;
291 unsigned half : 3; /* 000 for i32, 100 for i8, 111 for v2i16 */
292 unsigned unk : 1; /* always set? */
293 unsigned invert_1 : 1; /* Inverts sources to combining op */
294 /* For XOR, switches RSHIFT to LSHIFT since only one invert needed */
295 unsigned invert_2 : 1;
296 unsigned op : 8;
297 } __attribute__((packed));
298
299 struct bifrost_shift_add {
300 unsigned src0 : 3;
301 unsigned src1 : 3;
302 unsigned src2 : 3;
303 unsigned zero : 2;
304
305 unsigned invert_1 : 1;
306 unsigned invert_2 : 1;
307
308 unsigned op : 7;
309 } __attribute__((packed));
310
311 #define BIFROST_FMA_INT16_TO_32 (0xe0198 >> 3)
312
313 struct bifrost_fma_int16_to_32 {
314 unsigned src0 : 3;
315 unsigned is_unsigned : 1;
316 unsigned swizzle : 1;
317 unsigned to_float : 1;
318 unsigned op : 17;
319 } __attribute__((packed));
320
321 /* We could combine but it's easier to just use FMA_ONE_SRC */
322 #define BIFROST_FMA_FLOAT16_TO_32(y) (0xe01a2 | ((y) ? 1 : 0))
323
324 /* Two sources for vectorization */
325 #define BIFROST_FMA_FLOAT32_TO_16 (0xdd000 >> 3)
326
327 /* Again we could combine but easier to just ONE_SRC with an
328 * argumnt for unsignedness */
329 #define BIFROST_FMA_FLOAT32_TO_INT(u) (0xe0136 | ((u) ? 1 : 0))
330 #define BIFROST_FMA_INT_TO_FLOAT32(u) (0xe0178 | ((u) ? 1 : 0))
331
332 /* Used for f2i16 and i2f16 */
333 #define BIFROST_FMA_F2I16 (0xe00)
334
335 struct bifrost_fma_f2i_i2f16 {
336 unsigned src0 : 3;
337 unsigned is_unsigned : 1;
338 unsigned direction : 2; /* 00 for i2f, 11 for f2i */
339 unsigned swizzle : 2;
340 unsigned unk : 2; /* always 10 */
341 unsigned direction_2 : 1; /* 0 for f2i, 1 for i2f */
342 unsigned op : 12;
343 } __attribute__((packed));
344
345 enum bifrost_ldst_type {
346 BIFROST_LDST_F16 = 0,
347 BIFROST_LDST_F32 = 1,
348 BIFROST_LDST_I32 = 2,
349 BIFROST_LDST_U32 = 3
350 };
351
352 #define BIFROST_ADD_OP_LD_VAR_ADDR (0x18000 >> 10)
353
354 struct bifrost_ld_var_addr {
355 unsigned src0 : 3;
356 unsigned src1 : 3;
357 unsigned location : 5;
358 enum bifrost_ldst_type type : 2;
359 unsigned op : 7;
360 } __attribute__((packed));
361
362 #define BIFROST_ADD_OP_LD_ATTR (0x08000 >> 12)
363
364 struct bifrost_ld_attr {
365 unsigned src0 : 3;
366 unsigned src1 : 3;
367 unsigned location : 5;
368 unsigned channels : 2; /* MALI_POSITIVE */
369 enum bifrost_ldst_type type : 2;
370 unsigned op : 5;
371 } __attribute__((packed));
372
373 enum bifrost_interp_mode {
374 BIFROST_INTERP_PER_FRAG = 0x0,
375 BIFROST_INTERP_CENTROID = 0x1,
376 BIFROST_INTERP_DEFAULT = 0x2,
377 BIFROST_INTERP_EXPLICIT = 0x3
378 };
379
380 #define BIFROST_ADD_OP_LD_VAR_16 (0x1a << 1)
381 #define BIFROST_ADD_OP_LD_VAR_32 (0x0a << 1)
382
383 struct bifrost_ld_var {
384 unsigned src0 : 3;
385
386 /* If top two bits set, indirect with src in bottom three */
387 unsigned addr : 5;
388
389 unsigned channels : 2; /* MALI_POSITIVE */
390 enum bifrost_interp_mode interp_mode : 2;
391 unsigned reuse : 1;
392 unsigned flat : 1;
393 unsigned op : 6;
394 } __attribute__((packed));
395
396 struct bifrost_tex_ctrl {
397 unsigned sampler_index : 4; // also used to signal indirects
398 unsigned tex_index : 7;
399 bool no_merge_index : 1; // whether to merge (direct) sampler & texture indices
400 bool filter : 1; // use the usual filtering pipeline (0 for texelFetch & textureGather)
401 unsigned unk0 : 2;
402 bool texel_offset : 1; // *Offset()
403 bool is_shadow : 1;
404 bool is_array : 1;
405 unsigned tex_type : 2; // 2D, 3D, Cube, Buffer
406 bool compute_lod : 1; // 0 for *Lod()
407 bool not_supply_lod : 1; // 0 for *Lod() or when a bias is applied
408 bool calc_gradients : 1; // 0 for *Grad()
409 unsigned unk1 : 1;
410 unsigned result_type : 4; // integer, unsigned, float TODO: why is this 4 bits?
411 unsigned unk2 : 4;
412 } __attribute__((packed));
413
414 struct bifrost_dual_tex_ctrl {
415 unsigned sampler_index0 : 2;
416 unsigned unk0 : 2;
417 unsigned tex_index0 : 2;
418 unsigned sampler_index1 : 2;
419 unsigned tex_index1 : 2;
420 unsigned unk1 : 22;
421 } __attribute__((packed));
422
423 enum branch_bit_size {
424 BR_SIZE_32 = 0,
425 BR_SIZE_16XX = 1,
426 BR_SIZE_16YY = 2,
427 // For the above combinations of bitsize and location, an extra bit is
428 // encoded via comparing the sources. The only possible source of ambiguity
429 // would be if the sources were the same, but then the branch condition
430 // would be always true or always false anyways, so we can ignore it. But
431 // this no longer works when comparing the y component to the x component,
432 // since it's valid to compare the y component of a source against its own
433 // x component. Instead, the extra bit is encoded via an extra bitsize.
434 BR_SIZE_16YX0 = 3,
435 BR_SIZE_16YX1 = 4,
436 BR_SIZE_32_AND_16X = 5,
437 BR_SIZE_32_AND_16Y = 6,
438 // Used for comparisons with zero and always-true, see below. I think this
439 // only works for integer comparisons.
440 BR_SIZE_ZERO = 7,
441 };
442
443 enum bifrost_reg_write_unit {
444 REG_WRITE_NONE = 0, // don't write
445 REG_WRITE_TWO, // write using reg2
446 REG_WRITE_THREE, // write using reg3
447 };
448
449 struct bifrost_regs {
450 unsigned uniform_const : 8;
451 unsigned reg2 : 6;
452 unsigned reg3 : 6;
453 unsigned reg0 : 5;
454 unsigned reg1 : 6;
455 unsigned ctrl : 4;
456 } __attribute__((packed));
457
458 enum bifrost_branch_cond {
459 BR_COND_LT = 0,
460 BR_COND_LE = 1,
461 BR_COND_GE = 2,
462 BR_COND_GT = 3,
463 // Equal vs. not-equal determined by src0/src1 comparison
464 BR_COND_EQ = 4,
465 // floating-point comparisons
466 // Becomes UNE when you flip the arguments
467 BR_COND_OEQ = 5,
468 // TODO what happens when you flip the arguments?
469 BR_COND_OGT = 6,
470 BR_COND_OLT = 7,
471 };
472
473 enum bifrost_branch_code {
474 BR_ALWAYS = 63,
475 };
476
477 struct bifrost_branch {
478 unsigned src0 : 3;
479
480 /* For BR_SIZE_ZERO, upper two bits become ctrl */
481 unsigned src1 : 3;
482
483 /* Offset source -- always uniform/const but
484 * theoretically could support indirect jumps? */
485 unsigned src2 : 3;
486
487 enum bifrost_branch_cond cond : 3;
488 enum branch_bit_size size : 3;
489
490 unsigned op : 5;
491 };
492
493 /* Clause packing */
494
495 #define BIFROST_FMA_NOP (0x701960 | BIFROST_SRC_STAGE)
496 #define BIFROST_ADD_NOP (0x3D960 | BIFROST_SRC_STAGE)
497
498 struct bifrost_fmt1 {
499 unsigned ins_0 : 3;
500 unsigned tag : 5;
501 uint64_t ins_1 : 64;
502 unsigned ins_2 : 11;
503 uint64_t header : 45;
504 } __attribute__((packed));
505
506 #define BIFROST_FMT1_INSTRUCTIONS 0b00101
507 #define BIFROST_FMT1_FINAL 0b01001
508 #define BIFROST_FMT1_CONSTANTS 0b00001
509
510 #define BIFROST_FMTC_CONSTANTS 0b0011
511 #define BIFROST_FMTC_FINAL 0b0111
512
513 struct bifrost_fmt_constant {
514 unsigned pos : 4;
515 unsigned tag : 4;
516 uint64_t imm_1 : 60;
517 uint64_t imm_2 : 60;
518 } __attribute__((packed));
519
520 enum bifrost_reg_control {
521 BIFROST_WRITE_FMA_P2 = 1,
522 BIFROST_WRITE_FMA_P2_READ_P3 = 2,
523 BIFROST_FIRST_WRITE_FMA_P2_READ_P3 = 3,
524 BIFROST_READ_P3 = 4,
525 BIFROST_WRITE_ADD_P2 = 5,
526 BIFROST_WRITE_ADD_P2_READ_P3 = 6,
527 BIFROST_WRITE_ADD_P2_FMA_P3 = 7,
528
529 BIFROST_FIRST_NONE = 8,
530 BIFROST_FIRST_WRITE_FMA_P2 = 9,
531 /* INSTR_INVALID_ENC */
532 BIFROST_REG_NONE = 11,
533 BIFROST_FIRST_READ_P3 = 12,
534 BIFROST_FIRST_WRITE_ADD_P2 = 13,
535 BIFROST_FIRST_WRITE_ADD_P2_READ_P3 = 14,
536 BIFROST_FIRST_WRITE_ADD_P2_FMA_P3 = 15
537 };
538
539 #endif