Skip to content

Commit ae6817e

Browse files
committed
GPU: simplify logic of pci.ids file loading
1 parent 582b0fa commit ae6817e

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

src/detection/gpu/gpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus);
4949
const char* ffGetGPUVendorString(unsigned vendorId);
5050

5151
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun)
52-
void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
52+
void ffGPUParsePciIds(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
5353
#endif

src/detection/gpu/gpu_bsd.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,6 @@
88
#include <dev/pci/pcireg.h>
99
#include <sys/pciio.h>
1010
#include <fcntl.h>
11-
#include <paths.h>
12-
13-
static bool loadPciIds(FFstrbuf* pciids)
14-
{
15-
// https://github.com/freebsd/freebsd-src/blob/main/usr.sbin/pciconf/pathnames.h
16-
17-
ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", pciids);
18-
if (pciids->length > 0) return true;
19-
20-
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids", pciids);
21-
if (pciids->length > 0) return true;
22-
23-
return false;
24-
}
2511

2612
const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
2713
{
@@ -45,8 +31,6 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
4531
if (pcio.status == PCI_GETCONF_ERROR)
4632
return "ioctl(fd, PCIOCGETCONF, &pc) returned error";
4733

48-
FF_STRBUF_AUTO_DESTROY pciids = ffStrbufCreate();
49-
5034
for (uint32_t i = 0; i < pcio.num_matches; ++i)
5135
{
5236
struct pci_conf* pc = &confs[i];
@@ -74,9 +58,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
7458

7559
if (gpu->name.length == 0)
7660
{
77-
if (pciids.length == 0)
78-
loadPciIds(&pciids);
79-
ffGPUParsePciIds(&pciids, pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu);
61+
ffGPUParsePciIds(pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu);
8062
}
8163

8264
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA && (options->temp || options->driverSpecific))

src/detection/gpu/gpu_linux.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
303303

304304
if (gpu->name.length == 0)
305305
{
306-
static FFstrbuf pciids;
307-
if (pciids.chars == NULL)
308-
{
309-
ffStrbufInit(&pciids);
310-
loadPciIds(&pciids);
311-
}
312-
ffGPUParsePciIds(&pciids, subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
306+
ffGPUParsePciIds(subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
313307
}
314308

315309
pciDetectDriver(&gpu->driver, deviceDir, buffer, drmKey);

src/detection/gpu/gpu_pci.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
#include "gpu.h"
22

3-
void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu)
3+
#ifdef __FreeBSD__
4+
#include <paths.h>
5+
#endif
6+
7+
static const FFstrbuf* loadPciIds()
8+
{
9+
static FFstrbuf pciids;
10+
11+
if (pciids.chars) return &pciids;
12+
ffStrbufinit(&pciids);
13+
14+
#ifdef FF_CUSTOM_PCI_IDS_PATH
15+
16+
ffReadFileBuffer(FF_STR(FF_CUSTOM_PCI_IDS_PATH), pciids);
17+
18+
#else // FF_CUSTOM_PCI_IDS_PATH
19+
20+
#if __linux__
21+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/hwdata/pci.ids", pciids);
22+
if (pciids.length == 0)
23+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/misc/pci.ids", pciids); // debian?
24+
if (pciids.length == 0)
25+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/local/share/hwdata/pci.ids", pciids);
26+
#elif __FreeBSD__
27+
// https://github.com/freebsd/freebsd-src/blob/main/usr.sbin/pciconf/pathnames.h
28+
ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", pciids);
29+
if (pciids.length == 0)
30+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/share/pciids/pci.ids", pciids);
31+
#elif __sun
32+
ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/share/hwdata/pci.ids", &pciids);
33+
#endif
34+
35+
#endif // FF_CUSTOM_PCI_IDS_PATH
36+
37+
return &pciids;
38+
}
39+
40+
void ffGPUParsePciIds(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu)
441
{
42+
const FFstrbuf* content = loadPciIds();
543
if (content->length)
644
{
745
char buffer[32];

src/detection/gpu/gpu_sunos.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
2020
if (!ffStrbufStartsWithS(&buffer, "\npci "))
2121
return "Invalid scanpci result";
2222

23-
FF_STRBUF_AUTO_DESTROY pciids = ffStrbufCreate();
24-
2523
// pci bus 0x0000 cardnum 0x00 function 0x00: vendor 0x1414 device 0x008e
2624
// Device unknown
2725
// CardVendor 0x0000 card 0x0000 (Card unknown)
@@ -75,9 +73,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
7573

7674
if (gpu->name.length == 0)
7775
{
78-
if (pciids.length == 0)
79-
ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/share/hwdata/pci.ids", &pciids);
80-
ffGPUParsePciIds(&pciids, (uint8_t) subclass, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
76+
ffGPUParsePciIds((uint8_t) subclass, (uint16_t) vendorId, (uint16_t) deviceId, gpu);
8177
}
8278
}
8379

0 commit comments

Comments
 (0)