IOAPIC.cpp 12 KB

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