nvidia-gpu: add energy sensor

This commit introduces a energy sensor for the GPU.

Tested: Build an image for gb200nvl-obmc machine with the following
patches cherry picked. This patches are needed to enable the mctp stack.

https://gerrit.openbmc.org/c/openbmc/openbmc/+/79422

```
$ curl -s -k -u 'root:0penBmc' https://10.137.203.137/redfish/v1/Chassis/NVIDIA_GB200_1/Sensors/energy_NVIDIA_GB200_GPU_0_Energy_0
{
  "@odata.id": "/redfish/v1/Chassis/NVIDIA_GB200_1/Sensors/energy_NVIDIA_GB200_GPU_0_Energy_0",
  "@odata.type": "#Sensor.v1_2_0.Sensor",
  "Id": "energy_NVIDIA_GB200_GPU_0_Energy_0",
  "Name": "NVIDIA GB200 GPU 0 Energy 0",
  "Reading": 269947.856,
  "ReadingRangeMax": 1.8446744073709552e+16,
  "ReadingRangeMin": 0.0,
  "ReadingType": "EnergyJoules",
  "ReadingUnits": "J",
  "Status": {
    "Health": "OK",
    "State": "Enabled"
  }
}%
```

Change-Id: I6f53ab2a83eedd54005bbdcd781dc8d320d7f26a
Signed-off-by: Harshit Aghera <haghera@nvidia.com>
diff --git a/src/nvidia-gpu/NvidiaGpuMctpVdm.cpp b/src/nvidia-gpu/NvidiaGpuMctpVdm.cpp
index 2355bf9..b70e0c2 100644
--- a/src/nvidia-gpu/NvidiaGpuMctpVdm.cpp
+++ b/src/nvidia-gpu/NvidiaGpuMctpVdm.cpp
@@ -284,5 +284,70 @@
 
     return 0;
 }
+
+int encodeGetCurrentEnergyCounterRequest(uint8_t instanceId, uint8_t sensorId,
+                                         std::span<uint8_t> buf)
+{
+    if (buf.size() < sizeof(GetTemperatureReadingRequest))
+    {
+        return EINVAL;
+    }
+
+    auto* msg = reinterpret_cast<GetCurrentEnergyCounterRequest*>(buf.data());
+
+    ocp::accelerator_management::BindingPciVidInfo header{};
+    header.ocp_accelerator_management_msg_type =
+        static_cast<uint8_t>(ocp::accelerator_management::MessageType::REQUEST);
+    header.instance_id = instanceId &
+                         ocp::accelerator_management::instanceIdBitMask;
+    header.msg_type = static_cast<uint8_t>(MessageType::PLATFORM_ENVIRONMENTAL);
+
+    auto rc = packHeader(header, msg->hdr.msgHdr.hdr);
+
+    if (rc != 0)
+    {
+        return rc;
+    }
+
+    msg->hdr.command = static_cast<uint8_t>(
+        PlatformEnvironmentalCommands::GET_CURRENT_ENERGY_COUNTER);
+    msg->hdr.data_size = sizeof(sensorId);
+    msg->sensor_id = sensorId;
+
+    return 0;
+}
+
+int decodeGetCurrentEnergyCounterResponse(
+    std::span<const uint8_t> buf,
+    ocp::accelerator_management::CompletionCode& cc, uint16_t& reasonCode,
+    uint64_t& energy)
+{
+    auto rc =
+        ocp::accelerator_management::decodeReasonCodeAndCC(buf, cc, reasonCode);
+
+    if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS)
+    {
+        return rc;
+    }
+
+    if (buf.size() < sizeof(GetCurrentPowerDrawResponse))
+    {
+        return EINVAL;
+    }
+
+    const auto* response =
+        reinterpret_cast<const GetCurrentEnergyCounterResponse*>(buf.data());
+
+    const uint16_t dataSize = le16toh(response->hdr.data_size);
+
+    if (dataSize != sizeof(uint64_t))
+    {
+        return EINVAL;
+    }
+
+    energy = le32toh(response->energy);
+
+    return 0;
+}
 // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
 } // namespace gpu