InterruptManagement.cpp 7.8 KB

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