InterruptManagement.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/FixedArray.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/ACPI/MultiProcessorParser.h>
  29. #include <Kernel/Arch/i386/CPU.h>
  30. #include <Kernel/CommandLine.h>
  31. #include <Kernel/Interrupts/APIC.h>
  32. #include <Kernel/Interrupts/IOAPIC.h>
  33. #include <Kernel/Interrupts/InterruptManagement.h>
  34. #include <Kernel/Interrupts/PIC.h>
  35. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  36. #include <Kernel/Interrupts/UnhandledInterruptHandler.h>
  37. #include <Kernel/Syscall.h>
  38. #include <Kernel/VM/MemoryManager.h>
  39. #include <LibBareMetal/IO.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. ASSERT(InterruptManagement::initialized());
  50. return *s_interrupt_management;
  51. }
  52. void InterruptManagement::initialize()
  53. {
  54. ASSERT(!InterruptManagement::initialized());
  55. s_interrupt_management = new InterruptManagement();
  56. if (kernel_command_line().lookup("smp").value_or("off") == "on")
  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. ASSERT(index >= 0);
  72. ASSERT(!m_interrupt_controllers[index].is_null());
  73. return *m_interrupt_controllers[index];
  74. }
  75. Vector<RefPtr<ISAInterruptOverrideMetadata>> InterruptManagement::isa_overrides()
  76. {
  77. return m_isa_interrupt_overrides;
  78. }
  79. u8 InterruptManagement::acquire_mapped_interrupt_number(u8 original_irq)
  80. {
  81. if (!InterruptManagement::initialized()) {
  82. // This is necessary, because we install UnhandledInterruptHandlers before we actually initialize the Interrupt Management object...
  83. return original_irq;
  84. }
  85. return InterruptManagement::the().get_mapped_interrupt_vector(original_irq);
  86. }
  87. u8 InterruptManagement::acquire_irq_number(u8 mapped_interrupt_vector)
  88. {
  89. ASSERT(InterruptManagement::initialized());
  90. return InterruptManagement::the().get_irq_vector(mapped_interrupt_vector);
  91. }
  92. u8 InterruptManagement::get_mapped_interrupt_vector(u8 original_irq)
  93. {
  94. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  95. // FIXME: Find a better way to handle conflict with Syscall interrupt gate.
  96. ASSERT((original_irq + IRQ_VECTOR_BASE) != syscall_vector);
  97. return original_irq;
  98. }
  99. u8 InterruptManagement::get_irq_vector(u8 mapped_interrupt_vector)
  100. {
  101. // FIXME: For SMP configuration (with IOAPICs) use a better routing scheme to make redirections more efficient.
  102. return mapped_interrupt_vector;
  103. }
  104. RefPtr<IRQController> InterruptManagement::get_responsible_irq_controller(u8 interrupt_vector)
  105. {
  106. if (m_interrupt_controllers.size() == 1 && m_interrupt_controllers[0]->type() == IRQControllerType::i8259) {
  107. return m_interrupt_controllers[0];
  108. }
  109. for (auto irq_controller : m_interrupt_controllers) {
  110. if (irq_controller->gsi_base() <= interrupt_vector)
  111. if (!irq_controller->is_hard_disabled())
  112. return irq_controller;
  113. }
  114. ASSERT_NOT_REACHED();
  115. }
  116. PhysicalAddress InterruptManagement::search_for_madt()
  117. {
  118. dbg() << "Early access to ACPI tables for interrupt setup";
  119. auto rsdp = ACPI::StaticParsing::find_rsdp();
  120. if (rsdp.is_null())
  121. return {};
  122. return ACPI::StaticParsing::find_table(rsdp, "APIC");
  123. }
  124. InterruptManagement::InterruptManagement()
  125. : m_madt(search_for_madt())
  126. {
  127. if (m_madt.is_null()) {
  128. m_interrupt_controllers[0] = adopt(*new PIC());
  129. return;
  130. }
  131. // FIXME: Check what is the actual data size then map accordingly
  132. dbg() << "Interrupts: MADT @ P " << m_madt.as_ptr();
  133. locate_apic_data();
  134. }
  135. void InterruptManagement::switch_to_pic_mode()
  136. {
  137. klog() << "Interrupts: Switch to Legacy PIC mode";
  138. InterruptDisabler disabler;
  139. m_smp_enabled = false;
  140. SpuriousInterruptHandler::initialize(7);
  141. SpuriousInterruptHandler::initialize(15);
  142. for (auto& irq_controller : m_interrupt_controllers) {
  143. ASSERT(irq_controller);
  144. if (irq_controller->type() == IRQControllerType::i82093AA) {
  145. irq_controller->hard_disable();
  146. dbg() << "Interrupts: Detected " << irq_controller->model() << " - Disabled";
  147. } else {
  148. dbg() << "Interrupts: Detected " << irq_controller->model();
  149. }
  150. }
  151. }
  152. void InterruptManagement::switch_to_ioapic_mode()
  153. {
  154. klog() << "Interrupts: Switch to IOAPIC mode";
  155. InterruptDisabler disabler;
  156. m_smp_enabled = true;
  157. if (m_interrupt_controllers.size() == 1) {
  158. if (get_interrupt_controller(0).type() == IRQControllerType::i8259) {
  159. klog() << "Interrupts: NO IOAPIC detected, Reverting to PIC mode.";
  160. return;
  161. }
  162. }
  163. for (auto& irq_controller : m_interrupt_controllers) {
  164. ASSERT(irq_controller);
  165. if (irq_controller->type() == IRQControllerType::i8259) {
  166. irq_controller->hard_disable();
  167. dbg() << "Interrupts: Detected " << irq_controller->model() << " Disabled";
  168. } else {
  169. dbg() << "Interrupts: Detected " << irq_controller->model();
  170. }
  171. }
  172. APIC::init();
  173. APIC::enable_bsp();
  174. MultiProcessorParser::initialize();
  175. }
  176. void InterruptManagement::locate_apic_data()
  177. {
  178. ASSERT(!m_madt.is_null());
  179. auto region = MM.allocate_kernel_region(m_madt.page_base(), (PAGE_SIZE * 2), "Initializing Interrupts", Region::Access::Read);
  180. auto& madt = *(const ACPI::Structures::MADT*)region->vaddr().offset(m_madt.offset_in_page()).as_ptr();
  181. int irq_controller_count = 0;
  182. if (madt.flags & PCAT_COMPAT_FLAG) {
  183. m_interrupt_controllers[0] = adopt(*new PIC());
  184. irq_controller_count++;
  185. }
  186. size_t entry_index = 0;
  187. size_t entries_length = madt.h.length - sizeof(ACPI::Structures::MADT);
  188. auto* madt_entry = madt.entries;
  189. while (entries_length > 0) {
  190. size_t entry_length = madt_entry->length;
  191. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::IOAPIC) {
  192. auto* ioapic_entry = (const ACPI::Structures::MADTEntries::IOAPIC*)madt_entry;
  193. dbg() << "IOAPIC found @ MADT entry " << entry_index << ", MMIO Registers @ Px" << String::format("%x", ioapic_entry->ioapic_address);
  194. m_interrupt_controllers.resize(1 + irq_controller_count);
  195. m_interrupt_controllers[irq_controller_count] = adopt(*new IOAPIC(*(ioapic_mmio_regs*)ioapic_entry->ioapic_address, ioapic_entry->gsi_base));
  196. irq_controller_count++;
  197. }
  198. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::InterruptSourceOverride) {
  199. auto* interrupt_override_entry = (const ACPI::Structures::MADTEntries::InterruptSourceOverride*)madt_entry;
  200. m_isa_interrupt_overrides.append(adopt(*new ISAInterruptOverrideMetadata(
  201. interrupt_override_entry->bus,
  202. interrupt_override_entry->source,
  203. interrupt_override_entry->global_system_interrupt,
  204. interrupt_override_entry->flags)));
  205. dbg() << "Interrupts: Overriding INT 0x" << String::format("%x", interrupt_override_entry->source) << " with GSI " << interrupt_override_entry->global_system_interrupt << ", for bus 0x" << String::format("%x", interrupt_override_entry->bus);
  206. }
  207. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress((u32)madt_entry).offset(entry_length).get());
  208. entries_length -= entry_length;
  209. entry_index++;
  210. }
  211. }
  212. void InterruptManagement::locate_pci_interrupt_overrides()
  213. {
  214. // FIXME: calling the MultiProcessorParser causes a pagefault.
  215. ASSERT_NOT_REACHED();
  216. m_pci_interrupt_overrides = MultiProcessorParser::the().get_pci_interrupt_redirections();
  217. }
  218. ISAInterruptOverrideMetadata::ISAInterruptOverrideMetadata(u8 bus, u8 source, u32 global_system_interrupt, u16 flags)
  219. : m_bus(bus)
  220. , m_source(source)
  221. , m_global_system_interrupt(global_system_interrupt)
  222. , m_flags(flags)
  223. {
  224. }
  225. u8 ISAInterruptOverrideMetadata::bus() const
  226. {
  227. return m_bus;
  228. }
  229. u8 ISAInterruptOverrideMetadata::source() const
  230. {
  231. return m_source;
  232. }
  233. u32 ISAInterruptOverrideMetadata::gsi() const
  234. {
  235. return m_global_system_interrupt;
  236. }
  237. u16 ISAInterruptOverrideMetadata::flags() const
  238. {
  239. return m_flags;
  240. }
  241. }