Skip to content

Commit

Permalink
feat!: add PLIC device
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jan 19, 2024
1 parent 88291f2 commit 3823c2f
Show file tree
Hide file tree
Showing 41 changed files with 922 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/grpc-interfaces
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ LIBCARTESI_OBJS:= \
pma-driver.o \
clint.o \
clint-factory.o \
plic.o \
plic-factory.o \
dtb.o \
os.o \
htif.o \
Expand Down
6 changes: 6 additions & 0 deletions src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,12 @@ local function store_machine_config(config, output)
output(" mtimecmp = 0x%x,", clint.mtimecmp or def.clint.mtimecmp)
comment_default(clint.mtimecmp, def.clint.mtimecmp)
output(" },\n")
local plic = config.plic or {}
output(" plic = {\n")
output(" girqpend = 0x%x,", plic.girqpend or def.plic.girqpend)
output(" girqsrvd = 0x%x,", plic.girqsrvd or def.plic.girqsrvd)
comment_default(plic.girqpend, def.plic.girqpend)
output(" },\n")
output(" flash_drive = {\n")
for _, f in ipairs(config.flash_drive) do
output(" ")
Expand Down
6 changes: 6 additions & 0 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ IMPL_MACHINE_OBJ_READ_WRITE(htif_ihalt)
IMPL_MACHINE_OBJ_READ_WRITE(htif_iconsole)
IMPL_MACHINE_OBJ_READ_WRITE(htif_iyield)
IMPL_MACHINE_OBJ_READ_WRITE(clint_mtimecmp)
IMPL_MACHINE_OBJ_READ_WRITE(plic_girqpend)
IMPL_MACHINE_OBJ_READ_WRITE(plic_girqsrvd)
IMPL_MACHINE_OBJ_READ_WRITE(uarch_cycle)
IMPL_MACHINE_OBJ_READ_WRITE(uarch_pc)

