InterruptManagement.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/InterruptDisabler.h>
  9. #include <Kernel/Arch/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(IRQControllerType controller_type, u8 interrupt_vector)
  88. {
  89. for (auto& irq_controller : m_interrupt_controllers) {
  90. if (irq_controller->gsi_base() <= interrupt_vector && irq_controller->type() == controller_type)
  91. return irq_controller;
  92. }
  93. VERIFY_NOT_REACHED();
  94. }
  95. RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
  96. {
  97. if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
  98. return m_interrupt_controllers[0];
  99. }
  100. for (auto& irq_controller : m_interrupt_controllers) {
  101. if (irq_controller->gsi_base() <= interrupt_vector)
  102. if (!irq_controller->is_hard_disabled())
  103. return irq_controller;
  104. }
  105. VERIFY_NOT_REACHED();
  106. }
  107. UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt()
  108. {
  109. dbgln("Early access to ACPI tables for interrupt setup");
  110. auto rsdp = ACPI::StaticParsing::find_rsdp();
  111. if (!rsdp.has_value())
  112. return {};
  113. auto apic = ACPI::StaticParsing::find_table(rsdp.value(), "APIC");
  114. if (!apic.has_value())
  115. return {};
  116. return apic.value();
  117. }
  118. UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
  119. : m_madt(search_for_madt())
  120. {
  121. m_interrupt_controllers.resize(1);
  122. }
  123. UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
  124. {
  125. dmesgln("Interrupts: Switch to Legacy PIC mode");
  126. InterruptDisabler disabler;
  127. m_smp_enabled = false;
  128. m_interrupt_controllers[0] = adopt_ref(*new PIC());
  129. SpuriousInterruptHandler::initialize(7);
  130. SpuriousInterruptHandler::initialize(15);
  131. for (auto& irq_controller : m_interrupt_controllers) {
  132. VERIFY(irq_controller);
  133. if (irq_controller->type() == IRQControllerType::i82093AA) {
  134. irq_controller->hard_disable();
  135. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  136. } else {
  137. dbgln("Interrupts: Detected {}", irq_controller->model());
  138. }
  139. }
  140. }
  141. UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
  142. {
  143. dmesgln("Interrupts: Switch to IOAPIC mode");
  144. InterruptDisabler disabler;
  145. if (m_madt.is_null()) {
  146. dbgln("Interrupts: ACPI MADT is not available, reverting to PIC mode");
  147. switch_to_pic_mode();
  148. return;
  149. }
  150. dbgln("Interrupts: MADT @ P {}", m_madt.as_ptr());
  151. locate_apic_data();
  152. m_smp_enabled = true;
  153. if (m_interrupt_controllers.size() == 1) {
  154. if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
  155. dmesgln("Interrupts: NO IOAPIC detected, Reverting to PIC mode.");
  156. return;
  157. }
  158. }
  159. for (auto& irq_controller : m_interrupt_controllers) {
  160. VERIFY(irq_controller);
  161. if (irq_controller->type() == IRQControllerType::i8259) {
  162. irq_controller->hard_disable();
  163. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  164. SpuriousInterruptHandler::initialize_for_disabled_master_pic();
  165. SpuriousInterruptHandler::initialize_for_disabled_slave_pic();
  166. } else {
  167. dbgln("Interrupts: Detected {}", irq_controller->model());
  168. }
  169. }
  170. if (auto mp_parser = MultiProcessorParser::autodetect()) {
  171. m_pci_interrupt_overrides = mp_parser->get_pci_interrupt_redirections();
  172. }
  173. APIC::initialize();
  174. APIC::the().init_bsp();
  175. }
  176. UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
  177. {
  178. VERIFY(!m_madt.is_null());
  179. auto madt = Memory::map_typed<ACPI::Structures::MADT>(m_madt).release_value_but_fixme_should_propagate_errors();
  180. int irq_controller_count = 0;
  181. if (madt->flags & PCAT_COMPAT_FLAG) {
  182. m_interrupt_controllers[0] = adopt_ref(*new PIC());
  183. irq_controller_count++;
  184. }
  185. size_t entry_index = 0;
  186. size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
  187. auto* madt_entry = madt->entries;
  188. while (entries_length > 0) {
  189. size_t entry_length = madt_entry->length;
  190. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
  191. auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
  192. dbgln("IOAPIC found @ MADT entry {}, MMIO Registers @ {}", entry_index, PhysicalAddress(ioapic_entry->ioapic_address));
  193. m_interrupt_controllers.resize(1 + irq_controller_count);
  194. m_interrupt_controllers[irq_controller_count] = adopt_ref(*new IOAPIC(PhysicalAddress(ioapic_entry->ioapic_address), ioapic_entry->gsi_base));
  195. irq_controller_count++;
  196. }
  197. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
  198. auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
  199. u32 global_system_interrupt = 0;
  200. ByteReader::load<u32>(reinterpret_cast<u8 const*>(&interrupt_override_entry->global_system_interrupt), global_system_interrupt);
  201. u16 flags = 0;
  202. ByteReader::load<u16>(reinterpret_cast<u8 const*>(&interrupt_override_entry->flags), flags);
  203. MUST(m_isa_interrupt_overrides.try_empend(
  204. interrupt_override_entry->bus,
  205. interrupt_override_entry->source,
  206. global_system_interrupt,
  207. flags));
  208. dbgln("Interrupts: Overriding INT {:#x} with GSI {}, for bus {:#x}",
  209. interrupt_override_entry->source,
  210. global_system_interrupt,
  211. interrupt_override_entry->bus);
  212. }
  213. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
  214. entries_length -= entry_length;
  215. entry_index++;
  216. }
  217. }
  218. }