i8259.c 10 KB

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