blob: 7c10a09acb4ab7750d80b53a993deea4028b1ee8 [file] [log] [blame]
Cheng C Yang43c6a1d2019-12-19 00:48:34 +08001/*
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
Zhikui Ren18a5ab92020-09-01 21:35:20 -070019#include <bitset>
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080020#include <map>
21
22namespace phosphor
23{
24namespace smbios
25{
26
Zhikui Ren18a5ab92020-09-01 21:35:20 -070027void Cpu::socket(const uint8_t positionNum, const uint8_t structLen,
28 uint8_t* dataIn)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080029{
30 std::string result = positionToString(positionNum, structLen, dataIn);
31
Zhikui Ren18a5ab92020-09-01 21:35:20 -070032 processor::socket(result);
Jie Yang31720392021-07-22 21:45:45 -070033
34 location::locationCode(result);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080035}
36
Charles Boyer800bb702021-08-31 17:09:19 -050037static constexpr uint8_t processorFamily2Indicator = 0xfe;
38void Cpu::family(const uint8_t family, const uint16_t family2)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080039{
Charles Boyer800bb702021-08-31 17:09:19 -050040 std::map<uint8_t, const char*>::const_iterator it =
41 familyTable.find(family);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080042 if (it == familyTable.end())
43 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -070044 processor::family("Unknown Processor Family");
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080045 }
Charles Boyer800bb702021-08-31 17:09:19 -050046 else if (it->first == processorFamily2Indicator)
47 {
48 std::map<uint16_t, const char*>::const_iterator it2 =
49 family2Table.find(family2);
50 if (it2 == family2Table.end())
51 {
52 processor::family("Unknown Processor Family");
53 }
54 else
55 {
56 processor::family(it2->second);
Jiaqing Zhao19849572022-06-13 21:10:45 +080057 processor::effectiveFamily(family2);
Charles Boyer800bb702021-08-31 17:09:19 -050058 }
59 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080060 else
61 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -070062 processor::family(it->second);
Jiaqing Zhao19849572022-06-13 21:10:45 +080063 processor::effectiveFamily(family);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080064 }
65}
66
Zhikui Ren18a5ab92020-09-01 21:35:20 -070067void Cpu::manufacturer(const uint8_t positionNum, const uint8_t structLen,
68 uint8_t* dataIn)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080069{
70 std::string result = positionToString(positionNum, structLen, dataIn);
71
Zhikui Ren18a5ab92020-09-01 21:35:20 -070072 asset::manufacturer(result);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080073}
74
Charles Boyere6431692021-08-31 16:47:47 -050075void Cpu::partNumber(const uint8_t positionNum, const uint8_t structLen,
76 uint8_t* dataIn)
77{
78 std::string result = positionToString(positionNum, structLen, dataIn);
79
80 asset::partNumber(result);
81}
82
83void Cpu::serialNumber(const uint8_t positionNum, const uint8_t structLen,
84 uint8_t* dataIn)
85{
86 std::string result = positionToString(positionNum, structLen, dataIn);
87
88 asset::serialNumber(result);
89}
90
Zhikui Ren18a5ab92020-09-01 21:35:20 -070091void Cpu::version(const uint8_t positionNum, const uint8_t structLen,
92 uint8_t* dataIn)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080093{
94 std::string result;
95
96 result = positionToString(positionNum, structLen, dataIn);
97
Zhikui Ren18a5ab92020-09-01 21:35:20 -070098 rev::version(result);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +080099}
100
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700101void Cpu::characteristics(uint16_t value)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800102{
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700103 std::vector<processor::Capability> result;
104 std::optional<processor::Capability> cap;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800105
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700106 std::bitset<16> charBits = value;
107 for (uint8_t index = 0; index < charBits.size(); index++)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800108 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700109 if (charBits.test(index))
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800110 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700111 if (cap = characteristicsTable[index])
112 {
113 result.emplace_back(*cap);
114 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800115 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800116 }
117
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700118 processor::characteristics(result);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800119}
120
121static constexpr uint8_t maxOldVersionCount = 0xff;
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700122void Cpu::infoUpdate(void)
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800123{
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800124 uint8_t* dataIn = storage;
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800125
126 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
127 if (dataIn == nullptr)
128 {
129 return;
130 }
131
132 for (uint8_t index = 0; index < cpuNum; index++)
133 {
134 dataIn = smbiosNextPtr(dataIn);
135 if (dataIn == nullptr)
136 {
137 return;
138 }
139 dataIn = getSMBIOSTypePtr(dataIn, processorsType);
140 if (dataIn == nullptr)
141 {
142 return;
143 }
144 }
145
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800146 auto cpuInfo = reinterpret_cast<struct ProcessorInfo*>(dataIn);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800147
Jonathan Doman563570d2021-05-24 10:52:43 -0700148 socket(cpuInfo->socketDesignation, cpuInfo->length, dataIn); // offset 4h
149
150 constexpr uint32_t socketPopulatedMask = 1 << 6;
151 if ((cpuInfo->status & socketPopulatedMask) == 0)
152 {
153 // Don't attempt to fill in any other details if the CPU is not present.
154 present(false);
155 return;
156 }
157 present(true);
158
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700159 // this class is for type CPU //offset 5h
Charles Boyer800bb702021-08-31 17:09:19 -0500160 family(cpuInfo->family, cpuInfo->family2); // offset 6h and 28h
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700161 manufacturer(cpuInfo->manufacturer, cpuInfo->length,
Zhikui Ren59616932023-03-24 09:02:20 -0700162 dataIn); // offset 7h
163 id(cpuInfo->id); // offset 8h
164
165 // Step, EffectiveFamily, EffectiveModel computation for Intel processors.
166 std::map<uint8_t, const char*>::const_iterator it =
167 familyTable.find(cpuInfo->family);
168 if (it != familyTable.end())
169 {
170 std::string familyStr = it->second;
171 if ((familyStr.find(" Xeon ") != std::string::npos) ||
172 (familyStr.find(" Intel ") != std::string::npos))
173 {
174 // Processor ID field
175 // SteppinID: 4;
176 // Model: 4;
177 // Family: 4;
178 // Type: 2;
179 // Reserved1: 2;
180 // XModel: 4;
181 // XFamily: 8;
182 // Reserved2: 4;
183 uint16_t cpuStep = cpuInfo->id & 0xf;
184 uint16_t cpuModel = (cpuInfo->id & 0xf0) >> 4;
185 uint16_t cpuFamily = (cpuInfo->id & 0xf00) >> 8;
186 uint16_t cpuXModel = (cpuInfo->id & 0xf0000) >> 16;
187 uint16_t cpuXFamily = (cpuInfo->id & 0xff00000) >> 20;
188 step(cpuStep);
189 if (cpuFamily == 0xf)
190 {
191 effectiveFamily(cpuXFamily + cpuFamily);
192 }
193 else
194 {
195 effectiveFamily(cpuFamily);
196 }
197 if (cpuFamily == 0x6 || cpuFamily == 0xf)
198 {
199 effectiveModel((cpuXModel << 4) | cpuModel);
200 }
201 else
202 {
203 effectiveModel(cpuModel);
204 }
205 }
206 }
207
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700208 version(cpuInfo->version, cpuInfo->length, dataIn); // offset 10h
209 maxSpeedInMhz(cpuInfo->maxSpeed); // offset 14h
Charles Boyere6431692021-08-31 16:47:47 -0500210 serialNumber(cpuInfo->serialNum, cpuInfo->length,
211 dataIn); // offset 20h
212 partNumber(cpuInfo->partNum, cpuInfo->length,
213 dataIn); // offset 22h
214 if (cpuInfo->coreCount < maxOldVersionCount) // offset 23h or 2Ah
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800215 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700216 coreCount(cpuInfo->coreCount);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800217 }
218 else
219 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700220 coreCount(cpuInfo->coreCount2);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800221 }
222
223 if (cpuInfo->threadCount < maxOldVersionCount) // offset 25h or 2Eh)
224 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700225 threadCount(cpuInfo->threadCount);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800226 }
227 else
228 {
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700229 threadCount(cpuInfo->threadCount2);
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800230 }
231
Zhikui Ren18a5ab92020-09-01 21:35:20 -0700232 characteristics(cpuInfo->characteristics); // offset 26h
Jie Yange7cf3192021-08-20 11:21:43 -0700233
234 if (!motherboardPath.empty())
235 {
236 std::vector<std::tuple<std::string, std::string, std::string>> assocs;
237 assocs.emplace_back("chassis", "processors", motherboardPath);
238 association::associations(assocs);
239 }
Cheng C Yang43c6a1d2019-12-19 00:48:34 +0800240}
241
242} // namespace smbios
243} // namespace phosphor