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: |
| 40 | |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 41 | /** @brief Pure virtual destructor. */ |
| 42 | virtual ~HardwareRegister() = 0; |
| 43 | |
| 44 | protected: |
| 45 | |
| 46 | /** |
| 47 | * @brief Constructor from components. |
| 48 | * @param i_chipType Type of chip associated with this register. |
| 49 | * @param i_id Unique ID for this register. |
| 50 | * @param i_instance Instance of this register |
| 51 | * @param i_accessLevel Hardware access level for this register. |
| 52 | */ |
| 53 | HardwareRegister( ChipType_t i_chipType, RegisterId_t i_id, |
| 54 | RegisterInstance_t i_instance, |
| 55 | RegisterAccessLevel_t i_accessLevel ) : |
| 56 | Register(), iv_chipType( i_chipType ), iv_id( i_id ), |
| 57 | iv_instance( i_instance ), iv_accessLevel( i_accessLevel ) |
| 58 | {} |
| 59 | |
| 60 | private: // Instance variables |
| 61 | |
| 62 | /** The type of chip associated with register. */ |
| 63 | const ChipType_t iv_chipType; |
| 64 | |
| 65 | /** The unique ID for this register. */ |
| 66 | const RegisterId_t iv_id; |
| 67 | |
| 68 | /** A register may have multiple instances. All of which will have the same |
| 69 | * ID. This variable is used to distinguish between each instance of the |
| 70 | * register. */ |
| 71 | const RegisterInstance_t iv_instance; |
| 72 | |
| 73 | /** The hardware access level of this register (read/write, read-only, |
| 74 | * write-only, etc.). */ |
| 75 | const RegisterAccessLevel_t iv_accessLevel; |
| 76 | |
| 77 | public: // Accessor functions |
| 78 | |
| 79 | /** @return The type of chip associated with this register. */ |
| 80 | ChipType_t getChipType() const { return iv_chipType; } |
| 81 | |
| 82 | /* @return The unique ID for this register. */ |
| 83 | RegisterId_t getId() const { return iv_id; } |
| 84 | |
| 85 | /* @return The instance of this register. */ |
| 86 | RegisterInstance_t getInstance() const { return iv_instance; } |
| 87 | |
| 88 | /** @return The hardware access level of this register. */ |
| 89 | RegisterAccessLevel_t getAccessLevel() const { return iv_accessLevel; } |
| 90 | |
| 91 | // NOTE: The following are determined by child classes. |
| 92 | |
| 93 | /** @return This register's type. */ |
| 94 | virtual RegisterType_t getRegisterType() const = 0; |
| 95 | |
| 96 | /** @return The address of this register. */ |
| 97 | virtual RegisterAddress_t getAddress() const = 0; |
| 98 | |
| 99 | /** @return The size (in bytes) of this register. */ |
| 100 | virtual size_t getSize() const = 0; |
| 101 | |
| 102 | public: |
| 103 | |
Zane Shelley | 65ed96a | 2019-10-14 13:06:11 -0500 | [diff] [blame] | 104 | /** Function overloaded from parent Register class. */ |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 105 | const BitString * getBitString( const Chip & i_chip ) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 106 | |
Zane Shelley | 65ed96a | 2019-10-14 13:06:11 -0500 | [diff] [blame] | 107 | #if 0 |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 108 | /** |
| 109 | * @brief Updates bit string contents associated with register |
| 110 | * @param i_bs poiner to bit string |
| 111 | * @return Nil |
| 112 | */ |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame] | 113 | virtual void setBitString(const BitString * i_bs) ; |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 114 | #endif |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 115 | |
| 116 | /** |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 117 | * @brief Reads a register from hardware via the user interface APIs. |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 118 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 119 | * @param i_force When false, this function will only read from hardware if |
| 120 | * an entry for this instance does not already exist in the |
| 121 | * register cache. When true, the entry in the register |
| 122 | * cache is flushed, if it exists. Then this function will |
| 123 | * read from hardware and update the cache. |
| 124 | * @return See the return code from the registerRead() user interface API. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 125 | */ |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 126 | ReturnCode read( const Chip & i_chip, bool i_force = false ) const; |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 127 | |
| 128 | #ifndef __HEI_READ_ONLY |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 129 | |
| 130 | /** |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 131 | * @brief Writes the value stored in the register cache to hardware via the |
| 132 | * user interface APIs. |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 133 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 134 | * @return See the return code from the registerWrite() user interface API. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 135 | */ |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 136 | ReturnCode write( const Chip & i_chip ) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 137 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 138 | #endif // __HEI_READ_ONLY |
| 139 | |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 140 | protected: |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 141 | |
| 142 | /** |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 143 | * @brief Provides access to this register's BitString. |
| 144 | * |
| 145 | * WARNING: Allowing public access to this function may be dangerous. For |
| 146 | * now it should be left as protected. |
| 147 | * |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 148 | * @param i_chip The target chip in which this register belongs. |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 149 | * @return A reference to the BitString. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 150 | */ |
Zane Shelley | afa669a | 2019-10-15 13:23:17 -0500 | [diff] [blame] | 151 | BitString & accessBitString( const Chip & i_chip ); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 152 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 153 | private: // Hardware accessor management functions. |
| 154 | |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 155 | /** @brief Asserts this register belongs on the target accessor chip. */ |
| 156 | void verifyAccessorChip( const Chip & i_chip ) const |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 157 | { |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 158 | HEI_ASSERT( getChipType() == i_chip.getType() ); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 159 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 160 | |
| 161 | private: // Register cache class variable |
| 162 | |
| 163 | /** |
| 164 | * @brief Caches the contents of registers read from hardware. |
| 165 | * |
| 166 | * The goal is to create a snapshot of the hardware register contents as |
| 167 | * close to the reported attention as possible. This snapshot is then used |
| 168 | * for additional analysis/debug when needed. |
| 169 | */ |
| 170 | class Cache |
| 171 | { |
| 172 | public: |
| 173 | |
| 174 | /** @brief Default constructor. */ |
| 175 | Cache() = default; |
| 176 | |
| 177 | /** @brief Destructor. */ |
| 178 | ~Cache() = default; |
| 179 | |
| 180 | /** @brief Copy constructor. */ |
| 181 | Cache( const Cache & ) = delete; |
| 182 | |
| 183 | /** @brief Assignment operator. */ |
| 184 | Cache & operator=( const Cache & ) = delete; |
| 185 | |
| 186 | /** |
| 187 | * @brief Queries if a specific entry exists in the cache. |
| 188 | * @param i_chip The target chip. |
| 189 | * @param i_hwReg The target register. |
| 190 | * @return True if the entry exists, false otherwise. |
| 191 | */ |
| 192 | bool query( const Chip & i_chip, |
| 193 | const HardwareRegister * i_hwReg ) const; |
| 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 | */ |
| 203 | BitString & access( const Chip & i_chip, |
| 204 | const HardwareRegister * i_hwReg ); |
| 205 | |
| 206 | /** @brief Flushes entire contents from cache. */ |
| 207 | void flush(); |
| 208 | |
| 209 | /** |
| 210 | * @brief Removes a single register from the cache. |
| 211 | * @param i_chip The target chip. |
| 212 | * @param i_hwReg The target register. |
| 213 | */ |
| 214 | void flush( const Chip & i_chip, const HardwareRegister * i_hwReg ); |
| 215 | |
| 216 | private: |
| 217 | |
| 218 | /** |
| 219 | * @brief Stores a BitStringBuffer for each HardwareRegister per Chip. |
| 220 | * |
| 221 | * The HardwareRegister keys will just be pointers to the isolation |
| 222 | * objects created in the main initialize() API. Those should exist |
| 223 | * until the main uninitialize() API is called. It is important that the |
| 224 | * cache is flushed at the beginning of the uninitialize() API before |
| 225 | * the rest of the isolation objects are deleted. |
| 226 | * |
| 227 | * The Chip keys are copies of the objects passed to the isolator |
| 228 | * because the user application is responsible for storage of the |
| 229 | * objects passed to the isolator. We don't want to chance a Chip was |
| 230 | * created as a local variable that goes out of scope, or other similar |
| 231 | * situations. |
| 232 | */ |
| 233 | std::map<Chip, std::map<const HardwareRegister*, BitString*>> iv_cache; |
| 234 | }; |
| 235 | |
| 236 | /** This allows all HardwareRegister objects access to the cache. */ |
| 237 | static Cache cv_cache; |
| 238 | |
| 239 | public: // Register cache management functions. |
| 240 | |
| 241 | /** @brief Flushes the entire register cache. */ |
| 242 | static void flushAll() { cv_cache.flush(); } |
| 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 | */ |
| 248 | void flush( const Chip & i_chip ) const |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 249 | { |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -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. |
| 254 | |
| 255 | /** |
| 256 | * @param i_chip The target chip in which this register belongs. |
| 257 | * @return True if an entry for this register exist in this cache. |
| 258 | */ |
| 259 | bool queryCache( const Chip & i_chip ) const |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 260 | { |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 261 | return cv_cache.query( i_chip, this ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param i_chip The target chip in which this register belongs. |
| 266 | * @return A reference to this register's BitString in cache. |
| 267 | */ |
| 268 | BitString & accessCache( const Chip & i_chip ) const |
| 269 | { |
| 270 | return cv_cache.access( i_chip, this ); |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 271 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 272 | }; |
| 273 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 274 | } // end namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 275 | |