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 | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 50 | STATIC = 0x01, // Static Base Frequencey |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 51 | 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 Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 54 | MAX_FREQ = 0x09, // Maximum Frequency (per chip) |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 55 | DYNAMIC_PERF = 0x0A, // Dynamic / Balanced Performance |
| 56 | FFO = 0x0B, // Fixed Frequency Override (requires freqPt) |
| 57 | MAX_PERF = 0x0C // Maximum Performance |
| 58 | }; |
| 59 | |
Eddie James | b3c5704 | 2022-11-15 14:00:08 -0600 | [diff] [blame] | 60 | static inline auto format_as(SysPwrMode spm) |
| 61 | { |
| 62 | return fmt::underlying(spm); |
| 63 | } |
| 64 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 65 | // Only some of the SysPwrModes are currently supported and allowed to be set |
| 66 | #define VALID_POWER_MODE_SETTING(mode) \ |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 67 | ((mode == SysPwrMode::STATIC) || (mode == SysPwrMode::POWER_SAVING) || \ |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 68 | (mode == SysPwrMode::DYNAMIC_PERF) || (mode == SysPwrMode::MAX_PERF)) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 69 | #define VALID_OEM_POWER_MODE_SETTING(mode) \ |
| 70 | ((mode == SysPwrMode::SFP) || (mode == SysPwrMode::FFO) || \ |
| 71 | (mode == SysPwrMode::MAX_FREQ)) |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 72 | |
| 73 | enum 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 Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 86 | enum class CmdStatus |
| 87 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 88 | SUCCESS = 0x00, |
| 89 | FAILURE = 0x02, |
| 90 | COMM_FAILURE = 0x03 |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 91 | }; |
| 92 | |
Eddie James | b3c5704 | 2022-11-15 14:00:08 -0600 | [diff] [blame] | 93 | static inline auto format_as(CmdStatus cs) |
| 94 | { |
| 95 | return fmt::underlying(cs); |
| 96 | } |
| 97 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 98 | /** @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 | */ |
| 104 | void 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 | */ |
| 110 | class 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 Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 122 | * @param[in] path - Path to attach at |
| 123 | */ |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 124 | OccCommand(uint8_t instance, const char* path); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 125 | |
| 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 Williams | af40808 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 181 | void activeStatusEvent(sdbusplus::message_t& msg); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } // namespace occ |
| 185 | } // namespace open_power |