InterruptManagement.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteReader.h>
  7. #include <Kernel/API/Syscall.h>
  8. #include <Kernel/Arch/x86/InterruptDisabler.h>
  9. #include <Kernel/Arch/x86/Interrupts.h>
  10. #include <Kernel/CommandLine.h>
  11. #include <Kernel/Firmware/MultiProcessor/Parser.h>
  12. #include <Kernel/Interrupts/APIC.h>
  13. #include <Kernel/Interrupts/IOAPIC.h>
  14. #include <Kernel/Interrupts/InterruptManagement.h>
  15. #include <Kernel/Interrupts/PIC.h>
  16. #include <Kernel/Interrupts/SharedIRQHandler.h>
  17. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  18. #include <Kernel/Memory/TypedMapping.h>
  19. #include <Kernel/Sections.h>
  20. #define PCAT_COMPAT_FLAG 0x1
  21. namespace Kernel {
  22. static InterruptManagement* s_interrupt_management;
  23. bool InterruptManagement::initialized()
  24. {
  25. return (s_interrupt_management != nullptr);
  26. }
  27. InterruptManagement& InterruptManagement::the()
  28. {
  29. VERIFY(InterruptManagement::initialized());
  30. return *s_interrupt_management;
  31. }
  32. UNMAP_AFTER_INIT void InterruptManagement::initialize()
  33. {
  34. VERIFY(!InterruptManagement::initialized());
  35. s_interrupt_management = new InterruptManagement();
  36. if (!kernel_command_line().is_smp_enabled_without_ioapic_enabled()) {
  37. dbgln("Can't enable SMP mode without IOAPIC mode being enabled");
  38. }
  39. if (!kernel_command_line().is_ioapic_enabled() && !kernel_command_line().is_smp_enabled())
  40. InterruptManagement::the().switch_to_pic_mode();
  41. else
  42. InterruptManagement::the().switch_to_ioapic_mode();
  43. }
  44. void InterruptManagement::enumerate_interrupt_handlers(Function<void(GenericInterruptHandler&)> callback)
  45. {
  46. for (int i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; i++) {
  47. auto& handler = get_interrupt_handler(i);
  48. if (handler.type() == HandlerType::SharedIRQHandler) {
  49. static_cast<SharedIRQHandler&>(handler).enumerate_handlers(callback);
  50. continue;
  51. }
  52. if (handler.type() != HandlerType::UnhandledInterruptHandler)
  53. callback(handler);
  54. }
  55. }
  56. IRQController& InterruptManagement::get_interrupt_controller(int index)
  57. {
  58. VERIFY(index >= 0);
  59. VERIFY(!m_interrupt_controllers[index].is_null());
  60. return *m_interrupt_controllers[index];
  61. }
  62. u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
  63. {
  64. if (!InterruptManagement::initialized()) {
  65. // This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
  66. return original_irq;
  67. }
  68. return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
  69. }
  70. u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
  71. {
  72. VERIFY(InterruptManagement::initialized());
  73. return InterruptManagement::the().get_irq_vector(mapped_interrupt_vector);
  74. }
  75. u8 InterruptManagement::get_mapped_interrupt_vector(u8 original_irq)
  76. {
  77. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  78. // FIXME: Find a better way to handle conflict with Syscall interrupt gate.
  79. VERIFY((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
  80. return original_irq;
  81. }
  82. u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
  83. {
  84. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  85. return mapped_interrupt_vector;
  86. }
  87. RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
  88. {
  89. if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
  90. return m_interrupt_controllers[0];
  91. }
  92. for (auto& irq_controller : m_interrupt_controllers) {
  93. if (irq_controller->gsi_base() <= interrupt_vector)
  94. if (!irq_controller->is_hard_disabled())
  95. return irq_controller;
  96. }
  97. VERIFY_NOT_REACHED();
  98. }
  99. UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt()
  100. {
  101. dbgln("Early access to ACPI tables for interrupt setup");
  102. auto rsdp = ACPI::StaticParsing::find_rsdp();
  103. if (!rsdp.has_value())
  104. return {};
  105. auto apic = ACPI::StaticParsing::find_table(rsdp.value(), "APIC");
  106. if (!apic.has_value())
  107. return {};
  108. return apic.value();
  109. }
  110. UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
  111. : m_madt(search_for_madt())
  112. {
  113. m_interrupt_controllers.resize(1);
  114. }
  115. UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
  116. {
  117. dmesgln("Interrupts: Switch to Legacy PIC mode");
  118. InterruptDisabler disabler;
  119. m_smp_enabled = false;
  120. m_interrupt_controllers[0] = adopt_ref(*new PIC());
  121. SpuriousInterruptHandler::initialize(7);
  122. SpuriousInterruptHandler::initialize(15);
  123. for (auto& irq_controller : m_interrupt_controllers) {
  124. VERIFY(irq_controller);
  125. if (irq_controller->type() == IRQControllerType::i82093AA) {
  126. irq_controller->hard_disable();
  127. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  128. } else {
  129. dbgln("Interrupts: Detected {}", irq_controller->model());
  130. }
  131. }
  132. }
  133. UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
  134. {
  135. dmesgln("Interrupts: Switch to IOAPIC mode");
  136. InterruptDisabler disabler;
  137. if (m_madt.is_null()) {
  138. dbgln("Interrupts: ACPI MADT is not available, reverting to PIC mode");
  139. switch_to_pic_mode();
  140. return;
  141. }
  142. dbgln("Interrupts: MADT @ P {}", m_madt.as_ptr());
  143. locate_apic_data();
  144. m_smp_enabled = true;
  145. if (m_interrupt_controllers.size() == 1) {
  146. if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
  147. dmesgln("Interrupts: NO IOAPIC detected, Reverting to PIC mode.");
  148. return;
  149. }
  150. }
  151. for (auto& irq_controller : m_interrupt_controllers) {
  152. VERIFY(irq_controller);
  153. if (irq_controller->type() == IRQControllerType::i8259) {
  154. irq_controller->hard_disable();
  155. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  156. } else {
  157. dbgln("Interrupts: Detected {}", irq_controller->model());
  158. }
  159. }
  160. if (auto mp_parser = MultiProcessorParser::autodetect()) {
  161. m_pci_interrupt_overrides = mp_parser->get_pci_interrupt_redirections();
  162. }
  163. APIC::initialize();
  164. APIC::the().init_bsp();
  165. }
  166. UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
  167. {
  168. VERIFY(!m_madt.is_null());
  169. auto madt = Memory::map_typed<ACPI::Structures::MADT>(m_madt);
  170. int irq_controller_count = 0;
  171. if (madt->flags & PCAT_COMPAT_FLAG) {
  172. m_interrupt_controllers[0] = adopt_ref(*new PIC());
  173. irq_controller_count++;
  174. }
  175. size_t entry_index = 0;
  176. size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
  177. auto* madt_entry = madt->entries;
  178. while (entries_length > 0) {
  179. size_t entry_length = madt_entry->length;
  180. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
  181. auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
  182. dbgln("IOAPIC found @ MADT entry {}, MMIO Registers @ {}", entry_index, PhysicalAddress(ioapic_entry->ioapic_address));
  183. m_interrupt_controllers.resize(1 + irq_controller_count);
  184. m_interrupt_controllers[irq_controller_count] = adopt_ref(*new IOAPIC(PhysicalAddress(ioapic_entry->ioapic_address), ioapic_entry->gsi_base));
  185. irq_controller_count++;
  186. }
  187. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
  188. auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
  189. u32 global_system_interrupt = 0;
  190. ByteReader::load<u32>(reinterpret_cast<u8 const*>(&interrupt_override_entry->global_system_interrupt), global_system_interrupt);
  191. u16 flags = 0;
  192. ByteReader::load<u16>(reinterpret_cast<u8 const*>(&interrupt_override_entry->flags), flags);
  193. m_isa_interrupt_overrides.empend(
  194. interrupt_override_entry->bus,
  195. interrupt_override_entry->source,
  196. global_system_interrupt,
  197. flags);
  198. dbgln("Interrupts: Overriding INT {:#x} with GSI {}, for bus {:#x}",
  199. interrupt_override_entry->source,
  200. global_system_interrupt,
  201. interrupt_override_entry->bus);
  202. }
  203. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
  204. entries_length -= entry_length;
  205. entry_index++;
  206. }
  207. }
  208. }