i8259.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/linkage.h>
  3. #include <linux/errno.h>
  4. #include <linux/signal.h>
  5. #include <linux/sched.h>
  6. #include <linux/ioport.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/timex.h>
  10. #include <linux/random.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/syscore_ops.h>
  14. #include <linux/bitops.h>
  15. #include <linux/acpi.h>
  16. #include <linux/io.h>
  17. #include <linux/delay.h>
  18. #include <linux/atomic.h>
  19. #include <asm/timer.h>
  20. #include <asm/hw_irq.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/desc.h>
  23. #include <asm/apic.h>
  24. #include <asm/i8259.h>
  25. /*
  26. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  27. * present in the majority of PC/AT boxes.
  28. * plus some generic x86 specific things if generic specifics makes
  29. * any sense at all.
  30. */
  31. static void init_8259A(int auto_eoi);
  32. static int i8259A_auto_eoi;
  33. DEFINE_RAW_SPINLOCK(i8259A_lock);
  34. /*
  35. * 8259A PIC functions to handle ISA devices:
  36. */
  37. /*
  38. * This contains the irq mask for both 8259A irq controllers,
  39. */
  40. unsigned int cached_irq_mask = 0xffff;
  41. /*
  42. * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
  43. * boards the timer interrupt is not really connected to any IO-APIC pin,
  44. * it's fed to the master 8259A's IR0 line only.
  45. *
  46. * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
  47. * this 'mixed mode' IRQ handling costs nothing because it's only used
  48. * at IRQ setup time.
  49. */
  50. unsigned long io_apic_irqs;
  51. static void mask_8259A_irq(unsigned int irq)
  52. {
  53. unsigned int mask = 1 << irq;
  54. unsigned long flags;
  55. raw_spin_lock_irqsave(&i8259A_lock, flags);
  56. cached_irq_mask |= mask;
  57. if (irq & 8)
  58. outb(cached_slave_mask, PIC_SLAVE_IMR);
  59. else
  60. outb(cached_master_mask, PIC_MASTER_IMR);
  61. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  62. }
  63. static void disable_8259A_irq(struct irq_data *data)
  64. {
  65. mask_8259A_irq(data->irq);
  66. }
  67. static void unmask_8259A_irq(unsigned int irq)
  68. {
  69. unsigned int mask = ~(1 << irq);
  70. unsigned long flags;
  71. raw_spin_lock_irqsave(&i8259A_lock, flags);
  72. cached_irq_mask &= mask;
  73. if (irq & 8)
  74. outb(cached_slave_mask, PIC_SLAVE_IMR);
  75. else
  76. outb(cached_master_mask, PIC_MASTER_IMR);
  77. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  78. }
  79. static void enable_8259A_irq(struct irq_data *data)
  80. {
  81. unmask_8259A_irq(data->irq);
  82. }
  83. static int i8259A_irq_pending(unsigned int irq)
  84. {
  85. unsigned int mask = 1<<irq;
  86. unsigned long flags;
  87. int ret;
  88. raw_spin_lock_irqsave(&i8259A_lock, flags);
  89. if (irq < 8)
  90. ret = inb(PIC_MASTER_CMD) & mask;
  91. else
  92. ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
  93. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  94. return ret;
  95. }
  96. static void make_8259A_irq(unsigned int irq)
  97. {
  98. disable_irq_nosync(irq);
  99. io_apic_irqs &= ~(1<<irq);
  100. irq_set_chip_and_handler(irq, &i8259A_chip, handle_level_irq);
  101. enable_irq(irq);
  102. lapic_assign_legacy_vector(irq, true);
  103. }
  104. /*
  105. * This function assumes to be called rarely. Switching between
  106. * 8259A registers is slow.
  107. * This has to be protected by the irq controller spinlock
  108. * before being called.
  109. */
  110. static inline int i8259A_irq_real(unsigned int irq)
  111. {
  112. int value;
  113. int irqmask = 1<<irq;
  114. if (irq < 8) {
  115. outb(0x0B, PIC_MASTER_CMD); /* ISR register */
  116. value = inb(PIC_MASTER_CMD) & irqmask;
  117. outb(0x0A, PIC_MASTER_CMD); /* back to the IRR register */
  118. return value;
  119. }
  120. outb(0x0B, PIC_SLAVE_CMD); /* ISR register */
  121. value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
  122. outb(0x0A, PIC_SLAVE_CMD); /* back to the IRR register */
  123. return value;
  124. }
  125. /*
  126. * Careful! The 8259A is a fragile beast, it pretty
  127. * much _has_ to be done exactly like this (mask it
  128. * first, _then_ send the EOI, and the order of EOI
  129. * to the two 8259s is important!
  130. */
  131. static void mask_and_ack_8259A(struct irq_data *data)
  132. {
  133. unsigned int irq = data->irq;
  134. unsigned int irqmask = 1 << irq;
  135. unsigned long flags;
  136. raw_spin_lock_irqsave(&i8259A_lock, flags);
  137. /*
  138. * Lightweight spurious IRQ detection. We do not want
  139. * to overdo spurious IRQ handling - it's usually a sign
  140. * of hardware problems, so we only do the checks we can
  141. * do without slowing down good hardware unnecessarily.
  142. *
  143. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  144. * usually resulting from the 8259A-1|2 PICs) occur
  145. * even if the IRQ is masked in the 8259A. Thus we
  146. * can check spurious 8259A IRQs without doing the
  147. * quite slow i8259A_irq_real() call for every IRQ.
  148. * This does not cover 100% of spurious interrupts,
  149. * but should be enough to warn the user that there
  150. * is something bad going on ...
  151. */
  152. if (cached_irq_mask & irqmask)
  153. goto spurious_8259A_irq;
  154. cached_irq_mask |= irqmask;
  155. handle_real_irq:
  156. if (irq & 8) {
  157. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  158. outb(cached_slave_mask, PIC_SLAVE_IMR);
  159. /* 'Specific EOI' to slave */
  160. outb(0x60+(irq&7), PIC_SLAVE_CMD);
  161. /* 'Specific EOI' to master-IRQ2 */
  162. outb(0x60+PIC_CASCADE_IR, PIC_MASTER_CMD);
  163. } else {
  164. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  165. outb(cached_master_mask, PIC_MASTER_IMR);
  166. outb(0x60+irq, PIC_MASTER_CMD); /* 'Specific EOI to master */
  167. }
  168. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  169. return;
  170. spurious_8259A_irq:
  171. /*
  172. * this is the slow path - should happen rarely.
  173. */
  174. if (i8259A_irq_real(irq))
  175. /*
  176. * oops, the IRQ _is_ in service according to the
  177. * 8259A - not spurious, go handle it.
  178. */
  179. goto handle_real_irq;
  180. {
  181. static int spurious_irq_mask;
  182. /*
  183. * At this point we can be sure the IRQ is spurious,
  184. * lets ACK and report it. [once per IRQ]
  185. */
  186. if (!(spurious_irq_mask & irqmask)) {
  187. printk_deferred(KERN_DEBUG
  188. "spurious 8259A interrupt: IRQ%d.\n", irq);
  189. spurious_irq_mask |= irqmask;
  190. }
  191. atomic_inc(&irq_err_count);
  192. /*
  193. * Theoretically we do not have to handle this IRQ,
  194. * but in Linux this does not cause problems and is
  195. * simpler for us.
  196. */
  197. goto handle_real_irq;
  198. }
  199. }
  200. struct irq_chip i8259A_chip = {
  201. .name = "XT-PIC",
  202. .irq_mask = disable_8259A_irq,
  203. .irq_disable = disable_8259A_irq,
  204. .irq_unmask = enable_8259A_irq,
  205. .irq_mask_ack = mask_and_ack_8259A,
  206. };
  207. static char irq_trigger[2];
  208. /**
  209. * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
  210. */
  211. static void restore_ELCR(char *trigger)
  212. {
  213. outb(trigger[0], 0x4d0);
  214. outb(trigger[1], 0x4d1);
  215. }
  216. static void save_ELCR(char *trigger)
  217. {
  218. /* IRQ 0,1,2,8,13 are marked as reserved */
  219. trigger[0] = inb(0x4d0) & 0xF8;
  220. trigger[1] = inb(0x4d1) & 0xDE;
  221. }
  222. static void i8259A_resume(void)
  223. {
  224. init_8259A(i8259A_auto_eoi);
  225. restore_ELCR(irq_trigger);
  226. }
  227. static int i8259A_suspend(void)
  228. {
  229. save_ELCR(irq_trigger);
  230. return 0;
  231. }
  232. static void i8259A_shutdown(void)
  233. {
  234. /* Put the i8259A into a quiescent state that
  235. * the kernel initialization code can get it
  236. * out of.
  237. */
  238. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  239. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  240. }
  241. static struct syscore_ops i8259_syscore_ops = {
  242. .suspend = i8259A_suspend,
  243. .resume = i8259A_resume,
  244. .shutdown = i8259A_shutdown,
  245. };
  246. static void mask_8259A(void)
  247. {
  248. unsigned long flags;
  249. raw_spin_lock_irqsave(&i8259A_lock, flags);
  250. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  251. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  252. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  253. }
  254. static void unmask_8259A(void)
  255. {
  256. unsigned long flags;
  257. raw_spin_lock_irqsave(&i8259A_lock, flags);
  258. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  259. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  260. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  261. }
  262. static int probe_8259A(void)
  263. {
  264. unsigned long flags;
  265. unsigned char probe_val = ~(1 << PIC_CASCADE_IR);
  266. unsigned char new_val;
  267. /*
  268. * Check to see if we have a PIC.
  269. * Mask all except the cascade and read
  270. * back the value we just wrote. If we don't
  271. * have a PIC, we will read 0xff as opposed to the
  272. * value we wrote.
  273. */
  274. raw_spin_lock_irqsave(&i8259A_lock, flags);
  275. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  276. outb(probe_val, PIC_MASTER_IMR);
  277. new_val = inb(PIC_MASTER_IMR);
  278. if (new_val != probe_val) {
  279. printk(KERN_INFO "Using NULL legacy PIC\n");
  280. legacy_pic = &null_legacy_pic;
  281. }
  282. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  283. return nr_legacy_irqs();
  284. }
  285. static void init_8259A(int auto_eoi)
  286. {
  287. unsigned long flags;
  288. i8259A_auto_eoi = auto_eoi;
  289. raw_spin_lock_irqsave(&i8259A_lock, flags);
  290. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  291. /*
  292. * outb_pic - this has to work on a wide range of PC hardware.
  293. */
  294. outb_pic(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  295. /* ICW2: 8259A-1 IR0-7 mapped to ISA_IRQ_VECTOR(0) */
  296. outb_pic(ISA_IRQ_VECTOR(0), PIC_MASTER_IMR);
  297. /* 8259A-1 (the master) has a slave on IR2 */
  298. outb_pic(1U << PIC_CASCADE_IR, PIC_MASTER_IMR);
  299. if (auto_eoi) /* master does Auto EOI */
  300. outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  301. else /* master expects normal EOI */
  302. outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  303. outb_pic(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  304. /* ICW2: 8259A-2 IR0-7 mapped to ISA_IRQ_VECTOR(8) */
  305. outb_pic(ISA_IRQ_VECTOR(8), PIC_SLAVE_IMR);
  306. /* 8259A-2 is a slave on master's IR2 */
  307. outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR);
  308. /* (slave's support for AEOI in flat mode is to be investigated) */
  309. outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
  310. if (auto_eoi)
  311. /*
  312. * In AEOI mode we just have to mask the interrupt
  313. * when acking.
  314. */
  315. i8259A_chip.irq_mask_ack = disable_8259A_irq;
  316. else
  317. i8259A_chip.irq_mask_ack = mask_and_ack_8259A;
  318. udelay(100); /* wait for 8259A to initialize */
  319. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  320. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  321. raw_spin_unlock_irqrestore(&i8259A_lock, flags);
  322. }
  323. /*
  324. * make i8259 a driver so that we can select pic functions at run time. the goal
  325. * is to make x86 binary compatible among pc compatible and non-pc compatible
  326. * platforms, such as x86 MID.
  327. */
  328. static void legacy_pic_noop(void) { };
  329. static void legacy_pic_uint_noop(unsigned int unused) { };
  330. static void legacy_pic_int_noop(int unused) { };
  331. static int legacy_pic_irq_pending_noop(unsigned int irq)
  332. {
  333. return 0;
  334. }
  335. static int legacy_pic_probe(void)
  336. {
  337. return 0;
  338. }
  339. struct legacy_pic null_legacy_pic = {
  340. .nr_legacy_irqs = 0,
  341. .chip = &dummy_irq_chip,
  342. .mask = legacy_pic_uint_noop,
  343. .unmask = legacy_pic_uint_noop,
  344. .mask_all = legacy_pic_noop,
  345. .restore_mask = legacy_pic_noop,
  346. .init = legacy_pic_int_noop,
  347. .probe = legacy_pic_probe,
  348. .irq_pending = legacy_pic_irq_pending_noop,
  349. .make_irq = legacy_pic_uint_noop,
  350. };
  351. struct legacy_pic default_legacy_pic = {
  352. .nr_legacy_irqs = NR_IRQS_LEGACY,
  353. .chip = &i8259A_chip,
  354. .mask = mask_8259A_irq,
  355. .unmask = unmask_8259A_irq,
  356. .mask_all = mask_8259A,
  357. .restore_mask = unmask_8259A,
  358. .init = init_8259A,
  359. .probe = probe_8259A,
  360. .irq_pending = i8259A_irq_pending,
  361. .make_irq = make_8259A_irq,
  362. };
  363. struct legacy_pic *legacy_pic = &default_legacy_pic;
  364. EXPORT_SYMBOL(legacy_pic);
  365. static int __init i8259A_init_ops(void)
  366. {
  367. if (legacy_pic == &default_legacy_pic)
  368. register_syscore_ops(&i8259_syscore_ops);
  369. return 0;
  370. }
  371. device_initcall(i8259A_init_ops);