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
use crate::devices::pic;
use crate::devices::lapic::LocalApic;
use crate::devices::ioapic::IoApic;
use acpi::interrupt::{InterruptModel, InterruptSourceOverride};
use crate::sync::Once;
use alloc::vec::Vec;
use crate::mem::PhysicalAddress;
struct InterruptHandler {
root_lapic: LocalApic,
ioapics: Vec<IoApic>,
isa_mappings: Vec<InterruptSourceOverride>
}
static INTERRUPT_HANDLER: Once<InterruptHandler> = Once::new();
pub fn init() {
info!("Init pic");
pic::init();
info!("Mask all interrupts in PIC");
let pic = pic::get();
for i in 0..16 {
pic.mask(i);
}
info!("Acquire INTERRUPT_HANDLER");
let handler = INTERRUPT_HANDLER.call_once(|| {
match crate::i386::acpi::try_get_acpi_information().and_then(|v| v.interrupt_model().as_ref()) {
Some(InterruptModel::Apic { local_apic_address, io_apics, interrupt_source_overrides, .. }) => {
unsafe {
let lapic = LocalApic::new(PhysicalAddress(*local_apic_address as usize));
let ioapics: Vec<IoApic> = io_apics.iter().map(|v|
IoApic::new(PhysicalAddress(v.address as usize), v.global_system_interrupt_base, lapic.local_apic_id())
).collect();
for over_ride in interrupt_source_overrides {
let mut entry = ioapics[0].redirection_entry(over_ride.global_system_interrupt as u8);
entry.set_interrupt_vector(u64::from(0x20 + over_ride.isa_source));
ioapics[0].set_redirection_entry(over_ride.global_system_interrupt as u8, entry);
}
InterruptHandler {
root_lapic: lapic,
ioapics,
isa_mappings: interrupt_source_overrides.clone()
}
}
}
_ => panic!("ACPI did not find a Local APIC"),
}
});
info!("Enable the APIC");
handler.root_lapic.enable();
for mapping in &handler.isa_mappings {
if mapping.isa_source == 0 {
continue;
}
let irq = mapping.global_system_interrupt;
let ioapic = handler.ioapics.iter().find(|ioapic|
ioapic.interrupt_base() <= irq &&
irq < ioapic.interrupt_base() + ioapic.redirection_entry_count()).unwrap();
let mut redirection_entry = ioapic.redirection_entry((irq - ioapic.interrupt_base()) as u8);
info!("Mapping ISA interrupt {} (at IOAPIC {})", mapping.isa_source, irq);
redirection_entry.set_interrupt_vector(u64::from(0x20 + mapping.isa_source));
let trigger_mode = match mapping.trigger_mode {
acpi::interrupt::TriggerMode::Level => true,
_ => false
};
redirection_entry.set_trigger_mode(trigger_mode);
let polarity = match mapping.polarity {
acpi::interrupt::Polarity::ActiveHigh => false,
_ => true
};
redirection_entry.set_interrupt_input_pin_polarity(polarity);
ioapic.set_redirection_entry((irq - ioapic.interrupt_base()) as u8, redirection_entry);
}
}
pub fn acknowledge(_irq: u8) {
INTERRUPT_HANDLER.r#try().unwrap().root_lapic.acknowledge();
}
pub fn unmask(irq: u8) {
let ioapics = &INTERRUPT_HANDLER.r#try().unwrap().ioapics;
let irqisa = irq;
let irq = isa_to_ioapic_irq(irq);
debug!("Unmasking IRQ {} (ISA {})", irq, irqisa);
let ioapic = ioapics.iter().find(|ioapic|
ioapic.interrupt_base() <= irq &&
irq < ioapic.interrupt_base() + ioapic.redirection_entry_count()).unwrap();
let mut redirection_entry = ioapic.redirection_entry((irq - ioapic.interrupt_base()) as u8);
redirection_entry.set_interrupt_mask(false);
ioapic.set_redirection_entry((irq - ioapic.interrupt_base()) as u8, redirection_entry);
}
fn isa_to_ioapic_irq(irq: u8) -> u32 {
let isa_mappings = &INTERRUPT_HANDLER.r#try().unwrap().isa_mappings;
isa_mappings.iter()
.find(|v| v.isa_source == irq).map(|v| v.global_system_interrupt)
.unwrap_or_else(|| irq.into())
}