Path: blob/21.2-virgl/src/freedreno/ir2/disasm-a2xx.c
4565 views
/*1* Copyright (c) 2012 Rob Clark <[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* 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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include <fcntl.h>24#include <stdint.h>25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <unistd.h>29#include <sys/stat.h>30#include <sys/types.h>3132#include "disasm.h"33#include "instr-a2xx.h"3435static const char *levels[] = {36"\t",37"\t\t",38"\t\t\t",39"\t\t\t\t",40"\t\t\t\t\t",41"\t\t\t\t\t\t",42"\t\t\t\t\t\t\t",43"\t\t\t\t\t\t\t\t",44"\t\t\t\t\t\t\t\t\t",45"x",46"x",47"x",48"x",49"x",50"x",51};5253static enum debug_t debug;5455/*56* ALU instructions:57*/5859static const char chan_names[] = {60'x',61'y',62'z',63'w',64/* these only apply to FETCH dst's: */65'0',66'1',67'?',68'_',69};7071static void72print_srcreg(uint32_t num, uint32_t type, uint32_t swiz, uint32_t negate,73uint32_t abs)74{75if (negate)76printf("-");77if (abs)78printf("|");79printf("%c%u", type ? 'R' : 'C', num);80if (swiz) {81int i;82printf(".");83for (i = 0; i < 4; i++) {84printf("%c", chan_names[(swiz + i) & 0x3]);85swiz >>= 2;86}87}88if (abs)89printf("|");90}9192static void93print_dstreg(uint32_t num, uint32_t mask, uint32_t dst_exp)94{95printf("%s%u", dst_exp ? "export" : "R", num);96if (mask != 0xf) {97int i;98printf(".");99for (i = 0; i < 4; i++) {100printf("%c", (mask & 0x1) ? chan_names[i] : '_');101mask >>= 1;102}103}104}105106static void107print_export_comment(uint32_t num, gl_shader_stage type)108{109const char *name = NULL;110switch (type) {111case MESA_SHADER_VERTEX:112switch (num) {113case 62:114name = "gl_Position";115break;116case 63:117name = "gl_PointSize";118break;119}120break;121case MESA_SHADER_FRAGMENT:122switch (num) {123case 0:124name = "gl_FragColor";125break;126}127break;128default:129assert(!"not reached");130}131/* if we had a symbol table here, we could look132* up the name of the varying..133*/134if (name) {135printf("\t; %s", name);136}137}138139struct {140uint32_t num_srcs;141const char *name;142} vector_instructions[0x20] = {143#define INSTR(opc, num_srcs) [opc] = {num_srcs, #opc}144INSTR(ADDv, 2),145INSTR(MULv, 2),146INSTR(MAXv, 2),147INSTR(MINv, 2),148INSTR(SETEv, 2),149INSTR(SETGTv, 2),150INSTR(SETGTEv, 2),151INSTR(SETNEv, 2),152INSTR(FRACv, 1),153INSTR(TRUNCv, 1),154INSTR(FLOORv, 1),155INSTR(MULADDv, 3),156INSTR(CNDEv, 3),157INSTR(CNDGTEv, 3),158INSTR(CNDGTv, 3),159INSTR(DOT4v, 2),160INSTR(DOT3v, 2),161INSTR(DOT2ADDv, 3), // ???162INSTR(CUBEv, 2),163INSTR(MAX4v, 1),164INSTR(PRED_SETE_PUSHv, 2),165INSTR(PRED_SETNE_PUSHv, 2),166INSTR(PRED_SETGT_PUSHv, 2),167INSTR(PRED_SETGTE_PUSHv, 2),168INSTR(KILLEv, 2),169INSTR(KILLGTv, 2),170INSTR(KILLGTEv, 2),171INSTR(KILLNEv, 2),172INSTR(DSTv, 2),173INSTR(MOVAv, 1),174}, scalar_instructions[0x40] = {175INSTR(ADDs, 1),176INSTR(ADD_PREVs, 1),177INSTR(MULs, 1),178INSTR(MUL_PREVs, 1),179INSTR(MUL_PREV2s, 1),180INSTR(MAXs, 1),181INSTR(MINs, 1),182INSTR(SETEs, 1),183INSTR(SETGTs, 1),184INSTR(SETGTEs, 1),185INSTR(SETNEs, 1),186INSTR(FRACs, 1),187INSTR(TRUNCs, 1),188INSTR(FLOORs, 1),189INSTR(EXP_IEEE, 1),190INSTR(LOG_CLAMP, 1),191INSTR(LOG_IEEE, 1),192INSTR(RECIP_CLAMP, 1),193INSTR(RECIP_FF, 1),194INSTR(RECIP_IEEE, 1),195INSTR(RECIPSQ_CLAMP, 1),196INSTR(RECIPSQ_FF, 1),197INSTR(RECIPSQ_IEEE, 1),198INSTR(MOVAs, 1),199INSTR(MOVA_FLOORs, 1),200INSTR(SUBs, 1),201INSTR(SUB_PREVs, 1),202INSTR(PRED_SETEs, 1),203INSTR(PRED_SETNEs, 1),204INSTR(PRED_SETGTs, 1),205INSTR(PRED_SETGTEs, 1),206INSTR(PRED_SET_INVs, 1),207INSTR(PRED_SET_POPs, 1),208INSTR(PRED_SET_CLRs, 1),209INSTR(PRED_SET_RESTOREs, 1),210INSTR(KILLEs, 1),211INSTR(KILLGTs, 1),212INSTR(KILLGTEs, 1),213INSTR(KILLNEs, 1),214INSTR(KILLONEs, 1),215INSTR(SQRT_IEEE, 1),216INSTR(MUL_CONST_0, 1),217INSTR(MUL_CONST_1, 1),218INSTR(ADD_CONST_0, 1),219INSTR(ADD_CONST_1, 1),220INSTR(SUB_CONST_0, 1),221INSTR(SUB_CONST_1, 1),222INSTR(SIN, 1),223INSTR(COS, 1),224INSTR(RETAIN_PREV, 1),225#undef INSTR226};227228static int229disasm_alu(uint32_t *dwords, uint32_t alu_off, int level, int sync,230gl_shader_stage type)231{232instr_alu_t *alu = (instr_alu_t *)dwords;233234printf("%s", levels[level]);235if (debug & PRINT_RAW) {236printf("%02x: %08x %08x %08x\t", alu_off, dwords[0], dwords[1],237dwords[2]);238}239240printf(" %sALU:\t", sync ? "(S)" : " ");241242printf("%s", vector_instructions[alu->vector_opc].name);243244if (alu->pred_select & 0x2) {245/* seems to work similar to conditional execution in ARM instruction246* set, so let's use a similar syntax for now:247*/248printf((alu->pred_select & 0x1) ? "EQ" : "NE");249}250251printf("\t");252253print_dstreg(alu->vector_dest, alu->vector_write_mask, alu->export_data);254printf(" = ");255if (vector_instructions[alu->vector_opc].num_srcs == 3) {256print_srcreg(alu->src3_reg, alu->src3_sel, alu->src3_swiz,257alu->src3_reg_negate, alu->src3_reg_abs);258printf(", ");259}260print_srcreg(alu->src1_reg, alu->src1_sel, alu->src1_swiz,261alu->src1_reg_negate, alu->src1_reg_abs);262if (vector_instructions[alu->vector_opc].num_srcs > 1) {263printf(", ");264print_srcreg(alu->src2_reg, alu->src2_sel, alu->src2_swiz,265alu->src2_reg_negate, alu->src2_reg_abs);266}267268if (alu->vector_clamp)269printf(" CLAMP");270271if (alu->export_data)272print_export_comment(alu->vector_dest, type);273274printf("\n");275276if (alu->scalar_write_mask || !alu->vector_write_mask) {277/* 2nd optional scalar op: */278279printf("%s", levels[level]);280if (debug & PRINT_RAW)281printf(" \t");282283if (scalar_instructions[alu->scalar_opc].name) {284printf("\t \t%s\t", scalar_instructions[alu->scalar_opc].name);285} else {286printf("\t \tOP(%u)\t", alu->scalar_opc);287}288289print_dstreg(alu->scalar_dest, alu->scalar_write_mask, alu->export_data);290printf(" = ");291print_srcreg(alu->src3_reg, alu->src3_sel, alu->src3_swiz,292alu->src3_reg_negate, alu->src3_reg_abs);293// TODO ADD/MUL must have another src?!?294if (alu->scalar_clamp)295printf(" CLAMP");296if (alu->export_data)297print_export_comment(alu->scalar_dest, type);298printf("\n");299}300301return 0;302}303304/*305* FETCH instructions:306*/307308struct {309const char *name;310} fetch_types[0xff] = {311#define TYPE(id) [id] = {#id}312TYPE(FMT_1_REVERSE),313TYPE(FMT_32_FLOAT),314TYPE(FMT_32_32_FLOAT),315TYPE(FMT_32_32_32_FLOAT),316TYPE(FMT_32_32_32_32_FLOAT),317TYPE(FMT_16),318TYPE(FMT_16_16),319TYPE(FMT_16_16_16_16),320TYPE(FMT_8),321TYPE(FMT_8_8),322TYPE(FMT_8_8_8_8),323TYPE(FMT_32),324TYPE(FMT_32_32),325TYPE(FMT_32_32_32_32),326#undef TYPE327};328329static void330print_fetch_dst(uint32_t dst_reg, uint32_t dst_swiz)331{332int i;333printf("\tR%u.", dst_reg);334for (i = 0; i < 4; i++) {335printf("%c", chan_names[dst_swiz & 0x7]);336dst_swiz >>= 3;337}338}339340static void341print_fetch_vtx(instr_fetch_t *fetch)342{343instr_fetch_vtx_t *vtx = &fetch->vtx;344345if (vtx->pred_select) {346/* seems to work similar to conditional execution in ARM instruction347* set, so let's use a similar syntax for now:348*/349printf(vtx->pred_condition ? "EQ" : "NE");350}351352print_fetch_dst(vtx->dst_reg, vtx->dst_swiz);353printf(" = R%u.", vtx->src_reg);354printf("%c", chan_names[vtx->src_swiz & 0x3]);355if (fetch_types[vtx->format].name) {356printf(" %s", fetch_types[vtx->format].name);357} else {358printf(" TYPE(0x%x)", vtx->format);359}360printf(" %s", vtx->format_comp_all ? "SIGNED" : "UNSIGNED");361if (!vtx->num_format_all)362printf(" NORMALIZED");363printf(" STRIDE(%u)", vtx->stride);364if (vtx->offset)365printf(" OFFSET(%u)", vtx->offset);366printf(" CONST(%u, %u)", vtx->const_index, vtx->const_index_sel);367if (0) {368// XXX369printf(" src_reg_am=%u", vtx->src_reg_am);370printf(" dst_reg_am=%u", vtx->dst_reg_am);371printf(" num_format_all=%u", vtx->num_format_all);372printf(" signed_rf_mode_all=%u", vtx->signed_rf_mode_all);373printf(" exp_adjust_all=%u", vtx->exp_adjust_all);374}375}376377static void378print_fetch_tex(instr_fetch_t *fetch)379{380static const char *filter[] = {381[TEX_FILTER_POINT] = "POINT",382[TEX_FILTER_LINEAR] = "LINEAR",383[TEX_FILTER_BASEMAP] = "BASEMAP",384};385static const char *aniso_filter[] = {386[ANISO_FILTER_DISABLED] = "DISABLED",387[ANISO_FILTER_MAX_1_1] = "MAX_1_1",388[ANISO_FILTER_MAX_2_1] = "MAX_2_1",389[ANISO_FILTER_MAX_4_1] = "MAX_4_1",390[ANISO_FILTER_MAX_8_1] = "MAX_8_1",391[ANISO_FILTER_MAX_16_1] = "MAX_16_1",392};393static const char *arbitrary_filter[] = {394[ARBITRARY_FILTER_2X4_SYM] = "2x4_SYM",395[ARBITRARY_FILTER_2X4_ASYM] = "2x4_ASYM",396[ARBITRARY_FILTER_4X2_SYM] = "4x2_SYM",397[ARBITRARY_FILTER_4X2_ASYM] = "4x2_ASYM",398[ARBITRARY_FILTER_4X4_SYM] = "4x4_SYM",399[ARBITRARY_FILTER_4X4_ASYM] = "4x4_ASYM",400};401static const char *sample_loc[] = {402[SAMPLE_CENTROID] = "CENTROID",403[SAMPLE_CENTER] = "CENTER",404};405instr_fetch_tex_t *tex = &fetch->tex;406uint32_t src_swiz = tex->src_swiz;407int i;408409if (tex->pred_select) {410/* seems to work similar to conditional execution in ARM instruction411* set, so let's use a similar syntax for now:412*/413printf(tex->pred_condition ? "EQ" : "NE");414}415416print_fetch_dst(tex->dst_reg, tex->dst_swiz);417printf(" = R%u.", tex->src_reg);418for (i = 0; i < 3; i++) {419printf("%c", chan_names[src_swiz & 0x3]);420src_swiz >>= 2;421}422printf(" CONST(%u)", tex->const_idx);423if (tex->fetch_valid_only)424printf(" VALID_ONLY");425if (tex->tx_coord_denorm)426printf(" DENORM");427if (tex->mag_filter != TEX_FILTER_USE_FETCH_CONST)428printf(" MAG(%s)", filter[tex->mag_filter]);429if (tex->min_filter != TEX_FILTER_USE_FETCH_CONST)430printf(" MIN(%s)", filter[tex->min_filter]);431if (tex->mip_filter != TEX_FILTER_USE_FETCH_CONST)432printf(" MIP(%s)", filter[tex->mip_filter]);433if (tex->aniso_filter != ANISO_FILTER_USE_FETCH_CONST)434printf(" ANISO(%s)", aniso_filter[tex->aniso_filter]);435if (tex->arbitrary_filter != ARBITRARY_FILTER_USE_FETCH_CONST)436printf(" ARBITRARY(%s)", arbitrary_filter[tex->arbitrary_filter]);437if (tex->vol_mag_filter != TEX_FILTER_USE_FETCH_CONST)438printf(" VOL_MAG(%s)", filter[tex->vol_mag_filter]);439if (tex->vol_min_filter != TEX_FILTER_USE_FETCH_CONST)440printf(" VOL_MIN(%s)", filter[tex->vol_min_filter]);441if (!tex->use_comp_lod) {442printf(" LOD(%u)", tex->use_comp_lod);443printf(" LOD_BIAS(%u)", tex->lod_bias);444}445if (tex->use_reg_lod) {446printf(" REG_LOD(%u)", tex->use_reg_lod);447}448if (tex->use_reg_gradients)449printf(" USE_REG_GRADIENTS");450printf(" LOCATION(%s)", sample_loc[tex->sample_location]);451if (tex->offset_x || tex->offset_y || tex->offset_z)452printf(" OFFSET(%u,%u,%u)", tex->offset_x, tex->offset_y, tex->offset_z);453}454455struct {456const char *name;457void (*fxn)(instr_fetch_t *cf);458} fetch_instructions[] = {459#define INSTR(opc, name, fxn) [opc] = {name, fxn}460INSTR(VTX_FETCH, "VERTEX", print_fetch_vtx),461INSTR(TEX_FETCH, "SAMPLE", print_fetch_tex),462INSTR(TEX_GET_BORDER_COLOR_FRAC, "?", print_fetch_tex),463INSTR(TEX_GET_COMP_TEX_LOD, "?", print_fetch_tex),464INSTR(TEX_GET_GRADIENTS, "?", print_fetch_tex),465INSTR(TEX_GET_WEIGHTS, "?", print_fetch_tex),466INSTR(TEX_SET_TEX_LOD, "SET_TEX_LOD", print_fetch_tex),467INSTR(TEX_SET_GRADIENTS_H, "?", print_fetch_tex),468INSTR(TEX_SET_GRADIENTS_V, "?", print_fetch_tex),469INSTR(TEX_RESERVED_4, "?", print_fetch_tex),470#undef INSTR471};472473static int474disasm_fetch(uint32_t *dwords, uint32_t alu_off, int level, int sync)475{476instr_fetch_t *fetch = (instr_fetch_t *)dwords;477478printf("%s", levels[level]);479if (debug & PRINT_RAW) {480printf("%02x: %08x %08x %08x\t", alu_off, dwords[0], dwords[1],481dwords[2]);482}483484printf(" %sFETCH:\t", sync ? "(S)" : " ");485printf("%s", fetch_instructions[fetch->opc].name);486fetch_instructions[fetch->opc].fxn(fetch);487printf("\n");488489return 0;490}491492/*493* CF instructions:494*/495496static int497cf_exec(instr_cf_t *cf)498{499return (cf->opc == EXEC) || (cf->opc == EXEC_END) ||500(cf->opc == COND_EXEC) || (cf->opc == COND_EXEC_END) ||501(cf->opc == COND_PRED_EXEC) || (cf->opc == COND_PRED_EXEC_END) ||502(cf->opc == COND_EXEC_PRED_CLEAN) ||503(cf->opc == COND_EXEC_PRED_CLEAN_END);504}505506static int507cf_cond_exec(instr_cf_t *cf)508{509return (cf->opc == COND_EXEC) || (cf->opc == COND_EXEC_END) ||510(cf->opc == COND_PRED_EXEC) || (cf->opc == COND_PRED_EXEC_END) ||511(cf->opc == COND_EXEC_PRED_CLEAN) ||512(cf->opc == COND_EXEC_PRED_CLEAN_END);513}514515static void516print_cf_nop(instr_cf_t *cf)517{518}519520static void521print_cf_exec(instr_cf_t *cf)522{523printf(" ADDR(0x%x) CNT(0x%x)", cf->exec.address, cf->exec.count);524if (cf->exec.yeild)525printf(" YIELD");526if (cf->exec.vc)527printf(" VC(0x%x)", cf->exec.vc);528if (cf->exec.bool_addr)529printf(" BOOL_ADDR(0x%x)", cf->exec.bool_addr);530if (cf->exec.address_mode == ABSOLUTE_ADDR)531printf(" ABSOLUTE_ADDR");532if (cf_cond_exec(cf))533printf(" COND(%d)", cf->exec.condition);534}535536static void537print_cf_loop(instr_cf_t *cf)538{539printf(" ADDR(0x%x) LOOP_ID(%d)", cf->loop.address, cf->loop.loop_id);540if (cf->loop.address_mode == ABSOLUTE_ADDR)541printf(" ABSOLUTE_ADDR");542}543544static void545print_cf_jmp_call(instr_cf_t *cf)546{547printf(" ADDR(0x%x) DIR(%d)", cf->jmp_call.address, cf->jmp_call.direction);548if (cf->jmp_call.force_call)549printf(" FORCE_CALL");550if (cf->jmp_call.predicated_jmp)551printf(" COND(%d)", cf->jmp_call.condition);552if (cf->jmp_call.bool_addr)553printf(" BOOL_ADDR(0x%x)", cf->jmp_call.bool_addr);554if (cf->jmp_call.address_mode == ABSOLUTE_ADDR)555printf(" ABSOLUTE_ADDR");556}557558static void559print_cf_alloc(instr_cf_t *cf)560{561static const char *bufname[] = {562[SQ_NO_ALLOC] = "NO ALLOC",563[SQ_POSITION] = "POSITION",564[SQ_PARAMETER_PIXEL] = "PARAM/PIXEL",565[SQ_MEMORY] = "MEMORY",566};567printf(" %s SIZE(0x%x)", bufname[cf->alloc.buffer_select], cf->alloc.size);568if (cf->alloc.no_serial)569printf(" NO_SERIAL");570if (cf->alloc.alloc_mode) // ???571printf(" ALLOC_MODE");572}573574struct {575const char *name;576void (*fxn)(instr_cf_t *cf);577} cf_instructions[] = {578#define INSTR(opc, fxn) [opc] = {#opc, fxn}579INSTR(NOP, print_cf_nop),580INSTR(EXEC, print_cf_exec),581INSTR(EXEC_END, print_cf_exec),582INSTR(COND_EXEC, print_cf_exec),583INSTR(COND_EXEC_END, print_cf_exec),584INSTR(COND_PRED_EXEC, print_cf_exec),585INSTR(COND_PRED_EXEC_END, print_cf_exec),586INSTR(LOOP_START, print_cf_loop),587INSTR(LOOP_END, print_cf_loop),588INSTR(COND_CALL, print_cf_jmp_call),589INSTR(RETURN, print_cf_jmp_call),590INSTR(COND_JMP, print_cf_jmp_call),591INSTR(ALLOC, print_cf_alloc),592INSTR(COND_EXEC_PRED_CLEAN, print_cf_exec),593INSTR(COND_EXEC_PRED_CLEAN_END, print_cf_exec),594INSTR(MARK_VS_FETCH_DONE, print_cf_nop), // ??595#undef INSTR596};597598static void599print_cf(instr_cf_t *cf, int level)600{601printf("%s", levels[level]);602if (debug & PRINT_RAW) {603uint16_t words[3];604memcpy(&words, cf, sizeof(words));605printf(" %04x %04x %04x \t", words[0], words[1], words[2]);606}607printf("%s", cf_instructions[cf->opc].name);608cf_instructions[cf->opc].fxn(cf);609printf("\n");610}611612/*613* The adreno shader microcode consists of two parts:614* 1) A CF (control-flow) program, at the header of the compiled shader,615* which refers to ALU/FETCH instructions that follow it by address.616* 2) ALU and FETCH instructions617*/618619int620disasm_a2xx(uint32_t *dwords, int sizedwords, int level, gl_shader_stage type)621{622instr_cf_t *cfs = (instr_cf_t *)dwords;623int idx, max_idx;624625for (idx = 0;; idx++) {626instr_cf_t *cf = &cfs[idx];627if (cf_exec(cf)) {628max_idx = 2 * cf->exec.address;629break;630}631}632633for (idx = 0; idx < max_idx; idx++) {634instr_cf_t *cf = &cfs[idx];635636print_cf(cf, level);637638if (cf_exec(cf)) {639uint32_t sequence = cf->exec.serialize;640uint32_t i;641for (i = 0; i < cf->exec.count; i++) {642uint32_t alu_off = (cf->exec.address + i);643if (sequence & 0x1) {644disasm_fetch(dwords + alu_off * 3, alu_off, level,645sequence & 0x2);646} else {647disasm_alu(dwords + alu_off * 3, alu_off, level, sequence & 0x2,648type);649}650sequence >>= 2;651}652}653}654655return 0;656}657658void659disasm_a2xx_set_debug(enum debug_t d)660{661debug = d;662}663664665