i965, anv: Add extra unused character in disk_cache renderer temp string
[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 #include <drm_fourcc.h>
33
34 #include "anv_private.h"
35 #include "util/strtod.h"
36 #include "util/debug.h"
37 #include "util/build_id.h"
38 #include "util/disk_cache.h"
39 #include "util/mesa-sha1.h"
40 #include "vk_util.h"
41 #include "common/gen_defines.h"
42
43 #include "genxml/gen7_pack.h"
44
45 static void
46 compiler_debug_log(void *data, const char *fmt, ...)
47 { }
48
49 static void
50 compiler_perf_log(void *data, const char *fmt, ...)
51 {
52 va_list args;
53 va_start(args, fmt);
54
55 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
56 intel_logd_v(fmt, args);
57
58 va_end(args);
59 }
60
61 static VkResult
62 anv_compute_heap_size(int fd, uint64_t gtt_size, uint64_t *heap_size)
63 {
64 /* Query the total ram from the system */
65 struct sysinfo info;
66 sysinfo(&info);
67
68 uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit;
69
70 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
71 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
72 */
73 uint64_t available_ram;
74 if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
75 available_ram = total_ram / 2;
76 else
77 available_ram = total_ram * 3 / 4;
78
79 /* We also want to leave some padding for things we allocate in the driver,
80 * so don't go over 3/4 of the GTT either.
81 */
82 uint64_t available_gtt = gtt_size * 3 / 4;
83
84 *heap_size = MIN2(available_ram, available_gtt);
85
86 return VK_SUCCESS;
87 }
88
89 static VkResult
90 anv_physical_device_init_heaps(struct anv_physical_device *device, int fd)
91 {
92 uint64_t gtt_size;
93 if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
94 &gtt_size) == -1) {
95 /* If, for whatever reason, we can't actually get the GTT size from the
96 * kernel (too old?) fall back to the aperture size.
97 */
98 anv_perf_warn(NULL, NULL,
99 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
100
101 if (anv_gem_get_aperture(fd, &gtt_size) == -1) {
102 return vk_errorf(NULL, NULL, VK_ERROR_INITIALIZATION_FAILED,
103 "failed to get aperture size: %m");
104 }
105 }
106
107 device->supports_48bit_addresses = (device->info.gen >= 8) &&
108 gtt_size > (4ULL << 30 /* GiB */);
109
110 uint64_t heap_size = 0;
111 VkResult result = anv_compute_heap_size(fd, gtt_size, &heap_size);
112 if (result != VK_SUCCESS)
113 return result;
114
115 if (heap_size > (2ull << 30) && !device->supports_48bit_addresses) {
116 /* When running with an overridden PCI ID, we may get a GTT size from
117 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
118 * address support can still fail. Just clamp the address space size to
119 * 2 GiB if we don't have 48-bit support.
120 */
121 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
122 "not support for 48-bit addresses",
123 __FILE__, __LINE__);
124 heap_size = 2ull << 30;
125 }
126
127 if (heap_size <= 3ull * (1ull << 30)) {
128 /* In this case, everything fits nicely into the 32-bit address space,
129 * so there's no need for supporting 48bit addresses on client-allocated
130 * memory objects.
131 */
132 device->memory.heap_count = 1;
133 device->memory.heaps[0] = (struct anv_memory_heap) {
134 .size = heap_size,
135 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
136 .supports_48bit_addresses = false,
137 };
138 } else {
139 /* Not everything will fit nicely into a 32-bit address space. In this
140 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
141 * larger 48-bit heap. If we're in this case, then we have a total heap
142 * size larger than 3GiB which most likely means they have 8 GiB of
143 * video memory and so carving off 1 GiB for the 32-bit heap should be
144 * reasonable.
145 */
146 const uint64_t heap_size_32bit = 1ull << 30;
147 const uint64_t heap_size_48bit = heap_size - heap_size_32bit;
148
149 assert(device->supports_48bit_addresses);
150
151 device->memory.heap_count = 2;
152 device->memory.heaps[0] = (struct anv_memory_heap) {
153 .size = heap_size_48bit,
154 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
155 .supports_48bit_addresses = true,
156 };
157 device->memory.heaps[1] = (struct anv_memory_heap) {
158 .size = heap_size_32bit,
159 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
160 .supports_48bit_addresses = false,
161 };
162 }
163
164 uint32_t type_count = 0;
165 for (uint32_t heap = 0; heap < device->memory.heap_count; heap++) {
166 uint32_t valid_buffer_usage = ~0;
167
168 /* There appears to be a hardware issue in the VF cache where it only
169 * considers the bottom 32 bits of memory addresses. If you happen to
170 * have two vertex buffers which get placed exactly 4 GiB apart and use
171 * them in back-to-back draw calls, you can get collisions. In order to
172 * solve this problem, we require vertex and index buffers be bound to
173 * memory allocated out of the 32-bit heap.
174 */
175 if (device->memory.heaps[heap].supports_48bit_addresses) {
176 valid_buffer_usage &= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
177 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
178 }
179
180 if (device->info.has_llc) {
181 /* Big core GPUs share LLC with the CPU and thus one memory type can be
182 * both cached and coherent at the same time.
183 */
184 device->memory.types[type_count++] = (struct anv_memory_type) {
185 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
186 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
187 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
188 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
189 .heapIndex = heap,
190 .valid_buffer_usage = valid_buffer_usage,
191 };
192 } else {
193 /* The spec requires that we expose a host-visible, coherent memory
194 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
195 * to give the application a choice between cached, but not coherent and
196 * coherent but uncached (WC though).
197 */
198 device->memory.types[type_count++] = (struct anv_memory_type) {
199 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
200 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
201 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
202 .heapIndex = heap,
203 .valid_buffer_usage = valid_buffer_usage,
204 };
205 device->memory.types[type_count++] = (struct anv_memory_type) {
206 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
207 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
208 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
209 .heapIndex = heap,
210 .valid_buffer_usage = valid_buffer_usage,
211 };
212 }
213 }
214 device->memory.type_count = type_count;
215
216 return VK_SUCCESS;
217 }
218
219 static VkResult
220 anv_physical_device_init_uuids(struct anv_physical_device *device)
221 {
222 const struct build_id_note *note =
223 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids);
224 if (!note) {
225 return vk_errorf(device->instance, device,
226 VK_ERROR_INITIALIZATION_FAILED,
227 "Failed to find build-id");
228 }
229
230 unsigned build_id_len = build_id_length(note);
231 if (build_id_len < 20) {
232 return vk_errorf(device->instance, device,
233 VK_ERROR_INITIALIZATION_FAILED,
234 "build-id too short. It needs to be a SHA");
235 }
236
237 memcpy(device->driver_build_sha1, build_id_data(note), 20);
238
239 struct mesa_sha1 sha1_ctx;
240 uint8_t sha1[20];
241 STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
242
243 /* The pipeline cache UUID is used for determining when a pipeline cache is
244 * invalid. It needs both a driver build and the PCI ID of the device.
245 */
246 _mesa_sha1_init(&sha1_ctx);
247 _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
248 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
249 sizeof(device->chipset_id));
250 _mesa_sha1_final(&sha1_ctx, sha1);
251 memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
252
253 /* The driver UUID is used for determining sharability of images and memory
254 * between two Vulkan instances in separate processes. People who want to
255 * share memory need to also check the device UUID (below) so all this
256 * needs to be is the build-id.
257 */
258 memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
259
260 /* The device UUID uniquely identifies the given device within the machine.
261 * Since we never have more than one device, this doesn't need to be a real
262 * UUID. However, on the off-chance that someone tries to use this to
263 * cache pre-tiled images or something of the like, we use the PCI ID and
264 * some bits of ISL info to ensure that this is safe.
265 */
266 _mesa_sha1_init(&sha1_ctx);
267 _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
268 sizeof(device->chipset_id));
269 _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
270 sizeof(device->isl_dev.has_bit6_swizzling));
271 _mesa_sha1_final(&sha1_ctx, sha1);
272 memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
273
274 return VK_SUCCESS;
275 }
276
277 static void
278 anv_physical_device_init_disk_cache(struct anv_physical_device *device)
279 {
280 #ifdef ENABLE_SHADER_CACHE
281 char renderer[10];
282 MAYBE_UNUSED int len = snprintf(renderer, sizeof(renderer), "anv_%04x",
283 device->chipset_id);
284 assert(len == sizeof(renderer) - 2);
285
286 char timestamp[41];
287 _mesa_sha1_format(timestamp, device->driver_build_sha1);
288
289 device->disk_cache = disk_cache_create(renderer, timestamp, 0);
290 #else
291 device->disk_cache = NULL;
292 #endif
293 }
294
295 static void
296 anv_physical_device_free_disk_cache(struct anv_physical_device *device)
297 {
298 #ifdef ENABLE_SHADER_CACHE
299 if (device->disk_cache)
300 disk_cache_destroy(device->disk_cache);
301 #else
302 assert(device->disk_cache == NULL);
303 #endif
304 }
305
306 static VkResult
307 anv_physical_device_init(struct anv_physical_device *device,
308 struct anv_instance *instance,
309 const char *primary_path,
310 const char *path)
311 {
312 VkResult result;
313 int fd;
314 int master_fd = -1;
315
316 brw_process_intel_debug_variable();
317
318 fd = open(path, O_RDWR | O_CLOEXEC);
319 if (fd < 0)
320 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
321
322 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
323 device->instance = instance;
324
325 assert(strlen(path) < ARRAY_SIZE(device->path));
326 strncpy(device->path, path, ARRAY_SIZE(device->path));
327
328 device->no_hw = getenv("INTEL_NO_HW") != NULL;
329
330 const int pci_id_override = gen_get_pci_device_id_override();
331 if (pci_id_override < 0) {
332 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
333 if (!device->chipset_id) {
334 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
335 goto fail;
336 }
337 } else {
338 device->chipset_id = pci_id_override;
339 device->no_hw = true;
340 }
341
342 device->name = gen_get_device_name(device->chipset_id);
343 if (!gen_get_device_info(device->chipset_id, &device->info)) {
344 result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
345 goto fail;
346 }
347
348 if (device->info.is_haswell) {
349 intel_logw("Haswell Vulkan support is incomplete");
350 } else if (device->info.gen == 7 && !device->info.is_baytrail) {
351 intel_logw("Ivy Bridge Vulkan support is incomplete");
352 } else if (device->info.gen == 7 && device->info.is_baytrail) {
353 intel_logw("Bay Trail Vulkan support is incomplete");
354 } else if (device->info.gen >= 8 && device->info.gen <= 10) {
355 /* Gen8-10 fully supported */
356 } else if (device->info.gen == 11) {
357 intel_logw("Vulkan is not yet fully supported on gen11.");
358 } else {
359 result = vk_errorf(device->instance, device,
360 VK_ERROR_INCOMPATIBLE_DRIVER,
361 "Vulkan not yet supported on %s", device->name);
362 goto fail;
363 }
364
365 device->cmd_parser_version = -1;
366 if (device->info.gen == 7) {
367 device->cmd_parser_version =
368 anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
369 if (device->cmd_parser_version == -1) {
370 result = vk_errorf(device->instance, device,
371 VK_ERROR_INITIALIZATION_FAILED,
372 "failed to get command parser version");
373 goto fail;
374 }
375 }
376
377 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
378 result = vk_errorf(device->instance, device,
379 VK_ERROR_INITIALIZATION_FAILED,
380 "kernel missing gem wait");
381 goto fail;
382 }
383
384 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
385 result = vk_errorf(device->instance, device,
386 VK_ERROR_INITIALIZATION_FAILED,
387 "kernel missing execbuf2");
388 goto fail;
389 }
390
391 if (!device->info.has_llc &&
392 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
393 result = vk_errorf(device->instance, device,
394 VK_ERROR_INITIALIZATION_FAILED,
395 "kernel missing wc mmap");
396 goto fail;
397 }
398
399 result = anv_physical_device_init_heaps(device, fd);
400 if (result != VK_SUCCESS)
401 goto fail;
402
403 device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
404 device->has_exec_capture = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CAPTURE);
405 device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
406 device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
407 device->has_syncobj_wait = device->has_syncobj &&
408 anv_gem_supports_syncobj_wait(fd);
409 device->has_context_priority = anv_gem_has_context_priority(fd);
410
411 device->use_softpin = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_SOFTPIN)
412 && device->supports_48bit_addresses;
413
414 device->has_context_isolation =
415 anv_gem_get_param(fd, I915_PARAM_HAS_CONTEXT_ISOLATION);
416
417 bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
418
419 /* Starting with Gen10, the timestamp frequency of the command streamer may
420 * vary from one part to another. We can query the value from the kernel.
421 */
422 if (device->info.gen >= 10) {
423 int timestamp_frequency =
424 anv_gem_get_param(fd, I915_PARAM_CS_TIMESTAMP_FREQUENCY);
425
426 if (timestamp_frequency < 0)
427 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
428 else
429 device->info.timestamp_frequency = timestamp_frequency;
430 }
431
432 /* GENs prior to 8 do not support EU/Subslice info */
433 if (device->info.gen >= 8) {
434 device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
435 device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
436
437 /* Without this information, we cannot get the right Braswell
438 * brandstrings, and we have to use conservative numbers for GPGPU on
439 * many platforms, but otherwise, things will just work.
440 */
441 if (device->subslice_total < 1 || device->eu_total < 1) {
442 intel_logw("Kernel 4.1 required to properly query GPU properties");
443 }
444 } else if (device->info.gen == 7) {
445 device->subslice_total = 1 << (device->info.gt - 1);
446 }
447
448 if (device->info.is_cherryview &&
449 device->subslice_total > 0 && device->eu_total > 0) {
450 /* Logical CS threads = EUs per subslice * num threads per EU */
451 uint32_t max_cs_threads =
452 device->eu_total / device->subslice_total * device->info.num_thread_per_eu;
453
454 /* Fuse configurations may give more threads than expected, never less. */
455 if (max_cs_threads > device->info.max_cs_threads)
456 device->info.max_cs_threads = max_cs_threads;
457 }
458
459 device->compiler = brw_compiler_create(NULL, &device->info);
460 if (device->compiler == NULL) {
461 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
462 goto fail;
463 }
464 device->compiler->shader_debug_log = compiler_debug_log;
465 device->compiler->shader_perf_log = compiler_perf_log;
466 device->compiler->supports_pull_constants = false;
467 device->compiler->constant_buffer_0_is_relative =
468 device->info.gen < 8 || !device->has_context_isolation;
469 device->compiler->supports_shader_constants = true;
470
471 isl_device_init(&device->isl_dev, &device->info, swizzled);
472
473 result = anv_physical_device_init_uuids(device);
474 if (result != VK_SUCCESS)
475 goto fail;
476
477 anv_physical_device_init_disk_cache(device);
478
479 if (instance->enabled_extensions.KHR_display) {
480 master_fd = open(primary_path, O_RDWR | O_CLOEXEC);
481 if (master_fd >= 0) {
482 /* prod the device with a GETPARAM call which will fail if
483 * we don't have permission to even render on this device
484 */
485 if (anv_gem_get_param(master_fd, I915_PARAM_CHIPSET_ID) == 0) {
486 close(master_fd);
487 master_fd = -1;
488 }
489 }
490 }
491 device->master_fd = master_fd;
492
493 result = anv_init_wsi(device);
494 if (result != VK_SUCCESS) {
495 ralloc_free(device->compiler);
496 anv_physical_device_free_disk_cache(device);
497 goto fail;
498 }
499
500 anv_physical_device_get_supported_extensions(device,
501 &device->supported_extensions);
502
503
504 device->local_fd = fd;
505
506 return VK_SUCCESS;
507
508 fail:
509 close(fd);
510 if (master_fd != -1)
511 close(master_fd);
512 return result;
513 }
514
515 static void
516 anv_physical_device_finish(struct anv_physical_device *device)
517 {
518 anv_finish_wsi(device);
519 anv_physical_device_free_disk_cache(device);
520 ralloc_free(device->compiler);
521 close(device->local_fd);
522 if (device->master_fd >= 0)
523 close(device->master_fd);
524 }
525
526 static void *
527 default_alloc_func(void *pUserData, size_t size, size_t align,
528 VkSystemAllocationScope allocationScope)
529 {
530 return malloc(size);
531 }
532
533 static void *
534 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
535 size_t align, VkSystemAllocationScope allocationScope)
536 {
537 return realloc(pOriginal, size);
538 }
539
540 static void
541 default_free_func(void *pUserData, void *pMemory)
542 {
543 free(pMemory);
544 }
545
546 static const VkAllocationCallbacks default_alloc = {
547 .pUserData = NULL,
548 .pfnAllocation = default_alloc_func,
549 .pfnReallocation = default_realloc_func,
550 .pfnFree = default_free_func,
551 };
552
553 VkResult anv_EnumerateInstanceExtensionProperties(
554 const char* pLayerName,
555 uint32_t* pPropertyCount,
556 VkExtensionProperties* pProperties)
557 {
558 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
559
560 for (int i = 0; i < ANV_INSTANCE_EXTENSION_COUNT; i++) {
561 if (anv_instance_extensions_supported.extensions[i]) {
562 vk_outarray_append(&out, prop) {
563 *prop = anv_instance_extensions[i];
564 }
565 }
566 }
567
568 return vk_outarray_status(&out);
569 }
570
571 VkResult anv_CreateInstance(
572 const VkInstanceCreateInfo* pCreateInfo,
573 const VkAllocationCallbacks* pAllocator,
574 VkInstance* pInstance)
575 {
576 struct anv_instance *instance;
577 VkResult result;
578
579 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
580
581 struct anv_instance_extension_table enabled_extensions = {};
582 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
583 int idx;
584 for (idx = 0; idx < ANV_INSTANCE_EXTENSION_COUNT; idx++) {
585 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
586 anv_instance_extensions[idx].extensionName) == 0)
587 break;
588 }
589
590 if (idx >= ANV_INSTANCE_EXTENSION_COUNT)
591 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
592
593 if (!anv_instance_extensions_supported.extensions[idx])
594 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
595
596 enabled_extensions.extensions[idx] = true;
597 }
598
599 instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
600 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
601 if (!instance)
602 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
603
604 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
605
606 if (pAllocator)
607 instance->alloc = *pAllocator;
608 else
609 instance->alloc = default_alloc;
610
611 if (pCreateInfo->pApplicationInfo &&
612 pCreateInfo->pApplicationInfo->apiVersion != 0) {
613 instance->apiVersion = pCreateInfo->pApplicationInfo->apiVersion;
614 } else {
615 anv_EnumerateInstanceVersion(&instance->apiVersion);
616 }
617
618 instance->enabled_extensions = enabled_extensions;
619
620 for (unsigned i = 0; i < ARRAY_SIZE(instance->dispatch.entrypoints); i++) {
621 /* Vulkan requires that entrypoints for extensions which have not been
622 * enabled must not be advertised.
623 */
624 if (!anv_entrypoint_is_enabled(i, instance->apiVersion,
625 &instance->enabled_extensions, NULL)) {
626 instance->dispatch.entrypoints[i] = NULL;
627 } else if (anv_dispatch_table.entrypoints[i] != NULL) {
628 instance->dispatch.entrypoints[i] = anv_dispatch_table.entrypoints[i];
629 } else {
630 instance->dispatch.entrypoints[i] =
631 anv_tramp_dispatch_table.entrypoints[i];
632 }
633 }
634
635 instance->physicalDeviceCount = -1;
636
637 result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
638 if (result != VK_SUCCESS) {
639 vk_free2(&default_alloc, pAllocator, instance);
640 return vk_error(result);
641 }
642
643 instance->pipeline_cache_enabled =
644 env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
645
646 _mesa_locale_init();
647
648 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
649
650 *pInstance = anv_instance_to_handle(instance);
651
652 return VK_SUCCESS;
653 }
654
655 void anv_DestroyInstance(
656 VkInstance _instance,
657 const VkAllocationCallbacks* pAllocator)
658 {
659 ANV_FROM_HANDLE(anv_instance, instance, _instance);
660
661 if (!instance)
662 return;
663
664 if (instance->physicalDeviceCount > 0) {
665 /* We support at most one physical device. */
666 assert(instance->physicalDeviceCount == 1);
667 anv_physical_device_finish(&instance->physicalDevice);
668 }
669
670 VG(VALGRIND_DESTROY_MEMPOOL(instance));
671
672 vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
673
674 _mesa_locale_fini();
675
676 vk_free(&instance->alloc, instance);
677 }
678
679 static VkResult
680 anv_enumerate_devices(struct anv_instance *instance)
681 {
682 /* TODO: Check for more devices ? */
683 drmDevicePtr devices[8];
684 VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
685 int max_devices;
686
687 instance->physicalDeviceCount = 0;
688
689 max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
690 if (max_devices < 1)
691 return VK_ERROR_INCOMPATIBLE_DRIVER;
692
693 for (unsigned i = 0; i < (unsigned)max_devices; i++) {
694 if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
695 devices[i]->bustype == DRM_BUS_PCI &&
696 devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
697
698 result = anv_physical_device_init(&instance->physicalDevice,
699 instance,
700 devices[i]->nodes[DRM_NODE_PRIMARY],
701 devices[i]->nodes[DRM_NODE_RENDER]);
702 if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
703 break;
704 }
705 }
706 drmFreeDevices(devices, max_devices);
707
708 if (result == VK_SUCCESS)
709 instance->physicalDeviceCount = 1;
710
711 return result;
712 }
713
714 static VkResult
715 anv_instance_ensure_physical_device(struct anv_instance *instance)
716 {
717 if (instance->physicalDeviceCount < 0) {
718 VkResult result = anv_enumerate_devices(instance);
719 if (result != VK_SUCCESS &&
720 result != VK_ERROR_INCOMPATIBLE_DRIVER)
721 return result;
722 }
723
724 return VK_SUCCESS;
725 }
726
727 VkResult anv_EnumeratePhysicalDevices(
728 VkInstance _instance,
729 uint32_t* pPhysicalDeviceCount,
730 VkPhysicalDevice* pPhysicalDevices)
731 {
732 ANV_FROM_HANDLE(anv_instance, instance, _instance);
733 VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
734
735 VkResult result = anv_instance_ensure_physical_device(instance);
736 if (result != VK_SUCCESS)
737 return result;
738
739 if (instance->physicalDeviceCount == 0)
740 return VK_SUCCESS;
741
742 assert(instance->physicalDeviceCount == 1);
743 vk_outarray_append(&out, i) {
744 *i = anv_physical_device_to_handle(&instance->physicalDevice);
745 }
746
747 return vk_outarray_status(&out);
748 }
749
750 VkResult anv_EnumeratePhysicalDeviceGroups(
751 VkInstance _instance,
752 uint32_t* pPhysicalDeviceGroupCount,
753 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties)
754 {
755 ANV_FROM_HANDLE(anv_instance, instance, _instance);
756 VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
757 pPhysicalDeviceGroupCount);
758
759 VkResult result = anv_instance_ensure_physical_device(instance);
760 if (result != VK_SUCCESS)
761 return result;
762
763 if (instance->physicalDeviceCount == 0)
764 return VK_SUCCESS;
765
766 assert(instance->physicalDeviceCount == 1);
767
768 vk_outarray_append(&out, p) {
769 p->physicalDeviceCount = 1;
770 memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
771 p->physicalDevices[0] =
772 anv_physical_device_to_handle(&instance->physicalDevice);
773 p->subsetAllocation = VK_FALSE;
774
775 vk_foreach_struct(ext, p->pNext)
776 anv_debug_ignored_stype(ext->sType);
777 }
778
779 return vk_outarray_status(&out);
780 }
781
782 void anv_GetPhysicalDeviceFeatures(
783 VkPhysicalDevice physicalDevice,
784 VkPhysicalDeviceFeatures* pFeatures)
785 {
786 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
787
788 *pFeatures = (VkPhysicalDeviceFeatures) {
789 .robustBufferAccess = true,
790 .fullDrawIndexUint32 = true,
791 .imageCubeArray = true,
792 .independentBlend = true,
793 .geometryShader = true,
794 .tessellationShader = true,
795 .sampleRateShading = true,
796 .dualSrcBlend = true,
797 .logicOp = true,
798 .multiDrawIndirect = true,
799 .drawIndirectFirstInstance = true,
800 .depthClamp = true,
801 .depthBiasClamp = true,
802 .fillModeNonSolid = true,
803 .depthBounds = false,
804 .wideLines = true,
805 .largePoints = true,
806 .alphaToOne = true,
807 .multiViewport = true,
808 .samplerAnisotropy = true,
809 .textureCompressionETC2 = pdevice->info.gen >= 8 ||
810 pdevice->info.is_baytrail,
811 .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
812 .textureCompressionBC = true,
813 .occlusionQueryPrecise = true,
814 .pipelineStatisticsQuery = true,
815 .fragmentStoresAndAtomics = true,
816 .shaderTessellationAndGeometryPointSize = true,
817 .shaderImageGatherExtended = true,
818 .shaderStorageImageExtendedFormats = true,
819 .shaderStorageImageMultisample = false,
820 .shaderStorageImageReadWithoutFormat = false,
821 .shaderStorageImageWriteWithoutFormat = true,
822 .shaderUniformBufferArrayDynamicIndexing = true,
823 .shaderSampledImageArrayDynamicIndexing = true,
824 .shaderStorageBufferArrayDynamicIndexing = true,
825 .shaderStorageImageArrayDynamicIndexing = true,
826 .shaderClipDistance = true,
827 .shaderCullDistance = true,
828 .shaderFloat64 = pdevice->info.gen >= 8 &&
829 pdevice->info.has_64bit_types,
830 .shaderInt64 = pdevice->info.gen >= 8 &&
831 pdevice->info.has_64bit_types,
832 .shaderInt16 = pdevice->info.gen >= 8,
833 .shaderResourceMinLod = false,
834 .variableMultisampleRate = true,
835 .inheritedQueries = true,
836 };
837
838 /* We can't do image stores in vec4 shaders */
839 pFeatures->vertexPipelineStoresAndAtomics =
840 pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
841 pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
842 }
843
844 void anv_GetPhysicalDeviceFeatures2(
845 VkPhysicalDevice physicalDevice,
846 VkPhysicalDeviceFeatures2* pFeatures)
847 {
848 anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
849
850 vk_foreach_struct(ext, pFeatures->pNext) {
851 switch (ext->sType) {
852 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
853 VkPhysicalDeviceProtectedMemoryFeatures *features = (void *)ext;
854 features->protectedMemory = VK_FALSE;
855 break;
856 }
857
858 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
859 VkPhysicalDeviceMultiviewFeatures *features =
860 (VkPhysicalDeviceMultiviewFeatures *)ext;
861 features->multiview = true;
862 features->multiviewGeometryShader = true;
863 features->multiviewTessellationShader = true;
864 break;
865 }
866
867 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES: {
868 VkPhysicalDeviceVariablePointerFeatures *features = (void *)ext;
869 features->variablePointersStorageBuffer = true;
870 features->variablePointers = true;
871 break;
872 }
873
874 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
875 VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
876 (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
877 features->samplerYcbcrConversion = true;
878 break;
879 }
880
881 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES: {
882 VkPhysicalDeviceShaderDrawParameterFeatures *features = (void *)ext;
883 features->shaderDrawParameters = true;
884 break;
885 }
886
887 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR: {
888 VkPhysicalDevice16BitStorageFeaturesKHR *features =
889 (VkPhysicalDevice16BitStorageFeaturesKHR *)ext;
890 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
891
892 features->storageBuffer16BitAccess = pdevice->info.gen >= 8;
893 features->uniformAndStorageBuffer16BitAccess = pdevice->info.gen >= 8;
894 features->storagePushConstant16 = pdevice->info.gen >= 8;
895 features->storageInputOutput16 = false;
896 break;
897 }
898
899 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
900 VkPhysicalDevice8BitStorageFeaturesKHR *features =
901 (VkPhysicalDevice8BitStorageFeaturesKHR *)ext;
902 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
903
904 features->storageBuffer8BitAccess = pdevice->info.gen >= 8;
905 features->uniformAndStorageBuffer8BitAccess = pdevice->info.gen >= 8;
906 features->storagePushConstant8 = pdevice->info.gen >= 8;
907 break;
908 }
909
910 default:
911 anv_debug_ignored_stype(ext->sType);
912 break;
913 }
914 }
915 }
916
917 void anv_GetPhysicalDeviceProperties(
918 VkPhysicalDevice physicalDevice,
919 VkPhysicalDeviceProperties* pProperties)
920 {
921 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
922 const struct gen_device_info *devinfo = &pdevice->info;
923
924 /* See assertions made when programming the buffer surface state. */
925 const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
926 (1ul << 30) : (1ul << 27);
927
928 const uint32_t max_samplers = (devinfo->gen >= 8 || devinfo->is_haswell) ?
929 128 : 16;
930
931 VkSampleCountFlags sample_counts =
932 isl_device_get_sample_counts(&pdevice->isl_dev);
933
934 VkPhysicalDeviceLimits limits = {
935 .maxImageDimension1D = (1 << 14),
936 .maxImageDimension2D = (1 << 14),
937 .maxImageDimension3D = (1 << 11),
938 .maxImageDimensionCube = (1 << 14),
939 .maxImageArrayLayers = (1 << 11),
940 .maxTexelBufferElements = 128 * 1024 * 1024,
941 .maxUniformBufferRange = (1ul << 27),
942 .maxStorageBufferRange = max_raw_buffer_sz,
943 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
944 .maxMemoryAllocationCount = UINT32_MAX,
945 .maxSamplerAllocationCount = 64 * 1024,
946 .bufferImageGranularity = 64, /* A cache line */
947 .sparseAddressSpaceSize = 0,
948 .maxBoundDescriptorSets = MAX_SETS,
949 .maxPerStageDescriptorSamplers = max_samplers,
950 .maxPerStageDescriptorUniformBuffers = 64,
951 .maxPerStageDescriptorStorageBuffers = 64,
952 .maxPerStageDescriptorSampledImages = max_samplers,
953 .maxPerStageDescriptorStorageImages = 64,
954 .maxPerStageDescriptorInputAttachments = 64,
955 .maxPerStageResources = 250,
956 .maxDescriptorSetSamplers = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSamplers */
957 .maxDescriptorSetUniformBuffers = 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
958 .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
959 .maxDescriptorSetStorageBuffers = 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
960 .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
961 .maxDescriptorSetSampledImages = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSampledImages */
962 .maxDescriptorSetStorageImages = 6 * 64, /* number of stages * maxPerStageDescriptorStorageImages */
963 .maxDescriptorSetInputAttachments = 256,
964 .maxVertexInputAttributes = MAX_VBS,
965 .maxVertexInputBindings = MAX_VBS,
966 .maxVertexInputAttributeOffset = 2047,
967 .maxVertexInputBindingStride = 2048,
968 .maxVertexOutputComponents = 128,
969 .maxTessellationGenerationLevel = 64,
970 .maxTessellationPatchSize = 32,
971 .maxTessellationControlPerVertexInputComponents = 128,
972 .maxTessellationControlPerVertexOutputComponents = 128,
973 .maxTessellationControlPerPatchOutputComponents = 128,
974 .maxTessellationControlTotalOutputComponents = 2048,
975 .maxTessellationEvaluationInputComponents = 128,
976 .maxTessellationEvaluationOutputComponents = 128,
977 .maxGeometryShaderInvocations = 32,
978 .maxGeometryInputComponents = 64,
979 .maxGeometryOutputComponents = 128,
980 .maxGeometryOutputVertices = 256,
981 .maxGeometryTotalOutputComponents = 1024,
982 .maxFragmentInputComponents = 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
983 .maxFragmentOutputAttachments = 8,
984 .maxFragmentDualSrcAttachments = 1,
985 .maxFragmentCombinedOutputResources = 8,
986 .maxComputeSharedMemorySize = 32768,
987 .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
988 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
989 .maxComputeWorkGroupSize = {
990 16 * devinfo->max_cs_threads,
991 16 * devinfo->max_cs_threads,
992 16 * devinfo->max_cs_threads,
993 },
994 .subPixelPrecisionBits = 4 /* FIXME */,
995 .subTexelPrecisionBits = 4 /* FIXME */,
996 .mipmapPrecisionBits = 4 /* FIXME */,
997 .maxDrawIndexedIndexValue = UINT32_MAX,
998 .maxDrawIndirectCount = UINT32_MAX,
999 .maxSamplerLodBias = 16,
1000 .maxSamplerAnisotropy = 16,
1001 .maxViewports = MAX_VIEWPORTS,
1002 .maxViewportDimensions = { (1 << 14), (1 << 14) },
1003 .viewportBoundsRange = { INT16_MIN, INT16_MAX },
1004 .viewportSubPixelBits = 13, /* We take a float? */
1005 .minMemoryMapAlignment = 4096, /* A page */
1006 .minTexelBufferOffsetAlignment = 1,
1007 /* We need 16 for UBO block reads to work and 32 for push UBOs */
1008 .minUniformBufferOffsetAlignment = 32,
1009 .minStorageBufferOffsetAlignment = 4,
1010 .minTexelOffset = -8,
1011 .maxTexelOffset = 7,
1012 .minTexelGatherOffset = -32,
1013 .maxTexelGatherOffset = 31,
1014 .minInterpolationOffset = -0.5,
1015 .maxInterpolationOffset = 0.4375,
1016 .subPixelInterpolationOffsetBits = 4,
1017 .maxFramebufferWidth = (1 << 14),
1018 .maxFramebufferHeight = (1 << 14),
1019 .maxFramebufferLayers = (1 << 11),
1020 .framebufferColorSampleCounts = sample_counts,
1021 .framebufferDepthSampleCounts = sample_counts,
1022 .framebufferStencilSampleCounts = sample_counts,
1023 .framebufferNoAttachmentsSampleCounts = sample_counts,
1024 .maxColorAttachments = MAX_RTS,
1025 .sampledImageColorSampleCounts = sample_counts,
1026 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
1027 .sampledImageDepthSampleCounts = sample_counts,
1028 .sampledImageStencilSampleCounts = sample_counts,
1029 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
1030 .maxSampleMaskWords = 1,
1031 .timestampComputeAndGraphics = false,
1032 .timestampPeriod = 1000000000.0 / devinfo->timestamp_frequency,
1033 .maxClipDistances = 8,
1034 .maxCullDistances = 8,
1035 .maxCombinedClipAndCullDistances = 8,
1036 .discreteQueuePriorities = 1,
1037 .pointSizeRange = { 0.125, 255.875 },
1038 .lineWidthRange = { 0.0, 7.9921875 },
1039 .pointSizeGranularity = (1.0 / 8.0),
1040 .lineWidthGranularity = (1.0 / 128.0),
1041 .strictLines = false, /* FINISHME */
1042 .standardSampleLocations = true,
1043 .optimalBufferCopyOffsetAlignment = 128,
1044 .optimalBufferCopyRowPitchAlignment = 128,
1045 .nonCoherentAtomSize = 64,
1046 };
1047
1048 *pProperties = (VkPhysicalDeviceProperties) {
1049 .apiVersion = anv_physical_device_api_version(pdevice),
1050 .driverVersion = vk_get_driver_version(),
1051 .vendorID = 0x8086,
1052 .deviceID = pdevice->chipset_id,
1053 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
1054 .limits = limits,
1055 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
1056 };
1057
1058 snprintf(pProperties->deviceName, sizeof(pProperties->deviceName),
1059 "%s", pdevice->name);
1060 memcpy(pProperties->pipelineCacheUUID,
1061 pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
1062 }
1063
1064 void anv_GetPhysicalDeviceProperties2(
1065 VkPhysicalDevice physicalDevice,
1066 VkPhysicalDeviceProperties2* pProperties)
1067 {
1068 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
1069
1070 anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
1071
1072 vk_foreach_struct(ext, pProperties->pNext) {
1073 switch (ext->sType) {
1074 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
1075 VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
1076 (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
1077
1078 properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
1079 break;
1080 }
1081
1082 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
1083 VkPhysicalDeviceIDProperties *id_props =
1084 (VkPhysicalDeviceIDProperties *)ext;
1085 memcpy(id_props->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
1086 memcpy(id_props->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
1087 /* The LUID is for Windows. */
1088 id_props->deviceLUIDValid = false;
1089 break;
1090 }
1091
1092 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
1093 VkPhysicalDeviceMaintenance3Properties *props =
1094 (VkPhysicalDeviceMaintenance3Properties *)ext;
1095 /* This value doesn't matter for us today as our per-stage
1096 * descriptors are the real limit.
1097 */
1098 props->maxPerSetDescriptors = 1024;
1099 props->maxMemoryAllocationSize = MAX_MEMORY_ALLOCATION_SIZE;
1100 break;
1101 }
1102
1103 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
1104 VkPhysicalDeviceMultiviewProperties *properties =
1105 (VkPhysicalDeviceMultiviewProperties *)ext;
1106 properties->maxMultiviewViewCount = 16;
1107 properties->maxMultiviewInstanceIndex = UINT32_MAX / 16;
1108 break;
1109 }
1110
1111 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
1112 VkPhysicalDevicePointClippingProperties *properties =
1113 (VkPhysicalDevicePointClippingProperties *) ext;
1114 properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
1115 anv_finishme("Implement pop-free point clipping");
1116 break;
1117 }
1118
1119 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
1120 VkPhysicalDeviceSubgroupProperties *properties = (void *)ext;
1121
1122 properties->subgroupSize = BRW_SUBGROUP_SIZE;
1123
1124 VkShaderStageFlags scalar_stages = 0;
1125 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1126 if (pdevice->compiler->scalar_stage[stage])
1127 scalar_stages |= mesa_to_vk_shader_stage(stage);
1128 }
1129 properties->supportedStages = scalar_stages;
1130
1131 properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
1132 VK_SUBGROUP_FEATURE_VOTE_BIT |
1133 VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
1134 VK_SUBGROUP_FEATURE_BALLOT_BIT |
1135 VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
1136 VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT |
1137 VK_SUBGROUP_FEATURE_CLUSTERED_BIT |
1138 VK_SUBGROUP_FEATURE_QUAD_BIT;
1139 properties->quadOperationsInAllStages = VK_TRUE;
1140 break;
1141 }
1142
1143 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
1144 VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
1145 (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
1146 /* We have to restrict this a bit for multiview */
1147 props->maxVertexAttribDivisor = UINT32_MAX / 16;
1148 break;
1149 }
1150
1151 default:
1152 anv_debug_ignored_stype(ext->sType);
1153 break;
1154 }
1155 }
1156 }
1157
1158 /* We support exactly one queue family. */
1159 static const VkQueueFamilyProperties
1160 anv_queue_family_properties = {
1161 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
1162 VK_QUEUE_COMPUTE_BIT |
1163 VK_QUEUE_TRANSFER_BIT,
1164 .queueCount = 1,
1165 .timestampValidBits = 36, /* XXX: Real value here */
1166 .minImageTransferGranularity = { 1, 1, 1 },
1167 };
1168
1169 void anv_GetPhysicalDeviceQueueFamilyProperties(
1170 VkPhysicalDevice physicalDevice,
1171 uint32_t* pCount,
1172 VkQueueFamilyProperties* pQueueFamilyProperties)
1173 {
1174 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
1175
1176 vk_outarray_append(&out, p) {
1177 *p = anv_queue_family_properties;
1178 }
1179 }
1180
1181 void anv_GetPhysicalDeviceQueueFamilyProperties2(
1182 VkPhysicalDevice physicalDevice,
1183 uint32_t* pQueueFamilyPropertyCount,
1184 VkQueueFamilyProperties2* pQueueFamilyProperties)
1185 {
1186
1187 VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
1188
1189 vk_outarray_append(&out, p) {
1190 p->queueFamilyProperties = anv_queue_family_properties;
1191
1192 vk_foreach_struct(s, p->pNext) {
1193 anv_debug_ignored_stype(s->sType);
1194 }
1195 }
1196 }
1197
1198 void anv_GetPhysicalDeviceMemoryProperties(
1199 VkPhysicalDevice physicalDevice,
1200 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
1201 {
1202 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1203
1204 pMemoryProperties->memoryTypeCount = physical_device->memory.type_count;
1205 for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
1206 pMemoryProperties->memoryTypes[i] = (VkMemoryType) {
1207 .propertyFlags = physical_device->memory.types[i].propertyFlags,
1208 .heapIndex = physical_device->memory.types[i].heapIndex,
1209 };
1210 }
1211
1212 pMemoryProperties->memoryHeapCount = physical_device->memory.heap_count;
1213 for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
1214 pMemoryProperties->memoryHeaps[i] = (VkMemoryHeap) {
1215 .size = physical_device->memory.heaps[i].size,
1216 .flags = physical_device->memory.heaps[i].flags,
1217 };
1218 }
1219 }
1220
1221 void anv_GetPhysicalDeviceMemoryProperties2(
1222 VkPhysicalDevice physicalDevice,
1223 VkPhysicalDeviceMemoryProperties2* pMemoryProperties)
1224 {
1225 anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
1226 &pMemoryProperties->memoryProperties);
1227
1228 vk_foreach_struct(ext, pMemoryProperties->pNext) {
1229 switch (ext->sType) {
1230 default:
1231 anv_debug_ignored_stype(ext->sType);
1232 break;
1233 }
1234 }
1235 }
1236
1237 void
1238 anv_GetDeviceGroupPeerMemoryFeatures(
1239 VkDevice device,
1240 uint32_t heapIndex,
1241 uint32_t localDeviceIndex,
1242 uint32_t remoteDeviceIndex,
1243 VkPeerMemoryFeatureFlags* pPeerMemoryFeatures)
1244 {
1245 assert(localDeviceIndex == 0 && remoteDeviceIndex == 0);
1246 *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
1247 VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
1248 VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
1249 VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
1250 }
1251
1252 PFN_vkVoidFunction anv_GetInstanceProcAddr(
1253 VkInstance _instance,
1254 const char* pName)
1255 {
1256 ANV_FROM_HANDLE(anv_instance, instance, _instance);
1257
1258 /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
1259 * when we have to return valid function pointers, NULL, or it's left
1260 * undefined. See the table for exact details.
1261 */
1262 if (pName == NULL)
1263 return NULL;
1264
1265 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
1266 if (strcmp(pName, "vk" #entrypoint) == 0) \
1267 return (PFN_vkVoidFunction)anv_##entrypoint
1268
1269 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties);
1270 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties);
1271 LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion);
1272 LOOKUP_ANV_ENTRYPOINT(CreateInstance);
1273
1274 #undef LOOKUP_ANV_ENTRYPOINT
1275
1276 if (instance == NULL)
1277 return NULL;
1278
1279 int idx = anv_get_entrypoint_index(pName);
1280 if (idx < 0)
1281 return NULL;
1282
1283 return instance->dispatch.entrypoints[idx];
1284 }
1285
1286 /* With version 1+ of the loader interface the ICD should expose
1287 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1288 */
1289 PUBLIC
1290 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1291 VkInstance instance,
1292 const char* pName);
1293
1294 PUBLIC
1295 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
1296 VkInstance instance,
1297 const char* pName)
1298 {
1299 return anv_GetInstanceProcAddr(instance, pName);
1300 }
1301
1302 PFN_vkVoidFunction anv_GetDeviceProcAddr(
1303 VkDevice _device,
1304 const char* pName)
1305 {
1306 ANV_FROM_HANDLE(anv_device, device, _device);
1307
1308 if (!device || !pName)
1309 return NULL;
1310
1311 int idx = anv_get_entrypoint_index(pName);
1312 if (idx < 0)
1313 return NULL;
1314
1315 return device->dispatch.entrypoints[idx];
1316 }
1317
1318 VkResult
1319 anv_CreateDebugReportCallbackEXT(VkInstance _instance,
1320 const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
1321 const VkAllocationCallbacks* pAllocator,
1322 VkDebugReportCallbackEXT* pCallback)
1323 {
1324 ANV_FROM_HANDLE(anv_instance, instance, _instance);
1325 return vk_create_debug_report_callback(&instance->debug_report_callbacks,
1326 pCreateInfo, pAllocator, &instance->alloc,
1327 pCallback);
1328 }
1329
1330 void
1331 anv_DestroyDebugReportCallbackEXT(VkInstance _instance,
1332 VkDebugReportCallbackEXT _callback,
1333 const VkAllocationCallbacks* pAllocator)
1334 {
1335 ANV_FROM_HANDLE(anv_instance, instance, _instance);
1336 vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
1337 _callback, pAllocator, &instance->alloc);
1338 }
1339
1340 void
1341 anv_DebugReportMessageEXT(VkInstance _instance,
1342 VkDebugReportFlagsEXT flags,
1343 VkDebugReportObjectTypeEXT objectType,
1344 uint64_t object,
1345 size_t location,
1346 int32_t messageCode,
1347 const char* pLayerPrefix,
1348 const char* pMessage)
1349 {
1350 ANV_FROM_HANDLE(anv_instance, instance, _instance);
1351 vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
1352 object, location, messageCode, pLayerPrefix, pMessage);
1353 }
1354
1355 static void
1356 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
1357 {
1358 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1359 queue->device = device;
1360 queue->flags = 0;
1361 }
1362
1363 static void
1364 anv_queue_finish(struct anv_queue *queue)
1365 {
1366 }
1367
1368 static struct anv_state
1369 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
1370 {
1371 struct anv_state state;
1372
1373 state = anv_state_pool_alloc(pool, size, align);
1374 memcpy(state.map, p, size);
1375
1376 anv_state_flush(pool->block_pool.device, state);
1377
1378 return state;
1379 }
1380
1381 struct gen8_border_color {
1382 union {
1383 float float32[4];
1384 uint32_t uint32[4];
1385 };
1386 /* Pad out to 64 bytes */
1387 uint32_t _pad[12];
1388 };
1389
1390 static void
1391 anv_device_init_border_colors(struct anv_device *device)
1392 {
1393 static const struct gen8_border_color border_colors[] = {
1394 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
1395 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
1396 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
1397 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
1398 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
1399 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
1400 };
1401
1402 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
1403 sizeof(border_colors), 64,
1404 border_colors);
1405 }
1406
1407 static void
1408 anv_device_init_trivial_batch(struct anv_device *device)
1409 {
1410 anv_bo_init_new(&device->trivial_batch_bo, device, 4096);
1411
1412 if (device->instance->physicalDevice.has_exec_async)
1413 device->trivial_batch_bo.flags |= EXEC_OBJECT_ASYNC;
1414
1415 if (device->instance->physicalDevice.use_softpin)
1416 device->trivial_batch_bo.flags |= EXEC_OBJECT_PINNED;
1417
1418 anv_vma_alloc(device, &device->trivial_batch_bo);
1419
1420 void *map = anv_gem_mmap(device, device->trivial_batch_bo.gem_handle,
1421 0, 4096, 0);
1422
1423 struct anv_batch batch = {
1424 .start = map,
1425 .next = map,
1426 .end = map + 4096,
1427 };
1428
1429 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
1430 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
1431
1432 if (!device->info.has_llc)
1433 gen_clflush_range(map, batch.next - map);
1434
1435 anv_gem_munmap(map, device->trivial_batch_bo.size);
1436 }
1437
1438 VkResult anv_EnumerateDeviceExtensionProperties(
1439 VkPhysicalDevice physicalDevice,
1440 const char* pLayerName,
1441 uint32_t* pPropertyCount,
1442 VkExtensionProperties* pProperties)
1443 {
1444 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
1445 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
1446 (void)device;
1447
1448 for (int i = 0; i < ANV_DEVICE_EXTENSION_COUNT; i++) {
1449 if (device->supported_extensions.extensions[i]) {
1450 vk_outarray_append(&out, prop) {
1451 *prop = anv_device_extensions[i];
1452 }
1453 }
1454 }
1455
1456 return vk_outarray_status(&out);
1457 }
1458
1459 static void
1460 anv_device_init_dispatch(struct anv_device *device)
1461 {
1462 const struct anv_dispatch_table *genX_table;
1463 switch (device->info.gen) {
1464 case 11:
1465 genX_table = &gen11_dispatch_table;
1466 break;
1467 case 10:
1468 genX_table = &gen10_dispatch_table;
1469 break;
1470 case 9:
1471 genX_table = &gen9_dispatch_table;
1472 break;
1473 case 8:
1474 genX_table = &gen8_dispatch_table;
1475 break;
1476 case 7:
1477 if (device->info.is_haswell)
1478 genX_table = &gen75_dispatch_table;
1479 else
1480 genX_table = &gen7_dispatch_table;
1481 break;
1482 default:
1483 unreachable("unsupported gen\n");
1484 }
1485
1486 for (unsigned i = 0; i < ARRAY_SIZE(device->dispatch.entrypoints); i++) {
1487 /* Vulkan requires that entrypoints for extensions which have not been
1488 * enabled must not be advertised.
1489 */
1490 if (!anv_entrypoint_is_enabled(i, device->instance->apiVersion,
1491 &device->instance->enabled_extensions,
1492 &device->enabled_extensions)) {
1493 device->dispatch.entrypoints[i] = NULL;
1494 } else if (genX_table->entrypoints[i]) {
1495 device->dispatch.entrypoints[i] = genX_table->entrypoints[i];
1496 } else {
1497 device->dispatch.entrypoints[i] = anv_dispatch_table.entrypoints[i];
1498 }
1499 }
1500 }
1501
1502 static int
1503 vk_priority_to_gen(int priority)
1504 {
1505 switch (priority) {
1506 case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
1507 return GEN_CONTEXT_LOW_PRIORITY;
1508 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
1509 return GEN_CONTEXT_MEDIUM_PRIORITY;
1510 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
1511 return GEN_CONTEXT_HIGH_PRIORITY;
1512 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
1513 return GEN_CONTEXT_REALTIME_PRIORITY;
1514 default:
1515 unreachable("Invalid priority");
1516 }
1517 }
1518
1519 static void
1520 anv_device_init_hiz_clear_batch(struct anv_device *device)
1521 {
1522 anv_bo_init_new(&device->hiz_clear_bo, device, 4096);
1523 uint32_t *map = anv_gem_mmap(device, device->hiz_clear_bo.gem_handle,
1524 0, 4096, 0);
1525
1526 union isl_color_value hiz_clear = { .u32 = { 0, } };
1527 hiz_clear.f32[0] = ANV_HZ_FC_VAL;
1528
1529 memcpy(map, hiz_clear.u32, sizeof(hiz_clear.u32));
1530 anv_gem_munmap(map, device->hiz_clear_bo.size);
1531 }
1532
1533 VkResult anv_CreateDevice(
1534 VkPhysicalDevice physicalDevice,
1535 const VkDeviceCreateInfo* pCreateInfo,
1536 const VkAllocationCallbacks* pAllocator,
1537 VkDevice* pDevice)
1538 {
1539 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
1540 VkResult result;
1541 struct anv_device *device;
1542
1543 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
1544
1545 struct anv_device_extension_table enabled_extensions = { };
1546 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
1547 int idx;
1548 for (idx = 0; idx < ANV_DEVICE_EXTENSION_COUNT; idx++) {
1549 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
1550 anv_device_extensions[idx].extensionName) == 0)
1551 break;
1552 }
1553
1554 if (idx >= ANV_DEVICE_EXTENSION_COUNT)
1555 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
1556
1557 if (!physical_device->supported_extensions.extensions[idx])
1558 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
1559
1560 enabled_extensions.extensions[idx] = true;
1561 }
1562
1563 /* Check enabled features */
1564 if (pCreateInfo->pEnabledFeatures) {
1565 VkPhysicalDeviceFeatures supported_features;
1566 anv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
1567 VkBool32 *supported_feature = (VkBool32 *)&supported_features;
1568 VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
1569 unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1570 for (uint32_t i = 0; i < num_features; i++) {
1571 if (enabled_feature[i] && !supported_feature[i])
1572 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
1573 }
1574 }
1575
1576 /* Check requested queues and fail if we are requested to create any
1577 * queues with flags we don't support.
1578 */
1579 assert(pCreateInfo->queueCreateInfoCount > 0);
1580 for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
1581 if (pCreateInfo->pQueueCreateInfos[i].flags != 0)
1582 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
1583 }
1584
1585 /* Check if client specified queue priority. */
1586 const VkDeviceQueueGlobalPriorityCreateInfoEXT *queue_priority =
1587 vk_find_struct_const(pCreateInfo->pQueueCreateInfos[0].pNext,
1588 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
1589
1590 VkQueueGlobalPriorityEXT priority =
1591 queue_priority ? queue_priority->globalPriority :
1592 VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
1593
1594 device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
1595 sizeof(*device), 8,
1596 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
1597 if (!device)
1598 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1599
1600 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
1601 device->instance = physical_device->instance;
1602 device->chipset_id = physical_device->chipset_id;
1603 device->no_hw = physical_device->no_hw;
1604 device->lost = false;
1605
1606 if (pAllocator)
1607 device->alloc = *pAllocator;
1608 else
1609 device->alloc = physical_device->instance->alloc;
1610
1611 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1612 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
1613 if (device->fd == -1) {
1614 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1615 goto fail_device;
1616 }
1617
1618 device->context_id = anv_gem_create_context(device);
1619 if (device->context_id == -1) {
1620 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1621 goto fail_fd;
1622 }
1623
1624 if (physical_device->use_softpin) {
1625 if (pthread_mutex_init(&device->vma_mutex, NULL) != 0) {
1626 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1627 goto fail_fd;
1628 }
1629
1630 /* keep the page with address zero out of the allocator */
1631 util_vma_heap_init(&device->vma_lo, LOW_HEAP_MIN_ADDRESS, LOW_HEAP_SIZE);
1632 device->vma_lo_available =
1633 physical_device->memory.heaps[physical_device->memory.heap_count - 1].size;
1634
1635 /* Leave the last 4GiB out of the high vma range, so that no state base
1636 * address + size can overflow 48 bits. For more information see the
1637 * comment about Wa32bitGeneralStateOffset in anv_allocator.c
1638 */
1639 util_vma_heap_init(&device->vma_hi, HIGH_HEAP_MIN_ADDRESS,
1640 HIGH_HEAP_SIZE);
1641 device->vma_hi_available = physical_device->memory.heap_count == 1 ? 0 :
1642 physical_device->memory.heaps[0].size;
1643 }
1644
1645 /* As per spec, the driver implementation may deny requests to acquire
1646 * a priority above the default priority (MEDIUM) if the caller does not
1647 * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
1648 * is returned.
1649 */
1650 if (physical_device->has_context_priority) {
1651 int err = anv_gem_set_context_param(device->fd, device->context_id,
1652 I915_CONTEXT_PARAM_PRIORITY,
1653 vk_priority_to_gen(priority));
1654 if (err != 0 && priority > VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT) {
1655 result = vk_error(VK_ERROR_NOT_PERMITTED_EXT);
1656 goto fail_fd;
1657 }
1658 }
1659
1660 device->info = physical_device->info;
1661 device->isl_dev = physical_device->isl_dev;
1662
1663 /* On Broadwell and later, we can use batch chaining to more efficiently
1664 * implement growing command buffers. Prior to Haswell, the kernel
1665 * command parser gets in the way and we have to fall back to growing
1666 * the batch.
1667 */
1668 device->can_chain_batches = device->info.gen >= 8;
1669
1670 device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
1671 pCreateInfo->pEnabledFeatures->robustBufferAccess;
1672 device->enabled_extensions = enabled_extensions;
1673
1674 anv_device_init_dispatch(device);
1675
1676 if (pthread_mutex_init(&device->mutex, NULL) != 0) {
1677 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1678 goto fail_context_id;
1679 }
1680
1681 pthread_condattr_t condattr;
1682 if (pthread_condattr_init(&condattr) != 0) {
1683 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1684 goto fail_mutex;
1685 }
1686 if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
1687 pthread_condattr_destroy(&condattr);
1688 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1689 goto fail_mutex;
1690 }
1691 if (pthread_cond_init(&device->queue_submit, NULL) != 0) {
1692 pthread_condattr_destroy(&condattr);
1693 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
1694 goto fail_mutex;
1695 }
1696 pthread_condattr_destroy(&condattr);
1697
1698 uint64_t bo_flags =
1699 (physical_device->supports_48bit_addresses ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0) |
1700 (physical_device->has_exec_async ? EXEC_OBJECT_ASYNC : 0) |
1701 (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0) |
1702 (physical_device->use_softpin ? EXEC_OBJECT_PINNED : 0);
1703
1704 anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags);
1705
1706 result = anv_bo_cache_init(&device->bo_cache);
1707 if (result != VK_SUCCESS)
1708 goto fail_batch_bo_pool;
1709
1710 if (!physical_device->use_softpin)
1711 bo_flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1712
1713 result = anv_state_pool_init(&device->dynamic_state_pool, device,
1714 DYNAMIC_STATE_POOL_MIN_ADDRESS,
1715 16384,
1716 bo_flags);
1717 if (result != VK_SUCCESS)
1718 goto fail_bo_cache;
1719
1720 result = anv_state_pool_init(&device->instruction_state_pool, device,
1721 INSTRUCTION_STATE_POOL_MIN_ADDRESS,
1722 16384,
1723 bo_flags);
1724 if (result != VK_SUCCESS)
1725 goto fail_dynamic_state_pool;
1726
1727 result = anv_state_pool_init(&device->surface_state_pool, device,
1728 SURFACE_STATE_POOL_MIN_ADDRESS,
1729 4096,
1730 bo_flags);
1731 if (result != VK_SUCCESS)
1732 goto fail_instruction_state_pool;
1733
1734 if (physical_device->use_softpin) {
1735 result = anv_state_pool_init(&device->binding_table_pool, device,
1736 BINDING_TABLE_POOL_MIN_ADDRESS,
1737 4096,
1738 bo_flags);
1739 if (result != VK_SUCCESS)
1740 goto fail_surface_state_pool;
1741 }
1742
1743 result = anv_bo_init_new(&device->workaround_bo, device, 1024);
1744 if (result != VK_SUCCESS)
1745 goto fail_binding_table_pool;
1746
1747 if (physical_device->use_softpin)
1748 device->workaround_bo.flags |= EXEC_OBJECT_PINNED;
1749
1750 if (!anv_vma_alloc(device, &device->workaround_bo))
1751 goto fail_workaround_bo;
1752
1753 anv_device_init_trivial_batch(device);
1754
1755 if (device->info.gen >= 10)
1756 anv_device_init_hiz_clear_batch(device);
1757
1758 anv_scratch_pool_init(device, &device->scratch_pool);
1759
1760 anv_queue_init(device, &device->queue);
1761
1762 switch (device->info.gen) {
1763 case 7:
1764 if (!device->info.is_haswell)
1765 result = gen7_init_device_state(device);
1766 else
1767 result = gen75_init_device_state(device);
1768 break;
1769 case 8:
1770 result = gen8_init_device_state(device);
1771 break;
1772 case 9:
1773 result = gen9_init_device_state(device);
1774 break;
1775 case 10:
1776 result = gen10_init_device_state(device);
1777 break;
1778 case 11:
1779 result = gen11_init_device_state(device);
1780 break;
1781 default:
1782 /* Shouldn't get here as we don't create physical devices for any other
1783 * gens. */
1784 unreachable("unhandled gen");
1785 }
1786 if (result != VK_SUCCESS)
1787 goto fail_workaround_bo;
1788
1789 anv_pipeline_cache_init(&device->default_pipeline_cache, device, true);
1790
1791 anv_device_init_blorp(device);
1792
1793 anv_device_init_border_colors(device);
1794
1795 *pDevice = anv_device_to_handle(device);
1796
1797 return VK_SUCCESS;
1798
1799 fail_workaround_bo:
1800 anv_queue_finish(&device->queue);
1801 anv_scratch_pool_finish(device, &device->scratch_pool);
1802 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1803 anv_gem_close(device, device->workaround_bo.gem_handle);
1804 fail_binding_table_pool:
1805 if (physical_device->use_softpin)
1806 anv_state_pool_finish(&device->binding_table_pool);
1807 fail_surface_state_pool:
1808 anv_state_pool_finish(&device->surface_state_pool);
1809 fail_instruction_state_pool:
1810 anv_state_pool_finish(&device->instruction_state_pool);
1811 fail_dynamic_state_pool:
1812 anv_state_pool_finish(&device->dynamic_state_pool);
1813 fail_bo_cache:
1814 anv_bo_cache_finish(&device->bo_cache);
1815 fail_batch_bo_pool:
1816 anv_bo_pool_finish(&device->batch_bo_pool);
1817 pthread_cond_destroy(&device->queue_submit);
1818 fail_mutex:
1819 pthread_mutex_destroy(&device->mutex);
1820 fail_context_id:
1821 anv_gem_destroy_context(device, device->context_id);
1822 fail_fd:
1823 close(device->fd);
1824 fail_device:
1825 vk_free(&device->alloc, device);
1826
1827 return result;
1828 }
1829
1830 void anv_DestroyDevice(
1831 VkDevice _device,
1832 const VkAllocationCallbacks* pAllocator)
1833 {
1834 ANV_FROM_HANDLE(anv_device, device, _device);
1835 struct anv_physical_device *physical_device = &device->instance->physicalDevice;
1836
1837 if (!device)
1838 return;
1839
1840 anv_device_finish_blorp(device);
1841
1842 anv_pipeline_cache_finish(&device->default_pipeline_cache);
1843
1844 anv_queue_finish(&device->queue);
1845
1846 #ifdef HAVE_VALGRIND
1847 /* We only need to free these to prevent valgrind errors. The backing
1848 * BO will go away in a couple of lines so we don't actually leak.
1849 */
1850 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
1851 #endif
1852
1853 anv_scratch_pool_finish(device, &device->scratch_pool);
1854
1855 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
1856 anv_vma_free(device, &device->workaround_bo);
1857 anv_gem_close(device, device->workaround_bo.gem_handle);
1858
1859 anv_vma_free(device, &device->trivial_batch_bo);
1860 anv_gem_close(device, device->trivial_batch_bo.gem_handle);
1861 if (device->info.gen >= 10)
1862 anv_gem_close(device, device->hiz_clear_bo.gem_handle);
1863
1864 if (physical_device->use_softpin)
1865 anv_state_pool_finish(&device->binding_table_pool);
1866 anv_state_pool_finish(&device->surface_state_pool);
1867 anv_state_pool_finish(&device->instruction_state_pool);
1868 anv_state_pool_finish(&device->dynamic_state_pool);
1869
1870 anv_bo_cache_finish(&device->bo_cache);
1871
1872 anv_bo_pool_finish(&device->batch_bo_pool);
1873
1874 pthread_cond_destroy(&device->queue_submit);
1875 pthread_mutex_destroy(&device->mutex);
1876
1877 anv_gem_destroy_context(device, device->context_id);
1878
1879 close(device->fd);
1880
1881 vk_free(&device->alloc, device);
1882 }
1883
1884 VkResult anv_EnumerateInstanceLayerProperties(
1885 uint32_t* pPropertyCount,
1886 VkLayerProperties* pProperties)
1887 {
1888 if (pProperties == NULL) {
1889 *pPropertyCount = 0;
1890 return VK_SUCCESS;
1891 }
1892
1893 /* None supported at this time */
1894 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1895 }
1896
1897 VkResult anv_EnumerateDeviceLayerProperties(
1898 VkPhysicalDevice physicalDevice,
1899 uint32_t* pPropertyCount,
1900 VkLayerProperties* pProperties)
1901 {
1902 if (pProperties == NULL) {
1903 *pPropertyCount = 0;
1904 return VK_SUCCESS;
1905 }
1906
1907 /* None supported at this time */
1908 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
1909 }
1910
1911 void anv_GetDeviceQueue(
1912 VkDevice _device,
1913 uint32_t queueNodeIndex,
1914 uint32_t queueIndex,
1915 VkQueue* pQueue)
1916 {
1917 ANV_FROM_HANDLE(anv_device, device, _device);
1918
1919 assert(queueIndex == 0);
1920
1921 *pQueue = anv_queue_to_handle(&device->queue);
1922 }
1923
1924 void anv_GetDeviceQueue2(
1925 VkDevice _device,
1926 const VkDeviceQueueInfo2* pQueueInfo,
1927 VkQueue* pQueue)
1928 {
1929 ANV_FROM_HANDLE(anv_device, device, _device);
1930
1931 assert(pQueueInfo->queueIndex == 0);
1932
1933 if (pQueueInfo->flags == device->queue.flags)
1934 *pQueue = anv_queue_to_handle(&device->queue);
1935 else
1936 *pQueue = NULL;
1937 }
1938
1939 VkResult
1940 anv_device_query_status(struct anv_device *device)
1941 {
1942 /* This isn't likely as most of the callers of this function already check
1943 * for it. However, it doesn't hurt to check and it potentially lets us
1944 * avoid an ioctl.
1945 */
1946 if (unlikely(device->lost))
1947 return VK_ERROR_DEVICE_LOST;
1948
1949 uint32_t active, pending;
1950 int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
1951 if (ret == -1) {
1952 /* We don't know the real error. */
1953 device->lost = true;
1954 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1955 "get_reset_stats failed: %m");
1956 }
1957
1958 if (active) {
1959 device->lost = true;
1960 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1961 "GPU hung on one of our command buffers");
1962 } else if (pending) {
1963 device->lost = true;
1964 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1965 "GPU hung with commands in-flight");
1966 }
1967
1968 return VK_SUCCESS;
1969 }
1970
1971 VkResult
1972 anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
1973 {
1974 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1975 * Other usages of the BO (such as on different hardware) will not be
1976 * flagged as "busy" by this ioctl. Use with care.
1977 */
1978 int ret = anv_gem_busy(device, bo->gem_handle);
1979 if (ret == 1) {
1980 return VK_NOT_READY;
1981 } else if (ret == -1) {
1982 /* We don't know the real error. */
1983 device->lost = true;
1984 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
1985 "gem wait failed: %m");
1986 }
1987
1988 /* Query for device status after the busy call. If the BO we're checking
1989 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1990 * client because it clearly doesn't have valid data. Yes, this most
1991 * likely means an ioctl, but we just did an ioctl to query the busy status
1992 * so it's no great loss.
1993 */
1994 return anv_device_query_status(device);
1995 }
1996
1997 VkResult
1998 anv_device_wait(struct anv_device *device, struct anv_bo *bo,
1999 int64_t timeout)
2000 {
2001 int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
2002 if (ret == -1 && errno == ETIME) {
2003 return VK_TIMEOUT;
2004 } else if (ret == -1) {
2005 /* We don't know the real error. */
2006 device->lost = true;
2007 return vk_errorf(device->instance, device, VK_ERROR_DEVICE_LOST,
2008 "gem wait failed: %m");
2009 }
2010
2011 /* Query for device status after the wait. If the BO we're waiting on got
2012 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
2013 * because it clearly doesn't have valid data. Yes, this most likely means
2014 * an ioctl, but we just did an ioctl to wait so it's no great loss.
2015 */
2016 return anv_device_query_status(device);
2017 }
2018
2019 VkResult anv_DeviceWaitIdle(
2020 VkDevice _device)
2021 {
2022 ANV_FROM_HANDLE(anv_device, device, _device);
2023 if (unlikely(device->lost))
2024 return VK_ERROR_DEVICE_LOST;
2025
2026 struct anv_batch batch;
2027
2028 uint32_t cmds[8];
2029 batch.start = batch.next = cmds;
2030 batch.end = (void *) cmds + sizeof(cmds);
2031
2032 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
2033 anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
2034
2035 return anv_device_submit_simple_batch(device, &batch);
2036 }
2037
2038 bool
2039 anv_vma_alloc(struct anv_device *device, struct anv_bo *bo)
2040 {
2041 if (!(bo->flags & EXEC_OBJECT_PINNED))
2042 return true;
2043
2044 pthread_mutex_lock(&device->vma_mutex);
2045
2046 bo->offset = 0;
2047
2048 if (bo->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS &&
2049 device->vma_hi_available >= bo->size) {
2050 uint64_t addr = util_vma_heap_alloc(&device->vma_hi, bo->size, 4096);
2051 if (addr) {
2052 bo->offset = gen_canonical_address(addr);
2053 assert(addr == gen_48b_address(bo->offset));
2054 device->vma_hi_available -= bo->size;
2055 }
2056 }
2057
2058 if (bo->offset == 0 && device->vma_lo_available >= bo->size) {
2059 uint64_t addr = util_vma_heap_alloc(&device->vma_lo, bo->size, 4096);
2060 if (addr) {
2061 bo->offset = gen_canonical_address(addr);
2062 assert(addr == gen_48b_address(bo->offset));
2063 device->vma_lo_available -= bo->size;
2064 }
2065 }
2066
2067 pthread_mutex_unlock(&device->vma_mutex);
2068
2069 return bo->offset != 0;
2070 }
2071
2072 void
2073 anv_vma_free(struct anv_device *device, struct anv_bo *bo)
2074 {
2075 if (!(bo->flags & EXEC_OBJECT_PINNED))
2076 return;
2077
2078 const uint64_t addr_48b = gen_48b_address(bo->offset);
2079
2080 pthread_mutex_lock(&device->vma_mutex);
2081
2082 if (addr_48b >= LOW_HEAP_MIN_ADDRESS &&
2083 addr_48b <= LOW_HEAP_MAX_ADDRESS) {
2084 util_vma_heap_free(&device->vma_lo, addr_48b, bo->size);
2085 device->vma_lo_available += bo->size;
2086 } else {
2087 assert(addr_48b >= HIGH_HEAP_MIN_ADDRESS &&
2088 addr_48b <= HIGH_HEAP_MAX_ADDRESS);
2089 util_vma_heap_free(&device->vma_hi, addr_48b, bo->size);
2090 device->vma_hi_available += bo->size;
2091 }
2092
2093 pthread_mutex_unlock(&device->vma_mutex);
2094
2095 bo->offset = 0;
2096 }
2097
2098 VkResult
2099 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
2100 {
2101 uint32_t gem_handle = anv_gem_create(device, size);
2102 if (!gem_handle)
2103 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
2104
2105 anv_bo_init(bo, gem_handle, size);
2106
2107 return VK_SUCCESS;
2108 }
2109
2110 VkResult anv_AllocateMemory(
2111 VkDevice _device,
2112 const VkMemoryAllocateInfo* pAllocateInfo,
2113 const VkAllocationCallbacks* pAllocator,
2114 VkDeviceMemory* pMem)
2115 {
2116 ANV_FROM_HANDLE(anv_device, device, _device);
2117 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
2118 struct anv_device_memory *mem;
2119 VkResult result = VK_SUCCESS;
2120
2121 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
2122
2123 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
2124 assert(pAllocateInfo->allocationSize > 0);
2125
2126 if (pAllocateInfo->allocationSize > MAX_MEMORY_ALLOCATION_SIZE)
2127 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
2128
2129 /* FINISHME: Fail if allocation request exceeds heap size. */
2130
2131 mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
2132 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2133 if (mem == NULL)
2134 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2135
2136 assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
2137 mem->type = &pdevice->memory.types[pAllocateInfo->memoryTypeIndex];
2138 mem->map = NULL;
2139 mem->map_size = 0;
2140
2141 uint64_t bo_flags = 0;
2142
2143 assert(mem->type->heapIndex < pdevice->memory.heap_count);
2144 if (pdevice->memory.heaps[mem->type->heapIndex].supports_48bit_addresses)
2145 bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
2146
2147 const struct wsi_memory_allocate_info *wsi_info =
2148 vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
2149 if (wsi_info && wsi_info->implicit_sync) {
2150 /* We need to set the WRITE flag on window system buffers so that GEM
2151 * will know we're writing to them and synchronize uses on other rings
2152 * (eg if the display server uses the blitter ring).
2153 */
2154 bo_flags |= EXEC_OBJECT_WRITE;
2155 } else if (pdevice->has_exec_async) {
2156 bo_flags |= EXEC_OBJECT_ASYNC;
2157 }
2158
2159 if (pdevice->use_softpin)
2160 bo_flags |= EXEC_OBJECT_PINNED;
2161
2162 const VkImportMemoryFdInfoKHR *fd_info =
2163 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
2164
2165 /* The Vulkan spec permits handleType to be 0, in which case the struct is
2166 * ignored.
2167 */
2168 if (fd_info && fd_info->handleType) {
2169 /* At the moment, we support only the below handle types. */
2170 assert(fd_info->handleType ==
2171 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2172 fd_info->handleType ==
2173 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2174
2175 result = anv_bo_cache_import(device, &device->bo_cache,
2176 fd_info->fd, bo_flags, &mem->bo);
2177 if (result != VK_SUCCESS)
2178 goto fail;
2179
2180 VkDeviceSize aligned_alloc_size =
2181 align_u64(pAllocateInfo->allocationSize, 4096);
2182
2183 /* For security purposes, we reject importing the bo if it's smaller
2184 * than the requested allocation size. This prevents a malicious client
2185 * from passing a buffer to a trusted client, lying about the size, and
2186 * telling the trusted client to try and texture from an image that goes
2187 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
2188 * in the trusted client. The trusted client can protect itself against
2189 * this sort of attack but only if it can trust the buffer size.
2190 */
2191 if (mem->bo->size < aligned_alloc_size) {
2192 result = vk_errorf(device->instance, device,
2193 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
2194 "aligned allocationSize too large for "
2195 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
2196 "%"PRIu64"B > %"PRIu64"B",
2197 aligned_alloc_size, mem->bo->size);
2198 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
2199 goto fail;
2200 }
2201
2202 /* From the Vulkan spec:
2203 *
2204 * "Importing memory from a file descriptor transfers ownership of
2205 * the file descriptor from the application to the Vulkan
2206 * implementation. The application must not perform any operations on
2207 * the file descriptor after a successful import."
2208 *
2209 * If the import fails, we leave the file descriptor open.
2210 */
2211 close(fd_info->fd);
2212 } else {
2213 result = anv_bo_cache_alloc(device, &device->bo_cache,
2214 pAllocateInfo->allocationSize, bo_flags,
2215 &mem->bo);
2216 if (result != VK_SUCCESS)
2217 goto fail;
2218
2219 const VkMemoryDedicatedAllocateInfoKHR *dedicated_info =
2220 vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO_KHR);
2221 if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
2222 ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
2223
2224 /* Some legacy (non-modifiers) consumers need the tiling to be set on
2225 * the BO. In this case, we have a dedicated allocation.
2226 */
2227 if (image->needs_set_tiling) {
2228 const uint32_t i915_tiling =
2229 isl_tiling_to_i915_tiling(image->planes[0].surface.isl.tiling);
2230 int ret = anv_gem_set_tiling(device, mem->bo->gem_handle,
2231 image->planes[0].surface.isl.row_pitch,
2232 i915_tiling);
2233 if (ret) {
2234 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
2235 return vk_errorf(device->instance, NULL,
2236 VK_ERROR_OUT_OF_DEVICE_MEMORY,
2237 "failed to set BO tiling: %m");
2238 }
2239 }
2240 }
2241 }
2242
2243 *pMem = anv_device_memory_to_handle(mem);
2244
2245 return VK_SUCCESS;
2246
2247 fail:
2248 vk_free2(&device->alloc, pAllocator, mem);
2249
2250 return result;
2251 }
2252
2253 VkResult anv_GetMemoryFdKHR(
2254 VkDevice device_h,
2255 const VkMemoryGetFdInfoKHR* pGetFdInfo,
2256 int* pFd)
2257 {
2258 ANV_FROM_HANDLE(anv_device, dev, device_h);
2259 ANV_FROM_HANDLE(anv_device_memory, mem, pGetFdInfo->memory);
2260
2261 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
2262
2263 assert(pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
2264 pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
2265
2266 return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
2267 }
2268
2269 VkResult anv_GetMemoryFdPropertiesKHR(
2270 VkDevice _device,
2271 VkExternalMemoryHandleTypeFlagBitsKHR handleType,
2272 int fd,
2273 VkMemoryFdPropertiesKHR* pMemoryFdProperties)
2274 {
2275 ANV_FROM_HANDLE(anv_device, device, _device);
2276 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
2277
2278 switch (handleType) {
2279 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
2280 /* dma-buf can be imported as any memory type */
2281 pMemoryFdProperties->memoryTypeBits =
2282 (1 << pdevice->memory.type_count) - 1;
2283 return VK_SUCCESS;
2284
2285 default:
2286 /* The valid usage section for this function says:
2287 *
2288 * "handleType must not be one of the handle types defined as
2289 * opaque."
2290 *
2291 * So opaque handle types fall into the default "unsupported" case.
2292 */
2293 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
2294 }
2295 }
2296
2297 void anv_FreeMemory(
2298 VkDevice _device,
2299 VkDeviceMemory _mem,
2300 const VkAllocationCallbacks* pAllocator)
2301 {
2302 ANV_FROM_HANDLE(anv_device, device, _device);
2303 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
2304
2305 if (mem == NULL)
2306 return;
2307
2308 if (mem->map)
2309 anv_UnmapMemory(_device, _mem);
2310
2311 anv_bo_cache_release(device, &device->bo_cache, mem->bo);
2312
2313 vk_free2(&device->alloc, pAllocator, mem);
2314 }
2315
2316 VkResult anv_MapMemory(
2317 VkDevice _device,
2318 VkDeviceMemory _memory,
2319 VkDeviceSize offset,
2320 VkDeviceSize size,
2321 VkMemoryMapFlags flags,
2322 void** ppData)
2323 {
2324 ANV_FROM_HANDLE(anv_device, device, _device);
2325 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
2326
2327 if (mem == NULL) {
2328 *ppData = NULL;
2329 return VK_SUCCESS;
2330 }
2331
2332 if (size == VK_WHOLE_SIZE)
2333 size = mem->bo->size - offset;
2334
2335 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
2336 *
2337 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
2338 * assert(size != 0);
2339 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
2340 * equal to the size of the memory minus offset
2341 */
2342 assert(size > 0);
2343 assert(offset + size <= mem->bo->size);
2344
2345 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
2346 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
2347 * at a time is valid. We could just mmap up front and return an offset
2348 * pointer here, but that may exhaust virtual memory on 32 bit
2349 * userspace. */
2350
2351 uint32_t gem_flags = 0;
2352
2353 if (!device->info.has_llc &&
2354 (mem->type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
2355 gem_flags |= I915_MMAP_WC;
2356
2357 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
2358 uint64_t map_offset = offset & ~4095ull;
2359 assert(offset >= map_offset);
2360 uint64_t map_size = (offset + size) - map_offset;
2361
2362 /* Let's map whole pages */
2363 map_size = align_u64(map_size, 4096);
2364
2365 void *map = anv_gem_mmap(device, mem->bo->gem_handle,
2366 map_offset, map_size, gem_flags);
2367 if (map == MAP_FAILED)
2368 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
2369
2370 mem->map = map;
2371 mem->map_size = map_size;
2372
2373 *ppData = mem->map + (offset - map_offset);
2374
2375 return VK_SUCCESS;
2376 }
2377
2378 void anv_UnmapMemory(
2379 VkDevice _device,
2380 VkDeviceMemory _memory)
2381 {
2382 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
2383
2384 if (mem == NULL)
2385 return;
2386
2387 anv_gem_munmap(mem->map, mem->map_size);
2388
2389 mem->map = NULL;
2390 mem->map_size = 0;
2391 }
2392
2393 static void
2394 clflush_mapped_ranges(struct anv_device *device,
2395 uint32_t count,
2396 const VkMappedMemoryRange *ranges)
2397 {
2398 for (uint32_t i = 0; i < count; i++) {
2399 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
2400 if (ranges[i].offset >= mem->map_size)
2401 continue;
2402
2403 gen_clflush_range(mem->map + ranges[i].offset,
2404 MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
2405 }
2406 }
2407
2408 VkResult anv_FlushMappedMemoryRanges(
2409 VkDevice _device,
2410 uint32_t memoryRangeCount,
2411 const VkMappedMemoryRange* pMemoryRanges)
2412 {
2413 ANV_FROM_HANDLE(anv_device, device, _device);
2414
2415 if (device->info.has_llc)
2416 return VK_SUCCESS;
2417
2418 /* Make sure the writes we're flushing have landed. */
2419 __builtin_ia32_mfence();
2420
2421 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
2422
2423 return VK_SUCCESS;
2424 }
2425
2426 VkResult anv_InvalidateMappedMemoryRanges(
2427 VkDevice _device,
2428 uint32_t memoryRangeCount,
2429 const VkMappedMemoryRange* pMemoryRanges)
2430 {
2431 ANV_FROM_HANDLE(anv_device, device, _device);
2432
2433 if (device->info.has_llc)
2434 return VK_SUCCESS;
2435
2436 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
2437
2438 /* Make sure no reads get moved up above the invalidate. */
2439 __builtin_ia32_mfence();
2440
2441 return VK_SUCCESS;
2442 }
2443
2444 void anv_GetBufferMemoryRequirements(
2445 VkDevice _device,
2446 VkBuffer _buffer,
2447 VkMemoryRequirements* pMemoryRequirements)
2448 {
2449 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2450 ANV_FROM_HANDLE(anv_device, device, _device);
2451 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
2452
2453 /* The Vulkan spec (git aaed022) says:
2454 *
2455 * memoryTypeBits is a bitfield and contains one bit set for every
2456 * supported memory type for the resource. The bit `1<<i` is set if and
2457 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2458 * structure for the physical device is supported.
2459 */
2460 uint32_t memory_types = 0;
2461 for (uint32_t i = 0; i < pdevice->memory.type_count; i++) {
2462 uint32_t valid_usage = pdevice->memory.types[i].valid_buffer_usage;
2463 if ((valid_usage & buffer->usage) == buffer->usage)
2464 memory_types |= (1u << i);
2465 }
2466
2467 /* Base alignment requirement of a cache line */
2468 uint32_t alignment = 16;
2469
2470 /* We need an alignment of 32 for pushing UBOs */
2471 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
2472 alignment = MAX2(alignment, 32);
2473
2474 pMemoryRequirements->size = buffer->size;
2475 pMemoryRequirements->alignment = alignment;
2476
2477 /* Storage and Uniform buffers should have their size aligned to
2478 * 32-bits to avoid boundary checks when last DWord is not complete.
2479 * This would ensure that not internal padding would be needed for
2480 * 16-bit types.
2481 */
2482 if (device->robust_buffer_access &&
2483 (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
2484 buffer->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
2485 pMemoryRequirements->size = align_u64(buffer->size, 4);
2486
2487 pMemoryRequirements->memoryTypeBits = memory_types;
2488 }
2489
2490 void anv_GetBufferMemoryRequirements2(
2491 VkDevice _device,
2492 const VkBufferMemoryRequirementsInfo2* pInfo,
2493 VkMemoryRequirements2* pMemoryRequirements)
2494 {
2495 anv_GetBufferMemoryRequirements(_device, pInfo->buffer,
2496 &pMemoryRequirements->memoryRequirements);
2497
2498 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
2499 switch (ext->sType) {
2500 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
2501 VkMemoryDedicatedRequirements *requirements = (void *)ext;
2502 requirements->prefersDedicatedAllocation = VK_FALSE;
2503 requirements->requiresDedicatedAllocation = VK_FALSE;
2504 break;
2505 }
2506
2507 default:
2508 anv_debug_ignored_stype(ext->sType);
2509 break;
2510 }
2511 }
2512 }
2513
2514 void anv_GetImageMemoryRequirements(
2515 VkDevice _device,
2516 VkImage _image,
2517 VkMemoryRequirements* pMemoryRequirements)
2518 {
2519 ANV_FROM_HANDLE(anv_image, image, _image);
2520 ANV_FROM_HANDLE(anv_device, device, _device);
2521 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
2522
2523 /* The Vulkan spec (git aaed022) says:
2524 *
2525 * memoryTypeBits is a bitfield and contains one bit set for every
2526 * supported memory type for the resource. The bit `1<<i` is set if and
2527 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
2528 * structure for the physical device is supported.
2529 *
2530 * All types are currently supported for images.
2531 */
2532 uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
2533
2534 pMemoryRequirements->size = image->size;
2535 pMemoryRequirements->alignment = image->alignment;
2536 pMemoryRequirements->memoryTypeBits = memory_types;
2537 }
2538
2539 void anv_GetImageMemoryRequirements2(
2540 VkDevice _device,
2541 const VkImageMemoryRequirementsInfo2* pInfo,
2542 VkMemoryRequirements2* pMemoryRequirements)
2543 {
2544 ANV_FROM_HANDLE(anv_device, device, _device);
2545 ANV_FROM_HANDLE(anv_image, image, pInfo->image);
2546
2547 anv_GetImageMemoryRequirements(_device, pInfo->image,
2548 &pMemoryRequirements->memoryRequirements);
2549
2550 vk_foreach_struct_const(ext, pInfo->pNext) {
2551 switch (ext->sType) {
2552 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO: {
2553 struct anv_physical_device *pdevice = &device->instance->physicalDevice;
2554 const VkImagePlaneMemoryRequirementsInfoKHR *plane_reqs =
2555 (const VkImagePlaneMemoryRequirementsInfoKHR *) ext;
2556 uint32_t plane = anv_image_aspect_to_plane(image->aspects,
2557 plane_reqs->planeAspect);
2558
2559 assert(image->planes[plane].offset == 0);
2560
2561 /* The Vulkan spec (git aaed022) says:
2562 *
2563 * memoryTypeBits is a bitfield and contains one bit set for every
2564 * supported memory type for the resource. The bit `1<<i` is set
2565 * if and only if the memory type `i` in the
2566 * VkPhysicalDeviceMemoryProperties structure for the physical
2567 * device is supported.
2568 *
2569 * All types are currently supported for images.
2570 */
2571 pMemoryRequirements->memoryRequirements.memoryTypeBits =
2572 (1ull << pdevice->memory.type_count) - 1;
2573
2574 pMemoryRequirements->memoryRequirements.size = image->planes[plane].size;
2575 pMemoryRequirements->memoryRequirements.alignment =
2576 image->planes[plane].alignment;
2577 break;
2578 }
2579
2580 default:
2581 anv_debug_ignored_stype(ext->sType);
2582 break;
2583 }
2584 }
2585
2586 vk_foreach_struct(ext, pMemoryRequirements->pNext) {
2587 switch (ext->sType) {
2588 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
2589 VkMemoryDedicatedRequirements *requirements = (void *)ext;
2590 if (image->needs_set_tiling) {
2591 /* If we need to set the tiling for external consumers, we need a
2592 * dedicated allocation.
2593 *
2594 * See also anv_AllocateMemory.
2595 */
2596 requirements->prefersDedicatedAllocation = VK_TRUE;
2597 requirements->requiresDedicatedAllocation = VK_TRUE;
2598 } else {
2599 requirements->prefersDedicatedAllocation = VK_FALSE;
2600 requirements->requiresDedicatedAllocation = VK_FALSE;
2601 }
2602 break;
2603 }
2604
2605 default:
2606 anv_debug_ignored_stype(ext->sType);
2607 break;
2608 }
2609 }
2610 }
2611
2612 void anv_GetImageSparseMemoryRequirements(
2613 VkDevice device,
2614 VkImage image,
2615 uint32_t* pSparseMemoryRequirementCount,
2616 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
2617 {
2618 *pSparseMemoryRequirementCount = 0;
2619 }
2620
2621 void anv_GetImageSparseMemoryRequirements2(
2622 VkDevice device,
2623 const VkImageSparseMemoryRequirementsInfo2* pInfo,
2624 uint32_t* pSparseMemoryRequirementCount,
2625 VkSparseImageMemoryRequirements2* pSparseMemoryRequirements)
2626 {
2627 *pSparseMemoryRequirementCount = 0;
2628 }
2629
2630 void anv_GetDeviceMemoryCommitment(
2631 VkDevice device,
2632 VkDeviceMemory memory,
2633 VkDeviceSize* pCommittedMemoryInBytes)
2634 {
2635 *pCommittedMemoryInBytes = 0;
2636 }
2637
2638 static void
2639 anv_bind_buffer_memory(const VkBindBufferMemoryInfo *pBindInfo)
2640 {
2641 ANV_FROM_HANDLE(anv_device_memory, mem, pBindInfo->memory);
2642 ANV_FROM_HANDLE(anv_buffer, buffer, pBindInfo->buffer);
2643
2644 assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO);
2645
2646 if (mem) {
2647 assert((buffer->usage & mem->type->valid_buffer_usage) == buffer->usage);
2648 buffer->address = (struct anv_address) {
2649 .bo = mem->bo,
2650 .offset = pBindInfo->memoryOffset,
2651 };
2652 } else {
2653 buffer->address = ANV_NULL_ADDRESS;
2654 }
2655 }
2656
2657 VkResult anv_BindBufferMemory(
2658 VkDevice device,
2659 VkBuffer buffer,
2660 VkDeviceMemory memory,
2661 VkDeviceSize memoryOffset)
2662 {
2663 anv_bind_buffer_memory(
2664 &(VkBindBufferMemoryInfo) {
2665 .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
2666 .buffer = buffer,
2667 .memory = memory,
2668 .memoryOffset = memoryOffset,
2669 });
2670
2671 return VK_SUCCESS;
2672 }
2673
2674 VkResult anv_BindBufferMemory2(
2675 VkDevice device,
2676 uint32_t bindInfoCount,
2677 const VkBindBufferMemoryInfo* pBindInfos)
2678 {
2679 for (uint32_t i = 0; i < bindInfoCount; i++)
2680 anv_bind_buffer_memory(&pBindInfos[i]);
2681
2682 return VK_SUCCESS;
2683 }
2684
2685 VkResult anv_QueueBindSparse(
2686 VkQueue _queue,
2687 uint32_t bindInfoCount,
2688 const VkBindSparseInfo* pBindInfo,
2689 VkFence fence)
2690 {
2691 ANV_FROM_HANDLE(anv_queue, queue, _queue);
2692 if (unlikely(queue->device->lost))
2693 return VK_ERROR_DEVICE_LOST;
2694
2695 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
2696 }
2697
2698 // Event functions
2699
2700 VkResult anv_CreateEvent(
2701 VkDevice _device,
2702 const VkEventCreateInfo* pCreateInfo,
2703 const VkAllocationCallbacks* pAllocator,
2704 VkEvent* pEvent)
2705 {
2706 ANV_FROM_HANDLE(anv_device, device, _device);
2707 struct anv_state state;
2708 struct anv_event *event;
2709
2710 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
2711
2712 state = anv_state_pool_alloc(&device->dynamic_state_pool,
2713 sizeof(*event), 8);
2714 event = state.map;
2715 event->state = state;
2716 event->semaphore = VK_EVENT_RESET;
2717
2718 if (!device->info.has_llc) {
2719 /* Make sure the writes we're flushing have landed. */
2720 __builtin_ia32_mfence();
2721 __builtin_ia32_clflush(event);
2722 }
2723
2724 *pEvent = anv_event_to_handle(event);
2725
2726 return VK_SUCCESS;
2727 }
2728
2729 void anv_DestroyEvent(
2730 VkDevice _device,
2731 VkEvent _event,
2732 const VkAllocationCallbacks* pAllocator)
2733 {
2734 ANV_FROM_HANDLE(anv_device, device, _device);
2735 ANV_FROM_HANDLE(anv_event, event, _event);
2736
2737 if (!event)
2738 return;
2739
2740 anv_state_pool_free(&device->dynamic_state_pool, event->state);
2741 }
2742
2743 VkResult anv_GetEventStatus(
2744 VkDevice _device,
2745 VkEvent _event)
2746 {
2747 ANV_FROM_HANDLE(anv_device, device, _device);
2748 ANV_FROM_HANDLE(anv_event, event, _event);
2749
2750 if (unlikely(device->lost))
2751 return VK_ERROR_DEVICE_LOST;
2752
2753 if (!device->info.has_llc) {
2754 /* Invalidate read cache before reading event written by GPU. */
2755 __builtin_ia32_clflush(event);
2756 __builtin_ia32_mfence();
2757
2758 }
2759
2760 return event->semaphore;
2761 }
2762
2763 VkResult anv_SetEvent(
2764 VkDevice _device,
2765 VkEvent _event)
2766 {
2767 ANV_FROM_HANDLE(anv_device, device, _device);
2768 ANV_FROM_HANDLE(anv_event, event, _event);
2769
2770 event->semaphore = VK_EVENT_SET;
2771
2772 if (!device->info.has_llc) {
2773 /* Make sure the writes we're flushing have landed. */
2774 __builtin_ia32_mfence();
2775 __builtin_ia32_clflush(event);
2776 }
2777
2778 return VK_SUCCESS;
2779 }
2780
2781 VkResult anv_ResetEvent(
2782 VkDevice _device,
2783 VkEvent _event)
2784 {
2785 ANV_FROM_HANDLE(anv_device, device, _device);
2786 ANV_FROM_HANDLE(anv_event, event, _event);
2787
2788 event->semaphore = VK_EVENT_RESET;
2789
2790 if (!device->info.has_llc) {
2791 /* Make sure the writes we're flushing have landed. */
2792 __builtin_ia32_mfence();
2793 __builtin_ia32_clflush(event);
2794 }
2795
2796 return VK_SUCCESS;
2797 }
2798
2799 // Buffer functions
2800
2801 VkResult anv_CreateBuffer(
2802 VkDevice _device,
2803 const VkBufferCreateInfo* pCreateInfo,
2804 const VkAllocationCallbacks* pAllocator,
2805 VkBuffer* pBuffer)
2806 {
2807 ANV_FROM_HANDLE(anv_device, device, _device);
2808 struct anv_buffer *buffer;
2809
2810 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
2811
2812 buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
2813 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2814 if (buffer == NULL)
2815 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2816
2817 buffer->size = pCreateInfo->size;
2818 buffer->usage = pCreateInfo->usage;
2819 buffer->address = ANV_NULL_ADDRESS;
2820
2821 *pBuffer = anv_buffer_to_handle(buffer);
2822
2823 return VK_SUCCESS;
2824 }
2825
2826 void anv_DestroyBuffer(
2827 VkDevice _device,
2828 VkBuffer _buffer,
2829 const VkAllocationCallbacks* pAllocator)
2830 {
2831 ANV_FROM_HANDLE(anv_device, device, _device);
2832 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
2833
2834 if (!buffer)
2835 return;
2836
2837 vk_free2(&device->alloc, pAllocator, buffer);
2838 }
2839
2840 void
2841 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
2842 enum isl_format format,
2843 struct anv_address address,
2844 uint32_t range, uint32_t stride)
2845 {
2846 isl_buffer_fill_state(&device->isl_dev, state.map,
2847 .address = anv_address_physical(address),
2848 .mocs = device->default_mocs,
2849 .size = range,
2850 .format = format,
2851 .stride = stride);
2852
2853 anv_state_flush(device, state);
2854 }
2855
2856 void anv_DestroySampler(
2857 VkDevice _device,
2858 VkSampler _sampler,
2859 const VkAllocationCallbacks* pAllocator)
2860 {
2861 ANV_FROM_HANDLE(anv_device, device, _device);
2862 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
2863
2864 if (!sampler)
2865 return;
2866
2867 vk_free2(&device->alloc, pAllocator, sampler);
2868 }
2869
2870 VkResult anv_CreateFramebuffer(
2871 VkDevice _device,
2872 const VkFramebufferCreateInfo* pCreateInfo,
2873 const VkAllocationCallbacks* pAllocator,
2874 VkFramebuffer* pFramebuffer)
2875 {
2876 ANV_FROM_HANDLE(anv_device, device, _device);
2877 struct anv_framebuffer *framebuffer;
2878
2879 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
2880
2881 size_t size = sizeof(*framebuffer) +
2882 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
2883 framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
2884 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2885 if (framebuffer == NULL)
2886 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2887
2888 framebuffer->attachment_count = pCreateInfo->attachmentCount;
2889 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
2890 VkImageView _iview = pCreateInfo->pAttachments[i];
2891 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
2892 }
2893
2894 framebuffer->width = pCreateInfo->width;
2895 framebuffer->height = pCreateInfo->height;
2896 framebuffer->layers = pCreateInfo->layers;
2897
2898 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
2899
2900 return VK_SUCCESS;
2901 }
2902
2903 void anv_DestroyFramebuffer(
2904 VkDevice _device,
2905 VkFramebuffer _fb,
2906 const VkAllocationCallbacks* pAllocator)
2907 {
2908 ANV_FROM_HANDLE(anv_device, device, _device);
2909 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
2910
2911 if (!fb)
2912 return;
2913
2914 vk_free2(&device->alloc, pAllocator, fb);
2915 }
2916
2917 /* vk_icd.h does not declare this function, so we declare it here to
2918 * suppress Wmissing-prototypes.
2919 */
2920 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2921 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
2922
2923 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2924 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
2925 {
2926 /* For the full details on loader interface versioning, see
2927 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2928 * What follows is a condensed summary, to help you navigate the large and
2929 * confusing official doc.
2930 *
2931 * - Loader interface v0 is incompatible with later versions. We don't
2932 * support it.
2933 *
2934 * - In loader interface v1:
2935 * - The first ICD entrypoint called by the loader is
2936 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2937 * entrypoint.
2938 * - The ICD must statically expose no other Vulkan symbol unless it is
2939 * linked with -Bsymbolic.
2940 * - Each dispatchable Vulkan handle created by the ICD must be
2941 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2942 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2943 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2944 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2945 * such loader-managed surfaces.
2946 *
2947 * - Loader interface v2 differs from v1 in:
2948 * - The first ICD entrypoint called by the loader is
2949 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2950 * statically expose this entrypoint.
2951 *
2952 * - Loader interface v3 differs from v2 in:
2953 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2954 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2955 * because the loader no longer does so.
2956 */
2957 *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
2958 return VK_SUCCESS;
2959 }