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
32struct vc4_bo;
33struct vc4_job;
34struct vc4_cl;
35
36/**
37 * Undefined structure, used for typechecking that you're passing the pointers
38 * to these functions correctly.
39 */
40struct vc4_cl_out;
41
42/** A reference to a BO used in the CL packing functions */
43struct vc4_cl_reloc {
44        struct vc4_bo *bo;
45        uint32_t offset;
46};
47
48static inline void cl_pack_emit_reloc(struct vc4_cl *cl, const struct vc4_cl_reloc *);
49
50#define __gen_user_data struct vc4_cl
51#define __gen_address_type struct vc4_cl_reloc
52#define __gen_address_offset(reloc) ((reloc)->offset)
53#define __gen_emit_reloc cl_pack_emit_reloc
54
55#include "kernel/vc4_packet.h"
56#include "broadcom/cle/v3d_packet_v21_pack.h"
57
58struct vc4_cl {
59        void *base;
60        struct vc4_job *job;
61        struct vc4_cl_out *next;
62        struct vc4_cl_out *reloc_next;
63        uint32_t size;
64#ifndef NDEBUG
65        uint32_t reloc_count;
66#endif
67};
68
69void vc4_init_cl(struct vc4_job *job, struct vc4_cl *cl);
70void vc4_reset_cl(struct vc4_cl *cl);
71uint32_t vc4_gem_hindex(struct vc4_job *job, struct vc4_bo *bo);
72
73struct PACKED unaligned_16 { uint16_t x; };
74struct PACKED unaligned_32 { uint32_t x; };
75
76static inline uint32_t cl_offset(struct vc4_cl *cl)
77{
78        return (char *)cl->next - (char *)cl->base;
79}
80
81static inline void
82cl_advance(struct vc4_cl_out **cl, uint32_t n)
83{
84        (*cl) = (struct vc4_cl_out *)((char *)(*cl) + n);
85}
86
87static inline struct vc4_cl_out *
88cl_start(struct vc4_cl *cl)
89{
90        return cl->next;
91}
92
93static inline void
94cl_end(struct vc4_cl *cl, struct vc4_cl_out *next)
95{
96        cl->next = next;
97        assert(cl_offset(cl) <= cl->size);
98}
99
100
101static inline void
102put_unaligned_32(struct vc4_cl_out *ptr, uint32_t val)
103{
104        struct unaligned_32 *p = (void *)ptr;
105        p->x = val;
106}
107
108static inline void
109put_unaligned_16(struct vc4_cl_out *ptr, uint16_t val)
110{
111        struct unaligned_16 *p = (void *)ptr;
112        p->x = val;
113}
114
115static inline void
116cl_u8(struct vc4_cl_out **cl, uint8_t n)
117{
118        *(uint8_t *)(*cl) = n;
119        cl_advance(cl, 1);
120}
121
122static inline void
123cl_u16(struct vc4_cl_out **cl, uint16_t n)
124{
125        put_unaligned_16(*cl, n);
126        cl_advance(cl, 2);
127}
128
129static inline void
130cl_u32(struct vc4_cl_out **cl, uint32_t n)
131{
132        put_unaligned_32(*cl, n);
133        cl_advance(cl, 4);
134}
135
136static inline void
137cl_aligned_u32(struct vc4_cl_out **cl, uint32_t n)
138{
139        *(uint32_t *)(*cl) = n;
140        cl_advance(cl, 4);
141}
142
143static inline void
144cl_ptr(struct vc4_cl_out **cl, void *ptr)
145{
146        *(struct vc4_cl_out **)(*cl) = ptr;
147        cl_advance(cl, sizeof(void *));
148}
149
150static inline void
151cl_f(struct vc4_cl_out **cl, float f)
152{
153        cl_u32(cl, fui(f));
154}
155
156static inline void
157cl_aligned_f(struct vc4_cl_out **cl, float f)
158{
159        cl_aligned_u32(cl, fui(f));
160}
161
162static inline struct vc4_cl_out *
163cl_start_shader_reloc(struct vc4_cl *cl, uint32_t n)
164{
165        assert(cl->reloc_count == 0);
166#ifndef NDEBUG
167        cl->reloc_count = n;
168#endif
169        cl->reloc_next = cl->next;
170
171        /* Reserve the space where hindex will be written. */
172        cl_advance(&cl->next, n * 4);
173
174        return cl->next;
175}
176
177static inline void
178cl_reloc(struct vc4_job *job, struct vc4_cl *cl, struct vc4_cl_out **cl_out,
179         struct vc4_bo *bo, uint32_t offset)
180{
181        *(uint32_t *)cl->reloc_next = vc4_gem_hindex(job, bo);
182        cl_advance(&cl->reloc_next, 4);
183
184#ifndef NDEBUG
185        cl->reloc_count--;
186#endif
187
188        cl_u32(cl_out, offset);
189}
190
191static inline void
192cl_aligned_reloc(struct vc4_job *job, struct vc4_cl *cl,
193                 struct vc4_cl_out **cl_out,
194                 struct vc4_bo *bo, uint32_t offset)
195{
196        *(uint32_t *)cl->reloc_next = vc4_gem_hindex(job, bo);
197        cl_advance(&cl->reloc_next, 4);
198
199#ifndef NDEBUG
200        cl->reloc_count--;
201#endif
202
203        cl_aligned_u32(cl_out, offset);
204}
205
206/**
207 * Reference to a BO with its associated offset, used in the pack process.
208 */
209static inline struct vc4_cl_reloc
210cl_address(struct vc4_bo *bo, uint32_t offset)
211{
212        struct vc4_cl_reloc reloc = {
213                .bo = bo,
214                .offset = offset,
215        };
216        return reloc;
217}
218
219void cl_ensure_space(struct vc4_cl *cl, uint32_t size);
220
221#define cl_packet_header(packet) V3D21_ ## packet ## _header
222#define cl_packet_length(packet) V3D21_ ## packet ## _length
223#define cl_packet_pack(packet)   V3D21_ ## packet ## _pack
224#define cl_packet_struct(packet)   V3D21_ ## packet
225
226static inline void *
227cl_get_emit_space(struct vc4_cl_out **cl, size_t size)
228{
229        void *addr = *cl;
230        cl_advance(cl, size);
231        return addr;
232}
233
234/* Macro for setting up an emit of a CL struct.  A temporary unpacked struct
235 * is created, which you get to set fields in of the form:
236 *
237 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {
238 *     .flags.flat_shade_flags = 1 << 2,
239 * }
240 *
241 * or default values only can be emitted with just:
242 *
243 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags);
244 *
245 * The trick here is that we make a for loop that will execute the body
246 * (either the block or the ';' after the macro invocation) exactly once.
247 * Also, *dst is actually of the wrong type, it's the
248 * uint8_t[cl_packet_length()] in the CL, not a cl_packet_struct(packet).
249 */
250#define cl_emit(cl, packet, name)                                \
251        for (struct cl_packet_struct(packet) name = {            \
252                cl_packet_header(packet)                         \
253        },                                                       \
254        *_loop_terminate = &name;                                \
255        __builtin_expect(_loop_terminate != NULL, 1);            \
256        ({                                                       \
257                struct vc4_cl_out *cl_out = cl_start(cl);        \
258                cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \
259                VG(VALGRIND_CHECK_MEM_IS_DEFINED(cl_out,         \
260                                                 cl_packet_length(packet))); \
261                cl_advance(&cl_out, cl_packet_length(packet));   \
262                cl_end(cl, cl_out);                              \
263                _loop_terminate = NULL;                          \
264        }))                                                      \
265
266#define cl_emit_prepacked(cl, packet) do {                       \
267        memcpy((cl)->next, packet, sizeof(*packet));             \
268        cl_advance(&(cl)->next, sizeof(*packet));                \
269} while (0)
270
271/**
272 * Helper function called by the XML-generated pack functions for filling in
273 * an address field in shader records.
274 *
275 * Relocations for shader recs and texturing involve the packet (or uniforms
276 * stream) being preceded by the handles to the BOs, and the offset within the
277 * BO being in the stream (the output of this function).
278 */
279static inline void
280cl_pack_emit_reloc(struct vc4_cl *cl, const struct vc4_cl_reloc *reloc)
281{
282        *(uint32_t *)cl->reloc_next = vc4_gem_hindex(cl->job, reloc->bo);
283        cl_advance(&cl->reloc_next, 4);
284
285#ifndef NDEBUG
286        cl->reloc_count--;
287#endif
288}
289
290#endif /* VC4_CL_H */
291