radv: Add support for VK_KHR_driver_properties.
[mesa.git] / src / amd / vulkan / radv_device.c
index 5936b43093e1603586154a1aec829b761795df6b..8e43a3aab58620dfc756387c068dd8e8dd8a89d2 100644 (file)
 #include "ac_llvm_util.h"
 #include "vk_format.h"
 #include "sid.h"
+#include "git_sha1.h"
 #include "gfx9d.h"
 #include "addrlib/gfx9/chip/gfx9_enum.h"
+#include "util/build_id.h"
 #include "util/debug.h"
+#include "util/mesa-sha1.h"
+
+static bool
+radv_get_build_id(void *ptr, struct mesa_sha1 *ctx)
+{
+       uint32_t timestamp;
+
+#ifdef HAVE_DL_ITERATE_PHDR
+       const struct build_id_note *note = NULL;
+       if ((note = build_id_find_nhdr_for_addr(ptr))) {
+               _mesa_sha1_update(ctx, build_id_data(note), build_id_length(note));
+       } else
+#endif
+       if (disk_cache_get_function_timestamp(ptr, &timestamp)) {
+               _mesa_sha1_update(ctx, &timestamp, sizeof(timestamp));
+       } else
+               return false;
+       return true;
+}
 
 static int
 radv_device_get_cache_uuid(enum radeon_family family, void *uuid)
 {
-       uint32_t mesa_timestamp, llvm_timestamp;
-       uint16_t f = family;
+       struct mesa_sha1 ctx;
+       unsigned char sha1[20];
+       unsigned ptr_size = sizeof(void*);
+
        memset(uuid, 0, VK_UUID_SIZE);
-       if (!disk_cache_get_function_timestamp(radv_device_get_cache_uuid, &mesa_timestamp) ||
-           !disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo, &llvm_timestamp))
+       _mesa_sha1_init(&ctx);
+
+       if (!radv_get_build_id(radv_device_get_cache_uuid, &ctx) ||
+           !radv_get_build_id(LLVMInitializeAMDGPUTargetInfo, &ctx))
                return -1;
 
-       memcpy(uuid, &mesa_timestamp, 4);
-       memcpy((char*)uuid + 4, &llvm_timestamp, 4);
-       memcpy((char*)uuid + 8, &f, 2);
-       snprintf((char*)uuid + 10, VK_UUID_SIZE - 10, "radv");
+       _mesa_sha1_update(&ctx, &family, sizeof(family));
+       _mesa_sha1_update(&ctx, &ptr_size, sizeof(ptr_size));
+       _mesa_sha1_final(&ctx, sha1);
+
+       memcpy(uuid, sha1, VK_UUID_SIZE);
        return 0;
 }
 
