blob: a35273521ace6dd307d7dcb9b9f4da7a601587fb [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;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700171 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
172 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
Jeff Lin62050922021-02-22 13:19:16 +0800173 hwmonNamePaths, 6))
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
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700241 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800242 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700243 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700244 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700245 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 auto sensorBase = sensorData->find(sensorType);
249 if (sensorBase != sensorData->end())
250 {
251 baseConfiguration = &(*sensorBase);
252 break;
253 }
254 }
255 if (baseConfiguration == nullptr)
256 {
257 std::cerr << "error finding base configuration for" << hwmonName
258 << "\n";
259 continue;
260 }
261 auto configurationBus = baseConfiguration->second.find("Bus");
262 auto configurationAddress =
263 baseConfiguration->second.find("Address");
264
265 if (configurationBus == baseConfiguration->second.end() ||
266 configurationAddress == baseConfiguration->second.end())
267 {
268 std::cerr << "error finding bus or address in configuration";
269 continue;
270 }
271
James Feist3eb82622019-02-08 13:10:22 -0800272 if (std::get<uint64_t>(configurationBus->second) != bus ||
273 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700274 {
275 continue;
276 }
277
278 interfacePath = &(sensor.first.str);
279 break;
280 }
281 if (interfacePath == nullptr)
282 {
283 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700284 continue;
285 }
286
287 auto findCpuId = baseConfiguration->second.find("CpuID");
288 if (findCpuId == baseConfiguration->second.end())
289 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700290 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700291 continue;
292 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700293 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800294 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700295
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700296 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700297 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200298 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200299 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700300 {
301 std::cerr << "No temperature sensors in system\n";
302 continue;
303 }
304
305 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800306 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700307 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200308 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200309 if (!fileParts)
310 {
311 continue;
312 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200313 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700314 auto inputPathStr = inputPath.string();
315 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200316 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700317 std::ifstream labelFile(labelPath);
318 if (!labelFile.good())
319 {
320 std::cerr << "Failure reading " << labelPath << "\n";
321 continue;
322 }
323 std::string label;
324 std::getline(labelFile, label);
325 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800326
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200327 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700328
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800329 auto findSensor = gCpuSensors.find(sensorName);
330 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700332 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 {
334 std::cout << "Skipped: " << inputPath << ": " << sensorName
335 << " is already created\n";
336 }
337 continue;
338 }
339
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800340 // check hidden properties
341 bool show = true;
342 for (const char* prop : hiddenProps)
343 {
344 if (label == prop)
345 {
346 show = false;
347 break;
348 }
349 }
350
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700351 /*
352 * Find if there is DtsCritOffset is configured in config file
353 * set it if configured or else set it to 0
354 */
355 double dtsOffset = 0;
356 if (label == "DTS")
357 {
358 auto findThrOffset =
359 baseConfiguration->second.find("DtsCritOffset");
360 if (findThrOffset != baseConfiguration->second.end())
361 {
362 dtsOffset = std::visit(VariantToDoubleVisitor(),
363 findThrOffset->second);
364 }
365 }
366
James Feist6714a252018-09-10 15:26:18 -0700367 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700368 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700369 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700370 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800371 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700372 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700373 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700374 CPUSensor::sensorScaleFactor,
375 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700376 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700377 std::cerr << "error populating thresholds for "
378 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700379 }
380 }
James Feistc140e202019-11-14 15:23:51 -0800381 auto& sensorPtr = gCpuSensors[sensorName];
382 // make sure destructor fires before creating a new one
383 sensorPtr = nullptr;
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530384 sensorPtr = std::make_shared<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700385 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800386 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700387 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530388 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700389 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700390 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700391 {
James Feist6714a252018-09-10 15:26:18 -0700392 std::cout << "Mapped: " << inputPath << " to " << sensorName
393 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700394 }
James Feist6714a252018-09-10 15:26:18 -0700395 }
396 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700397
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700398 if (createdSensors.size())
399 {
400 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
401 << " created\n";
402 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700403
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700404 return true;
James Feist6714a252018-09-10 15:26:18 -0700405}
406
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700407void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700408{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700409 std::ostringstream hex;
410 hex << std::hex << config.addr;
411 const std::string& addrHexStr = hex.str();
412 std::string busStr = std::to_string(config.bus);
413
414 std::string parameters = "peci-client 0x" + addrHexStr;
415 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
416
James Feistcf3bce62019-01-08 10:07:19 -0800417 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700418 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800419 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700420 {
James Feistcf3bce62019-01-08 10:07:19 -0800421 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700422 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700423 continue;
James Feist6714a252018-09-10 15:26:18 -0700424 }
425
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700426 const std::string& directoryName = path.path().filename();
427 if (boost::starts_with(directoryName, busStr) &&
428 boost::ends_with(directoryName, addrHexStr))
429 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700430 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700431 {
432 std::cout << parameters << " on bus " << busStr
433 << " is already exported\n";
434 }
435 return;
436 }
James Feist6714a252018-09-10 15:26:18 -0700437 }
438
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700439 std::ofstream deviceFile(device);
440 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700441 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700442 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700443 return;
444 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700445 deviceFile << parameters;
446 deviceFile.close();
447
448 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700449}
450
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800451void detectCpu(boost::asio::deadline_timer& pingTimer,
452 boost::asio::deadline_timer& creationTimer,
453 boost::asio::io_service& io,
454 sdbusplus::asio::object_server& objectServer,
455 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
456 boost::container::flat_set<CPUConfig>& cpuConfigs,
457 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700458{
James Feist6714a252018-09-10 15:26:18 -0700459 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700460 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700461
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800462 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700463 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700464 std::string peciDevPath = peciDev + std::to_string(config.bus);
465 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
466 if (file < 0)
467 {
468 std::cerr << "unable to open " << peciDevPath << "\n";
469 std::exit(EXIT_FAILURE);
470 }
471
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700472 State newState;
James Feist6714a252018-09-10 15:26:18 -0700473 struct peci_ping_msg msg;
474 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 {
480 struct peci_rd_pkg_cfg_msg msg;
481 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800482 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700483 msg.param = rank;
484 msg.rx_len = 4;
485 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
486 {
487 if (msg.pkg_config[0] || msg.pkg_config[1] ||
488 msg.pkg_config[2])
489 {
490 dimmReady = true;
491 break;
492 }
493 }
494 else
495 {
496 break;
497 }
498 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700499
James Feist6714a252018-09-10 15:26:18 -0700500 if (dimmReady)
501 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700502 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700503 }
504 else
505 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700506 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700507 }
508 }
509 else
510 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700511 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700512 }
513
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700514 close(file);
515
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700516 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700517 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700518 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700519 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700520 if (config.state == State::OFF)
521 {
522 std::cout << config.name << " is detected\n";
523 exportDevice(config);
524 }
James Feist6714a252018-09-10 15:26:18 -0700525
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700526 if (newState == State::ON)
527 {
528 rescanDelaySeconds = 3;
529 }
530 else if (newState == State::READY)
531 {
532 rescanDelaySeconds = 5;
533 std::cout << "DIMM(s) on " << config.name
534 << " is/are detected\n";
535 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700536 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700537
538 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700539 }
540
541 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700542 {
543 keepPinging = true;
544 }
545
Ed Tanous8a57ec02020-10-09 12:46:52 -0700546 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700547 {
548 std::cout << config.name << ", state: " << config.state << "\n";
549 }
James Feist6714a252018-09-10 15:26:18 -0700550 }
551
James Feist6714a252018-09-10 15:26:18 -0700552 if (rescanDelaySeconds)
553 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700554 creationTimer.expires_from_now(
555 boost::posix_time::seconds(rescanDelaySeconds));
556 creationTimer.async_wait([&](const boost::system::error_code& ec) {
557 if (ec == boost::asio::error::operation_aborted)
558 {
559 return; // we're being canceled
560 }
561
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800562 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700563 sensorConfigs) ||
564 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700565 {
566 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800567 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700568 }
569 });
James Feist6714a252018-09-10 15:26:18 -0700570 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700571 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700572 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800573 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800574 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700575 }
576}
577
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700578void detectCpuAsync(
579 boost::asio::deadline_timer& pingTimer,
580 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
581 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800582 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800583 boost::container::flat_set<CPUConfig>& cpuConfigs,
584 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700585{
586 pingTimer.expires_from_now(boost::posix_time::seconds(1));
587 pingTimer.async_wait([&](const boost::system::error_code& ec) {
588 if (ec == boost::asio::error::operation_aborted)
589 {
590 return; // we're being canceled
591 }
592
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800593 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800594 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700595 });
596}
597
James Feistc140e202019-11-14 15:23:51 -0800598bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
599 boost::container::flat_set<CPUConfig>& cpuConfigs,
600 ManagedObjectType& sensorConfigs,
601 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700602{
James Feist6714a252018-09-10 15:26:18 -0700603 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800604 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700605 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700606 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700607 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700608 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800609 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700610 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700611 return false;
James Feist6714a252018-09-10 15:26:18 -0700612 }
613 useCache = true;
614 }
615
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700616 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700617 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700618 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700619 {
620 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800621 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700622 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700623 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700624 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700625 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700626 {
627 continue;
628 }
629
James Feist6714a252018-09-10 15:26:18 -0700630 auto findName = config.second.find("Name");
631 if (findName == config.second.end())
632 {
633 continue;
634 }
James Feist3eb82622019-02-08 13:10:22 -0800635 std::string nameRaw =
636 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700637 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700638 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700639
James Feistc140e202019-11-14 15:23:51 -0800640 auto present = std::optional<bool>();
641 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700642 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700643 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700644 if (suppConfig.first.find("PresenceGpio") !=
645 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700646 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700647 present = cpuIsPresent(suppConfig.second);
648 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
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700664 auto findBus = config.second.find("Bus");
665 if (findBus == config.second.end())
666 {
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
673 auto findAddress = config.second.find("Address");
674 if (findAddress == config.second.end())
675 {
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
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800696 if (cpuConfigs.size())
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 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
714 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700715 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700716 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800717 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700718
719 filterTimer.expires_from_now(boost::posix_time::seconds(1));
720 filterTimer.async_wait([&](const boost::system::error_code& ec) {
721 if (ec == boost::asio::error::operation_aborted)
722 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700723 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700724 }
725
James Feistc140e202019-11-14 15:23:51 -0800726 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700727 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800728 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800729 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700730 }
731 });
732
James Feist6714a252018-09-10 15:26:18 -0700733 std::function<void(sdbusplus::message::message&)> eventHandler =
734 [&](sdbusplus::message::message& message) {
735 if (message.is_method_error())
736 {
737 std::cerr << "callback method error\n";
738 return;
739 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700740
Ed Tanous8a57ec02020-10-09 12:46:52 -0700741 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700742 {
743 std::cout << message.get_path() << " is changed\n";
744 }
745
James Feist6714a252018-09-10 15:26:18 -0700746 // this implicitly cancels the timer
747 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700748 filterTimer.async_wait([&](const boost::system::error_code& ec) {
749 if (ec == boost::asio::error::operation_aborted)
750 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700751 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700752 }
753
James Feist58295ad2019-05-30 15:01:41 -0700754 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
James Feistc140e202019-11-14 15:23:51 -0800755 objectServer))
James Feist6714a252018-09-10 15:26:18 -0700756 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700757 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800758 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700759 }
760 });
761 };
762
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700763 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700764 {
765 auto match = std::make_unique<sdbusplus::bus::match::match>(
766 static_cast<sdbusplus::bus::bus&>(*systemBus),
767 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700768 std::string(inventoryPath) + "',arg0namespace='" +
769 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700770 eventHandler);
771 matches.emplace_back(std::move(match));
772 }
773
Jae Hyun Yoo59e47982020-01-15 10:33:13 -0800774 systemBus->request_name("xyz.openbmc_project.CPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800775
776 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700777 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700778 return 0;
James Feist6714a252018-09-10 15:26:18 -0700779}