i8259.c 14 KB

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