blob: 1c5574bdc10d283857bc4b62e100f7ff382609fa [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <CPUSensor.hpp>
20#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
46#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080047#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
48#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
49#endif
James Feistf87dc4c2018-12-05 14:39:51 -080050// clang-format on
51
Ed Tanous8a57ec02020-10-09 12:46:52 -070052static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070053
Arun P. Mohanan04d05062021-10-29 20:30:26 +053054boost::container::flat_map<std::string, std::shared_ptr<CPUSensor>> gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080055boost::container::flat_map<std::string,
56 std::shared_ptr<sdbusplus::asio::dbus_interface>>
57 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080058
James Feist6714a252018-09-10 15:26:18 -070059enum State
60{
61 OFF, // host powered down
62 ON, // host powered on
63 READY // host powered on and mem test passed - fully ready
64};
65
66struct CPUConfig
67{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070068 CPUConfig(const uint64_t& bus, const uint64_t& addr,
69 const std::string& name, const State& state) :
70 bus(bus),
71 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070072 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070073 int bus;
James Feist6714a252018-09-10 15:26:18 -070074 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 std::string name;
James Feist6714a252018-09-10 15:26:18 -070076 State state;
77
78 bool operator<(const CPUConfig& rhs) const
79 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093080 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070081 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070082 }
83};
84
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070085static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070087
James Feistcf3bce62019-01-08 10:07:19 -080088namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080089
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070091 "xyz.openbmc_project.Configuration.";
Brandon Kim66558232021-11-09 16:53:08 -080092static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
93static constexpr auto hiddenProps{std::to_array<const char*>(
94 {CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070095
Jae Hyun Yood64262b2018-11-01 13:31:16 -070096void detectCpuAsync(
97 boost::asio::deadline_timer& pingTimer,
98 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
99 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;
108 if (item.compare("input") != 0)
109 {
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) {
117 if (std::isspace(c))
118 {
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 });
131 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
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700240 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800241 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700242 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700244 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700246 sensorType = configPrefix + std::string(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
277 interfacePath = &(sensor.first.str);
278 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,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700373 CPUSensor::sensorScaleFactor,
374 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;
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530383 sensorPtr = std::make_shared<CPUSensor>(
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
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700397 if (createdSensors.size())
398 {
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();
426 if (boost::starts_with(directoryName, busStr) &&
427 boost::ends_with(directoryName, addrHexStr))
428 {
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);
464 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
465 if (file < 0)
466 {
467 std::cerr << "unable to open " << peciDevPath << "\n";
468 std::exit(EXIT_FAILURE);
469 }
470
Ed Tanousa771f6a2022-01-14 09:36:51 -0800471 State newState = State::OFF;
472 struct peci_ping_msg msg
473 {};
James Feist6714a252018-09-10 15:26:18 -0700474 msg.addr = config.addr;
475 if (!ioctl(file, PECI_IOC_PING, &msg))
476 {
477 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700478 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700479 {
Ed Tanousa771f6a2022-01-14 09:36:51 -0800480 struct peci_rd_pkg_cfg_msg msg
481 {};
James Feist6714a252018-09-10 15:26:18 -0700482 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800483 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700484 msg.param = rank;
485 msg.rx_len = 4;
486 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
487 {
488 if (msg.pkg_config[0] || msg.pkg_config[1] ||
489 msg.pkg_config[2])
490 {
491 dimmReady = true;
492 break;
493 }
494 }
495 else
496 {
497 break;
498 }
499 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700500
James Feist6714a252018-09-10 15:26:18 -0700501 if (dimmReady)
502 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700503 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700504 }
505 else
506 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700507 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700508 }
509 }
James Feist6714a252018-09-10 15:26:18 -0700510
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700511 close(file);
512
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700513 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700514 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700515 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700516 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700517 if (config.state == State::OFF)
518 {
519 std::cout << config.name << " is detected\n";
520 exportDevice(config);
521 }
James Feist6714a252018-09-10 15:26:18 -0700522
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700523 if (newState == State::ON)
524 {
525 rescanDelaySeconds = 3;
526 }
527 else if (newState == State::READY)
528 {
529 rescanDelaySeconds = 5;
530 std::cout << "DIMM(s) on " << config.name
531 << " is/are detected\n";
532 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700533 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700534
535 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700536 }
537
538 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700539 {
540 keepPinging = true;
541 }
542
Ed Tanous8a57ec02020-10-09 12:46:52 -0700543 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700544 {
545 std::cout << config.name << ", state: " << config.state << "\n";
546 }
James Feist6714a252018-09-10 15:26:18 -0700547 }
548
James Feist6714a252018-09-10 15:26:18 -0700549 if (rescanDelaySeconds)
550 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700551 creationTimer.expires_from_now(
552 boost::posix_time::seconds(rescanDelaySeconds));
553 creationTimer.async_wait([&](const boost::system::error_code& ec) {
554 if (ec == boost::asio::error::operation_aborted)
555 {
556 return; // we're being canceled
557 }
558
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800559 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700560 sensorConfigs) ||
561 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700562 {
563 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800564 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700565 }
566 });
James Feist6714a252018-09-10 15:26:18 -0700567 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700568 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700569 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800570 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800571 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700572 }
573}
574
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700575void detectCpuAsync(
576 boost::asio::deadline_timer& pingTimer,
577 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
578 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800579 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800580 boost::container::flat_set<CPUConfig>& cpuConfigs,
581 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700582{
583 pingTimer.expires_from_now(boost::posix_time::seconds(1));
584 pingTimer.async_wait([&](const boost::system::error_code& ec) {
585 if (ec == boost::asio::error::operation_aborted)
586 {
587 return; // we're being canceled
588 }
589
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800590 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800591 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700592 });
593}
594
James Feistc140e202019-11-14 15:23:51 -0800595bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
596 boost::container::flat_set<CPUConfig>& cpuConfigs,
597 ManagedObjectType& sensorConfigs,
598 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700599{
James Feist6714a252018-09-10 15:26:18 -0700600 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800601 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700602 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700603 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700604 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700605 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800606 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700607 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700608 return false;
James Feist6714a252018-09-10 15:26:18 -0700609 }
610 useCache = true;
611 }
612
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700613 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700614 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700615 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700616 {
617 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800618 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700619 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700620 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700621 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700622 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700623 {
624 continue;
625 }
626
James Feist6714a252018-09-10 15:26:18 -0700627 auto findName = config.second.find("Name");
628 if (findName == config.second.end())
629 {
630 continue;
631 }
James Feist3eb82622019-02-08 13:10:22 -0800632 std::string nameRaw =
633 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700634 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700635 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700636
James Feistc140e202019-11-14 15:23:51 -0800637 auto present = std::optional<bool>();
638 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700639 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700640 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700641 if (suppConfig.first.find("PresenceGpio") !=
642 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700643 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700644 present = cpuIsPresent(suppConfig.second);
645 break;
James Feist58295ad2019-05-30 15:01:41 -0700646 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700647 }
648
James Feistc140e202019-11-14 15:23:51 -0800649 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
650 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700651 {
652 auto iface = objectServer.add_interface(
653 cpuInventoryPath + std::string("/") + name,
654 "xyz.openbmc_project.Inventory.Item");
655 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800656 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700657 iface->initialize();
658 inventoryIfaces[name] = std::move(iface);
659 }
James Feist58295ad2019-05-30 15:01:41 -0700660
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700661 auto findBus = config.second.find("Bus");
662 if (findBus == config.second.end())
663 {
664 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
665 continue;
666 }
James Feist3eb82622019-02-08 13:10:22 -0800667 uint64_t bus =
668 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700669
670 auto findAddress = config.second.find("Address");
671 if (findAddress == config.second.end())
672 {
673 std::cerr << "Can't find 'Address' setting in " << name
674 << "\n";
675 continue;
676 }
James Feist3eb82622019-02-08 13:10:22 -0800677 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
678 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700679
Ed Tanous8a57ec02020-10-09 12:46:52 -0700680 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700681 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700682 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700683 std::cout << "addr: " << addr << "\n";
684 std::cout << "name: " << name << "\n";
685 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700686 }
687
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800688 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700689 }
690 }
691 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700692
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800693 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700694 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800695 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700696 << " parsed\n";
697 return true;
698 }
699
700 return false;
James Feist6714a252018-09-10 15:26:18 -0700701}
702
James Feistb6c0b912019-07-09 12:21:44 -0700703int main()
James Feist6714a252018-09-10 15:26:18 -0700704{
705 boost::asio::io_service io;
706 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800707 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700708
James Feist6714a252018-09-10 15:26:18 -0700709 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700710 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
711 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700712 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700713 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800714 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700715
716 filterTimer.expires_from_now(boost::posix_time::seconds(1));
717 filterTimer.async_wait([&](const boost::system::error_code& ec) {
718 if (ec == boost::asio::error::operation_aborted)
719 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700720 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700721 }
722
James Feistc140e202019-11-14 15:23:51 -0800723 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700724 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800725 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800726 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700727 }
728 });
729
James Feist6714a252018-09-10 15:26:18 -0700730 std::function<void(sdbusplus::message::message&)> eventHandler =
731 [&](sdbusplus::message::message& message) {
732 if (message.is_method_error())
733 {
734 std::cerr << "callback method error\n";
735 return;
736 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700737
Ed Tanous8a57ec02020-10-09 12:46:52 -0700738 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700739 {
740 std::cout << message.get_path() << " is changed\n";
741 }
742
James Feist6714a252018-09-10 15:26:18 -0700743 // this implicitly cancels the timer
744 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700745 filterTimer.async_wait([&](const boost::system::error_code& ec) {
746 if (ec == boost::asio::error::operation_aborted)
747 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700748 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700749 }
750
James Feist58295ad2019-05-30 15:01:41 -0700751 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
James Feistc140e202019-11-14 15:23:51 -0800752 objectServer))
James Feist6714a252018-09-10 15:26:18 -0700753 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700754 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800755 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700756 }
757 });
758 };
759
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700760 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700761 {
762 auto match = std::make_unique<sdbusplus::bus::match::match>(
763 static_cast<sdbusplus::bus::bus&>(*systemBus),
764 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700765 std::string(inventoryPath) + "',arg0namespace='" +
766 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700767 eventHandler);
768 matches.emplace_back(std::move(match));
769 }
770
Jae Hyun Yoo59e47982020-01-15 10:33:13 -0800771 systemBus->request_name("xyz.openbmc_project.CPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800772
773 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700774 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700775 return 0;
James Feist6714a252018-09-10 15:26:18 -0700776}