From a6ea7ab7db3506238ce30e3fedfd3fc60848d11e Mon Sep 17 00:00:00 2001 From: "Vilvaraj, T J Vivek" Date: Thu, 23 Jul 2020 07:04:28 +0530 Subject: [PATCH] update Sysman PCI APIs to lastest Spec Change-Id: Ie4daf2eb3596f05f824579eff3fe811ebb2f2032 --- level_zero/api/sysman/zes_sysman.cpp | 8 +- .../tools/source/sysman/linux/fs_access.cpp | 19 ++- .../tools/source/sysman/linux/fs_access.h | 3 +- .../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 | 4 +- .../tools/source/sysman/pci/pci_imp.cpp | 15 +- .../source/sysman/pci/windows/os_pci_imp.cpp | 8 +- level_zero/tools/source/sysman/sysman.h | 4 + level_zero/tools/source/sysman/sysman_imp.cpp | 21 +++ level_zero/tools/source/sysman/sysman_imp.h | 5 + .../sources/sysman/pci/linux/CMakeLists.txt | 6 +- .../sources/sysman/pci/linux/mock_sysfs_pci.h | 18 +-- .../{test_sysman_pci.cpp => test_zes_pci.cpp} | 150 +++++++++--------- 14 files changed, 173 insertions(+), 124 deletions(-) rename level_zero/tools/test/unit_tests/sources/sysman/pci/linux/{test_sysman_pci.cpp => test_zes_pci.cpp} (54%) diff --git a/level_zero/api/sysman/zes_sysman.cpp b/level_zero/api/sysman/zes_sysman.cpp index 18b25b622c..3049e99ccf 100644 --- a/level_zero/api/sysman/zes_sysman.cpp +++ b/level_zero/api/sysman/zes_sysman.cpp @@ -110,14 +110,14 @@ ZE_APIEXPORT ze_result_t ZE_APICALL zesDevicePciGetProperties( zes_device_handle_t hDevice, zes_pci_properties_t *pProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->pciGetProperties(pProperties); } ZE_APIEXPORT ze_result_t ZE_APICALL zesDevicePciGetState( zes_device_handle_t hDevice, zes_pci_state_t *pState) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->pciGetState(pState); } ZE_APIEXPORT ze_result_t ZE_APICALL @@ -125,14 +125,14 @@ zesDevicePciGetBars( zes_device_handle_t hDevice, uint32_t *pCount, zes_pci_bar_properties_t *pProperties) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->pciGetBars(pCount, pProperties); } ZE_APIEXPORT ze_result_t ZE_APICALL zesDevicePciGetStats( zes_device_handle_t hDevice, zes_pci_stats_t *pStats) { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; + return L0::SysmanDevice::fromHandle(hDevice)->pciGetStats(pStats); } ZE_APIEXPORT ze_result_t ZE_APICALL diff --git a/level_zero/tools/source/sysman/linux/fs_access.cpp b/level_zero/tools/source/sysman/linux/fs_access.cpp index 1515cff0b6..ddf5db7f86 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.cpp +++ b/level_zero/tools/source/sysman/linux/fs_access.cpp @@ -70,6 +70,23 @@ ze_result_t FsAccess::read(const std::string file, double &val) { return ZE_RESULT_SUCCESS; } +ze_result_t FsAccess::read(const std::string file, int32_t &val) { + // Read a single line from text file without trailing newline + std::ifstream fs; + + fs.open(file.c_str()); + if (fs.fail()) { + return getResult(errno); + } + fs >> val; + if (fs.fail()) { + fs.close(); + return getResult(errno); + } + fs.close(); + return ZE_RESULT_SUCCESS; +} + ze_result_t FsAccess::read(const std::string file, uint32_t &val) { // Read a single line from text file without trailing newline std::ifstream fs; @@ -379,7 +396,7 @@ ze_result_t SysfsAccess::read(const std::string file, std::string &val) { return FsAccess::read(fullPath(file).c_str(), val); } -ze_result_t SysfsAccess::read(const std::string file, int &val) { +ze_result_t SysfsAccess::read(const std::string file, int32_t &val) { std::string str; ze_result_t result; diff --git a/level_zero/tools/source/sysman/linux/fs_access.h b/level_zero/tools/source/sysman/linux/fs_access.h index 4dce3bb93c..cb0c0f4023 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.h +++ b/level_zero/tools/source/sysman/linux/fs_access.h @@ -37,6 +37,7 @@ class FsAccess { virtual ze_result_t read(const std::string file, std::vector &val); virtual ze_result_t read(const std::string file, double &val); virtual ze_result_t read(const std::string file, uint32_t &val); + virtual ze_result_t read(const std::string file, int32_t &val); virtual ze_result_t write(const std::string file, const std::string val); @@ -82,7 +83,7 @@ class SysfsAccess : private FsAccess { ze_result_t getFileMode(const std::string file, ::mode_t &mode) override; ze_result_t read(const std::string file, std::string &val) override; - MOCKABLE_VIRTUAL ze_result_t read(const std::string file, int &val); + ze_result_t read(const std::string file, int32_t &val) override; ze_result_t read(const std::string file, uint32_t &val) override; ze_result_t read(const std::string file, uint64_t &val) override; ze_result_t read(const std::string file, double &val) override; 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 afc8f3425c..ab86c62ee4 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 @@ -74,7 +74,7 @@ ze_result_t LinuxPciImp::getMaxLinkSpeed(double &maxLinkSpeed) { return ZE_RESULT_SUCCESS; } -ze_result_t LinuxPciImp::getMaxLinkWidth(uint32_t &maxLinkwidth) { +ze_result_t LinuxPciImp::getMaxLinkWidth(int32_t &maxLinkwidth) { ze_result_t result; if (isLmemSupported) { std::string rootPortPath; @@ -85,30 +85,30 @@ ze_result_t LinuxPciImp::getMaxLinkWidth(uint32_t &maxLinkwidth) { // the root port is always at a fixed distance as defined in HW rootPortPath = changeDirNLevelsUp(realRootPath, 2); if (ZE_RESULT_SUCCESS != result) { - maxLinkwidth = 0; + maxLinkwidth = -1; return result; } result = pfsAccess->read(rootPortPath + '/' + "max_link_width", maxLinkwidth); if (ZE_RESULT_SUCCESS != result) { - maxLinkwidth = 0; + maxLinkwidth = -1; return result; } - if (maxLinkwidth == static_cast(unknownPcieLinkWidth)) { - maxLinkwidth = 0; + if (maxLinkwidth == static_cast(unknownPcieLinkWidth)) { + maxLinkwidth = -1; } } else { result = pSysfsAccess->read(maxLinkWidthFile, maxLinkwidth); if (ZE_RESULT_SUCCESS != result) { return result; } - if (maxLinkwidth == static_cast(unknownPcieLinkWidth)) { - maxLinkwidth = 0; + if (maxLinkwidth == static_cast(unknownPcieLinkWidth)) { + maxLinkwidth = -1; } } return ZE_RESULT_SUCCESS; } -ze_result_t LinuxPciImp::getLinkGen(uint32_t &linkGen) { +ze_result_t LinuxPciImp::getLinkGen(int32_t &linkGen) { double maxLinkSpeed; getMaxLinkSpeed(maxLinkSpeed); if (maxLinkSpeed == 2.5) { @@ -122,7 +122,7 @@ ze_result_t LinuxPciImp::getLinkGen(uint32_t &linkGen) { } else if (maxLinkSpeed == 32) { linkGen = 5; } else { - linkGen = 0; + linkGen = -1; } return ZE_RESULT_SUCCESS; @@ -152,18 +152,20 @@ ze_result_t LinuxPciImp::initializeBarProperties(std::vectorindex = i; pBarProp->base = baseAddr; pBarProp->size = barSize; // Bar Flags Desc. // Bit-0 - Value 0x0 -> MMIO type BAR - // Bit-0 - Value 0x1 -> I/O Type BAR - // Bit-1 - Reserved - // Bit-2 - Valid only for MMIO type BAR - // Value 0x1 -> 64bit BAR*/ - pBarProp->type = ZES_PCI_BAR_TYPE_MMIO; + // Bit-0 - Value 0x1 -> I/O type BAR + if (i == 0) { // GRaphics MMIO is at BAR0, and is a 64-bit + pBarProp->type = ZES_PCI_BAR_TYPE_MMIO; + } + if (i == 2) { + pBarProp->type = ZES_PCI_BAR_TYPE_MEM; // device memory is always at BAR2 + } if (i == 6) { // the 7th entry of resource file is expected to be ROM BAR pBarProp->type = ZES_PCI_BAR_TYPE_ROM; } 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 d71c60085e..63b8ad2244 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 @@ -19,8 +19,8 @@ class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass { public: ze_result_t getPciBdf(std::string &bdf) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; - ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override; - ze_result_t getLinkGen(uint32_t &linkGen) override; + ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; + ze_result_t getLinkGen(int32_t &linkGen) override; void setLmemSupport(bool val) override; ze_result_t initializeBarProperties(std::vector &pBarProperties) override; LinuxPciImp() = default; diff --git a/level_zero/tools/source/sysman/pci/os_pci.h b/level_zero/tools/source/sysman/pci/os_pci.h index 51d0d3992a..8b69d5607a 100644 --- a/level_zero/tools/source/sysman/pci/os_pci.h +++ b/level_zero/tools/source/sysman/pci/os_pci.h @@ -19,8 +19,8 @@ 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(uint32_t &maxLinkWidth) = 0; - virtual ze_result_t getLinkGen(uint32_t &linkGen) = 0; + virtual ze_result_t getMaxLinkWidth(int32_t &maxLinkWidth) = 0; + virtual ze_result_t getLinkGen(int32_t &linkGen) = 0; virtual void setLmemSupport(bool val) = 0; virtual ze_result_t initializeBarProperties(std::vector &pBarProperties) = 0; static OsPci *create(OsSysman *pOsSysman); diff --git a/level_zero/tools/source/sysman/pci/pci_imp.cpp b/level_zero/tools/source/sysman/pci/pci_imp.cpp index f5e802ee3d..e3110f8787 100644 --- a/level_zero/tools/source/sysman/pci/pci_imp.cpp +++ b/level_zero/tools/source/sysman/pci/pci_imp.cpp @@ -23,7 +23,7 @@ namespace L0 { // pcieSpeedWithEnc = maxLinkSpeedInGt * (Gigabit to Megabit) * Encoding = // maxLinkSpeedInGt * 1000 * Encoding // -uint64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { +int64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { double pcieSpeedWithEnc; if ((maxLinkSpeedInGt == 16) || (maxLinkSpeedInGt == 8)) { pcieSpeedWithEnc = maxLinkSpeedInGt * 1000 * 128 / 130; @@ -39,7 +39,7 @@ uint64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { // Now, because 1Mb/s = (1000*1000)/8 bytes/second = 125000 bytes/second // pcieSpeedWithEnc = pcieSpeedWithEnc * 125000; - return static_cast(pcieSpeedWithEnc); + return static_cast(pcieSpeedWithEnc); } ze_result_t PciImp::pciStaticProperties(zes_pci_properties_t *pProperties) { @@ -80,14 +80,17 @@ void PciImp::init() { &pciProperties.address.device, &pciProperties.address.function); } - uint32_t maxLinkWidth = 0, gen = 0; - uint64_t maxBandWidth = 0; + int32_t maxLinkWidth = -1, gen = -1; + int64_t maxBandWidth = -1; double maxLinkSpeed = 0; pOsPci->getMaxLinkSpeed(maxLinkSpeed); pOsPci->getMaxLinkWidth(maxLinkWidth); maxBandWidth = maxLinkWidth * convertPcieSpeedFromGTsToBs(maxLinkSpeed); - - pciProperties.maxSpeed.maxBandwidth = maxBandWidth; + if (maxBandWidth == 0) { + pciProperties.maxSpeed.maxBandwidth = -1; + } else { + pciProperties.maxSpeed.maxBandwidth = maxBandWidth; + } pciProperties.maxSpeed.width = maxLinkWidth; pOsPci->getLinkGen(gen); pciProperties.maxSpeed.gen = gen; 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 1088c7301a..4489a880f7 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 @@ -14,8 +14,8 @@ class WddmPciImp : public OsPci { public: ze_result_t getPciBdf(std::string &bdf) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; - ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override; - ze_result_t getLinkGen(uint32_t &linkGen) override; + ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override; + ze_result_t getLinkGen(int32_t &linkGen) override; void setLmemSupport(bool val) override; ze_result_t initializeBarProperties(std::vector &pBarProperties) override; ~WddmPciImp() override = default; @@ -29,11 +29,11 @@ ze_result_t WddmPciImp::getMaxLinkSpeed(double &maxLinkSpeed) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } -ze_result_t WddmPciImp::getMaxLinkWidth(uint32_t &maxLinkwidth) { +ze_result_t WddmPciImp::getMaxLinkWidth(int32_t &maxLinkwidth) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } -ze_result_t WddmPciImp::getLinkGen(uint32_t &linkGen) { +ze_result_t WddmPciImp::getLinkGen(int32_t &linkGen) { return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; } diff --git a/level_zero/tools/source/sysman/sysman.h b/level_zero/tools/source/sysman/sysman.h index 0ba2fc3722..4f7b7f6ccc 100644 --- a/level_zero/tools/source/sysman/sysman.h +++ b/level_zero/tools/source/sysman/sysman.h @@ -37,6 +37,10 @@ struct SysmanDevice : _ze_device_handle_t { virtual ze_result_t temperatureGet(uint32_t *pCount, zes_temp_handle_t *phTemperature) = 0; virtual ze_result_t standbyGet(uint32_t *pCount, zes_standby_handle_t *phStandby) = 0; virtual ze_result_t engineGet(uint32_t *pCount, zes_engine_handle_t *phEngine) = 0; + virtual ze_result_t pciGetProperties(zes_pci_properties_t *pProperties) = 0; + virtual ze_result_t pciGetState(zes_pci_state_t *pState) = 0; + virtual ze_result_t pciGetBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) = 0; + virtual ze_result_t pciGetStats(zes_pci_stats_t *pStats) = 0; virtual ~SysmanDevice() = default; }; diff --git a/level_zero/tools/source/sysman/sysman_imp.cpp b/level_zero/tools/source/sysman/sysman_imp.cpp index 545db69aee..523e344d29 100644 --- a/level_zero/tools/source/sysman/sysman_imp.cpp +++ b/level_zero/tools/source/sysman/sysman_imp.cpp @@ -22,6 +22,7 @@ SysmanDeviceImp::SysmanDeviceImp(ze_device_handle_t hDevice) { hCoreDevice = hDevice; pOsSysman = OsSysman::create(this); UNRECOVERABLE_IF(nullptr == pOsSysman); + pPci = new PciImp(pOsSysman, hCoreDevice); pPowerHandleContext = new PowerHandleContext(pOsSysman); pFrequencyHandleContext = new FrequencyHandleContext(pOsSysman); pFabricPortHandleContext = new FabricPortHandleContext(pOsSysman); @@ -35,6 +36,7 @@ SysmanDeviceImp::~SysmanDeviceImp() { freeResource(pStandbyHandleContext); freeResource(pTempHandleContext); freeResource(pFabricPortHandleContext); + freeResource(pPci); freeResource(pFrequencyHandleContext); freeResource(pPowerHandleContext); freeResource(pOsSysman); @@ -54,6 +56,9 @@ void SysmanDeviceImp::init() { if (pTempHandleContext) { pTempHandleContext->init(); } + if (pPci) { + pPci->init(); + } if (pStandbyHandleContext) { pStandbyHandleContext->init(); } @@ -66,6 +71,22 @@ ze_result_t SysmanDeviceImp::frequencyGet(uint32_t *pCount, zes_freq_handle_t *p return pFrequencyHandleContext->frequencyGet(pCount, phFrequency); } +ze_result_t SysmanDeviceImp::pciGetProperties(zes_pci_properties_t *pProperties) { + return pPci->pciStaticProperties(pProperties); +} + +ze_result_t SysmanDeviceImp::pciGetState(zes_pci_state_t *pState) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + +ze_result_t SysmanDeviceImp::pciGetBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) { + return pPci->pciGetInitializedBars(pCount, pProperties); +} + +ze_result_t SysmanDeviceImp::pciGetStats(zes_pci_stats_t *pStats) { + return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; +} + ze_result_t SysmanDeviceImp::powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower) { return pPowerHandleContext->powerGet(pCount, phPower); } diff --git a/level_zero/tools/source/sysman/sysman_imp.h b/level_zero/tools/source/sysman/sysman_imp.h index 30bf56a3db..e40cbac45b 100644 --- a/level_zero/tools/source/sysman/sysman_imp.h +++ b/level_zero/tools/source/sysman/sysman_imp.h @@ -26,6 +26,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { ze_device_handle_t hCoreDevice = nullptr; OsSysman *pOsSysman = nullptr; + Pci *pPci = nullptr; PowerHandleContext *pPowerHandleContext = nullptr; FrequencyHandleContext *pFrequencyHandleContext = nullptr; FabricPortHandleContext *pFabricPortHandleContext = nullptr; @@ -39,6 +40,10 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass { ze_result_t temperatureGet(uint32_t *pCount, zes_temp_handle_t *phTemperature) override; ze_result_t standbyGet(uint32_t *pCount, zes_standby_handle_t *phStandby) override; ze_result_t engineGet(uint32_t *pCount, zes_engine_handle_t *phEngine) override; + ze_result_t pciGetProperties(zes_pci_properties_t *pProperties) override; + ze_result_t pciGetState(zes_pci_state_t *pState) override; + ze_result_t pciGetBars(uint32_t *pCount, zes_pci_bar_properties_t *pProperties) override; + ze_result_t pciGetStats(zes_pci_stats_t *pStats) override; private: template diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/CMakeLists.txt b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/CMakeLists.txt index 533fce6b97..295e84e7a5 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/CMakeLists.txt +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/CMakeLists.txt @@ -7,8 +7,8 @@ if(UNIX) target_sources(${TARGET_NAME} PRIVATE - # ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt - # ${CMAKE_CURRENT_SOURCE_DIR}/test_sysman_pci.cpp - # ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_pci.h + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_pci.h + ${CMAKE_CURRENT_SOURCE_DIR}/test_zes_pci.cpp ) endif() 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 b2f98e83b2..22f6945b83 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 @@ -12,8 +12,6 @@ #include "sysman/pci/pci_imp.h" -using ::testing::_; - namespace L0 { namespace ult { @@ -49,9 +47,9 @@ class PcifsAccess : public FsAccess {}; template <> struct Mock : public PcifsAccess { - uint32_t mockMaxLinkWidth = 0; + int32_t 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, uint32_t &val), (override)); + MOCK_METHOD(ze_result_t, read, (const std::string file, int32_t &val), (override)); ze_result_t getValDouble(const std::string file, double &val) { if (file.compare(mockRealPath2LevelsUp + '/' + "max_link_speed") == 0) { @@ -61,7 +59,7 @@ struct Mock : public PcifsAccess { return ZE_RESULT_ERROR_NOT_AVAILABLE; } - ze_result_t getValInt(const std::string file, uint32_t &val) { + ze_result_t getValInt(const std::string file, int32_t &val) { if (file.compare(mockRealPath2LevelsUp + '/' + "max_link_width") == 0) { val = mockMaxLinkWidth; return ZE_RESULT_SUCCESS; @@ -69,7 +67,7 @@ struct Mock : public PcifsAccess { return ZE_RESULT_ERROR_NOT_AVAILABLE; } - ze_result_t setValInt(const std::string file, uint32_t val) { + ze_result_t setValInt(const std::string file, int32_t val) { if (file.compare(maxLinkWidthFile) == 0) { mockMaxLinkWidth = val; } @@ -79,9 +77,9 @@ struct Mock : public PcifsAccess { template <> struct Mock : public PciSysfsAccess { - uint32_t mockMaxLinkWidth = 0; + int32_t 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, uint32_t &val), (override)); + MOCK_METHOD(ze_result_t, read, (const std::string file, int32_t &val), (override)); MOCK_METHOD(ze_result_t, read, (const std::string file, std::vector &val), (override)); MOCK_METHOD(ze_result_t, readSymLink, (const std::string file, std::string &buf), (override)); MOCK_METHOD(ze_result_t, getRealPath, (const std::string file, std::string &buf), (override)); @@ -94,7 +92,7 @@ struct Mock : public PciSysfsAccess { return ZE_RESULT_ERROR_NOT_AVAILABLE; } - ze_result_t setValInt(const std::string file, uint32_t val) { + ze_result_t setValInt(const std::string file, int32_t val) { if (file.compare(maxLinkWidthFile) == 0) { mockMaxLinkWidth = val; return ZE_RESULT_SUCCESS; @@ -102,7 +100,7 @@ struct Mock : public PciSysfsAccess { return ZE_RESULT_ERROR_NOT_AVAILABLE; } - ze_result_t getValInt(const std::string file, uint32_t &val) { + ze_result_t getValInt(const std::string file, int32_t &val) { if (file.compare(maxLinkWidthFile) == 0) { val = mockMaxLinkWidth; return ZE_RESULT_SUCCESS; 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_zes_pci.cpp similarity index 54% rename from level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_sysman_pci.cpp rename to level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp index 7ef1666a46..88e4f7141f 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_zes_pci.cpp @@ -5,24 +5,17 @@ * */ -#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" -#include "level_zero/tools/source/sysman/pci/linux/os_pci_imp.h" -#include "level_zero/tools/source/sysman/sysman_imp.h" +#include "level_zero/tools/test/unit_tests/sources/sysman/mock_sysman_fixture.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" #include "mock_sysfs_pci.h" #include using ::testing::_; -using ::testing::DoAll; -using ::testing::InSequence; using ::testing::Invoke; using ::testing::Matcher; using ::testing::NiceMock; -using ::testing::Return; namespace L0 { namespace ult { @@ -31,29 +24,25 @@ 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 +constexpr int32_t expectedWidth = 1u; +constexpr int32_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; +constexpr int64_t expectedBandwidth = 250000000u; struct MockMemoryManagerPci : public MemoryManagerMock { MockMemoryManagerPci(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast(executionEnvironment)) {} }; -class SysmanPciFixture : public ::testing::Test { +class ZesPciFixture : public ::testing::Test { protected: - std::unique_ptr sysmanImp; - zet_sysman_handle_t hSysman; - - OsPci *pOsPci = nullptr; - Mock *pSysfsAccess = nullptr; - Mock *pfsAccess = nullptr; - L0::Pci *pPciPrev = nullptr; - L0::PciImp pciImp; - PublicLinuxPciImp linuxPciImp; - + std::unique_ptr> pSysfsAccess; + std::unique_ptr> pfsAccess; MockMemoryManagerPci *memoryManager = nullptr; + SysfsAccess *pOriginalSysfsAccess = nullptr; + FsAccess *pOriginalFsAccess = nullptr; + L0::PciImp *pPciImp; + OsPci *pOsPciPrev; std::unique_ptr> driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; @@ -68,55 +57,64 @@ class SysmanPciFixture : public ::testing::Test { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; - sysmanImp = std::make_unique(device->toHandle()); - pSysfsAccess = new NiceMock>; - linuxPciImp.pSysfsAccess = pSysfsAccess; - pfsAccess = new NiceMock>; - linuxPciImp.pfsAccess = pfsAccess; - pOsPci = static_cast(&linuxPciImp); + neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique(); + auto osInterface = device->getOsInterface().get(); + osInterface->setDrm(new SysmanMockDrm(const_cast(neoDevice->getRootDeviceEnvironment()))); + setenv("ZES_ENABLE_SYSMAN", "1", 1); + device->setSysmanHandle(L0::SysmanDeviceHandleContext::init(device->toHandle())); + pSysmanDevice = device->getSysmanHandle(); + pSysmanDeviceImp = static_cast(pSysmanDevice); + pOsSysman = pSysmanDeviceImp->pOsSysman; + pLinuxSysmanImp = static_cast(pOsSysman); + + pSysfsAccess = std::make_unique>>(); + pOriginalSysfsAccess = pLinuxSysmanImp->pSysfsAccess; + pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get(); + pfsAccess = std::make_unique>>(); + pOriginalFsAccess = pLinuxSysmanImp->pFsAccess; + pLinuxSysmanImp->pFsAccess = pfsAccess.get(); pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth); pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth); - ON_CALL(*pSysfsAccess, read(_, Matcher &>(_))) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValVector)); - ON_CALL(*pSysfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValInt)); - ON_CALL(*pSysfsAccess, readSymLink(_, _)) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValStringSymLink)); - ON_CALL(*pSysfsAccess, getRealPath(_, _)) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValStringRealPath)); - ON_CALL(*pSysfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValDouble)); - ON_CALL(*pfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pfsAccess, &Mock::getValDouble)); - ON_CALL(*pfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pfsAccess, &Mock::getValInt)); - - pPciPrev = sysmanImp->pPci; - sysmanImp->pPci = static_cast(&pciImp); - pciImp.pOsPci = pOsPci; - pciImp.hCoreDevice = device; + ON_CALL(*pSysfsAccess.get(), read(_, Matcher &>(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValVector)); + ON_CALL(*pSysfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValInt)); + ON_CALL(*pSysfsAccess.get(), readSymLink(_, _)) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValStringSymLink)); + ON_CALL(*pSysfsAccess.get(), getRealPath(_, _)) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValStringRealPath)); + ON_CALL(*pSysfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValDouble)); + ON_CALL(*pfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock::getValDouble)); + ON_CALL(*pfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock::getValInt)); + pPciImp = static_cast(pSysmanDeviceImp->pPci); + pPciImp->hCoreDevice = device->toHandle(); + pOsPciPrev = pPciImp->pOsPci; + pPciImp->pOsPci = nullptr; memoryManager->localMemorySupported[0] = 0; - pciImp.init(); - hSysman = sysmanImp->toHandle(); + pPciImp->init(); } void TearDown() override { - sysmanImp->pPci = pPciPrev; - pciImp.pOsPci = nullptr; - // cleanup - if (pSysfsAccess != nullptr) { - delete pSysfsAccess; - pSysfsAccess = nullptr; - } - if (pfsAccess != nullptr) { - delete pfsAccess; - pfsAccess = nullptr; + if (nullptr != pPciImp->pOsPci) { + delete pPciImp->pOsPci; } + pPciImp->pOsPci = pOsPciPrev; + pPciImp = nullptr; + unsetenv("ZES_ENABLE_SYSMAN"); + pLinuxSysmanImp->pSysfsAccess = pOriginalSysfsAccess; + pLinuxSysmanImp->pFsAccess = pOriginalFsAccess; } + SysmanDevice *pSysmanDevice = nullptr; + SysmanDeviceImp *pSysmanDeviceImp = nullptr; + OsSysman *pOsSysman = nullptr; + PublicLinuxSysmanImp *pLinuxSysmanImp = nullptr; }; -TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { zes_pci_properties_t properties, propertiesBefore; memset(&properties.address.bus, std::numeric_limits::max(), sizeof(properties.address.bus)); @@ -127,7 +125,7 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropert memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits::max(), sizeof(properties.maxSpeed.maxBandwidth)); propertiesBefore = properties; - ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties); + ze_result_t result = zesDevicePciGetProperties(device, &properties); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(properties.address.bus, expectedBus); @@ -145,10 +143,10 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropert EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth); } -TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { zes_pci_properties_t properties, propertiesBefore; memoryManager->localMemorySupported[0] = 1; - pciImp.init(); + pPciImp->init(); memset(&properties.address.bus, std::numeric_limits::max(), sizeof(properties.address.bus)); memset(&properties.address.device, std::numeric_limits::max(), sizeof(properties.address.device)); @@ -158,7 +156,7 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingz memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits::max(), sizeof(properties.maxSpeed.maxBandwidth)); propertiesBefore = properties; - ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties); + ze_result_t result = zesDevicePciGetProperties(device, &properties); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(properties.address.bus, expectedBus); @@ -176,31 +174,31 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingz EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth); } -TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) { - uint32_t width = 0; +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) { + int32_t width = 0; pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid); pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid); - ON_CALL(*pSysfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pSysfsAccess, &Mock::getValInt)); - ON_CALL(*pfsAccess, read(_, Matcher(_))) - .WillByDefault(::testing::Invoke(pfsAccess, &Mock::getValInt)); + ON_CALL(*pSysfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock::getValInt)); + ON_CALL(*pfsAccess.get(), read(_, Matcher(_))) + .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock::getValInt)); - EXPECT_EQ(ZE_RESULT_SUCCESS, pciImp.pOsPci->getMaxLinkWidth(width)); - EXPECT_EQ(width, 0u); + EXPECT_EQ(ZE_RESULT_SUCCESS, pPciImp->pOsPci->getMaxLinkWidth(width)); + EXPECT_EQ(width, -1); } -TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) { +TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) { uint32_t count = 0; - ze_result_t result = zetSysmanPciGetBars(hSysman, &count, nullptr); + ze_result_t result = zesDevicePciGetBars(device, &count, nullptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_GT(count, 0u); std::vector pciBarProps(count); - result = zetSysmanPciGetBars(hSysman, &count, pciBarProps.data()); + result = zesDevicePciGetBars(device, &count, pciBarProps.data()); EXPECT_EQ(ZE_RESULT_SUCCESS, result); for (uint32_t i = 0; i < count; i++) { - EXPECT_LE(pciBarProps[i].type, ZET_PCI_BAR_TYPE_OTHER); + EXPECT_LE(pciBarProps[i].type, ZES_PCI_BAR_TYPE_MEM); EXPECT_NE(pciBarProps[i].base, 0u); EXPECT_NE(pciBarProps[i].size, 0u); }