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 | 0003f46 | 2018-10-26 10:09:01 -0700 | [diff] [blame^] | 62 | static constexpr const char* peciDev = "/dev/peci-"; |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 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 | { |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 360 | size_t rescanDelaySeconds = 0; |
| 361 | bool keepPinging = false; |
Jae Hyun Yoo | 0003f46 | 2018-10-26 10:09:01 -0700 | [diff] [blame^] | 362 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 363 | for (CPUConfig& config : configs) |
| 364 | { |
Jae Hyun Yoo | 0003f46 | 2018-10-26 10:09:01 -0700 | [diff] [blame^] | 365 | std::string peciDevPath = peciDev + std::to_string(config.bus); |
| 366 | auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC); |
| 367 | if (file < 0) |
| 368 | { |
| 369 | std::cerr << "unable to open " << peciDevPath << "\n"; |
| 370 | std::exit(EXIT_FAILURE); |
| 371 | } |
| 372 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 373 | State state; |
| 374 | struct peci_ping_msg msg; |
| 375 | msg.addr = config.addr; |
| 376 | if (!ioctl(file, PECI_IOC_PING, &msg)) |
| 377 | { |
| 378 | bool dimmReady = false; |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 379 | for (unsigned int rank = 0; rank < rankNumMax; rank++) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 380 | { |
| 381 | struct peci_rd_pkg_cfg_msg msg; |
| 382 | msg.addr = config.addr; |
| 383 | msg.index = MBX_INDEX_DDR_DIMM_TEMP; |
| 384 | msg.param = rank; |
| 385 | msg.rx_len = 4; |
| 386 | if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg)) |
| 387 | { |
| 388 | if (msg.pkg_config[0] || msg.pkg_config[1] || |
| 389 | msg.pkg_config[2]) |
| 390 | { |
| 391 | dimmReady = true; |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | break; |
| 398 | } |
| 399 | } |
Yoo, Jae Hyun | 5481fac | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 400 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 401 | if (dimmReady) |
| 402 | { |
| 403 | state = State::READY; |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | state = State::ON; |
| 408 | } |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | state = State::OFF; |
| 413 | } |
| 414 | |
Jae Hyun Yoo | 0003f46 | 2018-10-26 10:09:01 -0700 | [diff] [blame^] | 415 | close(file); |
| 416 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 417 | if (config.state != state) |
| 418 | { |
| 419 | if (config.state == State::OFF) |
| 420 | { |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 421 | std::cout << config.name << " is detected\n"; |
| 422 | exportDevice(config); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 423 | } |
Yoo, Jae Hyun | 5481fac | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 424 | if (state == State::READY) |
| 425 | { |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 426 | std::cout << "DIMM(s) on " << config.name |
Yoo, Jae Hyun | 5481fac | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 427 | << " is/are detected\n"; |
| 428 | } |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 429 | config.state = state; |
| 430 | } |
| 431 | |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 432 | if (config.state != State::OFF) |
| 433 | { |
| 434 | if (config.state == State::ON) |
| 435 | { |
| 436 | rescanDelaySeconds = 1; |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | rescanDelaySeconds = 5; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (config.state != State::READY) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 445 | { |
| 446 | keepPinging = true; |
| 447 | } |
| 448 | |
| 449 | if (DEBUG) |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 450 | { |
| 451 | std::cout << config.name << ", state: " << config.state << "\n"; |
| 452 | } |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 453 | } |
| 454 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 455 | if (rescanDelaySeconds) |
| 456 | { |
| 457 | std::this_thread::sleep_for(std::chrono::seconds(rescanDelaySeconds)); |
Yoo, Jae Hyun | 60e14d3 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 458 | if (!createSensors(io, objectServer, sensors, configs, dbusConnection)) |
| 459 | { |
| 460 | keepPinging = true; |
| 461 | } |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if (keepPinging) |
| 465 | { |
| 466 | timer.expires_from_now(boost::posix_time::seconds(1)); |
| 467 | timer.async_wait([&](const boost::system::error_code& ec) { |
| 468 | if (ec == boost::asio::error::operation_aborted) |
| 469 | { |
| 470 | /* we were canceled*/ |
| 471 | return; |
| 472 | } |
| 473 | else if (ec) |
| 474 | { |
| 475 | std::cerr << "timer error\n"; |
| 476 | return; |
| 477 | } |
| 478 | detectCpu(timer, io, objectServer, sensors, configs, |
| 479 | dbusConnection); |
| 480 | }); |
| 481 | } |
| 482 | } |
| 483 | |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 484 | bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 485 | boost::container::flat_set<CPUConfig>& configs) |
| 486 | { |
| 487 | ManagedObjectType sensorConfigurations; |
| 488 | bool useCache = false; |
| 489 | // use new data the first time, then refresh |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 490 | for (const char* type : sensorTypes) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 491 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 492 | if (!getSensorConfiguration(configPrefix + std::string(type), systemBus, |
| 493 | sensorConfigurations, useCache)) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 494 | { |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 495 | return false; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 496 | } |
| 497 | useCache = true; |
| 498 | } |
| 499 | |
| 500 | // check PECI client addresses and DT overlay names from CPU configuration |
| 501 | // before starting ping operation |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 502 | for (const char* type : sensorTypes) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 503 | { |
| 504 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 505 | sensor : sensorConfigurations) |
| 506 | { |
| 507 | for (const std::pair< |
| 508 | std::string, |
| 509 | boost::container::flat_map<std::string, BasicVariantType>>& |
| 510 | config : sensor.second) |
| 511 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 512 | if ((configPrefix + std::string(type)) != config.first) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 513 | { |
| 514 | continue; |
| 515 | } |
| 516 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 517 | auto findName = config.second.find("Name"); |
| 518 | if (findName == config.second.end()) |
| 519 | { |
| 520 | continue; |
| 521 | } |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 522 | std::string nameRaw = variant_ns::visit( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 523 | VariantToStringVisitor(), findName->second); |
| 524 | std::string name = |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 525 | std::regex_replace(nameRaw, illegalDbusRegex, "_"); |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 526 | |
| 527 | auto findBus = config.second.find("Bus"); |
| 528 | if (findBus == config.second.end()) |
| 529 | { |
| 530 | std::cerr << "Can't find 'Bus' setting in " << name << "\n"; |
| 531 | continue; |
| 532 | } |
| 533 | uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(), |
| 534 | findBus->second); |
| 535 | |
| 536 | auto findAddress = config.second.find("Address"); |
| 537 | if (findAddress == config.second.end()) |
| 538 | { |
| 539 | std::cerr << "Can't find 'Address' setting in " << name |
| 540 | << "\n"; |
| 541 | continue; |
| 542 | } |
| 543 | uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(), |
| 544 | findAddress->second); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 545 | |
| 546 | if (DEBUG) |
| 547 | { |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 548 | std::cout << "bus: " << bus << "\n"; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 549 | std::cout << "addr: " << addr << "\n"; |
| 550 | std::cout << "name: " << name << "\n"; |
| 551 | std::cout << "type: " << type << "\n"; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 554 | configs.emplace(bus, addr, name, State::OFF); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
Yoo, Jae Hyun | 5481fac | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 558 | |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 559 | if (configs.size()) |
| 560 | { |
| 561 | std::cout << "CPU config" << (configs.size() == 1 ? " is" : "s are") |
| 562 | << " parsed\n"; |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | return false; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | int main(int argc, char** argv) |
| 570 | { |
| 571 | boost::asio::io_service io; |
| 572 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 573 | boost::container::flat_set<CPUConfig> configs; |
| 574 | |
| 575 | systemBus->request_name("xyz.openbmc_project.CPUSensor"); |
| 576 | sdbusplus::asio::object_server objectServer(systemBus); |
| 577 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors; |
| 578 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 579 | boost::asio::deadline_timer pingTimer(io); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 580 | boost::asio::deadline_timer filterTimer(io); |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 581 | |
| 582 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 583 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 584 | if (ec == boost::asio::error::operation_aborted) |
| 585 | { |
| 586 | /* we were canceled*/ |
| 587 | return; |
| 588 | } |
| 589 | else if (ec) |
| 590 | { |
| 591 | std::cerr << "timer error\n"; |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | if (getCpuConfig(systemBus, configs)) |
| 596 | { |
| 597 | detectCpu(pingTimer, io, objectServer, sensors, configs, systemBus); |
| 598 | } |
| 599 | }); |
| 600 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 601 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 602 | [&](sdbusplus::message::message& message) { |
| 603 | if (message.is_method_error()) |
| 604 | { |
| 605 | std::cerr << "callback method error\n"; |
| 606 | return; |
| 607 | } |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 608 | |
| 609 | if (DEBUG) |
| 610 | { |
| 611 | std::cout << message.get_path() << " is changed\n"; |
| 612 | } |
| 613 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 614 | // this implicitly cancels the timer |
| 615 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 616 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 617 | if (ec == boost::asio::error::operation_aborted) |
| 618 | { |
| 619 | /* we were canceled*/ |
| 620 | return; |
| 621 | } |
| 622 | else if (ec) |
| 623 | { |
| 624 | std::cerr << "timer error\n"; |
| 625 | return; |
| 626 | } |
| 627 | |
Jae Hyun Yoo | a8fa4d2 | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 628 | if (getCpuConfig(systemBus, configs)) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 629 | { |
| 630 | detectCpu(pingTimer, io, objectServer, sensors, configs, |
| 631 | systemBus); |
| 632 | } |
| 633 | }); |
| 634 | }; |
| 635 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 636 | for (const char* type : sensorTypes) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 637 | { |
| 638 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 639 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 640 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 641 | std::string(inventoryPath) + "',arg0namespace='" + |
| 642 | configPrefix + type + "'", |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 643 | eventHandler); |
| 644 | matches.emplace_back(std::move(match)); |
| 645 | } |
| 646 | |
| 647 | io.run(); |
| 648 | } |