diff --git a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp index 804a1807bb..baa6ae7c00 100644 --- a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.cpp @@ -19,6 +19,8 @@ const std::string LinuxPciImp::resourceFile("device/resource"); const std::string LinuxPciImp::maxLinkSpeedFile("device/max_link_speed"); const std::string LinuxPciImp::maxLinkWidthFile("device/max_link_width"); constexpr uint8_t maxPciBars = 6; +// Linux kernel would report 255 link width, as an indication of unknown. +constexpr uint32_t unknownPcieLinkWidth = 255u; ze_result_t LinuxPciImp::getPciBdf(std::string &bdf) { std::string bdfDir; @@ -50,6 +52,9 @@ ze_result_t LinuxPciImp::getMaxLinkWidth(uint32_t &maxLinkwidth) { if (ZE_RESULT_SUCCESS != result) { return result; } + if (intVal == static_cast(unknownPcieLinkWidth)) { + intVal = 0; + } maxLinkwidth = intVal; return ZE_RESULT_SUCCESS; } diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/mock_sysfs_pci.h b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/mock_sysfs_pci.h index 60f2b41ecf..fc39d18856 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/mock_sysfs_pci.h +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/mock_sysfs_pci.h @@ -24,7 +24,6 @@ const std::string maxLinkSpeedFile("device/max_link_speed"); const std::string maxLinkWidthFile("device/max_link_width"); const std::string mockBdf = "0000:00:02.0"; constexpr double mockMaxLinkSpeed = 2.5; -constexpr int mockMaxLinkWidth = 1; const std::vector mockReadBytes = { "0x00000000bf000000 0x00000000bfffffff 0x0000000000140204", @@ -46,6 +45,7 @@ class PciSysfsAccess : public SysfsAccess {}; template <> struct Mock : public SysfsAccess { + int mockMaxLinkWidth = 0; MOCK_METHOD(ze_result_t, read, (const std::string file, double &val), (override)); MOCK_METHOD(ze_result_t, read, (const std::string file, int &val), (override)); MOCK_METHOD(ze_result_t, read, (const std::string file, std::vector &val), (override)); @@ -58,6 +58,13 @@ struct Mock : public SysfsAccess { return ZE_RESULT_SUCCESS; } + ze_result_t setValInt(const std::string file, int val) { + if (file.compare(maxLinkWidthFile) == 0) { + mockMaxLinkWidth = val; + } + return ZE_RESULT_SUCCESS; + } + ze_result_t getValInt(const std::string file, int &val) { if (file.compare(maxLinkWidthFile) == 0) { val = mockMaxLinkWidth; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_sysman_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_sysman_pci.cpp index 83eff5d5fa..93665d4aef 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_sysman_pci.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_sysman_pci.cpp @@ -24,7 +24,15 @@ using ::testing::Return; namespace L0 { namespace ult { - +constexpr int mockMaxLinkWidth = 1; +constexpr int mockMaxLinkWidthInvalid = 255; +constexpr uint32_t expectedBus = 0u; +constexpr uint32_t expectedDevice = 2u; +constexpr uint32_t expectedFunction = 0u; +constexpr uint32_t expectedWidth = 1u; +constexpr uint32_t expectedGen = 1u; // As mockMaxLinkSpeed = 2.5, hence expectedGen should be 1 +// As mockMaxLinkSpeed = 2.5, hence, pcieSpeedWithEnc = mockMaxLinkWidth * (2.5 * 1000 * 8/10 * 125000) = 250000000 +constexpr uint64_t expectedBandwidth = 250000000u; class SysmanPciFixture : public DeviceFixture, public ::testing::Test { protected: @@ -43,6 +51,7 @@ class SysmanPciFixture : public DeviceFixture, public ::testing::Test { pSysfsAccess = new NiceMock>; linuxPciImp.pSysfsAccess = pSysfsAccess; pOsPci = static_cast(&linuxPciImp); + pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth); ON_CALL(*pSysfsAccess, read(_, Matcher &>(_))) .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValVector)); @@ -78,12 +87,22 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropert ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties); EXPECT_EQ(ZE_RESULT_SUCCESS, result); - EXPECT_LT(properties.address.bus, 256u); - EXPECT_LT(properties.address.device, 32u); - EXPECT_LT(properties.address.function, 8u); - EXPECT_LE(properties.maxSpeed.gen, 5u); - EXPECT_LE(properties.maxSpeed.width, 32u); - EXPECT_LE(properties.maxSpeed.maxBandwidth, std::numeric_limits::max()); + EXPECT_EQ(properties.address.bus, expectedBus); + EXPECT_EQ(properties.address.device, expectedDevice); + EXPECT_EQ(properties.address.function, expectedFunction); + EXPECT_EQ(properties.maxSpeed.gen, expectedGen); + EXPECT_EQ(properties.maxSpeed.width, expectedWidth); + EXPECT_EQ(properties.maxSpeed.maxBandwidth, expectedBandwidth); +} + +TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) { + uint32_t width = 0; + pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid); + ON_CALL(*pSysfsAccess, read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValInt)); + + EXPECT_EQ(ZE_RESULT_SUCCESS, pciImp.pOsPci->getMaxLinkWidth(width)); + EXPECT_EQ(width, 0u); } TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) {