| 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/stringdatasourcestub.h" |
| 9 | #include "common/vectorstringdatasourcestub.h" |
| 10 | #include "core/components/controls/amd/pm/advanced/overdrive/voltcurve/pmvoltcurve.h" |
| 11 | |
| 12 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 13 | |
| 14 | namespace Tests::AMD::PMVoltCurve { |
| 15 | |
| 16 | class PMVoltCurveTestAdapter : public ::AMD::PMVoltCurve |
| 17 | { |
| 18 | public: |
| 19 | using ::AMD::PMVoltCurve::PMVoltCurve; |
| 20 | |
| 21 | using ::AMD::PMVoltCurve::cleanControl; |
| 22 | using ::AMD::PMVoltCurve::controlCmdId; |
| 23 | using ::AMD::PMVoltCurve::exportControl; |
| 24 | using ::AMD::PMVoltCurve::importControl; |
| 25 | using ::AMD::PMVoltCurve::mode; |
| 26 | using ::AMD::PMVoltCurve::modes; |
| 27 | using ::AMD::PMVoltCurve::point; |
| 28 | using ::AMD::PMVoltCurve::points; |
| 29 | using ::AMD::PMVoltCurve::pointsRange; |
| 30 | using ::AMD::PMVoltCurve::syncControl; |
| 31 | }; |
| 32 | |
| 33 | class PMVoltCurveImporterStub final : public ::AMD::PMVoltCurve::Importer |
| 34 | { |
| 35 | public: |
| 36 | PMVoltCurveImporterStub( |
| 37 | std::string const &mode, |
| 38 | std::vector<std::pair<units::frequency::megahertz_t, |
| 39 | units::voltage::millivolt_t>> const points) |
| 40 | : mode_(mode) |
| 41 | , points_(points) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 46 | provideImporter(Item const &) override |
| 47 | { |
| 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | bool provideActive() const override |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | std::string const &providePMVoltCurveMode() const override |
| 57 | { |
| 58 | return mode_; |
| 59 | } |
| 60 | |
| 61 | std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t> |
| 62 | providePMVoltCurvePoint(unsigned int index) const override |
| 63 | { |
| 64 | return points_[index]; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | std::string const mode_; |
| 69 | std::vector<std::pair<units::frequency::megahertz_t, |
| 70 | units::voltage::millivolt_t>> const points_; |
| 71 | }; |
| 72 | |
| 73 | class PMVoltCurveExporterMock : public ::AMD::PMVoltCurve::Exporter |
| 74 | { |
| 75 | public: |
| 76 | MAKE_MOCK1(takePMVoltCurveModes, void(std::vector<std::string> const &), |
| 77 | override); |
| 78 | MAKE_MOCK1(takePMVoltCurveMode, void(std::string const &), override); |
| 79 | MAKE_MOCK1( |
| 80 | takePMVoltCurvePointsRange, |
| 81 | void(std::vector<std::pair< |
| 82 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>, |
| 83 | std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>>> const |
| 84 | &), |
| 85 | override); |
| 86 | MAKE_MOCK1(takePMVoltCurvePoints, |
| 87 | void(std::vector<std::pair<units::frequency::megahertz_t, |
| 88 | units::voltage::millivolt_t>> const &), |
| 89 | override); |
| 90 | MAKE_MOCK1(takeActive, void(bool), override); |
| 91 | MAKE_MOCK1( |
| 92 | provideExporter, |
| 93 | std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &), |
| 94 | override); |
| 95 | }; |
| 96 | |
| 97 | TEST_CASE("AMD PMVoltCurve tests", |
| 98 | "[GPU][AMD][PM][PMAdvanced][PMOverdrive][PMVoltCurve]") |
| 99 | { |
| 100 | // clang-format off |
| 101 | std::vector<std::string> ppOdClkVoltageData { |
| 102 | "OD_SCLK:", |
| 103 | "0: 200MHz", |
| 104 | "1: 1000MHz", |
| 105 | "OD_MCLK:", |
| 106 | "1: 1000MHz", |
| 107 | "OD_VDDC_CURVE:", |
| 108 | "0: 200MHz 800mV", |
| 109 | "1: 1000MHz 850mV", |
| 110 | "2: 2000MHz 900mV", |
| 111 | "OD_RANGE:", |
| 112 | "SCLK: 200MHz 2000MHz", |
| 113 | "MCLK: 300MHz 3000MHz", |
| 114 | "VDDC_CURVE_SCLK[0]: 200MHz 2000MHz", |
| 115 | "VDDC_CURVE_VOLT[0]: 800mV 900mV", |
| 116 | "VDDC_CURVE_SCLK[1]: 200MHz 2000MHz", |
| 117 | "VDDC_CURVE_VOLT[1]: 800mV 900mV", |
| 118 | "VDDC_CURVE_SCLK[2]: 200MHz 2000MHz", |
| 119 | "VDDC_CURVE_VOLT[2]: 800mV 900mV" }; |
| 120 | // clang-format on |
| 121 | CommandQueueStub ctlCmds; |
| 122 | |
| 123 | SECTION("Has PMVoltCurve ID") |
| 124 | { |
| 125 | PMVoltCurveTestAdapter ts("vc", |
| 126 | std::make_unique<VectorStringDataSourceStub>()); |
| 127 | REQUIRE(ts.ID() == ::AMD::PMVoltCurve::ItemID); |
| 128 | } |
| 129 | |
| 130 | SECTION("Is active by default") |
| 131 | { |
| 132 | PMVoltCurveTestAdapter ts("vc", |
| 133 | std::make_unique<VectorStringDataSourceStub>()); |
| 134 | REQUIRE(ts.active()); |
| 135 | } |
| 136 | |
| 137 | SECTION("Has 'auto' mode by default") |
| 138 | { |
| 139 | PMVoltCurveTestAdapter ts("vc", |
| 140 | std::make_unique<VectorStringDataSourceStub>()); |
| 141 | REQUIRE(ts.mode() == "auto"); |
| 142 | } |
| 143 | |
| 144 | SECTION("Has overdrive control command id") |
| 145 | { |
| 146 | PMVoltCurveTestAdapter ts("vc", |
| 147 | std::make_unique<VectorStringDataSourceStub>()); |
| 148 | REQUIRE(ts.controlCmdId() == "vc"); |
| 149 | } |
| 150 | |
| 151 | SECTION("Does not generate pre-init control commands") |
| 152 | { |
| 153 | PMVoltCurveTestAdapter ts( |
| 154 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 155 | ppOdClkVoltageData)); |
| 156 | ts.preInit(ctlCmds); |
| 157 | |
| 158 | auto &commands = ctlCmds.commands(); |
| 159 | REQUIRE(commands.empty()); |
| 160 | } |
| 161 | |
| 162 | SECTION("Generate post-init control commands") |
| 163 | { |
| 164 | PMVoltCurveTestAdapter ts( |
| 165 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 166 | ppOdClkVoltageData)); |
| 167 | ts.preInit(ctlCmds); |
| 168 | ctlCmds.clear(); |
| 169 | ts.postInit(ctlCmds); |
| 170 | |
| 171 | auto &commands = ctlCmds.commands(); |
| 172 | REQUIRE(commands.size() == 3); |
| 173 | |
| 174 | auto &[cmd0Path, cmd0Value] = commands.at(0); |
| 175 | REQUIRE(cmd0Path == "pp_od_clk_voltage"); |
| 176 | REQUIRE(cmd0Value == "vc 0 200 800"); |
| 177 | |
| 178 | auto &[cmd1Path, cmd1Value] = commands.at(1); |
| 179 | REQUIRE(cmd1Path == "pp_od_clk_voltage"); |
| 180 | REQUIRE(cmd1Value == "vc 1 1000 850"); |
| 181 | |
| 182 | auto &[cmd2Path, cmd2Value] = commands.at(2); |
| 183 | REQUIRE(cmd2Path == "pp_od_clk_voltage"); |
| 184 | REQUIRE(cmd2Value == "vc 2 2000 900"); |
| 185 | } |
| 186 | |
| 187 | SECTION("Initializes points and range from pp_od_clk_voltage data source") |
| 188 | { |
| 189 | PMVoltCurveTestAdapter ts( |
| 190 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 191 | ppOdClkVoltageData)); |
| 192 | ts.init(); |
| 193 | |
| 194 | auto &pointsRange = ts.pointsRange(); |
| 195 | REQUIRE(pointsRange.size() == 3); |
| 196 | |
| 197 | auto &[p0RangeFreq, p0RangeVolt] = pointsRange.at(0); |
| 198 | REQUIRE(p0RangeFreq.first == units::frequency::megahertz_t(200)); |
| 199 | REQUIRE(p0RangeFreq.second == units::frequency::megahertz_t(2000)); |
| 200 | REQUIRE(p0RangeVolt.first == units::voltage::millivolt_t(800)); |
| 201 | REQUIRE(p0RangeVolt.second == units::voltage::millivolt_t(900)); |
| 202 | |
| 203 | auto &[p1RangeFreq, p1RangeVolt] = pointsRange.at(1); |
| 204 | REQUIRE(p1RangeFreq.first == units::frequency::megahertz_t(200)); |
| 205 | REQUIRE(p1RangeFreq.second == units::frequency::megahertz_t(2000)); |
| 206 | REQUIRE(p1RangeVolt.first == units::voltage::millivolt_t(800)); |
| 207 | REQUIRE(p1RangeVolt.second == units::voltage::millivolt_t(900)); |
| 208 | |
| 209 | auto &[p2RangeFreq, p2RangeVolt] = pointsRange.at(2); |
| 210 | REQUIRE(p2RangeFreq.first == units::frequency::megahertz_t(200)); |
| 211 | REQUIRE(p2RangeFreq.second == units::frequency::megahertz_t(2000)); |
| 212 | REQUIRE(p2RangeVolt.first == units::voltage::millivolt_t(800)); |
| 213 | REQUIRE(p2RangeVolt.second == units::voltage::millivolt_t(900)); |
| 214 | |
| 215 | auto &points = ts.points(); |
| 216 | auto &[p0Freq, p0Volt] = points.at(0); |
| 217 | REQUIRE(p0Freq == units::frequency::megahertz_t(200)); |
| 218 | REQUIRE(p0Volt == units::voltage::millivolt_t(800)); |
| 219 | |
| 220 | auto &[p1Freq, p1Volt] = points.at(1); |
| 221 | REQUIRE(p1Freq == units::frequency::megahertz_t(1000)); |
| 222 | REQUIRE(p1Volt == units::voltage::millivolt_t(850)); |
| 223 | |
| 224 | auto &[p2Freq, p2Volt] = points.at(2); |
| 225 | REQUIRE(p2Freq == units::frequency::megahertz_t(2000)); |
| 226 | REQUIRE(p2Volt == units::voltage::millivolt_t(900)); |
| 227 | } |
| 228 | |
| 229 | SECTION("Clamps point values in range") |
| 230 | { |
| 231 | PMVoltCurveTestAdapter ts( |
| 232 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 233 | ppOdClkVoltageData)); |
| 234 | ts.init(); |
| 235 | |
| 236 | // min |
| 237 | ts.point(0, units::frequency::megahertz_t(0), units::voltage::millivolt_t(0)); |
| 238 | // max |
| 239 | ts.point(1, units::frequency::megahertz_t(10000), |
| 240 | units::voltage::millivolt_t(10000)); |
| 241 | |
| 242 | auto curve = ts.points(); |
| 243 | REQUIRE(curve.size() == 3); |
| 244 | |
| 245 | // min |
| 246 | auto &[p0Freq, p0Volt] = curve.at(0); |
| 247 | REQUIRE(p0Freq == units::frequency::megahertz_t(200)); |
| 248 | REQUIRE(p0Volt == units::voltage::millivolt_t(800)); |
| 249 | |
| 250 | // max |
| 251 | auto &[p1Freq, p1Volt] = curve.at(1); |
| 252 | REQUIRE(p1Freq == units::frequency::megahertz_t(2000)); |
| 253 | REQUIRE(p1Volt == units::voltage::millivolt_t(900)); |
| 254 | } |
| 255 | |
| 256 | SECTION("Imports its state") |
| 257 | { |
| 258 | PMVoltCurveTestAdapter ts( |
| 259 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 260 | ppOdClkVoltageData)); |
| 261 | ts.init(); |
| 262 | |
| 263 | std::vector<std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t>> |
| 264 | points; |
| 265 | points.emplace_back(units::frequency::megahertz_t(201), |
| 266 | units::voltage::millivolt_t(801)); |
| 267 | points.emplace_back(units::frequency::megahertz_t(1001), |
| 268 | units::voltage::millivolt_t(851)); |
| 269 | points.emplace_back(units::frequency::megahertz_t(1000), |
| 270 | units::voltage::millivolt_t(850)); |
| 271 | |
| 272 | PMVoltCurveImporterStub i("manual", points); |
| 273 | |
| 274 | ts.importControl(i); |
| 275 | |
| 276 | REQUIRE(ts.mode() == "manual"); |
| 277 | REQUIRE(ts.points() == points); |
| 278 | } |
| 279 | |
| 280 | SECTION("Exports its state") |
| 281 | { |
| 282 | PMVoltCurveTestAdapter ts( |
| 283 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 284 | ppOdClkVoltageData)); |
| 285 | ts.init(); |
| 286 | |
| 287 | std::vector<std::string> modes{"auto", "manual"}; |
| 288 | |
| 289 | std::vector<std::pair< |
| 290 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>, |
| 291 | std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>>> |
| 292 | pointsRange; |
| 293 | for (int i = 0; i < 3; ++i) |
| 294 | pointsRange.emplace_back( |
| 295 | std::make_pair(units::frequency::megahertz_t(200), |
| 296 | units::frequency::megahertz_t(2000)), |
| 297 | std::make_pair(units::voltage::millivolt_t(800), |
| 298 | units::voltage::millivolt_t(900))); |
| 299 | |
| 300 | std::vector<std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t>> |
| 301 | points; |
| 302 | points.emplace_back(units::frequency::megahertz_t(200), |
| 303 | units::voltage::millivolt_t(800)); |
| 304 | points.emplace_back(units::frequency::megahertz_t(1000), |
| 305 | units::voltage::millivolt_t(850)); |
| 306 | points.emplace_back(units::frequency::megahertz_t(2000), |
| 307 | units::voltage::millivolt_t(900)); |
| 308 | |
| 309 | trompeloeil::sequence seq; |
| 310 | PMVoltCurveExporterMock e; |
| 311 | REQUIRE_CALL(e, takePMVoltCurveModes(trompeloeil::_)) |
| 312 | .LR_WITH(_1 == modes) |
| 313 | .IN_SEQUENCE(seq); |
| 314 | REQUIRE_CALL(e, takePMVoltCurveMode("auto")).IN_SEQUENCE(seq); |
| 315 | |
| 316 | REQUIRE_CALL(e, takePMVoltCurvePointsRange(trompeloeil::_)) |
| 317 | .LR_WITH(_1 == pointsRange) |
| 318 | .IN_SEQUENCE(seq); |
| 319 | |
| 320 | REQUIRE_CALL(e, takePMVoltCurvePoints(trompeloeil::_)) |
| 321 | .LR_WITH(_1 == points) |
| 322 | .IN_SEQUENCE(seq); |
| 323 | |
| 324 | ts.exportControl(e); |
| 325 | } |
| 326 | |
| 327 | SECTION("Does not generate clean control commands") |
| 328 | { |
| 329 | PMVoltCurveTestAdapter ts( |
| 330 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 331 | ppOdClkVoltageData)); |
| 332 | ts.init(); |
| 333 | ts.cleanControl(ctlCmds); |
| 334 | |
| 335 | auto &commands = ctlCmds.commands(); |
| 336 | REQUIRE(commands.empty()); |
| 337 | } |
| 338 | |
| 339 | SECTION("Does not generate sync control commands when is synced") |
| 340 | { |
| 341 | |
| 342 | PMVoltCurveTestAdapter ts( |
| 343 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 344 | ppOdClkVoltageData)); |
| 345 | ts.init(); |
| 346 | ts.syncControl(ctlCmds); |
| 347 | |
| 348 | REQUIRE(ctlCmds.commands().empty()); |
| 349 | } |
| 350 | |
| 351 | SECTION("Generates sync control commands when points are out of sync") |
| 352 | { |
| 353 | PMVoltCurveTestAdapter ts( |
| 354 | "vc", std::make_unique<VectorStringDataSourceStub>("pp_od_clk_voltage", |
| 355 | ppOdClkVoltageData)); |
| 356 | ts.init(); |
| 357 | |
| 358 | ts.mode("manual"); |
| 359 | ts.point(0, units::frequency::megahertz_t(201), |
| 360 | units::voltage::millivolt_t(801)); |
| 361 | ts.syncControl(ctlCmds); |
| 362 | |
| 363 | auto &commands = ctlCmds.commands(); |
| 364 | REQUIRE(commands.size() == 1); |
| 365 | |
| 366 | auto &[cmd0Path, cmd0Value] = commands.at(0); |
| 367 | REQUIRE(cmd0Path == "pp_od_clk_voltage"); |
| 368 | REQUIRE(cmd0Value == "vc 0 201 801"); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | } // namespace Tests::AMD::PMVoltCurve |