update Sysman PCI APIs to lastest Spec

Change-Id: Ie4daf2eb3596f05f824579eff3fe811ebb2f2032
This commit is contained in:
Vilvaraj, T J Vivek
2020-07-23 07:04:28 +05:30
committed by sys_ocldev
parent 171c40d58a
commit a6ea7ab7db
14 changed files with 173 additions and 124 deletions

View File

@ -110,14 +110,14 @@ ZE_APIEXPORT ze_result_t ZE_APICALL
zesDevicePciGetProperties( zesDevicePciGetProperties(
zes_device_handle_t hDevice, zes_device_handle_t hDevice,
zes_pci_properties_t *pProperties) { 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 ZE_APIEXPORT ze_result_t ZE_APICALL
zesDevicePciGetState( zesDevicePciGetState(
zes_device_handle_t hDevice, zes_device_handle_t hDevice,
zes_pci_state_t *pState) { 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 ZE_APIEXPORT ze_result_t ZE_APICALL
@ -125,14 +125,14 @@ zesDevicePciGetBars(
zes_device_handle_t hDevice, zes_device_handle_t hDevice,
uint32_t *pCount, uint32_t *pCount,
zes_pci_bar_properties_t *pProperties) { 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 ZE_APIEXPORT ze_result_t ZE_APICALL
zesDevicePciGetStats( zesDevicePciGetStats(
zes_device_handle_t hDevice, zes_device_handle_t hDevice,
zes_pci_stats_t *pStats) { 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 ZE_APIEXPORT ze_result_t ZE_APICALL

View File

@ -70,6 +70,23 @@ ze_result_t FsAccess::read(const std::string file, double &val) {
return ZE_RESULT_SUCCESS; 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) { ze_result_t FsAccess::read(const std::string file, uint32_t &val) {
// Read a single line from text file without trailing newline // Read a single line from text file without trailing newline
std::ifstream fs; 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); 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; std::string str;
ze_result_t result; ze_result_t result;

View File

@ -37,6 +37,7 @@ class FsAccess {
virtual ze_result_t read(const std::string file, std::vector<std::string> &val); virtual ze_result_t read(const std::string file, std::vector<std::string> &val);
virtual ze_result_t read(const std::string file, double &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, 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); 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 getFileMode(const std::string file, ::mode_t &mode) override;
ze_result_t read(const std::string file, std::string &val) 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, uint32_t &val) override;
ze_result_t read(const std::string file, uint64_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; ze_result_t read(const std::string file, double &val) override;

View File

@ -74,7 +74,7 @@ ze_result_t LinuxPciImp::getMaxLinkSpeed(double &maxLinkSpeed) {
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;
} }
ze_result_t LinuxPciImp::getMaxLinkWidth(uint32_t &maxLinkwidth) { ze_result_t LinuxPciImp::getMaxLinkWidth(int32_t &maxLinkwidth) {
ze_result_t result; ze_result_t result;
if (isLmemSupported) { if (isLmemSupported) {
std::string rootPortPath; 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 // the root port is always at a fixed distance as defined in HW
rootPortPath = changeDirNLevelsUp(realRootPath, 2); rootPortPath = changeDirNLevelsUp(realRootPath, 2);
if (ZE_RESULT_SUCCESS != result) { if (ZE_RESULT_SUCCESS != result) {
maxLinkwidth = 0; maxLinkwidth = -1;
return result; return result;
} }
result = pfsAccess->read(rootPortPath + '/' + "max_link_width", maxLinkwidth); result = pfsAccess->read(rootPortPath + '/' + "max_link_width", maxLinkwidth);
if (ZE_RESULT_SUCCESS != result) { if (ZE_RESULT_SUCCESS != result) {
maxLinkwidth = 0; maxLinkwidth = -1;
return result; return result;
} }
if (maxLinkwidth == static_cast<uint32_t>(unknownPcieLinkWidth)) { if (maxLinkwidth == static_cast<int32_t>(unknownPcieLinkWidth)) {
maxLinkwidth = 0; maxLinkwidth = -1;
} }
} else { } else {
result = pSysfsAccess->read(maxLinkWidthFile, maxLinkwidth); result = pSysfsAccess->read(maxLinkWidthFile, maxLinkwidth);
if (ZE_RESULT_SUCCESS != result) { if (ZE_RESULT_SUCCESS != result) {
return result; return result;
} }
if (maxLinkwidth == static_cast<uint32_t>(unknownPcieLinkWidth)) { if (maxLinkwidth == static_cast<int32_t>(unknownPcieLinkWidth)) {
maxLinkwidth = 0; maxLinkwidth = -1;
} }
} }
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;
} }
ze_result_t LinuxPciImp::getLinkGen(uint32_t &linkGen) { ze_result_t LinuxPciImp::getLinkGen(int32_t &linkGen) {
double maxLinkSpeed; double maxLinkSpeed;
getMaxLinkSpeed(maxLinkSpeed); getMaxLinkSpeed(maxLinkSpeed);
if (maxLinkSpeed == 2.5) { if (maxLinkSpeed == 2.5) {
@ -122,7 +122,7 @@ ze_result_t LinuxPciImp::getLinkGen(uint32_t &linkGen) {
} else if (maxLinkSpeed == 32) { } else if (maxLinkSpeed == 32) {
linkGen = 5; linkGen = 5;
} else { } else {
linkGen = 0; linkGen = -1;
} }
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;
@ -152,18 +152,20 @@ ze_result_t LinuxPciImp::initializeBarProperties(std::vector<zes_pci_bar_propert
for (uint32_t i = 0; i <= maxPciBars; i++) { for (uint32_t i = 0; i <= maxPciBars; i++) {
uint64_t baseAddr, barSize, barFlags; uint64_t baseAddr, barSize, barFlags;
getBarBaseAndSize(ReadBytes[i], baseAddr, barSize, barFlags); getBarBaseAndSize(ReadBytes[i], baseAddr, barSize, barFlags);
if (baseAddr) { if (baseAddr && !(barFlags & 0x1)) { // we do not update for I/O ports
zes_pci_bar_properties_t *pBarProp = new zes_pci_bar_properties_t; zes_pci_bar_properties_t *pBarProp = new zes_pci_bar_properties_t;
pBarProp->index = i; pBarProp->index = i;
pBarProp->base = baseAddr; pBarProp->base = baseAddr;
pBarProp->size = barSize; pBarProp->size = barSize;
// Bar Flags Desc. // Bar Flags Desc.
// Bit-0 - Value 0x0 -> MMIO type BAR // Bit-0 - Value 0x0 -> MMIO type BAR
// Bit-0 - Value 0x1 -> I/O Type BAR // Bit-0 - Value 0x1 -> I/O type BAR
// Bit-1 - Reserved if (i == 0) { // GRaphics MMIO is at BAR0, and is a 64-bit
// Bit-2 - Valid only for MMIO type BAR pBarProp->type = ZES_PCI_BAR_TYPE_MMIO;
// Value 0x1 -> 64bit BAR*/ }
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 if (i == 6) { // the 7th entry of resource file is expected to be ROM BAR
pBarProp->type = ZES_PCI_BAR_TYPE_ROM; pBarProp->type = ZES_PCI_BAR_TYPE_ROM;
} }

View File

@ -19,8 +19,8 @@ class LinuxPciImp : public OsPci, NEO::NonCopyableOrMovableClass {
public: public:
ze_result_t getPciBdf(std::string &bdf) override; ze_result_t getPciBdf(std::string &bdf) override;
ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override;
ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override; ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override;
ze_result_t getLinkGen(uint32_t &linkGen) override; ze_result_t getLinkGen(int32_t &linkGen) override;
void setLmemSupport(bool val) override; void setLmemSupport(bool val) override;
ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) override; ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) override;
LinuxPciImp() = default; LinuxPciImp() = default;

View File

@ -19,8 +19,8 @@ class OsPci {
public: public:
virtual ze_result_t getPciBdf(std::string &bdf) = 0; virtual ze_result_t getPciBdf(std::string &bdf) = 0;
virtual ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) = 0; virtual ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) = 0;
virtual ze_result_t getMaxLinkWidth(uint32_t &maxLinkWidth) = 0; virtual ze_result_t getMaxLinkWidth(int32_t &maxLinkWidth) = 0;
virtual ze_result_t getLinkGen(uint32_t &linkGen) = 0; virtual ze_result_t getLinkGen(int32_t &linkGen) = 0;
virtual void setLmemSupport(bool val) = 0; virtual void setLmemSupport(bool val) = 0;
virtual ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) = 0; virtual ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) = 0;
static OsPci *create(OsSysman *pOsSysman); static OsPci *create(OsSysman *pOsSysman);

View File

@ -23,7 +23,7 @@ namespace L0 {
// pcieSpeedWithEnc = maxLinkSpeedInGt * (Gigabit to Megabit) * Encoding = // pcieSpeedWithEnc = maxLinkSpeedInGt * (Gigabit to Megabit) * Encoding =
// maxLinkSpeedInGt * 1000 * Encoding // maxLinkSpeedInGt * 1000 * Encoding
// //
uint64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) { int64_t convertPcieSpeedFromGTsToBs(double maxLinkSpeedInGt) {
double pcieSpeedWithEnc; double pcieSpeedWithEnc;
if ((maxLinkSpeedInGt == 16) || (maxLinkSpeedInGt == 8)) { if ((maxLinkSpeedInGt == 16) || (maxLinkSpeedInGt == 8)) {
pcieSpeedWithEnc = maxLinkSpeedInGt * 1000 * 128 / 130; 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 // Now, because 1Mb/s = (1000*1000)/8 bytes/second = 125000 bytes/second
// //
pcieSpeedWithEnc = pcieSpeedWithEnc * 125000; pcieSpeedWithEnc = pcieSpeedWithEnc * 125000;
return static_cast<uint64_t>(pcieSpeedWithEnc); return static_cast<int64_t>(pcieSpeedWithEnc);
} }
ze_result_t PciImp::pciStaticProperties(zes_pci_properties_t *pProperties) { ze_result_t PciImp::pciStaticProperties(zes_pci_properties_t *pProperties) {
@ -80,14 +80,17 @@ void PciImp::init() {
&pciProperties.address.device, &pciProperties.address.function); &pciProperties.address.device, &pciProperties.address.function);
} }
uint32_t maxLinkWidth = 0, gen = 0; int32_t maxLinkWidth = -1, gen = -1;
uint64_t maxBandWidth = 0; int64_t maxBandWidth = -1;
double maxLinkSpeed = 0; double maxLinkSpeed = 0;
pOsPci->getMaxLinkSpeed(maxLinkSpeed); pOsPci->getMaxLinkSpeed(maxLinkSpeed);
pOsPci->getMaxLinkWidth(maxLinkWidth); pOsPci->getMaxLinkWidth(maxLinkWidth);
maxBandWidth = maxLinkWidth * convertPcieSpeedFromGTsToBs(maxLinkSpeed); maxBandWidth = maxLinkWidth * convertPcieSpeedFromGTsToBs(maxLinkSpeed);
if (maxBandWidth == 0) {
pciProperties.maxSpeed.maxBandwidth = maxBandWidth; pciProperties.maxSpeed.maxBandwidth = -1;
} else {
pciProperties.maxSpeed.maxBandwidth = maxBandWidth;
}
pciProperties.maxSpeed.width = maxLinkWidth; pciProperties.maxSpeed.width = maxLinkWidth;
pOsPci->getLinkGen(gen); pOsPci->getLinkGen(gen);
pciProperties.maxSpeed.gen = gen; pciProperties.maxSpeed.gen = gen;

View File

@ -14,8 +14,8 @@ class WddmPciImp : public OsPci {
public: public:
ze_result_t getPciBdf(std::string &bdf) override; ze_result_t getPciBdf(std::string &bdf) override;
ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override; ze_result_t getMaxLinkSpeed(double &maxLinkSpeed) override;
ze_result_t getMaxLinkWidth(uint32_t &maxLinkwidth) override; ze_result_t getMaxLinkWidth(int32_t &maxLinkwidth) override;
ze_result_t getLinkGen(uint32_t &linkGen) override; ze_result_t getLinkGen(int32_t &linkGen) override;
void setLmemSupport(bool val) override; void setLmemSupport(bool val) override;
ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) override; ze_result_t initializeBarProperties(std::vector<zes_pci_bar_properties_t *> &pBarProperties) override;
~WddmPciImp() override = default; ~WddmPciImp() override = default;
@ -29,11 +29,11 @@ ze_result_t WddmPciImp::getMaxLinkSpeed(double &maxLinkSpeed) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; 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; 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; return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
} }

View File

@ -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 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 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 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; virtual ~SysmanDevice() = default;
}; };

View File

@ -22,6 +22,7 @@ SysmanDeviceImp::SysmanDeviceImp(ze_device_handle_t hDevice) {
hCoreDevice = hDevice; hCoreDevice = hDevice;
pOsSysman = OsSysman::create(this); pOsSysman = OsSysman::create(this);
UNRECOVERABLE_IF(nullptr == pOsSysman); UNRECOVERABLE_IF(nullptr == pOsSysman);
pPci = new PciImp(pOsSysman, hCoreDevice);
pPowerHandleContext = new PowerHandleContext(pOsSysman); pPowerHandleContext = new PowerHandleContext(pOsSysman);
pFrequencyHandleContext = new FrequencyHandleContext(pOsSysman); pFrequencyHandleContext = new FrequencyHandleContext(pOsSysman);
pFabricPortHandleContext = new FabricPortHandleContext(pOsSysman); pFabricPortHandleContext = new FabricPortHandleContext(pOsSysman);
@ -35,6 +36,7 @@ SysmanDeviceImp::~SysmanDeviceImp() {
freeResource(pStandbyHandleContext); freeResource(pStandbyHandleContext);
freeResource(pTempHandleContext); freeResource(pTempHandleContext);
freeResource(pFabricPortHandleContext); freeResource(pFabricPortHandleContext);
freeResource(pPci);
freeResource(pFrequencyHandleContext); freeResource(pFrequencyHandleContext);
freeResource(pPowerHandleContext); freeResource(pPowerHandleContext);
freeResource(pOsSysman); freeResource(pOsSysman);
@ -54,6 +56,9 @@ void SysmanDeviceImp::init() {
if (pTempHandleContext) { if (pTempHandleContext) {
pTempHandleContext->init(); pTempHandleContext->init();
} }
if (pPci) {
pPci->init();
}
if (pStandbyHandleContext) { if (pStandbyHandleContext) {
pStandbyHandleContext->init(); pStandbyHandleContext->init();
} }
@ -66,6 +71,22 @@ ze_result_t SysmanDeviceImp::frequencyGet(uint32_t *pCount, zes_freq_handle_t *p
return pFrequencyHandleContext->frequencyGet(pCount, phFrequency); 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) { ze_result_t SysmanDeviceImp::powerGet(uint32_t *pCount, zes_pwr_handle_t *phPower) {
return pPowerHandleContext->powerGet(pCount, phPower); return pPowerHandleContext->powerGet(pCount, phPower);
} }

View File

@ -26,6 +26,7 @@ struct SysmanDeviceImp : SysmanDevice, NEO::NonCopyableOrMovableClass {
ze_device_handle_t hCoreDevice = nullptr; ze_device_handle_t hCoreDevice = nullptr;
OsSysman *pOsSysman = nullptr; OsSysman *pOsSysman = nullptr;
Pci *pPci = nullptr;
PowerHandleContext *pPowerHandleContext = nullptr; PowerHandleContext *pPowerHandleContext = nullptr;
FrequencyHandleContext *pFrequencyHandleContext = nullptr; FrequencyHandleContext *pFrequencyHandleContext = nullptr;
FabricPortHandleContext *pFabricPortHandleContext = 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 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 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 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: private:
template <typename T> template <typename T>

View File

@ -7,8 +7,8 @@
if(UNIX) if(UNIX)
target_sources(${TARGET_NAME} target_sources(${TARGET_NAME}
PRIVATE PRIVATE
# ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt ${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}/mock_sysfs_pci.h ${CMAKE_CURRENT_SOURCE_DIR}/test_zes_pci.cpp
) )
endif() endif()

View File

@ -12,8 +12,6 @@
#include "sysman/pci/pci_imp.h" #include "sysman/pci/pci_imp.h"
using ::testing::_;
namespace L0 { namespace L0 {
namespace ult { namespace ult {
@ -49,9 +47,9 @@ class PcifsAccess : public FsAccess {};
template <> template <>
struct Mock<PcifsAccess> : public PcifsAccess { struct Mock<PcifsAccess> : 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, 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) { ze_result_t getValDouble(const std::string file, double &val) {
if (file.compare(mockRealPath2LevelsUp + '/' + "max_link_speed") == 0) { if (file.compare(mockRealPath2LevelsUp + '/' + "max_link_speed") == 0) {
@ -61,7 +59,7 @@ struct Mock<PcifsAccess> : public PcifsAccess {
return ZE_RESULT_ERROR_NOT_AVAILABLE; 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) { if (file.compare(mockRealPath2LevelsUp + '/' + "max_link_width") == 0) {
val = mockMaxLinkWidth; val = mockMaxLinkWidth;
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;
@ -69,7 +67,7 @@ struct Mock<PcifsAccess> : public PcifsAccess {
return ZE_RESULT_ERROR_NOT_AVAILABLE; 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) { if (file.compare(maxLinkWidthFile) == 0) {
mockMaxLinkWidth = val; mockMaxLinkWidth = val;
} }
@ -79,9 +77,9 @@ struct Mock<PcifsAccess> : public PcifsAccess {
template <> template <>
struct Mock<PciSysfsAccess> : public PciSysfsAccess { struct Mock<PciSysfsAccess> : 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, 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<std::string> &val), (override)); MOCK_METHOD(ze_result_t, read, (const std::string file, std::vector<std::string> &val), (override));
MOCK_METHOD(ze_result_t, readSymLink, (const std::string file, std::string &buf), (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)); MOCK_METHOD(ze_result_t, getRealPath, (const std::string file, std::string &buf), (override));
@ -94,7 +92,7 @@ struct Mock<PciSysfsAccess> : public PciSysfsAccess {
return ZE_RESULT_ERROR_NOT_AVAILABLE; 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) { if (file.compare(maxLinkWidthFile) == 0) {
mockMaxLinkWidth = val; mockMaxLinkWidth = val;
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;
@ -102,7 +100,7 @@ struct Mock<PciSysfsAccess> : public PciSysfsAccess {
return ZE_RESULT_ERROR_NOT_AVAILABLE; 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) { if (file.compare(maxLinkWidthFile) == 0) {
val = mockMaxLinkWidth; val = mockMaxLinkWidth;
return ZE_RESULT_SUCCESS; return ZE_RESULT_SUCCESS;

View File

@ -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/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/test/unit_tests/sources/sysman/mock_sysman_fixture.h"
#include "level_zero/tools/source/sysman/sysman_imp.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mock_sysfs_pci.h" #include "mock_sysfs_pci.h"
#include <string> #include <string>
using ::testing::_; using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke; using ::testing::Invoke;
using ::testing::Matcher; using ::testing::Matcher;
using ::testing::NiceMock; using ::testing::NiceMock;
using ::testing::Return;
namespace L0 { namespace L0 {
namespace ult { namespace ult {
@ -31,29 +24,25 @@ constexpr int mockMaxLinkWidthInvalid = 255;
constexpr uint32_t expectedBus = 0u; constexpr uint32_t expectedBus = 0u;
constexpr uint32_t expectedDevice = 2u; constexpr uint32_t expectedDevice = 2u;
constexpr uint32_t expectedFunction = 0u; constexpr uint32_t expectedFunction = 0u;
constexpr uint32_t expectedWidth = 1u; constexpr int32_t expectedWidth = 1u;
constexpr uint32_t expectedGen = 1u; // As mockMaxLinkSpeed = 2.5, hence expectedGen should be 1 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 // 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 { struct MockMemoryManagerPci : public MemoryManagerMock {
MockMemoryManagerPci(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast<NEO::ExecutionEnvironment &>(executionEnvironment)) {} MockMemoryManagerPci(NEO::ExecutionEnvironment &executionEnvironment) : MemoryManagerMock(const_cast<NEO::ExecutionEnvironment &>(executionEnvironment)) {}
}; };
class SysmanPciFixture : public ::testing::Test { class ZesPciFixture : public ::testing::Test {
protected: protected:
std::unique_ptr<SysmanImp> sysmanImp; std::unique_ptr<Mock<PciSysfsAccess>> pSysfsAccess;
zet_sysman_handle_t hSysman; std::unique_ptr<Mock<PcifsAccess>> pfsAccess;
OsPci *pOsPci = nullptr;
Mock<PciSysfsAccess> *pSysfsAccess = nullptr;
Mock<PcifsAccess> *pfsAccess = nullptr;
L0::Pci *pPciPrev = nullptr;
L0::PciImp pciImp;
PublicLinuxPciImp linuxPciImp;
MockMemoryManagerPci *memoryManager = nullptr; MockMemoryManagerPci *memoryManager = nullptr;
SysfsAccess *pOriginalSysfsAccess = nullptr;
FsAccess *pOriginalFsAccess = nullptr;
L0::PciImp *pPciImp;
OsPci *pOsPciPrev;
std::unique_ptr<Mock<L0::DriverHandleImp>> driverHandle; std::unique_ptr<Mock<L0::DriverHandleImp>> driverHandle;
NEO::MockDevice *neoDevice = nullptr; NEO::MockDevice *neoDevice = nullptr;
L0::Device *device = nullptr; L0::Device *device = nullptr;
@ -68,55 +57,64 @@ class SysmanPciFixture : public ::testing::Test {
driverHandle->initialize(std::move(devices)); driverHandle->initialize(std::move(devices));
device = driverHandle->devices[0]; device = driverHandle->devices[0];
sysmanImp = std::make_unique<SysmanImp>(device->toHandle()); neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique<NEO::OSInterface>();
pSysfsAccess = new NiceMock<Mock<PciSysfsAccess>>; auto osInterface = device->getOsInterface().get();
linuxPciImp.pSysfsAccess = pSysfsAccess; osInterface->setDrm(new SysmanMockDrm(const_cast<NEO::RootDeviceEnvironment &>(neoDevice->getRootDeviceEnvironment())));
pfsAccess = new NiceMock<Mock<PcifsAccess>>; setenv("ZES_ENABLE_SYSMAN", "1", 1);
linuxPciImp.pfsAccess = pfsAccess; device->setSysmanHandle(L0::SysmanDeviceHandleContext::init(device->toHandle()));
pOsPci = static_cast<OsPci *>(&linuxPciImp); pSysmanDevice = device->getSysmanHandle();
pSysmanDeviceImp = static_cast<SysmanDeviceImp *>(pSysmanDevice);
pOsSysman = pSysmanDeviceImp->pOsSysman;
pLinuxSysmanImp = static_cast<PublicLinuxSysmanImp *>(pOsSysman);
pSysfsAccess = std::make_unique<NiceMock<Mock<PciSysfsAccess>>>();
pOriginalSysfsAccess = pLinuxSysmanImp->pSysfsAccess;
pLinuxSysmanImp->pSysfsAccess = pSysfsAccess.get();
pfsAccess = std::make_unique<NiceMock<Mock<PcifsAccess>>>();
pOriginalFsAccess = pLinuxSysmanImp->pFsAccess;
pLinuxSysmanImp->pFsAccess = pfsAccess.get();
pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth); pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth);
pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth); pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidth);
ON_CALL(*pSysfsAccess, read(_, Matcher<std::vector<std::string> &>(_))) ON_CALL(*pSysfsAccess.get(), read(_, Matcher<std::vector<std::string> &>(_)))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValVector)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValVector));
ON_CALL(*pSysfsAccess, read(_, Matcher<uint32_t &>(_))) ON_CALL(*pSysfsAccess.get(), read(_, Matcher<int32_t &>(_)))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValInt)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValInt));
ON_CALL(*pSysfsAccess, readSymLink(_, _)) ON_CALL(*pSysfsAccess.get(), readSymLink(_, _))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValStringSymLink)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValStringSymLink));
ON_CALL(*pSysfsAccess, getRealPath(_, _)) ON_CALL(*pSysfsAccess.get(), getRealPath(_, _))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValStringRealPath)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValStringRealPath));
ON_CALL(*pSysfsAccess, read(_, Matcher<double &>(_))) ON_CALL(*pSysfsAccess.get(), read(_, Matcher<double &>(_)))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValDouble)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValDouble));
ON_CALL(*pfsAccess, read(_, Matcher<double &>(_))) ON_CALL(*pfsAccess.get(), read(_, Matcher<double &>(_)))
.WillByDefault(::testing::Invoke(pfsAccess, &Mock<PcifsAccess>::getValDouble)); .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock<PcifsAccess>::getValDouble));
ON_CALL(*pfsAccess, read(_, Matcher<uint32_t &>(_))) ON_CALL(*pfsAccess.get(), read(_, Matcher<int32_t &>(_)))
.WillByDefault(::testing::Invoke(pfsAccess, &Mock<PcifsAccess>::getValInt)); .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock<PcifsAccess>::getValInt));
pPciImp = static_cast<L0::PciImp *>(pSysmanDeviceImp->pPci);
pPciPrev = sysmanImp->pPci; pPciImp->hCoreDevice = device->toHandle();
sysmanImp->pPci = static_cast<Pci *>(&pciImp); pOsPciPrev = pPciImp->pOsPci;
pciImp.pOsPci = pOsPci; pPciImp->pOsPci = nullptr;
pciImp.hCoreDevice = device;
memoryManager->localMemorySupported[0] = 0; memoryManager->localMemorySupported[0] = 0;
pciImp.init(); pPciImp->init();
hSysman = sysmanImp->toHandle();
} }
void TearDown() override { void TearDown() override {
sysmanImp->pPci = pPciPrev; if (nullptr != pPciImp->pOsPci) {
pciImp.pOsPci = nullptr; delete pPciImp->pOsPci;
// cleanup
if (pSysfsAccess != nullptr) {
delete pSysfsAccess;
pSysfsAccess = nullptr;
}
if (pfsAccess != nullptr) {
delete pfsAccess;
pfsAccess = nullptr;
} }
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; zes_pci_properties_t properties, propertiesBefore;
memset(&properties.address.bus, std::numeric_limits<int>::max(), sizeof(properties.address.bus)); memset(&properties.address.bus, std::numeric_limits<int>::max(), sizeof(properties.address.bus));
@ -127,7 +125,7 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropert
memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits<int>::max(), sizeof(properties.maxSpeed.maxBandwidth)); memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits<int>::max(), sizeof(properties.maxSpeed.maxBandwidth));
propertiesBefore = properties; propertiesBefore = properties;
ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties); ze_result_t result = zesDevicePciGetProperties(device, &properties);
EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_EQ(properties.address.bus, expectedBus); EXPECT_EQ(properties.address.bus, expectedBus);
@ -145,10 +143,10 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetPropert
EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth); EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth);
} }
TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) { TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingzetSysmanPciGetPropertiesThenVerifyzetSysmanPciGetPropertiesCallSucceeds) {
zes_pci_properties_t properties, propertiesBefore; zes_pci_properties_t properties, propertiesBefore;
memoryManager->localMemorySupported[0] = 1; memoryManager->localMemorySupported[0] = 1;
pciImp.init(); pPciImp->init();
memset(&properties.address.bus, std::numeric_limits<int>::max(), sizeof(properties.address.bus)); memset(&properties.address.bus, std::numeric_limits<int>::max(), sizeof(properties.address.bus));
memset(&properties.address.device, std::numeric_limits<int>::max(), sizeof(properties.address.device)); memset(&properties.address.device, std::numeric_limits<int>::max(), sizeof(properties.address.device));
@ -158,7 +156,7 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingz
memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits<int>::max(), sizeof(properties.maxSpeed.maxBandwidth)); memset(&properties.maxSpeed.maxBandwidth, std::numeric_limits<int>::max(), sizeof(properties.maxSpeed.maxBandwidth));
propertiesBefore = properties; propertiesBefore = properties;
ze_result_t result = zetSysmanPciGetProperties(hSysman, &properties); ze_result_t result = zesDevicePciGetProperties(device, &properties);
EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_EQ(properties.address.bus, expectedBus); EXPECT_EQ(properties.address.bus, expectedBus);
@ -176,31 +174,31 @@ TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenSettingLmemSupportAndCallingz
EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth); EXPECT_NE(properties.maxSpeed.maxBandwidth, propertiesBefore.maxSpeed.maxBandwidth);
} }
TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) { TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenGettingPCIWidthThenZeroWidthIsReturnedIfSystemProvidesInvalidValue) {
uint32_t width = 0; int32_t width = 0;
pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid); pSysfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid);
pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid); pfsAccess->setValInt(maxLinkWidthFile, mockMaxLinkWidthInvalid);
ON_CALL(*pSysfsAccess, read(_, Matcher<uint32_t &>(_))) ON_CALL(*pSysfsAccess.get(), read(_, Matcher<int32_t &>(_)))
.WillByDefault(::testing::Invoke(pSysfsAccess, &Mock<PciSysfsAccess>::getValInt)); .WillByDefault(::testing::Invoke(pSysfsAccess.get(), &Mock<PciSysfsAccess>::getValInt));
ON_CALL(*pfsAccess, read(_, Matcher<uint32_t &>(_))) ON_CALL(*pfsAccess.get(), read(_, Matcher<int32_t &>(_)))
.WillByDefault(::testing::Invoke(pfsAccess, &Mock<PcifsAccess>::getValInt)); .WillByDefault(::testing::Invoke(pfsAccess.get(), &Mock<PcifsAccess>::getValInt));
EXPECT_EQ(ZE_RESULT_SUCCESS, pciImp.pOsPci->getMaxLinkWidth(width)); EXPECT_EQ(ZE_RESULT_SUCCESS, pPciImp->pOsPci->getMaxLinkWidth(width));
EXPECT_EQ(width, 0u); EXPECT_EQ(width, -1);
} }
TEST_F(SysmanPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) { TEST_F(ZesPciFixture, GivenValidSysmanHandleWhenCallingzetSysmanPciGetBarsThenVerifyzetSysmanPciGetBarsCallSucceeds) {
uint32_t count = 0; 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_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_GT(count, 0u); EXPECT_GT(count, 0u);
std::vector<zes_pci_bar_properties_t> pciBarProps(count); std::vector<zes_pci_bar_properties_t> pciBarProps(count);
result = zetSysmanPciGetBars(hSysman, &count, pciBarProps.data()); result = zesDevicePciGetBars(device, &count, pciBarProps.data());
EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(ZE_RESULT_SUCCESS, result);
for (uint32_t i = 0; i < count; i++) { 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].base, 0u);
EXPECT_NE(pciBarProps[i].size, 0u); EXPECT_NE(pciBarProps[i].size, 0u);
} }