Home | History | Annotate | Line # | Download | only in vulkan
      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 <sys/sysinfo.h>
     29 #include <unistd.h>
     30 #include <fcntl.h>
     31 #include <xf86drm.h>
     32 #include "drm-uapi/drm_fourcc.h"
     33 
     34 #include "anv_private.h"
     35 #include "util/strtod.h"
     36 #include "util/debug.h"
     37 #include "util/build_id.h"
     38 #include "util/disk_cache.h"
     39 #include "util/mesa-sha1.h"
     40 #include "util/os_file.h"
     41 #include "util/u_atomic.h"
     42 #include "util/u_string.h"
     43 #include "util/xmlpool.h"
     44 #include "git_sha1.h"
     45 #include "vk_util.h"
     46 #include "common/gen_defines.h"
     47 #include "compiler/glsl_types.h"
     48 
     49 #include "genxml/gen7_pack.h"
     50 
     51 static const char anv_dri_options_xml[] =
     52 DRI_CONF_BEGIN
     53    DRI_CONF_SECTION_PERFORMANCE
     54       DRI_CONF_VK_X11_OVERRIDE_MIN_IMAGE_COUNT(0)
     55    DRI_CONF_SECTION_END
     56 DRI_CONF_END;
     57 
     58 /* This is probably far to big but it reflects the max size used for messages
     59  * in OpenGLs KHR_debug.
     60  */
     61 #define MAX_DEBUG_MESSAGE_LENGTH    4096
     62 
     63 static void
     64 compiler_debug_log(void *data, const char *fmt, ...)
     65 {
     66    char str[MAX_DEBUG_MESSAGE_LENGTH];
     67    struct anv_device *device = (struct anv_device *)data;
     68 
     69    if (list_empty(&device->instance->debug_report_callbacks.callbacks))
     70       return;
     71 
     72    va_list args;
     73    va_start(args, fmt);
     74    (void) vsnprintf(str, MAX_DEBUG_MESSAGE_LENGTH, fmt, args);
     75    va_end(args);
     76 
     77    vk_debug_report(&device->instance->debug_report_callbacks,
     78                    VK_DEBUG_REPORT_DEBUG_BIT_EXT,
     79                    VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
     80                    0, 0, 0, "anv", str);
     81 }
     82 
     83 static void
     84 compiler_perf_log(void *data, const char *fmt, ...)
     85 {
     86    va_list args;
     87    va_start(args, fmt);
     88 
     89    if (unlikely(INTEL_DEBUG & DEBUG_PERF))
     90       intel_logd_v(fmt, args);
     91 
     92    va_end(args);
     93 }
     94 
     95 static uint64_t
     96 anv_compute_heap_size(int fd, uint64_t gtt_size)
     97 {
     98    /* Query the total ram from the system */
     99    struct sysinfo info;
    100    sysinfo(&info);
    101 
    102    uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit;
    103 
    104    /* We don't want to burn too much ram with the GPU.  If the user has 4GiB
    105     * or less, we use at most half.  If they have more than 4GiB, we use 3/4.
    106     */
    107    uint64_t available_ram;
    108    if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
    109       available_ram = total_ram / 2;
    110    else
    111       available_ram = total_ram * 3 / 4;
    112 
    113    /* We also want to leave some padding for things we allocate in the driver,
    114     * so don't go over 3/4 of the GTT either.
    115     */
    116    uint64_t available_gtt = gtt_size * 3 / 4;
    117 
    118    return MIN2(available_ram, available_gtt);
    119 }
    120 
    121 static VkResult
    122 anv_physical_device_init_heaps(struct anv_physical_device *device, int fd)
    123 {
    124    uint64_t gtt_size;
    125    if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
    126                                  &gtt_size) == -1) {
    127       /* If, for whatever reason, we can't actually get the GTT size from the
    128        * kernel (too old?) fall back to the aperture size.
    129        */
    130       anv_perf_warn(NULL, NULL,
    131                     "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
    132 
    133       if (anv_gem_get_aperture(fd, &gtt_size) == -1) {
    134          return vk_errorf(NULL, NULL, VK_ERROR_INITIALIZATION_FAILED,
    135                           "failed to get aperture size: %m");
    136       }
    137    }
    138 
    139    device->supports_48bit_addresses = (device->info.gen >= 8) &&
    140       gtt_size > (4ULL << 30 /* GiB */);
    141 
    142    uint64_t heap_size = anv_compute_heap_size(fd, gtt_size);
    143 
    144    if (heap_size > (2ull << 30) && !device->supports_48bit_addresses) {
    145       /* When running with an overridden PCI ID, we may get a GTT size from
    146        * the kernel that is greater than 2 GiB but the execbuf check for 48bit
    147        * address support can still fail.  Just clamp the address space size to
    148        * 2 GiB if we don't have 48-bit support.
    149        */
    150       intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
    151                         "not support for 48-bit addresses",
    152                         __FILE__, __LINE__);
    153       heap_size = 2ull << 30;
    154    }
    155 
    156    if (heap_size <= 3ull * (1ull << 30)) {
    157       /* In this case, everything fits nicely into the 32-bit address space,
    158        * so there's no need for supporting 48bit addresses on client-allocated
    159        * memory objects.
    160        */
    161       device->memory.heap_count = 1;
    162       device->memory.heaps[0] = (struct anv_memory_heap) {
    163          .vma_start = LOW_HEAP_MIN_ADDRESS,
    164          .vma_size = LOW_HEAP_SIZE,
    165          .size = heap_size,
    166          .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
    167          .supports_48bit_addresses = false,
    168       };
    169    } else {
    170       /* Not everything will fit nicely into a 32-bit address space.  In this
    171        * case we need a 64-bit heap.  Advertise a small 32-bit heap and a
    172        * larger 48-bit heap.  If we're in this case, then we have a total heap
    173        * size larger than 3GiB which most likely means they have 8 GiB of
    174        * video memory and so carving off 1 GiB for the 32-bit heap should be
    175        * reasonable.
    176        */
    177       const uint64_t heap_size_32bit = 1ull << 30;
    178       const uint64_t heap_size_48bit = heap_size - heap_size_32bit;
    179 
    180       assert(device->supports_48bit_addresses);
    181 
    182       device->memory.heap_count = 2;
    183       device->memory.heaps[0] = (struct anv_memory_heap) {
    184          .vma_start = HIGH_HEAP_MIN_ADDRESS,
    185          /* Leave the last 4GiB out of the high vma range, so that no state
    186           * base address + size can overflow 48 bits. For more information see
    187           * the comment about Wa32bitGeneralStateOffset in anv_allocator.c
    188           */
    189          .vma_size = gtt_size - (1ull << 32) - HIGH_HEAP_MIN_ADDRESS,
    190          .size = heap_size_48bit,
    191          .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
    192          .supports_48bit_addresses = true,
    193       };
    194       device->memory.heaps[1] = (struct anv_memory_heap) {
    195          .vma_start = LOW_HEAP_MIN_ADDRESS,
    196          .vma_size = LOW_HEAP_SIZE,
    197          .size = heap_size_32bit,
    198          .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
    199          .supports_48bit_addresses = false,
    200       };
    201    }
    202 
    203    uint32_t type_count = 0;
    204    for (uint32_t heap = 0; heap < device->memory.heap_count; heap++) {
    205       uint32_t valid_buffer_usage = ~0;
    206 
    207       /* There appears to be a hardware issue in the VF cache where it only
    208        * considers the bottom 32 bits of memory addresses.  If you happen to
    209        * have two vertex buffers which get placed exactly 4 GiB apart and use
    210        * them in back-to-back draw calls, you can get collisions.  In order to
    211        * solve this problem, we require vertex and index buffers be bound to
    212        * memory allocated out of the 32-bit heap.
    213        */
    214       if (device->memory.heaps[heap].supports_48bit_addresses) {
    215          valid_buffer_usage &= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
    216                                  VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
    217       }
    218 
    219       if (device->info.has_llc) {
    220          /* Big core GPUs share LLC with the CPU and thus one memory type can be
    221           * both cached and coherent at the same time.
    222           */
    223          device->memory.types[type_count++] = (struct anv_memory_type) {
    224             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
    225                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
    226                              VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
    227                              VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
    228             .heapIndex = heap,
    229             .valid_buffer_usage = valid_buffer_usage,
    230          };
    231       } else {
    232          /* The spec requires that we expose a host-visible, coherent memory
    233           * type, but Atom GPUs don't share LLC. Thus we offer two memory types
    234           * to give the application a choice between cached, but not coherent and
    235           * coherent but uncached (WC though).
    236           */
    237          device->memory.types[type_count++] = (struct anv_memory_type) {
    238             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
    239                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
    240                              VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    241             .heapIndex = heap,
    242             .valid_buffer_usage = valid_buffer_usage,
    243          };
    244          device->memory.types[type_count++] = (struct anv_memory_type) {
    245             .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
    246                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
    247                              VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
    248             .heapIndex = heap,
    249             .valid_buffer_usage = valid_buffer_usage,
    250          };
    251       }
    252    }
    253    device->memory.type_count = type_count;
    254 
    255    return VK_SUCCESS;
    256 }
    257 
    258 static VkResult
    259 anv_physical_device_init_uuids(struct anv_physical_device *device)
    260 {
    261    const struct build_id_note *note =
    262       build_id_find_nhdr_for_addr(anv_physical_device_init_uuids);
    263    if (!note) {
    264       return vk_errorf(device->instance, device,
    265                        VK_ERROR_INITIALIZATION_FAILED,
    266                        "Failed to find build-id");
    267    }
    268 
    269    unsigned build_id_len = build_id_length(note);
    270    if (build_id_len < 20) {
    271       return vk_errorf(device->instance, device,
    272                        VK_ERROR_INITIALIZATION_FAILED,
    273                        "build-id too short.  It needs to be a SHA");
    274    }
    275 
    276    memcpy(device->driver_build_sha1, build_id_data(note), 20);
    277 
    278    struct mesa_sha1 sha1_ctx;
    279    uint8_t sha1[20];
    280    STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
    281 
    282    /* The pipeline cache UUID is used for determining when a pipeline cache is
    283     * invalid.  It needs both a driver build and the PCI ID of the device.
    284     */
    285    _mesa_sha1_init(&sha1_ctx);
    286    _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
    287    _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
    288                      sizeof(device->chipset_id));
    289    _mesa_sha1_update(&sha1_ctx, &device->always_use_bindless,
    290                      sizeof(device->always_use_bindless));
    291    _mesa_sha1_update(&sha1_ctx, &device->has_a64_buffer_access,
    292                      sizeof(device->has_a64_buffer_access));
    293    _mesa_sha1_update(&sha1_ctx, &device->has_bindless_images,
    294                      sizeof(device->has_bindless_images));
    295    _mesa_sha1_update(&sha1_ctx, &device->has_bindless_samplers,
    296                      sizeof(device->has_bindless_samplers));
    297    _mesa_sha1_final(&sha1_ctx, sha1);
    298    memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
    299 
    300    /* The driver UUID is used for determining sharability of images and memory
    301     * between two Vulkan instances in separate processes.  People who want to
    302     * share memory need to also check the device UUID (below) so all this
    303     * needs to be is the build-id.
    304     */
    305    memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
    306 
    307    /* The device UUID uniquely identifies the given device within the machine.
    308     * Since we never have more than one device, this doesn't need to be a real
    309     * UUID.  However, on the off-chance that someone tries to use this to
    310     * cache pre-tiled images or something of the like, we use the PCI ID and
    311     * some bits of ISL info to ensure that this is safe.
    312     */
    313    _mesa_sha1_init(&sha1_ctx);
    314    _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
    315                      sizeof(device->chipset_id));
    316    _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
    317                      sizeof(device->isl_dev.has_bit6_swizzling));
    318    _mesa_sha1_final(&sha1_ctx, sha1);
    319    memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
    320 
    321    return VK_SUCCESS;
    322 }
    323 
    324 static void
    325 anv_physical_device_init_disk_cache(struct anv_physical_device *device)
    326 {
    327 #ifdef ENABLE_SHADER_CACHE
    328    char renderer[10];
    329    MAYBE_UNUSED int len = snprintf(renderer, sizeof(renderer), "anv_%04x",
    330                                    device->chipset_id);
    331    assert(len == sizeof(renderer) - 2);
    332 
    333    char timestamp[41];
    334    _mesa_sha1_format(timestamp, device->driver_build_sha1);
    335 
    336    const uint64_t driver_flags =
    337       brw_get_compiler_config_value(device->compiler);
    338    device->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
    339 #else
    340    device->disk_cache = NULL;
    341 #endif
    342 }
    343 
    344 static void
    345 anv_physical_device_free_disk_cache(struct anv_physical_device *device)
    346 {
    347 #ifdef ENABLE_SHADER_CACHE
    348    if (device->disk_cache)
    349       disk_cache_destroy(device->disk_cache);
    350 #else
    351    assert(device->disk_cache == NULL);
    352 #endif
    353 }
    354 
    355 static uint64_t
    356 get_available_system_memory()
    357 {
    358    char *meminfo = os_read_file("/proc/meminfo");
    359    if (!meminfo)
    360       return 0;
    361 
    362    char *str = strstr(meminfo, "MemAvailable:");
    363    if (!str) {
    364       free(meminfo);
    365       return 0;
    366    }
    367 
    368    uint64_t kb_mem_available;
    369    if (sscanf(str, "MemAvailable: %" PRIx64, &kb_mem_available) == 1) {
    370       free(meminfo);
    371       return kb_mem_available << 10;
    372    }
    373 
    374    free(meminfo);
    375    return 0;
    376 }
    377 
    378 static VkResult
    379 anv_physical_device_init(struct anv_physical_device *device,
    380                          struct anv_instance *instance,
    381                          drmDevicePtr drm_device)
    382 {
    383    const char *primary_path = drm_device->nodes[DRM_NODE_PRIMARY];
    384    const char *path = drm_device->nodes[DRM_NODE_RENDER];
    385    VkResult result;
    386    int fd;
    387    int master_fd = -1;
    388 
    389    brw_process_intel_debug_variable();
    390 
    391    fd = open(path, O_RDWR | O_CLOEXEC);
    392    if (fd < 0)
    393       return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
    394 
    395    device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
    396    device->instance = instance;
    397 
    398    assert(strlen(path) < ARRAY_SIZE(device->path));
    399    snprintf(device->path, ARRAY_SIZE(device->path), "%s", path);
    400 
    401    device->no_hw = getenv("INTEL_NO_HW") != NULL;
    402 
    403    const int pci_id_override = gen_get_pci_device_id_override();
    404    if (pci_id_override < 0) {
    405       device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
    406       if (!device->chipset_id) {
    407          result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
    408          goto fail;
    409       }
    410    } else {
    411       device->chipset_id = pci_id_override;
    412       device->no_hw = true;
    413    }
    414 
    415    device->pci_info.domain = drm_device->businfo.pci->domain;
    416    device->pci_info.bus = drm_device->businfo.pci->bus;
    417    device->pci_info.device = drm_device->businfo.pci->dev;
    418    device->pci_info.function = drm_device->businfo.pci->func;
    419 
    420    device->name = gen_get_device_name(device->chipset_id);
    421    if (!gen_get_device_info(device->chipset_id, &device->info)) {
    422       result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
    423       goto fail;
    424    }
    425 
    426    if (device->info.is_haswell) {
    427       intel_logw("Haswell Vulkan support is incomplete");
    428    } else if (device->info.gen == 7 && !device->info.is_baytrail) {
    429       intel_logw("Ivy Bridge Vulkan support is incomplete");
    430    } else if (device->info.gen == 7 && device->info.is_baytrail) {
    431       intel_logw("Bay Trail Vulkan support is incomplete");
    432    } else if (device->info.gen >= 8 && device->info.gen <= 11) {
    433       /* Gen8-11 fully supported */
    434    } else {
    435       result = vk_errorf(device->instance, device,
    436                          VK_ERROR_INCOMPATIBLE_DRIVER,
    437                          "Vulkan not yet supported on %s", device->name);
    438       goto fail;
    439    }
    440 
    441    device->cmd_parser_version = -1;
    442    if (device->info.gen == 7) {
    443       device->cmd_parser_version =
    444          anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
    445       if (device->cmd_parser_version == -1) {
    446          result = vk_errorf(device->instance, device,
    447                             VK_ERROR_INITIALIZATION_FAILED,
    448                             "failed to get command parser version");
    449          goto fail;
    450       }
    451    }
    452 
    453    if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
    454       result = vk_errorf(device->instance, device,
    455                          VK_ERROR_INITIALIZATION_FAILED,
    456                          "kernel missing gem wait");
    457       goto fail;
    458    }
    459 
    460    if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
    461       result = vk_errorf(device->instance, device,
    462                          VK_ERROR_INITIALIZATION_FAILED,
    463                          "kernel missing execbuf2");
    464       goto fail;
    465    }
    466 
    467    if (!device->info.has_llc &&
    468        anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
    469       result = vk_errorf(device->instance, device,
    470                          VK_ERROR_INITIALIZATION_FAILED,
    471                          "kernel missing wc mmap");
    472       goto fail;
    473    }
    474 
    475    result = anv_physical_device_init_heaps(device, fd);
    476    if (result != VK_SUCCESS)
    477       goto fail;
    478 
    479    device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
    480    device->has_exec_capture = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CAPTURE);
    481    device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
    482    device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
    483    device->has_syncobj_wait = device->has_syncobj &&
    484                               anv_gem_supports_syncobj_wait(fd);
    485    device->has_context_priority = anv_gem_has_context_priority(fd);
    486 
    487    device->use_softpin = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_SOFTPIN)
    488       && device->supports_48bit_addresses;
    489 
    490    device->has_context_isolation =
    491       anv_gem_get_param(fd, I915_PARAM_HAS_CONTEXT_ISOLATION);
    492 
    493    device->always_use_bindless =
    494       env_var_as_boolean("ANV_ALWAYS_BINDLESS", false);
    495 
    496    /* We first got the A64 messages on broadwell and we can only use them if
    497     * we can pass addresses directly into the shader which requires softpin.
    498     */
    499    device->has_a64_buffer_access = device->info.gen >= 8 &&
    500                                    device->use_softpin;
    501 
    502    /* We first get bindless image access on Skylake and we can only really do
    503     * it if we don't have any relocations so we need softpin.
    504     */
    505    device->has_bindless_images = device->info.gen >= 9 &&
    506                                  device->use_softpin;
    507 
    508    /* We've had bindless samplers since Ivy Bridge (forever in Vulkan terms)
    509     * because it's just a matter of setting the sampler address in the sample
    510     * message header.  However, we've not bothered to wire it up for vec4 so
    511     * we leave it disabled on gen7.
    512     */
    513    device->has_bindless_samplers = device->info.gen >= 8;
    514 
    515    device->has_mem_available = get_available_system_memory() != 0;
    516 
    517    /* Starting with Gen10, the timestamp frequency of the command streamer may
    518     * vary from one part to another. We can query the value from the kernel.
    519     */
    520    if (device->info.gen >= 10) {
    521       int timestamp_frequency =
    522          anv_gem_get_param(fd, I915_PARAM_CS_TIMESTAMP_FREQUENCY);
    523 
    524       if (timestamp_frequency < 0)
    525          intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
    526       else
    527          device->info.timestamp_frequency = timestamp_frequency;
    528    }
    529 
    530    /* GENs prior to 8 do not support EU/Subslice info */
    531    if (device->info.gen >= 8) {
    532       device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
    533       device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
    534 
    535       /* Without this information, we cannot get the right Braswell
    536        * brandstrings, and we have to use conservative numbers for GPGPU on
    537        * many platforms, but otherwise, things will just work.
    538        */
    539       if (device->subslice_total < 1 || device->eu_total < 1) {
    540          intel_logw("Kernel 4.1 required to properly query GPU properties");
    541       }
    542    } else if (device->info.gen == 7) {
    543       device->subslice_total = 1 << (device->info.gt - 1);
    544    }
    545 
    546    if (device->info.is_cherryview &&
    547        device->subslice_total > 0 && device->eu_total > 0) {
    548       /* Logical CS threads = EUs per subslice * num threads per EU */
    549       uint32_t max_cs_threads =
    550          device->eu_total / device->subslice_total * device->info.num_thread_per_eu;
    551 
    552       /* Fuse configurations may give more threads than expected, never less. */
    553       if (max_cs_threads > device->info.max_cs_threads)
    554          device->info.max_cs_threads = max_cs_threads;
    555    }
    556 
    557    device->compiler = brw_compiler_create(NULL, &device->info);
    558    if (device->compiler == NULL) {
    559       result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
    560       goto fail;
    561    }
    562    device->compiler->shader_debug_log = compiler_debug_log;
    563    device->compiler->shader_perf_log = compiler_perf_log;
    564    device->compiler->supports_pull_constants = false;
    565    device->compiler->constant_buffer_0_is_relative =
    566       device->info.gen < 8 || !device->has_context_isolation;
    567    device->compiler->supports_shader_constants = true;
    568 
    569    /* Broadwell PRM says:
    570     *
    571     *   "Before Gen8, there was a historical configuration control field to
    572     *    swizzle address bit[6] for in X/Y tiling modes. This was set in three
    573     *    different places: TILECTL[1:0], ARB_MODE[5:4], and
    574     *    DISP_ARB_CTL[14:13].
    575     *
    576     *    For Gen8 and subsequent generations, the swizzle fields are all
    577     *    reserved, and the CPU's memory controller performs all address
    578     *    swizzling modifications."
    579     */
    580    bool swizzled =
    581       device->info.gen < 8 && anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
    582 
    583    isl_device_init(&device->isl_dev, &device->info, swizzled);
    584 
    585    result = anv_physical_device_init_uuids(device);
    586    if (result != VK_SUCCESS)
    587       goto fail;
    588 
    589    anv_physical_device_init_disk_cache(device);
    590 
    591    if (instance->enabled_extensions.KHR_display) {
    592       master_fd = open(primary_path, O_RDWR | O_CLOEXEC);
    593       if (master_fd >= 0) {
    594          /* prod the device with a GETPARAM call which will fail if
    595           * we don't have permission to even render on this device
    596           */
    597          if (anv_gem_get_param(master_fd, I915_PARAM_CHIPSET_ID) == 0) {
    598             close(master_fd);
    599             master_fd = -1;
    600          }
    601       }
    602    }
    603    device->master_fd = master_fd;
    604 
    605    result = anv_init_wsi(device);
    606    if (result != VK_SUCCESS) {
    607       ralloc_free(device->compiler);
    608       anv_physical_device_free_disk_cache(device);
    609       goto fail;
    610    }
    611 
    612    anv_physical_device_get_supported_extensions(device,
    613                                                 &device->supported_extensions);
    614 
    615 
    616    device->local_fd = fd;
    617 
    618    return VK_SUCCESS;
    619 
    620 fail:
    621    close(fd);
    622    if (master_fd != -1)
    623       close(master_fd);
    624    return result;
    625 }
    626 
    627 static void
    628 anv_physical_device_finish(struct anv_physical_device *device)
    629 {
    630    anv_finish_wsi(device);
    631    anv_physical_device_free_disk_cache(device);
    632    ralloc_free(device->compiler);
    633    close(device->local_fd);
    634    if (device->master_fd >= 0)
    635       close(device->master_fd);
    636 }
    637 
    638 static void *
    639 default_alloc_func(void *pUserData, size_t size, size_t align,
    640                    VkSystemAllocationScope allocationScope)
    641 {
    642    return malloc(size);
    643 }
    644 
    645 static void *
    646 default_realloc_func(void *pUserData, void *pOriginal, size_t size,
    647                      size_t align, VkSystemAllocationScope allocationScope)
    648 {
    649    return realloc(pOriginal, size);
    650 }
    651 
    652 static void
    653 default_free_func(void *pUserData, void *pMemory)
    654 {
    655    free(pMemory);
    656 }
    657 
    658 static const VkAllocationCallbacks default_alloc = {
    659    .pUserData = NULL,
    660    .pfnAllocation = default_alloc_func,
    661    .pfnReallocation = default_realloc_func,
    662    .pfnFree = default_free_func,
    663 };
    664 
    665 VkResult anv_EnumerateInstanceExtensionProperties(
    666     const char*                                 pLayerName,
    667     uint32_t*                                   pPropertyCount,
    668     VkExtensionProperties*                      pProperties)
    669 {
    670    VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
    671 
    672    for (int i = 0; i < ANV_INSTANCE_EXTENSION_COUNT; i++) {
    673       if (anv_instance_extensions_supported.extensions[i]) {
    674          vk_outarray_append(&out, prop) {
    675             *prop = anv_instance_extensions[i];
    676          }
    677       }
    678    }
    679 
    680    return vk_outarray_status(&out);
    681 }
    682 
    683 VkResult anv_CreateInstance(
    684     const VkInstanceCreateInfo*                 pCreateInfo,
    685     const VkAllocationCallbacks*                pAllocator,
    686     VkInstance*                                 pInstance)
    687 {
    688    struct anv_instance *instance;
    689    VkResult result;
    690 
    691    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
    692 
    693    struct anv_instance_extension_table enabled_extensions = {};
    694    for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
    695       int idx;
    696       for (idx = 0; idx < ANV_INSTANCE_EXTENSION_COUNT; idx++) {
    697          if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
    698                     anv_instance_extensions[idx].extensionName) == 0)
    699             break;
    700       }
    701 
    702       if (idx >= ANV_INSTANCE_EXTENSION_COUNT)
    703          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
    704 
    705       if (!anv_instance_extensions_supported.extensions[idx])
    706          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
    707 
    708       enabled_extensions.extensions[idx] = true;
    709    }
    710 
    711    instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
    712                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
    713    if (!instance)
    714       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
    715 
    716    instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
    717 
    718    if (pAllocator)
    719       instance->alloc = *pAllocator;
    720    else
    721       instance->alloc = default_alloc;
    722 
    723    instance->app_info = (struct anv_app_info) { .api_version = 0 };
    724    if (pCreateInfo->pApplicationInfo) {
    725       const VkApplicationInfo *app = pCreateInfo->pApplicationInfo;
    726 
    727       instance->app_info.app_name =
    728          vk_strdup(&instance->alloc, app->pApplicationName,
    729                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
    730       instance->app_info.app_version = app->applicationVersion;
    731 
    732       instance->app_info.engine_name =
    733          vk_strdup(&instance->alloc, app->pEngineName,
    734                    VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
    735       instance->app_info.engine_version = app->engineVersion;
    736 
    737       instance->app_info.api_version = app->apiVersion;
    738    }
    739 
    740    if (instance->app_info.api_version == 0)
    741       instance->app_info.api_version = VK_API_VERSION_1_0;
    742 
    743    instance->enabled_extensions = enabled_extensions;
    744 
    745    for (unsigned i = 0; i < ARRAY_SIZE(instance->dispatch.entrypoints); i++) {
    746       /* Vulkan requires that entrypoints for extensions which have not been
    747        * enabled must not be advertised.
    748        */
    749       if (!anv_instance_entrypoint_is_enabled(i, instance->app_info.api_version,
    750                                               &instance->enabled_extensions)) {
    751          instance->dispatch.entrypoints[i] = NULL;
    752       } else {
    753          instance->dispatch.entrypoints[i] =
    754             anv_instance_dispatch_table.entrypoints[i];
    755       }
    756    }
    757 
    758    for (unsigned i = 0; i < ARRAY_SIZE(instance->device_dispatch.entrypoints); i++) {
    759       /* Vulkan requires that entrypoints for extensions which have not been
    760        * enabled must not be advertised.
    761        */
    762       if (!anv_device_entrypoint_is_enabled(i, instance->app_info.api_version,
    763                                             &instance->enabled_extensions, NULL)) {
    764          instance->device_dispatch.entrypoints[i] = NULL;
    765       } else {
    766          instance->device_dispatch.entrypoints[i] =
    767             anv_device_dispatch_table.entrypoints[i];
    768       }
    769    }
    770 
    771    instance->physicalDeviceCount = -1;
    772 
    773    result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
    774    if (result != VK_SUCCESS) {
    775       vk_free2(&default_alloc, pAllocator, instance);
    776       return vk_error(result);
    777    }
    778 
    779    instance->pipeline_cache_enabled =
    780       env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
    781 
    782    _mesa_locale_init();
    783    glsl_type_singleton_init_or_ref();
    784 
    785    VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
    786 
    787    driParseOptionInfo(&instance->available_dri_options, anv_dri_options_xml);
    788    driParseConfigFiles(&instance->dri_options, &instance->available_dri_options,
    789                        0, "anv", NULL);
    790 
    791    *pInstance = anv_instance_to_handle(instance);
    792 
    793    return VK_SUCCESS;
    794 }
    795 
    796 void anv_DestroyInstance(
    797     VkInstance                                  _instance,
    798     const VkAllocationCallbacks*                pAllocator)
    799 {
    800    ANV_FROM_HANDLE(anv_instance, instance, _instance);
    801 
    802    if (!instance)
    803       return;
    804 
    805    if (instance->physicalDeviceCount > 0) {
    806       /* We support at most one physical device. */
    807       assert(instance->physicalDeviceCount == 1);
    808       anv_physical_device_finish(&instance->physicalDevice);
    809    }
    810 
    811    vk_free(&instance->alloc, (char *)instance->app_info.app_name);
    812    vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
    813 
    814    VG(VALGRIND_DESTROY_MEMPOOL(instance));
    815 
    816    vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
    817 
    818    glsl_type_singleton_decref();
    819    _mesa_locale_fini();
    820 
    821    driDestroyOptionCache(&instance->dri_options);
    822    driDestroyOptionInfo(&instance->available_dri_options);
    823 
    824    vk_free(&instance->alloc, instance);
    825 }
    826 
    827 static VkResult
    828 anv_enumerate_devices(struct anv_instance *instance)
    829 {
    830    /* TODO: Check for more devices ? */
    831    drmDevicePtr devices[8];
    832    VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
    833    int max_devices;
    834 
    835    instance->physicalDeviceCount = 0;
    836 
    837    max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
    838    if (max_devices < 1)
    839       return VK_ERROR_INCOMPATIBLE_DRIVER;
    840 
    841    for (unsigned i = 0; i < (unsigned)max_devices; i++) {
    842       if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
    843           devices[i]->bustype == DRM_BUS_PCI &&
    844           devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
    845 
    846          result = anv_physical_device_init(&instance->physicalDevice,
    847                                            instance, devices[i]);
    848          if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
    849             break;
    850       }
    851    }
    852    drmFreeDevices(devices, max_devices);
    853 
    854    if (result == VK_SUCCESS)
    855       instance->physicalDeviceCount = 1;
    856 
    857    return result;
    858 }
    859 
    860 static VkResult
    861 anv_instance_ensure_physical_device(struct anv_instance *instance)
    862 {
    863    if (instance->physicalDeviceCount < 0) {
    864       VkResult result = anv_enumerate_devices(instance);
    865       if (result != VK_SUCCESS &&
    866           result != VK_ERROR_INCOMPATIBLE_DRIVER)
    867          return result;
    868    }
    869 
    870    return VK_SUCCESS;
    871 }
    872 
    873 VkResult anv_EnumeratePhysicalDevices(
    874     VkInstance                                  _instance,
    875     uint32_t*                                   pPhysicalDeviceCount,
    876     VkPhysicalDevice*                           pPhysicalDevices)
    877 {
    878    ANV_FROM_HANDLE(anv_instance, instance, _instance);
    879    VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
    880 
    881    VkResult result = anv_instance_ensure_physical_device(instance);
    882    if (result != VK_SUCCESS)
    883       return result;
    884 
    885    if (instance->physicalDeviceCount == 0)
    886       return VK_SUCCESS;
    887 
    888    assert(instance->physicalDeviceCount == 1);
    889    vk_outarray_append(&out, i) {
    890       *i = anv_physical_device_to_handle(&instance->physicalDevice);
    891    }
    892 
    893    return vk_outarray_status(&out);
    894 }
    895 
    896 VkResult anv_EnumeratePhysicalDeviceGroups(
    897     VkInstance                                  _instance,
    898     uint32_t*                                   pPhysicalDeviceGroupCount,
    899     VkPhysicalDeviceGroupProperties*            pPhysicalDeviceGroupProperties)
    900 {
    901    ANV_FROM_HANDLE(anv_instance, instance, _instance);
    902    VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
    903                          pPhysicalDeviceGroupCount);
    904 
    905    VkResult result = anv_instance_ensure_physical_device(instance);
    906    if (result != VK_SUCCESS)
    907       return result;
    908 
    909    if (instance->physicalDeviceCount == 0)
    910       return VK_SUCCESS;
    911 
    912    assert(instance->physicalDeviceCount == 1);
    913 
    914    vk_outarray_append(&out, p) {
    915       p->physicalDeviceCount = 1;
    916       memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
    917       p->physicalDevices[0] =
    918          anv_physical_device_to_handle(&instance->physicalDevice);
    919       p->subsetAllocation = false;
    920 
    921       vk_foreach_struct(ext, p->pNext)
    922          anv_debug_ignored_stype(ext->sType);
    923    }
    924 
    925    return vk_outarray_status(&out);
    926 }
    927 
    928 void anv_GetPhysicalDeviceFeatures(
    929     VkPhysicalDevice                            physicalDevice,
    930     VkPhysicalDeviceFeatures*                   pFeatures)
    931 {
    932    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
    933 
    934    *pFeatures = (VkPhysicalDeviceFeatures) {
    935       .robustBufferAccess                       = true,
    936       .fullDrawIndexUint32                      = true,
    937       .imageCubeArray                           = true,
    938       .independentBlend                         = true,
    939       .geometryShader                           = true,
    940       .tessellationShader                       = true,
    941       .sampleRateShading                        = true,
    942       .dualSrcBlend                             = true,
    943       .logicOp                                  = true,
    944       .multiDrawIndirect                        = true,
    945       .drawIndirectFirstInstance                = true,
    946       .depthClamp                               = true,
    947       .depthBiasClamp                           = true,
    948       .fillModeNonSolid                         = true,
    949       .depthBounds                              = false,
    950       .wideLines                                = true,
    951       .largePoints                              = true,
    952       .alphaToOne                               = true,
    953       .multiViewport                            = true,
    954       .samplerAnisotropy                        = true,
    955       .textureCompressionETC2                   = pdevice->info.gen >= 8 ||
    956                                                   pdevice->info.is_baytrail,
    957       .textureCompressionASTC_LDR               = pdevice->info.gen >= 9, /* FINISHME CHV */
    958       .textureCompressionBC                     = true,
    959       .occlusionQueryPrecise                    = true,
    960       .pipelineStatisticsQuery                  = true,
    961       .fragmentStoresAndAtomics                 = true,
    962       .shaderTessellationAndGeometryPointSize   = true,
    963       .shaderImageGatherExtended                = true,
    964       .shaderStorageImageExtendedFormats        = true,
    965       .shaderStorageImageMultisample            = false,
    966       .shaderStorageImageReadWithoutFormat      = false,
    967       .shaderStorageImageWriteWithoutFormat     = true,
    968       .shaderUniformBufferArrayDynamicIndexing  = true,
    969       .shaderSampledImageArrayDynamicIndexing   = true,
    970       .shaderStorageBufferArrayDynamicIndexing  = true,
    971       .shaderStorageImageArrayDynamicIndexing   = true,
    972       .shaderClipDistance                       = true,
    973       .shaderCullDistance                       = true,
    974       .shaderFloat64                            = pdevice->info.gen >= 8 &&
    975                                                   pdevice->info.has_64bit_types,
    976       .shaderInt64                              = pdevice->info.gen >= 8 &&
    977                                                   pdevice->info.has_64bit_types,
    978       .shaderInt16                              = pdevice->info.gen >= 8,
    979       .shaderResourceMinLod                     = pdevice->info.gen >= 9,
    980       .variableMultisampleRate                  = true,
    981       .inheritedQueries                         = true,
    982    };
    983 
    984    /* We can't do image stores in vec4 shaders */
    985    pFeatures->vertexPipelineStoresAndAtomics =
    986       pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
    987       pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
    988 
    989    struct anv_app_info *app_info = &pdevice->instance->app_info;
    990 
    991    /* The new DOOM and Wolfenstein games require depthBounds without
    992     * checking for it.  They seem to run fine without it so just claim it's
    993     * there and accept the consequences.
    994     */
    995    if (app_info->engine_name && strcmp(app_info->engine_name, "idTech") == 0)
    996       pFeatures->depthBounds = true;
    997 }
    998 
    999 void anv_GetPhysicalDeviceFeatures2(
   1000     VkPhysicalDevice                            physicalDevice,
   1001     VkPhysicalDeviceFeatures2*                  pFeatures)
   1002 {
   1003    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
   1004    anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
   1005 
   1006    vk_foreach_struct(ext, pFeatures->pNext) {
   1007       switch (ext->sType) {
   1008       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
   1009          VkPhysicalDevice8BitStorageFeaturesKHR *features =
   1010             (VkPhysicalDevice8BitStorageFeaturesKHR *)ext;
   1011          features->storageBuffer8BitAccess = pdevice->info.gen >= 8;
   1012          features->uniformAndStorageBuffer8BitAccess = pdevice->info.gen >= 8;
   1013          features->storagePushConstant8 = pdevice->info.gen >= 8;
   1014          break;
   1015       }
   1016 
   1017       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
   1018          VkPhysicalDevice16BitStorageFeatures *features =
   1019             (VkPhysicalDevice16BitStorageFeatures *)ext;
   1020          features->storageBuffer16BitAccess = pdevice->info.gen >= 8;
   1021          features->uniformAndStorageBuffer16BitAccess = pdevice->info.gen >= 8;
   1022          features->storagePushConstant16 = pdevice->info.gen >= 8;
   1023          features->storageInputOutput16 = false;
   1024          break;
   1025       }
   1026 
   1027       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT: {
   1028          VkPhysicalDeviceBufferDeviceAddressFeaturesEXT *features = (void *)ext;
   1029          features->bufferDeviceAddress = pdevice->has_a64_buffer_access;
   1030          features->bufferDeviceAddressCaptureReplay = false;
   1031          features->bufferDeviceAddressMultiDevice = false;
   1032          break;
   1033       }
   1034 
   1035       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COMPUTE_SHADER_DERIVATIVES_FEATURES_NV: {
   1036          VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *features =
   1037             (VkPhysicalDeviceComputeShaderDerivativesFeaturesNV *)ext;
   1038          features->computeDerivativeGroupQuads = true;
   1039          features->computeDerivativeGroupLinear = true;
   1040          break;
   1041       }
   1042 
   1043       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
   1044          VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
   1045             (VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
   1046          features->conditionalRendering = pdevice->info.gen >= 8 ||
   1047                                           pdevice->info.is_haswell;
   1048          features->inheritedConditionalRendering = pdevice->info.gen >= 8 ||
   1049                                                    pdevice->info.is_haswell;
   1050          break;
   1051       }
   1052 
   1053       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: {
   1054          VkPhysicalDeviceDepthClipEnableFeaturesEXT *features =
   1055             (VkPhysicalDeviceDepthClipEnableFeaturesEXT *)ext;
   1056          features->depthClipEnable = true;
   1057          break;
   1058       }
   1059 
   1060       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR: {
   1061          VkPhysicalDeviceFloat16Int8FeaturesKHR *features = (void *)ext;
   1062          features->shaderFloat16 = pdevice->info.gen >= 8;
   1063          features->shaderInt8 = pdevice->info.gen >= 8;
   1064          break;
   1065       }
   1066 
   1067       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT: {
   1068          VkPhysicalDeviceHostQueryResetFeaturesEXT *features =
   1069             (VkPhysicalDeviceHostQueryResetFeaturesEXT *)ext;
   1070          features->hostQueryReset = true;
   1071          break;
   1072       }
   1073 
   1074       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT: {
   1075          VkPhysicalDeviceDescriptorIndexingFeaturesEXT *features =
   1076             (VkPhysicalDeviceDescriptorIndexingFeaturesEXT *)ext;
   1077          features->shaderInputAttachmentArrayDynamicIndexing = false;
   1078          features->shaderUniformTexelBufferArrayDynamicIndexing = true;
   1079          features->shaderStorageTexelBufferArrayDynamicIndexing = true;
   1080          features->shaderUniformBufferArrayNonUniformIndexing = false;
   1081          features->shaderSampledImageArrayNonUniformIndexing = true;
   1082          features->shaderStorageBufferArrayNonUniformIndexing = true;
   1083          features->shaderStorageImageArrayNonUniformIndexing = true;
   1084          features->shaderInputAttachmentArrayNonUniformIndexing = false;
   1085          features->shaderUniformTexelBufferArrayNonUniformIndexing = true;
   1086          features->shaderStorageTexelBufferArrayNonUniformIndexing = true;
   1087          features->descriptorBindingUniformBufferUpdateAfterBind = false;
   1088          features->descriptorBindingSampledImageUpdateAfterBind = true;
   1089          features->descriptorBindingStorageImageUpdateAfterBind = true;
   1090          features->descriptorBindingStorageBufferUpdateAfterBind = true;
   1091          features->descriptorBindingUniformTexelBufferUpdateAfterBind = true;
   1092          features->descriptorBindingStorageTexelBufferUpdateAfterBind = true;
   1093          features->descriptorBindingUpdateUnusedWhilePending = true;
   1094          features->descriptorBindingPartiallyBound = true;
   1095          features->descriptorBindingVariableDescriptorCount = false;
   1096          features->runtimeDescriptorArray = true;
   1097          break;
   1098       }
   1099 
   1100       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_FEATURES_EXT: {
   1101          VkPhysicalDeviceInlineUniformBlockFeaturesEXT *features =
   1102             (VkPhysicalDeviceInlineUniformBlockFeaturesEXT *)ext;
   1103          features->inlineUniformBlock = true;
   1104          features->descriptorBindingInlineUniformBlockUpdateAfterBind = true;
   1105          break;
   1106       }
   1107 
   1108       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
   1109          VkPhysicalDeviceMultiviewFeatures *features =
   1110             (VkPhysicalDeviceMultiviewFeatures *)ext;
   1111          features->multiview = true;
   1112          features->multiviewGeometryShader = true;
   1113          features->multiviewTessellationShader = true;
   1114          break;
   1115       }
   1116 
   1117       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
   1118          VkPhysicalDeviceProtectedMemoryFeatures *features = (void *)ext;
   1119          features->protectedMemory = false;
   1120          break;
   1121       }
   1122 
   1123       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
   1124          VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
   1125             (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
   1126          features->samplerYcbcrConversion = true;
   1127          break;
   1128       }
   1129 
   1130       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
   1131          VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
   1132             (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
   1133          features->scalarBlockLayout = true;
   1134          break;
   1135       }
   1136 
   1137       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR: {
   1138          VkPhysicalDeviceShaderAtomicInt64FeaturesKHR *features = (void *)ext;
   1139          features->shaderBufferInt64Atomics =
   1140             pdevice->info.gen >= 9 && pdevice->use_softpin;
   1141          features->shaderSharedInt64Atomics = VK_FALSE;
   1142          break;
   1143       }
   1144 
   1145       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES: {
   1146          VkPhysicalDeviceShaderDrawParametersFeatures *features = (void *)ext;
   1147          features->shaderDrawParameters = true;
   1148          break;
   1149       }
   1150 
   1151       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES: {
   1152          VkPhysicalDeviceVariablePointersFeatures *features = (void *)ext;
   1153          features->variablePointersStorageBuffer = true;
   1154          features->variablePointers = true;
   1155          break;
   1156       }
   1157 
   1158       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT: {
   1159          VkPhysicalDeviceTransformFeedbackFeaturesEXT *features =
   1160             (VkPhysicalDeviceTransformFeedbackFeaturesEXT *)ext;
   1161          features->transformFeedback = true;
   1162          features->geometryStreams = true;
   1163          break;
   1164       }
   1165 
   1166       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
   1167          VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
   1168             (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
   1169          features->vertexAttributeInstanceRateDivisor = true;
   1170          features->vertexAttributeInstanceRateZeroDivisor = true;
   1171          break;
   1172       }
   1173 
   1174       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_YCBCR_IMAGE_ARRAYS_FEATURES_EXT: {
   1175          VkPhysicalDeviceYcbcrImageArraysFeaturesEXT *features =
   1176             (VkPhysicalDeviceYcbcrImageArraysFeaturesEXT *)ext;
   1177          features->ycbcrImageArrays = true;
   1178          break;
   1179       }
   1180 
   1181       default:
   1182          anv_debug_ignored_stype(ext->sType);
   1183          break;
   1184       }
   1185    }
   1186 }
   1187 
   1188 #define MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS   64
   1189 
   1190 #define MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS 64
   1191 #define MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS       256
   1192 
   1193 void anv_GetPhysicalDeviceProperties(
   1194     VkPhysicalDevice                            physicalDevice,
   1195     VkPhysicalDeviceProperties*                 pProperties)
   1196 {
   1197    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
   1198    const struct gen_device_info *devinfo = &pdevice->info;
   1199 
   1200    /* See assertions made when programming the buffer surface state. */
   1201    const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
   1202                                       (1ul << 30) : (1ul << 27);
   1203 
   1204    const uint32_t max_ssbos = pdevice->has_a64_buffer_access ? UINT16_MAX : 64;
   1205    const uint32_t max_textures =
   1206       pdevice->has_bindless_images ? UINT16_MAX : 128;
   1207    const uint32_t max_samplers =
   1208       pdevice->has_bindless_samplers ? UINT16_MAX :
   1209       (devinfo->gen >= 8 || devinfo->is_haswell) ? 128 : 16;
   1210    const uint32_t max_images =
   1211       pdevice->has_bindless_images ? UINT16_MAX : MAX_IMAGES;
   1212 
   1213    /* The moment we have anything bindless, claim a high per-stage limit */
   1214    const uint32_t max_per_stage =
   1215       pdevice->has_a64_buffer_access ? UINT32_MAX :
   1216                                        MAX_BINDING_TABLE_SIZE - MAX_RTS;
   1217 
   1218    const uint32_t max_workgroup_size = 32 * devinfo->max_cs_threads;
   1219 
   1220    VkSampleCountFlags sample_counts =
   1221       isl_device_get_sample_counts(&pdevice->isl_dev);
   1222 
   1223 
   1224    VkPhysicalDeviceLimits limits = {
   1225       .maxImageDimension1D                      = (1 << 14),
   1226       .maxImageDimension2D                      = (1 << 14),
   1227       .maxImageDimension3D                      = (1 << 11),
   1228       .maxImageDimensionCube                    = (1 << 14),
   1229       .maxImageArrayLayers                      = (1 << 11),
   1230       .maxTexelBufferElements                   = 128 * 1024 * 1024,
   1231       .maxUniformBufferRange                    = (1ul << 27),
   1232       .maxStorageBufferRange                    = max_raw_buffer_sz,
   1233       .maxPushConstantsSize                     = MAX_PUSH_CONSTANTS_SIZE,
   1234       .maxMemoryAllocationCount                 = UINT32_MAX,
   1235       .maxSamplerAllocationCount                = 64 * 1024,
   1236       .bufferImageGranularity                   = 64, /* A cache line */
   1237       .sparseAddressSpaceSize                   = 0,
   1238       .maxBoundDescriptorSets                   = MAX_SETS,
   1239       .maxPerStageDescriptorSamplers            = max_samplers,
   1240       .maxPerStageDescriptorUniformBuffers      = MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS,
   1241       .maxPerStageDescriptorStorageBuffers      = max_ssbos,
   1242       .maxPerStageDescriptorSampledImages       = max_textures,
   1243       .maxPerStageDescriptorStorageImages       = max_images,
   1244       .maxPerStageDescriptorInputAttachments    = MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS,
   1245       .maxPerStageResources                     = max_per_stage,
   1246       .maxDescriptorSetSamplers                 = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSamplers */
   1247       .maxDescriptorSetUniformBuffers           = 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS,           /* number of stages * maxPerStageDescriptorUniformBuffers */
   1248       .maxDescriptorSetUniformBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
   1249       .maxDescriptorSetStorageBuffers           = 6 * max_ssbos,    /* number of stages * maxPerStageDescriptorStorageBuffers */
   1250       .maxDescriptorSetStorageBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
   1251       .maxDescriptorSetSampledImages            = 6 * max_textures, /* number of stages * maxPerStageDescriptorSampledImages */
   1252       .maxDescriptorSetStorageImages            = 6 * max_images,   /* number of stages * maxPerStageDescriptorStorageImages */
   1253       .maxDescriptorSetInputAttachments         = MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS,
   1254       .maxVertexInputAttributes                 = MAX_VBS,
   1255       .maxVertexInputBindings                   = MAX_VBS,
   1256       .maxVertexInputAttributeOffset            = 2047,
   1257       .maxVertexInputBindingStride              = 2048,
   1258       .maxVertexOutputComponents                = 128,
   1259       .maxTessellationGenerationLevel           = 64,
   1260       .maxTessellationPatchSize                 = 32,
   1261       .maxTessellationControlPerVertexInputComponents = 128,
   1262       .maxTessellationControlPerVertexOutputComponents = 128,
   1263       .maxTessellationControlPerPatchOutputComponents = 128,
   1264       .maxTessellationControlTotalOutputComponents = 2048,
   1265       .maxTessellationEvaluationInputComponents = 128,
   1266       .maxTessellationEvaluationOutputComponents = 128,
   1267       .maxGeometryShaderInvocations             = 32,
   1268       .maxGeometryInputComponents               = 64,
   1269       .maxGeometryOutputComponents              = 128,
   1270       .maxGeometryOutputVertices                = 256,
   1271       .maxGeometryTotalOutputComponents         = 1024,
   1272       .maxFragmentInputComponents               = 116, /* 128 components - (PSIZ, CLIP_DIST0, CLIP_DIST1) */
   1273       .maxFragmentOutputAttachments             = 8,
   1274       .maxFragmentDualSrcAttachments            = 1,
   1275       .maxFragmentCombinedOutputResources       = 8,
   1276       .maxComputeSharedMemorySize               = 32768,
   1277       .maxComputeWorkGroupCount                 = { 65535, 65535, 65535 },
   1278       .maxComputeWorkGroupInvocations           = max_workgroup_size,
   1279       .maxComputeWorkGroupSize = {
   1280          max_workgroup_size,
   1281          max_workgroup_size,
   1282          max_workgroup_size,
   1283       },
   1284       .subPixelPrecisionBits                    = 8,
   1285       .subTexelPrecisionBits                    = 8,
   1286       .mipmapPrecisionBits                      = 8,
   1287       .maxDrawIndexedIndexValue                 = UINT32_MAX,
   1288       .maxDrawIndirectCount                     = UINT32_MAX,
   1289       .maxSamplerLodBias                        = 16,
   1290       .maxSamplerAnisotropy                     = 16,
   1291       .maxViewports                             = MAX_VIEWPORTS,
   1292       .maxViewportDimensions                    = { (1 << 14), (1 << 14) },
   1293       .viewportBoundsRange                      = { INT16_MIN, INT16_MAX },
   1294       .viewportSubPixelBits                     = 13, /* We take a float? */
   1295       .minMemoryMapAlignment                    = 4096, /* A page */
   1296       .minTexelBufferOffsetAlignment            = 1,
   1297       /* We need 16 for UBO block reads to work and 32 for push UBOs */
   1298       .minUniformBufferOffsetAlignment          = 32,
   1299       .minStorageBufferOffsetAlignment          = 4,
   1300       .minTexelOffset                           = -8,
   1301       .maxTexelOffset                           = 7,
   1302       .minTexelGatherOffset                     = -32,
   1303       .maxTexelGatherOffset                     = 31,
   1304       .minInterpolationOffset                   = -0.5,
   1305       .maxInterpolationOffset                   = 0.4375,
   1306       .subPixelInterpolationOffsetBits          = 4,
   1307       .maxFramebufferWidth                      = (1 << 14),
   1308       .maxFramebufferHeight                     = (1 << 14),
   1309       .maxFramebufferLayers                     = (1 << 11),
   1310       .framebufferColorSampleCounts             = sample_counts,
   1311       .framebufferDepthSampleCounts             = sample_counts,
   1312       .framebufferStencilSampleCounts           = sample_counts,
   1313       .framebufferNoAttachmentsSampleCounts     = sample_counts,
   1314       .maxColorAttachments                      = MAX_RTS,
   1315       .sampledImageColorSampleCounts            = sample_counts,
   1316       .sampledImageIntegerSampleCounts          = VK_SAMPLE_COUNT_1_BIT,
   1317       .sampledImageDepthSampleCounts            = sample_counts,
   1318       .sampledImageStencilSampleCounts          = sample_counts,
   1319       .storageImageSampleCounts                 = VK_SAMPLE_COUNT_1_BIT,
   1320       .maxSampleMaskWords                       = 1,
   1321       .timestampComputeAndGraphics              = true,
   1322       .timestampPeriod                          = 1000000000.0 / devinfo->timestamp_frequency,
   1323       .maxClipDistances                         = 8,
   1324       .maxCullDistances                         = 8,
   1325       .maxCombinedClipAndCullDistances          = 8,
   1326       .discreteQueuePriorities                  = 2,
   1327       .pointSizeRange                           = { 0.125, 255.875 },
   1328       .lineWidthRange                           = { 0.0, 7.9921875 },
   1329       .pointSizeGranularity                     = (1.0 / 8.0),
   1330       .lineWidthGranularity                     = (1.0 / 128.0),
   1331       .strictLines                              = false, /* FINISHME */
   1332       .standardSampleLocations                  = true,
   1333       .optimalBufferCopyOffsetAlignment         = 128,
   1334       .optimalBufferCopyRowPitchAlignment       = 128,
   1335       .nonCoherentAtomSize                      = 64,
   1336    };
   1337 
   1338    *pProperties = (VkPhysicalDeviceProperties) {
   1339       .apiVersion = anv_physical_device_api_version(pdevice),
   1340       .driverVersion = vk_get_driver_version(),
   1341       .vendorID = 0x8086,
   1342       .deviceID = pdevice->chipset_id,
   1343       .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
   1344       .limits = limits,
   1345       .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
   1346    };
   1347 
   1348    snprintf(pProperties->deviceName, sizeof(pProperties->deviceName),
   1349             "%s", pdevice->name);
   1350    memcpy(pProperties->pipelineCacheUUID,
   1351           pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
   1352 }
   1353 
   1354 void anv_GetPhysicalDeviceProperties2(
   1355     VkPhysicalDevice                            physicalDevice,
   1356     VkPhysicalDeviceProperties2*                pProperties)
   1357 {
   1358    ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
   1359 
   1360    anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
   1361 
   1362    vk_foreach_struct(ext, pProperties->pNext) {
   1363       switch (ext->sType) {
   1364       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR: {
   1365          VkPhysicalDeviceDepthStencilResolvePropertiesKHR *props =
   1366             (VkPhysicalDeviceDepthStencilResolvePropertiesKHR *)ext;
   1367 
   1368          /* We support all of the depth resolve modes */
   1369          props->supportedDepthResolveModes =
   1370             VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR |
   1371             VK_RESOLVE_MODE_AVERAGE_BIT_KHR |
   1372             VK_RESOLVE_MODE_MIN_BIT_KHR |
   1373             VK_RESOLVE_MODE_MAX_BIT_KHR;
   1374 
   1375          /* Average doesn't make sense for stencil so we don't support that */
   1376          props->supportedStencilResolveModes =
   1377             VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR;
   1378          if (pdevice->info.gen >= 8) {
   1379             /* The advanced stencil resolve modes currently require stencil
   1380              * sampling be supported by the hardware.
   1381              */
   1382             props->supportedStencilResolveModes |=
   1383                VK_RESOLVE_MODE_MIN_BIT_KHR |
   1384                VK_RESOLVE_MODE_MAX_BIT_KHR;
   1385          }
   1386 
   1387          props->independentResolveNone = true;
   1388          props->independentResolve = true;
   1389          break;
   1390       }
   1391 
   1392       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT: {
   1393          VkPhysicalDeviceDescriptorIndexingPropertiesEXT *props =
   1394             (VkPhysicalDeviceDescriptorIndexingPropertiesEXT *)ext;
   1395 
   1396          /* It's a bit hard to exactly map our implementation to the limits
   1397           * described here.  The bindless surface handle in the extended
   1398           * message descriptors is 20 bits and it's an index into the table of
   1399           * RENDER_SURFACE_STATE structs that starts at bindless surface base
   1400           * address.  Given that most things consume two surface states per
   1401           * view (general/sampled for textures and write-only/read-write for
   1402           * images), we claim 2^19 things.
   1403           *
   1404           * For SSBOs, we just use A64 messages so there is no real limit
   1405           * there beyond the limit on the total size of a descriptor set.
   1406           */
   1407          const unsigned max_bindless_views = 1 << 19;
   1408 
   1409          props->maxUpdateAfterBindDescriptorsInAllPools = max_bindless_views;
   1410          props->shaderUniformBufferArrayNonUniformIndexingNative = false;
   1411          props->shaderSampledImageArrayNonUniformIndexingNative = false;
   1412          props->shaderStorageBufferArrayNonUniformIndexingNative = true;
   1413          props->shaderStorageImageArrayNonUniformIndexingNative = false;
   1414          props->shaderInputAttachmentArrayNonUniformIndexingNative = false;
   1415          props->robustBufferAccessUpdateAfterBind = true;
   1416          props->quadDivergentImplicitLod = false;
   1417          props->maxPerStageDescriptorUpdateAfterBindSamplers = max_bindless_views;
   1418          props->maxPerStageDescriptorUpdateAfterBindUniformBuffers = MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS;
   1419          props->maxPerStageDescriptorUpdateAfterBindStorageBuffers = UINT32_MAX;
   1420          props->maxPerStageDescriptorUpdateAfterBindSampledImages = max_bindless_views;
   1421          props->maxPerStageDescriptorUpdateAfterBindStorageImages = max_bindless_views;
   1422          props->maxPerStageDescriptorUpdateAfterBindInputAttachments = MAX_PER_STAGE_DESCRIPTOR_INPUT_ATTACHMENTS;
   1423          props->maxPerStageUpdateAfterBindResources = UINT32_MAX;
   1424          props->maxDescriptorSetUpdateAfterBindSamplers = max_bindless_views;
   1425          props->maxDescriptorSetUpdateAfterBindUniformBuffers = 6 * MAX_PER_STAGE_DESCRIPTOR_UNIFORM_BUFFERS;
   1426          props->maxDescriptorSetUpdateAfterBindUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2;
   1427          props->maxDescriptorSetUpdateAfterBindStorageBuffers = UINT32_MAX;
   1428          props->maxDescriptorSetUpdateAfterBindStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2;
   1429          props->maxDescriptorSetUpdateAfterBindSampledImages = max_bindless_views;
   1430          props->maxDescriptorSetUpdateAfterBindStorageImages = max_bindless_views;
   1431          props->maxDescriptorSetUpdateAfterBindInputAttachments = MAX_DESCRIPTOR_SET_INPUT_ATTACHMENTS;
   1432          break;
   1433       }
   1434 
   1435       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
   1436          VkPhysicalDeviceDriverPropertiesKHR *driver_props =
   1437             (VkPhysicalDeviceDriverPropertiesKHR *) ext;
   1438 
   1439          driver_props->driverID = VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR;
   1440          util_snprintf(driver_props->driverName, VK_MAX_DRIVER_NAME_SIZE_KHR,
   1441                 "Intel open-source Mesa driver");
   1442 
   1443          util_snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
   1444                 "Mesa " PACKAGE_VERSION MESA_GIT_SHA1);
   1445 
   1446          driver_props->conformanceVersion = (VkConformanceVersionKHR) {
   1447             .major = 1,
   1448             .minor = 1,
   1449             .subminor = 2,
   1450             .patch = 0,
   1451          };
   1452          break;
   1453       }
   1454 
   1455       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT: {
   1456          VkPhysicalDeviceExternalMemoryHostPropertiesEXT *props =
   1457             (VkPhysicalDeviceExternalMemoryHostPropertiesEXT *) ext;
   1458          /* Userptr needs page aligned memory. */
   1459          props->minImportedHostPointerAlignment = 4096;
   1460          break;
   1461       }
   1462 
   1463       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
   1464          VkPhysicalDeviceIDProperties *id_props =
   1465             (VkPhysicalDeviceIDProperties *)ext;
   1466          memcpy(id_props->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
   1467          memcpy(id_props->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
   1468          /* The LUID is for Windows. */
   1469          id_props->deviceLUIDValid = false;
   1470          break;
   1471       }
   1472 
   1473       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INLINE_UNIFORM_BLOCK_PROPERTIES_EXT: {
   1474          VkPhysicalDeviceInlineUniformBlockPropertiesEXT *props =
   1475             (VkPhysicalDeviceInlineUniformBlockPropertiesEXT *)ext;
   1476          props->maxInlineUniformBlockSize = MAX_INLINE_UNIFORM_BLOCK_SIZE;
   1477          props->maxPerStageDescriptorInlineUniformBlocks =
   1478             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
   1479          props->maxPerStageDescriptorUpdateAfterBindInlineUniformBlocks =
   1480             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
   1481          props->maxDescriptorSetInlineUniformBlocks =
   1482             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
   1483          props->maxDescriptorSetUpdateAfterBindInlineUniformBlocks =
   1484             MAX_INLINE_UNIFORM_BLOCK_DESCRIPTORS;
   1485          break;
   1486       }
   1487 
   1488       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
   1489          VkPhysicalDeviceMaintenance3Properties *props =
   1490             (VkPhysicalDeviceMaintenance3Properties *)ext;
   1491          /* This value doesn't matter for us today as our per-stage
   1492           * descriptors are the real limit.
   1493           */
   1494          props->maxPerSetDescriptors = 1024;
   1495          props->maxMemoryAllocationSize = MAX_MEMORY_ALLOCATION_SIZE;
   1496          break;
   1497       }
   1498 
   1499       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
   1500          VkPhysicalDeviceMultiviewProperties *properties =
   1501             (VkPhysicalDeviceMultiviewProperties *)ext;
   1502          properties->maxMultiviewViewCount = 16;
   1503          properties->maxMultiviewInstanceIndex = UINT32_MAX / 16;
   1504          break;
   1505       }
   1506 
   1507       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
   1508          VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
   1509             (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
   1510          properties->pciDomain = pdevice->pci_info.domain;
   1511          properties->pciBus = pdevice->pci_info.bus;
   1512          properties->pciDevice = pdevice->pci_info.device;
   1513          properties->pciFunction = pdevice->pci_info.function;
   1514          break;
   1515       }
   1516 
   1517       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
   1518          VkPhysicalDevicePointClippingProperties *properties =
   1519             (VkPhysicalDevicePointClippingProperties *) ext;
   1520          properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
   1521          anv_finishme("Implement pop-free point clipping");
   1522          break;
   1523       }
   1524 
   1525       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
   1526          VkPhysicalDeviceProtectedMemoryProperties *props =
   1527             (VkPhysicalDeviceProtectedMemoryProperties *)ext;
   1528          props->protectedNoFault = false;
   1529          break;
   1530       }
   1531 
   1532       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
   1533          VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
   1534             (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
   1535 
   1536          properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
   1537          break;
   1538       }
   1539 
   1540       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
   1541          VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
   1542             (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
   1543          properties->filterMinmaxImageComponentMapping = pdevice->info.gen >= 9;
   1544          properties->filterMinmaxSingleComponentFormats = true;
   1545          break;
   1546       }
   1547 
   1548       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
   1549          VkPhysicalDeviceSubgroupProperties *properties = (void *)ext;
   1550 
   1551          properties->subgroupSize = BRW_SUBGROUP_SIZE;
   1552 
   1553          VkShaderStageFlags scalar_stages = 0;
   1554          for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
   1555             if (pdevice->compiler->scalar_stage[stage])
   1556                scalar_stages |= mesa_to_vk_shader_stage(stage);
   1557          }
   1558          properties->supportedStages = scalar_stages;
   1559 
   1560          properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
   1561                                            VK_SUBGROUP_FEATURE_VOTE_BIT |
   1562                                            VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
   1563                                            VK_SUBGROUP_FEATURE_BALLOT_BIT |
   1564                                            VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
   1565                                            VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT |
   1566                                            VK_SUBGROUP_FEATURE_CLUSTERED_BIT |
   1567                                            VK_SUBGROUP_FEATURE_QUAD_BIT;
   1568          properties->quadOperationsInAllStages = true;
   1569          break;
   1570       }
   1571 
   1572       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT: {
   1573          VkPhysicalDeviceTransformFeedbackPropertiesEXT *props =
   1574             (VkPhysicalDeviceTransformFeedbackPropertiesEXT *)ext;
   1575 
   1576          props->maxTransformFeedbackStreams = MAX_XFB_STREAMS;
   1577          props->maxTransformFeedbackBuffers = MAX_XFB_BUFFERS;
   1578          props->maxTransformFeedbackBufferSize = (1ull << 32);
   1579          props->maxTransformFeedbackStreamDataSize = 128 * 4;
   1580          props->maxTransformFeedbackBufferDataSize = 128 * 4;
   1581          props->maxTransformFeedbackBufferDataStride = 2048;
   1582          props->transformFeedbackQueries = true;
   1583          props->transformFeedbackStreamsLinesTriangles = false;
   1584          props->transformFeedbackRasterizationStreamSelect = false;
   1585          props->transformFeedbackDraw = true;
   1586          break;
   1587       }
   1588 
   1589       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
   1590          VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
   1591             (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
   1592          /* We have to restrict this a bit for multiview */
   1593          props->maxVertexAttribDivisor = UINT32_MAX / 16;
   1594          break;
   1595       }
   1596 
   1597       default:
   1598          anv_debug_ignored_stype(ext->sType);
   1599          break;
   1600       }
   1601    }
   1602 }
   1603 
   1604 /* We support exactly one queue family. */
   1605 static const VkQueueFamilyProperties
   1606 anv_queue_family_properties = {
   1607    .queueFlags = VK_QUEUE_GRAPHICS_BIT |
   1608                  VK_QUEUE_COMPUTE_BIT |
   1609                  VK_QUEUE_TRANSFER_BIT,
   1610    .queueCount = 1,
   1611    .timestampValidBits = 36, /* XXX: Real value here */
   1612    .minImageTransferGranularity = { 1, 1, 1 },
   1613 };
   1614 
   1615 void anv_GetPhysicalDeviceQueueFamilyProperties(
   1616     VkPhysicalDevice                            physicalDevice,
   1617     uint32_t*                                   pCount,
   1618     VkQueueFamilyProperties*                    pQueueFamilyProperties)
   1619 {
   1620    VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
   1621 
   1622    vk_outarray_append(&out, p) {
   1623       *p = anv_queue_family_properties;
   1624    }
   1625 }
   1626 
   1627 void anv_GetPhysicalDeviceQueueFamilyProperties2(
   1628     VkPhysicalDevice                            physicalDevice,
   1629     uint32_t*                                   pQueueFamilyPropertyCount,
   1630     VkQueueFamilyProperties2*                   pQueueFamilyProperties)
   1631 {
   1632 
   1633    VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
   1634 
   1635    vk_outarray_append(&out, p) {
   1636       p->queueFamilyProperties = anv_queue_family_properties;
   1637 
   1638       vk_foreach_struct(s, p->pNext) {
   1639          anv_debug_ignored_stype(s->sType);
   1640       }
   1641    }
   1642 }
   1643 
   1644 void anv_GetPhysicalDeviceMemoryProperties(
   1645     VkPhysicalDevice                            physicalDevice,
   1646     VkPhysicalDeviceMemoryProperties*           pMemoryProperties)
   1647 {
   1648    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
   1649 
   1650    pMemoryProperties->memoryTypeCount = physical_device->memory.type_count;
   1651    for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
   1652       pMemoryProperties->memoryTypes[i] = (VkMemoryType) {
   1653          .propertyFlags = physical_device->memory.types[i].propertyFlags,
   1654          .heapIndex     = physical_device->memory.types[i].heapIndex,
   1655       };
   1656    }
   1657 
   1658    pMemoryProperties->memoryHeapCount = physical_device->memory.heap_count;
   1659    for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
   1660       pMemoryProperties->memoryHeaps[i] = (VkMemoryHeap) {
   1661          .size    = physical_device->memory.heaps[i].size,
   1662          .flags   = physical_device->memory.heaps[i].flags,
   1663       };
   1664    }
   1665 }
   1666 
   1667 static void
   1668 anv_get_memory_budget(VkPhysicalDevice physicalDevice,
   1669                       VkPhysicalDeviceMemoryBudgetPropertiesEXT *memoryBudget)
   1670 {
   1671    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
   1672    uint64_t sys_available = get_available_system_memory();
   1673    assert(sys_available > 0);
   1674 
   1675    VkDeviceSize total_heaps_size = 0;
   1676    for (size_t i = 0; i < device->memory.heap_count; i++)
   1677          total_heaps_size += device->memory.heaps[i].size;
   1678 
   1679    for (size_t i = 0; i < device->memory.heap_count; i++) {
   1680       VkDeviceSize heap_size = device->memory.heaps[i].size;
   1681       VkDeviceSize heap_used = device->memory.heaps[i].used;
   1682       VkDeviceSize heap_budget;
   1683 
   1684       double heap_proportion = (double) heap_size / total_heaps_size;
   1685       VkDeviceSize sys_available_prop = sys_available * heap_proportion;
   1686 
   1687       /*
   1688        * Let's not incite the app to starve the system: report at most 90% of
   1689        * available system memory.
   1690        */
   1691       uint64_t heap_available = sys_available_prop * 9 / 10;
   1692       heap_budget = MIN2(heap_size, heap_used + heap_available);
   1693 
   1694       /*
   1695        * Round down to the nearest MB
   1696        */
   1697       heap_budget &= ~((1ull << 20) - 1);
   1698 
   1699       /*
   1700        * The heapBudget value must be non-zero for array elements less than
   1701        * VkPhysicalDeviceMemoryProperties::memoryHeapCount. The heapBudget
   1702        * value must be less than or equal to VkMemoryHeap::size for each heap.
   1703        */
   1704       assert(0 < heap_budget && heap_budget <= heap_size);
   1705 
   1706       memoryBudget->heapUsage[i] = heap_used;
   1707       memoryBudget->heapBudget[i] = heap_budget;
   1708    }
   1709 
   1710    /* The heapBudget and heapUsage values must be zero for array elements
   1711     * greater than or equal to VkPhysicalDeviceMemoryProperties::memoryHeapCount
   1712     */
   1713    for (uint32_t i = device->memory.heap_count; i < VK_MAX_MEMORY_HEAPS; i++) {
   1714       memoryBudget->heapBudget[i] = 0;
   1715       memoryBudget->heapUsage[i] = 0;
   1716    }
   1717 }
   1718 
   1719 void anv_GetPhysicalDeviceMemoryProperties2(
   1720     VkPhysicalDevice                            physicalDevice,
   1721     VkPhysicalDeviceMemoryProperties2*          pMemoryProperties)
   1722 {
   1723    anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
   1724                                          &pMemoryProperties->memoryProperties);
   1725 
   1726    vk_foreach_struct(ext, pMemoryProperties->pNext) {
   1727       switch (ext->sType) {
   1728       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT:
   1729          anv_get_memory_budget(physicalDevice, (void*)ext);
   1730          break;
   1731       default:
   1732          anv_debug_ignored_stype(ext->sType);
   1733          break;
   1734       }
   1735    }
   1736 }
   1737 
   1738 void
   1739 anv_GetDeviceGroupPeerMemoryFeatures(
   1740     VkDevice                                    device,
   1741     uint32_t                                    heapIndex,
   1742     uint32_t                                    localDeviceIndex,
   1743     uint32_t                                    remoteDeviceIndex,
   1744     VkPeerMemoryFeatureFlags*                   pPeerMemoryFeatures)
   1745 {
   1746    assert(localDeviceIndex == 0 && remoteDeviceIndex == 0);
   1747    *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
   1748                           VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
   1749                           VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
   1750                           VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
   1751 }
   1752 
   1753 PFN_vkVoidFunction anv_GetInstanceProcAddr(
   1754     VkInstance                                  _instance,
   1755     const char*                                 pName)
   1756 {
   1757    ANV_FROM_HANDLE(anv_instance, instance, _instance);
   1758 
   1759    /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
   1760     * when we have to return valid function pointers, NULL, or it's left
   1761     * undefined.  See the table for exact details.
   1762     */
   1763    if (pName == NULL)
   1764       return NULL;
   1765 
   1766 #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
   1767    if (strcmp(pName, "vk" #entrypoint) == 0) \
   1768       return (PFN_vkVoidFunction)anv_##entrypoint
   1769 
   1770    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties);
   1771    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties);
   1772    LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion);
   1773    LOOKUP_ANV_ENTRYPOINT(CreateInstance);
   1774 
   1775 #undef LOOKUP_ANV_ENTRYPOINT
   1776 
   1777    if (instance == NULL)
   1778       return NULL;
   1779 
   1780    int idx = anv_get_instance_entrypoint_index(pName);
   1781    if (idx >= 0)
   1782       return instance->dispatch.entrypoints[idx];
   1783 
   1784    idx = anv_get_device_entrypoint_index(pName);
   1785    if (idx >= 0)
   1786       return instance->device_dispatch.entrypoints[idx];
   1787 
   1788    return NULL;
   1789 }
   1790 
   1791 /* With version 1+ of the loader interface the ICD should expose
   1792  * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
   1793  */
   1794 PUBLIC
   1795 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
   1796     VkInstance                                  instance,
   1797     const char*                                 pName);
   1798 
   1799 PUBLIC
   1800 VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
   1801     VkInstance                                  instance,
   1802     const char*                                 pName)
   1803 {
   1804    return anv_GetInstanceProcAddr(instance, pName);
   1805 }
   1806 
   1807 PFN_vkVoidFunction anv_GetDeviceProcAddr(
   1808     VkDevice                                    _device,
   1809     const char*                                 pName)
   1810 {
   1811    ANV_FROM_HANDLE(anv_device, device, _device);
   1812 
   1813    if (!device || !pName)
   1814       return NULL;
   1815 
   1816    int idx = anv_get_device_entrypoint_index(pName);
   1817    if (idx < 0)
   1818       return NULL;
   1819 
   1820    return device->dispatch.entrypoints[idx];
   1821 }
   1822 
   1823 VkResult
   1824 anv_CreateDebugReportCallbackEXT(VkInstance _instance,
   1825                                  const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
   1826                                  const VkAllocationCallbacks* pAllocator,
   1827                                  VkDebugReportCallbackEXT* pCallback)
   1828 {
   1829    ANV_FROM_HANDLE(anv_instance, instance, _instance);
   1830    return vk_create_debug_report_callback(&instance->debug_report_callbacks,
   1831                                           pCreateInfo, pAllocator, &instance->alloc,
   1832                                           pCallback);
   1833 }
   1834 
   1835 void
   1836 anv_DestroyDebugReportCallbackEXT(VkInstance _instance,
   1837                                   VkDebugReportCallbackEXT _callback,
   1838                                   const VkAllocationCallbacks* pAllocator)
   1839 {
   1840    ANV_FROM_HANDLE(anv_instance, instance, _instance);
   1841    vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
   1842                                     _callback, pAllocator, &instance->alloc);
   1843 }
   1844 
   1845 void
   1846 anv_DebugReportMessageEXT(VkInstance _instance,
   1847                           VkDebugReportFlagsEXT flags,
   1848                           VkDebugReportObjectTypeEXT objectType,
   1849                           uint64_t object,
   1850                           size_t location,
   1851                           int32_t messageCode,
   1852                           const char* pLayerPrefix,
   1853                           const char* pMessage)
   1854 {
   1855    ANV_FROM_HANDLE(anv_instance, instance, _instance);
   1856    vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
   1857                    object, location, messageCode, pLayerPrefix, pMessage);
   1858 }
   1859 
   1860 static void
   1861 anv_queue_init(struct anv_device *device, struct anv_queue *queue)
   1862 {
   1863    queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
   1864    queue->device = device;
   1865    queue->flags = 0;
   1866 }
   1867 
   1868 static void
   1869 anv_queue_finish(struct anv_queue *queue)
   1870 {
   1871 }
   1872 
   1873 static struct anv_state
   1874 anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
   1875 {
   1876    struct anv_state state;
   1877 
   1878    state = anv_state_pool_alloc(pool, size, align);
   1879    memcpy(state.map, p, size);
   1880 
   1881    return state;
   1882 }
   1883 
   1884 struct gen8_border_color {
   1885    union {
   1886       float float32[4];
   1887       uint32_t uint32[4];
   1888    };
   1889    /* Pad out to 64 bytes */
   1890    uint32_t _pad[12];
   1891 };
   1892 
   1893 static void
   1894 anv_device_init_border_colors(struct anv_device *device)
   1895 {
   1896    static const struct gen8_border_color border_colors[] = {
   1897       [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] =  { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
   1898       [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] =       { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
   1899       [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] =       { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
   1900       [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] =    { .uint32 = { 0, 0, 0, 0 } },
   1901       [VK_BORDER_COLOR_INT_OPAQUE_BLACK] =         { .uint32 = { 0, 0, 0, 1 } },
   1902       [VK_BORDER_COLOR_INT_OPAQUE_WHITE] =         { .uint32 = { 1, 1, 1, 1 } },
   1903    };
   1904 
   1905    device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
   1906                                                     sizeof(border_colors), 64,
   1907                                                     border_colors);
   1908 }
   1909 
   1910 static void
   1911 anv_device_init_trivial_batch(struct anv_device *device)
   1912 {
   1913    anv_bo_init_new(&device->trivial_batch_bo, device, 4096);
   1914 
   1915    if (device->instance->physicalDevice.has_exec_async)
   1916       device->trivial_batch_bo.flags |= EXEC_OBJECT_ASYNC;
   1917 
   1918    if (device->instance->physicalDevice.use_softpin)
   1919       device->trivial_batch_bo.flags |= EXEC_OBJECT_PINNED;
   1920 
   1921    anv_vma_alloc(device, &device->trivial_batch_bo);
   1922 
   1923    void *map = anv_gem_mmap(device, device->trivial_batch_bo.gem_handle,
   1924                             0, 4096, 0);
   1925 
   1926    struct anv_batch batch = {
   1927       .start = map,
   1928       .next = map,
   1929       .end = map + 4096,
   1930    };
   1931 
   1932    anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
   1933    anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
   1934 
   1935    if (!device->info.has_llc)
   1936       gen_clflush_range(map, batch.next - map);
   1937 
   1938    anv_gem_munmap(map, device->trivial_batch_bo.size);
   1939 }
   1940 
   1941 VkResult anv_EnumerateDeviceExtensionProperties(
   1942     VkPhysicalDevice                            physicalDevice,
   1943     const char*                                 pLayerName,
   1944     uint32_t*                                   pPropertyCount,
   1945     VkExtensionProperties*                      pProperties)
   1946 {
   1947    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
   1948    VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
   1949 
   1950    for (int i = 0; i < ANV_DEVICE_EXTENSION_COUNT; i++) {
   1951       if (device->supported_extensions.extensions[i]) {
   1952          vk_outarray_append(&out, prop) {
   1953             *prop = anv_device_extensions[i];
   1954          }
   1955       }
   1956    }
   1957 
   1958    return vk_outarray_status(&out);
   1959 }
   1960 
   1961 static void
   1962 anv_device_init_dispatch(struct anv_device *device)
   1963 {
   1964    const struct anv_device_dispatch_table *genX_table;
   1965    switch (device->info.gen) {
   1966    case 11:
   1967       genX_table = &gen11_device_dispatch_table;
   1968       break;
   1969    case 10:
   1970       genX_table = &gen10_device_dispatch_table;
   1971       break;
   1972    case 9:
   1973       genX_table = &gen9_device_dispatch_table;
   1974       break;
   1975    case 8:
   1976       genX_table = &gen8_device_dispatch_table;
   1977       break;
   1978    case 7:
   1979       if (device->info.is_haswell)
   1980          genX_table = &gen75_device_dispatch_table;
   1981       else
   1982          genX_table = &gen7_device_dispatch_table;
   1983       break;
   1984    default:
   1985       unreachable("unsupported gen\n");
   1986    }
   1987 
   1988    for (unsigned i = 0; i < ARRAY_SIZE(device->dispatch.entrypoints); i++) {
   1989       /* Vulkan requires that entrypoints for extensions which have not been
   1990        * enabled must not be advertised.
   1991        */
   1992       if (!anv_device_entrypoint_is_enabled(i, device->instance->app_info.api_version,
   1993                                             &device->instance->enabled_extensions,
   1994                                             &device->enabled_extensions)) {
   1995          device->dispatch.entrypoints[i] = NULL;
   1996       } else if (genX_table->entrypoints[i]) {
   1997          device->dispatch.entrypoints[i] = genX_table->entrypoints[i];
   1998       } else {
   1999          device->dispatch.entrypoints[i] =
   2000             anv_device_dispatch_table.entrypoints[i];
   2001       }
   2002    }
   2003 }
   2004 
   2005 static int
   2006 vk_priority_to_gen(int priority)
   2007 {
   2008    switch (priority) {
   2009    case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
   2010       return GEN_CONTEXT_LOW_PRIORITY;
   2011    case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
   2012       return GEN_CONTEXT_MEDIUM_PRIORITY;
   2013    case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
   2014       return GEN_CONTEXT_HIGH_PRIORITY;
   2015    case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
   2016       return GEN_CONTEXT_REALTIME_PRIORITY;
   2017    default:
   2018       unreachable("Invalid priority");
   2019    }
   2020 }
   2021 
   2022 static void
   2023 anv_device_init_hiz_clear_value_bo(struct anv_device *device)
   2024 {
   2025    anv_bo_init_new(&device->hiz_clear_bo, device, 4096);
   2026 
   2027    if (device->instance->physicalDevice.has_exec_async)
   2028       device->hiz_clear_bo.flags |= EXEC_OBJECT_ASYNC;
   2029 
   2030    if (device->instance->physicalDevice.use_softpin)
   2031       device->hiz_clear_bo.flags |= EXEC_OBJECT_PINNED;
   2032 
   2033    anv_vma_alloc(device, &device->hiz_clear_bo);
   2034 
   2035    uint32_t *map = anv_gem_mmap(device, device->hiz_clear_bo.gem_handle,
   2036                                 0, 4096, 0);
   2037 
   2038    union isl_color_value hiz_clear = { .u32 = { 0, } };
   2039    hiz_clear.f32[0] = ANV_HZ_FC_VAL;
   2040 
   2041    memcpy(map, hiz_clear.u32, sizeof(hiz_clear.u32));
   2042    anv_gem_munmap(map, device->hiz_clear_bo.size);
   2043 }
   2044 
   2045 static bool
   2046 get_bo_from_pool(struct gen_batch_decode_bo *ret,
   2047                  struct anv_block_pool *pool,
   2048                  uint64_t address)
   2049 {
   2050    for (uint32_t i = 0; i < pool->nbos; i++) {
   2051       uint64_t bo_address = pool->bos[i].offset & (~0ull >> 16);
   2052       uint32_t bo_size = pool->bos[i].size;
   2053       if (address >= bo_address && address < (bo_address + bo_size)) {
   2054          *ret = (struct gen_batch_decode_bo) {
   2055             .addr = bo_address,
   2056             .size = bo_size,
   2057             .map = pool->bos[i].map,
   2058          };
   2059          return true;
   2060       }
   2061    }
   2062    return false;
   2063 }
   2064 
   2065 /* Finding a buffer for batch decoding */
   2066 static struct gen_batch_decode_bo
   2067 decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
   2068 {
   2069    struct anv_device *device = v_batch;
   2070    struct gen_batch_decode_bo ret_bo = {};
   2071 
   2072    assert(ppgtt);
   2073 
   2074    if (get_bo_from_pool(&ret_bo, &device->dynamic_state_pool.block_pool, address))
   2075       return ret_bo;
   2076    if (get_bo_from_pool(&ret_bo, &device->instruction_state_pool.block_pool, address))
   2077       return ret_bo;
   2078    if (get_bo_from_pool(&ret_bo, &device->binding_table_pool.block_pool, address))
   2079       return ret_bo;
   2080    if (get_bo_from_pool(&ret_bo, &device->surface_state_pool.block_pool, address))
   2081       return ret_bo;
   2082 
   2083    if (!device->cmd_buffer_being_decoded)
   2084       return (struct gen_batch_decode_bo) { };
   2085 
   2086    struct anv_batch_bo **bo;
   2087 
   2088    u_vector_foreach(bo, &device->cmd_buffer_being_decoded->seen_bbos) {
   2089       /* The decoder zeroes out the top 16 bits, so we need to as well */
   2090       uint64_t bo_address = (*bo)->bo.offset & (~0ull >> 16);
   2091 
   2092       if (address >= bo_address && address < bo_address + (*bo)->bo.size) {
   2093          return (struct gen_batch_decode_bo) {
   2094             .addr = bo_address,
   2095             .size = (*bo)->bo.size,
   2096             .map = (*bo)->bo.map,
   2097          };
   2098       }
   2099    }
   2100 
   2101    return (struct gen_batch_decode_bo) { };
   2102 }
   2103 
   2104 VkResult anv_CreateDevice(
   2105     VkPhysicalDevice                            physicalDevice,
   2106     const VkDeviceCreateInfo*                   pCreateInfo,
   2107     const VkAllocationCallbacks*                pAllocator,
   2108     VkDevice*                                   pDevice)
   2109 {
   2110    ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
   2111    VkResult result;
   2112    struct anv_device *device;
   2113 
   2114    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
   2115 
   2116    struct anv_device_extension_table enabled_extensions = { };
   2117    for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
   2118       int idx;
   2119       for (idx = 0; idx < ANV_DEVICE_EXTENSION_COUNT; idx++) {
   2120          if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
   2121                     anv_device_extensions[idx].extensionName) == 0)
   2122             break;
   2123       }
   2124 
   2125       if (idx >= ANV_DEVICE_EXTENSION_COUNT)
   2126          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
   2127 
   2128       if (!physical_device->supported_extensions.extensions[idx])
   2129          return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
   2130 
   2131       enabled_extensions.extensions[idx] = true;
   2132    }
   2133 
   2134    /* Check enabled features */
   2135    if (pCreateInfo->pEnabledFeatures) {
   2136       VkPhysicalDeviceFeatures supported_features;
   2137       anv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
   2138       VkBool32 *supported_feature = (VkBool32 *)&supported_features;
   2139       VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
   2140       unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
   2141       for (uint32_t i = 0; i < num_features; i++) {
   2142          if (enabled_feature[i] && !supported_feature[i])
   2143             return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
   2144       }
   2145    }
   2146 
   2147    /* Check requested queues and fail if we are requested to create any
   2148     * queues with flags we don't support.
   2149     */
   2150    assert(pCreateInfo->queueCreateInfoCount > 0);
   2151    for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
   2152       if (pCreateInfo->pQueueCreateInfos[i].flags != 0)
   2153          return vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2154    }
   2155 
   2156    /* Check if client specified queue priority. */
   2157    const VkDeviceQueueGlobalPriorityCreateInfoEXT *queue_priority =
   2158       vk_find_struct_const(pCreateInfo->pQueueCreateInfos[0].pNext,
   2159                            DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
   2160 
   2161    VkQueueGlobalPriorityEXT priority =
   2162       queue_priority ? queue_priority->globalPriority :
   2163          VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
   2164 
   2165    device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
   2166                        sizeof(*device), 8,
   2167                        VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
   2168    if (!device)
   2169       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
   2170 
   2171    if (INTEL_DEBUG & DEBUG_BATCH) {
   2172       const unsigned decode_flags =
   2173          GEN_BATCH_DECODE_FULL |
   2174          ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
   2175          GEN_BATCH_DECODE_OFFSETS |
   2176          GEN_BATCH_DECODE_FLOATS;
   2177 
   2178       gen_batch_decode_ctx_init(&device->decoder_ctx,
   2179                                 &physical_device->info,
   2180                                 stderr, decode_flags, NULL,
   2181                                 decode_get_bo, NULL, device);
   2182    }
   2183 
   2184    device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
   2185    device->instance = physical_device->instance;
   2186    device->chipset_id = physical_device->chipset_id;
   2187    device->no_hw = physical_device->no_hw;
   2188    device->_lost = false;
   2189 
   2190    if (pAllocator)
   2191       device->alloc = *pAllocator;
   2192    else
   2193       device->alloc = physical_device->instance->alloc;
   2194 
   2195    /* XXX(chadv): Can we dup() physicalDevice->fd here? */
   2196    device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
   2197    if (device->fd == -1) {
   2198       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2199       goto fail_device;
   2200    }
   2201 
   2202    device->context_id = anv_gem_create_context(device);
   2203    if (device->context_id == -1) {
   2204       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2205       goto fail_fd;
   2206    }
   2207 
   2208    if (physical_device->use_softpin) {
   2209       if (pthread_mutex_init(&device->vma_mutex, NULL) != 0) {
   2210          result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2211          goto fail_fd;
   2212       }
   2213 
   2214       /* keep the page with address zero out of the allocator */
   2215       struct anv_memory_heap *low_heap =
   2216          &physical_device->memory.heaps[physical_device->memory.heap_count - 1];
   2217       util_vma_heap_init(&device->vma_lo, low_heap->vma_start, low_heap->vma_size);
   2218       device->vma_lo_available = low_heap->size;
   2219 
   2220       struct anv_memory_heap *high_heap =
   2221          &physical_device->memory.heaps[0];
   2222       util_vma_heap_init(&device->vma_hi, high_heap->vma_start, high_heap->vma_size);
   2223       device->vma_hi_available = physical_device->memory.heap_count == 1 ? 0 :
   2224          high_heap->size;
   2225    }
   2226 
   2227    list_inithead(&device->memory_objects);
   2228 
   2229    /* As per spec, the driver implementation may deny requests to acquire
   2230     * a priority above the default priority (MEDIUM) if the caller does not
   2231     * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
   2232     * is returned.
   2233     */
   2234    if (physical_device->has_context_priority) {
   2235       int err = anv_gem_set_context_param(device->fd, device->context_id,
   2236                                           I915_CONTEXT_PARAM_PRIORITY,
   2237                                           vk_priority_to_gen(priority));
   2238       if (err != 0 && priority > VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT) {
   2239          result = vk_error(VK_ERROR_NOT_PERMITTED_EXT);
   2240          goto fail_fd;
   2241       }
   2242    }
   2243 
   2244    device->info = physical_device->info;
   2245    device->isl_dev = physical_device->isl_dev;
   2246 
   2247    /* On Broadwell and later, we can use batch chaining to more efficiently
   2248     * implement growing command buffers.  Prior to Haswell, the kernel
   2249     * command parser gets in the way and we have to fall back to growing
   2250     * the batch.
   2251     */
   2252    device->can_chain_batches = device->info.gen >= 8;
   2253 
   2254    device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
   2255       pCreateInfo->pEnabledFeatures->robustBufferAccess;
   2256    device->enabled_extensions = enabled_extensions;
   2257 
   2258    anv_device_init_dispatch(device);
   2259 
   2260    if (pthread_mutex_init(&device->mutex, NULL) != 0) {
   2261       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2262       goto fail_context_id;
   2263    }
   2264 
   2265    pthread_condattr_t condattr;
   2266    if (pthread_condattr_init(&condattr) != 0) {
   2267       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2268       goto fail_mutex;
   2269    }
   2270    if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
   2271       pthread_condattr_destroy(&condattr);
   2272       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2273       goto fail_mutex;
   2274    }
   2275    if (pthread_cond_init(&device->queue_submit, &condattr) != 0) {
   2276       pthread_condattr_destroy(&condattr);
   2277       result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
   2278       goto fail_mutex;
   2279    }
   2280    pthread_condattr_destroy(&condattr);
   2281 
   2282    uint64_t bo_flags =
   2283       (physical_device->supports_48bit_addresses ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0) |
   2284       (physical_device->has_exec_async ? EXEC_OBJECT_ASYNC : 0) |
   2285       (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0) |
   2286       (physical_device->use_softpin ? EXEC_OBJECT_PINNED : 0);
   2287 
   2288    anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags);
   2289 
   2290    result = anv_bo_cache_init(&device->bo_cache);
   2291    if (result != VK_SUCCESS)
   2292       goto fail_batch_bo_pool;
   2293 
   2294    if (!physical_device->use_softpin)
   2295       bo_flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
   2296 
   2297    result = anv_state_pool_init(&device->dynamic_state_pool, device,
   2298                                 DYNAMIC_STATE_POOL_MIN_ADDRESS,
   2299                                 16384,
   2300                                 bo_flags);
   2301    if (result != VK_SUCCESS)
   2302       goto fail_bo_cache;
   2303 
   2304    result = anv_state_pool_init(&device->instruction_state_pool, device,
   2305                                 INSTRUCTION_STATE_POOL_MIN_ADDRESS,
   2306                                 16384,
   2307                                 bo_flags);
   2308    if (result != VK_SUCCESS)
   2309       goto fail_dynamic_state_pool;
   2310 
   2311    result = anv_state_pool_init(&device->surface_state_pool, device,
   2312                                 SURFACE_STATE_POOL_MIN_ADDRESS,
   2313                                 4096,
   2314                                 bo_flags);
   2315    if (result != VK_SUCCESS)
   2316       goto fail_instruction_state_pool;
   2317 
   2318    if (physical_device->use_softpin) {
   2319       result = anv_state_pool_init(&device->binding_table_pool, device,
   2320                                    BINDING_TABLE_POOL_MIN_ADDRESS,
   2321                                    4096,
   2322                                    bo_flags);
   2323       if (result != VK_SUCCESS)
   2324          goto fail_surface_state_pool;
   2325    }
   2326 
   2327    result = anv_bo_init_new(&device->workaround_bo, device, 1024);
   2328    if (result != VK_SUCCESS)
   2329       goto fail_binding_table_pool;
   2330 
   2331    if (physical_device->use_softpin)
   2332       device->workaround_bo.flags |= EXEC_OBJECT_PINNED;
   2333 
   2334    if (!anv_vma_alloc(device, &device->workaround_bo))
   2335       goto fail_workaround_bo;
   2336 
   2337    anv_device_init_trivial_batch(device);
   2338 
   2339    if (device->info.gen >= 10)
   2340       anv_device_init_hiz_clear_value_bo(device);
   2341 
   2342    anv_scratch_pool_init(device, &device->scratch_pool);
   2343 
   2344    anv_queue_init(device, &device->queue);
   2345 
   2346    switch (device->info.gen) {
   2347    case 7:
   2348       if (!device->info.is_haswell)
   2349          result = gen7_init_device_state(device);
   2350       else
   2351          result = gen75_init_device_state(device);
   2352       break;
   2353    case 8:
   2354       result = gen8_init_device_state(device);
   2355       break;
   2356    case 9:
   2357       result = gen9_init_device_state(device);
   2358       break;
   2359    case 10:
   2360       result = gen10_init_device_state(device);
   2361       break;
   2362    case 11:
   2363       result = gen11_init_device_state(device);
   2364       break;
   2365    default:
   2366       /* Shouldn't get here as we don't create physical devices for any other
   2367        * gens. */
   2368       unreachable("unhandled gen");
   2369    }
   2370    if (result != VK_SUCCESS)
   2371       goto fail_workaround_bo;
   2372 
   2373    anv_pipeline_cache_init(&device->default_pipeline_cache, device, true);
   2374 
   2375    anv_device_init_blorp(device);
   2376 
   2377    anv_device_init_border_colors(device);
   2378 
   2379    *pDevice = anv_device_to_handle(device);
   2380 
   2381    return VK_SUCCESS;
   2382 
   2383  fail_workaround_bo:
   2384    anv_queue_finish(&device->queue);
   2385    anv_scratch_pool_finish(device, &device->scratch_pool);
   2386    anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
   2387    anv_gem_close(device, device->workaround_bo.gem_handle);
   2388  fail_binding_table_pool:
   2389    if (physical_device->use_softpin)
   2390       anv_state_pool_finish(&device->binding_table_pool);
   2391  fail_surface_state_pool:
   2392    anv_state_pool_finish(&device->surface_state_pool);
   2393  fail_instruction_state_pool:
   2394    anv_state_pool_finish(&device->instruction_state_pool);
   2395  fail_dynamic_state_pool:
   2396    anv_state_pool_finish(&device->dynamic_state_pool);
   2397  fail_bo_cache:
   2398    anv_bo_cache_finish(&device->bo_cache);
   2399  fail_batch_bo_pool:
   2400    anv_bo_pool_finish(&device->batch_bo_pool);
   2401    pthread_cond_destroy(&device->queue_submit);
   2402  fail_mutex:
   2403    pthread_mutex_destroy(&device->mutex);
   2404  fail_context_id:
   2405    anv_gem_destroy_context(device, device->context_id);
   2406  fail_fd:
   2407    close(device->fd);
   2408  fail_device:
   2409    vk_free(&device->alloc, device);
   2410 
   2411    return result;
   2412 }
   2413 
   2414 void anv_DestroyDevice(
   2415     VkDevice                                    _device,
   2416     const VkAllocationCallbacks*                pAllocator)
   2417 {
   2418    ANV_FROM_HANDLE(anv_device, device, _device);
   2419    struct anv_physical_device *physical_device;
   2420 
   2421    if (!device)
   2422       return;
   2423 
   2424    physical_device = &device->instance->physicalDevice;
   2425 
   2426    anv_device_finish_blorp(device);
   2427 
   2428    anv_pipeline_cache_finish(&device->default_pipeline_cache);
   2429 
   2430    anv_queue_finish(&device->queue);
   2431 
   2432 #ifdef HAVE_VALGRIND
   2433    /* We only need to free these to prevent valgrind errors.  The backing
   2434     * BO will go away in a couple of lines so we don't actually leak.
   2435     */
   2436    anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
   2437 #endif
   2438 
   2439    anv_scratch_pool_finish(device, &device->scratch_pool);
   2440 
   2441    anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
   2442    anv_vma_free(device, &device->workaround_bo);
   2443    anv_gem_close(device, device->workaround_bo.gem_handle);
   2444 
   2445    anv_vma_free(device, &device->trivial_batch_bo);
   2446    anv_gem_close(device, device->trivial_batch_bo.gem_handle);
   2447    if (device->info.gen >= 10)
   2448       anv_gem_close(device, device->hiz_clear_bo.gem_handle);
   2449 
   2450    if (physical_device->use_softpin)
   2451       anv_state_pool_finish(&device->binding_table_pool);
   2452    anv_state_pool_finish(&device->surface_state_pool);
   2453    anv_state_pool_finish(&device->instruction_state_pool);
   2454    anv_state_pool_finish(&device->dynamic_state_pool);
   2455 
   2456    anv_bo_cache_finish(&device->bo_cache);
   2457 
   2458    anv_bo_pool_finish(&device->batch_bo_pool);
   2459 
   2460    pthread_cond_destroy(&device->queue_submit);
   2461    pthread_mutex_destroy(&device->mutex);
   2462 
   2463    anv_gem_destroy_context(device, device->context_id);
   2464 
   2465    if (INTEL_DEBUG & DEBUG_BATCH)
   2466       gen_batch_decode_ctx_finish(&device->decoder_ctx);
   2467 
   2468    close(device->fd);
   2469 
   2470    vk_free(&device->alloc, device);
   2471 }
   2472 
   2473 VkResult anv_EnumerateInstanceLayerProperties(
   2474     uint32_t*                                   pPropertyCount,
   2475     VkLayerProperties*                          pProperties)
   2476 {
   2477    if (pProperties == NULL) {
   2478       *pPropertyCount = 0;
   2479       return VK_SUCCESS;
   2480    }
   2481 
   2482    /* None supported at this time */
   2483    return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
   2484 }
   2485 
   2486 VkResult anv_EnumerateDeviceLayerProperties(
   2487     VkPhysicalDevice                            physicalDevice,
   2488     uint32_t*                                   pPropertyCount,
   2489     VkLayerProperties*                          pProperties)
   2490 {
   2491    if (pProperties == NULL) {
   2492       *pPropertyCount = 0;
   2493       return VK_SUCCESS;
   2494    }
   2495 
   2496    /* None supported at this time */
   2497    return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
   2498 }
   2499 
   2500 void anv_GetDeviceQueue(
   2501     VkDevice                                    _device,
   2502     uint32_t                                    queueNodeIndex,
   2503     uint32_t                                    queueIndex,
   2504     VkQueue*                                    pQueue)
   2505 {
   2506    ANV_FROM_HANDLE(anv_device, device, _device);
   2507 
   2508    assert(queueIndex == 0);
   2509 
   2510    *pQueue = anv_queue_to_handle(&device->queue);
   2511 }
   2512 
   2513 void anv_GetDeviceQueue2(
   2514     VkDevice                                    _device,
   2515     const VkDeviceQueueInfo2*                   pQueueInfo,
   2516     VkQueue*                                    pQueue)
   2517 {
   2518    ANV_FROM_HANDLE(anv_device, device, _device);
   2519 
   2520    assert(pQueueInfo->queueIndex == 0);
   2521 
   2522    if (pQueueInfo->flags == device->queue.flags)
   2523       *pQueue = anv_queue_to_handle(&device->queue);
   2524    else
   2525       *pQueue = NULL;
   2526 }
   2527 
   2528 VkResult
   2529 _anv_device_set_lost(struct anv_device *device,
   2530                      const char *file, int line,
   2531                      const char *msg, ...)
   2532 {
   2533    VkResult err;
   2534    va_list ap;
   2535 
   2536    device->_lost = true;
   2537 
   2538    va_start(ap, msg);
   2539    err = __vk_errorv(device->instance, device,
   2540                      VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
   2541                      VK_ERROR_DEVICE_LOST, file, line, msg, ap);
   2542    va_end(ap);
   2543 
   2544    if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
   2545       abort();
   2546 
   2547    return err;
   2548 }
   2549 
   2550 VkResult
   2551 anv_device_query_status(struct anv_device *device)
   2552 {
   2553    /* This isn't likely as most of the callers of this function already check
   2554     * for it.  However, it doesn't hurt to check and it potentially lets us
   2555     * avoid an ioctl.
   2556     */
   2557    if (anv_device_is_lost(device))
   2558       return VK_ERROR_DEVICE_LOST;
   2559 
   2560    uint32_t active, pending;
   2561    int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
   2562    if (ret == -1) {
   2563       /* We don't know the real error. */
   2564       return anv_device_set_lost(device, "get_reset_stats failed: %m");
   2565    }
   2566 
   2567    if (active) {
   2568       return anv_device_set_lost(device, "GPU hung on one of our command buffers");
   2569    } else if (pending) {
   2570       return anv_device_set_lost(device, "GPU hung with commands in-flight");
   2571    }
   2572 
   2573    return VK_SUCCESS;
   2574 }
   2575 
   2576 VkResult
   2577 anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
   2578 {
   2579    /* Note:  This only returns whether or not the BO is in use by an i915 GPU.
   2580     * Other usages of the BO (such as on different hardware) will not be
   2581     * flagged as "busy" by this ioctl.  Use with care.
   2582     */
   2583    int ret = anv_gem_busy(device, bo->gem_handle);
   2584    if (ret == 1) {
   2585       return VK_NOT_READY;
   2586    } else if (ret == -1) {
   2587       /* We don't know the real error. */
   2588       return anv_device_set_lost(device, "gem wait failed: %m");
   2589    }
   2590 
   2591    /* Query for device status after the busy call.  If the BO we're checking
   2592     * got caught in a GPU hang we don't want to return VK_SUCCESS to the
   2593     * client because it clearly doesn't have valid data.  Yes, this most
   2594     * likely means an ioctl, but we just did an ioctl to query the busy status
   2595     * so it's no great loss.
   2596     */
   2597    return anv_device_query_status(device);
   2598 }
   2599 
   2600 VkResult
   2601 anv_device_wait(struct anv_device *device, struct anv_bo *bo,
   2602                 int64_t timeout)
   2603 {
   2604    int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
   2605    if (ret == -1 && errno == ETIME) {
   2606       return VK_TIMEOUT;
   2607    } else if (ret == -1) {
   2608       /* We don't know the real error. */
   2609       return anv_device_set_lost(device, "gem wait failed: %m");
   2610    }
   2611 
   2612    /* Query for device status after the wait.  If the BO we're waiting on got
   2613     * caught in a GPU hang we don't want to return VK_SUCCESS to the client
   2614     * because it clearly doesn't have valid data.  Yes, this most likely means
   2615     * an ioctl, but we just did an ioctl to wait so it's no great loss.
   2616     */
   2617    return anv_device_query_status(device);
   2618 }
   2619 
   2620 VkResult anv_DeviceWaitIdle(
   2621     VkDevice                                    _device)
   2622 {
   2623    ANV_FROM_HANDLE(anv_device, device, _device);
   2624    if (anv_device_is_lost(device))
   2625       return VK_ERROR_DEVICE_LOST;
   2626 
   2627    struct anv_batch batch;
   2628 
   2629    uint32_t cmds[8];
   2630    batch.start = batch.next = cmds;
   2631    batch.end = (void *) cmds + sizeof(cmds);
   2632 
   2633    anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
   2634    anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
   2635 
   2636    return anv_device_submit_simple_batch(device, &batch);
   2637 }
   2638 
   2639 bool
   2640 anv_vma_alloc(struct anv_device *device, struct anv_bo *bo)
   2641 {
   2642    if (!(bo->flags & EXEC_OBJECT_PINNED))
   2643       return true;
   2644 
   2645    pthread_mutex_lock(&device->vma_mutex);
   2646 
   2647    bo->offset = 0;
   2648 
   2649    if (bo->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS &&
   2650        device->vma_hi_available >= bo->size) {
   2651       uint64_t addr = util_vma_heap_alloc(&device->vma_hi, bo->size, 4096);
   2652       if (addr) {
   2653          bo->offset = gen_canonical_address(addr);
   2654          assert(addr == gen_48b_address(bo->offset));
   2655          device->vma_hi_available -= bo->size;
   2656       }
   2657    }
   2658 
   2659    if (bo->offset == 0 && device->vma_lo_available >= bo->size) {
   2660       uint64_t addr = util_vma_heap_alloc(&device->vma_lo, bo->size, 4096);
   2661       if (addr) {
   2662          bo->offset = gen_canonical_address(addr);
   2663          assert(addr == gen_48b_address(bo->offset));
   2664          device->vma_lo_available -= bo->size;
   2665       }
   2666    }
   2667 
   2668    pthread_mutex_unlock(&device->vma_mutex);
   2669 
   2670    return bo->offset != 0;
   2671 }
   2672 
   2673 void
   2674 anv_vma_free(struct anv_device *device, struct anv_bo *bo)
   2675 {
   2676    if (!(bo->flags & EXEC_OBJECT_PINNED))
   2677       return;
   2678 
   2679    const uint64_t addr_48b = gen_48b_address(bo->offset);
   2680 
   2681    pthread_mutex_lock(&device->vma_mutex);
   2682 
   2683    if (addr_48b >= LOW_HEAP_MIN_ADDRESS &&
   2684        addr_48b <= LOW_HEAP_MAX_ADDRESS) {
   2685       util_vma_heap_free(&device->vma_lo, addr_48b, bo->size);
   2686       device->vma_lo_available += bo->size;
   2687    } else {
   2688       MAYBE_UNUSED const struct anv_physical_device *physical_device =
   2689          &device->instance->physicalDevice;
   2690       assert(addr_48b >= physical_device->memory.heaps[0].vma_start &&
   2691              addr_48b < (physical_device->memory.heaps[0].vma_start +
   2692                          physical_device->memory.heaps[0].vma_size));
   2693       util_vma_heap_free(&device->vma_hi, addr_48b, bo->size);
   2694       device->vma_hi_available += bo->size;
   2695    }
   2696 
   2697    pthread_mutex_unlock(&device->vma_mutex);
   2698 
   2699    bo->offset = 0;
   2700 }
   2701 
   2702 VkResult
   2703 anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
   2704 {
   2705    uint32_t gem_handle = anv_gem_create(device, size);
   2706    if (!gem_handle)
   2707       return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
   2708 
   2709    anv_bo_init(bo, gem_handle, size);
   2710 
   2711    return VK_SUCCESS;
   2712 }
   2713 
   2714 VkResult anv_AllocateMemory(
   2715     VkDevice                                    _device,
   2716     const VkMemoryAllocateInfo*                 pAllocateInfo,
   2717     const VkAllocationCallbacks*                pAllocator,
   2718     VkDeviceMemory*                             pMem)
   2719 {
   2720    ANV_FROM_HANDLE(anv_device, device, _device);
   2721    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   2722    struct anv_device_memory *mem;
   2723    VkResult result = VK_SUCCESS;
   2724 
   2725    assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
   2726 
   2727    /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
   2728    assert(pAllocateInfo->allocationSize > 0);
   2729 
   2730    if (pAllocateInfo->allocationSize > MAX_MEMORY_ALLOCATION_SIZE)
   2731       return VK_ERROR_OUT_OF_DEVICE_MEMORY;
   2732 
   2733    /* FINISHME: Fail if allocation request exceeds heap size. */
   2734 
   2735    mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
   2736                     VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
   2737    if (mem == NULL)
   2738       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
   2739 
   2740    assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
   2741    mem->type = &pdevice->memory.types[pAllocateInfo->memoryTypeIndex];
   2742    mem->map = NULL;
   2743    mem->map_size = 0;
   2744    mem->ahw = NULL;
   2745    mem->host_ptr = NULL;
   2746 
   2747    uint64_t bo_flags = 0;
   2748 
   2749    assert(mem->type->heapIndex < pdevice->memory.heap_count);
   2750    if (pdevice->memory.heaps[mem->type->heapIndex].supports_48bit_addresses)
   2751       bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
   2752 
   2753    const struct wsi_memory_allocate_info *wsi_info =
   2754       vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
   2755    if (wsi_info && wsi_info->implicit_sync) {
   2756       /* We need to set the WRITE flag on window system buffers so that GEM
   2757        * will know we're writing to them and synchronize uses on other rings
   2758        * (eg if the display server uses the blitter ring).
   2759        */
   2760       bo_flags |= EXEC_OBJECT_WRITE;
   2761    } else if (pdevice->has_exec_async) {
   2762       bo_flags |= EXEC_OBJECT_ASYNC;
   2763    }
   2764 
   2765    if (pdevice->use_softpin)
   2766       bo_flags |= EXEC_OBJECT_PINNED;
   2767 
   2768    const VkExportMemoryAllocateInfo *export_info =
   2769       vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO);
   2770 
   2771    /* Check if we need to support Android HW buffer export. If so,
   2772     * create AHardwareBuffer and import memory from it.
   2773     */
   2774    bool android_export = false;
   2775    if (export_info && export_info->handleTypes &
   2776        VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)
   2777       android_export = true;
   2778 
   2779    /* Android memory import. */
   2780    const struct VkImportAndroidHardwareBufferInfoANDROID *ahw_import_info =
   2781       vk_find_struct_const(pAllocateInfo->pNext,
   2782                            IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);
   2783 
   2784    if (ahw_import_info) {
   2785       result = anv_import_ahw_memory(_device, mem, ahw_import_info);
   2786       if (result != VK_SUCCESS)
   2787          goto fail;
   2788 
   2789       goto success;
   2790    } else if (android_export) {
   2791       result = anv_create_ahw_memory(_device, mem, pAllocateInfo);
   2792       if (result != VK_SUCCESS)
   2793          goto fail;
   2794 
   2795       const struct VkImportAndroidHardwareBufferInfoANDROID import_info = {
   2796          .buffer = mem->ahw,
   2797       };
   2798       result = anv_import_ahw_memory(_device, mem, &import_info);
   2799       if (result != VK_SUCCESS)
   2800          goto fail;
   2801 
   2802       goto success;
   2803    }
   2804 
   2805    const VkImportMemoryFdInfoKHR *fd_info =
   2806       vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
   2807 
   2808    /* The Vulkan spec permits handleType to be 0, in which case the struct is
   2809     * ignored.
   2810     */
   2811    if (fd_info && fd_info->handleType) {
   2812       /* At the moment, we support only the below handle types. */
   2813       assert(fd_info->handleType ==
   2814                VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
   2815              fd_info->handleType ==
   2816                VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
   2817 
   2818       result = anv_bo_cache_import(device, &device->bo_cache, fd_info->fd,
   2819                                    bo_flags | ANV_BO_EXTERNAL, &mem->bo);
   2820       if (result != VK_SUCCESS)
   2821          goto fail;
   2822 
   2823       VkDeviceSize aligned_alloc_size =
   2824          align_u64(pAllocateInfo->allocationSize, 4096);
   2825 
   2826       /* For security purposes, we reject importing the bo if it's smaller
   2827        * than the requested allocation size.  This prevents a malicious client
   2828        * from passing a buffer to a trusted client, lying about the size, and
   2829        * telling the trusted client to try and texture from an image that goes
   2830        * out-of-bounds.  This sort of thing could lead to GPU hangs or worse
   2831        * in the trusted client.  The trusted client can protect itself against
   2832        * this sort of attack but only if it can trust the buffer size.
   2833        */
   2834       if (mem->bo->size < aligned_alloc_size) {
   2835          result = vk_errorf(device->instance, device,
   2836                             VK_ERROR_INVALID_EXTERNAL_HANDLE,
   2837                             "aligned allocationSize too large for "
   2838                             "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
   2839                             "%"PRIu64"B > %"PRIu64"B",
   2840                             aligned_alloc_size, mem->bo->size);
   2841          anv_bo_cache_release(device, &device->bo_cache, mem->bo);
   2842          goto fail;
   2843       }
   2844 
   2845       /* From the Vulkan spec:
   2846        *
   2847        *    "Importing memory from a file descriptor transfers ownership of
   2848        *    the file descriptor from the application to the Vulkan
   2849        *    implementation. The application must not perform any operations on
   2850        *    the file descriptor after a successful import."
   2851        *
   2852        * If the import fails, we leave the file descriptor open.
   2853        */
   2854       close(fd_info->fd);
   2855       goto success;
   2856    }
   2857 
   2858    const VkImportMemoryHostPointerInfoEXT *host_ptr_info =
   2859       vk_find_struct_const(pAllocateInfo->pNext,
   2860                            IMPORT_MEMORY_HOST_POINTER_INFO_EXT);
   2861    if (host_ptr_info && host_ptr_info->handleType) {
   2862       if (host_ptr_info->handleType ==
   2863           VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT) {
   2864          result = vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
   2865          goto fail;
   2866       }
   2867 
   2868       assert(host_ptr_info->handleType ==
   2869              VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
   2870 
   2871       result = anv_bo_cache_import_host_ptr(
   2872          device, &device->bo_cache, host_ptr_info->pHostPointer,
   2873          pAllocateInfo->allocationSize, bo_flags, &mem->bo);
   2874 
   2875       if (result != VK_SUCCESS)
   2876          goto fail;
   2877 
   2878       mem->host_ptr = host_ptr_info->pHostPointer;
   2879       goto success;
   2880    }
   2881 
   2882    /* Regular allocate (not importing memory). */
   2883 
   2884    if (export_info && export_info->handleTypes)
   2885       bo_flags |= ANV_BO_EXTERNAL;
   2886 
   2887    result = anv_bo_cache_alloc(device, &device->bo_cache,
   2888                                pAllocateInfo->allocationSize, bo_flags,
   2889                                &mem->bo);
   2890    if (result != VK_SUCCESS)
   2891       goto fail;
   2892 
   2893    const VkMemoryDedicatedAllocateInfo *dedicated_info =
   2894       vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
   2895    if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
   2896       ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
   2897 
   2898       /* Some legacy (non-modifiers) consumers need the tiling to be set on
   2899        * the BO.  In this case, we have a dedicated allocation.
   2900        */
   2901       if (image->needs_set_tiling) {
   2902          const uint32_t i915_tiling =
   2903             isl_tiling_to_i915_tiling(image->planes[0].surface.isl.tiling);
   2904          int ret = anv_gem_set_tiling(device, mem->bo->gem_handle,
   2905                                       image->planes[0].surface.isl.row_pitch_B,
   2906                                       i915_tiling);
   2907          if (ret) {
   2908             anv_bo_cache_release(device, &device->bo_cache, mem->bo);
   2909             return vk_errorf(device->instance, NULL,
   2910                              VK_ERROR_OUT_OF_DEVICE_MEMORY,
   2911                              "failed to set BO tiling: %m");
   2912          }
   2913       }
   2914    }
   2915 
   2916  success:
   2917    pthread_mutex_lock(&device->mutex);
   2918    list_addtail(&mem->link, &device->memory_objects);
   2919    pthread_mutex_unlock(&device->mutex);
   2920 
   2921    *pMem = anv_device_memory_to_handle(mem);
   2922 
   2923    p_atomic_add(&pdevice->memory.heaps[mem->type->heapIndex].used,
   2924                 mem->bo->size);
   2925 
   2926    return VK_SUCCESS;
   2927 
   2928  fail:
   2929    vk_free2(&device->alloc, pAllocator, mem);
   2930 
   2931    return result;
   2932 }
   2933 
   2934 VkResult anv_GetMemoryFdKHR(
   2935     VkDevice                                    device_h,
   2936     const VkMemoryGetFdInfoKHR*                 pGetFdInfo,
   2937     int*                                        pFd)
   2938 {
   2939    ANV_FROM_HANDLE(anv_device, dev, device_h);
   2940    ANV_FROM_HANDLE(anv_device_memory, mem, pGetFdInfo->memory);
   2941 
   2942    assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
   2943 
   2944    assert(pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
   2945           pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
   2946 
   2947    return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
   2948 }
   2949 
   2950 VkResult anv_GetMemoryFdPropertiesKHR(
   2951     VkDevice                                    _device,
   2952     VkExternalMemoryHandleTypeFlagBits          handleType,
   2953     int                                         fd,
   2954     VkMemoryFdPropertiesKHR*                    pMemoryFdProperties)
   2955 {
   2956    ANV_FROM_HANDLE(anv_device, device, _device);
   2957    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   2958 
   2959    switch (handleType) {
   2960    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
   2961       /* dma-buf can be imported as any memory type */
   2962       pMemoryFdProperties->memoryTypeBits =
   2963          (1 << pdevice->memory.type_count) - 1;
   2964       return VK_SUCCESS;
   2965 
   2966    default:
   2967       /* The valid usage section for this function says:
   2968        *
   2969        *    "handleType must not be one of the handle types defined as
   2970        *    opaque."
   2971        *
   2972        * So opaque handle types fall into the default "unsupported" case.
   2973        */
   2974       return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
   2975    }
   2976 }
   2977 
   2978 VkResult anv_GetMemoryHostPointerPropertiesEXT(
   2979    VkDevice                                    _device,
   2980    VkExternalMemoryHandleTypeFlagBits          handleType,
   2981    const void*                                 pHostPointer,
   2982    VkMemoryHostPointerPropertiesEXT*           pMemoryHostPointerProperties)
   2983 {
   2984    ANV_FROM_HANDLE(anv_device, device, _device);
   2985 
   2986    assert(pMemoryHostPointerProperties->sType ==
   2987           VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT);
   2988 
   2989    switch (handleType) {
   2990    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT: {
   2991       struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   2992 
   2993       /* Host memory can be imported as any memory type. */
   2994       pMemoryHostPointerProperties->memoryTypeBits =
   2995          (1ull << pdevice->memory.type_count) - 1;
   2996 
   2997       return VK_SUCCESS;
   2998    }
   2999    default:
   3000       return VK_ERROR_INVALID_EXTERNAL_HANDLE;
   3001    }
   3002 }
   3003 
   3004 void anv_FreeMemory(
   3005     VkDevice                                    _device,
   3006     VkDeviceMemory                              _mem,
   3007     const VkAllocationCallbacks*                pAllocator)
   3008 {
   3009    ANV_FROM_HANDLE(anv_device, device, _device);
   3010    ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
   3011    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   3012 
   3013    if (mem == NULL)
   3014       return;
   3015 
   3016    pthread_mutex_lock(&device->mutex);
   3017    list_del(&mem->link);
   3018    pthread_mutex_unlock(&device->mutex);
   3019 
   3020    if (mem->map)
   3021       anv_UnmapMemory(_device, _mem);
   3022 
   3023    p_atomic_add(&pdevice->memory.heaps[mem->type->heapIndex].used,
   3024                 -mem->bo->size);
   3025 
   3026    anv_bo_cache_release(device, &device->bo_cache, mem->bo);
   3027 
   3028 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
   3029    if (mem->ahw)
   3030       AHardwareBuffer_release(mem->ahw);
   3031 #endif
   3032 
   3033    vk_free2(&device->alloc, pAllocator, mem);
   3034 }
   3035 
   3036 VkResult anv_MapMemory(
   3037     VkDevice                                    _device,
   3038     VkDeviceMemory                              _memory,
   3039     VkDeviceSize                                offset,
   3040     VkDeviceSize                                size,
   3041     VkMemoryMapFlags                            flags,
   3042     void**                                      ppData)
   3043 {
   3044    ANV_FROM_HANDLE(anv_device, device, _device);
   3045    ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
   3046 
   3047    if (mem == NULL) {
   3048       *ppData = NULL;
   3049       return VK_SUCCESS;
   3050    }
   3051 
   3052    if (mem->host_ptr) {
   3053       *ppData = mem->host_ptr + offset;
   3054       return VK_SUCCESS;
   3055    }
   3056 
   3057    if (size == VK_WHOLE_SIZE)
   3058       size = mem->bo->size - offset;
   3059 
   3060    /* From the Vulkan spec version 1.0.32 docs for MapMemory:
   3061     *
   3062     *  * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
   3063     *    assert(size != 0);
   3064     *  * If size is not equal to VK_WHOLE_SIZE, size must be less than or
   3065     *    equal to the size of the memory minus offset
   3066     */
   3067    assert(size > 0);
   3068    assert(offset + size <= mem->bo->size);
   3069 
   3070    /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
   3071     * takes a VkDeviceMemory pointer, it seems like only one map of the memory
   3072     * at a time is valid. We could just mmap up front and return an offset
   3073     * pointer here, but that may exhaust virtual memory on 32 bit
   3074     * userspace. */
   3075 
   3076    uint32_t gem_flags = 0;
   3077 
   3078    if (!device->info.has_llc &&
   3079        (mem->type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
   3080       gem_flags |= I915_MMAP_WC;
   3081 
   3082    /* GEM will fail to map if the offset isn't 4k-aligned.  Round down. */
   3083    uint64_t map_offset = offset & ~4095ull;
   3084    assert(offset >= map_offset);
   3085    uint64_t map_size = (offset + size) - map_offset;
   3086 
   3087    /* Let's map whole pages */
   3088    map_size = align_u64(map_size, 4096);
   3089 
   3090    void *map = anv_gem_mmap(device, mem->bo->gem_handle,
   3091                             map_offset, map_size, gem_flags);
   3092    if (map == MAP_FAILED)
   3093       return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
   3094 
   3095    mem->map = map;
   3096    mem->map_size = map_size;
   3097 
   3098    *ppData = mem->map + (offset - map_offset);
   3099 
   3100    return VK_SUCCESS;
   3101 }
   3102 
   3103 void anv_UnmapMemory(
   3104     VkDevice                                    _device,
   3105     VkDeviceMemory                              _memory)
   3106 {
   3107    ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
   3108 
   3109    if (mem == NULL || mem->host_ptr)
   3110       return;
   3111 
   3112    anv_gem_munmap(mem->map, mem->map_size);
   3113 
   3114    mem->map = NULL;
   3115    mem->map_size = 0;
   3116 }
   3117 
   3118 static void
   3119 clflush_mapped_ranges(struct anv_device         *device,
   3120                       uint32_t                   count,
   3121                       const VkMappedMemoryRange *ranges)
   3122 {
   3123    for (uint32_t i = 0; i < count; i++) {
   3124       ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
   3125       if (ranges[i].offset >= mem->map_size)
   3126          continue;
   3127 
   3128       gen_clflush_range(mem->map + ranges[i].offset,
   3129                         MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
   3130    }
   3131 }
   3132 
   3133 VkResult anv_FlushMappedMemoryRanges(
   3134     VkDevice                                    _device,
   3135     uint32_t                                    memoryRangeCount,
   3136     const VkMappedMemoryRange*                  pMemoryRanges)
   3137 {
   3138    ANV_FROM_HANDLE(anv_device, device, _device);
   3139 
   3140    if (device->info.has_llc)
   3141       return VK_SUCCESS;
   3142 
   3143    /* Make sure the writes we're flushing have landed. */
   3144    __builtin_ia32_mfence();
   3145 
   3146    clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
   3147 
   3148    return VK_SUCCESS;
   3149 }
   3150 
   3151 VkResult anv_InvalidateMappedMemoryRanges(
   3152     VkDevice                                    _device,
   3153     uint32_t                                    memoryRangeCount,
   3154     const VkMappedMemoryRange*                  pMemoryRanges)
   3155 {
   3156    ANV_FROM_HANDLE(anv_device, device, _device);
   3157 
   3158    if (device->info.has_llc)
   3159       return VK_SUCCESS;
   3160 
   3161    clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
   3162 
   3163    /* Make sure no reads get moved up above the invalidate. */
   3164    __builtin_ia32_mfence();
   3165 
   3166    return VK_SUCCESS;
   3167 }
   3168 
   3169 void anv_GetBufferMemoryRequirements(
   3170     VkDevice                                    _device,
   3171     VkBuffer                                    _buffer,
   3172     VkMemoryRequirements*                       pMemoryRequirements)
   3173 {
   3174    ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
   3175    ANV_FROM_HANDLE(anv_device, device, _device);
   3176    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   3177 
   3178    /* The Vulkan spec (git aaed022) says:
   3179     *
   3180     *    memoryTypeBits is a bitfield and contains one bit set for every
   3181     *    supported memory type for the resource. The bit `1<<i` is set if and
   3182     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
   3183     *    structure for the physical device is supported.
   3184     */
   3185    uint32_t memory_types = 0;
   3186    for (uint32_t i = 0; i < pdevice->memory.type_count; i++) {
   3187       uint32_t valid_usage = pdevice->memory.types[i].valid_buffer_usage;
   3188       if ((valid_usage & buffer->usage) == buffer->usage)
   3189          memory_types |= (1u << i);
   3190    }
   3191 
   3192    /* Base alignment requirement of a cache line */
   3193    uint32_t alignment = 16;
   3194 
   3195    /* We need an alignment of 32 for pushing UBOs */
   3196    if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
   3197       alignment = MAX2(alignment, 32);
   3198 
   3199    pMemoryRequirements->size = buffer->size;
   3200    pMemoryRequirements->alignment = alignment;
   3201 
   3202    /* Storage and Uniform buffers should have their size aligned to
   3203     * 32-bits to avoid boundary checks when last DWord is not complete.
   3204     * This would ensure that not internal padding would be needed for
   3205     * 16-bit types.
   3206     */
   3207    if (device->robust_buffer_access &&
   3208        (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
   3209         buffer->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
   3210       pMemoryRequirements->size = align_u64(buffer->size, 4);
   3211 
   3212    pMemoryRequirements->memoryTypeBits = memory_types;
   3213 }
   3214 
   3215 void anv_GetBufferMemoryRequirements2(
   3216     VkDevice                                    _device,
   3217     const VkBufferMemoryRequirementsInfo2*      pInfo,
   3218     VkMemoryRequirements2*                      pMemoryRequirements)
   3219 {
   3220    anv_GetBufferMemoryRequirements(_device, pInfo->buffer,
   3221                                    &pMemoryRequirements->memoryRequirements);
   3222 
   3223    vk_foreach_struct(ext, pMemoryRequirements->pNext) {
   3224       switch (ext->sType) {
   3225       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
   3226          VkMemoryDedicatedRequirements *requirements = (void *)ext;
   3227          requirements->prefersDedicatedAllocation = false;
   3228          requirements->requiresDedicatedAllocation = false;
   3229          break;
   3230       }
   3231 
   3232       default:
   3233          anv_debug_ignored_stype(ext->sType);
   3234          break;
   3235       }
   3236    }
   3237 }
   3238 
   3239 void anv_GetImageMemoryRequirements(
   3240     VkDevice                                    _device,
   3241     VkImage                                     _image,
   3242     VkMemoryRequirements*                       pMemoryRequirements)
   3243 {
   3244    ANV_FROM_HANDLE(anv_image, image, _image);
   3245    ANV_FROM_HANDLE(anv_device, device, _device);
   3246    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   3247 
   3248    /* The Vulkan spec (git aaed022) says:
   3249     *
   3250     *    memoryTypeBits is a bitfield and contains one bit set for every
   3251     *    supported memory type for the resource. The bit `1<<i` is set if and
   3252     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
   3253     *    structure for the physical device is supported.
   3254     *
   3255     * All types are currently supported for images.
   3256     */
   3257    uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
   3258 
   3259    /* We must have image allocated or imported at this point. According to the
   3260     * specification, external images must have been bound to memory before
   3261     * calling GetImageMemoryRequirements.
   3262     */
   3263    assert(image->size > 0);
   3264 
   3265    pMemoryRequirements->size = image->size;
   3266    pMemoryRequirements->alignment = image->alignment;
   3267    pMemoryRequirements->memoryTypeBits = memory_types;
   3268 }
   3269 
   3270 void anv_GetImageMemoryRequirements2(
   3271     VkDevice                                    _device,
   3272     const VkImageMemoryRequirementsInfo2*       pInfo,
   3273     VkMemoryRequirements2*                      pMemoryRequirements)
   3274 {
   3275    ANV_FROM_HANDLE(anv_device, device, _device);
   3276    ANV_FROM_HANDLE(anv_image, image, pInfo->image);
   3277 
   3278    anv_GetImageMemoryRequirements(_device, pInfo->image,
   3279                                   &pMemoryRequirements->memoryRequirements);
   3280 
   3281    vk_foreach_struct_const(ext, pInfo->pNext) {
   3282       switch (ext->sType) {
   3283       case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO: {
   3284          struct anv_physical_device *pdevice = &device->instance->physicalDevice;
   3285          const VkImagePlaneMemoryRequirementsInfo *plane_reqs =
   3286             (const VkImagePlaneMemoryRequirementsInfo *) ext;
   3287          uint32_t plane = anv_image_aspect_to_plane(image->aspects,
   3288                                                     plane_reqs->planeAspect);
   3289 
   3290          assert(image->planes[plane].offset == 0);
   3291 
   3292          /* The Vulkan spec (git aaed022) says:
   3293           *
   3294           *    memoryTypeBits is a bitfield and contains one bit set for every
   3295           *    supported memory type for the resource. The bit `1<<i` is set
   3296           *    if and only if the memory type `i` in the
   3297           *    VkPhysicalDeviceMemoryProperties structure for the physical
   3298           *    device is supported.
   3299           *
   3300           * All types are currently supported for images.
   3301           */
   3302          pMemoryRequirements->memoryRequirements.memoryTypeBits =
   3303                (1ull << pdevice->memory.type_count) - 1;
   3304 
   3305          /* We must have image allocated or imported at this point. According to the
   3306           * specification, external images must have been bound to memory before
   3307           * calling GetImageMemoryRequirements.
   3308           */
   3309          assert(image->planes[plane].size > 0);
   3310 
   3311          pMemoryRequirements->memoryRequirements.size = image->planes[plane].size;
   3312          pMemoryRequirements->memoryRequirements.alignment =
   3313             image->planes[plane].alignment;
   3314          break;
   3315       }
   3316 
   3317       default:
   3318          anv_debug_ignored_stype(ext->sType);
   3319          break;
   3320       }
   3321    }
   3322 
   3323    vk_foreach_struct(ext, pMemoryRequirements->pNext) {
   3324       switch (ext->sType) {
   3325       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
   3326          VkMemoryDedicatedRequirements *requirements = (void *)ext;
   3327          if (image->needs_set_tiling || image->external_format) {
   3328             /* If we need to set the tiling for external consumers, we need a
   3329              * dedicated allocation.
   3330              *
   3331              * See also anv_AllocateMemory.
   3332              */
   3333             requirements->prefersDedicatedAllocation = true;
   3334             requirements->requiresDedicatedAllocation = true;
   3335          } else {
   3336             requirements->prefersDedicatedAllocation = false;
   3337             requirements->requiresDedicatedAllocation = false;
   3338          }
   3339          break;
   3340       }
   3341 
   3342       default:
   3343          anv_debug_ignored_stype(ext->sType);
   3344          break;
   3345       }
   3346    }
   3347 }
   3348 
   3349 void anv_GetImageSparseMemoryRequirements(
   3350     VkDevice                                    device,
   3351     VkImage                                     image,
   3352     uint32_t*                                   pSparseMemoryRequirementCount,
   3353     VkSparseImageMemoryRequirements*            pSparseMemoryRequirements)
   3354 {
   3355    *pSparseMemoryRequirementCount = 0;
   3356 }
   3357 
   3358 void anv_GetImageSparseMemoryRequirements2(
   3359     VkDevice                                    device,
   3360     const VkImageSparseMemoryRequirementsInfo2* pInfo,
   3361     uint32_t*                                   pSparseMemoryRequirementCount,
   3362     VkSparseImageMemoryRequirements2*           pSparseMemoryRequirements)
   3363 {
   3364    *pSparseMemoryRequirementCount = 0;
   3365 }
   3366 
   3367 void anv_GetDeviceMemoryCommitment(
   3368     VkDevice                                    device,
   3369     VkDeviceMemory                              memory,
   3370     VkDeviceSize*                               pCommittedMemoryInBytes)
   3371 {
   3372    *pCommittedMemoryInBytes = 0;
   3373 }
   3374 
   3375 static void
   3376 anv_bind_buffer_memory(const VkBindBufferMemoryInfo *pBindInfo)
   3377 {
   3378    ANV_FROM_HANDLE(anv_device_memory, mem, pBindInfo->memory);
   3379    ANV_FROM_HANDLE(anv_buffer, buffer, pBindInfo->buffer);
   3380 
   3381    assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO);
   3382 
   3383    if (mem) {
   3384       assert((buffer->usage & mem->type->valid_buffer_usage) == buffer->usage);
   3385       buffer->address = (struct anv_address) {
   3386          .bo = mem->bo,
   3387          .offset = pBindInfo->memoryOffset,
   3388       };
   3389    } else {
   3390       buffer->address = ANV_NULL_ADDRESS;
   3391    }
   3392 }
   3393 
   3394 VkResult anv_BindBufferMemory(
   3395     VkDevice                                    device,
   3396     VkBuffer                                    buffer,
   3397     VkDeviceMemory                              memory,
   3398     VkDeviceSize                                memoryOffset)
   3399 {
   3400    anv_bind_buffer_memory(
   3401       &(VkBindBufferMemoryInfo) {
   3402          .sType         = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
   3403          .buffer        = buffer,
   3404          .memory        = memory,
   3405          .memoryOffset  = memoryOffset,
   3406       });
   3407 
   3408    return VK_SUCCESS;
   3409 }
   3410 
   3411 VkResult anv_BindBufferMemory2(
   3412     VkDevice                                    device,
   3413     uint32_t                                    bindInfoCount,
   3414     const VkBindBufferMemoryInfo*               pBindInfos)
   3415 {
   3416    for (uint32_t i = 0; i < bindInfoCount; i++)
   3417       anv_bind_buffer_memory(&pBindInfos[i]);
   3418 
   3419    return VK_SUCCESS;
   3420 }
   3421 
   3422 VkResult anv_QueueBindSparse(
   3423     VkQueue                                     _queue,
   3424     uint32_t                                    bindInfoCount,
   3425     const VkBindSparseInfo*                     pBindInfo,
   3426     VkFence                                     fence)
   3427 {
   3428    ANV_FROM_HANDLE(anv_queue, queue, _queue);
   3429    if (anv_device_is_lost(queue->device))
   3430       return VK_ERROR_DEVICE_LOST;
   3431 
   3432    return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
   3433 }
   3434 
   3435 // Event functions
   3436 
   3437 VkResult anv_CreateEvent(
   3438     VkDevice                                    _device,
   3439     const VkEventCreateInfo*                    pCreateInfo,
   3440     const VkAllocationCallbacks*                pAllocator,
   3441     VkEvent*                                    pEvent)
   3442 {
   3443    ANV_FROM_HANDLE(anv_device, device, _device);
   3444    struct anv_state state;
   3445    struct anv_event *event;
   3446 
   3447    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
   3448 
   3449    state = anv_state_pool_alloc(&device->dynamic_state_pool,
   3450                                 sizeof(*event), 8);
   3451    event = state.map;
   3452    event->state = state;
   3453    event->semaphore = VK_EVENT_RESET;
   3454 
   3455    if (!device->info.has_llc) {
   3456       /* Make sure the writes we're flushing have landed. */
   3457       __builtin_ia32_mfence();
   3458       __builtin_ia32_clflush(event);
   3459    }
   3460 
   3461    *pEvent = anv_event_to_handle(event);
   3462 
   3463    return VK_SUCCESS;
   3464 }
   3465 
   3466 void anv_DestroyEvent(
   3467     VkDevice                                    _device,
   3468     VkEvent                                     _event,
   3469     const VkAllocationCallbacks*                pAllocator)
   3470 {
   3471    ANV_FROM_HANDLE(anv_device, device, _device);
   3472    ANV_FROM_HANDLE(anv_event, event, _event);
   3473 
   3474    if (!event)
   3475       return;
   3476 
   3477    anv_state_pool_free(&device->dynamic_state_pool, event->state);
   3478 }
   3479 
   3480 VkResult anv_GetEventStatus(
   3481     VkDevice                                    _device,
   3482     VkEvent                                     _event)
   3483 {
   3484    ANV_FROM_HANDLE(anv_device, device, _device);
   3485    ANV_FROM_HANDLE(anv_event, event, _event);
   3486 
   3487    if (anv_device_is_lost(device))
   3488       return VK_ERROR_DEVICE_LOST;
   3489 
   3490    if (!device->info.has_llc) {
   3491       /* Invalidate read cache before reading event written by GPU. */
   3492       __builtin_ia32_clflush(event);
   3493       __builtin_ia32_mfence();
   3494 
   3495    }
   3496 
   3497    return event->semaphore;
   3498 }
   3499 
   3500 VkResult anv_SetEvent(
   3501     VkDevice                                    _device,
   3502     VkEvent                                     _event)
   3503 {
   3504    ANV_FROM_HANDLE(anv_device, device, _device);
   3505    ANV_FROM_HANDLE(anv_event, event, _event);
   3506 
   3507    event->semaphore = VK_EVENT_SET;
   3508 
   3509    if (!device->info.has_llc) {
   3510       /* Make sure the writes we're flushing have landed. */
   3511       __builtin_ia32_mfence();
   3512       __builtin_ia32_clflush(event);
   3513    }
   3514 
   3515    return VK_SUCCESS;
   3516 }
   3517 
   3518 VkResult anv_ResetEvent(
   3519     VkDevice                                    _device,
   3520     VkEvent                                     _event)
   3521 {
   3522    ANV_FROM_HANDLE(anv_device, device, _device);
   3523    ANV_FROM_HANDLE(anv_event, event, _event);
   3524 
   3525    event->semaphore = VK_EVENT_RESET;
   3526 
   3527    if (!device->info.has_llc) {
   3528       /* Make sure the writes we're flushing have landed. */
   3529       __builtin_ia32_mfence();
   3530       __builtin_ia32_clflush(event);
   3531    }
   3532 
   3533    return VK_SUCCESS;
   3534 }
   3535 
   3536 // Buffer functions
   3537 
   3538 VkResult anv_CreateBuffer(
   3539     VkDevice                                    _device,
   3540     const VkBufferCreateInfo*                   pCreateInfo,
   3541     const VkAllocationCallbacks*                pAllocator,
   3542     VkBuffer*                                   pBuffer)
   3543 {
   3544    ANV_FROM_HANDLE(anv_device, device, _device);
   3545    struct anv_buffer *buffer;
   3546 
   3547    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
   3548 
   3549    buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
   3550                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
   3551    if (buffer == NULL)
   3552       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
   3553 
   3554    buffer->size = pCreateInfo->size;
   3555    buffer->usage = pCreateInfo->usage;
   3556    buffer->address = ANV_NULL_ADDRESS;
   3557 
   3558    *pBuffer = anv_buffer_to_handle(buffer);
   3559 
   3560    return VK_SUCCESS;
   3561 }
   3562 
   3563 void anv_DestroyBuffer(
   3564     VkDevice                                    _device,
   3565     VkBuffer                                    _buffer,
   3566     const VkAllocationCallbacks*                pAllocator)
   3567 {
   3568    ANV_FROM_HANDLE(anv_device, device, _device);
   3569    ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
   3570 
   3571    if (!buffer)
   3572       return;
   3573 
   3574    vk_free2(&device->alloc, pAllocator, buffer);
   3575 }
   3576 
   3577 VkDeviceAddress anv_GetBufferDeviceAddressEXT(
   3578     VkDevice                                    device,
   3579     const VkBufferDeviceAddressInfoEXT*         pInfo)
   3580 {
   3581    ANV_FROM_HANDLE(anv_buffer, buffer, pInfo->buffer);
   3582 
   3583    assert(buffer->address.bo->flags & EXEC_OBJECT_PINNED);
   3584 
   3585    return anv_address_physical(buffer->address);
   3586 }
   3587 
   3588 void
   3589 anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
   3590                               enum isl_format format,
   3591                               struct anv_address address,
   3592                               uint32_t range, uint32_t stride)
   3593 {
   3594    isl_buffer_fill_state(&device->isl_dev, state.map,
   3595                          .address = anv_address_physical(address),
   3596                          .mocs = device->default_mocs,
   3597                          .size_B = range,
   3598                          .format = format,
   3599                          .swizzle = ISL_SWIZZLE_IDENTITY,
   3600                          .stride_B = stride);
   3601 }
   3602 
   3603 void anv_DestroySampler(
   3604     VkDevice                                    _device,
   3605     VkSampler                                   _sampler,
   3606     const VkAllocationCallbacks*                pAllocator)
   3607 {
   3608    ANV_FROM_HANDLE(anv_device, device, _device);
   3609    ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
   3610 
   3611    if (!sampler)
   3612       return;
   3613 
   3614    if (sampler->bindless_state.map) {
   3615       anv_state_pool_free(&device->dynamic_state_pool,
   3616                           sampler->bindless_state);
   3617    }
   3618 
   3619    vk_free2(&device->alloc, pAllocator, sampler);
   3620 }
   3621 
   3622 VkResult anv_CreateFramebuffer(
   3623     VkDevice                                    _device,
   3624     const VkFramebufferCreateInfo*              pCreateInfo,
   3625     const VkAllocationCallbacks*                pAllocator,
   3626     VkFramebuffer*                              pFramebuffer)
   3627 {
   3628    ANV_FROM_HANDLE(anv_device, device, _device);
   3629    struct anv_framebuffer *framebuffer;
   3630 
   3631    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
   3632 
   3633    size_t size = sizeof(*framebuffer) +
   3634                  sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
   3635    framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
   3636                             VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
   3637    if (framebuffer == NULL)
   3638       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
   3639 
   3640    framebuffer->attachment_count = pCreateInfo->attachmentCount;
   3641    for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
   3642       VkImageView _iview = pCreateInfo->pAttachments[i];
   3643       framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
   3644    }
   3645 
   3646    framebuffer->width = pCreateInfo->width;
   3647    framebuffer->height = pCreateInfo->height;
   3648    framebuffer->layers = pCreateInfo->layers;
   3649 
   3650    *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
   3651 
   3652    return VK_SUCCESS;
   3653 }
   3654 
   3655 void anv_DestroyFramebuffer(
   3656     VkDevice                                    _device,
   3657     VkFramebuffer                               _fb,
   3658     const VkAllocationCallbacks*                pAllocator)
   3659 {
   3660    ANV_FROM_HANDLE(anv_device, device, _device);
   3661    ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
   3662 
   3663    if (!fb)
   3664       return;
   3665 
   3666    vk_free2(&device->alloc, pAllocator, fb);
   3667 }
   3668 
   3669 static const VkTimeDomainEXT anv_time_domains[] = {
   3670    VK_TIME_DOMAIN_DEVICE_EXT,
   3671    VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
   3672    VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
   3673 };
   3674 
   3675 VkResult anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
   3676    VkPhysicalDevice                             physicalDevice,
   3677    uint32_t                                     *pTimeDomainCount,
   3678    VkTimeDomainEXT                              *pTimeDomains)
   3679 {
   3680    int d;
   3681    VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
   3682 
   3683    for (d = 0; d < ARRAY_SIZE(anv_time_domains); d++) {
   3684       vk_outarray_append(&out, i) {
   3685          *i = anv_time_domains[d];
   3686       }
   3687    }
   3688 
   3689    return vk_outarray_status(&out);
   3690 }
   3691 
   3692 static uint64_t
   3693 anv_clock_gettime(clockid_t clock_id)
   3694 {
   3695    struct timespec current;
   3696    int ret;
   3697 
   3698    ret = clock_gettime(clock_id, &current);
   3699    if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
   3700       ret = clock_gettime(CLOCK_MONOTONIC, &current);
   3701    if (ret < 0)
   3702       return 0;
   3703 
   3704    return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
   3705 }
   3706 
   3707 #define TIMESTAMP 0x2358
   3708 
   3709 VkResult anv_GetCalibratedTimestampsEXT(
   3710    VkDevice                                     _device,
   3711    uint32_t                                     timestampCount,
   3712    const VkCalibratedTimestampInfoEXT           *pTimestampInfos,
   3713    uint64_t                                     *pTimestamps,
   3714    uint64_t                                     *pMaxDeviation)
   3715 {
   3716    ANV_FROM_HANDLE(anv_device, device, _device);
   3717    uint64_t timestamp_frequency = device->info.timestamp_frequency;
   3718    int  ret;
   3719    int d;
   3720    uint64_t begin, end;
   3721    uint64_t max_clock_period = 0;
   3722 
   3723    begin = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
   3724 
   3725    for (d = 0; d < timestampCount; d++) {
   3726       switch (pTimestampInfos[d].timeDomain) {
   3727       case VK_TIME_DOMAIN_DEVICE_EXT:
   3728          ret = anv_gem_reg_read(device, TIMESTAMP | 1,
   3729                                 &pTimestamps[d]);
   3730 
   3731          if (ret != 0) {
   3732             return anv_device_set_lost(device, "Failed to read the TIMESTAMP "
   3733                                                "register: %m");
   3734          }
   3735          uint64_t device_period = DIV_ROUND_UP(1000000000, timestamp_frequency);
   3736          max_clock_period = MAX2(max_clock_period, device_period);
   3737          break;
   3738       case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
   3739          pTimestamps[d] = anv_clock_gettime(CLOCK_MONOTONIC);
   3740          max_clock_period = MAX2(max_clock_period, 1);
   3741          break;
   3742 
   3743       case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
   3744          pTimestamps[d] = begin;
   3745          break;
   3746       default:
   3747          pTimestamps[d] = 0;
   3748          break;
   3749       }
   3750    }
   3751 
   3752    end = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
   3753 
   3754     /*
   3755      * The maximum deviation is the sum of the interval over which we
   3756      * perform the sampling and the maximum period of any sampled
   3757      * clock. That's because the maximum skew between any two sampled
   3758      * clock edges is when the sampled clock with the largest period is
   3759      * sampled at the end of that period but right at the beginning of the
   3760      * sampling interval and some other clock is sampled right at the
   3761      * begining of its sampling period and right at the end of the
   3762      * sampling interval. Let's assume the GPU has the longest clock
   3763      * period and that the application is sampling GPU and monotonic:
   3764      *
   3765      *                               s                 e
   3766      *			 w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
   3767      *	Raw              -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
   3768      *
   3769      *                               g
   3770      *		  0         1         2         3
   3771      *	GPU       -----_____-----_____-----_____-----_____
   3772      *
   3773      *                                                m
   3774      *					    x y z 0 1 2 3 4 5 6 7 8 9 a b c
   3775      *	Monotonic                           -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
   3776      *
   3777      *	Interval                     <----------------->
   3778      *	Deviation           <-------------------------->
   3779      *
   3780      *		s  = read(raw)       2
   3781      *		g  = read(GPU)       1
   3782      *		m  = read(monotonic) 2
   3783      *		e  = read(raw)       b
   3784      *
   3785      * We round the sample interval up by one tick to cover sampling error
   3786      * in the interval clock
   3787      */
   3788 
   3789    uint64_t sample_interval = end - begin + 1;
   3790 
   3791    *pMaxDeviation = sample_interval + max_clock_period;
   3792 
   3793    return VK_SUCCESS;
   3794 }
   3795 
   3796 /* vk_icd.h does not declare this function, so we declare it here to
   3797  * suppress Wmissing-prototypes.
   3798  */
   3799 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
   3800 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
   3801 
   3802 PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
   3803 vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
   3804 {
   3805    /* For the full details on loader interface versioning, see
   3806     * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
   3807     * What follows is a condensed summary, to help you navigate the large and
   3808     * confusing official doc.
   3809     *
   3810     *   - Loader interface v0 is incompatible with later versions. We don't
   3811     *     support it.
   3812     *
   3813     *   - In loader interface v1:
   3814     *       - The first ICD entrypoint called by the loader is
   3815     *         vk_icdGetInstanceProcAddr(). The ICD must statically expose this
   3816     *         entrypoint.
   3817     *       - The ICD must statically expose no other Vulkan symbol unless it is
   3818     *         linked with -Bsymbolic.
   3819     *       - Each dispatchable Vulkan handle created by the ICD must be
   3820     *         a pointer to a struct whose first member is VK_LOADER_DATA. The
   3821     *         ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
   3822     *       - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
   3823     *         vkDestroySurfaceKHR(). The ICD must be capable of working with
   3824     *         such loader-managed surfaces.
   3825     *
   3826     *    - Loader interface v2 differs from v1 in:
   3827     *       - The first ICD entrypoint called by the loader is
   3828     *         vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
   3829     *         statically expose this entrypoint.
   3830     *
   3831     *    - Loader interface v3 differs from v2 in:
   3832     *        - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
   3833     *          vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
   3834     *          because the loader no longer does so.
   3835     */
   3836    *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
   3837    return VK_SUCCESS;
   3838 }
   3839