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