|
| 1 | +#include "gpu_driver_specific.h" |
| 2 | + |
| 3 | +#include "common/io/io.h" |
| 4 | +#include "3rdparty/nvml/nvml.h" |
| 5 | +#include "util/mallocHelper.h" |
| 6 | +#include "common/io/io.h" |
| 7 | + |
| 8 | +#include <dev/pci/pcireg.h> |
| 9 | +#include <sys/pciio.h> |
| 10 | +#include <fcntl.h> |
| 11 | +#include <paths.h> |
| 12 | + |
| 13 | +static bool loadPciIds(FFstrbuf* pciids) |
| 14 | +{ |
| 15 | + // https://github.com/freebsd/freebsd-src/blob/main/usr.sbin/pciconf/pathnames.h |
| 16 | + |
| 17 | + ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", pciids); |
| 18 | + if (pciids->length > 0) return true; |
| 19 | + |
| 20 | + ffReadFileBuffer("/usr/share/pciids/pci.ids", pciids); |
| 21 | + if (pciids->length > 0) return true; |
| 22 | + |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) |
| 27 | +{ |
| 28 | + FF_AUTO_CLOSE_FD int fd = open("/dev/pci", O_RDONLY, 0); |
| 29 | + struct pci_conf confs[128]; |
| 30 | + struct pci_match_conf match = { |
| 31 | + .pc_class = PCIC_DISPLAY, |
| 32 | + .flags = PCI_GETCONF_MATCH_CLASS, |
| 33 | + }; |
| 34 | + struct pci_conf_io pcio = { |
| 35 | + .pat_buf_len = sizeof(match), |
| 36 | + .num_patterns = 1, |
| 37 | + .patterns = &match, |
| 38 | + .match_buf_len = sizeof(confs), |
| 39 | + .matches = confs, |
| 40 | + }; |
| 41 | + |
| 42 | + if (ioctl(fd, PCIOCGETCONF, &pcio) < 0) |
| 43 | + return "ioctl(fd, PCIOCGETCONF, &pc) failed"; |
| 44 | + |
| 45 | + if (pcio.status == PCI_GETCONF_ERROR) |
| 46 | + return "ioctl(fd, PCIOCGETCONF, &pc) returned error"; |
| 47 | + |
| 48 | + FF_STRBUF_AUTO_DESTROY pciids = ffStrbufCreate(); |
| 49 | + loadPciIds(&pciids); |
| 50 | + |
| 51 | + for (uint32_t i = 0; i < pcio.num_matches; ++i) |
| 52 | + { |
| 53 | + struct pci_conf* pc = &confs[i]; |
| 54 | + |
| 55 | + FFGPUResult* gpu = (FFGPUResult*)ffListAdd(gpus); |
| 56 | + ffStrbufInitStatic(&gpu->vendor, ffGetGPUVendorString(pc->pc_vendor)); |
| 57 | + ffStrbufInit(&gpu->name); |
| 58 | + ffStrbufInitS(&gpu->driver, pc->pd_name); |
| 59 | + ffStrbufInit(&gpu->platformApi); |
| 60 | + gpu->temperature = FF_GPU_TEMP_UNSET; |
| 61 | + gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; |
| 62 | + gpu->type = FF_GPU_TYPE_UNKNOWN; |
| 63 | + gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; |
| 64 | + gpu->deviceId = ((uint64_t) pc->pc_vendor << 16) + pc->pc_device; |
| 65 | + gpu->frequency = FF_GPU_FREQUENCY_UNSET; |
| 66 | + |
| 67 | + if (pciids.length) |
| 68 | + { |
| 69 | + char buffer[32]; |
| 70 | + uint32_t len = (uint32_t) snprintf(buffer, sizeof(buffer), "\n%04x ", pc->pc_vendor); |
| 71 | + char* start = (char*) memmem(pciids.chars, pciids.length, buffer, len); |
| 72 | + char* end = pciids.chars + pciids.length; |
| 73 | + if (start) |
| 74 | + { |
| 75 | + start += len; |
| 76 | + end = memchr(start, '\n', (uint32_t) (end - start)); |
| 77 | + if (!end) |
| 78 | + end = pciids.chars + pciids.length; |
| 79 | + if (!gpu->vendor.length) |
| 80 | + ffStrbufSetNS(&gpu->vendor, (uint32_t) (end - start), start); |
| 81 | + |
| 82 | + start = end; // point to '\n' of vendor |
| 83 | + end = start + 1; // point to start of devices |
| 84 | + // find the start of next vendor |
| 85 | + while (end[0] == '\t' || end[0] == '#') |
| 86 | + { |
| 87 | + end = strchr(end, '\n'); |
| 88 | + if (!end) |
| 89 | + { |
| 90 | + end = pciids.chars + pciids.length; |
| 91 | + break; |
| 92 | + } |
| 93 | + else |
| 94 | + end++; |
| 95 | + } |
| 96 | + |
| 97 | + len = (uint32_t) snprintf(buffer, sizeof(buffer), "\n\t%04x ", pc->pc_device); |
| 98 | + start = memmem(start, (size_t) (end - start), buffer, len); |
| 99 | + if (start) |
| 100 | + { |
| 101 | + start += len; |
| 102 | + end = memchr(start, '\n', (uint32_t) (end - start)); |
| 103 | + if (!end) |
| 104 | + end = pciids.chars + pciids.length; |
| 105 | + |
| 106 | + char* openingBracket = memchr(start, '[', (uint32_t) (end - start)); |
| 107 | + if (openingBracket) |
| 108 | + { |
| 109 | + openingBracket++; |
| 110 | + char* closingBracket = memchr(openingBracket, ']', (uint32_t) (end - openingBracket)); |
| 111 | + if (closingBracket) |
| 112 | + ffStrbufSetNS(&gpu->name, (uint32_t) (closingBracket - openingBracket), openingBracket); |
| 113 | + } |
| 114 | + if (!gpu->name.length) |
| 115 | + ffStrbufSetNS(&gpu->name, (uint32_t) (end - start), start); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (!gpu->name.length) |
| 121 | + { |
| 122 | + const char* subclass; |
| 123 | + switch (pc->pc_subclass) |
| 124 | + { |
| 125 | + case PCIS_DISPLAY_VGA: subclass = " (VGA compatible)"; break; |
| 126 | + case PCIS_DISPLAY_XGA: subclass = " (XGA compatible)"; break; |
| 127 | + case PCIS_DISPLAY_3D: subclass = " (3D)"; break; |
| 128 | + default: subclass = ""; break; |
| 129 | + } |
| 130 | + |
| 131 | + ffStrbufSetF(&gpu->name, "%s Device %04X%s", gpu->vendor.length ? gpu->vendor.chars : "Unknown", pc->pc_device, subclass); |
| 132 | + } |
| 133 | + |
| 134 | + #ifdef FF_USE_PROPRIETARY_GPU_DRIVER_API |
| 135 | + if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA && (options->temp || options->driverSpecific)) |
| 136 | + { |
| 137 | + ffDetectNvidiaGpuInfo(&(FFGpuDriverCondition) { |
| 138 | + .type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID, |
| 139 | + .pciBusId = { |
| 140 | + .domain = (uint32_t) pc->pc_sel.pc_domain, |
| 141 | + .bus = pc->pc_sel.pc_bus, |
| 142 | + .device = pc->pc_sel.pc_dev, |
| 143 | + .func = pc->pc_sel.pc_func, |
| 144 | + }, |
| 145 | + }, (FFGpuDriverResult) { |
| 146 | + .temp = options->temp ? &gpu->temperature : NULL, |
| 147 | + .memory = options->driverSpecific ? &gpu->dedicated : NULL, |
| 148 | + .coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL, |
| 149 | + .type = &gpu->type, |
| 150 | + .frequency = &gpu->frequency, |
| 151 | + }, "libnvidia-ml.so"); |
| 152 | + |
| 153 | + if (gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET) |
| 154 | + gpu->type = gpu->dedicated.total > (uint64_t)1024 * 1024 * 1024 ? FF_GPU_TYPE_DISCRETE : FF_GPU_TYPE_INTEGRATED; |
| 155 | + } |
| 156 | + #endif // FF_USE_PROPRIETARY_GPU_DRIVER_API |
| 157 | + } |
| 158 | + |
| 159 | + return NULL; |
| 160 | +} |
0 commit comments