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