Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2020 Intel Corporation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "speed_select.hpp" |
| 16 | |
| 17 | #include "cpuinfo.hpp" |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 18 | #include "cpuinfo_utils.hpp" |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 19 | |
| 20 | #include <peci.h> |
| 21 | |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 22 | #include <boost/asio/error.hpp> |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 23 | #include <boost/asio/steady_timer.hpp> |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 25 | #include <xyz/openbmc_project/Common/error.hpp> |
| 26 | #include <xyz/openbmc_project/Control/Processor/CurrentOperatingConfig/server.hpp> |
| 27 | #include <xyz/openbmc_project/Inventory/Item/Cpu/OperatingConfig/server.hpp> |
| 28 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 29 | #include <algorithm> |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <memory> |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 32 | #include <stdexcept> |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 33 | #include <string> |
| 34 | |
| 35 | namespace cpu_info |
| 36 | { |
| 37 | namespace sst |
| 38 | { |
| 39 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 40 | // Specialize char to print the integer value instead of ascii. We basically |
| 41 | // never want to print a single ascii char. |
| 42 | std::ostream& operator<<(std::ostream& os, uint8_t value) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 43 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 44 | return os << static_cast<int>(value); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 47 | bool checkPECIStatus(EPECIStatus libStatus, uint8_t completionCode) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 48 | { |
| 49 | if (libStatus != PECI_CC_SUCCESS || completionCode != PECI_DEV_CC_SUCCESS) |
| 50 | { |
| 51 | std::cerr << "PECI command failed." |
| 52 | << " Driver Status = " << libStatus << "," |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 53 | << " Completion Code = " << completionCode << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 59 | std::vector<uint32_t> convertMaskToList(std::bitset<64> mask) |
| 60 | { |
| 61 | std::vector<uint32_t> bitList; |
| 62 | for (size_t i = 0; i < mask.size(); ++i) |
| 63 | { |
| 64 | if (mask.test(i)) |
| 65 | { |
| 66 | bitList.push_back(i); |
| 67 | } |
| 68 | } |
| 69 | return bitList; |
| 70 | } |
| 71 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 72 | static std::vector<BackendProvider>& getProviders() |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 73 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 74 | static auto* providers = new std::vector<BackendProvider>; |
| 75 | return *providers; |
| 76 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 77 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 78 | void registerBackend(BackendProvider providerFn) |
| 79 | { |
| 80 | getProviders().push_back(providerFn); |
| 81 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 82 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 83 | std::unique_ptr<SSTInterface> getInstance(uint8_t address, CPUModel model) |
| 84 | { |
| 85 | DEBUG_PRINT << "Searching for provider for " << address << ", model " |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 86 | << std::hex << model << std::dec << '\n'; |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 87 | for (const auto& provider : getProviders()) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 88 | { |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 89 | try |
| 90 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 91 | auto interface = provider(address, model); |
| 92 | DEBUG_PRINT << "returned " << interface << '\n'; |
| 93 | if (interface) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 94 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 95 | return interface; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 98 | catch (...) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 99 | {} |
| 100 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 101 | DEBUG_PRINT << "No supported backends found\n"; |
| 102 | return nullptr; |
| 103 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 104 | |
| 105 | using BaseCurrentOperatingConfig = |
| 106 | sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Control:: |
| 107 | Processor::server::CurrentOperatingConfig>; |
| 108 | |
| 109 | using BaseOperatingConfig = |
| 110 | sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Inventory:: |
| 111 | Item::Cpu::server::OperatingConfig>; |
| 112 | |
| 113 | class OperatingConfig : public BaseOperatingConfig |
| 114 | { |
| 115 | public: |
| 116 | std::string path; |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 117 | unsigned int level; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 118 | |
| 119 | public: |
| 120 | using BaseOperatingConfig::BaseOperatingConfig; |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 121 | OperatingConfig(sdbusplus::bus_t& bus, unsigned int level_, |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 122 | std::string path_) : |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 123 | BaseOperatingConfig(bus, path_.c_str(), action::defer_emit), |
| 124 | path(std::move(path_)), level(level_) |
| 125 | {} |
| 126 | }; |
| 127 | |
| 128 | class CPUConfig : public BaseCurrentOperatingConfig |
| 129 | { |
| 130 | private: |
| 131 | /** Objects describing all available SST configs - not modifiable. */ |
| 132 | std::vector<std::unique_ptr<OperatingConfig>> availConfigs; |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 133 | sdbusplus::bus_t& bus; |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 134 | const uint8_t peciAddress; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 135 | const std::string path; ///< D-Bus path of CPU object |
| 136 | const CPUModel cpuModel; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 137 | |
| 138 | // Keep mutable copies of the properties so we can cache values that we |
| 139 | // retrieve in the getters. We don't want to throw an error on a D-Bus |
| 140 | // get-property call (extra error handling in clients), so by caching we can |
| 141 | // hide any temporary hiccup in PECI communication. |
| 142 | // These values can be changed by in-band software so we have to do a full |
| 143 | // PECI read on every get-property, and can't assume that values will change |
| 144 | // only when set-property is done. |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 145 | mutable unsigned int currentLevel; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 146 | mutable bool bfEnabled; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Enforce common pre-conditions for D-Bus set property handlers. |
| 150 | */ |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 151 | void setPropertyCheckOrThrow(SSTInterface& sst) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 152 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 153 | if (!sst.supportsControl()) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 154 | { |
| 155 | throw sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed(); |
| 156 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 157 | if (hostState != HostState::postComplete || !sst.ready()) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 158 | { |
| 159 | throw sdbusplus::xyz::openbmc_project::Common::Error::Unavailable(); |
| 160 | } |
| 161 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 162 | |
| 163 | public: |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 164 | CPUConfig(sdbusplus::bus_t& bus_, uint8_t index, CPUModel model) : |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 165 | BaseCurrentOperatingConfig(bus_, generatePath(index).c_str(), |
| 166 | action::defer_emit), |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 167 | bus(bus_), peciAddress(index + MIN_CLIENT_ADDR), |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 168 | path(generatePath(index)), cpuModel(model), currentLevel(0), |
| 169 | bfEnabled(false) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 170 | {} |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 171 | |
| 172 | // |
| 173 | // D-Bus Property Overrides |
| 174 | // |
| 175 | |
| 176 | sdbusplus::message::object_path appliedConfig() const override |
| 177 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 178 | DEBUG_PRINT << "Reading AppliedConfig\n"; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 179 | if (hostState != HostState::off) |
| 180 | { |
| 181 | // Otherwise, try to read current state |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 182 | auto sst = getInstance(peciAddress, cpuModel); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 183 | if (!sst || !sst->ready()) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 184 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 185 | std::cerr << __func__ |
| 186 | << ": Failed to get SST provider instance\n"; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 187 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 188 | else |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 189 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 190 | try |
| 191 | { |
| 192 | currentLevel = sst->currentLevel(); |
| 193 | } |
| 194 | catch (const PECIError& error) |
| 195 | { |
| 196 | std::cerr << "Failed to get SST-PP level: " << error.what() |
| 197 | << "\n"; |
| 198 | } |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 199 | } |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 200 | } |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 201 | return generateConfigPath(currentLevel); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool baseSpeedPriorityEnabled() const override |
| 205 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 206 | DEBUG_PRINT << "Reading BaseSpeedPriorityEnabled\n"; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 207 | if (hostState != HostState::off) |
| 208 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 209 | auto sst = getInstance(peciAddress, cpuModel); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 210 | if (!sst || !sst->ready()) |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 211 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 212 | std::cerr << __func__ |
| 213 | << ": Failed to get SST provider instance\n"; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 214 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 215 | else |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 216 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 217 | try |
| 218 | { |
| 219 | bfEnabled = sst->bfEnabled(currentLevel); |
| 220 | } |
| 221 | catch (const PECIError& error) |
| 222 | { |
| 223 | std::cerr << "Failed to get SST-BF status: " << error.what() |
| 224 | << "\n"; |
| 225 | } |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 226 | } |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 227 | } |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 228 | return bfEnabled; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | sdbusplus::message::object_path |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 232 | appliedConfig(sdbusplus::message::object_path value) override |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 233 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 234 | DEBUG_PRINT << "Writing AppliedConfig\n"; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 235 | const OperatingConfig* newConfig = nullptr; |
| 236 | for (const auto& config : availConfigs) |
| 237 | { |
| 238 | if (config->path == value.str) |
| 239 | { |
| 240 | newConfig = config.get(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (newConfig == nullptr) |
| 245 | { |
| 246 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 247 | InvalidArgument(); |
| 248 | } |
| 249 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 250 | auto sst = getInstance(peciAddress, cpuModel); |
| 251 | if (!sst) |
| 252 | { |
| 253 | std::cerr << __func__ << ": Failed to get SST provider instance\n"; |
| 254 | return sdbusplus::message::object_path(); |
| 255 | } |
| 256 | setPropertyCheckOrThrow(*sst); |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 257 | try |
| 258 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 259 | sst->setCurrentLevel(newConfig->level); |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 260 | currentLevel = newConfig->level; |
| 261 | } |
| 262 | catch (const PECIError& error) |
| 263 | { |
| 264 | std::cerr << "Failed to set new SST-PP level: " << error.what() |
| 265 | << "\n"; |
| 266 | throw sdbusplus::xyz::openbmc_project::Common::Device::Error:: |
| 267 | WriteFailure(); |
| 268 | } |
| 269 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 270 | // return value not used |
| 271 | return sdbusplus::message::object_path(); |
| 272 | } |
| 273 | |
Jonathan Doman | 0663963 | 2022-06-13 22:00:45 -0700 | [diff] [blame] | 274 | bool baseSpeedPriorityEnabled(bool /* value */) override |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 275 | { |
Jonathan Doman | 0663963 | 2022-06-13 22:00:45 -0700 | [diff] [blame] | 276 | DEBUG_PRINT << "Writing BaseSpeedPriorityEnabled not allowed\n"; |
| 277 | throw sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed(); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 278 | // return value not used |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // |
| 283 | // Additions |
| 284 | // |
| 285 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 286 | OperatingConfig& newConfig(unsigned int level) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 287 | { |
| 288 | availConfigs.emplace_back(std::make_unique<OperatingConfig>( |
| 289 | bus, level, generateConfigPath(level))); |
| 290 | return *availConfigs.back(); |
| 291 | } |
| 292 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 293 | std::string generateConfigPath(unsigned int level) const |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 294 | { |
| 295 | return path + "/config" + std::to_string(level); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Emit the interface added signals which were deferred. This is required |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 300 | * for ObjectMapper to pick up the objects, if we initially defered the |
| 301 | * signal emitting. |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 302 | */ |
| 303 | void finalize() |
| 304 | { |
| 305 | emit_added(); |
| 306 | for (auto& config : availConfigs) |
| 307 | { |
| 308 | config->emit_added(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static std::string generatePath(int index) |
| 313 | { |
Jonathan Doman | 0a38537 | 2021-03-08 17:04:13 -0800 | [diff] [blame] | 314 | return cpuPath + std::to_string(index); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
| 318 | /** |
| 319 | * Retrieve the SST parameters for a single config and fill the values into the |
| 320 | * properties on the D-Bus interface. |
| 321 | * |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 322 | * @param[in,out] sst Interface to SST backend. |
| 323 | * @param[in] level Config TDP level to retrieve. |
| 324 | * @param[out] config D-Bus interface to update. |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 325 | */ |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 326 | static void getSingleConfig(SSTInterface& sst, unsigned int level, |
| 327 | OperatingConfig& config) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 328 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 329 | config.powerLimit(sst.tdp(level)); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 330 | DEBUG_PRINT << " TDP = " << config.powerLimit() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 331 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 332 | config.availableCoreCount(sst.coreCount(level)); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 333 | DEBUG_PRINT << " coreCount = " << config.availableCoreCount() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 334 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 335 | config.baseSpeed(sst.p1Freq(level)); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 336 | DEBUG_PRINT << " baseSpeed = " << config.baseSpeed() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 337 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 338 | config.maxSpeed(sst.p0Freq(level)); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 339 | DEBUG_PRINT << " maxSpeed = " << config.maxSpeed() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 340 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 341 | config.maxJunctionTemperature(sst.prochotTemp(level)); |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 342 | DEBUG_PRINT << " procHot = " << config.maxJunctionTemperature() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 343 | |
| 344 | // Construct BaseSpeedPrioritySettings |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 345 | std::vector<std::tuple<uint32_t, std::vector<uint32_t>>> baseSpeeds; |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 346 | if (sst.bfSupported(level)) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 347 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 348 | std::vector<uint32_t> totalCoreList, loFreqCoreList, hiFreqCoreList; |
| 349 | totalCoreList = sst.enabledCoreList(level); |
| 350 | hiFreqCoreList = sst.bfHighPriorityCoreList(level); |
| 351 | std::set_difference( |
| 352 | totalCoreList.begin(), totalCoreList.end(), hiFreqCoreList.begin(), |
| 353 | hiFreqCoreList.end(), |
| 354 | std::inserter(loFreqCoreList, loFreqCoreList.begin())); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 355 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 356 | baseSpeeds = {{sst.bfHighPriorityFreq(level), hiFreqCoreList}, |
| 357 | {sst.bfLowPriorityFreq(level), loFreqCoreList}}; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 358 | } |
| 359 | config.baseSpeedPrioritySettings(baseSpeeds); |
| 360 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 361 | config.turboProfile(sst.sseTurboProfile(level)); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Retrieve all SST configuration info for all discoverable CPUs, and publish |
| 366 | * the info on new D-Bus objects on the given bus connection. |
| 367 | * |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 368 | * @param[in,out] ioc ASIO context. |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 369 | * @param[in,out] conn D-Bus ASIO connection. |
| 370 | * |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 371 | * @return Whether discovery was successfully finished. |
| 372 | * |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 373 | * @throw PECIError A PECI command failed on a CPU which had previously |
| 374 | * responded to a command. |
| 375 | */ |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 376 | static bool discoverCPUsAndConfigs(boost::asio::io_context& ioc, |
| 377 | sdbusplus::asio::connection& conn) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 378 | { |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 379 | // Persistent list - only populated after complete/successful discovery |
| 380 | static std::vector<std::unique_ptr<CPUConfig>> cpus; |
| 381 | cpus.clear(); |
| 382 | |
| 383 | // Temporary staging list. In case there is any failure, these temporary |
| 384 | // objects will get dropped to avoid presenting incomplete info until the |
| 385 | // next discovery attempt. |
| 386 | std::vector<std::unique_ptr<CPUConfig>> cpuList; |
| 387 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 388 | for (uint8_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; ++i) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 389 | { |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 390 | // Let the event handler run any waiting tasks. If there is a lot of |
| 391 | // PECI contention, SST discovery could take a long time. This lets us |
| 392 | // get updates to hostState and handle any D-Bus requests. |
| 393 | ioc.poll(); |
| 394 | |
| 395 | if (hostState == HostState::off) |
| 396 | { |
| 397 | return false; |
| 398 | } |
| 399 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 400 | unsigned int cpuIndex = i - MIN_CLIENT_ADDR; |
| 401 | DEBUG_PRINT << "Discovering CPU " << cpuIndex << '\n'; |
| 402 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 403 | // We could possibly check D-Bus for CPU presence and model, but PECI is |
| 404 | // 10x faster and so much simpler. |
| 405 | uint8_t cc, stepping; |
| 406 | CPUModel cpuModel; |
| 407 | EPECIStatus status = peci_GetCPUID(i, &cpuModel, &stepping, &cc); |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 408 | if (status == PECI_CC_TIMEOUT) |
| 409 | { |
| 410 | // Timing out indicates the CPU is present but PCS services not |
| 411 | // working yet. Try again later. |
| 412 | throw PECIError("Get CPUID timed out"); |
| 413 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 414 | if (status == PECI_CC_CPU_NOT_PRESENT) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 415 | { |
| 416 | continue; |
| 417 | } |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 418 | if (status != PECI_CC_SUCCESS || cc != PECI_DEV_CC_SUCCESS) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 419 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 420 | std::cerr << "GetCPUID returned status " << status |
| 421 | << ", cc = " << cc << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 425 | std::unique_ptr<SSTInterface> sst = getInstance(i, cpuModel); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 426 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 427 | if (!sst) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 428 | { |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 429 | // No supported backend for this CPU. |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | if (!sst->ready()) |
| 434 | { |
| 435 | // Supported CPU but it can't be queried yet. Try again later. |
| 436 | std::cerr << "sst not ready yet\n"; |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | if (!sst->ppEnabled()) |
| 441 | { |
| 442 | // Supported CPU but the specific SKU doesn't support SST-PP. |
| 443 | std::cerr << "CPU doesn't support SST-PP\n"; |
| 444 | continue; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Create the per-CPU configuration object |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 448 | cpuList.emplace_back( |
| 449 | std::make_unique<CPUConfig>(conn, cpuIndex, cpuModel)); |
| 450 | CPUConfig& cpu = *cpuList.back(); |
| 451 | |
| 452 | bool foundCurrentLevel = false; |
| 453 | |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 454 | for (unsigned int level = 0; level <= sst->maxLevel(); ++level) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 455 | { |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 456 | DEBUG_PRINT << "checking level " << level << ": "; |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 457 | // levels 1 and 2 were legacy/deprecated, originally used for AVX |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 458 | // license pre-granting. They may be reused for more levels in |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 459 | // future generations. So we need to check for discontinuities. |
| 460 | if (!sst->levelSupported(level)) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 461 | { |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 462 | DEBUG_PRINT << "not supported\n"; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 463 | continue; |
| 464 | } |
| 465 | |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 466 | DEBUG_PRINT << "supported\n"; |
| 467 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 468 | getSingleConfig(*sst, level, cpu.newConfig(level)); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 469 | |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 470 | if (level == sst->currentLevel()) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 471 | { |
| 472 | foundCurrentLevel = true; |
| 473 | } |
| 474 | } |
| 475 | |
Jonathan Doman | b4c3bcd | 2023-03-09 11:41:41 -0800 | [diff] [blame] | 476 | DEBUG_PRINT << "current level is " << sst->currentLevel() << '\n'; |
| 477 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 478 | if (!foundCurrentLevel) |
| 479 | { |
| 480 | // In case we didn't encounter a PECI error, but also didn't find |
| 481 | // the config which is supposedly applied, we won't be able to |
| 482 | // populate the CurrentOperatingConfig so we have to remove this CPU |
| 483 | // from consideration. |
| 484 | std::cerr << "CPU " << cpuIndex |
| 485 | << " claimed SST support but invalid configs\n"; |
| 486 | cpuList.pop_back(); |
| 487 | continue; |
| 488 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 489 | } |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 490 | |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 491 | cpuList.swap(cpus); |
| 492 | std::for_each(cpus.begin(), cpus.end(), [](auto& cpu) { cpu->finalize(); }); |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 493 | return true; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 496 | /** |
| 497 | * Attempt discovery process, and if it fails, wait for 10 seconds to try again. |
| 498 | */ |
| 499 | static void discoverOrWait() |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 500 | { |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 501 | static boost::asio::steady_timer peciRetryTimer(dbus::getIOContext()); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 502 | static int peciErrorCount = 0; |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 503 | bool finished = false; |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 504 | |
| 505 | // This function may be called from hostStateHandler or by retrying itself. |
| 506 | // In case those overlap, cancel any outstanding retry timer. |
| 507 | peciRetryTimer.cancel(); |
| 508 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 509 | try |
| 510 | { |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 511 | DEBUG_PRINT << "Starting discovery\n"; |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 512 | finished = discoverCPUsAndConfigs(dbus::getIOContext(), |
| 513 | *dbus::getConnection()); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 514 | } |
| 515 | catch (const PECIError& err) |
| 516 | { |
| 517 | std::cerr << "PECI Error: " << err.what() << '\n'; |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 518 | |
| 519 | // In case of repeated failure to finish discovery, turn off this |
| 520 | // feature altogether. Possible cause is that the CPU model does not |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 521 | // actually support the necessary commands. |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 522 | if (++peciErrorCount >= 50) |
| 523 | { |
| 524 | std::cerr << "Aborting SST discovery\n"; |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | std::cerr << "Retrying SST discovery later\n"; |
| 529 | } |
| 530 | |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 531 | DEBUG_PRINT << "Finished discovery attempt: " << finished << '\n'; |
| 532 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 533 | // Retry later if no CPUs were available, or there was a PECI error. |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 534 | if (!finished) |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 535 | { |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 536 | peciRetryTimer.expires_after(std::chrono::seconds(10)); |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 537 | peciRetryTimer.async_wait([](boost::system::error_code ec) { |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 538 | if (ec) |
| 539 | { |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 540 | if (ec != boost::asio::error::operation_aborted) |
| 541 | { |
| 542 | std::cerr << "SST PECI Retry Timer failed: " << ec << '\n'; |
| 543 | } |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 544 | return; |
| 545 | } |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 546 | discoverOrWait(); |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 547 | }); |
| 548 | } |
| 549 | } |
| 550 | |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 551 | static void hostStateHandler(HostState prevState, HostState) |
| 552 | { |
| 553 | if (prevState == HostState::off) |
| 554 | { |
| 555 | // Start or re-start discovery any time the host moves out of the |
| 556 | // powered off state. |
| 557 | discoverOrWait(); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void init() |
| 562 | { |
| 563 | addHostStateCallback(hostStateHandler); |
| 564 | } |
| 565 | |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 566 | } // namespace sst |
| 567 | } // namespace cpu_info |