GPU: code cleanups and bug fixes

This commit is contained in:
Carter Li
2026-07-09 20:03:18 +08:00
parent 0495c47909
commit 55e674e1cb
7 changed files with 24 additions and 21 deletions
+1 -1
View File
@@ -324,7 +324,7 @@ const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result) {
return "GPU detection failed";
}
bool ffGPUFillVendorByDeviceName(FFGPUResult* gpu) {
bool ffGPUDetectTypeByVendorAndName(FFGPUResult* gpu) {
if (gpu->type != FF_GPU_TYPE_UNKNOWN) {
return true;
}
+1 -1
View File
@@ -107,4 +107,4 @@ static inline uint64_t ffGPUGeneral2Id(uint64_t originalId) {
return (1ULL << 63) | originalId;
}
bool ffGPUFillVendorByDeviceName(FFGPUResult* gpu);
bool ffGPUDetectTypeByVendorAndName(FFGPUResult* gpu);
+1 -1
View File
@@ -77,7 +77,7 @@ static const char* detectByPci(const FFGPUOptions* options, FFlist* gpus) {
}
}
ffGPUFillVendorByDeviceName(gpu);
ffGPUDetectTypeByVendorAndName(gpu);
}
return NULL;
+2 -3
View File
@@ -72,7 +72,6 @@ static const char* drmGetPciinfo(int drmfd, const char* drmId, struct drm_pciinf
};
if (ioctl(pcifd, PCIOCGETCONF, &pcio) < 0) {
perror("ioctl");
return "ioctl(pcifd, PCIOCGETCONF, &pc) failed";
}
@@ -188,7 +187,7 @@ const char* ffGPUDetectByDrmBSD(const FFGPUOptions* options, FFlist* gpus) {
}
}
ffGPUFillVendorByDeviceName(gpu);
ffGPUDetectTypeByVendorAndName(gpu);
}
return NULL;
@@ -201,4 +200,4 @@ const char* ffGPUDetectByDrmBSD(const FFGPUOptions* options, FFlist* gpus) {
return "Fastfetch was built without libdrm support";
}
#endif // __FreeBSD__ || __OpenBSD__
#endif // FF_HAVE_DRM
+5 -4
View File
@@ -50,7 +50,7 @@ const char* ffDrmDetectRadeon(const FFGPUOptions* options, FFGPUResult* gpu, con
}
if (ioctl(fd, DRM_IOCTL_RADEON_INFO, &(struct drm_radeon_info) {
.request = RADEON_INFO_MAX_SCLK, // MHz
.request = RADEON_INFO_MAX_SCLK, // KHz
.value = (uintptr_t) &value,
}) >= 0) {
gpu->frequency = (uint32_t) (value / 1000u);
@@ -59,7 +59,7 @@ const char* ffDrmDetectRadeon(const FFGPUOptions* options, FFGPUResult* gpu, con
if (options->driverSpecific) {
struct drm_radeon_gem_info gemInfo;
if (ioctl(fd, DRM_IOCTL_RADEON_GEM_INFO, &gemInfo) >= 0) {
// vram_usage can be bigger than vram_usage, so we use vram_size here
// vram_usage can be bigger than vram_size, so we use vram_size here
gpu->dedicated.total = gemInfo.vram_size;
gpu->shared.total = gemInfo.gart_size;
@@ -146,6 +146,7 @@ const char* ffDrmDetectAmdgpu(const FFGPUOptions* options, FFGPUResult* gpu, con
ffStrbufAppendF(&gpu->memoryType, "Unknown (%u)", devInfo.vram_type);
break;
}
#undef FF_VRAM_CASE
}
struct drm_amdgpu_memory_info memInfo;
@@ -228,12 +229,12 @@ static inline int popcountBytes(uint8_t* bytes, uint32_t length) {
length -= 4;
}
if (length >= 2) {
count += __builtin_popcountl(*(uint16_t*) bytes);
count += __builtin_popcount(*(uint16_t*) bytes);
bytes += 2;
length -= 2;
}
if (length) {
count += __builtin_popcountl(*(uint8_t*) bytes);
count += __builtin_popcount(*(uint8_t*) bytes);
}
return count;
}
+11 -8
View File
@@ -7,7 +7,7 @@
#include <inttypes.h>
#include <stdint.h>
static bool pciDetectDriver(FFstrbuf* result, FFstrbuf* pciDir, FFstrbuf* buffer, FF_A_UNUSED const char* drmKey) {
static bool detectDriverFromSysfs(FFstrbuf* result, FFstrbuf* pciDir, FFstrbuf* buffer, FF_A_UNUSED const char* drmKey) {
uint32_t pciDirLength = pciDir->length;
ffStrbufAppendS(pciDir, "/driver");
char pathBuf[PATH_MAX];
@@ -90,6 +90,7 @@ static void pciDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu,
ffStrbufAppendS(pciDir, "/hwmon/");
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(pciDir->chars);
if (!dirp) {
ffStrbufSubstrBefore(pciDir, pciDirLen);
return;
}
@@ -101,6 +102,7 @@ static void pciDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu,
break;
}
if (!entry) {
ffStrbufSubstrBefore(pciDir, pciDirLen);
return;
}
ffStrbufAppendS(pciDir, entry->d_name);
@@ -407,7 +409,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
ffStrbufSetF(&gpu->platformApi, "DRM (%s)", drmKey);
}
pciDetectDriver(&gpu->driver, deviceDir, buffer, drmKey);
detectDriverFromSysfs(&gpu->driver, deviceDir, buffer, drmKey);
ffStrbufSubstrBefore(deviceDir, drmDirPathLength);
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD) {
@@ -457,7 +459,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
ffGPUFillVendorAndName(subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
}
ffGPUFillVendorByDeviceName(gpu);
ffGPUDetectTypeByVendorAndName(gpu);
if (options->driverSpecific && gpu->pcieSpeed == FF_GPU_PCIE_SPEED_UNSET) {
ffStrbufAppendS(deviceDir, "/max_link_speed");
@@ -535,12 +537,12 @@ static const char* detectOf(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, co
gpu->temperature = FF_GPU_TEMP_UNSET;
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
gpu->type = FF_GPU_TYPE_INTEGRATED;
gpu->type = FF_GPU_TYPE_INTEGRATED; // Open Firmware is only used on integrated GPUs
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
gpu->pcieSpeed = FF_GPU_PCIE_SPEED_UNSET;
pciDetectDriver(&gpu->driver, drmDir, buffer, drmKey);
detectDriverFromSysfs(&gpu->driver, drmDir, buffer, drmKey);
#ifdef __aarch64__
if (ffStrbufEqualS(&gpu->driver, "asahi")) {
@@ -557,7 +559,7 @@ static const char* detectOf(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, co
ffStrbufSetStatic(&gpu->vendor, "Broadcom"); // Raspberry Pi
} else {
ffStrbufSetS(&gpu->vendor, compatible);
gpu->vendor.chars[0] = (char) toupper(compatible[0]);
gpu->vendor.chars[0] = (char) toupper((uint8_t) compatible[0]);
}
}
@@ -586,10 +588,11 @@ static const char* drmDetectGPUs(const FFGPUOptions* options, FFlist* gpus) {
ffStrbufAppendS(&drmDir, entry->d_name);
ffStrbufAppendS(&drmDir, "/device/modalias");
if (!ffReadFileBuffer(drmDir.chars, &buffer)) {
ffReadFileBuffer(drmDir.chars, &buffer);
ffStrbufSubstrBefore(&drmDir, drmDir.length - (uint32_t) strlen("/modalias"));
if (!buffer.length) {
continue;
}
ffStrbufSubstrBefore(&drmDir, drmDir.length - (uint32_t) strlen("/modalias"));
if (ffStrbufStartsWithS(&buffer, "pci:")) {
detectPci(options, gpus, &buffer, &drmDir, entry->d_name);
+3 -3
View File
@@ -225,7 +225,7 @@ static void ffStrbufSetWS(FFstrbuf* strbuf, const char16_t* str) {
static void closeDxgfd(void) {
if (dxgfd >= 0) {
close(dxgfd);
dxgfd = 0;
dxgfd = -2;
FF_DEBUG("Closed /dev/dxg file descriptor");
}
}
@@ -294,7 +294,7 @@ ffGPUDetectWsl2
});
if (!NT_SUCCESS(status)) {
FF_DEBUG("KMTQAITYPE_ADAPTERTYPE query failed for adapter #%u: %s", i, ffDebugNtStatus(status));
continue;
goto close_adapter;
}
if (adapterType.SoftwareDevice) {
FF_DEBUG("Skipping software adapter #%u", i);
@@ -607,7 +607,7 @@ ffGPUDetectWsl2
}
if (gpu->type == FF_GPU_TYPE_UNKNOWN) {
if (ffGPUFillVendorByDeviceName(gpu)) {
if (ffGPUDetectTypeByVendorAndName(gpu)) {
// OK
}
#if _WIN32