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
31 #include "anv_private.h"
32 #include "anv_timestamp.h"
33 #include "util/strtod.h"
34 #include "util/debug.h"
36 #include "genxml/gen7_pack.h"
38 struct anv_dispatch_table dtable
;
41 compiler_debug_log(void *data
, const char *fmt
, ...)
45 compiler_perf_log(void *data
, const char *fmt
, ...)
50 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
51 vfprintf(stderr
, fmt
, args
);
57 anv_physical_device_init(struct anv_physical_device
*device
,
58 struct anv_instance
*instance
,
64 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
66 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
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(VK_ERROR_INCOMPATIBLE_DRIVER
);
80 device
->name
= gen_get_device_name(device
->chipset_id
);
81 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
82 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
86 if (device
->info
.is_haswell
) {
87 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
88 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
89 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
90 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
91 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
92 } else if (device
->info
.gen
>= 8) {
93 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
94 * supported as anything */
96 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
97 "Vulkan not yet supported on %s", device
->name
);
101 device
->cmd_parser_version
= -1;
102 if (device
->info
.gen
== 7) {
103 device
->cmd_parser_version
=
104 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
105 if (device
->cmd_parser_version
== -1) {
106 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
107 "failed to get command parser version");
112 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
113 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
114 "failed to get aperture size: %m");
118 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
119 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
120 "kernel missing gem wait");
124 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
125 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
126 "kernel missing execbuf2");
130 if (!device
->info
.has_llc
&&
131 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
132 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
133 "kernel missing wc mmap");
137 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
139 /* GENs prior to 8 do not support EU/Subslice info */
140 if (device
->info
.gen
>= 8) {
141 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
142 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
144 /* Without this information, we cannot get the right Braswell
145 * brandstrings, and we have to use conservative numbers for GPGPU on
146 * many platforms, but otherwise, things will just work.
148 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
149 fprintf(stderr
, "WARNING: Kernel 4.1 required to properly"
150 " query GPU properties.\n");
152 } else if (device
->info
.gen
== 7) {
153 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
156 if (device
->info
.is_cherryview
&&
157 device
->subslice_total
> 0 && device
->eu_total
> 0) {
158 /* Logical CS threads = EUs per subslice * 7 threads per EU */
159 uint32_t max_cs_threads
= device
->eu_total
/ device
->subslice_total
* 7;
161 /* Fuse configurations may give more threads than expected, never less. */
162 if (max_cs_threads
> device
->info
.max_cs_threads
)
163 device
->info
.max_cs_threads
= max_cs_threads
;
168 brw_process_intel_debug_variable();
170 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
171 if (device
->compiler
== NULL
) {
172 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
175 device
->compiler
->shader_debug_log
= compiler_debug_log
;
176 device
->compiler
->shader_perf_log
= compiler_perf_log
;
178 result
= anv_init_wsi(device
);
179 if (result
!= VK_SUCCESS
)
182 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
192 anv_physical_device_finish(struct anv_physical_device
*device
)
194 anv_finish_wsi(device
);
195 ralloc_free(device
->compiler
);
198 static const VkExtensionProperties global_extensions
[] = {
200 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
203 #ifdef VK_USE_PLATFORM_XCB_KHR
205 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
209 #ifdef VK_USE_PLATFORM_XLIB_KHR
211 .extensionName
= VK_KHR_XLIB_SURFACE_EXTENSION_NAME
,
215 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
217 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
223 static const VkExtensionProperties device_extensions
[] = {
225 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
231 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
232 VkSystemAllocationScope allocationScope
)
238 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
239 size_t align
, VkSystemAllocationScope allocationScope
)
241 return realloc(pOriginal
, size
);
245 default_free_func(void *pUserData
, void *pMemory
)
250 static const VkAllocationCallbacks default_alloc
= {
252 .pfnAllocation
= default_alloc_func
,
253 .pfnReallocation
= default_realloc_func
,
254 .pfnFree
= default_free_func
,
257 VkResult
anv_CreateInstance(
258 const VkInstanceCreateInfo
* pCreateInfo
,
259 const VkAllocationCallbacks
* pAllocator
,
260 VkInstance
* pInstance
)
262 struct anv_instance
*instance
;
264 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
266 uint32_t client_version
;
267 if (pCreateInfo
->pApplicationInfo
&&
268 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
269 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
271 client_version
= VK_MAKE_VERSION(1, 0, 0);
274 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
275 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
276 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
277 "Client requested version %d.%d.%d",
278 VK_VERSION_MAJOR(client_version
),
279 VK_VERSION_MINOR(client_version
),
280 VK_VERSION_PATCH(client_version
));
283 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
285 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
286 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
287 global_extensions
[j
].extensionName
) == 0) {
293 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
296 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
297 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
299 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
301 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
304 instance
->alloc
= *pAllocator
;
306 instance
->alloc
= default_alloc
;
308 instance
->apiVersion
= client_version
;
309 instance
->physicalDeviceCount
= -1;
313 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
315 *pInstance
= anv_instance_to_handle(instance
);
320 void anv_DestroyInstance(
321 VkInstance _instance
,
322 const VkAllocationCallbacks
* pAllocator
)
324 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
326 if (instance
->physicalDeviceCount
> 0) {
327 /* We support at most one physical device. */
328 assert(instance
->physicalDeviceCount
== 1);
329 anv_physical_device_finish(&instance
->physicalDevice
);
332 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
336 vk_free(&instance
->alloc
, instance
);
339 VkResult
anv_EnumeratePhysicalDevices(
340 VkInstance _instance
,
341 uint32_t* pPhysicalDeviceCount
,
342 VkPhysicalDevice
* pPhysicalDevices
)
344 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
347 if (instance
->physicalDeviceCount
< 0) {
349 for (unsigned i
= 0; i
< 8; i
++) {
350 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
351 result
= anv_physical_device_init(&instance
->physicalDevice
,
353 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
357 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
358 instance
->physicalDeviceCount
= 0;
359 } else if (result
== VK_SUCCESS
) {
360 instance
->physicalDeviceCount
= 1;
366 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
367 * otherwise it's an inout parameter.
369 * The Vulkan spec (git aaed022) says:
371 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
372 * that is initialized with the number of devices the application is
373 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
374 * an array of at least this many VkPhysicalDevice handles [...].
376 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
377 * overwrites the contents of the variable pointed to by
378 * pPhysicalDeviceCount with the number of physical devices in in the
379 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
380 * pPhysicalDeviceCount with the number of physical handles written to
383 if (!pPhysicalDevices
) {
384 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
385 } else if (*pPhysicalDeviceCount
>= 1) {
386 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
387 *pPhysicalDeviceCount
= 1;
388 } else if (*pPhysicalDeviceCount
< instance
->physicalDeviceCount
) {
389 return VK_INCOMPLETE
;
391 *pPhysicalDeviceCount
= 0;
397 void anv_GetPhysicalDeviceFeatures(
398 VkPhysicalDevice physicalDevice
,
399 VkPhysicalDeviceFeatures
* pFeatures
)
401 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
403 *pFeatures
= (VkPhysicalDeviceFeatures
) {
404 .robustBufferAccess
= true,
405 .fullDrawIndexUint32
= true,
406 .imageCubeArray
= false,
407 .independentBlend
= true,
408 .geometryShader
= true,
409 .tessellationShader
= false,
410 .sampleRateShading
= true,
411 .dualSrcBlend
= true,
413 .multiDrawIndirect
= false,
414 .drawIndirectFirstInstance
= false,
416 .depthBiasClamp
= false,
417 .fillModeNonSolid
= true,
418 .depthBounds
= false,
422 .multiViewport
= true,
423 .samplerAnisotropy
= true,
424 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
425 pdevice
->info
.is_baytrail
,
426 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
427 .textureCompressionBC
= true,
428 .occlusionQueryPrecise
= true,
429 .pipelineStatisticsQuery
= false,
430 .fragmentStoresAndAtomics
= true,
431 .shaderTessellationAndGeometryPointSize
= true,
432 .shaderImageGatherExtended
= false,
433 .shaderStorageImageExtendedFormats
= false,
434 .shaderStorageImageMultisample
= false,
435 .shaderUniformBufferArrayDynamicIndexing
= true,
436 .shaderSampledImageArrayDynamicIndexing
= true,
437 .shaderStorageBufferArrayDynamicIndexing
= true,
438 .shaderStorageImageArrayDynamicIndexing
= true,
439 .shaderStorageImageReadWithoutFormat
= false,
440 .shaderStorageImageWriteWithoutFormat
= true,
441 .shaderClipDistance
= true,
442 .shaderCullDistance
= true,
443 .shaderFloat64
= false,
444 .shaderInt64
= false,
445 .shaderInt16
= false,
447 .variableMultisampleRate
= false,
448 .inheritedQueries
= false,
451 /* We can't do image stores in vec4 shaders */
452 pFeatures
->vertexPipelineStoresAndAtomics
=
453 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
454 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
458 anv_device_get_cache_uuid(void *uuid
)
460 memset(uuid
, 0, VK_UUID_SIZE
);
461 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", ANV_TIMESTAMP
);
464 void anv_GetPhysicalDeviceProperties(
465 VkPhysicalDevice physicalDevice
,
466 VkPhysicalDeviceProperties
* pProperties
)
468 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
469 const struct gen_device_info
*devinfo
= &pdevice
->info
;
471 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
473 /* See assertions made when programming the buffer surface state. */
474 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
475 (1ul << 30) : (1ul << 27);
477 VkSampleCountFlags sample_counts
=
478 isl_device_get_sample_counts(&pdevice
->isl_dev
);
480 VkPhysicalDeviceLimits limits
= {
481 .maxImageDimension1D
= (1 << 14),
482 .maxImageDimension2D
= (1 << 14),
483 .maxImageDimension3D
= (1 << 11),
484 .maxImageDimensionCube
= (1 << 14),
485 .maxImageArrayLayers
= (1 << 11),
486 .maxTexelBufferElements
= 128 * 1024 * 1024,
487 .maxUniformBufferRange
= (1ul << 27),
488 .maxStorageBufferRange
= max_raw_buffer_sz
,
489 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
490 .maxMemoryAllocationCount
= UINT32_MAX
,
491 .maxSamplerAllocationCount
= 64 * 1024,
492 .bufferImageGranularity
= 64, /* A cache line */
493 .sparseAddressSpaceSize
= 0,
494 .maxBoundDescriptorSets
= MAX_SETS
,
495 .maxPerStageDescriptorSamplers
= 64,
496 .maxPerStageDescriptorUniformBuffers
= 64,
497 .maxPerStageDescriptorStorageBuffers
= 64,
498 .maxPerStageDescriptorSampledImages
= 64,
499 .maxPerStageDescriptorStorageImages
= 64,
500 .maxPerStageDescriptorInputAttachments
= 64,
501 .maxPerStageResources
= 128,
502 .maxDescriptorSetSamplers
= 256,
503 .maxDescriptorSetUniformBuffers
= 256,
504 .maxDescriptorSetUniformBuffersDynamic
= 256,
505 .maxDescriptorSetStorageBuffers
= 256,
506 .maxDescriptorSetStorageBuffersDynamic
= 256,
507 .maxDescriptorSetSampledImages
= 256,
508 .maxDescriptorSetStorageImages
= 256,
509 .maxDescriptorSetInputAttachments
= 256,
510 .maxVertexInputAttributes
= 32,
511 .maxVertexInputBindings
= 32,
512 .maxVertexInputAttributeOffset
= 2047,
513 .maxVertexInputBindingStride
= 2048,
514 .maxVertexOutputComponents
= 128,
515 .maxTessellationGenerationLevel
= 0,
516 .maxTessellationPatchSize
= 0,
517 .maxTessellationControlPerVertexInputComponents
= 0,
518 .maxTessellationControlPerVertexOutputComponents
= 0,
519 .maxTessellationControlPerPatchOutputComponents
= 0,
520 .maxTessellationControlTotalOutputComponents
= 0,
521 .maxTessellationEvaluationInputComponents
= 0,
522 .maxTessellationEvaluationOutputComponents
= 0,
523 .maxGeometryShaderInvocations
= 32,
524 .maxGeometryInputComponents
= 64,
525 .maxGeometryOutputComponents
= 128,
526 .maxGeometryOutputVertices
= 256,
527 .maxGeometryTotalOutputComponents
= 1024,
528 .maxFragmentInputComponents
= 128,
529 .maxFragmentOutputAttachments
= 8,
530 .maxFragmentDualSrcAttachments
= 2,
531 .maxFragmentCombinedOutputResources
= 8,
532 .maxComputeSharedMemorySize
= 32768,
533 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
534 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
535 .maxComputeWorkGroupSize
= {
536 16 * devinfo
->max_cs_threads
,
537 16 * devinfo
->max_cs_threads
,
538 16 * devinfo
->max_cs_threads
,
540 .subPixelPrecisionBits
= 4 /* FIXME */,
541 .subTexelPrecisionBits
= 4 /* FIXME */,
542 .mipmapPrecisionBits
= 4 /* FIXME */,
543 .maxDrawIndexedIndexValue
= UINT32_MAX
,
544 .maxDrawIndirectCount
= UINT32_MAX
,
545 .maxSamplerLodBias
= 16,
546 .maxSamplerAnisotropy
= 16,
547 .maxViewports
= MAX_VIEWPORTS
,
548 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
549 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
550 .viewportSubPixelBits
= 13, /* We take a float? */
551 .minMemoryMapAlignment
= 4096, /* A page */
552 .minTexelBufferOffsetAlignment
= 1,
553 .minUniformBufferOffsetAlignment
= 1,
554 .minStorageBufferOffsetAlignment
= 1,
555 .minTexelOffset
= -8,
557 .minTexelGatherOffset
= -8,
558 .maxTexelGatherOffset
= 7,
559 .minInterpolationOffset
= -0.5,
560 .maxInterpolationOffset
= 0.4375,
561 .subPixelInterpolationOffsetBits
= 4,
562 .maxFramebufferWidth
= (1 << 14),
563 .maxFramebufferHeight
= (1 << 14),
564 .maxFramebufferLayers
= (1 << 10),
565 .framebufferColorSampleCounts
= sample_counts
,
566 .framebufferDepthSampleCounts
= sample_counts
,
567 .framebufferStencilSampleCounts
= sample_counts
,
568 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
569 .maxColorAttachments
= MAX_RTS
,
570 .sampledImageColorSampleCounts
= sample_counts
,
571 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
572 .sampledImageDepthSampleCounts
= sample_counts
,
573 .sampledImageStencilSampleCounts
= sample_counts
,
574 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
575 .maxSampleMaskWords
= 1,
576 .timestampComputeAndGraphics
= false,
577 .timestampPeriod
= time_stamp_base
,
578 .maxClipDistances
= 8,
579 .maxCullDistances
= 8,
580 .maxCombinedClipAndCullDistances
= 8,
581 .discreteQueuePriorities
= 1,
582 .pointSizeRange
= { 0.125, 255.875 },
583 .lineWidthRange
= { 0.0, 7.9921875 },
584 .pointSizeGranularity
= (1.0 / 8.0),
585 .lineWidthGranularity
= (1.0 / 128.0),
586 .strictLines
= false, /* FINISHME */
587 .standardSampleLocations
= true,
588 .optimalBufferCopyOffsetAlignment
= 128,
589 .optimalBufferCopyRowPitchAlignment
= 128,
590 .nonCoherentAtomSize
= 64,
593 *pProperties
= (VkPhysicalDeviceProperties
) {
594 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
597 .deviceID
= pdevice
->chipset_id
,
598 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
600 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
603 strcpy(pProperties
->deviceName
, pdevice
->name
);
604 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
607 void anv_GetPhysicalDeviceQueueFamilyProperties(
608 VkPhysicalDevice physicalDevice
,
610 VkQueueFamilyProperties
* pQueueFamilyProperties
)
612 if (pQueueFamilyProperties
== NULL
) {
617 assert(*pCount
>= 1);
619 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
620 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
621 VK_QUEUE_COMPUTE_BIT
|
622 VK_QUEUE_TRANSFER_BIT
,
624 .timestampValidBits
= 36, /* XXX: Real value here */
625 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
629 void anv_GetPhysicalDeviceMemoryProperties(
630 VkPhysicalDevice physicalDevice
,
631 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
633 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
634 VkDeviceSize heap_size
;
636 /* Reserve some wiggle room for the driver by exposing only 75% of the
637 * aperture to the heap.
639 heap_size
= 3 * physical_device
->aperture_size
/ 4;
641 if (physical_device
->info
.has_llc
) {
642 /* Big core GPUs share LLC with the CPU and thus one memory type can be
643 * both cached and coherent at the same time.
645 pMemoryProperties
->memoryTypeCount
= 1;
646 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
647 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
648 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
649 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
650 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
654 /* The spec requires that we expose a host-visible, coherent memory
655 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
656 * to give the application a choice between cached, but not coherent and
657 * coherent but uncached (WC though).
659 pMemoryProperties
->memoryTypeCount
= 2;
660 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
661 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
662 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
663 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
666 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
667 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
668 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
669 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
674 pMemoryProperties
->memoryHeapCount
= 1;
675 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
677 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
681 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
685 return anv_lookup_entrypoint(NULL
, pName
);
688 /* With version 1+ of the loader interface the ICD should expose
689 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
692 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
697 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
701 return anv_GetInstanceProcAddr(instance
, pName
);
704 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
708 ANV_FROM_HANDLE(anv_device
, device
, _device
);
709 return anv_lookup_entrypoint(&device
->info
, pName
);
713 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
715 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
716 queue
->device
= device
;
717 queue
->pool
= &device
->surface_state_pool
;
723 anv_queue_finish(struct anv_queue
*queue
)
727 static struct anv_state
728 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
730 struct anv_state state
;
732 state
= anv_state_pool_alloc(pool
, size
, align
);
733 memcpy(state
.map
, p
, size
);
735 if (!pool
->block_pool
->device
->info
.has_llc
)
736 anv_state_clflush(state
);
741 struct gen8_border_color
{
746 /* Pad out to 64 bytes */
751 anv_device_init_border_colors(struct anv_device
*device
)
753 static const struct gen8_border_color border_colors
[] = {
754 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
755 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
756 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
757 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
758 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
759 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
762 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
763 sizeof(border_colors
), 64,
768 anv_device_submit_simple_batch(struct anv_device
*device
,
769 struct anv_batch
*batch
)
771 struct drm_i915_gem_execbuffer2 execbuf
;
772 struct drm_i915_gem_exec_object2 exec2_objects
[1];
773 struct anv_bo bo
, *exec_bos
[1];
774 VkResult result
= VK_SUCCESS
;
779 /* Kernel driver requires 8 byte aligned batch length */
780 size
= align_u32(batch
->next
- batch
->start
, 8);
781 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
782 if (result
!= VK_SUCCESS
)
785 memcpy(bo
.map
, batch
->start
, size
);
786 if (!device
->info
.has_llc
)
787 anv_clflush_range(bo
.map
, size
);
790 exec2_objects
[0].handle
= bo
.gem_handle
;
791 exec2_objects
[0].relocation_count
= 0;
792 exec2_objects
[0].relocs_ptr
= 0;
793 exec2_objects
[0].alignment
= 0;
794 exec2_objects
[0].offset
= bo
.offset
;
795 exec2_objects
[0].flags
= 0;
796 exec2_objects
[0].rsvd1
= 0;
797 exec2_objects
[0].rsvd2
= 0;
799 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
800 execbuf
.buffer_count
= 1;
801 execbuf
.batch_start_offset
= 0;
802 execbuf
.batch_len
= size
;
803 execbuf
.cliprects_ptr
= 0;
804 execbuf
.num_cliprects
= 0;
809 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
810 execbuf
.rsvd1
= device
->context_id
;
813 result
= anv_device_execbuf(device
, &execbuf
, exec_bos
);
814 if (result
!= VK_SUCCESS
)
818 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
820 /* We don't know the real error. */
821 result
= vk_errorf(VK_ERROR_DEVICE_LOST
, "execbuf2 failed: %m");
826 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
831 VkResult
anv_CreateDevice(
832 VkPhysicalDevice physicalDevice
,
833 const VkDeviceCreateInfo
* pCreateInfo
,
834 const VkAllocationCallbacks
* pAllocator
,
837 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
839 struct anv_device
*device
;
841 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
843 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
845 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
846 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
847 device_extensions
[j
].extensionName
) == 0) {
853 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
856 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
858 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
860 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
862 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
863 device
->instance
= physical_device
->instance
;
864 device
->chipset_id
= physical_device
->chipset_id
;
867 device
->alloc
= *pAllocator
;
869 device
->alloc
= physical_device
->instance
->alloc
;
871 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
872 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
873 if (device
->fd
== -1) {
874 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
878 device
->context_id
= anv_gem_create_context(device
);
879 if (device
->context_id
== -1) {
880 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
884 device
->info
= physical_device
->info
;
885 device
->isl_dev
= physical_device
->isl_dev
;
887 /* On Broadwell and later, we can use batch chaining to more efficiently
888 * implement growing command buffers. Prior to Haswell, the kernel
889 * command parser gets in the way and we have to fall back to growing
892 device
->can_chain_batches
= device
->info
.gen
>= 8;
894 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
895 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
897 pthread_mutex_init(&device
->mutex
, NULL
);
899 pthread_condattr_t condattr
;
900 pthread_condattr_init(&condattr
);
901 pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
);
902 pthread_cond_init(&device
->queue_submit
, NULL
);
903 pthread_condattr_destroy(&condattr
);
905 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
907 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
909 anv_state_pool_init(&device
->dynamic_state_pool
,
910 &device
->dynamic_state_block_pool
);
912 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
913 anv_state_pool_init(&device
->instruction_state_pool
,
914 &device
->instruction_block_pool
);
916 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
918 anv_state_pool_init(&device
->surface_state_pool
,
919 &device
->surface_state_block_pool
);
921 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
923 anv_scratch_pool_init(device
, &device
->scratch_pool
);
925 anv_queue_init(device
, &device
->queue
);
927 switch (device
->info
.gen
) {
929 if (!device
->info
.is_haswell
)
930 result
= gen7_init_device_state(device
);
932 result
= gen75_init_device_state(device
);
935 result
= gen8_init_device_state(device
);
938 result
= gen9_init_device_state(device
);
941 /* Shouldn't get here as we don't create physical devices for any other
943 unreachable("unhandled gen");
945 if (result
!= VK_SUCCESS
)
948 anv_device_init_blorp(device
);
950 anv_device_init_border_colors(device
);
952 *pDevice
= anv_device_to_handle(device
);
959 vk_free(&device
->alloc
, device
);
964 void anv_DestroyDevice(
966 const VkAllocationCallbacks
* pAllocator
)
968 ANV_FROM_HANDLE(anv_device
, device
, _device
);
970 anv_queue_finish(&device
->queue
);
972 anv_device_finish_blorp(device
);
975 /* We only need to free these to prevent valgrind errors. The backing
976 * BO will go away in a couple of lines so we don't actually leak.
978 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
981 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
982 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
984 anv_bo_pool_finish(&device
->batch_bo_pool
);
985 anv_state_pool_finish(&device
->dynamic_state_pool
);
986 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
987 anv_state_pool_finish(&device
->instruction_state_pool
);
988 anv_block_pool_finish(&device
->instruction_block_pool
);
989 anv_state_pool_finish(&device
->surface_state_pool
);
990 anv_block_pool_finish(&device
->surface_state_block_pool
);
991 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
995 pthread_mutex_destroy(&device
->mutex
);
997 vk_free(&device
->alloc
, device
);
1000 VkResult
anv_EnumerateInstanceExtensionProperties(
1001 const char* pLayerName
,
1002 uint32_t* pPropertyCount
,
1003 VkExtensionProperties
* pProperties
)
1005 if (pProperties
== NULL
) {
1006 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
1010 *pPropertyCount
= MIN2(*pPropertyCount
, ARRAY_SIZE(global_extensions
));
1011 typed_memcpy(pProperties
, global_extensions
, *pPropertyCount
);
1013 if (*pPropertyCount
< ARRAY_SIZE(global_extensions
))
1014 return VK_INCOMPLETE
;
1019 VkResult
anv_EnumerateDeviceExtensionProperties(
1020 VkPhysicalDevice physicalDevice
,
1021 const char* pLayerName
,
1022 uint32_t* pPropertyCount
,
1023 VkExtensionProperties
* pProperties
)
1025 if (pProperties
== NULL
) {
1026 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1030 *pPropertyCount
= MIN2(*pPropertyCount
, ARRAY_SIZE(device_extensions
));
1031 typed_memcpy(pProperties
, device_extensions
, *pPropertyCount
);
1033 if (*pPropertyCount
< ARRAY_SIZE(device_extensions
))
1034 return VK_INCOMPLETE
;
1039 VkResult
anv_EnumerateInstanceLayerProperties(
1040 uint32_t* pPropertyCount
,
1041 VkLayerProperties
* pProperties
)
1043 if (pProperties
== NULL
) {
1044 *pPropertyCount
= 0;
1048 /* None supported at this time */
1049 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1052 VkResult
anv_EnumerateDeviceLayerProperties(
1053 VkPhysicalDevice physicalDevice
,
1054 uint32_t* pPropertyCount
,
1055 VkLayerProperties
* pProperties
)
1057 if (pProperties
== NULL
) {
1058 *pPropertyCount
= 0;
1062 /* None supported at this time */
1063 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1066 void anv_GetDeviceQueue(
1068 uint32_t queueNodeIndex
,
1069 uint32_t queueIndex
,
1072 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1074 assert(queueIndex
== 0);
1076 *pQueue
= anv_queue_to_handle(&device
->queue
);
1080 anv_device_execbuf(struct anv_device
*device
,
1081 struct drm_i915_gem_execbuffer2
*execbuf
,
1082 struct anv_bo
**execbuf_bos
)
1084 int ret
= anv_gem_execbuffer(device
, execbuf
);
1086 /* We don't know the real error. */
1087 return vk_errorf(VK_ERROR_DEVICE_LOST
, "execbuf2 failed: %m");
1090 struct drm_i915_gem_exec_object2
*objects
=
1091 (void *)(uintptr_t)execbuf
->buffers_ptr
;
1092 for (uint32_t k
= 0; k
< execbuf
->buffer_count
; k
++)
1093 execbuf_bos
[k
]->offset
= objects
[k
].offset
;
1098 VkResult
anv_QueueSubmit(
1100 uint32_t submitCount
,
1101 const VkSubmitInfo
* pSubmits
,
1104 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1105 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1106 struct anv_device
*device
= queue
->device
;
1107 VkResult result
= VK_SUCCESS
;
1109 /* We lock around QueueSubmit for three main reasons:
1111 * 1) When a block pool is resized, we create a new gem handle with a
1112 * different size and, in the case of surface states, possibly a
1113 * different center offset but we re-use the same anv_bo struct when
1114 * we do so. If this happens in the middle of setting up an execbuf,
1115 * we could end up with our list of BOs out of sync with our list of
1118 * 2) The algorithm we use for building the list of unique buffers isn't
1119 * thread-safe. While the client is supposed to syncronize around
1120 * QueueSubmit, this would be extremely difficult to debug if it ever
1121 * came up in the wild due to a broken app. It's better to play it
1122 * safe and just lock around QueueSubmit.
1124 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1125 * userspace. Due to the fact that the surface state buffer is shared
1126 * between batches, we can't afford to have that happen from multiple
1127 * threads at the same time. Even though the user is supposed to
1128 * ensure this doesn't happen, we play it safe as in (2) above.
1130 * Since the only other things that ever take the device lock such as block
1131 * pool resize only rarely happen, this will almost never be contended so
1132 * taking a lock isn't really an expensive operation in this case.
1134 pthread_mutex_lock(&device
->mutex
);
1136 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1137 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1138 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1139 pSubmits
[i
].pCommandBuffers
[j
]);
1140 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1142 result
= anv_cmd_buffer_execbuf(device
, cmd_buffer
);
1143 if (result
!= VK_SUCCESS
)
1149 struct anv_bo
*fence_bo
= &fence
->bo
;
1150 result
= anv_device_execbuf(device
, &fence
->execbuf
, &fence_bo
);
1151 if (result
!= VK_SUCCESS
)
1154 /* Update the fence and wake up any waiters */
1155 assert(fence
->state
== ANV_FENCE_STATE_RESET
);
1156 fence
->state
= ANV_FENCE_STATE_SUBMITTED
;
1157 pthread_cond_broadcast(&device
->queue_submit
);
1161 pthread_mutex_unlock(&device
->mutex
);
1166 VkResult
anv_QueueWaitIdle(
1169 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1171 return anv_DeviceWaitIdle(anv_device_to_handle(queue
->device
));
1174 VkResult
anv_DeviceWaitIdle(
1177 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1178 struct anv_batch batch
;
1181 batch
.start
= batch
.next
= cmds
;
1182 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1184 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1185 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1187 return anv_device_submit_simple_batch(device
, &batch
);
1191 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1193 uint32_t gem_handle
= anv_gem_create(device
, size
);
1195 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1197 anv_bo_init(bo
, gem_handle
, size
);
1202 VkResult
anv_AllocateMemory(
1204 const VkMemoryAllocateInfo
* pAllocateInfo
,
1205 const VkAllocationCallbacks
* pAllocator
,
1206 VkDeviceMemory
* pMem
)
1208 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1209 struct anv_device_memory
*mem
;
1212 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1214 if (pAllocateInfo
->allocationSize
== 0) {
1215 /* Apparently, this is allowed */
1216 *pMem
= VK_NULL_HANDLE
;
1220 /* We support exactly one memory heap. */
1221 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1222 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1224 /* FINISHME: Fail if allocation request exceeds heap size. */
1226 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1227 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1229 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1231 /* The kernel is going to give us whole pages anyway */
1232 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1234 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1235 if (result
!= VK_SUCCESS
)
1238 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1243 *pMem
= anv_device_memory_to_handle(mem
);
1248 vk_free2(&device
->alloc
, pAllocator
, mem
);
1253 void anv_FreeMemory(
1255 VkDeviceMemory _mem
,
1256 const VkAllocationCallbacks
* pAllocator
)
1258 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1259 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1265 anv_UnmapMemory(_device
, _mem
);
1268 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1270 if (mem
->bo
.gem_handle
!= 0)
1271 anv_gem_close(device
, mem
->bo
.gem_handle
);
1273 vk_free2(&device
->alloc
, pAllocator
, mem
);
1276 VkResult
anv_MapMemory(
1278 VkDeviceMemory _memory
,
1279 VkDeviceSize offset
,
1281 VkMemoryMapFlags flags
,
1284 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1285 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1292 if (size
== VK_WHOLE_SIZE
)
1293 size
= mem
->bo
.size
- offset
;
1295 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1297 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1298 * assert(size != 0);
1299 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1300 * equal to the size of the memory minus offset
1303 assert(offset
+ size
<= mem
->bo
.size
);
1305 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1306 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1307 * at a time is valid. We could just mmap up front and return an offset
1308 * pointer here, but that may exhaust virtual memory on 32 bit
1311 uint32_t gem_flags
= 0;
1312 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1313 gem_flags
|= I915_MMAP_WC
;
1315 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1316 uint64_t map_offset
= offset
& ~4095ull;
1317 assert(offset
>= map_offset
);
1318 uint64_t map_size
= (offset
+ size
) - map_offset
;
1320 /* Let's map whole pages */
1321 map_size
= align_u64(map_size
, 4096);
1323 void *map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1324 map_offset
, map_size
, gem_flags
);
1325 if (map
== MAP_FAILED
)
1326 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
1329 mem
->map_size
= map_size
;
1331 *ppData
= mem
->map
+ (offset
- map_offset
);
1336 void anv_UnmapMemory(
1338 VkDeviceMemory _memory
)
1340 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1345 anv_gem_munmap(mem
->map
, mem
->map_size
);
1352 clflush_mapped_ranges(struct anv_device
*device
,
1354 const VkMappedMemoryRange
*ranges
)
1356 for (uint32_t i
= 0; i
< count
; i
++) {
1357 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1358 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1361 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1362 end
= mem
->map
+ mem
->map_size
;
1364 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1367 __builtin_ia32_clflush(p
);
1368 p
+= CACHELINE_SIZE
;
1373 VkResult
anv_FlushMappedMemoryRanges(
1375 uint32_t memoryRangeCount
,
1376 const VkMappedMemoryRange
* pMemoryRanges
)
1378 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1380 if (device
->info
.has_llc
)
1383 /* Make sure the writes we're flushing have landed. */
1384 __builtin_ia32_mfence();
1386 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1391 VkResult
anv_InvalidateMappedMemoryRanges(
1393 uint32_t memoryRangeCount
,
1394 const VkMappedMemoryRange
* pMemoryRanges
)
1396 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1398 if (device
->info
.has_llc
)
1401 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1403 /* Make sure no reads get moved up above the invalidate. */
1404 __builtin_ia32_mfence();
1409 void anv_GetBufferMemoryRequirements(
1412 VkMemoryRequirements
* pMemoryRequirements
)
1414 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1416 /* The Vulkan spec (git aaed022) says:
1418 * memoryTypeBits is a bitfield and contains one bit set for every
1419 * supported memory type for the resource. The bit `1<<i` is set if and
1420 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1421 * structure for the physical device is supported.
1423 * We support exactly one memory type.
1425 pMemoryRequirements
->memoryTypeBits
= 1;
1427 pMemoryRequirements
->size
= buffer
->size
;
1428 pMemoryRequirements
->alignment
= 16;
1431 void anv_GetImageMemoryRequirements(
1434 VkMemoryRequirements
* pMemoryRequirements
)
1436 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1438 /* The Vulkan spec (git aaed022) says:
1440 * memoryTypeBits is a bitfield and contains one bit set for every
1441 * supported memory type for the resource. The bit `1<<i` is set if and
1442 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1443 * structure for the physical device is supported.
1445 * We support exactly one memory type.
1447 pMemoryRequirements
->memoryTypeBits
= 1;
1449 pMemoryRequirements
->size
= image
->size
;
1450 pMemoryRequirements
->alignment
= image
->alignment
;
1453 void anv_GetImageSparseMemoryRequirements(
1456 uint32_t* pSparseMemoryRequirementCount
,
1457 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1462 void anv_GetDeviceMemoryCommitment(
1464 VkDeviceMemory memory
,
1465 VkDeviceSize
* pCommittedMemoryInBytes
)
1467 *pCommittedMemoryInBytes
= 0;
1470 VkResult
anv_BindBufferMemory(
1473 VkDeviceMemory _memory
,
1474 VkDeviceSize memoryOffset
)
1476 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1477 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1480 buffer
->bo
= &mem
->bo
;
1481 buffer
->offset
= memoryOffset
;
1490 VkResult
anv_QueueBindSparse(
1492 uint32_t bindInfoCount
,
1493 const VkBindSparseInfo
* pBindInfo
,
1496 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1499 VkResult
anv_CreateFence(
1501 const VkFenceCreateInfo
* pCreateInfo
,
1502 const VkAllocationCallbacks
* pAllocator
,
1505 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1506 struct anv_bo fence_bo
;
1507 struct anv_fence
*fence
;
1508 struct anv_batch batch
;
1511 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1513 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1514 if (result
!= VK_SUCCESS
)
1517 /* Fences are small. Just store the CPU data structure in the BO. */
1518 fence
= fence_bo
.map
;
1519 fence
->bo
= fence_bo
;
1521 /* Place the batch after the CPU data but on its own cache line. */
1522 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1523 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1524 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1525 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1526 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1528 if (!device
->info
.has_llc
) {
1529 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1530 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1531 __builtin_ia32_mfence();
1532 __builtin_ia32_clflush(batch
.start
);
1535 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1536 fence
->exec2_objects
[0].relocation_count
= 0;
1537 fence
->exec2_objects
[0].relocs_ptr
= 0;
1538 fence
->exec2_objects
[0].alignment
= 0;
1539 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1540 fence
->exec2_objects
[0].flags
= 0;
1541 fence
->exec2_objects
[0].rsvd1
= 0;
1542 fence
->exec2_objects
[0].rsvd2
= 0;
1544 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1545 fence
->execbuf
.buffer_count
= 1;
1546 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1547 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1548 fence
->execbuf
.cliprects_ptr
= 0;
1549 fence
->execbuf
.num_cliprects
= 0;
1550 fence
->execbuf
.DR1
= 0;
1551 fence
->execbuf
.DR4
= 0;
1553 fence
->execbuf
.flags
=
1554 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1555 fence
->execbuf
.rsvd1
= device
->context_id
;
1556 fence
->execbuf
.rsvd2
= 0;
1558 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
) {
1559 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1561 fence
->state
= ANV_FENCE_STATE_RESET
;
1564 *pFence
= anv_fence_to_handle(fence
);
1569 void anv_DestroyFence(
1572 const VkAllocationCallbacks
* pAllocator
)
1574 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1575 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1580 assert(fence
->bo
.map
== fence
);
1581 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1584 VkResult
anv_ResetFences(
1586 uint32_t fenceCount
,
1587 const VkFence
* pFences
)
1589 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1590 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1591 fence
->state
= ANV_FENCE_STATE_RESET
;
1597 VkResult
anv_GetFenceStatus(
1601 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1602 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1606 switch (fence
->state
) {
1607 case ANV_FENCE_STATE_RESET
:
1608 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1609 return VK_NOT_READY
;
1611 case ANV_FENCE_STATE_SIGNALED
:
1612 /* It's been signaled, return success */
1615 case ANV_FENCE_STATE_SUBMITTED
:
1616 /* It's been submitted to the GPU but we don't know if it's done yet. */
1617 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1619 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1622 return VK_NOT_READY
;
1625 unreachable("Invalid fence status");
1629 #define NSEC_PER_SEC 1000000000
1630 #define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)
1632 VkResult
anv_WaitForFences(
1634 uint32_t fenceCount
,
1635 const VkFence
* pFences
,
1639 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1642 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1643 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1644 * for a couple of kernel releases. Since there's no way to know
1645 * whether or not the kernel we're using is one of the broken ones, the
1646 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1647 * maximum timeout from 584 years to 292 years - likely not a big deal.
1649 int64_t timeout
= MIN2(_timeout
, INT64_MAX
);
1651 uint32_t pending_fences
= fenceCount
;
1652 while (pending_fences
) {
1654 bool signaled_fences
= false;
1655 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1656 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1657 switch (fence
->state
) {
1658 case ANV_FENCE_STATE_RESET
:
1659 /* This fence hasn't been submitted yet, we'll catch it the next
1660 * time around. Yes, this may mean we dead-loop but, short of
1661 * lots of locking and a condition variable, there's not much that
1662 * we can do about that.
1667 case ANV_FENCE_STATE_SIGNALED
:
1668 /* This fence is not pending. If waitAll isn't set, we can return
1669 * early. Otherwise, we have to keep going.
1675 case ANV_FENCE_STATE_SUBMITTED
:
1676 /* These are the fences we really care about. Go ahead and wait
1677 * on it until we hit a timeout.
1679 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &timeout
);
1680 if (ret
== -1 && errno
== ETIME
) {
1682 } else if (ret
== -1) {
1683 /* We don't know the real error. */
1684 return vk_errorf(VK_ERROR_DEVICE_LOST
, "gem wait failed: %m");
1686 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1687 signaled_fences
= true;
1695 if (pending_fences
&& !signaled_fences
) {
1696 /* If we've hit this then someone decided to vkWaitForFences before
1697 * they've actually submitted any of them to a queue. This is a
1698 * fairly pessimal case, so it's ok to lock here and use a standard
1699 * pthreads condition variable.
1701 pthread_mutex_lock(&device
->mutex
);
1703 /* It's possible that some of the fences have changed state since the
1704 * last time we checked. Now that we have the lock, check for
1705 * pending fences again and don't wait if it's changed.
1707 uint32_t now_pending_fences
= 0;
1708 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1709 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1710 if (fence
->state
== ANV_FENCE_STATE_RESET
)
1711 now_pending_fences
++;
1713 assert(now_pending_fences
<= pending_fences
);
1715 if (now_pending_fences
== pending_fences
) {
1716 struct timespec before
;
1717 clock_gettime(CLOCK_MONOTONIC
, &before
);
1719 uint32_t abs_nsec
= before
.tv_nsec
+ timeout
% NSEC_PER_SEC
;
1720 uint64_t abs_sec
= before
.tv_sec
+ (abs_nsec
/ NSEC_PER_SEC
) +
1721 (timeout
/ NSEC_PER_SEC
);
1722 abs_nsec
%= NSEC_PER_SEC
;
1724 /* Avoid roll-over in tv_sec on 32-bit systems if the user
1725 * provided timeout is UINT64_MAX
1727 struct timespec abstime
;
1728 abstime
.tv_nsec
= abs_nsec
;
1729 abstime
.tv_sec
= MIN2(abs_sec
, INT_TYPE_MAX(abstime
.tv_sec
));
1731 ret
= pthread_cond_timedwait(&device
->queue_submit
,
1732 &device
->mutex
, &abstime
);
1733 assert(ret
!= EINVAL
);
1735 struct timespec after
;
1736 clock_gettime(CLOCK_MONOTONIC
, &after
);
1737 uint64_t time_elapsed
=
1738 ((uint64_t)after
.tv_sec
* NSEC_PER_SEC
+ after
.tv_nsec
) -
1739 ((uint64_t)before
.tv_sec
* NSEC_PER_SEC
+ before
.tv_nsec
);
1741 if (time_elapsed
>= timeout
) {
1742 pthread_mutex_unlock(&device
->mutex
);
1746 timeout
-= time_elapsed
;
1749 pthread_mutex_unlock(&device
->mutex
);
1756 // Queue semaphore functions
1758 VkResult
anv_CreateSemaphore(
1760 const VkSemaphoreCreateInfo
* pCreateInfo
,
1761 const VkAllocationCallbacks
* pAllocator
,
1762 VkSemaphore
* pSemaphore
)
1764 /* The DRM execbuffer ioctl always execute in-oder, even between different
1765 * rings. As such, there's nothing to do for the user space semaphore.
1768 *pSemaphore
= (VkSemaphore
)1;
1773 void anv_DestroySemaphore(
1775 VkSemaphore semaphore
,
1776 const VkAllocationCallbacks
* pAllocator
)
1782 VkResult
anv_CreateEvent(
1784 const VkEventCreateInfo
* pCreateInfo
,
1785 const VkAllocationCallbacks
* pAllocator
,
1788 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1789 struct anv_state state
;
1790 struct anv_event
*event
;
1792 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1794 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1797 event
->state
= state
;
1798 event
->semaphore
= VK_EVENT_RESET
;
1800 if (!device
->info
.has_llc
) {
1801 /* Make sure the writes we're flushing have landed. */
1802 __builtin_ia32_mfence();
1803 __builtin_ia32_clflush(event
);
1806 *pEvent
= anv_event_to_handle(event
);
1811 void anv_DestroyEvent(
1814 const VkAllocationCallbacks
* pAllocator
)
1816 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1817 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1822 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1825 VkResult
anv_GetEventStatus(
1829 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1830 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1832 if (!device
->info
.has_llc
) {
1833 /* Invalidate read cache before reading event written by GPU. */
1834 __builtin_ia32_clflush(event
);
1835 __builtin_ia32_mfence();
1839 return event
->semaphore
;
1842 VkResult
anv_SetEvent(
1846 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1847 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1849 event
->semaphore
= VK_EVENT_SET
;
1851 if (!device
->info
.has_llc
) {
1852 /* Make sure the writes we're flushing have landed. */
1853 __builtin_ia32_mfence();
1854 __builtin_ia32_clflush(event
);
1860 VkResult
anv_ResetEvent(
1864 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1865 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1867 event
->semaphore
= VK_EVENT_RESET
;
1869 if (!device
->info
.has_llc
) {
1870 /* Make sure the writes we're flushing have landed. */
1871 __builtin_ia32_mfence();
1872 __builtin_ia32_clflush(event
);
1880 VkResult
anv_CreateBuffer(
1882 const VkBufferCreateInfo
* pCreateInfo
,
1883 const VkAllocationCallbacks
* pAllocator
,
1886 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1887 struct anv_buffer
*buffer
;
1889 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1891 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1892 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1894 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1896 buffer
->size
= pCreateInfo
->size
;
1897 buffer
->usage
= pCreateInfo
->usage
;
1901 *pBuffer
= anv_buffer_to_handle(buffer
);
1906 void anv_DestroyBuffer(
1909 const VkAllocationCallbacks
* pAllocator
)
1911 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1912 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1917 vk_free2(&device
->alloc
, pAllocator
, buffer
);
1921 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1922 enum isl_format format
,
1923 uint32_t offset
, uint32_t range
, uint32_t stride
)
1925 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1927 .mocs
= device
->default_mocs
,
1932 if (!device
->info
.has_llc
)
1933 anv_state_clflush(state
);
1936 void anv_DestroySampler(
1939 const VkAllocationCallbacks
* pAllocator
)
1941 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1942 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1947 vk_free2(&device
->alloc
, pAllocator
, sampler
);
1950 VkResult
anv_CreateFramebuffer(
1952 const VkFramebufferCreateInfo
* pCreateInfo
,
1953 const VkAllocationCallbacks
* pAllocator
,
1954 VkFramebuffer
* pFramebuffer
)
1956 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1957 struct anv_framebuffer
*framebuffer
;
1959 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1961 size_t size
= sizeof(*framebuffer
) +
1962 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1963 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1964 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1965 if (framebuffer
== NULL
)
1966 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1968 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1969 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1970 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1971 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1974 framebuffer
->width
= pCreateInfo
->width
;
1975 framebuffer
->height
= pCreateInfo
->height
;
1976 framebuffer
->layers
= pCreateInfo
->layers
;
1978 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1983 void anv_DestroyFramebuffer(
1986 const VkAllocationCallbacks
* pAllocator
)
1988 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1989 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1994 vk_free2(&device
->alloc
, pAllocator
, fb
);