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_device_get_cache_uuid(void *uuid
)
59 memset(uuid
, 0, VK_UUID_SIZE
);
60 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", ANV_TIMESTAMP
);
64 anv_physical_device_init(struct anv_physical_device
*device
,
65 struct anv_instance
*instance
,
71 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
73 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
75 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
76 device
->instance
= instance
;
78 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
79 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
81 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
82 if (!device
->chipset_id
) {
83 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
87 device
->name
= gen_get_device_name(device
->chipset_id
);
88 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
89 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
93 if (device
->info
.is_haswell
) {
94 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
95 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
96 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
97 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
98 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
99 } else if (device
->info
.gen
>= 8) {
100 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
101 * supported as anything */
103 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
104 "Vulkan not yet supported on %s", device
->name
);
108 device
->cmd_parser_version
= -1;
109 if (device
->info
.gen
== 7) {
110 device
->cmd_parser_version
=
111 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
112 if (device
->cmd_parser_version
== -1) {
113 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
114 "failed to get command parser version");
119 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
120 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
121 "failed to get aperture size: %m");
125 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
126 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
127 "kernel missing gem wait");
131 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
132 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
133 "kernel missing execbuf2");
137 if (!device
->info
.has_llc
&&
138 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
139 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
140 "kernel missing wc mmap");
144 anv_device_get_cache_uuid(device
->uuid
);
145 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
147 /* GENs prior to 8 do not support EU/Subslice info */
148 if (device
->info
.gen
>= 8) {
149 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
150 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
152 /* Without this information, we cannot get the right Braswell
153 * brandstrings, and we have to use conservative numbers for GPGPU on
154 * many platforms, but otherwise, things will just work.
156 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
157 fprintf(stderr
, "WARNING: Kernel 4.1 required to properly"
158 " query GPU properties.\n");
160 } else if (device
->info
.gen
== 7) {
161 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
164 if (device
->info
.is_cherryview
&&
165 device
->subslice_total
> 0 && device
->eu_total
> 0) {
166 /* Logical CS threads = EUs per subslice * 7 threads per EU */
167 uint32_t max_cs_threads
= device
->eu_total
/ device
->subslice_total
* 7;
169 /* Fuse configurations may give more threads than expected, never less. */
170 if (max_cs_threads
> device
->info
.max_cs_threads
)
171 device
->info
.max_cs_threads
= max_cs_threads
;
176 brw_process_intel_debug_variable();
178 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
179 if (device
->compiler
== NULL
) {
180 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
183 device
->compiler
->shader_debug_log
= compiler_debug_log
;
184 device
->compiler
->shader_perf_log
= compiler_perf_log
;
186 result
= anv_init_wsi(device
);
187 if (result
!= VK_SUCCESS
)
190 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
200 anv_physical_device_finish(struct anv_physical_device
*device
)
202 anv_finish_wsi(device
);
203 ralloc_free(device
->compiler
);
206 static const VkExtensionProperties global_extensions
[] = {
208 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
211 #ifdef VK_USE_PLATFORM_XCB_KHR
213 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
217 #ifdef VK_USE_PLATFORM_XLIB_KHR
219 .extensionName
= VK_KHR_XLIB_SURFACE_EXTENSION_NAME
,
223 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
225 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
231 static const VkExtensionProperties device_extensions
[] = {
233 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
239 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
240 VkSystemAllocationScope allocationScope
)
246 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
247 size_t align
, VkSystemAllocationScope allocationScope
)
249 return realloc(pOriginal
, size
);
253 default_free_func(void *pUserData
, void *pMemory
)
258 static const VkAllocationCallbacks default_alloc
= {
260 .pfnAllocation
= default_alloc_func
,
261 .pfnReallocation
= default_realloc_func
,
262 .pfnFree
= default_free_func
,
265 VkResult
anv_CreateInstance(
266 const VkInstanceCreateInfo
* pCreateInfo
,
267 const VkAllocationCallbacks
* pAllocator
,
268 VkInstance
* pInstance
)
270 struct anv_instance
*instance
;
272 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
274 uint32_t client_version
;
275 if (pCreateInfo
->pApplicationInfo
&&
276 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
277 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
279 client_version
= VK_MAKE_VERSION(1, 0, 0);
282 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
283 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
284 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
285 "Client requested version %d.%d.%d",
286 VK_VERSION_MAJOR(client_version
),
287 VK_VERSION_MINOR(client_version
),
288 VK_VERSION_PATCH(client_version
));
291 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
293 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
294 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
295 global_extensions
[j
].extensionName
) == 0) {
301 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
304 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
305 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
307 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
309 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
312 instance
->alloc
= *pAllocator
;
314 instance
->alloc
= default_alloc
;
316 instance
->apiVersion
= client_version
;
317 instance
->physicalDeviceCount
= -1;
321 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
323 *pInstance
= anv_instance_to_handle(instance
);
328 void anv_DestroyInstance(
329 VkInstance _instance
,
330 const VkAllocationCallbacks
* pAllocator
)
332 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
334 if (instance
->physicalDeviceCount
> 0) {
335 /* We support at most one physical device. */
336 assert(instance
->physicalDeviceCount
== 1);
337 anv_physical_device_finish(&instance
->physicalDevice
);
340 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
344 vk_free(&instance
->alloc
, instance
);
347 VkResult
anv_EnumeratePhysicalDevices(
348 VkInstance _instance
,
349 uint32_t* pPhysicalDeviceCount
,
350 VkPhysicalDevice
* pPhysicalDevices
)
352 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
355 if (instance
->physicalDeviceCount
< 0) {
357 for (unsigned i
= 0; i
< 8; i
++) {
358 snprintf(path
, sizeof(path
), "/dev/dri/renderD%d", 128 + i
);
359 result
= anv_physical_device_init(&instance
->physicalDevice
,
361 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
365 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
366 instance
->physicalDeviceCount
= 0;
367 } else if (result
== VK_SUCCESS
) {
368 instance
->physicalDeviceCount
= 1;
374 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
375 * otherwise it's an inout parameter.
377 * The Vulkan spec (git aaed022) says:
379 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
380 * that is initialized with the number of devices the application is
381 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
382 * an array of at least this many VkPhysicalDevice handles [...].
384 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
385 * overwrites the contents of the variable pointed to by
386 * pPhysicalDeviceCount with the number of physical devices in in the
387 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
388 * pPhysicalDeviceCount with the number of physical handles written to
391 if (!pPhysicalDevices
) {
392 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
393 } else if (*pPhysicalDeviceCount
>= 1) {
394 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
395 *pPhysicalDeviceCount
= 1;
396 } else if (*pPhysicalDeviceCount
< instance
->physicalDeviceCount
) {
397 return VK_INCOMPLETE
;
399 *pPhysicalDeviceCount
= 0;
405 void anv_GetPhysicalDeviceFeatures(
406 VkPhysicalDevice physicalDevice
,
407 VkPhysicalDeviceFeatures
* pFeatures
)
409 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
411 *pFeatures
= (VkPhysicalDeviceFeatures
) {
412 .robustBufferAccess
= true,
413 .fullDrawIndexUint32
= true,
414 .imageCubeArray
= false,
415 .independentBlend
= true,
416 .geometryShader
= true,
417 .tessellationShader
= false,
418 .sampleRateShading
= true,
419 .dualSrcBlend
= true,
421 .multiDrawIndirect
= false,
422 .drawIndirectFirstInstance
= false,
424 .depthBiasClamp
= false,
425 .fillModeNonSolid
= true,
426 .depthBounds
= false,
430 .multiViewport
= true,
431 .samplerAnisotropy
= true,
432 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
433 pdevice
->info
.is_baytrail
,
434 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
435 .textureCompressionBC
= true,
436 .occlusionQueryPrecise
= true,
437 .pipelineStatisticsQuery
= false,
438 .fragmentStoresAndAtomics
= true,
439 .shaderTessellationAndGeometryPointSize
= true,
440 .shaderImageGatherExtended
= false,
441 .shaderStorageImageExtendedFormats
= false,
442 .shaderStorageImageMultisample
= false,
443 .shaderUniformBufferArrayDynamicIndexing
= true,
444 .shaderSampledImageArrayDynamicIndexing
= true,
445 .shaderStorageBufferArrayDynamicIndexing
= true,
446 .shaderStorageImageArrayDynamicIndexing
= true,
447 .shaderStorageImageReadWithoutFormat
= false,
448 .shaderStorageImageWriteWithoutFormat
= true,
449 .shaderClipDistance
= true,
450 .shaderCullDistance
= true,
451 .shaderFloat64
= false,
452 .shaderInt64
= false,
453 .shaderInt16
= false,
455 .variableMultisampleRate
= false,
456 .inheritedQueries
= false,
459 /* We can't do image stores in vec4 shaders */
460 pFeatures
->vertexPipelineStoresAndAtomics
=
461 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
462 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
465 void anv_GetPhysicalDeviceProperties(
466 VkPhysicalDevice physicalDevice
,
467 VkPhysicalDeviceProperties
* pProperties
)
469 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
470 const struct gen_device_info
*devinfo
= &pdevice
->info
;
472 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
474 /* See assertions made when programming the buffer surface state. */
475 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
476 (1ul << 30) : (1ul << 27);
478 VkSampleCountFlags sample_counts
=
479 isl_device_get_sample_counts(&pdevice
->isl_dev
);
481 VkPhysicalDeviceLimits limits
= {
482 .maxImageDimension1D
= (1 << 14),
483 .maxImageDimension2D
= (1 << 14),
484 .maxImageDimension3D
= (1 << 11),
485 .maxImageDimensionCube
= (1 << 14),
486 .maxImageArrayLayers
= (1 << 11),
487 .maxTexelBufferElements
= 128 * 1024 * 1024,
488 .maxUniformBufferRange
= (1ul << 27),
489 .maxStorageBufferRange
= max_raw_buffer_sz
,
490 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
491 .maxMemoryAllocationCount
= UINT32_MAX
,
492 .maxSamplerAllocationCount
= 64 * 1024,
493 .bufferImageGranularity
= 64, /* A cache line */
494 .sparseAddressSpaceSize
= 0,
495 .maxBoundDescriptorSets
= MAX_SETS
,
496 .maxPerStageDescriptorSamplers
= 64,
497 .maxPerStageDescriptorUniformBuffers
= 64,
498 .maxPerStageDescriptorStorageBuffers
= 64,
499 .maxPerStageDescriptorSampledImages
= 64,
500 .maxPerStageDescriptorStorageImages
= 64,
501 .maxPerStageDescriptorInputAttachments
= 64,
502 .maxPerStageResources
= 128,
503 .maxDescriptorSetSamplers
= 256,
504 .maxDescriptorSetUniformBuffers
= 256,
505 .maxDescriptorSetUniformBuffersDynamic
= 256,
506 .maxDescriptorSetStorageBuffers
= 256,
507 .maxDescriptorSetStorageBuffersDynamic
= 256,
508 .maxDescriptorSetSampledImages
= 256,
509 .maxDescriptorSetStorageImages
= 256,
510 .maxDescriptorSetInputAttachments
= 256,
511 .maxVertexInputAttributes
= 32,
512 .maxVertexInputBindings
= 32,
513 .maxVertexInputAttributeOffset
= 2047,
514 .maxVertexInputBindingStride
= 2048,
515 .maxVertexOutputComponents
= 128,
516 .maxTessellationGenerationLevel
= 0,
517 .maxTessellationPatchSize
= 0,
518 .maxTessellationControlPerVertexInputComponents
= 0,
519 .maxTessellationControlPerVertexOutputComponents
= 0,
520 .maxTessellationControlPerPatchOutputComponents
= 0,
521 .maxTessellationControlTotalOutputComponents
= 0,
522 .maxTessellationEvaluationInputComponents
= 0,
523 .maxTessellationEvaluationOutputComponents
= 0,
524 .maxGeometryShaderInvocations
= 32,
525 .maxGeometryInputComponents
= 64,
526 .maxGeometryOutputComponents
= 128,
527 .maxGeometryOutputVertices
= 256,
528 .maxGeometryTotalOutputComponents
= 1024,
529 .maxFragmentInputComponents
= 128,
530 .maxFragmentOutputAttachments
= 8,
531 .maxFragmentDualSrcAttachments
= 2,
532 .maxFragmentCombinedOutputResources
= 8,
533 .maxComputeSharedMemorySize
= 32768,
534 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
535 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
536 .maxComputeWorkGroupSize
= {
537 16 * devinfo
->max_cs_threads
,
538 16 * devinfo
->max_cs_threads
,
539 16 * devinfo
->max_cs_threads
,
541 .subPixelPrecisionBits
= 4 /* FIXME */,
542 .subTexelPrecisionBits
= 4 /* FIXME */,
543 .mipmapPrecisionBits
= 4 /* FIXME */,
544 .maxDrawIndexedIndexValue
= UINT32_MAX
,
545 .maxDrawIndirectCount
= UINT32_MAX
,
546 .maxSamplerLodBias
= 16,
547 .maxSamplerAnisotropy
= 16,
548 .maxViewports
= MAX_VIEWPORTS
,
549 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
550 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
551 .viewportSubPixelBits
= 13, /* We take a float? */
552 .minMemoryMapAlignment
= 4096, /* A page */
553 .minTexelBufferOffsetAlignment
= 1,
554 .minUniformBufferOffsetAlignment
= 1,
555 .minStorageBufferOffsetAlignment
= 1,
556 .minTexelOffset
= -8,
558 .minTexelGatherOffset
= -8,
559 .maxTexelGatherOffset
= 7,
560 .minInterpolationOffset
= -0.5,
561 .maxInterpolationOffset
= 0.4375,
562 .subPixelInterpolationOffsetBits
= 4,
563 .maxFramebufferWidth
= (1 << 14),
564 .maxFramebufferHeight
= (1 << 14),
565 .maxFramebufferLayers
= (1 << 10),
566 .framebufferColorSampleCounts
= sample_counts
,
567 .framebufferDepthSampleCounts
= sample_counts
,
568 .framebufferStencilSampleCounts
= sample_counts
,
569 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
570 .maxColorAttachments
= MAX_RTS
,
571 .sampledImageColorSampleCounts
= sample_counts
,
572 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
573 .sampledImageDepthSampleCounts
= sample_counts
,
574 .sampledImageStencilSampleCounts
= sample_counts
,
575 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
576 .maxSampleMaskWords
= 1,
577 .timestampComputeAndGraphics
= false,
578 .timestampPeriod
= time_stamp_base
,
579 .maxClipDistances
= 8,
580 .maxCullDistances
= 8,
581 .maxCombinedClipAndCullDistances
= 8,
582 .discreteQueuePriorities
= 1,
583 .pointSizeRange
= { 0.125, 255.875 },
584 .lineWidthRange
= { 0.0, 7.9921875 },
585 .pointSizeGranularity
= (1.0 / 8.0),
586 .lineWidthGranularity
= (1.0 / 128.0),
587 .strictLines
= false, /* FINISHME */
588 .standardSampleLocations
= true,
589 .optimalBufferCopyOffsetAlignment
= 128,
590 .optimalBufferCopyRowPitchAlignment
= 128,
591 .nonCoherentAtomSize
= 64,
594 *pProperties
= (VkPhysicalDeviceProperties
) {
595 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
598 .deviceID
= pdevice
->chipset_id
,
599 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
601 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
604 strcpy(pProperties
->deviceName
, pdevice
->name
);
605 memcpy(pProperties
->pipelineCacheUUID
, pdevice
->uuid
, VK_UUID_SIZE
);
608 void anv_GetPhysicalDeviceQueueFamilyProperties(
609 VkPhysicalDevice physicalDevice
,
611 VkQueueFamilyProperties
* pQueueFamilyProperties
)
613 if (pQueueFamilyProperties
== NULL
) {
618 assert(*pCount
>= 1);
620 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
621 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
622 VK_QUEUE_COMPUTE_BIT
|
623 VK_QUEUE_TRANSFER_BIT
,
625 .timestampValidBits
= 36, /* XXX: Real value here */
626 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
630 void anv_GetPhysicalDeviceMemoryProperties(
631 VkPhysicalDevice physicalDevice
,
632 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
634 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
635 VkDeviceSize heap_size
;
637 /* Reserve some wiggle room for the driver by exposing only 75% of the
638 * aperture to the heap.
640 heap_size
= 3 * physical_device
->aperture_size
/ 4;
642 if (physical_device
->info
.has_llc
) {
643 /* Big core GPUs share LLC with the CPU and thus one memory type can be
644 * both cached and coherent at the same time.
646 pMemoryProperties
->memoryTypeCount
= 1;
647 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
648 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
649 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
650 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
651 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
655 /* The spec requires that we expose a host-visible, coherent memory
656 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
657 * to give the application a choice between cached, but not coherent and
658 * coherent but uncached (WC though).
660 pMemoryProperties
->memoryTypeCount
= 2;
661 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
662 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
663 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
664 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
667 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
668 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
669 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
670 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
675 pMemoryProperties
->memoryHeapCount
= 1;
676 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
678 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
682 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
686 return anv_lookup_entrypoint(NULL
, pName
);
689 /* With version 1+ of the loader interface the ICD should expose
690 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
693 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
698 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
702 return anv_GetInstanceProcAddr(instance
, pName
);
705 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
709 ANV_FROM_HANDLE(anv_device
, device
, _device
);
710 return anv_lookup_entrypoint(&device
->info
, pName
);
714 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
716 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
717 queue
->device
= device
;
718 queue
->pool
= &device
->surface_state_pool
;
724 anv_queue_finish(struct anv_queue
*queue
)
728 static struct anv_state
729 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
731 struct anv_state state
;
733 state
= anv_state_pool_alloc(pool
, size
, align
);
734 memcpy(state
.map
, p
, size
);
736 if (!pool
->block_pool
->device
->info
.has_llc
)
737 anv_state_clflush(state
);
742 struct gen8_border_color
{
747 /* Pad out to 64 bytes */
752 anv_device_init_border_colors(struct anv_device
*device
)
754 static const struct gen8_border_color border_colors
[] = {
755 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
756 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
757 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
758 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
759 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
760 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
763 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
764 sizeof(border_colors
), 64,
769 anv_device_submit_simple_batch(struct anv_device
*device
,
770 struct anv_batch
*batch
)
772 struct drm_i915_gem_execbuffer2 execbuf
;
773 struct drm_i915_gem_exec_object2 exec2_objects
[1];
774 struct anv_bo bo
, *exec_bos
[1];
775 VkResult result
= VK_SUCCESS
;
780 /* Kernel driver requires 8 byte aligned batch length */
781 size
= align_u32(batch
->next
- batch
->start
, 8);
782 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
783 if (result
!= VK_SUCCESS
)
786 memcpy(bo
.map
, batch
->start
, size
);
787 if (!device
->info
.has_llc
)
788 anv_clflush_range(bo
.map
, size
);
791 exec2_objects
[0].handle
= bo
.gem_handle
;
792 exec2_objects
[0].relocation_count
= 0;
793 exec2_objects
[0].relocs_ptr
= 0;
794 exec2_objects
[0].alignment
= 0;
795 exec2_objects
[0].offset
= bo
.offset
;
796 exec2_objects
[0].flags
= 0;
797 exec2_objects
[0].rsvd1
= 0;
798 exec2_objects
[0].rsvd2
= 0;
800 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
801 execbuf
.buffer_count
= 1;
802 execbuf
.batch_start_offset
= 0;
803 execbuf
.batch_len
= size
;
804 execbuf
.cliprects_ptr
= 0;
805 execbuf
.num_cliprects
= 0;
810 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
811 execbuf
.rsvd1
= device
->context_id
;
814 result
= anv_device_execbuf(device
, &execbuf
, exec_bos
);
815 if (result
!= VK_SUCCESS
)
819 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
821 /* We don't know the real error. */
822 result
= vk_errorf(VK_ERROR_DEVICE_LOST
, "execbuf2 failed: %m");
827 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
832 VkResult
anv_CreateDevice(
833 VkPhysicalDevice physicalDevice
,
834 const VkDeviceCreateInfo
* pCreateInfo
,
835 const VkAllocationCallbacks
* pAllocator
,
838 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
840 struct anv_device
*device
;
842 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
844 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
846 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
847 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
848 device_extensions
[j
].extensionName
) == 0) {
854 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
857 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
859 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
861 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
863 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
864 device
->instance
= physical_device
->instance
;
865 device
->chipset_id
= physical_device
->chipset_id
;
868 device
->alloc
= *pAllocator
;
870 device
->alloc
= physical_device
->instance
->alloc
;
872 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
873 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
874 if (device
->fd
== -1) {
875 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
879 device
->context_id
= anv_gem_create_context(device
);
880 if (device
->context_id
== -1) {
881 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
885 device
->info
= physical_device
->info
;
886 device
->isl_dev
= physical_device
->isl_dev
;
888 /* On Broadwell and later, we can use batch chaining to more efficiently
889 * implement growing command buffers. Prior to Haswell, the kernel
890 * command parser gets in the way and we have to fall back to growing
893 device
->can_chain_batches
= device
->info
.gen
>= 8;
895 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
896 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
898 pthread_mutex_init(&device
->mutex
, NULL
);
900 pthread_condattr_t condattr
;
901 pthread_condattr_init(&condattr
);
902 pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
);
903 pthread_cond_init(&device
->queue_submit
, NULL
);
904 pthread_condattr_destroy(&condattr
);
906 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
908 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
910 anv_state_pool_init(&device
->dynamic_state_pool
,
911 &device
->dynamic_state_block_pool
);
913 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
914 anv_state_pool_init(&device
->instruction_state_pool
,
915 &device
->instruction_block_pool
);
917 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
919 anv_state_pool_init(&device
->surface_state_pool
,
920 &device
->surface_state_block_pool
);
922 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
924 anv_scratch_pool_init(device
, &device
->scratch_pool
);
926 anv_queue_init(device
, &device
->queue
);
928 switch (device
->info
.gen
) {
930 if (!device
->info
.is_haswell
)
931 result
= gen7_init_device_state(device
);
933 result
= gen75_init_device_state(device
);
936 result
= gen8_init_device_state(device
);
939 result
= gen9_init_device_state(device
);
942 /* Shouldn't get here as we don't create physical devices for any other
944 unreachable("unhandled gen");
946 if (result
!= VK_SUCCESS
)
949 anv_device_init_blorp(device
);
951 anv_device_init_border_colors(device
);
953 *pDevice
= anv_device_to_handle(device
);
960 vk_free(&device
->alloc
, device
);
965 void anv_DestroyDevice(
967 const VkAllocationCallbacks
* pAllocator
)
969 ANV_FROM_HANDLE(anv_device
, device
, _device
);
971 anv_queue_finish(&device
->queue
);
973 anv_device_finish_blorp(device
);
976 /* We only need to free these to prevent valgrind errors. The backing
977 * BO will go away in a couple of lines so we don't actually leak.
979 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
982 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
983 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
985 anv_bo_pool_finish(&device
->batch_bo_pool
);
986 anv_state_pool_finish(&device
->dynamic_state_pool
);
987 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
988 anv_state_pool_finish(&device
->instruction_state_pool
);
989 anv_block_pool_finish(&device
->instruction_block_pool
);
990 anv_state_pool_finish(&device
->surface_state_pool
);
991 anv_block_pool_finish(&device
->surface_state_block_pool
);
992 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
996 pthread_mutex_destroy(&device
->mutex
);
998 vk_free(&device
->alloc
, device
);
1001 VkResult
anv_EnumerateInstanceExtensionProperties(
1002 const char* pLayerName
,
1003 uint32_t* pPropertyCount
,
1004 VkExtensionProperties
* pProperties
)
1006 if (pProperties
== NULL
) {
1007 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
1011 *pPropertyCount
= MIN2(*pPropertyCount
, ARRAY_SIZE(global_extensions
));
1012 typed_memcpy(pProperties
, global_extensions
, *pPropertyCount
);
1014 if (*pPropertyCount
< ARRAY_SIZE(global_extensions
))
1015 return VK_INCOMPLETE
;
1020 VkResult
anv_EnumerateDeviceExtensionProperties(
1021 VkPhysicalDevice physicalDevice
,
1022 const char* pLayerName
,
1023 uint32_t* pPropertyCount
,
1024 VkExtensionProperties
* pProperties
)
1026 if (pProperties
== NULL
) {
1027 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
1031 *pPropertyCount
= MIN2(*pPropertyCount
, ARRAY_SIZE(device_extensions
));
1032 typed_memcpy(pProperties
, device_extensions
, *pPropertyCount
);
1034 if (*pPropertyCount
< ARRAY_SIZE(device_extensions
))
1035 return VK_INCOMPLETE
;
1040 VkResult
anv_EnumerateInstanceLayerProperties(
1041 uint32_t* pPropertyCount
,
1042 VkLayerProperties
* pProperties
)
1044 if (pProperties
== NULL
) {
1045 *pPropertyCount
= 0;
1049 /* None supported at this time */
1050 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1053 VkResult
anv_EnumerateDeviceLayerProperties(
1054 VkPhysicalDevice physicalDevice
,
1055 uint32_t* pPropertyCount
,
1056 VkLayerProperties
* pProperties
)
1058 if (pProperties
== NULL
) {
1059 *pPropertyCount
= 0;
1063 /* None supported at this time */
1064 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1067 void anv_GetDeviceQueue(
1069 uint32_t queueNodeIndex
,
1070 uint32_t queueIndex
,
1073 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1075 assert(queueIndex
== 0);
1077 *pQueue
= anv_queue_to_handle(&device
->queue
);
1081 anv_device_execbuf(struct anv_device
*device
,
1082 struct drm_i915_gem_execbuffer2
*execbuf
,
1083 struct anv_bo
**execbuf_bos
)
1085 int ret
= anv_gem_execbuffer(device
, execbuf
);
1087 /* We don't know the real error. */
1088 return vk_errorf(VK_ERROR_DEVICE_LOST
, "execbuf2 failed: %m");
1091 struct drm_i915_gem_exec_object2
*objects
=
1092 (void *)(uintptr_t)execbuf
->buffers_ptr
;
1093 for (uint32_t k
= 0; k
< execbuf
->buffer_count
; k
++)
1094 execbuf_bos
[k
]->offset
= objects
[k
].offset
;
1099 VkResult
anv_QueueSubmit(
1101 uint32_t submitCount
,
1102 const VkSubmitInfo
* pSubmits
,
1105 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1106 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1107 struct anv_device
*device
= queue
->device
;
1108 VkResult result
= VK_SUCCESS
;
1110 /* We lock around QueueSubmit for three main reasons:
1112 * 1) When a block pool is resized, we create a new gem handle with a
1113 * different size and, in the case of surface states, possibly a
1114 * different center offset but we re-use the same anv_bo struct when
1115 * we do so. If this happens in the middle of setting up an execbuf,
1116 * we could end up with our list of BOs out of sync with our list of
1119 * 2) The algorithm we use for building the list of unique buffers isn't
1120 * thread-safe. While the client is supposed to syncronize around
1121 * QueueSubmit, this would be extremely difficult to debug if it ever
1122 * came up in the wild due to a broken app. It's better to play it
1123 * safe and just lock around QueueSubmit.
1125 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1126 * userspace. Due to the fact that the surface state buffer is shared
1127 * between batches, we can't afford to have that happen from multiple
1128 * threads at the same time. Even though the user is supposed to
1129 * ensure this doesn't happen, we play it safe as in (2) above.
1131 * Since the only other things that ever take the device lock such as block
1132 * pool resize only rarely happen, this will almost never be contended so
1133 * taking a lock isn't really an expensive operation in this case.
1135 pthread_mutex_lock(&device
->mutex
);
1137 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1138 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1139 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1140 pSubmits
[i
].pCommandBuffers
[j
]);
1141 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1143 result
= anv_cmd_buffer_execbuf(device
, cmd_buffer
);
1144 if (result
!= VK_SUCCESS
)
1150 struct anv_bo
*fence_bo
= &fence
->bo
;
1151 result
= anv_device_execbuf(device
, &fence
->execbuf
, &fence_bo
);
1152 if (result
!= VK_SUCCESS
)
1155 /* Update the fence and wake up any waiters */
1156 assert(fence
->state
== ANV_FENCE_STATE_RESET
);
1157 fence
->state
= ANV_FENCE_STATE_SUBMITTED
;
1158 pthread_cond_broadcast(&device
->queue_submit
);
1162 pthread_mutex_unlock(&device
->mutex
);
1167 VkResult
anv_QueueWaitIdle(
1170 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1172 return anv_DeviceWaitIdle(anv_device_to_handle(queue
->device
));
1175 VkResult
anv_DeviceWaitIdle(
1178 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1179 struct anv_batch batch
;
1182 batch
.start
= batch
.next
= cmds
;
1183 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1185 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1186 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1188 return anv_device_submit_simple_batch(device
, &batch
);
1192 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1194 uint32_t gem_handle
= anv_gem_create(device
, size
);
1196 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1198 anv_bo_init(bo
, gem_handle
, size
);
1203 VkResult
anv_AllocateMemory(
1205 const VkMemoryAllocateInfo
* pAllocateInfo
,
1206 const VkAllocationCallbacks
* pAllocator
,
1207 VkDeviceMemory
* pMem
)
1209 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1210 struct anv_device_memory
*mem
;
1213 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1215 if (pAllocateInfo
->allocationSize
== 0) {
1216 /* Apparently, this is allowed */
1217 *pMem
= VK_NULL_HANDLE
;
1221 /* We support exactly one memory heap. */
1222 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1223 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1225 /* FINISHME: Fail if allocation request exceeds heap size. */
1227 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1228 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1230 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1232 /* The kernel is going to give us whole pages anyway */
1233 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1235 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1236 if (result
!= VK_SUCCESS
)
1239 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1244 *pMem
= anv_device_memory_to_handle(mem
);
1249 vk_free2(&device
->alloc
, pAllocator
, mem
);
1254 void anv_FreeMemory(
1256 VkDeviceMemory _mem
,
1257 const VkAllocationCallbacks
* pAllocator
)
1259 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1260 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1266 anv_UnmapMemory(_device
, _mem
);
1269 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1271 if (mem
->bo
.gem_handle
!= 0)
1272 anv_gem_close(device
, mem
->bo
.gem_handle
);
1274 vk_free2(&device
->alloc
, pAllocator
, mem
);
1277 VkResult
anv_MapMemory(
1279 VkDeviceMemory _memory
,
1280 VkDeviceSize offset
,
1282 VkMemoryMapFlags flags
,
1285 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1286 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1293 if (size
== VK_WHOLE_SIZE
)
1294 size
= mem
->bo
.size
- offset
;
1296 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1298 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1299 * assert(size != 0);
1300 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1301 * equal to the size of the memory minus offset
1304 assert(offset
+ size
<= mem
->bo
.size
);
1306 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1307 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1308 * at a time is valid. We could just mmap up front and return an offset
1309 * pointer here, but that may exhaust virtual memory on 32 bit
1312 uint32_t gem_flags
= 0;
1313 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1314 gem_flags
|= I915_MMAP_WC
;
1316 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1317 uint64_t map_offset
= offset
& ~4095ull;
1318 assert(offset
>= map_offset
);
1319 uint64_t map_size
= (offset
+ size
) - map_offset
;
1321 /* Let's map whole pages */
1322 map_size
= align_u64(map_size
, 4096);
1324 void *map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1325 map_offset
, map_size
, gem_flags
);
1326 if (map
== MAP_FAILED
)
1327 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
1330 mem
->map_size
= map_size
;
1332 *ppData
= mem
->map
+ (offset
- map_offset
);
1337 void anv_UnmapMemory(
1339 VkDeviceMemory _memory
)
1341 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1346 anv_gem_munmap(mem
->map
, mem
->map_size
);
1353 clflush_mapped_ranges(struct anv_device
*device
,
1355 const VkMappedMemoryRange
*ranges
)
1357 for (uint32_t i
= 0; i
< count
; i
++) {
1358 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1359 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1362 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1363 end
= mem
->map
+ mem
->map_size
;
1365 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1368 __builtin_ia32_clflush(p
);
1369 p
+= CACHELINE_SIZE
;
1374 VkResult
anv_FlushMappedMemoryRanges(
1376 uint32_t memoryRangeCount
,
1377 const VkMappedMemoryRange
* pMemoryRanges
)
1379 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1381 if (device
->info
.has_llc
)
1384 /* Make sure the writes we're flushing have landed. */
1385 __builtin_ia32_mfence();
1387 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1392 VkResult
anv_InvalidateMappedMemoryRanges(
1394 uint32_t memoryRangeCount
,
1395 const VkMappedMemoryRange
* pMemoryRanges
)
1397 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1399 if (device
->info
.has_llc
)
1402 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1404 /* Make sure no reads get moved up above the invalidate. */
1405 __builtin_ia32_mfence();
1410 void anv_GetBufferMemoryRequirements(
1413 VkMemoryRequirements
* pMemoryRequirements
)
1415 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1417 /* The Vulkan spec (git aaed022) says:
1419 * memoryTypeBits is a bitfield and contains one bit set for every
1420 * supported memory type for the resource. The bit `1<<i` is set if and
1421 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1422 * structure for the physical device is supported.
1424 * We support exactly one memory type.
1426 pMemoryRequirements
->memoryTypeBits
= 1;
1428 pMemoryRequirements
->size
= buffer
->size
;
1429 pMemoryRequirements
->alignment
= 16;
1432 void anv_GetImageMemoryRequirements(
1435 VkMemoryRequirements
* pMemoryRequirements
)
1437 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1439 /* The Vulkan spec (git aaed022) says:
1441 * memoryTypeBits is a bitfield and contains one bit set for every
1442 * supported memory type for the resource. The bit `1<<i` is set if and
1443 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1444 * structure for the physical device is supported.
1446 * We support exactly one memory type.
1448 pMemoryRequirements
->memoryTypeBits
= 1;
1450 pMemoryRequirements
->size
= image
->size
;
1451 pMemoryRequirements
->alignment
= image
->alignment
;
1454 void anv_GetImageSparseMemoryRequirements(
1457 uint32_t* pSparseMemoryRequirementCount
,
1458 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1463 void anv_GetDeviceMemoryCommitment(
1465 VkDeviceMemory memory
,
1466 VkDeviceSize
* pCommittedMemoryInBytes
)
1468 *pCommittedMemoryInBytes
= 0;
1471 VkResult
anv_BindBufferMemory(
1474 VkDeviceMemory _memory
,
1475 VkDeviceSize memoryOffset
)
1477 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1478 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1481 buffer
->bo
= &mem
->bo
;
1482 buffer
->offset
= memoryOffset
;
1491 VkResult
anv_QueueBindSparse(
1493 uint32_t bindInfoCount
,
1494 const VkBindSparseInfo
* pBindInfo
,
1497 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1500 VkResult
anv_CreateFence(
1502 const VkFenceCreateInfo
* pCreateInfo
,
1503 const VkAllocationCallbacks
* pAllocator
,
1506 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1507 struct anv_bo fence_bo
;
1508 struct anv_fence
*fence
;
1509 struct anv_batch batch
;
1512 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1514 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1515 if (result
!= VK_SUCCESS
)
1518 /* Fences are small. Just store the CPU data structure in the BO. */
1519 fence
= fence_bo
.map
;
1520 fence
->bo
= fence_bo
;
1522 /* Place the batch after the CPU data but on its own cache line. */
1523 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1524 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1525 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1526 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1527 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1529 if (!device
->info
.has_llc
) {
1530 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1531 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1532 __builtin_ia32_mfence();
1533 __builtin_ia32_clflush(batch
.start
);
1536 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1537 fence
->exec2_objects
[0].relocation_count
= 0;
1538 fence
->exec2_objects
[0].relocs_ptr
= 0;
1539 fence
->exec2_objects
[0].alignment
= 0;
1540 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1541 fence
->exec2_objects
[0].flags
= 0;
1542 fence
->exec2_objects
[0].rsvd1
= 0;
1543 fence
->exec2_objects
[0].rsvd2
= 0;
1545 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1546 fence
->execbuf
.buffer_count
= 1;
1547 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1548 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1549 fence
->execbuf
.cliprects_ptr
= 0;
1550 fence
->execbuf
.num_cliprects
= 0;
1551 fence
->execbuf
.DR1
= 0;
1552 fence
->execbuf
.DR4
= 0;
1554 fence
->execbuf
.flags
=
1555 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1556 fence
->execbuf
.rsvd1
= device
->context_id
;
1557 fence
->execbuf
.rsvd2
= 0;
1559 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
) {
1560 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1562 fence
->state
= ANV_FENCE_STATE_RESET
;
1565 *pFence
= anv_fence_to_handle(fence
);
1570 void anv_DestroyFence(
1573 const VkAllocationCallbacks
* pAllocator
)
1575 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1576 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1581 assert(fence
->bo
.map
== fence
);
1582 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1585 VkResult
anv_ResetFences(
1587 uint32_t fenceCount
,
1588 const VkFence
* pFences
)
1590 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1591 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1592 fence
->state
= ANV_FENCE_STATE_RESET
;
1598 VkResult
anv_GetFenceStatus(
1602 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1603 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1607 switch (fence
->state
) {
1608 case ANV_FENCE_STATE_RESET
:
1609 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1610 return VK_NOT_READY
;
1612 case ANV_FENCE_STATE_SIGNALED
:
1613 /* It's been signaled, return success */
1616 case ANV_FENCE_STATE_SUBMITTED
:
1617 /* It's been submitted to the GPU but we don't know if it's done yet. */
1618 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1620 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1623 return VK_NOT_READY
;
1626 unreachable("Invalid fence status");
1630 #define NSEC_PER_SEC 1000000000
1631 #define INT_TYPE_MAX(type) ((1ull << (sizeof(type) * 8 - 1)) - 1)
1633 VkResult
anv_WaitForFences(
1635 uint32_t fenceCount
,
1636 const VkFence
* pFences
,
1640 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1643 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1644 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1645 * for a couple of kernel releases. Since there's no way to know
1646 * whether or not the kernel we're using is one of the broken ones, the
1647 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1648 * maximum timeout from 584 years to 292 years - likely not a big deal.
1650 int64_t timeout
= MIN2(_timeout
, INT64_MAX
);
1652 uint32_t pending_fences
= fenceCount
;
1653 while (pending_fences
) {
1655 bool signaled_fences
= false;
1656 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1657 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1658 switch (fence
->state
) {
1659 case ANV_FENCE_STATE_RESET
:
1660 /* This fence hasn't been submitted yet, we'll catch it the next
1661 * time around. Yes, this may mean we dead-loop but, short of
1662 * lots of locking and a condition variable, there's not much that
1663 * we can do about that.
1668 case ANV_FENCE_STATE_SIGNALED
:
1669 /* This fence is not pending. If waitAll isn't set, we can return
1670 * early. Otherwise, we have to keep going.
1676 case ANV_FENCE_STATE_SUBMITTED
:
1677 /* These are the fences we really care about. Go ahead and wait
1678 * on it until we hit a timeout.
1680 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &timeout
);
1681 if (ret
== -1 && errno
== ETIME
) {
1683 } else if (ret
== -1) {
1684 /* We don't know the real error. */
1685 return vk_errorf(VK_ERROR_DEVICE_LOST
, "gem wait failed: %m");
1687 fence
->state
= ANV_FENCE_STATE_SIGNALED
;
1688 signaled_fences
= true;
1696 if (pending_fences
&& !signaled_fences
) {
1697 /* If we've hit this then someone decided to vkWaitForFences before
1698 * they've actually submitted any of them to a queue. This is a
1699 * fairly pessimal case, so it's ok to lock here and use a standard
1700 * pthreads condition variable.
1702 pthread_mutex_lock(&device
->mutex
);
1704 /* It's possible that some of the fences have changed state since the
1705 * last time we checked. Now that we have the lock, check for
1706 * pending fences again and don't wait if it's changed.
1708 uint32_t now_pending_fences
= 0;
1709 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1710 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1711 if (fence
->state
== ANV_FENCE_STATE_RESET
)
1712 now_pending_fences
++;
1714 assert(now_pending_fences
<= pending_fences
);
1716 if (now_pending_fences
== pending_fences
) {
1717 struct timespec before
;
1718 clock_gettime(CLOCK_MONOTONIC
, &before
);
1720 uint32_t abs_nsec
= before
.tv_nsec
+ timeout
% NSEC_PER_SEC
;
1721 uint64_t abs_sec
= before
.tv_sec
+ (abs_nsec
/ NSEC_PER_SEC
) +
1722 (timeout
/ NSEC_PER_SEC
);
1723 abs_nsec
%= NSEC_PER_SEC
;
1725 /* Avoid roll-over in tv_sec on 32-bit systems if the user
1726 * provided timeout is UINT64_MAX
1728 struct timespec abstime
;
1729 abstime
.tv_nsec
= abs_nsec
;
1730 abstime
.tv_sec
= MIN2(abs_sec
, INT_TYPE_MAX(abstime
.tv_sec
));
1732 ret
= pthread_cond_timedwait(&device
->queue_submit
,
1733 &device
->mutex
, &abstime
);
1734 assert(ret
!= EINVAL
);
1736 struct timespec after
;
1737 clock_gettime(CLOCK_MONOTONIC
, &after
);
1738 uint64_t time_elapsed
=
1739 ((uint64_t)after
.tv_sec
* NSEC_PER_SEC
+ after
.tv_nsec
) -
1740 ((uint64_t)before
.tv_sec
* NSEC_PER_SEC
+ before
.tv_nsec
);
1742 if (time_elapsed
>= timeout
) {
1743 pthread_mutex_unlock(&device
->mutex
);
1747 timeout
-= time_elapsed
;
1750 pthread_mutex_unlock(&device
->mutex
);
1757 // Queue semaphore functions
1759 VkResult
anv_CreateSemaphore(
1761 const VkSemaphoreCreateInfo
* pCreateInfo
,
1762 const VkAllocationCallbacks
* pAllocator
,
1763 VkSemaphore
* pSemaphore
)
1765 /* The DRM execbuffer ioctl always execute in-oder, even between different
1766 * rings. As such, there's nothing to do for the user space semaphore.
1769 *pSemaphore
= (VkSemaphore
)1;
1774 void anv_DestroySemaphore(
1776 VkSemaphore semaphore
,
1777 const VkAllocationCallbacks
* pAllocator
)
1783 VkResult
anv_CreateEvent(
1785 const VkEventCreateInfo
* pCreateInfo
,
1786 const VkAllocationCallbacks
* pAllocator
,
1789 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1790 struct anv_state state
;
1791 struct anv_event
*event
;
1793 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1795 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1798 event
->state
= state
;
1799 event
->semaphore
= VK_EVENT_RESET
;
1801 if (!device
->info
.has_llc
) {
1802 /* Make sure the writes we're flushing have landed. */
1803 __builtin_ia32_mfence();
1804 __builtin_ia32_clflush(event
);
1807 *pEvent
= anv_event_to_handle(event
);
1812 void anv_DestroyEvent(
1815 const VkAllocationCallbacks
* pAllocator
)
1817 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1818 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1823 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1826 VkResult
anv_GetEventStatus(
1830 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1831 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1833 if (!device
->info
.has_llc
) {
1834 /* Invalidate read cache before reading event written by GPU. */
1835 __builtin_ia32_clflush(event
);
1836 __builtin_ia32_mfence();
1840 return event
->semaphore
;
1843 VkResult
anv_SetEvent(
1847 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1848 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1850 event
->semaphore
= VK_EVENT_SET
;
1852 if (!device
->info
.has_llc
) {
1853 /* Make sure the writes we're flushing have landed. */
1854 __builtin_ia32_mfence();
1855 __builtin_ia32_clflush(event
);
1861 VkResult
anv_ResetEvent(
1865 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1866 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1868 event
->semaphore
= VK_EVENT_RESET
;
1870 if (!device
->info
.has_llc
) {
1871 /* Make sure the writes we're flushing have landed. */
1872 __builtin_ia32_mfence();
1873 __builtin_ia32_clflush(event
);
1881 VkResult
anv_CreateBuffer(
1883 const VkBufferCreateInfo
* pCreateInfo
,
1884 const VkAllocationCallbacks
* pAllocator
,
1887 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1888 struct anv_buffer
*buffer
;
1890 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1892 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1893 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1895 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1897 buffer
->size
= pCreateInfo
->size
;
1898 buffer
->usage
= pCreateInfo
->usage
;
1902 *pBuffer
= anv_buffer_to_handle(buffer
);
1907 void anv_DestroyBuffer(
1910 const VkAllocationCallbacks
* pAllocator
)
1912 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1913 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1918 vk_free2(&device
->alloc
, pAllocator
, buffer
);
1922 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1923 enum isl_format format
,
1924 uint32_t offset
, uint32_t range
, uint32_t stride
)
1926 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1928 .mocs
= device
->default_mocs
,
1933 if (!device
->info
.has_llc
)
1934 anv_state_clflush(state
);
1937 void anv_DestroySampler(
1940 const VkAllocationCallbacks
* pAllocator
)
1942 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1943 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1948 vk_free2(&device
->alloc
, pAllocator
, sampler
);
1951 VkResult
anv_CreateFramebuffer(
1953 const VkFramebufferCreateInfo
* pCreateInfo
,
1954 const VkAllocationCallbacks
* pAllocator
,
1955 VkFramebuffer
* pFramebuffer
)
1957 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1958 struct anv_framebuffer
*framebuffer
;
1960 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1962 size_t size
= sizeof(*framebuffer
) +
1963 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1964 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1965 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1966 if (framebuffer
== NULL
)
1967 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1969 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1970 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1971 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1972 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1975 framebuffer
->width
= pCreateInfo
->width
;
1976 framebuffer
->height
= pCreateInfo
->height
;
1977 framebuffer
->layers
= pCreateInfo
->layers
;
1979 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1984 void anv_DestroyFramebuffer(
1987 const VkAllocationCallbacks
* pAllocator
)
1989 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1990 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1995 vk_free2(&device
->alloc
, pAllocator
, fb
);