PIC.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/Assertions.h>
  27. #include <AK/Types.h>
  28. #include <Kernel/Arch/i386/CPU.h>
  29. #include <Kernel/IO.h>
  30. #include <Kernel/Interrupts/GenericInterruptHandler.h>
  31. #include <Kernel/Interrupts/PIC.h>
  32. namespace Kernel {
  33. // The slave 8259 is connected to the master's IRQ2 line.
  34. // This is really only to enhance clarity.
  35. #define SLAVE_INDEX 2
  36. #define PIC0_CTL 0x20
  37. #define PIC0_CMD 0x21
  38. #define PIC1_CTL 0xA0
  39. #define PIC1_CMD 0xA1
  40. #define ICW1_ICW4 0x01 /* ICW4 (not) needed */
  41. #define ICW1_SINGLE 0x02 /* Single (cascade) mode */
  42. #define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
  43. #define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
  44. #define ICW1_INIT 0x10 /* Initialization - required! */
  45. #define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
  46. #define ICW4_AUTO 0x02 /* Auto (normal) EOI */
  47. #define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
  48. #define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
  49. #define ICW4_SFNM 0x10 /* Special fully nested (not) */
  50. bool inline static is_all_masked(u16 reg)
  51. {
  52. return reg == 0xFFFF;
  53. }
  54. bool PIC::is_enabled() const
  55. {
  56. return !is_all_masked(m_cached_irq_mask) && !is_hard_disabled();
  57. }
  58. void PIC::disable(const GenericInterruptHandler& handler)
  59. {
  60. InterruptDisabler disabler;
  61. VERIFY(!is_hard_disabled());
  62. VERIFY(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  63. u8 irq = handler.interrupt_number();
  64. if (m_cached_irq_mask & (1 << irq))
  65. return;
  66. u8 imr;
  67. if (irq & 8) {
  68. imr = IO::in8(PIC1_CMD);
  69. imr |= 1 << (irq & 7);
  70. IO::out8(PIC1_CMD, imr);
  71. } else {
  72. imr = IO::in8(PIC0_CMD);
  73. imr |= 1 << irq;
  74. IO::out8(PIC0_CMD, imr);
  75. }
  76. m_cached_irq_mask |= 1 << irq;
  77. }
  78. UNMAP_AFTER_INIT PIC::PIC()
  79. {
  80. initialize();
  81. }
  82. void PIC::spurious_eoi(const GenericInterruptHandler& handler) const
  83. {
  84. VERIFY(handler.type() == HandlerType::SpuriousInterruptHandler);
  85. if (handler.interrupt_number() == 7)
  86. return;
  87. if (handler.interrupt_number() == 15) {
  88. IO::in8(PIC1_CMD); /* dummy read */
  89. IO::out8(PIC0_CTL, 0x60 | (2));
  90. }
  91. }
  92. bool PIC::is_vector_enabled(u8 irq) const
  93. {
  94. return m_cached_irq_mask & (1 << irq);
  95. }
  96. void PIC::enable(const GenericInterruptHandler& handler)
  97. {
  98. InterruptDisabler disabler;
  99. VERIFY(!is_hard_disabled());
  100. VERIFY(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  101. enable_vector(handler.interrupt_number());
  102. }
  103. void PIC::enable_vector(u8 irq)
  104. {
  105. InterruptDisabler disabler;
  106. VERIFY(!is_hard_disabled());
  107. if (!(m_cached_irq_mask & (1 << irq)))
  108. return;
  109. u8 imr;
  110. if (irq & 8) {
  111. imr = IO::in8(PIC1_CMD);
  112. imr &= ~(1 << (irq & 7));
  113. IO::out8(PIC1_CMD, imr);
  114. } else {
  115. imr = IO::in8(PIC0_CMD);
  116. imr &= ~(1 << irq);
  117. IO::out8(PIC0_CMD, imr);
  118. }
  119. m_cached_irq_mask &= ~(1 << irq);
  120. }
  121. void PIC::eoi(const GenericInterruptHandler& handler) const
  122. {
  123. InterruptDisabler disabler;
  124. VERIFY(!is_hard_disabled());
  125. u8 irq = handler.interrupt_number();
  126. VERIFY(irq >= gsi_base() && irq < interrupt_vectors_count());
  127. if ((1 << irq) & m_cached_irq_mask) {
  128. spurious_eoi(handler);
  129. return;
  130. }
  131. eoi_interrupt(irq);
  132. }
  133. void PIC::eoi_interrupt(u8 irq) const
  134. {
  135. if (irq & 8) {
  136. IO::in8(PIC1_CMD); /* dummy read */
  137. IO::out8(PIC1_CTL, 0x60 | (irq & 7));
  138. IO::out8(PIC0_CTL, 0x60 | (2));
  139. return;
  140. }
  141. IO::in8(PIC0_CMD); /* dummy read */
  142. IO::out8(PIC0_CTL, 0x60 | irq);
  143. }
  144. void PIC::complete_eoi() const
  145. {
  146. IO::out8(PIC1_CTL, 0x20);
  147. IO::out8(PIC0_CTL, 0x20);
  148. }
  149. void PIC::hard_disable()
  150. {
  151. InterruptDisabler disabler;
  152. remap(0x20);
  153. IO::out8(PIC0_CMD, 0xff);
  154. IO::out8(PIC1_CMD, 0xff);
  155. m_cached_irq_mask = 0xffff;
  156. IRQController::hard_disable();
  157. }
  158. void PIC::remap(u8 offset)
  159. {
  160. /* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
  161. IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
  162. IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
  163. /* ICW2 (upper 5 bits specify ISR indices, lower 3 idunno) */
  164. IO::out8(PIC0_CMD, offset);
  165. IO::out8(PIC1_CMD, offset + 0x08);
  166. /* ICW3 (configure master/slave relationship) */
  167. IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
  168. IO::out8(PIC1_CMD, SLAVE_INDEX);
  169. /* ICW4 (set x86 mode) */
  170. IO::out8(PIC0_CMD, ICW4_8086);
  171. IO::out8(PIC1_CMD, ICW4_8086);
  172. // Mask -- start out with all IRQs disabled.
  173. IO::out8(PIC0_CMD, 0xff);
  174. IO::out8(PIC1_CMD, 0xff);
  175. m_cached_irq_mask = 0xffff;
  176. // ...except IRQ2, since that's needed for the master to let through slave interrupts.
  177. enable_vector(2);
  178. }
  179. UNMAP_AFTER_INIT void PIC::initialize()
  180. {
  181. /* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
  182. IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
  183. IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
  184. /* ICW2 (upper 5 bits specify ISR indices, lower 3 idunno) */
  185. IO::out8(PIC0_CMD, IRQ_VECTOR_BASE);
  186. IO::out8(PIC1_CMD, IRQ_VECTOR_BASE + 0x08);
  187. /* ICW3 (configure master/slave relationship) */
  188. IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
  189. IO::out8(PIC1_CMD, SLAVE_INDEX);
  190. /* ICW4 (set x86 mode) */
  191. IO::out8(PIC0_CMD, ICW4_8086);
  192. IO::out8(PIC1_CMD, ICW4_8086);
  193. // Mask -- start out with all IRQs disabled.
  194. IO::out8(PIC0_CMD, 0xff);
  195. IO::out8(PIC1_CMD, 0xff);
  196. // ...except IRQ2, since that's needed for the master to let through slave interrupts.
  197. enable_vector(2);
  198. klog() << "PIC(i8259): cascading mode, vectors 0x" << String::format("%x", IRQ_VECTOR_BASE) << "-0x" << String::format("%x", IRQ_VECTOR_BASE + 0xf);
  199. }
  200. u16 PIC::get_isr() const
  201. {
  202. IO::out8(PIC0_CTL, 0x0b);
  203. IO::out8(PIC1_CTL, 0x0b);
  204. u8 isr0 = IO::in8(PIC0_CTL);
  205. u8 isr1 = IO::in8(PIC1_CTL);
  206. return (isr1 << 8) | isr0;
  207. }
  208. u16 PIC::get_irr() const
  209. {
  210. IO::out8(PIC0_CTL, 0x0a);
  211. IO::out8(PIC1_CTL, 0x0a);
  212. u8 irr0 = IO::in8(PIC0_CTL);
  213. u8 irr1 = IO::in8(PIC1_CTL);
  214. return (irr1 << 8) | irr0;
  215. }
  216. }