IOAPIC.cpp 12 KB

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