panfrost/midgard: Add shifting int modifiers
[mesa.git] / src / gallium / drivers / panfrost / midgard / midgard.h
1 /* Author(s):
2 * Connor Abbott
3 * Alyssa Rosenzweig
4 *
5 * Copyright (c) 2013 Connor Abbott (connor@abbott.cx)
6 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #ifndef __midgard_h__
28 #define __midgard_h__
29
30 #include <stdint.h>
31 #include <stdbool.h>
32
33 #define MIDGARD_DBG_MSGS 0x0001
34 #define MIDGARD_DBG_SHADERS 0x0002
35
36 extern int midgard_debug;
37
38 typedef enum {
39 midgard_word_type_alu,
40 midgard_word_type_load_store,
41 midgard_word_type_texture,
42 midgard_word_type_unknown
43 } midgard_word_type;
44
45 typedef enum {
46 midgard_alu_vmul,
47 midgard_alu_sadd,
48 midgard_alu_smul,
49 midgard_alu_vadd,
50 midgard_alu_lut
51 } midgard_alu;
52
53 /*
54 * ALU words
55 */
56
57 typedef enum {
58 midgard_alu_op_fadd = 0x10,
59 midgard_alu_op_fmul = 0x14,
60
61 midgard_alu_op_fmin = 0x28,
62 midgard_alu_op_fmax = 0x2C,
63
64 midgard_alu_op_fmov = 0x30,
65 midgard_alu_op_froundeven = 0x34,
66 midgard_alu_op_ftrunc = 0x35,
67 midgard_alu_op_ffloor = 0x36,
68 midgard_alu_op_fceil = 0x37,
69 midgard_alu_op_ffma = 0x38,
70 midgard_alu_op_fdot3 = 0x3C,
71 midgard_alu_op_fdot3r = 0x3D,
72 midgard_alu_op_fdot4 = 0x3E,
73 midgard_alu_op_freduce = 0x3F,
74
75 midgard_alu_op_iadd = 0x40,
76 midgard_alu_op_ishladd = 0x41,
77 midgard_alu_op_isub = 0x46,
78 midgard_alu_op_iaddsat = 0x48,
79 midgard_alu_op_uaddsat = 0x49,
80 midgard_alu_op_isubsat = 0x4E,
81 midgard_alu_op_usubsat = 0x4F,
82
83 midgard_alu_op_imul = 0x58,
84
85 midgard_alu_op_imin = 0x60,
86 midgard_alu_op_umin = 0x61,
87 midgard_alu_op_imax = 0x62,
88 midgard_alu_op_umax = 0x63,
89 midgard_alu_op_ihadd = 0x64,
90 midgard_alu_op_uhadd = 0x65,
91 midgard_alu_op_irhadd = 0x66,
92 midgard_alu_op_urhadd = 0x67,
93 midgard_alu_op_iasr = 0x68,
94 midgard_alu_op_ilsr = 0x69,
95 midgard_alu_op_ishl = 0x6E,
96
97 midgard_alu_op_iand = 0x70,
98 midgard_alu_op_ior = 0x71,
99 midgard_alu_op_inand = 0x72, /* ~(a & b), for inot let a = b */
100 midgard_alu_op_inor = 0x73, /* ~(a | b) */
101 midgard_alu_op_iandnot = 0x74, /* (a & ~b), used for not/b2f */
102 midgard_alu_op_iornot = 0x75, /* (a | ~b) */
103 midgard_alu_op_ixor = 0x76,
104 midgard_alu_op_inxor = 0x77, /* ~(a & b) */
105 midgard_alu_op_iclz = 0x78, /* Number of zeroes on left */
106 midgard_alu_op_ibitcount8 = 0x7A, /* Counts bits in 8-bit increments */
107 midgard_alu_op_imov = 0x7B,
108 midgard_alu_op_iabsdiff = 0x7C,
109 midgard_alu_op_uabsdiff = 0x7D,
110 midgard_alu_op_ichoose = 0x7E, /* vector, component number - dupe for shuffle() */
111
112 midgard_alu_op_feq = 0x80,
113 midgard_alu_op_fne = 0x81,
114 midgard_alu_op_flt = 0x82,
115 midgard_alu_op_fle = 0x83,
116 midgard_alu_op_fball_eq = 0x88,
117 midgard_alu_op_bball_eq = 0x89,
118 midgard_alu_op_fball_lt = 0x8A, /* all(lessThan(.., ..)) */
119 midgard_alu_op_fball_lte = 0x8B, /* all(lessThanEqual(.., ..)) */
120
121 midgard_alu_op_bbany_neq = 0x90, /* used for bvec4(1) */
122 midgard_alu_op_fbany_neq = 0x91, /* bvec4(0) also */
123 midgard_alu_op_fbany_lt = 0x92, /* any(lessThan(.., ..)) */
124 midgard_alu_op_fbany_lte = 0x93, /* any(lessThanEqual(.., ..)) */
125 midgard_alu_op_f2i = 0x99,
126 midgard_alu_op_f2u8 = 0x9C,
127 midgard_alu_op_f2u = 0x9D,
128
129 midgard_alu_op_ieq = 0xA0,
130 midgard_alu_op_ine = 0xA1,
131 midgard_alu_op_ult = 0xA2,
132 midgard_alu_op_ule = 0xA3,
133 midgard_alu_op_ilt = 0xA4,
134 midgard_alu_op_ile = 0xA5,
135 midgard_alu_op_iball_eq = 0xA8,
136 midgard_alu_op_iball_neq = 0xA9,
137 midgard_alu_op_uball_lt = 0xAA,
138 midgard_alu_op_uball_lte = 0xAB,
139 midgard_alu_op_iball_lt = 0xAC,
140 midgard_alu_op_iball_lte = 0xAD,
141
142 midgard_alu_op_ibany_eq = 0xB0,
143 midgard_alu_op_ibany_neq = 0xB1,
144 midgard_alu_op_ubany_lt = 0xB2,
145 midgard_alu_op_ubany_lte = 0xB3,
146 midgard_alu_op_ibany_lt = 0xB4, /* any(lessThan(.., ..)) */
147 midgard_alu_op_ibany_lte = 0xB5, /* any(lessThanEqual(.., ..)) */
148 midgard_alu_op_i2f = 0xB8,
149 midgard_alu_op_u2f = 0xBC,
150
151 midgard_alu_op_icsel_v = 0xC0, /* condition code r31 */
152 midgard_alu_op_icsel = 0xC1, /* condition code r31.w */
153 midgard_alu_op_fcsel_v = 0xC4,
154 midgard_alu_op_fcsel = 0xC5,
155 midgard_alu_op_fround = 0xC6,
156
157 midgard_alu_op_fatan_pt2 = 0xE8,
158 midgard_alu_op_fpow_pt1 = 0xEC,
159 midgard_alu_op_fpown_pt1 = 0xED,
160 midgard_alu_op_fpowr_pt1 = 0xEE,
161
162 midgard_alu_op_frcp = 0xF0,
163 midgard_alu_op_frsqrt = 0xF2,
164 midgard_alu_op_fsqrt = 0xF3,
165 midgard_alu_op_fexp2 = 0xF4,
166 midgard_alu_op_flog2 = 0xF5,
167 midgard_alu_op_fsin = 0xF6,
168 midgard_alu_op_fcos = 0xF7,
169 midgard_alu_op_fatan2_pt1 = 0xF9,
170 } midgard_alu_op;
171
172 typedef enum {
173 midgard_outmod_none = 0,
174 midgard_outmod_pos = 1,
175 /* 0x2 unknown */
176 midgard_outmod_sat = 3
177 } midgard_outmod_float;
178
179 typedef enum {
180 midgard_outmod_int_saturate = 0,
181 midgard_outmod_uint_saturate = 1,
182 midgard_outmod_int_wrap = 2,
183 midgard_outmod_int_high = 3, /* Overflowed portion */
184 } midgard_outmod_int;
185
186 typedef enum {
187 midgard_reg_mode_8 = 0,
188 midgard_reg_mode_16 = 1,
189 midgard_reg_mode_32 = 2,
190 midgard_reg_mode_64 = 3
191 } midgard_reg_mode;
192
193 typedef enum {
194 midgard_dest_override_lower = 0,
195 midgard_dest_override_upper = 1,
196 midgard_dest_override_none = 2
197 } midgard_dest_override;
198
199 typedef enum {
200 midgard_int_sign_extend = 0,
201 midgard_int_zero_extend = 1,
202 midgard_int_normal = 2,
203 midgard_int_shift = 3
204 } midgard_int_mod;
205
206 #define MIDGARD_FLOAT_MOD_ABS (1 << 0)
207 #define MIDGARD_FLOAT_MOD_NEG (1 << 1)
208
209 typedef struct
210 __attribute__((__packed__))
211 {
212 /* Either midgard_int_mod or from midgard_float_mod_*, depending on the
213 * type of op */
214 unsigned mod : 2;
215
216 /* replicate lower half if dest = half, or low/high half selection if
217 * dest = full
218 */
219 bool rep_low : 1;
220 bool rep_high : 1; /* unused if dest = full */
221 bool half : 1; /* only matters if dest = full */
222 unsigned swizzle : 8;
223 }
224 midgard_vector_alu_src;
225
226 typedef struct
227 __attribute__((__packed__))
228 {
229 midgard_alu_op op : 8;
230 midgard_reg_mode reg_mode : 2;
231 unsigned src1 : 13;
232 unsigned src2 : 13;
233 midgard_dest_override dest_override : 2;
234 midgard_outmod_float outmod : 2;
235 unsigned mask : 8;
236 }
237 midgard_vector_alu;
238
239 typedef struct
240 __attribute__((__packed__))
241 {
242 bool abs : 1;
243 bool negate : 1;
244 bool full : 1; /* 0 = half, 1 = full */
245 unsigned component : 3;
246 }
247 midgard_scalar_alu_src;
248
249 typedef struct
250 __attribute__((__packed__))
251 {
252 midgard_alu_op op : 8;
253 unsigned src1 : 6;
254 unsigned src2 : 11;
255 unsigned unknown : 1;
256 unsigned outmod : 2;
257 bool output_full : 1;
258 unsigned output_component : 3;
259 }
260 midgard_scalar_alu;
261
262 typedef struct
263 __attribute__((__packed__))
264 {
265 unsigned src1_reg : 5;
266 unsigned src2_reg : 5;
267 unsigned out_reg : 5;
268 bool src2_imm : 1;
269 }
270 midgard_reg_info;
271
272 /* In addition to conditional branches and jumps (unconditional branches),
273 * Midgard implements a bit of fixed function functionality used in fragment
274 * shaders via specially crafted branches. These have special branch opcodes,
275 * which perform a fixed-function operation and/or use the results of a
276 * fixed-function operation as the branch condition. */
277
278 typedef enum {
279 /* Regular branches */
280 midgard_jmp_writeout_op_branch_uncond = 1,
281 midgard_jmp_writeout_op_branch_cond = 2,
282
283 /* In a fragment shader, execute a discard_if instruction, with the
284 * corresponding condition code. Terminates the shader, so generally
285 * set the branch target to out of the shader */
286 midgard_jmp_writeout_op_discard = 4,
287
288 /* Branch if the tilebuffer is not yet ready. At the beginning of a
289 * fragment shader that reads from the tile buffer, for instance via
290 * ARM_shader_framebuffer_fetch or EXT_pixel_local_storage, this branch
291 * operation should be used as a loop. An instruction like
292 * "br.tilebuffer.always -1" does the trick, corresponding to
293 * "while(!is_tilebuffer_ready) */
294 midgard_jmp_writeout_op_tilebuffer_pending = 6,
295
296 /* In a fragment shader, try to write out the value pushed to r0 to the
297 * tilebuffer, subject to unknown state in r1.z and r1.w. If this
298 * succeeds, the shader terminates. If it fails, it branches to the
299 * specified branch target. Generally, this should be used in a loop to
300 * itself, acting as "do { write(r0); } while(!write_successful);" */
301 midgard_jmp_writeout_op_writeout = 7,
302 } midgard_jmp_writeout_op;
303
304 typedef enum {
305 midgard_condition_write0 = 0,
306
307 /* These condition codes denote a conditional branch on FALSE and on
308 * TRUE respectively */
309 midgard_condition_false = 1,
310 midgard_condition_true = 2,
311
312 /* This condition code always branches. For a pure branch, the
313 * unconditional branch coding should be used instead, but for
314 * fixed-function branch opcodes, this is still useful */
315 midgard_condition_always = 3,
316 } midgard_condition;
317
318 typedef struct
319 __attribute__((__packed__))
320 {
321 midgard_jmp_writeout_op op : 3; /* == branch_uncond */
322 unsigned dest_tag : 4; /* tag of branch destination */
323 unsigned unknown : 2;
324 int offset : 7;
325 }
326 midgard_branch_uncond;
327
328 typedef struct
329 __attribute__((__packed__))
330 {
331 midgard_jmp_writeout_op op : 3; /* == branch_cond */
332 unsigned dest_tag : 4; /* tag of branch destination */
333 int offset : 7;
334 midgard_condition cond : 2;
335 }
336 midgard_branch_cond;
337
338 typedef struct
339 __attribute__((__packed__))
340 {
341 midgard_jmp_writeout_op op : 3; /* == branch_cond */
342 unsigned dest_tag : 4; /* tag of branch destination */
343 unsigned unknown : 2;
344 signed offset : 23;
345 unsigned cond : 16;
346 }
347 midgard_branch_extended;
348
349 typedef struct
350 __attribute__((__packed__))
351 {
352 midgard_jmp_writeout_op op : 3; /* == writeout */
353 unsigned unknown : 13;
354 }
355 midgard_writeout;
356
357 /*
358 * Load/store words
359 */
360
361 typedef enum {
362 midgard_op_ld_st_noop = 0x03,
363
364 /* Unclear why this is on the L/S unit, but (with an address of 0,
365 * appropriate swizzle, magic constant 0x24, and xy mask?) moves fp32 cube
366 * map coordinates in r27 to its cube map texture coordinate
367 * destination (e.g r29). 0x4 magic for lding from fp16 instead */
368
369 midgard_op_st_cubemap_coords = 0x0E,
370
371 /* Used in OpenCL. Probably can ld other things as well */
372 midgard_op_ld_global_id = 0x10,
373
374 /* val in r27.y, address embedded, outputs result to argument. Invert val for sub. Let val = +-1 for inc/dec. */
375 midgard_op_atomic_add = 0x40,
376 midgard_op_atomic_and = 0x44,
377 midgard_op_atomic_or = 0x48,
378 midgard_op_atomic_xor = 0x4C,
379
380 midgard_op_atomic_imin = 0x50,
381 midgard_op_atomic_umin = 0x54,
382 midgard_op_atomic_imax = 0x58,
383 midgard_op_atomic_umax = 0x5C,
384
385 midgard_op_atomic_xchg = 0x60,
386
387 /* Used for compute shader's __global arguments, __local variables (or
388 * for register spilling) */
389
390 midgard_op_ld_char = 0x81,
391 midgard_op_ld_char2 = 0x84,
392 midgard_op_ld_short = 0x85,
393 midgard_op_ld_char4 = 0x88, /* short2, int, float */
394 midgard_op_ld_short4 = 0x8C, /* int2, float2, long */
395 midgard_op_ld_int4 = 0x90, /* float4, long2 */
396
397 midgard_op_ld_attr_32 = 0x94,
398 midgard_op_ld_attr_16 = 0x95,
399 midgard_op_ld_attr_32i = 0x97,
400 midgard_op_ld_vary_32 = 0x98,
401 midgard_op_ld_vary_16 = 0x99,
402 midgard_op_ld_vary_32i = 0x9B,
403 midgard_op_ld_color_buffer_16 = 0x9D,
404
405 midgard_op_ld_uniform_16 = 0xAC,
406
407 midgard_op_ld_uniform_32 = 0xB0,
408 midgard_op_ld_color_buffer_8 = 0xBA,
409
410 midgard_op_st_char = 0xC0,
411 midgard_op_st_char2 = 0xC4, /* short */
412 midgard_op_st_char4 = 0xC8, /* short2, int, float */
413 midgard_op_st_short4 = 0xCC, /* int2, float2, long */
414 midgard_op_st_int4 = 0xD0, /* float4, long2 */
415
416 midgard_op_st_vary_32 = 0xD4,
417 midgard_op_st_vary_16 = 0xD5,
418 midgard_op_st_vary_32i = 0xD7,
419
420 /* Value to st in r27, location r26.w as short2 */
421 midgard_op_st_image_f = 0xD8,
422 midgard_op_st_image_ui = 0xDA,
423 midgard_op_st_image_i = 0xDB,
424 } midgard_load_store_op;
425
426 typedef enum {
427 midgard_interp_centroid = 1,
428 midgard_interp_default = 2
429 } midgard_interpolation;
430
431 typedef struct
432 __attribute__((__packed__))
433 {
434 unsigned zero1 : 4; /* Always zero */
435
436 /* Varying qualifiers, zero if not a varying */
437 unsigned flat : 1;
438 unsigned is_varying : 1; /* Always one for varying, but maybe something else? */
439 midgard_interpolation interpolation : 2;
440
441 unsigned zero2 : 2; /* Always zero */
442 }
443 midgard_varying_parameter;
444
445 typedef struct
446 __attribute__((__packed__))
447 {
448 midgard_load_store_op op : 8;
449 unsigned reg : 5;
450 unsigned mask : 4;
451 unsigned swizzle : 8;
452 unsigned unknown : 16;
453
454 unsigned varying_parameters : 10;
455
456 unsigned address : 9;
457 }
458 midgard_load_store_word;
459
460 typedef struct
461 __attribute__((__packed__))
462 {
463 unsigned type : 4;
464 unsigned next_type : 4;
465 uint64_t word1 : 60;
466 uint64_t word2 : 60;
467 }
468 midgard_load_store;
469
470 /* Texture pipeline results are in r28-r29 */
471 #define REG_TEX_BASE 28
472
473 /* Texture opcodes... maybe? */
474 #define TEXTURE_OP_NORMAL 0x11
475 #define TEXTURE_OP_TEXEL_FETCH 0x14
476
477 /* Texture format types, found in format */
478 #define TEXTURE_CUBE 0x00
479 #define TEXTURE_2D 0x02
480 #define TEXTURE_3D 0x03
481
482 typedef struct
483 __attribute__((__packed__))
484 {
485 unsigned type : 4;
486 unsigned next_type : 4;
487
488 unsigned op : 6;
489 unsigned shadow : 1;
490 unsigned unknown3 : 1;
491
492 /* A little obscure, but last is set for the last texture operation in
493 * a shader. cont appears to just be last's opposite (?). Yeah, I know,
494 * kind of funky.. BiOpen thinks it could do with memory hinting, or
495 * tile locking? */
496
497 unsigned cont : 1;
498 unsigned last : 1;
499
500 unsigned format : 5;
501 unsigned has_offset : 1;
502
503 /* Like in Bifrost */
504 unsigned filter : 1;
505
506 unsigned in_reg_select : 1;
507 unsigned in_reg_upper : 1;
508
509 unsigned in_reg_swizzle_left : 2;
510 unsigned in_reg_swizzle_right : 2;
511
512 unsigned unknown1 : 2;
513
514 unsigned unknown8 : 4;
515
516 unsigned out_full : 1;
517
518 /* Always 1 afaict... */
519 unsigned unknown7 : 2;
520
521 unsigned out_reg_select : 1;
522 unsigned out_upper : 1;
523
524 unsigned mask : 4;
525
526 unsigned unknown2 : 2;
527
528 unsigned swizzle : 8;
529 unsigned unknown4 : 8;
530
531 unsigned unknownA : 4;
532
533 unsigned offset_unknown1 : 1;
534 unsigned offset_reg_select : 1;
535 unsigned offset_reg_upper : 1;
536 unsigned offset_unknown4 : 1;
537 unsigned offset_unknown5 : 1;
538 unsigned offset_unknown6 : 1;
539 unsigned offset_unknown7 : 1;
540 unsigned offset_unknown8 : 1;
541 unsigned offset_unknown9 : 1;
542
543 unsigned unknownB : 3;
544
545 /* Texture bias or LOD, depending on whether it is executed in a
546 * fragment/vertex shader respectively. Compute as int(2^8 * biasf).
547 *
548 * For texel fetch, this is the LOD as is. */
549 unsigned bias : 8;
550
551 unsigned unknown9 : 8;
552
553 unsigned texture_handle : 16;
554 unsigned sampler_handle : 16;
555 }
556 midgard_texture_word;
557
558 #endif