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