@@ -225,6 +251,7 @@ radv_physical_device_init(struct radv_physical_device *device,
        VkResult result;
        drmVersionPtr version;
        int fd;
+       int master_fd = -1;
 
        fd = open(path, O_RDWR | O_CLOEXEC);
        if (fd < 0) {
@@ -271,6 +298,24 @@ radv_physical_device_init(struct radv_physical_device *device,
                goto fail;
        }
 
+       if (instance->enabled_extensions.KHR_display) {
+               master_fd = open(drm_device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC);
+               if (master_fd >= 0) {
+                       uint32_t accel_working = 0;
+                       struct drm_amdgpu_info request = {
+                               .return_pointer = (uintptr_t)&accel_working,
+                               .return_size = sizeof(accel_working),
+                               .query = AMDGPU_INFO_ACCEL_WORKING
+                       };
+
+                       if (drmCommandWrite(master_fd, DRM_AMDGPU_INFO, &request, sizeof (struct drm_amdgpu_info)) < 0 || !accel_working) {
+                               close(master_fd);
+                               master_fd = -1;
+                       }
+               }
+       }
+
+       device->master_fd = master_fd;
        device->local_fd = fd;
        device->ws->query_info(device->ws, &device->rad_info);
 
@@ -329,12 +374,21 @@ radv_physical_device_init(struct radv_physical_device *device,
        device->out_of_order_rast_allowed = device->has_out_of_order_rast &&
                                            !(device->instance->debug_flags & RADV_DEBUG_NO_OUT_OF_ORDER);
 
-       device->dcc_msaa_allowed = device->rad_info.chip_class == VI &&
-                                  (device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
+       device->dcc_msaa_allowed =
+               (device->instance->perftest_flags & RADV_PERFTEST_DCC_MSAA);
 
        radv_physical_device_init_mem_types(device);
        radv_fill_device_extension_table(device, &device->supported_extensions);
 
+       device->bus_info = *drm_device->businfo.pci;
+
+       if ((device->instance->debug_flags & RADV_DEBUG_INFO))
+               ac_print_gpu_info(&device->rad_info);
+
+       /* The WSI is structured as a layer on top of the driver, so this has
+        * to be the last part of initialization (at least until we get other
+        * semi-layers).
+        */
        result = radv_init_wsi(device);
        if (result != VK_SUCCESS) {
                device->ws->destroy(device->ws);
@@ -342,13 +396,12 @@ radv_physical_device_init(struct radv_physical_device *device,
                goto fail;
        }
 
-       if ((device->instance->debug_flags & RADV_DEBUG_INFO))
-               ac_print_gpu_info(&device->rad_info);
-
        return VK_SUCCESS;
 
 fail:
        close(fd);
+       if (master_fd != -1)
+               close(master_fd);
        return result;
 }
 
@@ -359,6 +412,8 @@ radv_physical_device_finish(struct radv_physical_device *device)
        device->ws->destroy(device->ws);
        disk_cache_destroy(device->disk_cache);
        close(device->local_fd);
+       if (device->master_fd != -1)
+               close(device->master_fd);
 }
 
 static void *
@@ -410,6 +465,8 @@ static const struct debug_control radv_debug_options[] = {
        {"info", RADV_DEBUG_INFO},
        {"errors", RADV_DEBUG_ERRORS},
        {"startup", RADV_DEBUG_STARTUP},
+       {"checkir", RADV_DEBUG_CHECKIR},
+       {"nothreadllvm", RADV_DEBUG_NOTHREADLLVM},
        {NULL, 0}
 };
 
@@ -432,7 +489,7 @@ static const struct debug_control radv_perftest_options[] = {
 const char *
 radv_get_perftest_option_name(int id)
 {
-       assert(id < ARRAY_SIZE(radv_debug_options) - 1);
+       assert(id < ARRAY_SIZE(radv_perftest_options) - 1);
        return radv_perftest_options[id].string;
 }
 
@@ -453,6 +510,9 @@ radv_handle_per_app_options(struct radv_instance *instance,
                         */
                        instance->perftest_flags |= RADV_PERFTEST_SISCHED;
                }
+       } else if (!strcmp(name, "DOOM_VFR")) {
+               /* Work around a Doom VFR game bug */
+               instance->debug_flags |= RADV_DEBUG_NO_DYNAMIC_BOUNDS;
        }
 }
 
@@ -658,6 +718,7 @@ void radv_GetPhysicalDeviceFeatures(
        VkPhysicalDevice                            physicalDevice,
        VkPhysicalDeviceFeatures*                   pFeatures)
 {
+       RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
        memset(pFeatures, 0, sizeof(*pFeatures));
 
        *pFeatures = (VkPhysicalDeviceFeatures) {
@@ -681,7 +742,8 @@ void radv_GetPhysicalDeviceFeatures(
                .alphaToOne                               = true,
                .multiViewport                            = true,
                .samplerAnisotropy                        = true,
-               .textureCompressionETC2                   = false,
+               .textureCompressionETC2                   = pdevice->rad_info.chip_class >= GFX9 ||
+                                                           pdevice->rad_info.family == CHIP_STONEY,
                .textureCompressionASTC_LDR               = false,
                .textureCompressionBC                     = true,
                .occlusionQueryPrecise                    = true,
@@ -702,7 +764,7 @@ void radv_GetPhysicalDeviceFeatures(
                .shaderCullDistance                       = true,
                .shaderFloat64                            = true,
                .shaderInt64                              = true,
-               .shaderInt16                              = false,
+               .shaderInt16                              = pdevice->rad_info.chip_class >= GFX9 && HAVE_LLVM >= 0x700,
                .sparseBinding                            = true,
                .variableMultisampleRate                  = true,
                .inheritedQueries                         = true,
@@ -713,6 +775,7 @@ void radv_GetPhysicalDeviceFeatures2(
        VkPhysicalDevice                            physicalDevice,
        VkPhysicalDeviceFeatures2KHR               *pFeatures)
 {
+       RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
        vk_foreach_struct(ext, pFeatures->pNext) {
                switch (ext->sType) {
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES_KHR: {
@@ -743,10 +806,11 @@ void radv_GetPhysicalDeviceFeatures2(
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
                        VkPhysicalDevice16BitStorageFeatures *features =
                            (VkPhysicalDevice16BitStorageFeatures*)ext;
-                       features->storageBuffer16BitAccess = false;
-                       features->uniformAndStorageBuffer16BitAccess = false;
-                       features->storagePushConstant16 = false;
-                       features->storageInputOutput16 = false;
+                       bool enabled = HAVE_LLVM >= 0x0700 && pdevice->rad_info.chip_class >= VI;
+                       features->storageBuffer16BitAccess = enabled;
+                       features->uniformAndStorageBuffer16BitAccess = enabled;
+                       features->storagePushConstant16 = enabled;
+                       features->storageInputOutput16 = enabled;
                        break;
                }
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
@@ -780,6 +844,20 @@ void radv_GetPhysicalDeviceFeatures2(
                        features->runtimeDescriptorArray = true;
                        break;
                }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONDITIONAL_RENDERING_FEATURES_EXT: {
+                       VkPhysicalDeviceConditionalRenderingFeaturesEXT *features =
+                               (VkPhysicalDeviceConditionalRenderingFeaturesEXT*)ext;
+                       features->conditionalRendering = true;
+                       features->inheritedConditionalRendering = false;
+                       break;
+               }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
+                       VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
+                               (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
+                       features->vertexAttributeInstanceRateDivisor = VK_TRUE;
+                       features->vertexAttributeInstanceRateZeroDivisor = VK_TRUE;
+                       break;
+               }
                default:
                        break;
                }
@@ -908,7 +986,7 @@ void radv_GetPhysicalDeviceProperties(
                .maxClipDistances                         = 8,
                .maxCullDistances                         = 8,
                .maxCombinedClipAndCullDistances          = 8,
-               .discreteQueuePriorities                  = 1,
+               .discreteQueuePriorities                  = 2,
                .pointSizeRange                           = { 0.125, 255.875 },
                .lineWidthRange                           = { 0.0, 7.9921875 },
                .pointSizeGranularity                     = (1.0 / 8.0),
@@ -985,11 +1063,14 @@ void radv_GetPhysicalDeviceProperties2(
                            (VkPhysicalDeviceSubgroupProperties*)ext;
                        properties->subgroupSize = 64;
                        properties->supportedStages = VK_SHADER_STAGE_ALL;
+                       /* TODO: Enable VK_SUBGROUP_FEATURE_VOTE_BIT when wwm
+                        * is fixed in LLVM.
+                        */
                        properties->supportedOperations =
+                                                       VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
                                                        VK_SUBGROUP_FEATURE_BASIC_BIT |
                                                        VK_SUBGROUP_FEATURE_BALLOT_BIT |
-                                                       VK_SUBGROUP_FEATURE_QUAD_BIT |
-                                                       VK_SUBGROUP_FEATURE_VOTE_BIT;
+                                                       VK_SUBGROUP_FEATURE_QUAD_BIT;
                        if (pdevice->rad_info.chip_class >= VI) {
                                properties->supportedOperations |=
                                                        VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
@@ -1098,6 +1179,58 @@ void radv_GetPhysicalDeviceProperties2(
                        properties->maxDescriptorSetUpdateAfterBindInputAttachments = max_descriptor_set_size;
                        break;
                }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
+                       VkPhysicalDeviceProtectedMemoryProperties *properties =
+                               (VkPhysicalDeviceProtectedMemoryProperties *)ext;
+                       properties->protectedNoFault = false;
+                       break;
+               }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CONSERVATIVE_RASTERIZATION_PROPERTIES_EXT: {
+                       VkPhysicalDeviceConservativeRasterizationPropertiesEXT *properties =
+                               (VkPhysicalDeviceConservativeRasterizationPropertiesEXT *)ext;
+                       properties->primitiveOverestimationSize = 0;
+                       properties->maxExtraPrimitiveOverestimationSize = 0;
+                       properties->extraPrimitiveOverestimationSizeGranularity = 0;
+                       properties->primitiveUnderestimation = VK_FALSE;
+                       properties->conservativePointAndLineRasterization = VK_FALSE;
+                       properties->degenerateTrianglesRasterized = VK_FALSE;
+                       properties->degenerateLinesRasterized = VK_FALSE;
+                       properties->fullyCoveredFragmentShaderInputVariable = VK_FALSE;
+                       properties->conservativeRasterizationPostDepthCoverage = VK_FALSE;
+                       break;
+               }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
+                       VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
+                               (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
+                       properties->pciDomain = pdevice->bus_info.domain;
+                       properties->pciBus = pdevice->bus_info.bus;
+                       properties->pciDevice = pdevice->bus_info.dev;
+                       properties->pciFunction = pdevice->bus_info.func;
+                       break;
+               }
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
+                       VkPhysicalDeviceDriverPropertiesKHR *driver_props =
+                               (VkPhysicalDeviceDriverPropertiesKHR *) ext;
+
+                       driver_props->driverID = VK_DRIVER_ID_MESA_RADV_KHR;
+                       memset(driver_props->driverName, 0, VK_MAX_DRIVER_NAME_SIZE_KHR);
+                       strcpy(driver_props->driverName, "radv");
+
+                       memset(driver_props->driverInfo, 0, VK_MAX_DRIVER_INFO_SIZE_KHR);
+                       snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
+                               "Mesa " PACKAGE_VERSION " (" MESA_GIT_SHA1 ")"
+                               " (LLVM %d.%d.%d)",
+                                (HAVE_LLVM >> 8) & 0xff, HAVE_LLVM & 0xff,
+                                MESA_LLVM_VERSION_PATCH);
+
+                       driver_props->conformanceVersion = (VkConformanceVersionKHR) {
+                               .major = 1,
+                               .minor = 1,
+                               .subminor = 2,
+                               .patch = 0,
+                       };
+                       break;
+               }
                default:
                        break;
                }
@@ -1381,6 +1514,28 @@ static int radv_get_device_extension_index(const char *name)
        return -1;
 }
 
+static int
+radv_get_int_debug_option(const char *name, int default_value)
+{
+       const char *str;
+       int result;
+
+       str = getenv(name);
+       if (!str) {
+               result = default_value;
+       } else {
+               char *endptr;
+
+               result = strtol(str, &endptr, 0);
+               if (str == endptr) {
+                       /* No digits founs. */
+                       result = default_value;
+               }
+       }
+
+       return result;
+}
+
 VkResult radv_CreateDevice(
        VkPhysicalDevice                            physicalDevice,
        const VkDeviceCreateInfo*                   pCreateInfo,
@@ -1475,10 +1630,12 @@ VkResult radv_CreateDevice(
        }
 
        device->pbb_allowed = device->physical_device->rad_info.chip_class >= GFX9 &&
-                             (device->instance->perftest_flags & RADV_PERFTEST_BINNING);
+                       ((device->instance->perftest_flags & RADV_PERFTEST_BINNING) ||
+                        device->physical_device->rad_info.family == CHIP_RAVEN);
 
        /* Disabled and not implemented for now. */
-       device->dfsm_allowed = device->pbb_allowed && false;
+       device->dfsm_allowed = device->pbb_allowed &&
+                              device->physical_device->rad_info.family == CHIP_RAVEN;
 
 #ifdef ANDROID
        device->always_use_syncobj = device->physical_device->rad_info.has_syncobj_wait_for_submit;
@@ -1525,6 +1682,10 @@ VkResult radv_CreateDevice(
                if (!radv_init_trace(device))
                        goto fail;
 
+               fprintf(stderr, "*****************************************************************************\n");
+               fprintf(stderr, "* WARNING: RADV_TRACE_FILE is costly and should only be used for debugging! *\n");
+               fprintf(stderr, "*****************************************************************************\n");
+
                fprintf(stderr, "Trace file will be dumped to %s\n", filename);
                radv_dump_enabled_options(device, stderr);
        }
@@ -1570,6 +1731,13 @@ VkResult radv_CreateDevice(
 
        device->mem_cache = radv_pipeline_cache_from_handle(pc);
 
+       device->force_aniso =
+               MIN2(16, radv_get_int_debug_option("RADV_TEX_ANISO", -1));
+       if (device->force_aniso >= 0) {
+               fprintf(stderr, "radv: Forcing anisotropy filter to %ix\n",
+                       1 << util_logbase2(device->force_aniso));
+       }
+
        *pDevice = radv_device_to_handle(device);
        return VK_SUCCESS;
 
@@ -1848,10 +2016,30 @@ radv_get_hs_offchip_param(struct radv_device *device, uint32_t *max_offchip_buff
                device->physical_device->rad_info.family != CHIP_CARRIZO &&
                device->physical_device->rad_info.family != CHIP_STONEY;
        unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
-       unsigned max_offchip_buffers = max_offchip_buffers_per_se *
-               device->physical_device->rad_info.max_se;
+       unsigned max_offchip_buffers;
        unsigned offchip_granularity;
        unsigned hs_offchip_param;
+
+       /*
+        * Per RadeonSI:
+        * This must be one less than the maximum number due to a hw limitation.
+         * Various hardware bugs in SI, CIK, and GFX9 need this.
+        *
+        * Per AMDVLK:
+        * Vega10 should limit max_offchip_buffers to 508 (4 * 127).
+        * Gfx7 should limit max_offchip_buffers to 508
+        * Gfx6 should limit max_offchip_buffers to 126 (2 * 63)
+        *
+        * Follow AMDVLK here.
+        */
+       if (device->physical_device->rad_info.family == CHIP_VEGA10 ||
+           device->physical_device->rad_info.chip_class == CIK ||
+           device->physical_device->rad_info.chip_class == SI)
+               --max_offchip_buffers_per_se;
+
+       max_offchip_buffers = max_offchip_buffers_per_se *
+               device->physical_device->rad_info.max_se;
+
        switch (device->tess_offchip_block_dw_size) {
        default:
                assert(0);
@@ -1891,7 +2079,7 @@ radv_get_hs_offchip_param(struct radv_device *device, uint32_t *max_offchip_buff
 }
 
 static void
-radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_winsys_cs *cs,
+radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_cmdbuf *cs,
                        struct radeon_winsys_bo *esgs_ring_bo,
                        uint32_t esgs_ring_size,
                        struct radeon_winsys_bo *gsvs_ring_bo,
@@ -1901,10 +2089,10 @@ radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_winsys_cs *cs,
                return;
 
        if (esgs_ring_bo)
-               radv_cs_add_buffer(queue->device->ws, cs, esgs_ring_bo, 8);
+               radv_cs_add_buffer(queue->device->ws, cs, esgs_ring_bo);
 
        if (gsvs_ring_bo)
-               radv_cs_add_buffer(queue->device->ws, cs, gsvs_ring_bo, 8);
+               radv_cs_add_buffer(queue->device->ws, cs, gsvs_ring_bo);
 
        if (queue->device->physical_device->rad_info.chip_class >= CIK) {
                radeon_set_uconfig_reg_seq(cs, R_030900_VGT_ESGS_RING_SIZE, 2);
@@ -1918,7 +2106,7 @@ radv_emit_gs_ring_sizes(struct radv_queue *queue, struct radeon_winsys_cs *cs,
 }
 
 static void
-radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_winsys_cs *cs,
+radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_cmdbuf *cs,
                           unsigned hs_offchip_param, unsigned tf_ring_size,
                           struct radeon_winsys_bo *tess_rings_bo)
 {
@@ -1929,7 +2117,7 @@ radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_winsys_cs *cs
 
        tf_va = radv_buffer_get_va(tess_rings_bo);
 
-       radv_cs_add_buffer(queue->device->ws, cs, tess_rings_bo, 8);
+       radv_cs_add_buffer(queue->device->ws, cs, tess_rings_bo);
 
        if (queue->device->physical_device->rad_info.chip_class >= CIK) {
                radeon_set_uconfig_reg(cs, R_030938_VGT_TF_RING_SIZE,
@@ -1953,7 +2141,7 @@ radv_emit_tess_factor_ring(struct radv_queue *queue, struct radeon_winsys_cs *cs
 }
 
 static void
-radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_winsys_cs *cs,
+radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_cmdbuf *cs,
                          struct radeon_winsys_bo *compute_scratch_bo)
 {
        uint64_t scratch_va;
@@ -1963,7 +2151,7 @@ radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_winsys_cs *cs,
 
        scratch_va = radv_buffer_get_va(compute_scratch_bo);
 
-       radv_cs_add_buffer(queue->device->ws, cs, compute_scratch_bo, 8);
+       radv_cs_add_buffer(queue->device->ws, cs, compute_scratch_bo);
 
        radeon_set_sh_reg_seq(cs, R_00B900_COMPUTE_USER_DATA_0, 2);
        radeon_emit(cs, scratch_va);
@@ -1973,7 +2161,7 @@ radv_emit_compute_scratch(struct radv_queue *queue, struct radeon_winsys_cs *cs,
 
 static void
 radv_emit_global_shader_pointers(struct radv_queue *queue,
-                                struct radeon_winsys_cs *cs,
+                                struct radeon_cmdbuf *cs,
                                 struct radeon_winsys_bo *descriptor_bo)
 {
        uint64_t va;
@@ -1983,7 +2171,7 @@ radv_emit_global_shader_pointers(struct radv_queue *queue,
 
        va = radv_buffer_get_va(descriptor_bo);
 
-       radv_cs_add_buffer(queue->device->ws, cs, descriptor_bo, 8);
+       radv_cs_add_buffer(queue->device->ws, cs, descriptor_bo);
 
        if (queue->device->physical_device->rad_info.chip_class >= GFX9) {
                uint32_t regs[] = {R_00B030_SPI_SHADER_USER_DATA_PS_0,
@@ -2010,6 +2198,33 @@ radv_emit_global_shader_pointers(struct radv_queue *queue,
        }
 }
 
+static void
+radv_init_graphics_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
+{
+       struct radv_device *device = queue->device;
+
+       if (device->gfx_init) {
+               uint64_t va = radv_buffer_get_va(device->gfx_init);
+
+               radeon_emit(cs, PKT3(PKT3_INDIRECT_BUFFER_CIK, 2, 0));
+               radeon_emit(cs, va);
+               radeon_emit(cs, va >> 32);
+               radeon_emit(cs, device->gfx_init_size_dw & 0xffff);
+
+               radv_cs_add_buffer(device->ws, cs, device->gfx_init);
+       } else {
+               struct radv_physical_device *physical_device = device->physical_device;
+               si_emit_graphics(physical_device, cs);
+       }
+}
+
+static void
+radv_init_compute_state(struct radeon_cmdbuf *cs, struct radv_queue *queue)
+{
+       struct radv_physical_device *physical_device = queue->device->physical_device;
+       si_emit_compute(physical_device, cs);
+}
+
 static VkResult
 radv_get_preamble_cs(struct radv_queue *queue,
                      uint32_t scratch_size,
@@ -2018,9 +2233,9 @@ radv_get_preamble_cs(struct radv_queue *queue,
                     uint32_t gsvs_ring_size,
                     bool needs_tess_rings,
                     bool needs_sample_positions,
-                    struct radeon_winsys_cs **initial_full_flush_preamble_cs,
-                     struct radeon_winsys_cs **initial_preamble_cs,
-                     struct radeon_winsys_cs **continue_preamble_cs)
+                    struct radeon_cmdbuf **initial_full_flush_preamble_cs,
+                     struct radeon_cmdbuf **initial_preamble_cs,
+                     struct radeon_cmdbuf **continue_preamble_cs)
 {
        struct radeon_winsys_bo *scratch_bo = NULL;
        struct radeon_winsys_bo *descriptor_bo = NULL;
@@ -2028,7 +2243,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
        struct radeon_winsys_bo *esgs_ring_bo = NULL;
        struct radeon_winsys_bo *gsvs_ring_bo = NULL;
        struct radeon_winsys_bo *tess_rings_bo = NULL;
-       struct radeon_winsys_cs *dest_cs[3] = {0};
+       struct radeon_cmdbuf *dest_cs[3] = {0};
        bool add_tess_rings = false, add_sample_positions = false;
        unsigned tess_factor_ring_size = 0, tess_offchip_ring_size = 0;
        unsigned max_offchip_buffers;
@@ -2153,7 +2368,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
                descriptor_bo = queue->descriptor_bo;
 
        for(int i = 0; i < 3; ++i) {
-               struct radeon_winsys_cs *cs = NULL;
+               struct radeon_cmdbuf *cs = NULL;
                cs = queue->device->ws->cs_create(queue->device->ws,
                                                  queue->queue_family_index ? RING_COMPUTE : RING_GFX);
                if (!cs)
@@ -2162,7 +2377,19 @@ radv_get_preamble_cs(struct radv_queue *queue,
                dest_cs[i] = cs;
 
                if (scratch_bo)
-                       radv_cs_add_buffer(queue->device->ws, cs, scratch_bo, 8);
+                       radv_cs_add_buffer(queue->device->ws, cs, scratch_bo);
+
+               /* Emit initial configuration. */
+               switch (queue->queue_family_index) {
+               case RADV_QUEUE_GENERAL:
+                       radv_init_graphics_state(cs, queue);
+                       break;
+               case RADV_QUEUE_COMPUTE:
+                       radv_init_compute_state(cs, queue);
+                       break;
+               case RADV_QUEUE_TRANSFER:
+                       break;
+               }
 
                if (descriptor_bo != queue->descriptor_bo) {
                        uint32_t *map = (uint32_t*)queue->device->ws->buffer_map(descriptor_bo);
@@ -2212,7 +2439,8 @@ radv_get_preamble_cs(struct radv_queue *queue,
                                               RADV_CMD_FLAG_INV_ICACHE |
                                               RADV_CMD_FLAG_INV_SMEM_L1 |
                                               RADV_CMD_FLAG_INV_VMEM_L1 |
-                                              RADV_CMD_FLAG_INV_GLOBAL_L2);
+                                              RADV_CMD_FLAG_INV_GLOBAL_L2 |
+                                              RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
                } else if (i == 1) {
                        si_cs_emit_cache_flush(cs,
                                               queue->device->physical_device->rad_info.chip_class,
@@ -2222,7 +2450,8 @@ radv_get_preamble_cs(struct radv_queue *queue,
                                               RADV_CMD_FLAG_INV_ICACHE |
                                               RADV_CMD_FLAG_INV_SMEM_L1 |
                                               RADV_CMD_FLAG_INV_VMEM_L1 |
-                                              RADV_CMD_FLAG_INV_GLOBAL_L2);
+                                              RADV_CMD_FLAG_INV_GLOBAL_L2 |
+                                              RADV_CMD_FLAG_START_PIPELINE_STATS, 0);
                }
 
                if (!queue->device->ws->cs_finalize(cs))
@@ -2444,9 +2673,8 @@ static VkResult radv_signal_fence(struct radv_queue *queue,
                                           false, fence->fence);
        radv_free_sem_info(&sem_info);
 
-       /* TODO: find a better error */
        if (ret)
-               return vk_error(queue->device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
+               return vk_error(queue->device->instance, VK_ERROR_DEVICE_LOST);
 
        return VK_SUCCESS;
 }
@@ -2466,7 +2694,7 @@ VkResult radv_QueueSubmit(
        uint32_t scratch_size = 0;
        uint32_t compute_scratch_size = 0;
        uint32_t esgs_ring_size = 0, gsvs_ring_size = 0;
-       struct radeon_winsys_cs *initial_preamble_cs = NULL, *initial_flush_preamble_cs = NULL, *continue_preamble_cs = NULL;
+       struct radeon_cmdbuf *initial_preamble_cs = NULL, *initial_flush_preamble_cs = NULL, *continue_preamble_cs = NULL;
        VkResult result;
        bool fence_emitted = false;
        bool tess_rings_needed = false;
@@ -2497,7 +2725,7 @@ VkResult radv_QueueSubmit(
                return result;
 
        for (uint32_t i = 0; i < submitCount; i++) {
-               struct radeon_winsys_cs **cs_array;
+               struct radeon_cmdbuf **cs_array;
                bool do_flush = !i || pSubmits[i].pWaitDstStageMask;
                bool can_patch = true;
                uint32_t advance;
@@ -2530,7 +2758,7 @@ VkResult radv_QueueSubmit(
                        continue;
                }
 
-               cs_array = malloc(sizeof(struct radeon_winsys_cs *) *
+               cs_array = malloc(sizeof(struct radeon_cmdbuf *) *
                                                (pSubmits[i].commandBufferCount));
 
                for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
@@ -2546,7 +2774,7 @@ VkResult radv_QueueSubmit(
                }
 
                for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j += advance) {
-                       struct radeon_winsys_cs *initial_preamble = (do_flush && !j) ? initial_flush_preamble_cs : initial_preamble_cs;
+                       struct radeon_cmdbuf *initial_preamble = (do_flush && !j) ? initial_flush_preamble_cs : initial_preamble_cs;
                        const struct radv_winsys_bo_list *bo_list = NULL;
 
                        advance = MIN2(max_cs_submission,
@@ -2590,7 +2818,9 @@ VkResult radv_QueueSubmit(
 
        if (fence) {
                if (!fence_emitted) {
-                       radv_signal_fence(queue, fence);
+                       result = radv_signal_fence(queue, fence);
+                       if (result != VK_SUCCESS)
+                               return result;
                }
                fence->submitted = true;
        }
@@ -3147,6 +3377,8 @@ radv_sparse_image_opaque_bind_memory(struct radv_device *device,
        RADV_FROM_HANDLE(radv_queue, queue, _queue);
        struct radeon_winsys_fence *base_fence = fence ? fence->fence : NULL;
        bool fence_emitted = false;
+       VkResult result;
+       int ret;
 
        for (uint32_t i = 0; i < bindInfoCount; ++i) {
                struct radv_winsys_sem_info sem_info;
@@ -3172,11 +3404,16 @@ radv_sparse_image_opaque_bind_memory(struct radv_device *device,
                        return result;
 
                if (pBindInfo[i].waitSemaphoreCount || pBindInfo[i].signalSemaphoreCount) {
-                       queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
-                                                    &queue->device->empty_cs[queue->queue_family_index],
-                                                    1, NULL, NULL,
-                                                    &sem_info, NULL,
-                                                    false, base_fence);
+                       ret = queue->device->ws->cs_submit(queue->hw_ctx, queue->queue_idx,
+                                                         &queue->device->empty_cs[queue->queue_family_index],
+                                                         1, NULL, NULL,
+                                                         &sem_info, NULL,
+                                                         false, base_fence);
+                       if (ret) {
+                               radv_loge("failed to submit CS %d\n", i);
+                               abort();
+                       }
+
                        fence_emitted = true;
                        if (fence)
                                fence->submitted = true;
@@ -3188,7 +3425,9 @@ radv_sparse_image_opaque_bind_memory(struct radv_device *device,
 
        if (fence) {
                if (!fence_emitted) {
-                       radv_signal_fence(queue, fence);
+                       result = radv_signal_fence(queue, fence);
+                       if (result != VK_SUCCESS)
+                               return result;
                }
                fence->submitted = true;
        }
@@ -3215,6 +3454,7 @@ VkResult radv_CreateFence(
        if (!fence)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       fence->fence_wsi = NULL;
        fence->submitted = false;
        fence->signalled = !!(pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT);
        fence->temp_syncobj = 0;
@@ -3259,6 +3499,8 @@ void radv_DestroyFence(
                device->ws->destroy_syncobj(device->ws, fence->syncobj);
        if (fence->fence)
                device->ws->destroy_fence(fence->fence);
+       if (fence->fence_wsi)
+               fence->fence_wsi->destroy(fence->fence_wsi);
        vk_free2(&device->alloc, pAllocator, fence);
 }
 
@@ -3284,7 +3526,19 @@ static bool radv_all_fences_plain_and_submitted(uint32_t fenceCount, const VkFen
 {
        for (uint32_t i = 0; i < fenceCount; ++i) {
                RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
-               if (fence->syncobj || fence->temp_syncobj || (!fence->signalled && !fence->submitted))
+               if (fence->fence == NULL || fence->syncobj ||
+                   fence->temp_syncobj ||
+                   (!fence->signalled && !fence->submitted))
+                       return false;
+       }
+       return true;
+}
+
+static bool radv_all_fences_syncobj(uint32_t fenceCount, const VkFence *pFences)
+{
+       for (uint32_t i = 0; i < fenceCount; ++i) {
+               RADV_FROM_HANDLE(radv_fence, fence, pFences[i]);
+               if (fence->syncobj == 0 && fence->temp_syncobj == 0)
                        return false;
        }
        return true;
@@ -3300,7 +3554,9 @@ VkResult radv_WaitForFences(
        RADV_FROM_HANDLE(radv_device, device, _device);
        timeout = radv_get_absolute_timeout(timeout);
 
-       if (device->always_use_syncobj) {
+       if (device->always_use_syncobj &&
+           radv_all_fences_syncobj(fenceCount, pFences))
+       {
                uint32_t *handles = malloc(sizeof(uint32_t) * fenceCount);
                if (!handles)
                        return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
@@ -3370,21 +3626,34 @@ VkResult radv_WaitForFences(
                if (fence->signalled)
                        continue;
 
-               if (!fence->submitted) {
-                       while(radv_get_current_time() <= timeout && !fence->submitted)
-                               /* Do nothing */;
+               if (fence->fence) {
+                       if (!fence->submitted) {
+                               while(radv_get_current_time() <= timeout &&
+                                     !fence->submitted)
+                                       /* Do nothing */;
 
-                       if (!fence->submitted)
-                               return VK_TIMEOUT;
+                               if (!fence->submitted)
+                                       return VK_TIMEOUT;
+
+                               /* Recheck as it may have been set by
+                                * submitting operations. */
+
+                               if (fence->signalled)
+                                       continue;
+                       }
 
-                       /* Recheck as it may have been set by submitting operations. */
-                       if (fence->signalled)
-                               continue;
+                       expired = device->ws->fence_wait(device->ws,
+                                                        fence->fence,
+                                                        true, timeout);
+                       if (!expired)
+                               return VK_TIMEOUT;
                }
 
-               expired = device->ws->fence_wait(device->ws, fence->fence, true, timeout);
-               if (!expired)
-                       return VK_TIMEOUT;
+               if (fence->fence_wsi) {
+                       VkResult result = fence->fence_wsi->wait(fence->fence_wsi, timeout);
+                       if (result != VK_SUCCESS)
+                               return result;
+               }
 
                fence->signalled = true;
        }
@@ -3436,9 +3705,19 @@ VkResult radv_GetFenceStatus(VkDevice _device, VkFence _fence)
                return VK_SUCCESS;
        if (!fence->submitted)
                return VK_NOT_READY;
-       if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
-               return VK_NOT_READY;
+       if (fence->fence) {
+               if (!device->ws->fence_wait(device->ws, fence->fence, false, 0))
+                       return VK_NOT_READY;
+       }
+       if (fence->fence_wsi) {
+               VkResult result = fence->fence_wsi->wait(fence->fence_wsi, 0);
 
+               if (result != VK_SUCCESS) {
+                       if (result == VK_TIMEOUT)
+                               return VK_NOT_READY;
+                       return result;
+               }
+       }
        return VK_SUCCESS;
 }
 
@@ -3657,7 +3936,7 @@ radv_init_dcc_control_reg(struct radv_device *device,
        unsigned max_compressed_block_size;
        unsigned independent_64b_blocks;
 
-       if (device->physical_device->rad_info.chip_class < VI)
+       if (!radv_image_has_dcc(iview->image))
                return 0;
 
        if (iview->image->info.samples > 1) {
@@ -4257,13 +4536,26 @@ radv_tex_filter_mode(VkSamplerReductionModeEXT mode)
        return 0;
 }
 
+static uint32_t
+radv_get_max_anisotropy(struct radv_device *device,
+                       const VkSamplerCreateInfo *pCreateInfo)
+{
+       if (device->force_aniso >= 0)
+               return device->force_aniso;
+
+       if (pCreateInfo->anisotropyEnable &&
+           pCreateInfo->maxAnisotropy > 1.0f)
+               return (uint32_t)pCreateInfo->maxAnisotropy;
+
+       return 0;
+}
+
 static void
 radv_init_sampler(struct radv_device *device,
                  struct radv_sampler *sampler,
                  const VkSamplerCreateInfo *pCreateInfo)
 {
-       uint32_t max_aniso = pCreateInfo->anisotropyEnable && pCreateInfo->maxAnisotropy > 1.0 ?
-                                       (uint32_t) pCreateInfo->maxAnisotropy : 0;
+       uint32_t max_aniso = radv_get_max_anisotropy(device, pCreateInfo);
        uint32_t max_aniso_ratio = radv_tex_aniso_filter(max_aniso);
        bool is_vi = (device->physical_device->rad_info.chip_class >= VI);
        unsigned filter_mode = SQ_IMG_FILTER_MODE_BLEND;
@@ -4694,3 +4986,122 @@ radv_GetDeviceGroupPeerMemoryFeatures(
                               VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
                               VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
 }
+
+static const VkTimeDomainEXT radv_time_domains[] = {
+       VK_TIME_DOMAIN_DEVICE_EXT,
+       VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
+       VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
+};
+
+VkResult radv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
+       VkPhysicalDevice                             physicalDevice,
+       uint32_t                                     *pTimeDomainCount,
+       VkTimeDomainEXT                              *pTimeDomains)
+{
+       int d;
+       VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
+
+       for (d = 0; d < ARRAY_SIZE(radv_time_domains); d++) {
+               vk_outarray_append(&out, i) {
+                       *i = radv_time_domains[d];
+               }
+       }
+
+       return vk_outarray_status(&out);
+}
+
+static uint64_t
+radv_clock_gettime(clockid_t clock_id)
+{
+       struct timespec current;
+       int ret;
+
+       ret = clock_gettime(clock_id, &current);
+       if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
+               ret = clock_gettime(CLOCK_MONOTONIC, &current);
+       if (ret < 0)
+               return 0;
+
+       return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
+}
+
+VkResult radv_GetCalibratedTimestampsEXT(
+       VkDevice                                     _device,
+       uint32_t                                     timestampCount,
+       const VkCalibratedTimestampInfoEXT           *pTimestampInfos,
+       uint64_t                                     *pTimestamps,
+       uint64_t                                     *pMaxDeviation)
+{
+       RADV_FROM_HANDLE(radv_device, device, _device);
+       uint32_t clock_crystal_freq = device->physical_device->rad_info.clock_crystal_freq;
+       int d;
+       uint64_t begin, end;
+        uint64_t max_clock_period = 0;
+
+       begin = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
+
+       for (d = 0; d < timestampCount; d++) {
+               switch (pTimestampInfos[d].timeDomain) {
+               case VK_TIME_DOMAIN_DEVICE_EXT:
+                       pTimestamps[d] = device->ws->query_value(device->ws,
+                                                                RADEON_TIMESTAMP);
+                        uint64_t device_period = DIV_ROUND_UP(1000000, clock_crystal_freq);
+                        max_clock_period = MAX2(max_clock_period, device_period);
+                       break;
+               case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
+                       pTimestamps[d] = radv_clock_gettime(CLOCK_MONOTONIC);
+                        max_clock_period = MAX2(max_clock_period, 1);
+                       break;
+
+               case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
+                       pTimestamps[d] = begin;
+                       break;
+               default:
+                       pTimestamps[d] = 0;
+                       break;
+               }
+       }
+
+       end = radv_clock_gettime(CLOCK_MONOTONIC_RAW);
+
+        /*
+         * The maximum deviation is the sum of the interval over which we
+         * perform the sampling and the maximum period of any sampled
+         * clock. That's because the maximum skew between any two sampled
+         * clock edges is when the sampled clock with the largest period is
+         * sampled at the end of that period but right at the beginning of the
+         * sampling interval and some other clock is sampled right at the
+         * begining of its sampling period and right at the end of the
+         * sampling interval. Let's assume the GPU has the longest clock
+         * period and that the application is sampling GPU and monotonic:
+         *
+         *                               s                 e
+         *                      w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
+         *     Raw              -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
+         *
+         *                               g
+         *               0         1         2         3
+         *     GPU       -----_____-----_____-----_____-----_____
+         *
+         *                                                m
+         *                                         x y z 0 1 2 3 4 5 6 7 8 9 a b c
+         *     Monotonic                           -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
+         *
+         *     Interval                     <----------------->
+         *     Deviation           <-------------------------->
+         *
+         *             s  = read(raw)       2
+         *             g  = read(GPU)       1
+         *             m  = read(monotonic) 2
+         *             e  = read(raw)       b
+         *
+         * We round the sample interval up by one tick to cover sampling error
+         * in the interval clock
+         */
+
+        uint64_t sample_interval = end - begin + 1;
+
+        *pMaxDeviation = sample_interval + max_clock_period;
+
+       return VK_SUCCESS;
+}