From 6a2e016d7f03086130e10df60bd089a242a113d5 Mon Sep 17 00:00:00 2001 From: Joshua Santosh Ranjan Date: Tue, 25 Oct 2022 15:44:56 +0000 Subject: [PATCH] Add support for UUID Related-To: LOCI-3304 Signed-off-by: Joshua Santosh Ranjan --- .../black_box_tests/common/zello_common.cpp | 7 +- shared/source/os_interface/linux/drm_neo.h | 2 +- .../xe_hpg_core/linux/hw_info_config_dg2.cpp | 34 ++++++ .../linux/hw_info_config_linux_tests.cpp | 7 ++ .../linux/hw_info_config_uuid_tests.cpp | 105 ++++++++++++++++++ 5 files changed, 153 insertions(+), 2 deletions(-) diff --git a/level_zero/core/test/black_box_tests/common/zello_common.cpp b/level_zero/core/test/black_box_tests/common/zello_common.cpp index 92d703c0aa..3b632ebe38 100644 --- a/level_zero/core/test/black_box_tests/common/zello_common.cpp +++ b/level_zero/core/test/black_box_tests/common/zello_common.cpp @@ -366,7 +366,12 @@ void printDeviceProperties(const ze_device_properties_t &props) { << " * numSubslicesPerSlice : " << props.numSubslicesPerSlice << "\n" << " * numSlices : " << props.numSlices << "\n" << " * physicalEUSimdWidth : " << props.physicalEUSimdWidth << "\n" - << " * timerResolution : " << props.timerResolution << "\n"; + << " * timerResolution : " << props.timerResolution << "\n" + << " * uuid : "; + for (uint32_t i = 0; i < ZE_MAX_UUID_SIZE; i++) { + std::cout << std::hex << static_cast(props.uuid.id[i]) << " "; + } + std::cout << "\n"; } else { std::cout << "Device : \n" << " * name : " << props.name << "\n" diff --git a/shared/source/os_interface/linux/drm_neo.h b/shared/source/os_interface/linux/drm_neo.h index 2576162c42..14975dbfeb 100644 --- a/shared/source/os_interface/linux/drm_neo.h +++ b/shared/source/os_interface/linux/drm_neo.h @@ -258,6 +258,7 @@ class Drm : public DriverModel { MOCKABLE_VIRTUAL bool getDeviceMemoryMaxClockRateInMhz(uint32_t tileId, uint32_t &clkRate); MOCKABLE_VIRTUAL bool getDeviceMemoryPhysicalSizeInBytes(uint32_t tileId, uint64_t &physicalSize); void cleanup() override; + bool readSysFsAsString(const std::string &relativeFilePath, std::string &readString); protected: Drm(std::unique_ptr &&hwDeviceIdIn, RootDeviceEnvironment &rootDeviceEnvironment); @@ -274,7 +275,6 @@ class Drm : public DriverModel { static std::string getDrmVersion(int fileDescriptor); bool queryDeviceIdAndRevision(); bool queryI915DeviceIdAndRevision(); - bool readSysFsAsString(const std::string &filePath, std::string &readString); #pragma pack(1) struct PCIConfig { diff --git a/shared/source/xe_hpg_core/linux/hw_info_config_dg2.cpp b/shared/source/xe_hpg_core/linux/hw_info_config_dg2.cpp index 973bb8d093..3e08f33a7c 100644 --- a/shared/source/xe_hpg_core/linux/hw_info_config_dg2.cpp +++ b/shared/source/xe_hpg_core/linux/hw_info_config_dg2.cpp @@ -9,11 +9,13 @@ #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/driver_model_type.h" #include "shared/source/helpers/hw_info.h" +#include "shared/source/helpers/string.h" #include "shared/source/kernel/kernel_properties.h" #include "shared/source/os_interface/hw_info_config.h" #include "shared/source/os_interface/hw_info_config.inl" #include "shared/source/os_interface/hw_info_config_dg2_and_later.inl" #include "shared/source/os_interface/hw_info_config_xehp_and_later.inl" +#include "shared/source/os_interface/linux/drm_neo.h" #include "shared/source/xe_hpg_core/hw_cmds_dg2.h" #include "platforms.h" @@ -44,5 +46,37 @@ int HwInfoConfigHw::configureHardwareCustom(HardwareInfo *hwInfo, OS return 0; } +template <> +bool HwInfoConfigHw::getUuid(Device *device, std::array &uuid) const { + + UNRECOVERABLE_IF(device == nullptr); + if (device->getRootDeviceEnvironment().osInterface == nullptr) { + return false; + } + + const auto driverModel = device->getRootDeviceEnvironment().osInterface->getDriverModel(); + if (driverModel->getDriverModelType() != DriverModelType::DRM) { + return false; + } + + auto pDrm = driverModel->as(); + std::string readString(64u, '\0'); + errno = 0; + if (pDrm->readSysFsAsString("/prelim_csc_unique_id", readString) == false) { + return false; + } + + char *endPtr = nullptr; + uint64_t uuidValue = std::strtoull(readString.data(), &endPtr, 16); + if ((endPtr == readString.data()) || (errno != 0)) { + return false; + } + + uuid.fill(0); + memcpy_s(uuid.data(), uuid.size(), &uuidValue, sizeof(uuidValue)); + + return true; +} + template class HwInfoConfigHw; } // namespace NEO diff --git a/shared/test/unit_test/os_interface/linux/hw_info_config_linux_tests.cpp b/shared/test/unit_test/os_interface/linux/hw_info_config_linux_tests.cpp index 19f0afeaf4..f2330dfab1 100644 --- a/shared/test/unit_test/os_interface/linux/hw_info_config_linux_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/hw_info_config_linux_tests.cpp @@ -49,6 +49,13 @@ HWTEST2_F(HwInfoConfigTestLinuxDummy, givenDebugFlagSetWhenEnablingBlitterOperat EXPECT_FALSE(hardwareInfo.capabilityTable.blitterOperationsSupported); } +HWTEST2_F(HwInfoConfigTestLinuxDummy, givenUnsupportedChipsetUniqueUUIDWhenGettingUuidThenReturnFalse, IsAtMostGen11) { + HardwareInfo hardwareInfo = *defaultHwInfo; + auto hwInfoConfig = HwInfoConfig::get(hardwareInfo.platform.eProductFamily); + std::array id; + EXPECT_FALSE(hwInfoConfig->getUuid(nullptr, id)); +} + TEST_F(HwInfoConfigTestLinuxDummy, GivenDummyConfigThenEdramIsDetected) { hwConfig.use128MbEdram = true; int ret = hwConfig.configureHwInfoDrm(&pInHwInfo, &outHwInfo, osInterface); diff --git a/shared/test/unit_test/os_interface/linux/hw_info_config_uuid_tests.cpp b/shared/test/unit_test/os_interface/linux/hw_info_config_uuid_tests.cpp index 2c5f49a33d..6dd2c64040 100644 --- a/shared/test/unit_test/os_interface/linux/hw_info_config_uuid_tests.cpp +++ b/shared/test/unit_test/os_interface/linux/hw_info_config_uuid_tests.cpp @@ -7,11 +7,13 @@ #include "shared/source/device/root_device.h" #include "shared/source/os_interface/hw_info_config.h" +#include "shared/source/os_interface/linux/os_inc.h" #include "shared/source/os_interface/linux/pmt_util.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" #include "shared/test/common/libult/linux/drm_mock.h" #include "shared/test/common/mocks/linux/mock_drm_allocation.h" #include "shared/test/common/mocks/mock_device.h" +#include "shared/test/common/mocks/mock_driver_model.h" #include "shared/test/common/mocks/mock_memory_manager.h" #include "shared/test/common/mocks/ult_device_factory.h" #include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h" @@ -696,3 +698,106 @@ TEST(PmtUtilTest, givenDataPtrIsNullWhenPmtUtilReadTelemIsCalledThenVerifyZeroIs EXPECT_TRUE(0 == PmtUtil::readTelem("dummy", 16, 0, nullptr)); } + +namespace NEO { +struct SysfsBasedUuidTest : public ::testing::Test { + void SetUp() override { + + deviceFactory = std::make_unique(1, 0); + device = static_cast(deviceFactory->rootDevices[0]); + auto executionEnvironment = device->getExecutionEnvironment(); + executionEnvironment->rootDeviceEnvironments[0]->osInterface.reset(new OSInterface); + auto drmMock = new DrmMockResources(*executionEnvironment->rootDeviceEnvironments[0]); + executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::unique_ptr(drmMock)); + } + + void TearDown() override {} + + std::unique_ptr deviceFactory; + Device *device = nullptr; +}; +extern std::map> directoryFilesMap; +} // namespace NEO + +HWTEST2_F(SysfsBasedUuidTest, whenRetrievingDeviceUuidThenCorrectUuidIsReceived, IsDG2) { + + const auto baseFolder = std::string(Os::sysFsPciPathPrefix) + "/drm"; + NEO::directoryFilesMap.insert({baseFolder, {baseFolder + "/card0"}}); + VariableBackup mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int { + return 1; + }); + + VariableBackup mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t { + std::string uuid("6769df256e271362\n"); + memcpy_s(buf, count, uuid.c_str(), uuid.size()); + return count; + }); + + // Prepare expected Uuid value + const uint64_t expectedUuidValue = 0x6769df256e271362; + std::array uuid; + EXPECT_TRUE(HwInfoConfig::get(productFamily)->getUuid(device, uuid)); + EXPECT_TRUE(0 == std::memcmp(uuid.data(), &expectedUuidValue, sizeof(expectedUuidValue))); + NEO::directoryFilesMap.clear(); +} + +HWTEST2_F(SysfsBasedUuidTest, givenSysfsFileNotAvailableWhenRetrievingDeviceUuidThenFailureIsReturned, IsDG2) { + + const auto baseFolder = std::string(Os::sysFsPciPathPrefix) + "/drm"; + NEO::directoryFilesMap.insert({baseFolder, {baseFolder + "/card0"}}); + VariableBackup mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int { + return -1; + }); + + std::array uuid; + EXPECT_FALSE(HwInfoConfig::get(productFamily)->getUuid(device, uuid)); + NEO::directoryFilesMap.clear(); +} + +HWTEST2_F(SysfsBasedUuidTest, givenIncorrectUuidWhenRetrievingDeviceUuidThenFailureIsReturned, IsDG2) { + + const auto baseFolder = std::string(Os::sysFsPciPathPrefix) + "/drm"; + NEO::directoryFilesMap.insert({baseFolder, {baseFolder + "/card0"}}); + VariableBackup mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int { + return 1; + }); + + VariableBackup mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t { + std::string uuid("Z76Qdf256e271362\n"); + memcpy_s(buf, count, uuid.c_str(), uuid.size()); + return count; + }); + + std::array uuid; + EXPECT_FALSE(HwInfoConfig::get(productFamily)->getUuid(device, uuid)); + NEO::directoryFilesMap.clear(); +} + +HWTEST2_F(SysfsBasedUuidTest, givenErrnoIsSetWhenRetrievingDeviceUuidThenFailureIsReturned, IsDG2) { + + const auto baseFolder = std::string(Os::sysFsPciPathPrefix) + "/drm"; + NEO::directoryFilesMap.insert({baseFolder, {baseFolder + "/card0"}}); + VariableBackup mockOpen(&SysCalls::sysCallsOpen, [](const char *pathname, int flags) -> int { + return 1; + }); + + VariableBackup mockPread(&SysCalls::sysCallsPread, [](int fd, void *buf, size_t count, off_t offset) -> ssize_t { + std::string uuid("6769df256e271362\n"); + memcpy_s(buf, count, uuid.c_str(), uuid.size()); + errno = 1; + return count; + }); + + std::array uuid; + EXPECT_FALSE(HwInfoConfig::get(productFamily)->getUuid(device, uuid)); + NEO::directoryFilesMap.clear(); +} + +HWTEST2_F(SysfsBasedUuidTest, givenDriverModelIsNotDrmWhenRetrievingDeviceUuidThenFailureIsReturned, IsDG2) { + + auto driverModelMock = std::make_unique(); + auto executionEnvironment = device->getExecutionEnvironment(); + executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::move(driverModelMock)); + std::array uuid; + EXPECT_FALSE(HwInfoConfig::get(productFamily)->getUuid(device, uuid)); +} \ No newline at end of file