Skip to content

Commit 10ca776

Browse files
committed
GPU (Linux): detect GPU core count and freq for asahi linux
1 parent c2eaea3 commit 10ca776

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

src/detection/gpu/gpu_linux.c

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,44 @@
1515

1616
#include <inttypes.h>
1717

18-
#ifdef FF_HAVE_DRM
19-
#include "common/library.h"
20-
#include "util/mallocHelper.h"
18+
#if __has_include(<drm/drm.h>)
19+
#include <drm/drm.h>
20+
#define FF_HAVE_DRM_H 1
21+
#elif __has_include(<libdrm/drm.h>)
22+
#include <libdrm/drm.h>
23+
#define FF_HAVE_DRM_H 1
24+
#endif
25+
26+
#if FF_HAVE_DRM_H
27+
#include <fcntl.h>
28+
#include <sys/ioctl.h>
29+
30+
#if __aarch64__ && __has_include(<drm/asahi_drm.h>)
31+
#include <drm/asahi_drm.h>
32+
#define FF_HAVE_ASAHI_DRM_H 1
33+
#endif
34+
#endif
2135

22-
#include <xf86drm.h>
23-
#include <fcntl.h>
2436

37+
#if FF_HAVE_DRM_H
2538
static const char* drmDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer, const char* drmKey)
2639
{
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-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, drmFreeVersion);
30-
3140
ffStrbufSetS(buffer, "/dev/dri/");
3241
ffStrbufAppendS(buffer, drmKey);
3342
FF_AUTO_CLOSE_FD int fd = open(buffer->chars, O_RDONLY);
3443
if (fd < 0) return "open(/dev/dri/drm_key) failed";
3544

36-
drmVersionPtr version = ffdrmGetVersion(fd);
37-
ffStrbufSetNS(&gpu->driver, (uint32_t) version->name_len, version->name);
38-
if (version->version_major || version->version_minor || version->version_patchlevel)
39-
ffStrbufAppendF(&gpu->driver, " %d.%d.%d", version->version_major, version->version_minor, version->version_patchlevel);
45+
ffStrbufEnsureFixedLengthFree(&gpu->driver, 128);
46+
drm_version_t version = {
47+
.name = gpu->driver.chars,
48+
.name_len = gpu->driver.allocated,
49+
};
50+
if (ioctl(fd, DRM_IOCTL_VERSION, &version) < 0) return "ioctl(DRM_IOCTL_VERSION) failed";
51+
gpu->driver.length = (uint32_t) version.name_len;
52+
gpu->driver.chars[gpu->driver.length] = '\0';
53+
54+
if (version.version_major || version.version_minor || version.version_patchlevel)
55+
ffStrbufAppendF(&gpu->driver, " %d.%d.%d", version.version_major, version.version_minor, version.version_patchlevel);
4056
else
4157
{
4258
ffStrbufAppendS(pciDir, "/driver/module/version");
@@ -47,14 +63,13 @@ static const char* drmDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf*
4763
ffStrbufAppend(&gpu->driver, buffer);
4864
}
4965
}
50-
ffdrmFreeVersion(version);
5166
return NULL;
5267
}
5368
#endif
5469

5570
static bool pciDetectDriver(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer, const char* drmKey)
5671
{
57-
#ifdef FF_HAVE_DRM
72+
#if __has_include(<drm/drm.h>)
5873
if (drmKey)
5974
{
6075
drmDetectDriver(gpu, pciDir, buffer, drmKey);
@@ -332,7 +347,7 @@ FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, F
332347
ffStrbufInitStatic(&gpu->name, ffCPUAppleCodeToName((uint32_t) gpu->deviceId));
333348
ffStrbufInitStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_APPLE);
334349
ffStrbufInit(&gpu->driver);
335-
ffStrbufInit(&gpu->platformApi);
350+
ffStrbufInitF(&gpu->platformApi, "DRM (%s)", drmKey);
336351
gpu->temperature = FF_GPU_TEMP_UNSET;
337352
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
338353
gpu->type = FF_GPU_TYPE_INTEGRATED;
@@ -341,6 +356,26 @@ FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, F
341356

342357
pciDetectDriver(gpu, drmDir, buffer, drmKey);
343358

359+
#if FF_HAVE_ASAHI_DRM_H
360+
ffStrbufSetS(buffer, "/dev/dri/");
361+
ffStrbufAppendS(buffer, drmKey);
362+
FF_AUTO_CLOSE_FD int fd = open(buffer->chars, O_RDONLY);
363+
if (fd >= 0)
364+
{
365+
struct drm_asahi_params_global paramsGlobal = {};
366+
if (ioctl(fd, DRM_IOCTL_ASAHI_GET_PARAMS, &(struct drm_asahi_get_params){
367+
.param_group = DRM_ASAHI_GET_PARAMS,
368+
.pointer = (uint64_t) &paramsGlobal,
369+
.size = sizeof(paramsGlobal),
370+
}) >= 0)
371+
{
372+
gpu->coreCount = (int) paramsGlobal.num_cores_total_active;
373+
gpu->frequency = paramsGlobal.max_frequency_khz / 1e6;
374+
gpu->deviceId = paramsGlobal.chip_id;
375+
}
376+
}
377+
#endif
378+
344379
return NULL;
345380
}
346381

src/modules/gpu/gpu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void ffPrintGPU(FFGPUOptions* options)
127127
ffStrbufDestroy(&gpu->vendor);
128128
ffStrbufDestroy(&gpu->name);
129129
ffStrbufDestroy(&gpu->driver);
130+
ffStrbufDestroy(&gpu->platformApi);
130131
}
131132
}
132133

@@ -327,6 +328,7 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_
327328
ffStrbufDestroy(&gpu->vendor);
328329
ffStrbufDestroy(&gpu->name);
329330
ffStrbufDestroy(&gpu->driver);
331+
ffStrbufDestroy(&gpu->platformApi);
330332
}
331333
}
332334

0 commit comments

Comments
 (0)