Skip to content

Commit 0414128

Browse files
committed
GPU (Linux): refactor code to support non-apple SOCs
1 parent 5dbd6b9 commit 0414128

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

src/detection/gpu/gpu_linux.c

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
257257
uint32_t vendorId, deviceId, subVendorId, subDeviceId;
258258
uint8_t classId, subclassId;
259259
if (sscanf(buffer->chars + strlen("pci:"), "v%8" SCNx32 "d%8" SCNx32 "sv%8" SCNx32 "sd%8" SCNx32 "bc%2" SCNx8 "sc%2" SCNx8, &vendorId, &deviceId, &subVendorId, &subDeviceId, &classId, &subclassId) != 6)
260-
return "Invalid modalias string";
260+
return "Failed to parse pci modalias";
261261

262262
if (classId != 0x03 /*PCI_BASE_CLASS_DISPLAY*/)
263263
return "Not a GPU device";
@@ -398,25 +398,11 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
398398

399399
#if __aarch64__
400400

401-
FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, const char* drmKey)
401+
FF_MAYBE_UNUSED static const char* drmDetectAsahiSpecific(FFGPUResult* gpu, const char* name, FFstrbuf* buffer, const char* drmKey)
402402
{
403-
uint32_t index = ffStrbufFirstIndexS(buffer, "apple,agx-t");
404-
if (index == buffer->length) return "display-subsystem?";
405-
index += (uint32_t) strlen("apple,agx-t");
406-
407-
FFGPUResult* gpu = (FFGPUResult*)ffListAdd(gpus);
408-
gpu->index = FF_GPU_INDEX_UNSET;
409-
gpu->deviceId = strtoul(buffer->chars + index, NULL, 10);
410-
ffStrbufInitStatic(&gpu->name, ffCPUAppleCodeToName((uint32_t) gpu->deviceId));
411-
ffStrbufInitStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_APPLE);
412-
ffStrbufInit(&gpu->driver);
413-
ffStrbufInitF(&gpu->platformApi, "DRM (%s)", drmKey);
414-
gpu->temperature = FF_GPU_TEMP_UNSET;
415-
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
416-
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
417-
gpu->type = FF_GPU_TYPE_INTEGRATED;
418-
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
419-
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
403+
if (sscanf(name, "agx-t%lu", &gpu->deviceId) == 1)
404+
ffStrbufSetStatic(&gpu->name, ffCPUAppleCodeToName((uint32_t) gpu->deviceId));
405+
ffStrbufSetStatic(&gpu->vendor, FF_GPU_VENDOR_NAME_APPLE);
420406

421407
#if FF_HAVE_DRM
422408
ffStrbufSetS(buffer, "/dev/dri/");
@@ -431,7 +417,7 @@ FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, F
431417
.size = sizeof(paramsGlobal),
432418
}) >= 0)
433419
{
434-
ffStrbufSetF(&gpu->driver, "asahi %u", paramsGlobal.unstable_uabi_version);
420+
ffStrbufAppendF(&gpu->driver, " %u", paramsGlobal.unstable_uabi_version);
435421

436422
// FIXME: They will introduce ABI breaking changes. Always check the latest version
437423
// https://www.reddit.com/r/AsahiLinux/comments/1ei2qiv/comment/lgm0v5s/
@@ -466,18 +452,56 @@ FF_MAYBE_UNUSED static const char* detectAsahi(FFlist* gpus, FFstrbuf* buffer, F
466452
}
467453
}
468454
}
455+
#endif
456+
457+
return NULL;
458+
}
459+
#endif
460+
461+
static const char* detectOf(FFlist* gpus, FFstrbuf* buffer, FFstrbuf* drmDir, const char* drmKey)
462+
{
463+
char compatible[256]; // vendor,model-name
464+
if (sscanf(buffer->chars + strlen("of:"), "NgpuT%*[^C]C%256[^C]", compatible) != 1)
465+
return "Failed to parse of modalias or not a GPU device";
469466

470-
if (!gpu->driver.length)
467+
char* name = strchr(compatible, ',');
468+
if (name)
471469
{
472-
pciDetectDriver(&gpu->driver, drmDir, buffer, drmKey);
473-
if (!gpu->name.length)
474-
ffStrbufSetF(&gpu->name, "Apple Silicon T%u", (uint32_t) gpu->deviceId);
470+
*name = '\0';
471+
++name;
475472
}
473+
474+
FFGPUResult* gpu = (FFGPUResult*)ffListAdd(gpus);
475+
gpu->index = FF_GPU_INDEX_UNSET;
476+
gpu->deviceId = 0;
477+
ffStrbufInit(&gpu->name);
478+
ffStrbufInit(&gpu->vendor);
479+
ffStrbufInit(&gpu->driver);
480+
ffStrbufInitF(&gpu->platformApi, "DRM (%s)", drmKey);
481+
gpu->temperature = FF_GPU_TEMP_UNSET;
482+
gpu->coreCount = FF_GPU_CORE_COUNT_UNSET;
483+
gpu->coreUsage = FF_GPU_CORE_USAGE_UNSET;
484+
gpu->type = FF_GPU_TYPE_INTEGRATED;
485+
gpu->dedicated.total = gpu->dedicated.used = gpu->shared.total = gpu->shared.used = FF_GPU_VMEM_SIZE_UNSET;
486+
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
487+
488+
pciDetectDriver(&gpu->driver, drmDir, buffer, drmKey);
489+
490+
#ifdef __aarch64__
491+
if (ffStrbufEqualS(&gpu->driver, "asahi"))
492+
drmDetectAsahiSpecific(gpu, name, buffer, drmKey);
476493
#endif
477494

495+
if (!gpu->name.length)
496+
ffStrbufSetS(&gpu->name, name ? name : compatible);
497+
if (!gpu->vendor.length && name)
498+
{
499+
compatible[0] = (char) toupper(compatible[0]);
500+
ffStrbufSetS(&gpu->vendor, compatible);
501+
}
502+
478503
return NULL;
479504
}
480-
#endif
481505

482506
static const char* drmDetectGPUs(const FFGPUOptions* options, FFlist* gpus)
483507
{
@@ -507,10 +531,8 @@ static const char* drmDetectGPUs(const FFGPUOptions* options, FFlist* gpus)
507531

508532
if (ffStrbufStartsWithS(&buffer, "pci:"))
509533
detectPci(options, gpus, &buffer, &drmDir, entry->d_name);
510-
#ifdef __aarch64__
511534
else if (ffStrbufStartsWithS(&buffer, "of:"))
512-
detectAsahi(gpus, &buffer, &drmDir, entry->d_name);
513-
#endif
535+
detectOf(gpus, &buffer, &drmDir, entry->d_name);
514536

515537
ffStrbufSubstrBefore(&drmDir, drmDirLength);
516538
}

0 commit comments

Comments
 (0)