| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2021 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <catch2/trompeloeil.hpp> |
| 6 | |
| 7 | #include "common/commandqueuestub.h" |
| 8 | #include "common/vectorstringdatasourcestub.h" |
| 9 | #include "core/components/controls/amd/pm/advanced/overdrive/freqrange/pmfreqrange.h" |
| 10 | #include <units.h> |
| 11 | |
| 12 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 13 | |
| 14 | namespace Tests::AMD::PMFreqRange { |
| 15 | |
| 16 | class PMFreqRangeTestAdapter : public ::AMD::PMFreqRange |
| 17 | { |
| 18 | public: |
| 19 | using ::AMD::PMFreqRange::PMFreqRange; |
| 20 | |
| 21 | using ::AMD::PMFreqRange::cleanControl; |
| 22 | using ::AMD::PMFreqRange::controlCmdId; |
| 23 | using ::AMD::PMFreqRange::controlName; |
| 24 | using ::AMD::PMFreqRange::exportControl; |
| 25 | using ::AMD::PMFreqRange::importControl; |
| 26 | using ::AMD::PMFreqRange::state; |
| 27 | using ::AMD::PMFreqRange::stateRange; |
| 28 | using ::AMD::PMFreqRange::states; |
| 29 | using ::AMD::PMFreqRange::syncControl; |
| 30 | }; |
| 31 | |
| 32 | class PMFreqRangeImporterStub final : public ::AMD::PMFreqRange::Importer |
| 33 | { |
| 34 | public: |
| 35 | PMFreqRangeImporterStub( |
| 36 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> const |
| 37 | &states) |
| 38 | : states_(states) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 43 | provideImporter(Item const &) override |
| 44 | { |
| 45 | return {}; |
| 46 | } |
| 47 | |
| 48 | bool provideActive() const override |
| 49 | { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | units::frequency::megahertz_t |
| 54 | providePMFreqRangeState(unsigned int index) const override |
| 55 | { |
| 56 | auto stateIt = std::find_if( |
| 57 | states_.cbegin(), states_.cend(), |
| 58 | [=](auto const &state) { return state.first == index; }); |
| 59 | |
| 60 | return stateIt->second; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> const states_; |
| 65 | }; |
| 66 | |
| 67 | class PMFreqRangeExporterMock : public ::AMD::PMFreqRange::Exporter |
| 68 | { |
| 69 | public: |
| 70 | MAKE_MOCK1(takePMFreqRangeControlName, void(std::string const &), override); |
| 71 | MAKE_MOCK2(takePMFreqRangeStateRange, |
| 72 | void(units::frequency::megahertz_t, units::frequency::megahertz_t), |
| 73 | override); |
| 74 | MAKE_MOCK1( |
| 75 | takePMFreqRangeStates, |
| 76 | void(std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> const |
| 77 | &), |
| 78 | override); |
| 79 | |
| 80 | MAKE_MOCK1(takeActive, void(bool), override); |
| 81 | MAKE_MOCK1( |
| 82 | provideExporter, |
| 83 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 84 | override); |
| 85 | }; |
| 86 | |
| 87 | TEST_CASE("AMD PMFreqRange tests", |
| 88 | "[GPU][AMD][PM][PMAdvanced][PMOverdrive][PMFreqRange]") |
| 89 | { |
| 90 | // clang-format off |
| 91 | std::vector<std::string> ppOdClkVoltageData { |
| 92 | "OD_SCLK:", |
| 93 | "0: 200MHz", |
| 94 | "1: 1000MHz", |
| 95 | "OD_RANGE:", |
| 96 | "SCLK: 200MHz 2000MHz"}; |
| 97 | // clang-format on |
| 98 | CommandQueueStub ctlCmds; |
| 99 | |
| 100 | SECTION("Has PMFreqRange ID") |
| 101 | { |
| 102 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 103 | std::make_unique<VectorStringDataSourceStub>()); |
| 104 | REQUIRE(ts.ID() == ::AMD::PMFreqRange::ItemID); |
| 105 | } |
| 106 | |
| 107 | SECTION("Is active by default") |
| 108 | { |
| 109 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 110 | std::make_unique<VectorStringDataSourceStub>()); |
| 111 | REQUIRE(ts.active()); |
| 112 | } |
| 113 | |
| 114 | SECTION("Has overdrive control name") |
| 115 | { |
| 116 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 117 | std::make_unique<VectorStringDataSourceStub>()); |
| 118 | REQUIRE(ts.controlName() == "SCLK"); |
| 119 | } |
| 120 | |
| 121 | SECTION("Has overdrive control command id") |
| 122 | { |
| 123 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 124 | std::make_unique<VectorStringDataSourceStub>()); |
| 125 | REQUIRE(ts.controlCmdId() == "s"); |
| 126 | } |
| 127 | |
| 128 | SECTION("Does not generate pre-init control commands") |
| 129 | { |
| 130 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 131 | std::make_unique<VectorStringDataSourceStub>( |
| 132 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 133 | ts.preInit(ctlCmds); |
| 134 | |
| 135 | auto &commands = ctlCmds.commands(); |
| 136 | REQUIRE(commands.empty()); |
| 137 | } |
| 138 | |
| 139 | SECTION("Generates post-init control commands") |
| 140 | { |
| 141 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 142 | std::make_unique<VectorStringDataSourceStub>( |
| 143 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 144 | ts.preInit(ctlCmds); |
| 145 | ctlCmds.clear(); |
| 146 | ts.postInit(ctlCmds); |
| 147 | |
| 148 | auto &commands = ctlCmds.commands(); |
| 149 | REQUIRE(commands.size() == 2); |
| 150 | |
| 151 | auto &[cmd0Path, cmd0Value] = commands.at(0); |
| 152 | REQUIRE(cmd0Path == "pp_od_clk_voltage"); |
| 153 | REQUIRE(cmd0Value == "s 0 200"); |
| 154 | |
| 155 | auto &[cmd1Path, cmd1Value] = commands.at(1); |
| 156 | REQUIRE(cmd1Path == "pp_od_clk_voltage"); |
| 157 | REQUIRE(cmd1Value == "s 1 1000"); |
| 158 | } |
| 159 | |
| 160 | SECTION( |
| 161 | "Does not generate post-init control commands for disabled bound states") |
| 162 | { |
| 163 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 164 | std::make_unique<VectorStringDataSourceStub>( |
| 165 | "pp_od_clk_voltage", ppOdClkVoltageData), |
| 166 | ::AMD::PMFreqRange::DisabledBound{1}); |
| 167 | ts.preInit(ctlCmds); |
| 168 | ctlCmds.clear(); |
| 169 | ts.postInit(ctlCmds); |
| 170 | |
| 171 | auto &commands = ctlCmds.commands(); |
| 172 | REQUIRE(commands.size() == 1); |
| 173 | |
| 174 | auto &[cmd0Path, cmd0Value] = commands.at(0); |
| 175 | REQUIRE(cmd0Path == "pp_od_clk_voltage"); |
| 176 | REQUIRE(cmd0Value == "s 0 200"); |
| 177 | } |
| 178 | |
| 179 | SECTION("Initializes states and range from pp_od_clk_voltage data source") |
| 180 | { |
| 181 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 182 | std::make_unique<VectorStringDataSourceStub>( |
| 183 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 184 | ts.init(); |
| 185 | |
| 186 | auto states = ts.states(); |
| 187 | REQUIRE(states.size() == 2); |
| 188 | |
| 189 | auto &[s0Index, s0Freq] = states.at(0); |
| 190 | REQUIRE(s0Index == 0); |
| 191 | REQUIRE(s0Freq == units::frequency::megahertz_t(200)); |
| 192 | |
| 193 | auto &[s1Index, s1Freq] = states.at(1); |
| 194 | REQUIRE(s1Index == 1); |
| 195 | REQUIRE(s1Freq == units::frequency::megahertz_t(1000)); |
| 196 | |
| 197 | auto &[min, max] = ts.stateRange(); |
| 198 | REQUIRE(min == units::frequency::megahertz_t(200)); |
| 199 | REQUIRE(max == units::frequency::megahertz_t(2000)); |
| 200 | } |
| 201 | |
| 202 | SECTION("Initializes states and range from pp_od_clk_voltage data source, " |
| 203 | "skipping disabled bound states") |
| 204 | { |
| 205 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 206 | std::make_unique<VectorStringDataSourceStub>( |
| 207 | "pp_od_clk_voltage", ppOdClkVoltageData), |
| 208 | ::AMD::PMFreqRange::DisabledBound{1}); |
| 209 | ts.init(); |
| 210 | |
| 211 | auto states = ts.states(); |
| 212 | REQUIRE(states.size() == 1); |
| 213 | |
| 214 | auto &[s0Index, s0Freq] = states.at(0); |
| 215 | REQUIRE(s0Index == 0); |
| 216 | REQUIRE(s0Freq == units::frequency::megahertz_t(200)); |
| 217 | |
| 218 | auto &[min, max] = ts.stateRange(); |
| 219 | REQUIRE(min == units::frequency::megahertz_t(200)); |
| 220 | REQUIRE(max == units::frequency::megahertz_t(2000)); |
| 221 | } |
| 222 | |
| 223 | SECTION("Clamps state frequency value in range") |
| 224 | { |
| 225 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 226 | std::make_unique<VectorStringDataSourceStub>( |
| 227 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 228 | ts.init(); |
| 229 | |
| 230 | // min |
| 231 | ts.state(0, units::frequency::megahertz_t(0)); |
| 232 | // max |
| 233 | ts.state(1, units::frequency::megahertz_t(10000)); |
| 234 | |
| 235 | auto states = ts.states(); |
| 236 | REQUIRE(states.size() == 2); |
| 237 | |
| 238 | // min |
| 239 | auto &[s0Index, s0Freq] = states.at(0); |
| 240 | REQUIRE(s0Index == 0); |
| 241 | REQUIRE(s0Freq == units::frequency::megahertz_t(200)); |
| 242 | |
| 243 | // max |
| 244 | auto &[s1Index, s1Freq] = states.at(1); |
| 245 | REQUIRE(s1Index == 1); |
| 246 | REQUIRE(s1Freq == units::frequency::megahertz_t(2000)); |
| 247 | } |
| 248 | |
| 249 | SECTION("Imports its state") |
| 250 | { |
| 251 | std::vector<unsigned int> gpuActiveStates{0, 1}; |
| 252 | std::vector<unsigned int> memActiveStates{1}; |
| 253 | |
| 254 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 255 | std::make_unique<VectorStringDataSourceStub>( |
| 256 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 257 | ts.init(); |
| 258 | |
| 259 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> states; |
| 260 | states.emplace_back(0, units::frequency::megahertz_t(201)); |
| 261 | states.emplace_back(1, units::frequency::megahertz_t(1001)); |
| 262 | |
| 263 | PMFreqRangeImporterStub i(states); |
| 264 | |
| 265 | ts.importControl(i); |
| 266 | |
| 267 | REQUIRE(ts.states() == states); |
| 268 | } |
| 269 | |
| 270 | SECTION("Exports its state") |
| 271 | { |
| 272 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 273 | std::make_unique<VectorStringDataSourceStub>( |
| 274 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 275 | ts.init(); |
| 276 | |
| 277 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> states; |
| 278 | states.emplace_back(0, units::frequency::megahertz_t(200)); |
| 279 | states.emplace_back(1, units::frequency::megahertz_t(1000)); |
| 280 | |
| 281 | trompeloeil::sequence seq; |
| 282 | PMFreqRangeExporterMock e; |
| 283 | REQUIRE_CALL(e, takePMFreqRangeControlName(trompeloeil::_)) |
| 284 | .LR_WITH(_1 == "SCLK") |
| 285 | .IN_SEQUENCE(seq); |
| 286 | REQUIRE_CALL(e, takePMFreqRangeStateRange( |
| 287 | trompeloeil::eq(units::frequency::megahertz_t(200)), |
| 288 | trompeloeil::eq(units::frequency::megahertz_t(2000)))) |
| 289 | .IN_SEQUENCE(seq); |
| 290 | REQUIRE_CALL(e, takePMFreqRangeStates(trompeloeil::_)) |
| 291 | .LR_WITH(_1 == states) |
| 292 | .IN_SEQUENCE(seq); |
| 293 | |
| 294 | ts.exportControl(e); |
| 295 | } |
| 296 | |
| 297 | SECTION("Does not generate clean control commands") |
| 298 | { |
| 299 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 300 | std::make_unique<VectorStringDataSourceStub>( |
| 301 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 302 | ts.init(); |
| 303 | ts.cleanControl(ctlCmds); |
| 304 | |
| 305 | auto &commands = ctlCmds.commands(); |
| 306 | REQUIRE(commands.empty()); |
| 307 | } |
| 308 | |
| 309 | SECTION("Does not generate sync control commands when is synced") |
| 310 | { |
| 311 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 312 | std::make_unique<VectorStringDataSourceStub>( |
| 313 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 314 | ts.init(); |
| 315 | ts.syncControl(ctlCmds); |
| 316 | |
| 317 | REQUIRE(ctlCmds.commands().empty()); |
| 318 | } |
| 319 | |
| 320 | SECTION("Generates sync control commands when is out of sync") |
| 321 | { |
| 322 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 323 | std::make_unique<VectorStringDataSourceStub>( |
| 324 | "pp_od_clk_voltage", ppOdClkVoltageData)); |
| 325 | ts.init(); |
| 326 | |
| 327 | ts.state(0, units::frequency::megahertz_t(201)); |
| 328 | ts.syncControl(ctlCmds); |
| 329 | |
| 330 | auto &commands = ctlCmds.commands(); |
| 331 | REQUIRE(commands.size() == 1); |
| 332 | |
| 333 | auto &[cmd0Path, cmd0Value] = commands.at(0); |
| 334 | REQUIRE(cmd0Path == "pp_od_clk_voltage"); |
| 335 | REQUIRE(cmd0Value == "s 0 201"); |
| 336 | } |
| 337 | |
| 338 | SECTION("Generated sync control commands ignores disabled bound state") |
| 339 | { |
| 340 | // clang-format off |
| 341 | std::vector<std::string> ppOdClkVoltageData { |
| 342 | "OD_SCLK:", |
| 343 | "0: 10MHz", // Bogus state that will be disabled |
| 344 | "1: 1000MHz", |
| 345 | "OD_RANGE:", |
| 346 | "SCLK: 200MHz 2000MHz"}; |
| 347 | // clang-format on |
| 348 | |
| 349 | PMFreqRangeTestAdapter ts("SCLK", "s", |
| 350 | std::make_unique<VectorStringDataSourceStub>( |
| 351 | "pp_od_clk_voltage", ppOdClkVoltageData), |
| 352 | ::AMD::PMFreqRange::DisabledBound{0}); |
| 353 | ts.init(); |
| 354 | ts.syncControl(ctlCmds); |
| 355 | |
| 356 | REQUIRE(ctlCmds.commands().empty()); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | } // namespace Tests::AMD::PMFreqRange |