blob: bf6e08bfc1fcb8efe7de2f8098c3041963aa2b11 [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>
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>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29#include <sdbusplus/bus/match.hpp>
30
31#include <array>
Jakub Nowackia38e5612023-03-08 12:27:01 +010032#include <cerrno>
James Feist24f02f22019-04-15 11:05:39 -070033#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070034#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <functional>
36#include <memory>
James Feist6714a252018-09-10 15:26:18 -070037#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <sstream>
39#include <stdexcept>
40#include <string>
41#include <utility>
42#include <variant>
43#include <vector>
James Feist6714a252018-09-10 15:26:18 -070044
James Feistf87dc4c2018-12-05 14:39:51 -080045// clang-format off
46// this needs to be included last or we'll have build issues
47#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080048#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
49#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
50#endif
James Feistf87dc4c2018-12-05 14:39:51 -080051// clang-format on
52
Ed Tanous8a57ec02020-10-09 12:46:52 -070053static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070054
Thu Nguyen255da6b2022-07-29 10:05:52 +070055boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
56 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080057boost::container::flat_map<std::string,
58 std::shared_ptr<sdbusplus::asio::dbus_interface>>
59 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080060
James Feist6714a252018-09-10 15:26:18 -070061enum State
62{
63 OFF, // host powered down
64 ON, // host powered on
65 READY // host powered on and mem test passed - fully ready
66};
67
68struct CPUConfig
69{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070070 CPUConfig(const uint64_t& bus, const uint64_t& addr,
71 const std::string& name, const State& state) :
72 bus(bus),
73 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070074 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 int bus;
James Feist6714a252018-09-10 15:26:18 -070076 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070077 std::string name;
James Feist6714a252018-09-10 15:26:18 -070078 State state;
79
80 bool operator<(const CPUConfig& rhs) const
81 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093082 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070083 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070084 }
85};
86
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070087static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070089
James Feistcf3bce62019-01-08 10:07:19 -080090namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080091
Zev Weiss054aad82022-08-18 01:37:34 -070092static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080093static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070094 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070095
Jae Hyun Yood64262b2018-11-01 13:31:16 -070096void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -070097 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -080098 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070099 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800100 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 boost::container::flat_set<CPUConfig>& cpuConfigs,
102 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700103
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200104std::string createSensorName(const std::string& label, const std::string& item,
105 const int& cpuId)
106{
107 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700108 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200109 {
110 sensorName += " " + item;
111 }
112 sensorName += " CPU" + std::to_string(cpuId);
113 // converting to Upper Camel case whole name
114 bool isWordEnd = true;
115 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
116 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700117 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700118 {
119 isWordEnd = true;
120 }
121 else
122 {
123 if (isWordEnd)
124 {
125 isWordEnd = false;
126 return std::toupper(c);
127 }
128 }
129 return c;
130 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200131 return sensorName;
132}
133
Ed Tanous1f978632023-02-28 18:16:39 -0800134bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800135 sdbusplus::asio::object_server& objectServer,
136 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
137 boost::container::flat_set<CPUConfig>& cpuConfigs,
138 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700139{
140 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800141 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700142 {
143 if (cpu.state != State::OFF)
144 {
145 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500146 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800147 inventoryIfaces[cpu.name];
148 if (iface != nullptr)
149 {
150 continue;
151 }
152 iface = objectServer.add_interface(
153 cpuInventoryPath + std::string("/") + cpu.name,
154 "xyz.openbmc_project.Inventory.Item");
155 iface->register_property("PrettyName", cpu.name);
156 iface->register_property("Present", true);
157 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700158 }
159 }
160 if (!available)
161 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700162 return false;
James Feist6714a252018-09-10 15:26:18 -0700163 }
164
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800165 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700166 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800167 return false;
James Feist6714a252018-09-10 15:26:18 -0700168 }
169
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700170 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800171 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
172 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700173 {
174 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700175 return true;
James Feist6714a252018-09-10 15:26:18 -0700176 }
177
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700178 boost::container::flat_set<std::string> scannedDirectories;
179 boost::container::flat_set<std::string> createdSensors;
180
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800181 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700182 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700183 auto hwmonDirectory = hwmonNamePath.parent_path();
184
185 auto ret = scannedDirectories.insert(hwmonDirectory.string());
186 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700187 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700188 continue; // already searched this path
189 }
190
191 fs::path::iterator it = hwmonNamePath.begin();
192 std::advance(it, 6); // pick the 6th part for a PECI client device name
193 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700194 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 if (findHyphen == std::string::npos)
196 {
197 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700198 continue;
199 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700200 std::string busStr = deviceName.substr(0, findHyphen);
201 std::string addrStr = deviceName.substr(findHyphen + 1);
202
203 size_t bus = 0;
204 size_t addr = 0;
205 try
206 {
207 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700208 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700209 }
Patrick Williams26601e82021-10-06 12:43:25 -0500210 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700211 {
212 continue;
213 }
214
215 std::ifstream nameFile(hwmonNamePath);
216 if (!nameFile.good())
217 {
218 std::cerr << "Failure reading " << hwmonNamePath << "\n";
219 continue;
220 }
221 std::string hwmonName;
222 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700223 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800224 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700225 {
226 // shouldn't have an empty name file
227 continue;
228 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700229 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700230 {
231 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
232 << "\n";
233 }
James Feist6714a252018-09-10 15:26:18 -0700234
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700235 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700236 const SensorData* sensorData = nullptr;
237 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700238 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700239
Zev Weiss8908b3c2022-08-12 18:21:01 -0700240 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700241 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700242 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700243 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700244 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700245 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700246 auto sensorBase =
247 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 if (sensorBase != sensorData->end())
249 {
250 baseConfiguration = &(*sensorBase);
251 break;
252 }
253 }
254 if (baseConfiguration == nullptr)
255 {
256 std::cerr << "error finding base configuration for" << hwmonName
257 << "\n";
258 continue;
259 }
260 auto configurationBus = baseConfiguration->second.find("Bus");
261 auto configurationAddress =
262 baseConfiguration->second.find("Address");
263
264 if (configurationBus == baseConfiguration->second.end() ||
265 configurationAddress == baseConfiguration->second.end())
266 {
267 std::cerr << "error finding bus or address in configuration";
268 continue;
269 }
270
James Feist3eb82622019-02-08 13:10:22 -0800271 if (std::get<uint64_t>(configurationBus->second) != bus ||
272 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700273 {
274 continue;
275 }
276
Zev Weiss8908b3c2022-08-12 18:21:01 -0700277 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700278 break;
279 }
280 if (interfacePath == nullptr)
281 {
282 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700283 continue;
284 }
285
286 auto findCpuId = baseConfiguration->second.find("CpuID");
287 if (findCpuId == baseConfiguration->second.end())
288 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700289 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700290 continue;
291 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800293 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700294
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700296 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200297 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200298 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700299 {
300 std::cerr << "No temperature sensors in system\n";
301 continue;
302 }
303
304 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800305 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700306 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200307 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200308 if (!fileParts)
309 {
310 continue;
311 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200312 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700313 auto inputPathStr = inputPath.string();
314 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200315 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700316 std::ifstream labelFile(labelPath);
317 if (!labelFile.good())
318 {
319 std::cerr << "Failure reading " << labelPath << "\n";
320 continue;
321 }
322 std::string label;
323 std::getline(labelFile, label);
324 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800325
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200326 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700327
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800328 auto findSensor = gCpuSensors.find(sensorName);
329 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700331 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700332 {
333 std::cout << "Skipped: " << inputPath << ": " << sensorName
334 << " is already created\n";
335 }
336 continue;
337 }
338
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800339 // check hidden properties
340 bool show = true;
341 for (const char* prop : hiddenProps)
342 {
343 if (label == prop)
344 {
345 show = false;
346 break;
347 }
348 }
349
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700350 /*
351 * Find if there is DtsCritOffset is configured in config file
352 * set it if configured or else set it to 0
353 */
354 double dtsOffset = 0;
355 if (label == "DTS")
356 {
357 auto findThrOffset =
358 baseConfiguration->second.find("DtsCritOffset");
359 if (findThrOffset != baseConfiguration->second.end())
360 {
361 dtsOffset = std::visit(VariantToDoubleVisitor(),
362 findThrOffset->second);
363 }
364 }
365
James Feist6714a252018-09-10 15:26:18 -0700366 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700367 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700368 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700369 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800370 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700371 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700372 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700373 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700374 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700375 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700376 std::cerr << "error populating thresholds for "
377 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700378 }
379 }
James Feistc140e202019-11-14 15:23:51 -0800380 auto& sensorPtr = gCpuSensors[sensorName];
381 // make sure destructor fires before creating a new one
382 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700383 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700384 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800385 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700386 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530387 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700388 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700389 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 {
James Feist6714a252018-09-10 15:26:18 -0700391 std::cout << "Mapped: " << inputPath << " to " << sensorName
392 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700393 }
James Feist6714a252018-09-10 15:26:18 -0700394 }
395 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700396
Ed Tanous2049bd22022-07-09 07:20:26 -0700397 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700398 {
399 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
400 << " created\n";
401 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700402
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700403 return true;
James Feist6714a252018-09-10 15:26:18 -0700404}
405
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700407{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700408 std::ostringstream hex;
409 hex << std::hex << config.addr;
410 const std::string& addrHexStr = hex.str();
411 std::string busStr = std::to_string(config.bus);
412
413 std::string parameters = "peci-client 0x" + addrHexStr;
414 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
415
James Feistcf3bce62019-01-08 10:07:19 -0800416 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700417 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800418 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700419 {
James Feistcf3bce62019-01-08 10:07:19 -0800420 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700421 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700422 continue;
James Feist6714a252018-09-10 15:26:18 -0700423 }
424
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700425 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700426 if (directoryName.starts_with(busStr) &&
427 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700428 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700429 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700430 {
431 std::cout << parameters << " on bus " << busStr
432 << " is already exported\n";
433 }
434 return;
435 }
James Feist6714a252018-09-10 15:26:18 -0700436 }
437
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700438 std::ofstream deviceFile(device);
439 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700440 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700441 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700442 return;
443 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700444 deviceFile << parameters;
445 deviceFile.close();
446
447 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700448}
449
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700450void detectCpu(boost::asio::steady_timer& pingTimer,
451 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800452 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800453 sdbusplus::asio::object_server& objectServer,
454 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
455 boost::container::flat_set<CPUConfig>& cpuConfigs,
456 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700457{
James Feist6714a252018-09-10 15:26:18 -0700458 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700459 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700460
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800461 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700462 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700463 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800464
465 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700466 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
467 if (file < 0)
468 {
Jakub Nowackia38e5612023-03-08 12:27:01 +0100469 std::cerr << "unable to open " << peciDevPath << " "
470 << std::strerror(errno) << "\n";
471 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
472 dbusConnection, cpuConfigs, sensorConfigs);
473 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700474 }
475
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100476 peci_SetDevName(peciDevPath.data());
Ed Tanousa771f6a2022-01-14 09:36:51 -0800477 State newState = State::OFF;
Ed Tanous99c44092022-01-14 09:59:09 -0800478
479 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100480 if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700481 {
482 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700483 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700484 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100485 std::array<uint8_t, 8> pkgConfig{};
486 uint8_t cc = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800487
488 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100489 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_DDR_DIMM_TEMP,
490 rank, 4, &pkgConfig[0],
491 &cc) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700492 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100493 if ((pkgConfig[0] != 0U) || (pkgConfig[1] != 0U) ||
494 (pkgConfig[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700495 {
496 dimmReady = true;
497 break;
498 }
499 }
500 else
501 {
502 break;
503 }
504 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700505
James Feist6714a252018-09-10 15:26:18 -0700506 if (dimmReady)
507 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700508 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700509 }
510 else
511 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700512 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700513 }
514 }
James Feist6714a252018-09-10 15:26:18 -0700515
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700516 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700517 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700518 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700519 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700520 if (config.state == State::OFF)
521 {
522 std::cout << config.name << " is detected\n";
523 exportDevice(config);
524 }
James Feist6714a252018-09-10 15:26:18 -0700525
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700526 if (newState == State::ON)
527 {
528 rescanDelaySeconds = 3;
529 }
530 else if (newState == State::READY)
531 {
532 rescanDelaySeconds = 5;
533 std::cout << "DIMM(s) on " << config.name
534 << " is/are detected\n";
535 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700536 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700537
538 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700539 }
540
541 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700542 {
543 keepPinging = true;
544 }
545
Ed Tanous8a57ec02020-10-09 12:46:52 -0700546 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700547 {
548 std::cout << config.name << ", state: " << config.state << "\n";
549 }
James Feist6714a252018-09-10 15:26:18 -0700550 }
551
Ed Tanous2049bd22022-07-09 07:20:26 -0700552 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700553 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800554 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700555 creationTimer.async_wait([&](const boost::system::error_code& ec) {
556 if (ec == boost::asio::error::operation_aborted)
557 {
558 return; // we're being canceled
559 }
560
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800561 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700562 sensorConfigs) ||
563 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700564 {
565 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800566 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700567 }
568 });
James Feist6714a252018-09-10 15:26:18 -0700569 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700570 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700571 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800572 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800573 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700574 }
575}
576
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700577void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700578 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800579 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700580 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800581 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800582 boost::container::flat_set<CPUConfig>& cpuConfigs,
583 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700584{
Ed Tanous83db50c2023-03-01 10:20:24 -0800585 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700586 pingTimer.async_wait([&](const boost::system::error_code& ec) {
587 if (ec == boost::asio::error::operation_aborted)
588 {
589 return; // we're being canceled
590 }
591
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800592 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800593 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700594 });
595}
596
James Feistc140e202019-11-14 15:23:51 -0800597bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
598 boost::container::flat_set<CPUConfig>& cpuConfigs,
599 ManagedObjectType& sensorConfigs,
600 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700601{
James Feist6714a252018-09-10 15:26:18 -0700602 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800603 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700604 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700605 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700606 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700607 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700608 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700609 return false;
James Feist6714a252018-09-10 15:26:18 -0700610 }
611 useCache = true;
612 }
613
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700614 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700615 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700616 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700617 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700618 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700619 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700620 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700621 {
Zev Weiss054aad82022-08-18 01:37:34 -0700622 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700623 {
624 continue;
625 }
626
Zev Weiss8908b3c2022-08-12 18:21:01 -0700627 auto findName = cfg.find("Name");
628 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700629 {
630 continue;
631 }
James Feist3eb82622019-02-08 13:10:22 -0800632 std::string nameRaw =
633 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700634 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700635 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700636
James Feistc140e202019-11-14 15:23:51 -0800637 auto present = std::optional<bool>();
638 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700639 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700640 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700641 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700642 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700643 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700644 break;
James Feist58295ad2019-05-30 15:01:41 -0700645 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700646 }
647
James Feistc140e202019-11-14 15:23:51 -0800648 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
649 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700650 {
651 auto iface = objectServer.add_interface(
652 cpuInventoryPath + std::string("/") + name,
653 "xyz.openbmc_project.Inventory.Item");
654 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800655 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700656 iface->initialize();
657 inventoryIfaces[name] = std::move(iface);
658 }
James Feist58295ad2019-05-30 15:01:41 -0700659
Zev Weiss8908b3c2022-08-12 18:21:01 -0700660 auto findBus = cfg.find("Bus");
661 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700662 {
663 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
664 continue;
665 }
James Feist3eb82622019-02-08 13:10:22 -0800666 uint64_t bus =
667 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700668
Zev Weiss8908b3c2022-08-12 18:21:01 -0700669 auto findAddress = cfg.find("Address");
670 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700671 {
672 std::cerr << "Can't find 'Address' setting in " << name
673 << "\n";
674 continue;
675 }
James Feist3eb82622019-02-08 13:10:22 -0800676 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
677 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700678
Ed Tanous8a57ec02020-10-09 12:46:52 -0700679 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700680 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700681 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700682 std::cout << "addr: " << addr << "\n";
683 std::cout << "name: " << name << "\n";
684 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700685 }
686
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800687 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700688 }
689 }
690 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700691
Ed Tanous2049bd22022-07-09 07:20:26 -0700692 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700693 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800694 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700695 << " parsed\n";
696 return true;
697 }
698
699 return false;
James Feist6714a252018-09-10 15:26:18 -0700700}
701
James Feistb6c0b912019-07-09 12:21:44 -0700702int main()
James Feist6714a252018-09-10 15:26:18 -0700703{
Ed Tanous1f978632023-02-28 18:16:39 -0800704 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700705 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800706 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700707
Ed Tanous14ed5e92022-07-12 15:50:23 -0700708 sdbusplus::asio::object_server objectServer(systemBus, true);
709 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700710 boost::asio::steady_timer pingTimer(io);
711 boost::asio::steady_timer creationTimer(io);
712 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800713 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700714
Ed Tanous83db50c2023-03-01 10:20:24 -0800715 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700716 filterTimer.async_wait([&](const boost::system::error_code& ec) {
717 if (ec == boost::asio::error::operation_aborted)
718 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700719 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700720 }
721
James Feistc140e202019-11-14 15:23:51 -0800722 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700723 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800724 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800725 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700726 }
727 });
728
Patrick Williams92f8f512022-07-22 19:26:55 -0500729 std::function<void(sdbusplus::message_t&)> eventHandler =
730 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700731 if (message.is_method_error())
732 {
733 std::cerr << "callback method error\n";
734 return;
735 }
736
737 if (debug)
738 {
739 std::cout << message.get_path() << " is changed\n";
740 }
741
742 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800743 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700744 filterTimer.async_wait([&](const boost::system::error_code& ec) {
745 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700746 {
Ed Tanousbb679322022-05-16 16:10:00 -0700747 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700748 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700749
Ed Tanousbb679322022-05-16 16:10:00 -0700750 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
751 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700752 {
Ed Tanousbb679322022-05-16 16:10:00 -0700753 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
754 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700755 }
Ed Tanousbb679322022-05-16 16:10:00 -0700756 });
757 };
James Feist6714a252018-09-10 15:26:18 -0700758
Zev Weiss214d9712022-08-12 12:54:31 -0700759 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
760 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700761
Thu Nguyen255da6b2022-07-29 10:05:52 +0700762 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800763
764 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700765 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700766 return 0;
James Feist6714a252018-09-10 15:26:18 -0700767}