| 1 |
/* |
| 2 |
* Copyright (C) 2020-2026 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "shared/source/execution_environment/root_device_environment.h" |
| 11 |
#include "shared/source/os_interface/device_factory.h" |
| 12 |
#include "shared/source/os_interface/linux/drm_neo.h" |
| 13 |
#include "shared/source/os_interface/os_interface.h" |
| 14 |
#include "shared/test/common/mocks/mock_device.h" |
| 15 |
|
| 16 |
#include "level_zero/core/source/device/device.h" |
| 17 |
#include "level_zero/core/source/driver/driver.h" |
| 18 |
#include "level_zero/core/test/unit_tests/fixtures/device_fixture.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/udev/udev_lib.h" |
| 22 |
#include "level_zero/tools/source/sysman/sysman.h" |
| 23 |
#include "level_zero/tools/source/sysman/sysman_imp.h" |
| 24 |
#include "level_zero/tools/test/unit_tests/sources/sysman/firmware_util/mock_fw_util_fixture.h" |
| 25 |
#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_procfs_access_fixture.h" |
| 26 |
#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysfs_access_fixture.h" |
| 27 |
|
| 28 |
extern bool sysmanUltsEnable; |
| 29 |
|
| 30 |
using namespace NEO; |
| 31 |
|
| 32 |
namespace L0 { |
| 33 |
namespace ult { |
| 34 |
constexpr int mockFd = 0; |
| 35 |
class SysmanMockDrm : public Drm { |
| 36 |
public: |
| 37 |
SysmanMockDrm(RootDeviceEnvironment &rootDeviceEnvironment) : Drm(std::make_unique<HwDeviceIdDrm>(mockFd, ""), rootDeviceEnvironment) { |
| 38 |
setupIoctlHelper(rootDeviceEnvironment.getHardwareInfo()->platform.eProductFamily); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
class PublicLinuxSysmanImp : public L0::LinuxSysmanImp { |
| 43 |
public: |
| 44 |
using LinuxSysmanImp::mapOfSubDeviceIdToPmtObject; |
| 45 |
using LinuxSysmanImp::memType; |
| 46 |
using LinuxSysmanImp::pDrm; |
| 47 |
using LinuxSysmanImp::pFsAccess; |
| 48 |
using LinuxSysmanImp::pFwUtilInterface; |
| 49 |
using LinuxSysmanImp::pPmuInterface; |
| 50 |
using LinuxSysmanImp::pProcfsAccess; |
| 51 |
using LinuxSysmanImp::pSysfsAccess; |
| 52 |
}; |
| 53 |
|
| 54 |
class SysmanDeviceFixture : public DeviceFixture, public ::testing::Test { |
| 55 |
public: |
| 56 |
MockLinuxSysfsAccess *pSysfsAccess = nullptr; |
| 57 |
MockLinuxProcfsAccess *pProcfsAccess = nullptr; |
| 58 |
MockFwUtilInterface *pFwUtilInterface = nullptr; |
| 59 |
void SetUp() override { |
| 60 |
if (!sysmanUltsEnable) { |
| 61 |
GTEST_SKIP(); |
| 62 |
} |
| 63 |
debugManager.flags.EnableHostUsmAllocationPool.set(0); |
| 64 |
debugManager.flags.EnableDeviceUsmAllocationPool.set(0); |
| 65 |
DeviceFixture::setUp(); |
| 66 |
neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique<NEO::OSInterface>(); |
| 67 |
auto &osInterface = *device->getOsInterface(); |
| 68 |
osInterface.setDriverModel(std::make_unique<SysmanMockDrm>(const_cast<NEO::RootDeviceEnvironment &>(neoDevice->getRootDeviceEnvironment()))); |
| 69 |
setenv("ZES_ENABLE_SYSMAN", "1", 1); |
| 70 |
device->setSysmanHandle(new SysmanDeviceImp(device->toHandle())); |
| 71 |
pSysmanDevice = device->getSysmanHandle(); |
| 72 |
pSysmanDeviceImp = static_cast<SysmanDeviceImp *>(pSysmanDevice); |
| 73 |
pOsSysman = pSysmanDeviceImp->pOsSysman; |
| 74 |
pLinuxSysmanImp = static_cast<PublicLinuxSysmanImp *>(pOsSysman); |
| 75 |
|
| 76 |
pFwUtilInterface = new MockFwUtilInterface(); |
| 77 |
pSysfsAccess = new MockLinuxSysfsAccess; |
| 78 |
pProcfsAccess = new MockLinuxProcfsAccess; |
| 79 |
pLinuxSysmanImp->pFwUtilInterface = pFwUtilInterface; |
| 80 |
pLinuxSysmanImp->pSysfsAccess = pSysfsAccess; |
| 81 |
pLinuxSysmanImp->pProcfsAccess = pProcfsAccess; |
| 82 |
|
| 83 |
if (globalOsSysmanDriver == nullptr) { |
| 84 |
globalOsSysmanDriver = L0::OsSysmanDriver::create(); |
| 85 |
} |
| 86 |
|
| 87 |
pSysmanDeviceImp->init(); |
| 88 |
L0::sysmanInitFromCore = true; |
| 89 |
} |
| 90 |
void TearDown() override { |
| 91 |
if (!sysmanUltsEnable) { |
| 92 |
GTEST_SKIP(); |
| 93 |
} |
| 94 |
|
| 95 |
if (globalOsSysmanDriver != nullptr) { |
| 96 |
delete globalOsSysmanDriver; |
| 97 |
globalOsSysmanDriver = nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
DeviceFixture::tearDown(); |
| 101 |
unsetenv("ZES_ENABLE_SYSMAN"); |
| 102 |
} |
| 103 |
DebugManagerStateRestore restorer; |
| 104 |
SysmanDevice *pSysmanDevice = nullptr; |
| 105 |
SysmanDeviceImp *pSysmanDeviceImp = nullptr; |
| 106 |
OsSysman *pOsSysman = nullptr; |
| 107 |
PublicLinuxSysmanImp *pLinuxSysmanImp = nullptr; |
| 108 |
}; |
| 109 |
|
| 110 |
class SysmanMultiDeviceFixture : public MultiDeviceFixture, public ::testing::Test { |
| 111 |
public: |
| 112 |
MockLinuxSysfsAccess *pSysfsAccess = nullptr; |
| 113 |
MockLinuxProcfsAccess *pProcfsAccess = nullptr; |
| 114 |
MockFwUtilInterface *pFwUtilInterface = nullptr; |
| 115 |
void SetUp() override { |
| 116 |
if (!sysmanUltsEnable) { |
| 117 |
GTEST_SKIP(); |
| 118 |
} |
| 119 |
MultiDeviceFixture::setUp(); |
| 120 |
device = driverHandle->devices[0]; |
| 121 |
neoDevice = device->getNEODevice(); |
| 122 |
neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique<NEO::OSInterface>(); |
| 123 |
auto &osInterface = *device->getOsInterface(); |
| 124 |
osInterface.setDriverModel(std::make_unique<SysmanMockDrm>(const_cast<NEO::RootDeviceEnvironment &>(neoDevice->getRootDeviceEnvironment()))); |
| 125 |
setenv("ZES_ENABLE_SYSMAN", "1", 1); |
| 126 |
device->setSysmanHandle(new SysmanDeviceImp(device->toHandle())); |
| 127 |
pSysmanDevice = device->getSysmanHandle(); |
| 128 |
for (auto &subDevice : device->subDevices) { |
| 129 |
static_cast<Device *>(subDevice)->setSysmanHandle(pSysmanDevice); |
| 130 |
} |
| 131 |
|
| 132 |
pSysmanDeviceImp = static_cast<SysmanDeviceImp *>(pSysmanDevice); |
| 133 |
pOsSysman = pSysmanDeviceImp->pOsSysman; |
| 134 |
pLinuxSysmanImp = static_cast<PublicLinuxSysmanImp *>(pOsSysman); |
| 135 |
|
| 136 |
pFwUtilInterface = new MockFwUtilInterface(); |
| 137 |
pSysfsAccess = new MockLinuxSysfsAccess; |
| 138 |
pProcfsAccess = new MockLinuxProcfsAccess; |
| 139 |
pLinuxSysmanImp->pFwUtilInterface = pFwUtilInterface; |
| 140 |
pLinuxSysmanImp->pSysfsAccess = pSysfsAccess; |
| 141 |
pLinuxSysmanImp->pProcfsAccess = pProcfsAccess; |
| 142 |
|
| 143 |
if (globalOsSysmanDriver == nullptr) { |
| 144 |
globalOsSysmanDriver = L0::OsSysmanDriver::create(); |
| 145 |
} |
| 146 |
|
| 147 |
pSysmanDeviceImp->init(); |
| 148 |
subDeviceCount = numSubDevices; |
| 149 |
L0::sysmanInitFromCore = true; |
| 150 |
} |
| 151 |
void TearDown() override { |
| 152 |
if (!sysmanUltsEnable) { |
| 153 |
GTEST_SKIP(); |
| 154 |
} |
| 155 |
if (globalOsSysmanDriver != nullptr) { |
| 156 |
delete globalOsSysmanDriver; |
| 157 |
globalOsSysmanDriver = nullptr; |
| 158 |
} |
| 159 |
unsetenv("ZES_ENABLE_SYSMAN"); |
| 160 |
MultiDeviceFixture::tearDown(); |
| 161 |
} |
| 162 |
|
| 163 |
SysmanDevice *pSysmanDevice = nullptr; |
| 164 |
SysmanDeviceImp *pSysmanDeviceImp = nullptr; |
| 165 |
OsSysman *pOsSysman = nullptr; |
| 166 |
PublicLinuxSysmanImp *pLinuxSysmanImp = nullptr; |
| 167 |
NEO::Device *neoDevice = nullptr; |
| 168 |
L0::Device *device = nullptr; |
| 169 |
uint32_t subDeviceCount = 0u; |
| 170 |
}; |
| 171 |
|
| 172 |
class PublicFsAccess : public L0::FsAccess { |
| 173 |
public: |
| 174 |
using FsAccess::accessSyscall; |
| 175 |
using FsAccess::statSyscall; |
| 176 |
}; |
| 177 |
|
| 178 |
class PublicSysfsAccess : public L0::SysfsAccess { |
| 179 |
public: |
| 180 |
using SysfsAccess::accessSyscall; |
| 181 |
using SysfsAccess::statSyscall; |
| 182 |
}; |
| 183 |
|
| 184 |
class UdevLibMock : public UdevLib { |
| 185 |
public: |
| 186 |
UdevLibMock() = default; |
| 187 |
|
| 188 |
ADDMETHOD_NOBASE(registerEventsFromSubsystemAndGetFd, int, mockFd, (std::vector<std::string> & subsystemList)); |
| 189 |
ADDMETHOD_NOBASE(getEventGenerationSourceDevice, dev_t, 0, (void *dev)); |
| 190 |
ADDMETHOD_NOBASE(getEventType, const char *, "change", (void *dev)); |
| 191 |
ADDMETHOD_NOBASE(getEventPropertyValue, const char *, "MOCK", (void *dev, const char *key)); |
| 192 |
ADDMETHOD_NOBASE(allocateDeviceToReceiveData, void *, (void *)(0x12345678), ()); |
| 193 |
ADDMETHOD_NOBASE_VOIDRETURN(dropDeviceReference, (void *dev)); |
| 194 |
}; |
| 195 |
|
| 196 |
class PublicLinuxSysmanDriverImp : public L0::LinuxSysmanDriverImp { |
| 197 |
public: |
| 198 |
PublicLinuxSysmanDriverImp() : LinuxSysmanDriverImp() {} |
| 199 |
using LinuxSysmanDriverImp::pLinuxEventsUtil; |
| 200 |
using LinuxSysmanDriverImp::pUdevLib; |
| 201 |
}; |
| 202 |
|
| 203 |
} // namespace ult |
| 204 |
} // namespace L0 |