Add DIMM dbus service for MDR V2

Add all DIMM information in smbios table and provide dbus
interface for redfish to get DIMM information.

Tested:
DC cycle the system and waiting for BIOS entring setup page.
Correct DIMM information should show in Redfish.

Signed-off-by: Cheng C Yang <cheng.c.yang@linux.intel.com>
Change-Id: I8c2678c5d40f4d1fde81292b21aa94e80b0ab586
diff --git a/include/dimm.hpp b/include/dimm.hpp
new file mode 100644
index 0000000..ec0a4e9
--- /dev/null
+++ b/include/dimm.hpp
@@ -0,0 +1,144 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#pragma once
+#include "smbios.hpp"
+
+#include <xyz/openbmc_project/Inventory/Decorator/Asset/server.hpp>
+#include <xyz/openbmc_project/Inventory/Item/Dimm/server.hpp>
+
+namespace phosphor
+{
+
+namespace smbios
+{
+
+using DeviceType =
+    sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::DeviceType;
+
+class Dimm
+    : sdbusplus::server::object::object<
+          sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm>,
+      sdbusplus::server::object::object<
+          sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::Asset>
+{
+  public:
+    Dimm() = delete;
+    ~Dimm() = default;
+    Dimm(const Dimm&) = delete;
+    Dimm& operator=(const Dimm&) = delete;
+    Dimm(Dimm&&) = default;
+    Dimm& operator=(Dimm&&) = default;
+
+    Dimm(sdbusplus::bus::bus& bus, const std::string& objPath,
+         const uint8_t& dimmId, uint8_t* smbiosTableStorage) :
+
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm>(
+            bus, objPath.c_str()),
+        sdbusplus::server::object::object<
+            sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
+                Asset>(bus, objPath.c_str()),
+        dimmNum(dimmId), storage(smbiosTableStorage)
+    {
+        memoryInfoUpdate();
+    }
+
+    void memoryInfoUpdate(void);
+
+    uint16_t memoryDataWidth(uint16_t value) override;
+    uint32_t memorySizeInKB(uint32_t value) override;
+    std::string memoryDeviceLocator(std::string value) override;
+    DeviceType memoryType(DeviceType value) override;
+    std::string memoryTypeDetail(std::string value) override;
+    uint16_t maxMemorySpeedInMhz(uint16_t value) override;
+    std::string manufacturer(std::string value) override;
+    std::string serialNumber(std::string value) override;
+    std::string partNumber(std::string value) override;
+    uint8_t memoryAttributes(uint8_t value) override;
+    uint16_t memoryConfiguredSpeedInMhz(uint16_t value) override;
+
+  private:
+    uint8_t dimmNum;
+
+    uint8_t* storage;
+
+    void dimmSize(const uint16_t size);
+    void dimmSizeExt(const uint32_t size);
+    void dimmDeviceLocator(const uint8_t positionNum, const uint8_t structLen,
+                           uint8_t* dataIn);
+    void dimmType(const uint8_t type);
+    void dimmTypeDetail(const uint16_t detail);
+    void dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
+                          uint8_t* dataIn);
+    void dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
+                       uint8_t* dataIn);
+    void dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
+                     uint8_t* dataIn);
+
+    struct MemoryInfo
+    {
+        uint8_t type;
+        uint8_t length;
+        uint16_t handle;
+        uint16_t phyArrayHandle;
+        uint16_t errInfoHandle;
+        uint16_t totalWidth;
+        uint16_t dataWidth;
+        uint16_t size;
+        uint8_t formFactor;
+        uint8_t deviceSet;
+        uint8_t deviceLocator;
+        uint8_t bankLocator;
+        uint8_t memoryType;
+        uint16_t typeDetail;
+        uint16_t speed;
+        uint8_t manufacturer;
+        uint8_t serialNum;
+        uint8_t assetTag;
+        uint8_t partNum;
+        uint8_t attributes;
+        uint32_t extendedSize;
+        uint16_t confClockSpeed;
+    } __attribute__((packed));
+};
+
+const std::map<uint8_t, DeviceType> dimmTypeTable = {
+    {0x1, DeviceType::Other},         {0x2, DeviceType::Unknown},
+    {0x3, DeviceType::DRAM},          {0x4, DeviceType::EDRAM},
+    {0x5, DeviceType::VRAM},          {0x6, DeviceType::SRAM},
+    {0x7, DeviceType::RAM},           {0x8, DeviceType::ROM},
+    {0x9, DeviceType::FLASH},         {0xa, DeviceType::EEPROM},
+    {0xb, DeviceType::FEPROM},        {0xc, DeviceType::EPROM},
+    {0xd, DeviceType::CDRAM},         {0xe, DeviceType::ThreeDRAM},
+    {0xf, DeviceType::SDRAM},         {0x10, DeviceType::DDR_SGRAM},
+    {0x11, DeviceType::RDRAM},        {0x12, DeviceType::DDR},
+    {0x13, DeviceType::DDR2},         {0x14, DeviceType::DDR2_SDRAM_FB_DIMM},
+    {0x18, DeviceType::DDR3},         {0x19, DeviceType::FBD2},
+    {0x1a, DeviceType::DDR4},         {0x1b, DeviceType::LPDDR_SDRAM},
+    {0x1c, DeviceType::LPDDR2_SDRAM}, {0x1d, DeviceType::LPDDR3_SDRAM},
+    {0x1e, DeviceType::LPDDR4_SDRAM}, {0x1f, DeviceType::Logical},
+    {0x20, DeviceType::HBM},          {0x21, DeviceType::HBM2}};
+
+const std::array<std::string, 16> detailTable{
+    "Reserved",      "Other",         "Unknown",     "Fast-paged",
+    "Static column", "Pseudo-static", "RAMBUS",      "Synchronous",
+    "CMOS",          "EDO",           "Window DRAM", "Cache DRAM",
+    "Non-volatile",  "Registered",    "Unbuffered",  "LRDIMM"};
+
+} // namespace smbios
+
+} // namespace phosphor
diff --git a/include/mdrv2.hpp b/include/mdrv2.hpp
index 363a004..3dfedf5 100644
--- a/include/mdrv2.hpp
+++ b/include/mdrv2.hpp
@@ -173,7 +173,9 @@
     void systemInfoUpdate(void);
 
     int getTotalCpuSlot(void);
