2024-01-25 10:18:19 +08:00
|
|
|
#include "gpu_driver_specific.h"
|
|
|
|
|
|
|
|
|
|
#include "common/io/io.h"
|
2024-02-12 18:09:11 +08:00
|
|
|
#include "common/properties.h"
|
2024-01-25 10:18:19 +08:00
|
|
|
#include "util/mallocHelper.h"
|
|
|
|
|
|
|
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
|
#include <sys/pciio.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <paths.h>
|
|
|
|
|
|
|
|
|
|
static bool loadPciIds(FFstrbuf* pciids)
|
|
|
|
|
{
|
|
|
|
|
// https://github.com/freebsd/freebsd-src/blob/main/usr.sbin/pciconf/pathnames.h
|
|
|
|
|
|
|
|
|
|
ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", pciids);
|
|
|
|
|
if (pciids->length > 0) return true;
|
|
|
|
|
|
2024-02-26 13:30:09 +08:00
|
|
|
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids", pciids);
|
2024-01-25 10:18:19 +08:00
|
|
|
if (pciids->length > 0) return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
|
|
|
|
|
{
|
|
|
|
|
FF_AUTO_CLOSE_FD int fd = open("/dev/pci", O_RDONLY, 0);
|
|
|
|
|
struct pci_conf confs[128];
|
|
|
|
|
struct pci_match_conf match = {
|
|
|
|
|
.pc_class = PCIC_DISPLAY,
|
|
|
|
|
.flags = PCI_GETCONF_MATCH_CLASS,
|
|
|
|
|
};
|
|
|
|
|
struct pci_conf_io pcio = {
|
|
|
|
|
.pat_buf_len = sizeof(match),
|
|
|
|
|
.num_patterns = 1,
|
|
|
|
|
.patterns = &match,
|
|
|
|
|
.match_buf_len = sizeof(confs),
|
|
|
|
|
.matches = confs,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, PCIOCGETCONF, &pcio) < 0)
|
|
|
|
|
return "ioctl(fd, PCIOCGETCONF, &pc) failed";
|
|
|
|
|
|
|
|
|
|
if (pcio.status == PCI_GETCONF_ERROR)
|
|
|
|
|
return "ioctl(fd, PCIOCGETCONF, &pc) returned error";
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY pciids = ffStrbufCreate();
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < pcio.num_matches; ++i)
|
|
|
|
|
{
|
2024-01-25 20:22:28 +08:00
|
|
|
struct pci_conf* pc = &confs[i];
|
2024-01-25 10:18:19 +08:00
|
|
|
|
|
|
|
|
FFGPUResult* gpu = (FFGPUResult*)ffListAdd(gpus);
|
|
|
|
|
ffStrbufInitStatic(&gpu->vendor, ffGetGPUVendorString(pc->pc_vendor));
|
|
|
|
|
ffStrbufInit(&gpu->name);
|
|
|
|
|
ffStrbufInitS(&gpu->driver, pc->pd_name);
|
|
|
|
|
ffStrbufInit(&gpu->platformApi);
|
|
|
|
|
gpu->temperature = FF_GPU_TEMP_UNSET;
|
|
|
|
|
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
|
2024-08-01 13:54:03 +08:00
|
|
|
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
|
2024-01-25 10:18:19 +08:00
|
|
|
gpu->type = FF_GPU_TYPE_UNKNOWN;
|
|
|
|
|
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
|
2024-01-31 10:39:09 +08:00
|
|
|
gpu->deviceId = ((uint64_t) pc->pc_sel.pc_domain << 6) | ((uint64_t) pc->pc_sel.pc_bus << 4) | ((uint64_t) pc->pc_sel.pc_dev << 2) | pc->pc_sel.pc_func;
|
2024-01-25 10:18:19 +08:00
|
|
|
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
|
|
|
|
|
|
2024-02-12 18:09:11 +08:00
|
|
|
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
|
|
|
|
|
{
|
|
|
|
|
char query[32];
|
|
|
|
|
snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) pc->pc_device, (unsigned) pc->pc_revid);
|
|
|
|
|
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-10 21:03:56 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY coreName = ffStrbufCreate();
|
2024-02-12 18:09:11 +08:00
|
|
|
if (gpu->name.length == 0)
|
|
|
|
|
{
|
|
|
|
|
if (pciids.length == 0)
|
|
|
|
|
loadPciIds(&pciids);
|
2024-08-10 21:03:56 +08:00
|
|
|
ffGPUParsePciIds(&pciids, pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu, &coreName);
|
2024-02-12 18:09:11 +08:00
|
|
|
}
|
2024-01-25 10:18:19 +08:00
|
|
|
|
|
|
|
|
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA && (options->temp || options->driverSpecific))
|
|
|
|
|
{
|
|
|
|
|
ffDetectNvidiaGpuInfo(&(FFGpuDriverCondition) {
|
|
|
|
|
.type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID,
|
|
|
|
|
.pciBusId = {
|
|
|
|
|
.domain = (uint32_t) pc->pc_sel.pc_domain,
|
|
|
|
|
.bus = pc->pc_sel.pc_bus,
|
|
|
|
|
.device = pc->pc_sel.pc_dev,
|
|
|
|
|
.func = pc->pc_sel.pc_func,
|
|
|
|
|
},
|
|
|
|
|
}, (FFGpuDriverResult) {
|
|
|
|
|
.temp = options->temp ? &gpu->temperature : NULL,
|
|
|
|
|
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
|
|
|
|
|
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
|
|
|
|
|
.type = &gpu->type,
|
|
|
|
|
.frequency = &gpu->frequency,
|
2024-08-01 13:54:03 +08:00
|
|
|
.coreUsage = &gpu->coreUsage,
|
2024-08-02 15:21:42 +08:00
|
|
|
.name = options->driverSpecific ? &gpu->name : NULL,
|
2024-01-25 10:18:19 +08:00
|
|
|
}, "libnvidia-ml.so");
|
2024-08-02 16:47:57 +08:00
|
|
|
}
|
2024-01-25 10:18:19 +08:00
|
|
|
|
2024-08-02 16:47:57 +08:00
|
|
|
if (gpu->type == FF_GPU_TYPE_UNKNOWN)
|
|
|
|
|
{
|
|
|
|
|
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA)
|
|
|
|
|
{
|
|
|
|
|
if (ffStrbufStartsWithIgnCaseS(&gpu->name, "GeForce") ||
|
|
|
|
|
ffStrbufStartsWithIgnCaseS(&gpu->name, "Quadro") ||
|
|
|
|
|
ffStrbufStartsWithIgnCaseS(&gpu->name, "Tesla"))
|
|
|
|
|
gpu->type = FF_GPU_TYPE_DISCRETE;
|
|
|
|
|
}
|
|
|
|
|
else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_MTHREADS)
|
|
|
|
|
{
|
|
|
|
|
if (ffStrbufStartsWithIgnCaseS(&gpu->name, "MTT "))
|
|
|
|
|
gpu->type = FF_GPU_TYPE_DISCRETE;
|
|
|
|
|
}
|
2024-08-05 10:07:17 +08:00
|
|
|
else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_INTEL)
|
|
|
|
|
{
|
2024-08-10 21:03:56 +08:00
|
|
|
if ((coreName->chars[0] == 'D' || coreName->chars[0] == 'S') &&
|
|
|
|
|
coreName->chars[1] == 'G' &&
|
|
|
|
|
ffCharIsDigit(coreName->chars[2]))
|
|
|
|
|
gpu->type = FF_GPU_TYPE_DISCRETE; // DG1 / DG2 / SG1
|
|
|
|
|
else
|
|
|
|
|
gpu->type = FF_GPU_TYPE_INTEGRATED;
|
2024-08-05 10:07:17 +08:00
|
|
|
}
|
2024-01-25 10:18:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|