blob: b2238a6c828ac736d208e7250f40fec5112e1d45 [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
Zev Weiss26fb1492022-08-17 15:33:46 -070093static constexpr auto sensorTypes{
94 std::to_array<const char*>({"xyz.openbmc_project.Configuration.XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080095static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070096 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070097
Jae Hyun Yood64262b2018-11-01 13:31:16 -070098void detectCpuAsync(
99 boost::asio::deadline_timer& pingTimer,
100 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
101 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800102 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800103 boost::container::flat_set<CPUConfig>& cpuConfigs,
104 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700105
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200106std::string createSensorName(const std::string& label, const std::string& item,
107 const int& cpuId)
108{
109 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700110 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200111 {
112 sensorName += " " + item;
113 }
114 sensorName += " CPU" + std::to_string(cpuId);
115 // converting to Upper Camel case whole name
116 bool isWordEnd = true;
117 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
118 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700119 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700120 {
121 isWordEnd = true;
122 }
123 else
124 {
125 if (isWordEnd)
126 {
127 isWordEnd = false;
128 return std::toupper(c);
129 }
130 }
131 return c;
132 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200133 return sensorName;
134}
135
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800136bool createSensors(boost::asio::io_service& io,
137 sdbusplus::asio::object_server& objectServer,
138 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
139 boost::container::flat_set<CPUConfig>& cpuConfigs,
140 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700141{
142 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800143 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700144 {
145 if (cpu.state != State::OFF)
146 {
147 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500148 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800149 inventoryIfaces[cpu.name];
150 if (iface != nullptr)
151 {
152 continue;
153 }
154 iface = objectServer.add_interface(
155 cpuInventoryPath + std::string("/") + cpu.name,
156 "xyz.openbmc_project.Inventory.Item");
157 iface->register_property("PrettyName", cpu.name);
158 iface->register_property("Present", true);
159 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 }
162 if (!available)
163 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700164 return false;
James Feist6714a252018-09-10 15:26:18 -0700165 }
166
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800167 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700168 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800169 return false;
James Feist6714a252018-09-10 15:26:18 -0700170 }
171
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700172 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800173 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
174 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700175 {
176 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700177 return true;
James Feist6714a252018-09-10 15:26:18 -0700178 }
179
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700180 boost::container::flat_set<std::string> scannedDirectories;
181 boost::container::flat_set<std::string> createdSensors;
182
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800183 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700184 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700185 auto hwmonDirectory = hwmonNamePath.parent_path();
186
187 auto ret = scannedDirectories.insert(hwmonDirectory.string());
188 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700189 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700190 continue; // already searched this path
191 }
192
193 fs::path::iterator it = hwmonNamePath.begin();
194 std::advance(it, 6); // pick the 6th part for a PECI client device name
195 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700196 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700197 if (findHyphen == std::string::npos)
198 {
199 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700200 continue;
201 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700202 std::string busStr = deviceName.substr(0, findHyphen);
203 std::string addrStr = deviceName.substr(findHyphen + 1);
204
205 size_t bus = 0;
206 size_t addr = 0;
207 try
208 {
209 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700210 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700211 }
Patrick Williams26601e82021-10-06 12:43:25 -0500212 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 {
214 continue;
215 }
216
217 std::ifstream nameFile(hwmonNamePath);
218 if (!nameFile.good())
219 {
220 std::cerr << "Failure reading " << hwmonNamePath << "\n";
221 continue;
222 }
223 std::string hwmonName;
224 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700225 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800226 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700227 {
228 // shouldn't have an empty name file
229 continue;
230 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700231 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700232 {
233 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
234 << "\n";
235 }
James Feist6714a252018-09-10 15:26:18 -0700236
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700237 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700238 const SensorData* sensorData = nullptr;
239 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700240 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700241
Zev Weiss8908b3c2022-08-12 18:21:01 -0700242 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700243 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700244 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700245 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700247 sensorType = type;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 auto sensorBase = sensorData->find(sensorType);
249 if (sensorBase != sensorData->end())
250 {
251 baseConfiguration = &(*sensorBase);
252 break;
253 }
254 }
255 if (baseConfiguration == nullptr)
256 {
257 std::cerr << "error finding base configuration for" << hwmonName
258 << "\n";
259 continue;
260 }
261 auto configurationBus = baseConfiguration->second.find("Bus");
262 auto configurationAddress =
263 baseConfiguration->second.find("Address");
264
265 if (configurationBus == baseConfiguration->second.end() ||
266 configurationAddress == baseConfiguration->second.end())
267 {
268 std::cerr << "error finding bus or address in configuration";
269 continue;
270 }
271
James Feist3eb82622019-02-08 13:10:22 -0800272 if (std::get<uint64_t>(configurationBus->second) != bus ||
273 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700274 {
275 continue;
276 }
277
Zev Weiss8908b3c2022-08-12 18:21:01 -0700278 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700279 break;
280 }
281 if (interfacePath == nullptr)
282 {
283 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700284 continue;
285 }
286
287 auto findCpuId = baseConfiguration->second.find("CpuID");
288 if (findCpuId == baseConfiguration->second.end())
289 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700290 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700291 continue;
292 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700293 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800294 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700295
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700296 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700297 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200298 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200299 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700300 {
301 std::cerr << "No temperature sensors in system\n";
302 continue;
303 }
304
305 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800306 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700307 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200308 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200309 if (!fileParts)
310 {
311 continue;
312 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200313 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700314 auto inputPathStr = inputPath.string();
315 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200316 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700317 std::ifstream labelFile(labelPath);
318 if (!labelFile.good())
319 {
320 std::cerr << "Failure reading " << labelPath << "\n";
321 continue;
322 }
323 std::string label;
324 std::getline(labelFile, label);
325 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800326
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200327 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700328
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800329 auto findSensor = gCpuSensors.find(sensorName);
330 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700332 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 {
334 std::cout << "Skipped: " << inputPath << ": " << sensorName
335 << " is already created\n";
336 }
337 continue;
338 }
339
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800340 // check hidden properties
341 bool show = true;
342 for (const char* prop : hiddenProps)
343 {
344 if (label == prop)
345 {
346 show = false;
347 break;
348 }
349 }
350
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700351 /*
352 * Find if there is DtsCritOffset is configured in config file
353 * set it if configured or else set it to 0
354 */
355 double dtsOffset = 0;
356 if (label == "DTS")
357 {
358 auto findThrOffset =
359 baseConfiguration->second.find("DtsCritOffset");
360 if (findThrOffset != baseConfiguration->second.end())
361 {
362 dtsOffset = std::visit(VariantToDoubleVisitor(),
363 findThrOffset->second);
364 }
365 }
366
James Feist6714a252018-09-10 15:26:18 -0700367 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700368 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700369 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700370 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800371 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700372 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700373 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700374 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700375 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700376 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700377 std::cerr << "error populating thresholds for "
378 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700379 }
380 }
James Feistc140e202019-11-14 15:23:51 -0800381 auto& sensorPtr = gCpuSensors[sensorName];
382 // make sure destructor fires before creating a new one
383 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700384 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700385 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800386 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700387 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530388 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700389 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700390 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700391 {
James Feist6714a252018-09-10 15:26:18 -0700392 std::cout << "Mapped: " << inputPath << " to " << sensorName
393 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700394 }
James Feist6714a252018-09-10 15:26:18 -0700395 }
396 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700397
Ed Tanous2049bd22022-07-09 07:20:26 -0700398 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700399 {
400 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
401 << " created\n";
402 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700403
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700404 return true;
James Feist6714a252018-09-10 15:26:18 -0700405}
406
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700407void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700408{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700409 std::ostringstream hex;
410 hex << std::hex << config.addr;
411 const std::string& addrHexStr = hex.str();
412 std::string busStr = std::to_string(config.bus);
413
414 std::string parameters = "peci-client 0x" + addrHexStr;
415 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
416
James Feistcf3bce62019-01-08 10:07:19 -0800417 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700418 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800419 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700420 {
James Feistcf3bce62019-01-08 10:07:19 -0800421 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700422 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700423 continue;
James Feist6714a252018-09-10 15:26:18 -0700424 }
425
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700426 const std::string& directoryName = path.path().filename();
427 if (boost::starts_with(directoryName, busStr) &&
428 boost::ends_with(directoryName, addrHexStr))
429 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700430 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700431 {
432 std::cout << parameters << " on bus " << busStr
433 << " is already exported\n";
434 }
435 return;
436 }
James Feist6714a252018-09-10 15:26:18 -0700437 }
438
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700439 std::ofstream deviceFile(device);
440 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700441 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700442 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700443 return;
444 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700445 deviceFile << parameters;
446 deviceFile.close();
447
448 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700449}
450
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800451void detectCpu(boost::asio::deadline_timer& pingTimer,
452 boost::asio::deadline_timer& creationTimer,
453 boost::asio::io_service& io,
454 sdbusplus::asio::object_server& objectServer,
455 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
456 boost::container::flat_set<CPUConfig>& cpuConfigs,
457 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700458{
James Feist6714a252018-09-10 15:26:18 -0700459 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700460 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700461
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800462 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700463 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700464 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800465
466 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700467 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
468 if (file < 0)
469 {
470 std::cerr << "unable to open " << peciDevPath << "\n";
471 std::exit(EXIT_FAILURE);
472 }
473
Ed Tanousa771f6a2022-01-14 09:36:51 -0800474 State newState = State::OFF;
475 struct peci_ping_msg msg
476 {};
James Feist6714a252018-09-10 15:26:18 -0700477 msg.addr = config.addr;
Ed Tanous99c44092022-01-14 09:59:09 -0800478
479 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700480 if (ioctl(file, PECI_IOC_PING, &msg) == 0)
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 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800485 struct peci_rd_pkg_cfg_msg msg
486 {};
James Feist6714a252018-09-10 15:26:18 -0700487 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800488 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700489 msg.param = rank;
490 msg.rx_len = 4;
Ed Tanous99c44092022-01-14 09:59:09 -0800491
492 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700493 if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700494 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700495 if ((msg.pkg_config[0] != 0U) ||
496 (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700497 {
498 dimmReady = true;
499 break;
500 }
501 }
502 else
503 {
504 break;
505 }
506 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700507
James Feist6714a252018-09-10 15:26:18 -0700508 if (dimmReady)
509 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700510 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700511 }
512 else
513 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700514 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700515 }
516 }
James Feist6714a252018-09-10 15:26:18 -0700517
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700518 close(file);
519
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700520 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700521 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700522 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700523 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700524 if (config.state == State::OFF)
525 {
526 std::cout << config.name << " is detected\n";
527 exportDevice(config);
528 }
James Feist6714a252018-09-10 15:26:18 -0700529
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700530 if (newState == State::ON)
531 {
532 rescanDelaySeconds = 3;
533 }
534 else if (newState == State::READY)
535 {
536 rescanDelaySeconds = 5;
537 std::cout << "DIMM(s) on " << config.name
538 << " is/are detected\n";
539 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700540 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700541
542 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700543 }
544
545 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700546 {
547 keepPinging = true;
548 }
549
Ed Tanous8a57ec02020-10-09 12:46:52 -0700550 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700551 {
552 std::cout << config.name << ", state: " << config.state << "\n";
553 }
James Feist6714a252018-09-10 15:26:18 -0700554 }
555
Ed Tanous2049bd22022-07-09 07:20:26 -0700556 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700557 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700558 creationTimer.expires_from_now(
559 boost::posix_time::seconds(rescanDelaySeconds));
560 creationTimer.async_wait([&](const boost::system::error_code& ec) {
561 if (ec == boost::asio::error::operation_aborted)
562 {
563 return; // we're being canceled
564 }
565
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800566 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700567 sensorConfigs) ||
568 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700569 {
570 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800571 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700572 }
573 });
James Feist6714a252018-09-10 15:26:18 -0700574 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700575 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700576 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800577 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800578 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700579 }
580}
581
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700582void detectCpuAsync(
583 boost::asio::deadline_timer& pingTimer,
584 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
585 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800586 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800587 boost::container::flat_set<CPUConfig>& cpuConfigs,
588 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700589{
590 pingTimer.expires_from_now(boost::posix_time::seconds(1));
591 pingTimer.async_wait([&](const boost::system::error_code& ec) {
592 if (ec == boost::asio::error::operation_aborted)
593 {
594 return; // we're being canceled
595 }
596
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800597 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800598 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700599 });
600}
601
James Feistc140e202019-11-14 15:23:51 -0800602bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
603 boost::container::flat_set<CPUConfig>& cpuConfigs,
604 ManagedObjectType& sensorConfigs,
605 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700606{
James Feist6714a252018-09-10 15:26:18 -0700607 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800608 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700609 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700610 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700611 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700612 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700613 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700614 return false;
James Feist6714a252018-09-10 15:26:18 -0700615 }
616 useCache = true;
617 }
618
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700619 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700620 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700621 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700622 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700623 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700624 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700625 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700626 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700627 if (intf != type)
James Feist6714a252018-09-10 15:26:18 -0700628 {
629 continue;
630 }
631
Zev Weiss8908b3c2022-08-12 18:21:01 -0700632 auto findName = cfg.find("Name");
633 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700634 {
635 continue;
636 }
James Feist3eb82622019-02-08 13:10:22 -0800637 std::string nameRaw =
638 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700639 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700640 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700641
James Feistc140e202019-11-14 15:23:51 -0800642 auto present = std::optional<bool>();
643 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700644 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700645 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700646 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700647 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700648 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700649 break;
James Feist58295ad2019-05-30 15:01:41 -0700650 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700651 }
652
James Feistc140e202019-11-14 15:23:51 -0800653 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
654 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700655 {
656 auto iface = objectServer.add_interface(
657 cpuInventoryPath + std::string("/") + name,
658 "xyz.openbmc_project.Inventory.Item");
659 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800660 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700661 iface->initialize();
662 inventoryIfaces[name] = std::move(iface);
663 }
James Feist58295ad2019-05-30 15:01:41 -0700664
Zev Weiss8908b3c2022-08-12 18:21:01 -0700665 auto findBus = cfg.find("Bus");
666 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700667 {
668 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
669 continue;
670 }
James Feist3eb82622019-02-08 13:10:22 -0800671 uint64_t bus =
672 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700673
Zev Weiss8908b3c2022-08-12 18:21:01 -0700674 auto findAddress = cfg.find("Address");
675 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700676 {
677 std::cerr << "Can't find 'Address' setting in " << name
678 << "\n";
679 continue;
680 }
James Feist3eb82622019-02-08 13:10:22 -0800681 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
682 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700683
Ed Tanous8a57ec02020-10-09 12:46:52 -0700684 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700685 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700686 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700687 std::cout << "addr: " << addr << "\n";
688 std::cout << "name: " << name << "\n";
689 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700690 }
691
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800692 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700693 }
694 }
695 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700696
Ed Tanous2049bd22022-07-09 07:20:26 -0700697 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800699 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700700 << " parsed\n";
701 return true;
702 }
703
704 return false;
James Feist6714a252018-09-10 15:26:18 -0700705}
706
James Feistb6c0b912019-07-09 12:21:44 -0700707int main()
James Feist6714a252018-09-10 15:26:18 -0700708{
709 boost::asio::io_service io;
710 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800711 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700712
James Feist6714a252018-09-10 15:26:18 -0700713 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700714 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700715 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700716 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800717 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700718
719 filterTimer.expires_from_now(boost::posix_time::seconds(1));
720 filterTimer.async_wait([&](const boost::system::error_code& ec) {
721 if (ec == boost::asio::error::operation_aborted)
722 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700723 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700724 }
725
James Feistc140e202019-11-14 15:23:51 -0800726 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700727 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800728 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800729 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700730 }
731 });
732
Patrick Williams92f8f512022-07-22 19:26:55 -0500733 std::function<void(sdbusplus::message_t&)> eventHandler =
734 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700735 if (message.is_method_error())
736 {
737 std::cerr << "callback method error\n";
738 return;
739 }
740
741 if (debug)
742 {
743 std::cout << message.get_path() << " is changed\n";
744 }
745
746 // this implicitly cancels the timer
747 filterTimer.expires_from_now(boost::posix_time::seconds(1));
748 filterTimer.async_wait([&](const boost::system::error_code& ec) {
749 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700750 {
Ed Tanousbb679322022-05-16 16:10:00 -0700751 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700752 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700753
Ed Tanousbb679322022-05-16 16:10:00 -0700754 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
755 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700756 {
Ed Tanousbb679322022-05-16 16:10:00 -0700757 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
758 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700759 }
Ed Tanousbb679322022-05-16 16:10:00 -0700760 });
761 };
James Feist6714a252018-09-10 15:26:18 -0700762
Zev Weiss214d9712022-08-12 12:54:31 -0700763 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
764 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700765
Thu Nguyen255da6b2022-07-29 10:05:52 +0700766 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800767
768 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700769 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700770 return 0;
James Feist6714a252018-09-10 15:26:18 -0700771}