aco: gfx10_wave64_bpermute reduce op to print_ir
[mesa.git] / src / broadcom / qpu / qpu_pack.c
1 /*
2 * Copyright © 2016 Broadcom
3 *
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:
10 *
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
13 * Software.
14 *
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <string.h>
25 #include "util/macros.h"
26
27 #include "broadcom/common/v3d_device_info.h"
28 #include "qpu_instr.h"
29
30 #ifndef QPU_MASK
31 #define QPU_MASK(high, low) ((((uint64_t)1<<((high)-(low)+1))-1)<<(low))
32 /* Using the GNU statement expression extension */
33 #define QPU_SET_FIELD(value, field) \
34 ({ \
35 uint64_t fieldval = (uint64_t)(value) << field ## _SHIFT; \
36 assert((fieldval & ~ field ## _MASK) == 0); \
37 fieldval & field ## _MASK; \
38 })
39
40 #define QPU_GET_FIELD(word, field) ((uint32_t)(((word) & field ## _MASK) >> field ## _SHIFT))
41
42 #define QPU_UPDATE_FIELD(inst, value, field) \
43 (((inst) & ~(field ## _MASK)) | QPU_SET_FIELD(value, field))
44 #endif /* QPU_MASK */
45
46 #define VC5_QPU_OP_MUL_SHIFT 58
47 #define VC5_QPU_OP_MUL_MASK QPU_MASK(63, 58)
48
49 #define VC5_QPU_SIG_SHIFT 53
50 #define VC5_QPU_SIG_MASK QPU_MASK(57, 53)
51
52 #define VC5_QPU_COND_SHIFT 46
53 #define VC5_QPU_COND_MASK QPU_MASK(52, 46)
54 #define VC5_QPU_COND_SIG_MAGIC_ADDR (1 << 6)
55
56 #define VC5_QPU_MM QPU_MASK(45, 45)
57 #define VC5_QPU_MA QPU_MASK(44, 44)
58
59 #define V3D_QPU_WADDR_M_SHIFT 38
60 #define V3D_QPU_WADDR_M_MASK QPU_MASK(43, 38)
61
62 #define VC5_QPU_BRANCH_ADDR_LOW_SHIFT 35
63 #define VC5_QPU_BRANCH_ADDR_LOW_MASK QPU_MASK(55, 35)
64
65 #define V3D_QPU_WADDR_A_SHIFT 32
66 #define V3D_QPU_WADDR_A_MASK QPU_MASK(37, 32)
67
68 #define VC5_QPU_BRANCH_COND_SHIFT 32
69 #define VC5_QPU_BRANCH_COND_MASK QPU_MASK(34, 32)
70
71 #define VC5_QPU_BRANCH_ADDR_HIGH_SHIFT 24
72 #define VC5_QPU_BRANCH_ADDR_HIGH_MASK QPU_MASK(31, 24)
73
74 #define VC5_QPU_OP_ADD_SHIFT 24
75 #define VC5_QPU_OP_ADD_MASK QPU_MASK(31, 24)
76
77 #define VC5_QPU_MUL_B_SHIFT 21
78 #define VC5_QPU_MUL_B_MASK QPU_MASK(23, 21)
79
80 #define VC5_QPU_BRANCH_MSFIGN_SHIFT 21
81 #define VC5_QPU_BRANCH_MSFIGN_MASK QPU_MASK(22, 21)
82
83 #define VC5_QPU_MUL_A_SHIFT 18
84 #define VC5_QPU_MUL_A_MASK QPU_MASK(20, 18)
85
86 #define VC5_QPU_ADD_B_SHIFT 15
87 #define VC5_QPU_ADD_B_MASK QPU_MASK(17, 15)
88
89 #define VC5_QPU_BRANCH_BDU_SHIFT 15
90 #define VC5_QPU_BRANCH_BDU_MASK QPU_MASK(17, 15)
91
92 #define VC5_QPU_BRANCH_UB QPU_MASK(14, 14)
93
94 #define VC5_QPU_ADD_A_SHIFT 12
95 #define VC5_QPU_ADD_A_MASK QPU_MASK(14, 12)
96
97 #define VC5_QPU_BRANCH_BDI_SHIFT 12
98 #define VC5_QPU_BRANCH_BDI_MASK QPU_MASK(13, 12)
99
100 #define VC5_QPU_RADDR_A_SHIFT 6
101 #define VC5_QPU_RADDR_A_MASK QPU_MASK(11, 6)
102
103 #define VC5_QPU_RADDR_B_SHIFT 0
104 #define VC5_QPU_RADDR_B_MASK QPU_MASK(5, 0)
105
106 #define THRSW .thrsw = true
107 #define LDUNIF .ldunif = true
108 #define LDUNIFRF .ldunifrf = true
109 #define LDUNIFA .ldunifa = true
110 #define LDUNIFARF .ldunifarf = true
111 #define LDTMU .ldtmu = true
112 #define LDVARY .ldvary = true
113 #define LDVPM .ldvpm = true
114 #define SMIMM .small_imm = true
115 #define LDTLB .ldtlb = true
116 #define LDTLBU .ldtlbu = true
117 #define UCB .ucb = true
118 #define ROT .rotate = true
119 #define WRTMUC .wrtmuc = true
120
121 static const struct v3d_qpu_sig v33_sig_map[] = {
122 /* MISC R3 R4 R5 */
123 [0] = { },
124 [1] = { THRSW, },
125 [2] = { LDUNIF },
126 [3] = { THRSW, LDUNIF },
127 [4] = { LDTMU, },
128 [5] = { THRSW, LDTMU, },
129 [6] = { LDTMU, LDUNIF },
130 [7] = { THRSW, LDTMU, LDUNIF },
131 [8] = { LDVARY, },
132 [9] = { THRSW, LDVARY, },
133 [10] = { LDVARY, LDUNIF },
134 [11] = { THRSW, LDVARY, LDUNIF },
135 [12] = { LDVARY, LDTMU, },
136 [13] = { THRSW, LDVARY, LDTMU, },
137 [14] = { SMIMM, LDVARY, },
138 [15] = { SMIMM, },
139 [16] = { LDTLB, },
140 [17] = { LDTLBU, },
141 /* 18-21 reserved */
142 [22] = { UCB, },
143 [23] = { ROT, },
144 [24] = { LDVPM, },
145 [25] = { THRSW, LDVPM, },
146 [26] = { LDVPM, LDUNIF },
147 [27] = { THRSW, LDVPM, LDUNIF },
148 [28] = { LDVPM, LDTMU, },
149 [29] = { THRSW, LDVPM, LDTMU, },
150 [30] = { SMIMM, LDVPM, },
151 [31] = { SMIMM, },
152 };
153
154 static const struct v3d_qpu_sig v40_sig_map[] = {
155 /* MISC R3 R4 R5 */
156 [0] = { },
157 [1] = { THRSW, },
158 [2] = { LDUNIF },
159 [3] = { THRSW, LDUNIF },
160 [4] = { LDTMU, },
161 [5] = { THRSW, LDTMU, },
162 [6] = { LDTMU, LDUNIF },
163 [7] = { THRSW, LDTMU, LDUNIF },
164 [8] = { LDVARY, },
165 [9] = { THRSW, LDVARY, },
166 [10] = { LDVARY, LDUNIF },
167 [11] = { THRSW, LDVARY, LDUNIF },
168 /* 12-13 reserved */
169 [14] = { SMIMM, LDVARY, },
170 [15] = { SMIMM, },
171 [16] = { LDTLB, },
172 [17] = { LDTLBU, },
173 [18] = { WRTMUC },
174 [19] = { THRSW, WRTMUC },
175 [20] = { LDVARY, WRTMUC },
176 [21] = { THRSW, LDVARY, WRTMUC },
177 [22] = { UCB, },
178 [23] = { ROT, },
179 /* 24-30 reserved */
180 [31] = { SMIMM, LDTMU, },
181 };
182
183 static const struct v3d_qpu_sig v41_sig_map[] = {
184 /* MISC phys R5 */
185 [0] = { },
186 [1] = { THRSW, },
187 [2] = { LDUNIF },
188 [3] = { THRSW, LDUNIF },
189 [4] = { LDTMU, },
190 [5] = { THRSW, LDTMU, },
191 [6] = { LDTMU, LDUNIF },
192 [7] = { THRSW, LDTMU, LDUNIF },
193 [8] = { LDVARY, },
194 [9] = { THRSW, LDVARY, },
195 [10] = { LDVARY, LDUNIF },
196 [11] = { THRSW, LDVARY, LDUNIF },
197 [12] = { LDUNIFRF },
198 [13] = { THRSW, LDUNIFRF },
199 [14] = { SMIMM, LDVARY, },
200 [15] = { SMIMM, },
201 [16] = { LDTLB, },
202 [17] = { LDTLBU, },
203 [18] = { WRTMUC },
204 [19] = { THRSW, WRTMUC },
205 [20] = { LDVARY, WRTMUC },
206 [21] = { THRSW, LDVARY, WRTMUC },
207 [22] = { UCB, },
208 [23] = { ROT, },
209 /* 24-30 reserved */
210 [24] = { LDUNIFA},
211 [25] = { LDUNIFARF },
212 [31] = { SMIMM, LDTMU, },
213 };
214
215 bool
216 v3d_qpu_sig_unpack(const struct v3d_device_info *devinfo,
217 uint32_t packed_sig,
218 struct v3d_qpu_sig *sig)
219 {
220 if (packed_sig >= ARRAY_SIZE(v33_sig_map))
221 return false;
222
223 if (devinfo->ver >= 41)
224 *sig = v41_sig_map[packed_sig];
225 else if (devinfo->ver == 40)
226 *sig = v40_sig_map[packed_sig];
227 else
228 *sig = v33_sig_map[packed_sig];
229
230 /* Signals with zeroed unpacked contents after element 0 are reserved. */
231 return (packed_sig == 0 ||
232 memcmp(sig, &v33_sig_map[0], sizeof(*sig)) != 0);
233 }
234
235 bool
236 v3d_qpu_sig_pack(const struct v3d_device_info *devinfo,
237 const struct v3d_qpu_sig *sig,
238 uint32_t *packed_sig)
239 {
240 static const struct v3d_qpu_sig *map;
241
242 if (devinfo->ver >= 41)
243 map = v41_sig_map;
244 else if (devinfo->ver == 40)
245 map = v40_sig_map;
246 else
247 map = v33_sig_map;
248
249 for (int i = 0; i < ARRAY_SIZE(v33_sig_map); i++) {
250 if (memcmp(&map[i], sig, sizeof(*sig)) == 0) {
251 *packed_sig = i;
252 return true;
253 }
254 }
255
256 return false;
257 }
258 static inline unsigned
259 fui( float f )
260 {
261 union {float f; unsigned ui;} fi;
262 fi.f = f;
263 return fi.ui;
264 }
265
266 static const uint32_t small_immediates[] = {
267 0, 1, 2, 3,
268 4, 5, 6, 7,
269 8, 9, 10, 11,
270 12, 13, 14, 15,
271 -16, -15, -14, -13,
272 -12, -11, -10, -9,
273 -8, -7, -6, -5,
274 -4, -3, -2, -1,
275 0x3b800000, /* 2.0^-8 */
276 0x3c000000, /* 2.0^-7 */
277 0x3c800000, /* 2.0^-6 */
278 0x3d000000, /* 2.0^-5 */
279 0x3d800000, /* 2.0^-4 */
280 0x3e000000, /* 2.0^-3 */
281 0x3e800000, /* 2.0^-2 */
282 0x3f000000, /* 2.0^-1 */
283 0x3f800000, /* 2.0^0 */
284 0x40000000, /* 2.0^1 */
285 0x40800000, /* 2.0^2 */
286 0x41000000, /* 2.0^3 */
287 0x41800000, /* 2.0^4 */
288 0x42000000, /* 2.0^5 */
289 0x42800000, /* 2.0^6 */
290 0x43000000, /* 2.0^7 */
291 };
292
293 bool
294 v3d_qpu_small_imm_unpack(const struct v3d_device_info *devinfo,
295 uint32_t packed_small_immediate,
296 uint32_t *small_immediate)
297 {
298 if (packed_small_immediate >= ARRAY_SIZE(small_immediates))
299 return false;
300
301 *small_immediate = small_immediates[packed_small_immediate];
302 return true;
303 }
304
305 bool
306 v3d_qpu_small_imm_pack(const struct v3d_device_info *devinfo,
307 uint32_t value,
308 uint32_t *packed_small_immediate)
309 {
310 STATIC_ASSERT(ARRAY_SIZE(small_immediates) == 48);
311
312 for (int i = 0; i < ARRAY_SIZE(small_immediates); i++) {
313 if (small_immediates[i] == value) {
314 *packed_small_immediate = i;
315 return true;
316 }
317 }
318
319 return false;
320 }
321
322 bool
323 v3d_qpu_flags_unpack(const struct v3d_device_info *devinfo,
324 uint32_t packed_cond,
325 struct v3d_qpu_flags *cond)
326 {
327 static const enum v3d_qpu_cond cond_map[4] = {
328 [0] = V3D_QPU_COND_IFA,
329 [1] = V3D_QPU_COND_IFB,
330 [2] = V3D_QPU_COND_IFNA,
331 [3] = V3D_QPU_COND_IFNB,
332 };
333
334 cond->ac = V3D_QPU_COND_NONE;
335 cond->mc = V3D_QPU_COND_NONE;
336 cond->apf = V3D_QPU_PF_NONE;
337 cond->mpf = V3D_QPU_PF_NONE;
338 cond->auf = V3D_QPU_UF_NONE;
339 cond->muf = V3D_QPU_UF_NONE;
340
341 if (packed_cond == 0) {
342 return true;
343 } else if (packed_cond >> 2 == 0) {
344 cond->apf = packed_cond & 0x3;
345 } else if (packed_cond >> 4 == 0) {
346 cond->auf = (packed_cond & 0xf) - 4 + V3D_QPU_UF_ANDZ;
347 } else if (packed_cond == 0x10) {
348 return false;
349 } else if (packed_cond >> 2 == 0x4) {
350 cond->mpf = packed_cond & 0x3;
351 } else if (packed_cond >> 4 == 0x1) {
352 cond->muf = (packed_cond & 0xf) - 4 + V3D_QPU_UF_ANDZ;
353 } else if (packed_cond >> 4 == 0x2) {
354 cond->ac = ((packed_cond >> 2) & 0x3) + V3D_QPU_COND_IFA;
355 cond->mpf = packed_cond & 0x3;
356 } else if (packed_cond >> 4 == 0x3) {
357 cond->mc = ((packed_cond >> 2) & 0x3) + V3D_QPU_COND_IFA;
358 cond->apf = packed_cond & 0x3;
359 } else if (packed_cond >> 6) {
360 cond->mc = cond_map[(packed_cond >> 4) & 0x3];
361 if (((packed_cond >> 2) & 0x3) == 0) {
362 cond->ac = cond_map[packed_cond & 0x3];
363 } else {
364 cond->auf = (packed_cond & 0xf) - 4 + V3D_QPU_UF_ANDZ;
365 }
366 }
367
368 return true;
369 }
370
371 bool
372 v3d_qpu_flags_pack(const struct v3d_device_info *devinfo,
373 const struct v3d_qpu_flags *cond,
374 uint32_t *packed_cond)
375 {
376 #define AC (1 << 0)
377 #define MC (1 << 1)
378 #define APF (1 << 2)
379 #define MPF (1 << 3)
380 #define AUF (1 << 4)
381 #define MUF (1 << 5)
382 static const struct {
383 uint8_t flags_present;
384 uint8_t bits;
385 } flags_table[] = {
386 { 0, 0 },
387 { APF, 0 },
388 { AUF, 0 },
389 { MPF, (1 << 4) },
390 { MUF, (1 << 4) },
391 { AC, (1 << 5) },
392 { AC | MPF, (1 << 5) },
393 { MC, (1 << 5) | (1 << 4) },
394 { MC | APF, (1 << 5) | (1 << 4) },
395 { MC | AC, (1 << 6) },
396 { MC | AUF, (1 << 6) },
397 };
398
399 uint8_t flags_present = 0;
400 if (cond->ac != V3D_QPU_COND_NONE)
401 flags_present |= AC;
402 if (cond->mc != V3D_QPU_COND_NONE)
403 flags_present |= MC;
404 if (cond->apf != V3D_QPU_PF_NONE)
405 flags_present |= APF;
406 if (cond->mpf != V3D_QPU_PF_NONE)
407 flags_present |= MPF;
408 if (cond->auf != V3D_QPU_UF_NONE)
409 flags_present |= AUF;
410 if (cond->muf != V3D_QPU_UF_NONE)
411 flags_present |= MUF;
412
413 for (int i = 0; i < ARRAY_SIZE(flags_table); i++) {
414 if (flags_table[i].flags_present != flags_present)
415 continue;
416
417 *packed_cond = flags_table[i].bits;
418
419 *packed_cond |= cond->apf;
420 *packed_cond |= cond->mpf;
421
422 if (flags_present & AUF)
423 *packed_cond |= cond->auf - V3D_QPU_UF_ANDZ + 4;
424 if (flags_present & MUF)
425 *packed_cond |= cond->muf - V3D_QPU_UF_ANDZ + 4;
426
427 if (flags_present & AC)
428 *packed_cond |= (cond->ac - V3D_QPU_COND_IFA) << 2;
429
430 if (flags_present & MC) {
431 if (*packed_cond & (1 << 6))
432 *packed_cond |= (cond->mc -
433 V3D_QPU_COND_IFA) << 4;
434 else
435 *packed_cond |= (cond->mc -
436 V3D_QPU_COND_IFA) << 2;
437 }
438
439 return true;
440 }
441
442 return false;
443 }
444
445 /* Make a mapping of the table of opcodes in the spec. The opcode is
446 * determined by a combination of the opcode field, and in the case of 0 or
447 * 1-arg opcodes, the mux_b field as well.
448 */
449 #define MUX_MASK(bot, top) (((1 << (top + 1)) - 1) - ((1 << (bot)) - 1))
450 #define ANYMUX MUX_MASK(0, 7)
451
452 struct opcode_desc {
453 uint8_t opcode_first;
454 uint8_t opcode_last;
455 uint8_t mux_b_mask;
456 uint8_t mux_a_mask;
457 uint8_t op;
458 /* 0 if it's the same across V3D versions, or a specific V3D version. */
459 uint8_t ver;
460 };
461
462 static const struct opcode_desc add_ops[] = {
463 /* FADD is FADDNF depending on the order of the mux_a/mux_b. */
464 { 0, 47, ANYMUX, ANYMUX, V3D_QPU_A_FADD },
465 { 0, 47, ANYMUX, ANYMUX, V3D_QPU_A_FADDNF },
466 { 53, 55, ANYMUX, ANYMUX, V3D_QPU_A_VFPACK },
467 { 56, 56, ANYMUX, ANYMUX, V3D_QPU_A_ADD },
468 { 57, 59, ANYMUX, ANYMUX, V3D_QPU_A_VFPACK },
469 { 60, 60, ANYMUX, ANYMUX, V3D_QPU_A_SUB },
470 { 61, 63, ANYMUX, ANYMUX, V3D_QPU_A_VFPACK },
471 { 64, 111, ANYMUX, ANYMUX, V3D_QPU_A_FSUB },
472 { 120, 120, ANYMUX, ANYMUX, V3D_QPU_A_MIN },
473 { 121, 121, ANYMUX, ANYMUX, V3D_QPU_A_MAX },
474 { 122, 122, ANYMUX, ANYMUX, V3D_QPU_A_UMIN },
475 { 123, 123, ANYMUX, ANYMUX, V3D_QPU_A_UMAX },
476 { 124, 124, ANYMUX, ANYMUX, V3D_QPU_A_SHL },
477 { 125, 125, ANYMUX, ANYMUX, V3D_QPU_A_SHR },
478 { 126, 126, ANYMUX, ANYMUX, V3D_QPU_A_ASR },
479 { 127, 127, ANYMUX, ANYMUX, V3D_QPU_A_ROR },
480 /* FMIN is instead FMAX depending on the order of the mux_a/mux_b. */
481 { 128, 175, ANYMUX, ANYMUX, V3D_QPU_A_FMIN },
482 { 128, 175, ANYMUX, ANYMUX, V3D_QPU_A_FMAX },
483 { 176, 180, ANYMUX, ANYMUX, V3D_QPU_A_VFMIN },
484
485 { 181, 181, ANYMUX, ANYMUX, V3D_QPU_A_AND },
486 { 182, 182, ANYMUX, ANYMUX, V3D_QPU_A_OR },
487 { 183, 183, ANYMUX, ANYMUX, V3D_QPU_A_XOR },
488
489 { 184, 184, ANYMUX, ANYMUX, V3D_QPU_A_VADD },
490 { 185, 185, ANYMUX, ANYMUX, V3D_QPU_A_VSUB },
491 { 186, 186, 1 << 0, ANYMUX, V3D_QPU_A_NOT },
492 { 186, 186, 1 << 1, ANYMUX, V3D_QPU_A_NEG },
493 { 186, 186, 1 << 2, ANYMUX, V3D_QPU_A_FLAPUSH },
494 { 186, 186, 1 << 3, ANYMUX, V3D_QPU_A_FLBPUSH },
495 { 186, 186, 1 << 4, ANYMUX, V3D_QPU_A_FLPOP },
496 { 186, 186, 1 << 5, ANYMUX, V3D_QPU_A_RECIP },
497 { 186, 186, 1 << 6, ANYMUX, V3D_QPU_A_SETMSF },
498 { 186, 186, 1 << 7, ANYMUX, V3D_QPU_A_SETREVF },
499 { 187, 187, 1 << 0, 1 << 0, V3D_QPU_A_NOP, 0 },
500 { 187, 187, 1 << 0, 1 << 1, V3D_QPU_A_TIDX },
501 { 187, 187, 1 << 0, 1 << 2, V3D_QPU_A_EIDX },
502 { 187, 187, 1 << 0, 1 << 3, V3D_QPU_A_LR },
503 { 187, 187, 1 << 0, 1 << 4, V3D_QPU_A_VFLA },
504 { 187, 187, 1 << 0, 1 << 5, V3D_QPU_A_VFLNA },
505 { 187, 187, 1 << 0, 1 << 6, V3D_QPU_A_VFLB },
506 { 187, 187, 1 << 0, 1 << 7, V3D_QPU_A_VFLNB },
507
508 { 187, 187, 1 << 1, MUX_MASK(0, 2), V3D_QPU_A_FXCD },
509 { 187, 187, 1 << 1, 1 << 3, V3D_QPU_A_XCD },
510 { 187, 187, 1 << 1, MUX_MASK(4, 6), V3D_QPU_A_FYCD },
511 { 187, 187, 1 << 1, 1 << 7, V3D_QPU_A_YCD },
512
513 { 187, 187, 1 << 2, 1 << 0, V3D_QPU_A_MSF },
514 { 187, 187, 1 << 2, 1 << 1, V3D_QPU_A_REVF },
515 { 187, 187, 1 << 2, 1 << 2, V3D_QPU_A_VDWWT, 33 },
516 { 187, 187, 1 << 2, 1 << 2, V3D_QPU_A_IID, 40 },
517 { 187, 187, 1 << 2, 1 << 3, V3D_QPU_A_SAMPID, 40 },
518 { 187, 187, 1 << 2, 1 << 4, V3D_QPU_A_BARRIERID, 40 },
519 { 187, 187, 1 << 2, 1 << 5, V3D_QPU_A_TMUWT },
520 { 187, 187, 1 << 2, 1 << 6, V3D_QPU_A_VPMWT },
521
522 { 187, 187, 1 << 3, ANYMUX, V3D_QPU_A_VPMSETUP, 33 },
523 { 188, 188, 1 << 0, ANYMUX, V3D_QPU_A_LDVPMV_IN, 40 },
524 { 188, 188, 1 << 0, ANYMUX, V3D_QPU_A_LDVPMV_OUT, 40 },
525 { 188, 188, 1 << 1, ANYMUX, V3D_QPU_A_LDVPMD_IN, 40 },
526 { 188, 188, 1 << 1, ANYMUX, V3D_QPU_A_LDVPMD_OUT, 40 },
527 { 188, 188, 1 << 2, ANYMUX, V3D_QPU_A_LDVPMP, 40 },
528 { 188, 188, 1 << 3, ANYMUX, V3D_QPU_A_RSQRT, 41 },
529 { 188, 188, 1 << 4, ANYMUX, V3D_QPU_A_EXP, 41 },
530 { 188, 188, 1 << 5, ANYMUX, V3D_QPU_A_LOG, 41 },
531 { 188, 188, 1 << 6, ANYMUX, V3D_QPU_A_SIN, 41 },
532 { 188, 188, 1 << 7, ANYMUX, V3D_QPU_A_RSQRT2, 41 },
533 { 189, 189, ANYMUX, ANYMUX, V3D_QPU_A_LDVPMG_IN, 40 },
534 { 189, 189, ANYMUX, ANYMUX, V3D_QPU_A_LDVPMG_OUT, 40 },
535
536 /* FIXME: MORE COMPLICATED */
537 /* { 190, 191, ANYMUX, ANYMUX, V3D_QPU_A_VFMOVABSNEGNAB }, */
538
539 { 192, 239, ANYMUX, ANYMUX, V3D_QPU_A_FCMP },
540 { 240, 244, ANYMUX, ANYMUX, V3D_QPU_A_VFMAX },
541
542 { 245, 245, MUX_MASK(0, 2), ANYMUX, V3D_QPU_A_FROUND },
543 { 245, 245, 1 << 3, ANYMUX, V3D_QPU_A_FTOIN },
544 { 245, 245, MUX_MASK(4, 6), ANYMUX, V3D_QPU_A_FTRUNC },
545 { 245, 245, 1 << 7, ANYMUX, V3D_QPU_A_FTOIZ },
546 { 246, 246, MUX_MASK(0, 2), ANYMUX, V3D_QPU_A_FFLOOR },
547 { 246, 246, 1 << 3, ANYMUX, V3D_QPU_A_FTOUZ },
548 { 246, 246, MUX_MASK(4, 6), ANYMUX, V3D_QPU_A_FCEIL },
549 { 246, 246, 1 << 7, ANYMUX, V3D_QPU_A_FTOC },
550
551 { 247, 247, MUX_MASK(0, 2), ANYMUX, V3D_QPU_A_FDX },
552 { 247, 247, MUX_MASK(4, 6), ANYMUX, V3D_QPU_A_FDY },
553
554 /* The stvpms are distinguished by the waddr field. */
555 { 248, 248, ANYMUX, ANYMUX, V3D_QPU_A_STVPMV },
556 { 248, 248, ANYMUX, ANYMUX, V3D_QPU_A_STVPMD },
557 { 248, 248, ANYMUX, ANYMUX, V3D_QPU_A_STVPMP },
558
559 { 252, 252, MUX_MASK(0, 2), ANYMUX, V3D_QPU_A_ITOF },
560 { 252, 252, 1 << 3, ANYMUX, V3D_QPU_A_CLZ },
561 { 252, 252, MUX_MASK(4, 6), ANYMUX, V3D_QPU_A_UTOF },
562 };
563
564 static const struct opcode_desc mul_ops[] = {
565 { 1, 1, ANYMUX, ANYMUX, V3D_QPU_M_ADD },
566 { 2, 2, ANYMUX, ANYMUX, V3D_QPU_M_SUB },
567 { 3, 3, ANYMUX, ANYMUX, V3D_QPU_M_UMUL24 },
568 { 4, 8, ANYMUX, ANYMUX, V3D_QPU_M_VFMUL },
569 { 9, 9, ANYMUX, ANYMUX, V3D_QPU_M_SMUL24 },
570 { 10, 10, ANYMUX, ANYMUX, V3D_QPU_M_MULTOP },
571 { 14, 14, ANYMUX, ANYMUX, V3D_QPU_M_FMOV },
572 { 15, 15, MUX_MASK(0, 3), ANYMUX, V3D_QPU_M_FMOV },
573 { 15, 15, 1 << 4, 1 << 0, V3D_QPU_M_NOP, 0 },
574 { 15, 15, 1 << 7, ANYMUX, V3D_QPU_M_MOV },
575 { 16, 63, ANYMUX, ANYMUX, V3D_QPU_M_FMUL },
576 };
577
578 static const struct opcode_desc *
579 lookup_opcode(const struct opcode_desc *opcodes, size_t num_opcodes,
580 uint32_t opcode, uint32_t mux_a, uint32_t mux_b)
581 {
582 for (int i = 0; i < num_opcodes; i++) {
583 const struct opcode_desc *op_desc = &opcodes[i];
584
585 if (opcode < op_desc->opcode_first ||
586 opcode > op_desc->opcode_last)
587 continue;
588
589 if (!(op_desc->mux_b_mask & (1 << mux_b)))
590 continue;
591
592 if (!(op_desc->mux_a_mask & (1 << mux_a)))
593 continue;
594
595 return op_desc;
596 }
597
598 return NULL;
599 }
600
601 static bool
602 v3d_qpu_float32_unpack_unpack(uint32_t packed,
603 enum v3d_qpu_input_unpack *unpacked)
604 {
605 switch (packed) {
606 case 0:
607 *unpacked = V3D_QPU_UNPACK_ABS;
608 return true;
609 case 1:
610 *unpacked = V3D_QPU_UNPACK_NONE;
611 return true;
612 case 2:
613 *unpacked = V3D_QPU_UNPACK_L;
614 return true;
615 case 3:
616 *unpacked = V3D_QPU_UNPACK_H;
617 return true;
618 default:
619 return false;
620 }
621 }
622
623 static bool
624 v3d_qpu_float32_unpack_pack(enum v3d_qpu_input_unpack unpacked,
625 uint32_t *packed)
626 {
627 switch (unpacked) {
628 case V3D_QPU_UNPACK_ABS:
629 *packed = 0;
630 return true;
631 case V3D_QPU_UNPACK_NONE:
632 *packed = 1;
633 return true;
634 case V3D_QPU_UNPACK_L:
635 *packed = 2;
636 return true;
637 case V3D_QPU_UNPACK_H:
638 *packed = 3;
639 return true;
640 default:
641 return false;
642 }
643 }
644
645 static bool
646 v3d_qpu_float16_unpack_unpack(uint32_t packed,
647 enum v3d_qpu_input_unpack *unpacked)
648 {
649 switch (packed) {
650 case 0:
651 *unpacked = V3D_QPU_UNPACK_NONE;
652 return true;
653 case 1:
654 *unpacked = V3D_QPU_UNPACK_REPLICATE_32F_16;
655 return true;
656 case 2:
657 *unpacked = V3D_QPU_UNPACK_REPLICATE_L_16;
658 return true;
659 case 3:
660 *unpacked = V3D_QPU_UNPACK_REPLICATE_H_16;
661 return true;
662 case 4:
663 *unpacked = V3D_QPU_UNPACK_SWAP_16;
664 return true;
665 default:
666 return false;
667 }
668 }
669
670 static bool
671 v3d_qpu_float16_unpack_pack(enum v3d_qpu_input_unpack unpacked,
672 uint32_t *packed)
673 {
674 switch (unpacked) {
675 case V3D_QPU_UNPACK_NONE:
676 *packed = 0;
677 return true;
678 case V3D_QPU_UNPACK_REPLICATE_32F_16:
679 *packed = 1;
680 return true;
681 case V3D_QPU_UNPACK_REPLICATE_L_16:
682 *packed = 2;
683 return true;
684 case V3D_QPU_UNPACK_REPLICATE_H_16:
685 *packed = 3;
686 return true;
687 case V3D_QPU_UNPACK_SWAP_16:
688 *packed = 4;
689 return true;
690 default:
691 return false;
692 }
693 }
694
695 static bool
696 v3d_qpu_float32_pack_pack(enum v3d_qpu_input_unpack unpacked,
697 uint32_t *packed)
698 {
699 switch (unpacked) {
700 case V3D_QPU_PACK_NONE:
701 *packed = 0;
702 return true;
703 case V3D_QPU_PACK_L:
704 *packed = 1;
705 return true;
706 case V3D_QPU_PACK_H:
707 *packed = 2;
708 return true;
709 default:
710 return false;
711 }
712 }
713
714 static bool
715 v3d_qpu_add_unpack(const struct v3d_device_info *devinfo, uint64_t packed_inst,
716 struct v3d_qpu_instr *instr)
717 {
718 uint32_t op = QPU_GET_FIELD(packed_inst, VC5_QPU_OP_ADD);
719 uint32_t mux_a = QPU_GET_FIELD(packed_inst, VC5_QPU_ADD_A);
720 uint32_t mux_b = QPU_GET_FIELD(packed_inst, VC5_QPU_ADD_B);
721 uint32_t waddr = QPU_GET_FIELD(packed_inst, V3D_QPU_WADDR_A);
722
723 uint32_t map_op = op;
724 /* Some big clusters of opcodes are replicated with unpack
725 * flags
726 */
727 if (map_op >= 249 && map_op <= 251)
728 map_op = (map_op - 249 + 245);
729 if (map_op >= 253 && map_op <= 255)
730 map_op = (map_op - 253 + 245);
731
732 const struct opcode_desc *desc =
733 lookup_opcode(add_ops, ARRAY_SIZE(add_ops),
734 map_op, mux_a, mux_b);
735 if (!desc)
736 return false;
737
738 instr->alu.add.op = desc->op;
739
740 /* FADD/FADDNF and FMIN/FMAX are determined by the orders of the
741 * operands.
742 */
743 if (((op >> 2) & 3) * 8 + mux_a > (op & 3) * 8 + mux_b) {
744 if (instr->alu.add.op == V3D_QPU_A_FMIN)
745 instr->alu.add.op = V3D_QPU_A_FMAX;
746 if (instr->alu.add.op == V3D_QPU_A_FADD)
747 instr->alu.add.op = V3D_QPU_A_FADDNF;
748 }
749
750 /* Some QPU ops require a bit more than just basic opcode and mux a/b
751 * comparisons to distinguish them.
752 */
753 switch (instr->alu.add.op) {
754 case V3D_QPU_A_STVPMV:
755 case V3D_QPU_A_STVPMD:
756 case V3D_QPU_A_STVPMP:
757 switch (waddr) {
758 case 0:
759 instr->alu.add.op = V3D_QPU_A_STVPMV;
760 break;
761 case 1:
762 instr->alu.add.op = V3D_QPU_A_STVPMD;
763 break;
764 case 2:
765 instr->alu.add.op = V3D_QPU_A_STVPMP;
766 break;
767 default:
768 return false;
769 }
770 break;
771 default:
772 break;
773 }
774
775 switch (instr->alu.add.op) {
776 case V3D_QPU_A_FADD:
777 case V3D_QPU_A_FADDNF:
778 case V3D_QPU_A_FSUB:
779 case V3D_QPU_A_FMIN:
780 case V3D_QPU_A_FMAX:
781 case V3D_QPU_A_FCMP:
782 case V3D_QPU_A_VFPACK:
783 if (instr->alu.add.op != V3D_QPU_A_VFPACK)
784 instr->alu.add.output_pack = (op >> 4) & 0x3;
785 else
786 instr->alu.add.output_pack = V3D_QPU_PACK_NONE;
787
788 if (!v3d_qpu_float32_unpack_unpack((op >> 2) & 0x3,
789 &instr->alu.add.a_unpack)) {
790 return false;
791 }
792
793 if (!v3d_qpu_float32_unpack_unpack((op >> 0) & 0x3,
794 &instr->alu.add.b_unpack)) {
795 return false;
796 }
797 break;
798
799 case V3D_QPU_A_FFLOOR:
800 case V3D_QPU_A_FROUND:
801 case V3D_QPU_A_FTRUNC:
802 case V3D_QPU_A_FCEIL:
803 case V3D_QPU_A_FDX:
804 case V3D_QPU_A_FDY:
805 instr->alu.add.output_pack = mux_b & 0x3;
806
807 if (!v3d_qpu_float32_unpack_unpack((op >> 2) & 0x3,
808 &instr->alu.add.a_unpack)) {
809 return false;
810 }
811 break;
812
813 case V3D_QPU_A_FTOIN:
814 case V3D_QPU_A_FTOIZ:
815 case V3D_QPU_A_FTOUZ:
816 case V3D_QPU_A_FTOC:
817 instr->alu.add.output_pack = V3D_QPU_PACK_NONE;
818
819 if (!v3d_qpu_float32_unpack_unpack((op >> 2) & 0x3,
820 &instr->alu.add.a_unpack)) {
821 return false;
822 }
823 break;
824
825 case V3D_QPU_A_VFMIN:
826 case V3D_QPU_A_VFMAX:
827 if (!v3d_qpu_float16_unpack_unpack(op & 0x7,
828 &instr->alu.add.a_unpack)) {
829 return false;
830 }
831
832 instr->alu.add.output_pack = V3D_QPU_PACK_NONE;
833 instr->alu.add.b_unpack = V3D_QPU_UNPACK_NONE;
834 break;
835
836 default:
837 instr->alu.add.output_pack = V3D_QPU_PACK_NONE;
838 instr->alu.add.a_unpack = V3D_QPU_UNPACK_NONE;
839 instr->alu.add.b_unpack = V3D_QPU_UNPACK_NONE;
840 break;
841 }
842
843 instr->alu.add.a = mux_a;
844 instr->alu.add.b = mux_b;
845 instr->alu.add.waddr = QPU_GET_FIELD(packed_inst, V3D_QPU_WADDR_A);
846
847 instr->alu.add.magic_write = false;
848 if (packed_inst & VC5_QPU_MA) {
849 switch (instr->alu.add.op) {
850 case V3D_QPU_A_LDVPMV_IN:
851 instr->alu.add.op = V3D_QPU_A_LDVPMV_OUT;
852 break;
853 case V3D_QPU_A_LDVPMD_IN:
854 instr->alu.add.op = V3D_QPU_A_LDVPMD_OUT;
855 break;
856 case V3D_QPU_A_LDVPMG_IN:
857 instr->alu.add.op = V3D_QPU_A_LDVPMG_OUT;
858 break;
859 default:
860 instr->alu.add.magic_write = true;
861 break;
862 }
863 }
864
865 return true;
866 }
867
868 static bool
869 v3d_qpu_mul_unpack(const struct v3d_device_info *devinfo, uint64_t packed_inst,
870 struct v3d_qpu_instr *instr)
871 {
872 uint32_t op = QPU_GET_FIELD(packed_inst, VC5_QPU_OP_MUL);
873 uint32_t mux_a = QPU_GET_FIELD(packed_inst, VC5_QPU_MUL_A);
874 uint32_t mux_b = QPU_GET_FIELD(packed_inst, VC5_QPU_MUL_B);
875
876 {
877 const struct opcode_desc *desc =
878 lookup_opcode(mul_ops, ARRAY_SIZE(mul_ops),
879 op, mux_a, mux_b);
880 if (!desc)
881 return false;
882
883 instr->alu.mul.op = desc->op;
884 }
885
886 switch (instr->alu.mul.op) {
887 case V3D_QPU_M_FMUL:
888 instr->alu.mul.output_pack = ((op >> 4) & 0x3) - 1;
889
890 if (!v3d_qpu_float32_unpack_unpack((op >> 2) & 0x3,
891 &instr->alu.mul.a_unpack)) {
892 return false;
893 }
894
895 if (!v3d_qpu_float32_unpack_unpack((op >> 0) & 0x3,
896 &instr->alu.mul.b_unpack)) {
897 return false;
898 }
899
900 break;
901
902 case V3D_QPU_M_FMOV:
903 instr->alu.mul.output_pack = (((op & 1) << 1) +
904 ((mux_b >> 2) & 1));
905
906 if (!v3d_qpu_float32_unpack_unpack(mux_b & 0x3,
907 &instr->alu.mul.a_unpack)) {
908 return false;
909 }
910
911 break;
912
913 case V3D_QPU_M_VFMUL:
914 instr->alu.mul.output_pack = V3D_QPU_PACK_NONE;
915
916 if (!v3d_qpu_float16_unpack_unpack(((op & 0x7) - 4) & 7,
917 &instr->alu.mul.a_unpack)) {
918 return false;
919 }
920
921 instr->alu.mul.b_unpack = V3D_QPU_UNPACK_NONE;
922
923 break;
924
925 default:
926 instr->alu.mul.output_pack = V3D_QPU_PACK_NONE;
927 instr->alu.mul.a_unpack = V3D_QPU_UNPACK_NONE;
928 instr->alu.mul.b_unpack = V3D_QPU_UNPACK_NONE;
929 break;
930 }
931
932 instr->alu.mul.a = mux_a;
933 instr->alu.mul.b = mux_b;
934 instr->alu.mul.waddr = QPU_GET_FIELD(packed_inst, V3D_QPU_WADDR_M);
935 instr->alu.mul.magic_write = packed_inst & VC5_QPU_MM;
936
937 return true;
938 }
939
940 static bool
941 v3d_qpu_add_pack(const struct v3d_device_info *devinfo,
942 const struct v3d_qpu_instr *instr, uint64_t *packed_instr)
943 {
944 uint32_t waddr = instr->alu.add.waddr;
945 uint32_t mux_a = instr->alu.add.a;
946 uint32_t mux_b = instr->alu.add.b;
947 int nsrc = v3d_qpu_add_op_num_src(instr->alu.add.op);
948 const struct opcode_desc *desc;
949
950 int opcode;
951 for (desc = add_ops; desc != &add_ops[ARRAY_SIZE(add_ops)];
952 desc++) {
953 if (desc->op == instr->alu.add.op)
954 break;
955 }
956 if (desc == &add_ops[ARRAY_SIZE(add_ops)])
957 return false;
958
959 opcode = desc->opcode_first;
960
961 /* If an operation doesn't use an arg, its mux values may be used to
962 * identify the operation type.
963 */
964 if (nsrc < 2)
965 mux_b = ffs(desc->mux_b_mask) - 1;
966
967 if (nsrc < 1)
968 mux_a = ffs(desc->mux_a_mask) - 1;
969
970 bool no_magic_write = false;
971
972 switch (instr->alu.add.op) {
973 case V3D_QPU_A_STVPMV:
974 waddr = 0;
975 no_magic_write = true;
976 break;
977 case V3D_QPU_A_STVPMD:
978 waddr = 1;
979 no_magic_write = true;
980 break;
981 case V3D_QPU_A_STVPMP:
982 waddr = 2;
983 no_magic_write = true;
984 break;
985
986 case V3D_QPU_A_LDVPMV_IN:
987 case V3D_QPU_A_LDVPMD_IN:
988 case V3D_QPU_A_LDVPMP:
989 case V3D_QPU_A_LDVPMG_IN:
990 assert(!instr->alu.add.magic_write);
991 break;
992
993 case V3D_QPU_A_LDVPMV_OUT:
994 case V3D_QPU_A_LDVPMD_OUT:
995 case V3D_QPU_A_LDVPMG_OUT:
996 assert(!instr->alu.add.magic_write);
997 *packed_instr |= VC5_QPU_MA;
998 break;
999
1000 default:
1001 break;
1002 }
1003
1004 switch (instr->alu.add.op) {
1005 case V3D_QPU_A_FADD:
1006 case V3D_QPU_A_FADDNF:
1007 case V3D_QPU_A_FSUB:
1008 case V3D_QPU_A_FMIN:
1009 case V3D_QPU_A_FMAX:
1010 case V3D_QPU_A_FCMP: {
1011 uint32_t output_pack;
1012 uint32_t a_unpack;
1013 uint32_t b_unpack;
1014
1015 if (!v3d_qpu_float32_pack_pack(instr->alu.add.output_pack,
1016 &output_pack)) {
1017 return false;
1018 }
1019 opcode |= output_pack << 4;
1020
1021 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.a_unpack,
1022 &a_unpack)) {
1023 return false;
1024 }
1025
1026 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.b_unpack,
1027 &b_unpack)) {
1028 return false;
1029 }
1030
1031 /* These operations with commutative operands are
1032 * distinguished by which order their operands come in.
1033 */
1034 bool ordering = a_unpack * 8 + mux_a > b_unpack * 8 + mux_b;
1035 if (((instr->alu.add.op == V3D_QPU_A_FMIN ||
1036 instr->alu.add.op == V3D_QPU_A_FADD) && ordering) ||
1037 ((instr->alu.add.op == V3D_QPU_A_FMAX ||
1038 instr->alu.add.op == V3D_QPU_A_FADDNF) && !ordering)) {
1039 uint32_t temp;
1040
1041 temp = a_unpack;
1042 a_unpack = b_unpack;
1043 b_unpack = temp;
1044
1045 temp = mux_a;
1046 mux_a = mux_b;
1047 mux_b = temp;
1048 }
1049
1050 opcode |= a_unpack << 2;
1051 opcode |= b_unpack << 0;
1052
1053 break;
1054 }
1055
1056 case V3D_QPU_A_VFPACK: {
1057 uint32_t a_unpack;
1058 uint32_t b_unpack;
1059
1060 if (instr->alu.add.a_unpack == V3D_QPU_UNPACK_ABS ||
1061 instr->alu.add.b_unpack == V3D_QPU_UNPACK_ABS) {
1062 return false;
1063 }
1064
1065 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.a_unpack,
1066 &a_unpack)) {
1067 return false;
1068 }
1069
1070 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.b_unpack,
1071 &b_unpack)) {
1072 return false;
1073 }
1074
1075 opcode = (opcode & ~(1 << 2)) | (a_unpack << 2);
1076 opcode = (opcode & ~(1 << 0)) | (b_unpack << 0);
1077
1078 break;
1079 }
1080
1081 case V3D_QPU_A_FFLOOR:
1082 case V3D_QPU_A_FROUND:
1083 case V3D_QPU_A_FTRUNC:
1084 case V3D_QPU_A_FCEIL:
1085 case V3D_QPU_A_FDX:
1086 case V3D_QPU_A_FDY: {
1087 uint32_t packed;
1088
1089 if (!v3d_qpu_float32_pack_pack(instr->alu.add.output_pack,
1090 &packed)) {
1091 return false;
1092 }
1093 mux_b |= packed;
1094
1095 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.a_unpack,
1096 &packed)) {
1097 return false;
1098 }
1099 if (packed == 0)
1100 return false;
1101 opcode = (opcode & ~(1 << 2)) | packed << 2;
1102 break;
1103 }
1104
1105 case V3D_QPU_A_FTOIN:
1106 case V3D_QPU_A_FTOIZ:
1107 case V3D_QPU_A_FTOUZ:
1108 case V3D_QPU_A_FTOC:
1109 if (instr->alu.add.output_pack != V3D_QPU_PACK_NONE)
1110 return false;
1111
1112 uint32_t packed;
1113 if (!v3d_qpu_float32_unpack_pack(instr->alu.add.a_unpack,
1114 &packed)) {
1115 return false;
1116 }
1117 if (packed == 0)
1118 return false;
1119 opcode |= packed << 2;
1120
1121 break;
1122
1123 case V3D_QPU_A_VFMIN:
1124 case V3D_QPU_A_VFMAX:
1125 if (instr->alu.add.output_pack != V3D_QPU_PACK_NONE ||
1126 instr->alu.add.b_unpack != V3D_QPU_UNPACK_NONE) {
1127 return false;
1128 }
1129
1130 if (!v3d_qpu_float16_unpack_pack(instr->alu.add.a_unpack,
1131 &packed)) {
1132 return false;
1133 }
1134 opcode |= packed;
1135 break;
1136
1137 default:
1138 if (instr->alu.add.op != V3D_QPU_A_NOP &&
1139 (instr->alu.add.output_pack != V3D_QPU_PACK_NONE ||
1140 instr->alu.add.a_unpack != V3D_QPU_UNPACK_NONE ||
1141 instr->alu.add.b_unpack != V3D_QPU_UNPACK_NONE)) {
1142 return false;
1143 }
1144 break;
1145 }
1146
1147 *packed_instr |= QPU_SET_FIELD(mux_a, VC5_QPU_ADD_A);
1148 *packed_instr |= QPU_SET_FIELD(mux_b, VC5_QPU_ADD_B);
1149 *packed_instr |= QPU_SET_FIELD(opcode, VC5_QPU_OP_ADD);
1150 *packed_instr |= QPU_SET_FIELD(waddr, V3D_QPU_WADDR_A);
1151 if (instr->alu.add.magic_write && !no_magic_write)
1152 *packed_instr |= VC5_QPU_MA;
1153
1154 return true;
1155 }
1156
1157 static bool
1158 v3d_qpu_mul_pack(const struct v3d_device_info *devinfo,
1159 const struct v3d_qpu_instr *instr, uint64_t *packed_instr)
1160 {
1161 uint32_t mux_a = instr->alu.mul.a;
1162 uint32_t mux_b = instr->alu.mul.b;
1163 int nsrc = v3d_qpu_mul_op_num_src(instr->alu.mul.op);
1164 const struct opcode_desc *desc;
1165
1166 for (desc = mul_ops; desc != &mul_ops[ARRAY_SIZE(mul_ops)];
1167 desc++) {
1168 if (desc->op == instr->alu.mul.op)
1169 break;
1170 }
1171 if (desc == &mul_ops[ARRAY_SIZE(mul_ops)])
1172 return false;
1173
1174 uint32_t opcode = desc->opcode_first;
1175
1176 /* Some opcodes have a single valid value for their mux a/b, so set
1177 * that here. If mux a/b determine packing, it will be set below.
1178 */
1179 if (nsrc < 2)
1180 mux_b = ffs(desc->mux_b_mask) - 1;
1181
1182 if (nsrc < 1)
1183 mux_a = ffs(desc->mux_a_mask) - 1;
1184
1185 switch (instr->alu.mul.op) {
1186 case V3D_QPU_M_FMUL: {
1187 uint32_t packed;
1188
1189 if (!v3d_qpu_float32_pack_pack(instr->alu.mul.output_pack,
1190 &packed)) {
1191 return false;
1192 }
1193 /* No need for a +1 because desc->opcode_first has a 1 in this
1194 * field.
1195 */
1196 opcode += packed << 4;
1197
1198 if (!v3d_qpu_float32_unpack_pack(instr->alu.mul.a_unpack,
1199 &packed)) {
1200 return false;
1201 }
1202 opcode |= packed << 2;
1203
1204 if (!v3d_qpu_float32_unpack_pack(instr->alu.mul.b_unpack,
1205 &packed)) {
1206 return false;
1207 }
1208 opcode |= packed << 0;
1209 break;
1210 }
1211
1212 case V3D_QPU_M_FMOV: {
1213 uint32_t packed;
1214
1215 if (!v3d_qpu_float32_pack_pack(instr->alu.mul.output_pack,
1216 &packed)) {
1217 return false;
1218 }
1219 opcode |= (packed >> 1) & 1;
1220 mux_b = (packed & 1) << 2;
1221
1222 if (!v3d_qpu_float32_unpack_pack(instr->alu.mul.a_unpack,
1223 &packed)) {
1224 return false;
1225 }
1226 mux_b |= packed;
1227 break;
1228 }
1229
1230 case V3D_QPU_M_VFMUL: {
1231 uint32_t packed;
1232
1233 if (instr->alu.mul.output_pack != V3D_QPU_PACK_NONE)
1234 return false;
1235
1236 if (!v3d_qpu_float16_unpack_pack(instr->alu.mul.a_unpack,
1237 &packed)) {
1238 return false;
1239 }
1240 if (instr->alu.mul.a_unpack == V3D_QPU_UNPACK_SWAP_16)
1241 opcode = 8;
1242 else
1243 opcode |= (packed + 4) & 7;
1244
1245 if (instr->alu.mul.b_unpack != V3D_QPU_UNPACK_NONE)
1246 return false;
1247
1248 break;
1249 }
1250
1251 default:
1252 break;
1253 }
1254
1255 *packed_instr |= QPU_SET_FIELD(mux_a, VC5_QPU_MUL_A);
1256 *packed_instr |= QPU_SET_FIELD(mux_b, VC5_QPU_MUL_B);
1257
1258 *packed_instr |= QPU_SET_FIELD(opcode, VC5_QPU_OP_MUL);
1259 *packed_instr |= QPU_SET_FIELD(instr->alu.mul.waddr, V3D_QPU_WADDR_M);
1260 if (instr->alu.mul.magic_write)
1261 *packed_instr |= VC5_QPU_MM;
1262
1263 return true;
1264 }
1265
1266 static bool
1267 v3d_qpu_instr_unpack_alu(const struct v3d_device_info *devinfo,
1268 uint64_t packed_instr,
1269 struct v3d_qpu_instr *instr)
1270 {
1271 instr->type = V3D_QPU_INSTR_TYPE_ALU;
1272
1273 if (!v3d_qpu_sig_unpack(devinfo,
1274 QPU_GET_FIELD(packed_instr, VC5_QPU_SIG),
1275 &instr->sig))
1276 return false;
1277
1278 uint32_t packed_cond = QPU_GET_FIELD(packed_instr, VC5_QPU_COND);
1279 if (v3d_qpu_sig_writes_address(devinfo, &instr->sig)) {
1280 instr->sig_addr = packed_cond & ~VC5_QPU_COND_SIG_MAGIC_ADDR;
1281 instr->sig_magic = packed_cond & VC5_QPU_COND_SIG_MAGIC_ADDR;
1282
1283 instr->flags.ac = V3D_QPU_COND_NONE;
1284 instr->flags.mc = V3D_QPU_COND_NONE;
1285 instr->flags.apf = V3D_QPU_PF_NONE;
1286 instr->flags.mpf = V3D_QPU_PF_NONE;
1287 instr->flags.auf = V3D_QPU_UF_NONE;
1288 instr->flags.muf = V3D_QPU_UF_NONE;
1289 } else {
1290 if (!v3d_qpu_flags_unpack(devinfo, packed_cond, &instr->flags))
1291 return false;
1292 }
1293
1294 instr->raddr_a = QPU_GET_FIELD(packed_instr, VC5_QPU_RADDR_A);
1295 instr->raddr_b = QPU_GET_FIELD(packed_instr, VC5_QPU_RADDR_B);
1296
1297 if (!v3d_qpu_add_unpack(devinfo, packed_instr, instr))
1298 return false;
1299
1300 if (!v3d_qpu_mul_unpack(devinfo, packed_instr, instr))
1301 return false;
1302
1303 return true;
1304 }
1305
1306 static bool
1307 v3d_qpu_instr_unpack_branch(const struct v3d_device_info *devinfo,
1308 uint64_t packed_instr,
1309 struct v3d_qpu_instr *instr)
1310 {
1311 instr->type = V3D_QPU_INSTR_TYPE_BRANCH;
1312
1313 uint32_t cond = QPU_GET_FIELD(packed_instr, VC5_QPU_BRANCH_COND);
1314 if (cond == 0)
1315 instr->branch.cond = V3D_QPU_BRANCH_COND_ALWAYS;
1316 else if (V3D_QPU_BRANCH_COND_A0 + (cond - 2) <=
1317 V3D_QPU_BRANCH_COND_ALLNA)
1318 instr->branch.cond = V3D_QPU_BRANCH_COND_A0 + (cond - 2);
1319 else
1320 return false;
1321
1322 uint32_t msfign = QPU_GET_FIELD(packed_instr, VC5_QPU_BRANCH_MSFIGN);
1323 if (msfign == 3)
1324 return false;
1325 instr->branch.msfign = msfign;
1326
1327 instr->branch.bdi = QPU_GET_FIELD(packed_instr, VC5_QPU_BRANCH_BDI);
1328
1329 instr->branch.ub = packed_instr & VC5_QPU_BRANCH_UB;
1330 if (instr->branch.ub) {
1331 instr->branch.bdu = QPU_GET_FIELD(packed_instr,
1332 VC5_QPU_BRANCH_BDU);
1333 }
1334
1335 instr->branch.raddr_a = QPU_GET_FIELD(packed_instr,
1336 VC5_QPU_RADDR_A);
1337
1338 instr->branch.offset = 0;
1339
1340 instr->branch.offset +=
1341 QPU_GET_FIELD(packed_instr,
1342 VC5_QPU_BRANCH_ADDR_LOW) << 3;
1343
1344 instr->branch.offset +=
1345 QPU_GET_FIELD(packed_instr,
1346 VC5_QPU_BRANCH_ADDR_HIGH) << 24;
1347
1348 return true;
1349 }
1350
1351 bool
1352 v3d_qpu_instr_unpack(const struct v3d_device_info *devinfo,
1353 uint64_t packed_instr,
1354 struct v3d_qpu_instr *instr)
1355 {
1356 if (QPU_GET_FIELD(packed_instr, VC5_QPU_OP_MUL) != 0) {
1357 return v3d_qpu_instr_unpack_alu(devinfo, packed_instr, instr);
1358 } else {
1359 uint32_t sig = QPU_GET_FIELD(packed_instr, VC5_QPU_SIG);
1360
1361 if ((sig & 24) == 16) {
1362 return v3d_qpu_instr_unpack_branch(devinfo, packed_instr,
1363 instr);
1364 } else {
1365 return false;
1366 }
1367 }
1368 }
1369
1370 static bool
1371 v3d_qpu_instr_pack_alu(const struct v3d_device_info *devinfo,
1372 const struct v3d_qpu_instr *instr,
1373 uint64_t *packed_instr)
1374 {
1375 uint32_t sig;
1376 if (!v3d_qpu_sig_pack(devinfo, &instr->sig, &sig))
1377 return false;
1378 *packed_instr |= QPU_SET_FIELD(sig, VC5_QPU_SIG);
1379
1380 if (instr->type == V3D_QPU_INSTR_TYPE_ALU) {
1381 *packed_instr |= QPU_SET_FIELD(instr->raddr_a, VC5_QPU_RADDR_A);
1382 *packed_instr |= QPU_SET_FIELD(instr->raddr_b, VC5_QPU_RADDR_B);
1383
1384 if (!v3d_qpu_add_pack(devinfo, instr, packed_instr))
1385 return false;
1386 if (!v3d_qpu_mul_pack(devinfo, instr, packed_instr))
1387 return false;
1388
1389 uint32_t flags;
1390 if (v3d_qpu_sig_writes_address(devinfo, &instr->sig)) {
1391 if (instr->flags.ac != V3D_QPU_COND_NONE ||
1392 instr->flags.mc != V3D_QPU_COND_NONE ||
1393 instr->flags.apf != V3D_QPU_PF_NONE ||
1394 instr->flags.mpf != V3D_QPU_PF_NONE ||
1395 instr->flags.auf != V3D_QPU_UF_NONE ||
1396 instr->flags.muf != V3D_QPU_UF_NONE) {
1397 return false;
1398 }
1399
1400 flags = instr->sig_addr;
1401 if (instr->sig_magic)
1402 flags |= VC5_QPU_COND_SIG_MAGIC_ADDR;
1403 } else {
1404 if (!v3d_qpu_flags_pack(devinfo, &instr->flags, &flags))
1405 return false;
1406 }
1407
1408 *packed_instr |= QPU_SET_FIELD(flags, VC5_QPU_COND);
1409 } else {
1410 if (v3d_qpu_sig_writes_address(devinfo, &instr->sig))
1411 return false;
1412 }
1413
1414 return true;
1415 }
1416
1417 static bool
1418 v3d_qpu_instr_pack_branch(const struct v3d_device_info *devinfo,
1419 const struct v3d_qpu_instr *instr,
1420 uint64_t *packed_instr)
1421 {
1422 *packed_instr |= QPU_SET_FIELD(16, VC5_QPU_SIG);
1423
1424 if (instr->branch.cond != V3D_QPU_BRANCH_COND_ALWAYS) {
1425 *packed_instr |= QPU_SET_FIELD(2 + (instr->branch.cond -
1426 V3D_QPU_BRANCH_COND_A0),
1427 VC5_QPU_BRANCH_COND);
1428 }
1429
1430 *packed_instr |= QPU_SET_FIELD(instr->branch.msfign,
1431 VC5_QPU_BRANCH_MSFIGN);
1432
1433 *packed_instr |= QPU_SET_FIELD(instr->branch.bdi,
1434 VC5_QPU_BRANCH_BDI);
1435
1436 if (instr->branch.ub) {
1437 *packed_instr |= VC5_QPU_BRANCH_UB;
1438 *packed_instr |= QPU_SET_FIELD(instr->branch.bdu,
1439 VC5_QPU_BRANCH_BDU);
1440 }
1441
1442 switch (instr->branch.bdi) {
1443 case V3D_QPU_BRANCH_DEST_ABS:
1444 case V3D_QPU_BRANCH_DEST_REL:
1445 *packed_instr |= QPU_SET_FIELD(instr->branch.msfign,
1446 VC5_QPU_BRANCH_MSFIGN);
1447
1448 *packed_instr |= QPU_SET_FIELD((instr->branch.offset &
1449 ~0xff000000) >> 3,
1450 VC5_QPU_BRANCH_ADDR_LOW);
1451
1452 *packed_instr |= QPU_SET_FIELD(instr->branch.offset >> 24,
1453 VC5_QPU_BRANCH_ADDR_HIGH);
1454
1455 case V3D_QPU_BRANCH_DEST_REGFILE:
1456 *packed_instr |= QPU_SET_FIELD(instr->branch.raddr_a,
1457 VC5_QPU_RADDR_A);
1458 break;
1459
1460 default:
1461 break;
1462 }
1463
1464 return true;
1465 }
1466
1467 bool
1468 v3d_qpu_instr_pack(const struct v3d_device_info *devinfo,
1469 const struct v3d_qpu_instr *instr,
1470 uint64_t *packed_instr)
1471 {
1472 *packed_instr = 0;
1473
1474 switch (instr->type) {
1475 case V3D_QPU_INSTR_TYPE_ALU:
1476 return v3d_qpu_instr_pack_alu(devinfo, instr, packed_instr);
1477 case V3D_QPU_INSTR_TYPE_BRANCH:
1478 return v3d_qpu_instr_pack_branch(devinfo, instr, packed_instr);
1479 default:
1480 return false;
1481 }
1482 }