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