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 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 71 | |
| 72 | #ifdef POWER10 |
| 73 | // Create the power mode monitor object for master occ (0) |
| 74 | if (!pmode) |
| 75 | { |
| 76 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 77 | *statusObjects.front()); |
| 78 | } |
| 79 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Manager::statusCallBack(bool status) |
| 83 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 84 | using InternalFailure = |
| 85 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 86 | |
| 87 | // At this time, it won't happen but keeping it |
| 88 | // here just in case something changes in the future |
| 89 | if ((activeCount == 0) && (!status)) |
| 90 | { |
| 91 | log<level::ERR>("Invalid update on OCCActive"); |
| 92 | elog<InternalFailure>(); |
| 93 | } |
| 94 | |
| 95 | activeCount += status ? 1 : -1; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 96 | |
| 97 | // Only start presence detection if all the OCCs are bound |
| 98 | if (activeCount == statusObjects.size()) |
| 99 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 100 | for (auto& obj : statusObjects) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 101 | { |
| 102 | obj->addPresenceWatchMaster(); |
| 103 | } |
| 104 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 105 | |
| 106 | if ((!_pollTimer->isEnabled()) && (activeCount > 0)) |
| 107 | { |
| 108 | log<level::INFO>(fmt::format("Manager::statusCallBack(): {} OCCs will " |
| 109 | "be polled every {} seconds", |
| 110 | activeCount, pollInterval) |
| 111 | .c_str()); |
| 112 | |
| 113 | // Send poll and start OCC poll timer |
| 114 | pollerTimerExpired(); |
| 115 | } |
| 116 | else if ((_pollTimer->isEnabled()) && (activeCount == 0)) |
| 117 | { |
| 118 | // Stop OCC poll timer |
| 119 | log<level::INFO>("Manager::statusCallBack(): OCCs are not running, " |
| 120 | "stopping poll timer"); |
| 121 | _pollTimer->setEnabled(false); |
| 122 | } |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #ifdef I2C_OCC |
| 126 | void Manager::initStatusObjects() |
| 127 | { |
| 128 | // Make sure we have a valid path string |
| 129 | static_assert(sizeof(DEV_PATH) != 0); |
| 130 | |
| 131 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 132 | auto occMasterName = deviceNames.front(); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 133 | for (auto& name : deviceNames) |
| 134 | { |
| 135 | i2c_occ::i2cToDbus(name); |
Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 136 | name = std::string(OCC_NAME) + '_' + name; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 137 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 138 | statusObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 139 | std::make_unique<Status>(event, path.c_str(), *this)); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 140 | } |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 141 | // The first device is master occ |
| 142 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 143 | *statusObjects.front(), occMasterName); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 144 | #ifdef POWER10 |
| 145 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 146 | *statusObjects.front()); |
| 147 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 148 | } |
| 149 | #endif |
| 150 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 151 | #ifdef PLDM |
| 152 | bool Manager::updateOCCActive(instanceID instance, bool status) |
| 153 | { |
| 154 | return (statusObjects[instance])->occActive(status); |
| 155 | } |
| 156 | #endif |
| 157 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 158 | void Manager::pollerTimerExpired() |
| 159 | { |
| 160 | if (activeCount == 0) |
| 161 | { |
| 162 | // No OCCs running, so poll timer will not be restarted |
| 163 | log<level::INFO>("Manager::pollerTimerExpire(): No OCCs running, poll " |
| 164 | "timer not restarted"); |
| 165 | } |
| 166 | |
| 167 | if (!_pollTimer) |
| 168 | { |
| 169 | log<level::ERR>( |
| 170 | "Manager::pollerTimerExpired() ERROR: Timer not defined"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | for (auto& obj : statusObjects) |
| 175 | { |
| 176 | // Read sysfs to force kernel to poll OCC |
| 177 | obj->readOccState(); |
| 178 | } |
| 179 | |
| 180 | // Restart OCC poll timer |
| 181 | _pollTimer->restartOnce(std::chrono::seconds(pollInterval)); |
| 182 | } |
| 183 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 184 | } // namespace occ |
| 185 | } // namespace open_power |