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" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | |
| 24 | #include <boost/asio/io_service.hpp> |
| 25 | #include <boost/asio/steady_timer.hpp> |
| 26 | |
| 27 | #include <optional> |
| 28 | #include <sstream> |
| 29 | #include <string> |
| 30 | |
| 31 | extern "C" |
| 32 | { |
| 33 | #include <i2c/smbus.h> |
| 34 | #include <linux/i2c-dev.h> |
| 35 | } |
| 36 | |
| 37 | #include <peci.h> |
| 38 | |
| 39 | #include <phosphor-logging/log.hpp> |
| 40 | #include <sdbusplus/asio/object_server.hpp> |
| 41 | |
| 42 | namespace phosphor |
| 43 | { |
| 44 | namespace cpu_info |
| 45 | { |
| 46 | |
| 47 | static constexpr const char* cpuPath = |
| 48 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu"; |
| 49 | static constexpr const char* cpuInterfaceName = |
| 50 | "xyz.openbmc_project.Inventory.Decorator.Asset"; |
| 51 | static constexpr const char* cpuProcessName = |
| 52 | "xyz.openbmc_project.Smbios.MDR_V2"; |
| 53 | |
| 54 | struct ProcessorInfo |
| 55 | { |
| 56 | uint64_t ppin; |
| 57 | std::string sspec; |
| 58 | }; |
| 59 | |
| 60 | using CPUMap = |
| 61 | boost::container::flat_map<size_t, |
| 62 | std::pair<int, std::shared_ptr<CPUInfo>>>; |
| 63 | |
| 64 | static CPUMap cpuMap = {}; |
| 65 | |
| 66 | static std::unique_ptr<sdbusplus::bus::match_t> cpuUpdatedMatch = nullptr; |
| 67 | |
| 68 | static std::optional<std::string> readSSpec(uint8_t bus, uint8_t slaveAddr, |
| 69 | uint8_t regAddr, size_t count) |
| 70 | { |
| 71 | unsigned long funcs = 0; |
| 72 | std::string devPath = "/dev/i2c-" + std::to_string(bus); |
| 73 | |
| 74 | int fd = ::open(devPath.c_str(), O_RDWR); |
| 75 | if (fd < 0) |
| 76 | { |
| 77 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 78 | "Error in open!", |
| 79 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 80 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 81 | return std::nullopt; |
| 82 | } |
| 83 | |
| 84 | if (::ioctl(fd, I2C_FUNCS, &funcs) < 0) |
| 85 | { |
| 86 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 87 | "Error in I2C_FUNCS!", |
| 88 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 89 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 90 | ::close(fd); |
| 91 | return std::nullopt; |
| 92 | } |
| 93 | |
| 94 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 95 | { |
| 96 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 97 | "i2c bus does not support read!", |
| 98 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 99 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 100 | ::close(fd); |
| 101 | return std::nullopt; |
| 102 | } |
| 103 | |
| 104 | if (::ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0) |
| 105 | { |
| 106 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 107 | "Error in I2C_SLAVE_FORCE!", |
| 108 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 109 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 110 | ::close(fd); |
| 111 | return std::nullopt; |
| 112 | } |
| 113 | |
| 114 | int value = 0; |
| 115 | std::string sspec; |
| 116 | sspec.reserve(count); |
| 117 | |
| 118 | for (size_t i = 0; i < count; i++) |
| 119 | { |
| 120 | value = ::i2c_smbus_read_byte_data(fd, regAddr++); |
| 121 | if (value < 0) |
| 122 | { |
| 123 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 124 | "Error in i2c read!", |
| 125 | phosphor::logging::entry("PATH=%s", devPath.c_str()), |
| 126 | phosphor::logging::entry("SLAVEADDR=0x%x", slaveAddr)); |
| 127 | ::close(fd); |
| 128 | return std::nullopt; |
| 129 | } |
| 130 | if (!std::isprint(static_cast<unsigned char>(value))) |
| 131 | { |
| 132 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 133 | "Non printable value in sspec, ignored."); |
| 134 | continue; |
| 135 | } |
| 136 | sspec.push_back(static_cast<unsigned char>(value)); |
| 137 | } |
| 138 | ::close(fd); |
| 139 | return sspec; |
| 140 | } |
| 141 | |
| 142 | // PECI Client Address Map |
| 143 | static void getPECIAddrMap(CPUMap& cpuMap) |
| 144 | { |
| 145 | int idx = 0; |
| 146 | for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++) |
| 147 | { |
| 148 | if (peci_Ping(i) == PECI_CC_SUCCESS) |
| 149 | { |
| 150 | cpuMap.emplace(std::make_pair(i, std::make_pair(idx, nullptr))); |
| 151 | idx++; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static std::shared_ptr<CPUInfo> |
| 157 | createCPUInfo(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 158 | const int& cpu) |
| 159 | { |
| 160 | std::string path = cpuPath + std::to_string(cpu); |
| 161 | std::shared_ptr<CPUInfo> cpuInfo = std::make_shared<CPUInfo>( |
| 162 | static_cast<sdbusplus::bus::bus&>(*conn), path); |
| 163 | return cpuInfo; |
| 164 | } |
| 165 | |
| 166 | static void setAssetProperty( |
| 167 | std::shared_ptr<sdbusplus::asio::connection>& conn, const int& cpu, |
| 168 | const std::vector<std::pair<std::string, std::string>>& propValues) |
| 169 | { |
| 170 | |
| 171 | const std::string objectPath = cpuPath + std::to_string(cpu); |
| 172 | for (const auto& prop : propValues) |
| 173 | { |
| 174 | conn->async_method_call( |
| 175 | [](const boost::system::error_code ec) { |
| 176 | // Use "Set" method to set the property value. |
| 177 | if (ec) |
| 178 | { |
| 179 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 180 | "Cannot get CPU property!"); |
| 181 | return; |
| 182 | } |
| 183 | }, |
| 184 | cpuProcessName, objectPath.c_str(), |
| 185 | "org.freedesktop.DBus.Properties", "Set", cpuInterfaceName, |
| 186 | prop.first.c_str(), std::variant<std::string>{prop.second}); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void createCpuUpdatedMatch( |
| 191 | std::shared_ptr<sdbusplus::asio::connection>& conn, const int& cpu, |
| 192 | const std::vector<std::pair<std::string, std::string>>& propValues) |
| 193 | { |
| 194 | if (cpuUpdatedMatch) |
| 195 | { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | const std::string objectPath = cpuPath + std::to_string(cpu); |
| 200 | |
| 201 | cpuUpdatedMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 202 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 203 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 204 | sdbusplus::bus::match::rules::argNpath(0, objectPath.c_str()), |
| 205 | [&conn, cpu, propValues](sdbusplus::message::message& msg) { |
Jonathan Doman | a43eec8 | 2020-10-06 09:23:09 -0700 | [diff] [blame] | 206 | sdbusplus::message::object_path objectName; |
Zhikui Ren | 18a5ab9 | 2020-09-01 21:35:20 -0700 | [diff] [blame] | 207 | boost::container::flat_map< |
| 208 | std::string, |
| 209 | boost::container::flat_map<std::string, |
| 210 | std::variant<std::string, uint64_t>>> |
| 211 | msgData; |
| 212 | |
| 213 | msg.read(objectName, msgData); |
| 214 | |
| 215 | // Check for xyz.openbmc_project.Inventory.Item.Cpu |
| 216 | // interface match |
| 217 | auto intfFound = msgData.find(cpuInterfaceName); |
| 218 | if (msgData.end() != intfFound) |
| 219 | { |
| 220 | setAssetProperty(conn, cpu, propValues); |
| 221 | } |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | // constants for reading QDF string from PIROM |
| 226 | // Currently, they are the same for platforms with icx |
| 227 | // \todo: move into configuration file to be more robust |
| 228 | static constexpr uint8_t i2cBus = 13; |
| 229 | static constexpr uint8_t slaveAddr0 = 0x50; |
| 230 | static constexpr uint8_t regAddr = 0xf; |
| 231 | static constexpr uint8_t sspecSize = 4; |
| 232 | |
| 233 | static void getProcessorInfo(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 234 | sdbusplus::asio::object_server& objServer, |
| 235 | CPUMap& cpuMap) |
| 236 | { |
| 237 | |
| 238 | for (auto& cpu : cpuMap) |
| 239 | { |
| 240 | uint8_t cc = 0; |
| 241 | CPUModel model{}; |
| 242 | uint8_t stepping = 0; |
| 243 | |
| 244 | /// \todo in a follwup patch |
| 245 | // CPUInfo will be updated as the centrol place for CPU information |
| 246 | // std::shared_ptr<CPUInfo> cpuInfo = |
| 247 | // createCPUInfo(conn, cpu.second.first); |
| 248 | // cpu.second.second = cpuInfo; |
| 249 | |
| 250 | if (peci_GetCPUID(cpu.first, &model, &stepping, &cc) != PECI_CC_SUCCESS) |
| 251 | { |
| 252 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 253 | "Cannot get CPUID!", |
| 254 | phosphor::logging::entry("PECIADDR=0x%x", cpu.first)); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | switch (model) |
| 259 | { |
| 260 | case icx: |
| 261 | { |
| 262 | // get processor ID |
| 263 | static constexpr uint8_t u8Size = 4; // default to a DWORD |
| 264 | static constexpr uint8_t u8PPINPkgIndex = 19; |
| 265 | static constexpr uint16_t u16PPINPkgParamHigh = 2; |
| 266 | static constexpr uint16_t u16PPINPkgParamLow = 1; |
| 267 | uint64_t cpuPPIN = 0; |
| 268 | uint32_t u32PkgValue = 0; |
| 269 | |
| 270 | int ret = peci_RdPkgConfig(cpu.first, u8PPINPkgIndex, |
| 271 | u16PPINPkgParamLow, u8Size, |
| 272 | (uint8_t*)&u32PkgValue, &cc); |
| 273 | if (0 != ret) |
| 274 | { |
| 275 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 276 | "peci read package config failed at address", |
| 277 | phosphor::logging::entry("PECIADDR=0x%x", cpu.first), |
| 278 | phosphor::logging::entry("CC=0x%x", cc)); |
| 279 | u32PkgValue = 0; |
| 280 | } |
| 281 | |
| 282 | cpuPPIN = u32PkgValue; |
| 283 | ret = peci_RdPkgConfig(cpu.first, u8PPINPkgIndex, |
| 284 | u16PPINPkgParamHigh, u8Size, |
| 285 | (uint8_t*)&u32PkgValue, &cc); |
| 286 | if (0 != ret) |
| 287 | { |
| 288 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 289 | "peci read package config failed at address", |
| 290 | phosphor::logging::entry("PECIADDR=0x%x", cpu.first), |
| 291 | phosphor::logging::entry("CC=0x%x", cc)); |
| 292 | cpuPPIN = 0; |
| 293 | u32PkgValue = 0; |
| 294 | } |
| 295 | |
| 296 | cpuPPIN |= static_cast<uint64_t>(u32PkgValue) << 32; |
| 297 | |
| 298 | std::vector<std::pair<std::string, std::string>> values; |
| 299 | |
| 300 | // set SerialNumber if cpuPPIN is valid |
| 301 | if (0 != cpuPPIN) |
| 302 | { |
| 303 | std::stringstream stream; |
| 304 | stream << std::hex << cpuPPIN; |
| 305 | std::string serialNumber(stream.str()); |
| 306 | // cpuInfo->serialNumber(serialNumber); |
| 307 | values.emplace_back( |
| 308 | std::make_pair("SerialNumber", serialNumber)); |
| 309 | } |
| 310 | |
| 311 | // assuming the slaveAddress will be incrementing like peci |
| 312 | // client address |
| 313 | std::optional<std::string> sspec = readSSpec( |
| 314 | i2cBus, static_cast<uint8_t>(slaveAddr0 + cpu.second.first), |
| 315 | regAddr, sspecSize); |
| 316 | // cpuInfo->model(sspec.value_or("")); |
| 317 | values.emplace_back( |
| 318 | std::make_pair("Model", sspec.value_or(""))); |
| 319 | |
| 320 | /// \todo in followup patch |
| 321 | // CPUInfo is created by this service |
| 322 | // update the below logic, which is needed because smbios |
| 323 | // service creates the cpu object |
| 324 | createCpuUpdatedMatch(conn, cpu.second.first, values); |
| 325 | setAssetProperty(conn, cpu.second.first, values); |
| 326 | break; |
| 327 | } |
| 328 | default: |
| 329 | phosphor::logging::log<phosphor::logging::level::INFO>( |
| 330 | "in-compatible cpu for cpu asset info"); |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static bool isPECIAvailable(void) |
| 337 | { |
| 338 | for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++) |
| 339 | { |
| 340 | if (peci_Ping(i) == PECI_CC_SUCCESS) |
| 341 | { |
| 342 | return true; |
| 343 | } |
| 344 | } |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer, |
| 350 | boost::asio::io_service& io, |
| 351 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 352 | sdbusplus::asio::object_server& objServer) |
| 353 | { |
| 354 | bool peciAvailable = isPECIAvailable(); |
| 355 | if (peciAvailable) |
| 356 | { |
| 357 | // get the PECI client address list |
| 358 | getPECIAddrMap(cpuMap); |
| 359 | getProcessorInfo(conn, objServer, cpuMap); |
| 360 | } |
| 361 | if (!peciAvailable || !cpuMap.size()) |
| 362 | { |
| 363 | peciWaitTimer.expires_after( |
| 364 | std::chrono::seconds(6 * peciCheckInterval)); |
| 365 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &conn, &objServer]( |
| 366 | const boost::system::error_code& ec) { |
| 367 | if (ec) |
| 368 | { |
| 369 | // operation_aborted is expected if timer is canceled |
| 370 | // before completion. |
| 371 | if (ec != boost::asio::error::operation_aborted) |
| 372 | { |
| 373 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 374 | "PECI Available Check async_wait failed", |
| 375 | phosphor::logging::entry("EC=0x%x", ec.value())); |
| 376 | } |
| 377 | return; |
| 378 | } |
| 379 | peciAvailableCheck(peciWaitTimer, io, conn, objServer); |
| 380 | }); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | } // namespace cpu_info |
| 385 | } // namespace phosphor |
| 386 | |
| 387 | int main(int argc, char* argv[]) |
| 388 | { |
| 389 | // setup connection to dbus |
| 390 | boost::asio::io_service io; |
| 391 | std::shared_ptr<sdbusplus::asio::connection> conn = |
| 392 | std::make_shared<sdbusplus::asio::connection>(io); |
| 393 | |
| 394 | // CPUInfo Object |
| 395 | conn->request_name(phosphor::cpu_info::cpuInfoObject); |
| 396 | sdbusplus::asio::object_server server = |
| 397 | sdbusplus::asio::object_server(conn); |
| 398 | sdbusplus::bus::bus& bus = static_cast<sdbusplus::bus::bus&>(*conn); |
| 399 | sdbusplus::server::manager::manager objManager( |
| 400 | bus, "/xyz/openbmc_project/inventory"); |
| 401 | |
| 402 | // Start the PECI check loop |
| 403 | boost::asio::steady_timer peciWaitTimer( |
| 404 | io, std::chrono::seconds(phosphor::cpu_info::peciCheckInterval)); |
| 405 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &conn, |
| 406 | &server](const boost::system::error_code& ec) { |
| 407 | if (ec) |
| 408 | { |
| 409 | // operation_aborted is expected if timer is canceled |
| 410 | // before completion. |
| 411 | if (ec != boost::asio::error::operation_aborted) |
| 412 | { |
| 413 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 414 | "PECI Available Check async_wait failed ", |
| 415 | phosphor::logging::entry("EC=0x%x", ec.value())); |
| 416 | } |
| 417 | return; |
| 418 | } |
| 419 | phosphor::cpu_info::peciAvailableCheck(peciWaitTimer, io, conn, server); |
| 420 | }); |
| 421 | |
| 422 | io.run(); |
| 423 | |
| 424 | return 0; |
| 425 | } |