master ohmu/fastfetch-openbsd / src / detection / gpu / gpu.h
ohmu  ·  2026-10-15
  1#pragma once
  2
  3#include "fastfetch.h"
  4#include "modules/gpu/option.h"
  5
  6#define FF_GPU_TEMP_UNSET (-DBL_MAX)
  7#define FF_GPU_CORE_COUNT_UNSET -1
  8#define FF_GPU_VMEM_SIZE_UNSET ((uint64_t) -1)
  9#define FF_GPU_FREQUENCY_UNSET 0
 10#define FF_GPU_PCIE_SPEED_UNSET 0
 11#define FF_GPU_CORE_USAGE_UNSET (-DBL_MAX)
 12#define FF_GPU_INDEX_UNSET ((uint32_t) -1)
 13
 14extern const char* FF_GPU_VENDOR_NAME_APPLE;
 15extern const char* FF_GPU_VENDOR_NAME_AMD;
 16extern const char* FF_GPU_VENDOR_NAME_INTEL;
 17extern const char* FF_GPU_VENDOR_NAME_NVIDIA;
 18extern const char* FF_GPU_VENDOR_NAME_MTHREADS;
 19extern const char* FF_GPU_VENDOR_NAME_QUALCOMM;
 20extern const char* FF_GPU_VENDOR_NAME_MTK;
 21extern const char* FF_GPU_VENDOR_NAME_VMWARE;
 22extern const char* FF_GPU_VENDOR_NAME_PARALLELS;
 23extern const char* FF_GPU_VENDOR_NAME_MICROSOFT;
 24extern const char* FF_GPU_VENDOR_NAME_REDHAT;
 25extern const char* FF_GPU_VENDOR_NAME_ORACLE;
 26extern const char* FF_GPU_VENDOR_NAME_BROADCOM;
 27extern const char* FF_GPU_VENDOR_NAME_LOONGSON;
 28extern const char* FF_GPU_VENDOR_NAME_JINGJIA_MICRO;
 29extern const char* FF_GPU_VENDOR_NAME_HUAWEI;
 30extern const char* FF_GPU_VENDOR_NAME_ZHAOXIN;
 31extern const char* FF_GPU_VENDOR_NAME_QEMU;
 32
 33typedef struct FFGPUMemory {
 34    uint64_t total;
 35    uint64_t used;
 36} FFGPUMemory;
 37
 38typedef struct FFGPUPcieSpeed {
 39    uint16_t gen;
 40    uint16_t lanes;
 41} FFGPUPcieSpeed;
 42
 43typedef struct FFGPUResult {
 44    uint32_t index;
 45    FFGPUType type;
 46    FFstrbuf vendor;
 47    FFstrbuf name;
 48    FFstrbuf driver;
 49    FFstrbuf platformApi;
 50    FFstrbuf memoryType;
 51    double temperature;
 52    double coreUsage;
 53    int32_t coreCount;
 54    uint32_t frequency; // Maximum time clock frequency in MHz
 55    union {
 56        struct {
 57            FFGPUPcieSpeed psMax;
 58            FFGPUPcieSpeed psCurr;
 59        };
 60        uint64_t pcieSpeed;
 61    };
 62    FFGPUMemory dedicated;
 63    FFGPUMemory shared;
 64    uint64_t deviceId;
 65} FFGPUResult;
 66
 67static_assert(sizeof(((FFGPUResult*) nullptr)->pcieSpeed) == 8, "pcieSpeed is not 8 bytes");
 68static_assert(sizeof(((FFGPUResult*) nullptr)->psMax) == 4, "psMax has padding");
 69
 70const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result);
 71const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus);
 72
 73const char* ffGPUGetVendorString(unsigned vendorId);
 74
 75typedef struct FFGpuDriverPciBusId {
 76    uint32_t domain;
 77    uint32_t bus;
 78    uint32_t device;
 79    uint32_t func;
 80} FFGpuDriverPciBusId;
 81
 82#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__GNU__)
 83#define FF_GPU_HAS_PCI_IDS 1
 84void ffGPUParsePciIdsFile(const FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
 85void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
 86void ffGPUQueryAmdGpuName(uint16_t deviceId, uint8_t revisionId, FFGPUResult* gpu);
 87
 88    #if FF_HAVE_DRM || __has_include(<drm/drm.h>)
 89const char* ffDrmDetectRadeon(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath);
 90const char* ffDrmDetectAmdgpu(const FFGPUOptions* options, FFGPUResult* gpu, const char* renderPath);
 91const char* ffDrmDetectI915(FFGPUResult* gpu, int fd);
 92const char* ffDrmDetectXe(FFGPUResult* gpu, int fd);
 93const char* ffDrmDetectAsahi(FFGPUResult* gpu, int fd);
 94const char* ffDrmDetectNouveau(FFGPUResult* gpu, int fd);
 95        #if __FreeBSD__ || __OpenBSD__ // DRM is not available on NetBSD
 96const char* ffGPUDetectByDrmBSD(const FFGPUOptions* options, FFlist* gpus);
 97        #endif
 98    #endif // FF_HAVE_DRM || __has_include(<drm/drm.h>)
 99
100const char* ffGPUDetectDriverSpecific(const FFGPUOptions* options, FFGPUResult* gpu, FFGpuDriverPciBusId pciBusId);
101#endif // defined(XXX)
102
103static inline uint64_t ffGPUPciAddr2Id(uint64_t domain, uint64_t bus, uint64_t device, uint64_t function) {
104    return (domain << 16) | (bus << 8) | (device << 3) | function;
105}
106
107static inline uint64_t ffGPUGeneral2Id(uint64_t originalId) {
108    // Note: originalId may already have the MSB set
109    return (1ULL << 63) | originalId;
110}
111
112bool ffGPUDetectTypeByVendorAndName(FFGPUResult* gpu);