Debian Codegraph @

src/panfrost/vulkan/panvk_physical_device.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 *
4 * Derived from tu_device.c which is:
5 * Copyright © 2016 Red Hat.
6 * Copyright © 2016 Bas Nieuwenhuizen
7 * Copyright © 2015 Intel Corporation
8 *
9 * SPDX-License-Identifier: MIT
10 */
11
12 #include <sys/stat.h>
13 #include <sys/sysinfo.h>
14
15 #include "util/disk_cache.h"
16 #include "util/os_misc.h"
17 #include "util/u_atomic.h"
18 #include "git_sha1.h"
19
20 #include "vk_android.h"
21 #include "vk_device.h"
22 #include "vk_drm_syncobj.h"
23 #include "vk_format.h"
24 #include "vk_log.h"
25 #include "vk_util.h"
26
27 #include "panvk_device.h"
28 #include "panvk_entrypoints.h"
29 #include "panvk_image.h"
30 #include "panvk_instance.h"
31 #include "panvk_physical_device.h"
32 #include "panvk_wsi.h"
33
34 #include "pan_afbc.h"
35 #include "pan_props.h"
36
37 #include "genxml/gen_macros.h"
38
39 #define PER_ARCH_FUNCS(_ver) \
40 void panvk_v##_ver##_get_physical_device_extensions( \
41 const struct panvk_physical_device *device, \
42 struct vk_device_extension_table *ext); \
43 \
44 void panvk_v##_ver##_get_physical_device_features( \
45 const struct panvk_instance *instance, \
46 const struct panvk_physical_device *device, \
47 struct vk_features *features); \
48 \
49 void panvk_v##_ver##_get_physical_device_properties( \
50 const struct panvk_instance *instance, \
51 const struct panvk_physical_device *device, \
52 struct vk_properties *properties); \
53 \
54 VkResult panvk_v##_ver##_create_device( \
55 struct panvk_physical_device *physical_device, \
56 const VkDeviceCreateInfo *pCreateInfo, \
57 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice); \
58 \
59 void panvk_v##_ver##_destroy_device( \
60 struct panvk_device *device, const VkAllocationCallbacks *pAllocator)
61
62 PER_ARCH_FUNCS(6);
63 PER_ARCH_FUNCS(7);
64 PER_ARCH_FUNCS(10);
65 PER_ARCH_FUNCS(12);
66 PER_ARCH_FUNCS(13);
67
68 static VkResult
69 create_kmod_dev(struct panvk_physical_device *device,
70 const struct panvk_instance *instance, drmDevicePtr drm_device)
71 {
72 const char *path = drm_device->nodes[DRM_NODE_RENDER];
73 drmVersionPtr version;
74 int fd;
75
76 fd = open(path, O_RDWR | O_CLOEXEC);
77 if (fd < 0) {
78 return panvk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
79 "failed to open device %s", path);
80 }
81
82 version = drmGetVersion(fd);
83 if (!version) {
84 close(fd);
85 return panvk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
86 "failed to query kernel driver version for device %s",
87 path);
88 }
89
90 if (strcmp(version->name, "panfrost") && strcmp(version->name, "panthor")) {
91 drmFreeVersion(version);
92 close(fd);
93 return VK_ERROR_INCOMPATIBLE_DRIVER;
94 }
95
96 drmFreeVersion(version);
97
98 if (PANVK_DEBUG(STARTUP))
99 mesa_logi("Found compatible device '%s'.", path);
100
101 uint32_t flags = PAN_KMOD_DEV_FLAG_OWNS_FD;
102
103 if (PANVK_DEBUG(NO_USER_MMAP_SYNC))
104 flags |= PAN_KMOD_DEV_FLAG_MMAP_SYNC_THROUGH_KERNEL;
105
106 device->kmod.dev = pan_kmod_dev_create(fd, flags, &instance->kmod.allocator);
107
108 if (!device->kmod.dev) {
109 close(fd);
110 return panvk_errorf(instance, VK_ERROR_OUT_OF_HOST_MEMORY,
111 "cannot create device");
112 }
113
114 return VK_SUCCESS;
115 }
116
117 static VkResult
118 get_drm_device_ids(struct panvk_physical_device *device,
119 const struct panvk_instance *instance,
120 drmDevicePtr drm_device)
121 {
122 struct stat st;
123
124 if (stat(drm_device->nodes[DRM_NODE_RENDER], &st)) {
125 return vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
126 "failed to query render node stat");
127 }
128
129 device->drm.render_rdev = st.st_rdev;
130
131 if (drm_device->available_nodes & (1 << DRM_NODE_PRIMARY)) {
132 if (stat(drm_device->nodes[DRM_NODE_PRIMARY], &st)) {
133 return vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
134 "failed to query primary node stat");
135 }
136
137 device->drm.primary_rdev = st.st_rdev;
138 }
139
140 return VK_SUCCESS;
141 }
142
143 static void
144 init_shader_caches(struct panvk_physical_device *device,
145 const struct panvk_instance *instance)
146 {
147 blake3_hasher blake3_ctx;
148 _mesa_blake3_init(&blake3_ctx);
149
150 _mesa_blake3_update(&blake3_ctx, instance->driver_build_sha,
151 sizeof(instance->driver_build_sha));
152
153 _mesa_blake3_update(&blake3_ctx, &device->kmod.dev->props.gpu_id,
154 sizeof(device->kmod.dev->props.gpu_id));
155
156 unsigned char blake3[BLAKE3_KEY_LEN];
157 _mesa_blake3_final(&blake3_ctx, blake3);
158
159 STATIC_ASSERT(VK_UUID_SIZE <= BLAKE3_KEY_LEN);
160 memcpy(device->cache_uuid, blake3, VK_UUID_SIZE);
161
162 #ifdef ENABLE_SHADER_CACHE
163 char renderer[25];
164 ASSERTED int len =
165 snprintf(renderer, sizeof(renderer), "panvk_0x%016" PRIx64,
166 device->kmod.dev->props.gpu_id);
167 assert(len == sizeof(renderer) - 1);
168
169 char timestamp[BLAKE3_HEX_LEN];
170 _mesa_blake3_format(timestamp, instance->driver_build_sha);
171
172 const uint64_t driver_flags = 0;
173 device->vk.disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
174 #endif
175 }
176
177 static void
178 free_disk_cache(struct panvk_physical_device *device)
179 {
180 #ifdef ENABLE_SHADER_CACHE
181 if (device->vk.disk_cache) {
182 disk_cache_destroy(device->vk.disk_cache);
183 device->vk.disk_cache = NULL;
184 }
185 #else
186 assert(device->vk.disk_cache == NULL);
187 #endif
188 }
189
190 static VkResult
191 get_core_mask(struct panvk_physical_device *device,
192 const struct panvk_instance *instance, const char *option_name,
193 uint64_t *mask)
194 {
195 uint64_t present = device->kmod.dev->props.shader_present;
196 *mask = driQueryOptionu64(&instance->dri_options, option_name) & present;
197
198 if (!*mask)
199 return panvk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
200 "None of the cores specified in %s are present. "
201 "Available shader cores are 0x%" PRIx64 ".\n",
202 option_name, present);
203
204 return VK_SUCCESS;
205 }
206
207 static VkResult
208 get_core_masks(struct panvk_physical_device *device,
209 const struct panvk_instance *instance)
210 {
211 VkResult result;
212
213 result = get_core_mask(device, instance, "pan_compute_core_mask",
214 &device->compute_core_mask);
215 if (result != VK_SUCCESS)
216 return result;
217 result = get_core_mask(device, instance, "pan_fragment_core_mask",
218 &device->fragment_core_mask);
219
220 return result;
221 }
222
223 static uint64_t
224 get_system_heap_size()
225 {
226 struct sysinfo info;
227 sysinfo(&info);
228
229 uint64_t total_ram = (uint64_t)info.totalram * info.mem_unit;
230
231 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
232 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
233 */
234 uint64_t available_ram;
235 if (total_ram <= 4ull * 1024 * 1024 * 1024)
236 available_ram = total_ram / 2;
237 else
238 available_ram = total_ram * 3 / 4;
239
240 return available_ram;
241 }
242
243 static VkResult
244 get_device_heaps(struct panvk_physical_device *device,
245 const struct panvk_instance *instance)
246 {
247 int host_coherent_not_cached_idx = -1;
248 int host_cached_not_coherent_idx = -1;
249
250 device->memory.heap_count = 1;
251 device->memory.heaps[0] = (VkMemoryHeap) {
252 .size = get_system_heap_size(),
253 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
254 };
255
256 device->memory.type_count = 0;
257
258 /* We don't have VRAM, but we expose a device-local only type so we can
259 * prevent imported dma-bufs that come from other drivers/subsystems from
260 * being CPU-mapped.
261 */
262 device->memory.types[device->memory.type_count++] = (VkMemoryType) {
263 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
264 .heapIndex = 0,
265 };
266
267 if (device->kmod.dev->props.is_io_coherent) {
268 assert(device->memory.type_count < ARRAY_SIZE(device->memory.types));
269 /* If the device is coherent, we just have one memory type that's both
270 * host-cached and host-coherent. */
271 device->memory.types[device->memory.type_count++] = (VkMemoryType) {
272 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
273 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
274 VK_MEMORY_PROPERTY_HOST_CACHED_BIT |
275 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
276 .heapIndex = 0,
277 };
278 }
279
280 if (!PANVK_DEBUG(NO_WB_MMAP) &&
281 (device->kmod.dev->props.supported_bo_flags & PAN_KMOD_BO_FLAG_WB_MMAP)) {
282 assert(device->memory.type_count < ARRAY_SIZE(device->memory.types));
283 host_cached_not_coherent_idx = device->memory.type_count;
284 device->memory.types[device->memory.type_count++] = (VkMemoryType) {
285 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
286 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
287 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
288 .heapIndex = 0,
289 };
290 }
291
292 assert(device->memory.type_count < ARRAY_SIZE(device->memory.types));
293 host_coherent_not_cached_idx = device->memory.type_count;
294 device->memory.types[device->memory.type_count++] = (VkMemoryType) {
295 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
296 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
297 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
298 .heapIndex = 0,
299 };
300
301 /* Ideally, we'd place HOST_CACHED first for perf reasons, but there's
302 * so many broken CTS tests (missing or invalid flush/invalidate
303 * calls), and so many added at each version that it gets impossible to
304 * catch up. So, keep things ordered in a way that the first HOST_VISIBLE
305 * type is also the one requiring no CPU cache maintenance if we're asked
306 * to.
307 */
308 if (PANVK_DEBUG(COHERENT_BEFORE_CACHED) &&
309 host_cached_not_coherent_idx != -1 &&
310 host_coherent_not_cached_idx != -1 &&
311 host_coherent_not_cached_idx > host_cached_not_coherent_idx) {
312 VkMemoryType host_cached_not_coherent_type =
313 device->memory.types[host_cached_not_coherent_idx];
314
315 device->memory.types[host_cached_not_coherent_idx] =
316 device->memory.types[host_coherent_not_cached_idx];
317 device->memory.types[host_coherent_not_cached_idx] =
318 host_cached_not_coherent_type;
319 }
320
321 const uint64_t request_va =
322 PANVK_DEBUG(NO_EXTENDED_VA_RANGE) ? 1ull << 32 : 1ull << 48;
323 device->memory.max_supported_va =
324 pan_clamp_to_usable_va_range(device->kmod.dev, request_va);
325
326 return VK_SUCCESS;
327 }
328
329 static VkResult
330 get_device_sync_types(struct panvk_physical_device *device,
331 const struct panvk_instance *instance)
332 {
333 const unsigned arch = pan_arch(device->kmod.dev->props.gpu_id);
334 uint32_t sync_type_count = 0;
335
336 device->drm_syncobj_type = vk_drm_syncobj_get_type(device->kmod.dev->fd);
337 if (!device->drm_syncobj_type.features) {
338 return vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
339 "failed to query syncobj features");
340 }
341
342 device->sync_types[sync_type_count++] = &device->drm_syncobj_type;
343
344 if (arch >= 10) {
345 assert(device->drm_syncobj_type.features & VK_SYNC_FEATURE_TIMELINE);
346 } else {
347 /* We don't support timelines in the uAPI yet and we don't want it getting
348 * suddenly turned on by vk_drm_syncobj_get_type() without us adding panvk
349 * code for it first.
350 */
351 device->drm_syncobj_type.features &= ~VK_SYNC_FEATURE_TIMELINE;
352
353 device->sync_timeline_type =
354 vk_sync_timeline_get_type(&device->drm_syncobj_type);
355 device->sync_types[sync_type_count++] = &device->sync_timeline_type.sync;
356 }
357
358 assert(sync_type_count < ARRAY_SIZE(device->sync_types));
359 device->sync_types[sync_type_count] = NULL;
360
361 return VK_SUCCESS;
362 }
363
364 float
365 panvk_get_gpu_system_timestamp_period(const struct panvk_physical_device *device)
366 {
367 if (!device->kmod.dev->props.gpu_can_query_timestamp ||
368 !device->kmod.dev->props.timestamp_frequency)
369 return 0;
370
371 const float ns_per_s = 1000000000.0;
372 return ns_per_s / (float)device->kmod.dev->props.timestamp_frequency;
373 }
374
375 void
376 panvk_physical_device_finish(struct panvk_physical_device *device)
377 {
378 panvk_wsi_finish(device);
379
380 free_disk_cache(device);
381
382 pan_kmod_dev_destroy(device->kmod.dev);
383
384 vk_physical_device_finish(&device->vk);
385 }
386
387 VkResult
388 panvk_physical_device_init(struct panvk_physical_device *device,
389 struct panvk_instance *instance,
390 drmDevicePtr drm_device)
391 {
392 VkResult result;
393
394 result = create_kmod_dev(device, instance, drm_device);
395 if (result != VK_SUCCESS)
396 return result;
397
398 device->model = pan_get_model(device->kmod.dev->props.gpu_id,
399 device->kmod.dev->props.gpu_variant);
400
401 unsigned arch = pan_arch(device->kmod.dev->props.gpu_id);
402
403 if (!device->model) {
404 result = panvk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
405 "Unknown gpu_id (%#" PRIx64 ") or variant (%#x)",
406 device->kmod.dev->props.gpu_id,
407 device->kmod.dev->props.gpu_variant);
408 goto fail;
409 }
410
411 switch (arch) {
412 case 6:
413 case 7:
414 if (!os_get_option("PAN_I_WANT_A_BROKEN_VULKAN_DRIVER")) {
415 result = panvk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
416 "WARNING: panvk is not well-tested on v%d, "
417 "pass PAN_I_WANT_A_BROKEN_VULKAN_DRIVER=1 "
418 "if you know what you're doing.", arch);
419 goto fail;
420 }
421 break;
422
423 case 10:
424 case 12:
425 case 13:
426 break;
427
428 default:
429 result = panvk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
430 "%s not supported", device->model->name);
431 goto fail;
432 }
433
434 result = get_drm_device_ids(device, instance, drm_device);
435 if (result != VK_SUCCESS)
436 goto fail;
437
438 device->formats.all = pan_format_table(arch);
439 device->formats.blendable = pan_blendable_format_table(arch);
440
441 unsigned core_id_range;
442 unsigned core_count =
443 pan_query_core_count(&device->kmod.dev->props, &core_id_range);
444
445 memset(device->name, 0, sizeof(device->name));
446 sprintf(device->name, "%s MC%u", device->model->name, core_count);
447
448 result = get_core_masks(device, instance);
449 if (result != VK_SUCCESS)
450 goto fail;
451
452 result = get_device_heaps(device, instance);
453 if (result != VK_SUCCESS)
454 goto fail;
455
456 result = get_device_sync_types(device, instance);
457 if (result != VK_SUCCESS)
458 goto fail;
459
460 if (arch >= 10) {
461 /* XXX: Make dri options for thoses */
462 device->csf.tiler.chunk_size = 2 * 1024 * 1024;
463 device->csf.tiler.initial_chunks = 5;
464 device->csf.tiler.max_chunks = 64;
465 }
466
467 if (arch != 10)
468 vk_warn_non_conformant_implementation("panvk");
469
470 struct vk_device_extension_table supported_extensions;
471 panvk_arch_dispatch(arch, get_physical_device_extensions, device,
472 &supported_extensions);
473
474 struct vk_features supported_features;
475 panvk_arch_dispatch(arch, get_physical_device_features, instance,
476 device, &supported_features);
477
478 struct vk_physical_device_dispatch_table dispatch_table;
479 vk_physical_device_dispatch_table_from_entrypoints(
480 &dispatch_table, &panvk_physical_device_entrypoints, true);
481 vk_physical_device_dispatch_table_from_entrypoints(
482 &dispatch_table, &wsi_physical_device_entrypoints, false);
483
484 result =
485 vk_physical_device_init(&device->vk, &instance->vk, &supported_extensions,
486 &supported_features, NULL, &dispatch_table);
487
488 if (result != VK_SUCCESS)
489 goto fail;
490
491 /* initialize disk cache after vk_physical_device_init */
492 init_shader_caches(device, instance);
493
494 /* pipeline binary props rely on disk cache init state */
495 panvk_arch_dispatch(arch, get_physical_device_properties, instance, device,
496 &device->vk.properties);
497
498 device->vk.supported_sync_types = device->sync_types;
499
500 result = panvk_wsi_init(device);
501 if (result != VK_SUCCESS)
502 goto fail;
503
504 return VK_SUCCESS;
505
506 fail:
507 free_disk_cache(device);
508
509 if (device->vk.instance)
510 vk_physical_device_finish(&device->vk);
511
512 pan_kmod_dev_destroy(device->kmod.dev);
513
514 return result;
515 }
516
517 static void
518 panvk_fill_global_priority(const struct panvk_physical_device *physical_device,
519 uint32_t family_idx,
520 VkQueueFamilyGlobalPriorityPropertiesKHR *prio)
521 {
522 const unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
523 uint32_t prio_idx = 0;
524
525 switch (family_idx) {
526 case PANVK_QUEUE_FAMILY_GPU: {
527 enum pan_kmod_group_allow_priority_flags prio_mask =
528 physical_device->kmod.dev->props.allowed_group_priorities_mask;
529
530 /* Non-medium priority context is not hooked-up in the JM backend, even
531 * though the panfrost kmod advertize it. Manually filter non-medium
532 * priority for now.
533 */
534 if (arch < 10)
535 prio_mask &= PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM;
536
537 if (prio_mask & PAN_KMOD_GROUP_ALLOW_PRIORITY_LOW)
538 prio->priorities[prio_idx++] = VK_QUEUE_GLOBAL_PRIORITY_LOW_KHR;
539 if (prio_mask & PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM)
540 prio->priorities[prio_idx++] = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR;
541 if (prio_mask & PAN_KMOD_GROUP_ALLOW_PRIORITY_HIGH)
542 prio->priorities[prio_idx++] = VK_QUEUE_GLOBAL_PRIORITY_HIGH_KHR;
543 if (prio_mask & PAN_KMOD_GROUP_ALLOW_PRIORITY_REALTIME)
544 prio->priorities[prio_idx++] = VK_QUEUE_GLOBAL_PRIORITY_REALTIME_KHR;
545 break;
546 }
547
548 case PANVK_QUEUE_FAMILY_BIND:
549 prio->priorities[prio_idx++] = VK_QUEUE_GLOBAL_PRIORITY_MEDIUM;
550 break;
551
552 default:
553 UNREACHABLE("Unknown queue family");
554 }
555
556 prio->priorityCount = prio_idx;
557 }
558
559 VKAPI_ATTR void VKAPI_CALL
560 panvk_GetPhysicalDeviceQueueFamilyProperties2(
561 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount,
562 VkQueueFamilyProperties2 *pQueueFamilyProperties)
563 {
564 VK_FROM_HANDLE(panvk_physical_device, physical_device, physicalDevice);
565 VK_OUTARRAY_MAKE_TYPED(VkQueueFamilyProperties2, out, pQueueFamilyProperties,
566 pQueueFamilyPropertyCount);
567 unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
568
569 const VkQueueFamilyProperties qfamily_props[PANVK_QUEUE_FAMILY_COUNT] = {
570 [PANVK_QUEUE_FAMILY_GPU] = {
571 .queueFlags =
572 VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
573 /* On v10+ we can support up to 127 queues but this causes timeout on
574 some CTS tests */
575 .queueCount = arch >= 10 ? 2 : 1,
576 .timestampValidBits =
577 arch >= 10 &&
578 physical_device->kmod.dev->props.gpu_can_query_timestamp
579 ? 64
580 : 0,
581 .minImageTransferGranularity = {1, 1, 1},
582 },
583 [PANVK_QUEUE_FAMILY_BIND] = {
584 .queueFlags = VK_QUEUE_SPARSE_BINDING_BIT,
585 .queueCount = 1,
586 },
587 };
588
589 for (uint32_t family = 0; family < ARRAY_SIZE(qfamily_props); family++) {
590 if (family == PANVK_QUEUE_FAMILY_BIND &&
591 !physical_device->vk.supported_features.sparseBinding)
592 break;
593
594 vk_outarray_append_typed(VkQueueFamilyProperties2, &out, p) {
595 p->queueFamilyProperties = qfamily_props[family];
596
597 VkQueueFamilyGlobalPriorityPropertiesKHR *prio =
598 vk_find_struct(p->pNext, QUEUE_FAMILY_GLOBAL_PRIORITY_PROPERTIES_KHR);
599 if (prio)
600 panvk_fill_global_priority(physical_device, family, prio);
601 }
602 }
603 }
604
605 VKAPI_ATTR void VKAPI_CALL
606 panvk_GetPhysicalDeviceMemoryProperties2(
607 VkPhysicalDevice physicalDevice,
608 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
609 {
610 VK_FROM_HANDLE(panvk_physical_device, physical_device, physicalDevice);
611
612 pMemoryProperties->memoryProperties.memoryHeapCount =
613 physical_device->memory.heap_count;
614 for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
615 pMemoryProperties->memoryProperties.memoryHeaps[i] =
616 physical_device->memory.heaps[i];
617 }
618
619 pMemoryProperties->memoryProperties.memoryTypeCount =
620 physical_device->memory.type_count;
621 for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
622 pMemoryProperties->memoryProperties.memoryTypes[i] =
623 physical_device->memory.types[i];
624 }
625
626 vk_foreach_struct(ext, pMemoryProperties->pNext) {
627 switch (ext->sType) {
628 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT: {
629 VkPhysicalDeviceMemoryBudgetPropertiesEXT *p = (void *)ext;
630
631 uint64_t used = p_atomic_read(&physical_device->memory.heap_used);
632 uint64_t heap_size = physical_device->memory.heaps[0].size;
633 uint64_t available;
634
635 if (!os_get_available_system_memory(&available))
636 available = heap_size;
637
638 /* From the Vulkan 1.3.278 spec:
639 *
640 * "heapUsage is an array of VK_MAX_MEMORY_HEAPS VkDeviceSize
641 * values in which memory usages are returned, with one element
642 * for each memory heap. A heap’s usage is an estimate of how
643 * much memory the process is currently using in that heap."
644 */
645 p->heapUsage[0] = used;
646
647 /* From the Vulkan 1.3.278 spec:
648 *
649 * "heapBudget is an array of VK_MAX_MEMORY_HEAPS VkDeviceSize
650 * values in which memory budgets are returned, with one
651 * element for each memory heap. A heap’s budget is a rough
652 * estimate of how much memory the process can allocate from
653 * that heap before allocations may fail or cause performance
654 * degradation. The budget includes any currently allocated
655 * device memory."
656 *
657 * and
658 *
659 * "The heapBudget value must be less than or equal to
660 * VkMemoryHeap::size for each heap."
661 *
662 * available (queried above) is the total amount of free memory
663 * system-wide and does not include our allocations so we need
664 * to add that in.
665 */
666 uint64_t budget = MIN2(available + used, heap_size);
667
668 /* Set the budget at 90% of available to avoid thrashing */
669 p->heapBudget[0] = ROUND_DOWN_TO(budget * 9 / 10, 1 << 20);
670
671 /* From the Vulkan 1.3.278 spec:
672 *
673 * "The heapBudget and heapUsage values must be zero for array
674 * elements greater than or equal to
675 * VkPhysicalDeviceMemoryProperties::memoryHeapCount. The
676 * heapBudget value must be non-zero for array elements less than
677 * VkPhysicalDeviceMemoryProperties::memoryHeapCount."
678 */
679 for (unsigned i = 1; i < VK_MAX_MEMORY_HEAPS; i++) {
680 p->heapBudget[i] = 0;
681 p->heapUsage[i] = 0;
682 }
683 break;
684 }
685 default:
686 vk_debug_ignored_stype(ext->sType);
687 break;
688 }
689 }
690 }
691
692 VKAPI_ATTR VkResult VKAPI_CALL
693 panvk_CreateDevice(VkPhysicalDevice physicalDevice,
694 const VkDeviceCreateInfo *pCreateInfo,
695 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
696 {
697 VK_FROM_HANDLE(panvk_physical_device, physical_device, physicalDevice);
698 unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
699 VkResult result = VK_ERROR_INITIALIZATION_FAILED;
700
701 panvk_arch_dispatch_ret(arch, create_device, result, physical_device,
702 pCreateInfo, pAllocator, pDevice);
703
704 return result;
705 }
706
707 VKAPI_ATTR void VKAPI_CALL
708 panvk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
709 {
710 VK_FROM_HANDLE(panvk_device, device, _device);
711 struct panvk_physical_device *physical_device =
712 to_panvk_physical_device(device->vk.physical);
713 unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
714
715 panvk_arch_dispatch(arch, destroy_device, device, pAllocator);
716 }
717
718 static bool
719 unsupported_yuv_format(enum pipe_format pfmt)
720 {
721 switch (pfmt) {
722 /* 3-plane YUV 444 and 16-bit 3-plane YUV are not supported natively by
723 * the HW.
724 */
725 case PIPE_FORMAT_Y8_U8_V8_444_UNORM:
726 case PIPE_FORMAT_Y16_U16_V16_420_UNORM:
727 case PIPE_FORMAT_Y16_U16_V16_422_UNORM:
728 case PIPE_FORMAT_Y16_U16_V16_444_UNORM:
729 return true;
730 default:
731 return false;
732 }
733 }
734
735 static bool
736 format_is_supported(struct panvk_physical_device *physical_device,
737 const struct pan_format fmt, enum pipe_format pfmt)
738 {
739 if (pfmt == PIPE_FORMAT_NONE)
740 return false;
741
742 if (unsupported_yuv_format(pfmt))
743 return false;
744
745 /* If the format ID is zero, it's not supported. */
746 if (!fmt.hw)
747 return false;
748
749 /* Compressed formats (ID < 32) are optional. We need to check against
750 * the supported formats reported by the GPU. */
751 if (util_format_is_compressed(pfmt)) {
752 uint32_t supported_compr_fmts =
753 pan_query_compressed_formats(&physical_device->kmod.dev->props);
754
755 if (!(BITFIELD_BIT(fmt.texfeat_bit) & supported_compr_fmts))
756 return false;
757 }
758
759 return true;
760 }
761
762 static VkFormatFeatureFlags2
763 get_image_plane_format_features(struct panvk_physical_device *physical_device,
764 VkFormat format)
765 {
766 VkFormatFeatureFlags2 features = 0;
767 enum pipe_format pfmt = vk_format_to_pipe_format(format);
768 const struct pan_format fmt = physical_device->formats.all[pfmt];
769 unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
770
771 if (!format_is_supported(physical_device, fmt, pfmt))
772 return 0;
773
774 if (fmt.bind & PAN_BIND_SAMPLER_VIEW) {
775 features |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT |
776 VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
777 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT;
778
779 if (arch >= 10)
780 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
781
782 /* Integer formats only support nearest filtering */
783 if (!util_format_is_scaled(pfmt) && !util_format_is_pure_integer(pfmt))
784 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
785
786 features |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
787
788 if (vk_format_has_depth(format))
789 features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
790 }
791
792 if (fmt.bind & PAN_BIND_RENDER_TARGET) {
793 features |= VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
794 features |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT;
795 features |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
796 }
797
798 if (fmt.bind & PAN_BIND_STORAGE_IMAGE) {
799 features |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT |
800 VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
801 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
802 if (pfmt == PIPE_FORMAT_R32_UINT || pfmt == PIPE_FORMAT_R32_SINT ||
803 pfmt == PIPE_FORMAT_R32_FLOAT)
804 features |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
805 }
806
807 if (fmt.bind & PAN_BIND_DEPTH_STENCIL)
808 features |= VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT;
809
810 if (features != 0)
811 features |= VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT;
812
813 return features;
814 }
815
816 static VkFormatFeatureFlags2
817 get_image_format_features(struct panvk_physical_device *physical_device,
818 VkFormat format)
819 {
820 const struct vk_format_ycbcr_info *ycbcr_info =
821 vk_format_get_ycbcr_info(format);
822
823 if (ycbcr_info == NULL)
824 return get_image_plane_format_features(physical_device, format);
825
826 if (unsupported_yuv_format(vk_format_to_pipe_format(format)))
827 return 0;
828
829 /* For multi-plane, we get the feature flags of each plane separately,
830 * then take their intersection as the overall format feature flags
831 */
832 VkFormatFeatureFlags2 features = ~0ull;
833 bool cosited_chroma = false;
834 for (uint8_t plane = 0; plane < ycbcr_info->n_planes; plane++) {
835 const struct vk_format_ycbcr_plane *plane_info =
836 &ycbcr_info->planes[plane];
837 features &=
838 get_image_plane_format_features(physical_device, plane_info->format);
839 if (plane_info->denominator_scales[0] > 1 ||
840 plane_info->denominator_scales[1] > 1)
841 cosited_chroma = true;
842 }
843 if (features == 0)
844 return 0;
845
846 /* Uh... We really should be able to sample from YCbCr */
847 assert(features & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT);
848 assert(features & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT);
849
850 /* Siting is handled in the YCbCr lowering pass. */
851 features |= VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
852 if (cosited_chroma)
853 features |= VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT;
854
855 /* These aren't allowed for YCbCr formats */
856 features &= ~(VK_FORMAT_FEATURE_2_BLIT_SRC_BIT |
857 VK_FORMAT_FEATURE_2_BLIT_DST_BIT |
858 VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
859 VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT |
860 VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT |
861 VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
862 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT);
863
864 /* This is supported on all YCbCr formats */
865 features |=
866 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
867
868 if (ycbcr_info->n_planes > 1) {
869 /* DISJOINT_BIT implies that each plane has its own separate binding,
870 * while SEPARATE_RECONSTRUCTION_FILTER_BIT implies that luma and chroma
871 * each have their own, separate filters, so these two bits make sense
872 * for multi-planar formats only.
873 */
874 features |= VK_FORMAT_FEATURE_2_DISJOINT_BIT |
875 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT;
876 }
877
878 return features;
879 }
880
881 /* Note: update nir_shader_compiler_options.max_samples when changing this. */
882 VkSampleCountFlags
883 panvk_get_sample_counts(unsigned arch, unsigned max_tib_size,
884 unsigned max_cbuf_atts, unsigned format_size)
885 {
886 VkSampleCountFlags sample_counts =
887 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT;
888
889 unsigned max_msaa =
890 pan_get_max_msaa(arch, max_tib_size, max_cbuf_atts, format_size);
891
892 assert(max_msaa >= 4);
893
894 if (arch >= 12)
895 sample_counts |= VK_SAMPLE_COUNT_2_BIT;
896
897 if (max_msaa >= 8)
898 sample_counts |= VK_SAMPLE_COUNT_8_BIT;
899
900 if (max_msaa >= 16)
901 sample_counts |= VK_SAMPLE_COUNT_16_BIT;
902
903 return sample_counts;
904 }
905
906 static VkFormatFeatureFlags2
907 get_image_format_sample_counts(struct panvk_physical_device *physical_device,
908 VkFormat format)
909 {
910 unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
911 unsigned max_tib_size = pan_query_tib_size(physical_device->model);
912 unsigned max_cbuf_atts = pan_get_max_cbufs(arch, max_tib_size);
913
914 assert(!vk_format_is_compressed(format));
915
916 enum pipe_format pfmt = vk_format_to_pipe_format(format);
917 unsigned format_size =
918 pan_format_tib_size(pfmt, physical_device->formats.blendable[pfmt].internal);
919
920 return panvk_get_sample_counts(arch, max_tib_size, max_cbuf_atts,
921 format_size);
922 }
923
924 static VkFormatFeatureFlags2
925 get_buffer_format_features(struct panvk_physical_device *physical_device,
926 VkFormat format)
927 {
928 VkFormatFeatureFlags2 features = 0;
929 enum pipe_format pfmt = vk_format_to_pipe_format(format);
930 const struct pan_format fmt = physical_device->formats.all[pfmt];
931
932 if (!format_is_supported(physical_device, fmt, pfmt))
933 return 0;
934
935 /* Reject sRGB formats (see
936 * https://github.com/KhronosGroup/Vulkan-Docs/issues/2214).
937 */
938 if ((fmt.bind & PAN_BIND_VERTEX_BUFFER) && !util_format_is_srgb(pfmt))
939 features |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
940
941 if (fmt.bind & PAN_BIND_TEXEL_BUFFER)
942 features |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT |
943 VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT |
944 VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
945 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
946
947 if (pfmt == PIPE_FORMAT_R32_UINT || pfmt == PIPE_FORMAT_R32_SINT)
948 features |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
949
950 return features;
951 }
952
953 VKAPI_ATTR void VKAPI_CALL
954 panvk_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,
955 VkFormat format,
956 VkFormatProperties2 *pFormatProperties)
957 {
958 VK_FROM_HANDLE(panvk_physical_device, physical_device, physicalDevice);
959 const unsigned arch = pan_arch(physical_device->kmod.dev->props.gpu_id);
960
961 VkFormatFeatureFlags2 tex =
962 get_image_format_features(physical_device, format);
963 VkFormatFeatureFlags2 buffer =
964 get_buffer_format_features(physical_device, format);
965
966 pFormatProperties->formatProperties = (VkFormatProperties){
967 .linearTilingFeatures = tex,
968 .optimalTilingFeatures = tex,
969 .bufferFeatures = buffer,
970 };
971
972 VkFormatProperties3 *formatProperties3 =
973 vk_find_struct(pFormatProperties->pNext, FORMAT_PROPERTIES_3);
974 if (formatProperties3) {
975 formatProperties3->linearTilingFeatures = tex;
976 formatProperties3->optimalTilingFeatures = tex;
977 formatProperties3->bufferFeatures = buffer;
978 }
979
980 VkDrmFormatModifierPropertiesListEXT *list = vk_find_struct(
981 pFormatProperties->pNext, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
982 if (list) {
983 VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out,
984 list->pDrmFormatModifierProperties,
985 &list->drmFormatModifierCount);
986 if (pFormatProperties->formatProperties.optimalTilingFeatures &&
987 PANVK_DEBUG(WSI_AFBC))
988 {
989 PAN_SUPPORTED_MODIFIERS(supported);
990 for (int mi = 0; mi < ARRAY_SIZE(supported); mi++) {
991 if (drm_is_afbc(supported[mi]) &&
992 pan_afbc_supports_format(arch, vk_format_to_pipe_format(format)))
993 {
994 vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, mod_props)
995 {
996 mod_props->drmFormatModifier = supported[mi];
997 mod_props->drmFormatModifierPlaneCount = 1;
998 mod_props->drmFormatModifierTilingFeatures =
999 pFormatProperties->formatProperties.optimalTilingFeatures;
1000 }
1001 }
1002 }
1003 }
1004
1005 if (pFormatProperties->formatProperties.linearTilingFeatures) {
1006 vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out,
1007 mod_props)
1008 {
1009 mod_props->drmFormatModifier = DRM_FORMAT_MOD_LINEAR;
1010 mod_props->drmFormatModifierPlaneCount = 1;
1011 mod_props->drmFormatModifierTilingFeatures =
1012 pFormatProperties->formatProperties.linearTilingFeatures;
1013 }
1014 }
1015 }
1016
1017 VkSubpassResolvePerformanceQueryEXT *subpass_resolve_perf = vk_find_struct(
1018 pFormatProperties->pNext, SUBPASS_RESOLVE_PERFORMANCE_QUERY_EXT);
1019 if (subpass_resolve_perf) {
1020 /* We always resolve in a separate command instead of in HW atm. */
1021 subpass_resolve_perf->optimal = VK_FALSE;
1022 }
1023 }
1024
1025 #define MAX_IMAGE_SIZE_PX (1 << 16)
1026
1027 static VkExtent3D
1028 get_max_2d_image_size(struct panvk_physical_device *phys_dev, VkFormat format)
1029 {
1030 const unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
1031 const uint64_t max_img_size_B =
1032 arch <= 10 ? u_uintN_max(32) : u_uintN_max(48);
1033 const enum pipe_format pfmt = vk_format_to_pipe_format(format);
1034 const uint32_t fmt_blksize = util_format_get_blocksize(pfmt);
1035 /* Evenly split blocks across all axis. */
1036 const uint32_t max_size_el = floor(sqrt(max_img_size_B / fmt_blksize));
1037 const VkExtent3D ret = {
1038 .width = MIN2(max_size_el * util_format_get_blockwidth(pfmt),
1039 MAX_IMAGE_SIZE_PX),
1040 .height = MIN2(max_size_el * util_format_get_blockheight(pfmt),
1041 MAX_IMAGE_SIZE_PX),
1042 .depth = 1,
1043 };
1044
1045 assert(ret.width >= phys_dev->vk.properties.maxImageDimension2D);
1046 assert(ret.height >= phys_dev->vk.properties.maxImageDimension2D);
1047 return ret;
1048 }
1049
1050 static VkExtent3D
1051 get_max_3d_image_size(struct panvk_physical_device *phys_dev, VkFormat format)
1052 {
1053 const unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
1054 const uint64_t max_img_size_B =
1055 arch <= 10 ? u_uintN_max(32) : u_uintN_max(48);
1056 enum pipe_format pfmt = vk_format_to_pipe_format(format);
1057 uint32_t fmt_blksize = util_format_get_blocksize(pfmt);
1058 /* Evenly split blocks across each axis. */
1059 const uint32_t max_size_el = floor(cbrt(max_img_size_B / fmt_blksize));
1060 const VkExtent3D ret = {
1061 .width = MIN2(max_size_el * util_format_get_blockwidth(pfmt),
1062 MAX_IMAGE_SIZE_PX),
1063 .height = MIN2(max_size_el * util_format_get_blockheight(pfmt),
1064 MAX_IMAGE_SIZE_PX),
1065 .depth = MIN2(max_size_el * util_format_get_blockdepth(pfmt),
1066 MAX_IMAGE_SIZE_PX),
1067 };
1068
1069 assert(ret.width >= phys_dev->vk.properties.maxImageDimension3D);
1070 assert(ret.height >= phys_dev->vk.properties.maxImageDimension3D);
1071 assert(ret.depth >= phys_dev->vk.properties.maxImageDimension3D);
1072 return ret;
1073 }
1074
1075 static VkResult
1076 get_image_format_properties(struct panvk_physical_device *physical_device,
1077 const VkPhysicalDeviceImageFormatInfo2 *info,
1078 VkImageFormatProperties *pImageFormatProperties,
1079 VkFormatFeatureFlags2 *p_feature_flags)
1080 {
1081 VkFormatFeatureFlags2 format_feature_flags;
1082 VkExtent3D maxExtent;
1083 uint32_t maxMipLevels;
1084 uint32_t maxArraySize;
1085 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1086 enum pipe_format format = vk_format_to_pipe_format(info->format);
1087
1088 const VkImageStencilUsageCreateInfo *stencil_usage_info =
1089 vk_find_struct_const(info->pNext, IMAGE_STENCIL_USAGE_CREATE_INFO);
1090 VkImageUsageFlags stencil_usage =
1091 stencil_usage_info ? stencil_usage_info->stencilUsage : info->usage;
1092 VkImageUsageFlags all_usage = info->usage | stencil_usage;
1093 const struct vk_format_ycbcr_info *ycbcr_info =
1094 vk_format_get_ycbcr_info(info->format);
1095
1096 if (info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1097 if (!physical_device->vk.supported_features.sparseBinding)
1098 goto unsupported;
1099
1100 /*
1101 * Sparse only manipulates device mappings and we implement host copies on
1102 * host. Purely hypotetically, we could implement host copies for sparse
1103 * images in one of, but not limited to, the following ways:
1104 *
1105 * * submitting a device copy and immediately waiting on it
1106 *
1107 * * mirror sparse binds' modifications to device mappings on host
1108 *
1109 * * share a single address space and thus mappings between host and device
1110 *
1111 * but realistically speaking, the set of people, apps and tests in the
1112 * CTS that expect a driver to implement host copies on sparse images is
1113 * exactly empty, so let's just not bother.
1114 */
1115 if (info->usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT)
1116 goto unsupported;
1117 }
1118
1119 if (info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1120 if (!((info->type == VK_IMAGE_TYPE_2D &&
1121 physical_device->vk.supported_features.sparseResidencyImage2D) ||
1122 (info->type == VK_IMAGE_TYPE_3D &&
1123 physical_device->vk.supported_features.sparseResidencyImage3D)))
1124 goto unsupported;
1125
1126 /* Only single aspect (thus single plane) stuff is supported for now */
1127 if (util_bitcount(vk_format_aspects(info->format)) != 1)
1128 goto unsupported;
1129
1130 if (info->tiling != VK_IMAGE_TILING_OPTIMAL)
1131 goto unsupported;
1132
1133 struct panvk_sparse_block_desc sblock_desc = panvk_get_sparse_block_desc(info->type, info->format);
1134 if (!panvk_sparse_block_is_valid(sblock_desc))
1135 goto unsupported;
1136
1137 VkImageUsageFlags allowed_usages =
1138 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1139 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
1140 VK_IMAGE_USAGE_SAMPLED_BIT |
1141 VK_IMAGE_USAGE_STORAGE_BIT |
1142 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1143 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT |
1144 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
1145 if (all_usage & ~allowed_usages)
1146 goto unsupported;
1147 }
1148
1149 switch (info->tiling) {
1150 case VK_IMAGE_TILING_LINEAR:
1151 case VK_IMAGE_TILING_OPTIMAL:
1152 break;
1153 case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT: {
1154 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *mod_info =
1155 vk_find_struct_const(
1156 info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
1157
1158 /* TODO: switch to using a more generic function for checking mod support here
1159 * when adding new modifiers, so that this case doesn't become too big. */
1160 const bool can_use_afbc =
1161 PANVK_DEBUG(WSI_AFBC) &&
1162 panvk_image_can_use_afbc(physical_device, info->format, info->usage,
1163 info->type, info->tiling, 0);
1164 const bool supported = (drm_is_afbc(mod_info->drmFormatModifier) && can_use_afbc) ||
1165 mod_info->drmFormatModifier == DRM_FORMAT_MOD_LINEAR;
1166 if (!supported)
1167 goto unsupported;
1168
1169 /* The only difference between optimal and linear is currently whether
1170 * depth/stencil attachments are allowed on depth/stencil formats.
1171 * There's no reason to allow importing depth/stencil textures, so just
1172 * disallow it and then this annoying edge case goes away.
1173 */
1174 if (util_format_is_depth_or_stencil(format))
1175 goto unsupported;
1176 break;
1177 }
1178 default:
1179 /* VK_KHR_maintenance5: Physical-device-level functions can now be called
1180 * with any value in the valid range for a type beyond the defined
1181 * enumerants [...] */
1182 goto unsupported;
1183 }
1184
1185 /* For the purposes of these checks, we don't care about all the extra
1186 * YCbCr features and we just want the intersection of features available
1187 * to all planes of the given format.
1188 */
1189 if (ycbcr_info == NULL) {
1190 format_feature_flags =
1191 get_image_format_features(physical_device, info->format);
1192 } else {
1193 format_feature_flags = ~0u;
1194 assert(ycbcr_info->n_planes > 0);
1195 for (uint8_t plane = 0; plane < ycbcr_info->n_planes; plane++) {
1196 const VkFormat plane_format = ycbcr_info->planes[plane].format;
1197 format_feature_flags &=
1198 get_image_format_features(physical_device, plane_format);
1199 }
1200 }
1201
1202 if (format_feature_flags == 0)
1203 goto unsupported;
1204
1205 if (ycbcr_info && info->type != VK_IMAGE_TYPE_2D)
1206 goto unsupported;
1207
1208 switch (info->type) {
1209 case VK_IMAGE_TYPE_1D:
1210 maxExtent.width = 1 << 16;
1211 maxExtent.height = 1;
1212 maxExtent.depth = 1;
1213 maxMipLevels = 17; /* log2(maxWidth) + 1 */
1214 maxArraySize = 1 << 16;
1215 break;
1216 case VK_IMAGE_TYPE_2D:
1217 maxExtent = get_max_2d_image_size(physical_device, info->format);
1218 maxMipLevels = util_logbase2(maxExtent.width) + 1;
1219 maxArraySize = 1 << 16;
1220 break;
1221 case VK_IMAGE_TYPE_3D:
1222 maxExtent = get_max_3d_image_size(physical_device, info->format);
1223 maxMipLevels = util_logbase2(maxExtent.width) + 1;
1224 maxArraySize = 1;
1225 break;
1226 default:
1227 /* VK_KHR_maintenance5: Physical-device-level functions can now be called
1228 * with any value in the valid range for a type beyond the defined
1229 * enumerants [...] */
1230 goto unsupported;
1231 }
1232
1233 if (ycbcr_info)
1234 maxMipLevels = 1;
1235
1236 if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1237 info->type == VK_IMAGE_TYPE_2D && ycbcr_info == NULL &&
1238 (format_feature_flags &
1239 (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT |
1240 VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1241 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1242 !(all_usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1243 sampleCounts |=
1244 get_image_format_sample_counts(physical_device, info->format);
1245 }
1246
1247 /* From the Vulkan 1.2.199 spec:
1248 *
1249 * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be
1250 * created with usage flags that are not supported for the format the
1251 * image is created with but are supported for at least one format a
1252 * VkImageView created from the image can have."
1253 *
1254 * If VK_IMAGE_CREATE_EXTENDED_USAGE_BIT is set, views can be created with
1255 * different usage than the image so we can't always filter on usage.
1256 * There is one exception to this below for storage.
1257 */
1258 if (!(info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)) {
1259 if (all_usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1260 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
1261 goto unsupported;
1262 }
1263 }
1264
1265 if (all_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1266 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
1267 goto unsupported;
1268 }
1269 }
1270
1271 if (all_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT ||
1272 ((all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) &&
1273 !vk_format_is_depth_or_stencil(info->format))) {
1274 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
1275 goto unsupported;
1276 }
1277 }
1278
1279 if ((all_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ||
1280 ((all_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) &&
1281 vk_format_is_depth_or_stencil(info->format))) {
1282 if (!(format_feature_flags &
1283 VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1284 goto unsupported;
1285 }
1286 }
1287 }
1288
1289 *pImageFormatProperties = (VkImageFormatProperties){
1290 .maxExtent = maxExtent,
1291 .maxMipLevels = maxMipLevels,
1292 .maxArrayLayers = maxArraySize,
1293 .sampleCounts = sampleCounts,
1294
1295 /* We need to limit images to 32-bit range, because the maximum
1296 * slice-stride is 32-bit wide, meaning that if we allocate an image
1297 * with the maximum width and height, we end up overflowing it.
1298 *
1299 * We get around this by simply limiting the maximum resource size.
1300 */
1301 .maxResourceSize = UINT32_MAX,
1302 };
1303
1304 if (p_feature_flags)
1305 *p_feature_flags = format_feature_flags;
1306
1307 return VK_SUCCESS;
1308 unsupported:
1309 *pImageFormatProperties = (VkImageFormatProperties){
1310 .maxExtent = {0, 0, 0},
1311 .maxMipLevels = 0,
1312 .maxArrayLayers = 0,
1313 .sampleCounts = 0,
1314 .maxResourceSize = 0,
1315 };
1316
1317 return VK_ERROR_FORMAT_NOT_SUPPORTED;
1318 }
1319
1320 static VkResult
1321 panvk_get_external_image_format_properties(
1322 const struct panvk_physical_device *physical_device,
1323 const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1324 VkExternalMemoryHandleTypeFlagBits handleType,
1325 VkExternalMemoryProperties *external_properties)
1326 {
1327 const VkExternalMemoryHandleTypeFlags supported_handle_types =
1328 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1329 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1330
1331 if (!(handleType & supported_handle_types)) {
1332 return panvk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1333 "VkExternalMemoryTypeFlagBits(0x%x) unsupported",
1334 handleType);
1335 }
1336
1337 /* pan_image_layout_init requires 2D for explicit layout */
1338 if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D) {
1339 return panvk_errorf(
1340 physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1341 "VkExternalMemoryTypeFlagBits(0x%x) unsupported for VkImageType(%d)",
1342 handleType, pImageFormatInfo->type);
1343 }
1344
1345 /* There is no restriction on opaque fds. But for dma-bufs, we want to
1346 * make sure vkGetImageSubresourceLayout can be used to query the image
1347 * layout of an exported dma-buf. We also want to make sure
1348 * VkImageDrmFormatModifierExplicitCreateInfoEXT can be used to specify the
1349 * image layout of an imported dma-buf. These add restrictions on the
1350 * image tilings.
1351 */
1352 VkExternalMemoryFeatureFlags features = 0;
1353 if (handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
1354 pImageFormatInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1355 features |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1356 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1357 } else if (pImageFormatInfo->tiling == VK_IMAGE_TILING_LINEAR) {
1358 features |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1359 }
1360
1361 if (!features) {
1362 return panvk_errorf(
1363 physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1364 "VkExternalMemoryTypeFlagBits(0x%x) unsupported for VkImageTiling(%d)",
1365 handleType, pImageFormatInfo->tiling);
1366 }
1367
1368 *external_properties = (VkExternalMemoryProperties){
1369 .externalMemoryFeatures = features,
1370 .exportFromImportedHandleTypes = supported_handle_types,
1371 .compatibleHandleTypes = supported_handle_types,
1372 };
1373
1374 return VK_SUCCESS;
1375 }
1376
1377 VKAPI_ATTR VkResult VKAPI_CALL
1378 panvk_GetPhysicalDeviceImageFormatProperties2(
1379 VkPhysicalDevice physicalDevice,
1380 const VkPhysicalDeviceImageFormatInfo2 *base_info,
1381 VkImageFormatProperties2 *base_props)
1382 {
1383 VK_FROM_HANDLE(panvk_physical_device, physical_device, physicalDevice);
1384 const VkImageStencilUsageCreateInfo *stencil_usage_info = NULL;
1385 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1386 const VkPhysicalDeviceImageViewImageFormatInfoEXT *image_view_info = NULL;
1387 VkExternalImageFormatProperties *external_props = NULL;
1388 VkFilterCubicImageViewImageFormatPropertiesEXT *cubic_props = NULL;
1389 VkFormatFeatureFlags2 format_feature_flags;
1390 VkHostImageCopyDevicePerformanceQuery *hic_props = NULL;
1391 VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1392 VkResult result;
1393
1394 result = get_image_format_properties(physical_device, base_info,
1395 &base_props->imageFormatProperties,
1396 &format_feature_flags);
1397 if (result != VK_SUCCESS)
1398 return result;
1399
1400 /* Extract input structs */
1401 vk_foreach_struct_const(s, base_info->pNext) {
1402 switch (s->sType) {
1403 case VK_STRUCTURE_TYPE_IMAGE_STENCIL_USAGE_CREATE_INFO:
1404 stencil_usage_info = (const void*)s;
1405 break;
1406 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1407 external_info = (const void *)s;
1408 break;
1409 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_IMAGE_FORMAT_INFO_EXT:
1410 image_view_info = (const void *)s;
1411 break;
1412 default:
1413 break;
1414 }
1415 }
1416
1417 /* Extract output structs */
1418 vk_foreach_struct(s, base_props->pNext) {
1419 switch (s->sType) {
1420 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1421 external_props = (void *)s;
1422 break;
1423 case VK_STRUCTURE_TYPE_FILTER_CUBIC_IMAGE_VIEW_IMAGE_FORMAT_PROPERTIES_EXT:
1424 cubic_props = (void *)s;
1425 break;
1426 case VK_STRUCTURE_TYPE_HOST_IMAGE_COPY_DEVICE_PERFORMANCE_QUERY:
1427 hic_props = (void *)s;
1428 break;
1429 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1430 ycbcr_props = (void *)s;
1431 break;
1432 default:
1433 break;
1434 }
1435 }
1436
1437 /* From the Vulkan 1.0.42 spec:
1438 *
1439 * If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1440 * behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1441 * present and VkExternalImageFormatProperties will be ignored.
1442 */
1443 if (external_info && external_info->handleType != 0) {
1444 VkExternalImageFormatProperties fallback_external_props;
1445
1446 if (!external_props) {
1447 memset(&fallback_external_props, 0, sizeof(fallback_external_props));
1448 external_props = &fallback_external_props;
1449 }
1450
1451 if (external_info->handleType ==
1452 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) {
1453 result = vk_android_get_ahb_image_properties(physicalDevice, base_info,
1454 base_props);
1455 } else {
1456 result = panvk_get_external_image_format_properties(
1457 physical_device, base_info, external_info->handleType,
1458 &external_props->externalMemoryProperties);
1459 }
1460 if (result != VK_SUCCESS)
1461 goto fail;
1462
1463 /* pan_image_layout_init requirements for explicit layout */
1464 base_props->imageFormatProperties.maxMipLevels = 1;
1465 base_props->imageFormatProperties.maxArrayLayers = 1;
1466 base_props->imageFormatProperties.sampleCounts = 1;
1467 }
1468
1469 if (cubic_props) {
1470 /* note: blob only allows cubic filtering for 2D and 2D array views
1471 * its likely we can enable it for 1D and CUBE, needs testing however
1472 */
1473 if ((image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D ||
1474 image_view_info->imageViewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY) &&
1475 (format_feature_flags &
1476 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT)) {
1477 cubic_props->filterCubic = true;
1478 cubic_props->filterCubicMinmax = true;
1479 } else {
1480 cubic_props->filterCubic = false;
1481 cubic_props->filterCubicMinmax = false;
1482 }
1483 }
1484
1485 if (hic_props) {
1486 VkImageUsageFlags stencil_usage = stencil_usage_info ?
1487 stencil_usage_info->stencilUsage : base_info->usage;
1488
1489 /* We don't support AFBC for images used for host transfer. So, if an
1490 * image could have been tiled as AFBC if it weren't for host transfer,
1491 * report suboptimal access. */
1492 VkImageUsageFlags usage = base_info->usage | stencil_usage;
1493 usage &= ~VK_IMAGE_USAGE_HOST_TRANSFER_BIT;
1494 bool can_use_afbc = panvk_image_can_use_afbc(
1495 physical_device, base_info->format, usage, base_info->type,
1496 base_info->tiling, base_info->flags);
1497 hic_props->optimalDeviceAccess = !can_use_afbc;
1498
1499 /* FIXME: we only support host transfer with certain modifiers and for now
1500 * there's no easy way to know whether the presence of HOST_TRANSFER will
1501 * be the thing that causes the modifier to be filtered out, and thus
1502 * causing a difference in memory layout.
1503 *
1504 * See https://gitlab.freedesktop.org/panfrost/mesa/-/issues/281 for
1505 * details.
1506 */
1507 hic_props->identicalMemoryLayout = false;
1508 }
1509
1510 const struct vk_format_ycbcr_info *ycbcr_info =
1511 vk_format_get_ycbcr_info(base_info->format);
1512 const unsigned plane_count =
1513 vk_format_get_plane_count(base_info->format);
1514
1515 /* From the Vulkan 1.3.259 spec, VkImageCreateInfo:
1516 *
1517 * VUID-VkImageCreateInfo-imageCreateFormatFeatures-02260
1518 *
1519 * "If format is a multi-planar format, and if imageCreateFormatFeatures
1520 * (as defined in Image Creation Limits) does not contain
1521 * VK_FORMAT_FEATURE_2_DISJOINT_BIT, then flags must not contain
1522 * VK_IMAGE_CREATE_DISJOINT_BIT"
1523 *
1524 * This is satisfied trivially because we support DISJOINT on all
1525 * multi-plane formats. Also,
1526 *
1527 * VUID-VkImageCreateInfo-format-01577
1528 *
1529 * "If format is not a multi-planar format, and flags does not include
1530 * VK_IMAGE_CREATE_ALIAS_BIT, flags must not contain
1531 * VK_IMAGE_CREATE_DISJOINT_BIT"
1532 */
1533 if (plane_count == 1 &&
1534 !(base_info->flags & VK_IMAGE_CREATE_ALIAS_BIT) &&
1535 (base_info->flags & VK_IMAGE_CREATE_DISJOINT_BIT))
1536 goto fail;
1537
1538 if (ycbcr_info &&
1539 ((base_info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) ||
1540 (base_info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT)))
1541 goto fail;
1542
1543 if ((base_info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) &&
1544 (base_info->usage & VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT))
1545 goto fail;
1546
1547 if (ycbcr_props)
1548 ycbcr_props->combinedImageSamplerDescriptorCount = 1;
1549
1550 return VK_SUCCESS;
1551
1552 fail:
1553 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1554 /* From the Vulkan 1.0.42 spec:
1555 *
1556 * If the combination of parameters to
1557 * vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1558 * the implementation for use in vkCreateImage, then all members of
1559 * imageFormatProperties will be filled with zero.
1560 */
1561 base_props->imageFormatProperties = (VkImageFormatProperties){};
1562 }
1563
1564 return result;
1565 }
1566
1567 VKAPI_ATTR void VKAPI_CALL
1568 panvk_GetPhysicalDeviceSparseImageFormatProperties2(
1569 VkPhysicalDevice physicalDevice,
1570 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1571 uint32_t *pPropertyCount, VkSparseImageFormatProperties2 *pProperties)
1572 {
1573 VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, out, pProperties, pPropertyCount);
1574
1575 VkPhysicalDeviceImageFormatInfo2 img_info = {
1576 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1577 .format = pFormatInfo->format,
1578 .type = pFormatInfo->type,
1579 .tiling = pFormatInfo->tiling,
1580 .usage = pFormatInfo->usage,
1581 .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
1582 VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
1583 };
1584 VkImageFormatProperties2 img_props = {};
1585 if (panvk_GetPhysicalDeviceImageFormatProperties2(physicalDevice, &img_info, &img_props) != VK_SUCCESS)
1586 return;
1587
1588 if (!(img_props.imageFormatProperties.sampleCounts & pFormatInfo->samples))
1589 return;
1590
1591 /*
1592 * We don't support multisampled sparse partially-resident images for now.
1593 * Weirdly enough, banning it the obvious way by making
1594 * get_image_format_properties report sampleCounts of 1 when flags includes
1595 * SPARSE_RESIDENCY causes "required sample counts not supported" CTS fails,
1596 * so we ban them here.
1597 */
1598 if (pFormatInfo->samples != 1)
1599 return;
1600
1601 vk_outarray_append_typed(VkSparseImageFormatProperties2, &out, prop) {
1602 prop->properties = panvk_get_sparse_image_fmt_props(pFormatInfo->type, pFormatInfo->format);
1603 }
1604 }
1605
1606 VKAPI_ATTR void VKAPI_CALL
1607 panvk_GetPhysicalDeviceExternalBufferProperties(
1608 VkPhysicalDevice physicalDevice,
1609 const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
1610 VkExternalBufferProperties *pExternalBufferProperties)
1611 {
1612 if (pExternalBufferInfo->handleType ==
1613 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) {
1614 vk_android_get_ahb_buffer_properties(physicalDevice, pExternalBufferInfo,
1615 pExternalBufferProperties);
1616 return;
1617 }
1618
1619 const VkExternalMemoryHandleTypeFlags supported_handle_types =
1620 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1621 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1622
1623 /* From the Vulkan 1.3.298 spec:
1624 *
1625 * compatibleHandleTypes must include at least handleType.
1626 */
1627 VkExternalMemoryHandleTypeFlags handle_types =
1628 pExternalBufferInfo->handleType;
1629 VkExternalMemoryFeatureFlags features = 0;
1630 if (pExternalBufferInfo->handleType & supported_handle_types) {
1631 handle_types |= supported_handle_types;
1632 features |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1633 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1634 }
1635
1636 pExternalBufferProperties->externalMemoryProperties =
1637 (VkExternalMemoryProperties){
1638 .externalMemoryFeatures = features,
1639 .exportFromImportedHandleTypes = handle_types,
1640 .compatibleHandleTypes = handle_types,
1641 };
1642 }