PIC.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/Interrupts/GenericInterruptHandler.h>
  30. #include <Kernel/Interrupts/PIC.h>
  31. #include <LibBareMetal/IO.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(u8 reg)
  51. {
  52. return reg == 0xFF;
  53. }
  54. void PIC::disable(const GenericInterruptHandler& handler)
  55. {
  56. InterruptDisabler disabler;
  57. ASSERT(!is_hard_disabled());
  58. ASSERT(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  59. u8 irq = handler.interrupt_number();
  60. u8 imr;
  61. if (irq >= 8) {
  62. imr = IO::in8(PIC1_CMD);
  63. imr |= 1 << (irq - 8);
  64. IO::out8(PIC1_CMD, imr);
  65. } else {
  66. imr = IO::in8(PIC0_CMD);
  67. imr |= 1 << irq;
  68. IO::out8(PIC0_CMD, imr);
  69. }
  70. if (is_all_masked(imr))
  71. m_enabled = false;
  72. }
  73. PIC::PIC()
  74. {
  75. initialize();
  76. }
  77. void PIC::spurious_eoi(const GenericInterruptHandler& handler) const
  78. {
  79. ASSERT(handler.type() == HandlerType::SpuriousInterruptHandler);
  80. if (handler.interrupt_number() == 15)
  81. eoi_interrupt(7);
  82. }
  83. bool PIC::is_vector_enabled(u8 irq) const
  84. {
  85. u8 imr;
  86. if (irq >= 8) {
  87. imr = IO::in8(PIC1_CMD);
  88. imr &= 1 << (irq - 8);
  89. } else {
  90. imr = IO::in8(PIC0_CMD);
  91. imr &= 1 << irq;
  92. }
  93. return imr != 0;
  94. }
  95. void PIC::enable(const GenericInterruptHandler& handler)
  96. {
  97. InterruptDisabler disabler;
  98. ASSERT(!is_hard_disabled());
  99. ASSERT(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  100. enable_vector(handler.interrupt_number());
  101. }
  102. void PIC::enable_vector(u8 irq)
  103. {
  104. InterruptDisabler disabler;
  105. ASSERT(!is_hard_disabled());
  106. u8 imr;
  107. if (irq >= 8) {
  108. imr = IO::in8(PIC1_CMD);
  109. imr &= ~(1 << (irq - 8));
  110. IO::out8(PIC1_CMD, imr);
  111. } else {
  112. imr = IO::in8(PIC0_CMD);
  113. imr &= ~(1 << irq);
  114. IO::out8(PIC0_CMD, imr);
  115. }
  116. m_enabled = true;
  117. }
  118. void PIC::eoi(const GenericInterruptHandler& handler) const
  119. {
  120. InterruptDisabler disabler;
  121. ASSERT(!is_hard_disabled());
  122. ASSERT(handler.interrupt_number() >= gsi_base() && handler.interrupt_number() < interrupt_vectors_count());
  123. eoi_interrupt(handler.interrupt_number());
  124. }
  125. void PIC::eoi_interrupt(u8 irq) const
  126. {
  127. if (irq >= 8)
  128. IO::out8(PIC1_CTL, 0x20);
  129. IO::out8(PIC0_CTL, 0x20);
  130. }
  131. void PIC::complete_eoi() const
  132. {
  133. IO::out8(PIC1_CTL, 0x20);
  134. IO::out8(PIC0_CTL, 0x20);
  135. }
  136. void PIC::hard_disable()
  137. {
  138. InterruptDisabler disabler;
  139. remap(0x20);
  140. IO::out8(PIC0_CMD, 0xff);
  141. IO::out8(PIC1_CMD, 0xff);
  142. IRQController::hard_disable();
  143. }
  144. void PIC::remap(u8 offset)
  145. {
  146. /* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
  147. IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
  148. IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
  149. /* ICW2 (upper 5 bits specify ISR indices, lower 3 idunno) */
  150. IO::out8(PIC0_CMD, offset);
  151. IO::out8(PIC1_CMD, offset + 0x08);
  152. /* ICW3 (configure master/slave relationship) */
  153. IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
  154. IO::out8(PIC1_CMD, SLAVE_INDEX);
  155. /* ICW4 (set x86 mode) */
  156. IO::out8(PIC0_CMD, 0x01);
  157. IO::out8(PIC1_CMD, 0x01);
  158. // Mask -- start out with all IRQs disabled.
  159. IO::out8(PIC0_CMD, 0xff);
  160. IO::out8(PIC1_CMD, 0xff);
  161. // ...except IRQ2, since that's needed for the master to let through slave interrupts.
  162. enable_vector(2);
  163. }
  164. void PIC::initialize()
  165. {
  166. /* ICW1 (edge triggered mode, cascading controllers, expect ICW4) */
  167. IO::out8(PIC0_CTL, ICW1_INIT | ICW1_ICW4);
  168. IO::out8(PIC1_CTL, ICW1_INIT | ICW1_ICW4);
  169. /* ICW2 (upper 5 bits specify ISR indices, lower 3 idunno) */
  170. IO::out8(PIC0_CMD, IRQ_VECTOR_BASE);
  171. IO::out8(PIC1_CMD, IRQ_VECTOR_BASE + 0x08);
  172. /* ICW3 (configure master/slave relationship) */
  173. IO::out8(PIC0_CMD, 1 << SLAVE_INDEX);
  174. IO::out8(PIC1_CMD, SLAVE_INDEX);
  175. /* ICW4 (set x86 mode) */
  176. IO::out8(PIC0_CMD, 0x01);
  177. IO::out8(PIC1_CMD, 0x01);
  178. // Mask -- start out with all IRQs disabled.
  179. IO::out8(PIC0_CMD, 0xff);
  180. IO::out8(PIC1_CMD, 0xff);
  181. // ...except IRQ2, since that's needed for the master to let through slave interrupts.
  182. enable_vector(2);
  183. klog() << "PIC(i8259): cascading mode, vectors 0x" << String::format("%x", IRQ_VECTOR_BASE) << "-0x" << String::format("%x", IRQ_VECTOR_BASE + 0xf);
  184. }
  185. u16 PIC::get_isr() const
  186. {
  187. IO::out8(PIC0_CTL, 0x0b);
  188. IO::out8(PIC1_CTL, 0x0b);
  189. u8 isr0 = IO::in8(PIC0_CTL);
  190. u8 isr1 = IO::in8(PIC1_CTL);
  191. return (isr1 << 8) | isr0;
  192. }
  193. u16 PIC::get_irr() const
  194. {
  195. IO::out8(PIC0_CTL, 0x0a);
  196. IO::out8(PIC1_CTL, 0x0a);
  197. u8 irr0 = IO::in8(PIC0_CTL);
  198. u8 irr1 = IO::in8(PIC1_CTL);
  199. return (irr1 << 8) | irr0;
  200. }
  201. }