blob: 665daa849430db1cef2bc59a9895650e8eb278f4 [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"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070022
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070025#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
Jakub Nowackia38e5612023-03-08 12:27:01 +010031#include <cerrno>
James Feist24f02f22019-04-15 11:05:39 -070032#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070033#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <functional>
35#include <memory>
James Feist6714a252018-09-10 15:26:18 -070036#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <sstream>
38#include <stdexcept>
39#include <string>
40#include <utility>
41#include <variant>
42#include <vector>
James Feist6714a252018-09-10 15:26:18 -070043
James Feistf87dc4c2018-12-05 14:39:51 -080044// clang-format off
45// this needs to be included last or we'll have build issues
46#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080047#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
48#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
49#endif
James Feistf87dc4c2018-12-05 14:39:51 -080050// clang-format on
51
Ed Tanous8a57ec02020-10-09 12:46:52 -070052static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070053
Thu Nguyen255da6b2022-07-29 10:05:52 +070054boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
55 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080056boost::container::flat_map<std::string,
57 std::shared_ptr<sdbusplus::asio::dbus_interface>>
58 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080059
James Feist6714a252018-09-10 15:26:18 -070060enum State
61{
62 OFF, // host powered down
63 ON, // host powered on
64 READY // host powered on and mem test passed - fully ready
65};
66
67struct CPUConfig
68{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070069 CPUConfig(const uint64_t& bus, const uint64_t& addr,
70 const std::string& name, const State& state) :
71 bus(bus),
72 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070073 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070074 int bus;
James Feist6714a252018-09-10 15:26:18 -070075 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070076 std::string name;
James Feist6714a252018-09-10 15:26:18 -070077 State state;
78
79 bool operator<(const CPUConfig& rhs) const
80 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093081 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070082 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070083 }
84};
85
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070086static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070088
James Feistcf3bce62019-01-08 10:07:19 -080089namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080090
Zev Weiss054aad82022-08-18 01:37:34 -070091static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080092static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070093 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070094
Jae Hyun Yood64262b2018-11-01 13:31:16 -070095void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -070096 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -080097 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070098 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080099 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800100 boost::container::flat_set<CPUConfig>& cpuConfigs,
101 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700102
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200103std::string createSensorName(const std::string& label, const std::string& item,
104 const int& cpuId)
105{
106 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700107 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200108 {
109 sensorName += " " + item;
110 }
111 sensorName += " CPU" + std::to_string(cpuId);
112 // converting to Upper Camel case whole name
113 bool isWordEnd = true;
114 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
115 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700116 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700117 {
118 isWordEnd = true;
119 }
120 else
121 {
122 if (isWordEnd)
123 {
124 isWordEnd = false;
125 return std::toupper(c);
126 }
127 }
128 return c;
129 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200130 return sensorName;
131}
132
Ed Tanous1f978632023-02-28 18:16:39 -0800133bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800134 sdbusplus::asio::object_server& objectServer,
135 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
136 boost::container::flat_set<CPUConfig>& cpuConfigs,
137 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700138{
139 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800140 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700141 {
142 if (cpu.state != State::OFF)
143 {
144 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500145 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800146 inventoryIfaces[cpu.name];
147 if (iface != nullptr)
148 {
149 continue;
150 }
151 iface = objectServer.add_interface(
152 cpuInventoryPath + std::string("/") + cpu.name,
153 "xyz.openbmc_project.Inventory.Item");
154 iface->register_property("PrettyName", cpu.name);
155 iface->register_property("Present", true);
156 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700157 }
158 }
159 if (!available)
160 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700161 return false;
James Feist6714a252018-09-10 15:26:18 -0700162 }
163
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800164 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700165 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800166 return false;
James Feist6714a252018-09-10 15:26:18 -0700167 }
168
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700169 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800170 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
171 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700172 {
173 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700174 return true;
James Feist6714a252018-09-10 15:26:18 -0700175 }
176
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700177 boost::container::flat_set<std::string> scannedDirectories;
178 boost::container::flat_set<std::string> createdSensors;
179
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800180 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700181 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700182 auto hwmonDirectory = hwmonNamePath.parent_path();
183
184 auto ret = scannedDirectories.insert(hwmonDirectory.string());
185 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700186 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700187 continue; // already searched this path
188 }
189
190 fs::path::iterator it = hwmonNamePath.begin();
191 std::advance(it, 6); // pick the 6th part for a PECI client device name
192 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700193 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 if (findHyphen == std::string::npos)
195 {
196 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700197 continue;
198 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700199 std::string busStr = deviceName.substr(0, findHyphen);
200 std::string addrStr = deviceName.substr(findHyphen + 1);
201
202 size_t bus = 0;
203 size_t addr = 0;
204 try
205 {
206 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700207 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700208 }
Patrick Williams26601e82021-10-06 12:43:25 -0500209 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700210 {
211 continue;
212 }
213
214 std::ifstream nameFile(hwmonNamePath);
215 if (!nameFile.good())
216 {
217 std::cerr << "Failure reading " << hwmonNamePath << "\n";
218 continue;
219 }
220 std::string hwmonName;
221 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700222 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800223 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700224 {
225 // shouldn't have an empty name file
226 continue;
227 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700228 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700229 {
230 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
231 << "\n";
232 }
James Feist6714a252018-09-10 15:26:18 -0700233
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700234 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700235 const SensorData* sensorData = nullptr;
236 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700237 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700238
Zev Weiss8908b3c2022-08-12 18:21:01 -0700239 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700240 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700241 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700242 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700244 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700245 auto sensorBase =
246 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700247 if (sensorBase != sensorData->end())
248 {
249 baseConfiguration = &(*sensorBase);
250 break;
251 }
252 }
253 if (baseConfiguration == nullptr)
254 {
255 std::cerr << "error finding base configuration for" << hwmonName
256 << "\n";
257 continue;
258 }
259 auto configurationBus = baseConfiguration->second.find("Bus");
260 auto configurationAddress =
261 baseConfiguration->second.find("Address");
262
263 if (configurationBus == baseConfiguration->second.end() ||
264 configurationAddress == baseConfiguration->second.end())
265 {
266 std::cerr << "error finding bus or address in configuration";
267 continue;
268 }
269
James Feist3eb82622019-02-08 13:10:22 -0800270 if (std::get<uint64_t>(configurationBus->second) != bus ||
271 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700272 {
273 continue;
274 }
275
Zev Weiss8908b3c2022-08-12 18:21:01 -0700276 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700277 break;
278 }
279 if (interfacePath == nullptr)
280 {
281 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700282 continue;
283 }
284
285 auto findCpuId = baseConfiguration->second.find("CpuID");
286 if (findCpuId == baseConfiguration->second.end())
287 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700288 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700289 continue;
290 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700291 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800292 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700293
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700294 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700295 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200296 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200297 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700298 {
299 std::cerr << "No temperature sensors in system\n";
300 continue;
301 }
302
303 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800304 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700305 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200306 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200307 if (!fileParts)
308 {
309 continue;
310 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200311 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700312 auto inputPathStr = inputPath.string();
313 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200314 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700315 std::ifstream labelFile(labelPath);
316 if (!labelFile.good())
317 {
318 std::cerr << "Failure reading " << labelPath << "\n";
319 continue;
320 }
321 std::string label;
322 std::getline(labelFile, label);
323 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800324
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200325 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700326
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800327 auto findSensor = gCpuSensors.find(sensorName);
328 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700329 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700330 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 {
332 std::cout << "Skipped: " << inputPath << ": " << sensorName
333 << " is already created\n";
334 }
335 continue;
336 }
337
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800338 // check hidden properties
339 bool show = true;
340 for (const char* prop : hiddenProps)
341 {
342 if (label == prop)
343 {
344 show = false;
345 break;
346 }
347 }
348
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700349 /*
350 * Find if there is DtsCritOffset is configured in config file
351 * set it if configured or else set it to 0
352 */
353 double dtsOffset = 0;
354 if (label == "DTS")
355 {
356 auto findThrOffset =
357 baseConfiguration->second.find("DtsCritOffset");
358 if (findThrOffset != baseConfiguration->second.end())
359 {
360 dtsOffset = std::visit(VariantToDoubleVisitor(),
361 findThrOffset->second);
362 }
363 }
364
James Feist6714a252018-09-10 15:26:18 -0700365 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700366 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700367 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700368 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800369 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700370 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700371 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700372 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700373 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700374 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700375 std::cerr << "error populating thresholds for "
376 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700377 }
378 }
James Feistc140e202019-11-14 15:23:51 -0800379 auto& sensorPtr = gCpuSensors[sensorName];
380 // make sure destructor fires before creating a new one
381 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700382 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700383 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800384 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700385 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530386 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700387 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700388 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700389 {
James Feist6714a252018-09-10 15:26:18 -0700390 std::cout << "Mapped: " << inputPath << " to " << sensorName
391 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700392 }
James Feist6714a252018-09-10 15:26:18 -0700393 }
394 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700395
Ed Tanous2049bd22022-07-09 07:20:26 -0700396 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700397 {
398 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
399 << " created\n";
400 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700401
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700402 return true;
James Feist6714a252018-09-10 15:26:18 -0700403}
404
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700405void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700406{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700407 std::ostringstream hex;
408 hex << std::hex << config.addr;
409 const std::string& addrHexStr = hex.str();
410 std::string busStr = std::to_string(config.bus);
411
412 std::string parameters = "peci-client 0x" + addrHexStr;
413 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
414
James Feistcf3bce62019-01-08 10:07:19 -0800415 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700416 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800417 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700418 {
James Feistcf3bce62019-01-08 10:07:19 -0800419 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700420 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700421 continue;
James Feist6714a252018-09-10 15:26:18 -0700422 }
423
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700424 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700425 if (directoryName.starts_with(busStr) &&
426 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700427 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700428 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700429 {
430 std::cout << parameters << " on bus " << busStr
431 << " is already exported\n";
432 }
433 return;
434 }
James Feist6714a252018-09-10 15:26:18 -0700435 }
436
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700437 std::ofstream deviceFile(device);
438 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700439 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700440 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700441 return;
442 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700443 deviceFile << parameters;
444 deviceFile.close();
445
446 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700447}
448
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700449void detectCpu(boost::asio::steady_timer& pingTimer,
450 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800451 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800452 sdbusplus::asio::object_server& objectServer,
453 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
454 boost::container::flat_set<CPUConfig>& cpuConfigs,
455 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700456{
James Feist6714a252018-09-10 15:26:18 -0700457 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700458 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700459
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800460 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700461 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700462 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800463
464 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700465 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
466 if (file < 0)
467 {
Jakub Nowackia38e5612023-03-08 12:27:01 +0100468 std::cerr << "unable to open " << peciDevPath << " "
469 << std::strerror(errno) << "\n";
470 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
471 dbusConnection, cpuConfigs, sensorConfigs);
472 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700473 }
474
Ed Tanousa771f6a2022-01-14 09:36:51 -0800475 State newState = State::OFF;
476 struct peci_ping_msg msg
477 {};
James Feist6714a252018-09-10 15:26:18 -0700478 msg.addr = config.addr;
Ed Tanous99c44092022-01-14 09:59:09 -0800479
480 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700481 if (ioctl(file, PECI_IOC_PING, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700482 {
483 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700484 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700485 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800486 struct peci_rd_pkg_cfg_msg msg
487 {};
James Feist6714a252018-09-10 15:26:18 -0700488 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800489 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700490 msg.param = rank;
491 msg.rx_len = 4;
Ed Tanous99c44092022-01-14 09:59:09 -0800492
493 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700494 if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700495 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700496 if ((msg.pkg_config[0] != 0U) ||
497 (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700498 {
499 dimmReady = true;
500 break;
501 }
502 }
503 else
504 {
505 break;
506 }
507 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700508
James Feist6714a252018-09-10 15:26:18 -0700509 if (dimmReady)
510 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700511 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700512 }
513 else
514 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700515 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700516 }
517 }
James Feist6714a252018-09-10 15:26:18 -0700518
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700519 close(file);
520
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700521 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700522 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700523 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700524 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700525 if (config.state == State::OFF)
526 {
527 std::cout << config.name << " is detected\n";
528 exportDevice(config);
529 }
James Feist6714a252018-09-10 15:26:18 -0700530
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700531 if (newState == State::ON)
532 {
533 rescanDelaySeconds = 3;
534 }
535 else if (newState == State::READY)
536 {
537 rescanDelaySeconds = 5;
538 std::cout << "DIMM(s) on " << config.name
539 << " is/are detected\n";
540 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700541 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700542
543 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700544 }
545
546 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700547 {
548 keepPinging = true;
549 }
550
Ed Tanous8a57ec02020-10-09 12:46:52 -0700551 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700552 {
553 std::cout << config.name << ", state: " << config.state << "\n";
554 }
James Feist6714a252018-09-10 15:26:18 -0700555 }
556
Ed Tanous2049bd22022-07-09 07:20:26 -0700557 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700558 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800559 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700560 creationTimer.async_wait([&](const boost::system::error_code& ec) {
561 if (ec == boost::asio::error::operation_aborted)
562 {
563 return; // we're being canceled
564 }
565
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800566 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700567 sensorConfigs) ||
568 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700569 {
570 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800571 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700572 }
573 });
James Feist6714a252018-09-10 15:26:18 -0700574 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700575 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700576 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800577 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800578 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700579 }
580}
581
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700582void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700583 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800584 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700585 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800586 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800587 boost::container::flat_set<CPUConfig>& cpuConfigs,
588 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700589{
Ed Tanous83db50c2023-03-01 10:20:24 -0800590 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700591 pingTimer.async_wait([&](const boost::system::error_code& ec) {
592 if (ec == boost::asio::error::operation_aborted)
593 {
594 return; // we're being canceled
595 }
596
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800597 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800598 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700599 });
600}
601
James Feistc140e202019-11-14 15:23:51 -0800602bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
603 boost::container::flat_set<CPUConfig>& cpuConfigs,
604 ManagedObjectType& sensorConfigs,
605 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700606{
James Feist6714a252018-09-10 15:26:18 -0700607 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800608 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700609 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700610 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700611 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700612 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700613 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700614 return false;
James Feist6714a252018-09-10 15:26:18 -0700615 }
616 useCache = true;
617 }
618
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700619 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700620 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700621 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700622 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700623 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700624 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700625 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700626 {
Zev Weiss054aad82022-08-18 01:37:34 -0700627 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700628 {
629 continue;
630 }
631
Zev Weiss8908b3c2022-08-12 18:21:01 -0700632 auto findName = cfg.find("Name");
633 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700634 {
635 continue;
636 }
James Feist3eb82622019-02-08 13:10:22 -0800637 std::string nameRaw =
638 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700639 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700640 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700641
James Feistc140e202019-11-14 15:23:51 -0800642 auto present = std::optional<bool>();
643 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700644 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700645 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700646 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700647 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700648 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700649 break;
James Feist58295ad2019-05-30 15:01:41 -0700650 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700651 }
652
James Feistc140e202019-11-14 15:23:51 -0800653 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
654 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700655 {
656 auto iface = objectServer.add_interface(
657 cpuInventoryPath + std::string("/") + name,
658 "xyz.openbmc_project.Inventory.Item");
659 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800660 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700661 iface->initialize();
662 inventoryIfaces[name] = std::move(iface);
663 }
James Feist58295ad2019-05-30 15:01:41 -0700664
Zev Weiss8908b3c2022-08-12 18:21:01 -0700665 auto findBus = cfg.find("Bus");
666 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700667 {
668 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
669 continue;
670 }
James Feist3eb82622019-02-08 13:10:22 -0800671 uint64_t bus =
672 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700673
Zev Weiss8908b3c2022-08-12 18:21:01 -0700674 auto findAddress = cfg.find("Address");
675 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700676 {
677 std::cerr << "Can't find 'Address' setting in " << name
678 << "\n";
679 continue;
680 }
James Feist3eb82622019-02-08 13:10:22 -0800681 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
682 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700683
Ed Tanous8a57ec02020-10-09 12:46:52 -0700684 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700685 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700686 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700687 std::cout << "addr: " << addr << "\n";
688 std::cout << "name: " << name << "\n";
689 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700690 }
691
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800692 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700693 }
694 }
695 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700696
Ed Tanous2049bd22022-07-09 07:20:26 -0700697 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800699 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700700 << " parsed\n";
701 return true;
702 }
703
704 return false;
James Feist6714a252018-09-10 15:26:18 -0700705}
706
James Feistb6c0b912019-07-09 12:21:44 -0700707int main()
James Feist6714a252018-09-10 15:26:18 -0700708{
Ed Tanous1f978632023-02-28 18:16:39 -0800709 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700710 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800711 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700712
Ed Tanous14ed5e92022-07-12 15:50:23 -0700713 sdbusplus::asio::object_server objectServer(systemBus, true);
714 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700715 boost::asio::steady_timer pingTimer(io);
716 boost::asio::steady_timer creationTimer(io);
717 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800718 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700719
Ed Tanous83db50c2023-03-01 10:20:24 -0800720 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700721 filterTimer.async_wait([&](const boost::system::error_code& ec) {
722 if (ec == boost::asio::error::operation_aborted)
723 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700724 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700725 }
726
James Feistc140e202019-11-14 15:23:51 -0800727 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700728 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800729 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800730 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700731 }
732 });
733
Patrick Williams92f8f512022-07-22 19:26:55 -0500734 std::function<void(sdbusplus::message_t&)> eventHandler =
735 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700736 if (message.is_method_error())
737 {
738 std::cerr << "callback method error\n";
739 return;
740 }
741
742 if (debug)
743 {
744 std::cout << message.get_path() << " is changed\n";
745 }
746
747 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800748 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700749 filterTimer.async_wait([&](const boost::system::error_code& ec) {
750 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700751 {
Ed Tanousbb679322022-05-16 16:10:00 -0700752 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700753 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700754
Ed Tanousbb679322022-05-16 16:10:00 -0700755 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
756 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700757 {
Ed Tanousbb679322022-05-16 16:10:00 -0700758 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
759 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700760 }
Ed Tanousbb679322022-05-16 16:10:00 -0700761 });
762 };
James Feist6714a252018-09-10 15:26:18 -0700763
Zev Weiss214d9712022-08-12 12:54:31 -0700764 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
765 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700766
Thu Nguyen255da6b2022-07-29 10:05:52 +0700767 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800768
769 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700770 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700771 return 0;
James Feist6714a252018-09-10 15:26:18 -0700772}