blob: 287be9f3ddf4f9d4575c09ad8a4ea1a2e2fb101e [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
17#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070018
Thu Nguyen255da6b2022-07-29 10:05:52 +070019#include <IntelCPUSensor.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070020#include <Utils.hpp>
21#include <VariantVisitors.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/predicate.hpp>
23#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>
26#include <boost/date_time/posix_time/posix_time.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>
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
Ed Tanous74cffa82022-01-25 13:00:28 -080046extern "C" {
James Feistf87dc4c2018-12-05 14:39:51 -080047#include <linux/peci-ioctl.h>
Ed Tanous74cffa82022-01-25 13:00:28 -080048}
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080049#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
50#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
51#endif
James Feistf87dc4c2018-12-05 14:39:51 -080052// clang-format on
53
Ed Tanous8a57ec02020-10-09 12:46:52 -070054static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070055
Thu Nguyen255da6b2022-07-29 10:05:52 +070056boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
57 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080058boost::container::flat_map<std::string,
59 std::shared_ptr<sdbusplus::asio::dbus_interface>>
60 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080061
James Feist6714a252018-09-10 15:26:18 -070062enum State
63{
64 OFF, // host powered down
65 ON, // host powered on
66 READY // host powered on and mem test passed - fully ready
67};
68
69struct CPUConfig
70{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070071 CPUConfig(const uint64_t& bus, const uint64_t& addr,
72 const std::string& name, const State& state) :
73 bus(bus),
74 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070075 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070076 int bus;
James Feist6714a252018-09-10 15:26:18 -070077 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070078 std::string name;
James Feist6714a252018-09-10 15:26:18 -070079 State state;
80
81 bool operator<(const CPUConfig& rhs) const
82 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093083 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070084 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070085 }
86};
87
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070088static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070090
James Feistcf3bce62019-01-08 10:07:19 -080091namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080092
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070094 "xyz.openbmc_project.Configuration.";
Brandon Kim66558232021-11-09 16:53:08 -080095static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
96static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070097 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070098
Jae Hyun Yood64262b2018-11-01 13:31:16 -070099void detectCpuAsync(
100 boost::asio::deadline_timer& pingTimer,
101 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
102 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800103 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800104 boost::container::flat_set<CPUConfig>& cpuConfigs,
105 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700106
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200107std::string createSensorName(const std::string& label, const std::string& item,
108 const int& cpuId)
109{
110 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700111 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200112 {
113 sensorName += " " + item;
114 }
115 sensorName += " CPU" + std::to_string(cpuId);
116 // converting to Upper Camel case whole name
117 bool isWordEnd = true;
118 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
119 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700120 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700121 {
122 isWordEnd = true;
123 }
124 else
125 {
126 if (isWordEnd)
127 {
128 isWordEnd = false;
129 return std::toupper(c);
130 }
131 }
132 return c;
133 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200134 return sensorName;
135}
136
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800137bool createSensors(boost::asio::io_service& io,
138 sdbusplus::asio::object_server& objectServer,
139 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
140 boost::container::flat_set<CPUConfig>& cpuConfigs,
141 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700142{
143 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800144 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700145 {
146 if (cpu.state != State::OFF)
147 {
148 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500149 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800150 inventoryIfaces[cpu.name];
151 if (iface != nullptr)
152 {
153 continue;
154 }
155 iface = objectServer.add_interface(
156 cpuInventoryPath + std::string("/") + cpu.name,
157 "xyz.openbmc_project.Inventory.Item");
158 iface->register_property("PrettyName", cpu.name);
159 iface->register_property("Present", true);
160 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700161 }
162 }
163 if (!available)
164 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700165 return false;
James Feist6714a252018-09-10 15:26:18 -0700166 }
167
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800168 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700169 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800170 return false;
James Feist6714a252018-09-10 15:26:18 -0700171 }
172
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700173 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800174 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
175 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700176 {
177 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700178 return true;
James Feist6714a252018-09-10 15:26:18 -0700179 }
180
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700181 boost::container::flat_set<std::string> scannedDirectories;
182 boost::container::flat_set<std::string> createdSensors;
183
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800184 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700185 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700186 auto hwmonDirectory = hwmonNamePath.parent_path();
187
188 auto ret = scannedDirectories.insert(hwmonDirectory.string());
189 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700190 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700191 continue; // already searched this path
192 }
193
194 fs::path::iterator it = hwmonNamePath.begin();
195 std::advance(it, 6); // pick the 6th part for a PECI client device name
196 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700197 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700198 if (findHyphen == std::string::npos)
199 {
200 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700201 continue;
202 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 std::string busStr = deviceName.substr(0, findHyphen);
204 std::string addrStr = deviceName.substr(findHyphen + 1);
205
206 size_t bus = 0;
207 size_t addr = 0;
208 try
209 {
210 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700211 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700212 }
Patrick Williams26601e82021-10-06 12:43:25 -0500213 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700214 {
215 continue;
216 }
217
218 std::ifstream nameFile(hwmonNamePath);
219 if (!nameFile.good())
220 {
221 std::cerr << "Failure reading " << hwmonNamePath << "\n";
222 continue;
223 }
224 std::string hwmonName;
225 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700226 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800227 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700228 {
229 // shouldn't have an empty name file
230 continue;
231 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700232 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700233 {
234 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
235 << "\n";
236 }
James Feist6714a252018-09-10 15:26:18 -0700237
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700238 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700239 const SensorData* sensorData = nullptr;
240 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700241 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700242
Zev Weiss8908b3c2022-08-12 18:21:01 -0700243 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700244 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700245 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700246 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700247 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700248 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700249 auto sensorBase = sensorData->find(sensorType);
250 if (sensorBase != sensorData->end())
251 {
252 baseConfiguration = &(*sensorBase);
253 break;
254 }
255 }
256 if (baseConfiguration == nullptr)
257 {
258 std::cerr << "error finding base configuration for" << hwmonName
259 << "\n";
260 continue;
261 }
262 auto configurationBus = baseConfiguration->second.find("Bus");
263 auto configurationAddress =
264 baseConfiguration->second.find("Address");
265
266 if (configurationBus == baseConfiguration->second.end() ||
267 configurationAddress == baseConfiguration->second.end())
268 {
269 std::cerr << "error finding bus or address in configuration";
270 continue;
271 }
272
James Feist3eb82622019-02-08 13:10:22 -0800273 if (std::get<uint64_t>(configurationBus->second) != bus ||
274 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700275 {
276 continue;
277 }
278
Zev Weiss8908b3c2022-08-12 18:21:01 -0700279 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700280 break;
281 }
282 if (interfacePath == nullptr)
283 {
284 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700285 continue;
286 }
287
288 auto findCpuId = baseConfiguration->second.find("CpuID");
289 if (findCpuId == baseConfiguration->second.end())
290 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700291 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700292 continue;
293 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700294 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800295 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700296
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700297 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700298 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200299 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200300 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700301 {
302 std::cerr << "No temperature sensors in system\n";
303 continue;
304 }
305
306 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800307 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700308 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200309 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200310 if (!fileParts)
311 {
312 continue;
313 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200314 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700315 auto inputPathStr = inputPath.string();
316 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200317 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700318 std::ifstream labelFile(labelPath);
319 if (!labelFile.good())
320 {
321 std::cerr << "Failure reading " << labelPath << "\n";
322 continue;
323 }
324 std::string label;
325 std::getline(labelFile, label);
326 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800327
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200328 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700329
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800330 auto findSensor = gCpuSensors.find(sensorName);
331 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700332 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700333 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700334 {
335 std::cout << "Skipped: " << inputPath << ": " << sensorName
336 << " is already created\n";
337 }
338 continue;
339 }
340
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800341 // check hidden properties
342 bool show = true;
343 for (const char* prop : hiddenProps)
344 {
345 if (label == prop)
346 {
347 show = false;
348 break;
349 }
350 }
351
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700352 /*
353 * Find if there is DtsCritOffset is configured in config file
354 * set it if configured or else set it to 0
355 */
356 double dtsOffset = 0;
357 if (label == "DTS")
358 {
359 auto findThrOffset =
360 baseConfiguration->second.find("DtsCritOffset");
361 if (findThrOffset != baseConfiguration->second.end())
362 {
363 dtsOffset = std::visit(VariantToDoubleVisitor(),
364 findThrOffset->second);
365 }
366 }
367
James Feist6714a252018-09-10 15:26:18 -0700368 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700369 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700370 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700371 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800372 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700373 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700374 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700375 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700376 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700377 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700378 std::cerr << "error populating thresholds for "
379 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700380 }
381 }
James Feistc140e202019-11-14 15:23:51 -0800382 auto& sensorPtr = gCpuSensors[sensorName];
383 // make sure destructor fires before creating a new one
384 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700385 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700386 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800387 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700388 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530389 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700391 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700392 {
James Feist6714a252018-09-10 15:26:18 -0700393 std::cout << "Mapped: " << inputPath << " to " << sensorName
394 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700395 }
James Feist6714a252018-09-10 15:26:18 -0700396 }
397 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700398
Ed Tanous2049bd22022-07-09 07:20:26 -0700399 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700400 {
401 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
402 << " created\n";
403 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700404
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700405 return true;
James Feist6714a252018-09-10 15:26:18 -0700406}
407
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700408void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700409{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700410 std::ostringstream hex;
411 hex << std::hex << config.addr;
412 const std::string& addrHexStr = hex.str();
413 std::string busStr = std::to_string(config.bus);
414
415 std::string parameters = "peci-client 0x" + addrHexStr;
416 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
417
James Feistcf3bce62019-01-08 10:07:19 -0800418 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700419 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800420 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700421 {
James Feistcf3bce62019-01-08 10:07:19 -0800422 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700423 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700424 continue;
James Feist6714a252018-09-10 15:26:18 -0700425 }
426
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700427 const std::string& directoryName = path.path().filename();
428 if (boost::starts_with(directoryName, busStr) &&
429 boost::ends_with(directoryName, addrHexStr))
430 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700431 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700432 {
433 std::cout << parameters << " on bus " << busStr
434 << " is already exported\n";
435 }
436 return;
437 }
James Feist6714a252018-09-10 15:26:18 -0700438 }
439
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700440 std::ofstream deviceFile(device);
441 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700442 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700443 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700444 return;
445 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700446 deviceFile << parameters;
447 deviceFile.close();
448
449 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700450}
451
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800452void detectCpu(boost::asio::deadline_timer& pingTimer,
453 boost::asio::deadline_timer& creationTimer,
454 boost::asio::io_service& io,
455 sdbusplus::asio::object_server& objectServer,
456 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
457 boost::container::flat_set<CPUConfig>& cpuConfigs,
458 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700459{
James Feist6714a252018-09-10 15:26:18 -0700460 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700461 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700462
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800463 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700464 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700465 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800466
467 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700468 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
469 if (file < 0)
470 {
471 std::cerr << "unable to open " << peciDevPath << "\n";
472 std::exit(EXIT_FAILURE);
473 }
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 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700559 creationTimer.expires_from_now(
560 boost::posix_time::seconds(rescanDelaySeconds));
561 creationTimer.async_wait([&](const boost::system::error_code& ec) {
562 if (ec == boost::asio::error::operation_aborted)
563 {
564 return; // we're being canceled
565 }
566
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800567 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700568 sensorConfigs) ||
569 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700570 {
571 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800572 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700573 }
574 });
James Feist6714a252018-09-10 15:26:18 -0700575 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700576 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700577 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800578 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800579 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700580 }
581}
582
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700583void detectCpuAsync(
584 boost::asio::deadline_timer& pingTimer,
585 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
586 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800587 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800588 boost::container::flat_set<CPUConfig>& cpuConfigs,
589 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700590{
591 pingTimer.expires_from_now(boost::posix_time::seconds(1));
592 pingTimer.async_wait([&](const boost::system::error_code& ec) {
593 if (ec == boost::asio::error::operation_aborted)
594 {
595 return; // we're being canceled
596 }
597
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800598 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800599 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700600 });
601}
602
James Feistc140e202019-11-14 15:23:51 -0800603bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
604 boost::container::flat_set<CPUConfig>& cpuConfigs,
605 ManagedObjectType& sensorConfigs,
606 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700607{
James Feist6714a252018-09-10 15:26:18 -0700608 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800609 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700610 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700611 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700612 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700613 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800614 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700615 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700616 return false;
James Feist6714a252018-09-10 15:26:18 -0700617 }
618 useCache = true;
619 }
620
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700621 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700622 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700623 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700624 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700625 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700626 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700627 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700628 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700629 if ((configPrefix + std::string(type)) != intf)
James Feist6714a252018-09-10 15:26:18 -0700630 {
631 continue;
632 }
633
Zev Weiss8908b3c2022-08-12 18:21:01 -0700634 auto findName = cfg.find("Name");
635 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700636 {
637 continue;
638 }
James Feist3eb82622019-02-08 13:10:22 -0800639 std::string nameRaw =
640 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700641 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700642 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700643
James Feistc140e202019-11-14 15:23:51 -0800644 auto present = std::optional<bool>();
645 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700646 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700647 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700648 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700649 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700650 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700651 break;
James Feist58295ad2019-05-30 15:01:41 -0700652 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700653 }
654
James Feistc140e202019-11-14 15:23:51 -0800655 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
656 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700657 {
658 auto iface = objectServer.add_interface(
659 cpuInventoryPath + std::string("/") + name,
660 "xyz.openbmc_project.Inventory.Item");
661 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800662 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700663 iface->initialize();
664 inventoryIfaces[name] = std::move(iface);
665 }
James Feist58295ad2019-05-30 15:01:41 -0700666
Zev Weiss8908b3c2022-08-12 18:21:01 -0700667 auto findBus = cfg.find("Bus");
668 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700669 {
670 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
671 continue;
672 }
James Feist3eb82622019-02-08 13:10:22 -0800673 uint64_t bus =
674 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700675
Zev Weiss8908b3c2022-08-12 18:21:01 -0700676 auto findAddress = cfg.find("Address");
677 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700678 {
679 std::cerr << "Can't find 'Address' setting in " << name
680 << "\n";
681 continue;
682 }
James Feist3eb82622019-02-08 13:10:22 -0800683 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
684 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700685
Ed Tanous8a57ec02020-10-09 12:46:52 -0700686 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700687 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700688 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700689 std::cout << "addr: " << addr << "\n";
690 std::cout << "name: " << name << "\n";
691 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700692 }
693
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800694 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700695 }
696 }
697 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700698
Ed Tanous2049bd22022-07-09 07:20:26 -0700699 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700700 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800701 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700702 << " parsed\n";
703 return true;
704 }
705
706 return false;
James Feist6714a252018-09-10 15:26:18 -0700707}
708
James Feistb6c0b912019-07-09 12:21:44 -0700709int main()
James Feist6714a252018-09-10 15:26:18 -0700710{
711 boost::asio::io_service io;
712 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800713 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700714
James Feist6714a252018-09-10 15:26:18 -0700715 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700716 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700717 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700718 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800719 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700720
721 filterTimer.expires_from_now(boost::posix_time::seconds(1));
722 filterTimer.async_wait([&](const boost::system::error_code& ec) {
723 if (ec == boost::asio::error::operation_aborted)
724 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700725 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700726 }
727
James Feistc140e202019-11-14 15:23:51 -0800728 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700729 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800730 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800731 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700732 }
733 });
734
Patrick Williams92f8f512022-07-22 19:26:55 -0500735 std::function<void(sdbusplus::message_t&)> eventHandler =
736 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700737 if (message.is_method_error())
738 {
739 std::cerr << "callback method error\n";
740 return;
741 }
742
743 if (debug)
744 {
745 std::cout << message.get_path() << " is changed\n";
746 }
747
748 // this implicitly cancels the timer
749 filterTimer.expires_from_now(boost::posix_time::seconds(1));
750 filterTimer.async_wait([&](const boost::system::error_code& ec) {
751 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700752 {
Ed Tanousbb679322022-05-16 16:10:00 -0700753 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700754 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700755
Ed Tanousbb679322022-05-16 16:10:00 -0700756 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
757 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700758 {
Ed Tanousbb679322022-05-16 16:10:00 -0700759 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
760 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700761 }
Ed Tanousbb679322022-05-16 16:10:00 -0700762 });
763 };
James Feist6714a252018-09-10 15:26:18 -0700764
Zev Weiss214d9712022-08-12 12:54:31 -0700765 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
766 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700767
Thu Nguyen255da6b2022-07-29 10:05:52 +0700768 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800769
770 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700771 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700772 return 0;
James Feist6714a252018-09-10 15:26:18 -0700773}