2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33 #include "util/debug.h"
35 #include "genxml/gen7_pack.h"
37 struct anv_dispatch_table dtable
;
40 compiler_debug_log(void *data
, const char *fmt
, ...)
44 compiler_perf_log(void *data
, const char *fmt
, ...)
49 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
50 vfprintf(stderr
, fmt
, args
);
56 anv_physical_device_init(struct anv_physical_device
*device
,
57 struct anv_instance
*instance
,
63 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
65 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
66 "failed to open %s: %m", path
);
68 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
69 device
->instance
= instance
;
72 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
73 if (!device
->chipset_id
) {
74 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
75 "failed to get chipset id: %m");
79 device
->name
= brw_get_device_name(device
->chipset_id
);
80 device
->info
= brw_get_device_info(device
->chipset_id
);
82 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
83 "failed to get device info");
87 if (device
->info
->is_haswell
) {
88 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
89 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
90 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
91 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
92 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
93 } else if (device
->info
->gen
>= 8) {
94 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
95 * supported as anything */
97 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
98 "Vulkan not yet supported on %s", device
->name
);
102 device
->cmd_parser_version
= -1;
103 if (device
->info
->gen
== 7) {
104 device
->cmd_parser_version
=
105 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
106 if (device
->cmd_parser_version
== -1) {
107 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
108 "failed to get command parser version");
113 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
114 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
115 "failed to get aperture size: %m");
119 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
120 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
121 "kernel missing gem wait");
125 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
126 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
127 "kernel missing execbuf2");
131 if (!device
->info
->has_llc
&&
132 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
133 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
134 "kernel missing wc mmap");
138 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
142 brw_process_intel_debug_variable();
144 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
145 if (device
->compiler
== NULL
) {
146 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
149 device
->compiler
->shader_debug_log
= compiler_debug_log
;
150 device
->compiler
->shader_perf_log
= compiler_perf_log
;
152 /* XXX: Actually detect bit6 swizzling */
153 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
163 anv_physical_device_finish(struct anv_physical_device
*device
)
165 ralloc_free(device
->compiler
);
168 static const VkExtensionProperties global_extensions
[] = {
170 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
174 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
177 #ifdef HAVE_WAYLAND_PLATFORM
179 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
185 static const VkExtensionProperties device_extensions
[] = {
187 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
193 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
194 VkSystemAllocationScope allocationScope
)
200 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
201 size_t align
, VkSystemAllocationScope allocationScope
)
203 return realloc(pOriginal
, size
);
207 default_free_func(void *pUserData
, void *pMemory
)
212 static const VkAllocationCallbacks default_alloc
= {
214 .pfnAllocation
= default_alloc_func
,
215 .pfnReallocation
= default_realloc_func
,
216 .pfnFree
= default_free_func
,
219 VkResult
anv_CreateInstance(
220 const VkInstanceCreateInfo
* pCreateInfo
,
221 const VkAllocationCallbacks
* pAllocator
,
222 VkInstance
* pInstance
)
224 struct anv_instance
*instance
;
226 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
228 uint32_t client_version
;
229 if (pCreateInfo
->pApplicationInfo
&&
230 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
231 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
233 client_version
= VK_MAKE_VERSION(1, 0, 0);
236 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
237 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
238 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
239 "Client requested version %d.%d.%d",
240 VK_VERSION_MAJOR(client_version
),
241 VK_VERSION_MINOR(client_version
),
242 VK_VERSION_PATCH(client_version
));
245 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
247 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
248 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
249 global_extensions
[j
].extensionName
) == 0) {
255 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
258 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
259 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
261 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
263 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
266 instance
->alloc
= *pAllocator
;
268 instance
->alloc
= default_alloc
;
270 instance
->apiVersion
= client_version
;
271 instance
->physicalDeviceCount
= -1;
273 memset(instance
->wsi
, 0, sizeof(instance
->wsi
));
277 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
279 anv_init_wsi(instance
);
281 *pInstance
= anv_instance_to_handle(instance
);
286 void anv_DestroyInstance(
287 VkInstance _instance
,
288 const VkAllocationCallbacks
* pAllocator
)
290 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
292 if (instance
->physicalDeviceCount
> 0) {
293 /* We support at most one physical device. */
294 assert(instance
->physicalDeviceCount
== 1);
295 anv_physical_device_finish(&instance
->physicalDevice
);
298 anv_finish_wsi(instance
);
300 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
304 anv_free(&instance
->alloc
, instance
);
307 VkResult
anv_EnumeratePhysicalDevices(
308 VkInstance _instance
,
309 uint32_t* pPhysicalDeviceCount
,
310 VkPhysicalDevice
* pPhysicalDevices
)
312 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
315 if (instance
->physicalDeviceCount
< 0) {
316 result
= anv_physical_device_init(&instance
->physicalDevice
,
317 instance
, "/dev/dri/renderD128");
318 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
319 instance
->physicalDeviceCount
= 0;
320 } else if (result
== VK_SUCCESS
) {
321 instance
->physicalDeviceCount
= 1;
327 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
328 * otherwise it's an inout parameter.
330 * The Vulkan spec (git aaed022) says:
332 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
333 * that is initialized with the number of devices the application is
334 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
335 * an array of at least this many VkPhysicalDevice handles [...].
337 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
338 * overwrites the contents of the variable pointed to by
339 * pPhysicalDeviceCount with the number of physical devices in in the
340 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
341 * pPhysicalDeviceCount with the number of physical handles written to
344 if (!pPhysicalDevices
) {
345 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
346 } else if (*pPhysicalDeviceCount
>= 1) {
347 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
348 *pPhysicalDeviceCount
= 1;
350 *pPhysicalDeviceCount
= 0;
356 void anv_GetPhysicalDeviceFeatures(
357 VkPhysicalDevice physicalDevice
,
358 VkPhysicalDeviceFeatures
* pFeatures
)
360 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
362 *pFeatures
= (VkPhysicalDeviceFeatures
) {
363 .robustBufferAccess
= true,
364 .fullDrawIndexUint32
= true,
365 .imageCubeArray
= false,
366 .independentBlend
= pdevice
->info
->gen
>= 8,
367 .geometryShader
= true,
368 .tessellationShader
= false,
369 .sampleRateShading
= false,
370 .dualSrcBlend
= true,
372 .multiDrawIndirect
= false,
373 .drawIndirectFirstInstance
= false,
375 .depthBiasClamp
= false,
376 .fillModeNonSolid
= true,
377 .depthBounds
= false,
381 .multiViewport
= true,
382 .samplerAnisotropy
= false, /* FINISHME */
383 .textureCompressionETC2
= true,
384 .textureCompressionASTC_LDR
= true,
385 .textureCompressionBC
= true,
386 .occlusionQueryPrecise
= true,
387 .pipelineStatisticsQuery
= false,
388 .vertexPipelineStoresAndAtomics
= pdevice
->info
->gen
>= 8,
389 .fragmentStoresAndAtomics
= true,
390 .shaderTessellationAndGeometryPointSize
= true,
391 .shaderImageGatherExtended
= true,
392 .shaderStorageImageExtendedFormats
= false,
393 .shaderStorageImageMultisample
= false,
394 .shaderUniformBufferArrayDynamicIndexing
= true,
395 .shaderSampledImageArrayDynamicIndexing
= true,
396 .shaderStorageBufferArrayDynamicIndexing
= true,
397 .shaderStorageImageArrayDynamicIndexing
= true,
398 .shaderStorageImageReadWithoutFormat
= false,
399 .shaderStorageImageWriteWithoutFormat
= true,
400 .shaderClipDistance
= false,
401 .shaderCullDistance
= false,
402 .shaderFloat64
= false,
403 .shaderInt64
= false,
404 .shaderInt16
= false,
406 .variableMultisampleRate
= false,
407 .inheritedQueries
= false,
412 anv_device_get_cache_uuid(void *uuid
)
414 memset(uuid
, 0, VK_UUID_SIZE
);
415 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", MESA_GIT_SHA1
+ 4);
418 void anv_GetPhysicalDeviceProperties(
419 VkPhysicalDevice physicalDevice
,
420 VkPhysicalDeviceProperties
* pProperties
)
422 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
423 const struct brw_device_info
*devinfo
= pdevice
->info
;
425 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
427 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
429 VkSampleCountFlags sample_counts
=
430 isl_device_get_sample_counts(&pdevice
->isl_dev
);
432 VkPhysicalDeviceLimits limits
= {
433 .maxImageDimension1D
= (1 << 14),
434 .maxImageDimension2D
= (1 << 14),
435 .maxImageDimension3D
= (1 << 11),
436 .maxImageDimensionCube
= (1 << 14),
437 .maxImageArrayLayers
= (1 << 11),
438 .maxTexelBufferElements
= 128 * 1024 * 1024,
439 .maxUniformBufferRange
= UINT32_MAX
,
440 .maxStorageBufferRange
= UINT32_MAX
,
441 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
442 .maxMemoryAllocationCount
= UINT32_MAX
,
443 .maxSamplerAllocationCount
= 64 * 1024,
444 .bufferImageGranularity
= 64, /* A cache line */
445 .sparseAddressSpaceSize
= 0,
446 .maxBoundDescriptorSets
= MAX_SETS
,
447 .maxPerStageDescriptorSamplers
= 64,
448 .maxPerStageDescriptorUniformBuffers
= 64,
449 .maxPerStageDescriptorStorageBuffers
= 64,
450 .maxPerStageDescriptorSampledImages
= 64,
451 .maxPerStageDescriptorStorageImages
= 64,
452 .maxPerStageDescriptorInputAttachments
= 64,
453 .maxPerStageResources
= 128,
454 .maxDescriptorSetSamplers
= 256,
455 .maxDescriptorSetUniformBuffers
= 256,
456 .maxDescriptorSetUniformBuffersDynamic
= 256,
457 .maxDescriptorSetStorageBuffers
= 256,
458 .maxDescriptorSetStorageBuffersDynamic
= 256,
459 .maxDescriptorSetSampledImages
= 256,
460 .maxDescriptorSetStorageImages
= 256,
461 .maxDescriptorSetInputAttachments
= 256,
462 .maxVertexInputAttributes
= 32,
463 .maxVertexInputBindings
= 32,
464 .maxVertexInputAttributeOffset
= 2047,
465 .maxVertexInputBindingStride
= 2048,
466 .maxVertexOutputComponents
= 128,
467 .maxTessellationGenerationLevel
= 0,
468 .maxTessellationPatchSize
= 0,
469 .maxTessellationControlPerVertexInputComponents
= 0,
470 .maxTessellationControlPerVertexOutputComponents
= 0,
471 .maxTessellationControlPerPatchOutputComponents
= 0,
472 .maxTessellationControlTotalOutputComponents
= 0,
473 .maxTessellationEvaluationInputComponents
= 0,
474 .maxTessellationEvaluationOutputComponents
= 0,
475 .maxGeometryShaderInvocations
= 32,
476 .maxGeometryInputComponents
= 64,
477 .maxGeometryOutputComponents
= 128,
478 .maxGeometryOutputVertices
= 256,
479 .maxGeometryTotalOutputComponents
= 1024,
480 .maxFragmentInputComponents
= 128,
481 .maxFragmentOutputAttachments
= 8,
482 .maxFragmentDualSrcAttachments
= 2,
483 .maxFragmentCombinedOutputResources
= 8,
484 .maxComputeSharedMemorySize
= 32768,
485 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
486 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
487 .maxComputeWorkGroupSize
= {
488 16 * devinfo
->max_cs_threads
,
489 16 * devinfo
->max_cs_threads
,
490 16 * devinfo
->max_cs_threads
,
492 .subPixelPrecisionBits
= 4 /* FIXME */,
493 .subTexelPrecisionBits
= 4 /* FIXME */,
494 .mipmapPrecisionBits
= 4 /* FIXME */,
495 .maxDrawIndexedIndexValue
= UINT32_MAX
,
496 .maxDrawIndirectCount
= UINT32_MAX
,
497 .maxSamplerLodBias
= 16,
498 .maxSamplerAnisotropy
= 16,
499 .maxViewports
= MAX_VIEWPORTS
,
500 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
501 .viewportBoundsRange
= { -16384.0, 16384.0 },
502 .viewportSubPixelBits
= 13, /* We take a float? */
503 .minMemoryMapAlignment
= 4096, /* A page */
504 .minTexelBufferOffsetAlignment
= 1,
505 .minUniformBufferOffsetAlignment
= 1,
506 .minStorageBufferOffsetAlignment
= 1,
507 .minTexelOffset
= -8,
509 .minTexelGatherOffset
= -8,
510 .maxTexelGatherOffset
= 7,
511 .minInterpolationOffset
= 0, /* FIXME */
512 .maxInterpolationOffset
= 0, /* FIXME */
513 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
514 .maxFramebufferWidth
= (1 << 14),
515 .maxFramebufferHeight
= (1 << 14),
516 .maxFramebufferLayers
= (1 << 10),
517 .framebufferColorSampleCounts
= sample_counts
,
518 .framebufferDepthSampleCounts
= sample_counts
,
519 .framebufferStencilSampleCounts
= sample_counts
,
520 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
521 .maxColorAttachments
= MAX_RTS
,
522 .sampledImageColorSampleCounts
= sample_counts
,
523 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
524 .sampledImageDepthSampleCounts
= sample_counts
,
525 .sampledImageStencilSampleCounts
= sample_counts
,
526 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
527 .maxSampleMaskWords
= 1,
528 .timestampComputeAndGraphics
= false,
529 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
530 .maxClipDistances
= 0 /* FIXME */,
531 .maxCullDistances
= 0 /* FIXME */,
532 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
533 .discreteQueuePriorities
= 1,
534 .pointSizeRange
= { 0.125, 255.875 },
535 .lineWidthRange
= { 0.0, 7.9921875 },
536 .pointSizeGranularity
= (1.0 / 8.0),
537 .lineWidthGranularity
= (1.0 / 128.0),
538 .strictLines
= false, /* FINISHME */
539 .standardSampleLocations
= true,
540 .optimalBufferCopyOffsetAlignment
= 128,
541 .optimalBufferCopyRowPitchAlignment
= 128,
542 .nonCoherentAtomSize
= 64,
545 *pProperties
= (VkPhysicalDeviceProperties
) {
546 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
549 .deviceID
= pdevice
->chipset_id
,
550 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
552 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
555 strcpy(pProperties
->deviceName
, pdevice
->name
);
556 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
559 void anv_GetPhysicalDeviceQueueFamilyProperties(
560 VkPhysicalDevice physicalDevice
,
562 VkQueueFamilyProperties
* pQueueFamilyProperties
)
564 if (pQueueFamilyProperties
== NULL
) {
569 assert(*pCount
>= 1);
571 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
572 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
573 VK_QUEUE_COMPUTE_BIT
|
574 VK_QUEUE_TRANSFER_BIT
,
576 .timestampValidBits
= 36, /* XXX: Real value here */
577 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
581 void anv_GetPhysicalDeviceMemoryProperties(
582 VkPhysicalDevice physicalDevice
,
583 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
585 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
586 VkDeviceSize heap_size
;
588 /* Reserve some wiggle room for the driver by exposing only 75% of the
589 * aperture to the heap.
591 heap_size
= 3 * physical_device
->aperture_size
/ 4;
593 if (physical_device
->info
->has_llc
) {
594 /* Big core GPUs share LLC with the CPU and thus one memory type can be
595 * both cached and coherent at the same time.
597 pMemoryProperties
->memoryTypeCount
= 1;
598 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
599 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
600 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
601 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
602 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
606 /* The spec requires that we expose a host-visible, coherent memory
607 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
608 * to give the application a choice between cached, but not coherent and
609 * coherent but uncached (WC though).
611 pMemoryProperties
->memoryTypeCount
= 2;
612 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
613 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
614 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
615 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
618 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
619 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
620 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
621 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
626 pMemoryProperties
->memoryHeapCount
= 1;
627 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
629 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
633 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
637 return anv_lookup_entrypoint(pName
);
640 /* The loader wants us to expose a second GetInstanceProcAddr function
641 * to work around certain LD_PRELOAD issues seen in apps.
643 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
647 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
651 return anv_GetInstanceProcAddr(instance
, pName
);
654 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
658 return anv_lookup_entrypoint(pName
);
662 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
664 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
665 queue
->device
= device
;
666 queue
->pool
= &device
->surface_state_pool
;
672 anv_queue_finish(struct anv_queue
*queue
)
676 static struct anv_state
677 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
679 struct anv_state state
;
681 state
= anv_state_pool_alloc(pool
, size
, align
);
682 memcpy(state
.map
, p
, size
);
684 if (!pool
->block_pool
->device
->info
.has_llc
)
685 anv_state_clflush(state
);
690 struct gen8_border_color
{
695 /* Pad out to 64 bytes */
700 anv_device_init_border_colors(struct anv_device
*device
)
702 static const struct gen8_border_color border_colors
[] = {
703 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
704 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
705 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
706 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
707 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
708 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
711 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
712 sizeof(border_colors
), 64,
717 anv_device_submit_simple_batch(struct anv_device
*device
,
718 struct anv_batch
*batch
)
720 struct drm_i915_gem_execbuffer2 execbuf
;
721 struct drm_i915_gem_exec_object2 exec2_objects
[1];
723 VkResult result
= VK_SUCCESS
;
728 /* Kernel driver requires 8 byte aligned batch length */
729 size
= align_u32(batch
->next
- batch
->start
, 8);
730 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
731 if (result
!= VK_SUCCESS
)
734 memcpy(bo
.map
, batch
->start
, size
);
735 if (!device
->info
.has_llc
)
736 anv_clflush_range(bo
.map
, size
);
738 exec2_objects
[0].handle
= bo
.gem_handle
;
739 exec2_objects
[0].relocation_count
= 0;
740 exec2_objects
[0].relocs_ptr
= 0;
741 exec2_objects
[0].alignment
= 0;
742 exec2_objects
[0].offset
= bo
.offset
;
743 exec2_objects
[0].flags
= 0;
744 exec2_objects
[0].rsvd1
= 0;
745 exec2_objects
[0].rsvd2
= 0;
747 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
748 execbuf
.buffer_count
= 1;
749 execbuf
.batch_start_offset
= 0;
750 execbuf
.batch_len
= size
;
751 execbuf
.cliprects_ptr
= 0;
752 execbuf
.num_cliprects
= 0;
757 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
758 execbuf
.rsvd1
= device
->context_id
;
761 ret
= anv_gem_execbuffer(device
, &execbuf
);
763 /* We don't know the real error. */
764 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
769 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
771 /* We don't know the real error. */
772 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
777 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
782 VkResult
anv_CreateDevice(
783 VkPhysicalDevice physicalDevice
,
784 const VkDeviceCreateInfo
* pCreateInfo
,
785 const VkAllocationCallbacks
* pAllocator
,
788 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
790 struct anv_device
*device
;
792 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
794 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
796 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
797 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
798 device_extensions
[j
].extensionName
) == 0) {
804 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
807 anv_set_dispatch_devinfo(physical_device
->info
);
809 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
811 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
813 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
815 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
816 device
->instance
= physical_device
->instance
;
817 device
->chipset_id
= physical_device
->chipset_id
;
820 device
->alloc
= *pAllocator
;
822 device
->alloc
= physical_device
->instance
->alloc
;
824 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
825 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
826 if (device
->fd
== -1) {
827 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
831 device
->context_id
= anv_gem_create_context(device
);
832 if (device
->context_id
== -1) {
833 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
837 device
->info
= *physical_device
->info
;
838 device
->isl_dev
= physical_device
->isl_dev
;
840 /* On Broadwell and later, we can use batch chaining to more efficiently
841 * implement growing command buffers. Prior to Haswell, the kernel
842 * command parser gets in the way and we have to fall back to growing
845 device
->can_chain_batches
= device
->info
.gen
>= 8;
847 pthread_mutex_init(&device
->mutex
, NULL
);
849 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
851 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
853 anv_state_pool_init(&device
->dynamic_state_pool
,
854 &device
->dynamic_state_block_pool
);
856 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
857 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
859 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
861 anv_state_pool_init(&device
->surface_state_pool
,
862 &device
->surface_state_block_pool
);
864 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
866 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
868 anv_queue_init(device
, &device
->queue
);
870 switch (device
->info
.gen
) {
872 if (!device
->info
.is_haswell
)
873 result
= gen7_init_device_state(device
);
875 result
= gen75_init_device_state(device
);
878 result
= gen8_init_device_state(device
);
881 result
= gen9_init_device_state(device
);
884 /* Shouldn't get here as we don't create physical devices for any other
886 unreachable("unhandled gen");
888 if (result
!= VK_SUCCESS
)
891 result
= anv_device_init_meta(device
);
892 if (result
!= VK_SUCCESS
)
895 anv_device_init_border_colors(device
);
897 *pDevice
= anv_device_to_handle(device
);
904 anv_free(&device
->alloc
, device
);
909 void anv_DestroyDevice(
911 const VkAllocationCallbacks
* pAllocator
)
913 ANV_FROM_HANDLE(anv_device
, device
, _device
);
915 anv_queue_finish(&device
->queue
);
917 anv_device_finish_meta(device
);
920 /* We only need to free these to prevent valgrind errors. The backing
921 * BO will go away in a couple of lines so we don't actually leak.
923 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
926 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
927 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
929 anv_bo_pool_finish(&device
->batch_bo_pool
);
930 anv_state_pool_finish(&device
->dynamic_state_pool
);
931 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
932 anv_block_pool_finish(&device
->instruction_block_pool
);
933 anv_state_pool_finish(&device
->surface_state_pool
);
934 anv_block_pool_finish(&device
->surface_state_block_pool
);
935 anv_block_pool_finish(&device
->scratch_block_pool
);
939 pthread_mutex_destroy(&device
->mutex
);
941 anv_free(&device
->alloc
, device
);
944 VkResult
anv_EnumerateInstanceExtensionProperties(
945 const char* pLayerName
,
946 uint32_t* pPropertyCount
,
947 VkExtensionProperties
* pProperties
)
949 if (pProperties
== NULL
) {
950 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
954 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
956 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
957 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
962 VkResult
anv_EnumerateDeviceExtensionProperties(
963 VkPhysicalDevice physicalDevice
,
964 const char* pLayerName
,
965 uint32_t* pPropertyCount
,
966 VkExtensionProperties
* pProperties
)
968 if (pProperties
== NULL
) {
969 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
973 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
975 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
976 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
981 VkResult
anv_EnumerateInstanceLayerProperties(
982 uint32_t* pPropertyCount
,
983 VkLayerProperties
* pProperties
)
985 if (pProperties
== NULL
) {
990 /* None supported at this time */
991 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
994 VkResult
anv_EnumerateDeviceLayerProperties(
995 VkPhysicalDevice physicalDevice
,
996 uint32_t* pPropertyCount
,
997 VkLayerProperties
* pProperties
)
999 if (pProperties
== NULL
) {
1000 *pPropertyCount
= 0;
1004 /* None supported at this time */
1005 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1008 void anv_GetDeviceQueue(
1010 uint32_t queueNodeIndex
,
1011 uint32_t queueIndex
,
1014 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1016 assert(queueIndex
== 0);
1018 *pQueue
= anv_queue_to_handle(&device
->queue
);
1021 VkResult
anv_QueueSubmit(
1023 uint32_t submitCount
,
1024 const VkSubmitInfo
* pSubmits
,
1027 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1028 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1029 struct anv_device
*device
= queue
->device
;
1032 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1033 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1034 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1035 pSubmits
[i
].pCommandBuffers
[j
]);
1036 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1038 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1040 /* We don't know the real error. */
1041 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1042 "execbuf2 failed: %m");
1045 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1046 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1051 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1053 /* We don't know the real error. */
1054 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1055 "execbuf2 failed: %m");
1062 VkResult
anv_QueueWaitIdle(
1065 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1067 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1070 VkResult
anv_DeviceWaitIdle(
1073 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1074 struct anv_batch batch
;
1077 batch
.start
= batch
.next
= cmds
;
1078 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1080 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1081 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1083 return anv_device_submit_simple_batch(device
, &batch
);
1087 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1089 bo
->gem_handle
= anv_gem_create(device
, size
);
1090 if (!bo
->gem_handle
)
1091 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1097 bo
->is_winsys_bo
= false;
1102 VkResult
anv_AllocateMemory(
1104 const VkMemoryAllocateInfo
* pAllocateInfo
,
1105 const VkAllocationCallbacks
* pAllocator
,
1106 VkDeviceMemory
* pMem
)
1108 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1109 struct anv_device_memory
*mem
;
1112 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1114 if (pAllocateInfo
->allocationSize
== 0) {
1115 /* Apparently, this is allowed */
1116 *pMem
= VK_NULL_HANDLE
;
1120 /* We support exactly one memory heap. */
1121 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1122 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1124 /* FINISHME: Fail if allocation request exceeds heap size. */
1126 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1127 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1129 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1131 /* The kernel is going to give us whole pages anyway */
1132 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1134 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1135 if (result
!= VK_SUCCESS
)
1138 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1140 *pMem
= anv_device_memory_to_handle(mem
);
1145 anv_free2(&device
->alloc
, pAllocator
, mem
);
1150 void anv_FreeMemory(
1152 VkDeviceMemory _mem
,
1153 const VkAllocationCallbacks
* pAllocator
)
1155 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1156 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1162 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1164 if (mem
->bo
.gem_handle
!= 0)
1165 anv_gem_close(device
, mem
->bo
.gem_handle
);
1167 anv_free2(&device
->alloc
, pAllocator
, mem
);
1170 VkResult
anv_MapMemory(
1172 VkDeviceMemory _memory
,
1173 VkDeviceSize offset
,
1175 VkMemoryMapFlags flags
,
1178 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1179 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1186 if (size
== VK_WHOLE_SIZE
)
1187 size
= mem
->bo
.size
- offset
;
1189 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1190 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1191 * at a time is valid. We could just mmap up front and return an offset
1192 * pointer here, but that may exhaust virtual memory on 32 bit
1195 uint32_t gem_flags
= 0;
1196 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1197 gem_flags
|= I915_MMAP_WC
;
1199 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1200 uint64_t map_offset
= offset
& ~4095ull;
1201 assert(offset
>= map_offset
);
1202 uint64_t map_size
= (offset
+ size
) - map_offset
;
1204 /* Let's map whole pages */
1205 map_size
= align_u64(map_size
, 4096);
1207 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1208 map_offset
, map_size
, gem_flags
);
1209 mem
->map_size
= map_size
;
1211 *ppData
= mem
->map
+ (offset
- map_offset
);
1216 void anv_UnmapMemory(
1218 VkDeviceMemory _memory
)
1220 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1225 anv_gem_munmap(mem
->map
, mem
->map_size
);
1229 clflush_mapped_ranges(struct anv_device
*device
,
1231 const VkMappedMemoryRange
*ranges
)
1233 for (uint32_t i
= 0; i
< count
; i
++) {
1234 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1235 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1238 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1239 end
= mem
->map
+ mem
->map_size
;
1241 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1244 __builtin_ia32_clflush(p
);
1245 p
+= CACHELINE_SIZE
;
1250 VkResult
anv_FlushMappedMemoryRanges(
1252 uint32_t memoryRangeCount
,
1253 const VkMappedMemoryRange
* pMemoryRanges
)
1255 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1257 if (device
->info
.has_llc
)
1260 /* Make sure the writes we're flushing have landed. */
1261 __builtin_ia32_mfence();
1263 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1268 VkResult
anv_InvalidateMappedMemoryRanges(
1270 uint32_t memoryRangeCount
,
1271 const VkMappedMemoryRange
* pMemoryRanges
)
1273 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1275 if (device
->info
.has_llc
)
1278 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1280 /* Make sure no reads get moved up above the invalidate. */
1281 __builtin_ia32_mfence();
1286 void anv_GetBufferMemoryRequirements(
1289 VkMemoryRequirements
* pMemoryRequirements
)
1291 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1293 /* The Vulkan spec (git aaed022) says:
1295 * memoryTypeBits is a bitfield and contains one bit set for every
1296 * supported memory type for the resource. The bit `1<<i` is set if and
1297 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1298 * structure for the physical device is supported.
1300 * We support exactly one memory type.
1302 pMemoryRequirements
->memoryTypeBits
= 1;
1304 pMemoryRequirements
->size
= buffer
->size
;
1305 pMemoryRequirements
->alignment
= 16;
1308 void anv_GetImageMemoryRequirements(
1311 VkMemoryRequirements
* pMemoryRequirements
)
1313 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1315 /* The Vulkan spec (git aaed022) says:
1317 * memoryTypeBits is a bitfield and contains one bit set for every
1318 * supported memory type for the resource. The bit `1<<i` is set if and
1319 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1320 * structure for the physical device is supported.
1322 * We support exactly one memory type.
1324 pMemoryRequirements
->memoryTypeBits
= 1;
1326 pMemoryRequirements
->size
= image
->size
;
1327 pMemoryRequirements
->alignment
= image
->alignment
;
1330 void anv_GetImageSparseMemoryRequirements(
1333 uint32_t* pSparseMemoryRequirementCount
,
1334 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1339 void anv_GetDeviceMemoryCommitment(
1341 VkDeviceMemory memory
,
1342 VkDeviceSize
* pCommittedMemoryInBytes
)
1344 *pCommittedMemoryInBytes
= 0;
1347 VkResult
anv_BindBufferMemory(
1350 VkDeviceMemory _memory
,
1351 VkDeviceSize memoryOffset
)
1353 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1354 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1357 buffer
->bo
= &mem
->bo
;
1358 buffer
->offset
= memoryOffset
;
1367 VkResult
anv_BindImageMemory(
1370 VkDeviceMemory _memory
,
1371 VkDeviceSize memoryOffset
)
1373 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1374 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1377 image
->bo
= &mem
->bo
;
1378 image
->offset
= memoryOffset
;
1387 VkResult
anv_QueueBindSparse(
1389 uint32_t bindInfoCount
,
1390 const VkBindSparseInfo
* pBindInfo
,
1393 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1396 VkResult
anv_CreateFence(
1398 const VkFenceCreateInfo
* pCreateInfo
,
1399 const VkAllocationCallbacks
* pAllocator
,
1402 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1403 struct anv_bo fence_bo
;
1404 struct anv_fence
*fence
;
1405 struct anv_batch batch
;
1408 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1410 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1411 if (result
!= VK_SUCCESS
)
1414 /* Fences are small. Just store the CPU data structure in the BO. */
1415 fence
= fence_bo
.map
;
1416 fence
->bo
= fence_bo
;
1418 /* Place the batch after the CPU data but on its own cache line. */
1419 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1420 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1421 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1422 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
);
1423 anv_batch_emit(&batch
, GEN7_MI_NOOP
);
1425 if (!device
->info
.has_llc
) {
1426 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1427 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1428 __builtin_ia32_mfence();
1429 __builtin_ia32_clflush(batch
.start
);
1432 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1433 fence
->exec2_objects
[0].relocation_count
= 0;
1434 fence
->exec2_objects
[0].relocs_ptr
= 0;
1435 fence
->exec2_objects
[0].alignment
= 0;
1436 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1437 fence
->exec2_objects
[0].flags
= 0;
1438 fence
->exec2_objects
[0].rsvd1
= 0;
1439 fence
->exec2_objects
[0].rsvd2
= 0;
1441 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1442 fence
->execbuf
.buffer_count
= 1;
1443 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1444 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1445 fence
->execbuf
.cliprects_ptr
= 0;
1446 fence
->execbuf
.num_cliprects
= 0;
1447 fence
->execbuf
.DR1
= 0;
1448 fence
->execbuf
.DR4
= 0;
1450 fence
->execbuf
.flags
=
1451 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1452 fence
->execbuf
.rsvd1
= device
->context_id
;
1453 fence
->execbuf
.rsvd2
= 0;
1455 fence
->ready
= false;
1457 *pFence
= anv_fence_to_handle(fence
);
1462 void anv_DestroyFence(
1465 const VkAllocationCallbacks
* pAllocator
)
1467 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1468 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1470 assert(fence
->bo
.map
== fence
);
1471 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1474 VkResult
anv_ResetFences(
1476 uint32_t fenceCount
,
1477 const VkFence
* pFences
)
1479 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1480 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1481 fence
->ready
= false;
1487 VkResult
anv_GetFenceStatus(
1491 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1492 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1499 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1501 fence
->ready
= true;
1505 return VK_NOT_READY
;
1508 VkResult
anv_WaitForFences(
1510 uint32_t fenceCount
,
1511 const VkFence
* pFences
,
1515 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1517 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1518 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1519 * for a couple of kernel releases. Since there's no way to know
1520 * whether or not the kernel we're using is one of the broken ones, the
1521 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1522 * maximum timeout from 584 years to 292 years - likely not a big deal.
1524 if (timeout
> INT64_MAX
)
1525 timeout
= INT64_MAX
;
1527 int64_t t
= timeout
;
1529 /* FIXME: handle !waitAll */
1531 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1532 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1533 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1534 if (ret
== -1 && errno
== ETIME
) {
1536 } else if (ret
== -1) {
1537 /* We don't know the real error. */
1538 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1539 "gem wait failed: %m");
1546 // Queue semaphore functions
1548 VkResult
anv_CreateSemaphore(
1550 const VkSemaphoreCreateInfo
* pCreateInfo
,
1551 const VkAllocationCallbacks
* pAllocator
,
1552 VkSemaphore
* pSemaphore
)
1554 /* The DRM execbuffer ioctl always execute in-oder, even between different
1555 * rings. As such, there's nothing to do for the user space semaphore.
1558 *pSemaphore
= (VkSemaphore
)1;
1563 void anv_DestroySemaphore(
1565 VkSemaphore semaphore
,
1566 const VkAllocationCallbacks
* pAllocator
)
1572 VkResult
anv_CreateEvent(
1574 const VkEventCreateInfo
* pCreateInfo
,
1575 const VkAllocationCallbacks
* pAllocator
,
1578 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1579 struct anv_state state
;
1580 struct anv_event
*event
;
1582 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1584 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1587 event
->state
= state
;
1588 event
->semaphore
= VK_EVENT_RESET
;
1590 if (!device
->info
.has_llc
) {
1591 /* Make sure the writes we're flushing have landed. */
1592 __builtin_ia32_mfence();
1593 __builtin_ia32_clflush(event
);
1596 *pEvent
= anv_event_to_handle(event
);
1601 void anv_DestroyEvent(
1604 const VkAllocationCallbacks
* pAllocator
)
1606 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1607 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1609 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1612 VkResult
anv_GetEventStatus(
1616 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1617 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1619 if (!device
->info
.has_llc
) {
1620 /* Invalidate read cache before reading event written by GPU. */
1621 __builtin_ia32_clflush(event
);
1622 __builtin_ia32_mfence();
1626 return event
->semaphore
;
1629 VkResult
anv_SetEvent(
1633 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1634 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1636 event
->semaphore
= VK_EVENT_SET
;
1638 if (!device
->info
.has_llc
) {
1639 /* Make sure the writes we're flushing have landed. */
1640 __builtin_ia32_mfence();
1641 __builtin_ia32_clflush(event
);
1647 VkResult
anv_ResetEvent(
1651 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1652 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1654 event
->semaphore
= VK_EVENT_RESET
;
1656 if (!device
->info
.has_llc
) {
1657 /* Make sure the writes we're flushing have landed. */
1658 __builtin_ia32_mfence();
1659 __builtin_ia32_clflush(event
);
1667 VkResult
anv_CreateBuffer(
1669 const VkBufferCreateInfo
* pCreateInfo
,
1670 const VkAllocationCallbacks
* pAllocator
,
1673 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1674 struct anv_buffer
*buffer
;
1676 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1678 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1679 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1681 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1683 buffer
->size
= pCreateInfo
->size
;
1684 buffer
->usage
= pCreateInfo
->usage
;
1688 *pBuffer
= anv_buffer_to_handle(buffer
);
1693 void anv_DestroyBuffer(
1696 const VkAllocationCallbacks
* pAllocator
)
1698 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1699 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1701 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1705 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1706 enum isl_format format
,
1707 uint32_t offset
, uint32_t range
, uint32_t stride
)
1709 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1711 .mocs
= device
->default_mocs
,
1716 if (!device
->info
.has_llc
)
1717 anv_state_clflush(state
);
1720 void anv_DestroySampler(
1723 const VkAllocationCallbacks
* pAllocator
)
1725 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1726 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1728 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1731 VkResult
anv_CreateFramebuffer(
1733 const VkFramebufferCreateInfo
* pCreateInfo
,
1734 const VkAllocationCallbacks
* pAllocator
,
1735 VkFramebuffer
* pFramebuffer
)
1737 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1738 struct anv_framebuffer
*framebuffer
;
1740 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1742 size_t size
= sizeof(*framebuffer
) +
1743 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1744 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1745 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1746 if (framebuffer
== NULL
)
1747 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1749 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1750 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1751 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1752 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1755 framebuffer
->width
= pCreateInfo
->width
;
1756 framebuffer
->height
= pCreateInfo
->height
;
1757 framebuffer
->layers
= pCreateInfo
->layers
;
1759 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1764 void anv_DestroyFramebuffer(
1767 const VkAllocationCallbacks
* pAllocator
)
1769 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1770 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1772 anv_free2(&device
->alloc
, pAllocator
, fb
);
1775 void vkCmdDbgMarkerBegin(
1776 VkCommandBuffer commandBuffer
,
1777 const char* pMarker
)
1778 __attribute__ ((visibility ("default")));
1780 void vkCmdDbgMarkerEnd(
1781 VkCommandBuffer commandBuffer
)
1782 __attribute__ ((visibility ("default")));
1784 void vkCmdDbgMarkerBegin(
1785 VkCommandBuffer commandBuffer
,
1786 const char* pMarker
)
1790 void vkCmdDbgMarkerEnd(
1791 VkCommandBuffer commandBuffer
)