• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    bool has_wayland = getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET");
150    bool has_xcb = !!getenv("DISPLAY");
151 
152    for (unsigned i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
153 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
154       if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME) && has_wayland)
155          info->has_wayland = true;
156 #endif
157 #ifdef VK_USE_PLATFORM_XCB_KHR
158       if (!strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_XCB_SURFACE_EXTENSION_NAME) && has_xcb)
159          info->has_xcb = !info->xserver || !info->zink;
160 #endif
161    }
162 
163    /*
164     * The loader is currently not able to handle GetPhysicalDeviceProperties2KHR calls in
165     * EnumeratePhysicalDevices when there are other layers present. To avoid mysterious crashes
166     * for users just use only the vulkan version for now.
167     */
168    info->has_vulkan11 = pCreateInfo->pApplicationInfo &&
169                         pCreateInfo->pApplicationInfo->apiVersion >= VK_MAKE_VERSION(1, 1, 0);
170 
171 #define DEVSEL_GET_CB(func) info->func = (PFN_vk##func)info->GetInstanceProcAddr(*pInstance, "vk" #func)
172    DEVSEL_GET_CB(DestroyInstance);
173    DEVSEL_GET_CB(EnumeratePhysicalDevices);
174    DEVSEL_GET_CB(EnumeratePhysicalDeviceGroups);
175    DEVSEL_GET_CB(GetPhysicalDeviceProperties);
176    DEVSEL_GET_CB(EnumerateDeviceExtensionProperties);
177    if (info->has_vulkan11)
178       DEVSEL_GET_CB(GetPhysicalDeviceProperties2);
179 #undef DEVSEL_GET_CB
180 
181    device_select_layer_add_instance(*pInstance, info);
182 
183    return VK_SUCCESS;
184 }
185 
device_select_DestroyInstance(VkInstance instance,const VkAllocationCallbacks * pAllocator)186 static void device_select_DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
187 {
188    struct instance_info *info = device_select_layer_get_instance(instance);
189 
190    device_select_layer_remove_instance(instance);
191    info->DestroyInstance(instance, pAllocator);
192    free(info);
193 }
194 
get_device_properties(const struct instance_info * info,VkPhysicalDevice device,VkPhysicalDeviceProperties2 * properties)195 static void get_device_properties(const struct instance_info *info, VkPhysicalDevice device, VkPhysicalDeviceProperties2 *properties)
196 {
197     info->GetPhysicalDeviceProperties(device, &properties->properties);
198 
199     if (info->GetPhysicalDeviceProperties2 && properties->properties.apiVersion >= VK_API_VERSION_1_1)
200         info->GetPhysicalDeviceProperties2(device, properties);
201 }
202 
print_gpu(const struct instance_info * info,unsigned index,VkPhysicalDevice device)203 static void print_gpu(const struct instance_info *info, unsigned index, VkPhysicalDevice device)
204 {
205    const char *type = "";
206    VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {
207       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
208    };
209    VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){
210       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
211    };
212    if (info->has_vulkan11 && info->has_pci_bus)
213       properties.pNext = &ext_pci_properties;
214    get_device_properties(info, device, &properties);
215 
216    switch(properties.properties.deviceType) {
217    case VK_PHYSICAL_DEVICE_TYPE_OTHER:
218    default:
219       type = "other";
220       break;
221    case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
222       type = "integrated GPU";
223       break;
224    case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
225       type = "discrete GPU";
226       break;
227    case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
228       type = "virtual GPU";
229       break;
230    case VK_PHYSICAL_DEVICE_TYPE_CPU:
231       type = "CPU";
232       break;
233    }
234    fprintf(stderr, "  GPU %d: %x:%x \"%s\" %s", index, properties.properties.vendorID,
235            properties.properties.deviceID, properties.properties.deviceName, type);
236    if (info->has_vulkan11 && info->has_pci_bus)
237       fprintf(stderr, " %04x:%02x:%02x.%x", ext_pci_properties.pciDomain,
238               ext_pci_properties.pciBus, ext_pci_properties.pciDevice,
239               ext_pci_properties.pciFunction);
240    fprintf(stderr, "\n");
241 }
242 
fill_drm_device_info(const struct instance_info * info,struct device_pci_info * drm_device,VkPhysicalDevice device)243 static bool fill_drm_device_info(const struct instance_info *info,
244                                  struct device_pci_info *drm_device,
245                                  VkPhysicalDevice device)
246 {
247    VkPhysicalDevicePCIBusInfoPropertiesEXT ext_pci_properties = (VkPhysicalDevicePCIBusInfoPropertiesEXT) {
248       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT
249    };
250 
251    VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){
252       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
253    };
254 
255    if (info->has_vulkan11 && info->has_pci_bus)
256       properties.pNext = &ext_pci_properties;
257    get_device_properties(info, device, &properties);
258 
259    drm_device->cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
260    drm_device->dev_info.vendor_id = properties.properties.vendorID;
261    drm_device->dev_info.device_id = properties.properties.deviceID;
262    if (info->has_vulkan11 && info->has_pci_bus) {
263      drm_device->has_bus_info = true;
264      drm_device->bus_info.domain = ext_pci_properties.pciDomain;
265      drm_device->bus_info.bus = ext_pci_properties.pciBus;
266      drm_device->bus_info.dev = ext_pci_properties.pciDevice;
267      drm_device->bus_info.func = ext_pci_properties.pciFunction;
268    }
269    return drm_device->cpu_device;
270 }
271 
device_select_find_explicit_default(struct device_pci_info * pci_infos,uint32_t device_count,const char * selection)272 static int device_select_find_explicit_default(struct device_pci_info *pci_infos,
273                                                uint32_t device_count,
274                                                const char *selection)
275 {
276    int default_idx = -1;
277    unsigned vendor_id, device_id;
278    int matched = sscanf(selection, "%x:%x", &vendor_id, &device_id);
279    if (matched != 2)
280       return default_idx;
281 
282    for (unsigned i = 0; i < device_count; ++i) {
283       if (pci_infos[i].dev_info.vendor_id == vendor_id &&
284           pci_infos[i].dev_info.device_id == device_id)
285          default_idx = i;
286    }
287    return default_idx;
288 }
289 
device_select_find_dri_prime_tag_default(struct device_pci_info * pci_infos,uint32_t device_count,const char * dri_prime)290 static int device_select_find_dri_prime_tag_default(struct device_pci_info *pci_infos,
291                                                     uint32_t device_count,
292                                                     const char *dri_prime)
293 {
294    int default_idx = -1;
295 
296    /* Drop the trailing '!' if present. */
297    int ref = strlen("pci-xxxx_yy_zz_w");
298    int n = strlen(dri_prime);
299    if (n < ref)
300       return default_idx;
301    if (n == ref + 1 && dri_prime[n - 1] == '!')
302       n--;
303 
304    for (unsigned i = 0; i < device_count; ++i) {
305       char *tag = NULL;
306       if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
307                    pci_infos[i].bus_info.domain,
308                    pci_infos[i].bus_info.bus,
309                    pci_infos[i].bus_info.dev,
310                    pci_infos[i].bus_info.func) >= 0) {
311          if (strncmp(dri_prime, tag, n) == 0)
312             default_idx = i;
313       }
314       free(tag);
315    }
316    return default_idx;
317 }
318 
device_select_find_boot_vga_vid_did(struct device_pci_info * pci_infos,uint32_t device_count)319 static int device_select_find_boot_vga_vid_did(struct device_pci_info *pci_infos,
320                                                uint32_t device_count)
321 {
322    char path[1024];
323    int fd;
324    int default_idx = -1;
325    uint8_t boot_vga = 0;
326    ssize_t size_ret;
327    #pragma pack(push, 1)
328    struct id {
329       uint16_t vid;
330       uint16_t did;
331    }id;
332    #pragma pack(pop)
333 
334    for (unsigned i = 0; i < 64; i++) {
335       snprintf(path, 1023, "/sys/class/drm/card%d/device/boot_vga", i);
336       fd = open(path, O_RDONLY);
337       if (fd != -1) {
338          uint8_t val;
339          size_ret = read(fd, &val, 1);
340          close(fd);
341          if (size_ret == 1 && val == '1')
342             boot_vga = 1;
343       } else {
344          return default_idx;
345       }
346 
347       if (boot_vga) {
348          snprintf(path, 1023, "/sys/class/drm/card%d/device/config", i);
349          fd = open(path, O_RDONLY);
350          if (fd != -1) {
351             size_ret = read(fd, &id, 4);
352             close(fd);
353             if (size_ret != 4)
354                return default_idx;
355          } else {
356             return default_idx;
357          }
358          break;
359       }
360    }
361 
362    if (!boot_vga)
363       return default_idx;
364 
365    for (unsigned i = 0; i < device_count; ++i) {
366       if (id.vid == pci_infos[i].dev_info.vendor_id &&
367           id.did == pci_infos[i].dev_info.device_id) {
368          default_idx = i;
369          break;
370       }
371    }
372 
373    return default_idx;
374 }
375 
device_select_find_boot_vga_default(struct device_pci_info * pci_infos,uint32_t device_count)376 static int device_select_find_boot_vga_default(struct device_pci_info *pci_infos,
377                                                uint32_t device_count)
378 {
379    char boot_vga_path[1024];
380    int default_idx = -1;
381    for (unsigned i = 0; i < device_count; ++i) {
382       /* fallback to probing the pci bus boot_vga device. */
383       snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain,
384                pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func);
385       int fd = open(boot_vga_path, O_RDONLY);
386       if (fd != -1) {
387          uint8_t val;
388          if (read(fd, &val, 1) == 1) {
389             if (val == '1')
390                default_idx = i;
391          }
392          close(fd);
393       }
394       if (default_idx != -1)
395          break;
396    }
397    return default_idx;
398 }
399 
device_select_find_non_cpu(struct device_pci_info * pci_infos,uint32_t device_count)400 static int device_select_find_non_cpu(struct device_pci_info *pci_infos,
401                                       uint32_t device_count)
402 {
403    int default_idx = -1;
404 
405    /* pick first GPU device */
406    for (unsigned i = 0; i < device_count; ++i) {
407       if (!pci_infos[i].cpu_device){
408          default_idx = i;
409          break;
410       }
411    }
412    return default_idx;
413 }
414 
find_non_cpu_skip(struct device_pci_info * pci_infos,uint32_t device_count,int skip_idx,int skip_count)415 static int find_non_cpu_skip(struct device_pci_info *pci_infos,
416                         uint32_t device_count,
417                         int skip_idx,
418                         int skip_count)
419 {
420    for (unsigned i = 0; i < device_count; ++i) {
421       if (i == skip_idx)
422          continue;
423       if (pci_infos[i].cpu_device)
424          continue;
425       skip_count--;
426       if (skip_count > 0)
427          continue;
428 
429       return i;
430    }
431    return -1;
432 }
433 
should_debug_device_selection()434 static bool should_debug_device_selection() {
435    return debug_get_bool_option("MESA_VK_DEVICE_SELECT_DEBUG", false) ||
436       debug_get_bool_option("DRI_PRIME_DEBUG", false);
437 }
438 
ends_with_exclamation_mark(const char * str)439 static bool ends_with_exclamation_mark(const char *str) {
440    size_t n = strlen(str);
441    return n > 1 && str[n - 1] == '!';
442 }
443 
get_default_device(const struct instance_info * info,const char * selection,uint32_t physical_device_count,VkPhysicalDevice * pPhysicalDevices,bool * expose_only_one_dev)444 static uint32_t get_default_device(const struct instance_info *info,
445                                    const char *selection,
446                                    uint32_t physical_device_count,
447                                    VkPhysicalDevice *pPhysicalDevices,
448                                    bool *expose_only_one_dev)
449 {
450    int default_idx = -1;
451    const char *dri_prime = getenv("DRI_PRIME");
452    bool debug = should_debug_device_selection();
453    int dri_prime_as_int = -1;
454    int cpu_count = 0;
455    if (dri_prime) {
456       if (strchr(dri_prime, ':') == NULL)
457          dri_prime_as_int = atoi(dri_prime);
458 
459       if (dri_prime_as_int < 0)
460          dri_prime_as_int = 0;
461    }
462 
463    struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info));
464    if (!pci_infos)
465      return 0;
466 
467    for (unsigned i = 0; i < physical_device_count; ++i) {
468       cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;
469    }
470 
471    if (selection)
472       default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection);
473    if (default_idx != -1) {
474       *expose_only_one_dev = ends_with_exclamation_mark(selection);
475    }
476 
477    if (default_idx == -1 && dri_prime && dri_prime_as_int == 0) {
478       /* Try DRI_PRIME=vendor_id:device_id */
479       default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, dri_prime);
480       if (default_idx != -1) {
481          if (debug)
482             fprintf(stderr, "device-select: device_select_find_explicit_default selected %i\n", default_idx);
483          *expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
484       }
485 
486       if (default_idx == -1) {
487          /* Try DRI_PRIME=pci-xxxx_yy_zz_w */
488          if (!info->has_vulkan11 && !info->has_pci_bus)
489             fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");
490          else
491             default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime);
492 
493          if (default_idx != -1) {
494             if (debug)
495                fprintf(stderr, "device-select: device_select_find_dri_prime_tag_default selected %i\n", default_idx);
496             *expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
497          }
498       }
499    }
500    if (default_idx == -1 && info->has_wayland) {
501       default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);
502       if (debug && default_idx != -1)
503          fprintf(stderr, "device-select: device_select_find_wayland_pci_default selected %i\n", default_idx);
504    }
505    if (default_idx == -1 && info->has_xcb) {
506       default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);
507       if (debug && default_idx != -1)
508          fprintf(stderr, "device-select: device_select_find_xcb_pci_default selected %i\n", default_idx);
509    }
510    if (default_idx == -1) {
511       if (info->has_vulkan11 && info->has_pci_bus)
512          default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);
513       else
514          default_idx = device_select_find_boot_vga_vid_did(pci_infos, physical_device_count);
515       if (debug && default_idx != -1)
516          fprintf(stderr, "device-select: device_select_find_boot_vga selected %i\n", default_idx);
517    }
518    if (default_idx == -1 && cpu_count) {
519       default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
520       if (debug && default_idx != -1)
521          fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
522    }
523    /* If no GPU has been selected so far, select the first non-CPU device. If none are available,
524     * pick the first CPU device.
525     */
526    if (default_idx == -1) {
527       default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
528       if (default_idx != -1) {
529          if (debug)
530             fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
531       } else if (cpu_count) {
532          default_idx = 0;
533       }
534    }
535    /* DRI_PRIME=n handling - pick any other device than default. */
536    if (dri_prime_as_int > 0 && debug)
537       fprintf(stderr, "device-select: DRI_PRIME=%d, default_idx so far: %i\n", dri_prime_as_int, default_idx);
538    if (dri_prime_as_int > 0 && physical_device_count > (cpu_count + 1)) {
539       if (default_idx == 0 || default_idx == 1) {
540          default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx, dri_prime_as_int);
541          if (default_idx != -1) {
542             if (debug)
543                fprintf(stderr, "device-select: find_non_cpu_skip selected %i\n", default_idx);
544             *expose_only_one_dev = ends_with_exclamation_mark(dri_prime);
545          }
546       }
547    }
548    free(pci_infos);
549    return default_idx == -1 ? 0 : default_idx;
550 }
551 
device_select_EnumeratePhysicalDevices(VkInstance instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)552 static VkResult device_select_EnumeratePhysicalDevices(VkInstance instance,
553 						       uint32_t* pPhysicalDeviceCount,
554 						       VkPhysicalDevice *pPhysicalDevices)
555 {
556    struct instance_info *info = device_select_layer_get_instance(instance);
557    uint32_t physical_device_count = 0;
558    uint32_t selected_physical_device_count = 0;
559    const char* selection = getenv("MESA_VK_DEVICE_SELECT");
560    bool expose_only_one_dev = false;
561    if (info->zink && info->xwayland)
562       return info->EnumeratePhysicalDevices(instance, pPhysicalDeviceCount, pPhysicalDevices);
563    VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL);
564    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
565    if (result != VK_SUCCESS)
566       return result;
567 
568    VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),  physical_device_count);
569    VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),
570                                                                            physical_device_count);
571 
572    if (!physical_devices || !selected_physical_devices) {
573       result = VK_ERROR_OUT_OF_HOST_MEMORY;
574       goto out;
575    }
576 
577    result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices);
578    if (result != VK_SUCCESS)
579       goto out;
580 
581    for (unsigned i = 0; i < physical_device_count; i++) {
582       uint32_t count;
583       info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL);
584       if (count > 0) {
585 	 VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties));
586          if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) {
587 	    for (unsigned j = 0; j < count; j++) {
588                if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME))
589                   info->has_pci_bus = true;
590             }
591          }
592 	 free(extensions);
593       }
594    }
595    if (should_debug_device_selection() || (selection && strcmp(selection, "list") == 0)) {
596       fprintf(stderr, "selectable devices:\n");
597       for (unsigned i = 0; i < physical_device_count; ++i)
598          print_gpu(info, i, physical_devices[i]);
599 
600       if (selection && strcmp(selection, "list") == 0)
601          exit(0);
602    }
603 
604    unsigned selected_index = get_default_device(info, selection, physical_device_count,
605                                                 physical_devices, &expose_only_one_dev);
606    selected_physical_device_count = physical_device_count;
607    selected_physical_devices[0] = physical_devices[selected_index];
608    for (unsigned i = 0; i < physical_device_count - 1; ++i) {
609       unsigned  this_idx = i < selected_index ? i : i + 1;
610       selected_physical_devices[i + 1] = physical_devices[this_idx];
611    }
612 
613    if (selected_physical_device_count == 0) {
614       fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n");
615    }
616 
617    assert(result == VK_SUCCESS);
618 
619    /* do not give multiple device option to app if force default device */
620    const char *force_default_device = getenv("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE");
621    if (force_default_device && !strcmp(force_default_device, "1") && selected_physical_device_count != 0)
622       expose_only_one_dev = true;
623 
624    if (expose_only_one_dev)
625       selected_physical_device_count = 1;
626 
627    for (unsigned i = 0; i < selected_physical_device_count; i++) {
628       vk_outarray_append_typed(VkPhysicalDevice, &out, ent) {
629          *ent = selected_physical_devices[i];
630       }
631    }
632    result = vk_outarray_status(&out);
633  out:
634    free(physical_devices);
635    free(selected_physical_devices);
636    return result;
637 }
638 
device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,uint32_t * pPhysicalDeviceGroupCount,VkPhysicalDeviceGroupProperties * pPhysicalDeviceGroups)639 static VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,
640                                                             uint32_t* pPhysicalDeviceGroupCount,
641                                                             VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups)
642 {
643    struct instance_info *info = device_select_layer_get_instance(instance);
644    uint32_t physical_device_group_count = 0;
645    uint32_t selected_physical_device_group_count = 0;
646    if (info->zink && info->xwayland)
647       return info->EnumeratePhysicalDeviceGroups(instance, pPhysicalDeviceGroupCount, pPhysicalDeviceGroups);
648    VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL);
649    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount);
650 
651    if (result != VK_SUCCESS)
652       return result;
653 
654    VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
655    VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
656 
657    if (!physical_device_groups || !selected_physical_device_groups) {
658       result = VK_ERROR_OUT_OF_HOST_MEMORY;
659       goto out;
660    }
661 
662    for (unsigned i = 0; i < physical_device_group_count; i++)
663       physical_device_groups[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
664 
665    result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups);
666    if (result != VK_SUCCESS)
667       goto out;
668 
669    /* just sort groups with CPU devices to the end? - assume nobody will mix these */
670    int num_gpu_groups = 0;
671    int num_cpu_groups = 0;
672    selected_physical_device_group_count = physical_device_group_count;
673    for (unsigned i = 0; i < physical_device_group_count; i++) {
674       bool group_has_cpu_device = false;
675       for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) {
676          VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j];
677          VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){
678             .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
679          };
680          info->GetPhysicalDeviceProperties(physical_device, &properties.properties);
681          group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
682       }
683 
684       if (group_has_cpu_device) {
685          selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i];
686          num_cpu_groups++;
687       } else {
688          selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i];
689          num_gpu_groups++;
690       }
691    }
692 
693    assert(result == VK_SUCCESS);
694 
695    for (unsigned i = 0; i < selected_physical_device_group_count; i++) {
696       vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, ent) {
697          *ent = selected_physical_device_groups[i];
698       }
699    }
700    result = vk_outarray_status(&out);
701 out:
702    free(physical_device_groups);
703    free(selected_physical_device_groups);
704    return result;
705 }
706 
get_instance_proc_addr(VkInstance instance,const char * name)707 static void  (*get_instance_proc_addr(VkInstance instance, const char* name))()
708 {
709    if (strcmp(name, "vkGetInstanceProcAddr") == 0)
710       return (void(*)())get_instance_proc_addr;
711    if (strcmp(name, "vkCreateInstance") == 0)
712       return (void(*)())device_select_CreateInstance;
713    if (strcmp(name, "vkDestroyInstance") == 0)
714       return (void(*)())device_select_DestroyInstance;
715    if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)
716       return (void(*)())device_select_EnumeratePhysicalDevices;
717    if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
718       return (void(*)())device_select_EnumeratePhysicalDeviceGroups;
719 
720    struct instance_info *info = device_select_layer_get_instance(instance);
721    return info->GetInstanceProcAddr(instance, name);
722 }
723 
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface * pVersionStruct)724 PUBLIC VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
725 {
726    if (pVersionStruct->loaderLayerInterfaceVersion < 2)
727       return VK_ERROR_INITIALIZATION_FAILED;
728    pVersionStruct->loaderLayerInterfaceVersion = 2;
729 
730    pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr;
731 
732    return VK_SUCCESS;
733 }
734