i8259.c 15 KB

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