blob: 6f0c056460f7b514266fa81311bca37c254732ae [file] [log] [blame]
Chris Caina8857c52021-01-27 11:53:05 -06001#pragma once
2
3#include "occ_errors.hpp"
George Liuf3b75142021-06-10 11:22:50 +08004#include "utils.hpp"
Chris Caina8857c52021-01-27 11:53:05 -06005
Eddie Jamesb2515462022-11-08 17:01:04 -06006#include <fmt/ostream.h>
7
Chris Caina8857c52021-01-27 11:53:05 -06008#include <org/open_power/OCC/PassThrough/server.hpp>
9#include <sdbusplus/bus.hpp>
10#include <sdbusplus/server/object.hpp>
George Liub5ca1012021-09-10 12:53:11 +080011
Chris Caina8857c52021-01-27 11:53:05 -060012#include <string>
13
14namespace open_power
15{
16namespace occ
17{
18
19// For waiting on signals
20namespace sdbusRule = sdbusplus::bus::match::rules;
21
Chris Cain78e86012021-03-04 16:15:31 -060022enum class CmdType
23{
24 POLL = 0x00,
25 CLEAR_ERROR_LOG = 0x12,
26 SET_MODE_AND_STATE = 0x20,
27 SET_CONFIG_DATA = 0x21,
28 SET_USER_PCAP = 0x22,
29 RESET_PREP = 0x25,
Chris Cain17257672021-10-22 13:41:03 -050030 SEND_AMBIENT = 0x30,
Chris Cain78e86012021-03-04 16:15:31 -060031 DEBUG_PASS_THROUGH = 0x40,
32 AME_PASS_THROUGH = 0x41,
33 GET_FIELD_DEBUG_DATA = 0x42,
34 MFG_TEST = 0x53
35};
36
37enum class OccState
38{
39 NO_CHANGE = 0x00,
40 STANDBY = 0x01,
41 OBSERVATION = 0x02,
42 ACTIVE = 0x03,
43 SAFE = 0x04,
Sheldon Bailey373af752022-02-21 15:14:00 -060044 CHARACTERIZATION = 0x05
Chris Cain78e86012021-03-04 16:15:31 -060045};
46
47enum class SysPwrMode
48{
49 NO_CHANGE = 0,
Chris Cain36f9cde2021-11-22 11:18:21 -060050 STATIC = 0x01, // Static Base Frequencey
Chris Cain78e86012021-03-04 16:15:31 -060051 SFP = 0x03, // Static Frequency Point (requires freqPt)
52 SAFE = 0x04, // reported when system is in SAFE mode (not settable)
53 POWER_SAVING = 0x05, // Static Power Saving
Chris Cain36f9cde2021-11-22 11:18:21 -060054 MAX_FREQ = 0x09, // Maximum Frequency (per chip)
Chris Cain78e86012021-03-04 16:15:31 -060055 DYNAMIC_PERF = 0x0A, // Dynamic / Balanced Performance
56 FFO = 0x0B, // Fixed Frequency Override (requires freqPt)
57 MAX_PERF = 0x0C // Maximum Performance
58};
59
60// Only some of the SysPwrModes are currently supported and allowed to be set
61#define VALID_POWER_MODE_SETTING(mode) \
Chris Cain36f9cde2021-11-22 11:18:21 -060062 ((mode == SysPwrMode::STATIC) || (mode == SysPwrMode::POWER_SAVING) || \
Chris Cain78e86012021-03-04 16:15:31 -060063 (mode == SysPwrMode::DYNAMIC_PERF) || (mode == SysPwrMode::MAX_PERF))
Chris Cain36f9cde2021-11-22 11:18:21 -060064#define VALID_OEM_POWER_MODE_SETTING(mode) \
65 ((mode == SysPwrMode::SFP) || (mode == SysPwrMode::FFO) || \
66 (mode == SysPwrMode::MAX_FREQ))
Chris Cain78e86012021-03-04 16:15:31 -060067
68enum class RspStatus
69{
70 SUCCESS = 0x00,
71 CONDITIONAL_SUCCESS = 0x01,
72 INVALID_COMMAND = 0x11,
73 INVALID_COMMAND_LENGTH = 0x12,
74 INVALID_DATA_FIELD = 0x13,
75 CHECKSUM_FAILURE = 0x14,
76 INTERNAL_ERROR = 0x15,
77 PRESENT_STATE_PROHIBITS = 0x16,
78 COMMAND_IN_PROGRESS = 0xFF
79};
80
Chris Caina8857c52021-01-27 11:53:05 -060081enum class CmdStatus
82{
Chris Cainc567dc82022-04-01 15:09:17 -050083 SUCCESS = 0x00,
84 FAILURE = 0x02,
85 COMM_FAILURE = 0x03
Chris Caina8857c52021-01-27 11:53:05 -060086};
87
88/** @brief Trace block of data in hex with log<level:INFO>
89 *
90 * @param[in] data - vector containing data to trace
91 * @param[in] data_len - optional number of bytes to trace
92 * If 0, entire vector will be traced.
93 */
94void dump_hex(const std::vector<std::uint8_t>& data,
95 const unsigned int data_len = 0);
96
97/** @class OccCommand
98 * @brief Send commands and process respsonses from the OCC
99 */
100class OccCommand
101{
102 public:
103 OccCommand() = delete;
104 OccCommand(const OccCommand&) = delete;
105 OccCommand& operator=(const OccCommand&) = delete;
106 OccCommand(OccCommand&&) = default;
107 OccCommand& operator=(OccCommand&&) = default;
108
109 /** @brief Ctor to set up which OCC the command will go to
110 *
111 * @param[in] instance - OCC instance
Chris Caina8857c52021-01-27 11:53:05 -0600112 * @param[in] path - Path to attach at
113 */
George Liuf3b75142021-06-10 11:22:50 +0800114 OccCommand(uint8_t instance, const char* path);
Chris Caina8857c52021-01-27 11:53:05 -0600115
116 /** @brief Dtor to clean up and close device */
117 ~OccCommand()
118 {
119 closeDevice();
120 }
121
122 /** @brief Send the command to the OCC and collect the response.
123 * The checksum will be validated and removed from the response.
124 *
125 * @param[in] command - command to pass-through
126 * @param[out] response - response
127 * returns SUCCESS if response was received
128 */
129 CmdStatus send(const std::vector<std::uint8_t>& command,
130 std::vector<std::uint8_t>& response);
131
132 private:
133 /** @brief Instance number of the target OCC */
134 uint8_t occInstance;
135
136 /** @brief OCC path on the bus */
137 std::string path;
138
139 /** @brief OCC device path
140 * For now, here is the hard-coded mapping until
141 * the udev rule is in.
142 * occ0 --> /dev/occ1
143 * occ1 --> /dev/occ2
144 * ...
145 */
146 std::string devicePath;
147
148 /** @brief Indicates whether or not the OCC is currently active */
149 bool occActive = false;
150
151 /** brief file descriptor associated with occ device */
152 int fd = -1;
153
154 /** @brief Subscribe to OCC Status signal
155 *
156 * Once the OCC status gets to active, only then we will get /dev/occ2
157 * populated and hence need to wait on that before opening that
158 */
159 sdbusplus::bus::match_t activeStatusSignal;
160
161 /** Opens devicePath and populates file descriptor */
162 void openDevice();
163
164 /** Closed the fd associated with opened device */
165 void closeDevice();
166
167 /** @brief Callback function on OCC Status change signals
168 *
169 * @param[in] msg - Data associated with subscribed signal
170 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500171 void activeStatusEvent(sdbusplus::message_t& msg);
Chris Caina8857c52021-01-27 11:53:05 -0600172};
173
174} // namespace occ
175} // namespace open_power
Eddie Jamesb2515462022-11-08 17:01:04 -0600176
177template <>
178struct fmt::formatter<open_power::occ::SysPwrMode> : ostream_formatter
179{};