blob: 5a02326b71ff9993877dce079aded271fbdb15d2 [file] [log] [blame]
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
* AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
*/
#include "GpuTLimitSensor.hpp"
#include "SensorPaths.hpp"
#include "Thresholds.hpp"
#include "UpdatableSensor.hpp"
#include "Utils.hpp"
#include <bits/basic_string.h>
#include <GpuDevice.hpp>
#include <GpuMctpVdm.hpp>
#include <MctpRequester.hpp>
#include <OcpMctpVdm.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
using namespace std::literals;
constexpr uint8_t gpuTLimitSensorIdm{2};
static constexpr double gpuTLimitSensorMaxReading = 127;
static constexpr double gpuTLimitSensorMinReading = -128;
GpuTLimitSensor::GpuTLimitSensor(
std::shared_ptr<sdbusplus::asio::connection>& conn,
mctp::MctpRequester& mctpRequester, const std::string& name,
const std::string& sensorConfiguration, uint8_t eid,
sdbusplus::asio::object_server& objectServer,
std::vector<thresholds::Threshold>&& thresholdData) :
GpuSensor(escapeName(name), std::move(thresholdData), sensorConfiguration,
"temperature", false, true, gpuTLimitSensorMaxReading,
gpuTLimitSensorMinReading, conn),
eid(eid), sensorId{gpuTLimitSensorIdm}, mctpRequester(mctpRequester),
objectServer(objectServer)
{
std::string dbusPath =
sensorPathPrefix + "temperature/"s + escapeName(name);
sensorInterface = objectServer.add_interface(
dbusPath, "xyz.openbmc_project.Sensor.Value");
for (const auto& threshold : thresholds)
{
std::string interface = thresholds::getInterface(threshold.level);
thresholdInterfaces[static_cast<size_t>(threshold.level)] =
objectServer.add_interface(dbusPath, interface);
}
association = objectServer.add_interface(dbusPath, association::interface);
setInitialProperties(sensor_paths::unitDegreesC);
}
GpuTLimitSensor::~GpuTLimitSensor()
{
for (const auto& iface : thresholdInterfaces)
{
objectServer.remove_interface(iface);
}
objectServer.remove_interface(sensorInterface);
objectServer.remove_interface(association);
}
void GpuTLimitSensor::checkThresholds()
{
thresholds::checkThresholds(this);
}
void GpuTLimitSensor::update()
{
std::vector<uint8_t> reqMsg(
sizeof(ocp::accelerator_management::BindingPciVid) +
sizeof(gpu::GetTemperatureReadingRequest));
auto* msg = new (reqMsg.data()) ocp::accelerator_management::Message;
auto rc = gpu::encodeGetTemperatureReadingRequest(0, sensorId, *msg);
if (rc != ocp::accelerator_management::CompletionCode::SUCCESS)
{
lg2::error(
"GpuTLimitSensor::update(): gpuEncodeGetTemperatureReadingRequest failed, rc={RC}",
"RC", static_cast<int>(rc));
return;
}
mctpRequester.sendRecvMsg(
eid, reqMsg,
[this](int sendRecvMsgResult, std::vector<uint8_t> respMsg) {
if (sendRecvMsgResult != 0)
{
lg2::error(
"GpuTLimitSensor::update(): MctpRequester::sendRecvMsg() failed, rc={RC}",
"RC", sendRecvMsgResult);
return;
}
if (respMsg.empty())
{
lg2::error(
"GpuTLimitSensor::update(): MctpRequester::sendRecvMsg() failed, respMsgLen=0");
return;
}
uint8_t cc = 0;
uint16_t reasonCode = 0;
double tempValue = 0;
auto rc = gpu::decodeGetTemperatureReadingResponse(
*new (respMsg.data()) ocp::accelerator_management::Message,
respMsg.size(), cc, reasonCode, tempValue);
if (rc != ocp::accelerator_management::CompletionCode::SUCCESS ||
cc != static_cast<uint8_t>(
ocp::accelerator_management::CompletionCode::SUCCESS))
{
lg2::error(
"GpuTLimitSensor::update(): gpuDecodeGetTemperatureReadingResponse() failed, rc={RC} cc={CC} reasonCode={RESC}",
"RC", static_cast<int>(rc), "CC", cc, "RESC", reasonCode);
return;
}
updateValue(tempValue);
});
}