blob: 2138886cef132f3fda19f6febe6bf353ad695961 [file] [log] [blame]
Cheng C Yang8c3fab62019-12-19 00:51:06 +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 "dimm.hpp"
18
19#include "mdrv2.hpp"
20
kasunath634ec6a2022-07-25 15:34:17 -070021#include <boost/algorithm/string.hpp>
kasunath2eca4fe2022-08-17 17:30:07 -070022#include <phosphor-logging/elog-errors.hpp>
kasunath634ec6a2022-07-25 15:34:17 -070023
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +000024#include <regex>
25
Cheng C Yang8c3fab62019-12-19 00:51:06 +080026namespace phosphor
27{
28namespace smbios
29{
30
John Edward Broadbentefd41542022-12-13 16:39:18 -080031#ifdef DIMM_ONLY_LOCATOR
32bool onlyDimmLocationCode = true;
33#else
34bool onlyDimmLocationCode = false;
35#endif
36
Cheng C Yang8c3fab62019-12-19 00:51:06 +080037using DeviceType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070038 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::DeviceType;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080039
kasunath2eca4fe2022-08-17 17:30:07 -070040using EccType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070041 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::Ecc;
kasunath2eca4fe2022-08-17 17:30:07 -070042
Cheng C Yang8c3fab62019-12-19 00:51:06 +080043static constexpr uint16_t maxOldDimmSize = 0x7fff;
Brandon Kim5a122a62023-05-04 04:25:03 +000044void Dimm::memoryInfoUpdate(uint8_t* smbiosTableStorage,
45 const std::string& motherboard)
Cheng C Yang8c3fab62019-12-19 00:51:06 +080046{
Brandon Kim5a122a62023-05-04 04:25:03 +000047 storage = smbiosTableStorage;
48 motherboardPath = motherboard;
49
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080050 uint8_t* dataIn = storage;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080051
52 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
53
54 if (dataIn == nullptr)
55 {
56 return;
57 }
58 for (uint8_t index = 0; index < dimmNum; index++)
59 {
60 dataIn = smbiosNextPtr(dataIn);
61 if (dataIn == nullptr)
62 {
63 return;
64 }
65 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
66 if (dataIn == nullptr)
67 {
68 return;
69 }
70 }
71
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080072 auto memoryInfo = reinterpret_cast<struct MemoryInfo*>(dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080073
74 memoryDataWidth(memoryInfo->dataWidth);
75
76 if (memoryInfo->size == maxOldDimmSize)
77 {
78 dimmSizeExt(memoryInfo->extendedSize);
79 }
80 else
81 {
82 dimmSize(memoryInfo->size);
83 }
Tom Tung39cc3682023-03-15 04:44:04 +080084 // If the size is 0, no memory device is installed in the socket.
85 const auto isDimmPresent = memoryInfo->size > 0;
86 present(isDimmPresent);
87 functional(isDimmPresent);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080088
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +000089 dimmDeviceLocator(memoryInfo->bankLocator, memoryInfo->deviceLocator,
90 memoryInfo->length, dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080091 dimmType(memoryInfo->memoryType);
92 dimmTypeDetail(memoryInfo->typeDetail);
93 maxMemorySpeedInMhz(memoryInfo->speed);
94 dimmManufacturer(memoryInfo->manufacturer, memoryInfo->length, dataIn);
95 dimmSerialNum(memoryInfo->serialNum, memoryInfo->length, dataIn);
96 dimmPartNum(memoryInfo->partNum, memoryInfo->length, dataIn);
97 memoryAttributes(memoryInfo->attributes);
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +000098 dimmMedia(memoryInfo->memoryTechnology);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080099 memoryConfiguredSpeedInMhz(memoryInfo->confClockSpeed);
100
kasunath2eca4fe2022-08-17 17:30:07 -0700101 updateEccType(memoryInfo->phyArrayHandle);
102
Jie Yange7cf3192021-08-20 11:21:43 -0700103 if (!motherboardPath.empty())
104 {
105 std::vector<std::tuple<std::string, std::string, std::string>> assocs;
106 assocs.emplace_back("chassis", "memories", motherboardPath);
107 association::associations(assocs);
108 }
109
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800110 return;
111}
112
kasunath2eca4fe2022-08-17 17:30:07 -0700113void Dimm::updateEccType(uint16_t exPhyArrayHandle)
114{
115 uint8_t* dataIn = storage;
116
117 while (dataIn != nullptr)
118 {
119 dataIn = getSMBIOSTypePtr(dataIn, physicalMemoryArrayType);
120 if (dataIn == nullptr)
121 {
122 phosphor::logging::log<phosphor::logging::level::ERR>(
123 "Failed to get SMBIOS table type-16 data.");
124 return;
125 }
126
127 auto info = reinterpret_cast<struct PhysicalMemoryArrayInfo*>(dataIn);
128 if (info->handle == exPhyArrayHandle)
129 {
130 std::map<uint8_t, EccType>::const_iterator it =
131 dimmEccTypeMap.find(info->memoryErrorCorrection);
132 if (it == dimmEccTypeMap.end())
133 {
134 ecc(EccType::NoECC);
135 }
136 else
137 {
138 ecc(it->second);
139 }
140 return;
141 }
142
143 dataIn = smbiosNextPtr(dataIn);
144 }
145 phosphor::logging::log<phosphor::logging::level::ERR>(
146 "Failed find the corresponding SMBIOS table type-16 data for dimm:",
147 phosphor::logging::entry("DIMM:%d", dimmNum));
148}
149
150EccType Dimm::ecc(EccType value)
151{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700152 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::ecc(
kasunath2eca4fe2022-08-17 17:30:07 -0700153 value);
154}
155
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800156uint16_t Dimm::memoryDataWidth(uint16_t value)
157{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700158 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800159 memoryDataWidth(value);
160}
161
162static constexpr uint16_t baseNewVersionDimmSize = 0x8000;
163static constexpr uint16_t dimmSizeUnit = 1024;
164void Dimm::dimmSize(const uint16_t size)
165{
Joseph Fu0b1d9422023-03-31 17:47:12 +0800166 uint32_t result = size & maxOldDimmSize;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800167 if (0 == (size & baseNewVersionDimmSize))
168 {
169 result = result * dimmSizeUnit;
170 }
171 memorySizeInKB(result);
172}
173
Joseph Fu0b1d9422023-03-31 17:47:12 +0800174void Dimm::dimmSizeExt(uint32_t size)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800175{
176 size = size * dimmSizeUnit;
177 memorySizeInKB(size);
178}
179
Jason M. Billse7770992021-05-14 13:24:33 -0700180size_t Dimm::memorySizeInKB(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800181{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700182 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800183 memorySizeInKB(value);
184}
185
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000186void Dimm::dimmDeviceLocator(const uint8_t bankLocatorPositionNum,
187 const uint8_t deviceLocatorPositionNum,
188 const uint8_t structLen, uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800189{
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500190 std::string deviceLocator = positionToString(deviceLocatorPositionNum,
191 structLen, dataIn);
192 std::string bankLocator = positionToString(bankLocatorPositionNum,
193 structLen, dataIn);
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000194
195 std::string result;
John Edward Broadbentefd41542022-12-13 16:39:18 -0800196 if (bankLocator.empty() || onlyDimmLocationCode)
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000197 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800198 result = deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000199 }
200 else
201 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800202 result = bankLocator + " " + deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000203 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800204
205 memoryDeviceLocator(result);
Jie Yang31720392021-07-22 21:45:45 -0700206
207 locationCode(result);
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000208 const std::string substrCpu = "CPU";
209 auto cpuPos = deviceLocator.find(substrCpu);
210
211 if (cpuPos != std::string::npos)
212 {
213 std::string socketString =
214 deviceLocator.substr(cpuPos + substrCpu.length(), 1);
215 try
216 {
217 uint8_t socketNum =
218 static_cast<uint8_t>(std::stoi(socketString) + 1);
219 socket(socketNum);
220 }
221 catch (const sdbusplus::exception_t& ex)
222 {
223 phosphor::logging::log<phosphor::logging::level::ERR>(
224 "std::stoi operation failed ",
225 phosphor::logging::entry("ERROR=%s", ex.what()));
226 }
227 }
228
229 const std::string substrDimm = "DIMM";
230 auto dimmPos = deviceLocator.find(substrDimm);
231
232 if (dimmPos != std::string::npos)
233 {
234 std::string slotString =
235 deviceLocator.substr(dimmPos + substrDimm.length() + 1);
236 /* slotString is extracted from substrDimm (DIMM_A) if slotString is
237 * single alphabet like A, B , C.. then assign ASCII value of slotString
238 * to slot */
239 if ((std::regex_match(slotString, std::regex("^[A-Za-z]+$"))) &&
240 (slotString.length() == 1))
241 {
242 slot(static_cast<uint8_t>(toupper(slotString[0])));
243 }
244 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800245}
246
247std::string Dimm::memoryDeviceLocator(std::string value)
248{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700249 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800250 memoryDeviceLocator(value);
251}
252
253void Dimm::dimmType(const uint8_t type)
254{
255 std::map<uint8_t, DeviceType>::const_iterator it = dimmTypeTable.find(type);
256 if (it == dimmTypeTable.end())
257 {
258 memoryType(DeviceType::Unknown);
259 }
260 else
261 {
262 memoryType(it->second);
263 }
264}
265
266DeviceType Dimm::memoryType(DeviceType value)
267{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700268 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800269 memoryType(value);
270}
271
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000272void Dimm::dimmMedia(const uint8_t type)
273{
274 std::map<uint8_t, MemoryTechType>::const_iterator it =
275 dimmMemoryTechTypeMap.find(type);
276 if (it == dimmMemoryTechTypeMap.end())
277 {
278 memoryMedia(MemoryTechType::Unknown);
279 }
280 else
281 {
282 memoryMedia(it->second);
283 }
284}
285
286MemoryTechType Dimm::memoryMedia(MemoryTechType value)
287{
288 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
289 memoryMedia(value);
290}
291
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800292void Dimm::dimmTypeDetail(uint16_t detail)
293{
294 std::string result;
295 for (uint8_t index = 0; index < (8 * sizeof(detail)); index++)
296 {
297 if (detail & 0x01)
298 {
299 result += detailTable[index];
300 }
301 detail >>= 1;
302 }
303 memoryTypeDetail(result);
304}
305
306std::string Dimm::memoryTypeDetail(std::string value)
307{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700308 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800309 memoryTypeDetail(value);
310}
311
312uint16_t Dimm::maxMemorySpeedInMhz(uint16_t value)
313{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700314 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800315 maxMemorySpeedInMhz(value);
316}
317
318void Dimm::dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800319 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800320{
321 std::string result = positionToString(positionNum, structLen, dataIn);
322
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530323 if (result == "NO DIMM")
324 {
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530325 // No dimm presence so making manufacturer value as "" (instead of
326 // NO DIMM - as there won't be any manufacturer for DIMM which is not
327 // present).
328 result = "";
329 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800330 manufacturer(result);
331}
332
333std::string Dimm::manufacturer(std::string value)
334{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700335 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800336 Asset::manufacturer(value);
337}
338
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530339bool Dimm::present(bool value)
340{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700341 return sdbusplus::server::xyz::openbmc_project::inventory::Item::present(
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530342 value);
343}
344
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800345void Dimm::dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800346 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800347{
348 std::string result = positionToString(positionNum, structLen, dataIn);
349
350 serialNumber(result);
351}
352
353std::string Dimm::serialNumber(std::string value)
354{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700355 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800356 Asset::serialNumber(value);
357}
358
359void Dimm::dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800360 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800361{
362 std::string result = positionToString(positionNum, structLen, dataIn);
363
kasunath634ec6a2022-07-25 15:34:17 -0700364 // Part number could contain spaces at the end. Eg: "abcd123 ". Since its
365 // unnecessary, we should remove them.
366 boost::algorithm::trim_right(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800367 partNumber(result);
368}
369
370std::string Dimm::partNumber(std::string value)
371{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700372 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800373 Asset::partNumber(value);
374}
375
Jie Yang31720392021-07-22 21:45:45 -0700376std::string Dimm::locationCode(std::string value)
377{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700378 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Jie Yang31720392021-07-22 21:45:45 -0700379 LocationCode::locationCode(value);
380}
381
George Liu036374a2023-06-15 08:47:46 +0800382size_t Dimm::memoryAttributes(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800383{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700384 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800385 memoryAttributes(value);
386}
387
Jayaprakash Mutyalaa1ff2442023-06-15 07:10:07 +0000388uint8_t Dimm::slot(uint8_t value)
389{
390 return sdbusplus::server::xyz::openbmc_project::inventory::item::dimm::
391 MemoryLocation::slot(value);
392}
393
394uint8_t Dimm::socket(uint8_t value)
395{
396 return sdbusplus::server::xyz::openbmc_project::inventory::item::dimm::
397 MemoryLocation::socket(value);
398}
399
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800400uint16_t Dimm::memoryConfiguredSpeedInMhz(uint16_t value)
401{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700402 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800403 memoryConfiguredSpeedInMhz(value);
404}
405
Tim Leedc469c72021-07-20 10:55:58 +0800406bool Dimm::functional(bool value)
407{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700408 return sdbusplus::server::xyz::openbmc_project::state::decorator::
Tim Leedc469c72021-07-20 10:55:58 +0800409 OperationalStatus::functional(value);
410}
411
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800412} // namespace smbios
413} // namespace phosphor