blob: 2261af7fd9184fb86a1bdeddce3164217f96b31b [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
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200105std::string createSensorName(const std::string& label, const std::string& item,
106 const int& cpuId)
107{
108 std::string sensorName = label;
109 if (item.compare("input") != 0)
110 {
111 sensorName += " " + item;
112 }
113 sensorName += " CPU" + std::to_string(cpuId);
114 // converting to Upper Camel case whole name
115 bool isWordEnd = true;
116 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
117 [&isWordEnd](int c) {
118 if (std::isspace(c))
119 {
120 isWordEnd = true;
121 }
122 else
123 {
124 if (isWordEnd)
125 {
126 isWordEnd = false;
127 return std::toupper(c);
128 }
129 }
130 return c;
131 });
132 return sensorName;
133}
134
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800135bool createSensors(boost::asio::io_service& io,
136 sdbusplus::asio::object_server& objectServer,
137 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
138 boost::container::flat_set<CPUConfig>& cpuConfigs,
139 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700140{
141 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800142 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700143 {
144 if (cpu.state != State::OFF)
145 {
146 available = true;
James Feistc140e202019-11-14 15:23:51 -0800147 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
148 inventoryIfaces[cpu.name];
149 if (iface != nullptr)
150 {
151 continue;
152 }
153 iface = objectServer.add_interface(
154 cpuInventoryPath + std::string("/") + cpu.name,
155 "xyz.openbmc_project.Inventory.Item");
156 iface->register_property("PrettyName", cpu.name);
157 iface->register_property("Present", true);
158 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700159 }
160 }
161 if (!available)
162 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700163 return false;
James Feist6714a252018-09-10 15:26:18 -0700164 }
165
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800166 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700167 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800168 return false;
James Feist6714a252018-09-10 15:26:18 -0700169 }
170
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700171 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700172 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
173 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
174 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700175 {
176 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700177 return true;
James Feist6714a252018-09-10 15:26:18 -0700178 }
179
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700180 boost::container::flat_set<std::string> scannedDirectories;
181 boost::container::flat_set<std::string> createdSensors;
182
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800183 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700184 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700185 const std::string& pathStr = hwmonNamePath.string();
186 auto hwmonDirectory = hwmonNamePath.parent_path();
187
188 auto ret = scannedDirectories.insert(hwmonDirectory.string());
189 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700190 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700191 continue; // already searched this path
192 }
193
194 fs::path::iterator it = hwmonNamePath.begin();
195 std::advance(it, 6); // pick the 6th part for a PECI client device name
196 std::string deviceName = *it;
197 auto findHyphen = deviceName.find("-");
198 if (findHyphen == std::string::npos)
199 {
200 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700201 continue;
202 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 std::string busStr = deviceName.substr(0, findHyphen);
204 std::string addrStr = deviceName.substr(findHyphen + 1);
205
206 size_t bus = 0;
207 size_t addr = 0;
208 try
209 {
210 bus = std::stoi(busStr);
211 addr = std::stoi(addrStr, 0, 16);
212 }
James Feistb6c0b912019-07-09 12:21:44 -0700213 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700214 {
215 continue;
216 }
217
218 std::ifstream nameFile(hwmonNamePath);
219 if (!nameFile.good())
220 {
221 std::cerr << "Failure reading " << hwmonNamePath << "\n";
222 continue;
223 }
224 std::string hwmonName;
225 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700226 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800227 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700228 {
229 // shouldn't have an empty name file
230 continue;
231 }
James Feist6714a252018-09-10 15:26:18 -0700232 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700233 {
234 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
235 << "\n";
236 }
James Feist6714a252018-09-10 15:26:18 -0700237
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700238 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700239 const SensorData* sensorData = nullptr;
240 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700241 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700242
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800244 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700245 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700249 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700250 auto sensorBase = sensorData->find(sensorType);
251 if (sensorBase != sensorData->end())
252 {
253 baseConfiguration = &(*sensorBase);
254 break;
255 }
256 }
257 if (baseConfiguration == nullptr)
258 {
259 std::cerr << "error finding base configuration for" << hwmonName
260 << "\n";
261 continue;
262 }
263 auto configurationBus = baseConfiguration->second.find("Bus");
264 auto configurationAddress =
265 baseConfiguration->second.find("Address");
266
267 if (configurationBus == baseConfiguration->second.end() ||
268 configurationAddress == baseConfiguration->second.end())
269 {
270 std::cerr << "error finding bus or address in configuration";
271 continue;
272 }
273
James Feist3eb82622019-02-08 13:10:22 -0800274 if (std::get<uint64_t>(configurationBus->second) != bus ||
275 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700276 {
277 continue;
278 }
279
280 interfacePath = &(sensor.first.str);
281 break;
282 }
283 if (interfacePath == nullptr)
284 {
285 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700286 continue;
287 }
288
289 auto findCpuId = baseConfiguration->second.find("CpuID");
290 if (findCpuId == baseConfiguration->second.end())
291 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700292 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700293 continue;
294 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700295 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800296 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700297
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700298 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700299 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200300 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200301 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700302 {
303 std::cerr << "No temperature sensors in system\n";
304 continue;
305 }
306
307 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800308 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700309 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200310 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200311 if (!fileParts)
312 {
313 continue;
314 }
315 auto [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700316 auto inputPathStr = inputPath.string();
317 auto labelPath =
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200318 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700319 std::ifstream labelFile(labelPath);
320 if (!labelFile.good())
321 {
322 std::cerr << "Failure reading " << labelPath << "\n";
323 continue;
324 }
325 std::string label;
326 std::getline(labelFile, label);
327 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800328
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200329 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800331 auto findSensor = gCpuSensors.find(sensorName);
332 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 {
334 if (DEBUG)
335 {
336 std::cout << "Skipped: " << inputPath << ": " << sensorName
337 << " is already created\n";
338 }
339 continue;
340 }
341
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800342 // check hidden properties
343 bool show = true;
344 for (const char* prop : hiddenProps)
345 {
346 if (label == prop)
347 {
348 show = false;
349 break;
350 }
351 }
352
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700353 /*
354 * Find if there is DtsCritOffset is configured in config file
355 * set it if configured or else set it to 0
356 */
357 double dtsOffset = 0;
358 if (label == "DTS")
359 {
360 auto findThrOffset =
361 baseConfiguration->second.find("DtsCritOffset");
362 if (findThrOffset != baseConfiguration->second.end())
363 {
364 dtsOffset = std::visit(VariantToDoubleVisitor(),
365 findThrOffset->second);
366 }
367 }
368
James Feist6714a252018-09-10 15:26:18 -0700369 std::vector<thresholds::Threshold> sensorThresholds;
370 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700371 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700372 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800373 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700374 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700375 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700376 CPUSensor::sensorScaleFactor,
377 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700378 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700379 std::cerr << "error populating thresholds for "
380 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700381 }
382 }
James Feistc140e202019-11-14 15:23:51 -0800383 auto& sensorPtr = gCpuSensors[sensorName];
384 // make sure destructor fires before creating a new one
385 sensorPtr = nullptr;
386 sensorPtr = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700387 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800388 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700389 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700391 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700392 {
James Feist6714a252018-09-10 15:26:18 -0700393 std::cout << "Mapped: " << inputPath << " to " << sensorName
394 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700395 }
James Feist6714a252018-09-10 15:26:18 -0700396 }
397 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700398
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700399 if (createdSensors.size())
400 {
401 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
402 << " created\n";
403 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700404
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700405 return true;
James Feist6714a252018-09-10 15:26:18 -0700406}
407
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700408void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700409{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700410 std::ostringstream hex;
411 hex << std::hex << config.addr;
412 const std::string& addrHexStr = hex.str();
413 std::string busStr = std::to_string(config.bus);
414
415 std::string parameters = "peci-client 0x" + addrHexStr;
416 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
417
James Feistcf3bce62019-01-08 10:07:19 -0800418 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700419 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800420 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700421 {
James Feistcf3bce62019-01-08 10:07:19 -0800422 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700423 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700424 continue;
James Feist6714a252018-09-10 15:26:18 -0700425 }
426
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700427 const std::string& directoryName = path.path().filename();
428 if (boost::starts_with(directoryName, busStr) &&
429 boost::ends_with(directoryName, addrHexStr))
430 {
431 if (DEBUG)
432 {
433 std::cout << parameters << " on bus " << busStr
434 << " is already exported\n";
435 }
436 return;
437 }
James Feist6714a252018-09-10 15:26:18 -0700438 }
439
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700440 std::ofstream deviceFile(device);
441 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700442 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700443 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700444 return;
445 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700446 deviceFile << parameters;
447 deviceFile.close();
448
449 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700450}
451
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800452void detectCpu(boost::asio::deadline_timer& pingTimer,
453 boost::asio::deadline_timer& creationTimer,
454 boost::asio::io_service& io,
455 sdbusplus::asio::object_server& objectServer,
456 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
457 boost::container::flat_set<CPUConfig>& cpuConfigs,
458 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700459{
James Feist6714a252018-09-10 15:26:18 -0700460 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700461 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700462
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800463 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700464 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700465 std::string peciDevPath = peciDev + std::to_string(config.bus);
466 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
467 if (file < 0)
468 {
469 std::cerr << "unable to open " << peciDevPath << "\n";
470 std::exit(EXIT_FAILURE);
471 }
472
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700473 State newState;
James Feist6714a252018-09-10 15:26:18 -0700474 struct peci_ping_msg msg;
475 msg.addr = config.addr;
476 if (!ioctl(file, PECI_IOC_PING, &msg))
477 {
478 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700479 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700480 {
481 struct peci_rd_pkg_cfg_msg msg;
482 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 }
510 else
511 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700512 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700513 }
514
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700515 close(file);
516
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700517 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700518 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700519 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700520 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700521 if (config.state == State::OFF)
522 {
523 std::cout << config.name << " is detected\n";
524 exportDevice(config);
525 }
James Feist6714a252018-09-10 15:26:18 -0700526
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700527 if (newState == State::ON)
528 {
529 rescanDelaySeconds = 3;
530 }
531 else if (newState == State::READY)
532 {
533 rescanDelaySeconds = 5;
534 std::cout << "DIMM(s) on " << config.name
535 << " is/are detected\n";
536 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700537 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700538
539 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700540 }
541
542 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700543 {
544 keepPinging = true;
545 }
546
547 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700548 {
549 std::cout << config.name << ", state: " << config.state << "\n";
550 }
James Feist6714a252018-09-10 15:26:18 -0700551 }
552
James Feist6714a252018-09-10 15:26:18 -0700553 if (rescanDelaySeconds)
554 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700555 creationTimer.expires_from_now(
556 boost::posix_time::seconds(rescanDelaySeconds));
557 creationTimer.async_wait([&](const boost::system::error_code& ec) {
558 if (ec == boost::asio::error::operation_aborted)
559 {
560 return; // we're being canceled
561 }
562
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800563 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700564 sensorConfigs) ||
565 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700566 {
567 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800568 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700569 }
570 });
James Feist6714a252018-09-10 15:26:18 -0700571 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700572 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700573 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800574 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800575 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700576 }
577}
578
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700579void detectCpuAsync(
580 boost::asio::deadline_timer& pingTimer,
581 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
582 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800583 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800584 boost::container::flat_set<CPUConfig>& cpuConfigs,
585 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700586{
587 pingTimer.expires_from_now(boost::posix_time::seconds(1));
588 pingTimer.async_wait([&](const boost::system::error_code& ec) {
589 if (ec == boost::asio::error::operation_aborted)
590 {
591 return; // we're being canceled
592 }
593
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800594 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800595 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700596 });
597}
598
James Feistc140e202019-11-14 15:23:51 -0800599bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
600 boost::container::flat_set<CPUConfig>& cpuConfigs,
601 ManagedObjectType& sensorConfigs,
602 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700603{
James Feist6714a252018-09-10 15:26:18 -0700604 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800605 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700606 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700607 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700608 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700609 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800610 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700611 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700612 return false;
James Feist6714a252018-09-10 15:26:18 -0700613 }
614 useCache = true;
615 }
616
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700617 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700618 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700619 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700620 {
621 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800622 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700623 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700624 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700625 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700626 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700627 {
628 continue;
629 }
630
James Feist6714a252018-09-10 15:26:18 -0700631 auto findName = config.second.find("Name");
632 if (findName == config.second.end())
633 {
634 continue;
635 }
James Feist3eb82622019-02-08 13:10:22 -0800636 std::string nameRaw =
637 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700638 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700639 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700640
James Feistc140e202019-11-14 15:23:51 -0800641 auto present = std::optional<bool>();
642 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700643 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700644 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700645 if (suppConfig.first.find("PresenceGpio") !=
646 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700647 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700648 present = cpuIsPresent(suppConfig.second);
649 break;
James Feist58295ad2019-05-30 15:01:41 -0700650 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700651 }
652
James Feistc140e202019-11-14 15:23:51 -0800653 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
654 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700655 {
656 auto iface = objectServer.add_interface(
657 cpuInventoryPath + std::string("/") + name,
658 "xyz.openbmc_project.Inventory.Item");
659 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800660 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700661 iface->initialize();
662 inventoryIfaces[name] = std::move(iface);
663 }
James Feist58295ad2019-05-30 15:01:41 -0700664
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700665 auto findBus = config.second.find("Bus");
666 if (findBus == config.second.end())
667 {
668 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
669 continue;
670 }
James Feist3eb82622019-02-08 13:10:22 -0800671 uint64_t bus =
672 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700673
674 auto findAddress = config.second.find("Address");
675 if (findAddress == config.second.end())
676 {
677 std::cerr << "Can't find 'Address' setting in " << name
678 << "\n";
679 continue;
680 }
James Feist3eb82622019-02-08 13:10:22 -0800681 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
682 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700683
684 if (DEBUG)
685 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700686 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700687 std::cout << "addr: " << addr << "\n";
688 std::cout << "name: " << name << "\n";
689 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700690 }
691
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800692 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700693 }
694 }
695 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700696
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800697 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800699 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700700 << " parsed\n";
701 return true;
702 }
703
704 return false;
James Feist6714a252018-09-10 15:26:18 -0700705}
706
James Feistb6c0b912019-07-09 12:21:44 -0700707int main()
James Feist6714a252018-09-10 15:26:18 -0700708{
709 boost::asio::io_service io;
710 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800711 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700712
James Feist6714a252018-09-10 15:26:18 -0700713 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700714 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
715 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700716 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700717 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800718 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700719
720 filterTimer.expires_from_now(boost::posix_time::seconds(1));
721 filterTimer.async_wait([&](const boost::system::error_code& ec) {
722 if (ec == boost::asio::error::operation_aborted)
723 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700724 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700725 }
726
James Feistc140e202019-11-14 15:23:51 -0800727 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700728 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800729 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800730 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700731 }
732 });
733
James Feist6714a252018-09-10 15:26:18 -0700734 std::function<void(sdbusplus::message::message&)> eventHandler =
735 [&](sdbusplus::message::message& message) {
736 if (message.is_method_error())
737 {
738 std::cerr << "callback method error\n";
739 return;
740 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700741
742 if (DEBUG)
743 {
744 std::cout << message.get_path() << " is changed\n";
745 }
746
James Feist6714a252018-09-10 15:26:18 -0700747 // this implicitly cancels the timer
748 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700749 filterTimer.async_wait([&](const boost::system::error_code& ec) {
750 if (ec == boost::asio::error::operation_aborted)
751 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700752 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700753 }
754
James Feist58295ad2019-05-30 15:01:41 -0700755 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
James Feistc140e202019-11-14 15:23:51 -0800756 objectServer))
James Feist6714a252018-09-10 15:26:18 -0700757 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700758 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800759 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700760 }
761 });
762 };
763
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700764 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700765 {
766 auto match = std::make_unique<sdbusplus::bus::match::match>(
767 static_cast<sdbusplus::bus::bus&>(*systemBus),
768 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700769 std::string(inventoryPath) + "',arg0namespace='" +
770 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700771 eventHandler);
772 matches.emplace_back(std::move(match));
773 }
774
Jae Hyun Yoo59e47982020-01-15 10:33:13 -0800775 systemBus->request_name("xyz.openbmc_project.CPUSensor");
James Feist6714a252018-09-10 15:26:18 -0700776 io.run();
777}