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
use aarch64_cpu::registers::DAIF;
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};

/// Enable interrupts
#[inline]
pub fn enable() {
    DAIF.modify(DAIF::A::Masked + DAIF::I::Masked + DAIF::F::Masked);
}

/// Disable interrupts
#[inline]
pub fn disable() {
    DAIF.modify(DAIF::A::Unmasked + DAIF::I::Unmasked + DAIF::F::Unmasked);
}

/// Disable IRQs (nested)
///
/// Disable all IRQs when unsure if IRQs were enabled at all.
/// This function together with nested_enable can be used
/// in situations when interrupts shouldn't be activated if they
/// were not activated before calling this function.
#[inline]
pub fn nested_disable() -> u64 {
    let bits = DAIF.get();
    disable();
    bits
}

/// Enable all IRQs (nested)
///
/// Can be used in conjunction with nested_disable() to only enable
/// interrupts again if they were enabled before.
#[inline]
pub fn nested_enable(bits: u64) {
    DAIF.set(bits);
}