Path: blob/21.2-virgl/src/gallium/drivers/r600/r600_asm.c
4570 views
/*1* Copyright 2010 Jerome Glisse <[email protected]>2*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* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the 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 NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/22#include "r600_sq.h"23#include "r600_opcodes.h"24#include "r600_formats.h"25#include "r600_shader.h"26#include "r600d.h"2728#include <errno.h>29#include "util/u_bitcast.h"30#include "util/u_dump.h"31#include "util/u_memory.h"32#include "util/u_math.h"33#include "pipe/p_shader_tokens.h"3435#include "sb/sb_public.h"3637#define NUM_OF_CYCLES 338#define NUM_OF_COMPONENTS 43940static inline bool alu_writes(struct r600_bytecode_alu *alu)41{42return alu->dst.write || alu->is_op3;43}4445static inline unsigned int r600_bytecode_get_num_operands(const struct r600_bytecode_alu *alu)46{47return r600_isa_alu(alu->op)->src_count;48}4950static struct r600_bytecode_cf *r600_bytecode_cf(void)51{52struct r600_bytecode_cf *cf = CALLOC_STRUCT(r600_bytecode_cf);5354if (!cf)55return NULL;56list_inithead(&cf->list);57list_inithead(&cf->alu);58list_inithead(&cf->vtx);59list_inithead(&cf->tex);60list_inithead(&cf->gds);61return cf;62}6364static struct r600_bytecode_alu *r600_bytecode_alu(void)65{66struct r600_bytecode_alu *alu = CALLOC_STRUCT(r600_bytecode_alu);6768if (!alu)69return NULL;70list_inithead(&alu->list);71return alu;72}7374static struct r600_bytecode_vtx *r600_bytecode_vtx(void)75{76struct r600_bytecode_vtx *vtx = CALLOC_STRUCT(r600_bytecode_vtx);7778if (!vtx)79return NULL;80list_inithead(&vtx->list);81return vtx;82}8384static struct r600_bytecode_tex *r600_bytecode_tex(void)85{86struct r600_bytecode_tex *tex = CALLOC_STRUCT(r600_bytecode_tex);8788if (!tex)89return NULL;90list_inithead(&tex->list);91return tex;92}9394static struct r600_bytecode_gds *r600_bytecode_gds(void)95{96struct r600_bytecode_gds *gds = CALLOC_STRUCT(r600_bytecode_gds);9798if (gds == NULL)99return NULL;100list_inithead(&gds->list);101return gds;102}103104static unsigned stack_entry_size(enum radeon_family chip) {105/* Wavefront size:106* 64: R600/RV670/RV770/Cypress/R740/Barts/Turks/Caicos/107* Aruba/Sumo/Sumo2/redwood/juniper108* 32: R630/R730/R710/Palm/Cedar109* 16: R610/Rs780110*111* Stack row size:112* Wavefront Size 16 32 48 64113* Columns per Row (R6xx/R7xx/R8xx only) 8 8 4 4114* Columns per Row (R9xx+) 8 4 4 4 */115116switch (chip) {117/* FIXME: are some chips missing here? */118/* wavefront size 16 */119case CHIP_RV610:120case CHIP_RS780:121case CHIP_RV620:122case CHIP_RS880:123/* wavefront size 32 */124case CHIP_RV630:125case CHIP_RV635:126case CHIP_RV730:127case CHIP_RV710:128case CHIP_PALM:129case CHIP_CEDAR:130return 8;131132/* wavefront size 64 */133default:134return 4;135}136}137138void r600_bytecode_init(struct r600_bytecode *bc,139enum chip_class chip_class,140enum radeon_family family,141bool has_compressed_msaa_texturing)142{143static unsigned next_shader_id = 0;144145bc->debug_id = ++next_shader_id;146147if ((chip_class == R600) &&148(family != CHIP_RV670 && family != CHIP_RS780 && family != CHIP_RS880)) {149bc->ar_handling = AR_HANDLE_RV6XX;150bc->r6xx_nop_after_rel_dst = 1;151} else {152bc->ar_handling = AR_HANDLE_NORMAL;153bc->r6xx_nop_after_rel_dst = 0;154}155156list_inithead(&bc->cf);157bc->chip_class = chip_class;158bc->family = family;159bc->has_compressed_msaa_texturing = has_compressed_msaa_texturing;160bc->stack.entry_size = stack_entry_size(family);161}162163int r600_bytecode_add_cf(struct r600_bytecode *bc)164{165struct r600_bytecode_cf *cf = r600_bytecode_cf();166167if (!cf)168return -ENOMEM;169list_addtail(&cf->list, &bc->cf);170if (bc->cf_last) {171cf->id = bc->cf_last->id + 2;172if (bc->cf_last->eg_alu_extended) {173/* take into account extended alu size */174cf->id += 2;175bc->ndw += 2;176}177}178bc->cf_last = cf;179bc->ncf++;180bc->ndw += 2;181bc->force_add_cf = 0;182bc->ar_loaded = 0;183return 0;184}185186int r600_bytecode_add_output(struct r600_bytecode *bc,187const struct r600_bytecode_output *output)188{189int r;190191if (output->gpr >= bc->ngpr)192bc->ngpr = output->gpr + 1;193194if (bc->cf_last && (bc->cf_last->op == output->op ||195(bc->cf_last->op == CF_OP_EXPORT &&196output->op == CF_OP_EXPORT_DONE)) &&197output->type == bc->cf_last->output.type &&198output->elem_size == bc->cf_last->output.elem_size &&199output->swizzle_x == bc->cf_last->output.swizzle_x &&200output->swizzle_y == bc->cf_last->output.swizzle_y &&201output->swizzle_z == bc->cf_last->output.swizzle_z &&202output->swizzle_w == bc->cf_last->output.swizzle_w &&203output->comp_mask == bc->cf_last->output.comp_mask &&204(output->burst_count + bc->cf_last->output.burst_count) <= 16) {205206if ((output->gpr + output->burst_count) == bc->cf_last->output.gpr &&207(output->array_base + output->burst_count) == bc->cf_last->output.array_base) {208209bc->cf_last->op = bc->cf_last->output.op = output->op;210bc->cf_last->output.gpr = output->gpr;211bc->cf_last->output.array_base = output->array_base;212bc->cf_last->output.burst_count += output->burst_count;213return 0;214215} else if (output->gpr == (bc->cf_last->output.gpr + bc->cf_last->output.burst_count) &&216output->array_base == (bc->cf_last->output.array_base + bc->cf_last->output.burst_count)) {217218bc->cf_last->op = bc->cf_last->output.op = output->op;219bc->cf_last->output.burst_count += output->burst_count;220return 0;221}222}223224r = r600_bytecode_add_cf(bc);225if (r)226return r;227bc->cf_last->op = output->op;228memcpy(&bc->cf_last->output, output, sizeof(struct r600_bytecode_output));229bc->cf_last->barrier = 1;230return 0;231}232233int r600_bytecode_add_pending_output(struct r600_bytecode *bc,234const struct r600_bytecode_output *output)235{236assert(bc->n_pending_outputs + 1 < ARRAY_SIZE(bc->pending_outputs));237bc->pending_outputs[bc->n_pending_outputs++] = *output;238239return 0;240}241242void r600_bytecode_need_wait_ack(struct r600_bytecode *bc, boolean need_wait_ack)243{244bc->need_wait_ack = need_wait_ack;245}246247boolean r600_bytecode_get_need_wait_ack(struct r600_bytecode *bc)248{249return bc->need_wait_ack;250}251252/* alu instructions that can ony exits once per group */253static int is_alu_once_inst(struct r600_bytecode_alu *alu)254{255return r600_isa_alu(alu->op)->flags & (AF_KILL | AF_PRED) || alu->is_lds_idx_op || alu->op == ALU_OP0_GROUP_BARRIER;256}257258static int is_alu_reduction_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)259{260return (r600_isa_alu(alu->op)->flags & AF_REPL) &&261(r600_isa_alu_slots(bc->isa->hw_class, alu->op) == AF_4V);262}263264static int is_alu_mova_inst(struct r600_bytecode_alu *alu)265{266return r600_isa_alu(alu->op)->flags & AF_MOVA;267}268269static int alu_uses_rel(struct r600_bytecode_alu *alu)270{271unsigned num_src = r600_bytecode_get_num_operands(alu);272unsigned src;273274if (alu->dst.rel) {275return 1;276}277278for (src = 0; src < num_src; ++src) {279if (alu->src[src].rel) {280return 1;281}282}283return 0;284}285286static int is_lds_read(int sel)287{288return sel == EG_V_SQ_ALU_SRC_LDS_OQ_A_POP || sel == EG_V_SQ_ALU_SRC_LDS_OQ_B_POP;289}290291static int alu_uses_lds(struct r600_bytecode_alu *alu)292{293unsigned num_src = r600_bytecode_get_num_operands(alu);294unsigned src;295296for (src = 0; src < num_src; ++src) {297if (is_lds_read(alu->src[src].sel)) {298return 1;299}300}301return 0;302}303304static int is_alu_64bit_inst(struct r600_bytecode_alu *alu)305{306const struct alu_op_info *op = r600_isa_alu(alu->op);307return (op->flags & AF_64);308}309310static int is_alu_vec_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)311{312unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);313return !(slots & AF_S);314}315316static int is_alu_trans_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)317{318unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);319return !(slots & AF_V);320}321322/* alu instructions that can execute on any unit */323static int is_alu_any_unit_inst(struct r600_bytecode *bc, struct r600_bytecode_alu *alu)324{325unsigned slots = r600_isa_alu_slots(bc->isa->hw_class, alu->op);326return slots == AF_VS;327}328329static int is_nop_inst(struct r600_bytecode_alu *alu)330{331return alu->op == ALU_OP0_NOP;332}333334static int assign_alu_units(struct r600_bytecode *bc, struct r600_bytecode_alu *alu_first,335struct r600_bytecode_alu *assignment[5])336{337struct r600_bytecode_alu *alu;338unsigned i, chan, trans;339int max_slots = bc->chip_class == CAYMAN ? 4 : 5;340341for (i = 0; i < max_slots; i++)342assignment[i] = NULL;343344for (alu = alu_first; alu; alu = LIST_ENTRY(struct r600_bytecode_alu, alu->list.next, list)) {345chan = alu->dst.chan;346if (max_slots == 4)347trans = 0;348else if (is_alu_trans_unit_inst(bc, alu))349trans = 1;350else if (is_alu_vec_unit_inst(bc, alu))351trans = 0;352else if (assignment[chan])353trans = 1; /* Assume ALU_INST_PREFER_VECTOR. */354else355trans = 0;356357if (trans) {358if (assignment[4]) {359assert(0); /* ALU.Trans has already been allocated. */360return -1;361}362assignment[4] = alu;363} else {364if (assignment[chan]) {365assert(0); /* ALU.chan has already been allocated. */366return -1;367}368assignment[chan] = alu;369}370371if (alu->last)372break;373}374return 0;375}376377struct alu_bank_swizzle {378int hw_gpr[NUM_OF_CYCLES][NUM_OF_COMPONENTS];379int hw_cfile_addr[4];380int hw_cfile_elem[4];381};382383static const unsigned cycle_for_bank_swizzle_vec[][3] = {384[SQ_ALU_VEC_012] = { 0, 1, 2 },385[SQ_ALU_VEC_021] = { 0, 2, 1 },386[SQ_ALU_VEC_120] = { 1, 2, 0 },387[SQ_ALU_VEC_102] = { 1, 0, 2 },388[SQ_ALU_VEC_201] = { 2, 0, 1 },389[SQ_ALU_VEC_210] = { 2, 1, 0 }390};391392static const unsigned cycle_for_bank_swizzle_scl[][3] = {393[SQ_ALU_SCL_210] = { 2, 1, 0 },394[SQ_ALU_SCL_122] = { 1, 2, 2 },395[SQ_ALU_SCL_212] = { 2, 1, 2 },396[SQ_ALU_SCL_221] = { 2, 2, 1 }397};398399static void init_bank_swizzle(struct alu_bank_swizzle *bs)400{401int i, cycle, component;402/* set up gpr use */403for (cycle = 0; cycle < NUM_OF_CYCLES; cycle++)404for (component = 0; component < NUM_OF_COMPONENTS; component++)405bs->hw_gpr[cycle][component] = -1;406for (i = 0; i < 4; i++)407bs->hw_cfile_addr[i] = -1;408for (i = 0; i < 4; i++)409bs->hw_cfile_elem[i] = -1;410}411412static int reserve_gpr(struct alu_bank_swizzle *bs, unsigned sel, unsigned chan, unsigned cycle)413{414if (bs->hw_gpr[cycle][chan] == -1)415bs->hw_gpr[cycle][chan] = sel;416else if (bs->hw_gpr[cycle][chan] != (int)sel) {417/* Another scalar operation has already used the GPR read port for the channel. */418return -1;419}420return 0;421}422423static int reserve_cfile(const struct r600_bytecode *bc,424struct alu_bank_swizzle *bs, unsigned sel, unsigned chan)425{426int res, num_res = 4;427if (bc->chip_class >= R700) {428num_res = 2;429chan /= 2;430}431for (res = 0; res < num_res; ++res) {432if (bs->hw_cfile_addr[res] == -1) {433bs->hw_cfile_addr[res] = sel;434bs->hw_cfile_elem[res] = chan;435return 0;436} else if (bs->hw_cfile_addr[res] == sel &&437bs->hw_cfile_elem[res] == chan)438return 0; /* Read for this scalar element already reserved, nothing to do here. */439}440/* All cfile read ports are used, cannot reference vector element. */441return -1;442}443444static int is_gpr(unsigned sel)445{446return (sel <= 127);447}448449/* CB constants start at 512, and get translated to a kcache index when ALU450* clauses are constructed. Note that we handle kcache constants the same way451* as (the now gone) cfile constants, is that really required? */452static int is_cfile(unsigned sel)453{454return (sel > 255 && sel < 512) ||455(sel > 511 && sel < 4607) || /* Kcache before translation. */456(sel > 127 && sel < 192); /* Kcache after translation. */457}458459static int is_const(int sel)460{461return is_cfile(sel) ||462(sel >= V_SQ_ALU_SRC_0 &&463sel <= V_SQ_ALU_SRC_LITERAL);464}465466static int check_vector(const struct r600_bytecode *bc, const struct r600_bytecode_alu *alu,467struct alu_bank_swizzle *bs, int bank_swizzle)468{469int r, src, num_src, sel, elem, cycle;470471num_src = r600_bytecode_get_num_operands(alu);472for (src = 0; src < num_src; src++) {473sel = alu->src[src].sel;474elem = alu->src[src].chan;475if (is_gpr(sel)) {476cycle = cycle_for_bank_swizzle_vec[bank_swizzle][src];477if (src == 1 && sel == alu->src[0].sel && elem == alu->src[0].chan)478/* Nothing to do; special-case optimization,479* second source uses first source’s reservation. */480continue;481else {482r = reserve_gpr(bs, sel, elem, cycle);483if (r)484return r;485}486} else if (is_cfile(sel)) {487r = reserve_cfile(bc, bs, (alu->src[src].kc_bank<<16) + sel, elem);488if (r)489return r;490}491/* No restrictions on PV, PS, literal or special constants. */492}493return 0;494}495496static int check_scalar(const struct r600_bytecode *bc, const struct r600_bytecode_alu *alu,497struct alu_bank_swizzle *bs, int bank_swizzle)498{499int r, src, num_src, const_count, sel, elem, cycle;500501num_src = r600_bytecode_get_num_operands(alu);502for (const_count = 0, src = 0; src < num_src; ++src) {503sel = alu->src[src].sel;504elem = alu->src[src].chan;505if (is_const(sel)) { /* Any constant, including literal and inline constants. */506if (const_count >= 2)507/* More than two references to a constant in508* transcendental operation. */509return -1;510else511const_count++;512}513if (is_cfile(sel)) {514r = reserve_cfile(bc, bs, (alu->src[src].kc_bank<<16) + sel, elem);515if (r)516return r;517}518}519for (src = 0; src < num_src; ++src) {520sel = alu->src[src].sel;521elem = alu->src[src].chan;522if (is_gpr(sel)) {523cycle = cycle_for_bank_swizzle_scl[bank_swizzle][src];524if (cycle < const_count)525/* Cycle for GPR load conflicts with526* constant load in transcendental operation. */527return -1;528r = reserve_gpr(bs, sel, elem, cycle);529if (r)530return r;531}532/* PV PS restrictions */533if (const_count && (sel == 254 || sel == 255)) {534cycle = cycle_for_bank_swizzle_scl[bank_swizzle][src];535if (cycle < const_count)536return -1;537}538}539return 0;540}541542static int check_and_set_bank_swizzle(const struct r600_bytecode *bc,543struct r600_bytecode_alu *slots[5])544{545struct alu_bank_swizzle bs;546int bank_swizzle[5];547int i, r = 0, forced = 1;548boolean scalar_only = bc->chip_class == CAYMAN ? false : true;549int max_slots = bc->chip_class == CAYMAN ? 4 : 5;550551for (i = 0; i < max_slots; i++) {552if (slots[i]) {553if (slots[i]->bank_swizzle_force) {554slots[i]->bank_swizzle = slots[i]->bank_swizzle_force;555} else {556forced = 0;557}558}559560if (i < 4 && slots[i])561scalar_only = false;562}563if (forced)564return 0;565566/* Just check every possible combination of bank swizzle.567* Not very efficent, but works on the first try in most of the cases. */568for (i = 0; i < 4; i++)569if (!slots[i] || !slots[i]->bank_swizzle_force)570bank_swizzle[i] = SQ_ALU_VEC_012;571else572bank_swizzle[i] = slots[i]->bank_swizzle;573574bank_swizzle[4] = SQ_ALU_SCL_210;575while(bank_swizzle[4] <= SQ_ALU_SCL_221) {576577init_bank_swizzle(&bs);578if (scalar_only == false) {579for (i = 0; i < 4; i++) {580if (slots[i]) {581r = check_vector(bc, slots[i], &bs, bank_swizzle[i]);582if (r)583break;584}585}586} else587r = 0;588589if (!r && max_slots == 5 && slots[4]) {590r = check_scalar(bc, slots[4], &bs, bank_swizzle[4]);591}592if (!r) {593for (i = 0; i < max_slots; i++) {594if (slots[i])595slots[i]->bank_swizzle = bank_swizzle[i];596}597return 0;598}599600if (scalar_only) {601bank_swizzle[4]++;602} else {603for (i = 0; i < max_slots; i++) {604if (!slots[i] || !slots[i]->bank_swizzle_force) {605bank_swizzle[i]++;606if (bank_swizzle[i] <= SQ_ALU_VEC_210)607break;608else if (i < max_slots - 1)609bank_swizzle[i] = SQ_ALU_VEC_012;610else611return -1;612}613}614}615}616617/* Couldn't find a working swizzle. */618return -1;619}620621static int replace_gpr_with_pv_ps(struct r600_bytecode *bc,622struct r600_bytecode_alu *slots[5], struct r600_bytecode_alu *alu_prev)623{624struct r600_bytecode_alu *prev[5];625int gpr[5], chan[5];626int i, j, r, src, num_src;627int max_slots = bc->chip_class == CAYMAN ? 4 : 5;628629r = assign_alu_units(bc, alu_prev, prev);630if (r)631return r;632633for (i = 0; i < max_slots; ++i) {634if (prev[i] && alu_writes(prev[i]) && !prev[i]->dst.rel) {635636if (is_alu_64bit_inst(prev[i])) {637gpr[i] = -1;638continue;639}640641gpr[i] = prev[i]->dst.sel;642/* cube writes more than PV.X */643if (is_alu_reduction_inst(bc, prev[i]))644chan[i] = 0;645else646chan[i] = prev[i]->dst.chan;647} else648gpr[i] = -1;649}650651for (i = 0; i < max_slots; ++i) {652struct r600_bytecode_alu *alu = slots[i];653if (!alu)654continue;655656if (is_alu_64bit_inst(alu))657continue;658num_src = r600_bytecode_get_num_operands(alu);659for (src = 0; src < num_src; ++src) {660if (!is_gpr(alu->src[src].sel) || alu->src[src].rel)661continue;662663if (bc->chip_class < CAYMAN) {664if (alu->src[src].sel == gpr[4] &&665alu->src[src].chan == chan[4] &&666alu_prev->pred_sel == alu->pred_sel) {667alu->src[src].sel = V_SQ_ALU_SRC_PS;668alu->src[src].chan = 0;669continue;670}671}672673for (j = 0; j < 4; ++j) {674if (alu->src[src].sel == gpr[j] &&675alu->src[src].chan == j &&676alu_prev->pred_sel == alu->pred_sel) {677alu->src[src].sel = V_SQ_ALU_SRC_PV;678alu->src[src].chan = chan[j];679break;680}681}682}683}684685return 0;686}687688void r600_bytecode_special_constants(uint32_t value, unsigned *sel)689{690switch(value) {691case 0:692*sel = V_SQ_ALU_SRC_0;693break;694case 1:695*sel = V_SQ_ALU_SRC_1_INT;696break;697case -1:698*sel = V_SQ_ALU_SRC_M_1_INT;699break;700case 0x3F800000: /* 1.0f */701*sel = V_SQ_ALU_SRC_1;702break;703case 0x3F000000: /* 0.5f */704*sel = V_SQ_ALU_SRC_0_5;705break;706default:707*sel = V_SQ_ALU_SRC_LITERAL;708break;709}710}711712/* compute how many literal are needed */713static int r600_bytecode_alu_nliterals(struct r600_bytecode_alu *alu,714uint32_t literal[4], unsigned *nliteral)715{716unsigned num_src = r600_bytecode_get_num_operands(alu);717unsigned i, j;718719for (i = 0; i < num_src; ++i) {720if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {721uint32_t value = alu->src[i].value;722unsigned found = 0;723for (j = 0; j < *nliteral; ++j) {724if (literal[j] == value) {725found = 1;726break;727}728}729if (!found) {730if (*nliteral >= 4)731return -EINVAL;732literal[(*nliteral)++] = value;733}734}735}736return 0;737}738739static void r600_bytecode_alu_adjust_literals(struct r600_bytecode_alu *alu,740uint32_t literal[4], unsigned nliteral)741{742unsigned num_src = r600_bytecode_get_num_operands(alu);743unsigned i, j;744745for (i = 0; i < num_src; ++i) {746if (alu->src[i].sel == V_SQ_ALU_SRC_LITERAL) {747uint32_t value = alu->src[i].value;748for (j = 0; j < nliteral; ++j) {749if (literal[j] == value) {750alu->src[i].chan = j;751break;752}753}754}755}756}757758static int merge_inst_groups(struct r600_bytecode *bc, struct r600_bytecode_alu *slots[5],759struct r600_bytecode_alu *alu_prev)760{761struct r600_bytecode_alu *prev[5];762struct r600_bytecode_alu *result[5] = { NULL };763764uint8_t interp_xz = 0;765766uint32_t literal[4], prev_literal[4];767unsigned nliteral = 0, prev_nliteral = 0;768769int i, j, r, src, num_src;770int num_once_inst = 0;771int have_mova = 0, have_rel = 0;772int max_slots = bc->chip_class == CAYMAN ? 4 : 5;773774r = assign_alu_units(bc, alu_prev, prev);775if (r)776return r;777778for (i = 0; i < max_slots; ++i) {779if (prev[i]) {780if (prev[i]->pred_sel)781return 0;782if (is_alu_once_inst(prev[i]))783return 0;784785if (prev[i]->op == ALU_OP2_INTERP_X)786interp_xz |= 1;787if (prev[i]->op == ALU_OP2_INTERP_Z)788interp_xz |= 2;789}790if (slots[i]) {791if (slots[i]->pred_sel)792return 0;793if (is_alu_once_inst(slots[i]))794return 0;795if (slots[i]->op == ALU_OP2_INTERP_X)796interp_xz |= 1;797if (slots[i]->op == ALU_OP2_INTERP_Z)798interp_xz |= 2;799}800if (interp_xz == 3)801return 0;802}803804for (i = 0; i < max_slots; ++i) {805struct r600_bytecode_alu *alu;806807if (num_once_inst > 0)808return 0;809810/* check number of literals */811if (prev[i]) {812if (r600_bytecode_alu_nliterals(prev[i], literal, &nliteral))813return 0;814if (r600_bytecode_alu_nliterals(prev[i], prev_literal, &prev_nliteral))815return 0;816if (is_alu_mova_inst(prev[i])) {817if (have_rel)818return 0;819have_mova = 1;820}821822if (alu_uses_rel(prev[i])) {823if (have_mova) {824return 0;825}826have_rel = 1;827}828if (alu_uses_lds(prev[i]))829return 0;830831num_once_inst += is_alu_once_inst(prev[i]);832}833if (slots[i] && r600_bytecode_alu_nliterals(slots[i], literal, &nliteral))834return 0;835836/* Let's check used slots. */837if (prev[i] && !slots[i]) {838result[i] = prev[i];839continue;840} else if (prev[i] && slots[i]) {841if (max_slots == 5 && result[4] == NULL && prev[4] == NULL && slots[4] == NULL) {842/* Trans unit is still free try to use it. */843if (is_alu_any_unit_inst(bc, slots[i]) && !alu_uses_lds(slots[i])) {844result[i] = prev[i];845result[4] = slots[i];846} else if (is_alu_any_unit_inst(bc, prev[i])) {847if (slots[i]->dst.sel == prev[i]->dst.sel &&848alu_writes(slots[i]) &&849alu_writes(prev[i]))850return 0;851852result[i] = slots[i];853result[4] = prev[i];854} else855return 0;856} else857return 0;858} else if(!slots[i]) {859continue;860} else {861if (max_slots == 5 && slots[i] && prev[4] &&862slots[i]->dst.sel == prev[4]->dst.sel &&863slots[i]->dst.chan == prev[4]->dst.chan &&864alu_writes(slots[i]) &&865alu_writes(prev[4]))866return 0;867868result[i] = slots[i];869}870871alu = slots[i];872num_once_inst += is_alu_once_inst(alu);873874/* don't reschedule NOPs */875if (is_nop_inst(alu))876return 0;877878if (is_alu_mova_inst(alu)) {879if (have_rel) {880return 0;881}882have_mova = 1;883}884885if (alu_uses_rel(alu)) {886if (have_mova) {887return 0;888}889have_rel = 1;890}891892if (alu->op == ALU_OP0_SET_CF_IDX0 ||893alu->op == ALU_OP0_SET_CF_IDX1)894return 0; /* data hazard with MOVA */895896/* Let's check source gprs */897num_src = r600_bytecode_get_num_operands(alu);898for (src = 0; src < num_src; ++src) {899900/* Constants don't matter. */901if (!is_gpr(alu->src[src].sel))902continue;903904for (j = 0; j < max_slots; ++j) {905if (!prev[j] || !alu_writes(prev[j]))906continue;907908/* If it's relative then we can't determin which gpr is really used. */909if (prev[j]->dst.chan == alu->src[src].chan &&910(prev[j]->dst.sel == alu->src[src].sel ||911prev[j]->dst.rel || alu->src[src].rel))912return 0;913}914}915}916917/* more than one PRED_ or KILL_ ? */918if (num_once_inst > 1)919return 0;920921/* check if the result can still be swizzlet */922r = check_and_set_bank_swizzle(bc, result);923if (r)924return 0;925926/* looks like everything worked out right, apply the changes */927928/* undo adding previus literals */929bc->cf_last->ndw -= align(prev_nliteral, 2);930931/* sort instructions */932for (i = 0; i < max_slots; ++i) {933slots[i] = result[i];934if (result[i]) {935list_del(&result[i]->list);936result[i]->last = 0;937list_addtail(&result[i]->list, &bc->cf_last->alu);938}939}940941/* determine new last instruction */942LIST_ENTRY(struct r600_bytecode_alu, bc->cf_last->alu.prev, list)->last = 1;943944/* determine new first instruction */945for (i = 0; i < max_slots; ++i) {946if (result[i]) {947bc->cf_last->curr_bs_head = result[i];948break;949}950}951952bc->cf_last->prev_bs_head = bc->cf_last->prev2_bs_head;953bc->cf_last->prev2_bs_head = NULL;954955return 0;956}957958/* we'll keep kcache sets sorted by bank & addr */959static int r600_bytecode_alloc_kcache_line(struct r600_bytecode *bc,960struct r600_bytecode_kcache *kcache,961unsigned bank, unsigned line, unsigned index_mode)962{963int i, kcache_banks = bc->chip_class >= EVERGREEN ? 4 : 2;964965for (i = 0; i < kcache_banks; i++) {966if (kcache[i].mode) {967int d;968969if (kcache[i].bank < bank)970continue;971972if ((kcache[i].bank == bank && kcache[i].addr > line+1) ||973kcache[i].bank > bank) {974/* try to insert new line */975if (kcache[kcache_banks-1].mode) {976/* all sets are in use */977return -ENOMEM;978}979980memmove(&kcache[i+1],&kcache[i], (kcache_banks-i-1)*sizeof(struct r600_bytecode_kcache));981kcache[i].mode = V_SQ_CF_KCACHE_LOCK_1;982kcache[i].bank = bank;983kcache[i].addr = line;984kcache[i].index_mode = index_mode;985return 0;986}987988d = line - kcache[i].addr;989990if (d == -1) {991kcache[i].addr--;992if (kcache[i].mode == V_SQ_CF_KCACHE_LOCK_2) {993/* we are prepending the line to the current set,994* discarding the existing second line,995* so we'll have to insert line+2 after it */996line += 2;997continue;998} else if (kcache[i].mode == V_SQ_CF_KCACHE_LOCK_1) {999kcache[i].mode = V_SQ_CF_KCACHE_LOCK_2;1000return 0;1001} else {1002/* V_SQ_CF_KCACHE_LOCK_LOOP_INDEX is not supported */1003return -ENOMEM;1004}1005} else if (d == 1) {1006kcache[i].mode = V_SQ_CF_KCACHE_LOCK_2;1007return 0;1008} else if (d == 0)1009return 0;1010} else { /* free kcache set - use it */1011kcache[i].mode = V_SQ_CF_KCACHE_LOCK_1;1012kcache[i].bank = bank;1013kcache[i].addr = line;1014kcache[i].index_mode = index_mode;1015return 0;1016}1017}1018return -ENOMEM;1019}10201021static int r600_bytecode_alloc_inst_kcache_lines(struct r600_bytecode *bc,1022struct r600_bytecode_kcache *kcache,1023struct r600_bytecode_alu *alu)1024{1025int i, r;10261027for (i = 0; i < 3; i++) {1028unsigned bank, line, sel = alu->src[i].sel, index_mode;10291030if (sel < 512)1031continue;10321033bank = alu->src[i].kc_bank;1034assert(bank < R600_MAX_HW_CONST_BUFFERS);1035line = (sel-512)>>4;1036index_mode = alu->src[i].kc_rel ? 1 : 0; // V_SQ_CF_INDEX_0 / V_SQ_CF_INDEX_NONE10371038if ((r = r600_bytecode_alloc_kcache_line(bc, kcache, bank, line, index_mode)))1039return r;1040}1041return 0;1042}10431044static int r600_bytecode_assign_kcache_banks(1045struct r600_bytecode_alu *alu,1046struct r600_bytecode_kcache * kcache)1047{1048int i, j;10491050/* Alter the src operands to refer to the kcache. */1051for (i = 0; i < 3; ++i) {1052static const unsigned int base[] = {128, 160, 256, 288};1053unsigned int line, sel = alu->src[i].sel, found = 0;10541055if (sel < 512)1056continue;10571058sel -= 512;1059line = sel>>4;10601061for (j = 0; j < 4 && !found; ++j) {1062switch (kcache[j].mode) {1063case V_SQ_CF_KCACHE_NOP:1064case V_SQ_CF_KCACHE_LOCK_LOOP_INDEX:1065R600_ERR("unexpected kcache line mode\n");1066return -ENOMEM;1067default:1068if (kcache[j].bank == alu->src[i].kc_bank &&1069kcache[j].addr <= line &&1070line < kcache[j].addr + kcache[j].mode) {1071alu->src[i].sel = sel - (kcache[j].addr<<4);1072alu->src[i].sel += base[j];1073found=1;1074}1075}1076}1077}1078return 0;1079}10801081static int r600_bytecode_alloc_kcache_lines(struct r600_bytecode *bc,1082struct r600_bytecode_alu *alu,1083unsigned type)1084{1085struct r600_bytecode_kcache kcache_sets[4];1086struct r600_bytecode_kcache *kcache = kcache_sets;1087int r;10881089memcpy(kcache, bc->cf_last->kcache, 4 * sizeof(struct r600_bytecode_kcache));10901091if ((r = r600_bytecode_alloc_inst_kcache_lines(bc, kcache, alu))) {1092/* can't alloc, need to start new clause */1093if ((r = r600_bytecode_add_cf(bc))) {1094return r;1095}1096bc->cf_last->op = type;10971098/* retry with the new clause */1099kcache = bc->cf_last->kcache;1100if ((r = r600_bytecode_alloc_inst_kcache_lines(bc, kcache, alu))) {1101/* can't alloc again- should never happen */1102return r;1103}1104} else {1105/* update kcache sets */1106memcpy(bc->cf_last->kcache, kcache, 4 * sizeof(struct r600_bytecode_kcache));1107}11081109/* if we actually used more than 2 kcache sets, or have relative indexing - use ALU_EXTENDED on eg+ */1110if (kcache[2].mode != V_SQ_CF_KCACHE_NOP ||1111kcache[0].index_mode || kcache[1].index_mode || kcache[2].index_mode || kcache[3].index_mode) {1112if (bc->chip_class < EVERGREEN)1113return -ENOMEM;1114bc->cf_last->eg_alu_extended = 1;1115}11161117return 0;1118}11191120static int insert_nop_r6xx(struct r600_bytecode *bc)1121{1122struct r600_bytecode_alu alu;1123int r, i;11241125for (i = 0; i < 4; i++) {1126memset(&alu, 0, sizeof(alu));1127alu.op = ALU_OP0_NOP;1128alu.src[0].chan = i;1129alu.dst.chan = i;1130alu.last = (i == 3);1131r = r600_bytecode_add_alu(bc, &alu);1132if (r)1133return r;1134}1135return 0;1136}11371138/* load AR register from gpr (bc->ar_reg) with MOVA_INT */1139static int load_ar_r6xx(struct r600_bytecode *bc)1140{1141struct r600_bytecode_alu alu;1142int r;11431144if (bc->ar_loaded)1145return 0;11461147/* hack to avoid making MOVA the last instruction in the clause */1148if ((bc->cf_last->ndw>>1) >= 110)1149bc->force_add_cf = 1;11501151memset(&alu, 0, sizeof(alu));1152alu.op = ALU_OP1_MOVA_GPR_INT;1153alu.src[0].sel = bc->ar_reg;1154alu.src[0].chan = bc->ar_chan;1155alu.last = 1;1156alu.index_mode = INDEX_MODE_LOOP;1157r = r600_bytecode_add_alu(bc, &alu);1158if (r)1159return r;11601161/* no requirement to set uses waterfall on MOVA_GPR_INT */1162bc->ar_loaded = 1;1163return 0;1164}11651166/* load AR register from gpr (bc->ar_reg) with MOVA_INT */1167static int load_ar(struct r600_bytecode *bc)1168{1169struct r600_bytecode_alu alu;1170int r;11711172if (bc->ar_handling)1173return load_ar_r6xx(bc);11741175if (bc->ar_loaded)1176return 0;11771178/* hack to avoid making MOVA the last instruction in the clause */1179if ((bc->cf_last->ndw>>1) >= 110)1180bc->force_add_cf = 1;11811182memset(&alu, 0, sizeof(alu));1183alu.op = ALU_OP1_MOVA_INT;1184alu.src[0].sel = bc->ar_reg;1185alu.src[0].chan = bc->ar_chan;1186alu.last = 1;1187r = r600_bytecode_add_alu(bc, &alu);1188if (r)1189return r;11901191bc->cf_last->r6xx_uses_waterfall = 1;1192bc->ar_loaded = 1;1193return 0;1194}11951196int r600_bytecode_add_alu_type(struct r600_bytecode *bc,1197const struct r600_bytecode_alu *alu, unsigned type)1198{1199struct r600_bytecode_alu *nalu = r600_bytecode_alu();1200struct r600_bytecode_alu *lalu;1201int i, r;12021203if (!nalu)1204return -ENOMEM;1205memcpy(nalu, alu, sizeof(struct r600_bytecode_alu));12061207if (alu->is_op3) {1208/* will fail later since alu does not support it. */1209assert(!alu->src[0].abs && !alu->src[1].abs && !alu->src[2].abs);1210}12111212if (bc->cf_last != NULL && bc->cf_last->op != type) {1213/* check if we could add it anyway */1214if (bc->cf_last->op == CF_OP_ALU &&1215type == CF_OP_ALU_PUSH_BEFORE) {1216LIST_FOR_EACH_ENTRY(lalu, &bc->cf_last->alu, list) {1217if (lalu->execute_mask) {1218bc->force_add_cf = 1;1219break;1220}1221}1222} else1223bc->force_add_cf = 1;1224}12251226/* cf can contains only alu or only vtx or only tex */1227if (bc->cf_last == NULL || bc->force_add_cf) {1228r = r600_bytecode_add_cf(bc);1229if (r) {1230free(nalu);1231return r;1232}1233}1234bc->cf_last->op = type;12351236/* Load index register if required */1237if (bc->chip_class >= EVERGREEN) {1238for (i = 0; i < 3; i++)1239if (nalu->src[i].kc_bank && nalu->src[i].kc_rel)1240egcm_load_index_reg(bc, 0, true);1241}12421243/* Check AR usage and load it if required */1244for (i = 0; i < 3; i++)1245if (nalu->src[i].rel && !bc->ar_loaded)1246load_ar(bc);12471248if (nalu->dst.rel && !bc->ar_loaded)1249load_ar(bc);12501251/* Setup the kcache for this ALU instruction. This will start a new1252* ALU clause if needed. */1253if ((r = r600_bytecode_alloc_kcache_lines(bc, nalu, type))) {1254free(nalu);1255return r;1256}12571258if (!bc->cf_last->curr_bs_head) {1259bc->cf_last->curr_bs_head = nalu;1260}1261/* number of gpr == the last gpr used in any alu */1262for (i = 0; i < 3; i++) {1263if (nalu->src[i].sel >= bc->ngpr && nalu->src[i].sel < 128) {1264bc->ngpr = nalu->src[i].sel + 1;1265}1266if (nalu->src[i].sel == V_SQ_ALU_SRC_LITERAL)1267r600_bytecode_special_constants(nalu->src[i].value,1268&nalu->src[i].sel);1269}1270if (nalu->dst.sel >= bc->ngpr) {1271bc->ngpr = nalu->dst.sel + 1;1272}1273list_addtail(&nalu->list, &bc->cf_last->alu);1274/* each alu use 2 dwords */1275bc->cf_last->ndw += 2;1276bc->ndw += 2;12771278/* process cur ALU instructions for bank swizzle */1279if (nalu->last) {1280uint32_t literal[4];1281unsigned nliteral;1282struct r600_bytecode_alu *slots[5];1283int max_slots = bc->chip_class == CAYMAN ? 4 : 5;1284r = assign_alu_units(bc, bc->cf_last->curr_bs_head, slots);1285if (r)1286return r;12871288if (bc->cf_last->prev_bs_head) {1289r = merge_inst_groups(bc, slots, bc->cf_last->prev_bs_head);1290if (r)1291return r;1292}12931294if (bc->cf_last->prev_bs_head) {1295r = replace_gpr_with_pv_ps(bc, slots, bc->cf_last->prev_bs_head);1296if (r)1297return r;1298}12991300r = check_and_set_bank_swizzle(bc, slots);1301if (r)1302return r;13031304for (i = 0, nliteral = 0; i < max_slots; i++) {1305if (slots[i]) {1306r = r600_bytecode_alu_nliterals(slots[i], literal, &nliteral);1307if (r)1308return r;1309}1310}1311bc->cf_last->ndw += align(nliteral, 2);13121313/* at most 128 slots, one add alu can add 5 slots + 4 constants(2 slots)1314* worst case */1315if ((bc->cf_last->ndw >> 1) >= 120) {1316bc->force_add_cf = 1;1317}13181319bc->cf_last->prev2_bs_head = bc->cf_last->prev_bs_head;1320bc->cf_last->prev_bs_head = bc->cf_last->curr_bs_head;1321bc->cf_last->curr_bs_head = NULL;1322}13231324if (nalu->dst.rel && bc->r6xx_nop_after_rel_dst)1325insert_nop_r6xx(bc);13261327/* Might need to insert spill write ops after current clause */1328if (nalu->last && bc->n_pending_outputs) {1329while (bc->n_pending_outputs) {1330r = r600_bytecode_add_output(bc, &bc->pending_outputs[--bc->n_pending_outputs]);1331if (r)1332return r;1333}1334}13351336return 0;1337}13381339int r600_bytecode_add_alu(struct r600_bytecode *bc, const struct r600_bytecode_alu *alu)1340{1341return r600_bytecode_add_alu_type(bc, alu, CF_OP_ALU);1342}13431344static unsigned r600_bytecode_num_tex_and_vtx_instructions(const struct r600_bytecode *bc)1345{1346switch (bc->chip_class) {1347case R600:1348return 8;13491350case R700:1351case EVERGREEN:1352case CAYMAN:1353return 16;13541355default:1356R600_ERR("Unknown chip class %d.\n", bc->chip_class);1357return 8;1358}1359}13601361static inline boolean last_inst_was_not_vtx_fetch(struct r600_bytecode *bc)1362{1363return !((r600_isa_cf(bc->cf_last->op)->flags & CF_FETCH) &&1364bc->cf_last->op != CF_OP_GDS &&1365(bc->chip_class == CAYMAN ||1366bc->cf_last->op != CF_OP_TEX));1367}13681369static int r600_bytecode_add_vtx_internal(struct r600_bytecode *bc, const struct r600_bytecode_vtx *vtx,1370bool use_tc)1371{1372struct r600_bytecode_vtx *nvtx = r600_bytecode_vtx();1373int r;13741375if (!nvtx)1376return -ENOMEM;1377memcpy(nvtx, vtx, sizeof(struct r600_bytecode_vtx));13781379/* Load index register if required */1380if (bc->chip_class >= EVERGREEN) {1381if (vtx->buffer_index_mode)1382egcm_load_index_reg(bc, vtx->buffer_index_mode - 1, false);1383}13841385/* cf can contains only alu or only vtx or only tex */1386if (bc->cf_last == NULL ||1387last_inst_was_not_vtx_fetch(bc) ||1388bc->force_add_cf) {1389r = r600_bytecode_add_cf(bc);1390if (r) {1391free(nvtx);1392return r;1393}1394switch (bc->chip_class) {1395case R600:1396case R700:1397bc->cf_last->op = CF_OP_VTX;1398break;1399case EVERGREEN:1400if (use_tc)1401bc->cf_last->op = CF_OP_TEX;1402else1403bc->cf_last->op = CF_OP_VTX;1404break;1405case CAYMAN:1406bc->cf_last->op = CF_OP_TEX;1407break;1408default:1409R600_ERR("Unknown chip class %d.\n", bc->chip_class);1410free(nvtx);1411return -EINVAL;1412}1413}1414list_addtail(&nvtx->list, &bc->cf_last->vtx);1415/* each fetch use 4 dwords */1416bc->cf_last->ndw += 4;1417bc->ndw += 4;1418if ((bc->cf_last->ndw / 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc))1419bc->force_add_cf = 1;14201421bc->ngpr = MAX2(bc->ngpr, vtx->src_gpr + 1);1422bc->ngpr = MAX2(bc->ngpr, vtx->dst_gpr + 1);14231424return 0;1425}14261427int r600_bytecode_add_vtx(struct r600_bytecode *bc, const struct r600_bytecode_vtx *vtx)1428{1429return r600_bytecode_add_vtx_internal(bc, vtx, false);1430}14311432int r600_bytecode_add_vtx_tc(struct r600_bytecode *bc, const struct r600_bytecode_vtx *vtx)1433{1434return r600_bytecode_add_vtx_internal(bc, vtx, true);1435}14361437int r600_bytecode_add_tex(struct r600_bytecode *bc, const struct r600_bytecode_tex *tex)1438{1439struct r600_bytecode_tex *ntex = r600_bytecode_tex();1440int r;14411442if (!ntex)1443return -ENOMEM;1444memcpy(ntex, tex, sizeof(struct r600_bytecode_tex));14451446/* Load index register if required */1447if (bc->chip_class >= EVERGREEN) {1448if (tex->sampler_index_mode || tex->resource_index_mode)1449egcm_load_index_reg(bc, 1, false);1450}14511452/* we can't fetch data und use it as texture lookup address in the same TEX clause */1453if (bc->cf_last != NULL &&1454bc->cf_last->op == CF_OP_TEX) {1455struct r600_bytecode_tex *ttex;1456LIST_FOR_EACH_ENTRY(ttex, &bc->cf_last->tex, list) {1457if (ttex->dst_gpr == ntex->src_gpr &&1458(ttex->dst_sel_x < 4 || ttex->dst_sel_y < 4 ||1459ttex->dst_sel_z < 4 || ttex->dst_sel_w < 4)) {1460bc->force_add_cf = 1;1461break;1462}1463}1464/* slight hack to make gradients always go into same cf */1465if (ntex->op == FETCH_OP_SET_GRADIENTS_H)1466bc->force_add_cf = 1;1467}14681469/* cf can contains only alu or only vtx or only tex */1470if (bc->cf_last == NULL ||1471bc->cf_last->op != CF_OP_TEX ||1472bc->force_add_cf) {1473r = r600_bytecode_add_cf(bc);1474if (r) {1475free(ntex);1476return r;1477}1478bc->cf_last->op = CF_OP_TEX;1479}1480if (ntex->src_gpr >= bc->ngpr) {1481bc->ngpr = ntex->src_gpr + 1;1482}1483if (ntex->dst_gpr >= bc->ngpr) {1484bc->ngpr = ntex->dst_gpr + 1;1485}1486list_addtail(&ntex->list, &bc->cf_last->tex);1487/* each texture fetch use 4 dwords */1488bc->cf_last->ndw += 4;1489bc->ndw += 4;1490if ((bc->cf_last->ndw / 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc))1491bc->force_add_cf = 1;1492return 0;1493}14941495int r600_bytecode_add_gds(struct r600_bytecode *bc, const struct r600_bytecode_gds *gds)1496{1497struct r600_bytecode_gds *ngds = r600_bytecode_gds();1498int r;14991500if (ngds == NULL)1501return -ENOMEM;1502memcpy(ngds, gds, sizeof(struct r600_bytecode_gds));15031504if (bc->chip_class >= EVERGREEN) {1505if (gds->uav_index_mode)1506egcm_load_index_reg(bc, gds->uav_index_mode - 1, false);1507}15081509if (bc->cf_last == NULL ||1510bc->cf_last->op != CF_OP_GDS ||1511bc->force_add_cf) {1512r = r600_bytecode_add_cf(bc);1513if (r) {1514free(ngds);1515return r;1516}1517bc->cf_last->op = CF_OP_GDS;1518}15191520list_addtail(&ngds->list, &bc->cf_last->gds);1521bc->cf_last->ndw += 4; /* each GDS uses 4 dwords */1522if ((bc->cf_last->ndw / 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc))1523bc->force_add_cf = 1;1524return 0;1525}15261527int r600_bytecode_add_cfinst(struct r600_bytecode *bc, unsigned op)1528{1529int r;15301531/* Emit WAIT_ACK before control flow to ensure pending writes are always acked. */1532if (op != CF_OP_MEM_SCRATCH && bc->need_wait_ack) {1533bc->need_wait_ack = false;1534r = r600_bytecode_add_cfinst(bc, CF_OP_WAIT_ACK);1535}15361537r = r600_bytecode_add_cf(bc);1538if (r)1539return r;15401541bc->cf_last->cond = V_SQ_CF_COND_ACTIVE;1542bc->cf_last->op = op;1543return 0;1544}15451546int cm_bytecode_add_cf_end(struct r600_bytecode *bc)1547{1548return r600_bytecode_add_cfinst(bc, CF_OP_CF_END);1549}15501551/* common to all 3 families */1552static int r600_bytecode_vtx_build(struct r600_bytecode *bc, struct r600_bytecode_vtx *vtx, unsigned id)1553{1554if (r600_isa_fetch(vtx->op)->flags & FF_MEM)1555return r700_bytecode_fetch_mem_build(bc, vtx, id);1556bc->bytecode[id] = S_SQ_VTX_WORD0_VTX_INST(r600_isa_fetch_opcode(bc->isa->hw_class, vtx->op)) |1557S_SQ_VTX_WORD0_BUFFER_ID(vtx->buffer_id) |1558S_SQ_VTX_WORD0_FETCH_TYPE(vtx->fetch_type) |1559S_SQ_VTX_WORD0_SRC_GPR(vtx->src_gpr) |1560S_SQ_VTX_WORD0_SRC_SEL_X(vtx->src_sel_x);1561if (bc->chip_class < CAYMAN)1562bc->bytecode[id] |= S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx->mega_fetch_count);1563id++;1564bc->bytecode[id++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx->dst_sel_x) |1565S_SQ_VTX_WORD1_DST_SEL_Y(vtx->dst_sel_y) |1566S_SQ_VTX_WORD1_DST_SEL_Z(vtx->dst_sel_z) |1567S_SQ_VTX_WORD1_DST_SEL_W(vtx->dst_sel_w) |1568S_SQ_VTX_WORD1_USE_CONST_FIELDS(vtx->use_const_fields) |1569S_SQ_VTX_WORD1_DATA_FORMAT(vtx->data_format) |1570S_SQ_VTX_WORD1_NUM_FORMAT_ALL(vtx->num_format_all) |1571S_SQ_VTX_WORD1_FORMAT_COMP_ALL(vtx->format_comp_all) |1572S_SQ_VTX_WORD1_SRF_MODE_ALL(vtx->srf_mode_all) |1573S_SQ_VTX_WORD1_GPR_DST_GPR(vtx->dst_gpr);1574bc->bytecode[id] = S_SQ_VTX_WORD2_OFFSET(vtx->offset)|1575S_SQ_VTX_WORD2_ENDIAN_SWAP(vtx->endian);1576if (bc->chip_class >= EVERGREEN)1577bc->bytecode[id] |= ((vtx->buffer_index_mode & 0x3) << 21); // S_SQ_VTX_WORD2_BIM(vtx->buffer_index_mode);1578if (bc->chip_class < CAYMAN)1579bc->bytecode[id] |= S_SQ_VTX_WORD2_MEGA_FETCH(1);1580id++;1581bc->bytecode[id++] = 0;1582return 0;1583}15841585/* common to all 3 families */1586static int r600_bytecode_tex_build(struct r600_bytecode *bc, struct r600_bytecode_tex *tex, unsigned id)1587{1588bc->bytecode[id] = S_SQ_TEX_WORD0_TEX_INST(1589r600_isa_fetch_opcode(bc->isa->hw_class, tex->op)) |1590EG_S_SQ_TEX_WORD0_INST_MOD(tex->inst_mod) |1591S_SQ_TEX_WORD0_RESOURCE_ID(tex->resource_id) |1592S_SQ_TEX_WORD0_SRC_GPR(tex->src_gpr) |1593S_SQ_TEX_WORD0_SRC_REL(tex->src_rel);1594if (bc->chip_class >= EVERGREEN)1595bc->bytecode[id] |= ((tex->sampler_index_mode & 0x3) << 27) | // S_SQ_TEX_WORD0_SIM(tex->sampler_index_mode);1596((tex->resource_index_mode & 0x3) << 25); // S_SQ_TEX_WORD0_RIM(tex->resource_index_mode)1597id++;1598bc->bytecode[id++] = S_SQ_TEX_WORD1_DST_GPR(tex->dst_gpr) |1599S_SQ_TEX_WORD1_DST_REL(tex->dst_rel) |1600S_SQ_TEX_WORD1_DST_SEL_X(tex->dst_sel_x) |1601S_SQ_TEX_WORD1_DST_SEL_Y(tex->dst_sel_y) |1602S_SQ_TEX_WORD1_DST_SEL_Z(tex->dst_sel_z) |1603S_SQ_TEX_WORD1_DST_SEL_W(tex->dst_sel_w) |1604S_SQ_TEX_WORD1_LOD_BIAS(tex->lod_bias) |1605S_SQ_TEX_WORD1_COORD_TYPE_X(tex->coord_type_x) |1606S_SQ_TEX_WORD1_COORD_TYPE_Y(tex->coord_type_y) |1607S_SQ_TEX_WORD1_COORD_TYPE_Z(tex->coord_type_z) |1608S_SQ_TEX_WORD1_COORD_TYPE_W(tex->coord_type_w);1609bc->bytecode[id++] = S_SQ_TEX_WORD2_OFFSET_X(tex->offset_x) |1610S_SQ_TEX_WORD2_OFFSET_Y(tex->offset_y) |1611S_SQ_TEX_WORD2_OFFSET_Z(tex->offset_z) |1612S_SQ_TEX_WORD2_SAMPLER_ID(tex->sampler_id) |1613S_SQ_TEX_WORD2_SRC_SEL_X(tex->src_sel_x) |1614S_SQ_TEX_WORD2_SRC_SEL_Y(tex->src_sel_y) |1615S_SQ_TEX_WORD2_SRC_SEL_Z(tex->src_sel_z) |1616S_SQ_TEX_WORD2_SRC_SEL_W(tex->src_sel_w);1617bc->bytecode[id++] = 0;1618return 0;1619}16201621/* r600 only, r700/eg bits in r700_asm.c */1622static int r600_bytecode_alu_build(struct r600_bytecode *bc, struct r600_bytecode_alu *alu, unsigned id)1623{1624unsigned opcode = r600_isa_alu_opcode(bc->isa->hw_class, alu->op);16251626/* don't replace gpr by pv or ps for destination register */1627bc->bytecode[id++] = S_SQ_ALU_WORD0_SRC0_SEL(alu->src[0].sel) |1628S_SQ_ALU_WORD0_SRC0_REL(alu->src[0].rel) |1629S_SQ_ALU_WORD0_SRC0_CHAN(alu->src[0].chan) |1630S_SQ_ALU_WORD0_SRC0_NEG(alu->src[0].neg) |1631S_SQ_ALU_WORD0_SRC1_SEL(alu->src[1].sel) |1632S_SQ_ALU_WORD0_SRC1_REL(alu->src[1].rel) |1633S_SQ_ALU_WORD0_SRC1_CHAN(alu->src[1].chan) |1634S_SQ_ALU_WORD0_SRC1_NEG(alu->src[1].neg) |1635S_SQ_ALU_WORD0_INDEX_MODE(alu->index_mode) |1636S_SQ_ALU_WORD0_PRED_SEL(alu->pred_sel) |1637S_SQ_ALU_WORD0_LAST(alu->last);16381639if (alu->is_op3) {1640assert(!alu->src[0].abs && !alu->src[1].abs && !alu->src[2].abs);1641bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |1642S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |1643S_SQ_ALU_WORD1_DST_REL(alu->dst.rel) |1644S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |1645S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu->src[2].sel) |1646S_SQ_ALU_WORD1_OP3_SRC2_REL(alu->src[2].rel) |1647S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu->src[2].chan) |1648S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu->src[2].neg) |1649S_SQ_ALU_WORD1_OP3_ALU_INST(opcode) |1650S_SQ_ALU_WORD1_BANK_SWIZZLE(alu->bank_swizzle);1651} else {1652bc->bytecode[id++] = S_SQ_ALU_WORD1_DST_GPR(alu->dst.sel) |1653S_SQ_ALU_WORD1_DST_CHAN(alu->dst.chan) |1654S_SQ_ALU_WORD1_DST_REL(alu->dst.rel) |1655S_SQ_ALU_WORD1_CLAMP(alu->dst.clamp) |1656S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu->src[0].abs) |1657S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu->src[1].abs) |1658S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu->dst.write) |1659S_SQ_ALU_WORD1_OP2_OMOD(alu->omod) |1660S_SQ_ALU_WORD1_OP2_ALU_INST(opcode) |1661S_SQ_ALU_WORD1_BANK_SWIZZLE(alu->bank_swizzle) |1662S_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(alu->execute_mask) |1663S_SQ_ALU_WORD1_OP2_UPDATE_PRED(alu->update_pred);1664}1665return 0;1666}16671668static void r600_bytecode_cf_vtx_build(uint32_t *bytecode, const struct r600_bytecode_cf *cf)1669{1670*bytecode++ = S_SQ_CF_WORD0_ADDR(cf->addr >> 1);1671*bytecode++ = S_SQ_CF_WORD1_CF_INST(r600_isa_cf_opcode(ISA_CC_R600, cf->op)) |1672S_SQ_CF_WORD1_BARRIER(1) |1673S_SQ_CF_WORD1_COUNT((cf->ndw / 4) - 1)|1674S_SQ_CF_WORD1_END_OF_PROGRAM(cf->end_of_program);1675}16761677/* common for r600/r700 - eg in eg_asm.c */1678static int r600_bytecode_cf_build(struct r600_bytecode *bc, struct r600_bytecode_cf *cf)1679{1680unsigned id = cf->id;1681const struct cf_op_info *cfop = r600_isa_cf(cf->op);1682unsigned opcode = r600_isa_cf_opcode(bc->isa->hw_class, cf->op);168316841685if (cf->op == CF_NATIVE) {1686bc->bytecode[id++] = cf->isa[0];1687bc->bytecode[id++] = cf->isa[1];1688} else if (cfop->flags & CF_ALU) {1689bc->bytecode[id++] = S_SQ_CF_ALU_WORD0_ADDR(cf->addr >> 1) |1690S_SQ_CF_ALU_WORD0_KCACHE_MODE0(cf->kcache[0].mode) |1691S_SQ_CF_ALU_WORD0_KCACHE_BANK0(cf->kcache[0].bank) |1692S_SQ_CF_ALU_WORD0_KCACHE_BANK1(cf->kcache[1].bank);16931694bc->bytecode[id++] = S_SQ_CF_ALU_WORD1_CF_INST(opcode) |1695S_SQ_CF_ALU_WORD1_KCACHE_MODE1(cf->kcache[1].mode) |1696S_SQ_CF_ALU_WORD1_KCACHE_ADDR0(cf->kcache[0].addr) |1697S_SQ_CF_ALU_WORD1_KCACHE_ADDR1(cf->kcache[1].addr) |1698S_SQ_CF_ALU_WORD1_BARRIER(1) |1699S_SQ_CF_ALU_WORD1_USES_WATERFALL(bc->chip_class == R600 ? cf->r6xx_uses_waterfall : 0) |1700S_SQ_CF_ALU_WORD1_COUNT((cf->ndw / 2) - 1);1701} else if (cfop->flags & CF_FETCH) {1702if (bc->chip_class == R700)1703r700_bytecode_cf_vtx_build(&bc->bytecode[id], cf);1704else1705r600_bytecode_cf_vtx_build(&bc->bytecode[id], cf);1706} else if (cfop->flags & CF_EXP) {1707bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf->output.gpr) |1708S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |1709S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |1710S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type) |1711S_SQ_CF_ALLOC_EXPORT_WORD0_INDEX_GPR(cf->output.index_gpr);1712bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf->output.burst_count - 1) |1713S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf->output.swizzle_x) |1714S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf->output.swizzle_y) |1715S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf->output.swizzle_z) |1716S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf->output.swizzle_w) |1717S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf->barrier) |1718S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode) |1719S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf->end_of_program);1720} else if (cfop->flags & CF_MEM) {1721bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf->output.gpr) |1722S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf->output.elem_size) |1723S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf->output.array_base) |1724S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf->output.type) |1725S_SQ_CF_ALLOC_EXPORT_WORD0_INDEX_GPR(cf->output.index_gpr);1726bc->bytecode[id++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf->output.burst_count - 1) |1727S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf->barrier) |1728S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode) |1729S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf->end_of_program) |1730S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(cf->output.array_size) |1731S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(cf->output.comp_mask);1732} else {1733bc->bytecode[id++] = S_SQ_CF_WORD0_ADDR(cf->cf_addr >> 1);1734bc->bytecode[id++] = S_SQ_CF_WORD1_CF_INST(opcode) |1735S_SQ_CF_WORD1_BARRIER(1) |1736S_SQ_CF_WORD1_COND(cf->cond) |1737S_SQ_CF_WORD1_POP_COUNT(cf->pop_count) |1738S_SQ_CF_WORD1_END_OF_PROGRAM(cf->end_of_program);1739}1740return 0;1741}17421743int r600_bytecode_build(struct r600_bytecode *bc)1744{1745struct r600_bytecode_cf *cf;1746struct r600_bytecode_alu *alu;1747struct r600_bytecode_vtx *vtx;1748struct r600_bytecode_tex *tex;1749struct r600_bytecode_gds *gds;1750uint32_t literal[4];1751unsigned nliteral;1752unsigned addr;1753int i, r;17541755if (!bc->nstack) { // If not 0, Stack_size already provided by llvm1756if (bc->stack.max_entries)1757bc->nstack = bc->stack.max_entries;1758else if (bc->type == PIPE_SHADER_VERTEX ||1759bc->type == PIPE_SHADER_TESS_EVAL ||1760bc->type == PIPE_SHADER_TESS_CTRL)1761bc->nstack = 1;1762}17631764/* first path compute addr of each CF block */1765/* addr start after all the CF instructions */1766addr = bc->cf_last->id + 2;1767LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {1768if (r600_isa_cf(cf->op)->flags & CF_FETCH) {1769addr += 3;1770addr &= 0xFFFFFFFCUL;1771}1772cf->addr = addr;1773addr += cf->ndw;1774bc->ndw = cf->addr + cf->ndw;1775}1776free(bc->bytecode);1777bc->bytecode = calloc(4, bc->ndw);1778if (bc->bytecode == NULL)1779return -ENOMEM;1780LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {1781const struct cf_op_info *cfop = r600_isa_cf(cf->op);1782addr = cf->addr;1783if (bc->chip_class >= EVERGREEN)1784r = eg_bytecode_cf_build(bc, cf);1785else1786r = r600_bytecode_cf_build(bc, cf);1787if (r)1788return r;1789if (cfop->flags & CF_ALU) {1790nliteral = 0;1791memset(literal, 0, sizeof(literal));1792LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {1793r = r600_bytecode_alu_nliterals(alu, literal, &nliteral);1794if (r)1795return r;1796r600_bytecode_alu_adjust_literals(alu, literal, nliteral);1797r600_bytecode_assign_kcache_banks(alu, cf->kcache);17981799switch(bc->chip_class) {1800case R600:1801r = r600_bytecode_alu_build(bc, alu, addr);1802break;1803case R700:1804r = r700_bytecode_alu_build(bc, alu, addr);1805break;1806case EVERGREEN:1807case CAYMAN:1808r = eg_bytecode_alu_build(bc, alu, addr);1809break;1810default:1811R600_ERR("unknown chip class %d.\n", bc->chip_class);1812return -EINVAL;1813}1814if (r)1815return r;1816addr += 2;1817if (alu->last) {1818for (i = 0; i < align(nliteral, 2); ++i) {1819bc->bytecode[addr++] = literal[i];1820}1821nliteral = 0;1822memset(literal, 0, sizeof(literal));1823}1824}1825} else if (cf->op == CF_OP_VTX) {1826LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {1827r = r600_bytecode_vtx_build(bc, vtx, addr);1828if (r)1829return r;1830addr += 4;1831}1832} else if (cf->op == CF_OP_GDS) {1833assert(bc->chip_class >= EVERGREEN);1834LIST_FOR_EACH_ENTRY(gds, &cf->gds, list) {1835r = eg_bytecode_gds_build(bc, gds, addr);1836if (r)1837return r;1838addr += 4;1839}1840} else if (cf->op == CF_OP_TEX) {1841LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {1842assert(bc->chip_class >= EVERGREEN);1843r = r600_bytecode_vtx_build(bc, vtx, addr);1844if (r)1845return r;1846addr += 4;1847}1848LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {1849r = r600_bytecode_tex_build(bc, tex, addr);1850if (r)1851return r;1852addr += 4;1853}1854}1855}1856return 0;1857}18581859void r600_bytecode_clear(struct r600_bytecode *bc)1860{1861struct r600_bytecode_cf *cf = NULL, *next_cf;18621863free(bc->bytecode);1864bc->bytecode = NULL;18651866LIST_FOR_EACH_ENTRY_SAFE(cf, next_cf, &bc->cf, list) {1867struct r600_bytecode_alu *alu = NULL, *next_alu;1868struct r600_bytecode_tex *tex = NULL, *next_tex;1869struct r600_bytecode_tex *vtx = NULL, *next_vtx;1870struct r600_bytecode_gds *gds = NULL, *next_gds;18711872LIST_FOR_EACH_ENTRY_SAFE(alu, next_alu, &cf->alu, list) {1873free(alu);1874}18751876list_inithead(&cf->alu);18771878LIST_FOR_EACH_ENTRY_SAFE(tex, next_tex, &cf->tex, list) {1879free(tex);1880}18811882list_inithead(&cf->tex);18831884LIST_FOR_EACH_ENTRY_SAFE(vtx, next_vtx, &cf->vtx, list) {1885free(vtx);1886}18871888list_inithead(&cf->vtx);18891890LIST_FOR_EACH_ENTRY_SAFE(gds, next_gds, &cf->gds, list) {1891free(gds);1892}18931894list_inithead(&cf->gds);18951896free(cf);1897}18981899list_inithead(&cf->list);1900}19011902static int print_swizzle(unsigned swz)1903{1904const char * swzchars = "xyzw01?_";1905assert(swz<8 && swz != 6);1906return fprintf(stderr, "%c", swzchars[swz]);1907}19081909static int print_sel(unsigned sel, unsigned rel, unsigned index_mode,1910unsigned need_brackets)1911{1912int o = 0;1913if (rel && index_mode >= 5 && sel < 128)1914o += fprintf(stderr, "G");1915if (rel || need_brackets) {1916o += fprintf(stderr, "[");1917}1918o += fprintf(stderr, "%d", sel);1919if (rel) {1920if (index_mode == 0 || index_mode == 6)1921o += fprintf(stderr, "+AR");1922else if (index_mode == 4)1923o += fprintf(stderr, "+AL");1924}1925if (rel || need_brackets) {1926o += fprintf(stderr, "]");1927}1928return o;1929}19301931static int print_dst(struct r600_bytecode_alu *alu)1932{1933int o = 0;1934unsigned sel = alu->dst.sel;1935char reg_char = 'R';1936if (sel > 128 - 4) { /* clause temporary gpr */1937sel -= 128 - 4;1938reg_char = 'T';1939}19401941if (alu_writes(alu)) {1942o += fprintf(stderr, "%c", reg_char);1943o += print_sel(alu->dst.sel, alu->dst.rel, alu->index_mode, 0);1944} else {1945o += fprintf(stderr, "__");1946}1947o += fprintf(stderr, ".");1948o += print_swizzle(alu->dst.chan);1949return o;1950}19511952static int print_src(struct r600_bytecode_alu *alu, unsigned idx)1953{1954int o = 0;1955struct r600_bytecode_alu_src *src = &alu->src[idx];1956unsigned sel = src->sel, need_sel = 1, need_chan = 1, need_brackets = 0;19571958if (src->neg)1959o += fprintf(stderr,"-");1960if (src->abs)1961o += fprintf(stderr,"|");19621963if (sel < 128 - 4) {1964o += fprintf(stderr, "R");1965} else if (sel < 128) {1966o += fprintf(stderr, "T");1967sel -= 128 - 4;1968} else if (sel < 160) {1969o += fprintf(stderr, "KC0");1970need_brackets = 1;1971sel -= 128;1972} else if (sel < 192) {1973o += fprintf(stderr, "KC1");1974need_brackets = 1;1975sel -= 160;1976} else if (sel >= 512) {1977o += fprintf(stderr, "C%d", src->kc_bank);1978need_brackets = 1;1979sel -= 512;1980} else if (sel >= 448) {1981o += fprintf(stderr, "Param");1982sel -= 448;1983need_chan = 0;1984} else if (sel >= 288) {1985o += fprintf(stderr, "KC3");1986need_brackets = 1;1987sel -= 288;1988} else if (sel >= 256) {1989o += fprintf(stderr, "KC2");1990need_brackets = 1;1991sel -= 256;1992} else {1993need_sel = 0;1994need_chan = 0;1995switch (sel) {1996case EG_V_SQ_ALU_SRC_LDS_DIRECT_A:1997o += fprintf(stderr, "LDS_A[0x%08X]", src->value);1998break;1999case EG_V_SQ_ALU_SRC_LDS_DIRECT_B:2000o += fprintf(stderr, "LDS_B[0x%08X]", src->value);2001break;2002case EG_V_SQ_ALU_SRC_LDS_OQ_A:2003o += fprintf(stderr, "LDS_OQ_A");2004need_chan = 1;2005break;2006case EG_V_SQ_ALU_SRC_LDS_OQ_B:2007o += fprintf(stderr, "LDS_OQ_B");2008need_chan = 1;2009break;2010case EG_V_SQ_ALU_SRC_LDS_OQ_A_POP:2011o += fprintf(stderr, "LDS_OQ_A_POP");2012need_chan = 1;2013break;2014case EG_V_SQ_ALU_SRC_LDS_OQ_B_POP:2015o += fprintf(stderr, "LDS_OQ_B_POP");2016need_chan = 1;2017break;2018case EG_V_SQ_ALU_SRC_TIME_LO:2019o += fprintf(stderr, "TIME_LO");2020break;2021case EG_V_SQ_ALU_SRC_TIME_HI:2022o += fprintf(stderr, "TIME_HI");2023break;2024case EG_V_SQ_ALU_SRC_SE_ID:2025o += fprintf(stderr, "SE_ID");2026break;2027case EG_V_SQ_ALU_SRC_SIMD_ID:2028o += fprintf(stderr, "SIMD_ID");2029break;2030case EG_V_SQ_ALU_SRC_HW_WAVE_ID:2031o += fprintf(stderr, "HW_WAVE_ID");2032break;2033case V_SQ_ALU_SRC_PS:2034o += fprintf(stderr, "PS");2035break;2036case V_SQ_ALU_SRC_PV:2037o += fprintf(stderr, "PV");2038need_chan = 1;2039break;2040case V_SQ_ALU_SRC_LITERAL:2041o += fprintf(stderr, "[0x%08X %f]", src->value, u_bitcast_u2f(src->value));2042break;2043case V_SQ_ALU_SRC_0_5:2044o += fprintf(stderr, "0.5");2045break;2046case V_SQ_ALU_SRC_M_1_INT:2047o += fprintf(stderr, "-1");2048break;2049case V_SQ_ALU_SRC_1_INT:2050o += fprintf(stderr, "1");2051break;2052case V_SQ_ALU_SRC_1:2053o += fprintf(stderr, "1.0");2054break;2055case V_SQ_ALU_SRC_0:2056o += fprintf(stderr, "0");2057break;2058default:2059o += fprintf(stderr, "??IMM_%d", sel);2060break;2061}2062}20632064if (need_sel)2065o += print_sel(sel, src->rel, alu->index_mode, need_brackets);20662067if (need_chan) {2068o += fprintf(stderr, ".");2069o += print_swizzle(src->chan);2070}20712072if (src->abs)2073o += fprintf(stderr,"|");20742075return o;2076}20772078static int print_indent(int p, int c)2079{2080int o = 0;2081while (p++ < c)2082o += fprintf(stderr, " ");2083return o;2084}20852086void r600_bytecode_disasm(struct r600_bytecode *bc)2087{2088const char *index_mode[] = {"CF_INDEX_NONE", "CF_INDEX_0", "CF_INDEX_1"};2089static int index = 0;2090struct r600_bytecode_cf *cf = NULL;2091struct r600_bytecode_alu *alu = NULL;2092struct r600_bytecode_vtx *vtx = NULL;2093struct r600_bytecode_tex *tex = NULL;2094struct r600_bytecode_gds *gds = NULL;20952096unsigned i, id, ngr = 0, last;2097uint32_t literal[4];2098unsigned nliteral;2099char chip = '6';21002101switch (bc->chip_class) {2102case R700:2103chip = '7';2104break;2105case EVERGREEN:2106chip = 'E';2107break;2108case CAYMAN:2109chip = 'C';2110break;2111case R600:2112default:2113chip = '6';2114break;2115}2116fprintf(stderr, "bytecode %d dw -- %d gprs -- %d nstack -------------\n",2117bc->ndw, bc->ngpr, bc->nstack);2118fprintf(stderr, "shader %d -- %c\n", index++, chip);21192120LIST_FOR_EACH_ENTRY(cf, &bc->cf, list) {2121id = cf->id;2122if (cf->op == CF_NATIVE) {2123fprintf(stderr, "%04d %08X %08X CF_NATIVE\n", id, bc->bytecode[id],2124bc->bytecode[id + 1]);2125} else {2126const struct cf_op_info *cfop = r600_isa_cf(cf->op);2127if (cfop->flags & CF_ALU) {2128if (cf->eg_alu_extended) {2129fprintf(stderr, "%04d %08X %08X %s\n", id, bc->bytecode[id],2130bc->bytecode[id + 1], "ALU_EXT");2131id += 2;2132}2133fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],2134bc->bytecode[id + 1], cfop->name);2135fprintf(stderr, "%d @%d ", cf->ndw / 2, cf->addr);2136for (i = 0; i < 4; ++i) {2137if (cf->kcache[i].mode) {2138int c_start = (cf->kcache[i].addr << 4);2139int c_end = c_start + (cf->kcache[i].mode << 4);2140fprintf(stderr, "KC%d[CB%d:%d-%d%s%s] ",2141i, cf->kcache[i].bank, c_start, c_end,2142cf->kcache[i].index_mode ? " " : "",2143cf->kcache[i].index_mode ? index_mode[cf->kcache[i].index_mode] : "");2144}2145}2146fprintf(stderr, "\n");2147} else if (cfop->flags & CF_FETCH) {2148fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],2149bc->bytecode[id + 1], cfop->name);2150fprintf(stderr, "%d @%d ", cf->ndw / 4, cf->addr);2151if (cf->vpm)2152fprintf(stderr, "VPM ");2153if (cf->end_of_program)2154fprintf(stderr, "EOP ");2155fprintf(stderr, "\n");21562157} else if (cfop->flags & CF_EXP) {2158int o = 0;2159const char *exp_type[] = {"PIXEL", "POS ", "PARAM"};2160o += fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],2161bc->bytecode[id + 1], cfop->name);2162o += print_indent(o, 43);2163o += fprintf(stderr, "%s ", exp_type[cf->output.type]);2164if (cf->output.burst_count > 1) {2165o += fprintf(stderr, "%d-%d ", cf->output.array_base,2166cf->output.array_base + cf->output.burst_count - 1);21672168o += print_indent(o, 55);2169o += fprintf(stderr, "R%d-%d.", cf->output.gpr,2170cf->output.gpr + cf->output.burst_count - 1);2171} else {2172o += fprintf(stderr, "%d ", cf->output.array_base);2173o += print_indent(o, 55);2174o += fprintf(stderr, "R%d.", cf->output.gpr);2175}21762177o += print_swizzle(cf->output.swizzle_x);2178o += print_swizzle(cf->output.swizzle_y);2179o += print_swizzle(cf->output.swizzle_z);2180o += print_swizzle(cf->output.swizzle_w);21812182print_indent(o, 67);21832184fprintf(stderr, " ES:%X ", cf->output.elem_size);2185if (cf->mark)2186fprintf(stderr, "MARK ");2187if (!cf->barrier)2188fprintf(stderr, "NO_BARRIER ");2189if (cf->end_of_program)2190fprintf(stderr, "EOP ");2191fprintf(stderr, "\n");2192} else if (r600_isa_cf(cf->op)->flags & CF_MEM) {2193int o = 0;2194const char *exp_type[] = {"WRITE", "WRITE_IND", "WRITE_ACK",2195"WRITE_IND_ACK"};2196o += fprintf(stderr, "%04d %08X %08X %s ", id,2197bc->bytecode[id], bc->bytecode[id + 1], cfop->name);2198o += print_indent(o, 43);2199o += fprintf(stderr, "%s ", exp_type[cf->output.type]);22002201if (r600_isa_cf(cf->op)->flags & CF_RAT) {2202o += fprintf(stderr, "RAT%d", cf->rat.id);2203if (cf->rat.index_mode) {2204o += fprintf(stderr, "[IDX%d]", cf->rat.index_mode - 1);2205}2206o += fprintf(stderr, " INST: %d ", cf->rat.inst);2207}22082209if (cf->output.burst_count > 1) {2210o += fprintf(stderr, "%d-%d ", cf->output.array_base,2211cf->output.array_base + cf->output.burst_count - 1);2212o += print_indent(o, 55);2213o += fprintf(stderr, "R%d-%d.", cf->output.gpr,2214cf->output.gpr + cf->output.burst_count - 1);2215} else {2216o += fprintf(stderr, "%d ", cf->output.array_base);2217o += print_indent(o, 55);2218o += fprintf(stderr, "R%d.", cf->output.gpr);2219}2220for (i = 0; i < 4; ++i) {2221if (cf->output.comp_mask & (1 << i))2222o += print_swizzle(i);2223else2224o += print_swizzle(7);2225}22262227if (cf->output.type == V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE_IND ||2228cf->output.type == V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_READ_IND)2229o += fprintf(stderr, " R%d", cf->output.index_gpr);22302231o += print_indent(o, 67);22322233fprintf(stderr, " ES:%i ", cf->output.elem_size);2234if (cf->output.array_size != 0xFFF)2235fprintf(stderr, "AS:%i ", cf->output.array_size);2236if (cf->mark)2237fprintf(stderr, "MARK ");2238if (!cf->barrier)2239fprintf(stderr, "NO_BARRIER ");2240if (cf->end_of_program)2241fprintf(stderr, "EOP ");22422243if (cf->output.mark)2244fprintf(stderr, "MARK ");22452246fprintf(stderr, "\n");2247} else {2248fprintf(stderr, "%04d %08X %08X %s ", id, bc->bytecode[id],2249bc->bytecode[id + 1], cfop->name);2250fprintf(stderr, "@%d ", cf->cf_addr);2251if (cf->cond)2252fprintf(stderr, "CND:%X ", cf->cond);2253if (cf->pop_count)2254fprintf(stderr, "POP:%X ", cf->pop_count);2255if (cf->count && (cfop->flags & CF_EMIT))2256fprintf(stderr, "STREAM%d ", cf->count);2257if (cf->vpm)2258fprintf(stderr, "VPM ");2259if (cf->end_of_program)2260fprintf(stderr, "EOP ");2261fprintf(stderr, "\n");2262}2263}22642265id = cf->addr;2266nliteral = 0;2267last = 1;2268LIST_FOR_EACH_ENTRY(alu, &cf->alu, list) {2269const char *omod_str[] = {"","*2","*4","/2"};2270const struct alu_op_info *aop = r600_isa_alu(alu->op);2271int o = 0;22722273r600_bytecode_alu_nliterals(alu, literal, &nliteral);2274o += fprintf(stderr, " %04d %08X %08X ", id, bc->bytecode[id], bc->bytecode[id+1]);2275if (last)2276o += fprintf(stderr, "%4d ", ++ngr);2277else2278o += fprintf(stderr, " ");2279o += fprintf(stderr, "%c%c %c ", alu->execute_mask ? 'M':' ',2280alu->update_pred ? 'P':' ',2281alu->pred_sel ? alu->pred_sel==2 ? '0':'1':' ');22822283o += fprintf(stderr, "%s%s%s ", aop->name,2284omod_str[alu->omod], alu->dst.clamp ? "_sat":"");22852286o += print_indent(o,60);2287o += print_dst(alu);2288for (i = 0; i < aop->src_count; ++i) {2289o += fprintf(stderr, i == 0 ? ", ": ", ");2290o += print_src(alu, i);2291}22922293if (alu->bank_swizzle) {2294o += print_indent(o,75);2295o += fprintf(stderr, " BS:%d", alu->bank_swizzle);2296}22972298fprintf(stderr, "\n");2299id += 2;23002301if (alu->last) {2302for (i = 0; i < nliteral; i++, id++) {2303float *f = (float*)(bc->bytecode + id);2304o = fprintf(stderr, " %04d %08X", id, bc->bytecode[id]);2305print_indent(o, 60);2306fprintf(stderr, " %f (%d)\n", *f, *(bc->bytecode + id));2307}2308id += nliteral & 1;2309nliteral = 0;2310}2311last = alu->last;2312}23132314LIST_FOR_EACH_ENTRY(tex, &cf->tex, list) {2315int o = 0;2316o += fprintf(stderr, " %04d %08X %08X %08X ", id, bc->bytecode[id],2317bc->bytecode[id + 1], bc->bytecode[id + 2]);23182319o += fprintf(stderr, "%s ", r600_isa_fetch(tex->op)->name);23202321o += print_indent(o, 50);23222323o += fprintf(stderr, "R%d.", tex->dst_gpr);2324o += print_swizzle(tex->dst_sel_x);2325o += print_swizzle(tex->dst_sel_y);2326o += print_swizzle(tex->dst_sel_z);2327o += print_swizzle(tex->dst_sel_w);23282329o += fprintf(stderr, ", R%d.", tex->src_gpr);2330o += print_swizzle(tex->src_sel_x);2331o += print_swizzle(tex->src_sel_y);2332o += print_swizzle(tex->src_sel_z);2333o += print_swizzle(tex->src_sel_w);23342335o += fprintf(stderr, ", RID:%d", tex->resource_id);2336o += fprintf(stderr, ", SID:%d ", tex->sampler_id);23372338if (tex->sampler_index_mode)2339fprintf(stderr, "SQ_%s ", index_mode[tex->sampler_index_mode]);23402341if (tex->lod_bias)2342fprintf(stderr, "LB:%d ", tex->lod_bias);23432344fprintf(stderr, "CT:%c%c%c%c ",2345tex->coord_type_x ? 'N' : 'U',2346tex->coord_type_y ? 'N' : 'U',2347tex->coord_type_z ? 'N' : 'U',2348tex->coord_type_w ? 'N' : 'U');23492350if (tex->offset_x)2351fprintf(stderr, "OX:%d ", tex->offset_x);2352if (tex->offset_y)2353fprintf(stderr, "OY:%d ", tex->offset_y);2354if (tex->offset_z)2355fprintf(stderr, "OZ:%d ", tex->offset_z);23562357id += 4;2358fprintf(stderr, "\n");2359}23602361LIST_FOR_EACH_ENTRY(vtx, &cf->vtx, list) {2362int o = 0;2363const char * fetch_type[] = {"VERTEX", "INSTANCE", ""};2364o += fprintf(stderr, " %04d %08X %08X %08X ", id, bc->bytecode[id],2365bc->bytecode[id + 1], bc->bytecode[id + 2]);23662367o += fprintf(stderr, "%s ", r600_isa_fetch(vtx->op)->name);23682369o += print_indent(o, 50);23702371o += fprintf(stderr, "R%d.", vtx->dst_gpr);2372o += print_swizzle(vtx->dst_sel_x);2373o += print_swizzle(vtx->dst_sel_y);2374o += print_swizzle(vtx->dst_sel_z);2375o += print_swizzle(vtx->dst_sel_w);23762377o += fprintf(stderr, ", R%d.", vtx->src_gpr);2378o += print_swizzle(vtx->src_sel_x);2379if (r600_isa_fetch(vtx->op)->flags & FF_MEM)2380o += print_swizzle(vtx->src_sel_y);23812382if (vtx->offset)2383fprintf(stderr, " +%db", vtx->offset);23842385o += print_indent(o, 55);23862387fprintf(stderr, ", RID:%d ", vtx->buffer_id);23882389fprintf(stderr, "%s ", fetch_type[vtx->fetch_type]);23902391if (bc->chip_class < CAYMAN && vtx->mega_fetch_count)2392fprintf(stderr, "MFC:%d ", vtx->mega_fetch_count);23932394if (bc->chip_class >= EVERGREEN && vtx->buffer_index_mode)2395fprintf(stderr, "SQ_%s ", index_mode[vtx->buffer_index_mode]);23962397if (r600_isa_fetch(vtx->op)->flags & FF_MEM) {2398if (vtx->uncached)2399fprintf(stderr, "UNCACHED ");2400if (vtx->indexed)2401fprintf(stderr, "INDEXED:%d ", vtx->indexed);24022403fprintf(stderr, "ELEM_SIZE:%d ", vtx->elem_size);2404if (vtx->burst_count)2405fprintf(stderr, "BURST_COUNT:%d ", vtx->burst_count);2406fprintf(stderr, "ARRAY_BASE:%d ", vtx->array_base);2407fprintf(stderr, "ARRAY_SIZE:%d ", vtx->array_size);2408}24092410fprintf(stderr, "UCF:%d ", vtx->use_const_fields);2411fprintf(stderr, "FMT(DTA:%d ", vtx->data_format);2412fprintf(stderr, "NUM:%d ", vtx->num_format_all);2413fprintf(stderr, "COMP:%d ", vtx->format_comp_all);2414fprintf(stderr, "MODE:%d)\n", vtx->srf_mode_all);24152416id += 4;2417}24182419LIST_FOR_EACH_ENTRY(gds, &cf->gds, list) {2420int o = 0;2421o += fprintf(stderr, " %04d %08X %08X %08X ", id, bc->bytecode[id],2422bc->bytecode[id + 1], bc->bytecode[id + 2]);24232424o += fprintf(stderr, "%s ", r600_isa_fetch(gds->op)->name);24252426if (gds->op != FETCH_OP_TF_WRITE) {2427o += fprintf(stderr, "R%d.", gds->dst_gpr);2428o += print_swizzle(gds->dst_sel_x);2429o += print_swizzle(gds->dst_sel_y);2430o += print_swizzle(gds->dst_sel_z);2431o += print_swizzle(gds->dst_sel_w);2432}24332434o += fprintf(stderr, ", R%d.", gds->src_gpr);2435o += print_swizzle(gds->src_sel_x);2436o += print_swizzle(gds->src_sel_y);2437o += print_swizzle(gds->src_sel_z);24382439if (gds->op != FETCH_OP_TF_WRITE) {2440o += fprintf(stderr, ", R%d.", gds->src_gpr2);2441}2442if (gds->alloc_consume) {2443o += fprintf(stderr, " UAV: %d", gds->uav_id);2444if (gds->uav_index_mode)2445o += fprintf(stderr, "[%s]", index_mode[gds->uav_index_mode]);2446}2447fprintf(stderr, "\n");2448id += 4;2449}2450}24512452fprintf(stderr, "--------------------------------------\n");2453}24542455void r600_vertex_data_type(enum pipe_format pformat,2456unsigned *format,2457unsigned *num_format, unsigned *format_comp, unsigned *endian)2458{2459const struct util_format_description *desc;2460unsigned i;24612462*format = 0;2463*num_format = 0;2464*format_comp = 0;2465*endian = ENDIAN_NONE;24662467if (pformat == PIPE_FORMAT_R11G11B10_FLOAT) {2468*format = FMT_10_11_11_FLOAT;2469*endian = r600_endian_swap(32);2470return;2471}24722473if (pformat == PIPE_FORMAT_B5G6R5_UNORM) {2474*format = FMT_5_6_5;2475*endian = r600_endian_swap(16);2476return;2477}24782479if (pformat == PIPE_FORMAT_B5G5R5A1_UNORM) {2480*format = FMT_1_5_5_5;2481*endian = r600_endian_swap(16);2482return;2483}24842485if (pformat == PIPE_FORMAT_A1B5G5R5_UNORM) {2486*format = FMT_5_5_5_1;2487return;2488}24892490desc = util_format_description(pformat);2491if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {2492goto out_unknown;2493}24942495/* Find the first non-VOID channel. */2496for (i = 0; i < 4; i++) {2497if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {2498break;2499}2500}25012502*endian = r600_endian_swap(desc->channel[i].size);25032504switch (desc->channel[i].type) {2505/* Half-floats, floats, ints */2506case UTIL_FORMAT_TYPE_FLOAT:2507switch (desc->channel[i].size) {2508case 16:2509switch (desc->nr_channels) {2510case 1:2511*format = FMT_16_FLOAT;2512break;2513case 2:2514*format = FMT_16_16_FLOAT;2515break;2516case 3:2517case 4:2518*format = FMT_16_16_16_16_FLOAT;2519break;2520}2521break;2522case 32:2523switch (desc->nr_channels) {2524case 1:2525*format = FMT_32_FLOAT;2526break;2527case 2:2528*format = FMT_32_32_FLOAT;2529break;2530case 3:2531*format = FMT_32_32_32_FLOAT;2532break;2533case 4:2534*format = FMT_32_32_32_32_FLOAT;2535break;2536}2537break;2538default:2539goto out_unknown;2540}2541break;2542/* Unsigned ints */2543case UTIL_FORMAT_TYPE_UNSIGNED:2544/* Signed ints */2545case UTIL_FORMAT_TYPE_SIGNED:2546switch (desc->channel[i].size) {2547case 4:2548switch (desc->nr_channels) {2549case 2:2550*format = FMT_4_4;2551break;2552case 4:2553*format = FMT_4_4_4_4;2554break;2555}2556break;2557case 8:2558switch (desc->nr_channels) {2559case 1:2560*format = FMT_8;2561break;2562case 2:2563*format = FMT_8_8;2564break;2565case 3:2566case 4:2567*format = FMT_8_8_8_8;2568break;2569}2570break;2571case 10:2572if (desc->nr_channels != 4)2573goto out_unknown;25742575*format = FMT_2_10_10_10;2576break;2577case 16:2578switch (desc->nr_channels) {2579case 1:2580*format = FMT_16;2581break;2582case 2:2583*format = FMT_16_16;2584break;2585case 3:2586case 4:2587*format = FMT_16_16_16_16;2588break;2589}2590break;2591case 32:2592switch (desc->nr_channels) {2593case 1:2594*format = FMT_32;2595break;2596case 2:2597*format = FMT_32_32;2598break;2599case 3:2600*format = FMT_32_32_32;2601break;2602case 4:2603*format = FMT_32_32_32_32;2604break;2605}2606break;2607default:2608goto out_unknown;2609}2610break;2611default:2612goto out_unknown;2613}26142615if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {2616*format_comp = 1;2617}26182619*num_format = 0;2620if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED ||2621desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {2622if (!desc->channel[i].normalized) {2623if (desc->channel[i].pure_integer)2624*num_format = 1;2625else2626*num_format = 2;2627}2628}2629return;2630out_unknown:2631R600_ERR("unsupported vertex format %s\n", util_format_name(pformat));2632}26332634void *r600_create_vertex_fetch_shader(struct pipe_context *ctx,2635unsigned count,2636const struct pipe_vertex_element *elements)2637{2638struct r600_context *rctx = (struct r600_context *)ctx;2639struct r600_bytecode bc;2640struct r600_bytecode_vtx vtx;2641const struct util_format_description *desc;2642unsigned fetch_resource_start = rctx->b.chip_class >= EVERGREEN ? 0 : 160;2643unsigned format, num_format, format_comp, endian;2644uint32_t *bytecode;2645int i, j, r, fs_size;2646struct r600_fetch_shader *shader;2647unsigned no_sb = rctx->screen->b.debug_flags & DBG_NO_SB ||2648(rctx->screen->b.debug_flags & DBG_NIR);2649unsigned sb_disasm = !no_sb || (rctx->screen->b.debug_flags & DBG_SB_DISASM);26502651assert(count < 32);26522653memset(&bc, 0, sizeof(bc));2654r600_bytecode_init(&bc, rctx->b.chip_class, rctx->b.family,2655rctx->screen->has_compressed_msaa_texturing);26562657bc.isa = rctx->isa;26582659for (i = 0; i < count; i++) {2660if (elements[i].instance_divisor > 1) {2661if (rctx->b.chip_class == CAYMAN) {2662for (j = 0; j < 4; j++) {2663struct r600_bytecode_alu alu;2664memset(&alu, 0, sizeof(alu));2665alu.op = ALU_OP2_MULHI_UINT;2666alu.src[0].sel = 0;2667alu.src[0].chan = 3;2668alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;2669alu.src[1].value = (1ll << 32) / elements[i].instance_divisor + 1;2670alu.dst.sel = i + 1;2671alu.dst.chan = j;2672alu.dst.write = j == 3;2673alu.last = j == 3;2674if ((r = r600_bytecode_add_alu(&bc, &alu))) {2675r600_bytecode_clear(&bc);2676return NULL;2677}2678}2679} else {2680struct r600_bytecode_alu alu;2681memset(&alu, 0, sizeof(alu));2682alu.op = ALU_OP2_MULHI_UINT;2683alu.src[0].sel = 0;2684alu.src[0].chan = 3;2685alu.src[1].sel = V_SQ_ALU_SRC_LITERAL;2686alu.src[1].value = (1ll << 32) / elements[i].instance_divisor + 1;2687alu.dst.sel = i + 1;2688alu.dst.chan = 3;2689alu.dst.write = 1;2690alu.last = 1;2691if ((r = r600_bytecode_add_alu(&bc, &alu))) {2692r600_bytecode_clear(&bc);2693return NULL;2694}2695}2696}2697}26982699for (i = 0; i < count; i++) {2700r600_vertex_data_type(elements[i].src_format,2701&format, &num_format, &format_comp, &endian);27022703desc = util_format_description(elements[i].src_format);2704if (!desc) {2705r600_bytecode_clear(&bc);2706R600_ERR("unknown format %d\n", elements[i].src_format);2707return NULL;2708}27092710if (elements[i].src_offset > 65535) {2711r600_bytecode_clear(&bc);2712R600_ERR("too big src_offset: %u\n", elements[i].src_offset);2713return NULL;2714}27152716memset(&vtx, 0, sizeof(vtx));2717vtx.buffer_id = elements[i].vertex_buffer_index + fetch_resource_start;2718vtx.fetch_type = elements[i].instance_divisor ? SQ_VTX_FETCH_INSTANCE_DATA : SQ_VTX_FETCH_VERTEX_DATA;2719vtx.src_gpr = elements[i].instance_divisor > 1 ? i + 1 : 0;2720vtx.src_sel_x = elements[i].instance_divisor ? 3 : 0;2721vtx.mega_fetch_count = 0x1F;2722vtx.dst_gpr = i + 1;2723vtx.dst_sel_x = desc->swizzle[0];2724vtx.dst_sel_y = desc->swizzle[1];2725vtx.dst_sel_z = desc->swizzle[2];2726vtx.dst_sel_w = desc->swizzle[3];2727vtx.data_format = format;2728vtx.num_format_all = num_format;2729vtx.format_comp_all = format_comp;2730vtx.offset = elements[i].src_offset;2731vtx.endian = endian;27322733if ((r = r600_bytecode_add_vtx(&bc, &vtx))) {2734r600_bytecode_clear(&bc);2735return NULL;2736}2737}27382739r600_bytecode_add_cfinst(&bc, CF_OP_RET);27402741if ((r = r600_bytecode_build(&bc))) {2742r600_bytecode_clear(&bc);2743return NULL;2744}27452746if (rctx->screen->b.debug_flags & DBG_FS) {2747fprintf(stderr, "--------------------------------------------------------------\n");2748fprintf(stderr, "Vertex elements state:\n");2749for (i = 0; i < count; i++) {2750fprintf(stderr, " ");2751util_dump_vertex_element(stderr, elements+i);2752fprintf(stderr, "\n");2753}27542755if (!sb_disasm) {2756r600_bytecode_disasm(&bc);27572758fprintf(stderr, "______________________________________________________________\n");2759} else {2760r600_sb_bytecode_process(rctx, &bc, NULL, 1 /*dump*/, 0 /*optimize*/);2761}2762}27632764fs_size = bc.ndw*4;27652766/* Allocate the CSO. */2767shader = CALLOC_STRUCT(r600_fetch_shader);2768if (!shader) {2769r600_bytecode_clear(&bc);2770return NULL;2771}27722773u_suballocator_alloc(&rctx->allocator_fetch_shader, fs_size, 256,2774&shader->offset,2775(struct pipe_resource**)&shader->buffer);2776if (!shader->buffer) {2777r600_bytecode_clear(&bc);2778FREE(shader);2779return NULL;2780}27812782bytecode = r600_buffer_map_sync_with_rings2783(&rctx->b, shader->buffer,2784PIPE_MAP_WRITE | PIPE_MAP_UNSYNCHRONIZED | RADEON_MAP_TEMPORARY);2785bytecode += shader->offset / 4;27862787if (R600_BIG_ENDIAN) {2788for (i = 0; i < fs_size / 4; ++i) {2789bytecode[i] = util_cpu_to_le32(bc.bytecode[i]);2790}2791} else {2792memcpy(bytecode, bc.bytecode, fs_size);2793}2794rctx->b.ws->buffer_unmap(rctx->b.ws, shader->buffer->buf);27952796r600_bytecode_clear(&bc);2797return shader;2798}27992800void r600_bytecode_alu_read(struct r600_bytecode *bc,2801struct r600_bytecode_alu *alu, uint32_t word0, uint32_t word1)2802{2803/* WORD0 */2804alu->src[0].sel = G_SQ_ALU_WORD0_SRC0_SEL(word0);2805alu->src[0].rel = G_SQ_ALU_WORD0_SRC0_REL(word0);2806alu->src[0].chan = G_SQ_ALU_WORD0_SRC0_CHAN(word0);2807alu->src[0].neg = G_SQ_ALU_WORD0_SRC0_NEG(word0);2808alu->src[1].sel = G_SQ_ALU_WORD0_SRC1_SEL(word0);2809alu->src[1].rel = G_SQ_ALU_WORD0_SRC1_REL(word0);2810alu->src[1].chan = G_SQ_ALU_WORD0_SRC1_CHAN(word0);2811alu->src[1].neg = G_SQ_ALU_WORD0_SRC1_NEG(word0);2812alu->index_mode = G_SQ_ALU_WORD0_INDEX_MODE(word0);2813alu->pred_sel = G_SQ_ALU_WORD0_PRED_SEL(word0);2814alu->last = G_SQ_ALU_WORD0_LAST(word0);28152816/* WORD1 */2817alu->bank_swizzle = G_SQ_ALU_WORD1_BANK_SWIZZLE(word1);2818if (alu->bank_swizzle)2819alu->bank_swizzle_force = alu->bank_swizzle;2820alu->dst.sel = G_SQ_ALU_WORD1_DST_GPR(word1);2821alu->dst.rel = G_SQ_ALU_WORD1_DST_REL(word1);2822alu->dst.chan = G_SQ_ALU_WORD1_DST_CHAN(word1);2823alu->dst.clamp = G_SQ_ALU_WORD1_CLAMP(word1);2824if (G_SQ_ALU_WORD1_ENCODING(word1)) /*ALU_DWORD1_OP3*/2825{2826alu->is_op3 = 1;2827alu->src[2].sel = G_SQ_ALU_WORD1_OP3_SRC2_SEL(word1);2828alu->src[2].rel = G_SQ_ALU_WORD1_OP3_SRC2_REL(word1);2829alu->src[2].chan = G_SQ_ALU_WORD1_OP3_SRC2_CHAN(word1);2830alu->src[2].neg = G_SQ_ALU_WORD1_OP3_SRC2_NEG(word1);2831alu->op = r600_isa_alu_by_opcode(bc->isa,2832G_SQ_ALU_WORD1_OP3_ALU_INST(word1), /* is_op3 = */ 1);28332834}2835else /*ALU_DWORD1_OP2*/2836{2837alu->src[0].abs = G_SQ_ALU_WORD1_OP2_SRC0_ABS(word1);2838alu->src[1].abs = G_SQ_ALU_WORD1_OP2_SRC1_ABS(word1);2839alu->op = r600_isa_alu_by_opcode(bc->isa,2840G_SQ_ALU_WORD1_OP2_ALU_INST(word1), /* is_op3 = */ 0);2841alu->omod = G_SQ_ALU_WORD1_OP2_OMOD(word1);2842alu->dst.write = G_SQ_ALU_WORD1_OP2_WRITE_MASK(word1);2843alu->update_pred = G_SQ_ALU_WORD1_OP2_UPDATE_PRED(word1);2844alu->execute_mask =2845G_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(word1);2846}2847}28482849#if 02850void r600_bytecode_export_read(struct r600_bytecode *bc,2851struct r600_bytecode_output *output, uint32_t word0, uint32_t word1)2852{2853output->array_base = G_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(word0);2854output->type = G_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(word0);2855output->gpr = G_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(word0);2856output->elem_size = G_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(word0);28572858output->swizzle_x = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(word1);2859output->swizzle_y = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(word1);2860output->swizzle_z = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(word1);2861output->swizzle_w = G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(word1);2862output->burst_count = G_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(word1);2863output->end_of_program = G_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(word1);2864output->op = r600_isa_cf_by_opcode(bc->isa,2865G_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(word1), 0);2866output->barrier = G_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(word1);2867output->array_size = G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(word1);2868output->comp_mask = G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(word1);2869}2870#endif287128722873