James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "CPUSensor.hpp" |
| 18 | #include "Utils.hpp" |
| 19 | #include "VariantVisitors.hpp" |
| 20 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <array> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/predicate.hpp> |
| 25 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 26 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | #include <boost/container/flat_set.hpp> |
| 28 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 29 | #include <boost/process/child.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 30 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <functional> |
| 33 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | #include <regex> |
| 35 | #include <sdbusplus/asio/connection.hpp> |
| 36 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 37 | #include <sdbusplus/bus/match.hpp> |
| 38 | #include <sstream> |
| 39 | #include <stdexcept> |
| 40 | #include <string> |
| 41 | #include <utility> |
| 42 | #include <variant> |
| 43 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 45 | // clang-format off |
| 46 | // this needs to be included last or we'll have build issues |
| 47 | #include <linux/peci-ioctl.h> |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 48 | #if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP) |
| 49 | #define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP |
| 50 | #endif |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 51 | // clang-format on |
| 52 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | static constexpr bool DEBUG = false; |
| 54 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 55 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors; |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 56 | boost::container::flat_map<std::string, |
| 57 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 58 | inventoryIfaces; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 59 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | enum State |
| 61 | { |
| 62 | OFF, // host powered down |
| 63 | ON, // host powered on |
| 64 | READY // host powered on and mem test passed - fully ready |
| 65 | }; |
| 66 | |
| 67 | struct CPUConfig |
| 68 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 69 | CPUConfig(const uint64_t& bus, const uint64_t& addr, |
| 70 | const std::string& name, const State& state) : |
| 71 | bus(bus), |
| 72 | addr(addr), name(name), state(state) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | { |
| 74 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 75 | int bus; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | int addr; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 77 | std::string name; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 78 | State state; |
| 79 | |
| 80 | bool operator<(const CPUConfig& rhs) const |
| 81 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 82 | return (name < rhs.name); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 86 | static constexpr const char* peciDev = "/dev/peci-"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | static constexpr const unsigned int rankNumMax = 8; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 88 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 89 | namespace fs = std::filesystem; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 90 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 91 | static constexpr const char* configPrefix = |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 92 | "xyz.openbmc_project.Configuration."; |
Jae Hyun Yoo | 7d47bf5 | 2019-04-23 16:43:50 -0700 | [diff] [blame] | 93 | static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"}; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 94 | static constexpr std::array<const char*, 3> hiddenProps = { |
| 95 | CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 96 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 97 | void detectCpuAsync( |
| 98 | boost::asio::deadline_timer& pingTimer, |
| 99 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, |
| 100 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 101 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 102 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 103 | ManagedObjectType& sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 104 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 105 | bool createSensors(boost::asio::io_service& io, |
| 106 | sdbusplus::asio::object_server& objectServer, |
| 107 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 108 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 109 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | { |
| 111 | bool available = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 112 | for (const CPUConfig& cpu : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | { |
| 114 | if (cpu.state != State::OFF) |
| 115 | { |
| 116 | available = true; |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 117 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 118 | inventoryIfaces[cpu.name]; |
| 119 | if (iface != nullptr) |
| 120 | { |
| 121 | continue; |
| 122 | } |
| 123 | iface = objectServer.add_interface( |
| 124 | cpuInventoryPath + std::string("/") + cpu.name, |
| 125 | "xyz.openbmc_project.Inventory.Item"); |
| 126 | iface->register_property("PrettyName", cpu.name); |
| 127 | iface->register_property("Present", true); |
| 128 | iface->initialize(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | if (!available) |
| 132 | { |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 133 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 136 | if (sensorConfigs.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 137 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 138 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 141 | std::vector<fs::path> hwmonNamePaths; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 142 | if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"), |
| 143 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 144 | hwmonNamePaths, 1)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | { |
| 146 | std::cerr << "No CPU sensors in system\n"; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 147 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 150 | boost::container::flat_set<std::string> scannedDirectories; |
| 151 | boost::container::flat_set<std::string> createdSensors; |
| 152 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 153 | for (const fs::path& hwmonNamePath : hwmonNamePaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 155 | const std::string& pathStr = hwmonNamePath.string(); |
| 156 | auto hwmonDirectory = hwmonNamePath.parent_path(); |
| 157 | |
| 158 | auto ret = scannedDirectories.insert(hwmonDirectory.string()); |
| 159 | if (!ret.second) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 161 | continue; // already searched this path |
| 162 | } |
| 163 | |
| 164 | fs::path::iterator it = hwmonNamePath.begin(); |
| 165 | std::advance(it, 6); // pick the 6th part for a PECI client device name |
| 166 | std::string deviceName = *it; |
| 167 | auto findHyphen = deviceName.find("-"); |
| 168 | if (findHyphen == std::string::npos) |
| 169 | { |
| 170 | std::cerr << "found bad device " << deviceName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 173 | std::string busStr = deviceName.substr(0, findHyphen); |
| 174 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 175 | |
| 176 | size_t bus = 0; |
| 177 | size_t addr = 0; |
| 178 | try |
| 179 | { |
| 180 | bus = std::stoi(busStr); |
| 181 | addr = std::stoi(addrStr, 0, 16); |
| 182 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 183 | catch (std::invalid_argument&) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 184 | { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | std::ifstream nameFile(hwmonNamePath); |
| 189 | if (!nameFile.good()) |
| 190 | { |
| 191 | std::cerr << "Failure reading " << hwmonNamePath << "\n"; |
| 192 | continue; |
| 193 | } |
| 194 | std::string hwmonName; |
| 195 | std::getline(nameFile, hwmonName); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 196 | nameFile.close(); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 197 | if (hwmonName.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 198 | { |
| 199 | // shouldn't have an empty name file |
| 200 | continue; |
| 201 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 202 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 203 | { |
| 204 | std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName |
| 205 | << "\n"; |
| 206 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 207 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 208 | std::string sensorType; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 209 | const SensorData* sensorData = nullptr; |
| 210 | const std::string* interfacePath = nullptr; |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 211 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 212 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 213 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 214 | sensor : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 215 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 216 | sensorData = &(sensor.second); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 217 | for (const char* type : sensorTypes) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 218 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 219 | sensorType = configPrefix + std::string(type); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 220 | auto sensorBase = sensorData->find(sensorType); |
| 221 | if (sensorBase != sensorData->end()) |
| 222 | { |
| 223 | baseConfiguration = &(*sensorBase); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (baseConfiguration == nullptr) |
| 228 | { |
| 229 | std::cerr << "error finding base configuration for" << hwmonName |
| 230 | << "\n"; |
| 231 | continue; |
| 232 | } |
| 233 | auto configurationBus = baseConfiguration->second.find("Bus"); |
| 234 | auto configurationAddress = |
| 235 | baseConfiguration->second.find("Address"); |
| 236 | |
| 237 | if (configurationBus == baseConfiguration->second.end() || |
| 238 | configurationAddress == baseConfiguration->second.end()) |
| 239 | { |
| 240 | std::cerr << "error finding bus or address in configuration"; |
| 241 | continue; |
| 242 | } |
| 243 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 244 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 245 | std::get<uint64_t>(configurationAddress->second) != addr) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 246 | { |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | interfacePath = &(sensor.first.str); |
| 251 | break; |
| 252 | } |
| 253 | if (interfacePath == nullptr) |
| 254 | { |
| 255 | std::cerr << "failed to find match for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 256 | continue; |
| 257 | } |
| 258 | |
| 259 | auto findCpuId = baseConfiguration->second.find("CpuID"); |
| 260 | if (findCpuId == baseConfiguration->second.end()) |
| 261 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 262 | std::cerr << "could not determine CPU ID for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 265 | int cpuId = |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 266 | std::visit(VariantToUnsignedIntVisitor(), findCpuId->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 267 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 268 | auto directory = hwmonNamePath.parent_path(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 269 | std::vector<fs::path> inputPaths; |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 270 | if (!findFiles(directory, R"(temp\d+_input$)", inputPaths, 0)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 271 | { |
| 272 | std::cerr << "No temperature sensors in system\n"; |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | // iterate through all found temp sensors |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 277 | for (const auto& inputPath : inputPaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 278 | { |
| 279 | auto inputPathStr = inputPath.string(); |
| 280 | auto labelPath = |
| 281 | boost::replace_all_copy(inputPathStr, "input", "label"); |
| 282 | std::ifstream labelFile(labelPath); |
| 283 | if (!labelFile.good()) |
| 284 | { |
| 285 | std::cerr << "Failure reading " << labelPath << "\n"; |
| 286 | continue; |
| 287 | } |
| 288 | std::string label; |
| 289 | std::getline(labelFile, label); |
| 290 | labelFile.close(); |
Jae Hyun Yoo | 13f4888 | 2019-02-19 13:37:07 -0800 | [diff] [blame] | 291 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 292 | std::string sensorName = label + " CPU" + std::to_string(cpuId); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 293 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 294 | auto findSensor = gCpuSensors.find(sensorName); |
| 295 | if (findSensor != gCpuSensors.end()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 296 | { |
| 297 | if (DEBUG) |
| 298 | { |
| 299 | std::cout << "Skipped: " << inputPath << ": " << sensorName |
| 300 | << " is already created\n"; |
| 301 | } |
| 302 | continue; |
| 303 | } |
| 304 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 305 | // check hidden properties |
| 306 | bool show = true; |
| 307 | for (const char* prop : hiddenProps) |
| 308 | { |
| 309 | if (label == prop) |
| 310 | { |
| 311 | show = false; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 316 | /* |
| 317 | * Find if there is DtsCritOffset is configured in config file |
| 318 | * set it if configured or else set it to 0 |
| 319 | */ |
| 320 | double dtsOffset = 0; |
| 321 | if (label == "DTS") |
| 322 | { |
| 323 | auto findThrOffset = |
| 324 | baseConfiguration->second.find("DtsCritOffset"); |
| 325 | if (findThrOffset != baseConfiguration->second.end()) |
| 326 | { |
| 327 | dtsOffset = std::visit(VariantToDoubleVisitor(), |
| 328 | findThrOffset->second); |
| 329 | } |
| 330 | } |
| 331 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 332 | std::vector<thresholds::Threshold> sensorThresholds; |
| 333 | std::string labelHead = label.substr(0, label.find(" ")); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 334 | parseThresholdsFromConfig(*sensorData, sensorThresholds, |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 335 | &labelHead); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 336 | if (sensorThresholds.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 337 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 338 | if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 339 | CPUSensor::sensorScaleFactor, |
| 340 | dtsOffset)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 341 | { |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 342 | std::cerr << "error populating thresholds for " |
| 343 | << sensorName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 346 | auto& sensorPtr = gCpuSensors[sensorName]; |
| 347 | // make sure destructor fires before creating a new one |
| 348 | sensorPtr = nullptr; |
| 349 | sensorPtr = std::make_unique<CPUSensor>( |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 350 | inputPathStr, sensorType, objectServer, dbusConnection, io, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 351 | sensorName, std::move(sensorThresholds), *interfacePath, cpuId, |
Vijay Khemka | 86dea2b | 2019-06-06 11:14:37 -0700 | [diff] [blame] | 352 | show, dtsOffset); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 353 | createdSensors.insert(sensorName); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 354 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 355 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 356 | std::cout << "Mapped: " << inputPath << " to " << sensorName |
| 357 | << "\n"; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 358 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 361 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 362 | if (createdSensors.size()) |
| 363 | { |
| 364 | std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are") |
| 365 | << " created\n"; |
| 366 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 367 | |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 368 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 371 | void exportDevice(const CPUConfig& config) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 372 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 373 | std::ostringstream hex; |
| 374 | hex << std::hex << config.addr; |
| 375 | const std::string& addrHexStr = hex.str(); |
| 376 | std::string busStr = std::to_string(config.bus); |
| 377 | |
| 378 | std::string parameters = "peci-client 0x" + addrHexStr; |
| 379 | std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device"; |
| 380 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 381 | std::filesystem::path devicePath(device); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 382 | const std::string& dir = devicePath.parent_path().string(); |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 383 | for (const auto& path : std::filesystem::directory_iterator(dir)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 384 | { |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 385 | if (!std::filesystem::is_directory(path)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 386 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 387 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 390 | const std::string& directoryName = path.path().filename(); |
| 391 | if (boost::starts_with(directoryName, busStr) && |
| 392 | boost::ends_with(directoryName, addrHexStr)) |
| 393 | { |
| 394 | if (DEBUG) |
| 395 | { |
| 396 | std::cout << parameters << " on bus " << busStr |
| 397 | << " is already exported\n"; |
| 398 | } |
| 399 | return; |
| 400 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 403 | std::ofstream deviceFile(device); |
| 404 | if (!deviceFile.good()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 405 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 406 | std::cerr << "Error writing " << device << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 407 | return; |
| 408 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 409 | deviceFile << parameters; |
| 410 | deviceFile.close(); |
| 411 | |
| 412 | std::cout << parameters << " on bus " << busStr << " is exported\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 415 | void detectCpu(boost::asio::deadline_timer& pingTimer, |
| 416 | boost::asio::deadline_timer& creationTimer, |
| 417 | boost::asio::io_service& io, |
| 418 | sdbusplus::asio::object_server& objectServer, |
| 419 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 420 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 421 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 422 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 423 | size_t rescanDelaySeconds = 0; |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 424 | static bool keepPinging = false; |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 425 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 426 | for (CPUConfig& config : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 427 | { |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 428 | std::string peciDevPath = peciDev + std::to_string(config.bus); |
| 429 | auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC); |
| 430 | if (file < 0) |
| 431 | { |
| 432 | std::cerr << "unable to open " << peciDevPath << "\n"; |
| 433 | std::exit(EXIT_FAILURE); |
| 434 | } |
| 435 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 436 | State newState; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 437 | struct peci_ping_msg msg; |
| 438 | msg.addr = config.addr; |
| 439 | if (!ioctl(file, PECI_IOC_PING, &msg)) |
| 440 | { |
| 441 | bool dimmReady = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 442 | for (unsigned int rank = 0; rank < rankNumMax; rank++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 443 | { |
| 444 | struct peci_rd_pkg_cfg_msg msg; |
| 445 | msg.addr = config.addr; |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 446 | msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 447 | msg.param = rank; |
| 448 | msg.rx_len = 4; |
| 449 | if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg)) |
| 450 | { |
| 451 | if (msg.pkg_config[0] || msg.pkg_config[1] || |
| 452 | msg.pkg_config[2]) |
| 453 | { |
| 454 | dimmReady = true; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | break; |
| 461 | } |
| 462 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 463 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 464 | if (dimmReady) |
| 465 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 466 | newState = State::READY; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 470 | newState = State::ON; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | else |
| 474 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 475 | newState = State::OFF; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 478 | close(file); |
| 479 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 480 | if (config.state != newState) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 481 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 482 | if (newState != State::OFF) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 483 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 484 | if (config.state == State::OFF) |
| 485 | { |
| 486 | std::cout << config.name << " is detected\n"; |
| 487 | exportDevice(config); |
| 488 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 489 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 490 | if (newState == State::ON) |
| 491 | { |
| 492 | rescanDelaySeconds = 3; |
| 493 | } |
| 494 | else if (newState == State::READY) |
| 495 | { |
| 496 | rescanDelaySeconds = 5; |
| 497 | std::cout << "DIMM(s) on " << config.name |
| 498 | << " is/are detected\n"; |
| 499 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 500 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 501 | |
| 502 | config.state = newState; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (config.state != State::READY) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 506 | { |
| 507 | keepPinging = true; |
| 508 | } |
| 509 | |
| 510 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 511 | { |
| 512 | std::cout << config.name << ", state: " << config.state << "\n"; |
| 513 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 514 | } |
| 515 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 516 | if (rescanDelaySeconds) |
| 517 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 518 | creationTimer.expires_from_now( |
| 519 | boost::posix_time::seconds(rescanDelaySeconds)); |
| 520 | creationTimer.async_wait([&](const boost::system::error_code& ec) { |
| 521 | if (ec == boost::asio::error::operation_aborted) |
| 522 | { |
| 523 | return; // we're being canceled |
| 524 | } |
| 525 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 526 | if (!createSensors(io, objectServer, dbusConnection, cpuConfigs, |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 527 | sensorConfigs) || |
| 528 | keepPinging) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 529 | { |
| 530 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 531 | dbusConnection, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 532 | } |
| 533 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 534 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 535 | else if (keepPinging) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 536 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 537 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 538 | dbusConnection, cpuConfigs, sensorConfigs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 542 | void detectCpuAsync( |
| 543 | boost::asio::deadline_timer& pingTimer, |
| 544 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, |
| 545 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 546 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 547 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 548 | ManagedObjectType& sensorConfigs) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 549 | { |
| 550 | pingTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 551 | pingTimer.async_wait([&](const boost::system::error_code& ec) { |
| 552 | if (ec == boost::asio::error::operation_aborted) |
| 553 | { |
| 554 | return; // we're being canceled |
| 555 | } |
| 556 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 557 | detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 558 | cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 559 | }); |
| 560 | } |
| 561 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 562 | bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
| 563 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 564 | ManagedObjectType& sensorConfigs, |
| 565 | sdbusplus::asio::object_server& objectServer) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 566 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 567 | bool useCache = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 568 | sensorConfigs.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 569 | // use new data the first time, then refresh |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 570 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 571 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 572 | if (!getSensorConfiguration(configPrefix + std::string(type), systemBus, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 573 | sensorConfigs, useCache)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 574 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 575 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 576 | } |
| 577 | useCache = true; |
| 578 | } |
| 579 | |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 580 | // check PECI client addresses and names from CPU configuration |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 581 | // before starting ping operation |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 582 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 583 | { |
| 584 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 585 | sensor : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 586 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 587 | for (const SensorBaseConfiguration& config : sensor.second) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 588 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 589 | if ((configPrefix + std::string(type)) != config.first) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 590 | { |
| 591 | continue; |
| 592 | } |
| 593 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 594 | auto findName = config.second.find("Name"); |
| 595 | if (findName == config.second.end()) |
| 596 | { |
| 597 | continue; |
| 598 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 599 | std::string nameRaw = |
| 600 | std::visit(VariantToStringVisitor(), findName->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 601 | std::string name = |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 602 | std::regex_replace(nameRaw, illegalDbusRegex, "_"); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 603 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 604 | auto present = std::optional<bool>(); |
| 605 | // if we can't detect it via gpio, we set presence later |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 606 | for (const SensorBaseConfiguration& suppConfig : sensor.second) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 607 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 608 | if (suppConfig.first.find("PresenceGpio") != |
| 609 | std::string::npos) |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 610 | { |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 611 | present = cpuIsPresent(suppConfig.second); |
| 612 | break; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 613 | } |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 614 | } |
| 615 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 616 | if (inventoryIfaces.find(name) == inventoryIfaces.end() && |
| 617 | present) |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 618 | { |
| 619 | auto iface = objectServer.add_interface( |
| 620 | cpuInventoryPath + std::string("/") + name, |
| 621 | "xyz.openbmc_project.Inventory.Item"); |
| 622 | iface->register_property("PrettyName", name); |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 623 | iface->register_property("Present", *present); |
Jae Hyun Yoo | ffa07e2 | 2019-07-17 10:47:18 -0700 | [diff] [blame] | 624 | iface->initialize(); |
| 625 | inventoryIfaces[name] = std::move(iface); |
| 626 | } |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 627 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 628 | auto findBus = config.second.find("Bus"); |
| 629 | if (findBus == config.second.end()) |
| 630 | { |
| 631 | std::cerr << "Can't find 'Bus' setting in " << name << "\n"; |
| 632 | continue; |
| 633 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 634 | uint64_t bus = |
| 635 | std::visit(VariantToUnsignedIntVisitor(), findBus->second); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 636 | |
| 637 | auto findAddress = config.second.find("Address"); |
| 638 | if (findAddress == config.second.end()) |
| 639 | { |
| 640 | std::cerr << "Can't find 'Address' setting in " << name |
| 641 | << "\n"; |
| 642 | continue; |
| 643 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 644 | uint64_t addr = std::visit(VariantToUnsignedIntVisitor(), |
| 645 | findAddress->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 646 | |
| 647 | if (DEBUG) |
| 648 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 649 | std::cout << "bus: " << bus << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 650 | std::cout << "addr: " << addr << "\n"; |
| 651 | std::cout << "name: " << name << "\n"; |
| 652 | std::cout << "type: " << type << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 655 | cpuConfigs.emplace(bus, addr, name, State::OFF); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 659 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 660 | if (cpuConfigs.size()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 661 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 662 | std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are") |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 663 | << " parsed\n"; |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 668 | } |
| 669 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 670 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 671 | { |
| 672 | boost::asio::io_service io; |
| 673 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 674 | boost::container::flat_set<CPUConfig> cpuConfigs; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 675 | |
| 676 | systemBus->request_name("xyz.openbmc_project.CPUSensor"); |
| 677 | sdbusplus::asio::object_server objectServer(systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 678 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 679 | boost::asio::deadline_timer pingTimer(io); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 680 | boost::asio::deadline_timer creationTimer(io); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 681 | boost::asio::deadline_timer filterTimer(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 682 | ManagedObjectType sensorConfigs; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 683 | |
| 684 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 685 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 686 | if (ec == boost::asio::error::operation_aborted) |
| 687 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 688 | return; // we're being canceled |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 689 | } |
| 690 | |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 691 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer)) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 692 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 693 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 694 | systemBus, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 695 | } |
| 696 | }); |
| 697 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 698 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 699 | [&](sdbusplus::message::message& message) { |
| 700 | if (message.is_method_error()) |
| 701 | { |
| 702 | std::cerr << "callback method error\n"; |
| 703 | return; |
| 704 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 705 | |
| 706 | if (DEBUG) |
| 707 | { |
| 708 | std::cout << message.get_path() << " is changed\n"; |
| 709 | } |
| 710 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 711 | // this implicitly cancels the timer |
| 712 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 713 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 714 | if (ec == boost::asio::error::operation_aborted) |
| 715 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 716 | return; // we're being canceled |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 717 | } |
| 718 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 719 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, |
James Feist | c140e20 | 2019-11-14 15:23:51 -0800 | [diff] [blame^] | 720 | objectServer)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 721 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 722 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 723 | systemBus, cpuConfigs, sensorConfigs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 724 | } |
| 725 | }); |
| 726 | }; |
| 727 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 728 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 729 | { |
| 730 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 731 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 732 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 733 | std::string(inventoryPath) + "',arg0namespace='" + |
| 734 | configPrefix + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 735 | eventHandler); |
| 736 | matches.emplace_back(std::move(match)); |
| 737 | } |
| 738 | |
| 739 | io.run(); |
| 740 | } |