blob: 6eb60e4e3fd5f6c8952bf07b379bb16b12e51762 [file] [log] [blame]
Zhikui Ren18a5ab92020-09-01 21:35:20 -07001/*
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 Doman703a1852020-11-11 13:04:02 -080018#include "cpuinfo_utils.hpp"
Zhikui Ren18a5ab92020-09-01 21:35:20 -070019
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. Bills1e1ca732022-09-21 12:37:52 -070027#include <boost/container/flat_map.hpp>
Zhikui Ren18a5ab92020-09-01 21:35:20 -070028
Zhikui Ren6d3ad582020-09-11 21:25:59 -070029#include <iostream>
Jonathan Doman2285be42021-03-08 14:54:12 -080030#include <list>
Zhikui Ren18a5ab92020-09-01 21:35:20 -070031#include <optional>
32#include <sstream>
33#include <string>
34
35extern "C"
36{
37#include <i2c/smbus.h>
38#include <linux/i2c-dev.h>
39}
40
Jonathan Doman4e1cf092024-03-08 19:57:56 -080041#if PECI_ENABLED
42#include "speed_select.hpp"
43
Zhikui Ren18a5ab92020-09-01 21:35:20 -070044#include <peci.h>
Jonathan Doman4e1cf092024-03-08 19:57:56 -080045#endif
Zhikui Ren18a5ab92020-09-01 21:35:20 -070046
47#include <phosphor-logging/log.hpp>
48#include <sdbusplus/asio/object_server.hpp>
49
Zhikui Ren18a5ab92020-09-01 21:35:20 -070050namespace cpu_info
51{
Zhikui Ren6d3ad582020-09-11 21:25:59 -070052static constexpr bool debug = false;
Jonathan Doman2285be42021-03-08 14:54:12 -080053static constexpr const char* assetInterfaceName =
Zhikui Ren18a5ab92020-09-01 21:35:20 -070054 "xyz.openbmc_project.Inventory.Decorator.Asset";
55static constexpr const char* cpuProcessName =
56 "xyz.openbmc_project.Smbios.MDR_V2";
57
Zhikui Ren6d3ad582020-09-11 21:25:59 -070058// constants for reading SSPEC or QDF string from PIROM
Jason M. Billsb86e4f12024-04-01 13:19:19 -070059// Currently, they are the same for platforms with Ice Lake
Zhikui Ren6d3ad582020-09-11 21:25:59 -070060static constexpr uint8_t defaultI2cBus = 13;
61static constexpr uint8_t defaultI2cSlaveAddr0 = 0x50;
62static constexpr uint8_t sspecRegAddr = 0xd;
63static constexpr uint8_t sspecSize = 6;
Zhikui Ren18a5ab92020-09-01 21:35:20 -070064
Zhikui Ren6d3ad582020-09-11 21:25:59 -070065using CPUInfoMap = boost::container::flat_map<size_t, std::shared_ptr<CPUInfo>>;
Zhikui Ren18a5ab92020-09-01 21:35:20 -070066
Zhikui Ren6d3ad582020-09-11 21:25:59 -070067static CPUInfoMap cpuInfoMap = {};
Zhikui Ren18a5ab92020-09-01 21:35:20 -070068
Jonathan Doman2285be42021-03-08 14:54:12 -080069/**
70 * Simple aggregate to define an external D-Bus property which needs to be set
71 * by this application.
72 */
73struct 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
Manojkiran Eda0fe13ab2024-06-17 14:55:10 +053083 * around so that if any target objects are removed+re-added, then we can set
84 * the values again.
Jonathan Doman2285be42021-03-08 14:54:12 -080085 */
86static std::list<CpuProperty> propertiesToSet;
87
88static std::ostream& logStream(int cpu)
89{
90 return std::cerr << "[CPU " << cpu << "] ";
91}
92
93static 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);
97static void
98 setDbusProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn,
99 size_t cpu, const CpuProperty& newProp);
100static void createCpuUpdatedMatch(
101 const std::shared_ptr<sdbusplus::asio::connection>& conn, size_t cpu);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700102
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400103static std::optional<std::string>
104 readSSpec(uint8_t bus, uint8_t slaveAddr, uint8_t regAddr, size_t count)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700105{
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 Ren6d3ad582020-09-11 21:25:59 -0700155 value = ::i2c_smbus_read_byte_data(fd, regAddr + i);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700156 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 Ren6d3ad582020-09-11 21:25:59 -0700171 // 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 Ren18a5ab92020-09-01 21:35:20 -0700178 sspec.push_back(static_cast<unsigned char>(value));
179 }
180 ::close(fd);
Jonathan Doman2285be42021-03-08 14:54:12 -0800181
182 if (sspec.size() < 4)
183 {
184 return std::nullopt;
185 }
186
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700187 return sspec;
188}
189
Jonathan Doman2285be42021-03-08 14:54:12 -0800190/**
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 Doman2285be42021-03-08 14:54:12 -0800195 */
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400196static void tryReadSSpec(
197 const std::shared_ptr<sdbusplus::asio::connection>& conn, size_t cpuIndex)
Jonathan Doman2285be42021-03-08 14:54:12 -0800198{
199 static int failedReads = 0;
200
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800201 auto cpuInfoIt = cpuInfoMap.find(cpuIndex);
202 if (cpuInfoIt == cpuInfoMap.end())
203 {
204 return;
205 }
206 auto cpuInfo = cpuInfoIt->second;
207
Jonathan Doman2285be42021-03-08 14:54:12 -0800208 std::optional<std::string> newSSpec =
209 readSSpec(cpuInfo->i2cBus, cpuInfo->i2cDevice, sspecRegAddr, sspecSize);
210 logStream(cpuInfo->id) << "SSpec read status: "
211 << static_cast<bool>(newSSpec) << "\n";
212 if (newSSpec && newSSpec == cpuInfo->sSpec)
213 {
214 setCpuProperty(conn, cpuInfo->id, assetInterfaceName, "Model",
215 *newSSpec);
216 return;
217 }
218
219 // If this read failed, back off for a little longer so that hopefully the
220 // transient condition affecting PIROM reads will pass, but give up after
221 // several consecutive failures. But if this read looked OK, try again
222 // sooner to confirm it.
223 int retrySeconds;
224 if (newSSpec)
225 {
226 retrySeconds = 1;
227 failedReads = 0;
228 cpuInfo->sSpec = *newSSpec;
229 }
230 else
231 {
232 retrySeconds = 5;
233 if (++failedReads > 10)
234 {
235 logStream(cpuInfo->id) << "PIROM Read failed too many times\n";
236 return;
237 }
238 }
239
240 auto sspecTimer = std::make_shared<boost::asio::steady_timer>(
241 conn->get_io_context(), std::chrono::seconds(retrySeconds));
242 sspecTimer->async_wait(
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800243 [sspecTimer, conn, cpuIndex](boost::system::error_code ec) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400244 if (ec)
245 {
246 return;
247 }
248 tryReadSSpec(conn, cpuIndex);
249 });
Jonathan Doman2285be42021-03-08 14:54:12 -0800250}
251
252/**
253 * Add a D-Bus property to the global list, and attempt to set it by calling
254 * `setDbusProperty`.
255 *
256 * @param[in,out] conn D-Bus connection.
257 * @param[in] cpu 1-based CPU index.
258 * @param[in] interface Interface to set.
259 * @param[in] propName Property to set.
260 * @param[in] propVal Value to set.
261 */
262static void
263 setCpuProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn,
264 size_t cpu, const std::string& interface,
265 const std::string& propName, const std::string& propVal)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700266{
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700267 // cpuId from configuration is one based as
268 // dbus object path used by smbios is 0 based
269 const std::string objectPath = cpuPath + std::to_string(cpu - 1);
Jonathan Doman2285be42021-03-08 14:54:12 -0800270
271 // Can switch to emplace_back if you define a CpuProperty constructor.
272 propertiesToSet.push_back(
273 CpuProperty{objectPath, interface, propName, propVal});
274
275 setDbusProperty(conn, cpu, propertiesToSet.back());
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700276}
277
Jonathan Doman2285be42021-03-08 14:54:12 -0800278/**
279 * Set a D-Bus property which is already contained in the global list, and also
280 * setup a D-Bus match to make sure the target property stays correct.
281 *
282 * @param[in,out] conn D-Bus connection.
283 * @param[in] cpu 1-baesd CPU index.
284 * @param[in] newProp Property to set.
285 */
286static void
287 setDbusProperty(const std::shared_ptr<sdbusplus::asio::connection>& conn,
288 size_t cpu, const CpuProperty& newProp)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700289{
Jonathan Doman2285be42021-03-08 14:54:12 -0800290 createCpuUpdatedMatch(conn, cpu);
291 conn->async_method_call(
292 [](const boost::system::error_code ec) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400293 if (ec)
294 {
295 phosphor::logging::log<phosphor::logging::level::ERR>(
296 "Cannot set CPU property!");
297 return;
298 }
299 },
Jonathan Doman2285be42021-03-08 14:54:12 -0800300 cpuProcessName, newProp.object.c_str(),
301 "org.freedesktop.DBus.Properties", "Set", newProp.interface,
302 newProp.name, std::variant<std::string>{newProp.value});
303}
304
305/**
306 * Set up a D-Bus match (if one does not already exist) to watch for any new
307 * interfaces on the cpu object. When new interfaces are added, re-send all
308 * properties targeting that object/interface.
309 *
310 * @param[in,out] conn D-Bus connection.
311 * @param[in] cpu 1-based CPU index.
312 */
313static void createCpuUpdatedMatch(
314 const std::shared_ptr<sdbusplus::asio::connection>& conn, size_t cpu)
315{
316 static boost::container::flat_map<size_t,
317 std::unique_ptr<sdbusplus::bus::match_t>>
318 cpuUpdatedMatch;
319
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700320 if (cpuUpdatedMatch[cpu])
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700321 {
322 return;
323 }
324
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700325 const std::string objectPath = cpuPath + std::to_string(cpu - 1);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700326
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700327 cpuUpdatedMatch.insert_or_assign(
328 cpu,
Patrick Williams77b9c472022-07-22 19:26:57 -0500329 std::make_unique<sdbusplus::bus::match_t>(
330 static_cast<sdbusplus::bus_t&>(*conn),
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700331 sdbusplus::bus::match::rules::interfacesAdded() +
332 sdbusplus::bus::match::rules::argNpath(0, objectPath.c_str()),
Patrick Williams77b9c472022-07-22 19:26:57 -0500333 [conn, cpu](sdbusplus::message_t& msg) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400334 sdbusplus::message::object_path objectName;
335 boost::container::flat_map<
336 std::string,
337 boost::container::flat_map<
338 std::string, std::variant<std::string, uint64_t>>>
339 msgData;
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700340
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400341 msg.read(objectName, msgData);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700342
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400343 // 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 &&
348 msgData.contains(prop.interface))
349 {
350 setDbusProperty(conn, cpu, prop);
351 }
352 }
353 }));
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700354}
355
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800356#if PECI_ENABLED
357static void getPPIN(boost::asio::io_service& io,
358 const std::shared_ptr<sdbusplus::asio::connection>& conn,
359 const size_t& cpu)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700360{
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700361 if (cpuInfoMap.find(cpu) == cpuInfoMap.end() || cpuInfoMap[cpu] == nullptr)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700362 {
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700363 std::cerr << "No information found for cpu " << cpu << "\n";
364 return;
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700365 }
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700366
Jonathan Doman2285be42021-03-08 14:54:12 -0800367 std::shared_ptr<CPUInfo> cpuInfo = cpuInfoMap[cpu];
368
369 if (cpuInfo->id != cpu)
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700370 {
Jonathan Doman2285be42021-03-08 14:54:12 -0800371 std::cerr << "Incorrect CPU id " << (unsigned)cpuInfo->id << " expect "
372 << cpu << "\n";
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700373 return;
374 }
375
Jonathan Doman2285be42021-03-08 14:54:12 -0800376 uint8_t cpuAddr = cpuInfo->peciAddr;
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700377
378 uint8_t cc = 0;
379 CPUModel model{};
380 uint8_t stepping = 0;
381
Jonathan Doman0a385372021-03-08 17:04:13 -0800382 // Wait for POST to complete to ensure that BIOS has time to enable the
383 // PPIN. Before BIOS enables it, we would get a 0x90 CC on PECI.
384 if (hostState != HostState::postComplete ||
385 peci_GetCPUID(cpuAddr, &model, &stepping, &cc) != PECI_CC_SUCCESS)
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700386 {
387 // Start the PECI check loop
388 auto waitTimer = std::make_shared<boost::asio::steady_timer>(io);
389 waitTimer->expires_after(
Jonathan Doman0a385372021-03-08 17:04:13 -0800390 std::chrono::seconds(cpu_info::peciCheckInterval));
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700391
392 waitTimer->async_wait(
393 [waitTimer, &io, conn, cpu](const boost::system::error_code& ec) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400394 if (ec)
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700395 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400396 // operation_aborted is expected if timer is canceled
397 // before completion.
398 if (ec != boost::asio::error::operation_aborted)
399 {
400 phosphor::logging::log<phosphor::logging::level::ERR>(
401 "info update timer async_wait failed ",
402 phosphor::logging::entry("EC=0x%x", ec.value()));
403 }
404 return;
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700405 }
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400406 getPPIN(io, conn, cpu);
407 });
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700408 return;
409 }
410
411 switch (model)
412 {
Jason M. Billsb86e4f12024-04-01 13:19:19 -0700413 case iceLake:
414 case iceLakeD:
415 case sapphireRapids:
416 case emeraldRapids:
417 case graniteRapids:
418 case graniteRapidsD:
419 case sierraForest:
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700420 {
421 // PPIN can be read through PCS 19
422 static constexpr uint8_t u8Size = 4; // default to a DWORD
423 static constexpr uint8_t u8PPINPkgIndex = 19;
424 static constexpr uint16_t u16PPINPkgParamHigh = 2;
425 static constexpr uint16_t u16PPINPkgParamLow = 1;
426 uint64_t cpuPPIN = 0;
427 uint32_t u32PkgValue = 0;
428
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400429 int ret =
430 peci_RdPkgConfig(cpuAddr, u8PPINPkgIndex, u16PPINPkgParamLow,
431 u8Size, (uint8_t*)&u32PkgValue, &cc);
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700432 if (0 != ret)
433 {
434 phosphor::logging::log<phosphor::logging::level::ERR>(
435 "peci read package config failed at address",
436 phosphor::logging::entry("PECIADDR=0x%x",
437 (unsigned)cpuAddr),
438 phosphor::logging::entry("CC=0x%x", cc));
439 u32PkgValue = 0;
440 }
441
442 cpuPPIN = u32PkgValue;
443 ret = peci_RdPkgConfig(cpuAddr, u8PPINPkgIndex, u16PPINPkgParamHigh,
444 u8Size, (uint8_t*)&u32PkgValue, &cc);
445 if (0 != ret)
446 {
447 phosphor::logging::log<phosphor::logging::level::ERR>(
448 "peci read package config failed at address",
449 phosphor::logging::entry("PECIADDR=0x%x",
450 (unsigned)cpuAddr),
451 phosphor::logging::entry("CC=0x%x", cc));
452 cpuPPIN = 0;
453 u32PkgValue = 0;
454 }
455
456 cpuPPIN |= static_cast<uint64_t>(u32PkgValue) << 32;
457
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700458 // set SerialNumber if cpuPPIN is valid
459 if (0 != cpuPPIN)
460 {
461 std::stringstream stream;
462 stream << std::hex << cpuPPIN;
463 std::string serialNumber(stream.str());
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800464 cpuInfo->publishUUID(*conn, serialNumber);
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700465 }
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700466 break;
467 }
468 default:
469 phosphor::logging::log<phosphor::logging::level::INFO>(
470 "in-compatible cpu for cpu asset info");
471 break;
472 }
473}
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800474#endif
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700475
476/**
477 * Get cpu and pirom address
478 */
479static void
480 getCpuAddress(boost::asio::io_service& io,
481 const std::shared_ptr<sdbusplus::asio::connection>& conn,
482 const std::string& service, const std::string& object,
483 const std::string& interface)
484{
485 conn->async_method_call(
486 [&io, conn](boost::system::error_code ec,
487 const boost::container::flat_map<
488 std::string,
489 std::variant<std::string, uint64_t, uint32_t, uint16_t,
490 std::vector<std::string>>>& properties) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400491 const uint64_t* value = nullptr;
492 std::optional<uint8_t> peciAddress;
493 uint8_t i2cBus = defaultI2cBus;
494 std::optional<uint8_t> i2cDevice;
495 std::optional<size_t> cpu;
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700496
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400497 if (ec)
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700498 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400499 std::cerr << "DBUS response error " << ec.value() << ": "
500 << ec.message() << "\n";
501 return;
502 }
503
504 for (const auto& property : properties)
505 {
506 std::cerr << "property " << property.first << "\n";
507 if (property.first == "Address")
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700508 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400509 value = std::get_if<uint64_t>(&property.second);
510 if (value != nullptr)
511 {
512 peciAddress = static_cast<uint8_t>(*value);
513 }
514 }
515 if (property.first == "CpuID")
516 {
517 value = std::get_if<uint64_t>(&property.second);
518 if (value != nullptr)
519 {
520 cpu = static_cast<size_t>(*value);
521 }
522 }
523 if (property.first == "PiromI2cAddress")
524 {
525 value = std::get_if<uint64_t>(&property.second);
526 if (value != nullptr)
527 {
528 i2cDevice = static_cast<uint8_t>(*value);
529 }
530 }
531 if (property.first == "PiromI2cBus")
532 {
533 value = std::get_if<uint64_t>(&property.second);
534 if (value != nullptr)
535 {
536 i2cBus = static_cast<uint8_t>(*value);
537 }
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700538 }
539 }
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400540
541 if (!cpu || !peciAddress)
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700542 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400543 return;
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700544 }
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400545
546 if (!i2cDevice)
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500547 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400548 i2cDevice = defaultI2cSlaveAddr0 + *cpu - 1;
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500549 }
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400550
551 auto key = cpuInfoMap.find(*cpu);
552
553 if (key != cpuInfoMap.end())
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500554 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400555 cpuInfoMap.erase(key);
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500556 }
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500557
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400558 cpuInfoMap.emplace(*cpu,
559 std::make_shared<CPUInfo>(*cpu, *peciAddress,
560 i2cBus, *i2cDevice));
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800561
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400562 tryReadSSpec(conn, *cpu);
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800563
564#if PECI_ENABLED
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400565 getPPIN(io, conn, *cpu);
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800566#endif
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400567 },
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700568 service, object, "org.freedesktop.DBus.Properties", "GetAll",
569 interface);
570}
571
572/**
573 * D-Bus client: to get platform specific configs
574 */
575static void getCpuConfiguration(
576 boost::asio::io_service& io,
577 const std::shared_ptr<sdbusplus::asio::connection>& conn,
578 sdbusplus::asio::object_server& objServer)
579{
580 // Get the Cpu configuration
581 // In case it's not available, set a match for it
Patrick Williams77b9c472022-07-22 19:26:57 -0500582 static std::unique_ptr<sdbusplus::bus::match_t> cpuConfigMatch =
583 std::make_unique<sdbusplus::bus::match_t>(
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700584 *conn,
585 "type='signal',interface='org.freedesktop.DBus.Properties',member='"
586 "PropertiesChanged',arg0='xyz.openbmc_project."
587 "Configuration.XeonCPU'",
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700588 [&io, conn, &objServer](sdbusplus::message_t& /* msg */) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400589 std::cerr << "get cpu configuration match\n";
590 static boost::asio::steady_timer filterTimer(io);
591 filterTimer.expires_after(
592 std::chrono::seconds(configCheckInterval));
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700593
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400594 filterTimer.async_wait(
595 [&io, conn,
596 &objServer](const boost::system::error_code& ec) {
597 if (ec == boost::asio::error::operation_aborted)
598 {
599 return; // we're being canceled
600 }
601 else if (ec)
602 {
603 std::cerr << "Error: " << ec.message() << "\n";
604 return;
605 }
606 getCpuConfiguration(io, conn, objServer);
607 });
608 });
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700609
610 conn->async_method_call(
611 [&io, conn](
612 boost::system::error_code ec,
613 const std::vector<std::pair<
614 std::string,
615 std::vector<std::pair<std::string, std::vector<std::string>>>>>&
616 subtree) {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400617 if constexpr (debug)
618 std::cerr << "async_method_call callback\n";
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700619
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400620 if (ec)
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500621 {
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400622 std::cerr << "error with async_method_call\n";
623 return;
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500624 }
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400625 if (subtree.empty())
626 {
627 // No config data yet, so wait for the match
628 return;
629 }
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500630
Patrick Williams1d73dcc2024-08-16 15:21:42 -0400631 for (const auto& object : subtree)
632 {
633 for (const auto& service : object.second)
634 {
635 getCpuAddress(io, conn, service.first, object.first,
636 "xyz.openbmc_project.Configuration.XeonCPU");
637 }
638 }
639 if constexpr (debug)
640 std::cerr << "getCpuConfiguration callback complete\n";
641
642 return;
643 },
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700644 "xyz.openbmc_project.ObjectMapper",
645 "/xyz/openbmc_project/object_mapper",
646 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
647 "/xyz/openbmc_project/", 0,
648 std::array<const char*, 1>{
649 "xyz.openbmc_project.Configuration.XeonCPU"});
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700650}
651
652} // namespace cpu_info
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700653
Jonathan Domanf2d8bb42023-07-26 10:13:34 -0700654int main()
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700655{
656 // setup connection to dbus
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700657 boost::asio::io_service& io = cpu_info::dbus::getIOContext();
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700658 std::shared_ptr<sdbusplus::asio::connection> conn =
Jonathan Doman16a2ced2021-11-01 11:13:22 -0700659 cpu_info::dbus::getConnection();
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700660
661 // CPUInfo Object
Jonathan Doman0a385372021-03-08 17:04:13 -0800662 conn->request_name(cpu_info::cpuInfoObject);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700663 sdbusplus::asio::object_server server =
664 sdbusplus::asio::object_server(conn);
Patrick Williams77b9c472022-07-22 19:26:57 -0500665 sdbusplus::bus_t& bus = static_cast<sdbusplus::bus_t&>(*conn);
666 sdbusplus::server::manager_t objManager(bus,
667 "/xyz/openbmc_project/inventory");
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700668
Jonathan Doman703a1852020-11-11 13:04:02 -0800669 cpu_info::hostStateSetup(conn);
670
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800671#if PECI_ENABLED
Jonathan Doman49ea8302022-05-26 14:29:46 -0700672 cpu_info::sst::init();
Jonathan Doman4e1cf092024-03-08 19:57:56 -0800673#endif
Jonathan Doman94c94bf2020-10-05 23:25:45 -0700674
Zhikui Ren6d3ad582020-09-11 21:25:59 -0700675 // shared_ptr conn is global for the service
676 // const reference of conn is passed to async calls
Jonathan Doman0a385372021-03-08 17:04:13 -0800677 cpu_info::getCpuConfiguration(io, conn, server);
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700678
679 io.run();
680
681 return 0;
682}