forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreConfig.cpp
More file actions
654 lines (559 loc) · 16.7 KB
/
CoreConfig.cpp
File metadata and controls
654 lines (559 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include "CoreConfig.hpp"
#include "Core.hpp"
using namespace WdRiscv;
CoreConfig::CoreConfig()
{
config_ = new nlohmann::json();
}
CoreConfig::~CoreConfig()
{
delete config_;
config_ = nullptr;
}
bool
CoreConfig::loadConfigFile(const std::string& filePath)
{
std::ifstream ifs(filePath);
if (not ifs.good())
{
std::cerr << "Failed to open config file '" << filePath
<< "' for input.\n";
return false;
}
try
{
ifs >> *config_;
}
catch (std::exception& e)
{
std::cerr << e.what() << "\n";
return false;
}
catch (...)
{
std::cerr << "Caught unknown exception while parsing "
<< " config file '" << filePath << "'\n";
return false;
}
return true;
}
namespace WdRiscv
{
/// Convert given json value to an unsigned integer honoring
/// hexadecimal prefix (0x) if any.
template <typename URV>
URV
getJsonUnsigned(const std::string& tag, const nlohmann::json& js)
{
if (js.is_number())
return js.get<unsigned>();
if (js.is_string())
{
char *end = nullptr;
std::string str = js.get<std::string>();
URV x = 0;
if constexpr (sizeof(URV) == 4)
x = strtoul(str.c_str(), &end, 0);
else
x = strtoull(str.c_str(), &end, 0);
if (end and *end)
std::cerr << "Invalid config file value for '" << tag << "': "
<< str << '\n';
return x;
}
std::cerr << "Config file entry '" << tag << "' must contain a number\n";
return 0;
}
/// Convert given json array value to an vector of unsigned integers
/// honoring any hexadecimal prefix (0x) if any.
std::vector<uint64_t>
getJsonUnsignedVec(const std::string& tag, const nlohmann::json& js)
{
std::vector<uint64_t> vec;
if (not js.is_array())
{
std::cerr << "Invalid config file value for '" << tag << "'"
<< " -- expecting array of numbers\n";
return vec;
}
for (const auto& item :js)
{
if (item.is_number())
vec.push_back(item.get<unsigned>());
else if (item.is_string())
{
char *end = nullptr;
std::string str = item.get<std::string>();
uint64_t x = strtoull(str.c_str(), &end, 0);
if (end and *end)
std::cerr << "Invalid config file value for '" << tag << "': "
<< str << '\n';
else
vec.push_back(x);
}
else
std::cerr << "Invalid config file value for '" << tag << "'"
<< " -- expecting array of number\n";
}
return vec;
}
/// Convert given json value to a boolean.
bool
getJsonBoolean(const std::string& tag, const nlohmann::json& js)
{
if (js.is_boolean())
return js.get<bool>();
if (js.is_number())
return js.get<unsigned>();
if (js.is_string())
{
std::string str = js.get<std::string>();
if (str == "0" or str == "false" or str == "False")
return false;
if (str == "1" or str == "true" or str == "True")
return true;
std::cerr << "Invalid config file value for '" << tag << "': "
<< str << '\n';
return false;
}
std::cerr << "Config file entry '" << tag << "' must contain a bool\n";
return false;
}
}
template <typename URV>
static
bool
applyCsrConfig(Core<URV>& core, const nlohmann::json& config, bool verbose)
{
if (not config.count("csr"))
return true; // Nothing to apply
const auto& csrs = config.at("csr");
if (not csrs.is_object())
{
std::cerr << "Invalid csr entry in config file (expecting an object)\n";
return false;
}
unsigned errors = 0;
for (auto it = csrs.begin(); it != csrs.end(); ++it)
{
const std::string& csrName = it.key();
const auto& conf = it.value();
URV reset = 0, mask = 0, pokeMask = 0;
bool isDebug = false, exists = true;
const Csr<URV>* csr = core.findCsr(csrName);
if (csr)
{
reset = csr->getResetValue();
mask = csr->getWriteMask();
pokeMask = csr->getPokeMask();
isDebug = csr->isDebug();
}
if (conf.count("reset"))
reset = getJsonUnsigned<URV>(csrName + ".reset", conf.at("reset"));
if (conf.count("mask"))
{
mask = getJsonUnsigned<URV>(csrName + ".mask", conf.at("mask"));
// If defining a non-standard CSR (as popposed to
// configuring an existing CSR) then default the poke-mask
// to the write-mask.
if (not csr)
pokeMask = mask;
}
if (conf.count("poke_mask"))
pokeMask = getJsonUnsigned<URV>(csrName + ".poke_mask",
conf.at("poke_mask"));
if (conf.count("debug"))
isDebug = getJsonBoolean(csrName + ".bool", conf.at("debug"));
if (conf.count("exists"))
exists = getJsonBoolean(csrName + ".bool", conf.at("exists"));
// If number present and csr is not defined, then define a new
// CSR; otherwise, configure.
if (conf.count("number"))
{
unsigned number = getJsonUnsigned<URV>(csrName + ".number",
conf.at("number"));
if (csr)
{
if (csr->getNumber() != CsrNumber(number))
{
std::cerr << "Invalid config file entry for CSR "
<< csrName << ": Number (0x" << std::hex << number
<< ") does not match that of previous definition ("
<< "0x" << std::hex << unsigned(csr->getNumber())
<< ")\n";
errors++;
continue;
}
// If number matches we configure below
}
else if (core.defineCsr(csrName, CsrNumber(number), exists,
reset, mask, pokeMask, isDebug))
{
csr = core.findCsr(csrName);
assert(csr);
}
else
{
std::cerr << "Invalid config file CSR definition with name "
<< csrName << " and number 0x" << std::hex << number
<< ": Number already in use\n";
errors++;
continue;
}
}
bool exists0 = csr->isImplemented(), isDebug0 = csr->isDebug();
URV reset0 = csr->getResetValue(), mask0 = csr->getWriteMask();
URV pokeMask0 = csr->getPokeMask();
if (not core.configCsr(csrName, exists, reset, mask, pokeMask,
isDebug))
{
std::cerr << "Invalid CSR (" << csrName << ") in config file.\n";
errors++;
}
else if (verbose)
{
if (exists0 != exists or isDebug0 != isDebug or reset0 != reset or
mask0 != mask or pokeMask0 != pokeMask)
{
std::cerr << "Configuration of CSR (" << csrName <<
") changed in config file:\n";
if (exists0 != exists)
std::cerr << " implemented: " << exists0 << " to "
<< exists << '\n';
if (isDebug0 != isDebug)
std::cerr << " debug: " << isDebug0 << " to "
<< isDebug << '\n';
if (reset0 != reset)
std::cerr << " reset: 0x" << std::hex << reset0
<< " to 0x" << std::hex << reset << '\n';
if (mask0 != mask)
std::cerr << " mask: 0x" << std::hex << mask0
<< " to 0x" << std::hex << mask << '\n';
if (pokeMask0 != pokeMask)
std::cerr << " poke_mask: " << std::hex << pokeMask0
<< " to 0x" << pokeMask << '\n';
}
}
}
return errors == 0;
}
template <typename URV>
static
bool
applyPicConfig(Core<URV>& core, const nlohmann::json& config)
{
if (not config.count("pic"))
return true; // Nothing to apply.
const auto& pic = config.at("pic");
for (const auto& tag : { "region", "size", "offset", "mpiccfg_offset",
"meipl_offset", "meip_offset", "meie_offset", "meigwctrl_offset",
"meigwclr_offset", "total_int", "int_words" } )
{
if (not pic.count(tag))
{
std::cerr << "Missing '" << tag << "' entry in "
<< "config file PIC section\n";
}
}
// Define pic region.
uint64_t region = getJsonUnsigned<URV>("region", pic.at("region"));
uint64_t size = getJsonUnsigned<URV>("size", pic.at("size"));
uint64_t regionOffset = getJsonUnsigned<URV>("offset", pic.at("offset"));
if (not core.defineMemoryMappedRegisterRegion(region, regionOffset, size))
return false;
// Define the memory mapped registers.
uint64_t smax = getJsonUnsigned<URV>("pic.total_int", pic.at("total_int"));
uint64_t xmax = getJsonUnsigned<URV>("pic.int_words", pic.at("int_words"));
unsigned errors = 0;
// Start by giving all registers in region a mask of zero.
size_t possibleRegCount = size / 4;
for (size_t ix = 0; ix < possibleRegCount; ++ix)
core.defineMemoryMappedRegisterWriteMask(region, regionOffset, 0, ix, 0);
std::vector<std::string> names = { "mpiccfg_offset", "meipl_offset",
"meip_offset", "meie_offset",
"meigwctrl_offset", "meigwclr_offset" };
// These should be in the config file. The mask for meigwclr is zero
// because the state is always zero.
std::vector<uint32_t> masks = { 1, 0xf, 0, 1, 3, 0 };
std::vector<size_t> counts = { 1, smax, xmax, smax, smax, smax };
// meipl, meie, meigwctrl and meigwclr indexing start at 1 (instead
// of 0): adjust
std::vector<size_t> adjust = { 0, 4, 0, 4, 4, 4 };
for (size_t i = 0; i < names.size(); ++i)
{
auto mask = masks.at(i);
const auto& name = names.at(i);
auto count = counts.at(i);
if (not pic.count(name))
continue; // Should be an error.
uint64_t registerOffset = getJsonUnsigned<URV>(("pic." + name),
pic.at(name));
registerOffset += adjust.at(i);
for (size_t regIx = 0; regIx < count; ++regIx)
if (not core.defineMemoryMappedRegisterWriteMask(region, regionOffset,
registerOffset, regIx,
mask))
errors++;
}
return errors == 0;
}
template <typename URV>
static
bool
applyTriggerConfig(Core<URV>& core, const nlohmann::json& config)
{
if (not config.count("triggers"))
return true; // Nothing to apply
const auto& triggers = config.at("triggers");
if (not triggers.is_array())
{
std::cerr << "Invalid triggers entry in config file (expecting an array)\n";
return false;
}
unsigned errors = 0;
unsigned ix = 0;
for (auto it = triggers.begin(); it != triggers.end(); ++it, ++ix)
{
const auto& trig = *it;
std::string name = std::string("trigger") + std::to_string(ix);
if (not trig.is_object())
{
std::cerr << "Invalid trigger in config file triggers array "
<< "(expecting an object at index " << std::dec << ix << ")\n";
++errors;
break;
}
bool ok = true;
for (const auto& tag : {"reset", "mask", "poke_mask"})
if (not trig.count(tag))
{
std::cerr << "Trigger " << name << " has no '" << tag
<< "' entry in config file\n";
ok = false;
}
if (not ok)
{
errors++;
continue;
}
auto resets = getJsonUnsignedVec(name + ".reset", trig.at("reset"));
auto masks = getJsonUnsignedVec(name + ".mask", trig.at("mask"));
auto pokeMasks = getJsonUnsignedVec(name + ".poke_mask", trig.at("poke_mask"));
if (resets.size() != 3)
{
std::cerr << "Trigger " << name << ": Bad item count (" << resets.size()
<< ") for 'reset' field in config file. Expecting 3.\n";
ok = false;
}
if (masks.size() != 3)
{
std::cerr << "Trigger " << name << ": Bad item count (" << masks.size()
<< ") for 'mask' field in config file. Expecting 3.\n";
ok = false;
}
if (pokeMasks.size() != 3)
{
std::cerr << "Trigger " << name << ": Bad item count (" << pokeMasks.size()
<< ") for 'poke_mask' field in config file. Expecting 3.\n";
ok = false;
}
if (not ok)
{
errors++;
continue;
}
if (not core.configTrigger(ix, resets.at(0), resets.at(1), resets.at(2),
masks.at(0), masks.at(1), masks.at(2),
pokeMasks.at(0), pokeMasks.at(1), pokeMasks.at(2)))
{
std::cerr << "Failed to configure trigger " << std::dec << ix << '\n';
++errors;
}
}
return errors == 0;
}
template<typename URV>
bool
CoreConfig::applyConfig(Core<URV>& core, bool verbose) const
{
// Define PC value after reset.
std::string tag = "reset_vec";
if (config_ -> count(tag))
{
URV resetPc = getJsonUnsigned<URV>(tag, config_ -> at(tag));
core.defineResetPc(resetPc);
}
// Define non-maskable-interrupt pc
tag = "nmi_vec";
if (config_ -> count(tag))
{
URV nmiPc = getJsonUnsigned<URV>(tag, config_ -> at(tag));
core.defineNmiPc(nmiPc);
}
// Use ABI register names (e.g. sp instead of x2).
tag = "abi_names";
if (config_ -> count(tag))
{
bool abiNames = getJsonBoolean(tag, config_ ->at(tag));
core.enableAbiNames(abiNames);
}
// Enable debug triggers.
tag ="enable_triggers";
if (config_ -> count(tag))
{
bool et = getJsonBoolean(tag, config_ ->at(tag));
core.enableTriggers(et);
}
// Enable performance counters.
tag ="enable_performance_counters";
if (config_ -> count(tag))
{
bool epc = getJsonBoolean(tag, config_ ->at(tag));
core.enablePerformanceCounters(epc);
}
// Enable rollback of memory on store error.
tag = "store_error_rollback";
if (config_ -> count(tag))
{
bool ser = getJsonBoolean(tag, config_ -> at(tag));
core.enableStoreErrorRollback(ser);
}
// Enable rollback of register on load error.
tag = "load_error_rollback";
if (config_ -> count(tag))
{
bool ler = getJsonBoolean(tag, config_ -> at(tag));
core.enableLoadErrorRollback(ler);
}
tag = "load_queue_size";
if (config_ -> count(tag))
{
unsigned lqs = getJsonUnsigned<URV>(tag, config_ -> at(tag));
if (lqs > 64)
{
std::cerr << "Config file load queue size (" << lqs << ") too large"
<< " -- using 64.\n";
lqs = 64;
}
core.setLoadQueueSize(lqs);
}
if (config_ -> count("memmap"))
{
const auto& memmap = config_ -> at("memmap");
tag = "consoleio";
if (memmap.count(tag))
{
URV io = getJsonUnsigned<URV>("memmap.consoleio", memmap.at(tag));
core.setConsoleIo(io);
}
}
tag = "even_odd_trigger_chains";
if (config_ -> count(tag))
{
bool chainPairs = getJsonBoolean(tag, config_ -> at(tag));
core.configEvenOddTriggerChaining(chainPairs);
}
unsigned errors = 0;
if (config_ -> count("iccm"))
{
const auto& iccm = config_ -> at("iccm");
if (iccm.count("region") and iccm.count("size") and iccm.count("offset"))
{
size_t region = getJsonUnsigned<URV>("iccm.region", iccm.at("region"));
size_t size = getJsonUnsigned<URV>("iccm.size", iccm.at("size"));
size_t offset = getJsonUnsigned<URV>("iccm.offset", iccm.at("offset"));
if (not core.defineIccm(region, offset, size))
errors++;
}
else
{
std::cerr << "The ICCM entry in the configuration file must contain "
<< "a region, offset and a size entry.\n";
errors++;
}
}
if (config_ -> count("dccm"))
{
const auto& dccm = config_ -> at("dccm");
if (dccm.count("region") and dccm.count("size") and dccm.count("offset"))
{
size_t region = getJsonUnsigned<URV>("dccm.region", dccm.at("region"));
size_t size = getJsonUnsigned<URV>("dccm.size", dccm.at("size"));
size_t offset = getJsonUnsigned<URV>("dccm.offset", dccm.at("offset"));
if (not core.defineDccm(region, offset, size))
errors++;
}
else
{
std::cerr << "The DCCM entry in the configuration file must contain "
<< "a region, offset and a size entry.\n";
errors++;
}
}
tag = "num_mmode_perf_regs";
if (config_ -> count(tag))
{
unsigned count = getJsonUnsigned<URV>(tag, config_ -> at(tag));
if (not core.configMachineModePerfCounters(count))
errors++;
}
tag = "max_mmode_perf_event";
if (config_ -> count(tag))
{
unsigned maxId = getJsonUnsigned<URV>(tag, config_ -> at(tag));
core.configMachineModeMaxPerfEvent(maxId);
}
if (not applyCsrConfig(core, *config_, verbose))
errors++;
if (not applyPicConfig(core, *config_))
errors++;
if (not applyTriggerConfig(core, *config_))
errors++;
core.finishMemoryConfig();
return errors == 0;
}
bool
CoreConfig::getXlen(unsigned& xlen) const
{
if (config_ -> count("xlen"))
{
xlen = getJsonUnsigned<uint32_t>("xlen", config_ -> at("xlen"));
return true;
}
return false;
}
void
CoreConfig::clear()
{
config_ -> clear();
}
bool
CoreConfig::apply(CoreConfig& conf, Core<uint32_t>& core, bool verbose)
{
return conf.applyConfig(core, verbose);
}
bool
CoreConfig::apply(CoreConfig& conf, Core<uint64_t>& core, bool verbose)
{
return conf.applyConfig(core, verbose);
}