i8259.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <asm/acpi.h>
  15. #include <asm/atomic.h>
  16. #include <asm/system.h>
  17. #include <asm/io.h>
  18. #include <asm/timer.h>
  19. #include <asm/hw_irq.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/delay.h>
  22. #include <asm/desc.h>
  23. #include <asm/apic.h>
  24. #include <asm/arch_hooks.h>
  25. #include <asm/i8259.h>
  26. /*
  27. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  28. * present in the majority of PC/AT boxes.
  29. * plus some generic x86 specific things if generic specifics makes
  30. * any sense at all.
  31. */
  32. static int i8259A_auto_eoi;
  33. DEFINE_SPINLOCK(i8259A_lock);
  34. static void mask_and_ack_8259A(unsigned int);
  35. struct irq_chip i8259A_chip = {
  36. .name = "XT-PIC",
  37. .mask = disable_8259A_irq,
  38. .disable = disable_8259A_irq,
  39. .unmask = enable_8259A_irq,
  40. .mask_ack = mask_and_ack_8259A,
  41. };
  42. /*
  43. * 8259A PIC functions to handle ISA devices:
  44. */
  45. /*
  46. * This contains the irq mask for both 8259A irq controllers,
  47. */
  48. unsigned int cached_irq_mask = 0xffff;
  49. /*
  50. * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
  51. * boards the timer interrupt is not really connected to any IO-APIC pin,
  52. * it's fed to the master 8259A's IR0 line only.
  53. *
  54. * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
  55. * this 'mixed mode' IRQ handling costs nothing because it's only used
  56. * at IRQ setup time.
  57. */
  58. unsigned long io_apic_irqs;
  59. void disable_8259A_irq(unsigned int irq)
  60. {
  61. unsigned int mask = 1 << irq;
  62. unsigned long flags;
  63. spin_lock_irqsave(&i8259A_lock, flags);
  64. cached_irq_mask |= mask;
  65. if (irq & 8)
  66. outb(cached_slave_mask, PIC_SLAVE_IMR);
  67. else
  68. outb(cached_master_mask, PIC_MASTER_IMR);
  69. spin_unlock_irqrestore(&i8259A_lock, flags);
  70. }
  71. void enable_8259A_irq(unsigned int irq)
  72. {
  73. unsigned int mask = ~(1 << irq);
  74. unsigned long flags;
  75. spin_lock_irqsave(&i8259A_lock, flags);
  76. cached_irq_mask &= mask;
  77. if (irq & 8)
  78. outb(cached_slave_mask, PIC_SLAVE_IMR);
  79. else
  80. outb(cached_master_mask, PIC_MASTER_IMR);
  81. spin_unlock_irqrestore(&i8259A_lock, flags);
  82. }
  83. int i8259A_irq_pending(unsigned int irq)
  84. {
  85. unsigned int mask = 1<<irq;
  86. unsigned long flags;
  87. int ret;
  88. 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. spin_unlock_irqrestore(&i8259A_lock, flags);
  94. return ret;
  95. }
  96. void make_8259A_irq(unsigned int irq)
  97. {
  98. disable_irq_nosync(irq);
  99. io_apic_irqs &= ~(1<<irq);
  100. set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
  101. "XT");
  102. enable_irq(irq);
  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(unsigned int irq)
  132. {
  133. unsigned int irqmask = 1 << irq;
  134. unsigned long flags;
  135. spin_lock_irqsave(&i8259A_lock, flags);
  136. /*
  137. * Lightweight spurious IRQ detection. We do not want
  138. * to overdo spurious IRQ handling - it's usually a sign
  139. * of hardware problems, so we only do the checks we can
  140. * do without slowing down good hardware unnecessarily.
  141. *
  142. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  143. * usually resulting from the 8259A-1|2 PICs) occur
  144. * even if the IRQ is masked in the 8259A. Thus we
  145. * can check spurious 8259A IRQs without doing the
  146. * quite slow i8259A_irq_real() call for every IRQ.
  147. * This does not cover 100% of spurious interrupts,
  148. * but should be enough to warn the user that there
  149. * is something bad going on ...
  150. */
  151. if (cached_irq_mask & irqmask)
  152. goto spurious_8259A_irq;
  153. cached_irq_mask |= irqmask;
  154. handle_real_irq:
  155. if (irq & 8) {
  156. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  157. outb(cached_slave_mask, PIC_SLAVE_IMR);
  158. #ifndef CONFIG_X86_64
  159. outb(0x60+(irq&7),PIC_SLAVE_CMD);/* 'Specific EOI' to slave */
  160. outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD); /* 'Specific EOI' to master-IRQ2 */
  161. #else /* CONFIG_X86_64 */
  162. /* 'Specific EOI' to slave */
  163. outb(0x60+(irq&7),PIC_SLAVE_CMD);
  164. /* 'Specific EOI' to master-IRQ2 */
  165. outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD);
  166. #endif /* CONFIG_X86_64 */
  167. } else {
  168. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  169. outb(cached_master_mask, PIC_MASTER_IMR);
  170. #ifndef CONFIG_X86_64
  171. outb(0x60+irq,PIC_MASTER_CMD); /* 'Specific EOI to master */
  172. #else /* CONFIG_X86_64 */
  173. /* 'Specific EOI' to master */
  174. outb(0x60+irq,PIC_MASTER_CMD);
  175. #endif /* CONFIG_X86_64 */
  176. }
  177. spin_unlock_irqrestore(&i8259A_lock, flags);
  178. return;
  179. spurious_8259A_irq:
  180. /*
  181. * this is the slow path - should happen rarely.
  182. */
  183. if (i8259A_irq_real(irq))
  184. /*
  185. * oops, the IRQ _is_ in service according to the
  186. * 8259A - not spurious, go handle it.
  187. */
  188. goto handle_real_irq;
  189. {
  190. static int spurious_irq_mask;
  191. /*
  192. * At this point we can be sure the IRQ is spurious,
  193. * lets ACK and report it. [once per IRQ]
  194. */
  195. if (!(spurious_irq_mask & irqmask)) {
  196. #ifndef CONFIG_X86_64
  197. printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
  198. #else /* CONFIG_X86_64 */
  199. printk(KERN_DEBUG
  200. "spurious 8259A interrupt: IRQ%d.\n", irq);
  201. #endif /* CONFIG_X86_64 */
  202. spurious_irq_mask |= irqmask;
  203. }
  204. atomic_inc(&irq_err_count);
  205. /*
  206. * Theoretically we do not have to handle this IRQ,
  207. * but in Linux this does not cause problems and is
  208. * simpler for us.
  209. */
  210. goto handle_real_irq;
  211. }
  212. }
  213. static char irq_trigger[2];
  214. /**
  215. * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
  216. */
  217. static void restore_ELCR(char *trigger)
  218. {
  219. outb(trigger[0], 0x4d0);
  220. outb(trigger[1], 0x4d1);
  221. }
  222. static void save_ELCR(char *trigger)
  223. {
  224. /* IRQ 0,1,2,8,13 are marked as reserved */
  225. trigger[0] = inb(0x4d0) & 0xF8;
  226. trigger[1] = inb(0x4d1) & 0xDE;
  227. }
  228. static int i8259A_resume(struct sys_device *dev)
  229. {
  230. init_8259A(i8259A_auto_eoi);
  231. restore_ELCR(irq_trigger);
  232. return 0;
  233. }
  234. static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
  235. {
  236. save_ELCR(irq_trigger);
  237. return 0;
  238. }
  239. static int i8259A_shutdown(struct sys_device *dev)
  240. {
  241. /* Put the i8259A into a quiescent state that
  242. * the kernel initialization code can get it
  243. * out of.
  244. */
  245. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  246. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
  247. return 0;
  248. }
  249. static struct sysdev_class i8259_sysdev_class = {
  250. .name = "i8259",
  251. .suspend = i8259A_suspend,
  252. .resume = i8259A_resume,
  253. .shutdown = i8259A_shutdown,
  254. };
  255. static struct sys_device device_i8259A = {
  256. .id = 0,
  257. .cls = &i8259_sysdev_class,
  258. };
  259. static int __init i8259A_init_sysfs(void)
  260. {
  261. int error = sysdev_class_register(&i8259_sysdev_class);
  262. if (!error)
  263. error = sysdev_register(&device_i8259A);
  264. return error;
  265. }
  266. device_initcall(i8259A_init_sysfs);
  267. void init_8259A(int auto_eoi)
  268. {
  269. unsigned long flags;
  270. i8259A_auto_eoi = auto_eoi;
  271. spin_lock_irqsave(&i8259A_lock, flags);
  272. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  273. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  274. /*
  275. * outb_pic - this has to work on a wide range of PC hardware.
  276. */
  277. outb_pic(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  278. #ifndef CONFIG_X86_64
  279. outb_pic(0x20 + 0, PIC_MASTER_IMR); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
  280. outb_pic(1U << PIC_CASCADE_IR, PIC_MASTER_IMR); /* 8259A-1 (the master) has a slave on IR2 */
  281. #else /* CONFIG_X86_64 */
  282. /* ICW2: 8259A-1 IR0-7 mapped to 0x30-0x37 */
  283. outb_pic(IRQ0_VECTOR, PIC_MASTER_IMR);
  284. /* 8259A-1 (the master) has a slave on IR2 */
  285. outb_pic(0x04, PIC_MASTER_IMR);
  286. #endif /* CONFIG_X86_64 */
  287. if (auto_eoi) /* master does Auto EOI */
  288. outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  289. else /* master expects normal EOI */
  290. outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  291. outb_pic(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  292. #ifndef CONFIG_X86_64
  293. outb_pic(0x20 + 8, PIC_SLAVE_IMR); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
  294. outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR); /* 8259A-2 is a slave on master's IR2 */
  295. outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR); /* (slave's support for AEOI in flat mode is to be investigated) */
  296. #else /* CONFIG_X86_64 */
  297. /* ICW2: 8259A-2 IR0-7 mapped to 0x38-0x3f */
  298. outb_pic(IRQ8_VECTOR, PIC_SLAVE_IMR);
  299. /* 8259A-2 is a slave on master's IR2 */
  300. outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR);
  301. /* (slave's support for AEOI in flat mode is to be investigated) */
  302. outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
  303. #endif /* CONFIG_X86_64 */
  304. if (auto_eoi)
  305. /*
  306. * In AEOI mode we just have to mask the interrupt
  307. * when acking.
  308. */
  309. i8259A_chip.mask_ack = disable_8259A_irq;
  310. else
  311. i8259A_chip.mask_ack = mask_and_ack_8259A;
  312. udelay(100); /* wait for 8259A to initialize */
  313. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  314. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  315. spin_unlock_irqrestore(&i8259A_lock, flags);
  316. }