1 /*
2  * Copyright © 2015 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  */
23 
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <xf86drm.h>
31 #include "drm-uapi/drm_fourcc.h"
32 
33 #include "anv_private.h"
34 #include "util/debug.h"
35 #include "util/build_id.h"
36 #include "util/disk_cache.h"
37 #include "util/mesa-sha1.h"
38 #include "util/os_file.h"
39 #include "util/os_misc.h"
40 #include "util/u_atomic.h"
41 #include "util/u_string.h"
42 #include "util/driconf.h"
43 #include "git_sha1.h"
44 #include "vk_util.h"
45 #include "common/gen_aux_map.h"
46 #include "common/gen_defines.h"
47 #include "compiler/glsl_types.h"
48 
49 #include "genxml/gen7_pack.h"
50 
51 #if DETECT_OS_FREEBSD
52 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC_FAST
53 #endif
54 
55 static const char anv_dri_options_xml[] =
56 DRI_CONF_BEGIN
57    DRI_CONF_SECTION_PERFORMANCE
58       DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
59       DRI_CONF_VK_X11_STRICT_IMAGE_COUNT("false")
60    DRI_CONF_SECTION_END
61 
62    DRI_CONF_SECTION_DEBUG
63       DRI_CONF_ALWAYS_FLUSH_CACHE("false")
64       DRI_CONF_VK_WSI_FORCE_BGRA8_UNORM_FIRST("false")
65    DRI_CONF_SECTION_END
66 DRI_CONF_END;
67 
68 /* This is probably far to big but it reflects the max size used for messages
69  * in OpenGLs KHR_debug.
70  */
71 #define MAX_DEBUG_MESSAGE_LENGTH    4096
72 
73 /* Render engine timestamp register */
74 #define TIMESTAMP 0x2358
75 
76 static void
compiler_debug_log(void * data,const char * fmt,...)77 compiler_debug_log(void *data, const char *fmt, ...)
78 {
79    char str[MAX_DEBUG_MESSAGE_LENGTH];
80    struct anv_device *device = (struct anv_device *)data;
81    struct anv_instance *instance = device->physical->instance;
82 
83    if (list_is_empty(&instance->debug_report_callbacks.callbacks))
84       return;
85 
86    va_list args;
87    va_start(args, fmt);
88    (void) vsnprintf(str, MAX_DEBUG_MESSAGE_LENGTH, fmt, args);
89    va_end(args);
90 
91    vk_debug_report(&instance->debug_report_callbacks,
92                    VK_DEBUG_REPORT_DEBUG_BIT_EXT,
93                    VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
94                    0, 0, 0, "anv", str);
95 }
96 
97 static void
compiler_perf_log(void * data,const char * fmt,...)98 compiler_perf_log(void *data, const char *fmt, ...)
99 {
100    va_list args;
101    va_start(args, fmt);
102 
103    if (unlikely(INTEL_DEBUG & DEBUG_PERF))
104       intel_logd_v(fmt, args);
105 
106    va_end(args);
107 }
108 
109 static uint64_t
anv_compute_heap_size(int fd,uint64_t gtt_size)110 anv_compute_heap_size(int fd, uint64_t gtt_size)
111 {
112    /* Query the total ram from the system */
113    uint64_t total_ram;
114    if (!os_get_total_physical_memory(&total_ram))
115       return 0;
116 
117    /* We don't want to burn too much ram with the GPU.  If the user has 4GiB
118     * or less, we use at most half.  If they have more than 4GiB, we use 3/4.
119     */
120    uint64_t available_ram;
121    if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
122       available_ram = total_ram / 2;
123    else
124       available_ram = total_ram * 3 / 4;
125 
126    /* We also want to leave some padding for things we allocate in the driver,
127     * so don't go over 3/4 of the GTT either.
128     */
129    uint64_t available_gtt = gtt_size * 3 / 4;
130 
131    return MIN2(available_ram, available_gtt);
132 }
133 
134 static VkResult
anv_physical_device_init_heaps(struct anv_physical_device * device,int fd)135 anv_physical_device_init_heaps(struct anv_physical_device *device, int fd)
136 {
137    if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
138                                  &device->gtt_size) == -1) {
139       /* If, for whatever reason, we can't actually get the GTT size from the
140        * kernel (too old?) fall back to the aperture size.
141        */
142       anv_perf_warn(NULL, NULL,
143                     "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
144 
145       if (gen_get_aperture_size(fd, &device->gtt_size) == -1) {
146          return vk_errorfi(device->instance, NULL,
147                            VK_ERROR_INITIALIZATION_FAILED,
148                            "failed to get aperture size: %m");
149       }
150    }
151 
152    /* We only allow 48-bit addresses with softpin because knowing the actual
153     * address is required for the vertex cache flush workaround.
154     */
155    device->supports_48bit_addresses = (device->info.gen >= 8) &&
156                                       device->has_softpin &&
157                                       device->gtt_size > (4ULL << 30 /* GiB */);
158 
159    uint64_t heap_size = anv_compute_heap_size(fd, device->gtt_size);
160 
161    if (heap_size > (2ull << 30) && !device->supports_48bit_addresses) {
162       /* When running with an overridden PCI ID, we may get a GTT size from
163        * the kernel that is greater than 2 GiB but the execbuf check for 48bit
164        * address support can still fail.  Just clamp the address space size to
165        * 2 GiB if we don't have 48-bit support.
166        */
167       intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
168                         "not support for 48-bit addresses",
169                         __FILE__, __LINE__);
170       heap_size = 2ull << 30;
171    }
172 
173    device->memory.heap_count = 1;
174    device->memory.heaps[0] = (struct anv_memory_heap) {
175       .size = heap_size,
176       .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
177    };
178 
179    uint32_t type_count = 0;
180    for (uint32_t heap = 0; heap < device->memory.heap_count; heap++) {
181       if (device->info.has_llc) {
182          /* Big core GPUs share LLC with the CPU and thus one memory type can be
183           * both cached and coherent at the same time.
184           */
185          device->memory.types[type_count++] = (struct anv_memory_type) {
186             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
187                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
188                              VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
189                              VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
190             .heapIndex = heap,
191          };
192       } else {
193          /* The spec requires that we expose a host-visible, coherent memory
194           * type, but Atom GPUs don't share LLC. Thus we offer two memory types
195           * to give the application a choice between cached, but not coherent and
196           * coherent but uncached (WC though).
197           */
198          device->memory.types[type_count++] = (struct anv_memory_type) {
199             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
200                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
201                              VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
202             .heapIndex = heap,
203          };
204          device->memory.types[type_count++] = (struct anv_memory_type) {
205             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
206                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
207                              VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
208             .heapIndex = heap,
209          };
210       }
211    }
212    device->memory.type_count = type_count;
213 
214    return VK_SUCCESS;
215 }
216 
217 static VkResult
anv_physical_device_init_uuids(struct anv_physical_device * device)218 anv_physical_device_init_uuids(struct anv_physical_device *device)
219 {
220    const struct build_id_note *note =
221       build_id_find_nhdr_for_addr(anv_physical_device_init_uuids);
222    if (!note) {
223       return vk_errorfi(device->instance, NULL,
224                         VK_ERROR_INITIALIZATION_FAILED,
225                         "Failed to find build-id");
226    }
227 
228    unsigned build_id_len = build_id_length(note);
229    if (build_id_len < 20) {
230       return vk_errorfi(device->instance, NULL,
231                         VK_ERROR_INITIALIZATION_FAILED,
232                         "build-id too short.  It needs to be a SHA");
233    }
234 
235    memcpy(device->driver_build_sha1, build_id_data(note), 20);
236 
237    struct mesa_sha1 sha1_ctx;
238    uint8_t sha1[20];
239    STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
240 
241    /* The pipeline cache UUID is used for determining when a pipeline cache is
242     * invalid.  It needs both a driver build and the PCI ID of the device.
243     */
244    _mesa_sha1_init(&sha1_ctx);
245    _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
246    _mesa_sha1_update(&sha1_ctx, &device->info.chipset_id,
247                      sizeof(device->info.chipset_id));
248    _mesa_sha1_update(&sha1_ctx, &device->always_use_bindless,
249                      sizeof(device->always_use_bindless));
250    _mesa_sha1_update(&sha1_ctx, &device->has_a64_buffer_access,
251                      sizeof(device->has_a64_buffer_access));
252    _mesa_sha1_update(&sha1_ctx, &device->has_bindless_images,
253                      sizeof(device->has_bindless_images));
254    _mesa_sha1_update(&sha1_ctx, &device->has_bindless_samplers,
255                      sizeof(device->has_bindless_samplers));
256    _mesa_sha1_final(&sha1_ctx, sha1);
257    memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
258 
259    /* The driver UUID is used for determining sharability of images and memory
260     * between two Vulkan instances in separate processes.  People who want to
261     * share memory need to also check the device UUID (below) so all this
262     * needs to be is the build-id.
263     */
264    memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
265 
266    /* The device UUID uniquely identifies the given device within the machine.
267     * Since we never have more than one device, this doesn't need to be a real
268     * UUID.  However, on the off-chance that someone tries to use this to
269     * cache pre-tiled images or something of the like, we use the PCI ID and
270     * some bits of ISL info to ensure that this is safe.
271     */
272    _mesa_sha1_init(&sha1_ctx);
273    _mesa_sha1_update(&sha1_ctx, &device->info.chipset_id,
274                      sizeof(device->info.chipset_id));
275    _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
276                      sizeof(device->isl_dev.has_bit6_swizzling));
277    _mesa_sha1_final(&sha1_ctx, sha1);
278    memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
279 
280    return VK_SUCCESS;
281 }
282 
283 static void
anv_physical_device_init_disk_cache(struct anv_physical_device * device)284 anv_physical_device_init_disk_cache(struct anv_physical_device *device)
285 {
286 #ifdef ENABLE_SHADER_CACHE
287    char renderer[10];
288    ASSERTED int len = snprintf(renderer, sizeof(renderer), "anv_%04x",
289                                device->info.chipset_id);
290    assert(len == sizeof(renderer) - 2);
291 
292    char timestamp[41];
293    _mesa_sha1_format(timestamp, device->driver_build_sha1);
294 
295    const uint64_t driver_flags =
296       brw_get_compiler_config_value(device->compiler);
297    device->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
298 #else
299    device->disk_cache = NULL;
300 #endif
301 }
302 
303 static void
anv_physical_device_free_disk_cache(struct anv_physical_device * device)304 anv_physical_device_free_disk_cache(struct anv_physical_device *device)
305 {
306 #ifdef ENABLE_SHADER_CACHE
307    if (device->disk_cache)
308       disk_cache_destroy(device->disk_cache);
309 #else
310    assert(device->disk_cache == NULL);
311 #endif
312 }
313 
314 static VkResult
anv_physical_device_try_create(struct anv_instance * instance,drmDevicePtr drm_device,struct anv_physical_device ** device_out)315 anv_physical_device_try_create(struct anv_instance *instance,
316                                drmDevicePtr drm_device,
317                                struct anv_physical_device **device_out)
318 {
319    const char *primary_path = drm_device->nodes[DRM_NODE_PRIMARY];
320    const char *path = drm_device->nodes[DRM_NODE_RENDER];
321    VkResult result;
322    int fd;
323    int master_fd = -1;
324 
325    brw_process_intel_debug_variable();
326 
327    fd = open(path, O_RDWR | O_CLOEXEC);
328    if (fd < 0)
329       return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
330 
331    struct gen_device_info devinfo;
332    if (!gen_get_device_info_from_fd(fd, &devinfo)) {
333       result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
334       goto fail_fd;
335    }
336 
337    const char *device_name = gen_get_device_name(devinfo.chipset_id);
338 
339    if (devinfo.is_haswell) {
340       intel_logw("Haswell Vulkan support is incomplete");
341    } else if (devinfo.gen == 7 && !devinfo.is_baytrail) {
342       intel_logw("Ivy Bridge Vulkan support is incomplete");
343    } else if (devinfo.gen == 7 && devinfo.is_baytrail) {
344       intel_logw("Bay Trail Vulkan support is incomplete");
345    } else if (devinfo.gen >= 8 && devinfo.gen <= 11) {
346       /* Gen8-11 fully supported */
347    } else if (devinfo.gen == 12) {
348       intel_logw("Vulkan is not yet fully supported on gen12");
349    } else {
350       result = vk_errorfi(instance, NULL, VK_ERROR_INCOMPATIBLE_DRIVER,
351                           "Vulkan not yet supported on %s", device_name);
352       goto fail_fd;
353    }
354 
355    struct anv_physical_device *device =
356       vk_alloc(&instance->alloc, sizeof(*device), 8,
357                VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
358    if (device == NULL) {
359       result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
360       goto fail_fd;
361    }
362 
363    vk_object_base_init(NULL, &device->base, VK_OBJECT_TYPE_PHYSICAL_DEVICE);
364    device->instance = instance;
365 
366    assert(strlen(path) < ARRAY_SIZE(device->path));
367    snprintf(device->path, ARRAY_SIZE(device->path), "%s", path);
368 
369    device->info = devinfo;
370    device->name = device_name;
371 
372    device->no_hw = device->info.no_hw;
373    if (getenv("INTEL_NO_HW") != NULL)
374       device->no_hw = true;
375 
376    device->pci_info.domain = drm_device->businfo.pci->domain;
377    device->pci_info.bus = drm_device->businfo.pci->bus;
378    device->pci_info.device = drm_device->businfo.pci->dev;
379    device->pci_info.function = drm_device->businfo.pci->func;
380 
381    device->cmd_parser_version = -1;
382    if (device->info.gen == 7) {
383       device->cmd_parser_version =
384          anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
385       if (device->cmd_parser_version == -1) {
386          result = vk_errorfi(device->instance, NULL,
387                              VK_ERROR_INITIALIZATION_FAILED,
388                              "failed to get command parser version");
389          goto fail_alloc;
390       }
391    }
392 
393    if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
394       result = vk_errorfi(device->instance, NULL,
395                           VK_ERROR_INITIALIZATION_FAILED,
396                           "kernel missing gem wait");
397       goto fail_alloc;
398    }
399 
400    if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
401       result = vk_errorfi(device->instance, NULL,
402                           VK_ERROR_INITIALIZATION_FAILED,
403                           "kernel missing execbuf2");
404       goto fail_alloc;
405    }
406 
407    if (!device->info.has_llc &&
408        anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
409       result = vk_errorfi(device->instance, NULL,
410                           VK_ERROR_INITIALIZATION_FAILED,
411                           "kernel missing wc mmap");
412       goto fail_alloc;
413    }
414 
415    device->has_softpin = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_SOFTPIN);
416    device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
417    device->has_exec_capture = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CAPTURE);
418    device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
419    device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
420    device->has_syncobj_wait = device->has_syncobj &&
421                               anv_gem_supports_syncobj_wait(fd);
422    device->has_context_priority = anv_gem_has_context_priority(fd);
423 
424    result = anv_physical_device_init_heaps(device, fd);
425    if (result != VK_SUCCESS)
426       goto fail_alloc;
427 
428    device->use_softpin = device->has_softpin &&
429                          device->supports_48bit_addresses;
430 
431    device->has_context_isolation =
432       anv_gem_get_param(fd, I915_PARAM_HAS_CONTEXT_ISOLATION);
433 
434    device->always_use_bindless =
435       env_var_as_boolean("ANV_ALWAYS_BINDLESS", false);
436 
437    device->use_call_secondary =
438       device->use_softpin &&
439       !env_var_as_boolean("ANV_DISABLE_SECONDARY_CMD_BUFFER_CALLS", false);
440 
441    /* We first got the A64 messages on broadwell and we can only use them if
442     * we can pass addresses directly into the shader which requires softpin.
443     */
444    device->has_a64_buffer_access = device->info.gen >= 8 &&
445                                    device->use_softpin;
446 
447    /* We first get bindless image access on Skylake and we can only really do
448     * it if we don't have any relocations so we need softpin.
449     */
450    device->has_bindless_images = device->info.gen >= 9 &&
451                                  device->use_softpin;
452 
453    /* We've had bindless samplers since Ivy Bridge (forever in Vulkan terms)
454     * because it's just a matter of setting the sampler address in the sample
455     * message header.  However, we've not bothered to wire it up for vec4 so
456     * we leave it disabled on gen7.
457     */
458    device->has_bindless_samplers = device->info.gen >= 8;
459 
460    device->has_implicit_ccs = device->info.has_aux_map;
461 
462    /* Check if we can read the GPU timestamp register from the CPU */
463    uint64_t u64_ignore;
464    device->has_reg_timestamp = anv_gem_reg_read(fd, TIMESTAMP | I915_REG_READ_8B_WA,
465                                                 &u64_ignore) == 0;
466 
467    uint64_t avail_mem;
468    device->has_mem_available = os_get_available_system_memory(&avail_mem);
469 
470    device->always_flush_cache =
471       driQueryOptionb(&instance->dri_options, "always_flush_cache");
472 
473    device->has_mmap_offset =
474       anv_gem_get_param(fd, I915_PARAM_MMAP_GTT_VERSION) >= 4;
475 
476    /* GENs prior to 8 do not support EU/Subslice info */
477    if (device->info.gen >= 8) {
478       device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
479       device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
480 
481       /* Without this information, we cannot get the right Braswell
482        * brandstrings, and we have to use conservative numbers for GPGPU on
483        * many platforms, but otherwise, things will just work.
484        */
485       if (device->subslice_total < 1 || device->eu_total < 1) {
486          intel_logw("Kernel 4.1 required to properly query GPU properties");
487       }
488    } else if (device->info.gen == 7) {
489       device->subslice_total = 1 << (device->info.gt - 1);
490    }
491 
492    if (device->info.is_cherryview &&
493        device->subslice_total > 0 && device->eu_total > 0) {
494       /* Logical CS threads = EUs per subslice * num threads per EU */
495       uint32_t max_cs_threads =
496          device->eu_total / device->subslice_total * device->info.num_thread_per_eu;
497 
498       /* Fuse configurations may give more threads than expected, never less. */
499       if (max_cs_threads > device->info.max_cs_threads)
500          device->info.max_cs_threads = max_cs_threads;
501    }
502 
503    device->compiler = brw_compiler_create(NULL, &device->info);
504    if (device->compiler == NULL) {
505       result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
506       goto fail_alloc;
507    }
508    device->compiler->shader_debug_log = compiler_debug_log;
509    device->compiler->shader_perf_log = compiler_perf_log;
510    device->compiler->supports_pull_constants = false;
511    device->compiler->constant_buffer_0_is_relative =
512       device->info.gen < 8 || !device->has_context_isolation;
513    device->compiler->supports_shader_constants = true;
514    device->compiler->compact_params = false;
515 
516    /* Broadwell PRM says:
517     *
518     *   "Before Gen8, there was a historical configuration control field to
519     *    swizzle address bit[6] for in X/Y tiling modes. This was set in three
520     *    different places: TILECTL[1:0], ARB_MODE[5:4], and
521     *    DISP_ARB_CTL[14:13].
522     *
523     *    For Gen8 and subsequent generations, the swizzle fields are all
524     *    reserved, and the CPU's memory controller performs all address
525     *    swizzling modifications."
526     */
527    bool swizzled =
528       device->info.gen < 8 && anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
529 
530    isl_device_init(&device->isl_dev, &device->info, swizzled);
531 
532    result = anv_physical_device_init_uuids(device);
533    if (result != VK_SUCCESS)
534       goto fail_compiler;
535 
536    anv_physical_device_init_disk_cache(device);
537 
538    if (instance->enabled_extensions.KHR_display) {
539       master_fd = open(primary_path, O_RDWR | O_CLOEXEC);
540       if (master_fd >= 0) {
541          /* prod the device with a GETPARAM call which will fail if
542           * we don't have permission to even render on this device
543           */
544          if (anv_gem_get_param(master_fd, I915_PARAM_CHIPSET_ID) == 0) {
545             close(master_fd);
546             master_fd = -1;
547          }
548       }
549    }
550    device->master_fd = master_fd;
551 
552    result = anv_init_wsi(device);
553    if (result != VK_SUCCESS)
554       goto fail_disk_cache;
555 
556    device->perf = anv_get_perf(&device->info, fd);
557 
558    anv_physical_device_get_supported_extensions(device,
559                                                 &device->supported_extensions);
560 
561 
562    device->local_fd = fd;
563 
564    *device_out = device;
565 
566    return VK_SUCCESS;
567 
568 fail_disk_cache:
569    anv_physical_device_free_disk_cache(device);
570 fail_compiler:
571    ralloc_free(device->compiler);
572 fail_alloc:
573    vk_free(&instance->alloc, device);
574 fail_fd:
575    close(fd);
576    if (master_fd != -1)
577       close(master_fd);
578    return result;
579 }
580 
581 static void
anv_physical_device_destroy(struct anv_physical_device * device)582 anv_physical_device_destroy(struct anv_physical_device *device)
583 {
584    anv_finish_wsi(device);
585    anv_physical_device_free_disk_cache(device);
586    ralloc_free(device->compiler);
587    ralloc_free(device->perf);
588    close(device->local_fd);
589    if (device->master_fd >= 0)
590       close(device->master_fd);
591    vk_object_base_finish(&device->base);
592    vk_free(&device->instance->alloc, device);
593 }
594 
595 static void *
default_alloc_func(void * pUserData,size_t size,size_t align,VkSystemAllocationScope allocationScope)596 default_alloc_func(void *pUserData, size_t size, size_t align,
597                    VkSystemAllocationScope allocationScope)
598 {
599    return malloc(size);
600 }
601 
602 static void *
default_realloc_func(void * pUserData,void * pOriginal,size_t size,size_t align,VkSystemAllocationScope allocationScope)603 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
604                      size_t align, VkSystemAllocationScope allocationScope)
605 {
606    return realloc(pOriginal, size);
607 }
608 
609 static void
default_free_func(void * pUserData,void * pMemory)610 default_free_func(void *pUserData, void *pMemory)
611 {
612    free(pMemory);
613 }
614 
615 static const VkAllocationCallbacks default_alloc = {
616    .pUserData = NULL,
617    .pfnAllocation = default_alloc_func,
618    .pfnReallocation = default_realloc_func,
619    .pfnFree = default_free_func,
620 };
621 
anv_EnumerateInstanceExtensionProperties(const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)622 VkResult anv_EnumerateInstanceExtensionProperties(
623     const char*                                 pLayerName,
624     uint32_t*                                   pPropertyCount,
625     VkExtensionProperties*                      pProperties)
626 {
627    VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
628 
629    for (int i = 0; i < ANV_INSTANCE_EXTENSION_COUNT; i++) {
630       if (anv_instance_extensions_supported.extensions[i]) {
631          vk_outarray_append(&out, prop) {
632             *prop = anv_instance_extensions[i];
633          }
634       }
635    }
636 
637    return vk_outarray_status(&out);
638 }
639 
anv_CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)640 VkResult anv_CreateInstance(
641     const VkInstanceCreateInfo*                 pCreateInfo,
642     const VkAllocationCallbacks*                pAllocator,
643     VkInstance*                                 pInstance)
644 {
645    struct anv_instance *instance;
646    VkResult result;
647 
648    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
649 
650    struct anv_instance_extension_table enabled_extensions = {};
651    for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
652       int idx;
653       for (idx = 0; idx < ANV_INSTANCE_EXTENSION_COUNT; idx++) {
654          if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
655                     anv_instance_extensions[idx].extensionName) == 0)
656             break;
657       }
658 
659       if (idx >= ANV_INSTANCE_EXTENSION_COUNT)
660          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
661 
662       if (!anv_instance_extensions_supported.extensions[idx])
663          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
664 
665       enabled_extensions.extensions[idx] = true;
666    }
667 
668    instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
669                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
670    if (!instance)
671       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
672 
673    vk_object_base_init(NULL, &instance->base, VK_OBJECT_TYPE_INSTANCE);
674 
675    if (pAllocator)
676       instance->alloc = *pAllocator;
677    else
678       instance->alloc = default_alloc;
679 
680    instance->app_info = (struct anv_app_info) { .api_version = 0 };
681    if (pCreateInfo->pApplicationInfo) {
682       const VkApplicationInfo *app = pCreateInfo->pApplicationInfo;
683 
684       instance->app_info.app_name =
685          vk_strdup(&instance->alloc, app->pApplicationName,
686                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
687       instance->app_info.app_version = app->applicationVersion;
688 
689       instance->app_info.engine_name =
690          vk_strdup(&instance->alloc, app->pEngineName,
691                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
692       instance->app_info.engine_version = app->engineVersion;
693 
694       instance->app_info.api_version = app->apiVersion;
695    }
696 
697    if (instance->app_info.api_version == 0)
698       instance->app_info.api_version = VK_API_VERSION_1_0;
699 
700    instance->enabled_extensions = enabled_extensions;
701 
702    for (unsigned i = 0; i < ARRAY_SIZE(instance->dispatch.entrypoints); i++) {
703       /* Vulkan requires that entrypoints for extensions which have not been
704        * enabled must not be advertised.
705        */
706       if (!anv_instance_entrypoint_is_enabled(i, instance->app_info.api_version,
707                                               &instance->enabled_extensions)) {
708          instance->dispatch.entrypoints[i] = NULL;
709       } else {
710          instance->dispatch.entrypoints[i] =
711             anv_instance_dispatch_table.entrypoints[i];
712       }
713    }
714 
715    for (unsigned i = 0; i < ARRAY_SIZE(instance->physical_device_dispatch.entrypoints); i++) {
716       /* Vulkan requires that entrypoints for extensions which have not been
717        * enabled must not be advertised.
718        */
719       if (!anv_physical_device_entrypoint_is_enabled(i, instance->app_info.api_version,
720                                                      &instance->enabled_extensions)) {
721          instance->physical_device_dispatch.entrypoints[i] = NULL;
722       } else {
723          instance->physical_device_dispatch.entrypoints[i] =
724             anv_physical_device_dispatch_table.entrypoints[i];
725       }
726    }
727 
728    for (unsigned i = 0; i < ARRAY_SIZE(instance->device_dispatch.entrypoints); i++) {
729       /* Vulkan requires that entrypoints for extensions which have not been
730        * enabled must not be advertised.
731        */
732       if (!anv_device_entrypoint_is_enabled(i, instance->app_info.api_version,
733                                             &instance->enabled_extensions, NULL)) {
734          instance->device_dispatch.entrypoints[i] = NULL;
735       } else {
736          instance->device_dispatch.entrypoints[i] =
737             anv_device_dispatch_table.entrypoints[i];
738       }
739    }
740 
741    instance->physical_devices_enumerated = false;
742    list_inithead(&instance->physical_devices);
743 
744    result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
745    if (result != VK_SUCCESS) {
746       vk_free2(&default_alloc, pAllocator, instance);
747       return vk_error(result);
748    }
749 
750    instance->pipeline_cache_enabled =
751       env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
752 
753    glsl_type_singleton_init_or_ref();
754 
755    VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
756 
757    driParseOptionInfo(&instance->available_dri_options, anv_dri_options_xml);
758    driParseConfigFiles(&instance->dri_options, &instance->available_dri_options,
759                        0, "anv", NULL,
760                        instance->app_info.app_name,
761                        instance->app_info.app_version,
762                        instance->app_info.engine_name,
763                        instance->app_info.engine_version);
764 
765    *pInstance = anv_instance_to_handle(instance);
766 
767    return VK_SUCCESS;
768 }
769 
anv_DestroyInstance(VkInstance _instance,const VkAllocationCallbacks * pAllocator)770 void anv_DestroyInstance(
771     VkInstance                                  _instance,
772     const VkAllocationCallbacks*                pAllocator)
773 {
774    ANV_FROM_HANDLE(anv_instance, instance, _instance);
775 
776    if (!instance)
777       return;
778 
779    list_for_each_entry_safe(struct anv_physical_device, pdevice,
780                             &instance->physical_devices, link)
781       anv_physical_device_destroy(pdevice);
782 
783    vk_free(&instance->alloc, (char *)instance->app_info.app_name);
784    vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
785 
786    VG(VALGRIND_DESTROY_MEMPOOL(instance));
787 
788    vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
789 
790    glsl_type_singleton_decref();
791 
792    driDestroyOptionCache(&instance->dri_options);
793    driDestroyOptionInfo(&instance->available_dri_options);
794 
795    vk_object_base_finish(&instance->base);
796    vk_free(&instance->alloc, instance);
797 }
798 
799 static VkResult
anv_enumerate_physical_devices(struct anv_instance * instance)800 anv_enumerate_physical_devices(struct anv_instance *instance)
801 {
802    if (instance->physical_devices_enumerated)
803       return VK_SUCCESS;
804 
805    instance->physical_devices_enumerated = true;
806 
807    /* TODO: Check for more devices ? */
808    drmDevicePtr devices[8];
809    int max_devices;
810 
811    max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
812    if (max_devices < 1)
813       return VK_SUCCESS;
814 
815    VkResult result = VK_SUCCESS;
816    for (unsigned i = 0; i < (unsigned)max_devices; i++) {
817       if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
818           devices[i]->bustype == DRM_BUS_PCI &&
819           devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
820 
821          struct anv_physical_device *pdevice;
822          result = anv_physical_device_try_create(instance, devices[i],
823                                                  &pdevice);
824          /* Incompatible DRM device, skip. */
825          if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
826             result = VK_SUCCESS;
827             continue;
828          }
829 
830          /* Error creating the physical device, report the error. */
831          if (result != VK_SUCCESS)
832             break;
833 
834          list_addtail(&pdevice->link, &instance->physical_devices);
835       }
836    }
837    drmFreeDevices(devices, max_devices);
838 
839    /* If we successfully enumerated any devices, call it success */
840    return result;
841 }
842 
anv_EnumeratePhysicalDevices(VkInstance _instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)843 VkResult anv_EnumeratePhysicalDevices(
844     VkInstance                                  _instance,
845     uint32_t*                                   pPhysicalDeviceCount,
846     VkPhysicalDevice*                           pPhysicalDevices)
847 {
848    ANV_FROM_HANDLE(anv_instance, instance, _instance);
849    VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
850 
851    VkResult result = anv_enumerate_physical_devices(instance);
852    if (result != VK_SUCCESS)
853       return result;
854 
855    list_for_each_entry(struct anv_physical_device, pdevice,
856                        &instance->physical_devices, link) {
857       vk_outarray_append(&out, i) {
858          *i = anv_physical_device_to_handle(pdevice);
859       }
860    }
861 
862    return vk_outarray_status(&out);
863 }
864 
anv_EnumeratePhysicalDeviceGroups(VkInstance _instance,uint32_t * pPhysicalDeviceGroupCount,VkPhysicalDeviceGroupProperties * pPhysicalDeviceGroupProperties)865 VkResult anv_EnumeratePhysicalDeviceGroups(
866     VkInstance                                  _instance,
867     uint32_t*                                   pPhysicalDeviceGroupCount,
868     VkPhysicalDeviceGroupProperties*            pPhysicalDeviceGroupProperties)
869 {
870    ANV_FROM_HANDLE(anv_instance, instance, _instance);
871    VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
872                          pPhysicalDeviceGroupCount);
873 
874    VkResult result = anv_enumerate_physical_devices(instance);
875    if (result != VK_SUCCESS)
876       return result;
877 
878    list_for_each_entry(struct anv_physical_device, pdevice,
879                        &instance->physical_devices, link) {
880       vk_outarray_append(&out, p) {
881          p->physicalDeviceCount = 1;
882          memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
883          p->physicalDevices[0] = anv_physical_device_to_handle(pdevice);
884          p->subsetAllocation = false;
885 
886          vk_foreach_struct(ext, p->pNext)
887             anv_debug_ignored_stype(ext->sType);
888       }
889    }
890 
891    return vk_outarray_status(&out);
892 }
893 
anv_GetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,VkPhysicalDeviceFeatures * pFeatures)894 void anv_GetPhysicalDeviceFeatures(
895     VkPhysicalDevice                            physicalDevice,
896     VkPhysicalDeviceFeatures*                   pFeatures)
897 {
898    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
899 
900    *pFeatures = (VkPhysicalDeviceFeatures) {
901       .robustBufferAccess                       = true,
902       .fullDrawIndexUint32                      = true,
903       .imageCubeArray                           = true,
904       .independentBlend                         = true,
905       .geometryShader                           = true,
906       .tessellationShader                       = true,
907       .sampleRateShading                        = true,
908       .dualSrcBlend                             = true,
909       .logicOp                                  = true,
910       .multiDrawIndirect                        = true,
911       .drawIndirectFirstInstance                = true,
912       .depthClamp                               = true,
913       .depthBiasClamp                           = true,
914       .fillModeNonSolid                         = true,
915       .depthBounds                              = pdevice->info.gen >= 12,
916       .wideLines                                = true,
917       .largePoints                              = true,
918       .alphaToOne                               = true,
919       .multiViewport                            = true,
920       .samplerAnisotropy                        = true,
921       .textureCompressionETC2                   = pdevice->info.gen >= 8 ||
922                                                   pdevice->info.is_baytrail,
923       .textureCompressionASTC_LDR               = pdevice->info.gen >= 9, /* FINISHME CHV */
924       .textureCompressionBC                     = true,
925       .occlusionQueryPrecise                    = true,
926       .pipelineStatisticsQuery                  = true,
927       .fragmentStoresAndAtomics                 = true,
928       .shaderTessellationAndGeometryPointSize   = true,
929       .shaderImageGatherExtended                = true,
930       .shaderStorageImageExtendedFormats        = true,
931       .shaderStorageImageMultisample            = false,
932       .shaderStorageImageReadWithoutFormat      = false,
933       .shaderStorageImageWriteWithoutFormat     = true,
934       .shaderUniformBufferArrayDynamicIndexing  = true,
935       .shaderSampledImageArrayDynamicIndexing   = true,
936       .shaderStorageBufferArrayDynamicIndexing  = true,
937       .shaderStorageImageArrayDynamicIndexing   = true,
938       .shaderClipDistance                       = true,
939       .shaderCullDistance                       = true,
940       .shaderFloat64                            = pdevice->info.gen >= 8 &&
941                                                   pdevice->info.has_64bit_float,
942       .shaderInt64                              = pdevice->info.gen >= 8 &&
943                                                   pdevice->info.has_64bit_int,
944       .shaderInt16                              = pdevice->info.gen >= 8,
945       .shaderResourceMinLod                     = pdevice->info.gen >= 9,
946       .variableMultisampleRate                  = true,
947       .inheritedQueries                         = true,
948    };
949 
950    /* We can't do image stores in vec4 shaders */
951    pFeatures->vertexPipelineStoresAndAtomics =
952       pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
953       pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
954 
955    struct anv_app_info *app_info = &pdevice->instance->app_info;
956 
957    /* The new DOOM and Wolfenstein games require depthBounds without
958     * checking for it.  They seem to run fine without it so just claim it's
959     * there and accept the consequences.
960     */
961    if (app_info->engine_name && strcmp(app_info->engine_name, "idTech") == 0)
962       pFeatures->depthBounds = true;
963 }
964 
965 static void
anv_get_physical_device_features_1_1(struct anv_physical_device * pdevice,VkPhysicalDeviceVulkan11Features * f)966 anv_get_physical_device_features_1_1(struct anv_physical_device *pdevice,
967                                      VkPhysicalDeviceVulkan11Features *f)
968 {
969    assert(f->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES);
970 
971    f->storageBuffer16BitAccess            = pdevice->info.gen >= 8;
972    f->uniformAndStorageBuffer16BitAccess  = pdevice->info.gen >= 8;
973    f->storagePushConstant16               = pdevice->info.gen >= 8;
974    f->storageInputOutput16                = false;
975    f->multiview                           = true;
976    f->multiviewGeometryShader             = true;
977    f->multiviewTessellationShader         = true;
978    f->variablePointersStorageBuffer       = true;
979    f->variablePointers                    = true;
980    f->protectedMemory                     = false;
981    f->samplerYcbcrConversion              = true;
982    f->shaderDrawParameters                = true;
983 }
984 
985 static void
anv_get_physical_device_features_1_2(struct anv_physical_device * pdevice,VkPhysicalDeviceVulkan12Features * f)986 anv_get_physical_device_features_1_2(struct anv_physical_device *pdevice,
987                                      VkPhysicalDeviceVulkan12Features *f)
988 {
989    assert(f->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES);
990 
991    f->samplerMirrorClampToEdge            = true;
992    f->drawIndirectCount                   = true;
993    f->storageBuffer8BitAccess             = pdevice->info.gen >= 8;
994    f->uniformAndStorageBuffer8BitAccess   = pdevice->info.gen >= 8;
995    f->storagePushConstant8                = pdevice->info.gen >= 8;
996    f->shaderBufferInt64Atomics            = pdevice->info.gen >= 9 &&
997                                             pdevice->use_softpin;
998    f->shaderSharedInt64Atomics            = false;
999    f->shaderFloat16                       = pdevice->info.gen >= 8;
1000    f->shaderInt8                          = pdevice->info.gen >= 8;
1001 
1002    bool descIndexing = pdevice->has_a64_buffer_access &&
1003                        pdevice->has_bindless_images;
1004    f->descriptorIndexing                                 = descIndexing;
1005    f->shaderInputAttachmentArrayDynamicIndexing          = false;
1006    f->shaderUniformTexelBufferArrayDynamicIndexing       = descIndexing;
1007    f->shaderStorageTexelBufferArrayDynamicIndexing       = descIndexing;
1008    f->shaderUniformBufferArrayNonUniformIndexing         = false;
1009    f->shaderSampledImageArrayNonUniformIndexing          = descIndexing;
1010    f->shaderStorageBufferArrayNonUniformIndexing         = descIndexing;
1011    f->shaderStorageImageArrayNonUniformIndexing          = descIndexing;
1012    f->shaderInputAttachmentArrayNonUniformIndexing       = false;
1013    f->shaderUniformTexelBufferArrayNonUniformIndexing    = descIndexing;
1014    f->shaderStorageTexelBufferArrayNonUniformIndexing    = descIndexing;
1015    f->descriptorBindingUniformBufferUpdateAfterBind      = false;
1016    f->descriptorBindingSampledImageUpdateAfterBind       = descIndexing;
1017    f->descriptorBindingStorageImageUpdateAfterBind       = descIndexing;
1018    f->descriptorBindingStorageBufferUpdateAfterBind      = descIndexing;
1019    f->descriptorBindingUniformTexelBufferUpdateAfterBind = descIndexing;
1020    f->descriptorBindingStorageTexelBufferUpdateAfterBind = descIndexing;
1021    f->descriptorBindingUpdateUnusedWhilePending          = descIndexing;
1022    f->descriptorBindingPartiallyBound                    = descIndexing;
1023    f->descriptorBindingVariableDescriptorCount           = false;
1024    f->runtimeDescriptorArray                             = descIndexing;
1025 
1026    f->samplerFilterMinmax                 = pdevice->info.gen >= 9;
1027    f->scalarBlockLayout                   = true;
1028    f->imagelessFramebuffer                = true;
1029    f->uniformBufferStandardLayout         = true;
1030    f->shaderSubgroupExtendedTypes         = true;
1031    f->separateDepthStencilLayouts         = true;
1032    f->hostQueryReset                      = true;
1033    f->timelineSemaphore                   = true;
1034    f->bufferDeviceAddress                 = pdevice->has_a64_buffer_access;
1035    f->bufferDeviceAddressCaptureReplay    = pdevice->has_a64_buffer_access;
1036    f->bufferDeviceAddressMultiDevice      = false;
1037    f->vulkanMemoryModel                   = true;
1038    f->vulkanMemoryModelDeviceScope        = true;
1039    f->vulkanMemoryModelAvailabilityVisibilityChains = true;
1040    f->shaderOutputViewportIndex           = true;
1041    f->shaderOutputLayer                   = true;
1042    f->subgroupBroadcastDynamicId          = true;
1043 }
1044 
anv_GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceFeatures2 * pFeatures)1045 void anv_GetPhysicalDeviceFeatures2(
1046     VkPhysicalDevice                            physicalDevice,
1047     VkPhysicalDeviceFeatures2*                  pFeatures)
1048 {
1049    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
1050    anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
1051 
1052    VkPhysicalDeviceVulkan11Features core_1_1 = {
1053       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES,
1054    };
1055    anv_get_physical_device_features_1_1(pdevice, &core_1_1);
1056 
1057    VkPhysicalDeviceVulkan12Features core_1_2 = {
1058       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
1059    };
1060    anv_get_physical_device_features_1_2(pdevice, &core_1_2);
1061 
1062 #define CORE_FEATURE(major, minor, feature) \
1063    features->feature = core_##major##_##minor.feature
1064 
1065 
1066    vk_foreach_struct(ext, pFeatures->pNext) {
1067       switch (ext->sType) {
1068       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_4444_FORMATS_FEATURES_EXT: {
1069          VkPhysicalDevice4444FormatsFeaturesEXT *features =
1070             (VkPhysicalDevice4444FormatsFeaturesEXT *)ext;
1071          features->formatA4R4G4B4 = true;
1072          features->formatA4B4G4R4 = false;
1073          break;
1074       }
1075 
1076       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
1077          VkPhysicalDevice8BitStorageFeaturesKHR *features =
1078             (VkPhysicalDevice8BitStorageFeaturesKHR *)ext;
1079          CORE_FEATURE(1, 2, storageBuffer8BitAccess);
1080          CORE_FEATURE(1, 2, uniformAndStorageBuffer8BitAccess);
1081          CORE_FEATURE(1, 2, storagePushConstant8);
1082          break;
1083       }
1084 
1085       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
1086          VkPhysicalDevice16BitStorageFeatures *features =
1087             (VkPhysicalDevice16BitStorageFeatures *)ext;
1088          CORE_FEATURE(1, 1, storageBuffer16BitAccess);
1089          CORE_FEATURE(1, 1, uniformAndStorageBuffer16BitAccess);
1090          CORE_FEATURE(1, 1, storagePushConstant16);
1091          CORE_FEATURE(1, 1, storageInputOutput16);
1092          break;
1093       }
1094 
1095       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT: {
1096          VkPhysicalDeviceBufferDeviceAddressFeaturesEXT *features = (void *)ext;
1097          features->bufferDeviceAddress = pdevice->has_a64_buffer_access;
1098          features->bufferDeviceAddressCaptureReplay = false;
1099          features->bufferDeviceAddressMultiDevice = false;
1100          break;
1101       }
1102 
1103       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_KHR: {
1104          VkPhysicalDeviceBufferDeviceAddressFeaturesKHR *features = (void *)ext;
1105          CORE_FEATURE(1, 2, bufferDeviceAddress);
1106          CORE_FEATURE(1, 2, bufferDeviceAddressCaptureReplay);
1107          CORE_FEATURE(1, 2, bufferDeviceAddressMultiDevice);
1108          break;
1109       }
1110 
1111       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV: {
1112          VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *features =
1113             (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *)ext;
1114          features->computeDerivativeGroupQuads = true;
1115          features->computeDerivativeGroupLinear = true;
1116          break;
1117       }
1118 
1119       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
1120          VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
1121             (VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
1122          features->conditionalRendering = pdevice->info.gen >= 8 ||
1123                                           pdevice->info.is_haswell;
1124          features->inheritedConditionalRendering = pdevice->info.gen >= 8 ||
1125                                                    pdevice->info.is_haswell;
1126          break;
1127       }
1128 
1129       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT: {
1130          VkPhysicalDeviceCustomBorderColorFeaturesEXT *features =
1131             (VkPhysicalDeviceCustomBorderColorFeaturesEXT *)ext;
1132          features->customBorderColors = pdevice->info.gen >= 8;
1133          features->customBorderColorWithoutFormat = pdevice->info.gen >= 8;
1134          break;
1135       }
1136 
1137       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: {
1138          VkPhysicalDeviceDepthClipEnableFeaturesEXT *features =
1139             (VkPhysicalDeviceDepthClipEnableFeaturesEXT *)ext;
1140          features->depthClipEnable = true;
1141          break;
1142       }
1143 
1144       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR: {
1145          VkPhysicalDeviceFloat16Int8FeaturesKHR *features = (void *)ext;
1146          CORE_FEATURE(1, 2, shaderFloat16);
1147          CORE_FEATURE(1, 2, shaderInt8);
1148          break;
1149       }
1150 
1151       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_INTERLOCK_FEATURES_EXT: {
1152          VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT *features =
1153             (VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT *)ext;
1154          features->fragmentShaderSampleInterlock = pdevice->info.gen >= 9;
1155          features->fragmentShaderPixelInterlock = pdevice->info.gen >= 9;
1156          features->fragmentShaderShadingRateInterlock = false;
1157          break;
1158       }
1159 
1160       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT: {
1161          VkPhysicalDeviceHostQueryResetFeaturesEXT *features =
1162             (VkPhysicalDeviceHostQueryResetFeaturesEXT *)ext;
1163          CORE_FEATURE(1, 2, hostQueryReset);
1164          break;
1165       }
1166 
1167       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
1168          VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
1169             (VkPhysicalDeviceDescriptorIndexingFeaturesEXT *)ext;
1170          CORE_FEATURE(1, 2, shaderInputAttachmentArrayDynamicIndexing);
1171          CORE_FEATURE(1, 2, shaderUniformTexelBufferArrayDynamicIndexing);
1172          CORE_FEATURE(1, 2, shaderStorageTexelBufferArrayDynamicIndexing);
1173          CORE_FEATURE(1, 2, shaderUniformBufferArrayNonUniformIndexing);
1174          CORE_FEATURE(1, 2, shaderSampledImageArrayNonUniformIndexing);
1175          CORE_FEATURE(1, 2, shaderStorageBufferArrayNonUniformIndexing);
1176          CORE_FEATURE(1, 2, shaderStorageImageArrayNonUniformIndexing);
1177          CORE_FEATURE(1, 2, shaderInputAttachmentArrayNonUniformIndexing);
1178          CORE_FEATURE(1, 2, shaderUniformTexelBufferArrayNonUniformIndexing);
1179          CORE_FEATURE(1, 2, shaderStorageTexelBufferArrayNonUniformIndexing);
1180          CORE_FEATURE(1, 2, descriptorBindingUniformBufferUpdateAfterBind);
1181          CORE_FEATURE(1, 2, descriptorBindingSampledImageUpdateAfterBind);
1182          CORE_FEATURE(1, 2, descriptorBindingStorageImageUpdateAfterBind);
1183          CORE_FEATURE(1, 2, descriptorBindingStorageBufferUpdateAfterBind);
1184          CORE_FEATURE(1, 2, descriptorBindingUniformTexelBufferUpdateAfterBind);
1185          CORE_FEATURE(1, 2, descriptorBindingStorageTexelBufferUpdateAfterBind);
1186          CORE_FEATURE(1, 2, descriptorBindingUpdateUnusedWhilePending);
1187          CORE_FEATURE(1, 2, descriptorBindingPartiallyBound);
1188          CORE_FEATURE(1, 2, descriptorBindingVariableDescriptorCount);
1189          CORE_FEATURE(1, 2, runtimeDescriptorArray);
1190          break;
1191       }
1192 
1193       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_ROBUSTNESS_FEATURES_EXT: {
1194          VkPhysicalDeviceImageRobustnessFeaturesEXT *features =
1195             (VkPhysicalDeviceImageRobustnessFeaturesEXT *)ext;
1196          features->robustImageAccess = true;
1197          break;
1198       }
1199 
1200       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT: {
1201          VkPhysicalDeviceIndexTypeUint8FeaturesEXT *features =
1202             (VkPhysicalDeviceIndexTypeUint8FeaturesEXT *)ext;
1203          features->indexTypeUint8 = true;
1204          break;
1205       }
1206 
1207       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT: {
1208          VkPhysicalDeviceInlineUniformBlockFeaturesEXT *features =
1209             (VkPhysicalDeviceInlineUniformBlockFeaturesEXT *)ext;
1210          features->inlineUniformBlock = true;
1211          features->descriptorBindingInlineUniformBlockUpdateAfterBind = true;
1212          break;
1213       }
1214 
1215       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT: {
1216          VkPhysicalDeviceLineRasterizationFeaturesEXT *features =
1217             (VkPhysicalDeviceLineRasterizationFeaturesEXT *)ext;
1218          features->rectangularLines = true;
1219          features->bresenhamLines = true;
1220          /* Support for Smooth lines with MSAA was removed on gen11.  From the
1221           * BSpec section "Multisample ModesState" table for "AA Line Support
1222           * Requirements":
1223           *
1224           *    GEN10:BUG:######## 	NUM_MULTISAMPLES == 1
1225           *
1226           * Fortunately, this isn't a case most people care about.
1227           */
1228          features->smoothLines = pdevice->info.gen < 10;
1229          features->stippledRectangularLines = false;
1230          features->stippledBresenhamLines = true;
1231          features->stippledSmoothLines = false;
1232          break;
1233       }
1234 
1235       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
1236          VkPhysicalDeviceMultiviewFeatures *features =
1237             (VkPhysicalDeviceMultiviewFeatures *)ext;
1238          CORE_FEATURE(1, 1, multiview);
1239          CORE_FEATURE(1, 1, multiviewGeometryShader);
1240          CORE_FEATURE(1, 1, multiviewTessellationShader);
1241          break;
1242       }
1243 
1244       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGELESS_FRAMEBUFFER_FEATURES_KHR: {
1245          VkPhysicalDeviceImagelessFramebufferFeaturesKHR *features =
1246             (VkPhysicalDeviceImagelessFramebufferFeaturesKHR *)ext;
1247          CORE_FEATURE(1, 2, imagelessFramebuffer);
1248          break;
1249       }
1250 
1251       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PERFORMANCE_QUERY_FEATURES_KHR: {
1252          VkPhysicalDevicePerformanceQueryFeaturesKHR *feature =
1253             (VkPhysicalDevicePerformanceQueryFeaturesKHR *)ext;
1254          feature->performanceCounterQueryPools = true;
1255          /* HW only supports a single configuration at a time. */
1256          feature->performanceCounterMultipleQueryPools = false;
1257          break;
1258       }
1259 
1260       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_CREATION_CACHE_CONTROL_FEATURES_EXT: {
1261          VkPhysicalDevicePipelineCreationCacheControlFeaturesEXT *features =
1262             (VkPhysicalDevicePipelineCreationCacheControlFeaturesEXT *)ext;
1263          features->pipelineCreationCacheControl = true;
1264          break;
1265       }
1266 
1267       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR: {
1268          VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR *features =
1269             (VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR *)ext;
1270          features->pipelineExecutableInfo = true;
1271          break;
1272       }
1273 
1274       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIVATE_DATA_FEATURES_EXT: {
1275          VkPhysicalDevicePrivateDataFeaturesEXT *features = (void *)ext;
1276          features->privateData = true;
1277          break;
1278       }
1279 
1280       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
1281          VkPhysicalDeviceProtectedMemoryFeatures *features = (void *)ext;
1282          CORE_FEATURE(1, 1, protectedMemory);
1283          break;
1284       }
1285 
1286       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT: {
1287          VkPhysicalDeviceRobustness2FeaturesEXT *features = (void *)ext;
1288          features->robustBufferAccess2 = true;
1289          features->robustImageAccess2 = true;
1290          features->nullDescriptor = true;
1291          break;
1292       }
1293 
1294       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
1295          VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
1296             (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
1297          CORE_FEATURE(1, 1, samplerYcbcrConversion);
1298          break;
1299       }
1300 
1301       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
1302          VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
1303             (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
1304          CORE_FEATURE(1, 2, scalarBlockLayout);
1305          break;
1306       }
1307 
1308       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SEPARATE_DEPTH_STENCIL_LAYOUTS_FEATURES_KHR: {
1309          VkPhysicalDeviceSeparateDepthStencilLayoutsFeaturesKHR *features =
1310             (VkPhysicalDeviceSeparateDepthStencilLayoutsFeaturesKHR *)ext;
1311          CORE_FEATURE(1, 2, separateDepthStencilLayouts);
1312          break;
1313       }
1314 
1315       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_FLOAT_FEATURES_EXT: {
1316          VkPhysicalDeviceShaderAtomicFloatFeaturesEXT *features = (void *)ext;
1317          features->shaderBufferFloat32Atomics =    true;
1318          features->shaderBufferFloat32AtomicAdd =  false;
1319          features->shaderBufferFloat64Atomics =    false;
1320          features->shaderBufferFloat64AtomicAdd =  false;
1321          features->shaderSharedFloat32Atomics =    true;
1322          features->shaderSharedFloat32AtomicAdd =  false;
1323          features->shaderSharedFloat64Atomics =    false;
1324          features->shaderSharedFloat64AtomicAdd =  false;
1325          features->shaderImageFloat32Atomics =     true;
1326          features->shaderImageFloat32AtomicAdd =   false;
1327          features->sparseImageFloat32Atomics =     false;
1328          features->sparseImageFloat32AtomicAdd =   false;
1329          break;
1330       }
1331 
1332       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR: {
1333          VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *features = (void *)ext;
1334          CORE_FEATURE(1, 2, shaderBufferInt64Atomics);
1335          CORE_FEATURE(1, 2, shaderSharedInt64Atomics);
1336          break;
1337       }
1338 
1339       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT: {
1340          VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT *features = (void *)ext;
1341          features->shaderDemoteToHelperInvocation = true;
1342          break;
1343       }
1344 
1345       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CLOCK_FEATURES_KHR: {
1346          VkPhysicalDeviceShaderClockFeaturesKHR *features =
1347             (VkPhysicalDeviceShaderClockFeaturesKHR *)ext;
1348          features->shaderSubgroupClock = true;
1349          features->shaderDeviceClock = false;
1350          break;
1351       }
1352 
1353       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
1354          VkPhysicalDeviceShaderDrawParametersFeatures *features = (void *)ext;
1355          CORE_FEATURE(1, 1, shaderDrawParameters);
1356          break;
1357       }
1358 
1359       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_INTEGER_FUNCTIONS_2_FEATURES_INTEL: {
1360          VkPhysicalDeviceShaderIntegerFunctions2FeaturesINTEL *features =
1361             (VkPhysicalDeviceShaderIntegerFunctions2FeaturesINTEL *)ext;
1362          features->shaderIntegerFunctions2 = true;
1363          break;
1364       }
1365 
1366       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_SUBGROUP_EXTENDED_TYPES_FEATURES_KHR: {
1367          VkPhysicalDeviceShaderSubgroupExtendedTypesFeaturesKHR *features =
1368             (VkPhysicalDeviceShaderSubgroupExtendedTypesFeaturesKHR *)ext;
1369          CORE_FEATURE(1, 2, shaderSubgroupExtendedTypes);
1370          break;
1371       }
1372 
1373       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT: {
1374          VkPhysicalDeviceSubgroupSizeControlFeaturesEXT *features =
1375             (VkPhysicalDeviceSubgroupSizeControlFeaturesEXT *)ext;
1376          features->subgroupSizeControl = true;
1377          features->computeFullSubgroups = true;
1378          break;
1379       }
1380 
1381       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TEXEL_BUFFER_ALIGNMENT_FEATURES_EXT: {
1382          VkPhysicalDeviceTexelBufferAlignmentFeaturesEXT *features =
1383             (VkPhysicalDeviceTexelBufferAlignmentFeaturesEXT *)ext;
1384          features->texelBufferAlignment = true;
1385          break;
1386       }
1387 
1388       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES_KHR: {
1389          VkPhysicalDeviceTimelineSemaphoreFeaturesKHR *features =
1390             (VkPhysicalDeviceTimelineSemaphoreFeaturesKHR *) ext;
1391          CORE_FEATURE(1, 2, timelineSemaphore);
1392          break;
1393       }
1394 
1395       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
1396          VkPhysicalDeviceVariablePointersFeatures *features = (void *)ext;
1397          CORE_FEATURE(1, 1, variablePointersStorageBuffer);
1398          CORE_FEATURE(1, 1, variablePointers);
1399          break;
1400       }
1401 
1402       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
1403          VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
1404             (VkPhysicalDeviceTransformFeedbackFeaturesEXT *)ext;
1405          features->transformFeedback = true;
1406          features->geometryStreams = true;
1407          break;
1408       }
1409 
1410       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR: {
1411          VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR *features =
1412             (VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR *)ext;
1413          CORE_FEATURE(1, 2, uniformBufferStandardLayout);
1414          break;
1415       }
1416 
1417       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
1418          VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
1419             (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
1420          features->vertexAttributeInstanceRateDivisor = true;
1421          features->vertexAttributeInstanceRateZeroDivisor = true;
1422          break;
1423       }
1424 
1425       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES:
1426          anv_get_physical_device_features_1_1(pdevice, (void *)ext);
1427          break;
1428 
1429       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES:
1430          anv_get_physical_device_features_1_2(pdevice, (void *)ext);
1431          break;
1432 
1433       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_MEMORY_MODEL_FEATURES_KHR: {
1434          VkPhysicalDeviceVulkanMemoryModelFeaturesKHR *features = (void *)ext;
1435          CORE_FEATURE(1, 2, vulkanMemoryModel);
1436          CORE_FEATURE(1, 2, vulkanMemoryModelDeviceScope);
1437          CORE_FEATURE(1, 2, vulkanMemoryModelAvailabilityVisibilityChains);
1438          break;
1439       }
1440 
1441       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT: {
1442          VkPhysicalDeviceYcbcrImageArraysFeaturesEXT *features =
1443             (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT *)ext;
1444          features->ycbcrImageArrays = true;
1445          break;
1446       }
1447 
1448       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT: {
1449          VkPhysicalDeviceExtendedDynamicStateFeaturesEXT *features =
1450             (VkPhysicalDeviceExtendedDynamicStateFeaturesEXT *)ext;
1451          features->extendedDynamicState = true;
1452          break;
1453       }
1454 
1455       default:
1456          anv_debug_ignored_stype(ext->sType);
1457          break;
1458       }
1459    }
1460 
1461 #undef CORE_FEATURE
1462 }
1463 
1464 #define MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS   64
1465 
1466 #define MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS 64
1467 #define MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS       256
1468 
1469 #define MAX_CUSTOM_BORDER_COLORS                   4096
1470 
anv_GetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,VkPhysicalDeviceProperties * pProperties)1471 void anv_GetPhysicalDeviceProperties(
1472     VkPhysicalDevice                            physicalDevice,
1473     VkPhysicalDeviceProperties*                 pProperties)
1474 {
1475    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
1476    const struct gen_device_info *devinfo = &pdevice->info;
1477 
1478    /* See assertions made when programming the buffer surface state. */
1479    const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
1480                                       (1ul << 30) : (1ul << 27);
1481 
1482    const uint32_t max_ssbos = pdevice->has_a64_buffer_access ? UINT16_MAX : 64;
1483    const uint32_t max_textures =
1484       pdevice->has_bindless_images ? UINT16_MAX : 128;
1485    const uint32_t max_samplers =
1486       pdevice->has_bindless_samplers ? UINT16_MAX :
1487       (devinfo->gen >= 8 || devinfo->is_haswell) ? 128 : 16;
1488    const uint32_t max_images =
1489       pdevice->has_bindless_images ? UINT16_MAX : MAX_IMAGES;
1490 
1491    /* If we can use bindless for everything, claim a high per-stage limit,
1492     * otherwise use the binding table size, minus the slots reserved for
1493     * render targets and one slot for the descriptor buffer. */
1494    const uint32_t max_per_stage =
1495       pdevice->has_bindless_images && pdevice->has_a64_buffer_access
1496       ? UINT32_MAX : MAX_BINDING_TABLE_SIZE - MAX_RTS - 1;
1497 
1498    /* Limit max_threads to 64 for the GPGPU_WALKER command */
1499    const uint32_t max_workgroup_size = 32 * MIN2(64, devinfo->max_cs_threads);
1500 
1501    VkSampleCountFlags sample_counts =
1502       isl_device_get_sample_counts(&pdevice->isl_dev);
1503 
1504 
1505    VkPhysicalDeviceLimits limits = {
1506       .maxImageDimension1D                      = (1 << 14),
1507       .maxImageDimension2D                      = (1 << 14),
1508       .maxImageDimension3D                      = (1 << 11),
1509       .maxImageDimensionCube                    = (1 << 14),
1510       .maxImageArrayLayers                      = (1 << 11),
1511       .maxTexelBufferElements                   = 128 * 1024 * 1024,
1512       .maxUniformBufferRange                    = (1ul << 27),
1513       .maxStorageBufferRange                    = max_raw_buffer_sz,
1514       .maxPushConstantsSize                     = MAX_PUSH_CONSTANTS_SIZE,
1515       .maxMemoryAllocationCount                 = UINT32_MAX,
1516       .maxSamplerAllocationCount                = 64 * 1024,
1517       .bufferImageGranularity                   = 64, /* A cache line */
1518       .sparseAddressSpaceSize                   = 0,
1519       .maxBoundDescriptorSets                   = MAX_SETS,
1520       .maxPerStageDescriptorSamplers            = max_samplers,
1521       .maxPerStageDescriptorUniformBuffers      = MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS,
1522       .maxPerStageDescriptorStorageBuffers      = max_ssbos,
1523       .maxPerStageDescriptorSampledImages       = max_textures,
1524       .maxPerStageDescriptorStorageImages       = max_images,
1525       .maxPerStageDescriptorInputAttachments    = MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS,
1526       .maxPerStageResources                     = max_per_stage,
1527       .maxDescriptorSetSamplers                 = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSamplers */
1528       .maxDescriptorSetUniformBuffers           = 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS,           /* number of stages * maxPerStageDescriptorUniformBuffers */
1529       .maxDescriptorSetUniformBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
1530       .maxDescriptorSetStorageBuffers           = 6 * max_ssbos,    /* number of stages * maxPerStageDescriptorStorageBuffers */
1531       .maxDescriptorSetStorageBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
1532       .maxDescriptorSetSampledImages            = 6 * max_textures, /* number of stages * maxPerStageDescriptorSampledImages */
1533       .maxDescriptorSetStorageImages            = 6 * max_images,   /* number of stages * maxPerStageDescriptorStorageImages */
1534       .maxDescriptorSetInputAttachments         = MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS,
1535       .maxVertexInputAttributes                 = MAX_VBS,
1536       .maxVertexInputBindings                   = MAX_VBS,
1537       .maxVertexInputAttributeOffset            = 2047,
1538       .maxVertexInputBindingStride              = 2048,
1539       .maxVertexOutputComponents                = 128,
1540       .maxTessellationGenerationLevel           = 64,
1541       .maxTessellationPatchSize                 = 32,
1542       .maxTessellationControlPerVertexInputComponents = 128,
1543       .maxTessellationControlPerVertexOutputComponents = 128,
1544       .maxTessellationControlPerPatchOutputComponents = 128,
1545       .maxTessellationControlTotalOutputComponents = 2048,
1546       .maxTessellationEvaluationInputComponents = 128,
1547       .maxTessellationEvaluationOutputComponents = 128,
1548       .maxGeometryShaderInvocations             = 32,
1549       .maxGeometryInputComponents               = 64,
1550       .maxGeometryOutputComponents              = 128,
1551       .maxGeometryOutputVertices                = 256,
1552       .maxGeometryTotalOutputComponents         = 1024,
1553       .maxFragmentInputComponents               = 116, /* 128 components - (PSIZ, CLIP_DIST0, CLIP_DIST1) */
1554       .maxFragmentOutputAttachments             = 8,
1555       .maxFragmentDualSrcAttachments            = 1,
1556       .maxFragmentCombinedOutputResources       = 8,
1557       .maxComputeSharedMemorySize               = 64 * 1024,
1558       .maxComputeWorkGroupCount                 = { 65535, 65535, 65535 },
1559       .maxComputeWorkGroupInvocations           = max_workgroup_size,
1560       .maxComputeWorkGroupSize = {
1561          max_workgroup_size,
1562          max_workgroup_size,
1563          max_workgroup_size,
1564       },
1565       .subPixelPrecisionBits                    = 8,
1566       .subTexelPrecisionBits                    = 8,
1567       .mipmapPrecisionBits                      = 8,
1568       .maxDrawIndexedIndexValue                 = UINT32_MAX,
1569       .maxDrawIndirectCount                     = UINT32_MAX,
1570       .maxSamplerLodBias                        = 16,
1571       .maxSamplerAnisotropy                     = 16,
1572       .maxViewports                             = MAX_VIEWPORTS,
1573       .maxViewportDimensions                    = { (1 << 14), (1 << 14) },
1574       .viewportBoundsRange                      = { INT16_MIN, INT16_MAX },
1575       .viewportSubPixelBits                     = 13, /* We take a float? */
1576       .minMemoryMapAlignment                    = 4096, /* A page */
1577       /* The dataport requires texel alignment so we need to assume a worst
1578        * case of R32G32B32A32 which is 16 bytes.
1579        */
1580       .minTexelBufferOffsetAlignment            = 16,
1581       .minUniformBufferOffsetAlignment          = ANV_UBO_ALIGNMENT,
1582       .minStorageBufferOffsetAlignment          = 4,
1583       .minTexelOffset                           = -8,
1584       .maxTexelOffset                           = 7,
1585       .minTexelGatherOffset                     = -32,
1586       .maxTexelGatherOffset                     = 31,
1587       .minInterpolationOffset                   = -0.5,
1588       .maxInterpolationOffset                   = 0.4375,
1589       .subPixelInterpolationOffsetBits          = 4,
1590       .maxFramebufferWidth                      = (1 << 14),
1591       .maxFramebufferHeight                     = (1 << 14),
1592       .maxFramebufferLayers                     = (1 << 11),
1593       .framebufferColorSampleCounts             = sample_counts,
1594       .framebufferDepthSampleCounts             = sample_counts,
1595       .framebufferStencilSampleCounts           = sample_counts,
1596       .framebufferNoAttachmentsSampleCounts     = sample_counts,
1597       .maxColorAttachments                      = MAX_RTS,
1598       .sampledImageColorSampleCounts            = sample_counts,
1599       .sampledImageIntegerSampleCounts          = sample_counts,
1600       .sampledImageDepthSampleCounts            = sample_counts,
1601       .sampledImageStencilSampleCounts          = sample_counts,
1602       .storageImageSampleCounts                 = VK_SAMPLE_COUNT_1_BIT,
1603       .maxSampleMaskWords                       = 1,
1604       .timestampComputeAndGraphics              = true,
1605       .timestampPeriod                          = 1000000000.0 / devinfo->timestamp_frequency,
1606       .maxClipDistances                         = 8,
1607       .maxCullDistances                         = 8,
1608       .maxCombinedClipAndCullDistances          = 8,
1609       .discreteQueuePriorities                  = 2,
1610       .pointSizeRange                           = { 0.125, 255.875 },
1611       .lineWidthRange                           = {
1612          0.0,
1613          (devinfo->gen >= 9 || devinfo->is_cherryview) ?
1614             2047.9921875 : 7.9921875,
1615       },
1616       .pointSizeGranularity                     = (1.0 / 8.0),
1617       .lineWidthGranularity                     = (1.0 / 128.0),
1618       .strictLines                              = false,
1619       .standardSampleLocations                  = true,
1620       .optimalBufferCopyOffsetAlignment         = 128,
1621       .optimalBufferCopyRowPitchAlignment       = 128,
1622       .nonCoherentAtomSize                      = 64,
1623    };
1624 
1625    *pProperties = (VkPhysicalDeviceProperties) {
1626       .apiVersion = anv_physical_device_api_version(pdevice),
1627       .driverVersion = vk_get_driver_version(),
1628       .vendorID = 0x8086,
1629       .deviceID = pdevice->info.chipset_id,
1630       .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
1631       .limits = limits,
1632       .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
1633    };
1634 
1635    snprintf(pProperties->deviceName, sizeof(pProperties->deviceName),
1636             "%s", pdevice->name);
1637    memcpy(pProperties->pipelineCacheUUID,
1638           pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
1639 }
1640 
1641 static void
anv_get_physical_device_properties_1_1(struct anv_physical_device * pdevice,VkPhysicalDeviceVulkan11Properties * p)1642 anv_get_physical_device_properties_1_1(struct anv_physical_device *pdevice,
1643                                        VkPhysicalDeviceVulkan11Properties *p)
1644 {
1645    assert(p->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES);
1646 
1647    memcpy(p->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
1648    memcpy(p->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
1649    memset(p->deviceLUID, 0, VK_LUID_SIZE);
1650    p->deviceNodeMask = 0;
1651    p->deviceLUIDValid = false;
1652 
1653    p->subgroupSize = BRW_SUBGROUP_SIZE;
1654    VkShaderStageFlags scalar_stages = 0;
1655    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
1656       if (pdevice->compiler->scalar_stage[stage])
1657          scalar_stages |= mesa_to_vk_shader_stage(stage);
1658    }
1659    p->subgroupSupportedStages = scalar_stages;
1660    p->subgroupSupportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
1661                                     VK_SUBGROUP_FEATURE_VOTE_BIT |
1662                                     VK_SUBGROUP_FEATURE_BALLOT_BIT |
1663                                     VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
1664                                     VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT |
1665                                     VK_SUBGROUP_FEATURE_QUAD_BIT;
1666    if (pdevice->info.gen >= 8) {
1667       /* TODO: There's no technical reason why these can't be made to
1668        * work on gen7 but they don't at the moment so it's best to leave
1669        * the feature disabled than enabled and broken.
1670        */
1671       p->subgroupSupportedOperations |= VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
1672                                         VK_SUBGROUP_FEATURE_CLUSTERED_BIT;
1673    }
1674    p->subgroupQuadOperationsInAllStages = pdevice->info.gen >= 8;
1675 
1676    p->pointClippingBehavior      = VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY;
1677    p->maxMultiviewViewCount      = 16;
1678    p->maxMultiviewInstanceIndex  = UINT32_MAX / 16;
1679    p->protectedNoFault           = false;
1680    /* This value doesn't matter for us today as our per-stage descriptors are
1681     * the real limit.
1682     */
1683    p->maxPerSetDescriptors       = 1024;
1684    p->maxMemoryAllocationSize    = MAX_MEMORY_ALLOCATION_SIZE;
1685 }
1686 
1687 static void
anv_get_physical_device_properties_1_2(struct anv_physical_device * pdevice,VkPhysicalDeviceVulkan12Properties * p)1688 anv_get_physical_device_properties_1_2(struct anv_physical_device *pdevice,
1689                                        VkPhysicalDeviceVulkan12Properties *p)
1690 {
1691    assert(p->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES);
1692 
1693    p->driverID = VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR;
1694    memset(p->driverName, 0, sizeof(p->driverName));
1695    snprintf(p->driverName, VK_MAX_DRIVER_NAME_SIZE_KHR,
1696             "Intel open-source Mesa driver");
1697    memset(p->driverInfo, 0, sizeof(p->driverInfo));
1698    snprintf(p->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
1699             "Mesa " PACKAGE_VERSION MESA_GIT_SHA1);
1700    p->conformanceVersion = (VkConformanceVersionKHR) {
1701       .major = 1,
1702       .minor = 2,
1703       .subminor = 0,
1704       .patch = 0,
1705    };
1706 
1707    p->denormBehaviorIndependence =
1708       VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR;
1709    p->roundingModeIndependence =
1710       VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR;
1711 
1712    /* Broadwell does not support HF denorms and there are restrictions
1713     * other gens. According to Kabylake's PRM:
1714     *
1715     * "math - Extended Math Function
1716     * [...]
1717     * Restriction : Half-float denorms are always retained."
1718     */
1719    p->shaderDenormFlushToZeroFloat16         = false;
1720    p->shaderDenormPreserveFloat16            = pdevice->info.gen > 8;
1721    p->shaderRoundingModeRTEFloat16           = true;
1722    p->shaderRoundingModeRTZFloat16           = true;
1723    p->shaderSignedZeroInfNanPreserveFloat16  = true;
1724 
1725    p->shaderDenormFlushToZeroFloat32         = true;
1726    p->shaderDenormPreserveFloat32            = true;
1727    p->shaderRoundingModeRTEFloat32           = true;
1728    p->shaderRoundingModeRTZFloat32           = true;
1729    p->shaderSignedZeroInfNanPreserveFloat32  = true;
1730 
1731    p->shaderDenormFlushToZeroFloat64         = true;
1732    p->shaderDenormPreserveFloat64            = true;
1733    p->shaderRoundingModeRTEFloat64           = true;
1734    p->shaderRoundingModeRTZFloat64           = true;
1735    p->shaderSignedZeroInfNanPreserveFloat64  = true;
1736 
1737    /* It's a bit hard to exactly map our implementation to the limits
1738     * described here.  The bindless surface handle in the extended
1739     * message descriptors is 20 bits and it's an index into the table of
1740     * RENDER_SURFACE_STATE structs that starts at bindless surface base
1741     * address.  Given that most things consume two surface states per
1742     * view (general/sampled for textures and write-only/read-write for
1743     * images), we claim 2^19 things.
1744     *
1745     * For SSBOs, we just use A64 messages so there is no real limit
1746     * there beyond the limit on the total size of a descriptor set.
1747     */
1748    const unsigned max_bindless_views = 1 << 19;
1749    p->maxUpdateAfterBindDescriptorsInAllPools            = max_bindless_views;
1750    p->shaderUniformBufferArrayNonUniformIndexingNative   = false;
1751    p->shaderSampledImageArrayNonUniformIndexingNative    = false;
1752    p->shaderStorageBufferArrayNonUniformIndexingNative   = true;
1753    p->shaderStorageImageArrayNonUniformIndexingNative    = false;
1754    p->shaderInputAttachmentArrayNonUniformIndexingNative = false;
1755    p->robustBufferAccessUpdateAfterBind                  = true;
1756    p->quadDivergentImplicitLod                           = false;
1757    p->maxPerStageDescriptorUpdateAfterBindSamplers       = max_bindless_views;
1758    p->maxPerStageDescriptorUpdateAfterBindUniformBuffers = MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS;
1759    p->maxPerStageDescriptorUpdateAfterBindStorageBuffers = UINT32_MAX;
1760    p->maxPerStageDescriptorUpdateAfterBindSampledImages  = max_bindless_views;
1761    p->maxPerStageDescriptorUpdateAfterBindStorageImages  = max_bindless_views;
1762    p->maxPerStageDescriptorUpdateAfterBindInputAttachments = MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS;
1763    p->maxPerStageUpdateAfterBindResources                = UINT32_MAX;
1764    p->maxDescriptorSetUpdateAfterBindSamplers            = max_bindless_views;
1765    p->maxDescriptorSetUpdateAfterBindUniformBuffers      = 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS;
1766    p->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2;
1767    p->maxDescriptorSetUpdateAfterBindStorageBuffers      = UINT32_MAX;
1768    p->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2;
1769    p->maxDescriptorSetUpdateAfterBindSampledImages       = max_bindless_views;
1770    p->maxDescriptorSetUpdateAfterBindStorageImages       = max_bindless_views;
1771    p->maxDescriptorSetUpdateAfterBindInputAttachments    = MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS;
1772 
1773    /* We support all of the depth resolve modes */
1774    p->supportedDepthResolveModes    = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR |
1775                                       VK_RESOLVE_MODE_AVERAGE_BIT_KHR |
1776                                       VK_RESOLVE_MODE_MIN_BIT_KHR |
1777                                       VK_RESOLVE_MODE_MAX_BIT_KHR;
1778    /* Average doesn't make sense for stencil so we don't support that */
1779    p->supportedStencilResolveModes  = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR;
1780    if (pdevice->info.gen >= 8) {
1781       /* The advanced stencil resolve modes currently require stencil
1782        * sampling be supported by the hardware.
1783        */
1784       p->supportedStencilResolveModes |= VK_RESOLVE_MODE_MIN_BIT_KHR |
1785                                          VK_RESOLVE_MODE_MAX_BIT_KHR;
1786    }
1787    p->independentResolveNone  = true;
1788    p->independentResolve      = true;
1789 
1790    p->filterMinmaxSingleComponentFormats  = pdevice->info.gen >= 9;
1791    p->filterMinmaxImageComponentMapping   = pdevice->info.gen >= 9;
1792 
1793    p->maxTimelineSemaphoreValueDifference = UINT64_MAX;
1794 
1795    p->framebufferIntegerColorSampleCounts =
1796       isl_device_get_sample_counts(&pdevice->isl_dev);
1797 }
1798 
anv_GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceProperties2 * pProperties)1799 void anv_GetPhysicalDeviceProperties2(
1800     VkPhysicalDevice                            physicalDevice,
1801     VkPhysicalDeviceProperties2*                pProperties)
1802 {
1803    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
1804 
1805    anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
1806 
1807    VkPhysicalDeviceVulkan11Properties core_1_1 = {
1808       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES,
1809    };
1810    anv_get_physical_device_properties_1_1(pdevice, &core_1_1);
1811 
1812    VkPhysicalDeviceVulkan12Properties core_1_2 = {
1813       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES,
1814    };
1815    anv_get_physical_device_properties_1_2(pdevice, &core_1_2);
1816 
1817 #define CORE_RENAMED_PROPERTY(major, minor, ext_property, core_property) \
1818    memcpy(&properties->ext_property, &core_##major##_##minor.core_property, \
1819           sizeof(core_##major##_##minor.core_property))
1820 
1821 #define CORE_PROPERTY(major, minor, property) \
1822    CORE_RENAMED_PROPERTY(major, minor, property, property)
1823 
1824    vk_foreach_struct(ext, pProperties->pNext) {
1825       switch (ext->sType) {
1826       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_PROPERTIES_EXT: {
1827          VkPhysicalDeviceCustomBorderColorPropertiesEXT *properties =
1828             (VkPhysicalDeviceCustomBorderColorPropertiesEXT *)ext;
1829          properties->maxCustomBorderColorSamplers = MAX_CUSTOM_BORDER_COLORS;
1830          break;
1831       }
1832 
1833       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR: {
1834          VkPhysicalDeviceDepthStencilResolvePropertiesKHR *properties =
1835             (VkPhysicalDeviceDepthStencilResolvePropertiesKHR *)ext;
1836          CORE_PROPERTY(1, 2, supportedDepthResolveModes);
1837          CORE_PROPERTY(1, 2, supportedStencilResolveModes);
1838          CORE_PROPERTY(1, 2, independentResolveNone);
1839          CORE_PROPERTY(1, 2, independentResolve);
1840          break;
1841       }
1842 
1843       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT: {
1844          VkPhysicalDeviceDescriptorIndexingPropertiesEXT *properties =
1845             (VkPhysicalDeviceDescriptorIndexingPropertiesEXT *)ext;
1846          CORE_PROPERTY(1, 2, maxUpdateAfterBindDescriptorsInAllPools);
1847          CORE_PROPERTY(1, 2, shaderUniformBufferArrayNonUniformIndexingNative);
1848          CORE_PROPERTY(1, 2, shaderSampledImageArrayNonUniformIndexingNative);
1849          CORE_PROPERTY(1, 2, shaderStorageBufferArrayNonUniformIndexingNative);
1850          CORE_PROPERTY(1, 2, shaderStorageImageArrayNonUniformIndexingNative);
1851          CORE_PROPERTY(1, 2, shaderInputAttachmentArrayNonUniformIndexingNative);
1852          CORE_PROPERTY(1, 2, robustBufferAccessUpdateAfterBind);
1853          CORE_PROPERTY(1, 2, quadDivergentImplicitLod);
1854          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindSamplers);
1855          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindUniformBuffers);
1856          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindStorageBuffers);
1857          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindSampledImages);
1858          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindStorageImages);
1859          CORE_PROPERTY(1, 2, maxPerStageDescriptorUpdateAfterBindInputAttachments);
1860          CORE_PROPERTY(1, 2, maxPerStageUpdateAfterBindResources);
1861          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindSamplers);
1862          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindUniformBuffers);
1863          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindUniformBuffersDynamic);
1864          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindStorageBuffers);
1865          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindStorageBuffersDynamic);
1866          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindSampledImages);
1867          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindStorageImages);
1868          CORE_PROPERTY(1, 2, maxDescriptorSetUpdateAfterBindInputAttachments);
1869          break;
1870       }
1871 
1872       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
1873          VkPhysicalDeviceDriverPropertiesKHR *properties =
1874             (VkPhysicalDeviceDriverPropertiesKHR *) ext;
1875          CORE_PROPERTY(1, 2, driverID);
1876          CORE_PROPERTY(1, 2, driverName);
1877          CORE_PROPERTY(1, 2, driverInfo);
1878          CORE_PROPERTY(1, 2, conformanceVersion);
1879          break;
1880       }
1881 
1882       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT: {
1883          VkPhysicalDeviceExternalMemoryHostPropertiesEXT *props =
1884             (VkPhysicalDeviceExternalMemoryHostPropertiesEXT *) ext;
1885          /* Userptr needs page aligned memory. */
1886          props->minImportedHostPointerAlignment = 4096;
1887          break;
1888       }
1889 
1890       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
1891          VkPhysicalDeviceIDProperties *properties =
1892             (VkPhysicalDeviceIDProperties *)ext;
1893          CORE_PROPERTY(1, 1, deviceUUID);
1894          CORE_PROPERTY(1, 1, driverUUID);
1895          CORE_PROPERTY(1, 1, deviceLUID);
1896          CORE_PROPERTY(1, 1, deviceLUIDValid);
1897          break;
1898       }
1899 
1900       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT: {
1901          VkPhysicalDeviceInlineUniformBlockPropertiesEXT *props =
1902             (VkPhysicalDeviceInlineUniformBlockPropertiesEXT *)ext;
1903          props->maxInlineUniformBlockSize = MAX_INLINE_UNIFORM_BLOCK_SIZE;
1904          props->maxPerStageDescriptorInlineUniformBlocks =
1905             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
1906          props->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks =
1907             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
1908          props->maxDescriptorSetInlineUniformBlocks =
1909             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
1910          props->maxDescriptorSetUpdateAfterBindInlineUniformBlocks =
1911             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
1912          break;
1913       }
1914 
1915       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_PROPERTIES_EXT: {
1916          VkPhysicalDeviceLineRasterizationPropertiesEXT *props =
1917             (VkPhysicalDeviceLineRasterizationPropertiesEXT *)ext;
1918          /* In the Skylake PRM Vol. 7, subsection titled "GIQ (Diamond)
1919           * Sampling Rules - Legacy Mode", it says the following:
1920           *
1921           *    "Note that the device divides a pixel into a 16x16 array of
1922           *    subpixels, referenced by their upper left corners."
1923           *
1924           * This is the only known reference in the PRMs to the subpixel
1925           * precision of line rasterization and a "16x16 array of subpixels"
1926           * implies 4 subpixel precision bits.  Empirical testing has shown
1927           * that 4 subpixel precision bits applies to all line rasterization
1928           * types.
1929           */
1930          props->lineSubPixelPrecisionBits = 4;
1931          break;
1932       }
1933 
1934       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
1935          VkPhysicalDeviceMaintenance3Properties *properties =
1936             (VkPhysicalDeviceMaintenance3Properties *)ext;
1937          /* This value doesn't matter for us today as our per-stage
1938           * descriptors are the real limit.
1939           */
1940          CORE_PROPERTY(1, 1, maxPerSetDescriptors);
1941          CORE_PROPERTY(1, 1, maxMemoryAllocationSize);
1942          break;
1943       }
1944 
1945       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
1946          VkPhysicalDeviceMultiviewProperties *properties =
1947             (VkPhysicalDeviceMultiviewProperties *)ext;
1948          CORE_PROPERTY(1, 1, maxMultiviewViewCount);
1949          CORE_PROPERTY(1, 1, maxMultiviewInstanceIndex);
1950          break;
1951       }
1952 
1953       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
1954          VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
1955             (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
1956          properties->pciDomain = pdevice->pci_info.domain;
1957          properties->pciBus = pdevice->pci_info.bus;
1958          properties->pciDevice = pdevice->pci_info.device;
1959          properties->pciFunction = pdevice->pci_info.function;
1960          break;
1961       }
1962 
1963       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PERFORMANCE_QUERY_PROPERTIES_KHR: {
1964          VkPhysicalDevicePerformanceQueryPropertiesKHR *properties =
1965             (VkPhysicalDevicePerformanceQueryPropertiesKHR *)ext;
1966          /* We could support this by spawning a shader to do the equation
1967           * normalization.
1968           */
1969          properties->allowCommandBufferQueryCopies = false;
1970          break;
1971       }
1972 
1973       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
1974          VkPhysicalDevicePointClippingProperties *properties =
1975             (VkPhysicalDevicePointClippingProperties *) ext;
1976          CORE_PROPERTY(1, 1, pointClippingBehavior);
1977          break;
1978       }
1979 
1980 #pragma GCC diagnostic push
1981 #pragma GCC diagnostic ignored "-Wswitch"
1982       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID: {
1983          VkPhysicalDevicePresentationPropertiesANDROID *props =
1984             (VkPhysicalDevicePresentationPropertiesANDROID *)ext;
1985          props->sharedImage = VK_FALSE;
1986          break;
1987       }
1988 #pragma GCC diagnostic pop
1989 
1990       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
1991          VkPhysicalDeviceProtectedMemoryProperties *properties =
1992             (VkPhysicalDeviceProtectedMemoryProperties *)ext;
1993          CORE_PROPERTY(1, 1, protectedNoFault);
1994          break;
1995       }
1996 
1997       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
1998          VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
1999             (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
2000          properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
2001          break;
2002       }
2003 
2004       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_PROPERTIES_EXT: {
2005          VkPhysicalDeviceRobustness2PropertiesEXT *properties = (void *)ext;
2006          properties->robustStorageBufferAccessSizeAlignment =
2007             ANV_SSBO_BOUNDS_CHECK_ALIGNMENT;
2008          properties->robustUniformBufferAccessSizeAlignment =
2009             ANV_UBO_ALIGNMENT;
2010          break;
2011       }
2012 
2013       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
2014          VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
2015             (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
2016          CORE_PROPERTY(1, 2, filterMinmaxImageComponentMapping);
2017          CORE_PROPERTY(1, 2, filterMinmaxSingleComponentFormats);
2018          break;
2019       }
2020 
2021       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
2022          VkPhysicalDeviceSubgroupProperties *properties = (void *)ext;
2023          CORE_PROPERTY(1, 1, subgroupSize);
2024          CORE_RENAMED_PROPERTY(1, 1, supportedStages,
2025                                      subgroupSupportedStages);
2026          CORE_RENAMED_PROPERTY(1, 1, supportedOperations,
2027                                      subgroupSupportedOperations);
2028          CORE_RENAMED_PROPERTY(1, 1, quadOperationsInAllStages,
2029                                      subgroupQuadOperationsInAllStages);
2030          break;
2031       }
2032 
2033       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT: {
2034          VkPhysicalDeviceSubgroupSizeControlPropertiesEXT *props =
2035             (VkPhysicalDeviceSubgroupSizeControlPropertiesEXT *)ext;
2036          STATIC_ASSERT(8 <= BRW_SUBGROUP_SIZE && BRW_SUBGROUP_SIZE <= 32);
2037          props->minSubgroupSize = 8;
2038          props->maxSubgroupSize = 32;
2039          props->maxComputeWorkgroupSubgroups = pdevice->info.max_cs_threads;
2040          props->requiredSubgroupSizeStages = VK_SHADER_STAGE_COMPUTE_BIT;
2041          break;
2042       }
2043       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES_KHR : {
2044          VkPhysicalDeviceFloatControlsPropertiesKHR *properties = (void *)ext;
2045          CORE_PROPERTY(1, 2, denormBehaviorIndependence);
2046          CORE_PROPERTY(1, 2, roundingModeIndependence);
2047          CORE_PROPERTY(1, 2, shaderDenormFlushToZeroFloat16);
2048          CORE_PROPERTY(1, 2, shaderDenormPreserveFloat16);
2049          CORE_PROPERTY(1, 2, shaderRoundingModeRTEFloat16);
2050          CORE_PROPERTY(1, 2, shaderRoundingModeRTZFloat16);
2051          CORE_PROPERTY(1, 2, shaderSignedZeroInfNanPreserveFloat16);
2052          CORE_PROPERTY(1, 2, shaderDenormFlushToZeroFloat32);
2053          CORE_PROPERTY(1, 2, shaderDenormPreserveFloat32);
2054          CORE_PROPERTY(1, 2, shaderRoundingModeRTEFloat32);
2055          CORE_PROPERTY(1, 2, shaderRoundingModeRTZFloat32);
2056          CORE_PROPERTY(1, 2, shaderSignedZeroInfNanPreserveFloat32);
2057          CORE_PROPERTY(1, 2, shaderDenormFlushToZeroFloat64);
2058          CORE_PROPERTY(1, 2, shaderDenormPreserveFloat64);
2059          CORE_PROPERTY(1, 2, shaderRoundingModeRTEFloat64);
2060          CORE_PROPERTY(1, 2, shaderRoundingModeRTZFloat64);
2061          CORE_PROPERTY(1, 2, shaderSignedZeroInfNanPreserveFloat64);
2062          break;
2063       }
2064 
2065       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TEXEL_BUFFER_ALIGNMENT_PROPERTIES_EXT: {
2066          VkPhysicalDeviceTexelBufferAlignmentPropertiesEXT *props =
2067             (VkPhysicalDeviceTexelBufferAlignmentPropertiesEXT *)ext;
2068 
2069          /* From the SKL PRM Vol. 2d, docs for RENDER_SURFACE_STATE::Surface
2070           * Base Address:
2071           *
2072           *    "For SURFTYPE_BUFFER non-rendertarget surfaces, this field
2073           *    specifies the base address of the first element of the surface,
2074           *    computed in software by adding the surface base address to the
2075           *    byte offset of the element in the buffer. The base address must
2076           *    be aligned to element size."
2077           *
2078           * The typed dataport messages require that things be texel aligned.
2079           * Otherwise, we may just load/store the wrong data or, in the worst
2080           * case, there may be hangs.
2081           */
2082          props->storageTexelBufferOffsetAlignmentBytes = 16;
2083          props->storageTexelBufferOffsetSingleTexelAlignment = true;
2084 
2085          /* The sampler, however, is much more forgiving and it can handle
2086           * arbitrary byte alignment for linear and buffer surfaces.  It's
2087           * hard to find a good PRM citation for this but years of empirical
2088           * experience demonstrate that this is true.
2089           */
2090          props->uniformTexelBufferOffsetAlignmentBytes = 1;
2091          props->uniformTexelBufferOffsetSingleTexelAlignment = false;
2092          break;
2093       }
2094 
2095       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_PROPERTIES_KHR: {
2096          VkPhysicalDeviceTimelineSemaphorePropertiesKHR *properties =
2097             (VkPhysicalDeviceTimelineSemaphorePropertiesKHR *) ext;
2098          CORE_PROPERTY(1, 2, maxTimelineSemaphoreValueDifference);
2099          break;
2100       }
2101 
2102       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
2103          VkPhysicalDeviceTransformFeedbackPropertiesEXT *props =
2104             (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
2105 
2106          props->maxTransformFeedbackStreams = MAX_XFB_STREAMS;
2107          props->maxTransformFeedbackBuffers = MAX_XFB_BUFFERS;
2108          props->maxTransformFeedbackBufferSize = (1ull << 32);
2109          props->maxTransformFeedbackStreamDataSize = 128 * 4;
2110          props->maxTransformFeedbackBufferDataSize = 128 * 4;
2111          props->maxTransformFeedbackBufferDataStride = 2048;
2112          props->transformFeedbackQueries = true;
2113          props->transformFeedbackStreamsLinesTriangles = false;
2114          props->transformFeedbackRasterizationStreamSelect = false;
2115          props->transformFeedbackDraw = true;
2116          break;
2117       }
2118 
2119       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
2120          VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
2121             (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
2122          /* We have to restrict this a bit for multiview */
2123          props->maxVertexAttribDivisor = UINT32_MAX / 16;
2124          break;
2125       }
2126 
2127       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES:
2128          anv_get_physical_device_properties_1_1(pdevice, (void *)ext);
2129          break;
2130 
2131       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES:
2132          anv_get_physical_device_properties_1_2(pdevice, (void *)ext);
2133          break;
2134 
2135       default:
2136          anv_debug_ignored_stype(ext->sType);
2137          break;
2138       }
2139    }
2140 
2141 #undef CORE_RENAMED_PROPERTY
2142 #undef CORE_PROPERTY
2143 }
2144 
2145 /* We support exactly one queue family. */
2146 static const VkQueueFamilyProperties
2147 anv_queue_family_properties = {
2148    .queueFlags = VK_QUEUE_GRAPHICS_BIT |
2149                  VK_QUEUE_COMPUTE_BIT |
2150                  VK_QUEUE_TRANSFER_BIT,
2151    .queueCount = 1,
2152    .timestampValidBits = 36, /* XXX: Real value here */
2153    .minImageTransferGranularity = { 1, 1, 1 },
2154 };
2155 
anv_GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,uint32_t * pCount,VkQueueFamilyProperties * pQueueFamilyProperties)2156 void anv_GetPhysicalDeviceQueueFamilyProperties(
2157     VkPhysicalDevice                            physicalDevice,
2158     uint32_t*                                   pCount,
2159     VkQueueFamilyProperties*                    pQueueFamilyProperties)
2160 {
2161    VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
2162 
2163    vk_outarray_append(&out, p) {
2164       *p = anv_queue_family_properties;
2165    }
2166 }
2167 
anv_GetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,uint32_t * pQueueFamilyPropertyCount,VkQueueFamilyProperties2 * pQueueFamilyProperties)2168 void anv_GetPhysicalDeviceQueueFamilyProperties2(
2169     VkPhysicalDevice                            physicalDevice,
2170     uint32_t*                                   pQueueFamilyPropertyCount,
2171     VkQueueFamilyProperties2*                   pQueueFamilyProperties)
2172 {
2173 
2174    VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
2175 
2176    vk_outarray_append(&out, p) {
2177       p->queueFamilyProperties = anv_queue_family_properties;
2178 
2179       vk_foreach_struct(s, p->pNext) {
2180          anv_debug_ignored_stype(s->sType);
2181       }
2182    }
2183 }
2184 
anv_GetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice,VkPhysicalDeviceMemoryProperties * pMemoryProperties)2185 void anv_GetPhysicalDeviceMemoryProperties(
2186     VkPhysicalDevice                            physicalDevice,
2187     VkPhysicalDeviceMemoryProperties*           pMemoryProperties)
2188 {
2189    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
2190 
2191    pMemoryProperties->memoryTypeCount = physical_device->memory.type_count;
2192    for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
2193       pMemoryProperties->memoryTypes[i] = (VkMemoryType) {
2194          .propertyFlags = physical_device->memory.types[i].propertyFlags,
2195          .heapIndex     = physical_device->memory.types[i].heapIndex,
2196       };
2197    }
2198 
2199    pMemoryProperties->memoryHeapCount = physical_device->memory.heap_count;
2200    for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
2201       pMemoryProperties->memoryHeaps[i] = (VkMemoryHeap) {
2202          .size    = physical_device->memory.heaps[i].size,
2203          .flags   = physical_device->memory.heaps[i].flags,
2204       };
2205    }
2206 }
2207 
2208 static void
anv_get_memory_budget(VkPhysicalDevice physicalDevice,VkPhysicalDeviceMemoryBudgetPropertiesEXT * memoryBudget)2209 anv_get_memory_budget(VkPhysicalDevice physicalDevice,
2210                       VkPhysicalDeviceMemoryBudgetPropertiesEXT *memoryBudget)
2211 {
2212    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
2213    uint64_t sys_available;
2214    ASSERTED bool has_available_memory =
2215       os_get_available_system_memory(&sys_available);
2216    assert(has_available_memory);
2217 
2218    VkDeviceSize total_heaps_size = 0;
2219    for (size_t i = 0; i < device->memory.heap_count; i++)
2220          total_heaps_size += device->memory.heaps[i].size;
2221 
2222    for (size_t i = 0; i < device->memory.heap_count; i++) {
2223       VkDeviceSize heap_size = device->memory.heaps[i].size;
2224       VkDeviceSize heap_used = device->memory.heaps[i].used;
2225       VkDeviceSize heap_budget;
2226 
2227       double heap_proportion = (double) heap_size / total_heaps_size;
2228       VkDeviceSize sys_available_prop = sys_available * heap_proportion;
2229 
2230       /*
2231        * Let's not incite the app to starve the system: report at most 90% of
2232        * available system memory.
2233        */
2234       uint64_t heap_available = sys_available_prop * 9 / 10;
2235       heap_budget = MIN2(heap_size, heap_used + heap_available);
2236 
2237       /*
2238        * Round down to the nearest MB
2239        */
2240       heap_budget &= ~((1ull << 20) - 1);
2241 
2242       /*
2243        * The heapBudget value must be non-zero for array elements less than
2244        * VkPhysicalDeviceMemoryProperties::memoryHeapCount. The heapBudget
2245        * value must be less than or equal to VkMemoryHeap::size for each heap.
2246        */
2247       assert(0 < heap_budget && heap_budget <= heap_size);
2248 
2249       memoryBudget->heapUsage[i] = heap_used;
2250       memoryBudget->heapBudget[i] = heap_budget;
2251    }
2252 
2253    /* The heapBudget and heapUsage values must be zero for array elements
2254     * greater than or equal to VkPhysicalDeviceMemoryProperties::memoryHeapCount
2255     */
2256    for (uint32_t i = device->memory.heap_count; i < VK_MAX_MEMORY_HEAPS; i++) {
2257       memoryBudget->heapBudget[i] = 0;
2258       memoryBudget->heapUsage[i] = 0;
2259    }
2260 }
2261 
anv_GetPhysicalDeviceMemoryProperties2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceMemoryProperties2 * pMemoryProperties)2262 void anv_GetPhysicalDeviceMemoryProperties2(
2263     VkPhysicalDevice                            physicalDevice,
2264     VkPhysicalDeviceMemoryProperties2*          pMemoryProperties)
2265 {
2266    anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
2267                                          &pMemoryProperties->memoryProperties);
2268 
2269    vk_foreach_struct(ext, pMemoryProperties->pNext) {
2270       switch (ext->sType) {
2271       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT:
2272          anv_get_memory_budget(physicalDevice, (void*)ext);
2273          break;
2274       default:
2275          anv_debug_ignored_stype(ext->sType);
2276          break;
2277       }
2278    }
2279 }
2280 
2281 void
anv_GetDeviceGroupPeerMemoryFeatures(VkDevice device,uint32_t heapIndex,uint32_t localDeviceIndex,uint32_t remoteDeviceIndex,VkPeerMemoryFeatureFlags * pPeerMemoryFeatures)2282 anv_GetDeviceGroupPeerMemoryFeatures(
2283     VkDevice                                    device,
2284     uint32_t                                    heapIndex,
2285     uint32_t                                    localDeviceIndex,
2286     uint32_t                                    remoteDeviceIndex,
2287     VkPeerMemoryFeatureFlags*                   pPeerMemoryFeatures)
2288 {
2289    assert(localDeviceIndex == 0 && remoteDeviceIndex == 0);
2290    *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
2291                           VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
2292                           VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
2293                           VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
2294 }
2295 
anv_GetInstanceProcAddr(VkInstance _instance,const char * pName)2296 PFN_vkVoidFunction anv_GetInstanceProcAddr(
2297     VkInstance                                  _instance,
2298     const char*                                 pName)
2299 {
2300    ANV_FROM_HANDLE(anv_instance, instance, _instance);
2301 
2302    /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
2303     * when we have to return valid function pointers, NULL, or it's left
2304     * undefined.  See the table for exact details.
2305     */
2306    if (pName == NULL)
2307       return NULL;
2308 
2309 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
2310    if (strcmp(pName, "vk" #entrypoint) == 0) \
2311       return (PFN_vkVoidFunction)anv_##entrypoint
2312 
2313    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties);
2314    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties);
2315    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion);
2316    LOOKUP_ANV_ENTRYPOINT(CreateInstance);
2317 
2318    /* GetInstanceProcAddr() can also be called with a NULL instance.
2319     * See https://gitlab.khronos.org/vulkan/vulkan/issues/2057
2320     */
2321    LOOKUP_ANV_ENTRYPOINT(GetInstanceProcAddr);
2322 
2323 #undef LOOKUP_ANV_ENTRYPOINT
2324 
2325    if (instance == NULL)
2326       return NULL;
2327 
2328    int idx = anv_get_instance_entrypoint_index(pName);
2329    if (idx >= 0)
2330       return instance->dispatch.entrypoints[idx];
2331 
2332    idx = anv_get_physical_device_entrypoint_index(pName);
2333    if (idx >= 0)
2334       return instance->physical_device_dispatch.entrypoints[idx];
2335 
2336    idx = anv_get_device_entrypoint_index(pName);
2337    if (idx >= 0)
2338       return instance->device_dispatch.entrypoints[idx];
2339 
2340    return NULL;
2341 }
2342 
2343 /* With version 1+ of the loader interface the ICD should expose
2344  * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
2345  */
2346 PUBLIC
2347 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2348     VkInstance                                  instance,
2349     const char*                                 pName);
2350 
2351 PUBLIC
vk_icdGetInstanceProcAddr(VkInstance instance,const char * pName)2352 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
2353     VkInstance                                  instance,
2354     const char*                                 pName)
2355 {
2356    return anv_GetInstanceProcAddr(instance, pName);
2357 }
2358 
anv_GetDeviceProcAddr(VkDevice _device,const char * pName)2359 PFN_vkVoidFunction anv_GetDeviceProcAddr(
2360     VkDevice                                    _device,
2361     const char*                                 pName)
2362 {
2363    ANV_FROM_HANDLE(anv_device, device, _device);
2364 
2365    if (!device || !pName)
2366       return NULL;
2367 
2368    int idx = anv_get_device_entrypoint_index(pName);
2369    if (idx < 0)
2370       return NULL;
2371 
2372    return device->dispatch.entrypoints[idx];
2373 }
2374 
2375 /* With version 4+ of the loader interface the ICD should expose
2376  * vk_icdGetPhysicalDeviceProcAddr()
2377  */
2378 PUBLIC
2379 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetPhysicalDeviceProcAddr(
2380     VkInstance  _instance,
2381     const char* pName);
2382 
vk_icdGetPhysicalDeviceProcAddr(VkInstance _instance,const char * pName)2383 PFN_vkVoidFunction vk_icdGetPhysicalDeviceProcAddr(
2384     VkInstance  _instance,
2385     const char* pName)
2386 {
2387    ANV_FROM_HANDLE(anv_instance, instance, _instance);
2388 
2389    if (!pName || !instance)
2390       return NULL;
2391 
2392    int idx = anv_get_physical_device_entrypoint_index(pName);
2393    if (idx < 0)
2394       return NULL;
2395 
2396    return instance->physical_device_dispatch.entrypoints[idx];
2397 }
2398 
2399 
2400 VkResult
anv_CreateDebugReportCallbackEXT(VkInstance _instance,const VkDebugReportCallbackCreateInfoEXT * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDebugReportCallbackEXT * pCallback)2401 anv_CreateDebugReportCallbackEXT(VkInstance _instance,
2402                                  const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
2403                                  const VkAllocationCallbacks* pAllocator,
2404                                  VkDebugReportCallbackEXT* pCallback)
2405 {
2406    ANV_FROM_HANDLE(anv_instance, instance, _instance);
2407    return vk_create_debug_report_callback(&instance->debug_report_callbacks,
2408                                           pCreateInfo, pAllocator, &instance->alloc,
2409                                           pCallback);
2410 }
2411 
2412 void
anv_DestroyDebugReportCallbackEXT(VkInstance _instance,VkDebugReportCallbackEXT _callback,const VkAllocationCallbacks * pAllocator)2413 anv_DestroyDebugReportCallbackEXT(VkInstance _instance,
2414                                   VkDebugReportCallbackEXT _callback,
2415                                   const VkAllocationCallbacks* pAllocator)
2416 {
2417    ANV_FROM_HANDLE(anv_instance, instance, _instance);
2418    vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
2419                                     _callback, pAllocator, &instance->alloc);
2420 }
2421 
2422 void
anv_DebugReportMessageEXT(VkInstance _instance,VkDebugReportFlagsEXT flags,VkDebugReportObjectTypeEXT objectType,uint64_t object,size_t location,int32_t messageCode,const char * pLayerPrefix,const char * pMessage)2423 anv_DebugReportMessageEXT(VkInstance _instance,
2424                           VkDebugReportFlagsEXT flags,
2425                           VkDebugReportObjectTypeEXT objectType,
2426                           uint64_t object,
2427                           size_t location,
2428                           int32_t messageCode,
2429                           const char* pLayerPrefix,
2430                           const char* pMessage)
2431 {
2432    ANV_FROM_HANDLE(anv_instance, instance, _instance);
2433    vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
2434                    object, location, messageCode, pLayerPrefix, pMessage);
2435 }
2436 
2437 static struct anv_state
anv_state_pool_emit_data(struct anv_state_pool * pool,size_t size,size_t align,const void * p)2438 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
2439 {
2440    struct anv_state state;
2441 
2442    state = anv_state_pool_alloc(pool, size, align);
2443    memcpy(state.map, p, size);
2444 
2445    return state;
2446 }
2447 
2448 static void
anv_device_init_border_colors(struct anv_device * device)2449 anv_device_init_border_colors(struct anv_device *device)
2450 {
2451    if (device->info.is_haswell) {
2452       static const struct hsw_border_color border_colors[] = {
2453          [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] =  { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
2454          [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] =       { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
2455          [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] =       { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
2456          [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] =    { .uint32 = { 0, 0, 0, 0 } },
2457          [VK_BORDER_COLOR_INT_OPAQUE_BLACK] =         { .uint32 = { 0, 0, 0, 1 } },
2458          [VK_BORDER_COLOR_INT_OPAQUE_WHITE] =         { .uint32 = { 1, 1, 1, 1 } },
2459       };
2460 
2461       device->border_colors =
2462          anv_state_pool_emit_data(&device->dynamic_state_pool,
2463                                   sizeof(border_colors), 512, border_colors);
2464    } else {
2465       static const struct gen8_border_color border_colors[] = {
2466          [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] =  { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
2467          [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] =       { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
2468          [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] =       { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
2469          [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] =    { .uint32 = { 0, 0, 0, 0 } },
2470          [VK_BORDER_COLOR_INT_OPAQUE_BLACK] =         { .uint32 = { 0, 0, 0, 1 } },
2471          [VK_BORDER_COLOR_INT_OPAQUE_WHITE] =         { .uint32 = { 1, 1, 1, 1 } },
2472       };
2473 
2474       device->border_colors =
2475          anv_state_pool_emit_data(&device->dynamic_state_pool,
2476                                   sizeof(border_colors), 64, border_colors);
2477    }
2478 }
2479 
2480 static VkResult
anv_device_init_trivial_batch(struct anv_device * device)2481 anv_device_init_trivial_batch(struct anv_device *device)
2482 {
2483    VkResult result = anv_device_alloc_bo(device, 4096,
2484                                          ANV_BO_ALLOC_MAPPED,
2485                                          0 /* explicit_address */,
2486                                          &device->trivial_batch_bo);
2487    if (result != VK_SUCCESS)
2488       return result;
2489 
2490    struct anv_batch batch = {
2491       .start = device->trivial_batch_bo->map,
2492       .next = device->trivial_batch_bo->map,
2493       .end = device->trivial_batch_bo->map + 4096,
2494    };
2495 
2496    anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
2497    anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
2498 
2499    if (!device->info.has_llc)
2500       gen_clflush_range(batch.start, batch.next - batch.start);
2501 
2502    return VK_SUCCESS;
2503 }
2504 
anv_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)2505 VkResult anv_EnumerateDeviceExtensionProperties(
2506     VkPhysicalDevice                            physicalDevice,
2507     const char*                                 pLayerName,
2508     uint32_t*                                   pPropertyCount,
2509     VkExtensionProperties*                      pProperties)
2510 {
2511    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
2512    VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
2513 
2514    for (int i = 0; i < ANV_DEVICE_EXTENSION_COUNT; i++) {
2515       if (device->supported_extensions.extensions[i]) {
2516          vk_outarray_append(&out, prop) {
2517             *prop = anv_device_extensions[i];
2518          }
2519       }
2520    }
2521 
2522    return vk_outarray_status(&out);
2523 }
2524 
2525 static int
vk_priority_to_gen(int priority)2526 vk_priority_to_gen(int priority)
2527 {
2528    switch (priority) {
2529    case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
2530       return GEN_CONTEXT_LOW_PRIORITY;
2531    case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
2532       return GEN_CONTEXT_MEDIUM_PRIORITY;
2533    case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
2534       return GEN_CONTEXT_HIGH_PRIORITY;
2535    case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
2536       return GEN_CONTEXT_REALTIME_PRIORITY;
2537    default:
2538       unreachable("Invalid priority");
2539    }
2540 }
2541 
2542 static VkResult
anv_device_init_hiz_clear_value_bo(struct anv_device * device)2543 anv_device_init_hiz_clear_value_bo(struct anv_device *device)
2544 {
2545    VkResult result = anv_device_alloc_bo(device, 4096,
2546                                          ANV_BO_ALLOC_MAPPED,
2547                                          0 /* explicit_address */,
2548                                          &device->hiz_clear_bo);
2549    if (result != VK_SUCCESS)
2550       return result;
2551 
2552    union isl_color_value hiz_clear = { .u32 = { 0, } };
2553    hiz_clear.f32[0] = ANV_HZ_FC_VAL;
2554 
2555    memcpy(device->hiz_clear_bo->map, hiz_clear.u32, sizeof(hiz_clear.u32));
2556 
2557    if (!device->info.has_llc)
2558       gen_clflush_range(device->hiz_clear_bo->map, sizeof(hiz_clear.u32));
2559 
2560    return VK_SUCCESS;
2561 }
2562 
2563 static bool
get_bo_from_pool(struct gen_batch_decode_bo * ret,struct anv_block_pool * pool,uint64_t address)2564 get_bo_from_pool(struct gen_batch_decode_bo *ret,
2565                  struct anv_block_pool *pool,
2566                  uint64_t address)
2567 {
2568    anv_block_pool_foreach_bo(bo, pool) {
2569       uint64_t bo_address = gen_48b_address(bo->offset);
2570       if (address >= bo_address && address < (bo_address + bo->size)) {
2571          *ret = (struct gen_batch_decode_bo) {
2572             .addr = bo_address,
2573             .size = bo->size,
2574             .map = bo->map,
2575          };
2576          return true;
2577       }
2578    }
2579    return false;
2580 }
2581 
2582 /* Finding a buffer for batch decoding */
2583 static struct gen_batch_decode_bo
decode_get_bo(void * v_batch,bool ppgtt,uint64_t address)2584 decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
2585 {
2586    struct anv_device *device = v_batch;
2587    struct gen_batch_decode_bo ret_bo = {};
2588 
2589    assert(ppgtt);
2590 
2591    if (get_bo_from_pool(&ret_bo, &device->dynamic_state_pool.block_pool, address))
2592       return ret_bo;
2593    if (get_bo_from_pool(&ret_bo, &device->instruction_state_pool.block_pool, address))
2594       return ret_bo;
2595    if (get_bo_from_pool(&ret_bo, &device->binding_table_pool.block_pool, address))
2596       return ret_bo;
2597    if (get_bo_from_pool(&ret_bo, &device->surface_state_pool.block_pool, address))
2598       return ret_bo;
2599 
2600    if (!device->cmd_buffer_being_decoded)
2601       return (struct gen_batch_decode_bo) { };
2602 
2603    struct anv_batch_bo **bo;
2604 
2605    u_vector_foreach(bo, &device->cmd_buffer_being_decoded->seen_bbos) {
2606       /* The decoder zeroes out the top 16 bits, so we need to as well */
2607       uint64_t bo_address = (*bo)->bo->offset & (~0ull >> 16);
2608 
2609       if (address >= bo_address && address < bo_address + (*bo)->bo->size) {
2610          return (struct gen_batch_decode_bo) {
2611             .addr = bo_address,
2612             .size = (*bo)->bo->size,
2613             .map = (*bo)->bo->map,
2614          };
2615       }
2616    }
2617 
2618    return (struct gen_batch_decode_bo) { };
2619 }
2620 
2621 struct gen_aux_map_buffer {
2622    struct gen_buffer base;
2623    struct anv_state state;
2624 };
2625 
2626 static struct gen_buffer *
gen_aux_map_buffer_alloc(void * driver_ctx,uint32_t size)2627 gen_aux_map_buffer_alloc(void *driver_ctx, uint32_t size)
2628 {
2629    struct gen_aux_map_buffer *buf = malloc(sizeof(struct gen_aux_map_buffer));
2630    if (!buf)
2631       return NULL;
2632 
2633    struct anv_device *device = (struct anv_device*)driver_ctx;
2634    assert(device->physical->supports_48bit_addresses &&
2635           device->physical->use_softpin);
2636 
2637    struct anv_state_pool *pool = &device->dynamic_state_pool;
2638    buf->state = anv_state_pool_alloc(pool, size, size);
2639 
2640    buf->base.gpu = pool->block_pool.bo->offset + buf->state.offset;
2641    buf->base.gpu_end = buf->base.gpu + buf->state.alloc_size;
2642    buf->base.map = buf->state.map;
2643    buf->base.driver_bo = &buf->state;
2644    return &buf->base;
2645 }
2646 
2647 static void
gen_aux_map_buffer_free(void * driver_ctx,struct gen_buffer * buffer)2648 gen_aux_map_buffer_free(void *driver_ctx, struct gen_buffer *buffer)
2649 {
2650    struct gen_aux_map_buffer *buf = (struct gen_aux_map_buffer*)buffer;
2651    struct anv_device *device = (struct anv_device*)driver_ctx;
2652    struct anv_state_pool *pool = &device->dynamic_state_pool;
2653    anv_state_pool_free(pool, buf->state);
2654    free(buf);
2655 }
2656 
2657 static struct gen_mapped_pinned_buffer_alloc aux_map_allocator = {
2658    .alloc = gen_aux_map_buffer_alloc,
2659    .free = gen_aux_map_buffer_free,
2660 };
2661 
2662 static VkResult
check_physical_device_features(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceFeatures * features)2663 check_physical_device_features(VkPhysicalDevice physicalDevice,
2664                                const VkPhysicalDeviceFeatures *features)
2665 {
2666    VkPhysicalDeviceFeatures supported_features;
2667    anv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
2668    VkBool32 *supported_feature = (VkBool32 *)&supported_features;
2669    VkBool32 *enabled_feature = (VkBool32 *)features;
2670    unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
2671    for (uint32_t i = 0; i < num_features; i++) {
2672       if (enabled_feature[i] && !supported_feature[i])
2673          return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
2674    }
2675 
2676    return VK_SUCCESS;
2677 }
2678 
anv_CreateDevice(VkPhysicalDevice physicalDevice,const VkDeviceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDevice * pDevice)2679 VkResult anv_CreateDevice(
2680     VkPhysicalDevice                            physicalDevice,
2681     const VkDeviceCreateInfo*                   pCreateInfo,
2682     const VkAllocationCallbacks*                pAllocator,
2683     VkDevice*                                   pDevice)
2684 {
2685    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
2686    VkResult result;
2687    struct anv_device *device;
2688 
2689    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
2690 
2691    struct anv_device_extension_table enabled_extensions = { };
2692    for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
2693       int idx;
2694       for (idx = 0; idx < ANV_DEVICE_EXTENSION_COUNT; idx++) {
2695          if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
2696                     anv_device_extensions[idx].extensionName) == 0)
2697             break;
2698       }
2699 
2700       if (idx >= ANV_DEVICE_EXTENSION_COUNT)
2701          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
2702 
2703       if (!physical_device->supported_extensions.extensions[idx])
2704          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
2705 
2706       enabled_extensions.extensions[idx] = true;
2707    }
2708 
2709    /* Check enabled features */
2710    bool robust_buffer_access = false;
2711    if (pCreateInfo->pEnabledFeatures) {
2712       result = check_physical_device_features(physicalDevice,
2713                                               pCreateInfo->pEnabledFeatures);
2714       if (result != VK_SUCCESS)
2715          return result;
2716 
2717       if (pCreateInfo->pEnabledFeatures->robustBufferAccess)
2718          robust_buffer_access = true;
2719    }
2720 
2721    vk_foreach_struct_const(ext, pCreateInfo->pNext) {
2722       switch (ext->sType) {
2723       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2: {
2724          const VkPhysicalDeviceFeatures2 *features = (const void *)ext;
2725          result = check_physical_device_features(physicalDevice,
2726                                                  &features->features);
2727          if (result != VK_SUCCESS)
2728             return result;
2729 
2730          if (features->features.robustBufferAccess)
2731             robust_buffer_access = true;
2732          break;
2733       }
2734 
2735       default:
2736          /* Don't warn */
2737          break;
2738       }
2739    }
2740 
2741    /* Check requested queues and fail if we are requested to create any
2742     * queues with flags we don't support.
2743     */
2744    assert(pCreateInfo->queueCreateInfoCount > 0);
2745    for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
2746       if (pCreateInfo->pQueueCreateInfos[i].flags != 0)
2747          return vk_error(VK_ERROR_INITIALIZATION_FAILED);
2748    }
2749 
2750    /* Check if client specified queue priority. */
2751    const VkDeviceQueueGlobalPriorityCreateInfoEXT *queue_priority =
2752       vk_find_struct_const(pCreateInfo->pQueueCreateInfos[0].pNext,
2753                            DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
2754 
2755    VkQueueGlobalPriorityEXT priority =
2756       queue_priority ? queue_priority->globalPriority :
2757          VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
2758 
2759    device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
2760                        sizeof(*device), 8,
2761                        VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
2762    if (!device)
2763       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2764 
2765    vk_device_init(&device->vk, pCreateInfo,
2766                   &physical_device->instance->alloc, pAllocator);
2767 
2768    if (INTEL_DEBUG & DEBUG_BATCH) {
2769       const unsigned decode_flags =
2770          GEN_BATCH_DECODE_FULL |
2771          ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
2772          GEN_BATCH_DECODE_OFFSETS |
2773          GEN_BATCH_DECODE_FLOATS;
2774 
2775       gen_batch_decode_ctx_init(&device->decoder_ctx,
2776                                 &physical_device->info,
2777                                 stderr, decode_flags, NULL,
2778                                 decode_get_bo, NULL, device);
2779    }
2780 
2781    device->physical = physical_device;
2782    device->no_hw = physical_device->no_hw;
2783    device->_lost = false;
2784 
2785    /* XXX(chadv): Can we dup() physicalDevice->fd here? */
2786    device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
2787    if (device->fd == -1) {
2788       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2789       goto fail_device;
2790    }
2791 
2792    device->context_id = anv_gem_create_context(device);
2793    if (device->context_id == -1) {
2794       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2795       goto fail_fd;
2796    }
2797 
2798    result = anv_queue_init(device, &device->queue);
2799    if (result != VK_SUCCESS)
2800       goto fail_context_id;
2801 
2802    if (physical_device->use_softpin) {
2803       if (pthread_mutex_init(&device->vma_mutex, NULL) != 0) {
2804          result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2805          goto fail_queue;
2806       }
2807 
2808       /* keep the page with address zero out of the allocator */
2809       util_vma_heap_init(&device->vma_lo,
2810                          LOW_HEAP_MIN_ADDRESS, LOW_HEAP_SIZE);
2811 
2812       util_vma_heap_init(&device->vma_cva, CLIENT_VISIBLE_HEAP_MIN_ADDRESS,
2813                          CLIENT_VISIBLE_HEAP_SIZE);
2814 
2815       /* Leave the last 4GiB out of the high vma range, so that no state
2816        * base address + size can overflow 48 bits. For more information see
2817        * the comment about Wa32bitGeneralStateOffset in anv_allocator.c
2818        */
2819       util_vma_heap_init(&device->vma_hi, HIGH_HEAP_MIN_ADDRESS,
2820                          physical_device->gtt_size - (1ull << 32) -
2821                          HIGH_HEAP_MIN_ADDRESS);
2822    }
2823 
2824    list_inithead(&device->memory_objects);
2825 
2826    /* As per spec, the driver implementation may deny requests to acquire
2827     * a priority above the default priority (MEDIUM) if the caller does not
2828     * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
2829     * is returned.
2830     */
2831    if (physical_device->has_context_priority) {
2832       int err = anv_gem_set_context_param(device->fd, device->context_id,
2833                                           I915_CONTEXT_PARAM_PRIORITY,
2834                                           vk_priority_to_gen(priority));
2835       if (err != 0 && priority > VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT) {
2836          result = vk_error(VK_ERROR_NOT_PERMITTED_EXT);
2837          goto fail_vmas;
2838       }
2839    }
2840 
2841    device->info = physical_device->info;
2842    device->isl_dev = physical_device->isl_dev;
2843 
2844    /* On Broadwell and later, we can use batch chaining to more efficiently
2845     * implement growing command buffers.  Prior to Haswell, the kernel
2846     * command parser gets in the way and we have to fall back to growing
2847     * the batch.
2848     */
2849    device->can_chain_batches = device->info.gen >= 8;
2850 
2851    device->robust_buffer_access = robust_buffer_access;
2852    device->enabled_extensions = enabled_extensions;
2853 
2854    const struct anv_instance *instance = physical_device->instance;
2855    for (unsigned i = 0; i < ARRAY_SIZE(device->dispatch.entrypoints); i++) {
2856       /* Vulkan requires that entrypoints for extensions which have not been
2857        * enabled must not be advertised.
2858        */
2859       if (!anv_device_entrypoint_is_enabled(i, instance->app_info.api_version,
2860                                             &instance->enabled_extensions,
2861                                             &device->enabled_extensions)) {
2862          device->dispatch.entrypoints[i] = NULL;
2863       } else {
2864          device->dispatch.entrypoints[i] =
2865             anv_resolve_device_entrypoint(&device->info, i);
2866       }
2867    }
2868 
2869    if (pthread_mutex_init(&device->mutex, NULL) != 0) {
2870       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2871       goto fail_queue;
2872    }
2873 
2874    pthread_condattr_t condattr;
2875    if (pthread_condattr_init(&condattr) != 0) {
2876       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2877       goto fail_mutex;
2878    }
2879    if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
2880       pthread_condattr_destroy(&condattr);
2881       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2882       goto fail_mutex;
2883    }
2884    if (pthread_cond_init(&device->queue_submit, &condattr) != 0) {
2885       pthread_condattr_destroy(&condattr);
2886       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
2887       goto fail_mutex;
2888    }
2889    pthread_condattr_destroy(&condattr);
2890 
2891    result = anv_bo_cache_init(&device->bo_cache);
2892    if (result != VK_SUCCESS)
2893       goto fail_queue_cond;
2894 
2895    anv_bo_pool_init(&device->batch_bo_pool, device);
2896 
2897    result = anv_state_pool_init(&device->dynamic_state_pool, device,
2898                                 DYNAMIC_STATE_POOL_MIN_ADDRESS, 0, 16384);
2899    if (result != VK_SUCCESS)
2900       goto fail_batch_bo_pool;
2901 
2902    if (device->info.gen >= 8) {
2903       /* The border color pointer is limited to 24 bits, so we need to make
2904        * sure that any such color used at any point in the program doesn't
2905        * exceed that limit.
2906        * We achieve that by reserving all the custom border colors we support
2907        * right off the bat, so they are close to the base address.
2908        */
2909       anv_state_reserved_pool_init(&device->custom_border_colors,
2910                                    &device->dynamic_state_pool,
2911                                    MAX_CUSTOM_BORDER_COLORS,
2912                                    sizeof(struct gen8_border_color), 64);
2913    }
2914 
2915    result = anv_state_pool_init(&device->instruction_state_pool, device,
2916                                 INSTRUCTION_STATE_POOL_MIN_ADDRESS, 0, 16384);
2917    if (result != VK_SUCCESS)
2918       goto fail_dynamic_state_pool;
2919 
2920    result = anv_state_pool_init(&device->surface_state_pool, device,
2921                                 SURFACE_STATE_POOL_MIN_ADDRESS, 0, 4096);
2922    if (result != VK_SUCCESS)
2923       goto fail_instruction_state_pool;
2924 
2925    if (physical_device->use_softpin) {
2926       int64_t bt_pool_offset = (int64_t)BINDING_TABLE_POOL_MIN_ADDRESS -
2927                                (int64_t)SURFACE_STATE_POOL_MIN_ADDRESS;
2928       assert(INT32_MIN < bt_pool_offset && bt_pool_offset < 0);
2929       result = anv_state_pool_init(&device->binding_table_pool, device,
2930                                    SURFACE_STATE_POOL_MIN_ADDRESS,
2931                                    bt_pool_offset, 4096);
2932       if (result != VK_SUCCESS)
2933          goto fail_surface_state_pool;
2934    }
2935 
2936    if (device->info.has_aux_map) {
2937       device->aux_map_ctx = gen_aux_map_init(device, &aux_map_allocator,
2938                                              &physical_device->info);
2939       if (!device->aux_map_ctx)
2940          goto fail_binding_table_pool;
2941    }
2942 
2943    result = anv_device_alloc_bo(device, 4096,
2944                                 ANV_BO_ALLOC_CAPTURE | ANV_BO_ALLOC_MAPPED /* flags */,
2945                                 0 /* explicit_address */,
2946                                 &device->workaround_bo);
2947    if (result != VK_SUCCESS)
2948       goto fail_surface_aux_map_pool;
2949 
2950    device->workaround_address = (struct anv_address) {
2951       .bo = device->workaround_bo,
2952       .offset = align_u32(
2953          intel_debug_write_identifiers(device->workaround_bo->map,
2954                                        device->workaround_bo->size,
2955                                        "Anv") + 8, 8),
2956    };
2957 
2958    if (!device->info.has_llc) {
2959       gen_clflush_range(device->workaround_bo->map,
2960                         device->workaround_address.offset);
2961    }
2962 
2963    result = anv_device_init_trivial_batch(device);
2964    if (result != VK_SUCCESS)
2965       goto fail_workaround_bo;
2966 
2967    /* Allocate a null surface state at surface state offset 0.  This makes
2968     * NULL descriptor handling trivial because we can just memset structures
2969     * to zero and they have a valid descriptor.
2970     */
2971    device->null_surface_state =
2972       anv_state_pool_alloc(&device->surface_state_pool,
2973                            device->isl_dev.ss.size,
2974                            device->isl_dev.ss.align);
2975    isl_null_fill_state(&device->isl_dev, device->null_surface_state.map,
2976                        isl_extent3d(1, 1, 1) /* This shouldn't matter */);
2977    assert(device->null_surface_state.offset == 0);
2978 
2979    if (device->info.gen >= 10) {
2980       result = anv_device_init_hiz_clear_value_bo(device);
2981       if (result != VK_SUCCESS)
2982          goto fail_trivial_batch_bo;
2983    }
2984 
2985    anv_scratch_pool_init(device, &device->scratch_pool);
2986 
2987    switch (device->info.gen) {
2988    case 7:
2989       if (!device->info.is_haswell)
2990          result = gen7_init_device_state(device);
2991       else
2992          result = gen75_init_device_state(device);
2993       break;
2994    case 8:
2995       result = gen8_init_device_state(device);
2996       break;
2997    case 9:
2998       result = gen9_init_device_state(device);
2999       break;
3000    case 10:
3001       result = gen10_init_device_state(device);
3002       break;
3003    case 11:
3004       result = gen11_init_device_state(device);
3005       break;
3006    case 12:
3007       result = gen12_init_device_state(device);
3008       break;
3009    default:
3010       /* Shouldn't get here as we don't create physical devices for any other
3011        * gens. */
3012       unreachable("unhandled gen");
3013    }
3014    if (result != VK_SUCCESS)
3015       goto fail_clear_value_bo;
3016 
3017    anv_pipeline_cache_init(&device->default_pipeline_cache, device,
3018                            true /* cache_enabled */, false /* external_sync */);
3019 
3020    anv_device_init_blorp(device);
3021 
3022    anv_device_init_border_colors(device);
3023 
3024    anv_device_perf_init(device);
3025 
3026    *pDevice = anv_device_to_handle(device);
3027 
3028    return VK_SUCCESS;
3029 
3030  fail_clear_value_bo:
3031    if (device->info.gen >= 10)
3032       anv_device_release_bo(device, device->hiz_clear_bo);
3033    anv_scratch_pool_finish(device, &device->scratch_pool);
3034  fail_trivial_batch_bo:
3035    anv_device_release_bo(device, device->trivial_batch_bo);
3036  fail_workaround_bo:
3037    anv_device_release_bo(device, device->workaround_bo);
3038  fail_surface_aux_map_pool:
3039    if (device->info.has_aux_map) {
3040       gen_aux_map_finish(device->aux_map_ctx);
3041       device->aux_map_ctx = NULL;
3042    }
3043  fail_binding_table_pool:
3044    if (physical_device->use_softpin)
3045       anv_state_pool_finish(&device->binding_table_pool);
3046  fail_surface_state_pool:
3047    anv_state_pool_finish(&device->surface_state_pool);
3048  fail_instruction_state_pool:
3049    anv_state_pool_finish(&device->instruction_state_pool);
3050  fail_dynamic_state_pool:
3051    if (device->info.gen >= 8)
3052       anv_state_reserved_pool_finish(&device->custom_border_colors);
3053    anv_state_pool_finish(&device->dynamic_state_pool);
3054  fail_batch_bo_pool:
3055    anv_bo_pool_finish(&device->batch_bo_pool);
3056    anv_bo_cache_finish(&device->bo_cache);
3057  fail_queue_cond:
3058    pthread_cond_destroy(&device->queue_submit);
3059  fail_mutex:
3060    pthread_mutex_destroy(&device->mutex);
3061  fail_vmas:
3062    if (physical_device->use_softpin) {
3063       util_vma_heap_finish(&device->vma_hi);
3064       util_vma_heap_finish(&device->vma_cva);
3065       util_vma_heap_finish(&device->vma_lo);
3066    }
3067  fail_queue:
3068    anv_queue_finish(&device->queue);
3069  fail_context_id:
3070    anv_gem_destroy_context(device, device->context_id);
3071  fail_fd:
3072    close(device->fd);
3073  fail_device:
3074    vk_free(&device->vk.alloc, device);
3075 
3076    return result;
3077 }
3078 
anv_DestroyDevice(VkDevice _device,const VkAllocationCallbacks * pAllocator)3079 void anv_DestroyDevice(
3080     VkDevice                                    _device,
3081     const VkAllocationCallbacks*                pAllocator)
3082 {
3083    ANV_FROM_HANDLE(anv_device, device, _device);
3084 
3085    if (!device)
3086       return;
3087 
3088    anv_device_finish_blorp(device);
3089 
3090    anv_pipeline_cache_finish(&device->default_pipeline_cache);
3091 
3092    anv_queue_finish(&device->queue);
3093 
3094 #ifdef HAVE_VALGRIND
3095    /* We only need to free these to prevent valgrind errors.  The backing
3096     * BO will go away in a couple of lines so we don't actually leak.
3097     */
3098    if (device->info.gen >= 8)
3099       anv_state_reserved_pool_finish(&device->custom_border_colors);
3100    anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
3101    anv_state_pool_free(&device->dynamic_state_pool, device->slice_hash);
3102 #endif
3103 
3104    anv_scratch_pool_finish(device, &device->scratch_pool);
3105 
3106    anv_device_release_bo(device, device->workaround_bo);
3107    anv_device_release_bo(device, device->trivial_batch_bo);
3108    if (device->info.gen >= 10)
3109       anv_device_release_bo(device, device->hiz_clear_bo);
3110 
3111    if (device->info.has_aux_map) {
3112       gen_aux_map_finish(device->aux_map_ctx);
3113       device->aux_map_ctx = NULL;
3114    }
3115 
3116    if (device->physical->use_softpin)
3117       anv_state_pool_finish(&device->binding_table_pool);
3118    anv_state_pool_finish(&device->surface_state_pool);
3119    anv_state_pool_finish(&device->instruction_state_pool);
3120    anv_state_pool_finish(&device->dynamic_state_pool);
3121 
3122    anv_bo_pool_finish(&device->batch_bo_pool);
3123 
3124    anv_bo_cache_finish(&device->bo_cache);
3125 
3126    if (device->physical->use_softpin) {
3127       util_vma_heap_finish(&device->vma_hi);
3128       util_vma_heap_finish(&device->vma_cva);
3129       util_vma_heap_finish(&device->vma_lo);
3130    }
3131 
3132    pthread_cond_destroy(&device->queue_submit);
3133    pthread_mutex_destroy(&device->mutex);
3134 
3135    anv_gem_destroy_context(device, device->context_id);
3136 
3137    if (INTEL_DEBUG & DEBUG_BATCH)
3138       gen_batch_decode_ctx_finish(&device->decoder_ctx);
3139 
3140    close(device->fd);
3141 
3142    vk_device_finish(&device->vk);
3143    vk_free(&device->vk.alloc, device);
3144 }
3145 
anv_EnumerateInstanceLayerProperties(uint32_t * pPropertyCount,VkLayerProperties * pProperties)3146 VkResult anv_EnumerateInstanceLayerProperties(
3147     uint32_t*                                   pPropertyCount,
3148     VkLayerProperties*                          pProperties)
3149 {
3150    if (pProperties == NULL) {
3151       *pPropertyCount = 0;
3152       return VK_SUCCESS;
3153    }
3154 
3155    /* None supported at this time */
3156    return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
3157 }
3158 
anv_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,uint32_t * pPropertyCount,VkLayerProperties * pProperties)3159 VkResult anv_EnumerateDeviceLayerProperties(
3160     VkPhysicalDevice                            physicalDevice,
3161     uint32_t*                                   pPropertyCount,
3162     VkLayerProperties*                          pProperties)
3163 {
3164    if (pProperties == NULL) {
3165       *pPropertyCount = 0;
3166       return VK_SUCCESS;
3167    }
3168 
3169    /* None supported at this time */
3170    return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
3171 }
3172 
anv_GetDeviceQueue(VkDevice _device,uint32_t queueNodeIndex,uint32_t queueIndex,VkQueue * pQueue)3173 void anv_GetDeviceQueue(
3174     VkDevice                                    _device,
3175     uint32_t                                    queueNodeIndex,
3176     uint32_t                                    queueIndex,
3177     VkQueue*                                    pQueue)
3178 {
3179    const VkDeviceQueueInfo2 info = {
3180       .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
3181       .pNext = NULL,
3182       .flags = 0,
3183       .queueFamilyIndex = queueNodeIndex,
3184       .queueIndex = queueIndex,
3185    };
3186 
3187    anv_GetDeviceQueue2(_device, &info, pQueue);
3188 }
3189 
anv_GetDeviceQueue2(VkDevice _device,const VkDeviceQueueInfo2 * pQueueInfo,VkQueue * pQueue)3190 void anv_GetDeviceQueue2(
3191     VkDevice                                    _device,
3192     const VkDeviceQueueInfo2*                   pQueueInfo,
3193     VkQueue*                                    pQueue)
3194 {
3195    ANV_FROM_HANDLE(anv_device, device, _device);
3196 
3197    assert(pQueueInfo->queueIndex == 0);
3198 
3199    if (pQueueInfo->flags == device->queue.flags)
3200       *pQueue = anv_queue_to_handle(&device->queue);
3201    else
3202       *pQueue = NULL;
3203 }
3204 
3205 VkResult
_anv_device_set_lost(struct anv_device * device,const char * file,int line,const char * msg,...)3206 _anv_device_set_lost(struct anv_device *device,
3207                      const char *file, int line,
3208                      const char *msg, ...)
3209 {
3210    VkResult err;
3211    va_list ap;
3212 
3213    p_atomic_inc(&device->_lost);
3214 
3215    va_start(ap, msg);
3216    err = __vk_errorv(device->physical->instance, device,
3217                      VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
3218                      VK_ERROR_DEVICE_LOST, file, line, msg, ap);
3219    va_end(ap);
3220 
3221    if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
3222       abort();
3223 
3224    return err;
3225 }
3226 
3227 VkResult
_anv_queue_set_lost(struct anv_queue * queue,const char * file,int line,const char * msg,...)3228 _anv_queue_set_lost(struct anv_queue *queue,
3229                     const char *file, int line,
3230                     const char *msg, ...)
3231 {
3232    VkResult err;
3233    va_list ap;
3234 
3235    p_atomic_inc(&queue->device->_lost);
3236 
3237    va_start(ap, msg);
3238    err = __vk_errorv(queue->device->physical->instance, queue->device,
3239                      VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
3240                      VK_ERROR_DEVICE_LOST, file, line, msg, ap);
3241    va_end(ap);
3242 
3243    if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
3244       abort();
3245 
3246    return err;
3247 }
3248 
3249 VkResult
anv_device_query_status(struct anv_device * device)3250 anv_device_query_status(struct anv_device *device)
3251 {
3252    /* This isn't likely as most of the callers of this function already check
3253     * for it.  However, it doesn't hurt to check and it potentially lets us
3254     * avoid an ioctl.
3255     */
3256    if (anv_device_is_lost(device))
3257       return VK_ERROR_DEVICE_LOST;
3258 
3259    uint32_t active, pending;
3260    int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
3261    if (ret == -1) {
3262       /* We don't know the real error. */
3263       return anv_device_set_lost(device, "get_reset_stats failed: %m");
3264    }
3265 
3266    if (active) {
3267       return anv_device_set_lost(device, "GPU hung on one of our command buffers");
3268    } else if (pending) {
3269       return anv_device_set_lost(device, "GPU hung with commands in-flight");
3270    }
3271 
3272    return VK_SUCCESS;
3273 }
3274 
3275 VkResult
anv_device_bo_busy(struct anv_device * device,struct anv_bo * bo)3276 anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
3277 {
3278    /* Note:  This only returns whether or not the BO is in use by an i915 GPU.
3279     * Other usages of the BO (such as on different hardware) will not be
3280     * flagged as "busy" by this ioctl.  Use with care.
3281     */
3282    int ret = anv_gem_busy(device, bo->gem_handle);
3283    if (ret == 1) {
3284       return VK_NOT_READY;
3285    } else if (ret == -1) {
3286       /* We don't know the real error. */
3287       return anv_device_set_lost(device, "gem wait failed: %m");
3288    }
3289 
3290    /* Query for device status after the busy call.  If the BO we're checking
3291     * got caught in a GPU hang we don't want to return VK_SUCCESS to the
3292     * client because it clearly doesn't have valid data.  Yes, this most
3293     * likely means an ioctl, but we just did an ioctl to query the busy status
3294     * so it's no great loss.
3295     */
3296    return anv_device_query_status(device);
3297 }
3298 
3299 VkResult
anv_device_wait(struct anv_device * device,struct anv_bo * bo,int64_t timeout)3300 anv_device_wait(struct anv_device *device, struct anv_bo *bo,
3301                 int64_t timeout)
3302 {
3303    int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
3304    if (ret == -1 && errno == ETIME) {
3305       return VK_TIMEOUT;
3306    } else if (ret == -1) {
3307       /* We don't know the real error. */
3308       return anv_device_set_lost(device, "gem wait failed: %m");
3309    }
3310 
3311    /* Query for device status after the wait.  If the BO we're waiting on got
3312     * caught in a GPU hang we don't want to return VK_SUCCESS to the client
3313     * because it clearly doesn't have valid data.  Yes, this most likely means
3314     * an ioctl, but we just did an ioctl to wait so it's no great loss.
3315     */
3316    return anv_device_query_status(device);
3317 }
3318 
anv_DeviceWaitIdle(VkDevice _device)3319 VkResult anv_DeviceWaitIdle(
3320     VkDevice                                    _device)
3321 {
3322    ANV_FROM_HANDLE(anv_device, device, _device);
3323 
3324    if (anv_device_is_lost(device))
3325       return VK_ERROR_DEVICE_LOST;
3326 
3327    return anv_queue_submit_simple_batch(&device->queue, NULL);
3328 }
3329 
3330 uint64_t
anv_vma_alloc(struct anv_device * device,uint64_t size,uint64_t align,enum anv_bo_alloc_flags alloc_flags,uint64_t client_address)3331 anv_vma_alloc(struct anv_device *device,
3332               uint64_t size, uint64_t align,
3333               enum anv_bo_alloc_flags alloc_flags,
3334               uint64_t client_address)
3335 {
3336    pthread_mutex_lock(&device->vma_mutex);
3337 
3338    uint64_t addr = 0;
3339 
3340    if (alloc_flags & ANV_BO_ALLOC_CLIENT_VISIBLE_ADDRESS) {
3341       if (client_address) {
3342          if (util_vma_heap_alloc_addr(&device->vma_cva,
3343                                       client_address, size)) {
3344             addr = client_address;
3345          }
3346       } else {
3347          addr = util_vma_heap_alloc(&device->vma_cva, size, align);
3348       }
3349       /* We don't want to fall back to other heaps */
3350       goto done;
3351    }
3352 
3353    assert(client_address == 0);
3354 
3355    if (!(alloc_flags & ANV_BO_ALLOC_32BIT_ADDRESS))
3356       addr = util_vma_heap_alloc(&device->vma_hi, size, align);
3357 
3358    if (addr == 0)
3359       addr = util_vma_heap_alloc(&device->vma_lo, size, align);
3360 
3361 done:
3362    pthread_mutex_unlock(&device->vma_mutex);
3363 
3364    assert(addr == gen_48b_address(addr));
3365    return gen_canonical_address(addr);
3366 }
3367 
3368 void
anv_vma_free(struct anv_device * device,uint64_t address,uint64_t size)3369 anv_vma_free(struct anv_device *device,
3370              uint64_t address, uint64_t size)
3371 {
3372    const uint64_t addr_48b = gen_48b_address(address);
3373 
3374    pthread_mutex_lock(&device->vma_mutex);
3375 
3376    if (addr_48b >= LOW_HEAP_MIN_ADDRESS &&
3377        addr_48b <= LOW_HEAP_MAX_ADDRESS) {
3378       util_vma_heap_free(&device->vma_lo, addr_48b, size);
3379    } else if (addr_48b >= CLIENT_VISIBLE_HEAP_MIN_ADDRESS &&
3380               addr_48b <= CLIENT_VISIBLE_HEAP_MAX_ADDRESS) {
3381       util_vma_heap_free(&device->vma_cva, addr_48b, size);
3382    } else {
3383       assert(addr_48b >= HIGH_HEAP_MIN_ADDRESS);
3384       util_vma_heap_free(&device->vma_hi, addr_48b, size);
3385    }
3386 
3387    pthread_mutex_unlock(&device->vma_mutex);
3388 }
3389 
anv_AllocateMemory(VkDevice _device,const VkMemoryAllocateInfo * pAllocateInfo,const VkAllocationCallbacks * pAllocator,VkDeviceMemory * pMem)3390 VkResult anv_AllocateMemory(
3391     VkDevice                                    _device,
3392     const VkMemoryAllocateInfo*                 pAllocateInfo,
3393     const VkAllocationCallbacks*                pAllocator,
3394     VkDeviceMemory*                             pMem)
3395 {
3396    ANV_FROM_HANDLE(anv_device, device, _device);
3397    struct anv_physical_device *pdevice = device->physical;
3398    struct anv_device_memory *mem;
3399    VkResult result = VK_SUCCESS;
3400 
3401    assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
3402 
3403    /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
3404    assert(pAllocateInfo->allocationSize > 0);
3405 
3406    VkDeviceSize aligned_alloc_size =
3407       align_u64(pAllocateInfo->allocationSize, 4096);
3408 
3409    if (aligned_alloc_size > MAX_MEMORY_ALLOCATION_SIZE)
3410       return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
3411 
3412    assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
3413    struct anv_memory_type *mem_type =
3414       &pdevice->memory.types[pAllocateInfo->memoryTypeIndex];
3415    assert(mem_type->heapIndex < pdevice->memory.heap_count);
3416    struct anv_memory_heap *mem_heap =
3417       &pdevice->memory.heaps[mem_type->heapIndex];
3418 
3419    uint64_t mem_heap_used = p_atomic_read(&mem_heap->used);
3420    if (mem_heap_used + aligned_alloc_size > mem_heap->size)
3421       return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
3422 
3423    mem = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*mem), 8,
3424                     VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3425    if (mem == NULL)
3426       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
3427 
3428    assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
3429    vk_object_base_init(&device->vk, &mem->base, VK_OBJECT_TYPE_DEVICE_MEMORY);
3430    mem->type = mem_type;
3431    mem->map = NULL;
3432    mem->map_size = 0;
3433    mem->ahw = NULL;
3434    mem->host_ptr = NULL;
3435 
3436    enum anv_bo_alloc_flags alloc_flags = 0;
3437 
3438    const VkExportMemoryAllocateInfo *export_info = NULL;
3439    const VkImportAndroidHardwareBufferInfoANDROID *ahw_import_info = NULL;
3440    const VkImportMemoryFdInfoKHR *fd_info = NULL;
3441    const VkImportMemoryHostPointerInfoEXT *host_ptr_info = NULL;
3442    const VkMemoryDedicatedAllocateInfo *dedicated_info = NULL;
3443    VkMemoryAllocateFlags vk_flags = 0;
3444    uint64_t client_address = 0;
3445 
3446    vk_foreach_struct_const(ext, pAllocateInfo->pNext) {
3447       switch (ext->sType) {
3448       case VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO:
3449          export_info = (void *)ext;
3450          break;
3451 
3452       case VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID:
3453          ahw_import_info = (void *)ext;
3454          break;
3455 
3456       case VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR:
3457          fd_info = (void *)ext;
3458          break;
3459 
3460       case VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT:
3461          host_ptr_info = (void *)ext;
3462          break;
3463 
3464       case VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO: {
3465          const VkMemoryAllocateFlagsInfo *flags_info = (void *)ext;
3466          vk_flags = flags_info->flags;
3467          break;
3468       }
3469 
3470       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO:
3471          dedicated_info = (void *)ext;
3472          break;
3473 
3474       case VK_STRUCTURE_TYPE_MEMORY_OPAQUE_CAPTURE_ADDRESS_ALLOCATE_INFO_KHR: {
3475          const VkMemoryOpaqueCaptureAddressAllocateInfoKHR *addr_info =
3476             (const VkMemoryOpaqueCaptureAddressAllocateInfoKHR *)ext;
3477          client_address = addr_info->opaqueCaptureAddress;
3478          break;
3479       }
3480 
3481       default:
3482          anv_debug_ignored_stype(ext->sType);
3483          break;
3484       }
3485    }
3486 
3487    /* By default, we want all VkDeviceMemory objects to support CCS */
3488    if (device->physical->has_implicit_ccs)
3489       alloc_flags |= ANV_BO_ALLOC_IMPLICIT_CCS;
3490 
3491    if (vk_flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR)
3492       alloc_flags |= ANV_BO_ALLOC_CLIENT_VISIBLE_ADDRESS;
3493 
3494    if ((export_info && export_info->handleTypes) ||
3495        (fd_info && fd_info->handleType) ||
3496        (host_ptr_info && host_ptr_info->handleType)) {
3497       /* Anything imported or exported is EXTERNAL */
3498       alloc_flags |= ANV_BO_ALLOC_EXTERNAL;
3499 
3500       /* We can't have implicit CCS on external memory with an AUX-table.
3501        * Doing so would require us to sync the aux tables across processes
3502        * which is impractical.
3503        */
3504       if (device->info.has_aux_map)
3505          alloc_flags &= ~ANV_BO_ALLOC_IMPLICIT_CCS;
3506    }
3507 
3508    /* Check if we need to support Android HW buffer export. If so,
3509     * create AHardwareBuffer and import memory from it.
3510     */
3511    bool android_export = false;
3512    if (export_info && export_info->handleTypes &
3513        VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)
3514       android_export = true;
3515 
3516    if (ahw_import_info) {
3517       result = anv_import_ahw_memory(_device, mem, ahw_import_info);
3518       if (result != VK_SUCCESS)
3519          goto fail;
3520 
3521       goto success;
3522    } else if (android_export) {
3523       result = anv_create_ahw_memory(_device, mem, pAllocateInfo);
3524       if (result != VK_SUCCESS)
3525          goto fail;
3526 
3527       const VkImportAndroidHardwareBufferInfoANDROID import_info = {
3528          .buffer = mem->ahw,
3529       };
3530       result = anv_import_ahw_memory(_device, mem, &import_info);
3531       if (result != VK_SUCCESS)
3532          goto fail;
3533 
3534       goto success;
3535    }
3536 
3537    /* The Vulkan spec permits handleType to be 0, in which case the struct is
3538     * ignored.
3539     */
3540    if (fd_info && fd_info->handleType) {
3541       /* At the moment, we support only the below handle types. */
3542       assert(fd_info->handleType ==
3543                VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
3544              fd_info->handleType ==
3545                VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3546 
3547       result = anv_device_import_bo(device, fd_info->fd, alloc_flags,
3548                                     client_address, &mem->bo);
3549       if (result != VK_SUCCESS)
3550          goto fail;
3551 
3552       /* For security purposes, we reject importing the bo if it's smaller
3553        * than the requested allocation size.  This prevents a malicious client
3554        * from passing a buffer to a trusted client, lying about the size, and
3555        * telling the trusted client to try and texture from an image that goes
3556        * out-of-bounds.  This sort of thing could lead to GPU hangs or worse
3557        * in the trusted client.  The trusted client can protect itself against
3558        * this sort of attack but only if it can trust the buffer size.
3559        */
3560       if (mem->bo->size < aligned_alloc_size) {
3561          result = vk_errorf(device, device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
3562                             "aligned allocationSize too large for "
3563                             "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
3564                             "%"PRIu64"B > %"PRIu64"B",
3565                             aligned_alloc_size, mem->bo->size);
3566          anv_device_release_bo(device, mem->bo);
3567          goto fail;
3568       }
3569 
3570       /* From the Vulkan spec:
3571        *
3572        *    "Importing memory from a file descriptor transfers ownership of
3573        *    the file descriptor from the application to the Vulkan
3574        *    implementation. The application must not perform any operations on
3575        *    the file descriptor after a successful import."
3576        *
3577        * If the import fails, we leave the file descriptor open.
3578        */
3579       close(fd_info->fd);
3580       goto success;
3581    }
3582 
3583    if (host_ptr_info && host_ptr_info->handleType) {
3584       if (host_ptr_info->handleType ==
3585           VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT) {
3586          result = vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
3587          goto fail;
3588       }
3589 
3590       assert(host_ptr_info->handleType ==
3591              VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
3592 
3593       result = anv_device_import_bo_from_host_ptr(device,
3594                                                   host_ptr_info->pHostPointer,
3595                                                   pAllocateInfo->allocationSize,
3596                                                   alloc_flags,
3597                                                   client_address,
3598                                                   &mem->bo);
3599       if (result != VK_SUCCESS)
3600          goto fail;
3601 
3602       mem->host_ptr = host_ptr_info->pHostPointer;
3603       goto success;
3604    }
3605 
3606    /* Regular allocate (not importing memory). */
3607 
3608    result = anv_device_alloc_bo(device, pAllocateInfo->allocationSize,
3609                                 alloc_flags, client_address, &mem->bo);
3610    if (result != VK_SUCCESS)
3611       goto fail;
3612 
3613    if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
3614       ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
3615 
3616       /* Some legacy (non-modifiers) consumers need the tiling to be set on
3617        * the BO.  In this case, we have a dedicated allocation.
3618        */
3619       if (image->needs_set_tiling) {
3620          const uint32_t i915_tiling =
3621             isl_tiling_to_i915_tiling(image->planes[0].surface.isl.tiling);
3622          int ret = anv_gem_set_tiling(device, mem->bo->gem_handle,
3623                                       image->planes[0].surface.isl.row_pitch_B,
3624                                       i915_tiling);
3625          if (ret) {
3626             anv_device_release_bo(device, mem->bo);
3627             result = vk_errorf(device, device, VK_ERROR_OUT_OF_DEVICE_MEMORY,
3628                                "failed to set BO tiling: %m");
3629             goto fail;
3630          }
3631       }
3632    }
3633 
3634  success:
3635    mem_heap_used = p_atomic_add_return(&mem_heap->used, mem->bo->size);
3636    if (mem_heap_used > mem_heap->size) {
3637       p_atomic_add(&mem_heap->used, -mem->bo->size);
3638       anv_device_release_bo(device, mem->bo);
3639       result = vk_errorf(device, device, VK_ERROR_OUT_OF_DEVICE_MEMORY,
3640                          "Out of heap memory");
3641       goto fail;
3642    }
3643 
3644    pthread_mutex_lock(&device->mutex);
3645    list_addtail(&mem->link, &device->memory_objects);
3646    pthread_mutex_unlock(&device->mutex);
3647 
3648    *pMem = anv_device_memory_to_handle(mem);
3649 
3650    return VK_SUCCESS;
3651 
3652  fail:
3653    vk_free2(&device->vk.alloc, pAllocator, mem);
3654 
3655    return result;
3656 }
3657 
anv_GetMemoryFdKHR(VkDevice device_h,const VkMemoryGetFdInfoKHR * pGetFdInfo,int * pFd)3658 VkResult anv_GetMemoryFdKHR(
3659     VkDevice                                    device_h,
3660     const VkMemoryGetFdInfoKHR*                 pGetFdInfo,
3661     int*                                        pFd)
3662 {
3663    ANV_FROM_HANDLE(anv_device, dev, device_h);
3664    ANV_FROM_HANDLE(anv_device_memory, mem, pGetFdInfo->memory);
3665 
3666    assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
3667 
3668    assert(pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
3669           pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
3670 
3671    return anv_device_export_bo(dev, mem->bo, pFd);
3672 }
3673 
anv_GetMemoryFdPropertiesKHR(VkDevice _device,VkExternalMemoryHandleTypeFlagBits handleType,int fd,VkMemoryFdPropertiesKHR * pMemoryFdProperties)3674 VkResult anv_GetMemoryFdPropertiesKHR(
3675     VkDevice                                    _device,
3676     VkExternalMemoryHandleTypeFlagBits          handleType,
3677     int                                         fd,
3678     VkMemoryFdPropertiesKHR*                    pMemoryFdProperties)
3679 {
3680    ANV_FROM_HANDLE(anv_device, device, _device);
3681 
3682    switch (handleType) {
3683    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
3684       /* dma-buf can be imported as any memory type */
3685       pMemoryFdProperties->memoryTypeBits =
3686          (1 << device->physical->memory.type_count) - 1;
3687       return VK_SUCCESS;
3688 
3689    default:
3690       /* The valid usage section for this function says:
3691        *
3692        *    "handleType must not be one of the handle types defined as
3693        *    opaque."
3694        *
3695        * So opaque handle types fall into the default "unsupported" case.
3696        */
3697       return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
3698    }
3699 }
3700 
anv_GetMemoryHostPointerPropertiesEXT(VkDevice _device,VkExternalMemoryHandleTypeFlagBits handleType,const void * pHostPointer,VkMemoryHostPointerPropertiesEXT * pMemoryHostPointerProperties)3701 VkResult anv_GetMemoryHostPointerPropertiesEXT(
3702    VkDevice                                    _device,
3703    VkExternalMemoryHandleTypeFlagBits          handleType,
3704    const void*                                 pHostPointer,
3705    VkMemoryHostPointerPropertiesEXT*           pMemoryHostPointerProperties)
3706 {
3707    ANV_FROM_HANDLE(anv_device, device, _device);
3708 
3709    assert(pMemoryHostPointerProperties->sType ==
3710           VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT);
3711 
3712    switch (handleType) {
3713    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
3714       /* Host memory can be imported as any memory type. */
3715       pMemoryHostPointerProperties->memoryTypeBits =
3716          (1ull << device->physical->memory.type_count) - 1;
3717 
3718       return VK_SUCCESS;
3719 
3720    default:
3721       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
3722    }
3723 }
3724 
anv_FreeMemory(VkDevice _device,VkDeviceMemory _mem,const VkAllocationCallbacks * pAllocator)3725 void anv_FreeMemory(
3726     VkDevice                                    _device,
3727     VkDeviceMemory                              _mem,
3728     const VkAllocationCallbacks*                pAllocator)
3729 {
3730    ANV_FROM_HANDLE(anv_device, device, _device);
3731    ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
3732 
3733    if (mem == NULL)
3734       return;
3735 
3736    pthread_mutex_lock(&device->mutex);
3737    list_del(&mem->link);
3738    pthread_mutex_unlock(&device->mutex);
3739 
3740    if (mem->map)
3741       anv_UnmapMemory(_device, _mem);
3742 
3743    p_atomic_add(&device->physical->memory.heaps[mem->type->heapIndex].used,
3744                 -mem->bo->size);
3745 
3746    anv_device_release_bo(device, mem->bo);
3747 
3748 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
3749    if (mem->ahw)
3750       AHardwareBuffer_release(mem->ahw);
3751 #endif
3752 
3753    vk_object_base_finish(&mem->base);
3754    vk_free2(&device->vk.alloc, pAllocator, mem);
3755 }
3756 
anv_MapMemory(VkDevice _device,VkDeviceMemory _memory,VkDeviceSize offset,VkDeviceSize size,VkMemoryMapFlags flags,void ** ppData)3757 VkResult anv_MapMemory(
3758     VkDevice                                    _device,
3759     VkDeviceMemory                              _memory,
3760     VkDeviceSize                                offset,
3761     VkDeviceSize                                size,
3762     VkMemoryMapFlags                            flags,
3763     void**                                      ppData)
3764 {
3765    ANV_FROM_HANDLE(anv_device, device, _device);
3766    ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
3767 
3768    if (mem == NULL) {
3769       *ppData = NULL;
3770       return VK_SUCCESS;
3771    }
3772 
3773    if (mem->host_ptr) {
3774       *ppData = mem->host_ptr + offset;
3775       return VK_SUCCESS;
3776    }
3777 
3778    if (size == VK_WHOLE_SIZE)
3779       size = mem->bo->size - offset;
3780 
3781    /* From the Vulkan spec version 1.0.32 docs for MapMemory:
3782     *
3783     *  * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
3784     *    assert(size != 0);
3785     *  * If size is not equal to VK_WHOLE_SIZE, size must be less than or
3786     *    equal to the size of the memory minus offset
3787     */
3788    assert(size > 0);
3789    assert(offset + size <= mem->bo->size);
3790 
3791    /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
3792     * takes a VkDeviceMemory pointer, it seems like only one map of the memory
3793     * at a time is valid. We could just mmap up front and return an offset
3794     * pointer here, but that may exhaust virtual memory on 32 bit
3795     * userspace. */
3796 
3797    uint32_t gem_flags = 0;
3798 
3799    if (!device->info.has_llc &&
3800        (mem->type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
3801       gem_flags |= I915_MMAP_WC;
3802 
3803    /* GEM will fail to map if the offset isn't 4k-aligned.  Round down. */
3804    uint64_t map_offset;
3805    if (!device->physical->has_mmap_offset)
3806       map_offset = offset & ~4095ull;
3807    else
3808       map_offset = 0;
3809    assert(offset >= map_offset);
3810    uint64_t map_size = (offset + size) - map_offset;
3811 
3812    /* Let's map whole pages */
3813    map_size = align_u64(map_size, 4096);
3814 
3815    void *map = anv_gem_mmap(device, mem->bo->gem_handle,
3816                             map_offset, map_size, gem_flags);
3817    if (map == MAP_FAILED)
3818       return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
3819 
3820    mem->map = map;
3821    mem->map_size = map_size;
3822 
3823    *ppData = mem->map + (offset - map_offset);
3824 
3825    return VK_SUCCESS;
3826 }
3827 
anv_UnmapMemory(VkDevice _device,VkDeviceMemory _memory)3828 void anv_UnmapMemory(
3829     VkDevice                                    _device,
3830     VkDeviceMemory                              _memory)
3831 {
3832    ANV_FROM_HANDLE(anv_device, device, _device);
3833    ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
3834 
3835    if (mem == NULL || mem->host_ptr)
3836       return;
3837 
3838    anv_gem_munmap(device, mem->map, mem->map_size);
3839 
3840    mem->map = NULL;
3841    mem->map_size = 0;
3842 }
3843 
3844 static void
clflush_mapped_ranges(struct anv_device * device,uint32_t count,const VkMappedMemoryRange * ranges)3845 clflush_mapped_ranges(struct anv_device         *device,
3846                       uint32_t                   count,
3847                       const VkMappedMemoryRange *ranges)
3848 {
3849    for (uint32_t i = 0; i < count; i++) {
3850       ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
3851       if (ranges[i].offset >= mem->map_size)
3852          continue;
3853 
3854       gen_clflush_range(mem->map + ranges[i].offset,
3855                         MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
3856    }
3857 }
3858 
anv_FlushMappedMemoryRanges(VkDevice _device,uint32_t memoryRangeCount,const VkMappedMemoryRange * pMemoryRanges)3859 VkResult anv_FlushMappedMemoryRanges(
3860     VkDevice                                    _device,
3861     uint32_t                                    memoryRangeCount,
3862     const VkMappedMemoryRange*                  pMemoryRanges)
3863 {
3864    ANV_FROM_HANDLE(anv_device, device, _device);
3865 
3866    if (device->info.has_llc)
3867       return VK_SUCCESS;
3868 
3869    /* Make sure the writes we're flushing have landed. */
3870    __builtin_ia32_mfence();
3871 
3872    clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
3873 
3874    return VK_SUCCESS;
3875 }
3876 
anv_InvalidateMappedMemoryRanges(VkDevice _device,uint32_t memoryRangeCount,const VkMappedMemoryRange * pMemoryRanges)3877 VkResult anv_InvalidateMappedMemoryRanges(
3878     VkDevice                                    _device,
3879     uint32_t                                    memoryRangeCount,
3880     const VkMappedMemoryRange*                  pMemoryRanges)
3881 {
3882    ANV_FROM_HANDLE(anv_device, device, _device);
3883 
3884    if (device->info.has_llc)
3885       return VK_SUCCESS;
3886 
3887    clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
3888 
3889    /* Make sure no reads get moved up above the invalidate. */
3890    __builtin_ia32_mfence();
3891 
3892    return VK_SUCCESS;
3893 }
3894 
anv_GetBufferMemoryRequirements(VkDevice _device,VkBuffer _buffer,VkMemoryRequirements * pMemoryRequirements)3895 void anv_GetBufferMemoryRequirements(
3896     VkDevice                                    _device,
3897     VkBuffer                                    _buffer,
3898     VkMemoryRequirements*                       pMemoryRequirements)
3899 {
3900    ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
3901    ANV_FROM_HANDLE(anv_device, device, _device);
3902 
3903    /* The Vulkan spec (git aaed022) says:
3904     *
3905     *    memoryTypeBits is a bitfield and contains one bit set for every
3906     *    supported memory type for the resource. The bit `1<<i` is set if and
3907     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3908     *    structure for the physical device is supported.
3909     */
3910    uint32_t memory_types = (1ull << device->physical->memory.type_count) - 1;
3911 
3912    /* Base alignment requirement of a cache line */
3913    uint32_t alignment = 16;
3914 
3915    if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
3916       alignment = MAX2(alignment, ANV_UBO_ALIGNMENT);
3917 
3918    pMemoryRequirements->size = buffer->size;
3919    pMemoryRequirements->alignment = alignment;
3920 
3921    /* Storage and Uniform buffers should have their size aligned to
3922     * 32-bits to avoid boundary checks when last DWord is not complete.
3923     * This would ensure that not internal padding would be needed for
3924     * 16-bit types.
3925     */
3926    if (device->robust_buffer_access &&
3927        (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
3928         buffer->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
3929       pMemoryRequirements->size = align_u64(buffer->size, 4);
3930 
3931    pMemoryRequirements->memoryTypeBits = memory_types;
3932 }
3933 
anv_GetBufferMemoryRequirements2(VkDevice _device,const VkBufferMemoryRequirementsInfo2 * pInfo,VkMemoryRequirements2 * pMemoryRequirements)3934 void anv_GetBufferMemoryRequirements2(
3935     VkDevice                                    _device,
3936     const VkBufferMemoryRequirementsInfo2*      pInfo,
3937     VkMemoryRequirements2*                      pMemoryRequirements)
3938 {
3939    anv_GetBufferMemoryRequirements(_device, pInfo->buffer,
3940                                    &pMemoryRequirements->memoryRequirements);
3941 
3942    vk_foreach_struct(ext, pMemoryRequirements->pNext) {
3943       switch (ext->sType) {
3944       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
3945          VkMemoryDedicatedRequirements *requirements = (void *)ext;
3946          requirements->prefersDedicatedAllocation = false;
3947          requirements->requiresDedicatedAllocation = false;
3948          break;
3949       }
3950 
3951       default:
3952          anv_debug_ignored_stype(ext->sType);
3953          break;
3954       }
3955    }
3956 }
3957 
anv_GetImageMemoryRequirements(VkDevice _device,VkImage _image,VkMemoryRequirements * pMemoryRequirements)3958 void anv_GetImageMemoryRequirements(
3959     VkDevice                                    _device,
3960     VkImage                                     _image,
3961     VkMemoryRequirements*                       pMemoryRequirements)
3962 {
3963    ANV_FROM_HANDLE(anv_image, image, _image);
3964    ANV_FROM_HANDLE(anv_device, device, _device);
3965 
3966    /* The Vulkan spec (git aaed022) says:
3967     *
3968     *    memoryTypeBits is a bitfield and contains one bit set for every
3969     *    supported memory type for the resource. The bit `1<<i` is set if and
3970     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
3971     *    structure for the physical device is supported.
3972     *
3973     * All types are currently supported for images.
3974     */
3975    uint32_t memory_types = (1ull << device->physical->memory.type_count) - 1;
3976 
3977    pMemoryRequirements->size = image->size;
3978    pMemoryRequirements->alignment = image->alignment;
3979    pMemoryRequirements->memoryTypeBits = memory_types;
3980 }
3981 
anv_GetImageMemoryRequirements2(VkDevice _device,const VkImageMemoryRequirementsInfo2 * pInfo,VkMemoryRequirements2 * pMemoryRequirements)3982 void anv_GetImageMemoryRequirements2(
3983     VkDevice                                    _device,
3984     const VkImageMemoryRequirementsInfo2*       pInfo,
3985     VkMemoryRequirements2*                      pMemoryRequirements)
3986 {
3987    ANV_FROM_HANDLE(anv_device, device, _device);
3988    ANV_FROM_HANDLE(anv_image, image, pInfo->image);
3989 
3990    anv_GetImageMemoryRequirements(_device, pInfo->image,
3991                                   &pMemoryRequirements->memoryRequirements);
3992 
3993    vk_foreach_struct_const(ext, pInfo->pNext) {
3994       switch (ext->sType) {
3995       case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO: {
3996          const VkImagePlaneMemoryRequirementsInfo *plane_reqs =
3997             (const VkImagePlaneMemoryRequirementsInfo *) ext;
3998          uint32_t plane = anv_image_aspect_to_plane(image->aspects,
3999                                                     plane_reqs->planeAspect);
4000 
4001          assert(image->planes[plane].offset == 0);
4002 
4003          /* The Vulkan spec (git aaed022) says:
4004           *
4005           *    memoryTypeBits is a bitfield and contains one bit set for every
4006           *    supported memory type for the resource. The bit `1<<i` is set
4007           *    if and only if the memory type `i` in the
4008           *    VkPhysicalDeviceMemoryProperties structure for the physical
4009           *    device is supported.
4010           *
4011           * All types are currently supported for images.
4012           */
4013          pMemoryRequirements->memoryRequirements.memoryTypeBits =
4014                (1ull << device->physical->memory.type_count) - 1;
4015 
4016          pMemoryRequirements->memoryRequirements.size = image->planes[plane].size;
4017          pMemoryRequirements->memoryRequirements.alignment =
4018             image->planes[plane].alignment;
4019          break;
4020       }
4021 
4022       default:
4023          anv_debug_ignored_stype(ext->sType);
4024          break;
4025       }
4026    }
4027 
4028    vk_foreach_struct(ext, pMemoryRequirements->pNext) {
4029       switch (ext->sType) {
4030       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
4031          VkMemoryDedicatedRequirements *requirements = (void *)ext;
4032          if (image->needs_set_tiling || image->external_format) {
4033             /* If we need to set the tiling for external consumers, we need a
4034              * dedicated allocation.
4035              *
4036              * See also anv_AllocateMemory.
4037              */
4038             requirements->prefersDedicatedAllocation = true;
4039             requirements->requiresDedicatedAllocation = true;
4040          } else {
4041             requirements->prefersDedicatedAllocation = false;
4042             requirements->requiresDedicatedAllocation = false;
4043          }
4044          break;
4045       }
4046 
4047       default:
4048          anv_debug_ignored_stype(ext->sType);
4049          break;
4050       }
4051    }
4052 }
4053 
anv_GetImageSparseMemoryRequirements(VkDevice device,VkImage image,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements * pSparseMemoryRequirements)4054 void anv_GetImageSparseMemoryRequirements(
4055     VkDevice                                    device,
4056     VkImage                                     image,
4057     uint32_t*                                   pSparseMemoryRequirementCount,
4058     VkSparseImageMemoryRequirements*            pSparseMemoryRequirements)
4059 {
4060    *pSparseMemoryRequirementCount = 0;
4061 }
4062 
anv_GetImageSparseMemoryRequirements2(VkDevice device,const VkImageSparseMemoryRequirementsInfo2 * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)4063 void anv_GetImageSparseMemoryRequirements2(
4064     VkDevice                                    device,
4065     const VkImageSparseMemoryRequirementsInfo2* pInfo,
4066     uint32_t*                                   pSparseMemoryRequirementCount,
4067     VkSparseImageMemoryRequirements2*           pSparseMemoryRequirements)
4068 {
4069    *pSparseMemoryRequirementCount = 0;
4070 }
4071 
anv_GetDeviceMemoryCommitment(VkDevice device,VkDeviceMemory memory,VkDeviceSize * pCommittedMemoryInBytes)4072 void anv_GetDeviceMemoryCommitment(
4073     VkDevice                                    device,
4074     VkDeviceMemory                              memory,
4075     VkDeviceSize*                               pCommittedMemoryInBytes)
4076 {
4077    *pCommittedMemoryInBytes = 0;
4078 }
4079 
4080 static void
anv_bind_buffer_memory(const VkBindBufferMemoryInfo * pBindInfo)4081 anv_bind_buffer_memory(const VkBindBufferMemoryInfo *pBindInfo)
4082 {
4083    ANV_FROM_HANDLE(anv_device_memory, mem, pBindInfo->memory);
4084    ANV_FROM_HANDLE(anv_buffer, buffer, pBindInfo->buffer);
4085 
4086    assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO);
4087 
4088    if (mem) {
4089       buffer->address = (struct anv_address) {
4090          .bo = mem->bo,
4091          .offset = pBindInfo->memoryOffset,
4092       };
4093    } else {
4094       buffer->address = ANV_NULL_ADDRESS;
4095    }
4096 }
4097 
anv_BindBufferMemory(VkDevice device,VkBuffer buffer,VkDeviceMemory memory,VkDeviceSize memoryOffset)4098 VkResult anv_BindBufferMemory(
4099     VkDevice                                    device,
4100     VkBuffer                                    buffer,
4101     VkDeviceMemory                              memory,
4102     VkDeviceSize                                memoryOffset)
4103 {
4104    anv_bind_buffer_memory(
4105       &(VkBindBufferMemoryInfo) {
4106          .sType         = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
4107          .buffer        = buffer,
4108          .memory        = memory,
4109          .memoryOffset  = memoryOffset,
4110       });
4111 
4112    return VK_SUCCESS;
4113 }
4114 
anv_BindBufferMemory2(VkDevice device,uint32_t bindInfoCount,const VkBindBufferMemoryInfo * pBindInfos)4115 VkResult anv_BindBufferMemory2(
4116     VkDevice                                    device,
4117     uint32_t                                    bindInfoCount,
4118     const VkBindBufferMemoryInfo*               pBindInfos)
4119 {
4120    for (uint32_t i = 0; i < bindInfoCount; i++)
4121       anv_bind_buffer_memory(&pBindInfos[i]);
4122 
4123    return VK_SUCCESS;
4124 }
4125 
anv_QueueBindSparse(VkQueue _queue,uint32_t bindInfoCount,const VkBindSparseInfo * pBindInfo,VkFence fence)4126 VkResult anv_QueueBindSparse(
4127     VkQueue                                     _queue,
4128     uint32_t                                    bindInfoCount,
4129     const VkBindSparseInfo*                     pBindInfo,
4130     VkFence                                     fence)
4131 {
4132    ANV_FROM_HANDLE(anv_queue, queue, _queue);
4133    if (anv_device_is_lost(queue->device))
4134       return VK_ERROR_DEVICE_LOST;
4135 
4136    return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
4137 }
4138 
4139 // Event functions
4140 
anv_CreateEvent(VkDevice _device,const VkEventCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkEvent * pEvent)4141 VkResult anv_CreateEvent(
4142     VkDevice                                    _device,
4143     const VkEventCreateInfo*                    pCreateInfo,
4144     const VkAllocationCallbacks*                pAllocator,
4145     VkEvent*                                    pEvent)
4146 {
4147    ANV_FROM_HANDLE(anv_device, device, _device);
4148    struct anv_event *event;
4149 
4150    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
4151 
4152    event = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*event), 8,
4153                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4154    if (event == NULL)
4155       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
4156 
4157    vk_object_base_init(&device->vk, &event->base, VK_OBJECT_TYPE_EVENT);
4158    event->state = anv_state_pool_alloc(&device->dynamic_state_pool,
4159                                        sizeof(uint64_t), 8);
4160    *(uint64_t *)event->state.map = VK_EVENT_RESET;
4161 
4162    *pEvent = anv_event_to_handle(event);
4163 
4164    return VK_SUCCESS;
4165 }
4166 
anv_DestroyEvent(VkDevice _device,VkEvent _event,const VkAllocationCallbacks * pAllocator)4167 void anv_DestroyEvent(
4168     VkDevice                                    _device,
4169     VkEvent                                     _event,
4170     const VkAllocationCallbacks*                pAllocator)
4171 {
4172    ANV_FROM_HANDLE(anv_device, device, _device);
4173    ANV_FROM_HANDLE(anv_event, event, _event);
4174 
4175    if (!event)
4176       return;
4177 
4178    anv_state_pool_free(&device->dynamic_state_pool, event->state);
4179 
4180    vk_object_base_finish(&event->base);
4181    vk_free2(&device->vk.alloc, pAllocator, event);
4182 }
4183 
anv_GetEventStatus(VkDevice _device,VkEvent _event)4184 VkResult anv_GetEventStatus(
4185     VkDevice                                    _device,
4186     VkEvent                                     _event)
4187 {
4188    ANV_FROM_HANDLE(anv_device, device, _device);
4189    ANV_FROM_HANDLE(anv_event, event, _event);
4190 
4191    if (anv_device_is_lost(device))
4192       return VK_ERROR_DEVICE_LOST;
4193 
4194    return *(uint64_t *)event->state.map;
4195 }
4196 
anv_SetEvent(VkDevice _device,VkEvent _event)4197 VkResult anv_SetEvent(
4198     VkDevice                                    _device,
4199     VkEvent                                     _event)
4200 {
4201    ANV_FROM_HANDLE(anv_event, event, _event);
4202 
4203    *(uint64_t *)event->state.map = VK_EVENT_SET;
4204 
4205    return VK_SUCCESS;
4206 }
4207 
anv_ResetEvent(VkDevice _device,VkEvent _event)4208 VkResult anv_ResetEvent(
4209     VkDevice                                    _device,
4210     VkEvent                                     _event)
4211 {
4212    ANV_FROM_HANDLE(anv_event, event, _event);
4213 
4214    *(uint64_t *)event->state.map = VK_EVENT_RESET;
4215 
4216    return VK_SUCCESS;
4217 }
4218 
4219 // Buffer functions
4220 
anv_CreateBuffer(VkDevice _device,const VkBufferCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkBuffer * pBuffer)4221 VkResult anv_CreateBuffer(
4222     VkDevice                                    _device,
4223     const VkBufferCreateInfo*                   pCreateInfo,
4224     const VkAllocationCallbacks*                pAllocator,
4225     VkBuffer*                                   pBuffer)
4226 {
4227    ANV_FROM_HANDLE(anv_device, device, _device);
4228    struct anv_buffer *buffer;
4229 
4230    /* Don't allow creating buffers bigger than our address space.  The real
4231     * issue here is that we may align up the buffer size and we don't want
4232     * doing so to cause roll-over.  However, no one has any business
4233     * allocating a buffer larger than our GTT size.
4234     */
4235    if (pCreateInfo->size > device->physical->gtt_size)
4236       return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
4237 
4238    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
4239 
4240    buffer = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*buffer), 8,
4241                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4242    if (buffer == NULL)
4243       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
4244 
4245    vk_object_base_init(&device->vk, &buffer->base, VK_OBJECT_TYPE_BUFFER);
4246    buffer->size = pCreateInfo->size;
4247    buffer->usage = pCreateInfo->usage;
4248    buffer->address = ANV_NULL_ADDRESS;
4249 
4250    *pBuffer = anv_buffer_to_handle(buffer);
4251 
4252    return VK_SUCCESS;
4253 }
4254 
anv_DestroyBuffer(VkDevice _device,VkBuffer _buffer,const VkAllocationCallbacks * pAllocator)4255 void anv_DestroyBuffer(
4256     VkDevice                                    _device,
4257     VkBuffer                                    _buffer,
4258     const VkAllocationCallbacks*                pAllocator)
4259 {
4260    ANV_FROM_HANDLE(anv_device, device, _device);
4261    ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
4262 
4263    if (!buffer)
4264       return;
4265 
4266    vk_object_base_finish(&buffer->base);
4267    vk_free2(&device->vk.alloc, pAllocator, buffer);
4268 }
4269 
anv_GetBufferDeviceAddress(VkDevice device,const VkBufferDeviceAddressInfoKHR * pInfo)4270 VkDeviceAddress anv_GetBufferDeviceAddress(
4271     VkDevice                                    device,
4272     const VkBufferDeviceAddressInfoKHR*         pInfo)
4273 {
4274    ANV_FROM_HANDLE(anv_buffer, buffer, pInfo->buffer);
4275 
4276    assert(!anv_address_is_null(buffer->address));
4277    assert(buffer->address.bo->flags & EXEC_OBJECT_PINNED);
4278 
4279    return anv_address_physical(buffer->address);
4280 }
4281 
anv_GetBufferOpaqueCaptureAddress(VkDevice device,const VkBufferDeviceAddressInfoKHR * pInfo)4282 uint64_t anv_GetBufferOpaqueCaptureAddress(
4283     VkDevice                                    device,
4284     const VkBufferDeviceAddressInfoKHR*         pInfo)
4285 {
4286    return 0;
4287 }
4288 
anv_GetDeviceMemoryOpaqueCaptureAddress(VkDevice device,const VkDeviceMemoryOpaqueCaptureAddressInfoKHR * pInfo)4289 uint64_t anv_GetDeviceMemoryOpaqueCaptureAddress(
4290     VkDevice                                    device,
4291     const VkDeviceMemoryOpaqueCaptureAddressInfoKHR* pInfo)
4292 {
4293    ANV_FROM_HANDLE(anv_device_memory, memory, pInfo->memory);
4294 
4295    assert(memory->bo->flags & EXEC_OBJECT_PINNED);
4296    assert(memory->bo->has_client_visible_address);
4297 
4298    return gen_48b_address(memory->bo->offset);
4299 }
4300 
4301 void
anv_fill_buffer_surface_state(struct anv_device * device,struct anv_state state,enum isl_format format,struct anv_address address,uint32_t range,uint32_t stride)4302 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
4303                               enum isl_format format,
4304                               struct anv_address address,
4305                               uint32_t range, uint32_t stride)
4306 {
4307    isl_buffer_fill_state(&device->isl_dev, state.map,
4308                          .address = anv_address_physical(address),
4309                          .mocs = device->isl_dev.mocs.internal,
4310                          .size_B = range,
4311                          .format = format,
4312                          .swizzle = ISL_SWIZZLE_IDENTITY,
4313                          .stride_B = stride);
4314 }
4315 
anv_DestroySampler(VkDevice _device,VkSampler _sampler,const VkAllocationCallbacks * pAllocator)4316 void anv_DestroySampler(
4317     VkDevice                                    _device,
4318     VkSampler                                   _sampler,
4319     const VkAllocationCallbacks*                pAllocator)
4320 {
4321    ANV_FROM_HANDLE(anv_device, device, _device);
4322    ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
4323 
4324    if (!sampler)
4325       return;
4326 
4327    if (sampler->bindless_state.map) {
4328       anv_state_pool_free(&device->dynamic_state_pool,
4329                           sampler->bindless_state);
4330    }
4331 
4332    if (sampler->custom_border_color.map) {
4333       anv_state_reserved_pool_free(&device->custom_border_colors,
4334                                    sampler->custom_border_color);
4335    }
4336 
4337    vk_object_base_finish(&sampler->base);
4338    vk_free2(&device->vk.alloc, pAllocator, sampler);
4339 }
4340 
anv_CreateFramebuffer(VkDevice _device,const VkFramebufferCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkFramebuffer * pFramebuffer)4341 VkResult anv_CreateFramebuffer(
4342     VkDevice                                    _device,
4343     const VkFramebufferCreateInfo*              pCreateInfo,
4344     const VkAllocationCallbacks*                pAllocator,
4345     VkFramebuffer*                              pFramebuffer)
4346 {
4347    ANV_FROM_HANDLE(anv_device, device, _device);
4348    struct anv_framebuffer *framebuffer;
4349 
4350    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
4351 
4352    size_t size = sizeof(*framebuffer);
4353 
4354    /* VK_KHR_imageless_framebuffer extension says:
4355     *
4356     *    If flags includes VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR,
4357     *    parameter pAttachments is ignored.
4358     */
4359    if (!(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR)) {
4360       size += sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
4361       framebuffer = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
4362                               VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4363       if (framebuffer == NULL)
4364          return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
4365 
4366       for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
4367          ANV_FROM_HANDLE(anv_image_view, iview, pCreateInfo->pAttachments[i]);
4368          framebuffer->attachments[i] = iview;
4369       }
4370       framebuffer->attachment_count = pCreateInfo->attachmentCount;
4371    } else {
4372       framebuffer = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
4373                               VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4374       if (framebuffer == NULL)
4375          return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
4376 
4377       framebuffer->attachment_count = 0;
4378    }
4379 
4380    vk_object_base_init(&device->vk, &framebuffer->base,
4381                        VK_OBJECT_TYPE_FRAMEBUFFER);
4382 
4383    framebuffer->width = pCreateInfo->width;
4384    framebuffer->height = pCreateInfo->height;
4385    framebuffer->layers = pCreateInfo->layers;
4386 
4387    *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
4388 
4389    return VK_SUCCESS;
4390 }
4391 
anv_DestroyFramebuffer(VkDevice _device,VkFramebuffer _fb,const VkAllocationCallbacks * pAllocator)4392 void anv_DestroyFramebuffer(
4393     VkDevice                                    _device,
4394     VkFramebuffer                               _fb,
4395     const VkAllocationCallbacks*                pAllocator)
4396 {
4397    ANV_FROM_HANDLE(anv_device, device, _device);
4398    ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
4399 
4400    if (!fb)
4401       return;
4402 
4403    vk_object_base_finish(&fb->base);
4404    vk_free2(&device->vk.alloc, pAllocator, fb);
4405 }
4406 
4407 static const VkTimeDomainEXT anv_time_domains[] = {
4408    VK_TIME_DOMAIN_DEVICE_EXT,
4409    VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
4410 #ifdef CLOCK_MONOTONIC_RAW
4411    VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
4412 #endif
4413 };
4414 
anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(VkPhysicalDevice physicalDevice,uint32_t * pTimeDomainCount,VkTimeDomainEXT * pTimeDomains)4415 VkResult anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
4416    VkPhysicalDevice                             physicalDevice,
4417    uint32_t                                     *pTimeDomainCount,
4418    VkTimeDomainEXT                              *pTimeDomains)
4419 {
4420    int d;
4421    VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
4422 
4423    for (d = 0; d < ARRAY_SIZE(anv_time_domains); d++) {
4424       vk_outarray_append(&out, i) {
4425          *i = anv_time_domains[d];
4426       }
4427    }
4428 
4429    return vk_outarray_status(&out);
4430 }
4431 
4432 static uint64_t
anv_clock_gettime(clockid_t clock_id)4433 anv_clock_gettime(clockid_t clock_id)
4434 {
4435    struct timespec current;
4436    int ret;
4437 
4438    ret = clock_gettime(clock_id, &current);
4439 #ifdef CLOCK_MONOTONIC_RAW
4440    if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
4441       ret = clock_gettime(CLOCK_MONOTONIC, &current);
4442 #endif
4443    if (ret < 0)
4444       return 0;
4445 
4446    return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
4447 }
4448 
anv_GetCalibratedTimestampsEXT(VkDevice _device,uint32_t timestampCount,const VkCalibratedTimestampInfoEXT * pTimestampInfos,uint64_t * pTimestamps,uint64_t * pMaxDeviation)4449 VkResult anv_GetCalibratedTimestampsEXT(
4450    VkDevice                                     _device,
4451    uint32_t                                     timestampCount,
4452    const VkCalibratedTimestampInfoEXT           *pTimestampInfos,
4453    uint64_t                                     *pTimestamps,
4454    uint64_t                                     *pMaxDeviation)
4455 {
4456    ANV_FROM_HANDLE(anv_device, device, _device);
4457    uint64_t timestamp_frequency = device->info.timestamp_frequency;
4458    int  ret;
4459    int d;
4460    uint64_t begin, end;
4461    uint64_t max_clock_period = 0;
4462 
4463 #ifdef CLOCK_MONOTONIC_RAW
4464    begin = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
4465 #else
4466    begin = anv_clock_gettime(CLOCK_MONOTONIC);
4467 #endif
4468 
4469    for (d = 0; d < timestampCount; d++) {
4470       switch (pTimestampInfos[d].timeDomain) {
4471       case VK_TIME_DOMAIN_DEVICE_EXT:
4472          ret = anv_gem_reg_read(device->fd, TIMESTAMP | I915_REG_READ_8B_WA,
4473                                 &pTimestamps[d]);
4474 
4475          if (ret != 0) {
4476             return anv_device_set_lost(device, "Failed to read the TIMESTAMP "
4477                                                "register: %m");
4478          }
4479          uint64_t device_period = DIV_ROUND_UP(1000000000, timestamp_frequency);
4480          max_clock_period = MAX2(max_clock_period, device_period);
4481          break;
4482       case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
4483          pTimestamps[d] = anv_clock_gettime(CLOCK_MONOTONIC);
4484          max_clock_period = MAX2(max_clock_period, 1);
4485          break;
4486 
4487 #ifdef CLOCK_MONOTONIC_RAW
4488       case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
4489          pTimestamps[d] = begin;
4490          break;
4491 #endif
4492       default:
4493          pTimestamps[d] = 0;
4494          break;
4495       }
4496    }
4497 
4498 #ifdef CLOCK_MONOTONIC_RAW
4499    end = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
4500 #else
4501    end = anv_clock_gettime(CLOCK_MONOTONIC);
4502 #endif
4503 
4504     /*
4505      * The maximum deviation is the sum of the interval over which we
4506      * perform the sampling and the maximum period of any sampled
4507      * clock. That's because the maximum skew between any two sampled
4508      * clock edges is when the sampled clock with the largest period is
4509      * sampled at the end of that period but right at the beginning of the
4510      * sampling interval and some other clock is sampled right at the
4511      * begining of its sampling period and right at the end of the
4512      * sampling interval. Let's assume the GPU has the longest clock
4513      * period and that the application is sampling GPU and monotonic:
4514      *
4515      *                               s                 e
4516      *			 w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
4517      *	Raw              -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
4518      *
4519      *                               g
4520      *		  0         1         2         3
4521      *	GPU       -----_____-----_____-----_____-----_____
4522      *
4523      *                                                m
4524      *					    x y z 0 1 2 3 4 5 6 7 8 9 a b c
4525      *	Monotonic                           -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
4526      *
4527      *	Interval                     <----------------->
4528      *	Deviation           <-------------------------->
4529      *
4530      *		s  = read(raw)       2
4531      *		g  = read(GPU)       1
4532      *		m  = read(monotonic) 2
4533      *		e  = read(raw)       b
4534      *
4535      * We round the sample interval up by one tick to cover sampling error
4536      * in the interval clock
4537      */
4538 
4539    uint64_t sample_interval = end - begin + 1;
4540 
4541    *pMaxDeviation = sample_interval + max_clock_period;
4542 
4543    return VK_SUCCESS;
4544 }
4545 
4546 /* vk_icd.h does not declare this function, so we declare it here to
4547  * suppress Wmissing-prototypes.
4548  */
4549 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
4550 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
4551 
4552 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t * pSupportedVersion)4553 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
4554 {
4555    /* For the full details on loader interface versioning, see
4556     * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
4557     * What follows is a condensed summary, to help you navigate the large and
4558     * confusing official doc.
4559     *
4560     *   - Loader interface v0 is incompatible with later versions. We don't
4561     *     support it.
4562     *
4563     *   - In loader interface v1:
4564     *       - The first ICD entrypoint called by the loader is
4565     *         vk_icdGetInstanceProcAddr(). The ICD must statically expose this
4566     *         entrypoint.
4567     *       - The ICD must statically expose no other Vulkan symbol unless it is
4568     *         linked with -Bsymbolic.
4569     *       - Each dispatchable Vulkan handle created by the ICD must be
4570     *         a pointer to a struct whose first member is VK_LOADER_DATA. The
4571     *         ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
4572     *       - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
4573     *         vkDestroySurfaceKHR(). The ICD must be capable of working with
4574     *         such loader-managed surfaces.
4575     *
4576     *    - Loader interface v2 differs from v1 in:
4577     *       - The first ICD entrypoint called by the loader is
4578     *         vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
4579     *         statically expose this entrypoint.
4580     *
4581     *    - Loader interface v3 differs from v2 in:
4582     *        - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
4583     *          vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
4584     *          because the loader no longer does so.
4585     *
4586     *    - Loader interface v4 differs from v3 in:
4587     *        - The ICD must implement vk_icdGetPhysicalDeviceProcAddr().
4588     */
4589    *pSupportedVersion = MIN2(*pSupportedVersion, 4u);
4590    return VK_SUCCESS;
4591 }
4592 
anv_CreatePrivateDataSlotEXT(VkDevice _device,const VkPrivateDataSlotCreateInfoEXT * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkPrivateDataSlotEXT * pPrivateDataSlot)4593 VkResult anv_CreatePrivateDataSlotEXT(
4594     VkDevice                                    _device,
4595     const VkPrivateDataSlotCreateInfoEXT*       pCreateInfo,
4596     const VkAllocationCallbacks*                pAllocator,
4597     VkPrivateDataSlotEXT*                       pPrivateDataSlot)
4598 {
4599    ANV_FROM_HANDLE(anv_device, device, _device);
4600    return vk_private_data_slot_create(&device->vk, pCreateInfo, pAllocator,
4601                                       pPrivateDataSlot);
4602 }
4603 
anv_DestroyPrivateDataSlotEXT(VkDevice _device,VkPrivateDataSlotEXT privateDataSlot,const VkAllocationCallbacks * pAllocator)4604 void anv_DestroyPrivateDataSlotEXT(
4605     VkDevice                                    _device,
4606     VkPrivateDataSlotEXT                        privateDataSlot,
4607     const VkAllocationCallbacks*                pAllocator)
4608 {
4609    ANV_FROM_HANDLE(anv_device, device, _device);
4610    vk_private_data_slot_destroy(&device->vk, privateDataSlot, pAllocator);
4611 }
4612 
anv_SetPrivateDataEXT(VkDevice _device,VkObjectType objectType,uint64_t objectHandle,VkPrivateDataSlotEXT privateDataSlot,uint64_t data)4613 VkResult anv_SetPrivateDataEXT(
4614     VkDevice                                    _device,
4615     VkObjectType                                objectType,
4616     uint64_t                                    objectHandle,
4617     VkPrivateDataSlotEXT                        privateDataSlot,
4618     uint64_t                                    data)
4619 {
4620    ANV_FROM_HANDLE(anv_device, device, _device);
4621    return vk_object_base_set_private_data(&device->vk,
4622                                           objectType, objectHandle,
4623                                           privateDataSlot, data);
4624 }
4625 
anv_GetPrivateDataEXT(VkDevice _device,VkObjectType objectType,uint64_t objectHandle,VkPrivateDataSlotEXT privateDataSlot,uint64_t * pData)4626 void anv_GetPrivateDataEXT(
4627     VkDevice                                    _device,
4628     VkObjectType                                objectType,
4629     uint64_t                                    objectHandle,
4630     VkPrivateDataSlotEXT                        privateDataSlot,
4631     uint64_t*                                   pData)
4632 {
4633    ANV_FROM_HANDLE(anv_device, device, _device);
4634    vk_object_base_get_private_data(&device->vk,
4635                                    objectType, objectHandle,
4636                                    privateDataSlot, pData);
4637 }
4638