i8259.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Code to handle x86 style IRQs plus some generic interrupt stuff.
  7. *
  8. * Copyright (C) 1992 Linus Torvalds
  9. * Copyright (C) 1994 - 2000 Ralf Baechle
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/irq.h>
  19. #include <asm/i8259.h>
  20. #include <asm/io.h>
  21. /*
  22. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  23. * present in the majority of PC/AT boxes.
  24. * plus some generic x86 specific things if generic specifics makes
  25. * any sense at all.
  26. * this file should become arch/i386/kernel/irq.c when the old irq.c
  27. * moves to arch independent land
  28. */
  29. static int i8259A_auto_eoi = -1;
  30. DEFINE_RAW_SPINLOCK(i8259A_lock);
  31. static void disable_8259A_irq(struct irq_data *d);
  32. static void enable_8259A_irq(struct irq_data *d);
  33. static void mask_and_ack_8259A(struct irq_data *d);
  34. static void init_8259A(int auto_eoi);
  35. static struct irq_chip i8259A_chip = {
  36. .name = "XT-PIC",
  37. .irq_mask = disable_8259A_irq,
  38. .irq_disable = disable_8259A_irq,
  39. .irq_unmask = enable_8259A_irq,
  40. .irq_mask_ack = mask_and_ack_8259A,
  41. };
  42. /*
  43. * 8259A PIC functions to handle ISA devices:
  44. */
  45. /*
  46. * This contains the irq mask for both 8259A irq controllers,
  47. */
  48. static unsigned int cached_irq_mask = 0xffff;
  49. #define cached_master_mask (cached_irq_mask)
  50. #define cached_slave_mask (cached_irq_mask >> 8)
  51. static void disable_8259A_irq(struct irq_data *d)
  52. {
  53. unsigned int mask, irq = d->irq - I8259A_IRQ_BASE;
  54. unsigned long flags;
  55. mask = 1 << irq;
  56. raw_spin_lock_irqsave(&i8259A_lock, flags);
  57. cached_irq_mask |= mask;
  58. if (irq & 8)
  59. outb(cached_slave_mask, PIC_SLAVE_IMR);
  60. else
  61. outb(cached_master_mask, PIC_MASTER_IMR);
  62. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  63. }
  64. static void enable_8259A_irq(struct irq_data *d)
  65. {
  66. unsigned int mask, irq = d->irq - I8259A_IRQ_BASE;
  67. unsigned long flags;
  68. mask = ~(1 << irq);
  69. raw_spin_lock_irqsave(&i8259A_lock, flags);
  70. cached_irq_mask &= mask;
  71. if (irq & 8)
  72. outb(cached_slave_mask, PIC_SLAVE_IMR);
  73. else
  74. outb(cached_master_mask, PIC_MASTER_IMR);
  75. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  76. }
  77. int i8259A_irq_pending(unsigned int irq)
  78. {
  79. unsigned int mask;
  80. unsigned long flags;
  81. int ret;
  82. irq -= I8259A_IRQ_BASE;
  83. mask = 1 << irq;
  84. raw_spin_lock_irqsave(&i8259A_lock, flags);
  85. if (irq < 8)
  86. ret = inb(PIC_MASTER_CMD) & mask;
  87. else
  88. ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
  89. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  90. return ret;
  91. }
  92. void make_8259A_irq(unsigned int irq)
  93. {
  94. disable_irq_nosync(irq);
  95. irq_set_chip_and_handler(irq, &i8259A_chip, handle_level_irq);
  96. enable_irq(irq);
  97. }
  98. /*
  99. * This function assumes to be called rarely. Switching between
  100. * 8259A registers is slow.
  101. * This has to be protected by the irq controller spinlock
  102. * before being called.
  103. */
  104. static inline int i8259A_irq_real(unsigned int irq)
  105. {
  106. int value;
  107. int irqmask = 1 << irq;
  108. if (irq < 8) {
  109. outb(0x0B, PIC_MASTER_CMD); /* ISR register */
  110. value = inb(PIC_MASTER_CMD) & irqmask;
  111. outb(0x0A, PIC_MASTER_CMD); /* back to the IRR register */
  112. return value;
  113. }
  114. outb(0x0B, PIC_SLAVE_CMD); /* ISR register */
  115. value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
  116. outb(0x0A, PIC_SLAVE_CMD); /* back to the IRR register */
  117. return value;
  118. }
  119. /*
  120. * Careful! The 8259A is a fragile beast, it pretty
  121. * much _has_ to be done exactly like this (mask it
  122. * first, _then_ send the EOI, and the order of EOI
  123. * to the two 8259s is important!
  124. */
  125. static void mask_and_ack_8259A(struct irq_data *d)
  126. {
  127. unsigned int irqmask, irq = d->irq - I8259A_IRQ_BASE;
  128. unsigned long flags;
  129. irqmask = 1 << irq;
  130. raw_spin_lock_irqsave(&i8259A_lock, flags);
  131. /*
  132. * Lightweight spurious IRQ detection. We do not want
  133. * to overdo spurious IRQ handling - it's usually a sign
  134. * of hardware problems, so we only do the checks we can
  135. * do without slowing down good hardware unnecessarily.
  136. *
  137. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  138. * usually resulting from the 8259A-1|2 PICs) occur
  139. * even if the IRQ is masked in the 8259A. Thus we
  140. * can check spurious 8259A IRQs without doing the
  141. * quite slow i8259A_irq_real() call for every IRQ.
  142. * This does not cover 100% of spurious interrupts,
  143. * but should be enough to warn the user that there
  144. * is something bad going on ...
  145. */
  146. if (cached_irq_mask & irqmask)
  147. goto spurious_8259A_irq;
  148. cached_irq_mask |= irqmask;
  149. handle_real_irq:
  150. if (irq & 8) {
  151. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  152. outb(cached_slave_mask, PIC_SLAVE_IMR);
  153. outb(0x60+(irq&7), PIC_SLAVE_CMD);/* 'Specific EOI' to slave */
  154. outb(0x60+PIC_CASCADE_IR, PIC_MASTER_CMD); /* 'Specific EOI' to master-IRQ2 */
  155. } else {
  156. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  157. outb(cached_master_mask, PIC_MASTER_IMR);
  158. outb(0x60+irq, PIC_MASTER_CMD); /* 'Specific EOI to master */
  159. }
  160. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  161. return;
  162. spurious_8259A_irq:
  163. /*
  164. * this is the slow path - should happen rarely.
  165. */
  166. if (i8259A_irq_real(irq))
  167. /*
  168. * oops, the IRQ _is_ in service according to the
  169. * 8259A - not spurious, go handle it.
  170. */
  171. goto handle_real_irq;
  172. {
  173. static int spurious_irq_mask;
  174. /*
  175. * At this point we can be sure the IRQ is spurious,
  176. * lets ACK and report it. [once per IRQ]
  177. */
  178. if (!(spurious_irq_mask & irqmask)) {
  179. printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
  180. spurious_irq_mask |= irqmask;
  181. }
  182. atomic_inc(&irq_err_count);
  183. /*
  184. * Theoretically we do not have to handle this IRQ,
  185. * but in Linux this does not cause problems and is
  186. * simpler for us.
  187. */
  188. goto handle_real_irq;
  189. }
  190. }
  191. static void i8259A_resume(void)
  192. {
  193. if (i8259A_auto_eoi >= 0)
  194. init_8259A(i8259A_auto_eoi);
  195. }
  196. static void i8259A_shutdown(void)
  197. {
  198. /* Put the i8259A into a quiescent state that
  199. * the kernel initialization code can get it
  200. * out of.
  201. */
  202. if (i8259A_auto_eoi >= 0) {
  203. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  204. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  205. }
  206. }
  207. static struct syscore_ops i8259_syscore_ops = {
  208. .resume = i8259A_resume,
  209. .shutdown = i8259A_shutdown,
  210. };
  211. static int __init i8259A_init_sysfs(void)
  212. {
  213. register_syscore_ops(&i8259_syscore_ops);
  214. return 0;
  215. }
  216. device_initcall(i8259A_init_sysfs);
  217. static void init_8259A(int auto_eoi)
  218. {
  219. unsigned long flags;
  220. i8259A_auto_eoi = auto_eoi;
  221. raw_spin_lock_irqsave(&i8259A_lock, flags);
  222. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  223. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  224. /*
  225. * outb_p - this has to work on a wide range of PC hardware.
  226. */
  227. outb_p(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  228. outb_p(I8259A_IRQ_BASE + 0, PIC_MASTER_IMR); /* ICW2: 8259A-1 IR0 mapped to I8259A_IRQ_BASE + 0x00 */
  229. outb_p(1U << PIC_CASCADE_IR, PIC_MASTER_IMR); /* 8259A-1 (the master) has a slave on IR2 */
  230. if (auto_eoi) /* master does Auto EOI */
  231. outb_p(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  232. else /* master expects normal EOI */
  233. outb_p(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  234. outb_p(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  235. outb_p(I8259A_IRQ_BASE + 8, PIC_SLAVE_IMR); /* ICW2: 8259A-2 IR0 mapped to I8259A_IRQ_BASE + 0x08 */
  236. outb_p(PIC_CASCADE_IR, PIC_SLAVE_IMR); /* 8259A-2 is a slave on master's IR2 */
  237. outb_p(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR); /* (slave's support for AEOI in flat mode is to be investigated) */
  238. if (auto_eoi)
  239. /*
  240. * In AEOI mode we just have to mask the interrupt
  241. * when acking.
  242. */
  243. i8259A_chip.irq_mask_ack = disable_8259A_irq;
  244. else
  245. i8259A_chip.irq_mask_ack = mask_and_ack_8259A;
  246. udelay(100); /* wait for 8259A to initialize */
  247. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  248. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  249. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  250. }
  251. /*
  252. * IRQ2 is cascade interrupt to second interrupt controller
  253. */
  254. static struct irqaction irq2 = {
  255. .handler = no_action,
  256. .name = "cascade",
  257. .flags = IRQF_NO_THREAD,
  258. };
  259. static struct resource pic1_io_resource = {
  260. .name = "pic1",
  261. .start = PIC_MASTER_CMD,
  262. .end = PIC_MASTER_IMR,
  263. .flags = IORESOURCE_BUSY
  264. };
  265. static struct resource pic2_io_resource = {
  266. .name = "pic2",
  267. .start = PIC_SLAVE_CMD,
  268. .end = PIC_SLAVE_IMR,
  269. .flags = IORESOURCE_BUSY
  270. };
  271. /*
  272. * On systems with i8259-style interrupt controllers we assume for
  273. * driver compatibility reasons interrupts 0 - 15 to be the i8259
  274. * interrupts even if the hardware uses a different interrupt numbering.
  275. */
  276. void __init init_i8259_irqs(void)
  277. {
  278. int i;
  279. insert_resource(&ioport_resource, &pic1_io_resource);
  280. insert_resource(&ioport_resource, &pic2_io_resource);
  281. init_8259A(0);
  282. for (i = I8259A_IRQ_BASE; i < I8259A_IRQ_BASE + 16; i++) {
  283. irq_set_chip_and_handler(i, &i8259A_chip, handle_level_irq);
  284. irq_set_probe(i);
  285. }
  286. setup_irq(I8259A_IRQ_BASE + PIC_CASCADE_IR, &irq2);
  287. }