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