amd: assume HTILE is always rb/pipe_aligned, remove ac_surface.u.gfx9.htile
[mesa.git] / src / amd / vulkan / radv_device.c
index e8bdc4d76c137551130398f409598240b120110b..d4b78ef06df985f8f419288aa25e55fc4b5c1c30 100644 (file)
@@ -239,9 +239,9 @@ radv_physical_device_init_mem_types(struct radv_physical_device *device)
 }
 
 static VkResult
-radv_physical_device_init(struct radv_physical_device *device,
-                         struct radv_instance *instance,
-                         drmDevicePtr drm_device)
+radv_physical_device_try_create(struct radv_instance *instance,
+                               drmDevicePtr drm_device,
+                               struct radv_physical_device **device_out)
 {
        VkResult result;
        int fd = -1;
@@ -285,6 +285,14 @@ radv_physical_device_init(struct radv_physical_device *device,
                                radv_logi("Found compatible device '%s'.", path);
        }
 
+       struct radv_physical_device *device =
+               vk_zalloc2(&instance->alloc, NULL, sizeof(*device), 8,
+                          VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+       if (!device) {
+               result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY);
+               goto fail_fd;
+       }
+
        device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
        device->instance = instance;
 
@@ -296,8 +304,9 @@ radv_physical_device_init(struct radv_physical_device *device,
        }
 
        if (!device->ws) {
-               result = vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
-               goto fail;
+               result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
+                                  "failed to initialize winsys");
+               goto fail_alloc;
        }
 
        if (drm_device && instance->enabled_extensions.KHR_display) {
@@ -328,10 +337,9 @@ radv_physical_device_init(struct radv_physical_device *device,
                 device->rad_info.name);
 
        if (radv_device_get_cache_uuid(device->rad_info.family, device->cache_uuid)) {
-               device->ws->destroy(device->ws);
                result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED,
                                   "cannot generate UUID");
-               goto fail;
+               goto fail_wsi;
        }
 
        /* These flags affect shader compilation. */
@@ -399,14 +407,21 @@ radv_physical_device_init(struct radv_physical_device *device,
         */
        result = radv_init_wsi(device);
        if (result != VK_SUCCESS) {
-               device->ws->destroy(device->ws);
                vk_error(instance, result);
-               goto fail;
+               goto fail_disk_cache;
        }
 
+       *device_out = device;
+
        return VK_SUCCESS;
 
-fail:
+fail_disk_cache:
+       disk_cache_destroy(device->disk_cache);
+fail_wsi:
+       device->ws->destroy(device->ws);
+fail_alloc:
+       vk_free(&instance->alloc, device);
+fail_fd:
        close(fd);
        if (master_fd != -1)
                close(master_fd);
@@ -414,7 +429,7 @@ fail:
 }
 
 static void
-radv_physical_device_finish(struct radv_physical_device *device)
+radv_physical_device_destroy(struct radv_physical_device *device)
 {
        radv_finish_wsi(device);
        device->ws->destroy(device->ws);
@@ -422,6 +437,7 @@ radv_physical_device_finish(struct radv_physical_device *device)
        close(device->local_fd);
        if (device->master_fd != -1)
                close(device->master_fd);
+       vk_free(&device->instance->alloc, device);
 }
 
 static void *
@@ -609,8 +625,6 @@ VkResult radv_CreateInstance(
        if (instance->apiVersion == 0)
                instance->apiVersion = VK_API_VERSION_1_0;
 
-       instance->physicalDeviceCount = -1;
-
        /* Get secure compile thread count. NOTE: We cap this at 32 */
 #define MAX_SC_PROCS 32
        char *num_sc_threads = getenv("RADV_SECURE_COMPILE_THREADS");
@@ -694,6 +708,9 @@ VkResult radv_CreateInstance(
                }
        }
 
