InterruptManagement.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringView.h>
  27. #include <Kernel/ACPI/MultiProcessorParser.h>
  28. #include <Kernel/API/Syscall.h>
  29. #include <Kernel/Arch/i386/CPU.h>
  30. #include <Kernel/CommandLine.h>
  31. #include <Kernel/IO.h>
  32. #include <Kernel/Interrupts/APIC.h>
  33. #include <Kernel/Interrupts/IOAPIC.h>
  34. #include <Kernel/Interrupts/InterruptManagement.h>
  35. #include <Kernel/Interrupts/PIC.h>
  36. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  37. #include <Kernel/Interrupts/UnhandledInterruptHandler.h>
  38. #include <Kernel/VM/MemoryManager.h>
  39. #include <Kernel/VM/TypedMapping.h>
  40. #define PCAT_COMPAT_FLAG 0x1
  41. namespace Kernel {
  42. static InterruptManagement* s_interrupt_management;
  43. bool InterruptManagement::initialized()
  44. {
  45. return (s_interrupt_management != nullptr);
  46. }
  47. InterruptManagement& InterruptManagement::the()
  48. {
  49. VERIFY(InterruptManagement::initialized());
  50. return *s_interrupt_management;
  51. }
  52. UNMAP_AFTER_INIT void InterruptManagement::initialize()
  53. {
  54. VERIFY(!InterruptManagement::initialized());
  55. s_interrupt_management = new InterruptManagement();
  56. if (kernel_command_line().is_smp_enabled())
  57. InterruptManagement::the().switch_to_ioapic_mode();
  58. else
  59. InterruptManagement::the().switch_to_pic_mode();
  60. }
  61. void InterruptManagement::enumerate_interrupt_handlers(Function<void(GenericInterruptHandler&)> callback)
  62. {
  63. for (int i = 0; i < GENERIC_INTERRUPT_HANDLERS_COUNT; i++) {
  64. auto& handler = get_interrupt_handler(i);
  65. if (handler.type() != HandlerType::UnhandledInterruptHandler)
  66. callback(handler);
  67. }
  68. }
  69. IRQController& InterruptManagement::get_interrupt_controller(int index)
  70. {
  71. VERIFY(index >= 0);
  72. VERIFY(!m_interrupt_controllers[index].is_null());
  73. return *m_interrupt_controllers[index];
  74. }
  75. u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
  76. {
  77. if (!InterruptManagement::initialized()) {
  78. // This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
  79. return original_irq;
  80. }
  81. return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
  82. }
  83. u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
  84. {
  85. VERIFY(InterruptManagement::initialized());
  86. return InterruptManagement::the().get_irq_vector(mapped_interrupt_vector);
  87. }
  88. u8 InterruptManagement::get_mapped_interrupt_vector(u8 original_irq)
  89. {
  90. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  91. // FIXME: Find a better way to handle conflict with Syscall interrupt gate.
  92. VERIFY((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
  93. return original_irq;
  94. }
  95. u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
  96. {
  97. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  98. return mapped_interrupt_vector;
  99. }
  100. RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
  101. {
  102. if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
  103. return m_interrupt_controllers[0];
  104. }
  105. for (auto& irq_controller : m_interrupt_controllers) {
  106. if (irq_controller->gsi_base() <= interrupt_vector)
  107. if (!irq_controller->is_hard_disabled())
  108. return irq_controller;
  109. }
  110. VERIFY_NOT_REACHED();
  111. }
  112. UNMAP_AFTER_INIT PhysicalAddress InterruptManagement::search_for_madt()
  113. {
  114. dbgln("Early access to ACPI tables for interrupt setup");
  115. auto rsdp = ACPI::StaticParsing::find_rsdp();
  116. if (!rsdp.has_value())
  117. return {};
  118. return ACPI::StaticParsing::find_table(rsdp.value(), "APIC");
  119. }
  120. UNMAP_AFTER_INIT InterruptManagement::InterruptManagement()
  121. : m_madt(search_for_madt())
  122. {
  123. m_interrupt_controllers.resize(1);
  124. }
  125. UNMAP_AFTER_INIT void InterruptManagement::switch_to_pic_mode()
  126. {
  127. dmesgln("Interrupts: Switch to Legacy PIC mode");
  128. InterruptDisabler disabler;
  129. m_smp_enabled = false;
  130. m_interrupt_controllers[0] = adopt(*new PIC());
  131. SpuriousInterruptHandler::initialize(7);
  132. SpuriousInterruptHandler::initialize(15);
  133. for (auto& irq_controller : m_interrupt_controllers) {
  134. VERIFY(irq_controller);
  135. if (irq_controller->type() == IRQControllerType::i82093AA) {
  136. irq_controller->hard_disable();
  137. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  138. } else {
  139. dbgln("Interrupts: Detected {}", irq_controller->model());
  140. }
  141. }
  142. }
  143. UNMAP_AFTER_INIT void InterruptManagement::switch_to_ioapic_mode()
  144. {
  145. dmesgln("Interrupts: Switch to IOAPIC mode");
  146. InterruptDisabler disabler;
  147. if (m_madt.is_null()) {
  148. dbgln("Interrupts: ACPI MADT is not available, reverting to PIC mode");
  149. switch_to_pic_mode();
  150. return;
  151. }
  152. dbgln("Interrupts: MADT @ P {}", m_madt.as_ptr());
  153. locate_apic_data();
  154. m_smp_enabled = true;
  155. if (m_interrupt_controllers.size() == 1) {
  156. if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
  157. dmesgln("Interrupts: NO IOAPIC detected, Reverting to PIC mode.");
  158. return;
  159. }
  160. }
  161. for (auto& irq_controller : m_interrupt_controllers) {
  162. VERIFY(irq_controller);
  163. if (irq_controller->type() == IRQControllerType::i8259) {
  164. irq_controller->hard_disable();
  165. dbgln("Interrupts: Detected {} - Disabled", irq_controller->model());
  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::the().init_bsp();
  174. }
  175. UNMAP_AFTER_INIT void InterruptManagement::locate_apic_data()
  176. {
  177. VERIFY(!m_madt.is_null());
  178. auto madt = map_typed<ACPI::Structures::MADT>(m_madt);
  179. int irq_controller_count = 0;
  180. if (madt->flags & PCAT_COMPAT_FLAG) {
  181. m_interrupt_controllers[0] = adopt(*new PIC());
  182. irq_controller_count++;
  183. }
  184. size_t entry_index = 0;
  185. size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
  186. auto* madt_entry = madt->entries;
  187. while (entries_length > 0) {
  188. size_t entry_length = madt_entry->length;
  189. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
  190. auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
  191. dbgln("IOAPIC found @ MADT entry {}, MMIO Registers @ {}", entry_index, PhysicalAddress(ioapic_entry->ioapic_address));
  192. m_interrupt_controllers.resize(1 + irq_controller_count);
  193. m_interrupt_controllers[irq_controller_count] = adopt(*new IOAPIC(PhysicalAddress(ioapic_entry->ioapic_address), ioapic_entry->gsi_base));
  194. irq_controller_count++;
  195. }
  196. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
  197. auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
  198. m_isa_interrupt_overrides.empend(
  199. interrupt_override_entry->bus,
  200. interrupt_override_entry->source,
  201. interrupt_override_entry->global_system_interrupt,
  202. interrupt_override_entry->flags);
  203. dbgln("Interrupts: Overriding INT {:#x} with GSI {}, for bus {:#x}",
  204. interrupt_override_entry->source,
  205. interrupt_override_entry->global_system_interrupt,
  206. interrupt_override_entry->bus);
  207. }
  208. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
  209. entries_length -= entry_length;
  210. entry_index++;
  211. }
  212. }
  213. }