Cheng C Yang | 43c6a1d | 2019-12-19 00:48:34 +0800 | [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 "cpu.hpp" |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <map> |
| 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace smbios |
| 25 | { |
| 26 | |
| 27 | void Cpu::cpuSocket(const uint8_t positionNum, const uint8_t structLen, |
| 28 | uint8_t *dataIn) |
| 29 | { |
| 30 | std::string result = positionToString(positionNum, structLen, dataIn); |
| 31 | |
| 32 | processorSocket(result); |
| 33 | } |
| 34 | |
| 35 | std::string Cpu::processorSocket(std::string value) |
| 36 | { |
| 37 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 38 | processorSocket(value); |
| 39 | } |
| 40 | |
| 41 | void Cpu::cpuType(const uint8_t value) |
| 42 | { |
| 43 | std::map<uint8_t, const char *>::const_iterator it = |
| 44 | processorTypeTable.find(value); |
| 45 | if (it == processorTypeTable.end()) |
| 46 | { |
| 47 | processorType("Unknown Processor Type"); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | processorType(it->second); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | std::string Cpu::processorType(std::string value) |
| 56 | { |
| 57 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 58 | processorType(value); |
| 59 | } |
| 60 | |
| 61 | void Cpu::cpuFamily(const uint8_t value) |
| 62 | { |
| 63 | std::map<uint8_t, const char *>::const_iterator it = |
| 64 | familyTable.find(value); |
| 65 | if (it == familyTable.end()) |
| 66 | { |
| 67 | processorFamily("Unknown Processor Family"); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | processorFamily(it->second); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::string Cpu::processorFamily(std::string value) |
| 76 | { |
| 77 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 78 | processorFamily(value); |
| 79 | } |
| 80 | |
| 81 | void Cpu::cpuManufacturer(const uint8_t positionNum, const uint8_t structLen, |
| 82 | uint8_t *dataIn) |
| 83 | { |
| 84 | std::string result = positionToString(positionNum, structLen, dataIn); |
| 85 | |
| 86 | manufacturer(result); |
| 87 | } |
| 88 | |
| 89 | std::string Cpu::manufacturer(std::string value) |
| 90 | { |
| 91 | return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server:: |
| 92 | Asset::manufacturer(value); |
| 93 | } |
| 94 | |
| 95 | uint32_t Cpu::processorId(uint32_t value) |
| 96 | { |
| 97 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 98 | processorId(value); |
| 99 | } |
| 100 | |
| 101 | void Cpu::cpuVersion(const uint8_t positionNum, const uint8_t structLen, |
| 102 | uint8_t *dataIn) |
| 103 | { |
| 104 | std::string result; |
| 105 | |
| 106 | result = positionToString(positionNum, structLen, dataIn); |
| 107 | |
| 108 | processorVersion(result); |
| 109 | } |
| 110 | |
| 111 | std::string Cpu::processorVersion(std::string value) |
| 112 | { |
| 113 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 114 | processorVersion(value); |
| 115 | } |
| 116 | |
| 117 | uint16_t Cpu::processorMaxSpeed(uint16_t value) |
| 118 | { |
| 119 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 120 | processorMaxSpeed(value); |
| 121 | } |
| 122 | |
| 123 | void Cpu::cpuCharacteristics(uint16_t value) |
| 124 | { |
| 125 | std::string result = ""; |
| 126 | for (uint8_t index = 0; index < (8 * sizeof(value)); index++) |
| 127 | { |
| 128 | if (value & 0x01) |
| 129 | { |
| 130 | result += characteristicsTable[index]; |
| 131 | } |
| 132 | value >>= 1; |
| 133 | } |
| 134 | |
| 135 | processorCharacteristics(result); |
| 136 | } |
| 137 | |
| 138 | std::string Cpu::processorCharacteristics(std::string value) |
| 139 | { |
| 140 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 141 | processorCharacteristics(value); |
| 142 | } |
| 143 | |
| 144 | uint16_t Cpu::processorCoreCount(uint16_t value) |
| 145 | { |
| 146 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 147 | processorCoreCount(value); |
| 148 | } |
| 149 | |
| 150 | uint16_t Cpu::processorThreadCount(uint16_t value) |
| 151 | { |
| 152 | return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Cpu:: |
| 153 | processorThreadCount(value); |
| 154 | } |
| 155 | |
| 156 | static constexpr const uint8_t populateMask = 1 << 6; |
| 157 | static constexpr const uint8_t statusMask = 0x07; |
| 158 | void Cpu::cpuStatus(uint8_t value) |
| 159 | { |
| 160 | if (!(value & populateMask)) |
| 161 | { |
| 162 | present(false); |
| 163 | functional(false); |
| 164 | return; |
| 165 | } |
| 166 | present(true); |
| 167 | if ((value & statusMask) == 1) |
| 168 | { |
| 169 | functional(true); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | functional(false); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool Cpu::present(bool value) |
| 178 | { |
| 179 | return sdbusplus::xyz::openbmc_project::Inventory::server::Item::present( |
| 180 | value); |
| 181 | } |
| 182 | |
| 183 | bool Cpu::functional(bool value) |
| 184 | { |
| 185 | return sdbusplus::xyz::openbmc_project::State::Decorator::server:: |
| 186 | OperationalStatus::functional(value); |
| 187 | } |
| 188 | |
| 189 | static constexpr uint8_t maxOldVersionCount = 0xff; |
| 190 | void Cpu::processorInfoUpdate(void) |
| 191 | { |
| 192 | uint8_t *dataIn = storage; |
| 193 | |
| 194 | dataIn = getSMBIOSTypePtr(dataIn, processorsType); |
| 195 | if (dataIn == nullptr) |
| 196 | { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | for (uint8_t index = 0; index < cpuNum; index++) |
| 201 | { |
| 202 | dataIn = smbiosNextPtr(dataIn); |
| 203 | if (dataIn == nullptr) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | dataIn = getSMBIOSTypePtr(dataIn, processorsType); |
| 208 | if (dataIn == nullptr) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | auto cpuInfo = reinterpret_cast<struct ProcessorInfo *>(dataIn); |
| 215 | |
| 216 | cpuSocket(cpuInfo->socketDesignation, cpuInfo->length, |
| 217 | dataIn); // offset 4h |
| 218 | cpuType(cpuInfo->processorType); // offset 5h |
| 219 | cpuFamily(cpuInfo->family); // offset 6h |
| 220 | cpuManufacturer(cpuInfo->manufacturer, cpuInfo->length, |
| 221 | dataIn); // offset 7h |
| 222 | processorId(cpuInfo->id); // offset 8h |
| 223 | cpuVersion(cpuInfo->version, cpuInfo->length, dataIn); // offset 10h |
| 224 | processorMaxSpeed(cpuInfo->maxSpeed); // offset 14h |
| 225 | if (cpuInfo->coreCount < maxOldVersionCount) // offset 23h or 2Ah |
| 226 | { |
| 227 | processorCoreCount(cpuInfo->coreCount); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | processorCoreCount(cpuInfo->coreCount2); |
| 232 | } |
| 233 | |
| 234 | if (cpuInfo->threadCount < maxOldVersionCount) // offset 25h or 2Eh) |
| 235 | { |
| 236 | processorThreadCount(cpuInfo->threadCount); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | processorThreadCount(cpuInfo->threadCount2); |
| 241 | } |
| 242 | |
| 243 | cpuCharacteristics(cpuInfo->characteristics); // offset 26h |
| 244 | |
| 245 | cpuStatus(cpuInfo->status); |
| 246 | } |
| 247 | |
| 248 | } // namespace smbios |
| 249 | } // namespace phosphor |