Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <asm/byteorder.h> |
| 9 | |
| 10 | #include <OcpMctpVdm.hpp> |
| 11 | |
| 12 | #include <cstddef> |
| 13 | #include <cstdint> |
| 14 | |
| 15 | namespace gpu |
| 16 | { |
| 17 | |
| 18 | /** @brief NVIDIA PCI vendor ID */ |
| 19 | constexpr uint16_t nvidiaPciVendorId = 0x10de; |
| 20 | |
| 21 | /** @brief GPU message types |
| 22 | * |
| 23 | * Enumeration of different message types used in GPU protocol. |
| 24 | * These types categorize different classes of messages for device management |
| 25 | * and monitoring. |
| 26 | */ |
| 27 | enum class MessageType : uint8_t |
| 28 | { |
| 29 | DEVICE_CAPABILITY_DISCOVERY = 0, |
| 30 | PLATFORM_ENVIRONMENTAL = 3 |
| 31 | }; |
| 32 | |
| 33 | /** @brief Type0 Device Capability Discovery Commands |
| 34 | */ |
| 35 | enum class DeviceCapabilityDiscoveryCommands : uint8_t |
| 36 | { |
| 37 | QUERY_DEVICE_IDENTIFICATION = 0x09, |
| 38 | }; |
| 39 | |
| 40 | /** @brief Type3 platform environmental commands |
| 41 | */ |
| 42 | enum class PlatformEnvironmentalCommands : uint8_t |
| 43 | { |
| 44 | GET_TEMPERATURE_READING = 0x00, |
| 45 | }; |
| 46 | |
| 47 | /** @brief device identification types |
| 48 | * |
| 49 | * Enumeration of different device types that can be identified in the system. |
| 50 | * This is used to distinguish between various components during device |
| 51 | * discovery. |
| 52 | */ |
| 53 | enum class DeviceIdentification : uint8_t |
| 54 | { |
| 55 | DEVICE_GPU = 0 |
| 56 | }; |
| 57 | |
| 58 | /** @struct QueryDeviceIdentificationRequest |
| 59 | * |
| 60 | * Structure representing query device identification request |
| 61 | */ |
| 62 | struct QueryDeviceIdentificationRequest |
| 63 | { |
| 64 | ocp::accelerator_management::CommonRequest hdr; |
| 65 | } __attribute__((packed)); |
| 66 | |
| 67 | /** @struct QueryDeviceIdentificationResponse |
| 68 | * |
| 69 | * Structure representing query device identification response. |
| 70 | */ |
| 71 | struct QueryDeviceIdentificationResponse |
| 72 | { |
| 73 | ocp::accelerator_management::CommonResponse hdr; |
| 74 | uint8_t device_identification; |
| 75 | uint8_t instance_id; |
| 76 | } __attribute__((packed)); |
| 77 | |
| 78 | /** @struct GetNumericSensorReadingRequest |
| 79 | * |
| 80 | * Structure representing request to get reading of certain numeric |
| 81 | * sensors. |
| 82 | */ |
| 83 | struct GetNumericSensorReadingRequest |
| 84 | { |
| 85 | ocp::accelerator_management::CommonRequest hdr; |
| 86 | uint8_t sensor_id; |
| 87 | } __attribute__((packed)); |
| 88 | |
| 89 | /** @struct GetTemperatureReadingRequest |
| 90 | * |
| 91 | * Structure representing get temperature reading request. |
| 92 | */ |
| 93 | using GetTemperatureReadingRequest = GetNumericSensorReadingRequest; |
| 94 | |
| 95 | /** @struct GetTemperatureReadingResponse |
| 96 | * |
| 97 | * Structure representing get temperature reading response. |
| 98 | */ |
| 99 | struct GetTemperatureReadingResponse |
| 100 | { |
| 101 | ocp::accelerator_management::CommonResponse hdr; |
| 102 | int32_t reading; |
| 103 | } __attribute__((packed)); |
| 104 | |
| 105 | /** |
| 106 | * @brief Populate the GPU message with the GPU header. |
| 107 | * The caller of this API allocates buffer for the GPU header |
| 108 | * when forming the GPU message. |
| 109 | * The buffer is passed to this API to pack the GPU header. |
| 110 | * |
| 111 | * @param[in] hdr - Reference to the OCP MCTP VDM header information |
| 112 | * @param[out] msg - Reference to GPU message header |
| 113 | * |
| 114 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 115 | * otherwise appropriate error code. |
| 116 | * @note Caller is responsible for alloc and dealloc of msg |
| 117 | * and hdr params |
| 118 | */ |
| 119 | ocp::accelerator_management::CompletionCode packHeader( |
| 120 | const ocp::accelerator_management::BindingPciVidInfo& hdr, |
| 121 | ocp::accelerator_management::BindingPciVid& msg); |
| 122 | |
| 123 | /** @brief Encode reason code |
| 124 | * |
| 125 | * @param[in] cc - Completion Code |
| 126 | * @param[in] reason_code - reason code |
| 127 | * @param[in] command_code - command code |
| 128 | * @param[out] msg - Reference to message |
| 129 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 130 | * otherwise appropriate error code. |
| 131 | */ |
| 132 | ocp::accelerator_management::CompletionCode encodeReasonCode( |
| 133 | uint8_t cc, uint16_t reasonCode, uint8_t commandCode, |
| 134 | ocp::accelerator_management::Message& msg); |
| 135 | |
| 136 | /** @brief Decode to get reason code |
| 137 | * |
| 138 | * @param[in] msg - response message |
| 139 | * @param[in] msg_len - Length of response message |
| 140 | * @param[out] cc - reference to completion code |
| 141 | * @param[out] reason_code - reference to reason_code |
| 142 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 143 | * otherwise appropriate error code. |
| 144 | */ |
| 145 | ocp::accelerator_management::CompletionCode decodeReasonCodeAndCC( |
| 146 | const ocp::accelerator_management::Message& msg, size_t msgLen, uint8_t& cc, |
| 147 | uint16_t& reasonCode); |
| 148 | |
| 149 | /** @brief Create a Query device identification request message |
| 150 | * |
| 151 | * @param[in] instance_id - instance ID |
| 152 | * @param[out] msg - Reference to message that will be written to |
| 153 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 154 | * otherwise appropriate error code. |
| 155 | */ |
| 156 | ocp::accelerator_management::CompletionCode |
| 157 | encodeQueryDeviceIdentificationRequest( |
| 158 | uint8_t instanceId, ocp::accelerator_management::Message& msg); |
| 159 | |
| 160 | /** @brief Encode a Query device identification response message |
| 161 | * |
| 162 | * @param[in] instance_id - instance ID |
| 163 | * @param[in] cc - completion code |
| 164 | * @param[in] reason_code - reason code |
| 165 | * @param[in] device_identification - device identification |
| 166 | * @param[in] device_instance - device instance id |
| 167 | * @param[out] msg - Reference to message that will be written to |
| 168 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 169 | * otherwise appropriate error code. |
| 170 | */ |
| 171 | ocp::accelerator_management::CompletionCode |
| 172 | encodeQueryDeviceIdentificationResponse( |
| 173 | uint8_t instanceId, uint8_t cc, uint16_t reasonCode, |
| 174 | uint8_t deviceIdentification, uint8_t deviceInstance, |
| 175 | ocp::accelerator_management::Message& msg); |
| 176 | |
| 177 | /** @brief Decode a Query device identification response message |
| 178 | * |
| 179 | * @param[in] msg - response message |
| 180 | * @param[in] msg_len - Length of response message |
| 181 | * @param[out] cc - reference to completion code |
| 182 | * @param[out] reason_code - reference to reason code |
| 183 | * @param[out] device_identification - reference to device_identification |
| 184 | * @param[out] device_instance - reference to instance id |
| 185 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 186 | * otherwise appropriate error code. |
| 187 | */ |
| 188 | ocp::accelerator_management::CompletionCode |
| 189 | decodeQueryDeviceIdentificationResponse( |
| 190 | const ocp::accelerator_management::Message& msg, size_t msgLen, |
| 191 | uint8_t& cc, uint16_t& reasonCode, uint8_t& deviceIdentification, |
| 192 | uint8_t& deviceInstance); |
| 193 | |
| 194 | /** @brief Encode a Get temperature readings request message |
| 195 | * |
| 196 | * @param[in] instance_id - instance ID |
| 197 | * @param[in] sensor_id - sensor id |
| 198 | * @param[out] msg - Reference to message that will be written to |
| 199 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 200 | * otherwise appropriate error code. |
| 201 | */ |
| 202 | ocp::accelerator_management::CompletionCode encodeGetTemperatureReadingRequest( |
| 203 | uint8_t instanceId, uint8_t sensorId, |
| 204 | ocp::accelerator_management::Message& msg); |
| 205 | |
| 206 | /** @brief Decode a Get temperature readings request message |
| 207 | * |
| 208 | * @param[in] msg - request message |
| 209 | * @param[in] msg_len - Length of request message |
| 210 | * @param[out] sensor_id - reference to sensor id |
| 211 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 212 | * otherwise appropriate error code. |
| 213 | */ |
| 214 | ocp::accelerator_management::CompletionCode decodeGetTemperatureReadingRequest( |
| 215 | const ocp::accelerator_management::Message& msg, size_t msgLen, |
| 216 | uint8_t& sensorId); |
| 217 | |
| 218 | /** @brief Encode a Get temperature readings response message |
| 219 | * |
| 220 | * @param[in] instance_id - instance ID |
| 221 | * @param[in] cc - pointer to response message completion code |
| 222 | * @param[in] reason_code - reason code |
| 223 | * @param[in] temperature_reading - temperature reading |
| 224 | * @param[out] msg - Reference to message that will be written to |
| 225 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 226 | * otherwise appropriate error code. |
| 227 | */ |
| 228 | ocp::accelerator_management::CompletionCode encodeGetTemperatureReadingResponse( |
| 229 | uint8_t instanceId, uint8_t cc, uint16_t reasonCode, |
| 230 | double temperatureReading, ocp::accelerator_management::Message& msg); |
| 231 | |
| 232 | /** @brief Decode a Get temperature readings response message |
| 233 | * |
| 234 | * @param[in] msg - response message |
| 235 | * @param[in] msg_len - Length of response message |
| 236 | * @param[out] cc - reference to response message completion code |
| 237 | * @param[out] reason_code - reference to reason code |
| 238 | * @param[out] temperature_reading - reference to temperature_reading |
| 239 | * @return ocp::accelerator_management::CompletionCode::SUCCESS on success, |
| 240 | * otherwise appropriate error code. |
| 241 | */ |
| 242 | ocp::accelerator_management::CompletionCode decodeGetTemperatureReadingResponse( |
| 243 | const ocp::accelerator_management::Message& msg, size_t msgLen, uint8_t& cc, |
| 244 | uint16_t& reasonCode, double& temperatureReading); |
| 245 | |
| 246 | } // namespace gpu |