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
24bf215546Sopenharmony_ci#ifndef H_ETNA_INTERNAL
25bf215546Sopenharmony_ci#define H_ETNA_INTERNAL
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <assert.h>
28bf215546Sopenharmony_ci#include <stdbool.h>
29bf215546Sopenharmony_ci#include <stdint.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "hw/common.xml.h"
32bf215546Sopenharmony_ci#include "hw/common_3d.xml.h"
33bf215546Sopenharmony_ci#include "hw/state.xml.h"
34bf215546Sopenharmony_ci#include "hw/state_3d.xml.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "drm/etnaviv_drmif.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#define ETNA_NUM_INPUTS (16)
39bf215546Sopenharmony_ci#define ETNA_NUM_VARYINGS 16
40bf215546Sopenharmony_ci#define ETNA_NUM_LOD (14)
41bf215546Sopenharmony_ci#define ETNA_NUM_LAYERS (6)
42bf215546Sopenharmony_ci#define ETNA_MAX_UNIFORMS (256)
43bf215546Sopenharmony_ci#define ETNA_MAX_CONST_BUF 16
44bf215546Sopenharmony_ci#define ETNA_MAX_PIXELPIPES 2
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci/* All RS operations must have width%16 = 0 */
47bf215546Sopenharmony_ci#define ETNA_RS_WIDTH_MASK (16 - 1)
48bf215546Sopenharmony_ci/* RS tiled operations must have height%4 = 0 */
49bf215546Sopenharmony_ci#define ETNA_RS_HEIGHT_MASK (3)
50bf215546Sopenharmony_ci/* PE render targets must be aligned to 64 bytes */
51bf215546Sopenharmony_ci#define ETNA_PE_ALIGNMENT (64)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci/* These demarcate the margin (fixp16) between the computed sizes and the
54bf215546Sopenharmony_ci  value sent to the chip. These have been set to the numbers used by the
55bf215546Sopenharmony_ci  Vivante driver on gc2000. They used to be -1 for scissor right and bottom. I
56bf215546Sopenharmony_ci  am not sure whether older hardware was relying on these or they were just a
57bf215546Sopenharmony_ci  guess. But if so, these need to be moved to the _specs structure.
58bf215546Sopenharmony_ci*/
59bf215546Sopenharmony_ci#define ETNA_SE_SCISSOR_MARGIN_RIGHT (0x1119)
60bf215546Sopenharmony_ci#define ETNA_SE_SCISSOR_MARGIN_BOTTOM (0x1111)
61bf215546Sopenharmony_ci#define ETNA_SE_CLIP_MARGIN_RIGHT (0xffff)
62bf215546Sopenharmony_ci#define ETNA_SE_CLIP_MARGIN_BOTTOM (0xffff)
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci/* GPU chip 3D specs */
65bf215546Sopenharmony_cistruct etna_specs {
66bf215546Sopenharmony_ci   /* HALTI (gross architecture) level. -1 for pre-HALTI. */
67bf215546Sopenharmony_ci   int halti : 8;
68bf215546Sopenharmony_ci   /* supports SUPERTILE (64x64) tiling? */
69bf215546Sopenharmony_ci   unsigned can_supertile : 1;
70bf215546Sopenharmony_ci   /* needs z=(z+w)/2, for older GCxxx */
71bf215546Sopenharmony_ci   unsigned vs_need_z_div : 1;
72bf215546Sopenharmony_ci   /* supports trigonometric instructions */
73bf215546Sopenharmony_ci   unsigned has_sin_cos_sqrt : 1;
74bf215546Sopenharmony_ci   /* has SIGN/FLOOR/CEIL instructions */
75bf215546Sopenharmony_ci   unsigned has_sign_floor_ceil : 1;
76bf215546Sopenharmony_ci   /* can use VS_RANGE, PS_RANGE registers*/
77bf215546Sopenharmony_ci   unsigned has_shader_range_registers : 1;
78bf215546Sopenharmony_ci   /* has the new sin/cos/log functions */
79bf215546Sopenharmony_ci   unsigned has_new_transcendentals : 1;
80bf215546Sopenharmony_ci   /* has the new dp2/dpX_norm instructions, among others */
81bf215546Sopenharmony_ci   unsigned has_halti2_instructions : 1;
82bf215546Sopenharmony_ci   /* has no limit on the number of constant sources per instruction */
83bf215546Sopenharmony_ci   unsigned has_no_oneconst_limit : 1;
84bf215546Sopenharmony_ci   /* has V4_COMPRESSION */
85bf215546Sopenharmony_ci   unsigned v4_compression : 1;
86bf215546Sopenharmony_ci   /* supports single-buffer rendering with multiple pixel pipes */
87bf215546Sopenharmony_ci   unsigned single_buffer : 1;
88bf215546Sopenharmony_ci   /* has unified uniforms memory */
89bf215546Sopenharmony_ci   unsigned has_unified_uniforms : 1;
90bf215546Sopenharmony_ci   /* can load shader instructions from memory */
91bf215546Sopenharmony_ci   unsigned has_icache : 1;
92bf215546Sopenharmony_ci   /* ASTC texture support (and has associated states) */
93bf215546Sopenharmony_ci   unsigned tex_astc : 1;
94bf215546Sopenharmony_ci   /* has BLT engine instead of RS */
95bf215546Sopenharmony_ci   unsigned use_blt : 1;
96bf215546Sopenharmony_ci   /* can use any kind of wrapping mode on npot textures */
97bf215546Sopenharmony_ci   unsigned npot_tex_any_wrap : 1;
98bf215546Sopenharmony_ci   /* supports seamless cube map */
99bf215546Sopenharmony_ci   unsigned seamless_cube_map : 1;
100bf215546Sopenharmony_ci   /* number of bits per TS tile */
101bf215546Sopenharmony_ci   unsigned bits_per_tile;
102bf215546Sopenharmony_ci   /* clear value for TS (dependent on bits_per_tile) */
103bf215546Sopenharmony_ci   uint32_t ts_clear_value;
104bf215546Sopenharmony_ci   /* base of vertex texture units */
105bf215546Sopenharmony_ci   unsigned vertex_sampler_offset;
106bf215546Sopenharmony_ci   /* number of fragment sampler units */
107bf215546Sopenharmony_ci   unsigned fragment_sampler_count;
108bf215546Sopenharmony_ci   /* number of vertex sampler units */
109bf215546Sopenharmony_ci   unsigned vertex_sampler_count;
110bf215546Sopenharmony_ci   /* size of vertex shader output buffer */
111bf215546Sopenharmony_ci   unsigned vertex_output_buffer_size;
112bf215546Sopenharmony_ci   /* maximum number of vertex element configurations */
113bf215546Sopenharmony_ci   unsigned vertex_max_elements;
114bf215546Sopenharmony_ci   /* size of a cached vertex (?) */
115bf215546Sopenharmony_ci   unsigned vertex_cache_size;
116bf215546Sopenharmony_ci   /* number of shader cores */
117bf215546Sopenharmony_ci   unsigned shader_core_count;
118bf215546Sopenharmony_ci   /* number of vertex streams */
119bf215546Sopenharmony_ci   unsigned stream_count;
120bf215546Sopenharmony_ci   /* vertex shader memory address*/
121bf215546Sopenharmony_ci   uint32_t vs_offset;
122bf215546Sopenharmony_ci   /* pixel shader memory address*/
123bf215546Sopenharmony_ci   uint32_t ps_offset;
124bf215546Sopenharmony_ci   /* vertex shader uniforms address*/
125bf215546Sopenharmony_ci   uint32_t vs_uniforms_offset;
126bf215546Sopenharmony_ci   /* pixel shader uniforms address*/
127bf215546Sopenharmony_ci   uint32_t ps_uniforms_offset;
128bf215546Sopenharmony_ci   /* vertex/fragment shader max instructions */
129bf215546Sopenharmony_ci   uint32_t max_instructions;
130bf215546Sopenharmony_ci   /* maximum number of varyings */
131bf215546Sopenharmony_ci   unsigned max_varyings;
132bf215546Sopenharmony_ci   /* maximum number of registers */
133bf215546Sopenharmony_ci   unsigned max_registers;
134bf215546Sopenharmony_ci   /* maximum vertex uniforms */
135bf215546Sopenharmony_ci   unsigned max_vs_uniforms;
136bf215546Sopenharmony_ci   /* maximum pixel uniforms */
137bf215546Sopenharmony_ci   unsigned max_ps_uniforms;
138bf215546Sopenharmony_ci   /* maximum texture size */
139bf215546Sopenharmony_ci   unsigned max_texture_size;
140bf215546Sopenharmony_ci   /* maximum texture size */
141bf215546Sopenharmony_ci   unsigned max_rendertarget_size;
142bf215546Sopenharmony_ci   /* available pixel pipes */
143bf215546Sopenharmony_ci   unsigned pixel_pipes;
144bf215546Sopenharmony_ci   /* number of constants */
145bf215546Sopenharmony_ci   unsigned num_constants;
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci/* Compiled Gallium state. All the different compiled state atoms are woven
149bf215546Sopenharmony_ci * together and uploaded only when it is necessary to synchronize the state,
150bf215546Sopenharmony_ci * for example before rendering. */
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci/* Compiled pipe_blend_color */
153bf215546Sopenharmony_cistruct compiled_blend_color {
154bf215546Sopenharmony_ci   float color[4];
155bf215546Sopenharmony_ci   uint32_t PE_ALPHA_BLEND_COLOR;
156bf215546Sopenharmony_ci   uint32_t PE_ALPHA_COLOR_EXT0;
157bf215546Sopenharmony_ci   uint32_t PE_ALPHA_COLOR_EXT1;
158bf215546Sopenharmony_ci};
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci/* Compiled pipe_stencil_ref */
161bf215546Sopenharmony_cistruct compiled_stencil_ref {
162bf215546Sopenharmony_ci   uint32_t PE_STENCIL_CONFIG[2];
163bf215546Sopenharmony_ci   uint32_t PE_STENCIL_CONFIG_EXT[2];
164bf215546Sopenharmony_ci};
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci/* Compiled pipe_viewport_state */
167bf215546Sopenharmony_cistruct compiled_viewport_state {
168bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_SCALE_X;
169bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_SCALE_Y;
170bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_SCALE_Z;
171bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_OFFSET_X;
172bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_OFFSET_Y;
173bf215546Sopenharmony_ci   uint32_t PA_VIEWPORT_OFFSET_Z;
174bf215546Sopenharmony_ci   uint32_t SE_SCISSOR_LEFT;
175bf215546Sopenharmony_ci   uint32_t SE_SCISSOR_TOP;
176bf215546Sopenharmony_ci   uint32_t SE_SCISSOR_RIGHT;
177bf215546Sopenharmony_ci   uint32_t SE_SCISSOR_BOTTOM;
178bf215546Sopenharmony_ci   uint32_t PE_DEPTH_NEAR;
179bf215546Sopenharmony_ci   uint32_t PE_DEPTH_FAR;
180bf215546Sopenharmony_ci};
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci/* Compiled pipe_framebuffer_state */
183bf215546Sopenharmony_cistruct compiled_framebuffer_state {
184bf215546Sopenharmony_ci   uint32_t GL_MULTI_SAMPLE_CONFIG;
185bf215546Sopenharmony_ci   uint32_t PE_COLOR_FORMAT;
186bf215546Sopenharmony_ci   uint32_t PE_DEPTH_CONFIG;
187bf215546Sopenharmony_ci   struct etna_reloc PE_DEPTH_ADDR;
188bf215546Sopenharmony_ci   struct etna_reloc PE_PIPE_DEPTH_ADDR[ETNA_MAX_PIXELPIPES];
189bf215546Sopenharmony_ci   uint32_t PE_DEPTH_STRIDE;
190bf215546Sopenharmony_ci   uint32_t PE_HDEPTH_CONTROL;
191bf215546Sopenharmony_ci   uint32_t PE_DEPTH_NORMALIZE;
192bf215546Sopenharmony_ci   struct etna_reloc PE_COLOR_ADDR;
193bf215546Sopenharmony_ci   struct etna_reloc PE_PIPE_COLOR_ADDR[ETNA_MAX_PIXELPIPES];
194bf215546Sopenharmony_ci   uint32_t PE_COLOR_STRIDE;
195bf215546Sopenharmony_ci   uint32_t PE_MEM_CONFIG;
196bf215546Sopenharmony_ci   uint32_t RA_MULTISAMPLE_UNK00E04;
197bf215546Sopenharmony_ci   uint32_t RA_MULTISAMPLE_UNK00E10[VIVS_RA_MULTISAMPLE_UNK00E10__LEN];
198bf215546Sopenharmony_ci   uint32_t RA_CENTROID_TABLE[VIVS_RA_CENTROID_TABLE__LEN];
199bf215546Sopenharmony_ci   uint32_t TS_MEM_CONFIG;
200bf215546Sopenharmony_ci   uint32_t TS_DEPTH_CLEAR_VALUE;
201bf215546Sopenharmony_ci   struct etna_reloc TS_DEPTH_STATUS_BASE;
202bf215546Sopenharmony_ci   struct etna_reloc TS_DEPTH_SURFACE_BASE;
203bf215546Sopenharmony_ci   uint32_t TS_COLOR_CLEAR_VALUE;
204bf215546Sopenharmony_ci   uint32_t TS_COLOR_CLEAR_VALUE_EXT;
205bf215546Sopenharmony_ci   struct etna_reloc TS_COLOR_STATUS_BASE;
206bf215546Sopenharmony_ci   struct etna_reloc TS_COLOR_SURFACE_BASE;
207bf215546Sopenharmony_ci   uint32_t PE_LOGIC_OP;
208bf215546Sopenharmony_ci   uint32_t PS_CONTROL;
209bf215546Sopenharmony_ci   uint32_t PS_CONTROL_EXT;
210bf215546Sopenharmony_ci   bool msaa_mode; /* adds input (and possible temp) to PS */
211bf215546Sopenharmony_ci};
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci/* Compiled context->create_vertex_elements_state */
214bf215546Sopenharmony_cistruct compiled_vertex_elements_state {
215bf215546Sopenharmony_ci   unsigned num_elements;
216bf215546Sopenharmony_ci   uint32_t FE_VERTEX_ELEMENT_CONFIG[VIVS_FE_VERTEX_ELEMENT_CONFIG__LEN];
217bf215546Sopenharmony_ci   uint32_t NFE_GENERIC_ATTRIB_CONFIG0[VIVS_NFE_GENERIC_ATTRIB__LEN];
218bf215546Sopenharmony_ci   uint32_t NFE_GENERIC_ATTRIB_SCALE[VIVS_NFE_GENERIC_ATTRIB__LEN];
219bf215546Sopenharmony_ci   uint32_t NFE_GENERIC_ATTRIB_CONFIG1[VIVS_NFE_GENERIC_ATTRIB__LEN];
220bf215546Sopenharmony_ci   unsigned num_buffers;
221bf215546Sopenharmony_ci   uint32_t NFE_VERTEX_STREAMS_VERTEX_DIVISOR[VIVS_NFE_VERTEX_STREAMS__LEN];
222bf215546Sopenharmony_ci};
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci/* Compiled context->set_vertex_buffer result */
225bf215546Sopenharmony_cistruct compiled_set_vertex_buffer {
226bf215546Sopenharmony_ci   uint32_t FE_VERTEX_STREAM_CONTROL;
227bf215546Sopenharmony_ci   struct etna_reloc FE_VERTEX_STREAM_BASE_ADDR;
228bf215546Sopenharmony_ci};
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci/* Compiled linked VS+PS shader state */
231bf215546Sopenharmony_cistruct compiled_shader_state {
232bf215546Sopenharmony_ci   uint32_t RA_CONTROL;
233bf215546Sopenharmony_ci   uint32_t PA_ATTRIBUTE_ELEMENT_COUNT;
234bf215546Sopenharmony_ci   uint32_t PA_CONFIG;
235bf215546Sopenharmony_ci   uint32_t PA_SHADER_ATTRIBUTES[VIVS_PA_SHADER_ATTRIBUTES__LEN];
236bf215546Sopenharmony_ci   uint32_t VS_END_PC;
237bf215546Sopenharmony_ci   uint32_t VS_OUTPUT_COUNT; /* number of outputs if point size per vertex disabled */
238bf215546Sopenharmony_ci   uint32_t VS_OUTPUT_COUNT_PSIZE; /* number of outputs of point size per vertex enabled */
239bf215546Sopenharmony_ci   uint32_t VS_INPUT_COUNT;
240bf215546Sopenharmony_ci   uint32_t VS_TEMP_REGISTER_CONTROL;
241bf215546Sopenharmony_ci   uint32_t VS_OUTPUT[4];
242bf215546Sopenharmony_ci   uint32_t VS_INPUT[4];
243bf215546Sopenharmony_ci   uint32_t VS_LOAD_BALANCING;
244bf215546Sopenharmony_ci   uint32_t VS_START_PC;
245bf215546Sopenharmony_ci   uint32_t PS_END_PC;
246bf215546Sopenharmony_ci   uint32_t PS_OUTPUT_REG;
247bf215546Sopenharmony_ci   uint32_t PS_INPUT_COUNT;
248bf215546Sopenharmony_ci   uint32_t PS_INPUT_COUNT_MSAA; /* Adds an input */
249bf215546Sopenharmony_ci   uint32_t PS_TEMP_REGISTER_CONTROL;
250bf215546Sopenharmony_ci   uint32_t PS_TEMP_REGISTER_CONTROL_MSAA; /* Adds a temporary if needed to make space for extra input */
251bf215546Sopenharmony_ci   uint32_t PS_START_PC;
252bf215546Sopenharmony_ci   uint32_t GL_VARYING_TOTAL_COMPONENTS;
253bf215546Sopenharmony_ci   uint32_t GL_VARYING_NUM_COMPONENTS[2];
254bf215546Sopenharmony_ci   uint32_t GL_VARYING_COMPONENT_USE[2];
255bf215546Sopenharmony_ci   uint32_t GL_HALTI5_SH_SPECIALS;
256bf215546Sopenharmony_ci   uint32_t FE_HALTI5_ID_CONFIG;
257bf215546Sopenharmony_ci   unsigned vs_inst_mem_size;
258bf215546Sopenharmony_ci   unsigned ps_inst_mem_size;
259bf215546Sopenharmony_ci   uint32_t *VS_INST_MEM;
260bf215546Sopenharmony_ci   uint32_t *PS_INST_MEM;
261bf215546Sopenharmony_ci   struct etna_reloc PS_INST_ADDR;
262bf215546Sopenharmony_ci   struct etna_reloc VS_INST_ADDR;
263bf215546Sopenharmony_ci   unsigned writes_z:1;
264bf215546Sopenharmony_ci   unsigned uses_discard:1;
265bf215546Sopenharmony_ci};
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci/* Helpers to assist creating and setting bitarrays (eg, for varyings).
268bf215546Sopenharmony_ci * field_size must be a power of two, and <= 32. */
269bf215546Sopenharmony_ci#define DEFINE_ETNA_BITARRAY(name, num, field_size) \
270bf215546Sopenharmony_ci   uint32_t name[(num) * (field_size) / 32]
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_cistatic inline void
273bf215546Sopenharmony_cietna_bitarray_set(uint32_t *array, size_t array_size, size_t field_size,
274bf215546Sopenharmony_ci                  size_t index, uint32_t value)
275bf215546Sopenharmony_ci{
276bf215546Sopenharmony_ci   size_t shift = (index * field_size) % 32;
277bf215546Sopenharmony_ci   size_t offset = (index * field_size) / 32;
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   assert(index < array_size * 32 / field_size);
280bf215546Sopenharmony_ci   assert(value < 1 << field_size);
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci   array[offset] |= value << shift;
283bf215546Sopenharmony_ci}
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci#define etna_bitarray_set(array, field_size, index, value) \
286bf215546Sopenharmony_ci   etna_bitarray_set((array), ARRAY_SIZE(array), field_size, index, value)
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci#endif
289