blob: c84bc51749a4012727aedfc722b8c03f073008ec [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
Cheng C Yang8c3fab62019-12-19 00:51:06 +080024namespace phosphor
25{
26namespace smbios
27{
28
John Edward Broadbentefd41542022-12-13 16:39:18 -080029#ifdef DIMM_ONLY_LOCATOR
30bool onlyDimmLocationCode = true;
31#else
32bool onlyDimmLocationCode = false;
33#endif
34
Cheng C Yang8c3fab62019-12-19 00:51:06 +080035using DeviceType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070036 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::DeviceType;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080037
kasunath2eca4fe2022-08-17 17:30:07 -070038using EccType =
Jason M. Bills33ae81f2023-04-26 09:06:08 -070039 sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::Ecc;
kasunath2eca4fe2022-08-17 17:30:07 -070040
Cheng C Yang8c3fab62019-12-19 00:51:06 +080041static constexpr uint16_t maxOldDimmSize = 0x7fff;
Brandon Kim5a122a62023-05-04 04:25:03 +000042void Dimm::memoryInfoUpdate(uint8_t* smbiosTableStorage,
43 const std::string& motherboard)
Cheng C Yang8c3fab62019-12-19 00:51:06 +080044{
Brandon Kim5a122a62023-05-04 04:25:03 +000045 storage = smbiosTableStorage;
46 motherboardPath = motherboard;
47
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080048 uint8_t* dataIn = storage;
Cheng C Yang8c3fab62019-12-19 00:51:06 +080049
50 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
51
52 if (dataIn == nullptr)
53 {
54 return;
55 }
56 for (uint8_t index = 0; index < dimmNum; index++)
57 {
58 dataIn = smbiosNextPtr(dataIn);
59 if (dataIn == nullptr)
60 {
61 return;
62 }
63 dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
64 if (dataIn == nullptr)
65 {
66 return;
67 }
68 }
69
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +080070 auto memoryInfo = reinterpret_cast<struct MemoryInfo*>(dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080071
72 memoryDataWidth(memoryInfo->dataWidth);
73
74 if (memoryInfo->size == maxOldDimmSize)
75 {
76 dimmSizeExt(memoryInfo->extendedSize);
77 }
78 else
79 {
80 dimmSize(memoryInfo->size);
81 }
Tom Tung39cc3682023-03-15 04:44:04 +080082 // If the size is 0, no memory device is installed in the socket.
83 const auto isDimmPresent = memoryInfo->size > 0;
84 present(isDimmPresent);
85 functional(isDimmPresent);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080086
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +000087 dimmDeviceLocator(memoryInfo->bankLocator, memoryInfo->deviceLocator,
88 memoryInfo->length, dataIn);
Cheng C Yang8c3fab62019-12-19 00:51:06 +080089 dimmType(memoryInfo->memoryType);
90 dimmTypeDetail(memoryInfo->typeDetail);
91 maxMemorySpeedInMhz(memoryInfo->speed);
92 dimmManufacturer(memoryInfo->manufacturer, memoryInfo->length, dataIn);
93 dimmSerialNum(memoryInfo->serialNum, memoryInfo->length, dataIn);
94 dimmPartNum(memoryInfo->partNum, memoryInfo->length, dataIn);
95 memoryAttributes(memoryInfo->attributes);
96 memoryConfiguredSpeedInMhz(memoryInfo->confClockSpeed);
97
kasunath2eca4fe2022-08-17 17:30:07 -070098 updateEccType(memoryInfo->phyArrayHandle);
99
Jie Yange7cf3192021-08-20 11:21:43 -0700100 if (!motherboardPath.empty())
101 {
102 std::vector<std::tuple<std::string, std::string, std::string>> assocs;
103 assocs.emplace_back("chassis", "memories", motherboardPath);
104 association::associations(assocs);
105 }
106
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800107 return;
108}
109
kasunath2eca4fe2022-08-17 17:30:07 -0700110void Dimm::updateEccType(uint16_t exPhyArrayHandle)
111{
112 uint8_t* dataIn = storage;
113
114 while (dataIn != nullptr)
115 {
116 dataIn = getSMBIOSTypePtr(dataIn, physicalMemoryArrayType);
117 if (dataIn == nullptr)
118 {
119 phosphor::logging::log<phosphor::logging::level::ERR>(
120 "Failed to get SMBIOS table type-16 data.");
121 return;
122 }
123
124 auto info = reinterpret_cast<struct PhysicalMemoryArrayInfo*>(dataIn);
125 if (info->handle == exPhyArrayHandle)
126 {
127 std::map<uint8_t, EccType>::const_iterator it =
128 dimmEccTypeMap.find(info->memoryErrorCorrection);
129 if (it == dimmEccTypeMap.end())
130 {
131 ecc(EccType::NoECC);
132 }
133 else
134 {
135 ecc(it->second);
136 }
137 return;
138 }
139
140 dataIn = smbiosNextPtr(dataIn);
141 }
142 phosphor::logging::log<phosphor::logging::level::ERR>(
143 "Failed find the corresponding SMBIOS table type-16 data for dimm:",
144 phosphor::logging::entry("DIMM:%d", dimmNum));
145}
146
147EccType Dimm::ecc(EccType value)
148{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700149 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::ecc(
kasunath2eca4fe2022-08-17 17:30:07 -0700150 value);
151}
152
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800153uint16_t Dimm::memoryDataWidth(uint16_t value)
154{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700155 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800156 memoryDataWidth(value);
157}
158
159static constexpr uint16_t baseNewVersionDimmSize = 0x8000;
160static constexpr uint16_t dimmSizeUnit = 1024;
161void Dimm::dimmSize(const uint16_t size)
162{
Joseph Fu0b1d9422023-03-31 17:47:12 +0800163 uint32_t result = size & maxOldDimmSize;
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800164 if (0 == (size & baseNewVersionDimmSize))
165 {
166 result = result * dimmSizeUnit;
167 }
168 memorySizeInKB(result);
169}
170
Joseph Fu0b1d9422023-03-31 17:47:12 +0800171void Dimm::dimmSizeExt(uint32_t size)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800172{
173 size = size * dimmSizeUnit;
174 memorySizeInKB(size);
175}
176
Jason M. Billse7770992021-05-14 13:24:33 -0700177size_t Dimm::memorySizeInKB(size_t value)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800178{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700179 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800180 memorySizeInKB(value);
181}
182
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000183void Dimm::dimmDeviceLocator(const uint8_t bankLocatorPositionNum,
184 const uint8_t deviceLocatorPositionNum,
185 const uint8_t structLen, uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800186{
Patrick Williamsc39d3df2023-05-10 07:51:14 -0500187 std::string deviceLocator = positionToString(deviceLocatorPositionNum,
188 structLen, dataIn);
189 std::string bankLocator = positionToString(bankLocatorPositionNum,
190 structLen, dataIn);
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000191
192 std::string result;
John Edward Broadbentefd41542022-12-13 16:39:18 -0800193 if (bankLocator.empty() || onlyDimmLocationCode)
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000194 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800195 result = deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000196 }
197 else
198 {
John Edward Broadbentefd41542022-12-13 16:39:18 -0800199 result = bankLocator + " " + deviceLocator;
Konstantin Aladyshev744b35a2022-11-02 08:34:27 +0000200 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800201
202 memoryDeviceLocator(result);
Jie Yang31720392021-07-22 21:45:45 -0700203
204 locationCode(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800205}
206
207std::string Dimm::memoryDeviceLocator(std::string value)
208{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700209 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800210 memoryDeviceLocator(value);
211}
212
213void Dimm::dimmType(const uint8_t type)
214{
215 std::map<uint8_t, DeviceType>::const_iterator it = dimmTypeTable.find(type);
216 if (it == dimmTypeTable.end())
217 {
218 memoryType(DeviceType::Unknown);
219 }
220 else
221 {
222 memoryType(it->second);
223 }
224}
225
226DeviceType Dimm::memoryType(DeviceType value)
227{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700228 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800229 memoryType(value);
230}
231
232void Dimm::dimmTypeDetail(uint16_t detail)
233{
234 std::string result;
235 for (uint8_t index = 0; index < (8 * sizeof(detail)); index++)
236 {
237 if (detail & 0x01)
238 {
239 result += detailTable[index];
240 }
241 detail >>= 1;
242 }
243 memoryTypeDetail(result);
244}
245
246std::string Dimm::memoryTypeDetail(std::string value)
247{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700248 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800249 memoryTypeDetail(value);
250}
251
252uint16_t Dimm::maxMemorySpeedInMhz(uint16_t value)
253{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700254 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800255 maxMemorySpeedInMhz(value);
256}
257
258void Dimm::dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800259 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800260{
261 std::string result = positionToString(positionNum, structLen, dataIn);
262
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530263 if (result == "NO DIMM")
264 {
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530265 // No dimm presence so making manufacturer value as "" (instead of
266 // NO DIMM - as there won't be any manufacturer for DIMM which is not
267 // present).
268 result = "";
269 }
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800270 manufacturer(result);
271}
272
273std::string Dimm::manufacturer(std::string value)
274{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700275 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800276 Asset::manufacturer(value);
277}
278
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530279bool Dimm::present(bool value)
280{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700281 return sdbusplus::server::xyz::openbmc_project::inventory::Item::present(
Joshi-Mansi33c948a2021-03-20 00:58:50 +0530282 value);
283}
284
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800285void Dimm::dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800286 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800287{
288 std::string result = positionToString(positionNum, structLen, dataIn);
289
290 serialNumber(result);
291}
292
293std::string Dimm::serialNumber(std::string value)
294{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700295 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800296 Asset::serialNumber(value);
297}
298
299void Dimm::dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
Cheng C Yang2ca7a0f2019-12-19 10:46:42 +0800300 uint8_t* dataIn)
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800301{
302 std::string result = positionToString(positionNum, structLen, dataIn);
303
kasunath634ec6a2022-07-25 15:34:17 -0700304 // Part number could contain spaces at the end. Eg: "abcd123 ". Since its
305 // unnecessary, we should remove them.
306 boost::algorithm::trim_right(result);
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800307 partNumber(result);
308}
309
310std::string Dimm::partNumber(std::string value)
311{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700312 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800313 Asset::partNumber(value);
314}
315
Jie Yang31720392021-07-22 21:45:45 -0700316std::string Dimm::locationCode(std::string value)
317{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700318 return sdbusplus::server::xyz::openbmc_project::inventory::decorator::
Jie Yang31720392021-07-22 21:45:45 -0700319 LocationCode::locationCode(value);
320}
321
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800322uint8_t Dimm::memoryAttributes(uint8_t value)
323{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700324 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800325 memoryAttributes(value);
326}
327
328uint16_t Dimm::memoryConfiguredSpeedInMhz(uint16_t value)
329{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700330 return sdbusplus::server::xyz::openbmc_project::inventory::item::Dimm::
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800331 memoryConfiguredSpeedInMhz(value);
332}
333
Tim Leedc469c72021-07-20 10:55:58 +0800334bool Dimm::functional(bool value)
335{
Jason M. Bills33ae81f2023-04-26 09:06:08 -0700336 return sdbusplus::server::xyz::openbmc_project::state::decorator::
Tim Leedc469c72021-07-20 10:55:58 +0800337 OperationalStatus::functional(value);
338}
339
Cheng C Yang8c3fab62019-12-19 00:51:06 +0800340} // namespace smbios
341} // namespace phosphor