Expand Down Expand Up @@ -547,6 +549,8 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"get_initial_config", machine_obj_index_get_initial_config},
{"get_root_hash", machine_obj_index_get_root_hash},
{"read_clint_mtimecmp", machine_obj_index_read_clint_mtimecmp},
{"read_plic_girqpend", machine_obj_index_read_plic_girqpend},
{"read_plic_girqsrvd", machine_obj_index_read_plic_girqsrvd},
{"read_csr", machine_obj_index_read_csr},
{"read_htif_fromhost", machine_obj_index_read_htif_fromhost},
{"read_htif_tohost", machine_obj_index_read_htif_tohost},
Expand Down Expand Up @@ -609,6 +613,8 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"verify_dirty_page_maps", machine_obj_index_verify_dirty_page_maps},
{"verify_merkle_tree", machine_obj_index_verify_merkle_tree},
{"write_clint_mtimecmp", machine_obj_index_write_clint_mtimecmp},
{"write_plic_girqpend", machine_obj_index_write_plic_girqpend},
{"write_plic_girqsrvd", machine_obj_index_write_plic_girqsrvd},
{"write_csr", machine_obj_index_write_csr},
{"write_htif_fromhost", machine_obj_index_write_htif_fromhost},
{"write_htif_fromhost_data", machine_obj_index_write_htif_fromhost_data},
Expand Down
27 changes: 27 additions & 0 deletions src/clua-machine-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ CM_PROC_CSR clua_check_cm_proc_csr(lua_State *L, int idx) try {
{"ilrsc", CM_PROC_ILRSC},
{"iflags", CM_PROC_IFLAGS},
{"clint_mtimecmp", CM_PROC_CLINT_MTIMECMP},
{"plic_girqpend", CM_PROC_PLIC_GIRQPEND},
{"plic_girqsrvd", CM_PROC_PLIC_GIRQSRVD},
{"htif_tohost", CM_PROC_HTIF_TOHOST},
{"htif_fromhost", CM_PROC_HTIF_FROMHOST},
{"htif_ihalt", CM_PROC_HTIF_IHALT},
Expand Down Expand Up @@ -861,6 +863,15 @@ static void push_cm_clint_config(lua_State *L, const cm_clint_config *c) {
clua_setintegerfield(L, c->mtimecmp, "mtimecmp", -1);
}

/// \brief Pushes an cm_plic_config to the Lua stack
/// \param L Lua state.
/// \param c Plic configuration to be pushed.
static void push_cm_plic_config(lua_State *L, const cm_plic_config *c) {
lua_newtable(L);
clua_setintegerfield(L, c->girqpend, "girqpend", -1);
clua_setintegerfield(L, c->girqsrvd, "girqsrvd", -1);
}

/// \brief Pushes cm_memory_range_config to the Lua stack
/// \param L Lua state.
/// \param m Memory range config to be pushed.
Expand Down Expand Up @@ -949,6 +960,8 @@ void clua_push_cm_machine_config(lua_State *L, const cm_machine_config *c) {
lua_setfield(L, -2, "htif"); // config
push_cm_clint_config(L, &c->clint); // config clint
lua_setfield(L, -2, "clint"); // config
push_cm_plic_config(L, &c->plic); // config plic
lua_setfield(L, -2, "plic"); // config
push_cm_flash_drive_configs(L, &c->flash_drive); // config flash_drive
lua_setfield(L, -2, "flash_drive"); // config
push_cm_ram_config(L, &c->ram); // config ram
Expand Down Expand Up @@ -1175,6 +1188,19 @@ static void check_cm_clint_config(lua_State *L, int tabidx, cm_clint_config *c)
lua_pop(L, 1);
}

/// \brief Loads C api PLIC config from Lua
/// \param L Lua state
/// \param tabidx Config stack index
/// \param c PLIC config structure to receive results
static void check_cm_plic_config(lua_State *L, int tabidx, cm_plic_config *c) {
if (!opt_table_field(L, tabidx, "plic")) {
return;
}
c->girqpend = opt_uint_field(L, -1, "girqpend", c->girqpend);
c->girqsrvd = opt_uint_field(L, -1, "girqsrvd", c->girqsrvd);
lua_pop(L, 1);
}

cm_processor_config get_default_processor_config(lua_State *L) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast): remove const to adjust config
const auto *config = cm_new_default_machine_config();
Expand Down Expand Up @@ -1264,6 +1290,7 @@ cm_machine_config *clua_check_cm_machine_config(lua_State *L, int tabidx, int ct
check_cm_tlb_config(L, tabidx, &config->tlb);
check_cm_htif_config(L, tabidx, &config->htif);
check_cm_clint_config(L, tabidx, &config->clint);
check_cm_plic_config(L, tabidx, &config->plic);
check_cm_uarch_config(L, tabidx, &config->uarch);
check_cm_rollup_config(L, tabidx, &config->rollup);
check_cm_flash_drive_configs(L, tabidx, &config->flash_drive);
Expand Down
16 changes: 16 additions & 0 deletions src/device-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ class device_state_access : public i_device_state_access {
return m_a.write_clint_mtimecmp(val);
}

uint64_t do_read_plic_girqpend(void) override {
return m_a.read_plic_girqpend();
}

void do_write_plic_girqpend(uint64_t val) override {
return m_a.write_plic_girqpend(val);
}

uint64_t do_read_plic_girqsrvd(void) override {
return m_a.read_plic_girqsrvd();
}

void do_write_plic_girqsrvd(uint64_t val) override {
return m_a.write_plic_girqsrvd(val);
}

uint64_t do_read_htif_fromhost(void) override {
return m_a.read_htif_fromhost();
}
Expand Down
14 changes: 13 additions & 1 deletion src/dtb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static std::string misa_to_isa_string(uint64_t misa) {

void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_length) {
using namespace std::string_literals;
constexpr uint32_t INTC_PHANDLE = 1;
enum : uint32_t { INTC_PHANDLE = 1, PLIC_PHANDLE };
constexpr uint32_t X_HOST = 13;
constexpr uint32_t BOOTARGS_MAX_LEN = 4096;

Expand Down Expand Up @@ -122,6 +122,18 @@ void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_le
{INTC_PHANDLE, MIP_MSIP_SHIFT, INTC_PHANDLE, MIP_MTIP_SHIFT});
fdt.end_node();
}
{ // plic
fdt.begin_node_num("plic", PMA_PLIC_START);
fdt.prop_u32("#interrupt-cells", 1);
fdt.prop_empty("interrupt-controller");
fdt.prop_string("compatible", "riscv,plic0");
fdt.prop_u32("riscv,ndev", PMA_PLIC_MAX_IRQ);
fdt.prop_u64_list<2>("reg", {PMA_PLIC_START, PMA_PLIC_LENGTH});
fdt.prop_u32_list<4>("interrupts-extended",
{INTC_PHANDLE, MIP_SEIP_SHIFT, INTC_PHANDLE, MIP_MEIP_SHIFT});
fdt.prop_u32("phandle", PLIC_PHANDLE);
fdt.end_node();
}
{ // htif
fdt.begin_node_num("htif", PMA_HTIF_START);
fdt.prop_string("compatible", "ucb,htif0");
Expand Down
16 changes: 16 additions & 0 deletions src/grpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,22 @@ void grpc_virtual_machine::do_write_clint_mtimecmp(uint64_t val) {
write_csr(csr::clint_mtimecmp, val);
}

uint64_t grpc_virtual_machine::do_read_plic_girqpend(void) const {
return read_csr(csr::plic_girqpend);
}

void grpc_virtual_machine::do_write_plic_girqpend(uint64_t val) {
write_csr(csr::plic_girqpend, val);
}

uint64_t grpc_virtual_machine::do_read_plic_girqsrvd(void) const {
return read_csr(csr::plic_girqsrvd);
}

void grpc_virtual_machine::do_write_plic_girqsrvd(uint64_t val) {
write_csr(csr::plic_girqsrvd, val);
}

void grpc_virtual_machine::do_get_root_hash(hash_type &hash) const {
GetRootHashResponse response;
const Void request;
Expand Down
4 changes: 4 additions & 0 deletions src/grpc-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class grpc_virtual_machine : public i_virtual_machine {
void do_write_htif_iyield(uint64_t val) override;
uint64_t do_read_clint_mtimecmp(void) const override;
void do_write_clint_mtimecmp(uint64_t val) override;
uint64_t do_read_plic_girqpend(void) const override;
void do_write_plic_girqpend(uint64_t val) override;
uint64_t do_read_plic_girqsrvd(void) const override;
void do_write_plic_girqsrvd(uint64_t val) override;
void do_get_root_hash(hash_type &hash) const override;
machine_merkle_tree::proof_type do_get_proof(uint64_t address, int log2_size) const override;
void do_replace_memory_range(const memory_range_config &new_range) override;
Expand Down
28 changes: 28 additions & 0 deletions src/i-device-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ class i_device_state_access {
return do_write_clint_mtimecmp(val);
}

/// \brief Reads PLIC's girqpend.
/// \returns Register value.
uint64_t read_plic_girqpend(void) {
return do_read_plic_girqpend();
}

/// \brief Writes PLIC's girqpend.
/// \param val New register value.
void write_plic_girqpend(uint64_t val) {
return do_write_plic_girqpend(val);
}

/// \brief Reads PLIC's girqsrvd.
/// \returns Register value.
uint64_t read_plic_girqsrvd(void) {
return do_read_plic_girqsrvd();
}

/// \brief Writes PLIC's girqsrvd.
/// \param val New register value.
void write_plic_girqsrvd(uint64_t val) {
return do_write_plic_girqsrvd(val);
}

/// \brief Reads HTIF's fromhost.
/// \returns Register value.
uint64_t read_htif_fromhost(void) {
Expand Down Expand Up @@ -178,6 +202,10 @@ class i_device_state_access {
virtual void do_set_iflags_X(void) = 0;
virtual uint64_t do_read_clint_mtimecmp(void) = 0;
virtual void do_write_clint_mtimecmp(uint64_t val) = 0;
virtual uint64_t do_read_plic_girqpend(void) = 0;
virtual void do_write_plic_girqpend(uint64_t val) = 0;
virtual uint64_t do_read_plic_girqsrvd(void) = 0;
virtual void do_write_plic_girqsrvd(uint64_t val) = 0;
virtual uint64_t do_read_htif_fromhost(void) = 0;
virtual void do_write_htif_fromhost(uint64_t val) = 0;
virtual uint64_t do_read_htif_tohost(void) = 0;
Expand Down
24 changes: 24 additions & 0 deletions src/i-state-access.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,30 @@ class i_state_access { // CRTP
return derived().do_write_clint_mtimecmp(val);
}

/// \brief Reads PLIC's girqpend.
/// \returns Register value.
uint64_t read_plic_girqpend(void) {
return derived().do_read_plic_girqpend();
}

/// \brief Writes PLIC's girqpend.
/// \param val New register value.
void write_plic_girqpend(uint64_t val) {
return derived().do_write_plic_girqpend(val);
}

/// \brief Reads PLIC's girqsrvd.
/// \returns Register value.
uint64_t read_plic_girqsrvd(void) {
return derived().do_read_plic_girqsrvd();
}

/// \brief Writes PLIC's girqsrvd.
/// \param val New register value.
void write_plic_girqsrvd(uint64_t val) {
return derived().do_write_plic_girqsrvd(val);
}

/// \brief Reads HTIF's fromhost.
/// \returns Register value.
uint64_t read_htif_fromhost(void) {
Expand Down
24 changes: 24 additions & 0 deletions src/i-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,26 @@ class i_virtual_machine {
do_write_clint_mtimecmp(val);
}

/// \brief Reads PLIC's girqpend
uint64_t read_plic_girqpend(void) const {
return do_read_plic_girqpend();
}

/// \brief Writes PLIC's girqpend
void write_plic_girqpend(uint64_t val) {
do_write_plic_girqpend(val);
}

/// \brief Reads PLIC's girqsrvd
uint64_t read_plic_girqsrvd(void) const {
return do_read_plic_girqsrvd();
}

/// \brief Writes PLIC's girqsrvd
void write_plic_girqsrvd(uint64_t val) {
do_write_plic_girqsrvd(val);
}

/// \brief Reads the value of a microarchitecture register.
/// \param i Register index. Between 0 and UARCH_X_REG_COUNT-1, inclusive.
/// \returns The value of the register.
Expand Down Expand Up @@ -743,6 +763,10 @@ class i_virtual_machine {
virtual void do_write_htif_iyield(uint64_t val) = 0;
virtual uint64_t do_read_clint_mtimecmp(void) const = 0;
virtual void do_write_clint_mtimecmp(uint64_t val) = 0;
virtual uint64_t do_read_plic_girqpend(void) const = 0;
virtual void do_write_plic_girqpend(uint64_t val) = 0;
virtual uint64_t do_read_plic_girqsrvd(void) const = 0;
virtual void do_write_plic_girqsrvd(uint64_t val) = 0;
virtual void do_replace_memory_range(const memory_range_config &new_range) = 0;
virtual uint64_t do_read_word(uint64_t address) const = 0;
virtual bool do_verify_dirty_page_maps(void) const = 0;
Expand Down
32 changes: 32 additions & 0 deletions src/json-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static auto csr_from_name(const std::string &name) {
{"ilrsc", csr::ilrsc},
{"iflags", csr::iflags},
{"clint_mtimecmp", csr::clint_mtimecmp},
{"plic_girqpend", csr::plic_girqpend},
{"plic_girqsrvd", csr::plic_girqsrvd},
{"htif_tohost", csr::htif_tohost},
{"htif_fromhost", csr::htif_fromhost},
{"htif_ihalt", csr::htif_ihalt},
Expand Down Expand Up @@ -165,6 +167,10 @@ static auto csr_to_name(machine::csr reg) {
return "iflags";
case csr::clint_mtimecmp:
return "clint_mtimecmp";
case csr::plic_girqpend:
return "plic_girqpend";
case csr::plic_girqsrvd:
return "plic_girqsrvd";
case csr::htif_tohost:
return "htif_tohost";
case csr::htif_fromhost:
Expand Down Expand Up @@ -949,6 +955,23 @@ template void ju_get_opt_field<uint64_t>(const nlohmann::json &j, const uint64_t
template void ju_get_opt_field<std::string>(const nlohmann::json &j, const std::string &key, clint_config &value,
const std::string &path);

template <typename K>
void ju_get_opt_field(const nlohmann::json &j, const K &key, plic_config &value, const std::string &path) {
if (!contains(j, key)) {
return;
}
const auto &jconfig = j[key];
const auto new_path = path + to_string(key) + "/";
ju_get_opt_field(jconfig, "girqpend"s, value.girqpend, new_path);
ju_get_opt_field(jconfig, "girqsrvd"s, value.girqsrvd, new_path);
}

template void ju_get_opt_field<uint64_t>(const nlohmann::json &j, const uint64_t &key, plic_config &value,
const std::string &path);

template void ju_get_opt_field<std::string>(const nlohmann::json &j, const std::string &key, plic_config &value,
const std::string &path);

template <typename K>
void ju_get_opt_field(const nlohmann::json &j, const K &key, htif_config &value, const std::string &path) {
if (!contains(j, key)) {
Expand Down Expand Up @@ -1078,6 +1101,7 @@ void ju_get_opt_field(const nlohmann::json &j, const K &key, machine_config &val
ju_get_opt_field(config, "flash_drive"s, value.flash_drive, new_path);
ju_get_opt_field(config, "tlb"s, value.tlb, new_path);
ju_get_opt_field(config, "clint"s, value.clint, new_path);
ju_get_opt_field(config, "plic"s, value.plic, new_path);
ju_get_opt_field(config, "htif"s, value.htif, new_path);
ju_get_opt_field(config, "uarch"s, value.uarch, new_path);
ju_get_opt_field(config, "rollup"s, value.rollup, new_path);
Expand Down Expand Up @@ -1258,6 +1282,13 @@ void to_json(nlohmann::json &j, const clint_config &config) {
};
}

void to_json(nlohmann::json &j, const plic_config &config) {
j = nlohmann::json{
{"girqpend", config.girqpend},
{"girqsrvd", config.girqsrvd},
};
}

void to_json(nlohmann::json &j, const htif_config &config) {
j = nlohmann::json{
{"fromhost", config.fromhost},
Expand Down Expand Up @@ -1308,6 +1339,7 @@ void to_json(nlohmann::json &j, const machine_config &config) {
{"flash_drive", config.flash_drive},
{"tlb", config.tlb},
{"clint", config.clint},
{"plic", config.plic},
{"htif", config.htif},
{"uarch", config.uarch},
};
Expand Down
Loading

0 comments on commit 3823c2f

Please sign in to comment.