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