mod.rs 3.2 KB

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