a893c3be148cad5e940070fde25234302de602ca
[mesa.git] / src / freedreno / vulkan / tu_device.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 */
27
28 #include "tu_private.h"
29
30 #include <fcntl.h>
31 #include <libsync.h>
32 #include <stdbool.h>
33 #include <string.h>
34 #include <sys/mman.h>
35 #include <sys/sysinfo.h>
36 #include <unistd.h>
37 #include <xf86drm.h>
38
39 #include "compiler/glsl_types.h"
40 #include "util/debug.h"
41 #include "util/disk_cache.h"
42 #include "util/u_atomic.h"
43 #include "vk_format.h"
44 #include "vk_util.h"
45
46 #include "drm-uapi/msm_drm.h"
47
48 /* for fd_get_driver/device_uuid() */
49 #include "freedreno/common/freedreno_uuid.h"
50
51 static void
52 tu_semaphore_remove_temp(struct tu_device *device,
53 struct tu_semaphore *sem);
54
55 static int
56 tu_device_get_cache_uuid(uint16_t family, void *uuid)
57 {
58 uint32_t mesa_timestamp;
59 uint16_t f = family;
60 memset(uuid, 0, VK_UUID_SIZE);
61 if (!disk_cache_get_function_timestamp(tu_device_get_cache_uuid,
62 &mesa_timestamp))
63 return -1;
64
65 memcpy(uuid, &mesa_timestamp, 4);
66 memcpy((char *) uuid + 4, &f, 2);
67 snprintf((char *) uuid + 6, VK_UUID_SIZE - 10, "tu");
68 return 0;
69 }
70
71 static VkResult
72 tu_bo_init(struct tu_device *dev,
73 struct tu_bo *bo,
74 uint32_t gem_handle,
75 uint64_t size)
76 {
77 uint64_t iova = tu_gem_info_iova(dev, gem_handle);
78 if (!iova)
79 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
80
81 *bo = (struct tu_bo) {
82 .gem_handle = gem_handle,
83 .size = size,
84 .iova = iova,
85 };
86
87 return VK_SUCCESS;
88 }
89
90 VkResult
91 tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size)
92 {
93 /* TODO: Choose better flags. As of 2018-11-12, freedreno/drm/msm_bo.c
94 * always sets `flags = MSM_BO_WC`, and we copy that behavior here.
95 */
96 uint32_t gem_handle = tu_gem_new(dev, size, MSM_BO_WC);
97 if (!gem_handle)
98 return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
99
100 VkResult result = tu_bo_init(dev, bo, gem_handle, size);
101 if (result != VK_SUCCESS) {
102 tu_gem_close(dev, gem_handle);
103 return vk_error(dev->instance, result);
104 }
105
106 return VK_SUCCESS;
107 }
108
109 VkResult
110 tu_bo_init_dmabuf(struct tu_device *dev,
111 struct tu_bo *bo,
112 uint64_t size,
113 int fd)
114 {
115 uint32_t gem_handle = tu_gem_import_dmabuf(dev, fd, size);
116 if (!gem_handle)
117 return vk_error(dev->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
118
119 VkResult result = tu_bo_init(dev, bo, gem_handle, size);
120 if (result != VK_SUCCESS) {
121 tu_gem_close(dev, gem_handle);
122 return vk_error(dev->instance, result);
123 }
124
125 return VK_SUCCESS;
126 }
127
128 int
129 tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo)
130 {
131 return tu_gem_export_dmabuf(dev, bo->gem_handle);
132 }
133
134 VkResult
135 tu_bo_map(struct tu_device *dev, struct tu_bo *bo)
136 {
137 if (bo->map)
138 return VK_SUCCESS;
139
140 uint64_t offset = tu_gem_info_offset(dev, bo->gem_handle);
141 if (!offset)
142 return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
143
144 /* TODO: Should we use the wrapper os_mmap() like Freedreno does? */
145 void *map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
146 dev->physical_device->local_fd, offset);
147 if (map == MAP_FAILED)
148 return vk_error(dev->instance, VK_ERROR_MEMORY_MAP_FAILED);
149
150 bo->map = map;
151 return VK_SUCCESS;
152 }
153
154 void
155 tu_bo_finish(struct tu_device *dev, struct tu_bo *bo)
156 {
157 assert(bo->gem_handle);
158
159 if (bo->map)
160 munmap(bo->map, bo->size);
161
162 tu_gem_close(dev, bo->gem_handle);
163 }
164
165 static VkResult
166 tu_physical_device_init(struct tu_physical_device *device,
167 struct tu_instance *instance,
168 drmDevicePtr drm_device)
169 {
170 const char *path = drm_device->nodes[DRM_NODE_RENDER];
171 VkResult result = VK_SUCCESS;
172 drmVersionPtr version;
173 int fd;
174 int master_fd = -1;
175
176 fd = open(path, O_RDWR | O_CLOEXEC);
177 if (fd < 0) {
178 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
179 "failed to open device %s", path);
180 }
181
182 /* Version 1.3 added MSM_INFO_IOVA. */
183 const int min_version_major = 1;
184 const int min_version_minor = 3;
185
186 version = drmGetVersion(fd);
187 if (!version) {
188 close(fd);
189 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
190 "failed to query kernel driver version for device %s",
191 path);
192 }
193
194 if (strcmp(version->name, "msm")) {
195 drmFreeVersion(version);
196 close(fd);
197 return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
198 "device %s does not use the msm kernel driver", path);
199 }
200
201 if (version->version_major != min_version_major ||
202 version->version_minor < min_version_minor) {
203 result = vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER,
204 "kernel driver for device %s has version %d.%d, "
205 "but Vulkan requires version >= %d.%d",
206 path, version->version_major, version->version_minor,
207 min_version_major, min_version_minor);
208 drmFreeVersion(version);
209 close(fd);
210 return result;
211 }
212
213 device->msm_major_version = version->version_major;
214 device->msm_minor_version = version->version_minor;
215
216 drmFreeVersion(version);
217
218 if (instance->debug_flags & TU_DEBUG_STARTUP)
219 tu_logi("Found compatible device '%s'.", path);
220
221 vk_object_base_init(NULL, &device->base, VK_OBJECT_TYPE_PHYSICAL_DEVICE);
222 device->instance = instance;
223 assert(strlen(path) < ARRAY_SIZE(device->path));
224 strncpy(device->path, path, ARRAY_SIZE(device->path));
225
226 if (instance->enabled_extensions.KHR_display) {
227 master_fd =
228 open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
229 if (master_fd >= 0) {
230 /* TODO: free master_fd is accel is not working? */
231 }
232 }
233
234 device->master_fd = master_fd;
235 device->local_fd = fd;
236
237 if (tu_drm_get_gpu_id(device, &device->gpu_id)) {
238 if (instance->debug_flags & TU_DEBUG_STARTUP)
239 tu_logi("Could not query the GPU ID");
240 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
241 "could not get GPU ID");
242 goto fail;
243 }
244
245 if (tu_drm_get_gmem_size(device, &device->gmem_size)) {
246 if (instance->debug_flags & TU_DEBUG_STARTUP)
247 tu_logi("Could not query the GMEM size");
248 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
249 "could not get GMEM size");
250 goto fail;
251 }
252
253 if (tu_drm_get_gmem_base(device, &device->gmem_base)) {
254 if (instance->debug_flags & TU_DEBUG_STARTUP)
255 tu_logi("Could not query the GMEM size");
256 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
257 "could not get GMEM size");
258 goto fail;
259 }
260
261 memset(device->name, 0, sizeof(device->name));
262 sprintf(device->name, "FD%d", device->gpu_id);
263
264 switch (device->gpu_id) {
265 case 618:
266 device->ccu_offset_gmem = 0x7c000; /* 0x7e000 in some cases? */
267 device->ccu_offset_bypass = 0x10000;
268 device->tile_align_w = 64;
269 device->magic.PC_UNKNOWN_9805 = 0x0;
270 device->magic.SP_UNKNOWN_A0F8 = 0x0;
271 break;
272 case 630:
273 case 640:
274 device->ccu_offset_gmem = 0xf8000;
275 device->ccu_offset_bypass = 0x20000;
276 device->tile_align_w = 64;
277 device->magic.PC_UNKNOWN_9805 = 0x1;
278 device->magic.SP_UNKNOWN_A0F8 = 0x1;
279 break;
280 case 650:
281 device->ccu_offset_gmem = 0x114000;
282 device->ccu_offset_bypass = 0x30000;
283 device->tile_align_w = 96;
284 device->magic.PC_UNKNOWN_9805 = 0x2;
285 device->magic.SP_UNKNOWN_A0F8 = 0x2;
286 break;
287 default:
288 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
289 "device %s is unsupported", device->name);
290 goto fail;
291 }
292 if (tu_device_get_cache_uuid(device->gpu_id, device->cache_uuid)) {
293 result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
294 "cannot generate UUID");
295 goto fail;
296 }
297
298 /* The gpu id is already embedded in the uuid so we just pass "tu"
299 * when creating the cache.
300 */
301 char buf[VK_UUID_SIZE * 2 + 1];
302 disk_cache_format_hex_id(buf, device->cache_uuid, VK_UUID_SIZE * 2);
303 device->disk_cache = disk_cache_create(device->name, buf, 0);
304
305 fprintf(stderr, "WARNING: tu is not a conformant vulkan implementation, "
306 "testing use only.\n");
307
308 fd_get_driver_uuid(device->driver_uuid);
309 fd_get_device_uuid(device->device_uuid, device->gpu_id);
310
311 tu_physical_device_get_supported_extensions(device, &device->supported_extensions);
312
313 if (result != VK_SUCCESS) {
314 vk_error(instance, result);
315 goto fail;
316 }
317
318 result = tu_wsi_init(device);
319 if (result != VK_SUCCESS) {
320 vk_error(instance, result);
321 goto fail;
322 }
323
324 return VK_SUCCESS;
325
326 fail:
327 close(fd);
328 if (master_fd != -1)
329 close(master_fd);
330 return result;
331 }
332
333 static void
334 tu_physical_device_finish(struct tu_physical_device *device)
335 {
336 tu_wsi_finish(device);
337
338 disk_cache_destroy(device->disk_cache);
339 close(device->local_fd);
340 if (device->master_fd != -1)
341 close(device->master_fd);
342
343 vk_object_base_finish(&device->base);
344 }
345
346 static VKAPI_ATTR void *
347 default_alloc_func(void *pUserData,
348 size_t size,
349 size_t align,
350 VkSystemAllocationScope allocationScope)
351 {
352 return malloc(size);
353 }
354
355 static VKAPI_ATTR void *
356 default_realloc_func(void *pUserData,
357 void *pOriginal,
358 size_t size,
359 size_t align,
360 VkSystemAllocationScope allocationScope)
361 {
362 return realloc(pOriginal, size);
363 }
364
365 static VKAPI_ATTR void
366 default_free_func(void *pUserData, void *pMemory)
367 {
368 free(pMemory);
369 }
370
371 static const VkAllocationCallbacks default_alloc = {
372 .pUserData = NULL,
373 .pfnAllocation = default_alloc_func,
374 .pfnReallocation = default_realloc_func,
375 .pfnFree = default_free_func,
376 };
377
378 static const struct debug_control tu_debug_options[] = {
379 { "startup", TU_DEBUG_STARTUP },
380 { "nir", TU_DEBUG_NIR },
381 { "ir3", TU_DEBUG_IR3 },
382 { "nobin", TU_DEBUG_NOBIN },
383 { "sysmem", TU_DEBUG_SYSMEM },
384 { "forcebin", TU_DEBUG_FORCEBIN },
385 { "noubwc", TU_DEBUG_NOUBWC },
386 { NULL, 0 }
387 };
388
389 const char *
390 tu_get_debug_option_name(int id)
391 {
392 assert(id < ARRAY_SIZE(tu_debug_options) - 1);
393 return tu_debug_options[id].string;
394 }
395
396 static int
397 tu_get_instance_extension_index(const char *name)
398 {
399 for (unsigned i = 0; i < TU_INSTANCE_EXTENSION_COUNT; ++i) {
400 if (strcmp(name, tu_instance_extensions[i].extensionName) == 0)
401 return i;
402 }
403 return -1;
404 }
405
406 VkResult
407 tu_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,
408 const VkAllocationCallbacks *pAllocator,
409 VkInstance *pInstance)
410 {
411 struct tu_instance *instance;
412 VkResult result;
413
414 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
415
416 uint32_t client_version;
417 if (pCreateInfo->pApplicationInfo &&
418 pCreateInfo->pApplicationInfo->apiVersion != 0) {
419 client_version = pCreateInfo->pApplicationInfo->apiVersion;
420 } else {
421 tu_EnumerateInstanceVersion(&client_version);
422 }
423
424 instance = vk_zalloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
425 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
426
427 if (!instance)
428 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
429
430 vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE);
431
432 if (pAllocator)
433 instance->alloc = *pAllocator;
434 else
435 instance->alloc = default_alloc;
436
437 instance->api_version = client_version;
438 instance->physical_device_count = -1;
439
440 instance->debug_flags =
441 parse_debug_string(getenv("TU_DEBUG"), tu_debug_options);
442
443 if (instance->debug_flags & TU_DEBUG_STARTUP)
444 tu_logi("Created an instance");
445
446 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
447 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
448 int index = tu_get_instance_extension_index(ext_name);
449
450 if (index < 0 || !tu_instance_extensions_supported.extensions[index]) {
451 vk_object_base_finish(&instance->base);
452 vk_free2(&default_alloc, pAllocator, instance);
453 return vk_error(instance, VK_ERROR_EXTENSION_NOT_PRESENT);
454 }
455
456 instance->enabled_extensions.extensions[index] = true;
457 }
458
459 result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
460 if (result != VK_SUCCESS) {
461 vk_object_base_finish(&instance->base);
462 vk_free2(&default_alloc, pAllocator, instance);
463 return vk_error(instance, result);
464 }
465
466 glsl_type_singleton_init_or_ref();
467
468 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
469
470 *pInstance = tu_instance_to_handle(instance);
471
472 return VK_SUCCESS;
473 }
474
475 void
476 tu_DestroyInstance(VkInstance _instance,
477 const VkAllocationCallbacks *pAllocator)
478 {
479 TU_FROM_HANDLE(tu_instance, instance, _instance);
480
481 if (!instance)
482 return;
483
484 for (int i = 0; i < instance->physical_device_count; ++i) {
485 tu_physical_device_finish(instance->physical_devices + i);
486 }
487
488 VG(VALGRIND_DESTROY_MEMPOOL(instance));
489
490 glsl_type_singleton_decref();
491
492 vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
493
494 vk_object_base_finish(&instance->base);
495 vk_free(&instance->alloc, instance);
496 }
497
498 static VkResult
499 tu_enumerate_devices(struct tu_instance *instance)
500 {
501 /* TODO: Check for more devices ? */
502 drmDevicePtr devices[8];
503 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
504 int max_devices;
505
506 instance->physical_device_count = 0;
507
508 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
509
510 if (instance->debug_flags & TU_DEBUG_STARTUP) {
511 if (max_devices < 0)
512 tu_logi("drmGetDevices2 returned error: %s\n", strerror(max_devices));
513 else
514 tu_logi("Found %d drm nodes", max_devices);
515 }
516
517 if (max_devices < 1)
518 return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
519
520 for (unsigned i = 0; i < (unsigned) max_devices; i++) {
521 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
522 devices[i]->bustype == DRM_BUS_PLATFORM) {
523
524 result = tu_physical_device_init(
525 instance->physical_devices + instance->physical_device_count,
526 instance, devices[i]);
527 if (result == VK_SUCCESS)
528 ++instance->physical_device_count;
529 else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
530 break;
531 }
532 }
533 drmFreeDevices(devices, max_devices);
534
535 return result;
536 }
537
538 VkResult
539 tu_EnumeratePhysicalDevices(VkInstance _instance,
540 uint32_t *pPhysicalDeviceCount,
541 VkPhysicalDevice *pPhysicalDevices)
542 {
543 TU_FROM_HANDLE(tu_instance, instance, _instance);
544 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
545
546 VkResult result;
547
548 if (instance->physical_device_count < 0) {
549 result = tu_enumerate_devices(instance);
550 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
551 return result;
552 }
553
554 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
555 vk_outarray_append(&out, p)
556 {
557 *p = tu_physical_device_to_handle(instance->physical_devices + i);
558 }
559 }
560
561 return vk_outarray_status(&out);
562 }
563
564 VkResult
565 tu_EnumeratePhysicalDeviceGroups(
566 VkInstance _instance,
567 uint32_t *pPhysicalDeviceGroupCount,
568 VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroupProperties)
569 {
570 TU_FROM_HANDLE(tu_instance, instance, _instance);
571 VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
572 pPhysicalDeviceGroupCount);
573 VkResult result;
574
575 if (instance->physical_device_count < 0) {
576 result = tu_enumerate_devices(instance);
577 if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER)
578 return result;
579 }
580
581 for (uint32_t i = 0; i < instance->physical_device_count; ++i) {
582 vk_outarray_append(&out, p)
583 {
584 p->physicalDeviceCount = 1;
585 p->physicalDevices[0] =
586 tu_physical_device_to_handle(instance->physical_devices + i);
587 p->subsetAllocation = false;
588 }
589 }
590
591 return vk_outarray_status(&out);
592 }
593
594 void
595 tu_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
596 VkPhysicalDeviceFeatures *pFeatures)
597 {
598 memset(pFeatures, 0, sizeof(*pFeatures));
599
600 *pFeatures = (VkPhysicalDeviceFeatures) {
601 .robustBufferAccess = true,
602 .fullDrawIndexUint32 = true,
603 .imageCubeArray = true,
604 .independentBlend = true,
605 .geometryShader = true,
606 .tessellationShader = true,
607 .sampleRateShading = true,
608 .dualSrcBlend = true,
609 .logicOp = true,
610 .multiDrawIndirect = true,
611 .drawIndirectFirstInstance = true,
612 .depthClamp = true,
613 .depthBiasClamp = true,
614 .fillModeNonSolid = true,
615 .depthBounds = true,
616 .wideLines = false,
617 .largePoints = true,
618 .alphaToOne = true,
619 .multiViewport = false,
620 .samplerAnisotropy = true,
621 .textureCompressionETC2 = true,
622 .textureCompressionASTC_LDR = true,
623 .textureCompressionBC = true,
624 .occlusionQueryPrecise = true,
625 .pipelineStatisticsQuery = false,
626 .vertexPipelineStoresAndAtomics = false,
627 .fragmentStoresAndAtomics = false,
628 .shaderTessellationAndGeometryPointSize = false,
629 .shaderImageGatherExtended = false,
630 .shaderStorageImageExtendedFormats = false,
631 .shaderStorageImageMultisample = false,
632 .shaderUniformBufferArrayDynamicIndexing = false,
633 .shaderSampledImageArrayDynamicIndexing = false,
634 .shaderStorageBufferArrayDynamicIndexing = false,
635 .shaderStorageImageArrayDynamicIndexing = false,
636 .shaderStorageImageReadWithoutFormat = false,
637 .shaderStorageImageWriteWithoutFormat = false,
638 .shaderClipDistance = false,
639 .shaderCullDistance = false,
640 .shaderFloat64 = false,
641 .shaderInt64 = false,
642 .shaderInt16 = false,
643 .sparseBinding = false,
644 .variableMultisampleRate = false,
645 .inheritedQueries = false,
646 };
647 }
648
649 void
650 tu_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
651 VkPhysicalDeviceFeatures2 *pFeatures)
652 {
653 vk_foreach_struct(ext, pFeatures->pNext)
654 {
655 switch (ext->sType) {
656 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES: {
657 VkPhysicalDeviceVulkan11Features *features = (void *) ext;
658 features->storageBuffer16BitAccess = false;
659 features->uniformAndStorageBuffer16BitAccess = false;
660 features->storagePushConstant16 = false;
661 features->storageInputOutput16 = false;
662 features->multiview = false;
663 features->multiviewGeometryShader = false;
664 features->multiviewTessellationShader = false;
665 features->variablePointersStorageBuffer = true;
666 features->variablePointers = true;
667 features->protectedMemory = false;
668 features->samplerYcbcrConversion = true;
669 features->shaderDrawParameters = true;
670 break;
671 }
672 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
673 VkPhysicalDeviceVariablePointersFeatures *features = (void *) ext;
674 features->variablePointersStorageBuffer = true;
675 features->variablePointers = true;
676 break;
677 }
678 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
679 VkPhysicalDeviceMultiviewFeatures *features =
680 (VkPhysicalDeviceMultiviewFeatures *) ext;
681 features->multiview = false;
682 features->multiviewGeometryShader = false;
683 features->multiviewTessellationShader = false;
684 break;
685 }
686 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
687 VkPhysicalDeviceShaderDrawParametersFeatures *features =
688 (VkPhysicalDeviceShaderDrawParametersFeatures *) ext;
689 features->shaderDrawParameters = true;
690 break;
691 }
692 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
693 VkPhysicalDeviceProtectedMemoryFeatures *features =
694 (VkPhysicalDeviceProtectedMemoryFeatures *) ext;
695 features->protectedMemory = false;
696 break;
697 }
698 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
699 VkPhysicalDevice16BitStorageFeatures *features =
700 (VkPhysicalDevice16BitStorageFeatures *) ext;
701 features->storageBuffer16BitAccess = false;
702 features->uniformAndStorageBuffer16BitAccess = false;
703 features->storagePushConstant16 = false;
704 features->storageInputOutput16 = false;
705 break;
706 }
707 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
708 VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
709 (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
710 features->samplerYcbcrConversion = true;
711 break;
712 }
713 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
714 VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
715 (VkPhysicalDeviceDescriptorIndexingFeaturesEXT *) ext;
716 features->shaderInputAttachmentArrayDynamicIndexing = false;
717 features->shaderUniformTexelBufferArrayDynamicIndexing = false;
718 features->shaderStorageTexelBufferArrayDynamicIndexing = false;
719 features->shaderUniformBufferArrayNonUniformIndexing = false;
720 features->shaderSampledImageArrayNonUniformIndexing = false;
721 features->shaderStorageBufferArrayNonUniformIndexing = false;
722 features->shaderStorageImageArrayNonUniformIndexing = false;
723 features->shaderInputAttachmentArrayNonUniformIndexing = false;
724 features->shaderUniformTexelBufferArrayNonUniformIndexing = false;
725 features->shaderStorageTexelBufferArrayNonUniformIndexing = false;
726 features->descriptorBindingUniformBufferUpdateAfterBind = false;
727 features->descriptorBindingSampledImageUpdateAfterBind = false;
728 features->descriptorBindingStorageImageUpdateAfterBind = false;
729 features->descriptorBindingStorageBufferUpdateAfterBind = false;
730 features->descriptorBindingUniformTexelBufferUpdateAfterBind = false;
731 features->descriptorBindingStorageTexelBufferUpdateAfterBind = false;
732 features->descriptorBindingUpdateUnusedWhilePending = false;
733 features->descriptorBindingPartiallyBound = false;
734 features->descriptorBindingVariableDescriptorCount = false;
735 features->runtimeDescriptorArray = false;
736 break;
737 }
738 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
739 VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
740 (VkPhysicalDeviceConditionalRenderingFeaturesEXT *) ext;
741 features->conditionalRendering = false;
742 features->inheritedConditionalRendering = false;
743 break;
744 }
745 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
746 VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
747 (VkPhysicalDeviceTransformFeedbackFeaturesEXT *) ext;
748 features->transformFeedback = true;
749 features->geometryStreams = false;
750 break;
751 }
752 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT: {
753 VkPhysicalDeviceIndexTypeUint8FeaturesEXT *features =
754 (VkPhysicalDeviceIndexTypeUint8FeaturesEXT *)ext;
755 features->indexTypeUint8 = true;
756 break;
757 }
758 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
759 VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
760 (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
761 features->vertexAttributeInstanceRateDivisor = true;
762 features->vertexAttributeInstanceRateZeroDivisor = true;
763 break;
764 }
765 default:
766 break;
767 }
768 }
769 return tu_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
770 }
771
772 void
773 tu_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
774 VkPhysicalDeviceProperties *pProperties)
775 {
776 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
777 VkSampleCountFlags sample_counts =
778 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
779
780 /* I have no idea what the maximum size is, but the hardware supports very
781 * large numbers of descriptors (at least 2^16). This limit is based on
782 * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
783 * we don't have to think about what to do if that overflows, but really
784 * nothing is likely to get close to this.
785 */
786 const size_t max_descriptor_set_size = (1 << 28) / A6XX_TEX_CONST_DWORDS;
787
788 VkPhysicalDeviceLimits limits = {
789 .maxImageDimension1D = (1 << 14),
790 .maxImageDimension2D = (1 << 14),
791 .maxImageDimension3D = (1 << 11),
792 .maxImageDimensionCube = (1 << 14),
793 .maxImageArrayLayers = (1 << 11),
794 .maxTexelBufferElements = 128 * 1024 * 1024,
795 .maxUniformBufferRange = MAX_UNIFORM_BUFFER_RANGE,
796 .maxStorageBufferRange = MAX_STORAGE_BUFFER_RANGE,
797 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
798 .maxMemoryAllocationCount = UINT32_MAX,
799 .maxSamplerAllocationCount = 64 * 1024,
800 .bufferImageGranularity = 64, /* A cache line */
801 .sparseAddressSpaceSize = 0xffffffffu, /* buffer max size */
802 .maxBoundDescriptorSets = MAX_SETS,
803 .maxPerStageDescriptorSamplers = max_descriptor_set_size,
804 .maxPerStageDescriptorUniformBuffers = max_descriptor_set_size,
805 .maxPerStageDescriptorStorageBuffers = max_descriptor_set_size,
806 .maxPerStageDescriptorSampledImages = max_descriptor_set_size,
807 .maxPerStageDescriptorStorageImages = max_descriptor_set_size,
808 .maxPerStageDescriptorInputAttachments = MAX_RTS,
809 .maxPerStageResources = max_descriptor_set_size,
810 .maxDescriptorSetSamplers = max_descriptor_set_size,
811 .maxDescriptorSetUniformBuffers = max_descriptor_set_size,
812 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_UNIFORM_BUFFERS,
813 .maxDescriptorSetStorageBuffers = max_descriptor_set_size,
814 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_STORAGE_BUFFERS,
815 .maxDescriptorSetSampledImages = max_descriptor_set_size,
816 .maxDescriptorSetStorageImages = max_descriptor_set_size,
817 .maxDescriptorSetInputAttachments = MAX_RTS,
818 .maxVertexInputAttributes = 32,
819 .maxVertexInputBindings = 32,
820 .maxVertexInputAttributeOffset = 4095,
821 .maxVertexInputBindingStride = 2048,
822 .maxVertexOutputComponents = 128,
823 .maxTessellationGenerationLevel = 64,
824 .maxTessellationPatchSize = 32,
825 .maxTessellationControlPerVertexInputComponents = 128,
826 .maxTessellationControlPerVertexOutputComponents = 128,
827 .maxTessellationControlPerPatchOutputComponents = 120,
828 .maxTessellationControlTotalOutputComponents = 4096,
829 .maxTessellationEvaluationInputComponents = 128,
830 .maxTessellationEvaluationOutputComponents = 128,
831 .maxGeometryShaderInvocations = 32,
832 .maxGeometryInputComponents = 64,
833 .maxGeometryOutputComponents = 128,
834 .maxGeometryOutputVertices = 256,
835 .maxGeometryTotalOutputComponents = 1024,
836 .maxFragmentInputComponents = 124,
837 .maxFragmentOutputAttachments = 8,
838 .maxFragmentDualSrcAttachments = 1,
839 .maxFragmentCombinedOutputResources = 8,
840 .maxComputeSharedMemorySize = 32768,
841 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
842 .maxComputeWorkGroupInvocations = 2048,
843 .maxComputeWorkGroupSize = { 2048, 2048, 2048 },
844 .subPixelPrecisionBits = 8,
845 .subTexelPrecisionBits = 8,
846 .mipmapPrecisionBits = 8,
847 .maxDrawIndexedIndexValue = UINT32_MAX,
848 .maxDrawIndirectCount = UINT32_MAX,
849 .maxSamplerLodBias = 4095.0 / 256.0, /* [-16, 15.99609375] */
850 .maxSamplerAnisotropy = 16,
851 .maxViewports = MAX_VIEWPORTS,
852 .maxViewportDimensions = { (1 << 14), (1 << 14) },
853 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
854 .viewportSubPixelBits = 8,
855 .minMemoryMapAlignment = 4096, /* A page */
856 .minTexelBufferOffsetAlignment = 64,
857 .minUniformBufferOffsetAlignment = 64,
858 .minStorageBufferOffsetAlignment = 64,
859 .minTexelOffset = -16,
860 .maxTexelOffset = 15,
861 .minTexelGatherOffset = -32,
862 .maxTexelGatherOffset = 31,
863 .minInterpolationOffset = -0.5,
864 .maxInterpolationOffset = 0.4375,
865 .subPixelInterpolationOffsetBits = 4,
866 .maxFramebufferWidth = (1 << 14),
867 .maxFramebufferHeight = (1 << 14),
868 .maxFramebufferLayers = (1 << 10),
869 .framebufferColorSampleCounts = sample_counts,
870 .framebufferDepthSampleCounts = sample_counts,
871 .framebufferStencilSampleCounts = sample_counts,
872 .framebufferNoAttachmentsSampleCounts = sample_counts,
873 .maxColorAttachments = MAX_RTS,
874 .sampledImageColorSampleCounts = sample_counts,
875 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
876 .sampledImageDepthSampleCounts = sample_counts,
877 .sampledImageStencilSampleCounts = sample_counts,
878 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
879 .maxSampleMaskWords = 1,
880 .timestampComputeAndGraphics = true,
881 .timestampPeriod = 1000000000.0 / 19200000.0, /* CP_ALWAYS_ON_COUNTER is fixed 19.2MHz */
882 .maxClipDistances = 8,
883 .maxCullDistances = 8,
884 .maxCombinedClipAndCullDistances = 8,
885 .discreteQueuePriorities = 1,
886 .pointSizeRange = { 1, 4092 },
887 .lineWidthRange = { 0.0, 7.9921875 },
888 .pointSizeGranularity = 0.0625,
889 .lineWidthGranularity = (1.0 / 128.0),
890 .strictLines = false, /* FINISHME */
891 .standardSampleLocations = true,
892 .optimalBufferCopyOffsetAlignment = 128,
893 .optimalBufferCopyRowPitchAlignment = 128,
894 .nonCoherentAtomSize = 64,
895 };
896
897 *pProperties = (VkPhysicalDeviceProperties) {
898 .apiVersion = tu_physical_device_api_version(pdevice),
899 .driverVersion = vk_get_driver_version(),
900 .vendorID = 0, /* TODO */
901 .deviceID = 0,
902 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
903 .limits = limits,
904 .sparseProperties = { 0 },
905 };
906
907 strcpy(pProperties->deviceName, pdevice->name);
908 memcpy(pProperties->pipelineCacheUUID, pdevice->cache_uuid, VK_UUID_SIZE);
909 }
910
911 void
912 tu_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
913 VkPhysicalDeviceProperties2 *pProperties)
914 {
915 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
916 tu_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
917
918 vk_foreach_struct(ext, pProperties->pNext)
919 {
920 switch (ext->sType) {
921 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
922 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
923 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
924 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
925 break;
926 }
927 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
928 VkPhysicalDeviceIDProperties *properties =
929 (VkPhysicalDeviceIDProperties *) ext;
930 memcpy(properties->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
931 memcpy(properties->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
932 properties->deviceLUIDValid = false;
933 break;
934 }
935 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
936 VkPhysicalDeviceMultiviewProperties *properties =
937 (VkPhysicalDeviceMultiviewProperties *) ext;
938 properties->maxMultiviewViewCount = MAX_VIEWS;
939 properties->maxMultiviewInstanceIndex = INT_MAX;
940 break;
941 }
942 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
943 VkPhysicalDevicePointClippingProperties *properties =
944 (VkPhysicalDevicePointClippingProperties *) ext;
945 properties->pointClippingBehavior =
946 VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
947 break;
948 }
949 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
950 VkPhysicalDeviceMaintenance3Properties *properties =
951 (VkPhysicalDeviceMaintenance3Properties *) ext;
952 /* Make sure everything is addressable by a signed 32-bit int, and
953 * our largest descriptors are 96 bytes. */
954 properties->maxPerSetDescriptors = (1ull << 31) / 96;
955 /* Our buffer size fields allow only this much */
956 properties->maxMemoryAllocationSize = 0xFFFFFFFFull;
957 break;
958 }
959 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
960 VkPhysicalDeviceTransformFeedbackPropertiesEXT *properties =
961 (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
962
963 properties->maxTransformFeedbackStreams = IR3_MAX_SO_STREAMS;
964 properties->maxTransformFeedbackBuffers = IR3_MAX_SO_BUFFERS;
965 properties->maxTransformFeedbackBufferSize = UINT32_MAX;
966 properties->maxTransformFeedbackStreamDataSize = 512;
967 properties->maxTransformFeedbackBufferDataSize = 512;
968 properties->maxTransformFeedbackBufferDataStride = 512;
969 properties->transformFeedbackQueries = true;
970 properties->transformFeedbackStreamsLinesTriangles = false;
971 properties->transformFeedbackRasterizationStreamSelect = false;
972 properties->transformFeedbackDraw = true;
973 break;
974 }
975 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLE_LOCATIONS_PROPERTIES_EXT: {
976 VkPhysicalDeviceSampleLocationsPropertiesEXT *properties =
977 (VkPhysicalDeviceSampleLocationsPropertiesEXT *)ext;
978 properties->sampleLocationSampleCounts = 0;
979 if (pdevice->supported_extensions.EXT_sample_locations) {
980 properties->sampleLocationSampleCounts =
981 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT;
982 }
983 properties->maxSampleLocationGridSize = (VkExtent2D) { 1 , 1 };
984 properties->sampleLocationCoordinateRange[0] = 0.0f;
985 properties->sampleLocationCoordinateRange[1] = 0.9375f;
986 properties->sampleLocationSubPixelBits = 4;
987 properties->variableSampleLocations = true;
988 break;
989 }
990 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES: {
991 VkPhysicalDeviceSamplerFilterMinmaxProperties *properties =
992 (VkPhysicalDeviceSamplerFilterMinmaxProperties *)ext;
993 properties->filterMinmaxImageComponentMapping = true;
994 properties->filterMinmaxSingleComponentFormats = true;
995 break;
996 }
997 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
998 VkPhysicalDeviceSubgroupProperties *properties =
999 (VkPhysicalDeviceSubgroupProperties *)ext;
1000 properties->subgroupSize = 64;
1001 properties->supportedStages = VK_SHADER_STAGE_COMPUTE_BIT;
1002 properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
1003 VK_SUBGROUP_FEATURE_VOTE_BIT;
1004 properties->quadOperationsInAllStages = false;
1005 break;
1006 }
1007 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
1008 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
1009 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
1010 props->maxVertexAttribDivisor = UINT32_MAX;
1011 break;
1012 }
1013 default:
1014 break;
1015 }
1016 }
1017 }
1018
1019 static const VkQueueFamilyProperties tu_queue_family_properties = {
1020 .queueFlags =
1021 VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT,
1022 .queueCount = 1,
1023 .timestampValidBits = 48,
1024 .minImageTransferGranularity = { 1, 1, 1 },
1025 };
1026
1027 void
1028 tu_GetPhysicalDeviceQueueFamilyProperties(
1029 VkPhysicalDevice physicalDevice,
1030 uint32_t *pQueueFamilyPropertyCount,
1031 VkQueueFamilyProperties *pQueueFamilyProperties)
1032 {
1033 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
1034
1035 vk_outarray_append(&out, p) { *p = tu_queue_family_properties; }
1036 }
1037
1038 void
1039 tu_GetPhysicalDeviceQueueFamilyProperties2(
1040 VkPhysicalDevice physicalDevice,
1041 uint32_t *pQueueFamilyPropertyCount,
1042 VkQueueFamilyProperties2 *pQueueFamilyProperties)
1043 {
1044 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
1045
1046 vk_outarray_append(&out, p)
1047 {
1048 p->queueFamilyProperties = tu_queue_family_properties;
1049 }
1050 }
1051
1052 static uint64_t
1053 tu_get_system_heap_size()
1054 {
1055 struct sysinfo info;
1056 sysinfo(&info);
1057
1058 uint64_t total_ram = (uint64_t) info.totalram * (uint64_t) info.mem_unit;
1059
1060 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
1061 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
1062 */
1063 uint64_t available_ram;
1064 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
1065 available_ram = total_ram / 2;
1066 else
1067 available_ram = total_ram * 3 / 4;
1068
1069 return available_ram;
1070 }
1071
1072 void
1073 tu_GetPhysicalDeviceMemoryProperties(
1074 VkPhysicalDevice physicalDevice,
1075 VkPhysicalDeviceMemoryProperties *pMemoryProperties)
1076 {
1077 pMemoryProperties->memoryHeapCount = 1;
1078 pMemoryProperties->memoryHeaps[0].size = tu_get_system_heap_size();
1079 pMemoryProperties->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
1080
1081 pMemoryProperties->memoryTypeCount = 1;
1082 pMemoryProperties->memoryTypes[0].propertyFlags =
1083 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
1084 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
1085 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1086 pMemoryProperties->memoryTypes[0].heapIndex = 0;
1087 }
1088
1089 void
1090 tu_GetPhysicalDeviceMemoryProperties2(
1091 VkPhysicalDevice physicalDevice,
1092 VkPhysicalDeviceMemoryProperties2 *pMemoryProperties)
1093 {
1094 return tu_GetPhysicalDeviceMemoryProperties(
1095 physicalDevice, &pMemoryProperties->memoryProperties);
1096 }
1097
1098 static VkResult
1099 tu_queue_init(struct tu_device *device,
1100 struct tu_queue *queue,
1101 uint32_t queue_family_index,
1102 int idx,
1103 VkDeviceQueueCreateFlags flags)
1104 {
1105 vk_object_base_init(&device->vk, &queue->base, VK_OBJECT_TYPE_QUEUE);
1106
1107 queue->device = device;
1108 queue->queue_family_index = queue_family_index;
1109 queue->queue_idx = idx;
1110 queue->flags = flags;
1111
1112 int ret = tu_drm_submitqueue_new(device, 0, &queue->msm_queue_id);
1113 if (ret)
1114 return VK_ERROR_INITIALIZATION_FAILED;
1115
1116 tu_fence_init(&queue->submit_fence, false);
1117
1118 return VK_SUCCESS;
1119 }
1120
1121 static void
1122 tu_queue_finish(struct tu_queue *queue)
1123 {
1124 tu_fence_finish(&queue->submit_fence);
1125 tu_drm_submitqueue_close(queue->device, queue->msm_queue_id);
1126 }
1127
1128 static int
1129 tu_get_device_extension_index(const char *name)
1130 {
1131 for (unsigned i = 0; i < TU_DEVICE_EXTENSION_COUNT; ++i) {
1132 if (strcmp(name, tu_device_extensions[i].extensionName) == 0)
1133 return i;
1134 }
1135 return -1;
1136 }
1137
1138 struct PACKED bcolor_entry {
1139 uint32_t fp32[4];
1140 uint16_t ui16[4];
1141 int16_t si16[4];
1142 uint16_t fp16[4];
1143 uint16_t rgb565;
1144 uint16_t rgb5a1;
1145 uint16_t rgba4;
1146 uint8_t __pad0[2];
1147 uint8_t ui8[4];
1148 int8_t si8[4];
1149 uint32_t rgb10a2;
1150 uint32_t z24; /* also s8? */
1151 uint16_t srgb[4]; /* appears to duplicate fp16[], but clamped, used for srgb */
1152 uint8_t __pad1[56];
1153 } border_color[] = {
1154 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = {},
1155 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = {},
1156 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = {
1157 .fp32[3] = 0x3f800000,
1158 .ui16[3] = 0xffff,
1159 .si16[3] = 0x7fff,
1160 .fp16[3] = 0x3c00,
1161 .rgb5a1 = 0x8000,
1162 .rgba4 = 0xf000,
1163 .ui8[3] = 0xff,
1164 .si8[3] = 0x7f,
1165 .rgb10a2 = 0xc0000000,
1166 .srgb[3] = 0x3c00,
1167 },
1168 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = {
1169 .fp32[3] = 1,
1170 .fp16[3] = 1,
1171 },
1172 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = {
1173 .fp32[0 ... 3] = 0x3f800000,
1174 .ui16[0 ... 3] = 0xffff,
1175 .si16[0 ... 3] = 0x7fff,
1176 .fp16[0 ... 3] = 0x3c00,
1177 .rgb565 = 0xffff,
1178 .rgb5a1 = 0xffff,
1179 .rgba4 = 0xffff,
1180 .ui8[0 ... 3] = 0xff,
1181 .si8[0 ... 3] = 0x7f,
1182 .rgb10a2 = 0xffffffff,
1183 .z24 = 0xffffff,
1184 .srgb[0 ... 3] = 0x3c00,
1185 },
1186 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = {
1187 .fp32[0 ... 3] = 1,
1188 .fp16[0 ... 3] = 1,
1189 },
1190 };
1191
1192 VkResult
1193 tu_CreateDevice(VkPhysicalDevice physicalDevice,
1194 const VkDeviceCreateInfo *pCreateInfo,
1195 const VkAllocationCallbacks *pAllocator,
1196 VkDevice *pDevice)
1197 {
1198 TU_FROM_HANDLE(tu_physical_device, physical_device, physicalDevice);
1199 VkResult result;
1200 struct tu_device *device;
1201
1202 /* Check enabled features */
1203 if (pCreateInfo->pEnabledFeatures) {
1204 VkPhysicalDeviceFeatures supported_features;
1205 tu_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1206 VkBool32 *supported_feature = (VkBool32 *) &supported_features;
1207 VkBool32 *enabled_feature = (VkBool32 *) pCreateInfo->pEnabledFeatures;
1208 unsigned num_features =
1209 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1210 for (uint32_t i = 0; i < num_features; i++) {
1211 if (enabled_feature[i] && !supported_feature[i])
1212 return vk_error(physical_device->instance,
1213 VK_ERROR_FEATURE_NOT_PRESENT);
1214 }
1215 }
1216
1217 device = vk_zalloc2(&physical_device->instance->alloc, pAllocator,
1218 sizeof(*device), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1219 if (!device)
1220 return vk_error(physical_device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1221
1222 vk_device_init(&device->vk, pCreateInfo,
1223 &physical_device->instance->alloc, pAllocator);
1224
1225 device->instance = physical_device->instance;
1226 device->physical_device = physical_device;
1227 device->_lost = false;
1228
1229 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1230 const char *ext_name = pCreateInfo->ppEnabledExtensionNames[i];
1231 int index = tu_get_device_extension_index(ext_name);
1232 if (index < 0 ||
1233 !physical_device->supported_extensions.extensions[index]) {
1234 vk_free(&device->vk.alloc, device);
1235 return vk_error(physical_device->instance,
1236 VK_ERROR_EXTENSION_NOT_PRESENT);
1237 }
1238
1239 device->enabled_extensions.extensions[index] = true;
1240 }
1241
1242 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1243 const VkDeviceQueueCreateInfo *queue_create =
1244 &pCreateInfo->pQueueCreateInfos[i];
1245 uint32_t qfi = queue_create->queueFamilyIndex;
1246 device->queues[qfi] = vk_alloc(
1247 &device->vk.alloc, queue_create->queueCount * sizeof(struct tu_queue),
1248 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1249 if (!device->queues[qfi]) {
1250 result = VK_ERROR_OUT_OF_HOST_MEMORY;
1251 goto fail_queues;
1252 }
1253
1254 memset(device->queues[qfi], 0,
1255 queue_create->queueCount * sizeof(struct tu_queue));
1256
1257 device->queue_count[qfi] = queue_create->queueCount;
1258
1259 for (unsigned q = 0; q < queue_create->queueCount; q++) {
1260 result = tu_queue_init(device, &device->queues[qfi][q], qfi, q,
1261 queue_create->flags);
1262 if (result != VK_SUCCESS)
1263 goto fail_queues;
1264 }
1265 }
1266
1267 device->compiler = ir3_compiler_create(NULL, physical_device->gpu_id);
1268 if (!device->compiler)
1269 goto fail_queues;
1270
1271 /* initial sizes, these will increase if there is overflow */
1272 device->vsc_draw_strm_pitch = 0x1000 + VSC_PAD;
1273 device->vsc_prim_strm_pitch = 0x4000 + VSC_PAD;
1274
1275 STATIC_ASSERT(sizeof(border_color) == sizeof(((struct tu6_global*) 0)->border_color));
1276 result = tu_bo_init_new(device, &device->global_bo, sizeof(struct tu6_global));
1277 if (result != VK_SUCCESS)
1278 goto fail_global_bo;
1279
1280 result = tu_bo_map(device, &device->global_bo);
1281 if (result != VK_SUCCESS)
1282 goto fail_global_bo_map;
1283
1284 memcpy(device->global_bo.map + gb_offset(border_color), border_color, sizeof(border_color));
1285 tu_init_clear_blit_shaders(device->global_bo.map);
1286
1287 VkPipelineCacheCreateInfo ci;
1288 ci.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
1289 ci.pNext = NULL;
1290 ci.flags = 0;
1291 ci.pInitialData = NULL;
1292 ci.initialDataSize = 0;
1293 VkPipelineCache pc;
1294 result =
1295 tu_CreatePipelineCache(tu_device_to_handle(device), &ci, NULL, &pc);
1296 if (result != VK_SUCCESS)
1297 goto fail_pipeline_cache;
1298
1299 device->mem_cache = tu_pipeline_cache_from_handle(pc);
1300
1301 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++)
1302 mtx_init(&device->scratch_bos[i].construct_mtx, mtx_plain);
1303
1304 mtx_init(&device->vsc_pitch_mtx, mtx_plain);
1305
1306 *pDevice = tu_device_to_handle(device);
1307 return VK_SUCCESS;
1308
1309 fail_pipeline_cache:
1310 fail_global_bo_map:
1311 tu_bo_finish(device, &device->global_bo);
1312
1313 fail_global_bo:
1314 ralloc_free(device->compiler);
1315
1316 fail_queues:
1317 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1318 for (unsigned q = 0; q < device->queue_count[i]; q++)
1319 tu_queue_finish(&device->queues[i][q]);
1320 if (device->queue_count[i])
1321 vk_object_free(&device->vk, NULL, device->queues[i]);
1322 }
1323
1324 vk_free(&device->vk.alloc, device);
1325 return result;
1326 }
1327
1328 void
1329 tu_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
1330 {
1331 TU_FROM_HANDLE(tu_device, device, _device);
1332
1333 if (!device)
1334 return;
1335
1336 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1337 for (unsigned q = 0; q < device->queue_count[i]; q++)
1338 tu_queue_finish(&device->queues[i][q]);
1339 if (device->queue_count[i])
1340 vk_object_free(&device->vk, NULL, device->queues[i]);
1341 }
1342
1343 for (unsigned i = 0; i < ARRAY_SIZE(device->scratch_bos); i++) {
1344 if (device->scratch_bos[i].initialized)
1345 tu_bo_finish(device, &device->scratch_bos[i].bo);
1346 }
1347
1348 ir3_compiler_destroy(device->compiler);
1349
1350 VkPipelineCache pc = tu_pipeline_cache_to_handle(device->mem_cache);
1351 tu_DestroyPipelineCache(tu_device_to_handle(device), pc, NULL);
1352
1353 vk_free(&device->vk.alloc, device);
1354 }
1355
1356 VkResult
1357 _tu_device_set_lost(struct tu_device *device,
1358 const char *file, int line,
1359 const char *msg, ...)
1360 {
1361 /* Set the flag indicating that waits should return in finite time even
1362 * after device loss.
1363 */
1364 p_atomic_inc(&device->_lost);
1365
1366 /* TODO: Report the log message through VkDebugReportCallbackEXT instead */
1367 fprintf(stderr, "%s:%d: ", file, line);
1368 va_list ap;
1369 va_start(ap, msg);
1370 vfprintf(stderr, msg, ap);
1371 va_end(ap);
1372
1373 if (env_var_as_boolean("TU_ABORT_ON_DEVICE_LOSS", false))
1374 abort();
1375
1376 return VK_ERROR_DEVICE_LOST;
1377 }
1378
1379 VkResult
1380 tu_get_scratch_bo(struct tu_device *dev, uint64_t size, struct tu_bo **bo)
1381 {
1382 unsigned size_log2 = MAX2(util_logbase2_ceil64(size), MIN_SCRATCH_BO_SIZE_LOG2);
1383 unsigned index = size_log2 - MIN_SCRATCH_BO_SIZE_LOG2;
1384 assert(index < ARRAY_SIZE(dev->scratch_bos));
1385
1386 for (unsigned i = index; i < ARRAY_SIZE(dev->scratch_bos); i++) {
1387 if (p_atomic_read(&dev->scratch_bos[i].initialized)) {
1388 /* Fast path: just return the already-allocated BO. */
1389 *bo = &dev->scratch_bos[i].bo;
1390 return VK_SUCCESS;
1391 }
1392 }
1393
1394 /* Slow path: actually allocate the BO. We take a lock because the process
1395 * of allocating it is slow, and we don't want to block the CPU while it
1396 * finishes.
1397 */
1398 mtx_lock(&dev->scratch_bos[index].construct_mtx);
1399
1400 /* Another thread may have allocated it already while we were waiting on
1401 * the lock. We need to check this in order to avoid double-allocating.
1402 */
1403 if (dev->scratch_bos[index].initialized) {
1404 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1405 *bo = &dev->scratch_bos[index].bo;
1406 return VK_SUCCESS;
1407 }
1408
1409 unsigned bo_size = 1ull << size_log2;
1410 VkResult result = tu_bo_init_new(dev, &dev->scratch_bos[index].bo, bo_size);
1411 if (result != VK_SUCCESS) {
1412 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1413 return result;
1414 }
1415
1416 p_atomic_set(&dev->scratch_bos[index].initialized, true);
1417
1418 mtx_unlock(&dev->scratch_bos[index].construct_mtx);
1419
1420 *bo = &dev->scratch_bos[index].bo;
1421 return VK_SUCCESS;
1422 }
1423
1424 VkResult
1425 tu_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
1426 VkLayerProperties *pProperties)
1427 {
1428 *pPropertyCount = 0;
1429 return VK_SUCCESS;
1430 }
1431
1432 VkResult
1433 tu_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1434 uint32_t *pPropertyCount,
1435 VkLayerProperties *pProperties)
1436 {
1437 *pPropertyCount = 0;
1438 return VK_SUCCESS;
1439 }
1440
1441 void
1442 tu_GetDeviceQueue2(VkDevice _device,
1443 const VkDeviceQueueInfo2 *pQueueInfo,
1444 VkQueue *pQueue)
1445 {
1446 TU_FROM_HANDLE(tu_device, device, _device);
1447 struct tu_queue *queue;
1448
1449 queue =
1450 &device->queues[pQueueInfo->queueFamilyIndex][pQueueInfo->queueIndex];
1451 if (pQueueInfo->flags != queue->flags) {
1452 /* From the Vulkan 1.1.70 spec:
1453 *
1454 * "The queue returned by vkGetDeviceQueue2 must have the same
1455 * flags value from this structure as that used at device
1456 * creation time in a VkDeviceQueueCreateInfo instance. If no
1457 * matching flags were specified at device creation time then
1458 * pQueue will return VK_NULL_HANDLE."
1459 */
1460 *pQueue = VK_NULL_HANDLE;
1461 return;
1462 }
1463
1464 *pQueue = tu_queue_to_handle(queue);
1465 }
1466
1467 void
1468 tu_GetDeviceQueue(VkDevice _device,
1469 uint32_t queueFamilyIndex,
1470 uint32_t queueIndex,
1471 VkQueue *pQueue)
1472 {
1473 const VkDeviceQueueInfo2 info =
1474 (VkDeviceQueueInfo2) { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
1475 .queueFamilyIndex = queueFamilyIndex,
1476 .queueIndex = queueIndex };
1477
1478 tu_GetDeviceQueue2(_device, &info, pQueue);
1479 }
1480
1481 static VkResult
1482 tu_get_semaphore_syncobjs(const VkSemaphore *sems,
1483 uint32_t sem_count,
1484 bool wait,
1485 struct drm_msm_gem_submit_syncobj **out,
1486 uint32_t *out_count)
1487 {
1488 uint32_t syncobj_count = 0;
1489 struct drm_msm_gem_submit_syncobj *syncobjs;
1490
1491 for (uint32_t i = 0; i < sem_count; ++i) {
1492 TU_FROM_HANDLE(tu_semaphore, sem, sems[i]);
1493
1494 struct tu_semaphore_part *part =
1495 sem->temporary.kind != TU_SEMAPHORE_NONE ?
1496 &sem->temporary : &sem->permanent;
1497
1498 if (part->kind == TU_SEMAPHORE_SYNCOBJ)
1499 ++syncobj_count;
1500 }
1501
1502 *out = NULL;
1503 *out_count = syncobj_count;
1504 if (!syncobj_count)
1505 return VK_SUCCESS;
1506
1507 *out = syncobjs = calloc(syncobj_count, sizeof (*syncobjs));
1508 if (!syncobjs)
1509 return VK_ERROR_OUT_OF_HOST_MEMORY;
1510
1511 for (uint32_t i = 0, j = 0; i < sem_count; ++i) {
1512 TU_FROM_HANDLE(tu_semaphore, sem, sems[i]);
1513
1514 struct tu_semaphore_part *part =
1515 sem->temporary.kind != TU_SEMAPHORE_NONE ?
1516 &sem->temporary : &sem->permanent;
1517
1518 if (part->kind == TU_SEMAPHORE_SYNCOBJ) {
1519 syncobjs[j].handle = part->syncobj;
1520 syncobjs[j].flags = wait ? MSM_SUBMIT_SYNCOBJ_RESET : 0;
1521 ++j;
1522 }
1523 }
1524
1525 return VK_SUCCESS;
1526 }
1527
1528
1529 static void
1530 tu_semaphores_remove_temp(struct tu_device *device,
1531 const VkSemaphore *sems,
1532 uint32_t sem_count)
1533 {
1534 for (uint32_t i = 0; i < sem_count; ++i) {
1535 TU_FROM_HANDLE(tu_semaphore, sem, sems[i]);
1536 tu_semaphore_remove_temp(device, sem);
1537 }
1538 }
1539
1540 VkResult
1541 tu_QueueSubmit(VkQueue _queue,
1542 uint32_t submitCount,
1543 const VkSubmitInfo *pSubmits,
1544 VkFence _fence)
1545 {
1546 TU_FROM_HANDLE(tu_queue, queue, _queue);
1547 VkResult result;
1548
1549 for (uint32_t i = 0; i < submitCount; ++i) {
1550 const VkSubmitInfo *submit = pSubmits + i;
1551 const bool last_submit = (i == submitCount - 1);
1552 struct drm_msm_gem_submit_syncobj *in_syncobjs = NULL, *out_syncobjs = NULL;
1553 uint32_t nr_in_syncobjs, nr_out_syncobjs;
1554 struct tu_bo_list bo_list;
1555 tu_bo_list_init(&bo_list);
1556
1557 result = tu_get_semaphore_syncobjs(pSubmits[i].pWaitSemaphores,
1558 pSubmits[i].waitSemaphoreCount,
1559 false, &in_syncobjs, &nr_in_syncobjs);
1560 if (result != VK_SUCCESS) {
1561 return tu_device_set_lost(queue->device,
1562 "failed to allocate space for semaphore submission\n");
1563 }
1564
1565 result = tu_get_semaphore_syncobjs(pSubmits[i].pSignalSemaphores,
1566 pSubmits[i].signalSemaphoreCount,
1567 false, &out_syncobjs, &nr_out_syncobjs);
1568 if (result != VK_SUCCESS) {
1569 free(in_syncobjs);
1570 return tu_device_set_lost(queue->device,
1571 "failed to allocate space for semaphore submission\n");
1572 }
1573
1574 uint32_t entry_count = 0;
1575 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1576 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1577 entry_count += cmdbuf->cs.entry_count;
1578 }
1579
1580 struct drm_msm_gem_submit_cmd cmds[entry_count];
1581 uint32_t entry_idx = 0;
1582 for (uint32_t j = 0; j < submit->commandBufferCount; ++j) {
1583 TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, submit->pCommandBuffers[j]);
1584 struct tu_cs *cs = &cmdbuf->cs;
1585 for (unsigned i = 0; i < cs->entry_count; ++i, ++entry_idx) {
1586 cmds[entry_idx].type = MSM_SUBMIT_CMD_BUF;
1587 cmds[entry_idx].submit_idx =
1588 tu_bo_list_add(&bo_list, cs->entries[i].bo,
1589 MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_DUMP);
1590 cmds[entry_idx].submit_offset = cs->entries[i].offset;
1591 cmds[entry_idx].size = cs->entries[i].size;
1592 cmds[entry_idx].pad = 0;
1593 cmds[entry_idx].nr_relocs = 0;
1594 cmds[entry_idx].relocs = 0;
1595 }
1596
1597 tu_bo_list_merge(&bo_list, &cmdbuf->bo_list);
1598 }
1599
1600 uint32_t flags = MSM_PIPE_3D0;
1601 if (nr_in_syncobjs) {
1602 flags |= MSM_SUBMIT_SYNCOBJ_IN;
1603 }
1604 if (nr_out_syncobjs) {
1605 flags |= MSM_SUBMIT_SYNCOBJ_OUT;
1606 }
1607
1608 if (last_submit) {
1609 flags |= MSM_SUBMIT_FENCE_FD_OUT;
1610 }
1611
1612 struct drm_msm_gem_submit req = {
1613 .flags = flags,
1614 .queueid = queue->msm_queue_id,
1615 .bos = (uint64_t)(uintptr_t) bo_list.bo_infos,
1616 .nr_bos = bo_list.count,
1617 .cmds = (uint64_t)(uintptr_t)cmds,
1618 .nr_cmds = entry_count,
1619 .in_syncobjs = (uint64_t)(uintptr_t)in_syncobjs,
1620 .out_syncobjs = (uint64_t)(uintptr_t)out_syncobjs,
1621 .nr_in_syncobjs = nr_in_syncobjs,
1622 .nr_out_syncobjs = nr_out_syncobjs,
1623 .syncobj_stride = sizeof(struct drm_msm_gem_submit_syncobj),
1624 };
1625
1626 int ret = drmCommandWriteRead(queue->device->physical_device->local_fd,
1627 DRM_MSM_GEM_SUBMIT,
1628 &req, sizeof(req));
1629 if (ret) {
1630 free(in_syncobjs);
1631 free(out_syncobjs);
1632 return tu_device_set_lost(queue->device, "submit failed: %s\n",
1633 strerror(errno));
1634 }
1635
1636 tu_bo_list_destroy(&bo_list);
1637 free(in_syncobjs);
1638 free(out_syncobjs);
1639
1640 tu_semaphores_remove_temp(queue->device, pSubmits[i].pWaitSemaphores,
1641 pSubmits[i].waitSemaphoreCount);
1642 if (last_submit) {
1643 /* no need to merge fences as queue execution is serialized */
1644 tu_fence_update_fd(&queue->submit_fence, req.fence_fd);
1645 } else if (last_submit) {
1646 close(req.fence_fd);
1647 }
1648 }
1649
1650 if (_fence != VK_NULL_HANDLE) {
1651 TU_FROM_HANDLE(tu_fence, fence, _fence);
1652 tu_fence_copy(fence, &queue->submit_fence);
1653 }
1654
1655 return VK_SUCCESS;
1656 }
1657
1658 VkResult
1659 tu_QueueWaitIdle(VkQueue _queue)
1660 {
1661 TU_FROM_HANDLE(tu_queue, queue, _queue);
1662
1663 if (tu_device_is_lost(queue->device))
1664 return VK_ERROR_DEVICE_LOST;
1665
1666 tu_fence_wait_idle(&queue->submit_fence);
1667
1668 return VK_SUCCESS;
1669 }
1670
1671 VkResult
1672 tu_DeviceWaitIdle(VkDevice _device)
1673 {
1674 TU_FROM_HANDLE(tu_device, device, _device);
1675
1676 if (tu_device_is_lost(device))
1677 return VK_ERROR_DEVICE_LOST;
1678
1679 for (unsigned i = 0; i < TU_MAX_QUEUE_FAMILIES; i++) {
1680 for (unsigned q = 0; q < device->queue_count[i]; q++) {
1681 tu_QueueWaitIdle(tu_queue_to_handle(&device->queues[i][q]));
1682 }
1683 }
1684 return VK_SUCCESS;
1685 }
1686
1687 VkResult
1688 tu_EnumerateInstanceExtensionProperties(const char *pLayerName,
1689 uint32_t *pPropertyCount,
1690 VkExtensionProperties *pProperties)
1691 {
1692 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1693
1694 /* We spport no lyaers */
1695 if (pLayerName)
1696 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1697
1698 for (int i = 0; i < TU_INSTANCE_EXTENSION_COUNT; i++) {
1699 if (tu_instance_extensions_supported.extensions[i]) {
1700 vk_outarray_append(&out, prop) { *prop = tu_instance_extensions[i]; }
1701 }
1702 }
1703
1704 return vk_outarray_status(&out);
1705 }
1706
1707 VkResult
1708 tu_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
1709 const char *pLayerName,
1710 uint32_t *pPropertyCount,
1711 VkExtensionProperties *pProperties)
1712 {
1713 /* We spport no lyaers */
1714 TU_FROM_HANDLE(tu_physical_device, device, physicalDevice);
1715 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1716
1717 /* We spport no lyaers */
1718 if (pLayerName)
1719 return vk_error(NULL, VK_ERROR_LAYER_NOT_PRESENT);
1720
1721 for (int i = 0; i < TU_DEVICE_EXTENSION_COUNT; i++) {
1722 if (device->supported_extensions.extensions[i]) {
1723 vk_outarray_append(&out, prop) { *prop = tu_device_extensions[i]; }
1724 }
1725 }
1726
1727 return vk_outarray_status(&out);
1728 }
1729
1730 PFN_vkVoidFunction
1731 tu_GetInstanceProcAddr(VkInstance _instance, const char *pName)
1732 {
1733 TU_FROM_HANDLE(tu_instance, instance, _instance);
1734
1735 return tu_lookup_entrypoint_checked(
1736 pName, instance ? instance->api_version : 0,
1737 instance ? &instance->enabled_extensions : NULL, NULL);
1738 }
1739
1740 /* The loader wants us to expose a second GetInstanceProcAddr function
1741 * to work around certain LD_PRELOAD issues seen in apps.
1742 */
1743 PUBLIC
1744 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1745 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName);
1746
1747 PUBLIC
1748 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
1749 vk_icdGetInstanceProcAddr(VkInstance instance, const char *pName)
1750 {
1751 return tu_GetInstanceProcAddr(instance, pName);
1752 }
1753
1754 PFN_vkVoidFunction
1755 tu_GetDeviceProcAddr(VkDevice _device, const char *pName)
1756 {
1757 TU_FROM_HANDLE(tu_device, device, _device);
1758
1759 return tu_lookup_entrypoint_checked(pName, device->instance->api_version,
1760 &device->instance->enabled_extensions,
1761 &device->enabled_extensions);
1762 }
1763
1764 static VkResult
1765 tu_alloc_memory(struct tu_device *device,
1766 const VkMemoryAllocateInfo *pAllocateInfo,
1767 const VkAllocationCallbacks *pAllocator,
1768 VkDeviceMemory *pMem)
1769 {
1770 struct tu_device_memory *mem;
1771 VkResult result;
1772
1773 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1774
1775 if (pAllocateInfo->allocationSize == 0) {
1776 /* Apparently, this is allowed */
1777 *pMem = VK_NULL_HANDLE;
1778 return VK_SUCCESS;
1779 }
1780
1781 mem = vk_object_alloc(&device->vk, pAllocator, sizeof(*mem),
1782 VK_OBJECT_TYPE_DEVICE_MEMORY);
1783 if (mem == NULL)
1784 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1785
1786 const VkImportMemoryFdInfoKHR *fd_info =
1787 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
1788 if (fd_info && !fd_info->handleType)
1789 fd_info = NULL;
1790
1791 if (fd_info) {
1792 assert(fd_info->handleType ==
1793 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
1794 fd_info->handleType ==
1795 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1796
1797 /*
1798 * TODO Importing the same fd twice gives us the same handle without
1799 * reference counting. We need to maintain a per-instance handle-to-bo
1800 * table and add reference count to tu_bo.
1801 */
1802 result = tu_bo_init_dmabuf(device, &mem->bo,
1803 pAllocateInfo->allocationSize, fd_info->fd);
1804 if (result == VK_SUCCESS) {
1805 /* take ownership and close the fd */
1806 close(fd_info->fd);
1807 }
1808 } else {
1809 result =
1810 tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize);
1811 }
1812
1813 if (result != VK_SUCCESS) {
1814 vk_object_free(&device->vk, pAllocator, mem);
1815 return result;
1816 }
1817
1818 mem->size = pAllocateInfo->allocationSize;
1819 mem->type_index = pAllocateInfo->memoryTypeIndex;
1820
1821 mem->map = NULL;
1822 mem->user_ptr = NULL;
1823
1824 *pMem = tu_device_memory_to_handle(mem);
1825
1826 return VK_SUCCESS;
1827 }
1828
1829 VkResult
1830 tu_AllocateMemory(VkDevice _device,
1831 const VkMemoryAllocateInfo *pAllocateInfo,
1832 const VkAllocationCallbacks *pAllocator,
1833 VkDeviceMemory *pMem)
1834 {
1835 TU_FROM_HANDLE(tu_device, device, _device);
1836 return tu_alloc_memory(device, pAllocateInfo, pAllocator, pMem);
1837 }
1838
1839 void
1840 tu_FreeMemory(VkDevice _device,
1841 VkDeviceMemory _mem,
1842 const VkAllocationCallbacks *pAllocator)
1843 {
1844 TU_FROM_HANDLE(tu_device, device, _device);
1845 TU_FROM_HANDLE(tu_device_memory, mem, _mem);
1846
1847 if (mem == NULL)
1848 return;
1849
1850 tu_bo_finish(device, &mem->bo);
1851 vk_object_free(&device->vk, pAllocator, mem);
1852 }
1853
1854 VkResult
1855 tu_MapMemory(VkDevice _device,
1856 VkDeviceMemory _memory,
1857 VkDeviceSize offset,
1858 VkDeviceSize size,
1859 VkMemoryMapFlags flags,
1860 void **ppData)
1861 {
1862 TU_FROM_HANDLE(tu_device, device, _device);
1863 TU_FROM_HANDLE(tu_device_memory, mem, _memory);
1864 VkResult result;
1865
1866 if (mem == NULL) {
1867 *ppData = NULL;
1868 return VK_SUCCESS;
1869 }
1870
1871 if (mem->user_ptr) {
1872 *ppData = mem->user_ptr;
1873 } else if (!mem->map) {
1874 result = tu_bo_map(device, &mem->bo);
1875 if (result != VK_SUCCESS)
1876 return result;
1877 *ppData = mem->map = mem->bo.map;
1878 } else
1879 *ppData = mem->map;
1880
1881 if (*ppData) {
1882 *ppData += offset;
1883 return VK_SUCCESS;
1884 }
1885
1886 return vk_error(device->instance, VK_ERROR_MEMORY_MAP_FAILED);
1887 }
1888
1889 void
1890 tu_UnmapMemory(VkDevice _device, VkDeviceMemory _memory)
1891 {
1892 /* I do not see any unmapping done by the freedreno Gallium driver. */
1893 }
1894
1895 VkResult
1896 tu_FlushMappedMemoryRanges(VkDevice _device,
1897 uint32_t memoryRangeCount,
1898 const VkMappedMemoryRange *pMemoryRanges)
1899 {
1900 return VK_SUCCESS;
1901 }
1902
1903 VkResult
1904 tu_InvalidateMappedMemoryRanges(VkDevice _device,
1905 uint32_t memoryRangeCount,
1906 const VkMappedMemoryRange *pMemoryRanges)
1907 {
1908 return VK_SUCCESS;
1909 }
1910
1911 void
1912 tu_GetBufferMemoryRequirements(VkDevice _device,
1913 VkBuffer _buffer,
1914 VkMemoryRequirements *pMemoryRequirements)
1915 {
1916 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
1917
1918 pMemoryRequirements->memoryTypeBits = 1;
1919 pMemoryRequirements->alignment = 64;
1920 pMemoryRequirements->size =
1921 align64(buffer->size, pMemoryRequirements->alignment);
1922 }
1923
1924 void
1925 tu_GetBufferMemoryRequirements2(
1926 VkDevice device,
1927 const VkBufferMemoryRequirementsInfo2 *pInfo,
1928 VkMemoryRequirements2 *pMemoryRequirements)
1929 {
1930 tu_GetBufferMemoryRequirements(device, pInfo->buffer,
1931 &pMemoryRequirements->memoryRequirements);
1932 }
1933
1934 void
1935 tu_GetImageMemoryRequirements(VkDevice _device,
1936 VkImage _image,
1937 VkMemoryRequirements *pMemoryRequirements)
1938 {
1939 TU_FROM_HANDLE(tu_image, image, _image);
1940
1941 pMemoryRequirements->memoryTypeBits = 1;
1942 pMemoryRequirements->size = image->total_size;
1943 pMemoryRequirements->alignment = image->layout[0].base_align;
1944 }
1945
1946 void
1947 tu_GetImageMemoryRequirements2(VkDevice device,
1948 const VkImageMemoryRequirementsInfo2 *pInfo,
1949 VkMemoryRequirements2 *pMemoryRequirements)
1950 {
1951 tu_GetImageMemoryRequirements(device, pInfo->image,
1952 &pMemoryRequirements->memoryRequirements);
1953 }
1954
1955 void
1956 tu_GetImageSparseMemoryRequirements(
1957 VkDevice device,
1958 VkImage image,
1959 uint32_t *pSparseMemoryRequirementCount,
1960 VkSparseImageMemoryRequirements *pSparseMemoryRequirements)
1961 {
1962 tu_stub();
1963 }
1964
1965 void
1966 tu_GetImageSparseMemoryRequirements2(
1967 VkDevice device,
1968 const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1969 uint32_t *pSparseMemoryRequirementCount,
1970 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1971 {
1972 tu_stub();
1973 }
1974
1975 void
1976 tu_GetDeviceMemoryCommitment(VkDevice device,
1977 VkDeviceMemory memory,
1978 VkDeviceSize *pCommittedMemoryInBytes)
1979 {
1980 *pCommittedMemoryInBytes = 0;
1981 }
1982
1983 VkResult
1984 tu_BindBufferMemory2(VkDevice device,
1985 uint32_t bindInfoCount,
1986 const VkBindBufferMemoryInfo *pBindInfos)
1987 {
1988 for (uint32_t i = 0; i < bindInfoCount; ++i) {
1989 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
1990 TU_FROM_HANDLE(tu_buffer, buffer, pBindInfos[i].buffer);
1991
1992 if (mem) {
1993 buffer->bo = &mem->bo;
1994 buffer->bo_offset = pBindInfos[i].memoryOffset;
1995 } else {
1996 buffer->bo = NULL;
1997 }
1998 }
1999 return VK_SUCCESS;
2000 }
2001
2002 VkResult
2003 tu_BindBufferMemory(VkDevice device,
2004 VkBuffer buffer,
2005 VkDeviceMemory memory,
2006 VkDeviceSize memoryOffset)
2007 {
2008 const VkBindBufferMemoryInfo info = {
2009 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
2010 .buffer = buffer,
2011 .memory = memory,
2012 .memoryOffset = memoryOffset
2013 };
2014
2015 return tu_BindBufferMemory2(device, 1, &info);
2016 }
2017
2018 VkResult
2019 tu_BindImageMemory2(VkDevice device,
2020 uint32_t bindInfoCount,
2021 const VkBindImageMemoryInfo *pBindInfos)
2022 {
2023 for (uint32_t i = 0; i < bindInfoCount; ++i) {
2024 TU_FROM_HANDLE(tu_image, image, pBindInfos[i].image);
2025 TU_FROM_HANDLE(tu_device_memory, mem, pBindInfos[i].memory);
2026
2027 if (mem) {
2028 image->bo = &mem->bo;
2029 image->bo_offset = pBindInfos[i].memoryOffset;
2030 } else {
2031 image->bo = NULL;
2032 image->bo_offset = 0;
2033 }
2034 }
2035
2036 return VK_SUCCESS;
2037 }
2038
2039 VkResult
2040 tu_BindImageMemory(VkDevice device,
2041 VkImage image,
2042 VkDeviceMemory memory,
2043 VkDeviceSize memoryOffset)
2044 {
2045 const VkBindImageMemoryInfo info = {
2046 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
2047 .image = image,
2048 .memory = memory,
2049 .memoryOffset = memoryOffset
2050 };
2051
2052 return tu_BindImageMemory2(device, 1, &info);
2053 }
2054
2055 VkResult
2056 tu_QueueBindSparse(VkQueue _queue,
2057 uint32_t bindInfoCount,
2058 const VkBindSparseInfo *pBindInfo,
2059 VkFence _fence)
2060 {
2061 return VK_SUCCESS;
2062 }
2063
2064 // Queue semaphore functions
2065
2066
2067 static void
2068 tu_semaphore_part_destroy(struct tu_device *device,
2069 struct tu_semaphore_part *part)
2070 {
2071 switch(part->kind) {
2072 case TU_SEMAPHORE_NONE:
2073 break;
2074 case TU_SEMAPHORE_SYNCOBJ:
2075 drmSyncobjDestroy(device->physical_device->local_fd, part->syncobj);
2076 break;
2077 }
2078 part->kind = TU_SEMAPHORE_NONE;
2079 }
2080
2081 static void
2082 tu_semaphore_remove_temp(struct tu_device *device,
2083 struct tu_semaphore *sem)
2084 {
2085 if (sem->temporary.kind != TU_SEMAPHORE_NONE) {
2086 tu_semaphore_part_destroy(device, &sem->temporary);
2087 }
2088 }
2089
2090 VkResult
2091 tu_CreateSemaphore(VkDevice _device,
2092 const VkSemaphoreCreateInfo *pCreateInfo,
2093 const VkAllocationCallbacks *pAllocator,
2094 VkSemaphore *pSemaphore)
2095 {
2096 TU_FROM_HANDLE(tu_device, device, _device);
2097
2098 struct tu_semaphore *sem =
2099 vk_object_alloc(&device->vk, pAllocator, sizeof(*sem),
2100 VK_OBJECT_TYPE_SEMAPHORE);
2101 if (!sem)
2102 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2103
2104 const VkExportSemaphoreCreateInfo *export =
2105 vk_find_struct_const(pCreateInfo->pNext, EXPORT_SEMAPHORE_CREATE_INFO);
2106 VkExternalSemaphoreHandleTypeFlags handleTypes =
2107 export ? export->handleTypes : 0;
2108
2109 sem->permanent.kind = TU_SEMAPHORE_NONE;
2110 sem->temporary.kind = TU_SEMAPHORE_NONE;
2111
2112 if (handleTypes) {
2113 if (drmSyncobjCreate(device->physical_device->local_fd, 0, &sem->permanent.syncobj) < 0) {
2114 vk_free2(&device->vk.alloc, pAllocator, sem);
2115 return VK_ERROR_OUT_OF_HOST_MEMORY;
2116 }
2117 sem->permanent.kind = TU_SEMAPHORE_SYNCOBJ;
2118 }
2119 *pSemaphore = tu_semaphore_to_handle(sem);
2120 return VK_SUCCESS;
2121 }
2122
2123 void
2124 tu_DestroySemaphore(VkDevice _device,
2125 VkSemaphore _semaphore,
2126 const VkAllocationCallbacks *pAllocator)
2127 {
2128 TU_FROM_HANDLE(tu_device, device, _device);
2129 TU_FROM_HANDLE(tu_semaphore, sem, _semaphore);
2130 if (!_semaphore)
2131 return;
2132
2133 tu_semaphore_part_destroy(device, &sem->permanent);
2134 tu_semaphore_part_destroy(device, &sem->temporary);
2135
2136 vk_object_free(&device->vk, pAllocator, sem);
2137 }
2138
2139 VkResult
2140 tu_CreateEvent(VkDevice _device,
2141 const VkEventCreateInfo *pCreateInfo,
2142 const VkAllocationCallbacks *pAllocator,
2143 VkEvent *pEvent)
2144 {
2145 TU_FROM_HANDLE(tu_device, device, _device);
2146
2147 struct tu_event *event =
2148 vk_object_alloc(&device->vk, pAllocator, sizeof(*event),
2149 VK_OBJECT_TYPE_EVENT);
2150 if (!event)
2151 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2152
2153 VkResult result = tu_bo_init_new(device, &event->bo, 0x1000);
2154 if (result != VK_SUCCESS)
2155 goto fail_alloc;
2156
2157 result = tu_bo_map(device, &event->bo);
2158 if (result != VK_SUCCESS)
2159 goto fail_map;
2160
2161 *pEvent = tu_event_to_handle(event);
2162
2163 return VK_SUCCESS;
2164
2165 fail_map:
2166 tu_bo_finish(device, &event->bo);
2167 fail_alloc:
2168 vk_object_free(&device->vk, pAllocator, event);
2169 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2170 }
2171
2172 void
2173 tu_DestroyEvent(VkDevice _device,
2174 VkEvent _event,
2175 const VkAllocationCallbacks *pAllocator)
2176 {
2177 TU_FROM_HANDLE(tu_device, device, _device);
2178 TU_FROM_HANDLE(tu_event, event, _event);
2179
2180 if (!event)
2181 return;
2182
2183 tu_bo_finish(device, &event->bo);
2184 vk_object_free(&device->vk, pAllocator, event);
2185 }
2186
2187 VkResult
2188 tu_GetEventStatus(VkDevice _device, VkEvent _event)
2189 {
2190 TU_FROM_HANDLE(tu_event, event, _event);
2191
2192 if (*(uint64_t*) event->bo.map == 1)
2193 return VK_EVENT_SET;
2194 return VK_EVENT_RESET;
2195 }
2196
2197 VkResult
2198 tu_SetEvent(VkDevice _device, VkEvent _event)
2199 {
2200 TU_FROM_HANDLE(tu_event, event, _event);
2201 *(uint64_t*) event->bo.map = 1;
2202
2203 return VK_SUCCESS;
2204 }
2205
2206 VkResult
2207 tu_ResetEvent(VkDevice _device, VkEvent _event)
2208 {
2209 TU_FROM_HANDLE(tu_event, event, _event);
2210 *(uint64_t*) event->bo.map = 0;
2211
2212 return VK_SUCCESS;
2213 }
2214
2215 VkResult
2216 tu_CreateBuffer(VkDevice _device,
2217 const VkBufferCreateInfo *pCreateInfo,
2218 const VkAllocationCallbacks *pAllocator,
2219 VkBuffer *pBuffer)
2220 {
2221 TU_FROM_HANDLE(tu_device, device, _device);
2222 struct tu_buffer *buffer;
2223
2224 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2225
2226 buffer = vk_object_alloc(&device->vk, pAllocator, sizeof(*buffer),
2227 VK_OBJECT_TYPE_BUFFER);
2228 if (buffer == NULL)
2229 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2230
2231 buffer->size = pCreateInfo->size;
2232 buffer->usage = pCreateInfo->usage;
2233 buffer->flags = pCreateInfo->flags;
2234
2235 *pBuffer = tu_buffer_to_handle(buffer);
2236
2237 return VK_SUCCESS;
2238 }
2239
2240 void
2241 tu_DestroyBuffer(VkDevice _device,
2242 VkBuffer _buffer,
2243 const VkAllocationCallbacks *pAllocator)
2244 {
2245 TU_FROM_HANDLE(tu_device, device, _device);
2246 TU_FROM_HANDLE(tu_buffer, buffer, _buffer);
2247
2248 if (!buffer)
2249 return;
2250
2251 vk_object_free(&device->vk, pAllocator, buffer);
2252 }
2253
2254 VkResult
2255 tu_CreateFramebuffer(VkDevice _device,
2256 const VkFramebufferCreateInfo *pCreateInfo,
2257 const VkAllocationCallbacks *pAllocator,
2258 VkFramebuffer *pFramebuffer)
2259 {
2260 TU_FROM_HANDLE(tu_device, device, _device);
2261 TU_FROM_HANDLE(tu_render_pass, pass, pCreateInfo->renderPass);
2262 struct tu_framebuffer *framebuffer;
2263
2264 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2265
2266 size_t size = sizeof(*framebuffer) + sizeof(struct tu_attachment_info) *
2267 pCreateInfo->attachmentCount;
2268 framebuffer = vk_object_alloc(&device->vk, pAllocator, size,
2269 VK_OBJECT_TYPE_FRAMEBUFFER);
2270 if (framebuffer == NULL)
2271 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2272
2273 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2274 framebuffer->width = pCreateInfo->width;
2275 framebuffer->height = pCreateInfo->height;
2276 framebuffer->layers = pCreateInfo->layers;
2277 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2278 VkImageView _iview = pCreateInfo->pAttachments[i];
2279 struct tu_image_view *iview = tu_image_view_from_handle(_iview);
2280 framebuffer->attachments[i].attachment = iview;
2281 }
2282
2283 tu_framebuffer_tiling_config(framebuffer, device, pass);
2284
2285 *pFramebuffer = tu_framebuffer_to_handle(framebuffer);
2286 return VK_SUCCESS;
2287 }
2288
2289 void
2290 tu_DestroyFramebuffer(VkDevice _device,
2291 VkFramebuffer _fb,
2292 const VkAllocationCallbacks *pAllocator)
2293 {
2294 TU_FROM_HANDLE(tu_device, device, _device);
2295 TU_FROM_HANDLE(tu_framebuffer, fb, _fb);
2296
2297 if (!fb)
2298 return;
2299
2300 vk_object_free(&device->vk, pAllocator, fb);
2301 }
2302
2303 static void
2304 tu_init_sampler(struct tu_device *device,
2305 struct tu_sampler *sampler,
2306 const VkSamplerCreateInfo *pCreateInfo)
2307 {
2308 const struct VkSamplerReductionModeCreateInfo *reduction =
2309 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_REDUCTION_MODE_CREATE_INFO);
2310 const struct VkSamplerYcbcrConversionInfo *ycbcr_conversion =
2311 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_YCBCR_CONVERSION_INFO);
2312
2313 unsigned aniso = pCreateInfo->anisotropyEnable ?
2314 util_last_bit(MIN2((uint32_t)pCreateInfo->maxAnisotropy >> 1, 8)) : 0;
2315 bool miplinear = (pCreateInfo->mipmapMode == VK_SAMPLER_MIPMAP_MODE_LINEAR);
2316 float min_lod = CLAMP(pCreateInfo->minLod, 0.0f, 4095.0f / 256.0f);
2317 float max_lod = CLAMP(pCreateInfo->maxLod, 0.0f, 4095.0f / 256.0f);
2318
2319 sampler->descriptor[0] =
2320 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
2321 A6XX_TEX_SAMP_0_XY_MAG(tu6_tex_filter(pCreateInfo->magFilter, aniso)) |
2322 A6XX_TEX_SAMP_0_XY_MIN(tu6_tex_filter(pCreateInfo->minFilter, aniso)) |
2323 A6XX_TEX_SAMP_0_ANISO(aniso) |
2324 A6XX_TEX_SAMP_0_WRAP_S(tu6_tex_wrap(pCreateInfo->addressModeU)) |
2325 A6XX_TEX_SAMP_0_WRAP_T(tu6_tex_wrap(pCreateInfo->addressModeV)) |
2326 A6XX_TEX_SAMP_0_WRAP_R(tu6_tex_wrap(pCreateInfo->addressModeW)) |
2327 A6XX_TEX_SAMP_0_LOD_BIAS(pCreateInfo->mipLodBias);
2328 sampler->descriptor[1] =
2329 /* COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) | */
2330 COND(pCreateInfo->unnormalizedCoordinates, A6XX_TEX_SAMP_1_UNNORM_COORDS) |
2331 A6XX_TEX_SAMP_1_MIN_LOD(min_lod) |
2332 A6XX_TEX_SAMP_1_MAX_LOD(max_lod) |
2333 COND(pCreateInfo->compareEnable,
2334 A6XX_TEX_SAMP_1_COMPARE_FUNC(tu6_compare_func(pCreateInfo->compareOp)));
2335 /* This is an offset into the border_color BO, which we fill with all the
2336 * possible Vulkan border colors in the correct order, so we can just use
2337 * the Vulkan enum with no translation necessary.
2338 */
2339 sampler->descriptor[2] =
2340 A6XX_TEX_SAMP_2_BCOLOR_OFFSET((unsigned) pCreateInfo->borderColor *
2341 sizeof(struct bcolor_entry));
2342 sampler->descriptor[3] = 0;
2343
2344 if (reduction) {
2345 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_REDUCTION_MODE(
2346 tu6_reduction_mode(reduction->reductionMode));
2347 }
2348
2349 sampler->ycbcr_sampler = ycbcr_conversion ?
2350 tu_sampler_ycbcr_conversion_from_handle(ycbcr_conversion->conversion) : NULL;
2351
2352 if (sampler->ycbcr_sampler &&
2353 sampler->ycbcr_sampler->chroma_filter == VK_FILTER_LINEAR) {
2354 sampler->descriptor[2] |= A6XX_TEX_SAMP_2_CHROMA_LINEAR;
2355 }
2356
2357 /* TODO:
2358 * A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR disables mipmapping, but vk has no NONE mipfilter?
2359 */
2360 }
2361
2362 VkResult
2363 tu_CreateSampler(VkDevice _device,
2364 const VkSamplerCreateInfo *pCreateInfo,
2365 const VkAllocationCallbacks *pAllocator,
2366 VkSampler *pSampler)
2367 {
2368 TU_FROM_HANDLE(tu_device, device, _device);
2369 struct tu_sampler *sampler;
2370
2371 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
2372
2373 sampler = vk_object_alloc(&device->vk, pAllocator, sizeof(*sampler),
2374 VK_OBJECT_TYPE_SAMPLER);
2375 if (!sampler)
2376 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
2377
2378 tu_init_sampler(device, sampler, pCreateInfo);
2379 *pSampler = tu_sampler_to_handle(sampler);
2380
2381 return VK_SUCCESS;
2382 }
2383
2384 void
2385 tu_DestroySampler(VkDevice _device,
2386 VkSampler _sampler,
2387 const VkAllocationCallbacks *pAllocator)
2388 {
2389 TU_FROM_HANDLE(tu_device, device, _device);
2390 TU_FROM_HANDLE(tu_sampler, sampler, _sampler);
2391
2392 if (!sampler)
2393 return;
2394
2395 vk_object_free(&device->vk, pAllocator, sampler);
2396 }
2397
2398 /* vk_icd.h does not declare this function, so we declare it here to
2399 * suppress Wmissing-prototypes.
2400 */
2401 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2402 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion);
2403
2404 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2405 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *pSupportedVersion)
2406 {
2407 /* For the full details on loader interface versioning, see
2408 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2409 * What follows is a condensed summary, to help you navigate the large and
2410 * confusing official doc.
2411 *
2412 * - Loader interface v0 is incompatible with later versions. We don't
2413 * support it.
2414 *
2415 * - In loader interface v1:
2416 * - The first ICD entrypoint called by the loader is
2417 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2418 * entrypoint.
2419 * - The ICD must statically expose no other Vulkan symbol unless it
2420 * is linked with -Bsymbolic.
2421 * - Each dispatchable Vulkan handle created by the ICD must be
2422 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2423 * ICD must initialize VK_LOADER_DATA.loadMagic to
2424 * ICD_LOADER_MAGIC.
2425 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2426 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2427 * such loader-managed surfaces.
2428 *
2429 * - Loader interface v2 differs from v1 in:
2430 * - The first ICD entrypoint called by the loader is
2431 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2432 * statically expose this entrypoint.
2433 *
2434 * - Loader interface v3 differs from v2 in:
2435 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2436 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2437 * because the loader no longer does so.
2438 */
2439 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2440 return VK_SUCCESS;
2441 }
2442
2443 VkResult
2444 tu_GetMemoryFdKHR(VkDevice _device,
2445 const VkMemoryGetFdInfoKHR *pGetFdInfo,
2446 int *pFd)
2447 {
2448 TU_FROM_HANDLE(tu_device, device, _device);
2449 TU_FROM_HANDLE(tu_device_memory, memory, pGetFdInfo->memory);
2450
2451 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
2452
2453 /* At the moment, we support only the below handle types. */
2454 assert(pGetFdInfo->handleType ==
2455 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2456 pGetFdInfo->handleType ==
2457 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2458
2459 int prime_fd = tu_bo_export_dmabuf(device, &memory->bo);
2460 if (prime_fd < 0)
2461 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
2462
2463 *pFd = prime_fd;
2464 return VK_SUCCESS;
2465 }
2466
2467 VkResult
2468 tu_GetMemoryFdPropertiesKHR(VkDevice _device,
2469 VkExternalMemoryHandleTypeFlagBits handleType,
2470 int fd,
2471 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
2472 {
2473 assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2474 pMemoryFdProperties->memoryTypeBits = 1;
2475 return VK_SUCCESS;
2476 }
2477
2478 VkResult
2479 tu_ImportFenceFdKHR(VkDevice _device,
2480 const VkImportFenceFdInfoKHR *pImportFenceFdInfo)
2481 {
2482 tu_stub();
2483
2484 return VK_SUCCESS;
2485 }
2486
2487 VkResult
2488 tu_GetFenceFdKHR(VkDevice _device,
2489 const VkFenceGetFdInfoKHR *pGetFdInfo,
2490 int *pFd)
2491 {
2492 tu_stub();
2493
2494 return VK_SUCCESS;
2495 }
2496
2497 VkResult
2498 tu_ImportSemaphoreFdKHR(VkDevice _device,
2499 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo)
2500 {
2501 TU_FROM_HANDLE(tu_device, device, _device);
2502 TU_FROM_HANDLE(tu_semaphore, sem, pImportSemaphoreFdInfo->semaphore);
2503 int ret;
2504 struct tu_semaphore_part *dst = NULL;
2505
2506 if (pImportSemaphoreFdInfo->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT) {
2507 dst = &sem->temporary;
2508 } else {
2509 dst = &sem->permanent;
2510 }
2511
2512 uint32_t syncobj = dst->kind == TU_SEMAPHORE_SYNCOBJ ? dst->syncobj : 0;
2513
2514 switch(pImportSemaphoreFdInfo->handleType) {
2515 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT: {
2516 uint32_t old_syncobj = syncobj;
2517 ret = drmSyncobjFDToHandle(device->physical_device->local_fd, pImportSemaphoreFdInfo->fd, &syncobj);
2518 if (ret == 0) {
2519 close(pImportSemaphoreFdInfo->fd);
2520 if (old_syncobj)
2521 drmSyncobjDestroy(device->physical_device->local_fd, old_syncobj);
2522 }
2523 break;
2524 }
2525 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT: {
2526 if (!syncobj) {
2527 ret = drmSyncobjCreate(device->physical_device->local_fd, 0, &syncobj);
2528 if (ret)
2529 break;
2530 }
2531 if (pImportSemaphoreFdInfo->fd == -1) {
2532 ret = drmSyncobjSignal(device->physical_device->local_fd, &syncobj, 1);
2533 } else {
2534 ret = drmSyncobjImportSyncFile(device->physical_device->local_fd, syncobj, pImportSemaphoreFdInfo->fd);
2535 }
2536 if (!ret)
2537 close(pImportSemaphoreFdInfo->fd);
2538 break;
2539 }
2540 default:
2541 unreachable("Unhandled semaphore handle type");
2542 }
2543
2544 if (ret) {
2545 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
2546 }
2547 dst->syncobj = syncobj;
2548 dst->kind = TU_SEMAPHORE_SYNCOBJ;
2549
2550 return VK_SUCCESS;
2551 }
2552
2553 VkResult
2554 tu_GetSemaphoreFdKHR(VkDevice _device,
2555 const VkSemaphoreGetFdInfoKHR *pGetFdInfo,
2556 int *pFd)
2557 {
2558 TU_FROM_HANDLE(tu_device, device, _device);
2559 TU_FROM_HANDLE(tu_semaphore, sem, pGetFdInfo->semaphore);
2560 int ret;
2561 uint32_t syncobj_handle;
2562
2563 if (sem->temporary.kind != TU_SEMAPHORE_NONE) {
2564 assert(sem->temporary.kind == TU_SEMAPHORE_SYNCOBJ);
2565 syncobj_handle = sem->temporary.syncobj;
2566 } else {
2567 assert(sem->permanent.kind == TU_SEMAPHORE_SYNCOBJ);
2568 syncobj_handle = sem->permanent.syncobj;
2569 }
2570
2571 switch(pGetFdInfo->handleType) {
2572 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT:
2573 ret = drmSyncobjHandleToFD(device->physical_device->local_fd, syncobj_handle, pFd);
2574 break;
2575 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT:
2576 ret = drmSyncobjExportSyncFile(device->physical_device->local_fd, syncobj_handle, pFd);
2577 if (!ret) {
2578 if (sem->temporary.kind != TU_SEMAPHORE_NONE) {
2579 tu_semaphore_part_destroy(device, &sem->temporary);
2580 } else {
2581 drmSyncobjReset(device->physical_device->local_fd, &syncobj_handle, 1);
2582 }
2583 }
2584 break;
2585 default:
2586 unreachable("Unhandled semaphore handle type");
2587 }
2588
2589 if (ret)
2590 return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE);
2591 return VK_SUCCESS;
2592 }
2593
2594
2595 static bool tu_has_syncobj(struct tu_physical_device *pdev)
2596 {
2597 uint64_t value;
2598 if (drmGetCap(pdev->local_fd, DRM_CAP_SYNCOBJ, &value))
2599 return false;
2600 return value && pdev->msm_major_version == 1 && pdev->msm_minor_version >= 6;
2601 }
2602
2603 void
2604 tu_GetPhysicalDeviceExternalSemaphoreProperties(
2605 VkPhysicalDevice physicalDevice,
2606 const VkPhysicalDeviceExternalSemaphoreInfo *pExternalSemaphoreInfo,
2607 VkExternalSemaphoreProperties *pExternalSemaphoreProperties)
2608 {
2609 TU_FROM_HANDLE(tu_physical_device, pdev, physicalDevice);
2610
2611 if (tu_has_syncobj(pdev) &&
2612 (pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT ||
2613 pExternalSemaphoreInfo->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT)) {
2614 pExternalSemaphoreProperties->exportFromImportedHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
2615 pExternalSemaphoreProperties->compatibleHandleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
2616 pExternalSemaphoreProperties->externalSemaphoreFeatures = VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT |
2617 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT;
2618 } else {
2619 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
2620 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
2621 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
2622 }
2623 }
2624
2625 void
2626 tu_GetPhysicalDeviceExternalFenceProperties(
2627 VkPhysicalDevice physicalDevice,
2628 const VkPhysicalDeviceExternalFenceInfo *pExternalFenceInfo,
2629 VkExternalFenceProperties *pExternalFenceProperties)
2630 {
2631 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
2632 pExternalFenceProperties->compatibleHandleTypes = 0;
2633 pExternalFenceProperties->externalFenceFeatures = 0;
2634 }
2635
2636 VkResult
2637 tu_CreateDebugReportCallbackEXT(
2638 VkInstance _instance,
2639 const VkDebugReportCallbackCreateInfoEXT *pCreateInfo,
2640 const VkAllocationCallbacks *pAllocator,
2641 VkDebugReportCallbackEXT *pCallback)
2642 {
2643 TU_FROM_HANDLE(tu_instance, instance, _instance);
2644 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
2645 pCreateInfo, pAllocator,
2646 &instance->alloc, pCallback);
2647 }
2648
2649 void
2650 tu_DestroyDebugReportCallbackEXT(VkInstance _instance,
2651 VkDebugReportCallbackEXT _callback,
2652 const VkAllocationCallbacks *pAllocator)
2653 {
2654 TU_FROM_HANDLE(tu_instance, instance, _instance);
2655 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
2656 _callback, pAllocator, &instance->alloc);
2657 }
2658
2659 void
2660 tu_DebugReportMessageEXT(VkInstance _instance,
2661 VkDebugReportFlagsEXT flags,
2662 VkDebugReportObjectTypeEXT objectType,
2663 uint64_t object,
2664 size_t location,
2665 int32_t messageCode,
2666 const char *pLayerPrefix,
2667 const char *pMessage)
2668 {
2669 TU_FROM_HANDLE(tu_instance, instance, _instance);
2670 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
2671 object, location, messageCode, pLayerPrefix, pMessage);
2672 }
2673
2674 void
2675 tu_GetDeviceGroupPeerMemoryFeatures(
2676 VkDevice device,
2677 uint32_t heapIndex,
2678 uint32_t localDeviceIndex,
2679 uint32_t remoteDeviceIndex,
2680 VkPeerMemoryFeatureFlags *pPeerMemoryFeatures)
2681 {
2682 assert(localDeviceIndex == remoteDeviceIndex);
2683
2684 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
2685 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
2686 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
2687 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
2688 }
2689
2690 void tu_GetPhysicalDeviceMultisamplePropertiesEXT(
2691 VkPhysicalDevice physicalDevice,
2692 VkSampleCountFlagBits samples,
2693 VkMultisamplePropertiesEXT* pMultisampleProperties)
2694 {
2695 TU_FROM_HANDLE(tu_physical_device, pdevice, physicalDevice);
2696
2697 if (samples <= VK_SAMPLE_COUNT_4_BIT && pdevice->supported_extensions.EXT_sample_locations)
2698 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 1, 1 };
2699 else
2700 pMultisampleProperties->maxSampleLocationGridSize = (VkExtent2D){ 0, 0 };
2701 }