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