InterruptManagement.cpp 9.5 KB

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