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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// TODO: add docs to these methods...
#![allow(clippy::missing_safety_doc)]

use core::{
    ptr::null_mut,
    sync::atomic::{AtomicPtr, Ordering},
};

use d1_pac::{plic, Interrupt, PLIC};
use kernel::isr::Isr;

/// Interrupt Priority from 0..31
pub type Priority = plic::prio::PRIORITY_A;

#[doc = r" TryFromPrioritytError"]
#[derive(Debug, Copy, Clone)]
pub struct TryFromPriorityError(());

/// Errors returned by [`Plic::activate`] and [`Plic::deactivate`].
#[derive(Debug, Copy, Clone)]
pub enum MaskError {
    /// No such interrupt was found!
    NotFound(Interrupt),
    /// The interrupt did not have a handler.
    NoHandler(Interrupt),
}

/// Platform-Level Interrupt Controller (PLIC) interface
pub struct Plic {
    plic: PLIC,
}

impl Plic {
    /// Create a new `Plic` from the [`PLIC`] peripheral
    pub fn new(plic: PLIC) -> Self {
        // TODO any initial setup we should be doing for the PLIC at startup?
        Self { plic }
    }

    /// Obtain a static `Plic` instance for use in e.g. interrupt handlers
    ///
    /// # Safety
    ///
    /// 'Tis thine responsibility, that which thou doth summon.
    pub unsafe fn summon() -> Self {
        Self {
            plic: d1_pac::Peripherals::steal().PLIC,
        }
    }

    /// Enable an interrupt
    ///
    /// # Safety
    ///
    /// May effect normal interrupt processing
    pub unsafe fn unmask(&self, interrupt: Interrupt) {
        let (mie, irq_en) = self.index_mie(interrupt);
        mie.modify(|r, w| w.bits(r.bits() | irq_en));
    }

    /// Disable an interrupt
    pub fn mask(&self, interrupt: Interrupt) {
        let (mie, irq_en) = self.index_mie(interrupt);
        mie.modify(|r, w| unsafe { w.bits(r.bits() | irq_en) });
    }

    /// Globally set priority for one interrupt
    ///
    /// # Safety
    ///
    /// May effect normal interrupt processing
    pub unsafe fn set_priority(&self, interrupt: Interrupt, priority: Priority) {
        let nr = interrupt.into_bits() as usize;
        self.plic.prio[nr].write(|w| w.bits(priority.into_bits()));
    }

    pub fn claim(&self) -> Interrupt {
        let claim = self.plic.mclaim.read().mclaim().bits() as u8;
        match Interrupt::try_from(claim) {
            Ok(interrupt) => interrupt,
            Err(e) => {
                panic!("error claiming interrupt: {e:?}");
            }
        }
    }

    /// Dispatch an interrupt to a vectored handler.
    ///
    /// # Safety
    ///
    /// Should only be called in an ISR such as `MachineInternal`!
    pub unsafe fn dispatch_interrupt(&self) {
        debug_assert!(
            Isr::is_in_isr(),
            "Plic::dispatch should only be called in an ISR!"
        );
        let claim = self.claim();
        let claim_u16 = claim as u16;

        // Is this a known interrupt?
        // N.B. that the index is always in bounds, because `claim()` has
        // panicked already if the interrupt is not a valid interrupt number.
        // Theoretically, we could probably optimize this to use
        // `get_unchecked`, if we cared to.
        let &Vectored { id, ref handler } = &INTERRUPT_ARRAY[claim_u16 as usize];
        debug_assert_eq!(
            id,
            Some(claim),
            "FLAGRANT ERROR: interrupt ID {id:?} does not match index \
                ({claim_u16}); perhaps the interrupt dispatch table has \
                somehow been corrupted?"
        );
        let ptr = handler.load(Ordering::SeqCst); // todo: ordering
        if !ptr.is_null() {
            let hdlr: fn() = unsafe { core::mem::transmute(ptr) };
            (hdlr)();
        } // otherwise, the ISR hasn't been registered yet; just do nothing.

        // Release claim
        self.complete(claim);
    }

