2020-02-22 19:25:29 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-22 19:25:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
|
#include <AK/Types.h>
|
2022-05-16 14:42:49 +02:00
|
|
|
#include <Kernel/Arch/InterruptDisabler.h>
|
2021-10-01 09:58:50 +03:00
|
|
|
#include <Kernel/Arch/x86/IO.h>
|
2020-03-08 12:47:33 +02:00
|
|
|
#include <Kernel/Interrupts/GenericInterruptHandler.h>
|
2020-02-22 19:25:29 +02:00
|
|
|
#include <Kernel/Interrupts/PIC.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2020-02-22 19:25:29 +02:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
|
|
// The slave 8259 is connected to the master's IRQ2 line.
|
|
|
|
|
// This is really only to enhance clarity.
|
|
|
|
|
#define SLAVE_INDEX 2
|
|
|
|
|
|
|
|
|
|
#define PIC0_CTL 0x20
|
|
|
|
|
#define PIC0_CMD 0x21
|
|
|
|
|
#define PIC1_CTL 0xA0
|
|
|
|
|
#define PIC1_CMD 0xA1
|
|
|
|
|
|
2021-03-12 17:38:49 +01:00
|
|
|
#define ICW1_ICW4 0x01 // ICW4 (not) needed
|
|
|
|
|
#define ICW1_SINGLE 0x02 // Single (cascade) mode
|
|
|
|
|
#define ICW1_INTERVAL4 0x04 // Call address interval 4 (8)
|
|
|
|
|
#define ICW1_LEVEL 0x08 // Level triggered (edge) mode
|
|
|
|
|
#define ICW1_INIT 0x10 // Initialization - required
|
2020-02-22 19:25:29 +02:00
|
|
|
|
2021-03-12 17:38:49 +01:00
|
|
|
#define ICW4_8086 0x01 // 8086/88 (MCS-80/85) mode
|
|
|
|
|
#define ICW4_AUTO 0x02 // Auto (normal) EOI
|
|
|
|
|
#define ICW4_BUF_SLAVE 0x08 // Buffered mode/slave
|
|
|
|
|
#define ICW4_BUF_MASTER 0x0C // Buffered mode/master
|
|
|
|
|
#define ICW4_SFNM 0x10 // Special fully nested (not)
|
2020-02-22 19:25:29 +02:00
|
|
|
|
2020-03-21 09:33:58 +02:00
|
|
|
bool inline static is_all_masked(u16 reg)
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
2020-03-21 09:33:58 +02:00
|
|
|
return reg == 0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PIC::is_enabled() const
|
|
|
|
|
{
|
|
|
|
|
return !is_all_masked(m_cached_irq_mask) && !is_hard_disabled();
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void PIC::disable(GenericInterruptHandler const& handler)
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_hard_disabled());
|
|
|
|
|
VERIFY(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
|
2020-03-08 12:47:33 +02:00
|
|
|
u8 irq = handler.interrupt_number();
|
2020-03-21 09:33:58 +02:00
|
|
|
if (m_cached_irq_mask & (1 << irq))
|
|
|
|
|
return;
|
2020-02-22 19:25:29 +02:00
|
|
|
u8 imr;
|
2020-03-20 16:52:14 +02:00
|
|
|
if (irq & 8) {
|
2020-02-22 19:25:29 +02:00
|
|
|
imr = IO::in8(PIC1_CMD);
|
2020-03-20 16:52:14 +02:00
|
|
|
imr |= 1 << (irq & 7);
|
2020-02-22 19:25:29 +02:00
|
|
|
IO::out8(PIC1_CMD, imr);
|
|
|
|
|
} else {
|
|
|
|
|
imr = IO::in8(PIC0_CMD);
|
|
|
|
|
imr |= 1 << irq;
|
|
|
|
|
IO::out8(PIC0_CMD, imr);
|
|
|
|
|
}
|
2020-03-21 09:33:58 +02:00
|
|
|
m_cached_irq_mask |= 1 << irq;
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT PIC::PIC()
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
|
|
|
|
initialize();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void PIC::spurious_eoi(GenericInterruptHandler const& handler) const
|
2020-03-08 12:47:33 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(handler.type() == HandlerType::SpuriousInterruptHandler);
|
2020-03-20 16:52:14 +02:00
|
|
|
if (handler.interrupt_number() == 7)
|
|
|
|
|
return;
|
|
|
|
|
if (handler.interrupt_number() == 15) {
|
|
|
|
|
IO::in8(PIC1_CMD); /* dummy read */
|
|
|
|
|
IO::out8(PIC0_CTL, 0x60 | (2));
|
|
|
|
|
}
|
2020-03-08 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 19:25:29 +02:00
|
|
|
bool PIC::is_vector_enabled(u8 irq) const
|
|
|
|
|
{
|
2020-03-21 09:33:58 +02:00
|
|
|
return m_cached_irq_mask & (1 << irq);
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void PIC::enable(GenericInterruptHandler const& handler)
|
2020-03-08 12:47:33 +02:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_hard_disabled());
|
|
|
|
|
VERIFY(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
|
2020-03-08 12:47:33 +02:00
|
|
|
enable_vector(handler.interrupt_number());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIC::enable_vector(u8 irq)
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_hard_disabled());
|
2020-03-21 09:33:58 +02:00
|
|
|
if (!(m_cached_irq_mask & (1 << irq)))
|
|
|
|
|
return;
|
2020-02-22 19:25:29 +02:00
|
|
|
u8 imr;
|
2020-03-20 16:52:14 +02:00
|
|
|
if (irq & 8) {
|
2020-02-22 19:25:29 +02:00
|
|
|
imr = IO::in8(PIC1_CMD);
|
2020-03-20 16:52:14 +02:00
|
|
|
imr &= ~(1 << (irq & 7));
|
2020-02-22 19:25:29 +02:00
|
|
|
IO::out8(PIC1_CMD, imr);
|
|
|
|
|
} else {
|
|
|
|
|
imr = IO::in8(PIC0_CMD);
|
|
|
|
|
imr &= ~(1 << irq);
|
|
|
|
|
IO::out8(PIC0_CMD, imr);
|
|
|
|
|
}
|
2020-03-21 09:33:58 +02:00
|
|
|
m_cached_irq_mask &= ~(1 << irq);
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void PIC::eoi(GenericInterruptHandler const& handler) const
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_hard_disabled());
|
2020-03-21 12:03:22 +02:00
|
|
|
u8 irq = handler.interrupt_number();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(irq >= gsi_base() && irq < interrupt_vectors_count());
|
2020-03-21 12:03:22 +02:00
|
|
|
if ((1 << irq) & m_cached_irq_mask) {
|
|
|
|
|
spurious_eoi(handler);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
eoi_interrupt(irq);
|
2020-03-08 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIC::eoi_interrupt(u8 irq) const
|
|
|
|
|
{
|
2020-03-20 16:52:14 +02:00
|
|
|
if (irq & 8) {
|
|
|
|
|
IO::in8(PIC1_CMD); /* dummy read */
|
|
|
|
|
IO::out8(PIC1_CTL, 0x60 | (irq & 7));
|
|
|
|
|
IO::out8(PIC0_CTL, 0x60 | (2));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
IO::in8(PIC0_CMD); /* dummy read */
|
|
|
|
|
IO::out8(PIC0_CTL, 0x60 | irq);
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIC::complete_eoi() const
|
|
|
|
|
{
|
|
|
|
|
IO::out8(PIC1_CTL, 0x20);
|
|
|
|
|
IO::out8(PIC0_CTL, 0x20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIC::hard_disable()
|
|
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2022-01-29 15:38:29 +02:00
|
|
|
remap(pic_disabled_vector_base);
|
2020-02-22 19:25:29 +02:00
|
|
|
IO::out8(PIC0_CMD, 0xff);
|
|
|
|
|
IO::out8(PIC1_CMD, 0xff);
|
2020-03-21 09:33:58 +02:00
|
|
|
m_cached_irq_mask = 0xffff;
|
2020-02-22 19:25:29 +02:00
|
|
|
IRQController::hard_disable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PIC::remap(u8 offset)
|
|
|
|
|
{
|
|
|
|
|
/* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
|
|
|
|
|
IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
|
|
|
|
|
IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
|
|
|
|
|
|
2022-01-06 07:07:15 -07:00
|
|
|
/* ICW2 (upper 5 bits specify ISR indices, lower 3 don't specify anything) */
|
2020-02-22 19:25:29 +02:00
|
|
|
IO::out8(PIC0_CMD, offset);
|
|
|
|
|
IO::out8(PIC1_CMD, offset + 0x08);
|
|
|
|
|
|
|
|
|
|
/* ICW3 (configure master/slave relationship) */
|
|
|
|
|
IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
|
|
|
|
|
IO::out8(PIC1_CMD, SLAVE_INDEX);
|
|
|
|
|
|
|
|
|
|
/* ICW4 (set x86 mode) */
|
2020-03-20 16:52:14 +02:00
|
|
|
IO::out8(PIC0_CMD, ICW4_8086);
|
|
|
|
|
IO::out8(PIC1_CMD, ICW4_8086);
|
2020-02-22 19:25:29 +02:00
|
|
|
|
|
|
|
|
// Mask -- start out with all IRQs disabled.
|
|
|
|
|
IO::out8(PIC0_CMD, 0xff);
|
|
|
|
|
IO::out8(PIC1_CMD, 0xff);
|
2020-03-21 09:33:58 +02:00
|
|
|
m_cached_irq_mask = 0xffff;
|
2020-02-22 19:25:29 +02:00
|
|
|
|
|
|
|
|
// ...except IRQ2, since that's needed for the master to let through slave interrupts.
|
2020-03-08 12:47:33 +02:00
|
|
|
enable_vector(2);
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 21:29:46 +01:00
|
|
|
UNMAP_AFTER_INIT void PIC::initialize()
|
2020-02-22 19:25:29 +02:00
|
|
|
{
|
|
|
|
|
/* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
|
|
|
|
|
IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
|
|
|
|
|
IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
|
|
|
|
|
|
2022-01-06 07:07:15 -07:00
|
|
|
/* ICW2 (upper 5 bits specify ISR indices, lower 3 don't specify anything) */
|
2020-02-22 19:25:29 +02:00
|
|
|
IO::out8(PIC0_CMD, IRQ_VECTOR_BASE);
|
|
|
|
|
IO::out8(PIC1_CMD, IRQ_VECTOR_BASE + 0x08);
|
|
|
|
|
|
|
|
|
|
/* ICW3 (configure master/slave relationship) */
|
|
|
|
|
IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
|
|
|
|
|
IO::out8(PIC1_CMD, SLAVE_INDEX);
|
|
|
|
|
|
|
|
|
|
/* ICW4 (set x86 mode) */
|
2020-03-20 16:52:14 +02:00
|
|
|
IO::out8(PIC0_CMD, ICW4_8086);
|
|
|
|
|
IO::out8(PIC1_CMD, ICW4_8086);
|
2020-02-22 19:25:29 +02:00
|
|
|
|
|
|
|
|
// Mask -- start out with all IRQs disabled.
|
|
|
|
|
IO::out8(PIC0_CMD, 0xff);
|
|
|
|
|
IO::out8(PIC1_CMD, 0xff);
|
|
|
|
|
|
|
|
|
|
// ...except IRQ2, since that's needed for the master to let through slave interrupts.
|
2020-03-08 12:47:33 +02:00
|
|
|
enable_vector(2);
|
2020-02-22 19:25:29 +02:00
|
|
|
|
2021-03-12 14:06:37 +01:00
|
|
|
dmesgln("PIC: Cascading mode, vectors {:#02x}-{:#02x}", IRQ_VECTOR_BASE, IRQ_VECTOR_BASE + 0xf);
|
2020-02-22 19:25:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u16 PIC::get_isr() const
|
|
|
|
|
{
|
|
|
|
|
IO::out8(PIC0_CTL, 0x0b);
|
|
|
|
|
IO::out8(PIC1_CTL, 0x0b);
|
|
|
|
|
u8 isr0 = IO::in8(PIC0_CTL);
|
|
|
|
|
u8 isr1 = IO::in8(PIC1_CTL);
|
|
|
|
|
return (isr1 << 8) | isr0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u16 PIC::get_irr() const
|
|
|
|
|
{
|
|
|
|
|
IO::out8(PIC0_CTL, 0x0a);
|
|
|
|
|
IO::out8(PIC1_CTL, 0x0a);
|
|
|
|
|
u8 irr0 = IO::in8(PIC0_CTL);
|
|
|
|
|
u8 irr1 = IO::in8(PIC1_CTL);
|
|
|
|
|
return (irr1 << 8) | irr0;
|
|
|
|
|
}
|
|
|
|
|
}
|