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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use core::marker::PhantomData;

use r3_core::{
    kernel::{
        traits::KernelInterruptLine, ClearInterruptLineError, EnableInterruptLineError,
        InterruptNum, InterruptPriority, PendInterruptLineError, QueryInterruptLineError,
        SetInterruptLinePriorityError,
    },
    utils::Init,
};

use crate::{klock, KernelTraits, PortInterrupts, System};

unsafe impl<Traits: KernelTraits> r3_core::kernel::raw::KernelInterruptLine for System<Traits> {
    const RAW_MANAGED_INTERRUPT_PRIORITY_RANGE: core::ops::Range<InterruptPriority> =
        <Traits as PortInterrupts>::MANAGED_INTERRUPT_PRIORITY_RANGE;

    const RAW_MANAGED_INTERRUPT_LINES: &'static [InterruptNum] =
        <Traits as PortInterrupts>::MANAGED_INTERRUPT_LINES;

    #[cfg_attr(not(feature = "inline_syscall"), inline(never))]
    unsafe fn raw_interrupt_line_set_priority(
        this: InterruptNum,
        value: InterruptPriority,
    ) -> Result<(), SetInterruptLinePriorityError> {
        let mut _lock = klock::lock_cpu::<Traits>()?;

        // Deny a non-task context
        if !Traits::is_task_context() {
            return Err(SetInterruptLinePriorityError::BadContext);
        }

        // Safety: (1) We are the kernel, so it's okay to call `Port`'s methods.
        //         (2) CPU Lock active
        unsafe { Traits::set_interrupt_line_priority(this, value) }
    }

    #[inline]
    unsafe fn raw_interrupt_line_enable(
        this: InterruptNum,
    ) -> Result<(), EnableInterruptLineError> {
        // Safety: We are the kernel, so it's okay to call `Port`'s methods
        unsafe { Traits::enable_interrupt_line(this) }
    }

    #[inline]
    unsafe fn raw_interrupt_line_disable(
        this: InterruptNum,
    ) -> Result<(), EnableInterruptLineError> {
        // Safety: We are the kernel, so it's okay to call `Port`'s methods
        unsafe { Traits::disable_interrupt_line(this) }
    }

    #[inline]
    unsafe fn raw_interrupt_line_pend(this: InterruptNum) -> Result<(), PendInterruptLineError> {
        // Safety: We are the kernel, so it's okay to call `Port`'s methods
        unsafe { Traits::pend_interrupt_line(this) }
    }

    #[inline]
    unsafe fn raw_interrupt_line_clear(this: InterruptNum) -> Result<(), ClearInterruptLineError> {
        // Safety: We are the kernel, so it's okay to call `Port`'s methods
        unsafe { Traits::clear_interrupt_line(this) }
    }

    #[inline]
    unsafe fn raw_interrupt_line_is_pending(
        this: InterruptNum,
    ) -> Result<bool, QueryInterruptLineError> {
        // Safety: We are the kernel, so it's okay to call `Port`'s methods
        unsafe { Traits::is_interrupt_line_pending(this) }
    }
}

/// Initialization parameter for an interrupt line.
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct InterruptLineInit {
    pub(super) line: InterruptNum,
    pub(super) priority: InterruptPriority,
    pub(super) flags: InterruptLineInitFlags,
}

impl Init for InterruptLineInit {
    const INIT: Self = Self {
        line: 0,
        priority: Init::INIT,
        flags: InterruptLineInitFlags::empty(),
    };
}

bitflags::bitflags! {
    /// Flags for [`InterruptLineInit`].
    #[doc(hidden)]
    #[derive(Debug, Clone, Copy)]
    pub struct InterruptLineInitFlags: u32 {
        const ENABLE = 1 << 0;
        const SET_PRIORITY = 1 << 1;
    }
}

/// Initialization parameter for interrupt lines.
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct InterruptAttr<Traits> {
    pub _phantom: PhantomData<Traits>,
    pub line_inits: &'static [InterruptLineInit],
}

impl<Traits: KernelTraits> InterruptAttr<Traits> {
    /// Initialize interrupt lines.
    ///
    /// # Safety
    ///
    /// This method may call `InterruptLine::set_priority_unchecked`. The caller
    /// is responsible for ensuring *unmanaged safety*.
    ///
    /// Can be called only during a boot phase.
    pub(super) unsafe fn init(&self, _lock: klock::CpuLockTokenRefMut<Traits>) {
        for line_init in self.line_inits {
            if line_init
                .flags
                .contains(InterruptLineInitFlags::SET_PRIORITY)
            {
                // Safety: (1) The caller is responsible for ensuring unmanaged
                //             safety.
                //         (2) Boot phase
                //         (3) CPU Lock
                unsafe {
                    <Traits as PortInterrupts>::set_interrupt_line_priority(
                        line_init.line,
                        line_init.priority,
                    )
                    .unwrap();
                }
            }
            if line_init.flags.contains(InterruptLineInitFlags::ENABLE) {
                unsafe { System::<Traits>::raw_interrupt_line_enable(line_init.line).unwrap() };
            }
        }
    }
}