i8259.c 9.0 KB

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