1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora Ltd.
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, sublicense,
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 next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * 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 NONINFRINGEMENT.  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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors (Collabora):
24bf215546Sopenharmony_ci *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef __VALHALL_H
28bf215546Sopenharmony_ci#define __VALHALL_H
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdint.h>
31bf215546Sopenharmony_ci#include "bi_opcodes.h"
32bf215546Sopenharmony_ci#include "valhall_enums.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#ifdef __cplusplus
35bf215546Sopenharmony_ciextern "C" {
36bf215546Sopenharmony_ci#endif
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#define VA_NUM_GENERAL_SLOTS 3
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ciextern const uint32_t valhall_immediates[32];
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cienum va_size {
43bf215546Sopenharmony_ci   VA_SIZE_8 = 0,
44bf215546Sopenharmony_ci   VA_SIZE_16 = 1,
45bf215546Sopenharmony_ci   VA_SIZE_32 = 2,
46bf215546Sopenharmony_ci   VA_SIZE_64 = 3,
47bf215546Sopenharmony_ci};
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cienum va_unit {
50bf215546Sopenharmony_ci   /** Fused floating-point multiply-add */
51bf215546Sopenharmony_ci   VA_UNIT_FMA = 0,
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   /** Type conversion and basic arithmetic */
54bf215546Sopenharmony_ci   VA_UNIT_CVT = 1,
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   /** Special function unit */
57bf215546Sopenharmony_ci   VA_UNIT_SFU = 2,
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   /** Varying */
60bf215546Sopenharmony_ci   VA_UNIT_V = 3,
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   /** General load/store */
63bf215546Sopenharmony_ci   VA_UNIT_LS = 4,
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   /** Texture */
66bf215546Sopenharmony_ci   VA_UNIT_T = 5,
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   /** Fused varying and texture */
69bf215546Sopenharmony_ci   VA_UNIT_VT = 6,
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   /** Produces a message for a unit not otherwise specified */
72bf215546Sopenharmony_ci   VA_UNIT_NONE = 7
73bf215546Sopenharmony_ci};
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistruct va_src_info {
76bf215546Sopenharmony_ci   bool absneg : 1;
77bf215546Sopenharmony_ci   bool swizzle : 1;
78bf215546Sopenharmony_ci   bool notted : 1;
79bf215546Sopenharmony_ci   bool lane : 1;
80bf215546Sopenharmony_ci   bool lanes : 1;
81bf215546Sopenharmony_ci   bool halfswizzle : 1;
82bf215546Sopenharmony_ci   bool widen : 1;
83bf215546Sopenharmony_ci   bool combine : 1;
84bf215546Sopenharmony_ci   enum va_size size : 2;
85bf215546Sopenharmony_ci} __attribute__((packed));
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_cistruct va_opcode_info {
88bf215546Sopenharmony_ci   uint64_t exact;
89bf215546Sopenharmony_ci   struct va_src_info srcs[4];
90bf215546Sopenharmony_ci   uint8_t type_size : 8;
91bf215546Sopenharmony_ci   enum va_unit unit : 3;
92bf215546Sopenharmony_ci   unsigned nr_srcs : 3;
93bf215546Sopenharmony_ci   unsigned nr_staging_srcs : 2;
94bf215546Sopenharmony_ci   unsigned nr_staging_dests : 2;
95bf215546Sopenharmony_ci   bool has_dest : 1;
96bf215546Sopenharmony_ci   bool is_signed : 1;
97bf215546Sopenharmony_ci   bool clamp : 1;
98bf215546Sopenharmony_ci   bool round_mode : 1;
99bf215546Sopenharmony_ci   bool condition : 1;
100bf215546Sopenharmony_ci   bool result_type : 1;
101bf215546Sopenharmony_ci   bool vecsize : 1;
102bf215546Sopenharmony_ci   bool register_format : 1;
103bf215546Sopenharmony_ci   bool slot : 1;
104bf215546Sopenharmony_ci   bool sr_count : 1;
105bf215546Sopenharmony_ci   bool sr_write_count : 1;
106bf215546Sopenharmony_ci   unsigned sr_control : 2;
107bf215546Sopenharmony_ci};
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ciextern const struct va_opcode_info
110bf215546Sopenharmony_civalhall_opcodes[BI_NUM_OPCODES];
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci/* Bifrost specifies the source of bitwise operations as (A, B, shift), but
113bf215546Sopenharmony_ci * Valhall specifies (A, shift, B). We follow Bifrost conventions in the
114bf215546Sopenharmony_ci * compiler, so normalize.
115bf215546Sopenharmony_ci *
116bf215546Sopenharmony_ci * Bifrost specifies BLEND as staging + (coverage, blend descriptor), but
117bf215546Sopenharmony_ci * Valhall specifies staging + (blend descriptor, coverage). Given we put
118bf215546Sopenharmony_ci * staging sources first, this works out to the same swap as bitwise ops.
119bf215546Sopenharmony_ci */
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_cistatic inline bool
122bf215546Sopenharmony_civa_swap_12(enum bi_opcode op)
123bf215546Sopenharmony_ci{
124bf215546Sopenharmony_ci   switch (op) {
125bf215546Sopenharmony_ci   case BI_OPCODE_BLEND:
126bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_AND_I32:
127bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_AND_V2I16:
128bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_AND_V4I8:
129bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_OR_I32:
130bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_OR_V2I16:
131bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_OR_V4I8:
132bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_XOR_I32:
133bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_XOR_V2I16:
134bf215546Sopenharmony_ci   case BI_OPCODE_LSHIFT_XOR_V4I8:
135bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_AND_I32:
136bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_AND_V2I16:
137bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_AND_V4I8:
138bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_OR_I32:
139bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_OR_V2I16:
140bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_OR_V4I8:
141bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_XOR_I32:
142bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_XOR_V2I16:
143bf215546Sopenharmony_ci   case BI_OPCODE_RSHIFT_XOR_V4I8:
144bf215546Sopenharmony_ci      return true;
145bf215546Sopenharmony_ci   default:
146bf215546Sopenharmony_ci      return false;
147bf215546Sopenharmony_ci   }
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic inline struct va_src_info
151bf215546Sopenharmony_civa_src_info(enum bi_opcode op, unsigned src)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   unsigned idx = (va_swap_12(op) && (src == 1 || src == 2)) ? (3 - src) : src;
154bf215546Sopenharmony_ci   return valhall_opcodes[op].srcs[idx];
155bf215546Sopenharmony_ci}
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_cistatic inline bool
158bf215546Sopenharmony_civa_flow_is_wait_or_none(enum va_flow flow)
159bf215546Sopenharmony_ci{
160bf215546Sopenharmony_ci   return (flow <= VA_FLOW_WAIT);
161bf215546Sopenharmony_ci}
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci#ifdef __cplusplus
164bf215546Sopenharmony_ci} /* extern C */
165bf215546Sopenharmony_ci#endif
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci#endif
168