pan/bi: Pack a constant quadword
[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 #define BIFROST_ADD_OP_BLEND (0x1952c)
107
108 struct bifrost_add_inst {
109 unsigned src0 : 3;
110 unsigned op : 17;
111 } __attribute__((packed));
112
113 #define BIFROST_ADD_OP_ATEST (0xc8f)
114
115 struct bifrost_add_atest {
116 /* gl_SampleMask (R60) */
117 unsigned src0 : 3;
118
119 /* Alpha value */
120 unsigned src1 : 3;
121
122 /* If half, X/Y select. If !half, always set */
123 unsigned component : 1;
124 unsigned half : 1;
125
126 unsigned op : 12;
127 } __attribute__((packed));
128
129 enum bifrost_outmod {
130 BIFROST_NONE = 0x0,
131 BIFROST_POS = 0x1,
132 BIFROST_SAT_SIGNED = 0x2,
133 BIFROST_SAT = 0x3,
134 };
135
136 enum bifrost_roundmode {
137 BIFROST_RTE = 0x0, /* round to even */
138 BIFROST_RTP = 0x1, /* round to positive */
139 BIFROST_RTN = 0x2, /* round to negative */
140 BIFROST_RTZ = 0x3 /* round to zero */
141 };
142
143 /* NONE: Same as fmax() and fmin() -- return the other
144 * number if any number is NaN. Also always return +0 if
145 * one argument is +0 and the other is -0.
146 *
147 * NAN_WINS: Instead of never returning a NaN, always return
148 * one. The "greater"/"lesser" NaN is always returned, first
149 * by checking the sign and then the mantissa bits.
150 *
151 * SRC1_WINS: For max, implement src0 > src1 ? src0 : src1.
152 * For min, implement src0 < src1 ? src0 : src1. This
153 * includes handling NaN's and signedness of 0 differently
154 * from above, since +0 and -0 compare equal and comparisons
155 * always return false for NaN's. As a result, this mode is
156 * *not* commutative.
157 *
158 * SRC0_WINS: For max, implement src0 < src1 ? src1 : src0
159 * For min, implement src0 > src1 ? src1 : src0
160 */
161
162
163 enum bifrost_minmax_mode {
164 BIFROST_MINMAX_NONE = 0x0,
165 BIFROST_NAN_WINS = 0x1,
166 BIFROST_SRC1_WINS = 0x2,
167 BIFROST_SRC0_WINS = 0x3,
168 };
169
170 #define BIFROST_FMA_OP_FADD32 (0x58 >> 2)
171
172 struct bifrost_fma_add {
173 unsigned src0 : 3;
174 unsigned src1 : 3;
175 unsigned src1_abs : 1;
176 unsigned src0_neg : 1;
177 unsigned src1_neg : 1;
178 unsigned unk : 3;
179 unsigned src0_abs : 1;
180 enum bifrost_outmod outmod : 2;
181 enum bifrost_roundmode roundmode : 2;
182 unsigned op : 6;
183 } __attribute__((packed));
184
185 #define BIFROST_FMA_OP_FMA (0x00)
186
187 struct bifrost_fma_fma {
188 unsigned src0 : 3;
189 unsigned src1 : 3;
190 unsigned src2 : 3;
191 unsigned src_expand : 3;
192 unsigned src0_abs : 1;
193 unsigned unk : 4;
194 unsigned src0_neg : 1;
195 unsigned src2_neg : 1;
196 unsigned src1_abs : 1;
197 unsigned src2_abs : 1;
198 unsigned op : 5;
199 } __attribute__((packed));
200
201 enum bifrost_csel_cond {
202 BIFROST_FEQ_F = 0x0,
203 BIFROST_FGT_F = 0x1,
204 BIFROST_FGE_F = 0x2,
205 BIFROST_IEQ_F = 0x3,
206 BIFROST_IGT_I = 0x4,
207 BIFROST_IGE_I = 0x5,
208 BIFROST_UGT_I = 0x6,
209 BIFROST_UGE_I = 0x7
210 };
211
212 struct bifrost_csel4 {
213 unsigned src0 : 3;
214 unsigned src1 : 3;
215 unsigned src2 : 3;
216 unsigned src3 : 3;
217 enum bifrost_csel_cond cond : 3;
218 unsigned op : 8;
219 } __attribute__((packed));
220
221 struct bifrost_shift_fma {
222 unsigned src0 : 3;
223 unsigned src1 : 3;
224 unsigned src2 : 3;
225 unsigned half : 3; /* 000 for i32, 100 for i8, 111 for v2i16 */
226 unsigned unk : 1; /* always set? */
227 unsigned invert_1 : 1; /* Inverts sources to combining op */
228 /* For XOR, switches RSHIFT to LSHIFT since only one invert needed */
229 unsigned invert_2 : 1;
230 unsigned op : 8;
231 } __attribute__((packed));
232
233 struct bifrost_shift_add {
234 unsigned src0 : 3;
235 unsigned src1 : 3;
236 unsigned src2 : 3;
237 unsigned zero : 2;
238
239 unsigned invert_1 : 1;
240 unsigned invert_2 : 1;
241
242 unsigned op : 7;
243 } __attribute__((packed));
244
245 enum bifrost_ldst_type {
246 BIFROST_LDST_F16 = 0,
247 BIFROST_LDST_F32 = 1,
248 BIFROST_LDST_I32 = 2,
249 BIFROST_LDST_U32 = 3
250 };
251
252 struct bifrost_ld_var_addr {
253 unsigned src0 : 3;
254 unsigned src1 : 3;
255 unsigned location : 5;
256 enum bifrost_ldst_type type : 2;
257 unsigned op : 7;
258 } __attribute__((packed));
259
260 struct bifrost_ld_attr {
261 unsigned src0 : 3;
262 unsigned src1 : 3;
263 unsigned location : 5;
264 unsigned channels : 2; /* MALI_POSITIVE */
265 enum bifrost_ldst_type type : 2;
266 unsigned op : 5;
267 } __attribute__((packed));
268
269 enum bifrost_interp_mode {
270 BIFROST_INTERP_PER_FRAG = 0x0,
271 BIFROST_INTERP_CENTROID = 0x1,
272 BIFROST_INTERP_DEFAULT = 0x2,
273 BIFROST_INTERP_EXPLICIT = 0x3
274 };
275
276 #define BIFROST_ADD_OP_LD_VAR_16 (0x1a << 1)
277 #define BIFROST_ADD_OP_LD_VAR_32 (0x0a << 1)
278
279 struct bifrost_ld_var {
280 unsigned src0 : 3;
281
282 /* If top two bits set, indirect with src in bottom three */
283 unsigned addr : 5;
284
285 unsigned channels : 2; /* MALI_POSITIVE */
286 enum bifrost_interp_mode interp_mode : 2;
287 unsigned reuse : 1;
288 unsigned flat : 1;
289 unsigned op : 6;
290 } __attribute__((packed));
291
292 struct bifrost_tex_ctrl {
293 unsigned sampler_index : 4; // also used to signal indirects
294 unsigned tex_index : 7;
295 bool no_merge_index : 1; // whether to merge (direct) sampler & texture indices
296 bool filter : 1; // use the usual filtering pipeline (0 for texelFetch & textureGather)
297 unsigned unk0 : 2;
298 bool texel_offset : 1; // *Offset()
299 bool is_shadow : 1;
300 bool is_array : 1;
301 unsigned tex_type : 2; // 2D, 3D, Cube, Buffer
302 bool compute_lod : 1; // 0 for *Lod()
303 bool not_supply_lod : 1; // 0 for *Lod() or when a bias is applied
304 bool calc_gradients : 1; // 0 for *Grad()
305 unsigned unk1 : 1;
306 unsigned result_type : 4; // integer, unsigned, float TODO: why is this 4 bits?
307 unsigned unk2 : 4;
308 } __attribute__((packed));
309
310 struct bifrost_dual_tex_ctrl {
311 unsigned sampler_index0 : 2;
312 unsigned unk0 : 2;
313 unsigned tex_index0 : 2;
314 unsigned sampler_index1 : 2;
315 unsigned tex_index1 : 2;
316 unsigned unk1 : 22;
317 } __attribute__((packed));
318
319 enum branch_bit_size {
320 BR_SIZE_32 = 0,
321 BR_SIZE_16XX = 1,
322 BR_SIZE_16YY = 2,
323 // For the above combinations of bitsize and location, an extra bit is
324 // encoded via comparing the sources. The only possible source of ambiguity
325 // would be if the sources were the same, but then the branch condition
326 // would be always true or always false anyways, so we can ignore it. But
327 // this no longer works when comparing the y component to the x component,
328 // since it's valid to compare the y component of a source against its own
329 // x component. Instead, the extra bit is encoded via an extra bitsize.
330 BR_SIZE_16YX0 = 3,
331 BR_SIZE_16YX1 = 4,
332 BR_SIZE_32_AND_16X = 5,
333 BR_SIZE_32_AND_16Y = 6,
334 // Used for comparisons with zero and always-true, see below. I think this
335 // only works for integer comparisons.
336 BR_SIZE_ZERO = 7,
337 };
338
339 enum bifrost_reg_write_unit {
340 REG_WRITE_NONE = 0, // don't write
341 REG_WRITE_TWO, // write using reg2
342 REG_WRITE_THREE, // write using reg3
343 };
344
345 struct bifrost_regs {
346 unsigned uniform_const : 8;
347 unsigned reg2 : 6;
348 unsigned reg3 : 6;
349 unsigned reg0 : 5;
350 unsigned reg1 : 6;
351 unsigned ctrl : 4;
352 } __attribute__((packed));
353
354 enum bifrost_branch_cond {
355 BR_COND_LT = 0,
356 BR_COND_LE = 1,
357 BR_COND_GE = 2,
358 BR_COND_GT = 3,
359 // Equal vs. not-equal determined by src0/src1 comparison
360 BR_COND_EQ = 4,
361 // floating-point comparisons
362 // Becomes UNE when you flip the arguments
363 BR_COND_OEQ = 5,
364 // TODO what happens when you flip the arguments?
365 BR_COND_OGT = 6,
366 BR_COND_OLT = 7,
367 };
368
369 enum bifrost_branch_code {
370 BR_ALWAYS = 63,
371 };
372
373 struct bifrost_branch {
374 unsigned src0 : 3;
375
376 /* For BR_SIZE_ZERO, upper two bits become ctrl */
377 unsigned src1 : 3;
378
379 /* Offset source -- always uniform/const but
380 * theoretically could support indirect jumps? */
381 unsigned src2 : 3;
382
383 enum bifrost_branch_cond cond : 3;
384 enum branch_bit_size size : 3;
385
386 unsigned op : 5;
387 };
388
389 /* Clause packing */
390
391 #define BIFROST_FMA_NOP (0x701960)
392 #define BIFROST_ADD_NOP (0x3D960)
393
394 struct bifrost_fmt1 {
395 unsigned ins_0 : 3;
396 unsigned tag : 5;
397 uint64_t ins_1 : 64;
398 unsigned ins_2 : 11;
399 uint64_t header : 45;
400 } __attribute__((packed));
401
402 #define BIFROST_FMT1_INSTRUCTIONS 0b00101
403 #define BIFROST_FMT1_FINAL 0b01001
404 #define BIFROST_FMT1_CONSTANTS 0b00001
405
406 #define BIFROST_FMTC_CONSTANTS 0b0011
407 #define BIFROST_FMTC_FINAL 0b0111
408
409 struct bifrost_fmt_constant {
410 unsigned pos : 4;
411 unsigned tag : 4;
412 uint64_t imm_1 : 60;
413 uint64_t imm_2 : 60;
414 } __attribute__((packed));
415
416 enum bifrost_reg_control {
417 BIFROST_WRITE_FMA_P2 = 1,
418 BIFROST_WRITE_FMA_P2_READ_P3 = 2,
419 BIFROST_WRITE_FMA_P2_READ_P3_ALT = 3,
420 BIFROST_READ_P3 = 4,
421 BIFROST_WRITE_ADD_P2 = 5,
422 BIFROST_WRITE_ADD_P2_READ_P3 = 6,
423 BIFROST_WRITE_ADD_P2_FMA_P3 = 7,
424
425 BIFROST_FIRST_NONE = 8,
426 BIFROST_FIRST_WRITE_FMA_P2 = 9,
427 BIFROST_REG_NONE = 11,
428 BIFROST_FIRST_READ_P3 = 12,
429 BIFROST_FIRST_WRITE_ADD_P2 = 13,
430 BIFROST_FIRST_WRITE_ADD_P2_READ_P3 = 14,
431 BIFROST_FIRST_WRITE_ADD_P2_FMA_P3 = 15
432 };
433
434 #endif