1/*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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.
22 *
23 * Authors:
24 *    Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27#ifndef H_ETNAVIV_ASM
28#define H_ETNAVIV_ASM
29
30#include <stdint.h>
31#include "hw/isa.xml.h"
32
33/* Size of an instruction in 32-bit words */
34#define ETNA_INST_SIZE (4)
35/* Number of source operands per instruction */
36#define ETNA_NUM_SRC (3)
37
38/* Broadcast swizzle to all four components */
39#define INST_SWIZ_BROADCAST(x) \
40        (INST_SWIZ_X(x) | INST_SWIZ_Y(x) | INST_SWIZ_Z(x) | INST_SWIZ_W(x))
41/* Identity (NOP) swizzle */
42#define INST_SWIZ_IDENTITY \
43        (INST_SWIZ_X(0) | INST_SWIZ_Y(1) | INST_SWIZ_Z(2) | INST_SWIZ_W(3))
44/* Fully specified swizzle */
45#define INST_SWIZ(x,y,z,w) \
46        (INST_SWIZ_X(x) | INST_SWIZ_Y(y) | INST_SWIZ_Z(z) | INST_SWIZ_W(w))
47#define SWIZZLE(c0,c1,c2,c3) \
48        INST_SWIZ(INST_SWIZ_COMP_##c0, \
49                  INST_SWIZ_COMP_##c1, \
50                  INST_SWIZ_COMP_##c2, \
51                  INST_SWIZ_COMP_##c3)
52
53/*** operands ***/
54
55/* destination operand */
56struct etna_inst_dst {
57   unsigned use:1; /* 0: not in use, 1: in use */
58   unsigned amode:3; /* INST_AMODE_* */
59   unsigned reg:7; /* register number 0..127 */
60   unsigned comps:4; /* INST_COMPS_* */
61};
62
63/* texture operand */
64struct etna_inst_tex {
65   unsigned id:5; /* sampler id */
66   unsigned amode:3; /* INST_AMODE_* */
67   unsigned swiz:8; /* INST_SWIZ */
68};
69
70/* source operand */
71struct etna_inst_src {
72   unsigned use:1; /* 0: not in use, 1: in use */
73   unsigned reg:9; /* register or uniform number 0..511 */
74   unsigned swiz:8;   /* INST_SWIZ */
75   unsigned neg:1;    /* negate (flip sign) if set */
76   unsigned abs:1;    /* absolute (remove sign) if set */
77   unsigned amode:3;  /* INST_AMODE_* */
78   unsigned rgroup:3; /* INST_RGROUP_* */
79};
80
81/*** instruction ***/
82struct etna_inst {
83   uint8_t opcode; /* INST_OPCODE_* */
84   uint8_t type; /* INST_TYPE_* */
85   unsigned cond:5; /* INST_CONDITION_* */
86   unsigned sat:1; /* saturate result between 0..1 */
87   struct etna_inst_dst dst; /* destination operand */
88   struct etna_inst_tex tex; /* texture operand */
89   struct etna_inst_src src[ETNA_NUM_SRC]; /* source operand */
90   unsigned imm;  /* takes place of src[2] for BRANCH/CALL */
91};
92
93/* Compose two swizzles (computes swz1.swz2) */
94static inline uint32_t inst_swiz_compose(uint32_t swz1, uint32_t swz2)
95{
96   return INST_SWIZ_X((swz1 >> (((swz2 >> 0)&3)*2))&3) |
97          INST_SWIZ_Y((swz1 >> (((swz2 >> 2)&3)*2))&3) |
98          INST_SWIZ_Z((swz1 >> (((swz2 >> 4)&3)*2))&3) |
99          INST_SWIZ_W((swz1 >> (((swz2 >> 6)&3)*2))&3);
100};
101
102/* Return whether the rgroup is one of the uniforms */
103static inline int
104etna_rgroup_is_uniform(unsigned rgroup)
105{
106   return rgroup == INST_RGROUP_UNIFORM_0 ||
107          rgroup == INST_RGROUP_UNIFORM_1;
108}
109
110/**
111 * Build vivante instruction from structure with
112 *  opcode, cond, sat, dst_use, dst_amode,
113 *  dst_reg, dst_comps, tex_id, tex_amode, tex_swiz,
114 *  src[0-2]_reg, use, swiz, neg, abs, amode, rgroup,
115 *  imm
116 *
117 * Return 0 if successful, and a non-zero
118 * value otherwise.
119 */
120int
121etna_assemble(uint32_t *out, const struct etna_inst *inst);
122
123/**
124 * Set field imm of already-assembled instruction.
125 * This is used for filling in jump destinations in a separate pass.
126 */
127static inline void
128etna_assemble_set_imm(uint32_t *out, uint32_t imm)
129{
130    out[3] |= VIV_ISA_WORD_3_SRC2_IMM(imm);
131}
132
133#endif
134