PIC.cpp 5.9 KB

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