blob: 4b5df045e5dbf9d6635ebd788c94c5a5b501188e [file] [log] [blame]
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301#include "config.h"
2
Gunnar Mills94df8c92018-09-14 14:50:03 -05003#include "occ_manager.hpp"
4
5#include "i2c_occ.hpp"
Chicago Duanbb895cb2021-06-18 19:37:16 +08006#include "occ_dbus.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05007#include "utils.hpp"
8
Chicago Duanbb895cb2021-06-18 19:37:16 +08009#include <cmath>
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <experimental/filesystem>
11#include <phosphor-logging/elog-errors.hpp>
12#include <phosphor-logging/log.hpp>
Chicago Duanbb895cb2021-06-18 19:37:16 +080013#include <regex>
Gunnar Mills94df8c92018-09-14 14:50:03 -050014#include <xyz/openbmc_project/Common/error.hpp>
15
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053016namespace open_power
17{
18namespace occ
19{
20
Matt Spinler8b8abee2021-08-25 15:18:21 -050021constexpr uint32_t fruTypeNotAvailable = 0xFF;
22
Chris Caina8857c52021-01-27 11:53:05 -060023using namespace phosphor::logging;
24
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053025void Manager::findAndCreateObjects()
26{
Deepak Kodihalli370f06b2017-10-25 04:26:07 -050027 for (auto id = 0; id < MAX_CPUS; ++id)
28 {
Deepak Kodihalli30417a12017-12-04 00:54:01 -060029 // Create one occ per cpu
30 auto occ = std::string(OCC_NAME) + std::to_string(id);
31 createObjects(occ);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053032 }
33}
34
35int Manager::cpuCreated(sdbusplus::message::message& msg)
36{
37 namespace fs = std::experimental::filesystem;
38
39 sdbusplus::message::object_path o;
40 msg.read(o);
41 fs::path cpuPath(std::string(std::move(o)));
42
43 auto name = cpuPath.filename().string();
44 auto index = name.find(CPU_NAME);
45 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
46
47 createObjects(name);
48
49 return 0;
50}
51
52void Manager::createObjects(const std::string& occ)
53{
54 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
55
56 passThroughObjects.emplace_back(
George Liuf3b75142021-06-10 11:22:50 +080057 std::make_unique<PassThrough>(path.c_str()));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053058
Gunnar Mills94df8c92018-09-14 14:50:03 -050059 statusObjects.emplace_back(std::make_unique<Status>(
George Liuf3b75142021-06-10 11:22:50 +080060 event, path.c_str(), *this,
Gunnar Mills94df8c92018-09-14 14:50:03 -050061 std::bind(std::mem_fn(&Manager::statusCallBack), this,
Tom Joseph00325232020-07-29 17:51:48 +053062 std::placeholders::_1)
63#ifdef PLDM
64 ,
65 std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(),
66 std::placeholders::_1)
67#endif
68 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053069
70 // Create the power cap monitor object for master occ (0)
71 if (!pcap)
72 {
73 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
George Liuf3b75142021-06-10 11:22:50 +080074 *statusObjects.front());
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053075 }
Chris Cain78e86012021-03-04 16:15:31 -060076
77#ifdef POWER10
78 // Create the power mode monitor object for master occ (0)
79 if (!pmode)
80 {
81 pmode = std::make_unique<open_power::occ::powermode::PowerMode>(
82 *statusObjects.front());
83 }
84#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053085}
86
87void Manager::statusCallBack(bool status)
88{
Gunnar Mills94df8c92018-09-14 14:50:03 -050089 using InternalFailure =
90 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053091
92 // At this time, it won't happen but keeping it
93 // here just in case something changes in the future
94 if ((activeCount == 0) && (!status))
95 {
96 log<level::ERR>("Invalid update on OCCActive");
97 elog<InternalFailure>();
98 }
99
100 activeCount += status ? 1 : -1;
Eddie Jamesdae2d942017-12-20 10:50:03 -0600101
102 // Only start presence detection if all the OCCs are bound
103 if (activeCount == statusObjects.size())
104 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500105 for (auto& obj : statusObjects)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600106 {
107 obj->addPresenceWatchMaster();
108 }
109 }
Chris Caina8857c52021-01-27 11:53:05 -0600110
111 if ((!_pollTimer->isEnabled()) && (activeCount > 0))
112 {
113 log<level::INFO>(fmt::format("Manager::statusCallBack(): {} OCCs will "
114 "be polled every {} seconds",
115 activeCount, pollInterval)
116 .c_str());
117
118 // Send poll and start OCC poll timer
119 pollerTimerExpired();
120 }
121 else if ((_pollTimer->isEnabled()) && (activeCount == 0))
122 {
123 // Stop OCC poll timer
124 log<level::INFO>("Manager::statusCallBack(): OCCs are not running, "
125 "stopping poll timer");
126 _pollTimer->setEnabled(false);
127 }
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530128}
129
130#ifdef I2C_OCC
131void Manager::initStatusObjects()
132{
133 // Make sure we have a valid path string
134 static_assert(sizeof(DEV_PATH) != 0);
135
136 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
Lei YU41470e52017-11-30 16:03:50 +0800137 auto occMasterName = deviceNames.front();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530138 for (auto& name : deviceNames)
139 {
140 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +0800141 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530142 auto path = fs::path(OCC_CONTROL_ROOT) / name;
143 statusObjects.emplace_back(
George Liuf3b75142021-06-10 11:22:50 +0800144 std::make_unique<Status>(event, path.c_str(), *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530145 }
Lei YU41470e52017-11-30 16:03:50 +0800146 // The first device is master occ
147 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
George Liuf3b75142021-06-10 11:22:50 +0800148 *statusObjects.front(), occMasterName);
Chris Cain78e86012021-03-04 16:15:31 -0600149#ifdef POWER10
150 pmode = std::make_unique<open_power::occ::powermode::PowerMode>(
151 *statusObjects.front());
152#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530153}
154#endif
155
Tom Joseph815f9f52020-07-27 12:12:13 +0530156#ifdef PLDM
157bool Manager::updateOCCActive(instanceID instance, bool status)
158{
159 return (statusObjects[instance])->occActive(status);
160}
161#endif
162
Chris Caina8857c52021-01-27 11:53:05 -0600163void Manager::pollerTimerExpired()
164{
165 if (activeCount == 0)
166 {
167 // No OCCs running, so poll timer will not be restarted
168 log<level::INFO>("Manager::pollerTimerExpire(): No OCCs running, poll "
169 "timer not restarted");
170 }
171
172 if (!_pollTimer)
173 {
174 log<level::ERR>(
175 "Manager::pollerTimerExpired() ERROR: Timer not defined");
176 return;
177 }
178
179 for (auto& obj : statusObjects)
180 {
181 // Read sysfs to force kernel to poll OCC
182 obj->readOccState();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800183
184#ifdef READ_OCC_SENSORS
185 // Read occ sensor values
186 auto id = obj->getOccInstanceID();
187 if (!obj->occActive())
188 {
189 // Occ not activated
190 setSensorValueToNaN(id);
191 continue;
192 }
193 getSensorValues(id, obj->isMasterOcc());
194#endif
Chris Caina8857c52021-01-27 11:53:05 -0600195 }
196
197 // Restart OCC poll timer
198 _pollTimer->restartOnce(std::chrono::seconds(pollInterval));
199}
200
Chicago Duanbb895cb2021-06-18 19:37:16 +0800201#ifdef READ_OCC_SENSORS
202void Manager::readTempSensors(const fs::path& path, uint32_t id)
203{
204 const int open_errno = errno;
205 std::regex expr{"temp\\d+_label$"}; // Example: temp5_label
206 for (auto& file : fs::directory_iterator(path))
207 {
208 if (!std::regex_search(file.path().string(), expr))
209 {
210 continue;
211 }
212 std::ifstream fileOpen(file.path(), std::ios::in);
213 if (!fileOpen)
214 {
215 // If not able to read, OCC may be offline
216 log<level::DEBUG>(
217 fmt::format("readTempSensors: open failed(errno = {}) ",
218 open_errno)
219 .c_str());
220
221 continue;
222 }
223 std::string labelValue;
224 fileOpen >> labelValue;
225 fileOpen.close();
226
227 const std::string& tempLabel = "label";
228 const std::string filePathString = file.path().string().substr(
229 0, file.path().string().length() - tempLabel.length());
230 std::ifstream fruTypeFile(filePathString + "fru_type", std::ios::in);
231 if (!fruTypeFile)
232 {
233 // If not able to read, OCC may be offline
234 log<level::DEBUG>(
235 fmt::format("readTempSensors: open failed(errno = {}) ",
236 open_errno)
237 .c_str());
238 continue;
239 }
240 uint32_t fruTypeValue{0};
241 fruTypeFile >> fruTypeValue;
242 fruTypeFile.close();
243
244 std::string sensorPath =
245 OCC_SENSORS_ROOT + std::string("/temperature/");
246
247 if (fruTypeValue == VRMVdd)
248 {
249 sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp");
250 }
251 else
252 {
253 auto sensorTypeID =
254 open_power::occ::utils::checkLabelValue(labelValue);
255 if (sensorTypeID == std::nullopt)
256 {
257 continue;
258 }
259 auto& [type, instanceID] = *sensorTypeID;
260
261 if (type == OCC_DIMM_TEMP_SENSOR_TYPE)
262 {
Matt Spinler8b8abee2021-08-25 15:18:21 -0500263 if (fruTypeValue == fruTypeNotAvailable)
264 {
265 // Not all DIMM related temps are available to read
266 // (no _input file in this case)
267 continue;
268 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800269 auto iter = dimmTempSensorName.find(fruTypeValue);
270 if (iter == dimmTempSensorName.end())
271 {
272 log<level::ERR>(fmt::format("readTempSensors: Fru type "
273 "error! fruTypeValue = {}) ",
274 fruTypeValue)
275 .c_str());
276 continue;
277 }
278
279 sensorPath.append("dimm" + std::to_string(instanceID) +
280 iter->second);
281 }
282 else if (type == OCC_CPU_TEMP_SENSOR_TYPE)
283 {
284 if (fruTypeValue != processorCore)
285 {
Matt Spinlerabbcfc52021-08-25 15:22:22 -0500286 // TODO: support IO ring temp
Chicago Duanbb895cb2021-06-18 19:37:16 +0800287 continue;
288 }
289
290 sensorPath.append("proc" + std::to_string(id) + "_core" +
291 std::to_string(instanceID) + "_temp");
292 }
293 else
294 {
295 continue;
296 }
297 }
298
299 std::ifstream faultPathFile(filePathString + "fault", std::ios::in);
300 if (faultPathFile)
301 {
302 uint32_t faultValue;
303 faultPathFile >> faultValue;
304 faultPathFile.close();
305
306 if (faultValue != 0)
307 {
308 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
309 sensorPath, std::numeric_limits<double>::quiet_NaN());
310
311 open_power::occ::dbus::OccDBusSensors::getOccDBus()
312 .setOperationalStatus(sensorPath, false);
313
314 continue;
315 }
316 }
317
318 std::ifstream inputFile(filePathString + "input", std::ios::in);
319 if (inputFile)
320 {
321 double tempValue;
322 inputFile >> tempValue;
323
324 inputFile.close();
325
326 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
327 sensorPath, tempValue * std::pow(10, -3));
328
329 open_power::occ::dbus::OccDBusSensors::getOccDBus()
330 .setOperationalStatus(sensorPath, true);
331
332 existingSensors[sensorPath] = id;
333 }
334 else
335 {
336 // If not able to read, OCC may be offline
337 log<level::DEBUG>(
338 fmt::format("readTempSensors: open failed(errno = {}) ",
339 open_errno)
340 .c_str());
341 }
342 }
343 return;
344}
345
346std::optional<std::string>
347 Manager::getPowerLabelFunctionID(const std::string& value)
348{
349 // If the value is "system", then the FunctionID is "system".
350 if (value == "system")
351 {
352 return value;
353 }
354
355 // If the value is not "system", then the label value have 3 numbers, of
356 // which we only care about the middle one:
357 // <sensor id>_<function id>_<apss channel>
358 // eg: The value is "0_10_5" , then the FunctionID is "10".
359 if (value.find("_") == std::string::npos)
360 {
361 return std::nullopt;
362 }
363
364 auto powerLabelValue = value.substr((value.find("_") + 1));
365
366 if (powerLabelValue.find("_") == std::string::npos)
367 {
368 return std::nullopt;
369 }
370
371 return powerLabelValue.substr(0, powerLabelValue.find("_"));
372}
373
374void Manager::readPowerSensors(const fs::path& path, uint32_t id)
375{
376 const int open_errno = errno;
377 std::regex expr{"power\\d+_label$"}; // Example: power5_label
378 for (auto& file : fs::directory_iterator(path))
379 {
380 if (!std::regex_search(file.path().string(), expr))
381 {
382 continue;
383 }
384 std::ifstream fileOpen(file.path(), std::ios::in);
385 if (!fileOpen)
386 {
387 // If not able to read, OCC may be offline
388 log<level::DEBUG>(
389 fmt::format("readPowerSensors: open failed(errno = {}) ",
390 open_errno)
391 .c_str());
392
393 continue;
394 }
395 std::string labelValue;
396 fileOpen >> labelValue;
397 fileOpen.close();
398
399 auto functionID = getPowerLabelFunctionID(labelValue);
400 if (functionID == std::nullopt)
401 {
402 continue;
403 }
404
405 const std::string& tempLabel = "label";
406 const std::string filePathString = file.path().string().substr(
407 0, file.path().string().length() - tempLabel.length());
408
409 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
410
411 auto iter = powerSensorName.find(*functionID);
412 if (iter == powerSensorName.end())
413 {
414 continue;
415 }
416 sensorPath.append(iter->second);
417
418 std::ifstream faultPathFile(filePathString + "fault", std::ios::in);
419 if (faultPathFile)
420 {
421 uint32_t faultValue{0};
422 faultPathFile >> faultValue;
423 faultPathFile.close();
424
425 if (faultValue != 0)
426 {
427 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
428 sensorPath, std::numeric_limits<double>::quiet_NaN());
429
430 open_power::occ::dbus::OccDBusSensors::getOccDBus()
431 .setOperationalStatus(sensorPath, false);
432
433 continue;
434 }
435 }
436
437 std::ifstream inputFile(filePathString + "input", std::ios::in);
438 if (inputFile)
439 {
440 double tempValue;
441 inputFile >> tempValue;
442 inputFile.close();
443
444 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
445 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
446
447 open_power::occ::dbus::OccDBusSensors::getOccDBus()
448 .setOperationalStatus(sensorPath, true);
449
450 existingSensors[sensorPath] = id;
451 }
452 else
453 {
454 // If not able to read, OCC may be offline
455 log<level::DEBUG>(
456 fmt::format("readPowerSensors: open failed(errno = {}) ",
457 open_errno)
458 .c_str());
459 }
460 }
461 return;
462}
463
464void Manager::setSensorValueToNaN(uint32_t id)
465{
466 for (const auto& [sensorPath, occId] : existingSensors)
467 {
468 if (occId == id)
469 {
470 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
471 sensorPath, std::numeric_limits<double>::quiet_NaN());
472 }
473 }
474 return;
475}
476
477void Manager::getSensorValues(uint32_t id, bool masterOcc)
478{
479 const auto occ = std::string("occ-hwmon.") + std::to_string(id + 1);
480
481 fs::path fileName{OCC_HWMON_PATH + occ + "/hwmon/"};
482
483 // Need to get the hwmonXX directory name, there better only be 1 dir
484 assert(std::distance(fs::directory_iterator(fileName),
485 fs::directory_iterator{}) == 1);
486 // Now set our path to this full path, including this hwmonXX directory
487 fileName = fs::path(*fs::directory_iterator(fileName));
488
489 // Read temperature sensors
490 readTempSensors(fileName, id);
491
492 if (masterOcc)
493 {
494 // Read power sensors
495 readPowerSensors(fileName, id);
496 }
497
498 return;
499}
500#endif
501
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530502} // namespace occ
503} // namespace open_power