| 1 |
/* |
| 2 |
* Copyright (C) 2020-2025 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
#include "shared/source/helpers/ptr_math.h" |
| 10 |
#include "shared/source/os_interface/linux/drm_neo.h" |
| 11 |
#include "shared/source/os_interface/linux/ioctl_helper.h" |
| 12 |
#include "shared/source/os_interface/linux/system_info.h" |
| 13 |
#include "shared/test/common/test_macros/mock_method_macros.h" |
| 14 |
|
| 15 |
#include "level_zero/tools/source/sysman/events/events_imp.h" |
| 16 |
#include "level_zero/tools/source/sysman/events/linux/os_events_imp.h" |
| 17 |
#include "level_zero/tools/source/sysman/firmware_util/firmware_util.h" |
| 18 |
#include "level_zero/tools/source/sysman/linux/fs_access.h" |
| 19 |
#include "level_zero/tools/source/sysman/linux/os_sysman_driver_imp.h" |
| 20 |
#include "level_zero/tools/source/sysman/linux/os_sysman_imp.h" |
| 21 |
#include "level_zero/tools/source/sysman/linux/pmu/pmu_imp.h" |
| 22 |
|
| 23 |
#include <cstring> |
| 24 |
|
| 25 |
using namespace NEO; |
| 26 |
namespace L0 { |
| 27 |
namespace ult { |
| 28 |
|
| 29 |
const std::string ueventWedgedFile("/var/lib/libze_intel_gpu/wedged_file"); |
| 30 |
const std::string ueventDetachFile("/var/lib/libze_intel_gpu/remove-pci-0000_03_00_0"); |
| 31 |
const std::string ueventAttachFile("/var/lib/libze_intel_gpu/add-pci-0000_03_00_0"); |
| 32 |
const std::string ueventFabricFile("/var/lib/libze_intel_gpu/fabric-pci-0000_03_00_0"); |
| 33 |
const std::string deviceDir("device"); |
| 34 |
const std::string deviceMemoryHealth("device_memory_health"); |
| 35 |
const std::string eventsDir("/sys/devices/i915_0000_03_00.0/events"); |
| 36 |
constexpr int64_t mockPmuFd = 10; |
| 37 |
constexpr uint64_t errorCount = 10u; |
| 38 |
constexpr uint64_t mockTimeStamp = 1100u; |
| 39 |
|
| 40 |
struct MockPmuInterfaceImpForEvents : public PmuInterfaceImp { |
| 41 |
using PmuInterfaceImp::perfEventOpen; |
| 42 |
MockPmuInterfaceImpForEvents(LinuxSysmanImp *pLinuxSysmanImp) : PmuInterfaceImp(pLinuxSysmanImp) {} |
| 43 |
|
| 44 |
bool mockPmuReadFail = false; |
| 45 |
|
| 46 |
int64_t perfEventOpen(perf_event_attr *attr, pid_t pid, int cpu, int groupFd, uint64_t flags) override { |
| 47 |
return mockPmuFd; |
| 48 |
} |
| 49 |
|
| 50 |
int pmuRead(int fd, uint64_t *data, ssize_t sizeOfdata) override { |
| 51 |
|
| 52 |
if (mockPmuReadFail == true) { |
| 53 |
return mockedPmuReadAndFailureReturn(fd, data, sizeOfdata); |
| 54 |
} |
| 55 |
|
| 56 |
data[0] = 0; |
| 57 |
data[1] = mockTimeStamp; |
| 58 |
data[2] = errorCount; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
int mockedPmuReadAndFailureReturn(int fd, uint64_t *data, ssize_t sizeOfdata) { |
| 62 |
return -1; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
struct MockEventsFsAccess : public FsAccess { |
| 67 |
|
| 68 |
bool mockReadValSuccess = false; |
| 69 |
bool mockReadValOne = false; |
| 70 |
bool mockReadValZero = false; |
| 71 |
bool mockFileNotFoundError = false; |
| 72 |
ze_result_t mockListDirectoryResult = ZE_RESULT_SUCCESS; |
| 73 |
|
| 74 |
ze_result_t getValReturnValAsOne(const std::string file, uint32_t &val) { |
| 75 |
if (file.compare(ueventWedgedFile) == 0) { |
| 76 |
val = 1; |
| 77 |
} else if (file.compare(ueventDetachFile) == 0) { |
| 78 |
val = 1; |
| 79 |
} else if (file.compare(ueventAttachFile) == 0) { |
| 80 |
val = 1; |
| 81 |
} else if (file.compare(ueventFabricFile) == 0) { |
| 82 |
val = 1; |
| 83 |
} else { |
| 84 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 85 |
} |
| 86 |
return ZE_RESULT_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
ze_result_t getValReturnValAsZero(const std::string file, uint32_t &val) { |
| 90 |
if (file.compare(ueventWedgedFile) == 0) { |
| 91 |
val = 0; |
| 92 |
} else if (file.compare(ueventDetachFile) == 0) { |
| 93 |
val = 0; |
| 94 |
} else if (file.compare(ueventAttachFile) == 0) { |
| 95 |
val = 0; |
| 96 |
} else if (file.compare(ueventFabricFile) == 0) { |
| 97 |
val = 0; |
| 98 |
} else { |
| 99 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 100 |
} |
| 101 |
return ZE_RESULT_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
ze_result_t read(const std::string file, std::string &config) override { |
| 105 |
if (file.compare(eventsDir + "/" + "error--correctable-eu-grf") == 0) { |
| 106 |
config = "config=0x0000000000000001"; |
| 107 |
return ZE_RESULT_SUCCESS; |
| 108 |
} else if (file.compare(eventsDir + "/" + "error--engine-reset") == 0) { |
| 109 |
config = "config=0x000000000000010"; |
| 110 |
return ZE_RESULT_SUCCESS; |
| 111 |
} |
| 112 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 113 |
} |
| 114 |
|
| 115 |
ze_result_t getValFileNotFound(const std::string file, uint32_t &val) { |
| 116 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 117 |
} |
| 118 |
|
| 119 |
ze_result_t getValFileInsufficientPermissions(const std::string file, uint32_t &val) { |
| 120 |
return ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS; |
| 121 |
} |
| 122 |
|
| 123 |
ze_result_t listDirectory(const std::string directory, std::vector<std::string> &events) override { |
| 124 |
if (mockListDirectoryResult != ZE_RESULT_SUCCESS) { |
| 125 |
return mockListDirectoryResult; |
| 126 |
} |
| 127 |
|
| 128 |
if (directory.compare(eventsDir) == 0) { |
| 129 |
events.push_back("error--correctable-eu-grf"); |
| 130 |
events.push_back("error--engine-reset"); |
| 131 |
return ZE_RESULT_SUCCESS; |
| 132 |
} |
| 133 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 134 |
} |
| 135 |
|
| 136 |
ze_result_t readValSuccess(const std::string file, uint32_t &val) { |
| 137 |
val = 23; |
| 138 |
return ZE_RESULT_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
ze_result_t read(const std::string file, uint32_t &val) override { |
| 142 |
|
| 143 |
if (mockReadValOne == true) { |
| 144 |
return getValReturnValAsOne(file, val); |
| 145 |
} |
| 146 |
|
| 147 |
else if (mockReadValZero == true) { |
| 148 |
return getValReturnValAsZero(file, val); |
| 149 |
} |
| 150 |
|
| 151 |
else if (mockFileNotFoundError == true) { |
| 152 |
return getValFileNotFound(file, val); |
| 153 |
} |
| 154 |
|
| 155 |
else if (mockReadValSuccess == true) { |
| 156 |
val = 23; |
| 157 |
return readValSuccess(file, val); |
| 158 |
} |
| 159 |
|
| 160 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 161 |
} |
| 162 |
|
| 163 |
bool isRootUser() override { |
| 164 |
return true; |
| 165 |
} |
| 166 |
bool userIsNotRoot() { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
MockEventsFsAccess() = default; |
| 171 |
}; |
| 172 |
|
| 173 |
struct MockEventsSysfsAccess : public SysfsAccess { |
| 174 |
ze_result_t getRealPathResult = ZE_RESULT_SUCCESS; |
| 175 |
std::string realPath = "/sys/devices/pci0000:97/0000:97:02.0/0000:98:00.0/0000:99:01.0/0000:9a:00.0"; |
| 176 |
|
| 177 |
ze_result_t getRealPath(const std::string file, std::string &val) override { |
| 178 |
val = realPath; |
| 179 |
return getRealPathResult; |
| 180 |
} |
| 181 |
|
| 182 |
ze_result_t readSymLink(const std::string file, std::string &val) override { |
| 183 |
|
| 184 |
if (file.compare(deviceDir) == 0) { |
| 185 |
val = "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/0000:02:01.0/0000:03:00.0"; |
| 186 |
return ZE_RESULT_SUCCESS; |
| 187 |
} |
| 188 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 189 |
} |
| 190 |
|
| 191 |
ze_result_t read(const std::string file, uint64_t &val) override { |
| 192 |
if (file.compare("gt/gt0/error_counter/correctable_eu_grf") == 0) { |
| 193 |
val = 5u; |
| 194 |
return ZE_RESULT_SUCCESS; |
| 195 |
} else if (file.compare("gt/gt0/error_counter/engine_reset") == 0) { |
| 196 |
val = 8u; |
| 197 |
return ZE_RESULT_SUCCESS; |
| 198 |
} |
| 199 |
return ZE_RESULT_ERROR_NOT_AVAILABLE; |
| 200 |
} |
| 201 |
|
| 202 |
MockEventsSysfsAccess() = default; |
| 203 |
}; |
| 204 |
|
| 205 |
class EventsUdevLibMock : public UdevLib { |
| 206 |
public: |
| 207 |
EventsUdevLibMock() = default; |
| 208 |
std::string eventPropertyValueTypeResult = "PORT_CHANGE"; |
| 209 |
std::string eventPropertyValueDevPathResult = "/devices/pci0000:97/0000:97:02.0/0000:98:00.0/0000:99:01.0/0000:9a:00.0/i915.iaf.0"; |
| 210 |
std::string getEventPropertyValueResult = "1"; |
| 211 |
|
| 212 |
const char *getEventPropertyValue(void *dev, const char *key) override { |
| 213 |
if (strcmp(key, "TYPE") == 0) { |
| 214 |
if (!eventPropertyValueTypeResult.empty()) { |
| 215 |
return eventPropertyValueTypeResult.c_str(); |
| 216 |
} else { |
| 217 |
return nullptr; |
| 218 |
} |
| 219 |
} else if (strcmp(key, "DEVPATH") == 0) { |
| 220 |
if (eventPropertyValueDevPathResult.empty()) { |
| 221 |
return nullptr; |
| 222 |
} |
| 223 |
return eventPropertyValueDevPathResult.c_str(); |
| 224 |
} else if (getEventPropertyValueResult.empty()) { |
| 225 |
return nullptr; |
| 226 |
} |
| 227 |
return getEventPropertyValueResult.c_str(); |
| 228 |
} |
| 229 |
|
| 230 |
ADDMETHOD_NOBASE(registerEventsFromSubsystemAndGetFd, int, 0, (std::vector<std::string> & subsystemList)); |
| 231 |
ADDMETHOD_NOBASE(getEventGenerationSourceDevice, dev_t, 0, (void *dev)); |
| 232 |
ADDMETHOD_NOBASE(getEventType, const char *, "change", (void *dev)); |
| 233 |
ADDMETHOD_NOBASE(allocateDeviceToReceiveData, void *, (void *)(0x12345678), ()); |
| 234 |
ADDMETHOD_NOBASE_VOIDRETURN(dropDeviceReference, (void *dev)); |
| 235 |
}; |
| 236 |
|
| 237 |
struct MockEventsFwInterface : public FirmwareUtil { |
| 238 |
bool mockIfrStatus = false; |
| 239 |
ze_result_t fwIfrApplied(bool &ifrStatus) override { |
| 240 |
ifrStatus = mockIfrStatus; |
| 241 |
return ZE_RESULT_SUCCESS; |
| 242 |
} |
| 243 |
MockEventsFwInterface() = default; |
| 244 |
|
| 245 |
ADDMETHOD_NOBASE(fwDeviceInit, ze_result_t, ZE_RESULT_SUCCESS, (void)); |
| 246 |
ADDMETHOD_NOBASE(getFirstDevice, ze_result_t, ZE_RESULT_SUCCESS, (IgscDeviceInfo * info)); |
| 247 |
ADDMETHOD_NOBASE(getFwVersion, ze_result_t, ZE_RESULT_SUCCESS, (std::string fwType, std::string &firmwareVersion)); |
| 248 |
ADDMETHOD_NOBASE(flashFirmware, ze_result_t, ZE_RESULT_SUCCESS, (std::string fwType, void *pImage, uint32_t size)); |
| 249 |
ADDMETHOD_NOBASE(fwSupportedDiagTests, ze_result_t, ZE_RESULT_SUCCESS, (std::vector<std::string> & supportedDiagTests)); |
| 250 |
ADDMETHOD_NOBASE(fwRunDiagTests, ze_result_t, ZE_RESULT_SUCCESS, (std::string & osDiagType, zes_diag_result_t *pResult)); |
| 251 |
ADDMETHOD_NOBASE(fwGetMemoryErrorCount, ze_result_t, ZE_RESULT_SUCCESS, (zes_ras_error_type_t category, uint32_t subDeviceCount, uint32_t subDeviceId, uint64_t &count)); |
| 252 |
ADDMETHOD_NOBASE(fwGetEccConfig, ze_result_t, ZE_RESULT_SUCCESS, (uint8_t * currentState, uint8_t *pendingState)); |
| 253 |
ADDMETHOD_NOBASE(fwSetEccConfig, ze_result_t, ZE_RESULT_SUCCESS, (uint8_t newState, uint8_t *currentState, uint8_t *pendingState)); |
| 254 |
ADDMETHOD_NOBASE_VOIDRETURN(getDeviceSupportedFwTypes, (std::vector<std::string> & fwTypes)); |
| 255 |
ADDMETHOD_NOBASE_VOIDRETURN(fwGetMemoryHealthIndicator, (zes_mem_health_t * health)); |
| 256 |
}; |
| 257 |
|
| 258 |
struct MockEventNeoDrm : public Drm { |
| 259 |
using Drm::ioctlHelper; |
| 260 |
uint32_t mockMemoryType = NEO::DeviceBlobConstants::MemoryType::hbm2e; |
| 261 |
const int mockFd = 33; |
| 262 |
std::vector<bool> mockQuerySystemInfoReturnValue{}; |
| 263 |
bool isRepeated = false; |
| 264 |
bool mockReturnEmptyRegions = false; |
| 265 |
MockEventNeoDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique<HwDeviceIdDrm>(mockFd, ""), rootDeviceEnvironment) {} |
| 266 |
|
| 267 |
void setMemoryType(uint32_t memory) { |
| 268 |
mockMemoryType = memory; |
| 269 |
} |
| 270 |
|
| 271 |
std::vector<uint64_t> getMemoryRegionsReturnsEmpty() { |
| 272 |
return {}; |
| 273 |
} |
| 274 |
|
| 275 |
bool querySystemInfo() override { |
| 276 |
bool returnValue = true; |
| 277 |
if (!mockQuerySystemInfoReturnValue.empty()) { |
| 278 |
returnValue = mockQuerySystemInfoReturnValue.front(); |
| 279 |
if (isRepeated != true) { |
| 280 |
mockQuerySystemInfoReturnValue.erase(mockQuerySystemInfoReturnValue.begin()); |
| 281 |
} |
| 282 |
return returnValue; |
| 283 |
} |
| 284 |
|
| 285 |
uint32_t hwBlob[] = {NEO::DeviceBlobConstants::maxMemoryChannels, 1, 8, NEO::DeviceBlobConstants::memoryType, 0, mockMemoryType}; |
| 286 |
std::vector<uint32_t> inputBlobData(reinterpret_cast<uint32_t *>(hwBlob), reinterpret_cast<uint32_t *>(ptrOffset(hwBlob, sizeof(hwBlob)))); |
| 287 |
this->systemInfo.reset(new SystemInfo(inputBlobData)); |
| 288 |
return returnValue; |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
class PublicLinuxEventsImp : public L0::LinuxEventsImp { |
| 293 |
public: |
| 294 |
PublicLinuxEventsImp(OsSysman *pOsSysman) : LinuxEventsImp(pOsSysman) {} |
| 295 |
}; |
| 296 |
|
| 297 |
class PublicLinuxEventsUtil : public L0::LinuxEventsUtil { |
| 298 |
public: |
| 299 |
PublicLinuxEventsUtil() : LinuxEventsUtil() {} |
| 300 |
using LinuxEventsUtil::deviceEventsMap; |
| 301 |
using LinuxEventsUtil::listenSystemEvents; |
| 302 |
using LinuxEventsUtil::pipeFd; |
| 303 |
using LinuxEventsUtil::pUdevLib; |
| 304 |
}; |
| 305 |
|
| 306 |
} // namespace ult |
| 307 |
} // namespace L0 |