1 use core::ops::Add; 2 3 use system_error::SystemError; 4 5 use crate::arch::CurrentIrqArch; 6 7 pub mod dummychip; 8 pub mod handle; 9 pub mod init; 10 pub mod ipi; 11 pub mod irqchip; 12 pub mod irqdata; 13 pub mod irqdesc; 14 pub mod irqdomain; 15 pub mod manage; 16 pub mod msi; 17 mod resend; 18 pub mod softirq; 19 pub mod sysfs; 20 21 /// 中断的架构相关的trait 22 pub trait InterruptArch: Send + Sync { 23 /// 架构相关的中断初始化 arch_irq_init() -> Result<(), SystemError>24 unsafe fn arch_irq_init() -> Result<(), SystemError>; 25 /// 使能中断 interrupt_enable()26 unsafe fn interrupt_enable(); 27 /// 禁止中断 interrupt_disable()28 unsafe fn interrupt_disable(); 29 /// 检查中断是否被禁止 is_irq_enabled() -> bool30 fn is_irq_enabled() -> bool; 31 32 /// 保存当前中断状态,并且禁止中断 save_and_disable_irq() -> IrqFlagsGuard33 unsafe fn save_and_disable_irq() -> IrqFlagsGuard; restore_irq(flags: IrqFlags)34 unsafe fn restore_irq(flags: IrqFlags); 35 36 /// 检测系统支持的中断总数 probe_total_irq_num() -> u3237 fn probe_total_irq_num() -> u32; 38 arch_early_irq_init() -> Result<(), SystemError>39 fn arch_early_irq_init() -> Result<(), SystemError> { 40 Ok(()) 41 } 42 43 /// 响应未注册的中断 ack_bad_irq(irq: IrqNumber)44 fn ack_bad_irq(irq: IrqNumber); 45 } 46 47 #[derive(Debug, Clone, Copy)] 48 pub struct IrqFlags { 49 flags: usize, 50 } 51 52 impl IrqFlags { new(flags: usize) -> Self53 pub fn new(flags: usize) -> Self { 54 IrqFlags { flags } 55 } 56 flags(&self) -> usize57 pub fn flags(&self) -> usize { 58 self.flags 59 } 60 } 61 62 /// @brief 当前中断状态的保护器,当该对象被drop时,会恢复之前的中断状态 63 /// 64 /// # Example 65 /// 66 /// ``` 67 /// use crate::arch::CurrentIrqArch; 68 /// 69 /// // disable irq and save irq state (这是唯一的获取IrqFlagsGuard的方法) 70 /// let guard = unsafe{CurrentIrqArch::save_and_disable_irq()}; 71 /// 72 /// // do something 73 /// 74 /// // 销毁guard时,会恢复之前的中断状态 75 /// drop(guard); 76 /// 77 /// ``` 78 #[derive(Debug)] 79 pub struct IrqFlagsGuard { 80 flags: IrqFlags, 81 } 82 83 impl IrqFlagsGuard { 84 /// @brief 创建IrqFlagsGuard对象 85 /// 86 /// # Safety 87 /// 88 /// 该函数不安全,因为它不会检查flags是否是一个有效的IrqFlags对象, 而当它被drop时,会恢复flags中的中断状态 89 /// 90 /// 该函数只应被`CurrentIrqArch::save_and_disable_irq`调用 new(flags: IrqFlags) -> Self91 pub unsafe fn new(flags: IrqFlags) -> Self { 92 IrqFlagsGuard { flags } 93 } 94 } 95 impl Drop for IrqFlagsGuard { drop(&mut self)96 fn drop(&mut self) { 97 unsafe { 98 CurrentIrqArch::restore_irq(self.flags); 99 } 100 } 101 } 102 103 // 定义中断号结构体 104 // 用于表示软件逻辑视角的中断号,全局唯一 105 int_like!(IrqNumber, u32); 106 107 impl IrqNumber { 108 /// 如果一个(PCI)设备中断没有被连接,我们将设置irqnumber为IRQ_NOTCONNECTED。 109 /// 这导致request_irq()失败,返回-ENOTCONN,这样我们就可以区分这种情况和其他错误返回。 110 pub const IRQ_NOTCONNECTED: IrqNumber = IrqNumber::new(u32::MAX); 111 } 112 113 // 硬件中断号 114 // 用于表示在某个IrqDomain中的中断号 115 int_like!(HardwareIrqNumber, u32); 116 117 impl Add<u32> for HardwareIrqNumber { 118 type Output = HardwareIrqNumber; 119 add(self, rhs: u32) -> HardwareIrqNumber120 fn add(self, rhs: u32) -> HardwareIrqNumber { 121 HardwareIrqNumber::new(self.0 + rhs) 122 } 123 } 124 125 impl Add<u32> for IrqNumber { 126 type Output = IrqNumber; 127 add(self, rhs: u32) -> IrqNumber128 fn add(self, rhs: u32) -> IrqNumber { 129 IrqNumber::new(self.0 + rhs) 130 } 131 } 132