use bit_field::BitField;
pub mod ioapic;
pub mod x2apic;
pub mod xapic;
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum DeliveryMode {
Fixed = 0b000,
LowestPriority = 0b001,
SMI = 0b010,
_Reserved = 0b11,
NMI = 0b100,
Init = 0b101,
StartUp = 0b110,
}
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum DestinationMode {
Physical = 0,
Logical = 1,
}
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum DeliveryStatus {
Idle = 0,
SendPending = 1,
}
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum Level {
Deassert = 0,
Assert = 1,
}
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum TriggerMode {
Edge = 0,
Level = 1,
}
#[derive(Debug, Eq, PartialEq)]
#[repr(u64)]
pub enum DestinationShorthand {
NoShorthand = 0b00,
Myself = 0b01,
AllIncludingSelf = 0b10,
AllExcludingSelf = 0b11,
}
#[derive(Debug, Eq, PartialEq)]
pub struct Icr(u64);
impl Icr {
fn id_to_xapic_destination(destination: ApicId) -> u64 {
match destination {
ApicId::XApic(d) => (d as u64) << 56,
ApicId::X2Apic(_d) => {
unreachable!("x2APIC IDs are not supported for xAPIC (use the x2APIC controller)")
}
}
}
fn id_to_x2apic_destination(destination: ApicId) -> u64 {
let d: u64 = match destination {
ApicId::XApic(d) => d as u64,
ApicId::X2Apic(d) => d as u64,
};
d << 32
}
#[allow(clippy::too_many_arguments)]
fn new(
dest_encoder: fn(ApicId) -> u64,
vector: u8,
destination: ApicId,
destination_shorthand: DestinationShorthand,
delivery_mode: DeliveryMode,
destination_mode: DestinationMode,
delivery_status: DeliveryStatus,
level: Level,
trigger_mode: TriggerMode,
) -> Icr {
Icr(dest_encoder(destination)
| (destination_shorthand as u64) << 18
| (trigger_mode as u64) << 15
| (level as u64) << 14
| (delivery_status as u64) << 12
| (destination_mode as u64) << 11
| (delivery_mode as u64) << 8
| (vector as u64))
}
#[allow(clippy::too_many_arguments)]
pub fn for_x2apic(
vector: u8,
destination: ApicId,
destination_shorthand: DestinationShorthand,
delivery_mode: DeliveryMode,
destination_mode: DestinationMode,
delivery_status: DeliveryStatus,
level: Level,
trigger_mode: TriggerMode,
) -> Icr {
Icr::new(
Icr::id_to_x2apic_destination,
vector,
destination,
destination_shorthand,
delivery_mode,
destination_mode,
delivery_status,
level,
trigger_mode,
)
}
#[allow(clippy::too_many_arguments)]
pub fn for_xapic(
vector: u8,
destination: ApicId,
destination_shorthand: DestinationShorthand,
delivery_mode: DeliveryMode,
destination_mode: DestinationMode,
delivery_status: DeliveryStatus,
level: Level,
trigger_mode: TriggerMode,
) -> Icr {
Icr::new(
Icr::id_to_xapic_destination,
vector,
destination,
destination_shorthand,
delivery_mode,
destination_mode,
delivery_status,
level,
trigger_mode,
)
}
pub fn lower(&self) -> u32 {
self.0 as u32
}
pub fn upper(&self) -> u32 {
(self.0 >> 32) as u32
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ApicId {
XApic(u8),
X2Apic(u32),
}
impl ApicId {
pub fn x2apic_logical_id(&self) -> u32 {
self.x2apic_logical_cluster_id() << 16 | 1 << self.x2apic_logical_cluster_address()
}
pub fn x2apic_logical_cluster_address(&self) -> u32 {
let d = match *self {
ApicId::XApic(id) => id as u32,
ApicId::X2Apic(id) => id as u32,
};
d.get_bits(0..=3)
}
pub fn x2apic_logical_cluster_id(&self) -> u32 {
let d = match *self {
ApicId::XApic(id) => id as u32,
ApicId::X2Apic(id) => id as u32,
};
d.get_bits(4..=19)
}
}
#[allow(clippy::clippy::from_over_into)]
impl Into<usize> for ApicId {
fn into(self) -> usize {
match self {
ApicId::XApic(id) => id as usize,
ApicId::X2Apic(id) => id as usize,
}
}
}
pub trait ApicControl {
fn bsp(&self) -> bool;
fn id(&self) -> u32;
fn logical_id(&self) -> u32;
fn version(&self) -> u32;
fn eoi(&mut self);
fn tsc_enable(&mut self, vector: u8);
fn tsc_set(&self, value: u64);
unsafe fn ipi_init(&mut self, core: ApicId);
unsafe fn ipi_init_deassert(&mut self);
unsafe fn ipi_startup(&mut self, core: ApicId, start_page: u8);
unsafe fn send_ipi(&mut self, icr: Icr);
}