i8259_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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/hw_irq.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/delay.h>
  21. #include <asm/desc.h>
  22. #include <asm/apic.h>
  23. #include <asm/i8259.h>
  24. /*
  25. * Common place to define all x86 IRQ vectors
  26. *
  27. * This builds up the IRQ handler stubs using some ugly macros in irq.h
  28. *
  29. * These macros create the low-level assembly IRQ routines that save
  30. * register context and call do_IRQ(). do_IRQ() then does all the
  31. * operations that are needed to keep the AT (or SMP IOAPIC)
  32. * interrupt-controller happy.
  33. */
  34. #define BI(x,y) \
  35. BUILD_IRQ(x##y)
  36. #define BUILD_16_IRQS(x) \
  37. BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
  38. BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
  39. BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
  40. BI(x,c) BI(x,d) BI(x,e) BI(x,f)
  41. /*
  42. * ISA PIC or low IO-APIC triggered (INTA-cycle or APIC) interrupts:
  43. * (these are usually mapped to vectors 0x30-0x3f)
  44. */
  45. /*
  46. * The IO-APIC gives us many more interrupt sources. Most of these
  47. * are unused but an SMP system is supposed to have enough memory ...
  48. * sometimes (mostly wrt. hw bugs) we get corrupted vectors all
  49. * across the spectrum, so we really want to be prepared to get all
  50. * of these. Plus, more powerful systems might have more than 64
  51. * IO-APIC registers.
  52. *
  53. * (these are usually mapped into the 0x30-0xff vector range)
  54. */
  55. BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
  56. BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
  57. BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
  58. BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd) BUILD_16_IRQS(0xe) BUILD_16_IRQS(0xf)
  59. #undef BUILD_16_IRQS
  60. #undef BI
  61. #define IRQ(x,y) \
  62. IRQ##x##y##_interrupt
  63. #define IRQLIST_16(x) \
  64. IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
  65. IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
  66. IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
  67. IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
  68. /* for the irq vectors */
  69. static void (*__initdata interrupt[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
  70. IRQLIST_16(0x2), IRQLIST_16(0x3),
  71. IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
  72. IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
  73. IRQLIST_16(0xc), IRQLIST_16(0xd), IRQLIST_16(0xe), IRQLIST_16(0xf)
  74. };
  75. #undef IRQ
  76. #undef IRQLIST_16
  77. /*
  78. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  79. * present in the majority of PC/AT boxes.
  80. * plus some generic x86 specific things if generic specifics makes
  81. * any sense at all.
  82. * this file should become arch/i386/kernel/irq.c when the old irq.c
  83. * moves to arch independent land
  84. */
  85. static int i8259A_auto_eoi;
  86. DEFINE_SPINLOCK(i8259A_lock);
  87. static void mask_and_ack_8259A(unsigned int);
  88. static struct irq_chip i8259A_chip = {
  89. .name = "XT-PIC",
  90. .mask = disable_8259A_irq,
  91. .disable = disable_8259A_irq,
  92. .unmask = enable_8259A_irq,
  93. .mask_ack = mask_and_ack_8259A,
  94. };
  95. /*
  96. * 8259A PIC functions to handle ISA devices:
  97. */
  98. /*
  99. * This contains the irq mask for both 8259A irq controllers,
  100. */
  101. unsigned int cached_irq_mask = 0xffff;
  102. /*
  103. * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
  104. * boards the timer interrupt is not really connected to any IO-APIC pin,
  105. * it's fed to the master 8259A's IR0 line only.
  106. *
  107. * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
  108. * this 'mixed mode' IRQ handling costs nothing because it's only used
  109. * at IRQ setup time.
  110. */
  111. unsigned long io_apic_irqs;
  112. void disable_8259A_irq(unsigned int irq)
  113. {
  114. unsigned int mask = 1 << irq;
  115. unsigned long flags;
  116. spin_lock_irqsave(&i8259A_lock, flags);
  117. cached_irq_mask |= mask;
  118. if (irq & 8)
  119. outb(cached_slave_mask, PIC_SLAVE_IMR);
  120. else
  121. outb(cached_master_mask, PIC_MASTER_IMR);
  122. spin_unlock_irqrestore(&i8259A_lock, flags);
  123. }
  124. void enable_8259A_irq(unsigned int irq)
  125. {
  126. unsigned int mask = ~(1 << irq);
  127. unsigned long flags;
  128. spin_lock_irqsave(&i8259A_lock, flags);
  129. cached_irq_mask &= mask;
  130. if (irq & 8)
  131. outb(cached_slave_mask, PIC_SLAVE_IMR);
  132. else
  133. outb(cached_master_mask, PIC_MASTER_IMR);
  134. spin_unlock_irqrestore(&i8259A_lock, flags);
  135. }
  136. int i8259A_irq_pending(unsigned int irq)
  137. {
  138. unsigned int mask = 1<<irq;
  139. unsigned long flags;
  140. int ret;
  141. spin_lock_irqsave(&i8259A_lock, flags);
  142. if (irq < 8)
  143. ret = inb(PIC_MASTER_CMD) & mask;
  144. else
  145. ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
  146. spin_unlock_irqrestore(&i8259A_lock, flags);
  147. return ret;
  148. }
  149. void make_8259A_irq(unsigned int irq)
  150. {
  151. disable_irq_nosync(irq);
  152. io_apic_irqs &= ~(1<<irq);
  153. set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
  154. "XT");
  155. enable_irq(irq);
  156. }
  157. /*
  158. * This function assumes to be called rarely. Switching between
  159. * 8259A registers is slow.
  160. * This has to be protected by the irq controller spinlock
  161. * before being called.
  162. */
  163. static inline int i8259A_irq_real(unsigned int irq)
  164. {
  165. int value;
  166. int irqmask = 1<<irq;
  167. if (irq < 8) {
  168. outb(0x0B,PIC_MASTER_CMD); /* ISR register */
  169. value = inb(PIC_MASTER_CMD) & irqmask;
  170. outb(0x0A,PIC_MASTER_CMD); /* back to the IRR register */
  171. return value;
  172. }
  173. outb(0x0B,PIC_SLAVE_CMD); /* ISR register */
  174. value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
  175. outb(0x0A,PIC_SLAVE_CMD); /* back to the IRR register */
  176. return value;
  177. }
  178. /*
  179. * Careful! The 8259A is a fragile beast, it pretty
  180. * much _has_ to be done exactly like this (mask it
  181. * first, _then_ send the EOI, and the order of EOI
  182. * to the two 8259s is important!
  183. */
  184. static void mask_and_ack_8259A(unsigned int irq)
  185. {
  186. unsigned int irqmask = 1 << irq;
  187. unsigned long flags;
  188. spin_lock_irqsave(&i8259A_lock, flags);
  189. /*
  190. * Lightweight spurious IRQ detection. We do not want
  191. * to overdo spurious IRQ handling - it's usually a sign
  192. * of hardware problems, so we only do the checks we can
  193. * do without slowing down good hardware unnecessarily.
  194. *
  195. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  196. * usually resulting from the 8259A-1|2 PICs) occur
  197. * even if the IRQ is masked in the 8259A. Thus we
  198. * can check spurious 8259A IRQs without doing the
  199. * quite slow i8259A_irq_real() call for every IRQ.
  200. * This does not cover 100% of spurious interrupts,
  201. * but should be enough to warn the user that there
  202. * is something bad going on ...
  203. */
  204. if (cached_irq_mask & irqmask)
  205. goto spurious_8259A_irq;
  206. cached_irq_mask |= irqmask;
  207. handle_real_irq:
  208. if (irq & 8) {
  209. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  210. outb(cached_slave_mask, PIC_SLAVE_IMR);
  211. /* 'Specific EOI' to slave */
  212. outb(0x60+(irq&7),PIC_SLAVE_CMD);
  213. /* 'Specific EOI' to master-IRQ2 */
  214. outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD);
  215. } else {
  216. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  217. outb(cached_master_mask, PIC_MASTER_IMR);
  218. /* 'Specific EOI' to master */
  219. outb(0x60+irq,PIC_MASTER_CMD);
  220. }
  221. spin_unlock_irqrestore(&i8259A_lock, flags);
  222. return;
  223. spurious_8259A_irq:
  224. /*
  225. * this is the slow path - should happen rarely.
  226. */
  227. if (i8259A_irq_real(irq))
  228. /*
  229. * oops, the IRQ _is_ in service according to the
  230. * 8259A - not spurious, go handle it.
  231. */
  232. goto handle_real_irq;
  233. {
  234. static int spurious_irq_mask;
  235. /*
  236. * At this point we can be sure the IRQ is spurious,
  237. * lets ACK and report it. [once per IRQ]
  238. */
  239. if (!(spurious_irq_mask & irqmask)) {
  240. printk(KERN_DEBUG
  241. "spurious 8259A interrupt: IRQ%d.\n", irq);
  242. spurious_irq_mask |= irqmask;
  243. }
  244. atomic_inc(&irq_err_count);
  245. /*
  246. * Theoretically we do not have to handle this IRQ,
  247. * but in Linux this does not cause problems and is
  248. * simpler for us.
  249. */
  250. goto handle_real_irq;
  251. }
  252. }
  253. static char irq_trigger[2];
  254. /**
  255. * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
  256. */
  257. static void restore_ELCR(char *trigger)
  258. {
  259. outb(trigger[0], 0x4d0);
  260. outb(trigger[1], 0x4d1);
  261. }
  262. static void save_ELCR(char *trigger)
  263. {
  264. /* IRQ 0,1,2,8,13 are marked as reserved */
  265. trigger[0] = inb(0x4d0) & 0xF8;
  266. trigger[1] = inb(0x4d1) & 0xDE;
  267. }
  268. static int i8259A_resume(struct sys_device *dev)
  269. {
  270. init_8259A(i8259A_auto_eoi);
  271. restore_ELCR(irq_trigger);
  272. return 0;
  273. }
  274. static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
  275. {
  276. save_ELCR(irq_trigger);
  277. return 0;
  278. }
  279. static int i8259A_shutdown(struct sys_device *dev)
  280. {
  281. /* Put the i8259A into a quiescent state that
  282. * the kernel initialization code can get it
  283. * out of.
  284. */
  285. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  286. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
  287. return 0;
  288. }
  289. static struct sysdev_class i8259_sysdev_class = {
  290. .name = "i8259",
  291. .suspend = i8259A_suspend,
  292. .resume = i8259A_resume,
  293. .shutdown = i8259A_shutdown,
  294. };
  295. static struct sys_device device_i8259A = {
  296. .id = 0,
  297. .cls = &i8259_sysdev_class,
  298. };
  299. static int __init i8259A_init_sysfs(void)
  300. {
  301. int error = sysdev_class_register(&i8259_sysdev_class);
  302. if (!error)
  303. error = sysdev_register(&device_i8259A);
  304. return error;
  305. }
  306. device_initcall(i8259A_init_sysfs);
  307. void init_8259A(int auto_eoi)
  308. {
  309. unsigned long flags;
  310. i8259A_auto_eoi = auto_eoi;
  311. spin_lock_irqsave(&i8259A_lock, flags);
  312. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  313. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  314. /*
  315. * outb_pic - this has to work on a wide range of PC hardware.
  316. */
  317. outb_pic(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  318. /* ICW2: 8259A-1 IR0-7 mapped to 0x30-0x37 */
  319. outb_pic(IRQ0_VECTOR, PIC_MASTER_IMR);
  320. /* 8259A-1 (the master) has a slave on IR2 */
  321. outb_pic(0x04, PIC_MASTER_IMR);
  322. if (auto_eoi) /* master does Auto EOI */
  323. outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  324. else /* master expects normal EOI */
  325. outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  326. outb_pic(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  327. /* ICW2: 8259A-2 IR0-7 mapped to 0x38-0x3f */
  328. outb_pic(IRQ8_VECTOR, PIC_SLAVE_IMR);
  329. /* 8259A-2 is a slave on master's IR2 */
  330. outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR);
  331. /* (slave's support for AEOI in flat mode is to be investigated) */
  332. outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
  333. if (auto_eoi)
  334. /*
  335. * In AEOI mode we just have to mask the interrupt
  336. * when acking.
  337. */
  338. i8259A_chip.mask_ack = disable_8259A_irq;
  339. else
  340. i8259A_chip.mask_ack = mask_and_ack_8259A;
  341. udelay(100); /* wait for 8259A to initialize */
  342. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  343. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  344. spin_unlock_irqrestore(&i8259A_lock, flags);
  345. }
  346. /*
  347. * IRQ2 is cascade interrupt to second interrupt controller
  348. */
  349. static struct irqaction irq2 = {
  350. .handler = no_action,
  351. .mask = CPU_MASK_NONE,
  352. .name = "cascade",
  353. };
  354. DEFINE_PER_CPU(vector_irq_t, vector_irq) = {
  355. [0 ... IRQ0_VECTOR - 1] = -1,
  356. [IRQ0_VECTOR] = 0,
  357. [IRQ1_VECTOR] = 1,
  358. [IRQ2_VECTOR] = 2,
  359. [IRQ3_VECTOR] = 3,
  360. [IRQ4_VECTOR] = 4,
  361. [IRQ5_VECTOR] = 5,
  362. [IRQ6_VECTOR] = 6,
  363. [IRQ7_VECTOR] = 7,
  364. [IRQ8_VECTOR] = 8,
  365. [IRQ9_VECTOR] = 9,
  366. [IRQ10_VECTOR] = 10,
  367. [IRQ11_VECTOR] = 11,
  368. [IRQ12_VECTOR] = 12,
  369. [IRQ13_VECTOR] = 13,
  370. [IRQ14_VECTOR] = 14,
  371. [IRQ15_VECTOR] = 15,
  372. [IRQ15_VECTOR + 1 ... NR_VECTORS - 1] = -1
  373. };
  374. void __init init_ISA_irqs (void)
  375. {
  376. int i;
  377. init_bsp_APIC();
  378. init_8259A(0);
  379. for (i = 0; i < NR_IRQS; i++) {
  380. irq_desc[i].status = IRQ_DISABLED;
  381. irq_desc[i].action = NULL;
  382. irq_desc[i].depth = 1;
  383. if (i < 16) {
  384. /*
  385. * 16 old-style INTA-cycle interrupts:
  386. */
  387. set_irq_chip_and_handler_name(i, &i8259A_chip,
  388. handle_level_irq, "XT");
  389. } else {
  390. /*
  391. * 'high' PCI IRQs filled in on demand
  392. */
  393. irq_desc[i].chip = &no_irq_chip;
  394. }
  395. }
  396. }
  397. void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
  398. void __init native_init_IRQ(void)
  399. {
  400. int i;
  401. init_ISA_irqs();
  402. /*
  403. * Cover the whole vector space, no vector can escape
  404. * us. (some of these will be overridden and become
  405. * 'special' SMP interrupts)
  406. */
  407. for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
  408. int vector = FIRST_EXTERNAL_VECTOR + i;
  409. if (vector != IA32_SYSCALL_VECTOR)
  410. set_intr_gate(vector, interrupt[i]);
  411. }
  412. #ifdef CONFIG_SMP
  413. /*
  414. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  415. * IPI, driven by wakeup.
  416. */
  417. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  418. /* IPIs for invalidation */
  419. set_intr_gate(INVALIDATE_TLB_VECTOR_START+0, invalidate_interrupt0);
  420. set_intr_gate(INVALIDATE_TLB_VECTOR_START+1, invalidate_interrupt1);
  421. set_intr_gate(INVALIDATE_TLB_VECTOR_START+2, invalidate_interrupt2);
  422. set_intr_gate(INVALIDATE_TLB_VECTOR_START+3, invalidate_interrupt3);
  423. set_intr_gate(INVALIDATE_TLB_VECTOR_START+4, invalidate_interrupt4);
  424. set_intr_gate(INVALIDATE_TLB_VECTOR_START+5, invalidate_interrupt5);
  425. set_intr_gate(INVALIDATE_TLB_VECTOR_START+6, invalidate_interrupt6);
  426. set_intr_gate(INVALIDATE_TLB_VECTOR_START+7, invalidate_interrupt7);
  427. /* IPI for generic function call */
  428. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  429. /* Low priority IPI to cleanup after moving an irq */
  430. set_intr_gate(IRQ_MOVE_CLEANUP_VECTOR, irq_move_cleanup_interrupt);
  431. #endif
  432. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  433. set_intr_gate(THRESHOLD_APIC_VECTOR, threshold_interrupt);
  434. /* self generated IPI for local APIC timer */
  435. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  436. /* IPI vectors for APIC spurious and error interrupts */
  437. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  438. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  439. if (!acpi_ioapic)
  440. setup_irq(2, &irq2);
  441. }