1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::ops::Range;

use super::Scheme;
use crate::DeviceResult;

/// A type alias for
pub type IrqHandler = Box<dyn Fn() + Send + Sync>;

#[derive(Debug)]
pub enum IrqTriggerMode {
    Edge,
    Level,
}

#[derive(Debug)]
pub enum IrqPolarity {
    ActiveHigh,
    ActiveLow,
}

pub trait IrqScheme: Scheme {
    /// Is a valid IRQ number.
    fn is_valid_irq(&self, irq_num: usize) -> bool;

    /// Disable IRQ.
    fn mask(&self, irq_num: usize) -> DeviceResult;

    /// Enable IRQ.
    fn unmask(&self, irq_num: usize) -> DeviceResult;

    /// Configure the specified interrupt vector. If it is invoked, it must be
    /// invoked prior to interrupt registration.
    fn configure(&self, _irq_num: usize, _tm: IrqTriggerMode, _pol: IrqPolarity) -> DeviceResult {
        unimplemented!()
    }

    /// Add an interrupt handler to an IRQ.
    fn register_handler(&self, irq_num: usize, handler: IrqHandler) -> DeviceResult;

    /// Register the device to delivery an IRQ.
    fn register_device(&self, irq_num: usize, dev: Arc<dyn Scheme>) -> DeviceResult {
        self.register_handler(irq_num, Box::new(move || dev.handle_irq(irq_num)))
    }

    /// Remove the interrupt handler to an IRQ.
    fn unregister(&self, irq_num: usize) -> DeviceResult;

    /// Method used for platform allocation of blocks of MSI and MSI-X compatible
    /// IRQ targets.
    fn msi_alloc_block(&self, _requested_irqs: usize) -> DeviceResult<Range<usize>> {
        unimplemented!()
    }

    /// Method used to free a block of MSI IRQs previously allocated by msi_alloc_block().
    /// This does not unregister IRQ handlers.
    fn msi_free_block(&self, _block: Range<usize>) -> DeviceResult {
        unimplemented!()
    }

    /// Register a handler function for a given msi_id within an msi_block_t. Passing a
    /// NULL handler will effectively unregister a handler for a given msi_id within the
    /// block.
    fn msi_register_handler(
        &self,
        _block: Range<usize>,
        _msi_id: usize,
        _handler: IrqHandler,
    ) -> DeviceResult {
        unimplemented!()
    }

    /// Init irq for current cpu.
    /// Some IRQ hardware requires per-CPU initialization.
    fn init_hart(&self) {
        unimplemented!()
    }

    /// [for x86_64] enable apic timer
    fn apic_timer_enable(&self) {
        unimplemented!()
    }
}