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