| 1 |
/* |
| 2 |
* Copyright (C) 2020-2025 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
#include "level_zero/core/test/unit_tests/mock.h" |
| 10 |
#include "level_zero/tools/source/sysman/power/power_imp.h" |
| 11 |
#include "level_zero/tools/test/unit_tests/sources/sysman/windows/mock_kmd_sys_manager.h" |
| 12 |
|
| 13 |
namespace L0 { |
| 14 |
namespace ult { |
| 15 |
constexpr uint32_t mockLimitCount = 3u; |
| 16 |
|
| 17 |
class PowerKmdSysManager : public Mock<MockKmdSysManager> {}; |
| 18 |
|
| 19 |
template <> |
| 20 |
struct Mock<PowerKmdSysManager> : public PowerKmdSysManager { |
| 21 |
|
| 22 |
uint32_t mockPowerLimit1Enabled = 1; |
| 23 |
uint32_t mockPowerLimit2Enabled = 1; |
| 24 |
int32_t mockPowerLimit1 = 25000; |
| 25 |
int32_t mockPowerLimit2 = 41000; |
| 26 |
int32_t mockTauPowerLimit1 = 20800; |
| 27 |
uint32_t mockTpdDefault = 34000; |
| 28 |
uint32_t mockMinPowerLimit = 1000; |
| 29 |
uint32_t mockMaxPowerLimit = 80000; |
| 30 |
int32_t mockAcPowerPeak = 0; |
| 31 |
int32_t mockDcPowerPeak = 0; |
| 32 |
uint32_t mockEnergyThreshold = 0; |
| 33 |
uint32_t mockEnergyCounter = 3231121; |
| 34 |
uint32_t mockTimeStamp = 1123412412; |
| 35 |
uint32_t mockEnergyUnit = 14; |
| 36 |
uint64_t mockEnergyCounter64Bit = 32323232323232; |
| 37 |
uint32_t mockFrequencyTimeStamp = 38400000; |
| 38 |
uint32_t mockEnergyThresholdProcessId = 12345; |
| 39 |
uint32_t mockPowerFailure[KmdSysman::Requests::Power::MaxPowerRequests] = {0}; |
| 40 |
|
| 41 |
uint32_t getCurrentProcessId() { |
| 42 |
// For testing, we'll use a simulated value that can be verified |
| 43 |
return 999; // Test process ID |
| 44 |
} |
| 45 |
|
| 46 |
void getActivityProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { |
| 47 |
uint8_t *pBuffer = reinterpret_cast<uint8_t *>(pResponse); |
| 48 |
pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderOut); |
| 49 |
|
| 50 |
switch (pRequest->inRequestId) { |
| 51 |
case KmdSysman::Requests::Activity::TimestampFrequency: { |
| 52 |
uint32_t *pValueFrequency = reinterpret_cast<uint32_t *>(pBuffer); |
| 53 |
*pValueFrequency = mockFrequencyTimeStamp; |
| 54 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 55 |
pResponse->outDataSize = sizeof(uint32_t); |
| 56 |
} break; |
| 57 |
default: { |
| 58 |
pResponse->outDataSize = 0; |
| 59 |
pResponse->outReturnCode = KmdSysman::KmdSysmanFail; |
| 60 |
} break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
uint32_t getReturnCode(uint32_t powerRequestCode) { |
| 65 |
return mockPowerFailure[powerRequestCode] ? KmdSysman::KmdSysmanFail : KmdSysman::KmdSysmanSuccess; |
| 66 |
} |
| 67 |
|
| 68 |
void getPowerProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { |
| 69 |
uint8_t *pBuffer = reinterpret_cast<uint8_t *>(pResponse); |
| 70 |
pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderOut); |
| 71 |
|
| 72 |
switch (pRequest->inRequestId) { |
| 73 |
case KmdSysman::Requests::Power::EnergyThresholdSupported: { |
| 74 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 75 |
*pValue = static_cast<uint32_t>(this->allowSetCalls); |
| 76 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 77 |
pResponse->outDataSize = sizeof(uint32_t); |
| 78 |
} break; |
| 79 |
case KmdSysman::Requests::Power::TdpDefault: { |
| 80 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 81 |
*pValue = mockTpdDefault; |
| 82 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 83 |
pResponse->outDataSize = sizeof(uint32_t); |
| 84 |
} break; |
| 85 |
case KmdSysman::Requests::Power::MinPowerLimitDefault: { |
| 86 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 87 |
*pValue = mockMinPowerLimit; |
| 88 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 89 |
pResponse->outDataSize = sizeof(uint32_t); |
| 90 |
} break; |
| 91 |
case KmdSysman::Requests::Power::MaxPowerLimitDefault: { |
| 92 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 93 |
*pValue = mockMaxPowerLimit; |
| 94 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 95 |
pResponse->outDataSize = sizeof(uint32_t); |
| 96 |
} break; |
| 97 |
case KmdSysman::Requests::Power::PowerLimit1Enabled: { |
| 98 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 99 |
*pValue = mockPowerLimit1Enabled; |
| 100 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 101 |
pResponse->outDataSize = sizeof(uint32_t); |
| 102 |
} break; |
| 103 |
case KmdSysman::Requests::Power::PowerLimit2Enabled: { |
| 104 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 105 |
*pValue = mockPowerLimit2Enabled; |
| 106 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 107 |
pResponse->outDataSize = sizeof(uint32_t); |
| 108 |
} break; |
| 109 |
case KmdSysman::Requests::Power::CurrentPowerLimit1: { |
| 110 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 111 |
*pValue = mockPowerLimit1; |
| 112 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 113 |
pResponse->outDataSize = sizeof(int32_t); |
| 114 |
} break; |
| 115 |
case KmdSysman::Requests::Power::CurrentPowerLimit1Tau: { |
| 116 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 117 |
*pValue = mockTauPowerLimit1; |
| 118 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 119 |
pResponse->outDataSize = sizeof(int32_t); |
| 120 |
} break; |
| 121 |
case KmdSysman::Requests::Power::CurrentPowerLimit2: { |
| 122 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 123 |
*pValue = mockPowerLimit2; |
| 124 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 125 |
pResponse->outDataSize = sizeof(int32_t); |
| 126 |
} break; |
| 127 |
case KmdSysman::Requests::Power::CurrentPowerLimit4Ac: { |
| 128 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 129 |
*pValue = mockAcPowerPeak; |
| 130 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 131 |
pResponse->outDataSize = sizeof(int32_t); |
| 132 |
} break; |
| 133 |
case KmdSysman::Requests::Power::CurrentPowerLimit4Dc: { |
| 134 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 135 |
*pValue = mockDcPowerPeak; |
| 136 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 137 |
pResponse->outDataSize = sizeof(uint32_t); |
| 138 |
} break; |
| 139 |
case KmdSysman::Requests::Power::CurrentEnergyThreshold: { |
| 140 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 141 |
uint32_t *pProcessId = reinterpret_cast<uint32_t *>(pBuffer + sizeof(uint32_t)); |
| 142 |
*pValue = mockEnergyThreshold; |
| 143 |
*pProcessId = mockEnergyThresholdProcessId; |
| 144 |
pResponse->outReturnCode = getReturnCode(pRequest->inRequestId); |
| 145 |
pResponse->outDataSize = sizeof(uint32_t) + sizeof(uint32_t); |
| 146 |
} break; |
| 147 |
case KmdSysman::Requests::Power::CurrentEnergyCounter: { |
| 148 |
uint32_t *pValueCounter = reinterpret_cast<uint32_t *>(pBuffer); |
| 149 |
uint64_t *pValueTS = reinterpret_cast<uint64_t *>(pBuffer + sizeof(uint32_t)); |
| 150 |
*pValueCounter = mockEnergyCounter; |
| 151 |
*pValueTS = mockTimeStamp; |
| 152 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 153 |
pResponse->outDataSize = sizeof(uint32_t) + sizeof(uint64_t); |
| 154 |
} break; |
| 155 |
case KmdSysman::Requests::Power::EnergyCounterUnits: { |
| 156 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 157 |
*pValue = mockEnergyUnit; |
| 158 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 159 |
pResponse->outDataSize = sizeof(uint32_t); |
| 160 |
} break; |
| 161 |
case KmdSysman::Requests::Power::CurrentEnergyCounter64Bit: { |
| 162 |
uint64_t *pValueCounter = reinterpret_cast<uint64_t *>(pBuffer); |
| 163 |
uint64_t *pValueTS = reinterpret_cast<uint64_t *>(pBuffer + sizeof(uint64_t)); |
| 164 |
memcpy_s(pValueCounter, sizeof(uint64_t), &mockEnergyCounter64Bit, sizeof(mockEnergyCounter64Bit)); |
| 165 |
memcpy_s(pValueTS, sizeof(uint64_t), &mockTimeStamp, sizeof(mockEnergyCounter64Bit)); |
| 166 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 167 |
pResponse->outDataSize = sizeof(uint64_t) + sizeof(uint64_t); |
| 168 |
} break; |
| 169 |
default: { |
| 170 |
pResponse->outDataSize = 0; |
| 171 |
pResponse->outReturnCode = KmdSysman::KmdSysmanFail; |
| 172 |
} break; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void setPowerProperty(KmdSysman::GfxSysmanReqHeaderIn *pRequest, KmdSysman::GfxSysmanReqHeaderOut *pResponse) override { |
| 177 |
uint8_t *pBuffer = reinterpret_cast<uint8_t *>(pRequest); |
| 178 |
pBuffer += sizeof(KmdSysman::GfxSysmanReqHeaderIn); |
| 179 |
|
| 180 |
switch (pRequest->inRequestId) { |
| 181 |
case KmdSysman::Requests::Power::CurrentPowerLimit1: { |
| 182 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 183 |
mockPowerLimit1 = *pValue; |
| 184 |
pResponse->outDataSize = 0; |
| 185 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 186 |
} break; |
| 187 |
case KmdSysman::Requests::Power::CurrentPowerLimit1Tau: { |
| 188 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 189 |
mockTauPowerLimit1 = *pValue; |
| 190 |
pResponse->outDataSize = 0; |
| 191 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 192 |
} break; |
| 193 |
case KmdSysman::Requests::Power::CurrentPowerLimit2: { |
| 194 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 195 |
mockPowerLimit2 = *pValue; |
| 196 |
pResponse->outDataSize = 0; |
| 197 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 198 |
} break; |
| 199 |
case KmdSysman::Requests::Power::CurrentPowerLimit4Ac: { |
| 200 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 201 |
mockAcPowerPeak = *pValue; |
| 202 |
pResponse->outDataSize = 0; |
| 203 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 204 |
} break; |
| 205 |
case KmdSysman::Requests::Power::CurrentPowerLimit4Dc: { |
| 206 |
int32_t *pValue = reinterpret_cast<int32_t *>(pBuffer); |
| 207 |
mockDcPowerPeak = *pValue; |
| 208 |
pResponse->outDataSize = 0; |
| 209 |
pResponse->outReturnCode = KmdSysman::KmdSysmanSuccess; |
| 210 |
} break; |
| 211 |
case KmdSysman::Requests::Power::CurrentEnergyThreshold: { |
| 212 |
uint32_t *pValue = reinterpret_cast<uint32_t *>(pBuffer); |
| 213 |
pResponse->outReturnCode = getReturnCode(pRequest->inRequestId); |
| 214 |
if (!pResponse->outReturnCode) { |
| 215 |
mockEnergyThreshold = *pValue; |
| 216 |
mockEnergyThresholdProcessId = getCurrentProcessId(); |
| 217 |
pResponse->outDataSize = 0; |
| 218 |
} |
| 219 |
} break; |
| 220 |
default: { |
| 221 |
pResponse->outDataSize = 0; |
| 222 |
pResponse->outReturnCode = KmdSysman::KmdSysmanFail; |
| 223 |
} break; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
Mock() = default; |
| 228 |
~Mock() = default; |
| 229 |
}; |
| 230 |
|
| 231 |
} // namespace ult |
| 232 |
} // namespace L0 |