blob: f0b5f8cea52ed2b6d284d5defa06635f02710eea [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>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
34#include <memory>
James Feist6714a252018-09-10 15:26:18 -070035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <sstream>
37#include <stdexcept>
38#include <string>
39#include <utility>
40#include <variant>
41#include <vector>
James Feist6714a252018-09-10 15:26:18 -070042
James Feistf87dc4c2018-12-05 14:39:51 -080043// clang-format off
44// this needs to be included last or we'll have build issues
45#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080046#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
47#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
48#endif
James Feistf87dc4c2018-12-05 14:39:51 -080049// clang-format on
50
Ed Tanous8a57ec02020-10-09 12:46:52 -070051static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070052
Thu Nguyen255da6b2022-07-29 10:05:52 +070053boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
54 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080055boost::container::flat_map<std::string,
56 std::shared_ptr<sdbusplus::asio::dbus_interface>>
57 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058
James Feist6714a252018-09-10 15:26:18 -070059enum State
60{
61 OFF, // host powered down
62 ON, // host powered on
63 READY // host powered on and mem test passed - fully ready
64};
65
66struct CPUConfig
67{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070068 CPUConfig(const uint64_t& bus, const uint64_t& addr,
69 const std::string& name, const State& state) :
70 bus(bus),
71 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070072 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070073 int bus;
James Feist6714a252018-09-10 15:26:18 -070074 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 std::string name;
James Feist6714a252018-09-10 15:26:18 -070076 State state;
77
78 bool operator<(const CPUConfig& rhs) const
79 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093080 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070081 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070082 }
83};
84
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070085static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070087
James Feistcf3bce62019-01-08 10:07:19 -080088namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080089
Zev Weiss054aad82022-08-18 01:37:34 -070090static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080091static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070092 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070093
Jae Hyun Yood64262b2018-11-01 13:31:16 -070094void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -070095 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -080096 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070097 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080098 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080099 boost::container::flat_set<CPUConfig>& cpuConfigs,
100 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700101
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200102std::string createSensorName(const std::string& label, const std::string& item,
103 const int& cpuId)
104{
105 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700106 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200107 {
108 sensorName += " " + item;
109 }
110 sensorName += " CPU" + std::to_string(cpuId);
111 // converting to Upper Camel case whole name
112 bool isWordEnd = true;
113 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
114 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700115 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700116 {
117 isWordEnd = true;
118 }
119 else
120 {
121 if (isWordEnd)
122 {
123 isWordEnd = false;
124 return std::toupper(c);
125 }
126 }
127 return c;
128 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200129 return sensorName;
130}
131
Ed Tanous1f978632023-02-28 18:16:39 -0800132bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800133 sdbusplus::asio::object_server& objectServer,
134 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
135 boost::container::flat_set<CPUConfig>& cpuConfigs,
136 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700137{
138 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800139 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700140 {
141 if (cpu.state != State::OFF)
142 {
143 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500144 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800145 inventoryIfaces[cpu.name];
146 if (iface != nullptr)
147 {
148 continue;
149 }
150 iface = objectServer.add_interface(
151 cpuInventoryPath + std::string("/") + cpu.name,
152 "xyz.openbmc_project.Inventory.Item");
153 iface->register_property("PrettyName", cpu.name);
154 iface->register_property("Present", true);
155 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700156 }
157 }
158 if (!available)
159 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700160 return false;
James Feist6714a252018-09-10 15:26:18 -0700161 }
162
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800163 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700164 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800165 return false;
James Feist6714a252018-09-10 15:26:18 -0700166 }
167
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700168 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800169 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
170 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700171 {
172 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700173 return true;
James Feist6714a252018-09-10 15:26:18 -0700174 }
175
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700176 boost::container::flat_set<std::string> scannedDirectories;
177 boost::container::flat_set<std::string> createdSensors;
178
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800179 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700180 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700181 auto hwmonDirectory = hwmonNamePath.parent_path();
182
183 auto ret = scannedDirectories.insert(hwmonDirectory.string());
184 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700185 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700186 continue; // already searched this path
187 }
188
189 fs::path::iterator it = hwmonNamePath.begin();
190 std::advance(it, 6); // pick the 6th part for a PECI client device name
191 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700192 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700193 if (findHyphen == std::string::npos)
194 {
195 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700196 continue;
197 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700198 std::string busStr = deviceName.substr(0, findHyphen);
199 std::string addrStr = deviceName.substr(findHyphen + 1);
200
201 size_t bus = 0;
202 size_t addr = 0;
203 try
204 {
205 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700206 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700207 }
Patrick Williams26601e82021-10-06 12:43:25 -0500208 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700209 {
210 continue;
211 }
212
213 std::ifstream nameFile(hwmonNamePath);
214 if (!nameFile.good())
215 {
216 std::cerr << "Failure reading " << hwmonNamePath << "\n";
217 continue;
218 }
219 std::string hwmonName;
220 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700221 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800222 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700223 {
224 // shouldn't have an empty name file
225 continue;
226 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700227 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700228 {
229 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
230 << "\n";
231 }
James Feist6714a252018-09-10 15:26:18 -0700232
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700233 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700234 const SensorData* sensorData = nullptr;
235 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700236 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700237
Zev Weiss8908b3c2022-08-12 18:21:01 -0700238 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700239 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700240 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700241 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700243 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700244 auto sensorBase =
245 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 if (sensorBase != sensorData->end())
247 {
248 baseConfiguration = &(*sensorBase);
249 break;
250 }
251 }
252 if (baseConfiguration == nullptr)
253 {
254 std::cerr << "error finding base configuration for" << hwmonName
255 << "\n";
256 continue;
257 }
258 auto configurationBus = baseConfiguration->second.find("Bus");
259 auto configurationAddress =
260 baseConfiguration->second.find("Address");
261
262 if (configurationBus == baseConfiguration->second.end() ||
263 configurationAddress == baseConfiguration->second.end())
264 {
265 std::cerr << "error finding bus or address in configuration";
266 continue;
267 }
268
James Feist3eb82622019-02-08 13:10:22 -0800269 if (std::get<uint64_t>(configurationBus->second) != bus ||
270 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700271 {
272 continue;
273 }
274
Zev Weiss8908b3c2022-08-12 18:21:01 -0700275 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700276 break;
277 }
278 if (interfacePath == nullptr)
279 {
280 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700281 continue;
282 }
283
284 auto findCpuId = baseConfiguration->second.find("CpuID");
285 if (findCpuId == baseConfiguration->second.end())
286 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700287 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700288 continue;
289 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700290 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800291 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700292
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700293 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700294 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200295 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200296 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700297 {
298 std::cerr << "No temperature sensors in system\n";
299 continue;
300 }
301
302 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800303 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700304 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200305 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200306 if (!fileParts)
307 {
308 continue;
309 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200310 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700311 auto inputPathStr = inputPath.string();
312 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200313 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700314 std::ifstream labelFile(labelPath);
315 if (!labelFile.good())
316 {
317 std::cerr << "Failure reading " << labelPath << "\n";
318 continue;
319 }
320 std::string label;
321 std::getline(labelFile, label);
322 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800323
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200324 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700325
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800326 auto findSensor = gCpuSensors.find(sensorName);
327 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700328 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700329 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330 {
331 std::cout << "Skipped: " << inputPath << ": " << sensorName
332 << " is already created\n";
333 }
334 continue;
335 }
336
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800337 // check hidden properties
338 bool show = true;
339 for (const char* prop : hiddenProps)
340 {
341 if (label == prop)
342 {
343 show = false;
344 break;
345 }
346 }
347
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700348 /*
349 * Find if there is DtsCritOffset is configured in config file
350 * set it if configured or else set it to 0
351 */
352 double dtsOffset = 0;
353 if (label == "DTS")
354 {
355 auto findThrOffset =
356 baseConfiguration->second.find("DtsCritOffset");
357 if (findThrOffset != baseConfiguration->second.end())
358 {
359 dtsOffset = std::visit(VariantToDoubleVisitor(),
360 findThrOffset->second);
361 }
362 }
363
James Feist6714a252018-09-10 15:26:18 -0700364 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700365 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700366 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700367 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800368 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700369 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700370 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700371 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700372 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700373 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700374 std::cerr << "error populating thresholds for "
375 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700376 }
377 }
James Feistc140e202019-11-14 15:23:51 -0800378 auto& sensorPtr = gCpuSensors[sensorName];
379 // make sure destructor fires before creating a new one
380 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700381 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700382 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800383 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700384 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530385 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700386 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700387 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700388 {
James Feist6714a252018-09-10 15:26:18 -0700389 std::cout << "Mapped: " << inputPath << " to " << sensorName
390 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700391 }
James Feist6714a252018-09-10 15:26:18 -0700392 }
393 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700394
Ed Tanous2049bd22022-07-09 07:20:26 -0700395 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700396 {
397 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
398 << " created\n";
399 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700400
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700401 return true;
James Feist6714a252018-09-10 15:26:18 -0700402}
403
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700404void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700405{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406 std::ostringstream hex;
407 hex << std::hex << config.addr;
408 const std::string& addrHexStr = hex.str();
409 std::string busStr = std::to_string(config.bus);
410
411 std::string parameters = "peci-client 0x" + addrHexStr;
412 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
413
James Feistcf3bce62019-01-08 10:07:19 -0800414 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700415 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800416 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700417 {
James Feistcf3bce62019-01-08 10:07:19 -0800418 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700419 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700420 continue;
James Feist6714a252018-09-10 15:26:18 -0700421 }
422
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700423 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700424 if (directoryName.starts_with(busStr) &&
425 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700426 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700427 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700428 {
429 std::cout << parameters << " on bus " << busStr
430 << " is already exported\n";
431 }
432 return;
433 }
James Feist6714a252018-09-10 15:26:18 -0700434 }
435
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700436 std::ofstream deviceFile(device);
437 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700438 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700439 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700440 return;
441 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700442 deviceFile << parameters;
443 deviceFile.close();
444
445 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700446}
447
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700448void detectCpu(boost::asio::steady_timer& pingTimer,
449 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800450 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800451 sdbusplus::asio::object_server& objectServer,
452 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
453 boost::container::flat_set<CPUConfig>& cpuConfigs,
454 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700455{
James Feist6714a252018-09-10 15:26:18 -0700456 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700457 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700458
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800459 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700460 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700461 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800462
463 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700464 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
465 if (file < 0)
466 {
467 std::cerr << "unable to open " << peciDevPath << "\n";
468 std::exit(EXIT_FAILURE);
469 }
470
Ed Tanousa771f6a2022-01-14 09:36:51 -0800471 State newState = State::OFF;
472 struct peci_ping_msg msg
473 {};
James Feist6714a252018-09-10 15:26:18 -0700474 msg.addr = config.addr;
Ed Tanous99c44092022-01-14 09:59:09 -0800475
476 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700477 if (ioctl(file, PECI_IOC_PING, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700478 {
479 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700480 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700481 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800482 struct peci_rd_pkg_cfg_msg msg
483 {};
James Feist6714a252018-09-10 15:26:18 -0700484 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800485 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700486 msg.param = rank;
487 msg.rx_len = 4;
Ed Tanous99c44092022-01-14 09:59:09 -0800488
489 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700490 if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700491 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700492 if ((msg.pkg_config[0] != 0U) ||
493 (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700494 {
495 dimmReady = true;
496 break;
497 }
498 }
499 else
500 {
501 break;
502 }
503 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700504
James Feist6714a252018-09-10 15:26:18 -0700505 if (dimmReady)
506 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700507 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700508 }
509 else
510 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700511 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700512 }
513 }
James Feist6714a252018-09-10 15:26:18 -0700514
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700515 close(file);
516
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700517 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700518 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700519 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700520 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700521 if (config.state == State::OFF)
522 {
523 std::cout << config.name << " is detected\n";
524 exportDevice(config);
525 }
James Feist6714a252018-09-10 15:26:18 -0700526
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700527 if (newState == State::ON)
528 {
529 rescanDelaySeconds = 3;
530 }
531 else if (newState == State::READY)
532 {
533 rescanDelaySeconds = 5;
534 std::cout << "DIMM(s) on " << config.name
535 << " is/are detected\n";
536 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700537 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700538
539 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700540 }
541
542 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700543 {
544 keepPinging = true;
545 }
546
Ed Tanous8a57ec02020-10-09 12:46:52 -0700547 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700548 {
549 std::cout << config.name << ", state: " << config.state << "\n";
550 }
James Feist6714a252018-09-10 15:26:18 -0700551 }
552
Ed Tanous2049bd22022-07-09 07:20:26 -0700553 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700554 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700555 creationTimer.expires_from_now(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700556 std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700557 creationTimer.async_wait([&](const boost::system::error_code& ec) {
558 if (ec == boost::asio::error::operation_aborted)
559 {
560 return; // we're being canceled
561 }
562
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800563 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700564 sensorConfigs) ||
565 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700566 {
567 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800568 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700569 }
570 });
James Feist6714a252018-09-10 15:26:18 -0700571 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700572 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700573 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800574 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800575 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700576 }
577}
578
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700579void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700580 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800581 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700582 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800583 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800584 boost::container::flat_set<CPUConfig>& cpuConfigs,
585 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700586{
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700587 pingTimer.expires_from_now(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700588 pingTimer.async_wait([&](const boost::system::error_code& ec) {
589 if (ec == boost::asio::error::operation_aborted)
590 {
591 return; // we're being canceled
592 }
593
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800594 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800595 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700596 });
597}
598
James Feistc140e202019-11-14 15:23:51 -0800599bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
600 boost::container::flat_set<CPUConfig>& cpuConfigs,
601 ManagedObjectType& sensorConfigs,
602 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700603{
James Feist6714a252018-09-10 15:26:18 -0700604 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800605 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700606 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700607 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700608 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700609 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700610 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700611 return false;
James Feist6714a252018-09-10 15:26:18 -0700612 }
613 useCache = true;
614 }
615
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700616 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700617 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700618 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700619 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700620 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700621 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700622 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700623 {
Zev Weiss054aad82022-08-18 01:37:34 -0700624 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700625 {
626 continue;
627 }
628
Zev Weiss8908b3c2022-08-12 18:21:01 -0700629 auto findName = cfg.find("Name");
630 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700631 {
632 continue;
633 }
James Feist3eb82622019-02-08 13:10:22 -0800634 std::string nameRaw =
635 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700636 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700637 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700638
James Feistc140e202019-11-14 15:23:51 -0800639 auto present = std::optional<bool>();
640 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700641 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700642 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700643 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700644 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700645 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700646 break;
James Feist58295ad2019-05-30 15:01:41 -0700647 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700648 }
649
James Feistc140e202019-11-14 15:23:51 -0800650 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
651 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700652 {
653 auto iface = objectServer.add_interface(
654 cpuInventoryPath + std::string("/") + name,
655 "xyz.openbmc_project.Inventory.Item");
656 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800657 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700658 iface->initialize();
659 inventoryIfaces[name] = std::move(iface);
660 }
James Feist58295ad2019-05-30 15:01:41 -0700661
Zev Weiss8908b3c2022-08-12 18:21:01 -0700662 auto findBus = cfg.find("Bus");
663 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700664 {
665 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
666 continue;
667 }
James Feist3eb82622019-02-08 13:10:22 -0800668 uint64_t bus =
669 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700670
Zev Weiss8908b3c2022-08-12 18:21:01 -0700671 auto findAddress = cfg.find("Address");
672 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700673 {
674 std::cerr << "Can't find 'Address' setting in " << name
675 << "\n";
676 continue;
677 }
James Feist3eb82622019-02-08 13:10:22 -0800678 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
679 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700680
Ed Tanous8a57ec02020-10-09 12:46:52 -0700681 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700682 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700683 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700684 std::cout << "addr: " << addr << "\n";
685 std::cout << "name: " << name << "\n";
686 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700687 }
688
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800689 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700690 }
691 }
692 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700693
Ed Tanous2049bd22022-07-09 07:20:26 -0700694 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700695 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800696 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700697 << " parsed\n";
698 return true;
699 }
700
701 return false;
James Feist6714a252018-09-10 15:26:18 -0700702}
703
James Feistb6c0b912019-07-09 12:21:44 -0700704int main()
James Feist6714a252018-09-10 15:26:18 -0700705{
Ed Tanous1f978632023-02-28 18:16:39 -0800706 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700707 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800708 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700709
Ed Tanous14ed5e92022-07-12 15:50:23 -0700710 sdbusplus::asio::object_server objectServer(systemBus, true);
711 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700712 boost::asio::steady_timer pingTimer(io);
713 boost::asio::steady_timer creationTimer(io);
714 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800715 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700716
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700717 filterTimer.expires_from_now(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700718 filterTimer.async_wait([&](const boost::system::error_code& ec) {
719 if (ec == boost::asio::error::operation_aborted)
720 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700721 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700722 }
723
James Feistc140e202019-11-14 15:23:51 -0800724 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700725 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800726 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800727 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700728 }
729 });
730
Patrick Williams92f8f512022-07-22 19:26:55 -0500731 std::function<void(sdbusplus::message_t&)> eventHandler =
732 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700733 if (message.is_method_error())
734 {
735 std::cerr << "callback method error\n";
736 return;
737 }
738
739 if (debug)
740 {
741 std::cout << message.get_path() << " is changed\n";
742 }
743
744 // this implicitly cancels the timer
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700745 filterTimer.expires_from_now(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700746 filterTimer.async_wait([&](const boost::system::error_code& ec) {
747 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700748 {
Ed Tanousbb679322022-05-16 16:10:00 -0700749 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700750 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700751
Ed Tanousbb679322022-05-16 16:10:00 -0700752 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
753 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700754 {
Ed Tanousbb679322022-05-16 16:10:00 -0700755 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
756 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700757 }
Ed Tanousbb679322022-05-16 16:10:00 -0700758 });
759 };
James Feist6714a252018-09-10 15:26:18 -0700760
Zev Weiss214d9712022-08-12 12:54:31 -0700761 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
762 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700763
Thu Nguyen255da6b2022-07-29 10:05:52 +0700764 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800765
766 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700767 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700768 return 0;
James Feist6714a252018-09-10 15:26:18 -0700769}