2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
31 #include "mesa/main/git_sha1.h"
32 #include "util/strtod.h"
33 #include "util/debug.h"
35 #include "genxml/gen7_pack.h"
37 struct anv_dispatch_table dtable
;
40 compiler_debug_log(void *data
, const char *fmt
, ...)
44 compiler_perf_log(void *data
, const char *fmt
, ...)
49 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
50 vfprintf(stderr
, fmt
, args
);
56 anv_physical_device_init(struct anv_physical_device
*device
,
57 struct anv_instance
*instance
,
63 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
65 return vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
66 "failed to open %s: %m", path
);
68 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
69 device
->instance
= instance
;
72 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
73 if (!device
->chipset_id
) {
74 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
75 "failed to get chipset id: %m");
79 device
->name
= brw_get_device_name(device
->chipset_id
);
80 device
->info
= brw_get_device_info(device
->chipset_id
);
82 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
83 "failed to get device info");
87 if (device
->info
->is_haswell
) {
88 fprintf(stderr
, "WARNING: Haswell Vulkan support is incomplete\n");
89 } else if (device
->info
->gen
== 7 && !device
->info
->is_baytrail
) {
90 fprintf(stderr
, "WARNING: Ivy Bridge Vulkan support is incomplete\n");
91 } else if (device
->info
->gen
== 7 && device
->info
->is_baytrail
) {
92 fprintf(stderr
, "WARNING: Bay Trail Vulkan support is incomplete\n");
93 } else if (device
->info
->gen
>= 8) {
94 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake is as fully
95 * supported as anything */
97 result
= vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
98 "Vulkan not yet supported on %s", device
->name
);
102 device
->cmd_parser_version
= -1;
103 if (device
->info
->gen
== 7) {
104 device
->cmd_parser_version
=
105 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
106 if (device
->cmd_parser_version
== -1) {
107 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
108 "failed to get command parser version");
113 if (anv_gem_get_aperture(fd
, &device
->aperture_size
) == -1) {
114 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
115 "failed to get aperture size: %m");
119 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
120 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
121 "kernel missing gem wait");
125 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
126 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
127 "kernel missing execbuf2");
131 if (!device
->info
->has_llc
&&
132 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
133 result
= vk_errorf(VK_ERROR_INITIALIZATION_FAILED
,
134 "kernel missing wc mmap");
138 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
142 brw_process_intel_debug_variable();
144 device
->compiler
= brw_compiler_create(NULL
, device
->info
);
145 if (device
->compiler
== NULL
) {
146 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
149 device
->compiler
->shader_debug_log
= compiler_debug_log
;
150 device
->compiler
->shader_perf_log
= compiler_perf_log
;
152 /* XXX: Actually detect bit6 swizzling */
153 isl_device_init(&device
->isl_dev
, device
->info
, swizzled
);
163 anv_physical_device_finish(struct anv_physical_device
*device
)
165 ralloc_free(device
->compiler
);
168 static const VkExtensionProperties global_extensions
[] = {
170 .extensionName
= VK_KHR_SURFACE_EXTENSION_NAME
,
174 .extensionName
= VK_KHR_XCB_SURFACE_EXTENSION_NAME
,
177 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
179 .extensionName
= VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
,
185 static const VkExtensionProperties device_extensions
[] = {
187 .extensionName
= VK_KHR_SWAPCHAIN_EXTENSION_NAME
,
193 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
194 VkSystemAllocationScope allocationScope
)
200 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
201 size_t align
, VkSystemAllocationScope allocationScope
)
203 return realloc(pOriginal
, size
);
207 default_free_func(void *pUserData
, void *pMemory
)
212 static const VkAllocationCallbacks default_alloc
= {
214 .pfnAllocation
= default_alloc_func
,
215 .pfnReallocation
= default_realloc_func
,
216 .pfnFree
= default_free_func
,
219 VkResult
anv_CreateInstance(
220 const VkInstanceCreateInfo
* pCreateInfo
,
221 const VkAllocationCallbacks
* pAllocator
,
222 VkInstance
* pInstance
)
224 struct anv_instance
*instance
;
226 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
228 uint32_t client_version
;
229 if (pCreateInfo
->pApplicationInfo
&&
230 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
231 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
233 client_version
= VK_MAKE_VERSION(1, 0, 0);
236 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
237 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
238 return vk_errorf(VK_ERROR_INCOMPATIBLE_DRIVER
,
239 "Client requested version %d.%d.%d",
240 VK_VERSION_MAJOR(client_version
),
241 VK_VERSION_MINOR(client_version
),
242 VK_VERSION_PATCH(client_version
));
245 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
247 for (uint32_t j
= 0; j
< ARRAY_SIZE(global_extensions
); j
++) {
248 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
249 global_extensions
[j
].extensionName
) == 0) {
255 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
258 instance
= anv_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
259 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
261 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
263 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
266 instance
->alloc
= *pAllocator
;
268 instance
->alloc
= default_alloc
;
270 instance
->apiVersion
= client_version
;
271 instance
->physicalDeviceCount
= -1;
273 memset(instance
->wsi
, 0, sizeof(instance
->wsi
));
277 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
279 anv_init_wsi(instance
);
281 *pInstance
= anv_instance_to_handle(instance
);
286 void anv_DestroyInstance(
287 VkInstance _instance
,
288 const VkAllocationCallbacks
* pAllocator
)
290 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
292 if (instance
->physicalDeviceCount
> 0) {
293 /* We support at most one physical device. */
294 assert(instance
->physicalDeviceCount
== 1);
295 anv_physical_device_finish(&instance
->physicalDevice
);
298 anv_finish_wsi(instance
);
300 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
304 anv_free(&instance
->alloc
, instance
);
307 VkResult
anv_EnumeratePhysicalDevices(
308 VkInstance _instance
,
309 uint32_t* pPhysicalDeviceCount
,
310 VkPhysicalDevice
* pPhysicalDevices
)
312 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
315 if (instance
->physicalDeviceCount
< 0) {
316 result
= anv_physical_device_init(&instance
->physicalDevice
,
317 instance
, "/dev/dri/renderD128");
318 if (result
== VK_ERROR_INCOMPATIBLE_DRIVER
) {
319 instance
->physicalDeviceCount
= 0;
320 } else if (result
== VK_SUCCESS
) {
321 instance
->physicalDeviceCount
= 1;
327 /* pPhysicalDeviceCount is an out parameter if pPhysicalDevices is NULL;
328 * otherwise it's an inout parameter.
330 * The Vulkan spec (git aaed022) says:
332 * pPhysicalDeviceCount is a pointer to an unsigned integer variable
333 * that is initialized with the number of devices the application is
334 * prepared to receive handles to. pname:pPhysicalDevices is pointer to
335 * an array of at least this many VkPhysicalDevice handles [...].
337 * Upon success, if pPhysicalDevices is NULL, vkEnumeratePhysicalDevices
338 * overwrites the contents of the variable pointed to by
339 * pPhysicalDeviceCount with the number of physical devices in in the
340 * instance; otherwise, vkEnumeratePhysicalDevices overwrites
341 * pPhysicalDeviceCount with the number of physical handles written to
344 if (!pPhysicalDevices
) {
345 *pPhysicalDeviceCount
= instance
->physicalDeviceCount
;
346 } else if (*pPhysicalDeviceCount
>= 1) {
347 pPhysicalDevices
[0] = anv_physical_device_to_handle(&instance
->physicalDevice
);
348 *pPhysicalDeviceCount
= 1;
350 *pPhysicalDeviceCount
= 0;
356 void anv_GetPhysicalDeviceFeatures(
357 VkPhysicalDevice physicalDevice
,
358 VkPhysicalDeviceFeatures
* pFeatures
)
360 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
362 *pFeatures
= (VkPhysicalDeviceFeatures
) {
363 .robustBufferAccess
= true,
364 .fullDrawIndexUint32
= true,
365 .imageCubeArray
= false,
366 .independentBlend
= pdevice
->info
->gen
>= 8,
367 .geometryShader
= true,
368 .tessellationShader
= false,
369 .sampleRateShading
= false,
370 .dualSrcBlend
= true,
372 .multiDrawIndirect
= false,
373 .drawIndirectFirstInstance
= false,
375 .depthBiasClamp
= false,
376 .fillModeNonSolid
= true,
377 .depthBounds
= false,
381 .multiViewport
= true,
382 .samplerAnisotropy
= false, /* FINISHME */
383 .textureCompressionETC2
= pdevice
->info
->gen
>= 8 ||
384 pdevice
->info
->is_baytrail
,
385 .textureCompressionASTC_LDR
= false, /* FINISHME */
386 .textureCompressionBC
= true,
387 .occlusionQueryPrecise
= true,
388 .pipelineStatisticsQuery
= false,
389 .fragmentStoresAndAtomics
= true,
390 .shaderTessellationAndGeometryPointSize
= true,
391 .shaderImageGatherExtended
= true,
392 .shaderStorageImageExtendedFormats
= false,
393 .shaderStorageImageMultisample
= false,
394 .shaderUniformBufferArrayDynamicIndexing
= true,
395 .shaderSampledImageArrayDynamicIndexing
= true,
396 .shaderStorageBufferArrayDynamicIndexing
= true,
397 .shaderStorageImageArrayDynamicIndexing
= true,
398 .shaderStorageImageReadWithoutFormat
= false,
399 .shaderStorageImageWriteWithoutFormat
= true,
400 .shaderClipDistance
= false,
401 .shaderCullDistance
= false,
402 .shaderFloat64
= false,
403 .shaderInt64
= false,
404 .shaderInt16
= false,
406 .variableMultisampleRate
= false,
407 .inheritedQueries
= false,
410 /* We can't do image stores in vec4 shaders */
411 pFeatures
->vertexPipelineStoresAndAtomics
=
412 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
413 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
417 anv_device_get_cache_uuid(void *uuid
)
419 memset(uuid
, 0, VK_UUID_SIZE
);
420 snprintf(uuid
, VK_UUID_SIZE
, "anv-%s", MESA_GIT_SHA1
+ 4);
423 void anv_GetPhysicalDeviceProperties(
424 VkPhysicalDevice physicalDevice
,
425 VkPhysicalDeviceProperties
* pProperties
)
427 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
428 const struct brw_device_info
*devinfo
= pdevice
->info
;
430 anv_finishme("Get correct values for VkPhysicalDeviceLimits");
432 const float time_stamp_base
= devinfo
->gen
>= 9 ? 83.333 : 80.0;
434 VkSampleCountFlags sample_counts
=
435 isl_device_get_sample_counts(&pdevice
->isl_dev
);
437 VkPhysicalDeviceLimits limits
= {
438 .maxImageDimension1D
= (1 << 14),
439 .maxImageDimension2D
= (1 << 14),
440 .maxImageDimension3D
= (1 << 11),
441 .maxImageDimensionCube
= (1 << 14),
442 .maxImageArrayLayers
= (1 << 11),
443 .maxTexelBufferElements
= 128 * 1024 * 1024,
444 .maxUniformBufferRange
= UINT32_MAX
,
445 .maxStorageBufferRange
= UINT32_MAX
,
446 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
447 .maxMemoryAllocationCount
= UINT32_MAX
,
448 .maxSamplerAllocationCount
= 64 * 1024,
449 .bufferImageGranularity
= 64, /* A cache line */
450 .sparseAddressSpaceSize
= 0,
451 .maxBoundDescriptorSets
= MAX_SETS
,
452 .maxPerStageDescriptorSamplers
= 64,
453 .maxPerStageDescriptorUniformBuffers
= 64,
454 .maxPerStageDescriptorStorageBuffers
= 64,
455 .maxPerStageDescriptorSampledImages
= 64,
456 .maxPerStageDescriptorStorageImages
= 64,
457 .maxPerStageDescriptorInputAttachments
= 64,
458 .maxPerStageResources
= 128,
459 .maxDescriptorSetSamplers
= 256,
460 .maxDescriptorSetUniformBuffers
= 256,
461 .maxDescriptorSetUniformBuffersDynamic
= 256,
462 .maxDescriptorSetStorageBuffers
= 256,
463 .maxDescriptorSetStorageBuffersDynamic
= 256,
464 .maxDescriptorSetSampledImages
= 256,
465 .maxDescriptorSetStorageImages
= 256,
466 .maxDescriptorSetInputAttachments
= 256,
467 .maxVertexInputAttributes
= 32,
468 .maxVertexInputBindings
= 32,
469 .maxVertexInputAttributeOffset
= 2047,
470 .maxVertexInputBindingStride
= 2048,
471 .maxVertexOutputComponents
= 128,
472 .maxTessellationGenerationLevel
= 0,
473 .maxTessellationPatchSize
= 0,
474 .maxTessellationControlPerVertexInputComponents
= 0,
475 .maxTessellationControlPerVertexOutputComponents
= 0,
476 .maxTessellationControlPerPatchOutputComponents
= 0,
477 .maxTessellationControlTotalOutputComponents
= 0,
478 .maxTessellationEvaluationInputComponents
= 0,
479 .maxTessellationEvaluationOutputComponents
= 0,
480 .maxGeometryShaderInvocations
= 32,
481 .maxGeometryInputComponents
= 64,
482 .maxGeometryOutputComponents
= 128,
483 .maxGeometryOutputVertices
= 256,
484 .maxGeometryTotalOutputComponents
= 1024,
485 .maxFragmentInputComponents
= 128,
486 .maxFragmentOutputAttachments
= 8,
487 .maxFragmentDualSrcAttachments
= 2,
488 .maxFragmentCombinedOutputResources
= 8,
489 .maxComputeSharedMemorySize
= 32768,
490 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
491 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
492 .maxComputeWorkGroupSize
= {
493 16 * devinfo
->max_cs_threads
,
494 16 * devinfo
->max_cs_threads
,
495 16 * devinfo
->max_cs_threads
,
497 .subPixelPrecisionBits
= 4 /* FIXME */,
498 .subTexelPrecisionBits
= 4 /* FIXME */,
499 .mipmapPrecisionBits
= 4 /* FIXME */,
500 .maxDrawIndexedIndexValue
= UINT32_MAX
,
501 .maxDrawIndirectCount
= UINT32_MAX
,
502 .maxSamplerLodBias
= 16,
503 .maxSamplerAnisotropy
= 16,
504 .maxViewports
= MAX_VIEWPORTS
,
505 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
506 .viewportBoundsRange
= { -16384.0, 16384.0 },
507 .viewportSubPixelBits
= 13, /* We take a float? */
508 .minMemoryMapAlignment
= 4096, /* A page */
509 .minTexelBufferOffsetAlignment
= 1,
510 .minUniformBufferOffsetAlignment
= 1,
511 .minStorageBufferOffsetAlignment
= 1,
512 .minTexelOffset
= -8,
514 .minTexelGatherOffset
= -8,
515 .maxTexelGatherOffset
= 7,
516 .minInterpolationOffset
= 0, /* FIXME */
517 .maxInterpolationOffset
= 0, /* FIXME */
518 .subPixelInterpolationOffsetBits
= 0, /* FIXME */
519 .maxFramebufferWidth
= (1 << 14),
520 .maxFramebufferHeight
= (1 << 14),
521 .maxFramebufferLayers
= (1 << 10),
522 .framebufferColorSampleCounts
= sample_counts
,
523 .framebufferDepthSampleCounts
= sample_counts
,
524 .framebufferStencilSampleCounts
= sample_counts
,
525 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
526 .maxColorAttachments
= MAX_RTS
,
527 .sampledImageColorSampleCounts
= sample_counts
,
528 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
529 .sampledImageDepthSampleCounts
= sample_counts
,
530 .sampledImageStencilSampleCounts
= sample_counts
,
531 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
532 .maxSampleMaskWords
= 1,
533 .timestampComputeAndGraphics
= false,
534 .timestampPeriod
= time_stamp_base
/ (1000 * 1000 * 1000),
535 .maxClipDistances
= 0 /* FIXME */,
536 .maxCullDistances
= 0 /* FIXME */,
537 .maxCombinedClipAndCullDistances
= 0 /* FIXME */,
538 .discreteQueuePriorities
= 1,
539 .pointSizeRange
= { 0.125, 255.875 },
540 .lineWidthRange
= { 0.0, 7.9921875 },
541 .pointSizeGranularity
= (1.0 / 8.0),
542 .lineWidthGranularity
= (1.0 / 128.0),
543 .strictLines
= false, /* FINISHME */
544 .standardSampleLocations
= true,
545 .optimalBufferCopyOffsetAlignment
= 128,
546 .optimalBufferCopyRowPitchAlignment
= 128,
547 .nonCoherentAtomSize
= 64,
550 *pProperties
= (VkPhysicalDeviceProperties
) {
551 .apiVersion
= VK_MAKE_VERSION(1, 0, 5),
554 .deviceID
= pdevice
->chipset_id
,
555 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
557 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
560 strcpy(pProperties
->deviceName
, pdevice
->name
);
561 anv_device_get_cache_uuid(pProperties
->pipelineCacheUUID
);
564 void anv_GetPhysicalDeviceQueueFamilyProperties(
565 VkPhysicalDevice physicalDevice
,
567 VkQueueFamilyProperties
* pQueueFamilyProperties
)
569 if (pQueueFamilyProperties
== NULL
) {
574 assert(*pCount
>= 1);
576 *pQueueFamilyProperties
= (VkQueueFamilyProperties
) {
577 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
578 VK_QUEUE_COMPUTE_BIT
|
579 VK_QUEUE_TRANSFER_BIT
,
581 .timestampValidBits
= 36, /* XXX: Real value here */
582 .minImageTransferGranularity
= (VkExtent3D
) { 1, 1, 1 },
586 void anv_GetPhysicalDeviceMemoryProperties(
587 VkPhysicalDevice physicalDevice
,
588 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
590 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
591 VkDeviceSize heap_size
;
593 /* Reserve some wiggle room for the driver by exposing only 75% of the
594 * aperture to the heap.
596 heap_size
= 3 * physical_device
->aperture_size
/ 4;
598 if (physical_device
->info
->has_llc
) {
599 /* Big core GPUs share LLC with the CPU and thus one memory type can be
600 * both cached and coherent at the same time.
602 pMemoryProperties
->memoryTypeCount
= 1;
603 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
604 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
605 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
606 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
607 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
611 /* The spec requires that we expose a host-visible, coherent memory
612 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
613 * to give the application a choice between cached, but not coherent and
614 * coherent but uncached (WC though).
616 pMemoryProperties
->memoryTypeCount
= 2;
617 pMemoryProperties
->memoryTypes
[0] = (VkMemoryType
) {
618 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
619 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
620 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
623 pMemoryProperties
->memoryTypes
[1] = (VkMemoryType
) {
624 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
625 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
626 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
631 pMemoryProperties
->memoryHeapCount
= 1;
632 pMemoryProperties
->memoryHeaps
[0] = (VkMemoryHeap
) {
634 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
638 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
642 return anv_lookup_entrypoint(pName
);
645 /* The loader wants us to expose a second GetInstanceProcAddr function
646 * to work around certain LD_PRELOAD issues seen in apps.
648 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
652 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
656 return anv_GetInstanceProcAddr(instance
, pName
);
659 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
663 return anv_lookup_entrypoint(pName
);
667 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
669 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
670 queue
->device
= device
;
671 queue
->pool
= &device
->surface_state_pool
;
677 anv_queue_finish(struct anv_queue
*queue
)
681 static struct anv_state
682 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
684 struct anv_state state
;
686 state
= anv_state_pool_alloc(pool
, size
, align
);
687 memcpy(state
.map
, p
, size
);
689 if (!pool
->block_pool
->device
->info
.has_llc
)
690 anv_state_clflush(state
);
695 struct gen8_border_color
{
700 /* Pad out to 64 bytes */
705 anv_device_init_border_colors(struct anv_device
*device
)
707 static const struct gen8_border_color border_colors
[] = {
708 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
709 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
710 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
711 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
712 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
713 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
716 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
717 sizeof(border_colors
), 64,
722 anv_device_submit_simple_batch(struct anv_device
*device
,
723 struct anv_batch
*batch
)
725 struct drm_i915_gem_execbuffer2 execbuf
;
726 struct drm_i915_gem_exec_object2 exec2_objects
[1];
728 VkResult result
= VK_SUCCESS
;
733 /* Kernel driver requires 8 byte aligned batch length */
734 size
= align_u32(batch
->next
- batch
->start
, 8);
735 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &bo
, size
);
736 if (result
!= VK_SUCCESS
)
739 memcpy(bo
.map
, batch
->start
, size
);
740 if (!device
->info
.has_llc
)
741 anv_clflush_range(bo
.map
, size
);
743 exec2_objects
[0].handle
= bo
.gem_handle
;
744 exec2_objects
[0].relocation_count
= 0;
745 exec2_objects
[0].relocs_ptr
= 0;
746 exec2_objects
[0].alignment
= 0;
747 exec2_objects
[0].offset
= bo
.offset
;
748 exec2_objects
[0].flags
= 0;
749 exec2_objects
[0].rsvd1
= 0;
750 exec2_objects
[0].rsvd2
= 0;
752 execbuf
.buffers_ptr
= (uintptr_t) exec2_objects
;
753 execbuf
.buffer_count
= 1;
754 execbuf
.batch_start_offset
= 0;
755 execbuf
.batch_len
= size
;
756 execbuf
.cliprects_ptr
= 0;
757 execbuf
.num_cliprects
= 0;
762 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
763 execbuf
.rsvd1
= device
->context_id
;
766 ret
= anv_gem_execbuffer(device
, &execbuf
);
768 /* We don't know the real error. */
769 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
774 ret
= anv_gem_wait(device
, bo
.gem_handle
, &timeout
);
776 /* We don't know the real error. */
777 result
= vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
, "execbuf2 failed: %m");
782 anv_bo_pool_free(&device
->batch_bo_pool
, &bo
);
787 VkResult
anv_CreateDevice(
788 VkPhysicalDevice physicalDevice
,
789 const VkDeviceCreateInfo
* pCreateInfo
,
790 const VkAllocationCallbacks
* pAllocator
,
793 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
795 struct anv_device
*device
;
797 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
799 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
801 for (uint32_t j
= 0; j
< ARRAY_SIZE(device_extensions
); j
++) {
802 if (strcmp(pCreateInfo
->ppEnabledExtensionNames
[i
],
803 device_extensions
[j
].extensionName
) == 0) {
809 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
812 anv_set_dispatch_devinfo(physical_device
->info
);
814 device
= anv_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
816 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
818 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
820 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
821 device
->instance
= physical_device
->instance
;
822 device
->chipset_id
= physical_device
->chipset_id
;
825 device
->alloc
= *pAllocator
;
827 device
->alloc
= physical_device
->instance
->alloc
;
829 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
830 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
831 if (device
->fd
== -1) {
832 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
836 device
->context_id
= anv_gem_create_context(device
);
837 if (device
->context_id
== -1) {
838 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
842 device
->info
= *physical_device
->info
;
843 device
->isl_dev
= physical_device
->isl_dev
;
845 /* On Broadwell and later, we can use batch chaining to more efficiently
846 * implement growing command buffers. Prior to Haswell, the kernel
847 * command parser gets in the way and we have to fall back to growing
850 device
->can_chain_batches
= device
->info
.gen
>= 8;
852 pthread_mutex_init(&device
->mutex
, NULL
);
854 anv_bo_pool_init(&device
->batch_bo_pool
, device
);
856 anv_block_pool_init(&device
->dynamic_state_block_pool
, device
, 16384);
858 anv_state_pool_init(&device
->dynamic_state_pool
,
859 &device
->dynamic_state_block_pool
);
861 anv_block_pool_init(&device
->instruction_block_pool
, device
, 128 * 1024);
862 anv_pipeline_cache_init(&device
->default_pipeline_cache
, device
);
864 anv_block_pool_init(&device
->surface_state_block_pool
, device
, 4096);
866 anv_state_pool_init(&device
->surface_state_pool
,
867 &device
->surface_state_block_pool
);
869 anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
871 anv_block_pool_init(&device
->scratch_block_pool
, device
, 0x10000);
873 anv_queue_init(device
, &device
->queue
);
875 switch (device
->info
.gen
) {
877 if (!device
->info
.is_haswell
)
878 result
= gen7_init_device_state(device
);
880 result
= gen75_init_device_state(device
);
883 result
= gen8_init_device_state(device
);
886 result
= gen9_init_device_state(device
);
889 /* Shouldn't get here as we don't create physical devices for any other
891 unreachable("unhandled gen");
893 if (result
!= VK_SUCCESS
)
896 result
= anv_device_init_meta(device
);
897 if (result
!= VK_SUCCESS
)
900 anv_device_init_border_colors(device
);
902 *pDevice
= anv_device_to_handle(device
);
909 anv_free(&device
->alloc
, device
);
914 void anv_DestroyDevice(
916 const VkAllocationCallbacks
* pAllocator
)
918 ANV_FROM_HANDLE(anv_device
, device
, _device
);
920 anv_queue_finish(&device
->queue
);
922 anv_device_finish_meta(device
);
925 /* We only need to free these to prevent valgrind errors. The backing
926 * BO will go away in a couple of lines so we don't actually leak.
928 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
931 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
932 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
934 anv_bo_pool_finish(&device
->batch_bo_pool
);
935 anv_state_pool_finish(&device
->dynamic_state_pool
);
936 anv_block_pool_finish(&device
->dynamic_state_block_pool
);
937 anv_block_pool_finish(&device
->instruction_block_pool
);
938 anv_state_pool_finish(&device
->surface_state_pool
);
939 anv_block_pool_finish(&device
->surface_state_block_pool
);
940 anv_block_pool_finish(&device
->scratch_block_pool
);
944 pthread_mutex_destroy(&device
->mutex
);
946 anv_free(&device
->alloc
, device
);
949 VkResult
anv_EnumerateInstanceExtensionProperties(
950 const char* pLayerName
,
951 uint32_t* pPropertyCount
,
952 VkExtensionProperties
* pProperties
)
954 if (pProperties
== NULL
) {
955 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
959 assert(*pPropertyCount
>= ARRAY_SIZE(global_extensions
));
961 *pPropertyCount
= ARRAY_SIZE(global_extensions
);
962 memcpy(pProperties
, global_extensions
, sizeof(global_extensions
));
967 VkResult
anv_EnumerateDeviceExtensionProperties(
968 VkPhysicalDevice physicalDevice
,
969 const char* pLayerName
,
970 uint32_t* pPropertyCount
,
971 VkExtensionProperties
* pProperties
)
973 if (pProperties
== NULL
) {
974 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
978 assert(*pPropertyCount
>= ARRAY_SIZE(device_extensions
));
980 *pPropertyCount
= ARRAY_SIZE(device_extensions
);
981 memcpy(pProperties
, device_extensions
, sizeof(device_extensions
));
986 VkResult
anv_EnumerateInstanceLayerProperties(
987 uint32_t* pPropertyCount
,
988 VkLayerProperties
* pProperties
)
990 if (pProperties
== NULL
) {
995 /* None supported at this time */
996 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
999 VkResult
anv_EnumerateDeviceLayerProperties(
1000 VkPhysicalDevice physicalDevice
,
1001 uint32_t* pPropertyCount
,
1002 VkLayerProperties
* pProperties
)
1004 if (pProperties
== NULL
) {
1005 *pPropertyCount
= 0;
1009 /* None supported at this time */
1010 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1013 void anv_GetDeviceQueue(
1015 uint32_t queueNodeIndex
,
1016 uint32_t queueIndex
,
1019 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1021 assert(queueIndex
== 0);
1023 *pQueue
= anv_queue_to_handle(&device
->queue
);
1026 VkResult
anv_QueueSubmit(
1028 uint32_t submitCount
,
1029 const VkSubmitInfo
* pSubmits
,
1032 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1033 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1034 struct anv_device
*device
= queue
->device
;
1037 for (uint32_t i
= 0; i
< submitCount
; i
++) {
1038 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1039 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1040 pSubmits
[i
].pCommandBuffers
[j
]);
1041 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1043 ret
= anv_gem_execbuffer(device
, &cmd_buffer
->execbuf2
.execbuf
);
1045 /* We don't know the real error. */
1046 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1047 "execbuf2 failed: %m");
1050 for (uint32_t k
= 0; k
< cmd_buffer
->execbuf2
.bo_count
; k
++)
1051 cmd_buffer
->execbuf2
.bos
[k
]->offset
= cmd_buffer
->execbuf2
.objects
[k
].offset
;
1056 ret
= anv_gem_execbuffer(device
, &fence
->execbuf
);
1058 /* We don't know the real error. */
1059 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1060 "execbuf2 failed: %m");
1067 VkResult
anv_QueueWaitIdle(
1070 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1072 return ANV_CALL(DeviceWaitIdle
)(anv_device_to_handle(queue
->device
));
1075 VkResult
anv_DeviceWaitIdle(
1078 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1079 struct anv_batch batch
;
1082 batch
.start
= batch
.next
= cmds
;
1083 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1085 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1086 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1088 return anv_device_submit_simple_batch(device
, &batch
);
1092 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1094 bo
->gem_handle
= anv_gem_create(device
, size
);
1095 if (!bo
->gem_handle
)
1096 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1102 bo
->is_winsys_bo
= false;
1107 VkResult
anv_AllocateMemory(
1109 const VkMemoryAllocateInfo
* pAllocateInfo
,
1110 const VkAllocationCallbacks
* pAllocator
,
1111 VkDeviceMemory
* pMem
)
1113 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1114 struct anv_device_memory
*mem
;
1117 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1119 if (pAllocateInfo
->allocationSize
== 0) {
1120 /* Apparently, this is allowed */
1121 *pMem
= VK_NULL_HANDLE
;
1125 /* We support exactly one memory heap. */
1126 assert(pAllocateInfo
->memoryTypeIndex
== 0 ||
1127 (!device
->info
.has_llc
&& pAllocateInfo
->memoryTypeIndex
< 2));
1129 /* FINISHME: Fail if allocation request exceeds heap size. */
1131 mem
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1132 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1134 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1136 /* The kernel is going to give us whole pages anyway */
1137 uint64_t alloc_size
= align_u64(pAllocateInfo
->allocationSize
, 4096);
1139 result
= anv_bo_init_new(&mem
->bo
, device
, alloc_size
);
1140 if (result
!= VK_SUCCESS
)
1143 mem
->type_index
= pAllocateInfo
->memoryTypeIndex
;
1145 *pMem
= anv_device_memory_to_handle(mem
);
1150 anv_free2(&device
->alloc
, pAllocator
, mem
);
1155 void anv_FreeMemory(
1157 VkDeviceMemory _mem
,
1158 const VkAllocationCallbacks
* pAllocator
)
1160 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1161 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1167 anv_gem_munmap(mem
->bo
.map
, mem
->bo
.size
);
1169 if (mem
->bo
.gem_handle
!= 0)
1170 anv_gem_close(device
, mem
->bo
.gem_handle
);
1172 anv_free2(&device
->alloc
, pAllocator
, mem
);
1175 VkResult
anv_MapMemory(
1177 VkDeviceMemory _memory
,
1178 VkDeviceSize offset
,
1180 VkMemoryMapFlags flags
,
1183 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1184 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1191 if (size
== VK_WHOLE_SIZE
)
1192 size
= mem
->bo
.size
- offset
;
1194 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1195 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1196 * at a time is valid. We could just mmap up front and return an offset
1197 * pointer here, but that may exhaust virtual memory on 32 bit
1200 uint32_t gem_flags
= 0;
1201 if (!device
->info
.has_llc
&& mem
->type_index
== 0)
1202 gem_flags
|= I915_MMAP_WC
;
1204 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1205 uint64_t map_offset
= offset
& ~4095ull;
1206 assert(offset
>= map_offset
);
1207 uint64_t map_size
= (offset
+ size
) - map_offset
;
1209 /* Let's map whole pages */
1210 map_size
= align_u64(map_size
, 4096);
1212 mem
->map
= anv_gem_mmap(device
, mem
->bo
.gem_handle
,
1213 map_offset
, map_size
, gem_flags
);
1214 mem
->map_size
= map_size
;
1216 *ppData
= mem
->map
+ (offset
- map_offset
);
1221 void anv_UnmapMemory(
1223 VkDeviceMemory _memory
)
1225 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1230 anv_gem_munmap(mem
->map
, mem
->map_size
);
1234 clflush_mapped_ranges(struct anv_device
*device
,
1236 const VkMappedMemoryRange
*ranges
)
1238 for (uint32_t i
= 0; i
< count
; i
++) {
1239 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1240 void *p
= mem
->map
+ (ranges
[i
].offset
& ~CACHELINE_MASK
);
1243 if (ranges
[i
].offset
+ ranges
[i
].size
> mem
->map_size
)
1244 end
= mem
->map
+ mem
->map_size
;
1246 end
= mem
->map
+ ranges
[i
].offset
+ ranges
[i
].size
;
1249 __builtin_ia32_clflush(p
);
1250 p
+= CACHELINE_SIZE
;
1255 VkResult
anv_FlushMappedMemoryRanges(
1257 uint32_t memoryRangeCount
,
1258 const VkMappedMemoryRange
* pMemoryRanges
)
1260 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1262 if (device
->info
.has_llc
)
1265 /* Make sure the writes we're flushing have landed. */
1266 __builtin_ia32_mfence();
1268 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1273 VkResult
anv_InvalidateMappedMemoryRanges(
1275 uint32_t memoryRangeCount
,
1276 const VkMappedMemoryRange
* pMemoryRanges
)
1278 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1280 if (device
->info
.has_llc
)
1283 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1285 /* Make sure no reads get moved up above the invalidate. */
1286 __builtin_ia32_mfence();
1291 void anv_GetBufferMemoryRequirements(
1294 VkMemoryRequirements
* pMemoryRequirements
)
1296 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1298 /* The Vulkan spec (git aaed022) says:
1300 * memoryTypeBits is a bitfield and contains one bit set for every
1301 * supported memory type for the resource. The bit `1<<i` is set if and
1302 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1303 * structure for the physical device is supported.
1305 * We support exactly one memory type.
1307 pMemoryRequirements
->memoryTypeBits
= 1;
1309 pMemoryRequirements
->size
= buffer
->size
;
1310 pMemoryRequirements
->alignment
= 16;
1313 void anv_GetImageMemoryRequirements(
1316 VkMemoryRequirements
* pMemoryRequirements
)
1318 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1320 /* The Vulkan spec (git aaed022) says:
1322 * memoryTypeBits is a bitfield and contains one bit set for every
1323 * supported memory type for the resource. The bit `1<<i` is set if and
1324 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1325 * structure for the physical device is supported.
1327 * We support exactly one memory type.
1329 pMemoryRequirements
->memoryTypeBits
= 1;
1331 pMemoryRequirements
->size
= image
->size
;
1332 pMemoryRequirements
->alignment
= image
->alignment
;
1335 void anv_GetImageSparseMemoryRequirements(
1338 uint32_t* pSparseMemoryRequirementCount
,
1339 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
1344 void anv_GetDeviceMemoryCommitment(
1346 VkDeviceMemory memory
,
1347 VkDeviceSize
* pCommittedMemoryInBytes
)
1349 *pCommittedMemoryInBytes
= 0;
1352 VkResult
anv_BindBufferMemory(
1355 VkDeviceMemory _memory
,
1356 VkDeviceSize memoryOffset
)
1358 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1359 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1362 buffer
->bo
= &mem
->bo
;
1363 buffer
->offset
= memoryOffset
;
1372 VkResult
anv_BindImageMemory(
1375 VkDeviceMemory _memory
,
1376 VkDeviceSize memoryOffset
)
1378 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1379 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1382 image
->bo
= &mem
->bo
;
1383 image
->offset
= memoryOffset
;
1392 VkResult
anv_QueueBindSparse(
1394 uint32_t bindInfoCount
,
1395 const VkBindSparseInfo
* pBindInfo
,
1398 stub_return(VK_ERROR_INCOMPATIBLE_DRIVER
);
1401 VkResult
anv_CreateFence(
1403 const VkFenceCreateInfo
* pCreateInfo
,
1404 const VkAllocationCallbacks
* pAllocator
,
1407 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1408 struct anv_bo fence_bo
;
1409 struct anv_fence
*fence
;
1410 struct anv_batch batch
;
1413 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1415 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, &fence_bo
, 4096);
1416 if (result
!= VK_SUCCESS
)
1419 /* Fences are small. Just store the CPU data structure in the BO. */
1420 fence
= fence_bo
.map
;
1421 fence
->bo
= fence_bo
;
1423 /* Place the batch after the CPU data but on its own cache line. */
1424 const uint32_t batch_offset
= align_u32(sizeof(*fence
), CACHELINE_SIZE
);
1425 batch
.next
= batch
.start
= fence
->bo
.map
+ batch_offset
;
1426 batch
.end
= fence
->bo
.map
+ fence
->bo
.size
;
1427 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1428 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1430 if (!device
->info
.has_llc
) {
1431 assert(((uintptr_t) batch
.start
& CACHELINE_MASK
) == 0);
1432 assert(batch
.next
- batch
.start
<= CACHELINE_SIZE
);
1433 __builtin_ia32_mfence();
1434 __builtin_ia32_clflush(batch
.start
);
1437 fence
->exec2_objects
[0].handle
= fence
->bo
.gem_handle
;
1438 fence
->exec2_objects
[0].relocation_count
= 0;
1439 fence
->exec2_objects
[0].relocs_ptr
= 0;
1440 fence
->exec2_objects
[0].alignment
= 0;
1441 fence
->exec2_objects
[0].offset
= fence
->bo
.offset
;
1442 fence
->exec2_objects
[0].flags
= 0;
1443 fence
->exec2_objects
[0].rsvd1
= 0;
1444 fence
->exec2_objects
[0].rsvd2
= 0;
1446 fence
->execbuf
.buffers_ptr
= (uintptr_t) fence
->exec2_objects
;
1447 fence
->execbuf
.buffer_count
= 1;
1448 fence
->execbuf
.batch_start_offset
= batch
.start
- fence
->bo
.map
;
1449 fence
->execbuf
.batch_len
= batch
.next
- batch
.start
;
1450 fence
->execbuf
.cliprects_ptr
= 0;
1451 fence
->execbuf
.num_cliprects
= 0;
1452 fence
->execbuf
.DR1
= 0;
1453 fence
->execbuf
.DR4
= 0;
1455 fence
->execbuf
.flags
=
1456 I915_EXEC_HANDLE_LUT
| I915_EXEC_NO_RELOC
| I915_EXEC_RENDER
;
1457 fence
->execbuf
.rsvd1
= device
->context_id
;
1458 fence
->execbuf
.rsvd2
= 0;
1460 fence
->ready
= false;
1462 *pFence
= anv_fence_to_handle(fence
);
1467 void anv_DestroyFence(
1470 const VkAllocationCallbacks
* pAllocator
)
1472 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1473 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1475 assert(fence
->bo
.map
== fence
);
1476 anv_bo_pool_free(&device
->batch_bo_pool
, &fence
->bo
);
1479 VkResult
anv_ResetFences(
1481 uint32_t fenceCount
,
1482 const VkFence
* pFences
)
1484 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1485 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1486 fence
->ready
= false;
1492 VkResult
anv_GetFenceStatus(
1496 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1497 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1504 ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1506 fence
->ready
= true;
1510 return VK_NOT_READY
;
1513 VkResult
anv_WaitForFences(
1515 uint32_t fenceCount
,
1516 const VkFence
* pFences
,
1520 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1522 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is supposed
1523 * to block indefinitely timeouts <= 0. Unfortunately, this was broken
1524 * for a couple of kernel releases. Since there's no way to know
1525 * whether or not the kernel we're using is one of the broken ones, the
1526 * best we can do is to clamp the timeout to INT64_MAX. This limits the
1527 * maximum timeout from 584 years to 292 years - likely not a big deal.
1529 if (timeout
> INT64_MAX
)
1530 timeout
= INT64_MAX
;
1532 int64_t t
= timeout
;
1534 /* FIXME: handle !waitAll */
1536 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1537 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1538 int ret
= anv_gem_wait(device
, fence
->bo
.gem_handle
, &t
);
1539 if (ret
== -1 && errno
== ETIME
) {
1541 } else if (ret
== -1) {
1542 /* We don't know the real error. */
1543 return vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1544 "gem wait failed: %m");
1551 // Queue semaphore functions
1553 VkResult
anv_CreateSemaphore(
1555 const VkSemaphoreCreateInfo
* pCreateInfo
,
1556 const VkAllocationCallbacks
* pAllocator
,
1557 VkSemaphore
* pSemaphore
)
1559 /* The DRM execbuffer ioctl always execute in-oder, even between different
1560 * rings. As such, there's nothing to do for the user space semaphore.
1563 *pSemaphore
= (VkSemaphore
)1;
1568 void anv_DestroySemaphore(
1570 VkSemaphore semaphore
,
1571 const VkAllocationCallbacks
* pAllocator
)
1577 VkResult
anv_CreateEvent(
1579 const VkEventCreateInfo
* pCreateInfo
,
1580 const VkAllocationCallbacks
* pAllocator
,
1583 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1584 struct anv_state state
;
1585 struct anv_event
*event
;
1587 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
1589 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
1592 event
->state
= state
;
1593 event
->semaphore
= VK_EVENT_RESET
;
1595 if (!device
->info
.has_llc
) {
1596 /* Make sure the writes we're flushing have landed. */
1597 __builtin_ia32_mfence();
1598 __builtin_ia32_clflush(event
);
1601 *pEvent
= anv_event_to_handle(event
);
1606 void anv_DestroyEvent(
1609 const VkAllocationCallbacks
* pAllocator
)
1611 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1612 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1614 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
1617 VkResult
anv_GetEventStatus(
1621 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1622 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1624 if (!device
->info
.has_llc
) {
1625 /* Invalidate read cache before reading event written by GPU. */
1626 __builtin_ia32_clflush(event
);
1627 __builtin_ia32_mfence();
1631 return event
->semaphore
;
1634 VkResult
anv_SetEvent(
1638 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1639 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1641 event
->semaphore
= VK_EVENT_SET
;
1643 if (!device
->info
.has_llc
) {
1644 /* Make sure the writes we're flushing have landed. */
1645 __builtin_ia32_mfence();
1646 __builtin_ia32_clflush(event
);
1652 VkResult
anv_ResetEvent(
1656 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1657 ANV_FROM_HANDLE(anv_event
, event
, _event
);
1659 event
->semaphore
= VK_EVENT_RESET
;
1661 if (!device
->info
.has_llc
) {
1662 /* Make sure the writes we're flushing have landed. */
1663 __builtin_ia32_mfence();
1664 __builtin_ia32_clflush(event
);
1672 VkResult
anv_CreateBuffer(
1674 const VkBufferCreateInfo
* pCreateInfo
,
1675 const VkAllocationCallbacks
* pAllocator
,
1678 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1679 struct anv_buffer
*buffer
;
1681 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
1683 buffer
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
1684 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1686 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1688 buffer
->size
= pCreateInfo
->size
;
1689 buffer
->usage
= pCreateInfo
->usage
;
1693 *pBuffer
= anv_buffer_to_handle(buffer
);
1698 void anv_DestroyBuffer(
1701 const VkAllocationCallbacks
* pAllocator
)
1703 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1704 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1706 anv_free2(&device
->alloc
, pAllocator
, buffer
);
1710 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
1711 enum isl_format format
,
1712 uint32_t offset
, uint32_t range
, uint32_t stride
)
1714 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
1716 .mocs
= device
->default_mocs
,
1721 if (!device
->info
.has_llc
)
1722 anv_state_clflush(state
);
1725 void anv_DestroySampler(
1728 const VkAllocationCallbacks
* pAllocator
)
1730 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1731 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
1733 anv_free2(&device
->alloc
, pAllocator
, sampler
);
1736 VkResult
anv_CreateFramebuffer(
1738 const VkFramebufferCreateInfo
* pCreateInfo
,
1739 const VkAllocationCallbacks
* pAllocator
,
1740 VkFramebuffer
* pFramebuffer
)
1742 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1743 struct anv_framebuffer
*framebuffer
;
1745 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
1747 size_t size
= sizeof(*framebuffer
) +
1748 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
1749 framebuffer
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
1750 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1751 if (framebuffer
== NULL
)
1752 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1754 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
1755 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
1756 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
1757 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
1760 framebuffer
->width
= pCreateInfo
->width
;
1761 framebuffer
->height
= pCreateInfo
->height
;
1762 framebuffer
->layers
= pCreateInfo
->layers
;
1764 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
1769 void anv_DestroyFramebuffer(
1772 const VkAllocationCallbacks
* pAllocator
)
1774 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1775 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
1777 anv_free2(&device
->alloc
, pAllocator
, fb
);
1780 void vkCmdDbgMarkerBegin(
1781 VkCommandBuffer commandBuffer
,
1782 const char* pMarker
)
1783 __attribute__ ((visibility ("default")));
1785 void vkCmdDbgMarkerEnd(
1786 VkCommandBuffer commandBuffer
)
1787 __attribute__ ((visibility ("default")));
1789 void vkCmdDbgMarkerBegin(
1790 VkCommandBuffer commandBuffer
,
1791 const char* pMarker
)
1795 void vkCmdDbgMarkerEnd(
1796 VkCommandBuffer commandBuffer
)