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 | #include "GpuMctpVdm.hpp" |
| 7 | |
| 8 | #include "OcpMctpVdm.hpp" |
| 9 | |
| 10 | #include <endian.h> |
| 11 | |
| 12 | #include <cstdint> |
| 13 | #include <cstring> |
| 14 | |
| 15 | namespace gpu |
| 16 | { |
| 17 | |
| 18 | ocp::accelerator_management::CompletionCode packHeader( |
| 19 | const ocp::accelerator_management::BindingPciVidInfo& hdr, |
| 20 | ocp::accelerator_management::BindingPciVid& msg) |
| 21 | { |
| 22 | return ocp::accelerator_management::packHeader(nvidiaPciVendorId, hdr, msg); |
| 23 | } |
| 24 | |
| 25 | ocp::accelerator_management::CompletionCode encodeReasonCode( |
| 26 | uint8_t cc, uint16_t reasonCode, uint8_t commandCode, |
| 27 | ocp::accelerator_management::Message& msg) |
| 28 | { |
| 29 | return ocp::accelerator_management::encodeReasonCode(cc, reasonCode, |
| 30 | commandCode, msg); |
| 31 | } |
| 32 | |
| 33 | ocp::accelerator_management::CompletionCode decodeReasonCodeAndCC( |
| 34 | const ocp::accelerator_management::Message& msg, size_t msgLen, uint8_t& cc, |
| 35 | uint16_t& reasonCode) |
| 36 | { |
| 37 | if (be16toh(msg.hdr.pci_vendor_id) != nvidiaPciVendorId) |
| 38 | { |
| 39 | return ocp::accelerator_management::CompletionCode::ERR_INVALID_DATA; |
| 40 | } |
| 41 | |
| 42 | return ocp::accelerator_management::decodeReasonCodeAndCC( |
| 43 | msg, msgLen, cc, reasonCode); |
| 44 | } |
| 45 | |
| 46 | ocp::accelerator_management::CompletionCode |
| 47 | encodeQueryDeviceIdentificationRequest( |
| 48 | uint8_t instanceId, ocp::accelerator_management::Message& msg) |
| 49 | { |
| 50 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 51 | header.ocp_accelerator_management_msg_type = |
| 52 | static_cast<uint8_t>(ocp::accelerator_management::MessageType::REQUEST); |
| 53 | header.instance_id = instanceId & |
| 54 | ocp::accelerator_management::instanceIdMask; |
| 55 | header.msg_type = |
| 56 | static_cast<uint8_t>(MessageType::DEVICE_CAPABILITY_DISCOVERY); |
| 57 | |
| 58 | auto rc = packHeader(header, msg.hdr); |
| 59 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 60 | { |
| 61 | return rc; |
| 62 | } |
| 63 | |
| 64 | QueryDeviceIdentificationRequest request{}; |
| 65 | request.hdr.command = static_cast<uint8_t>( |
| 66 | DeviceCapabilityDiscoveryCommands::QUERY_DEVICE_IDENTIFICATION); |
| 67 | request.hdr.data_size = 0; |
| 68 | |
| 69 | std::memcpy(&msg.data, &request, sizeof(request)); |
| 70 | |
| 71 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 72 | } |
| 73 | |
| 74 | ocp::accelerator_management::CompletionCode |
| 75 | encodeQueryDeviceIdentificationResponse( |
| 76 | uint8_t instanceId, uint8_t cc, uint16_t reasonCode, |
| 77 | uint8_t deviceIdentification, uint8_t deviceInstance, |
| 78 | ocp::accelerator_management::Message& msg) |
| 79 | { |
| 80 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 81 | header.ocp_accelerator_management_msg_type = static_cast<uint8_t>( |
| 82 | ocp::accelerator_management::MessageType::RESPONSE); |
| 83 | header.instance_id = instanceId & |
| 84 | ocp::accelerator_management::instanceIdMask; |
| 85 | header.msg_type = |
| 86 | static_cast<uint8_t>(MessageType::DEVICE_CAPABILITY_DISCOVERY); |
| 87 | |
| 88 | auto rc = packHeader(header, msg.hdr); |
| 89 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 90 | { |
| 91 | return rc; |
| 92 | } |
| 93 | |
| 94 | if (cc != static_cast<uint8_t>( |
| 95 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 96 | { |
| 97 | return gpu::encodeReasonCode( |
| 98 | cc, reasonCode, |
| 99 | static_cast<uint8_t>( |
| 100 | DeviceCapabilityDiscoveryCommands::QUERY_DEVICE_IDENTIFICATION), |
| 101 | msg); |
| 102 | } |
| 103 | |
| 104 | QueryDeviceIdentificationResponse response{}; |
| 105 | response.hdr.command = static_cast<uint8_t>( |
| 106 | DeviceCapabilityDiscoveryCommands::QUERY_DEVICE_IDENTIFICATION); |
| 107 | response.hdr.completion_code = cc; |
| 108 | response.hdr.data_size = htole16(2); |
| 109 | response.device_identification = deviceIdentification; |
| 110 | response.instance_id = deviceInstance; |
| 111 | |
| 112 | std::memcpy(&msg.data, &response, sizeof(response)); |
| 113 | |
| 114 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 115 | } |
| 116 | |
| 117 | ocp::accelerator_management::CompletionCode |
| 118 | decodeQueryDeviceIdentificationResponse( |
| 119 | const ocp::accelerator_management::Message& msg, size_t msgLen, |
| 120 | uint8_t& cc, uint16_t& reasonCode, uint8_t& deviceIdentification, |
| 121 | uint8_t& deviceInstance) |
| 122 | { |
| 123 | auto rc = gpu::decodeReasonCodeAndCC(msg, msgLen, cc, reasonCode); |
| 124 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS || |
| 125 | cc != static_cast<uint8_t>( |
| 126 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 127 | { |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | if (msgLen < sizeof(ocp::accelerator_management::BindingPciVid) + |
| 132 | sizeof(QueryDeviceIdentificationResponse)) |
| 133 | { |
| 134 | return ocp::accelerator_management::CompletionCode:: |
| 135 | ERR_INVALID_DATA_LENGTH; |
| 136 | } |
| 137 | |
| 138 | QueryDeviceIdentificationResponse response{}; |
| 139 | std::memcpy(&response, &msg.data, sizeof(response)); |
| 140 | |
| 141 | deviceIdentification = response.device_identification; |
| 142 | deviceInstance = response.instance_id; |
| 143 | |
| 144 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 145 | } |
| 146 | |
| 147 | ocp::accelerator_management::CompletionCode encodeGetTemperatureReadingRequest( |
| 148 | uint8_t instanceId, uint8_t sensorId, |
| 149 | ocp::accelerator_management::Message& msg) |
| 150 | { |
| 151 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 152 | header.ocp_accelerator_management_msg_type = |
| 153 | static_cast<uint8_t>(ocp::accelerator_management::MessageType::REQUEST); |
| 154 | header.instance_id = instanceId & |
| 155 | ocp::accelerator_management::instanceIdMask; |
| 156 | header.msg_type = static_cast<uint8_t>(MessageType::PLATFORM_ENVIRONMENTAL); |
| 157 | |
| 158 | auto rc = packHeader(header, msg.hdr); |
| 159 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 160 | { |
| 161 | return rc; |
| 162 | } |
| 163 | |
| 164 | GetTemperatureReadingRequest request{}; |
| 165 | request.hdr.command = static_cast<uint8_t>( |
| 166 | PlatformEnvironmentalCommands::GET_TEMPERATURE_READING); |
| 167 | request.hdr.data_size = sizeof(sensorId); |
| 168 | request.sensor_id = sensorId; |
| 169 | |
| 170 | std::memcpy(&msg.data, &request, sizeof(request)); |
| 171 | |
| 172 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 173 | } |
| 174 | |
| 175 | ocp::accelerator_management::CompletionCode decodeGetTemperatureReadingRequest( |
| 176 | const ocp::accelerator_management::Message& msg, size_t msgLen, |
| 177 | uint8_t& sensorId) |
| 178 | { |
| 179 | if (msgLen < sizeof(ocp::accelerator_management::BindingPciVid) + |
| 180 | sizeof(GetTemperatureReadingRequest)) |
| 181 | { |
| 182 | return ocp::accelerator_management::CompletionCode:: |
| 183 | ERR_INVALID_DATA_LENGTH; |
| 184 | } |
| 185 | |
| 186 | GetTemperatureReadingRequest request{}; |
| 187 | std::memcpy(&request, &msg.data, sizeof(request)); |
| 188 | |
| 189 | if (request.hdr.data_size < sizeof(request.sensor_id)) |
| 190 | { |
| 191 | return ocp::accelerator_management::CompletionCode::ERR_INVALID_DATA; |
| 192 | } |
| 193 | |
| 194 | sensorId = request.sensor_id; |
| 195 | |
| 196 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 197 | } |
| 198 | |
| 199 | ocp::accelerator_management::CompletionCode encodeGetTemperatureReadingResponse( |
| 200 | uint8_t instanceId, uint8_t cc, uint16_t reasonCode, |
| 201 | double temperatureReading, ocp::accelerator_management::Message& msg) |
| 202 | { |
| 203 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 204 | header.ocp_accelerator_management_msg_type = static_cast<uint8_t>( |
| 205 | ocp::accelerator_management::MessageType::RESPONSE); |
| 206 | header.instance_id = instanceId & |
| 207 | ocp::accelerator_management::instanceIdMask; |
| 208 | header.msg_type = static_cast<uint8_t>(MessageType::PLATFORM_ENVIRONMENTAL); |
| 209 | |
| 210 | auto rc = packHeader(header, msg.hdr); |
| 211 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 212 | { |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | if (cc != static_cast<uint8_t>( |
| 217 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 218 | { |
| 219 | return gpu::encodeReasonCode( |
| 220 | cc, reasonCode, |
| 221 | static_cast<uint8_t>( |
| 222 | PlatformEnvironmentalCommands::GET_TEMPERATURE_READING), |
| 223 | msg); |
| 224 | } |
| 225 | |
| 226 | GetTemperatureReadingResponse response{}; |
| 227 | response.hdr.command = static_cast<uint8_t>( |
| 228 | PlatformEnvironmentalCommands::GET_TEMPERATURE_READING); |
| 229 | response.hdr.completion_code = cc; |
| 230 | response.hdr.data_size = htole16(sizeof(uint32_t)); |
| 231 | |
| 232 | int32_t reading = static_cast<int32_t>(temperatureReading * (1 << 8)); |
| 233 | response.reading = htole32(reading); |
| 234 | |
| 235 | std::memcpy(&msg.data, &response, sizeof(response)); |
| 236 | |
| 237 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 238 | } |
| 239 | |
| 240 | ocp::accelerator_management::CompletionCode decodeGetTemperatureReadingResponse( |
| 241 | const ocp::accelerator_management::Message& msg, size_t msgLen, uint8_t& cc, |
| 242 | uint16_t& reasonCode, double& temperatureReading) |
| 243 | { |
| 244 | auto rc = gpu::decodeReasonCodeAndCC(msg, msgLen, cc, reasonCode); |
| 245 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS || |
| 246 | cc != static_cast<uint8_t>( |
| 247 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 248 | { |
| 249 | return rc; |
| 250 | } |
| 251 | |
| 252 | if (msgLen < sizeof(ocp::accelerator_management::BindingPciVid) + |
| 253 | sizeof(GetTemperatureReadingResponse)) |
| 254 | { |
| 255 | return ocp::accelerator_management::CompletionCode:: |
| 256 | ERR_INVALID_DATA_LENGTH; |
| 257 | } |
| 258 | |
| 259 | GetTemperatureReadingResponse response{}; |
| 260 | std::memcpy(&response, &msg.data, sizeof(response)); |
| 261 | |
| 262 | uint16_t dataSize = le16toh(response.hdr.data_size); |
| 263 | if (dataSize != sizeof(int32_t)) |
| 264 | { |
| 265 | return ocp::accelerator_management::CompletionCode::ERR_INVALID_DATA; |
| 266 | } |
| 267 | |
| 268 | int32_t reading = le32toh(response.reading); |
| 269 | temperatureReading = reading / static_cast<double>(1 << 8); |
| 270 | |
| 271 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 272 | } |
Harshit Aghera | 09f6f2c | 2025-05-07 16:20:16 +0530 | [diff] [blame^] | 273 | |
| 274 | ocp::accelerator_management::CompletionCode encodeReadThermalParametersRequest( |
| 275 | uint8_t instanceId, uint8_t sensorId, |
| 276 | ocp::accelerator_management::Message& msg) |
| 277 | { |
| 278 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 279 | header.ocp_accelerator_management_msg_type = |
| 280 | static_cast<uint8_t>(ocp::accelerator_management::MessageType::REQUEST); |
| 281 | header.instance_id = instanceId & |
| 282 | ocp::accelerator_management::instanceIdMask; |
| 283 | header.msg_type = static_cast<uint8_t>(MessageType::PLATFORM_ENVIRONMENTAL); |
| 284 | |
| 285 | auto rc = packHeader(header, msg.hdr); |
| 286 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 287 | { |
| 288 | return rc; |
| 289 | } |
| 290 | |
| 291 | ReadThermalParametersRequest request{}; |
| 292 | request.hdr.command = static_cast<uint8_t>( |
| 293 | PlatformEnvironmentalCommands::READ_THERMAL_PARAMETERS); |
| 294 | request.hdr.data_size = sizeof(sensorId); |
| 295 | request.sensor_id = sensorId; |
| 296 | |
| 297 | std::memcpy(&msg.data, &request, sizeof(request)); |
| 298 | |
| 299 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 300 | } |
| 301 | |
| 302 | ocp::accelerator_management::CompletionCode decodeReadThermalParametersRequest( |
| 303 | const ocp::accelerator_management::Message& msg, size_t msgLen, |
| 304 | uint8_t& sensorId) |
| 305 | { |
| 306 | if (msgLen < sizeof(ocp::accelerator_management::BindingPciVid) + |
| 307 | sizeof(ReadThermalParametersRequest)) |
| 308 | { |
| 309 | return ocp::accelerator_management::CompletionCode:: |
| 310 | ERR_INVALID_DATA_LENGTH; |
| 311 | } |
| 312 | |
| 313 | ReadThermalParametersRequest request{}; |
| 314 | std::memcpy(&request, &msg.data, sizeof(request)); |
| 315 | |
| 316 | if (request.hdr.data_size < sizeof(request.sensor_id)) |
| 317 | { |
| 318 | return ocp::accelerator_management::CompletionCode::ERR_INVALID_DATA; |
| 319 | } |
| 320 | |
| 321 | sensorId = request.sensor_id; |
| 322 | |
| 323 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 324 | } |
| 325 | |
| 326 | ocp::accelerator_management::CompletionCode encodeReadThermalParametersResponse( |
| 327 | uint8_t instanceId, uint8_t cc, uint16_t reasonCode, int32_t threshold, |
| 328 | ocp::accelerator_management::Message& msg) |
| 329 | { |
| 330 | ocp::accelerator_management::BindingPciVidInfo header{}; |
| 331 | header.ocp_accelerator_management_msg_type = static_cast<uint8_t>( |
| 332 | ocp::accelerator_management::MessageType::RESPONSE); |
| 333 | header.instance_id = instanceId & |
| 334 | ocp::accelerator_management::instanceIdMask; |
| 335 | header.msg_type = static_cast<uint8_t>(MessageType::PLATFORM_ENVIRONMENTAL); |
| 336 | |
| 337 | auto rc = packHeader(header, msg.hdr); |
| 338 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 339 | { |
| 340 | return rc; |
| 341 | } |
| 342 | |
| 343 | if (cc != static_cast<uint8_t>( |
| 344 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 345 | { |
| 346 | return gpu::encodeReasonCode( |
| 347 | cc, reasonCode, |
| 348 | static_cast<uint8_t>( |
| 349 | PlatformEnvironmentalCommands::READ_THERMAL_PARAMETERS), |
| 350 | msg); |
| 351 | } |
| 352 | |
| 353 | ReadThermalParametersResponse response{}; |
| 354 | response.hdr.command = static_cast<uint8_t>( |
| 355 | PlatformEnvironmentalCommands::READ_THERMAL_PARAMETERS); |
| 356 | response.hdr.completion_code = cc; |
| 357 | response.hdr.data_size = htole16(sizeof(uint32_t)); |
| 358 | |
| 359 | response.threshold = htole32(threshold); |
| 360 | |
| 361 | std::memcpy(&msg.data, &response, sizeof(response)); |
| 362 | |
| 363 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 364 | } |
| 365 | |
| 366 | ocp::accelerator_management::CompletionCode decodeReadThermalParametersResponse( |
| 367 | const ocp::accelerator_management::Message& msg, size_t msgLen, uint8_t& cc, |
| 368 | uint16_t& reasonCode, int32_t& threshold) |
| 369 | { |
| 370 | auto rc = gpu::decodeReasonCodeAndCC(msg, msgLen, cc, reasonCode); |
| 371 | if (rc != ocp::accelerator_management::CompletionCode::SUCCESS || |
| 372 | cc != static_cast<uint8_t>( |
| 373 | ocp::accelerator_management::CompletionCode::SUCCESS)) |
| 374 | { |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | if (msgLen < sizeof(ocp::accelerator_management::BindingPciVid) + |
| 379 | sizeof(ReadThermalParametersResponse)) |
| 380 | { |
| 381 | return ocp::accelerator_management::CompletionCode:: |
| 382 | ERR_INVALID_DATA_LENGTH; |
| 383 | } |
| 384 | |
| 385 | ReadThermalParametersResponse response{}; |
| 386 | std::memcpy(&response, &msg.data, sizeof(response)); |
| 387 | |
| 388 | uint16_t dataSize = le16toh(response.hdr.data_size); |
| 389 | if (dataSize != sizeof(int32_t)) |
| 390 | { |
| 391 | return ocp::accelerator_management::CompletionCode::ERR_INVALID_DATA; |
| 392 | } |
| 393 | |
| 394 | threshold = response.threshold; |
| 395 | |
| 396 | return ocp::accelerator_management::CompletionCode::SUCCESS; |
| 397 | } |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 398 | } // namespace gpu |