blob: 8facad559070bdc86a9e7e8c98d27ecad1a31754 [file] [log] [blame]
Jonathan Doman94c94bf2020-10-05 23:25:45 -07001// Copyright (c) 2020 Intel Corporation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "speed_select.hpp"
16
17#include "cpuinfo.hpp"
Jonathan Doman703a1852020-11-11 13:04:02 -080018#include "cpuinfo_utils.hpp"
Jonathan Doman94c94bf2020-10-05 23:25:45 -070019
20#include <peci.h>
21
Jonathan Doman49ea8302022-05-26 14:29:46 -070022#include <boost/asio/error.hpp>
Jonathan Doman94c94bf2020-10-05 23:25:45 -070023#include <boost/asio/steady_timer.hpp>
Jonathan Doman703a1852020-11-11 13:04:02 -080024#include <xyz/openbmc_project/Common/Device/error.hpp>
Jonathan Doman94c94bf2020-10-05 23:25:45 -070025#include <xyz/openbmc_project/Common/error.hpp>
26#include <xyz/openbmc_project/Control/Processor/CurrentOperatingConfig/server.hpp>
27#include <xyz/openbmc_project/Inventory/Item/Cpu/OperatingConfig/server.hpp>
28
Jonathan Doman16a2ced2021-11-01 11:13:22 -070029#include <algorithm>
Jonathan Doman94c94bf2020-10-05 23:25:45 -070030#include <iostream>
31#include <memory>
Jonathan Doman16a2ced2021-11-01 11:13:22 -070032#include <stdexcept>
Jonathan Doman94c94bf2020-10-05 23:25:45 -070033#include <string>
34
35namespace cpu_info
36{
37namespace sst
38{
39
Jonathan Doman16a2ced2021-11-01 11:13:22 -070040// Specialize char to print the integer value instead of ascii. We basically
41// never want to print a single ascii char.
42std::ostream& operator<<(std::ostream& os, uint8_t value)
Jonathan Doman94c94bf2020-10-05 23:25:45 -070043{
Jonathan Doman16a2ced2021-11-01 11:13:22 -070044 return os << static_cast<int>(value);
Jonathan Doman94c94bf2020-10-05 23:25:45 -070045}
46
Jonathan Doman16a2ced2021-11-01 11:13:22 -070047bool checkPECIStatus(EPECIStatus libStatus, uint8_t completionCode)
Jonathan Doman94c94bf2020-10-05 23:25:45 -070048{
49 if (libStatus != PECI_CC_SUCCESS || completionCode != PECI_DEV_CC_SUCCESS)
50 {
51 std::cerr << "PECI command failed."
52 << " Driver Status = " << libStatus << ","
Jonathan Doman16a2ced2021-11-01 11:13:22 -070053 << " Completion Code = " << completionCode << '\n';
Jonathan Doman94c94bf2020-10-05 23:25:45 -070054 return false;
55 }
56 return true;
57}
58
Jonathan Doman49ea8302022-05-26 14:29:46 -070059std::vector<uint32_t> convertMaskToList(std::bitset<64> mask)
60{
61 std::vector<uint32_t> bitList;
62 for (size_t i = 0; i < mask.size(); ++i)
63 {
64 if (mask.test(i))
65 {
66 bitList.push_back(i);
67 }
68 }
69 return bitList;
70}
71
Jonathan Doman16a2ced2021-11-01 11:13:22 -070072static std::vector<BackendProvider>& getProviders()
Jonathan Doman94c94bf2020-10-05 23:25:45 -070073{
Jonathan Doman16a2ced2021-11-01 11:13:22 -070074 static auto* providers = new std::vector<BackendProvider>;
75 return *providers;
76}
Jonathan Doman94c94bf2020-10-05 23:25:45 -070077
Jonathan Doman16a2ced2021-11-01 11:13:22 -070078void registerBackend(BackendProvider providerFn)
79{
80 getProviders().push_back(providerFn);
81}
Jonathan Doman94c94bf2020-10-05 23:25:45 -070082
Jonathan Doman16a2ced2021-11-01 11:13:22 -070083std::unique_ptr<SSTInterface> getInstance(uint8_t address, CPUModel model)
84{
85 DEBUG_PRINT << "Searching for provider for " << address << ", model "
86 << std::hex << model << '\n';
87 for (const auto& provider : getProviders())
Jonathan Doman94c94bf2020-10-05 23:25:45 -070088 {
Jonathan Doman94c94bf2020-10-05 23:25:45 -070089 try
90 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -070091 auto interface = provider(address, model);
92 DEBUG_PRINT << "returned " << interface << '\n';
93 if (interface)
Jonathan Doman94c94bf2020-10-05 23:25:45 -070094 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -070095 return interface;
Jonathan Doman94c94bf2020-10-05 23:25:45 -070096 }
97 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -070098 catch (...)
Jonathan Doman94c94bf2020-10-05 23:25:45 -070099 {}
100 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700101 DEBUG_PRINT << "No supported backends found\n";
102 return nullptr;
103}
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700104
105using BaseCurrentOperatingConfig =
106 sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Control::
107 Processor::server::CurrentOperatingConfig>;
108
109using BaseOperatingConfig =
110 sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Inventory::
111 Item::Cpu::server::OperatingConfig>;
112
113class OperatingConfig : public BaseOperatingConfig
114{
115 public:
116 std::string path;
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700117 unsigned int level;
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700118
119 public:
120 using BaseOperatingConfig::BaseOperatingConfig;
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700121 OperatingConfig(sdbusplus::bus::bus& bus, unsigned int level_,
122 std::string path_) :
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700123 BaseOperatingConfig(bus, path_.c_str(), action::defer_emit),
124 path(std::move(path_)), level(level_)
125 {}
126};
127
128class CPUConfig : public BaseCurrentOperatingConfig
129{
130 private:
131 /** Objects describing all available SST configs - not modifiable. */
132 std::vector<std::unique_ptr<OperatingConfig>> availConfigs;
133 sdbusplus::bus::bus& bus;
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700134 const uint8_t peciAddress;
Jonathan Doman703a1852020-11-11 13:04:02 -0800135 const std::string path; ///< D-Bus path of CPU object
136 const CPUModel cpuModel;
Jonathan Doman703a1852020-11-11 13:04:02 -0800137
138 // Keep mutable copies of the properties so we can cache values that we
139 // retrieve in the getters. We don't want to throw an error on a D-Bus
140 // get-property call (extra error handling in clients), so by caching we can
141 // hide any temporary hiccup in PECI communication.
142 // These values can be changed by in-band software so we have to do a full
143 // PECI read on every get-property, and can't assume that values will change
144 // only when set-property is done.
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700145 mutable unsigned int currentLevel;
Jonathan Doman703a1852020-11-11 13:04:02 -0800146 mutable bool bfEnabled;
Jonathan Doman703a1852020-11-11 13:04:02 -0800147
148 /**
149 * Enforce common pre-conditions for D-Bus set property handlers.
150 */
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700151 void setPropertyCheckOrThrow(SSTInterface& sst)
Jonathan Doman703a1852020-11-11 13:04:02 -0800152 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700153 if (!sst.supportsControl())
Jonathan Doman703a1852020-11-11 13:04:02 -0800154 {
155 throw sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed();
156 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700157 if (hostState != HostState::postComplete || !sst.ready())
Jonathan Doman703a1852020-11-11 13:04:02 -0800158 {
159 throw sdbusplus::xyz::openbmc_project::Common::Error::Unavailable();
160 }
161 }
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700162
163 public:
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700164 CPUConfig(sdbusplus::bus::bus& bus_, uint8_t index, CPUModel model) :
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700165 BaseCurrentOperatingConfig(bus_, generatePath(index).c_str(),
166 action::defer_emit),
Jonathan Doman703a1852020-11-11 13:04:02 -0800167 bus(bus_), peciAddress(index + MIN_CLIENT_ADDR),
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700168 path(generatePath(index)), cpuModel(model), currentLevel(0),
169 bfEnabled(false)
Jonathan Doman703a1852020-11-11 13:04:02 -0800170 {}
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700171
172 //
173 // D-Bus Property Overrides
174 //
175
176 sdbusplus::message::object_path appliedConfig() const override
177 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700178 DEBUG_PRINT << "Reading AppliedConfig\n";
Jonathan Doman703a1852020-11-11 13:04:02 -0800179 // If CPU is powered off, return power-up default value of Level 0.
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700180 unsigned int level = 0;
Jonathan Doman703a1852020-11-11 13:04:02 -0800181 if (hostState != HostState::off)
182 {
183 // Otherwise, try to read current state
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700184 auto sst = getInstance(peciAddress, cpuModel);
185 if (!sst)
Jonathan Doman703a1852020-11-11 13:04:02 -0800186 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700187 std::cerr << __func__
188 << ": Failed to get SST provider instance\n";
Jonathan Doman703a1852020-11-11 13:04:02 -0800189 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700190 else
Jonathan Doman703a1852020-11-11 13:04:02 -0800191 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700192 try
193 {
194 currentLevel = sst->currentLevel();
195 }
196 catch (const PECIError& error)
197 {
198 std::cerr << "Failed to get SST-PP level: " << error.what()
199 << "\n";
200 }
Jonathan Doman703a1852020-11-11 13:04:02 -0800201 }
202 level = currentLevel;
203 }
204 return generateConfigPath(level);
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700205 }
206
207 bool baseSpeedPriorityEnabled() const override
208 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700209 DEBUG_PRINT << "Reading BaseSpeedPriorityEnabled\n";
Jonathan Doman703a1852020-11-11 13:04:02 -0800210 bool enabled = false;
211 if (hostState != HostState::off)
212 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700213 auto sst = getInstance(peciAddress, cpuModel);
214 if (!sst)
Jonathan Doman703a1852020-11-11 13:04:02 -0800215 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700216 std::cerr << __func__
217 << ": Failed to get SST provider instance\n";
Jonathan Doman703a1852020-11-11 13:04:02 -0800218 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700219 else
Jonathan Doman703a1852020-11-11 13:04:02 -0800220 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700221 try
222 {
223 bfEnabled = sst->bfEnabled(currentLevel);
224 }
225 catch (const PECIError& error)
226 {
227 std::cerr << "Failed to get SST-BF status: " << error.what()
228 << "\n";
229 }
Jonathan Doman703a1852020-11-11 13:04:02 -0800230 }
231 enabled = bfEnabled;
232 }
233 return enabled;
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700234 }
235
236 sdbusplus::message::object_path
Jonathan Doman703a1852020-11-11 13:04:02 -0800237 appliedConfig(sdbusplus::message::object_path value) override
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700238 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700239 DEBUG_PRINT << "Writing AppliedConfig\n";
Jonathan Doman703a1852020-11-11 13:04:02 -0800240 const OperatingConfig* newConfig = nullptr;
241 for (const auto& config : availConfigs)
242 {
243 if (config->path == value.str)
244 {
245 newConfig = config.get();
246 }
247 }
248
249 if (newConfig == nullptr)
250 {
251 throw sdbusplus::xyz::openbmc_project::Common::Error::
252 InvalidArgument();
253 }
254
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700255 auto sst = getInstance(peciAddress, cpuModel);
256 if (!sst)
257 {
258 std::cerr << __func__ << ": Failed to get SST provider instance\n";
259 return sdbusplus::message::object_path();
260 }
261 setPropertyCheckOrThrow(*sst);
Jonathan Doman703a1852020-11-11 13:04:02 -0800262 try
263 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700264 sst->setCurrentLevel(newConfig->level);
Jonathan Doman703a1852020-11-11 13:04:02 -0800265 currentLevel = newConfig->level;
266 }
267 catch (const PECIError& error)
268 {
269 std::cerr << "Failed to set new SST-PP level: " << error.what()
270 << "\n";
271 throw sdbusplus::xyz::openbmc_project::Common::Device::Error::
272 WriteFailure();
273 }
274
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700275 // return value not used
276 return sdbusplus::message::object_path();
277 }
278
Jonathan Doman703a1852020-11-11 13:04:02 -0800279 bool baseSpeedPriorityEnabled(bool value) override
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700280 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700281 DEBUG_PRINT << "Writing BaseSpeedPriorityEnabled\n";
282 auto sst = getInstance(peciAddress, cpuModel);
283 if (!sst)
284 {
285 std::cerr << __func__ << ": Failed to get SST provider instance\n";
286 return false;
287 }
288 setPropertyCheckOrThrow(*sst);
Jonathan Doman703a1852020-11-11 13:04:02 -0800289 try
290 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700291 sst->setBfEnabled(value);
Jonathan Doman703a1852020-11-11 13:04:02 -0800292 }
293 catch (const PECIError& error)
294 {
295 std::cerr << "Failed to set SST-BF status: " << error.what()
296 << "\n";
297 throw sdbusplus::xyz::openbmc_project::Common::Device::Error::
298 WriteFailure();
299 }
300
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700301 // return value not used
302 return false;
303 }
304
305 //
306 // Additions
307 //
308
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700309 OperatingConfig& newConfig(unsigned int level)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700310 {
311 availConfigs.emplace_back(std::make_unique<OperatingConfig>(
312 bus, level, generateConfigPath(level)));
313 return *availConfigs.back();
314 }
315
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700316 std::string generateConfigPath(unsigned int level) const
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700317 {
318 return path + "/config" + std::to_string(level);
319 }
320
321 /**
322 * Emit the interface added signals which were deferred. This is required
Jonathan Doman703a1852020-11-11 13:04:02 -0800323 * for ObjectMapper to pick up the objects, if we initially defered the
324 * signal emitting.
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700325 */
326 void finalize()
327 {
328 emit_added();
329 for (auto& config : availConfigs)
330 {
331 config->emit_added();
332 }
333 }
334
335 static std::string generatePath(int index)
336 {
Jonathan Doman0a385372021-03-08 17:04:13 -0800337 return cpuPath + std::to_string(index);
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700338 }
339};
340
341/**
342 * Retrieve the SST parameters for a single config and fill the values into the
343 * properties on the D-Bus interface.
344 *
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700345 * @param[in,out] sst Interface to SST backend.
346 * @param[in] level Config TDP level to retrieve.
347 * @param[out] config D-Bus interface to update.
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700348 */
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700349static void getSingleConfig(SSTInterface& sst, unsigned int level,
350 OperatingConfig& config)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700351{
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700352 config.powerLimit(sst.tdp(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700353
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700354 config.availableCoreCount(sst.coreCount(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700355
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700356 config.baseSpeed(sst.p1Freq(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700357
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700358 config.maxSpeed(sst.p0Freq(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700359
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700360 config.maxJunctionTemperature(sst.prochotTemp(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700361
362 // Construct BaseSpeedPrioritySettings
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700363 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>> baseSpeeds;
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700364 if (sst.bfSupported(level))
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700365 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700366 std::vector<uint32_t> totalCoreList, loFreqCoreList, hiFreqCoreList;
367 totalCoreList = sst.enabledCoreList(level);
368 hiFreqCoreList = sst.bfHighPriorityCoreList(level);
369 std::set_difference(
370 totalCoreList.begin(), totalCoreList.end(), hiFreqCoreList.begin(),
371 hiFreqCoreList.end(),
372 std::inserter(loFreqCoreList, loFreqCoreList.begin()));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700373
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700374 baseSpeeds = {{sst.bfHighPriorityFreq(level), hiFreqCoreList},
375 {sst.bfLowPriorityFreq(level), loFreqCoreList}};
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700376 }
377 config.baseSpeedPrioritySettings(baseSpeeds);
378
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700379 config.turboProfile(sst.sseTurboProfile(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700380}
381
382/**
383 * Retrieve all SST configuration info for all discoverable CPUs, and publish
384 * the info on new D-Bus objects on the given bus connection.
385 *
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700386 * @param[in,out] ioc ASIO context.
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700387 * @param[in,out] conn D-Bus ASIO connection.
388 *
Jonathan Doman703a1852020-11-11 13:04:02 -0800389 * @return Whether discovery was successfully finished.
390 *
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700391 * @throw PECIError A PECI command failed on a CPU which had previously
392 * responded to a command.
393 */
Jonathan Doman49ea8302022-05-26 14:29:46 -0700394static bool discoverCPUsAndConfigs(boost::asio::io_context& ioc,
395 sdbusplus::asio::connection& conn)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700396{
Jonathan Doman49ea8302022-05-26 14:29:46 -0700397 // Persistent list - only populated after complete/successful discovery
398 static std::vector<std::unique_ptr<CPUConfig>> cpus;
399 cpus.clear();
400
401 // Temporary staging list. In case there is any failure, these temporary
402 // objects will get dropped to avoid presenting incomplete info until the
403 // next discovery attempt.
404 std::vector<std::unique_ptr<CPUConfig>> cpuList;
405
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700406 for (uint8_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; ++i)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700407 {
Jonathan Doman703a1852020-11-11 13:04:02 -0800408 // Let the event handler run any waiting tasks. If there is a lot of
409 // PECI contention, SST discovery could take a long time. This lets us
410 // get updates to hostState and handle any D-Bus requests.
411 ioc.poll();
412
413 if (hostState == HostState::off)
414 {
415 return false;
416 }
417
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700418 unsigned int cpuIndex = i - MIN_CLIENT_ADDR;
419 DEBUG_PRINT << "Discovering CPU " << cpuIndex << '\n';
420
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700421 // We could possibly check D-Bus for CPU presence and model, but PECI is
422 // 10x faster and so much simpler.
423 uint8_t cc, stepping;
424 CPUModel cpuModel;
425 EPECIStatus status = peci_GetCPUID(i, &cpuModel, &stepping, &cc);
Jonathan Doman703a1852020-11-11 13:04:02 -0800426 if (status == PECI_CC_TIMEOUT)
427 {
428 // Timing out indicates the CPU is present but PCS services not
429 // working yet. Try again later.
430 throw PECIError("Get CPUID timed out");
431 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700432 if (status == PECI_CC_CPU_NOT_PRESENT)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700433 {
434 continue;
435 }
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700436 if (status != PECI_CC_SUCCESS || cc != PECI_DEV_CC_SUCCESS)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700437 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700438 std::cerr << "GetCPUID returned status " << status
439 << ", cc = " << cc << '\n';
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700440 continue;
441 }
442
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700443 std::unique_ptr<SSTInterface> sst = getInstance(i, cpuModel);
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700444
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700445 if (!sst)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700446 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700447 // No supported backend for this CPU.
448 continue;
449 }
450
451 if (!sst->ready())
452 {
453 // Supported CPU but it can't be queried yet. Try again later.
454 std::cerr << "sst not ready yet\n";
455 return false;
456 }
457
458 if (!sst->ppEnabled())
459 {
460 // Supported CPU but the specific SKU doesn't support SST-PP.
461 std::cerr << "CPU doesn't support SST-PP\n";
462 continue;
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700463 }
464
465 // Create the per-CPU configuration object
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700466 cpuList.emplace_back(
467 std::make_unique<CPUConfig>(conn, cpuIndex, cpuModel));
468 CPUConfig& cpu = *cpuList.back();
469
470 bool foundCurrentLevel = false;
471
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700472 for (unsigned int level = 0; level <= sst->numLevels(); ++level)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700473 {
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700474 // levels 1 and 2 were legacy/deprecated, originally used for AVX
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700475 // license pre-granting. They may be reused for more levels in
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700476 // future generations. So we need to check for discontinuities.
477 if (!sst->levelSupported(level))
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700478 {
479 continue;
480 }
481
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700482 getSingleConfig(*sst, level, cpu.newConfig(level));
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700483
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700484 if (level == sst->currentLevel())
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700485 {
486 foundCurrentLevel = true;
487 }
488 }
489
490 if (!foundCurrentLevel)
491 {
492 // In case we didn't encounter a PECI error, but also didn't find
493 // the config which is supposedly applied, we won't be able to
494 // populate the CurrentOperatingConfig so we have to remove this CPU
495 // from consideration.
496 std::cerr << "CPU " << cpuIndex
497 << " claimed SST support but invalid configs\n";
498 cpuList.pop_back();
499 continue;
500 }
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700501 }
Jonathan Doman703a1852020-11-11 13:04:02 -0800502
Jonathan Doman49ea8302022-05-26 14:29:46 -0700503 cpuList.swap(cpus);
504 std::for_each(cpus.begin(), cpus.end(), [](auto& cpu) { cpu->finalize(); });
Jonathan Doman703a1852020-11-11 13:04:02 -0800505 return true;
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700506}
507
Jonathan Doman49ea8302022-05-26 14:29:46 -0700508/**
509 * Attempt discovery process, and if it fails, wait for 10 seconds to try again.
510 */
511static void discoverOrWait()
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700512{
Jonathan Doman49ea8302022-05-26 14:29:46 -0700513 static boost::asio::steady_timer peciRetryTimer(dbus::getIOContext());
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700514 static int peciErrorCount = 0;
Jonathan Doman703a1852020-11-11 13:04:02 -0800515 bool finished = false;
Jonathan Doman49ea8302022-05-26 14:29:46 -0700516
517 // This function may be called from hostStateHandler or by retrying itself.
518 // In case those overlap, cancel any outstanding retry timer.
519 peciRetryTimer.cancel();
520
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700521 try
522 {
Jonathan Doman703a1852020-11-11 13:04:02 -0800523 DEBUG_PRINT << "Starting discovery\n";
Jonathan Doman49ea8302022-05-26 14:29:46 -0700524 finished = discoverCPUsAndConfigs(dbus::getIOContext(),
525 *dbus::getConnection());
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700526 }
527 catch (const PECIError& err)
528 {
529 std::cerr << "PECI Error: " << err.what() << '\n';
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700530
531 // In case of repeated failure to finish discovery, turn off this
532 // feature altogether. Possible cause is that the CPU model does not
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700533 // actually support the necessary commands.
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700534 if (++peciErrorCount >= 50)
535 {
536 std::cerr << "Aborting SST discovery\n";
537 return;
538 }
539
540 std::cerr << "Retrying SST discovery later\n";
541 }
542
Jonathan Doman703a1852020-11-11 13:04:02 -0800543 DEBUG_PRINT << "Finished discovery attempt: " << finished << '\n';
544
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700545 // Retry later if no CPUs were available, or there was a PECI error.
Jonathan Doman703a1852020-11-11 13:04:02 -0800546 if (!finished)
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700547 {
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700548 peciRetryTimer.expires_after(std::chrono::seconds(10));
Jonathan Doman49ea8302022-05-26 14:29:46 -0700549 peciRetryTimer.async_wait([](boost::system::error_code ec) {
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700550 if (ec)
551 {
Jonathan Doman49ea8302022-05-26 14:29:46 -0700552 if (ec != boost::asio::error::operation_aborted)
553 {
554 std::cerr << "SST PECI Retry Timer failed: " << ec << '\n';
555 }
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700556 return;
557 }
Jonathan Doman49ea8302022-05-26 14:29:46 -0700558 discoverOrWait();
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700559 });
560 }
561}
562
Jonathan Doman49ea8302022-05-26 14:29:46 -0700563static void hostStateHandler(HostState prevState, HostState)
564{
565 if (prevState == HostState::off)
566 {
567 // Start or re-start discovery any time the host moves out of the
568 // powered off state.
569 discoverOrWait();
570 }
571}
572
573void init()
574{
575 addHostStateCallback(hostStateHandler);
576}
577
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700578} // namespace sst
579} // namespace cpu_info