2020-02-22 20:36:40 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-22 20:36:40 +03:00
|
|
|
*/
|
|
|
|
|
2020-09-06 22:40:46 +03:00
|
|
|
#include <Kernel/API/Syscall.h>
|
2021-06-21 18:34:09 +03:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
|
|
|
#include <Kernel/Arch/x86/Interrupts.h>
|
2020-04-09 20:16:51 +03:00
|
|
|
#include <Kernel/CommandLine.h>
|
2021-09-11 10:39:47 +03:00
|
|
|
#include <Kernel/Firmware/ACPI/MultiProcessorParser.h>
|
2020-05-23 16:46:28 +03:00
|
|
|
#include <Kernel/IO.h>
|
2020-03-06 04:19:40 +03:00
|
|
|
#include <Kernel/Interrupts/APIC.h>
|
2020-02-22 20:36:40 +03:00
|
|
|
#include <Kernel/Interrupts/IOAPIC.h>
|
|
|
|
#include <Kernel/Interrupts/InterruptManagement.h>
|
|
|
|
#include <Kernel/Interrupts/PIC.h>
|
2021-06-05 07:50:27 +03:00
|
|
|
#include <Kernel/Interrupts/SharedIRQHandler.h>
|
2020-02-28 19:26:44 +03:00
|
|
|
#include <Kernel/Interrupts/SpuriousInterruptHandler.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/TypedMapping.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2020-02-22 20:36:40 +03:00
|
|
|
|
|
|
|
#define PCAT_COMPAT_FLAG 0x1
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
static InterruptManagement* s_interrupt_management;
|
|
|
|
|
|
|
|
bool InterruptManagement::initialized()
|
|
|
|
{
|
|
|
|
return (s_interrupt_management != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
InterruptManagement& InterruptManagement::the()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(InterruptManagement::initialized());
|
2020-02-22 20:36:40 +03:00
|
|
|
return *s_interrupt_management;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::initialize()
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!InterruptManagement::initialized());
|
2020-02-28 23:33:41 +03:00
|
|
|
s_interrupt_management = new InterruptManagement();
|
2020-04-09 20:16:51 +03:00
|
|
|
|
2021-03-03 11:51:55 +03:00
|
|
|
if (kernel_command_line().is_smp_enabled())
|
2020-04-09 20:16:51 +03:00
|
|
|
InterruptManagement::the().switch_to_ioapic_mode();
|
|
|
|
else
|
|
|
|
InterruptManagement::the().switch_to_pic_mode();
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManagement::enumerate_interrupt_handlers(Function<void(GenericInterruptHandler&)> callback)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; i++) {
|
|
|
|
auto& handler = get_interrupt_handler(i);
|
2021-06-05 07:50:27 +03:00
|
|
|
if (handler.type() == HandlerType::SharedIRQHandler) {
|
|
|
|
static_cast<SharedIRQHandler&>(handler).enumerate_handlers(callback);
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-05 20:15:38 +03:00
|
|
|
if (handler.type() != HandlerType::UnhandledInterruptHandler)
|
2020-02-22 20:36:40 +03:00
|
|
|
callback(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:33:41 +03:00
|
|
|
IRQController& InterruptManagement::get_interrupt_controller(int index)
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(index >= 0);
|
|
|
|
VERIFY(!m_interrupt_controllers[index].is_null());
|
2020-02-28 23:33:41 +03:00
|
|
|
return *m_interrupt_controllers[index];
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 03:34:46 +03:00
|
|
|
u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
|
2020-03-06 04:19:40 +03:00
|
|
|
{
|
|
|
|
if (!InterruptManagement::initialized()) {
|
|
|
|
// This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
|
2020-03-08 03:34:46 +03:00
|
|
|
return original_irq;
|
2020-03-06 04:19:40 +03:00
|
|
|
}
|
2020-03-08 03:34:46 +03:00
|
|
|
return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
|
2020-03-06 04:19:40 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 03:34:46 +03:00
|
|
|
u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(InterruptManagement::initialized());
|
2020-03-08 03:34:46 +03:00
|
|
|
return InterruptManagement::the().get_irq_vector(mapped_interrupt_vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 InterruptManagement::get_mapped_interrupt_vector(u8 original_irq)
|
|
|
|
{
|
|
|
|
// FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
|
2020-03-14 21:19:38 +03:00
|
|
|
// FIXME: Find a better way to handle conflict with Syscall interrupt gate.
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
|
2020-03-08 03:34:46 +03:00
|
|
|
return original_irq;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
|
2020-03-06 04:19:40 +03:00
|
|
|
{
|
|
|
|
// FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
|
2020-03-08 03:34:46 +03:00
|
|
|
return mapped_interrupt_vector;
|
2020-03-06 04:19:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:33:41 +03:00
|
|
|
RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2020-02-28 19:26:44 +03:00
|
|
|
if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
|
2020-02-28 23:33:41 +03:00
|
|
|
return m_interrupt_controllers[0];
|
2020-02-28 19:26:44 +03:00
|
|
|
}
|
2021-02-15 14:56:39 +03:00
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
2020-03-08 13:47:33 +03:00
|
|
|
if (irq_controller->gsi_base() <= interrupt_vector)
|
2020-02-22 20:36:40 +03:00
|
|
|
if (!irq_controller->is_hard_disabled())
|
2020-02-28 23:33:41 +03:00
|
|
|
return irq_controller;
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt()
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-01-09 20:51:44 +03:00
|
|
|
dbgln("Early access to ACPI tables for interrupt setup");
|
2020-04-09 18:12:58 +03:00
|
|
|
auto rsdp = ACPI::StaticParsing::find_rsdp();
|
2020-05-22 14:34:53 +03:00
|
|
|
if (!rsdp.has_value())
|
2020-02-28 23:33:41 +03:00
|
|
|
return {};
|
2021-09-07 07:29:11 +03:00
|
|
|
return ACPI::StaticParsing::find_table(rsdp.value(), "APIC").value();
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
|
2020-02-28 23:33:41 +03:00
|
|
|
: m_madt(search_for_madt())
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2020-09-07 12:53:54 +03:00
|
|
|
m_interrupt_controllers.resize(1);
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-03-12 16:02:58 +03:00
|
|
|
dmesgln("Interrupts: Switch to Legacy PIC mode");
|
2020-03-06 04:19:40 +03:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_smp_enabled = false;
|
2021-04-23 17:46:57 +03:00
|
|
|
m_interrupt_controllers[0] = adopt_ref(*new PIC());
|
2020-02-28 19:26:44 +03:00
|
|
|
SpuriousInterruptHandler::initialize(7);
|
|
|
|
SpuriousInterruptHandler::initialize(15);
|
2020-02-22 20:36:40 +03:00
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(irq_controller);
|
2020-02-22 20:36:40 +03:00
|
|
|
if (irq_controller->type() == IRQControllerType::i82093AA) {
|
|
|
|
irq_controller->hard_disable();
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
|
2020-02-22 20:36:40 +03:00
|
|
|
} else {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Interrupts: Detected {}", irq_controller->model());
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-03-12 16:02:58 +03:00
|
|
|
dmesgln("Interrupts: Switch to IOAPIC mode");
|
2020-03-06 04:19:40 +03:00
|
|
|
InterruptDisabler disabler;
|
2020-04-09 21:17:02 +03:00
|
|
|
|
|
|
|
if (m_madt.is_null()) {
|
2021-01-09 20:51:44 +03:00
|
|
|
dbgln("Interrupts: ACPI MADT is not available, reverting to PIC mode");
|
2020-04-09 21:17:02 +03:00
|
|
|
switch_to_pic_mode();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Interrupts: MADT @ P {}", m_madt.as_ptr());
|
2020-04-09 21:17:02 +03:00
|
|
|
locate_apic_data();
|
2020-03-06 04:19:40 +03:00
|
|
|
m_smp_enabled = true;
|
2020-02-22 20:36:40 +03:00
|
|
|
if (m_interrupt_controllers.size() == 1) {
|
|
|
|
if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
|
2021-03-12 16:02:58 +03:00
|
|
|
dmesgln("Interrupts: NO IOAPIC detected, Reverting to PIC mode.");
|
2020-02-22 20:36:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(irq_controller);
|
2020-02-22 20:36:40 +03:00
|
|
|
if (irq_controller->type() == IRQControllerType::i8259) {
|
|
|
|
irq_controller->hard_disable();
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
|
2020-02-22 20:36:40 +03:00
|
|
|
} else {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("Interrupts: Detected {}", irq_controller->model());
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 14:34:53 +03:00
|
|
|
|
|
|
|
if (auto mp_parser = MultiProcessorParser::autodetect()) {
|
|
|
|
m_pci_interrupt_overrides = mp_parser->get_pci_interrupt_redirections();
|
|
|
|
}
|
2020-07-06 16:27:22 +03:00
|
|
|
|
|
|
|
APIC::the().init_bsp();
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 20:41:50 +03:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
|
2020-02-22 20:36:40 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_madt.is_null());
|
2021-08-06 14:49:36 +03:00
|
|
|
auto madt = Memory::map_typed<ACPI::Structures::MADT>(m_madt);
|
2020-02-22 20:36:40 +03:00
|
|
|
|
2020-02-28 23:33:41 +03:00
|
|
|
int irq_controller_count = 0;
|
2020-05-23 16:46:28 +03:00
|
|
|
if (madt->flags & PCAT_COMPAT_FLAG) {
|
2021-04-23 17:46:57 +03:00
|
|
|
m_interrupt_controllers[0] = adopt_ref(*new PIC());
|
2020-02-28 23:33:41 +03:00
|
|
|
irq_controller_count++;
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
|
|
|
size_t entry_index = 0;
|
2020-05-23 16:46:28 +03:00
|
|
|
size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
|
|
|
|
auto* madt_entry = madt->entries;
|
2020-02-22 20:36:40 +03:00
|
|
|
while (entries_length > 0) {
|
|
|
|
size_t entry_length = madt_entry->length;
|
2020-02-28 23:33:41 +03:00
|
|
|
if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
|
|
|
|
auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("IOAPIC found @ MADT entry {}, MMIO Registers @ {}", entry_index, PhysicalAddress(ioapic_entry->ioapic_address));
|
2020-02-28 23:33:41 +03:00
|
|
|
m_interrupt_controllers.resize(1 + irq_controller_count);
|
2021-04-23 17:46:57 +03:00
|
|
|
m_interrupt_controllers[irq_controller_count] = adopt_ref(*new IOAPIC(PhysicalAddress(ioapic_entry->ioapic_address), ioapic_entry->gsi_base));
|
2020-02-28 23:33:41 +03:00
|
|
|
irq_controller_count++;
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
2020-02-28 23:33:41 +03:00
|
|
|
if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
|
|
|
|
auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
|
2020-05-08 22:07:04 +03:00
|
|
|
m_isa_interrupt_overrides.empend(
|
2020-02-22 20:36:40 +03:00
|
|
|
interrupt_override_entry->bus,
|
|
|
|
interrupt_override_entry->source,
|
|
|
|
interrupt_override_entry->global_system_interrupt,
|
2020-05-08 22:07:04 +03:00
|
|
|
interrupt_override_entry->flags);
|
2021-01-10 17:17:54 +03:00
|
|
|
|
|
|
|
dbgln("Interrupts: Overriding INT {:#x} with GSI {}, for bus {:#x}",
|
|
|
|
interrupt_override_entry->source,
|
|
|
|
interrupt_override_entry->global_system_interrupt,
|
|
|
|
interrupt_override_entry->bus);
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|
2020-05-23 14:43:34 +03:00
|
|
|
madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
|
2020-02-22 20:36:40 +03:00
|
|
|
entries_length -= entry_length;
|
|
|
|
entry_index++;
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 22:07:04 +03:00
|
|
|
|
2020-02-22 20:36:40 +03:00
|
|
|
}
|