vc4: Start using the pack header.
[mesa.git] / src / gallium / drivers / vc4 / vc4_cl.h
1 /*
2 * Copyright © 2014 Broadcom
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, sublicense,
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 next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 NONINFRINGEMENT. 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VC4_CL_H
25 #define VC4_CL_H
26
27 #include <stdint.h>
28
29 #include "util/u_math.h"
30 #include "util/macros.h"
31
32 #include "kernel/vc4_packet.h"
33
34 struct vc4_bo;
35 struct vc4_job;
36
37 /**
38 * Undefined structure, used for typechecking that you're passing the pointers
39 * to these functions correctly.
40 */
41 struct vc4_cl_out;
42
43 /** A reference to a BO used in the CL packing functions */
44 struct vc4_cl_reloc {
45 struct vc4_bo *bo;
46 uint32_t offset;
47 };
48
49 /* We don't call anything that packs a reloc yet, so don't implement it. */
50 static inline void cl_pack_emit_reloc(void *cl, const struct vc4_cl_reloc *reloc)
51 {
52 abort();
53 }
54
55 /* We don't use the data arg yet */
56 #define __gen_user_data void
57 #define __gen_address_type struct vc4_cl_reloc
58 #define __gen_address_offset(reloc) ((reloc)->offset)
59 #define __gen_emit_reloc cl_pack_emit_reloc
60
61 #include "kernel/vc4_packet.h"
62 #include "broadcom/cle/v3d_packet_v21_pack.h"
63
64 struct vc4_cl {
65 void *base;
66 struct vc4_cl_out *next;
67 struct vc4_cl_out *reloc_next;
68 uint32_t size;
69 #ifdef DEBUG
70 uint32_t reloc_count;
71 #endif
72 };
73
74 void vc4_init_cl(void *mem_ctx, struct vc4_cl *cl);
75 void vc4_reset_cl(struct vc4_cl *cl);
76 void vc4_dump_cl(void *cl, uint32_t size, bool is_render);
77 uint32_t vc4_gem_hindex(struct vc4_job *job, struct vc4_bo *bo);
78
79 struct PACKED unaligned_16 { uint16_t x; };
80 struct PACKED unaligned_32 { uint32_t x; };
81
82 static inline uint32_t cl_offset(struct vc4_cl *cl)
83 {
84 return (char *)cl->next - (char *)cl->base;
85 }
86
87 static inline void
88 cl_advance(struct vc4_cl_out **cl, uint32_t n)
89 {
90 (*cl) = (struct vc4_cl_out *)((char *)(*cl) + n);
91 }
92
93 static inline struct vc4_cl_out *
94 cl_start(struct vc4_cl *cl)
95 {
96 return cl->next;
97 }
98
99 static inline void
100 cl_end(struct vc4_cl *cl, struct vc4_cl_out *next)
101 {
102 cl->next = next;
103 assert(cl_offset(cl) <= cl->size);
104 }
105
106
107 static inline void
108 put_unaligned_32(struct vc4_cl_out *ptr, uint32_t val)
109 {
110 struct unaligned_32 *p = (void *)ptr;
111 p->x = val;
112 }
113
114 static inline void
115 put_unaligned_16(struct vc4_cl_out *ptr, uint16_t val)
116 {
117 struct unaligned_16 *p = (void *)ptr;
118 p->x = val;
119 }
120
121 static inline void
122 cl_u8(struct vc4_cl_out **cl, uint8_t n)
123 {
124 *(uint8_t *)(*cl) = n;
125 cl_advance(cl, 1);
126 }
127
128 static inline void
129 cl_u16(struct vc4_cl_out **cl, uint16_t n)
130 {
131 put_unaligned_16(*cl, n);
132 cl_advance(cl, 2);
133 }
134
135 static inline void
136 cl_u32(struct vc4_cl_out **cl, uint32_t n)
137 {
138 put_unaligned_32(*cl, n);
139 cl_advance(cl, 4);
140 }
141
142 static inline void
143 cl_aligned_u32(struct vc4_cl_out **cl, uint32_t n)
144 {
145 *(uint32_t *)(*cl) = n;
146 cl_advance(cl, 4);
147 }
148
149 static inline void
150 cl_ptr(struct vc4_cl_out **cl, void *ptr)
151 {
152 *(struct vc4_cl_out **)(*cl) = ptr;
153 cl_advance(cl, sizeof(void *));
154 }
155
156 static inline void
157 cl_f(struct vc4_cl_out **cl, float f)
158 {
159 cl_u32(cl, fui(f));
160 }
161
162 static inline void
163 cl_aligned_f(struct vc4_cl_out **cl, float f)
164 {
165 cl_aligned_u32(cl, fui(f));
166 }
167
168 static inline void
169 cl_start_reloc(struct vc4_cl *cl, struct vc4_cl_out **out, uint32_t n)
170 {
171 assert(n == 1 || n == 2);
172 #ifdef DEBUG
173 assert(cl->reloc_count == 0);
174 cl->reloc_count = n;
175 #endif
176
177 cl_u8(out, VC4_PACKET_GEM_HANDLES);
178 cl->reloc_next = *out;
179 cl_u32(out, 0); /* Space where hindex will be written. */
180 cl_u32(out, 0); /* Space where hindex will be written. */
181 }
182
183 static inline struct vc4_cl_out *
184 cl_start_shader_reloc(struct vc4_cl *cl, uint32_t n)
185 {
186 #ifdef DEBUG
187 assert(cl->reloc_count == 0);
188 cl->reloc_count = n;
189 #endif
190 cl->reloc_next = cl->next;
191
192 /* Reserve the space where hindex will be written. */
193 cl_advance(&cl->next, n * 4);
194
195 return cl->next;
196 }
197
198 static inline void
199 cl_reloc(struct vc4_job *job, struct vc4_cl *cl, struct vc4_cl_out **cl_out,
200 struct vc4_bo *bo, uint32_t offset)
201 {
202 *(uint32_t *)cl->reloc_next = vc4_gem_hindex(job, bo);
203 cl_advance(&cl->reloc_next, 4);
204
205 #ifdef DEBUG
206 cl->reloc_count--;
207 #endif
208
209 cl_u32(cl_out, offset);
210 }
211
212 static inline void
213 cl_aligned_reloc(struct vc4_job *job, struct vc4_cl *cl,
214 struct vc4_cl_out **cl_out,
215 struct vc4_bo *bo, uint32_t offset)
216 {
217 *(uint32_t *)cl->reloc_next = vc4_gem_hindex(job, bo);
218 cl_advance(&cl->reloc_next, 4);
219
220 #ifdef DEBUG
221 cl->reloc_count--;
222 #endif
223
224 cl_aligned_u32(cl_out, offset);
225 }
226
227 void cl_ensure_space(struct vc4_cl *cl, uint32_t size);
228
229 #define cl_packet_header(packet) V3D21_ ## packet ## _header
230 #define cl_packet_length(packet) V3D21_ ## packet ## _length
231 #define cl_packet_pack(packet) V3D21_ ## packet ## _pack
232 #define cl_packet_struct(packet) V3D21_ ## packet
233
234 static inline void *
235 cl_get_emit_space(struct vc4_cl_out **cl, size_t size)
236 {
237 void *addr = *cl;
238 cl_advance(cl, size);
239 return addr;
240 }
241
242 /* Macro for setting up an emit of a CL struct. A temporary unpacked struct
243 * is created, which you get to set fields in of the form:
244 *
245 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {
246 * .flags.flat_shade_flags = 1 << 2,
247 * }
248 *
249 * or default values only can be emitted with just:
250 *
251 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags);
252 *
253 * The trick here is that we make a for loop that will execute the body
254 * (either the block or the ';' after the macro invocation) exactly once.
255 * Also, *dst is actually of the wrong type, it's the
256 * uint8_t[cl_packet_length()] in the CL, not a cl_packet_struct(packet).
257 */
258 #define cl_emit(cl_out, packet, name) \
259 for (struct cl_packet_struct(packet) name = { \
260 cl_packet_header(packet) \
261 }, \
262 *_dst = cl_get_emit_space(cl_out, cl_packet_length(packet)); \
263 __builtin_expect(_dst != NULL, 1); \
264 ({ \
265 cl_packet_pack(packet)(NULL, (uint8_t *)_dst, &name); \
266 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, \
267 cl_packet_length(packet))); \
268 _dst = NULL; \
269 })) \
270
271 #endif /* VC4_CL_H */