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"
29 #include "util/ralloc.h"
32 vc4_dump_program(struct vc4_compile *c)
34 fprintf(stderr, "%s prog %d/%d QPU:\n",
35 qir_get_stage_name(c->stage),
36 c->program_id, c->variant_id);
38 for (int i = 0; i < c->qpu_inst_count; i++) {
39 fprintf(stderr, "0x%016"PRIx64" ", c->qpu_insts[i]);
40 vc4_qpu_disasm(&c->qpu_insts[i], 1);
41 fprintf(stderr, "\n");
46 queue(struct vc4_compile *c, uint64_t inst)
48 struct queued_qpu_inst *q = rzalloc(c, struct queued_qpu_inst);
50 list_addtail(&q->link, &c->qpu_inst_list);
54 last_inst(struct vc4_compile *c)
56 struct queued_qpu_inst *q =
57 (struct queued_qpu_inst *)c->qpu_inst_list.prev;
62 set_last_cond_add(struct vc4_compile *c, uint32_t cond)
64 *last_inst(c) = qpu_set_cond_add(*last_inst(c), cond);
68 * Some special registers can be read from either file, which lets us resolve
69 * raddr conflicts without extra MOVs.
72 swap_file(struct qpu_reg *src)
77 if (src->mux == QPU_MUX_SMALL_IMM) {
80 if (src->mux == QPU_MUX_A)
93 * This is used to resolve the fact that we might register-allocate two
94 * different operands of an instruction to the same physical register file
95 * even though instructions have only one field for the register file source
98 * In that case, we need to move one to a temporary that can be used in the
99 * instruction, instead. We reserve ra31/rb31 for this purpose.
102 fixup_raddr_conflict(struct vc4_compile *c,
104 struct qpu_reg *src0, struct qpu_reg *src1)
106 uint32_t mux0 = src0->mux == QPU_MUX_SMALL_IMM ? QPU_MUX_B : src0->mux;
107 uint32_t mux1 = src1->mux == QPU_MUX_SMALL_IMM ? QPU_MUX_B : src1->mux;
109 if (mux0 <= QPU_MUX_R5 ||
111 (src0->addr == src1->addr &&
112 src0->mux == src1->mux)) {
116 if (swap_file(src0) || swap_file(src1))
119 if (mux0 == QPU_MUX_A) {
120 queue(c, qpu_a_MOV(qpu_rb(31), *src0));
123 queue(c, qpu_a_MOV(qpu_ra(31), *src0));
129 vc4_generate_code(struct vc4_context *vc4, struct vc4_compile *c)
131 struct qpu_reg *temp_registers = vc4_register_allocate(vc4, c);
132 bool discard = false;
133 uint32_t inputs_remaining = c->num_inputs;
134 uint32_t vpm_read_fifo_count = 0;
135 uint32_t vpm_read_offset = 0;
136 int last_vpm_read_index = -1;
137 /* Map from the QIR ops enum order to QPU unpack bits. */
138 static const uint32_t unpack_map[] = {
147 list_inithead(&c->qpu_inst_list);
152 /* There's a 4-entry FIFO for VPMVCD reads, each of which can
153 * load up to 16 dwords (4 vec4s) per vertex.
155 while (inputs_remaining) {
156 uint32_t num_entries = MIN2(inputs_remaining, 16);
157 queue(c, qpu_load_imm_ui(qpu_vrsetup(),
160 ((num_entries & 0xf) << 20)));
161 inputs_remaining -= num_entries;
162 vpm_read_offset += num_entries;
163 vpm_read_fifo_count++;
165 assert(vpm_read_fifo_count <= 4);
167 queue(c, qpu_load_imm_ui(qpu_vwsetup(), 0x00001a00));
173 list_for_each_entry(struct qinst, qinst, &c->instructions, link) {
175 fprintf(stderr, "translating qinst to qpu: ");
176 qir_dump_inst(qinst);
177 fprintf(stderr, "\n");
180 static const struct {
183 #define A(name) [QOP_##name] = {QPU_A_##name}
184 #define M(name) [QOP_##name] = {QPU_M_##name}
214 struct qpu_reg src[4];
215 for (int i = 0; i < qir_get_op_nsrc(qinst->op); i++) {
216 int index = qinst->src[i].index;
217 switch (qinst->src[i].file) {
222 src[i] = temp_registers[index];
230 case QFILE_SMALL_IMM:
231 src[i].mux = QPU_MUX_SMALL_IMM;
232 src[i].addr = qpu_encode_small_immediate(qinst->src[i].index);
233 /* This should only have returned a valid
234 * small immediate field, not ~0 for failure.
236 assert(src[i].addr <= 47);
239 assert((int)qinst->src[i].index >=
240 last_vpm_read_index);
241 (void)last_vpm_read_index;
242 last_vpm_read_index = qinst->src[i].index;
243 src[i] = qpu_ra(QPU_R_VPM);
249 switch (qinst->dst.file) {
251 dst = qpu_ra(QPU_W_NOP);
254 dst = temp_registers[qinst->dst.index];
257 dst = qpu_ra(QPU_W_VPM);
261 case QFILE_SMALL_IMM:
262 assert(!"not reached");
268 /* Skip emitting the MOV if it's a no-op. */
269 if (qir_is_raw_mov(qinst) ||
270 dst.mux != src[0].mux || dst.addr != src[0].addr) {
271 queue(c, qpu_a_MOV(dst, src[0]));
279 queue(c, qpu_a_MOV(dst, src[0]));
280 set_last_cond_add(c, qinst->op - QOP_SEL_X_0_ZS +
283 queue(c, qpu_a_XOR(dst, qpu_r0(), qpu_r0()));
284 set_last_cond_add(c, ((qinst->op - QOP_SEL_X_0_ZS) ^
292 queue(c, qpu_a_MOV(dst, src[0]));
293 set_last_cond_add(c, qinst->op - QOP_SEL_X_Y_ZS +
296 queue(c, qpu_a_MOV(dst, src[1]));
297 set_last_cond_add(c, ((qinst->op - QOP_SEL_X_Y_ZS) ^
308 queue(c, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIP),
312 queue(c, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIPSQRT),
316 queue(c, qpu_a_MOV(qpu_rb(QPU_W_SFU_EXP),
320 queue(c, qpu_a_MOV(qpu_rb(QPU_W_SFU_LOG),
327 if (dst.mux != QPU_MUX_R4)
328 queue(c, qpu_a_MOV(dst, qpu_r4()));
332 case QOP_PACK_8888_F:
333 queue(c, qpu_m_MOV(dst, src[0]));
334 *last_inst(c) |= QPU_PM;
335 *last_inst(c) |= QPU_SET_FIELD(QPU_PACK_MUL_8888,
344 qpu_m_MOV(dst, src[0]) |
346 QPU_SET_FIELD(QPU_PACK_MUL_8A +
347 qinst->op - QOP_PACK_8A_F,
352 queue(c, qpu_a_ITOF(dst,
353 qpu_ra(QPU_R_XY_PIXEL_COORD)));
357 queue(c, qpu_a_ITOF(dst,
358 qpu_rb(QPU_R_XY_PIXEL_COORD)));
361 case QOP_FRAG_REV_FLAG:
362 queue(c, qpu_a_ITOF(dst,
363 qpu_rb(QPU_R_MS_REV_FLAGS)));
368 /* QOP_FRAG_Z/W don't emit instructions, just allocate
369 * the register to the Z/W payload.
373 case QOP_TLB_DISCARD_SETUP:
375 queue(c, qpu_a_MOV(src[0], src[0]));
376 *last_inst(c) |= QPU_SF;
379 case QOP_TLB_STENCIL_SETUP:
380 queue(c, qpu_a_MOV(qpu_ra(QPU_W_TLB_STENCIL_SETUP), src[0]));
383 case QOP_TLB_Z_WRITE:
384 queue(c, qpu_a_MOV(qpu_ra(QPU_W_TLB_Z), src[0]));
386 set_last_cond_add(c, QPU_COND_ZS);
390 case QOP_TLB_COLOR_READ:
392 *last_inst(c) = qpu_set_sig(*last_inst(c),
395 if (dst.mux != QPU_MUX_R4)
396 queue(c, qpu_a_MOV(dst, qpu_r4()));
399 case QOP_TLB_COLOR_WRITE:
400 queue(c, qpu_a_MOV(qpu_tlbc(), src[0]));
402 set_last_cond_add(c, QPU_COND_ZS);
407 queue(c, qpu_a_FADD(dst, src[0], qpu_r5()));
414 queue(c, qpu_a_MOV(qpu_rb(QPU_W_TMU0_S +
415 (qinst->op - QOP_TEX_S)),
420 fixup_raddr_conflict(c, dst, &src[0], &src[1]);
421 queue(c, qpu_a_ADD(qpu_rb(QPU_W_TMU0_S), src[0], src[1]));
426 *last_inst(c) = qpu_set_sig(*last_inst(c),
428 if (dst.mux != QPU_MUX_R4)
429 queue(c, qpu_a_MOV(dst, qpu_r4()));
432 case QOP_UNPACK_8A_F:
433 case QOP_UNPACK_8B_F:
434 case QOP_UNPACK_8C_F:
435 case QOP_UNPACK_8D_F:
436 case QOP_UNPACK_16A_F:
437 case QOP_UNPACK_16B_F: {
438 if (src[0].mux == QPU_MUX_R4) {
439 queue(c, qpu_a_MOV(dst, src[0]));
440 *last_inst(c) |= QPU_PM;
441 *last_inst(c) |= QPU_SET_FIELD(QPU_UNPACK_8A +
446 assert(src[0].mux == QPU_MUX_A);
448 /* Since we're setting the pack bits, if the
449 * destination is in A it would get re-packed.
451 queue(c, qpu_a_FMAX((dst.mux == QPU_MUX_A ?
455 QPU_SET_FIELD(unpack_map[qinst->op -
459 if (dst.mux == QPU_MUX_A) {
460 queue(c, qpu_a_MOV(dst, qpu_rb(31)));
466 case QOP_UNPACK_8A_I:
467 case QOP_UNPACK_8B_I:
468 case QOP_UNPACK_8C_I:
469 case QOP_UNPACK_8D_I:
470 case QOP_UNPACK_16A_I:
471 case QOP_UNPACK_16B_I: {
472 assert(src[0].mux == QPU_MUX_A);
474 /* Since we're setting the pack bits, if the
475 * destination is in A it would get re-packed.
477 queue(c, qpu_a_MOV((dst.mux == QPU_MUX_A ?
478 qpu_rb(31) : dst), src[0]));
479 *last_inst(c) |= QPU_SET_FIELD(unpack_map[qinst->op -
483 if (dst.mux == QPU_MUX_A) {
484 queue(c, qpu_a_MOV(dst, qpu_rb(31)));
490 assert(qinst->op < ARRAY_SIZE(translate));
491 assert(translate[qinst->op].op != 0); /* NOPs */
493 /* If we have only one source, put it in the second
494 * argument slot as well so that we don't take up
495 * another raddr just to get unused data.
497 if (qir_get_op_nsrc(qinst->op) == 1)
500 fixup_raddr_conflict(c, dst, &src[0], &src[1]);
502 if (qir_is_mul(qinst)) {
503 queue(c, qpu_m_alu2(translate[qinst->op].op,
506 if (qinst->dst.pack) {
507 *last_inst(c) |= QPU_PM;
508 *last_inst(c) |= QPU_SET_FIELD(qinst->dst.pack,
512 queue(c, qpu_a_alu2(translate[qinst->op].op,
515 if (qinst->dst.pack) {
516 assert(dst.mux == QPU_MUX_A);
517 *last_inst(c) |= QPU_SET_FIELD(qinst->dst.pack,
526 assert(!qir_is_multi_instruction(qinst));
527 *last_inst(c) |= QPU_SF;
531 qpu_schedule_instructions(c);
533 /* thread end can't have VPM write or read */
534 if (QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
535 QPU_WADDR_ADD) == QPU_W_VPM ||
536 QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
537 QPU_WADDR_MUL) == QPU_W_VPM ||
538 QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
539 QPU_RADDR_A) == QPU_R_VPM ||
540 QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
541 QPU_RADDR_B) == QPU_R_VPM) {
542 qpu_serialize_one_inst(c, qpu_NOP());
545 /* thread end can't have uniform read */
546 if (QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
547 QPU_RADDR_A) == QPU_R_UNIF ||
548 QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],
549 QPU_RADDR_B) == QPU_R_UNIF) {
550 qpu_serialize_one_inst(c, qpu_NOP());
553 /* thread end can't have TLB operations */
554 if (qpu_inst_is_tlb(c->qpu_insts[c->qpu_inst_count - 1]))
555 qpu_serialize_one_inst(c, qpu_NOP());
557 c->qpu_insts[c->qpu_inst_count - 1] =
558 qpu_set_sig(c->qpu_insts[c->qpu_inst_count - 1],
560 qpu_serialize_one_inst(c, qpu_NOP());
561 qpu_serialize_one_inst(c, qpu_NOP());
568 c->qpu_insts[c->qpu_inst_count - 1] =
569 qpu_set_sig(c->qpu_insts[c->qpu_inst_count - 1],
570 QPU_SIG_SCOREBOARD_UNLOCK);
574 if (vc4_debug & VC4_DEBUG_QPU)
577 vc4_qpu_validate(c->qpu_insts, c->qpu_inst_count);
579 free(temp_registers);