i8259.c 9.3 KB

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