i8259.c 10 KB

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