Skip to content

Commit 6917f94

Browse files
committed
GPU (Linux / FreeBSD): add DRM detection support for old radeon driver
Ref: #1810
1 parent 8559cc4 commit 6917f94

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

src/detection/gpu/gpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device,
5454
void ffGPUQueryAmdGpuName(uint16_t deviceId, uint8_t revisionId, FFGPUResult* gpu);
5555

5656
#if FF_HAVE_DRM
57+
const char* ffDrmDetectRadeon(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath);
5758
const char* ffDrmDetectAmdgpu(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath);
5859
const char* ffDrmDetectI915(FFGPUResult* gpu, int fd);
5960
const char* ffDrmDetectXe(FFGPUResult* gpu, int fd);

src/detection/gpu/gpu_bsd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static const char* detectByDrm(const FFGPUOptions* options, FFlist* gpus)
109109
ffDrmDetectI915(gpu, fd);
110110
else if (ffStrStartsWith(driverName, "amdgpu"))
111111
ffDrmDetectAmdgpu(options, gpu, dev->nodes[DRM_NODE_RENDER]);
112+
else if (ffStrStartsWith(driverName, "radeon"))
113+
ffDrmDetectRadeon(options, gpu, dev->nodes[DRM_NODE_RENDER]);
112114
else if (ffStrStartsWith(driverName, "xe"))
113115
ffDrmDetectXe(gpu, fd);
114116
else if (ffStrStartsWith(driverName, "asahi"))

src/detection/gpu/gpu_drm.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,44 @@
1212

1313
#include "intel_drm.h"
1414
#include "asahi_drm.h"
15+
#include <radeon_drm.h>
16+
17+
const char* ffDrmDetectRadeon(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath)
18+
{
19+
FF_AUTO_CLOSE_FD int fd = open(renderPath, O_RDONLY);
20+
if (fd < 0) return "Failed to open DRM render device";
21+
22+
struct drm_radeon_info info;
23+
info.request = RADEON_INFO_ACTIVE_CU_COUNT;
24+
if (ioctl(fd, DRM_IOCTL_RADEON_INFO, &info) >= 0)
25+
gpu->coreCount = (int32_t) info.value;
26+
27+
if (options->temp)
28+
{
29+
info.request = RADEON_INFO_CURRENT_GPU_TEMP; // millidegrees C
30+
if (ioctl(fd, DRM_IOCTL_RADEON_INFO, &info) >= 0)
31+
gpu->temperature = (double) info.value / 1000.0;
32+
}
33+
34+
info.request = RADEON_INFO_MAX_SCLK; // MHz
35+
if (ioctl(fd, DRM_IOCTL_RADEON_INFO, &info) >= 0)
36+
gpu->frequency = (uint32_t) (info.value / 1000u);
37+
38+
struct drm_radeon_gem_info gemInfo;
39+
if (ioctl(fd, DRM_IOCTL_RADEON_GEM_INFO, &gemInfo) >= 0 && gemInfo.vram_visible > 0)
40+
{
41+
gpu->type = FF_GPU_TYPE_DISCRETE;
42+
if (options->driverSpecific)
43+
{
44+
gpu->dedicated.total = gemInfo.vram_visible;
45+
info.request = RADEON_INFO_VRAM_USAGE;
46+
if (ioctl(fd, DRM_IOCTL_RADEON_INFO, &info) >= 0)
47+
gpu->dedicated.used = info.value;
48+
}
49+
}
50+
51+
return NULL;
52+
}
1553

1654
#ifdef FF_HAVE_DRM_AMDGPU
1755
#include <amdgpu.h>

src/detection/gpu/gpu_linux.c

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,19 @@ FF_MAYBE_UNUSED static const char* drmFindRenderFromCard(const char* drmCardKey,
105105

106106
static const char* drmDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu, const char* drmKey, FFstrbuf* buffer)
107107
{
108-
#if FF_HAVE_DRM_AMDGPU
109108
const char* error = drmFindRenderFromCard(drmKey, buffer);
110109
if (error) return error;
111-
return ffDrmDetectAmdgpu(options, gpu, buffer->chars);
112-
#else
113-
FF_UNUSED(options, gpu, drmKey, buffer);
114-
return "Fastfetch is not compiled with libdrm_amdgpu support";
110+
if (ffStrbufEqualS(&gpu->driver, "radeon"))
111+
return ffDrmDetectRadeon(options, gpu, buffer->chars);
112+
else
113+
{
114+
#if FF_HAVE_DRM_AMDGPU
115+
return ffDrmDetectAmdgpu(options, gpu, buffer->chars);
116+
#else
117+
FF_UNUSED(options, gpu, drmKey, buffer);
118+
return "Fastfetch is not compiled with libdrm_amdgpu support";
115119
#endif
120+
}
116121
}
117122

