pan/bi: Structify ADD unit add/min/max
[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 #define BIFROST_ADD_OP_FRCP_FAST_F32 (0x0cc00)
114 #define BIFROST_ADD_OP_FRCP_FAST_F16_X (0x0ce10)
115 #define BIFROST_ADD_OP_FRCP_FAST_F16_Y (0x0ce30)
116 #define BIFROST_ADD_OP_FRSQ_FAST_F32 (0x0cc20)
117 #define BIFROST_ADD_OP_FRSQ_FAST_F16_X (0x0ce50)
118 #define BIFROST_ADD_OP_FRSQ_FAST_F16_Y (0x0ce70)
119
120 struct bifrost_add_inst {
121 unsigned src0 : 3;
122 unsigned op : 17;
123 } __attribute__((packed));
124
125 #define BIFROST_ADD_OP_LD_UBO_1 (0x0c1a0 >> 3)
126 #define BIFROST_ADD_OP_LD_UBO_2 (0x0c1e0 >> 3)
127 #define BIFROST_ADD_OP_LD_UBO_3 (0x0caa0 >> 3)
128 #define BIFROST_ADD_OP_LD_UBO_4 (0x0c220 >> 3)
129
130 struct bifrost_add_2src {
131 unsigned src0 : 3;
132 unsigned src1 : 3;
133 unsigned op : 14;
134 } __attribute__((packed));
135
136 #define BIFROST_ADD_OP_FMAX32 (0x00)
137 #define BIFROST_ADD_OP_FMIN32 (0x01)
138 #define BIFROST_ADD_OP_FADD32 (0x02)
139
140 struct bifrost_add_faddmin {
141 unsigned src0 : 3;
142 unsigned src1 : 3;
143 unsigned src1_abs : 1;
144 unsigned src0_neg : 1;
145 unsigned src1_neg : 1;
146 unsigned select : 2;
147 unsigned outmod : 2;
148 unsigned mode : 2;
149 unsigned src0_abs : 1;
150 unsigned op : 4;
151 } __attribute__((packed));
152
153 #define BIFROST_ADD_OP_ST_VAR (0x19300 >> 8)
154
155 struct bifrost_st_vary {
156 unsigned src0 : 3;
157 unsigned src1 : 3;
158 unsigned src2 : 3;
159 unsigned channels : 2;
160 unsigned op : 9;
161 } __attribute__((packed));
162
163 #define BIFROST_ADD_OP_ATEST (0xc8f)
164
165 struct bifrost_add_atest {
166 /* gl_SampleMask (R60) */
167 unsigned src0 : 3;
168
169 /* Alpha value */
170 unsigned src1 : 3;
171
172 /* If half, X/Y select. If !half, always set */
173 unsigned component : 1;
174 unsigned half : 1;
175
176 unsigned op : 12;
177 } __attribute__((packed));
178
179 enum bifrost_outmod {
180 BIFROST_NONE = 0x0,
181 BIFROST_POS = 0x1,
182 BIFROST_SAT_SIGNED = 0x2,
183 BIFROST_SAT = 0x3,
184 };
185
186 enum bifrost_roundmode {
187 BIFROST_RTE = 0x0, /* round to even */
188 BIFROST_RTP = 0x1, /* round to positive */
189 BIFROST_RTN = 0x2, /* round to negative */
190 BIFROST_RTZ = 0x3 /* round to zero */
191 };
192
193 /* NONE: Same as fmax() and fmin() -- return the other
194 * number if any number is NaN. Also always return +0 if
195 * one argument is +0 and the other is -0.
196 *
197 * NAN_WINS: Instead of never returning a NaN, always return
198 * one. The "greater"/"lesser" NaN is always returned, first
199 * by checking the sign and then the mantissa bits.
200 *
201 * SRC1_WINS: For max, implement src0 > src1 ? src0 : src1.
202 * For min, implement src0 < src1 ? src0 : src1. This
203 * includes handling NaN's and signedness of 0 differently
204 * from above, since +0 and -0 compare equal and comparisons
205 * always return false for NaN's. As a result, this mode is
206 * *not* commutative.
207 *
208 * SRC0_WINS: For max, implement src0 < src1 ? src1 : src0
209 * For min, implement src0 > src1 ? src1 : src0
210 */
211
212
213 enum bifrost_minmax_mode {
214 BIFROST_MINMAX_NONE = 0x0,
215 BIFROST_NAN_WINS = 0x1,
216 BIFROST_SRC1_WINS = 0x2,
217 BIFROST_SRC0_WINS = 0x3,
218 };
219
220 #define BIFROST_FMA_OP_FADD32 (0x58 >> 2)
221 #define BIFROST_FMA_OP_FMAX32 (0x40 >> 2)
222 #define BIFROST_FMA_OP_FMIN32 (0x44 >> 2)
223
224 struct bifrost_fma_add {
225 unsigned src0 : 3;
226 unsigned src1 : 3;
227 unsigned src1_abs : 1;
228 unsigned src0_neg : 1;
229 unsigned src1_neg : 1;
230 unsigned unk : 3;
231 unsigned src0_abs : 1;
232 enum bifrost_roundmode roundmode : 2;
233 enum bifrost_outmod outmod : 2;
234 unsigned op : 6;
235 } __attribute__((packed));
236
237 #define BIFROST_FMA_OP_FMAX16 (0xC0 >> 2)
238 #define BIFROST_FMA_OP_FMIN16 (0xCC >> 2)
239 #define BIFROST_FMA_OP_FADD16 (0xD8 >> 2)
240
241 struct bifrost_fma_add_minmax16 {
242 unsigned src0 : 3;
243 unsigned src1 : 3;
244 /* abs2 inferred as (src1 < src0) */
245 unsigned abs1 : 1;
246 unsigned src0_neg : 1;
247 unsigned src1_neg : 1;
248 unsigned src0_swizzle : 2;
249 unsigned src1_swizzle : 2;
250 unsigned mode : 2;
251 enum bifrost_outmod outmod : 2;
252 /* roundmode for add, min/max mode for min/max */
253 unsigned op : 6;
254 } __attribute__((packed));
255
256 #define BIFROST_FMA_OP_FMA (0x00)
257
258 struct bifrost_fma_fma {
259 unsigned src0 : 3;
260 unsigned src1 : 3;
261 unsigned src2 : 3;
262 unsigned src_expand : 3;
263 unsigned src0_abs : 1;
264 enum bifrost_roundmode roundmode : 2;
265 enum bifrost_outmod outmod : 2;
266 unsigned src0_neg : 1; /* 14 */
267 unsigned src2_neg : 1;
268 unsigned src1_abs : 1;
269 unsigned src2_abs : 1; /* 17 */
270 unsigned op : 2;
271 } __attribute__((packed));
272
273 #define BIFROST_FMA_OP_FMA16 (0x2)
274
275 struct bifrost_fma_fma16 {
276 unsigned src0 : 3;
277 unsigned src1 : 3;
278 unsigned src2 : 3;
279 unsigned swizzle_0 : 2;
280 unsigned swizzle_1 : 2;
281 enum bifrost_roundmode roundmode : 2;
282 enum bifrost_outmod outmod : 2;
283 unsigned src0_neg : 1;
284 unsigned src2_neg : 1;
285 unsigned swizzle_2 : 2;
286 unsigned op : 2;
287 } __attribute__((packed));
288
289 enum bifrost_csel_cond {
290 BIFROST_FEQ_F = 0x0,
291 BIFROST_FGT_F = 0x1,
292 BIFROST_FGE_F = 0x2,
293 BIFROST_IEQ_F = 0x3,
294 BIFROST_IGT_I = 0x4,
295 BIFROST_IGE_I = 0x5,
296 BIFROST_UGT_I = 0x6,
297 BIFROST_UGE_I = 0x7
298 };
299
300 #define BIFROST_FMA_OP_CSEL4 (0x5c)
301 #define BIFROST_FMA_OP_CSEL4_V16 (0xdc)
302
303 struct bifrost_csel4 {
304 unsigned src0 : 3;
305 unsigned src1 : 3;
306 unsigned src2 : 3;
307 unsigned src3 : 3;
308 enum bifrost_csel_cond cond : 3;
309 unsigned op : 8;
310 } __attribute__((packed));
311
312 struct bifrost_shift_fma {
313 unsigned src0 : 3;
314 unsigned src1 : 3;
315 unsigned src2 : 3;
316 unsigned half : 3; /* 000 for i32, 100 for i8, 111 for v2i16 */
317 unsigned unk : 1; /* always set? */
318 unsigned invert_1 : 1; /* Inverts sources to combining op */
319 /* For XOR, switches RSHIFT to LSHIFT since only one invert needed */
320 unsigned invert_2 : 1;
321 unsigned op : 8;
322 } __attribute__((packed));
323
324 struct bifrost_shift_add {
325 unsigned src0 : 3;
326 unsigned src1 : 3;
327 unsigned src2 : 3;
328 unsigned zero : 2;
329
330 unsigned invert_1 : 1;
331 unsigned invert_2 : 1;
332
333 unsigned op : 7;
334 } __attribute__((packed));
335
336 #define BIFROST_FMA_INT16_TO_32 (0xe0198 >> 3)
337
338 struct bifrost_fma_int16_to_32 {
339 unsigned src0 : 3;
340 unsigned is_unsigned : 1;
341 unsigned swizzle : 1;
342 unsigned to_float : 1;
343 unsigned op : 17;
344 } __attribute__((packed));
345
346 /* We could combine but it's easier to just use FMA_ONE_SRC */
347 #define BIFROST_FMA_FLOAT16_TO_32(y) (0xe01a2 | ((y) ? 1 : 0))
348
349 /* Two sources for vectorization */
350 #define BIFROST_FMA_FLOAT32_TO_16 (0xdd000 >> 3)
351
352 /* Again we could combine but easier to just ONE_SRC with an
353 * argumnt for unsignedness */
354 #define BIFROST_FMA_FLOAT32_TO_INT(u) (0xe0136 | ((u) ? 1 : 0))
355 #define BIFROST_FMA_INT_TO_FLOAT32(u) (0xe0178 | ((u) ? 1 : 0))
356
357 /* Used for f2i16 and i2f16 */
358 #define BIFROST_FMA_F2I16 (0xe00)
359
360 struct bifrost_fma_f2i_i2f16 {
361 unsigned src0 : 3;
362 unsigned is_unsigned : 1;
363 unsigned direction : 2; /* 00 for i2f, 11 for f2i */
364 unsigned swizzle : 2;
365 unsigned unk : 2; /* always 10 */
366 unsigned direction_2 : 1; /* 0 for f2i, 1 for i2f */
367 unsigned op : 12;
368 } __attribute__((packed));
369
370 enum bifrost_ldst_type {
371 BIFROST_LDST_F16 = 0,
372 BIFROST_LDST_F32 = 1,
373 BIFROST_LDST_I32 = 2,
374 BIFROST_LDST_U32 = 3
375 };
376
377 #define BIFROST_ADD_OP_LD_VAR_ADDR (0x18000 >> 10)
378
379 struct bifrost_ld_var_addr {
380 unsigned src0 : 3;
381 unsigned src1 : 3;
382 unsigned location : 5;
383 enum bifrost_ldst_type type : 2;
384 unsigned op : 7;
385 } __attribute__((packed));
386
387 #define BIFROST_ADD_OP_LD_ATTR (0x08000 >> 12)
388
389 struct bifrost_ld_attr {
390 unsigned src0 : 3;
391 unsigned src1 : 3;
392 unsigned location : 5;
393 unsigned channels : 2; /* MALI_POSITIVE */
394 enum bifrost_ldst_type type : 2;
395 unsigned op : 5;
396 } __attribute__((packed));
397
398 enum bifrost_interp_mode {
399 BIFROST_INTERP_PER_FRAG = 0x0,
400 BIFROST_INTERP_CENTROID = 0x1,
401 BIFROST_INTERP_DEFAULT = 0x2,
402 BIFROST_INTERP_EXPLICIT = 0x3
403 };
404
405 #define BIFROST_ADD_OP_LD_VAR_16 (0x1a << 1)
406 #define BIFROST_ADD_OP_LD_VAR_32 (0x0a << 1)
407
408 struct bifrost_ld_var {
409 unsigned src0 : 3;
410
411 /* If top two bits set, indirect with src in bottom three */
412 unsigned addr : 5;
413
414 unsigned channels : 2; /* MALI_POSITIVE */
415 enum bifrost_interp_mode interp_mode : 2;
416 unsigned reuse : 1;
417 unsigned flat : 1;
418 unsigned op : 6;
419 } __attribute__((packed));
420
421 struct bifrost_tex_ctrl {
422 unsigned sampler_index : 4; // also used to signal indirects
423 unsigned tex_index : 7;
424 bool no_merge_index : 1; // whether to merge (direct) sampler & texture indices
425 bool filter : 1; // use the usual filtering pipeline (0 for texelFetch & textureGather)
426 unsigned unk0 : 2;
427 bool texel_offset : 1; // *Offset()
428 bool is_shadow : 1;
429 bool is_array : 1;
430 unsigned tex_type : 2; // 2D, 3D, Cube, Buffer
431 bool compute_lod : 1; // 0 for *Lod()
432 bool not_supply_lod : 1; // 0 for *Lod() or when a bias is applied
433 bool calc_gradients : 1; // 0 for *Grad()
434 unsigned unk1 : 1;
435 unsigned result_type : 4; // integer, unsigned, float TODO: why is this 4 bits?
436 unsigned unk2 : 4;
437 } __attribute__((packed));
438
439 struct bifrost_dual_tex_ctrl {
440 unsigned sampler_index0 : 2;
441 unsigned unk0 : 2;
442 unsigned tex_index0 : 2;
443 unsigned sampler_index1 : 2;
444 unsigned tex_index1 : 2;
445 unsigned unk1 : 22;
446 } __attribute__((packed));
447
448 enum branch_bit_size {
449 BR_SIZE_32 = 0,
450 BR_SIZE_16XX = 1,
451 BR_SIZE_16YY = 2,
452 // For the above combinations of bitsize and location, an extra bit is
453 // encoded via comparing the sources. The only possible source of ambiguity
454 // would be if the sources were the same, but then the branch condition
455 // would be always true or always false anyways, so we can ignore it. But
456 // this no longer works when comparing the y component to the x component,
457 // since it's valid to compare the y component of a source against its own
458 // x component. Instead, the extra bit is encoded via an extra bitsize.
459 BR_SIZE_16YX0 = 3,
460 BR_SIZE_16YX1 = 4,
461 BR_SIZE_32_AND_16X = 5,
462 BR_SIZE_32_AND_16Y = 6,
463 // Used for comparisons with zero and always-true, see below. I think this
464 // only works for integer comparisons.
465 BR_SIZE_ZERO = 7,
466 };
467
468 enum bifrost_reg_write_unit {
469 REG_WRITE_NONE = 0, // don't write
470 REG_WRITE_TWO, // write using reg2
471 REG_WRITE_THREE, // write using reg3
472 };
473
474 struct bifrost_regs {
475 unsigned uniform_const : 8;
476 unsigned reg2 : 6;
477 unsigned reg3 : 6;
478 unsigned reg0 : 5;
479 unsigned reg1 : 6;
480 unsigned ctrl : 4;
481 } __attribute__((packed));
482
483 enum bifrost_branch_cond {
484 BR_COND_LT = 0,
485 BR_COND_LE = 1,
486 BR_COND_GE = 2,
487 BR_COND_GT = 3,
488 // Equal vs. not-equal determined by src0/src1 comparison
489 BR_COND_EQ = 4,
490 // floating-point comparisons
491 // Becomes UNE when you flip the arguments
492 BR_COND_OEQ = 5,
493 // TODO what happens when you flip the arguments?
494 BR_COND_OGT = 6,
495 BR_COND_OLT = 7,
496 };
497
498 enum bifrost_branch_code {
499 BR_ALWAYS = 63,
500 };
501
502 struct bifrost_branch {
503 unsigned src0 : 3;
504
505 /* For BR_SIZE_ZERO, upper two bits become ctrl */
506 unsigned src1 : 3;
507
508 /* Offset source -- always uniform/const but
509 * theoretically could support indirect jumps? */
510 unsigned src2 : 3;
511
512 enum bifrost_branch_cond cond : 3;
513 enum branch_bit_size size : 3;
514
515 unsigned op : 5;
516 };
517
518 /* Clause packing */
519
520 #define BIFROST_FMA_NOP (0x701960 | BIFROST_SRC_STAGE)
521 #define BIFROST_ADD_NOP (0x3D960 | BIFROST_SRC_STAGE)
522
523 struct bifrost_fmt1 {
524 unsigned ins_0 : 3;
525 unsigned tag : 5;
526 uint64_t ins_1 : 64;
527 unsigned ins_2 : 11;
528 uint64_t header : 45;
529 } __attribute__((packed));
530
531 #define BIFROST_FMT1_INSTRUCTIONS 0b00101
532 #define BIFROST_FMT1_FINAL 0b01001
533 #define BIFROST_FMT1_CONSTANTS 0b00001
534
535 #define BIFROST_FMTC_CONSTANTS 0b0011
536 #define BIFROST_FMTC_FINAL 0b0111
537
538 struct bifrost_fmt_constant {
539 unsigned pos : 4;
540 unsigned tag : 4;
541 uint64_t imm_1 : 60;
542 uint64_t imm_2 : 60;
543 } __attribute__((packed));
544
545 enum bifrost_reg_control {
546 BIFROST_WRITE_FMA_P2 = 1,
547 BIFROST_WRITE_FMA_P2_READ_P3 = 2,
548 BIFROST_FIRST_WRITE_FMA_P2_READ_P3 = 3,
549 BIFROST_READ_P3 = 4,
550 BIFROST_WRITE_ADD_P2 = 5,
551 BIFROST_WRITE_ADD_P2_READ_P3 = 6,
552 BIFROST_WRITE_ADD_P2_FMA_P3 = 7,
553
554 BIFROST_FIRST_NONE = 8,
555 BIFROST_FIRST_WRITE_FMA_P2 = 9,
556 /* INSTR_INVALID_ENC */
557 BIFROST_REG_NONE = 11,
558 BIFROST_FIRST_READ_P3 = 12,
559 BIFROST_FIRST_WRITE_ADD_P2 = 13,
560 BIFROST_FIRST_WRITE_ADD_P2_READ_P3 = 14,
561 BIFROST_FIRST_WRITE_ADD_P2_FMA_P3 = 15
562 };
563
564 #endif