Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "occ_errors.hpp" |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 4 | #include "utils.hpp" |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 5 | |
Eddie James | b3c5704 | 2022-11-15 14:00:08 -0600 | [diff] [blame] | 6 | #include <fmt/format.h> |
Eddie James | b251546 | 2022-11-08 17:01:04 -0600 | [diff] [blame] | 7 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 8 | #include <org/open_power/OCC/PassThrough/server.hpp> |
| 9 | #include <sdbusplus/bus.hpp> |
| 10 | #include <sdbusplus/server/object.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 11 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | namespace open_power |
| 15 | { |
| 16 | namespace occ |
| 17 | { |
| 18 | |
| 19 | // For waiting on signals |
| 20 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 21 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 22 | enum 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 Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 30 | SEND_AMBIENT = 0x30, |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 31 | DEBUG_PASS_THROUGH = 0x40, |
| 32 | AME_PASS_THROUGH = 0x41, |
| 33 | GET_FIELD_DEBUG_DATA = 0x42, |
| 34 | MFG_TEST = 0x53 |
| 35 | }; |
| 36 | |
| 37 | enum class OccState |
| 38 | { |
| 39 | NO_CHANGE = 0x00, |
| 40 | STANDBY = 0x01, |
| 41 | OBSERVATION = 0x02, |
| 42 | ACTIVE = 0x03, |
| 43 | SAFE = 0x04, |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 44 | CHARACTERIZATION = 0x05 |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | enum class SysPwrMode |
| 48 | { |
| 49 | NO_CHANGE = 0, |
Chris Cain | 2ff2886 | 2024-01-31 10:21:13 -0600 | [diff] [blame^] | 50 | STATIC = 0x01, // Static Base Frequencey |
| 51 | NON_DETERMINISTIC = 0x02, // Non-Deterministic |
| 52 | SFP = 0x03, // Static Frequency Point (requires freqPt) |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 53 | SAFE = 0x04, // reported when system is in SAFE mode (not settable) |
| 54 | POWER_SAVING = 0x05, // Static Power Saving |
Chris Cain | 2ff2886 | 2024-01-31 10:21:13 -0600 | [diff] [blame^] | 55 | EFF_FAVOR_POWER = 0x06, // Efficiency - Favor Power |
| 56 | EFF_FAVOR_PERF = 0x07, // Efficiency - Favor Performance |
| 57 | MAX_FREQ = 0x09, // Maximum Frequency (per chip) |
| 58 | BALANCED_PERF = 0x0A, // Balanced Performance |
| 59 | FFO = 0x0B, // Fixed Frequency Override (requires freqPt) |
| 60 | MAX_PERF = 0x0C // Maximum Performance |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
Eddie James | b3c5704 | 2022-11-15 14:00:08 -0600 | [diff] [blame] | 63 | static inline auto format_as(SysPwrMode spm) |
| 64 | { |
| 65 | return fmt::underlying(spm); |
| 66 | } |
| 67 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 68 | // Only some of the SysPwrModes are currently supported and allowed to be set |
| 69 | #define VALID_POWER_MODE_SETTING(mode) \ |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 70 | ((mode == SysPwrMode::STATIC) || (mode == SysPwrMode::POWER_SAVING) || \ |
Chris Cain | 2ff2886 | 2024-01-31 10:21:13 -0600 | [diff] [blame^] | 71 | (mode == SysPwrMode::BALANCED_PERF) || (mode == SysPwrMode::MAX_PERF) || \ |
| 72 | (mode == SysPwrMode::EFF_FAVOR_POWER) || \ |
| 73 | (mode == SysPwrMode::EFF_FAVOR_PERF)) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 74 | #define VALID_OEM_POWER_MODE_SETTING(mode) \ |
| 75 | ((mode == SysPwrMode::SFP) || (mode == SysPwrMode::FFO) || \ |
Chris Cain | 2ff2886 | 2024-01-31 10:21:13 -0600 | [diff] [blame^] | 76 | (mode == SysPwrMode::MAX_FREQ) || \ |
| 77 | (mode == SysPwrMode::NON_DETERMINISTIC)) |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 78 | |
| 79 | enum class RspStatus |
| 80 | { |
| 81 | SUCCESS = 0x00, |
| 82 | CONDITIONAL_SUCCESS = 0x01, |
| 83 | INVALID_COMMAND = 0x11, |
| 84 | INVALID_COMMAND_LENGTH = 0x12, |
| 85 | INVALID_DATA_FIELD = 0x13, |
| 86 | CHECKSUM_FAILURE = 0x14, |
| 87 | INTERNAL_ERROR = 0x15, |
| 88 | PRESENT_STATE_PROHIBITS = 0x16, |
| 89 | COMMAND_IN_PROGRESS = 0xFF |
| 90 | }; |
| 91 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 92 | enum class CmdStatus |
| 93 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 94 | SUCCESS = 0x00, |
| 95 | FAILURE = 0x02, |
| 96 | COMM_FAILURE = 0x03 |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 97 | }; |
| 98 | |
Eddie James | b3c5704 | 2022-11-15 14:00:08 -0600 | [diff] [blame] | 99 | static inline auto format_as(CmdStatus cs) |
| 100 | { |
| 101 | return fmt::underlying(cs); |
| 102 | } |
| 103 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 104 | /** @brief Trace block of data in hex with log<level:INFO> |
| 105 | * |
| 106 | * @param[in] data - vector containing data to trace |
| 107 | * @param[in] data_len - optional number of bytes to trace |
| 108 | * If 0, entire vector will be traced. |
| 109 | */ |
| 110 | void dump_hex(const std::vector<std::uint8_t>& data, |
| 111 | const unsigned int data_len = 0); |
| 112 | |
| 113 | /** @class OccCommand |
| 114 | * @brief Send commands and process respsonses from the OCC |
| 115 | */ |
| 116 | class OccCommand |
| 117 | { |
| 118 | public: |
| 119 | OccCommand() = delete; |
| 120 | OccCommand(const OccCommand&) = delete; |
| 121 | OccCommand& operator=(const OccCommand&) = delete; |
| 122 | OccCommand(OccCommand&&) = default; |
| 123 | OccCommand& operator=(OccCommand&&) = default; |
| 124 | |
| 125 | /** @brief Ctor to set up which OCC the command will go to |
| 126 | * |
| 127 | * @param[in] instance - OCC instance |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 128 | * @param[in] path - Path to attach at |
| 129 | */ |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 130 | OccCommand(uint8_t instance, const char* path); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 131 | |
| 132 | /** @brief Dtor to clean up and close device */ |
| 133 | ~OccCommand() |
| 134 | { |
| 135 | closeDevice(); |
| 136 | } |
| 137 | |
| 138 | /** @brief Send the command to the OCC and collect the response. |
| 139 | * The checksum will be validated and removed from the response. |
| 140 | * |
| 141 | * @param[in] command - command to pass-through |
| 142 | * @param[out] response - response |
| 143 | * returns SUCCESS if response was received |
| 144 | */ |
| 145 | CmdStatus send(const std::vector<std::uint8_t>& command, |
| 146 | std::vector<std::uint8_t>& response); |
| 147 | |
| 148 | private: |
| 149 | /** @brief Instance number of the target OCC */ |
| 150 | uint8_t occInstance; |
| 151 | |
| 152 | /** @brief OCC path on the bus */ |
| 153 | std::string path; |
| 154 | |
| 155 | /** @brief OCC device path |
| 156 | * For now, here is the hard-coded mapping until |
| 157 | * the udev rule is in. |
| 158 | * occ0 --> /dev/occ1 |
| 159 | * occ1 --> /dev/occ2 |
| 160 | * ... |
| 161 | */ |
| 162 | std::string devicePath; |
| 163 | |
| 164 | /** @brief Indicates whether or not the OCC is currently active */ |
| 165 | bool occActive = false; |
| 166 | |
| 167 | /** brief file descriptor associated with occ device */ |
| 168 | int fd = -1; |
| 169 | |
| 170 | /** @brief Subscribe to OCC Status signal |
| 171 | * |
| 172 | * Once the OCC status gets to active, only then we will get /dev/occ2 |
| 173 | * populated and hence need to wait on that before opening that |
| 174 | */ |
| 175 | sdbusplus::bus::match_t activeStatusSignal; |
| 176 | |
| 177 | /** Opens devicePath and populates file descriptor */ |
| 178 | void openDevice(); |
| 179 | |
| 180 | /** Closed the fd associated with opened device */ |
| 181 | void closeDevice(); |
| 182 | |
| 183 | /** @brief Callback function on OCC Status change signals |
| 184 | * |
| 185 | * @param[in] msg - Data associated with subscribed signal |
| 186 | */ |
Patrick Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 187 | void activeStatusEvent(sdbusplus::message_t& msg); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace occ |
| 191 | } // namespace open_power |