    pub fn complete(&self, interrupt: Interrupt) {
        self.plic
            .mclaim
            .write(|w| w.mclaim().variant(interrupt.into_bits() as u16));
    }

    #[track_caller]
    pub unsafe fn register(&self, interrupt: Interrupt, new_hdl: fn()) {
        let idx = interrupt as usize;
        let Some(&Vectored { id, ref handler }) = INTERRUPT_ARRAY.get(idx) else {
            panic!("interrupt not found in dispatch table: {interrupt:?} (index {idx})")
        };
        assert_eq!(
            Some(interrupt),
            id,
            "FLAGRANT ERROR: interrupt ID for {interrupt:?} does not \
            match index {idx}; perhaps the interrupt dispatch table has \
            somehow been corrupted?"
        );
        handler.store(new_hdl as *mut fn() as *mut (), Ordering::Release);
    }

    pub unsafe fn activate(&self, interrupt: Interrupt, prio: Priority) -> Result<(), MaskError> {
        self.can_mask(interrupt)?;
        self.set_priority(interrupt, prio);
        self.unmask(interrupt);
        Ok(())
    }

    pub fn deactivate(&self, interrupt: Interrupt) -> Result<(), MaskError> {
        self.can_mask(interrupt)?;
        self.mask(interrupt);
        Ok(())
    }

    fn can_mask(&self, interrupt: Interrupt) -> Result<(), MaskError> {
        let &Vectored { id, ref handler } = INTERRUPT_ARRAY
            .get(interrupt as usize)
            .ok_or(MaskError::NotFound(interrupt))?;

        if id != Some(interrupt) {
            return Err(MaskError::NotFound(interrupt));
        }

        if handler.load(Ordering::SeqCst).is_null() {
            return Err(MaskError::NoHandler(interrupt));
        }

        Ok(())
    }

    #[inline(always)]
    fn index_mie(&self, interrupt: Interrupt) -> (&plic::MIE, u32) {
        let nr = interrupt.into_bits() as usize;
        (&self.plic.mie[nr / 32], 1 << (nr % 32))
    }
}

/// Bit conversions
trait IntoBits: Sized + Copy {
    fn into_bits(self) -> u32;
}

#[allow(dead_code)]
trait TryFromBits: Sized + Copy {
    type Error;
    fn try_from_bits(bits: u32) -> Result<Self, Self::Error>;
}

impl IntoBits for Interrupt {
    fn into_bits(self) -> u32 {
        self as u8 as u32
    }
}

impl IntoBits for Priority {
    fn into_bits(self) -> u32 {
        u8::from(self) as u32
    }
}

impl TryFromBits for Priority {
    type Error = TryFromPriorityError;
    fn try_from_bits(bits: u32) -> Result<Self, Self::Error> {
        match bits {
            0 => Ok(Priority::P0),
            1 => Ok(Priority::P1),
            2 => Ok(Priority::P2),
            3 => Ok(Priority::P3),
            4 => Ok(Priority::P4),
            5 => Ok(Priority::P5),
            6 => Ok(Priority::P6),
            7 => Ok(Priority::P7),
            8 => Ok(Priority::P8),
            9 => Ok(Priority::P9),
            10 => Ok(Priority::P10),
            11 => Ok(Priority::P11),
            12 => Ok(Priority::P12),
            13 => Ok(Priority::P13),
            14 => Ok(Priority::P14),
            15 => Ok(Priority::P15),
            16 => Ok(Priority::P16),
            17 => Ok(Priority::P17),
            18 => Ok(Priority::P18),
            19 => Ok(Priority::P19),
            20 => Ok(Priority::P20),
            21 => Ok(Priority::P21),
            22 => Ok(Priority::P22),
            23 => Ok(Priority::P23),
            24 => Ok(Priority::P24),
            25 => Ok(Priority::P25),
            26 => Ok(Priority::P26),
            27 => Ok(Priority::P27),
            28 => Ok(Priority::P28),
            29 => Ok(Priority::P29),
            30 => Ok(Priority::P30),
            31 => Ok(Priority::P31),
            _ => Err(TryFromPriorityError(())),
        }
    }
}

