blob: 858ab243e5a8f664b2902bebace81b80355f76fe [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
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors;
45
James Feist6714a252018-09-10 15:26:18 -070046enum State
47{
48 OFF, // host powered down
49 ON, // host powered on
50 READY // host powered on and mem test passed - fully ready
51};
52
53struct CPUConfig
54{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070055 CPUConfig(const uint64_t& bus, const uint64_t& addr,
56 const std::string& name, const State& state) :
57 bus(bus),
58 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070059 {
60 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070061 int bus;
James Feist6714a252018-09-10 15:26:18 -070062 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070063 std::string name;
James Feist6714a252018-09-10 15:26:18 -070064 State state;
65
66 bool operator<(const CPUConfig& rhs) const
67 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070068 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070069 }
70};
71
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070072static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070074
James Feistcf3bce62019-01-08 10:07:19 -080075namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080076
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070078 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070080 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080081static constexpr std::array<const char*, 3> hiddenProps = {
82 CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070083
Jae Hyun Yood64262b2018-11-01 13:31:16 -070084void detectCpuAsync(
85 boost::asio::deadline_timer& pingTimer,
86 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
87 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080088 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080089 boost::container::flat_set<CPUConfig>& cpuConfigs,
90 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070091
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080092bool createSensors(boost::asio::io_service& io,
93 sdbusplus::asio::object_server& objectServer,
94 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
95 boost::container::flat_set<CPUConfig>& cpuConfigs,
96 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070097{
98 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080099 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700100 {
101 if (cpu.state != State::OFF)
102 {
103 available = true;
104 break;
105 }
106 }
107 if (!available)
108 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700109 return false;
James Feist6714a252018-09-10 15:26:18 -0700110 }
111
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700113 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800114 return false;
James Feist6714a252018-09-10 15:26:18 -0700115 }
116
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700117 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
119 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
120 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700121 {
122 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700123 return true;
James Feist6714a252018-09-10 15:26:18 -0700124 }
125
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700126 boost::container::flat_set<std::string> scannedDirectories;
127 boost::container::flat_set<std::string> createdSensors;
128
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800129 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700130 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700131 const std::string& pathStr = hwmonNamePath.string();
132 auto hwmonDirectory = hwmonNamePath.parent_path();
133
134 auto ret = scannedDirectories.insert(hwmonDirectory.string());
135 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700136 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700137 continue; // already searched this path
138 }
139
140 fs::path::iterator it = hwmonNamePath.begin();
141 std::advance(it, 6); // pick the 6th part for a PECI client device name
142 std::string deviceName = *it;
143 auto findHyphen = deviceName.find("-");
144 if (findHyphen == std::string::npos)
145 {
146 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700147 continue;
148 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700149 std::string busStr = deviceName.substr(0, findHyphen);
150 std::string addrStr = deviceName.substr(findHyphen + 1);
151
152 size_t bus = 0;
153 size_t addr = 0;
154 try
155 {
156 bus = std::stoi(busStr);
157 addr = std::stoi(addrStr, 0, 16);
158 }
159 catch (std::invalid_argument)
160 {
161 continue;
162 }
163
164 std::ifstream nameFile(hwmonNamePath);
165 if (!nameFile.good())
166 {
167 std::cerr << "Failure reading " << hwmonNamePath << "\n";
168 continue;
169 }
170 std::string hwmonName;
171 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700172 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800173 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700174 {
175 // shouldn't have an empty name file
176 continue;
177 }
James Feist6714a252018-09-10 15:26:18 -0700178 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700179 {
180 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
181 << "\n";
182 }
James Feist6714a252018-09-10 15:26:18 -0700183
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700184 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700185 const SensorData* sensorData = nullptr;
186 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700187 const std::pair<std::string, boost::container::flat_map<
188 std::string, BasicVariantType>>*
189 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700190
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700191 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800192 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700193 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700196 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700197 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700198 auto sensorBase = sensorData->find(sensorType);
199 if (sensorBase != sensorData->end())
200 {
201 baseConfiguration = &(*sensorBase);
202 break;
203 }
204 }
205 if (baseConfiguration == nullptr)
206 {
207 std::cerr << "error finding base configuration for" << hwmonName
208 << "\n";
209 continue;
210 }
211 auto configurationBus = baseConfiguration->second.find("Bus");
212 auto configurationAddress =
213 baseConfiguration->second.find("Address");
214
215 if (configurationBus == baseConfiguration->second.end() ||
216 configurationAddress == baseConfiguration->second.end())
217 {
218 std::cerr << "error finding bus or address in configuration";
219 continue;
220 }
221
James Feist3eb82622019-02-08 13:10:22 -0800222 if (std::get<uint64_t>(configurationBus->second) != bus ||
223 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700224 {
225 continue;
226 }
227
228 interfacePath = &(sensor.first.str);
229 break;
230 }
231 if (interfacePath == nullptr)
232 {
233 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700234 continue;
235 }
236
237 auto findCpuId = baseConfiguration->second.find("CpuID");
238 if (findCpuId == baseConfiguration->second.end())
239 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700240 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700241 continue;
242 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800244 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700245
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700247 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700248 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700249 {
250 std::cerr << "No temperature sensors in system\n";
251 continue;
252 }
253
254 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800255 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700256 {
257 auto inputPathStr = inputPath.string();
258 auto labelPath =
259 boost::replace_all_copy(inputPathStr, "input", "label");
260 std::ifstream labelFile(labelPath);
261 if (!labelFile.good())
262 {
263 std::cerr << "Failure reading " << labelPath << "\n";
264 continue;
265 }
266 std::string label;
267 std::getline(labelFile, label);
268 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800269
James Feist6714a252018-09-10 15:26:18 -0700270 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700271
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800272 auto findSensor = gCpuSensors.find(sensorName);
273 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700274 {
275 if (DEBUG)
276 {
277 std::cout << "Skipped: " << inputPath << ": " << sensorName
278 << " is already created\n";
279 }
280 continue;
281 }
282
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800283 // check hidden properties
284 bool show = true;
285 for (const char* prop : hiddenProps)
286 {
287 if (label == prop)
288 {
289 show = false;
290 break;
291 }
292 }
293
James Feist6714a252018-09-10 15:26:18 -0700294 std::vector<thresholds::Threshold> sensorThresholds;
295 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700296 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700297 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800298 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700299 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700300 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
301 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700302 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700303 std::cerr << "error populating thresholds for "
304 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700305 }
306 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800307 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700308 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800309 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
310 show);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700311 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700312 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700313 {
James Feist6714a252018-09-10 15:26:18 -0700314 std::cout << "Mapped: " << inputPath << " to " << sensorName
315 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700316 }
James Feist6714a252018-09-10 15:26:18 -0700317 }
318 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700319
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700320 if (createdSensors.size())
321 {
322 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
323 << " created\n";
324 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700325
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700326 return true;
James Feist6714a252018-09-10 15:26:18 -0700327}
328
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700329void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700330{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 std::ostringstream hex;
332 hex << std::hex << config.addr;
333 const std::string& addrHexStr = hex.str();
334 std::string busStr = std::to_string(config.bus);
335
336 std::string parameters = "peci-client 0x" + addrHexStr;
337 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
338
James Feistcf3bce62019-01-08 10:07:19 -0800339 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700340 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800341 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700342 {
James Feistcf3bce62019-01-08 10:07:19 -0800343 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700344 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700345 continue;
James Feist6714a252018-09-10 15:26:18 -0700346 }
347
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700348 const std::string& directoryName = path.path().filename();
349 if (boost::starts_with(directoryName, busStr) &&
350 boost::ends_with(directoryName, addrHexStr))
351 {
352 if (DEBUG)
353 {
354 std::cout << parameters << " on bus " << busStr
355 << " is already exported\n";
356 }
357 return;
358 }
James Feist6714a252018-09-10 15:26:18 -0700359 }
360
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700361 std::ofstream deviceFile(device);
362 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700363 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700364 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700365 return;
366 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700367 deviceFile << parameters;
368 deviceFile.close();
369
370 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700371}
372
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800373void detectCpu(boost::asio::deadline_timer& pingTimer,
374 boost::asio::deadline_timer& creationTimer,
375 boost::asio::io_service& io,
376 sdbusplus::asio::object_server& objectServer,
377 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
378 boost::container::flat_set<CPUConfig>& cpuConfigs,
379 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700380{
James Feist6714a252018-09-10 15:26:18 -0700381 size_t rescanDelaySeconds = 0;
382 bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700383
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800384 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700385 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700386 std::string peciDevPath = peciDev + std::to_string(config.bus);
387 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
388 if (file < 0)
389 {
390 std::cerr << "unable to open " << peciDevPath << "\n";
391 std::exit(EXIT_FAILURE);
392 }
393
James Feist6714a252018-09-10 15:26:18 -0700394 State state;
395 struct peci_ping_msg msg;
396 msg.addr = config.addr;
397 if (!ioctl(file, PECI_IOC_PING, &msg))
398 {
399 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700400 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700401 {
402 struct peci_rd_pkg_cfg_msg msg;
403 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800404 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700405 msg.param = rank;
406 msg.rx_len = 4;
407 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
408 {
409 if (msg.pkg_config[0] || msg.pkg_config[1] ||
410 msg.pkg_config[2])
411 {
412 dimmReady = true;
413 break;
414 }
415 }
416 else
417 {
418 break;
419 }
420 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700421
James Feist6714a252018-09-10 15:26:18 -0700422 if (dimmReady)
423 {
424 state = State::READY;
425 }
426 else
427 {
428 state = State::ON;
429 }
430 }
431 else
432 {
433 state = State::OFF;
434 }
435
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700436 close(file);
437
James Feist6714a252018-09-10 15:26:18 -0700438 if (config.state != state)
439 {
440 if (config.state == State::OFF)
441 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700442 std::cout << config.name << " is detected\n";
443 exportDevice(config);
James Feist6714a252018-09-10 15:26:18 -0700444 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700445 if (state == State::READY)
446 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700447 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700448 << " is/are detected\n";
449 }
James Feist6714a252018-09-10 15:26:18 -0700450 config.state = state;
451 }
452
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700453 if (config.state != State::OFF)
454 {
455 if (config.state == State::ON)
456 {
457 rescanDelaySeconds = 1;
458 }
459 else
460 {
461 rescanDelaySeconds = 5;
462 }
463 }
464
465 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700466 {
467 keepPinging = true;
468 }
469
470 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700471 {
472 std::cout << config.name << ", state: " << config.state << "\n";
473 }
James Feist6714a252018-09-10 15:26:18 -0700474 }
475
James Feist6714a252018-09-10 15:26:18 -0700476 if (rescanDelaySeconds)
477 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700478 creationTimer.expires_from_now(
479 boost::posix_time::seconds(rescanDelaySeconds));
480 creationTimer.async_wait([&](const boost::system::error_code& ec) {
481 if (ec == boost::asio::error::operation_aborted)
482 {
483 return; // we're being canceled
484 }
485
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800486 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
487 sensorConfigs))
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700488 {
489 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800490 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700491 }
492 });
James Feist6714a252018-09-10 15:26:18 -0700493 }
494
495 if (keepPinging)
496 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800497 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800498 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700499 }
500}
501
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700502void detectCpuAsync(
503 boost::asio::deadline_timer& pingTimer,
504 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
505 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800506 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800507 boost::container::flat_set<CPUConfig>& cpuConfigs,
508 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700509{
510 pingTimer.expires_from_now(boost::posix_time::seconds(1));
511 pingTimer.async_wait([&](const boost::system::error_code& ec) {
512 if (ec == boost::asio::error::operation_aborted)
513 {
514 return; // we're being canceled
515 }
516
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800517 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800518 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700519 });
520}
521
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700522bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800523 boost::container::flat_set<CPUConfig>& cpuConfigs,
524 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700525{
James Feist6714a252018-09-10 15:26:18 -0700526 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800527 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700528 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700529 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700530 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700531 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800532 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700533 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700534 return false;
James Feist6714a252018-09-10 15:26:18 -0700535 }
536 useCache = true;
537 }
538
539 // check PECI client addresses and DT overlay names from CPU configuration
540 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700541 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700542 {
543 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800544 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700545 {
546 for (const std::pair<
547 std::string,
548 boost::container::flat_map<std::string, BasicVariantType>>&
549 config : sensor.second)
550 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700551 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700552 {
553 continue;
554 }
555
James Feist6714a252018-09-10 15:26:18 -0700556 auto findName = config.second.find("Name");
557 if (findName == config.second.end())
558 {
559 continue;
560 }
James Feist3eb82622019-02-08 13:10:22 -0800561 std::string nameRaw =
562 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700563 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700564 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700565
566 auto findBus = config.second.find("Bus");
567 if (findBus == config.second.end())
568 {
569 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
570 continue;
571 }
James Feist3eb82622019-02-08 13:10:22 -0800572 uint64_t bus =
573 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700574
575 auto findAddress = config.second.find("Address");
576 if (findAddress == config.second.end())
577 {
578 std::cerr << "Can't find 'Address' setting in " << name
579 << "\n";
580 continue;
581 }
James Feist3eb82622019-02-08 13:10:22 -0800582 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
583 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700584
585 if (DEBUG)
586 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700587 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700588 std::cout << "addr: " << addr << "\n";
589 std::cout << "name: " << name << "\n";
590 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700591 }
592
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800593 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700594 }
595 }
596 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700597
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800598 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700599 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800600 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700601 << " parsed\n";
602 return true;
603 }
604
605 return false;
James Feist6714a252018-09-10 15:26:18 -0700606}
607
608int main(int argc, char** argv)
609{
610 boost::asio::io_service io;
611 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800612 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700613
614 systemBus->request_name("xyz.openbmc_project.CPUSensor");
615 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700616 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
617 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700618 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700619 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800620 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700621
622 filterTimer.expires_from_now(boost::posix_time::seconds(1));
623 filterTimer.async_wait([&](const boost::system::error_code& ec) {
624 if (ec == boost::asio::error::operation_aborted)
625 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700626 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700627 }
628
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800629 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700630 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800631 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800632 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700633 }
634 });
635
James Feist6714a252018-09-10 15:26:18 -0700636 std::function<void(sdbusplus::message::message&)> eventHandler =
637 [&](sdbusplus::message::message& message) {
638 if (message.is_method_error())
639 {
640 std::cerr << "callback method error\n";
641 return;
642 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700643
644 if (DEBUG)
645 {
646 std::cout << message.get_path() << " is changed\n";
647 }
648
James Feist6714a252018-09-10 15:26:18 -0700649 // this implicitly cancels the timer
650 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700651 filterTimer.async_wait([&](const boost::system::error_code& ec) {
652 if (ec == boost::asio::error::operation_aborted)
653 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700654 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700655 }
656
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800657 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
James Feist6714a252018-09-10 15:26:18 -0700658 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700659 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800660 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700661 }
662 });
663 };
664
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700665 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700666 {
667 auto match = std::make_unique<sdbusplus::bus::match::match>(
668 static_cast<sdbusplus::bus::bus&>(*systemBus),
669 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700670 std::string(inventoryPath) + "',arg0namespace='" +
671 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700672 eventHandler);
673 matches.emplace_back(std::move(match));
674 }
675
676 io.run();
677}