Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2020 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "cpuinfo.hpp" |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 18 | #include "cpuinfo_utils.hpp" |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <stdio.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | |
| 25 | #include <boost/asio/io_service.hpp> |
| 26 | #include <boost/asio/steady_timer.hpp> |
Jason M. Bills | 1e1ca73 | 2022-09-21 12:37:52 -0700 | [diff] [blame] | 27 | #include <boost/container/flat_map.hpp> |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 28 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 29 | #include <iostream> |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 30 | #include <list> |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 31 | #include <optional> |
| 32 | #include <sstream> |
| 33 | #include <string> |
| 34 | |
| 35 | extern "C" |
| 36 | { |
| 37 | #include <i2c/smbus.h> |
| 38 | #include <linux/i2c-dev.h> |
| 39 | } |
| 40 | |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 41 | #if PECI_ENABLED |
| 42 | #include "speed_select.hpp" |
| 43 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 44 | #include <peci.h> |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 45 | #endif |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 46 | |
| 47 | #include <phosphor-logging/log.hpp> |
| 48 | #include <sdbusplus/asio/object_server.hpp> |
| 49 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 50 | namespace cpu_info |
| 51 | { |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 52 | static constexpr bool debug = false; |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 53 | static constexpr const char* assetInterfaceName = |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 54 | "xyz.openbmc_project.Inventory.Decorator.Asset"; |
| 55 | static constexpr const char* cpuProcessName = |
| 56 | "xyz.openbmc_project.Smbios.MDR_V2"; |
| 57 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 58 | // constants for reading SSPEC or QDF string from PIROM |
Jason M. Bills | b86e4f1 | 2024-04-01 13:19:19 -0700 | [diff] [blame^] | 59 | // Currently, they are the same for platforms with Ice Lake |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 60 | static constexpr uint8_t defaultI2cBus = 13; |
| 61 | static constexpr uint8_t defaultI2cSlaveAddr0 = 0x50; |
| 62 | static constexpr uint8_t sspecRegAddr = 0xd; |
| 63 | static constexpr uint8_t sspecSize = 6; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 64 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 65 | using CPUInfoMap = boost::container::flat_map<size_t, std::shared_ptr<CPUInfo>>; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 66 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 67 | static CPUInfoMap cpuInfoMap = {}; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 68 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Simple aggregate to define an external D-Bus property which needs to be set |
| 71 | * by this application. |
| 72 | */ |
| 73 | struct CpuProperty |
| 74 | { |
| 75 | std::string object; |
| 76 | std::string interface; |
| 77 | std::string name; |
| 78 | std::string value; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * List of properties we want to set on other D-Bus objects. This list is kept |
| 83 | * around so that if any target objects are removed+readded, then we can set the |
| 84 | * values again. |
| 85 | */ |
| 86 | static std::list<CpuProperty> propertiesToSet; |
| 87 | |
| 88 | static std::ostream& logStream(int cpu) |
| 89 | { |
| 90 | return std::cerr << "[CPU " << cpu << "] "; |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | setCpuProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 95 | size_t cpu, const std::string& interface, |
| 96 | const std::string& propName, const std::string& propVal); |
| 97 | static void |
| 98 | setDbusProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 99 | size_t cpu, const CpuProperty& newProp); |
| 100 | static void createCpuUpdatedMatch( |
| 101 | const std::shared_ptr<sdbusplus::asio::connection>& conn, size_t cpu); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 102 | |
| 103 | static std::optional<std::string> readSSpec(uint8_t bus, uint8_t slaveAddr, |
| 104 | uint8_t regAddr, size_t count) |
| 105 | { |
| 106 | unsigned long funcs = 0; |
| 107 | std::string devPath = "/dev/i2c-" + std::to_string(bus); |
| 108 | |
| 109 | int fd = ::open(devPath.c_str(), O_RDWR); |
| 110 | if (fd < 0) |
| 111 | { |
| 112 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 113 | "Error in open!", |
| 114 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 115 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 116 | return std::nullopt; |
| 117 | } |
| 118 | |
| 119 | if (::ioctl(fd, I2C_FUNCS, &funcs) < 0) |
| 120 | { |
| 121 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 122 | "Error in I2C_FUNCS!", |
| 123 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 124 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 125 | ::close(fd); |
| 126 | return std::nullopt; |
| 127 | } |
| 128 | |
| 129 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 130 | { |
| 131 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 132 | "i2c bus does not support read!", |
| 133 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 134 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 135 | ::close(fd); |
| 136 | return std::nullopt; |
| 137 | } |
| 138 | |
| 139 | if (::ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0) |
| 140 | { |
| 141 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 142 | "Error in I2C_SLAVE_FORCE!", |
| 143 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 144 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 145 | ::close(fd); |
| 146 | return std::nullopt; |
| 147 | } |
| 148 | |
| 149 | int value = 0; |
| 150 | std::string sspec; |
| 151 | sspec.reserve(count); |
| 152 | |
| 153 | for (size_t i = 0; i < count; i++) |
| 154 | { |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 155 | value = ::i2c_smbus_read_byte_data(fd, regAddr + i); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 156 | if (value < 0) |
| 157 | { |
| 158 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 159 | "Error in i2c read!", |
| 160 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 161 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 162 | ::close(fd); |
| 163 | return std::nullopt; |
| 164 | } |
| 165 | if (!std::isprint(static_cast<unsigned char>(value))) |
| 166 | { |
| 167 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 168 | "Non printable value in sspec, ignored."); |
| 169 | continue; |
| 170 | } |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 171 | // sspec always starts with S, |
| 172 | // if not assume it is QDF string which starts at offset 2 |
| 173 | if (i == 0 && static_cast<unsigned char>(value) != 'S') |
| 174 | { |
| 175 | i = 1; |
| 176 | continue; |
| 177 | } |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 178 | sspec.push_back(static_cast<unsigned char>(value)); |
| 179 | } |
| 180 | ::close(fd); |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 181 | |
| 182 | if (sspec.size() < 4) |
| 183 | { |
| 184 | return std::nullopt; |
| 185 | } |
| 186 | |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 187 | return sspec; |
| 188 | } |
| 189 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Higher level SSpec logic. |
| 192 | * This handles retrying the PIROM reads until two subsequent reads are |
| 193 | * successful and return matching data. When we have confidence that the data |
| 194 | * read is correct, then set the property on D-Bus. |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 195 | */ |
| 196 | static void |
| 197 | tryReadSSpec(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 198 | size_t cpuIndex) |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 199 | { |
| 200 | static int failedReads = 0; |
| 201 | |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 202 | auto cpuInfoIt = cpuInfoMap.find(cpuIndex); |
| 203 | if (cpuInfoIt == cpuInfoMap.end()) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | auto cpuInfo = cpuInfoIt->second; |
| 208 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 209 | std::optional<std::string> newSSpec = |
| 210 | readSSpec(cpuInfo->i2cBus, cpuInfo->i2cDevice, sspecRegAddr, sspecSize); |
| 211 | logStream(cpuInfo->id) << "SSpec read status: " |
| 212 | << static_cast<bool>(newSSpec) << "\n"; |
| 213 | if (newSSpec && newSSpec == cpuInfo->sSpec) |
| 214 | { |
| 215 | setCpuProperty(conn, cpuInfo->id, assetInterfaceName, "Model", |
| 216 | *newSSpec); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // If this read failed, back off for a little longer so that hopefully the |
| 221 | // transient condition affecting PIROM reads will pass, but give up after |
| 222 | // several consecutive failures. But if this read looked OK, try again |
| 223 | // sooner to confirm it. |
| 224 | int retrySeconds; |
| 225 | if (newSSpec) |
| 226 | { |
| 227 | retrySeconds = 1; |
| 228 | failedReads = 0; |
| 229 | cpuInfo->sSpec = *newSSpec; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | retrySeconds = 5; |
| 234 | if (++failedReads > 10) |
| 235 | { |
| 236 | logStream(cpuInfo->id) << "PIROM Read failed too many times\n"; |
| 237 | return; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | auto sspecTimer = std::make_shared<boost::asio::steady_timer>( |
| 242 | conn->get_io_context(), std::chrono::seconds(retrySeconds)); |
| 243 | sspecTimer->async_wait( |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 244 | [sspecTimer, conn, cpuIndex](boost::system::error_code ec) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 245 | if (ec) |
| 246 | { |
| 247 | return; |
| 248 | } |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 249 | tryReadSSpec(conn, cpuIndex); |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 250 | }); |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Add a D-Bus property to the global list, and attempt to set it by calling |
| 255 | * `setDbusProperty`. |
| 256 | * |
| 257 | * @param[in,out] conn D-Bus connection. |
| 258 | * @param[in] cpu 1-based CPU index. |
| 259 | * @param[in] interface Interface to set. |
| 260 | * @param[in] propName Property to set. |
| 261 | * @param[in] propVal Value to set. |
| 262 | */ |
| 263 | static void |
| 264 | setCpuProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 265 | size_t cpu, const std::string& interface, |
| 266 | const std::string& propName, const std::string& propVal) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 267 | { |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 268 | // cpuId from configuration is one based as |
| 269 | // dbus object path used by smbios is 0 based |
| 270 | const std::string objectPath = cpuPath + std::to_string(cpu - 1); |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 271 | |
| 272 | // Can switch to emplace_back if you define a CpuProperty constructor. |
| 273 | propertiesToSet.push_back( |
| 274 | CpuProperty{objectPath, interface, propName, propVal}); |
| 275 | |
| 276 | setDbusProperty(conn, cpu, propertiesToSet.back()); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 279 | /** |
| 280 | * Set a D-Bus property which is already contained in the global list, and also |
| 281 | * setup a D-Bus match to make sure the target property stays correct. |
| 282 | * |
| 283 | * @param[in,out] conn D-Bus connection. |
| 284 | * @param[in] cpu 1-baesd CPU index. |
| 285 | * @param[in] newProp Property to set. |
| 286 | */ |
| 287 | static void |
| 288 | setDbusProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 289 | size_t cpu, const CpuProperty& newProp) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 290 | { |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 291 | createCpuUpdatedMatch(conn, cpu); |
| 292 | conn->async_method_call( |
| 293 | [](const boost::system::error_code ec) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 294 | if (ec) |
| 295 | { |
| 296 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 297 | "Cannot set CPU property!"); |
| 298 | return; |
| 299 | } |
Patrick Williams | badedf1 | 2023-10-20 11:19:42 -0500 | [diff] [blame] | 300 | }, |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 301 | cpuProcessName, newProp.object.c_str(), |
| 302 | "org.freedesktop.DBus.Properties", "Set", newProp.interface, |
| 303 | newProp.name, std::variant<std::string>{newProp.value}); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Set up a D-Bus match (if one does not already exist) to watch for any new |
| 308 | * interfaces on the cpu object. When new interfaces are added, re-send all |
| 309 | * properties targeting that object/interface. |
| 310 | * |
| 311 | * @param[in,out] conn D-Bus connection. |
| 312 | * @param[in] cpu 1-based CPU index. |
| 313 | */ |
| 314 | static void createCpuUpdatedMatch( |
| 315 | const std::shared_ptr<sdbusplus::asio::connection>& conn, size_t cpu) |
| 316 | { |
| 317 | static boost::container::flat_map<size_t, |
| 318 | std::unique_ptr<sdbusplus::bus::match_t>> |
| 319 | cpuUpdatedMatch; |
| 320 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 321 | if (cpuUpdatedMatch[cpu]) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 322 | { |
| 323 | return; |
| 324 | } |
| 325 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 326 | const std::string objectPath = cpuPath + std::to_string(cpu - 1); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 327 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 328 | cpuUpdatedMatch.insert_or_assign( |
| 329 | cpu, |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 330 | std::make_unique<sdbusplus::bus::match_t>( |
| 331 | static_cast<sdbusplus::bus_t&>(*conn), |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 332 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 333 | sdbusplus::bus::match::rules::argNpath(0, objectPath.c_str()), |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 334 | [conn, cpu](sdbusplus::message_t& msg) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 335 | sdbusplus::message::object_path objectName; |
| 336 | boost::container::flat_map< |
| 337 | std::string, boost::container::flat_map< |
| 338 | std::string, std::variant<std::string, uint64_t>>> |
| 339 | msgData; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 340 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 341 | msg.read(objectName, msgData); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 342 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 343 | // Go through all the property changes, and retry all of them |
| 344 | // targeting this object/interface which was just added. |
| 345 | for (const CpuProperty& prop : propertiesToSet) |
| 346 | { |
| 347 | if (prop.object == objectName && msgData.contains(prop.interface)) |
| 348 | { |
| 349 | setDbusProperty(conn, cpu, prop); |
| 350 | } |
| 351 | } |
Patrick Williams | badedf1 | 2023-10-20 11:19:42 -0500 | [diff] [blame] | 352 | })); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 355 | #if PECI_ENABLED |
| 356 | static void getPPIN(boost::asio::io_service& io, |
| 357 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 358 | const size_t& cpu) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 359 | { |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 360 | if (cpuInfoMap.find(cpu) == cpuInfoMap.end() || cpuInfoMap[cpu] == nullptr) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 361 | { |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 362 | std::cerr << "No information found for cpu " << cpu << "\n"; |
| 363 | return; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 364 | } |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 365 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 366 | std::shared_ptr<CPUInfo> cpuInfo = cpuInfoMap[cpu]; |
| 367 | |
| 368 | if (cpuInfo->id != cpu) |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 369 | { |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 370 | std::cerr << "Incorrect CPU id " << (unsigned)cpuInfo->id << " expect " |
| 371 | << cpu << "\n"; |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Jonathan Doman | 2285be4 | 2021-03-08 14:54:12 -0800 | [diff] [blame] | 375 | uint8_t cpuAddr = cpuInfo->peciAddr; |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 376 | |
| 377 | uint8_t cc = 0; |
| 378 | CPUModel model{}; |
| 379 | uint8_t stepping = 0; |
| 380 | |
Jonathan Doman | 0a38537 | 2021-03-08 17:04:13 -0800 | [diff] [blame] | 381 | // Wait for POST to complete to ensure that BIOS has time to enable the |
| 382 | // PPIN. Before BIOS enables it, we would get a 0x90 CC on PECI. |
| 383 | if (hostState != HostState::postComplete || |
| 384 | peci_GetCPUID(cpuAddr, &model, &stepping, &cc) != PECI_CC_SUCCESS) |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 385 | { |
| 386 | // Start the PECI check loop |
| 387 | auto waitTimer = std::make_shared<boost::asio::steady_timer>(io); |
| 388 | waitTimer->expires_after( |
Jonathan Doman | 0a38537 | 2021-03-08 17:04:13 -0800 | [diff] [blame] | 389 | std::chrono::seconds(cpu_info::peciCheckInterval)); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 390 | |
| 391 | waitTimer->async_wait( |
| 392 | [waitTimer, &io, conn, cpu](const boost::system::error_code& ec) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 393 | if (ec) |
| 394 | { |
| 395 | // operation_aborted is expected if timer is canceled |
| 396 | // before completion. |
| 397 | if (ec != boost::asio::error::operation_aborted) |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 398 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 399 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 400 | "info update timer async_wait failed ", |
| 401 | phosphor::logging::entry("EC=0x%x", ec.value())); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 402 | } |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 403 | return; |
| 404 | } |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 405 | getPPIN(io, conn, cpu); |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 406 | }); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
| 410 | switch (model) |
| 411 | { |
Jason M. Bills | b86e4f1 | 2024-04-01 13:19:19 -0700 | [diff] [blame^] | 412 | case iceLake: |
| 413 | case iceLakeD: |
| 414 | case sapphireRapids: |
| 415 | case emeraldRapids: |
| 416 | case graniteRapids: |
| 417 | case graniteRapidsD: |
| 418 | case sierraForest: |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 419 | { |
| 420 | // PPIN can be read through PCS 19 |
| 421 | static constexpr uint8_t u8Size = 4; // default to a DWORD |
| 422 | static constexpr uint8_t u8PPINPkgIndex = 19; |
| 423 | static constexpr uint16_t u16PPINPkgParamHigh = 2; |
| 424 | static constexpr uint16_t u16PPINPkgParamLow = 1; |
| 425 | uint64_t cpuPPIN = 0; |
| 426 | uint32_t u32PkgValue = 0; |
| 427 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 428 | int ret = peci_RdPkgConfig(cpuAddr, u8PPINPkgIndex, |
| 429 | u16PPINPkgParamLow, u8Size, |
| 430 | (uint8_t*)&u32PkgValue, &cc); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 431 | if (0 != ret) |
| 432 | { |
| 433 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 434 | "peci read package config failed at address", |
| 435 | phosphor::logging::entry("PECIADDR=0x%x", |
| 436 | (unsigned)cpuAddr), |
| 437 | phosphor::logging::entry("CC=0x%x", cc)); |
| 438 | u32PkgValue = 0; |
| 439 | } |
| 440 | |
| 441 | cpuPPIN = u32PkgValue; |
| 442 | ret = peci_RdPkgConfig(cpuAddr, u8PPINPkgIndex, u16PPINPkgParamHigh, |
| 443 | u8Size, (uint8_t*)&u32PkgValue, &cc); |
| 444 | if (0 != ret) |
| 445 | { |
| 446 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 447 | "peci read package config failed at address", |
| 448 | phosphor::logging::entry("PECIADDR=0x%x", |
| 449 | (unsigned)cpuAddr), |
| 450 | phosphor::logging::entry("CC=0x%x", cc)); |
| 451 | cpuPPIN = 0; |
| 452 | u32PkgValue = 0; |
| 453 | } |
| 454 | |
| 455 | cpuPPIN |= static_cast<uint64_t>(u32PkgValue) << 32; |
| 456 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 457 | // set SerialNumber if cpuPPIN is valid |
| 458 | if (0 != cpuPPIN) |
| 459 | { |
| 460 | std::stringstream stream; |
| 461 | stream << std::hex << cpuPPIN; |
| 462 | std::string serialNumber(stream.str()); |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 463 | cpuInfo->publishUUID(*conn, serialNumber); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 464 | } |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | default: |
| 468 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 469 | "in-compatible cpu for cpu asset info"); |
| 470 | break; |
| 471 | } |
| 472 | } |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 473 | #endif |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * Get cpu and pirom address |
| 477 | */ |
| 478 | static void |
| 479 | getCpuAddress(boost::asio::io_service& io, |
| 480 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 481 | const std::string& service, const std::string& object, |
| 482 | const std::string& interface) |
| 483 | { |
| 484 | conn->async_method_call( |
| 485 | [&io, conn](boost::system::error_code ec, |
| 486 | const boost::container::flat_map< |
| 487 | std::string, |
| 488 | std::variant<std::string, uint64_t, uint32_t, uint16_t, |
| 489 | std::vector<std::string>>>& properties) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 490 | const uint64_t* value = nullptr; |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 491 | std::optional<uint8_t> peciAddress; |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 492 | uint8_t i2cBus = defaultI2cBus; |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 493 | std::optional<uint8_t> i2cDevice; |
| 494 | std::optional<size_t> cpu; |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 495 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 496 | if (ec) |
| 497 | { |
| 498 | std::cerr << "DBUS response error " << ec.value() << ": " |
| 499 | << ec.message() << "\n"; |
| 500 | return; |
| 501 | } |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 502 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 503 | for (const auto& property : properties) |
| 504 | { |
| 505 | std::cerr << "property " << property.first << "\n"; |
| 506 | if (property.first == "Address") |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 507 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 508 | value = std::get_if<uint64_t>(&property.second); |
| 509 | if (value != nullptr) |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 510 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 511 | peciAddress = static_cast<uint8_t>(*value); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 514 | if (property.first == "CpuID") |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 515 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 516 | value = std::get_if<uint64_t>(&property.second); |
| 517 | if (value != nullptr) |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 518 | { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 519 | cpu = static_cast<size_t>(*value); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 520 | } |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 521 | } |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 522 | if (property.first == "PiromI2cAddress") |
| 523 | { |
| 524 | value = std::get_if<uint64_t>(&property.second); |
| 525 | if (value != nullptr) |
| 526 | { |
| 527 | i2cDevice = static_cast<uint8_t>(*value); |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | if (property.first == "PiromI2cBus") |
| 531 | { |
| 532 | value = std::get_if<uint64_t>(&property.second); |
| 533 | if (value != nullptr) |
| 534 | { |
| 535 | i2cBus = static_cast<uint8_t>(*value); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 540 | if (!cpu || !peciAddress) |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 541 | { |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 542 | return; |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 543 | } |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 544 | |
| 545 | if (!i2cDevice) |
| 546 | { |
| 547 | i2cDevice = defaultI2cSlaveAddr0 + *cpu - 1; |
| 548 | } |
| 549 | |
| 550 | auto key = cpuInfoMap.find(*cpu); |
| 551 | |
| 552 | if (key != cpuInfoMap.end()) |
| 553 | { |
| 554 | cpuInfoMap.erase(key); |
| 555 | } |
| 556 | |
| 557 | cpuInfoMap.emplace(*cpu, std::make_shared<CPUInfo>(*cpu, *peciAddress, |
| 558 | i2cBus, *i2cDevice)); |
| 559 | |
| 560 | tryReadSSpec(conn, *cpu); |
| 561 | |
| 562 | #if PECI_ENABLED |
| 563 | getPPIN(io, conn, *cpu); |
| 564 | #endif |
Patrick Williams | badedf1 | 2023-10-20 11:19:42 -0500 | [diff] [blame] | 565 | }, |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 566 | service, object, "org.freedesktop.DBus.Properties", "GetAll", |
| 567 | interface); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * D-Bus client: to get platform specific configs |
| 572 | */ |
| 573 | static void getCpuConfiguration( |
| 574 | boost::asio::io_service& io, |
| 575 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 576 | sdbusplus::asio::object_server& objServer) |
| 577 | { |
| 578 | // Get the Cpu configuration |
| 579 | // In case it's not available, set a match for it |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 580 | static std::unique_ptr<sdbusplus::bus::match_t> cpuConfigMatch = |
| 581 | std::make_unique<sdbusplus::bus::match_t>( |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 582 | *conn, |
| 583 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 584 | "PropertiesChanged',arg0='xyz.openbmc_project." |
| 585 | "Configuration.XeonCPU'", |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 586 | [&io, conn, &objServer](sdbusplus::message_t& /* msg */) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 587 | std::cerr << "get cpu configuration match\n"; |
| 588 | static boost::asio::steady_timer filterTimer(io); |
| 589 | filterTimer.expires_after(std::chrono::seconds(configCheckInterval)); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 590 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 591 | filterTimer.async_wait( |
| 592 | [&io, conn, &objServer](const boost::system::error_code& ec) { |
| 593 | if (ec == boost::asio::error::operation_aborted) |
| 594 | { |
| 595 | return; // we're being canceled |
| 596 | } |
| 597 | else if (ec) |
| 598 | { |
| 599 | std::cerr << "Error: " << ec.message() << "\n"; |
| 600 | return; |
| 601 | } |
| 602 | getCpuConfiguration(io, conn, objServer); |
| 603 | }); |
Patrick Williams | badedf1 | 2023-10-20 11:19:42 -0500 | [diff] [blame] | 604 | }); |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 605 | |
| 606 | conn->async_method_call( |
| 607 | [&io, conn]( |
| 608 | boost::system::error_code ec, |
| 609 | const std::vector<std::pair< |
| 610 | std::string, |
| 611 | std::vector<std::pair<std::string, std::vector<std::string>>>>>& |
| 612 | subtree) { |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 613 | if constexpr (debug) |
| 614 | std::cerr << "async_method_call callback\n"; |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 615 | |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 616 | if (ec) |
| 617 | { |
| 618 | std::cerr << "error with async_method_call\n"; |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 619 | return; |
Patrick Williams | c39d3df | 2023-05-10 07:51:14 -0500 | [diff] [blame] | 620 | } |
| 621 | if (subtree.empty()) |
| 622 | { |
| 623 | // No config data yet, so wait for the match |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | for (const auto& object : subtree) |
| 628 | { |
| 629 | for (const auto& service : object.second) |
| 630 | { |
| 631 | getCpuAddress(io, conn, service.first, object.first, |
| 632 | "xyz.openbmc_project.Configuration.XeonCPU"); |
| 633 | } |
| 634 | } |
| 635 | if constexpr (debug) |
| 636 | std::cerr << "getCpuConfiguration callback complete\n"; |
| 637 | |
| 638 | return; |
Patrick Williams | badedf1 | 2023-10-20 11:19:42 -0500 | [diff] [blame] | 639 | }, |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 640 | "xyz.openbmc_project.ObjectMapper", |
| 641 | "/xyz/openbmc_project/object_mapper", |
| 642 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 643 | "/xyz/openbmc_project/", 0, |
| 644 | std::array<const char*, 1>{ |
| 645 | "xyz.openbmc_project.Configuration.XeonCPU"}); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | } // namespace cpu_info |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 649 | |
Jonathan Doman | f2d8bb4 | 2023-07-26 10:13:34 -0700 | [diff] [blame] | 650 | int main() |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 651 | { |
| 652 | // setup connection to dbus |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 653 | boost::asio::io_service& io = cpu_info::dbus::getIOContext(); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 654 | std::shared_ptr<sdbusplus::asio::connection> conn = |
Jonathan Doman | 16a2ced | 2021-11-01 11:13:22 -0700 | [diff] [blame] | 655 | cpu_info::dbus::getConnection(); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 656 | |
| 657 | // CPUInfo Object |
Jonathan Doman | 0a38537 | 2021-03-08 17:04:13 -0800 | [diff] [blame] | 658 | conn->request_name(cpu_info::cpuInfoObject); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 659 | sdbusplus::asio::object_server server = |
| 660 | sdbusplus::asio::object_server(conn); |
Patrick Williams | 77b9c47 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 661 | sdbusplus::bus_t& bus = static_cast<sdbusplus::bus_t&>(*conn); |
| 662 | sdbusplus::server::manager_t objManager(bus, |
| 663 | "/xyz/openbmc_project/inventory"); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 664 | |
Jonathan Doman | 703a185 | 2020-11-11 13:04:02 -0800 | [diff] [blame] | 665 | cpu_info::hostStateSetup(conn); |
| 666 | |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 667 | #if PECI_ENABLED |
Jonathan Doman | 49ea830 | 2022-05-26 14:29:46 -0700 | [diff] [blame] | 668 | cpu_info::sst::init(); |
Jonathan Doman | 4e1cf09 | 2024-03-08 19:57:56 -0800 | [diff] [blame] | 669 | #endif |
Jonathan Doman | 94c94bf | 2020-10-05 23:25:45 -0700 | [diff] [blame] | 670 | |
Zhikui Ren | 6d3ad58 | 2020-09-11 21:25:59 -0700 | [diff] [blame] | 671 | // shared_ptr conn is global for the service |
| 672 | // const reference of conn is passed to async calls |
Jonathan Doman | 0a38537 | 2021-03-08 17:04:13 -0800 | [diff] [blame] | 673 | cpu_info::getCpuConfiguration(io, conn, server); |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 674 | |
| 675 | io.run(); |
| 676 | |
| 677 | return 0; |
| 678 | } |