struct Vectored {
    id: Option<Interrupt>,
    handler: AtomicPtr<()>,
}

impl Vectored {
    const fn new(interrupt: Interrupt) -> Self {
        Self {
            id: Some(interrupt),
            handler: AtomicPtr::new(null_mut()),
        }
    }

    const fn none() -> Self {
        Self {
            id: None,
            handler: AtomicPtr::new(no_such_interrupt as *mut fn() as *mut ()),
        }
    }
}

const INTERRUPT_LIST: &[Interrupt] = &[
    Interrupt::UART0,
    Interrupt::UART1,
    Interrupt::UART2,
    Interrupt::UART3,
    Interrupt::UART4,
    Interrupt::UART5,
    Interrupt::TWI0,
    Interrupt::TWI1,
    Interrupt::TWI2,
    Interrupt::TWI3,
    Interrupt::SPI0,
    Interrupt::SPI1,
    Interrupt::PWM,
    Interrupt::IR_TX,
    Interrupt::LEDC,
    Interrupt::OWA,
    Interrupt::DMIC,
    Interrupt::AUDIO_CODEC,
    Interrupt::I2S_PCM0,
    Interrupt::I2S_PCM1,
    Interrupt::I2S_PCM2,
    Interrupt::USB0_DEVICE,
    Interrupt::USB0_EHCI,
    Interrupt::USB0_OHCI,
    Interrupt::USB1_EHCI,
    Interrupt::USB1_OHCI,
    Interrupt::SMHC0,
    Interrupt::SMHC1,
    Interrupt::SMHC2,
    Interrupt::EMAC,
    Interrupt::DMAC_NS,
    Interrupt::CE_NS,
    Interrupt::SPINLOCK,
    Interrupt::HSTIMER0,
    Interrupt::HSTIMER1,
    Interrupt::GPADC,
    Interrupt::THS,
    Interrupt::TIMER0,
    Interrupt::TIMER1,
    Interrupt::LRADC,
    Interrupt::TPADC,
    Interrupt::WATCHDOG,
    Interrupt::IOMMU,
    Interrupt::GPIOB_NS,
    Interrupt::GPIOC_NS,
    Interrupt::GPIOD_NS,
    Interrupt::GPIOE_NS,
    Interrupt::GPIOF_NS,
    Interrupt::CSI_DMA0,
    Interrupt::CSI_DMA1,
    Interrupt::CSI_TOP_PKT,
    Interrupt::TVD,
    Interrupt::DSP_MBOX_RV_W,
    Interrupt::RV_MBOX_RV,
    Interrupt::RV_MBOX_DSP,
    Interrupt::IR_RX,
];

const N_INTERRUPTS: usize = {
    let mut max = 0;
    // INTERRUPT_LIST is sorted, but --- because we're doing this in a const fn
    // --- we may as well just find the actual max vector, instead of taking the
    // vector of the last element in the array; this will *always* be correct,
    // even if the interrupt list is ever out of order.
    //
    // Unfortunately, as you probably already know, const fn. So, we do this
    // with a goofy while loop, instead of `iter().max()` --- you can pretend it
    // says that, if you like.
    let mut i = 0;
    while i < INTERRUPT_LIST.len() {
        let vector = INTERRUPT_LIST[i] as usize;
        if vector > max {
            max = vector;
        }
        i += 1;
    }
    max + 1
};

static INTERRUPT_ARRAY: [Vectored; N_INTERRUPTS] = {
    // This is a static initializer, ignore the stupid goddamn clippy lint.
    #[allow(clippy::declare_interior_mutable_const)]
    const TRAP: Vectored = Vectored::none();
    let mut array = [TRAP; N_INTERRUPTS];

    // Populate the real interrupt vectors.
    let mut i = 0;
    while i < INTERRUPT_LIST.len() {
        let interrupt = INTERRUPT_LIST[i];
        let vector = interrupt as usize;
        array[vector] = Vectored::new(interrupt);
        i += 1;
    }
    array
};

fn no_such_interrupt() {
    panic!("no such interrupt!");
}