Debian Codegraph @

src/imagination/vulkan/pvr_query.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * 'pvr_write_query_to_buffer()' and 'pvr_wait_for_available()' based on anv:
24 * Copyright © 2015 Intel Corporation
25 */
26
27 #include "pvr_query.h"
28
29 #include <assert.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <vulkan/vulkan.h>
34
35 #include "pvr_bo.h"
36 #include "pvr_csb.h"
37 #include "pvr_device.h"
38 #include "pvr_device_info.h"
39 #include "pvr_entrypoints.h"
40 #include "pvr_hw_pass.h"
41 #include "pvr_macros.h"
42 #include "pvr_pass.h"
43 #include "pvr_physical_device.h"
44 #include "util/macros.h"
45 #include "util/os_time.h"
46 #include "vk_log.h"
47 #include "vk_object.h"
48
49 VkResult pvr_CreateQueryPool(VkDevice _device,
50 const VkQueryPoolCreateInfo *pCreateInfo,
51 const VkAllocationCallbacks *pAllocator,
52 VkQueryPool *pQueryPool)
53 {
54 VK_FROM_HANDLE(pvr_device, device, _device);
55 const uint32_t core_count = device->pdevice->dev_runtime_info.core_count;
56 const uint32_t query_size = pCreateInfo->queryCount * sizeof(uint32_t);
57 struct pvr_query_pool *pool;
58 uint64_t alloc_size;
59 VkResult result;
60
61 /* Vulkan 1.0 supports only occlusion, timestamp, and pipeline statistics
62 * query.
63 * We don't currently support timestamp queries.
64 * VkQueueFamilyProperties->timestampValidBits = 0.
65 * We don't currently support pipeline statistics queries.
66 * VkPhysicalDeviceFeatures->pipelineStatisticsQuery = false.
67 */
68 assert(!device->vk.enabled_features.pipelineStatisticsQuery);
69 assert(pCreateInfo->queryType == VK_QUERY_TYPE_OCCLUSION);
70
71 pool = vk_object_alloc(&device->vk,
72 pAllocator,
73 sizeof(*pool),
74 VK_OBJECT_TYPE_QUERY_POOL);
75 if (!pool)
76 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
77
78 pool->result_stride = ALIGN_POT(query_size, ROGUE_OQUERY_ALIGN);
79
80 pool->query_count = pCreateInfo->queryCount;
81
82 /* Each Phantom writes to a separate offset within the vis test heap so
83 * allocate space for the total number of Phantoms.
84 */
85 alloc_size = (uint64_t)pool->result_stride * core_count;
86
87 result = pvr_bo_suballoc(&device->suballoc_vis_test,
88 alloc_size,
89 ROGUE_OQUERY_ALIGN,
90 false,
91 &pool->result_buffer);
92 if (result != VK_SUCCESS)
93 goto err_free_pool;
94
95 result = pvr_bo_suballoc(&device->suballoc_general,
96 query_size,
97 sizeof(uint32_t),
98 false,
99 &pool->availability_buffer);
100 if (result != VK_SUCCESS)
101 goto err_free_result_buffer;
102
103 *pQueryPool = pvr_query_pool_to_handle(pool);
104
105 return VK_SUCCESS;
106
107 err_free_result_buffer:
108 pvr_bo_suballoc_free(pool->result_buffer);
109
110 err_free_pool:
111 vk_object_free(&device->vk, pAllocator, pool);
112
113 return result;
114 }
115
116 void pvr_DestroyQueryPool(VkDevice _device,
117 VkQueryPool queryPool,
118 const VkAllocationCallbacks *pAllocator)
119 {
120 VK_FROM_HANDLE(pvr_query_pool, pool, queryPool);
121 VK_FROM_HANDLE(pvr_device, device, _device);
122
123 if (!pool)
124 return;
125
126 pvr_bo_suballoc_free(pool->availability_buffer);
127 pvr_bo_suballoc_free(pool->result_buffer);
128
129 vk_object_free(&device->vk, pAllocator, pool);
130 }
131
132 /* Note: make sure to make the availability buffer's memory defined in
133 * accordance to how the device is expected to fill it. We don't make it defined
134 * here since that would cover up usage of this function while the underlying
135 * buffer region being accessed wasn't expect to have been written by the
136 * device.
137 */
138 static inline bool pvr_query_is_available(const struct pvr_query_pool *pool,
139 uint32_t query_idx)
140 {
141 volatile uint32_t *available =
142 pvr_bo_suballoc_get_map_addr(pool->availability_buffer);
143 return !!available[query_idx];
144 }
145
146 #define NSEC_PER_SEC UINT64_C(1000000000)
147 #define PVR_WAIT_TIMEOUT UINT64_C(5)
148
149 /* Note: make sure to make the availability buffer's memory defined in
150 * accordance to how the device is expected to fill it. We don't make it defined
151 * here since that would cover up usage of this function while the underlying
152 * buffer region being accessed wasn't expect to have been written by the
153 * device.
154 */
155 /* TODO: Handle device loss scenario properly. */
156 static VkResult pvr_wait_for_available(struct pvr_device *device,
157 const struct pvr_query_pool *pool,
158 uint32_t query_idx)
159 {
160 const uint64_t abs_timeout =
161 os_time_get_absolute_timeout(PVR_WAIT_TIMEOUT * NSEC_PER_SEC);
162
163 /* From the Vulkan 1.0 spec:
164 *
165 * Commands that wait indefinitely for device execution (namely
166 * vkDeviceWaitIdle, vkQueueWaitIdle, vkWaitForFences or
167 * vkAcquireNextImageKHR with a maximum timeout, and
168 * vkGetQueryPoolResults with the VK_QUERY_RESULT_WAIT_BIT bit set in
169 * flags) must return in finite time even in the case of a lost device,
170 * and return either VK_SUCCESS or VK_ERROR_DEVICE_LOST.
171 */
172 while (os_time_get_nano() < abs_timeout) {
173 if (pvr_query_is_available(pool, query_idx) != 0)
174 return VK_SUCCESS;
175 }
176
177 return vk_error(device, VK_ERROR_DEVICE_LOST);
178 }
179
180 #undef NSEC_PER_SEC
181 #undef PVR_WAIT_TIMEOUT
182
183 static inline void pvr_write_query_to_buffer(uint8_t *buffer,
184 VkQueryResultFlags flags,
185 uint32_t idx,
186 uint64_t value)
187 {
188 if (flags & VK_QUERY_RESULT_64_BIT) {
189 uint64_t *query_data = (uint64_t *)buffer;
190 query_data[idx] = value;
191 } else {
192 uint32_t *query_data = (uint32_t *)buffer;
193 query_data[idx] = value;
194 }
195 }
196
197 VkResult pvr_GetQueryPoolResults(VkDevice _device,
198 VkQueryPool queryPool,
199 uint32_t firstQuery,
200 uint32_t queryCount,
201 size_t dataSize,
202 void *pData,
203 VkDeviceSize stride,
204 VkQueryResultFlags flags)
205 {
206 VK_FROM_HANDLE(pvr_query_pool, pool, queryPool);
207 VK_FROM_HANDLE(pvr_device, device, _device);
208 VG(volatile uint32_t *available =
209 pvr_bo_suballoc_get_map_addr(pool->availability_buffer));
210 volatile uint32_t *query_results =
211 pvr_bo_suballoc_get_map_addr(pool->result_buffer);
212 const uint32_t core_count = device->pdevice->dev_runtime_info.core_count;
213 uint8_t *data = (uint8_t *)pData;
214 VkResult result = VK_SUCCESS;
215
216 /* TODO: Instead of making the memory defined here for valgrind, to better
217 * catch out of bounds access and other memory errors we should move them
218 * where where the query buffers are changed by the driver or device (e.g.
219 * "vkCmdResetQueryPool()", "vkGetQueryPoolResults()", etc.).
220 */
221
222 VG(VALGRIND_MAKE_MEM_DEFINED(&available[firstQuery],
223 queryCount * sizeof(uint32_t)));
224
225 for (uint32_t i = 0; i < core_count; i++) {
226 VG(VALGRIND_MAKE_MEM_DEFINED(
227 &query_results[firstQuery + i * pool->result_stride],
228 queryCount * sizeof(uint32_t)));
229 }
230
231 for (uint32_t i = 0; i < queryCount; i++) {
232 bool is_available = pvr_query_is_available(pool, firstQuery + i);
233 uint64_t count = 0;
234
235 if (flags & VK_QUERY_RESULT_WAIT_BIT && !is_available) {
236 result = pvr_wait_for_available(device, pool, firstQuery + i);
237 if (result != VK_SUCCESS)
238 return result;
239
240 is_available = true;
241 }
242
243 for (uint32_t j = 0; j < core_count; j++)
244 count += query_results[pool->result_stride * j + firstQuery + i];
245
246 if (is_available || (flags & VK_QUERY_RESULT_PARTIAL_BIT))
247 pvr_write_query_to_buffer(data, flags, 0, count);
248 else
249 result = VK_NOT_READY;
250
251 if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT)
252 pvr_write_query_to_buffer(data, flags, 1, is_available);
253
254 data += stride;
255 }
256
257 VG(VALGRIND_MAKE_MEM_UNDEFINED(&available[firstQuery],
258 queryCount * sizeof(uint32_t)));
259
260 for (uint32_t i = 0; i < core_count; i++) {
261 VG(VALGRIND_MAKE_MEM_UNDEFINED(
262 &query_results[firstQuery + i * pool->result_stride],
263 queryCount * sizeof(uint32_t)));
264 }
265
266 return result;
267 }
268
269 void pvr_ResetQueryPool(VkDevice _device,
270 VkQueryPool queryPool,
271 uint32_t firstQuery,
272 uint32_t queryCount)
273 {
274 VK_FROM_HANDLE(pvr_query_pool, pool, queryPool);
275 uint32_t *availability =
276 pvr_bo_suballoc_get_map_addr(pool->availability_buffer);
277
278 memset(availability + firstQuery, 0, sizeof(uint32_t) * queryCount);
279 }