2 * Copyright (c) 2012-2015 Etnaviv Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
32 #include "util/u_math.h"
33 #include "hw/isa.xml.h"
35 /* Size of an instruction in 32-bit words */
36 #define ETNA_INST_SIZE (4)
37 /* Number of source operands per instruction */
38 #define ETNA_NUM_SRC (3)
40 /* Broadcast swizzle to all four components */
41 #define INST_SWIZ_BROADCAST(x) \
42 (INST_SWIZ_X(x) | INST_SWIZ_Y(x) | INST_SWIZ_Z(x) | INST_SWIZ_W(x))
43 /* Identity (NOP) swizzle */
44 #define INST_SWIZ_IDENTITY \
45 (INST_SWIZ_X(0) | INST_SWIZ_Y(1) | INST_SWIZ_Z(2) | INST_SWIZ_W(3))
46 /* Fully specified swizzle */
47 #define INST_SWIZ(x,y,z,w) \
48 (INST_SWIZ_X(x) | INST_SWIZ_Y(y) | INST_SWIZ_Z(z) | INST_SWIZ_W(w))
49 #define SWIZZLE(c0,c1,c2,c3) \
50 INST_SWIZ(INST_SWIZ_COMP_##c0, \
51 INST_SWIZ_COMP_##c1, \
52 INST_SWIZ_COMP_##c2, \
57 /* destination operand */
58 struct etna_inst_dst
{
59 unsigned use
:1; /* 0: not in use, 1: in use */
60 unsigned amode
:3; /* INST_AMODE_* */
61 unsigned reg
:7; /* register number 0..127 */
62 unsigned write_mask
:4; /* INST_COMPS_* */
66 struct etna_inst_tex
{
67 unsigned id
:5; /* sampler id */
68 unsigned amode
:3; /* INST_AMODE_* */
69 unsigned swiz
:8; /* INST_SWIZ */
73 struct etna_inst_src
{
74 unsigned use
:1; /* 0: not in use, 1: in use */
75 unsigned rgroup
:3; /* INST_RGROUP_* */
77 struct __attribute__((__packed__
)) {
78 unsigned reg
:9; /* register or uniform number 0..511 */
79 unsigned swiz
:8; /* INST_SWIZ */
80 unsigned neg
:1; /* negate (flip sign) if set */
81 unsigned abs
:1; /* absolute (remove sign) if set */
82 unsigned amode
:3; /* INST_AMODE_* */
84 struct __attribute__((__packed__
)) {
85 unsigned imm_val
: 20;
86 unsigned imm_type
: 2;
93 uint8_t opcode
; /* INST_OPCODE_* */
94 uint8_t type
; /* INST_TYPE_* */
95 unsigned cond
:5; /* INST_CONDITION_* */
96 unsigned sat
:1; /* saturate result between 0..1 */
97 unsigned sel_bit0
:1; /* select low half mediump */
98 unsigned sel_bit1
:1; /* select high half mediump */
99 unsigned dst_full
:1; /* write to highp register */
100 unsigned halti5
:1; /* allow multiple different uniform sources */
101 struct etna_inst_dst dst
; /* destination operand */
102 struct etna_inst_tex tex
; /* texture operand */
103 struct etna_inst_src src
[ETNA_NUM_SRC
]; /* source operand */
104 unsigned imm
; /* takes place of src[2] for BRANCH/CALL */
107 /* Compose two swizzles (computes swz1.swz2) */
108 static inline uint32_t inst_swiz_compose(uint32_t swz1
, uint32_t swz2
)
110 return INST_SWIZ_X((swz1
>> (((swz2
>> 0)&3)*2))&3) |
111 INST_SWIZ_Y((swz1
>> (((swz2
>> 2)&3)*2))&3) |
112 INST_SWIZ_Z((swz1
>> (((swz2
>> 4)&3)*2))&3) |
113 INST_SWIZ_W((swz1
>> (((swz2
>> 6)&3)*2))&3);
116 /* Compose two write_masks (computes wm1.wm2) */
117 static inline uint32_t inst_write_mask_compose(uint32_t wm1
, uint32_t wm2
)
120 for (unsigned i
= 0, j
= 0; i
< 4; i
++) {
121 if (wm2
& (1 << i
)) {
130 /* Return whether the rgroup is one of the uniforms */
132 etna_rgroup_is_uniform(unsigned rgroup
)
134 return rgroup
== INST_RGROUP_UNIFORM_0
||
135 rgroup
== INST_RGROUP_UNIFORM_1
;
138 static inline struct etna_inst_src
139 etna_immediate_src(unsigned type
, uint32_t bits
)
141 return (struct etna_inst_src
) {
143 .rgroup
= INST_RGROUP_IMMEDIATE
,
149 static inline struct etna_inst_src
150 etna_immediate_float(float x
)
152 uint32_t bits
= fui(x
);
153 assert((bits
&& 0xfff) == 0); /* 12 lsb cut off */
154 return etna_immediate_src(0, bits
>> 12);
157 static inline struct etna_inst_src
158 etna_immediate_int(int x
)
160 assert(x
>= -0x80000 && x
< 0x80000); /* 20-bit signed int */
161 return etna_immediate_src(1, x
);
165 * Build vivante instruction from structure with
166 * opcode, cond, sat, dst_use, dst_amode,
167 * dst_reg, dst_comps, tex_id, tex_amode, tex_swiz,
168 * src[0-2]_reg, use, swiz, neg, abs, amode, rgroup,
171 * Return 0 if successful, and a non-zero
175 etna_assemble(uint32_t *out
, const struct etna_inst
*inst
);
178 * Set field imm of already-assembled instruction.
179 * This is used for filling in jump destinations in a separate pass.
182 etna_assemble_set_imm(uint32_t *out
, uint32_t imm
)
184 out
[3] |= VIV_ISA_WORD_3_SRC2_IMM(imm
);