garage44.eu/jeroen/pinnacle

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

.. /kernel/src/gic.rs

raw

//! The GICv3 interrupt controller: the distributor, and each core's
//! redistributor and the CPU interface through system registers.

use core::sync::atomic::{AtomicUsize, Ordering};

/// The distributor's and the first redistributor's registers, from the
/// device tree's `arm,gic-v3` node (its first two windows), else where the
/// board puts them.
static DISTRIBUTOR: AtomicUsize = AtomicUsize::new(crate::board::gic::DISTRIBUTOR);
static REDISTRIBUTORS: AtomicUsize = AtomicUsize::new(crate::board::gic::REDISTRIBUTOR);

fn distributor() -> usize {
    DISTRIBUTOR.load(Ordering::Relaxed)
}

fn redistributors_at() -> usize {
    REDISTRIBUTORS.load(Ordering::Relaxed)
}

const GICD_CTLR: usize = 0x0;
const GICD_CTLR_RWP: u32 = 1 << 31;
/// Affinity routing, and both interrupt groups on.
const GICD_CTLR_ENABLE: u32 = (1 << 4) | (1 << 1) | 1;

const GICR_WAKER: usize = 0x14;
const WAKER_PROCESSOR_SLEEP: u32 = 1 << 1;
const WAKER_CHILDREN_ASLEEP: u32 = 1 << 2;

/// The redistributor's second frame, for SGIs and PPIs.
const SGI_BASE: usize = 0x1_0000;
const GICR_IGROUPR0: usize = SGI_BASE + 0x80;
const GICR_ISENABLER0: usize = SGI_BASE + 0x100;
const GICR_IPRIORITYR: usize = SGI_BASE + 0x400;

/// What ICC_IAR1_EL1 reads when no interrupt is pending.
pub const SPURIOUS: u32 = 1023;

fn read(base: usize, offset: usize) -> u32 {
    // SAFETY: a GIC register, mapped as Device memory.
    unsafe { ((base + offset) as *const u32).read_volatile() }
}

fn write(base: usize, offset: usize, value: u32) {
    // SAFETY: as in `read`.
    unsafe { ((base + offset) as *mut u32).write_volatile(value) }
}

/// Each core's redistributor follows the previous one's two 64 KiB frames.
const REDISTRIBUTOR_STRIDE: usize = 0x2_0000;

/// GICR_TYPER: the core a redistributor serves (bits 63:32) and whether
/// it is the last frame (bit 4).
const GICR_TYPER: usize = 0x08;
const TYPER_LAST: u64 = 1 << 4;
/// More frames than any GICv3 has in one region, should none say "last".
const MAX_FRAMES: usize = 512;

/// Every redistributor, in order: its frame's index and the MPIDR
/// affinity (Aff3, Aff2, Aff1, Aff0) of the core it serves.
pub fn redistributors() -> alloc::vec::Vec<(usize, u64)> {
    let mut out = alloc::vec::Vec::new();
    for frame in 0..MAX_FRAMES {
        let at = redistributors_at() + frame * REDISTRIBUTOR_STRIDE + GICR_TYPER;
        // SAFETY: GICR_TYPER of a frame within the redistributor region,
        // mapped as Device memory; the walk stops at the last frame.
        let typer = unsafe { (at as *const u64).read_volatile() };
        let aff = typer >> 32;
        let mpidr = (aff & 0xff_ffff) | ((aff >> 24) & 0xff) << 32;
        out.push((frame, mpidr));
        if typer & TYPER_LAST != 0 {
            break;
        }
    }
    out
}

/// The distributor, once, on core 0 before the others start; the cores
/// are found first.
pub fn init() {
    if let Some(gic) = crate::device::find("arm,gic-v3")
        && let [(distributor, _), (redistributors, _), ..] = gic.regs[..]
    {
        DISTRIBUTOR.store(crate::mmu::virt(distributor), Ordering::Relaxed);
        REDISTRIBUTORS.store(crate::mmu::virt(redistributors), Ordering::Relaxed);
        crate::device::mark(&gic.name, "gic-v3", crate::device::Class::Interrupts);
    }
    crate::smp::discover();
    write(distributor(), GICD_CTLR, GICD_CTLR_ENABLE);
    while read(distributor(), GICD_CTLR) & GICD_CTLR_RWP != 0 {}
    init_core(0);
}

fn redistributor(core: usize) -> usize {
    redistributors_at() + crate::smp::frame(core) * REDISTRIBUTOR_STRIDE
}

