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" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 6 | #include "utils.hpp" |
| 7 | |
| 8 | #include <experimental/filesystem> |
| 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 13 | namespace open_power |
| 14 | { |
| 15 | namespace occ |
| 16 | { |
| 17 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 18 | using namespace phosphor::logging; |
| 19 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 20 | void Manager::findAndCreateObjects() |
| 21 | { |
Deepak Kodihalli | 370f06b | 2017-10-25 04:26:07 -0500 | [diff] [blame] | 22 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 23 | { |
Deepak Kodihalli | 30417a1 | 2017-12-04 00:54:01 -0600 | [diff] [blame] | 24 | // Create one occ per cpu |
| 25 | auto occ = std::string(OCC_NAME) + std::to_string(id); |
| 26 | createObjects(occ); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
| 30 | int Manager::cpuCreated(sdbusplus::message::message& msg) |
| 31 | { |
| 32 | namespace fs = std::experimental::filesystem; |
| 33 | |
| 34 | sdbusplus::message::object_path o; |
| 35 | msg.read(o); |
| 36 | fs::path cpuPath(std::string(std::move(o))); |
| 37 | |
| 38 | auto name = cpuPath.filename().string(); |
| 39 | auto index = name.find(CPU_NAME); |
| 40 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 41 | |
| 42 | createObjects(name); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | void Manager::createObjects(const std::string& occ) |
| 48 | { |
| 49 | auto path = fs::path(OCC_CONTROL_ROOT) / occ; |
| 50 | |
| 51 | passThroughObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 52 | std::make_unique<PassThrough>(path.c_str())); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 53 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 54 | statusObjects.emplace_back(std::make_unique<Status>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 55 | event, path.c_str(), *this, |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 56 | std::bind(std::mem_fn(&Manager::statusCallBack), this, |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 57 | std::placeholders::_1) |
| 58 | #ifdef PLDM |
| 59 | , |
| 60 | std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(), |
| 61 | std::placeholders::_1) |
| 62 | #endif |
| 63 | )); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 64 | |
| 65 | // Create the power cap monitor object for master occ (0) |
| 66 | if (!pcap) |
| 67 | { |
| 68 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 69 | *statusObjects.front()); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | void Manager::statusCallBack(bool status) |
| 74 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 75 | using InternalFailure = |
| 76 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 77 | |
| 78 | // At this time, it won't happen but keeping it |
| 79 | // here just in case something changes in the future |
| 80 | if ((activeCount == 0) && (!status)) |
| 81 | { |
| 82 | log<level::ERR>("Invalid update on OCCActive"); |
| 83 | elog<InternalFailure>(); |
| 84 | } |
| 85 | |
| 86 | activeCount += status ? 1 : -1; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 87 | |
| 88 | // Only start presence detection if all the OCCs are bound |
| 89 | if (activeCount == statusObjects.size()) |
| 90 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 91 | for (auto& obj : statusObjects) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 92 | { |
| 93 | obj->addPresenceWatchMaster(); |
| 94 | } |
| 95 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 96 | |
| 97 | if ((!_pollTimer->isEnabled()) && (activeCount > 0)) |
| 98 | { |
| 99 | log<level::INFO>(fmt::format("Manager::statusCallBack(): {} OCCs will " |
| 100 | "be polled every {} seconds", |
| 101 | activeCount, pollInterval) |
| 102 | .c_str()); |
| 103 | |
| 104 | // Send poll and start OCC poll timer |
| 105 | pollerTimerExpired(); |
| 106 | } |
| 107 | else if ((_pollTimer->isEnabled()) && (activeCount == 0)) |
| 108 | { |
| 109 | // Stop OCC poll timer |
| 110 | log<level::INFO>("Manager::statusCallBack(): OCCs are not running, " |
| 111 | "stopping poll timer"); |
| 112 | _pollTimer->setEnabled(false); |
| 113 | } |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | #ifdef I2C_OCC |
| 117 | void Manager::initStatusObjects() |
| 118 | { |
| 119 | // Make sure we have a valid path string |
| 120 | static_assert(sizeof(DEV_PATH) != 0); |
| 121 | |
| 122 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 123 | auto occMasterName = deviceNames.front(); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 124 | for (auto& name : deviceNames) |
| 125 | { |
| 126 | i2c_occ::i2cToDbus(name); |
Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 127 | name = std::string(OCC_NAME) + '_' + name; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 128 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 129 | statusObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 130 | std::make_unique<Status>(event, path.c_str(), *this)); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 131 | } |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 132 | // The first device is master occ |
| 133 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 134 | *statusObjects.front(), occMasterName); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 135 | } |
| 136 | #endif |
| 137 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 138 | #ifdef PLDM |
| 139 | bool Manager::updateOCCActive(instanceID instance, bool status) |
| 140 | { |
| 141 | return (statusObjects[instance])->occActive(status); |
| 142 | } |
| 143 | #endif |
| 144 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 145 | void Manager::pollerTimerExpired() |
| 146 | { |
| 147 | if (activeCount == 0) |
| 148 | { |
| 149 | // No OCCs running, so poll timer will not be restarted |
| 150 | log<level::INFO>("Manager::pollerTimerExpire(): No OCCs running, poll " |
| 151 | "timer not restarted"); |
| 152 | } |
| 153 | |
| 154 | if (!_pollTimer) |
| 155 | { |
| 156 | log<level::ERR>( |
| 157 | "Manager::pollerTimerExpired() ERROR: Timer not defined"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | for (auto& obj : statusObjects) |
| 162 | { |
| 163 | // Read sysfs to force kernel to poll OCC |
| 164 | obj->readOccState(); |
| 165 | } |
| 166 | |
| 167 | // Restart OCC poll timer |
| 168 | _pollTimer->restartOnce(std::chrono::seconds(pollInterval)); |
| 169 | } |
| 170 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 171 | } // namespace occ |
| 172 | } // namespace open_power |