Skip to main content

strat9_kernel/arch/x86_64/
pic.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! 8259 Programmable Interrupt Controller (PIC) driver
24//! Inspired by MaestroOS `pic.rs`
25//!
26//! The PIC handles hardware interrupts (IRQs) and maps them to
27//! CPU interrupt vectors. We remap IRQs to vectors 0x20-0x2F.
28
29use super::io::{inb, outb};
30
31/// Master PIC command port
32const MASTER_COMMAND: u16 = 0x20;
33/// Master PIC data port
34const MASTER_DATA: u16 = 0x21;
35/// Slave PIC command port
36const SLAVE_COMMAND: u16 = 0xA0;
37/// Slave PIC data port
38const SLAVE_DATA: u16 = 0xA1;
39
40/// ICW1: Initialization + ICW4 needed
41const ICW1_INIT: u8 = 0x10;
42const ICW1_ICW4: u8 = 0x01;
43/// ICW3: Slave PIC on IRQ2
44const ICW3_SLAVE_PIC: u8 = 0x04;
45/// ICW3: Cascade identity for slave
46const ICW3_CASCADE: u8 = 0x02;
47/// ICW4: 8086 mode
48const ICW4_8086: u8 = 0x01;
49
50/// End-of-interrupt command
51const COMMAND_EOI: u8 = 0x20;
52
53/// IRQ offset for master PIC (IRQ0 -> interrupt 0x20)
54pub const PIC1_OFFSET: u8 = 0x20;
55/// IRQ offset for slave PIC (IRQ8 -> interrupt 0x28)
56pub const PIC2_OFFSET: u8 = 0x28;
57
58/// Initialize the PIC with the given offsets.
59///
60/// Remaps IRQ0-7 to `offset1` and IRQ8-15 to `offset2`.
61pub fn init(offset1: u8, offset2: u8) {
62    unsafe {
63        // Save masks
64        let mask1 = inb(MASTER_DATA);
65        let mask2 = inb(SLAVE_DATA);
66
67        // Start initialization sequence
68        outb(MASTER_COMMAND, ICW1_INIT | ICW1_ICW4);
69        super::io::io_wait();
70        outb(SLAVE_COMMAND, ICW1_INIT | ICW1_ICW4);
71        super::io::io_wait();
72
73        // Set vector offsets
74        outb(MASTER_DATA, offset1);
75        super::io::io_wait();
76        outb(SLAVE_DATA, offset2);
77        super::io::io_wait();
78
79        // Configure cascading
80        outb(MASTER_DATA, ICW3_SLAVE_PIC);
81        super::io::io_wait();
82        outb(SLAVE_DATA, ICW3_CASCADE);
83        super::io::io_wait();
84
85        // Set 8086 mode
86        outb(MASTER_DATA, ICW4_8086);
87        super::io::io_wait();
88        outb(SLAVE_DATA, ICW4_8086);
89        super::io::io_wait();
90
91        // Restore saved masks
92        outb(MASTER_DATA, mask1);
93        outb(SLAVE_DATA, mask2);
94    }
95}
96
97/// Disable all IRQs on both PICs.
98pub fn disable() {
99    unsafe {
100        outb(MASTER_DATA, 0xFF);
101        outb(SLAVE_DATA, 0xFF);
102    }
103}
104
105/// Disable the 8259 PIC permanently by masking all IRQs.
106///
107/// Call this after remapping the PIC (to avoid stray interrupts at
108/// CPU exception vectors) and after the I/O APIC is ready to take over.
109pub fn disable_permanently() {
110    unsafe {
111        outb(MASTER_DATA, 0xFF);
112        outb(SLAVE_DATA, 0xFF);
113    }
114    log::info!("Legacy 8259 PIC disabled permanently");
115}
116
117/// Enable a specific IRQ line.
118pub fn enable_irq(mut irq: u8) {
119    let port = if irq < 8 {
120        MASTER_DATA
121    } else {
122        irq -= 8;
123        SLAVE_DATA
124    };
125    unsafe {
126        let value = inb(port) & !(1 << irq);
127        outb(port, value);
128    }
129}
130
131/// Disable a specific IRQ line.
132pub fn disable_irq(mut irq: u8) {
133    let port = if irq < 8 {
134        MASTER_DATA
135    } else {
136        irq -= 8;
137        SLAVE_DATA
138    };
139    unsafe {
140        let value = inb(port) | (1 << irq);
141        outb(port, value);
142    }
143}
144
145/// Send End-Of-Interrupt to the PIC for the given IRQ.
146pub fn end_of_interrupt(irq: u8) {
147    unsafe {
148        if irq >= 8 {
149            outb(SLAVE_COMMAND, COMMAND_EOI);
150        }
151        outb(MASTER_COMMAND, COMMAND_EOI);
152    }
153}