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 "anv_timestamp.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
;
71 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
72 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
74 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
75 if (!device
->chipset_id
) {
76 result
= VK_ERROR_INITIALIZATION_FAILED
;
80 device
->name
= brw_get_device_name(device
->chipset_id
);
81 device
->info
= brw_get_device_info(device
->chipset_id
);
83 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
84 "failed to get device info");
88 if (device
->info
->is_haswell
) {
89 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
90 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
91 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
92 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
93 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
94 } else if (device
->info
->gen
>= 8) {
95 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
96 * supported as anything */
98 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
99 "Vulkan not yet supported on %s", device
->name
);
103 device
->cmd_parser_version
= -1;
104 if (device
->info
->gen
== 7) {
105 device
->cmd_parser_version
=
106 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
107 if (device
->cmd_parser_version
== -1) {
108 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
109 "failed to get command parser version");
114 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
115 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
116 "failed to get aperture size: %m");
120 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
121 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
122 "kernel missing gem wait");
126 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
127 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
128 "kernel missing execbuf2");
132 if (!device
->info
->has_llc
&&
133 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
134 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
135 "kernel missing wc mmap");
139 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
143 brw_process_intel_debug_variable();
145 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
146 if (device
->compiler
== NULL
) {
147 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
150 device
->compiler
->shader_debug_log
= compiler_debug_log
;
151 device
->compiler
->shader_perf_log
= compiler_perf_log
;
153 result
= anv_init_wsi(device
);
154 if (result
!= VK_SUCCESS
)
157 /* XXX: Actually detect bit6 swizzling */
158 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
168 anv_physical_device_finish(struct anv_physical_device
*device
)
170 anv_finish_wsi(device
);
171 ralloc_free(device
->compiler
);
174 static const VkExtensionProperties global_extensions
[] = {
176 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
179 #ifdef VK_USE_PLATFORM_XCB_KHR
181 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
185 #ifdef VK_USE_PLATFORM_XLIB_KHR
187 .extensionName
= VK_KHR_XLIB_SURFACE_EXTENSION_NAME
,
191 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
193 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
199 static const VkExtensionProperties device_extensions
[] = {
201 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
207 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
208 VkSystemAllocationScope allocationScope
)
214 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
215 size_t align
, VkSystemAllocationScope allocationScope
)
217 return realloc(pOriginal
, size
);
221 default_free_func(void *pUserData
, void *pMemory
)
226 static const VkAllocationCallbacks default_alloc
= {
228 .pfnAllocation
= default_alloc_func
,
229 .pfnReallocation
= default_realloc_func
,
230 .pfnFree
= default_free_func
,
233 VkResult
anv_CreateInstance(
234 const VkInstanceCreateInfo
* pCreateInfo
,
235 const VkAllocationCallbacks
* pAllocator
,
236 VkInstance
* pInstance
)
238 struct anv_instance
*instance
;
240 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
242 uint32_t client_version
;
243 if (pCreateInfo
->pApplicationInfo
&&
244 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
245 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
247 client_version
= VK_MAKE_VERSION(1, 0, 0);
250 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
251 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
252 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
253 "Client requested version %d.%d.%d",
254 VK_VERSION_MAJOR(client_version
),
255 VK_VERSION_MINOR(client_version
),
256 VK_VERSION_PATCH(client_version
));
259 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
261 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
262 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
263 global_extensions
[j
].extensionName
) == 0) {
269 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
272 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
273 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
275 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
277 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
280 instance
->alloc
= *pAllocator
;
282 instance
->alloc
= default_alloc
;
284 instance
->apiVersion
= client_version
;
285 instance
->physicalDeviceCount
= -1;
289 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
291 *pInstance
= anv_instance_to_handle(instance
);
296 void anv_DestroyInstance(
297 VkInstance _instance
,
298 const VkAllocationCallbacks
* pAllocator
)
300 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
302 if (instance
->physicalDeviceCount
> 0) {
303 /* We support at most one physical device. */
304 assert(instance
->physicalDeviceCount
== 1);
305 anv_physical_device_finish(&instance
->physicalDevice
);
308 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
312 anv_free(&instance
->alloc
, instance
);
315 VkResult
anv_EnumeratePhysicalDevices(
316 VkInstance _instance
,
317 uint32_t* pPhysicalDeviceCount
,
318 VkPhysicalDevice
* pPhysicalDevices
)
320 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
323 if (instance
->physicalDeviceCount
< 0) {
325 for (unsigned i
= 0; i
< 8; i
++) {
326 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
327 result
= anv_physical_device_init(&instance
->physicalDevice
,
329 if (result
== VK_SUCCESS
)
333 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
334 instance
->physicalDeviceCount
= 0;
335 } else if (result
== VK_SUCCESS
) {
336 instance
->physicalDeviceCount
= 1;
342 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
343 * otherwise it's an inout parameter.
345 * The Vulkan spec (git aaed022) says:
347 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
348 * that is initialized with the number of devices the application is
349 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
350 * an array of at least this many VkPhysicalDevice handles [...].
352 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
353 * overwrites the contents of the variable pointed to by
354 * pPhysicalDeviceCount with the number of physical devices in in the
355 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
356 * pPhysicalDeviceCount with the number of physical handles written to
359 if (!pPhysicalDevices
) {
360 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
361 } else if (*pPhysicalDeviceCount
>= 1) {
362 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
363 *pPhysicalDeviceCount
= 1;
365 *pPhysicalDeviceCount
= 0;
371 void anv_GetPhysicalDeviceFeatures(
372 VkPhysicalDevice physicalDevice
,
373 VkPhysicalDeviceFeatures
* pFeatures
)
375 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
377 *pFeatures
= (VkPhysicalDeviceFeatures
) {
378 .robustBufferAccess
= true,
379 .fullDrawIndexUint32
= true,
380 .imageCubeArray
= false,
381 .independentBlend
= true,
382 .geometryShader
= true,
383 .tessellationShader
= false,
384 .sampleRateShading
= true,
385 .dualSrcBlend
= true,
387 .multiDrawIndirect
= false,
388 .drawIndirectFirstInstance
= false,
390 .depthBiasClamp
= false,
391 .fillModeNonSolid
= true,
392 .depthBounds
= false,
396 .multiViewport
= true,
397 .samplerAnisotropy
= false, /* FINISHME */
398 .textureCompressionETC2
= pdevice
->info
->gen
>= 8 ||
399 pdevice
->info
->is_baytrail
,
400 .textureCompressionASTC_LDR
= pdevice
->info
->gen
>= 9, /* FINISHME CHV */
401 .textureCompressionBC
= true,
402 .occlusionQueryPrecise
= true,
403 .pipelineStatisticsQuery
= false,
404 .fragmentStoresAndAtomics
= true,
405 .shaderTessellationAndGeometryPointSize
= true,
406 .shaderImageGatherExtended
= false,
407 .shaderStorageImageExtendedFormats
= false,
408 .shaderStorageImageMultisample
= false,
409 .shaderUniformBufferArrayDynamicIndexing
= true,
410 .shaderSampledImageArrayDynamicIndexing
= true,
411 .shaderStorageBufferArrayDynamicIndexing
= true,
412 .shaderStorageImageArrayDynamicIndexing
= true,
413 .shaderStorageImageReadWithoutFormat
= false,
414 .shaderStorageImageWriteWithoutFormat
= true,
415 .shaderClipDistance
= false,
416 .shaderCullDistance
= false,
417 .shaderFloat64
= false,
418 .shaderInt64
= false,
419 .shaderInt16
= false,
421 .variableMultisampleRate
= false,
422 .inheritedQueries
= false,
425 /* We can't do image stores in vec4 shaders */
426 pFeatures
->vertexPipelineStoresAndAtomics
=
427 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
428 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
432 anv_device_get_cache_uuid(void *uuid
)
434 memset(uuid
, 0, VK_UUID_SIZE
);
435 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", ANV_TIMESTAMP
);
438 void anv_GetPhysicalDeviceProperties(
439 VkPhysicalDevice physicalDevice
,
440 VkPhysicalDeviceProperties
* pProperties
)
442 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
443 const struct brw_device_info
*devinfo
= pdevice
->info
;
445 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
447 /* See assertions made when programming the buffer surface state. */
448 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
449 (1ul << 30) : (1ul << 27);
451 VkSampleCountFlags sample_counts
=
452 isl_device_get_sample_counts(&pdevice
->isl_dev
);
454 VkPhysicalDeviceLimits limits
= {
455 .maxImageDimension1D
= (1 << 14),
456 .maxImageDimension2D
= (1 << 14),
457 .maxImageDimension3D
= (1 << 11),
458 .maxImageDimensionCube
= (1 << 14),
459 .maxImageArrayLayers
= (1 << 11),
460 .maxTexelBufferElements
= 128 * 1024 * 1024,
461 .maxUniformBufferRange
= (1ul << 27),
462 .maxStorageBufferRange
= max_raw_buffer_sz
,
463 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
464 .maxMemoryAllocationCount
= UINT32_MAX
,
465 .maxSamplerAllocationCount
= 64 * 1024,
466 .bufferImageGranularity
= 64, /* A cache line */
467 .sparseAddressSpaceSize
= 0,
468 .maxBoundDescriptorSets
= MAX_SETS
,
469 .maxPerStageDescriptorSamplers
= 64,
470 .maxPerStageDescriptorUniformBuffers
= 64,
471 .maxPerStageDescriptorStorageBuffers
= 64,
472 .maxPerStageDescriptorSampledImages
= 64,
473 .maxPerStageDescriptorStorageImages
= 64,
474 .maxPerStageDescriptorInputAttachments
= 64,
475 .maxPerStageResources
= 128,
476 .maxDescriptorSetSamplers
= 256,
477 .maxDescriptorSetUniformBuffers
= 256,
478 .maxDescriptorSetUniformBuffersDynamic
= 256,
479 .maxDescriptorSetStorageBuffers
= 256,
480 .maxDescriptorSetStorageBuffersDynamic
= 256,
481 .maxDescriptorSetSampledImages
= 256,
482 .maxDescriptorSetStorageImages
= 256,
483 .maxDescriptorSetInputAttachments
= 256,
484 .maxVertexInputAttributes
= 32,
485 .maxVertexInputBindings
= 32,
486 .maxVertexInputAttributeOffset
= 2047,
487 .maxVertexInputBindingStride
= 2048,
488 .maxVertexOutputComponents
= 128,
489 .maxTessellationGenerationLevel
= 0,
490 .maxTessellationPatchSize
= 0,
491 .maxTessellationControlPerVertexInputComponents
= 0,
492 .maxTessellationControlPerVertexOutputComponents
= 0,
493 .maxTessellationControlPerPatchOutputComponents
= 0,
494 .maxTessellationControlTotalOutputComponents
= 0,
495 .maxTessellationEvaluationInputComponents
= 0,
496 .maxTessellationEvaluationOutputComponents
= 0,
497 .maxGeometryShaderInvocations
= 32,
498 .maxGeometryInputComponents
= 64,
499 .maxGeometryOutputComponents
= 128,
500 .maxGeometryOutputVertices
= 256,
501 .maxGeometryTotalOutputComponents
= 1024,
502 .maxFragmentInputComponents
= 128,
503 .maxFragmentOutputAttachments
= 8,
504 .maxFragmentDualSrcAttachments
= 2,
505 .maxFragmentCombinedOutputResources
= 8,
506 .maxComputeSharedMemorySize
= 32768,
507 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
508 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
509 .maxComputeWorkGroupSize
= {
510 16 * devinfo
->max_cs_threads
,
511 16 * devinfo
->max_cs_threads
,
512 16 * devinfo
->max_cs_threads
,
514 .subPixelPrecisionBits
= 4 /* FIXME */,
515 .subTexelPrecisionBits
= 4 /* FIXME */,
516 .mipmapPrecisionBits
= 4 /* FIXME */,
517 .maxDrawIndexedIndexValue
= UINT32_MAX
,
518 .maxDrawIndirectCount
= UINT32_MAX
,
519 .maxSamplerLodBias
= 16,
520 .maxSamplerAnisotropy
= 16,
521 .maxViewports
= MAX_VIEWPORTS
,
522 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
523 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
524 .viewportSubPixelBits
= 13, /* We take a float? */
525 .minMemoryMapAlignment
= 4096, /* A page */
526 .minTexelBufferOffsetAlignment
= 1,
527 .minUniformBufferOffsetAlignment
= 1,
528 .minStorageBufferOffsetAlignment
= 1,
529 .minTexelOffset
= -8,
531 .minTexelGatherOffset
= -8,
532 .maxTexelGatherOffset
= 7,
533 .minInterpolationOffset
= -0.5,
534 .maxInterpolationOffset
= 0.4375,
535 .subPixelInterpolationOffsetBits
= 4,
536 .maxFramebufferWidth
= (1 << 14),
537 .maxFramebufferHeight
= (1 << 14),
538 .maxFramebufferLayers
= (1 << 10),
539 .framebufferColorSampleCounts
= sample_counts
,
540 .framebufferDepthSampleCounts
= sample_counts
,
541 .framebufferStencilSampleCounts
= sample_counts
,
542 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
543 .maxColorAttachments
= MAX_RTS
,
544 .sampledImageColorSampleCounts
= sample_counts
,
545 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
546 .sampledImageDepthSampleCounts
= sample_counts
,
547 .sampledImageStencilSampleCounts
= sample_counts
,
548 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
549 .maxSampleMaskWords
= 1,
550 .timestampComputeAndGraphics
= false,
551 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
552 .maxClipDistances
= 0 /* FIXME */,
553 .maxCullDistances
= 0 /* FIXME */,
554 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
555 .discreteQueuePriorities
= 1,
556 .pointSizeRange
= { 0.125, 255.875 },
557 .lineWidthRange
= { 0.0, 7.9921875 },
558 .pointSizeGranularity
= (1.0 / 8.0),
559 .lineWidthGranularity
= (1.0 / 128.0),
560 .strictLines
= false, /* FINISHME */
561 .standardSampleLocations
= true,
562 .optimalBufferCopyOffsetAlignment
= 128,
563 .optimalBufferCopyRowPitchAlignment
= 128,
564 .nonCoherentAtomSize
= 64,
567 *pProperties
= (VkPhysicalDeviceProperties
) {
568 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
571 .deviceID
= pdevice
->chipset_id
,
572 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
574 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
577 strcpy(pProperties
->deviceName
, pdevice
->name
);
578 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
581 void anv_GetPhysicalDeviceQueueFamilyProperties(
582 VkPhysicalDevice physicalDevice
,
584 VkQueueFamilyProperties
* pQueueFamilyProperties
)
586 if (pQueueFamilyProperties
== NULL
) {
591 assert(*pCount
>= 1);
593 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
594 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
595 VK_QUEUE_COMPUTE_BIT
|
596 VK_QUEUE_TRANSFER_BIT
,
598 .timestampValidBits
= 36, /* XXX: Real value here */
599 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
603 void anv_GetPhysicalDeviceMemoryProperties(
604 VkPhysicalDevice physicalDevice
,
605 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
607 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
608 VkDeviceSize heap_size
;
610 /* Reserve some wiggle room for the driver by exposing only 75% of the
611 * aperture to the heap.
613 heap_size
= 3 * physical_device
->aperture_size
/ 4;
615 if (physical_device
->info
->has_llc
) {
616 /* Big core GPUs share LLC with the CPU and thus one memory type can be
617 * both cached and coherent at the same time.
619 pMemoryProperties
->memoryTypeCount
= 1;
620 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
621 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
622 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
623 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
624 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
628 /* The spec requires that we expose a host-visible, coherent memory
629 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
630 * to give the application a choice between cached, but not coherent and
631 * coherent but uncached (WC though).
633 pMemoryProperties
->memoryTypeCount
= 2;
634 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
635 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
636 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
637 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
640 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
641 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
642 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
643 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
648 pMemoryProperties
->memoryHeapCount
= 1;
649 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
651 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
655 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
659 return anv_lookup_entrypoint(pName
);
662 /* With version 1+ of the loader interface the ICD should expose
663 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
666 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
671 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
675 return anv_GetInstanceProcAddr(instance
, pName
);
678 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
682 return anv_lookup_entrypoint(pName
);
686 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
688 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
689 queue
->device
= device
;
690 queue
->pool
= &device
->surface_state_pool
;
696 anv_queue_finish(struct anv_queue
*queue
)
700 static struct anv_state
701 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
703 struct anv_state state
;
705 state
= anv_state_pool_alloc(pool
, size
, align
);
706 memcpy(state
.map
, p
, size
);
708 if (!pool
->block_pool
->device
->info
.has_llc
)
709 anv_state_clflush(state
);
714 struct gen8_border_color
{
719 /* Pad out to 64 bytes */
724 anv_device_init_border_colors(struct anv_device
*device
)
726 static const struct gen8_border_color border_colors
[] = {
727 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
728 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
729 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
730 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
731 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
732 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
735 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
736 sizeof(border_colors
), 64,
741 anv_device_submit_simple_batch(struct anv_device
*device
,
742 struct anv_batch
*batch
)
744 struct drm_i915_gem_execbuffer2 execbuf
;
745 struct drm_i915_gem_exec_object2 exec2_objects
[1];
747 VkResult result
= VK_SUCCESS
;
752 /* Kernel driver requires 8 byte aligned batch length */
753 size
= align_u32(batch
->next
- batch
->start
, 8);
754 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
755 if (result
!= VK_SUCCESS
)
758 memcpy(bo
.map
, batch
->start
, size
);
759 if (!device
->info
.has_llc
)
760 anv_clflush_range(bo
.map
, size
);
762 exec2_objects
[0].handle
= bo
.gem_handle
;
763 exec2_objects
[0].relocation_count
= 0;
764 exec2_objects
[0].relocs_ptr
= 0;
765 exec2_objects
[0].alignment
= 0;
766 exec2_objects
[0].offset
= bo
.offset
;
767 exec2_objects
[0].flags
= 0;
768 exec2_objects
[0].rsvd1
= 0;
769 exec2_objects
[0].rsvd2
= 0;
771 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
772 execbuf
.buffer_count
= 1;
773 execbuf
.batch_start_offset
= 0;
774 execbuf
.batch_len
= size
;
775 execbuf
.cliprects_ptr
= 0;
776 execbuf
.num_cliprects
= 0;
781 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
782 execbuf
.rsvd1
= device
->context_id
;
785 ret
= anv_gem_execbuffer(device
, &execbuf
);
787 /* We don't know the real error. */
788 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
793 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
795 /* We don't know the real error. */
796 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
801 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
806 VkResult
anv_CreateDevice(
807 VkPhysicalDevice physicalDevice
,
808 const VkDeviceCreateInfo
* pCreateInfo
,
809 const VkAllocationCallbacks
* pAllocator
,
812 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
814 struct anv_device
*device
;
816 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
818 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
820 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
821 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
822 device_extensions
[j
].extensionName
) == 0) {
828 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
831 anv_set_dispatch_devinfo(physical_device
->info
);
833 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
835 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
837 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
839 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
840 device
->instance
= physical_device
->instance
;
841 device
->chipset_id
= physical_device
->chipset_id
;
844 device
->alloc
= *pAllocator
;
846 device
->alloc
= physical_device
->instance
->alloc
;
848 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
849 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
850 if (device
->fd
== -1) {
851 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
855 device
->context_id
= anv_gem_create_context(device
);
856 if (device
->context_id
== -1) {
857 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
861 device
->info
= *physical_device
->info
;
862 device
->isl_dev
= physical_device
->isl_dev
;
864 /* On Broadwell and later, we can use batch chaining to more efficiently
865 * implement growing command buffers. Prior to Haswell, the kernel
866 * command parser gets in the way and we have to fall back to growing
869 device
->can_chain_batches
= device
->info
.gen
>= 8;
871 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
872 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
874 pthread_mutex_init(&device
->mutex
, NULL
);
876 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
878 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
880 anv_state_pool_init(&device
->dynamic_state_pool
,
881 &device
->dynamic_state_block_pool
);
883 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
884 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
886 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
888 anv_state_pool_init(&device
->surface_state_pool
,
889 &device
->surface_state_block_pool
);
891 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
893 anv_scratch_pool_init(device
, &device
->scratch_pool
);
895 anv_queue_init(device
, &device
->queue
);
897 switch (device
->info
.gen
) {
899 if (!device
->info
.is_haswell
)
900 result
= gen7_init_device_state(device
);
902 result
= gen75_init_device_state(device
);
905 result
= gen8_init_device_state(device
);
908 result
= gen9_init_device_state(device
);
911 /* Shouldn't get here as we don't create physical devices for any other
913 unreachable("unhandled gen");
915 if (result
!= VK_SUCCESS
)
918 result
= anv_device_init_meta(device
);
919 if (result
!= VK_SUCCESS
)
922 anv_device_init_border_colors(device
);
924 *pDevice
= anv_device_to_handle(device
);
931 anv_free(&device
->alloc
, device
);
936 void anv_DestroyDevice(
938 const VkAllocationCallbacks
* pAllocator
)
940 ANV_FROM_HANDLE(anv_device
, device
, _device
);
942 anv_queue_finish(&device
->queue
);
944 anv_device_finish_meta(device
);
947 /* We only need to free these to prevent valgrind errors. The backing
948 * BO will go away in a couple of lines so we don't actually leak.
950 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
953 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
954 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
956 anv_bo_pool_finish(&device
->batch_bo_pool
);
957 anv_state_pool_finish(&device
->dynamic_state_pool
);
958 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
959 anv_block_pool_finish(&device
->instruction_block_pool
);
960 anv_state_pool_finish(&device
->surface_state_pool
);
961 anv_block_pool_finish(&device
->surface_state_block_pool
);
962 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
966 pthread_mutex_destroy(&device
->mutex
);
968 anv_free(&device
->alloc
, device
);
971 VkResult
anv_EnumerateInstanceExtensionProperties(
972 const char* pLayerName
,
973 uint32_t* pPropertyCount
,
974 VkExtensionProperties
* pProperties
)
976 if (pProperties
== NULL
) {
977 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
981 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
983 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
984 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
989 VkResult
anv_EnumerateDeviceExtensionProperties(
990 VkPhysicalDevice physicalDevice
,
991 const char* pLayerName
,
992 uint32_t* pPropertyCount
,
993 VkExtensionProperties
* pProperties
)
995 if (pProperties
== NULL
) {
996 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1000 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
1002 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1003 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
1008 VkResult
anv_EnumerateInstanceLayerProperties(
1009 uint32_t* pPropertyCount
,
1010 VkLayerProperties
* pProperties
)
1012 if (pProperties
== NULL
) {
1013 *pPropertyCount
= 0;
1017 /* None supported at this time */
1018 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1021 VkResult
anv_EnumerateDeviceLayerProperties(
1022 VkPhysicalDevice physicalDevice
,
1023 uint32_t* pPropertyCount
,
1024 VkLayerProperties
* pProperties
)
1026 if (pProperties
== NULL
) {
1027 *pPropertyCount
= 0;
1031 /* None supported at this time */
1032 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1035 void anv_GetDeviceQueue(
1037 uint32_t queueNodeIndex
,
1038 uint32_t queueIndex
,
1041 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1043 assert(queueIndex
== 0);
1045 *pQueue
= anv_queue_to_handle(&device
->queue
);
1048 VkResult
anv_QueueSubmit(
1050 uint32_t submitCount
,
1051 const VkSubmitInfo
* pSubmits
,
1054 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1055 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1056 struct anv_device
*device
= queue
->device
;
1059 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1060 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1061 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1062 pSubmits
[i
].pCommandBuffers
[j
]);
1063 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1065 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1067 /* We don't know the real error. */
1068 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1069 "execbuf2 failed: %m");
1072 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1073 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1078 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1080 /* We don't know the real error. */
1081 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1082 "execbuf2 failed: %m");
1089 VkResult
anv_QueueWaitIdle(
1092 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1094 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1097 VkResult
anv_DeviceWaitIdle(
1100 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1101 struct anv_batch batch
;
1104 batch
.start
= batch
.next
= cmds
;
1105 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1107 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1108 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1110 return anv_device_submit_simple_batch(device
, &batch
);
1114 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1116 bo
->gem_handle
= anv_gem_create(device
, size
);
1117 if (!bo
->gem_handle
)
1118 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1124 bo
->is_winsys_bo
= false;
1129 VkResult
anv_AllocateMemory(
1131 const VkMemoryAllocateInfo
* pAllocateInfo
,
1132 const VkAllocationCallbacks
* pAllocator
,
1133 VkDeviceMemory
* pMem
)
1135 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1136 struct anv_device_memory
*mem
;
1139 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1141 if (pAllocateInfo
->allocationSize
== 0) {
1142 /* Apparently, this is allowed */
1143 *pMem
= VK_NULL_HANDLE
;
1147 /* We support exactly one memory heap. */
1148 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1149 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1151 /* FINISHME: Fail if allocation request exceeds heap size. */
1153 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1154 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1156 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1158 /* The kernel is going to give us whole pages anyway */
1159 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1161 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1162 if (result
!= VK_SUCCESS
)
1165 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1167 *pMem
= anv_device_memory_to_handle(mem
);
1172 anv_free2(&device
->alloc
, pAllocator
, mem
);
1177 void anv_FreeMemory(
1179 VkDeviceMemory _mem
,
1180 const VkAllocationCallbacks
* pAllocator
)
1182 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1183 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1189 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1191 if (mem
->bo
.gem_handle
!= 0)
1192 anv_gem_close(device
, mem
->bo
.gem_handle
);
1194 anv_free2(&device
->alloc
, pAllocator
, mem
);
1197 VkResult
anv_MapMemory(
1199 VkDeviceMemory _memory
,
1200 VkDeviceSize offset
,
1202 VkMemoryMapFlags flags
,
1205 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1206 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1213 if (size
== VK_WHOLE_SIZE
)
1214 size
= mem
->bo
.size
- offset
;
1216 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1217 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1218 * at a time is valid. We could just mmap up front and return an offset
1219 * pointer here, but that may exhaust virtual memory on 32 bit
1222 uint32_t gem_flags
= 0;
1223 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1224 gem_flags
|= I915_MMAP_WC
;
1226 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1227 uint64_t map_offset
= offset
& ~4095ull;
1228 assert(offset
>= map_offset
);
1229 uint64_t map_size
= (offset
+ size
) - map_offset
;
1231 /* Let's map whole pages */
1232 map_size
= align_u64(map_size
, 4096);
1234 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1235 map_offset
, map_size
, gem_flags
);
1236 mem
->map_size
= map_size
;
1238 *ppData
= mem
->map
+ (offset
- map_offset
);
1243 void anv_UnmapMemory(
1245 VkDeviceMemory _memory
)
1247 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1252 anv_gem_munmap(mem
->map
, mem
->map_size
);
1256 clflush_mapped_ranges(struct anv_device
*device
,
1258 const VkMappedMemoryRange
*ranges
)
1260 for (uint32_t i
= 0; i
< count
; i
++) {
1261 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1262 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1265 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1266 end
= mem
->map
+ mem
->map_size
;
1268 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1271 __builtin_ia32_clflush(p
);
1272 p
+= CACHELINE_SIZE
;
1277 VkResult
anv_FlushMappedMemoryRanges(
1279 uint32_t memoryRangeCount
,
1280 const VkMappedMemoryRange
* pMemoryRanges
)
1282 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1284 if (device
->info
.has_llc
)
1287 /* Make sure the writes we're flushing have landed. */
1288 __builtin_ia32_mfence();
1290 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1295 VkResult
anv_InvalidateMappedMemoryRanges(
1297 uint32_t memoryRangeCount
,
1298 const VkMappedMemoryRange
* pMemoryRanges
)
1300 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1302 if (device
->info
.has_llc
)
1305 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1307 /* Make sure no reads get moved up above the invalidate. */
1308 __builtin_ia32_mfence();
1313 void anv_GetBufferMemoryRequirements(
1316 VkMemoryRequirements
* pMemoryRequirements
)
1318 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1320 /* The Vulkan spec (git aaed022) says:
1322 * memoryTypeBits is a bitfield and contains one bit set for every
1323 * supported memory type for the resource. The bit `1<<i` is set if and
1324 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1325 * structure for the physical device is supported.
1327 * We support exactly one memory type.
1329 pMemoryRequirements
->memoryTypeBits
= 1;
1331 pMemoryRequirements
->size
= buffer
->size
;
1332 pMemoryRequirements
->alignment
= 16;
1335 void anv_GetImageMemoryRequirements(
1338 VkMemoryRequirements
* pMemoryRequirements
)
1340 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1342 /* The Vulkan spec (git aaed022) says:
1344 * memoryTypeBits is a bitfield and contains one bit set for every
1345 * supported memory type for the resource. The bit `1<<i` is set if and
1346 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1347 * structure for the physical device is supported.
1349 * We support exactly one memory type.
1351 pMemoryRequirements
->memoryTypeBits
= 1;
1353 pMemoryRequirements
->size
= image
->size
;
1354 pMemoryRequirements
->alignment
= image
->alignment
;
1357 void anv_GetImageSparseMemoryRequirements(
1360 uint32_t* pSparseMemoryRequirementCount
,
1361 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1366 void anv_GetDeviceMemoryCommitment(
1368 VkDeviceMemory memory
,
1369 VkDeviceSize
* pCommittedMemoryInBytes
)
1371 *pCommittedMemoryInBytes
= 0;
1374 VkResult
anv_BindBufferMemory(
1377 VkDeviceMemory _memory
,
1378 VkDeviceSize memoryOffset
)
1380 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1381 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1384 buffer
->bo
= &mem
->bo
;
1385 buffer
->offset
= memoryOffset
;
1394 VkResult
anv_BindImageMemory(
1397 VkDeviceMemory _memory
,
1398 VkDeviceSize memoryOffset
)
1400 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1401 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1404 image
->bo
= &mem
->bo
;
1405 image
->offset
= memoryOffset
;
1414 VkResult
anv_QueueBindSparse(
1416 uint32_t bindInfoCount
,
1417 const VkBindSparseInfo
* pBindInfo
,
1420 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1423 VkResult
anv_CreateFence(
1425 const VkFenceCreateInfo
* pCreateInfo
,
1426 const VkAllocationCallbacks
* pAllocator
,
1429 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1430 struct anv_bo fence_bo
;
1431 struct anv_fence
*fence
;
1432 struct anv_batch batch
;
1435 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1437 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1438 if (result
!= VK_SUCCESS
)
1441 /* Fences are small. Just store the CPU data structure in the BO. */
1442 fence
= fence_bo
.map
;
1443 fence
->bo
= fence_bo
;
1445 /* Place the batch after the CPU data but on its own cache line. */
1446 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1447 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1448 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1449 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1450 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1452 if (!device
->info
.has_llc
) {
1453 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1454 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1455 __builtin_ia32_mfence();
1456 __builtin_ia32_clflush(batch
.start
);
1459 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1460 fence
->exec2_objects
[0].relocation_count
= 0;
1461 fence
->exec2_objects
[0].relocs_ptr
= 0;
1462 fence
->exec2_objects
[0].alignment
= 0;
1463 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1464 fence
->exec2_objects
[0].flags
= 0;
1465 fence
->exec2_objects
[0].rsvd1
= 0;
1466 fence
->exec2_objects
[0].rsvd2
= 0;
1468 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1469 fence
->execbuf
.buffer_count
= 1;
1470 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1471 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1472 fence
->execbuf
.cliprects_ptr
= 0;
1473 fence
->execbuf
.num_cliprects
= 0;
1474 fence
->execbuf
.DR1
= 0;
1475 fence
->execbuf
.DR4
= 0;
1477 fence
->execbuf
.flags
=
1478 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1479 fence
->execbuf
.rsvd1
= device
->context_id
;
1480 fence
->execbuf
.rsvd2
= 0;
1482 fence
->ready
= false;
1484 *pFence
= anv_fence_to_handle(fence
);
1489 void anv_DestroyFence(
1492 const VkAllocationCallbacks
* pAllocator
)
1494 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1495 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1497 assert(fence
->bo
.map
== fence
);
1498 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1501 VkResult
anv_ResetFences(
1503 uint32_t fenceCount
,
1504 const VkFence
* pFences
)
1506 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1507 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1508 fence
->ready
= false;
1514 VkResult
anv_GetFenceStatus(
1518 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1519 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1526 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1528 fence
->ready
= true;
1532 return VK_NOT_READY
;
1535 VkResult
anv_WaitForFences(
1537 uint32_t fenceCount
,
1538 const VkFence
* pFences
,
1542 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1544 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1545 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1546 * for a couple of kernel releases. Since there's no way to know
1547 * whether or not the kernel we're using is one of the broken ones, the
1548 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1549 * maximum timeout from 584 years to 292 years - likely not a big deal.
1551 if (timeout
> INT64_MAX
)
1552 timeout
= INT64_MAX
;
1554 int64_t t
= timeout
;
1556 /* FIXME: handle !waitAll */
1558 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1559 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1560 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1561 if (ret
== -1 && errno
== ETIME
) {
1563 } else if (ret
== -1) {
1564 /* We don't know the real error. */
1565 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1566 "gem wait failed: %m");
1573 // Queue semaphore functions
1575 VkResult
anv_CreateSemaphore(
1577 const VkSemaphoreCreateInfo
* pCreateInfo
,
1578 const VkAllocationCallbacks
* pAllocator
,
1579 VkSemaphore
* pSemaphore
)
1581 /* The DRM execbuffer ioctl always execute in-oder, even between different
1582 * rings. As such, there's nothing to do for the user space semaphore.
1585 *pSemaphore
= (VkSemaphore
)1;
1590 void anv_DestroySemaphore(
1592 VkSemaphore semaphore
,
1593 const VkAllocationCallbacks
* pAllocator
)
1599 VkResult
anv_CreateEvent(
1601 const VkEventCreateInfo
* pCreateInfo
,
1602 const VkAllocationCallbacks
* pAllocator
,
1605 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1606 struct anv_state state
;
1607 struct anv_event
*event
;
1609 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1611 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1614 event
->state
= state
;
1615 event
->semaphore
= VK_EVENT_RESET
;
1617 if (!device
->info
.has_llc
) {
1618 /* Make sure the writes we're flushing have landed. */
1619 __builtin_ia32_mfence();
1620 __builtin_ia32_clflush(event
);
1623 *pEvent
= anv_event_to_handle(event
);
1628 void anv_DestroyEvent(
1631 const VkAllocationCallbacks
* pAllocator
)
1633 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1634 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1636 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1639 VkResult
anv_GetEventStatus(
1643 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1644 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1646 if (!device
->info
.has_llc
) {
1647 /* Invalidate read cache before reading event written by GPU. */
1648 __builtin_ia32_clflush(event
);
1649 __builtin_ia32_mfence();
1653 return event
->semaphore
;
1656 VkResult
anv_SetEvent(
1660 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1661 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1663 event
->semaphore
= VK_EVENT_SET
;
1665 if (!device
->info
.has_llc
) {
1666 /* Make sure the writes we're flushing have landed. */
1667 __builtin_ia32_mfence();
1668 __builtin_ia32_clflush(event
);
1674 VkResult
anv_ResetEvent(
1678 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1679 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1681 event
->semaphore
= VK_EVENT_RESET
;
1683 if (!device
->info
.has_llc
) {
1684 /* Make sure the writes we're flushing have landed. */
1685 __builtin_ia32_mfence();
1686 __builtin_ia32_clflush(event
);
1694 VkResult
anv_CreateBuffer(
1696 const VkBufferCreateInfo
* pCreateInfo
,
1697 const VkAllocationCallbacks
* pAllocator
,
1700 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1701 struct anv_buffer
*buffer
;
1703 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1705 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1706 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1708 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1710 buffer
->size
= pCreateInfo
->size
;
1711 buffer
->usage
= pCreateInfo
->usage
;
1715 *pBuffer
= anv_buffer_to_handle(buffer
);
1720 void anv_DestroyBuffer(
1723 const VkAllocationCallbacks
* pAllocator
)
1725 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1726 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1728 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1732 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1733 enum isl_format format
,
1734 uint32_t offset
, uint32_t range
, uint32_t stride
)
1736 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1738 .mocs
= device
->default_mocs
,
1743 if (!device
->info
.has_llc
)
1744 anv_state_clflush(state
);
1747 void anv_DestroySampler(
1750 const VkAllocationCallbacks
* pAllocator
)
1752 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1753 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1755 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1758 VkResult
anv_CreateFramebuffer(
1760 const VkFramebufferCreateInfo
* pCreateInfo
,
1761 const VkAllocationCallbacks
* pAllocator
,
1762 VkFramebuffer
* pFramebuffer
)
1764 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1765 struct anv_framebuffer
*framebuffer
;
1767 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1769 size_t size
= sizeof(*framebuffer
) +
1770 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1771 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1772 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1773 if (framebuffer
== NULL
)
1774 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1776 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1777 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1778 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1779 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1782 framebuffer
->width
= pCreateInfo
->width
;
1783 framebuffer
->height
= pCreateInfo
->height
;
1784 framebuffer
->layers
= pCreateInfo
->layers
;
1786 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1791 void anv_DestroyFramebuffer(
1794 const VkAllocationCallbacks
* pAllocator
)
1796 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1797 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1799 anv_free2(&device
->alloc
, pAllocator
, fb
);