blob: ecc36dcf9ba5f8da13fb18979828e368510b269b [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IntelCPUSensor.hpp"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070022
James Feist6714a252018-09-10 15:26:18 -070023#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>
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 Weiss054aad82022-08-18 01:37:34 -070092static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080093static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070094 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070095
Jae Hyun Yood64262b2018-11-01 13:31:16 -070096void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -070097 boost::asio::steady_timer& pingTimer,
98 boost::asio::steady_timer& creationTimer, boost::asio::io_service& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070099 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800100 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 boost::container::flat_set<CPUConfig>& cpuConfigs,
102 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700103
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200104std::string createSensorName(const std::string& label, const std::string& item,
105 const int& cpuId)
106{
107 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700108 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200109 {
110 sensorName += " " + item;
111 }
112 sensorName += " CPU" + std::to_string(cpuId);
113 // converting to Upper Camel case whole name
114 bool isWordEnd = true;
115 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
116 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700117 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700118 {
119 isWordEnd = true;
120 }
121 else
122 {
123 if (isWordEnd)
124 {
125 isWordEnd = false;
126 return std::toupper(c);
127 }
128 }
129 return c;
130 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200131 return sensorName;
132}
133
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800134bool createSensors(boost::asio::io_service& io,
135 sdbusplus::asio::object_server& objectServer,
136 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
137 boost::container::flat_set<CPUConfig>& cpuConfigs,
138 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700139{
140 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800141 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700142 {
143 if (cpu.state != State::OFF)
144 {
145 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500146 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800147 inventoryIfaces[cpu.name];
148 if (iface != nullptr)
149 {
150 continue;
151 }
152 iface = objectServer.add_interface(
153 cpuInventoryPath + std::string("/") + cpu.name,
154 "xyz.openbmc_project.Inventory.Item");
155 iface->register_property("PrettyName", cpu.name);
156 iface->register_property("Present", true);
157 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700158 }
159 }
160 if (!available)
161 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700162 return false;
James Feist6714a252018-09-10 15:26:18 -0700163 }
164
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800165 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700166 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800167 return false;
James Feist6714a252018-09-10 15:26:18 -0700168 }
169
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700170 std::vector<fs::path> hwmonNamePaths;
Lei YU0b207a62021-10-20 13:41:51 +0800171 if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"),
172 R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5))
James Feist6714a252018-09-10 15:26:18 -0700173 {
174 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700175 return true;
James Feist6714a252018-09-10 15:26:18 -0700176 }
177
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700178 boost::container::flat_set<std::string> scannedDirectories;
179 boost::container::flat_set<std::string> createdSensors;
180
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800181 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700182 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700183 auto hwmonDirectory = hwmonNamePath.parent_path();
184
185 auto ret = scannedDirectories.insert(hwmonDirectory.string());
186 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700187 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700188 continue; // already searched this path
189 }
190
191 fs::path::iterator it = hwmonNamePath.begin();
192 std::advance(it, 6); // pick the 6th part for a PECI client device name
193 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700194 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 if (findHyphen == std::string::npos)
196 {
197 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700198 continue;
199 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700200 std::string busStr = deviceName.substr(0, findHyphen);
201 std::string addrStr = deviceName.substr(findHyphen + 1);
202
203 size_t bus = 0;
204 size_t addr = 0;
205 try
206 {
207 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700208 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700209 }
Patrick Williams26601e82021-10-06 12:43:25 -0500210 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700211 {
212 continue;
213 }
214
215 std::ifstream nameFile(hwmonNamePath);
216 if (!nameFile.good())
217 {
218 std::cerr << "Failure reading " << hwmonNamePath << "\n";
219 continue;
220 }
221 std::string hwmonName;
222 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700223 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800224 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700225 {
226 // shouldn't have an empty name file
227 continue;
228 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700229 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700230 {
231 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
232 << "\n";
233 }
James Feist6714a252018-09-10 15:26:18 -0700234
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700235 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700236 const SensorData* sensorData = nullptr;
237 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700238 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700239
Zev Weiss8908b3c2022-08-12 18:21:01 -0700240 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700241 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700242 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700243 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700244 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700245 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700246 auto sensorBase =
247 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 if (sensorBase != sensorData->end())
249 {
250 baseConfiguration = &(*sensorBase);
251 break;
252 }
253 }
254 if (baseConfiguration == nullptr)
255 {
256 std::cerr << "error finding base configuration for" << hwmonName
257 << "\n";
258 continue;
259 }
260 auto configurationBus = baseConfiguration->second.find("Bus");
261 auto configurationAddress =
262 baseConfiguration->second.find("Address");
263
264 if (configurationBus == baseConfiguration->second.end() ||
265 configurationAddress == baseConfiguration->second.end())
266 {
267 std::cerr << "error finding bus or address in configuration";
268 continue;
269 }
270
James Feist3eb82622019-02-08 13:10:22 -0800271 if (std::get<uint64_t>(configurationBus->second) != bus ||
272 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700273 {
274 continue;
275 }
276
Zev Weiss8908b3c2022-08-12 18:21:01 -0700277 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700278 break;
279 }
280 if (interfacePath == nullptr)
281 {
282 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700283 continue;
284 }
285
286 auto findCpuId = baseConfiguration->second.find("CpuID");
287 if (findCpuId == baseConfiguration->second.end())
288 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700289 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700290 continue;
291 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800293 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700294
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700296 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200297 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200298 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700299 {
300 std::cerr << "No temperature sensors in system\n";
301 continue;
302 }
303
304 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800305 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700306 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200307 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200308 if (!fileParts)
309 {
310 continue;
311 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200312 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700313 auto inputPathStr = inputPath.string();
314 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200315 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700316 std::ifstream labelFile(labelPath);
317 if (!labelFile.good())
318 {
319 std::cerr << "Failure reading " << labelPath << "\n";
320 continue;
321 }
322 std::string label;
323 std::getline(labelFile, label);
324 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800325
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200326 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700327
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800328 auto findSensor = gCpuSensors.find(sensorName);
329 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700331 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700332 {
333 std::cout << "Skipped: " << inputPath << ": " << sensorName
334 << " is already created\n";
335 }
336 continue;
337 }
338
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800339 // check hidden properties
340 bool show = true;
341 for (const char* prop : hiddenProps)
342 {
343 if (label == prop)
344 {
345 show = false;
346 break;
347 }
348 }
349
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700350 /*
351 * Find if there is DtsCritOffset is configured in config file
352 * set it if configured or else set it to 0
353 */
354 double dtsOffset = 0;
355 if (label == "DTS")
356 {
357 auto findThrOffset =
358 baseConfiguration->second.find("DtsCritOffset");
359 if (findThrOffset != baseConfiguration->second.end())
360 {
361 dtsOffset = std::visit(VariantToDoubleVisitor(),
362 findThrOffset->second);
363 }
364 }
365
James Feist6714a252018-09-10 15:26:18 -0700366 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700367 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700368 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700369 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800370 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700371 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700372 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700373 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700374 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700375 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700376 std::cerr << "error populating thresholds for "
377 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700378 }
379 }
James Feistc140e202019-11-14 15:23:51 -0800380 auto& sensorPtr = gCpuSensors[sensorName];
381 // make sure destructor fires before creating a new one
382 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700383 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700384 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800385 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700386 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530387 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700388 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700389 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 {
James Feist6714a252018-09-10 15:26:18 -0700391 std::cout << "Mapped: " << inputPath << " to " << sensorName
392 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700393 }
James Feist6714a252018-09-10 15:26:18 -0700394 }
395 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700396
Ed Tanous2049bd22022-07-09 07:20:26 -0700397 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700398 {
399 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
400 << " created\n";
401 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700402
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700403 return true;
James Feist6714a252018-09-10 15:26:18 -0700404}
405
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700407{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700408 std::ostringstream hex;
409 hex << std::hex << config.addr;
410 const std::string& addrHexStr = hex.str();
411 std::string busStr = std::to_string(config.bus);
412
413 std::string parameters = "peci-client 0x" + addrHexStr;
414 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
415
James Feistcf3bce62019-01-08 10:07:19 -0800416 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700417 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800418 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700419 {
James Feistcf3bce62019-01-08 10:07:19 -0800420 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700421 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700422 continue;
James Feist6714a252018-09-10 15:26:18 -0700423 }
424
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700425 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700426 if (directoryName.starts_with(busStr) &&
427 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700428 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700429 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700430 {
431 std::cout << parameters << " on bus " << busStr
432 << " is already exported\n";
433 }
434 return;
435 }
James Feist6714a252018-09-10 15:26:18 -0700436 }
437
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700438 std::ofstream deviceFile(device);
439 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700440 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700441 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700442 return;
443 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700444 deviceFile << parameters;
445 deviceFile.close();
446
447 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700448}
449
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700450void detectCpu(boost::asio::steady_timer& pingTimer,
451 boost::asio::steady_timer& creationTimer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800452 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(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700558 std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700559 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(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700582 boost::asio::steady_timer& pingTimer,
583 boost::asio::steady_timer& creationTimer, boost::asio::io_service& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700584 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{
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700589 pingTimer.expires_from_now(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700590 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 Weiss054aad82022-08-18 01:37:34 -0700626 if (intf != configInterfaceName(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
Ed Tanous14ed5e92022-07-12 15:50:23 -0700712 sdbusplus::asio::object_server objectServer(systemBus, true);
713 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700714 boost::asio::steady_timer pingTimer(io);
715 boost::asio::steady_timer creationTimer(io);
716 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800717 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700718
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700719 filterTimer.expires_from_now(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700720 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
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700747 filterTimer.expires_from_now(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700748 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}