James Feist | 139cb57 | 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> |
| 18 | #include <linux/peci-ioctl.h> |
| 19 | |
| 20 | #include <CPUSensor.hpp> |
| 21 | #include <Utils.hpp> |
| 22 | #include <VariantVisitors.hpp> |
| 23 | #include <boost/algorithm/string/predicate.hpp> |
| 24 | #include <boost/algorithm/string/replace.hpp> |
| 25 | #include <boost/container/flat_set.hpp> |
| 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 27 | #include <boost/process/child.hpp> |
| 28 | #include <experimental/filesystem> |
| 29 | #include <fstream> |
| 30 | #include <regex> |
| 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
| 33 | |
| 34 | static constexpr bool DEBUG = false; |
| 35 | |
| 36 | enum State |
| 37 | { |
| 38 | OFF, // host powered down |
| 39 | ON, // host powered on |
| 40 | READY // host powered on and mem test passed - fully ready |
| 41 | }; |
| 42 | |
| 43 | struct CPUConfig |
| 44 | { |
| 45 | CPUConfig(const int& address, const std::string& overlayName, |
| 46 | const State& st) : |
| 47 | addr(address), |
| 48 | ovName(overlayName), state(st) |
| 49 | { |
| 50 | } |
| 51 | int addr; |
| 52 | std::string ovName; |
| 53 | State state; |
| 54 | |
| 55 | bool operator<(const CPUConfig& rhs) const |
| 56 | { |
| 57 | return (ovName < rhs.ovName); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | static constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay"; |
| 62 | static constexpr const char* OVERLAY_DIR = "/tmp/overlays"; |
Yoo, Jae Hyun | f033e67 | 2018-10-17 12:07:54 -0700 | [diff] [blame] | 63 | static constexpr const char* PECI_DEV = "/dev/peci-0"; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | static constexpr const unsigned int RANK_NUM_MAX = 8; |
| 65 | |
| 66 | namespace fs = std::experimental::filesystem; |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame^] | 67 | namespace variant_ns = sdbusplus::message::variant_ns; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | static constexpr const char* CONFIG_PREFIX = |
| 69 | "xyz.openbmc_project.Configuration."; |
| 70 | static constexpr std::array<const char*, 3> SENSOR_TYPES = { |
| 71 | "SkylakeCPU", "BroadwellCPU", "HaswellCPU"}; |
| 72 | |
| 73 | const static std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]"); |
| 74 | |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 75 | bool createSensors( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 77 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>& |
| 78 | sensors, |
| 79 | boost::container::flat_set<CPUConfig>& configs, |
| 80 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 81 | { |
| 82 | bool available = false; |
| 83 | for (CPUConfig cpu : configs) |
| 84 | { |
| 85 | if (cpu.state != State::OFF) |
| 86 | { |
| 87 | available = true; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (!available) |
| 92 | { |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 93 | return false; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // use new data the first time, then refresh |
| 97 | ManagedObjectType sensorConfigurations; |
| 98 | bool useCache = false; |
| 99 | for (const char* type : SENSOR_TYPES) |
| 100 | { |
| 101 | if (!getSensorConfiguration(CONFIG_PREFIX + std::string(type), |
| 102 | dbusConnection, sensorConfigurations, |
| 103 | useCache)) |
| 104 | { |
| 105 | std::cerr << "error communicating to entity manager\n"; |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 106 | return false; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | } |
| 108 | useCache = true; |
| 109 | } |
| 110 | |
| 111 | std::vector<fs::path> oemNamePaths; |
| 112 | if (!find_files(fs::path(R"(/sys/bus/peci/devices)"), |
| 113 | R"(peci\d+/\d+-.+/of_node/oemname1$)", oemNamePaths, 2)) |
| 114 | { |
| 115 | std::cerr << "No CPU sensors in system\n"; |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 116 | return true; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | for (fs::path& oemNamePath : oemNamePaths) |
| 120 | { |
| 121 | std::ifstream nameFile(oemNamePath); |
| 122 | if (!nameFile.good()) |
| 123 | { |
| 124 | std::cerr << "Failure reading " << oemNamePath << "\n"; |
| 125 | continue; |
| 126 | } |
| 127 | std::string oemName; |
| 128 | std::getline(nameFile, oemName); |
| 129 | nameFile.close(); |
| 130 | if (!oemName.size()) |
| 131 | { |
| 132 | // shouldn't have an empty name file |
| 133 | continue; |
| 134 | } |
| 135 | oemName.pop_back(); // remove trailing null |
| 136 | if (DEBUG) |
| 137 | std::cout << "Checking: " << oemNamePath << ": " << oemName << "\n"; |
| 138 | |
| 139 | const SensorData* sensorData = nullptr; |
| 140 | const std::string* interfacePath = nullptr; |
| 141 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 142 | sensor : sensorConfigurations) |
| 143 | { |
| 144 | if (!boost::ends_with(sensor.first.str, oemName)) |
| 145 | { |
| 146 | continue; |
| 147 | } |
| 148 | sensorData = &(sensor.second); |
| 149 | interfacePath = &(sensor.first.str); |
| 150 | break; |
| 151 | } |
| 152 | if (sensorData == nullptr) |
| 153 | { |
| 154 | std::cerr << "failed to find match for " << oemName << "\n"; |
| 155 | continue; |
| 156 | } |
| 157 | const std::pair<std::string, boost::container::flat_map< |
| 158 | std::string, BasicVariantType>>* |
| 159 | baseConfiguration = nullptr; |
| 160 | std::string sensorObjectType; |
| 161 | for (const char* type : SENSOR_TYPES) |
| 162 | { |
| 163 | sensorObjectType = CONFIG_PREFIX + std::string(type); |
| 164 | auto sensorBase = sensorData->find(sensorObjectType); |
| 165 | if (sensorBase != sensorData->end()) |
| 166 | { |
| 167 | baseConfiguration = &(*sensorBase); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (baseConfiguration == nullptr) |
| 173 | { |
| 174 | std::cerr << "error finding base configuration for" << oemName |
| 175 | << "\n"; |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | auto findCpuId = baseConfiguration->second.find("CpuID"); |
| 180 | if (findCpuId == baseConfiguration->second.end()) |
| 181 | { |
| 182 | std::cerr << "could not determine CPU ID for " << oemName << "\n"; |
| 183 | continue; |
| 184 | } |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame^] | 185 | int cpuId = variant_ns::visit(VariantToIntVisitor(), findCpuId->second); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 186 | |
| 187 | auto directory = oemNamePath.parent_path().parent_path(); |
| 188 | std::vector<fs::path> inputPaths; |
| 189 | if (!find_files(fs::path(directory), |
| 190 | R"(peci-.+/hwmon/hwmon\d+/temp\d+_input$)", inputPaths, |
| 191 | 0)) |
| 192 | { |
| 193 | std::cerr << "No temperature sensors in system\n"; |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | // iterate through all found temp sensors |
| 198 | for (auto& inputPath : inputPaths) |
| 199 | { |
| 200 | auto inputPathStr = inputPath.string(); |
| 201 | auto labelPath = |
| 202 | boost::replace_all_copy(inputPathStr, "input", "label"); |
| 203 | std::ifstream labelFile(labelPath); |
| 204 | if (!labelFile.good()) |
| 205 | { |
| 206 | std::cerr << "Failure reading " << labelPath << "\n"; |
| 207 | continue; |
| 208 | } |
| 209 | std::string label; |
| 210 | std::getline(labelFile, label); |
| 211 | labelFile.close(); |
| 212 | std::string sensorName = label + " CPU" + std::to_string(cpuId); |
| 213 | std::vector<thresholds::Threshold> sensorThresholds; |
| 214 | std::string labelHead = label.substr(0, label.find(" ")); |
Yoo, Jae Hyun | ac18e14 | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 215 | ParseThresholdsFromConfig(*sensorData, sensorThresholds, |
| 216 | &labelHead); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 217 | if (!sensorThresholds.size()) |
| 218 | { |
| 219 | if (!ParseThresholdsFromAttr(sensorThresholds, inputPathStr, |
| 220 | CPUSensor::SENSOR_SCALE_FACTOR)) |
| 221 | { |
Yoo, Jae Hyun | ac18e14 | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 222 | std::cerr << "error populating thresholds for " |
| 223 | << sensorName << "\n"; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | sensors[sensorName] = std::make_unique<CPUSensor>( |
| 227 | inputPathStr, sensorObjectType, objectServer, dbusConnection, |
| 228 | io, sensorName, std::move(sensorThresholds), *interfacePath); |
| 229 | if (DEBUG) |
| 230 | std::cout << "Mapped: " << inputPath << " to " << sensorName |
| 231 | << "\n"; |
| 232 | } |
| 233 | } |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 234 | |
| 235 | return true; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void reloadOverlay(const std::string& overlay) |
| 239 | { |
| 240 | boost::process::child c1(DT_OVERLAY, "-d", OVERLAY_DIR, "-r", overlay); |
| 241 | c1.wait(); |
| 242 | if (c1.exit_code()) |
| 243 | { |
| 244 | if (DEBUG) |
| 245 | { |
| 246 | std::cout << "DTOverlay unload error with file " << overlay |
| 247 | << ". error: " << c1.exit_code() << "\n"; |
| 248 | } |
| 249 | |
| 250 | /* fall through anyway */ |
| 251 | } |
| 252 | |
| 253 | boost::process::child c2(DT_OVERLAY, "-d", OVERLAY_DIR, overlay); |
| 254 | c2.wait(); |
| 255 | if (c2.exit_code()) |
| 256 | { |
| 257 | std::cerr << "DTOverlay load error with file " << overlay |
| 258 | << ". error: " << c2.exit_code() << "\n"; |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void detectCpu(boost::asio::deadline_timer& timer, boost::asio::io_service& io, |
| 264 | sdbusplus::asio::object_server& objectServer, |
| 265 | boost::container::flat_map<std::string, |
| 266 | std::unique_ptr<CPUSensor>>& sensors, |
| 267 | boost::container::flat_set<CPUConfig>& configs, |
| 268 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 269 | { |
| 270 | auto file = open(PECI_DEV, O_RDWR); |
| 271 | if (file < 0) |
| 272 | { |
| 273 | std::cerr << "unable to open " << PECI_DEV << "\n"; |
| 274 | std::exit(EXIT_FAILURE); |
| 275 | } |
| 276 | |
| 277 | size_t rescanDelaySeconds = 0; |
| 278 | bool keepPinging = false; |
| 279 | for (CPUConfig& config : configs) |
| 280 | { |
| 281 | State state; |
| 282 | struct peci_ping_msg msg; |
| 283 | msg.addr = config.addr; |
| 284 | if (!ioctl(file, PECI_IOC_PING, &msg)) |
| 285 | { |
| 286 | bool dimmReady = false; |
| 287 | for (unsigned int rank = 0; rank < RANK_NUM_MAX; rank++) |
| 288 | { |
| 289 | struct peci_rd_pkg_cfg_msg msg; |
| 290 | msg.addr = config.addr; |
| 291 | msg.index = MBX_INDEX_DDR_DIMM_TEMP; |
| 292 | msg.param = rank; |
| 293 | msg.rx_len = 4; |
| 294 | if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg)) |
| 295 | { |
| 296 | if (msg.pkg_config[0] || msg.pkg_config[1] || |
| 297 | msg.pkg_config[2]) |
| 298 | { |
| 299 | dimmReady = true; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | if (dimmReady) |
| 309 | { |
| 310 | state = State::READY; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | state = State::ON; |
| 315 | } |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | state = State::OFF; |
| 320 | } |
| 321 | |
| 322 | if (config.state != state) |
| 323 | { |
| 324 | if (config.state == State::OFF) |
| 325 | { |
| 326 | reloadOverlay(config.ovName); |
| 327 | } |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 328 | config.state = state; |
| 329 | } |
| 330 | |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 331 | if (config.state != State::OFF) |
| 332 | { |
| 333 | if (config.state == State::ON) |
| 334 | { |
| 335 | rescanDelaySeconds = 1; |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | rescanDelaySeconds = 5; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | if (config.state != State::READY) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 344 | { |
| 345 | keepPinging = true; |
| 346 | } |
| 347 | |
| 348 | if (DEBUG) |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 349 | std::cout << config.ovName << ", state: " << config.state << "\n"; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | close(file); |
| 353 | |
| 354 | if (rescanDelaySeconds) |
| 355 | { |
| 356 | std::this_thread::sleep_for(std::chrono::seconds(rescanDelaySeconds)); |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 357 | if (!createSensors(io, objectServer, sensors, configs, dbusConnection)) |
| 358 | { |
| 359 | keepPinging = true; |
| 360 | } |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (keepPinging) |
| 364 | { |
| 365 | timer.expires_from_now(boost::posix_time::seconds(1)); |
| 366 | timer.async_wait([&](const boost::system::error_code& ec) { |
| 367 | if (ec == boost::asio::error::operation_aborted) |
| 368 | { |
| 369 | /* we were canceled*/ |
| 370 | return; |
| 371 | } |
| 372 | else if (ec) |
| 373 | { |
| 374 | std::cerr << "timer error\n"; |
| 375 | return; |
| 376 | } |
| 377 | detectCpu(timer, io, objectServer, sensors, configs, |
| 378 | dbusConnection); |
| 379 | }); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
| 384 | boost::container::flat_set<CPUConfig>& configs) |
| 385 | { |
| 386 | ManagedObjectType sensorConfigurations; |
| 387 | bool useCache = false; |
| 388 | // use new data the first time, then refresh |
| 389 | for (const char* type : SENSOR_TYPES) |
| 390 | { |
| 391 | if (!getSensorConfiguration(CONFIG_PREFIX + std::string(type), |
| 392 | systemBus, sensorConfigurations, useCache)) |
| 393 | { |
| 394 | std::cerr |
| 395 | << "getCpuConfig: error communicating to entity manager\n"; |
| 396 | return; |
| 397 | } |
| 398 | useCache = true; |
| 399 | } |
| 400 | |
| 401 | // check PECI client addresses and DT overlay names from CPU configuration |
| 402 | // before starting ping operation |
| 403 | for (const char* type : SENSOR_TYPES) |
| 404 | { |
| 405 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 406 | sensor : sensorConfigurations) |
| 407 | { |
| 408 | for (const std::pair< |
| 409 | std::string, |
| 410 | boost::container::flat_map<std::string, BasicVariantType>>& |
| 411 | config : sensor.second) |
| 412 | { |
| 413 | if ((CONFIG_PREFIX + std::string(type)) != config.first) |
| 414 | { |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | auto findAddress = config.second.find("Address"); |
| 419 | if (findAddress == config.second.end()) |
| 420 | { |
| 421 | continue; |
| 422 | } |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame^] | 423 | int addr = variant_ns::visit(VariantToIntVisitor(), |
| 424 | findAddress->second); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 425 | |
| 426 | auto findName = config.second.find("Name"); |
| 427 | if (findName == config.second.end()) |
| 428 | { |
| 429 | continue; |
| 430 | } |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame^] | 431 | std::string nameRaw = variant_ns::visit( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 432 | VariantToStringVisitor(), findName->second); |
| 433 | std::string name = |
| 434 | std::regex_replace(nameRaw, ILLEGAL_NAME_REGEX, "_"); |
| 435 | std::string overlayName = name + "_" + type; |
| 436 | |
| 437 | if (DEBUG) |
| 438 | { |
| 439 | std::cout << "addr: " << addr << "\n"; |
| 440 | std::cout << "name: " << name << "\n"; |
| 441 | std::cout << "type: " << type << "\n"; |
| 442 | std::cout << "overlayName: " << overlayName << "\n"; |
| 443 | } |
| 444 | |
| 445 | configs.emplace(addr, overlayName, State::OFF); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | int main(int argc, char** argv) |
| 452 | { |
| 453 | boost::asio::io_service io; |
| 454 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 455 | boost::container::flat_set<CPUConfig> configs; |
| 456 | |
| 457 | systemBus->request_name("xyz.openbmc_project.CPUSensor"); |
| 458 | sdbusplus::asio::object_server objectServer(systemBus); |
| 459 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors; |
| 460 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 461 | boost::asio::deadline_timer pingTimer(io); |
| 462 | getCpuConfig(systemBus, configs); |
| 463 | if (configs.size()) |
| 464 | { |
| 465 | detectCpu(pingTimer, io, objectServer, sensors, configs, systemBus); |
| 466 | } |
| 467 | |
| 468 | boost::asio::deadline_timer filterTimer(io); |
| 469 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 470 | [&](sdbusplus::message::message& message) { |
| 471 | if (message.is_method_error()) |
| 472 | { |
| 473 | std::cerr << "callback method error\n"; |
| 474 | return; |
| 475 | } |
| 476 | // this implicitly cancels the timer |
| 477 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 478 | |
| 479 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 480 | if (ec == boost::asio::error::operation_aborted) |
| 481 | { |
| 482 | /* we were canceled*/ |
| 483 | return; |
| 484 | } |
| 485 | else if (ec) |
| 486 | { |
| 487 | std::cerr << "timer error\n"; |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | getCpuConfig(systemBus, configs); |
| 492 | |
| 493 | if (configs.size()) |
| 494 | { |
| 495 | detectCpu(pingTimer, io, objectServer, sensors, configs, |
| 496 | systemBus); |
| 497 | } |
| 498 | }); |
| 499 | }; |
| 500 | |
| 501 | for (const char* type : SENSOR_TYPES) |
| 502 | { |
| 503 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 504 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 505 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 506 | std::string(INVENTORY_PATH) + "',arg0namespace='" + |
| 507 | CONFIG_PREFIX + type + "'", |
| 508 | eventHandler); |
| 509 | matches.emplace_back(std::move(match)); |
| 510 | } |
| 511 | |
| 512 | io.run(); |
| 513 | } |