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    for (unsigned i = 0; i < device_count; ++i) {
288       char *tag = NULL;
289       if (asprintf(&tag, "pci-%04x_%02x_%02x_%1u",
290                    pci_infos[i].bus_info.domain,
291                    pci_infos[i].bus_info.bus,
292                    pci_infos[i].bus_info.dev,
293                    pci_infos[i].bus_info.func) >= 0) {
294          if (strcmp(dri_prime, tag) == 0)
295             default_idx = i;
296       }
297       free(tag);
298    }
299    return default_idx;
300 }
301 
device_select_find_boot_vga_vid_did(struct device_pci_info * pci_infos,uint32_t device_count)302 static int device_select_find_boot_vga_vid_did(struct device_pci_info *pci_infos,
303                                                uint32_t device_count)
304 {
305    char path[1024];
306    int fd;
307    int default_idx = -1;
308    uint8_t boot_vga = 0;
309    ssize_t size_ret;
310    #pragma pack(push, 1)
311    struct id {
312       uint16_t vid;
313       uint16_t did;
314    }id;
315    #pragma pack(pop)
316 
317    for (unsigned i = 0; i < 64; i++) {
318       snprintf(path, 1023, "/sys/class/drm/card%d/device/boot_vga", i);
319       fd = open(path, O_RDONLY);
320       if (fd != -1) {
321          uint8_t val;
322          size_ret = read(fd, &val, 1);
323          close(fd);
324          if (size_ret == 1 && val == '1')
325             boot_vga = 1;
326       } else {
327          return default_idx;
328       }
329 
330       if (boot_vga) {
331          snprintf(path, 1023, "/sys/class/drm/card%d/device/config", i);
332          fd = open(path, O_RDONLY);
333          if (fd != -1) {
334             size_ret = read(fd, &id, 4);
335             close(fd);
336             if (size_ret != 4)
337                return default_idx;
338          } else {
339             return default_idx;
340          }
341          break;
342       }
343    }
344 
345    if (!boot_vga)
346       return default_idx;
347 
348    for (unsigned i = 0; i < device_count; ++i) {
349       if (id.vid == pci_infos[i].dev_info.vendor_id &&
350           id.did == pci_infos[i].dev_info.device_id) {
351          default_idx = i;
352          break;
353       }
354    }
355 
356    return default_idx;
357 }
358 
device_select_find_boot_vga_default(struct device_pci_info * pci_infos,uint32_t device_count)359 static int device_select_find_boot_vga_default(struct device_pci_info *pci_infos,
360                                                uint32_t device_count)
361 {
362    char boot_vga_path[1024];
363    int default_idx = -1;
364    for (unsigned i = 0; i < device_count; ++i) {
365       /* fallback to probing the pci bus boot_vga device. */
366       snprintf(boot_vga_path, 1023, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/boot_vga", pci_infos[i].bus_info.domain,
367                pci_infos[i].bus_info.bus, pci_infos[i].bus_info.dev, pci_infos[i].bus_info.func);
368       int fd = open(boot_vga_path, O_RDONLY);
369       if (fd != -1) {
370          uint8_t val;
371          if (read(fd, &val, 1) == 1) {
372             if (val == '1')
373                default_idx = i;
374          }
375          close(fd);
376       }
377       if (default_idx != -1)
378          break;
379    }
380    return default_idx;
381 }
382 
device_select_find_non_cpu(struct device_pci_info * pci_infos,uint32_t device_count)383 static int device_select_find_non_cpu(struct device_pci_info *pci_infos,
384                                       uint32_t device_count)
385 {
386    int default_idx = -1;
387 
388    /* pick first GPU device */
389    for (unsigned i = 0; i < device_count; ++i) {
390       if (!pci_infos[i].cpu_device){
391          default_idx = i;
392          break;
393       }
394    }
395    return default_idx;
396 }
397 
find_non_cpu_skip(struct device_pci_info * pci_infos,uint32_t device_count,int skip_idx)398 static int find_non_cpu_skip(struct device_pci_info *pci_infos,
399                         uint32_t device_count,
400                         int skip_idx)
401 {
402    for (unsigned i = 0; i < device_count; ++i) {
403       if (i == skip_idx)
404          continue;
405       if (pci_infos[i].cpu_device)
406          continue;
407       return i;
408    }
409    return -1;
410 }
411 
should_debug_device_selection()412 static bool should_debug_device_selection() {
413    return debug_get_bool_option("MESA_VK_DEVICE_SELECT_DEBUG", false) ||
414       debug_get_bool_option("DRI_PRIME_DEBUG", false);
415 }
416 
get_default_device(const struct instance_info * info,const char * selection,uint32_t physical_device_count,VkPhysicalDevice * pPhysicalDevices)417 static uint32_t get_default_device(const struct instance_info *info,
418                                    const char *selection,
419                                    uint32_t physical_device_count,
420                                    VkPhysicalDevice *pPhysicalDevices)
421 {
422    int default_idx = -1;
423    const char *dri_prime = getenv("DRI_PRIME");
424    bool debug = should_debug_device_selection();
425    bool dri_prime_is_one = false;
426    int cpu_count = 0;
427    if (dri_prime && !strcmp(dri_prime, "1"))
428       dri_prime_is_one = true;
429 
430    struct device_pci_info *pci_infos = (struct device_pci_info *)calloc(physical_device_count, sizeof(struct device_pci_info));
431    if (!pci_infos)
432      return 0;
433 
434    for (unsigned i = 0; i < physical_device_count; ++i) {
435       cpu_count += fill_drm_device_info(info, &pci_infos[i], pPhysicalDevices[i]) ? 1 : 0;
436    }
437 
438    if (selection)
439       default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, selection);
440 
441    if (default_idx == -1 && dri_prime && !dri_prime_is_one) {
442       /* Try DRI_PRIME=vendor_id:device_id */
443       default_idx = device_select_find_explicit_default(pci_infos, physical_device_count, dri_prime);
444       if (debug && default_idx != -1)
445          fprintf(stderr, "device-select: device_select_find_explicit_default selected %i\n", default_idx);
446 
447       if (default_idx == -1) {
448          /* Try DRI_PRIME=pci-xxxx_yy_zz_w */
449          if (!info->has_vulkan11 && !info->has_pci_bus)
450             fprintf(stderr, "device-select: cannot correctly use DRI_PRIME tag\n");
451          else
452             default_idx = device_select_find_dri_prime_tag_default(pci_infos, physical_device_count, dri_prime);
453 
454          if (debug && default_idx != -1)
455             fprintf(stderr, "device-select: device_select_find_dri_prime_tag_default selected %i\n", default_idx);
456       }
457    }
458    if (default_idx == -1 && info->has_wayland) {
459       default_idx = device_select_find_wayland_pci_default(pci_infos, physical_device_count);
460       if (debug && default_idx != -1)
461          fprintf(stderr, "device-select: device_select_find_wayland_pci_default selected %i\n", default_idx);
462    }
463    if (default_idx == -1 && info->has_xcb) {
464       default_idx = device_select_find_xcb_pci_default(pci_infos, physical_device_count);
465       if (debug && default_idx != -1)
466          fprintf(stderr, "device-select: device_select_find_xcb_pci_default selected %i\n", default_idx);
467    }
468    if (default_idx == -1) {
469       if (info->has_vulkan11 && info->has_pci_bus)
470          default_idx = device_select_find_boot_vga_default(pci_infos, physical_device_count);
471       else
472          default_idx = device_select_find_boot_vga_vid_did(pci_infos, physical_device_count);
473       if (debug && default_idx != -1)
474          fprintf(stderr, "device-select: device_select_find_boot_vga selected %i\n", default_idx);
475    }
476    if (default_idx == -1 && cpu_count) {
477       default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
478       if (debug && default_idx != -1)
479          fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
480    }
481    /* If no GPU has been selected so far, select the first non-CPU device. If none are available,
482     * pick the first CPU device.
483     */
484    if (default_idx == -1) {
485       default_idx = device_select_find_non_cpu(pci_infos, physical_device_count);
486       if (default_idx != -1) {
487          if (debug)
488             fprintf(stderr, "device-select: device_select_find_non_cpu selected %i\n", default_idx);
489       } else if (cpu_count) {
490          default_idx = 0;
491       }
492    }
493    /* DRI_PRIME=1 handling - pick any other device than default. */
494    if (dri_prime_is_one && debug)
495       fprintf(stderr, "device-select: DRI_PRIME=1, default_idx so far: %i\n", default_idx);
496    if (default_idx != -1 && dri_prime_is_one && physical_device_count > (cpu_count + 1)) {
497       default_idx = find_non_cpu_skip(pci_infos, physical_device_count, default_idx);
498       if (debug && default_idx != -1)
499          fprintf(stderr, "device-select: find_non_cpu_skip selected %i\n", default_idx);
500    }
501    free(pci_infos);
502    return default_idx == -1 ? 0 : default_idx;
503 }
504 
device_select_EnumeratePhysicalDevices(VkInstance instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)505 static VkResult device_select_EnumeratePhysicalDevices(VkInstance instance,
506 						       uint32_t* pPhysicalDeviceCount,
507 						       VkPhysicalDevice *pPhysicalDevices)
508 {
509    struct instance_info *info = device_select_layer_get_instance(instance);
510    uint32_t physical_device_count = 0;
511    uint32_t selected_physical_device_count = 0;
512    const char* selection = getenv("MESA_VK_DEVICE_SELECT");
513    VkResult result = info->EnumeratePhysicalDevices(instance, &physical_device_count, NULL);
514    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDevice, out, pPhysicalDevices, pPhysicalDeviceCount);
515    if (result != VK_SUCCESS)
516       return result;
517 
518    VkPhysicalDevice *physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),  physical_device_count);
519    VkPhysicalDevice *selected_physical_devices = (VkPhysicalDevice*)calloc(sizeof(VkPhysicalDevice),
520                                                                            physical_device_count);
521 
522    if (!physical_devices || !selected_physical_devices) {
523       result = VK_ERROR_OUT_OF_HOST_MEMORY;
524       goto out;
525    }
526 
527    result = info->EnumeratePhysicalDevices(instance, &physical_device_count, physical_devices);
528    if (result != VK_SUCCESS)
529       goto out;
530 
531    for (unsigned i = 0; i < physical_device_count; i++) {
532       uint32_t count;
533       info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, NULL);
534       if (count > 0) {
535 	 VkExtensionProperties *extensions = calloc(count, sizeof(VkExtensionProperties));
536          if (info->EnumerateDeviceExtensionProperties(physical_devices[i], NULL, &count, extensions) == VK_SUCCESS) {
537 	    for (unsigned j = 0; j < count; j++) {
538                if (!strcmp(extensions[j].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME))
539                   info->has_pci_bus = true;
540             }
541          }
542 	 free(extensions);
543       }
544    }
545    if (should_debug_device_selection() || (selection && strcmp(selection, "list") == 0)) {
546       fprintf(stderr, "selectable devices:\n");
547       for (unsigned i = 0; i < physical_device_count; ++i)
548          print_gpu(info, i, physical_devices[i]);
549 
550       if (selection && strcmp(selection, "list") == 0)
551          exit(0);
552    }
553 
554    unsigned selected_index = get_default_device(info, selection, physical_device_count, physical_devices);
555    selected_physical_device_count = physical_device_count;
556    selected_physical_devices[0] = physical_devices[selected_index];
557    for (unsigned i = 0; i < physical_device_count - 1; ++i) {
558       unsigned  this_idx = i < selected_index ? i : i + 1;
559       selected_physical_devices[i + 1] = physical_devices[this_idx];
560    }
561 
562    if (selected_physical_device_count == 0) {
563       fprintf(stderr, "WARNING: selected no devices with MESA_VK_DEVICE_SELECT\n");
564    }
565 
566    assert(result == VK_SUCCESS);
567 
568    /* do not give multiple device option to app if force default device */
569    const char *force_default_device = getenv("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE");
570    if (force_default_device && !strcmp(force_default_device, "1") && selected_physical_device_count != 0)
571       selected_physical_device_count = 1;
572 
573    for (unsigned i = 0; i < selected_physical_device_count; i++) {
574       vk_outarray_append_typed(VkPhysicalDevice, &out, ent) {
575          *ent = selected_physical_devices[i];
576       }
577    }
578    result = vk_outarray_status(&out);
579  out:
580    free(physical_devices);
581    free(selected_physical_devices);
582    return result;
583 }
584 
device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,uint32_t * pPhysicalDeviceGroupCount,VkPhysicalDeviceGroupProperties * pPhysicalDeviceGroups)585 static VkResult device_select_EnumeratePhysicalDeviceGroups(VkInstance instance,
586                                                             uint32_t* pPhysicalDeviceGroupCount,
587                                                             VkPhysicalDeviceGroupProperties *pPhysicalDeviceGroups)
588 {
589    struct instance_info *info = device_select_layer_get_instance(instance);
590    uint32_t physical_device_group_count = 0;
591    uint32_t selected_physical_device_group_count = 0;
592    VkResult result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, NULL);
593    VK_OUTARRAY_MAKE_TYPED(VkPhysicalDeviceGroupProperties, out, pPhysicalDeviceGroups, pPhysicalDeviceGroupCount);
594 
595    if (result != VK_SUCCESS)
596       return result;
597 
598    VkPhysicalDeviceGroupProperties *physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
599    VkPhysicalDeviceGroupProperties *selected_physical_device_groups = (VkPhysicalDeviceGroupProperties*)calloc(sizeof(VkPhysicalDeviceGroupProperties), physical_device_group_count);
600 
601    if (!physical_device_groups || !selected_physical_device_groups) {
602       result = VK_ERROR_OUT_OF_HOST_MEMORY;
603       goto out;
604    }
605 
606    for (unsigned i = 0; i < physical_device_group_count; i++)
607       physical_device_groups[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
608 
609    result = info->EnumeratePhysicalDeviceGroups(instance, &physical_device_group_count, physical_device_groups);
610    if (result != VK_SUCCESS)
611       goto out;
612 
613    /* just sort groups with CPU devices to the end? - assume nobody will mix these */
614    int num_gpu_groups = 0;
615    int num_cpu_groups = 0;
616    selected_physical_device_group_count = physical_device_group_count;
617    for (unsigned i = 0; i < physical_device_group_count; i++) {
618       bool group_has_cpu_device = false;
619       for (unsigned j = 0; j < physical_device_groups[i].physicalDeviceCount; j++) {
620          VkPhysicalDevice physical_device = physical_device_groups[i].physicalDevices[j];
621          VkPhysicalDeviceProperties2 properties = (VkPhysicalDeviceProperties2){
622             .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
623          };
624          info->GetPhysicalDeviceProperties(physical_device, &properties.properties);
625          group_has_cpu_device = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
626       }
627 
628       if (group_has_cpu_device) {
629          selected_physical_device_groups[physical_device_group_count - num_cpu_groups - 1] = physical_device_groups[i];
630          num_cpu_groups++;
631       } else {
632          selected_physical_device_groups[num_gpu_groups] = physical_device_groups[i];
633          num_gpu_groups++;
634       }
635    }
636 
637    assert(result == VK_SUCCESS);
638 
639    for (unsigned i = 0; i < selected_physical_device_group_count; i++) {
640       vk_outarray_append_typed(VkPhysicalDeviceGroupProperties, &out, ent) {
641          *ent = selected_physical_device_groups[i];
642       }
643    }
644    result = vk_outarray_status(&out);
645 out:
646    free(physical_device_groups);
647    free(selected_physical_device_groups);
648    return result;
649 }
650 
get_instance_proc_addr(VkInstance instance,const char * name)651 static void  (*get_instance_proc_addr(VkInstance instance, const char* name))()
652 {
653    if (strcmp(name, "vkGetInstanceProcAddr") == 0)
654       return (void(*)())get_instance_proc_addr;
655    if (strcmp(name, "vkCreateInstance") == 0)
656       return (void(*)())device_select_CreateInstance;
657    if (strcmp(name, "vkDestroyInstance") == 0)
658       return (void(*)())device_select_DestroyInstance;
659    if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)
660       return (void(*)())device_select_EnumeratePhysicalDevices;
661    if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
662       return (void(*)())device_select_EnumeratePhysicalDeviceGroups;
663 
664    struct instance_info *info = device_select_layer_get_instance(instance);
665    return info->GetInstanceProcAddr(instance, name);
666 }
667 
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface * pVersionStruct)668 PUBLIC VkResult vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *pVersionStruct)
669 {
670    if (pVersionStruct->loaderLayerInterfaceVersion < 2)
671       return VK_ERROR_INITIALIZATION_FAILED;
672    pVersionStruct->loaderLayerInterfaceVersion = 2;
673 
674    pVersionStruct->pfnGetInstanceProcAddr = get_instance_proc_addr;
675 
676    return VK_SUCCESS;
677 }
678