blob: 42336c6254cd96941ca3e9a22d16f8229d1bb75e [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "CPUSensor.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/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/container/flat_set.hpp>
27#include <boost/date_time/posix_time/posix_time.hpp>
28#include <boost/process/child.hpp>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31#include <sdbusplus/bus/match.hpp>
32
33#include <array>
James Feist24f02f22019-04-15 11:05:39 -070034#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070035#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <functional>
37#include <memory>
James Feist6714a252018-09-10 15:26:18 -070038#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <sstream>
40#include <stdexcept>
41#include <string>
42#include <utility>
43#include <variant>
44#include <vector>
James Feist6714a252018-09-10 15:26:18 -070045
James Feistf87dc4c2018-12-05 14:39:51 -080046// clang-format off
47// this needs to be included last or we'll have build issues
48#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080049#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
50#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
51#endif
James Feistf87dc4c2018-12-05 14:39:51 -080052// clang-format on
53
James Feist6714a252018-09-10 15:26:18 -070054static constexpr bool DEBUG = false;
55
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080056boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> 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 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070082 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070083 }
84};
85
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070086static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070088
James Feistcf3bce62019-01-08 10:07:19 -080089namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080090
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070092 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo7d47bf52019-04-23 16:43:50 -070093static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080094static constexpr std::array<const char*, 3> hiddenProps = {
95 CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070096
Jae Hyun Yood64262b2018-11-01 13:31:16 -070097void detectCpuAsync(
98 boost::asio::deadline_timer& pingTimer,
99 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
100 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800102 boost::container::flat_set<CPUConfig>& cpuConfigs,
103 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700104
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800105bool createSensors(boost::asio::io_service& io,
106 sdbusplus::asio::object_server& objectServer,
107 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
108 boost::container::flat_set<CPUConfig>& cpuConfigs,
109 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700110{
111 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700113 {
114 if (cpu.state != State::OFF)
115 {
116 available = true;
James Feistc140e202019-11-14 15:23:51 -0800117 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
118 inventoryIfaces[cpu.name];
119 if (iface != nullptr)
120 {
121 continue;
122 }
123 iface = objectServer.add_interface(
124 cpuInventoryPath + std::string("/") + cpu.name,
125 "xyz.openbmc_project.Inventory.Item");
126 iface->register_property("PrettyName", cpu.name);
127 iface->register_property("Present", true);
128 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700129 }
130 }
131 if (!available)
132 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700133 return false;
James Feist6714a252018-09-10 15:26:18 -0700134 }
135
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800136 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700137 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800138 return false;
James Feist6714a252018-09-10 15:26:18 -0700139 }
140
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700141 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
143 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
144 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700145 {
146 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700147 return true;
James Feist6714a252018-09-10 15:26:18 -0700148 }
149
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700150 boost::container::flat_set<std::string> scannedDirectories;
151 boost::container::flat_set<std::string> createdSensors;
152
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800153 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700154 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700155 const std::string& pathStr = hwmonNamePath.string();
156 auto hwmonDirectory = hwmonNamePath.parent_path();
157
158 auto ret = scannedDirectories.insert(hwmonDirectory.string());
159 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700160 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700161 continue; // already searched this path
162 }
163
164 fs::path::iterator it = hwmonNamePath.begin();
165 std::advance(it, 6); // pick the 6th part for a PECI client device name
166 std::string deviceName = *it;
167 auto findHyphen = deviceName.find("-");
168 if (findHyphen == std::string::npos)
169 {
170 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700171 continue;
172 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700173 std::string busStr = deviceName.substr(0, findHyphen);
174 std::string addrStr = deviceName.substr(findHyphen + 1);
175
176 size_t bus = 0;
177 size_t addr = 0;
178 try
179 {
180 bus = std::stoi(busStr);
181 addr = std::stoi(addrStr, 0, 16);
182 }
James Feistb6c0b912019-07-09 12:21:44 -0700183 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700184 {
185 continue;
186 }
187
188 std::ifstream nameFile(hwmonNamePath);
189 if (!nameFile.good())
190 {
191 std::cerr << "Failure reading " << hwmonNamePath << "\n";
192 continue;
193 }
194 std::string hwmonName;
195 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700196 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800197 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700198 {
199 // shouldn't have an empty name file
200 continue;
201 }
James Feist6714a252018-09-10 15:26:18 -0700202 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 {
204 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
205 << "\n";
206 }
James Feist6714a252018-09-10 15:26:18 -0700207
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700208 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700209 const SensorData* sensorData = nullptr;
210 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700211 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700212
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800214 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700215 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700216 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700217 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700218 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700219 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700220 auto sensorBase = sensorData->find(sensorType);
221 if (sensorBase != sensorData->end())
222 {
223 baseConfiguration = &(*sensorBase);
224 break;
225 }
226 }
227 if (baseConfiguration == nullptr)
228 {
229 std::cerr << "error finding base configuration for" << hwmonName
230 << "\n";
231 continue;
232 }
233 auto configurationBus = baseConfiguration->second.find("Bus");
234 auto configurationAddress =
235 baseConfiguration->second.find("Address");
236
237 if (configurationBus == baseConfiguration->second.end() ||
238 configurationAddress == baseConfiguration->second.end())
239 {
240 std::cerr << "error finding bus or address in configuration";
241 continue;
242 }
243
James Feist3eb82622019-02-08 13:10:22 -0800244 if (std::get<uint64_t>(configurationBus->second) != bus ||
245 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 {
247 continue;
248 }
249
250 interfacePath = &(sensor.first.str);
251 break;
252 }
253 if (interfacePath == nullptr)
254 {
255 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700256 continue;
257 }
258
259 auto findCpuId = baseConfiguration->second.find("CpuID");
260 if (findCpuId == baseConfiguration->second.end())
261 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700262 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700263 continue;
264 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700265 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800266 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700267
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700268 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700269 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200270 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200271 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700272 {
273 std::cerr << "No temperature sensors in system\n";
274 continue;
275 }
276
277 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800278 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700279 {
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200280 auto fileParts = thresholds::splitFileName(inputPath);
281 if (!fileParts)
282 {
283 continue;
284 }
285 auto [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700286 auto inputPathStr = inputPath.string();
287 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200288 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700289 std::ifstream labelFile(labelPath);
290 if (!labelFile.good())
291 {
292 std::cerr << "Failure reading " << labelPath << "\n";
293 continue;
294 }
295 std::string label;
296 std::getline(labelFile, label);
297 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800298
James Feist6714a252018-09-10 15:26:18 -0700299 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700300
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800301 auto findSensor = gCpuSensors.find(sensorName);
302 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700303 {
304 if (DEBUG)
305 {
306 std::cout << "Skipped: " << inputPath << ": " << sensorName
307 << " is already created\n";
308 }
309 continue;
310 }
311
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800312 // check hidden properties
313 bool show = true;
314 for (const char* prop : hiddenProps)
315 {
316 if (label == prop)
317 {
318 show = false;
319 break;
320 }
321 }
322
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700323 /*
324 * Find if there is DtsCritOffset is configured in config file
325 * set it if configured or else set it to 0
326 */
327 double dtsOffset = 0;
328 if (label == "DTS")
329 {
330 auto findThrOffset =
331 baseConfiguration->second.find("DtsCritOffset");
332 if (findThrOffset != baseConfiguration->second.end())
333 {
334 dtsOffset = std::visit(VariantToDoubleVisitor(),
335 findThrOffset->second);
336 }
337 }
338
James Feist6714a252018-09-10 15:26:18 -0700339 std::vector<thresholds::Threshold> sensorThresholds;
340 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700341 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700342 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800343 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700344 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700345 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700346 CPUSensor::sensorScaleFactor,
347 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700348 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700349 std::cerr << "error populating thresholds for "
350 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700351 }
352 }
James Feistc140e202019-11-14 15:23:51 -0800353 auto& sensorPtr = gCpuSensors[sensorName];
354 // make sure destructor fires before creating a new one
355 sensorPtr = nullptr;
356 sensorPtr = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700357 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800358 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700359 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700360 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700361 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700362 {
James Feist6714a252018-09-10 15:26:18 -0700363 std::cout << "Mapped: " << inputPath << " to " << sensorName
364 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700365 }
James Feist6714a252018-09-10 15:26:18 -0700366 }
367 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700368
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700369 if (createdSensors.size())
370 {
371 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
372 << " created\n";
373 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700374
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700375 return true;
James Feist6714a252018-09-10 15:26:18 -0700376}
377
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700378void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700379{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700380 std::ostringstream hex;
381 hex << std::hex << config.addr;
382 const std::string& addrHexStr = hex.str();
383 std::string busStr = std::to_string(config.bus);
384
385 std::string parameters = "peci-client 0x" + addrHexStr;
386 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
387
James Feistcf3bce62019-01-08 10:07:19 -0800388 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700389 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800390 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700391 {
James Feistcf3bce62019-01-08 10:07:19 -0800392 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700393 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700394 continue;
James Feist6714a252018-09-10 15:26:18 -0700395 }
396
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700397 const std::string& directoryName = path.path().filename();
398 if (boost::starts_with(directoryName, busStr) &&
399 boost::ends_with(directoryName, addrHexStr))
400 {
401 if (DEBUG)
402 {
403 std::cout << parameters << " on bus " << busStr
404 << " is already exported\n";
405 }
406 return;
407 }
James Feist6714a252018-09-10 15:26:18 -0700408 }
409
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700410 std::ofstream deviceFile(device);
411 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700412 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700413 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700414 return;
415 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700416 deviceFile << parameters;
417 deviceFile.close();
418
419 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700420}
421
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800422void detectCpu(boost::asio::deadline_timer& pingTimer,
423 boost::asio::deadline_timer& creationTimer,
424 boost::asio::io_service& io,
425 sdbusplus::asio::object_server& objectServer,
426 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
427 boost::container::flat_set<CPUConfig>& cpuConfigs,
428 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700429{
James Feist6714a252018-09-10 15:26:18 -0700430 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700431 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700432
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800433 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700434 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700435 std::string peciDevPath = peciDev + std::to_string(config.bus);
436 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
437 if (file < 0)
438 {
439 std::cerr << "unable to open " << peciDevPath << "\n";
440 std::exit(EXIT_FAILURE);
441 }
442
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700443 State newState;
James Feist6714a252018-09-10 15:26:18 -0700444 struct peci_ping_msg msg;
445 msg.addr = config.addr;
446 if (!ioctl(file, PECI_IOC_PING, &msg))
447 {
448 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700449 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700450 {
451 struct peci_rd_pkg_cfg_msg msg;
452 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800453 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700454 msg.param = rank;
455 msg.rx_len = 4;
456 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
457 {
458 if (msg.pkg_config[0] || msg.pkg_config[1] ||
459 msg.pkg_config[2])
460 {
461 dimmReady = true;
462 break;
463 }
464 }
465 else
466 {
467 break;
468 }
469 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700470
James Feist6714a252018-09-10 15:26:18 -0700471 if (dimmReady)
472 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700473 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700474 }
475 else
476 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700477 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700478 }
479 }
480 else
481 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700482 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700483 }
484
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700485 close(file);
486
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700487 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700488 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700489 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700490 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700491 if (config.state == State::OFF)
492 {
493 std::cout << config.name << " is detected\n";
494 exportDevice(config);
495 }
James Feist6714a252018-09-10 15:26:18 -0700496
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700497 if (newState == State::ON)
498 {
499 rescanDelaySeconds = 3;
500 }
501 else if (newState == State::READY)
502 {
503 rescanDelaySeconds = 5;
504 std::cout << "DIMM(s) on " << config.name
505 << " is/are detected\n";
506 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700507 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700508
509 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700510 }
511
512 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700513 {
514 keepPinging = true;
515 }
516
517 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700518 {
519 std::cout << config.name << ", state: " << config.state << "\n";
520 }
James Feist6714a252018-09-10 15:26:18 -0700521 }
522
James Feist6714a252018-09-10 15:26:18 -0700523 if (rescanDelaySeconds)
524 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700525 creationTimer.expires_from_now(
526 boost::posix_time::seconds(rescanDelaySeconds));
527 creationTimer.async_wait([&](const boost::system::error_code& ec) {
528 if (ec == boost::asio::error::operation_aborted)
529 {
530 return; // we're being canceled
531 }
532
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800533 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700534 sensorConfigs) ||
535 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700536 {
537 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800538 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700539 }
540 });
James Feist6714a252018-09-10 15:26:18 -0700541 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700542 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700543 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800544 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800545 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700546 }
547}
548
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700549void detectCpuAsync(
550 boost::asio::deadline_timer& pingTimer,
551 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
552 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800553 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800554 boost::container::flat_set<CPUConfig>& cpuConfigs,
555 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700556{
557 pingTimer.expires_from_now(boost::posix_time::seconds(1));
558 pingTimer.async_wait([&](const boost::system::error_code& ec) {
559 if (ec == boost::asio::error::operation_aborted)
560 {
561 return; // we're being canceled
562 }
563
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800564 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800565 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700566 });
567}
568
James Feistc140e202019-11-14 15:23:51 -0800569bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
570 boost::container::flat_set<CPUConfig>& cpuConfigs,
571 ManagedObjectType& sensorConfigs,
572 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700573{
James Feist6714a252018-09-10 15:26:18 -0700574 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800575 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700576 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700577 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700578 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700579 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800580 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700581 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700582 return false;
James Feist6714a252018-09-10 15:26:18 -0700583 }
584 useCache = true;
585 }
586
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700587 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700588 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700589 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700590 {
591 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800592 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700593 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700594 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700595 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700596 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700597 {
598 continue;
599 }
600
James Feist6714a252018-09-10 15:26:18 -0700601 auto findName = config.second.find("Name");
602 if (findName == config.second.end())
603 {
604 continue;
605 }
James Feist3eb82622019-02-08 13:10:22 -0800606 std::string nameRaw =
607 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700608 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700609 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700610
James Feistc140e202019-11-14 15:23:51 -0800611 auto present = std::optional<bool>();
612 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700613 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700614 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700615 if (suppConfig.first.find("PresenceGpio") !=
616 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700617 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700618 present = cpuIsPresent(suppConfig.second);
619 break;
James Feist58295ad2019-05-30 15:01:41 -0700620 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700621 }
622
James Feistc140e202019-11-14 15:23:51 -0800623 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
624 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700625 {
626 auto iface = objectServer.add_interface(
627 cpuInventoryPath + std::string("/") + name,
628 "xyz.openbmc_project.Inventory.Item");
629 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800630 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700631 iface->initialize();
632 inventoryIfaces[name] = std::move(iface);
633 }
James Feist58295ad2019-05-30 15:01:41 -0700634
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700635 auto findBus = config.second.find("Bus");
636 if (findBus == config.second.end())
637 {
638 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
639 continue;
640 }
James Feist3eb82622019-02-08 13:10:22 -0800641 uint64_t bus =
642 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700643
644 auto findAddress = config.second.find("Address");
645 if (findAddress == config.second.end())
646 {
647 std::cerr << "Can't find 'Address' setting in " << name
648 << "\n";
649 continue;
650 }
James Feist3eb82622019-02-08 13:10:22 -0800651 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
652 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700653
654 if (DEBUG)
655 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700656 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700657 std::cout << "addr: " << addr << "\n";
658 std::cout << "name: " << name << "\n";
659 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700660 }
661
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800662 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700663 }
664 }
665 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700666
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800667 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700668 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800669 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700670 << " parsed\n";
671 return true;
672 }
673
674 return false;
James Feist6714a252018-09-10 15:26:18 -0700675}
676
James Feistb6c0b912019-07-09 12:21:44 -0700677int main()
James Feist6714a252018-09-10 15:26:18 -0700678{
679 boost::asio::io_service io;
680 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800681 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700682
James Feist6714a252018-09-10 15:26:18 -0700683 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700684 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
685 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700686 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700687 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800688 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700689
690 filterTimer.expires_from_now(boost::posix_time::seconds(1));
691 filterTimer.async_wait([&](const boost::system::error_code& ec) {
692 if (ec == boost::asio::error::operation_aborted)
693 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700694 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700695 }
696
James Feistc140e202019-11-14 15:23:51 -0800697 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800699 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800700 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700701 }
702 });
703
James Feist6714a252018-09-10 15:26:18 -0700704 std::function<void(sdbusplus::message::message&)> eventHandler =
705 [&](sdbusplus::message::message& message) {
706 if (message.is_method_error())
707 {
708 std::cerr << "callback method error\n";
709 return;
710 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700711
712 if (DEBUG)
713 {
714 std::cout << message.get_path() << " is changed\n";
715 }
716
James Feist6714a252018-09-10 15:26:18 -0700717 // this implicitly cancels the timer
718 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700719 filterTimer.async_wait([&](const boost::system::error_code& ec) {
720 if (ec == boost::asio::error::operation_aborted)
721 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700722 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700723 }
724
James Feist58295ad2019-05-30 15:01:41 -0700725 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
James Feistc140e202019-11-14 15:23:51 -0800726 objectServer))
James Feist6714a252018-09-10 15:26:18 -0700727 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700728 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800729 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700730 }
731 });
732 };
733
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700734 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700735 {
736 auto match = std::make_unique<sdbusplus::bus::match::match>(
737 static_cast<sdbusplus::bus::bus&>(*systemBus),
738 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700739 std::string(inventoryPath) + "',arg0namespace='" +
740 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700741 eventHandler);
742 matches.emplace_back(std::move(match));
743 }
744
Jae Hyun Yoo59e47982020-01-15 10:33:13 -0800745 systemBus->request_name("xyz.openbmc_project.CPUSensor");
James Feist6714a252018-09-10 15:26:18 -0700746 io.run();
747}