blob: 054c18df890f7d63c63562bf378b2d52e0f084e5 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_AMD_CPU_SENSOR_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_AMD_CPU_SENSOR_H_
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "boost/asio.hpp" //NOLINT: boost::asio is commonly used in BMC
#include "psu_sensor_config.pb.h"
#include "sensor_instance_properties.pb.h"
#include "tlbmc/hal/sysfs/hwmon.h"
#include "tlbmc/hal/sysfs/i3c.h"
#include "sensor.pb.h"
#include "tlbmc/sensors/psu_sensor.h"
#include "tlbmc/sensors/sensor.h"
namespace milotic_tlbmc {
struct AmdCpuSensorSysfsPaths {
std::string i3c_sysfs_drivers_path = "/sys/bus/i3c/drivers/";
std::string i3c_controller_driver_path =
"/sys/bus/platform/drivers/mipi-i3c-hci/";
};
class AmdCpuSensor : public PsuSensor {
public:
static absl::StatusOr<std::vector<std::shared_ptr<Sensor>>> Create(
const PsuSensorConfig& config,
const std::shared_ptr<boost::asio::io_context>& io_context,
I3cSysfs& i3c_sysfs,
std::optional<NotificationCb> on_batch_notify = std::nullopt,
AmdCpuSensorSysfsPaths paths = AmdCpuSensorSysfsPaths());
static bool IsAmdCpuSensor(PsuSensorType psu_sensor_type);
// Resets the shared reload state. Must be called in test SetUp to ensure
// test case isolation.
static void ResetSharedStateForTest();
void RefreshHwmonDevpath();
AmdCpuSensor() = default;
AmdCpuSensor(Token token, PsuSensorType sensor_type,
const std::string& input_dev_path, const std::string& label,
const HalCommonConfig& hal_common_config,
const EntityCommonConfig& entity_common_config,
const SensorInstanceProperties& sensor_instance_properties,
const std::shared_ptr<boost::asio::io_context>& io_context,
std::optional<NotificationCb> on_batch_notify,
I3cSysfs& i3c_sysfs,
AmdCpuSensorSysfsPaths paths = AmdCpuSensorSysfsPaths())
: PsuSensor(token, sensor_type, input_dev_path, hal_common_config,
entity_common_config, sensor_instance_properties, io_context,
on_batch_notify, i3c_sysfs),
label_(label),
i3c_sysfs_(&i3c_sysfs),
i3c_sysfs_drivers_path_(paths.i3c_sysfs_drivers_path),
i3c_controller_driver_path_(paths.i3c_controller_driver_path) {}
void PowerOffToOnCallback(bool setup_run) override;
void PowerOnToOffCallback(bool setup_run) override;
void OSInactiveToStandbyCallback(bool setup_run) override;
void OSStandbyToInactiveCallback(bool setup_run) override;
protected:
// Performs the safe driver reload.
absl::Status ReloadDriver(uint64_t bus);
// Helper routines previously in I3cDriverReloader.
absl::Status UnbindI3cBusDevices(uint64_t bus);
absl::Status UnbindI3cBusController(uint64_t bus);
absl::Status BindI3cBusController(uint64_t bus);
absl::Mutex controller_mutex_;
// key: i3c bus id, value: vector of unbound controllers
absl::flat_hash_map<uint64_t, std::vector<std::string>>
unbound_i3c_controllers_ ABSL_GUARDED_BY(controller_mutex_);
std::string label_;
I3cSysfs* i3c_sysfs_ = nullptr;
std::string i3c_sysfs_drivers_path_;
std::string i3c_controller_driver_path_;
private:
struct SharedState {
struct ReloadResult {
absl::Time time;
absl::Status status;
};
absl::Mutex global_mutex;
absl::flat_hash_map<uint64_t, ReloadResult> last_reload;
absl::flat_hash_map<uint64_t, std::unique_ptr<absl::Mutex>> bus_mutexes;
};
static SharedState& GetSharedState();
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_SENSORS_AMD_CPU_SENSOR_H_