Path: blob/21.2-virgl/src/panfrost/midgard/helpers.h
4564 views
/* Copyright (c) 2018-2019 Alyssa Rosenzweig ([email protected])1* Copyright (C) 2019-2020 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a copy4* of this software and associated documentation files (the "Software"), to deal5* in the Software without restriction, including without limitation the rights6* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7* copies of the Software, and to permit persons to whom the Software is8* furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19* THE SOFTWARE.20*/2122#ifndef __MDG_HELPERS_H23#define __MDG_HELPERS_H2425#include "util/macros.h"26#include <stdio.h>27#include <string.h>2829#define OP_IS_LOAD_VARY_F(op) (\30op == midgard_op_ld_vary_16 || \31op == midgard_op_ld_vary_32 \32)3334#define OP_IS_PROJECTION(op) ( \35op == midgard_op_ldst_perspective_div_y || \36op == midgard_op_ldst_perspective_div_z || \37op == midgard_op_ldst_perspective_div_w \38)3940#define OP_IS_VEC4_ONLY(op) ( \41OP_IS_PROJECTION(op) || \42op == midgard_op_ld_cubemap_coords \43)4445#define OP_IS_MOVE(op) ( \46(op >= midgard_alu_op_fmov && op <= midgard_alu_op_fmov_rtp) || \47op == midgard_alu_op_imov \48)4950#define OP_IS_UBO_READ(op) ( \51op >= midgard_op_ld_ubo_u8 && \52op <= midgard_op_ld_ubo_128_bswap8 \53)5455#define OP_IS_CSEL_V(op) ( \56op == midgard_alu_op_icsel_v || \57op == midgard_alu_op_fcsel_v \58)5960#define OP_IS_CSEL(op) ( \61OP_IS_CSEL_V(op) || \62op == midgard_alu_op_icsel || \63op == midgard_alu_op_fcsel \64)6566#define OP_IS_UNSIGNED_CMP(op) ( \67op == midgard_alu_op_ult || \68op == midgard_alu_op_ule \69)7071#define OP_IS_INTEGER_CMP(op) ( \72op == midgard_alu_op_ieq || \73op == midgard_alu_op_ine || \74op == midgard_alu_op_ilt || \75op == midgard_alu_op_ile || \76OP_IS_UNSIGNED_CMP(op) \77)7879#define OP_IS_COMMON_STORE(op) ( \80op >= midgard_op_st_u8 && \81op <= midgard_op_st_128_bswap8 \82)8384#define OP_IS_IMAGE(op) ( \85(op >= midgard_op_ld_image_32f && op <= midgard_op_ld_image_32i) || \86(op >= midgard_op_st_image_32f && op <= midgard_op_st_image_32i) || \87op == midgard_op_lea_image \88)8990#define OP_IS_SPECIAL(op) ( \91(op >= midgard_op_ld_special_32f && op <= midgard_op_ld_special_32i) || \92(op >= midgard_op_st_special_32f && op <= midgard_op_st_special_32i) \93)9495#define OP_IS_PACK_COLOUR(op) ( \96(op >= midgard_op_pack_colour_f32 && op <= midgard_op_pack_colour_s32) \97)9899#define OP_IS_UNPACK_COLOUR(op) ( \100(op >= midgard_op_unpack_colour_f32 && op <= midgard_op_unpack_colour_s32) \101)102103/* Instructions that are on the load/store unit but don't access memory */104#define OP_IS_REG2REG_LDST(op) ( \105op >= midgard_op_unpack_colour_f32 && \106op <= midgard_op_ldst_perspective_div_w \107)108109/* ALU control words are single bit fields with a lot of space */110111#define ALU_ENAB_VEC_MUL (1 << 17)112#define ALU_ENAB_SCAL_ADD (1 << 19)113#define ALU_ENAB_VEC_ADD (1 << 21)114#define ALU_ENAB_SCAL_MUL (1 << 23)115#define ALU_ENAB_VEC_LUT (1 << 25)116#define ALU_ENAB_BR_COMPACT (1 << 26)117#define ALU_ENAB_BRANCH (1 << 27)118119/* Other opcode properties that don't conflict with the ALU_ENABs, non-ISA */120121/* Denotes an opcode that takes a vector input with a fixed-number of122* channels, but outputs to only a single output channel, like dot products.123* For these, to determine the effective mask, this quirk can be set. We have124* an intentional off-by-one (a la MALI_POSITIVE), since 0-channel makes no125* sense but we need to fit 4 channels in 2-bits. Similarly, 1-channel doesn't126* make sense (since then why are we quirked?), so that corresponds to "no127* count set" */128129#define OP_CHANNEL_COUNT(c) ((c - 1) << 0)130#define GET_CHANNEL_COUNT(c) ((c & (0x3 << 0)) ? ((c & (0x3 << 0)) + 1) : 0)131132/* For instructions that take a single argument, normally the first argument133* slot is used for the argument and the second slot is a dummy #0 constant.134* However, there are exceptions: instructions like fmov store their argument135* in the _second_ slot and store a dummy r24 in the first slot, designated by136* QUIRK_FLIPPED_R24 */137138#define QUIRK_FLIPPED_R24 (1 << 2)139140/* Is the op commutative? */141#define OP_COMMUTES (1 << 3)142143/* Does the op convert types between int- and float- space (i2f/f2u/etc) */144#define OP_TYPE_CONVERT (1 << 4)145146/* Is this opcode the first in a f2x (rte, rtz, rtn, rtp) sequence? If so,147* takes a roundmode argument in the IR. This has the semantic of rounding the148* source (it's all fused in), which is why it doesn't necessarily make sense149* for i2f (though folding there might be necessary for OpenCL reasons). Comes150* up in format conversion, i.e. f2u_rte */151#define MIDGARD_ROUNDS (1 << 5)152153/* Vector-independant shorthands for the above; these numbers are arbitrary and154* not from the ISA. Convert to the above with unit_enum_to_midgard */155156#define UNIT_MUL 0157#define UNIT_ADD 1158#define UNIT_LUT 2159160#define IS_ALU(tag) (tag >= TAG_ALU_4)161162/* Special register aliases */163164#define MAX_WORK_REGISTERS 16165166/* Uniforms are begin at (REGISTER_UNIFORMS - uniform_count) */167#define REGISTER_UNIFORMS 24168169/* r24 and r25 are special registers that only exist during the pipeline,170* by using them when we don't care about the register we skip a roundtrip171* to the register file. */172#define REGISTER_UNUSED 24173#define REGISTER_CONSTANT 26174#define REGISTER_LDST_BASE 26175#define REGISTER_TEXTURE_BASE 28176#define REGISTER_SELECT 31177178/* The following registers are read-only */179180/* XY is Program Counter, ZW is Stack Pointer */181#define REGISTER_LDST_PC_SP 2182183/* XY is Thread Local Storage pointer, ZW is Workgroup Local Storage pointer */184#define REGISTER_LDST_LOCAL_STORAGE_PTR 3185186#define REGISTER_LDST_LOCAL_THREAD_ID 4187#define REGISTER_LDST_GROUP_ID 5188#define REGISTER_LDST_GLOBAL_THREAD_ID 6189190/* This register is always zeroed when read. */191#define REGISTER_LDST_ZERO 7192193/* SSA helper aliases to mimic the registers. */194195#define SSA_FIXED_SHIFT 24196#define SSA_FIXED_REGISTER(reg) (((1 + (reg)) << SSA_FIXED_SHIFT) | 1)197#define SSA_REG_FROM_FIXED(reg) ((((reg) & ~1) >> SSA_FIXED_SHIFT) - 1)198#define SSA_FIXED_MINIMUM SSA_FIXED_REGISTER(0)199200#define COMPONENT_X 0x0201#define COMPONENT_Y 0x1202#define COMPONENT_Z 0x2203#define COMPONENT_W 0x3204205#define SWIZZLE_IDENTITY { \206{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \207{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \208{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, \209{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } \210}211212#define SWIZZLE_IDENTITY_4 { \213{ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \214{ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \215{ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \216{ 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \217}218219static inline unsigned220mask_of(unsigned nr_comp)221{222return (1 << nr_comp) - 1;223}224225/* See ISA notes */226227#define LDST_NOP (3)228229/* There are five ALU units: VMUL, VADD, SMUL, SADD, LUT. A given opcode is230* implemented on some subset of these units (or occassionally all of them).231* This table encodes a bit mask of valid units for each opcode, so the232* scheduler can figure where to plonk the instruction. */233234/* Shorthands for each unit */235#define UNIT_VMUL ALU_ENAB_VEC_MUL236#define UNIT_SADD ALU_ENAB_SCAL_ADD237#define UNIT_VADD ALU_ENAB_VEC_ADD238#define UNIT_SMUL ALU_ENAB_SCAL_MUL239#define UNIT_VLUT ALU_ENAB_VEC_LUT240241/* Shorthands for usual combinations of units */242243#define UNITS_MUL (UNIT_VMUL | UNIT_SMUL)244#define UNITS_ADD (UNIT_VADD | UNIT_SADD)245#define UNITS_MOST (UNITS_MUL | UNITS_ADD)246#define UNITS_ALL (UNITS_MOST | UNIT_VLUT)247#define UNITS_SCALAR (UNIT_SADD | UNIT_SMUL)248#define UNITS_VECTOR (UNIT_VMUL | UNIT_VADD)249#define UNITS_ANY_VECTOR (UNITS_VECTOR | UNIT_VLUT)250251struct mir_op_props {252const char *name;253unsigned props;254};255256/* For load/store */257258struct mir_ldst_op_props {259const char *name;260unsigned props;261};262263struct mir_tex_op_props {264const char *name;265unsigned props;266};267268struct mir_tag_props {269const char *name;270unsigned size;271};272273/* Lower 2-bits are a midgard_reg_mode */274#define GET_LDST_SIZE(c) (c & 3)275276/* Store (so the primary register is a source, not a destination */277#define LDST_STORE (1 << 2)278279/* Mask has special meaning and should not be manipulated directly */280#define LDST_SPECIAL_MASK (1 << 3)281282/* Non-store operation has side effects and should not be eliminated even if283* its mask is 0 */284#define LDST_SIDE_FX (1 << 4)285286/* Computes an address according to indirects/zext/shift/etc */287#define LDST_ADDRESS (1 << 5)288289/* Some fields such swizzle and address have special meanings */290#define LDST_ATOMIC (1 << 6)291292/* Operates on attributes/varyings (including images) */293#define LDST_ATTRIB (1 << 7)294295/* This file is common, so don't define the tables themselves. #include296* midgard_op.h if you need that, or edit midgard_ops.c directly */297298/* Duplicate bits to convert a per-component to duplicated 8-bit format,299* which is used for vector units */300301static inline unsigned302expand_writemask(unsigned mask, unsigned log2_channels)303{304unsigned o = 0;305unsigned factor = 8 >> log2_channels;306unsigned expanded = (1 << factor) - 1;307308for (unsigned i = 0; i < (1 << log2_channels); ++i)309if (mask & (1 << i))310o |= (expanded << (factor * i));311312return o;313}314315/* Coerce structs to integer */316317static inline unsigned318vector_alu_srco_unsigned(midgard_vector_alu_src src)319{320unsigned u;321memcpy(&u, &src, sizeof(src));322return u;323}324325static inline midgard_vector_alu_src326vector_alu_from_unsigned(unsigned u)327{328midgard_vector_alu_src s;329memcpy(&s, &u, sizeof(s));330return s;331}332333static inline void334mir_compose_swizzle(unsigned *left, unsigned *right, unsigned *final_out)335{336unsigned out[16];337338for (unsigned c = 0; c < 16; ++c)339out[c] = right[left[c]];340341memcpy(final_out, out, sizeof(out));342}343344/* Checks for an xyzw.. swizzle, given a mask */345346static inline bool347mir_is_simple_swizzle(unsigned *swizzle, unsigned mask)348{349for (unsigned i = 0; i < 16; ++i) {350if (!(mask & (1 << i))) continue;351352if (swizzle[i] != i)353return false;354}355356return true;357}358359/* Packs a load/store argument */360361static inline uint8_t362midgard_ldst_comp(unsigned reg, unsigned component, unsigned size)363{364assert((reg & ~1) == 0);365assert(size == 16 || size == 32 || size == 64);366367/* Shift so everything is in terms of 32-bit units */368if (size == 64) {369assert(component < 2);370component <<= 1;371} else if (size == 16) {372assert((component & 1) == 0);373component >>= 1;374}375376return component;377}378379/* Packs/unpacks a ubo index immediate */380381void midgard_pack_ubo_index_imm(midgard_load_store_word *word, unsigned index);382unsigned midgard_unpack_ubo_index_imm(midgard_load_store_word word);383384/* Packs/unpacks varying parameters.385* FIXME: IMPORTANT: We currently handle varying mode weirdly, by passing all386* parameters via an offset and using REGISTER_LDST_ZERO as base. This works387* for most parameters, but does not allow us to encode/decode direct sample388* position. */389void midgard_pack_varying_params(midgard_load_store_word *word, midgard_varying_params p);390midgard_varying_params midgard_unpack_varying_params(midgard_load_store_word word);391392/* Load/store ops' displacement helpers.393* This is useful because different types of load/store ops have different394* displacement bitsize. */395396#define UNPACK_LDST_ATTRIB_OFS(a) ((a) >> 9)397#define UNPACK_LDST_VERTEX_OFS(a) util_sign_extend((a) & 0x1FF, 9)398#define UNPACK_LDST_SELECTOR_OFS(a) ((a) >> 9)399#define UNPACK_LDST_UBO_OFS(a) ((a) >> 2)400#define UNPACK_LDST_MEM_OFS(a) ((a))401402#define PACK_LDST_ATTRIB_OFS(a) ((a) << 9)403#define PACK_LDST_VERTEX_OFS(a) ((a) & 0x1FF)404#define PACK_LDST_SELECTOR_OFS(a) ((a) << 9)405#define PACK_LDST_UBO_OFS(a) ((a) << 2)406#define PACK_LDST_MEM_OFS(a) ((a))407408static inline bool409midgard_is_branch_unit(unsigned unit)410{411return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT);412}413414/* Packs ALU mod argument */415struct midgard_instruction;416unsigned mir_pack_mod(struct midgard_instruction *ins, unsigned i, bool scalar);417418void419mir_print_constant_component(FILE *fp, const midgard_constants *consts,420unsigned c, midgard_reg_mode reg_mode, bool half,421unsigned mod, midgard_alu_op op);422423#endif424425426