Skip to content

Commit 8559cc4

Browse files
committed
GPU (Linux / FreeBSD): unifies DRM related code and code clean up
1 parent 40e33c6 commit 8559cc4

File tree

6 files changed

+378
-480
lines changed

6 files changed

+378
-480
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ if(LINUX)
529529
src/detection/displayserver/linux/xlib.c
530530
src/detection/font/font_linux.c
531531
src/detection/gpu/gpu_linux.c
532+
src/detection/gpu/gpu_drm.c
532533
src/detection/gpu/gpu_pci.c
533534
src/detection/gtk_qt/gtk.c
534535
src/detection/host/host_linux.c
@@ -676,6 +677,7 @@ elseif(FreeBSD)
676677
src/detection/displayserver/linux/xlib.c
677678
src/detection/font/font_linux.c
678679
src/detection/gpu/gpu_bsd.c
680+
src/detection/gpu/gpu_drm.c
679681
src/detection/gpu/gpu_pci.c
680682
src/detection/gtk_qt/gtk.c
681683
src/detection/host/host_bsd.c

src/detection/gpu/gpu.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,21 @@ const char* ffGPUGetVendorString(unsigned vendorId);
5252
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
5353
void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
5454
void ffGPUQueryAmdGpuName(uint16_t deviceId, uint8_t revisionId, FFGPUResult* gpu);
55-
#endif
55+
56+
#if FF_HAVE_DRM
57+
const char* ffDrmDetectAmdgpu(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath);
58+
const char* ffDrmDetectI915(FFGPUResult* gpu, int fd);
59+
const char* ffDrmDetectXe(FFGPUResult* gpu, int fd);
60+
const char* ffDrmDetectAsahi(FFGPUResult* gpu, int fd);
61+
#endif // FF_HAVE_DRM
62+
63+
typedef struct FFGpuDriverPciBusId
64+
{
65+
uint32_t domain;
66+
uint32_t bus;
67+
uint32_t device;
68+
uint32_t func;
69+
} FFGpuDriverPciBusId;
70+
const char* ffGPUDetectDriverSpecific(const FFGPUOptions* options, FFGPUResult* gpu, FFGpuDriverPciBusId pciBusId);
71+
72+
#endif // defined(XXX)

src/detection/gpu/gpu_bsd.c

Lines changed: 25 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@
1111
#include <bus/pci/pcireg.h> // DragonFly
1212
#endif
1313

14-
#include "common/library.h"
15-
#include "util/stringUtils.h"
16-
#include <xf86drm.h>
17-
#include <i915_drm.h>
18-
19-
#ifdef FF_HAVE_DRM_AMDGPU
20-
#include <amdgpu.h>
21-
#include <amdgpu_drm.h>
22-
#include <fcntl.h>
23-
#endif
24-
2514
static void fillGPUTypeGeneric(FFGPUResult* gpu)
2615
{
2716
if (gpu->type == FF_GPU_TYPE_UNKNOWN)
@@ -47,137 +36,10 @@ static void fillGPUTypeGeneric(FFGPUResult* gpu)
4736
}
4837

