i8259.c 12 KB

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