• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2021-2022 The Khronos Group Inc.
4  * Copyright (c) 2021-2022 Valve Corporation
5  * Copyright (c) 2021-2022 LunarG, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Author: Mark Young <marky@lunarg.com>
20  *
21  */
22 
23 // Non-windows and non-apple only header file, guard it so that accidental
24 // inclusion doesn't cause unknown header include errors
25 #if defined(LOADER_ENABLE_LINUX_SORT)
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "loader_linux.h"
31 
32 #include "allocation.h"
33 #include "loader_environment.h"
34 #include "loader.h"
35 #include "log.h"
36 #include "stack_allocation.h"
37 
38 // Determine a priority based on device type with the higher value being higher priority.
determine_priority_type_value(VkPhysicalDeviceType type)39 uint32_t determine_priority_type_value(VkPhysicalDeviceType type) {
40     switch (type) {
41         case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
42             return 10;
43         case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
44             return 5;
45         case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
46             return 3;
47         case VK_PHYSICAL_DEVICE_TYPE_OTHER:
48             return 2;
49         case VK_PHYSICAL_DEVICE_TYPE_CPU:
50             return 1;
51         case VK_PHYSICAL_DEVICE_TYPE_MAX_ENUM:  // Not really an enum, but throws warning if it's not here
52             break;
53     }
54     return 0;
55 }
56 
57 // Compare the two device types.
58 // This behaves similar to a qsort compare.
device_type_compare(VkPhysicalDeviceType a,VkPhysicalDeviceType b)59 int32_t device_type_compare(VkPhysicalDeviceType a, VkPhysicalDeviceType b) {
60     uint32_t a_value = determine_priority_type_value(a);
61     uint32_t b_value = determine_priority_type_value(b);
62     if (a_value > b_value) {
63         return -1;
64     } else if (b_value > a_value) {
65         return 1;
66     }
67     return 0;
68 }
69 
70 // Used to compare two devices and determine which one should have priority.  The criteria is
71 // simple:
72 //   1) Default device ALWAYS wins
73 //   2) Sort by type
74 //   3) Sort by PCI bus ID
75 //   4) Ties broken by device_ID XOR vendor_ID comparison
compare_devices(const void * a,const void * b)76 int32_t compare_devices(const void *a, const void *b) {
77     struct LinuxSortedDeviceInfo *left = (struct LinuxSortedDeviceInfo *)a;
78     struct LinuxSortedDeviceInfo *right = (struct LinuxSortedDeviceInfo *)b;
79 
80     // Default device always gets priority
81     if (left->default_device) {
82         return -1;
83     } else if (right->default_device) {
84         return 1;
85     }
86 
87     // Order by device type next
88     int32_t dev_type_comp = device_type_compare(left->device_type, right->device_type);
89     if (0 != dev_type_comp) {
90         return dev_type_comp;
91     }
92 
93     // Sort by PCI info (prioritize devices that have info over those that don't)
94     if (left->has_pci_bus_info && !right->has_pci_bus_info) {
95         return -1;
96     } else if (!left->has_pci_bus_info && right->has_pci_bus_info) {
97         return 1;
98     } else if (left->has_pci_bus_info && right->has_pci_bus_info) {
99         // Sort low to high PCI domain
100         if (left->pci_domain < right->pci_domain) {
101             return -1;
102         } else if (left->pci_domain > right->pci_domain) {
103             return 1;
104         }
105         // Sort low to high PCI bus
106         if (left->pci_bus < right->pci_bus) {
107             return -1;
108         } else if (left->pci_bus > right->pci_bus) {
109             return 1;
110         }
111         // Sort low to high PCI device
112         if (left->pci_device < right->pci_device) {
113             return -1;
114         } else if (left->pci_device > right->pci_device) {
115             return 1;
116         }
117         // Sort low to high PCI function
118         if (left->pci_function < right->pci_function) {
119             return -1;
120         } else if (left->pci_function > right->pci_function) {
121             return 1;
122         }
123     }
124 
125     // Somehow we have a tie above, so XOR vendorID and deviceID and compare
126     uint32_t left_xord_dev_vend = left->device_id ^ left->vendor_id;
127     uint32_t right_xord_dev_vend = right->device_id ^ right->vendor_id;
128     if (left_xord_dev_vend < right_xord_dev_vend) {
129         return -1;
130     } else if (right_xord_dev_vend < left_xord_dev_vend) {
131         return 1;
132     }
133     return 0;
134 }
135 
136 // Used to compare two device groups and determine which one should have priority.
137 // NOTE: This assumes that devices in each group have already been sorted.
138 // The group sort criteria is simple:
139 //   1) Group with the default device ALWAYS wins
140 //   2) Group with the best device type for device 0 wins
141 //   3) Group with best PCI bus ID for device 0 wins
142 //   4) Ties broken by group device 0 device_ID XOR vendor_ID comparison
compare_device_groups(const void * a,const void * b)143 int32_t compare_device_groups(const void *a, const void *b) {
144     struct loader_physical_device_group_term *grp_a = (struct loader_physical_device_group_term *)a;
145     struct loader_physical_device_group_term *grp_b = (struct loader_physical_device_group_term *)b;
146 
147     // Use the first GPU's info from each group to sort the groups by
148     struct LinuxSortedDeviceInfo *left = &grp_a->internal_device_info[0];
149     struct LinuxSortedDeviceInfo *right = &grp_b->internal_device_info[0];
150 
151     // Default device always gets priority
152     if (left->default_device) {
153         return -1;
154     } else if (right->default_device) {
155         return 1;
156     }
157 
158     // Order by device type next
159     int32_t dev_type_comp = device_type_compare(left->device_type, right->device_type);
160     if (0 != dev_type_comp) {
161         return dev_type_comp;
162     }
163 
164     // Sort by PCI info (prioritize devices that have info over those that don't)
165     if (left->has_pci_bus_info && !right->has_pci_bus_info) {
166         return -1;
167     } else if (!left->has_pci_bus_info && right->has_pci_bus_info) {
168         return 1;
169     } else if (left->has_pci_bus_info && right->has_pci_bus_info) {
170         // Sort low to high PCI domain
171         if (left->pci_domain < right->pci_domain) {
172             return -1;
173         } else if (left->pci_domain > right->pci_domain) {
174             return 1;
175         }
176         // Sort low to high PCI bus
177         if (left->pci_bus < right->pci_bus) {
178             return -1;
179         } else if (left->pci_bus > right->pci_bus) {
180             return 1;
181         }
182         // Sort low to high PCI device
183         if (left->pci_device < right->pci_device) {
184             return -1;
185         } else if (left->pci_device > right->pci_device) {
186             return 1;
187         }
188         // Sort low to high PCI function
189         if (left->pci_function < right->pci_function) {
190             return -1;
191         } else if (left->pci_function > right->pci_function) {
192             return 1;
193         }
194     }
195 
196     // Somehow we have a tie above, so XOR vendorID and deviceID and compare
197     uint32_t left_xord_dev_vend = left->device_id ^ left->vendor_id;
198     uint32_t right_xord_dev_vend = right->device_id ^ right->vendor_id;
199     if (left_xord_dev_vend < right_xord_dev_vend) {
200         return -1;
201     } else if (right_xord_dev_vend < left_xord_dev_vend) {
202         return 1;
203     }
204     return 0;
205 }
206 
207 // Search for the default device using the loader environment variable.
linux_env_var_default_device(struct loader_instance * inst,uint32_t device_count,struct LinuxSortedDeviceInfo * sorted_device_info)208 void linux_env_var_default_device(struct loader_instance *inst, uint32_t device_count,
209                                   struct LinuxSortedDeviceInfo *sorted_device_info) {
210     char *selection = loader_getenv("VK_LOADER_DEVICE_SELECT", inst);
211     if (NULL != selection) {
212         loader_log(inst, VULKAN_LOADER_DEBUG_BIT | VULKAN_LOADER_DRIVER_BIT, 0,
213                    "linux_env_var_default_device:  Found \'VK_LOADER_DEVICE_SELECT\' set to %s", selection);
214 
215         // The environment variable exists, so grab the vendor ID and device ID of the
216         // selected default device
217         unsigned vendor_id, device_id;
218         int32_t matched = sscanf(selection, "%x:%x", &vendor_id, &device_id);
219         if (matched == 2) {
220             for (int32_t i = 0; i < (int32_t)device_count; ++i) {
221                 if (sorted_device_info[i].vendor_id == vendor_id && sorted_device_info[i].device_id == device_id) {
222                     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0,
223                                "linux_env_var_default_device:  Found default at index %u \'%s\'", i,
224                                sorted_device_info[i].device_name);
225                     sorted_device_info[i].default_device = true;
226                     break;
227                 }
228             }
229         }
230 
231         loader_free_getenv(selection, inst);
232     }
233 }
234 
235 // This function allocates an array in sorted_devices which must be freed by the caller if not null
linux_read_sorted_physical_devices(struct loader_instance * inst,uint32_t icd_count,struct loader_icd_physical_devices * icd_devices,uint32_t phys_dev_count,struct loader_physical_device_term ** sorted_device_term)236 VkResult linux_read_sorted_physical_devices(struct loader_instance *inst, uint32_t icd_count,
237                                             struct loader_icd_physical_devices *icd_devices, uint32_t phys_dev_count,
238                                             struct loader_physical_device_term **sorted_device_term) {
239     VkResult res = VK_SUCCESS;
240     bool app_is_vulkan_1_1 = loader_check_version_meets_required(LOADER_VERSION_1_1_0, inst->app_api_version);
241 
242     struct LinuxSortedDeviceInfo *sorted_device_info = loader_instance_heap_calloc(
243         inst, phys_dev_count * sizeof(struct LinuxSortedDeviceInfo), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
244     if (NULL == sorted_device_info) {
245         res = VK_ERROR_OUT_OF_HOST_MEMORY;
246         goto out;
247     }
248 
249     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_read_sorted_physical_devices:");
250     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "     Original order:");
251 
252     // Grab all the necessary info we can about each device
253     uint32_t index = 0;
254     for (uint32_t icd_idx = 0; icd_idx < icd_count; ++icd_idx) {
255         for (uint32_t phys_dev = 0; phys_dev < icd_devices[icd_idx].device_count; ++phys_dev) {
256             struct loader_icd_term *icd_term = icd_devices[icd_idx].icd_term;
257             VkPhysicalDeviceProperties dev_props = {};
258 
259             sorted_device_info[index].physical_device = icd_devices[icd_idx].physical_devices[phys_dev];
260             sorted_device_info[index].icd_term = icd_term;
261             sorted_device_info[index].has_pci_bus_info = false;
262 
263             icd_term->dispatch.GetPhysicalDeviceProperties(sorted_device_info[index].physical_device, &dev_props);
264             sorted_device_info[index].device_type = dev_props.deviceType;
265             strncpy(sorted_device_info[index].device_name, dev_props.deviceName, VK_MAX_PHYSICAL_DEVICE_NAME_SIZE);
266             sorted_device_info[index].vendor_id = dev_props.vendorID;
267             sorted_device_info[index].device_id = dev_props.deviceID;
268 
269             bool device_is_1_1_capable =
270                 loader_check_version_meets_required(LOADER_VERSION_1_1_0, loader_make_version(dev_props.apiVersion));
271             if (!sorted_device_info[index].has_pci_bus_info) {
272                 uint32_t ext_count = 0;
273                 icd_term->dispatch.EnumerateDeviceExtensionProperties(sorted_device_info[index].physical_device, NULL, &ext_count,
274                                                                       NULL);
275                 if (ext_count > 0) {
276                     VkExtensionProperties *ext_props =
277                         (VkExtensionProperties *)loader_stack_alloc(sizeof(VkExtensionProperties) * ext_count);
278                     if (NULL == ext_props) {
279                         res = VK_ERROR_OUT_OF_HOST_MEMORY;
280                         goto out;
281                     }
282                     icd_term->dispatch.EnumerateDeviceExtensionProperties(sorted_device_info[index].physical_device, NULL,
283                                                                           &ext_count, ext_props);
284                     for (uint32_t ext = 0; ext < ext_count; ++ext) {
285                         if (!strcmp(ext_props[ext].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) {
286                             sorted_device_info[index].has_pci_bus_info = true;
287                             break;
288                         }
289                     }
290                 }
291             }
292 
293             if (sorted_device_info[index].has_pci_bus_info) {
294                 VkPhysicalDevicePCIBusInfoPropertiesEXT pci_props = (VkPhysicalDevicePCIBusInfoPropertiesEXT){
295                     .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT};
296                 VkPhysicalDeviceProperties2 dev_props2 =
297                     (VkPhysicalDeviceProperties2){.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &pci_props};
298 
299                 PFN_vkGetPhysicalDeviceProperties2 GetPhysDevProps2 = NULL;
300                 if (app_is_vulkan_1_1 && device_is_1_1_capable) {
301                     GetPhysDevProps2 = icd_term->dispatch.GetPhysicalDeviceProperties2;
302                 } else {
303                     GetPhysDevProps2 = (PFN_vkGetPhysicalDeviceProperties2)icd_term->dispatch.GetPhysicalDeviceProperties2KHR;
304                 }
305                 if (NULL != GetPhysDevProps2) {
306                     GetPhysDevProps2(sorted_device_info[index].physical_device, &dev_props2);
307                     sorted_device_info[index].pci_domain = pci_props.pciDomain;
308                     sorted_device_info[index].pci_bus = pci_props.pciBus;
309                     sorted_device_info[index].pci_device = pci_props.pciDevice;
310                     sorted_device_info[index].pci_function = pci_props.pciFunction;
311                 } else {
312                     sorted_device_info[index].has_pci_bus_info = false;
313                 }
314             }
315             loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           [%u] %s", index,
316                        sorted_device_info[index].device_name);
317             index++;
318         }
319     }
320 
321     // Select default device if set in the environment variable
322     linux_env_var_default_device(inst, phys_dev_count, sorted_device_info);
323 
324     // Sort devices by PCI info
325     qsort(sorted_device_info, phys_dev_count, sizeof(struct LinuxSortedDeviceInfo), compare_devices);
326 
327     // If we have a selected index, add that first.
328     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "     Sorted order:");
329 
330     // Add all others after (they've already been sorted)
331     for (uint32_t dev = 0; dev < phys_dev_count; ++dev) {
332         sorted_device_term[dev]->this_icd_term = sorted_device_info[dev].icd_term;
333         sorted_device_term[dev]->phys_dev = sorted_device_info[dev].physical_device;
334         loader_set_dispatch((void *)sorted_device_term[dev], inst->disp);
335         loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           [%u] %s  %s", dev,
336                    sorted_device_info[dev].device_name, (sorted_device_info[dev].default_device ? "[default]" : ""));
337     }
338 
339 out:
340     loader_instance_heap_free(inst, sorted_device_info);
341 
342     return res;
343 }
344 
345 // This function sorts an array of physical device groups
linux_sort_physical_device_groups(struct loader_instance * inst,uint32_t group_count,struct loader_physical_device_group_term * sorted_group_term)346 VkResult linux_sort_physical_device_groups(struct loader_instance *inst, uint32_t group_count,
347                                            struct loader_physical_device_group_term *sorted_group_term) {
348     VkResult res = VK_SUCCESS;
349     bool app_is_vulkan_1_1 = loader_check_version_meets_required(LOADER_VERSION_1_1_0, inst->app_api_version);
350 
351     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_sort_physical_device_groups:  Original order:");
352 
353     for (uint32_t group = 0; group < group_count; ++group) {
354         loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           Group %u", group);
355 
356         struct loader_icd_term *icd_term = sorted_group_term[group].this_icd_term;
357         for (uint32_t gpu = 0; gpu < sorted_group_term[group].group_props.physicalDeviceCount; ++gpu) {
358             VkPhysicalDeviceProperties dev_props = {};
359 
360             sorted_group_term[group].internal_device_info[gpu].physical_device =
361                 sorted_group_term[group].group_props.physicalDevices[gpu];
362             sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = false;
363 
364             icd_term->dispatch.GetPhysicalDeviceProperties(sorted_group_term[group].internal_device_info[gpu].physical_device,
365                                                            &dev_props);
366             sorted_group_term[group].internal_device_info[gpu].device_type = dev_props.deviceType;
367             strncpy(sorted_group_term[group].internal_device_info[gpu].device_name, dev_props.deviceName,
368                     VK_MAX_PHYSICAL_DEVICE_NAME_SIZE);
369             sorted_group_term[group].internal_device_info[gpu].vendor_id = dev_props.vendorID;
370             sorted_group_term[group].internal_device_info[gpu].device_id = dev_props.deviceID;
371 
372             bool device_is_1_1_capable =
373                 loader_check_version_meets_required(LOADER_VERSION_1_1_0, loader_make_version(dev_props.apiVersion));
374             if (!sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info) {
375                 uint32_t ext_count;
376                 icd_term->dispatch.EnumerateDeviceExtensionProperties(
377                     sorted_group_term[group].internal_device_info[gpu].physical_device, NULL, &ext_count, NULL);
378                 if (ext_count > 0) {
379                     VkExtensionProperties *ext_props =
380                         (VkExtensionProperties *)loader_stack_alloc(sizeof(VkExtensionProperties) * ext_count);
381                     if (NULL == ext_props) {
382                         return VK_ERROR_OUT_OF_HOST_MEMORY;
383                     }
384                     icd_term->dispatch.EnumerateDeviceExtensionProperties(
385                         sorted_group_term[group].internal_device_info[gpu].physical_device, NULL, &ext_count, ext_props);
386                     for (uint32_t ext = 0; ext < ext_count; ++ext) {
387                         if (!strcmp(ext_props[ext].extensionName, VK_EXT_PCI_BUS_INFO_EXTENSION_NAME)) {
388                             sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = true;
389                             break;
390                         }
391                     }
392                 }
393             }
394 
395             if (sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info) {
396                 VkPhysicalDevicePCIBusInfoPropertiesEXT pci_props = (VkPhysicalDevicePCIBusInfoPropertiesEXT){
397                     .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT};
398                 VkPhysicalDeviceProperties2 dev_props2 =
399                     (VkPhysicalDeviceProperties2){.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &pci_props};
400 
401                 PFN_vkGetPhysicalDeviceProperties2 GetPhysDevProps2 = NULL;
402                 if (app_is_vulkan_1_1 && device_is_1_1_capable) {
403                     GetPhysDevProps2 = icd_term->dispatch.GetPhysicalDeviceProperties2;
404                 } else {
405                     GetPhysDevProps2 = (PFN_vkGetPhysicalDeviceProperties2)icd_term->dispatch.GetPhysicalDeviceProperties2KHR;
406                 }
407                 if (NULL != GetPhysDevProps2) {
408                     GetPhysDevProps2(sorted_group_term[group].internal_device_info[gpu].physical_device, &dev_props2);
409                     sorted_group_term[group].internal_device_info[gpu].pci_domain = pci_props.pciDomain;
410                     sorted_group_term[group].internal_device_info[gpu].pci_bus = pci_props.pciBus;
411                     sorted_group_term[group].internal_device_info[gpu].pci_device = pci_props.pciDevice;
412                     sorted_group_term[group].internal_device_info[gpu].pci_function = pci_props.pciFunction;
413                 } else {
414                     sorted_group_term[group].internal_device_info[gpu].has_pci_bus_info = false;
415                 }
416             }
417             loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "               [%u] %s", gpu,
418                        sorted_group_term[group].internal_device_info[gpu].device_name);
419         }
420 
421         // Select default device if set in the environment variable
422         linux_env_var_default_device(inst, sorted_group_term[group].group_props.physicalDeviceCount,
423                                      sorted_group_term[group].internal_device_info);
424 
425         // Sort GPUs in each group
426         qsort(sorted_group_term[group].internal_device_info, sorted_group_term[group].group_props.physicalDeviceCount,
427               sizeof(struct LinuxSortedDeviceInfo), compare_devices);
428 
429         // Match the externally used physical device list with the sorted physical device list for this group.
430         for (uint32_t dev = 0; dev < sorted_group_term[group].group_props.physicalDeviceCount; ++dev) {
431             sorted_group_term[group].group_props.physicalDevices[dev] =
432                 sorted_group_term[group].internal_device_info[dev].physical_device;
433         }
434     }
435 
436     // Sort device groups by PCI info
437     qsort(sorted_group_term, group_count, sizeof(struct loader_physical_device_group_term), compare_device_groups);
438 
439     loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "linux_sort_physical_device_groups:  Sorted order:");
440     for (uint32_t group = 0; group < group_count; ++group) {
441         loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "           Group %u", group);
442         for (uint32_t gpu = 0; gpu < sorted_group_term[group].group_props.physicalDeviceCount; ++gpu) {
443             loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0, "               [%u] %s %p %s", gpu,
444                        sorted_group_term[group].internal_device_info[gpu].device_name,
445                        sorted_group_term[group].internal_device_info[gpu].physical_device,
446                        (sorted_group_term[group].internal_device_info[gpu].default_device ? "[default]" : ""));
447         }
448     }
449 
450     return res;
451 }
452 
453 #endif  // LOADER_ENABLE_LINUX_SORT
454