4938
#if FF_HAVE_DRM
50-
static const char* drmDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath)
51-
{
52-
#if FF_HAVE_DRM_AMDGPU
53-
FF_LIBRARY_LOAD(libdrm, "dlopen libdrm_amdgpu" FF_LIBRARY_EXTENSION " failed", "libdrm_amdgpu" FF_LIBRARY_EXTENSION, 1)
54-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_device_initialize)
55-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_get_marketing_name)
56-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_query_gpu_info)
57-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_query_sensor_info)
58-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_query_heap_info)
59-
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libdrm, amdgpu_device_deinitialize)
60-
61-
FF_AUTO_CLOSE_FD int fd = open(renderPath, O_RDONLY);
62-
if (fd < 0) return "Failed to open DRM device";
63-
64-
amdgpu_device_handle handle;
65-
uint32_t majorVersion, minorVersion;
66-
if (ffamdgpu_device_initialize(fd, &majorVersion, &minorVersion, &handle) < 0)
67-
return "Failed to initialize AMDGPU device";
68-
69-
ffStrbufAppendF(&gpu->driver, " %u.%u", (unsigned) majorVersion, (unsigned) minorVersion);
70-
71-
uint32_t value;
72-
73-
if (options->temp)
74-
{
75-
if (ffamdgpu_query_sensor_info(handle, AMDGPU_INFO_SENSOR_GPU_TEMP, sizeof(value), &value) >= 0)
76-
gpu->temperature = value / 1000.;
77-
}
78-
79-
ffStrbufSetS(&gpu->name, ffamdgpu_get_marketing_name(handle));
80-
81-
struct amdgpu_gpu_info gpuInfo;
82-
if (ffamdgpu_query_gpu_info(handle, &gpuInfo) >= 0)
83-
{
84-
gpu->coreCount = (int32_t) gpuInfo.cu_active_number;
85-
gpu->frequency = (uint32_t) (gpuInfo.max_engine_clk / 1000u);
86-
gpu->index = FF_GPU_INDEX_UNSET;
87-
gpu->type = gpuInfo.ids_flags & AMDGPU_IDS_FLAGS_FUSION ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE;
88-
#define FF_VRAM_CASE(name, value) case value /* AMDGPU_VRAM_TYPE_ ## name */: ffStrbufSetStatic(&gpu->memoryType, #name); break
89-
switch (gpuInfo.vram_type)
90-
{
91-
FF_VRAM_CASE(UNKNOWN, 0);
92-
FF_VRAM_CASE(GDDR1, 1);
93-
FF_VRAM_CASE(DDR2, 2);
94-
FF_VRAM_CASE(GDDR3, 3);
95-
FF_VRAM_CASE(GDDR4, 4);
96-
FF_VRAM_CASE(GDDR5, 5);
97-
FF_VRAM_CASE(HBM, 6);
98-
FF_VRAM_CASE(DDR3, 7);
99-
FF_VRAM_CASE(DDR4, 8);
100-
FF_VRAM_CASE(GDDR6, 9);
101-
FF_VRAM_CASE(DDR5, 10);
102-
FF_VRAM_CASE(LPDDR4, 11);
103-
FF_VRAM_CASE(LPDDR5, 12);
104-
default:
105-
ffStrbufAppendF(&gpu->memoryType, "Unknown (%u)", gpuInfo.vram_type);
106-
break;
107-
}
108-
109-
struct amdgpu_heap_info heapInfo;
110-
if (ffamdgpu_query_heap_info(handle, AMDGPU_GEM_DOMAIN_VRAM, 0, &heapInfo) >= 0)
111-
{
112-
if (gpu->type == FF_GPU_TYPE_DISCRETE)
113-
{
114-
gpu->dedicated.total = heapInfo.heap_size;
115-
gpu->dedicated.used = heapInfo.heap_usage;
116-
}
117-
else
118-
{
119-
gpu->shared.total = heapInfo.heap_size;
120-
gpu->shared.used = heapInfo.heap_usage;
121-
}
122-
}
123-
}
124-
125-
if (ffamdgpu_query_sensor_info(handle, AMDGPU_INFO_SENSOR_GPU_LOAD, sizeof(value), &value) >= 0)
126-
gpu->coreUsage = value;
127-
128-
ffamdgpu_device_deinitialize(handle);
129-
130-
return NULL;
131-
#else
132-
FF_UNUSED(options, gpu, drmKey, buffer);
133-
return "Fastfetch is compiled without libdrm support";
134-
#endif
135-
}
39+
#include "common/library.h"
40+
#include "util/stringUtils.h"
13641

137-
static const char* drmDetectIntelSpecific(FFGPUResult* gpu, int fd)
138-
{
139-
{
140-
int value;
141-
drm_i915_getparam_t getparam = { .param = I915_PARAM_EU_TOTAL, .value = &value };
142-
if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &getparam) >= 0)
143-
gpu->coreCount = value;
144-
}
145-
{
146-
struct drm_i915_query_item queryItem = {
147-
.query_id = DRM_I915_QUERY_MEMORY_REGIONS,
148-
};
149-
struct drm_i915_query query = {
150-
.items_ptr = (uintptr_t) &queryItem,
151-
.num_items = 1,
152-
};
153-
if (ioctl(fd, DRM_IOCTL_I915_QUERY, &query) >= 0 )
154-
{
155-
FF_AUTO_FREE uint8_t* buffer = calloc(1, (size_t) queryItem.length);
156-
queryItem.data_ptr = (uintptr_t) buffer;
157-
if (ioctl(fd, DRM_IOCTL_I915_QUERY, &query) >= 0)
158-
{
159-
gpu->dedicated.total = gpu->shared.total = gpu->dedicated.used = gpu->shared.used = 0;
160-
struct drm_i915_query_memory_regions* regionInfo = (void*) buffer;
161-
for (uint32_t i = 0; i < regionInfo->num_regions; i++)
162-
{
163-
struct drm_i915_memory_region_info* region = regionInfo->regions + i;
164-
switch (region->region.memory_class)
165-
{
166-
case I915_MEMORY_CLASS_SYSTEM:
167-
gpu->shared.total += region->probed_size;
168-
gpu->shared.used += region->probed_size - region->unallocated_size;
169-
break;
170-
case I915_MEMORY_CLASS_DEVICE:
171-
gpu->dedicated.total += region->probed_size;
172-
gpu->dedicated.used += region->probed_size - region->unallocated_size;
173-
break;
174-
}
175-
}
176-
}
177-
}
178-
}
179-
return NULL;
180-
}
42+
#include <xf86drm.h>
18143

