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