+       instance->physical_devices_enumerated = false;
+       list_inithead(&instance->physical_devices);
+
        result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
        if (result != VK_SUCCESS) {
                vk_free2(&default_alloc, pAllocator, instance);
@@ -721,8 +738,9 @@ void radv_DestroyInstance(
        if (!instance)
                return;
 
-       for (int i = 0; i < instance->physicalDeviceCount; ++i) {
-               radv_physical_device_finish(instance->physicalDevices + i);
+       list_for_each_entry_safe(struct radv_physical_device, pdevice,
+                                &instance->physical_devices, link) {
+               radv_physical_device_destroy(pdevice);
        }
 
        vk_free(&instance->alloc, instance->engineName);
@@ -740,25 +758,30 @@ void radv_DestroyInstance(
 }
 
 static VkResult
-radv_enumerate_devices(struct radv_instance *instance)
+radv_enumerate_physical_devices(struct radv_instance *instance)
 {
+       if (instance->physical_devices_enumerated)
+               return VK_SUCCESS;
+
+       instance->physical_devices_enumerated = true;
+
        /* TODO: Check for more devices ? */
        drmDevicePtr devices[8];
-       VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
+       VkResult result = VK_SUCCESS;
        int max_devices;
 
-       instance->physicalDeviceCount = 0;
-
        if (getenv("RADV_FORCE_FAMILY")) {
                /* When RADV_FORCE_FAMILY is set, the driver creates a nul
                 * device that allows to test the compiler without having an
                 * AMDGPU instance.
                 */
-               result = radv_physical_device_init(instance->physicalDevices +
-                                                  instance->physicalDeviceCount,
-                                                  instance, NULL);
+               struct radv_physical_device *pdevice;
 
-               ++instance->physicalDeviceCount;
+               result = radv_physical_device_try_create(instance, NULL, &pdevice);
+               if (result != VK_SUCCESS)
+                       return result;
+
+               list_addtail(&pdevice->link, &instance->physical_devices);
                return VK_SUCCESS;
        }
 
@@ -768,25 +791,32 @@ radv_enumerate_devices(struct radv_instance *instance)
                radv_logi("Found %d drm nodes", max_devices);
 
        if (max_devices < 1)
-               return vk_error(instance, VK_ERROR_INCOMPATIBLE_DRIVER);
+               return vk_error(instance, VK_SUCCESS);
 
        for (unsigned i = 0; i < (unsigned)max_devices; i++) {
                if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
                    devices[i]->bustype == DRM_BUS_PCI &&
                    devices[i]->deviceinfo.pci->vendor_id == ATI_VENDOR_ID) {
 
-                       result = radv_physical_device_init(instance->physicalDevices +
-                                                          instance->physicalDeviceCount,
-                                                          instance,
-                                                          devices[i]);
-                       if (result == VK_SUCCESS)
-                               ++instance->physicalDeviceCount;
-                       else if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
+                       struct radv_physical_device *pdevice;
+                       result = radv_physical_device_try_create(instance, devices[i],
+                                                                &pdevice);
+                       /* Incompatible DRM device, skip. */
+                       if (result == VK_ERROR_INCOMPATIBLE_DRIVER) {
+                               result = VK_SUCCESS;
+                               continue;
+                       }
+
+                       /* Error creating the physical device, report the error. */
+                       if (result != VK_SUCCESS)
                                break;
+
+                       list_addtail(&pdevice->link, &instance->physical_devices);
                }
        }
        drmFreeDevices(devices, max_devices);
 
+       /* If we successfully enumerated any devices, call it success */
        return result;
 }
 
@@ -796,25 +826,20 @@ VkResult radv_EnumeratePhysicalDevices(
        VkPhysicalDevice*                           pPhysicalDevices)
 {
        RADV_FROM_HANDLE(radv_instance, instance, _instance);
-       VkResult result;
+       VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
 
-       if (instance->physicalDeviceCount < 0) {
-               result = radv_enumerate_devices(instance);
-               if (result != VK_SUCCESS &&
-                   result != VK_ERROR_INCOMPATIBLE_DRIVER)
-                       return result;
-       }
+       VkResult result = radv_enumerate_physical_devices(instance);
+       if (result != VK_SUCCESS)
+               return result;
 
-       if (!pPhysicalDevices) {
-               *pPhysicalDeviceCount = instance->physicalDeviceCount;
-       } else {
-               *pPhysicalDeviceCount = MIN2(*pPhysicalDeviceCount, instance->physicalDeviceCount);
-               for (unsigned i = 0; i < *pPhysicalDeviceCount; ++i)
-                       pPhysicalDevices[i] = radv_physical_device_to_handle(instance->physicalDevices + i);
+       list_for_each_entry(struct radv_physical_device, pdevice,
+                           &instance->physical_devices, link) {
+               vk_outarray_append(&out, i) {
+                       *i = radv_physical_device_to_handle(pdevice);
+               }
        }
 
-       return *pPhysicalDeviceCount < instance->physicalDeviceCount ? VK_INCOMPLETE
-                                                                    : VK_SUCCESS;
+       return vk_outarray_status(&out);
 }
 
 VkResult radv_EnumeratePhysicalDeviceGroups(
@@ -823,27 +848,24 @@ VkResult radv_EnumeratePhysicalDeviceGroups(
     VkPhysicalDeviceGroupProperties*            pPhysicalDeviceGroupProperties)
 {
        RADV_FROM_HANDLE(radv_instance, instance, _instance);
-       VkResult result;
+       VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
+                             pPhysicalDeviceGroupCount);
 
-       if (instance->physicalDeviceCount < 0) {
-               result = radv_enumerate_devices(instance);
-               if (result != VK_SUCCESS &&
-                   result != VK_ERROR_INCOMPATIBLE_DRIVER)
-                       return result;
-       }
+       VkResult result = radv_enumerate_physical_devices(instance);
+       if (result != VK_SUCCESS)
+               return result;
 
-       if (!pPhysicalDeviceGroupProperties) {
-               *pPhysicalDeviceGroupCount = instance->physicalDeviceCount;
-       } else {
-               *pPhysicalDeviceGroupCount = MIN2(*pPhysicalDeviceGroupCount, instance->physicalDeviceCount);
-               for (unsigned i = 0; i < *pPhysicalDeviceGroupCount; ++i) {
-                       pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
-                       pPhysicalDeviceGroupProperties[i].physicalDevices[0] = radv_physical_device_to_handle(instance->physicalDevices + i);
-                       pPhysicalDeviceGroupProperties[i].subsetAllocation = false;
+       list_for_each_entry(struct radv_physical_device, pdevice,
+                           &instance->physical_devices, link) {
+               vk_outarray_append(&out, p) {
+                       p->physicalDeviceCount = 1;
+                       memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
+                       p->physicalDevices[0] = radv_physical_device_to_handle(pdevice);
+                       p->subsetAllocation = false;
                }
        }
-       return *pPhysicalDeviceGroupCount < instance->physicalDeviceCount ? VK_INCOMPLETE
-                                                                         : VK_SUCCESS;
+
+       return vk_outarray_status(&out);
 }
 
 void radv_GetPhysicalDeviceFeatures(
@@ -6447,18 +6469,20 @@ radv_initialise_color_surface(struct radv_device *device,
        cb->cb_color_base = va >> 8;
 
        if (device->physical_device->rad_info.chip_class >= GFX9) {
-               struct gfx9_surf_meta_flags meta;
-               if (iview->image->dcc_offset)
-                       meta = surf->u.gfx9.dcc;
-               else
-                       meta = surf->u.gfx9.cmask;
-
                if (device->physical_device->rad_info.chip_class >= GFX10) {
                        cb->cb_color_attrib3 |= S_028EE0_COLOR_SW_MODE(surf->u.gfx9.surf.swizzle_mode) |
                                S_028EE0_FMASK_SW_MODE(surf->u.gfx9.fmask.swizzle_mode) |
-                               S_028EE0_CMASK_PIPE_ALIGNED(surf->u.gfx9.cmask.pipe_aligned) |
+                               S_028EE0_CMASK_PIPE_ALIGNED(1) |
                                S_028EE0_DCC_PIPE_ALIGNED(surf->u.gfx9.dcc.pipe_aligned);
                } else {
+                       struct gfx9_surf_meta_flags meta = {
+                               .rb_aligned = 1,
+                               .pipe_aligned = 1,
+                       };
+
+                       if (iview->image->dcc_offset)
+                               meta = surf->u.gfx9.dcc;
+
                        cb->cb_color_attrib |= S_028C74_COLOR_SW_MODE(surf->u.gfx9.surf.swizzle_mode) |
                                S_028C74_FMASK_SW_MODE(surf->u.gfx9.fmask.swizzle_mode) |
                                S_028C74_RB_ALIGNED(meta.rb_aligned) |
@@ -6786,10 +6810,10 @@ radv_initialise_ds_surface(struct radv_device *device,
                                iview->image->htile_offset;
                        ds->db_htile_data_base = va >> 8;
                        ds->db_htile_surface = S_028ABC_FULL_CACHE(1) |
-                               S_028ABC_PIPE_ALIGNED(surf->u.gfx9.htile.pipe_aligned);
+                               S_028ABC_PIPE_ALIGNED(1);
 
                        if (device->physical_device->rad_info.chip_class == GFX9) {
-                               ds->db_htile_surface |= S_028ABC_RB_ALIGNED(surf->u.gfx9.htile.rb_aligned);
+                               ds->db_htile_surface |= S_028ABC_RB_ALIGNED(1);
                        }
                }
        } else {