summaryrefslogtreecommitdiff
path: root/utils/regtools/include/soc_desc.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/regtools/include/soc_desc.hpp')
-rw-r--r--utils/regtools/include/soc_desc.hpp384
1 files changed, 384 insertions, 0 deletions
diff --git a/utils/regtools/include/soc_desc.hpp b/utils/regtools/include/soc_desc.hpp
new file mode 100644
index 0000000000..66f2e3b6e1
--- /dev/null
+++ b/utils/regtools/include/soc_desc.hpp
@@ -0,0 +1,384 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2014 by Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef __SOC_DESC__
22#define __SOC_DESC__
23
24#include <stdint.h>
25#include <vector>
26#include <list>
27#include <string>
28#include <map>
29
30namespace soc_desc
31{
32
33const size_t MAJOR_VERSION = 2;
34const size_t MINOR_VERSION = 0;
35const size_t REVISION_VERSION = 0;
36
37/** Typedef for SoC types: word, address and flags */
38typedef uint32_t soc_addr_t;
39typedef uint32_t soc_word_t;
40typedef int soc_id_t;
41
42/** Error class */
43class error_t
44{
45public:
46 enum level_t
47 {
48 INFO,
49 WARNING,
50 FATAL
51 };
52 error_t(level_t lvl, const std::string& loc, const std::string& msg)
53 :m_level(lvl), m_loc(loc), m_msg(msg) {}
54 level_t level() const { return m_level; }
55 std::string location() const { return m_loc; }
56 std::string message() const { return m_msg; }
57protected:
58 level_t m_level;
59 std::string m_loc, m_msg;
60};
61
62/** Error context to log errors */
63class error_context_t
64{
65public:
66 void add(const error_t& err) { m_list.push_back(err); }
67 size_t count() const { return m_list.size(); }
68 error_t get(size_t i) const { return m_list[i]; }
69protected:
70 std::vector< error_t > m_list;
71};
72
73/**
74 * Bare representation of the format
75 */
76
77/** Enumerated value (aka named value), represents a special value for a field */
78struct enum_t
79{
80 soc_id_t id; /** ID (must be unique among field enums) */
81 std::string name; /** Name (must be unique among field enums) */
82 std::string desc; /** Optional description of the meaning of this value */
83 soc_word_t value; /** Value of the field */
84};
85
86/** Register field information */
87struct field_t
88{
89 soc_id_t id; /** ID (must be unique among register fields) */
90 std::string name; /** Name (must be unique among register fields) */
91 std::string desc; /** Optional description of the field */
92 size_t pos; /** Position of the least significant bit */
93 size_t width; /** Width of the field in bits */
94 std::vector< enum_t > enum_; /** List of special values */
95
96 /** Returns the bit mask of the field within the register */
97 soc_word_t bitmask() const
98 {
99 // WARNING beware of the case where width is 32
100 if(width == 32)
101 return 0xffffffff;
102 else
103 return ((1 << width) - 1) << pos;
104 }
105
106 /** Extract field value from register value */
107 soc_word_t extract(soc_word_t reg_val) const
108 {
109 return (reg_val & bitmask()) >> pos;
110 }
111
112 /** Replace the field value in a register value */
113 soc_word_t replace(soc_word_t reg_val, soc_word_t field_val) const
114 {
115 return (reg_val & ~bitmask()) | ((field_val << pos) & bitmask());
116 }
117
118 /** Return field value index, or -1 if none */
119 int find_value(soc_word_t v) const
120 {
121 for(size_t i = 0; i < enum_.size(); i++)
122 if(enum_[i].value == v)
123 return i;
124 return -1;
125 }
126};
127
128/** Register information */
129struct register_t
130{
131 size_t width; /** Size in bits */
132 std::vector< field_t > field; /** List of fields */
133};
134
135/** Node address range information */
136struct range_t
137{
138 enum type_t
139 {
140 STRIDE, /** Addresses are given by a base address and a stride */
141 FORMULA /** Addresses are given by a formula */
142 };
143
144 type_t type; /** Range type */
145 size_t first; /** First index in the range */
146 size_t count; /** Number of indexes in the range */
147 soc_word_t base; /** Base address (for STRIDE) */
148 soc_word_t stride; /** Stride value (for STRIDE) */
149 std::string formula; /** Formula (for FORMULA) */
150 std::string variable; /** Formula variable name (for FORMULA) */
151};
152
153/** Node instance information */
154struct instance_t
155{
156 enum type_t
157 {
158 SINGLE, /** There is a single instance at a specified address */
159 RANGE /** There are multiple addresses forming a range */
160 };
161
162 soc_id_t id; /** ID (must be unique among node instances) */
163 std::string name; /** Name (must be unique among node instances) */
164 std::string title; /** Optional instance human name */
165 std::string desc; /** Optional description of the instance */
166 type_t type; /** Instance type */
167 soc_word_t addr; /** Address (for SINGLE) */
168 range_t range; /** Range (for RANGE) */
169};
170
171/** Node information */
172struct node_t
173{
174 soc_id_t id; /** ID (must be unique among nodes) */
175 std::string name; /** Name (must be unique for the among nodes) */
176 std::string title; /** Optional node human name */
177 std::string desc; /** Optional description of the node */
178 std::vector< register_t> register_; /** Optional register */
179 std::vector< instance_t> instance; /** List of instances */
180 std::vector< node_t > node; /** List of sub-nodes */
181};
182
183/** System-on-chip information */
184struct soc_t
185{
186 std::string name; /** Codename of the SoC */
187 std::string title; /** Human name of the SoC */
188 std::string desc; /** Optional description of the SoC */
189 std::string isa; /** Instruction Set Assembly */
190 std::string version; /** Description version */
191 std::vector< std::string > author; /** List of authors of the description */
192 std::vector< node_t > node; /** List of nodes */
193};
194
195/** Parse a SoC description from a XML file, put it into <soc>. */
196bool parse_xml(const std::string& filename, soc_t& soc, error_context_t& error_ctx);
197/** Write a SoC description to a XML file, overwriting it. A file can contain
198 * multiple Soc descriptions */
199bool produce_xml(const std::string& filename, const soc_t& soc, error_context_t& error_ctx);
200/** Formula parser: try to parse and evaluate a formula with some variables */
201bool evaluate_formula(const std::string& formula,
202 const std::map< std::string, soc_word_t>& var, soc_word_t& result,
203 const std::string& loc, error_context_t& error_ctx);
204
205/**
206 * Convenience API to manipulate the format
207 *
208 * The idea is that *_ref_t objects are stable pointers: they stay valid even
209 * when the underlying soc changes. In particular:
210 * - modifying any structure data (except id fields) preserves all references
211 * - removing a structure invalidates all references pointing to this structure
212 * and its children
213 * - adding any structure preserves all references
214 * These references can be used to get pointers to the actual data
215 * of the representation when it needs to be read or write.
216 */
217
218class soc_ref_t;
219class node_ref_t;
220class register_ref_t;
221class field_ref_t;
222class node_inst_t;
223
224/** SoC reference */
225class soc_ref_t
226{
227 soc_t *m_soc; /* pointer to the soc */
228public:
229 /** Builds an invalid reference */
230 soc_ref_t();
231 /** Builds a reference to a soc */
232 soc_ref_t(soc_t *soc);
233 /** Checks whether this reference is valid */
234 bool valid() const;
235 /** Returns a pointer to the soc */
236 soc_t *get() const;
237 /** Returns a reference to the root node */
238 node_ref_t root() const;
239 /** Returns a reference to the root node instance */
240 node_inst_t root_inst() const;
241 /** Compare this reference to another */
242 bool operator==(const soc_ref_t& r) const;
243 inline bool operator!=(const soc_ref_t& r) const { return !operator==(r); }
244};
245
246/** SoC node reference
247 * NOTE: the root soc node is presented as a node with empty path */
248class node_ref_t
249{
250 friend class soc_ref_t;
251 friend class node_inst_t;
252 soc_ref_t m_soc; /* reference to the soc */
253 std::vector< soc_id_t > m_path; /* path from the root */
254
255 node_ref_t(soc_ref_t soc);
256 node_ref_t(soc_ref_t soc, const std::vector< soc_id_t >& path);
257public:
258 /** Builds an invalid reference */
259 node_ref_t();
260 /** Check whether this reference is valid */
261 bool valid() const;
262 /** Check whether this reference is the root node */
263 bool is_root() const;
264 /** Returns a pointer to the node, or 0 if invalid or root */
265 node_t *get() const;
266 /** Returns a reference to the soc */
267 soc_ref_t soc() const;
268 /** Returns a reference to the parent node */
269 node_ref_t parent() const;
270 /** Returns a reference to the register (which may be on a parent node) */
271 register_ref_t reg() const;
272 /** Returns a list of references to the sub-nodes */
273 std::vector< node_ref_t > children() const;
274 /** Returns a reference to a specific child */
275 node_ref_t child(const std::string& name) const;
276 /** Returns the path of the node, as the list of node names from the root */
277 std::vector< std::string > path() const;
278 /** Returns the name of the node */
279 std::string name() const;
280 /** Compare this reference to another */
281 bool operator==(const node_ref_t& r) const;
282 inline bool operator!=(const node_ref_t& r) const { return !operator==(r); }
283};
284
285/** SoC register reference */
286class register_ref_t
287{
288 friend class node_ref_t;
289 node_ref_t m_node; /* reference to the node owning the register */
290
291 register_ref_t(node_ref_t node);
292public:
293 /** Builds an invalid reference */
294 register_ref_t();
295 /** Check whether this reference is valid/exists */
296 bool valid() const;
297 /** Returns a pointer to the register, or 0 */
298 register_t *get() const;
299 /** Returns a reference to the node containing the register */
300 node_ref_t node() const;
301 /** Returns a list of references to the fields of the register */
302 std::vector< field_ref_t > fields() const;
303 /** Returns a reference to a particular field */
304 field_ref_t field(const std::string& name) const;
305 /** Compare this reference to another */
306 bool operator==(const register_ref_t& r) const;
307 inline bool operator!=(const register_ref_t& r) const { return !operator==(r); }
308};
309
310/** SoC register field reference */
311class field_ref_t
312{
313 friend class register_ref_t;
314 register_ref_t m_reg; /* reference to the register */
315 soc_id_t m_id; /* field name */
316
317 field_ref_t(register_ref_t reg, soc_id_t id);
318public:
319 /** Builds an invalid reference */
320 field_ref_t();
321 /** Check whether this reference is valid/exists */
322 bool valid() const;
323 /** Returns a pointer to the field, or 0 */
324 field_t *get() const;
325 /** Returns a reference to the register containing the field */
326 register_ref_t reg() const;
327 /** Compare this reference to another */
328 bool operator==(const field_ref_t& r) const;
329 inline bool operator!=(const field_ref_t& r) const { return !operator==(r); }
330};
331
332/** SoC node instance
333 * NOTE: the root soc node is presented as a node with a single instance at 0 */
334class node_inst_t
335{
336 friend class node_ref_t;
337 friend class soc_ref_t;
338 node_ref_t m_node; /* reference to the node */
339 std::vector< soc_id_t > m_id_path; /* list of instance IDs */
340 std::vector< size_t > m_index_path; /* list of instance indexes */
341
342 node_inst_t(soc_ref_t soc);
343 node_inst_t(node_ref_t soc, const std::vector< soc_id_t >& path,
344 const std::vector< size_t >& indexes);
345public:
346 /** Builds an invalid reference */
347 node_inst_t();
348 /** Check whether this instance is valid/exists */
349 bool valid() const;
350 /** Returns a reference to the soc */
351 soc_ref_t soc() const;
352 /** Returns a reference to the node */
353 node_ref_t node() const;
354 /** Check whether this reference is the root node instance */
355 bool is_root() const;
356 /** Returns a reference to the parent instance */
357 node_inst_t parent() const;
358 /** Returns a pointer to the instance of the node, or 0 */
359 instance_t *get() const;
360 /** Returns the address of this instance */
361 soc_addr_t addr() const;
362 /** Returns an instance to a child of this node's instance. If the subnode
363 * instance is a range, the returned reference is invalid */
364 node_inst_t child(const std::string& name) const;
365 /** Returns an instance to a child of this node's instance with a range index.
366 * If the subnode is not not a range or if the index is out of bounds,
367 * the returned reference is invalid */
368 node_inst_t child(const std::string& name, size_t index) const;
369 /** Returns a list of all instances of subnodes of this node's instance */
370 std::vector< node_inst_t > children() const;
371 /** Returns the name of the instance */
372 std::string name() const;
373 /** Checks whether this instance is indexed */
374 bool is_indexed() const;
375 /** Returns the index of the instance */
376 size_t index() const;
377 /** Compare this reference to another */
378 bool operator==(const node_inst_t& r) const;
379 inline bool operator!=(const node_inst_t& r) const { return !operator==(r); }
380};
381
382} // soc_desc
383
384#endif /* __SOC_DESC__ */