i8259.c 11 KB

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