2 * Copyright © 2014-2017 Broadcom
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:
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
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
29 #include "util/u_math.h"
30 #include "util/macros.h"
37 * Undefined structure, used for typechecking that you're passing the pointers
38 * to these functions correctly.
42 /** A reference to a BO used in the CL packing functions */
48 static inline void cl_pack_emit_reloc(struct vc5_cl
*cl
, const struct vc5_cl_reloc
*);
50 #define __gen_user_data struct vc5_cl
51 #define __gen_address_type struct vc5_cl_reloc
52 #define __gen_address_offset(reloc) (((reloc)->bo ? (reloc)->bo->offset : 0) + \
54 #define __gen_emit_reloc cl_pack_emit_reloc
59 struct vc5_cl_out
*next
;
64 void vc5_init_cl(struct vc5_job
*job
, struct vc5_cl
*cl
);
65 void vc5_destroy_cl(struct vc5_cl
*cl
);
66 void vc5_dump_cl(void *cl
, uint32_t size
, bool is_render
);
67 uint32_t vc5_gem_hindex(struct vc5_job
*job
, struct vc5_bo
*bo
);
69 struct PACKED unaligned_16
{ uint16_t x
; };
70 struct PACKED unaligned_32
{ uint32_t x
; };
72 static inline uint32_t cl_offset(struct vc5_cl
*cl
)
74 return (char *)cl
->next
- (char *)cl
->base
;
77 static inline struct vc5_cl_reloc
cl_get_address(struct vc5_cl
*cl
)
79 return (struct vc5_cl_reloc
){ .bo
= cl
->bo
, .offset
= cl_offset(cl
) };
83 cl_advance(struct vc5_cl_out
**cl
, uint32_t n
)
85 (*cl
) = (struct vc5_cl_out
*)((char *)(*cl
) + n
);
88 static inline struct vc5_cl_out
*
89 cl_start(struct vc5_cl
*cl
)
95 cl_end(struct vc5_cl
*cl
, struct vc5_cl_out
*next
)
98 assert(cl_offset(cl
) <= cl
->size
);
103 put_unaligned_32(struct vc5_cl_out
*ptr
, uint32_t val
)
105 struct unaligned_32
*p
= (void *)ptr
;
110 put_unaligned_16(struct vc5_cl_out
*ptr
, uint16_t val
)
112 struct unaligned_16
*p
= (void *)ptr
;
117 cl_u8(struct vc5_cl_out
**cl
, uint8_t n
)
119 *(uint8_t *)(*cl
) = n
;
124 cl_u16(struct vc5_cl_out
**cl
, uint16_t n
)
126 put_unaligned_16(*cl
, n
);
131 cl_u32(struct vc5_cl_out
**cl
, uint32_t n
)
133 put_unaligned_32(*cl
, n
);
138 cl_aligned_u32(struct vc5_cl_out
**cl
, uint32_t n
)
140 *(uint32_t *)(*cl
) = n
;
145 cl_aligned_reloc(struct vc5_cl
*cl
,
146 struct vc5_cl_out
**cl_out
,
147 struct vc5_bo
*bo
, uint32_t offset
)
149 cl_aligned_u32(cl_out
, bo
->offset
+ offset
);
150 vc5_job_add_bo(cl
->job
, bo
);
154 cl_ptr(struct vc5_cl_out
**cl
, void *ptr
)
156 *(struct vc5_cl_out
**)(*cl
) = ptr
;
157 cl_advance(cl
, sizeof(void *));
161 cl_f(struct vc5_cl_out
**cl
, float f
)
167 cl_aligned_f(struct vc5_cl_out
**cl
, float f
)
169 cl_aligned_u32(cl
, fui(f
));
173 * Reference to a BO with its associated offset, used in the pack process.
175 static inline struct vc5_cl_reloc
176 cl_address(struct vc5_bo
*bo
, uint32_t offset
)
178 struct vc5_cl_reloc reloc
= {
185 uint32_t vc5_cl_ensure_space(struct vc5_cl
*cl
, uint32_t size
, uint32_t align
);
186 void vc5_cl_ensure_space_with_branch(struct vc5_cl
*cl
, uint32_t size
);
188 #define cl_packet_header(packet) V3D33_ ## packet ## _header
189 #define cl_packet_length(packet) V3D33_ ## packet ## _length
190 #define cl_packet_pack(packet) V3D33_ ## packet ## _pack
191 #define cl_packet_struct(packet) V3D33_ ## packet
194 cl_get_emit_space(struct vc5_cl_out
**cl
, size_t size
)
197 cl_advance(cl
, size
);
201 /* Macro for setting up an emit of a CL struct. A temporary unpacked struct
202 * is created, which you get to set fields in of the form:
204 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {
205 * .flags.flat_shade_flags = 1 << 2,
208 * or default values only can be emitted with just:
210 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags);
212 * The trick here is that we make a for loop that will execute the body
213 * (either the block or the ';' after the macro invocation) exactly once.
215 #define cl_emit(cl, packet, name) \
216 for (struct cl_packet_struct(packet) name = { \
217 cl_packet_header(packet) \
219 *_loop_terminate = &name; \
220 __builtin_expect(_loop_terminate != NULL, 1); \
222 struct vc5_cl_out *cl_out = cl_start(cl); \
223 cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \
224 cl_advance(&cl_out, cl_packet_length(packet)); \
225 cl_end(cl, cl_out); \
226 _loop_terminate = NULL; \
229 #define cl_emit_with_prepacked(cl, packet, prepacked, name) \
230 for (struct cl_packet_struct(packet) name = { \
231 cl_packet_header(packet) \
233 *_loop_terminate = &name; \
234 __builtin_expect(_loop_terminate != NULL, 1); \
236 struct vc5_cl_out *cl_out = cl_start(cl); \
237 uint8_t packed[cl_packet_length(packet)]; \
238 cl_packet_pack(packet)(cl, packed, &name); \
239 for (int _i = 0; _i < cl_packet_length(packet); _i++) \
240 ((uint8_t *)cl_out)[_i] = packed[_i] | (prepacked)[_i]; \
241 cl_advance(&cl_out, cl_packet_length(packet)); \
242 cl_end(cl, cl_out); \
243 _loop_terminate = NULL; \
246 #define cl_emit_prepacked(cl, packet) do { \
247 memcpy((cl)->next, packet, sizeof(*packet)); \
248 cl_advance(&(cl)->next, sizeof(*packet)); \
251 #define v3dx_pack(packed, packet, name) \
252 for (struct cl_packet_struct(packet) name = { \
253 cl_packet_header(packet) \
255 *_loop_terminate = &name; \
256 __builtin_expect(_loop_terminate != NULL, 1); \
258 cl_packet_pack(packet)(NULL, (uint8_t *)packed, &name); \
259 VG(VALGRIND_CHECK_MEM_IS_DEFINED((uint8_t *)packed, \
260 cl_packet_length(packet))); \
261 _loop_terminate = NULL; \
265 * Helper function called by the XML-generated pack functions for filling in
266 * an address field in shader records.
268 * Since we have a private address space as of VC5, our BOs can have lifelong
269 * offsets, and all the kernel needs to know is which BOs need to be paged in
273 cl_pack_emit_reloc(struct vc5_cl
*cl
, const struct vc5_cl_reloc
*reloc
)
276 vc5_job_add_bo(cl
->job
, reloc
->bo
);
279 #endif /* VC5_CL_H */