Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_qpu_emit.c
4570 views
/*1* Copyright © 2014 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <inttypes.h>2425#include "vc4_context.h"26#include "vc4_qir.h"27#include "vc4_qpu.h"28#include "util/ralloc.h"2930static void31vc4_dump_program(struct vc4_compile *c)32{33fprintf(stderr, "%s prog %d/%d QPU:\n",34qir_get_stage_name(c->stage),35c->program_id, c->variant_id);3637for (int i = 0; i < c->qpu_inst_count; i++) {38fprintf(stderr, "0x%016"PRIx64" ", c->qpu_insts[i]);39vc4_qpu_disasm(&c->qpu_insts[i], 1);40fprintf(stderr, "\n");41}42fprintf(stderr, "\n");43}4445static void46queue(struct qblock *block, uint64_t inst)47{48struct queued_qpu_inst *q = rzalloc(block, struct queued_qpu_inst);49q->inst = inst;50list_addtail(&q->link, &block->qpu_inst_list);51}5253static uint64_t *54last_inst(struct qblock *block)55{56struct queued_qpu_inst *q =57(struct queued_qpu_inst *)block->qpu_inst_list.prev;58return &q->inst;59}6061static void62set_last_cond_add(struct qblock *block, uint32_t cond)63{64*last_inst(block) = qpu_set_cond_add(*last_inst(block), cond);65}6667static void68set_last_cond_mul(struct qblock *block, uint32_t cond)69{70*last_inst(block) = qpu_set_cond_mul(*last_inst(block), cond);71}7273/**74* Some special registers can be read from either file, which lets us resolve75* raddr conflicts without extra MOVs.76*/77static bool78swap_file(struct qpu_reg *src)79{80switch (src->addr) {81case QPU_R_UNIF:82case QPU_R_VARY:83if (src->mux == QPU_MUX_SMALL_IMM) {84return false;85} else {86if (src->mux == QPU_MUX_A)87src->mux = QPU_MUX_B;88else89src->mux = QPU_MUX_A;90return true;91}9293default:94return false;95}96}9798/**99* Sets up the VPM read FIFO before we do any VPM read.100*101* VPM reads (vertex attribute input) and VPM writes (varyings output) from102* the QPU reuse the VRI (varying interpolation) block's FIFOs to talk to the103* VPM block. In the VS/CS (unlike in the FS), the block starts out104* uninitialized, and you need to emit setup to the block before any VPM105* reads/writes.106*107* VRI has a FIFO in each direction, with each FIFO able to hold four108* 32-bit-per-vertex values. VPM reads come through the read FIFO and VPM109* writes go through the write FIFO. The read/write setup values from QPU go110* through the write FIFO as well, with a sideband signal indicating that111* they're setup values. Once a read setup reaches the other side of the112* FIFO, the VPM block will start asynchronously reading vertex attributes and113* filling the read FIFO -- that way hopefully the QPU doesn't have to block114* on reads later.115*116* VPM read setup can configure 16 32-bit-per-vertex values to be read at a117* time, which is 4 vec4s. If more than that is being read (since we support118* 8 vec4 vertex attributes), then multiple read setup writes need to be done.119*120* The existence of the FIFO makes it seem like you should be able to emit121* both setups for the 5-8 attribute cases and then do all the attribute122* reads. However, once the setup value makes it to the other end of the123* write FIFO, it will immediately update the VPM block's setup register.124* That updated setup register would be used for read FIFO fills from then on,125* breaking whatever remaining VPM values were supposed to be read into the126* read FIFO from the previous attribute set.127*128* As a result, we need to emit the read setup, pull every VPM read value from129* that setup, and only then emit the second setup if applicable.130*/131static void132setup_for_vpm_read(struct vc4_compile *c, struct qblock *block)133{134if (c->num_inputs_in_fifo) {135c->num_inputs_in_fifo--;136return;137}138139c->num_inputs_in_fifo = MIN2(c->num_inputs_remaining, 16);140141queue(block,142qpu_load_imm_ui(qpu_vrsetup(),143c->vpm_read_offset |1440x00001a00 |145((c->num_inputs_in_fifo & 0xf) << 20)));146c->num_inputs_remaining -= c->num_inputs_in_fifo;147c->vpm_read_offset += c->num_inputs_in_fifo;148149c->num_inputs_in_fifo--;150}151152/**153* This is used to resolve the fact that we might register-allocate two154* different operands of an instruction to the same physical register file155* even though instructions have only one field for the register file source156* address.157*158* In that case, we need to move one to a temporary that can be used in the159* instruction, instead. We reserve ra14/rb14 for this purpose.160*/161static void162fixup_raddr_conflict(struct qblock *block,163struct qpu_reg dst,164struct qpu_reg *src0, struct qpu_reg *src1,165struct qinst *inst, uint64_t *unpack)166{167uint32_t mux0 = src0->mux == QPU_MUX_SMALL_IMM ? QPU_MUX_B : src0->mux;168uint32_t mux1 = src1->mux == QPU_MUX_SMALL_IMM ? QPU_MUX_B : src1->mux;169170if (mux0 <= QPU_MUX_R5 ||171mux0 != mux1 ||172(src0->addr == src1->addr &&173src0->mux == src1->mux)) {174return;175}176177if (swap_file(src0) || swap_file(src1))178return;179180if (mux0 == QPU_MUX_A) {181/* Make sure we use the same type of MOV as the instruction,182* in case of unpacks.183*/184if (qir_is_float_input(inst))185queue(block, qpu_a_FMAX(qpu_rb(14), *src0, *src0));186else187queue(block, qpu_a_MOV(qpu_rb(14), *src0));188189/* If we had an unpack on this A-file source, we need to put190* it into this MOV, not into the later move from regfile B.191*/192if (inst->src[0].pack) {193*last_inst(block) |= *unpack;194*unpack = 0;195}196*src0 = qpu_rb(14);197} else {198queue(block, qpu_a_MOV(qpu_ra(14), *src0));199*src0 = qpu_ra(14);200}201}202203static void204set_last_dst_pack(struct qblock *block, struct qinst *inst)205{206ASSERTED bool had_pm = *last_inst(block) & QPU_PM;207ASSERTED bool had_ws = *last_inst(block) & QPU_WS;208ASSERTED uint32_t unpack = QPU_GET_FIELD(*last_inst(block), QPU_UNPACK);209210if (!inst->dst.pack)211return;212213*last_inst(block) |= QPU_SET_FIELD(inst->dst.pack, QPU_PACK);214215if (qir_is_mul(inst)) {216assert(!unpack || had_pm);217*last_inst(block) |= QPU_PM;218} else {219assert(!unpack || !had_pm);220assert(!had_ws); /* dst must be a-file to pack. */221}222}223224static void225handle_r4_qpu_write(struct qblock *block, struct qinst *qinst,226struct qpu_reg dst)227{228if (dst.mux != QPU_MUX_R4) {229queue(block, qpu_a_MOV(dst, qpu_r4()));230set_last_cond_add(block, qinst->cond);231} else {232assert(qinst->cond == QPU_COND_ALWAYS);233if (qinst->sf)234queue(block, qpu_a_MOV(qpu_ra(QPU_W_NOP), qpu_r4()));235}236}237238static void239vc4_generate_code_block(struct vc4_compile *c,240struct qblock *block,241struct qpu_reg *temp_registers)242{243int last_vpm_read_index = -1;244245qir_for_each_inst(qinst, block) {246#if 0247fprintf(stderr, "translating qinst to qpu: ");248qir_dump_inst(qinst);249fprintf(stderr, "\n");250#endif251252static const struct {253uint32_t op;254} translate[] = {255#define A(name) [QOP_##name] = {QPU_A_##name}256#define M(name) [QOP_##name] = {QPU_M_##name}257A(FADD),258A(FSUB),259A(FMIN),260A(FMAX),261A(FMINABS),262A(FMAXABS),263A(FTOI),264A(ITOF),265A(ADD),266A(SUB),267A(SHL),268A(SHR),269A(ASR),270A(MIN),271A(MAX),272A(AND),273A(OR),274A(XOR),275A(NOT),276277M(FMUL),278M(V8MULD),279M(V8MIN),280M(V8MAX),281M(V8ADDS),282M(V8SUBS),283M(MUL24),284285/* If we replicate src[0] out to src[1], this works286* out the same as a MOV.287*/288[QOP_MOV] = { QPU_A_OR },289[QOP_FMOV] = { QPU_A_FMAX },290[QOP_MMOV] = { QPU_M_V8MIN },291292[QOP_MIN_NOIMM] = { QPU_A_MIN },293};294295uint64_t unpack = 0;296struct qpu_reg src[ARRAY_SIZE(qinst->src)];297for (int i = 0; i < qir_get_nsrc(qinst); i++) {298int index = qinst->src[i].index;299switch (qinst->src[i].file) {300case QFILE_NULL:301case QFILE_LOAD_IMM:302src[i] = qpu_rn(0);303break;304case QFILE_TEMP:305src[i] = temp_registers[index];306if (qinst->src[i].pack) {307assert(!unpack ||308unpack == qinst->src[i].pack);309unpack = QPU_SET_FIELD(qinst->src[i].pack,310QPU_UNPACK);311if (src[i].mux == QPU_MUX_R4)312unpack |= QPU_PM;313}314break;315case QFILE_UNIF:316src[i] = qpu_unif();317break;318case QFILE_VARY:319src[i] = qpu_vary();320break;321case QFILE_SMALL_IMM:322src[i].mux = QPU_MUX_SMALL_IMM;323src[i].addr = qpu_encode_small_immediate(qinst->src[i].index);324/* This should only have returned a valid325* small immediate field, not ~0 for failure.326*/327assert(src[i].addr <= 47);328break;329case QFILE_VPM:330setup_for_vpm_read(c, block);331assert((int)qinst->src[i].index >=332last_vpm_read_index);333(void)last_vpm_read_index;334last_vpm_read_index = qinst->src[i].index;335src[i] = qpu_ra(QPU_R_VPM);336break;337338case QFILE_FRAG_X:339src[i] = qpu_ra(QPU_R_XY_PIXEL_COORD);340break;341case QFILE_FRAG_Y:342src[i] = qpu_rb(QPU_R_XY_PIXEL_COORD);343break;344case QFILE_FRAG_REV_FLAG:345src[i] = qpu_rb(QPU_R_MS_REV_FLAGS);346break;347case QFILE_QPU_ELEMENT:348src[i] = qpu_ra(QPU_R_ELEM_QPU);349break;350351case QFILE_TLB_COLOR_WRITE:352case QFILE_TLB_COLOR_WRITE_MS:353case QFILE_TLB_Z_WRITE:354case QFILE_TLB_STENCIL_SETUP:355case QFILE_TEX_S:356case QFILE_TEX_S_DIRECT:357case QFILE_TEX_T:358case QFILE_TEX_R:359case QFILE_TEX_B:360unreachable("bad qir src file");361}362}363364struct qpu_reg dst;365switch (qinst->dst.file) {366case QFILE_NULL:367dst = qpu_ra(QPU_W_NOP);368break;369case QFILE_TEMP:370dst = temp_registers[qinst->dst.index];371break;372case QFILE_VPM:373dst = qpu_ra(QPU_W_VPM);374break;375376case QFILE_TLB_COLOR_WRITE:377dst = qpu_tlbc();378break;379380case QFILE_TLB_COLOR_WRITE_MS:381dst = qpu_tlbc_ms();382break;383384case QFILE_TLB_Z_WRITE:385dst = qpu_ra(QPU_W_TLB_Z);386break;387388case QFILE_TLB_STENCIL_SETUP:389dst = qpu_ra(QPU_W_TLB_STENCIL_SETUP);390break;391392case QFILE_TEX_S:393case QFILE_TEX_S_DIRECT:394dst = qpu_rb(QPU_W_TMU0_S);395break;396397case QFILE_TEX_T:398dst = qpu_rb(QPU_W_TMU0_T);399break;400401case QFILE_TEX_R:402dst = qpu_rb(QPU_W_TMU0_R);403break;404405case QFILE_TEX_B:406dst = qpu_rb(QPU_W_TMU0_B);407break;408409case QFILE_VARY:410case QFILE_UNIF:411case QFILE_SMALL_IMM:412case QFILE_LOAD_IMM:413case QFILE_FRAG_X:414case QFILE_FRAG_Y:415case QFILE_FRAG_REV_FLAG:416case QFILE_QPU_ELEMENT:417assert(!"not reached");418break;419}420421ASSERTED bool handled_qinst_cond = false;422423switch (qinst->op) {424case QOP_RCP:425case QOP_RSQ:426case QOP_EXP2:427case QOP_LOG2:428switch (qinst->op) {429case QOP_RCP:430queue(block, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIP),431src[0]) | unpack);432break;433case QOP_RSQ:434queue(block, qpu_a_MOV(qpu_rb(QPU_W_SFU_RECIPSQRT),435src[0]) | unpack);436break;437case QOP_EXP2:438queue(block, qpu_a_MOV(qpu_rb(QPU_W_SFU_EXP),439src[0]) | unpack);440break;441case QOP_LOG2:442queue(block, qpu_a_MOV(qpu_rb(QPU_W_SFU_LOG),443src[0]) | unpack);444break;445default:446abort();447}448449handle_r4_qpu_write(block, qinst, dst);450handled_qinst_cond = true;451452break;453454case QOP_LOAD_IMM:455assert(qinst->src[0].file == QFILE_LOAD_IMM);456queue(block, qpu_load_imm_ui(dst, qinst->src[0].index));457break;458459case QOP_LOAD_IMM_U2:460queue(block, qpu_load_imm_u2(dst, qinst->src[0].index));461break;462463case QOP_LOAD_IMM_I2:464queue(block, qpu_load_imm_i2(dst, qinst->src[0].index));465break;466467case QOP_ROT_MUL:468/* Rotation at the hardware level occurs on the inputs469* to the MUL unit, and they must be accumulators in470* order to have the time necessary to move things.471*/472assert(src[0].mux <= QPU_MUX_R3);473474queue(block,475qpu_m_rot(dst, src[0], qinst->src[1].index -476QPU_SMALL_IMM_MUL_ROT) | unpack);477set_last_cond_mul(block, qinst->cond);478handled_qinst_cond = true;479set_last_dst_pack(block, qinst);480break;481482case QOP_MS_MASK:483src[1] = qpu_ra(QPU_R_MS_REV_FLAGS);484fixup_raddr_conflict(block, dst, &src[0], &src[1],485qinst, &unpack);486queue(block, qpu_a_AND(qpu_ra(QPU_W_MS_FLAGS),487src[0], src[1]) | unpack);488break;489490case QOP_FRAG_Z:491case QOP_FRAG_W:492/* QOP_FRAG_Z/W don't emit instructions, just allocate493* the register to the Z/W payload.494*/495break;496497case QOP_TLB_COLOR_READ:498queue(block, qpu_NOP());499*last_inst(block) = qpu_set_sig(*last_inst(block),500QPU_SIG_COLOR_LOAD);501handle_r4_qpu_write(block, qinst, dst);502handled_qinst_cond = true;503break;504505case QOP_VARY_ADD_C:506queue(block, qpu_a_FADD(dst, src[0], qpu_r5()) | unpack);507break;508509510case QOP_TEX_RESULT:511queue(block, qpu_NOP());512*last_inst(block) = qpu_set_sig(*last_inst(block),513QPU_SIG_LOAD_TMU0);514handle_r4_qpu_write(block, qinst, dst);515handled_qinst_cond = true;516break;517518case QOP_THRSW:519queue(block, qpu_NOP());520*last_inst(block) = qpu_set_sig(*last_inst(block),521QPU_SIG_THREAD_SWITCH);522c->last_thrsw = last_inst(block);523break;524525case QOP_BRANCH:526/* The branch target will be updated at QPU scheduling527* time.528*/529queue(block, (qpu_branch(qinst->cond, 0) |530QPU_BRANCH_REL));531handled_qinst_cond = true;532break;533534case QOP_UNIFORMS_RESET:535fixup_raddr_conflict(block, dst, &src[0], &src[1],536qinst, &unpack);537538queue(block, qpu_a_ADD(qpu_ra(QPU_W_UNIFORMS_ADDRESS),539src[0], src[1]));540break;541542default:543assert(qinst->op < ARRAY_SIZE(translate));544assert(translate[qinst->op].op != 0); /* NOPs */545546/* Skip emitting the MOV if it's a no-op. */547if (qir_is_raw_mov(qinst) &&548dst.mux == src[0].mux && dst.addr == src[0].addr) {549break;550}551552/* If we have only one source, put it in the second553* argument slot as well so that we don't take up554* another raddr just to get unused data.555*/556if (qir_get_non_sideband_nsrc(qinst) == 1)557src[1] = src[0];558559fixup_raddr_conflict(block, dst, &src[0], &src[1],560qinst, &unpack);561562if (qir_is_mul(qinst)) {563queue(block, qpu_m_alu2(translate[qinst->op].op,564dst,565src[0], src[1]) | unpack);566set_last_cond_mul(block, qinst->cond);567} else {568queue(block, qpu_a_alu2(translate[qinst->op].op,569dst,570src[0], src[1]) | unpack);571set_last_cond_add(block, qinst->cond);572}573handled_qinst_cond = true;574set_last_dst_pack(block, qinst);575576break;577}578579assert(qinst->cond == QPU_COND_ALWAYS ||580handled_qinst_cond);581582if (qinst->sf)583*last_inst(block) |= QPU_SF;584}585}586587void588vc4_generate_code(struct vc4_context *vc4, struct vc4_compile *c)589{590struct qblock *start_block = list_first_entry(&c->blocks,591struct qblock, link);592593struct qpu_reg *temp_registers = vc4_register_allocate(vc4, c);594if (!temp_registers)595return;596597switch (c->stage) {598case QSTAGE_VERT:599case QSTAGE_COORD:600c->num_inputs_remaining = c->num_inputs;601queue(start_block, qpu_load_imm_ui(qpu_vwsetup(), 0x00001a00));602break;603case QSTAGE_FRAG:604break;605}606607qir_for_each_block(block, c)608vc4_generate_code_block(c, block, temp_registers);609610/* Switch the last SIG_THRSW instruction to SIG_LAST_THRSW.611*612* LAST_THRSW is a new signal in BCM2708B0 (including Raspberry Pi)613* that ensures that a later thread doesn't try to lock the scoreboard614* and terminate before an earlier-spawned thread on the same QPU, by615* delaying switching back to the later shader until earlier has616* finished. Otherwise, if the earlier thread was hitting the same617* quad, the scoreboard would deadlock.618*/619if (c->last_thrsw) {620assert(QPU_GET_FIELD(*c->last_thrsw, QPU_SIG) ==621QPU_SIG_THREAD_SWITCH);622*c->last_thrsw = ((*c->last_thrsw & ~QPU_SIG_MASK) |623QPU_SET_FIELD(QPU_SIG_LAST_THREAD_SWITCH,624QPU_SIG));625}626627uint32_t cycles = qpu_schedule_instructions(c);628uint32_t inst_count_at_schedule_time = c->qpu_inst_count;629630/* thread end can't have VPM write or read */631if (QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],632QPU_WADDR_ADD) == QPU_W_VPM ||633QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],634QPU_WADDR_MUL) == QPU_W_VPM ||635QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],636QPU_RADDR_A) == QPU_R_VPM ||637QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],638QPU_RADDR_B) == QPU_R_VPM) {639qpu_serialize_one_inst(c, qpu_NOP());640}641642/* thread end can't have uniform read */643if (QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],644QPU_RADDR_A) == QPU_R_UNIF ||645QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],646QPU_RADDR_B) == QPU_R_UNIF) {647qpu_serialize_one_inst(c, qpu_NOP());648}649650/* thread end can't have TLB operations */651if (qpu_inst_is_tlb(c->qpu_insts[c->qpu_inst_count - 1]))652qpu_serialize_one_inst(c, qpu_NOP());653654/* Make sure there's no existing signal set (like for a small655* immediate)656*/657if (QPU_GET_FIELD(c->qpu_insts[c->qpu_inst_count - 1],658QPU_SIG) != QPU_SIG_NONE) {659qpu_serialize_one_inst(c, qpu_NOP());660}661662c->qpu_insts[c->qpu_inst_count - 1] =663qpu_set_sig(c->qpu_insts[c->qpu_inst_count - 1],664QPU_SIG_PROG_END);665qpu_serialize_one_inst(c, qpu_NOP());666qpu_serialize_one_inst(c, qpu_NOP());667668switch (c->stage) {669case QSTAGE_VERT:670case QSTAGE_COORD:671break;672case QSTAGE_FRAG:673c->qpu_insts[c->qpu_inst_count - 1] =674qpu_set_sig(c->qpu_insts[c->qpu_inst_count - 1],675QPU_SIG_SCOREBOARD_UNLOCK);676break;677}678679cycles += c->qpu_inst_count - inst_count_at_schedule_time;680681if (vc4_debug & VC4_DEBUG_SHADERDB) {682fprintf(stderr, "SHADER-DB: %s prog %d/%d: %d estimated cycles\n",683qir_get_stage_name(c->stage),684c->program_id, c->variant_id,685cycles);686}687688if (vc4_debug & VC4_DEBUG_QPU)689vc4_dump_program(c);690691vc4_qpu_validate(c->qpu_insts, c->qpu_inst_count);692693free(temp_registers);694}695696697