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