blob: a595236d28fb912d85c4e08b54cf5f55a314e5a0 [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
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <array>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/algorithm/string/predicate.hpp>
25#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070027#include <boost/container/flat_set.hpp>
28#include <boost/date_time/posix_time/posix_time.hpp>
29#include <boost/process/child.hpp>
James Feist24f02f22019-04-15 11:05:39 -070030#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070031#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <functional>
33#include <memory>
James Feist6714a252018-09-10 15:26:18 -070034#include <regex>
35#include <sdbusplus/asio/connection.hpp>
36#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <sdbusplus/bus/match.hpp>
38#include <sstream>
39#include <stdexcept>
40#include <string>
41#include <utility>
42#include <variant>
43#include <vector>
James Feist6714a252018-09-10 15:26:18 -070044
James Feistf87dc4c2018-12-05 14:39:51 -080045// clang-format off
46// this needs to be included last or we'll have build issues
47#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080048#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
49#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
50#endif
James Feistf87dc4c2018-12-05 14:39:51 -080051// clang-format on
52
James Feist6714a252018-09-10 15:26:18 -070053static constexpr bool DEBUG = false;
54
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080055boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors;
56
James Feist6714a252018-09-10 15:26:18 -070057enum State
58{
59 OFF, // host powered down
60 ON, // host powered on
61 READY // host powered on and mem test passed - fully ready
62};
63
64struct CPUConfig
65{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070066 CPUConfig(const uint64_t& bus, const uint64_t& addr,
67 const std::string& name, const State& state) :
68 bus(bus),
69 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070070 {
71 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070072 int bus;
James Feist6714a252018-09-10 15:26:18 -070073 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070074 std::string name;
James Feist6714a252018-09-10 15:26:18 -070075 State state;
76
77 bool operator<(const CPUConfig& rhs) const
78 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070079 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070080 }
81};
82
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070083static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070085
James Feistcf3bce62019-01-08 10:07:19 -080086namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080087
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070089 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo7d47bf52019-04-23 16:43:50 -070090static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080091static constexpr std::array<const char*, 3> hiddenProps = {
92 CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070093
Jae Hyun Yood64262b2018-11-01 13:31:16 -070094void detectCpuAsync(
95 boost::asio::deadline_timer& pingTimer,
96 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
97 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080098 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -080099 boost::container::flat_set<CPUConfig>& cpuConfigs,
100 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700101
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800102bool createSensors(boost::asio::io_service& io,
103 sdbusplus::asio::object_server& objectServer,
104 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
105 boost::container::flat_set<CPUConfig>& cpuConfigs,
106 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700107{
108 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800109 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700110 {
111 if (cpu.state != State::OFF)
112 {
113 available = true;
114 break;
115 }
116 }
117 if (!available)
118 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700119 return false;
James Feist6714a252018-09-10 15:26:18 -0700120 }
121
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800122 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700123 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800124 return false;
James Feist6714a252018-09-10 15:26:18 -0700125 }
126
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700127 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700128 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
129 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
130 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700131 {
132 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700133 return true;
James Feist6714a252018-09-10 15:26:18 -0700134 }
135
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700136 boost::container::flat_set<std::string> scannedDirectories;
137 boost::container::flat_set<std::string> createdSensors;
138
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800139 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700140 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700141 const std::string& pathStr = hwmonNamePath.string();
142 auto hwmonDirectory = hwmonNamePath.parent_path();
143
144 auto ret = scannedDirectories.insert(hwmonDirectory.string());
145 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700146 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700147 continue; // already searched this path
148 }
149
150 fs::path::iterator it = hwmonNamePath.begin();
151 std::advance(it, 6); // pick the 6th part for a PECI client device name
152 std::string deviceName = *it;
153 auto findHyphen = deviceName.find("-");
154 if (findHyphen == std::string::npos)
155 {
156 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700157 continue;
158 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700159 std::string busStr = deviceName.substr(0, findHyphen);
160 std::string addrStr = deviceName.substr(findHyphen + 1);
161
162 size_t bus = 0;
163 size_t addr = 0;
164 try
165 {
166 bus = std::stoi(busStr);
167 addr = std::stoi(addrStr, 0, 16);
168 }
James Feistb6c0b912019-07-09 12:21:44 -0700169 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700170 {
171 continue;
172 }
173
174 std::ifstream nameFile(hwmonNamePath);
175 if (!nameFile.good())
176 {
177 std::cerr << "Failure reading " << hwmonNamePath << "\n";
178 continue;
179 }
180 std::string hwmonName;
181 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700182 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800183 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700184 {
185 // shouldn't have an empty name file
186 continue;
187 }
James Feist6714a252018-09-10 15:26:18 -0700188 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700189 {
190 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
191 << "\n";
192 }
James Feist6714a252018-09-10 15:26:18 -0700193
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700195 const SensorData* sensorData = nullptr;
196 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700197 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700198
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700199 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800200 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700201 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700202 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700203 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700204 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700206 auto sensorBase = sensorData->find(sensorType);
207 if (sensorBase != sensorData->end())
208 {
209 baseConfiguration = &(*sensorBase);
210 break;
211 }
212 }
213 if (baseConfiguration == nullptr)
214 {
215 std::cerr << "error finding base configuration for" << hwmonName
216 << "\n";
217 continue;
218 }
219 auto configurationBus = baseConfiguration->second.find("Bus");
220 auto configurationAddress =
221 baseConfiguration->second.find("Address");
222
223 if (configurationBus == baseConfiguration->second.end() ||
224 configurationAddress == baseConfiguration->second.end())
225 {
226 std::cerr << "error finding bus or address in configuration";
227 continue;
228 }
229
James Feist3eb82622019-02-08 13:10:22 -0800230 if (std::get<uint64_t>(configurationBus->second) != bus ||
231 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700232 {
233 continue;
234 }
235
236 interfacePath = &(sensor.first.str);
237 break;
238 }
239 if (interfacePath == nullptr)
240 {
241 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700242 continue;
243 }
244
245 auto findCpuId = baseConfiguration->second.find("CpuID");
246 if (findCpuId == baseConfiguration->second.end())
247 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700249 continue;
250 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700251 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800252 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700253
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700254 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700255 std::vector<fs::path> inputPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700256 if (!findFiles(directory, R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700257 {
258 std::cerr << "No temperature sensors in system\n";
259 continue;
260 }
261
262 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800263 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700264 {
265 auto inputPathStr = inputPath.string();
266 auto labelPath =
267 boost::replace_all_copy(inputPathStr, "input", "label");
268 std::ifstream labelFile(labelPath);
269 if (!labelFile.good())
270 {
271 std::cerr << "Failure reading " << labelPath << "\n";
272 continue;
273 }
274 std::string label;
275 std::getline(labelFile, label);
276 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800277
James Feist6714a252018-09-10 15:26:18 -0700278 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700279
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800280 auto findSensor = gCpuSensors.find(sensorName);
281 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700282 {
283 if (DEBUG)
284 {
285 std::cout << "Skipped: " << inputPath << ": " << sensorName
286 << " is already created\n";
287 }
288 continue;
289 }
290
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800291 // check hidden properties
292 bool show = true;
293 for (const char* prop : hiddenProps)
294 {
295 if (label == prop)
296 {
297 show = false;
298 break;
299 }
300 }
301
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700302 /*
303 * Find if there is DtsCritOffset is configured in config file
304 * set it if configured or else set it to 0
305 */
306 double dtsOffset = 0;
307 if (label == "DTS")
308 {
309 auto findThrOffset =
310 baseConfiguration->second.find("DtsCritOffset");
311 if (findThrOffset != baseConfiguration->second.end())
312 {
313 dtsOffset = std::visit(VariantToDoubleVisitor(),
314 findThrOffset->second);
315 }
316 }
317
James Feist6714a252018-09-10 15:26:18 -0700318 std::vector<thresholds::Threshold> sensorThresholds;
319 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700320 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700321 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800322 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700323 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700324 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700325 CPUSensor::sensorScaleFactor,
326 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700327 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700328 std::cerr << "error populating thresholds for "
329 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700330 }
331 }
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800332 gCpuSensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800334 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700335 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700336 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700337 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700338 {
James Feist6714a252018-09-10 15:26:18 -0700339 std::cout << "Mapped: " << inputPath << " to " << sensorName
340 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700341 }
James Feist6714a252018-09-10 15:26:18 -0700342 }
343 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700344
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700345 if (createdSensors.size())
346 {
347 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
348 << " created\n";
349 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700350
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700351 return true;
James Feist6714a252018-09-10 15:26:18 -0700352}
353
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700354void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700355{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700356 std::ostringstream hex;
357 hex << std::hex << config.addr;
358 const std::string& addrHexStr = hex.str();
359 std::string busStr = std::to_string(config.bus);
360
361 std::string parameters = "peci-client 0x" + addrHexStr;
362 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
363
James Feistcf3bce62019-01-08 10:07:19 -0800364 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700365 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800366 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700367 {
James Feistcf3bce62019-01-08 10:07:19 -0800368 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700369 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700370 continue;
James Feist6714a252018-09-10 15:26:18 -0700371 }
372
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700373 const std::string& directoryName = path.path().filename();
374 if (boost::starts_with(directoryName, busStr) &&
375 boost::ends_with(directoryName, addrHexStr))
376 {
377 if (DEBUG)
378 {
379 std::cout << parameters << " on bus " << busStr
380 << " is already exported\n";
381 }
382 return;
383 }
James Feist6714a252018-09-10 15:26:18 -0700384 }
385
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700386 std::ofstream deviceFile(device);
387 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700388 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700389 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700390 return;
391 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700392 deviceFile << parameters;
393 deviceFile.close();
394
395 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700396}
397
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800398void detectCpu(boost::asio::deadline_timer& pingTimer,
399 boost::asio::deadline_timer& creationTimer,
400 boost::asio::io_service& io,
401 sdbusplus::asio::object_server& objectServer,
402 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
403 boost::container::flat_set<CPUConfig>& cpuConfigs,
404 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700405{
James Feist6714a252018-09-10 15:26:18 -0700406 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700407 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700408
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800409 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700410 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700411 std::string peciDevPath = peciDev + std::to_string(config.bus);
412 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
413 if (file < 0)
414 {
415 std::cerr << "unable to open " << peciDevPath << "\n";
416 std::exit(EXIT_FAILURE);
417 }
418
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700419 State newState;
James Feist6714a252018-09-10 15:26:18 -0700420 struct peci_ping_msg msg;
421 msg.addr = config.addr;
422 if (!ioctl(file, PECI_IOC_PING, &msg))
423 {
424 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700425 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700426 {
427 struct peci_rd_pkg_cfg_msg msg;
428 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800429 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700430 msg.param = rank;
431 msg.rx_len = 4;
432 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
433 {
434 if (msg.pkg_config[0] || msg.pkg_config[1] ||
435 msg.pkg_config[2])
436 {
437 dimmReady = true;
438 break;
439 }
440 }
441 else
442 {
443 break;
444 }
445 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700446
James Feist6714a252018-09-10 15:26:18 -0700447 if (dimmReady)
448 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700449 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700450 }
451 else
452 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700453 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700454 }
455 }
456 else
457 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700458 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700459 }
460
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700461 close(file);
462
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700463 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700464 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700465 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700466 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700467 if (config.state == State::OFF)
468 {
469 std::cout << config.name << " is detected\n";
470 exportDevice(config);
471 }
James Feist6714a252018-09-10 15:26:18 -0700472
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700473 if (newState == State::ON)
474 {
475 rescanDelaySeconds = 3;
476 }
477 else if (newState == State::READY)
478 {
479 rescanDelaySeconds = 5;
480 std::cout << "DIMM(s) on " << config.name
481 << " is/are detected\n";
482 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700483 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700484
485 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700486 }
487
488 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700489 {
490 keepPinging = true;
491 }
492
493 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700494 {
495 std::cout << config.name << ", state: " << config.state << "\n";
496 }
James Feist6714a252018-09-10 15:26:18 -0700497 }
498
James Feist6714a252018-09-10 15:26:18 -0700499 if (rescanDelaySeconds)
500 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700501 creationTimer.expires_from_now(
502 boost::posix_time::seconds(rescanDelaySeconds));
503 creationTimer.async_wait([&](const boost::system::error_code& ec) {
504 if (ec == boost::asio::error::operation_aborted)
505 {
506 return; // we're being canceled
507 }
508
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800509 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700510 sensorConfigs) ||
511 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700512 {
513 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800514 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700515 }
516 });
James Feist6714a252018-09-10 15:26:18 -0700517 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700518 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700519 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800520 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800521 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700522 }
523}
524
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700525void detectCpuAsync(
526 boost::asio::deadline_timer& pingTimer,
527 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
528 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800529 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800530 boost::container::flat_set<CPUConfig>& cpuConfigs,
531 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700532{
533 pingTimer.expires_from_now(boost::posix_time::seconds(1));
534 pingTimer.async_wait([&](const boost::system::error_code& ec) {
535 if (ec == boost::asio::error::operation_aborted)
536 {
537 return; // we're being canceled
538 }
539
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800540 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800541 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700542 });
543}
544
James Feist58295ad2019-05-30 15:01:41 -0700545bool getCpuConfig(
546 const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
547 boost::container::flat_set<CPUConfig>& cpuConfigs,
548 ManagedObjectType& sensorConfigs,
549 sdbusplus::asio::object_server& objectServer,
550 boost::container::flat_map<
551 std::string, std::shared_ptr<sdbusplus::asio::dbus_interface>>&
552 inventoryIfaces)
James Feist6714a252018-09-10 15:26:18 -0700553{
James Feist6714a252018-09-10 15:26:18 -0700554 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800555 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700556 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700557 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700558 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700559 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800560 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700561 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700562 return false;
James Feist6714a252018-09-10 15:26:18 -0700563 }
564 useCache = true;
565 }
566
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700567 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700568 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700569 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700570 {
571 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800572 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700573 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700574 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700575 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700576 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700577 {
578 continue;
579 }
580
James Feist6714a252018-09-10 15:26:18 -0700581 auto findName = config.second.find("Name");
582 if (findName == config.second.end())
583 {
584 continue;
585 }
James Feist3eb82622019-02-08 13:10:22 -0800586 std::string nameRaw =
587 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700588 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700589 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700590
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700591 bool present = true;
592 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700593 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700594 if (suppConfig.first.find("PresenceGpio") !=
595 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700596 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700597 present = cpuIsPresent(suppConfig.second);
598 break;
James Feist58295ad2019-05-30 15:01:41 -0700599 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700600 }
601
602 if (inventoryIfaces.find(name) == inventoryIfaces.end())
603 {
604 auto iface = objectServer.add_interface(
605 cpuInventoryPath + std::string("/") + name,
606 "xyz.openbmc_project.Inventory.Item");
607 iface->register_property("PrettyName", name);
608 iface->register_property("Present", present);
609 iface->initialize();
610 inventoryIfaces[name] = std::move(iface);
611 }
612 if (!present)
613 {
614 continue; // no reason to look for non present cpu
James Feist58295ad2019-05-30 15:01:41 -0700615 }
616
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700617 auto findBus = config.second.find("Bus");
618 if (findBus == config.second.end())
619 {
620 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
621 continue;
622 }
James Feist3eb82622019-02-08 13:10:22 -0800623 uint64_t bus =
624 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700625
626 auto findAddress = config.second.find("Address");
627 if (findAddress == config.second.end())
628 {
629 std::cerr << "Can't find 'Address' setting in " << name
630 << "\n";
631 continue;
632 }
James Feist3eb82622019-02-08 13:10:22 -0800633 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
634 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700635
636 if (DEBUG)
637 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700638 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700639 std::cout << "addr: " << addr << "\n";
640 std::cout << "name: " << name << "\n";
641 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700642 }
643
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800644 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700645 }
646 }
647 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700648
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800649 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700650 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800651 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700652 << " parsed\n";
653 return true;
654 }
655
656 return false;
James Feist6714a252018-09-10 15:26:18 -0700657}
658
James Feistb6c0b912019-07-09 12:21:44 -0700659int main()
James Feist6714a252018-09-10 15:26:18 -0700660{
661 boost::asio::io_service io;
662 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800663 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700664
665 systemBus->request_name("xyz.openbmc_project.CPUSensor");
666 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700667 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
668 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700669 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700670 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800671 ManagedObjectType sensorConfigs;
James Feist58295ad2019-05-30 15:01:41 -0700672 boost::container::flat_map<std::string,
673 std::shared_ptr<sdbusplus::asio::dbus_interface>>
674 inventoryIfaces;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700675
676 filterTimer.expires_from_now(boost::posix_time::seconds(1));
677 filterTimer.async_wait([&](const boost::system::error_code& ec) {
678 if (ec == boost::asio::error::operation_aborted)
679 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700680 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700681 }
682
James Feist58295ad2019-05-30 15:01:41 -0700683 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer,
684 inventoryIfaces))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700685 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800686 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800687 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700688 }
689 });
690
James Feist6714a252018-09-10 15:26:18 -0700691 std::function<void(sdbusplus::message::message&)> eventHandler =
692 [&](sdbusplus::message::message& message) {
693 if (message.is_method_error())
694 {
695 std::cerr << "callback method error\n";
696 return;
697 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698
699 if (DEBUG)
700 {
701 std::cout << message.get_path() << " is changed\n";
702 }
703
James Feist6714a252018-09-10 15:26:18 -0700704 // this implicitly cancels the timer
705 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700706 filterTimer.async_wait([&](const boost::system::error_code& ec) {
707 if (ec == boost::asio::error::operation_aborted)
708 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700709 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700710 }
711
James Feist58295ad2019-05-30 15:01:41 -0700712 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
713 objectServer, inventoryIfaces))
James Feist6714a252018-09-10 15:26:18 -0700714 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700715 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800716 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700717 }
718 });
719 };
720
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700721 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700722 {
723 auto match = std::make_unique<sdbusplus::bus::match::match>(
724 static_cast<sdbusplus::bus::bus&>(*systemBus),
725 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700726 std::string(inventoryPath) + "',arg0namespace='" +
727 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700728 eventHandler);
729 matches.emplace_back(std::move(match));
730 }
731
732 io.run();
733}