gem5  v22.1.0.0
fw_cfg.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef __DEV_QEMU_FW_CFG_HH__
29 #define __DEV_QEMU_FW_CFG_HH__
30 
31 #include <cstdint>
32 #include <map>
33 #include <string>
34 #include <type_traits>
35 #include <vector>
36 
38 #include "base/types.hh"
39 #include "dev/io_device.hh"
40 #include "params/QemuFwCfg.hh"
41 #include "params/QemuFwCfgIo.hh"
42 #include "params/QemuFwCfgItem.hh"
43 #include "params/QemuFwCfgItemBytes.hh"
44 #include "params/QemuFwCfgItemFile.hh"
45 #include "params/QemuFwCfgItemString.hh"
46 #include "params/QemuFwCfgMmio.hh"
47 
48 namespace gem5
49 {
50 
51 namespace qemu
52 {
53 
54 /*
55  * Items which can be reported by the firmware config device.
56  */
57 
58 class FwCfgItem
59 {
60  protected:
61  uint16_t _index;
62  const std::string _path;
64 
65  FwCfgItem(const std::string &new_path, bool arch_specific,
66  uint16_t new_index=0) :
67  _index(new_index), _path(new_path), _archSpecific(arch_specific)
68  {}
69 
70  public:
71  uint16_t index() const { return _index; }
72  void index(uint16_t new_index) { _index = new_index; }
73 
74  const std::string &path() const { return _path; }
75  bool archSpecific() const { return _archSpecific; }
76 
77  virtual uint64_t length() const = 0;
78 
79  virtual void read(void *buf, uint64_t offset, uint32_t to_read) = 0;
80 };
81 
82 // Read only items with precomputed data.
83 class FwCfgItemFixed : public FwCfgItem
84 {
85  protected:
86  virtual const void *bytes() const = 0;
87 
88  public:
90 
91  void read(void *buf, uint64_t offset, uint32_t to_read) override;
92 };
93 
94 // An item who's value comes from a file.
96 {
97  private:
99 
100  public:
101  FwCfgItemFile(const std::string &new_path, bool arch_specific,
102  const std::string path, uint16_t new_index=0) :
103  FwCfgItemFixed(new_path, arch_specific, new_index), data(path)
104  {}
105 
106  FwCfgItemFile(const QemuFwCfgItemFileParams &p) :
107  FwCfgItemFile(p.path, p.arch_specific, p.file, p.index)
108  {}
109 
110  const void *bytes() const override { return data.data(); }
111  uint64_t length() const override { return data.len(); }
112 };
113 
114 // An item who's value comes from a string.
116 {
117  private:
118  std::string str;
119 
120  public:
121  FwCfgItemString(const std::string &new_path, bool arch_specific,
122  const std::string _str, uint16_t new_index=0) :
123  FwCfgItemFixed(new_path, arch_specific, new_index), str(_str)
124  {}
125 
126  FwCfgItemString(const QemuFwCfgItemStringParams &p) :
127  FwCfgItemString(p.path, p.arch_specific, p.string, p.index)
128  {}
129 
130  const void *bytes() const override { return (void *)str.data(); }
131  uint64_t
132  length() const override
133  {
134  return sizeof(std::string::value_type) * str.length();
135  }
136 };
137 
138 // An item who's value comes from an array of bytes.
140 {
141  private:
143 
144  public:
145  FwCfgItemBytes(const std::string &new_path, bool arch_specific,
146  const std::vector<uint8_t> &_data, uint16_t new_index=0) :
147  FwCfgItemFixed(new_path, arch_specific, new_index), data(_data)
148  {}
149 
150  FwCfgItemBytes(const QemuFwCfgItemBytesParams &p) :
151  FwCfgItemBytes(p.path, p.arch_specific, p.data, p.index)
152  {}
153 
154  const void *bytes() const override { return (void *)data.data(); }
155  uint64_t length() const override { return data.size(); }
156 };
157 
158 /*
159  * Base and template classes for creating SimObject wrappers for item types.
160  */
161 
163 {
164  public:
165  PARAMS(QemuFwCfgItem);
167 
168  virtual FwCfgItem &item() = 0;
169 };
170 
171 template <class ItemType>
173 {
174  private:
175  ItemType _item;
176 
177  public:
178  template <class PType, class = typename std::enable_if_t<
179  std::is_base_of_v<SimObjectParams, PType>>>
181 
182  FwCfgItem &item() override { return _item; }
183 };
184 
185 /*
186  * The actual firmware config device itself.
187  */
188 
189 // The base class.
190 class FwCfg : public PioDevice
191 {
192  private:
193  std::map<std::string, uint16_t> names;
194  std::map<uint16_t, FwCfgItem *> numbers;
195 
196  uint32_t nextGenericIndex = 0x20;
197  static inline const uint32_t MaxGenericIndex = 0x3fff;
198 
199  uint32_t nextArchIndex = 0x8020;
200  static inline const uint32_t MaxArchIndex = 0xbfff;
201 
202  uint64_t offset = 0;
203  FwCfgItem *current = nullptr;
204 
205  class Directory : public FwCfgItemFixed
206  {
207  private:
209 
210  public:
211  Directory();
212 
213  void update(const std::map<std::string, uint16_t> &names,
214  const std::map<uint16_t, FwCfgItem *> &numbers);
215 
216  const void *bytes() const override { return (void *)data.data(); }
217  uint64_t length() const override { return data.size(); }
218  };
219 
223 
224  void addItem(FwCfgItem *item);
225 
226  protected:
228 
229  void select(uint16_t key);
230 
231  public:
232  PARAMS(QemuFwCfg);
233  FwCfg(const Params &p, const AddrRangeList &addr_ranges);
234 
235  AddrRangeList getAddrRanges() const override { return addrRanges; }
236 
237  void readItem(void *buf, uint32_t length);
238 };
239 
240 // A version which uses IO ports.
241 class FwCfgIo : public FwCfg
242 {
243  private:
245  const Addr dataAddr;
246 
247  public:
248  PARAMS(QemuFwCfgIo);
249  FwCfgIo(const Params &p);
250 
251  Tick read(PacketPtr pkt) override;
252  Tick write(PacketPtr pkt) override;
253 };
254 
255 // A version which uses memory mapped IO.
256 class FwCfgMmio : public FwCfg
257 {
258  private:
260  const Addr dataAddr;
261  const Addr dataSize;
262 
263  public:
264  PARAMS(QemuFwCfgMmio);
265  FwCfgMmio(const Params &p);
266 
267  Tick read(PacketPtr pkt) override;
268  Tick write(PacketPtr pkt) override;
269 };
270 
271 } // namespace qemu
272 } // namespace gem5
273 
274 #endif //__DEV_QEMU_FW_CFG_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
This device is the base class which all devices senstive to an address range inherit from.
Definition: io_device.hh:103
PioDeviceParams Params
Definition: io_device.hh:134
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
SimObjectParams Params
Definition: sim_object.hh:170
uint8_t const * data() const
PARAMS(QemuFwCfgIo)
const Addr dataAddr
Definition: fw_cfg.hh:245
const Addr selectorAddr
Definition: fw_cfg.hh:244
Tick read(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: fw_cfg.cc:235
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: fw_cfg.cc:262
FwCfgIo(const Params &p)
Definition: fw_cfg.cc:227
FwCfgItemBytes(const std::string &new_path, bool arch_specific, const std::vector< uint8_t > &_data, uint16_t new_index=0)
Definition: fw_cfg.hh:145
FwCfgItemBytes(const QemuFwCfgItemBytesParams &p)
Definition: fw_cfg.hh:150
std::vector< uint8_t > data
Definition: fw_cfg.hh:142
uint64_t length() const override
Definition: fw_cfg.hh:155
const void * bytes() const override
Definition: fw_cfg.hh:154
virtual FwCfgItem & item()=0
FwCfgItemFactoryBase(const Params &p)
Definition: fw_cfg.hh:166
FwCfgItem & item() override
Definition: fw_cfg.hh:182
FwCfgItemFactory(const PType &p)
Definition: fw_cfg.hh:180
uint64_t length() const override
Definition: fw_cfg.hh:111
FwCfgItemFile(const QemuFwCfgItemFileParams &p)
Definition: fw_cfg.hh:106
const gem5::loader::ImageFileData data
Definition: fw_cfg.hh:98
FwCfgItemFile(const std::string &new_path, bool arch_specific, const std::string path, uint16_t new_index=0)
Definition: fw_cfg.hh:101
const void * bytes() const override
Definition: fw_cfg.hh:110
void read(void *buf, uint64_t offset, uint32_t to_read) override
Definition: fw_cfg.cc:50
virtual const void * bytes() const =0
uint64_t length() const override
Definition: fw_cfg.hh:132
FwCfgItemString(const std::string &new_path, bool arch_specific, const std::string _str, uint16_t new_index=0)
Definition: fw_cfg.hh:121
FwCfgItemString(const QemuFwCfgItemStringParams &p)
Definition: fw_cfg.hh:126
const void * bytes() const override
Definition: fw_cfg.hh:130
const std::string _path
Definition: fw_cfg.hh:62
virtual void read(void *buf, uint64_t offset, uint32_t to_read)=0
bool archSpecific() const
Definition: fw_cfg.hh:75
virtual uint64_t length() const =0
void index(uint16_t new_index)
Definition: fw_cfg.hh:72
const std::string & path() const
Definition: fw_cfg.hh:74
FwCfgItem(const std::string &new_path, bool arch_specific, uint16_t new_index=0)
Definition: fw_cfg.hh:65
uint16_t index() const
Definition: fw_cfg.hh:71
const Addr selectorAddr
Definition: fw_cfg.hh:259
FwCfgMmio(const Params &p)
Definition: fw_cfg.cc:289
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: fw_cfg.cc:324
const Addr dataSize
Definition: fw_cfg.hh:261
Tick read(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: fw_cfg.cc:297
PARAMS(QemuFwCfgMmio)
const Addr dataAddr
Definition: fw_cfg.hh:260
uint64_t length() const override
Definition: fw_cfg.hh:217
void update(const std::map< std::string, uint16_t > &names, const std::map< uint16_t, FwCfgItem * > &numbers)
Definition: fw_cfg.cc:188
std::vector< uint8_t > data
Definition: fw_cfg.hh:208
const void * bytes() const override
Definition: fw_cfg.hh:216
uint32_t nextArchIndex
Definition: fw_cfg.hh:199
Directory directory
Definition: fw_cfg.hh:222
FwCfg(const Params &p, const AddrRangeList &addr_ranges)
Definition: fw_cfg.cc:79
AddrRangeList getAddrRanges() const override
Every PIO device is obliged to provide an implementation that returns the address ranges the device r...
Definition: fw_cfg.hh:235
void readItem(void *buf, uint32_t length)
Definition: fw_cfg.cc:160
uint32_t nextGenericIndex
Definition: fw_cfg.hh:196
FwCfgItemString id
Definition: fw_cfg.hh:221
std::map< uint16_t, FwCfgItem * > numbers
Definition: fw_cfg.hh:194
PARAMS(QemuFwCfg)
const AddrRangeList addrRanges
Definition: fw_cfg.hh:227
static const uint32_t MaxGenericIndex
Definition: fw_cfg.hh:197
FwCfgItem * current
Definition: fw_cfg.hh:203
static const uint32_t MaxArchIndex
Definition: fw_cfg.hh:200
void select(uint16_t key)
Definition: fw_cfg.cc:136
FwCfgItemString signature
Definition: fw_cfg.hh:220
uint64_t offset
Definition: fw_cfg.hh:202
std::map< std::string, uint16_t > names
Definition: fw_cfg.hh:193
void addItem(FwCfgItem *item)
Definition: fw_cfg.cc:116
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 54 > p
Definition: pagetable.hh:70
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
uint64_t Tick
Tick count type.
Definition: types.hh:58

Generated on Wed Dec 21 2022 10:22:34 for gem5 by doxygen 1.9.1