i8259.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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/smp_lock.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/sysdev.h>
  14. #include <linux/bitops.h>
  15. #include <asm/acpi.h>
  16. #include <asm/atomic.h>
  17. #include <asm/system.h>
  18. #include <asm/io.h>
  19. #include <asm/hw_irq.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/delay.h>
  22. #include <asm/desc.h>
  23. #include <asm/apic.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 0x20-0x2f)
  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. void (*interrupt[NR_IRQS])(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. .unmask = enable_8259A_irq,
  91. .mask_ack = mask_and_ack_8259A,
  92. };
  93. /*
  94. * 8259A PIC functions to handle ISA devices:
  95. */
  96. /*
  97. * This contains the irq mask for both 8259A irq controllers,
  98. */
  99. static unsigned int cached_irq_mask = 0xffff;
  100. #define __byte(x,y) (((unsigned char *)&(y))[x])
  101. #define cached_21 (__byte(0,cached_irq_mask))
  102. #define cached_A1 (__byte(1,cached_irq_mask))
  103. /*
  104. * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
  105. * boards the timer interrupt is not really connected to any IO-APIC pin,
  106. * it's fed to the master 8259A's IR0 line only.
  107. *
  108. * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
  109. * this 'mixed mode' IRQ handling costs nothing because it's only used
  110. * at IRQ setup time.
  111. */
  112. unsigned long io_apic_irqs;
  113. void disable_8259A_irq(unsigned int irq)
  114. {
  115. unsigned int mask = 1 << irq;
  116. unsigned long flags;
  117. spin_lock_irqsave(&i8259A_lock, flags);
  118. cached_irq_mask |= mask;
  119. if (irq & 8)
  120. outb(cached_A1,0xA1);
  121. else
  122. outb(cached_21,0x21);
  123. spin_unlock_irqrestore(&i8259A_lock, flags);
  124. }
  125. void enable_8259A_irq(unsigned int irq)
  126. {
  127. unsigned int mask = ~(1 << irq);
  128. unsigned long flags;
  129. spin_lock_irqsave(&i8259A_lock, flags);
  130. cached_irq_mask &= mask;
  131. if (irq & 8)
  132. outb(cached_A1,0xA1);
  133. else
  134. outb(cached_21,0x21);
  135. spin_unlock_irqrestore(&i8259A_lock, flags);
  136. }
  137. int i8259A_irq_pending(unsigned int irq)
  138. {
  139. unsigned int mask = 1<<irq;
  140. unsigned long flags;
  141. int ret;
  142. spin_lock_irqsave(&i8259A_lock, flags);
  143. if (irq < 8)
  144. ret = inb(0x20) & mask;
  145. else
  146. ret = inb(0xA0) & (mask >> 8);
  147. spin_unlock_irqrestore(&i8259A_lock, flags);
  148. return ret;
  149. }
  150. void make_8259A_irq(unsigned int irq)
  151. {
  152. disable_irq_nosync(irq);
  153. io_apic_irqs &= ~(1<<irq);
  154. set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
  155. "XT");
  156. enable_irq(irq);
  157. }
  158. /*
  159. * This function assumes to be called rarely. Switching between
  160. * 8259A registers is slow.
  161. * This has to be protected by the irq controller spinlock
  162. * before being called.
  163. */
  164. static inline int i8259A_irq_real(unsigned int irq)
  165. {
  166. int value;
  167. int irqmask = 1<<irq;
  168. if (irq < 8) {
  169. outb(0x0B,0x20); /* ISR register */
  170. value = inb(0x20) & irqmask;
  171. outb(0x0A,0x20); /* back to the IRR register */
  172. return value;
  173. }
  174. outb(0x0B,0xA0); /* ISR register */
  175. value = inb(0xA0) & (irqmask >> 8);
  176. outb(0x0A,0xA0); /* back to the IRR register */
  177. return value;
  178. }
  179. /*
  180. * Careful! The 8259A is a fragile beast, it pretty
  181. * much _has_ to be done exactly like this (mask it
  182. * first, _then_ send the EOI, and the order of EOI
  183. * to the two 8259s is important!
  184. */
  185. static void mask_and_ack_8259A(unsigned int irq)
  186. {
  187. unsigned int irqmask = 1 << irq;
  188. unsigned long flags;
  189. spin_lock_irqsave(&i8259A_lock, flags);
  190. /*
  191. * Lightweight spurious IRQ detection. We do not want
  192. * to overdo spurious IRQ handling - it's usually a sign
  193. * of hardware problems, so we only do the checks we can
  194. * do without slowing down good hardware unnecessarily.
  195. *
  196. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  197. * usually resulting from the 8259A-1|2 PICs) occur
  198. * even if the IRQ is masked in the 8259A. Thus we
  199. * can check spurious 8259A IRQs without doing the
  200. * quite slow i8259A_irq_real() call for every IRQ.
  201. * This does not cover 100% of spurious interrupts,
  202. * but should be enough to warn the user that there
  203. * is something bad going on ...
  204. */
  205. if (cached_irq_mask & irqmask)
  206. goto spurious_8259A_irq;
  207. cached_irq_mask |= irqmask;
  208. handle_real_irq:
  209. if (irq & 8) {
  210. inb(0xA1); /* DUMMY - (do we need this?) */
  211. outb(cached_A1,0xA1);
  212. outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
  213. outb(0x62,0x20); /* 'Specific EOI' to master-IRQ2 */
  214. } else {
  215. inb(0x21); /* DUMMY - (do we need this?) */
  216. outb(cached_21,0x21);
  217. outb(0x60+irq,0x20); /* 'Specific EOI' to master */
  218. }
  219. spin_unlock_irqrestore(&i8259A_lock, flags);
  220. return;
  221. spurious_8259A_irq:
  222. /*
  223. * this is the slow path - should happen rarely.
  224. */
  225. if (i8259A_irq_real(irq))
  226. /*
  227. * oops, the IRQ _is_ in service according to the
  228. * 8259A - not spurious, go handle it.
  229. */
  230. goto handle_real_irq;
  231. {
  232. static int spurious_irq_mask;
  233. /*
  234. * At this point we can be sure the IRQ is spurious,
  235. * lets ACK and report it. [once per IRQ]
  236. */
  237. if (!(spurious_irq_mask & irqmask)) {
  238. printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
  239. spurious_irq_mask |= irqmask;
  240. }
  241. atomic_inc(&irq_err_count);
  242. /*
  243. * Theoretically we do not have to handle this IRQ,
  244. * but in Linux this does not cause problems and is
  245. * simpler for us.
  246. */
  247. goto handle_real_irq;
  248. }
  249. }
  250. void init_8259A(int auto_eoi)
  251. {
  252. unsigned long flags;
  253. i8259A_auto_eoi = auto_eoi;
  254. spin_lock_irqsave(&i8259A_lock, flags);
  255. outb(0xff, 0x21); /* mask all of 8259A-1 */
  256. outb(0xff, 0xA1); /* mask all of 8259A-2 */
  257. /*
  258. * outb_p - this has to work on a wide range of PC hardware.
  259. */
  260. outb_p(0x11, 0x20); /* ICW1: select 8259A-1 init */
  261. outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
  262. outb_p(0x04, 0x21); /* 8259A-1 (the master) has a slave on IR2 */
  263. if (auto_eoi)
  264. outb_p(0x03, 0x21); /* master does Auto EOI */
  265. else
  266. outb_p(0x01, 0x21); /* master expects normal EOI */
  267. outb_p(0x11, 0xA0); /* ICW1: select 8259A-2 init */
  268. outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
  269. outb_p(0x02, 0xA1); /* 8259A-2 is a slave on master's IR2 */
  270. outb_p(0x01, 0xA1); /* (slave's support for AEOI in flat mode
  271. is to be investigated) */
  272. if (auto_eoi)
  273. /*
  274. * in AEOI mode we just have to mask the interrupt
  275. * when acking.
  276. */
  277. i8259A_chip.mask_ack = disable_8259A_irq;
  278. else
  279. i8259A_chip.mask_ack = mask_and_ack_8259A;
  280. udelay(100); /* wait for 8259A to initialize */
  281. outb(cached_21, 0x21); /* restore master IRQ mask */
  282. outb(cached_A1, 0xA1); /* restore slave IRQ mask */
  283. spin_unlock_irqrestore(&i8259A_lock, flags);
  284. }
  285. static char irq_trigger[2];
  286. /**
  287. * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
  288. */
  289. static void restore_ELCR(char *trigger)
  290. {
  291. outb(trigger[0], 0x4d0);
  292. outb(trigger[1], 0x4d1);
  293. }
  294. static void save_ELCR(char *trigger)
  295. {
  296. /* IRQ 0,1,2,8,13 are marked as reserved */
  297. trigger[0] = inb(0x4d0) & 0xF8;
  298. trigger[1] = inb(0x4d1) & 0xDE;
  299. }
  300. static int i8259A_resume(struct sys_device *dev)
  301. {
  302. init_8259A(i8259A_auto_eoi);
  303. restore_ELCR(irq_trigger);
  304. return 0;
  305. }
  306. static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
  307. {
  308. save_ELCR(irq_trigger);
  309. return 0;
  310. }
  311. static int i8259A_shutdown(struct sys_device *dev)
  312. {
  313. /* Put the i8259A into a quiescent state that
  314. * the kernel initialization code can get it
  315. * out of.
  316. */
  317. outb(0xff, 0x21); /* mask all of 8259A-1 */
  318. outb(0xff, 0xA1); /* mask all of 8259A-1 */
  319. return 0;
  320. }
  321. static struct sysdev_class i8259_sysdev_class = {
  322. set_kset_name("i8259"),
  323. .suspend = i8259A_suspend,
  324. .resume = i8259A_resume,
  325. .shutdown = i8259A_shutdown,
  326. };
  327. static struct sys_device device_i8259A = {
  328. .id = 0,
  329. .cls = &i8259_sysdev_class,
  330. };
  331. static int __init i8259A_init_sysfs(void)
  332. {
  333. int error = sysdev_class_register(&i8259_sysdev_class);
  334. if (!error)
  335. error = sysdev_register(&device_i8259A);
  336. return error;
  337. }
  338. device_initcall(i8259A_init_sysfs);
  339. /*
  340. * IRQ2 is cascade interrupt to second interrupt controller
  341. */
  342. static struct irqaction irq2 = { no_action, 0, CPU_MASK_NONE, "cascade", NULL, NULL};
  343. DEFINE_PER_CPU(vector_irq_t, vector_irq) = {
  344. [0 ... FIRST_EXTERNAL_VECTOR - 1] = -1,
  345. [FIRST_EXTERNAL_VECTOR + 0] = 0,
  346. [FIRST_EXTERNAL_VECTOR + 1] = 1,
  347. [FIRST_EXTERNAL_VECTOR + 2] = 2,
  348. [FIRST_EXTERNAL_VECTOR + 3] = 3,
  349. [FIRST_EXTERNAL_VECTOR + 4] = 4,
  350. [FIRST_EXTERNAL_VECTOR + 5] = 5,
  351. [FIRST_EXTERNAL_VECTOR + 6] = 6,
  352. [FIRST_EXTERNAL_VECTOR + 7] = 7,
  353. [FIRST_EXTERNAL_VECTOR + 8] = 8,
  354. [FIRST_EXTERNAL_VECTOR + 9] = 9,
  355. [FIRST_EXTERNAL_VECTOR + 10] = 10,
  356. [FIRST_EXTERNAL_VECTOR + 11] = 11,
  357. [FIRST_EXTERNAL_VECTOR + 12] = 12,
  358. [FIRST_EXTERNAL_VECTOR + 13] = 13,
  359. [FIRST_EXTERNAL_VECTOR + 14] = 14,
  360. [FIRST_EXTERNAL_VECTOR + 15] = 15,
  361. [FIRST_EXTERNAL_VECTOR + 16 ... NR_VECTORS - 1] = -1
  362. };
  363. void __init init_ISA_irqs (void)
  364. {
  365. int i;
  366. init_bsp_APIC();
  367. init_8259A(0);
  368. for (i = 0; i < NR_IRQS; i++) {
  369. irq_desc[i].status = IRQ_DISABLED;
  370. irq_desc[i].action = NULL;
  371. irq_desc[i].depth = 1;
  372. if (i < 16) {
  373. /*
  374. * 16 old-style INTA-cycle interrupts:
  375. */
  376. set_irq_chip_and_handler_name(i, &i8259A_chip,
  377. handle_level_irq, "XT");
  378. } else {
  379. /*
  380. * 'high' PCI IRQs filled in on demand
  381. */
  382. irq_desc[i].chip = &no_irq_chip;
  383. }
  384. }
  385. }
  386. void apic_timer_interrupt(void);
  387. void spurious_interrupt(void);
  388. void error_interrupt(void);
  389. void reschedule_interrupt(void);
  390. void call_function_interrupt(void);
  391. void invalidate_interrupt0(void);
  392. void invalidate_interrupt1(void);
  393. void invalidate_interrupt2(void);
  394. void invalidate_interrupt3(void);
  395. void invalidate_interrupt4(void);
  396. void invalidate_interrupt5(void);
  397. void invalidate_interrupt6(void);
  398. void invalidate_interrupt7(void);
  399. void thermal_interrupt(void);
  400. void threshold_interrupt(void);
  401. void i8254_timer_resume(void);
  402. static void setup_timer_hardware(void)
  403. {
  404. outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */
  405. udelay(10);
  406. outb_p(LATCH & 0xff , 0x40); /* LSB */
  407. udelay(10);
  408. outb(LATCH >> 8 , 0x40); /* MSB */
  409. }
  410. static int timer_resume(struct sys_device *dev)
  411. {
  412. setup_timer_hardware();
  413. return 0;
  414. }
  415. void i8254_timer_resume(void)
  416. {
  417. setup_timer_hardware();
  418. }
  419. static struct sysdev_class timer_sysclass = {
  420. set_kset_name("timer_pit"),
  421. .resume = timer_resume,
  422. };
  423. static struct sys_device device_timer = {
  424. .id = 0,
  425. .cls = &timer_sysclass,
  426. };
  427. static int __init init_timer_sysfs(void)
  428. {
  429. int error = sysdev_class_register(&timer_sysclass);
  430. if (!error)
  431. error = sysdev_register(&device_timer);
  432. return error;
  433. }
  434. device_initcall(init_timer_sysfs);
  435. void __init init_IRQ(void)
  436. {
  437. int i;
  438. init_ISA_irqs();
  439. /*
  440. * Cover the whole vector space, no vector can escape
  441. * us. (some of these will be overridden and become
  442. * 'special' SMP interrupts)
  443. */
  444. for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
  445. int vector = FIRST_EXTERNAL_VECTOR + i;
  446. if (vector != IA32_SYSCALL_VECTOR)
  447. set_intr_gate(vector, interrupt[i]);
  448. }
  449. #ifdef CONFIG_SMP
  450. /*
  451. * IRQ0 must be given a fixed assignment and initialized,
  452. * because it's used before the IO-APIC is set up.
  453. */
  454. __get_cpu_var(vector_irq)[FIRST_DEVICE_VECTOR] = 0;
  455. /*
  456. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  457. * IPI, driven by wakeup.
  458. */
  459. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  460. /* IPIs for invalidation */
  461. set_intr_gate(INVALIDATE_TLB_VECTOR_START+0, invalidate_interrupt0);
  462. set_intr_gate(INVALIDATE_TLB_VECTOR_START+1, invalidate_interrupt1);
  463. set_intr_gate(INVALIDATE_TLB_VECTOR_START+2, invalidate_interrupt2);
  464. set_intr_gate(INVALIDATE_TLB_VECTOR_START+3, invalidate_interrupt3);
  465. set_intr_gate(INVALIDATE_TLB_VECTOR_START+4, invalidate_interrupt4);
  466. set_intr_gate(INVALIDATE_TLB_VECTOR_START+5, invalidate_interrupt5);
  467. set_intr_gate(INVALIDATE_TLB_VECTOR_START+6, invalidate_interrupt6);
  468. set_intr_gate(INVALIDATE_TLB_VECTOR_START+7, invalidate_interrupt7);
  469. /* IPI for generic function call */
  470. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  471. #endif
  472. set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
  473. set_intr_gate(THRESHOLD_APIC_VECTOR, threshold_interrupt);
  474. /* self generated IPI for local APIC timer */
  475. set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
  476. /* IPI vectors for APIC spurious and error interrupts */
  477. set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
  478. set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
  479. /*
  480. * Set the clock to HZ Hz, we already have a valid
  481. * vector now:
  482. */
  483. setup_timer_hardware();
  484. if (!acpi_ioapic)
  485. setup_irq(2, &irq2);
  486. }