mod.rs 3.2 KB

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