rdif_intc/
lib.rs

1#![no_std]
2
3extern crate alloc;
4
5pub use alloc::{boxed::Box, vec::Vec};
6use core::{error::Error, fmt::Display};
7
8use cfg_if::cfg_if;
9use rdif_base::custom_type;
10pub use rdif_base::{DriverGeneric, DriverResult, IrqConfig, IrqId, Trigger};
11
12custom_type!(CpuId, usize, "{:#x}");
13
14pub type Hardware = Box<dyn Interface>;
15pub type BoxCPU = Box<dyn InterfaceCPU>;
16pub type BoxCPUCapLocalIrq = Box<dyn CPUCapLocalIrq>;
17
18/// Fdt 解析 `interrupts` 函数,一次解析一个`cell`
19pub type FuncFdtParseConfig =
20    fn(prop_interrupts_one_cell: &[u32]) -> Result<IrqConfig, Box<dyn Error>>;
21
22pub trait CPUCapLocalIrq: Send + Sync {
23    fn irq_enable(&self, irq: IrqId) -> Result<(), IntcError>;
24    fn irq_disable(&self, irq: IrqId) -> Result<(), IntcError>;
25    fn set_priority(&self, irq: IrqId, priority: usize) -> Result<(), IntcError>;
26    fn set_trigger(&self, irq: IrqId, trigger: Trigger) -> Result<(), IntcError>;
27}
28
29pub trait InterfaceCPU: Send + Sync {
30    fn setup(&self);
31    fn ack(&self) -> Option<IrqId>;
32    fn eoi(&self, irq: IrqId);
33
34    cfg_if! {
35        if #[cfg(target_arch = "aarch64")]{
36            fn dir(&self, intid: IrqId);
37            fn set_eoi_mode(&self, b: bool);
38            fn get_eoi_mode(&self) -> bool;
39        }
40    }
41
42    fn capability(&self) -> CPUCapability;
43}
44
45pub trait Interface: DriverGeneric {
46    fn cpu_interface(&self) -> BoxCPU;
47
48    fn irq_enable(&mut self, irq: IrqId) -> Result<(), IntcError>;
49    fn irq_disable(&mut self, irq: IrqId) -> Result<(), IntcError>;
50    fn set_priority(&mut self, irq: IrqId, priority: usize) -> Result<(), IntcError>;
51    fn set_trigger(&mut self, irq: IrqId, trigger: Trigger) -> Result<(), IntcError>;
52    fn set_target_cpu(&mut self, irq: IrqId, cpu: CpuId) -> Result<(), IntcError>;
53    fn capabilities(&self) -> Vec<Capability> {
54        Vec::new()
55    }
56}
57
58pub enum Capability {
59    FdtParseConfig(FuncFdtParseConfig),
60}
61
62pub enum CPUCapability {
63    None,
64    LocalIrq(BoxCPUCapLocalIrq),
65}
66
67#[derive(Debug, Clone, Copy)]
68pub enum IntcError {
69    IrqIdNotCompatible { id: IrqId },
70    NotSupport,
71}
72impl Display for IntcError {
73    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74        write!(f, "{self:?}")
75    }
76}
77impl Error for IntcError {}