Skip to content

Commit cb403be

Browse files
committed
GPU: use pciaccess to enumerate PCI devices
1 parent 2bc66d7 commit cb403be

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ cmake_dependent_option(ENABLE_DIRECTX_HEADERS "Enable DirectX headers for WSL" O
7777
cmake_dependent_option(ENABLE_ELF "Enable libelf" ON "LINUX OR ANDROID" OFF)
7878
cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND" OFF)
7979
cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR SunOS" OFF)
80+
cmake_dependent_option(ENABLE_PCIACCESS "Enable libpciaccess" ON "NetBSD OR OpenBSD OR SunOS" OFF)
8081

8182
option(ENABLE_SYSTEM_YYJSON "Use system provided (instead of fastfetch embedded) yyjson library" OFF)
8283
option(ENABLE_ASAN "Build fastfetch with ASAN (address sanitizer)" OFF)
@@ -705,7 +706,7 @@ elseif(NetBSD)
705706
src/detection/displayserver/linux/xcb.c
706707
src/detection/displayserver/linux/xlib.c
707708
src/detection/font/font_linux.c
708-
src/detection/gpu/gpu_nosupport.c
709+
src/detection/gpu/gpu_general.c
709710
src/detection/gpu/gpu_pci.c
710711
src/detection/gtk_qt/gtk.c
711712
src/detection/host/host_nbsd.c
@@ -787,7 +788,8 @@ elseif(OpenBSD)
787788
src/detection/displayserver/linux/xcb.c
788789
src/detection/displayserver/linux/xlib.c
789790
src/detection/font/font_linux.c
790-
src/detection/gpu/gpu_nosupport.c
791+
src/detection/gpu/gpu_pci.c
792+
src/detection/gpu/gpu_general.c
791793
src/detection/gtk_qt/gtk.c
792794
src/detection/host/host_obsd.c
793795
src/detection/lm/lm_nosupport.c
@@ -1327,6 +1329,10 @@ ff_lib_enable(DIRECTX_HEADERS
13271329
"DirectX-Headers"
13281330
"DirectX-Headers"
13291331
)
1332+
ff_lib_enable(PCIACCESS
1333+
"pciaccess"
1334+
"pciaccess"
1335+
)
13301336
# The system <libzfs.h> is only usable on SunOS. We provide our local copy of it so it's always available.
13311337
if(ENABLE_LIBZFS)
13321338
if(BINARY_LINK_TYPE STREQUAL "dlopen")

src/detection/gpu/gpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus);
4848

4949
const char* ffGetGPUVendorString(unsigned vendorId);
5050

51-
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun)
51+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__OpenBSD__)
5252
void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
5353
#endif

src/detection/gpu/gpu_general.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "gpu.h"
2+
3+
#ifdef FF_HAVE_PCIACCESS
4+
5+
#include "common/properties.h"
6+
#include "common/io/io.h"
7+
#include "common/library.h"
8+
9+
#include <pciaccess.h>
10+
11+
const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* gpus)
12+
{
13+
FF_LIBRARY_LOAD(pciaccess, "Failed to load libpciaccess" FF_LIBRARY_EXTENSION, "libpciaccess" FF_LIBRARY_EXTENSION, 0)
14+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pciaccess, pci_system_init)
15+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pciaccess, pci_slot_match_iterator_create)
16+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pciaccess, pci_device_next)
17+
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(pciaccess, pci_system_cleanup)
18+
19+
{
20+
// Requires root access
21+
// Same behavior can be observed with `cp $(which scanpci) /tmp/ && /tmp/scanpci`
22+
FF_SUPPRESS_IO();
23+
if (ffpci_system_init() < 0)
24+
return "pci_system_init() failed";
25+
}
26+
27+
struct pci_device_iterator* iter = ffpci_slot_match_iterator_create(NULL);
28+
for (struct pci_device* dev = NULL; (dev = ffpci_device_next(iter)); )
29+
{
30+
if (dev->device_class >> 16 != 0x03 /*PCI_BASE_CLASS_DISPLAY*/)
31+
continue;
32+
33+
FFGPUResult* gpu = (FFGPUResult*)ffListAdd(gpus);
34+
ffStrbufInitStatic(&gpu->vendor, ffGetGPUVendorString(dev->vendor_id));
35+
ffStrbufInit(&gpu->name);
36+
ffStrbufInit(&gpu->driver);
37+
ffStrbufInit(&gpu->platformApi);
38+
gpu->temperature = FF_GPU_TEMP_UNSET;
39+
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
40+
gpu->type = FF_GPU_TYPE_UNKNOWN;
41+
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
42+
gpu->deviceId = ((uint64_t) dev->domain << 6) | ((uint64_t) dev->bus << 4) | ((uint64_t) dev->dev << 2) | (uint64_t) dev->func;
43+
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
44+
45+
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
46+
{
47+
char query[32];
48+
snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) dev->device_id, (unsigned) dev->revision);
49+
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
50+
}
51+
52+
if (gpu->name.length == 0)
53+
{
54+
ffGPUFillVendorAndName((dev->device_class >> 8) & 8, dev->vendor_id, dev->device_id, gpu);
55+
}
56+
}
57+
58+
ffpci_system_cleanup();
59+
return NULL;
60+
}
61+
62+
#else
63+
#endif

src/detection/gpu/gpu_pci.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ static const FFstrbuf* loadPciIds()
4141
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids", &pciids);
4242
#elif __sun
4343
ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/share/hwdata/pci.ids", &pciids);
44+
#elif __OpenBSD__
45+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/local/share/pci.ids", &pciids);
46+
#elif __NetBSD__
47+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/pkg/share/pciutils/pci.ids", &pciids);
4448
#endif
4549

4650
#endif // FF_CUSTOM_PCI_IDS_PATH

0 commit comments

Comments
 (0)