From c7037aca4d8c29972f6ea25a18bf83aa8de71399 Mon Sep 17 00:00:00 2001 From: Daniel Enriquez Date: Fri, 14 Aug 2020 01:19:58 -0700 Subject: [PATCH] Enabling Windows Pci component. Change-Id: Idfd1acb8f257ab91dd28707f0b42b3da3981214e --- .../source/sysman/pci/linux/os_pci_imp.cpp | 32 +--- .../source/sysman/pci/linux/os_pci_imp.h | 4 +- level_zero/tools/source/sysman/pci/os_pci.h | 7 +- level_zero/tools/source/sysman/pci/pci.h | 17 ++ .../tools/source/sysman/pci/pci_imp.cpp | 52 +++++- level_zero/tools/source/sysman/pci/pci_imp.h | 1 + .../source/sysman/pci/windows/CMakeLists.txt | 1 + .../source/sysman/pci/windows/os_pci_imp.cpp | 176 ++++++++++++++++-- .../source/sysman/pci/windows/os_pci_imp.h | 35 ++++ level_zero/tools/source/sysman/sysman_imp.cpp | 2 +- .../sources/sysman/pci/linux/mock_sysfs_pci.h | 1 + .../sources/sysman/pci/linux/test_zes_pci.cpp | 20 +- .../sources/sysman/pci/windows/CMakeLists.txt | 14 ++ .../sources/sysman/pci/windows/mock_pci.h | 111 +++++++++++ .../sysman/pci/windows/test_zes_pci.cpp | 147 +++++++++++++++ 15 files changed, 570 insertions(+), 50 deletions(-) create mode 100644 level_zero/tools/source/sysman/pci/windows/os_pci_imp.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/pci/windows/CMakeLists.txt create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/pci/windows/mock_pci.h create mode 100644 level_zero/tools/test/unit_tests/sources/sysman/pci/windows/test_zes_pci.cpp 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 72162ea50a..55553c72e5 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 @@ -22,7 +22,7 @@ constexpr uint8_t maxPciBars = 6; // Linux kernel would report 255 link width, as an indication of unknown. constexpr uint32_t unknownPcieLinkWidth = 255u; -std::string changeDirNLevelsUp(std::string realRootPath, uint8_t nLevel) { +std::string LinuxPciImp::changeDirNLevelsUp(std::string realRootPath, uint8_t nLevel) { size_t loc; while (nLevel > 0) { loc = realRootPath.find_last_of('/'); @@ -31,7 +31,12 @@ std::string changeDirNLevelsUp(std::string realRootPath, uint8_t nLevel) { } return realRootPath; } - +ze_result_t LinuxPciImp::getProperties(zes_pci_properties_t *properties) { + properties->haveBandwidthCounters = false; + properties->havePacketCounters = false; + properties->haveReplayCounters = false; + return ZE_RESULT_SUCCESS; +} ze_result_t LinuxPciImp::getPciBdf(std::string &bdf) { std::string bdfDir; ze_result_t result = pSysfsAccess->readSymLink(deviceDir, bdfDir); @@ -106,26 +111,6 @@ ze_result_t LinuxPciImp::getMaxLinkWidth(int32_t &maxLinkwidth) { return ZE_RESULT_SUCCESS; } -ze_result_t LinuxPciImp::getLinkGen(int32_t &linkGen) { - double maxLinkSpeed; - getMaxLinkSpeed(maxLinkSpeed); - if (maxLinkSpeed == 2.5) { - linkGen = 1; - } else if (maxLinkSpeed == 5) { - linkGen = 2; - } else if (maxLinkSpeed == 8) { - linkGen = 3; - } else if (maxLinkSpeed == 16) { - linkGen = 4; - } else if (maxLinkSpeed == 32) { - linkGen = 5; - } else { - linkGen = -1; - } - - return ZE_RESULT_SUCCESS; -} - void getBarBaseAndSize(std::string readBytes, uint64_t &baseAddr, uint64_t &barSize, uint64_t &barFlags) { unsigned long long start, end, flags; @@ -176,6 +161,9 @@ ze_result_t LinuxPciImp::initializeBarProperties(std::vector(pOsSysman); pSysfsAccess = &pLinuxSysmanImp->getSysfsAccess(); 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 76002fe2e6..24e5e76450 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 @@ -20,7 +20,8 @@ class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass { ze_result_t getPciBdf(std::string &bdf) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; - ze_result_t getLinkGen(int32_t &linkGen) override; + ze_result_t getState(zes_pci_state_t *state) override; + ze_result_t getProperties(zes_pci_properties_t *properties) override; ze_result_t initializeBarProperties(std::vector &pBarProperties) override; LinuxPciImp() = default; LinuxPciImp(OsSysman *pOsSysman); @@ -29,6 +30,7 @@ class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass { protected: SysfsAccess *pSysfsAccess = nullptr; FsAccess *pfsAccess = nullptr; + std::string changeDirNLevelsUp(std::string realRootPath, uint8_t nLevel); private: static const std::string deviceDir; diff --git a/level_zero/tools/source/sysman/pci/os_pci.h b/level_zero/tools/source/sysman/pci/os_pci.h index 65929b481d..a7f4aab0ba 100644 --- a/level_zero/tools/source/sysman/pci/os_pci.h +++ b/level_zero/tools/source/sysman/pci/os_pci.h @@ -14,14 +14,15 @@ #include namespace L0 { -std::string changeDirNLevelsUp(std::string realRootPath, uint8_t nLevel); - +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 getMaxLinkSpeed(double &maxLinkSpeed) = 0; virtual ze_result_t getMaxLinkWidth(int32_t &maxLinkWidth) = 0; - virtual ze_result_t getLinkGen(int32_t &linkGen) = 0; + virtual ze_result_t getState(zes_pci_state_t *state) = 0; + virtual ze_result_t getProperties(zes_pci_properties_t *properties) = 0; virtual ze_result_t initializeBarProperties(std::vector &pBarProperties) = 0; static OsPci *create(OsSysman *pOsSysman); virtual ~OsPci() = default; diff --git a/level_zero/tools/source/sysman/pci/pci.h b/level_zero/tools/source/sysman/pci/pci.h index c60e7ea58b..db84ed246a 100644 --- a/level_zero/tools/source/sysman/pci/pci.h +++ b/level_zero/tools/source/sysman/pci/pci.h @@ -10,11 +10,28 @@ namespace L0 { +namespace PciLinkSpeeds { +constexpr double Pci2_5GigatransfersPerSecond = 2.5; +constexpr double Pci5_0GigatransfersPerSecond = 5.0; +constexpr double Pci8_0GigatransfersPerSecond = 8.0; +constexpr double Pci16_0GigatransfersPerSecond = 16.0; +constexpr double Pci32_0GigatransfersPerSecond = 32.0; +} // namespace PciLinkSpeeds + +enum PciGenerations { + PciGen1 = 1, + PciGen2, + PciGen3, + PciGen4, + PciGen5, +}; + class Pci { public: virtual ~Pci(){}; virtual ze_result_t pciStaticProperties(zes_pci_properties_t *pProperties) = 0; virtual ze_result_t pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) = 0; + virtual ze_result_t pciGetState(zes_pci_state_t *pState) = 0; virtual void init() = 0; }; diff --git a/level_zero/tools/source/sysman/pci/pci_imp.cpp b/level_zero/tools/source/sysman/pci/pci_imp.cpp index 38ba05226d..6ba62a0dcb 100644 --- a/level_zero/tools/source/sysman/pci/pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/pci_imp.cpp @@ -25,9 +25,9 @@ namespace L0 { // int64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { double pcieSpeedWithEnc; - if ((maxLinkSpeedInGt == 16) || (maxLinkSpeedInGt == 8)) { + if ((maxLinkSpeedInGt == PciLinkSpeeds::Pci16_0GigatransfersPerSecond) || (maxLinkSpeedInGt == PciLinkSpeeds::Pci8_0GigatransfersPerSecond)) { pcieSpeedWithEnc = maxLinkSpeedInGt * 1000 * 128 / 130; - } else if ((maxLinkSpeedInGt == 5) || (maxLinkSpeedInGt == 2.5)) { + } else if ((maxLinkSpeedInGt == PciLinkSpeeds::Pci5_0GigatransfersPerSecond) || (maxLinkSpeedInGt == PciLinkSpeeds::Pci2_5GigatransfersPerSecond)) { pcieSpeedWithEnc = maxLinkSpeedInGt * 1000 * 8 / 10; } else { pcieSpeedWithEnc = 0; @@ -42,6 +42,45 @@ int64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { return static_cast(pcieSpeedWithEnc); } +double convertPciGenToLinkSpeed(uint32_t gen) { + switch (gen) { + case PciGenerations::PciGen1: { + return PciLinkSpeeds::Pci2_5GigatransfersPerSecond; + } break; + case PciGenerations::PciGen2: { + return PciLinkSpeeds::Pci5_0GigatransfersPerSecond; + } break; + case PciGenerations::PciGen3: { + return PciLinkSpeeds::Pci8_0GigatransfersPerSecond; + } break; + case PciGenerations::PciGen4: { + return PciLinkSpeeds::Pci16_0GigatransfersPerSecond; + } break; + case PciGenerations::PciGen5: { + return PciLinkSpeeds::Pci32_0GigatransfersPerSecond; + } break; + default: { + return 0.0; + } break; + } +} + +int32_t convertLinkSpeedToPciGen(double speed) { + if (speed == PciLinkSpeeds::Pci2_5GigatransfersPerSecond) { + return PciGenerations::PciGen1; + } else if (speed == PciLinkSpeeds::Pci5_0GigatransfersPerSecond) { + return PciGenerations::PciGen2; + } else if (speed == PciLinkSpeeds::Pci8_0GigatransfersPerSecond) { + return PciGenerations::PciGen3; + } else if (speed == PciLinkSpeeds::Pci16_0GigatransfersPerSecond) { + return PciGenerations::PciGen4; + } else if (speed == PciLinkSpeeds::Pci32_0GigatransfersPerSecond) { + return PciGenerations::PciGen5; + } else { + return -1; + } +} + ze_result_t PciImp::pciStaticProperties(zes_pci_properties_t *pProperties) { *pProperties = pciProperties; return ZE_RESULT_SUCCESS; @@ -61,11 +100,15 @@ ze_result_t PciImp::pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properti return ZE_RESULT_SUCCESS; } +ze_result_t PciImp::pciGetState(zes_pci_state_t *pState) { + return pOsPci->getState(pState); +} void PciImp::init() { if (pOsPci == nullptr) { pOsPci = OsPci::create(pOsSysman); } UNRECOVERABLE_IF(nullptr == pOsPci); + pOsPci->getProperties(&pciProperties); std::string bdf; pOsPci->getPciBdf(bdf); if (bdf.empty()) { @@ -79,7 +122,7 @@ void PciImp::init() { &pciProperties.address.device, &pciProperties.address.function); } - int32_t maxLinkWidth = -1, gen = -1; + int32_t maxLinkWidth = -1; int64_t maxBandWidth = -1; double maxLinkSpeed = 0; pOsPci->getMaxLinkSpeed(maxLinkSpeed); @@ -91,8 +134,7 @@ void PciImp::init() { pciProperties.maxSpeed.maxBandwidth = maxBandWidth; } pciProperties.maxSpeed.width = maxLinkWidth; - pOsPci->getLinkGen(gen); - pciProperties.maxSpeed.gen = gen; + pciProperties.maxSpeed.gen = convertLinkSpeedToPciGen(maxLinkSpeed); pOsPci->initializeBarProperties(pciBarProperties); } diff --git a/level_zero/tools/source/sysman/pci/pci_imp.h b/level_zero/tools/source/sysman/pci/pci_imp.h index 251ce5ca02..408441628d 100644 --- a/level_zero/tools/source/sysman/pci/pci_imp.h +++ b/level_zero/tools/source/sysman/pci/pci_imp.h @@ -24,6 +24,7 @@ class PciImp : public Pci, NEO::NonCopyableOrMovableClass { void init() override; ze_result_t pciStaticProperties(zes_pci_properties_t *pProperties) override; ze_result_t pciGetInitializedBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) override; + ze_result_t pciGetState(zes_pci_state_t *pState) override; PciImp() = default; PciImp(OsSysman *pOsSysman) : pOsSysman(pOsSysman){}; diff --git a/level_zero/tools/source/sysman/pci/windows/CMakeLists.txt b/level_zero/tools/source/sysman/pci/windows/CMakeLists.txt index d7e8d09897..e4e66e4146 100644 --- a/level_zero/tools/source/sysman/pci/windows/CMakeLists.txt +++ b/level_zero/tools/source/sysman/pci/windows/CMakeLists.txt @@ -7,6 +7,7 @@ set(L0_SRCS_TOOLS_SYSMAN_PCI_WINDOWS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ${CMAKE_CURRENT_SOURCE_DIR}/os_pci_imp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_pci_imp.h ) if(WIN32) 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 51b1605efd..661cad1a44 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 @@ -5,43 +5,185 @@ * */ -#include "level_zero/tools/source/sysman/pci/os_pci.h" -#include "level_zero/tools/source/sysman/windows/os_sysman_imp.h" +#include "sysman/pci/windows/os_pci_imp.h" namespace L0 { -class WddmPciImp : public OsPci { - public: - ze_result_t getPciBdf(std::string &bdf) override; - ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; - ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; - ze_result_t getLinkGen(int32_t &linkGen) override; - ze_result_t initializeBarProperties(std::vector &pBarProperties) override; - ~WddmPciImp() override = default; -}; +ze_result_t WddmPciImp::getProperties(zes_pci_properties_t *properties) { + properties->haveBandwidthCounters = false; + properties->havePacketCounters = false; + properties->haveReplayCounters = false; + return ZE_RESULT_SUCCESS; +} ze_result_t WddmPciImp::getPciBdf(std::string &bdf) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + uint32_t valueSmall = 0; + uint32_t domain = 0, bus = 0, dev = 0, func = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::PciComponent; + request.requestId = KmdSysman::Requests::Pci::Bus; + + if (isLmemSupported) { + request.paramInfo = KmdSysman::PciDomainsType::PciRootPort; + } else { + request.paramInfo = KmdSysman::PciDomainsType::PciCurrentDevice; + } + + ze_result_t status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + bus = valueSmall; + + request.requestId = KmdSysman::Requests::Pci::Domain; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + domain = valueSmall; + + request.requestId = KmdSysman::Requests::Pci::Device; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + dev = valueSmall; + + request.requestId = KmdSysman::Requests::Pci::Function; + + status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + 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); + + return status; } ze_result_t WddmPciImp::getMaxLinkSpeed(double &maxLinkSpeed) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + uint32_t valueSmall = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::PciComponent; + request.requestId = KmdSysman::Requests::Pci::MaxLinkSpeed; + + if (isLmemSupported) { + request.paramInfo = KmdSysman::PciDomainsType::PciRootPort; + } else { + request.paramInfo = KmdSysman::PciDomainsType::PciCurrentDevice; + } + + ze_result_t status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + maxLinkSpeed = convertPciGenToLinkSpeed(valueSmall); + + return status; } ze_result_t WddmPciImp::getMaxLinkWidth(int32_t &maxLinkwidth) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + uint32_t valueSmall = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::PciComponent; + request.requestId = KmdSysman::Requests::Pci::MaxLinkWidth; + + if (isLmemSupported) { + request.paramInfo = KmdSysman::PciDomainsType::PciRootPort; + } else { + request.paramInfo = KmdSysman::PciDomainsType::PciCurrentDevice; + } + + ze_result_t status = pKmdSysManager->requestSingle(request, response); + + if (status != ZE_RESULT_SUCCESS) { + return status; + } + + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + maxLinkwidth = static_cast(valueSmall); + + return status; } -ze_result_t WddmPciImp::getLinkGen(int32_t &linkGen) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +ze_result_t WddmPciImp::getState(zes_pci_state_t *state) { + uint32_t valueSmall = 0; + KmdSysman::RequestProperty request; + KmdSysman::ResponseProperty response; + + state->qualityIssues = ZES_PCI_LINK_QUAL_ISSUE_FLAG_FORCE_UINT32; + state->stabilityIssues = ZES_PCI_LINK_STAB_ISSUE_FLAG_FORCE_UINT32; + state->status = ZES_PCI_LINK_STATUS_FORCE_UINT32; + + request.commandId = KmdSysman::Command::Get; + request.componentId = KmdSysman::Component::PciComponent; + request.requestId = KmdSysman::Requests::Pci::CurrentLinkSpeed; + + if (isLmemSupported) { + request.paramInfo = KmdSysman::PciDomainsType::PciRootPort; + } else { + request.paramInfo = KmdSysman::PciDomainsType::PciCurrentDevice; + } + + ze_result_t status = pKmdSysManager->requestSingle(request, response); + + if (status == ZE_RESULT_SUCCESS) { + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + state->speed.gen = static_cast(valueSmall); + } + + request.requestId = KmdSysman::Requests::Pci::CurrentLinkWidth; + + status = pKmdSysManager->requestSingle(request, response); + + if (status == ZE_RESULT_SUCCESS) { + memcpy_s(&valueSmall, sizeof(uint32_t), response.dataBuffer, sizeof(uint32_t)); + state->speed.width = static_cast(valueSmall); + } + + return status; } ze_result_t WddmPciImp::initializeBarProperties(std::vector &pBarProperties) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } +WddmPciImp::WddmPciImp(OsSysman *pOsSysman) { + WddmSysmanImp *pWddmSysmanImp = static_cast(pOsSysman); + pKmdSysManager = &pWddmSysmanImp->getKmdSysManager(); + Device *pDevice = pWddmSysmanImp->getDeviceHandle(); + isLmemSupported = pDevice->getDriverHandle()->getMemoryManager()->isLocalMemorySupported(pDevice->getRootDeviceIndex()); +} + OsPci *OsPci::create(OsSysman *pOsSysman) { - WddmPciImp *pWddmPciImp = new WddmPciImp(); + WddmPciImp *pWddmPciImp = new WddmPciImp(pOsSysman); return static_cast(pWddmPciImp); } 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 new file mode 100644 index 0000000000..68b1a155f8 --- /dev/null +++ b/level_zero/tools/source/sysman/pci/windows/os_pci_imp.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "shared/source/helpers/non_copyable_or_moveable.h" + +#include "sysman/pci/os_pci.h" +#include "sysman/windows/os_sysman_imp.h" + +namespace L0 { +class KmdSysManager; +class WddmPciImp : public OsPci, NEO::NonCopyableOrMovableClass { + public: + ze_result_t getPciBdf(std::string &bdf) 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; + ze_result_t getProperties(zes_pci_properties_t *properties) override; + ze_result_t initializeBarProperties(std::vector &pBarProperties) override; + WddmPciImp(OsSysman *pOsSysman); + WddmPciImp() = default; + ~WddmPciImp() override = default; + + protected: + KmdSysManager *pKmdSysManager = nullptr; + + private: + bool isLmemSupported = false; +}; + +} // namespace L0 diff --git a/level_zero/tools/source/sysman/sysman_imp.cpp b/level_zero/tools/source/sysman/sysman_imp.cpp index c8fb45e769..47ddc3b47f 100644 --- a/level_zero/tools/source/sysman/sysman_imp.cpp +++ b/level_zero/tools/source/sysman/sysman_imp.cpp @@ -111,7 +111,7 @@ ze_result_t SysmanDeviceImp::pciGetProperties(zes_pci_properties_t *pProperties) } ze_result_t SysmanDeviceImp::pciGetState(zes_pci_state_t *pState) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return pPci->pciGetState(pState); } ze_result_t SysmanDeviceImp::pciGetBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) { 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 22f6945b83..8b8009cfb3 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 @@ -136,6 +136,7 @@ struct Mock : public PciSysfsAccess { class PublicLinuxPciImp : public L0::LinuxPciImp { public: + using LinuxPciImp::changeDirNLevelsUp; using LinuxPciImp::pfsAccess; using LinuxPciImp::pSysfsAccess; }; 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 d17c635a2f..829bc8c011 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 @@ -208,8 +208,26 @@ TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVe } TEST_F(ZesPciFixture, GivenValidPathWhileCallingchangeDirNLevelsUpThenReturnedPathIsNLevelUpThenTheCurrentPath) { - std::string testMockRealPath2LevelsUp = changeDirNLevelsUp(mockRealPath, 2); + PublicLinuxPciImp *pOsPci = static_cast(pPciImp->pOsPci); + std::string testMockRealPath2LevelsUp = pOsPci->changeDirNLevelsUp(mockRealPath, 2); EXPECT_EQ(testMockRealPath2LevelsUp, mockRealPath2LevelsUp); } + +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetStateThenVerifyzetSysmanPciGetStateCallReturnNotSupported) { + zes_pci_state_t state; + EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, zesDevicePciGetState(device, &state)); +} + +TEST_F(ZesPciFixture, TestLinkSpeedToGenAndBack) { + for (int32_t i = PciGenerations::PciGen1; i <= PciGenerations::PciGen5; i++) { + double speed = convertPciGenToLinkSpeed(i); + int32_t gen = convertLinkSpeedToPciGen(speed); + EXPECT_EQ(i, gen); + } + + EXPECT_EQ(-1, convertLinkSpeedToPciGen(0.0)); + EXPECT_EQ(0.0, convertPciGenToLinkSpeed(0)); +} + } // namespace ult } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/CMakeLists.txt new file mode 100644 index 0000000000..8aa46e47ca --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +if(WIN32) + target_sources(${TARGET_NAME} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_zes_pci.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_pci.h + ) +endif() diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/mock_pci.h b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/mock_pci.h new file mode 100644 index 0000000000..68c3b0cd47 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/mock_pci.h @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/tools/source/sysman/pci/windows/os_pci_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h" + +#include "sysman/pci/pci_imp.h" + +namespace L0 { +namespace ult { + +struct MockMemoryManagerSysman : public MemoryManagerMock { + MockMemoryManagerSysman(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} +}; + +class PciKmdSysManager : public Mock {}; + +template <> +struct Mock : public PciKmdSysManager { + //PciCurrentDevice, PciParentDevice, PciRootPort + uint32_t mockDomain[3] = {0, 0, 0}; + uint32_t mockBus[3] = {0, 0, 3}; + uint32_t mockDevice[3] = {2, 0, 0}; + uint32_t mockFunction[3] = {0, 0, 0}; + uint32_t mockMaxLinkSpeed[3] = {1, 0, 4}; + uint32_t mockMaxLinkWidth[3] = {1, 0, 8}; + uint32_t mockCurrentLinkSpeed[3] = {1, 0, 3}; + uint32_t mockCurrentLinkWidth[3] = {1, 0, 1}; + + void getPciProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { + uint8_t *pBuffer = reinterpret_cast(pResponse); + pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderOut); + + KmdSysman::PciDomainsType domain = static_cast(pRequest->inCommandParam); + + switch (pRequest->inRequestId) { + case KmdSysman::Requests::Pci::Domain: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockDomain[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::Bus: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockBus[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::Device: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockDevice[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::Function: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockFunction[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::MaxLinkSpeed: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMaxLinkSpeed[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::MaxLinkWidth: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockMaxLinkWidth[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::CurrentLinkSpeed: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockCurrentLinkSpeed[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + case KmdSysman::Requests::Pci::CurrentLinkWidth: { + uint32_t *pValue = reinterpret_cast(pBuffer); + *pValue = mockCurrentLinkWidth[domain]; + pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; + pResponse->outDataSize = sizeof(uint32_t); + } break; + default: { + pResponse->outDataSize = 0; + pResponse->outReturnCode = KmdSysman::KmdSysmanFail; + } break; + } + } + + void setPciProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { + uint8_t *pBuffer = reinterpret_cast(pRequest); + pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderIn); + + pResponse->outDataSize = 0; + pResponse->outReturnCode = KmdSysman::KmdSysmanFail; + } + + Mock() = default; + ~Mock() = default; +}; + +} // namespace ult +} // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/test_zes_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/test_zes_pci.cpp new file mode 100644 index 0000000000..193ac1ca71 --- /dev/null +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/windows/test_zes_pci.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_sysman_fixture.h" + +#include "mock_pci.h" + +namespace L0 { +namespace ult { + +class SysmanDevicePciFixture : public SysmanDeviceFixture { + protected: + Mock *pKmdSysManager = nullptr; + KmdSysManager *pOriginalKmdSysManager = nullptr; + void SetUp() override { + SysmanDeviceFixture::SetUp(); + + pMemoryManagerOld = device->getDriverHandle()->getMemoryManager(); + + pMemoryManager = new ::testing::NiceMock(*neoDevice->getExecutionEnvironment()); + + pMemoryManager->localMemorySupported[0] = false; + + device->getDriverHandle()->setMemoryManager(pMemoryManager); + + pKmdSysManager = new Mock; + + EXPECT_CALL(*pKmdSysManager, escape(_, _, _, _, _)) + .WillRepeatedly(::testing::Invoke(pKmdSysManager, &Mock::mock_escape)); + + pOriginalKmdSysManager = pWddmSysmanImp->pKmdSysManager; + pWddmSysmanImp->pKmdSysManager = pKmdSysManager; + + delete pSysmanDeviceImp->pPci; + + pSysmanDeviceImp->pPci = new PciImp(pOsSysman); + + if (pSysmanDeviceImp->pPci) { + pSysmanDeviceImp->pPci->init(); + } + } + + void TearDown() override { + device->getDriverHandle()->setMemoryManager(pMemoryManagerOld); + SysmanDeviceFixture::TearDown(); + pWddmSysmanImp->pKmdSysManager = pOriginalKmdSysManager; + if (pKmdSysManager != nullptr) { + delete pKmdSysManager; + pKmdSysManager = nullptr; + } + if (pMemoryManager != nullptr) { + delete pMemoryManager; + pMemoryManager = nullptr; + } + } + + void setLocalMemorySupportedAndReinit(bool supported) { + pMemoryManager->localMemorySupported[0] = supported; + + delete pSysmanDeviceImp->pPci; + + pSysmanDeviceImp->pPci = new PciImp(pOsSysman); + + if (pSysmanDeviceImp->pPci) { + pSysmanDeviceImp->pPci->init(); + } + } + + MockMemoryManagerSysman *pMemoryManager = nullptr; + MemoryManager *pMemoryManagerOld; +}; + +TEST_F(SysmanDevicePciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesWithLocalMemoryThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { + setLocalMemorySupportedAndReinit(true); + + zes_pci_properties_t properties; + + ze_result_t result = zesDevicePciGetProperties(device, &properties); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(properties.address.domain, pKmdSysManager->mockDomain[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(properties.address.bus, pKmdSysManager->mockBus[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(properties.address.device, pKmdSysManager->mockDevice[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(properties.address.function, pKmdSysManager->mockFunction[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(properties.maxSpeed.gen, pKmdSysManager->mockMaxLinkSpeed[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(properties.maxSpeed.width, pKmdSysManager->mockMaxLinkWidth[KmdSysman::PciDomainsType::PciRootPort]); +} + +TEST_F(SysmanDevicePciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesWithNoLocalMemoryThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { + setLocalMemorySupportedAndReinit(false); + + zes_pci_properties_t properties; + + ze_result_t result = zesDevicePciGetProperties(device, &properties); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(properties.address.domain, pKmdSysManager->mockDomain[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(properties.address.bus, pKmdSysManager->mockBus[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(properties.address.device, pKmdSysManager->mockDevice[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(properties.address.function, pKmdSysManager->mockFunction[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(properties.maxSpeed.gen, pKmdSysManager->mockMaxLinkSpeed[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(properties.maxSpeed.width, pKmdSysManager->mockMaxLinkWidth[KmdSysman::PciDomainsType::PciCurrentDevice]); +} + +TEST_F(SysmanDevicePciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) { + uint32_t count = 0; + EXPECT_EQ(ZE_RESULT_SUCCESS, zesDevicePciGetBars(device, &count, nullptr)); +} + +TEST_F(SysmanDevicePciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetStatsWithLocalMemoryThenVerifyzetSysmanPciGetBarsCallSucceeds) { + setLocalMemorySupportedAndReinit(true); + + zes_pci_state_t state; + ze_result_t result = zesDevicePciGetState(device, &state); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(state.speed.gen, pKmdSysManager->mockCurrentLinkSpeed[KmdSysman::PciDomainsType::PciRootPort]); + EXPECT_EQ(state.speed.width, pKmdSysManager->mockCurrentLinkWidth[KmdSysman::PciDomainsType::PciRootPort]); +} + +TEST_F(SysmanDevicePciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetStatsWithNoLocalMemoryThenVerifyzetSysmanPciGetBarsCallSucceeds) { + setLocalMemorySupportedAndReinit(false); + + zes_pci_state_t state; + ze_result_t result = zesDevicePciGetState(device, &state); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(state.speed.gen, pKmdSysManager->mockCurrentLinkSpeed[KmdSysman::PciDomainsType::PciCurrentDevice]); + EXPECT_EQ(state.speed.width, pKmdSysManager->mockCurrentLinkWidth[KmdSysman::PciDomainsType::PciCurrentDevice]); +} + +TEST_F(SysmanDevicePciFixture, TestLinkSpeedToGenAndBack) { + for (int32_t i = PciGenerations::PciGen1; i <= PciGenerations::PciGen5; i++) { + double speed = convertPciGenToLinkSpeed(i); + int32_t gen = convertLinkSpeedToPciGen(speed); + EXPECT_EQ(i, gen); + } + + EXPECT_EQ(-1, convertLinkSpeedToPciGen(0.0)); + EXPECT_EQ(0.0, convertPciGenToLinkSpeed(0)); +} + +} // namespace ult +} // namespace L0