blob: eede83813350fce12e8ee588332fc2bb38cdbe13 [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>
George Liu4ab892a2025-02-20 14:35:49 +080030#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070035
Ed Tanouseacbfdd2024-04-04 12:00:24 -070036#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070037#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <cctype>
Jakub Nowackia38e5612023-03-08 12:27:01 +010039#include <cerrno>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <chrono>
41#include <cstddef>
42#include <cstdint>
43#include <cstring>
James Feist24f02f22019-04-15 11:05:39 -070044#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070045#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070046#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070047#include <ios>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070048#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
Zev Weiss054aad82022-08-18 01:37:34 -0700104static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -0800105static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +0700106 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -0700107
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700108void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700109 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800110 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700111 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800113 boost::container::flat_set<CPUConfig>& cpuConfigs,
114 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700115
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200116std::string createSensorName(const std::string& label, const std::string& item,
117 const int& cpuId)
118{
119 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700120 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200121 {
122 sensorName += " " + item;
123 }
PavanKumarIntel40baf772024-07-03 14:32:57 +0000124
125 std::string cpuStr = "CPU" + std::to_string(cpuId);
126 constexpr const char* subLabel = "DIMM";
127 std::size_t found = label.find(subLabel);
128 if (found != std::string::npos)
129 {
130 sensorName = cpuStr + " " + sensorName;
131 }
132 else
133 {
134 sensorName += " " + cpuStr;
135 }
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200136 // converting to Upper Camel case whole name
137 bool isWordEnd = true;
138 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
139 [&isWordEnd](int c) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400140 if (std::isspace(c) != 0)
141 {
142 isWordEnd = true;
143 }
144 else
145 {
146 if (isWordEnd)
147 {
148 isWordEnd = false;
149 return std::toupper(c);
150 }
151 }
152 return c;
153 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200154 return sensorName;
155}
156
Ed Tanous1f978632023-02-28 18:16:39 -0800157bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800158 sdbusplus::asio::object_server& objectServer,
159 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
160 boost::container::flat_set<CPUConfig>& cpuConfigs,
161 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700162{
163 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800164 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700165 {
166 if (cpu.state != State::OFF)
167 {
168 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500169 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800170 inventoryIfaces[cpu.name];
171 if (iface != nullptr)
172 {
173 continue;
174 }
175 iface = objectServer.add_interface(
176 cpuInventoryPath + std::string("/") + cpu.name,
177 "xyz.openbmc_project.Inventory.Item");
178 iface->register_property("PrettyName", cpu.name);
179 iface->register_property("Present", true);
180 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700181 }
182 }
183 if (!available)
184 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700185 return false;
James Feist6714a252018-09-10 15:26:18 -0700186 }
187
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800188 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700189 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800190 return false;
James Feist6714a252018-09-10 15:26:18 -0700191 }
192
Ed Tanous2e466962025-01-30 10:59:49 -0800193 std::vector<std::filesystem::path> hwmonNamePaths;
194 findFiles(std::filesystem::path(peciDevPath),
Paul Fertser6e699732023-03-29 12:58:30 +0000195 R"(peci-\d+/\d+-.+/peci[-_].+/hwmon/hwmon\d+/name$)",
196 hwmonNamePaths, 6);
197 if (hwmonNamePaths.empty())
James Feist6714a252018-09-10 15:26:18 -0700198 {
George Liu4ab892a2025-02-20 14:35:49 +0800199 lg2::error("No CPU sensors in system");
Paul Fertser75f92582023-03-29 12:48:07 +0000200 return false;
James Feist6714a252018-09-10 15:26:18 -0700201 }
202
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 boost::container::flat_set<std::string> scannedDirectories;
204 boost::container::flat_set<std::string> createdSensors;
205
Ed Tanous2e466962025-01-30 10:59:49 -0800206 for (const std::filesystem::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700207 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700208 auto hwmonDirectory = hwmonNamePath.parent_path();
209
210 auto ret = scannedDirectories.insert(hwmonDirectory.string());
211 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700212 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 continue; // already searched this path
214 }
215
Ed Tanous2e466962025-01-30 10:59:49 -0800216 std::filesystem::path::iterator it = hwmonNamePath.begin();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700217 std::advance(it, 6); // pick the 6th part for a PECI client device name
218 std::string deviceName = *it;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700219
220 size_t bus = 0;
221 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000222 if (!getDeviceBusAddr(deviceName, bus, addr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700223 {
224 continue;
225 }
226
227 std::ifstream nameFile(hwmonNamePath);
228 if (!nameFile.good())
229 {
George Liu4ab892a2025-02-20 14:35:49 +0800230 lg2::error("Failure reading '{PATH}'", "PATH", hwmonNamePath);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700231 continue;
232 }
233 std::string hwmonName;
234 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700235 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800236 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700237 {
238 // shouldn't have an empty name file
239 continue;
240 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700241 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 {
George Liu4ab892a2025-02-20 14:35:49 +0800243 lg2::info("Checking: '{PATH}': '{NAME}'", "PATH", hwmonNamePath,
244 "NAME", hwmonName);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 }
James Feist6714a252018-09-10 15:26:18 -0700246
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700247 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700248 const SensorData* sensorData = nullptr;
249 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700250 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700251
Zev Weiss8908b3c2022-08-12 18:21:01 -0700252 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700253 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700254 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700255 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700256 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700257 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700258 auto sensorBase =
259 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700260 if (sensorBase != sensorData->end())
261 {
262 baseConfiguration = &(*sensorBase);
263 break;
264 }
265 }
266 if (baseConfiguration == nullptr)
267 {
George Liu4ab892a2025-02-20 14:35:49 +0800268 lg2::error("error finding base configuration for '{NAME}'",
269 "NAME", hwmonName);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700270 continue;
271 }
272 auto configurationBus = baseConfiguration->second.find("Bus");
273 auto configurationAddress =
274 baseConfiguration->second.find("Address");
275
276 if (configurationBus == baseConfiguration->second.end() ||
277 configurationAddress == baseConfiguration->second.end())
278 {
George Liu4ab892a2025-02-20 14:35:49 +0800279 lg2::error("error finding bus or address in configuration");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700280 continue;
281 }
282
James Feist3eb82622019-02-08 13:10:22 -0800283 if (std::get<uint64_t>(configurationBus->second) != bus ||
284 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700285 {
286 continue;
287 }
288
Zev Weiss8908b3c2022-08-12 18:21:01 -0700289 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700290 break;
291 }
292 if (interfacePath == nullptr)
293 {
George Liu4ab892a2025-02-20 14:35:49 +0800294 lg2::error("failed to find match for '{NAME}'", "NAME", hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700295 continue;
296 }
297
298 auto findCpuId = baseConfiguration->second.find("CpuID");
299 if (findCpuId == baseConfiguration->second.end())
300 {
George Liu4ab892a2025-02-20 14:35:49 +0800301 lg2::error("could not determine CPU ID for '{NAME}'", "NAME",
302 hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700303 continue;
304 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400305 int cpuId =
306 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700307
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700308 auto directory = hwmonNamePath.parent_path();
Ed Tanous2e466962025-01-30 10:59:49 -0800309 std::vector<std::filesystem::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200310 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200311 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700312 {
George Liu4ab892a2025-02-20 14:35:49 +0800313 lg2::error("No temperature sensors in system");
James Feist6714a252018-09-10 15:26:18 -0700314 continue;
315 }
316
317 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800318 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700319 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200320 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200321 if (!fileParts)
322 {
323 continue;
324 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200325 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700326 auto inputPathStr = inputPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400327 auto labelPath =
328 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700329 std::ifstream labelFile(labelPath);
330 if (!labelFile.good())
331 {
George Liu4ab892a2025-02-20 14:35:49 +0800332 lg2::error("Failure reading '{PATH}'", "PATH", labelPath);
James Feist6714a252018-09-10 15:26:18 -0700333 continue;
334 }
335 std::string label;
336 std::getline(labelFile, label);
337 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800338
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200339 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700340
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800341 auto findSensor = gCpuSensors.find(sensorName);
342 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700343 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700344 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700345 {
George Liu4ab892a2025-02-20 14:35:49 +0800346 lg2::info("Skipped: '{PATH}': '{NAME}' is already created",
347 "PATH", inputPath, "NAME", sensorName);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700348 }
349 continue;
350 }
351
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800352 // check hidden properties
353 bool show = true;
354 for (const char* prop : hiddenProps)
355 {
356 if (label == prop)
357 {
358 show = false;
359 break;
360 }
361 }
362
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700363 /*
364 * Find if there is DtsCritOffset is configured in config file
365 * set it if configured or else set it to 0
366 */
367 double dtsOffset = 0;
368 if (label == "DTS")
369 {
370 auto findThrOffset =
371 baseConfiguration->second.find("DtsCritOffset");
372 if (findThrOffset != baseConfiguration->second.end())
373 {
374 dtsOffset = std::visit(VariantToDoubleVisitor(),
375 findThrOffset->second);
376 }
377 }
378
James Feist6714a252018-09-10 15:26:18 -0700379 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700380 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700381 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700382 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800383 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700384 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700385 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700386 IntelCPUSensor::sensorScaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500387 dtsOffset, 0))
James Feist6714a252018-09-10 15:26:18 -0700388 {
George Liu4ab892a2025-02-20 14:35:49 +0800389 lg2::error("error populating thresholds for '{NAME}'",
390 "NAME", sensorName);
James Feist6714a252018-09-10 15:26:18 -0700391 }
392 }
James Feistc140e202019-11-14 15:23:51 -0800393 auto& sensorPtr = gCpuSensors[sensorName];
394 // make sure destructor fires before creating a new one
395 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700396 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700397 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800398 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700399 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530400 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700401 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700402 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700403 {
George Liu4ab892a2025-02-20 14:35:49 +0800404 lg2::info("Mapped: '{PATH}' to '{NAME}'", "PATH", inputPath,
405 "NAME", sensorName);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406 }
James Feist6714a252018-09-10 15:26:18 -0700407 }
408 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700409
Ed Tanous2049bd22022-07-09 07:20:26 -0700410 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700411 {
George Liu4ab892a2025-02-20 14:35:49 +0800412 if (createdSensors.size() == 1)
413 {
414 lg2::info("Sensor is created");
415 }
416 else
417 {
418 lg2::info("Sensors are created");
419 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700420 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700421
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700422 return true;
James Feist6714a252018-09-10 15:26:18 -0700423}
424
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800425bool exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700426{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700427 std::ostringstream hex;
428 hex << std::hex << config.addr;
429 const std::string& addrHexStr = hex.str();
430 std::string busStr = std::to_string(config.bus);
431
432 std::string parameters = "peci-client 0x" + addrHexStr;
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800433 std::string devPath = peciDevPath;
434 std::string delDevice = devPath + "peci-" + busStr + "/delete_device";
435 std::string newDevice = devPath + "peci-" + busStr + "/new_device";
436 std::string newClient = devPath + busStr + "-" + addrHexStr + "/driver";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700437
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800438 std::filesystem::path devicePath(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700439 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800440 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700441 {
James Feistcf3bce62019-01-08 10:07:19 -0800442 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700443 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700444 continue;
James Feist6714a252018-09-10 15:26:18 -0700445 }
446
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700447 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700448 if (directoryName.starts_with(busStr) &&
449 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700450 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700451 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700452 {
George Liu4ab892a2025-02-20 14:35:49 +0800453 lg2::info("'{PARAMETERS}' on bus '{BUS}' is already exported",
454 "PARAMETERS", parameters, "BUS", busStr);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700455 }
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800456
457 std::ofstream delDeviceFile(delDevice);
458 if (!delDeviceFile.good())
459 {
George Liu4ab892a2025-02-20 14:35:49 +0800460 lg2::error("Error opening '{DEVICE}'", "DEVICE", delDevice);
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800461 return false;
462 }
463 delDeviceFile << parameters;
464 delDeviceFile.close();
465
466 break;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700467 }
James Feist6714a252018-09-10 15:26:18 -0700468 }
469
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800470 std::ofstream deviceFile(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700471 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700472 {
George Liu4ab892a2025-02-20 14:35:49 +0800473 lg2::error("Error opening '{DEVICE}'", "DEVICE", newDevice);
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800474 return false;
James Feist6714a252018-09-10 15:26:18 -0700475 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700476 deviceFile << parameters;
477 deviceFile.close();
478
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800479 if (!std::filesystem::exists(newClient))
480 {
George Liu4ab892a2025-02-20 14:35:49 +0800481 lg2::error("Error creating '{CLIENT}'", "CLIENT", newClient);
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800482 return false;
483 }
484
George Liu4ab892a2025-02-20 14:35:49 +0800485 lg2::info("'{PARAMETERS}' on bus '{BUS}' is exported", "PARAMETERS",
486 parameters, "BUS", busStr);
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800487
488 return true;
James Feist6714a252018-09-10 15:26:18 -0700489}
490
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700491void detectCpu(boost::asio::steady_timer& pingTimer,
492 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800493 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800494 sdbusplus::asio::object_server& objectServer,
495 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
496 boost::container::flat_set<CPUConfig>& cpuConfigs,
497 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700498{
James Feist6714a252018-09-10 15:26:18 -0700499 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700500 static bool keepPinging = false;
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200501 int peciFd = -1;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700502
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800503 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700504 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800505 if (config.state == State::READY)
506 {
507 continue;
508 }
509
Paul Fertser6e699732023-03-29 12:58:30 +0000510 std::fstream rescan{rescanPath, std::ios::out};
511 if (rescan.is_open())
512 {
Ed Tanous2e466962025-01-30 10:59:49 -0800513 std::vector<std::filesystem::path> peciPaths;
Paul Fertser6e699732023-03-29 12:58:30 +0000514 std::ostringstream searchPath;
515 searchPath << std::hex << "peci-" << config.bus << "/" << config.bus
516 << "-" << config.addr;
Ed Tanous2e466962025-01-30 10:59:49 -0800517 findFiles(std::filesystem::path(peciDevPath + searchPath.str()),
Paul Fertser6e699732023-03-29 12:58:30 +0000518 R"(peci_cpu.dimmtemp.+/hwmon/hwmon\d+/name$)", peciPaths,
519 3);
520 if (!peciPaths.empty())
521 {
522 config.state = State::READY;
523 rescanDelaySeconds = 1;
524 }
525 else
526 {
Ed Tanous2e466962025-01-30 10:59:49 -0800527 findFiles(std::filesystem::path(peciDevPath + searchPath.str()),
Paul Fertser6e699732023-03-29 12:58:30 +0000528 R"(peci_cpu.cputemp.+/hwmon/hwmon\d+/name$)",
529 peciPaths, 3);
530 if (!peciPaths.empty())
531 {
532 config.state = State::ON;
533 rescanDelaySeconds = 3;
534 }
535 else
536 {
537 // https://www.kernel.org/doc/html/latest/admin-guide/abi-testing.html#abi-sys-bus-peci-rescan
538 rescan << "1";
539 }
540 }
541 if (config.state != State::READY)
542 {
543 keepPinging = true;
544 }
545
546 continue;
547 }
548
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700549 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800550
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200551 peci_SetDevName(peciDevPath.data());
552
Ed Tanous99c44092022-01-14 09:59:09 -0800553 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200554 if ((peci_Lock(&peciFd, PECI_NO_WAIT) != PECI_CC_SUCCESS) ||
555 (peciFd < 0))
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700556 {
George Liu4ab892a2025-02-20 14:35:49 +0800557 lg2::error("unable to open '{PATH}', '{ERRNO}'", "PATH",
558 peciDevPath, "ERRNO", std::strerror(errno));
Jakub Nowackia38e5612023-03-08 12:27:01 +0100559 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
560 dbusConnection, cpuConfigs, sensorConfigs);
561 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700562 }
563
Ed Tanousa771f6a2022-01-14 09:36:51 -0800564 State newState = State::OFF;
Ed Tanous99c44092022-01-14 09:59:09 -0800565
566 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100567 if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700568 {
569 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700570 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700571 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100572 std::array<uint8_t, 8> pkgConfig{};
573 uint8_t cc = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800574
575 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100576 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_DDR_DIMM_TEMP,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400577 rank, 4, pkgConfig.data(), &cc) ==
578 PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700579 {
Matt Simmering38857572023-08-10 14:41:46 -0700580 // Depending on CPU generation, both 0 and 0xFF can be used
581 // to indicate no DIMM presence
582 if (((pkgConfig[0] != 0xFF) && (pkgConfig[0] != 0U)) ||
583 ((pkgConfig[1] != 0xFF) && (pkgConfig[1] != 0U)))
James Feist6714a252018-09-10 15:26:18 -0700584 {
585 dimmReady = true;
586 break;
587 }
588 }
589 else
590 {
591 break;
592 }
593 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700594
James Feist6714a252018-09-10 15:26:18 -0700595 if (dimmReady)
596 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700597 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700598 }
599 else
600 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700601 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700602 }
603 }
James Feist6714a252018-09-10 15:26:18 -0700604
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700605 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700606 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700607 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700608 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700609 if (config.state == State::OFF)
610 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800611 std::array<uint8_t, 8> pkgConfig{};
612 uint8_t cc = 0;
613
614 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_CPU_ID, 0,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400615 4, pkgConfig.data(), &cc) ==
616 PECI_CC_SUCCESS)
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800617 {
George Liu4ab892a2025-02-20 14:35:49 +0800618 lg2::info("'{NAME}' is detected", "NAME", config.name);
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800619 if (!exportDevice(config))
620 {
621 newState = State::OFF;
622 }
623 }
624 else
625 {
626 newState = State::OFF;
627 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700628 }
James Feist6714a252018-09-10 15:26:18 -0700629
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700630 if (newState == State::ON)
631 {
632 rescanDelaySeconds = 3;
633 }
634 else if (newState == State::READY)
635 {
636 rescanDelaySeconds = 5;
George Liu4ab892a2025-02-20 14:35:49 +0800637 lg2::info("DIMM(s) on '{NAME}' is/are detected", "NAME",
638 config.name);
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700639 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700640 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700641
642 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700643 }
644
645 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700646 {
647 keepPinging = true;
648 }
649
Ed Tanous8a57ec02020-10-09 12:46:52 -0700650 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700651 {
George Liu4ab892a2025-02-20 14:35:49 +0800652 lg2::info("'{NAME}', state: '{STATE}'", "NAME", config.name,
653 "STATE", config.state);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700654 }
Wang Xiaohua4cd5a902023-07-04 16:28:43 +0800655 peci_Unlock(peciFd);
James Feist6714a252018-09-10 15:26:18 -0700656 }
657
Ed Tanous2049bd22022-07-09 07:20:26 -0700658 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700659 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800660 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700661 creationTimer.async_wait([&](const boost::system::error_code& ec) {
662 if (ec == boost::asio::error::operation_aborted)
663 {
664 return; // we're being canceled
665 }
666
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800667 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700668 sensorConfigs) ||
669 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700670 {
671 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800672 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700673 }
674 });
James Feist6714a252018-09-10 15:26:18 -0700675 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700676 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700677 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800678 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800679 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700680 }
681}
682
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700683void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700684 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800685 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700686 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800687 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800688 boost::container::flat_set<CPUConfig>& cpuConfigs,
689 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700690{
Ed Tanous83db50c2023-03-01 10:20:24 -0800691 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700692 pingTimer.async_wait([&](const boost::system::error_code& ec) {
693 if (ec == boost::asio::error::operation_aborted)
694 {
695 return; // we're being canceled
696 }
697
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800698 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800699 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700700 });
701}
702
James Feistc140e202019-11-14 15:23:51 -0800703bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
704 boost::container::flat_set<CPUConfig>& cpuConfigs,
705 ManagedObjectType& sensorConfigs,
706 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700707{
James Feist6714a252018-09-10 15:26:18 -0700708 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800709 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700710 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700711 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700712 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700713 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700714 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700715 return false;
James Feist6714a252018-09-10 15:26:18 -0700716 }
717 useCache = true;
718 }
719
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700720 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700721 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700722 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700723 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700724 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700725 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700726 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700727 {
Zev Weiss054aad82022-08-18 01:37:34 -0700728 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700729 {
730 continue;
731 }
732
Zev Weiss8908b3c2022-08-12 18:21:01 -0700733 auto findName = cfg.find("Name");
734 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700735 {
736 continue;
737 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400738 std::string nameRaw =
739 std::visit(VariantToStringVisitor(), findName->second);
740 std::string name =
741 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700742
James Feistc140e202019-11-14 15:23:51 -0800743 auto present = std::optional<bool>();
744 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700745 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700746 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700747 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700748 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700749 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700750 break;
James Feist58295ad2019-05-30 15:01:41 -0700751 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700752 }
753
Patrick Williams80c68052025-05-13 23:31:49 -0400754 if (!inventoryIfaces.contains(name) && present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700755 {
756 auto iface = objectServer.add_interface(
757 cpuInventoryPath + std::string("/") + name,
758 "xyz.openbmc_project.Inventory.Item");
759 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800760 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700761 iface->initialize();
762 inventoryIfaces[name] = std::move(iface);
763 }
James Feist58295ad2019-05-30 15:01:41 -0700764
Zev Weiss8908b3c2022-08-12 18:21:01 -0700765 auto findBus = cfg.find("Bus");
766 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700767 {
George Liu4ab892a2025-02-20 14:35:49 +0800768 lg2::error("Can't find 'Bus' setting in '{NAME}'", "NAME",
769 name);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700770 continue;
771 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400772 uint64_t bus =
773 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700774
Zev Weiss8908b3c2022-08-12 18:21:01 -0700775 auto findAddress = cfg.find("Address");
776 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700777 {
George Liu4ab892a2025-02-20 14:35:49 +0800778 lg2::error("Can't find 'Address' setting in '{NAME}'",
779 "NAME", name);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700780 continue;
781 }
James Feist3eb82622019-02-08 13:10:22 -0800782 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
783 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700784
Ed Tanous8a57ec02020-10-09 12:46:52 -0700785 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700786 {
George Liu4ab892a2025-02-20 14:35:49 +0800787 lg2::info(
788 "bus: {BUS}, addr: {ADDR}, name: {NAME}, type: {TYPE}",
789 "BUS", bus, "ADDR", addr, "NAME", name, "TYPE", type);
James Feist6714a252018-09-10 15:26:18 -0700790 }
791
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800792 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700793 }
794 }
795 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700796
Ed Tanous2049bd22022-07-09 07:20:26 -0700797 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700798 {
George Liu4ab892a2025-02-20 14:35:49 +0800799 if (cpuConfigs.size() == 1)
800 {
801 lg2::info("CPU config is parsed");
802 }
803 else
804 {
805 lg2::info("CPU configs are parsed");
806 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700807 return true;
808 }
809
810 return false;
James Feist6714a252018-09-10 15:26:18 -0700811}
812
James Feistb6c0b912019-07-09 12:21:44 -0700813int main()
James Feist6714a252018-09-10 15:26:18 -0700814{
Ed Tanous1f978632023-02-28 18:16:39 -0800815 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700816 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800817 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700818
Ed Tanous14ed5e92022-07-12 15:50:23 -0700819 sdbusplus::asio::object_server objectServer(systemBus, true);
820 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700821 boost::asio::steady_timer pingTimer(io);
822 boost::asio::steady_timer creationTimer(io);
823 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800824 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700825
Ed Tanous83db50c2023-03-01 10:20:24 -0800826 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700827 filterTimer.async_wait([&](const boost::system::error_code& ec) {
828 if (ec == boost::asio::error::operation_aborted)
829 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700830 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700831 }
832
James Feistc140e202019-11-14 15:23:51 -0800833 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700834 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800835 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800836 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700837 }
838 });
839
Patrick Williams92f8f512022-07-22 19:26:55 -0500840 std::function<void(sdbusplus::message_t&)> eventHandler =
841 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400842 if (message.is_method_error())
James Feist6714a252018-09-10 15:26:18 -0700843 {
George Liu4ab892a2025-02-20 14:35:49 +0800844 lg2::error("callback method error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400845 return;
James Feist6714a252018-09-10 15:26:18 -0700846 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700847
Patrick Williams2aaf7172024-08-16 15:20:40 -0400848 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700849 {
George Liu4ab892a2025-02-20 14:35:49 +0800850 lg2::info("'{PATH}' is changed", "PATH", message.get_path());
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700851 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400852
853 // this implicitly cancels the timer
854 filterTimer.expires_after(std::chrono::seconds(1));
855 filterTimer.async_wait([&](const boost::system::error_code& ec) {
856 if (ec == boost::asio::error::operation_aborted)
857 {
858 return; // we're being canceled
859 }
860
861 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
862 objectServer))
863 {
864 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
865 systemBus, cpuConfigs, sensorConfigs);
866 }
867 });
868 };
James Feist6714a252018-09-10 15:26:18 -0700869
Zev Weiss214d9712022-08-12 12:54:31 -0700870 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
871 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700872
Thu Nguyen255da6b2022-07-29 10:05:52 +0700873 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800874
875 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700876 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700877 return 0;
James Feist6714a252018-09-10 15:26:18 -0700878}