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