blob: 7f437ce8eb1523ac9a5c798b0c4422ac265a9daf [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
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070020
21#include <CPUSensor.hpp>
22#include <Utils.hpp>
23#include <VariantVisitors.hpp>
24#include <boost/algorithm/string/predicate.hpp>
25#include <boost/algorithm/string/replace.hpp>
26#include <boost/container/flat_set.hpp>
27#include <boost/date_time/posix_time/posix_time.hpp>
28#include <boost/process/child.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
James Feistf87dc4c2018-12-05 14:39:51 -080034// clang-format off
35// this needs to be included last or we'll have build issues
36#include <linux/peci-ioctl.h>
37// clang-format on
38
James Feist6714a252018-09-10 15:26:18 -070039static constexpr bool DEBUG = false;
40
41enum State
42{
43 OFF, // host powered down
44 ON, // host powered on
45 READY // host powered on and mem test passed - fully ready
46};
47
48struct CPUConfig
49{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070050 CPUConfig(const uint64_t& bus, const uint64_t& addr,
51 const std::string& name, const State& state) :
52 bus(bus),
53 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070054 {
55 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070056 int bus;
James Feist6714a252018-09-10 15:26:18 -070057 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070058 std::string name;
James Feist6714a252018-09-10 15:26:18 -070059 State state;
60
61 bool operator<(const CPUConfig& rhs) const
62 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070063 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070064 }
65};
66
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070067static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070069
James Feistcf3bce62019-01-08 10:07:19 -080070namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080071
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070073 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070075 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
Jae Hyun Yoo13f48882019-02-19 13:37:07 -080076static constexpr std::array<const char*, 3> skipProps = {"Tcontrol",
77 "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070078
Jae Hyun Yood64262b2018-11-01 13:31:16 -070079void detectCpuAsync(
80 boost::asio::deadline_timer& pingTimer,
81 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
82 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080083 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070084 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080085 cpuSensors,
86 boost::container::flat_set<CPUConfig>& cpuConfigs,
87 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070088
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -070089bool createSensors(
James Feist6714a252018-09-10 15:26:18 -070090 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080091 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist6714a252018-09-10 15:26:18 -070092 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080093 cpuSensors,
94 boost::container::flat_set<CPUConfig>& cpuConfigs,
95 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070096{
97 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080098 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -070099 {
100 if (cpu.state != State::OFF)
101 {
102 available = true;
103 break;
104 }
105 }
106 if (!available)
107 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700108 return false;
James Feist6714a252018-09-10 15:26:18 -0700109 }
110
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800111 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700112 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800113 return false;
James Feist6714a252018-09-10 15:26:18 -0700114 }
115
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700116 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
118 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
119 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700120 {
121 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700122 return true;
James Feist6714a252018-09-10 15:26:18 -0700123 }
124
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700125 boost::container::flat_set<std::string> scannedDirectories;
126 boost::container::flat_set<std::string> createdSensors;
127
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800128 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700129 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700130 const std::string& pathStr = hwmonNamePath.string();
131 auto hwmonDirectory = hwmonNamePath.parent_path();
132
133 auto ret = scannedDirectories.insert(hwmonDirectory.string());
134 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700135 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700136 continue; // already searched this path
137 }
138
139 fs::path::iterator it = hwmonNamePath.begin();
140 std::advance(it, 6); // pick the 6th part for a PECI client device name
141 std::string deviceName = *it;
142 auto findHyphen = deviceName.find("-");
143 if (findHyphen == std::string::npos)
144 {
145 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700146 continue;
147 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700148 std::string busStr = deviceName.substr(0, findHyphen);
149 std::string addrStr = deviceName.substr(findHyphen + 1);
150
151 size_t bus = 0;
152 size_t addr = 0;
153 try
154 {
155 bus = std::stoi(busStr);
156 addr = std::stoi(addrStr, 0, 16);
157 }
158 catch (std::invalid_argument)
159 {
160 continue;
161 }
162
163 std::ifstream nameFile(hwmonNamePath);
164 if (!nameFile.good())
165 {
166 std::cerr << "Failure reading " << hwmonNamePath << "\n";
167 continue;
168 }
169 std::string hwmonName;
170 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700171 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800172 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700173 {
174 // shouldn't have an empty name file
175 continue;
176 }
James Feist6714a252018-09-10 15:26:18 -0700177 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700178 {
179 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
180 << "\n";
181 }
James Feist6714a252018-09-10 15:26:18 -0700182
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700183 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700184 const SensorData* sensorData = nullptr;
185 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700186 const std::pair<std::string, boost::container::flat_map<
187 std::string, BasicVariantType>>*
188 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700189
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700190 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800191 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700192 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700193 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700196 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700197 auto sensorBase = sensorData->find(sensorType);
198 if (sensorBase != sensorData->end())
199 {
200 baseConfiguration = &(*sensorBase);
201 break;
202 }
203 }
204 if (baseConfiguration == nullptr)
205 {
206 std::cerr << "error finding base configuration for" << hwmonName
207 << "\n";
208 continue;
209 }
210 auto configurationBus = baseConfiguration->second.find("Bus");
211 auto configurationAddress =
212 baseConfiguration->second.find("Address");
213
214 if (configurationBus == baseConfiguration->second.end() ||
215 configurationAddress == baseConfiguration->second.end())
216 {
217 std::cerr << "error finding bus or address in configuration";
218 continue;
219 }
220
James Feist3eb82622019-02-08 13:10:22 -0800221 if (std::get<uint64_t>(configurationBus->second) != bus ||
222 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700223 {
224 continue;
225 }
226
227 interfacePath = &(sensor.first.str);
228 break;
229 }
230 if (interfacePath == nullptr)
231 {
232 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700233 continue;
234 }
235
236 auto findCpuId = baseConfiguration->second.find("CpuID");
237 if (findCpuId == baseConfiguration->second.end())
238 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700239 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700240 continue;
241 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800243 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700244
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700246 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700248 {
249 std::cerr << "No temperature sensors in system\n";
250 continue;
251 }
252
253 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800254 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700255 {
256 auto inputPathStr = inputPath.string();
257 auto labelPath =
258 boost::replace_all_copy(inputPathStr, "input", "label");
259 std::ifstream labelFile(labelPath);
260 if (!labelFile.good())
261 {
262 std::cerr << "Failure reading " << labelPath << "\n";
263 continue;
264 }
265 std::string label;
266 std::getline(labelFile, label);
267 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800268
269 // skip non-sensor properties
270 bool skipIt = false;
271 for (const char* prop : skipProps)
272 {
273 if (label == prop)
274 {
275 skipIt = true;
276 break;
277 }
278 }
279 if (skipIt)
280 {
281 continue;
282 }
283
James Feist6714a252018-09-10 15:26:18 -0700284 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700285
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800286 auto findSensor = cpuSensors.find(sensorName);
287 if (findSensor != cpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700288 {
289 if (DEBUG)
290 {
291 std::cout << "Skipped: " << inputPath << ": " << sensorName
292 << " is already created\n";
293 }
294 continue;
295 }
296
James Feist6714a252018-09-10 15:26:18 -0700297 std::vector<thresholds::Threshold> sensorThresholds;
298 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700299 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700300 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800301 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700302 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700303 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
304 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700305 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700306 std::cerr << "error populating thresholds for "
307 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700308 }
309 }
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800310 cpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700311 inputPathStr, sensorType, objectServer, dbusConnection, io,
312 sensorName, std::move(sensorThresholds), *interfacePath);
313 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700314 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700315 {
James Feist6714a252018-09-10 15:26:18 -0700316 std::cout << "Mapped: " << inputPath << " to " << sensorName
317 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700318 }
James Feist6714a252018-09-10 15:26:18 -0700319 }
320 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700321
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700322 if (createdSensors.size())
323 {
324 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
325 << " created\n";
326 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700327
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700328 return true;
James Feist6714a252018-09-10 15:26:18 -0700329}
330
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700332{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 std::ostringstream hex;
334 hex << std::hex << config.addr;
335 const std::string& addrHexStr = hex.str();
336 std::string busStr = std::to_string(config.bus);
337
338 std::string parameters = "peci-client 0x" + addrHexStr;
339 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
340
James Feistcf3bce62019-01-08 10:07:19 -0800341 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700342 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800343 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700344 {
James Feistcf3bce62019-01-08 10:07:19 -0800345 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700346 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700347 continue;
James Feist6714a252018-09-10 15:26:18 -0700348 }
349
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700350 const std::string& directoryName = path.path().filename();
351 if (boost::starts_with(directoryName, busStr) &&
352 boost::ends_with(directoryName, addrHexStr))
353 {
354 if (DEBUG)
355 {
356 std::cout << parameters << " on bus " << busStr
357 << " is already exported\n";
358 }
359 return;
360 }
James Feist6714a252018-09-10 15:26:18 -0700361 }
362
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700363 std::ofstream deviceFile(device);
364 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700365 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700366 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700367 return;
368 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700369 deviceFile << parameters;
370 deviceFile.close();
371
372 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700373}
374
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800375void detectCpu(
376 boost::asio::deadline_timer& pingTimer,
377 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
378 sdbusplus::asio::object_server& objectServer,
379 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
380 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
381 cpuSensors,
382 boost::container::flat_set<CPUConfig>& cpuConfigs,
383 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700384{
James Feist6714a252018-09-10 15:26:18 -0700385 size_t rescanDelaySeconds = 0;
386 bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700387
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800388 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700389 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700390 std::string peciDevPath = peciDev + std::to_string(config.bus);
391 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
392 if (file < 0)
393 {
394 std::cerr << "unable to open " << peciDevPath << "\n";
395 std::exit(EXIT_FAILURE);
396 }
397
James Feist6714a252018-09-10 15:26:18 -0700398 State state;
399 struct peci_ping_msg msg;
400 msg.addr = config.addr;
401 if (!ioctl(file, PECI_IOC_PING, &msg))
402 {
403 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700404 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700405 {
406 struct peci_rd_pkg_cfg_msg msg;
407 msg.addr = config.addr;
408 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
409 msg.param = rank;
410 msg.rx_len = 4;
411 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
412 {
413 if (msg.pkg_config[0] || msg.pkg_config[1] ||
414 msg.pkg_config[2])
415 {
416 dimmReady = true;
417 break;
418 }
419 }
420 else
421 {
422 break;
423 }
424 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700425
James Feist6714a252018-09-10 15:26:18 -0700426 if (dimmReady)
427 {
428 state = State::READY;
429 }
430 else
431 {
432 state = State::ON;
433 }
434 }
435 else
436 {
437 state = State::OFF;
438 }
439
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700440 close(file);
441
James Feist6714a252018-09-10 15:26:18 -0700442 if (config.state != state)
443 {
444 if (config.state == State::OFF)
445 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700446 std::cout << config.name << " is detected\n";
447 exportDevice(config);
James Feist6714a252018-09-10 15:26:18 -0700448 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700449 if (state == State::READY)
450 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700451 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700452 << " is/are detected\n";
453 }
James Feist6714a252018-09-10 15:26:18 -0700454 config.state = state;
455 }
456
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700457 if (config.state != State::OFF)
458 {
459 if (config.state == State::ON)
460 {
461 rescanDelaySeconds = 1;
462 }
463 else
464 {
465 rescanDelaySeconds = 5;
466 }
467 }
468
469 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700470 {
471 keepPinging = true;
472 }
473
474 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700475 {
476 std::cout << config.name << ", state: " << config.state << "\n";
477 }
James Feist6714a252018-09-10 15:26:18 -0700478 }
479
James Feist6714a252018-09-10 15:26:18 -0700480 if (rescanDelaySeconds)
481 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700482 creationTimer.expires_from_now(
483 boost::posix_time::seconds(rescanDelaySeconds));
484 creationTimer.async_wait([&](const boost::system::error_code& ec) {
485 if (ec == boost::asio::error::operation_aborted)
486 {
487 return; // we're being canceled
488 }
489
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800490 if (!createSensors(io, objectServer, dbusConnection, cpuSensors,
491 cpuConfigs, sensorConfigs))
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700492 {
493 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800494 dbusConnection, cpuSensors, cpuConfigs,
495 sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700496 }
497 });
James Feist6714a252018-09-10 15:26:18 -0700498 }
499
500 if (keepPinging)
501 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800502 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
503 dbusConnection, cpuSensors, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700504 }
505}
506
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700507void detectCpuAsync(
508 boost::asio::deadline_timer& pingTimer,
509 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
510 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800511 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700512 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800513 cpuSensors,
514 boost::container::flat_set<CPUConfig>& cpuConfigs,
515 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700516{
517 pingTimer.expires_from_now(boost::posix_time::seconds(1));
518 pingTimer.async_wait([&](const boost::system::error_code& ec) {
519 if (ec == boost::asio::error::operation_aborted)
520 {
521 return; // we're being canceled
522 }
523
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800524 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
525 cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700526 });
527}
528
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700529bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800530 boost::container::flat_set<CPUConfig>& cpuConfigs,
531 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700532{
James Feist6714a252018-09-10 15:26:18 -0700533 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800534 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700535 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700536 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700537 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700538 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800539 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700540 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700541 return false;
James Feist6714a252018-09-10 15:26:18 -0700542 }
543 useCache = true;
544 }
545
546 // check PECI client addresses and DT overlay names from CPU configuration
547 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700548 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700549 {
550 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800551 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700552 {
553 for (const std::pair<
554 std::string,
555 boost::container::flat_map<std::string, BasicVariantType>>&
556 config : sensor.second)
557 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700558 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700559 {
560 continue;
561 }
562
James Feist6714a252018-09-10 15:26:18 -0700563 auto findName = config.second.find("Name");
564 if (findName == config.second.end())
565 {
566 continue;
567 }
James Feist3eb82622019-02-08 13:10:22 -0800568 std::string nameRaw =
569 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700570 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700571 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700572
573 auto findBus = config.second.find("Bus");
574 if (findBus == config.second.end())
575 {
576 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
577 continue;
578 }
James Feist3eb82622019-02-08 13:10:22 -0800579 uint64_t bus =
580 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700581
582 auto findAddress = config.second.find("Address");
583 if (findAddress == config.second.end())
584 {
585 std::cerr << "Can't find 'Address' setting in " << name
586 << "\n";
587 continue;
588 }
James Feist3eb82622019-02-08 13:10:22 -0800589 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
590 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700591
592 if (DEBUG)
593 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700594 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700595 std::cout << "addr: " << addr << "\n";
596 std::cout << "name: " << name << "\n";
597 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700598 }
599
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800600 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700601 }
602 }
603 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700604
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800605 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700606 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800607 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700608 << " parsed\n";
609 return true;
610 }
611
612 return false;
James Feist6714a252018-09-10 15:26:18 -0700613}
614
615int main(int argc, char** argv)
616{
617 boost::asio::io_service io;
618 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800619 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700620
621 systemBus->request_name("xyz.openbmc_project.CPUSensor");
622 sdbusplus::asio::object_server objectServer(systemBus);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800623 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
624 cpuSensors;
James Feist6714a252018-09-10 15:26:18 -0700625 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
626 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700627 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700628 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800629 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700630
631 filterTimer.expires_from_now(boost::posix_time::seconds(1));
632 filterTimer.async_wait([&](const boost::system::error_code& ec) {
633 if (ec == boost::asio::error::operation_aborted)
634 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700635 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700636 }
637
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800638 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700639 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800640 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
641 systemBus, cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700642 }
643 });
644
James Feist6714a252018-09-10 15:26:18 -0700645 std::function<void(sdbusplus::message::message&)> eventHandler =
646 [&](sdbusplus::message::message& message) {
647 if (message.is_method_error())
648 {
649 std::cerr << "callback method error\n";
650 return;
651 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700652
653 if (DEBUG)
654 {
655 std::cout << message.get_path() << " is changed\n";
656 }
657
James Feist6714a252018-09-10 15:26:18 -0700658 // this implicitly cancels the timer
659 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700660 filterTimer.async_wait([&](const boost::system::error_code& ec) {
661 if (ec == boost::asio::error::operation_aborted)
662 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700663 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700664 }
665
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800666 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
James Feist6714a252018-09-10 15:26:18 -0700667 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700668 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800669 systemBus, cpuSensors, cpuConfigs,
670 sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700671 }
672 });
673 };
674
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700675 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700676 {
677 auto match = std::make_unique<sdbusplus::bus::match::match>(
678 static_cast<sdbusplus::bus::bus&>(*systemBus),
679 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700680 std::string(inventoryPath) + "',arg0namespace='" +
681 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700682 eventHandler);
683 matches.emplace_back(std::move(match));
684 }
685
686 io.run();
687}