blob: 2a3ff16f8294bd92442b92e292d6e3435bfbea1e [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;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700185 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700186
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700187 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800188 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700189 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700190 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700191 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700192 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 auto sensorBase = sensorData->find(sensorType);
195 if (sensorBase != sensorData->end())
196 {
197 baseConfiguration = &(*sensorBase);
198 break;
199 }
200 }
201 if (baseConfiguration == nullptr)
202 {
203 std::cerr << "error finding base configuration for" << hwmonName
204 << "\n";
205 continue;
206 }
207 auto configurationBus = baseConfiguration->second.find("Bus");
208 auto configurationAddress =
209 baseConfiguration->second.find("Address");
210
211 if (configurationBus == baseConfiguration->second.end() ||
212 configurationAddress == baseConfiguration->second.end())
213 {
214 std::cerr << "error finding bus or address in configuration";
215 continue;
216 }
217
James Feist3eb82622019-02-08 13:10:22 -0800218 if (std::get<uint64_t>(configurationBus->second) != bus ||
219 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700220 {
221 continue;
222 }
223
224 interfacePath = &(sensor.first.str);
225 break;
226 }
227 if (interfacePath == nullptr)
228 {
229 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700230 continue;
231 }
232
233 auto findCpuId = baseConfiguration->second.find("CpuID");
234 if (findCpuId == baseConfiguration->second.end())
235 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700236 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700237 continue;
238 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700239 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800240 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700241
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700243 std::vector<fs::path> inputPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700244 if (!findFiles(directory, R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700245 {
246 std::cerr << "No temperature sensors in system\n";
247 continue;
248 }
249
250 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800251 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700252 {
253 auto inputPathStr = inputPath.string();
254 auto labelPath =
255 boost::replace_all_copy(inputPathStr, "input", "label");
256 std::ifstream labelFile(labelPath);
257 if (!labelFile.good())
258 {
259 std::cerr << "Failure reading " << labelPath << "\n";
260 continue;
261 }
262 std::string label;
263 std::getline(labelFile, label);
264 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800265
James Feist6714a252018-09-10 15:26:18 -0700266 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700267
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800268 auto findSensor = gCpuSensors.find(sensorName);
269 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700270 {
271 if (DEBUG)
272 {
273 std::cout << "Skipped: " << inputPath << ": " << sensorName
274 << " is already created\n";
275 }
276 continue;
277 }
278
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800279 // check hidden properties
280 bool show = true;
281 for (const char* prop : hiddenProps)
282 {
283 if (label == prop)
284 {
285 show = false;
286 break;
287 }
288 }
289
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700290 /*
291 * Find if there is DtsCritOffset is configured in config file
292 * set it if configured or else set it to 0
293 */
294 double dtsOffset = 0;
295 if (label == "DTS")
296 {
297 auto findThrOffset =
298 baseConfiguration->second.find("DtsCritOffset");
299 if (findThrOffset != baseConfiguration->second.end())
300 {
301 dtsOffset = std::visit(VariantToDoubleVisitor(),
302 findThrOffset->second);
303 }
304 }
305
James Feist6714a252018-09-10 15:26:18 -0700306 std::vector<thresholds::Threshold> sensorThresholds;
307 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700308 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700309 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800310 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700311 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700312 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700313 CPUSensor::sensorScaleFactor,
314 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700315 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700316 std::cerr << "error populating thresholds for "
317 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700318 }
319 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800320 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700321 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800322 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700323 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700324 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700325 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700326 {
James Feist6714a252018-09-10 15:26:18 -0700327 std::cout << "Mapped: " << inputPath << " to " << sensorName
328 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700329 }
James Feist6714a252018-09-10 15:26:18 -0700330 }
331 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700332
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 if (createdSensors.size())
334 {
335 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
336 << " created\n";
337 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700338
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700339 return true;
James Feist6714a252018-09-10 15:26:18 -0700340}
341
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700342void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700343{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700344 std::ostringstream hex;
345 hex << std::hex << config.addr;
346 const std::string& addrHexStr = hex.str();
347 std::string busStr = std::to_string(config.bus);
348
349 std::string parameters = "peci-client 0x" + addrHexStr;
350 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
351
James Feistcf3bce62019-01-08 10:07:19 -0800352 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700353 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800354 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700355 {
James Feistcf3bce62019-01-08 10:07:19 -0800356 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700357 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700358 continue;
James Feist6714a252018-09-10 15:26:18 -0700359 }
360
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700361 const std::string& directoryName = path.path().filename();
362 if (boost::starts_with(directoryName, busStr) &&
363 boost::ends_with(directoryName, addrHexStr))
364 {
365 if (DEBUG)
366 {
367 std::cout << parameters << " on bus " << busStr
368 << " is already exported\n";
369 }
370 return;
371 }
James Feist6714a252018-09-10 15:26:18 -0700372 }
373
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700374 std::ofstream deviceFile(device);
375 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700376 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700377 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700378 return;
379 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700380 deviceFile << parameters;
381 deviceFile.close();
382
383 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700384}
385
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800386void detectCpu(boost::asio::deadline_timer& pingTimer,
387 boost::asio::deadline_timer& creationTimer,
388 boost::asio::io_service& io,
389 sdbusplus::asio::object_server& objectServer,
390 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
391 boost::container::flat_set<CPUConfig>& cpuConfigs,
392 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700393{
James Feist6714a252018-09-10 15:26:18 -0700394 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700395 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700396
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800397 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700398 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700399 std::string peciDevPath = peciDev + std::to_string(config.bus);
400 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
401 if (file < 0)
402 {
403 std::cerr << "unable to open " << peciDevPath << "\n";
404 std::exit(EXIT_FAILURE);
405 }
406
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700407 State newState;
James Feist6714a252018-09-10 15:26:18 -0700408 struct peci_ping_msg msg;
409 msg.addr = config.addr;
410 if (!ioctl(file, PECI_IOC_PING, &msg))
411 {
412 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700413 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700414 {
415 struct peci_rd_pkg_cfg_msg msg;
416 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800417 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700418 msg.param = rank;
419 msg.rx_len = 4;
420 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
421 {
422 if (msg.pkg_config[0] || msg.pkg_config[1] ||
423 msg.pkg_config[2])
424 {
425 dimmReady = true;
426 break;
427 }
428 }
429 else
430 {
431 break;
432 }
433 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700434
James Feist6714a252018-09-10 15:26:18 -0700435 if (dimmReady)
436 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700437 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700438 }
439 else
440 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700441 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700442 }
443 }
444 else
445 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700446 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700447 }
448
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700449 close(file);
450
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700451 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700452 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700453 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700454 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700455 if (config.state == State::OFF)
456 {
457 std::cout << config.name << " is detected\n";
458 exportDevice(config);
459 }
James Feist6714a252018-09-10 15:26:18 -0700460
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700461 if (newState == State::ON)
462 {
463 rescanDelaySeconds = 3;
464 }
465 else if (newState == State::READY)
466 {
467 rescanDelaySeconds = 5;
468 std::cout << "DIMM(s) on " << config.name
469 << " is/are detected\n";
470 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700471 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700472
473 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700474 }
475
476 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700477 {
478 keepPinging = true;
479 }
480
481 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700482 {
483 std::cout << config.name << ", state: " << config.state << "\n";
484 }
James Feist6714a252018-09-10 15:26:18 -0700485 }
486
James Feist6714a252018-09-10 15:26:18 -0700487 if (rescanDelaySeconds)
488 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700489 creationTimer.expires_from_now(
490 boost::posix_time::seconds(rescanDelaySeconds));
491 creationTimer.async_wait([&](const boost::system::error_code& ec) {
492 if (ec == boost::asio::error::operation_aborted)
493 {
494 return; // we're being canceled
495 }
496
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800497 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700498 sensorConfigs) ||
499 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700500 {
501 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800502 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700503 }
504 });
James Feist6714a252018-09-10 15:26:18 -0700505 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700506 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700507 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800508 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800509 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700510 }
511}
512
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700513void detectCpuAsync(
514 boost::asio::deadline_timer& pingTimer,
515 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
516 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800517 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800518 boost::container::flat_set<CPUConfig>& cpuConfigs,
519 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700520{
521 pingTimer.expires_from_now(boost::posix_time::seconds(1));
522 pingTimer.async_wait([&](const boost::system::error_code& ec) {
523 if (ec == boost::asio::error::operation_aborted)
524 {
525 return; // we're being canceled
526 }
527
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800528 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800529 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700530 });
531}
532
James Feist58295ad2019-05-30 15:01:41 -0700533bool getCpuConfig(
534 const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
535 boost::container::flat_set<CPUConfig>& cpuConfigs,
536 ManagedObjectType& sensorConfigs,
537 sdbusplus::asio::object_server& objectServer,
538 boost::container::flat_map<
539 std::string, std::shared_ptr<sdbusplus::asio::dbus_interface>>&
540 inventoryIfaces)
James Feist6714a252018-09-10 15:26:18 -0700541{
James Feist6714a252018-09-10 15:26:18 -0700542 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800543 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700544 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700545 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700546 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700547 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800548 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700549 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700550 return false;
James Feist6714a252018-09-10 15:26:18 -0700551 }
552 useCache = true;
553 }
554
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700555 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700556 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700557 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700558 {
559 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800560 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700561 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700562 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700563 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700564 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700565 {
566 continue;
567 }
568
James Feist6714a252018-09-10 15:26:18 -0700569 auto findName = config.second.find("Name");
570 if (findName == config.second.end())
571 {
572 continue;
573 }
James Feist3eb82622019-02-08 13:10:22 -0800574 std::string nameRaw =
575 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700576 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700577 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700578
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700579 bool present = true;
580 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700581 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700582 if (suppConfig.first.find("PresenceGpio") !=
583 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700584 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700585 present = cpuIsPresent(suppConfig.second);
586 break;
James Feist58295ad2019-05-30 15:01:41 -0700587 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700588 }
589
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
James Feist58295ad2019-05-30 15:01:41 -0700603 }
604
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700605 auto findBus = config.second.find("Bus");
606 if (findBus == config.second.end())
607 {
608 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
609 continue;
610 }
James Feist3eb82622019-02-08 13:10:22 -0800611 uint64_t bus =
612 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700613
614 auto findAddress = config.second.find("Address");
615 if (findAddress == config.second.end())
616 {
617 std::cerr << "Can't find 'Address' setting in " << name
618 << "\n";
619 continue;
620 }
James Feist3eb82622019-02-08 13:10:22 -0800621 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
622 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700623
624 if (DEBUG)
625 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700626 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700627 std::cout << "addr: " << addr << "\n";
628 std::cout << "name: " << name << "\n";
629 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700630 }
631
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800632 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700633 }
634 }
635 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700636
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800637 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700638 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800639 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700640 << " parsed\n";
641 return true;
642 }
643
644 return false;
James Feist6714a252018-09-10 15:26:18 -0700645}
646
James Feistb6c0b912019-07-09 12:21:44 -0700647int main()
James Feist6714a252018-09-10 15:26:18 -0700648{
649 boost::asio::io_service io;
650 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800651 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700652
653 systemBus->request_name("xyz.openbmc_project.CPUSensor");
654 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700655 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
656 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700657 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700658 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800659 ManagedObjectType sensorConfigs;
James Feist58295ad2019-05-30 15:01:41 -0700660 boost::container::flat_map<std::string,
661 std::shared_ptr<sdbusplus::asio::dbus_interface>>
662 inventoryIfaces;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700663
664 filterTimer.expires_from_now(boost::posix_time::seconds(1));
665 filterTimer.async_wait([&](const boost::system::error_code& ec) {
666 if (ec == boost::asio::error::operation_aborted)
667 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700668 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700669 }
670
James Feist58295ad2019-05-30 15:01:41 -0700671 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer,
672 inventoryIfaces))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700673 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800674 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800675 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700676 }
677 });
678
James Feist6714a252018-09-10 15:26:18 -0700679 std::function<void(sdbusplus::message::message&)> eventHandler =
680 [&](sdbusplus::message::message& message) {
681 if (message.is_method_error())
682 {
683 std::cerr << "callback method error\n";
684 return;
685 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700686
687 if (DEBUG)
688 {
689 std::cout << message.get_path() << " is changed\n";
690 }
691
James Feist6714a252018-09-10 15:26:18 -0700692 // this implicitly cancels the timer
693 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700694 filterTimer.async_wait([&](const boost::system::error_code& ec) {
695 if (ec == boost::asio::error::operation_aborted)
696 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700697 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700698 }
699
James Feist58295ad2019-05-30 15:01:41 -0700700 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
701 objectServer, inventoryIfaces))
James Feist6714a252018-09-10 15:26:18 -0700702 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700703 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800704 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700705 }
706 });
707 };
708
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700709 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700710 {
711 auto match = std::make_unique<sdbusplus::bus::match::match>(
712 static_cast<sdbusplus::bus::bus&>(*systemBus),
713 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700714 std::string(inventoryPath) + "',arg0namespace='" +
715 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700716 eventHandler);
717 matches.emplace_back(std::move(match));
718 }
719
720 io.run();
721}