118123
static void pciDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer)
@@ -135,47 +140,50 @@ static void pciDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu,
135140
ffStrbufAppendC(pciDir, '/');
136141

137142
const uint32_t hwmonLen = pciDir->length;
138-
ffStrbufAppendS(pciDir, "in1_input"); // Northbridge voltage in millivolts (APUs only)
139-
if (ffPathExists(pciDir->chars, FF_PATHTYPE_ANY))
140-
gpu->type = FF_GPU_TYPE_INTEGRATED;
141-
else
142-
gpu->type = FF_GPU_TYPE_DISCRETE;
143-
144143
uint64_t value = 0;
145144
if (options->temp)
146145
{
147-
ffStrbufSubstrBefore(pciDir, hwmonLen);
148146
ffStrbufAppendS(pciDir, "temp1_input"); // The on die GPU temperature in millidegrees Celsius
149147
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
150148
gpu->temperature = (double) value / 1000;
151149
}
152150

153-
if (options->driverSpecific)
151+
if (ffStrbufEqualS(&gpu->driver, "amdgpu")) // Ancient radeon drivers don't have these files
154152
{
155-
ffStrbufSubstrBefore(pciDir, pciDirLen);
156-
ffStrbufAppendS(pciDir, "/mem_info_vis_vram_total");
157-
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
158-
{
159-
if (gpu->type == FF_GPU_TYPE_DISCRETE)
160-
gpu->dedicated.total = value;
161-
else
162-
gpu->shared.total = value;
153+
ffStrbufSubstrBefore(pciDir, hwmonLen);
154+
ffStrbufAppendS(pciDir, "in1_input"); // Northbridge voltage in millivolts (APUs only)
155+
if (ffPathExists(pciDir->chars, FF_PATHTYPE_ANY))
156+
gpu->type = FF_GPU_TYPE_INTEGRATED;
157+
else
158+
gpu->type = FF_GPU_TYPE_DISCRETE;
163159

164-
ffStrbufSubstrBefore(pciDir, pciDir->length - (uint32_t) strlen("/mem_info_vis_vram_total"));
165-
ffStrbufAppendS(pciDir, "/mem_info_vis_vram_used");
160+
if (options->driverSpecific)
161+
{
162+
ffStrbufSubstrBefore(pciDir, pciDirLen);
163+
ffStrbufAppendS(pciDir, "/mem_info_vis_vram_total");
166164
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
167165
{
168166
if (gpu->type == FF_GPU_TYPE_DISCRETE)
169-
gpu->dedicated.used = value;
167+
gpu->dedicated.total = value;
170168
else
171-
gpu->shared.used = value;
169+
gpu->shared.total = value;
170+
171+
ffStrbufSubstrBefore(pciDir, pciDir->length - (uint32_t) strlen("/mem_info_vis_vram_total"));
172+
ffStrbufAppendS(pciDir, "/mem_info_vis_vram_used");
173+
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
174+
{
175+
if (gpu->type == FF_GPU_TYPE_DISCRETE)
176+
gpu->dedicated.used = value;
177+
else
178+
gpu->shared.used = value;
179+
}
172180
}
173-
}
174181

175-
ffStrbufSubstrBefore(pciDir, pciDirLen);
176-
ffStrbufAppendS(pciDir, "/gpu_busy_percent");
177-
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
178-
gpu->coreUsage = (double) value;
182+
ffStrbufSubstrBefore(pciDir, pciDirLen);
183+
ffStrbufAppendS(pciDir, "/gpu_busy_percent");
184+
if (ffReadFileBuffer(pciDir->chars, buffer) && (value = ffStrbufToUInt(buffer, 0)))
185+
gpu->coreUsage = (double) value;
186+
}
179187
}
180188
}
181189

0 commit comments

Comments
 (0)