summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/sensors/dbus-sensors/0009-CPUSensor-Create-CPUConfig-for-each-PECI-adapter.patch
blob: 737ba91287222023c766c898f57acca50be5cd7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
From 262682632ee493d0b6593540cfc902d11286b7c3 Mon Sep 17 00:00:00 2001
From: Iwona Winiarska <iwona.winiarska@intel.com>
Date: Tue, 13 Jul 2021 15:16:16 +0200
Subject: [PATCH] CPUSensor: Create CPUConfig for each PECI adapter

Currently CPUSensor is based on configuration that defines CPUs for the
specific PECI adapter (usually peci-wire). As a consequence, MFD devices
are created only for the defined adapter.

Since duplicating static CPU records in configuration file for other
PECI adapters may be confusing to users, let's add CPUConfig for other
PECI adapters dynamically by detecting if there is more than one PECI
adapter driver bound in the system.

Please note, that this change is limited to enabling HWMON driver,
sensors for all adapters will not be exposed on D-Bus.

Tested:
No changes if only one PECI interface is available, HWMON drivers
creation will be based on provided configuration.
When both peci-aspeed (peci-wire) and peci-mctp are bound in the system,
HWMON drivers are created for each PECI interface.

Signed-off-by: Iwona Winiarska <iwona.winiarska@intel.com>
---
 src/CPUSensorMain.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)

diff --git a/src/CPUSensorMain.cpp b/src/CPUSensorMain.cpp
index 744ca50..6850df7 100644
--- a/src/CPUSensorMain.cpp
+++ b/src/CPUSensorMain.cpp
@@ -33,6 +33,7 @@
 #include <fstream>
 #include <functional>
 #include <memory>
+#include <optional>
 #include <regex>
 #include <sstream>
 #include <stdexcept>
@@ -658,6 +659,91 @@ void detectCpuAsync(
     });
 }
 
+std::optional<uint64_t> getPeciDeviceNum(const fs::path& peciAdapterNamePath)
+{
+    fs::path::iterator it = peciAdapterNamePath.begin();
+    std::advance(it, 5); // /sys/bus/peci/devices/peci-xxxx
+    std::string peciDeviceName = *it;
+    auto pos = peciDeviceName.find('-');
+    if (pos == std::string::npos)
+    {
+        std::cerr << "Incorrect PECI device name: " << peciDeviceName << "\n";
+        return std::nullopt;
+    }
+
+    try
+    {
+        return std::stoull(peciDeviceName.substr(pos + 1, 1));
+    }
+    catch (std::logic_error&)
+    {
+        return std::nullopt;
+    }
+}
+
+std::optional<std::string>
+    readPeciAdapterNameFromFile(const fs::path& peciAdapterNamePath)
+{
+    std::ifstream nameFile(peciAdapterNamePath);
+    if (!nameFile.good())
+    {
+        std::cerr << "Cannot read: " << peciAdapterNamePath << "\n";
+        return std::nullopt;
+    }
+
+    std::string peciAdapterName;
+    std::getline(nameFile, peciAdapterName);
+    nameFile.close();
+    if (peciAdapterName.empty())
+    {
+        return std::nullopt;
+    }
+
+    auto pos = peciAdapterName.find('-');
+    peciAdapterName = peciAdapterName.substr(pos + 1);
+
+    return peciAdapterName;
+}
+
+void addConfigsForOtherPeciAdapters(
+    boost::container::flat_set<CPUConfig>& cpuConfigs, uint64_t& bus,
+    uint64_t& addr, std::string& name, const State& state)
+{
+    std::vector<fs::path> peciAdapterNamePaths;
+    if (!findFiles(fs::path(peciDevPath), R"(peci-\d+/name$)",
+                   peciAdapterNamePaths, 1))
+    {
+        std::cerr << "No PECI adapters in system\n";
+        return;
+    }
+
+    for (const fs::path& peciAdapterNamePath : peciAdapterNamePaths)
+    {
+        std::optional<uint64_t> peciDeviceNum =
+            getPeciDeviceNum(peciAdapterNamePath);
+        if (!peciDeviceNum || peciDeviceNum == bus)
+        {
+            continue;
+        }
+
+        std::optional<std::string> peciAdapterName =
+            readPeciAdapterNameFromFile(peciAdapterNamePath);
+        if (!peciAdapterName)
+        {
+            continue;
+        }
+
+        // Change result for peci-aspeed
+        if (peciAdapterName->compare("bus") == 0)
+        {
+            peciAdapterName = "wire";
+        }
+
+        cpuConfigs.emplace(*peciDeviceNum, addr, name + "_" + *peciAdapterName,
+                           state);
+    }
+}
+
 bool getCpuConfig(std::shared_ptr<sdbusplus::asio::connection>& systemBus,
                   boost::container::flat_set<CPUConfig>& cpuConfigs,
                   ManagedObjectType& sensorConfigs, boost::asio::io_service& io,
@@ -789,7 +875,11 @@ bool getCpuConfig(std::shared_ptr<sdbusplus::asio::connection>& systemBus,
                     std::cout << "name: " << name << "\n";
                     std::cout << "type: " << type << "\n";
                 }
+
                 cpuConfigs.emplace(bus, addr, name, State::OFF);
+
+                addConfigsForOtherPeciAdapters(cpuConfigs, bus, addr, name,
+                                               State::OFF);
             }
         }
     }
-- 
2.31.1