blob: eb116e75dc30945660cf8609e188bff94f8d1dee [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
Chris Cain5d66a0a2022-02-09 08:52:10 -0600154 pmode = std::make_unique<powermode::PowerMode>(
Chris Cain1be43372021-12-09 19:29:37 -0600155 *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,
Sheldon Bailey373af752022-02-21 15:14:00 -0600165 std::placeholders::_1, std::placeholders::_2)
Tom Joseph00325232020-07-29 17:51:48 +0530166#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 Cain40501a22022-03-14 17:33:27 -0500173 // Create the power cap monitor object
174 if (!pcap)
175 {
176 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
177 *statusObjects.back());
178 }
179
Chris Cain36f9cde2021-11-22 11:18:21 -0600180 if (statusObjects.back()->isMasterOcc())
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530181 {
Chris Cain36f9cde2021-11-22 11:18:21 -0600182 log<level::INFO>(
183 fmt::format("Manager::createObjects(): OCC{} is the master",
184 statusObjects.back()->getOccInstanceID())
185 .c_str());
186 _pollTimer->setEnabled(false);
187
Chris Cain78e86012021-03-04 16:15:31 -0600188#ifdef POWER10
Chris Cain6fa848a2022-01-24 14:54:38 -0600189 // Set the master OCC on the PowerMode object
190 pmode->setMasterOcc(path);
Chris Cain40501a22022-03-14 17:33:27 -0500191 // Update power cap bounds
192 pcap->updatePcapBounds();
Chris Cain78e86012021-03-04 16:15:31 -0600193#endif
Chris Cain36f9cde2021-11-22 11:18:21 -0600194 }
195
196 passThroughObjects.emplace_back(std::make_unique<PassThrough>(path.c_str()
197#ifdef POWER10
198 ,
199 pmode
200#endif
201 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530202}
203
Sheldon Bailey373af752022-02-21 15:14:00 -0600204void Manager::statusCallBack(instanceID instance, bool status)
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530205{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500206 using InternalFailure =
207 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530208
209 // At this time, it won't happen but keeping it
210 // here just in case something changes in the future
211 if ((activeCount == 0) && (!status))
212 {
Sheldon Bailey373af752022-02-21 15:14:00 -0600213 log<level::ERR>(
214 fmt::format("Invalid update on OCCActive with OCC{}", instance)
215 .c_str());
216
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530217 elog<InternalFailure>();
218 }
219
Chris Caina7b74dc2021-11-10 17:03:43 -0600220 if (status == true)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600221 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600222 // OCC went active
223 ++activeCount;
224
225#ifdef POWER10
226 if (activeCount == 1)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600227 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600228 // First OCC went active (allow some time for all OCCs to go active)
229 waitForAllOccsTimer->restartOnce(30s);
Matt Spinler53f68142021-08-25 15:47:31 -0500230 }
231#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600232
233 if (activeCount == statusObjects.size())
234 {
235#ifdef POWER10
236 // All OCCs are now running
237 if (waitForAllOccsTimer->isEnabled())
238 {
239 // stop occ wait timer
240 waitForAllOccsTimer->setEnabled(false);
241 }
242#endif
243
244 // Verify master OCC and start presence monitor
245 validateOccMaster();
246 }
247
248 // Start poll timer if not already started
249 if (!_pollTimer->isEnabled())
250 {
251 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -0600252 fmt::format("Manager: OCCs will be polled every {} seconds",
253 pollInterval)
Chris Caina7b74dc2021-11-10 17:03:43 -0600254 .c_str());
255
256 // Send poll and start OCC poll timer
257 pollerTimerExpired();
258 }
259 }
260 else
261 {
262 // OCC went away
263 --activeCount;
264
265 if (activeCount == 0)
266 {
267 // No OCCs are running
268
269 // Stop OCC poll timer
270 if (_pollTimer->isEnabled())
271 {
272 log<level::INFO>(
273 "Manager::statusCallBack(): OCCs are not running, stopping poll timer");
274 _pollTimer->setEnabled(false);
275 }
276
277#ifdef POWER10
278 // stop wait timer
279 if (waitForAllOccsTimer->isEnabled())
280 {
281 waitForAllOccsTimer->setEnabled(false);
282 }
283#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600284 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600285#ifdef READ_OCC_SENSORS
286 // Clear OCC sensors
287 setSensorValueToNonFunctional(instance);
288#endif
Chris Caina8857c52021-01-27 11:53:05 -0600289 }
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530290}
291
292#ifdef I2C_OCC
293void Manager::initStatusObjects()
294{
295 // Make sure we have a valid path string
296 static_assert(sizeof(DEV_PATH) != 0);
297
298 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
299 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 }
Chris Cain40501a22022-03-14 17:33:27 -0500307 // The first device is master occ
308 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
309 *statusObjects.front());
Chris Cain78e86012021-03-04 16:15:31 -0600310#ifdef POWER10
Chris Cain5d66a0a2022-02-09 08:52:10 -0600311 pmode = std::make_unique<powermode::PowerMode>(*this, powermode::PMODE_PATH,
312 powermode::PIPS_PATH);
Chris Cain6fa848a2022-01-24 14:54:38 -0600313 // Set the master OCC on the PowerMode object
314 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600315#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530316}
317#endif
318
Tom Joseph815f9f52020-07-27 12:12:13 +0530319#ifdef PLDM
Eddie Jamescbad2192021-10-07 09:39:39 -0500320void Manager::sbeTimeout(unsigned int instance)
321{
Eddie James2a751d72022-03-04 09:16:12 -0600322 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
323 [instance](const auto& obj) {
324 return instance == obj->getOccInstanceID();
325 });
Eddie Jamescbad2192021-10-07 09:39:39 -0500326
Eddie Jamescb018da2022-03-05 11:49:37 -0600327 if (obj != statusObjects.end() && (*obj)->occActive())
Eddie James2a751d72022-03-04 09:16:12 -0600328 {
329 log<level::INFO>("SBE timeout, requesting HRESET",
330 entry("SBE=%d", instance));
Eddie Jamescbad2192021-10-07 09:39:39 -0500331
Eddie James2a751d72022-03-04 09:16:12 -0600332 setSBEState(instance, SBE_STATE_NOT_USABLE);
333
334 pldmHandle->sendHRESET(instance);
335 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500336}
337
Tom Joseph815f9f52020-07-27 12:12:13 +0530338bool Manager::updateOCCActive(instanceID instance, bool status)
339{
340 return (statusObjects[instance])->occActive(status);
341}
Eddie Jamescbad2192021-10-07 09:39:39 -0500342
343void Manager::sbeHRESETResult(instanceID instance, bool success)
344{
345 if (success)
346 {
347 log<level::INFO>("HRESET succeeded", entry("SBE=%d", instance));
348
349 setSBEState(instance, SBE_STATE_BOOTED);
350
351 return;
352 }
353
354 setSBEState(instance, SBE_STATE_FAILED);
355
356 if (sbeCanDump(instance))
357 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500358 log<level::INFO>("HRESET failed, triggering SBE dump",
359 entry("SBE=%d", instance));
360
361 auto& bus = utils::getBus();
362 uint32_t src6 = instance << 16;
363 uint32_t logId =
364 FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout",
365 src6, "SBE command timeout");
366
367 try
368 {
George Liuf3a4a692021-12-28 13:59:51 +0800369 constexpr auto path = "/org/openpower/dump";
370 constexpr auto interface = "xyz.openbmc_project.Dump.Create";
371 constexpr auto function = "CreateDump";
372
Eddie Jamescbad2192021-10-07 09:39:39 -0500373 std::string service = utils::getService(path, interface);
374 auto method =
375 bus.new_method_call(service.c_str(), path, interface, function);
376
377 std::map<std::string, std::variant<std::string, uint64_t>>
378 createParams{
379 {"com.ibm.Dump.Create.CreateParameters.ErrorLogId",
380 uint64_t(logId)},
381 {"com.ibm.Dump.Create.CreateParameters.DumpType",
382 "com.ibm.Dump.Create.DumpType.SBE"},
383 {"com.ibm.Dump.Create.CreateParameters.FailingUnitId",
384 uint64_t(instance)},
385 };
386
387 method.append(createParams);
388
389 auto response = bus.call(method);
390 }
391 catch (const sdbusplus::exception::exception& e)
392 {
393 constexpr auto ERROR_DUMP_DISABLED =
394 "xyz.openbmc_project.Dump.Create.Error.Disabled";
395 if (e.name() == ERROR_DUMP_DISABLED)
396 {
397 log<level::INFO>("Dump is disabled, skipping");
398 }
399 else
400 {
401 log<level::ERR>("Dump failed");
402 }
403 }
404 }
405}
406
407bool Manager::sbeCanDump(unsigned int instance)
408{
409 struct pdbg_target* proc = getPdbgTarget(instance);
410
411 if (!proc)
412 {
413 // allow the dump in the error case
414 return true;
415 }
416
417 try
418 {
419 if (!openpower::phal::sbe::isDumpAllowed(proc))
420 {
421 return false;
422 }
423
424 if (openpower::phal::pdbg::isSbeVitalAttnActive(proc))
425 {
426 return false;
427 }
428 }
429 catch (openpower::phal::exception::SbeError& e)
430 {
431 log<level::INFO>("Failed to query SBE state");
432 }
433
434 // allow the dump in the error case
435 return true;
436}
437
438void Manager::setSBEState(unsigned int instance, enum sbe_state state)
439{
440 struct pdbg_target* proc = getPdbgTarget(instance);
441
442 if (!proc)
443 {
444 return;
445 }
446
447 try
448 {
449 openpower::phal::sbe::setState(proc, state);
450 }
451 catch (const openpower::phal::exception::SbeError& e)
452 {
453 log<level::ERR>("Failed to set SBE state");
454 }
455}
456
457struct pdbg_target* Manager::getPdbgTarget(unsigned int instance)
458{
459 if (!pdbgInitialized)
460 {
461 try
462 {
463 openpower::phal::pdbg::init();
464 pdbgInitialized = true;
465 }
466 catch (const openpower::phal::exception::PdbgError& e)
467 {
468 log<level::ERR>("pdbg initialization failed");
469 return nullptr;
470 }
471 }
472
473 struct pdbg_target* proc = nullptr;
474 pdbg_for_each_class_target("proc", proc)
475 {
476 if (pdbg_target_index(proc) == instance)
477 {
478 return proc;
479 }
480 }
481
482 log<level::ERR>("Failed to get pdbg target");
483 return nullptr;
484}
Tom Joseph815f9f52020-07-27 12:12:13 +0530485#endif
486
Chris Caina8857c52021-01-27 11:53:05 -0600487void Manager::pollerTimerExpired()
488{
Chris Caina8857c52021-01-27 11:53:05 -0600489 if (!_pollTimer)
490 {
491 log<level::ERR>(
492 "Manager::pollerTimerExpired() ERROR: Timer not defined");
493 return;
494 }
495
496 for (auto& obj : statusObjects)
497 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600498 if (!obj->occActive())
499 {
500 // OCC is not running yet
501#ifdef READ_OCC_SENSORS
Chris Cain5d66a0a2022-02-09 08:52:10 -0600502 auto id = obj->getOccInstanceID();
Sheldon Bailey373af752022-02-21 15:14:00 -0600503 setSensorValueToNonFunctional(id);
Chris Caina7b74dc2021-11-10 17:03:43 -0600504#endif
505 continue;
506 }
507
Chris Caina8857c52021-01-27 11:53:05 -0600508 // Read sysfs to force kernel to poll OCC
509 obj->readOccState();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800510
511#ifdef READ_OCC_SENSORS
512 // Read occ sensor values
Chris Cain5d66a0a2022-02-09 08:52:10 -0600513 getSensorValues(obj);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800514#endif
Chris Caina8857c52021-01-27 11:53:05 -0600515 }
516
Chris Caina7b74dc2021-11-10 17:03:43 -0600517 if (activeCount > 0)
518 {
519 // Restart OCC poll timer
520 _pollTimer->restartOnce(std::chrono::seconds(pollInterval));
521 }
522 else
523 {
524 // No OCCs running, so poll timer will not be restarted
525 log<level::INFO>(
526 fmt::format(
527 "Manager::pollerTimerExpired: poll timer will not be restarted")
528 .c_str());
529 }
Chris Caina8857c52021-01-27 11:53:05 -0600530}
531
Chicago Duanbb895cb2021-06-18 19:37:16 +0800532#ifdef READ_OCC_SENSORS
533void Manager::readTempSensors(const fs::path& path, uint32_t id)
534{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800535 std::regex expr{"temp\\d+_label$"}; // Example: temp5_label
536 for (auto& file : fs::directory_iterator(path))
537 {
538 if (!std::regex_search(file.path().string(), expr))
539 {
540 continue;
541 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800542
Matt Spinlera26f1522021-08-25 15:50:20 -0500543 uint32_t labelValue{0};
544
545 try
546 {
547 labelValue = readFile<uint32_t>(file.path());
548 }
549 catch (const std::system_error& e)
550 {
551 log<level::DEBUG>(
552 fmt::format("readTempSensors: Failed reading {}, errno = {}",
553 file.path().string(), e.code().value())
554 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800555 continue;
556 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800557
558 const std::string& tempLabel = "label";
559 const std::string filePathString = file.path().string().substr(
560 0, file.path().string().length() - tempLabel.length());
Matt Spinlera26f1522021-08-25 15:50:20 -0500561
562 uint32_t fruTypeValue{0};
563 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800564 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500565 fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix);
566 }
567 catch (const std::system_error& e)
568 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800569 log<level::DEBUG>(
Matt Spinlera26f1522021-08-25 15:50:20 -0500570 fmt::format("readTempSensors: Failed reading {}, errno = {}",
571 filePathString + fruTypeSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800572 .c_str());
573 continue;
574 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800575
576 std::string sensorPath =
577 OCC_SENSORS_ROOT + std::string("/temperature/");
578
Matt Spinlerace67d82021-10-18 13:41:57 -0500579 std::string dvfsTempPath;
580
Chicago Duanbb895cb2021-06-18 19:37:16 +0800581 if (fruTypeValue == VRMVdd)
582 {
583 sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp");
584 }
Matt Spinlerace67d82021-10-18 13:41:57 -0500585 else if (fruTypeValue == processorIoRing)
586 {
587 sensorPath.append("proc" + std::to_string(id) + "_ioring_temp");
588 dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" +
589 std::to_string(id) + "_ioring_dvfs_temp";
590 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800591 else
592 {
Matt Spinler14d14022021-08-25 15:38:29 -0500593 uint16_t type = (labelValue & 0xFF000000) >> 24;
594 uint16_t instanceID = labelValue & 0x0000FFFF;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800595
596 if (type == OCC_DIMM_TEMP_SENSOR_TYPE)
597 {
Matt Spinler8b8abee2021-08-25 15:18:21 -0500598 if (fruTypeValue == fruTypeNotAvailable)
599 {
600 // Not all DIMM related temps are available to read
601 // (no _input file in this case)
602 continue;
603 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800604 auto iter = dimmTempSensorName.find(fruTypeValue);
605 if (iter == dimmTempSensorName.end())
606 {
George Liub5ca1012021-09-10 12:53:11 +0800607 log<level::ERR>(
608 fmt::format(
609 "readTempSensors: Fru type error! fruTypeValue = {}) ",
610 fruTypeValue)
611 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800612 continue;
613 }
614
615 sensorPath.append("dimm" + std::to_string(instanceID) +
616 iter->second);
617 }
618 else if (type == OCC_CPU_TEMP_SENSOR_TYPE)
619 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500620 if (fruTypeValue == processorCore)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800621 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500622 // The OCC reports small core temps, of which there are
623 // two per big core. All current P10 systems are in big
624 // core mode, so use a big core name.
625 uint16_t coreNum = instanceID / 2;
626 uint16_t tempNum = instanceID % 2;
627 sensorPath.append("proc" + std::to_string(id) + "_core" +
628 std::to_string(coreNum) + "_" +
629 std::to_string(tempNum) + "_temp");
630
631 dvfsTempPath = std::string{OCC_SENSORS_ROOT} +
632 "/temperature/proc" + std::to_string(id) +
633 "_core_dvfs_temp";
634 }
635 else
636 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800637 continue;
638 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800639 }
640 else
641 {
642 continue;
643 }
644 }
645
Matt Spinlerace67d82021-10-18 13:41:57 -0500646 // The dvfs temp file only needs to be read once per chip per type.
647 if (!dvfsTempPath.empty() &&
648 !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath))
649 {
650 try
651 {
652 auto dvfsValue = readFile<double>(filePathString + maxSuffix);
653
654 dbus::OccDBusSensors::getOccDBus().setDvfsTemp(
655 dvfsTempPath, dvfsValue * std::pow(10, -3));
656 }
657 catch (const std::system_error& e)
658 {
659 log<level::DEBUG>(
660 fmt::format(
661 "readTempSensors: Failed reading {}, errno = {}",
662 filePathString + maxSuffix, e.code().value())
663 .c_str());
664 }
665 }
666
Matt Spinlera26f1522021-08-25 15:50:20 -0500667 uint32_t faultValue{0};
668 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800669 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500670 faultValue = readFile<uint32_t>(filePathString + faultSuffix);
671 }
672 catch (const std::system_error& e)
673 {
674 log<level::DEBUG>(
675 fmt::format("readTempSensors: Failed reading {}, errno = {}",
676 filePathString + faultSuffix, e.code().value())
677 .c_str());
678 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800679 }
680
Matt Spinlera26f1522021-08-25 15:50:20 -0500681 if (faultValue != 0)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800682 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600683 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500684 sensorPath, std::numeric_limits<double>::quiet_NaN());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800685
Chris Cain5d66a0a2022-02-09 08:52:10 -0600686 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
687 false);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800688
Matt Spinlera26f1522021-08-25 15:50:20 -0500689 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800690 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500691
692 double tempValue{0};
693
694 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800695 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500696 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800697 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500698 catch (const std::system_error& e)
699 {
700 log<level::DEBUG>(
701 fmt::format("readTempSensors: Failed reading {}, errno = {}",
702 filePathString + inputSuffix, e.code().value())
703 .c_str());
704 continue;
705 }
706
Chris Cain5d66a0a2022-02-09 08:52:10 -0600707 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500708 sensorPath, tempValue * std::pow(10, -3));
709
Chris Cain5d66a0a2022-02-09 08:52:10 -0600710 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
711 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500712
Chris Cain6fa848a2022-01-24 14:54:38 -0600713 // At this point, the sensor will be created for sure.
714 if (existingSensors.find(sensorPath) == existingSensors.end())
715 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600716 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
717 sensorPath);
Chris Cain6fa848a2022-01-24 14:54:38 -0600718 }
719
Matt Spinlera26f1522021-08-25 15:50:20 -0500720 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800721 }
722 return;
723}
724
725std::optional<std::string>
726 Manager::getPowerLabelFunctionID(const std::string& value)
727{
728 // If the value is "system", then the FunctionID is "system".
729 if (value == "system")
730 {
731 return value;
732 }
733
734 // If the value is not "system", then the label value have 3 numbers, of
735 // which we only care about the middle one:
736 // <sensor id>_<function id>_<apss channel>
737 // eg: The value is "0_10_5" , then the FunctionID is "10".
738 if (value.find("_") == std::string::npos)
739 {
740 return std::nullopt;
741 }
742
743 auto powerLabelValue = value.substr((value.find("_") + 1));
744
745 if (powerLabelValue.find("_") == std::string::npos)
746 {
747 return std::nullopt;
748 }
749
750 return powerLabelValue.substr(0, powerLabelValue.find("_"));
751}
752
753void Manager::readPowerSensors(const fs::path& path, uint32_t id)
754{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800755 std::regex expr{"power\\d+_label$"}; // Example: power5_label
756 for (auto& file : fs::directory_iterator(path))
757 {
758 if (!std::regex_search(file.path().string(), expr))
759 {
760 continue;
761 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800762
Matt Spinlera26f1522021-08-25 15:50:20 -0500763 std::string labelValue;
764 try
765 {
766 labelValue = readFile<std::string>(file.path());
767 }
768 catch (const std::system_error& e)
769 {
770 log<level::DEBUG>(
771 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
772 file.path().string(), e.code().value())
773 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800774 continue;
775 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800776
777 auto functionID = getPowerLabelFunctionID(labelValue);
778 if (functionID == std::nullopt)
779 {
780 continue;
781 }
782
783 const std::string& tempLabel = "label";
784 const std::string filePathString = file.path().string().substr(
785 0, file.path().string().length() - tempLabel.length());
786
787 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
788
789 auto iter = powerSensorName.find(*functionID);
790 if (iter == powerSensorName.end())
791 {
792 continue;
793 }
794 sensorPath.append(iter->second);
795
Matt Spinlera26f1522021-08-25 15:50:20 -0500796 double tempValue{0};
797
798 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800799 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500800 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800801 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500802 catch (const std::system_error& e)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800803 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800804 log<level::DEBUG>(
Chris Cain5d66a0a2022-02-09 08:52:10 -0600805 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
Matt Spinlera26f1522021-08-25 15:50:20 -0500806 filePathString + inputSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800807 .c_str());
Matt Spinlera26f1522021-08-25 15:50:20 -0500808 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800809 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500810
Chris Cain5d66a0a2022-02-09 08:52:10 -0600811 dbus::OccDBusSensors::getOccDBus().setUnit(
Chris Caind84a8332022-01-13 08:58:45 -0600812 sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts");
813
Chris Cain5d66a0a2022-02-09 08:52:10 -0600814 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500815 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
816
Chris Cain5d66a0a2022-02-09 08:52:10 -0600817 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
818 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500819
Matt Spinler5901abd2021-09-23 13:50:03 -0500820 if (existingSensors.find(sensorPath) == existingSensors.end())
821 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600822 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
823 sensorPath);
Matt Spinler5901abd2021-09-23 13:50:03 -0500824 }
825
Matt Spinlera26f1522021-08-25 15:50:20 -0500826 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800827 }
828 return;
829}
830
831void Manager::setSensorValueToNaN(uint32_t id)
832{
833 for (const auto& [sensorPath, occId] : existingSensors)
834 {
835 if (occId == id)
836 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600837 dbus::OccDBusSensors::getOccDBus().setValue(
Chicago Duanbb895cb2021-06-18 19:37:16 +0800838 sensorPath, std::numeric_limits<double>::quiet_NaN());
839 }
840 }
841 return;
842}
843
Sheldon Bailey373af752022-02-21 15:14:00 -0600844void Manager::setSensorValueToNonFunctional(uint32_t id) const
845{
846 for (const auto& [sensorPath, occId] : existingSensors)
847 {
848 if (occId == id)
849 {
850 dbus::OccDBusSensors::getOccDBus().setValue(
851 sensorPath, std::numeric_limits<double>::quiet_NaN());
852
853 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
854 false);
855 }
856 }
857 return;
858}
859
Chris Cain5d66a0a2022-02-09 08:52:10 -0600860void Manager::getSensorValues(std::unique_ptr<Status>& occ)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800861{
Chris Caine2d0a432022-03-28 11:08:49 -0500862 static bool tracedError[8] = {0};
863 const fs::path sensorPath = occ->getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600864 const uint32_t id = occ->getOccInstanceID();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800865
Chris Caine2d0a432022-03-28 11:08:49 -0500866 if (fs::exists(sensorPath))
Chicago Duanbb895cb2021-06-18 19:37:16 +0800867 {
Chris Caine2d0a432022-03-28 11:08:49 -0500868 // Read temperature sensors
869 readTempSensors(sensorPath, id);
870
871 if (occ->isMasterOcc())
872 {
873 // Read power sensors
874 readPowerSensors(sensorPath, id);
875 }
876 tracedError[id] = false;
877 }
878 else
879 {
880 if (!tracedError[id])
881 {
882 log<level::ERR>(
883 fmt::format(
884 "Manager::getSensorValues: OCC{} sensor path missing: {}",
885 id, sensorPath.c_str())
886 .c_str());
887 tracedError[id] = true;
888 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800889 }
890
891 return;
892}
893#endif
Chris Cain17257672021-10-22 13:41:03 -0500894
895// Read the altitude from DBus
896void Manager::readAltitude()
897{
898 static bool traceAltitudeErr = true;
899
900 utils::PropertyValue altitudeProperty{};
901 try
902 {
903 altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE,
904 ALTITUDE_PROP);
905 auto sensorVal = std::get<double>(altitudeProperty);
906 if (sensorVal < 0xFFFF)
907 {
908 if (sensorVal < 0)
909 {
910 altitude = 0;
911 }
912 else
913 {
914 // Round to nearest meter
915 altitude = uint16_t(sensorVal + 0.5);
916 }
917 log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)",
918 sensorVal, altitude)
919 .c_str());
920 traceAltitudeErr = true;
921 }
922 else
923 {
924 if (traceAltitudeErr)
925 {
926 traceAltitudeErr = false;
927 log<level::DEBUG>(
928 fmt::format("Invalid altitude value: {}", sensorVal)
929 .c_str());
930 }
931 }
932 }
933 catch (const sdbusplus::exception::exception& e)
934 {
935 if (traceAltitudeErr)
936 {
937 traceAltitudeErr = false;
938 log<level::INFO>(
939 fmt::format("Unable to read Altitude: {}", e.what()).c_str());
940 }
941 altitude = 0xFFFF; // not available
942 }
943}
944
945// Callback function when ambient temperature changes
946void Manager::ambientCallback(sdbusplus::message::message& msg)
947{
948 double currentTemp = 0;
949 uint8_t truncatedTemp = 0xFF;
950 std::string msgSensor;
951 std::map<std::string, std::variant<double>> msgData;
952 msg.read(msgSensor, msgData);
953
954 auto valPropMap = msgData.find(AMBIENT_PROP);
955 if (valPropMap == msgData.end())
956 {
957 log<level::DEBUG>("ambientCallback: Unknown ambient property changed");
958 return;
959 }
960 currentTemp = std::get<double>(valPropMap->second);
961 if (std::isnan(currentTemp))
962 {
963 truncatedTemp = 0xFF;
964 }
965 else
966 {
967 if (currentTemp < 0)
968 {
969 truncatedTemp = 0;
970 }
971 else
972 {
973 // Round to nearest degree C
974 truncatedTemp = uint8_t(currentTemp + 0.5);
975 }
976 }
977
978 // If ambient changes, notify OCCs
979 if (truncatedTemp != ambient)
980 {
981 log<level::DEBUG>(
982 fmt::format("ambientCallback: Ambient change from {} to {}C",
983 ambient, currentTemp)
984 .c_str());
985
986 ambient = truncatedTemp;
987 if (altitude == 0xFFFF)
988 {
989 // No altitude yet, try reading again
990 readAltitude();
991 }
992
993 log<level::DEBUG>(
994 fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient,
995 altitude)
996 .c_str());
997#ifdef POWER10
998 // Send ambient and altitude to all OCCs
999 for (auto& obj : statusObjects)
1000 {
1001 if (obj->occActive())
1002 {
1003 obj->sendAmbient(ambient, altitude);
1004 }
1005 }
1006#endif // POWER10
1007 }
1008}
1009
1010// return the current ambient and altitude readings
1011void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
1012 uint16_t& altitudeValue) const
1013{
1014 ambientValid = true;
1015 ambientTemp = ambient;
1016 altitudeValue = altitude;
1017
1018 if (ambient == 0xFF)
1019 {
1020 ambientValid = false;
1021 }
1022}
1023
Chris Caina7b74dc2021-11-10 17:03:43 -06001024#ifdef POWER10
1025void Manager::occsNotAllRunning()
1026{
Chris Cain6fa848a2022-01-24 14:54:38 -06001027 // Function will also gets called when occ-control app gets
1028 // restarted. (occ active sensors do not change, so the Status
1029 // object does not call Manager back for all OCCs)
Chris Caina7b74dc2021-11-10 17:03:43 -06001030
1031 if (activeCount != statusObjects.size())
1032 {
1033 // Not all OCCs went active
1034 log<level::WARNING>(
1035 fmt::format(
1036 "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})",
1037 activeCount, statusObjects.size())
1038 .c_str());
1039 // Procs may be garded, so may not need reset.
1040 }
1041
1042 validateOccMaster();
1043}
1044#endif // POWER10
1045
1046// Verify single master OCC and start presence monitor
1047void Manager::validateOccMaster()
1048{
1049 int masterInstance = -1;
1050 for (auto& obj : statusObjects)
1051 {
Chris Caina7b74dc2021-11-10 17:03:43 -06001052 if (obj->isMasterOcc())
1053 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001054 obj->addPresenceWatchMaster();
1055
Chris Caina7b74dc2021-11-10 17:03:43 -06001056 if (masterInstance == -1)
1057 {
1058 masterInstance = obj->getOccInstanceID();
1059 }
1060 else
1061 {
1062 log<level::ERR>(
1063 fmt::format(
1064 "validateOccMaster: Multiple OCC masters! ({} and {})",
1065 masterInstance, obj->getOccInstanceID())
1066 .c_str());
1067 // request reset
1068 obj->deviceError();
1069 }
1070 }
1071 }
1072 if (masterInstance < 0)
1073 {
1074 log<level::ERR>("validateOccMaster: Master OCC not found!");
1075 // request reset
1076 statusObjects.front()->deviceError();
1077 }
1078 else
1079 {
1080 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -06001081 fmt::format("validateOccMaster: OCC{} is master of {} OCCs",
1082 masterInstance, activeCount)
Chris Caina7b74dc2021-11-10 17:03:43 -06001083 .c_str());
1084 }
1085}
1086
Chris Cain40501a22022-03-14 17:33:27 -05001087void Manager::updatePcapBounds() const
1088{
1089 if (pcap)
1090 {
1091 pcap->updatePcapBounds();
1092 }
1093}
1094
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301095} // namespace occ
1096} // namespace open_power