C++ Device API Reference Manual
Reference documentation for the Simics C++ Device API.
 
Loading...
Searching...
No Matches
mappable-conf-object.h
Go to the documentation of this file.
1// -*- mode: C++; c-file-style: "virtutech-c++" -*-
2
3/*
4 © 2022 Intel Corporation
5
6 This software and the related documents are Intel copyrighted materials, and
7 your use of them is governed by the express license under which they were
8 provided to you ("License"). Unless the License provides otherwise, you may
9 not use, modify, copy, publish, distribute, disclose or transmit this software
10 or the related documents without Intel's prior written permission.
11
12 This software and the related documents are provided as is, with no express or
13 implied warranties, other than those that are expressly stated in the License.
14*/
15
16#ifndef SIMICS_MAPPABLE_CONF_OBJECT_H
17#define SIMICS_MAPPABLE_CONF_OBJECT_H
18
19#include <simics/base/log.h> // SIM_LOG_ERROR
20
21#include <stdexcept>
22#include <string>
23#include <string_view>
24#include <tuple>
25#include <unordered_map>
26#include <unordered_set>
27
28#include "simics/conf-object.h"
31#include "simics/utility.h" // hash_str
32
33namespace simics {
34
45template <typename IFACE>
47 public:
59 void set_iface(const std::string &name, IFACE *iface) override {
60 if (!iface) {
61 throw std::invalid_argument {
62 "Cannot set with NULL interface"
63 };
64 } else if (name.empty()) {
65 throw std::invalid_argument {
66 "Cannot set with empty name string"
67 };
68 } else {
69 name_to_iface_[hash_str(name)] = iface;
70 }
71 }
72
82 IFACE *get_iface(const std::string &name) const override {
83 auto hashed = hash_str(name);
84 if (name_to_iface_.find(hashed) == name_to_iface_.end()) {
85 return nullptr;
86 }
87 return name_to_iface_.at(hashed);
88 }
89
99 IFACE *get_iface(size_t name_hash) const {
100 if (name_to_iface_.find(name_hash) == name_to_iface_.end()) {
101 return nullptr;
102 }
103 return name_to_iface_.at(name_hash);
104 }
105
114 void erase_iface(const std::string &name) override {
115 name_to_iface_.erase(hash_str(name));
116 }
117
118 private:
120 std::unordered_map<size_t, IFACE *> name_to_iface_;
121};
122
123class BankInterface;
124class RegisterInterface;
125class FieldInterface;
126
127/*
128 * A class extends ConfObject to support memory mapped bank registers
129 *
130 * It manages a container which associates a unique name to its corresponding
131 * operational interface. These 3 object types are supported: bank, register
132 * and field.
133 */
135 public:
137 virtual ~MappableConfObject() = default;
138
144 template <typename IFACE>
145 void set_iface(const std::string &name, IFACE *iface) {
146 if (finalized()) {
147 SIM_LOG_ERROR(obj(), 0,
148 "Cannot set interface for %s when ConfObject"
149 " has been finalized", name.c_str());
150 return;
151 }
152
153 auto current_iface = get_iface<IFACE>(name);
154 if (current_iface && current_iface != iface) {
155 if (iface_maps_write_protected_) {
156 SIM_LOG_INFO(3, obj(), 0,
157 "Interface for %s ignored since iface_map is"
158 " write protected", name.c_str());
159 return;
160 }
161 SIM_LOG_INFO(4, obj(), 0,
162 "Interface for %s overridden", name.c_str());
163 }
164
165 try {
166 std::get<MapNameToInterfaceObject<IFACE>>(
167 iface_maps_).set_iface(name, iface);
168 } catch (const std::exception &e) {
169 SIM_LOG_ERROR(obj(), 0, "%s", e.what());
170 }
171 }
172
179 template <typename IFACE>
180 IFACE *get_iface(const std::string &name) const {
181 return std::get<MapNameToInterfaceObject<IFACE>>(
182 iface_maps_).get_iface(name);
183 }
184
188 RegisterInterface *get_iface(size_t name_hash) const {
189 return std::get<MapNameToInterfaceObject<RegisterInterface>>(
190 iface_maps_).get_iface(name_hash);
191 }
192
197 template <typename IFACE>
198 void erase_iface(const std::string &name) {
199 std::get<MapNameToInterfaceObject<IFACE>>(
200 iface_maps_).erase_iface(name);
201 }
202
203 // Whether to represent the bits in big endian, i.e, whether the bit
204 // number 0 refers to the most significant bit.
205 virtual bool big_endian_bitorder() {
206 return false;
207 }
208
210 bank_memory_t *get_bank_memory(std::string_view name_of_memory) {
211 return &allocated_bank_memories_[name_of_memory.data()];
212 }
213
215 void write_protect_iface_maps(bool write_protect) {
216 iface_maps_write_protected_ = write_protect;
217 }
218
219 private:
221 std::tuple<MapNameToInterfaceObject<BankInterface>,
224
225 /*
226 * The allocated_bank_memories_ maps a string key to a
227 * value of type bank_memory_t.
228 *
229 * This structure groups bytes memory for all banks, can be used
230 * to efficiently store and access byte-level data using the name
231 * and byte offset as keys.
232 *
233 * By default, each bank uses its bank name as the key to get the
234 * bank memory. For SharedMemoryBank, multiple banks can share the
235 * same bank memory by using the same key. The key can be any unique
236 * string.
237 */
238 std::unordered_map<std::string,
239 bank_memory_t> allocated_bank_memories_;
240
242 bool iface_maps_write_protected_ {false};
243};
244
245} // namespace simics
246
247#endif
Base class for all Simics configuration objects.
Definition: conf-object.h:126
ConfObject(const ConfObjectRef &obj)
Create a ConfObject from ConfObjectRef.
Definition: conf-object.h:129
virtual bool finalized()
Return if the finalize method has been called.
Definition: conf-object.h:140
ConfObjectRef obj() const
Return a ConfObjectRef represents this object.
Definition: conf-object.h:137
Class that supports get/set a pointer to IFACE with a string name.
Definition: mappable-conf-object.h:46
void set_iface(const std::string &name, IFACE *iface) override
Sets the IFACE associated with the given name.
Definition: mappable-conf-object.h:59
IFACE * get_iface(const std::string &name) const override
Gets the interface associated with the given name.
Definition: mappable-conf-object.h:82
void erase_iface(const std::string &name) override
Erases the interface associated with the given name.
Definition: mappable-conf-object.h:114
IFACE * get_iface(size_t name_hash) const
Gets the interface associated with the given name hash.
Definition: mappable-conf-object.h:99
Definition: map-name-to-interface.h:24
Definition: mappable-conf-object.h:134
void write_protect_iface_maps(bool write_protect)
Whether to write protect the iface_maps, default is not write protected.
Definition: mappable-conf-object.h:215
void erase_iface(const std::string &name)
Erase the IFACE interface by name.
Definition: mappable-conf-object.h:198
RegisterInterface * get_iface(size_t name_hash) const
Get the RegisterInterface* by name hash.
Definition: mappable-conf-object.h:188
bank_memory_t * get_bank_memory(std::string_view name_of_memory)
Get the bank memory by name.
Definition: mappable-conf-object.h:210
virtual bool big_endian_bitorder()
Definition: mappable-conf-object.h:205
virtual ~MappableConfObject()=default
void set_iface(const std::string &name, IFACE *iface)
Set the IFACE interface* by name.
Definition: mappable-conf-object.h:145
IFACE * get_iface(const std::string &name) const
Get the IFACE interface* by name.
Definition: mappable-conf-object.h:180
Definition: register-interface.h:37
Definition: after-bank.h:33
std::unordered_map< Offset, uint8_t > bank_memory_t
Definition: bank-type.h:52
size_t hash_str(const std::string &name)
Hashes a string to a size_t value.