2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #define PACKED __attribute__((__packed__))
34 /* size of largest OPC field of all the instruction categories: */
37 #define _OPC(cat, opc) (((cat) << NOPC_BITS) | opc)
43 OPC_JUMP
= _OPC(0, 2),
44 OPC_CALL
= _OPC(0, 3),
46 OPC_KILL
= _OPC(0, 5),
48 OPC_EMIT
= _OPC(0, 7),
50 OPC_CHMASK
= _OPC(0, 9),
51 OPC_CHSH
= _OPC(0, 10),
52 OPC_FLOW_REV
= _OPC(0, 11),
58 OPC_ADD_F
= _OPC(2, 0),
59 OPC_MIN_F
= _OPC(2, 1),
60 OPC_MAX_F
= _OPC(2, 2),
61 OPC_MUL_F
= _OPC(2, 3),
62 OPC_SIGN_F
= _OPC(2, 4),
63 OPC_CMPS_F
= _OPC(2, 5),
64 OPC_ABSNEG_F
= _OPC(2, 6),
65 OPC_CMPV_F
= _OPC(2, 7),
67 OPC_FLOOR_F
= _OPC(2, 9),
68 OPC_CEIL_F
= _OPC(2, 10),
69 OPC_RNDNE_F
= _OPC(2, 11),
70 OPC_RNDAZ_F
= _OPC(2, 12),
71 OPC_TRUNC_F
= _OPC(2, 13),
73 OPC_ADD_U
= _OPC(2, 16),
74 OPC_ADD_S
= _OPC(2, 17),
75 OPC_SUB_U
= _OPC(2, 18),
76 OPC_SUB_S
= _OPC(2, 19),
77 OPC_CMPS_U
= _OPC(2, 20),
78 OPC_CMPS_S
= _OPC(2, 21),
79 OPC_MIN_U
= _OPC(2, 22),
80 OPC_MIN_S
= _OPC(2, 23),
81 OPC_MAX_U
= _OPC(2, 24),
82 OPC_MAX_S
= _OPC(2, 25),
83 OPC_ABSNEG_S
= _OPC(2, 26),
85 OPC_AND_B
= _OPC(2, 28),
86 OPC_OR_B
= _OPC(2, 29),
87 OPC_NOT_B
= _OPC(2, 30),
88 OPC_XOR_B
= _OPC(2, 31),
90 OPC_CMPV_U
= _OPC(2, 33),
91 OPC_CMPV_S
= _OPC(2, 34),
93 OPC_MUL_U
= _OPC(2, 48),
94 OPC_MUL_S
= _OPC(2, 49),
95 OPC_MULL_U
= _OPC(2, 50),
96 OPC_BFREV_B
= _OPC(2, 51),
97 OPC_CLZ_S
= _OPC(2, 52),
98 OPC_CLZ_B
= _OPC(2, 53),
99 OPC_SHL_B
= _OPC(2, 54),
100 OPC_SHR_B
= _OPC(2, 55),
101 OPC_ASHR_B
= _OPC(2, 56),
102 OPC_BARY_F
= _OPC(2, 57),
103 OPC_MGEN_B
= _OPC(2, 58),
104 OPC_GETBIT_B
= _OPC(2, 59),
105 OPC_SETRM
= _OPC(2, 60),
106 OPC_CBITS_B
= _OPC(2, 61),
107 OPC_SHB
= _OPC(2, 62),
108 OPC_MSAD
= _OPC(2, 63),
111 OPC_MAD_U16
= _OPC(3, 0),
112 OPC_MADSH_U16
= _OPC(3, 1),
113 OPC_MAD_S16
= _OPC(3, 2),
114 OPC_MADSH_M16
= _OPC(3, 3), /* should this be .s16? */
115 OPC_MAD_U24
= _OPC(3, 4),
116 OPC_MAD_S24
= _OPC(3, 5),
117 OPC_MAD_F16
= _OPC(3, 6),
118 OPC_MAD_F32
= _OPC(3, 7),
119 OPC_SEL_B16
= _OPC(3, 8),
120 OPC_SEL_B32
= _OPC(3, 9),
121 OPC_SEL_S16
= _OPC(3, 10),
122 OPC_SEL_S32
= _OPC(3, 11),
123 OPC_SEL_F16
= _OPC(3, 12),
124 OPC_SEL_F32
= _OPC(3, 13),
125 OPC_SAD_S16
= _OPC(3, 14),
126 OPC_SAD_S32
= _OPC(3, 15),
129 OPC_RCP
= _OPC(4, 0),
130 OPC_RSQ
= _OPC(4, 1),
131 OPC_LOG2
= _OPC(4, 2),
132 OPC_EXP2
= _OPC(4, 3),
133 OPC_SIN
= _OPC(4, 4),
134 OPC_COS
= _OPC(4, 5),
135 OPC_SQRT
= _OPC(4, 6),
139 OPC_ISAM
= _OPC(5, 0),
140 OPC_ISAML
= _OPC(5, 1),
141 OPC_ISAMM
= _OPC(5, 2),
142 OPC_SAM
= _OPC(5, 3),
143 OPC_SAMB
= _OPC(5, 4),
144 OPC_SAML
= _OPC(5, 5),
145 OPC_SAMGQ
= _OPC(5, 6),
146 OPC_GETLOD
= _OPC(5, 7),
147 OPC_CONV
= _OPC(5, 8),
148 OPC_CONVM
= _OPC(5, 9),
149 OPC_GETSIZE
= _OPC(5, 10),
150 OPC_GETBUF
= _OPC(5, 11),
151 OPC_GETPOS
= _OPC(5, 12),
152 OPC_GETINFO
= _OPC(5, 13),
153 OPC_DSX
= _OPC(5, 14),
154 OPC_DSY
= _OPC(5, 15),
155 OPC_GATHER4R
= _OPC(5, 16),
156 OPC_GATHER4G
= _OPC(5, 17),
157 OPC_GATHER4B
= _OPC(5, 18),
158 OPC_GATHER4A
= _OPC(5, 19),
159 OPC_SAMGP0
= _OPC(5, 20),
160 OPC_SAMGP1
= _OPC(5, 21),
161 OPC_SAMGP2
= _OPC(5, 22),
162 OPC_SAMGP3
= _OPC(5, 23),
163 OPC_DSXPP_1
= _OPC(5, 24),
164 OPC_DSYPP_1
= _OPC(5, 25),
165 OPC_RGETPOS
= _OPC(5, 26),
166 OPC_RGETINFO
= _OPC(5, 27),
169 OPC_LDG
= _OPC(6, 0), /* load-global */
170 OPC_LDL
= _OPC(6, 1),
171 OPC_LDP
= _OPC(6, 2),
172 OPC_STG
= _OPC(6, 3), /* store-global */
173 OPC_STL
= _OPC(6, 4),
174 OPC_STP
= _OPC(6, 5),
175 OPC_LDIB
= _OPC(6, 6),
176 OPC_G2L
= _OPC(6, 7),
177 OPC_L2G
= _OPC(6, 8),
178 OPC_PREFETCH
= _OPC(6, 9),
179 OPC_LDLW
= _OPC(6, 10),
180 OPC_STLW
= _OPC(6, 11),
181 OPC_RESFMT
= _OPC(6, 14),
182 OPC_RESINFO
= _OPC(6, 15),
183 OPC_ATOMIC_ADD
= _OPC(6, 16),
184 OPC_ATOMIC_SUB
= _OPC(6, 17),
185 OPC_ATOMIC_XCHG
= _OPC(6, 18),
186 OPC_ATOMIC_INC
= _OPC(6, 19),
187 OPC_ATOMIC_DEC
= _OPC(6, 20),
188 OPC_ATOMIC_CMPXCHG
= _OPC(6, 21),
189 OPC_ATOMIC_MIN
= _OPC(6, 22),
190 OPC_ATOMIC_MAX
= _OPC(6, 23),
191 OPC_ATOMIC_AND
= _OPC(6, 24),
192 OPC_ATOMIC_OR
= _OPC(6, 25),
193 OPC_ATOMIC_XOR
= _OPC(6, 26),
194 OPC_LDGB
= _OPC(6, 27),
195 OPC_STGB
= _OPC(6, 28),
196 OPC_STIB
= _OPC(6, 29),
197 OPC_LDC
= _OPC(6, 30),
198 OPC_LDLV
= _OPC(6, 31),
201 OPC_BAR
= _OPC(7, 0),
202 OPC_FENCE
= _OPC(7, 1),
204 /* meta instructions (category -1): */
205 /* placeholder instr to mark shader inputs: */
206 OPC_META_INPUT
= _OPC(-1, 0),
207 /* The "fan-in" and "fan-out" instructions are used for keeping
208 * track of instructions that write to multiple dst registers
209 * (fan-out) like texture sample instructions, or read multiple
210 * consecutive scalar registers (fan-in) (bary.f, texture samp)
212 OPC_META_FO
= _OPC(-1, 2),
213 OPC_META_FI
= _OPC(-1, 3),
215 /* placeholder for texture fetches that run before FS invocation
218 OPC_META_TEX_PREFETCH
= _OPC(-1, 4),
222 #define opc_cat(opc) ((int)((opc) >> NOPC_BITS))
223 #define opc_op(opc) ((unsigned)((opc) & ((1 << NOPC_BITS) - 1)))
233 TYPE_S8
= 7, // XXX I assume?
236 static inline uint32_t type_size(type_t type
)
251 assert(0); /* invalid type */
256 static inline int type_float(type_t type
)
258 return (type
== TYPE_F32
) || (type
== TYPE_F16
);
261 static inline int type_uint(type_t type
)
263 return (type
== TYPE_U32
) || (type
== TYPE_U16
) || (type
== TYPE_U8
);
266 static inline int type_sint(type_t type
)
268 return (type
== TYPE_S32
) || (type
== TYPE_S16
) || (type
== TYPE_S8
);
271 typedef union PACKED
{
272 /* normal gpr or const src register: */
277 /* for immediate val: */
278 int32_t iim_val
: 11;
279 /* to make compiler happy: */
281 uint32_t dummy10
: 10;
282 int32_t idummy10
: 10;
283 uint32_t dummy11
: 11;
284 uint32_t dummy12
: 12;
285 uint32_t dummy13
: 13;
289 /* special registers: */
290 #define REG_A0 61 /* address register */
291 #define REG_P0 62 /* predicate register */
293 static inline int reg_special(reg_t reg
)
295 return (reg
.num
== REG_A0
) || (reg
.num
== REG_P0
);
298 typedef struct PACKED
{
303 uint32_t dummy1
: 16;
307 uint32_t dummy1
: 12;
323 uint32_t jmp_tgt
: 1;
325 uint32_t opc_cat
: 3;
328 typedef struct PACKED
{
331 /* for normal src register: */
334 /* at least low bit of pad must be zero or it will
335 * look like a address relative src
339 /* for address relative: */
342 uint32_t src_rel_c
: 1;
343 uint32_t src_rel
: 1;
344 uint32_t unknown
: 20;
358 uint32_t dst_type
: 3;
359 uint32_t dst_rel
: 1;
360 uint32_t src_type
: 3;
364 uint32_t pos_inf
: 1;
365 uint32_t must_be_0
: 2;
366 uint32_t jmp_tgt
: 1;
368 uint32_t opc_cat
: 3;
371 typedef struct PACKED
{
376 uint32_t must_be_zero1
: 2;
377 uint32_t src1_im
: 1; /* immediate */
378 uint32_t src1_neg
: 1; /* negate */
379 uint32_t src1_abs
: 1; /* absolute value */
383 uint32_t src1_c
: 1; /* relative-const */
384 uint32_t src1_rel
: 1; /* relative address */
385 uint32_t must_be_zero
: 1;
390 uint32_t src1_c
: 1; /* const */
398 uint32_t must_be_zero2
: 2;
399 uint32_t src2_im
: 1; /* immediate */
400 uint32_t src2_neg
: 1; /* negate */
401 uint32_t src2_abs
: 1; /* absolute value */
405 uint32_t src2_c
: 1; /* relative-const */
406 uint32_t src2_rel
: 1; /* relative address */
407 uint32_t must_be_zero
: 1;
412 uint32_t src2_c
: 1; /* const */
421 uint32_t src1_r
: 1; /* doubles as nop0 if repeat==0 */
423 uint32_t ul
: 1; /* dunno */
424 uint32_t dst_half
: 1; /* or widen/narrow.. ie. dst hrN <-> rN */
427 uint32_t src2_r
: 1; /* doubles as nop1 if repeat==0 */
428 uint32_t full
: 1; /* not half */
430 uint32_t jmp_tgt
: 1;
432 uint32_t opc_cat
: 3;
435 typedef struct PACKED
{
440 uint32_t must_be_zero1
: 2;
442 uint32_t src1_neg
: 1;
443 uint32_t src2_r
: 1; /* doubles as nop1 if repeat==0 */
448 uint32_t src1_rel
: 1;
449 uint32_t must_be_zero
: 1;
462 uint32_t must_be_zero2
: 2;
464 uint32_t src2_neg
: 1;
465 uint32_t src3_neg
: 1;
470 uint32_t src3_rel
: 1;
471 uint32_t must_be_zero
: 1;
485 uint32_t src1_r
: 1; /* doubles as nop0 if repeat==0 */
488 uint32_t dst_half
: 1; /* or widen/narrow.. ie. dst hrN <-> rN */
491 uint32_t jmp_tgt
: 1;
493 uint32_t opc_cat
: 3;
496 static inline bool instr_cat3_full(instr_cat3_t
*cat3
)
498 switch (_OPC(3, cat3
->opc
)) {
506 case OPC_SAD_S32
: // really??
513 typedef struct PACKED
{
518 uint32_t must_be_zero1
: 2;
519 uint32_t src_im
: 1; /* immediate */
520 uint32_t src_neg
: 1; /* negate */
521 uint32_t src_abs
: 1; /* absolute value */
525 uint32_t src_c
: 1; /* relative-const */
526 uint32_t src_rel
: 1; /* relative address */
527 uint32_t must_be_zero
: 1;
532 uint32_t src_c
: 1; /* const */
536 uint32_t dummy1
: 16; /* seem to be ignored */
545 uint32_t dst_half
: 1; /* or widen/narrow.. ie. dst hrN <-> rN */
546 uint32_t dummy2
: 5; /* seem to be ignored */
547 uint32_t full
: 1; /* not half */
549 uint32_t jmp_tgt
: 1;
551 uint32_t opc_cat
: 3;
554 typedef struct PACKED
{
559 uint32_t full
: 1; /* not half */
562 uint32_t dummy1
: 4; /* seem to be ignored */
568 uint32_t full
: 1; /* not half */
575 /* same in either case: */
576 // XXX I think, confirm this
578 uint32_t full
: 1; /* not half */
586 uint32_t wrmask
: 4; /* write-mask */
588 uint32_t dummy2
: 1; /* seems to be ignored */
593 uint32_t is_s2en
: 1;
598 uint32_t jmp_tgt
: 1;
600 uint32_t opc_cat
: 3;
603 /* dword0 encoding for src_off: [src1 + off], src2: */
604 typedef struct PACKED
{
606 uint32_t mustbe1
: 1;
609 uint32_t src1_im
: 1;
610 uint32_t src2_im
: 1;
617 /* dword0 encoding for !src_off: [src1], src2 */
618 typedef struct PACKED
{
620 uint32_t mustbe0
: 1;
622 uint32_t ignore0
: 8;
623 uint32_t src1_im
: 1;
624 uint32_t src2_im
: 1;
631 /* dword1 encoding for dst_off: */
632 typedef struct PACKED
{
636 /* note: there is some weird stuff going on where sometimes
637 * cat6->a.off is involved.. but that seems like a bug in
638 * the blob, since it is used even if !cat6->src_off
639 * It would make sense for there to be some more bits to
640 * bring us to 11 bits worth of offset, but not sure..
643 uint32_t mustbe1
: 1;
648 /* dword1 encoding for !dst_off: */
649 typedef struct PACKED
{
654 uint32_t mustbe0
: 1;
659 /* ldgb and atomics..
661 * ldgb: pad0=0, pad3=1
662 * atomic .g: pad0=1, pad3=1
665 typedef struct PACKED
{
671 uint32_t type_size
: 2;
673 uint32_t src1_im
: 1;
674 uint32_t src2_im
: 1;
679 uint32_t mustbe0
: 1;
680 uint32_t src_ssbo
: 8;
681 uint32_t pad2
: 3; // type
684 uint32_t pad4
: 10; // opc/jmp_tgt/sync/opc_cat
687 /* stgb, pad0=0, pad3=2
689 typedef struct PACKED
{
691 uint32_t mustbe1
: 1; // ???
695 uint32_t type_size
: 2;
697 uint32_t src2_im
: 1;
702 uint32_t src3_im
: 1;
703 uint32_t dst_ssbo
: 8;
704 uint32_t pad2
: 3; // type
706 uint32_t pad4
: 10; // opc/jmp_tgt/sync/opc_cat
709 typedef union PACKED
{
714 instr_cat6ldgb_t ldgb
;
715 instr_cat6stgb_t stgb
;
718 uint32_t src_off
: 1;
723 uint32_t dst_off
: 1;
726 uint32_t g
: 1; /* or in some cases it means dst immed */
729 uint32_t jmp_tgt
: 1;
731 uint32_t opc_cat
: 3;
736 * For atomic ops (which return a value):
738 * pad1=1, pad2=c, pad3=0, pad4=3
739 * src1 - vecN offset/coords
740 * src2.x - is actually dest register
741 * src2.y - is 'data' except for cmpxchg where src2.y is 'compare'
742 * and src2.z is 'data'
744 * For stib (which does not return a value):
745 * pad1=0, pad2=c, pad3=0, pad4=2
746 * src1 - vecN offset/coords
747 * src2 - value to store
750 * pad1=1, pad2=c, pad3=0, pad4=2
751 * src1 - vecN offset/coords
753 * for ldc (load from UBO using descriptor):
754 * pad1=0, pad2=8, pad3=0, pad4=2
756 typedef struct PACKED
{
761 uint32_t type_size
: 2;
764 uint32_t src1
: 8; /* coordinate/offset */
767 uint32_t src2
: 8; /* or the dst for load instructions */
768 uint32_t pad3
: 1; //mustbe0 ?? or zero means imm vs reg for ssbo??
769 uint32_t ssbo
: 8; /* ssbo/image binding point */
772 uint32_t jmp_tgt
: 1;
774 uint32_t opc_cat
: 3;
777 typedef struct PACKED
{
783 uint32_t ss
: 1; /* maybe in the encoding, but blob only uses (sy) */
785 uint32_t w
: 1; /* write */
786 uint32_t r
: 1; /* read */
787 uint32_t l
: 1; /* local */
788 uint32_t g
: 1; /* global */
789 uint32_t opc
: 4; /* presumed, but only a couple known OPCs */
790 uint32_t jmp_tgt
: 1; /* (jp) */
791 uint32_t sync
: 1; /* (sy) */
792 uint32_t opc_cat
: 3;
795 typedef union PACKED
{
803 instr_cat6_a6xx_t cat6_a6xx
;
811 uint32_t ss
: 1; /* cat1-cat4 (cat0??) and cat7 (?) */
812 uint32_t ul
: 1; /* cat2-cat4 (and cat1 in blob.. which may be bug??) */
814 uint32_t jmp_tgt
: 1;
816 uint32_t opc_cat
: 3;
821 static inline uint32_t instr_repeat(instr_t
*instr
)
823 switch (instr
->opc_cat
) {
824 case 0: return instr
->cat0
.repeat
;
825 case 1: return instr
->cat1
.repeat
;
826 case 2: return instr
->cat2
.repeat
;
827 case 3: return instr
->cat3
.repeat
;
828 case 4: return instr
->cat4
.repeat
;
833 static inline bool instr_sat(instr_t
*instr
)
835 switch (instr
->opc_cat
) {
836 case 2: return instr
->cat2
.sat
;
837 case 3: return instr
->cat3
.sat
;
838 case 4: return instr
->cat4
.sat
;
839 default: return false;
843 /* We can probably drop the gpu_id arg, but keeping it for now so we can
844 * assert if we see something we think should be new encoding on an older
847 static inline bool is_cat6_legacy(instr_t
*instr
, unsigned gpu_id
)
849 instr_cat6_a6xx_t
*cat6
= &instr
->cat6_a6xx
;
851 /* At least one of these two bits is pad in all the possible
852 * "legacy" cat6 encodings, and a analysis of all the pre-a6xx
853 * cmdstream traces I have indicates that the pad bit is zero
854 * in all cases. So we can use this to detect new encoding:
856 if ((cat6
->pad2
& 0x8) && (cat6
->pad4
& 0x2)) {
857 assert(gpu_id
>= 600);
858 assert(instr
->cat6
.opc
== 0);
865 static inline uint32_t instr_opc(instr_t
*instr
, unsigned gpu_id
)
867 switch (instr
->opc_cat
) {
868 case 0: return instr
->cat0
.opc
;
870 case 2: return instr
->cat2
.opc
;
871 case 3: return instr
->cat3
.opc
;
872 case 4: return instr
->cat4
.opc
;
873 case 5: return instr
->cat5
.opc
;
875 if (!is_cat6_legacy(instr
, gpu_id
))
876 return instr
->cat6_a6xx
.opc
;
877 return instr
->cat6
.opc
;
878 case 7: return instr
->cat7
.opc
;
883 static inline bool is_mad(opc_t opc
)
898 static inline bool is_madsh(opc_t opc
)
909 static inline bool is_atomic(opc_t opc
)
914 case OPC_ATOMIC_XCHG
:
917 case OPC_ATOMIC_CMPXCHG
:
929 static inline bool is_ssbo(opc_t opc
)
943 static inline bool is_isam(opc_t opc
)
955 int disasm_a3xx(uint32_t *dwords
, int sizedwords
, int level
, FILE *out
, unsigned gpu_id
);
957 #endif /* INSTR_A3XX_H_ */