i8259.c 11 KB

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