Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cstring> |
| 4 | #include <vector> |
| 5 | #include <experimental/filesystem> |
| 6 | #include <functional> |
| 7 | #include <sdbusplus/bus.hpp> |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 10 | #include <powercap.hpp> |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 11 | #include "occ_pass_through.hpp" |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 12 | #include "occ_status.hpp" |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 13 | #include "occ_finder.hpp" |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 14 | #include "config.h" |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 15 | #include "i2c_occ.hpp" |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 16 | |
| 17 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 18 | namespace open_power |
| 19 | { |
| 20 | namespace occ |
| 21 | { |
| 22 | |
| 23 | /** @class Manager |
| 24 | * @brief Builds and manages OCC objects |
| 25 | */ |
| 26 | struct Manager |
| 27 | { |
| 28 | public: |
| 29 | Manager() = delete; |
| 30 | Manager(const Manager&) = delete; |
| 31 | Manager& operator=(const Manager&) = delete; |
| 32 | Manager(Manager&&) = default; |
| 33 | Manager& operator=(Manager&&) = default; |
| 34 | ~Manager() = default; |
| 35 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 36 | /** @brief Adds OCC pass-through and status objects on the bus |
| 37 | * when corresponding CPU inventory is created. |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 38 | * |
| 39 | * @param[in] bus - handle to the bus |
| 40 | * @param[in] event - Unique ptr reference to sd_event |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 41 | */ |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 42 | Manager(sdbusplus::bus::bus& bus, |
| 43 | EventPtr& event) : |
| 44 | bus(bus), |
| 45 | event(event) |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 46 | { |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 47 | #ifdef I2C_OCC |
| 48 | // I2C OCC status objects are initialized directly |
| 49 | initStatusObjects(); |
| 50 | #else |
Vishwanatha Subbanna | b57f151 | 2017-09-04 15:06:20 +0530 | [diff] [blame^] | 51 | |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 52 | // Check if CPU inventory exists already. |
Vishwanatha Subbanna | 1ec291f | 2017-08-21 17:07:29 +0530 | [diff] [blame] | 53 | auto occs = open_power::occ::finder::get(bus); |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 54 | if (occs.empty()) |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 55 | { |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 56 | // Need to watch for CPU inventory creation. |
| 57 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 58 | { |
| 59 | auto path = std::string(CPU_PATH) + std::to_string(id); |
| 60 | cpuMatches.emplace_back( |
| 61 | bus, |
| 62 | sdbusRule::interfacesAdded() + |
| 63 | sdbusRule::argNpath(0, path), |
| 64 | std::bind(std::mem_fn(&Manager::cpuCreated), |
| 65 | this, std::placeholders::_1)); |
| 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | for (const auto& occ : occs) |
| 71 | { |
| 72 | // CPU inventory exists already, OCC objects can be created. |
| 73 | createObjects(occ); |
| 74 | } |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 75 | } |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 76 | #endif |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 77 | } |
| 78 | |
Vishwanatha Subbanna | b57f151 | 2017-09-04 15:06:20 +0530 | [diff] [blame^] | 79 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 80 | private: |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 81 | /** @brief Callback that responds to cpu creation in the inventory - |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 82 | * by creating the needed objects. |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 83 | * |
| 84 | * @param[in] msg - bus message |
| 85 | * |
| 86 | * @returns 0 to indicate success |
| 87 | */ |
| 88 | int cpuCreated(sdbusplus::message::message& msg) |
| 89 | { |
| 90 | namespace fs = std::experimental::filesystem; |
| 91 | |
| 92 | sdbusplus::message::object_path o; |
| 93 | msg.read(o); |
| 94 | fs::path cpuPath(std::string(std::move(o))); |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 95 | |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 96 | auto name = cpuPath.filename().string(); |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 97 | auto index = name.find(CPU_NAME); |
| 98 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 99 | |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 100 | createObjects(name); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 105 | /** @brief Create child OCC objects. |
| 106 | * |
| 107 | * @param[in] occ - the occ name, such as occ0. |
| 108 | */ |
| 109 | void createObjects(const std::string& occ) |
| 110 | { |
| 111 | auto path = fs::path(OCC_CONTROL_ROOT) / occ; |
| 112 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 113 | passThroughObjects.emplace_back( |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 114 | std::make_unique<PassThrough>( |
| 115 | bus, |
| 116 | path.c_str())); |
| 117 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 118 | statusObjects.emplace_back( |
| 119 | std::make_unique<Status>( |
| 120 | bus, |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 121 | event, |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 122 | path.c_str(), |
| 123 | std::bind(std::mem_fn(&Manager::statusCallBack), |
| 124 | this, std::placeholders::_1))); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 125 | |
| 126 | // Create the power cap monitor object for master occ (0) |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 127 | if (!pcap) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 128 | { |
| 129 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
| 130 | bus, |
Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 131 | *statusObjects.front()); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 132 | } |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 133 | } |
| 134 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 135 | /** @brief Callback handler invoked by Status object when the OccActive |
| 136 | * property is changed. This is needed to make sure that the |
| 137 | * error detection is started only after all the OCCs are bound. |
| 138 | * Similarly, when one of the OCC gets its OccActive property |
| 139 | * un-set, then the OCC error detection needs to be stopped on |
| 140 | * all the OCCs |
| 141 | * |
| 142 | * @param[in] status - OccActive status |
| 143 | */ |
| 144 | void statusCallBack(bool status) |
| 145 | { |
| 146 | using namespace phosphor::logging; |
| 147 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 148 | Error::InternalFailure; |
| 149 | |
| 150 | // At this time, it won't happen but keeping it |
| 151 | // here just in case something changes in the future |
| 152 | if ((activeCount == 0) && (!status)) |
| 153 | { |
| 154 | log<level::ERR>("Invalid update on OCCActive"); |
| 155 | elog<InternalFailure>(); |
| 156 | } |
| 157 | |
| 158 | activeCount += status ? 1 : -1; |
| 159 | |
| 160 | // If all the OCCs are bound, then start error detection |
| 161 | if (activeCount == statusObjects.size()) |
| 162 | { |
| 163 | for (const auto& occ: statusObjects) |
| 164 | { |
| 165 | occ->addErrorWatch(); |
| 166 | } |
| 167 | } |
| 168 | else if (!status) |
| 169 | { |
| 170 | // If some OCCs are not bound yet, those will be a NO-OP |
| 171 | for (const auto& occ: statusObjects) |
| 172 | { |
| 173 | occ->removeErrorWatch(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 178 | /** @brief reference to the bus */ |
| 179 | sdbusplus::bus::bus& bus; |
| 180 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 181 | /** @brief reference to sd_event wrapped in unique_ptr */ |
| 182 | EventPtr& event; |
| 183 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 184 | /** @brief OCC pass-through objects */ |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 185 | std::vector<std::unique_ptr<PassThrough>> passThroughObjects; |
| 186 | |
| 187 | /** @brief OCC Status objects */ |
| 188 | std::vector<std::unique_ptr<Status>> statusObjects; |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 189 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 190 | /** @brief Power cap monitor and occ notification object */ |
| 191 | std::unique_ptr<open_power::occ::powercap::PowerCap> pcap; |
| 192 | |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 193 | /** @brief sbdbusplus match objects */ |
| 194 | std::vector<sdbusplus::bus::match_t> cpuMatches; |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 195 | |
| 196 | /** @brief Number of OCCs that are bound */ |
| 197 | uint8_t activeCount = 0; |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 198 | |
| 199 | #ifdef I2C_OCC |
| 200 | /** @brief Init Status objects for I2C OCC devices |
| 201 | * |
| 202 | * It iterates in /sys/bus/i2c/devices, finds all occ hwmon devices |
| 203 | * and creates status objects. |
| 204 | */ |
| 205 | void initStatusObjects() |
| 206 | { |
| 207 | // Make sure we have a valid path string |
| 208 | static_assert(sizeof(DEV_PATH) != 0); |
| 209 | |
| 210 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
| 211 | for (auto& name : deviceNames) |
| 212 | { |
| 213 | i2c_occ::i2cToDbus(name); |
| 214 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 215 | statusObjects.emplace_back( |
| 216 | std::make_unique<Status>( |
| 217 | bus, |
| 218 | event, |
| 219 | path.c_str())); |
| 220 | } |
| 221 | } |
| 222 | #endif |
Vishwanatha Subbanna | 2180b2d | 2017-06-28 14:05:57 +0530 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | } // namespace occ |
| 226 | } // namespace open_power |