blob: 6c4e2e59c2c44c4f710c8fa1dd4c9f6eb699b0c7 [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>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080037#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
38#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
39#endif
James Feistf87dc4c2018-12-05 14:39:51 -080040// clang-format on
41
James Feist6714a252018-09-10 15:26:18 -070042static constexpr bool DEBUG = false;
43
44enum State
45{
46 OFF, // host powered down
47 ON, // host powered on
48 READY // host powered on and mem test passed - fully ready
49};
50
51struct CPUConfig
52{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070053 CPUConfig(const uint64_t& bus, const uint64_t& addr,
54 const std::string& name, const State& state) :
55 bus(bus),
56 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070057 {
58 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070059 int bus;
James Feist6714a252018-09-10 15:26:18 -070060 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070061 std::string name;
James Feist6714a252018-09-10 15:26:18 -070062 State state;
63
64 bool operator<(const CPUConfig& rhs) const
65 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070066 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070067 }
68};
69
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070070static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070072
James Feistcf3bce62019-01-08 10:07:19 -080073namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080074
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070075static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070076 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070078 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
Jae Hyun Yoo13f48882019-02-19 13:37:07 -080079static constexpr std::array<const char*, 3> skipProps = {"Tcontrol",
80 "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070081
Jae Hyun Yood64262b2018-11-01 13:31:16 -070082void detectCpuAsync(
83 boost::asio::deadline_timer& pingTimer,
84 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
85 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080086 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -070087 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080088 cpuSensors,
89 boost::container::flat_set<CPUConfig>& cpuConfigs,
90 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070091
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -070092bool createSensors(
James Feist6714a252018-09-10 15:26:18 -070093 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080094 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist6714a252018-09-10 15:26:18 -070095 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080096 cpuSensors,
97 boost::container::flat_set<CPUConfig>& cpuConfigs,
98 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070099{
100 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700102 {
103 if (cpu.state != State::OFF)
104 {
105 available = true;
106 break;
107 }
108 }
109 if (!available)
110 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700111 return false;
James Feist6714a252018-09-10 15:26:18 -0700112 }
113
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800114 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700115 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800116 return false;
James Feist6714a252018-09-10 15:26:18 -0700117 }
118
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700119 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
121 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
122 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700123 {
124 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700125 return true;
James Feist6714a252018-09-10 15:26:18 -0700126 }
127
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700128 boost::container::flat_set<std::string> scannedDirectories;
129 boost::container::flat_set<std::string> createdSensors;
130
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800131 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700132 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700133 const std::string& pathStr = hwmonNamePath.string();
134 auto hwmonDirectory = hwmonNamePath.parent_path();
135
136 auto ret = scannedDirectories.insert(hwmonDirectory.string());
137 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700138 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700139 continue; // already searched this path
140 }
141
142 fs::path::iterator it = hwmonNamePath.begin();
143 std::advance(it, 6); // pick the 6th part for a PECI client device name
144 std::string deviceName = *it;
145 auto findHyphen = deviceName.find("-");
146 if (findHyphen == std::string::npos)
147 {
148 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700149 continue;
150 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700151 std::string busStr = deviceName.substr(0, findHyphen);
152 std::string addrStr = deviceName.substr(findHyphen + 1);
153
154 size_t bus = 0;
155 size_t addr = 0;
156 try
157 {
158 bus = std::stoi(busStr);
159 addr = std::stoi(addrStr, 0, 16);
160 }
161 catch (std::invalid_argument)
162 {
163 continue;
164 }
165
166 std::ifstream nameFile(hwmonNamePath);
167 if (!nameFile.good())
168 {
169 std::cerr << "Failure reading " << hwmonNamePath << "\n";
170 continue;
171 }
172 std::string hwmonName;
173 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700174 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800175 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700176 {
177 // shouldn't have an empty name file
178 continue;
179 }
James Feist6714a252018-09-10 15:26:18 -0700180 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700181 {
182 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
183 << "\n";
184 }
James Feist6714a252018-09-10 15:26:18 -0700185
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700186 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700187 const SensorData* sensorData = nullptr;
188 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700189 const std::pair<std::string, boost::container::flat_map<
190 std::string, BasicVariantType>>*
191 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700192
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700193 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800194 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700195 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700196 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700197 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700198 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700199 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700200 auto sensorBase = sensorData->find(sensorType);
201 if (sensorBase != sensorData->end())
202 {
203 baseConfiguration = &(*sensorBase);
204 break;
205 }
206 }
207 if (baseConfiguration == nullptr)
208 {
209 std::cerr << "error finding base configuration for" << hwmonName
210 << "\n";
211 continue;
212 }
213 auto configurationBus = baseConfiguration->second.find("Bus");
214 auto configurationAddress =
215 baseConfiguration->second.find("Address");
216
217 if (configurationBus == baseConfiguration->second.end() ||
218 configurationAddress == baseConfiguration->second.end())
219 {
220 std::cerr << "error finding bus or address in configuration";
221 continue;
222 }
223
James Feist3eb82622019-02-08 13:10:22 -0800224 if (std::get<uint64_t>(configurationBus->second) != bus ||
225 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700226 {
227 continue;
228 }
229
230 interfacePath = &(sensor.first.str);
231 break;
232 }
233 if (interfacePath == nullptr)
234 {
235 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700236 continue;
237 }
238
239 auto findCpuId = baseConfiguration->second.find("CpuID");
240 if (findCpuId == baseConfiguration->second.end())
241 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700243 continue;
244 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800246 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700247
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700249 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700250 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700251 {
252 std::cerr << "No temperature sensors in system\n";
253 continue;
254 }
255
256 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800257 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700258 {
259 auto inputPathStr = inputPath.string();
260 auto labelPath =
261 boost::replace_all_copy(inputPathStr, "input", "label");
262 std::ifstream labelFile(labelPath);
263 if (!labelFile.good())
264 {
265 std::cerr << "Failure reading " << labelPath << "\n";
266 continue;
267 }
268 std::string label;
269 std::getline(labelFile, label);
270 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800271
272 // skip non-sensor properties
273 bool skipIt = false;
274 for (const char* prop : skipProps)
275 {
276 if (label == prop)
277 {
278 skipIt = true;
279 break;
280 }
281 }
282 if (skipIt)
283 {
284 continue;
285 }
286
James Feist6714a252018-09-10 15:26:18 -0700287 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700288
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800289 auto findSensor = cpuSensors.find(sensorName);
290 if (findSensor != cpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700291 {
292 if (DEBUG)
293 {
294 std::cout << "Skipped: " << inputPath << ": " << sensorName
295 << " is already created\n";
296 }
297 continue;
298 }
299
James Feist6714a252018-09-10 15:26:18 -0700300 std::vector<thresholds::Threshold> sensorThresholds;
301 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700302 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700303 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800304 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700305 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700306 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
307 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700308 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700309 std::cerr << "error populating thresholds for "
310 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700311 }
312 }
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800313 cpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700314 inputPathStr, sensorType, objectServer, dbusConnection, io,
315 sensorName, std::move(sensorThresholds), *interfacePath);
316 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700317 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700318 {
James Feist6714a252018-09-10 15:26:18 -0700319 std::cout << "Mapped: " << inputPath << " to " << sensorName
320 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700321 }
James Feist6714a252018-09-10 15:26:18 -0700322 }
323 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700324
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700325 if (createdSensors.size())
326 {
327 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
328 << " created\n";
329 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700330
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700331 return true;
James Feist6714a252018-09-10 15:26:18 -0700332}
333
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700334void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700335{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700336 std::ostringstream hex;
337 hex << std::hex << config.addr;
338 const std::string& addrHexStr = hex.str();
339 std::string busStr = std::to_string(config.bus);
340
341 std::string parameters = "peci-client 0x" + addrHexStr;
342 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
343
James Feistcf3bce62019-01-08 10:07:19 -0800344 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700345 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800346 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700347 {
James Feistcf3bce62019-01-08 10:07:19 -0800348 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700349 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700350 continue;
James Feist6714a252018-09-10 15:26:18 -0700351 }
352
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700353 const std::string& directoryName = path.path().filename();
354 if (boost::starts_with(directoryName, busStr) &&
355 boost::ends_with(directoryName, addrHexStr))
356 {
357 if (DEBUG)
358 {
359 std::cout << parameters << " on bus " << busStr
360 << " is already exported\n";
361 }
362 return;
363 }
James Feist6714a252018-09-10 15:26:18 -0700364 }
365
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700366 std::ofstream deviceFile(device);
367 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700368 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700369 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700370 return;
371 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700372 deviceFile << parameters;
373 deviceFile.close();
374
375 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700376}
377
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800378void detectCpu(
379 boost::asio::deadline_timer& pingTimer,
380 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
381 sdbusplus::asio::object_server& objectServer,
382 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
383 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
384 cpuSensors,
385 boost::container::flat_set<CPUConfig>& cpuConfigs,
386 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700387{
James Feist6714a252018-09-10 15:26:18 -0700388 size_t rescanDelaySeconds = 0;
389 bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700390
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800391 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700392 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700393 std::string peciDevPath = peciDev + std::to_string(config.bus);
394 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
395 if (file < 0)
396 {
397 std::cerr << "unable to open " << peciDevPath << "\n";
398 std::exit(EXIT_FAILURE);
399 }
400
James Feist6714a252018-09-10 15:26:18 -0700401 State state;
402 struct peci_ping_msg msg;
403 msg.addr = config.addr;
404 if (!ioctl(file, PECI_IOC_PING, &msg))
405 {
406 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700407 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700408 {
409 struct peci_rd_pkg_cfg_msg msg;
410 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800411 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700412 msg.param = rank;
413 msg.rx_len = 4;
414 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
415 {
416 if (msg.pkg_config[0] || msg.pkg_config[1] ||
417 msg.pkg_config[2])
418 {
419 dimmReady = true;
420 break;
421 }
422 }
423 else
424 {
425 break;
426 }
427 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700428
James Feist6714a252018-09-10 15:26:18 -0700429 if (dimmReady)
430 {
431 state = State::READY;
432 }
433 else
434 {
435 state = State::ON;
436 }
437 }
438 else
439 {
440 state = State::OFF;
441 }
442
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700443 close(file);
444
James Feist6714a252018-09-10 15:26:18 -0700445 if (config.state != state)
446 {
447 if (config.state == State::OFF)
448 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700449 std::cout << config.name << " is detected\n";
450 exportDevice(config);
James Feist6714a252018-09-10 15:26:18 -0700451 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700452 if (state == State::READY)
453 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700454 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700455 << " is/are detected\n";
456 }
James Feist6714a252018-09-10 15:26:18 -0700457 config.state = state;
458 }
459
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700460 if (config.state != State::OFF)
461 {
462 if (config.state == State::ON)
463 {
464 rescanDelaySeconds = 1;
465 }
466 else
467 {
468 rescanDelaySeconds = 5;
469 }
470 }
471
472 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700473 {
474 keepPinging = true;
475 }
476
477 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700478 {
479 std::cout << config.name << ", state: " << config.state << "\n";
480 }
James Feist6714a252018-09-10 15:26:18 -0700481 }
482
James Feist6714a252018-09-10 15:26:18 -0700483 if (rescanDelaySeconds)
484 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700485 creationTimer.expires_from_now(
486 boost::posix_time::seconds(rescanDelaySeconds));
487 creationTimer.async_wait([&](const boost::system::error_code& ec) {
488 if (ec == boost::asio::error::operation_aborted)
489 {
490 return; // we're being canceled
491 }
492
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800493 if (!createSensors(io, objectServer, dbusConnection, cpuSensors,
494 cpuConfigs, sensorConfigs))
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700495 {
496 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800497 dbusConnection, cpuSensors, cpuConfigs,
498 sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700499 }
500 });
James Feist6714a252018-09-10 15:26:18 -0700501 }
502
503 if (keepPinging)
504 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800505 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
506 dbusConnection, cpuSensors, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700507 }
508}
509
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700510void detectCpuAsync(
511 boost::asio::deadline_timer& pingTimer,
512 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
513 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800514 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700515 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800516 cpuSensors,
517 boost::container::flat_set<CPUConfig>& cpuConfigs,
518 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700519{
520 pingTimer.expires_from_now(boost::posix_time::seconds(1));
521 pingTimer.async_wait([&](const boost::system::error_code& ec) {
522 if (ec == boost::asio::error::operation_aborted)
523 {
524 return; // we're being canceled
525 }
526
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800527 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
528 cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700529 });
530}
531
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700532bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800533 boost::container::flat_set<CPUConfig>& cpuConfigs,
534 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700535{
James Feist6714a252018-09-10 15:26:18 -0700536 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800537 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700538 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700539 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700540 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700541 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800542 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700543 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700544 return false;
James Feist6714a252018-09-10 15:26:18 -0700545 }
546 useCache = true;
547 }
548
549 // check PECI client addresses and DT overlay names from CPU configuration
550 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700551 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700552 {
553 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800554 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700555 {
556 for (const std::pair<
557 std::string,
558 boost::container::flat_map<std::string, BasicVariantType>>&
559 config : sensor.second)
560 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700561 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700562 {
563 continue;
564 }
565
James Feist6714a252018-09-10 15:26:18 -0700566 auto findName = config.second.find("Name");
567 if (findName == config.second.end())
568 {
569 continue;
570 }
James Feist3eb82622019-02-08 13:10:22 -0800571 std::string nameRaw =
572 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700573 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700574 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700575
576 auto findBus = config.second.find("Bus");
577 if (findBus == config.second.end())
578 {
579 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
580 continue;
581 }
James Feist3eb82622019-02-08 13:10:22 -0800582 uint64_t bus =
583 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700584
585 auto findAddress = config.second.find("Address");
586 if (findAddress == config.second.end())
587 {
588 std::cerr << "Can't find 'Address' setting in " << name
589 << "\n";
590 continue;
591 }
James Feist3eb82622019-02-08 13:10:22 -0800592 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
593 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700594
595 if (DEBUG)
596 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700597 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700598 std::cout << "addr: " << addr << "\n";
599 std::cout << "name: " << name << "\n";
600 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700601 }
602
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800603 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700604 }
605 }
606 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700607
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800608 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700609 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800610 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700611 << " parsed\n";
612 return true;
613 }
614
615 return false;
James Feist6714a252018-09-10 15:26:18 -0700616}
617
618int main(int argc, char** argv)
619{
620 boost::asio::io_service io;
621 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800622 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700623
624 systemBus->request_name("xyz.openbmc_project.CPUSensor");
625 sdbusplus::asio::object_server objectServer(systemBus);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800626 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>
627 cpuSensors;
James Feist6714a252018-09-10 15:26:18 -0700628 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
629 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700630 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700631 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800632 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700633
634 filterTimer.expires_from_now(boost::posix_time::seconds(1));
635 filterTimer.async_wait([&](const boost::system::error_code& ec) {
636 if (ec == boost::asio::error::operation_aborted)
637 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700638 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700639 }
640
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800641 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700642 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800643 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
644 systemBus, cpuSensors, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700645 }
646 });
647
James Feist6714a252018-09-10 15:26:18 -0700648 std::function<void(sdbusplus::message::message&)> eventHandler =
649 [&](sdbusplus::message::message& message) {
650 if (message.is_method_error())
651 {
652 std::cerr << "callback method error\n";
653 return;
654 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700655
656 if (DEBUG)
657 {
658 std::cout << message.get_path() << " is changed\n";
659 }
660
James Feist6714a252018-09-10 15:26:18 -0700661 // this implicitly cancels the timer
662 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700663 filterTimer.async_wait([&](const boost::system::error_code& ec) {
664 if (ec == boost::asio::error::operation_aborted)
665 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700666 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700667 }
668
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800669 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
James Feist6714a252018-09-10 15:26:18 -0700670 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700671 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800672 systemBus, cpuSensors, cpuConfigs,
673 sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700674 }
675 });
676 };
677
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700678 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700679 {
680 auto match = std::make_unique<sdbusplus::bus::match::match>(
681 static_cast<sdbusplus::bus::bus&>(*systemBus),
682 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700683 std::string(inventoryPath) + "',arg0namespace='" +
684 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700685 eventHandler);
686 matches.emplace_back(std::move(match));
687 }
688
689 io.run();
690}