From a3c0617f2215ae9b5be145ccde421ac2c8533117 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Mon, 23 Sep 2024 20:40:36 +0800 Subject: [PATCH] GPU: simplify integrated GPU detection --- src/detection/gpu/gpu.h | 2 +- src/detection/gpu/gpu_bsd.c | 13 +++------ src/detection/gpu/gpu_linux.c | 53 +++++++++++++++-------------------- src/detection/gpu/gpu_pci.c | 7 +---- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/detection/gpu/gpu.h b/src/detection/gpu/gpu.h index 85a51c27..e580d031 100644 --- a/src/detection/gpu/gpu.h +++ b/src/detection/gpu/gpu.h @@ -49,5 +49,5 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus); const char* ffGetGPUVendorString(unsigned vendorId); #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) -void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu, FFstrbuf* coreName); +void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu); #endif diff --git a/src/detection/gpu/gpu_bsd.c b/src/detection/gpu/gpu_bsd.c index 6d07dc02..8e8e8069 100644 --- a/src/detection/gpu/gpu_bsd.c +++ b/src/detection/gpu/gpu_bsd.c @@ -62,7 +62,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET; gpu->type = FF_GPU_TYPE_UNKNOWN; gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; - gpu->deviceId = ((uint64_t) pc->pc_sel.pc_domain << 6) | ((uint64_t) pc->pc_sel.pc_bus << 4) | ((uint64_t) pc->pc_sel.pc_dev << 2) | pc->pc_sel.pc_func; + 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; gpu->frequency = FF_GPU_FREQUENCY_UNSET; if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD) @@ -72,12 +72,11 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name); } - FF_STRBUF_AUTO_DESTROY coreName = ffStrbufCreate(); if (gpu->name.length == 0) { if (pciids.length == 0) loadPciIds(&pciids); - ffGPUParsePciIds(&pciids, pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu, &coreName); + ffGPUParsePciIds(&pciids, pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu); } if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_NVIDIA && (options->temp || options->driverSpecific)) @@ -118,12 +117,8 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) } else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_INTEL) { - if ((coreName.chars[0] == 'D' || coreName.chars[0] == 'S') && - coreName.chars[1] == 'G' && - ffCharIsDigit(coreName.chars[2])) - gpu->type = FF_GPU_TYPE_DISCRETE; // DG1 / DG2 / SG1 - else - gpu->type = FF_GPU_TYPE_INTEGRATED; + // 0000:00:02.0 is reserved for Intel integrated graphics + gpu->type = gpu->deviceId == 20 ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; } } } diff --git a/src/detection/gpu/gpu_linux.c b/src/detection/gpu/gpu_linux.c index 14bdaca9..d343de01 100644 --- a/src/detection/gpu/gpu_linux.c +++ b/src/detection/gpu/gpu_linux.c @@ -97,26 +97,6 @@ static const char* drmDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult uint32_t value; - if (ffamdgpu_query_sensor_info(handle, AMDGPU_INFO_SENSOR_VDDNB, sizeof(value), &value) >= 0 && value > 0) - gpu->type = FF_GPU_TYPE_DISCRETE; - else - gpu->type = FF_GPU_TYPE_INTEGRATED; - - struct amdgpu_heap_info heapInfo; - if (ffamdgpu_query_heap_info(handle, AMDGPU_GEM_DOMAIN_VRAM, 0, &heapInfo) >= 0) - { - if (gpu->type == FF_GPU_TYPE_DISCRETE) - { - gpu->dedicated.total = heapInfo.heap_size; - gpu->dedicated.used = heapInfo.heap_usage; - } - else - { - gpu->shared.total = heapInfo.heap_size; - gpu->shared.used = heapInfo.heap_usage; - } - } - if (options->temp) { if (ffamdgpu_query_sensor_info(handle, AMDGPU_INFO_SENSOR_GPU_TEMP, sizeof(value), &value) >= 0) @@ -131,6 +111,22 @@ static const char* drmDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult gpu->coreCount = (int32_t) gpuInfo.num_shader_engines; gpu->frequency = (uint32_t) (gpuInfo.max_engine_clk / 1000u); gpu->index = gpuInfo.asic_id; + gpu->type = gpuInfo.ids_flags & AMDGPU_IDS_FLAGS_FUSION ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; + + struct amdgpu_heap_info heapInfo; + if (ffamdgpu_query_heap_info(handle, AMDGPU_GEM_DOMAIN_VRAM, 0, &heapInfo) >= 0) + { + if (gpu->type == FF_GPU_TYPE_DISCRETE) + { + gpu->dedicated.total = heapInfo.heap_size; + gpu->dedicated.used = heapInfo.heap_usage; + } + else + { + gpu->shared.total = heapInfo.heap_size; + gpu->shared.used = heapInfo.heap_usage; + } + } } if (ffamdgpu_query_sensor_info(handle, AMDGPU_INFO_SENSOR_GPU_LOAD, sizeof(value), &value) >= 0) @@ -203,17 +199,13 @@ static void pciDetectAmdSpecific(const FFGPUOptions* options, FFGPUResult* gpu, } } -static void pciDetectIntelSpecific(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer, const FFstrbuf* coreName) +static void pciDetectIntelSpecific(FFGPUResult* gpu, FFstrbuf* pciDir, FFstrbuf* buffer) { // Works for Intel GPUs // https://patchwork.kernel.org/project/intel-gfx/patch/1422039866-11572-3-git-send-email-ville.syrjala@linux.intel.com/ - if ((coreName->chars[0] == 'D' || coreName->chars[0] == 'S') && - coreName->chars[1] == 'G' && - ffCharIsDigit(coreName->chars[2])) - gpu->type = FF_GPU_TYPE_DISCRETE; // DG1 / DG2 / SG1 - else - gpu->type = FF_GPU_TYPE_INTEGRATED; + // 0000:00:02.0 is reserved for Intel integrated graphics + gpu->type = gpu->deviceId == 20 ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE; if (ffStrbufEqualS(&gpu->driver, "xe")) { @@ -305,12 +297,11 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf gpu->coreCount = FF_GPU_CORE_COUNT_UNSET; gpu->type = FF_GPU_TYPE_UNKNOWN; gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET; - gpu->deviceId = ((uint64_t) pciDomain << 6) | ((uint64_t) pciBus << 4) | (deviceId << 2) | pciFunc; + gpu->deviceId = (pciDomain * 100000ull) + (pciBus * 1000ull) + (pciDevice * 10ull) + pciFunc; gpu->frequency = FF_GPU_FREQUENCY_UNSET; if (drmKey) ffStrbufSetF(&gpu->platformApi, "DRM (%s)", drmKey); - FF_STRBUF_AUTO_DESTROY coreName = ffStrbufCreate(); if (gpu->name.length == 0) { static FFstrbuf pciids; @@ -319,7 +310,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf ffStrbufInit(&pciids); loadPciIds(&pciids); } - ffGPUParsePciIds(&pciids, subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu, &coreName); + ffGPUParsePciIds(&pciids, subclassId, (uint16_t) vendorId, (uint16_t) deviceId, gpu); } pciDetectDriver(&gpu->driver, deviceDir, buffer, drmKey); @@ -357,7 +348,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf } else if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_INTEL) { - pciDetectIntelSpecific(gpu, deviceDir, buffer, &coreName); + pciDetectIntelSpecific(gpu, deviceDir, buffer); ffStrbufSubstrBefore(deviceDir, drmDirPathLength); } else diff --git a/src/detection/gpu/gpu_pci.c b/src/detection/gpu/gpu_pci.c index de81f9b6..990e669f 100644 --- a/src/detection/gpu/gpu_pci.c +++ b/src/detection/gpu/gpu_pci.c @@ -1,6 +1,6 @@ #include "gpu.h" -void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu, FFstrbuf* coreName) +void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu) { if (content->length) { @@ -52,11 +52,6 @@ void ffGPUParsePciIds(FFstrbuf* content, uint8_t subclass, uint16_t vendor, uint { openingBracket++; ffStrbufSetNS(&gpu->name, (uint32_t) (closingBracket - openingBracket), openingBracket); - if (coreName) - { - ffStrbufSetNS(coreName, (uint32_t) (openingBracket - start) - 1, start); - ffStrbufTrimRight(coreName, ' '); - } } } if (!gpu->name.length)