panfrost/midgard: Document sign-extension/zero-extension bits (vector)
[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 midgard_alu_op_fmin = 0x28,
61 midgard_alu_op_fmax = 0x2C,
62 midgard_alu_op_fmov = 0x30,
63 midgard_alu_op_froundeven = 0x34,
64 midgard_alu_op_ftrunc = 0x35,
65 midgard_alu_op_ffloor = 0x36,
66 midgard_alu_op_fceil = 0x37,
67 midgard_alu_op_ffma = 0x38,
68 midgard_alu_op_fdot3 = 0x3C,
69 midgard_alu_op_fdot3r = 0x3D,
70 midgard_alu_op_fdot4 = 0x3E,
71 midgard_alu_op_freduce = 0x3F,
72 midgard_alu_op_iadd = 0x40,
73 midgard_alu_op_ishladd = 0x41,
74 midgard_alu_op_isub = 0x46,
75 midgard_alu_op_imul = 0x58,
76 midgard_alu_op_imin = 0x60,
77 midgard_alu_op_umin = 0x61,
78 midgard_alu_op_imax = 0x62,
79 midgard_alu_op_umax = 0x63,
80 midgard_alu_op_iasr = 0x68,
81 midgard_alu_op_ilsr = 0x69,
82 midgard_alu_op_ishl = 0x6E,
83 midgard_alu_op_iand = 0x70,
84 midgard_alu_op_ior = 0x71,
85 midgard_alu_op_inot = 0x72,
86 midgard_alu_op_iandnot = 0x74, /* (a, b) -> a & ~b, used for not/b2f */
87 midgard_alu_op_ixor = 0x76,
88 midgard_alu_op_ilzcnt = 0x78, /* Number of zeroes on left. 31 - ilzcnt(x) = findMSB(x) */
89 midgard_alu_op_ibitcount8 = 0x7A, /* Counts bits in 8-bit increments */
90 midgard_alu_op_imov = 0x7B,
91 midgard_alu_op_iabs = 0x7C,
92 midgard_alu_op_feq = 0x80,
93 midgard_alu_op_fne = 0x81,
94 midgard_alu_op_flt = 0x82,
95 midgard_alu_op_fle = 0x83,
96 midgard_alu_op_fball_eq = 0x88,
97 midgard_alu_op_bball_eq = 0x89,
98 midgard_alu_op_fball_lt = 0x8A, /* all(lessThan(.., ..)) */
99 midgard_alu_op_fball_lte = 0x8B, /* all(lessThanEqual(.., ..)) */
100 midgard_alu_op_bbany_neq = 0x90, /* used for bvec4(1) */
101 midgard_alu_op_fbany_neq = 0x91, /* bvec4(0) also */
102 midgard_alu_op_fbany_lt = 0x92, /* any(lessThan(.., ..)) */
103 midgard_alu_op_fbany_lte = 0x93, /* any(lessThanEqual(.., ..)) */
104 midgard_alu_op_f2i = 0x99,
105 midgard_alu_op_f2u8 = 0x9C,
106 midgard_alu_op_f2u = 0x9D,
107
108 midgard_alu_op_ieq = 0xA0,
109 midgard_alu_op_ine = 0xA1,
110 midgard_alu_op_ult = 0xA2,
111 midgard_alu_op_ule = 0xA3,
112 midgard_alu_op_ilt = 0xA4,
113 midgard_alu_op_ile = 0xA5,
114 midgard_alu_op_iball_eq = 0xA8,
115 midgard_alu_op_ball = 0xA9,
116 midgard_alu_op_uball_lt = 0xAA,
117 midgard_alu_op_uball_lte = 0xAB,
118 midgard_alu_op_iball_lt = 0xAC,
119 midgard_alu_op_iball_lte = 0xAD,
120 midgard_alu_op_ibany_eq = 0xB0,
121 midgard_alu_op_ibany_neq = 0xB1,
122 midgard_alu_op_ubany_lt = 0xB2,
123 midgard_alu_op_ubany_lte = 0xB3,
124 midgard_alu_op_ibany_lt = 0xB4, /* any(lessThan(.., ..)) */
125 midgard_alu_op_ibany_lte = 0xB5, /* any(lessThanEqual(.., ..)) */
126 midgard_alu_op_i2f = 0xB8,
127 midgard_alu_op_u2f = 0xBC,
128 midgard_alu_op_icsel = 0xC1,
129 midgard_alu_op_fcsel_i = 0xC4,
130 midgard_alu_op_fcsel = 0xC5,
131 midgard_alu_op_fround = 0xC6,
132 midgard_alu_op_fatan_pt2 = 0xE8,
133 midgard_alu_op_fpow_pt1 = 0xEC,
134 midgard_alu_op_frcp = 0xF0,
135 midgard_alu_op_frsqrt = 0xF2,
136 midgard_alu_op_fsqrt = 0xF3,
137 midgard_alu_op_fexp2 = 0xF4,
138 midgard_alu_op_flog2 = 0xF5,
139 midgard_alu_op_fsin = 0xF6,
140 midgard_alu_op_fcos = 0xF7,
141 midgard_alu_op_fatan2_pt1 = 0xF9,
142 } midgard_alu_op;
143
144 typedef enum {
145 midgard_outmod_none = 0,
146 midgard_outmod_pos = 1,
147 midgard_outmod_int = 2,
148 midgard_outmod_sat = 3
149 } midgard_outmod;
150
151 typedef enum {
152 midgard_reg_mode_quarter = 0,
153 midgard_reg_mode_half = 1,
154 midgard_reg_mode_full = 2,
155 midgard_reg_mode_double = 3 /* TODO: verify */
156 } midgard_reg_mode;
157
158 typedef enum {
159 midgard_dest_override_lower = 0,
160 midgard_dest_override_upper = 1,
161 midgard_dest_override_none = 2
162 } midgard_dest_override;
163
164 typedef enum {
165 midgard_int_sign_extend = 0,
166 midgard_int_zero_extend = 1,
167 midgard_int_normal = 2,
168 midgard_int_reserved = 3
169 } midgard_int_mod;
170
171 #define MIDGARD_FLOAT_MOD_ABS (1 << 0)
172 #define MIDGARD_FLOAT_MOD_NEG (1 << 1)
173
174 typedef struct
175 __attribute__((__packed__))
176 {
177 /* Either midgard_int_mod or from midgard_float_mod_*, depending on the
178 * type of op */
179 unsigned mod : 2;
180
181 /* replicate lower half if dest = half, or low/high half selection if
182 * dest = full
183 */
184 bool rep_low : 1;
185 bool rep_high : 1; /* unused if dest = full */
186 bool half : 1; /* only matters if dest = full */
187 unsigned swizzle : 8;
188 }
189 midgard_vector_alu_src;
190
191 typedef struct
192 __attribute__((__packed__))
193 {
194 midgard_alu_op op : 8;
195 midgard_reg_mode reg_mode : 2;
196 unsigned src1 : 13;
197 unsigned src2 : 13;
198 midgard_dest_override dest_override : 2;
199 midgard_outmod outmod : 2;
200 unsigned mask : 8;
201 }
202 midgard_vector_alu;
203
204 typedef struct
205 __attribute__((__packed__))
206 {
207 bool abs : 1;
208 bool negate : 1;
209 bool full : 1; /* 0 = half, 1 = full */
210 unsigned component : 3;
211 }
212 midgard_scalar_alu_src;
213
214 typedef struct
215 __attribute__((__packed__))
216 {
217 midgard_alu_op op : 8;
218 unsigned src1 : 6;
219 unsigned src2 : 11;
220 unsigned unknown : 1;
221 midgard_outmod outmod : 2;
222 bool output_full : 1;
223 unsigned output_component : 3;
224 }
225 midgard_scalar_alu;
226
227 typedef struct
228 __attribute__((__packed__))
229 {
230 unsigned src1_reg : 5;
231 unsigned src2_reg : 5;
232 unsigned out_reg : 5;
233 bool src2_imm : 1;
234 }
235 midgard_reg_info;
236
237 /* In addition to conditional branches and jumps (unconditional branches),
238 * Midgard implements a bit of fixed function functionality used in fragment
239 * shaders via specially crafted branches. These have special branch opcodes,
240 * which perform a fixed-function operation and/or use the results of a
241 * fixed-function operation as the branch condition. */
242
243 typedef enum {
244 /* Regular branches */
245 midgard_jmp_writeout_op_branch_uncond = 1,
246 midgard_jmp_writeout_op_branch_cond = 2,
247
248 /* In a fragment shader, execute a discard_if instruction, with the
249 * corresponding condition code. Terminates the shader, so generally
250 * set the branch target to out of the shader */
251 midgard_jmp_writeout_op_discard = 4,
252
253 /* Branch if the tilebuffer is not yet ready. At the beginning of a
254 * fragment shader that reads from the tile buffer, for instance via
255 * ARM_shader_framebuffer_fetch or EXT_pixel_local_storage, this branch
256 * operation should be used as a loop. An instruction like
257 * "br.tilebuffer.always -1" does the trick, corresponding to
258 * "while(!is_tilebuffer_ready) */
259 midgard_jmp_writeout_op_tilebuffer_pending = 6,
260
261 /* In a fragment shader, try to write out the value pushed to r0 to the
262 * tilebuffer, subject to unknown state in r1.z and r1.w. If this
263 * succeeds, the shader terminates. If it fails, it branches to the
264 * specified branch target. Generally, this should be used in a loop to
265 * itself, acting as "do { write(r0); } while(!write_successful);" */
266 midgard_jmp_writeout_op_writeout = 7,
267 } midgard_jmp_writeout_op;
268
269 typedef enum {
270 midgard_condition_write0 = 0,
271
272 /* These condition codes denote a conditional branch on FALSE and on
273 * TRUE respectively */
274 midgard_condition_false = 1,
275 midgard_condition_true = 2,
276
277 /* This condition code always branches. For a pure branch, the
278 * unconditional branch coding should be used instead, but for
279 * fixed-function branch opcodes, this is still useful */
280 midgard_condition_always = 3,
281 } midgard_condition;
282
283 typedef struct
284 __attribute__((__packed__))
285 {
286 midgard_jmp_writeout_op op : 3; /* == branch_uncond */
287 unsigned dest_tag : 4; /* tag of branch destination */
288 unsigned unknown : 2;
289 int offset : 7;
290 }
291 midgard_branch_uncond;
292
293 typedef struct
294 __attribute__((__packed__))
295 {
296 midgard_jmp_writeout_op op : 3; /* == branch_cond */
297 unsigned dest_tag : 4; /* tag of branch destination */
298 int offset : 7;
299 midgard_condition cond : 2;
300 }
301 midgard_branch_cond;
302
303 typedef struct
304 __attribute__((__packed__))
305 {
306 midgard_jmp_writeout_op op : 3; /* == branch_cond */
307 unsigned dest_tag : 4; /* tag of branch destination */
308 unsigned unknown : 2;
309 signed offset : 23;
310 unsigned cond : 16;
311 }
312 midgard_branch_extended;
313
314 typedef struct
315 __attribute__((__packed__))
316 {
317 midgard_jmp_writeout_op op : 3; /* == writeout */
318 unsigned unknown : 13;
319 }
320 midgard_writeout;
321
322 /*
323 * Load/store words
324 */
325
326 typedef enum {
327 midgard_op_ld_st_noop = 0x03,
328
329 /* Unclear why this is on the L/S unit, but (with an address of 0,
330 * appropriate swizzle, magic constant 0x24, and xy mask?) moves fp32 cube
331 * map coordinates in r27 to its cube map texture coordinate
332 * destination (e.g r29). 0x4 magic for loading from fp16 instead */
333
334 midgard_op_store_cubemap_coords = 0x0E,
335
336 midgard_op_load_attr_16 = 0x95,
337 midgard_op_load_attr_32 = 0x94,
338 midgard_op_load_vary_16 = 0x99,
339 midgard_op_load_vary_32 = 0x98,
340 midgard_op_load_color_buffer_16 = 0x9D,
341 midgard_op_load_color_buffer_8 = 0xBA,
342 midgard_op_load_uniform_16 = 0xAC,
343 midgard_op_load_uniform_32 = 0xB0,
344 midgard_op_store_vary_16 = 0xD5,
345 midgard_op_store_vary_32 = 0xD4
346 } midgard_load_store_op;
347
348 typedef enum {
349 midgard_interp_centroid = 1,
350 midgard_interp_default = 2
351 } midgard_interpolation;
352
353 typedef struct
354 __attribute__((__packed__))
355 {
356 unsigned zero1 : 4; /* Always zero */
357
358 /* Varying qualifiers, zero if not a varying */
359 unsigned flat : 1;
360 unsigned is_varying : 1; /* Always one for varying, but maybe something else? */
361 midgard_interpolation interpolation : 2;
362
363 unsigned zero2 : 2; /* Always zero */
364 }
365 midgard_varying_parameter;
366
367 typedef struct
368 __attribute__((__packed__))
369 {
370 midgard_load_store_op op : 8;
371 unsigned reg : 5;
372 unsigned mask : 4;
373 unsigned swizzle : 8;
374 unsigned unknown : 16;
375
376 unsigned varying_parameters : 10;
377
378 unsigned address : 9;
379 }
380 midgard_load_store_word;
381
382 typedef struct
383 __attribute__((__packed__))
384 {
385 unsigned type : 4;
386 unsigned next_type : 4;
387 uint64_t word1 : 60;
388 uint64_t word2 : 60;
389 }
390 midgard_load_store;
391
392 /* Texture pipeline results are in r28-r29 */
393 #define REG_TEX_BASE 28
394
395 /* Texture opcodes... maybe? */
396 #define TEXTURE_OP_NORMAL 0x11
397 #define TEXTURE_OP_TEXEL_FETCH 0x14
398
399 /* Texture format types, found in format */
400 #define TEXTURE_CUBE 0x00
401 #define TEXTURE_2D 0x02
402 #define TEXTURE_3D 0x03
403
404 typedef struct
405 __attribute__((__packed__))
406 {
407 unsigned type : 4;
408 unsigned next_type : 4;
409
410 unsigned op : 6;
411 unsigned shadow : 1;
412 unsigned unknown3 : 1;
413
414 /* A little obscure, but last is set for the last texture operation in
415 * a shader. cont appears to just be last's opposite (?). Yeah, I know,
416 * kind of funky.. BiOpen thinks it could do with memory hinting, or
417 * tile locking? */
418
419 unsigned cont : 1;
420 unsigned last : 1;
421
422 unsigned format : 5;
423 unsigned has_offset : 1;
424
425 /* Like in Bifrost */
426 unsigned filter : 1;
427
428 unsigned in_reg_select : 1;
429 unsigned in_reg_upper : 1;
430
431 unsigned in_reg_swizzle_left : 2;
432 unsigned in_reg_swizzle_right : 2;
433
434 unsigned unknown1 : 2;
435
436 unsigned unknown8 : 4;
437
438 unsigned out_full : 1;
439
440 /* Always 1 afaict... */
441 unsigned unknown7 : 2;
442
443 unsigned out_reg_select : 1;
444 unsigned out_upper : 1;
445
446 unsigned mask : 4;
447
448 unsigned unknown2 : 2;
449
450 unsigned swizzle : 8;
451 unsigned unknown4 : 8;
452
453 unsigned unknownA : 4;
454
455 unsigned offset_unknown1 : 1;
456 unsigned offset_reg_select : 1;
457 unsigned offset_reg_upper : 1;
458 unsigned offset_unknown4 : 1;
459 unsigned offset_unknown5 : 1;
460 unsigned offset_unknown6 : 1;
461 unsigned offset_unknown7 : 1;
462 unsigned offset_unknown8 : 1;
463 unsigned offset_unknown9 : 1;
464
465 unsigned unknownB : 3;
466
467 /* Texture bias or LOD, depending on whether it is executed in a
468 * fragment/vertex shader respectively. Compute as int(2^8 * biasf).
469 *
470 * For texel fetch, this is the LOD as is. */
471 unsigned bias : 8;
472
473 unsigned unknown9 : 8;
474
475 unsigned texture_handle : 16;
476 unsigned sampler_handle : 16;
477 }
478 midgard_texture_word;
479
480 /* Opcode name table */
481
482 static char *alu_opcode_names[256] = {
483 [midgard_alu_op_fadd] = "fadd",
484 [midgard_alu_op_fmul] = "fmul",
485 [midgard_alu_op_fmin] = "fmin",
486 [midgard_alu_op_fmax] = "fmax",
487 [midgard_alu_op_fmov] = "fmov",
488 [midgard_alu_op_froundeven] = "froundeven",
489 [midgard_alu_op_ftrunc] = "ftrunc",
490 [midgard_alu_op_ffloor] = "ffloor",
491 [midgard_alu_op_fceil] = "fceil",
492 [midgard_alu_op_ffma] = "ffma",
493 [midgard_alu_op_fdot3] = "fdot3",
494 [midgard_alu_op_fdot3r] = "fdot3r",
495 [midgard_alu_op_fdot4] = "fdot4",
496 [midgard_alu_op_freduce] = "freduce",
497 [midgard_alu_op_imin] = "imin",
498 [midgard_alu_op_umin] = "umin",
499 [midgard_alu_op_imax] = "imax",
500 [midgard_alu_op_umax] = "umax",
501 [midgard_alu_op_ishl] = "ishl",
502 [midgard_alu_op_iasr] = "iasr",
503 [midgard_alu_op_ilsr] = "ilsr",
504 [midgard_alu_op_iadd] = "iadd",
505 [midgard_alu_op_ishladd] = "ishladd",
506 [midgard_alu_op_isub] = "isub",
507 [midgard_alu_op_imul] = "imul",
508 [midgard_alu_op_imov] = "imov",
509 [midgard_alu_op_iabs] = "iabs",
510 [midgard_alu_op_iand] = "iand",
511 [midgard_alu_op_ior] = "ior",
512 [midgard_alu_op_inot] = "inot",
513 [midgard_alu_op_iandnot] = "iandnot",
514 [midgard_alu_op_ixor] = "ixor",
515 [midgard_alu_op_ilzcnt] = "ilzcnt",
516 [midgard_alu_op_ibitcount8] = "ibitcount8",
517 [midgard_alu_op_feq] = "feq",
518 [midgard_alu_op_fne] = "fne",
519 [midgard_alu_op_flt] = "flt",
520 [midgard_alu_op_fle] = "fle",
521 [midgard_alu_op_fball_eq] = "fball_eq",
522 [midgard_alu_op_fbany_neq] = "fbany_neq",
523 [midgard_alu_op_bball_eq] = "bball_eq",
524 [midgard_alu_op_fball_lt] = "fball_lt",
525 [midgard_alu_op_fball_lte] = "fball_lte",
526 [midgard_alu_op_bbany_neq] = "bbany_neq",
527 [midgard_alu_op_fbany_lt] = "fbany_lt",
528 [midgard_alu_op_fbany_lte] = "fbany_lte",
529 [midgard_alu_op_f2i] = "f2i",
530 [midgard_alu_op_f2u] = "f2u",
531 [midgard_alu_op_f2u8] = "f2u8",
532 [midgard_alu_op_ieq] = "ieq",
533 [midgard_alu_op_ine] = "ine",
534 [midgard_alu_op_ult] = "ult",
535 [midgard_alu_op_ule] = "ule",
536 [midgard_alu_op_ilt] = "ilt",
537 [midgard_alu_op_ile] = "ile",
538 [midgard_alu_op_iball_eq] = "iball_eq",
539 [midgard_alu_op_ball] = "ball",
540 [midgard_alu_op_uball_lt] = "uball_lt",
541 [midgard_alu_op_uball_lte] = "uball_lte",
542 [midgard_alu_op_iball_lt] = "iball_lt",
543 [midgard_alu_op_iball_lte] = "iball_lte",
544 [midgard_alu_op_iball_eq] = "iball_eq",
545 [midgard_alu_op_ibany_neq] = "ibany_neq",
546 [midgard_alu_op_ubany_lt] = "ubany_lt",
547 [midgard_alu_op_ubany_lte] = "ubany_lte",
548 [midgard_alu_op_ibany_lt] = "ibany_lt",
549 [midgard_alu_op_ibany_lte] = "ibany_lte",
550 [midgard_alu_op_i2f] = "i2f",
551 [midgard_alu_op_u2f] = "u2f",
552 [midgard_alu_op_icsel] = "icsel",
553 [midgard_alu_op_fcsel_i] = "fcsel_i",
554 [midgard_alu_op_fcsel] = "fcsel",
555 [midgard_alu_op_fround] = "fround",
556 [midgard_alu_op_fatan_pt2] = "fatan_pt2",
557 [midgard_alu_op_frcp] = "frcp",
558 [midgard_alu_op_frsqrt] = "frsqrt",
559 [midgard_alu_op_fsqrt] = "fsqrt",
560 [midgard_alu_op_fpow_pt1] = "fpow_pt1",
561 [midgard_alu_op_fexp2] = "fexp2",
562 [midgard_alu_op_flog2] = "flog2",
563 [midgard_alu_op_fsin] = "fsin",
564 [midgard_alu_op_fcos] = "fcos",
565 [midgard_alu_op_fatan2_pt1] = "fatan2_pt1"
566 };
567
568 static char *load_store_opcode_names[256] = {
569 [midgard_op_store_cubemap_coords] = "st_cubemap_coords",
570 [midgard_op_load_attr_16] = "ld_attr_16",
571 [midgard_op_load_attr_32] = "ld_attr_32",
572 [midgard_op_load_vary_16] = "ld_vary_16",
573 [midgard_op_load_vary_32] = "ld_vary_32",
574 [midgard_op_load_uniform_16] = "ld_uniform_16",
575 [midgard_op_load_uniform_32] = "ld_uniform_32",
576 [midgard_op_load_color_buffer_8] = "ld_color_buffer_8",
577 [midgard_op_load_color_buffer_16] = "ld_color_buffer_16",
578 [midgard_op_store_vary_16] = "st_vary_16",
579 [midgard_op_store_vary_32] = "st_vary_32"
580 };
581
582 #endif