1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2017 Google 3bf215546Sopenharmony_ci * Copyright © 2019 Red Hat 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22bf215546Sopenharmony_ci * IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci/* Rules for device selection. 26bf215546Sopenharmony_ci * Is there an X or wayland connection open (or DISPLAY set). 27bf215546Sopenharmony_ci * If no - try and find which device was the boot_vga device. 28bf215546Sopenharmony_ci * If yes - try and work out which device is the connection primary, 29bf215546Sopenharmony_ci * DRI_PRIME tagged overrides only work if bus info, =1 will just pick an alternate. 30bf215546Sopenharmony_ci */ 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include <vulkan/vk_layer.h> 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci#include <assert.h> 35bf215546Sopenharmony_ci#include <stdio.h> 36bf215546Sopenharmony_ci#include <string.h> 37bf215546Sopenharmony_ci#include <fcntl.h> 38bf215546Sopenharmony_ci#include <unistd.h> 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci#include "device_select.h" 41bf215546Sopenharmony_ci#include "hash_table.h" 42bf215546Sopenharmony_ci#include "vk_util.h" 43bf215546Sopenharmony_ci#include "c11/threads.h" 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_cistruct instance_info { 46bf215546Sopenharmony_ci PFN_vkDestroyInstance DestroyInstance; 47bf215546Sopenharmony_ci PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices; 48bf215546Sopenharmony_ci PFN_vkEnumeratePhysicalDeviceGroups EnumeratePhysicalDeviceGroups; 49bf215546Sopenharmony_ci PFN_vkGetInstanceProcAddr GetInstanceProcAddr; 50bf215546Sopenharmony_ci PFN_vkEnumerateDeviceExtensionProperties EnumerateDeviceExtensionProperties; 51bf215546Sopenharmony_ci PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties; 52bf215546Sopenharmony_ci PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2; 53bf215546Sopenharmony_ci bool has_pci_bus, has_vulkan11; 54bf215546Sopenharmony_ci bool has_wayland, has_xcb; 55bf215546Sopenharmony_ci}; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_cistatic struct hash_table *device_select_instance_ht = NULL; 58bf215546Sopenharmony_cistatic mtx_t device_select_mutex; 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_cistatic once_flag device_select_is_init = ONCE_FLAG_INIT; 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_cistatic void device_select_once_init(void) { 63bf215546Sopenharmony_ci mtx_init(&device_select_mutex, mtx_plain); 64bf215546Sopenharmony_ci} 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_cistatic void 67bf215546Sopenharmony_cidevice_select_init_instances(void) 68bf215546Sopenharmony_ci{ 69bf215546Sopenharmony_ci call_once(&device_select_is_init, device_select_once_init); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci mtx_lock(&device_select_mutex); 72bf215546Sopenharmony_ci if (!device_select_instance_ht) 73bf215546Sopenharmony_ci device_select_instance_ht = _mesa_hash_table_create(NULL, _mesa_hash_pointer, 74bf215546Sopenharmony_ci _mesa_key_pointer_equal); 75bf215546Sopenharmony_ci mtx_unlock(&device_select_mutex); 76bf215546Sopenharmony_ci} 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_cistatic void 79bf215546Sopenharmony_cidevice_select_try_free_ht(void) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci mtx_lock(&device_select_mutex); 82bf215546Sopenharmony_ci if (device_select_instance_ht) { 83bf215546Sopenharmony_ci if (_mesa_hash_table_num_entries(device_select_instance_ht) == 0) { 84bf215546Sopenharmony_ci _mesa_hash_table_destroy(device_select_instance_ht, NULL); 85bf215546Sopenharmony_ci device_select_instance_ht = NULL; 86bf215546Sopenharmony_ci } 87bf215546Sopenharmony_ci } 88bf215546Sopenharmony_ci mtx_unlock(&device_select_mutex); 89bf215546Sopenharmony_ci} 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_cistatic void 92bf215546Sopenharmony_cidevice_select_layer_add_instance(VkInstance instance, struct instance_info *info) 93bf215546Sopenharmony_ci{ 94bf215546Sopenharmony_ci device_select_init_instances(); 95bf215546Sopenharmony_ci mtx_lock(&device_select_mutex); 96bf215546Sopenharmony_ci _mesa_hash_table_insert(device_select_instance_ht, instance, info); 97bf215546Sopenharmony_ci mtx_unlock(&device_select_mutex); 98bf215546Sopenharmony_ci} 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_cistatic struct instance_info * 101bf215546Sopenharmony_cidevice_select_layer_get_instance(VkInstance instance) 102bf215546Sopenharmony_ci{ 103bf215546Sopenharmony_ci struct hash_entry *entry; 104bf215546Sopenharmony_ci struct instance_info *info = NULL; 105bf215546Sopenharmony_ci mtx_lock(&device_select_mutex); 106bf215546Sopenharmony_ci entry = _mesa_hash_table_search(device_select_instance_ht, (void *)instance); 107bf215546Sopenharmony_ci if (entry) 108bf215546Sopenharmony_ci info = (struct instance_info *)entry->data; 109bf215546Sopenharmony_ci mtx_unlock(&device_select_mutex); 110bf215546Sopenharmony_ci return info; 111bf215546Sopenharmony_ci} 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_cistatic void 114bf215546Sopenharmony_cidevice_select_layer_remove_instance(VkInstance instance) 115bf215546Sopenharmony_ci{ 116bf215546Sopenharmony_ci mtx_lock(&device_select_mutex); 117bf215546Sopenharmony_ci _mesa_hash_table_remove_key(device_select_instance_ht, instance); 118bf215546Sopenharmony_ci mtx_unlock(&device_select_mutex); 119bf215546Sopenharmony_ci device_select_try_free_ht(); 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_cistatic VkResult device_select_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, 123bf215546Sopenharmony_ci const VkAllocationCallbacks *pAllocator, 124bf215546Sopenharmony_ci VkInstance *pInstance) 125bf215546Sopenharmony_ci{ 126bf215546Sopenharmony_ci VkLayerInstanceCreateInfo *chain_info; 127bf215546Sopenharmony_ci for(chain_info = (VkLayerInstanceCreateInfo*)pCreateInfo->pNext; chain_info; chain_info = (VkLayerInstanceCreateInfo*)chain_info->pNext) 128bf215546Sopenharmony_ci if(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && chain_info->function == VK_LAYER_LINK_INFO) 129bf215546Sopenharmony_ci break; 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci assert(chain_info->u.pLayerInfo); 132bf215546Sopenharmony_ci struct instance_info *info = (struct instance_info *)calloc(1, sizeof(struct instance_info)); 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci info->GetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; 135bf215546Sopenharmony_ci PFN_vkCreateInstance fpCreateInstance = 136bf215546Sopenharmony_ci (PFN_vkCreateInstance)info->GetInstanceProcAddr(NULL, "vkCreateInstance"); 137bf215546Sopenharmony_ci if (fpCreateInstance == NULL) { 138bf215546Sopenharmony_ci free(info); 139bf215546Sopenharmony_ci return VK_ERROR_INITIALIZATION_FAILED; 140bf215546Sopenharmony_ci } 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext; 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance); 145bf215546Sopenharmony_ci if (result != VK_SUCCESS) { 146bf215546Sopenharmony_ci free(info); 147bf215546Sopenharmony_ci return result; 148bf215546Sopenharmony_ci } 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci for (unsigned i = 0; i < pCreateInfo->enabledExtensionCount; i++) { 151bf215546Sopenharmony_ci#ifdef VK_USE_PLATFORM_WAYLAND_KHR 152bf215546Sopenharmony_ci if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME)) 153bf215546Sopenharmony_ci info->has_wayland = true; 154bf215546Sopenharmony_ci#endif 155bf215546Sopenharmony_ci#ifdef VK_USE_PLATFORM_XCB_KHR 156bf215546Sopenharmony_ci if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME)) 157bf215546Sopenharmony_ci info->has_xcb = true; 158bf215546Sopenharmony_ci#endif 159bf215546Sopenharmony_ci } 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci /* 162bf215546Sopenharmony_ci * The loader is currently not able to handle GetPhysicalDeviceProperties2KHR calls in 163bf215546Sopenharmony_ci * EnumeratePhysicalDevices when there are other layers present. To avoid mysterious crashes 164bf215546Sopenharmony_ci * for users just use only the vulkan version for now. 165bf215546Sopenharmony_ci */ 166bf215546Sopenharmony_ci info->has_vulkan11 = pCreateInfo->pApplicationInfo && 167bf215546Sopenharmony_ci pCreateInfo->pApplicationInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0); 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci#define DEVSEL_GET_CB(func) info->func = (PFN_vk##func)info->GetInstanceProcAddr(*pInstance, "vk" #func) 170bf215546Sopenharmony_ci DEVSEL_GET_CB(DestroyInstance); 171bf215546Sopenharmony_ci DEVSEL_GET_CB(EnumeratePhysicalDevices); 172bf215546Sopenharmony_ci DEVSEL_GET_CB(EnumeratePhysicalDeviceGroups); 173bf215546Sopenharmony_ci DEVSEL_GET_CB(GetPhysicalDeviceProperties); 174bf215546Sopenharmony_ci DEVSEL_GET_CB(EnumerateDeviceExtensionProperties); 175bf215546Sopenharmony_ci if (info->has_vulkan11) 176bf215546Sopenharmony_ci DEVSEL_GET_CB(GetPhysicalDeviceProperties2); 177bf215546Sopenharmony_ci#undef DEVSEL_GET_CB 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci device_select_layer_add_instance(*pInstance, info); 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci return VK_SUCCESS; 182bf215546Sopenharmony_ci} 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_cistatic void device_select_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator) 185bf215546Sopenharmony_ci{ 186bf215546Sopenharmony_ci struct instance_info *info = device_select_layer_get_instance(instance); 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci device_select_layer_remove_instance(instance); 189bf215546Sopenharmony_ci info->DestroyInstance(instance, pAllocator); 190bf215546Sopenharmony_ci free(info); 191bf215546Sopenharmony_ci} 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_cistatic void get_device_properties(const struct instance_info *info, VkPhysicalDevice device, VkPhysicalDeviceProperties2 *properties) 194bf215546Sopenharmony_ci{ 195bf215546Sopenharmony_ci info->GetPhysicalDeviceProperties(device, &properties->properties); 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci if (info->GetPhysicalDeviceProperties2 && properties->properties.apiVersion >= VK_API_VERSION_1_1) 198bf215546Sopenharmony_ci info->GetPhysicalDeviceProperties2(device, properties); 199bf215546Sopenharmony_ci} 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_cistatic void print_gpu(const struct instance_info *info, unsigned index, VkPhysicalDevice device) 202bf215546Sopenharmony_ci{ 203bf215546Sopenharmony_ci const char *type = ""; 204bf215546Sopenharmony_ci VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) { 205bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT 206bf215546Sopenharmony_ci }; 207bf215546Sopenharmony_ci VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){ 208bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 209bf215546Sopenharmony_ci }; 210bf215546Sopenharmony_ci if (info->has_vulkan11 && info->has_pci_bus) 211bf215546Sopenharmony_ci properties.pNext = &ext_pci_properties; 212bf215546Sopenharmony_ci get_device_properties(info, device, &properties); 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_ci switch(properties.properties.deviceType) { 215bf215546Sopenharmony_ci case VK_PHYSICAL_DEVICE_TYPE_OTHER: 216bf215546Sopenharmony_ci default: 217bf215546Sopenharmony_ci type = "other"; 218bf215546Sopenharmony_ci break; 219bf215546Sopenharmony_ci case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: 220bf215546Sopenharmony_ci type = "integrated GPU"; 221bf215546Sopenharmony_ci break; 222bf215546Sopenharmony_ci case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: 223bf215546Sopenharmony_ci type = "discrete GPU"; 224bf215546Sopenharmony_ci break; 225bf215546Sopenharmony_ci case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: 226bf215546Sopenharmony_ci type = "virtual GPU"; 227bf215546Sopenharmony_ci break; 228bf215546Sopenharmony_ci case VK_PHYSICAL_DEVICE_TYPE_CPU: 229bf215546Sopenharmony_ci type = "CPU"; 230bf215546Sopenharmony_ci break; 231bf215546Sopenharmony_ci } 232bf215546Sopenharmony_ci fprintf(stderr, " GPU %d: %x:%x \"%s\" %s", index, properties.properties.vendorID, 233bf215546Sopenharmony_ci properties.properties.deviceID, properties.properties.deviceName, type); 234bf215546Sopenharmony_ci if (info->has_vulkan11 && info->has_pci_bus) 235bf215546Sopenharmony_ci fprintf(stderr, " %04x:%02x:%02x.%x", ext_pci_properties.pciDomain, 236bf215546Sopenharmony_ci ext_pci_properties.pciBus, ext_pci_properties.pciDevice, 237bf215546Sopenharmony_ci ext_pci_properties.pciFunction); 238bf215546Sopenharmony_ci fprintf(stderr, "\n"); 239bf215546Sopenharmony_ci} 240bf215546Sopenharmony_ci 241bf215546Sopenharmony_cistatic bool fill_drm_device_info(const struct instance_info *info, 242bf215546Sopenharmony_ci struct device_pci_info *drm_device, 243bf215546Sopenharmony_ci VkPhysicalDevice device) 244bf215546Sopenharmony_ci{ 245bf215546Sopenharmony_ci VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) { 246bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT 247bf215546Sopenharmony_ci }; 248bf215546Sopenharmony_ci 249bf215546Sopenharmony_ci VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){ 250bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 251bf215546Sopenharmony_ci }; 252bf215546Sopenharmony_ci 253bf215546Sopenharmony_ci if (info->has_vulkan11 && info->has_pci_bus) 254bf215546Sopenharmony_ci properties.pNext = &ext_pci_properties; 255bf215546Sopenharmony_ci get_device_properties(info, device, &properties); 256bf215546Sopenharmony_ci 257bf215546Sopenharmony_ci drm_device->cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU; 258bf215546Sopenharmony_ci drm_device->dev_info.vendor_id = properties.properties.vendorID; 259bf215546Sopenharmony_ci drm_device->dev_info.device_id = properties.properties.deviceID; 260bf215546Sopenharmony_ci if (info->has_vulkan11 && info->has_pci_bus) { 261bf215546Sopenharmony_ci drm_device->has_bus_info = true; 262bf215546Sopenharmony_ci drm_device->bus_info.domain = ext_pci_properties.pciDomain; 263bf215546Sopenharmony_ci drm_device->bus_info.bus = ext_pci_properties.pciBus; 264bf215546Sopenharmony_ci drm_device->bus_info.dev = ext_pci_properties.pciDevice; 265bf215546Sopenharmony_ci drm_device->bus_info.func = ext_pci_properties.pciFunction; 266bf215546Sopenharmony_ci } 267bf215546Sopenharmony_ci return drm_device->cpu_device; 268bf215546Sopenharmony_ci} 269bf215546Sopenharmony_ci 270bf215546Sopenharmony_cistatic int device_select_find_explicit_default(struct device_pci_info *pci_infos, 271bf215546Sopenharmony_ci uint32_t device_count, 272bf215546Sopenharmony_ci const char *selection) 273bf215546Sopenharmony_ci{ 274bf215546Sopenharmony_ci int default_idx = -1; 275bf215546Sopenharmony_ci unsigned vendor_id, device_id; 276bf215546Sopenharmony_ci int matched = sscanf(selection, "%x:%x", &vendor_id, &device_id); 277bf215546Sopenharmony_ci if (matched != 2) 278bf215546Sopenharmony_ci return default_idx; 279bf215546Sopenharmony_ci 280bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 281bf215546Sopenharmony_ci if (pci_infos[i].dev_info.vendor_id == vendor_id && 282bf215546Sopenharmony_ci pci_infos[i].dev_info.device_id == device_id) 283bf215546Sopenharmony_ci default_idx = i; 284bf215546Sopenharmony_ci } 285bf215546Sopenharmony_ci return default_idx; 286bf215546Sopenharmony_ci} 287bf215546Sopenharmony_ci 288bf215546Sopenharmony_cistatic int device_select_find_dri_prime_tag_default(struct device_pci_info *pci_infos, 289bf215546Sopenharmony_ci uint32_t device_count, 290bf215546Sopenharmony_ci const char *dri_prime) 291bf215546Sopenharmony_ci{ 292bf215546Sopenharmony_ci int default_idx = -1; 293bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 294bf215546Sopenharmony_ci char *tag = NULL; 295bf215546Sopenharmony_ci if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u", 296bf215546Sopenharmony_ci pci_infos[i].bus_info.domain, 297bf215546Sopenharmony_ci pci_infos[i].bus_info.bus, 298bf215546Sopenharmony_ci pci_infos[i].bus_info.dev, 299bf215546Sopenharmony_ci pci_infos[i].bus_info.func) >= 0) { 300bf215546Sopenharmony_ci if (strcmp(dri_prime, tag)) 301bf215546Sopenharmony_ci default_idx = i; 302bf215546Sopenharmony_ci } 303bf215546Sopenharmony_ci free(tag); 304bf215546Sopenharmony_ci } 305bf215546Sopenharmony_ci return default_idx; 306bf215546Sopenharmony_ci} 307bf215546Sopenharmony_ci 308bf215546Sopenharmony_cistatic int device_select_find_boot_vga_vid_did(struct device_pci_info *pci_infos, 309bf215546Sopenharmony_ci uint32_t device_count) 310bf215546Sopenharmony_ci{ 311bf215546Sopenharmony_ci char path[1024]; 312bf215546Sopenharmony_ci int fd; 313bf215546Sopenharmony_ci int default_idx = -1; 314bf215546Sopenharmony_ci uint8_t boot_vga = 0; 315bf215546Sopenharmony_ci ssize_t size_ret; 316bf215546Sopenharmony_ci #pragma pack(push, 1) 317bf215546Sopenharmony_ci struct id { 318bf215546Sopenharmony_ci uint16_t vid; 319bf215546Sopenharmony_ci uint16_t did; 320bf215546Sopenharmony_ci }id; 321bf215546Sopenharmony_ci #pragma pack(pop) 322bf215546Sopenharmony_ci 323bf215546Sopenharmony_ci for (unsigned i = 0; i < 64; i++) { 324bf215546Sopenharmony_ci snprintf(path, 1023, "/sys/class/drm/card%d/device/boot_vga", i); 325bf215546Sopenharmony_ci fd = open(path, O_RDONLY); 326bf215546Sopenharmony_ci if (fd != -1) { 327bf215546Sopenharmony_ci uint8_t val; 328bf215546Sopenharmony_ci size_ret = read(fd, &val, 1); 329bf215546Sopenharmony_ci close(fd); 330bf215546Sopenharmony_ci if (size_ret == 1 && val == '1') 331bf215546Sopenharmony_ci boot_vga = 1; 332bf215546Sopenharmony_ci } else { 333bf215546Sopenharmony_ci return default_idx; 334bf215546Sopenharmony_ci } 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci if (boot_vga) { 337bf215546Sopenharmony_ci snprintf(path, 1023, "/sys/class/drm/card%d/device/config", i); 338bf215546Sopenharmony_ci fd = open(path, O_RDONLY); 339bf215546Sopenharmony_ci if (fd != -1) { 340bf215546Sopenharmony_ci size_ret = read(fd, &id, 4); 341bf215546Sopenharmony_ci close(fd); 342bf215546Sopenharmony_ci if (size_ret != 4) 343bf215546Sopenharmony_ci return default_idx; 344bf215546Sopenharmony_ci } else { 345bf215546Sopenharmony_ci return default_idx; 346bf215546Sopenharmony_ci } 347bf215546Sopenharmony_ci break; 348bf215546Sopenharmony_ci } 349bf215546Sopenharmony_ci } 350bf215546Sopenharmony_ci 351bf215546Sopenharmony_ci if (!boot_vga) 352bf215546Sopenharmony_ci return default_idx; 353bf215546Sopenharmony_ci 354bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 355bf215546Sopenharmony_ci if (id.vid == pci_infos[i].dev_info.vendor_id && 356bf215546Sopenharmony_ci id.did == pci_infos[i].dev_info.device_id) { 357bf215546Sopenharmony_ci default_idx = i; 358bf215546Sopenharmony_ci break; 359bf215546Sopenharmony_ci } 360bf215546Sopenharmony_ci } 361bf215546Sopenharmony_ci 362bf215546Sopenharmony_ci return default_idx; 363bf215546Sopenharmony_ci} 364bf215546Sopenharmony_ci 365bf215546Sopenharmony_cistatic int device_select_find_boot_vga_default(struct device_pci_info *pci_infos, 366bf215546Sopenharmony_ci uint32_t device_count) 367bf215546Sopenharmony_ci{ 368bf215546Sopenharmony_ci char boot_vga_path[1024]; 369bf215546Sopenharmony_ci int default_idx = -1; 370bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 371bf215546Sopenharmony_ci /* fallback to probing the pci bus boot_vga device. */ 372bf215546Sopenharmony_ci snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain, 373bf215546Sopenharmony_ci pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func); 374bf215546Sopenharmony_ci int fd = open(boot_vga_path, O_RDONLY); 375bf215546Sopenharmony_ci if (fd != -1) { 376bf215546Sopenharmony_ci uint8_t val; 377bf215546Sopenharmony_ci if (read(fd, &val, 1) == 1) { 378bf215546Sopenharmony_ci if (val == '1') 379bf215546Sopenharmony_ci default_idx = i; 380bf215546Sopenharmony_ci } 381bf215546Sopenharmony_ci close(fd); 382bf215546Sopenharmony_ci } 383bf215546Sopenharmony_ci if (default_idx != -1) 384bf215546Sopenharmony_ci break; 385bf215546Sopenharmony_ci } 386bf215546Sopenharmony_ci return default_idx; 387bf215546Sopenharmony_ci} 388bf215546Sopenharmony_ci 389bf215546Sopenharmony_cistatic int device_select_find_non_cpu(struct device_pci_info *pci_infos, 390bf215546Sopenharmony_ci uint32_t device_count) 391bf215546Sopenharmony_ci{ 392bf215546Sopenharmony_ci int default_idx = -1; 393bf215546Sopenharmony_ci 394bf215546Sopenharmony_ci /* pick first GPU device */ 395bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 396bf215546Sopenharmony_ci if (!pci_infos[i].cpu_device){ 397bf215546Sopenharmony_ci default_idx = i; 398bf215546Sopenharmony_ci break; 399bf215546Sopenharmony_ci } 400bf215546Sopenharmony_ci } 401bf215546Sopenharmony_ci return default_idx; 402bf215546Sopenharmony_ci} 403bf215546Sopenharmony_ci 404bf215546Sopenharmony_cistatic int find_non_cpu_skip(struct device_pci_info *pci_infos, 405bf215546Sopenharmony_ci uint32_t device_count, 406bf215546Sopenharmony_ci int skip_idx) 407bf215546Sopenharmony_ci{ 408bf215546Sopenharmony_ci for (unsigned i = 0; i < device_count; ++i) { 409bf215546Sopenharmony_ci if (i == skip_idx) 410bf215546Sopenharmony_ci continue; 411bf215546Sopenharmony_ci if (pci_infos[i].cpu_device) 412bf215546Sopenharmony_ci continue; 413bf215546Sopenharmony_ci return i; 414bf215546Sopenharmony_ci } 415bf215546Sopenharmony_ci return -1; 416bf215546Sopenharmony_ci} 417bf215546Sopenharmony_ci 418bf215546Sopenharmony_cistatic uint32_t get_default_device(const struct instance_info *info, 419bf215546Sopenharmony_ci const char *selection, 420bf215546Sopenharmony_ci uint32_t physical_device_count, 421bf215546Sopenharmony_ci VkPhysicalDevice *pPhysicalDevices) 422bf215546Sopenharmony_ci{ 423bf215546Sopenharmony_ci int default_idx = -1; 424bf215546Sopenharmony_ci const char *dri_prime = getenv("DRI_PRIME"); 425bf215546Sopenharmony_ci bool dri_prime_is_one = false; 426bf215546Sopenharmony_ci int cpu_count = 0; 427bf215546Sopenharmony_ci if (dri_prime && !strcmp(dri_prime, "1")) 428bf215546Sopenharmony_ci dri_prime_is_one = true; 429bf215546Sopenharmony_ci 430bf215546Sopenharmony_ci if (dri_prime && !dri_prime_is_one && !info->has_vulkan11 && !info->has_pci_bus) { 431bf215546Sopenharmony_ci fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n"); 432bf215546Sopenharmony_ci } 433bf215546Sopenharmony_ci 434bf215546Sopenharmony_ci struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info)); 435bf215546Sopenharmony_ci if (!pci_infos) 436bf215546Sopenharmony_ci return 0; 437bf215546Sopenharmony_ci 438bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_count; ++i) { 439bf215546Sopenharmony_ci cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0; 440bf215546Sopenharmony_ci } 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_ci if (selection) 443bf215546Sopenharmony_ci default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection); 444bf215546Sopenharmony_ci if (default_idx == -1 && info->has_vulkan11 && info->has_pci_bus && dri_prime && !dri_prime_is_one) 445bf215546Sopenharmony_ci default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime); 446bf215546Sopenharmony_ci if (default_idx == -1 && info->has_wayland) 447bf215546Sopenharmony_ci default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count); 448bf215546Sopenharmony_ci if (default_idx == -1 && info->has_xcb) 449bf215546Sopenharmony_ci default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count); 450bf215546Sopenharmony_ci if (default_idx == -1) { 451bf215546Sopenharmony_ci if (info->has_vulkan11 && info->has_pci_bus) 452bf215546Sopenharmony_ci default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count); 453bf215546Sopenharmony_ci else 454bf215546Sopenharmony_ci default_idx = device_select_find_boot_vga_vid_did(pci_infos, physical_device_count); 455bf215546Sopenharmony_ci } 456bf215546Sopenharmony_ci if (default_idx == -1 && cpu_count) 457bf215546Sopenharmony_ci default_idx = device_select_find_non_cpu(pci_infos, physical_device_count); 458bf215546Sopenharmony_ci /* DRI_PRIME=1 handling - pick any other device than default. */ 459bf215546Sopenharmony_ci if (default_idx != -1 && dri_prime_is_one && physical_device_count > (cpu_count + 1)) { 460bf215546Sopenharmony_ci if (default_idx == 0 || default_idx == 1) 461bf215546Sopenharmony_ci default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx); 462bf215546Sopenharmony_ci } 463bf215546Sopenharmony_ci free(pci_infos); 464bf215546Sopenharmony_ci return default_idx == -1 ? 0 : default_idx; 465bf215546Sopenharmony_ci} 466bf215546Sopenharmony_ci 467bf215546Sopenharmony_cistatic VkResult device_select_EnumeratePhysicalDevices(VkInstance instance, 468bf215546Sopenharmony_ci uint32_t* pPhysicalDeviceCount, 469bf215546Sopenharmony_ci VkPhysicalDevice *pPhysicalDevices) 470bf215546Sopenharmony_ci{ 471bf215546Sopenharmony_ci struct instance_info *info = device_select_layer_get_instance(instance); 472bf215546Sopenharmony_ci uint32_t physical_device_count = 0; 473bf215546Sopenharmony_ci uint32_t selected_physical_device_count = 0; 474bf215546Sopenharmony_ci const char* selection = getenv("MESA_VK_DEVICE_SELECT"); 475bf215546Sopenharmony_ci VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL); 476bf215546Sopenharmony_ci VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount); 477bf215546Sopenharmony_ci if (result != VK_SUCCESS) 478bf215546Sopenharmony_ci return result; 479bf215546Sopenharmony_ci 480bf215546Sopenharmony_ci VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice), physical_device_count); 481bf215546Sopenharmony_ci VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice), 482bf215546Sopenharmony_ci physical_device_count); 483bf215546Sopenharmony_ci 484bf215546Sopenharmony_ci if (!physical_devices || !selected_physical_devices) { 485bf215546Sopenharmony_ci result = VK_ERROR_OUT_OF_HOST_MEMORY; 486bf215546Sopenharmony_ci goto out; 487bf215546Sopenharmony_ci } 488bf215546Sopenharmony_ci 489bf215546Sopenharmony_ci result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices); 490bf215546Sopenharmony_ci if (result != VK_SUCCESS) 491bf215546Sopenharmony_ci goto out; 492bf215546Sopenharmony_ci 493bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_count; i++) { 494bf215546Sopenharmony_ci uint32_t count; 495bf215546Sopenharmony_ci info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL); 496bf215546Sopenharmony_ci if (count > 0) { 497bf215546Sopenharmony_ci VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties)); 498bf215546Sopenharmony_ci if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) { 499bf215546Sopenharmony_ci for (unsigned j = 0; j < count; j++) { 500bf215546Sopenharmony_ci if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) 501bf215546Sopenharmony_ci info->has_pci_bus = true; 502bf215546Sopenharmony_ci } 503bf215546Sopenharmony_ci } 504bf215546Sopenharmony_ci free(extensions); 505bf215546Sopenharmony_ci } 506bf215546Sopenharmony_ci } 507bf215546Sopenharmony_ci if (selection && strcmp(selection, "list") == 0) { 508bf215546Sopenharmony_ci fprintf(stderr, "selectable devices:\n"); 509bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_count; ++i) 510bf215546Sopenharmony_ci print_gpu(info, i, physical_devices[i]); 511bf215546Sopenharmony_ci exit(0); 512bf215546Sopenharmony_ci } else { 513bf215546Sopenharmony_ci unsigned selected_index = get_default_device(info, selection, physical_device_count, physical_devices); 514bf215546Sopenharmony_ci selected_physical_device_count = physical_device_count; 515bf215546Sopenharmony_ci selected_physical_devices[0] = physical_devices[selected_index]; 516bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_count - 1; ++i) { 517bf215546Sopenharmony_ci unsigned this_idx = i < selected_index ? i : i + 1; 518bf215546Sopenharmony_ci selected_physical_devices[i + 1] = physical_devices[this_idx]; 519bf215546Sopenharmony_ci } 520bf215546Sopenharmony_ci } 521bf215546Sopenharmony_ci 522bf215546Sopenharmony_ci if (selected_physical_device_count == 0) { 523bf215546Sopenharmony_ci fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n"); 524bf215546Sopenharmony_ci } 525bf215546Sopenharmony_ci 526bf215546Sopenharmony_ci assert(result == VK_SUCCESS); 527bf215546Sopenharmony_ci 528bf215546Sopenharmony_ci /* do not give multiple device option to app if force default device */ 529bf215546Sopenharmony_ci const char *force_default_device = getenv("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE"); 530bf215546Sopenharmony_ci if (force_default_device && !strcmp(force_default_device, "1") && selected_physical_device_count != 0) 531bf215546Sopenharmony_ci selected_physical_device_count = 1; 532bf215546Sopenharmony_ci 533bf215546Sopenharmony_ci for (unsigned i = 0; i < selected_physical_device_count; i++) { 534bf215546Sopenharmony_ci vk_outarray_append_typed(VkPhysicalDevice, &out, ent) { 535bf215546Sopenharmony_ci *ent = selected_physical_devices[i]; 536bf215546Sopenharmony_ci } 537bf215546Sopenharmony_ci } 538bf215546Sopenharmony_ci result = vk_outarray_status(&out); 539bf215546Sopenharmony_ci out: 540bf215546Sopenharmony_ci free(physical_devices); 541bf215546Sopenharmony_ci free(selected_physical_devices); 542bf215546Sopenharmony_ci return result; 543bf215546Sopenharmony_ci} 544bf215546Sopenharmony_ci 545bf215546Sopenharmony_cistatic VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance, 546bf215546Sopenharmony_ci uint32_t* pPhysicalDeviceGroupCount, 547bf215546Sopenharmony_ci VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups) 548bf215546Sopenharmony_ci{ 549bf215546Sopenharmony_ci struct instance_info *info = device_select_layer_get_instance(instance); 550bf215546Sopenharmony_ci uint32_t physical_device_group_count = 0; 551bf215546Sopenharmony_ci uint32_t selected_physical_device_group_count = 0; 552bf215546Sopenharmony_ci VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL); 553bf215546Sopenharmony_ci VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount); 554bf215546Sopenharmony_ci 555bf215546Sopenharmony_ci if (result != VK_SUCCESS) 556bf215546Sopenharmony_ci return result; 557bf215546Sopenharmony_ci 558bf215546Sopenharmony_ci VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count); 559bf215546Sopenharmony_ci VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count); 560bf215546Sopenharmony_ci 561bf215546Sopenharmony_ci if (!physical_device_groups || !selected_physical_device_groups) { 562bf215546Sopenharmony_ci result = VK_ERROR_OUT_OF_HOST_MEMORY; 563bf215546Sopenharmony_ci goto out; 564bf215546Sopenharmony_ci } 565bf215546Sopenharmony_ci 566bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_group_count; i++) 567bf215546Sopenharmony_ci physical_device_groups[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES; 568bf215546Sopenharmony_ci 569bf215546Sopenharmony_ci result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups); 570bf215546Sopenharmony_ci if (result != VK_SUCCESS) 571bf215546Sopenharmony_ci goto out; 572bf215546Sopenharmony_ci 573bf215546Sopenharmony_ci /* just sort groups with CPU devices to the end? - assume nobody will mix these */ 574bf215546Sopenharmony_ci int num_gpu_groups = 0; 575bf215546Sopenharmony_ci int num_cpu_groups = 0; 576bf215546Sopenharmony_ci selected_physical_device_group_count = physical_device_group_count; 577bf215546Sopenharmony_ci for (unsigned i = 0; i < physical_device_group_count; i++) { 578bf215546Sopenharmony_ci bool group_has_cpu_device = false; 579bf215546Sopenharmony_ci for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) { 580bf215546Sopenharmony_ci VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j]; 581bf215546Sopenharmony_ci VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){ 582bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 583bf215546Sopenharmony_ci }; 584bf215546Sopenharmony_ci info->GetPhysicalDeviceProperties(physical_device, &properties.properties); 585bf215546Sopenharmony_ci group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU; 586bf215546Sopenharmony_ci } 587bf215546Sopenharmony_ci 588bf215546Sopenharmony_ci if (group_has_cpu_device) { 589bf215546Sopenharmony_ci selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i]; 590bf215546Sopenharmony_ci num_cpu_groups++; 591bf215546Sopenharmony_ci } else { 592bf215546Sopenharmony_ci selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i]; 593bf215546Sopenharmony_ci num_gpu_groups++; 594bf215546Sopenharmony_ci } 595bf215546Sopenharmony_ci } 596bf215546Sopenharmony_ci 597bf215546Sopenharmony_ci assert(result == VK_SUCCESS); 598bf215546Sopenharmony_ci 599bf215546Sopenharmony_ci for (unsigned i = 0; i < selected_physical_device_group_count; i++) { 600bf215546Sopenharmony_ci vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, ent) { 601bf215546Sopenharmony_ci *ent = selected_physical_device_groups[i]; 602bf215546Sopenharmony_ci } 603bf215546Sopenharmony_ci } 604bf215546Sopenharmony_ci result = vk_outarray_status(&out); 605bf215546Sopenharmony_ciout: 606bf215546Sopenharmony_ci free(physical_device_groups); 607bf215546Sopenharmony_ci free(selected_physical_device_groups); 608bf215546Sopenharmony_ci return result; 609bf215546Sopenharmony_ci} 610bf215546Sopenharmony_ci 611bf215546Sopenharmony_cistatic void (*get_instance_proc_addr(VkInstance instance, const char* name))() 612bf215546Sopenharmony_ci{ 613bf215546Sopenharmony_ci if (strcmp(name, "vkGetInstanceProcAddr") == 0) 614bf215546Sopenharmony_ci return (void(*)())get_instance_proc_addr; 615bf215546Sopenharmony_ci if (strcmp(name, "vkCreateInstance") == 0) 616bf215546Sopenharmony_ci return (void(*)())device_select_CreateInstance; 617bf215546Sopenharmony_ci if (strcmp(name, "vkDestroyInstance") == 0) 618bf215546Sopenharmony_ci return (void(*)())device_select_DestroyInstance; 619bf215546Sopenharmony_ci if (strcmp(name, "vkEnumeratePhysicalDevices") == 0) 620bf215546Sopenharmony_ci return (void(*)())device_select_EnumeratePhysicalDevices; 621bf215546Sopenharmony_ci if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0) 622bf215546Sopenharmony_ci return (void(*)())device_select_EnumeratePhysicalDeviceGroups; 623bf215546Sopenharmony_ci 624bf215546Sopenharmony_ci struct instance_info *info = device_select_layer_get_instance(instance); 625bf215546Sopenharmony_ci return info->GetInstanceProcAddr(instance, name); 626bf215546Sopenharmony_ci} 627bf215546Sopenharmony_ci 628bf215546Sopenharmony_ciVK_LAYER_EXPORT VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct) 629bf215546Sopenharmony_ci{ 630bf215546Sopenharmony_ci if (pVersionStruct->loaderLayerInterfaceVersion < 2) 631bf215546Sopenharmony_ci return VK_ERROR_INITIALIZATION_FAILED; 632bf215546Sopenharmony_ci pVersionStruct->loaderLayerInterfaceVersion = 2; 633bf215546Sopenharmony_ci 634bf215546Sopenharmony_ci pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr; 635bf215546Sopenharmony_ci 636bf215546Sopenharmony_ci return VK_SUCCESS; 637bf215546Sopenharmony_ci} 638