From 28fa43454145be2843432eb12be2ec30ca51545c Mon Sep 17 00:00:00 2001 From: "Elaine, Wang" Date: Mon, 22 Jul 2024 10:31:51 +0000 Subject: [PATCH] feature: support pci path contains card0 Some OpenCL application need to render libva surface with drm display and use the same libva surface as OpenCL kernel output. Such applicatoins need to use /dev/dri/card0 instead of /dev/dri/renderD128 for getting the display fd. Related-To: NEO-11714 Signed-off-by: Elaine, Wang --- shared/source/os_interface/linux/pci_path.cpp | 4 +++ .../os_interface/linux/pci_path_tests.cpp | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/shared/source/os_interface/linux/pci_path.cpp b/shared/source/os_interface/linux/pci_path.cpp index 7247c71cc5..365cfd5777 100644 --- a/shared/source/os_interface/linux/pci_path.cpp +++ b/shared/source/os_interface/linux/pci_path.cpp @@ -48,6 +48,10 @@ std::optional getPciPath(int deviceFd) { auto pciPathPos = deviceLinkPath->find("/drm/render"); + if (pciPathPos == std::string::npos) { + pciPathPos = deviceLinkPath->find("/drm/card"); + } + if (pciPathPos == std::string::npos || pciPathPos < 12) { return std::nullopt; } diff --git a/shared/test/unit_test/os_interface/linux/pci_path_tests.cpp b/shared/test/unit_test/os_interface/linux/pci_path_tests.cpp index cd3bab749f..98fbf352e9 100644 --- a/shared/test/unit_test/os_interface/linux/pci_path_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/pci_path_tests.cpp @@ -81,3 +81,39 @@ TEST(DrmTest, whenGettingPciPathThenProperPathIsReturned) { EXPECT_TRUE(pciPath.has_value()); EXPECT_EQ(outPciPath, *pciPath); } + +TEST(DrmTest, whenGettingPciPathContainsCard0ThenProperPathIsReturned) { + static const int fileDescriptorCard = 0x127; + + static const char devicePathCard[] = "devicePathCard"; + + static const char linkPathCard[] = "../../devices/pci0000:4a/0000:4b:00.0/0000:4c:01.0/0001:02:03.4/drm/card0"; + + static const char outPciPath[] = "0001:02:03.4"; + + VariableBackup allowFakeDevicePathBackup{&SysCalls::allowFakeDevicePath, true}; + VariableBackup getDevicePathBackup(&SysCalls::sysCallsGetDevicePath); + VariableBackup readlinkBackup(&SysCalls::sysCallsReadlink); + + SysCalls::sysCallsGetDevicePath = [](int deviceFd, char *buf, size_t &bufSize) -> int { + if (deviceFd == fileDescriptorCard) { + bufSize = sizeof(devicePathCard); + strcpy_s(buf, bufSize, devicePathCard); + return 0; + } + return -1; + }; + + SysCalls::sysCallsReadlink = [](const char *path, char *buf, size_t bufSize) -> int { + if (strcmp(path, devicePathCard) == 0) { + bufSize = sizeof(linkPathCard); + strcpy_s(buf, bufSize, linkPathCard); + return static_cast(bufSize); + } + return -1; + }; + + auto pciPath = getPciPath(fileDescriptorCard); + EXPECT_TRUE(pciPath.has_value()); + EXPECT_EQ(outPciPath, *pciPath); +}