blob: 4ad085328aa85d27efedbc8f49a1536779e5fb4d [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IntelCPUSensor.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070018#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103019#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +010022#include <peci.h>
James Feist6714a252018-09-10 15:26:18 -070023
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/algorithm/string/replace.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/error.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070036#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070037#include <cctype>
Jakub Nowackia38e5612023-03-08 12:27:01 +010038#include <cerrno>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <chrono>
40#include <cstddef>
41#include <cstdint>
42#include <cstring>
James Feist24f02f22019-04-15 11:05:39 -070043#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070044#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070045#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <ios>
47#include <iostream>
48#include <iterator>
Patrick Venture96e97db2019-10-31 13:44:38 -070049#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070050#include <optional>
James Feist6714a252018-09-10 15:26:18 -070051#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <sstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070053#include <string>
54#include <utility>
55#include <variant>
56#include <vector>
James Feist6714a252018-09-10 15:26:18 -070057
James Feistf87dc4c2018-12-05 14:39:51 -080058// clang-format off
59// this needs to be included last or we'll have build issues
60#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080061#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
62#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
63#endif
James Feistf87dc4c2018-12-05 14:39:51 -080064// clang-format on
65
Ed Tanous8a57ec02020-10-09 12:46:52 -070066static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070067
Thu Nguyen255da6b2022-07-29 10:05:52 +070068boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
69 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080070boost::container::flat_map<std::string,
71 std::shared_ptr<sdbusplus::asio::dbus_interface>>
72 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080073
James Feist6714a252018-09-10 15:26:18 -070074enum State
75{
76 OFF, // host powered down
77 ON, // host powered on
78 READY // host powered on and mem test passed - fully ready
79};
80
81struct CPUConfig
82{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070083 CPUConfig(const uint64_t& bus, const uint64_t& addr,
84 const std::string& name, const State& state) :
Patrick Williams2aaf7172024-08-16 15:20:40 -040085 bus(bus), addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070086 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070087 int bus;
James Feist6714a252018-09-10 15:26:18 -070088 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070089 std::string name;
James Feist6714a252018-09-10 15:26:18 -070090 State state;
91
92 bool operator<(const CPUConfig& rhs) const
93 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093094 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070095 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070096 }
97};
98
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070099static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800100static constexpr const char* peciDevPath = "/sys/bus/peci/devices/";
Paul Fertser6e699732023-03-29 12:58:30 +0000101static constexpr const char* rescanPath = "/sys/bus/peci/rescan";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -0700103
James Feistcf3bce62019-01-08 10:07:19 -0800104namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -0800105
Zev Weiss054aad82022-08-18 01:37:34 -0700106static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -0800107static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +0700108 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -0700109
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700110void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700111 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800112 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700113 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800114 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800115 boost::container::flat_set<CPUConfig>& cpuConfigs,
116 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700117
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200118std::string createSensorName(const std::string& label, const std::string& item,
119 const int& cpuId)
120{
121 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700122 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200123 {
124 sensorName += " " + item;
125 }
PavanKumarIntel40baf772024-07-03 14:32:57 +0000126
127 std::string cpuStr = "CPU" + std::to_string(cpuId);
128 constexpr const char* subLabel = "DIMM";
129 std::size_t found = label.find(subLabel);
130 if (found != std::string::npos)
131 {
132 sensorName = cpuStr + " " + sensorName;
133 }
134 else
135 {
136 sensorName += " " + cpuStr;
137 }
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200138 // converting to Upper Camel case whole name
139 bool isWordEnd = true;
140 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
141 [&isWordEnd](int c) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400142 if (std::isspace(c) != 0)
143 {
144 isWordEnd = true;
145 }
146 else
147 {
148 if (isWordEnd)
149 {
150 isWordEnd = false;
151 return std::toupper(c);
152 }
153 }
154 return c;
155 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200156 return sensorName;
157}
158
Ed Tanous1f978632023-02-28 18:16:39 -0800159bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800160 sdbusplus::asio::object_server& objectServer,
161 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
162 boost::container::flat_set<CPUConfig>& cpuConfigs,
163 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700164{
165 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800166 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700167 {
168 if (cpu.state != State::OFF)
169 {
170 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500171 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800172 inventoryIfaces[cpu.name];
173 if (iface != nullptr)
174 {
175 continue;
176 }
177 iface = objectServer.add_interface(
178 cpuInventoryPath + std::string("/") + cpu.name,
179 "xyz.openbmc_project.Inventory.Item");
180 iface->register_property("PrettyName", cpu.name);
181 iface->register_property("Present", true);
182 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700183 }
184 }
185 if (!available)
186 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700187 return false;
James Feist6714a252018-09-10 15:26:18 -0700188 }
189
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800190 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700191 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800192 return false;
James Feist6714a252018-09-10 15:26:18 -0700193 }
194
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 std::vector<fs::path> hwmonNamePaths;
Paul Fertser6e699732023-03-29 12:58:30 +0000196 findFiles(fs::path(peciDevPath),
197 R"(peci-\d+/\d+-.+/peci[-_].+/hwmon/hwmon\d+/name$)",
198 hwmonNamePaths, 6);
199 if (hwmonNamePaths.empty())
James Feist6714a252018-09-10 15:26:18 -0700200 {
201 std::cerr << "No CPU sensors in system\n";
Paul Fertser75f92582023-03-29 12:48:07 +0000202 return false;
James Feist6714a252018-09-10 15:26:18 -0700203 }
204
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700205 boost::container::flat_set<std::string> scannedDirectories;
206 boost::container::flat_set<std::string> createdSensors;
207
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800208 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700209 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700210 auto hwmonDirectory = hwmonNamePath.parent_path();
211
212 auto ret = scannedDirectories.insert(hwmonDirectory.string());
213 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700214 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700215 continue; // already searched this path
216 }
217
218 fs::path::iterator it = hwmonNamePath.begin();
219 std::advance(it, 6); // pick the 6th part for a PECI client device name
220 std::string deviceName = *it;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700221
222 size_t bus = 0;
223 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000224 if (!getDeviceBusAddr(deviceName, bus, addr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700225 {
226 continue;
227 }
228
229 std::ifstream nameFile(hwmonNamePath);
230 if (!nameFile.good())
231 {
232 std::cerr << "Failure reading " << hwmonNamePath << "\n";
233 continue;
234 }
235 std::string hwmonName;
236 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700237 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800238 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700239 {
240 // shouldn't have an empty name file
241 continue;
242 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700243 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700244 {
245 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
246 << "\n";
247 }
James Feist6714a252018-09-10 15:26:18 -0700248
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700249 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700250 const SensorData* sensorData = nullptr;
251 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700252 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700253
Zev Weiss8908b3c2022-08-12 18:21:01 -0700254 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700255 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700256 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700257 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700258 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700259 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700260 auto sensorBase =
261 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700262 if (sensorBase != sensorData->end())
263 {
264 baseConfiguration = &(*sensorBase);
265 break;
266 }
267 }
268 if (baseConfiguration == nullptr)
269 {
270 std::cerr << "error finding base configuration for" << hwmonName
271 << "\n";
272 continue;
273 }
274 auto configurationBus = baseConfiguration->second.find("Bus");
275 auto configurationAddress =
276 baseConfiguration->second.find("Address");
277
278 if (configurationBus == baseConfiguration->second.end() ||
279 configurationAddress == baseConfiguration->second.end())
280 {
281 std::cerr << "error finding bus or address in configuration";
282 continue;
283 }
284
James Feist3eb82622019-02-08 13:10:22 -0800285 if (std::get<uint64_t>(configurationBus->second) != bus ||
286 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700287 {
288 continue;
289 }
290
Zev Weiss8908b3c2022-08-12 18:21:01 -0700291 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 break;
293 }
294 if (interfacePath == nullptr)
295 {
296 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700297 continue;
298 }
299
300 auto findCpuId = baseConfiguration->second.find("CpuID");
301 if (findCpuId == baseConfiguration->second.end())
302 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700303 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700304 continue;
305 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400306 int cpuId =
307 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700308
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700309 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700310 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200311 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200312 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700313 {
314 std::cerr << "No temperature sensors in system\n";
315 continue;
316 }
317
318 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800319 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700320 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200321 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200322 if (!fileParts)
323 {
324 continue;
325 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200326 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700327 auto inputPathStr = inputPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400328 auto labelPath =
329 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700330 std::ifstream labelFile(labelPath);
331 if (!labelFile.good())
332 {
333 std::cerr << "Failure reading " << labelPath << "\n";
334 continue;
335 }
336 std::string label;
337 std::getline(labelFile, label);
338 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800339
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200340 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700341
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800342 auto findSensor = gCpuSensors.find(sensorName);
343 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700344 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700345 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700346 {
347 std::cout << "Skipped: " << inputPath << ": " << sensorName
348 << " is already created\n";
349 }
350 continue;
351 }
352
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800353 // check hidden properties
354 bool show = true;
355 for (const char* prop : hiddenProps)
356 {
357 if (label == prop)
358 {
359 show = false;
360 break;
361 }
362 }
363
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700364 /*
365 * Find if there is DtsCritOffset is configured in config file
366 * set it if configured or else set it to 0
367 */
368 double dtsOffset = 0;
369 if (label == "DTS")
370 {
371 auto findThrOffset =
372 baseConfiguration->second.find("DtsCritOffset");
373 if (findThrOffset != baseConfiguration->second.end())
374 {
375 dtsOffset = std::visit(VariantToDoubleVisitor(),
376 findThrOffset->second);
377 }
378 }
379
James Feist6714a252018-09-10 15:26:18 -0700380 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700381 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700382 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700383 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800384 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700385 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700386 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700387 IntelCPUSensor::sensorScaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500388 dtsOffset, 0))
James Feist6714a252018-09-10 15:26:18 -0700389 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700390 std::cerr << "error populating thresholds for "
391 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700392 }
393 }
James Feistc140e202019-11-14 15:23:51 -0800394 auto& sensorPtr = gCpuSensors[sensorName];
395 // make sure destructor fires before creating a new one
396 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700397 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700398 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800399 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700400 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530401 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700402 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700403 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700404 {
James Feist6714a252018-09-10 15:26:18 -0700405 std::cout << "Mapped: " << inputPath << " to " << sensorName
406 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700407 }
James Feist6714a252018-09-10 15:26:18 -0700408 }
409 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700410
Ed Tanous2049bd22022-07-09 07:20:26 -0700411 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700412 {
413 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
414 << " created\n";
415 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700416
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700417 return true;
James Feist6714a252018-09-10 15:26:18 -0700418}
419
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800420bool exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700421{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700422 std::ostringstream hex;
423 hex << std::hex << config.addr;
424 const std::string& addrHexStr = hex.str();
425 std::string busStr = std::to_string(config.bus);
426
427 std::string parameters = "peci-client 0x" + addrHexStr;
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800428 std::string devPath = peciDevPath;
429 std::string delDevice = devPath + "peci-" + busStr + "/delete_device";
430 std::string newDevice = devPath + "peci-" + busStr + "/new_device";
431 std::string newClient = devPath + busStr + "-" + addrHexStr + "/driver";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700432
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800433 std::filesystem::path devicePath(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700434 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800435 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700436 {
James Feistcf3bce62019-01-08 10:07:19 -0800437 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700438 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700439 continue;
James Feist6714a252018-09-10 15:26:18 -0700440 }
441
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700442 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700443 if (directoryName.starts_with(busStr) &&
444 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700445 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700446 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700447 {
448 std::cout << parameters << " on bus " << busStr
449 << " is already exported\n";
450 }
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800451
452 std::ofstream delDeviceFile(delDevice);
453 if (!delDeviceFile.good())
454 {
455 std::cerr << "Error opening " << delDevice << "\n";
456 return false;
457 }
458 delDeviceFile << parameters;
459 delDeviceFile.close();
460
461 break;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700462 }
James Feist6714a252018-09-10 15:26:18 -0700463 }
464
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800465 std::ofstream deviceFile(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700466 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700467 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800468 std::cerr << "Error opening " << newDevice << "\n";
469 return false;
James Feist6714a252018-09-10 15:26:18 -0700470 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700471 deviceFile << parameters;
472 deviceFile.close();
473
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800474 if (!std::filesystem::exists(newClient))
475 {
476 std::cerr << "Error creating " << newClient << "\n";
477 return false;
478 }
479
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700480 std::cout << parameters << " on bus " << busStr << " is exported\n";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800481
482 return true;
James Feist6714a252018-09-10 15:26:18 -0700483}
484
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700485void detectCpu(boost::asio::steady_timer& pingTimer,
486 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800487 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800488 sdbusplus::asio::object_server& objectServer,
489 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
490 boost::container::flat_set<CPUConfig>& cpuConfigs,
491 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700492{
James Feist6714a252018-09-10 15:26:18 -0700493 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700494 static bool keepPinging = false;
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200495 int peciFd = -1;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700496
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800497 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700498 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800499 if (config.state == State::READY)
500 {
501 continue;
502 }
503
Paul Fertser6e699732023-03-29 12:58:30 +0000504 std::fstream rescan{rescanPath, std::ios::out};
505 if (rescan.is_open())
506 {
507 std::vector<fs::path> peciPaths;
508 std::ostringstream searchPath;
509 searchPath << std::hex << "peci-" << config.bus << "/" << config.bus
510 << "-" << config.addr;
511 findFiles(fs::path(peciDevPath + searchPath.str()),
512 R"(peci_cpu.dimmtemp.+/hwmon/hwmon\d+/name$)", peciPaths,
513 3);
514 if (!peciPaths.empty())
515 {
516 config.state = State::READY;
517 rescanDelaySeconds = 1;
518 }
519 else
520 {
521 findFiles(fs::path(peciDevPath + searchPath.str()),
522 R"(peci_cpu.cputemp.+/hwmon/hwmon\d+/name$)",
523 peciPaths, 3);
524 if (!peciPaths.empty())
525 {
526 config.state = State::ON;
527 rescanDelaySeconds = 3;
528 }
529 else
530 {
531 // https://www.kernel.org/doc/html/latest/admin-guide/abi-testing.html#abi-sys-bus-peci-rescan
532 rescan << "1";
533 }
534 }
535 if (config.state != State::READY)
536 {
537 keepPinging = true;
538 }
539
540 continue;
541 }
542
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700543 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800544
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200545 peci_SetDevName(peciDevPath.data());
546
Ed Tanous99c44092022-01-14 09:59:09 -0800547 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200548 if ((peci_Lock(&peciFd, PECI_NO_WAIT) != PECI_CC_SUCCESS) ||
549 (peciFd < 0))
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700550 {
Jakub Nowackia38e5612023-03-08 12:27:01 +0100551 std::cerr << "unable to open " << peciDevPath << " "
552 << std::strerror(errno) << "\n";
553 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
554 dbusConnection, cpuConfigs, sensorConfigs);
555 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700556 }
557
Ed Tanousa771f6a2022-01-14 09:36:51 -0800558 State newState = State::OFF;
Ed Tanous99c44092022-01-14 09:59:09 -0800559
560 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100561 if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700562 {
563 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700564 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700565 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100566 std::array<uint8_t, 8> pkgConfig{};
567 uint8_t cc = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800568
569 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100570 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_DDR_DIMM_TEMP,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400571 rank, 4, pkgConfig.data(), &cc) ==
572 PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700573 {
Matt Simmering38857572023-08-10 14:41:46 -0700574 // Depending on CPU generation, both 0 and 0xFF can be used
575 // to indicate no DIMM presence
576 if (((pkgConfig[0] != 0xFF) && (pkgConfig[0] != 0U)) ||
577 ((pkgConfig[1] != 0xFF) && (pkgConfig[1] != 0U)))
James Feist6714a252018-09-10 15:26:18 -0700578 {
579 dimmReady = true;
580 break;
581 }
582 }
583 else
584 {
585 break;
586 }
587 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700588
James Feist6714a252018-09-10 15:26:18 -0700589 if (dimmReady)
590 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700591 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700592 }
593 else
594 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700595 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700596 }
597 }
James Feist6714a252018-09-10 15:26:18 -0700598
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700599 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700600 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700601 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700602 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700603 if (config.state == State::OFF)
604 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800605 std::array<uint8_t, 8> pkgConfig{};
606 uint8_t cc = 0;
607
608 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_CPU_ID, 0,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400609 4, pkgConfig.data(), &cc) ==
610 PECI_CC_SUCCESS)
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800611 {
612 std::cout << config.name << " is detected\n";
613 if (!exportDevice(config))
614 {
615 newState = State::OFF;
616 }
617 }
618 else
619 {
620 newState = State::OFF;
621 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700622 }
James Feist6714a252018-09-10 15:26:18 -0700623
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700624 if (newState == State::ON)
625 {
626 rescanDelaySeconds = 3;
627 }
628 else if (newState == State::READY)
629 {
630 rescanDelaySeconds = 5;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400631 std::cout
632 << "DIMM(s) on " << config.name << " is/are detected\n";
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700633 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700634 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700635
636 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700637 }
638
639 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700640 {
641 keepPinging = true;
642 }
643
Ed Tanous8a57ec02020-10-09 12:46:52 -0700644 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700645 {
646 std::cout << config.name << ", state: " << config.state << "\n";
647 }
Wang Xiaohua4cd5a902023-07-04 16:28:43 +0800648 peci_Unlock(peciFd);
James Feist6714a252018-09-10 15:26:18 -0700649 }
650
Ed Tanous2049bd22022-07-09 07:20:26 -0700651 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700652 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800653 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700654 creationTimer.async_wait([&](const boost::system::error_code& ec) {
655 if (ec == boost::asio::error::operation_aborted)
656 {
657 return; // we're being canceled
658 }
659
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800660 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700661 sensorConfigs) ||
662 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700663 {
664 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800665 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700666 }
667 });
James Feist6714a252018-09-10 15:26:18 -0700668 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700669 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700670 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800671 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800672 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700673 }
674}
675
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700676void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700677 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800678 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700679 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800680 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800681 boost::container::flat_set<CPUConfig>& cpuConfigs,
682 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700683{
Ed Tanous83db50c2023-03-01 10:20:24 -0800684 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700685 pingTimer.async_wait([&](const boost::system::error_code& ec) {
686 if (ec == boost::asio::error::operation_aborted)
687 {
688 return; // we're being canceled
689 }
690
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800691 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800692 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700693 });
694}
695
James Feistc140e202019-11-14 15:23:51 -0800696bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
697 boost::container::flat_set<CPUConfig>& cpuConfigs,
698 ManagedObjectType& sensorConfigs,
699 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700700{
James Feist6714a252018-09-10 15:26:18 -0700701 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800702 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700703 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700704 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700705 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700706 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700707 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700708 return false;
James Feist6714a252018-09-10 15:26:18 -0700709 }
710 useCache = true;
711 }
712
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700713 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700714 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700715 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700716 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700717 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700718 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700719 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700720 {
Zev Weiss054aad82022-08-18 01:37:34 -0700721 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700722 {
723 continue;
724 }
725
Zev Weiss8908b3c2022-08-12 18:21:01 -0700726 auto findName = cfg.find("Name");
727 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700728 {
729 continue;
730 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400731 std::string nameRaw =
732 std::visit(VariantToStringVisitor(), findName->second);
733 std::string name =
734 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700735
James Feistc140e202019-11-14 15:23:51 -0800736 auto present = std::optional<bool>();
737 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700738 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700739 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700740 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700741 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700742 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700743 break;
James Feist58295ad2019-05-30 15:01:41 -0700744 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700745 }
746
James Feistc140e202019-11-14 15:23:51 -0800747 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
748 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700749 {
750 auto iface = objectServer.add_interface(
751 cpuInventoryPath + std::string("/") + name,
752 "xyz.openbmc_project.Inventory.Item");
753 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800754 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700755 iface->initialize();
756 inventoryIfaces[name] = std::move(iface);
757 }
James Feist58295ad2019-05-30 15:01:41 -0700758
Zev Weiss8908b3c2022-08-12 18:21:01 -0700759 auto findBus = cfg.find("Bus");
760 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700761 {
762 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
763 continue;
764 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400765 uint64_t bus =
766 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700767
Zev Weiss8908b3c2022-08-12 18:21:01 -0700768 auto findAddress = cfg.find("Address");
769 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700770 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400771 std::cerr
772 << "Can't find 'Address' setting in " << name << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700773 continue;
774 }
James Feist3eb82622019-02-08 13:10:22 -0800775 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
776 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700777
Ed Tanous8a57ec02020-10-09 12:46:52 -0700778 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700779 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700780 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700781 std::cout << "addr: " << addr << "\n";
782 std::cout << "name: " << name << "\n";
783 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700784 }
785
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800786 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700787 }
788 }
789 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700790
Ed Tanous2049bd22022-07-09 07:20:26 -0700791 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700792 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800793 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700794 << " parsed\n";
795 return true;
796 }
797
798 return false;
James Feist6714a252018-09-10 15:26:18 -0700799}
800
James Feistb6c0b912019-07-09 12:21:44 -0700801int main()
James Feist6714a252018-09-10 15:26:18 -0700802{
Ed Tanous1f978632023-02-28 18:16:39 -0800803 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700804 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800805 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700806
Ed Tanous14ed5e92022-07-12 15:50:23 -0700807 sdbusplus::asio::object_server objectServer(systemBus, true);
808 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700809 boost::asio::steady_timer pingTimer(io);
810 boost::asio::steady_timer creationTimer(io);
811 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800812 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700813
Ed Tanous83db50c2023-03-01 10:20:24 -0800814 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700815 filterTimer.async_wait([&](const boost::system::error_code& ec) {
816 if (ec == boost::asio::error::operation_aborted)
817 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700818 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700819 }
820
James Feistc140e202019-11-14 15:23:51 -0800821 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700822 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800823 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800824 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700825 }
826 });
827
Patrick Williams92f8f512022-07-22 19:26:55 -0500828 std::function<void(sdbusplus::message_t&)> eventHandler =
829 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400830 if (message.is_method_error())
James Feist6714a252018-09-10 15:26:18 -0700831 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400832 std::cerr << "callback method error\n";
833 return;
James Feist6714a252018-09-10 15:26:18 -0700834 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700835
Patrick Williams2aaf7172024-08-16 15:20:40 -0400836 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700837 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400838 std::cout << message.get_path() << " is changed\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700839 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400840
841 // this implicitly cancels the timer
842 filterTimer.expires_after(std::chrono::seconds(1));
843 filterTimer.async_wait([&](const boost::system::error_code& ec) {
844 if (ec == boost::asio::error::operation_aborted)
845 {
846 return; // we're being canceled
847 }
848
849 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
850 objectServer))
851 {
852 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
853 systemBus, cpuConfigs, sensorConfigs);
854 }
855 });
856 };
James Feist6714a252018-09-10 15:26:18 -0700857
Zev Weiss214d9712022-08-12 12:54:31 -0700858 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
859 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700860
Thu Nguyen255da6b2022-07-29 10:05:52 +0700861 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800862
863 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700864 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700865 return 0;
James Feist6714a252018-09-10 15:26:18 -0700866}