blob: 2588221ae2d7823b2ee348337415bf80ef71e8ef [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 }
James Feistb6c0b912019-07-09 12:21:44 -0700157 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700158 {
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;
James Feistb6c0b912019-07-09 12:21:44 -0700246 if (!findFiles(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
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700292 /*
293 * Find if there is DtsCritOffset is configured in config file
294 * set it if configured or else set it to 0
295 */
296 double dtsOffset = 0;
297 if (label == "DTS")
298 {
299 auto findThrOffset =
300 baseConfiguration->second.find("DtsCritOffset");
301 if (findThrOffset != baseConfiguration->second.end())
302 {
303 dtsOffset = std::visit(VariantToDoubleVisitor(),
304 findThrOffset->second);
305 }
306 }
307
James Feist6714a252018-09-10 15:26:18 -0700308 std::vector<thresholds::Threshold> sensorThresholds;
309 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700310 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700311 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800312 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700313 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700314 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700315 CPUSensor::sensorScaleFactor,
316 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700317 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700318 std::cerr << "error populating thresholds for "
319 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700320 }
321 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800322 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700323 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800324 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700325 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700326 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700327 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700328 {
James Feist6714a252018-09-10 15:26:18 -0700329 std::cout << "Mapped: " << inputPath << " to " << sensorName
330 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331 }
James Feist6714a252018-09-10 15:26:18 -0700332 }
333 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700334
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700335 if (createdSensors.size())
336 {
337 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
338 << " created\n";
339 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700340
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700341 return true;
James Feist6714a252018-09-10 15:26:18 -0700342}
343
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700344void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700345{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700346 std::ostringstream hex;
347 hex << std::hex << config.addr;
348 const std::string& addrHexStr = hex.str();
349 std::string busStr = std::to_string(config.bus);
350
351 std::string parameters = "peci-client 0x" + addrHexStr;
352 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
353
James Feistcf3bce62019-01-08 10:07:19 -0800354 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700355 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800356 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700357 {
James Feistcf3bce62019-01-08 10:07:19 -0800358 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700359 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700360 continue;
James Feist6714a252018-09-10 15:26:18 -0700361 }
362
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700363 const std::string& directoryName = path.path().filename();
364 if (boost::starts_with(directoryName, busStr) &&
365 boost::ends_with(directoryName, addrHexStr))
366 {
367 if (DEBUG)
368 {
369 std::cout << parameters << " on bus " << busStr
370 << " is already exported\n";
371 }
372 return;
373 }
James Feist6714a252018-09-10 15:26:18 -0700374 }
375
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700376 std::ofstream deviceFile(device);
377 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700378 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700379 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700380 return;
381 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700382 deviceFile << parameters;
383 deviceFile.close();
384
385 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700386}
387
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800388void detectCpu(boost::asio::deadline_timer& pingTimer,
389 boost::asio::deadline_timer& creationTimer,
390 boost::asio::io_service& io,
391 sdbusplus::asio::object_server& objectServer,
392 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
393 boost::container::flat_set<CPUConfig>& cpuConfigs,
394 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700395{
James Feist6714a252018-09-10 15:26:18 -0700396 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700397 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700398
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800399 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700400 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700401 std::string peciDevPath = peciDev + std::to_string(config.bus);
402 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
403 if (file < 0)
404 {
405 std::cerr << "unable to open " << peciDevPath << "\n";
406 std::exit(EXIT_FAILURE);
407 }
408
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700409 State newState;
James Feist6714a252018-09-10 15:26:18 -0700410 struct peci_ping_msg msg;
411 msg.addr = config.addr;
412 if (!ioctl(file, PECI_IOC_PING, &msg))
413 {
414 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700415 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700416 {
417 struct peci_rd_pkg_cfg_msg msg;
418 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800419 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700420 msg.param = rank;
421 msg.rx_len = 4;
422 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
423 {
424 if (msg.pkg_config[0] || msg.pkg_config[1] ||
425 msg.pkg_config[2])
426 {
427 dimmReady = true;
428 break;
429 }
430 }
431 else
432 {
433 break;
434 }
435 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700436
James Feist6714a252018-09-10 15:26:18 -0700437 if (dimmReady)
438 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700439 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700440 }
441 else
442 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700443 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700444 }
445 }
446 else
447 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700448 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700449 }
450
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700451 close(file);
452
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700453 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700454 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700455 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700456 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700457 if (config.state == State::OFF)
458 {
459 std::cout << config.name << " is detected\n";
460 exportDevice(config);
461 }
James Feist6714a252018-09-10 15:26:18 -0700462
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700463 if (newState == State::ON)
464 {
465 rescanDelaySeconds = 3;
466 }
467 else if (newState == State::READY)
468 {
469 rescanDelaySeconds = 5;
470 std::cout << "DIMM(s) on " << config.name
471 << " is/are detected\n";
472 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700473 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700474
475 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700476 }
477
478 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700479 {
480 keepPinging = true;
481 }
482
483 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700484 {
485 std::cout << config.name << ", state: " << config.state << "\n";
486 }
James Feist6714a252018-09-10 15:26:18 -0700487 }
488
James Feist6714a252018-09-10 15:26:18 -0700489 if (rescanDelaySeconds)
490 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700491 creationTimer.expires_from_now(
492 boost::posix_time::seconds(rescanDelaySeconds));
493 creationTimer.async_wait([&](const boost::system::error_code& ec) {
494 if (ec == boost::asio::error::operation_aborted)
495 {
496 return; // we're being canceled
497 }
498
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800499 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700500 sensorConfigs) ||
501 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700502 {
503 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800504 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700505 }
506 });
James Feist6714a252018-09-10 15:26:18 -0700507 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700508 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700509 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800510 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800511 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700512 }
513}
514
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700515void detectCpuAsync(
516 boost::asio::deadline_timer& pingTimer,
517 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
518 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800519 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800520 boost::container::flat_set<CPUConfig>& cpuConfigs,
521 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700522{
523 pingTimer.expires_from_now(boost::posix_time::seconds(1));
524 pingTimer.async_wait([&](const boost::system::error_code& ec) {
525 if (ec == boost::asio::error::operation_aborted)
526 {
527 return; // we're being canceled
528 }
529
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800530 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800531 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700532 });
533}
534
James Feist58295ad2019-05-30 15:01:41 -0700535bool getCpuConfig(
536 const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
537 boost::container::flat_set<CPUConfig>& cpuConfigs,
538 ManagedObjectType& sensorConfigs,
539 sdbusplus::asio::object_server& objectServer,
540 boost::container::flat_map<
541 std::string, std::shared_ptr<sdbusplus::asio::dbus_interface>>&
542 inventoryIfaces)
James Feist6714a252018-09-10 15:26:18 -0700543{
James Feist6714a252018-09-10 15:26:18 -0700544 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800545 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700546 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700547 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700548 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700549 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800550 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700551 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700552 return false;
James Feist6714a252018-09-10 15:26:18 -0700553 }
554 useCache = true;
555 }
556
557 // check PECI client addresses and DT overlay names from CPU configuration
558 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700559 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700560 {
561 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800562 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700563 {
564 for (const std::pair<
565 std::string,
566 boost::container::flat_map<std::string, BasicVariantType>>&
567 config : sensor.second)
568 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700569 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700570 {
571 continue;
572 }
573
James Feist6714a252018-09-10 15:26:18 -0700574 auto findName = config.second.find("Name");
575 if (findName == config.second.end())
576 {
577 continue;
578 }
James Feist3eb82622019-02-08 13:10:22 -0800579 std::string nameRaw =
580 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700581 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700582 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700583
James Feist58295ad2019-05-30 15:01:41 -0700584 auto findCpuGpio = config.second.find("PresenceGpio");
585 if (findCpuGpio != config.second.end())
586 {
587 size_t gpio = std::visit(VariantToUnsignedIntVisitor(),
588 findCpuGpio->second);
589 bool present = hostIsPresent(gpio);
590 if (inventoryIfaces.find(name) == inventoryIfaces.end())
591 {
592 auto iface = objectServer.add_interface(
593 cpuInventoryPath + std::string("/") + name,
594 "xyz.openbmc_project.Inventory.Item");
595 iface->register_property("PrettyName", name);
596 iface->register_property("Present", present);
597 iface->initialize();
598 inventoryIfaces[name] = std::move(iface);
599 }
600 if (!present)
601 {
602 continue; // no reason to look for non present cpu
603 }
604 }
605
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700606 auto findBus = config.second.find("Bus");
607 if (findBus == config.second.end())
608 {
609 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
610 continue;
611 }
James Feist3eb82622019-02-08 13:10:22 -0800612 uint64_t bus =
613 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700614
615 auto findAddress = config.second.find("Address");
616 if (findAddress == config.second.end())
617 {
618 std::cerr << "Can't find 'Address' setting in " << name
619 << "\n";
620 continue;
621 }
James Feist3eb82622019-02-08 13:10:22 -0800622 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
623 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700624
625 if (DEBUG)
626 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700627 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700628 std::cout << "addr: " << addr << "\n";
629 std::cout << "name: " << name << "\n";
630 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700631 }
632
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800633 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700634 }
635 }
636 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700637
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800638 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700639 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800640 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700641 << " parsed\n";
642 return true;
643 }
644
645 return false;
James Feist6714a252018-09-10 15:26:18 -0700646}
647
James Feistb6c0b912019-07-09 12:21:44 -0700648int main()
James Feist6714a252018-09-10 15:26:18 -0700649{
650 boost::asio::io_service io;
651 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800652 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700653
654 systemBus->request_name("xyz.openbmc_project.CPUSensor");
655 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700656 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
657 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700658 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700659 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800660 ManagedObjectType sensorConfigs;
James Feist58295ad2019-05-30 15:01:41 -0700661 boost::container::flat_map<std::string,
662 std::shared_ptr<sdbusplus::asio::dbus_interface>>
663 inventoryIfaces;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700664
665 filterTimer.expires_from_now(boost::posix_time::seconds(1));
666 filterTimer.async_wait([&](const boost::system::error_code& ec) {
667 if (ec == boost::asio::error::operation_aborted)
668 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700669 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700670 }
671
James Feist58295ad2019-05-30 15:01:41 -0700672 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer,
673 inventoryIfaces))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700674 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800675 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800676 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700677 }
678 });
679
James Feist6714a252018-09-10 15:26:18 -0700680 std::function<void(sdbusplus::message::message&)> eventHandler =
681 [&](sdbusplus::message::message& message) {
682 if (message.is_method_error())
683 {
684 std::cerr << "callback method error\n";
685 return;
686 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700687
688 if (DEBUG)
689 {
690 std::cout << message.get_path() << " is changed\n";
691 }
692
James Feist6714a252018-09-10 15:26:18 -0700693 // this implicitly cancels the timer
694 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700695 filterTimer.async_wait([&](const boost::system::error_code& ec) {
696 if (ec == boost::asio::error::operation_aborted)
697 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700698 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700699 }
700
James Feist58295ad2019-05-30 15:01:41 -0700701 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
702 objectServer, inventoryIfaces))
James Feist6714a252018-09-10 15:26:18 -0700703 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700704 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800705 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700706 }
707 });
708 };
709
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700710 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700711 {
712 auto match = std::make_unique<sdbusplus::bus::match::match>(
713 static_cast<sdbusplus::bus::bus&>(*systemBus),
714 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700715 std::string(inventoryPath) + "',arg0namespace='" +
716 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700717 eventHandler);
718 matches.emplace_back(std::move(match));
719 }
720
721 io.run();
722}