From 6c452aa1adb3de98bf5430497691068f44b469dd Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Wed, 17 Aug 2022 11:52:50 +0200 Subject: [PATCH] turnip: Use the common physical device enumeration Signed-off-by: Konstantin Seurer Reviewed-by: Chia-I Wu Part-of: --- src/freedreno/vulkan/tu_device.c | 77 ++++++-------------------------- src/freedreno/vulkan/tu_device.h | 2 - src/freedreno/vulkan/tu_drm.c | 73 +++++++++++------------------- src/freedreno/vulkan/tu_drm.h | 9 +++- src/freedreno/vulkan/tu_kgsl.c | 19 +++++--- 5 files changed, 62 insertions(+), 118 deletions(-) diff --git a/src/freedreno/vulkan/tu_device.c b/src/freedreno/vulkan/tu_device.c index 4a44526b113..030abb5acbc 100644 --- a/src/freedreno/vulkan/tu_device.c +++ b/src/freedreno/vulkan/tu_device.c @@ -355,6 +355,13 @@ tu_physical_device_finish(struct tu_physical_device *device) vk_physical_device_finish(&device->vk); } +static void +tu_destroy_physical_device(struct vk_physical_device *device) +{ + tu_physical_device_finish((struct tu_physical_device *) device); + vk_free(&device->instance->alloc, device); +} + static const struct debug_control tu_debug_options[] = { { "startup", TU_DEBUG_STARTUP }, { "nir", TU_DEBUG_NIR }, @@ -447,7 +454,13 @@ tu_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, return vk_error(NULL, result); } - instance->physical_device_count = -1; +#ifndef TU_USE_KGSL + instance->vk.physical_devices.try_create_for_drm = + tu_physical_device_try_create; +#else + instance->vk.physical_devices.enumerate = tu_enumerate_devices; +#endif + instance->vk.physical_devices.destroy = tu_destroy_physical_device; instance->debug_flags = parse_debug_string(os_get_option("TU_DEBUG"), tu_debug_options); @@ -485,10 +498,6 @@ tu_DestroyInstance(VkInstance _instance, if (!instance) return; - for (int i = 0; i < instance->physical_device_count; ++i) { - tu_physical_device_finish(instance->physical_devices + i); - } - VG(VALGRIND_DESTROY_MEMPOOL(instance)); driDestroyOptionCache(&instance->dri_options); @@ -498,64 +507,6 @@ tu_DestroyInstance(VkInstance _instance, vk_free(&instance->vk.alloc, instance); } -VKAPI_ATTR VkResult VKAPI_CALL -tu_EnumeratePhysicalDevices(VkInstance _instance, - uint32_t *pPhysicalDeviceCount, - VkPhysicalDevice *pPhysicalDevices) -{ - TU_FROM_HANDLE(tu_instance, instance, _instance); - VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, - pPhysicalDevices, pPhysicalDeviceCount); - - VkResult result; - - if (instance->physical_device_count < 0) { - result = tu_enumerate_devices(instance); - if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER) - return result; - } - - for (uint32_t i = 0; i < instance->physical_device_count; ++i) { - vk_outarray_append_typed(VkPhysicalDevice, &out, p) - { - *p = tu_physical_device_to_handle(instance->physical_devices + i); - } - } - - return vk_outarray_status(&out); -} - -VKAPI_ATTR VkResult VKAPI_CALL -tu_EnumeratePhysicalDeviceGroups( - VkInstance _instance, - uint32_t *pPhysicalDeviceGroupCount, - VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroupProperties) -{ - TU_FROM_HANDLE(tu_instance, instance, _instance); - VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, - pPhysicalDeviceGroupProperties, - pPhysicalDeviceGroupCount); - VkResult result; - - if (instance->physical_device_count < 0) { - result = tu_enumerate_devices(instance); - if (result != VK_SUCCESS && result != VK_ERROR_INCOMPATIBLE_DRIVER) - return result; - } - - for (uint32_t i = 0; i < instance->physical_device_count; ++i) { - vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, p) - { - p->physicalDeviceCount = 1; - p->physicalDevices[0] = - tu_physical_device_to_handle(instance->physical_devices + i); - p->subsetAllocation = false; - } - } - - return vk_outarray_status(&out); -} - static void tu_get_physical_device_features_1_1(struct tu_physical_device *pdevice, VkPhysicalDeviceVulkan11Features *features) diff --git a/src/freedreno/vulkan/tu_device.h b/src/freedreno/vulkan/tu_device.h index f7060e5f78b..e8c846c82f3 100644 --- a/src/freedreno/vulkan/tu_device.h +++ b/src/freedreno/vulkan/tu_device.h @@ -138,8 +138,6 @@ struct tu_instance struct vk_instance vk; uint32_t api_version; - int physical_device_count; - struct tu_physical_device physical_devices[TU_MAX_DRM_DEVICES]; struct driOptionCache dri_options; struct driOptionCache available_dri_options; diff --git a/src/freedreno/vulkan/tu_drm.c b/src/freedreno/vulkan/tu_drm.c index cb7cdeba137..87f0352cf36 100644 --- a/src/freedreno/vulkan/tu_drm.c +++ b/src/freedreno/vulkan/tu_drm.c @@ -721,11 +721,18 @@ const struct vk_sync_type tu_timeline_sync_type = { .wait_many = tu_timeline_sync_wait, }; -static VkResult -tu_drm_device_init(struct tu_physical_device *device, - struct tu_instance *instance, - drmDevicePtr drm_device) +VkResult +tu_physical_device_try_create(struct vk_instance *vk_instance, + struct _drmDevice *drm_device, + struct vk_physical_device **out) { + struct tu_instance *instance = + container_of(vk_instance, struct tu_instance, vk); + + if (!(drm_device->available_nodes & (1 << DRM_NODE_RENDER)) || + drm_device->bustype != DRM_BUS_PLATFORM) + return VK_ERROR_INCOMPATIBLE_DRIVER; + const char *primary_path = drm_device->nodes[DRM_NODE_PRIMARY]; const char *path = drm_device->nodes[DRM_NODE_RENDER]; VkResult result = VK_SUCCESS; @@ -772,6 +779,15 @@ tu_drm_device_init(struct tu_physical_device *device, return result; } + struct tu_physical_device *device = + vk_zalloc(&instance->vk.alloc, sizeof(*device), 8, + VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); + if (!device) { + result = vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY); + drmFreeVersion(version); + goto fail; + } + device->msm_major_version = version->version_major; device->msm_minor_version = version->version_minor; @@ -874,57 +890,20 @@ tu_drm_device_init(struct tu_physical_device *device, result = tu_physical_device_init(device, instance); - if (result == VK_SUCCESS) - return result; + if (result == VK_SUCCESS) { + *out = &device->vk; + return result; + } fail: + if (device) + vk_free(&instance->vk.alloc, device); close(fd); if (master_fd != -1) close(master_fd); return result; } -VkResult -tu_enumerate_devices(struct tu_instance *instance) -{ - /* TODO: Check for more devices ? */ - drmDevicePtr devices[8]; - VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER; - int max_devices; - - instance->physical_device_count = 0; - - max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices)); - - if (instance->debug_flags & TU_DEBUG_STARTUP) { - if (max_devices < 0) - mesa_logi("drmGetDevices2 returned error: %s\n", strerror(max_devices)); - else - mesa_logi("Found %d drm nodes", max_devices); - } - - if (max_devices < 1) - return vk_startup_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER, - "No DRM devices found"); - - for (unsigned i = 0; i < (unsigned) max_devices; i++) { - if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER && - devices[i]->bustype == DRM_BUS_PLATFORM) { - - result = tu_drm_device_init( - instance->physical_devices + instance->physical_device_count, - instance, devices[i]); - if (result == VK_SUCCESS) - ++instance->physical_device_count; - else if (result != VK_ERROR_INCOMPATIBLE_DRIVER) - break; - } - } - drmFreeDevices(devices, max_devices); - - return result; -} - static VkResult tu_queue_submit_create_locked(struct tu_queue *queue, struct vk_queue_submit *vk_submit, diff --git a/src/freedreno/vulkan/tu_drm.h b/src/freedreno/vulkan/tu_drm.h index b27dbbb3478..d2ec6909ae5 100644 --- a/src/freedreno/vulkan/tu_drm.h +++ b/src/freedreno/vulkan/tu_drm.h @@ -103,8 +103,15 @@ tu_bo_get_ref(struct tu_bo *bo) return bo; } +#ifdef TU_USE_KGSL VkResult -tu_enumerate_devices(struct tu_instance *instance); +tu_enumerate_devices(struct vk_instance *vk_instance); +#else +VkResult +tu_physical_device_try_create(struct vk_instance *vk_instance, + struct _drmDevice *drm_device, + struct vk_physical_device **out); +#endif int tu_device_get_gpu_timestamp(struct tu_device *dev, diff --git a/src/freedreno/vulkan/tu_kgsl.c b/src/freedreno/vulkan/tu_kgsl.c index 3f5dca645e7..1e3204f12d3 100644 --- a/src/freedreno/vulkan/tu_kgsl.c +++ b/src/freedreno/vulkan/tu_kgsl.c @@ -215,24 +215,32 @@ get_kgsl_prop(int fd, unsigned int type, void *value, size_t size) } VkResult -tu_enumerate_devices(struct tu_instance *instance) +tu_enumerate_devices(struct vk_instance *vk_instance) { + struct tu_instance *instance = + container_of(vk_instance, struct tu_instance, vk); + static const char path[] = "/dev/kgsl-3d0"; int fd; - struct tu_physical_device *device = &instance->physical_devices[0]; - if (instance->vk.enabled_extensions.KHR_display) return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER, "I can't KHR_display"); fd = open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { - instance->physical_device_count = 0; return vk_errorf(instance, VK_ERROR_INCOMPATIBLE_DRIVER, "failed to open device %s", path); } + struct tu_physical_device *device = + vk_zalloc(&instance->vk.alloc, sizeof(*device), 8, + VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); + if (!device) { + close(fd); + return vk_error(instance, VK_ERROR_OUT_OF_HOST_MEMORY); + } + struct kgsl_devinfo info; if (get_kgsl_prop(fd, KGSL_PROP_DEVICE_INFO, &info, sizeof(info))) goto fail; @@ -265,11 +273,12 @@ tu_enumerate_devices(struct tu_instance *instance) if (tu_physical_device_init(device, instance) != VK_SUCCESS) goto fail; - instance->physical_device_count = 1; + list_addtail(&device->vk.link, &instance->vk.physical_devices.list); return VK_SUCCESS; fail: + vk_free(&instance->vk.alloc, device); close(fd); return VK_ERROR_INITIALIZATION_FAILED; }