InterruptManagement.cpp 8.0 KB

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