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