James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame^] | 17 | #include "IntelCPUSensor.hpp" |
| 18 | #include "Utils.hpp" |
| 19 | #include "VariantVisitors.hpp" |
| 20 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 24 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 26 | #include <sdbusplus/asio/connection.hpp> |
| 27 | #include <sdbusplus/asio/object_server.hpp> |
| 28 | #include <sdbusplus/bus/match.hpp> |
| 29 | |
| 30 | #include <array> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 31 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <functional> |
| 34 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 36 | #include <sstream> |
| 37 | #include <stdexcept> |
| 38 | #include <string> |
| 39 | #include <utility> |
| 40 | #include <variant> |
| 41 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 43 | // clang-format off |
| 44 | // this needs to be included last or we'll have build issues |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 45 | extern "C" { |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 46 | #include <linux/peci-ioctl.h> |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 47 | } |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 48 | #if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP) |
| 49 | #define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP |
| 50 | #endif |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 51 | // clang-format on |
| 52 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 53 | static constexpr bool debug = false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 54 | |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 55 | boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>> |
| 56 | gCpuSensors; |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 57 | boost::container::flat_map<std::string, |
| 58 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 59 | inventoryIfaces; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 60 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | enum State |
| 62 | { |
| 63 | OFF, // host powered down |
| 64 | ON, // host powered on |
| 65 | READY // host powered on and mem test passed - fully ready |
| 66 | }; |
| 67 | |
| 68 | struct CPUConfig |
| 69 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 70 | CPUConfig(const uint64_t& bus, const uint64_t& addr, |
| 71 | const std::string& name, const State& state) : |
| 72 | bus(bus), |
| 73 | addr(addr), name(name), state(state) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 74 | {} |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 75 | int bus; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | int addr; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 77 | std::string name; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 78 | State state; |
| 79 | |
| 80 | bool operator<(const CPUConfig& rhs) const |
| 81 | { |
Andrew Jeffery | 92b9629 | 2021-05-27 16:41:31 +0930 | [diff] [blame] | 82 | // NOLINTNEXTLINE |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 83 | return (name < rhs.name); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 87 | static constexpr const char* peciDev = "/dev/peci-"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | static constexpr const unsigned int rankNumMax = 8; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 89 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 90 | namespace fs = std::filesystem; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 91 | |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 92 | static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})}; |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 93 | static constexpr auto hiddenProps{std::to_array<const char*>( |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 94 | {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 95 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 96 | void detectCpuAsync( |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 97 | boost::asio::steady_timer& pingTimer, |
| 98 | boost::asio::steady_timer& creationTimer, boost::asio::io_service& io, |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 99 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 100 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 101 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 102 | ManagedObjectType& sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 103 | |
Zbigniew Kurzynski | 0eee0c1 | 2020-06-18 14:20:08 +0200 | [diff] [blame] | 104 | std::string createSensorName(const std::string& label, const std::string& item, |
| 105 | const int& cpuId) |
| 106 | { |
| 107 | std::string sensorName = label; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 108 | if (item != "input") |
Zbigniew Kurzynski | 0eee0c1 | 2020-06-18 14:20:08 +0200 | [diff] [blame] | 109 | { |
| 110 | sensorName += " " + item; |
| 111 | } |
| 112 | sensorName += " CPU" + std::to_string(cpuId); |
| 113 | // converting to Upper Camel case whole name |
| 114 | bool isWordEnd = true; |
| 115 | std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(), |
| 116 | [&isWordEnd](int c) { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 117 | if (std::isspace(c) != 0) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 118 | { |
| 119 | isWordEnd = true; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if (isWordEnd) |
| 124 | { |
| 125 | isWordEnd = false; |
| 126 | return std::toupper(c); |
| 127 | } |
| 128 | } |
| 129 | return c; |
| 130 | }); |
Zbigniew Kurzynski | 0eee0c1 | 2020-06-18 14:20:08 +0200 | [diff] [blame] | 131 | return sensorName; |
| 132 | } |
| 133 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 134 | bool createSensors(boost::asio::io_service& io, |
| 135 | sdbusplus::asio::object_server& objectServer, |
| 136 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 137 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 138 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | { |
| 140 | bool available = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 141 | for (const CPUConfig& cpu : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | { |
| 143 | if (cpu.state != State::OFF) |
| 144 | { |
| 145 | available = true; |
Zev Weiss | 9702c9d | 2021-04-21 22:41:51 -0500 | [diff] [blame] | 146 | std::shared_ptr<sdbusplus::asio::dbus_interface>& iface = |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 147 | inventoryIfaces[cpu.name]; |
| 148 | if (iface != nullptr) |
| 149 | { |
| 150 | continue; |
| 151 | } |
| 152 | iface = objectServer.add_interface( |
| 153 | cpuInventoryPath + std::string("/") + cpu.name, |
| 154 | "xyz.openbmc_project.Inventory.Item"); |
| 155 | iface->register_property("PrettyName", cpu.name); |
| 156 | iface->register_property("Present", true); |
| 157 | iface->initialize(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | if (!available) |
| 161 | { |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 162 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 165 | if (sensorConfigs.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 167 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 170 | std::vector<fs::path> hwmonNamePaths; |
Lei YU | 0b207a6 | 2021-10-20 13:41:51 +0800 | [diff] [blame] | 171 | if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"), |
| 172 | R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | { |
| 174 | std::cerr << "No CPU sensors in system\n"; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 175 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 178 | boost::container::flat_set<std::string> scannedDirectories; |
| 179 | boost::container::flat_set<std::string> createdSensors; |
| 180 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 181 | for (const fs::path& hwmonNamePath : hwmonNamePaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 182 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 183 | auto hwmonDirectory = hwmonNamePath.parent_path(); |
| 184 | |
| 185 | auto ret = scannedDirectories.insert(hwmonDirectory.string()); |
| 186 | if (!ret.second) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 188 | continue; // already searched this path |
| 189 | } |
| 190 | |
| 191 | fs::path::iterator it = hwmonNamePath.begin(); |
| 192 | std::advance(it, 6); // pick the 6th part for a PECI client device name |
| 193 | std::string deviceName = *it; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 194 | auto findHyphen = deviceName.find('-'); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 195 | if (findHyphen == std::string::npos) |
| 196 | { |
| 197 | std::cerr << "found bad device " << deviceName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 198 | continue; |
| 199 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 200 | std::string busStr = deviceName.substr(0, findHyphen); |
| 201 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 202 | |
| 203 | size_t bus = 0; |
| 204 | size_t addr = 0; |
| 205 | try |
| 206 | { |
| 207 | bus = std::stoi(busStr); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 208 | addr = std::stoi(addrStr, nullptr, 16); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 209 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 210 | catch (const std::invalid_argument&) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 211 | { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | std::ifstream nameFile(hwmonNamePath); |
| 216 | if (!nameFile.good()) |
| 217 | { |
| 218 | std::cerr << "Failure reading " << hwmonNamePath << "\n"; |
| 219 | continue; |
| 220 | } |
| 221 | std::string hwmonName; |
| 222 | std::getline(nameFile, hwmonName); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 223 | nameFile.close(); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 224 | if (hwmonName.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 225 | { |
| 226 | // shouldn't have an empty name file |
| 227 | continue; |
| 228 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 229 | if (debug) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 230 | { |
| 231 | std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName |
| 232 | << "\n"; |
| 233 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 234 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 235 | std::string sensorType; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 236 | const SensorData* sensorData = nullptr; |
| 237 | const std::string* interfacePath = nullptr; |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 238 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 239 | |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 240 | for (const auto& [path, cfgData] : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 241 | { |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 242 | sensorData = &cfgData; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 243 | for (const char* type : sensorTypes) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 244 | { |
Zev Weiss | 26fb149 | 2022-08-17 15:33:46 -0700 | [diff] [blame] | 245 | sensorType = type; |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 246 | auto sensorBase = |
| 247 | sensorData->find(configInterfaceName(sensorType)); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 248 | if (sensorBase != sensorData->end()) |
| 249 | { |
| 250 | baseConfiguration = &(*sensorBase); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | if (baseConfiguration == nullptr) |
| 255 | { |
| 256 | std::cerr << "error finding base configuration for" << hwmonName |
| 257 | << "\n"; |
| 258 | continue; |
| 259 | } |
| 260 | auto configurationBus = baseConfiguration->second.find("Bus"); |
| 261 | auto configurationAddress = |
| 262 | baseConfiguration->second.find("Address"); |
| 263 | |
| 264 | if (configurationBus == baseConfiguration->second.end() || |
| 265 | configurationAddress == baseConfiguration->second.end()) |
| 266 | { |
| 267 | std::cerr << "error finding bus or address in configuration"; |
| 268 | continue; |
| 269 | } |
| 270 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 271 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 272 | std::get<uint64_t>(configurationAddress->second) != addr) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 273 | { |
| 274 | continue; |
| 275 | } |
| 276 | |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 277 | interfacePath = &path.str; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | if (interfacePath == nullptr) |
| 281 | { |
| 282 | std::cerr << "failed to find match for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 283 | continue; |
| 284 | } |
| 285 | |
| 286 | auto findCpuId = baseConfiguration->second.find("CpuID"); |
| 287 | if (findCpuId == baseConfiguration->second.end()) |
| 288 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 289 | std::cerr << "could not determine CPU ID for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 290 | continue; |
| 291 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 292 | int cpuId = |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 293 | std::visit(VariantToUnsignedIntVisitor(), findCpuId->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 294 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 295 | auto directory = hwmonNamePath.parent_path(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 296 | std::vector<fs::path> inputPaths; |
Zbigniew Kurzynski | 8d8d8d7 | 2020-05-29 19:21:24 +0200 | [diff] [blame] | 297 | if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)", |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 298 | inputPaths, 0)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 299 | { |
| 300 | std::cerr << "No temperature sensors in system\n"; |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | // iterate through all found temp sensors |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 305 | for (const auto& inputPath : inputPaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 306 | { |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 307 | auto fileParts = splitFileName(inputPath); |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 308 | if (!fileParts) |
| 309 | { |
| 310 | continue; |
| 311 | } |
Zbigniew Kurzynski | dbfd466 | 2020-09-28 18:06:00 +0200 | [diff] [blame] | 312 | auto& [type, nr, item] = *fileParts; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 313 | auto inputPathStr = inputPath.string(); |
| 314 | auto labelPath = |
Zbigniew Kurzynski | 0a4c480 | 2020-04-01 11:22:27 +0200 | [diff] [blame] | 315 | boost::replace_all_copy(inputPathStr, item, "label"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 316 | std::ifstream labelFile(labelPath); |
| 317 | if (!labelFile.good()) |
| 318 | { |
| 319 | std::cerr << "Failure reading " << labelPath << "\n"; |
| 320 | continue; |
| 321 | } |
| 322 | std::string label; |
| 323 | std::getline(labelFile, label); |
| 324 | labelFile.close(); |
Jae Hyun Yoo | 13f4888 | 2019-02-19 13:37:07 -0800 | [diff] [blame] | 325 | |
Zbigniew Kurzynski | 0eee0c1 | 2020-06-18 14:20:08 +0200 | [diff] [blame] | 326 | std::string sensorName = createSensorName(label, item, cpuId); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 327 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 328 | auto findSensor = gCpuSensors.find(sensorName); |
| 329 | if (findSensor != gCpuSensors.end()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 330 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 331 | if (debug) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 332 | { |
| 333 | std::cout << "Skipped: " << inputPath << ": " << sensorName |
| 334 | << " is already created\n"; |
| 335 | } |
| 336 | continue; |
| 337 | } |
| 338 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 339 | // check hidden properties |
| 340 | bool show = true; |
| 341 | for (const char* prop : hiddenProps) |
| 342 | { |
| 343 | if (label == prop) |
| 344 | { |
| 345 | show = false; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 350 | /* |
| 351 | * Find if there is DtsCritOffset is configured in config file |
| 352 | * set it if configured or else set it to 0 |
| 353 | */ |
| 354 | double dtsOffset = 0; |
| 355 | if (label == "DTS") |
| 356 | { |
| 357 | auto findThrOffset = |
| 358 | baseConfiguration->second.find("DtsCritOffset"); |
| 359 | if (findThrOffset != baseConfiguration->second.end()) |
| 360 | { |
| 361 | dtsOffset = std::visit(VariantToDoubleVisitor(), |
| 362 | findThrOffset->second); |
| 363 | } |
| 364 | } |
| 365 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 366 | std::vector<thresholds::Threshold> sensorThresholds; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 367 | std::string labelHead = label.substr(0, label.find(' ')); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 368 | parseThresholdsFromConfig(*sensorData, sensorThresholds, |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 369 | &labelHead); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 370 | if (sensorThresholds.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 371 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 372 | if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr, |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 373 | IntelCPUSensor::sensorScaleFactor, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 374 | dtsOffset)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 375 | { |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 376 | std::cerr << "error populating thresholds for " |
| 377 | << sensorName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 380 | auto& sensorPtr = gCpuSensors[sensorName]; |
| 381 | // make sure destructor fires before creating a new one |
| 382 | sensorPtr = nullptr; |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 383 | sensorPtr = std::make_shared<IntelCPUSensor>( |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 384 | inputPathStr, sensorType, objectServer, dbusConnection, io, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 385 | sensorName, std::move(sensorThresholds), *interfacePath, cpuId, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 386 | show, dtsOffset); |
Arun P. Mohanan | 04d0506 | 2021-10-29 20:30:26 +0530 | [diff] [blame] | 387 | sensorPtr->setupRead(); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 388 | createdSensors.insert(sensorName); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 389 | if (debug) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 390 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 391 | std::cout << "Mapped: " << inputPath << " to " << sensorName |
| 392 | << "\n"; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 393 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 396 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 397 | if (static_cast<unsigned int>(!createdSensors.empty()) != 0U) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 398 | { |
| 399 | std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are") |
| 400 | << " created\n"; |
| 401 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 402 | |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 403 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 406 | void exportDevice(const CPUConfig& config) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 407 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 408 | std::ostringstream hex; |
| 409 | hex << std::hex << config.addr; |
| 410 | const std::string& addrHexStr = hex.str(); |
| 411 | std::string busStr = std::to_string(config.bus); |
| 412 | |
| 413 | std::string parameters = "peci-client 0x" + addrHexStr; |
| 414 | std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device"; |
| 415 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 416 | std::filesystem::path devicePath(device); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 417 | const std::string& dir = devicePath.parent_path().string(); |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 418 | for (const auto& path : std::filesystem::directory_iterator(dir)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 419 | { |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 420 | if (!std::filesystem::is_directory(path)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 421 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 422 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 425 | const std::string& directoryName = path.path().filename(); |
Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 426 | if (directoryName.starts_with(busStr) && |
| 427 | directoryName.ends_with(addrHexStr)) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 428 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 429 | if (debug) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 430 | { |
| 431 | std::cout << parameters << " on bus " << busStr |
| 432 | << " is already exported\n"; |
| 433 | } |
| 434 | return; |
| 435 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 438 | std::ofstream deviceFile(device); |
| 439 | if (!deviceFile.good()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 440 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 441 | std::cerr << "Error writing " << device << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 444 | deviceFile << parameters; |
| 445 | deviceFile.close(); |
| 446 | |
| 447 | std::cout << parameters << " on bus " << busStr << " is exported\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 450 | void detectCpu(boost::asio::steady_timer& pingTimer, |
| 451 | boost::asio::steady_timer& creationTimer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 452 | boost::asio::io_service& io, |
| 453 | sdbusplus::asio::object_server& objectServer, |
| 454 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 455 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 456 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 457 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 458 | size_t rescanDelaySeconds = 0; |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 459 | static bool keepPinging = false; |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 460 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 461 | for (CPUConfig& config : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 462 | { |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 463 | std::string peciDevPath = peciDev + std::to_string(config.bus); |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 464 | |
| 465 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 466 | auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC); |
| 467 | if (file < 0) |
| 468 | { |
| 469 | std::cerr << "unable to open " << peciDevPath << "\n"; |
| 470 | std::exit(EXIT_FAILURE); |
| 471 | } |
| 472 | |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 473 | State newState = State::OFF; |
| 474 | struct peci_ping_msg msg |
| 475 | {}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 476 | msg.addr = config.addr; |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 477 | |
| 478 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 479 | if (ioctl(file, PECI_IOC_PING, &msg) == 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 480 | { |
| 481 | bool dimmReady = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 482 | for (unsigned int rank = 0; rank < rankNumMax; rank++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 483 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 484 | struct peci_rd_pkg_cfg_msg msg |
| 485 | {}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 486 | msg.addr = config.addr; |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 487 | msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 488 | msg.param = rank; |
| 489 | msg.rx_len = 4; |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 490 | |
| 491 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 492 | if (ioctl(file, PECI_IOC_RD_PKG_CFG, &msg) == 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 493 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 494 | if ((msg.pkg_config[0] != 0U) || |
| 495 | (msg.pkg_config[1] != 0U) || (msg.pkg_config[2] != 0U)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 496 | { |
| 497 | dimmReady = true; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | break; |
| 504 | } |
| 505 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 506 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 507 | if (dimmReady) |
| 508 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 509 | newState = State::READY; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 510 | } |
| 511 | else |
| 512 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 513 | newState = State::ON; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 516 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 517 | close(file); |
| 518 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 519 | if (config.state != newState) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 520 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 521 | if (newState != State::OFF) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 522 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 523 | if (config.state == State::OFF) |
| 524 | { |
| 525 | std::cout << config.name << " is detected\n"; |
| 526 | exportDevice(config); |
| 527 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 528 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 529 | if (newState == State::ON) |
| 530 | { |
| 531 | rescanDelaySeconds = 3; |
| 532 | } |
| 533 | else if (newState == State::READY) |
| 534 | { |
| 535 | rescanDelaySeconds = 5; |
| 536 | std::cout << "DIMM(s) on " << config.name |
| 537 | << " is/are detected\n"; |
| 538 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 539 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 540 | |
| 541 | config.state = newState; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (config.state != State::READY) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 545 | { |
| 546 | keepPinging = true; |
| 547 | } |
| 548 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 549 | if (debug) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 550 | { |
| 551 | std::cout << config.name << ", state: " << config.state << "\n"; |
| 552 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 555 | if (rescanDelaySeconds != 0U) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 556 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 557 | creationTimer.expires_from_now( |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 558 | std::chrono::seconds(rescanDelaySeconds)); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 559 | creationTimer.async_wait([&](const boost::system::error_code& ec) { |
| 560 | if (ec == boost::asio::error::operation_aborted) |
| 561 | { |
| 562 | return; // we're being canceled |
| 563 | } |
| 564 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 565 | if (!createSensors(io, objectServer, dbusConnection, cpuConfigs, |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 566 | sensorConfigs) || |
| 567 | keepPinging) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 568 | { |
| 569 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 570 | dbusConnection, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 571 | } |
| 572 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 573 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 574 | else if (keepPinging) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 575 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 576 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 577 | dbusConnection, cpuConfigs, sensorConfigs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 581 | void detectCpuAsync( |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 582 | boost::asio::steady_timer& pingTimer, |
| 583 | boost::asio::steady_timer& creationTimer, boost::asio::io_service& io, |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 584 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 585 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 586 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 587 | ManagedObjectType& sensorConfigs) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 588 | { |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 589 | pingTimer.expires_from_now(std::chrono::seconds(1)); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 590 | pingTimer.async_wait([&](const boost::system::error_code& ec) { |
| 591 | if (ec == boost::asio::error::operation_aborted) |
| 592 | { |
| 593 | return; // we're being canceled |
| 594 | } |
| 595 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 596 | detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 597 | cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 598 | }); |
| 599 | } |
| 600 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 601 | bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
| 602 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 603 | ManagedObjectType& sensorConfigs, |
| 604 | sdbusplus::asio::object_server& objectServer) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 605 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 606 | bool useCache = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 607 | sensorConfigs.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 608 | // use new data the first time, then refresh |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 609 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 610 | { |
Zev Weiss | 26fb149 | 2022-08-17 15:33:46 -0700 | [diff] [blame] | 611 | if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 612 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 613 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 614 | } |
| 615 | useCache = true; |
| 616 | } |
| 617 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 618 | // check PECI client addresses and names from CPU configuration |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 619 | // before starting ping operation |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 620 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 621 | { |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 622 | for (const auto& [path, cfgData] : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 623 | { |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 624 | for (const auto& [intf, cfg] : cfgData) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 625 | { |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 626 | if (intf != configInterfaceName(type)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 627 | { |
| 628 | continue; |
| 629 | } |
| 630 | |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 631 | auto findName = cfg.find("Name"); |
| 632 | if (findName == cfg.end()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 633 | { |
| 634 | continue; |
| 635 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 636 | std::string nameRaw = |
| 637 | std::visit(VariantToStringVisitor(), findName->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 638 | std::string name = |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 639 | std::regex_replace(nameRaw, illegalDbusRegex, "_"); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 640 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 641 | auto present = std::optional<bool>(); |
| 642 | // if we can't detect it via gpio, we set presence later |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 643 | for (const auto& [suppIntf, suppCfg] : cfgData) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 644 | { |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 645 | if (suppIntf.find("PresenceGpio") != std::string::npos) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 646 | { |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 647 | present = cpuIsPresent(suppCfg); |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 648 | break; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 649 | } |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 650 | } |
| 651 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 652 | if (inventoryIfaces.find(name) == inventoryIfaces.end() && |
| 653 | present) |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 654 | { |
| 655 | auto iface = objectServer.add_interface( |
| 656 | cpuInventoryPath + std::string("/") + name, |
| 657 | "xyz.openbmc_project.Inventory.Item"); |
| 658 | iface->register_property("PrettyName", name); |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 659 | iface->register_property("Present", *present); |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 660 | iface->initialize(); |
| 661 | inventoryIfaces[name] = std::move(iface); |
| 662 | } |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 663 | |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 664 | auto findBus = cfg.find("Bus"); |
| 665 | if (findBus == cfg.end()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 666 | { |
| 667 | std::cerr << "Can't find 'Bus' setting in " << name << "\n"; |
| 668 | continue; |
| 669 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 670 | uint64_t bus = |
| 671 | std::visit(VariantToUnsignedIntVisitor(), findBus->second); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 672 | |
Zev Weiss | 8908b3c | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 673 | auto findAddress = cfg.find("Address"); |
| 674 | if (findAddress == cfg.end()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 675 | { |
| 676 | std::cerr << "Can't find 'Address' setting in " << name |
| 677 | << "\n"; |
| 678 | continue; |
| 679 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 680 | uint64_t addr = std::visit(VariantToUnsignedIntVisitor(), |
| 681 | findAddress->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 682 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 683 | if (debug) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 684 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 685 | std::cout << "bus: " << bus << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 686 | std::cout << "addr: " << addr << "\n"; |
| 687 | std::cout << "name: " << name << "\n"; |
| 688 | std::cout << "type: " << type << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 691 | cpuConfigs.emplace(bus, addr, name, State::OFF); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 695 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 696 | if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 697 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 698 | std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are") |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 699 | << " parsed\n"; |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 704 | } |
| 705 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 706 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 707 | { |
| 708 | boost::asio::io_service io; |
| 709 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 710 | boost::container::flat_set<CPUConfig> cpuConfigs; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 711 | |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 712 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 713 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 714 | boost::asio::steady_timer pingTimer(io); |
| 715 | boost::asio::steady_timer creationTimer(io); |
| 716 | boost::asio::steady_timer filterTimer(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 717 | ManagedObjectType sensorConfigs; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 718 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 719 | filterTimer.expires_from_now(std::chrono::seconds(1)); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 720 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 721 | if (ec == boost::asio::error::operation_aborted) |
| 722 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 723 | return; // we're being canceled |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 724 | } |
| 725 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame] | 726 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer)) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 727 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 728 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 729 | systemBus, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 730 | } |
| 731 | }); |
| 732 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 733 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 734 | [&](sdbusplus::message_t& message) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 735 | if (message.is_method_error()) |
| 736 | { |
| 737 | std::cerr << "callback method error\n"; |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | if (debug) |
| 742 | { |
| 743 | std::cout << message.get_path() << " is changed\n"; |
| 744 | } |
| 745 | |
| 746 | // this implicitly cancels the timer |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 747 | filterTimer.expires_from_now(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 748 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 749 | if (ec == boost::asio::error::operation_aborted) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 750 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 751 | return; // we're being canceled |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 752 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 753 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 754 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, |
| 755 | objectServer)) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 756 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 757 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
| 758 | systemBus, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 759 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 760 | }); |
| 761 | }; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 762 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 763 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 764 | setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 765 | |
Thu Nguyen | 255da6b | 2022-07-29 10:05:52 +0700 | [diff] [blame] | 766 | systemBus->request_name("xyz.openbmc_project.IntelCPUSensor"); |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 767 | |
| 768 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 769 | io.run(); |
Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 770 | return 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 771 | } |