Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include "occ_manager.hpp" |
| 4 | |
| 5 | #include "i2c_occ.hpp" |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 6 | #include "occ_dbus.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 7 | #include "utils.hpp" |
| 8 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 13 | #include <chrono> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 14 | #include <cmath> |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 15 | #include <filesystem> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 16 | #include <regex> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 17 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 18 | namespace open_power |
| 19 | { |
| 20 | namespace occ |
| 21 | { |
| 22 | |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 23 | constexpr uint32_t fruTypeNotAvailable = 0xFF; |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 24 | constexpr auto fruTypeSuffix = "fru_type"; |
| 25 | constexpr auto faultSuffix = "fault"; |
| 26 | constexpr auto inputSuffix = "input"; |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 27 | constexpr auto maxSuffix = "max"; |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 28 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 29 | using namespace phosphor::logging; |
| 30 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 31 | template <typename T> |
| 32 | T readFile(const std::string& path) |
| 33 | { |
| 34 | std::ifstream ifs; |
| 35 | ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 36 | std::ifstream::eofbit); |
| 37 | T data; |
| 38 | |
| 39 | try |
| 40 | { |
| 41 | ifs.open(path); |
| 42 | ifs >> data; |
| 43 | ifs.close(); |
| 44 | } |
| 45 | catch (const std::exception& e) |
| 46 | { |
| 47 | auto err = errno; |
| 48 | throw std::system_error(err, std::generic_category()); |
| 49 | } |
| 50 | |
| 51 | return data; |
| 52 | } |
| 53 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 54 | void Manager::findAndCreateObjects() |
| 55 | { |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 56 | #ifndef POWER10 |
Deepak Kodihalli | 370f06b | 2017-10-25 04:26:07 -0500 | [diff] [blame] | 57 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 58 | { |
Deepak Kodihalli | 30417a1 | 2017-12-04 00:54:01 -0600 | [diff] [blame] | 59 | // Create one occ per cpu |
| 60 | auto occ = std::string(OCC_NAME) + std::to_string(id); |
| 61 | createObjects(occ); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 62 | } |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 63 | #else |
| 64 | // Create the OCCs based on on the /dev/occX devices |
| 65 | auto occs = findOCCsInDev(); |
| 66 | |
| 67 | if (occs.empty() || (prevOCCSearch.size() != occs.size())) |
| 68 | { |
| 69 | // Something changed or no OCCs yet, try again in 10s. |
| 70 | // Note on the first pass prevOCCSearch will be empty, |
| 71 | // so there will be at least one delay to give things |
| 72 | // a chance to settle. |
| 73 | prevOCCSearch = occs; |
| 74 | |
| 75 | using namespace std::literals::chrono_literals; |
| 76 | discoverTimer->restartOnce(10s); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | discoverTimer.reset(); |
| 81 | |
| 82 | // createObjects requires OCC0 first. |
| 83 | std::sort(occs.begin(), occs.end()); |
| 84 | |
| 85 | for (auto id : occs) |
| 86 | { |
| 87 | createObjects(std::string(OCC_NAME) + std::to_string(id)); |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | std::vector<int> Manager::findOCCsInDev() |
| 94 | { |
| 95 | std::vector<int> occs; |
| 96 | std::regex expr{R"(occ(\d+)$)"}; |
| 97 | |
| 98 | for (auto& file : fs::directory_iterator("/dev")) |
| 99 | { |
| 100 | std::smatch match; |
| 101 | std::string path{file.path().string()}; |
| 102 | if (std::regex_search(path, match, expr)) |
| 103 | { |
| 104 | auto num = std::stoi(match[1].str()); |
| 105 | |
| 106 | // /dev numbering starts at 1, ours starts at 0. |
| 107 | occs.push_back(num - 1); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return occs; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int Manager::cpuCreated(sdbusplus::message::message& msg) |
| 115 | { |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 116 | namespace fs = std::filesystem; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 117 | |
| 118 | sdbusplus::message::object_path o; |
| 119 | msg.read(o); |
| 120 | fs::path cpuPath(std::string(std::move(o))); |
| 121 | |
| 122 | auto name = cpuPath.filename().string(); |
| 123 | auto index = name.find(CPU_NAME); |
| 124 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 125 | |
| 126 | createObjects(name); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | void Manager::createObjects(const std::string& occ) |
| 132 | { |
| 133 | auto path = fs::path(OCC_CONTROL_ROOT) / occ; |
| 134 | |
| 135 | passThroughObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 136 | std::make_unique<PassThrough>(path.c_str())); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 137 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 138 | statusObjects.emplace_back(std::make_unique<Status>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 139 | event, path.c_str(), *this, |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 140 | std::bind(std::mem_fn(&Manager::statusCallBack), this, |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 141 | std::placeholders::_1) |
| 142 | #ifdef PLDM |
| 143 | , |
| 144 | std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(), |
| 145 | std::placeholders::_1) |
| 146 | #endif |
| 147 | )); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 148 | |
| 149 | // Create the power cap monitor object for master occ (0) |
| 150 | if (!pcap) |
| 151 | { |
| 152 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 153 | *statusObjects.front()); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 154 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 155 | |
| 156 | #ifdef POWER10 |
| 157 | // Create the power mode monitor object for master occ (0) |
| 158 | if (!pmode) |
| 159 | { |
| 160 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 161 | *statusObjects.front()); |
| 162 | } |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 163 | // Create the idle power saver monitor object for master occ (0) |
| 164 | if (!pips) |
| 165 | { |
| 166 | pips = std::make_unique<open_power::occ::powermode::PowerIPS>( |
| 167 | *statusObjects.front()); |
| 168 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 169 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void Manager::statusCallBack(bool status) |
| 173 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 174 | using InternalFailure = |
| 175 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 176 | |
| 177 | // At this time, it won't happen but keeping it |
| 178 | // here just in case something changes in the future |
| 179 | if ((activeCount == 0) && (!status)) |
| 180 | { |
| 181 | log<level::ERR>("Invalid update on OCCActive"); |
| 182 | elog<InternalFailure>(); |
| 183 | } |
| 184 | |
| 185 | activeCount += status ? 1 : -1; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 186 | |
| 187 | // Only start presence detection if all the OCCs are bound |
| 188 | if (activeCount == statusObjects.size()) |
| 189 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 190 | for (auto& obj : statusObjects) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 191 | { |
| 192 | obj->addPresenceWatchMaster(); |
| 193 | } |
| 194 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 195 | |
| 196 | if ((!_pollTimer->isEnabled()) && (activeCount > 0)) |
| 197 | { |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 198 | log<level::INFO>( |
| 199 | fmt::format( |
| 200 | "Manager::statusCallBack(): {} OCCs will be polled every {} seconds", |
| 201 | activeCount, pollInterval) |
| 202 | .c_str()); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 203 | |
| 204 | // Send poll and start OCC poll timer |
| 205 | pollerTimerExpired(); |
| 206 | } |
| 207 | else if ((_pollTimer->isEnabled()) && (activeCount == 0)) |
| 208 | { |
| 209 | // Stop OCC poll timer |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 210 | log<level::INFO>( |
| 211 | "Manager::statusCallBack(): OCCs are not running, stopping poll timer"); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 212 | _pollTimer->setEnabled(false); |
Matt Spinler | 53f6814 | 2021-08-25 15:47:31 -0500 | [diff] [blame] | 213 | |
| 214 | #ifdef READ_OCC_SENSORS |
| 215 | for (auto& obj : statusObjects) |
| 216 | { |
| 217 | setSensorValueToNaN(obj->getOccInstanceID()); |
| 218 | } |
| 219 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 220 | } |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | #ifdef I2C_OCC |
| 224 | void Manager::initStatusObjects() |
| 225 | { |
| 226 | // Make sure we have a valid path string |
| 227 | static_assert(sizeof(DEV_PATH) != 0); |
| 228 | |
| 229 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 230 | auto occMasterName = deviceNames.front(); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 231 | for (auto& name : deviceNames) |
| 232 | { |
| 233 | i2c_occ::i2cToDbus(name); |
Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 234 | name = std::string(OCC_NAME) + '_' + name; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 235 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 236 | statusObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 237 | std::make_unique<Status>(event, path.c_str(), *this)); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 238 | } |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 239 | // The first device is master occ |
| 240 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 241 | *statusObjects.front(), occMasterName); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 242 | #ifdef POWER10 |
| 243 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 244 | *statusObjects.front()); |
Chris Cain | 1d51da2 | 2021-09-21 14:13:41 -0500 | [diff] [blame] | 245 | pips = std::make_unique<open_power::occ::powermode::PowerIPS>( |
| 246 | *statusObjects.front()); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 247 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 248 | } |
| 249 | #endif |
| 250 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 251 | #ifdef PLDM |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 252 | void Manager::sbeTimeout(unsigned int instance) |
| 253 | { |
| 254 | log<level::INFO>("SBE timeout, requesting HRESET", |
| 255 | entry("SBE=%d", instance)); |
| 256 | |
| 257 | setSBEState(instance, SBE_STATE_NOT_USABLE); |
| 258 | |
| 259 | pldmHandle->sendHRESET(instance); |
| 260 | } |
| 261 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 262 | bool Manager::updateOCCActive(instanceID instance, bool status) |
| 263 | { |
| 264 | return (statusObjects[instance])->occActive(status); |
| 265 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 266 | |
| 267 | void Manager::sbeHRESETResult(instanceID instance, bool success) |
| 268 | { |
| 269 | if (success) |
| 270 | { |
| 271 | log<level::INFO>("HRESET succeeded", entry("SBE=%d", instance)); |
| 272 | |
| 273 | setSBEState(instance, SBE_STATE_BOOTED); |
| 274 | |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | setSBEState(instance, SBE_STATE_FAILED); |
| 279 | |
| 280 | if (sbeCanDump(instance)) |
| 281 | { |
| 282 | constexpr auto path = "/org/openpower/dump"; |
| 283 | constexpr auto interface = "xyz.openbmc_project.Dump.Create"; |
| 284 | constexpr auto function = "CreateDump"; |
| 285 | |
| 286 | log<level::INFO>("HRESET failed, triggering SBE dump", |
| 287 | entry("SBE=%d", instance)); |
| 288 | |
| 289 | auto& bus = utils::getBus(); |
| 290 | uint32_t src6 = instance << 16; |
| 291 | uint32_t logId = |
| 292 | FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout", |
| 293 | src6, "SBE command timeout"); |
| 294 | |
| 295 | try |
| 296 | { |
| 297 | std::string service = utils::getService(path, interface); |
| 298 | auto method = |
| 299 | bus.new_method_call(service.c_str(), path, interface, function); |
| 300 | |
| 301 | std::map<std::string, std::variant<std::string, uint64_t>> |
| 302 | createParams{ |
| 303 | {"com.ibm.Dump.Create.CreateParameters.ErrorLogId", |
| 304 | uint64_t(logId)}, |
| 305 | {"com.ibm.Dump.Create.CreateParameters.DumpType", |
| 306 | "com.ibm.Dump.Create.DumpType.SBE"}, |
| 307 | {"com.ibm.Dump.Create.CreateParameters.FailingUnitId", |
| 308 | uint64_t(instance)}, |
| 309 | }; |
| 310 | |
| 311 | method.append(createParams); |
| 312 | |
| 313 | auto response = bus.call(method); |
| 314 | } |
| 315 | catch (const sdbusplus::exception::exception& e) |
| 316 | { |
| 317 | constexpr auto ERROR_DUMP_DISABLED = |
| 318 | "xyz.openbmc_project.Dump.Create.Error.Disabled"; |
| 319 | if (e.name() == ERROR_DUMP_DISABLED) |
| 320 | { |
| 321 | log<level::INFO>("Dump is disabled, skipping"); |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | log<level::ERR>("Dump failed"); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | bool Manager::sbeCanDump(unsigned int instance) |
| 332 | { |
| 333 | struct pdbg_target* proc = getPdbgTarget(instance); |
| 334 | |
| 335 | if (!proc) |
| 336 | { |
| 337 | // allow the dump in the error case |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | try |
| 342 | { |
| 343 | if (!openpower::phal::sbe::isDumpAllowed(proc)) |
| 344 | { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | if (openpower::phal::pdbg::isSbeVitalAttnActive(proc)) |
| 349 | { |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | catch (openpower::phal::exception::SbeError& e) |
| 354 | { |
| 355 | log<level::INFO>("Failed to query SBE state"); |
| 356 | } |
| 357 | |
| 358 | // allow the dump in the error case |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | void Manager::setSBEState(unsigned int instance, enum sbe_state state) |
| 363 | { |
| 364 | struct pdbg_target* proc = getPdbgTarget(instance); |
| 365 | |
| 366 | if (!proc) |
| 367 | { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | try |
| 372 | { |
| 373 | openpower::phal::sbe::setState(proc, state); |
| 374 | } |
| 375 | catch (const openpower::phal::exception::SbeError& e) |
| 376 | { |
| 377 | log<level::ERR>("Failed to set SBE state"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | struct pdbg_target* Manager::getPdbgTarget(unsigned int instance) |
| 382 | { |
| 383 | if (!pdbgInitialized) |
| 384 | { |
| 385 | try |
| 386 | { |
| 387 | openpower::phal::pdbg::init(); |
| 388 | pdbgInitialized = true; |
| 389 | } |
| 390 | catch (const openpower::phal::exception::PdbgError& e) |
| 391 | { |
| 392 | log<level::ERR>("pdbg initialization failed"); |
| 393 | return nullptr; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | struct pdbg_target* proc = nullptr; |
| 398 | pdbg_for_each_class_target("proc", proc) |
| 399 | { |
| 400 | if (pdbg_target_index(proc) == instance) |
| 401 | { |
| 402 | return proc; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | log<level::ERR>("Failed to get pdbg target"); |
| 407 | return nullptr; |
| 408 | } |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 409 | #endif |
| 410 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 411 | void Manager::pollerTimerExpired() |
| 412 | { |
| 413 | if (activeCount == 0) |
| 414 | { |
| 415 | // No OCCs running, so poll timer will not be restarted |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 416 | log<level::INFO>( |
| 417 | "Manager::pollerTimerExpire(): No OCCs running, poll timer not restarted"); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (!_pollTimer) |
| 421 | { |
| 422 | log<level::ERR>( |
| 423 | "Manager::pollerTimerExpired() ERROR: Timer not defined"); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | for (auto& obj : statusObjects) |
| 428 | { |
| 429 | // Read sysfs to force kernel to poll OCC |
| 430 | obj->readOccState(); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 431 | |
| 432 | #ifdef READ_OCC_SENSORS |
| 433 | // Read occ sensor values |
| 434 | auto id = obj->getOccInstanceID(); |
| 435 | if (!obj->occActive()) |
| 436 | { |
| 437 | // Occ not activated |
| 438 | setSensorValueToNaN(id); |
| 439 | continue; |
| 440 | } |
| 441 | getSensorValues(id, obj->isMasterOcc()); |
| 442 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // Restart OCC poll timer |
| 446 | _pollTimer->restartOnce(std::chrono::seconds(pollInterval)); |
| 447 | } |
| 448 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 449 | #ifdef READ_OCC_SENSORS |
| 450 | void Manager::readTempSensors(const fs::path& path, uint32_t id) |
| 451 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 452 | std::regex expr{"temp\\d+_label$"}; // Example: temp5_label |
| 453 | for (auto& file : fs::directory_iterator(path)) |
| 454 | { |
| 455 | if (!std::regex_search(file.path().string(), expr)) |
| 456 | { |
| 457 | continue; |
| 458 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 459 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 460 | uint32_t labelValue{0}; |
| 461 | |
| 462 | try |
| 463 | { |
| 464 | labelValue = readFile<uint32_t>(file.path()); |
| 465 | } |
| 466 | catch (const std::system_error& e) |
| 467 | { |
| 468 | log<level::DEBUG>( |
| 469 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 470 | file.path().string(), e.code().value()) |
| 471 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 472 | continue; |
| 473 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 474 | |
| 475 | const std::string& tempLabel = "label"; |
| 476 | const std::string filePathString = file.path().string().substr( |
| 477 | 0, file.path().string().length() - tempLabel.length()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 478 | |
| 479 | uint32_t fruTypeValue{0}; |
| 480 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 481 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 482 | fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix); |
| 483 | } |
| 484 | catch (const std::system_error& e) |
| 485 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 486 | log<level::DEBUG>( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 487 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 488 | filePathString + fruTypeSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 489 | .c_str()); |
| 490 | continue; |
| 491 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 492 | |
| 493 | std::string sensorPath = |
| 494 | OCC_SENSORS_ROOT + std::string("/temperature/"); |
| 495 | |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 496 | std::string dvfsTempPath; |
| 497 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 498 | if (fruTypeValue == VRMVdd) |
| 499 | { |
| 500 | sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp"); |
| 501 | } |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 502 | else if (fruTypeValue == processorIoRing) |
| 503 | { |
| 504 | sensorPath.append("proc" + std::to_string(id) + "_ioring_temp"); |
| 505 | dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" + |
| 506 | std::to_string(id) + "_ioring_dvfs_temp"; |
| 507 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 508 | else |
| 509 | { |
Matt Spinler | 14d1402 | 2021-08-25 15:38:29 -0500 | [diff] [blame] | 510 | uint16_t type = (labelValue & 0xFF000000) >> 24; |
| 511 | uint16_t instanceID = labelValue & 0x0000FFFF; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 512 | |
| 513 | if (type == OCC_DIMM_TEMP_SENSOR_TYPE) |
| 514 | { |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 515 | if (fruTypeValue == fruTypeNotAvailable) |
| 516 | { |
| 517 | // Not all DIMM related temps are available to read |
| 518 | // (no _input file in this case) |
| 519 | continue; |
| 520 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 521 | auto iter = dimmTempSensorName.find(fruTypeValue); |
| 522 | if (iter == dimmTempSensorName.end()) |
| 523 | { |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 524 | log<level::ERR>( |
| 525 | fmt::format( |
| 526 | "readTempSensors: Fru type error! fruTypeValue = {}) ", |
| 527 | fruTypeValue) |
| 528 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 529 | continue; |
| 530 | } |
| 531 | |
| 532 | sensorPath.append("dimm" + std::to_string(instanceID) + |
| 533 | iter->second); |
| 534 | } |
| 535 | else if (type == OCC_CPU_TEMP_SENSOR_TYPE) |
| 536 | { |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 537 | if (fruTypeValue == processorCore) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 538 | { |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 539 | // The OCC reports small core temps, of which there are |
| 540 | // two per big core. All current P10 systems are in big |
| 541 | // core mode, so use a big core name. |
| 542 | uint16_t coreNum = instanceID / 2; |
| 543 | uint16_t tempNum = instanceID % 2; |
| 544 | sensorPath.append("proc" + std::to_string(id) + "_core" + |
| 545 | std::to_string(coreNum) + "_" + |
| 546 | std::to_string(tempNum) + "_temp"); |
| 547 | |
| 548 | dvfsTempPath = std::string{OCC_SENSORS_ROOT} + |
| 549 | "/temperature/proc" + std::to_string(id) + |
| 550 | "_core_dvfs_temp"; |
| 551 | } |
| 552 | else |
| 553 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 554 | continue; |
| 555 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 556 | } |
| 557 | else |
| 558 | { |
| 559 | continue; |
| 560 | } |
| 561 | } |
| 562 | |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame^] | 563 | // The dvfs temp file only needs to be read once per chip per type. |
| 564 | if (!dvfsTempPath.empty() && |
| 565 | !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath)) |
| 566 | { |
| 567 | try |
| 568 | { |
| 569 | auto dvfsValue = readFile<double>(filePathString + maxSuffix); |
| 570 | |
| 571 | dbus::OccDBusSensors::getOccDBus().setDvfsTemp( |
| 572 | dvfsTempPath, dvfsValue * std::pow(10, -3)); |
| 573 | } |
| 574 | catch (const std::system_error& e) |
| 575 | { |
| 576 | log<level::DEBUG>( |
| 577 | fmt::format( |
| 578 | "readTempSensors: Failed reading {}, errno = {}", |
| 579 | filePathString + maxSuffix, e.code().value()) |
| 580 | .c_str()); |
| 581 | } |
| 582 | } |
| 583 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 584 | uint32_t faultValue{0}; |
| 585 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 586 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 587 | faultValue = readFile<uint32_t>(filePathString + faultSuffix); |
| 588 | } |
| 589 | catch (const std::system_error& e) |
| 590 | { |
| 591 | log<level::DEBUG>( |
| 592 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 593 | filePathString + faultSuffix, e.code().value()) |
| 594 | .c_str()); |
| 595 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 596 | } |
| 597 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 598 | // At this point, the sensor will be created for sure. |
| 599 | if (existingSensors.find(sensorPath) == existingSensors.end()) |
| 600 | { |
| 601 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 602 | .setChassisAssociation(sensorPath); |
| 603 | } |
| 604 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 605 | if (faultValue != 0) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 606 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 607 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 608 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 609 | |
| 610 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 611 | .setOperationalStatus(sensorPath, false); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 612 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 613 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 614 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 615 | |
| 616 | double tempValue{0}; |
| 617 | |
| 618 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 619 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 620 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 621 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 622 | catch (const std::system_error& e) |
| 623 | { |
| 624 | log<level::DEBUG>( |
| 625 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 626 | filePathString + inputSuffix, e.code().value()) |
| 627 | .c_str()); |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 632 | sensorPath, tempValue * std::pow(10, -3)); |
| 633 | |
| 634 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 635 | .setOperationalStatus(sensorPath, true); |
| 636 | |
| 637 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 638 | } |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | std::optional<std::string> |
| 643 | Manager::getPowerLabelFunctionID(const std::string& value) |
| 644 | { |
| 645 | // If the value is "system", then the FunctionID is "system". |
| 646 | if (value == "system") |
| 647 | { |
| 648 | return value; |
| 649 | } |
| 650 | |
| 651 | // If the value is not "system", then the label value have 3 numbers, of |
| 652 | // which we only care about the middle one: |
| 653 | // <sensor id>_<function id>_<apss channel> |
| 654 | // eg: The value is "0_10_5" , then the FunctionID is "10". |
| 655 | if (value.find("_") == std::string::npos) |
| 656 | { |
| 657 | return std::nullopt; |
| 658 | } |
| 659 | |
| 660 | auto powerLabelValue = value.substr((value.find("_") + 1)); |
| 661 | |
| 662 | if (powerLabelValue.find("_") == std::string::npos) |
| 663 | { |
| 664 | return std::nullopt; |
| 665 | } |
| 666 | |
| 667 | return powerLabelValue.substr(0, powerLabelValue.find("_")); |
| 668 | } |
| 669 | |
| 670 | void Manager::readPowerSensors(const fs::path& path, uint32_t id) |
| 671 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 672 | std::regex expr{"power\\d+_label$"}; // Example: power5_label |
| 673 | for (auto& file : fs::directory_iterator(path)) |
| 674 | { |
| 675 | if (!std::regex_search(file.path().string(), expr)) |
| 676 | { |
| 677 | continue; |
| 678 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 679 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 680 | std::string labelValue; |
| 681 | try |
| 682 | { |
| 683 | labelValue = readFile<std::string>(file.path()); |
| 684 | } |
| 685 | catch (const std::system_error& e) |
| 686 | { |
| 687 | log<level::DEBUG>( |
| 688 | fmt::format("readPowerSensors: Failed reading {}, errno = {}", |
| 689 | file.path().string(), e.code().value()) |
| 690 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 691 | continue; |
| 692 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 693 | |
| 694 | auto functionID = getPowerLabelFunctionID(labelValue); |
| 695 | if (functionID == std::nullopt) |
| 696 | { |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | const std::string& tempLabel = "label"; |
| 701 | const std::string filePathString = file.path().string().substr( |
| 702 | 0, file.path().string().length() - tempLabel.length()); |
| 703 | |
| 704 | std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/"); |
| 705 | |
| 706 | auto iter = powerSensorName.find(*functionID); |
| 707 | if (iter == powerSensorName.end()) |
| 708 | { |
| 709 | continue; |
| 710 | } |
| 711 | sensorPath.append(iter->second); |
| 712 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 713 | double tempValue{0}; |
| 714 | |
| 715 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 716 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 717 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 718 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 719 | catch (const std::system_error& e) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 720 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 721 | log<level::DEBUG>( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 722 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 723 | filePathString + inputSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 724 | .c_str()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 725 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 726 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 727 | |
| 728 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 729 | sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3)); |
| 730 | |
| 731 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 732 | .setOperationalStatus(sensorPath, true); |
| 733 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 734 | if (existingSensors.find(sensorPath) == existingSensors.end()) |
| 735 | { |
| 736 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 737 | .setChassisAssociation(sensorPath); |
| 738 | } |
| 739 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 740 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 741 | } |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | void Manager::setSensorValueToNaN(uint32_t id) |
| 746 | { |
| 747 | for (const auto& [sensorPath, occId] : existingSensors) |
| 748 | { |
| 749 | if (occId == id) |
| 750 | { |
| 751 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 752 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
| 753 | } |
| 754 | } |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | void Manager::getSensorValues(uint32_t id, bool masterOcc) |
| 759 | { |
| 760 | const auto occ = std::string("occ-hwmon.") + std::to_string(id + 1); |
| 761 | |
| 762 | fs::path fileName{OCC_HWMON_PATH + occ + "/hwmon/"}; |
| 763 | |
| 764 | // Need to get the hwmonXX directory name, there better only be 1 dir |
| 765 | assert(std::distance(fs::directory_iterator(fileName), |
| 766 | fs::directory_iterator{}) == 1); |
| 767 | // Now set our path to this full path, including this hwmonXX directory |
| 768 | fileName = fs::path(*fs::directory_iterator(fileName)); |
| 769 | |
| 770 | // Read temperature sensors |
| 771 | readTempSensors(fileName, id); |
| 772 | |
| 773 | if (masterOcc) |
| 774 | { |
| 775 | // Read power sensors |
| 776 | readPowerSensors(fileName, id); |
| 777 | } |
| 778 | |
| 779 | return; |
| 780 | } |
| 781 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 782 | } // namespace occ |
| 783 | } // namespace open_power |