blob: ea144debe52c0fef06e4dd1825bd3c581ba3a36f [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
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800244 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700245 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700249 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700250 auto sensorBase = sensorData->find(sensorType);
251 if (sensorBase != sensorData->end())
252 {
253 baseConfiguration = &(*sensorBase);
254 break;
255 }
256 }
257 if (baseConfiguration == nullptr)
258 {
259 std::cerr << "error finding base configuration for" << hwmonName
260 << "\n";
261 continue;
262 }
263 auto configurationBus = baseConfiguration->second.find("Bus");
264 auto configurationAddress =
265 baseConfiguration->second.find("Address");
266
267 if (configurationBus == baseConfiguration->second.end() ||
268 configurationAddress == baseConfiguration->second.end())
269 {
270 std::cerr << "error finding bus or address in configuration";
271 continue;
272 }
273
James Feist3eb82622019-02-08 13:10:22 -0800274 if (std::get<uint64_t>(configurationBus->second) != bus ||
275 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700276 {
277 continue;
278 }
279
280 interfacePath = &(sensor.first.str);
281 break;
282 }
283 if (interfacePath == nullptr)
284 {
285 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700286 continue;
287 }
288
289 auto findCpuId = baseConfiguration->second.find("CpuID");
290 if (findCpuId == baseConfiguration->second.end())
291 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700293 continue;
294 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800296 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700297
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700298 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700299 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200300 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200301 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700302 {
303 std::cerr << "No temperature sensors in system\n";
304 continue;
305 }
306
307 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800308 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700309 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200310 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200311 if (!fileParts)
312 {
313 continue;
314 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200315 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700316 auto inputPathStr = inputPath.string();
317 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200318 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700319 std::ifstream labelFile(labelPath);
320 if (!labelFile.good())
321 {
322 std::cerr << "Failure reading " << labelPath << "\n";
323 continue;
324 }
325 std::string label;
326 std::getline(labelFile, label);
327 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800328
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200329 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800331 auto findSensor = gCpuSensors.find(sensorName);
332 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700334 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700335 {
336 std::cout << "Skipped: " << inputPath << ": " << sensorName
337 << " is already created\n";
338 }
339 continue;
340 }
341
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800342 // check hidden properties
343 bool show = true;
344 for (const char* prop : hiddenProps)
345 {
346 if (label == prop)
347 {
348 show = false;
349 break;
350 }
351 }
352
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700353 /*
354 * Find if there is DtsCritOffset is configured in config file
355 * set it if configured or else set it to 0
356 */
357 double dtsOffset = 0;
358 if (label == "DTS")
359 {
360 auto findThrOffset =
361 baseConfiguration->second.find("DtsCritOffset");
362 if (findThrOffset != baseConfiguration->second.end())
363 {
364 dtsOffset = std::visit(VariantToDoubleVisitor(),
365 findThrOffset->second);
366 }
367 }
368
James Feist6714a252018-09-10 15:26:18 -0700369 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700370 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700371 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700372 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800373 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700374 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700375 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700376 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700377 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700378 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700379 std::cerr << "error populating thresholds for "
380 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700381 }
382 }
James Feistc140e202019-11-14 15:23:51 -0800383 auto& sensorPtr = gCpuSensors[sensorName];
384 // make sure destructor fires before creating a new one
385 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700386 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700387 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800388 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700389 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530390 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700391 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700392 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700393 {
James Feist6714a252018-09-10 15:26:18 -0700394 std::cout << "Mapped: " << inputPath << " to " << sensorName
395 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700396 }
James Feist6714a252018-09-10 15:26:18 -0700397 }
398 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700399
Ed Tanous2049bd22022-07-09 07:20:26 -0700400 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700401 {
402 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
403 << " created\n";
404 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700405
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700406 return true;
James Feist6714a252018-09-10 15:26:18 -0700407}
408
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700409void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700410{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700411 std::ostringstream hex;
412 hex << std::hex << config.addr;
413 const std::string& addrHexStr = hex.str();
414 std::string busStr = std::to_string(config.bus);
415
416 std::string parameters = "peci-client 0x" + addrHexStr;
417 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
418
James Feistcf3bce62019-01-08 10:07:19 -0800419 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700420 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800421 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700422 {
James Feistcf3bce62019-01-08 10:07:19 -0800423 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700424 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700425 continue;
James Feist6714a252018-09-10 15:26:18 -0700426 }
427
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700428 const std::string& directoryName = path.path().filename();
429 if (boost::starts_with(directoryName, busStr) &&
430 boost::ends_with(directoryName, addrHexStr))
431 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700432 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700433 {
434 std::cout << parameters << " on bus " << busStr
435 << " is already exported\n";
436 }
437 return;
438 }
James Feist6714a252018-09-10 15:26:18 -0700439 }
440
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700441 std::ofstream deviceFile(device);
442 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700443 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700444 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700445 return;
446 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700447 deviceFile << parameters;
448 deviceFile.close();
449
450 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700451}
452
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800453void detectCpu(boost::asio::deadline_timer& pingTimer,
454 boost::asio::deadline_timer& creationTimer,
455 boost::asio::io_service& io,
456 sdbusplus::asio::object_server& objectServer,
457 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
458 boost::container::flat_set<CPUConfig>& cpuConfigs,
459 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700460{
James Feist6714a252018-09-10 15:26:18 -0700461 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700462 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700463
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800464 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700465 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700466 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800467
468 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700469 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
470 if (file < 0)
471 {
472 std::cerr << "unable to open " << peciDevPath << "\n";
473 std::exit(EXIT_FAILURE);
474 }
475
Ed Tanousa771f6a2022-01-14 09:36:51 -0800476 State newState = State::OFF;
477 struct peci_ping_msg msg
478 {};
James Feist6714a252018-09-10 15:26:18 -0700479 msg.addr = config.addr;
Ed Tanous99c44092022-01-14 09:59:09 -0800480
481 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700482 if (ioctl(file, PECI_IOC_PING, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700483 {
484 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700485 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700486 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800487 struct peci_rd_pkg_cfg_msg msg
488 {};
James Feist6714a252018-09-10 15:26:18 -0700489 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800490 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700491 msg.param = rank;
492 msg.rx_len = 4;
Ed Tanous99c44092022-01-14 09:59:09 -0800493
494 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700495 if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700496 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700497 if ((msg.pkg_config[0] != 0U) ||
498 (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700499 {
500 dimmReady = true;
501 break;
502 }
503 }
504 else
505 {
506 break;
507 }
508 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700509
James Feist6714a252018-09-10 15:26:18 -0700510 if (dimmReady)
511 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700512 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700513 }
514 else
515 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700516 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700517 }
518 }
James Feist6714a252018-09-10 15:26:18 -0700519
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700520 close(file);
521
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700522 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700523 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700524 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700525 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700526 if (config.state == State::OFF)
527 {
528 std::cout << config.name << " is detected\n";
529 exportDevice(config);
530 }
James Feist6714a252018-09-10 15:26:18 -0700531
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700532 if (newState == State::ON)
533 {
534 rescanDelaySeconds = 3;
535 }
536 else if (newState == State::READY)
537 {
538 rescanDelaySeconds = 5;
539 std::cout << "DIMM(s) on " << config.name
540 << " is/are detected\n";
541 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700542 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700543
544 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700545 }
546
547 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700548 {
549 keepPinging = true;
550 }
551
Ed Tanous8a57ec02020-10-09 12:46:52 -0700552 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700553 {
554 std::cout << config.name << ", state: " << config.state << "\n";
555 }
James Feist6714a252018-09-10 15:26:18 -0700556 }
557
Ed Tanous2049bd22022-07-09 07:20:26 -0700558 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700559 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700560 creationTimer.expires_from_now(
561 boost::posix_time::seconds(rescanDelaySeconds));
562 creationTimer.async_wait([&](const boost::system::error_code& ec) {
563 if (ec == boost::asio::error::operation_aborted)
564 {
565 return; // we're being canceled
566 }
567
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800568 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700569 sensorConfigs) ||
570 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700571 {
572 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800573 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700574 }
575 });
James Feist6714a252018-09-10 15:26:18 -0700576 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700577 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700578 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800579 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800580 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700581 }
582}
583
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700584void detectCpuAsync(
585 boost::asio::deadline_timer& pingTimer,
586 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
587 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800588 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800589 boost::container::flat_set<CPUConfig>& cpuConfigs,
590 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700591{
592 pingTimer.expires_from_now(boost::posix_time::seconds(1));
593 pingTimer.async_wait([&](const boost::system::error_code& ec) {
594 if (ec == boost::asio::error::operation_aborted)
595 {
596 return; // we're being canceled
597 }
598
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800599 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800600 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700601 });
602}
603
James Feistc140e202019-11-14 15:23:51 -0800604bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
605 boost::container::flat_set<CPUConfig>& cpuConfigs,
606 ManagedObjectType& sensorConfigs,
607 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700608{
James Feist6714a252018-09-10 15:26:18 -0700609 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800610 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700611 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700612 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700613 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700614 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800615 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700616 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700617 return false;
James Feist6714a252018-09-10 15:26:18 -0700618 }
619 useCache = true;
620 }
621
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700622 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700623 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700624 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700625 {
626 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800627 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700628 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700629 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700630 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700631 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700632 {
633 continue;
634 }
635
James Feist6714a252018-09-10 15:26:18 -0700636 auto findName = config.second.find("Name");
637 if (findName == config.second.end())
638 {
639 continue;
640 }
James Feist3eb82622019-02-08 13:10:22 -0800641 std::string nameRaw =
642 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700643 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700644 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700645
James Feistc140e202019-11-14 15:23:51 -0800646 auto present = std::optional<bool>();
647 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700648 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700649 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700650 if (suppConfig.first.find("PresenceGpio") !=
651 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700652 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700653 present = cpuIsPresent(suppConfig.second);
654 break;
James Feist58295ad2019-05-30 15:01:41 -0700655 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700656 }
657
James Feistc140e202019-11-14 15:23:51 -0800658 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
659 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700660 {
661 auto iface = objectServer.add_interface(
662 cpuInventoryPath + std::string("/") + name,
663 "xyz.openbmc_project.Inventory.Item");
664 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800665 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700666 iface->initialize();
667 inventoryIfaces[name] = std::move(iface);
668 }
James Feist58295ad2019-05-30 15:01:41 -0700669
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700670 auto findBus = config.second.find("Bus");
671 if (findBus == config.second.end())
672 {
673 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
674 continue;
675 }
James Feist3eb82622019-02-08 13:10:22 -0800676 uint64_t bus =
677 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700678
679 auto findAddress = config.second.find("Address");
680 if (findAddress == config.second.end())
681 {
682 std::cerr << "Can't find 'Address' setting in " << name
683 << "\n";
684 continue;
685 }
James Feist3eb82622019-02-08 13:10:22 -0800686 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
687 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700688
Ed Tanous8a57ec02020-10-09 12:46:52 -0700689 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700690 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700691 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700692 std::cout << "addr: " << addr << "\n";
693 std::cout << "name: " << name << "\n";
694 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700695 }
696
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800697 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700698 }
699 }
700 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700701
Ed Tanous2049bd22022-07-09 07:20:26 -0700702 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700703 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800704 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700705 << " parsed\n";
706 return true;
707 }
708
709 return false;
James Feist6714a252018-09-10 15:26:18 -0700710}
711
James Feistb6c0b912019-07-09 12:21:44 -0700712int main()
James Feist6714a252018-09-10 15:26:18 -0700713{
714 boost::asio::io_service io;
715 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800716 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700717
James Feist6714a252018-09-10 15:26:18 -0700718 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700719 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700720 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700721 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800722 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700723
724 filterTimer.expires_from_now(boost::posix_time::seconds(1));
725 filterTimer.async_wait([&](const boost::system::error_code& ec) {
726 if (ec == boost::asio::error::operation_aborted)
727 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700728 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700729 }
730
James Feistc140e202019-11-14 15:23:51 -0800731 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700732 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800733 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800734 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700735 }
736 });
737
Patrick Williams92f8f512022-07-22 19:26:55 -0500738 std::function<void(sdbusplus::message_t&)> eventHandler =
739 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700740 if (message.is_method_error())
741 {
742 std::cerr << "callback method error\n";
743 return;
744 }
745
746 if (debug)
747 {
748 std::cout << message.get_path() << " is changed\n";
749 }
750
751 // this implicitly cancels the timer
752 filterTimer.expires_from_now(boost::posix_time::seconds(1));
753 filterTimer.async_wait([&](const boost::system::error_code& ec) {
754 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700755 {
Ed Tanousbb679322022-05-16 16:10:00 -0700756 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700757 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700758
Ed Tanousbb679322022-05-16 16:10:00 -0700759 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
760 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700761 {
Ed Tanousbb679322022-05-16 16:10:00 -0700762 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
763 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700764 }
Ed Tanousbb679322022-05-16 16:10:00 -0700765 });
766 };
James Feist6714a252018-09-10 15:26:18 -0700767
Zev Weiss214d9712022-08-12 12:54:31 -0700768 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
769 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700770
Thu Nguyen255da6b2022-07-29 10:05:52 +0700771 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800772
773 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700774 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700775 return 0;
James Feist6714a252018-09-10 15:26:18 -0700776}