i8259.c 11 KB

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