blob: 040676bca3ef010480667e2447f773b21fef24ab [file] [log] [blame]
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301#include <experimental/filesystem>
2#include <phosphor-logging/log.hpp>
3#include <phosphor-logging/elog-errors.hpp>
4#include <xyz/openbmc_project/Common/error.hpp>
5#include "occ_finder.hpp"
6#include "occ_manager.hpp"
7#include "i2c_occ.hpp"
8#include "utils.hpp"
9#include "config.h"
10
11namespace open_power
12{
13namespace occ
14{
15
16void Manager::findAndCreateObjects()
17{
Deepak Kodihalli370f06b2017-10-25 04:26:07 -050018 for (auto id = 0; id < MAX_CPUS; ++id)
19 {
Deepak Kodihalli30417a12017-12-04 00:54:01 -060020 // Create one occ per cpu
21 auto occ = std::string(OCC_NAME) + std::to_string(id);
22 createObjects(occ);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053023 }
24}
25
26int Manager::cpuCreated(sdbusplus::message::message& msg)
27{
28 namespace fs = std::experimental::filesystem;
29
30 sdbusplus::message::object_path o;
31 msg.read(o);
32 fs::path cpuPath(std::string(std::move(o)));
33
34 auto name = cpuPath.filename().string();
35 auto index = name.find(CPU_NAME);
36 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
37
38 createObjects(name);
39
40 return 0;
41}
42
43void Manager::createObjects(const std::string& occ)
44{
45 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
46
47 passThroughObjects.emplace_back(
48 std::make_unique<PassThrough>(
49 bus,
50 path.c_str()));
51
52 statusObjects.emplace_back(
53 std::make_unique<Status>(
54 bus,
55 event,
56 path.c_str(),
Edward A. James636577f2017-10-06 10:53:55 -050057 *this,
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053058 std::bind(std::mem_fn(&Manager::statusCallBack),
59 this, std::placeholders::_1)));
60
61 // Create the power cap monitor object for master occ (0)
62 if (!pcap)
63 {
64 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
65 bus,
66 *statusObjects.front());
67 }
68}
69
70void Manager::statusCallBack(bool status)
71{
72 using namespace phosphor::logging;
73 using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
74 Error::InternalFailure;
75
76 // At this time, it won't happen but keeping it
77 // here just in case something changes in the future
78 if ((activeCount == 0) && (!status))
79 {
80 log<level::ERR>("Invalid update on OCCActive");
81 elog<InternalFailure>();
82 }
83
84 activeCount += status ? 1 : -1;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053085}
86
87#ifdef I2C_OCC
88void Manager::initStatusObjects()
89{
90 // Make sure we have a valid path string
91 static_assert(sizeof(DEV_PATH) != 0);
92
93 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
Lei YU41470e52017-11-30 16:03:50 +080094 auto occMasterName = deviceNames.front();
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053095 for (auto& name : deviceNames)
96 {
97 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +080098 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053099 auto path = fs::path(OCC_CONTROL_ROOT) / name;
100 statusObjects.emplace_back(
Lei YUb5259a12017-09-01 16:22:40 +0800101 std::make_unique<Status>(
102 bus,
103 event,
Edward A. James636577f2017-10-06 10:53:55 -0500104 path.c_str(),
105 *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530106 }
Lei YU41470e52017-11-30 16:03:50 +0800107 // The first device is master occ
108 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
109 bus,
110 *statusObjects.front(),
111 occMasterName);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530112}
113#endif
114
115} // namespace occ
116} // namespace open_power