Debian Codegraph @

tests/src/test_amdpmvoltoffset.cpp

raw symbols SCIP
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/voltoffset/pmvoltoffset.h"
11 #include <units.h>
12
13 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
14
15 namespace Tests::AMD::PMVoltOffset {
16
17 class PMVoltOffsetTestAdapter : public ::AMD::PMVoltOffset
18 {
19 public:
20 using ::AMD::PMVoltOffset::PMVoltOffset;
21
22 using ::AMD::PMVoltOffset::cleanControl;
23 using ::AMD::PMVoltOffset::exportControl;
24 using ::AMD::PMVoltOffset::importControl;
25 using ::AMD::PMVoltOffset::range;
26 using ::AMD::PMVoltOffset::syncControl;
27 using ::AMD::PMVoltOffset::value;
28 };
29
30 class PMVoltOffsetImporterStub final : public ::AMD::PMVoltOffset::Importer
31 {
32 public:
33 PMVoltOffsetImporterStub(units::voltage::millivolt_t const offset)
34 : offset_(offset)
35 {
36 }
37
38 std::optional<std::reference_wrapper<Importable::Importer>>
39 provideImporter(Item const &) override
40 {
41 return {};
42 }
43
44 bool provideActive() const override
45 {
46 return false;
47 }
48
49 units::voltage::millivolt_t providePMVoltOffsetValue() const override
50 {
51 return offset_;
52 }
53
54 private:
55 units::voltage::millivolt_t const offset_;
56 };
57
58 class PMVoltOffsetExporterMock : public ::AMD::PMVoltOffset::Exporter
59 {
60 public:
61 MAKE_MOCK2(takePMVoltOffsetRange,
62 void(units::voltage::millivolt_t, units::voltage::millivolt_t),
63 override);
64 MAKE_MOCK1(takePMVoltOffsetValue, void(units::voltage::millivolt_t), override);
65
66 MAKE_MOCK1(takeActive, void(bool), override);
67 MAKE_MOCK1(
68 provideExporter,
69 std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &),
70 override);
71 };
72
73 TEST_CASE("AMD PMVoltOffset tests",
74 "[GPU][AMD][PM][PMAdvanced][PMOverdrive][PMVoltOffset]")
75 {
76 // clang-format off
77 std::vector<std::string> ppOdClkVoltageData {
78 "OD_VDDGFX_OFFSET:",
79 "0mV"};
80 // NOTE The voltage offset range can be exposed in pp_od_clk_voltage for certain
81 // hardware (Linux 6.14+, RX 9000) with the following format:
82 // OD_RANGE:
83 // VDDGFX_OFFSET: -200mv 0mv
84 // clang-format on
85 ::AMD::PMVoltOffset::Range range{units::voltage::millivolt_t(-250),
86 units::voltage::millivolt_t(250)};
87
88 CommandQueueStub ctlCmds;
89
90 SECTION("Has PMVoltOffset ID")
91 {
92 PMVoltOffsetTestAdapter ts(std::move(range),
93 std::make_unique<VectorStringDataSourceStub>());
94 REQUIRE(ts.ID() == ::AMD::PMVoltOffset::ItemID);
95 }
96
97 SECTION("Is active by default")
98 {
99 PMVoltOffsetTestAdapter ts(std::move(range),
100 std::make_unique<VectorStringDataSourceStub>());
101 REQUIRE(ts.active());
102 }
103
104 SECTION("Does not generate pre-init control commands")
105 {
106 PMVoltOffsetTestAdapter ts(std::move(range),
107 std::make_unique<VectorStringDataSourceStub>(
108 "pp_od_clk_voltage", ppOdClkVoltageData));
109 ts.preInit(ctlCmds);
110
111 auto &commands = ctlCmds.commands();
112 REQUIRE(commands.empty());
113 }
114
115 SECTION("Generates post-init control commands")
116 {
117 PMVoltOffsetTestAdapter ts(std::move(range),
118 std::make_unique<VectorStringDataSourceStub>(
119 "pp_od_clk_voltage", ppOdClkVoltageData));
120 ts.preInit(ctlCmds);
121 ctlCmds.clear();
122 ts.postInit(ctlCmds);
123
124 auto &commands = ctlCmds.commands();
125 REQUIRE(commands.size() == 1);
126
127 auto &[cmd0Path, cmd0Value] = commands.at(0);
128 REQUIRE(cmd0Path == "pp_od_clk_voltage");
129 REQUIRE(cmd0Value == "vo 0");
130 }
131
132 SECTION("Initializes offset from pp_od_clk_voltage data source")
133 {
134 PMVoltOffsetTestAdapter ts(std::move(range),
135 std::make_unique<VectorStringDataSourceStub>(
136 "pp_od_clk_voltage", ppOdClkVoltageData));
137 ts.init();
138
139 auto offset = ts.value();
140 REQUIRE(offset == units::voltage::millivolt_t(0));
141 }
142
143 SECTION("Clamps offset value in range")
144 {
145 PMVoltOffsetTestAdapter ts(std::move(range),
146 std::make_unique<VectorStringDataSourceStub>(
147 "pp_od_clk_voltage", ppOdClkVoltageData));
148 ts.init();
149
150 auto range = ts.range();
151
152 // min
153 ts.value(units::voltage::millivolt_t(-1) + range.first);
154 REQUIRE(ts.value() == range.first);
155
156 // max
157 ts.value(units::voltage::millivolt_t(1) + range.second);
158 REQUIRE(ts.value() == range.second);
159 }
160
161 SECTION("Imports its state")
162 {
163 PMVoltOffsetTestAdapter ts(std::move(range),
164 std::make_unique<VectorStringDataSourceStub>(
165 "pp_od_clk_voltage", ppOdClkVoltageData));
166 ts.init();
167
168 auto offset = units::voltage::millivolt_t(-20);
169 PMVoltOffsetImporterStub i(offset);
170
171 ts.importControl(i);
172
173 REQUIRE(ts.value() == offset);
174 }
175
176 SECTION("Exports its state")
177 {
178 PMVoltOffsetTestAdapter ts(std::move(range),
179 std::make_unique<VectorStringDataSourceStub>(
180 "pp_od_clk_voltage", ppOdClkVoltageData));
181 ts.init();
182
183 auto range = ts.range();
184
185 trompeloeil::sequence seq;
186 PMVoltOffsetExporterMock e;
187 REQUIRE_CALL(e, takePMVoltOffsetRange(trompeloeil::eq(range.first),
188 trompeloeil::eq(range.second)))
189 .IN_SEQUENCE(seq);
190 REQUIRE_CALL(e, takePMVoltOffsetValue(units::voltage::millivolt_t(0)))
191 .IN_SEQUENCE(seq);
192
193 ts.exportControl(e);
194 }
195
196 SECTION("Does not generate clean control commands")
197 {
198 PMVoltOffsetTestAdapter ts(std::move(range),
199 std::make_unique<VectorStringDataSourceStub>(
200 "pp_od_clk_voltage", ppOdClkVoltageData));
201 ts.init();
202 ts.cleanControl(ctlCmds);
203
204 auto &commands = ctlCmds.commands();
205 REQUIRE(commands.empty());
206 }
207
208 SECTION("Does not generate sync control commands when is synced")
209 {
210 PMVoltOffsetTestAdapter ts(std::move(range),
211 std::make_unique<VectorStringDataSourceStub>(
212 "pp_od_clk_voltage", ppOdClkVoltageData));
213 ts.init();
214 ts.syncControl(ctlCmds);
215
216 REQUIRE(ctlCmds.commands().empty());
217 }
218
219 SECTION("Generates sync control commands when is out of sync")
220 {
221 PMVoltOffsetTestAdapter ts(std::move(range),
222 std::make_unique<VectorStringDataSourceStub>(
223 "pp_od_clk_voltage", ppOdClkVoltageData));
224 ts.init();
225
226 ts.value(units::voltage::millivolt_t(-20));
227 ts.syncControl(ctlCmds);
228
229 auto &commands = ctlCmds.commands();
230 REQUIRE(commands.size() == 1);
231
232 auto &[cmd0Path, cmd0Value] = commands.at(0);
233 REQUIRE(cmd0Path == "pp_od_clk_voltage");
234 REQUIRE(cmd0Value == "vo -20");
235 }
236 }
237
238 } // namespace Tests::AMD::PMVoltOffset