Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 1 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 2 | #include <powercap.hpp> |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 3 | |
| 4 | namespace open_power |
| 5 | { |
| 6 | namespace occ |
| 7 | { |
| 8 | namespace powercap |
| 9 | { |
| 10 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 11 | constexpr auto PCAP_PATH = "/xyz/openbmc_project/control/host0/power_cap"; |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 12 | constexpr auto PCAP_INTERFACE = "xyz.openbmc_project.Control.Power.Cap"; |
| 13 | |
| 14 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 15 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 16 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 17 | |
| 18 | constexpr auto POWER_CAP_PROP = "PowerCap"; |
| 19 | constexpr auto POWER_CAP_ENABLE_PROP = "PowerCapEnable"; |
| 20 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 21 | using namespace phosphor::logging; |
| 22 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 23 | std::string PowerCap::getService(std::string path, std::string interface) |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 24 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 25 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 26 | MAPPER_INTERFACE, "GetObject"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 27 | |
| 28 | mapper.append(path, std::vector<std::string>({interface})); |
| 29 | auto mapperResponseMsg = bus.call(mapper); |
| 30 | |
| 31 | if (mapperResponseMsg.is_method_error()) |
| 32 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 33 | log<level::ERR>("Error in mapper call", entry("PATH=%s", path.c_str()), |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 34 | entry("INTERFACE=%s", interface.c_str())); |
| 35 | // TODO openbmc/openbmc#851 - Once available, throw returned error |
| 36 | throw std::runtime_error("Error in mapper call"); |
| 37 | } |
| 38 | |
| 39 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 40 | mapperResponseMsg.read(mapperResponse); |
| 41 | if (mapperResponse.empty()) |
| 42 | { |
| 43 | log<level::ERR>("Error reading mapper response", |
| 44 | entry("PATH=%s", path.c_str()), |
| 45 | entry("INTERFACE=%s", interface.c_str())); |
| 46 | // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently |
| 47 | throw std::runtime_error("Error reading mapper response"); |
| 48 | } |
| 49 | |
| 50 | return mapperResponse.begin()->first; |
| 51 | } |
| 52 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 53 | uint32_t PowerCap::getOccInput(uint32_t pcap, bool pcapEnabled) |
| 54 | { |
| 55 | if (!pcapEnabled) |
| 56 | { |
| 57 | // Pcap disabled, return 0 to indicate disabled |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | // If pcap is not disabled then just return the pcap with the derating |
| 62 | // factor applied. |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 63 | return ((static_cast<uint64_t>(pcap) * PS_DERATING_FACTOR) / 100); |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 66 | uint32_t PowerCap::getPcap() |
| 67 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 68 | auto settingService = getService(PCAP_PATH, PCAP_INTERFACE); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 69 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 70 | auto method = |
| 71 | this->bus.new_method_call(settingService.c_str(), PCAP_PATH, |
| 72 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 73 | |
| 74 | method.append(PCAP_INTERFACE, POWER_CAP_PROP); |
| 75 | auto reply = this->bus.call(method); |
| 76 | |
| 77 | if (reply.is_method_error()) |
| 78 | { |
| 79 | log<level::ERR>("Error in getPcap prop"); |
| 80 | return 0; |
| 81 | } |
| 82 | sdbusplus::message::variant<uint32_t> pcap; |
| 83 | reply.read(pcap); |
| 84 | |
| 85 | return pcap.get<uint32_t>(); |
| 86 | } |
| 87 | |
| 88 | bool PowerCap::getPcapEnabled() |
| 89 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 90 | auto settingService = getService(PCAP_PATH, PCAP_INTERFACE); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 91 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 92 | auto method = |
| 93 | this->bus.new_method_call(settingService.c_str(), PCAP_PATH, |
| 94 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 95 | |
| 96 | method.append(PCAP_INTERFACE, POWER_CAP_ENABLE_PROP); |
| 97 | auto reply = this->bus.call(method); |
| 98 | |
| 99 | if (reply.is_method_error()) |
| 100 | { |
| 101 | log<level::ERR>("Error in getPcapEnabled prop"); |
| 102 | return 0; |
| 103 | } |
| 104 | sdbusplus::message::variant<bool> pcapEnabled; |
| 105 | reply.read(pcapEnabled); |
| 106 | |
| 107 | return pcapEnabled.get<bool>(); |
| 108 | } |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 109 | |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 110 | void PowerCap::writeOcc(uint32_t pcapValue) |
| 111 | { |
| 112 | // Create path out to master occ hwmon entry |
| 113 | std::unique_ptr<fs::path> fileName = |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 114 | std::make_unique<fs::path>(OCC_HWMON_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 115 | *fileName /= occMasterName; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 116 | *fileName /= "/hwmon/"; |
| 117 | |
| 118 | // Need to get the hwmonXX directory name, there better only be 1 dir |
| 119 | assert(std::distance(fs::directory_iterator(*fileName), |
| 120 | fs::directory_iterator{}) == 1); |
| 121 | // Now set our path to this full path, including this hwmonXX directory |
| 122 | fileName = std::make_unique<fs::path>(*fs::directory_iterator(*fileName)); |
| 123 | // Append on the hwmon string where we write the user power cap |
| 124 | *fileName /= "/caps1_user"; |
| 125 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 126 | auto pcapString{std::to_string(pcapValue)}; |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 127 | |
| 128 | log<level::INFO>("Writing pcap value to hwmon", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 129 | entry("PCAP_PATH=%s", fileName->c_str()), |
| 130 | entry("PCAP_VALUE=%s", pcapString.c_str())); |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 131 | // Open the hwmon file and write the power cap |
| 132 | std::ofstream file(*fileName, std::ios::out); |
| 133 | file << pcapString; |
| 134 | file.close(); |
| 135 | return; |
| 136 | } |
| 137 | |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 138 | void PowerCap::pcapChanged(sdbusplus::message::message& msg) |
| 139 | { |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 140 | if (!occStatus.occActive()) |
| 141 | { |
| 142 | // Nothing to do |
| 143 | return; |
| 144 | } |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 145 | |
| 146 | uint32_t pcap = 0; |
| 147 | bool pcapEnabled = false; |
| 148 | |
| 149 | std::string msgSensor; |
| 150 | std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; |
| 151 | msg.read(msgSensor, msgData); |
| 152 | |
| 153 | // Retrieve which property changed via the msg and read the other one |
| 154 | auto valPropMap = msgData.find(POWER_CAP_PROP); |
| 155 | if (valPropMap != msgData.end()) |
| 156 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 157 | pcap = |
| 158 | sdbusplus::message::variant_ns::get<uint32_t>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 159 | pcapEnabled = getPcapEnabled(); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | valPropMap = msgData.find(POWER_CAP_ENABLE_PROP); |
| 164 | if (valPropMap != msgData.end()) |
| 165 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 166 | pcapEnabled = |
| 167 | sdbusplus::message::variant_ns::get<bool>(valPropMap->second); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 168 | pcap = getPcap(); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | log<level::INFO>("Unknown power cap property changed"); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 177 | log<level::INFO>("Power Cap Property Change", entry("PCAP=%u", pcap), |
| 178 | entry("PCAP_ENABLED=%u", pcapEnabled)); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 179 | |
| 180 | // Determine desired action to write to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 181 | |
Andrew Geissler | 4cea4d2 | 2017-07-10 15:13:33 -0500 | [diff] [blame] | 182 | auto occInput = getOccInput(pcap, pcapEnabled); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 183 | |
| 184 | // Write action to occ |
Andrew Geissler | 6ac874e | 2017-07-10 15:54:58 -0500 | [diff] [blame] | 185 | writeOcc(occInput); |
Andrew Geissler | 52cf26a | 2017-07-06 12:56:32 -0500 | [diff] [blame] | 186 | |
| 187 | return; |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 190 | } // namespace powercap |
Andrew Geissler | 32016d1 | 2017-06-20 15:46:52 -0500 | [diff] [blame] | 191 | |
| 192 | } // namespace occ |
| 193 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 194 | } // namespace open_power |