Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 1 | #pragma once |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 2 | |
Zane Shelley | 52cb1a9 | 2019-08-21 14:38:31 -0500 | [diff] [blame] | 3 | #include <hei_includes.hpp> |
| 4 | #include <register/hei_register.hpp> |
| 5 | #include <util/hei_bit_string.hpp> |
| 6 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 7 | namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 8 | { |
| 9 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 10 | /** |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 11 | * @brief An abstract class containing information (e.g. address, type, length, |
| 12 | * etc.) for an actual hardware register. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 13 | * |
| 14 | * Hardware access: |
| 15 | * |
| 16 | * Actual hardware access is defined by the user application via the user |
| 17 | * interface APIs. In order to tell the user application which chip to target, |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 18 | * the user application will give the isolator a list of pointers to its |
| 19 | * objects. They will then be passed into the public functions of this class |
| 20 | * and eventually given back to the user application when hardware access is |
| 21 | * needed. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 22 | * |
| 23 | * Register cache: |
| 24 | * |
| 25 | * In order to save memory space, each instance of this class does not store |
| 26 | * the contents of the target hardware register. Instead, that data is stored |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame] | 27 | * in a register cache, which is a static variable defined in this class. This |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 28 | * allows us to store only what we need. The cache can also be thought of as a |
| 29 | * snapshot of the registers at the time of isolation, which can be useful if |
| 30 | * the hardware is still running and register values could change. |
| 31 | * |
| 32 | * In order to ensure stale data isn't used from the cache, call |
| 33 | * HardwareRegister::flushAll() before beginning isolation on a new attention. |
| 34 | * Also, HardwareRegister::flushAll() should be called when the isolator is |
| 35 | * uninitialized before the rest of the isolation objects are deleted. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 36 | */ |
Zane Shelley | cd36f43 | 2019-08-30 21:22:07 -0500 | [diff] [blame] | 37 | class HardwareRegister : public Register |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 38 | { |
| 39 | public: |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 40 | /** @brief Pure virtual destructor. */ |
| 41 | virtual ~HardwareRegister() = 0; |
| 42 | |
| 43 | protected: |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 44 | /** |
| 45 | * @brief Constructor from components. |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 46 | * @param i_id Unique ID for this register. |
| 47 | * @param i_instance Instance of this register |
| 48 | * @param i_flags Attribute flags for this register. |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 49 | */ |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 50 | HardwareRegister(RegisterId_t i_id, Instance_t i_instance, |
| 51 | RegisterAttributeFlags_t i_flags) : |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 52 | Register(), |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 53 | iv_id(i_id), iv_instance(i_instance), iv_flags(i_flags) |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 54 | {} |
| 55 | |
| 56 | private: // Instance variables |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 57 | /** The unique ID for this register. */ |
| 58 | const RegisterId_t iv_id; |
| 59 | |
| 60 | /** A register may have multiple instances. All of which will have the same |
| 61 | * ID. This variable is used to distinguish between each instance of the |
| 62 | * register. */ |
Zane Shelley | 13b182b | 2020-05-07 20:23:45 -0500 | [diff] [blame] | 63 | const Instance_t iv_instance; |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 64 | |
| 65 | /** The hardware access level of this register (read/write, read-only, |
| 66 | * write-only, etc.). */ |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 67 | const RegisterAttributeFlags_t iv_flags; |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 68 | |
| 69 | public: // Accessor functions |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 70 | /* @return The unique ID for this register. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 71 | RegisterId_t getId() const |
| 72 | { |
| 73 | return iv_id; |
| 74 | } |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 75 | |
| 76 | /* @return The instance of this register. */ |
Zane Shelley | 13b182b | 2020-05-07 20:23:45 -0500 | [diff] [blame] | 77 | Instance_t getInstance() const |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 78 | { |
| 79 | return iv_instance; |
| 80 | } |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 81 | |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 82 | /** @return True if given flag is enabled, false if disabled. */ |
| 83 | bool queryAttrFlag(RegisterAttributeFlags_t i_flag) const |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 84 | { |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 85 | return (0 != (iv_flags & i_flag)); |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 86 | } |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 87 | |
| 88 | // NOTE: The following are determined by child classes. |
| 89 | |
| 90 | /** @return This register's type. */ |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 91 | virtual RegisterType_t getType() const = 0; |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 92 | |
| 93 | /** @return The address of this register. */ |
| 94 | virtual RegisterAddress_t getAddress() const = 0; |
| 95 | |
| 96 | /** @return The size (in bytes) of this register. */ |
| 97 | virtual size_t getSize() const = 0; |
| 98 | |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 99 | public: // Operators |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 100 | /** @brief Equals operator. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 101 | bool operator==(const HardwareRegister& i_r) const |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 102 | { |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 103 | // Comparing register type and address should be sufficient. |
| 104 | return (getAddress() == i_r.getAddress()) && |
| 105 | (getType() == i_r.getType()); |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** @brief Less than operator. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 109 | bool operator<(const HardwareRegister& i_r) const |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 110 | { |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 111 | // Comparing register type and address should be sufficient. |
| 112 | if (getAddress() < i_r.getAddress()) |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 113 | { |
| 114 | return true; |
| 115 | } |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 116 | else if (getAddress() == i_r.getAddress()) |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 117 | { |
Zane Shelley | 5ec8810 | 2020-05-11 21:08:25 -0500 | [diff] [blame^] | 118 | return (getType() < i_r.getType()); |
Zane Shelley | 75e68e9 | 2019-10-18 16:16:23 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 124 | public: |
Zane Shelley | 65ed96a | 2019-10-14 13:06:11 -0500 | [diff] [blame] | 125 | /** Function overloaded from parent Register class. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 126 | const BitString* getBitString(const Chip& i_chip) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 127 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 128 | /** |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 129 | * @brief Reads a register from hardware via the user interface APIs. |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 130 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 131 | * @param i_force When false, this function will only read from hardware if |
| 132 | * an entry for this instance does not already exist in the |
| 133 | * register cache. When true, the entry in the register |
| 134 | * cache is flushed, if it exists. Then this function will |
| 135 | * read from hardware and update the cache. |
| 136 | * @return See the return code from the registerRead() user interface API. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 137 | */ |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 138 | bool read(const Chip& i_chip, bool i_force = false) const; |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 139 | |
Ben Tyner | 7b3420b | 2020-05-11 10:52:07 -0500 | [diff] [blame] | 140 | #ifdef __HEI_ENABLE_HW_WRITE |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 143 | * @brief Writes the value stored in the register cache to hardware via the |
| 144 | * user interface APIs. |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 145 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 146 | * @return See the return code from the registerWrite() user interface API. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 147 | */ |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 148 | bool write(const Chip& i_chip) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 149 | |
Ben Tyner | 7b3420b | 2020-05-11 10:52:07 -0500 | [diff] [blame] | 150 | #endif // __HEI_ENABLE_HW_WRITE |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 151 | |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 152 | protected: |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 153 | /** |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 154 | * @brief Provides access to this register's BitString. |
| 155 | * |
| 156 | * WARNING: Allowing public access to this function may be dangerous. For |
| 157 | * now it should be left as protected. |
| 158 | * |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 159 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 160 | * @return A reference to the BitString. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 161 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 162 | BitString& accessBitString(const Chip& i_chip); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 163 | |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 164 | private: // Register cache class variable |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 165 | /** |
| 166 | * @brief Caches the contents of registers read from hardware. |
| 167 | * |
| 168 | * The goal is to create a snapshot of the hardware register contents as |
| 169 | * close to the reported attention as possible. This snapshot is then used |
| 170 | * for additional analysis/debug when needed. |
| 171 | */ |
| 172 | class Cache |
| 173 | { |
| 174 | public: |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 175 | /** @brief Default constructor. */ |
| 176 | Cache() = default; |
| 177 | |
| 178 | /** @brief Destructor. */ |
| 179 | ~Cache() = default; |
| 180 | |
| 181 | /** @brief Copy constructor. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 182 | Cache(const Cache&) = delete; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 183 | |
| 184 | /** @brief Assignment operator. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 185 | Cache& operator=(const Cache&) = delete; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * @brief Queries if a specific entry exists in the cache. |
| 189 | * @param i_chip The target chip. |
| 190 | * @param i_hwReg The target register. |
| 191 | * @return True if the entry exists, false otherwise. |
| 192 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 193 | bool query(const Chip& i_chip, const HardwareRegister* i_hwReg) const; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * @brief Returns the data buffer for the given chip and register. |
| 197 | * @param i_chip The target chip. |
| 198 | * @param i_hwReg The target register. |
| 199 | * @return A reference to the BitString containing the register data. |
| 200 | * @note If an entry does not exist in the cache, an entry will be |
| 201 | * created and the BitString will be initialized to 0. |
| 202 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 203 | BitString& access(const Chip& i_chip, const HardwareRegister* i_hwReg); |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 204 | |
| 205 | /** @brief Flushes entire contents from cache. */ |
| 206 | void flush(); |
| 207 | |
| 208 | /** |
| 209 | * @brief Removes a single register from the cache. |
| 210 | * @param i_chip The target chip. |
| 211 | * @param i_hwReg The target register. |
| 212 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 213 | void flush(const Chip& i_chip, const HardwareRegister* i_hwReg); |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 214 | |
| 215 | private: |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 216 | /** |
| 217 | * @brief Stores a BitStringBuffer for each HardwareRegister per Chip. |
| 218 | * |
| 219 | * The HardwareRegister keys will just be pointers to the isolation |
| 220 | * objects created in the main initialize() API. Those should exist |
| 221 | * until the main uninitialize() API is called. It is important that the |
| 222 | * cache is flushed at the beginning of the uninitialize() API before |
| 223 | * the rest of the isolation objects are deleted. |
| 224 | * |
| 225 | * The Chip keys are copies of the objects passed to the isolator |
| 226 | * because the user application is responsible for storage of the |
| 227 | * objects passed to the isolator. We don't want to chance a Chip was |
| 228 | * created as a local variable that goes out of scope, or other similar |
| 229 | * situations. |
| 230 | */ |
| 231 | std::map<Chip, std::map<const HardwareRegister*, BitString*>> iv_cache; |
| 232 | }; |
| 233 | |
| 234 | /** This allows all HardwareRegister objects access to the cache. */ |
| 235 | static Cache cv_cache; |
| 236 | |
| 237 | public: // Register cache management functions. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 238 | /** @brief Flushes the entire register cache. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 239 | static void flushAll() |
| 240 | { |
| 241 | cv_cache.flush(); |
| 242 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 243 | |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 244 | /** |
| 245 | * @brief Flushes this register from the cache. |
| 246 | * @param i_chip The target chip in which this register belongs. |
| 247 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 248 | void flush(const Chip& i_chip) const |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 249 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 250 | cv_cache.flush(i_chip, this); |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 253 | private: // Register cache management functions. |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 254 | /** |
| 255 | * @param i_chip The target chip in which this register belongs. |
| 256 | * @return True if an entry for this register exist in this cache. |
| 257 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 258 | bool queryCache(const Chip& i_chip) const |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 259 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 260 | return cv_cache.query(i_chip, this); |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @param i_chip The target chip in which this register belongs. |
| 265 | * @return A reference to this register's BitString in cache. |
| 266 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 267 | BitString& accessCache(const Chip& i_chip) const |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 268 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 269 | return cv_cache.access(i_chip, this); |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 270 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 271 | }; |
| 272 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 273 | } // end namespace libhei |