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#include "etnaviv_asm.h"
28bf215546Sopenharmony_ci#include "etnaviv_debug.h"
29bf215546Sopenharmony_ci#include "etnaviv_util.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci/* An instruction can only read from one distinct uniform.
32bf215546Sopenharmony_ci * This function verifies this property and returns true if the instruction
33bf215546Sopenharmony_ci * is deemed correct and false otherwise.
34bf215546Sopenharmony_ci */
35bf215546Sopenharmony_cistatic bool
36bf215546Sopenharmony_cicheck_uniforms(const struct etna_inst *inst)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   unsigned uni_rgroup = -1;
39bf215546Sopenharmony_ci   unsigned uni_reg = -1;
40bf215546Sopenharmony_ci   bool conflict = false;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   for (unsigned i = 0; i < ETNA_NUM_SRC; i++) {
43bf215546Sopenharmony_ci      const struct etna_inst_src *src = &inst->src[i];
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci      if (!etna_rgroup_is_uniform(src->rgroup))
46bf215546Sopenharmony_ci         continue;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      if (uni_reg == -1) { /* first uniform used */
49bf215546Sopenharmony_ci         uni_rgroup = src->rgroup;
50bf215546Sopenharmony_ci         uni_reg = src->reg;
51bf215546Sopenharmony_ci      } else { /* second or later; check that it is a re-use */
52bf215546Sopenharmony_ci         if (uni_rgroup != src->rgroup || uni_reg != src->reg) {
53bf215546Sopenharmony_ci            conflict = true;
54bf215546Sopenharmony_ci         }
55bf215546Sopenharmony_ci      }
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   return !conflict;
59bf215546Sopenharmony_ci}
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ciint
62bf215546Sopenharmony_cietna_assemble(uint32_t *out, const struct etna_inst *inst)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   /* cannot have both src2 and imm */
65bf215546Sopenharmony_ci   if (inst->imm && inst->src[2].use)
66bf215546Sopenharmony_ci      return 1;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   if (!inst->no_oneconst_limit && !check_uniforms(inst))
69bf215546Sopenharmony_ci      BUG("error: generating instruction that accesses two different uniforms");
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   assert(!(inst->opcode&~0x7f));
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   out[0] = VIV_ISA_WORD_0_OPCODE(inst->opcode & 0x3f) |
74bf215546Sopenharmony_ci            VIV_ISA_WORD_0_COND(inst->cond) |
75bf215546Sopenharmony_ci            COND(inst->sat, VIV_ISA_WORD_0_SAT) |
76bf215546Sopenharmony_ci            COND(inst->dst.use, VIV_ISA_WORD_0_DST_USE) |
77bf215546Sopenharmony_ci            VIV_ISA_WORD_0_DST_AMODE(inst->dst.amode) |
78bf215546Sopenharmony_ci            VIV_ISA_WORD_0_DST_REG(inst->dst.reg) |
79bf215546Sopenharmony_ci            VIV_ISA_WORD_0_DST_COMPS(inst->dst.write_mask) |
80bf215546Sopenharmony_ci            VIV_ISA_WORD_0_TEX_ID(inst->tex.id);
81bf215546Sopenharmony_ci   out[1] = VIV_ISA_WORD_1_TEX_AMODE(inst->tex.amode) |
82bf215546Sopenharmony_ci            VIV_ISA_WORD_1_TEX_SWIZ(inst->tex.swiz) |
83bf215546Sopenharmony_ci            COND(inst->src[0].use, VIV_ISA_WORD_1_SRC0_USE) |
84bf215546Sopenharmony_ci            VIV_ISA_WORD_1_SRC0_REG(inst->src[0].reg) |
85bf215546Sopenharmony_ci            COND(inst->type & 0x4, VIV_ISA_WORD_1_TYPE_BIT2) |
86bf215546Sopenharmony_ci            VIV_ISA_WORD_1_SRC0_SWIZ(inst->src[0].swiz) |
87bf215546Sopenharmony_ci            COND(inst->src[0].neg, VIV_ISA_WORD_1_SRC0_NEG) |
88bf215546Sopenharmony_ci            COND(inst->src[0].abs, VIV_ISA_WORD_1_SRC0_ABS);
89bf215546Sopenharmony_ci   out[2] = VIV_ISA_WORD_2_SRC0_AMODE(inst->src[0].amode) |
90bf215546Sopenharmony_ci            VIV_ISA_WORD_2_SRC0_RGROUP(inst->src[0].rgroup) |
91bf215546Sopenharmony_ci            COND(inst->src[1].use, VIV_ISA_WORD_2_SRC1_USE) |
92bf215546Sopenharmony_ci            VIV_ISA_WORD_2_SRC1_REG(inst->src[1].reg) |
93bf215546Sopenharmony_ci            COND(inst->opcode & 0x40, VIV_ISA_WORD_2_OPCODE_BIT6) |
94bf215546Sopenharmony_ci            VIV_ISA_WORD_2_SRC1_SWIZ(inst->src[1].swiz) |
95bf215546Sopenharmony_ci            COND(inst->src[1].neg, VIV_ISA_WORD_2_SRC1_NEG) |
96bf215546Sopenharmony_ci            COND(inst->src[1].abs, VIV_ISA_WORD_2_SRC1_ABS) |
97bf215546Sopenharmony_ci            VIV_ISA_WORD_2_SRC1_AMODE(inst->src[1].amode) |
98bf215546Sopenharmony_ci            VIV_ISA_WORD_2_TYPE_BIT01(inst->type & 0x3);
99bf215546Sopenharmony_ci   out[3] = VIV_ISA_WORD_3_SRC1_RGROUP(inst->src[1].rgroup) |
100bf215546Sopenharmony_ci            COND(inst->src[2].use, VIV_ISA_WORD_3_SRC2_USE) |
101bf215546Sopenharmony_ci            VIV_ISA_WORD_3_SRC2_REG(inst->src[2].reg) |
102bf215546Sopenharmony_ci            VIV_ISA_WORD_3_SRC2_SWIZ(inst->src[2].swiz) |
103bf215546Sopenharmony_ci            COND(inst->src[2].neg, VIV_ISA_WORD_3_SRC2_NEG) |
104bf215546Sopenharmony_ci            COND(inst->src[2].abs, VIV_ISA_WORD_3_SRC2_ABS) |
105bf215546Sopenharmony_ci            VIV_ISA_WORD_3_SRC2_AMODE(inst->src[2].amode) |
106bf215546Sopenharmony_ci            VIV_ISA_WORD_3_SRC2_RGROUP(inst->src[2].rgroup) |
107bf215546Sopenharmony_ci            COND(inst->sel_bit0, VIV_ISA_WORD_3_SEL_BIT0) |
108bf215546Sopenharmony_ci            COND(inst->sel_bit1, VIV_ISA_WORD_3_SEL_BIT1) |
109bf215546Sopenharmony_ci            COND(inst->dst_full, VIV_ISA_WORD_3_DST_FULL);
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   out[3] |= VIV_ISA_WORD_3_SRC2_IMM(inst->imm);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   return 0;
114bf215546Sopenharmony_ci}
115