Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <stdexcept> |
| 3 | #include <array> |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 4 | #include <endian.h> |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 5 | #include "sbe_interfaces.hpp" |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 6 | #include "sbe_chipOp_handler.hpp" |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 7 | |
| 8 | namespace openpower |
| 9 | { |
| 10 | namespace sbe |
| 11 | { |
| 12 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 13 | constexpr size_t RESP_HEADER_LEN = 0x3; |
| 14 | |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 15 | //Helper interfaces |
| 16 | static inline uint32_t upper(uint64_t value) |
| 17 | { |
| 18 | return ((value & 0xFFFFFFFF00000000ull) >> 32); |
| 19 | } |
| 20 | |
| 21 | static inline uint32_t lower(uint64_t value) |
| 22 | { |
| 23 | return (value & 0xFFFFFFFF); |
| 24 | } |
| 25 | |
| 26 | using sbe_word_t = uint32_t; |
| 27 | |
| 28 | namespace scom |
| 29 | { |
| 30 | |
| 31 | //Constants specific to SCOM operations |
| 32 | static constexpr sbe_word_t READ_OPCODE = 0x0000A201; |
| 33 | static constexpr sbe_word_t WRITE_OPCODE = 0x0000A202; |
| 34 | static constexpr size_t READ_CMD_LENGTH = 0x4; |
| 35 | static constexpr size_t WRITE_CMD_LENGTH = 0x6; |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 36 | static constexpr size_t READ_RESP_LENGTH = 0x2; |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 37 | |
| 38 | //Reading SCOM Registers |
| 39 | uint64_t read(const char* devPath, |
| 40 | uint64_t address) |
| 41 | { |
| 42 | uint64_t value = 0; |
| 43 | |
| 44 | //Validate input device path |
| 45 | if (devPath == nullptr) |
| 46 | { |
| 47 | throw std::runtime_error("NULL FIFO device path"); |
| 48 | } |
| 49 | |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 50 | //Build SCOM read request command. |
| 51 | //Handle byte order mismatch ,SBE is big endian and BMC is |
| 52 | //little endian. |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 53 | std::array<sbe_word_t, READ_CMD_LENGTH> command = |
| 54 | { |
Benjamin Herrenschmidt | 7a6479f | 2018-05-17 15:25:24 +1000 | [diff] [blame] | 55 | static_cast<sbe_word_t>(htobe32(READ_CMD_LENGTH)), |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 56 | htobe32(READ_OPCODE), |
| 57 | htobe32(upper(address)), |
| 58 | htobe32(lower(address)) |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 61 | //Buffer to hold the response data along with the SBE header |
| 62 | const size_t respLength = RESP_HEADER_LEN + READ_RESP_LENGTH ; |
| 63 | std::array<sbe_word_t, respLength> response = {}; |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 64 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 65 | //Write the command buffer to the SBE FIFO and obtain the response from the |
| 66 | //SBE FIFO device.This interface will parse the obtained SBE response and |
| 67 | //any internal SBE failures will be communicated via exceptions |
| 68 | invokeSBEChipOperation(devPath, command, response); |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 69 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 70 | value = (((static_cast<uint64_t>(response[0])) << 32) | response[1]); |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 71 | return value; |
| 72 | } |
| 73 | |
| 74 | void write(const char* devPath, |
| 75 | uint64_t address, |
| 76 | uint64_t data) |
| 77 | { |
| 78 | //Validate input device path |
| 79 | if (devPath == nullptr) |
| 80 | { |
| 81 | throw std::runtime_error("NULL FIFO device path"); |
| 82 | } |
| 83 | |
| 84 | //Build SCOM write request command |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 85 | //Handle byte order mismatch, SBE is big endian and BMC is |
| 86 | //little endian. |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 87 | std::array<sbe_word_t, WRITE_CMD_LENGTH> command = |
| 88 | { |
Benjamin Herrenschmidt | 7a6479f | 2018-05-17 15:25:24 +1000 | [diff] [blame] | 89 | static_cast<sbe_word_t>(htobe32(WRITE_CMD_LENGTH)), |
Murulidhar Nataraju | 06a0c2c | 2017-07-11 08:55:33 -0500 | [diff] [blame] | 90 | htobe32(WRITE_OPCODE), |
| 91 | htobe32(upper(address)), |
| 92 | htobe32(lower(address)), |
| 93 | htobe32(upper(data)), |
| 94 | htobe32(lower(data)) |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 97 | //Buffer to hold the SBE response status |
| 98 | const size_t respLength = RESP_HEADER_LEN; |
| 99 | std::array<sbe_word_t, respLength> response = {}; |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 100 | |
Murulidhar Nataraju | 1adec02 | 2017-04-20 12:05:51 -0500 | [diff] [blame] | 101 | //Write the command buffer to the SBE FIFO and obtain the response from the |
| 102 | //SBE FIFO device.This interface will parse the obtained SBE response and |
| 103 | //any internal SBE failures will be communicated via exceptions |
| 104 | invokeSBEChipOperation(devPath, command, response); |
Murulidhar Nataraju | 207bed4 | 2017-03-31 07:38:55 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace scom |
| 108 | } // namespace sbe |
| 109 | } // namespace openpower |