IOAPIC.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/Optional.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/ACPI/MultiProcessorParser.h>
  29. #include <Kernel/Arch/x86/CPU.h>
  30. #include <Kernel/Debug.h>
  31. #include <Kernel/Interrupts/APIC.h>
  32. #include <Kernel/Interrupts/IOAPIC.h>
  33. #include <Kernel/Interrupts/InterruptManagement.h>
  34. #include <Kernel/VM/MemoryManager.h>
  35. #define IOAPIC_REDIRECTION_ENTRY_OFFSET 0x10
  36. namespace Kernel {
  37. enum DeliveryMode {
  38. Normal = 0,
  39. LowPriority = 1,
  40. SMI = 2,
  41. NMI = 3,
  42. INIT = 4,
  43. External = 7
  44. };
  45. UNMAP_AFTER_INIT IOAPIC::IOAPIC(PhysicalAddress address, u32 gsi_base)
  46. : m_address(address)
  47. , m_regs(map_typed_writable<ioapic_mmio_regs>(m_address))
  48. , m_gsi_base(gsi_base)
  49. , m_id((read_register(0x0) >> 24) & 0xFF)
  50. , m_version(read_register(0x1) & 0xFF)
  51. , m_redirection_entries_count((read_register(0x1) >> 16) + 1)
  52. {
  53. InterruptDisabler disabler;
  54. dmesgln("IOAPIC ID: {:#x}", m_id);
  55. dmesgln("IOAPIC Version: {:#x}, redirection entries: {}", m_version, m_redirection_entries_count);
  56. dmesgln("IOAPIC Arbitration ID {:#x}", read_register(0x2));
  57. mask_all_redirection_entries();
  58. }
  59. UNMAP_AFTER_INIT void IOAPIC::initialize()
  60. {
  61. }
  62. void IOAPIC::map_interrupt_redirection(u8 interrupt_vector)
  63. {
  64. InterruptDisabler disabler;
  65. for (auto redirection_override : InterruptManagement::the().isa_overrides()) {
  66. if (redirection_override.source() != interrupt_vector)
  67. continue;
  68. bool active_low;
  69. // See ACPI spec Version 6.2, page 205 to learn more about Interrupt Overriding Flags.
  70. switch ((redirection_override.flags() & 0b11)) {
  71. case 0:
  72. active_low = false;
  73. break;
  74. case 1:
  75. active_low = false;
  76. break;
  77. case 2:
  78. VERIFY_NOT_REACHED(); // Reserved value
  79. case 3:
  80. active_low = true;
  81. break;
  82. }
  83. bool trigger_level_mode;
  84. // See ACPI spec Version 6.2, page 205 to learn more about Interrupt Overriding Flags.
  85. switch (((redirection_override.flags() >> 2) & 0b11)) {
  86. case 0:
  87. trigger_level_mode = false;
  88. break;
  89. case 1:
  90. trigger_level_mode = false;
  91. break;
  92. case 2:
  93. VERIFY_NOT_REACHED(); // Reserved value
  94. case 3:
  95. trigger_level_mode = true;
  96. break;
  97. }
  98. configure_redirection_entry(redirection_override.gsi() - gsi_base(), InterruptManagement::acquire_mapped_interrupt_number(redirection_override.source()) + IRQ_VECTOR_BASE, DeliveryMode::Normal, false, active_low, trigger_level_mode, true, 0);
  99. return;
  100. }
  101. isa_identity_map(interrupt_vector);
  102. }
  103. void IOAPIC::isa_identity_map(int index)
  104. {
  105. InterruptDisabler disabler;
  106. configure_redirection_entry(index, InterruptManagement::acquire_mapped_interrupt_number(index) + IRQ_VECTOR_BASE, DeliveryMode::Normal, false, false, false, true, 0);
  107. }
  108. void IOAPIC::map_pci_interrupts()
  109. {
  110. InterruptDisabler disabler;
  111. configure_redirection_entry(11, 11 + IRQ_VECTOR_BASE, DeliveryMode::Normal, false, false, true, true, 0);
  112. }
  113. bool IOAPIC::is_enabled() const
  114. {
  115. return !is_hard_disabled();
  116. }
  117. void IOAPIC::spurious_eoi(const GenericInterruptHandler& handler) const
  118. {
  119. InterruptDisabler disabler;
  120. VERIFY(handler.type() == HandlerType::SpuriousInterruptHandler);
  121. VERIFY(handler.interrupt_number() == APIC::spurious_interrupt_vector());
  122. dbgln("IOAPIC: Spurious interrupt");
  123. }
  124. void IOAPIC::map_isa_interrupts()
  125. {
  126. InterruptDisabler disabler;
  127. for (auto redirection_override : InterruptManagement::the().isa_overrides()) {
  128. if ((redirection_override.gsi() < gsi_base()) || (redirection_override.gsi() >= (gsi_base() + m_redirection_entries_count)))
  129. continue;
  130. bool active_low;
  131. // See ACPI spec Version 6.2, page 205 to learn more about Interrupt Overriding Flags.
  132. switch ((redirection_override.flags() & 0b11)) {
  133. case 0:
  134. active_low = false;
  135. break;
  136. case 1:
  137. active_low = false;
  138. break;
  139. case 2:
  140. VERIFY_NOT_REACHED();
  141. case 3:
  142. active_low = true;
  143. break;
  144. }
  145. bool trigger_level_mode;
  146. // See ACPI spec Version 6.2, page 205 to learn more about Interrupt Overriding Flags.
  147. switch (((redirection_override.flags() >> 2) & 0b11)) {
  148. case 0:
  149. trigger_level_mode = false;
  150. break;
  151. case 1:
  152. trigger_level_mode = false;
  153. break;
  154. case 2:
  155. VERIFY_NOT_REACHED();
  156. case 3:
  157. trigger_level_mode = true;
  158. break;
  159. }
  160. configure_redirection_entry(redirection_override.gsi() - gsi_base(), InterruptManagement::acquire_mapped_interrupt_number(redirection_override.source()) + IRQ_VECTOR_BASE, 0, false, active_low, trigger_level_mode, true, 0);
  161. }
  162. }
  163. void IOAPIC::reset_all_redirection_entries() const
  164. {
  165. InterruptDisabler disabler;
  166. for (size_t index = 0; index < m_redirection_entries_count; index++)
  167. reset_redirection_entry(index);
  168. }
  169. void IOAPIC::hard_disable()
  170. {
  171. InterruptDisabler disabler;
  172. reset_all_redirection_entries();
  173. IRQController::hard_disable();
  174. }
  175. void IOAPIC::reset_redirection_entry(int index) const
  176. {
  177. InterruptDisabler disabler;
  178. configure_redirection_entry(index, 0, 0, false, false, false, true, 0);
  179. }
  180. void IOAPIC::configure_redirection_entry(int index, u8 interrupt_vector, u8 delivery_mode, bool logical_destination, bool active_low, bool trigger_level_mode, bool masked, u8 destination) const
  181. {
  182. InterruptDisabler disabler;
  183. VERIFY((u32)index < m_redirection_entries_count);
  184. u32 redirection_entry1 = interrupt_vector | (delivery_mode & 0b111) << 8 | logical_destination << 11 | active_low << 13 | trigger_level_mode << 15 | masked << 16;
  185. u32 redirection_entry2 = destination << 24;
  186. write_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET, redirection_entry1);
  187. if constexpr (IOAPIC_DEBUG)
  188. dbgln("IOAPIC Value: {:#x}", read_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET));
  189. write_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET + 1, redirection_entry2);
  190. if constexpr (IOAPIC_DEBUG)
  191. dbgln("IOAPIC Value: {:#x}", read_register((index << 1) + 0x11));
  192. }
  193. void IOAPIC::mask_all_redirection_entries() const
  194. {
  195. InterruptDisabler disabler;
  196. for (size_t index = 0; index < m_redirection_entries_count; index++)
  197. mask_redirection_entry(index);
  198. }
  199. void IOAPIC::mask_redirection_entry(u8 index) const
  200. {
  201. VERIFY((u32)index < m_redirection_entries_count);
  202. u32 redirection_entry = read_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET);
  203. if (redirection_entry & (1 << 16))
  204. return;
  205. write_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET, redirection_entry | (1 << 16));
  206. }
  207. bool IOAPIC::is_redirection_entry_masked(u8 index) const
  208. {
  209. VERIFY((u32)index < m_redirection_entries_count);
  210. return (read_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET) & (1 << 16)) != 0;
  211. }
  212. void IOAPIC::unmask_redirection_entry(u8 index) const
  213. {
  214. VERIFY((u32)index < m_redirection_entries_count);
  215. u32 redirection_entry = read_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET);
  216. if (!(redirection_entry & (1 << 16)))
  217. return;
  218. write_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET, redirection_entry & ~(1 << 16));
  219. }
  220. bool IOAPIC::is_vector_enabled(u8 interrupt_vector) const
  221. {
  222. InterruptDisabler disabler;
  223. return is_redirection_entry_masked(interrupt_vector);
  224. }
  225. u8 IOAPIC::read_redirection_entry_vector(u8 index) const
  226. {
  227. VERIFY((u32)index < m_redirection_entries_count);
  228. return (read_register((index << 1) + IOAPIC_REDIRECTION_ENTRY_OFFSET) & 0xFF);
  229. }
  230. Optional<int> IOAPIC::find_redirection_entry_by_vector(u8 vector) const
  231. {
  232. InterruptDisabler disabler;
  233. for (size_t index = 0; index < m_redirection_entries_count; index++) {
  234. if (read_redirection_entry_vector(index) == (InterruptManagement::acquire_mapped_interrupt_number(vector) + IRQ_VECTOR_BASE))
  235. return index;
  236. }
  237. return {};
  238. }
  239. void IOAPIC::disable(const GenericInterruptHandler& handler)
  240. {
  241. InterruptDisabler disabler;
  242. VERIFY(!is_hard_disabled());
  243. u8 interrupt_vector = handler.interrupt_number();
  244. VERIFY(interrupt_vector >= gsi_base() && interrupt_vector < interrupt_vectors_count());
  245. auto found_index = find_redirection_entry_by_vector(interrupt_vector);
  246. if (!found_index.has_value()) {
  247. map_interrupt_redirection(interrupt_vector);
  248. found_index = find_redirection_entry_by_vector(interrupt_vector);
  249. }
  250. VERIFY(found_index.has_value());
  251. mask_redirection_entry(found_index.value());
  252. }
  253. void IOAPIC::enable(const GenericInterruptHandler& handler)
  254. {
  255. InterruptDisabler disabler;
  256. VERIFY(!is_hard_disabled());
  257. u8 interrupt_vector = handler.interrupt_number();
  258. VERIFY(interrupt_vector >= gsi_base() && interrupt_vector < interrupt_vectors_count());
  259. auto found_index = find_redirection_entry_by_vector(interrupt_vector);
  260. if (!found_index.has_value()) {
  261. map_interrupt_redirection(interrupt_vector);
  262. found_index = find_redirection_entry_by_vector(interrupt_vector);
  263. }
  264. VERIFY(found_index.has_value());
  265. unmask_redirection_entry(found_index.value());
  266. }
  267. void IOAPIC::eoi(const GenericInterruptHandler& handler) const
  268. {
  269. InterruptDisabler disabler;
  270. VERIFY(!is_hard_disabled());
  271. VERIFY(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  272. VERIFY(handler.type() != HandlerType::SpuriousInterruptHandler);
  273. APIC::the().eoi();
  274. }
  275. u16 IOAPIC::get_isr() const
  276. {
  277. InterruptDisabler disabler;
  278. VERIFY_NOT_REACHED();
  279. }
  280. u16 IOAPIC::get_irr() const
  281. {
  282. InterruptDisabler disabler;
  283. VERIFY_NOT_REACHED();
  284. }
  285. void IOAPIC::write_register(u32 index, u32 value) const
  286. {
  287. InterruptDisabler disabler;
  288. m_regs->select = index;
  289. m_regs->window = value;
  290. dbgln_if(IOAPIC_DEBUG, "IOAPIC Writing, Value {:#x} @ offset {:#x}", (u32)m_regs->window, (u32)m_regs->select);
  291. }
  292. u32 IOAPIC::read_register(u32 index) const
  293. {
  294. InterruptDisabler disabler;
  295. m_regs->select = index;
  296. dbgln_if(IOAPIC_DEBUG, "IOAPIC Reading, Value {:#x} @ offset {:#x}", (u32)m_regs->window, (u32)m_regs->select);
  297. return m_regs->window;
  298. }
  299. }