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_types.h"
40 #include "pvr_winsys.h"
41 #include "util/list.h"
42 #include "util/macros.h"
43
44 #define __pvr_address_type pvr_dev_addr_t
45 #define __pvr_get_address(pvr_dev_addr) (pvr_dev_addr).addr
46 /* clang-format off */
47 #define __pvr_make_address(addr_u64) PVR_DEV_ADDR(addr_u64)
48 /* clang-format on */
49
50 #include "csbgen/rogue_hwdefs.h"
51
52 struct pvr_device;
53
54 enum pvr_cmd_stream_type {
55 PVR_CMD_STREAM_TYPE_INVALID = 0, /* explicitly treat 0 as invalid */
56 PVR_CMD_STREAM_TYPE_GRAPHICS,
57 PVR_CMD_STREAM_TYPE_COMPUTE,
58 };
59
60 struct pvr_csb {
61 struct pvr_device *device;
62
63 /* Pointer to current csb buffer object */
64 struct pvr_bo *pvr_bo;
65
66 /* pointers to current bo memory */
67 void *start;
68 void *end;
69 void *next;
70
71 /* List of csb buffer objects */
72 struct list_head pvr_bo_list;
73
74 enum pvr_cmd_stream_type stream_type;
75
76 /* Current error status of the command buffer. Used to track inconsistent
77 * or incomplete command buffer states that are the consequence of run-time
78 * errors such as out of memory scenarios. We want to track this in the
79 * csb because the command buffer object is not visible to some parts
80 * of the driver.
81 */
82 VkResult status;
83 };
84
85 /**
86 * \brief Gets the status of the csb.
87 *
88 * \param[in] csb Control Stream Builder object.
89 * \return VK_SUCCESS if the csb hasn't encountered any error or error code
90 * otherwise.
91 */
pvr_csb_get_status(const struct pvr_csb * csb)92 static inline VkResult pvr_csb_get_status(const struct pvr_csb *csb)
93 {
94 return csb->status;
95 }
96
97 /**
98 * \brief Checks if the control stream is empty or not.
99 *
100 * \param[in] csb Control Stream Builder object.
101 * \return true if csb is empty false otherwise.
102 */
pvr_csb_is_empty(const struct pvr_csb * csb)103 static inline bool pvr_csb_is_empty(const struct pvr_csb *csb)
104 {
105 return list_is_empty(&csb->pvr_bo_list);
106 }
107
108 static inline pvr_dev_addr_t
pvr_csb_get_start_address(const struct pvr_csb * csb)109 pvr_csb_get_start_address(const struct pvr_csb *csb)
110 {
111 if (!pvr_csb_is_empty(csb)) {
112 struct pvr_bo *pvr_bo =
113 list_first_entry(&csb->pvr_bo_list, struct pvr_bo, link);
114
115 return pvr_bo->vma->dev_addr;
116 }
117
118 return PVR_DEV_ADDR_INVALID;
119 }
120
121 void pvr_csb_init(struct pvr_device *device,
122 enum pvr_cmd_stream_type stream_type,
123 struct pvr_csb *csb);
124 void pvr_csb_finish(struct pvr_csb *csb);
125 void *pvr_csb_alloc_dwords(struct pvr_csb *csb, uint32_t num_dwords);
126 VkResult pvr_csb_emit_return(struct pvr_csb *csb);
127 VkResult pvr_csb_emit_terminate(struct pvr_csb *csb);
128
129 #define PVRX(x) ROGUE_##x
130 #define pvr_cmd_length(x) PVRX(x##_length)
131 #define pvr_cmd_header(x) PVRX(x##_header)
132
133 /* This helper is internal to this header. It should only be used as part of
134 * the pvr_csb_*() macros below.
135 */
136 #define __pvr_cmd_pack(x) PVRX(x##_pack)
137
138 /**
139 * \brief Packs a command/state into one or more dwords and stores them in the
140 * memory pointed to by _dst.
141 *
142 * \param[out] _dst Pointer to store the packed command/state.
143 * \param[in] cmd Command/state type.
144 * \param[in,out] name Name to give to the command/state structure variable,
145 * which contains the information to be packed and emitted.
146 * This can be used by the caller to modify the command or
147 * state information before it's packed.
148 */
149 #define pvr_csb_pack(_dst, cmd, name) \
150 for (struct PVRX(cmd) name = { pvr_cmd_header(cmd) }, \
151 *_loop_terminate = &name; \
152 __builtin_expect(_loop_terminate != NULL, 1); \
153 ({ \
154 STATIC_ASSERT(sizeof(*(_dst)) == pvr_cmd_length(cmd) * 4); \
155 __pvr_cmd_pack(cmd)((_dst), &name); \
156 _loop_terminate = NULL; \
157 }))
158
159 /**
160 * \brief Merges dwords0 and dwords1 arrays and stores the result into the
161 * control stream pointed by the csb object.
162 *
163 * \param[in] csb Control Stream Builder object.
164 * \param[in] dwords0 Dwords0 array.
165 * \param[in] dwords1 Dwords1 array.
166 */
167 #define pvr_csb_emit_merge(csb, dwords0, dwords1) \
168 do { \
169 uint32_t *dw; \
170 STATIC_ASSERT(ARRAY_SIZE(dwords0) == ARRAY_SIZE(dwords1)); \
171 dw = pvr_csb_alloc_dwords(csb, ARRAY_SIZE(dwords0)); \
172 if (!dw) \
173 break; \
174 for (uint32_t i = 0; i < ARRAY_SIZE(dwords0); i++) \
175 dw[i] = (dwords0)[i] | (dwords1)[i]; \
176 } while (0)
177
178 /**
179 * \brief Packs a command/state into one or more dwords and stores them into
180 * the control stream pointed by the csb object.
181 *
182 * \param[in] csb Control Stream Builder object.
183 * \param[in] cmd Command/state type.
184 * \param[in,out] name Name to give to the command/state structure variable,
185 * which contains the information to be packed. This can be
186 * used by the caller to modify the command or state
187 * information before it's packed.
188 */
189 #define pvr_csb_emit(csb, cmd, name) \
190 for (struct PVRX(cmd) \
191 name = { pvr_cmd_header(cmd) }, \
192 *_dst = pvr_csb_alloc_dwords(csb, pvr_cmd_length(cmd)); \
193 __builtin_expect(_dst != NULL, 1); \
194 ({ \
195 __pvr_cmd_pack(cmd)(_dst, &name); \
196 _dst = NULL; \
197 }))
198
199 /**
200 * \brief Stores dword into the control stream pointed by the csb object.
201 *
202 * \param[in] csb Control Stream Builder object.
203 * \param[in] dword Dword to store into control stream.
204 */
205 #define pvr_csb_emit_dword(csb, dword) \
206 do { \
207 uint32_t *dw; \
208 STATIC_ASSERT(sizeof(dword) == sizeof(uint32_t)); \
209 dw = pvr_csb_alloc_dwords(csb, 1U); \
210 if (!dw) \
211 break; \
212 *dw = dword; \
213 } while (0)
214
215 #endif /* PVR_CSB_H */
216