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_ETNA_EMIT
28 #define H_ETNA_EMIT
29
30 #include "etnaviv_screen.h"
31 #include "etnaviv_resource.h"
32 #include "etnaviv_util.h"
33 #include "hw/cmdstream.xml.h"
34
35 struct etna_context;
36
37 struct etna_coalesce {
38 uint32_t start;
39 uint32_t last_reg;
40 uint32_t last_fixp;
41 };
42
43 static inline void
etna_emit_load_state(struct etna_cmd_stream * stream,const uint16_t offset,const uint16_t count,const int fixp)44 etna_emit_load_state(struct etna_cmd_stream *stream, const uint16_t offset,
45 const uint16_t count, const int fixp)
46 {
47 uint32_t v;
48
49 v = VIV_FE_LOAD_STATE_HEADER_OP_LOAD_STATE |
50 COND(fixp, VIV_FE_LOAD_STATE_HEADER_FIXP) |
51 VIV_FE_LOAD_STATE_HEADER_OFFSET(offset) |
52 (VIV_FE_LOAD_STATE_HEADER_COUNT(count) &
53 VIV_FE_LOAD_STATE_HEADER_COUNT__MASK);
54
55 etna_cmd_stream_emit(stream, v);
56 }
57
58 static inline void
etna_set_state(struct etna_cmd_stream * stream,uint32_t address,uint32_t value)59 etna_set_state(struct etna_cmd_stream *stream, uint32_t address, uint32_t value)
60 {
61 etna_cmd_stream_reserve(stream, 2);
62 etna_emit_load_state(stream, address >> 2, 1, 0);
63 etna_cmd_stream_emit(stream, value);
64 }
65
66 static inline void
etna_set_state_reloc(struct etna_cmd_stream * stream,uint32_t address,const struct etna_reloc * reloc)67 etna_set_state_reloc(struct etna_cmd_stream *stream, uint32_t address,
68 const struct etna_reloc *reloc)
69 {
70 etna_cmd_stream_reserve(stream, 2);
71 etna_emit_load_state(stream, address >> 2, 1, 0);
72 etna_cmd_stream_reloc(stream, reloc);
73 }
74
75 static inline void
etna_set_state_multi(struct etna_cmd_stream * stream,uint32_t base,uint32_t num,const uint32_t * values)76 etna_set_state_multi(struct etna_cmd_stream *stream, uint32_t base,
77 uint32_t num, const uint32_t *values)
78 {
79 if (num == 0)
80 return;
81
82 etna_cmd_stream_reserve(stream, 1 + num + 1); /* 1 extra for potential alignment */
83 etna_emit_load_state(stream, base >> 2, num, 0);
84
85 for (uint32_t i = 0; i < num; i++)
86 etna_cmd_stream_emit(stream, values[i]);
87
88 /* add potential padding */
89 if ((num % 2) == 0)
90 etna_cmd_stream_emit(stream, 0);
91 }
92
93 void
94 etna_stall(struct etna_cmd_stream *stream, uint32_t from, uint32_t to);
95
96 static inline void
etna_draw_primitives(struct etna_cmd_stream * stream,uint32_t primitive_type,uint32_t start,uint32_t count)97 etna_draw_primitives(struct etna_cmd_stream *stream, uint32_t primitive_type,
98 uint32_t start, uint32_t count)
99 {
100 etna_cmd_stream_reserve(stream, 4);
101
102 etna_cmd_stream_emit(stream, VIV_FE_DRAW_PRIMITIVES_HEADER_OP_DRAW_PRIMITIVES);
103 etna_cmd_stream_emit(stream, primitive_type);
104 etna_cmd_stream_emit(stream, start);
105 etna_cmd_stream_emit(stream, count);
106 }
107
108 static inline void
etna_draw_indexed_primitives(struct etna_cmd_stream * stream,uint32_t primitive_type,uint32_t start,uint32_t count,uint32_t offset)109 etna_draw_indexed_primitives(struct etna_cmd_stream *stream,
110 uint32_t primitive_type, uint32_t start,
111 uint32_t count, uint32_t offset)
112 {
113 etna_cmd_stream_reserve(stream, 5 + 1);
114
115 etna_cmd_stream_emit(stream, VIV_FE_DRAW_INDEXED_PRIMITIVES_HEADER_OP_DRAW_INDEXED_PRIMITIVES);
116 etna_cmd_stream_emit(stream, primitive_type);
117 etna_cmd_stream_emit(stream, start);
118 etna_cmd_stream_emit(stream, count);
119 etna_cmd_stream_emit(stream, offset);
120 etna_cmd_stream_emit(stream, 0);
121 }
122
123 /* important: this takes a vertex count, not a primitive count */
124 static inline void
etna_draw_instanced(struct etna_cmd_stream * stream,uint32_t indexed,uint32_t primitive_type,uint32_t instance_count,uint32_t vertex_count,uint32_t offset)125 etna_draw_instanced(struct etna_cmd_stream *stream,
126 uint32_t indexed, uint32_t primitive_type,
127 uint32_t instance_count,
128 uint32_t vertex_count, uint32_t offset)
129 {
130 etna_cmd_stream_reserve(stream, 3 + 1);
131 etna_cmd_stream_emit(stream,
132 VIV_FE_DRAW_INSTANCED_HEADER_OP_DRAW_INSTANCED |
133 COND(indexed, VIV_FE_DRAW_INSTANCED_HEADER_INDEXED) |
134 VIV_FE_DRAW_INSTANCED_HEADER_TYPE(primitive_type) |
135 VIV_FE_DRAW_INSTANCED_HEADER_INSTANCE_COUNT_LO(instance_count & 0xffff));
136 etna_cmd_stream_emit(stream,
137 VIV_FE_DRAW_INSTANCED_COUNT_INSTANCE_COUNT_HI(instance_count >> 16) |
138 VIV_FE_DRAW_INSTANCED_COUNT_VERTEX_COUNT(vertex_count));
139 etna_cmd_stream_emit(stream,
140 VIV_FE_DRAW_INSTANCED_START_INDEX(offset));
141 etna_cmd_stream_emit(stream, 0);
142 }
143
144 static inline void
etna_draw_indirect(struct etna_cmd_stream * stream,uint32_t primitive_type,struct pipe_resource * buffer,unsigned offset,bool indexed)145 etna_draw_indirect(struct etna_cmd_stream *stream,
146 uint32_t primitive_type,
147 struct pipe_resource *buffer,
148 unsigned offset,
149 bool indexed)
150 {
151 etna_cmd_stream_reserve(stream, 2);
152 etna_cmd_stream_emit(stream,
153 VIV_FE_DRAW_INDIRECT_HEADER_OP_DRAW_INDIRECT |
154 VIV_FE_DRAW_INDIRECT_HEADER_TYPE(primitive_type) |
155 COND(indexed, VIV_FE_DRAW_INDIRECT_HEADER_INDEXED));
156 etna_cmd_stream_reloc(stream, &(struct etna_reloc) {
157 .bo = etna_resource(buffer)->bo,
158 .flags = ETNA_RELOC_READ,
159 .offset = offset,
160 });
161 }
162
163 static inline void
etna_coalesce_start(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce)164 etna_coalesce_start(struct etna_cmd_stream *stream,
165 struct etna_coalesce *coalesce)
166 {
167 coalesce->start = etna_cmd_stream_offset(stream);
168 coalesce->last_reg = 0;
169 coalesce->last_fixp = 0;
170 }
171
172 static inline void
etna_coalesce_end(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce)173 etna_coalesce_end(struct etna_cmd_stream *stream,
174 struct etna_coalesce *coalesce)
175 {
176 uint32_t end = etna_cmd_stream_offset(stream);
177 uint32_t size = end - coalesce->start;
178
179 if (size) {
180 uint32_t offset = coalesce->start - 1;
181 uint32_t value = etna_cmd_stream_get(stream, offset);
182
183 value |= VIV_FE_LOAD_STATE_HEADER_COUNT(size);
184 etna_cmd_stream_set(stream, offset, value);
185 }
186
187 /* append needed padding */
188 if (end % 2 == 1)
189 etna_cmd_stream_emit(stream, 0xdeadbeef);
190 }
191
192 static inline void
check_coalsence(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce,uint32_t reg,uint32_t fixp)193 check_coalsence(struct etna_cmd_stream *stream, struct etna_coalesce *coalesce,
194 uint32_t reg, uint32_t fixp)
195 {
196 if (coalesce->last_reg != 0) {
197 if (((coalesce->last_reg + 4) != reg) || (coalesce->last_fixp != fixp)) {
198 etna_coalesce_end(stream, coalesce);
199 etna_emit_load_state(stream, reg >> 2, 0, fixp);
200 coalesce->start = etna_cmd_stream_offset(stream);
201 }
202 } else {
203 etna_emit_load_state(stream, reg >> 2, 0, fixp);
204 coalesce->start = etna_cmd_stream_offset(stream);
205 }
206
207 coalesce->last_reg = reg;
208 coalesce->last_fixp = fixp;
209 }
210
211 static inline void
etna_coalsence_emit(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce,uint32_t reg,uint32_t value)212 etna_coalsence_emit(struct etna_cmd_stream *stream,
213 struct etna_coalesce *coalesce, uint32_t reg,
214 uint32_t value)
215 {
216 check_coalsence(stream, coalesce, reg, 0);
217 etna_cmd_stream_emit(stream, value);
218 }
219
220 static inline void
etna_coalsence_emit_fixp(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce,uint32_t reg,uint32_t value)221 etna_coalsence_emit_fixp(struct etna_cmd_stream *stream,
222 struct etna_coalesce *coalesce, uint32_t reg,
223 uint32_t value)
224 {
225 check_coalsence(stream, coalesce, reg, 1);
226 etna_cmd_stream_emit(stream, value);
227 }
228
229 static inline void
etna_coalsence_emit_reloc(struct etna_cmd_stream * stream,struct etna_coalesce * coalesce,uint32_t reg,const struct etna_reloc * r)230 etna_coalsence_emit_reloc(struct etna_cmd_stream *stream,
231 struct etna_coalesce *coalesce, uint32_t reg,
232 const struct etna_reloc *r)
233 {
234 if (r->bo) {
235 check_coalsence(stream, coalesce, reg, 0);
236 etna_cmd_stream_reloc(stream, r);
237 }
238 }
239
240 void
241 etna_emit_state(struct etna_context *ctx);
242
243 #endif
244