blob: b3983ff9742a52176a8a63f32e943f2a98fa517e [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 Jamesb3c57042022-11-15 14:00:08 -06006#include <fmt/format.h>
Eddie Jamesb2515462022-11-08 17:01:04 -06007
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
Eddie Jamesb3c57042022-11-15 14:00:08 -060060static inline auto format_as(SysPwrMode spm)
61{
62 return fmt::underlying(spm);
63}
64
Chris Cain78e86012021-03-04 16:15:31 -060065// Only some of the SysPwrModes are currently supported and allowed to be set
66#define VALID_POWER_MODE_SETTING(mode) \
Chris Cain36f9cde2021-11-22 11:18:21 -060067 ((mode == SysPwrMode::STATIC) || (mode == SysPwrMode::POWER_SAVING) || \
Chris Cain78e86012021-03-04 16:15:31 -060068 (mode == SysPwrMode::DYNAMIC_PERF) || (mode == SysPwrMode::MAX_PERF))
Chris Cain36f9cde2021-11-22 11:18:21 -060069#define VALID_OEM_POWER_MODE_SETTING(mode) \
70 ((mode == SysPwrMode::SFP) || (mode == SysPwrMode::FFO) || \
71 (mode == SysPwrMode::MAX_FREQ))
Chris Cain78e86012021-03-04 16:15:31 -060072
73enum class RspStatus
74{
75 SUCCESS = 0x00,
76 CONDITIONAL_SUCCESS = 0x01,
77 INVALID_COMMAND = 0x11,
78 INVALID_COMMAND_LENGTH = 0x12,
79 INVALID_DATA_FIELD = 0x13,
80 CHECKSUM_FAILURE = 0x14,
81 INTERNAL_ERROR = 0x15,
82 PRESENT_STATE_PROHIBITS = 0x16,
83 COMMAND_IN_PROGRESS = 0xFF
84};
85
Chris Caina8857c52021-01-27 11:53:05 -060086enum class CmdStatus
87{
Chris Cainc567dc82022-04-01 15:09:17 -050088 SUCCESS = 0x00,
89 FAILURE = 0x02,
90 COMM_FAILURE = 0x03
Chris Caina8857c52021-01-27 11:53:05 -060091};
92
Eddie Jamesb3c57042022-11-15 14:00:08 -060093static inline auto format_as(CmdStatus cs)
94{
95 return fmt::underlying(cs);
96}
97
Chris Caina8857c52021-01-27 11:53:05 -060098/** @brief Trace block of data in hex with log<level:INFO>
99 *
100 * @param[in] data - vector containing data to trace
101 * @param[in] data_len - optional number of bytes to trace
102 * If 0, entire vector will be traced.
103 */
104void dump_hex(const std::vector<std::uint8_t>& data,
105 const unsigned int data_len = 0);
106
107/** @class OccCommand
108 * @brief Send commands and process respsonses from the OCC
109 */
110class OccCommand
111{
112 public:
113 OccCommand() = delete;
114 OccCommand(const OccCommand&) = delete;
115 OccCommand& operator=(const OccCommand&) = delete;
116 OccCommand(OccCommand&&) = default;
117 OccCommand& operator=(OccCommand&&) = default;
118
119 /** @brief Ctor to set up which OCC the command will go to
120 *
121 * @param[in] instance - OCC instance
Chris Caina8857c52021-01-27 11:53:05 -0600122 * @param[in] path - Path to attach at
123 */
George Liuf3b75142021-06-10 11:22:50 +0800124 OccCommand(uint8_t instance, const char* path);
Chris Caina8857c52021-01-27 11:53:05 -0600125
126 /** @brief Dtor to clean up and close device */
127 ~OccCommand()
128 {
129 closeDevice();
130 }
131
132 /** @brief Send the command to the OCC and collect the response.
133 * The checksum will be validated and removed from the response.
134 *
135 * @param[in] command - command to pass-through
136 * @param[out] response - response
137 * returns SUCCESS if response was received
138 */
139 CmdStatus send(const std::vector<std::uint8_t>& command,
140 std::vector<std::uint8_t>& response);
141
142 private:
143 /** @brief Instance number of the target OCC */
144 uint8_t occInstance;
145
146 /** @brief OCC path on the bus */
147 std::string path;
148
149 /** @brief OCC device path
150 * For now, here is the hard-coded mapping until
151 * the udev rule is in.
152 * occ0 --> /dev/occ1
153 * occ1 --> /dev/occ2
154 * ...
155 */
156 std::string devicePath;
157
158 /** @brief Indicates whether or not the OCC is currently active */
159 bool occActive = false;
160
161 /** brief file descriptor associated with occ device */
162 int fd = -1;
163
164 /** @brief Subscribe to OCC Status signal
165 *
166 * Once the OCC status gets to active, only then we will get /dev/occ2
167 * populated and hence need to wait on that before opening that
168 */
169 sdbusplus::bus::match_t activeStatusSignal;
170
171 /** Opens devicePath and populates file descriptor */
172 void openDevice();
173
174 /** Closed the fd associated with opened device */
175 void closeDevice();
176
177 /** @brief Callback function on OCC Status change signals
178 *
179 * @param[in] msg - Data associated with subscribed signal
180 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500181 void activeStatusEvent(sdbusplus::message_t& msg);
Chris Caina8857c52021-01-27 11:53:05 -0600182};
183
184} // namespace occ
185} // namespace open_power