Skip to content

Commit 1b4fc38

Browse files
committed
GPU (Linux): detect drm driver version
1 parent 3571d92 commit 1b4fc38

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/detection/gpu/gpu_linux.c

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,51 @@
1515

1616
#include <inttypes.h>
1717

18-
static bool pciDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer)
18+
#ifdef FF_HAVE_DRM
19+
#include "common/library.h"
20+
#include "util/mallocHelper.h"
21+
22+
#include <xf86drm.h>
23+
#include <fcntl.h>
24+
25+
static const char* drmDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer, const char* drmKey)
1926
{
27+
FF_LIBRARY_LOAD(libdrm, &instance.config.library.libdrm, "dlopen(libdrm)" FF_LIBRARY_EXTENSION " failed", "libdrm" FF_LIBRARY_EXTENSION, 2)
28+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, drmGetVersion);
29+
30+
ffStrbufSetS(buffer, "/dev/dri/");
31+
ffStrbufAppendS(buffer, drmKey);
32+
FF_AUTO_CLOSE_FD int fd = open(buffer->chars, O_RDONLY);
33+
if (fd < 0) return "open(/dev/dri/drm_key) failed";
34+
35+
FF_AUTO_FREE drmVersionPtr version = ffdrmGetVersion(fd);
36+
ffStrbufSetNS(&gpu->driver, (uint32_t) version->name_len, version->name);
37+
if (version->version_major || version->version_minor || version->version_patchlevel)
38+
ffStrbufAppendF(&gpu->driver, " %d.%d.%d", version->version_major, version->version_minor, version->version_patchlevel);
39+
else
40+
{
41+
ffStrbufAppendS(pciDir, "/driver/module/version");
42+
if (ffReadFileBuffer(pciDir->chars, buffer))
43+
{
44+
ffStrbufTrimRightSpace(buffer);
45+
ffStrbufAppendC(&gpu->driver, ' ');
46+
ffStrbufAppend(&gpu->driver, buffer);
47+
}
48+
}
49+
return NULL;
50+
}
51+
#endif
52+
53+
static bool pciDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer, const char* drmKey)
54+
{
55+
#ifdef FF_HAVE_DRM
56+
if (drmKey)
57+
{
58+
drmDetectDriver(gpu, pciDir, buffer, drmKey);
59+
if (gpu->driver.length > 0) return true;
60+
}
61+
#endif
62+
2063
ffStrbufAppendS(pciDir, "/driver");
2164
char pathBuf[PATH_MAX];
2265
ssize_t resultLength = readlink(pciDir->chars, pathBuf, sizeof(pathBuf));
@@ -148,7 +191,7 @@ static bool loadPciIds(FFstrbuf* pciids)
148191
return false;
149192
}
150193

151-
static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf* buffer, FFstrbuf* deviceDir, const char* drmPath)
194+
static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf* buffer, FFstrbuf* deviceDir, const char* drmKey)
152195
{
153196
const uint32_t drmDirPathLength = deviceDir->length;
154197
uint32_t vendorId, deviceId, subVendorId, subDeviceId;
@@ -161,7 +204,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
161204

162205
char pciPath[PATH_MAX];
163206
const char* pPciPath = NULL;
164-
if (drmPath)
207+
if (drmKey)
165208
{
166209
ssize_t pathLength = readlink(deviceDir->chars, pciPath, sizeof(pciPath) - 1);
167210
if (pathLength <= 0)
@@ -194,6 +237,8 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
194237
gpu->deviceId = ((uint64_t) pciDomain << 6) | ((uint64_t) pciBus << 4) | (deviceId << 2) | pciFunc;
195238
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
196239

240+
if (drmKey) ffStrbufSetF(&gpu->platformApi, "DRM (%s)", drmKey);
241+
197242
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
198243
{
199244
ffStrbufAppendS(deviceDir, "/revision");
@@ -226,7 +271,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
226271
ffGPUParsePciIds(&pciids, subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
227272
}
228273

229-
pciDetectDriver(gpu, deviceDir, buffer);
274+
pciDetectDriver(gpu, deviceDir, buffer, drmKey);
230275
ffStrbufSubstrBefore(deviceDir, drmDirPathLength);
231276

232277
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
@@ -274,7 +319,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
274319
return NULL;
275320
}
276321

277-
FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir)
322+
FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, const char* drmKey)
278323
{
279324
uint32_t index = ffStrbufFirstIndexS(buffer, "apple,agx-t");
280325
if (index == buffer->length) return "display-subsystem?";
@@ -292,7 +337,7 @@ FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, F
292337
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
293338
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
294339

295-
pciDetectDriver(gpu, drmDir, buffer);
340+
pciDetectDriver(gpu, drmDir, buffer, drmKey);
296341

297342
return NULL;
298343
}
@@ -327,7 +372,7 @@ static const char* drmDetectGPUs(const FFGPUOptions* options, FFlist* gpus)
327372
detectPci(options, gpus, &buffer, &drmDir, entry->d_name);
328373
#ifdef __aarch64__
329374
else if (ffStrbufStartsWithS(&buffer, "of:"))
330-
detectAsahi(gpus, &buffer, &drmDir);
375+
detectAsahi(gpus, &buffer, &drmDir, entry->d_name);
331376
#endif
332377

333378
ffStrbufSubstrBefore(&drmDir, drmDirLength);

0 commit comments

Comments
 (0)