i8259_64.c 13 KB

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