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