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