/// This core's redistributor and CPU interface.
pub fn init_core(core: usize) {
    let rd = redistributor(core);
    let waker = read(rd, GICR_WAKER);
    write(rd, GICR_WAKER, waker & !WAKER_PROCESSOR_SLEEP);
    while read(rd, GICR_WAKER) & WAKER_CHILDREN_ASLEEP != 0 {}
    // Every SGI and PPI in group 1, the group IRQs come from.
    write(rd, GICR_IGROUPR0, u32::MAX);

    // SAFETY: the CPU interface's own registers; SRE first, or the others
    // are not accessible.
    unsafe {
        core::arch::asm!(
            "mrs {t}, icc_sre_el1",
            "orr {t}, {t}, #1",
            "msr icc_sre_el1, {t}",
            "isb",
            "mov {t}, #0xff",
            "msr icc_pmr_el1, {t}",
            "mov {t}, #1",
            "msr icc_igrpen1_el1, {t}",
            "isb",
            t = out(reg) _,
            options(nostack),
        );
    }
}

/// Enables a software-generated or private peripheral interrupt (INTID
/// 0–31) of this core.
pub fn enable_private(intid: u32) {
    assert!(intid < 32);
    let rd = redistributor(crate::arch::core_id());
    // SAFETY: one byte of GICR_IPRIORITYR, which is byte-accessible.
    unsafe { ((rd + GICR_IPRIORITYR + intid as usize) as *mut u8).write_volatile(0xa0) };
    write(rd, GICR_ISENABLER0, 1 << intid);
}

/// Takes the highest-priority pending interrupt, or [`SPURIOUS`].
pub fn acknowledge() -> u32 {
    let intid: u64;
    // SAFETY: reading IAR1 acknowledges the interrupt; nothing else.
    unsafe { core::arch::asm!("mrs {}, icc_iar1_el1", out(reg) intid, options(nomem, nostack)) };
    intid as u32
}

/// Ends an interrupt [`acknowledge`] took.
pub fn end(intid: u32) {
    // SAFETY: as in `acknowledge`.
    unsafe { core::arch::asm!("msr icc_eoir1_el1, {}", in(reg) u64::from(intid), options(nomem, nostack)) };
}

/// Sends software-generated interrupt `intid` (0–15) to `core`.
pub fn send_sgi(core: usize, intid: u32) {
    let mpidr = crate::smp::mpidr(core);
    let (aff0, aff1, aff2, aff3) = (
        mpidr & 0xff,
        (mpidr >> 8) & 0xff,
        (mpidr >> 16) & 0xff,
        (mpidr >> 32) & 0xff,
    );
    // ICC_SGI1R_EL1: the target list a bit per core of 16 (Aff0 mod 16),
    // RS which 16 (Aff0 / 16), and the cluster's Aff1-Aff3.
    let value = (1 << (aff0 % 16))
        | (aff1 << 16)
        | (u64::from(intid) << 24)
        | (aff2 << 32)
        | ((aff0 / 16) << 44)
        | (aff3 << 48);
    // SAFETY: ICC_SGI1R_EL1 only raises the interrupt.
    unsafe { core::arch::asm!("dsb ishst", "msr icc_sgi1r_el1, {}", "isb", in(reg) value, options(nostack)) };
}

const GICD_IGROUPR: usize = 0x80;
const GICD_ISENABLER: usize = 0x100;
const GICD_IPRIORITYR: usize = 0x400;
const GICD_IROUTER: usize = 0x6000;

/// Enables shared peripheral interrupt `intid` (32 and up), delivered to
/// core 0.
pub fn enable_shared(intid: u32) {
    enable_shared_on(intid, 0);
}

/// Enables shared peripheral interrupt `intid` (32 and up), delivered to
/// `core`: a device's interrupts can be kept off the core that has the
/// most else to do.
pub fn enable_shared_on(intid: u32, core: usize) {
    assert!(intid >= 32);
    let (word, bit) = ((intid / 32) as usize * 4, 1 << (intid % 32));
    write(
        distributor(),
        GICD_IGROUPR + word,
        read(distributor(), GICD_IGROUPR + word) | bit,
    );
    // SAFETY: one byte of GICD_IPRIORITYR, and GICD_IROUTER's 64 bits: the
    // core's affinity.
    unsafe {
        ((distributor() + GICD_IPRIORITYR + intid as usize) as *mut u8).write_volatile(0xa0);
        ((distributor() + GICD_IROUTER + intid as usize * 8) as *mut u64)
            .write_volatile(crate::smp::mpidr(core));
    }
    write(distributor(), GICD_ISENABLER + word, bit);
}

/// No message interrupts here (they would need the GIC's ITS): PCI devices
/// signal by INTx, routed by the device tree.
pub fn message_interrupt() -> Option<(u64, u32, u32)> {
    None
}