blob: d420eeaad4e61cd2759920c440c5f65c7f980fa2 [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"
6#include "occ_finder.hpp"
7#include "utils.hpp"
8
9#include <experimental/filesystem>
10#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/log.hpp>
12#include <xyz/openbmc_project/Common/error.hpp>
13
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053014namespace open_power
15{
16namespace occ
17{
18
19void Manager::findAndCreateObjects()
20{
Deepak Kodihalli370f06b2017-10-25 04:26:07 -050021 for (auto id = 0; id < MAX_CPUS; ++id)
22 {
Deepak Kodihalli30417a12017-12-04 00:54:01 -060023 // Create one occ per cpu
24 auto occ = std::string(OCC_NAME) + std::to_string(id);
25 createObjects(occ);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053026 }
27}
28
29int Manager::cpuCreated(sdbusplus::message::message& msg)
30{
31 namespace fs = std::experimental::filesystem;
32
33 sdbusplus::message::object_path o;
34 msg.read(o);
35 fs::path cpuPath(std::string(std::move(o)));
36
37 auto name = cpuPath.filename().string();
38 auto index = name.find(CPU_NAME);
39 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
40
41 createObjects(name);
42
43 return 0;
44}
45
46void Manager::createObjects(const std::string& occ)
47{
48 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
49
50 passThroughObjects.emplace_back(
Gunnar Mills94df8c92018-09-14 14:50:03 -050051 std::make_unique<PassThrough>(bus, path.c_str()));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053052
Gunnar Mills94df8c92018-09-14 14:50:03 -050053 statusObjects.emplace_back(std::make_unique<Status>(
54 bus, event, path.c_str(), *this,
55 std::bind(std::mem_fn(&Manager::statusCallBack), this,
56 std::placeholders::_1)));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053057
58 // Create the power cap monitor object for master occ (0)
59 if (!pcap)
60 {
61 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
Gunnar Mills94df8c92018-09-14 14:50:03 -050062 bus, *statusObjects.front());
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053063 }
64}
65
66void Manager::statusCallBack(bool status)
67{
68 using namespace phosphor::logging;
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 using InternalFailure =
70 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053071
72 // At this time, it won't happen but keeping it
73 // here just in case something changes in the future
74 if ((activeCount == 0) && (!status))
75 {
76 log<level::ERR>("Invalid update on OCCActive");
77 elog<InternalFailure>();
78 }
79
80 activeCount += status ? 1 : -1;
Eddie Jamesdae2d942017-12-20 10:50:03 -060081
82 // Only start presence detection if all the OCCs are bound
83 if (activeCount == statusObjects.size())
84 {
Gunnar Mills94df8c92018-09-14 14:50:03 -050085 for (auto& obj : statusObjects)
Eddie Jamesdae2d942017-12-20 10:50:03 -060086 {
87 obj->addPresenceWatchMaster();
88 }
89 }
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053090}
91
92#ifdef I2C_OCC
93void Manager::initStatusObjects()
94{
95 // Make sure we have a valid path string
96 static_assert(sizeof(DEV_PATH) != 0);
97
98 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
Lei YU41470e52017-11-30 16:03:50 +080099 auto occMasterName = deviceNames.front();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530100 for (auto& name : deviceNames)
101 {
102 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +0800103 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530104 auto path = fs::path(OCC_CONTROL_ROOT) / name;
105 statusObjects.emplace_back(
Gunnar Mills94df8c92018-09-14 14:50:03 -0500106 std::make_unique<Status>(bus, event, path.c_str(), *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530107 }
Lei YU41470e52017-11-30 16:03:50 +0800108 // The first device is master occ
109 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
Gunnar Mills94df8c92018-09-14 14:50:03 -0500110 bus, *statusObjects.front(), occMasterName);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530111}
112#endif
113
114} // namespace occ
115} // namespace open_power