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