1 /*
2 * Copyright © 2019 Raspberry Pi Ltd
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 V3DV_CL_H
25 #define V3DV_CL_H
26
27 #include "broadcom/cle/v3d_packet_helpers.h"
28
29 #include "util/list.h"
30 #include "util/macros.h"
31
32 struct v3dv_bo;
33 struct v3dv_job;
34 struct v3dv_cl;
35
36 void v3dv_job_add_bo(struct v3dv_job *job, struct v3dv_bo *bo);
37
38 /**
39 * Undefined structure, used for typechecking that you're passing the pointers
40 * to these functions correctly.
41 */
42 struct v3dv_cl_out;
43
44 /** A reference to a BO used in the CL packing functions */
45 struct v3dv_cl_reloc {
46 struct v3dv_bo *bo;
47 uint32_t offset;
48 };
49
50 static inline void
pack_emit_reloc(void * cl,const void * reloc)51 pack_emit_reloc(void *cl, const void *reloc) {}
52
53 #define __gen_user_data struct v3dv_cl
54 #define __gen_address_type struct v3dv_cl_reloc
55 #define __gen_address_offset(reloc) (((reloc)->bo ? (reloc)->bo->offset : 0) + \
56 (reloc)->offset)
57 #define __gen_emit_reloc cl_pack_emit_reloc
58 #define __gen_unpack_address(cl, s, e) __unpack_address(cl, s, e)
59
60 struct v3dv_cl {
61 void *base;
62 struct v3dv_job *job;
63 struct v3dv_cl_out *next;
64 struct v3dv_bo *bo;
65 uint32_t size;
66 struct list_head bo_list;
67 };
68
69 static inline struct v3dv_cl_reloc
__unpack_address(const uint8_t * cl,uint32_t s,uint32_t e)70 __unpack_address(const uint8_t *cl, uint32_t s, uint32_t e)
71 {
72 struct v3dv_cl_reloc reloc =
73 { NULL, __gen_unpack_uint(cl, s, e) << (31 - (e - s)) };
74 return reloc;
75 }
76
77 static inline uint32_t
v3dv_cl_offset(struct v3dv_cl * cl)78 v3dv_cl_offset(struct v3dv_cl *cl)
79 {
80 return (char *)cl->next - (char *)cl->base;
81 }
82
83 static inline struct v3dv_cl_reloc
v3dv_cl_address(struct v3dv_bo * bo,uint32_t offset)84 v3dv_cl_address(struct v3dv_bo *bo, uint32_t offset)
85 {
86 struct v3dv_cl_reloc reloc = {
87 .bo = bo,
88 .offset = offset,
89 };
90 return reloc;
91 }
92
93 static inline struct v3dv_cl_reloc
v3dv_cl_get_address(struct v3dv_cl * cl)94 v3dv_cl_get_address(struct v3dv_cl *cl)
95 {
96 return (struct v3dv_cl_reloc){ .bo = cl->bo, .offset = v3dv_cl_offset(cl) };
97 }
98
99 void v3dv_cl_init(struct v3dv_job *job, struct v3dv_cl *cl);
100 void v3dv_cl_destroy(struct v3dv_cl *cl);
101
102 static inline struct v3dv_cl_out *
cl_start(struct v3dv_cl * cl)103 cl_start(struct v3dv_cl *cl)
104 {
105 return cl->next;
106 }
107
108 static inline void
cl_end(struct v3dv_cl * cl,struct v3dv_cl_out * next)109 cl_end(struct v3dv_cl *cl, struct v3dv_cl_out *next)
110 {
111 cl->next = next;
112 assert(v3dv_cl_offset(cl) <= cl->size);
113 }
114
115 static inline void
cl_advance(struct v3dv_cl_out ** cl,uint32_t n)116 cl_advance(struct v3dv_cl_out **cl, uint32_t n)
117 {
118 (*cl) = (struct v3dv_cl_out *)((char *)(*cl) + n);
119 }
120
121 static inline void
cl_advance_and_end(struct v3dv_cl * cl,uint32_t n)122 cl_advance_and_end(struct v3dv_cl *cl, uint32_t n)
123 {
124 cl->next = (struct v3dv_cl_out *)((char *)(cl->next) + n);
125 assert(v3dv_cl_offset(cl) <= cl->size);
126 }
127
128 static inline void
cl_aligned_u32(struct v3dv_cl_out ** cl,uint32_t n)129 cl_aligned_u32(struct v3dv_cl_out **cl, uint32_t n)
130 {
131 *(uint32_t *)(*cl) = n;
132 cl_advance(cl, 4);
133 }
134
135 static inline void
cl_aligned_f(struct v3dv_cl_out ** cl,float f)136 cl_aligned_f(struct v3dv_cl_out **cl, float f)
137 {
138 cl_aligned_u32(cl, fui(f));
139 }
140
141 static inline void
cl_aligned_reloc(struct v3dv_cl * cl,struct v3dv_cl_out ** cl_out,struct v3dv_bo * bo,uint32_t offset)142 cl_aligned_reloc(struct v3dv_cl *cl,
143 struct v3dv_cl_out **cl_out,
144 struct v3dv_bo *bo,
145 uint32_t offset)
146 {
147 cl_aligned_u32(cl_out, bo->offset + offset);
148 v3dv_job_add_bo(cl->job, bo);
149 }
150
151 uint32_t v3dv_cl_ensure_space(struct v3dv_cl *cl, uint32_t space, uint32_t alignment);
152 void v3dv_cl_ensure_space_with_branch(struct v3dv_cl *cl, uint32_t space);
153
154 #define cl_packet_header(packet) V3DX(packet ## _header)
155 #define cl_packet_length(packet) V3DX(packet ## _length)
156 #define cl_aligned_packet_length(packet, alignment) ALIGN_POT(cl_packet_length(packet), alignment)
157 #define cl_packet_pack(packet) V3DX(packet ## _pack)
158 #define cl_packet_struct(packet) V3DX(packet)
159
160 /* Macro for setting up an emit of a CL struct. A temporary unpacked struct
161 * is created, which you get to set fields in of the form:
162 *
163 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags) {
164 * .flags.flat_shade_flags = 1 << 2,
165 * }
166 *
167 * or default values only can be emitted with just:
168 *
169 * cl_emit(bcl, FLAT_SHADE_FLAGS, flags);
170 *
171 * The trick here is that we make a for loop that will execute the body
172 * (either the block or the ';' after the macro invocation) exactly once.
173 */
174 #define cl_emit(cl, packet, name) \
175 for (struct cl_packet_struct(packet) name = { \
176 cl_packet_header(packet) \
177 }, \
178 *_loop_terminate = &name; \
179 __builtin_expect(_loop_terminate != NULL, 1); \
180 ({ \
181 struct v3dv_cl_out *cl_out = cl_start(cl); \
182 cl_packet_pack(packet)(cl, (uint8_t *)cl_out, &name); \
183 cl_advance_and_end(cl, cl_packet_length(packet)); \
184 _loop_terminate = NULL; \
185 })) \
186
187 #define cl_emit_with_prepacked(cl, packet, prepacked, name) \
188 for (struct cl_packet_struct(packet) name = { \
189 cl_packet_header(packet) \
190 }, \
191 *_loop_terminate = &name; \
192 __builtin_expect(_loop_terminate != NULL, 1); \
193 ({ \
194 struct v3dv_cl_out *cl_out = cl_start(cl); \
195 uint8_t packed[cl_packet_length(packet)]; \
196 cl_packet_pack(packet)(cl, packed, &name); \
197 for (int _i = 0; _i < cl_packet_length(packet); _i++) \
198 ((uint8_t *)cl_out)[_i] = packed[_i] | (prepacked)[_i]; \
199 cl_advance_and_end(cl, cl_packet_length(packet)); \
200 _loop_terminate = NULL; \
201 })) \
202
203 /**
204 * Helper function called by the XML-generated pack functions for filling in
205 * an address field in shader records.
206 *
207 * Since we have a private address space as of V3D, our BOs can have lifelong
208 * offsets, and all the kernel needs to know is which BOs need to be paged in
209 * for this exec.
210 */
211 static inline void
cl_pack_emit_reloc(struct v3dv_cl * cl,const struct v3dv_cl_reloc * reloc)212 cl_pack_emit_reloc(struct v3dv_cl *cl, const struct v3dv_cl_reloc *reloc)
213 {
214 if (reloc->bo)
215 v3dv_job_add_bo(cl->job, reloc->bo);
216 }
217
218 #define cl_emit_prepacked_sized(cl, packet, size) do { \
219 memcpy((cl)->next, packet, size); \
220 cl_advance(&(cl)->next, size); \
221 } while (0)
222
223 #define cl_emit_prepacked(cl, packet) \
224 cl_emit_prepacked_sized(cl, packet, sizeof(*(packet)))
225
226 #define v3dvx_pack(packed, packet, name) \
227 for (struct cl_packet_struct(packet) name = { \
228 cl_packet_header(packet) \
229 }, \
230 *_loop_terminate = &name; \
231 __builtin_expect(_loop_terminate != NULL, 1); \
232 ({ \
233 cl_packet_pack(packet)(NULL, (uint8_t *)packed, &name); \
234 VG(VALGRIND_CHECK_MEM_IS_DEFINED((uint8_t *)packed, \
235 cl_packet_length(packet))); \
236 _loop_terminate = NULL; \
237 })) \
238
239 #endif /* V3DV_CL_H */
240