+    int getTotalDimmSlot(void);
     std::vector<std::unique_ptr<Cpu>> cpus;
+    std::vector<std::unique_ptr<Dimm>> dimms;
 };
 
 } // namespace smbios
diff --git a/src/dimm.cpp b/src/dimm.cpp
new file mode 100644
index 0000000..db4170a
--- /dev/null
+++ b/src/dimm.cpp
@@ -0,0 +1,224 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+
+#include "dimm.hpp"
+
+#include "mdrv2.hpp"
+
+namespace phosphor
+{
+namespace smbios
+{
+
+using DeviceType =
+    sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::DeviceType;
+
+static constexpr uint16_t maxOldDimmSize = 0x7fff;
+void Dimm::memoryInfoUpdate(void)
+{
+    uint8_t *dataIn = storage;
+
+    dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
+
+    if (dataIn == nullptr)
+    {
+        return;
+    }
+    for (uint8_t index = 0; index < dimmNum; index++)
+    {
+        dataIn = smbiosNextPtr(dataIn);
+        if (dataIn == nullptr)
+        {
+            return;
+        }
+        dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
+        if (dataIn == nullptr)
+        {
+            return;
+        }
+    }
+
+    auto memoryInfo = reinterpret_cast<struct MemoryInfo *>(dataIn);
+
+    memoryDataWidth(memoryInfo->dataWidth);
+
+    if (memoryInfo->size == maxOldDimmSize)
+    {
+        dimmSizeExt(memoryInfo->extendedSize);
+    }
+    else
+    {
+        dimmSize(memoryInfo->size);
+    }
+
+    dimmDeviceLocator(memoryInfo->deviceLocator, memoryInfo->length, dataIn);
+    dimmType(memoryInfo->memoryType);
+    dimmTypeDetail(memoryInfo->typeDetail);
+    maxMemorySpeedInMhz(memoryInfo->speed);
+    dimmManufacturer(memoryInfo->manufacturer, memoryInfo->length, dataIn);
+    dimmSerialNum(memoryInfo->serialNum, memoryInfo->length, dataIn);
+    dimmPartNum(memoryInfo->partNum, memoryInfo->length, dataIn);
+    memoryAttributes(memoryInfo->attributes);
+    memoryConfiguredSpeedInMhz(memoryInfo->confClockSpeed);
+
+    return;
+}
+
+uint16_t Dimm::memoryDataWidth(uint16_t value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryDataWidth(value);
+}
+
+static constexpr uint16_t baseNewVersionDimmSize = 0x8000;
+static constexpr uint16_t dimmSizeUnit = 1024;
+void Dimm::dimmSize(const uint16_t size)
+{
+    uint32_t result = size & maxOldDimmSize;
+    if (0 == (size & baseNewVersionDimmSize))
+    {
+        result = result * dimmSizeUnit;
+    }
+    memorySizeInKB(result);
+}
+
+void Dimm::dimmSizeExt(uint32_t size)
+{
+    size = size * dimmSizeUnit;
+    memorySizeInKB(size);
+}
+
+uint32_t Dimm::memorySizeInKB(uint32_t value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memorySizeInKB(value);
+}
+
+void Dimm::dimmDeviceLocator(const uint8_t positionNum, const uint8_t structLen,
+                             uint8_t *dataIn)
+{
+    std::string result = positionToString(positionNum, structLen, dataIn);
+
+    memoryDeviceLocator(result);
+}
+
+std::string Dimm::memoryDeviceLocator(std::string value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryDeviceLocator(value);
+}
+
+void Dimm::dimmType(const uint8_t type)
+{
+    std::map<uint8_t, DeviceType>::const_iterator it = dimmTypeTable.find(type);
+    if (it == dimmTypeTable.end())
+    {
+        memoryType(DeviceType::Unknown);
+    }
+    else
+    {
+        memoryType(it->second);
+    }
+}
+
+DeviceType Dimm::memoryType(DeviceType value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryType(value);
+}
+
+void Dimm::dimmTypeDetail(uint16_t detail)
+{
+    std::string result;
+    for (uint8_t index = 0; index < (8 * sizeof(detail)); index++)
+    {
+        if (detail & 0x01)
+        {
+            result += detailTable[index];
+        }
+        detail >>= 1;
+    }
+    memoryTypeDetail(result);
+}
+
+std::string Dimm::memoryTypeDetail(std::string value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryTypeDetail(value);
+}
+
+uint16_t Dimm::maxMemorySpeedInMhz(uint16_t value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        maxMemorySpeedInMhz(value);
+}
+
+void Dimm::dimmManufacturer(const uint8_t positionNum, const uint8_t structLen,
+                            uint8_t *dataIn)
+{
+    std::string result = positionToString(positionNum, structLen, dataIn);
+
+    manufacturer(result);
+}
+
+std::string Dimm::manufacturer(std::string value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
+        Asset::manufacturer(value);
+}
+
+void Dimm::dimmSerialNum(const uint8_t positionNum, const uint8_t structLen,
+                         uint8_t *dataIn)
+{
+    std::string result = positionToString(positionNum, structLen, dataIn);
+
+    serialNumber(result);
+}
+
+std::string Dimm::serialNumber(std::string value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
+        Asset::serialNumber(value);
+}
+
+void Dimm::dimmPartNum(const uint8_t positionNum, const uint8_t structLen,
+                       uint8_t *dataIn)
+{
+    std::string result = positionToString(positionNum, structLen, dataIn);
+
+    partNumber(result);
+}
+
+std::string Dimm::partNumber(std::string value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::
+        Asset::partNumber(value);
+}
+
+uint8_t Dimm::memoryAttributes(uint8_t value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryAttributes(value);
+}
+
+uint16_t Dimm::memoryConfiguredSpeedInMhz(uint16_t value)
+{
+    return sdbusplus::xyz::openbmc_project::Inventory::Item::server::Dimm::
+        memoryConfiguredSpeedInMhz(value);
+}
+
+} // namespace smbios
+} // namespace phosphor
diff --git a/src/mdrv2.cpp b/src/mdrv2.cpp
index 8452741..c392127 100644
--- a/src/mdrv2.cpp
+++ b/src/mdrv2.cpp
@@ -379,6 +379,23 @@
         cpus.emplace_back(std::make_unique<phosphor::smbios::Cpu>(
             bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage));
     }
