Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 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 "peci_pcie.hpp" |
| 18 | |
| 19 | #include "pciDeviceClass.hpp" |
| 20 | #include "pciVendors.hpp" |
| 21 | |
Jason M. Bills | bce86a6 | 2020-10-08 16:03:47 -0700 | [diff] [blame] | 22 | #include <boost/asio/io_service.hpp> |
| 23 | #include <boost/asio/steady_timer.hpp> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 24 | #include <boost/container/flat_map.hpp> |
| 25 | #include <boost/container/flat_set.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus/match.hpp> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 28 | |
| 29 | #include <iomanip> |
| 30 | #include <iostream> |
| 31 | #include <set> |
| 32 | #include <sstream> |
| 33 | |
| 34 | namespace peci_pcie |
| 35 | { |
| 36 | static boost::container::flat_map< |
| 37 | int, boost::container::flat_map< |
| 38 | int, boost::container::flat_map< |
| 39 | int, std::shared_ptr<sdbusplus::asio::dbus_interface>>>> |
| 40 | pcieDeviceDBusMap; |
| 41 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 42 | static bool abortScan; |
| 43 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 44 | namespace function |
| 45 | { |
| 46 | static constexpr char const* functionTypeName = "FunctionType"; |
| 47 | static constexpr char const* deviceClassName = "DeviceClass"; |
| 48 | static constexpr char const* vendorIdName = "VendorId"; |
| 49 | static constexpr char const* deviceIdName = "DeviceId"; |
| 50 | static constexpr char const* classCodeName = "ClassCode"; |
| 51 | static constexpr char const* revisionIdName = "RevisionId"; |
| 52 | static constexpr char const* subsystemIdName = "SubsystemId"; |
| 53 | static constexpr char const* subsystemVendorIdName = "SubsystemVendorId"; |
| 54 | } // namespace function |
| 55 | } // namespace peci_pcie |
| 56 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 57 | enum class resCode |
| 58 | { |
| 59 | resOk, |
| 60 | resErr |
| 61 | }; |
| 62 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 63 | struct CPUInfo |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 64 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 65 | size_t addr; |
| 66 | bool skipCpuBuses; |
| 67 | boost::container::flat_set<size_t> cpuBusNums; |
| 68 | }; |
| 69 | |
| 70 | // PECI Client Address Map |
| 71 | static void getClientAddrMap(std::vector<CPUInfo>& cpuInfo) |
| 72 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 73 | cpuInfo.clear(); |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 74 | for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 75 | { |
| 76 | if (peci_Ping(i) == PECI_CC_SUCCESS) |
| 77 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 78 | cpuInfo.emplace_back(CPUInfo{i, false, {}}); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Get CPU PCIe Bus Numbers |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 84 | static resCode getCPUBusNums(std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 85 | { |
| 86 | for (CPUInfo& cpu : cpuInfo) |
| 87 | { |
| 88 | uint8_t cc = 0; |
| 89 | CPUModel model{}; |
| 90 | uint8_t stepping = 0; |
| 91 | if (peci_GetCPUID(cpu.addr, &model, &stepping, &cc) != PECI_CC_SUCCESS) |
| 92 | { |
| 93 | std::cerr << "Cannot get CPUID!\n"; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 94 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | switch (model) |
| 98 | { |
| 99 | case skx: |
| 100 | { |
| 101 | // Get the assigned CPU bus numbers from CPUBUSNO and CPUBUSNO1 |
| 102 | // (B(0) D8 F2 offsets CCh and D0h) |
| 103 | uint32_t cpuBusNum = 0; |
| 104 | if (peci_RdPCIConfigLocal(cpu.addr, 0, 8, 2, 0xCC, 4, |
| 105 | (uint8_t*)&cpuBusNum, |
| 106 | &cc) != PECI_CC_SUCCESS) |
| 107 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 108 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 109 | } |
| 110 | uint32_t cpuBusNum1 = 0; |
| 111 | if (peci_RdPCIConfigLocal(cpu.addr, 0, 8, 2, 0xD0, 4, |
| 112 | (uint8_t*)&cpuBusNum1, |
| 113 | &cc) != PECI_CC_SUCCESS) |
| 114 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 115 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Add the CPU bus numbers to the set for this CPU |
| 119 | while (cpuBusNum) |
| 120 | { |
| 121 | // Get the LSB |
| 122 | size_t busNum = cpuBusNum & 0xFF; |
| 123 | cpu.cpuBusNums.insert(busNum); |
| 124 | // Shift right by one byte |
| 125 | cpuBusNum >>= 8; |
| 126 | } |
| 127 | while (cpuBusNum1) |
| 128 | { |
| 129 | // Get the LSB |
| 130 | size_t busNum = cpuBusNum1 & 0xFF; |
| 131 | cpu.cpuBusNums.insert(busNum); |
| 132 | // Shift right by one byte |
Zev Weiss | 9fa54b5 | 2020-09-02 21:20:33 +0000 | [diff] [blame] | 133 | cpuBusNum1 >>= 8; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 134 | } |
| 135 | cpu.skipCpuBuses = true; |
| 136 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 139 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static bool isPECIAvailable(void) |
| 143 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 144 | for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 145 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 146 | if (peci_Ping(i) == PECI_CC_SUCCESS) |
| 147 | { |
| 148 | return true; |
| 149 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 150 | } |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 151 | return false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 154 | static resCode getDataFromPCIeConfig(const int& clientAddr, const int& bus, |
| 155 | const int& dev, const int& func, |
| 156 | const int& offset, const int& size, |
| 157 | uint32_t& pciData) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 158 | { |
| 159 | // PECI RdPCIConfig() currently only supports 4 byte reads, so adjust |
| 160 | // the offset and size to get the right data |
| 161 | static constexpr const int pciReadSize = 4; |
| 162 | int mod = offset % pciReadSize; |
| 163 | int pciOffset = offset - mod; |
| 164 | if (mod + size > pciReadSize) |
| 165 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 166 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | std::array<uint8_t, pciReadSize> data; |
| 170 | uint8_t cc; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 171 | int ret = PECI_CC_TIMEOUT; |
| 172 | for (int index = 0; (index < 5) && (ret == PECI_CC_TIMEOUT); index++) |
| 173 | { |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 174 | #ifdef USE_RDENDPOINTCFG |
| 175 | ret = peci_RdEndPointConfigPci(clientAddr, // CPU Address |
| 176 | 0, // PCI Seg (use 0 for now) |
| 177 | bus, // PCI Bus |
| 178 | dev, // PCI Device |
| 179 | func, // PCI Function |
| 180 | pciOffset, // PCI Offset |
| 181 | pciReadSize, // PCI Read Size |
| 182 | data.data(), // PCI Read Data |
| 183 | &cc); // PECI Completion Code |
| 184 | #else |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 185 | ret = peci_RdPCIConfig(clientAddr, // CPU Address |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 186 | bus, // PCI Bus |
| 187 | dev, // PCI Device |
| 188 | func, // PCI Function |
| 189 | pciOffset, // PCI Offset |
| 190 | data.data(), // PCI Read Data |
| 191 | &cc); // PECI Completion Code |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 192 | #endif |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 193 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 194 | if (ret != PECI_CC_SUCCESS || cc != PECI_DEV_CC_SUCCESS) |
| 195 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 196 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Now build the requested data into a single number |
| 200 | pciData = 0; |
| 201 | for (int i = mod; i < mod + size; i++) |
| 202 | { |
Jason M. Bills | e55832b | 2021-06-14 16:00:49 -0700 | [diff] [blame^] | 203 | pciData |= static_cast<uint32_t>(data[i]) << 8 * (i - mod); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 206 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 209 | static resCode getStringFromPCIeConfig(const int& clientAddr, const int& bus, |
| 210 | const int& dev, const int& func, |
| 211 | const int& offset, const int& size, |
| 212 | std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 213 | { |
| 214 | // Get the requested data |
| 215 | uint32_t data = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 216 | if (getDataFromPCIeConfig(clientAddr, bus, dev, func, offset, size, data) != |
| 217 | resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 218 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 219 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // And convert it to a string |
| 223 | std::stringstream dataStream; |
| 224 | dataStream << "0x" << std::hex << std::setfill('0') << std::setw(size * 2) |
| 225 | << data; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 226 | res = dataStream.str(); |
| 227 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 230 | static resCode getVendorName(const int& clientAddr, const int& bus, |
| 231 | const int& dev, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 232 | { |
| 233 | static constexpr const int vendorIDOffset = 0x00; |
| 234 | static constexpr const int vendorIDSize = 2; |
| 235 | |
| 236 | // Get the header type register from function 0 |
| 237 | uint32_t vendorID = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 238 | if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, vendorIDOffset, |
| 239 | vendorIDSize, vendorID) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 240 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 241 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 242 | } |
| 243 | // Get the vendor name or use Other if it doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 244 | res = pciVendors.try_emplace(vendorID, otherVendor).first->second; |
| 245 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 248 | static resCode getDeviceClass(const int& clientAddr, const int& bus, |
| 249 | const int& dev, const int& func, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 250 | { |
| 251 | static constexpr const int baseClassOffset = 0x0b; |
| 252 | static constexpr const int baseClassSize = 1; |
| 253 | |
| 254 | // Get the Device Base Class |
| 255 | uint32_t baseClass = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 256 | if (getDataFromPCIeConfig(clientAddr, bus, dev, func, baseClassOffset, |
| 257 | baseClassSize, baseClass) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 258 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 259 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 260 | } |
| 261 | // Get the base class name or use Other if it doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 262 | res = pciDeviceClasses.try_emplace(baseClass, otherClass).first->second; |
| 263 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 266 | static resCode isMultiFunction(const int& clientAddr, const int& bus, |
| 267 | const int& dev, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 268 | { |
| 269 | static constexpr const int headerTypeOffset = 0x0e; |
| 270 | static constexpr const int headerTypeSize = 1; |
| 271 | static constexpr const int multiFuncBit = 1 << 7; |
| 272 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 273 | res = false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 274 | // Get the header type register from function 0 |
| 275 | uint32_t headerType = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 276 | if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, headerTypeOffset, |
| 277 | headerTypeSize, headerType) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 278 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 279 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 280 | } |
| 281 | // Check if it's a multifunction device |
| 282 | if (headerType & multiFuncBit) |
| 283 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 284 | res = true; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 285 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 286 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 289 | static resCode pcieFunctionExists(const int& clientAddr, const int& bus, |
| 290 | const int& dev, const int& func, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 291 | { |
| 292 | constexpr const int pciIDOffset = 0; |
| 293 | constexpr const int pciIDSize = 4; |
| 294 | uint32_t pciID = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 295 | res = false; |
| 296 | if (getDataFromPCIeConfig(clientAddr, bus, dev, func, pciIDOffset, |
| 297 | pciIDSize, pciID) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 298 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 299 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // if VID and DID are all 0s or 1s, then the device doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 303 | if (pciID != 0x00000000 && pciID != 0xFFFFFFFF) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 304 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 305 | res = true; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 306 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 307 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 310 | static resCode pcieDeviceExists(const int& clientAddr, const int& bus, |
| 311 | const int& dev, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 312 | { |
| 313 | // Check if this device exists by checking function 0 |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 314 | return pcieFunctionExists(clientAddr, bus, dev, 0, res); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 317 | static resCode setPCIeProperty(const int& clientAddr, const int& bus, |
| 318 | const int& dev, const std::string& propertyName, |
| 319 | const std::string& propertyValue) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 320 | { |
| 321 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 322 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev]; |
| 323 | |
| 324 | if (iface->is_initialized()) |
| 325 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 326 | if (!iface->set_property(propertyName, propertyValue)) |
| 327 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 328 | } |
| 329 | else |
| 330 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 331 | if (!iface->register_property(propertyName, propertyValue)) |
| 332 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 333 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 334 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void setDefaultPCIeFunctionProperties(const int& clientAddr, |
| 338 | const int& bus, const int& dev, |
| 339 | const int& func) |
| 340 | { |
| 341 | // Set the function-specific properties |
| 342 | static constexpr const std::array functionProperties{ |
| 343 | peci_pcie::function::functionTypeName, |
| 344 | peci_pcie::function::deviceClassName, |
| 345 | peci_pcie::function::vendorIdName, |
| 346 | peci_pcie::function::deviceIdName, |
| 347 | peci_pcie::function::classCodeName, |
| 348 | peci_pcie::function::revisionIdName, |
| 349 | peci_pcie::function::subsystemIdName, |
| 350 | peci_pcie::function::subsystemVendorIdName, |
| 351 | }; |
| 352 | |
| 353 | for (const char* name : functionProperties) |
| 354 | { |
| 355 | setPCIeProperty(clientAddr, bus, dev, |
| 356 | "Function" + std::to_string(func) + std::string(name), |
| 357 | std::string()); |
| 358 | } |
| 359 | } |
| 360 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 361 | static resCode setPCIeFunctionProperties(const int& clientAddr, const int& bus, |
| 362 | const int& dev, const int& func) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 363 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 364 | std::string res; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 365 | // Set the function type always to physical for now |
| 366 | setPCIeProperty(clientAddr, bus, dev, |
| 367 | "Function" + std::to_string(func) + |
| 368 | std::string(peci_pcie::function::functionTypeName), |
| 369 | "Physical"); |
| 370 | |
| 371 | // Set the function Device Class |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 372 | resCode error = getDeviceClass(clientAddr, bus, dev, func, res); |
| 373 | if (error != resCode::resOk) |
| 374 | { |
| 375 | return error; |
| 376 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 377 | setPCIeProperty(clientAddr, bus, dev, |
| 378 | "Function" + std::to_string(func) + |
| 379 | std::string(peci_pcie::function::deviceClassName), |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 380 | res); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 381 | |
| 382 | // Get PCI Function Properties that come from PCI config with the following |
| 383 | // offset and size info |
| 384 | static constexpr const std::array pciConfigInfo{ |
| 385 | std::tuple<const char*, int, int>{peci_pcie::function::vendorIdName, 0, |
| 386 | 2}, |
| 387 | std::tuple<const char*, int, int>{peci_pcie::function::deviceIdName, 2, |
| 388 | 2}, |
| 389 | std::tuple<const char*, int, int>{peci_pcie::function::classCodeName, 9, |
| 390 | 3}, |
| 391 | std::tuple<const char*, int, int>{peci_pcie::function::revisionIdName, |
| 392 | 8, 1}, |
| 393 | std::tuple<const char*, int, int>{peci_pcie::function::subsystemIdName, |
| 394 | 0x2e, 2}, |
| 395 | std::tuple<const char*, int, int>{ |
| 396 | peci_pcie::function::subsystemVendorIdName, 0x2c, 2}}; |
| 397 | |
| 398 | for (const auto& [name, offset, size] : pciConfigInfo) |
| 399 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 400 | error = getStringFromPCIeConfig(clientAddr, bus, dev, func, offset, |
| 401 | size, res); |
| 402 | if (error != resCode::resOk) |
| 403 | { |
| 404 | return error; |
| 405 | } |
| 406 | setPCIeProperty(clientAddr, bus, dev, |
| 407 | "Function" + std::to_string(func) + std::string(name), |
| 408 | res); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 409 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 410 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 413 | static resCode setPCIeDeviceProperties(const int& clientAddr, const int& bus, |
| 414 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 415 | { |
| 416 | // Set the device manufacturer |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 417 | std::string manuf; |
| 418 | resCode error = getVendorName(clientAddr, bus, dev, manuf); |
| 419 | if (error != resCode::resOk) |
| 420 | { |
| 421 | return error; |
| 422 | } |
| 423 | setPCIeProperty(clientAddr, bus, dev, "Manufacturer", manuf); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 424 | |
| 425 | // Set the device type |
| 426 | constexpr char const* deviceTypeName = "DeviceType"; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 427 | bool multiFunc; |
| 428 | error = isMultiFunction(clientAddr, bus, dev, multiFunc); |
| 429 | if (error != resCode::resOk) |
| 430 | { |
| 431 | return error; |
| 432 | } |
| 433 | if (multiFunc) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 434 | { |
| 435 | setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "MultiFunction"); |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "SingleFunction"); |
| 440 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 441 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 444 | static resCode updatePCIeDevice(const int& clientAddr, const int& bus, |
| 445 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 446 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 447 | if (setPCIeDeviceProperties(clientAddr, bus, dev) != resCode::resOk) |
| 448 | { |
| 449 | return resCode::resErr; |
| 450 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 451 | |
| 452 | // Walk through and populate the functions for this device |
| 453 | for (int func = 0; func < peci_pcie::maxPCIFunctions; func++) |
| 454 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 455 | bool res; |
| 456 | resCode error = pcieFunctionExists(clientAddr, bus, dev, func, res); |
| 457 | if (error != resCode::resOk) |
| 458 | { |
| 459 | return error; |
| 460 | } |
| 461 | if (res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 462 | { |
| 463 | // Set the properties for this function |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 464 | if (setPCIeFunctionProperties(clientAddr, bus, dev, func) != |
| 465 | resCode::resOk) |
| 466 | { |
| 467 | return resCode::resErr; |
| 468 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 469 | } |
| 470 | else |
| 471 | { |
| 472 | // Set default properties for unused functions |
| 473 | setDefaultPCIeFunctionProperties(clientAddr, bus, dev, func); |
| 474 | } |
| 475 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 476 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void removePCIeDevice(sdbusplus::asio::object_server& objServer, |
| 480 | const int& clientAddr, const int& bus, |
| 481 | const int& dev) |
| 482 | { |
| 483 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 484 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev]; |
| 485 | |
| 486 | objServer.remove_interface(iface); |
| 487 | |
| 488 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus].erase(dev); |
| 489 | if (peci_pcie::pcieDeviceDBusMap[clientAddr][bus].empty()) |
| 490 | { |
| 491 | peci_pcie::pcieDeviceDBusMap[clientAddr].erase(bus); |
| 492 | } |
| 493 | if (peci_pcie::pcieDeviceDBusMap[clientAddr].empty()) |
| 494 | { |
| 495 | peci_pcie::pcieDeviceDBusMap.erase(clientAddr); |
| 496 | } |
| 497 | } |
| 498 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 499 | static resCode addPCIeDevice(sdbusplus::asio::object_server& objServer, |
| 500 | const int& clientAddr, const int& cpu, |
| 501 | const int& bus, const int& dev) |
| 502 | { |
| 503 | std::string pathName = std::string(peci_pcie::peciPCIePath) + "/S" + |
| 504 | std::to_string(cpu) + "B" + std::to_string(bus) + |
| 505 | "D" + std::to_string(dev); |
| 506 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 507 | objServer.add_interface(pathName, peci_pcie::peciPCIeDeviceInterface); |
| 508 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev] = iface; |
| 509 | |
| 510 | // Update the properties for the new device |
| 511 | if (updatePCIeDevice(clientAddr, bus, dev) != resCode::resOk) |
| 512 | { |
| 513 | removePCIeDevice(objServer, clientAddr, bus, dev); |
| 514 | return resCode::resErr; |
| 515 | } |
| 516 | |
| 517 | iface->initialize(); |
| 518 | return resCode::resOk; |
| 519 | } |
| 520 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 521 | static bool pcieDeviceInDBusMap(const int& clientAddr, const int& bus, |
| 522 | const int& dev) |
| 523 | { |
| 524 | if (auto clientAddrIt = peci_pcie::pcieDeviceDBusMap.find(clientAddr); |
| 525 | clientAddrIt != peci_pcie::pcieDeviceDBusMap.end()) |
| 526 | { |
| 527 | if (auto busIt = clientAddrIt->second.find(bus); |
| 528 | busIt != clientAddrIt->second.end()) |
| 529 | { |
| 530 | if (auto devIt = busIt->second.find(dev); |
| 531 | devIt != busIt->second.end()) |
| 532 | { |
| 533 | if (devIt->second) |
| 534 | { |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 543 | static void scanNextPCIeDevice(boost::asio::io_service& io, |
| 544 | sdbusplus::asio::object_server& objServer, |
| 545 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 546 | int dev); |
| 547 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 548 | static resCode probePCIeDevice(boost::asio::io_service& io, |
| 549 | sdbusplus::asio::object_server& objServer, |
| 550 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 551 | int dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 552 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 553 | bool res; |
| 554 | resCode error = pcieDeviceExists(cpuInfo[cpu].addr, bus, dev, res); |
| 555 | if (error != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 556 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 557 | return error; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 558 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 559 | if (res) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 560 | { |
| 561 | if (pcieDeviceInDBusMap(cpuInfo[cpu].addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 562 | { |
| 563 | // This device is already in D-Bus, so update it |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 564 | if (updatePCIeDevice(cpuInfo[cpu].addr, bus, dev) != resCode::resOk) |
| 565 | { |
| 566 | return resCode::resErr; |
| 567 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 568 | } |
| 569 | else |
| 570 | { |
| 571 | // This device is not in D-Bus, so add it |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 572 | if (addPCIeDevice(objServer, cpuInfo[cpu].addr, cpu, bus, dev) != |
| 573 | resCode::resOk) |
| 574 | { |
| 575 | return resCode::resErr; |
| 576 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | // If PECI is not available, then stop scanning |
| 582 | if (!isPECIAvailable()) |
| 583 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 584 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 587 | if (pcieDeviceInDBusMap(cpuInfo[cpu].addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 588 | { |
| 589 | // This device is in D-Bus, so remove it |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 590 | removePCIeDevice(objServer, cpuInfo[cpu].addr, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 593 | return resCode::resOk; |
| 594 | } |
| 595 | |
| 596 | static void scanPCIeDevice(boost::asio::io_service& io, |
| 597 | sdbusplus::asio::object_server& objServer, |
| 598 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 599 | int dev) |
| 600 | { |
| 601 | // Check if this is a CPU bus that we should skip |
| 602 | if (cpuInfo[cpu].skipCpuBuses && cpuInfo[cpu].cpuBusNums.count(bus)) |
| 603 | { |
| 604 | std::cout << "Skipping CPU " << cpu << " Bus Number " << bus << "\n"; |
| 605 | // Skip all the devices on this bus |
| 606 | dev = peci_pcie::maxPCIDevices; |
| 607 | scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | if (probePCIeDevice(io, objServer, cpuInfo, cpu, bus, dev) != |
| 612 | resCode::resOk) |
| 613 | { |
| 614 | std::cerr << "Failed to probe CPU " << cpu << " Bus " << bus |
| 615 | << " Device " << dev << "\n"; |
| 616 | } |
| 617 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 618 | scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
| 619 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 620 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 621 | static void scanNextPCIeDevice(boost::asio::io_service& io, |
| 622 | sdbusplus::asio::object_server& objServer, |
| 623 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 624 | int dev) |
| 625 | { |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 626 | if (peci_pcie::abortScan) |
| 627 | { |
| 628 | std::cerr << "PCIe scan aborted\n"; |
| 629 | return; |
| 630 | } |
| 631 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 632 | // PCIe Device scan completed, so move to the next device |
| 633 | if (++dev >= peci_pcie::maxPCIDevices) |
| 634 | { |
| 635 | // All devices scanned, so move to the next bus |
| 636 | dev = 0; |
| 637 | if (++bus >= peci_pcie::maxPCIBuses) |
| 638 | { |
| 639 | // All buses scanned, so move to the next CPU |
| 640 | bus = 0; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 641 | if (++cpu >= cpuInfo.size()) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 642 | { |
| 643 | // All CPUs scanned, so we're done |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 644 | std::cerr << "PCIe scan completed\n"; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | } |
| 648 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 649 | boost::asio::post(io, [&io, &objServer, &cpuInfo, cpu, bus, dev]() mutable { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 650 | scanPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 651 | }); |
| 652 | } |
| 653 | |
| 654 | static void peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer, |
| 655 | boost::asio::io_service& io, |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 656 | sdbusplus::asio::object_server& objServer, |
| 657 | std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 658 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 659 | static bool lastPECIState = false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 660 | bool peciAvailable = isPECIAvailable(); |
| 661 | if (peciAvailable && !lastPECIState) |
| 662 | { |
| 663 | lastPECIState = true; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 664 | static boost::asio::steady_timer pcieTimeout(io); |
| 665 | constexpr const int pcieWaitTime = 60; |
| 666 | pcieTimeout.expires_after(std::chrono::seconds(pcieWaitTime)); |
| 667 | pcieTimeout.async_wait( |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 668 | [&io, &objServer, &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 669 | if (ec) |
| 670 | { |
| 671 | // operation_aborted is expected if timer is canceled |
| 672 | // before completion. |
| 673 | if (ec != boost::asio::error::operation_aborted) |
| 674 | { |
| 675 | std::cerr << "PECI PCIe async_wait failed " << ec; |
| 676 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 677 | lastPECIState = false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | // get the PECI client address list |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 681 | getClientAddrMap(cpuInfo); |
| 682 | // get the CPU Bus Numbers to skip |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 683 | if (getCPUBusNums(cpuInfo) != resCode::resOk) |
| 684 | { |
| 685 | lastPECIState = false; |
| 686 | return; |
| 687 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 688 | // scan PCIe starting from CPU 0, Bus 0, Device 0 |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 689 | std::cerr << "PCIe scan started\n"; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 690 | scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 691 | }); |
| 692 | } |
| 693 | else if (!peciAvailable && lastPECIState) |
| 694 | { |
| 695 | lastPECIState = false; |
| 696 | } |
| 697 | |
| 698 | peciWaitTimer.expires_after( |
| 699 | std::chrono::seconds(peci_pcie::peciCheckInterval)); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 700 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &objServer, |
| 701 | &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 702 | if (ec) |
| 703 | { |
| 704 | // operation_aborted is expected if timer is canceled |
| 705 | // before completion. |
| 706 | if (ec != boost::asio::error::operation_aborted) |
| 707 | { |
| 708 | std::cerr << "PECI Available Check async_wait failed " << ec; |
| 709 | } |
| 710 | return; |
| 711 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 712 | peciAvailableCheck(peciWaitTimer, io, objServer, cpuInfo); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 713 | }); |
| 714 | } |
| 715 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 716 | static void waitForOSStandbyDelay(boost::asio::io_service& io, |
| 717 | sdbusplus::asio::object_server& objServer, |
| 718 | boost::asio::steady_timer& osStandbyTimer, |
| 719 | std::vector<CPUInfo>& cpuInfo) |
| 720 | { |
| 721 | osStandbyTimer.expires_after( |
| 722 | std::chrono::seconds(peci_pcie::osStandbyDelaySeconds)); |
| 723 | |
| 724 | osStandbyTimer.async_wait( |
| 725 | [&io, &objServer, &cpuInfo](const boost::system::error_code& ec) { |
| 726 | if (ec == boost::asio::error::operation_aborted) |
| 727 | { |
| 728 | return; // we're being canceled |
| 729 | } |
| 730 | else if (ec) |
| 731 | { |
| 732 | std::cerr << "OS Standby async_wait failed: " << ec.value() |
| 733 | << ": " << ec.message() << "\n"; |
| 734 | return; |
| 735 | } |
| 736 | // get the PECI client address list |
| 737 | getClientAddrMap(cpuInfo); |
| 738 | if (cpuInfo.empty()) |
| 739 | { |
| 740 | std::cerr << "No CPUs found, scan skipped\n"; |
| 741 | return; |
| 742 | } |
| 743 | // get the CPU Bus Numbers to skip |
| 744 | if (getCPUBusNums(cpuInfo) != resCode::resOk) |
| 745 | { |
| 746 | return; |
| 747 | } |
| 748 | // scan PCIe starting from CPU 0, Bus 0, Device 0 |
| 749 | std::cerr << "PCIe scan started\n"; |
| 750 | scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0); |
| 751 | }); |
| 752 | } |
| 753 | |
| 754 | static void monitorOSStandby(boost::asio::io_service& io, |
| 755 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 756 | sdbusplus::asio::object_server& objServer, |
| 757 | boost::asio::steady_timer& osStandbyTimer, |
| 758 | std::vector<CPUInfo>& cpuInfo) |
| 759 | { |
| 760 | std::cerr << "Start OperatingSystemState Monitor\n"; |
| 761 | |
| 762 | static sdbusplus::bus::match::match osStateMatch( |
| 763 | *conn, |
| 764 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 765 | "PropertiesChanged',arg0='xyz.openbmc_project.State.OperatingSystem." |
| 766 | "Status'", |
| 767 | [&io, &objServer, &osStandbyTimer, |
| 768 | &cpuInfo](sdbusplus::message::message& msg) { |
| 769 | // Get the OS State from the message |
| 770 | std::string osStateInterface; |
| 771 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 772 | propertiesChanged; |
| 773 | msg.read(osStateInterface, propertiesChanged); |
| 774 | |
| 775 | for (const auto& [name, value] : propertiesChanged) |
| 776 | { |
| 777 | if (name == "OperatingSystemState") |
| 778 | { |
| 779 | const std::string* state = std::get_if<std::string>(&value); |
| 780 | if (state == nullptr) |
| 781 | { |
| 782 | std::cerr << "Unable to read OS state value\n"; |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | if (*state == "Standby") |
| 787 | { |
| 788 | peci_pcie::abortScan = false; |
| 789 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, |
| 790 | cpuInfo); |
| 791 | } |
| 792 | else if (*state == "Inactive") |
| 793 | { |
| 794 | peci_pcie::abortScan = true; |
| 795 | osStandbyTimer.cancel(); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | }); |
| 800 | |
| 801 | // Check if the OS state is already available |
| 802 | conn->async_method_call( |
| 803 | [&io, &objServer, &osStandbyTimer, |
| 804 | &cpuInfo](boost::system::error_code ec, |
| 805 | const std::variant<std::string>& property) { |
| 806 | if (ec) |
| 807 | { |
| 808 | std::cerr << "error with OS state async_method_call\n"; |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | const std::string* state = std::get_if<std::string>(&property); |
| 813 | if (state == nullptr) |
| 814 | { |
| 815 | std::cerr << "Unable to read OS state value\n"; |
| 816 | return; |
| 817 | } |
| 818 | |
| 819 | // If the OS state is in Standby, then BIOS is done and we can |
| 820 | // continue. Otherwise, we just wait for the match |
| 821 | if (*state == "Standby") |
| 822 | { |
| 823 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, cpuInfo); |
| 824 | } |
| 825 | }, |
| 826 | "xyz.openbmc_project.State.OperatingSystem", |
| 827 | "/xyz/openbmc_project/state/os", "org.freedesktop.DBus.Properties", |
| 828 | "Get", "xyz.openbmc_project.State.OperatingSystem.Status", |
| 829 | "OperatingSystemState"); |
| 830 | } |
| 831 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 832 | int main(int argc, char* argv[]) |
| 833 | { |
| 834 | // setup connection to dbus |
| 835 | boost::asio::io_service io; |
| 836 | std::shared_ptr<sdbusplus::asio::connection> conn = |
| 837 | std::make_shared<sdbusplus::asio::connection>(io); |
| 838 | |
| 839 | // PECI PCIe Object |
| 840 | conn->request_name(peci_pcie::peciPCIeObject); |
| 841 | sdbusplus::asio::object_server server = |
| 842 | sdbusplus::asio::object_server(conn); |
| 843 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 844 | // CPU map |
| 845 | std::vector<CPUInfo> cpuInfo; |
| 846 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 847 | #ifdef WAIT_FOR_OS_STANDBY |
| 848 | boost::asio::steady_timer osStandbyTimer(io); |
| 849 | monitorOSStandby(io, conn, server, osStandbyTimer, cpuInfo); |
| 850 | #else |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 851 | // Start the PECI check loop |
| 852 | boost::asio::steady_timer peciWaitTimer( |
| 853 | io, std::chrono::seconds(peci_pcie::peciCheckInterval)); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 854 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &server, |
| 855 | &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 856 | if (ec) |
| 857 | { |
| 858 | // operation_aborted is expected if timer is canceled |
| 859 | // before completion. |
| 860 | if (ec != boost::asio::error::operation_aborted) |
| 861 | { |
| 862 | std::cerr << "PECI Available Check async_wait failed " << ec; |
| 863 | } |
| 864 | return; |
| 865 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 866 | peciAvailableCheck(peciWaitTimer, io, server, cpuInfo); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 867 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 868 | #endif |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 869 | |
| 870 | io.run(); |
| 871 | |
| 872 | return 0; |
| 873 | } |