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 813c5aaec6..dfff0692fb 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 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -7,6 +7,8 @@ #include "level_zero/tools/source/sysman/pci/linux/os_pci_imp.h" +#include "shared/source/utilities/directory.h" + #include "level_zero/tools/source/sysman/linux/fs_access.h" #include "level_zero/tools/source/sysman/sysman_const.h" @@ -27,14 +29,21 @@ ze_result_t LinuxPciImp::getProperties(zes_pci_properties_t *properties) { properties->haveReplayCounters = false; return ZE_RESULT_SUCCESS; } -ze_result_t LinuxPciImp::getPciBdf(std::string &bdf) { +ze_result_t LinuxPciImp::getPciBdf(zes_pci_properties_t &pciProperties) { std::string bdfDir; ze_result_t result = pSysfsAccess->readSymLink(deviceDir, bdfDir); if (ZE_RESULT_SUCCESS != result) { return result; } const auto loc = bdfDir.find_last_of('/'); - bdf = bdfDir.substr(loc + 1); + std::string bdf = bdfDir.substr(loc + 1); + uint16_t domain = 0; + uint8_t bus = 0, device = 0, function = 0; + NEO::parseBdfString(bdf.c_str(), domain, bus, device, function); + pciProperties.address.domain = static_cast(domain); + pciProperties.address.bus = static_cast(bus); + pciProperties.address.device = static_cast(device); + pciProperties.address.function = static_cast(function); return ZE_RESULT_SUCCESS; } diff --git a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.h b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.h index d07cbc7c48..1653a045a8 100644 --- a/level_zero/tools/source/sysman/pci/linux/os_pci_imp.h +++ b/level_zero/tools/source/sysman/pci/linux/os_pci_imp.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -19,7 +19,7 @@ class FsAccess; class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass { public: - ze_result_t getPciBdf(std::string &bdf) override; + ze_result_t getPciBdf(zes_pci_properties_t &pciProperties) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; ze_result_t getState(zes_pci_state_t *state) override; diff --git a/level_zero/tools/source/sysman/pci/os_pci.h b/level_zero/tools/source/sysman/pci/os_pci.h index 2da1c8c08c..1740619674 100644 --- a/level_zero/tools/source/sysman/pci/os_pci.h +++ b/level_zero/tools/source/sysman/pci/os_pci.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -19,7 +19,7 @@ int32_t convertLinkSpeedToPciGen(double speed); double convertPciGenToLinkSpeed(uint32_t gen); class OsPci { public: - virtual ze_result_t getPciBdf(std::string &bdf) = 0; + virtual ze_result_t getPciBdf(zes_pci_properties_t &pciProperties) = 0; virtual ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) = 0; virtual ze_result_t getMaxLinkWidth(int32_t &maxLinkWidth) = 0; virtual ze_result_t getState(zes_pci_state_t *state) = 0; diff --git a/level_zero/tools/source/sysman/pci/pci_imp.cpp b/level_zero/tools/source/sysman/pci/pci_imp.cpp index c139632a77..90e03aae96 100644 --- a/level_zero/tools/source/sysman/pci/pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/pci_imp.cpp @@ -125,22 +125,7 @@ void PciImp::pciGetStaticFields() { pOsPci->getProperties(&pciProperties); resizableBarSupported = pOsPci->resizableBarSupported(); std::string bdf; - pOsPci->getPciBdf(bdf); - if (bdf.empty()) { - pciProperties.address.domain = 0; - pciProperties.address.bus = 0; - pciProperties.address.device = 0; - pciProperties.address.function = 0; - } else { - uint16_t domain = -1; - uint8_t bus = -1, device = -1, function = -1; - NEO::parseBdfString(bdf.c_str(), domain, bus, device, function); - pciProperties.address.domain = static_cast(domain); - pciProperties.address.bus = static_cast(bus); - pciProperties.address.device = static_cast(device); - pciProperties.address.function = static_cast(function); - } - + pOsPci->getPciBdf(pciProperties); int32_t maxLinkWidth = -1; int64_t maxBandWidth = -1; double maxLinkSpeed = 0; diff --git a/level_zero/tools/source/sysman/pci/windows/os_pci_imp.cpp b/level_zero/tools/source/sysman/pci/windows/os_pci_imp.cpp index 95cc77fd58..480fcca1db 100644 --- a/level_zero/tools/source/sysman/pci/windows/os_pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/windows/os_pci_imp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -16,7 +16,7 @@ ze_result_t WddmPciImp::getProperties(zes_pci_properties_t *properties) { return ZE_RESULT_SUCCESS; } -ze_result_t WddmPciImp::getPciBdf(std::string &bdf) { +ze_result_t WddmPciImp::getPciBdf(zes_pci_properties_t &pciProperties) { uint32_t valueSmall = 0; uint32_t domain = 0, bus = 0, dev = 0, func = 0; std::vector vRequests = {}; @@ -65,7 +65,10 @@ ze_result_t WddmPciImp::getPciBdf(std::string &bdf) { func = valueSmall; } - bdf = std::to_string(domain) + std::string(":") + std::to_string(bus) + std::string(":") + std::to_string(dev) + std::string(".") + std::to_string(func); + pciProperties.address.domain = domain; + pciProperties.address.bus = bus; + pciProperties.address.device = dev; + pciProperties.address.function = func; return ZE_RESULT_SUCCESS; } diff --git a/level_zero/tools/source/sysman/pci/windows/os_pci_imp.h b/level_zero/tools/source/sysman/pci/windows/os_pci_imp.h index 6aef8207b7..18c27a513e 100644 --- a/level_zero/tools/source/sysman/pci/windows/os_pci_imp.h +++ b/level_zero/tools/source/sysman/pci/windows/os_pci_imp.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -15,7 +15,7 @@ namespace L0 { class KmdSysManager; class WddmPciImp : public OsPci, NEO::NonCopyableOrMovableClass { public: - ze_result_t getPciBdf(std::string &bdf) override; + ze_result_t getPciBdf(zes_pci_properties_t &pciProperties) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; ze_result_t getState(zes_pci_state_t *state) override; 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 dc71e3d80f..57d89dc558 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 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -114,6 +114,14 @@ struct Mock : public PciSysfsAccess { return ZE_RESULT_ERROR_NOT_AVAILABLE; } + ze_result_t getValStringSymLinkEmpty(const std::string file, std::string &val) { + if (file.compare(deviceDir) == 0) { + val = "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/0000:02:01.0/"; + return ZE_RESULT_SUCCESS; + } + return ZE_RESULT_ERROR_NOT_AVAILABLE; + } + ze_result_t getValStringSymLink(const std::string file, std::string &val) { if (file.compare(deviceDir) == 0) { val = mockBdf; @@ -121,6 +129,7 @@ struct Mock : public PciSysfsAccess { } return ZE_RESULT_ERROR_NOT_AVAILABLE; } + ze_result_t getValStringRealPath(const std::string file, std::string &val) { if (file.compare(deviceDir) == 0) { val = mockRealPath; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp index acdadb2441..354e6e1269 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -274,6 +274,20 @@ TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingzetS EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth); } +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesAndBdfStringIsEmptyThenVerifyApiCallSucceeds) { + zes_pci_properties_t properties; + ON_CALL(*pSysfsAccess.get(), readSymLink(_, _)) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValStringSymLinkEmpty)); + pPciImp->init(); + + ze_result_t result = zesDevicePciGetProperties(device, &properties); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(properties.address.bus, 0u); + EXPECT_EQ(properties.address.device, 0u); + EXPECT_EQ(properties.address.function, 0u); +} + TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) { int32_t width = 0; pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid);