blob: ab86d4be880eaaf5371c36cd5812bc5b2af07b6f [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
George Liub5ca1012021-09-10 12:53:11 +08009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
Matt Spinlerd267cec2021-09-01 14:49:19 -050013#include <chrono>
Chicago Duanbb895cb2021-06-18 19:37:16 +080014#include <cmath>
George Liubcef3b42021-09-10 12:39:02 +080015#include <filesystem>
Chris Cain36f9cde2021-11-22 11:18:21 -060016#include <fstream>
Chicago Duanbb895cb2021-06-18 19:37:16 +080017#include <regex>
Gunnar Mills94df8c92018-09-14 14:50:03 -050018
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053019namespace open_power
20{
21namespace occ
22{
23
Matt Spinler8b8abee2021-08-25 15:18:21 -050024constexpr uint32_t fruTypeNotAvailable = 0xFF;
Matt Spinlera26f1522021-08-25 15:50:20 -050025constexpr auto fruTypeSuffix = "fru_type";
26constexpr auto faultSuffix = "fault";
27constexpr auto inputSuffix = "input";
Matt Spinlerace67d82021-10-18 13:41:57 -050028constexpr auto maxSuffix = "max";
Matt Spinler8b8abee2021-08-25 15:18:21 -050029
Chris Cain1718fd82022-02-16 16:39:50 -060030const auto HOST_ON_FILE = "/run/openbmc/host@0-on";
31
Chris Caina8857c52021-01-27 11:53:05 -060032using namespace phosphor::logging;
Chris Caina7b74dc2021-11-10 17:03:43 -060033using namespace std::literals::chrono_literals;
Chris Caina8857c52021-01-27 11:53:05 -060034
Matt Spinlera26f1522021-08-25 15:50:20 -050035template <typename T>
36T readFile(const std::string& path)
37{
38 std::ifstream ifs;
39 ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit |
40 std::ifstream::eofbit);
41 T data;
42
43 try
44 {
45 ifs.open(path);
46 ifs >> data;
47 ifs.close();
48 }
49 catch (const std::exception& e)
50 {
51 auto err = errno;
52 throw std::system_error(err, std::generic_category());
53 }
54
55 return data;
56}
57
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053058void Manager::findAndCreateObjects()
59{
Matt Spinlerd267cec2021-09-01 14:49:19 -050060#ifndef POWER10
Deepak Kodihalli370f06b2017-10-25 04:26:07 -050061 for (auto id = 0; id < MAX_CPUS; ++id)
62 {
Deepak Kodihalli30417a12017-12-04 00:54:01 -060063 // Create one occ per cpu
64 auto occ = std::string(OCC_NAME) + std::to_string(id);
65 createObjects(occ);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053066 }
Matt Spinlerd267cec2021-09-01 14:49:19 -050067#else
Chris Cain1718fd82022-02-16 16:39:50 -060068 if (!fs::exists(HOST_ON_FILE))
Matt Spinlerd267cec2021-09-01 14:49:19 -050069 {
Chris Cain1718fd82022-02-16 16:39:50 -060070 // Create the OCCs based on on the /dev/occX devices
71 auto occs = findOCCsInDev();
Matt Spinlerd267cec2021-09-01 14:49:19 -050072
Chris Cain1718fd82022-02-16 16:39:50 -060073 if (occs.empty() || (prevOCCSearch.size() != occs.size()))
74 {
75 // Something changed or no OCCs yet, try again in 10s.
76 // Note on the first pass prevOCCSearch will be empty,
77 // so there will be at least one delay to give things
78 // a chance to settle.
79 prevOCCSearch = occs;
80
81 discoverTimer->restartOnce(10s);
82 }
83 else
84 {
85 discoverTimer.reset();
86
87 // createObjects requires OCC0 first.
88 std::sort(occs.begin(), occs.end());
89
90 for (auto id : occs)
91 {
92 createObjects(std::string(OCC_NAME) + std::to_string(id));
93 }
94 }
Matt Spinlerd267cec2021-09-01 14:49:19 -050095 }
96 else
97 {
Chris Cain1718fd82022-02-16 16:39:50 -060098 log<level::INFO>(
99 fmt::format(
100 "Manager::findAndCreateObjects(): Waiting for {} to complete...",
101 HOST_ON_FILE)
102 .c_str());
103 discoverTimer->restartOnce(10s);
Matt Spinlerd267cec2021-09-01 14:49:19 -0500104 }
105#endif
106}
107
108std::vector<int> Manager::findOCCsInDev()
109{
110 std::vector<int> occs;
111 std::regex expr{R"(occ(\d+)$)"};
112
113 for (auto& file : fs::directory_iterator("/dev"))
114 {
115 std::smatch match;
116 std::string path{file.path().string()};
117 if (std::regex_search(path, match, expr))
118 {
119 auto num = std::stoi(match[1].str());
120
121 // /dev numbering starts at 1, ours starts at 0.
122 occs.push_back(num - 1);
123 }
124 }
125
126 return occs;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530127}
128
129int Manager::cpuCreated(sdbusplus::message::message& msg)
130{
George Liubcef3b42021-09-10 12:39:02 +0800131 namespace fs = std::filesystem;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530132
133 sdbusplus::message::object_path o;
134 msg.read(o);
135 fs::path cpuPath(std::string(std::move(o)));
136
137 auto name = cpuPath.filename().string();
138 auto index = name.find(CPU_NAME);
139 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
140
141 createObjects(name);
142
143 return 0;
144}
145
146void Manager::createObjects(const std::string& occ)
147{
148 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
149
Chris Cain6fa848a2022-01-24 14:54:38 -0600150#ifdef POWER10
151 if (!pmode)
152 {
Chris Cain1be43372021-12-09 19:29:37 -0600153 // Create the power mode object
154 pmode = std::make_unique<open_power::occ::powermode::PowerMode>(
155 *this, powermode::PMODE_PATH, powermode::PIPS_PATH);
Chris Cain6fa848a2022-01-24 14:54:38 -0600156 }
157#endif
158
Gunnar Mills94df8c92018-09-14 14:50:03 -0500159 statusObjects.emplace_back(std::make_unique<Status>(
George Liuf3b75142021-06-10 11:22:50 +0800160 event, path.c_str(), *this,
Chris Cain36f9cde2021-11-22 11:18:21 -0600161#ifdef POWER10
162 pmode,
163#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500164 std::bind(std::mem_fn(&Manager::statusCallBack), this,
Tom Joseph00325232020-07-29 17:51:48 +0530165 std::placeholders::_1)
166#ifdef PLDM
167 ,
168 std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(),
169 std::placeholders::_1)
170#endif
171 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530172
Chris Cain36f9cde2021-11-22 11:18:21 -0600173 if (statusObjects.back()->isMasterOcc())
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530174 {
Chris Cain36f9cde2021-11-22 11:18:21 -0600175 log<level::INFO>(
176 fmt::format("Manager::createObjects(): OCC{} is the master",
177 statusObjects.back()->getOccInstanceID())
178 .c_str());
179 _pollTimer->setEnabled(false);
180
181 // Create the power cap monitor object for master OCC
182 if (!pcap)
183 {
184 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
Chris Cain1be43372021-12-09 19:29:37 -0600185 *statusObjects.back());
Chris Cain36f9cde2021-11-22 11:18:21 -0600186 }
Chris Cain78e86012021-03-04 16:15:31 -0600187
188#ifdef POWER10
Chris Cain6fa848a2022-01-24 14:54:38 -0600189 // Set the master OCC on the PowerMode object
190 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600191#endif
Chris Cain36f9cde2021-11-22 11:18:21 -0600192 }
193
194 passThroughObjects.emplace_back(std::make_unique<PassThrough>(path.c_str()
195#ifdef POWER10
196 ,
197 pmode
198#endif
199 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530200}
201
202void Manager::statusCallBack(bool status)
203{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500204 using InternalFailure =
205 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530206
207 // At this time, it won't happen but keeping it
208 // here just in case something changes in the future
209 if ((activeCount == 0) && (!status))
210 {
211 log<level::ERR>("Invalid update on OCCActive");
212 elog<InternalFailure>();
213 }
214
Chris Caina7b74dc2021-11-10 17:03:43 -0600215 if (status == true)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600216 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600217 // OCC went active
218 ++activeCount;
219
220#ifdef POWER10
221 if (activeCount == 1)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600222 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600223 // First OCC went active (allow some time for all OCCs to go active)
224 waitForAllOccsTimer->restartOnce(30s);
Matt Spinler53f68142021-08-25 15:47:31 -0500225 }
226#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600227
228 if (activeCount == statusObjects.size())
229 {
230#ifdef POWER10
231 // All OCCs are now running
232 if (waitForAllOccsTimer->isEnabled())
233 {
234 // stop occ wait timer
235 waitForAllOccsTimer->setEnabled(false);
236 }
237#endif
238
239 // Verify master OCC and start presence monitor
240 validateOccMaster();
241 }
242
243 // Start poll timer if not already started
244 if (!_pollTimer->isEnabled())
245 {
246 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -0600247 fmt::format("Manager: OCCs will be polled every {} seconds",
248 pollInterval)
Chris Caina7b74dc2021-11-10 17:03:43 -0600249 .c_str());
250
251 // Send poll and start OCC poll timer
252 pollerTimerExpired();
253 }
254 }
255 else
256 {
257 // OCC went away
258 --activeCount;
259
260 if (activeCount == 0)
261 {
262 // No OCCs are running
263
264 // Stop OCC poll timer
265 if (_pollTimer->isEnabled())
266 {
267 log<level::INFO>(
268 "Manager::statusCallBack(): OCCs are not running, stopping poll timer");
269 _pollTimer->setEnabled(false);
270 }
271
272#ifdef POWER10
273 // stop wait timer
274 if (waitForAllOccsTimer->isEnabled())
275 {
276 waitForAllOccsTimer->setEnabled(false);
277 }
278#endif
279
280#ifdef READ_OCC_SENSORS
281 // Clear OCC sensors
282 for (auto& obj : statusObjects)
283 {
284 setSensorValueToNaN(obj->getOccInstanceID());
285 }
286#endif
287 }
Chris Caina8857c52021-01-27 11:53:05 -0600288 }
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530289}
290
291#ifdef I2C_OCC
292void Manager::initStatusObjects()
293{
294 // Make sure we have a valid path string
295 static_assert(sizeof(DEV_PATH) != 0);
296
297 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
Lei YU41470e52017-11-30 16:03:50 +0800298 auto occMasterName = deviceNames.front();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530299 for (auto& name : deviceNames)
300 {
301 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +0800302 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530303 auto path = fs::path(OCC_CONTROL_ROOT) / name;
304 statusObjects.emplace_back(
George Liuf3b75142021-06-10 11:22:50 +0800305 std::make_unique<Status>(event, path.c_str(), *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530306 }
Lei YU41470e52017-11-30 16:03:50 +0800307 // The first device is master occ
308 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
George Liuf3b75142021-06-10 11:22:50 +0800309 *statusObjects.front(), occMasterName);
Chris Cain78e86012021-03-04 16:15:31 -0600310#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -0600311 pmode = std::make_unique<open_power::occ::powermode::PowerMode>(
312 *this, open_power::occ::powermode::PMODE_PATH,
313 open_power::occ::powermode::PIPS_PATH);
Chris Cain6fa848a2022-01-24 14:54:38 -0600314 // Set the master OCC on the PowerMode object
315 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600316#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530317}
318#endif
319
Tom Joseph815f9f52020-07-27 12:12:13 +0530320#ifdef PLDM
Eddie Jamescbad2192021-10-07 09:39:39 -0500321void Manager::sbeTimeout(unsigned int instance)
322{
Eddie James2a751d72022-03-04 09:16:12 -0600323 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
324 [instance](const auto& obj) {
325 return instance == obj->getOccInstanceID();
326 });
Eddie Jamescbad2192021-10-07 09:39:39 -0500327
Eddie James2a751d72022-03-04 09:16:12 -0600328 if (obj != statusObjects.end() && obj->occActive())
329 {
330 log<level::INFO>("SBE timeout, requesting HRESET",
331 entry("SBE=%d", instance));
Eddie Jamescbad2192021-10-07 09:39:39 -0500332
Eddie James2a751d72022-03-04 09:16:12 -0600333 setSBEState(instance, SBE_STATE_NOT_USABLE);
334
335 pldmHandle->sendHRESET(instance);
336 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500337}
338
Tom Joseph815f9f52020-07-27 12:12:13 +0530339bool Manager::updateOCCActive(instanceID instance, bool status)
340{
341 return (statusObjects[instance])->occActive(status);
342}
Eddie Jamescbad2192021-10-07 09:39:39 -0500343
344void Manager::sbeHRESETResult(instanceID instance, bool success)
345{
346 if (success)
347 {
348 log<level::INFO>("HRESET succeeded", entry("SBE=%d", instance));
349
350 setSBEState(instance, SBE_STATE_BOOTED);
351
352 return;
353 }
354
355 setSBEState(instance, SBE_STATE_FAILED);
356
357 if (sbeCanDump(instance))
358 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500359 log<level::INFO>("HRESET failed, triggering SBE dump",
360 entry("SBE=%d", instance));
361
362 auto& bus = utils::getBus();
363 uint32_t src6 = instance << 16;
364 uint32_t logId =
365 FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout",
366 src6, "SBE command timeout");
367
368 try
369 {
George Liuf3a4a692021-12-28 13:59:51 +0800370 constexpr auto path = "/org/openpower/dump";
371 constexpr auto interface = "xyz.openbmc_project.Dump.Create";
372 constexpr auto function = "CreateDump";
373
Eddie Jamescbad2192021-10-07 09:39:39 -0500374 std::string service = utils::getService(path, interface);
375 auto method =
376 bus.new_method_call(service.c_str(), path, interface, function);
377
378 std::map<std::string, std::variant<std::string, uint64_t>>
379 createParams{
380 {"com.ibm.Dump.Create.CreateParameters.ErrorLogId",
381 uint64_t(logId)},
382 {"com.ibm.Dump.Create.CreateParameters.DumpType",
383 "com.ibm.Dump.Create.DumpType.SBE"},
384 {"com.ibm.Dump.Create.CreateParameters.FailingUnitId",
385 uint64_t(instance)},
386 };
387
388 method.append(createParams);
389
390 auto response = bus.call(method);
391 }
392 catch (const sdbusplus::exception::exception& e)
393 {
394 constexpr auto ERROR_DUMP_DISABLED =
395 "xyz.openbmc_project.Dump.Create.Error.Disabled";
396 if (e.name() == ERROR_DUMP_DISABLED)
397 {
398 log<level::INFO>("Dump is disabled, skipping");
399 }
400 else
401 {
402 log<level::ERR>("Dump failed");
403 }
404 }
405 }
406}
407
408bool Manager::sbeCanDump(unsigned int instance)
409{
410 struct pdbg_target* proc = getPdbgTarget(instance);
411
412 if (!proc)
413 {
414 // allow the dump in the error case
415 return true;
416 }
417
418 try
419 {
420 if (!openpower::phal::sbe::isDumpAllowed(proc))
421 {
422 return false;
423 }
424
425 if (openpower::phal::pdbg::isSbeVitalAttnActive(proc))
426 {
427 return false;
428 }
429 }
430 catch (openpower::phal::exception::SbeError& e)
431 {
432 log<level::INFO>("Failed to query SBE state");
433 }
434
435 // allow the dump in the error case
436 return true;
437}
438
439void Manager::setSBEState(unsigned int instance, enum sbe_state state)
440{
441 struct pdbg_target* proc = getPdbgTarget(instance);
442
443 if (!proc)
444 {
445 return;
446 }
447
448 try
449 {
450 openpower::phal::sbe::setState(proc, state);
451 }
452 catch (const openpower::phal::exception::SbeError& e)
453 {
454 log<level::ERR>("Failed to set SBE state");
455 }
456}
457
458struct pdbg_target* Manager::getPdbgTarget(unsigned int instance)
459{
460 if (!pdbgInitialized)
461 {
462 try
463 {
464 openpower::phal::pdbg::init();
465 pdbgInitialized = true;
466 }
467 catch (const openpower::phal::exception::PdbgError& e)
468 {
469 log<level::ERR>("pdbg initialization failed");
470 return nullptr;
471 }
472 }
473
474 struct pdbg_target* proc = nullptr;
475 pdbg_for_each_class_target("proc", proc)
476 {
477 if (pdbg_target_index(proc) == instance)
478 {
479 return proc;
480 }
481 }
482
483 log<level::ERR>("Failed to get pdbg target");
484 return nullptr;
485}
Tom Joseph815f9f52020-07-27 12:12:13 +0530486#endif
487
Chris Caina8857c52021-01-27 11:53:05 -0600488void Manager::pollerTimerExpired()
489{
Chris Caina8857c52021-01-27 11:53:05 -0600490 if (!_pollTimer)
491 {
492 log<level::ERR>(
493 "Manager::pollerTimerExpired() ERROR: Timer not defined");
494 return;
495 }
496
497 for (auto& obj : statusObjects)
498 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600499#ifdef READ_OCC_SENSORS
500 auto id = obj->getOccInstanceID();
501#endif
502 if (!obj->occActive())
503 {
504 // OCC is not running yet
505#ifdef READ_OCC_SENSORS
506 setSensorValueToNaN(id);
507#endif
508 continue;
509 }
510
Chris Caina8857c52021-01-27 11:53:05 -0600511 // Read sysfs to force kernel to poll OCC
512 obj->readOccState();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800513
514#ifdef READ_OCC_SENSORS
515 // Read occ sensor values
Chicago Duanbb895cb2021-06-18 19:37:16 +0800516 getSensorValues(id, obj->isMasterOcc());
517#endif
Chris Caina8857c52021-01-27 11:53:05 -0600518 }
519
Chris Caina7b74dc2021-11-10 17:03:43 -0600520 if (activeCount > 0)
521 {
522 // Restart OCC poll timer
523 _pollTimer->restartOnce(std::chrono::seconds(pollInterval));
524 }
525 else
526 {
527 // No OCCs running, so poll timer will not be restarted
528 log<level::INFO>(
529 fmt::format(
530 "Manager::pollerTimerExpired: poll timer will not be restarted")
531 .c_str());
532 }
Chris Caina8857c52021-01-27 11:53:05 -0600533}
534
Chicago Duanbb895cb2021-06-18 19:37:16 +0800535#ifdef READ_OCC_SENSORS
536void Manager::readTempSensors(const fs::path& path, uint32_t id)
537{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800538 std::regex expr{"temp\\d+_label$"}; // Example: temp5_label
539 for (auto& file : fs::directory_iterator(path))
540 {
541 if (!std::regex_search(file.path().string(), expr))
542 {
543 continue;
544 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800545
Matt Spinlera26f1522021-08-25 15:50:20 -0500546 uint32_t labelValue{0};
547
548 try
549 {
550 labelValue = readFile<uint32_t>(file.path());
551 }
552 catch (const std::system_error& e)
553 {
554 log<level::DEBUG>(
555 fmt::format("readTempSensors: Failed reading {}, errno = {}",
556 file.path().string(), e.code().value())
557 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800558 continue;
559 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800560
561 const std::string& tempLabel = "label";
562 const std::string filePathString = file.path().string().substr(
563 0, file.path().string().length() - tempLabel.length());
Matt Spinlera26f1522021-08-25 15:50:20 -0500564
565 uint32_t fruTypeValue{0};
566 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800567 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500568 fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix);
569 }
570 catch (const std::system_error& e)
571 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800572 log<level::DEBUG>(
Matt Spinlera26f1522021-08-25 15:50:20 -0500573 fmt::format("readTempSensors: Failed reading {}, errno = {}",
574 filePathString + fruTypeSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800575 .c_str());
576 continue;
577 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800578
579 std::string sensorPath =
580 OCC_SENSORS_ROOT + std::string("/temperature/");
581
Matt Spinlerace67d82021-10-18 13:41:57 -0500582 std::string dvfsTempPath;
583
Chicago Duanbb895cb2021-06-18 19:37:16 +0800584 if (fruTypeValue == VRMVdd)
585 {
586 sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp");
587 }
Matt Spinlerace67d82021-10-18 13:41:57 -0500588 else if (fruTypeValue == processorIoRing)
589 {
590 sensorPath.append("proc" + std::to_string(id) + "_ioring_temp");
591 dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" +
592 std::to_string(id) + "_ioring_dvfs_temp";
593 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800594 else
595 {
Matt Spinler14d14022021-08-25 15:38:29 -0500596 uint16_t type = (labelValue & 0xFF000000) >> 24;
597 uint16_t instanceID = labelValue & 0x0000FFFF;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800598
599 if (type == OCC_DIMM_TEMP_SENSOR_TYPE)
600 {
Matt Spinler8b8abee2021-08-25 15:18:21 -0500601 if (fruTypeValue == fruTypeNotAvailable)
602 {
603 // Not all DIMM related temps are available to read
604 // (no _input file in this case)
605 continue;
606 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800607 auto iter = dimmTempSensorName.find(fruTypeValue);
608 if (iter == dimmTempSensorName.end())
609 {
George Liub5ca1012021-09-10 12:53:11 +0800610 log<level::ERR>(
611 fmt::format(
612 "readTempSensors: Fru type error! fruTypeValue = {}) ",
613 fruTypeValue)
614 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800615 continue;
616 }
617
618 sensorPath.append("dimm" + std::to_string(instanceID) +
619 iter->second);
620 }
621 else if (type == OCC_CPU_TEMP_SENSOR_TYPE)
622 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500623 if (fruTypeValue == processorCore)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800624 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500625 // The OCC reports small core temps, of which there are
626 // two per big core. All current P10 systems are in big
627 // core mode, so use a big core name.
628 uint16_t coreNum = instanceID / 2;
629 uint16_t tempNum = instanceID % 2;
630 sensorPath.append("proc" + std::to_string(id) + "_core" +
631 std::to_string(coreNum) + "_" +
632 std::to_string(tempNum) + "_temp");
633
634 dvfsTempPath = std::string{OCC_SENSORS_ROOT} +
635 "/temperature/proc" + std::to_string(id) +
636 "_core_dvfs_temp";
637 }
638 else
639 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800640 continue;
641 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800642 }
643 else
644 {
645 continue;
646 }
647 }
648
Matt Spinlerace67d82021-10-18 13:41:57 -0500649 // The dvfs temp file only needs to be read once per chip per type.
650 if (!dvfsTempPath.empty() &&
651 !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath))
652 {
653 try
654 {
655 auto dvfsValue = readFile<double>(filePathString + maxSuffix);
656
657 dbus::OccDBusSensors::getOccDBus().setDvfsTemp(
658 dvfsTempPath, dvfsValue * std::pow(10, -3));
659 }
660 catch (const std::system_error& e)
661 {
662 log<level::DEBUG>(
663 fmt::format(
664 "readTempSensors: Failed reading {}, errno = {}",
665 filePathString + maxSuffix, e.code().value())
666 .c_str());
667 }
668 }
669
Matt Spinlera26f1522021-08-25 15:50:20 -0500670 uint32_t faultValue{0};
671 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800672 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500673 faultValue = readFile<uint32_t>(filePathString + faultSuffix);
674 }
675 catch (const std::system_error& e)
676 {
677 log<level::DEBUG>(
678 fmt::format("readTempSensors: Failed reading {}, errno = {}",
679 filePathString + faultSuffix, e.code().value())
680 .c_str());
681 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800682 }
683
Matt Spinlera26f1522021-08-25 15:50:20 -0500684 if (faultValue != 0)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800685 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800686 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500687 sensorPath, std::numeric_limits<double>::quiet_NaN());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800688
689 open_power::occ::dbus::OccDBusSensors::getOccDBus()
Matt Spinlera26f1522021-08-25 15:50:20 -0500690 .setOperationalStatus(sensorPath, false);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800691
Matt Spinlera26f1522021-08-25 15:50:20 -0500692 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800693 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500694
695 double tempValue{0};
696
697 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800698 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500699 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800700 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500701 catch (const std::system_error& e)
702 {
703 log<level::DEBUG>(
704 fmt::format("readTempSensors: Failed reading {}, errno = {}",
705 filePathString + inputSuffix, e.code().value())
706 .c_str());
707 continue;
708 }
709
710 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
711 sensorPath, tempValue * std::pow(10, -3));
712
713 open_power::occ::dbus::OccDBusSensors::getOccDBus()
714 .setOperationalStatus(sensorPath, true);
715
Chris Cain6fa848a2022-01-24 14:54:38 -0600716 // At this point, the sensor will be created for sure.
717 if (existingSensors.find(sensorPath) == existingSensors.end())
718 {
719 open_power::occ::dbus::OccDBusSensors::getOccDBus()
720 .setChassisAssociation(sensorPath);
721 }
722
Matt Spinlera26f1522021-08-25 15:50:20 -0500723 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800724 }
725 return;
726}
727
728std::optional<std::string>
729 Manager::getPowerLabelFunctionID(const std::string& value)
730{
731 // If the value is "system", then the FunctionID is "system".
732 if (value == "system")
733 {
734 return value;
735 }
736
737 // If the value is not "system", then the label value have 3 numbers, of
738 // which we only care about the middle one:
739 // <sensor id>_<function id>_<apss channel>
740 // eg: The value is "0_10_5" , then the FunctionID is "10".
741 if (value.find("_") == std::string::npos)
742 {
743 return std::nullopt;
744 }
745
746 auto powerLabelValue = value.substr((value.find("_") + 1));
747
748 if (powerLabelValue.find("_") == std::string::npos)
749 {
750 return std::nullopt;
751 }
752
753 return powerLabelValue.substr(0, powerLabelValue.find("_"));
754}
755
756void Manager::readPowerSensors(const fs::path& path, uint32_t id)
757{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800758 std::regex expr{"power\\d+_label$"}; // Example: power5_label
759 for (auto& file : fs::directory_iterator(path))
760 {
761 if (!std::regex_search(file.path().string(), expr))
762 {
763 continue;
764 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800765
Matt Spinlera26f1522021-08-25 15:50:20 -0500766 std::string labelValue;
767 try
768 {
769 labelValue = readFile<std::string>(file.path());
770 }
771 catch (const std::system_error& e)
772 {
773 log<level::DEBUG>(
774 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
775 file.path().string(), e.code().value())
776 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800777 continue;
778 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800779
780 auto functionID = getPowerLabelFunctionID(labelValue);
781 if (functionID == std::nullopt)
782 {
783 continue;
784 }
785
786 const std::string& tempLabel = "label";
787 const std::string filePathString = file.path().string().substr(
788 0, file.path().string().length() - tempLabel.length());
789
790 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
791
792 auto iter = powerSensorName.find(*functionID);
793 if (iter == powerSensorName.end())
794 {
795 continue;
796 }
797 sensorPath.append(iter->second);
798
Matt Spinlera26f1522021-08-25 15:50:20 -0500799 double tempValue{0};
800
801 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800802 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500803 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800804 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500805 catch (const std::system_error& e)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800806 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800807 log<level::DEBUG>(
Matt Spinlera26f1522021-08-25 15:50:20 -0500808 fmt::format("readTempSensors: Failed reading {}, errno = {}",
809 filePathString + inputSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800810 .c_str());
Matt Spinlera26f1522021-08-25 15:50:20 -0500811 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800812 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500813
Chris Caind84a8332022-01-13 08:58:45 -0600814 open_power::occ::dbus::OccDBusSensors::getOccDBus().setUnit(
815 sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts");
816
Matt Spinlera26f1522021-08-25 15:50:20 -0500817 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
818 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
819
820 open_power::occ::dbus::OccDBusSensors::getOccDBus()
821 .setOperationalStatus(sensorPath, true);
822
Matt Spinler5901abd2021-09-23 13:50:03 -0500823 if (existingSensors.find(sensorPath) == existingSensors.end())
824 {
825 open_power::occ::dbus::OccDBusSensors::getOccDBus()
826 .setChassisAssociation(sensorPath);
827 }
828
Matt Spinlera26f1522021-08-25 15:50:20 -0500829 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800830 }
831 return;
832}
833
834void Manager::setSensorValueToNaN(uint32_t id)
835{
836 for (const auto& [sensorPath, occId] : existingSensors)
837 {
838 if (occId == id)
839 {
840 open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue(
841 sensorPath, std::numeric_limits<double>::quiet_NaN());
842 }
843 }
844 return;
845}
846
847void Manager::getSensorValues(uint32_t id, bool masterOcc)
848{
849 const auto occ = std::string("occ-hwmon.") + std::to_string(id + 1);
850
851 fs::path fileName{OCC_HWMON_PATH + occ + "/hwmon/"};
852
853 // Need to get the hwmonXX directory name, there better only be 1 dir
854 assert(std::distance(fs::directory_iterator(fileName),
855 fs::directory_iterator{}) == 1);
856 // Now set our path to this full path, including this hwmonXX directory
857 fileName = fs::path(*fs::directory_iterator(fileName));
858
859 // Read temperature sensors
860 readTempSensors(fileName, id);
861
862 if (masterOcc)
863 {
864 // Read power sensors
865 readPowerSensors(fileName, id);
866 }
867
868 return;
869}
870#endif
Chris Cain17257672021-10-22 13:41:03 -0500871
872// Read the altitude from DBus
873void Manager::readAltitude()
874{
875 static bool traceAltitudeErr = true;
876
877 utils::PropertyValue altitudeProperty{};
878 try
879 {
880 altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE,
881 ALTITUDE_PROP);
882 auto sensorVal = std::get<double>(altitudeProperty);
883 if (sensorVal < 0xFFFF)
884 {
885 if (sensorVal < 0)
886 {
887 altitude = 0;
888 }
889 else
890 {
891 // Round to nearest meter
892 altitude = uint16_t(sensorVal + 0.5);
893 }
894 log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)",
895 sensorVal, altitude)
896 .c_str());
897 traceAltitudeErr = true;
898 }
899 else
900 {
901 if (traceAltitudeErr)
902 {
903 traceAltitudeErr = false;
904 log<level::DEBUG>(
905 fmt::format("Invalid altitude value: {}", sensorVal)
906 .c_str());
907 }
908 }
909 }
910 catch (const sdbusplus::exception::exception& e)
911 {
912 if (traceAltitudeErr)
913 {
914 traceAltitudeErr = false;
915 log<level::INFO>(
916 fmt::format("Unable to read Altitude: {}", e.what()).c_str());
917 }
918 altitude = 0xFFFF; // not available
919 }
920}
921
922// Callback function when ambient temperature changes
923void Manager::ambientCallback(sdbusplus::message::message& msg)
924{
925 double currentTemp = 0;
926 uint8_t truncatedTemp = 0xFF;
927 std::string msgSensor;
928 std::map<std::string, std::variant<double>> msgData;
929 msg.read(msgSensor, msgData);
930
931 auto valPropMap = msgData.find(AMBIENT_PROP);
932 if (valPropMap == msgData.end())
933 {
934 log<level::DEBUG>("ambientCallback: Unknown ambient property changed");
935 return;
936 }
937 currentTemp = std::get<double>(valPropMap->second);
938 if (std::isnan(currentTemp))
939 {
940 truncatedTemp = 0xFF;
941 }
942 else
943 {
944 if (currentTemp < 0)
945 {
946 truncatedTemp = 0;
947 }
948 else
949 {
950 // Round to nearest degree C
951 truncatedTemp = uint8_t(currentTemp + 0.5);
952 }
953 }
954
955 // If ambient changes, notify OCCs
956 if (truncatedTemp != ambient)
957 {
958 log<level::DEBUG>(
959 fmt::format("ambientCallback: Ambient change from {} to {}C",
960 ambient, currentTemp)
961 .c_str());
962
963 ambient = truncatedTemp;
964 if (altitude == 0xFFFF)
965 {
966 // No altitude yet, try reading again
967 readAltitude();
968 }
969
970 log<level::DEBUG>(
971 fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient,
972 altitude)
973 .c_str());
974#ifdef POWER10
975 // Send ambient and altitude to all OCCs
976 for (auto& obj : statusObjects)
977 {
978 if (obj->occActive())
979 {
980 obj->sendAmbient(ambient, altitude);
981 }
982 }
983#endif // POWER10
984 }
985}
986
987// return the current ambient and altitude readings
988void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
989 uint16_t& altitudeValue) const
990{
991 ambientValid = true;
992 ambientTemp = ambient;
993 altitudeValue = altitude;
994
995 if (ambient == 0xFF)
996 {
997 ambientValid = false;
998 }
999}
1000
Chris Caina7b74dc2021-11-10 17:03:43 -06001001#ifdef POWER10
1002void Manager::occsNotAllRunning()
1003{
Chris Cain6fa848a2022-01-24 14:54:38 -06001004 // Function will also gets called when occ-control app gets
1005 // restarted. (occ active sensors do not change, so the Status
1006 // object does not call Manager back for all OCCs)
Chris Caina7b74dc2021-11-10 17:03:43 -06001007
1008 if (activeCount != statusObjects.size())
1009 {
1010 // Not all OCCs went active
1011 log<level::WARNING>(
1012 fmt::format(
1013 "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})",
1014 activeCount, statusObjects.size())
1015 .c_str());
1016 // Procs may be garded, so may not need reset.
1017 }
1018
1019 validateOccMaster();
1020}
1021#endif // POWER10
1022
1023// Verify single master OCC and start presence monitor
1024void Manager::validateOccMaster()
1025{
1026 int masterInstance = -1;
1027 for (auto& obj : statusObjects)
1028 {
1029 obj->addPresenceWatchMaster();
1030 if (obj->isMasterOcc())
1031 {
1032 if (masterInstance == -1)
1033 {
1034 masterInstance = obj->getOccInstanceID();
1035 }
1036 else
1037 {
1038 log<level::ERR>(
1039 fmt::format(
1040 "validateOccMaster: Multiple OCC masters! ({} and {})",
1041 masterInstance, obj->getOccInstanceID())
1042 .c_str());
1043 // request reset
1044 obj->deviceError();
1045 }
1046 }
1047 }
1048 if (masterInstance < 0)
1049 {
1050 log<level::ERR>("validateOccMaster: Master OCC not found!");
1051 // request reset
1052 statusObjects.front()->deviceError();
1053 }
1054 else
1055 {
1056 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -06001057 fmt::format("validateOccMaster: OCC{} is master of {} OCCs",
1058 masterInstance, activeCount)
Chris Caina7b74dc2021-11-10 17:03:43 -06001059 .c_str());
1060 }
1061}
1062
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301063} // namespace occ
1064} // namespace open_power