Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions riscv/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern device_factory_t* ns16550_factory;
sim_t::sim_t(const cfg_t *cfg, bool halted,
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
const std::vector<device_factory_sargs_t>& plugin_device_factories,
const bool dtb_discovery,
const std::vector<std::string>& args,
const debug_module_config_t &dm_config,
const char *log_path,
Expand All @@ -49,6 +50,7 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
: htif_t(args),
cfg(cfg),
mems(mems),
dtb_discovery(dtb_discovery),
dtb_enabled(dtb_enabled),
log_file(log_path),
cmd_file(cmd_file),
Expand Down Expand Up @@ -242,6 +244,48 @@ sim_t::sim_t(const cfg_t *cfg, bool halted,
cpu_idx++;
}


std::vector<device_factory_sargs_t> plugin_device_auto_discovery_factories;
if (dtb_discovery)
{
// Iterate through all devices in the map
for (const auto &pair : mmio_device_map())
{
const std::string &device_name = pair.first;
int offset = -1;
// Find all instances of this device type
while ((offset = fdt_node_offset_by_compatible(fdt, offset, device_name.c_str())) >= 0)
{
// Get node name
const char *node_name = fdt_get_name(fdt, offset, NULL);

// Get params from spike_plugin_params dts field.
int spike_plugin_params_len;
const char* spike_plugin_params=(char*) fdt_getprop(fdt, offset, "spike,plugin-params", &spike_plugin_params_len); //todo: remove hard-coded "spike_plugin_params"

std::cout << "DTB discovered device: " << node_name << " - compatible with device type:" << device_name <<" - params: "<<spike_plugin_params<< std::endl;

//Extract params in the comma-separated format
const std::string device_args(spike_plugin_params);
std::vector<std::string> parsed_args;
std::stringstream sstr(device_args);
while (sstr.good())
{
std::string substr;
getline(sstr, substr, ',');
parsed_args.push_back(substr);
}

//Add device
plugin_device_auto_discovery_factories.push_back(std::make_pair(pair.second, parsed_args));
}
}
}

device_factories.insert(device_factories.end(),
plugin_device_auto_discovery_factories.begin(),
plugin_device_auto_discovery_factories.end());

// must be located after procs/harts are set (devices might use sim_t get_* member functions)
for (size_t i = 0; i < device_factories.size(); i++) {
const device_factory_t* factory = device_factories[i].first;
Expand Down
2 changes: 2 additions & 0 deletions riscv/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class sim_t : public htif_t, public simif_t
sim_t(const cfg_t *cfg, bool halted,
std::vector<std::pair<reg_t, abstract_mem_t*>> mems,
const std::vector<device_factory_sargs_t>& plugin_device_factories,
const bool dtb_discovery,
const std::vector<std::string>& args,
const debug_module_config_t &dm_config, const char *log_path,
bool dtb_enabled, const char *dtb_file,
Expand Down Expand Up @@ -78,6 +79,7 @@ class sim_t : public htif_t, public simif_t
std::pair<reg_t, reg_t> initrd_range;
std::string dts;
std::string dtb;
const bool dtb_discovery;
bool dtb_enabled;
std::vector<std::shared_ptr<abstract_device_t>> devices;
std::shared_ptr<clint_t> clint;
Expand Down
15 changes: 14 additions & 1 deletion spike_main/spike.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static void help(int exit_code = 1)
fprintf(stderr, " --misaligned Support misaligned memory accesses\n");
fprintf(stderr, " --device=<name> Attach MMIO plugin device from an --extlib library,\n");
fprintf(stderr, " specify --device=<name>,<args> to pass down extra args.\n");
fprintf(stderr, " --dtb-discovery Enable direct device discovery from device tree blob. Requires --dtb and usage of special \"spike_plugin_params\" dts field.\n");
fprintf(stderr, " --log-cache-miss Generate a log of cache miss\n");
fprintf(stderr, " --log-commits Generate a log of commits info\n");
fprintf(stderr, " --extension=<name> Specify RoCC Extension\n");
Expand Down Expand Up @@ -325,6 +326,7 @@ int main(int argc, char** argv)
bool UNUSED socket = false; // command line option -s
bool dump_dts = false;
bool dtb_enabled = true;
bool dtb_discovery = false;
const char* kernel = NULL;
reg_t kernel_offset, kernel_size;
std::vector<device_factory_sargs_t> plugin_device_factories;
Expand Down Expand Up @@ -397,6 +399,7 @@ int main(int argc, char** argv)
parser.option(0, "pmpgranularity", 1, [&](const char* s){cfg.pmpgranularity = atoul_safe(s);});
parser.option(0, "priv", 1, [&](const char* s){cfg.priv = s;});
parser.option(0, "device", 1, device_parser);
parser.option(0, "dtb-discovery", 0, [&](const char UNUSED *s){ dtb_discovery = true;} );
parser.option(0, "extension", 1, [&](const char* s){extensions.push_back(find_extension(s));});
parser.option(0, "dump-dts", 0, [&](const char UNUSED *s){dump_dts = true;});
parser.option(0, "disable-dtb", 0, [&](const char UNUSED *s){dtb_enabled = false;});
Expand Down Expand Up @@ -520,8 +523,18 @@ int main(int argc, char** argv)
cfg.hartids = default_hartids;
}

if (dtb_discovery && !plugin_device_factories.empty()) {
std::cerr << "--dtb-discovery option is not compatible with --device option.\n";
exit(1);
}

if (dtb_discovery && dtb_file==NULL) {
std::cerr << "--dtb-discovery option required a dtb_file. Use --dtb option.\n";
exit(1);
}

sim_t s(&cfg, halted,
mems, plugin_device_factories, htif_args, dm_config, log_path, dtb_enabled, dtb_file,
mems, plugin_device_factories, dtb_discovery, htif_args, dm_config, log_path, dtb_enabled, dtb_file,
socket,
cmd_file,
instructions);
Expand Down