Skip to content

Commit

Permalink
fdt: restructure dtb create and config flow
Browse files Browse the repository at this point in the history
1. pass dtb option from constructor
2. separate dtb generation from rom initialization
3. setup clint base from dtb

Signed-off-by: Chih-Min Chao <[email protected]>
  • Loading branch information
chihminchao committed Apr 28, 2020
1 parent 65f42ac commit 3b26740
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
53 changes: 45 additions & 8 deletions riscv/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,25 @@ sim_t::sim_t(const char* isa, const char* priv, const char* varch,
const std::vector<std::string>& args,
std::vector<int> const hartids,
const debug_module_config_t &dm_config,
const char *log_path)
: htif_t(args), mems(mems), plugin_devices(plugin_devices),
const char *log_path,
bool dtb_enabled, const char *dtb_file)
: htif_t(args),
mems(mems),
plugin_devices(plugin_devices),
procs(std::max(nprocs, size_t(1))),
initrd_start(initrd_start), initrd_end(initrd_end), start_pc(start_pc),
initrd_start(initrd_start),
initrd_end(initrd_end),
start_pc(start_pc),
dtb_file(dtb_file ? dtb_file : ""),
dtb_enabled(dtb_enabled),
log_file(log_path),
current_step(0), current_proc(0), debug(false), histogram_enabled(false),
log(false), dtb_enabled(true),
remote_bitbang(NULL), debug_module(this, dm_config)
current_step(0),
current_proc(0),
debug(false),
histogram_enabled(false),
log(false),
remote_bitbang(NULL),
debug_module(this, dm_config)
{
signal(SIGINT, &handle_signal);

Expand Down Expand Up @@ -69,8 +80,15 @@ sim_t::sim_t(const char* isa, const char* priv, const char* varch,
log_file.get());
}

make_dtb();

clint.reset(new clint_t(procs, CPU_HZ / INSNS_PER_RTC_TICK, real_time_clint));
bus.add_device(CLINT_BASE, clint.get());
reg_t clint_base;
if (fdt_parse_clint((void *)dtb.c_str(), &clint_base, "riscv,clint0")) {
bus.add_device(CLINT_BASE, clint.get());
} else {
bus.add_device(clint_base, clint.get());
}
}

sim_t::~sim_t()
Expand Down Expand Up @@ -190,6 +208,25 @@ bool sim_t::mmio_store(reg_t addr, size_t len, const uint8_t* bytes)
}

void sim_t::make_dtb()
{
if (!dtb_file.empty()) {
std::ifstream fin(dtb_file.c_str(), std::ios::binary);
if (!fin.good()) {
std::cerr << "can't find dtb file: " << dtb_file << std::endl;
exit(-1);
}

std::stringstream strstream;
strstream << fin.rdbuf();

dtb = strstream.str();
} else {
dts = make_dts(INSNS_PER_RTC_TICK, CPU_HZ, initrd_start, initrd_end, procs, mems);
dtb = dts_compile(dts);
}
}

void sim_t::set_rom()
{
const int reset_vec_size = 8;

Expand Down Expand Up @@ -252,7 +289,7 @@ char* sim_t::addr_to_mem(reg_t addr) {
void sim_t::reset()
{
if (dtb_enabled)
make_dtb();
set_rom();
}

void sim_t::idle()
Expand Down
14 changes: 5 additions & 9 deletions riscv/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class sim_t : public htif_t, public simif_t
reg_t start_pc, std::vector<std::pair<reg_t, mem_t*>> mems,
std::vector<std::pair<reg_t, abstract_device_t*>> plugin_devices,
const std::vector<std::string>& args, const std::vector<int> hartids,
const debug_module_config_t &dm_config, const char *log_path);
const debug_module_config_t &dm_config, const char *log_path,
bool dtb_enabled, const char *dtb_file);
~sim_t();

// run the simulation to completion
Expand All @@ -46,13 +47,6 @@ class sim_t : public htif_t, public simif_t
void configure_log(bool enable_log, bool enable_commitlog);

void set_procs_debug(bool value);
void set_dtb_enabled(bool value) {
this->dtb_enabled = value;
}
void set_dtb_file(const char* value) {
if (value)
this->dtb_file = value;
}
void set_remote_bitbang(remote_bitbang_t* remote_bitbang) {
this->remote_bitbang = remote_bitbang;
}
Expand All @@ -72,7 +66,9 @@ class sim_t : public htif_t, public simif_t
reg_t initrd_end;
reg_t start_pc;
std::string dts;
std::string dtb;
std::string dtb_file;
bool dtb_enabled;
std::unique_ptr<rom_device_t> boot_rom;
std::unique_ptr<clint_t> clint;
bus_t bus;
Expand All @@ -88,14 +84,14 @@ class sim_t : public htif_t, public simif_t
bool debug;
bool histogram_enabled; // provide a histogram of PCs
bool log;
bool dtb_enabled;
remote_bitbang_t* remote_bitbang;

// memory-mapped I/O routines
char* addr_to_mem(reg_t addr);
bool mmio_load(reg_t addr, size_t len, uint8_t* bytes);
bool mmio_store(reg_t addr, size_t len, const uint8_t* bytes);
void make_dtb();
void set_rom();

// presents a prompt for introspection into the simulation
void interactive();
Expand Down
4 changes: 1 addition & 3 deletions spike_main/spike.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,14 @@ int main(int argc, char** argv)

sim_t s(isa, priv, varch, nprocs, halted, real_time_clint,
initrd_start, initrd_end, start_pc, mems, plugin_devices, htif_args,
std::move(hartids), dm_config, log_path);
std::move(hartids), dm_config, log_path, dtb_enabled, dtb_file);
std::unique_ptr<remote_bitbang_t> remote_bitbang((remote_bitbang_t *) NULL);
std::unique_ptr<jtag_dtm_t> jtag_dtm(
new jtag_dtm_t(&s.debug_module, dmi_rti));
if (use_rbb) {
remote_bitbang.reset(new remote_bitbang_t(rbb_port, &(*jtag_dtm)));
s.set_remote_bitbang(&(*remote_bitbang));
}
s.set_dtb_enabled(dtb_enabled);
s.set_dtb_file(dtb_file);

if (dump_dts) {
printf("%s", s.get_dts());
Expand Down

0 comments on commit 3b26740

Please sign in to comment.