Debian Codegraph @

level_zero/sysman/test/unit_tests/sources/events/linux/mock_events.h

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