Path: blob/21.2-virgl/src/vulkan/device-select-layer/device_select_layer.c
7086 views
/*1* Copyright © 2017 Google2* Copyright © 2019 Red Hat3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324/* Rules for device selection.25* Is there an X or wayland connection open (or DISPLAY set).26* If no - try and find which device was the boot_vga device.27* If yes - try and work out which device is the connection primary,28* DRI_PRIME tagged overrides only work if bus info, =1 will just pick an alternate.29*/3031#include <vulkan/vk_layer.h>3233#include <assert.h>34#include <stdio.h>35#include <string.h>36#include <fcntl.h>37#include <unistd.h>3839#include "device_select.h"40#include "c99_compat.h"41#include "hash_table.h"42#include "vk_util.h"43#include "c11/threads.h"4445struct instance_info {46PFN_vkDestroyInstance DestroyInstance;47PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;48PFN_vkEnumeratePhysicalDeviceGroups EnumeratePhysicalDeviceGroups;49PFN_vkGetInstanceProcAddr GetInstanceProcAddr;50PFN_GetPhysicalDeviceProcAddr GetPhysicalDeviceProcAddr;51PFN_vkEnumerateDeviceExtensionProperties EnumerateDeviceExtensionProperties;52PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties;53PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2;54bool has_pci_bus, has_vulkan11;55bool has_wayland, has_xcb;56};5758static struct hash_table *device_select_instance_ht = NULL;59static mtx_t device_select_mutex;6061static once_flag device_select_is_init = ONCE_FLAG_INIT;6263static void device_select_once_init(void) {64mtx_init(&device_select_mutex, mtx_plain);65}6667static void68device_select_init_instances(void)69{70call_once(&device_select_is_init, device_select_once_init);7172mtx_lock(&device_select_mutex);73if (!device_select_instance_ht)74device_select_instance_ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer,75_mesa_key_pointer_equal);76mtx_unlock(&device_select_mutex);77}7879static void80device_select_try_free_ht(void)81{82mtx_lock(&device_select_mutex);83if (device_select_instance_ht) {84if (_mesa_hash_table_num_entries(device_select_instance_ht) == 0) {85_mesa_hash_table_destroy(device_select_instance_ht, NULL);86device_select_instance_ht = NULL;87}88}89mtx_unlock(&device_select_mutex);90}9192static void93device_select_layer_add_instance(VkInstance instance, struct instance_info *info)94{95device_select_init_instances();96mtx_lock(&device_select_mutex);97_mesa_hash_table_insert(device_select_instance_ht, instance, info);98mtx_unlock(&device_select_mutex);99}100101static struct instance_info *102device_select_layer_get_instance(VkInstance instance)103{104struct hash_entry *entry;105struct instance_info *info = NULL;106mtx_lock(&device_select_mutex);107entry = _mesa_hash_table_search(device_select_instance_ht, (void *)instance);108if (entry)109info = (struct instance_info *)entry->data;110mtx_unlock(&device_select_mutex);111return info;112}113114static void115device_select_layer_remove_instance(VkInstance instance)116{117mtx_lock(&device_select_mutex);118_mesa_hash_table_remove_key(device_select_instance_ht, instance);119mtx_unlock(&device_select_mutex);120device_select_try_free_ht();121}122123static VkResult device_select_CreateInstance(const VkInstanceCreateInfo *pCreateInfo,124const VkAllocationCallbacks *pAllocator,125VkInstance *pInstance)126{127VkLayerInstanceCreateInfo *chain_info;128for(chain_info = (VkLayerInstanceCreateInfo*)pCreateInfo->pNext; chain_info; chain_info = (VkLayerInstanceCreateInfo*)chain_info->pNext)129if(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == VK_LAYER_LINK_INFO)130break;131132assert(chain_info->u.pLayerInfo);133struct instance_info *info = (struct instance_info *)calloc(1, sizeof(struct instance_info));134135info->GetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;136PFN_vkCreateInstance fpCreateInstance =137(PFN_vkCreateInstance)info->GetInstanceProcAddr(NULL, "vkCreateInstance");138if (fpCreateInstance == NULL) {139free(info);140return VK_ERROR_INITIALIZATION_FAILED;141}142143chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;144145VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);146if (result != VK_SUCCESS) {147free(info);148return result;149}150151for (unsigned i = 0; i < pCreateInfo->enabledExtensionCount; i++) {152#ifdef VK_USE_PLATFORM_WAYLAND_KHR153if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME))154info->has_wayland = true;155#endif156#ifdef VK_USE_PLATFORM_XCB_KHR157if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME))158info->has_xcb = true;159#endif160}161162/*163* The loader is currently not able to handle GetPhysicalDeviceProperties2KHR calls in164* EnumeratePhysicalDevices when there are other layers present. To avoid mysterious crashes165* for users just use only the vulkan version for now.166*/167info->has_vulkan11 = pCreateInfo->pApplicationInfo &&168pCreateInfo->pApplicationInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0);169170info->GetPhysicalDeviceProcAddr = (PFN_GetPhysicalDeviceProcAddr)info->GetInstanceProcAddr(*pInstance, "vk_layerGetPhysicalDeviceProcAddr");171#define DEVSEL_GET_CB(func) info->func = (PFN_vk##func)info->GetInstanceProcAddr(*pInstance, "vk" #func)172DEVSEL_GET_CB(DestroyInstance);173DEVSEL_GET_CB(EnumeratePhysicalDevices);174DEVSEL_GET_CB(EnumeratePhysicalDeviceGroups);175DEVSEL_GET_CB(GetPhysicalDeviceProperties);176DEVSEL_GET_CB(EnumerateDeviceExtensionProperties);177if (info->has_vulkan11)178DEVSEL_GET_CB(GetPhysicalDeviceProperties2);179#undef DEVSEL_GET_CB180181device_select_layer_add_instance(*pInstance, info);182183return VK_SUCCESS;184}185186static void device_select_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)187{188struct instance_info *info = device_select_layer_get_instance(instance);189190device_select_layer_remove_instance(instance);191info->DestroyInstance(instance, pAllocator);192free(info);193}194195static void get_device_properties(const struct instance_info *info, VkPhysicalDevice device, VkPhysicalDeviceProperties2 *properties)196{197info->GetPhysicalDeviceProperties(device, &properties->properties);198199if (info->GetPhysicalDeviceProperties2 && properties->properties.apiVersion >= VK_API_VERSION_1_1)200info->GetPhysicalDeviceProperties2(device, properties);201}202203static void print_gpu(const struct instance_info *info, unsigned index, VkPhysicalDevice device)204{205const char *type = "";206VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {207.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT208};209VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){210.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR211};212if (info->has_vulkan11 && info->has_pci_bus)213properties.pNext = &ext_pci_properties;214get_device_properties(info, device, &properties);215216switch(properties.properties.deviceType) {217case VK_PHYSICAL_DEVICE_TYPE_OTHER:218default:219type = "other";220break;221case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:222type = "integrated GPU";223break;224case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:225type = "discrete GPU";226break;227case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:228type = "virtual GPU";229break;230case VK_PHYSICAL_DEVICE_TYPE_CPU:231type = "CPU";232break;233}234fprintf(stderr, " GPU %d: %x:%x \"%s\" %s", index, properties.properties.vendorID,235properties.properties.deviceID, properties.properties.deviceName, type);236if (info->has_pci_bus)237fprintf(stderr, " %04x:%02x:%02x.%x", ext_pci_properties.pciDomain,238ext_pci_properties.pciBus, ext_pci_properties.pciDevice,239ext_pci_properties.pciFunction);240fprintf(stderr, "\n");241}242243static bool fill_drm_device_info(const struct instance_info *info,244struct device_pci_info *drm_device,245VkPhysicalDevice device)246{247VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {248.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT249};250251VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){252.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR253};254255if (info->has_vulkan11 && info->has_pci_bus)256properties.pNext = &ext_pci_properties;257get_device_properties(info, device, &properties);258259drm_device->cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;260drm_device->dev_info.vendor_id = properties.properties.vendorID;261drm_device->dev_info.device_id = properties.properties.deviceID;262if (info->has_pci_bus) {263drm_device->has_bus_info = true;264drm_device->bus_info.domain = ext_pci_properties.pciDomain;265drm_device->bus_info.bus = ext_pci_properties.pciBus;266drm_device->bus_info.dev = ext_pci_properties.pciDevice;267drm_device->bus_info.func = ext_pci_properties.pciFunction;268}269return drm_device->cpu_device;270}271272static int device_select_find_explicit_default(struct device_pci_info *pci_infos,273uint32_t device_count,274const char *selection)275{276int default_idx = -1;277unsigned vendor_id, device_id;278int matched = sscanf(selection, "%x:%x", &vendor_id, &device_id);279if (matched != 2)280return default_idx;281282for (unsigned i = 0; i < device_count; ++i) {283if (pci_infos[i].dev_info.vendor_id == vendor_id &&284pci_infos[i].dev_info.device_id == device_id)285default_idx = i;286}287return default_idx;288}289290static int device_select_find_dri_prime_tag_default(struct device_pci_info *pci_infos,291uint32_t device_count,292const char *dri_prime)293{294int default_idx = -1;295for (unsigned i = 0; i < device_count; ++i) {296char *tag = NULL;297if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",298pci_infos[i].bus_info.domain,299pci_infos[i].bus_info.bus,300pci_infos[i].bus_info.dev,301pci_infos[i].bus_info.func) >= 0) {302if (strcmp(dri_prime, tag))303default_idx = i;304}305free(tag);306}307return default_idx;308}309310static int device_select_find_boot_vga_default(struct device_pci_info *pci_infos,311uint32_t device_count)312{313char boot_vga_path[1024];314int default_idx = -1;315for (unsigned i = 0; i < device_count; ++i) {316/* fallback to probing the pci bus boot_vga device. */317snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain,318pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func);319int fd = open(boot_vga_path, O_RDONLY);320if (fd != -1) {321uint8_t val;322if (read(fd, &val, 1) == 1) {323if (val == '1')324default_idx = i;325}326close(fd);327}328if (default_idx != -1)329break;330}331return default_idx;332}333334static int device_select_find_non_cpu(struct device_pci_info *pci_infos,335uint32_t device_count)336{337int default_idx = -1;338339/* pick first GPU device */340for (unsigned i = 0; i < device_count; ++i) {341if (!pci_infos[i].cpu_device){342default_idx = i;343break;344}345}346return default_idx;347}348349static int find_non_cpu_skip(struct device_pci_info *pci_infos,350uint32_t device_count,351int skip_idx)352{353for (unsigned i = 0; i < device_count; ++i) {354if (i == skip_idx)355continue;356if (pci_infos[i].cpu_device)357continue;358return i;359}360return -1;361}362363static uint32_t get_default_device(const struct instance_info *info,364const char *selection,365uint32_t physical_device_count,366VkPhysicalDevice *pPhysicalDevices)367{368int default_idx = -1;369const char *dri_prime = getenv("DRI_PRIME");370bool dri_prime_is_one = false;371int cpu_count = 0;372if (dri_prime && !strcmp(dri_prime, "1"))373dri_prime_is_one = true;374375if (dri_prime && !dri_prime_is_one && !info->has_pci_bus) {376fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");377}378379struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info));380if (!pci_infos)381return 0;382383for (unsigned i = 0; i < physical_device_count; ++i) {384cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;385}386387if (selection)388default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection);389if (default_idx == -1 && info->has_pci_bus && dri_prime && !dri_prime_is_one)390default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime);391if (default_idx == -1 && info->has_wayland)392default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);393if (default_idx == -1 && info->has_xcb)394default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);395if (default_idx == -1 && info->has_pci_bus)396default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);397if (default_idx == -1 && cpu_count)398default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);399400/* DRI_PRIME=1 handling - pick any other device than default. */401if (default_idx != -1 && dri_prime_is_one && physical_device_count > (cpu_count + 1)) {402if (default_idx == 0 || default_idx == 1)403default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx);404}405free(pci_infos);406return default_idx == -1 ? 0 : default_idx;407}408409static VkResult device_select_EnumeratePhysicalDevices(VkInstance instance,410uint32_t* pPhysicalDeviceCount,411VkPhysicalDevice *pPhysicalDevices)412{413struct instance_info *info = device_select_layer_get_instance(instance);414uint32_t physical_device_count = 0;415uint32_t selected_physical_device_count = 0;416const char* selection = getenv("MESA_VK_DEVICE_SELECT");417VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL);418VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);419if (result != VK_SUCCESS)420return result;421422VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice), physical_device_count);423VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),424physical_device_count);425426if (!physical_devices || !selected_physical_devices) {427result = VK_ERROR_OUT_OF_HOST_MEMORY;428goto out;429}430431result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices);432if (result != VK_SUCCESS)433goto out;434435for (unsigned i = 0; i < physical_device_count; i++) {436uint32_t count;437info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL);438if (count > 0) {439VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties));440if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) {441for (unsigned j = 0; j < count; j++) {442if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME))443info->has_pci_bus = true;444}445}446free(extensions);447}448}449if (selection && strcmp(selection, "list") == 0) {450fprintf(stderr, "selectable devices:\n");451for (unsigned i = 0; i < physical_device_count; ++i)452print_gpu(info, i, physical_devices[i]);453exit(0);454} else {455unsigned selected_index = get_default_device(info, selection, physical_device_count, physical_devices);456selected_physical_device_count = physical_device_count;457selected_physical_devices[0] = physical_devices[selected_index];458for (unsigned i = 0; i < physical_device_count - 1; ++i) {459unsigned this_idx = i < selected_index ? i : i + 1;460selected_physical_devices[i + 1] = physical_devices[this_idx];461}462}463464if (selected_physical_device_count == 0) {465fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n");466}467468assert(result == VK_SUCCESS);469470for (unsigned i = 0; i < selected_physical_device_count; i++) {471vk_outarray_append(&out, ent) {472*ent = selected_physical_devices[i];473}474}475result = vk_outarray_status(&out);476out:477free(physical_devices);478free(selected_physical_devices);479return result;480}481482static VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,483uint32_t* pPhysicalDeviceGroupCount,484VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups)485{486struct instance_info *info = device_select_layer_get_instance(instance);487uint32_t physical_device_group_count = 0;488uint32_t selected_physical_device_group_count = 0;489VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL);490VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount);491492if (result != VK_SUCCESS)493return result;494495VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);496VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);497498if (!physical_device_groups || !selected_physical_device_groups) {499result = VK_ERROR_OUT_OF_HOST_MEMORY;500goto out;501}502503result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups);504if (result != VK_SUCCESS)505goto out;506507/* just sort groups with CPU devices to the end? - assume nobody will mix these */508int num_gpu_groups = 0;509int num_cpu_groups = 0;510selected_physical_device_group_count = physical_device_group_count;511for (unsigned i = 0; i < physical_device_group_count; i++) {512bool group_has_cpu_device = false;513for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) {514VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j];515VkPhysicalDeviceProperties2KHR properties = (VkPhysicalDeviceProperties2KHR){516.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR517};518info->GetPhysicalDeviceProperties(physical_device, &properties.properties);519group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;520}521522if (group_has_cpu_device) {523selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i];524num_cpu_groups++;525} else {526selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i];527num_gpu_groups++;528}529}530531assert(result == VK_SUCCESS);532533for (unsigned i = 0; i < selected_physical_device_group_count; i++) {534vk_outarray_append(&out, ent) {535*ent = selected_physical_device_groups[i];536}537}538result = vk_outarray_status(&out);539out:540free(physical_device_groups);541free(selected_physical_device_groups);542return result;543}544545static void (*get_pdevice_proc_addr(VkInstance instance, const char* name))()546{547struct instance_info *info = device_select_layer_get_instance(instance);548return info->GetPhysicalDeviceProcAddr(instance, name);549}550551static void (*get_instance_proc_addr(VkInstance instance, const char* name))()552{553if (strcmp(name, "vkGetInstanceProcAddr") == 0)554return (void(*)())get_instance_proc_addr;555if (strcmp(name, "vkCreateInstance") == 0)556return (void(*)())device_select_CreateInstance;557if (strcmp(name, "vkDestroyInstance") == 0)558return (void(*)())device_select_DestroyInstance;559if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)560return (void(*)())device_select_EnumeratePhysicalDevices;561if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)562return (void(*)())device_select_EnumeratePhysicalDeviceGroups;563564struct instance_info *info = device_select_layer_get_instance(instance);565return info->GetInstanceProcAddr(instance, name);566}567568VK_LAYER_EXPORT VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)569{570if (pVersionStruct->loaderLayerInterfaceVersion < 2)571return VK_ERROR_INITIALIZATION_FAILED;572pVersionStruct->loaderLayerInterfaceVersion = 2;573574pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr;575pVersionStruct->pfnGetPhysicalDeviceProcAddr = get_pdevice_proc_addr;576577return VK_SUCCESS;578}579580581