i8259_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include <linux/errno.h>
  2. #include <linux/signal.h>
  3. #include <linux/sched.h>
  4. #include <linux/ioport.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/slab.h>
  7. #include <linux/random.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel_stat.h>
  10. #include <linux/sysdev.h>
  11. #include <linux/bitops.h>
  12. #include <asm/atomic.h>
  13. #include <asm/system.h>
  14. #include <asm/io.h>
  15. #include <asm/timer.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/delay.h>
  18. #include <asm/desc.h>
  19. #include <asm/apic.h>
  20. #include <asm/arch_hooks.h>
  21. #include <asm/i8259.h>
  22. /*
  23. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  24. * present in the majority of PC/AT boxes.
  25. * plus some generic x86 specific things if generic specifics makes
  26. * any sense at all.
  27. */
  28. static int i8259A_auto_eoi;
  29. DEFINE_SPINLOCK(i8259A_lock);
  30. static void mask_and_ack_8259A(unsigned int);
  31. static struct irq_chip i8259A_chip = {
  32. .name = "XT-PIC",
  33. .mask = disable_8259A_irq,
  34. .disable = disable_8259A_irq,
  35. .unmask = enable_8259A_irq,
  36. .mask_ack = mask_and_ack_8259A,
  37. };
  38. /*
  39. * 8259A PIC functions to handle ISA devices:
  40. */
  41. /*
  42. * This contains the irq mask for both 8259A irq controllers,
  43. */
  44. unsigned int cached_irq_mask = 0xffff;
  45. /*
  46. * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
  47. * boards the timer interrupt is not really connected to any IO-APIC pin,
  48. * it's fed to the master 8259A's IR0 line only.
  49. *
  50. * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
  51. * this 'mixed mode' IRQ handling costs nothing because it's only used
  52. * at IRQ setup time.
  53. */
  54. unsigned long io_apic_irqs;
  55. void disable_8259A_irq(unsigned int irq)
  56. {
  57. unsigned int mask = 1 << irq;
  58. unsigned long flags;
  59. spin_lock_irqsave(&i8259A_lock, flags);
  60. cached_irq_mask |= mask;
  61. if (irq & 8)
  62. outb(cached_slave_mask, PIC_SLAVE_IMR);
  63. else
  64. outb(cached_master_mask, PIC_MASTER_IMR);
  65. spin_unlock_irqrestore(&i8259A_lock, flags);
  66. }
  67. void enable_8259A_irq(unsigned int irq)
  68. {
  69. unsigned int mask = ~(1 << irq);
  70. unsigned long flags;
  71. spin_lock_irqsave(&i8259A_lock, flags);
  72. cached_irq_mask &= mask;
  73. if (irq & 8)
  74. outb(cached_slave_mask, PIC_SLAVE_IMR);
  75. else
  76. outb(cached_master_mask, PIC_MASTER_IMR);
  77. spin_unlock_irqrestore(&i8259A_lock, flags);
  78. }
  79. int i8259A_irq_pending(unsigned int irq)
  80. {
  81. unsigned int mask = 1<<irq;
  82. unsigned long flags;
  83. int ret;
  84. spin_lock_irqsave(&i8259A_lock, flags);
  85. if (irq < 8)
  86. ret = inb(PIC_MASTER_CMD) & mask;
  87. else
  88. ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
  89. spin_unlock_irqrestore(&i8259A_lock, flags);
  90. return ret;
  91. }
  92. void make_8259A_irq(unsigned int irq)
  93. {
  94. disable_irq_nosync(irq);
  95. io_apic_irqs &= ~(1<<irq);
  96. set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
  97. "XT");
  98. enable_irq(irq);
  99. }
  100. /*
  101. * This function assumes to be called rarely. Switching between
  102. * 8259A registers is slow.
  103. * This has to be protected by the irq controller spinlock
  104. * before being called.
  105. */
  106. static inline int i8259A_irq_real(unsigned int irq)
  107. {
  108. int value;
  109. int irqmask = 1<<irq;
  110. if (irq < 8) {
  111. outb(0x0B,PIC_MASTER_CMD); /* ISR register */
  112. value = inb(PIC_MASTER_CMD) & irqmask;
  113. outb(0x0A,PIC_MASTER_CMD); /* back to the IRR register */
  114. return value;
  115. }
  116. outb(0x0B,PIC_SLAVE_CMD); /* ISR register */
  117. value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
  118. outb(0x0A,PIC_SLAVE_CMD); /* back to the IRR register */
  119. return value;
  120. }
  121. /*
  122. * Careful! The 8259A is a fragile beast, it pretty
  123. * much _has_ to be done exactly like this (mask it
  124. * first, _then_ send the EOI, and the order of EOI
  125. * to the two 8259s is important!
  126. */
  127. static void mask_and_ack_8259A(unsigned int irq)
  128. {
  129. unsigned int irqmask = 1 << irq;
  130. unsigned long flags;
  131. spin_lock_irqsave(&i8259A_lock, flags);
  132. /*
  133. * Lightweight spurious IRQ detection. We do not want
  134. * to overdo spurious IRQ handling - it's usually a sign
  135. * of hardware problems, so we only do the checks we can
  136. * do without slowing down good hardware unnecessarily.
  137. *
  138. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  139. * usually resulting from the 8259A-1|2 PICs) occur
  140. * even if the IRQ is masked in the 8259A. Thus we
  141. * can check spurious 8259A IRQs without doing the
  142. * quite slow i8259A_irq_real() call for every IRQ.
  143. * This does not cover 100% of spurious interrupts,
  144. * but should be enough to warn the user that there
  145. * is something bad going on ...
  146. */
  147. if (cached_irq_mask & irqmask)
  148. goto spurious_8259A_irq;
  149. cached_irq_mask |= irqmask;
  150. handle_real_irq:
  151. if (irq & 8) {
  152. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  153. outb(cached_slave_mask, PIC_SLAVE_IMR);
  154. outb(0x60+(irq&7),PIC_SLAVE_CMD);/* 'Specific EOI' to slave */
  155. outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD); /* 'Specific EOI' to master-IRQ2 */
  156. } else {
  157. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  158. outb(cached_master_mask, PIC_MASTER_IMR);
  159. outb(0x60+irq,PIC_MASTER_CMD); /* 'Specific EOI to master */
  160. }
  161. spin_unlock_irqrestore(&i8259A_lock, flags);
  162. return;
  163. spurious_8259A_irq:
  164. /*
  165. * this is the slow path - should happen rarely.
  166. */
  167. if (i8259A_irq_real(irq))
  168. /*
  169. * oops, the IRQ _is_ in service according to the
  170. * 8259A - not spurious, go handle it.
  171. */
  172. goto handle_real_irq;
  173. {
  174. static int spurious_irq_mask;
  175. /*
  176. * At this point we can be sure the IRQ is spurious,
  177. * lets ACK and report it. [once per IRQ]
  178. */
  179. if (!(spurious_irq_mask & irqmask)) {
  180. printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
  181. spurious_irq_mask |= irqmask;
  182. }
  183. atomic_inc(&irq_err_count);
  184. /*
  185. * Theoretically we do not have to handle this IRQ,
  186. * but in Linux this does not cause problems and is
  187. * simpler for us.
  188. */
  189. goto handle_real_irq;
  190. }
  191. }
  192. static char irq_trigger[2];
  193. /**
  194. * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
  195. */
  196. static void restore_ELCR(char *trigger)
  197. {
  198. outb(trigger[0], 0x4d0);
  199. outb(trigger[1], 0x4d1);
  200. }
  201. static void save_ELCR(char *trigger)
  202. {
  203. /* IRQ 0,1,2,8,13 are marked as reserved */
  204. trigger[0] = inb(0x4d0) & 0xF8;
  205. trigger[1] = inb(0x4d1) & 0xDE;
  206. }
  207. static int i8259A_resume(struct sys_device *dev)
  208. {
  209. init_8259A(i8259A_auto_eoi);
  210. restore_ELCR(irq_trigger);
  211. return 0;
  212. }
  213. static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
  214. {
  215. save_ELCR(irq_trigger);
  216. return 0;
  217. }
  218. static int i8259A_shutdown(struct sys_device *dev)
  219. {
  220. /* Put the i8259A into a quiescent state that
  221. * the kernel initialization code can get it
  222. * out of.
  223. */
  224. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  225. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
  226. return 0;
  227. }
  228. static struct sysdev_class i8259_sysdev_class = {
  229. .name = "i8259",
  230. .suspend = i8259A_suspend,
  231. .resume = i8259A_resume,
  232. .shutdown = i8259A_shutdown,
  233. };
  234. static struct sys_device device_i8259A = {
  235. .id = 0,
  236. .cls = &i8259_sysdev_class,
  237. };
  238. static int __init i8259A_init_sysfs(void)
  239. {
  240. int error = sysdev_class_register(&i8259_sysdev_class);
  241. if (!error)
  242. error = sysdev_register(&device_i8259A);
  243. return error;
  244. }
  245. device_initcall(i8259A_init_sysfs);
  246. void init_8259A(int auto_eoi)
  247. {
  248. unsigned long flags;
  249. i8259A_auto_eoi = auto_eoi;
  250. spin_lock_irqsave(&i8259A_lock, flags);
  251. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  252. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  253. /*
  254. * outb_pic - this has to work on a wide range of PC hardware.
  255. */
  256. outb_pic(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  257. outb_pic(0x20 + 0, PIC_MASTER_IMR); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
  258. outb_pic(1U << PIC_CASCADE_IR, PIC_MASTER_IMR); /* 8259A-1 (the master) has a slave on IR2 */
  259. if (auto_eoi) /* master does Auto EOI */
  260. outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  261. else /* master expects normal EOI */
  262. outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  263. outb_pic(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  264. outb_pic(0x20 + 8, PIC_SLAVE_IMR); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
  265. outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR); /* 8259A-2 is a slave on master's IR2 */
  266. outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR); /* (slave's support for AEOI in flat mode is to be investigated) */
  267. if (auto_eoi)
  268. /*
  269. * In AEOI mode we just have to mask the interrupt
  270. * when acking.
  271. */
  272. i8259A_chip.mask_ack = disable_8259A_irq;
  273. else
  274. i8259A_chip.mask_ack = mask_and_ack_8259A;
  275. udelay(100); /* wait for 8259A to initialize */
  276. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  277. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  278. spin_unlock_irqrestore(&i8259A_lock, flags);
  279. }
  280. /*
  281. * Note that on a 486, we don't want to do a SIGFPE on an irq13
  282. * as the irq is unreliable, and exception 16 works correctly
  283. * (ie as explained in the intel literature). On a 386, you
  284. * can't use exception 16 due to bad IBM design, so we have to
  285. * rely on the less exact irq13.
  286. *
  287. * Careful.. Not only is IRQ13 unreliable, but it is also
  288. * leads to races. IBM designers who came up with it should
  289. * be shot.
  290. */
  291. static irqreturn_t math_error_irq(int cpl, void *dev_id)
  292. {
  293. extern void math_error(void __user *);
  294. outb(0,0xF0);
  295. if (ignore_fpu_irq || !boot_cpu_data.hard_math)
  296. return IRQ_NONE;
  297. math_error((void __user *)get_irq_regs()->ip);
  298. return IRQ_HANDLED;
  299. }
  300. /*
  301. * New motherboards sometimes make IRQ 13 be a PCI interrupt,
  302. * so allow interrupt sharing.
  303. */
  304. static struct irqaction fpu_irq = {
  305. .handler = math_error_irq,
  306. .mask = CPU_MASK_NONE,
  307. .name = "fpu",
  308. };
  309. void __init init_ISA_irqs (void)
  310. {
  311. int i;
  312. #ifdef CONFIG_X86_LOCAL_APIC
  313. init_bsp_APIC();
  314. #endif
  315. init_8259A(0);
  316. /*
  317. * 16 old-style INTA-cycle interrupts:
  318. */
  319. for (i = 0; i < 16; i++) {
  320. set_irq_chip_and_handler_name(i, &i8259A_chip,
  321. handle_level_irq, "XT");
  322. }
  323. }
  324. /* Overridden in paravirt.c */
  325. void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
  326. void __init native_init_IRQ(void)
  327. {
  328. int i;
  329. /* all the set up before the call gates are initialised */
  330. pre_intr_init_hook();
  331. /*
  332. * Cover the whole vector space, no vector can escape
  333. * us. (some of these will be overridden and become
  334. * 'special' SMP interrupts)
  335. */
  336. for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
  337. int vector = FIRST_EXTERNAL_VECTOR + i;
  338. if (i >= NR_IRQS)
  339. break;
  340. /* SYSCALL_VECTOR was reserved in trap_init. */
  341. if (!test_bit(vector, used_vectors))
  342. set_intr_gate(vector, interrupt[i]);
  343. }
  344. /* setup after call gates are initialised (usually add in
  345. * the architecture specific gates)
  346. */
  347. intr_init_hook();
  348. /*
  349. * External FPU? Set up irq13 if so, for
  350. * original braindamaged IBM FERR coupling.
  351. */
  352. if (boot_cpu_data.hard_math && !cpu_has_fpu)
  353. setup_irq(FPU_IRQ, &fpu_irq);
  354. irq_ctx_init(smp_processor_id());
  355. }