Skip to content

Commit

Permalink
Add configuration for PLIC external interrupt handler
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Aug 27, 2024
1 parent 787473f commit 2e450c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ default = ["bin", "json", "yaml"]
bin = ["dep:clap", "dep:env_logger", "serde", "dep:irx-config"]
json = ["dep:serde_json"]
yaml = ["dep:serde_yaml"]
unstable-riscv = ["svd-rs/unstable-riscv", "svd-parser/unstable-riscv"]
unstable-riscv = []

[dependencies]
clap = { version = "4.0", optional = true }
Expand Down
12 changes: 11 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub struct RiscvConfig {
pub priorities: Option<Vec<RiscvEnumItem>>,
pub harts: Option<Vec<RiscvEnumItem>>,
pub clint: Option<RiscvClintConfig>,
pub plic: Option<String>,
pub plic: Option<RiscvPlicConfig>,
}

#[cfg(feature = "unstable-riscv")]
Expand Down Expand Up @@ -358,3 +358,13 @@ pub struct RiscvClintConfig {
pub freq: Option<usize>,
pub async_delay: bool,
}

#[cfg(feature = "unstable-riscv")]
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct RiscvPlicConfig {
pub name: String,
pub core_interrupt: Option<String>,
pub hart_id: Option<String>,
}
25 changes: 23 additions & 2 deletions src/generate/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn is_riscv_peripheral(p: &Peripheral, c: &Config) -> bool {
match &c.riscv_config {
Some(c) => {
c.clint.as_ref().is_some_and(|clint| clint.name == p.name)
|| c.plic.as_ref().is_some_and(|plic| plic == &p.name)
|| c.plic.as_ref().is_some_and(|plic| plic.name == p.name)
}
_ => false,
}
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn render(
});
}
if let Some(plic) = &c.plic {
let p = peripherals.iter().find(|&p| &p.name == plic).unwrap();
let p = peripherals.iter().find(|&p| p.name == plic.name).unwrap();
let base = TokenStream::from_str(&format!("base 0x{:X},", p.base_address)).unwrap();
let ctxs = harts
.iter()
Expand All @@ -271,6 +271,27 @@ pub fn render(
riscv_peripherals.extend(quote! {
riscv_peripheral::plic_codegen!(#base #ctxs);
});

if let Some(core_interrupt) = &plic.core_interrupt {
let core_interrupt = TokenStream::from_str(core_interrupt).unwrap();
let ctx = match &plic.hart_id {
Some(hart_id) => {
TokenStream::from_str(&format!("ctx(Hart::{hart_id})")).unwrap()
}
None => quote! { ctx_mhartid() },
};
mod_items.extend(quote! {
#[cfg(feature = "rt")]
#[riscv_rt::core_interrupt(CoreInterrupt::#core_interrupt)]
fn plic_handler() {
let claim = crate::PLIC::#ctx.claim();
if let Some(s) = claim.claim::<CoreInterrupt>() {
unsafe { _dispatch_core_interrupt(s.number()) }
claim.complete(s);
}
}
});
}
}
}

Expand Down

0 comments on commit 2e450c5

Please sign in to comment.