vk: Handle allocation failures in meta init paths
[mesa.git] / src / 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 <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33
34 #include "gen7_pack.h"
35
36 struct anv_dispatch_table dtable;
37
38 static void
39 compiler_debug_log(void *data, const char *fmt, ...)
40 { }
41
42 static void
43 compiler_perf_log(void *data, const char *fmt, ...)
44 {
45 va_list args;
46 va_start(args, fmt);
47
48 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
49 vfprintf(stderr, fmt, args);
50
51 va_end(args);
52 }
53
54 static VkResult
55 anv_physical_device_init(struct anv_physical_device *device,
56 struct anv_instance *instance,
57 const char *path)
58 {
59 VkResult result;
60 int fd;
61
62 fd = open(path, O_RDWR | O_CLOEXEC);
63 if (fd < 0)
64 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
65 "failed to open %s: %m", path);
66
67 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
68 device->instance = instance;
69 device->path = path;
70
71 device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
72 if (!device->chipset_id) {
73 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
74 "failed to get chipset id: %m");
75 goto fail;
76 }
77
78 device->name = brw_get_device_name(device->chipset_id);
79 device->info = brw_get_device_info(device->chipset_id);
80 if (!device->info) {
81 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
82 "failed to get device info");
83 goto fail;
84 }
85
86 if (device->info->is_haswell) {
87 fprintf(stderr, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device->info->gen == 7 && !device->info->is_baytrail) {
89 fprintf(stderr, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
90 } else if (device->info->gen == 7 && device->info->is_baytrail) {
91 fprintf(stderr, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device->info->gen == 9 && !device->info->is_broxton) {
93 fprintf(stderr, "WARNING: Skylake Vulkan support is incomplete\n");
94 } else if (device->info->gen == 9 && device->info->is_broxton) {
95 fprintf(stderr, "WARNING: Broxton Vulkan support is incomplete\n");
96 } else if (device->info->gen == 8) {
97 /* Broadwell/Cherryview is as fully supported as anything */
98 } else {
99 result = vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER,
100 "Vulkan not yet supported on %s", device->name);
101 goto fail;
102 }
103
104 if (anv_gem_get_aperture(fd, &device->aperture_size) == -1) {
105 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
106 "failed to get aperture size: %m");
107 goto fail;
108 }
109
110 if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
111 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
112 "kernel missing gem wait");
113 goto fail;
114 }
115
116 if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
117 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
118 "kernel missing execbuf2");
119 goto fail;
120 }
121
122 if (!device->info->has_llc &&
123 anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
124 result = vk_errorf(VK_ERROR_INITIALIZATION_FAILED,
125 "kernel missing wc mmap");
126 goto fail;
127 }
128
129 close(fd);
130
131 brw_process_intel_debug_variable();
132
133 device->compiler = brw_compiler_create(NULL, device->info);
134 if (device->compiler == NULL) {
135 result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
136 goto fail;
137 }
138 device->compiler->shader_debug_log = compiler_debug_log;
139 device->compiler->shader_perf_log = compiler_perf_log;
140
141 isl_device_init(&device->isl_dev, device->info);
142
143 return VK_SUCCESS;
144
145 fail:
146 close(fd);
147 return result;
148 }
149
150 static void
151 anv_physical_device_finish(struct anv_physical_device *device)
152 {
153 ralloc_free(device->compiler);
154 }
155
156 static const VkExtensionProperties global_extensions[] = {
157 {
158 .extensionName = VK_KHR_SURFACE_EXTENSION_NAME,
159 .specVersion = 24,
160 },
161 {
162 .extensionName = VK_KHR_XCB_SURFACE_EXTENSION_NAME,
163 .specVersion = 5,
164 },
165 #ifdef HAVE_WAYLAND_PLATFORM
166 {
167 .extensionName = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
168 .specVersion = 4,
169 },
170 #endif
171 };
172
173 static const VkExtensionProperties device_extensions[] = {
174 {
175 .extensionName = VK_KHR_SWAPCHAIN_EXTENSION_NAME,
176 .specVersion = 67,
177 },
178 };
179
180 static void *
181 default_alloc_func(void *pUserData, size_t size, size_t align,
182 VkSystemAllocationScope allocationScope)
183 {
184 return malloc(size);
185 }
186
187 static void *
188 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
189 size_t align, VkSystemAllocationScope allocationScope)
190 {
191 return realloc(pOriginal, size);
192 }
193
194 static void
195 default_free_func(void *pUserData, void *pMemory)
196 {
197 free(pMemory);
198 }
199
200 static const VkAllocationCallbacks default_alloc = {
201 .pUserData = NULL,
202 .pfnAllocation = default_alloc_func,
203 .pfnReallocation = default_realloc_func,
204 .pfnFree = default_free_func,
205 };
206
207 VkResult anv_CreateInstance(
208 const VkInstanceCreateInfo* pCreateInfo,
209 const VkAllocationCallbacks* pAllocator,
210 VkInstance* pInstance)
211 {
212 struct anv_instance *instance;
213
214 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
215
216 if (pCreateInfo->pApplicationInfo->apiVersion != VK_MAKE_VERSION(0, 210, 1))
217 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
218
219 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
220 bool found = false;
221 for (uint32_t j = 0; j < ARRAY_SIZE(global_extensions); j++) {
222 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
223 global_extensions[j].extensionName) == 0) {
224 found = true;
225 break;
226 }
227 }
228 if (!found)
229 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
230 }
231
232 instance = anv_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
233 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
234 if (!instance)
235 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
236
237 instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
238
239 if (pAllocator)
240 instance->alloc = *pAllocator;
241 else
242 instance->alloc = default_alloc;
243
244 instance->apiVersion = pCreateInfo->pApplicationInfo->apiVersion;
245 instance->physicalDeviceCount = -1;
246
247 _mesa_locale_init();
248
249 VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
250
251 anv_init_wsi(instance);
252
253 *pInstance = anv_instance_to_handle(instance);
254
255 return VK_SUCCESS;
256 }
257
258 void anv_DestroyInstance(
259 VkInstance _instance,
260 const VkAllocationCallbacks* pAllocator)
261 {
262 ANV_FROM_HANDLE(anv_instance, instance, _instance);
263
264 if (instance->physicalDeviceCount > 0) {
265 /* We support at most one physical device. */
266 assert(instance->physicalDeviceCount == 1);
267 anv_physical_device_finish(&instance->physicalDevice);
268 }
269
270 anv_finish_wsi(instance);
271
272 VG(VALGRIND_DESTROY_MEMPOOL(instance));
273
274 _mesa_locale_fini();
275
276 anv_free(&instance->alloc, instance);
277 }
278
279 VkResult anv_EnumeratePhysicalDevices(
280 VkInstance _instance,
281 uint32_t* pPhysicalDeviceCount,
282 VkPhysicalDevice* pPhysicalDevices)
283 {
284 ANV_FROM_HANDLE(anv_instance, instance, _instance);
285 VkResult result;
286
287 if (instance->physicalDeviceCount < 0) {
288 result = anv_physical_device_init(&instance->physicalDevice,
289 instance, "/dev/dri/renderD128");
290 if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
291 instance->physicalDeviceCount = 0;
292 } else if (result == VK_SUCCESS) {
293 instance->physicalDeviceCount = 1;
294 } else {
295 return result;
296 }
297 }
298
299 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
300 * otherwise it's an inout parameter.
301 *
302 * The Vulkan spec (git aaed022) says:
303 *
304 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
305 * that is initialized with the number of devices the application is
306 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
307 * an array of at least this many VkPhysicalDevice handles [...].
308 *
309 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
310 * overwrites the contents of the variable pointed to by
311 * pPhysicalDeviceCount with the number of physical devices in in the
312 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
313 * pPhysicalDeviceCount with the number of physical handles written to
314 * pPhysicalDevices.
315 */
316 if (!pPhysicalDevices) {
317 *pPhysicalDeviceCount = instance->physicalDeviceCount;
318 } else if (*pPhysicalDeviceCount >= 1) {
319 pPhysicalDevices[0] = anv_physical_device_to_handle(&instance->physicalDevice);
320 *pPhysicalDeviceCount = 1;
321 } else {
322 *pPhysicalDeviceCount = 0;
323 }
324
325 return VK_SUCCESS;
326 }
327
328 void anv_GetPhysicalDeviceFeatures(
329 VkPhysicalDevice physicalDevice,
330 VkPhysicalDeviceFeatures* pFeatures)
331 {
332 anv_finishme("Get correct values for PhysicalDeviceFeatures");
333
334 *pFeatures = (VkPhysicalDeviceFeatures) {
335 .robustBufferAccess = false,
336 .fullDrawIndexUint32 = false,
337 .imageCubeArray = false,
338 .independentBlend = false,
339 .geometryShader = true,
340 .tessellationShader = false,
341 .sampleRateShading = false,
342 .dualSrcBlend = true,
343 .logicOp = true,
344 .multiDrawIndirect = true,
345 .depthClamp = false,
346 .depthBiasClamp = false,
347 .fillModeNonSolid = true,
348 .depthBounds = false,
349 .wideLines = true,
350 .largePoints = true,
351 .alphaToOne = true,
352 .multiViewport = true,
353 .samplerAnisotropy = false, /* FINISHME */
354 .textureCompressionETC2 = true,
355 .textureCompressionASTC_LDR = true,
356 .textureCompressionBC = true,
357 .occlusionQueryPrecise = false, /* FINISHME */
358 .pipelineStatisticsQuery = true,
359 .vertexPipelineStoresAndAtomics = false,
360 .fragmentStoresAndAtomics = true,
361 .shaderTessellationAndGeometryPointSize = true,
362 .shaderImageGatherExtended = true,
363 .shaderStorageImageExtendedFormats = false,
364 .shaderStorageImageMultisample = false,
365 .shaderUniformBufferArrayDynamicIndexing = true,
366 .shaderSampledImageArrayDynamicIndexing = false,
367 .shaderStorageBufferArrayDynamicIndexing = false,
368 .shaderStorageImageArrayDynamicIndexing = false,
369 .shaderStorageImageReadWithoutFormat = false,
370 .shaderStorageImageWriteWithoutFormat = true,
371 .shaderClipDistance = false,
372 .shaderCullDistance = false,
373 .shaderFloat64 = false,
374 .shaderInt64 = false,
375 .shaderInt16 = false,
376 .alphaToOne = true,
377 .variableMultisampleRate = false,
378 };
379 }
380
381 void anv_GetPhysicalDeviceProperties(
382 VkPhysicalDevice physicalDevice,
383 VkPhysicalDeviceProperties* pProperties)
384 {
385 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
386 const struct brw_device_info *devinfo = pdevice->info;
387
388 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
389
390 VkSampleCountFlags sample_counts =
391 VK_SAMPLE_COUNT_1_BIT |
392 VK_SAMPLE_COUNT_2_BIT |
393 VK_SAMPLE_COUNT_4_BIT |
394 VK_SAMPLE_COUNT_8_BIT;
395
396 VkPhysicalDeviceLimits limits = {
397 .maxImageDimension1D = (1 << 14),
398 .maxImageDimension2D = (1 << 14),
399 .maxImageDimension3D = (1 << 10),
400 .maxImageDimensionCube = (1 << 14),
401 .maxImageArrayLayers = (1 << 10),
402 .maxTexelBufferElements = (1 << 14),
403 .maxUniformBufferRange = UINT32_MAX,
404 .maxStorageBufferRange = UINT32_MAX,
405 .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
406 .maxMemoryAllocationCount = UINT32_MAX,
407 .maxSamplerAllocationCount = UINT32_MAX,
408 .bufferImageGranularity = 64, /* A cache line */
409 .sparseAddressSpaceSize = 0,
410 .maxBoundDescriptorSets = MAX_SETS,
411 .maxPerStageDescriptorSamplers = 64,
412 .maxPerStageDescriptorUniformBuffers = 64,
413 .maxPerStageDescriptorStorageBuffers = 64,
414 .maxPerStageDescriptorSampledImages = 64,
415 .maxPerStageDescriptorStorageImages = 64,
416 .maxPerStageDescriptorInputAttachments = 64,
417 .maxPerStageResources = 128,
418 .maxDescriptorSetSamplers = 256,
419 .maxDescriptorSetUniformBuffers = 256,
420 .maxDescriptorSetUniformBuffersDynamic = 256,
421 .maxDescriptorSetStorageBuffers = 256,
422 .maxDescriptorSetStorageBuffersDynamic = 256,
423 .maxDescriptorSetSampledImages = 256,
424 .maxDescriptorSetStorageImages = 256,
425 .maxDescriptorSetInputAttachments = 256,
426 .maxVertexInputAttributes = 32,
427 .maxVertexInputBindings = 32,
428 .maxVertexInputAttributeOffset = 256,
429 .maxVertexInputBindingStride = 256,
430 .maxVertexOutputComponents = 32,
431 .maxTessellationGenerationLevel = 0,
432 .maxTessellationPatchSize = 0,
433 .maxTessellationControlPerVertexInputComponents = 0,
434 .maxTessellationControlPerVertexOutputComponents = 0,
435 .maxTessellationControlPerPatchOutputComponents = 0,
436 .maxTessellationControlTotalOutputComponents = 0,
437 .maxTessellationEvaluationInputComponents = 0,
438 .maxTessellationEvaluationOutputComponents = 0,
439 .maxGeometryShaderInvocations = 6,
440 .maxGeometryInputComponents = 16,
441 .maxGeometryOutputComponents = 16,
442 .maxGeometryOutputVertices = 16,
443 .maxGeometryTotalOutputComponents = 16,
444 .maxFragmentInputComponents = 16,
445 .maxFragmentOutputAttachments = 8,
446 .maxFragmentDualSrcAttachments = 2,
447 .maxFragmentCombinedOutputResources = 8,
448 .maxComputeSharedMemorySize = 1024,
449 .maxComputeWorkGroupCount = {
450 16 * devinfo->max_cs_threads,
451 16 * devinfo->max_cs_threads,
452 16 * devinfo->max_cs_threads,
453 },
454 .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
455 .maxComputeWorkGroupSize = {
456 16 * devinfo->max_cs_threads,
457 16 * devinfo->max_cs_threads,
458 16 * devinfo->max_cs_threads,
459 },
460 .subPixelPrecisionBits = 4 /* FIXME */,
461 .subTexelPrecisionBits = 4 /* FIXME */,
462 .mipmapPrecisionBits = 4 /* FIXME */,
463 .maxDrawIndexedIndexValue = UINT32_MAX,
464 .maxDrawIndirectCount = UINT32_MAX,
465 .maxSamplerLodBias = 16,
466 .maxSamplerAnisotropy = 16,
467 .maxViewports = MAX_VIEWPORTS,
468 .maxViewportDimensions = { (1 << 14), (1 << 14) },
469 .viewportBoundsRange = { -1.0, 1.0 }, /* FIXME */
470 .viewportSubPixelBits = 13, /* We take a float? */
471 .minMemoryMapAlignment = 4096, /* A page */
472 .minTexelBufferOffsetAlignment = 1,
473 .minUniformBufferOffsetAlignment = 1,
474 .minStorageBufferOffsetAlignment = 1,
475 .minTexelOffset = 0, /* FIXME */
476 .maxTexelOffset = 0, /* FIXME */
477 .minTexelGatherOffset = 0, /* FIXME */
478 .maxTexelGatherOffset = 0, /* FIXME */
479 .minInterpolationOffset = 0, /* FIXME */
480 .maxInterpolationOffset = 0, /* FIXME */
481 .subPixelInterpolationOffsetBits = 0, /* FIXME */
482 .maxFramebufferWidth = (1 << 14),
483 .maxFramebufferHeight = (1 << 14),
484 .maxFramebufferLayers = (1 << 10),
485 .framebufferColorSampleCounts = sample_counts,
486 .framebufferDepthSampleCounts = sample_counts,
487 .framebufferStencilSampleCounts = sample_counts,
488 .framebufferNoAttachmentsSampleCounts = sample_counts,
489 .maxColorAttachments = MAX_RTS,
490 .sampledImageColorSampleCounts = sample_counts,
491 .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
492 .sampledImageDepthSampleCounts = sample_counts,
493 .sampledImageStencilSampleCounts = sample_counts,
494 .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
495 .maxSampleMaskWords = 1,
496 .timestampPeriod = 80.0 / (1000 * 1000 * 1000),
497 .maxClipDistances = 0 /* FIXME */,
498 .maxCullDistances = 0 /* FIXME */,
499 .maxCombinedClipAndCullDistances = 0 /* FIXME */,
500 .discreteQueuePriorities = 1,
501 .pointSizeRange = { 0.125, 255.875 },
502 .lineWidthRange = { 0.0, 7.9921875 },
503 .pointSizeGranularity = (1.0 / 8.0),
504 .lineWidthGranularity = (1.0 / 128.0),
505 .strictLines = false, /* FINISHME */
506 .standardSampleLocations = true, /* FINISHME */
507 .optimalBufferCopyOffsetAlignment = 128,
508 .optimalBufferCopyRowPitchAlignment = 128,
509 .nonCoherentAtomSize = 64,
510 };
511
512 *pProperties = (VkPhysicalDeviceProperties) {
513 .apiVersion = VK_MAKE_VERSION(0, 210, 1),
514 .driverVersion = 1,
515 .vendorID = 0x8086,
516 .deviceID = pdevice->chipset_id,
517 .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
518 .limits = limits,
519 .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
520 };
521
522 strcpy(pProperties->deviceName, pdevice->name);
523 snprintf((char *)pProperties->pipelineCacheUUID, VK_UUID_SIZE,
524 "anv-%s", MESA_GIT_SHA1 + 4);
525 }
526
527 void anv_GetPhysicalDeviceQueueFamilyProperties(
528 VkPhysicalDevice physicalDevice,
529 uint32_t* pCount,
530 VkQueueFamilyProperties* pQueueFamilyProperties)
531 {
532 if (pQueueFamilyProperties == NULL) {
533 *pCount = 1;
534 return;
535 }
536
537 assert(*pCount >= 1);
538
539 *pQueueFamilyProperties = (VkQueueFamilyProperties) {
540 .queueFlags = VK_QUEUE_GRAPHICS_BIT |
541 VK_QUEUE_COMPUTE_BIT |
542 VK_QUEUE_TRANSFER_BIT,
543 .queueCount = 1,
544 .timestampValidBits = 0, /* XXX: Real value here */
545 .minImageTransferGranularity = (VkExtent3D) { 1, 1, 1 },
546 };
547 }
548
549 void anv_GetPhysicalDeviceMemoryProperties(
550 VkPhysicalDevice physicalDevice,
551 VkPhysicalDeviceMemoryProperties* pMemoryProperties)
552 {
553 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
554 VkDeviceSize heap_size;
555
556 /* Reserve some wiggle room for the driver by exposing only 75% of the
557 * aperture to the heap.
558 */
559 heap_size = 3 * physical_device->aperture_size / 4;
560
561 if (physical_device->info->has_llc) {
562 /* Big core GPUs share LLC with the CPU and thus one memory type can be
563 * both cached and coherent at the same time.
564 */
565 pMemoryProperties->memoryTypeCount = 1;
566 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
567 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
568 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
569 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
570 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
571 .heapIndex = 0,
572 };
573 } else {
574 /* The spec requires that we expose a host-visible, coherent memory
575 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
576 * to give the application a choice between cached, but not coherent and
577 * coherent but uncached (WC though).
578 */
579 pMemoryProperties->memoryTypeCount = 2;
580 pMemoryProperties->memoryTypes[0] = (VkMemoryType) {
581 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
582 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
583 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
584 .heapIndex = 0,
585 };
586 pMemoryProperties->memoryTypes[1] = (VkMemoryType) {
587 .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
588 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
589 VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
590 .heapIndex = 0,
591 };
592 }
593
594 pMemoryProperties->memoryHeapCount = 1;
595 pMemoryProperties->memoryHeaps[0] = (VkMemoryHeap) {
596 .size = heap_size,
597 .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
598 };
599 }
600
601 PFN_vkVoidFunction anv_GetInstanceProcAddr(
602 VkInstance instance,
603 const char* pName)
604 {
605 return anv_lookup_entrypoint(pName);
606 }
607
608 PFN_vkVoidFunction anv_GetDeviceProcAddr(
609 VkDevice device,
610 const char* pName)
611 {
612 return anv_lookup_entrypoint(pName);
613 }
614
615 static VkResult
616 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
617 {
618 queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
619 queue->device = device;
620 queue->pool = &device->surface_state_pool;
621
622 return VK_SUCCESS;
623 }
624
625 static void
626 anv_queue_finish(struct anv_queue *queue)
627 {
628 }
629
630 static struct anv_state
631 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
632 {
633 struct anv_state state;
634
635 state = anv_state_pool_alloc(pool, size, align);
636 memcpy(state.map, p, size);
637
638 if (!pool->block_pool->device->info.has_llc)
639 anv_state_clflush(state);
640
641 return state;
642 }
643
644 static void
645 anv_device_init_border_colors(struct anv_device *device)
646 {
647 static const VkClearColorValue border_colors[] = {
648 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
649 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
650 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
651 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
652 [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
653 [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
654 };
655
656 device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
657 sizeof(border_colors), 32, border_colors);
658 }
659
660 VkResult anv_CreateDevice(
661 VkPhysicalDevice physicalDevice,
662 const VkDeviceCreateInfo* pCreateInfo,
663 const VkAllocationCallbacks* pAllocator,
664 VkDevice* pDevice)
665 {
666 ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
667 VkResult result;
668 struct anv_device *device;
669
670 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
671
672 for (uint32_t i = 0; i < pCreateInfo->enabledExtensionNameCount; i++) {
673 bool found = false;
674 for (uint32_t j = 0; j < ARRAY_SIZE(device_extensions); j++) {
675 if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
676 device_extensions[j].extensionName) == 0) {
677 found = true;
678 break;
679 }
680 }
681 if (!found)
682 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
683 }
684
685 anv_set_dispatch_devinfo(physical_device->info);
686
687 device = anv_alloc2(&physical_device->instance->alloc, pAllocator,
688 sizeof(*device), 8,
689 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
690 if (!device)
691 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
692
693 device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
694 device->instance = physical_device->instance;
695
696 if (pAllocator)
697 device->alloc = *pAllocator;
698 else
699 device->alloc = physical_device->instance->alloc;
700
701 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
702 device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
703 if (device->fd == -1) {
704 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
705 goto fail_device;
706 }
707
708 device->context_id = anv_gem_create_context(device);
709 if (device->context_id == -1) {
710 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
711 goto fail_fd;
712 }
713
714 device->info = *physical_device->info;
715 device->isl_dev = physical_device->isl_dev;
716
717 pthread_mutex_init(&device->mutex, NULL);
718
719 anv_bo_pool_init(&device->batch_bo_pool, device, ANV_CMD_BUFFER_BATCH_SIZE);
720
721 anv_block_pool_init(&device->dynamic_state_block_pool, device, 2048);
722
723 anv_state_pool_init(&device->dynamic_state_pool,
724 &device->dynamic_state_block_pool);
725
726 anv_block_pool_init(&device->instruction_block_pool, device, 8192);
727 anv_block_pool_init(&device->surface_state_block_pool, device, 4096);
728
729 anv_state_pool_init(&device->surface_state_pool,
730 &device->surface_state_block_pool);
731
732 anv_bo_init_new(&device->workaround_bo, device, 1024);
733
734 anv_block_pool_init(&device->scratch_block_pool, device, 0x10000);
735
736 anv_queue_init(device, &device->queue);
737
738 result = anv_device_init_meta(device);
739 if (result != VK_SUCCESS)
740 goto fail_fd;
741
742 anv_device_init_border_colors(device);
743
744 *pDevice = anv_device_to_handle(device);
745
746 return VK_SUCCESS;
747
748 fail_fd:
749 close(device->fd);
750 fail_device:
751 anv_free(&device->alloc, device);
752
753 return result;
754 }
755
756 void anv_DestroyDevice(
757 VkDevice _device,
758 const VkAllocationCallbacks* pAllocator)
759 {
760 ANV_FROM_HANDLE(anv_device, device, _device);
761
762 anv_queue_finish(&device->queue);
763
764 anv_device_finish_meta(device);
765
766 #ifdef HAVE_VALGRIND
767 /* We only need to free these to prevent valgrind errors. The backing
768 * BO will go away in a couple of lines so we don't actually leak.
769 */
770 anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
771 #endif
772
773 anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
774 anv_gem_close(device, device->workaround_bo.gem_handle);
775
776 anv_bo_pool_finish(&device->batch_bo_pool);
777 anv_state_pool_finish(&device->dynamic_state_pool);
778 anv_block_pool_finish(&device->dynamic_state_block_pool);
779 anv_block_pool_finish(&device->instruction_block_pool);
780 anv_state_pool_finish(&device->surface_state_pool);
781 anv_block_pool_finish(&device->surface_state_block_pool);
782 anv_block_pool_finish(&device->scratch_block_pool);
783
784 close(device->fd);
785
786 anv_free(&device->alloc, device);
787 }
788
789 VkResult anv_EnumerateInstanceExtensionProperties(
790 const char* pLayerName,
791 uint32_t* pPropertyCount,
792 VkExtensionProperties* pProperties)
793 {
794 if (pProperties == NULL) {
795 *pPropertyCount = ARRAY_SIZE(global_extensions);
796 return VK_SUCCESS;
797 }
798
799 assert(*pPropertyCount >= ARRAY_SIZE(global_extensions));
800
801 *pPropertyCount = ARRAY_SIZE(global_extensions);
802 memcpy(pProperties, global_extensions, sizeof(global_extensions));
803
804 return VK_SUCCESS;
805 }
806
807 VkResult anv_EnumerateDeviceExtensionProperties(
808 VkPhysicalDevice physicalDevice,
809 const char* pLayerName,
810 uint32_t* pPropertyCount,
811 VkExtensionProperties* pProperties)
812 {
813 if (pProperties == NULL) {
814 *pPropertyCount = ARRAY_SIZE(device_extensions);
815 return VK_SUCCESS;
816 }
817
818 assert(*pPropertyCount >= ARRAY_SIZE(device_extensions));
819
820 *pPropertyCount = ARRAY_SIZE(device_extensions);
821 memcpy(pProperties, device_extensions, sizeof(device_extensions));
822
823 return VK_SUCCESS;
824 }
825
826 VkResult anv_EnumerateInstanceLayerProperties(
827 uint32_t* pPropertyCount,
828 VkLayerProperties* pProperties)
829 {
830 if (pProperties == NULL) {
831 *pPropertyCount = 0;
832 return VK_SUCCESS;
833 }
834
835 /* None supported at this time */
836 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
837 }
838
839 VkResult anv_EnumerateDeviceLayerProperties(
840 VkPhysicalDevice physicalDevice,
841 uint32_t* pPropertyCount,
842 VkLayerProperties* pProperties)
843 {
844 if (pProperties == NULL) {
845 *pPropertyCount = 0;
846 return VK_SUCCESS;
847 }
848
849 /* None supported at this time */
850 return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
851 }
852
853 void anv_GetDeviceQueue(
854 VkDevice _device,
855 uint32_t queueNodeIndex,
856 uint32_t queueIndex,
857 VkQueue* pQueue)
858 {
859 ANV_FROM_HANDLE(anv_device, device, _device);
860
861 assert(queueIndex == 0);
862
863 *pQueue = anv_queue_to_handle(&device->queue);
864 }
865
866 VkResult anv_QueueSubmit(
867 VkQueue _queue,
868 uint32_t submitCount,
869 const VkSubmitInfo* pSubmits,
870 VkFence _fence)
871 {
872 ANV_FROM_HANDLE(anv_queue, queue, _queue);
873 ANV_FROM_HANDLE(anv_fence, fence, _fence);
874 struct anv_device *device = queue->device;
875 int ret;
876
877 for (uint32_t i = 0; i < submitCount; i++) {
878 for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
879 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer,
880 pSubmits[i].pCommandBuffers[j]);
881 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
882
883 ret = anv_gem_execbuffer(device, &cmd_buffer->execbuf2.execbuf);
884 if (ret != 0) {
885 /* We don't know the real error. */
886 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
887 "execbuf2 failed: %m");
888 }
889
890 if (fence) {
891 ret = anv_gem_execbuffer(device, &fence->execbuf);
892 if (ret != 0) {
893 /* We don't know the real error. */
894 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
895 "execbuf2 failed: %m");
896 }
897 }
898
899 for (uint32_t k = 0; k < cmd_buffer->execbuf2.bo_count; k++)
900 cmd_buffer->execbuf2.bos[k]->offset = cmd_buffer->execbuf2.objects[k].offset;
901 }
902 }
903
904 return VK_SUCCESS;
905 }
906
907 VkResult anv_QueueWaitIdle(
908 VkQueue _queue)
909 {
910 ANV_FROM_HANDLE(anv_queue, queue, _queue);
911
912 return ANV_CALL(DeviceWaitIdle)(anv_device_to_handle(queue->device));
913 }
914
915 VkResult anv_DeviceWaitIdle(
916 VkDevice _device)
917 {
918 ANV_FROM_HANDLE(anv_device, device, _device);
919 struct anv_state state;
920 struct anv_batch batch;
921 struct drm_i915_gem_execbuffer2 execbuf;
922 struct drm_i915_gem_exec_object2 exec2_objects[1];
923 struct anv_bo *bo = NULL;
924 VkResult result;
925 int64_t timeout;
926 int ret;
927
928 state = anv_state_pool_alloc(&device->dynamic_state_pool, 32, 32);
929 bo = &device->dynamic_state_pool.block_pool->bo;
930 batch.start = batch.next = state.map;
931 batch.end = state.map + 32;
932 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
933 anv_batch_emit(&batch, GEN7_MI_NOOP);
934
935 if (!device->info.has_llc)
936 anv_state_clflush(state);
937
938 exec2_objects[0].handle = bo->gem_handle;
939 exec2_objects[0].relocation_count = 0;
940 exec2_objects[0].relocs_ptr = 0;
941 exec2_objects[0].alignment = 0;
942 exec2_objects[0].offset = bo->offset;
943 exec2_objects[0].flags = 0;
944 exec2_objects[0].rsvd1 = 0;
945 exec2_objects[0].rsvd2 = 0;
946
947 execbuf.buffers_ptr = (uintptr_t) exec2_objects;
948 execbuf.buffer_count = 1;
949 execbuf.batch_start_offset = state.offset;
950 execbuf.batch_len = batch.next - state.map;
951 execbuf.cliprects_ptr = 0;
952 execbuf.num_cliprects = 0;
953 execbuf.DR1 = 0;
954 execbuf.DR4 = 0;
955
956 execbuf.flags =
957 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
958 execbuf.rsvd1 = device->context_id;
959 execbuf.rsvd2 = 0;
960
961 ret = anv_gem_execbuffer(device, &execbuf);
962 if (ret != 0) {
963 /* We don't know the real error. */
964 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
965 goto fail;
966 }
967
968 timeout = INT64_MAX;
969 ret = anv_gem_wait(device, bo->gem_handle, &timeout);
970 if (ret != 0) {
971 /* We don't know the real error. */
972 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY, "execbuf2 failed: %m");
973 goto fail;
974 }
975
976 anv_state_pool_free(&device->dynamic_state_pool, state);
977
978 return VK_SUCCESS;
979
980 fail:
981 anv_state_pool_free(&device->dynamic_state_pool, state);
982
983 return result;
984 }
985
986 VkResult
987 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
988 {
989 bo->gem_handle = anv_gem_create(device, size);
990 if (!bo->gem_handle)
991 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
992
993 bo->map = NULL;
994 bo->index = 0;
995 bo->offset = 0;
996 bo->size = size;
997
998 return VK_SUCCESS;
999 }
1000
1001 VkResult anv_AllocateMemory(
1002 VkDevice _device,
1003 const VkMemoryAllocateInfo* pAllocateInfo,
1004 const VkAllocationCallbacks* pAllocator,
1005 VkDeviceMemory* pMem)
1006 {
1007 ANV_FROM_HANDLE(anv_device, device, _device);
1008 struct anv_device_memory *mem;
1009 VkResult result;
1010
1011 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
1012
1013 if (pAllocateInfo->allocationSize == 0) {
1014 /* Apparently, this is allowed */
1015 *pMem = VK_NULL_HANDLE;
1016 return VK_SUCCESS;
1017 }
1018
1019 /* We support exactly one memory heap. */
1020 assert(pAllocateInfo->memoryTypeIndex == 0 ||
1021 (!device->info.has_llc && pAllocateInfo->memoryTypeIndex < 2));
1022
1023 /* FINISHME: Fail if allocation request exceeds heap size. */
1024
1025 mem = anv_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
1026 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1027 if (mem == NULL)
1028 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1029
1030 /* The kernel is going to give us whole pages anyway */
1031 uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
1032
1033 result = anv_bo_init_new(&mem->bo, device, alloc_size);
1034 if (result != VK_SUCCESS)
1035 goto fail;
1036
1037 mem->type_index = pAllocateInfo->memoryTypeIndex;
1038
1039 *pMem = anv_device_memory_to_handle(mem);
1040
1041 return VK_SUCCESS;
1042
1043 fail:
1044 anv_free2(&device->alloc, pAllocator, mem);
1045
1046 return result;
1047 }
1048
1049 void anv_FreeMemory(
1050 VkDevice _device,
1051 VkDeviceMemory _mem,
1052 const VkAllocationCallbacks* pAllocator)
1053 {
1054 ANV_FROM_HANDLE(anv_device, device, _device);
1055 ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
1056
1057 if (mem == NULL)
1058 return;
1059
1060 if (mem->bo.map)
1061 anv_gem_munmap(mem->bo.map, mem->bo.size);
1062
1063 if (mem->bo.gem_handle != 0)
1064 anv_gem_close(device, mem->bo.gem_handle);
1065
1066 anv_free2(&device->alloc, pAllocator, mem);
1067 }
1068
1069 VkResult anv_MapMemory(
1070 VkDevice _device,
1071 VkDeviceMemory _memory,
1072 VkDeviceSize offset,
1073 VkDeviceSize size,
1074 VkMemoryMapFlags flags,
1075 void** ppData)
1076 {
1077 ANV_FROM_HANDLE(anv_device, device, _device);
1078 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1079
1080 if (mem == NULL) {
1081 *ppData = NULL;
1082 return VK_SUCCESS;
1083 }
1084
1085 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1086 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1087 * at a time is valid. We could just mmap up front and return an offset
1088 * pointer here, but that may exhaust virtual memory on 32 bit
1089 * userspace. */
1090
1091 uint32_t gem_flags = 0;
1092 if (!device->info.has_llc && mem->type_index == 0)
1093 gem_flags |= I915_MMAP_WC;
1094
1095 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1096 uint64_t map_offset = offset & ~4095ull;
1097 assert(offset >= map_offset);
1098 uint64_t map_size = (offset + size) - map_offset;
1099
1100 /* Let's map whole pages */
1101 map_size = align_u64(map_size, 4096);
1102
1103 mem->map = anv_gem_mmap(device, mem->bo.gem_handle,
1104 map_offset, map_size, gem_flags);
1105 mem->map_size = map_size;
1106
1107 *ppData = mem->map + (offset - map_offset);
1108
1109 return VK_SUCCESS;
1110 }
1111
1112 void anv_UnmapMemory(
1113 VkDevice _device,
1114 VkDeviceMemory _memory)
1115 {
1116 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1117
1118 if (mem == NULL)
1119 return;
1120
1121 anv_gem_munmap(mem->map, mem->map_size);
1122 }
1123
1124 static void
1125 clflush_mapped_ranges(struct anv_device *device,
1126 uint32_t count,
1127 const VkMappedMemoryRange *ranges)
1128 {
1129 for (uint32_t i = 0; i < count; i++) {
1130 ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
1131 void *p = mem->map + (ranges[i].offset & ~CACHELINE_MASK);
1132 void *end = mem->map + ranges[i].offset + ranges[i].size;
1133
1134 while (p < end) {
1135 __builtin_ia32_clflush(p);
1136 p += CACHELINE_SIZE;
1137 }
1138 }
1139 }
1140
1141 VkResult anv_FlushMappedMemoryRanges(
1142 VkDevice _device,
1143 uint32_t memoryRangeCount,
1144 const VkMappedMemoryRange* pMemoryRanges)
1145 {
1146 ANV_FROM_HANDLE(anv_device, device, _device);
1147
1148 if (device->info.has_llc)
1149 return VK_SUCCESS;
1150
1151 /* Make sure the writes we're flushing have landed. */
1152 __builtin_ia32_sfence();
1153
1154 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1155
1156 return VK_SUCCESS;
1157 }
1158
1159 VkResult anv_InvalidateMappedMemoryRanges(
1160 VkDevice _device,
1161 uint32_t memoryRangeCount,
1162 const VkMappedMemoryRange* pMemoryRanges)
1163 {
1164 ANV_FROM_HANDLE(anv_device, device, _device);
1165
1166 if (device->info.has_llc)
1167 return VK_SUCCESS;
1168
1169 clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
1170
1171 /* Make sure no reads get moved up above the invalidate. */
1172 __builtin_ia32_lfence();
1173
1174 return VK_SUCCESS;
1175 }
1176
1177 void anv_GetBufferMemoryRequirements(
1178 VkDevice device,
1179 VkBuffer _buffer,
1180 VkMemoryRequirements* pMemoryRequirements)
1181 {
1182 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1183
1184 /* The Vulkan spec (git aaed022) says:
1185 *
1186 * memoryTypeBits is a bitfield and contains one bit set for every
1187 * supported memory type for the resource. The bit `1<<i` is set if and
1188 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1189 * structure for the physical device is supported.
1190 *
1191 * We support exactly one memory type.
1192 */
1193 pMemoryRequirements->memoryTypeBits = 1;
1194
1195 pMemoryRequirements->size = buffer->size;
1196 pMemoryRequirements->alignment = 16;
1197 }
1198
1199 void anv_GetImageMemoryRequirements(
1200 VkDevice device,
1201 VkImage _image,
1202 VkMemoryRequirements* pMemoryRequirements)
1203 {
1204 ANV_FROM_HANDLE(anv_image, image, _image);
1205
1206 /* The Vulkan spec (git aaed022) says:
1207 *
1208 * memoryTypeBits is a bitfield and contains one bit set for every
1209 * supported memory type for the resource. The bit `1<<i` is set if and
1210 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1211 * structure for the physical device is supported.
1212 *
1213 * We support exactly one memory type.
1214 */
1215 pMemoryRequirements->memoryTypeBits = 1;
1216
1217 pMemoryRequirements->size = image->size;
1218 pMemoryRequirements->alignment = image->alignment;
1219 }
1220
1221 void anv_GetImageSparseMemoryRequirements(
1222 VkDevice device,
1223 VkImage image,
1224 uint32_t* pSparseMemoryRequirementCount,
1225 VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
1226 {
1227 stub();
1228 }
1229
1230 void anv_GetDeviceMemoryCommitment(
1231 VkDevice device,
1232 VkDeviceMemory memory,
1233 VkDeviceSize* pCommittedMemoryInBytes)
1234 {
1235 *pCommittedMemoryInBytes = 0;
1236 }
1237
1238 VkResult anv_BindBufferMemory(
1239 VkDevice device,
1240 VkBuffer _buffer,
1241 VkDeviceMemory _memory,
1242 VkDeviceSize memoryOffset)
1243 {
1244 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1245 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1246
1247 if (mem) {
1248 buffer->bo = &mem->bo;
1249 buffer->offset = memoryOffset;
1250 } else {
1251 buffer->bo = NULL;
1252 buffer->offset = 0;
1253 }
1254
1255 return VK_SUCCESS;
1256 }
1257
1258 VkResult anv_BindImageMemory(
1259 VkDevice device,
1260 VkImage _image,
1261 VkDeviceMemory _memory,
1262 VkDeviceSize memoryOffset)
1263 {
1264 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
1265 ANV_FROM_HANDLE(anv_image, image, _image);
1266
1267 if (mem) {
1268 image->bo = &mem->bo;
1269 image->offset = memoryOffset;
1270 } else {
1271 image->bo = NULL;
1272 image->offset = 0;
1273 }
1274
1275 return VK_SUCCESS;
1276 }
1277
1278 VkResult anv_QueueBindSparse(
1279 VkQueue queue,
1280 uint32_t bindInfoCount,
1281 const VkBindSparseInfo* pBindInfo,
1282 VkFence fence)
1283 {
1284 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER);
1285 }
1286
1287 VkResult anv_CreateFence(
1288 VkDevice _device,
1289 const VkFenceCreateInfo* pCreateInfo,
1290 const VkAllocationCallbacks* pAllocator,
1291 VkFence* pFence)
1292 {
1293 ANV_FROM_HANDLE(anv_device, device, _device);
1294 struct anv_fence *fence;
1295 struct anv_batch batch;
1296 VkResult result;
1297
1298 const uint32_t fence_size = 128;
1299
1300 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FENCE_CREATE_INFO);
1301
1302 fence = anv_alloc2(&device->alloc, pAllocator, sizeof(*fence), 8,
1303 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1304 if (fence == NULL)
1305 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1306
1307 result = anv_bo_init_new(&fence->bo, device, fence_size);
1308 if (result != VK_SUCCESS)
1309 goto fail;
1310
1311 fence->bo.map =
1312 anv_gem_mmap(device, fence->bo.gem_handle, 0, fence->bo.size, 0);
1313 batch.next = batch.start = fence->bo.map;
1314 batch.end = fence->bo.map + fence->bo.size;
1315 anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END);
1316 anv_batch_emit(&batch, GEN7_MI_NOOP);
1317
1318 if (!device->info.has_llc) {
1319 assert(((uintptr_t) fence->bo.map & CACHELINE_MASK) == 0);
1320 assert(batch.next - fence->bo.map <= CACHELINE_SIZE);
1321 __builtin_ia32_sfence();
1322 __builtin_ia32_clflush(fence->bo.map);
1323 }
1324
1325 fence->exec2_objects[0].handle = fence->bo.gem_handle;
1326 fence->exec2_objects[0].relocation_count = 0;
1327 fence->exec2_objects[0].relocs_ptr = 0;
1328 fence->exec2_objects[0].alignment = 0;
1329 fence->exec2_objects[0].offset = fence->bo.offset;
1330 fence->exec2_objects[0].flags = 0;
1331 fence->exec2_objects[0].rsvd1 = 0;
1332 fence->exec2_objects[0].rsvd2 = 0;
1333
1334 fence->execbuf.buffers_ptr = (uintptr_t) fence->exec2_objects;
1335 fence->execbuf.buffer_count = 1;
1336 fence->execbuf.batch_start_offset = 0;
1337 fence->execbuf.batch_len = batch.next - fence->bo.map;
1338 fence->execbuf.cliprects_ptr = 0;
1339 fence->execbuf.num_cliprects = 0;
1340 fence->execbuf.DR1 = 0;
1341 fence->execbuf.DR4 = 0;
1342
1343 fence->execbuf.flags =
1344 I915_EXEC_HANDLE_LUT | I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
1345 fence->execbuf.rsvd1 = device->context_id;
1346 fence->execbuf.rsvd2 = 0;
1347
1348 *pFence = anv_fence_to_handle(fence);
1349
1350 return VK_SUCCESS;
1351
1352 fail:
1353 anv_free2(&device->alloc, pAllocator, fence);
1354
1355 return result;
1356 }
1357
1358 void anv_DestroyFence(
1359 VkDevice _device,
1360 VkFence _fence,
1361 const VkAllocationCallbacks* pAllocator)
1362 {
1363 ANV_FROM_HANDLE(anv_device, device, _device);
1364 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1365
1366 anv_gem_munmap(fence->bo.map, fence->bo.size);
1367 anv_gem_close(device, fence->bo.gem_handle);
1368 anv_free2(&device->alloc, pAllocator, fence);
1369 }
1370
1371 VkResult anv_ResetFences(
1372 VkDevice _device,
1373 uint32_t fenceCount,
1374 const VkFence* pFences)
1375 {
1376 for (uint32_t i = 0; i < fenceCount; i++) {
1377 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1378 fence->ready = false;
1379 }
1380
1381 return VK_SUCCESS;
1382 }
1383
1384 VkResult anv_GetFenceStatus(
1385 VkDevice _device,
1386 VkFence _fence)
1387 {
1388 ANV_FROM_HANDLE(anv_device, device, _device);
1389 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1390 int64_t t = 0;
1391 int ret;
1392
1393 if (fence->ready)
1394 return VK_SUCCESS;
1395
1396 ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1397 if (ret == 0) {
1398 fence->ready = true;
1399 return VK_SUCCESS;
1400 }
1401
1402 return VK_NOT_READY;
1403 }
1404
1405 VkResult anv_WaitForFences(
1406 VkDevice _device,
1407 uint32_t fenceCount,
1408 const VkFence* pFences,
1409 VkBool32 waitAll,
1410 uint64_t timeout)
1411 {
1412 ANV_FROM_HANDLE(anv_device, device, _device);
1413
1414 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1415 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1416 * for a couple of kernel releases. Since there's no way to know
1417 * whether or not the kernel we're using is one of the broken ones, the
1418 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1419 * maximum timeout from 584 years to 292 years - likely not a big deal.
1420 */
1421 if (timeout > INT64_MAX)
1422 timeout = INT64_MAX;
1423
1424 int64_t t = timeout;
1425
1426 /* FIXME: handle !waitAll */
1427
1428 for (uint32_t i = 0; i < fenceCount; i++) {
1429 ANV_FROM_HANDLE(anv_fence, fence, pFences[i]);
1430 int ret = anv_gem_wait(device, fence->bo.gem_handle, &t);
1431 if (ret == -1 && errno == ETIME) {
1432 return VK_TIMEOUT;
1433 } else if (ret == -1) {
1434 /* We don't know the real error. */
1435 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
1436 "gem wait failed: %m");
1437 }
1438 }
1439
1440 return VK_SUCCESS;
1441 }
1442
1443 // Queue semaphore functions
1444
1445 VkResult anv_CreateSemaphore(
1446 VkDevice device,
1447 const VkSemaphoreCreateInfo* pCreateInfo,
1448 const VkAllocationCallbacks* pAllocator,
1449 VkSemaphore* pSemaphore)
1450 {
1451 /* The DRM execbuffer ioctl always execute in-oder, even between different
1452 * rings. As such, there's nothing to do for the user space semaphore.
1453 */
1454
1455 *pSemaphore = (VkSemaphore)1;
1456
1457 return VK_SUCCESS;
1458 }
1459
1460 void anv_DestroySemaphore(
1461 VkDevice device,
1462 VkSemaphore semaphore,
1463 const VkAllocationCallbacks* pAllocator)
1464 {
1465 }
1466
1467 // Event functions
1468
1469 VkResult anv_CreateEvent(
1470 VkDevice _device,
1471 const VkEventCreateInfo* pCreateInfo,
1472 const VkAllocationCallbacks* pAllocator,
1473 VkEvent* pEvent)
1474 {
1475 ANV_FROM_HANDLE(anv_device, device, _device);
1476 struct anv_state state;
1477 struct anv_event *event;
1478
1479 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
1480
1481 state = anv_state_pool_alloc(&device->dynamic_state_pool,
1482 sizeof(*event), 4);
1483 event = state.map;
1484 event->state = state;
1485 event->semaphore = VK_EVENT_RESET;
1486
1487 if (!device->info.has_llc) {
1488 /* Make sure the writes we're flushing have landed. */
1489 __builtin_ia32_sfence();
1490 __builtin_ia32_clflush(event);
1491 }
1492
1493 *pEvent = anv_event_to_handle(event);
1494
1495 return VK_SUCCESS;
1496 }
1497
1498 void anv_DestroyEvent(
1499 VkDevice _device,
1500 VkEvent _event,
1501 const VkAllocationCallbacks* pAllocator)
1502 {
1503 ANV_FROM_HANDLE(anv_device, device, _device);
1504 ANV_FROM_HANDLE(anv_event, event, _event);
1505
1506 anv_state_pool_free(&device->dynamic_state_pool, event->state);
1507 }
1508
1509 VkResult anv_GetEventStatus(
1510 VkDevice _device,
1511 VkEvent _event)
1512 {
1513 ANV_FROM_HANDLE(anv_device, device, _device);
1514 ANV_FROM_HANDLE(anv_event, event, _event);
1515
1516 if (!device->info.has_llc) {
1517 /* Make sure the writes we're flushing have landed. */
1518 __builtin_ia32_clflush(event);
1519 __builtin_ia32_lfence();
1520 }
1521
1522 return event->semaphore;
1523 }
1524
1525 VkResult anv_SetEvent(
1526 VkDevice _device,
1527 VkEvent _event)
1528 {
1529 ANV_FROM_HANDLE(anv_device, device, _device);
1530 ANV_FROM_HANDLE(anv_event, event, _event);
1531
1532 event->semaphore = VK_EVENT_SET;
1533
1534 if (!device->info.has_llc) {
1535 /* Make sure the writes we're flushing have landed. */
1536 __builtin_ia32_sfence();
1537 __builtin_ia32_clflush(event);
1538 }
1539
1540 return VK_SUCCESS;
1541 }
1542
1543 VkResult anv_ResetEvent(
1544 VkDevice _device,
1545 VkEvent _event)
1546 {
1547 ANV_FROM_HANDLE(anv_device, device, _device);
1548 ANV_FROM_HANDLE(anv_event, event, _event);
1549
1550 event->semaphore = VK_EVENT_RESET;
1551
1552 if (!device->info.has_llc) {
1553 /* Make sure the writes we're flushing have landed. */
1554 __builtin_ia32_sfence();
1555 __builtin_ia32_clflush(event);
1556 }
1557
1558 return VK_SUCCESS;
1559 }
1560
1561 // Buffer functions
1562
1563 VkResult anv_CreateBuffer(
1564 VkDevice _device,
1565 const VkBufferCreateInfo* pCreateInfo,
1566 const VkAllocationCallbacks* pAllocator,
1567 VkBuffer* pBuffer)
1568 {
1569 ANV_FROM_HANDLE(anv_device, device, _device);
1570 struct anv_buffer *buffer;
1571
1572 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
1573
1574 buffer = anv_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
1575 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1576 if (buffer == NULL)
1577 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1578
1579 buffer->size = pCreateInfo->size;
1580 buffer->usage = pCreateInfo->usage;
1581 buffer->bo = NULL;
1582 buffer->offset = 0;
1583
1584 *pBuffer = anv_buffer_to_handle(buffer);
1585
1586 return VK_SUCCESS;
1587 }
1588
1589 void anv_DestroyBuffer(
1590 VkDevice _device,
1591 VkBuffer _buffer,
1592 const VkAllocationCallbacks* pAllocator)
1593 {
1594 ANV_FROM_HANDLE(anv_device, device, _device);
1595 ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
1596
1597 anv_free2(&device->alloc, pAllocator, buffer);
1598 }
1599
1600 void
1601 anv_fill_buffer_surface_state(struct anv_device *device, void *state,
1602 enum isl_format format,
1603 uint32_t offset, uint32_t range, uint32_t stride)
1604 {
1605 switch (device->info.gen) {
1606 case 7:
1607 if (device->info.is_haswell)
1608 gen75_fill_buffer_surface_state(state, format, offset, range, stride);
1609 else
1610 gen7_fill_buffer_surface_state(state, format, offset, range, stride);
1611 break;
1612 case 8:
1613 gen8_fill_buffer_surface_state(state, format, offset, range, stride);
1614 break;
1615 case 9:
1616 gen9_fill_buffer_surface_state(state, format, offset, range, stride);
1617 break;
1618 default:
1619 unreachable("unsupported gen\n");
1620 }
1621 }
1622
1623 void anv_DestroySampler(
1624 VkDevice _device,
1625 VkSampler _sampler,
1626 const VkAllocationCallbacks* pAllocator)
1627 {
1628 ANV_FROM_HANDLE(anv_device, device, _device);
1629 ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
1630
1631 anv_free2(&device->alloc, pAllocator, sampler);
1632 }
1633
1634 VkResult anv_CreateFramebuffer(
1635 VkDevice _device,
1636 const VkFramebufferCreateInfo* pCreateInfo,
1637 const VkAllocationCallbacks* pAllocator,
1638 VkFramebuffer* pFramebuffer)
1639 {
1640 ANV_FROM_HANDLE(anv_device, device, _device);
1641 struct anv_framebuffer *framebuffer;
1642
1643 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
1644
1645 size_t size = sizeof(*framebuffer) +
1646 sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
1647 framebuffer = anv_alloc2(&device->alloc, pAllocator, size, 8,
1648 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1649 if (framebuffer == NULL)
1650 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1651
1652 framebuffer->attachment_count = pCreateInfo->attachmentCount;
1653 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
1654 VkImageView _iview = pCreateInfo->pAttachments[i];
1655 framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
1656 }
1657
1658 framebuffer->width = pCreateInfo->width;
1659 framebuffer->height = pCreateInfo->height;
1660 framebuffer->layers = pCreateInfo->layers;
1661
1662 *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
1663
1664 return VK_SUCCESS;
1665 }
1666
1667 void anv_DestroyFramebuffer(
1668 VkDevice _device,
1669 VkFramebuffer _fb,
1670 const VkAllocationCallbacks* pAllocator)
1671 {
1672 ANV_FROM_HANDLE(anv_device, device, _device);
1673 ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
1674
1675 anv_free2(&device->alloc, pAllocator, fb);
1676 }
1677
1678 void vkCmdDbgMarkerBegin(
1679 VkCommandBuffer commandBuffer,
1680 const char* pMarker)
1681 __attribute__ ((visibility ("default")));
1682
1683 void vkCmdDbgMarkerEnd(
1684 VkCommandBuffer commandBuffer)
1685 __attribute__ ((visibility ("default")));
1686
1687 void vkCmdDbgMarkerBegin(
1688 VkCommandBuffer commandBuffer,
1689 const char* pMarker)
1690 {
1691 }
1692
1693 void vkCmdDbgMarkerEnd(
1694 VkCommandBuffer commandBuffer)
1695 {
1696 }