4 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* Some constants and macros not found in the disassembler */
27 #define OP_IS_STORE(op) (\
28 op == midgard_op_store_vary_16 || \
29 op == midgard_op_store_vary_32 \
32 /* ALU control words are single bit fields with a lot of space */
34 #define ALU_ENAB_VEC_MUL (1 << 17)
35 #define ALU_ENAB_SCAL_ADD (1 << 19)
36 #define ALU_ENAB_VEC_ADD (1 << 21)
37 #define ALU_ENAB_SCAL_MUL (1 << 23)
38 #define ALU_ENAB_VEC_LUT (1 << 25)
39 #define ALU_ENAB_BR_COMPACT (1 << 26)
40 #define ALU_ENAB_BRANCH (1 << 27)
42 /* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */
44 /* Denotes an opcode that takes a vector input with a fixed-number of
45 * channels, but outputs to only a single output channel, like dot products.
46 * For these, to determine the effective mask, this quirk can be set. We have
47 * an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no
48 * sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't
49 * make sense (since then why are we quirked?), so that corresponds to "no
52 #define OP_CHANNEL_COUNT(c) ((c - 1) << 0)
53 #define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)
55 /* For instructions that take a single argument, normally the first argument
56 * slot is used for the argument and the second slot is a dummy #0 constant.
57 * However, there are exceptions: instructions like fmov store their argument
58 * in the _second_ slot and store a dummy r24 in the first slot, designated by
59 * QUIRK_FLIPPED_R24 */
61 #define QUIRK_FLIPPED_R24 (1 << 2)
63 /* Vector-independant shorthands for the above; these numbers are arbitrary and
64 * not from the ISA. Convert to the above with unit_enum_to_midgard */
72 #define TAG_TEXTURE_4 0x3
73 #define TAG_LOAD_STORE_4 0x5
76 #define TAG_ALU_12 0xA
77 #define TAG_ALU_16 0xB
79 /* Special register aliases */
81 #define MAX_WORK_REGISTERS 16
83 /* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */
84 #define REGISTER_UNIFORMS 24
86 #define REGISTER_UNUSED 24
87 #define REGISTER_CONSTANT 26
88 #define REGISTER_VARYING_BASE 26
89 #define REGISTER_OFFSET 27
90 #define REGISTER_TEXTURE_BASE 28
91 #define REGISTER_SELECT 31
93 /* Special uniforms used for e.g. vertex epilogues */
94 #define SPECIAL_UNIFORM_BASE (1 << 24)
95 #define UNIFORM_VIEWPORT (SPECIAL_UNIFORM_BASE + 0)
97 /* SSA helper aliases to mimic the registers. UNUSED_0 encoded as an inline
98 * constant. UNUSED_1 encoded as REGISTER_UNUSED */
100 #define SSA_UNUSED_0 0
101 #define SSA_UNUSED_1 -2
103 #define SSA_FIXED_SHIFT 24
104 #define SSA_FIXED_REGISTER(reg) ((1 + reg) << SSA_FIXED_SHIFT)
105 #define SSA_REG_FROM_FIXED(reg) ((reg >> SSA_FIXED_SHIFT) - 1)
106 #define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)
108 /* Swizzle support */
110 #define SWIZZLE(A, B, C, D) ((D << 6) | (C << 4) | (B << 2) | (A << 0))
111 #define SWIZZLE_FROM_ARRAY(r) SWIZZLE(r[0], r[1], r[2], r[3])
112 #define COMPONENT_X 0x0
113 #define COMPONENT_Y 0x1
114 #define COMPONENT_Z 0x2
115 #define COMPONENT_W 0x3
121 /* Is this opcode that of an integer? */
123 midgard_is_integer_op(int op
)
126 case midgard_alu_op_iadd
:
127 case midgard_alu_op_ishladd
:
128 case midgard_alu_op_isub
:
129 case midgard_alu_op_imul
:
130 case midgard_alu_op_imin
:
131 case midgard_alu_op_imax
:
132 case midgard_alu_op_iasr
:
133 case midgard_alu_op_ilsr
:
134 case midgard_alu_op_ishl
:
135 case midgard_alu_op_iand
:
136 case midgard_alu_op_ior
:
137 case midgard_alu_op_inot
:
138 case midgard_alu_op_iandnot
:
139 case midgard_alu_op_ixor
:
140 case midgard_alu_op_imov
:
142 //case midgard_alu_op_f2i:
143 //case midgard_alu_op_f2u:
144 case midgard_alu_op_ieq
:
145 case midgard_alu_op_ine
:
146 case midgard_alu_op_ilt
:
147 case midgard_alu_op_ile
:
148 case midgard_alu_op_iball_eq
:
149 case midgard_alu_op_ibany_neq
:
151 //case midgard_alu_op_i2f:
152 //case midgard_alu_op_u2f:
153 case midgard_alu_op_icsel
:
161 /* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is
162 * implemented on some subset of these units (or occassionally all of them).
163 * This table encodes a bit mask of valid units for each opcode, so the
164 * scheduler can figure where to plonk the instruction. */
166 /* Shorthands for each unit */
167 #define UNIT_VMUL ALU_ENAB_VEC_MUL
168 #define UNIT_SADD ALU_ENAB_SCAL_ADD
169 #define UNIT_VADD ALU_ENAB_VEC_ADD
170 #define UNIT_SMUL ALU_ENAB_SCAL_MUL
171 #define UNIT_VLUT ALU_ENAB_VEC_LUT
173 /* Shorthands for usual combinations of units */
175 #define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)
176 #define UNITS_ADD (UNIT_VADD | UNIT_SADD)
177 #define UNITS_MOST (UNITS_MUL | UNITS_ADD)
178 #define UNITS_ALL (UNITS_MOST | UNIT_VLUT)
179 #define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)
180 #define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)
181 #define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)
183 static unsigned alu_opcode_props
[256] = {
184 [midgard_alu_op_fadd
] = UNITS_ADD
,
185 [midgard_alu_op_fmul
] = UNITS_MUL
| UNIT_VLUT
,
186 [midgard_alu_op_fmin
] = UNITS_MUL
| UNITS_ADD
,
187 [midgard_alu_op_fmax
] = UNITS_MUL
| UNITS_ADD
,
188 [midgard_alu_op_imin
] = UNITS_MOST
,
189 [midgard_alu_op_imax
] = UNITS_MOST
,
190 [midgard_alu_op_fmov
] = UNITS_ALL
| QUIRK_FLIPPED_R24
,
191 [midgard_alu_op_ffloor
] = UNITS_ADD
,
192 [midgard_alu_op_fceil
] = UNITS_ADD
,
194 /* Though they output a scalar, they need to run on a vector unit
195 * since they process vectors */
196 [midgard_alu_op_fdot3
] = UNIT_VMUL
| OP_CHANNEL_COUNT(3),
197 [midgard_alu_op_fdot4
] = UNIT_VMUL
| OP_CHANNEL_COUNT(4),
199 /* Incredibly, iadd can run on vmul, etc */
200 [midgard_alu_op_iadd
] = UNITS_MOST
,
201 [midgard_alu_op_isub
] = UNITS_MOST
,
202 [midgard_alu_op_imul
] = UNITS_MOST
,
203 [midgard_alu_op_imov
] = UNITS_MOST
| QUIRK_FLIPPED_R24
,
205 /* For vector comparisons, use ball etc */
206 [midgard_alu_op_feq
] = UNITS_MOST
,
207 [midgard_alu_op_fne
] = UNITS_MOST
,
208 [midgard_alu_op_flt
] = UNIT_SADD
,
209 [midgard_alu_op_ieq
] = UNITS_MOST
,
210 [midgard_alu_op_ine
] = UNITS_MOST
,
211 [midgard_alu_op_ilt
] = UNITS_MOST
,
212 [midgard_alu_op_ile
] = UNITS_MOST
,
214 [midgard_alu_op_icsel
] = UNITS_ADD
,
215 [midgard_alu_op_fcsel
] = UNITS_ADD
| UNIT_SMUL
,
217 [midgard_alu_op_frcp
] = UNIT_VLUT
,
218 [midgard_alu_op_frsqrt
] = UNIT_VLUT
,
219 [midgard_alu_op_fsqrt
] = UNIT_VLUT
,
220 [midgard_alu_op_fexp2
] = UNIT_VLUT
,
221 [midgard_alu_op_flog2
] = UNIT_VLUT
,
223 [midgard_alu_op_f2i
] = UNITS_ADD
,
224 [midgard_alu_op_f2u
] = UNITS_ADD
,
225 [midgard_alu_op_f2u8
] = UNITS_ADD
,
226 [midgard_alu_op_i2f
] = UNITS_ADD
,
227 [midgard_alu_op_u2f
] = UNITS_ADD
,
229 [midgard_alu_op_fsin
] = UNIT_VLUT
,
230 [midgard_alu_op_fcos
] = UNIT_VLUT
,
232 [midgard_alu_op_iand
] = UNITS_ADD
, /* XXX: Test case where it's right on smul but not sadd */
233 [midgard_alu_op_ior
] = UNITS_ADD
,
234 [midgard_alu_op_ixor
] = UNITS_ADD
,
235 [midgard_alu_op_inot
] = UNITS_MOST
,
236 [midgard_alu_op_ishl
] = UNITS_ADD
,
237 [midgard_alu_op_iasr
] = UNITS_ADD
,
238 [midgard_alu_op_ilsr
] = UNITS_ADD
,
239 [midgard_alu_op_ilsr
] = UNITS_ADD
,
241 [midgard_alu_op_fball_eq
] = UNITS_MOST
,
242 [midgard_alu_op_fbany_neq
] = UNITS_MOST
,
243 [midgard_alu_op_iball_eq
] = UNITS_MOST
,
244 [midgard_alu_op_ibany_neq
] = UNITS_MOST