| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* Copyright © 2025 Arm Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "util/macros.h" |
| 8 |
#include "vk_log.h" |
| 9 |
|
| 10 |
#include "pan_props.h" |
| 11 |
#include "panvk_device.h" |
| 12 |
#include "panvk_entrypoints.h" |
| 13 |
#include "panvk_query_pool.h" |
| 14 |
|
| 15 |
#include "panvk_cmd_ts.h" |
| 16 |
|
| 17 |
#if PAN_ARCH >= 10 |
| 18 |
#include "panvk_queue.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#define PANVK_QUERY_TIMEOUT 2000000000ull |
| 22 |
|
| 23 |
static void |
| 24 |
reset_query_pool(struct panvk_query_pool *pool, uint32_t firstQuery, |
| 25 |
uint32_t queryCount) |
| 26 |
{ |
| 27 |
panvk_priv_mem_write_array(pool->available_mem, |
| 28 |
panvk_query_available_offset(pool, firstQuery), |
| 29 |
struct panvk_query_available_obj, queryCount, |
| 30 |
available) |
| 31 |
memset(available, 0, queryCount * sizeof(*available)); |
| 32 |
} |
| 33 |
|
| 34 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 35 |
panvk_per_arch(CreateQueryPool)(VkDevice _device, |
| 36 |
const VkQueryPoolCreateInfo *pCreateInfo, |
| 37 |
const VkAllocationCallbacks *pAllocator, |
| 38 |
VkQueryPool *pQueryPool) |
| 39 |
{ |
| 40 |
VK_FROM_HANDLE(panvk_device, device, _device); |
| 41 |
|
| 42 |
struct panvk_query_pool *pool; |
| 43 |
|
| 44 |
pool = |
| 45 |
vk_query_pool_create(&device->vk, pCreateInfo, pAllocator, sizeof(*pool)); |
| 46 |
if (!pool) |
| 47 |
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 48 |
|
| 49 |
uint32_t reports_per_query; |
| 50 |
switch (pCreateInfo->queryType) { |
| 51 |
case VK_QUERY_TYPE_OCCLUSION: { |
| 52 |
/* The counter is per core on Bifrost */ |
| 53 |
#if PAN_ARCH < 9 |
| 54 |
const struct panvk_physical_device *phys_dev = |
| 55 |
to_panvk_physical_device(device->vk.physical); |
| 56 |
|
| 57 |
pan_query_core_count(&phys_dev->kmod.dev->props, &reports_per_query); |
| 58 |
#else |
| 59 |
reports_per_query = 1; |
| 60 |
#endif |
| 61 |
break; |
| 62 |
} |
| 63 |
#if PAN_ARCH >= 10 |
| 64 |
case VK_QUERY_TYPE_TIMESTAMP: { |
| 65 |
/* One value per subqueue + 1 value for metadata. */ |
| 66 |
reports_per_query = PANVK_SUBQUEUE_COUNT + 1; |
| 67 |
break; |
| 68 |
} |
| 69 |
case VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT: { |
| 70 |
reports_per_query = 1; |
| 71 |
break; |
| 72 |
} |
| 73 |
#endif |
| 74 |
default: |
| 75 |
UNREACHABLE("Unsupported query type"); |
| 76 |
} |
| 77 |
|
| 78 |
pool->reports_per_query = reports_per_query; |
| 79 |
pool->query_stride = reports_per_query * sizeof(struct panvk_query_report); |
| 80 |
|
| 81 |
assert(pool->vk.query_count > 0); |
| 82 |
|
| 83 |
struct panvk_pool_alloc_info alloc_info = { |
| 84 |
.size = pool->reports_per_query * sizeof(struct panvk_query_report) * |
| 85 |
pool->vk.query_count, |
| 86 |
.alignment = sizeof(struct panvk_query_report), |
| 87 |
}; |
| 88 |
pool->mem = panvk_pool_alloc_mem(&device->mempools.rw, alloc_info); |
| 89 |
if (!pool->mem.bo) { |
| 90 |
vk_query_pool_destroy(&device->vk, pAllocator, &pool->vk); |
| 91 |
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 92 |
} |
| 93 |
|
| 94 |
struct panvk_pool_alloc_info syncobjs_alloc_info = { |
| 95 |
.size = sizeof(struct panvk_query_available_obj) * pool->vk.query_count, |
| 96 |
.alignment = 64, |
| 97 |
}; |
| 98 |
pool->available_mem = |
| 99 |
panvk_pool_alloc_mem(&device->mempools.rw_nc, syncobjs_alloc_info); |
| 100 |
if (!pool->available_mem.bo) { |
| 101 |
panvk_pool_free_mem(&pool->mem); |
| 102 |
vk_query_pool_destroy(&device->vk, pAllocator, &pool->vk); |
| 103 |
return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 104 |
} |
| 105 |
|
| 106 |
if (pCreateInfo->flags & VK_QUERY_POOL_CREATE_RESET_BIT_KHR) |
| 107 |
reset_query_pool(pool, 0, pool->vk.query_count); |
| 108 |
|
| 109 |
*pQueryPool = panvk_query_pool_to_handle(pool); |
| 110 |
|
| 111 |
return VK_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
VKAPI_ATTR void VKAPI_CALL |
| 115 |
panvk_per_arch(DestroyQueryPool)(VkDevice _device, VkQueryPool queryPool, |
| 116 |
const VkAllocationCallbacks *pAllocator) |
| 117 |
{ |
| 118 |
VK_FROM_HANDLE(panvk_device, device, _device); |
| 119 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 120 |
|
| 121 |
if (!pool) |
| 122 |
return; |
| 123 |
|
| 124 |
panvk_pool_free_mem(&pool->mem); |
| 125 |
panvk_pool_free_mem(&pool->available_mem); |
| 126 |
vk_query_pool_destroy(&device->vk, pAllocator, &pool->vk); |
| 127 |
} |
| 128 |
|
| 129 |
VKAPI_ATTR void VKAPI_CALL |
| 130 |
panvk_per_arch(ResetQueryPool)(VkDevice device, VkQueryPool queryPool, |
| 131 |
uint32_t firstQuery, uint32_t queryCount) |
| 132 |
{ |
| 133 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 134 |
reset_query_pool(pool, firstQuery, queryCount); |
| 135 |
} |
| 136 |
|
| 137 |
static bool |
| 138 |
panvk_query_is_available(struct panvk_query_pool *pool, uint32_t query) |
| 139 |
{ |
| 140 |
bool res = false; |
| 141 |
|
| 142 |
panvk_priv_mem_readback(pool->available_mem, |
| 143 |
panvk_query_available_offset(pool, query), |
| 144 |
struct panvk_query_available_obj, available) { |
| 145 |
#if PAN_ARCH >= 10 |
| 146 |
res = p_atomic_read(&available->sync_obj.seqno) != 0; |
| 147 |
#else |
| 148 |
res = p_atomic_read(&available->value) != 0; |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
return res; |
| 153 |
} |
| 154 |
|
| 155 |
static VkResult |
| 156 |
panvk_query_wait_for_available(struct panvk_device *dev, |
| 157 |
struct panvk_query_pool *pool, uint32_t query) |
| 158 |
{ |
| 159 |
int64_t abs_timeout_ns = os_time_get_absolute_timeout(PANVK_QUERY_TIMEOUT); |
| 160 |
|
| 161 |
while (os_time_get_nano() < abs_timeout_ns) { |
| 162 |
if (panvk_query_is_available(pool, query)) |
| 163 |
return VK_SUCCESS; |
| 164 |
|
| 165 |
VkResult status = vk_device_check_status(&dev->vk); |
| 166 |
if (status != VK_SUCCESS) |
| 167 |
return status; |
| 168 |
} |
| 169 |
|
| 170 |
return vk_device_set_lost(&dev->vk, "query timeout"); |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
cpu_write_query_result(void *dst, uint32_t idx, VkQueryResultFlags flags, |
| 175 |
uint64_t result) |
| 176 |
{ |
| 177 |
if (flags & VK_QUERY_RESULT_64_BIT) { |
| 178 |
uint64_t *dst64 = dst; |
| 179 |
dst64[idx] = result; |
| 180 |
} else { |
| 181 |
uint32_t *dst32 = dst; |
| 182 |
dst32[idx] = result; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
cpu_write_occlusion_query_result(void *dst, uint32_t idx, |
| 188 |
VkQueryResultFlags flags, |
| 189 |
const struct panvk_query_report *src, |
| 190 |
unsigned core_count) |
| 191 |
{ |
| 192 |
uint64_t result = 0; |
| 193 |
|
| 194 |
for (uint32_t core_idx = 0; core_idx < core_count; core_idx++) |
| 195 |
result += src[core_idx].value; |
| 196 |
|
| 197 |
cpu_write_query_result(dst, idx, flags, result); |
| 198 |
} |
| 199 |
|
| 200 |
#if PAN_ARCH >= 10 |
| 201 |
static void |
| 202 |
cpu_write_timestamp_query_result(void *dst, uint32_t idx, |
| 203 |
VkQueryResultFlags flags, |
| 204 |
const struct panvk_query_report *src, |
| 205 |
unsigned input_value_count) |
| 206 |
{ |
| 207 |
enum panvk_query_ts_op op = |
| 208 |
panvk_timestamp_info_get_op(src[input_value_count - 1].value); |
| 209 |
uint32_t sq_mask = |
| 210 |
panvk_timestamp_info_get_sq_mask(src[input_value_count - 1].value); |
| 211 |
|
| 212 |
uint64_t result = op == PANVK_QUERY_TS_OP_MIN ? UINT64_MAX : 0; |
| 213 |
|
| 214 |
for (uint32_t idx = 0; idx < input_value_count - 1; ++idx) { |
| 215 |
if ((sq_mask & BITFIELD_BIT(idx)) == 0) |
| 216 |
continue; |
| 217 |
if (src[idx].value == 0) |
| 218 |
continue; |
| 219 |
|
| 220 |
if (op == PANVK_QUERY_TS_OP_MIN) |
| 221 |
result = MIN2(result, src[idx].value); |
| 222 |
else |
| 223 |
result = MAX2(result, src[idx].value); |
| 224 |
} |
| 225 |
|
| 226 |
if (op == PANVK_QUERY_TS_OP_MIN && result == UINT64_MAX) |
| 227 |
result = 0; |
| 228 |
|
| 229 |
cpu_write_query_result(dst, idx, flags, result); |
| 230 |
} |
| 231 |
#endif |
| 232 |
|
| 233 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 234 |
panvk_per_arch(GetQueryPoolResults)(VkDevice _device, VkQueryPool queryPool, |
| 235 |
uint32_t firstQuery, uint32_t queryCount, |
| 236 |
size_t dataSize, void *pData, |
| 237 |
VkDeviceSize stride, |
| 238 |
VkQueryResultFlags flags) |
| 239 |
{ |
| 240 |
VK_FROM_HANDLE(panvk_device, device, _device); |
| 241 |
VK_FROM_HANDLE(panvk_query_pool, pool, queryPool); |
| 242 |
|
| 243 |
if (vk_device_is_lost(&device->vk)) |
| 244 |
return VK_ERROR_DEVICE_LOST; |
| 245 |
|
| 246 |
VkResult status = VK_SUCCESS; |
| 247 |
for (uint32_t i = 0; i < queryCount; i++) { |
| 248 |
const uint32_t query = firstQuery + i; |
| 249 |
|
| 250 |
bool available = panvk_query_is_available(pool, query); |
| 251 |
|
| 252 |
if (!available && (flags & VK_QUERY_RESULT_WAIT_BIT)) { |
| 253 |
status = panvk_query_wait_for_available(device, pool, query); |
| 254 |
if (status != VK_SUCCESS) |
| 255 |
return status; |
| 256 |
|
| 257 |
available = true; |
| 258 |
} |
| 259 |
|
| 260 |
bool write_results = available || (flags & VK_QUERY_RESULT_PARTIAL_BIT); |
| 261 |
|
| 262 |
assert(i * stride < dataSize); |
| 263 |
void *dst = (char *)pData + i * stride; |
| 264 |
|
| 265 |
panvk_priv_mem_readback(pool->mem, panvk_query_offset(pool, query), |
| 266 |
struct panvk_query_report, src) { |
| 267 |
switch (pool->vk.query_type) { |
| 268 |
case VK_QUERY_TYPE_OCCLUSION: { |
| 269 |
if (write_results) |
| 270 |
cpu_write_occlusion_query_result(dst, 0, flags, src, |
| 271 |
pool->reports_per_query); |
| 272 |
break; |
| 273 |
} |
| 274 |
#if PAN_ARCH >= 10 |
| 275 |
case VK_QUERY_TYPE_TIMESTAMP: { |
| 276 |
if (write_results) |
| 277 |
cpu_write_timestamp_query_result(dst, 0, flags, src, |
| 278 |
pool->reports_per_query); |
| 279 |
break; |
| 280 |
} |
| 281 |
case VK_QUERY_TYPE_PRIMITIVES_GENERATED_EXT: { |
| 282 |
if (write_results) |
| 283 |
cpu_write_query_result(dst, 0, flags, src[0].value); |
| 284 |
break; |
| 285 |
} |
| 286 |
#endif |
| 287 |
default: |
| 288 |
UNREACHABLE("Unsupported query type"); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if (!write_results) |
| 293 |
status = VK_NOT_READY; |
| 294 |
|
| 295 |
if (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) |
| 296 |
cpu_write_query_result(dst, 1, flags, available); |
| 297 |
} |
| 298 |
|
| 299 |
return status; |
| 300 |
} |