2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33 #include "util/debug.h"
35 #include "genxml/gen7_pack.h"
37 struct anv_dispatch_table dtable
;
40 compiler_debug_log(void *data
, const char *fmt
, ...)
44 compiler_perf_log(void *data
, const char *fmt
, ...)
49 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
50 vfprintf(stderr
, fmt
, args
);
56 anv_physical_device_init(struct anv_physical_device
*device
,
57 struct anv_instance
*instance
,
63 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
65 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
66 "failed to open %s: %m", path
);
68 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
69 device
->instance
= instance
;
72 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
73 if (!device
->chipset_id
) {
74 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
75 "failed to get chipset id: %m");
79 device
->name
= brw_get_device_name(device
->chipset_id
);
80 device
->info
= brw_get_device_info(device
->chipset_id
);
82 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
83 "failed to get device info");
87 if (device
->info
->is_haswell
) {
88 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
89 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
90 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
91 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
92 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
93 } else if (device
->info
->gen
>= 8) {
94 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
95 * supported as anything */
97 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
98 "Vulkan not yet supported on %s", device
->name
);
102 device
->cmd_parser_version
= -1;
103 if (device
->info
->gen
== 7) {
104 device
->cmd_parser_version
=
105 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
106 if (device
->cmd_parser_version
== -1) {
107 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
108 "failed to get command parser version");
113 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
114 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
115 "failed to get aperture size: %m");
119 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
120 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
121 "kernel missing gem wait");
125 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
126 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
127 "kernel missing execbuf2");
131 if (!device
->info
->has_llc
&&
132 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
133 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
134 "kernel missing wc mmap");
138 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
142 brw_process_intel_debug_variable();
144 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
145 if (device
->compiler
== NULL
) {
146 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
149 device
->compiler
->shader_debug_log
= compiler_debug_log
;
150 device
->compiler
->shader_perf_log
= compiler_perf_log
;
152 anv_init_wsi(device
);
154 /* XXX: Actually detect bit6 swizzling */
155 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
165 anv_physical_device_finish(struct anv_physical_device
*device
)
167 anv_finish_wsi(device
);
168 ralloc_free(device
->compiler
);
171 static const VkExtensionProperties global_extensions
[] = {
173 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
176 #ifdef VK_USE_PLATFORM_XCB_KHR
178 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
182 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
184 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
190 static const VkExtensionProperties device_extensions
[] = {
192 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
198 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
199 VkSystemAllocationScope allocationScope
)
205 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
206 size_t align
, VkSystemAllocationScope allocationScope
)
208 return realloc(pOriginal
, size
);
212 default_free_func(void *pUserData
, void *pMemory
)
217 static const VkAllocationCallbacks default_alloc
= {
219 .pfnAllocation
= default_alloc_func
,
220 .pfnReallocation
= default_realloc_func
,
221 .pfnFree
= default_free_func
,
224 VkResult
anv_CreateInstance(
225 const VkInstanceCreateInfo
* pCreateInfo
,
226 const VkAllocationCallbacks
* pAllocator
,
227 VkInstance
* pInstance
)
229 struct anv_instance
*instance
;
231 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
233 uint32_t client_version
;
234 if (pCreateInfo
->pApplicationInfo
&&
235 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
236 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
238 client_version
= VK_MAKE_VERSION(1, 0, 0);
241 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
242 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
243 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
244 "Client requested version %d.%d.%d",
245 VK_VERSION_MAJOR(client_version
),
246 VK_VERSION_MINOR(client_version
),
247 VK_VERSION_PATCH(client_version
));
250 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
252 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
253 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
254 global_extensions
[j
].extensionName
) == 0) {
260 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
263 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
264 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
266 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
268 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
271 instance
->alloc
= *pAllocator
;
273 instance
->alloc
= default_alloc
;
275 instance
->apiVersion
= client_version
;
276 instance
->physicalDeviceCount
= -1;
280 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
282 *pInstance
= anv_instance_to_handle(instance
);
287 void anv_DestroyInstance(
288 VkInstance _instance
,
289 const VkAllocationCallbacks
* pAllocator
)
291 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
293 if (instance
->physicalDeviceCount
> 0) {
294 /* We support at most one physical device. */
295 assert(instance
->physicalDeviceCount
== 1);
296 anv_physical_device_finish(&instance
->physicalDevice
);
299 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
303 anv_free(&instance
->alloc
, instance
);
306 VkResult
anv_EnumeratePhysicalDevices(
307 VkInstance _instance
,
308 uint32_t* pPhysicalDeviceCount
,
309 VkPhysicalDevice
* pPhysicalDevices
)
311 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
314 if (instance
->physicalDeviceCount
< 0) {
315 result
= anv_physical_device_init(&instance
->physicalDevice
,
316 instance
, "/dev/dri/renderD128");
317 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
318 instance
->physicalDeviceCount
= 0;
319 } else if (result
== VK_SUCCESS
) {
320 instance
->physicalDeviceCount
= 1;
326 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
327 * otherwise it's an inout parameter.
329 * The Vulkan spec (git aaed022) says:
331 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
332 * that is initialized with the number of devices the application is
333 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
334 * an array of at least this many VkPhysicalDevice handles [...].
336 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
337 * overwrites the contents of the variable pointed to by
338 * pPhysicalDeviceCount with the number of physical devices in in the
339 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
340 * pPhysicalDeviceCount with the number of physical handles written to
343 if (!pPhysicalDevices
) {
344 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
345 } else if (*pPhysicalDeviceCount
>= 1) {
346 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
347 *pPhysicalDeviceCount
= 1;
349 *pPhysicalDeviceCount
= 0;
355 void anv_GetPhysicalDeviceFeatures(
356 VkPhysicalDevice physicalDevice
,
357 VkPhysicalDeviceFeatures
* pFeatures
)
359 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
361 *pFeatures
= (VkPhysicalDeviceFeatures
) {
362 .robustBufferAccess
= true,
363 .fullDrawIndexUint32
= true,
364 .imageCubeArray
= false,
365 .independentBlend
= pdevice
->info
->gen
>= 8,
366 .geometryShader
= true,
367 .tessellationShader
= false,
368 .sampleRateShading
= false,
369 .dualSrcBlend
= true,
371 .multiDrawIndirect
= false,
372 .drawIndirectFirstInstance
= false,
374 .depthBiasClamp
= false,
375 .fillModeNonSolid
= true,
376 .depthBounds
= false,
380 .multiViewport
= true,
381 .samplerAnisotropy
= false, /* FINISHME */
382 .textureCompressionETC2
= pdevice
->info
->gen
>= 8 ||
383 pdevice
->info
->is_baytrail
,
384 .textureCompressionASTC_LDR
= false, /* FINISHME */
385 .textureCompressionBC
= true,
386 .occlusionQueryPrecise
= true,
387 .pipelineStatisticsQuery
= false,
388 .fragmentStoresAndAtomics
= true,
389 .shaderTessellationAndGeometryPointSize
= true,
390 .shaderImageGatherExtended
= false,
391 .shaderStorageImageExtendedFormats
= false,
392 .shaderStorageImageMultisample
= false,
393 .shaderUniformBufferArrayDynamicIndexing
= true,
394 .shaderSampledImageArrayDynamicIndexing
= true,
395 .shaderStorageBufferArrayDynamicIndexing
= true,
396 .shaderStorageImageArrayDynamicIndexing
= true,
397 .shaderStorageImageReadWithoutFormat
= false,
398 .shaderStorageImageWriteWithoutFormat
= true,
399 .shaderClipDistance
= false,
400 .shaderCullDistance
= false,
401 .shaderFloat64
= false,
402 .shaderInt64
= false,
403 .shaderInt16
= false,
405 .variableMultisampleRate
= false,
406 .inheritedQueries
= false,
409 /* We can't do image stores in vec4 shaders */
410 pFeatures
->vertexPipelineStoresAndAtomics
=
411 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
412 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
416 anv_device_get_cache_uuid(void *uuid
)
418 memset(uuid
, 0, VK_UUID_SIZE
);
419 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", MESA_GIT_SHA1
+ 4);
422 void anv_GetPhysicalDeviceProperties(
423 VkPhysicalDevice physicalDevice
,
424 VkPhysicalDeviceProperties
* pProperties
)
426 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
427 const struct brw_device_info
*devinfo
= pdevice
->info
;
429 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
431 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
433 VkSampleCountFlags sample_counts
=
434 isl_device_get_sample_counts(&pdevice
->isl_dev
);
436 VkPhysicalDeviceLimits limits
= {
437 .maxImageDimension1D
= (1 << 14),
438 .maxImageDimension2D
= (1 << 14),
439 .maxImageDimension3D
= (1 << 11),
440 .maxImageDimensionCube
= (1 << 14),
441 .maxImageArrayLayers
= (1 << 11),
442 .maxTexelBufferElements
= 128 * 1024 * 1024,
443 .maxUniformBufferRange
= UINT32_MAX
,
444 .maxStorageBufferRange
= UINT32_MAX
,
445 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
446 .maxMemoryAllocationCount
= UINT32_MAX
,
447 .maxSamplerAllocationCount
= 64 * 1024,
448 .bufferImageGranularity
= 64, /* A cache line */
449 .sparseAddressSpaceSize
= 0,
450 .maxBoundDescriptorSets
= MAX_SETS
,
451 .maxPerStageDescriptorSamplers
= 64,
452 .maxPerStageDescriptorUniformBuffers
= 64,
453 .maxPerStageDescriptorStorageBuffers
= 64,
454 .maxPerStageDescriptorSampledImages
= 64,
455 .maxPerStageDescriptorStorageImages
= 64,
456 .maxPerStageDescriptorInputAttachments
= 64,
457 .maxPerStageResources
= 128,
458 .maxDescriptorSetSamplers
= 256,
459 .maxDescriptorSetUniformBuffers
= 256,
460 .maxDescriptorSetUniformBuffersDynamic
= 256,
461 .maxDescriptorSetStorageBuffers
= 256,
462 .maxDescriptorSetStorageBuffersDynamic
= 256,
463 .maxDescriptorSetSampledImages
= 256,
464 .maxDescriptorSetStorageImages
= 256,
465 .maxDescriptorSetInputAttachments
= 256,
466 .maxVertexInputAttributes
= 32,
467 .maxVertexInputBindings
= 32,
468 .maxVertexInputAttributeOffset
= 2047,
469 .maxVertexInputBindingStride
= 2048,
470 .maxVertexOutputComponents
= 128,
471 .maxTessellationGenerationLevel
= 0,
472 .maxTessellationPatchSize
= 0,
473 .maxTessellationControlPerVertexInputComponents
= 0,
474 .maxTessellationControlPerVertexOutputComponents
= 0,
475 .maxTessellationControlPerPatchOutputComponents
= 0,
476 .maxTessellationControlTotalOutputComponents
= 0,
477 .maxTessellationEvaluationInputComponents
= 0,
478 .maxTessellationEvaluationOutputComponents
= 0,
479 .maxGeometryShaderInvocations
= 32,
480 .maxGeometryInputComponents
= 64,
481 .maxGeometryOutputComponents
= 128,
482 .maxGeometryOutputVertices
= 256,
483 .maxGeometryTotalOutputComponents
= 1024,
484 .maxFragmentInputComponents
= 128,
485 .maxFragmentOutputAttachments
= 8,
486 .maxFragmentDualSrcAttachments
= 2,
487 .maxFragmentCombinedOutputResources
= 8,
488 .maxComputeSharedMemorySize
= 32768,
489 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
490 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
491 .maxComputeWorkGroupSize
= {
492 16 * devinfo
->max_cs_threads
,
493 16 * devinfo
->max_cs_threads
,
494 16 * devinfo
->max_cs_threads
,
496 .subPixelPrecisionBits
= 4 /* FIXME */,
497 .subTexelPrecisionBits
= 4 /* FIXME */,
498 .mipmapPrecisionBits
= 4 /* FIXME */,
499 .maxDrawIndexedIndexValue
= UINT32_MAX
,
500 .maxDrawIndirectCount
= UINT32_MAX
,
501 .maxSamplerLodBias
= 16,
502 .maxSamplerAnisotropy
= 16,
503 .maxViewports
= MAX_VIEWPORTS
,
504 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
505 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
506 .viewportSubPixelBits
= 13, /* We take a float? */
507 .minMemoryMapAlignment
= 4096, /* A page */
508 .minTexelBufferOffsetAlignment
= 1,
509 .minUniformBufferOffsetAlignment
= 1,
510 .minStorageBufferOffsetAlignment
= 1,
511 .minTexelOffset
= -8,
513 .minTexelGatherOffset
= -8,
514 .maxTexelGatherOffset
= 7,
515 .minInterpolationOffset
= 0, /* FIXME */
516 .maxInterpolationOffset
= 0, /* FIXME */
517 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
518 .maxFramebufferWidth
= (1 << 14),
519 .maxFramebufferHeight
= (1 << 14),
520 .maxFramebufferLayers
= (1 << 10),
521 .framebufferColorSampleCounts
= sample_counts
,
522 .framebufferDepthSampleCounts
= sample_counts
,
523 .framebufferStencilSampleCounts
= sample_counts
,
524 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
525 .maxColorAttachments
= MAX_RTS
,
526 .sampledImageColorSampleCounts
= sample_counts
,
527 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
528 .sampledImageDepthSampleCounts
= sample_counts
,
529 .sampledImageStencilSampleCounts
= sample_counts
,
530 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
531 .maxSampleMaskWords
= 1,
532 .timestampComputeAndGraphics
= false,
533 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
534 .maxClipDistances
= 0 /* FIXME */,
535 .maxCullDistances
= 0 /* FIXME */,
536 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
537 .discreteQueuePriorities
= 1,
538 .pointSizeRange
= { 0.125, 255.875 },
539 .lineWidthRange
= { 0.0, 7.9921875 },
540 .pointSizeGranularity
= (1.0 / 8.0),
541 .lineWidthGranularity
= (1.0 / 128.0),
542 .strictLines
= false, /* FINISHME */
543 .standardSampleLocations
= true,
544 .optimalBufferCopyOffsetAlignment
= 128,
545 .optimalBufferCopyRowPitchAlignment
= 128,
546 .nonCoherentAtomSize
= 64,
549 *pProperties
= (VkPhysicalDeviceProperties
) {
550 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
553 .deviceID
= pdevice
->chipset_id
,
554 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
556 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
559 strcpy(pProperties
->deviceName
, pdevice
->name
);
560 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
563 void anv_GetPhysicalDeviceQueueFamilyProperties(
564 VkPhysicalDevice physicalDevice
,
566 VkQueueFamilyProperties
* pQueueFamilyProperties
)
568 if (pQueueFamilyProperties
== NULL
) {
573 assert(*pCount
>= 1);
575 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
576 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
577 VK_QUEUE_COMPUTE_BIT
|
578 VK_QUEUE_TRANSFER_BIT
,
580 .timestampValidBits
= 36, /* XXX: Real value here */
581 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
585 void anv_GetPhysicalDeviceMemoryProperties(
586 VkPhysicalDevice physicalDevice
,
587 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
589 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
590 VkDeviceSize heap_size
;
592 /* Reserve some wiggle room for the driver by exposing only 75% of the
593 * aperture to the heap.
595 heap_size
= 3 * physical_device
->aperture_size
/ 4;
597 if (physical_device
->info
->has_llc
) {
598 /* Big core GPUs share LLC with the CPU and thus one memory type can be
599 * both cached and coherent at the same time.
601 pMemoryProperties
->memoryTypeCount
= 1;
602 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
603 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
604 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
605 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
606 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
610 /* The spec requires that we expose a host-visible, coherent memory
611 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
612 * to give the application a choice between cached, but not coherent and
613 * coherent but uncached (WC though).
615 pMemoryProperties
->memoryTypeCount
= 2;
616 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
617 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
618 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
619 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
622 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
623 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
624 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
625 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
630 pMemoryProperties
->memoryHeapCount
= 1;
631 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
633 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
637 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
641 return anv_lookup_entrypoint(pName
);
644 /* The loader wants us to expose a second GetInstanceProcAddr function
645 * to work around certain LD_PRELOAD issues seen in apps.
647 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
651 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
655 return anv_GetInstanceProcAddr(instance
, pName
);
658 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
662 return anv_lookup_entrypoint(pName
);
666 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
668 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
669 queue
->device
= device
;
670 queue
->pool
= &device
->surface_state_pool
;
676 anv_queue_finish(struct anv_queue
*queue
)
680 static struct anv_state
681 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
683 struct anv_state state
;
685 state
= anv_state_pool_alloc(pool
, size
, align
);
686 memcpy(state
.map
, p
, size
);
688 if (!pool
->block_pool
->device
->info
.has_llc
)
689 anv_state_clflush(state
);
694 struct gen8_border_color
{
699 /* Pad out to 64 bytes */
704 anv_device_init_border_colors(struct anv_device
*device
)
706 static const struct gen8_border_color border_colors
[] = {
707 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
708 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
709 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
710 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
711 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
712 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
715 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
716 sizeof(border_colors
), 64,
721 anv_device_submit_simple_batch(struct anv_device
*device
,
722 struct anv_batch
*batch
)
724 struct drm_i915_gem_execbuffer2 execbuf
;
725 struct drm_i915_gem_exec_object2 exec2_objects
[1];
727 VkResult result
= VK_SUCCESS
;
732 /* Kernel driver requires 8 byte aligned batch length */
733 size
= align_u32(batch
->next
- batch
->start
, 8);
734 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
735 if (result
!= VK_SUCCESS
)
738 memcpy(bo
.map
, batch
->start
, size
);
739 if (!device
->info
.has_llc
)
740 anv_clflush_range(bo
.map
, size
);
742 exec2_objects
[0].handle
= bo
.gem_handle
;
743 exec2_objects
[0].relocation_count
= 0;
744 exec2_objects
[0].relocs_ptr
= 0;
745 exec2_objects
[0].alignment
= 0;
746 exec2_objects
[0].offset
= bo
.offset
;
747 exec2_objects
[0].flags
= 0;
748 exec2_objects
[0].rsvd1
= 0;
749 exec2_objects
[0].rsvd2
= 0;
751 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
752 execbuf
.buffer_count
= 1;
753 execbuf
.batch_start_offset
= 0;
754 execbuf
.batch_len
= size
;
755 execbuf
.cliprects_ptr
= 0;
756 execbuf
.num_cliprects
= 0;
761 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
762 execbuf
.rsvd1
= device
->context_id
;
765 ret
= anv_gem_execbuffer(device
, &execbuf
);
767 /* We don't know the real error. */
768 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
773 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
775 /* We don't know the real error. */
776 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
781 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
786 VkResult
anv_CreateDevice(
787 VkPhysicalDevice physicalDevice
,
788 const VkDeviceCreateInfo
* pCreateInfo
,
789 const VkAllocationCallbacks
* pAllocator
,
792 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
794 struct anv_device
*device
;
796 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
798 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
800 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
801 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
802 device_extensions
[j
].extensionName
) == 0) {
808 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
811 anv_set_dispatch_devinfo(physical_device
->info
);
813 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
815 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
817 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
819 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
820 device
->instance
= physical_device
->instance
;
821 device
->chipset_id
= physical_device
->chipset_id
;
824 device
->alloc
= *pAllocator
;
826 device
->alloc
= physical_device
->instance
->alloc
;
828 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
829 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
830 if (device
->fd
== -1) {
831 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
835 device
->context_id
= anv_gem_create_context(device
);
836 if (device
->context_id
== -1) {
837 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
841 device
->info
= *physical_device
->info
;
842 device
->isl_dev
= physical_device
->isl_dev
;
844 /* On Broadwell and later, we can use batch chaining to more efficiently
845 * implement growing command buffers. Prior to Haswell, the kernel
846 * command parser gets in the way and we have to fall back to growing
849 device
->can_chain_batches
= device
->info
.gen
>= 8;
851 pthread_mutex_init(&device
->mutex
, NULL
);
853 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
855 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
857 anv_state_pool_init(&device
->dynamic_state_pool
,
858 &device
->dynamic_state_block_pool
);
860 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
861 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
863 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
865 anv_state_pool_init(&device
->surface_state_pool
,
866 &device
->surface_state_block_pool
);
868 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
870 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
872 anv_queue_init(device
, &device
->queue
);
874 switch (device
->info
.gen
) {
876 if (!device
->info
.is_haswell
)
877 result
= gen7_init_device_state(device
);
879 result
= gen75_init_device_state(device
);
882 result
= gen8_init_device_state(device
);
885 result
= gen9_init_device_state(device
);
888 /* Shouldn't get here as we don't create physical devices for any other
890 unreachable("unhandled gen");
892 if (result
!= VK_SUCCESS
)
895 result
= anv_device_init_meta(device
);
896 if (result
!= VK_SUCCESS
)
899 anv_device_init_border_colors(device
);
901 *pDevice
= anv_device_to_handle(device
);
908 anv_free(&device
->alloc
, device
);
913 void anv_DestroyDevice(
915 const VkAllocationCallbacks
* pAllocator
)
917 ANV_FROM_HANDLE(anv_device
, device
, _device
);
919 anv_queue_finish(&device
->queue
);
921 anv_device_finish_meta(device
);
924 /* We only need to free these to prevent valgrind errors. The backing
925 * BO will go away in a couple of lines so we don't actually leak.
927 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
930 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
931 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
933 anv_bo_pool_finish(&device
->batch_bo_pool
);
934 anv_state_pool_finish(&device
->dynamic_state_pool
);
935 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
936 anv_block_pool_finish(&device
->instruction_block_pool
);
937 anv_state_pool_finish(&device
->surface_state_pool
);
938 anv_block_pool_finish(&device
->surface_state_block_pool
);
939 anv_block_pool_finish(&device
->scratch_block_pool
);
943 pthread_mutex_destroy(&device
->mutex
);
945 anv_free(&device
->alloc
, device
);
948 VkResult
anv_EnumerateInstanceExtensionProperties(
949 const char* pLayerName
,
950 uint32_t* pPropertyCount
,
951 VkExtensionProperties
* pProperties
)
953 if (pProperties
== NULL
) {
954 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
958 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
960 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
961 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
966 VkResult
anv_EnumerateDeviceExtensionProperties(
967 VkPhysicalDevice physicalDevice
,
968 const char* pLayerName
,
969 uint32_t* pPropertyCount
,
970 VkExtensionProperties
* pProperties
)
972 if (pProperties
== NULL
) {
973 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
977 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
979 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
980 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
985 VkResult
anv_EnumerateInstanceLayerProperties(
986 uint32_t* pPropertyCount
,
987 VkLayerProperties
* pProperties
)
989 if (pProperties
== NULL
) {
994 /* None supported at this time */
995 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
998 VkResult
anv_EnumerateDeviceLayerProperties(
999 VkPhysicalDevice physicalDevice
,
1000 uint32_t* pPropertyCount
,
1001 VkLayerProperties
* pProperties
)
1003 if (pProperties
== NULL
) {
1004 *pPropertyCount
= 0;
1008 /* None supported at this time */
1009 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1012 void anv_GetDeviceQueue(
1014 uint32_t queueNodeIndex
,
1015 uint32_t queueIndex
,
1018 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1020 assert(queueIndex
== 0);
1022 *pQueue
= anv_queue_to_handle(&device
->queue
);
1025 VkResult
anv_QueueSubmit(
1027 uint32_t submitCount
,
1028 const VkSubmitInfo
* pSubmits
,
1031 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1032 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1033 struct anv_device
*device
= queue
->device
;
1036 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1037 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1038 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1039 pSubmits
[i
].pCommandBuffers
[j
]);
1040 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1042 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1044 /* We don't know the real error. */
1045 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1046 "execbuf2 failed: %m");
1049 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1050 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1055 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1057 /* We don't know the real error. */
1058 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1059 "execbuf2 failed: %m");
1066 VkResult
anv_QueueWaitIdle(
1069 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1071 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1074 VkResult
anv_DeviceWaitIdle(
1077 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1078 struct anv_batch batch
;
1081 batch
.start
= batch
.next
= cmds
;
1082 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1084 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1085 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1087 return anv_device_submit_simple_batch(device
, &batch
);
1091 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1093 bo
->gem_handle
= anv_gem_create(device
, size
);
1094 if (!bo
->gem_handle
)
1095 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1101 bo
->is_winsys_bo
= false;
1106 VkResult
anv_AllocateMemory(
1108 const VkMemoryAllocateInfo
* pAllocateInfo
,
1109 const VkAllocationCallbacks
* pAllocator
,
1110 VkDeviceMemory
* pMem
)
1112 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1113 struct anv_device_memory
*mem
;
1116 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1118 if (pAllocateInfo
->allocationSize
== 0) {
1119 /* Apparently, this is allowed */
1120 *pMem
= VK_NULL_HANDLE
;
1124 /* We support exactly one memory heap. */
1125 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1126 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1128 /* FINISHME: Fail if allocation request exceeds heap size. */
1130 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1131 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1133 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1135 /* The kernel is going to give us whole pages anyway */
1136 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1138 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1139 if (result
!= VK_SUCCESS
)
1142 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1144 *pMem
= anv_device_memory_to_handle(mem
);
1149 anv_free2(&device
->alloc
, pAllocator
, mem
);
1154 void anv_FreeMemory(
1156 VkDeviceMemory _mem
,
1157 const VkAllocationCallbacks
* pAllocator
)
1159 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1160 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1166 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1168 if (mem
->bo
.gem_handle
!= 0)
1169 anv_gem_close(device
, mem
->bo
.gem_handle
);
1171 anv_free2(&device
->alloc
, pAllocator
, mem
);
1174 VkResult
anv_MapMemory(
1176 VkDeviceMemory _memory
,
1177 VkDeviceSize offset
,
1179 VkMemoryMapFlags flags
,
1182 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1183 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1190 if (size
== VK_WHOLE_SIZE
)
1191 size
= mem
->bo
.size
- offset
;
1193 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1194 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1195 * at a time is valid. We could just mmap up front and return an offset
1196 * pointer here, but that may exhaust virtual memory on 32 bit
1199 uint32_t gem_flags
= 0;
1200 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1201 gem_flags
|= I915_MMAP_WC
;
1203 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1204 uint64_t map_offset
= offset
& ~4095ull;
1205 assert(offset
>= map_offset
);
1206 uint64_t map_size
= (offset
+ size
) - map_offset
;
1208 /* Let's map whole pages */
1209 map_size
= align_u64(map_size
, 4096);
1211 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1212 map_offset
, map_size
, gem_flags
);
1213 mem
->map_size
= map_size
;
1215 *ppData
= mem
->map
+ (offset
- map_offset
);
1220 void anv_UnmapMemory(
1222 VkDeviceMemory _memory
)
1224 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1229 anv_gem_munmap(mem
->map
, mem
->map_size
);
1233 clflush_mapped_ranges(struct anv_device
*device
,
1235 const VkMappedMemoryRange
*ranges
)
1237 for (uint32_t i
= 0; i
< count
; i
++) {
1238 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1239 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1242 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1243 end
= mem
->map
+ mem
->map_size
;
1245 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1248 __builtin_ia32_clflush(p
);
1249 p
+= CACHELINE_SIZE
;
1254 VkResult
anv_FlushMappedMemoryRanges(
1256 uint32_t memoryRangeCount
,
1257 const VkMappedMemoryRange
* pMemoryRanges
)
1259 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1261 if (device
->info
.has_llc
)
1264 /* Make sure the writes we're flushing have landed. */
1265 __builtin_ia32_mfence();
1267 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1272 VkResult
anv_InvalidateMappedMemoryRanges(
1274 uint32_t memoryRangeCount
,
1275 const VkMappedMemoryRange
* pMemoryRanges
)
1277 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1279 if (device
->info
.has_llc
)
1282 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1284 /* Make sure no reads get moved up above the invalidate. */
1285 __builtin_ia32_mfence();
1290 void anv_GetBufferMemoryRequirements(
1293 VkMemoryRequirements
* pMemoryRequirements
)
1295 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1297 /* The Vulkan spec (git aaed022) says:
1299 * memoryTypeBits is a bitfield and contains one bit set for every
1300 * supported memory type for the resource. The bit `1<<i` is set if and
1301 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1302 * structure for the physical device is supported.
1304 * We support exactly one memory type.
1306 pMemoryRequirements
->memoryTypeBits
= 1;
1308 pMemoryRequirements
->size
= buffer
->size
;
1309 pMemoryRequirements
->alignment
= 16;
1312 void anv_GetImageMemoryRequirements(
1315 VkMemoryRequirements
* pMemoryRequirements
)
1317 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1319 /* The Vulkan spec (git aaed022) says:
1321 * memoryTypeBits is a bitfield and contains one bit set for every
1322 * supported memory type for the resource. The bit `1<<i` is set if and
1323 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1324 * structure for the physical device is supported.
1326 * We support exactly one memory type.
1328 pMemoryRequirements
->memoryTypeBits
= 1;
1330 pMemoryRequirements
->size
= image
->size
;
1331 pMemoryRequirements
->alignment
= image
->alignment
;
1334 void anv_GetImageSparseMemoryRequirements(
1337 uint32_t* pSparseMemoryRequirementCount
,
1338 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1343 void anv_GetDeviceMemoryCommitment(
1345 VkDeviceMemory memory
,
1346 VkDeviceSize
* pCommittedMemoryInBytes
)
1348 *pCommittedMemoryInBytes
= 0;
1351 VkResult
anv_BindBufferMemory(
1354 VkDeviceMemory _memory
,
1355 VkDeviceSize memoryOffset
)
1357 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1358 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1361 buffer
->bo
= &mem
->bo
;
1362 buffer
->offset
= memoryOffset
;
1371 VkResult
anv_BindImageMemory(
1374 VkDeviceMemory _memory
,
1375 VkDeviceSize memoryOffset
)
1377 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1378 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1381 image
->bo
= &mem
->bo
;
1382 image
->offset
= memoryOffset
;
1391 VkResult
anv_QueueBindSparse(
1393 uint32_t bindInfoCount
,
1394 const VkBindSparseInfo
* pBindInfo
,
1397 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1400 VkResult
anv_CreateFence(
1402 const VkFenceCreateInfo
* pCreateInfo
,
1403 const VkAllocationCallbacks
* pAllocator
,
1406 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1407 struct anv_bo fence_bo
;
1408 struct anv_fence
*fence
;
1409 struct anv_batch batch
;
1412 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1414 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1415 if (result
!= VK_SUCCESS
)
1418 /* Fences are small. Just store the CPU data structure in the BO. */
1419 fence
= fence_bo
.map
;
1420 fence
->bo
= fence_bo
;
1422 /* Place the batch after the CPU data but on its own cache line. */
1423 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1424 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1425 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1426 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1427 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1429 if (!device
->info
.has_llc
) {
1430 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1431 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1432 __builtin_ia32_mfence();
1433 __builtin_ia32_clflush(batch
.start
);
1436 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1437 fence
->exec2_objects
[0].relocation_count
= 0;
1438 fence
->exec2_objects
[0].relocs_ptr
= 0;
1439 fence
->exec2_objects
[0].alignment
= 0;
1440 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1441 fence
->exec2_objects
[0].flags
= 0;
1442 fence
->exec2_objects
[0].rsvd1
= 0;
1443 fence
->exec2_objects
[0].rsvd2
= 0;
1445 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1446 fence
->execbuf
.buffer_count
= 1;
1447 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1448 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1449 fence
->execbuf
.cliprects_ptr
= 0;
1450 fence
->execbuf
.num_cliprects
= 0;
1451 fence
->execbuf
.DR1
= 0;
1452 fence
->execbuf
.DR4
= 0;
1454 fence
->execbuf
.flags
=
1455 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1456 fence
->execbuf
.rsvd1
= device
->context_id
;
1457 fence
->execbuf
.rsvd2
= 0;
1459 fence
->ready
= false;
1461 *pFence
= anv_fence_to_handle(fence
);
1466 void anv_DestroyFence(
1469 const VkAllocationCallbacks
* pAllocator
)
1471 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1472 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1474 assert(fence
->bo
.map
== fence
);
1475 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1478 VkResult
anv_ResetFences(
1480 uint32_t fenceCount
,
1481 const VkFence
* pFences
)
1483 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1484 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1485 fence
->ready
= false;
1491 VkResult
anv_GetFenceStatus(
1495 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1496 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1503 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1505 fence
->ready
= true;
1509 return VK_NOT_READY
;
1512 VkResult
anv_WaitForFences(
1514 uint32_t fenceCount
,
1515 const VkFence
* pFences
,
1519 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1521 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1522 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1523 * for a couple of kernel releases. Since there's no way to know
1524 * whether or not the kernel we're using is one of the broken ones, the
1525 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1526 * maximum timeout from 584 years to 292 years - likely not a big deal.
1528 if (timeout
> INT64_MAX
)
1529 timeout
= INT64_MAX
;
1531 int64_t t
= timeout
;
1533 /* FIXME: handle !waitAll */
1535 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1536 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1537 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1538 if (ret
== -1 && errno
== ETIME
) {
1540 } else if (ret
== -1) {
1541 /* We don't know the real error. */
1542 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1543 "gem wait failed: %m");
1550 // Queue semaphore functions
1552 VkResult
anv_CreateSemaphore(
1554 const VkSemaphoreCreateInfo
* pCreateInfo
,
1555 const VkAllocationCallbacks
* pAllocator
,
1556 VkSemaphore
* pSemaphore
)
1558 /* The DRM execbuffer ioctl always execute in-oder, even between different
1559 * rings. As such, there's nothing to do for the user space semaphore.
1562 *pSemaphore
= (VkSemaphore
)1;
1567 void anv_DestroySemaphore(
1569 VkSemaphore semaphore
,
1570 const VkAllocationCallbacks
* pAllocator
)
1576 VkResult
anv_CreateEvent(
1578 const VkEventCreateInfo
* pCreateInfo
,
1579 const VkAllocationCallbacks
* pAllocator
,
1582 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1583 struct anv_state state
;
1584 struct anv_event
*event
;
1586 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1588 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1591 event
->state
= state
;
1592 event
->semaphore
= VK_EVENT_RESET
;
1594 if (!device
->info
.has_llc
) {
1595 /* Make sure the writes we're flushing have landed. */
1596 __builtin_ia32_mfence();
1597 __builtin_ia32_clflush(event
);
1600 *pEvent
= anv_event_to_handle(event
);
1605 void anv_DestroyEvent(
1608 const VkAllocationCallbacks
* pAllocator
)
1610 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1611 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1613 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1616 VkResult
anv_GetEventStatus(
1620 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1621 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1623 if (!device
->info
.has_llc
) {
1624 /* Invalidate read cache before reading event written by GPU. */
1625 __builtin_ia32_clflush(event
);
1626 __builtin_ia32_mfence();
1630 return event
->semaphore
;
1633 VkResult
anv_SetEvent(
1637 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1638 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1640 event
->semaphore
= VK_EVENT_SET
;
1642 if (!device
->info
.has_llc
) {
1643 /* Make sure the writes we're flushing have landed. */
1644 __builtin_ia32_mfence();
1645 __builtin_ia32_clflush(event
);
1651 VkResult
anv_ResetEvent(
1655 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1656 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1658 event
->semaphore
= VK_EVENT_RESET
;
1660 if (!device
->info
.has_llc
) {
1661 /* Make sure the writes we're flushing have landed. */
1662 __builtin_ia32_mfence();
1663 __builtin_ia32_clflush(event
);
1671 VkResult
anv_CreateBuffer(
1673 const VkBufferCreateInfo
* pCreateInfo
,
1674 const VkAllocationCallbacks
* pAllocator
,
1677 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1678 struct anv_buffer
*buffer
;
1680 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1682 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1683 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1685 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1687 buffer
->size
= pCreateInfo
->size
;
1688 buffer
->usage
= pCreateInfo
->usage
;
1692 *pBuffer
= anv_buffer_to_handle(buffer
);
1697 void anv_DestroyBuffer(
1700 const VkAllocationCallbacks
* pAllocator
)
1702 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1703 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1705 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1709 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1710 enum isl_format format
,
1711 uint32_t offset
, uint32_t range
, uint32_t stride
)
1713 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1715 .mocs
= device
->default_mocs
,
1720 if (!device
->info
.has_llc
)
1721 anv_state_clflush(state
);
1724 void anv_DestroySampler(
1727 const VkAllocationCallbacks
* pAllocator
)
1729 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1730 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1732 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1735 VkResult
anv_CreateFramebuffer(
1737 const VkFramebufferCreateInfo
* pCreateInfo
,
1738 const VkAllocationCallbacks
* pAllocator
,
1739 VkFramebuffer
* pFramebuffer
)
1741 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1742 struct anv_framebuffer
*framebuffer
;
1744 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1746 size_t size
= sizeof(*framebuffer
) +
1747 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1748 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1749 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1750 if (framebuffer
== NULL
)
1751 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1753 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1754 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1755 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1756 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1759 framebuffer
->width
= pCreateInfo
->width
;
1760 framebuffer
->height
= pCreateInfo
->height
;
1761 framebuffer
->layers
= pCreateInfo
->layers
;
1763 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1768 void anv_DestroyFramebuffer(
1771 const VkAllocationCallbacks
* pAllocator
)
1773 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1774 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1776 anv_free2(&device
->alloc
, pAllocator
, fb
);
1779 void vkCmdDbgMarkerBegin(
1780 VkCommandBuffer commandBuffer
,
1781 const char* pMarker
)
1782 __attribute__ ((visibility ("default")));
1784 void vkCmdDbgMarkerEnd(
1785 VkCommandBuffer commandBuffer
)
1786 __attribute__ ((visibility ("default")));
1788 void vkCmdDbgMarkerBegin(
1789 VkCommandBuffer commandBuffer
,
1790 const char* pMarker
)
1794 void vkCmdDbgMarkerEnd(
1795 VkCommandBuffer commandBuffer
)