blob: 7e8c7b0c5b7021d48ad7ba1eb91793046a79ee5e [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "CPUSensor.hpp"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070022
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/process/child.hpp>
James Feist24f02f22019-04-15 11:05:39 -070028#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
James Feistf87dc4c2018-12-05 14:39:51 -080034// clang-format off
35// this needs to be included last or we'll have build issues
36#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080037#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
38#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
39#endif
James Feistf87dc4c2018-12-05 14:39:51 -080040// clang-format on
41
James Feist6714a252018-09-10 15:26:18 -070042static constexpr bool DEBUG = false;
43
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080044boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors;
45
James Feist6714a252018-09-10 15:26:18 -070046enum State
47{
48 OFF, // host powered down
49 ON, // host powered on
50 READY // host powered on and mem test passed - fully ready
51};
52
53struct CPUConfig
54{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070055 CPUConfig(const uint64_t& bus, const uint64_t& addr,
56 const std::string& name, const State& state) :
57 bus(bus),
58 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070059 {
60 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070061 int bus;
James Feist6714a252018-09-10 15:26:18 -070062 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070063 std::string name;
James Feist6714a252018-09-10 15:26:18 -070064 State state;
65
66 bool operator<(const CPUConfig& rhs) const
67 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070068 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070069 }
70};
71
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070072static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070074
James Feistcf3bce62019-01-08 10:07:19 -080075namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080076
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070078 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo7d47bf52019-04-23 16:43:50 -070079static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080080static constexpr std::array<const char*, 3> hiddenProps = {
81 CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070082
Jae Hyun Yood64262b2018-11-01 13:31:16 -070083void detectCpuAsync(
84 boost::asio::deadline_timer& pingTimer,
85 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
86 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080087 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080088 boost::container::flat_set<CPUConfig>& cpuConfigs,
89 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -070090
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080091bool createSensors(boost::asio::io_service& io,
92 sdbusplus::asio::object_server& objectServer,
93 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
94 boost::container::flat_set<CPUConfig>& cpuConfigs,
95 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -070096{
97 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080098 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -070099 {
100 if (cpu.state != State::OFF)
101 {
102 available = true;
103 break;
104 }
105 }
106 if (!available)
107 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700108 return false;
James Feist6714a252018-09-10 15:26:18 -0700109 }
110
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800111 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700112 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800113 return false;
James Feist6714a252018-09-10 15:26:18 -0700114 }
115
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700116 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
118 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
119 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700120 {
121 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700122 return true;
James Feist6714a252018-09-10 15:26:18 -0700123 }
124
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700125 boost::container::flat_set<std::string> scannedDirectories;
126 boost::container::flat_set<std::string> createdSensors;
127
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800128 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700129 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700130 const std::string& pathStr = hwmonNamePath.string();
131 auto hwmonDirectory = hwmonNamePath.parent_path();
132
133 auto ret = scannedDirectories.insert(hwmonDirectory.string());
134 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700135 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700136 continue; // already searched this path
137 }
138
139 fs::path::iterator it = hwmonNamePath.begin();
140 std::advance(it, 6); // pick the 6th part for a PECI client device name
141 std::string deviceName = *it;
142 auto findHyphen = deviceName.find("-");
143 if (findHyphen == std::string::npos)
144 {
145 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700146 continue;
147 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700148 std::string busStr = deviceName.substr(0, findHyphen);
149 std::string addrStr = deviceName.substr(findHyphen + 1);
150
151 size_t bus = 0;
152 size_t addr = 0;
153 try
154 {
155 bus = std::stoi(busStr);
156 addr = std::stoi(addrStr, 0, 16);
157 }
James Feistb6c0b912019-07-09 12:21:44 -0700158 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700159 {
160 continue;
161 }
162
163 std::ifstream nameFile(hwmonNamePath);
164 if (!nameFile.good())
165 {
166 std::cerr << "Failure reading " << hwmonNamePath << "\n";
167 continue;
168 }
169 std::string hwmonName;
170 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700171 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800172 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700173 {
174 // shouldn't have an empty name file
175 continue;
176 }
James Feist6714a252018-09-10 15:26:18 -0700177 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700178 {
179 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
180 << "\n";
181 }
James Feist6714a252018-09-10 15:26:18 -0700182
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700183 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700184 const SensorData* sensorData = nullptr;
185 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700186 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700187
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700188 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800189 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700190 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700191 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700192 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700193 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 auto sensorBase = sensorData->find(sensorType);
196 if (sensorBase != sensorData->end())
197 {
198 baseConfiguration = &(*sensorBase);
199 break;
200 }
201 }
202 if (baseConfiguration == nullptr)
203 {
204 std::cerr << "error finding base configuration for" << hwmonName
205 << "\n";
206 continue;
207 }
208 auto configurationBus = baseConfiguration->second.find("Bus");
209 auto configurationAddress =
210 baseConfiguration->second.find("Address");
211
212 if (configurationBus == baseConfiguration->second.end() ||
213 configurationAddress == baseConfiguration->second.end())
214 {
215 std::cerr << "error finding bus or address in configuration";
216 continue;
217 }
218
James Feist3eb82622019-02-08 13:10:22 -0800219 if (std::get<uint64_t>(configurationBus->second) != bus ||
220 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700221 {
222 continue;
223 }
224
225 interfacePath = &(sensor.first.str);
226 break;
227 }
228 if (interfacePath == nullptr)
229 {
230 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700231 continue;
232 }
233
234 auto findCpuId = baseConfiguration->second.find("CpuID");
235 if (findCpuId == baseConfiguration->second.end())
236 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700237 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700238 continue;
239 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700240 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800241 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700242
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700243 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700244 std::vector<fs::path> inputPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700245 if (!findFiles(directory, R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700246 {
247 std::cerr << "No temperature sensors in system\n";
248 continue;
249 }
250
251 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800252 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700253 {
254 auto inputPathStr = inputPath.string();
255 auto labelPath =
256 boost::replace_all_copy(inputPathStr, "input", "label");
257 std::ifstream labelFile(labelPath);
258 if (!labelFile.good())
259 {
260 std::cerr << "Failure reading " << labelPath << "\n";
261 continue;
262 }
263 std::string label;
264 std::getline(labelFile, label);
265 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800266
James Feist6714a252018-09-10 15:26:18 -0700267 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700268
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800269 auto findSensor = gCpuSensors.find(sensorName);
270 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700271 {
272 if (DEBUG)
273 {
274 std::cout << "Skipped: " << inputPath << ": " << sensorName
275 << " is already created\n";
276 }
277 continue;
278 }
279
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800280 // check hidden properties
281 bool show = true;
282 for (const char* prop : hiddenProps)
283 {
284 if (label == prop)
285 {
286 show = false;
287 break;
288 }
289 }
290
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700291 /*
292 * Find if there is DtsCritOffset is configured in config file
293 * set it if configured or else set it to 0
294 */
295 double dtsOffset = 0;
296 if (label == "DTS")
297 {
298 auto findThrOffset =
299 baseConfiguration->second.find("DtsCritOffset");
300 if (findThrOffset != baseConfiguration->second.end())
301 {
302 dtsOffset = std::visit(VariantToDoubleVisitor(),
303 findThrOffset->second);
304 }
305 }
306
James Feist6714a252018-09-10 15:26:18 -0700307 std::vector<thresholds::Threshold> sensorThresholds;
308 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700309 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700310 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800311 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700312 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700313 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700314 CPUSensor::sensorScaleFactor,
315 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700316 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700317 std::cerr << "error populating thresholds for "
318 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700319 }
320 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800321 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700322 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800323 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700324 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700325 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700326 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700327 {
James Feist6714a252018-09-10 15:26:18 -0700328 std::cout << "Mapped: " << inputPath << " to " << sensorName
329 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700330 }
James Feist6714a252018-09-10 15:26:18 -0700331 }
332 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700333
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700334 if (createdSensors.size())
335 {
336 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
337 << " created\n";
338 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700339
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700340 return true;
James Feist6714a252018-09-10 15:26:18 -0700341}
342
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700343void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700344{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700345 std::ostringstream hex;
346 hex << std::hex << config.addr;
347 const std::string& addrHexStr = hex.str();
348 std::string busStr = std::to_string(config.bus);
349
350 std::string parameters = "peci-client 0x" + addrHexStr;
351 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
352
James Feistcf3bce62019-01-08 10:07:19 -0800353 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700354 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800355 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700356 {
James Feistcf3bce62019-01-08 10:07:19 -0800357 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700358 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700359 continue;
James Feist6714a252018-09-10 15:26:18 -0700360 }
361
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700362 const std::string& directoryName = path.path().filename();
363 if (boost::starts_with(directoryName, busStr) &&
364 boost::ends_with(directoryName, addrHexStr))
365 {
366 if (DEBUG)
367 {
368 std::cout << parameters << " on bus " << busStr
369 << " is already exported\n";
370 }
371 return;
372 }
James Feist6714a252018-09-10 15:26:18 -0700373 }
374
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700375 std::ofstream deviceFile(device);
376 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700377 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700378 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700379 return;
380 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700381 deviceFile << parameters;
382 deviceFile.close();
383
384 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700385}
386
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800387void detectCpu(boost::asio::deadline_timer& pingTimer,
388 boost::asio::deadline_timer& creationTimer,
389 boost::asio::io_service& io,
390 sdbusplus::asio::object_server& objectServer,
391 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
392 boost::container::flat_set<CPUConfig>& cpuConfigs,
393 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700394{
James Feist6714a252018-09-10 15:26:18 -0700395 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700396 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700397
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800398 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700399 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700400 std::string peciDevPath = peciDev + std::to_string(config.bus);
401 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
402 if (file < 0)
403 {
404 std::cerr << "unable to open " << peciDevPath << "\n";
405 std::exit(EXIT_FAILURE);
406 }
407
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700408 State newState;
James Feist6714a252018-09-10 15:26:18 -0700409 struct peci_ping_msg msg;
410 msg.addr = config.addr;
411 if (!ioctl(file, PECI_IOC_PING, &msg))
412 {
413 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700414 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700415 {
416 struct peci_rd_pkg_cfg_msg msg;
417 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800418 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700419 msg.param = rank;
420 msg.rx_len = 4;
421 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
422 {
423 if (msg.pkg_config[0] || msg.pkg_config[1] ||
424 msg.pkg_config[2])
425 {
426 dimmReady = true;
427 break;
428 }
429 }
430 else
431 {
432 break;
433 }
434 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700435
James Feist6714a252018-09-10 15:26:18 -0700436 if (dimmReady)
437 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700438 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700439 }
440 else
441 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700442 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700443 }
444 }
445 else
446 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700447 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700448 }
449
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700450 close(file);
451
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700452 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700453 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700454 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700455 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700456 if (config.state == State::OFF)
457 {
458 std::cout << config.name << " is detected\n";
459 exportDevice(config);
460 }
James Feist6714a252018-09-10 15:26:18 -0700461
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700462 if (newState == State::ON)
463 {
464 rescanDelaySeconds = 3;
465 }
466 else if (newState == State::READY)
467 {
468 rescanDelaySeconds = 5;
469 std::cout << "DIMM(s) on " << config.name
470 << " is/are detected\n";
471 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700472 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700473
474 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700475 }
476
477 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700478 {
479 keepPinging = true;
480 }
481
482 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700483 {
484 std::cout << config.name << ", state: " << config.state << "\n";
485 }
James Feist6714a252018-09-10 15:26:18 -0700486 }
487
James Feist6714a252018-09-10 15:26:18 -0700488 if (rescanDelaySeconds)
489 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700490 creationTimer.expires_from_now(
491 boost::posix_time::seconds(rescanDelaySeconds));
492 creationTimer.async_wait([&](const boost::system::error_code& ec) {
493 if (ec == boost::asio::error::operation_aborted)
494 {
495 return; // we're being canceled
496 }
497
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800498 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700499 sensorConfigs) ||
500 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700501 {
502 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800503 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700504 }
505 });
James Feist6714a252018-09-10 15:26:18 -0700506 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700507 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700508 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800509 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800510 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700511 }
512}
513
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700514void detectCpuAsync(
515 boost::asio::deadline_timer& pingTimer,
516 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
517 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800518 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800519 boost::container::flat_set<CPUConfig>& cpuConfigs,
520 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700521{
522 pingTimer.expires_from_now(boost::posix_time::seconds(1));
523 pingTimer.async_wait([&](const boost::system::error_code& ec) {
524 if (ec == boost::asio::error::operation_aborted)
525 {
526 return; // we're being canceled
527 }
528
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800529 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800530 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700531 });
532}
533
James Feist58295ad2019-05-30 15:01:41 -0700534bool getCpuConfig(
535 const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
536 boost::container::flat_set<CPUConfig>& cpuConfigs,
537 ManagedObjectType& sensorConfigs,
538 sdbusplus::asio::object_server& objectServer,
539 boost::container::flat_map<
540 std::string, std::shared_ptr<sdbusplus::asio::dbus_interface>>&
541 inventoryIfaces)
James Feist6714a252018-09-10 15:26:18 -0700542{
James Feist6714a252018-09-10 15:26:18 -0700543 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800544 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700545 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700546 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700547 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700548 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800549 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700550 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700551 return false;
James Feist6714a252018-09-10 15:26:18 -0700552 }
553 useCache = true;
554 }
555
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700556 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700557 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700558 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700559 {
560 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800561 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700562 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700563 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700564 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700565 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700566 {
567 continue;
568 }
569
James Feist6714a252018-09-10 15:26:18 -0700570 auto findName = config.second.find("Name");
571 if (findName == config.second.end())
572 {
573 continue;
574 }
James Feist3eb82622019-02-08 13:10:22 -0800575 std::string nameRaw =
576 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700577 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700578 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700579
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700580 bool present = true;
581 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700582 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700583 if (suppConfig.first.find("PresenceGpio") !=
584 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700585 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700586 present = cpuIsPresent(suppConfig.second);
587 break;
James Feist58295ad2019-05-30 15:01:41 -0700588 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700589 }
590
591 if (inventoryIfaces.find(name) == inventoryIfaces.end())
592 {
593 auto iface = objectServer.add_interface(
594 cpuInventoryPath + std::string("/") + name,
595 "xyz.openbmc_project.Inventory.Item");
596 iface->register_property("PrettyName", name);
597 iface->register_property("Present", present);
598 iface->initialize();
599 inventoryIfaces[name] = std::move(iface);
600 }
601 if (!present)
602 {
603 continue; // no reason to look for non present cpu
James Feist58295ad2019-05-30 15:01:41 -0700604 }
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}