blob: d16cf8a30924be951a5c7e78bf3035ae3114ad85 [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
17#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070018
19#include <CPUSensor.hpp>
20#include <Utils.hpp>
21#include <VariantVisitors.hpp>
22#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
26#include <boost/process/child.hpp>
James Feist24f02f22019-04-15 11:05:39 -070027#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070028#include <fstream>
29#include <regex>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
James Feistf87dc4c2018-12-05 14:39:51 -080033// clang-format off
34// this needs to be included last or we'll have build issues
35#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080036#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
37#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
38#endif
James Feistf87dc4c2018-12-05 14:39:51 -080039// clang-format on
40
James Feist6714a252018-09-10 15:26:18 -070041static constexpr bool DEBUG = false;
42
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080043boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors;
44
James Feist6714a252018-09-10 15:26:18 -070045enum State
46{
47 OFF, // host powered down
48 ON, // host powered on
49 READY // host powered on and mem test passed - fully ready
50};
51
52struct CPUConfig
53{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070054 CPUConfig(const uint64_t& bus, const uint64_t& addr,
55 const std::string& name, const State& state) :
56 bus(bus),
57 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070058 {
59 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070060 int bus;
James Feist6714a252018-09-10 15:26:18 -070061 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070062 std::string name;
James Feist6714a252018-09-10 15:26:18 -070063 State state;
64
65 bool operator<(const CPUConfig& rhs) const
66 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070067 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070068 }
69};
70
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070071static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070073
James Feistcf3bce62019-01-08 10:07:19 -080074namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080075
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070076static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070077 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo7d47bf52019-04-23 16:43:50 -070078static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080079static constexpr std::array<const char*, 3> hiddenProps = {
80 CPUSensor::labelTcontrol, "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 Yooe8b60d02019-01-14 15:27:13 -080087 boost::container::flat_set<CPUConfig>& cpuConfigs,
88 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070089
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080090bool createSensors(boost::asio::io_service& io,
91 sdbusplus::asio::object_server& objectServer,
92 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
93 boost::container::flat_set<CPUConfig>& cpuConfigs,
94 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070095{
96 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080097 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -070098 {
99 if (cpu.state != State::OFF)
100 {
101 available = true;
102 break;
103 }
104 }
105 if (!available)
106 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700107 return false;
James Feist6714a252018-09-10 15:26:18 -0700108 }
109
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800110 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700111 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 return false;
James Feist6714a252018-09-10 15:26:18 -0700113 }
114
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700115 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
117 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
118 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700119 {
120 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700121 return true;
James Feist6714a252018-09-10 15:26:18 -0700122 }
123
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700124 boost::container::flat_set<std::string> scannedDirectories;
125 boost::container::flat_set<std::string> createdSensors;
126
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800127 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700128 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700129 const std::string& pathStr = hwmonNamePath.string();
130 auto hwmonDirectory = hwmonNamePath.parent_path();
131
132 auto ret = scannedDirectories.insert(hwmonDirectory.string());
133 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700134 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700135 continue; // already searched this path
136 }
137
138 fs::path::iterator it = hwmonNamePath.begin();
139 std::advance(it, 6); // pick the 6th part for a PECI client device name
140 std::string deviceName = *it;
141 auto findHyphen = deviceName.find("-");
142 if (findHyphen == std::string::npos)
143 {
144 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700145 continue;
146 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700147 std::string busStr = deviceName.substr(0, findHyphen);
148 std::string addrStr = deviceName.substr(findHyphen + 1);
149
150 size_t bus = 0;
151 size_t addr = 0;
152 try
153 {
154 bus = std::stoi(busStr);
155 addr = std::stoi(addrStr, 0, 16);
156 }
157 catch (std::invalid_argument)
158 {
159 continue;
160 }
161
162 std::ifstream nameFile(hwmonNamePath);
163 if (!nameFile.good())
164 {
165 std::cerr << "Failure reading " << hwmonNamePath << "\n";
166 continue;
167 }
168 std::string hwmonName;
169 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700170 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800171 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700172 {
173 // shouldn't have an empty name file
174 continue;
175 }
James Feist6714a252018-09-10 15:26:18 -0700176 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700177 {
178 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
179 << "\n";
180 }
James Feist6714a252018-09-10 15:26:18 -0700181
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700182 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700183 const SensorData* sensorData = nullptr;
184 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700185 const std::pair<std::string, boost::container::flat_map<
186 std::string, BasicVariantType>>*
187 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700188
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700189 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800190 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700191 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700192 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700196 auto sensorBase = sensorData->find(sensorType);
197 if (sensorBase != sensorData->end())
198 {
199 baseConfiguration = &(*sensorBase);
200 break;
201 }
202 }
203 if (baseConfiguration == nullptr)
204 {
205 std::cerr << "error finding base configuration for" << hwmonName
206 << "\n";
207 continue;
208 }
209 auto configurationBus = baseConfiguration->second.find("Bus");
210 auto configurationAddress =
211 baseConfiguration->second.find("Address");
212
213 if (configurationBus == baseConfiguration->second.end() ||
214 configurationAddress == baseConfiguration->second.end())
215 {
216 std::cerr << "error finding bus or address in configuration";
217 continue;
218 }
219
James Feist3eb82622019-02-08 13:10:22 -0800220 if (std::get<uint64_t>(configurationBus->second) != bus ||
221 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700222 {
223 continue;
224 }
225
226 interfacePath = &(sensor.first.str);
227 break;
228 }
229 if (interfacePath == nullptr)
230 {
231 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700232 continue;
233 }
234
235 auto findCpuId = baseConfiguration->second.find("CpuID");
236 if (findCpuId == baseConfiguration->second.end())
237 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700238 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700239 continue;
240 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700241 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800242 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700243
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700244 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700245 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700246 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700247 {
248 std::cerr << "No temperature sensors in system\n";
249 continue;
250 }
251
252 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800253 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700254 {
255 auto inputPathStr = inputPath.string();
256 auto labelPath =
257 boost::replace_all_copy(inputPathStr, "input", "label");
258 std::ifstream labelFile(labelPath);
259 if (!labelFile.good())
260 {
261 std::cerr << "Failure reading " << labelPath << "\n";
262 continue;
263 }
264 std::string label;
265 std::getline(labelFile, label);
266 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800267
James Feist6714a252018-09-10 15:26:18 -0700268 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700269
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800270 auto findSensor = gCpuSensors.find(sensorName);
271 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700272 {
273 if (DEBUG)
274 {
275 std::cout << "Skipped: " << inputPath << ": " << sensorName
276 << " is already created\n";
277 }
278 continue;
279 }
280
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800281 // check hidden properties
282 bool show = true;
283 for (const char* prop : hiddenProps)
284 {
285 if (label == prop)
286 {
287 show = false;
288 break;
289 }
290 }
291
James Feist6714a252018-09-10 15:26:18 -0700292 std::vector<thresholds::Threshold> sensorThresholds;
293 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700294 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700295 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800296 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700297 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700298 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
299 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700300 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700301 std::cerr << "error populating thresholds for "
302 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700303 }
304 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800305 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700306 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800307 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
308 show);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700309 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700310 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700311 {
James Feist6714a252018-09-10 15:26:18 -0700312 std::cout << "Mapped: " << inputPath << " to " << sensorName
313 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700314 }
James Feist6714a252018-09-10 15:26:18 -0700315 }
316 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700317
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700318 if (createdSensors.size())
319 {
320 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
321 << " created\n";
322 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700323
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700324 return true;
James Feist6714a252018-09-10 15:26:18 -0700325}
326
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700327void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700328{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700329 std::ostringstream hex;
330 hex << std::hex << config.addr;
331 const std::string& addrHexStr = hex.str();
332 std::string busStr = std::to_string(config.bus);
333
334 std::string parameters = "peci-client 0x" + addrHexStr;
335 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
336
James Feistcf3bce62019-01-08 10:07:19 -0800337 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700338 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800339 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700340 {
James Feistcf3bce62019-01-08 10:07:19 -0800341 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700342 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700343 continue;
James Feist6714a252018-09-10 15:26:18 -0700344 }
345
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700346 const std::string& directoryName = path.path().filename();
347 if (boost::starts_with(directoryName, busStr) &&
348 boost::ends_with(directoryName, addrHexStr))
349 {
350 if (DEBUG)
351 {
352 std::cout << parameters << " on bus " << busStr
353 << " is already exported\n";
354 }
355 return;
356 }
James Feist6714a252018-09-10 15:26:18 -0700357 }
358
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700359 std::ofstream deviceFile(device);
360 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700361 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700362 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700363 return;
364 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700365 deviceFile << parameters;
366 deviceFile.close();
367
368 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700369}
370
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800371void detectCpu(boost::asio::deadline_timer& pingTimer,
372 boost::asio::deadline_timer& creationTimer,
373 boost::asio::io_service& io,
374 sdbusplus::asio::object_server& objectServer,
375 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
376 boost::container::flat_set<CPUConfig>& cpuConfigs,
377 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700378{
James Feist6714a252018-09-10 15:26:18 -0700379 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700380 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700381
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800382 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700383 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700384 std::string peciDevPath = peciDev + std::to_string(config.bus);
385 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
386 if (file < 0)
387 {
388 std::cerr << "unable to open " << peciDevPath << "\n";
389 std::exit(EXIT_FAILURE);
390 }
391
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700392 State newState;
James Feist6714a252018-09-10 15:26:18 -0700393 struct peci_ping_msg msg;
394 msg.addr = config.addr;
395 if (!ioctl(file, PECI_IOC_PING, &msg))
396 {
397 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700398 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700399 {
400 struct peci_rd_pkg_cfg_msg msg;
401 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800402 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700403 msg.param = rank;
404 msg.rx_len = 4;
405 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
406 {
407 if (msg.pkg_config[0] || msg.pkg_config[1] ||
408 msg.pkg_config[2])
409 {
410 dimmReady = true;
411 break;
412 }
413 }
414 else
415 {
416 break;
417 }
418 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700419
James Feist6714a252018-09-10 15:26:18 -0700420 if (dimmReady)
421 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700422 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700423 }
424 else
425 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700426 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700427 }
428 }
429 else
430 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700431 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700432 }
433
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700434 close(file);
435
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700436 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700437 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700438 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700439 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700440 if (config.state == State::OFF)
441 {
442 std::cout << config.name << " is detected\n";
443 exportDevice(config);
444 }
James Feist6714a252018-09-10 15:26:18 -0700445
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700446 if (newState == State::ON)
447 {
448 rescanDelaySeconds = 3;
449 }
450 else if (newState == State::READY)
451 {
452 rescanDelaySeconds = 5;
453 std::cout << "DIMM(s) on " << config.name
454 << " is/are detected\n";
455 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700456 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700457
458 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700459 }
460
461 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700462 {
463 keepPinging = true;
464 }
465
466 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700467 {
468 std::cout << config.name << ", state: " << config.state << "\n";
469 }
James Feist6714a252018-09-10 15:26:18 -0700470 }
471
James Feist6714a252018-09-10 15:26:18 -0700472 if (rescanDelaySeconds)
473 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700474 creationTimer.expires_from_now(
475 boost::posix_time::seconds(rescanDelaySeconds));
476 creationTimer.async_wait([&](const boost::system::error_code& ec) {
477 if (ec == boost::asio::error::operation_aborted)
478 {
479 return; // we're being canceled
480 }
481
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800482 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700483 sensorConfigs) ||
484 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700485 {
486 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800487 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700488 }
489 });
James Feist6714a252018-09-10 15:26:18 -0700490 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700491 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700492 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800493 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800494 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700495 }
496}
497
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700498void detectCpuAsync(
499 boost::asio::deadline_timer& pingTimer,
500 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
501 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800502 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800503 boost::container::flat_set<CPUConfig>& cpuConfigs,
504 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700505{
506 pingTimer.expires_from_now(boost::posix_time::seconds(1));
507 pingTimer.async_wait([&](const boost::system::error_code& ec) {
508 if (ec == boost::asio::error::operation_aborted)
509 {
510 return; // we're being canceled
511 }
512
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800513 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800514 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700515 });
516}
517
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700518bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800519 boost::container::flat_set<CPUConfig>& cpuConfigs,
520 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700521{
James Feist6714a252018-09-10 15:26:18 -0700522 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800523 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700524 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700525 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700526 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700527 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800528 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700529 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700530 return false;
James Feist6714a252018-09-10 15:26:18 -0700531 }
532 useCache = true;
533 }
534
535 // check PECI client addresses and DT overlay names from CPU configuration
536 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700537 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700538 {
539 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800540 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700541 {
542 for (const std::pair<
543 std::string,
544 boost::container::flat_map<std::string, BasicVariantType>>&
545 config : sensor.second)
546 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700547 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700548 {
549 continue;
550 }
551
James Feist6714a252018-09-10 15:26:18 -0700552 auto findName = config.second.find("Name");
553 if (findName == config.second.end())
554 {
555 continue;
556 }
James Feist3eb82622019-02-08 13:10:22 -0800557 std::string nameRaw =
558 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700559 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700560 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700561
562 auto findBus = config.second.find("Bus");
563 if (findBus == config.second.end())
564 {
565 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
566 continue;
567 }
James Feist3eb82622019-02-08 13:10:22 -0800568 uint64_t bus =
569 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700570
571 auto findAddress = config.second.find("Address");
572 if (findAddress == config.second.end())
573 {
574 std::cerr << "Can't find 'Address' setting in " << name
575 << "\n";
576 continue;
577 }
James Feist3eb82622019-02-08 13:10:22 -0800578 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
579 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700580
581 if (DEBUG)
582 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700583 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700584 std::cout << "addr: " << addr << "\n";
585 std::cout << "name: " << name << "\n";
586 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700587 }
588
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800589 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700590 }
591 }
592 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700593
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800594 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700595 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800596 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700597 << " parsed\n";
598 return true;
599 }
600
601 return false;
James Feist6714a252018-09-10 15:26:18 -0700602}
603
604int main(int argc, char** argv)
605{
606 boost::asio::io_service io;
607 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800608 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700609
610 systemBus->request_name("xyz.openbmc_project.CPUSensor");
611 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700612 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
613 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700614 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700615 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800616 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700617
618 filterTimer.expires_from_now(boost::posix_time::seconds(1));
619 filterTimer.async_wait([&](const boost::system::error_code& ec) {
620 if (ec == boost::asio::error::operation_aborted)
621 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700622 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700623 }
624
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800625 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700626 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800627 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800628 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700629 }
630 });
631
James Feist6714a252018-09-10 15:26:18 -0700632 std::function<void(sdbusplus::message::message&)> eventHandler =
633 [&](sdbusplus::message::message& message) {
634 if (message.is_method_error())
635 {
636 std::cerr << "callback method error\n";
637 return;
638 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700639
640 if (DEBUG)
641 {
642 std::cout << message.get_path() << " is changed\n";
643 }
644
James Feist6714a252018-09-10 15:26:18 -0700645 // this implicitly cancels the timer
646 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700647 filterTimer.async_wait([&](const boost::system::error_code& ec) {
648 if (ec == boost::asio::error::operation_aborted)
649 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700650 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700651 }
652
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800653 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs))
James Feist6714a252018-09-10 15:26:18 -0700654 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700655 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800656 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700657 }
658 });
659 };
660
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700661 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700662 {
663 auto match = std::make_unique<sdbusplus::bus::match::match>(
664 static_cast<sdbusplus::bus::bus&>(*systemBus),
665 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700666 std::string(inventoryPath) + "',arg0namespace='" +
667 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700668 eventHandler);
669 matches.emplace_back(std::move(match));
670 }
671
672 io.run();
673}