blob: 2780c1fd91353241da38813fd019a5208d915a33 [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/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/container/flat_set.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <sdbusplus/bus/match.hpp>
29
30#include <array>
James Feist24f02f22019-04-15 11:05:39 -070031#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070032#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <functional>
34#include <memory>
James Feist6714a252018-09-10 15:26:18 -070035#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <sstream>
37#include <stdexcept>
38#include <string>
39#include <utility>
40#include <variant>
41#include <vector>
James Feist6714a252018-09-10 15:26:18 -070042
James Feistf87dc4c2018-12-05 14:39:51 -080043// clang-format off
44// this needs to be included last or we'll have build issues
Ed Tanous74cffa82022-01-25 13:00:28 -080045extern "C" {
James Feistf87dc4c2018-12-05 14:39:51 -080046#include <linux/peci-ioctl.h>
Ed Tanous74cffa82022-01-25 13:00:28 -080047}
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080048#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
49#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
50#endif
James Feistf87dc4c2018-12-05 14:39:51 -080051// clang-format on
52
Ed Tanous8a57ec02020-10-09 12:46:52 -070053static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070054
Thu Nguyen255da6b2022-07-29 10:05:52 +070055boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
56 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080057boost::container::flat_map<std::string,
58 std::shared_ptr<sdbusplus::asio::dbus_interface>>
59 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080060
James Feist6714a252018-09-10 15:26:18 -070061enum State
62{
63 OFF, // host powered down
64 ON, // host powered on
65 READY // host powered on and mem test passed - fully ready
66};
67
68struct CPUConfig
69{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070070 CPUConfig(const uint64_t& bus, const uint64_t& addr,
71 const std::string& name, const State& state) :
72 bus(bus),
73 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070074 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 int bus;
James Feist6714a252018-09-10 15:26:18 -070076 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070077 std::string name;
James Feist6714a252018-09-10 15:26:18 -070078 State state;
79
80 bool operator<(const CPUConfig& rhs) const
81 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093082 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070083 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070084 }
85};
86
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070087static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070089
James Feistcf3bce62019-01-08 10:07:19 -080090namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080091
Zev Weiss26fb1492022-08-17 15:33:46 -070092static constexpr auto sensorTypes{
93 std::to_array<const char*>({"xyz.openbmc_project.Configuration.XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080094static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070095 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070096
Jae Hyun Yood64262b2018-11-01 13:31:16 -070097void detectCpuAsync(
98 boost::asio::deadline_timer& pingTimer,
99 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
100 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800102 boost::container::flat_set<CPUConfig>& cpuConfigs,
103 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700104
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200105std::string createSensorName(const std::string& label, const std::string& item,
106 const int& cpuId)
107{
108 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700109 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200110 {
111 sensorName += " " + item;
112 }
113 sensorName += " CPU" + std::to_string(cpuId);
114 // converting to Upper Camel case whole name
115 bool isWordEnd = true;
116 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
117 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700118 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700119 {
120 isWordEnd = true;
121 }
122 else
123 {
124 if (isWordEnd)
125 {
126 isWordEnd = false;
127 return std::toupper(c);
128 }
129 }
130 return c;
131 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200132 return sensorName;
133}
134
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800135bool createSensors(boost::asio::io_service& io,
136 sdbusplus::asio::object_server& objectServer,
137 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
138 boost::container::flat_set<CPUConfig>& cpuConfigs,
139 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700140{
141 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800142 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700143 {
144 if (cpu.state != State::OFF)
145 {
146 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500147 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800148 inventoryIfaces[cpu.name];
149 if (iface != nullptr)
150 {
151 continue;
152 }
153 iface = objectServer.add_interface(
154 cpuInventoryPath + std::string("/") + cpu.name,
155 "xyz.openbmc_project.Inventory.Item");
156 iface->register_property("PrettyName", cpu.name);
157 iface->register_property("Present", true);
158 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700159 }
160 }
161 if (!available)
162 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700163 return false;
James Feist6714a252018-09-10 15:26:18 -0700164 }
165
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800166 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700167 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800168 return false;
James Feist6714a252018-09-10 15:26:18 -0700169 }
170
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700171 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800172 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
173 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700174 {
175 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700176 return true;
James Feist6714a252018-09-10 15:26:18 -0700177 }
178
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700179 boost::container::flat_set<std::string> scannedDirectories;
180 boost::container::flat_set<std::string> createdSensors;
181
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800182 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700183 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700184 auto hwmonDirectory = hwmonNamePath.parent_path();
185
186 auto ret = scannedDirectories.insert(hwmonDirectory.string());
187 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700188 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700189 continue; // already searched this path
190 }
191
192 fs::path::iterator it = hwmonNamePath.begin();
193 std::advance(it, 6); // pick the 6th part for a PECI client device name
194 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700195 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700196 if (findHyphen == std::string::npos)
197 {
198 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700199 continue;
200 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700201 std::string busStr = deviceName.substr(0, findHyphen);
202 std::string addrStr = deviceName.substr(findHyphen + 1);
203
204 size_t bus = 0;
205 size_t addr = 0;
206 try
207 {
208 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700209 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700210 }
Patrick Williams26601e82021-10-06 12:43:25 -0500211 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700212 {
213 continue;
214 }
215
216 std::ifstream nameFile(hwmonNamePath);
217 if (!nameFile.good())
218 {
219 std::cerr << "Failure reading " << hwmonNamePath << "\n";
220 continue;
221 }
222 std::string hwmonName;
223 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700224 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800225 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700226 {
227 // shouldn't have an empty name file
228 continue;
229 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700230 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700231 {
232 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
233 << "\n";
234 }
James Feist6714a252018-09-10 15:26:18 -0700235
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700236 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700237 const SensorData* sensorData = nullptr;
238 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700239 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700240
Zev Weiss8908b3c2022-08-12 18:21:01 -0700241 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700242 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700243 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700244 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700246 sensorType = type;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700247 auto sensorBase = sensorData->find(sensorType);
248 if (sensorBase != sensorData->end())
249 {
250 baseConfiguration = &(*sensorBase);
251 break;
252 }
253 }
254 if (baseConfiguration == nullptr)
255 {
256 std::cerr << "error finding base configuration for" << hwmonName
257 << "\n";
258 continue;
259 }
260 auto configurationBus = baseConfiguration->second.find("Bus");
261 auto configurationAddress =
262 baseConfiguration->second.find("Address");
263
264 if (configurationBus == baseConfiguration->second.end() ||
265 configurationAddress == baseConfiguration->second.end())
266 {
267 std::cerr << "error finding bus or address in configuration";
268 continue;
269 }
270
James Feist3eb82622019-02-08 13:10:22 -0800271 if (std::get<uint64_t>(configurationBus->second) != bus ||
272 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700273 {
274 continue;
275 }
276
Zev Weiss8908b3c2022-08-12 18:21:01 -0700277 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700278 break;
279 }
280 if (interfacePath == nullptr)
281 {
282 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700283 continue;
284 }
285
286 auto findCpuId = baseConfiguration->second.find("CpuID");
287 if (findCpuId == baseConfiguration->second.end())
288 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700289 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700290 continue;
291 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800293 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700294
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700296 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200297 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200298 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700299 {
300 std::cerr << "No temperature sensors in system\n";
301 continue;
302 }
303
304 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800305 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700306 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200307 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200308 if (!fileParts)
309 {
310 continue;
311 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200312 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700313 auto inputPathStr = inputPath.string();
314 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200315 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700316 std::ifstream labelFile(labelPath);
317 if (!labelFile.good())
318 {
319 std::cerr << "Failure reading " << labelPath << "\n";
320 continue;
321 }
322 std::string label;
323 std::getline(labelFile, label);
324 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800325
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200326 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700327
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800328 auto findSensor = gCpuSensors.find(sensorName);
329 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700331 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700332 {
333 std::cout << "Skipped: " << inputPath << ": " << sensorName
334 << " is already created\n";
335 }
336 continue;
337 }
338
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800339 // check hidden properties
340 bool show = true;
341 for (const char* prop : hiddenProps)
342 {
343 if (label == prop)
344 {
345 show = false;
346 break;
347 }
348 }
349
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700350 /*
351 * Find if there is DtsCritOffset is configured in config file
352 * set it if configured or else set it to 0
353 */
354 double dtsOffset = 0;
355 if (label == "DTS")
356 {
357 auto findThrOffset =
358 baseConfiguration->second.find("DtsCritOffset");
359 if (findThrOffset != baseConfiguration->second.end())
360 {
361 dtsOffset = std::visit(VariantToDoubleVisitor(),
362 findThrOffset->second);
363 }
364 }
365
James Feist6714a252018-09-10 15:26:18 -0700366 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700367 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700368 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700369 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800370 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700371 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700372 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700373 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700374 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700375 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700376 std::cerr << "error populating thresholds for "
377 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700378 }
379 }
James Feistc140e202019-11-14 15:23:51 -0800380 auto& sensorPtr = gCpuSensors[sensorName];
381 // make sure destructor fires before creating a new one
382 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700383 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700384 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800385 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700386 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530387 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700388 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700389 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 {
James Feist6714a252018-09-10 15:26:18 -0700391 std::cout << "Mapped: " << inputPath << " to " << sensorName
392 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700393 }
James Feist6714a252018-09-10 15:26:18 -0700394 }
395 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700396
Ed Tanous2049bd22022-07-09 07:20:26 -0700397 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700398 {
399 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
400 << " created\n";
401 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700402
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700403 return true;
James Feist6714a252018-09-10 15:26:18 -0700404}
405
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700407{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700408 std::ostringstream hex;
409 hex << std::hex << config.addr;
410 const std::string& addrHexStr = hex.str();
411 std::string busStr = std::to_string(config.bus);
412
413 std::string parameters = "peci-client 0x" + addrHexStr;
414 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
415
James Feistcf3bce62019-01-08 10:07:19 -0800416 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700417 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800418 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700419 {
James Feistcf3bce62019-01-08 10:07:19 -0800420 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700421 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700422 continue;
James Feist6714a252018-09-10 15:26:18 -0700423 }
424
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700425 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700426 if (directoryName.starts_with(busStr) &&
427 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700428 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700429 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700430 {
431 std::cout << parameters << " on bus " << busStr
432 << " is already exported\n";
433 }
434 return;
435 }
James Feist6714a252018-09-10 15:26:18 -0700436 }
437
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700438 std::ofstream deviceFile(device);
439 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700440 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700441 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700442 return;
443 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700444 deviceFile << parameters;
445 deviceFile.close();
446
447 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700448}
449
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800450void detectCpu(boost::asio::deadline_timer& pingTimer,
451 boost::asio::deadline_timer& creationTimer,
452 boost::asio::io_service& io,
453 sdbusplus::asio::object_server& objectServer,
454 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
455 boost::container::flat_set<CPUConfig>& cpuConfigs,
456 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700457{
James Feist6714a252018-09-10 15:26:18 -0700458 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700459 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700460
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800461 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700462 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700463 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800464
465 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700466 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
467 if (file < 0)
468 {
469 std::cerr << "unable to open " << peciDevPath << "\n";
470 std::exit(EXIT_FAILURE);
471 }
472
Ed Tanousa771f6a2022-01-14 09:36:51 -0800473 State newState = State::OFF;
474 struct peci_ping_msg msg
475 {};
James Feist6714a252018-09-10 15:26:18 -0700476 msg.addr = config.addr;
Ed Tanous99c44092022-01-14 09:59:09 -0800477
478 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700479 if (ioctl(file, PECI_IOC_PING, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700480 {
481 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700482 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700483 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800484 struct peci_rd_pkg_cfg_msg msg
485 {};
James Feist6714a252018-09-10 15:26:18 -0700486 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800487 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700488 msg.param = rank;
489 msg.rx_len = 4;
Ed Tanous99c44092022-01-14 09:59:09 -0800490
491 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Ed Tanous2049bd22022-07-09 07:20:26 -0700492 if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0)
James Feist6714a252018-09-10 15:26:18 -0700493 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700494 if ((msg.pkg_config[0] != 0U) ||
495 (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U))
James Feist6714a252018-09-10 15:26:18 -0700496 {
497 dimmReady = true;
498 break;
499 }
500 }
501 else
502 {
503 break;
504 }
505 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700506
James Feist6714a252018-09-10 15:26:18 -0700507 if (dimmReady)
508 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700509 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700510 }
511 else
512 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700513 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700514 }
515 }
James Feist6714a252018-09-10 15:26:18 -0700516
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700517 close(file);
518
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700519 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700520 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700521 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700522 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700523 if (config.state == State::OFF)
524 {
525 std::cout << config.name << " is detected\n";
526 exportDevice(config);
527 }
James Feist6714a252018-09-10 15:26:18 -0700528
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700529 if (newState == State::ON)
530 {
531 rescanDelaySeconds = 3;
532 }
533 else if (newState == State::READY)
534 {
535 rescanDelaySeconds = 5;
536 std::cout << "DIMM(s) on " << config.name
537 << " is/are detected\n";
538 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700539 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700540
541 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700542 }
543
544 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700545 {
546 keepPinging = true;
547 }
548
Ed Tanous8a57ec02020-10-09 12:46:52 -0700549 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700550 {
551 std::cout << config.name << ", state: " << config.state << "\n";
552 }
James Feist6714a252018-09-10 15:26:18 -0700553 }
554
Ed Tanous2049bd22022-07-09 07:20:26 -0700555 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700556 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700557 creationTimer.expires_from_now(
558 boost::posix_time::seconds(rescanDelaySeconds));
559 creationTimer.async_wait([&](const boost::system::error_code& ec) {
560 if (ec == boost::asio::error::operation_aborted)
561 {
562 return; // we're being canceled
563 }
564
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800565 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700566 sensorConfigs) ||
567 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700568 {
569 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800570 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700571 }
572 });
James Feist6714a252018-09-10 15:26:18 -0700573 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700574 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700575 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800576 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800577 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700578 }
579}
580
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700581void detectCpuAsync(
582 boost::asio::deadline_timer& pingTimer,
583 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
584 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800585 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800586 boost::container::flat_set<CPUConfig>& cpuConfigs,
587 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700588{
589 pingTimer.expires_from_now(boost::posix_time::seconds(1));
590 pingTimer.async_wait([&](const boost::system::error_code& ec) {
591 if (ec == boost::asio::error::operation_aborted)
592 {
593 return; // we're being canceled
594 }
595
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800596 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800597 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700598 });
599}
600
James Feistc140e202019-11-14 15:23:51 -0800601bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
602 boost::container::flat_set<CPUConfig>& cpuConfigs,
603 ManagedObjectType& sensorConfigs,
604 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700605{
James Feist6714a252018-09-10 15:26:18 -0700606 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800607 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700608 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700609 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700610 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700611 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700612 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700613 return false;
James Feist6714a252018-09-10 15:26:18 -0700614 }
615 useCache = true;
616 }
617
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700618 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700619 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700620 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700621 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700622 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700623 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700624 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700625 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700626 if (intf != type)
James Feist6714a252018-09-10 15:26:18 -0700627 {
628 continue;
629 }
630
Zev Weiss8908b3c2022-08-12 18:21:01 -0700631 auto findName = cfg.find("Name");
632 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700633 {
634 continue;
635 }
James Feist3eb82622019-02-08 13:10:22 -0800636 std::string nameRaw =
637 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700638 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700639 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700640
James Feistc140e202019-11-14 15:23:51 -0800641 auto present = std::optional<bool>();
642 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700643 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700644 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700645 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700646 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700647 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700648 break;
James Feist58295ad2019-05-30 15:01:41 -0700649 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700650 }
651
James Feistc140e202019-11-14 15:23:51 -0800652 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
653 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700654 {
655 auto iface = objectServer.add_interface(
656 cpuInventoryPath + std::string("/") + name,
657 "xyz.openbmc_project.Inventory.Item");
658 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800659 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700660 iface->initialize();
661 inventoryIfaces[name] = std::move(iface);
662 }
James Feist58295ad2019-05-30 15:01:41 -0700663
Zev Weiss8908b3c2022-08-12 18:21:01 -0700664 auto findBus = cfg.find("Bus");
665 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700666 {
667 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
668 continue;
669 }
James Feist3eb82622019-02-08 13:10:22 -0800670 uint64_t bus =
671 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700672
Zev Weiss8908b3c2022-08-12 18:21:01 -0700673 auto findAddress = cfg.find("Address");
674 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700675 {
676 std::cerr << "Can't find 'Address' setting in " << name
677 << "\n";
678 continue;
679 }
James Feist3eb82622019-02-08 13:10:22 -0800680 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
681 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700682
Ed Tanous8a57ec02020-10-09 12:46:52 -0700683 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700684 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700685 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700686 std::cout << "addr: " << addr << "\n";
687 std::cout << "name: " << name << "\n";
688 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700689 }
690
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800691 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700692 }
693 }
694 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700695
Ed Tanous2049bd22022-07-09 07:20:26 -0700696 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700697 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800698 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700699 << " parsed\n";
700 return true;
701 }
702
703 return false;
James Feist6714a252018-09-10 15:26:18 -0700704}
705
James Feistb6c0b912019-07-09 12:21:44 -0700706int main()
James Feist6714a252018-09-10 15:26:18 -0700707{
708 boost::asio::io_service io;
709 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800710 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700711
James Feist6714a252018-09-10 15:26:18 -0700712 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700713 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700714 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700715 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800716 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700717
718 filterTimer.expires_from_now(boost::posix_time::seconds(1));
719 filterTimer.async_wait([&](const boost::system::error_code& ec) {
720 if (ec == boost::asio::error::operation_aborted)
721 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700722 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700723 }
724
James Feistc140e202019-11-14 15:23:51 -0800725 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700726 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800727 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800728 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700729 }
730 });
731
Patrick Williams92f8f512022-07-22 19:26:55 -0500732 std::function<void(sdbusplus::message_t&)> eventHandler =
733 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700734 if (message.is_method_error())
735 {
736 std::cerr << "callback method error\n";
737 return;
738 }
739
740 if (debug)
741 {
742 std::cout << message.get_path() << " is changed\n";
743 }
744
745 // this implicitly cancels the timer
746 filterTimer.expires_from_now(boost::posix_time::seconds(1));
747 filterTimer.async_wait([&](const boost::system::error_code& ec) {
748 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700749 {
Ed Tanousbb679322022-05-16 16:10:00 -0700750 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700751 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700752
Ed Tanousbb679322022-05-16 16:10:00 -0700753 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
754 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700755 {
Ed Tanousbb679322022-05-16 16:10:00 -0700756 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
757 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700758 }
Ed Tanousbb679322022-05-16 16:10:00 -0700759 });
760 };
James Feist6714a252018-09-10 15:26:18 -0700761
Zev Weiss214d9712022-08-12 12:54:31 -0700762 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
763 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700764
Thu Nguyen255da6b2022-07-29 10:05:52 +0700765 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800766
767 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700768 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700769 return 0;
James Feist6714a252018-09-10 15:26:18 -0700770}