garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/x86_64/gic.rs

raw

//! What the rest of the kernel asks of ARM's GIC, answered by a PC's APICs:
//! a core's software interrupt is an IPI through its local APIC, numbered
//! from [`SGI_BASE`]; a device's ("shared") interrupt comes by message
//! (MSI-X, MSI) or, wired, through the I/O APIC (`ioapic`), routed when
//! its driver asks: so [`enable_shared`] has nothing to do.

/// Where IPIs start among the vectors: SGI n is vector `SGI_BASE + n`.
pub const SGI_BASE: u32 = 0x40;

/// Sends software interrupt `intid` to core `core`.
pub fn send_sgi(core: usize, intid: u32) {
    crate::apic::send_ipi(crate::smp::apic_id(core), SGI_BASE + intid);
}

/// Lets a device's interrupt through to core 0: on a PC it is through as
/// soon as it has a vector.
pub fn enable_shared(_intid: u32) {}

/// As [`enable_shared`], to core `core`.
pub fn enable_shared_on(_intid: u32, _core: usize) {}

/// Lets a core's own interrupt through: the local APIC passes them all.
pub fn enable_private(_intid: u32) {}

/// Device interrupts by message (MSI-X) take vectors from here up.
const MESSAGE_BASE: u32 = 0x50;
const MESSAGE_END: u32 = 0xf0;

/// A vector of the devices' range not yet given out, or None once they
/// run out.
fn device_vector() -> Option<u32> {
    use core::sync::atomic::{AtomicU32, Ordering};
    static NEXT: AtomicU32 = AtomicU32::new(MESSAGE_BASE);
    let vector = NEXT.fetch_add(1, Ordering::Relaxed);
    (vector < MESSAGE_END).then_some(vector)
}

/// A message interrupt for a device to signal core 0 with: the address and
/// data it writes, and the vector (its "INTID") that then arrives. `None`
/// once the vectors run out.
pub fn message_interrupt() -> Option<(u64, u32, u32)> {
    let vector = device_vector()?;
    let address = 0xfee0_0000 | u64::from(crate::smp::apic_id(0) & 0xff) << 12;
    Some((address, vector, vector))
}

/// A wired interrupt, I/O APIC pin `gsi`, sent to core 0: the vector (its
/// "INTID") it arrives as, or None when no I/O APIC has the pin or the
/// vectors ran out.
pub fn wired_interrupt(gsi: u32, trigger: crate::ioapic::Trigger) -> Option<u32> {
    let vector = device_vector()?;
    crate::ioapic::route(gsi, vector, trigger).then_some(vector)
}

/// Whether `vector` is a device's message interrupt.
pub fn is_device(vector: u32) -> bool {
    (MESSAGE_BASE..MESSAGE_END).contains(&vector)
}