2020-02-22 17:36:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-22 17:36:40 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-28 17:23:58 +00:00
|
|
|
#include <AK/ByteReader.h>
|
2020-09-06 19:40:46 +00:00
|
|
|
#include <Kernel/API/Syscall.h>
|
2022-05-11 14:54:03 +00:00
|
|
|
#include <Kernel/Arch/Interrupts.h>
|
2023-06-09 18:24:25 +00:00
|
|
|
#include <Kernel/Arch/x86_64/Firmware/MultiProcessor/Parser.h>
|
2022-10-04 10:46:11 +00:00
|
|
|
#include <Kernel/Arch/x86_64/InterruptManagement.h>
|
|
|
|
#include <Kernel/Arch/x86_64/Interrupts/APIC.h>
|
|
|
|
#include <Kernel/Arch/x86_64/Interrupts/IOAPIC.h>
|
|
|
|
#include <Kernel/Arch/x86_64/Interrupts/PIC.h>
|
2023-02-24 18:21:53 +00:00
|
|
|
#include <Kernel/Boot/CommandLine.h>
|
2023-06-09 19:50:11 +00:00
|
|
|
#include <Kernel/Firmware/ACPI/StaticParsing.h>
|
2023-02-24 18:33:43 +00:00
|
|
|
#include <Kernel/Interrupts/InterruptDisabler.h>
|
2021-06-05 04:50:27 +00:00
|
|
|
#include <Kernel/Interrupts/SharedIRQHandler.h>
|
2020-02-28 16:26:44 +00:00
|
|
|
#include <Kernel/Interrupts/SpuriousInterruptHandler.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/TypedMapping.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2020-02-22 17:36:40 +00: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 19:42:32 +00:00
|
|
|
VERIFY(InterruptManagement::initialized());
|
2020-02-22 17:36:40 +00:00
|
|
|
return *s_interrupt_management;
|
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::initialize()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!InterruptManagement::initialized());
|
2020-02-28 20:33:41 +00:00
|
|
|
s_interrupt_management = new InterruptManagement();
|
2021-12-19 19:30:36 +00:00
|
|
|
if (!kernel_command_line().is_smp_enabled_without_ioapic_enabled()) {
|
|
|
|
dbgln("Can't enable SMP mode without IOAPIC mode being enabled");
|
|
|
|
}
|
2021-11-28 15:38:10 +00:00
|
|
|
if (!kernel_command_line().is_ioapic_enabled() && !kernel_command_line().is_smp_enabled())
|
2020-04-09 17:16:51 +00:00
|
|
|
InterruptManagement::the().switch_to_pic_mode();
|
2021-11-28 15:38:10 +00:00
|
|
|
else
|
|
|
|
InterruptManagement::the().switch_to_ioapic_mode();
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptManagement::enumerate_interrupt_handlers(Function<void(GenericInterruptHandler&)> callback)
|
|
|
|
{
|
2022-09-25 12:29:24 +00:00
|
|
|
for (size_t i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; i++) {
|
2020-02-22 17:36:40 +00:00
|
|
|
auto& handler = get_interrupt_handler(i);
|
2021-06-05 04:50:27 +00:00
|
|
|
if (handler.type() == HandlerType::SharedIRQHandler) {
|
|
|
|
static_cast<SharedIRQHandler&>(handler).enumerate_handlers(callback);
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-05 17:15:38 +00:00
|
|
|
if (handler.type() != HandlerType::UnhandledInterruptHandler)
|
2020-02-22 17:36:40 +00:00
|
|
|
callback(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:29:24 +00:00
|
|
|
IRQController& InterruptManagement::get_interrupt_controller(size_t index)
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2020-02-28 20:33:41 +00:00
|
|
|
return *m_interrupt_controllers[index];
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:34:46 +00:00
|
|
|
u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
|
2020-03-06 01:19:40 +00:00
|
|
|
{
|
|
|
|
if (!InterruptManagement::initialized()) {
|
|
|
|
// This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
|
2020-03-08 00:34:46 +00:00
|
|
|
return original_irq;
|
2020-03-06 01:19:40 +00:00
|
|
|
}
|
2020-03-08 00:34:46 +00:00
|
|
|
return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
|
2020-03-06 01:19:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:34:46 +00:00
|
|
|
u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(InterruptManagement::initialized());
|
2020-03-08 00:34:46 +00: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 18:19:38 +00:00
|
|
|
// FIXME: Find a better way to handle conflict with Syscall interrupt gate.
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
|
2020-03-08 00:34:46 +00:00
|
|
|
return original_irq;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
|
2020-03-06 01:19:40 +00:00
|
|
|
{
|
|
|
|
// FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
|
2020-03-08 00:34:46 +00:00
|
|
|
return mapped_interrupt_vector;
|
2020-03-06 01:19:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 12:25:14 +00:00
|
|
|
NonnullLockRefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(IRQControllerType controller_type, u8 interrupt_vector)
|
2022-01-28 16:18:14 +00:00
|
|
|
{
|
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
|
|
|
if (irq_controller->gsi_base() <= interrupt_vector && irq_controller->type() == controller_type)
|
|
|
|
return irq_controller;
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:25:14 +00:00
|
|
|
NonnullLockRefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2020-02-28 16:26:44 +00:00
|
|
|
if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
|
2020-02-28 20:33:41 +00:00
|
|
|
return m_interrupt_controllers[0];
|
2020-02-28 16:26:44 +00:00
|
|
|
}
|
2021-02-15 11:56:39 +00:00
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
2020-03-08 10:47:33 +00:00
|
|
|
if (irq_controller->gsi_base() <= interrupt_vector)
|
2020-02-22 17:36:40 +00:00
|
|
|
if (!irq_controller->is_hard_disabled())
|
2020-02-28 20:33:41 +00:00
|
|
|
return irq_controller;
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 19:50:11 +00:00
|
|
|
UNMAP_AFTER_INIT ErrorOr<Optional<PhysicalAddress>> InterruptManagement::find_madt_physical_address()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("Early access to ACPI tables for interrupt setup");
|
2023-06-09 19:50:11 +00:00
|
|
|
auto possible_rsdp_physical_address = TRY(ACPI::StaticParsing::find_rsdp_in_platform_specific_memory_locations());
|
|
|
|
if (!possible_rsdp_physical_address.has_value())
|
|
|
|
return Optional<PhysicalAddress> {};
|
|
|
|
auto possible_apic_physical_address = TRY(ACPI::StaticParsing::find_table(possible_rsdp_physical_address.value(), "APIC"sv));
|
|
|
|
if (!possible_apic_physical_address.has_value())
|
|
|
|
return Optional<PhysicalAddress> {};
|
|
|
|
return possible_apic_physical_address.value();
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2022-09-25 12:11:28 +00:00
|
|
|
VERIFY(m_interrupt_controllers.is_empty());
|
2021-03-12 13:02:58 +00:00
|
|
|
dmesgln("Interrupts: Switch to Legacy PIC mode");
|
2020-03-06 01:19:40 +00:00
|
|
|
InterruptDisabler disabler;
|
2022-09-25 12:09:10 +00:00
|
|
|
m_interrupt_controllers.append(adopt_lock_ref(*new PIC()));
|
2020-02-28 16:26:44 +00:00
|
|
|
SpuriousInterruptHandler::initialize(7);
|
|
|
|
SpuriousInterruptHandler::initialize(15);
|
2022-09-25 12:11:28 +00:00
|
|
|
dbgln("Interrupts: Detected {}", m_interrupt_controllers[0]->model());
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2021-03-12 13:02:58 +00:00
|
|
|
dmesgln("Interrupts: Switch to IOAPIC mode");
|
2020-03-06 01:19:40 +00:00
|
|
|
InterruptDisabler disabler;
|
2020-04-09 18:17:02 +00:00
|
|
|
|
2023-06-09 19:50:11 +00:00
|
|
|
m_madt_physical_address = MUST(find_madt_physical_address());
|
|
|
|
|
|
|
|
if (!m_madt_physical_address.has_value()) {
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("Interrupts: ACPI MADT is not available, reverting to PIC mode");
|
2020-04-09 18:17:02 +00:00
|
|
|
switch_to_pic_mode();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 19:50:11 +00:00
|
|
|
dbgln("Interrupts: MADT @ P {}", m_madt_physical_address.value().as_ptr());
|
2020-04-09 18:17:02 +00:00
|
|
|
locate_apic_data();
|
2020-02-22 17:36:40 +00:00
|
|
|
if (m_interrupt_controllers.size() == 1) {
|
|
|
|
if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
|
2021-03-12 13:02:58 +00:00
|
|
|
dmesgln("Interrupts: NO IOAPIC detected, Reverting to PIC mode.");
|
2020-02-22 17:36:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& irq_controller : m_interrupt_controllers) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(irq_controller);
|
2020-02-22 17:36:40 +00:00
|
|
|
if (irq_controller->type() == IRQControllerType::i8259) {
|
|
|
|
irq_controller->hard_disable();
|
2021-01-10 14:17:54 +00:00
|
|
|
dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
|
2022-01-28 16:18:14 +00:00
|
|
|
SpuriousInterruptHandler::initialize_for_disabled_master_pic();
|
|
|
|
SpuriousInterruptHandler::initialize_for_disabled_slave_pic();
|
2020-02-22 17:36:40 +00:00
|
|
|
} else {
|
2021-01-10 14:17:54 +00:00
|
|
|
dbgln("Interrupts: Detected {}", irq_controller->model());
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 11:34:53 +00:00
|
|
|
|
|
|
|
if (auto mp_parser = MultiProcessorParser::autodetect()) {
|
|
|
|
m_pci_interrupt_overrides = mp_parser->get_pci_interrupt_redirections();
|
|
|
|
}
|
2020-07-06 13:27:22 +00:00
|
|
|
|
2021-12-25 04:08:58 +00:00
|
|
|
APIC::initialize();
|
2020-07-06 13:27:22 +00:00
|
|
|
APIC::the().init_bsp();
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 17:41:50 +00:00
|
|
|
UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
|
2020-02-22 17:36:40 +00:00
|
|
|
{
|
2023-06-09 19:50:11 +00:00
|
|
|
VERIFY(m_madt_physical_address.has_value());
|
|
|
|
auto madt = Memory::map_typed<ACPI::Structures::MADT>(m_madt_physical_address.value()).release_value_but_fixme_should_propagate_errors();
|
2020-02-22 17:36:40 +00:00
|
|
|
|
2022-09-25 12:09:10 +00:00
|
|
|
if (madt->flags & PCAT_COMPAT_FLAG)
|
|
|
|
m_interrupt_controllers.append(adopt_lock_ref(*new PIC()));
|
2020-02-22 17:36:40 +00:00
|
|
|
size_t entry_index = 0;
|
2020-05-23 13:46:28 +00:00
|
|
|
size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
|
|
|
|
auto* madt_entry = madt->entries;
|
2020-02-22 17:36:40 +00:00
|
|
|
while (entries_length > 0) {
|
|
|
|
size_t entry_length = madt_entry->length;
|
2020-02-28 20:33:41 +00:00
|
|
|
if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
|
|
|
|
auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
|
2021-01-10 14:17:54 +00:00
|
|
|
dbgln("IOAPIC found @ MADT entry {}, MMIO Registers @ {}", entry_index, PhysicalAddress(ioapic_entry->ioapic_address));
|
2022-09-25 12:09:10 +00:00
|
|
|
m_interrupt_controllers.append(adopt_lock_ref(*new IOAPIC(PhysicalAddress(ioapic_entry->ioapic_address), ioapic_entry->gsi_base)));
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
2020-02-28 20:33:41 +00:00
|
|
|
if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
|
|
|
|
auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
|
2021-11-28 17:23:58 +00:00
|
|
|
u32 global_system_interrupt = 0;
|
|
|
|
ByteReader::load<u32>(reinterpret_cast<u8 const*>(&interrupt_override_entry->global_system_interrupt), global_system_interrupt);
|
|
|
|
u16 flags = 0;
|
|
|
|
ByteReader::load<u16>(reinterpret_cast<u8 const*>(&interrupt_override_entry->flags), flags);
|
2022-01-03 11:25:29 +00:00
|
|
|
MUST(m_isa_interrupt_overrides.try_empend(
|
2020-02-22 17:36:40 +00:00
|
|
|
interrupt_override_entry->bus,
|
|
|
|
interrupt_override_entry->source,
|
2021-11-28 17:23:58 +00:00
|
|
|
global_system_interrupt,
|
2022-01-03 11:25:29 +00:00
|
|
|
flags));
|
2021-01-10 14:17:54 +00:00
|
|
|
|
|
|
|
dbgln("Interrupts: Overriding INT {:#x} with GSI {}, for bus {:#x}",
|
|
|
|
interrupt_override_entry->source,
|
2021-11-28 17:23:58 +00:00
|
|
|
global_system_interrupt,
|
2021-11-28 15:38:10 +00:00
|
|
|
interrupt_override_entry->bus);
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|
2020-05-23 11:43:34 +00:00
|
|
|
madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
|
2020-02-22 17:36:40 +00:00
|
|
|
entries_length -= entry_length;
|
|
|
|
entry_index++;
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 19:07:04 +00:00
|
|
|
|
2020-02-22 17:36:40 +00:00
|
|
|
}
|