+
+    dimms.clear();
+
+    num = getTotalDimmSlot();
+    if (num == -1)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "get dimm total slot failed");
+        return;
+    }
+
+    for (int index = 0; index < num; index++)
+    {
+        std::string path = dimmPath + std::to_string(index);
+        dimms.emplace_back(std::make_unique<phosphor::smbios::Dimm>(
+            bus, path, index, smbiosDir.dir[smbiosDirIndex].dataStorage));
+    }
 }
 
 int MDR_V2::getTotalCpuSlot()
@@ -415,6 +432,40 @@
     return num;
 }
 
+int MDR_V2::getTotalDimmSlot()
+{
+    uint8_t* dataIn = smbiosDir.dir[smbiosDirIndex].dataStorage;
+    uint8_t num = 0;
+
+    if (dataIn == nullptr)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Fail to get dimm total slot - no storage data");
+        return -1;
+    }
+
+    while (1)
+    {
+        dataIn = getSMBIOSTypePtr(dataIn, memoryDeviceType);
+        if (dataIn == nullptr)
+        {
+            break;
+        }
+        num++;
+        dataIn = smbiosNextPtr(dataIn);
+        if (dataIn == nullptr)
+        {
+            break;
+        }
+        if (num >= limitEntryLen)
+        {
+            break;
+        }
+    }
+
+    return num;
+}
+
 bool MDR_V2::agentSynchronizeData()
 {
     struct MDRSMBIOSHeader mdr2SMBIOS;