2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "vc4_context.h"
31 vc4_dump_program(struct vc4_compile
*c
)
33 fprintf(stderr
, "%s prog %d/%d QPU:\n",
34 qir_get_stage_name(c
->stage
),
35 c
->program_id
, c
->variant_id
);
37 for (int i
= 0; i
< c
->qpu_inst_count
; i
++) {
38 fprintf(stderr
, "0x%016"PRIx64
" ", c
->qpu_insts
[i
]);
39 vc4_qpu_disasm(&c
->qpu_insts
[i
], 1);
40 fprintf(stderr
, "\n");
45 queue(struct vc4_compile
*c
, uint64_t inst
)
47 struct queued_qpu_inst
*q
= calloc(1, sizeof(*q
));
49 insert_at_tail(&c
->qpu_inst_list
, &q
->link
);
53 last_inst(struct vc4_compile
*c
)
55 struct queued_qpu_inst
*q
=
56 (struct queued_qpu_inst
*)last_elem(&c
->qpu_inst_list
);
61 set_last_cond_add(struct vc4_compile
*c
, uint32_t cond
)
63 *last_inst(c
) = qpu_set_cond_add(*last_inst(c
), cond
);
67 * Some special registers can be read from either file, which lets us resolve
68 * raddr conflicts without extra MOVs.
71 swap_file(struct qpu_reg
*src
)
76 if (src
->mux
== QPU_MUX_A
)
88 * This is used to resolve the fact that we might register-allocate two
89 * different operands of an instruction to the same physical register file
90 * even though instructions have only one field for the register file source
93 * In that case, we need to move one to a temporary that can be used in the
94 * instruction, instead.
97 fixup_raddr_conflict(struct vc4_compile
*c
,
99 struct qpu_reg
*src0
, struct qpu_reg
*src1
,
102 if ((src0
->mux
!= QPU_MUX_A
&& src0
->mux
!= QPU_MUX_B
) ||
103 src0
->mux
!= src1
->mux
||
104 src0
->addr
== src1
->addr
) {
108 if (swap_file(src0
) || swap_file(src1
))
111 if (src0
->mux
== QPU_MUX_A
) {
112 /* If we're conflicting over the A regfile, then we can just
113 * use the reserved rb31.
115 queue(c
, qpu_a_MOV(qpu_rb(31), *src1
));
119 /* Otherwise, we need a non-B regfile. So, we spill r3 out to
120 * rb31, then store our desired value in r3, and tell the
121 * caller to put rb31 back into r3 when we're done.
124 queue(c
, qpu_a_MOV(qpu_rb(31), qpu_r3()));
125 queue(c
, qpu_a_MOV(qpu_r3(), *src1
));
129 return r3_live
&& dst
.mux
!= QPU_MUX_R3
;
134 vc4_generate_code(struct vc4_context
*vc4
, struct vc4_compile
*c
)
136 struct qpu_reg
*temp_registers
= vc4_register_allocate(vc4
, c
);
137 bool discard
= false;
138 uint32_t inputs_remaining
= c
->num_inputs
;
139 uint32_t vpm_read_fifo_count
= 0;
140 uint32_t vpm_read_offset
= 0;
141 bool written_r3
= false;
144 make_empty_list(&c
->qpu_inst_list
);
149 /* There's a 4-entry FIFO for VPMVCD reads, each of which can
150 * load up to 16 dwords (4 vec4s) per vertex.
152 while (inputs_remaining
) {
153 uint32_t num_entries
= MIN2(inputs_remaining
, 16);
154 queue(c
, qpu_load_imm_ui(qpu_vrsetup(),
157 ((num_entries
& 0xf) << 20)));
158 inputs_remaining
-= num_entries
;
159 vpm_read_offset
+= num_entries
;
160 vpm_read_fifo_count
++;
162 assert(vpm_read_fifo_count
<= 4);
164 queue(c
, qpu_load_imm_ui(qpu_vwsetup(), 0x00001a00));
170 struct simple_node
*node
;
171 foreach(node
, &c
->instructions
) {
172 struct qinst
*qinst
= (struct qinst
*)node
;
175 fprintf(stderr
, "translating qinst to qpu: ");
176 qir_dump_inst(qinst
);
177 fprintf(stderr
, "\n");
180 static const struct {
184 #define A(name) [QOP_##name] = {QPU_A_##name, false}
185 #define M(name) [QOP_##name] = {QPU_M_##name, true}
210 struct qpu_reg src
[4];
211 for (int i
= 0; i
< qir_get_op_nsrc(qinst
->op
); i
++) {
212 int index
= qinst
->src
[i
].index
;
213 switch (qinst
->src
[i
].file
) {
218 src
[i
] = temp_registers
[index
];
230 switch (qinst
->dst
.file
) {
232 dst
= qpu_ra(QPU_W_NOP
);
235 dst
= temp_registers
[qinst
->dst
.index
];
239 assert(!"not reached");
245 /* Skip emitting the MOV if it's a no-op. */
246 if (dst
.mux
== QPU_MUX_A
|| dst
.mux
== QPU_MUX_B
||
247 dst
.mux
!= src
[0].mux
|| dst
.addr
!= src
[0].addr
) {
248 queue(c
, qpu_a_MOV(dst
, src
[0]));
253 queue(c
, qpu_a_MOV(qpu_ra(QPU_W_NOP
), src
[0]));
254 *last_inst(c
) |= QPU_SF
;
261 queue(c
, qpu_a_MOV(dst
, src
[0]));
262 set_last_cond_add(c
, qinst
->op
- QOP_SEL_X_0_ZS
+
265 queue(c
, qpu_a_XOR(dst
, qpu_r0(), qpu_r0()));
266 set_last_cond_add(c
, ((qinst
->op
- QOP_SEL_X_0_ZS
) ^
274 queue(c
, qpu_a_MOV(dst
, src
[0]));
275 set_last_cond_add(c
, qinst
->op
- QOP_SEL_X_Y_ZS
+
278 queue(c
, qpu_a_MOV(dst
, src
[1]));
279 set_last_cond_add(c
, ((qinst
->op
- QOP_SEL_X_Y_ZS
) ^
285 queue(c
, qpu_a_MOV(qpu_ra(QPU_W_VPM
), src
[0]));
289 queue(c
, qpu_a_MOV(dst
, qpu_ra(QPU_R_VPM
)));
298 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIP
),
302 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIPSQRT
),
306 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_EXP
),
310 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_SFU_LOG
),
317 queue(c
, qpu_a_MOV(dst
, qpu_r4()));
321 case QOP_PACK_COLORS
: {
322 /* We have to be careful not to start writing over one
323 * of our source values when incrementally writing the
324 * destination. So, if the dst is one of the srcs, we
325 * pack that one first (and we pack 4 channels at once
326 * for the first pack).
328 struct qpu_reg first_pack
= src
[0];
329 for (int i
= 0; i
< 4; i
++) {
330 if (src
[i
].mux
== dst
.mux
&&
331 src
[i
].addr
== dst
.addr
) {
336 queue(c
, qpu_m_MOV(dst
, first_pack
));
337 *last_inst(c
) |= QPU_PM
;
338 *last_inst(c
) |= QPU_SET_FIELD(QPU_PACK_MUL_8888
,
341 for (int i
= 0; i
< 4; i
++) {
342 if (src
[i
].mux
== first_pack
.mux
&&
343 src
[i
].addr
== first_pack
.addr
) {
347 queue(c
, qpu_m_MOV(dst
, src
[i
]));
348 *last_inst(c
) |= QPU_PM
;
349 *last_inst(c
) |= QPU_SET_FIELD(QPU_PACK_MUL_8A
+ i
,
357 queue(c
, qpu_a_ITOF(dst
,
358 qpu_ra(QPU_R_XY_PIXEL_COORD
)));
362 queue(c
, qpu_a_ITOF(dst
,
363 qpu_rb(QPU_R_XY_PIXEL_COORD
)));
366 case QOP_FRAG_REV_FLAG
:
367 queue(c
, qpu_a_ITOF(dst
,
368 qpu_rb(QPU_R_MS_REV_FLAGS
)));
373 /* QOP_FRAG_Z/W don't emit instructions, just allocate
374 * the register to the Z/W payload.
378 case QOP_TLB_DISCARD_SETUP
:
380 queue(c
, qpu_a_MOV(src
[0], src
[0]));
381 *last_inst(c
) |= QPU_SF
;
384 case QOP_TLB_STENCIL_SETUP
:
385 queue(c
, qpu_a_MOV(qpu_ra(QPU_W_TLB_STENCIL_SETUP
), src
[0]));
388 case QOP_TLB_Z_WRITE
:
389 queue(c
, qpu_a_MOV(qpu_ra(QPU_W_TLB_Z
), src
[0]));
391 set_last_cond_add(c
, QPU_COND_ZS
);
395 case QOP_TLB_COLOR_READ
:
397 *last_inst(c
) = qpu_set_sig(*last_inst(c
),
402 case QOP_TLB_COLOR_WRITE
:
403 queue(c
, qpu_a_MOV(qpu_tlbc(), src
[0]));
405 set_last_cond_add(c
, QPU_COND_ZS
);
410 queue(c
, qpu_a_FADD(dst
, src
[0], qpu_r5()));
413 case QOP_PACK_SCALED
: {
414 uint64_t a
= (qpu_a_MOV(dst
, src
[0]) |
415 QPU_SET_FIELD(QPU_PACK_A_16A
,
417 uint64_t b
= (qpu_a_MOV(dst
, src
[1]) |
418 QPU_SET_FIELD(QPU_PACK_A_16B
,
421 if (dst
.mux
== src
[1].mux
&& dst
.addr
== src
[1].addr
) {
435 queue(c
, qpu_a_MOV(qpu_rb(QPU_W_TMU0_S
+
436 (qinst
->op
- QOP_TEX_S
)),
441 needs_restore
= fixup_raddr_conflict(c
, dst
,
444 queue(c
, qpu_a_ADD(qpu_rb(QPU_W_TMU0_S
), src
[0], src
[1]));
446 queue(c
, qpu_a_MOV(qpu_r3(), qpu_rb(31)));
451 *last_inst(c
) = qpu_set_sig(*last_inst(c
),
456 case QOP_R4_UNPACK_A
:
457 case QOP_R4_UNPACK_B
:
458 case QOP_R4_UNPACK_C
:
459 case QOP_R4_UNPACK_D
:
460 assert(src
[0].mux
== QPU_MUX_R4
);
461 queue(c
, qpu_a_MOV(dst
, src
[0]));
462 *last_inst(c
) |= QPU_PM
;
463 *last_inst(c
) |= QPU_SET_FIELD(QPU_UNPACK_8A
+
473 case QOP_UNPACK_8D
: {
474 assert(src
[0].mux
== QPU_MUX_A
);
476 /* And, since we're setting the pack bits, if the
477 * destination is in A it would get re-packed.
479 struct qpu_reg orig_dst
= dst
;
480 if (orig_dst
.mux
== QPU_MUX_A
)
483 queue(c
, qpu_a_FMAX(dst
, src
[0], src
[0]));
484 *last_inst(c
) |= QPU_SET_FIELD(QPU_UNPACK_8A
+
489 if (orig_dst
.mux
== QPU_MUX_A
) {
490 queue(c
, qpu_a_MOV(orig_dst
, dst
));
496 assert(qinst
->op
< ARRAY_SIZE(translate
));
497 assert(translate
[qinst
->op
].op
!= 0); /* NOPs */
499 /* If we have only one source, put it in the second
500 * argument slot as well so that we don't take up
501 * another raddr just to get unused data.
503 if (qir_get_op_nsrc(qinst
->op
) == 1)
506 needs_restore
= fixup_raddr_conflict(c
, dst
,
510 if (translate
[qinst
->op
].is_mul
) {
511 queue(c
, qpu_m_alu2(translate
[qinst
->op
].op
,
515 queue(c
, qpu_a_alu2(translate
[qinst
->op
].op
,
520 queue(c
, qpu_a_MOV(qpu_r3(), qpu_rb(31)));
525 if (dst
.mux
== QPU_MUX_R3
)
529 qpu_schedule_instructions(c
);
531 /* thread end can't have VPM write or read */
532 if (QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
533 QPU_WADDR_ADD
) == QPU_W_VPM
||
534 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
535 QPU_WADDR_MUL
) == QPU_W_VPM
||
536 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
537 QPU_RADDR_A
) == QPU_R_VPM
||
538 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
539 QPU_RADDR_B
) == QPU_R_VPM
) {
540 qpu_serialize_one_inst(c
, qpu_NOP());
543 /* thread end can't have uniform read */
544 if (QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
545 QPU_RADDR_A
) == QPU_R_UNIF
||
546 QPU_GET_FIELD(c
->qpu_insts
[c
->qpu_inst_count
- 1],
547 QPU_RADDR_B
) == QPU_R_UNIF
) {
548 qpu_serialize_one_inst(c
, qpu_NOP());
551 /* thread end can't have TLB operations */
552 if (qpu_inst_is_tlb(c
->qpu_insts
[c
->qpu_inst_count
- 1]))
553 qpu_serialize_one_inst(c
, qpu_NOP());
555 c
->qpu_insts
[c
->qpu_inst_count
- 1] =
556 qpu_set_sig(c
->qpu_insts
[c
->qpu_inst_count
- 1],
558 qpu_serialize_one_inst(c
, qpu_NOP());
559 qpu_serialize_one_inst(c
, qpu_NOP());
566 c
->qpu_insts
[c
->qpu_inst_count
- 1] =
567 qpu_set_sig(c
->qpu_insts
[c
->qpu_inst_count
- 1],
568 QPU_SIG_SCOREBOARD_UNLOCK
);
572 if (vc4_debug
& VC4_DEBUG_QPU
)
575 vc4_qpu_validate(c
->qpu_insts
, c
->qpu_inst_count
);
577 free(temp_registers
);