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