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
28 #include <sys/sysinfo.h>
32 #include <drm_fourcc.h>
34 #include "anv_private.h"
35 #include "util/strtod.h"
36 #include "util/debug.h"
37 #include "util/build_id.h"
38 #include "util/mesa-sha1.h"
41 #include "genxml/gen7_pack.h"
44 compiler_debug_log(void *data
, const char *fmt
, ...)
48 compiler_perf_log(void *data
, const char *fmt
, ...)
53 if (unlikely(INTEL_DEBUG
& DEBUG_PERF
))
54 intel_logd_v(fmt
, args
);
60 anv_compute_heap_size(int fd
, uint64_t *heap_size
)
63 if (anv_gem_get_context_param(fd
, 0, I915_CONTEXT_PARAM_GTT_SIZE
,
65 /* If, for whatever reason, we can't actually get the GTT size from the
66 * kernel (too old?) fall back to the aperture size.
68 anv_perf_warn(NULL
, NULL
,
69 "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
71 if (anv_gem_get_aperture(fd
, >t_size
) == -1) {
72 return vk_errorf(NULL
, NULL
, VK_ERROR_INITIALIZATION_FAILED
,
73 "failed to get aperture size: %m");
77 /* Query the total ram from the system */
81 uint64_t total_ram
= (uint64_t)info
.totalram
* (uint64_t)info
.mem_unit
;
83 /* We don't want to burn too much ram with the GPU. If the user has 4GiB
84 * or less, we use at most half. If they have more than 4GiB, we use 3/4.
86 uint64_t available_ram
;
87 if (total_ram
<= 4ull * 1024ull * 1024ull * 1024ull)
88 available_ram
= total_ram
/ 2;
90 available_ram
= total_ram
* 3 / 4;
92 /* We also want to leave some padding for things we allocate in the driver,
93 * so don't go over 3/4 of the GTT either.
95 uint64_t available_gtt
= gtt_size
* 3 / 4;
97 *heap_size
= MIN2(available_ram
, available_gtt
);
103 anv_physical_device_init_heaps(struct anv_physical_device
*device
, int fd
)
105 /* The kernel query only tells us whether or not the kernel supports the
106 * EXEC_OBJECT_SUPPORTS_48B_ADDRESS flag and not whether or not the
107 * hardware has actual 48bit address support.
109 device
->supports_48bit_addresses
=
110 (device
->info
.gen
>= 8) && anv_gem_supports_48b_addresses(fd
);
113 VkResult result
= anv_compute_heap_size(fd
, &heap_size
);
114 if (result
!= VK_SUCCESS
)
117 if (heap_size
> (2ull << 30) && !device
->supports_48bit_addresses
) {
118 /* When running with an overridden PCI ID, we may get a GTT size from
119 * the kernel that is greater than 2 GiB but the execbuf check for 48bit
120 * address support can still fail. Just clamp the address space size to
121 * 2 GiB if we don't have 48-bit support.
123 intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
124 "not support for 48-bit addresses",
126 heap_size
= 2ull << 30;
129 if (heap_size
<= 3ull * (1ull << 30)) {
130 /* In this case, everything fits nicely into the 32-bit address space,
131 * so there's no need for supporting 48bit addresses on client-allocated
134 device
->memory
.heap_count
= 1;
135 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
137 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
138 .supports_48bit_addresses
= false,
141 /* Not everything will fit nicely into a 32-bit address space. In this
142 * case we need a 64-bit heap. Advertise a small 32-bit heap and a
143 * larger 48-bit heap. If we're in this case, then we have a total heap
144 * size larger than 3GiB which most likely means they have 8 GiB of
145 * video memory and so carving off 1 GiB for the 32-bit heap should be
148 const uint64_t heap_size_32bit
= 1ull << 30;
149 const uint64_t heap_size_48bit
= heap_size
- heap_size_32bit
;
151 assert(device
->supports_48bit_addresses
);
153 device
->memory
.heap_count
= 2;
154 device
->memory
.heaps
[0] = (struct anv_memory_heap
) {
155 .size
= heap_size_48bit
,
156 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
157 .supports_48bit_addresses
= true,
159 device
->memory
.heaps
[1] = (struct anv_memory_heap
) {
160 .size
= heap_size_32bit
,
161 .flags
= VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
,
162 .supports_48bit_addresses
= false,
166 uint32_t type_count
= 0;
167 for (uint32_t heap
= 0; heap
< device
->memory
.heap_count
; heap
++) {
168 uint32_t valid_buffer_usage
= ~0;
170 /* There appears to be a hardware issue in the VF cache where it only
171 * considers the bottom 32 bits of memory addresses. If you happen to
172 * have two vertex buffers which get placed exactly 4 GiB apart and use
173 * them in back-to-back draw calls, you can get collisions. In order to
174 * solve this problem, we require vertex and index buffers be bound to
175 * memory allocated out of the 32-bit heap.
177 if (device
->memory
.heaps
[heap
].supports_48bit_addresses
) {
178 valid_buffer_usage
&= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT
|
179 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT
);
182 if (device
->info
.has_llc
) {
183 /* Big core GPUs share LLC with the CPU and thus one memory type can be
184 * both cached and coherent at the same time.
186 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
187 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
188 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
189 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
190 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
192 .valid_buffer_usage
= valid_buffer_usage
,
195 /* The spec requires that we expose a host-visible, coherent memory
196 * type, but Atom GPUs don't share LLC. Thus we offer two memory types
197 * to give the application a choice between cached, but not coherent and
198 * coherent but uncached (WC though).
200 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
201 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
202 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
203 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
,
205 .valid_buffer_usage
= valid_buffer_usage
,
207 device
->memory
.types
[type_count
++] = (struct anv_memory_type
) {
208 .propertyFlags
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
209 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
210 VK_MEMORY_PROPERTY_HOST_CACHED_BIT
,
212 .valid_buffer_usage
= valid_buffer_usage
,
216 device
->memory
.type_count
= type_count
;
222 anv_physical_device_init_uuids(struct anv_physical_device
*device
)
224 const struct build_id_note
*note
=
225 build_id_find_nhdr_for_addr(anv_physical_device_init_uuids
);
227 return vk_errorf(device
->instance
, device
,
228 VK_ERROR_INITIALIZATION_FAILED
,
229 "Failed to find build-id");
232 unsigned build_id_len
= build_id_length(note
);
233 if (build_id_len
< 20) {
234 return vk_errorf(device
->instance
, device
,
235 VK_ERROR_INITIALIZATION_FAILED
,
236 "build-id too short. It needs to be a SHA");
239 struct mesa_sha1 sha1_ctx
;
241 STATIC_ASSERT(VK_UUID_SIZE
<= sizeof(sha1
));
243 /* The pipeline cache UUID is used for determining when a pipeline cache is
244 * invalid. It needs both a driver build and the PCI ID of the device.
246 _mesa_sha1_init(&sha1_ctx
);
247 _mesa_sha1_update(&sha1_ctx
, build_id_data(note
), build_id_len
);
248 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
249 sizeof(device
->chipset_id
));
250 _mesa_sha1_final(&sha1_ctx
, sha1
);
251 memcpy(device
->pipeline_cache_uuid
, sha1
, VK_UUID_SIZE
);
253 /* The driver UUID is used for determining sharability of images and memory
254 * between two Vulkan instances in separate processes. People who want to
255 * share memory need to also check the device UUID (below) so all this
256 * needs to be is the build-id.
258 memcpy(device
->driver_uuid
, build_id_data(note
), VK_UUID_SIZE
);
260 /* The device UUID uniquely identifies the given device within the machine.
261 * Since we never have more than one device, this doesn't need to be a real
262 * UUID. However, on the off-chance that someone tries to use this to
263 * cache pre-tiled images or something of the like, we use the PCI ID and
264 * some bits of ISL info to ensure that this is safe.
266 _mesa_sha1_init(&sha1_ctx
);
267 _mesa_sha1_update(&sha1_ctx
, &device
->chipset_id
,
268 sizeof(device
->chipset_id
));
269 _mesa_sha1_update(&sha1_ctx
, &device
->isl_dev
.has_bit6_swizzling
,
270 sizeof(device
->isl_dev
.has_bit6_swizzling
));
271 _mesa_sha1_final(&sha1_ctx
, sha1
);
272 memcpy(device
->device_uuid
, sha1
, VK_UUID_SIZE
);
278 anv_physical_device_init(struct anv_physical_device
*device
,
279 struct anv_instance
*instance
,
285 brw_process_intel_debug_variable();
287 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
289 return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
291 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
292 device
->instance
= instance
;
294 assert(strlen(path
) < ARRAY_SIZE(device
->path
));
295 strncpy(device
->path
, path
, ARRAY_SIZE(device
->path
));
297 device
->chipset_id
= anv_gem_get_param(fd
, I915_PARAM_CHIPSET_ID
);
298 if (!device
->chipset_id
) {
299 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
303 device
->name
= gen_get_device_name(device
->chipset_id
);
304 if (!gen_get_device_info(device
->chipset_id
, &device
->info
)) {
305 result
= vk_error(VK_ERROR_INCOMPATIBLE_DRIVER
);
309 if (device
->info
.is_haswell
) {
310 intel_logw("Haswell Vulkan support is incomplete");
311 } else if (device
->info
.gen
== 7 && !device
->info
.is_baytrail
) {
312 intel_logw("Ivy Bridge Vulkan support is incomplete");
313 } else if (device
->info
.gen
== 7 && device
->info
.is_baytrail
) {
314 intel_logw("Bay Trail Vulkan support is incomplete");
315 } else if (device
->info
.gen
>= 8 && device
->info
.gen
<= 9) {
316 /* Broadwell, Cherryview, Skylake, Broxton, Kabylake, Coffelake is as
317 * fully supported as anything */
318 } else if (device
->info
.gen
== 10) {
319 intel_logw("Cannonlake Vulkan support is alpha");
321 result
= vk_errorf(device
->instance
, device
,
322 VK_ERROR_INCOMPATIBLE_DRIVER
,
323 "Vulkan not yet supported on %s", device
->name
);
327 device
->cmd_parser_version
= -1;
328 if (device
->info
.gen
== 7) {
329 device
->cmd_parser_version
=
330 anv_gem_get_param(fd
, I915_PARAM_CMD_PARSER_VERSION
);
331 if (device
->cmd_parser_version
== -1) {
332 result
= vk_errorf(device
->instance
, device
,
333 VK_ERROR_INITIALIZATION_FAILED
,
334 "failed to get command parser version");
339 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_WAIT_TIMEOUT
)) {
340 result
= vk_errorf(device
->instance
, device
,
341 VK_ERROR_INITIALIZATION_FAILED
,
342 "kernel missing gem wait");
346 if (!anv_gem_get_param(fd
, I915_PARAM_HAS_EXECBUF2
)) {
347 result
= vk_errorf(device
->instance
, device
,
348 VK_ERROR_INITIALIZATION_FAILED
,
349 "kernel missing execbuf2");
353 if (!device
->info
.has_llc
&&
354 anv_gem_get_param(fd
, I915_PARAM_MMAP_VERSION
) < 1) {
355 result
= vk_errorf(device
->instance
, device
,
356 VK_ERROR_INITIALIZATION_FAILED
,
357 "kernel missing wc mmap");
361 result
= anv_physical_device_init_heaps(device
, fd
);
362 if (result
!= VK_SUCCESS
)
365 device
->has_exec_async
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_ASYNC
);
366 device
->has_exec_capture
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_CAPTURE
);
367 device
->has_exec_fence
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE
);
368 device
->has_syncobj
= anv_gem_get_param(fd
, I915_PARAM_HAS_EXEC_FENCE_ARRAY
);
369 device
->has_syncobj_wait
= device
->has_syncobj
&&
370 anv_gem_supports_syncobj_wait(fd
);
372 bool swizzled
= anv_gem_get_bit6_swizzle(fd
, I915_TILING_X
);
374 /* Starting with Gen10, the timestamp frequency of the command streamer may
375 * vary from one part to another. We can query the value from the kernel.
377 if (device
->info
.gen
>= 10) {
378 int timestamp_frequency
=
379 anv_gem_get_param(fd
, I915_PARAM_CS_TIMESTAMP_FREQUENCY
);
381 if (timestamp_frequency
< 0)
382 intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
384 device
->info
.timestamp_frequency
= timestamp_frequency
;
387 /* GENs prior to 8 do not support EU/Subslice info */
388 if (device
->info
.gen
>= 8) {
389 device
->subslice_total
= anv_gem_get_param(fd
, I915_PARAM_SUBSLICE_TOTAL
);
390 device
->eu_total
= anv_gem_get_param(fd
, I915_PARAM_EU_TOTAL
);
392 /* Without this information, we cannot get the right Braswell
393 * brandstrings, and we have to use conservative numbers for GPGPU on
394 * many platforms, but otherwise, things will just work.
396 if (device
->subslice_total
< 1 || device
->eu_total
< 1) {
397 intel_logw("Kernel 4.1 required to properly query GPU properties");
399 } else if (device
->info
.gen
== 7) {
400 device
->subslice_total
= 1 << (device
->info
.gt
- 1);
403 if (device
->info
.is_cherryview
&&
404 device
->subslice_total
> 0 && device
->eu_total
> 0) {
405 /* Logical CS threads = EUs per subslice * num threads per EU */
406 uint32_t max_cs_threads
=
407 device
->eu_total
/ device
->subslice_total
* device
->info
.num_thread_per_eu
;
409 /* Fuse configurations may give more threads than expected, never less. */
410 if (max_cs_threads
> device
->info
.max_cs_threads
)
411 device
->info
.max_cs_threads
= max_cs_threads
;
414 device
->compiler
= brw_compiler_create(NULL
, &device
->info
);
415 if (device
->compiler
== NULL
) {
416 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
419 device
->compiler
->shader_debug_log
= compiler_debug_log
;
420 device
->compiler
->shader_perf_log
= compiler_perf_log
;
421 device
->compiler
->supports_pull_constants
= false;
423 isl_device_init(&device
->isl_dev
, &device
->info
, swizzled
);
425 result
= anv_physical_device_init_uuids(device
);
426 if (result
!= VK_SUCCESS
)
429 result
= anv_init_wsi(device
);
430 if (result
!= VK_SUCCESS
) {
431 ralloc_free(device
->compiler
);
435 device
->local_fd
= fd
;
444 anv_physical_device_finish(struct anv_physical_device
*device
)
446 anv_finish_wsi(device
);
447 ralloc_free(device
->compiler
);
448 close(device
->local_fd
);
452 default_alloc_func(void *pUserData
, size_t size
, size_t align
,
453 VkSystemAllocationScope allocationScope
)
459 default_realloc_func(void *pUserData
, void *pOriginal
, size_t size
,
460 size_t align
, VkSystemAllocationScope allocationScope
)
462 return realloc(pOriginal
, size
);
466 default_free_func(void *pUserData
, void *pMemory
)
471 static const VkAllocationCallbacks default_alloc
= {
473 .pfnAllocation
= default_alloc_func
,
474 .pfnReallocation
= default_realloc_func
,
475 .pfnFree
= default_free_func
,
478 VkResult
anv_CreateInstance(
479 const VkInstanceCreateInfo
* pCreateInfo
,
480 const VkAllocationCallbacks
* pAllocator
,
481 VkInstance
* pInstance
)
483 struct anv_instance
*instance
;
485 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
);
487 /* Check if user passed a debug report callback to be used during
488 * Create/Destroy of instance.
490 const VkDebugReportCallbackCreateInfoEXT
*ctor_cb
=
491 vk_find_struct_const(pCreateInfo
->pNext
,
492 DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT
);
494 uint32_t client_version
;
495 if (pCreateInfo
->pApplicationInfo
&&
496 pCreateInfo
->pApplicationInfo
->apiVersion
!= 0) {
497 client_version
= pCreateInfo
->pApplicationInfo
->apiVersion
;
499 client_version
= VK_MAKE_VERSION(1, 0, 0);
502 if (VK_MAKE_VERSION(1, 0, 0) > client_version
||
503 client_version
> VK_MAKE_VERSION(1, 0, 0xfff)) {
505 if (ctor_cb
&& ctor_cb
->flags
& VK_DEBUG_REPORT_ERROR_BIT_EXT
)
506 ctor_cb
->pfnCallback(VK_DEBUG_REPORT_ERROR_BIT_EXT
,
507 VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT
,
508 VK_NULL_HANDLE
, /* No handle available yet. */
512 "incompatible driver version",
515 return vk_errorf(NULL
, NULL
, VK_ERROR_INCOMPATIBLE_DRIVER
,
516 "Client requested version %d.%d.%d",
517 VK_VERSION_MAJOR(client_version
),
518 VK_VERSION_MINOR(client_version
),
519 VK_VERSION_PATCH(client_version
));
522 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
523 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
524 if (!anv_instance_extension_supported(ext_name
))
525 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
528 instance
= vk_alloc2(&default_alloc
, pAllocator
, sizeof(*instance
), 8,
529 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
531 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
533 instance
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
536 instance
->alloc
= *pAllocator
;
538 instance
->alloc
= default_alloc
;
540 instance
->apiVersion
= client_version
;
541 instance
->physicalDeviceCount
= -1;
543 if (pthread_mutex_init(&instance
->callbacks_mutex
, NULL
) != 0) {
544 vk_free2(&default_alloc
, pAllocator
, instance
);
545 return vk_error(VK_ERROR_INITIALIZATION_FAILED
);
548 list_inithead(&instance
->callbacks
);
550 /* Store report debug callback to be used during DestroyInstance. */
552 instance
->destroy_debug_cb
.flags
= ctor_cb
->flags
;
553 instance
->destroy_debug_cb
.callback
= ctor_cb
->pfnCallback
;
554 instance
->destroy_debug_cb
.data
= ctor_cb
->pUserData
;
559 VG(VALGRIND_CREATE_MEMPOOL(instance
, 0, false));
561 *pInstance
= anv_instance_to_handle(instance
);
566 void anv_DestroyInstance(
567 VkInstance _instance
,
568 const VkAllocationCallbacks
* pAllocator
)
570 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
575 if (instance
->physicalDeviceCount
> 0) {
576 /* We support at most one physical device. */
577 assert(instance
->physicalDeviceCount
== 1);
578 anv_physical_device_finish(&instance
->physicalDevice
);
581 VG(VALGRIND_DESTROY_MEMPOOL(instance
));
583 pthread_mutex_destroy(&instance
->callbacks_mutex
);
587 vk_free(&instance
->alloc
, instance
);
591 anv_enumerate_devices(struct anv_instance
*instance
)
593 /* TODO: Check for more devices ? */
594 drmDevicePtr devices
[8];
595 VkResult result
= VK_ERROR_INCOMPATIBLE_DRIVER
;
598 instance
->physicalDeviceCount
= 0;
600 max_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
602 return VK_ERROR_INCOMPATIBLE_DRIVER
;
604 for (unsigned i
= 0; i
< (unsigned)max_devices
; i
++) {
605 if (devices
[i
]->available_nodes
& 1 << DRM_NODE_RENDER
&&
606 devices
[i
]->bustype
== DRM_BUS_PCI
&&
607 devices
[i
]->deviceinfo
.pci
->vendor_id
== 0x8086) {
609 result
= anv_physical_device_init(&instance
->physicalDevice
,
611 devices
[i
]->nodes
[DRM_NODE_RENDER
]);
612 if (result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
616 drmFreeDevices(devices
, max_devices
);
618 if (result
== VK_SUCCESS
)
619 instance
->physicalDeviceCount
= 1;
625 VkResult
anv_EnumeratePhysicalDevices(
626 VkInstance _instance
,
627 uint32_t* pPhysicalDeviceCount
,
628 VkPhysicalDevice
* pPhysicalDevices
)
630 ANV_FROM_HANDLE(anv_instance
, instance
, _instance
);
631 VK_OUTARRAY_MAKE(out
, pPhysicalDevices
, pPhysicalDeviceCount
);
634 if (instance
->physicalDeviceCount
< 0) {
635 result
= anv_enumerate_devices(instance
);
636 if (result
!= VK_SUCCESS
&&
637 result
!= VK_ERROR_INCOMPATIBLE_DRIVER
)
641 if (instance
->physicalDeviceCount
> 0) {
642 assert(instance
->physicalDeviceCount
== 1);
643 vk_outarray_append(&out
, i
) {
644 *i
= anv_physical_device_to_handle(&instance
->physicalDevice
);
648 return vk_outarray_status(&out
);
651 void anv_GetPhysicalDeviceFeatures(
652 VkPhysicalDevice physicalDevice
,
653 VkPhysicalDeviceFeatures
* pFeatures
)
655 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
657 *pFeatures
= (VkPhysicalDeviceFeatures
) {
658 .robustBufferAccess
= true,
659 .fullDrawIndexUint32
= true,
660 .imageCubeArray
= true,
661 .independentBlend
= true,
662 .geometryShader
= true,
663 .tessellationShader
= true,
664 .sampleRateShading
= true,
665 .dualSrcBlend
= true,
667 .multiDrawIndirect
= true,
668 .drawIndirectFirstInstance
= true,
670 .depthBiasClamp
= true,
671 .fillModeNonSolid
= true,
672 .depthBounds
= false,
676 .multiViewport
= true,
677 .samplerAnisotropy
= true,
678 .textureCompressionETC2
= pdevice
->info
.gen
>= 8 ||
679 pdevice
->info
.is_baytrail
,
680 .textureCompressionASTC_LDR
= pdevice
->info
.gen
>= 9, /* FINISHME CHV */
681 .textureCompressionBC
= true,
682 .occlusionQueryPrecise
= true,
683 .pipelineStatisticsQuery
= true,
684 .fragmentStoresAndAtomics
= true,
685 .shaderTessellationAndGeometryPointSize
= true,
686 .shaderImageGatherExtended
= true,
687 .shaderStorageImageExtendedFormats
= true,
688 .shaderStorageImageMultisample
= false,
689 .shaderStorageImageReadWithoutFormat
= false,
690 .shaderStorageImageWriteWithoutFormat
= true,
691 .shaderUniformBufferArrayDynamicIndexing
= true,
692 .shaderSampledImageArrayDynamicIndexing
= true,
693 .shaderStorageBufferArrayDynamicIndexing
= true,
694 .shaderStorageImageArrayDynamicIndexing
= true,
695 .shaderClipDistance
= true,
696 .shaderCullDistance
= true,
697 .shaderFloat64
= pdevice
->info
.gen
>= 8,
698 .shaderInt64
= pdevice
->info
.gen
>= 8,
699 .shaderInt16
= false,
700 .shaderResourceMinLod
= false,
701 .variableMultisampleRate
= false,
702 .inheritedQueries
= true,
705 /* We can't do image stores in vec4 shaders */
706 pFeatures
->vertexPipelineStoresAndAtomics
=
707 pdevice
->compiler
->scalar_stage
[MESA_SHADER_VERTEX
] &&
708 pdevice
->compiler
->scalar_stage
[MESA_SHADER_GEOMETRY
];
711 void anv_GetPhysicalDeviceFeatures2KHR(
712 VkPhysicalDevice physicalDevice
,
713 VkPhysicalDeviceFeatures2KHR
* pFeatures
)
715 anv_GetPhysicalDeviceFeatures(physicalDevice
, &pFeatures
->features
);
717 vk_foreach_struct(ext
, pFeatures
->pNext
) {
718 switch (ext
->sType
) {
719 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES_KHX
: {
720 VkPhysicalDeviceMultiviewFeaturesKHX
*features
=
721 (VkPhysicalDeviceMultiviewFeaturesKHX
*)ext
;
722 features
->multiview
= true;
723 features
->multiviewGeometryShader
= true;
724 features
->multiviewTessellationShader
= true;
728 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR
: {
729 VkPhysicalDeviceVariablePointerFeaturesKHR
*features
= (void *)ext
;
730 features
->variablePointersStorageBuffer
= true;
731 features
->variablePointers
= true;
735 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES_KHR
: {
736 VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR
*features
=
737 (VkPhysicalDeviceSamplerYcbcrConversionFeaturesKHR
*) ext
;
738 features
->samplerYcbcrConversion
= true;
743 anv_debug_ignored_stype(ext
->sType
);
749 void anv_GetPhysicalDeviceProperties(
750 VkPhysicalDevice physicalDevice
,
751 VkPhysicalDeviceProperties
* pProperties
)
753 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
754 const struct gen_device_info
*devinfo
= &pdevice
->info
;
756 /* See assertions made when programming the buffer surface state. */
757 const uint32_t max_raw_buffer_sz
= devinfo
->gen
>= 7 ?
758 (1ul << 30) : (1ul << 27);
760 const uint32_t max_samplers
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
) ?
763 VkSampleCountFlags sample_counts
=
764 isl_device_get_sample_counts(&pdevice
->isl_dev
);
766 VkPhysicalDeviceLimits limits
= {
767 .maxImageDimension1D
= (1 << 14),
768 .maxImageDimension2D
= (1 << 14),
769 .maxImageDimension3D
= (1 << 11),
770 .maxImageDimensionCube
= (1 << 14),
771 .maxImageArrayLayers
= (1 << 11),
772 .maxTexelBufferElements
= 128 * 1024 * 1024,
773 .maxUniformBufferRange
= (1ul << 27),
774 .maxStorageBufferRange
= max_raw_buffer_sz
,
775 .maxPushConstantsSize
= MAX_PUSH_CONSTANTS_SIZE
,
776 .maxMemoryAllocationCount
= UINT32_MAX
,
777 .maxSamplerAllocationCount
= 64 * 1024,
778 .bufferImageGranularity
= 64, /* A cache line */
779 .sparseAddressSpaceSize
= 0,
780 .maxBoundDescriptorSets
= MAX_SETS
,
781 .maxPerStageDescriptorSamplers
= max_samplers
,
782 .maxPerStageDescriptorUniformBuffers
= 64,
783 .maxPerStageDescriptorStorageBuffers
= 64,
784 .maxPerStageDescriptorSampledImages
= max_samplers
,
785 .maxPerStageDescriptorStorageImages
= 64,
786 .maxPerStageDescriptorInputAttachments
= 64,
787 .maxPerStageResources
= 250,
788 .maxDescriptorSetSamplers
= 256,
789 .maxDescriptorSetUniformBuffers
= 256,
790 .maxDescriptorSetUniformBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
791 .maxDescriptorSetStorageBuffers
= 256,
792 .maxDescriptorSetStorageBuffersDynamic
= MAX_DYNAMIC_BUFFERS
/ 2,
793 .maxDescriptorSetSampledImages
= 256,
794 .maxDescriptorSetStorageImages
= 256,
795 .maxDescriptorSetInputAttachments
= 256,
796 .maxVertexInputAttributes
= MAX_VBS
,
797 .maxVertexInputBindings
= MAX_VBS
,
798 .maxVertexInputAttributeOffset
= 2047,
799 .maxVertexInputBindingStride
= 2048,
800 .maxVertexOutputComponents
= 128,
801 .maxTessellationGenerationLevel
= 64,
802 .maxTessellationPatchSize
= 32,
803 .maxTessellationControlPerVertexInputComponents
= 128,
804 .maxTessellationControlPerVertexOutputComponents
= 128,
805 .maxTessellationControlPerPatchOutputComponents
= 128,
806 .maxTessellationControlTotalOutputComponents
= 2048,
807 .maxTessellationEvaluationInputComponents
= 128,
808 .maxTessellationEvaluationOutputComponents
= 128,
809 .maxGeometryShaderInvocations
= 32,
810 .maxGeometryInputComponents
= 64,
811 .maxGeometryOutputComponents
= 128,
812 .maxGeometryOutputVertices
= 256,
813 .maxGeometryTotalOutputComponents
= 1024,
814 .maxFragmentInputComponents
= 128,
815 .maxFragmentOutputAttachments
= 8,
816 .maxFragmentDualSrcAttachments
= 1,
817 .maxFragmentCombinedOutputResources
= 8,
818 .maxComputeSharedMemorySize
= 32768,
819 .maxComputeWorkGroupCount
= { 65535, 65535, 65535 },
820 .maxComputeWorkGroupInvocations
= 16 * devinfo
->max_cs_threads
,
821 .maxComputeWorkGroupSize
= {
822 16 * devinfo
->max_cs_threads
,
823 16 * devinfo
->max_cs_threads
,
824 16 * devinfo
->max_cs_threads
,
826 .subPixelPrecisionBits
= 4 /* FIXME */,
827 .subTexelPrecisionBits
= 4 /* FIXME */,
828 .mipmapPrecisionBits
= 4 /* FIXME */,
829 .maxDrawIndexedIndexValue
= UINT32_MAX
,
830 .maxDrawIndirectCount
= UINT32_MAX
,
831 .maxSamplerLodBias
= 16,
832 .maxSamplerAnisotropy
= 16,
833 .maxViewports
= MAX_VIEWPORTS
,
834 .maxViewportDimensions
= { (1 << 14), (1 << 14) },
835 .viewportBoundsRange
= { INT16_MIN
, INT16_MAX
},
836 .viewportSubPixelBits
= 13, /* We take a float? */
837 .minMemoryMapAlignment
= 4096, /* A page */
838 .minTexelBufferOffsetAlignment
= 1,
839 .minUniformBufferOffsetAlignment
= 16,
840 .minStorageBufferOffsetAlignment
= 4,
841 .minTexelOffset
= -8,
843 .minTexelGatherOffset
= -32,
844 .maxTexelGatherOffset
= 31,
845 .minInterpolationOffset
= -0.5,
846 .maxInterpolationOffset
= 0.4375,
847 .subPixelInterpolationOffsetBits
= 4,
848 .maxFramebufferWidth
= (1 << 14),
849 .maxFramebufferHeight
= (1 << 14),
850 .maxFramebufferLayers
= (1 << 11),
851 .framebufferColorSampleCounts
= sample_counts
,
852 .framebufferDepthSampleCounts
= sample_counts
,
853 .framebufferStencilSampleCounts
= sample_counts
,
854 .framebufferNoAttachmentsSampleCounts
= sample_counts
,
855 .maxColorAttachments
= MAX_RTS
,
856 .sampledImageColorSampleCounts
= sample_counts
,
857 .sampledImageIntegerSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
858 .sampledImageDepthSampleCounts
= sample_counts
,
859 .sampledImageStencilSampleCounts
= sample_counts
,
860 .storageImageSampleCounts
= VK_SAMPLE_COUNT_1_BIT
,
861 .maxSampleMaskWords
= 1,
862 .timestampComputeAndGraphics
= false,
863 .timestampPeriod
= 1000000000.0 / devinfo
->timestamp_frequency
,
864 .maxClipDistances
= 8,
865 .maxCullDistances
= 8,
866 .maxCombinedClipAndCullDistances
= 8,
867 .discreteQueuePriorities
= 1,
868 .pointSizeRange
= { 0.125, 255.875 },
869 .lineWidthRange
= { 0.0, 7.9921875 },
870 .pointSizeGranularity
= (1.0 / 8.0),
871 .lineWidthGranularity
= (1.0 / 128.0),
872 .strictLines
= false, /* FINISHME */
873 .standardSampleLocations
= true,
874 .optimalBufferCopyOffsetAlignment
= 128,
875 .optimalBufferCopyRowPitchAlignment
= 128,
876 .nonCoherentAtomSize
= 64,
879 *pProperties
= (VkPhysicalDeviceProperties
) {
880 .apiVersion
= anv_physical_device_api_version(pdevice
),
881 .driverVersion
= vk_get_driver_version(),
883 .deviceID
= pdevice
->chipset_id
,
884 .deviceType
= VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU
,
886 .sparseProperties
= {0}, /* Broadwell doesn't do sparse. */
889 snprintf(pProperties
->deviceName
, sizeof(pProperties
->deviceName
),
890 "%s", pdevice
->name
);
891 memcpy(pProperties
->pipelineCacheUUID
,
892 pdevice
->pipeline_cache_uuid
, VK_UUID_SIZE
);
895 void anv_GetPhysicalDeviceProperties2KHR(
896 VkPhysicalDevice physicalDevice
,
897 VkPhysicalDeviceProperties2KHR
* pProperties
)
899 ANV_FROM_HANDLE(anv_physical_device
, pdevice
, physicalDevice
);
901 anv_GetPhysicalDeviceProperties(physicalDevice
, &pProperties
->properties
);
903 vk_foreach_struct(ext
, pProperties
->pNext
) {
904 switch (ext
->sType
) {
905 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR
: {
906 VkPhysicalDevicePushDescriptorPropertiesKHR
*properties
=
907 (VkPhysicalDevicePushDescriptorPropertiesKHR
*) ext
;
909 properties
->maxPushDescriptors
= MAX_PUSH_DESCRIPTORS
;
913 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHR
: {
914 VkPhysicalDeviceIDPropertiesKHR
*id_props
=
915 (VkPhysicalDeviceIDPropertiesKHR
*)ext
;
916 memcpy(id_props
->deviceUUID
, pdevice
->device_uuid
, VK_UUID_SIZE
);
917 memcpy(id_props
->driverUUID
, pdevice
->driver_uuid
, VK_UUID_SIZE
);
918 /* The LUID is for Windows. */
919 id_props
->deviceLUIDValid
= false;
923 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES_KHX
: {
924 VkPhysicalDeviceMultiviewPropertiesKHX
*properties
=
925 (VkPhysicalDeviceMultiviewPropertiesKHX
*)ext
;
926 properties
->maxMultiviewViewCount
= 16;
927 properties
->maxMultiviewInstanceIndex
= UINT32_MAX
/ 16;
931 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES_KHR
: {
932 VkPhysicalDevicePointClippingPropertiesKHR
*properties
=
933 (VkPhysicalDevicePointClippingPropertiesKHR
*) ext
;
934 properties
->pointClippingBehavior
= VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR
;
935 anv_finishme("Implement pop-free point clipping");
940 anv_debug_ignored_stype(ext
->sType
);
946 /* We support exactly one queue family. */
947 static const VkQueueFamilyProperties
948 anv_queue_family_properties
= {
949 .queueFlags
= VK_QUEUE_GRAPHICS_BIT
|
950 VK_QUEUE_COMPUTE_BIT
|
951 VK_QUEUE_TRANSFER_BIT
,
953 .timestampValidBits
= 36, /* XXX: Real value here */
954 .minImageTransferGranularity
= { 1, 1, 1 },
957 void anv_GetPhysicalDeviceQueueFamilyProperties(
958 VkPhysicalDevice physicalDevice
,
960 VkQueueFamilyProperties
* pQueueFamilyProperties
)
962 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pCount
);
964 vk_outarray_append(&out
, p
) {
965 *p
= anv_queue_family_properties
;
969 void anv_GetPhysicalDeviceQueueFamilyProperties2KHR(
970 VkPhysicalDevice physicalDevice
,
971 uint32_t* pQueueFamilyPropertyCount
,
972 VkQueueFamilyProperties2KHR
* pQueueFamilyProperties
)
975 VK_OUTARRAY_MAKE(out
, pQueueFamilyProperties
, pQueueFamilyPropertyCount
);
977 vk_outarray_append(&out
, p
) {
978 p
->queueFamilyProperties
= anv_queue_family_properties
;
980 vk_foreach_struct(s
, p
->pNext
) {
981 anv_debug_ignored_stype(s
->sType
);
986 void anv_GetPhysicalDeviceMemoryProperties(
987 VkPhysicalDevice physicalDevice
,
988 VkPhysicalDeviceMemoryProperties
* pMemoryProperties
)
990 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
992 pMemoryProperties
->memoryTypeCount
= physical_device
->memory
.type_count
;
993 for (uint32_t i
= 0; i
< physical_device
->memory
.type_count
; i
++) {
994 pMemoryProperties
->memoryTypes
[i
] = (VkMemoryType
) {
995 .propertyFlags
= physical_device
->memory
.types
[i
].propertyFlags
,
996 .heapIndex
= physical_device
->memory
.types
[i
].heapIndex
,
1000 pMemoryProperties
->memoryHeapCount
= physical_device
->memory
.heap_count
;
1001 for (uint32_t i
= 0; i
< physical_device
->memory
.heap_count
; i
++) {
1002 pMemoryProperties
->memoryHeaps
[i
] = (VkMemoryHeap
) {
1003 .size
= physical_device
->memory
.heaps
[i
].size
,
1004 .flags
= physical_device
->memory
.heaps
[i
].flags
,
1009 void anv_GetPhysicalDeviceMemoryProperties2KHR(
1010 VkPhysicalDevice physicalDevice
,
1011 VkPhysicalDeviceMemoryProperties2KHR
* pMemoryProperties
)
1013 anv_GetPhysicalDeviceMemoryProperties(physicalDevice
,
1014 &pMemoryProperties
->memoryProperties
);
1016 vk_foreach_struct(ext
, pMemoryProperties
->pNext
) {
1017 switch (ext
->sType
) {
1019 anv_debug_ignored_stype(ext
->sType
);
1025 PFN_vkVoidFunction
anv_GetInstanceProcAddr(
1026 VkInstance instance
,
1029 return anv_lookup_entrypoint(NULL
, pName
);
1032 /* With version 1+ of the loader interface the ICD should expose
1033 * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
1036 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1037 VkInstance instance
,
1041 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
vk_icdGetInstanceProcAddr(
1042 VkInstance instance
,
1045 return anv_GetInstanceProcAddr(instance
, pName
);
1048 PFN_vkVoidFunction
anv_GetDeviceProcAddr(
1052 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1053 return anv_lookup_entrypoint(&device
->info
, pName
);
1057 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
1059 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1060 queue
->device
= device
;
1061 queue
->pool
= &device
->surface_state_pool
;
1065 anv_queue_finish(struct anv_queue
*queue
)
1069 static struct anv_state
1070 anv_state_pool_emit_data(struct anv_state_pool
*pool
, size_t size
, size_t align
, const void *p
)
1072 struct anv_state state
;
1074 state
= anv_state_pool_alloc(pool
, size
, align
);
1075 memcpy(state
.map
, p
, size
);
1077 anv_state_flush(pool
->block_pool
.device
, state
);
1082 struct gen8_border_color
{
1087 /* Pad out to 64 bytes */
1092 anv_device_init_border_colors(struct anv_device
*device
)
1094 static const struct gen8_border_color border_colors
[] = {
1095 [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 0.0 } },
1096 [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK
] = { .float32
= { 0.0, 0.0, 0.0, 1.0 } },
1097 [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
] = { .float32
= { 1.0, 1.0, 1.0, 1.0 } },
1098 [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK
] = { .uint32
= { 0, 0, 0, 0 } },
1099 [VK_BORDER_COLOR_INT_OPAQUE_BLACK
] = { .uint32
= { 0, 0, 0, 1 } },
1100 [VK_BORDER_COLOR_INT_OPAQUE_WHITE
] = { .uint32
= { 1, 1, 1, 1 } },
1103 device
->border_colors
= anv_state_pool_emit_data(&device
->dynamic_state_pool
,
1104 sizeof(border_colors
), 64,
1109 anv_device_init_trivial_batch(struct anv_device
*device
)
1111 anv_bo_init_new(&device
->trivial_batch_bo
, device
, 4096);
1113 if (device
->instance
->physicalDevice
.has_exec_async
)
1114 device
->trivial_batch_bo
.flags
|= EXEC_OBJECT_ASYNC
;
1116 void *map
= anv_gem_mmap(device
, device
->trivial_batch_bo
.gem_handle
,
1119 struct anv_batch batch
= {
1125 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1126 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1128 if (!device
->info
.has_llc
)
1129 gen_clflush_range(map
, batch
.next
- map
);
1131 anv_gem_munmap(map
, device
->trivial_batch_bo
.size
);
1134 VkResult
anv_CreateDevice(
1135 VkPhysicalDevice physicalDevice
,
1136 const VkDeviceCreateInfo
* pCreateInfo
,
1137 const VkAllocationCallbacks
* pAllocator
,
1140 ANV_FROM_HANDLE(anv_physical_device
, physical_device
, physicalDevice
);
1142 struct anv_device
*device
;
1144 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO
);
1146 for (uint32_t i
= 0; i
< pCreateInfo
->enabledExtensionCount
; i
++) {
1147 const char *ext_name
= pCreateInfo
->ppEnabledExtensionNames
[i
];
1148 if (!anv_physical_device_extension_supported(physical_device
, ext_name
))
1149 return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT
);
1152 /* Check enabled features */
1153 if (pCreateInfo
->pEnabledFeatures
) {
1154 VkPhysicalDeviceFeatures supported_features
;
1155 anv_GetPhysicalDeviceFeatures(physicalDevice
, &supported_features
);
1156 VkBool32
*supported_feature
= (VkBool32
*)&supported_features
;
1157 VkBool32
*enabled_feature
= (VkBool32
*)pCreateInfo
->pEnabledFeatures
;
1158 unsigned num_features
= sizeof(VkPhysicalDeviceFeatures
) / sizeof(VkBool32
);
1159 for (uint32_t i
= 0; i
< num_features
; i
++) {
1160 if (enabled_feature
[i
] && !supported_feature
[i
])
1161 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
1165 device
= vk_alloc2(&physical_device
->instance
->alloc
, pAllocator
,
1167 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1169 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1171 device
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
1172 device
->instance
= physical_device
->instance
;
1173 device
->chipset_id
= physical_device
->chipset_id
;
1174 device
->lost
= false;
1177 device
->alloc
= *pAllocator
;
1179 device
->alloc
= physical_device
->instance
->alloc
;
1181 /* XXX(chadv): Can we dup() physicalDevice->fd here? */
1182 device
->fd
= open(physical_device
->path
, O_RDWR
| O_CLOEXEC
);
1183 if (device
->fd
== -1) {
1184 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1188 device
->context_id
= anv_gem_create_context(device
);
1189 if (device
->context_id
== -1) {
1190 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1194 device
->info
= physical_device
->info
;
1195 device
->isl_dev
= physical_device
->isl_dev
;
1197 /* On Broadwell and later, we can use batch chaining to more efficiently
1198 * implement growing command buffers. Prior to Haswell, the kernel
1199 * command parser gets in the way and we have to fall back to growing
1202 device
->can_chain_batches
= device
->info
.gen
>= 8;
1204 device
->robust_buffer_access
= pCreateInfo
->pEnabledFeatures
&&
1205 pCreateInfo
->pEnabledFeatures
->robustBufferAccess
;
1207 if (pthread_mutex_init(&device
->mutex
, NULL
) != 0) {
1208 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1209 goto fail_context_id
;
1212 pthread_condattr_t condattr
;
1213 if (pthread_condattr_init(&condattr
) != 0) {
1214 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1217 if (pthread_condattr_setclock(&condattr
, CLOCK_MONOTONIC
) != 0) {
1218 pthread_condattr_destroy(&condattr
);
1219 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1222 if (pthread_cond_init(&device
->queue_submit
, NULL
) != 0) {
1223 pthread_condattr_destroy(&condattr
);
1224 result
= vk_error(VK_ERROR_INITIALIZATION_FAILED
);
1227 pthread_condattr_destroy(&condattr
);
1230 (physical_device
->supports_48bit_addresses
? EXEC_OBJECT_SUPPORTS_48B_ADDRESS
: 0) |
1231 (physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0) |
1232 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0);
1234 anv_bo_pool_init(&device
->batch_bo_pool
, device
, bo_flags
);
1236 result
= anv_bo_cache_init(&device
->bo_cache
);
1237 if (result
!= VK_SUCCESS
)
1238 goto fail_batch_bo_pool
;
1240 /* For the state pools we explicitly disable 48bit. */
1241 bo_flags
= physical_device
->has_exec_async
? EXEC_OBJECT_ASYNC
: 0;
1243 result
= anv_state_pool_init(&device
->dynamic_state_pool
, device
, 16384,
1245 if (result
!= VK_SUCCESS
)
1248 result
= anv_state_pool_init(&device
->instruction_state_pool
, device
, 16384,
1250 (physical_device
->has_exec_capture
? EXEC_OBJECT_CAPTURE
: 0));
1251 if (result
!= VK_SUCCESS
)
1252 goto fail_dynamic_state_pool
;
1254 result
= anv_state_pool_init(&device
->surface_state_pool
, device
, 4096,
1256 if (result
!= VK_SUCCESS
)
1257 goto fail_instruction_state_pool
;
1259 result
= anv_bo_init_new(&device
->workaround_bo
, device
, 1024);
1260 if (result
!= VK_SUCCESS
)
1261 goto fail_surface_state_pool
;
1263 anv_device_init_trivial_batch(device
);
1265 anv_scratch_pool_init(device
, &device
->scratch_pool
);
1267 anv_queue_init(device
, &device
->queue
);
1269 switch (device
->info
.gen
) {
1271 if (!device
->info
.is_haswell
)
1272 result
= gen7_init_device_state(device
);
1274 result
= gen75_init_device_state(device
);
1277 result
= gen8_init_device_state(device
);
1280 result
= gen9_init_device_state(device
);
1283 result
= gen10_init_device_state(device
);
1286 /* Shouldn't get here as we don't create physical devices for any other
1288 unreachable("unhandled gen");
1290 if (result
!= VK_SUCCESS
)
1291 goto fail_workaround_bo
;
1293 anv_device_init_blorp(device
);
1295 anv_device_init_border_colors(device
);
1297 *pDevice
= anv_device_to_handle(device
);
1302 anv_queue_finish(&device
->queue
);
1303 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1304 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1305 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1306 fail_surface_state_pool
:
1307 anv_state_pool_finish(&device
->surface_state_pool
);
1308 fail_instruction_state_pool
:
1309 anv_state_pool_finish(&device
->instruction_state_pool
);
1310 fail_dynamic_state_pool
:
1311 anv_state_pool_finish(&device
->dynamic_state_pool
);
1313 anv_bo_cache_finish(&device
->bo_cache
);
1315 anv_bo_pool_finish(&device
->batch_bo_pool
);
1316 pthread_cond_destroy(&device
->queue_submit
);
1318 pthread_mutex_destroy(&device
->mutex
);
1320 anv_gem_destroy_context(device
, device
->context_id
);
1324 vk_free(&device
->alloc
, device
);
1329 void anv_DestroyDevice(
1331 const VkAllocationCallbacks
* pAllocator
)
1333 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1338 anv_device_finish_blorp(device
);
1340 anv_queue_finish(&device
->queue
);
1342 #ifdef HAVE_VALGRIND
1343 /* We only need to free these to prevent valgrind errors. The backing
1344 * BO will go away in a couple of lines so we don't actually leak.
1346 anv_state_pool_free(&device
->dynamic_state_pool
, device
->border_colors
);
1349 anv_scratch_pool_finish(device
, &device
->scratch_pool
);
1351 anv_gem_munmap(device
->workaround_bo
.map
, device
->workaround_bo
.size
);
1352 anv_gem_close(device
, device
->workaround_bo
.gem_handle
);
1354 anv_gem_close(device
, device
->trivial_batch_bo
.gem_handle
);
1356 anv_state_pool_finish(&device
->surface_state_pool
);
1357 anv_state_pool_finish(&device
->instruction_state_pool
);
1358 anv_state_pool_finish(&device
->dynamic_state_pool
);
1360 anv_bo_cache_finish(&device
->bo_cache
);
1362 anv_bo_pool_finish(&device
->batch_bo_pool
);
1364 pthread_cond_destroy(&device
->queue_submit
);
1365 pthread_mutex_destroy(&device
->mutex
);
1367 anv_gem_destroy_context(device
, device
->context_id
);
1371 vk_free(&device
->alloc
, device
);
1374 VkResult
anv_EnumerateInstanceLayerProperties(
1375 uint32_t* pPropertyCount
,
1376 VkLayerProperties
* pProperties
)
1378 if (pProperties
== NULL
) {
1379 *pPropertyCount
= 0;
1383 /* None supported at this time */
1384 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1387 VkResult
anv_EnumerateDeviceLayerProperties(
1388 VkPhysicalDevice physicalDevice
,
1389 uint32_t* pPropertyCount
,
1390 VkLayerProperties
* pProperties
)
1392 if (pProperties
== NULL
) {
1393 *pPropertyCount
= 0;
1397 /* None supported at this time */
1398 return vk_error(VK_ERROR_LAYER_NOT_PRESENT
);
1401 void anv_GetDeviceQueue(
1403 uint32_t queueNodeIndex
,
1404 uint32_t queueIndex
,
1407 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1409 assert(queueIndex
== 0);
1411 *pQueue
= anv_queue_to_handle(&device
->queue
);
1415 anv_device_query_status(struct anv_device
*device
)
1417 /* This isn't likely as most of the callers of this function already check
1418 * for it. However, it doesn't hurt to check and it potentially lets us
1421 if (unlikely(device
->lost
))
1422 return VK_ERROR_DEVICE_LOST
;
1424 uint32_t active
, pending
;
1425 int ret
= anv_gem_gpu_get_reset_stats(device
, &active
, &pending
);
1427 /* We don't know the real error. */
1428 device
->lost
= true;
1429 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1430 "get_reset_stats failed: %m");
1434 device
->lost
= true;
1435 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1436 "GPU hung on one of our command buffers");
1437 } else if (pending
) {
1438 device
->lost
= true;
1439 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1440 "GPU hung with commands in-flight");
1447 anv_device_bo_busy(struct anv_device
*device
, struct anv_bo
*bo
)
1449 /* Note: This only returns whether or not the BO is in use by an i915 GPU.
1450 * Other usages of the BO (such as on different hardware) will not be
1451 * flagged as "busy" by this ioctl. Use with care.
1453 int ret
= anv_gem_busy(device
, bo
->gem_handle
);
1455 return VK_NOT_READY
;
1456 } else if (ret
== -1) {
1457 /* We don't know the real error. */
1458 device
->lost
= true;
1459 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1460 "gem wait failed: %m");
1463 /* Query for device status after the busy call. If the BO we're checking
1464 * got caught in a GPU hang we don't want to return VK_SUCCESS to the
1465 * client because it clearly doesn't have valid data. Yes, this most
1466 * likely means an ioctl, but we just did an ioctl to query the busy status
1467 * so it's no great loss.
1469 return anv_device_query_status(device
);
1473 anv_device_wait(struct anv_device
*device
, struct anv_bo
*bo
,
1476 int ret
= anv_gem_wait(device
, bo
->gem_handle
, &timeout
);
1477 if (ret
== -1 && errno
== ETIME
) {
1479 } else if (ret
== -1) {
1480 /* We don't know the real error. */
1481 device
->lost
= true;
1482 return vk_errorf(device
->instance
, device
, VK_ERROR_DEVICE_LOST
,
1483 "gem wait failed: %m");
1486 /* Query for device status after the wait. If the BO we're waiting on got
1487 * caught in a GPU hang we don't want to return VK_SUCCESS to the client
1488 * because it clearly doesn't have valid data. Yes, this most likely means
1489 * an ioctl, but we just did an ioctl to wait so it's no great loss.
1491 return anv_device_query_status(device
);
1494 VkResult
anv_DeviceWaitIdle(
1497 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1498 if (unlikely(device
->lost
))
1499 return VK_ERROR_DEVICE_LOST
;
1501 struct anv_batch batch
;
1504 batch
.start
= batch
.next
= cmds
;
1505 batch
.end
= (void *) cmds
+ sizeof(cmds
);
1507 anv_batch_emit(&batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
1508 anv_batch_emit(&batch
, GEN7_MI_NOOP
, noop
);
1510 return anv_device_submit_simple_batch(device
, &batch
);
1514 anv_bo_init_new(struct anv_bo
*bo
, struct anv_device
*device
, uint64_t size
)
1516 uint32_t gem_handle
= anv_gem_create(device
, size
);
1518 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1520 anv_bo_init(bo
, gem_handle
, size
);
1525 VkResult
anv_AllocateMemory(
1527 const VkMemoryAllocateInfo
* pAllocateInfo
,
1528 const VkAllocationCallbacks
* pAllocator
,
1529 VkDeviceMemory
* pMem
)
1531 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1532 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
1533 struct anv_device_memory
*mem
;
1534 VkResult result
= VK_SUCCESS
;
1536 assert(pAllocateInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO
);
1538 /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
1539 assert(pAllocateInfo
->allocationSize
> 0);
1541 /* The kernel relocation API has a limitation of a 32-bit delta value
1542 * applied to the address before it is written which, in spite of it being
1543 * unsigned, is treated as signed . Because of the way that this maps to
1544 * the Vulkan API, we cannot handle an offset into a buffer that does not
1545 * fit into a signed 32 bits. The only mechanism we have for dealing with
1546 * this at the moment is to limit all VkDeviceMemory objects to a maximum
1547 * of 2GB each. The Vulkan spec allows us to do this:
1549 * "Some platforms may have a limit on the maximum size of a single
1550 * allocation. For example, certain systems may fail to create
1551 * allocations with a size greater than or equal to 4GB. Such a limit is
1552 * implementation-dependent, and if such a failure occurs then the error
1553 * VK_ERROR_OUT_OF_DEVICE_MEMORY should be returned."
1555 * We don't use vk_error here because it's not an error so much as an
1556 * indication to the application that the allocation is too large.
1558 if (pAllocateInfo
->allocationSize
> (1ull << 31))
1559 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
1561 /* FINISHME: Fail if allocation request exceeds heap size. */
1563 mem
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*mem
), 8,
1564 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1566 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1568 assert(pAllocateInfo
->memoryTypeIndex
< pdevice
->memory
.type_count
);
1569 mem
->type
= &pdevice
->memory
.types
[pAllocateInfo
->memoryTypeIndex
];
1573 const VkImportMemoryFdInfoKHR
*fd_info
=
1574 vk_find_struct_const(pAllocateInfo
->pNext
, IMPORT_MEMORY_FD_INFO_KHR
);
1576 /* The Vulkan spec permits handleType to be 0, in which case the struct is
1579 if (fd_info
&& fd_info
->handleType
) {
1580 /* At the moment, we support only the below handle types. */
1581 assert(fd_info
->handleType
==
1582 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR
||
1583 fd_info
->handleType
==
1584 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
1586 result
= anv_bo_cache_import(device
, &device
->bo_cache
,
1587 fd_info
->fd
, &mem
->bo
);
1588 if (result
!= VK_SUCCESS
)
1591 VkDeviceSize aligned_alloc_size
=
1592 align_u64(pAllocateInfo
->allocationSize
, 4096);
1594 /* For security purposes, we reject importing the bo if it's smaller
1595 * than the requested allocation size. This prevents a malicious client
1596 * from passing a buffer to a trusted client, lying about the size, and
1597 * telling the trusted client to try and texture from an image that goes
1598 * out-of-bounds. This sort of thing could lead to GPU hangs or worse
1599 * in the trusted client. The trusted client can protect itself against
1600 * this sort of attack but only if it can trust the buffer size.
1602 if (mem
->bo
->size
< aligned_alloc_size
) {
1603 result
= vk_errorf(device
->instance
, device
,
1604 VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
,
1605 "aligned allocationSize too large for "
1606 "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR: "
1607 "%"PRIu64
"B > %"PRIu64
"B",
1608 aligned_alloc_size
, mem
->bo
->size
);
1609 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
1613 /* From the Vulkan spec:
1615 * "Importing memory from a file descriptor transfers ownership of
1616 * the file descriptor from the application to the Vulkan
1617 * implementation. The application must not perform any operations on
1618 * the file descriptor after a successful import."
1620 * If the import fails, we leave the file descriptor open.
1624 result
= anv_bo_cache_alloc(device
, &device
->bo_cache
,
1625 pAllocateInfo
->allocationSize
,
1627 if (result
!= VK_SUCCESS
)
1630 const VkMemoryDedicatedAllocateInfoKHR
*dedicated_info
=
1631 vk_find_struct_const(pAllocateInfo
->pNext
, MEMORY_DEDICATED_ALLOCATE_INFO_KHR
);
1632 if (dedicated_info
&& dedicated_info
->image
!= VK_NULL_HANDLE
) {
1633 ANV_FROM_HANDLE(anv_image
, image
, dedicated_info
->image
);
1635 /* For images using modifiers, we require a dedicated allocation
1636 * and we set the BO tiling to match the tiling of the underlying
1637 * modifier. This is a bit unfortunate as this is completely
1638 * pointless for Vulkan. However, GL needs to be able to map things
1639 * so it needs the tiling to be set. The only way to do this in a
1640 * non-racy way is to set the tiling in the creator of the BO so that
1643 * One of these days, once the GL driver learns to not map things
1644 * through the GTT in random places, we can drop this and start
1645 * allowing multiple modified images in the same BO.
1647 if (image
->drm_format_mod
!= DRM_FORMAT_MOD_INVALID
) {
1648 assert(isl_drm_modifier_get_info(image
->drm_format_mod
)->tiling
==
1649 image
->planes
[0].surface
.isl
.tiling
);
1650 const uint32_t i915_tiling
=
1651 isl_tiling_to_i915_tiling(image
->planes
[0].surface
.isl
.tiling
);
1652 int ret
= anv_gem_set_tiling(device
, mem
->bo
->gem_handle
,
1653 image
->planes
[0].surface
.isl
.row_pitch
,
1656 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
1657 return vk_errorf(device
->instance
, NULL
,
1658 VK_ERROR_OUT_OF_DEVICE_MEMORY
,
1659 "failed to set BO tiling: %m");
1665 assert(mem
->type
->heapIndex
< pdevice
->memory
.heap_count
);
1666 if (pdevice
->memory
.heaps
[mem
->type
->heapIndex
].supports_48bit_addresses
)
1667 mem
->bo
->flags
|= EXEC_OBJECT_SUPPORTS_48B_ADDRESS
;
1669 const struct wsi_memory_allocate_info
*wsi_info
=
1670 vk_find_struct_const(pAllocateInfo
->pNext
, WSI_MEMORY_ALLOCATE_INFO_MESA
);
1671 if (wsi_info
&& wsi_info
->implicit_sync
) {
1672 /* We need to set the WRITE flag on window system buffers so that GEM
1673 * will know we're writing to them and synchronize uses on other rings
1674 * (eg if the display server uses the blitter ring).
1676 mem
->bo
->flags
|= EXEC_OBJECT_WRITE
;
1677 } else if (pdevice
->has_exec_async
) {
1678 mem
->bo
->flags
|= EXEC_OBJECT_ASYNC
;
1681 *pMem
= anv_device_memory_to_handle(mem
);
1686 vk_free2(&device
->alloc
, pAllocator
, mem
);
1691 VkResult
anv_GetMemoryFdKHR(
1693 const VkMemoryGetFdInfoKHR
* pGetFdInfo
,
1696 ANV_FROM_HANDLE(anv_device
, dev
, device_h
);
1697 ANV_FROM_HANDLE(anv_device_memory
, mem
, pGetFdInfo
->memory
);
1699 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR
);
1701 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR
||
1702 pGetFdInfo
->handleType
== VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT
);
1704 return anv_bo_cache_export(dev
, &dev
->bo_cache
, mem
->bo
, pFd
);
1707 VkResult
anv_GetMemoryFdPropertiesKHR(
1709 VkExternalMemoryHandleTypeFlagBitsKHR handleType
,
1711 VkMemoryFdPropertiesKHR
* pMemoryFdProperties
)
1713 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1714 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
1716 switch (handleType
) {
1717 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR
:
1718 /* dma-buf can be imported as any memory type */
1719 pMemoryFdProperties
->memoryTypeBits
=
1720 (1 << pdevice
->memory
.type_count
) - 1;
1724 /* The valid usage section for this function says:
1726 * "handleType must not be one of the handle types defined as
1729 * So opaque handle types fall into the default "unsupported" case.
1731 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
);
1735 void anv_FreeMemory(
1737 VkDeviceMemory _mem
,
1738 const VkAllocationCallbacks
* pAllocator
)
1740 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1741 ANV_FROM_HANDLE(anv_device_memory
, mem
, _mem
);
1747 anv_UnmapMemory(_device
, _mem
);
1749 anv_bo_cache_release(device
, &device
->bo_cache
, mem
->bo
);
1751 vk_free2(&device
->alloc
, pAllocator
, mem
);
1754 VkResult
anv_MapMemory(
1756 VkDeviceMemory _memory
,
1757 VkDeviceSize offset
,
1759 VkMemoryMapFlags flags
,
1762 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1763 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1770 if (size
== VK_WHOLE_SIZE
)
1771 size
= mem
->bo
->size
- offset
;
1773 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
1775 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
1776 * assert(size != 0);
1777 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
1778 * equal to the size of the memory minus offset
1781 assert(offset
+ size
<= mem
->bo
->size
);
1783 /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
1784 * takes a VkDeviceMemory pointer, it seems like only one map of the memory
1785 * at a time is valid. We could just mmap up front and return an offset
1786 * pointer here, but that may exhaust virtual memory on 32 bit
1789 uint32_t gem_flags
= 0;
1791 if (!device
->info
.has_llc
&&
1792 (mem
->type
->propertyFlags
& VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
))
1793 gem_flags
|= I915_MMAP_WC
;
1795 /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
1796 uint64_t map_offset
= offset
& ~4095ull;
1797 assert(offset
>= map_offset
);
1798 uint64_t map_size
= (offset
+ size
) - map_offset
;
1800 /* Let's map whole pages */
1801 map_size
= align_u64(map_size
, 4096);
1803 void *map
= anv_gem_mmap(device
, mem
->bo
->gem_handle
,
1804 map_offset
, map_size
, gem_flags
);
1805 if (map
== MAP_FAILED
)
1806 return vk_error(VK_ERROR_MEMORY_MAP_FAILED
);
1809 mem
->map_size
= map_size
;
1811 *ppData
= mem
->map
+ (offset
- map_offset
);
1816 void anv_UnmapMemory(
1818 VkDeviceMemory _memory
)
1820 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
1825 anv_gem_munmap(mem
->map
, mem
->map_size
);
1832 clflush_mapped_ranges(struct anv_device
*device
,
1834 const VkMappedMemoryRange
*ranges
)
1836 for (uint32_t i
= 0; i
< count
; i
++) {
1837 ANV_FROM_HANDLE(anv_device_memory
, mem
, ranges
[i
].memory
);
1838 if (ranges
[i
].offset
>= mem
->map_size
)
1841 gen_clflush_range(mem
->map
+ ranges
[i
].offset
,
1842 MIN2(ranges
[i
].size
, mem
->map_size
- ranges
[i
].offset
));
1846 VkResult
anv_FlushMappedMemoryRanges(
1848 uint32_t memoryRangeCount
,
1849 const VkMappedMemoryRange
* pMemoryRanges
)
1851 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1853 if (device
->info
.has_llc
)
1856 /* Make sure the writes we're flushing have landed. */
1857 __builtin_ia32_mfence();
1859 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1864 VkResult
anv_InvalidateMappedMemoryRanges(
1866 uint32_t memoryRangeCount
,
1867 const VkMappedMemoryRange
* pMemoryRanges
)
1869 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1871 if (device
->info
.has_llc
)
1874 clflush_mapped_ranges(device
, memoryRangeCount
, pMemoryRanges
);
1876 /* Make sure no reads get moved up above the invalidate. */
1877 __builtin_ia32_mfence();
1882 void anv_GetBufferMemoryRequirements(
1885 VkMemoryRequirements
* pMemoryRequirements
)
1887 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
1888 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1889 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
1891 /* The Vulkan spec (git aaed022) says:
1893 * memoryTypeBits is a bitfield and contains one bit set for every
1894 * supported memory type for the resource. The bit `1<<i` is set if and
1895 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1896 * structure for the physical device is supported.
1898 uint32_t memory_types
= 0;
1899 for (uint32_t i
= 0; i
< pdevice
->memory
.type_count
; i
++) {
1900 uint32_t valid_usage
= pdevice
->memory
.types
[i
].valid_buffer_usage
;
1901 if ((valid_usage
& buffer
->usage
) == buffer
->usage
)
1902 memory_types
|= (1u << i
);
1905 pMemoryRequirements
->size
= buffer
->size
;
1906 pMemoryRequirements
->alignment
= 16;
1907 pMemoryRequirements
->memoryTypeBits
= memory_types
;
1910 void anv_GetBufferMemoryRequirements2KHR(
1912 const VkBufferMemoryRequirementsInfo2KHR
* pInfo
,
1913 VkMemoryRequirements2KHR
* pMemoryRequirements
)
1915 anv_GetBufferMemoryRequirements(_device
, pInfo
->buffer
,
1916 &pMemoryRequirements
->memoryRequirements
);
1918 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
1919 switch (ext
->sType
) {
1920 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR
: {
1921 VkMemoryDedicatedRequirementsKHR
*requirements
= (void *)ext
;
1922 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
1923 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
1928 anv_debug_ignored_stype(ext
->sType
);
1934 void anv_GetImageMemoryRequirements(
1937 VkMemoryRequirements
* pMemoryRequirements
)
1939 ANV_FROM_HANDLE(anv_image
, image
, _image
);
1940 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1941 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
1943 /* The Vulkan spec (git aaed022) says:
1945 * memoryTypeBits is a bitfield and contains one bit set for every
1946 * supported memory type for the resource. The bit `1<<i` is set if and
1947 * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
1948 * structure for the physical device is supported.
1950 * All types are currently supported for images.
1952 uint32_t memory_types
= (1ull << pdevice
->memory
.type_count
) - 1;
1954 pMemoryRequirements
->size
= image
->size
;
1955 pMemoryRequirements
->alignment
= image
->alignment
;
1956 pMemoryRequirements
->memoryTypeBits
= memory_types
;
1959 void anv_GetImageMemoryRequirements2KHR(
1961 const VkImageMemoryRequirementsInfo2KHR
* pInfo
,
1962 VkMemoryRequirements2KHR
* pMemoryRequirements
)
1964 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1965 ANV_FROM_HANDLE(anv_image
, image
, pInfo
->image
);
1967 anv_GetImageMemoryRequirements(_device
, pInfo
->image
,
1968 &pMemoryRequirements
->memoryRequirements
);
1970 vk_foreach_struct_const(ext
, pInfo
->pNext
) {
1971 switch (ext
->sType
) {
1972 case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO_KHR
: {
1973 struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
1974 const VkImagePlaneMemoryRequirementsInfoKHR
*plane_reqs
=
1975 (const VkImagePlaneMemoryRequirementsInfoKHR
*) ext
;
1976 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
,
1977 plane_reqs
->planeAspect
);
1979 assert(image
->planes
[plane
].offset
== 0);
1981 /* The Vulkan spec (git aaed022) says:
1983 * memoryTypeBits is a bitfield and contains one bit set for every
1984 * supported memory type for the resource. The bit `1<<i` is set
1985 * if and only if the memory type `i` in the
1986 * VkPhysicalDeviceMemoryProperties structure for the physical
1987 * device is supported.
1989 * All types are currently supported for images.
1991 pMemoryRequirements
->memoryRequirements
.memoryTypeBits
=
1992 (1ull << pdevice
->memory
.type_count
) - 1;
1994 pMemoryRequirements
->memoryRequirements
.size
= image
->planes
[plane
].size
;
1995 pMemoryRequirements
->memoryRequirements
.alignment
=
1996 image
->planes
[plane
].alignment
;
2001 anv_debug_ignored_stype(ext
->sType
);
2006 vk_foreach_struct(ext
, pMemoryRequirements
->pNext
) {
2007 switch (ext
->sType
) {
2008 case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR
: {
2009 VkMemoryDedicatedRequirementsKHR
*requirements
= (void *)ext
;
2010 if (image
->drm_format_mod
!= DRM_FORMAT_MOD_INVALID
) {
2011 /* Require a dedicated allocation for images with modifiers.
2013 * See also anv_AllocateMemory.
2015 requirements
->prefersDedicatedAllocation
= VK_TRUE
;
2016 requirements
->requiresDedicatedAllocation
= VK_TRUE
;
2018 requirements
->prefersDedicatedAllocation
= VK_FALSE
;
2019 requirements
->requiresDedicatedAllocation
= VK_FALSE
;
2025 anv_debug_ignored_stype(ext
->sType
);
2031 void anv_GetImageSparseMemoryRequirements(
2034 uint32_t* pSparseMemoryRequirementCount
,
2035 VkSparseImageMemoryRequirements
* pSparseMemoryRequirements
)
2037 *pSparseMemoryRequirementCount
= 0;
2040 void anv_GetImageSparseMemoryRequirements2KHR(
2042 const VkImageSparseMemoryRequirementsInfo2KHR
* pInfo
,
2043 uint32_t* pSparseMemoryRequirementCount
,
2044 VkSparseImageMemoryRequirements2KHR
* pSparseMemoryRequirements
)
2046 *pSparseMemoryRequirementCount
= 0;
2049 void anv_GetDeviceMemoryCommitment(
2051 VkDeviceMemory memory
,
2052 VkDeviceSize
* pCommittedMemoryInBytes
)
2054 *pCommittedMemoryInBytes
= 0;
2058 anv_bind_buffer_memory(const VkBindBufferMemoryInfoKHR
*pBindInfo
)
2060 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
2061 ANV_FROM_HANDLE(anv_buffer
, buffer
, pBindInfo
->buffer
);
2063 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR
);
2066 assert((buffer
->usage
& mem
->type
->valid_buffer_usage
) == buffer
->usage
);
2067 buffer
->bo
= mem
->bo
;
2068 buffer
->offset
= pBindInfo
->memoryOffset
;
2075 VkResult
anv_BindBufferMemory(
2078 VkDeviceMemory memory
,
2079 VkDeviceSize memoryOffset
)
2081 anv_bind_buffer_memory(
2082 &(VkBindBufferMemoryInfoKHR
) {
2083 .sType
= VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO_KHR
,
2086 .memoryOffset
= memoryOffset
,
2092 VkResult
anv_BindBufferMemory2KHR(
2094 uint32_t bindInfoCount
,
2095 const VkBindBufferMemoryInfoKHR
* pBindInfos
)
2097 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
2098 anv_bind_buffer_memory(&pBindInfos
[i
]);
2103 VkResult
anv_QueueBindSparse(
2105 uint32_t bindInfoCount
,
2106 const VkBindSparseInfo
* pBindInfo
,
2109 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
2110 if (unlikely(queue
->device
->lost
))
2111 return VK_ERROR_DEVICE_LOST
;
2113 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT
);
2118 VkResult
anv_CreateEvent(
2120 const VkEventCreateInfo
* pCreateInfo
,
2121 const VkAllocationCallbacks
* pAllocator
,
2124 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2125 struct anv_state state
;
2126 struct anv_event
*event
;
2128 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_EVENT_CREATE_INFO
);
2130 state
= anv_state_pool_alloc(&device
->dynamic_state_pool
,
2133 event
->state
= state
;
2134 event
->semaphore
= VK_EVENT_RESET
;
2136 if (!device
->info
.has_llc
) {
2137 /* Make sure the writes we're flushing have landed. */
2138 __builtin_ia32_mfence();
2139 __builtin_ia32_clflush(event
);
2142 *pEvent
= anv_event_to_handle(event
);
2147 void anv_DestroyEvent(
2150 const VkAllocationCallbacks
* pAllocator
)
2152 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2153 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2158 anv_state_pool_free(&device
->dynamic_state_pool
, event
->state
);
2161 VkResult
anv_GetEventStatus(
2165 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2166 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2168 if (unlikely(device
->lost
))
2169 return VK_ERROR_DEVICE_LOST
;
2171 if (!device
->info
.has_llc
) {
2172 /* Invalidate read cache before reading event written by GPU. */
2173 __builtin_ia32_clflush(event
);
2174 __builtin_ia32_mfence();
2178 return event
->semaphore
;
2181 VkResult
anv_SetEvent(
2185 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2186 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2188 event
->semaphore
= VK_EVENT_SET
;
2190 if (!device
->info
.has_llc
) {
2191 /* Make sure the writes we're flushing have landed. */
2192 __builtin_ia32_mfence();
2193 __builtin_ia32_clflush(event
);
2199 VkResult
anv_ResetEvent(
2203 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2204 ANV_FROM_HANDLE(anv_event
, event
, _event
);
2206 event
->semaphore
= VK_EVENT_RESET
;
2208 if (!device
->info
.has_llc
) {
2209 /* Make sure the writes we're flushing have landed. */
2210 __builtin_ia32_mfence();
2211 __builtin_ia32_clflush(event
);
2219 VkResult
anv_CreateBuffer(
2221 const VkBufferCreateInfo
* pCreateInfo
,
2222 const VkAllocationCallbacks
* pAllocator
,
2225 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2226 struct anv_buffer
*buffer
;
2228 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO
);
2230 buffer
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*buffer
), 8,
2231 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2233 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2235 buffer
->size
= pCreateInfo
->size
;
2236 buffer
->usage
= pCreateInfo
->usage
;
2240 *pBuffer
= anv_buffer_to_handle(buffer
);
2245 void anv_DestroyBuffer(
2248 const VkAllocationCallbacks
* pAllocator
)
2250 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2251 ANV_FROM_HANDLE(anv_buffer
, buffer
, _buffer
);
2256 vk_free2(&device
->alloc
, pAllocator
, buffer
);
2260 anv_fill_buffer_surface_state(struct anv_device
*device
, struct anv_state state
,
2261 enum isl_format format
,
2262 uint32_t offset
, uint32_t range
, uint32_t stride
)
2264 isl_buffer_fill_state(&device
->isl_dev
, state
.map
,
2266 .mocs
= device
->default_mocs
,
2271 anv_state_flush(device
, state
);
2274 void anv_DestroySampler(
2277 const VkAllocationCallbacks
* pAllocator
)
2279 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2280 ANV_FROM_HANDLE(anv_sampler
, sampler
, _sampler
);
2285 vk_free2(&device
->alloc
, pAllocator
, sampler
);
2288 VkResult
anv_CreateFramebuffer(
2290 const VkFramebufferCreateInfo
* pCreateInfo
,
2291 const VkAllocationCallbacks
* pAllocator
,
2292 VkFramebuffer
* pFramebuffer
)
2294 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2295 struct anv_framebuffer
*framebuffer
;
2297 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO
);
2299 size_t size
= sizeof(*framebuffer
) +
2300 sizeof(struct anv_image_view
*) * pCreateInfo
->attachmentCount
;
2301 framebuffer
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
2302 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
2303 if (framebuffer
== NULL
)
2304 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2306 framebuffer
->attachment_count
= pCreateInfo
->attachmentCount
;
2307 for (uint32_t i
= 0; i
< pCreateInfo
->attachmentCount
; i
++) {
2308 VkImageView _iview
= pCreateInfo
->pAttachments
[i
];
2309 framebuffer
->attachments
[i
] = anv_image_view_from_handle(_iview
);
2312 framebuffer
->width
= pCreateInfo
->width
;
2313 framebuffer
->height
= pCreateInfo
->height
;
2314 framebuffer
->layers
= pCreateInfo
->layers
;
2316 *pFramebuffer
= anv_framebuffer_to_handle(framebuffer
);
2321 void anv_DestroyFramebuffer(
2324 const VkAllocationCallbacks
* pAllocator
)
2326 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2327 ANV_FROM_HANDLE(anv_framebuffer
, fb
, _fb
);
2332 vk_free2(&device
->alloc
, pAllocator
, fb
);
2335 /* vk_icd.h does not declare this function, so we declare it here to
2336 * suppress Wmissing-prototypes.
2338 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2339 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
);
2341 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
2342 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion
)
2344 /* For the full details on loader interface versioning, see
2345 * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
2346 * What follows is a condensed summary, to help you navigate the large and
2347 * confusing official doc.
2349 * - Loader interface v0 is incompatible with later versions. We don't
2352 * - In loader interface v1:
2353 * - The first ICD entrypoint called by the loader is
2354 * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
2356 * - The ICD must statically expose no other Vulkan symbol unless it is
2357 * linked with -Bsymbolic.
2358 * - Each dispatchable Vulkan handle created by the ICD must be
2359 * a pointer to a struct whose first member is VK_LOADER_DATA. The
2360 * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
2361 * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
2362 * vkDestroySurfaceKHR(). The ICD must be capable of working with
2363 * such loader-managed surfaces.
2365 * - Loader interface v2 differs from v1 in:
2366 * - The first ICD entrypoint called by the loader is
2367 * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
2368 * statically expose this entrypoint.
2370 * - Loader interface v3 differs from v2 in:
2371 * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
2372 * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
2373 * because the loader no longer does so.
2375 *pSupportedVersion
= MIN2(*pSupportedVersion
, 3u);