Debian Codegraph @

src/imagination/vulkan/pvr_csb.h

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * based in part on anv driver which is:
5 * Copyright © 2015 Intel Corporation
6 *
7 * based in part on v3dv_cl.h which is:
8 * Copyright © 2019 Raspberry Pi
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the next
18 * paragraph) shall be included in all copies or substantial portions of the
19 * Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #ifndef PVR_CSB_H
31 #define PVR_CSB_H
32
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <vulkan/vulkan.h>
37
38 #include "pvr_bo.h"
39 #include "pvr_macros.h"
40 #include "pvr_types.h"
41 #include "pvr_winsys.h"
42
43 #include "util/list.h"
44 #include "util/macros.h"
45 #include "util/u_dynarray.h"
46
47 #define __pvr_address_type pvr_dev_addr_t
48 #define __pvr_get_address(pvr_dev_addr) (pvr_dev_addr).addr
49 /* clang-format off */
50 #define __pvr_make_address(addr_u64) PVR_DEV_ADDR(addr_u64)
51 /* clang-format on */
52
53 #if defined(PVR_BUILD_ARCH_ROGUE)
54 # include "csbgen/rogue/cdm.h"
55 # include "csbgen/rogue/cr.h"
56 # include "csbgen/rogue/ipf.h"
57 # include "csbgen/rogue/kmd_stream.h"
58 # include "csbgen/rogue/lls.h"
59 # include "csbgen/rogue/pbestate.h"
60 # include "csbgen/rogue/pds.h"
61 # include "csbgen/rogue/ppp.h"
62 # include "csbgen/rogue/texstate.h"
63 # include "csbgen/rogue/vdm.h"
64 #endif
65
66 /**
67 * \brief Size of the individual csb buffer object.
68 */
69 #define PVR_CMD_BUFFER_CSB_BO_SIZE 4096
70
71 struct pvr_device;
72
73 enum pvr_cmd_stream_type {
74 PVR_CMD_STREAM_TYPE_INVALID = 0, /* explicitly treat 0 as invalid */
75 PVR_CMD_STREAM_TYPE_GRAPHICS,
76 PVR_CMD_STREAM_TYPE_GRAPHICS_DEFERRED,
77 PVR_CMD_STREAM_TYPE_COMPUTE,
78 };
79
80 struct pvr_csb {
81 struct pvr_device *device;
82
83 /* Pointer to current csb buffer object */
84 struct pvr_bo *pvr_bo;
85
86 /* pointers to current bo memory */
87 void *start;
88 void *end;
89 void *next;
90
91 /* When extending the control stream we can't break state updates across bos.
92 * This indicates where the current state update starts, so that it can be
93 * be relocated into the new bo without breaking the update.
94 */
95 void *relocation_mark;
96 #if MESA_DEBUG
97 /* Used to track the state of the `relocation_mark` and to catch cases where
98 * the driver might have emitted to the cs without using the
99 * `relocation_mark`. Doing so is mostly harmless but will waste memory in
100 * case the cs is extended while an untracked state update is emitted, as
101 * we'll have to relocate the cs contents from the last tracked state update
102 * instead of just the one currently being emitted.
103 */
104 enum pvr_csb_relocation_mark_status {
105 PVR_CSB_RELOCATION_MARK_UNINITIALIZED,
106 PVR_CSB_RELOCATION_MARK_SET,
107 PVR_CSB_RELOCATION_MARK_SET_AND_CONSUMED,
108 PVR_CSB_RELOCATION_MARK_CLEARED,
109 } relocation_mark_status;
110 #endif
111
112 /* List of csb buffer objects */
113 struct list_head pvr_bo_list;
114
115 struct util_dynarray deferred_cs_mem;
116
117 enum pvr_cmd_stream_type stream_type;
118
119 /* Current error status of the command buffer. Used to track inconsistent
120 * or incomplete command buffer states that are the consequence of run-time
121 * errors such as out of memory scenarios. We want to track this in the
122 * csb because the command buffer object is not visible to some parts
123 * of the driver.
124 */
125 VkResult status;
126 };
127
128 /**
129 * \brief Gets the status of the csb.
130 *
131 * \param[in] csb Control Stream Builder object.
132 * \return VK_SUCCESS if the csb hasn't encountered any error or error code
133 * otherwise.
134 */
135 static inline VkResult pvr_csb_get_status(const struct pvr_csb *csb)
136 {
137 return csb->status;
138 }
139
140 /**
141 * \brief Checks if the control stream is empty or not.
142 *
143 * \param[in] csb Control Stream Builder object.
144 * \return true if csb is empty false otherwise.
145 */
146 static inline bool pvr_csb_is_empty(const struct pvr_csb *csb)
147 {
148 return list_is_empty(&csb->pvr_bo_list);
149 }
150
151 static inline pvr_dev_addr_t
152 pvr_csb_get_start_address(const struct pvr_csb *csb)
153 {
154 if (!pvr_csb_is_empty(csb)) {
155 struct pvr_bo *pvr_bo =
156 list_first_entry(&csb->pvr_bo_list, struct pvr_bo, link);
157
158 return pvr_bo->vma->dev_addr;
159 }
160
161 return PVR_DEV_ADDR_INVALID;
162 }
163
164 /** \defgroup CSB relocation marking.
165 * Functions and macros related to relocation marking for control stream words.
166 *
167 * When there is no more space left in the current bo, csb needs has to extend
168 * the control stream by allocating a new bo and emitting a link to it. State
169 * updates have to be contiguous so cannot be broken by a link. Thus csb copies
170 * the current, in construction, state update into the new bo and emits a link
171 * in its place in the old bo. To do so however, it needs a hint from the driver
172 * to determine where the current state update started from, so a relocation
173 * mark is used.
174 *
175 * List of words demarking the beginning of state updates (i.e. state update
176 * headers):
177 * - ROGUE_VDMCTRL_PPP_STATE0
178 * - ROGUE_VDMCTRL_PDS_STATE0
179 * - ROGUE_VDMCTRL_VDM_STATE0
180 * - ROGUE_VDMCTRL_INDEX_LIST0
181 * - ROGUE_VDMCTRL_STREAM_LINK0
182 * - ROGUE_VDMCTRL_STREAM_RETURN
183 * - ROGUE_VDMCTRL_STREAM_TERMINATE
184 *
185 * - ROGUE_CDMCTRL_KERNEL0
186 * - ROGUE_CDMCTRL_STREAM_LINK0
187 * - ROGUE_CDMCTRL_STREAM_TERMINATE
188 *
189 * The driver should set the relocation mark whenever a new state update is
190 * started. And clear it when the state update is fully formed.
191 *
192 * PVR_CSB_RELOCATION_MARK state machine:
193 *
194 * UNINITIALIZED
195 * ↓
196 * ┌─── → SET ─────────┐
197 * │ ↓ │
198 * │ SET_AND_CONSUMED │
199 * │ ↓ │
200 * │ CLEARED ← ─────┘
201 * └───────┘
202 *
203 * @{
204 */
205 /* TODO: Add in the IPF transfer control stream state updates to the list once
206 * csb gets used for it
207 */
208
209 /**
210 * \brief Set the relocation mark.
211 *
212 * Indicates to csb that on cs extension it should relocate all words, starting
213 * from now, into the new bo.
214 */
215 static inline void pvr_csb_set_relocation_mark(struct pvr_csb *csb)
216 {
217 #if MESA_DEBUG
218 assert(csb->relocation_mark_status ==
219 PVR_CSB_RELOCATION_MARK_UNINITIALIZED ||
220 csb->relocation_mark_status == PVR_CSB_RELOCATION_MARK_CLEARED);
221
222 csb->relocation_mark_status = PVR_CSB_RELOCATION_MARK_SET;
223 #endif
224
225 csb->relocation_mark = csb->next;
226 }
227
228 /**
229 * \brief Clear the relocation mark.
230 *
231 * Indicate to csb that the state update is fully formed so it doesn't need to
232 * relocate it in case of cs extension.
233 */
234 static inline void pvr_csb_clear_relocation_mark(UNUSED struct pvr_csb *csb)
235 {
236 #if MESA_DEBUG
237 assert(csb->relocation_mark_status == PVR_CSB_RELOCATION_MARK_SET ||
238 csb->relocation_mark_status ==
239 PVR_CSB_RELOCATION_MARK_SET_AND_CONSUMED);
240
241 csb->relocation_mark_status = PVR_CSB_RELOCATION_MARK_CLEARED;
242 #endif
243 }
244
245 /** @} */
246 /* End of \defgroup CSB relocation marking. */
247
248 void pvr_csb_init(struct pvr_device *device,
249 enum pvr_cmd_stream_type stream_type,
250 struct pvr_csb *csb);
251 void pvr_csb_finish(struct pvr_csb *csb);
252 VkResult pvr_csb_bake(struct pvr_csb *csb, struct list_head *bo_list_out);
253
254 #ifdef PVR_PER_ARCH
255
256 void *PVR_PER_ARCH(csb_alloc_dwords)(struct pvr_csb *csb, uint32_t num_dwords);
257
258 # define pvr_arch_csb_alloc_dwords PVR_PER_ARCH(csb_alloc_dwords)
259
260 VkResult PVR_PER_ARCH(csb_copy)(struct pvr_csb *csb_dst,
261 struct pvr_csb *csb_src);
262
263 # define pvr_arch_csb_copy PVR_PER_ARCH(csb_copy)
264
265 void PVR_PER_ARCH(csb_emit_link)(struct pvr_csb *csb,
266 pvr_dev_addr_t addr,
267 bool ret);
268
269 # define pvr_arch_csb_emit_link PVR_PER_ARCH(csb_emit_link)
270
271 VkResult PVR_PER_ARCH(csb_emit_return)(struct pvr_csb *csb);
272
273 # define pvr_arch_csb_emit_return PVR_PER_ARCH(csb_emit_return)
274
275 VkResult PVR_PER_ARCH(csb_emit_terminate)(struct pvr_csb *csb);
276
277 # define pvr_arch_csb_emit_terminate PVR_PER_ARCH(csb_emit_terminate)
278
279 #endif /* PVR_PER_ARCH */
280
281 #ifdef PVR_BUILD_ARCH_ROGUE
282 void pvr_csb_dump(const struct pvr_csb *csb,
283 uint32_t frame_num,
284 uint32_t job_num);
285 #endif
286
287 #define pvr_cmd_length(x) ROGUE_##x##_length
288 #define pvr_cmd_header(x) ROGUE_##x##_header
289 #define pvr_cmd_pack(x) ROGUE_##x##_pack
290 #define pvr_cmd_unpack(x) ROGUE_##x##_unpack
291 #define pvr_cmd_enum_to_str(x) ROGUE_##x##_to_str
292
293 /**
294 * \brief Packs a command/state into one or more dwords and stores them into
295 * the control stream pointed by the csb object.
296 *
297 * \param[in] csb Control Stream Builder object.
298 * \param[in] cmd Command/state type.
299 * \param[in,out] name Name to give to the command/state structure variable,
300 * which contains the information to be packed. This can be
301 * used by the caller to modify the command or state
302 * information before it's packed.
303 */
304 #define pvr_csb_emit(csb, cmd, name) \
305 for (struct ROGUE_##cmd \
306 name = { pvr_cmd_header(cmd) }, \
307 *_dst = pvr_arch_csb_alloc_dwords(csb, pvr_cmd_length(cmd)); \
308 __builtin_expect(_dst != NULL, 1); \
309 ({ \
310 pvr_cmd_pack(cmd)(_dst, &name); \
311 _dst = NULL; \
312 }))
313
314 /**
315 * \name Raw command/state buffer helpers.
316 * These provide functionality to read or write control/state words from/to a
317 * raw buffer, accessed through a pointer, with some extra checks.
318 *
319 * The raw buffer doesn't have to be related to a control stream builder object
320 * so these can be used with any cpu accessible buffer.
321 */
322 /**@{*/
323
324 /**
325 * \brief Packs a command/state into one or more dwords and stores them in the
326 * memory pointed to by _dst.
327 *
328 * \param[out] _dst Pointer to store the packed command/state.
329 * \param[in] cmd Command/state type.
330 * \param[in,out] name Name to give to the command/state structure variable,
331 * which contains the information to be packed and emitted.
332 * This can be used by the caller to modify the command or
333 * state information before it's packed.
334 */
335 #define pvr_csb_pack(_dst, cmd, name) \
336 for (struct ROGUE_##cmd name = { pvr_cmd_header(cmd) }, \
337 *_loop_terminate = &name; \
338 __builtin_expect(_loop_terminate != NULL, 1); \
339 ({ \
340 STATIC_ASSERT(sizeof(*(_dst)) == \
341 PVR_DW_TO_BYTES(pvr_cmd_length(cmd))); \
342 pvr_cmd_pack(cmd)((_dst), &name); \
343 _loop_terminate = NULL; \
344 }))
345
346 /**
347 * \brief Unpacks one or more dwords into a command/state struct.
348 *
349 * Unlike pvr_csb_pack, this returns the stack-allocated struct directly
350 * since it is not needed afterwards.
351 *
352 * \param[in] _src Pointer to read the packed command/state from.
353 * \param[in] cmd Command/state type.
354 */
355 #define pvr_csb_unpack(_src, cmd) \
356 ({ \
357 struct ROGUE_##cmd _name; \
358 STATIC_ASSERT(sizeof(*(_src)) == PVR_DW_TO_BYTES(pvr_cmd_length(cmd))); \
359 pvr_cmd_unpack(cmd)((_src), &_name); \
360 _name; \
361 })
362
363 /**
364 * \brief Writes a command/state word value into a raw buffer and advance.
365 *
366 * The buffer pointer is incremented appropriately based on the control stream
367 * word length.
368 *
369 * \param[in,out] dst Raw buffer pointer for writing.
370 * \param[in] cmd Command/state type.
371 * \param[in] val Pre-packed value to write.
372 */
373 #define pvr_csb_write_value(dst, cmd, val) \
374 do { \
375 static_assert(sizeof(*(dst)) == PVR_DW_TO_BYTES(pvr_cmd_length(cmd)), \
376 "Size mismatch"); \
377 static_assert(sizeof(*(dst)) == sizeof(val), "Size mismatch"); \
378 *(dst) = (val); \
379 (dst)++; \
380 } while (0)
381
382 /**
383 * \brief Packs a command/state word struct and writes the value into a raw
384 * buffer and advance.
385 *
386 * The buffer pointer is incremented appropriately based on the control stream
387 * word length.
388 *
389 * \param[in,out] dst Raw buffer pointer for writing.
390 * \param[in] cmd Command/state type.
391 * \param[in] val Command/state struct to pack and write.
392 */
393 #define pvr_csb_write_struct(dst, cmd, val) \
394 do { \
395 static_assert(sizeof(*(dst)) == PVR_DW_TO_BYTES(pvr_cmd_length(cmd)), \
396 "Size mismatch"); \
397 pvr_cmd_pack(cmd)((dst), (val)); \
398 (dst)++; \
399 } while (0)
400
401 /**@}*/
402 /* End of \name Raw command/state buffer helpers. */
403
404 #endif /* PVR_CSB_H */