18244
static const char* detectByDrm(const FFGPUOptions* options, FFlist* gpus)
18345
{
@@ -235,43 +97,30 @@ static const char* detectByDrm(const FFGPUOptions* options, FFlist* gpus)
23597
if (fd < 0) continue;
23698

23799
char driverName[64];
238-
char driverDesc[64];
100+
driverName[0] = '\0';
239101
struct drm_version ver = {
240102
.name = driverName,
241103
.name_len = ARRAY_SIZE(driverName),
242-
.desc = driverDesc,
243-
.desc_len = ARRAY_SIZE(driverDesc),
244104
};
245105
if (ioctl(fd, DRM_IOCTL_VERSION, &ver) == 0)
246106
ffStrbufSetF(&gpu->driver, "%*s %d.%d.%d", (int) ver.name_len, ver.name, ver.version_major, ver.version_minor, ver.version_patchlevel);
247107

248-
if (dev->bustype != DRM_BUS_PCI)
249-
continue;
250-
251-
if (ffStrEquals(ver.name, "i915"))
252-
drmDetectIntelSpecific(gpu, fd);
253-
else if (ffStrEquals(ver.name, "amdgpu"))
254-
drmDetectAmdSpecific(options, gpu, dev->nodes[DRM_NODE_RENDER]);
255-
else if (ffStrEquals(ver.name, "nvidia-drm") && (options->temp || options->driverSpecific))
108+
if (ffStrStartsWith(driverName, "i915"))
109+
ffDrmDetectI915(gpu, fd);
110+
else if (ffStrStartsWith(driverName, "amdgpu"))
111+
ffDrmDetectAmdgpu(options, gpu, dev->nodes[DRM_NODE_RENDER]);
112+
else if (ffStrStartsWith(driverName, "xe"))
113+
ffDrmDetectXe(gpu, fd);
114+
else if (ffStrStartsWith(driverName, "asahi"))
115+
ffDrmDetectAsahi(gpu, fd);
116+
else if (dev->bustype == DRM_BUS_PCI)
256117
{
257-
ffDetectNvidiaGpuInfo(&(FFGpuDriverCondition) {
258-
.type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID,
259-
.pciBusId = {
260-
.domain = (uint32_t) dev->businfo.pci->domain,
261-
.bus = dev->businfo.pci->bus,
262-
.device = dev->businfo.pci->dev,
263-
.func = dev->businfo.pci->func,
264-
},
265-
}, (FFGpuDriverResult) {
266-
.index = &gpu->index,
267-
.temp = options->temp ? &gpu->temperature : NULL,
268-
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
269-
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
270-
.type = &gpu->type,
271-
.frequency = &gpu->frequency,
272-
.coreUsage = &gpu->coreUsage,
273-
.name = &gpu->name,
274-
}, "libnvidia-ml.so");
118+
ffGPUDetectDriverSpecific(options, gpu, (FFGpuDriverPciBusId) {
119+
.domain = (uint32_t) dev->businfo.pci->domain,
120+
.bus = dev->businfo.pci->bus,
121+
.device = dev->businfo.pci->dev,
122+
.func = dev->businfo.pci->func,
123+
});
275124
}
276125

277126
if (gpu->name.length == 0)
@@ -333,27 +182,12 @@ static const char* detectByPci(const FFGPUOptions* options, FFlist* gpus)
333182
gpu->deviceId = (pc->pc_sel.pc_domain * 100000ull) + (pc->pc_sel.pc_bus * 1000ull) + (pc->pc_sel.pc_dev * 10ull) + pc->pc_sel.pc_func;
334183
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
335184

336-
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA && (options->temp || options->driverSpecific))
337-
{
338-
ffDetectNvidiaGpuInfo(&(FFGpuDriverCondition) {
339-
.type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID,
340-
.pciBusId = {
341-
.domain = (uint32_t) pc->pc_sel.pc_domain,
342-
.bus = pc->pc_sel.pc_bus,
343-
.device = pc->pc_sel.pc_dev,
344-
.func = pc->pc_sel.pc_func,
345-
},
346-
}, (FFGpuDriverResult) {
347-
.index = &gpu->index,
348-
.temp = options->temp ? &gpu->temperature : NULL,
349-
.memory = options->driverSpecific ? &gpu->dedicated : NULL,
350-
.coreCount = options->driverSpecific ? (uint32_t*) &gpu->coreCount : NULL,
351-
.type = &gpu->type,
352-
.frequency = &gpu->frequency,
353-
.coreUsage = &gpu->coreUsage,
354-
.name = &gpu->name,
355-
}, "libnvidia-ml.so");
356-
}
185+
ffGPUDetectDriverSpecific(options, gpu, (FFGpuDriverPciBusId) {
186+
.domain = (uint32_t) pc->pc_sel.pc_domain,
187+
.bus = pc->pc_sel.pc_bus,
188+
.device = pc->pc_sel.pc_dev,
189+
.func = pc->pc_sel.pc_func,
190+
});
357191

358192
if (gpu->name.length == 0)
359193
{

src/detection/gpu/gpu_driver_specific.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ typedef enum __attribute__((__packed__)) FFGpuDriverConditionType
1010
FF_GPU_DRIVER_CONDITION_TYPE_FORCE_UNSIGNED = UINT8_MAX,
1111
} FFGpuDriverConditionType;
1212

13-
typedef struct FFGpuDriverPciBusId
14-
{
15-
uint32_t domain;
16-
uint32_t bus;
17-
uint32_t device;
18-
uint32_t func;
19-
} FFGpuDriverPciBusId;
20-
2113
typedef struct FFGpuDriverPciDeviceId
2214
{
2315
uint32_t deviceId;

0 commit comments

Comments
 (0)