anv: Use the BO cache for DeviceMemory allocations
[mesa.git] / src / intel / vulkan / anv_device.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/sysinfo.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <xf86drm.h>
32
33 #include "anv_private.h"
34 #include "util/strtod.h"
35 #include "util/debug.h"
36 #include "util/build_id.h"
37 #include "util/mesa-sha1.h"
38 #include "util/vk_util.h"
39
40 #include "genxml/gen7_pack.h"
41
42 static void
43 compiler_debug_log(void *data, const char *fmt, ...)
44 { }
45
46 static void
47 compiler_perf_log(void *data, const char *fmt, ...)
48 {
49 va_list args;
50 va_start(args, fmt);
51
52 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
53 vfprintf(stderr, fmt, args);
54
55 va_end(args);
56 }
57
58 static VkResult
59 anv_compute_heap_size(int fd, uint64_t *heap_size)
60 {
61 uint64_t gtt_size;
62 if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
63 &gtt_size) == -1) {
64 /* If, for whatever reason, we can't actually get the GTT size from the
65 * kernel (too old?) fall back to the aperture size.
66 */
67 anv_perf_warn("Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
68
69 if (anv_gem_get_aperture(fd, &gtt_size) == -1) {
70 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
71 "failed to get aperture size: %m");
72 }
73 }
74
75 /* Query the total ram from the system */
76 struct sysinfo info;
77 sysinfo(&info);
78
79 uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit;
80
81 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
82 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
83 */
84 uint64_t available_ram;
85 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
86 available_ram = total_ram / 2;
87 else
88 available_ram = total_ram * 3 / 4;
89
90 /* We also want to leave some padding for things we allocate in the driver,
91 * so don't go over 3/4 of the GTT either.
92 */
93 uint64_t available_gtt = gtt_size * 3 / 4;
94
95 *heap_size = MIN2(available_ram, available_gtt);
96
97 return VK_SUCCESS;
98 }
99
100 static VkResult
101 anv_physical_device_init_uuids(struct anv_physical_device *device)
102 {
103 const struct build_id_note *note = build_id_find_nhdr("libvulkan_intel.so");
104 if (!note) {
105 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
106 "Failed to find build-id");
107 }
108
109 unsigned build_id_len = build_id_length(note);
110 if (build_id_len < 20) {
111 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
112 "build-id too short. It needs to be a SHA");
113 }
114
115 struct mesa_sha1 sha1_ctx;
116 uint8_t sha1[20];
117 STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
118
119 /* The pipeline cache UUID is used for determining when a pipeline cache is
120 * invalid. It needs both a driver build and the PCI ID of the device.
121 */
122 _mesa_sha1_init(&sha1_ctx);
123 _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
124 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
125 sizeof(device->chipset_id));
126 _mesa_sha1_final(&sha1_ctx, sha1);
127 memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
128
129 /* The driver UUID is used for determining sharability of images and memory
130 * between two Vulkan instances in separate processes. People who want to
131 * share memory need to also check the device UUID (below) so all this
132 * needs to be is the build-id.
133 */
134 memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
135
136 /* The device UUID uniquely identifies the given device within the machine.
137 * Since we never have more than one device, this doesn't need to be a real
138 * UUID. However, on the off-chance that someone tries to use this to
139 * cache pre-tiled images or something of the like, we use the PCI ID and
140 * some bits of ISL info to ensure that this is safe.
141 */
142 _mesa_sha1_init(&sha1_ctx);
143 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
144 sizeof(device->chipset_id));
145 _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
146 sizeof(device->isl_dev.has_bit6_swizzling));
147 _mesa_sha1_final(&sha1_ctx, sha1);
148 memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
149
150 return VK_SUCCESS;
151 }
152
153 static VkResult
154 anv_physical_device_init(struct anv_physical_device *device,
155 struct anv_instance *instance,
156 const char *path)
157 {
158 VkResult result;
159 int fd;
160
161 fd = open(path, O_RDWR | O_CLOEXEC);
162 if (fd < 0)
163 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
164
165 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
166 device->instance = instance;
167
168 assert(strlen(path) < ARRAY_SIZE(device->path));
169 strncpy(device->path, path, ARRAY_SIZE(device->path));
170
171 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
172 if (!device->chipset_id) {
173 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
174 goto fail;
175 }
176
177 device->name = gen_get_device_name(device->chipset_id);
178 if (!gen_get_device_info(device->chipset_id, &device->info)) {
179 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
180 goto fail;
181 }
182
183 if (device->info.is_haswell) {
184 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
185 } else if (device->info.gen == 7 && !device->info.is_baytrail) {
186 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
187 } else if (device->info.gen == 7 && device->info.is_baytrail) {
188 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
189 } else if (device->info.gen >= 8) {
190 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
191 * supported as anything */
192 } else {
193 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
194 "Vulkan not yet supported on %s", device->name);
195 goto fail;
196 }
197
198 device->cmd_parser_version = -1;
199 if (device->info.gen == 7) {
200 device->cmd_parser_version =
201 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
202 if (device->cmd_parser_version == -1) {
203 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
204 "failed to get command parser version");
205 goto fail;
206 }
207 }
208
209 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
210 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
211 "kernel missing gem wait");
212 goto fail;
213 }
214
215 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
216 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
217 "kernel missing execbuf2");
218 goto fail;
219 }
220
221 if (!device->info.has_llc &&
222 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
223 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
224 "kernel missing wc mmap");
225 goto fail;
226 }
227
228 device->supports_48bit_addresses = anv_gem_supports_48b_addresses(fd);
229
230 result = anv_compute_heap_size(fd, &device->heap_size);
231 if (result != VK_SUCCESS)
232 goto fail;
233
234 device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
235
236 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
237
238 /* GENs prior to 8 do not support EU/Subslice info */
239 if (device->info.gen >= 8) {
240 device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
241 device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
242
243 /* Without this information, we cannot get the right Braswell
244 * brandstrings, and we have to use conservative numbers for GPGPU on
245 * many platforms, but otherwise, things will just work.
246 */
247 if (device->subslice_total < 1 || device->eu_total < 1) {
248 fprintf(stderr, "WARNING: Kernel 4.1 required to properly"
249 " query GPU properties.\n");
250 }
251 } else if (device->info.gen == 7) {
252 device->subslice_total = 1 << (device->info.gt - 1);
253 }
254
255 if (device->info.is_cherryview &&
256 device->subslice_total > 0 && device->eu_total > 0) {
257 /* Logical CS threads = EUs per subslice * 7 threads per EU */
258 uint32_t max_cs_threads = device->eu_total / device->subslice_total * 7;
259
260 /* Fuse configurations may give more threads than expected, never less. */
261 if (max_cs_threads > device->info.max_cs_threads)
262 device->info.max_cs_threads = max_cs_threads;
263 }
264
265 brw_process_intel_debug_variable();
266
267 device->compiler = brw_compiler_create(NULL, &device->info);
268 if (device->compiler == NULL) {
269 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
270 goto fail;
271 }
272 device->compiler->shader_debug_log = compiler_debug_log;
273 device->compiler->shader_perf_log = compiler_perf_log;
274
275 isl_device_init(&device->isl_dev, &device->info, swizzled);
276
277 result = anv_physical_device_init_uuids(device);
278 if (result != VK_SUCCESS)
279 goto fail;
280
281 result = anv_init_wsi(device);
282 if (result != VK_SUCCESS) {
283 ralloc_free(device->compiler);
284 goto fail;
285 }
286
287 device->local_fd = fd;
288 return VK_SUCCESS;
289
290 fail:
291 close(fd);
292 return result;
293 }
294
295 static void
296 anv_physical_device_finish(struct anv_physical_device *device)
297 {
298 anv_finish_wsi(device);
299 ralloc_free(device->compiler);
300 close(device->local_fd);
301 }
302
303 static const VkExtensionProperties global_extensions[] = {
304 {
305 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
306 .specVersion = 25,
307 },
308 #ifdef VK_USE_PLATFORM_XCB_KHR
309 {
310 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
311 .specVersion = 6,
312 },
313 #endif
314 #ifdef VK_USE_PLATFORM_XLIB_KHR
315 {
316 .extensionName = VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
317 .specVersion = 6,
318 },
319 #endif
320 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
321 {
322 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
323 .specVersion = 5,
324 },
325 #endif
326 {
327 .extensionName = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
328 .specVersion = 1,
329 },
330 {
331 .extensionName = VK_KHX_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
332 .specVersion = 1,
333 },
334 };
335
336 static const VkExtensionProperties device_extensions[] = {
337 {
338 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
339 .specVersion = 68,
340 },
341 {
342 .extensionName = VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
343 .specVersion = 1,
344 },
345 {
346 .extensionName = VK_KHR_MAINTENANCE1_EXTENSION_NAME,
347 .specVersion = 1,
348 },
349 {
350 .extensionName = VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME,
351 .specVersion = 1,
352 },
353 {
354 .extensionName = VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
355 .specVersion = 1,
356 },
357 {
358 .extensionName = VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
359 .specVersion = 1,
360 },
361 {
362 .extensionName = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
363 .specVersion = 1,
364 },
365 {
366 .extensionName = VK_KHX_EXTERNAL_MEMORY_EXTENSION_NAME,
367 .specVersion = 1,
368 },
369 };
370
371 static void *
372 default_alloc_func(void *pUserData, size_t size, size_t align,
373 VkSystemAllocationScope allocationScope)
374 {
375 return malloc(size);
376 }
377
378 static void *
379 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
380 size_t align, VkSystemAllocationScope allocationScope)
381 {
382 return realloc(pOriginal, size);
383 }
384
385 static void
386 default_free_func(void *pUserData, void *pMemory)
387 {
388 free(pMemory);
389 }
390
391 static const VkAllocationCallbacks default_alloc = {
392 .pUserData = NULL,
393 .pfnAllocation = default_alloc_func,
394 .pfnReallocation = default_realloc_func,
395 .pfnFree = default_free_func,
396 };
397
398 VkResult anv_CreateInstance(
399 const VkInstanceCreateInfo* pCreateInfo,
400 const VkAllocationCallbacks* pAllocator,
401 VkInstance* pInstance)
402 {
403 struct anv_instance *instance;
404
405 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
406
407 uint32_t client_version;
408 if (pCreateInfo->pApplicationInfo &&
409 pCreateInfo->pApplicationInfo->apiVersion != 0) {
410 client_version = pCreateInfo->pApplicationInfo->apiVersion;
411 } else {
412 client_version = VK_MAKE_VERSION(1, 0, 0);
413 }
414
415 if (VK_MAKE_VERSION(1, 0, 0) > client_version ||
416 client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {
417 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
418 "Client requested version %d.%d.%d",
419 VK_VERSION_MAJOR(client_version),
420 VK_VERSION_MINOR(client_version),
421 VK_VERSION_PATCH(client_version));
422 }
423
424 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
425 bool found = false;
426 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
427 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
428 global_extensions[j].extensionName) == 0) {
429 found = true;
430 break;
431 }
432 }
433 if (!found)
434 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
435 }
436
437 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
438 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
439 if (!instance)
440 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
441
442 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
443
444 if (pAllocator)
445 instance->alloc = *pAllocator;
446 else
447 instance->alloc = default_alloc;
448
449 instance->apiVersion = client_version;
450 instance->physicalDeviceCount = -1;
451
452 _mesa_locale_init();
453
454 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
455
456 *pInstance = anv_instance_to_handle(instance);
457
458 return VK_SUCCESS;
459 }
460
461 void anv_DestroyInstance(
462 VkInstance _instance,
463 const VkAllocationCallbacks* pAllocator)
464 {
465 ANV_FROM_HANDLE(anv_instance, instance, _instance);
466
467 if (!instance)
468 return;
469
470 if (instance->physicalDeviceCount > 0) {
471 /* We support at most one physical device. */
472 assert(instance->physicalDeviceCount == 1);
473 anv_physical_device_finish(&instance->physicalDevice);
474 }
475
476 VG(VALGRIND_DESTROY_MEMPOOL(instance));
477
478 _mesa_locale_fini();
479
480 vk_free(&instance->alloc, instance);
481 }
482
483 static VkResult
484 anv_enumerate_devices(struct anv_instance *instance)
485 {
486 /* TODO: Check for more devices ? */
487 drmDevicePtr devices[8];
488 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
489 int max_devices;
490
491 instance->physicalDeviceCount = 0;
492
493 max_devices = drmGetDevices2(0, devices, sizeof(devices));
494 if (max_devices < 1)
495 return VK_ERROR_INCOMPATIBLE_DRIVER;
496
497 for (unsigned i = 0; i < (unsigned)max_devices; i++) {
498 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
499 devices[i]->bustype == DRM_BUS_PCI &&
500 devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
501
502 result = anv_physical_device_init(&instance->physicalDevice,
503 instance,
504 devices[i]->nodes[DRM_NODE_RENDER]);
505 if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
506 break;
507 }
508 }
509
510 if (result == VK_SUCCESS)
511 instance->physicalDeviceCount = 1;
512
513 return result;
514 }
515
516
517 VkResult anv_EnumeratePhysicalDevices(
518 VkInstance _instance,
519 uint32_t* pPhysicalDeviceCount,
520 VkPhysicalDevice* pPhysicalDevices)
521 {
522 ANV_FROM_HANDLE(anv_instance, instance, _instance);
523 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
524 VkResult result;
525
526 if (instance->physicalDeviceCount < 0) {
527 result = anv_enumerate_devices(instance);
528 if (result != VK_SUCCESS &&
529 result != VK_ERROR_INCOMPATIBLE_DRIVER)
530 return result;
531 }
532
533 if (instance->physicalDeviceCount > 0) {
534 assert(instance->physicalDeviceCount == 1);
535 vk_outarray_append(&out, i) {
536 *i = anv_physical_device_to_handle(&instance->physicalDevice);
537 }
538 }
539
540 return vk_outarray_status(&out);
541 }
542
543 void anv_GetPhysicalDeviceFeatures(
544 VkPhysicalDevice physicalDevice,
545 VkPhysicalDeviceFeatures* pFeatures)
546 {
547 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
548
549 *pFeatures = (VkPhysicalDeviceFeatures) {
550 .robustBufferAccess = true,
551 .fullDrawIndexUint32 = true,
552 .imageCubeArray = true,
553 .independentBlend = true,
554 .geometryShader = true,
555 .tessellationShader = true,
556 .sampleRateShading = true,
557 .dualSrcBlend = true,
558 .logicOp = true,
559 .multiDrawIndirect = false,
560 .drawIndirectFirstInstance = true,
561 .depthClamp = true,
562 .depthBiasClamp = true,
563 .fillModeNonSolid = true,
564 .depthBounds = false,
565 .wideLines = true,
566 .largePoints = true,
567 .alphaToOne = true,
568 .multiViewport = true,
569 .samplerAnisotropy = true,
570 .textureCompressionETC2 = pdevice->info.gen >= 8 ||
571 pdevice->info.is_baytrail,
572 .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
573 .textureCompressionBC = true,
574 .occlusionQueryPrecise = true,
575 .pipelineStatisticsQuery = true,
576 .fragmentStoresAndAtomics = true,
577 .shaderTessellationAndGeometryPointSize = true,
578 .shaderImageGatherExtended = true,
579 .shaderStorageImageExtendedFormats = true,
580 .shaderStorageImageMultisample = false,
581 .shaderStorageImageReadWithoutFormat = false,
582 .shaderStorageImageWriteWithoutFormat = true,
583 .shaderUniformBufferArrayDynamicIndexing = true,
584 .shaderSampledImageArrayDynamicIndexing = true,
585 .shaderStorageBufferArrayDynamicIndexing = true,
586 .shaderStorageImageArrayDynamicIndexing = true,
587 .shaderClipDistance = true,
588 .shaderCullDistance = true,
589 .shaderFloat64 = pdevice->info.gen >= 8,
590 .shaderInt64 = pdevice->info.gen >= 8,
591 .shaderInt16 = false,
592 .shaderResourceMinLod = false,
593 .variableMultisampleRate = false,
594 .inheritedQueries = true,
595 };
596
597 /* We can't do image stores in vec4 shaders */
598 pFeatures->vertexPipelineStoresAndAtomics =
599 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
600 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
601 }
602
603 void anv_GetPhysicalDeviceFeatures2KHR(
604 VkPhysicalDevice physicalDevice,
605 VkPhysicalDeviceFeatures2KHR* pFeatures)
606 {
607 anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
608
609 vk_foreach_struct(ext, pFeatures->pNext) {
610 switch (ext->sType) {
611 default:
612 anv_debug_ignored_stype(ext->sType);
613 break;
614 }
615 }
616 }
617
618 void anv_GetPhysicalDeviceProperties(
619 VkPhysicalDevice physicalDevice,
620 VkPhysicalDeviceProperties* pProperties)
621 {
622 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
623 const struct gen_device_info *devinfo = &pdevice->info;
624
625 /* See assertions made when programming the buffer surface state. */
626 const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
627 (1ul << 30) : (1ul << 27);
628
629 VkSampleCountFlags sample_counts =
630 isl_device_get_sample_counts(&pdevice->isl_dev);
631
632 VkPhysicalDeviceLimits limits = {
633 .maxImageDimension1D = (1 << 14),
634 .maxImageDimension2D = (1 << 14),
635 .maxImageDimension3D = (1 << 11),
636 .maxImageDimensionCube = (1 << 14),
637 .maxImageArrayLayers = (1 << 11),
638 .maxTexelBufferElements = 128 * 1024 * 1024,
639 .maxUniformBufferRange = (1ul << 27),
640 .maxStorageBufferRange = max_raw_buffer_sz,
641 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
642 .maxMemoryAllocationCount = UINT32_MAX,
643 .maxSamplerAllocationCount = 64 * 1024,
644 .bufferImageGranularity = 64, /* A cache line */
645 .sparseAddressSpaceSize = 0,
646 .maxBoundDescriptorSets = MAX_SETS,
647 .maxPerStageDescriptorSamplers = 64,
648 .maxPerStageDescriptorUniformBuffers = 64,
649 .maxPerStageDescriptorStorageBuffers = 64,
650 .maxPerStageDescriptorSampledImages = 64,
651 .maxPerStageDescriptorStorageImages = 64,
652 .maxPerStageDescriptorInputAttachments = 64,
653 .maxPerStageResources = 128,
654 .maxDescriptorSetSamplers = 256,
655 .maxDescriptorSetUniformBuffers = 256,
656 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
657 .maxDescriptorSetStorageBuffers = 256,
658 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
659 .maxDescriptorSetSampledImages = 256,
660 .maxDescriptorSetStorageImages = 256,
661 .maxDescriptorSetInputAttachments = 256,
662 .maxVertexInputAttributes = MAX_VBS,
663 .maxVertexInputBindings = MAX_VBS,
664 .maxVertexInputAttributeOffset = 2047,
665 .maxVertexInputBindingStride = 2048,
666 .maxVertexOutputComponents = 128,
667 .maxTessellationGenerationLevel = 64,
668 .maxTessellationPatchSize = 32,
669 .maxTessellationControlPerVertexInputComponents = 128,
670 .maxTessellationControlPerVertexOutputComponents = 128,
671 .maxTessellationControlPerPatchOutputComponents = 128,
672 .maxTessellationControlTotalOutputComponents = 2048,
673 .maxTessellationEvaluationInputComponents = 128,
674 .maxTessellationEvaluationOutputComponents = 128,
675 .maxGeometryShaderInvocations = 32,
676 .maxGeometryInputComponents = 64,
677 .maxGeometryOutputComponents = 128,
678 .maxGeometryOutputVertices = 256,
679 .maxGeometryTotalOutputComponents = 1024,
680 .maxFragmentInputComponents = 128,
681 .maxFragmentOutputAttachments = 8,
682 .maxFragmentDualSrcAttachments = 1,
683 .maxFragmentCombinedOutputResources = 8,
684 .maxComputeSharedMemorySize = 32768,
685 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
686 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
687 .maxComputeWorkGroupSize = {
688 16 * devinfo->max_cs_threads,
689 16 * devinfo->max_cs_threads,
690 16 * devinfo->max_cs_threads,
691 },
692 .subPixelPrecisionBits = 4 /* FIXME */,
693 .subTexelPrecisionBits = 4 /* FIXME */,
694 .mipmapPrecisionBits = 4 /* FIXME */,
695 .maxDrawIndexedIndexValue = UINT32_MAX,
696 .maxDrawIndirectCount = UINT32_MAX,
697 .maxSamplerLodBias = 16,
698 .maxSamplerAnisotropy = 16,
699 .maxViewports = MAX_VIEWPORTS,
700 .maxViewportDimensions = { (1 << 14), (1 << 14) },
701 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
702 .viewportSubPixelBits = 13, /* We take a float? */
703 .minMemoryMapAlignment = 4096, /* A page */
704 .minTexelBufferOffsetAlignment = 1,
705 .minUniformBufferOffsetAlignment = 16,
706 .minStorageBufferOffsetAlignment = 4,
707 .minTexelOffset = -8,
708 .maxTexelOffset = 7,
709 .minTexelGatherOffset = -32,
710 .maxTexelGatherOffset = 31,
711 .minInterpolationOffset = -0.5,
712 .maxInterpolationOffset = 0.4375,
713 .subPixelInterpolationOffsetBits = 4,
714 .maxFramebufferWidth = (1 << 14),
715 .maxFramebufferHeight = (1 << 14),
716 .maxFramebufferLayers = (1 << 11),
717 .framebufferColorSampleCounts = sample_counts,
718 .framebufferDepthSampleCounts = sample_counts,
719 .framebufferStencilSampleCounts = sample_counts,
720 .framebufferNoAttachmentsSampleCounts = sample_counts,
721 .maxColorAttachments = MAX_RTS,
722 .sampledImageColorSampleCounts = sample_counts,
723 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
724 .sampledImageDepthSampleCounts = sample_counts,
725 .sampledImageStencilSampleCounts = sample_counts,
726 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
727 .maxSampleMaskWords = 1,
728 .timestampComputeAndGraphics = false,
729 .timestampPeriod = devinfo->timebase_scale,
730 .maxClipDistances = 8,
731 .maxCullDistances = 8,
732 .maxCombinedClipAndCullDistances = 8,
733 .discreteQueuePriorities = 1,
734 .pointSizeRange = { 0.125, 255.875 },
735 .lineWidthRange = { 0.0, 7.9921875 },
736 .pointSizeGranularity = (1.0 / 8.0),
737 .lineWidthGranularity = (1.0 / 128.0),
738 .strictLines = false, /* FINISHME */
739 .standardSampleLocations = true,
740 .optimalBufferCopyOffsetAlignment = 128,
741 .optimalBufferCopyRowPitchAlignment = 128,
742 .nonCoherentAtomSize = 64,
743 };
744
745 *pProperties = (VkPhysicalDeviceProperties) {
746 .apiVersion = VK_MAKE_VERSION(1, 0, 42),
747 .driverVersion = 1,
748 .vendorID = 0x8086,
749 .deviceID = pdevice->chipset_id,
750 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
751 .limits = limits,
752 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
753 };
754
755 strcpy(pProperties->deviceName, pdevice->name);
756 memcpy(pProperties->pipelineCacheUUID,
757 pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
758 }
759
760 void anv_GetPhysicalDeviceProperties2KHR(
761 VkPhysicalDevice physicalDevice,
762 VkPhysicalDeviceProperties2KHR* pProperties)
763 {
764 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
765
766 anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
767
768 vk_foreach_struct(ext, pProperties->pNext) {
769 switch (ext->sType) {
770 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
771 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
772 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
773
774 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
775 break;
776 }
777
778 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHX: {
779 VkPhysicalDeviceIDPropertiesKHX *id_props =
780 (VkPhysicalDeviceIDPropertiesKHX *)ext;
781 memcpy(id_props->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
782 memcpy(id_props->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
783 /* The LUID is for Windows. */
784 id_props->deviceLUIDValid = false;
785 break;
786 }
787
788 default:
789 anv_debug_ignored_stype(ext->sType);
790 break;
791 }
792 }
793 }
794
795 /* We support exactly one queue family. */
796 static const VkQueueFamilyProperties
797 anv_queue_family_properties = {
798 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
799 VK_QUEUE_COMPUTE_BIT |
800 VK_QUEUE_TRANSFER_BIT,
801 .queueCount = 1,
802 .timestampValidBits = 36, /* XXX: Real value here */
803 .minImageTransferGranularity = { 1, 1, 1 },
804 };
805
806 void anv_GetPhysicalDeviceQueueFamilyProperties(
807 VkPhysicalDevice physicalDevice,
808 uint32_t* pCount,
809 VkQueueFamilyProperties* pQueueFamilyProperties)
810 {
811 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
812
813 vk_outarray_append(&out, p) {
814 *p = anv_queue_family_properties;
815 }
816 }
817
818 void anv_GetPhysicalDeviceQueueFamilyProperties2KHR(
819 VkPhysicalDevice physicalDevice,
820 uint32_t* pQueueFamilyPropertyCount,
821 VkQueueFamilyProperties2KHR* pQueueFamilyProperties)
822 {
823
824 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
825
826 vk_outarray_append(&out, p) {
827 p->queueFamilyProperties = anv_queue_family_properties;
828
829 vk_foreach_struct(s, p->pNext) {
830 anv_debug_ignored_stype(s->sType);
831 }
832 }
833 }
834
835 void anv_GetPhysicalDeviceMemoryProperties(
836 VkPhysicalDevice physicalDevice,
837 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
838 {
839 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
840
841 if (physical_device->info.has_llc) {
842 /* Big core GPUs share LLC with the CPU and thus one memory type can be
843 * both cached and coherent at the same time.
844 */
845 pMemoryProperties->memoryTypeCount = 1;
846 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
847 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
848 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
849 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
850 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
851 .heapIndex = 0,
852 };
853 } else {
854 /* The spec requires that we expose a host-visible, coherent memory
855 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
856 * to give the application a choice between cached, but not coherent and
857 * coherent but uncached (WC though).
858 */
859 pMemoryProperties->memoryTypeCount = 2;
860 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
861 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
862 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
863 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
864 .heapIndex = 0,
865 };
866 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
867 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
868 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
869 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
870 .heapIndex = 0,
871 };
872 }
873
874 pMemoryProperties->memoryHeapCount = 1;
875 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
876 .size = physical_device->heap_size,
877 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
878 };
879 }
880
881 void anv_GetPhysicalDeviceMemoryProperties2KHR(
882 VkPhysicalDevice physicalDevice,
883 VkPhysicalDeviceMemoryProperties2KHR* pMemoryProperties)
884 {
885 anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
886 &pMemoryProperties->memoryProperties);
887
888 vk_foreach_struct(ext, pMemoryProperties->pNext) {
889 switch (ext->sType) {
890 default:
891 anv_debug_ignored_stype(ext->sType);
892 break;
893 }
894 }
895 }
896
897 PFN_vkVoidFunction anv_GetInstanceProcAddr(
898 VkInstance instance,
899 const char* pName)
900 {
901 return anv_lookup_entrypoint(NULL, pName);
902 }
903
904 /* With version 1+ of the loader interface the ICD should expose
905 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
906 */
907 PUBLIC
908 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
909 VkInstance instance,
910 const char* pName);
911
912 PUBLIC
913 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
914 VkInstance instance,
915 const char* pName)
916 {
917 return anv_GetInstanceProcAddr(instance, pName);
918 }
919
920 PFN_vkVoidFunction anv_GetDeviceProcAddr(
921 VkDevice _device,
922 const char* pName)
923 {
924 ANV_FROM_HANDLE(anv_device, device, _device);
925 return anv_lookup_entrypoint(&device->info, pName);
926 }
927
928 static void
929 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
930 {
931 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
932 queue->device = device;
933 queue->pool = &device->surface_state_pool;
934 }
935
936 static void
937 anv_queue_finish(struct anv_queue *queue)
938 {
939 }
940
941 static struct anv_state
942 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
943 {
944 struct anv_state state;
945
946 state = anv_state_pool_alloc(pool, size, align);
947 memcpy(state.map, p, size);
948
949 anv_state_flush(pool->block_pool->device, state);
950
951 return state;
952 }
953
954 struct gen8_border_color {
955 union {
956 float float32[4];
957 uint32_t uint32[4];
958 };
959 /* Pad out to 64 bytes */
960 uint32_t _pad[12];
961 };
962
963 static void
964 anv_device_init_border_colors(struct anv_device *device)
965 {
966 static const struct gen8_border_color border_colors[] = {
967 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
968 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
969 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
970 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
971 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
972 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
973 };
974
975 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
976 sizeof(border_colors), 64,
977 border_colors);
978 }
979
980 VkResult
981 anv_device_submit_simple_batch(struct anv_device *device,
982 struct anv_batch *batch)
983 {
984 struct drm_i915_gem_execbuffer2 execbuf;
985 struct drm_i915_gem_exec_object2 exec2_objects[1];
986 struct anv_bo bo, *exec_bos[1];
987 VkResult result = VK_SUCCESS;
988 uint32_t size;
989
990 /* Kernel driver requires 8 byte aligned batch length */
991 size = align_u32(batch->next - batch->start, 8);
992 result = anv_bo_pool_alloc(&device->batch_bo_pool, &bo, size);
993 if (result != VK_SUCCESS)
994 return result;
995
996 memcpy(bo.map, batch->start, size);
997 if (!device->info.has_llc)
998 anv_flush_range(bo.map, size);
999
1000 exec_bos[0] = &bo;
1001 exec2_objects[0].handle = bo.gem_handle;
1002 exec2_objects[0].relocation_count = 0;
1003 exec2_objects[0].relocs_ptr = 0;
1004 exec2_objects[0].alignment = 0;
1005 exec2_objects[0].offset = bo.offset;
1006 exec2_objects[0].flags = 0;
1007 exec2_objects[0].rsvd1 = 0;
1008 exec2_objects[0].rsvd2 = 0;
1009
1010 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
1011 execbuf.buffer_count = 1;
1012 execbuf.batch_start_offset = 0;
1013 execbuf.batch_len = size;
1014 execbuf.cliprects_ptr = 0;
1015 execbuf.num_cliprects = 0;
1016 execbuf.DR1 = 0;
1017 execbuf.DR4 = 0;
1018
1019 execbuf.flags =
1020 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1021 execbuf.rsvd1 = device->context_id;
1022 execbuf.rsvd2 = 0;
1023
1024 result = anv_device_execbuf(device, &execbuf, exec_bos);
1025 if (result != VK_SUCCESS)
1026 goto fail;
1027
1028 result = anv_device_wait(device, &bo, INT64_MAX);
1029
1030 fail:
1031 anv_bo_pool_free(&device->batch_bo_pool, &bo);
1032
1033 return result;
1034 }
1035
1036 VkResult anv_CreateDevice(
1037 VkPhysicalDevice physicalDevice,
1038 const VkDeviceCreateInfo* pCreateInfo,
1039 const VkAllocationCallbacks* pAllocator,
1040 VkDevice* pDevice)
1041 {
1042 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1043 VkResult result;
1044 struct anv_device *device;
1045
1046 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
1047
1048 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1049 bool found = false;
1050 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
1051 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
1052 device_extensions[j].extensionName) == 0) {
1053 found = true;
1054 break;
1055 }
1056 }
1057 if (!found)
1058 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
1059 }
1060
1061 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
1062 sizeof(*device), 8,
1063 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1064 if (!device)
1065 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1066
1067 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1068 device->instance = physical_device->instance;
1069 device->chipset_id = physical_device->chipset_id;
1070 device->lost = false;
1071
1072 if (pAllocator)
1073 device->alloc = *pAllocator;
1074 else
1075 device->alloc = physical_device->instance->alloc;
1076
1077 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1078 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
1079 if (device->fd == -1) {
1080 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1081 goto fail_device;
1082 }
1083
1084 device->context_id = anv_gem_create_context(device);
1085 if (device->context_id == -1) {
1086 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1087 goto fail_fd;
1088 }
1089
1090 device->info = physical_device->info;
1091 device->isl_dev = physical_device->isl_dev;
1092
1093 /* On Broadwell and later, we can use batch chaining to more efficiently
1094 * implement growing command buffers. Prior to Haswell, the kernel
1095 * command parser gets in the way and we have to fall back to growing
1096 * the batch.
1097 */
1098 device->can_chain_batches = device->info.gen >= 8;
1099
1100 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
1101 pCreateInfo->pEnabledFeatures->robustBufferAccess;
1102
1103 if (pthread_mutex_init(&device->mutex, NULL) != 0) {
1104 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1105 goto fail_context_id;
1106 }
1107
1108 pthread_condattr_t condattr;
1109 if (pthread_condattr_init(&condattr) != 0) {
1110 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1111 goto fail_mutex;
1112 }
1113 if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
1114 pthread_condattr_destroy(&condattr);
1115 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1116 goto fail_mutex;
1117 }
1118 if (pthread_cond_init(&device->queue_submit, NULL) != 0) {
1119 pthread_condattr_destroy(&condattr);
1120 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1121 goto fail_mutex;
1122 }
1123 pthread_condattr_destroy(&condattr);
1124
1125 anv_bo_pool_init(&device->batch_bo_pool, device);
1126
1127 result = anv_bo_cache_init(&device->bo_cache);
1128 if (result != VK_SUCCESS)
1129 goto fail_batch_bo_pool;
1130
1131 result = anv_block_pool_init(&device->dynamic_state_block_pool, device,
1132 16384);
1133 if (result != VK_SUCCESS)
1134 goto fail_bo_cache;
1135
1136 anv_state_pool_init(&device->dynamic_state_pool,
1137 &device->dynamic_state_block_pool);
1138
1139 result = anv_block_pool_init(&device->instruction_block_pool, device,
1140 1024 * 1024);
1141 if (result != VK_SUCCESS)
1142 goto fail_dynamic_state_pool;
1143
1144 anv_state_pool_init(&device->instruction_state_pool,
1145 &device->instruction_block_pool);
1146
1147 result = anv_block_pool_init(&device->surface_state_block_pool, device,
1148 4096);
1149 if (result != VK_SUCCESS)
1150 goto fail_instruction_state_pool;
1151
1152 anv_state_pool_init(&device->surface_state_pool,
1153 &device->surface_state_block_pool);
1154
1155 result = anv_bo_init_new(&device->workaround_bo, device, 1024);
1156 if (result != VK_SUCCESS)
1157 goto fail_surface_state_pool;
1158
1159 anv_scratch_pool_init(device, &device->scratch_pool);
1160
1161 anv_queue_init(device, &device->queue);
1162
1163 switch (device->info.gen) {
1164 case 7:
1165 if (!device->info.is_haswell)
1166 result = gen7_init_device_state(device);
1167 else
1168 result = gen75_init_device_state(device);
1169 break;
1170 case 8:
1171 result = gen8_init_device_state(device);
1172 break;
1173 case 9:
1174 result = gen9_init_device_state(device);
1175 break;
1176 default:
1177 /* Shouldn't get here as we don't create physical devices for any other
1178 * gens. */
1179 unreachable("unhandled gen");
1180 }
1181 if (result != VK_SUCCESS)
1182 goto fail_workaround_bo;
1183
1184 anv_device_init_blorp(device);
1185
1186 anv_device_init_border_colors(device);
1187
1188 *pDevice = anv_device_to_handle(device);
1189
1190 return VK_SUCCESS;
1191
1192 fail_workaround_bo:
1193 anv_queue_finish(&device->queue);
1194 anv_scratch_pool_finish(device, &device->scratch_pool);
1195 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1196 anv_gem_close(device, device->workaround_bo.gem_handle);
1197 fail_surface_state_pool:
1198 anv_state_pool_finish(&device->surface_state_pool);
1199 anv_block_pool_finish(&device->surface_state_block_pool);
1200 fail_instruction_state_pool:
1201 anv_state_pool_finish(&device->instruction_state_pool);
1202 anv_block_pool_finish(&device->instruction_block_pool);
1203 fail_dynamic_state_pool:
1204 anv_state_pool_finish(&device->dynamic_state_pool);
1205 anv_block_pool_finish(&device->dynamic_state_block_pool);
1206 fail_bo_cache:
1207 anv_bo_cache_finish(&device->bo_cache);
1208 fail_batch_bo_pool:
1209 anv_bo_pool_finish(&device->batch_bo_pool);
1210 pthread_cond_destroy(&device->queue_submit);
1211 fail_mutex:
1212 pthread_mutex_destroy(&device->mutex);
1213 fail_context_id:
1214 anv_gem_destroy_context(device, device->context_id);
1215 fail_fd:
1216 close(device->fd);
1217 fail_device:
1218 vk_free(&device->alloc, device);
1219
1220 return result;
1221 }
1222
1223 void anv_DestroyDevice(
1224 VkDevice _device,
1225 const VkAllocationCallbacks* pAllocator)
1226 {
1227 ANV_FROM_HANDLE(anv_device, device, _device);
1228
1229 if (!device)
1230 return;
1231
1232 anv_device_finish_blorp(device);
1233
1234 anv_queue_finish(&device->queue);
1235
1236 #ifdef HAVE_VALGRIND
1237 /* We only need to free these to prevent valgrind errors. The backing
1238 * BO will go away in a couple of lines so we don't actually leak.
1239 */
1240 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
1241 #endif
1242
1243 anv_scratch_pool_finish(device, &device->scratch_pool);
1244
1245 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1246 anv_gem_close(device, device->workaround_bo.gem_handle);
1247
1248 anv_state_pool_finish(&device->surface_state_pool);
1249 anv_block_pool_finish(&device->surface_state_block_pool);
1250 anv_state_pool_finish(&device->instruction_state_pool);
1251 anv_block_pool_finish(&device->instruction_block_pool);
1252 anv_state_pool_finish(&device->dynamic_state_pool);
1253 anv_block_pool_finish(&device->dynamic_state_block_pool);
1254
1255 anv_bo_cache_finish(&device->bo_cache);
1256
1257 anv_bo_pool_finish(&device->batch_bo_pool);
1258
1259 pthread_cond_destroy(&device->queue_submit);
1260 pthread_mutex_destroy(&device->mutex);
1261
1262 anv_gem_destroy_context(device, device->context_id);
1263
1264 close(device->fd);
1265
1266 vk_free(&device->alloc, device);
1267 }
1268
1269 VkResult anv_EnumerateInstanceExtensionProperties(
1270 const char* pLayerName,
1271 uint32_t* pPropertyCount,
1272 VkExtensionProperties* pProperties)
1273 {
1274 if (pProperties == NULL) {
1275 *pPropertyCount = ARRAY_SIZE(global_extensions);
1276 return VK_SUCCESS;
1277 }
1278
1279 *pPropertyCount = MIN2(*pPropertyCount, ARRAY_SIZE(global_extensions));
1280 typed_memcpy(pProperties, global_extensions, *pPropertyCount);
1281
1282 if (*pPropertyCount < ARRAY_SIZE(global_extensions))
1283 return VK_INCOMPLETE;
1284
1285 return VK_SUCCESS;
1286 }
1287
1288 VkResult anv_EnumerateDeviceExtensionProperties(
1289 VkPhysicalDevice physicalDevice,
1290 const char* pLayerName,
1291 uint32_t* pPropertyCount,
1292 VkExtensionProperties* pProperties)
1293 {
1294 if (pProperties == NULL) {
1295 *pPropertyCount = ARRAY_SIZE(device_extensions);
1296 return VK_SUCCESS;
1297 }
1298
1299 *pPropertyCount = MIN2(*pPropertyCount, ARRAY_SIZE(device_extensions));
1300 typed_memcpy(pProperties, device_extensions, *pPropertyCount);
1301
1302 if (*pPropertyCount < ARRAY_SIZE(device_extensions))
1303 return VK_INCOMPLETE;
1304
1305 return VK_SUCCESS;
1306 }
1307
1308 VkResult anv_EnumerateInstanceLayerProperties(
1309 uint32_t* pPropertyCount,
1310 VkLayerProperties* pProperties)
1311 {
1312 if (pProperties == NULL) {
1313 *pPropertyCount = 0;
1314 return VK_SUCCESS;
1315 }
1316
1317 /* None supported at this time */
1318 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1319 }
1320
1321 VkResult anv_EnumerateDeviceLayerProperties(
1322 VkPhysicalDevice physicalDevice,
1323 uint32_t* pPropertyCount,
1324 VkLayerProperties* pProperties)
1325 {
1326 if (pProperties == NULL) {
1327 *pPropertyCount = 0;
1328 return VK_SUCCESS;
1329 }
1330
1331 /* None supported at this time */
1332 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1333 }
1334
1335 void anv_GetDeviceQueue(
1336 VkDevice _device,
1337 uint32_t queueNodeIndex,
1338 uint32_t queueIndex,
1339 VkQueue* pQueue)
1340 {
1341 ANV_FROM_HANDLE(anv_device, device, _device);
1342
1343 assert(queueIndex == 0);
1344
1345 *pQueue = anv_queue_to_handle(&device->queue);
1346 }
1347
1348 VkResult
1349 anv_device_execbuf(struct anv_device *device,
1350 struct drm_i915_gem_execbuffer2 *execbuf,
1351 struct anv_bo **execbuf_bos)
1352 {
1353 int ret = anv_gem_execbuffer(device, execbuf);
1354 if (ret != 0) {
1355 /* We don't know the real error. */
1356 device->lost = true;
1357 return vk_errorf(VK_ERROR_DEVICE_LOST, "execbuf2 failed: %m");
1358 }
1359
1360 struct drm_i915_gem_exec_object2 *objects =
1361 (void *)(uintptr_t)execbuf->buffers_ptr;
1362 for (uint32_t k = 0; k < execbuf->buffer_count; k++)
1363 execbuf_bos[k]->offset = objects[k].offset;
1364
1365 return VK_SUCCESS;
1366 }
1367
1368 VkResult
1369 anv_device_query_status(struct anv_device *device)
1370 {
1371 /* This isn't likely as most of the callers of this function already check
1372 * for it. However, it doesn't hurt to check and it potentially lets us
1373 * avoid an ioctl.
1374 */
1375 if (unlikely(device->lost))
1376 return VK_ERROR_DEVICE_LOST;
1377
1378 uint32_t active, pending;
1379 int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
1380 if (ret == -1) {
1381 /* We don't know the real error. */
1382 device->lost = true;
1383 return vk_errorf(VK_ERROR_DEVICE_LOST, "get_reset_stats failed: %m");
1384 }
1385
1386 if (active) {
1387 device->lost = true;
1388 return vk_errorf(VK_ERROR_DEVICE_LOST,
1389 "GPU hung on one of our command buffers");
1390 } else if (pending) {
1391 device->lost = true;
1392 return vk_errorf(VK_ERROR_DEVICE_LOST,
1393 "GPU hung with commands in-flight");
1394 }
1395
1396 return VK_SUCCESS;
1397 }
1398
1399 VkResult
1400 anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
1401 {
1402 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1403 * Other usages of the BO (such as on different hardware) will not be
1404 * flagged as "busy" by this ioctl. Use with care.
1405 */
1406 int ret = anv_gem_busy(device, bo->gem_handle);
1407 if (ret == 1) {
1408 return VK_NOT_READY;
1409 } else if (ret == -1) {
1410 /* We don't know the real error. */
1411 device->lost = true;
1412 return vk_errorf(VK_ERROR_DEVICE_LOST, "gem wait failed: %m");
1413 }
1414
1415 /* Query for device status after the busy call. If the BO we're checking
1416 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1417 * client because it clearly doesn't have valid data. Yes, this most
1418 * likely means an ioctl, but we just did an ioctl to query the busy status
1419 * so it's no great loss.
1420 */
1421 return anv_device_query_status(device);
1422 }
1423
1424 VkResult
1425 anv_device_wait(struct anv_device *device, struct anv_bo *bo,
1426 int64_t timeout)
1427 {
1428 int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
1429 if (ret == -1 && errno == ETIME) {
1430 return VK_TIMEOUT;
1431 } else if (ret == -1) {
1432 /* We don't know the real error. */
1433 device->lost = true;
1434 return vk_errorf(VK_ERROR_DEVICE_LOST, "gem wait failed: %m");
1435 }
1436
1437 /* Query for device status after the wait. If the BO we're waiting on got
1438 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
1439 * because it clearly doesn't have valid data. Yes, this most likely means
1440 * an ioctl, but we just did an ioctl to wait so it's no great loss.
1441 */
1442 return anv_device_query_status(device);
1443 }
1444
1445 VkResult anv_QueueSubmit(
1446 VkQueue _queue,
1447 uint32_t submitCount,
1448 const VkSubmitInfo* pSubmits,
1449 VkFence _fence)
1450 {
1451 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1452 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1453 struct anv_device *device = queue->device;
1454
1455 /* Query for device status prior to submitting. Technically, we don't need
1456 * to do this. However, if we have a client that's submitting piles of
1457 * garbage, we would rather break as early as possible to keep the GPU
1458 * hanging contained. If we don't check here, we'll either be waiting for
1459 * the kernel to kick us or we'll have to wait until the client waits on a
1460 * fence before we actually know whether or not we've hung.
1461 */
1462 VkResult result = anv_device_query_status(device);
1463 if (result != VK_SUCCESS)
1464 return result;
1465
1466 /* We lock around QueueSubmit for three main reasons:
1467 *
1468 * 1) When a block pool is resized, we create a new gem handle with a
1469 * different size and, in the case of surface states, possibly a
1470 * different center offset but we re-use the same anv_bo struct when
1471 * we do so. If this happens in the middle of setting up an execbuf,
1472 * we could end up with our list of BOs out of sync with our list of
1473 * gem handles.
1474 *
1475 * 2) The algorithm we use for building the list of unique buffers isn't
1476 * thread-safe. While the client is supposed to syncronize around
1477 * QueueSubmit, this would be extremely difficult to debug if it ever
1478 * came up in the wild due to a broken app. It's better to play it
1479 * safe and just lock around QueueSubmit.
1480 *
1481 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1482 * userspace. Due to the fact that the surface state buffer is shared
1483 * between batches, we can't afford to have that happen from multiple
1484 * threads at the same time. Even though the user is supposed to
1485 * ensure this doesn't happen, we play it safe as in (2) above.
1486 *
1487 * Since the only other things that ever take the device lock such as block
1488 * pool resize only rarely happen, this will almost never be contended so
1489 * taking a lock isn't really an expensive operation in this case.
1490 */
1491 pthread_mutex_lock(&device->mutex);
1492
1493 for (uint32_t i = 0; i < submitCount; i++) {
1494 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
1495 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
1496 pSubmits[i].pCommandBuffers[j]);
1497 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1498 assert(!anv_batch_has_error(&cmd_buffer->batch));
1499
1500 result = anv_cmd_buffer_execbuf(device, cmd_buffer);
1501 if (result != VK_SUCCESS)
1502 goto out;
1503 }
1504 }
1505
1506 if (fence) {
1507 struct anv_bo *fence_bo = &fence->bo;
1508 result = anv_device_execbuf(device, &fence->execbuf, &fence_bo);
1509 if (result != VK_SUCCESS)
1510 goto out;
1511
1512 /* Update the fence and wake up any waiters */
1513 assert(fence->state == ANV_FENCE_STATE_RESET);
1514 fence->state = ANV_FENCE_STATE_SUBMITTED;
1515 pthread_cond_broadcast(&device->queue_submit);
1516 }
1517
1518 out:
1519 if (result != VK_SUCCESS) {
1520 /* In the case that something has gone wrong we may end up with an
1521 * inconsistent state from which it may not be trivial to recover.
1522 * For example, we might have computed address relocations and
1523 * any future attempt to re-submit this job will need to know about
1524 * this and avoid computing relocation addresses again.
1525 *
1526 * To avoid this sort of issues, we assume that if something was
1527 * wrong during submission we must already be in a really bad situation
1528 * anyway (such us being out of memory) and return
1529 * VK_ERROR_DEVICE_LOST to ensure that clients do not attempt to
1530 * submit the same job again to this device.
1531 */
1532 result = VK_ERROR_DEVICE_LOST;
1533 device->lost = true;
1534
1535 /* If we return VK_ERROR_DEVICE LOST here, we need to ensure that
1536 * vkWaitForFences() and vkGetFenceStatus() return a valid result
1537 * (VK_SUCCESS or VK_ERROR_DEVICE_LOST) in a finite amount of time.
1538 * Setting the fence status to SIGNALED ensures this will happen in
1539 * any case.
1540 */
1541 if (fence)
1542 fence->state = ANV_FENCE_STATE_SIGNALED;
1543 }
1544
1545 pthread_mutex_unlock(&device->mutex);
1546
1547 return result;
1548 }
1549
1550 VkResult anv_QueueWaitIdle(
1551 VkQueue _queue)
1552 {
1553 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1554
1555 return anv_DeviceWaitIdle(anv_device_to_handle(queue->device));
1556 }
1557
1558 VkResult anv_DeviceWaitIdle(
1559 VkDevice _device)
1560 {
1561 ANV_FROM_HANDLE(anv_device, device, _device);
1562 if (unlikely(device->lost))
1563 return VK_ERROR_DEVICE_LOST;
1564
1565 struct anv_batch batch;
1566
1567 uint32_t cmds[8];
1568 batch.start = batch.next = cmds;
1569 batch.end = (void *) cmds + sizeof(cmds);
1570
1571 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1572 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1573
1574 return anv_device_submit_simple_batch(device, &batch);
1575 }
1576
1577 VkResult
1578 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
1579 {
1580 uint32_t gem_handle = anv_gem_create(device, size);
1581 if (!gem_handle)
1582 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1583
1584 anv_bo_init(bo, gem_handle, size);
1585
1586 if (device->instance->physicalDevice.supports_48bit_addresses)
1587 bo->flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1588
1589 if (device->instance->physicalDevice.has_exec_async)
1590 bo->flags |= EXEC_OBJECT_ASYNC;
1591
1592 return VK_SUCCESS;
1593 }
1594
1595 VkResult anv_AllocateMemory(
1596 VkDevice _device,
1597 const VkMemoryAllocateInfo* pAllocateInfo,
1598 const VkAllocationCallbacks* pAllocator,
1599 VkDeviceMemory* pMem)
1600 {
1601 ANV_FROM_HANDLE(anv_device, device, _device);
1602 struct anv_device_memory *mem;
1603 VkResult result;
1604
1605 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1606
1607 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
1608 assert(pAllocateInfo->allocationSize > 0);
1609
1610 /* We support exactly one memory heap. */
1611 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1612 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
1613
1614 /* The kernel relocation API has a limitation of a 32-bit delta value
1615 * applied to the address before it is written which, in spite of it being
1616 * unsigned, is treated as signed . Because of the way that this maps to
1617 * the Vulkan API, we cannot handle an offset into a buffer that does not
1618 * fit into a signed 32 bits. The only mechanism we have for dealing with
1619 * this at the moment is to limit all VkDeviceMemory objects to a maximum
1620 * of 2GB each. The Vulkan spec allows us to do this:
1621 *
1622 * "Some platforms may have a limit on the maximum size of a single
1623 * allocation. For example, certain systems may fail to create
1624 * allocations with a size greater than or equal to 4GB. Such a limit is
1625 * implementation-dependent, and if such a failure occurs then the error
1626 * VK_ERROR_OUT_OF_DEVICE_MEMORY should be returned."
1627 *
1628 * We don't use vk_error here because it's not an error so much as an
1629 * indication to the application that the allocation is too large.
1630 */
1631 if (pAllocateInfo->allocationSize > (1ull << 31))
1632 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1633
1634 /* FINISHME: Fail if allocation request exceeds heap size. */
1635
1636 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1637 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1638 if (mem == NULL)
1639 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1640
1641 /* The kernel is going to give us whole pages anyway */
1642 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1643
1644 result = anv_bo_cache_alloc(device, &device->bo_cache,
1645 alloc_size, &mem->bo);
1646 if (result != VK_SUCCESS)
1647 goto fail;
1648
1649 mem->type_index = pAllocateInfo->memoryTypeIndex;
1650
1651 mem->map = NULL;
1652 mem->map_size = 0;
1653
1654 *pMem = anv_device_memory_to_handle(mem);
1655
1656 return VK_SUCCESS;
1657
1658 fail:
1659 vk_free2(&device->alloc, pAllocator, mem);
1660
1661 return result;
1662 }
1663
1664 void anv_FreeMemory(
1665 VkDevice _device,
1666 VkDeviceMemory _mem,
1667 const VkAllocationCallbacks* pAllocator)
1668 {
1669 ANV_FROM_HANDLE(anv_device, device, _device);
1670 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1671
1672 if (mem == NULL)
1673 return;
1674
1675 if (mem->map)
1676 anv_UnmapMemory(_device, _mem);
1677
1678 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
1679
1680 vk_free2(&device->alloc, pAllocator, mem);
1681 }
1682
1683 VkResult anv_MapMemory(
1684 VkDevice _device,
1685 VkDeviceMemory _memory,
1686 VkDeviceSize offset,
1687 VkDeviceSize size,
1688 VkMemoryMapFlags flags,
1689 void** ppData)
1690 {
1691 ANV_FROM_HANDLE(anv_device, device, _device);
1692 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1693
1694 if (mem == NULL) {
1695 *ppData = NULL;
1696 return VK_SUCCESS;
1697 }
1698
1699 if (size == VK_WHOLE_SIZE)
1700 size = mem->bo->size - offset;
1701
1702 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1703 *
1704 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1705 * assert(size != 0);
1706 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1707 * equal to the size of the memory minus offset
1708 */
1709 assert(size > 0);
1710 assert(offset + size <= mem->bo->size);
1711
1712 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1713 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1714 * at a time is valid. We could just mmap up front and return an offset
1715 * pointer here, but that may exhaust virtual memory on 32 bit
1716 * userspace. */
1717
1718 uint32_t gem_flags = 0;
1719 if (!device->info.has_llc && mem->type_index == 0)
1720 gem_flags |= I915_MMAP_WC;
1721
1722 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1723 uint64_t map_offset = offset & ~4095ull;
1724 assert(offset >= map_offset);
1725 uint64_t map_size = (offset + size) - map_offset;
1726
1727 /* Let's map whole pages */
1728 map_size = align_u64(map_size, 4096);
1729
1730 void *map = anv_gem_mmap(device, mem->bo->gem_handle,
1731 map_offset, map_size, gem_flags);
1732 if (map == MAP_FAILED)
1733 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
1734
1735 mem->map = map;
1736 mem->map_size = map_size;
1737
1738 *ppData = mem->map + (offset - map_offset);
1739
1740 return VK_SUCCESS;
1741 }
1742
1743 void anv_UnmapMemory(
1744 VkDevice _device,
1745 VkDeviceMemory _memory)
1746 {
1747 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1748
1749 if (mem == NULL)
1750 return;
1751
1752 anv_gem_munmap(mem->map, mem->map_size);
1753
1754 mem->map = NULL;
1755 mem->map_size = 0;
1756 }
1757
1758 static void
1759 clflush_mapped_ranges(struct anv_device *device,
1760 uint32_t count,
1761 const VkMappedMemoryRange *ranges)
1762 {
1763 for (uint32_t i = 0; i < count; i++) {
1764 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1765 if (ranges[i].offset >= mem->map_size)
1766 continue;
1767
1768 anv_clflush_range(mem->map + ranges[i].offset,
1769 MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
1770 }
1771 }
1772
1773 VkResult anv_FlushMappedMemoryRanges(
1774 VkDevice _device,
1775 uint32_t memoryRangeCount,
1776 const VkMappedMemoryRange* pMemoryRanges)
1777 {
1778 ANV_FROM_HANDLE(anv_device, device, _device);
1779
1780 if (device->info.has_llc)
1781 return VK_SUCCESS;
1782
1783 /* Make sure the writes we're flushing have landed. */
1784 __builtin_ia32_mfence();
1785
1786 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1787
1788 return VK_SUCCESS;
1789 }
1790
1791 VkResult anv_InvalidateMappedMemoryRanges(
1792 VkDevice _device,
1793 uint32_t memoryRangeCount,
1794 const VkMappedMemoryRange* pMemoryRanges)
1795 {
1796 ANV_FROM_HANDLE(anv_device, device, _device);
1797
1798 if (device->info.has_llc)
1799 return VK_SUCCESS;
1800
1801 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1802
1803 /* Make sure no reads get moved up above the invalidate. */
1804 __builtin_ia32_mfence();
1805
1806 return VK_SUCCESS;
1807 }
1808
1809 void anv_GetBufferMemoryRequirements(
1810 VkDevice _device,
1811 VkBuffer _buffer,
1812 VkMemoryRequirements* pMemoryRequirements)
1813 {
1814 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1815 ANV_FROM_HANDLE(anv_device, device, _device);
1816
1817 /* The Vulkan spec (git aaed022) says:
1818 *
1819 * memoryTypeBits is a bitfield and contains one bit set for every
1820 * supported memory type for the resource. The bit `1<<i` is set if and
1821 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1822 * structure for the physical device is supported.
1823 *
1824 * We support exactly one memory type on LLC, two on non-LLC.
1825 */
1826 pMemoryRequirements->memoryTypeBits = device->info.has_llc ? 1 : 3;
1827
1828 pMemoryRequirements->size = buffer->size;
1829 pMemoryRequirements->alignment = 16;
1830 }
1831
1832 void anv_GetImageMemoryRequirements(
1833 VkDevice _device,
1834 VkImage _image,
1835 VkMemoryRequirements* pMemoryRequirements)
1836 {
1837 ANV_FROM_HANDLE(anv_image, image, _image);
1838 ANV_FROM_HANDLE(anv_device, device, _device);
1839
1840 /* The Vulkan spec (git aaed022) says:
1841 *
1842 * memoryTypeBits is a bitfield and contains one bit set for every
1843 * supported memory type for the resource. The bit `1<<i` is set if and
1844 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1845 * structure for the physical device is supported.
1846 *
1847 * We support exactly one memory type on LLC, two on non-LLC.
1848 */
1849 pMemoryRequirements->memoryTypeBits = device->info.has_llc ? 1 : 3;
1850
1851 pMemoryRequirements->size = image->size;
1852 pMemoryRequirements->alignment = image->alignment;
1853 }
1854
1855 void anv_GetImageSparseMemoryRequirements(
1856 VkDevice device,
1857 VkImage image,
1858 uint32_t* pSparseMemoryRequirementCount,
1859 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1860 {
1861 *pSparseMemoryRequirementCount = 0;
1862 }
1863
1864 void anv_GetDeviceMemoryCommitment(
1865 VkDevice device,
1866 VkDeviceMemory memory,
1867 VkDeviceSize* pCommittedMemoryInBytes)
1868 {
1869 *pCommittedMemoryInBytes = 0;
1870 }
1871
1872 VkResult anv_BindBufferMemory(
1873 VkDevice device,
1874 VkBuffer _buffer,
1875 VkDeviceMemory _memory,
1876 VkDeviceSize memoryOffset)
1877 {
1878 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1879 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1880
1881 if (mem) {
1882 buffer->bo = mem->bo;
1883 buffer->offset = memoryOffset;
1884 } else {
1885 buffer->bo = NULL;
1886 buffer->offset = 0;
1887 }
1888
1889 return VK_SUCCESS;
1890 }
1891
1892 VkResult anv_QueueBindSparse(
1893 VkQueue _queue,
1894 uint32_t bindInfoCount,
1895 const VkBindSparseInfo* pBindInfo,
1896 VkFence fence)
1897 {
1898 ANV_FROM_HANDLE(anv_queue, queue, _queue);
1899 if (unlikely(queue->device->lost))
1900 return VK_ERROR_DEVICE_LOST;
1901
1902 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
1903 }
1904
1905 VkResult anv_CreateFence(
1906 VkDevice _device,
1907 const VkFenceCreateInfo* pCreateInfo,
1908 const VkAllocationCallbacks* pAllocator,
1909 VkFence* pFence)
1910 {
1911 ANV_FROM_HANDLE(anv_device, device, _device);
1912 struct anv_bo fence_bo;
1913 struct anv_fence *fence;
1914 struct anv_batch batch;
1915 VkResult result;
1916
1917 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1918
1919 result = anv_bo_pool_alloc(&device->batch_bo_pool, &fence_bo, 4096);
1920 if (result != VK_SUCCESS)
1921 return result;
1922
1923 /* Fences are small. Just store the CPU data structure in the BO. */
1924 fence = fence_bo.map;
1925 fence->bo = fence_bo;
1926
1927 /* Place the batch after the CPU data but on its own cache line. */
1928 const uint32_t batch_offset = align_u32(sizeof(*fence), CACHELINE_SIZE);
1929 batch.next = batch.start = fence->bo.map + batch_offset;
1930 batch.end = fence->bo.map + fence->bo.size;
1931 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1932 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1933
1934 if (!device->info.has_llc) {
1935 assert(((uintptr_t) batch.start & CACHELINE_MASK) == 0);
1936 assert(batch.next - batch.start <= CACHELINE_SIZE);
1937 __builtin_ia32_mfence();
1938 __builtin_ia32_clflush(batch.start);
1939 }
1940
1941 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1942 fence->exec2_objects[0].relocation_count = 0;
1943 fence->exec2_objects[0].relocs_ptr = 0;
1944 fence->exec2_objects[0].alignment = 0;
1945 fence->exec2_objects[0].offset = fence->bo.offset;
1946 fence->exec2_objects[0].flags = 0;
1947 fence->exec2_objects[0].rsvd1 = 0;
1948 fence->exec2_objects[0].rsvd2 = 0;
1949
1950 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1951 fence->execbuf.buffer_count = 1;
1952 fence->execbuf.batch_start_offset = batch.start - fence->bo.map;
1953 fence->execbuf.batch_len = batch.next - batch.start;
1954 fence->execbuf.cliprects_ptr = 0;
1955 fence->execbuf.num_cliprects = 0;
1956 fence->execbuf.DR1 = 0;
1957 fence->execbuf.DR4 = 0;
1958
1959 fence->execbuf.flags =
1960 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1961 fence->execbuf.rsvd1 = device->context_id;
1962 fence->execbuf.rsvd2 = 0;
1963
1964 if (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) {
1965 fence->state = ANV_FENCE_STATE_SIGNALED;
1966 } else {
1967 fence->state = ANV_FENCE_STATE_RESET;
1968 }
1969
1970 *pFence = anv_fence_to_handle(fence);
1971
1972 return VK_SUCCESS;
1973 }
1974
1975 void anv_DestroyFence(
1976 VkDevice _device,
1977 VkFence _fence,
1978 const VkAllocationCallbacks* pAllocator)
1979 {
1980 ANV_FROM_HANDLE(anv_device, device, _device);
1981 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1982
1983 if (!fence)
1984 return;
1985
1986 assert(fence->bo.map == fence);
1987 anv_bo_pool_free(&device->batch_bo_pool, &fence->bo);
1988 }
1989
1990 VkResult anv_ResetFences(
1991 VkDevice _device,
1992 uint32_t fenceCount,
1993 const VkFence* pFences)
1994 {
1995 for (uint32_t i = 0; i < fenceCount; i++) {
1996 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1997 fence->state = ANV_FENCE_STATE_RESET;
1998 }
1999
2000 return VK_SUCCESS;
2001 }
2002
2003 VkResult anv_GetFenceStatus(
2004 VkDevice _device,
2005 VkFence _fence)
2006 {
2007 ANV_FROM_HANDLE(anv_device, device, _device);
2008 ANV_FROM_HANDLE(anv_fence, fence, _fence);
2009
2010 if (unlikely(device->lost))
2011 return VK_ERROR_DEVICE_LOST;
2012
2013 switch (fence->state) {
2014 case ANV_FENCE_STATE_RESET:
2015 /* If it hasn't even been sent off to the GPU yet, it's not ready */
2016 return VK_NOT_READY;
2017
2018 case ANV_FENCE_STATE_SIGNALED:
2019 /* It's been signaled, return success */
2020 return VK_SUCCESS;
2021
2022 case ANV_FENCE_STATE_SUBMITTED: {
2023 VkResult result = anv_device_bo_busy(device, &fence->bo);
2024 if (result == VK_SUCCESS) {
2025 fence->state = ANV_FENCE_STATE_SIGNALED;
2026 return VK_SUCCESS;
2027 } else {
2028 return result;
2029 }
2030 }
2031 default:
2032 unreachable("Invalid fence status");
2033 }
2034 }
2035
2036 #define NSEC_PER_SEC 1000000000
2037 #define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)
2038
2039 VkResult anv_WaitForFences(
2040 VkDevice _device,
2041 uint32_t fenceCount,
2042 const VkFence* pFences,
2043 VkBool32 waitAll,
2044 uint64_t _timeout)
2045 {
2046 ANV_FROM_HANDLE(anv_device, device, _device);
2047 int ret;
2048
2049 if (unlikely(device->lost))
2050 return VK_ERROR_DEVICE_LOST;
2051
2052 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
2053 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
2054 * for a couple of kernel releases. Since there's no way to know
2055 * whether or not the kernel we're using is one of the broken ones, the
2056 * best we can do is to clamp the timeout to INT64_MAX. This limits the
2057 * maximum timeout from 584 years to 292 years - likely not a big deal.
2058 */
2059 int64_t timeout = MIN2(_timeout, INT64_MAX);
2060
2061 VkResult result = VK_SUCCESS;
2062 uint32_t pending_fences = fenceCount;
2063 while (pending_fences) {
2064 pending_fences = 0;
2065 bool signaled_fences = false;
2066 for (uint32_t i = 0; i < fenceCount; i++) {
2067 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
2068 switch (fence->state) {
2069 case ANV_FENCE_STATE_RESET:
2070 /* This fence hasn't been submitted yet, we'll catch it the next
2071 * time around. Yes, this may mean we dead-loop but, short of
2072 * lots of locking and a condition variable, there's not much that
2073 * we can do about that.
2074 */
2075 pending_fences++;
2076 continue;
2077
2078 case ANV_FENCE_STATE_SIGNALED:
2079 /* This fence is not pending. If waitAll isn't set, we can return
2080 * early. Otherwise, we have to keep going.
2081 */
2082 if (!waitAll) {
2083 result = VK_SUCCESS;
2084 goto done;
2085 }
2086 continue;
2087
2088 case ANV_FENCE_STATE_SUBMITTED:
2089 /* These are the fences we really care about. Go ahead and wait
2090 * on it until we hit a timeout.
2091 */
2092 result = anv_device_wait(device, &fence->bo, timeout);
2093 switch (result) {
2094 case VK_SUCCESS:
2095 fence->state = ANV_FENCE_STATE_SIGNALED;
2096 signaled_fences = true;
2097 if (!waitAll)
2098 goto done;
2099 break;
2100
2101 case VK_TIMEOUT:
2102 goto done;
2103
2104 default:
2105 return result;
2106 }
2107 }
2108 }
2109
2110 if (pending_fences && !signaled_fences) {
2111 /* If we've hit this then someone decided to vkWaitForFences before
2112 * they've actually submitted any of them to a queue. This is a
2113 * fairly pessimal case, so it's ok to lock here and use a standard
2114 * pthreads condition variable.
2115 */
2116 pthread_mutex_lock(&device->mutex);
2117
2118 /* It's possible that some of the fences have changed state since the
2119 * last time we checked. Now that we have the lock, check for
2120 * pending fences again and don't wait if it's changed.
2121 */
2122 uint32_t now_pending_fences = 0;
2123 for (uint32_t i = 0; i < fenceCount; i++) {
2124 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
2125 if (fence->state == ANV_FENCE_STATE_RESET)
2126 now_pending_fences++;
2127 }
2128 assert(now_pending_fences <= pending_fences);
2129
2130 if (now_pending_fences == pending_fences) {
2131 struct timespec before;
2132 clock_gettime(CLOCK_MONOTONIC, &before);
2133
2134 uint32_t abs_nsec = before.tv_nsec + timeout % NSEC_PER_SEC;
2135 uint64_t abs_sec = before.tv_sec + (abs_nsec / NSEC_PER_SEC) +
2136 (timeout / NSEC_PER_SEC);
2137 abs_nsec %= NSEC_PER_SEC;
2138
2139 /* Avoid roll-over in tv_sec on 32-bit systems if the user
2140 * provided timeout is UINT64_MAX
2141 */
2142 struct timespec abstime;
2143 abstime.tv_nsec = abs_nsec;
2144 abstime.tv_sec = MIN2(abs_sec, INT_TYPE_MAX(abstime.tv_sec));
2145
2146 ret = pthread_cond_timedwait(&device->queue_submit,
2147 &device->mutex, &abstime);
2148 assert(ret != EINVAL);
2149
2150 struct timespec after;
2151 clock_gettime(CLOCK_MONOTONIC, &after);
2152 uint64_t time_elapsed =
2153 ((uint64_t)after.tv_sec * NSEC_PER_SEC + after.tv_nsec) -
2154 ((uint64_t)before.tv_sec * NSEC_PER_SEC + before.tv_nsec);
2155
2156 if (time_elapsed >= timeout) {
2157 pthread_mutex_unlock(&device->mutex);
2158 result = VK_TIMEOUT;
2159 goto done;
2160 }
2161
2162 timeout -= time_elapsed;
2163 }
2164
2165 pthread_mutex_unlock(&device->mutex);
2166 }
2167 }
2168
2169 done:
2170 if (unlikely(device->lost))
2171 return VK_ERROR_DEVICE_LOST;
2172
2173 return result;
2174 }
2175
2176 // Queue semaphore functions
2177
2178 VkResult anv_CreateSemaphore(
2179 VkDevice device,
2180 const VkSemaphoreCreateInfo* pCreateInfo,
2181 const VkAllocationCallbacks* pAllocator,
2182 VkSemaphore* pSemaphore)
2183 {
2184 /* The DRM execbuffer ioctl always execute in-oder, even between different
2185 * rings. As such, there's nothing to do for the user space semaphore.
2186 */
2187
2188 *pSemaphore = (VkSemaphore)1;
2189
2190 return VK_SUCCESS;
2191 }
2192
2193 void anv_DestroySemaphore(
2194 VkDevice device,
2195 VkSemaphore semaphore,
2196 const VkAllocationCallbacks* pAllocator)
2197 {
2198 }
2199
2200 // Event functions
2201
2202 VkResult anv_CreateEvent(
2203 VkDevice _device,
2204 const VkEventCreateInfo* pCreateInfo,
2205 const VkAllocationCallbacks* pAllocator,
2206 VkEvent* pEvent)
2207 {
2208 ANV_FROM_HANDLE(anv_device, device, _device);
2209 struct anv_state state;
2210 struct anv_event *event;
2211
2212 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
2213
2214 state = anv_state_pool_alloc(&device->dynamic_state_pool,
2215 sizeof(*event), 8);
2216 event = state.map;
2217 event->state = state;
2218 event->semaphore = VK_EVENT_RESET;
2219
2220 if (!device->info.has_llc) {
2221 /* Make sure the writes we're flushing have landed. */
2222 __builtin_ia32_mfence();
2223 __builtin_ia32_clflush(event);
2224 }
2225
2226 *pEvent = anv_event_to_handle(event);
2227
2228 return VK_SUCCESS;
2229 }
2230
2231 void anv_DestroyEvent(
2232 VkDevice _device,
2233 VkEvent _event,
2234 const VkAllocationCallbacks* pAllocator)
2235 {
2236 ANV_FROM_HANDLE(anv_device, device, _device);
2237 ANV_FROM_HANDLE(anv_event, event, _event);
2238
2239 if (!event)
2240 return;
2241
2242 anv_state_pool_free(&device->dynamic_state_pool, event->state);
2243 }
2244
2245 VkResult anv_GetEventStatus(
2246 VkDevice _device,
2247 VkEvent _event)
2248 {
2249 ANV_FROM_HANDLE(anv_device, device, _device);
2250 ANV_FROM_HANDLE(anv_event, event, _event);
2251
2252 if (unlikely(device->lost))
2253 return VK_ERROR_DEVICE_LOST;
2254
2255 if (!device->info.has_llc) {
2256 /* Invalidate read cache before reading event written by GPU. */
2257 __builtin_ia32_clflush(event);
2258 __builtin_ia32_mfence();
2259
2260 }
2261
2262 return event->semaphore;
2263 }
2264
2265 VkResult anv_SetEvent(
2266 VkDevice _device,
2267 VkEvent _event)
2268 {
2269 ANV_FROM_HANDLE(anv_device, device, _device);
2270 ANV_FROM_HANDLE(anv_event, event, _event);
2271
2272 event->semaphore = VK_EVENT_SET;
2273
2274 if (!device->info.has_llc) {
2275 /* Make sure the writes we're flushing have landed. */
2276 __builtin_ia32_mfence();
2277 __builtin_ia32_clflush(event);
2278 }
2279
2280 return VK_SUCCESS;
2281 }
2282
2283 VkResult anv_ResetEvent(
2284 VkDevice _device,
2285 VkEvent _event)
2286 {
2287 ANV_FROM_HANDLE(anv_device, device, _device);
2288 ANV_FROM_HANDLE(anv_event, event, _event);
2289
2290 event->semaphore = VK_EVENT_RESET;
2291
2292 if (!device->info.has_llc) {
2293 /* Make sure the writes we're flushing have landed. */
2294 __builtin_ia32_mfence();
2295 __builtin_ia32_clflush(event);
2296 }
2297
2298 return VK_SUCCESS;
2299 }
2300
2301 // Buffer functions
2302
2303 VkResult anv_CreateBuffer(
2304 VkDevice _device,
2305 const VkBufferCreateInfo* pCreateInfo,
2306 const VkAllocationCallbacks* pAllocator,
2307 VkBuffer* pBuffer)
2308 {
2309 ANV_FROM_HANDLE(anv_device, device, _device);
2310 struct anv_buffer *buffer;
2311
2312 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2313
2314 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2315 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2316 if (buffer == NULL)
2317 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2318
2319 buffer->size = pCreateInfo->size;
2320 buffer->usage = pCreateInfo->usage;
2321 buffer->bo = NULL;
2322 buffer->offset = 0;
2323
2324 *pBuffer = anv_buffer_to_handle(buffer);
2325
2326 return VK_SUCCESS;
2327 }
2328
2329 void anv_DestroyBuffer(
2330 VkDevice _device,
2331 VkBuffer _buffer,
2332 const VkAllocationCallbacks* pAllocator)
2333 {
2334 ANV_FROM_HANDLE(anv_device, device, _device);
2335 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2336
2337 if (!buffer)
2338 return;
2339
2340 vk_free2(&device->alloc, pAllocator, buffer);
2341 }
2342
2343 void
2344 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
2345 enum isl_format format,
2346 uint32_t offset, uint32_t range, uint32_t stride)
2347 {
2348 isl_buffer_fill_state(&device->isl_dev, state.map,
2349 .address = offset,
2350 .mocs = device->default_mocs,
2351 .size = range,
2352 .format = format,
2353 .stride = stride);
2354
2355 anv_state_flush(device, state);
2356 }
2357
2358 void anv_DestroySampler(
2359 VkDevice _device,
2360 VkSampler _sampler,
2361 const VkAllocationCallbacks* pAllocator)
2362 {
2363 ANV_FROM_HANDLE(anv_device, device, _device);
2364 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
2365
2366 if (!sampler)
2367 return;
2368
2369 vk_free2(&device->alloc, pAllocator, sampler);
2370 }
2371
2372 VkResult anv_CreateFramebuffer(
2373 VkDevice _device,
2374 const VkFramebufferCreateInfo* pCreateInfo,
2375 const VkAllocationCallbacks* pAllocator,
2376 VkFramebuffer* pFramebuffer)
2377 {
2378 ANV_FROM_HANDLE(anv_device, device, _device);
2379 struct anv_framebuffer *framebuffer;
2380
2381 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2382
2383 size_t size = sizeof(*framebuffer) +
2384 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
2385 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2386 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2387 if (framebuffer == NULL)
2388 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2389
2390 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2391 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2392 VkImageView _iview = pCreateInfo->pAttachments[i];
2393 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
2394 }
2395
2396 framebuffer->width = pCreateInfo->width;
2397 framebuffer->height = pCreateInfo->height;
2398 framebuffer->layers = pCreateInfo->layers;
2399
2400 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
2401
2402 return VK_SUCCESS;
2403 }
2404
2405 void anv_DestroyFramebuffer(
2406 VkDevice _device,
2407 VkFramebuffer _fb,
2408 const VkAllocationCallbacks* pAllocator)
2409 {
2410 ANV_FROM_HANDLE(anv_device, device, _device);
2411 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
2412
2413 if (!fb)
2414 return;
2415
2416 vk_free2(&device->alloc, pAllocator, fb);
2417 }
2418
2419 /* vk_icd.h does not declare this function, so we declare it here to
2420 * suppress Wmissing-prototypes.
2421 */
2422 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2423 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
2424
2425 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2426 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
2427 {
2428 /* For the full details on loader interface versioning, see
2429 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2430 * What follows is a condensed summary, to help you navigate the large and
2431 * confusing official doc.
2432 *
2433 * - Loader interface v0 is incompatible with later versions. We don't
2434 * support it.
2435 *
2436 * - In loader interface v1:
2437 * - The first ICD entrypoint called by the loader is
2438 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2439 * entrypoint.
2440 * - The ICD must statically expose no other Vulkan symbol unless it is
2441 * linked with -Bsymbolic.
2442 * - Each dispatchable Vulkan handle created by the ICD must be
2443 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2444 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2445 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2446 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2447 * such loader-managed surfaces.
2448 *
2449 * - Loader interface v2 differs from v1 in:
2450 * - The first ICD entrypoint called by the loader is
2451 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2452 * statically expose this entrypoint.
2453 *
2454 * - Loader interface v3 differs from v2 in:
2455 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2456 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2457 * because the loader no longer does so.
2458 */
2459 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2460 return VK_SUCCESS;
2461 }