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