blob: 0e6f99de2154b91cd623337547dc45178152943d [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;
James Feistc140e202019-11-14 15:23:51 -080056boost::container::flat_map<std::string,
57 std::shared_ptr<sdbusplus::asio::dbus_interface>>
58 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080059
James Feist6714a252018-09-10 15:26:18 -070060enum State
61{
62 OFF, // host powered down
63 ON, // host powered on
64 READY // host powered on and mem test passed - fully ready
65};
66
67struct CPUConfig
68{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070069 CPUConfig(const uint64_t& bus, const uint64_t& addr,
70 const std::string& name, const State& state) :
71 bus(bus),
72 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070073 {
74 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 int bus;
James Feist6714a252018-09-10 15:26:18 -070076 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070077 std::string name;
James Feist6714a252018-09-10 15:26:18 -070078 State state;
79
80 bool operator<(const CPUConfig& rhs) const
81 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070082 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070083 }
84};
85
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070086static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070088
James Feistcf3bce62019-01-08 10:07:19 -080089namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080090
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070092 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo7d47bf52019-04-23 16:43:50 -070093static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"};
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080094static constexpr std::array<const char*, 3> hiddenProps = {
95 CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"};
James Feist6714a252018-09-10 15:26:18 -070096
Jae Hyun Yood64262b2018-11-01 13:31:16 -070097void detectCpuAsync(
98 boost::asio::deadline_timer& pingTimer,
99 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
100 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800101 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800102 boost::container::flat_set<CPUConfig>& cpuConfigs,
103 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700104
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800105bool createSensors(boost::asio::io_service& io,
106 sdbusplus::asio::object_server& objectServer,
107 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
108 boost::container::flat_set<CPUConfig>& cpuConfigs,
109 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700110{
111 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700113 {
114 if (cpu.state != State::OFF)
115 {
116 available = true;
James Feistc140e202019-11-14 15:23:51 -0800117 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
118 inventoryIfaces[cpu.name];
119 if (iface != nullptr)
120 {
121 continue;
122 }
123 iface = objectServer.add_interface(
124 cpuInventoryPath + std::string("/") + cpu.name,
125 "xyz.openbmc_project.Inventory.Item");
126 iface->register_property("PrettyName", cpu.name);
127 iface->register_property("Present", true);
128 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700129 }
130 }
131 if (!available)
132 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700133 return false;
James Feist6714a252018-09-10 15:26:18 -0700134 }
135
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800136 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700137 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800138 return false;
James Feist6714a252018-09-10 15:26:18 -0700139 }
140
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700141 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
143 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
144 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700145 {
146 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700147 return true;
James Feist6714a252018-09-10 15:26:18 -0700148 }
149
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700150 boost::container::flat_set<std::string> scannedDirectories;
151 boost::container::flat_set<std::string> createdSensors;
152
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800153 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700154 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700155 const std::string& pathStr = hwmonNamePath.string();
156 auto hwmonDirectory = hwmonNamePath.parent_path();
157
158 auto ret = scannedDirectories.insert(hwmonDirectory.string());
159 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700160 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700161 continue; // already searched this path
162 }
163
164 fs::path::iterator it = hwmonNamePath.begin();
165 std::advance(it, 6); // pick the 6th part for a PECI client device name
166 std::string deviceName = *it;
167 auto findHyphen = deviceName.find("-");
168 if (findHyphen == std::string::npos)
169 {
170 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700171 continue;
172 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700173 std::string busStr = deviceName.substr(0, findHyphen);
174 std::string addrStr = deviceName.substr(findHyphen + 1);
175
176 size_t bus = 0;
177 size_t addr = 0;
178 try
179 {
180 bus = std::stoi(busStr);
181 addr = std::stoi(addrStr, 0, 16);
182 }
James Feistb6c0b912019-07-09 12:21:44 -0700183 catch (std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700184 {
185 continue;
186 }
187
188 std::ifstream nameFile(hwmonNamePath);
189 if (!nameFile.good())
190 {
191 std::cerr << "Failure reading " << hwmonNamePath << "\n";
192 continue;
193 }
194 std::string hwmonName;
195 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700196 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800197 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700198 {
199 // shouldn't have an empty name file
200 continue;
201 }
James Feist6714a252018-09-10 15:26:18 -0700202 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 {
204 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
205 << "\n";
206 }
James Feist6714a252018-09-10 15:26:18 -0700207
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700208 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700209 const SensorData* sensorData = nullptr;
210 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700211 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700212
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800214 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700215 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700216 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700217 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700218 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700219 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700220 auto sensorBase = sensorData->find(sensorType);
221 if (sensorBase != sensorData->end())
222 {
223 baseConfiguration = &(*sensorBase);
224 break;
225 }
226 }
227 if (baseConfiguration == nullptr)
228 {
229 std::cerr << "error finding base configuration for" << hwmonName
230 << "\n";
231 continue;
232 }
233 auto configurationBus = baseConfiguration->second.find("Bus");
234 auto configurationAddress =
235 baseConfiguration->second.find("Address");
236
237 if (configurationBus == baseConfiguration->second.end() ||
238 configurationAddress == baseConfiguration->second.end())
239 {
240 std::cerr << "error finding bus or address in configuration";
241 continue;
242 }
243
James Feist3eb82622019-02-08 13:10:22 -0800244 if (std::get<uint64_t>(configurationBus->second) != bus ||
245 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 {
247 continue;
248 }
249
250 interfacePath = &(sensor.first.str);
251 break;
252 }
253 if (interfacePath == nullptr)
254 {
255 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700256 continue;
257 }
258
259 auto findCpuId = baseConfiguration->second.find("CpuID");
260 if (findCpuId == baseConfiguration->second.end())
261 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700262 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700263 continue;
264 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700265 int cpuId =
James Feist3eb82622019-02-08 13:10:22 -0800266 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700267
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700268 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700269 std::vector<fs::path> inputPaths;
James Feistb6c0b912019-07-09 12:21:44 -0700270 if (!findFiles(directory, R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700271 {
272 std::cerr << "No temperature sensors in system\n";
273 continue;
274 }
275
276 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800277 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700278 {
279 auto inputPathStr = inputPath.string();
280 auto labelPath =
281 boost::replace_all_copy(inputPathStr, "input", "label");
282 std::ifstream labelFile(labelPath);
283 if (!labelFile.good())
284 {
285 std::cerr << "Failure reading " << labelPath << "\n";
286 continue;
287 }
288 std::string label;
289 std::getline(labelFile, label);
290 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800291
James Feist6714a252018-09-10 15:26:18 -0700292 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700293
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800294 auto findSensor = gCpuSensors.find(sensorName);
295 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700296 {
297 if (DEBUG)
298 {
299 std::cout << "Skipped: " << inputPath << ": " << sensorName
300 << " is already created\n";
301 }
302 continue;
303 }
304
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800305 // check hidden properties
306 bool show = true;
307 for (const char* prop : hiddenProps)
308 {
309 if (label == prop)
310 {
311 show = false;
312 break;
313 }
314 }
315
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700316 /*
317 * Find if there is DtsCritOffset is configured in config file
318 * set it if configured or else set it to 0
319 */
320 double dtsOffset = 0;
321 if (label == "DTS")
322 {
323 auto findThrOffset =
324 baseConfiguration->second.find("DtsCritOffset");
325 if (findThrOffset != baseConfiguration->second.end())
326 {
327 dtsOffset = std::visit(VariantToDoubleVisitor(),
328 findThrOffset->second);
329 }
330 }
331
James Feist6714a252018-09-10 15:26:18 -0700332 std::vector<thresholds::Threshold> sensorThresholds;
333 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700334 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700335 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800336 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700337 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700338 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700339 CPUSensor::sensorScaleFactor,
340 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700341 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700342 std::cerr << "error populating thresholds for "
343 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700344 }
345 }
James Feistc140e202019-11-14 15:23:51 -0800346 auto& sensorPtr = gCpuSensors[sensorName];
347 // make sure destructor fires before creating a new one
348 sensorPtr = nullptr;
349 sensorPtr = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700350 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800351 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700352 show, dtsOffset);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700353 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700354 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700355 {
James Feist6714a252018-09-10 15:26:18 -0700356 std::cout << "Mapped: " << inputPath << " to " << sensorName
357 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700358 }
James Feist6714a252018-09-10 15:26:18 -0700359 }
360 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700361
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700362 if (createdSensors.size())
363 {
364 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
365 << " created\n";
366 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700367
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700368 return true;
James Feist6714a252018-09-10 15:26:18 -0700369}
370
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700371void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700372{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700373 std::ostringstream hex;
374 hex << std::hex << config.addr;
375 const std::string& addrHexStr = hex.str();
376 std::string busStr = std::to_string(config.bus);
377
378 std::string parameters = "peci-client 0x" + addrHexStr;
379 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
380
James Feistcf3bce62019-01-08 10:07:19 -0800381 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700382 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800383 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700384 {
James Feistcf3bce62019-01-08 10:07:19 -0800385 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700386 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700387 continue;
James Feist6714a252018-09-10 15:26:18 -0700388 }
389
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700390 const std::string& directoryName = path.path().filename();
391 if (boost::starts_with(directoryName, busStr) &&
392 boost::ends_with(directoryName, addrHexStr))
393 {
394 if (DEBUG)
395 {
396 std::cout << parameters << " on bus " << busStr
397 << " is already exported\n";
398 }
399 return;
400 }
James Feist6714a252018-09-10 15:26:18 -0700401 }
402
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700403 std::ofstream deviceFile(device);
404 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700405 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700406 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700407 return;
408 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700409 deviceFile << parameters;
410 deviceFile.close();
411
412 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700413}
414
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800415void detectCpu(boost::asio::deadline_timer& pingTimer,
416 boost::asio::deadline_timer& creationTimer,
417 boost::asio::io_service& io,
418 sdbusplus::asio::object_server& objectServer,
419 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
420 boost::container::flat_set<CPUConfig>& cpuConfigs,
421 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700422{
James Feist6714a252018-09-10 15:26:18 -0700423 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700424 static bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700425
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800426 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700427 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700428 std::string peciDevPath = peciDev + std::to_string(config.bus);
429 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
430 if (file < 0)
431 {
432 std::cerr << "unable to open " << peciDevPath << "\n";
433 std::exit(EXIT_FAILURE);
434 }
435
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700436 State newState;
James Feist6714a252018-09-10 15:26:18 -0700437 struct peci_ping_msg msg;
438 msg.addr = config.addr;
439 if (!ioctl(file, PECI_IOC_PING, &msg))
440 {
441 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700442 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700443 {
444 struct peci_rd_pkg_cfg_msg msg;
445 msg.addr = config.addr;
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -0800446 msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP;
James Feist6714a252018-09-10 15:26:18 -0700447 msg.param = rank;
448 msg.rx_len = 4;
449 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
450 {
451 if (msg.pkg_config[0] || msg.pkg_config[1] ||
452 msg.pkg_config[2])
453 {
454 dimmReady = true;
455 break;
456 }
457 }
458 else
459 {
460 break;
461 }
462 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700463
James Feist6714a252018-09-10 15:26:18 -0700464 if (dimmReady)
465 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700466 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700467 }
468 else
469 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700470 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700471 }
472 }
473 else
474 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700475 newState = State::OFF;
James Feist6714a252018-09-10 15:26:18 -0700476 }
477
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700478 close(file);
479
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700480 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700481 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700482 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700483 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700484 if (config.state == State::OFF)
485 {
486 std::cout << config.name << " is detected\n";
487 exportDevice(config);
488 }
James Feist6714a252018-09-10 15:26:18 -0700489
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700490 if (newState == State::ON)
491 {
492 rescanDelaySeconds = 3;
493 }
494 else if (newState == State::READY)
495 {
496 rescanDelaySeconds = 5;
497 std::cout << "DIMM(s) on " << config.name
498 << " is/are detected\n";
499 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700500 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700501
502 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700503 }
504
505 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700506 {
507 keepPinging = true;
508 }
509
510 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700511 {
512 std::cout << config.name << ", state: " << config.state << "\n";
513 }
James Feist6714a252018-09-10 15:26:18 -0700514 }
515
James Feist6714a252018-09-10 15:26:18 -0700516 if (rescanDelaySeconds)
517 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700518 creationTimer.expires_from_now(
519 boost::posix_time::seconds(rescanDelaySeconds));
520 creationTimer.async_wait([&](const boost::system::error_code& ec) {
521 if (ec == boost::asio::error::operation_aborted)
522 {
523 return; // we're being canceled
524 }
525
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800526 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700527 sensorConfigs) ||
528 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700529 {
530 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800531 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700532 }
533 });
James Feist6714a252018-09-10 15:26:18 -0700534 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700535 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700536 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800537 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800538 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700539 }
540}
541
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700542void detectCpuAsync(
543 boost::asio::deadline_timer& pingTimer,
544 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
545 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800546 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800547 boost::container::flat_set<CPUConfig>& cpuConfigs,
548 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700549{
550 pingTimer.expires_from_now(boost::posix_time::seconds(1));
551 pingTimer.async_wait([&](const boost::system::error_code& ec) {
552 if (ec == boost::asio::error::operation_aborted)
553 {
554 return; // we're being canceled
555 }
556
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800557 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800558 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700559 });
560}
561
James Feistc140e202019-11-14 15:23:51 -0800562bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
563 boost::container::flat_set<CPUConfig>& cpuConfigs,
564 ManagedObjectType& sensorConfigs,
565 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700566{
James Feist6714a252018-09-10 15:26:18 -0700567 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800568 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700569 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700570 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700571 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700572 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800573 sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700574 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700575 return false;
James Feist6714a252018-09-10 15:26:18 -0700576 }
577 useCache = true;
578 }
579
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700580 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700581 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700582 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700583 {
584 for (const std::pair<sdbusplus::message::object_path, SensorData>&
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800585 sensor : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700586 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700587 for (const SensorBaseConfiguration& config : sensor.second)
James Feist6714a252018-09-10 15:26:18 -0700588 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700589 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700590 {
591 continue;
592 }
593
James Feist6714a252018-09-10 15:26:18 -0700594 auto findName = config.second.find("Name");
595 if (findName == config.second.end())
596 {
597 continue;
598 }
James Feist3eb82622019-02-08 13:10:22 -0800599 std::string nameRaw =
600 std::visit(VariantToStringVisitor(), findName->second);
James Feist6714a252018-09-10 15:26:18 -0700601 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700602 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700603
James Feistc140e202019-11-14 15:23:51 -0800604 auto present = std::optional<bool>();
605 // if we can't detect it via gpio, we set presence later
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700606 for (const SensorBaseConfiguration& suppConfig : sensor.second)
James Feist58295ad2019-05-30 15:01:41 -0700607 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700608 if (suppConfig.first.find("PresenceGpio") !=
609 std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700610 {
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700611 present = cpuIsPresent(suppConfig.second);
612 break;
James Feist58295ad2019-05-30 15:01:41 -0700613 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700614 }
615
James Feistc140e202019-11-14 15:23:51 -0800616 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
617 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700618 {
619 auto iface = objectServer.add_interface(
620 cpuInventoryPath + std::string("/") + name,
621 "xyz.openbmc_project.Inventory.Item");
622 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800623 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700624 iface->initialize();
625 inventoryIfaces[name] = std::move(iface);
626 }
James Feist58295ad2019-05-30 15:01:41 -0700627
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700628 auto findBus = config.second.find("Bus");
629 if (findBus == config.second.end())
630 {
631 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
632 continue;
633 }
James Feist3eb82622019-02-08 13:10:22 -0800634 uint64_t bus =
635 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700636
637 auto findAddress = config.second.find("Address");
638 if (findAddress == config.second.end())
639 {
640 std::cerr << "Can't find 'Address' setting in " << name
641 << "\n";
642 continue;
643 }
James Feist3eb82622019-02-08 13:10:22 -0800644 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
645 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700646
647 if (DEBUG)
648 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700649 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700650 std::cout << "addr: " << addr << "\n";
651 std::cout << "name: " << name << "\n";
652 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700653 }
654
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800655 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700656 }
657 }
658 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700659
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800660 if (cpuConfigs.size())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700661 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800662 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700663 << " parsed\n";
664 return true;
665 }
666
667 return false;
James Feist6714a252018-09-10 15:26:18 -0700668}
669
James Feistb6c0b912019-07-09 12:21:44 -0700670int main()
James Feist6714a252018-09-10 15:26:18 -0700671{
672 boost::asio::io_service io;
673 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800674 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700675
676 systemBus->request_name("xyz.openbmc_project.CPUSensor");
677 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6714a252018-09-10 15:26:18 -0700678 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
679 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700680 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700681 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800682 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700683
684 filterTimer.expires_from_now(boost::posix_time::seconds(1));
685 filterTimer.async_wait([&](const boost::system::error_code& ec) {
686 if (ec == boost::asio::error::operation_aborted)
687 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700688 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700689 }
690
James Feistc140e202019-11-14 15:23:51 -0800691 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700692 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800693 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800694 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700695 }
696 });
697
James Feist6714a252018-09-10 15:26:18 -0700698 std::function<void(sdbusplus::message::message&)> eventHandler =
699 [&](sdbusplus::message::message& message) {
700 if (message.is_method_error())
701 {
702 std::cerr << "callback method error\n";
703 return;
704 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700705
706 if (DEBUG)
707 {
708 std::cout << message.get_path() << " is changed\n";
709 }
710
James Feist6714a252018-09-10 15:26:18 -0700711 // this implicitly cancels the timer
712 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700713 filterTimer.async_wait([&](const boost::system::error_code& ec) {
714 if (ec == boost::asio::error::operation_aborted)
715 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700716 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700717 }
718
James Feist58295ad2019-05-30 15:01:41 -0700719 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
James Feistc140e202019-11-14 15:23:51 -0800720 objectServer))
James Feist6714a252018-09-10 15:26:18 -0700721 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700722 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800723 systemBus, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700724 }
725 });
726 };
727
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700728 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700729 {
730 auto match = std::make_unique<sdbusplus::bus::match::match>(
731 static_cast<sdbusplus::bus::bus&>(*systemBus),
732 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700733 std::string(inventoryPath) + "',arg0namespace='" +
734 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700735 eventHandler);
736 matches.emplace_back(std::move(match));
737 }
738
739 io.run();
740}