1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2012-2015 Etnaviv Project
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the
12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
13b8e80941Smrg * of the Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#ifndef H_ETNA_INTERNAL
25b8e80941Smrg#define H_ETNA_INTERNAL
26b8e80941Smrg
27b8e80941Smrg#include <assert.h>
28b8e80941Smrg#include <stdbool.h>
29b8e80941Smrg#include <stdint.h>
30b8e80941Smrg
31b8e80941Smrg#include "hw/state.xml.h"
32b8e80941Smrg#include "hw/state_3d.xml.h"
33b8e80941Smrg
34b8e80941Smrg#include <etnaviv_drmif.h>
35b8e80941Smrg
36b8e80941Smrg#define ETNA_NUM_INPUTS (16)
37b8e80941Smrg#define ETNA_NUM_VARYINGS 8
38b8e80941Smrg#define ETNA_NUM_LOD (14)
39b8e80941Smrg#define ETNA_NUM_LAYERS (6)
40b8e80941Smrg#define ETNA_MAX_UNIFORMS (256)
41b8e80941Smrg#define ETNA_MAX_PIXELPIPES 2
42b8e80941Smrg
43b8e80941Smrg/* All RS operations must have width%16 = 0 */
44b8e80941Smrg#define ETNA_RS_WIDTH_MASK (16 - 1)
45b8e80941Smrg/* RS tiled operations must have height%4 = 0 */
46b8e80941Smrg#define ETNA_RS_HEIGHT_MASK (3)
47b8e80941Smrg/* PE render targets must be aligned to 64 bytes */
48b8e80941Smrg#define ETNA_PE_ALIGNMENT (64)
49b8e80941Smrg
50b8e80941Smrg/* These demarcate the margin (fixp16) between the computed sizes and the
51b8e80941Smrg  value sent to the chip. These have been set to the numbers used by the
52b8e80941Smrg  Vivante driver on gc2000. They used to be -1 for scissor right and bottom. I
53b8e80941Smrg  am not sure whether older hardware was relying on these or they were just a
54b8e80941Smrg  guess. But if so, these need to be moved to the _specs structure.
55b8e80941Smrg*/
56b8e80941Smrg#define ETNA_SE_SCISSOR_MARGIN_RIGHT (0x1119)
57b8e80941Smrg#define ETNA_SE_SCISSOR_MARGIN_BOTTOM (0x1111)
58b8e80941Smrg#define ETNA_SE_CLIP_MARGIN_RIGHT (0xffff)
59b8e80941Smrg#define ETNA_SE_CLIP_MARGIN_BOTTOM (0xffff)
60b8e80941Smrg
61b8e80941Smrg/* GPU chip 3D specs */
62b8e80941Smrgstruct etna_specs {
63b8e80941Smrg   /* HALTI (gross architecture) level. -1 for pre-HALTI. */
64b8e80941Smrg   int halti : 8;
65b8e80941Smrg   /* supports SUPERTILE (64x64) tiling? */
66b8e80941Smrg   unsigned can_supertile : 1;
67b8e80941Smrg   /* needs z=(z+w)/2, for older GCxxx */
68b8e80941Smrg   unsigned vs_need_z_div : 1;
69b8e80941Smrg   /* supports trigonometric instructions */
70b8e80941Smrg   unsigned has_sin_cos_sqrt : 1;
71b8e80941Smrg   /* has SIGN/FLOOR/CEIL instructions */
72b8e80941Smrg   unsigned has_sign_floor_ceil : 1;
73b8e80941Smrg   /* can use VS_RANGE, PS_RANGE registers*/
74b8e80941Smrg   unsigned has_shader_range_registers : 1;
75b8e80941Smrg   /* has the new sin/cos/log functions */
76b8e80941Smrg   unsigned has_new_transcendentals : 1;
77b8e80941Smrg   /* has the new dp2/dpX_norm instructions, among others */
78b8e80941Smrg   unsigned has_halti2_instructions : 1;
79b8e80941Smrg   /* supports single-buffer rendering with multiple pixel pipes */
80b8e80941Smrg   unsigned single_buffer : 1;
81b8e80941Smrg   /* has unified uniforms memory */
82b8e80941Smrg   unsigned has_unified_uniforms : 1;
83b8e80941Smrg   /* can load shader instructions from memory */
84b8e80941Smrg   unsigned has_icache : 1;
85b8e80941Smrg   /* ASTC texture support (and has associated states) */
86b8e80941Smrg   unsigned tex_astc : 1;
87b8e80941Smrg   /* has BLT engine instead of RS */
88b8e80941Smrg   unsigned use_blt : 1;
89b8e80941Smrg   /* can use any kind of wrapping mode on npot textures */
90b8e80941Smrg   unsigned npot_tex_any_wrap : 1;
91b8e80941Smrg   /* number of bits per TS tile */
92b8e80941Smrg   unsigned bits_per_tile;
93b8e80941Smrg   /* clear value for TS (dependent on bits_per_tile) */
94b8e80941Smrg   uint32_t ts_clear_value;
95b8e80941Smrg   /* base of vertex texture units */
96b8e80941Smrg   unsigned vertex_sampler_offset;
97b8e80941Smrg   /* number of fragment sampler units */
98b8e80941Smrg   unsigned fragment_sampler_count;
99b8e80941Smrg   /* number of vertex sampler units */
100b8e80941Smrg   unsigned vertex_sampler_count;
101b8e80941Smrg   /* size of vertex shader output buffer */
102b8e80941Smrg   unsigned vertex_output_buffer_size;
103b8e80941Smrg   /* maximum number of vertex element configurations */
104b8e80941Smrg   unsigned vertex_max_elements;
105b8e80941Smrg   /* size of a cached vertex (?) */
106b8e80941Smrg   unsigned vertex_cache_size;
107b8e80941Smrg   /* number of shader cores */
108b8e80941Smrg   unsigned shader_core_count;
109b8e80941Smrg   /* number of vertex streams */
110b8e80941Smrg   unsigned stream_count;
111b8e80941Smrg   /* vertex shader memory address*/
112b8e80941Smrg   uint32_t vs_offset;
113b8e80941Smrg   /* pixel shader memory address*/
114b8e80941Smrg   uint32_t ps_offset;
115b8e80941Smrg   /* vertex shader uniforms address*/
116b8e80941Smrg   uint32_t vs_uniforms_offset;
117b8e80941Smrg   /* pixel shader uniforms address*/
118b8e80941Smrg   uint32_t ps_uniforms_offset;
119b8e80941Smrg   /* vertex/fragment shader max instructions */
120b8e80941Smrg   uint32_t max_instructions;
121b8e80941Smrg   /* maximum number of varyings */
122b8e80941Smrg   unsigned max_varyings;
123b8e80941Smrg   /* maximum number of registers */
124b8e80941Smrg   unsigned max_registers;
125b8e80941Smrg   /* maximum vertex uniforms */
126b8e80941Smrg   unsigned max_vs_uniforms;
127b8e80941Smrg   /* maximum pixel uniforms */
128b8e80941Smrg   unsigned max_ps_uniforms;
129b8e80941Smrg   /* maximum texture size */
130b8e80941Smrg   unsigned max_texture_size;
131b8e80941Smrg   /* maximum texture size */
132b8e80941Smrg   unsigned max_rendertarget_size;
133b8e80941Smrg   /* available pixel pipes */
134b8e80941Smrg   unsigned pixel_pipes;
135b8e80941Smrg   /* number of constants */
136b8e80941Smrg   unsigned num_constants;
137b8e80941Smrg};
138b8e80941Smrg
139b8e80941Smrg/* Compiled Gallium state. All the different compiled state atoms are woven
140b8e80941Smrg * together and uploaded only when it is necessary to synchronize the state,
141b8e80941Smrg * for example before rendering. */
142b8e80941Smrg
143b8e80941Smrg/* Compiled pipe_blend_color */
144b8e80941Smrgstruct compiled_blend_color {
145b8e80941Smrg   float color[4];
146b8e80941Smrg   uint32_t PE_ALPHA_BLEND_COLOR;
147b8e80941Smrg};
148b8e80941Smrg
149b8e80941Smrg/* Compiled pipe_stencil_ref */
150b8e80941Smrgstruct compiled_stencil_ref {
151b8e80941Smrg   uint32_t PE_STENCIL_CONFIG;
152b8e80941Smrg   uint32_t PE_STENCIL_CONFIG_EXT;
153b8e80941Smrg};
154b8e80941Smrg
155b8e80941Smrg/* Compiled pipe_scissor_state */
156b8e80941Smrgstruct compiled_scissor_state {
157b8e80941Smrg   uint32_t SE_SCISSOR_LEFT;
158b8e80941Smrg   uint32_t SE_SCISSOR_TOP;
159b8e80941Smrg   uint32_t SE_SCISSOR_RIGHT;
160b8e80941Smrg   uint32_t SE_SCISSOR_BOTTOM;
161b8e80941Smrg   uint32_t SE_CLIP_RIGHT;
162b8e80941Smrg   uint32_t SE_CLIP_BOTTOM;
163b8e80941Smrg};
164b8e80941Smrg
165b8e80941Smrg/* Compiled pipe_viewport_state */
166b8e80941Smrgstruct compiled_viewport_state {
167b8e80941Smrg   uint32_t PA_VIEWPORT_SCALE_X;
168b8e80941Smrg   uint32_t PA_VIEWPORT_SCALE_Y;
169b8e80941Smrg   uint32_t PA_VIEWPORT_SCALE_Z;
170b8e80941Smrg   uint32_t PA_VIEWPORT_OFFSET_X;
171b8e80941Smrg   uint32_t PA_VIEWPORT_OFFSET_Y;
172b8e80941Smrg   uint32_t PA_VIEWPORT_OFFSET_Z;
173b8e80941Smrg   uint32_t SE_SCISSOR_LEFT;
174b8e80941Smrg   uint32_t SE_SCISSOR_TOP;
175b8e80941Smrg   uint32_t SE_SCISSOR_RIGHT;
176b8e80941Smrg   uint32_t SE_SCISSOR_BOTTOM;
177b8e80941Smrg   uint32_t SE_CLIP_RIGHT;
178b8e80941Smrg   uint32_t SE_CLIP_BOTTOM;
179b8e80941Smrg   uint32_t PE_DEPTH_NEAR;
180b8e80941Smrg   uint32_t PE_DEPTH_FAR;
181b8e80941Smrg};
182b8e80941Smrg
183b8e80941Smrg/* Compiled pipe_framebuffer_state */
184b8e80941Smrgstruct compiled_framebuffer_state {
185b8e80941Smrg   uint32_t GL_MULTI_SAMPLE_CONFIG;
186b8e80941Smrg   uint32_t PE_COLOR_FORMAT;
187b8e80941Smrg   uint32_t PE_DEPTH_CONFIG;
188b8e80941Smrg   struct etna_reloc PE_DEPTH_ADDR;
189b8e80941Smrg   struct etna_reloc PE_PIPE_DEPTH_ADDR[ETNA_MAX_PIXELPIPES];
190b8e80941Smrg   uint32_t PE_DEPTH_STRIDE;
191b8e80941Smrg   uint32_t PE_HDEPTH_CONTROL;
192b8e80941Smrg   uint32_t PE_DEPTH_NORMALIZE;
193b8e80941Smrg   struct etna_reloc PE_COLOR_ADDR;
194b8e80941Smrg   struct etna_reloc PE_PIPE_COLOR_ADDR[ETNA_MAX_PIXELPIPES];
195b8e80941Smrg   uint32_t PE_COLOR_STRIDE;
196b8e80941Smrg   uint32_t SE_SCISSOR_LEFT;
197b8e80941Smrg   uint32_t SE_SCISSOR_TOP;
198b8e80941Smrg   uint32_t SE_SCISSOR_RIGHT;
199b8e80941Smrg   uint32_t SE_SCISSOR_BOTTOM;
200b8e80941Smrg   uint32_t SE_CLIP_RIGHT;
201b8e80941Smrg   uint32_t SE_CLIP_BOTTOM;
202b8e80941Smrg   uint32_t RA_MULTISAMPLE_UNK00E04;
203b8e80941Smrg   uint32_t RA_MULTISAMPLE_UNK00E10[VIVS_RA_MULTISAMPLE_UNK00E10__LEN];
204b8e80941Smrg   uint32_t RA_CENTROID_TABLE[VIVS_RA_CENTROID_TABLE__LEN];
205b8e80941Smrg   uint32_t TS_MEM_CONFIG;
206b8e80941Smrg   uint32_t TS_DEPTH_CLEAR_VALUE;
207b8e80941Smrg   struct etna_reloc TS_DEPTH_STATUS_BASE;
208b8e80941Smrg   struct etna_reloc TS_DEPTH_SURFACE_BASE;
209b8e80941Smrg   uint32_t TS_COLOR_CLEAR_VALUE;
210b8e80941Smrg   struct etna_reloc TS_COLOR_STATUS_BASE;
211b8e80941Smrg   struct etna_reloc TS_COLOR_SURFACE_BASE;
212b8e80941Smrg   uint32_t PE_LOGIC_OP;
213b8e80941Smrg   bool msaa_mode; /* adds input (and possible temp) to PS */
214b8e80941Smrg};
215b8e80941Smrg
216b8e80941Smrg/* Compiled context->create_vertex_elements_state */
217b8e80941Smrgstruct compiled_vertex_elements_state {
218b8e80941Smrg   unsigned num_elements;
219b8e80941Smrg   uint32_t FE_VERTEX_ELEMENT_CONFIG[VIVS_FE_VERTEX_ELEMENT_CONFIG__LEN];
220b8e80941Smrg   uint32_t NFE_GENERIC_ATTRIB_CONFIG0[VIVS_NFE_GENERIC_ATTRIB__LEN];
221b8e80941Smrg   uint32_t NFE_GENERIC_ATTRIB_SCALE[VIVS_NFE_GENERIC_ATTRIB__LEN];
222b8e80941Smrg   uint32_t NFE_GENERIC_ATTRIB_CONFIG1[VIVS_NFE_GENERIC_ATTRIB__LEN];
223b8e80941Smrg};
224b8e80941Smrg
225b8e80941Smrg/* Compiled context->set_vertex_buffer result */
226b8e80941Smrgstruct compiled_set_vertex_buffer {
227b8e80941Smrg   uint32_t FE_VERTEX_STREAM_CONTROL;
228b8e80941Smrg   uint32_t FE_VERTEX_STREAM_UNK14680;
229b8e80941Smrg   struct etna_reloc FE_VERTEX_STREAM_BASE_ADDR;
230b8e80941Smrg};
231b8e80941Smrg
232b8e80941Smrg/* Compiled linked VS+PS shader state */
233b8e80941Smrgstruct compiled_shader_state {
234b8e80941Smrg   uint32_t RA_CONTROL;
235b8e80941Smrg   uint32_t PA_ATTRIBUTE_ELEMENT_COUNT;
236b8e80941Smrg   uint32_t PA_CONFIG;
237b8e80941Smrg   uint32_t PA_SHADER_ATTRIBUTES[VIVS_PA_SHADER_ATTRIBUTES__LEN];
238b8e80941Smrg   uint32_t VS_END_PC;
239b8e80941Smrg   uint32_t VS_OUTPUT_COUNT; /* number of outputs if point size per vertex disabled */
240b8e80941Smrg   uint32_t VS_OUTPUT_COUNT_PSIZE; /* number of outputs of point size per vertex enabled */
241b8e80941Smrg   uint32_t VS_INPUT_COUNT;
242b8e80941Smrg   uint32_t VS_TEMP_REGISTER_CONTROL;
243b8e80941Smrg   uint32_t VS_OUTPUT[4];
244b8e80941Smrg   uint32_t VS_INPUT[4];
245b8e80941Smrg   uint32_t VS_LOAD_BALANCING;
246b8e80941Smrg   uint32_t VS_START_PC;
247b8e80941Smrg   uint32_t PS_END_PC;
248b8e80941Smrg   uint32_t PS_OUTPUT_REG;
249b8e80941Smrg   uint32_t PS_INPUT_COUNT;
250b8e80941Smrg   uint32_t PS_INPUT_COUNT_MSAA; /* Adds an input */
251b8e80941Smrg   uint32_t PS_TEMP_REGISTER_CONTROL;
252b8e80941Smrg   uint32_t PS_TEMP_REGISTER_CONTROL_MSAA; /* Adds a temporary if needed to make space for extra input */
253b8e80941Smrg   uint32_t PS_CONTROL;
254b8e80941Smrg   uint32_t PS_START_PC;
255b8e80941Smrg   uint32_t GL_VARYING_TOTAL_COMPONENTS;
256b8e80941Smrg   uint32_t GL_VARYING_NUM_COMPONENTS;
257b8e80941Smrg   uint32_t GL_VARYING_COMPONENT_USE[2];
258b8e80941Smrg   uint32_t GL_HALTI5_SH_SPECIALS;
259b8e80941Smrg   unsigned vs_inst_mem_size;
260b8e80941Smrg   unsigned vs_uniforms_size;
261b8e80941Smrg   unsigned ps_inst_mem_size;
262b8e80941Smrg   unsigned ps_uniforms_size;
263b8e80941Smrg   uint32_t *VS_INST_MEM;
264b8e80941Smrg   uint32_t VS_UNIFORMS[ETNA_MAX_UNIFORMS * 4];
265b8e80941Smrg   uint32_t *PS_INST_MEM;
266b8e80941Smrg   uint32_t PS_UNIFORMS[ETNA_MAX_UNIFORMS * 4];
267b8e80941Smrg   struct etna_reloc PS_INST_ADDR;
268b8e80941Smrg   struct etna_reloc VS_INST_ADDR;
269b8e80941Smrg};
270b8e80941Smrg
271b8e80941Smrg/* state of some 3d and common registers relevant to etna driver */
272b8e80941Smrgstruct etna_3d_state {
273b8e80941Smrg   uint32_t /*05000*/ VS_UNIFORMS[VIVS_VS_UNIFORMS__LEN];
274b8e80941Smrg   uint32_t /*07000*/ PS_UNIFORMS[VIVS_PS_UNIFORMS__LEN];
275b8e80941Smrg};
276b8e80941Smrg
277b8e80941Smrg/* Helpers to assist creating and setting bitarrays (eg, for varyings).
278b8e80941Smrg * field_size must be a power of two, and <= 32. */
279b8e80941Smrg#define DEFINE_ETNA_BITARRAY(name, num, field_size) \
280b8e80941Smrg   uint32_t name[(num) * (field_size) / 32]
281b8e80941Smrg
282b8e80941Smrgstatic inline void
283b8e80941Smrgetna_bitarray_set(uint32_t *array, size_t array_size, size_t field_size,
284b8e80941Smrg                  size_t index, uint32_t value)
285b8e80941Smrg{
286b8e80941Smrg   size_t shift = (index * field_size) % 32;
287b8e80941Smrg   size_t offset = (index * field_size) / 32;
288b8e80941Smrg
289b8e80941Smrg   assert(index < array_size * 32 / field_size);
290b8e80941Smrg   assert(value < 1 << field_size);
291b8e80941Smrg
292b8e80941Smrg   array[offset] |= value << shift;
293b8e80941Smrg}
294b8e80941Smrg
295b8e80941Smrg#define etna_bitarray_set(array, field_size, index, value) \
296b8e80941Smrg   etna_bitarray_set((array), ARRAY_SIZE(array), field_size, index, value)
297b8e80941Smrg
298b8e80941Smrg#endif
299