irq_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
  3. *
  4. * This file contains the lowest level x86-specific interrupt
  5. * entry, irq-stacks and irq statistics code. All the remaining
  6. * irq logic is done by the generic kernel/irq/ code and
  7. * by the x86-specific irq controller code. (e.g. i8259.c and
  8. * io_apic.c.)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/notifier.h>
  15. #include <linux/cpu.h>
  16. #include <linux/delay.h>
  17. #include <asm/apic.h>
  18. #include <asm/uaccess.h>
  19. DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
  20. EXPORT_PER_CPU_SYMBOL(irq_stat);
  21. DEFINE_PER_CPU(struct pt_regs *, irq_regs);
  22. EXPORT_PER_CPU_SYMBOL(irq_regs);
  23. /*
  24. * 'what should we do if we get a hw irq event on an illegal vector'.
  25. * each architecture has to answer this themselves.
  26. */
  27. void ack_bad_irq(unsigned int irq)
  28. {
  29. printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
  30. #ifdef CONFIG_X86_LOCAL_APIC
  31. /*
  32. * Currently unexpected vectors happen only on SMP and APIC.
  33. * We _must_ ack these because every local APIC has only N
  34. * irq slots per priority level, and a 'hanging, unacked' IRQ
  35. * holds up an irq slot - in excessive cases (when multiple
  36. * unexpected vectors occur) that might lock up the APIC
  37. * completely.
  38. * But only ack when the APIC is enabled -AK
  39. */
  40. if (cpu_has_apic)
  41. ack_APIC_irq();
  42. #endif
  43. }
  44. #ifdef CONFIG_DEBUG_STACKOVERFLOW
  45. /* Debugging check for stack overflow: is there less than 1KB free? */
  46. static int check_stack_overflow(void)
  47. {
  48. long sp;
  49. __asm__ __volatile__("andl %%esp,%0" :
  50. "=r" (sp) : "0" (THREAD_SIZE - 1));
  51. return sp < (sizeof(struct thread_info) + STACK_WARN);
  52. }
  53. static void print_stack_overflow(void)
  54. {
  55. printk(KERN_WARNING "low stack detected by irq handler\n");
  56. dump_stack();
  57. }
  58. #else
  59. static inline int check_stack_overflow(void) { return 0; }
  60. static inline void print_stack_overflow(void) { }
  61. #endif
  62. #ifdef CONFIG_4KSTACKS
  63. /*
  64. * per-CPU IRQ handling contexts (thread information and stack)
  65. */
  66. union irq_ctx {
  67. struct thread_info tinfo;
  68. u32 stack[THREAD_SIZE/sizeof(u32)];
  69. };
  70. static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
  71. static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
  72. static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  73. static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
  74. static void call_on_stack(void *func, void *stack)
  75. {
  76. asm volatile("xchgl %%ebx,%%esp \n"
  77. "call *%%edi \n"
  78. "movl %%ebx,%%esp \n"
  79. : "=b" (stack)
  80. : "0" (stack),
  81. "D"(func)
  82. : "memory", "cc", "edx", "ecx", "eax");
  83. }
  84. static inline int
  85. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  86. {
  87. union irq_ctx *curctx, *irqctx;
  88. u32 *isp, arg1, arg2;
  89. curctx = (union irq_ctx *) current_thread_info();
  90. irqctx = hardirq_ctx[smp_processor_id()];
  91. /*
  92. * this is where we switch to the IRQ stack. However, if we are
  93. * already using the IRQ stack (because we interrupted a hardirq
  94. * handler) we can't do that and just have to keep using the
  95. * current stack (which is the irq stack already after all)
  96. */
  97. if (unlikely(curctx == irqctx))
  98. return 0;
  99. /* build the stack frame on the IRQ stack */
  100. isp = (u32 *) ((char*)irqctx + sizeof(*irqctx));
  101. irqctx->tinfo.task = curctx->tinfo.task;
  102. irqctx->tinfo.previous_esp = current_stack_pointer;
  103. /*
  104. * Copy the softirq bits in preempt_count so that the
  105. * softirq checks work in the hardirq context.
  106. */
  107. irqctx->tinfo.preempt_count =
  108. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  109. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  110. if (unlikely(overflow))
  111. call_on_stack(print_stack_overflow, isp);
  112. asm volatile("xchgl %%ebx,%%esp \n"
  113. "call *%%edi \n"
  114. "movl %%ebx,%%esp \n"
  115. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  116. : "0" (irq), "1" (desc), "2" (isp),
  117. "D" (desc->handle_irq)
  118. : "memory", "cc", "ecx");
  119. return 1;
  120. }
  121. /*
  122. * allocate per-cpu stacks for hardirq and for softirq processing
  123. */
  124. void __cpuinit irq_ctx_init(int cpu)
  125. {
  126. union irq_ctx *irqctx;
  127. if (hardirq_ctx[cpu])
  128. return;
  129. irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
  130. irqctx->tinfo.task = NULL;
  131. irqctx->tinfo.exec_domain = NULL;
  132. irqctx->tinfo.cpu = cpu;
  133. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  134. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  135. hardirq_ctx[cpu] = irqctx;
  136. irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
  137. irqctx->tinfo.task = NULL;
  138. irqctx->tinfo.exec_domain = NULL;
  139. irqctx->tinfo.cpu = cpu;
  140. irqctx->tinfo.preempt_count = 0;
  141. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  142. softirq_ctx[cpu] = irqctx;
  143. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  144. cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
  145. }
  146. void irq_ctx_exit(int cpu)
  147. {
  148. hardirq_ctx[cpu] = NULL;
  149. }
  150. asmlinkage void do_softirq(void)
  151. {
  152. unsigned long flags;
  153. struct thread_info *curctx;
  154. union irq_ctx *irqctx;
  155. u32 *isp;
  156. if (in_interrupt())
  157. return;
  158. local_irq_save(flags);
  159. if (local_softirq_pending()) {
  160. curctx = current_thread_info();
  161. irqctx = softirq_ctx[smp_processor_id()];
  162. irqctx->tinfo.task = curctx->task;
  163. irqctx->tinfo.previous_esp = current_stack_pointer;
  164. /* build the stack frame on the softirq stack */
  165. isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
  166. call_on_stack(__do_softirq, isp);
  167. /*
  168. * Shouldnt happen, we returned above if in_interrupt():
  169. */
  170. WARN_ON_ONCE(softirq_count());
  171. }
  172. local_irq_restore(flags);
  173. }
  174. #else
  175. static inline int
  176. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; }
  177. #endif
  178. /*
  179. * do_IRQ handles all normal device IRQ's (the special
  180. * SMP cross-CPU interrupts have their own specific
  181. * handlers).
  182. */
  183. unsigned int do_IRQ(struct pt_regs *regs)
  184. {
  185. struct pt_regs *old_regs;
  186. /* high bit used in ret_from_ code */
  187. int overflow;
  188. unsigned vector = ~regs->orig_ax;
  189. struct irq_desc *desc;
  190. unsigned irq;
  191. old_regs = set_irq_regs(regs);
  192. irq_enter();
  193. irq = __get_cpu_var(vector_irq)[vector];
  194. overflow = check_stack_overflow();
  195. desc = irq_to_desc(irq);
  196. if (unlikely(!desc)) {
  197. printk(KERN_EMERG "%s: cannot handle IRQ %d vector %#x cpu %d\n",
  198. __func__, irq, vector, smp_processor_id());
  199. BUG();
  200. }
  201. if (!execute_on_irq_stack(overflow, desc, irq)) {
  202. if (unlikely(overflow))
  203. print_stack_overflow();
  204. desc->handle_irq(irq, desc);
  205. }
  206. irq_exit();
  207. set_irq_regs(old_regs);
  208. return 1;
  209. }
  210. /*
  211. * Interrupt statistics:
  212. */
  213. atomic_t irq_err_count;
  214. /*
  215. * /proc/interrupts printing:
  216. */
  217. static int show_other_interrupts(struct seq_file *p)
  218. {
  219. int j;
  220. seq_printf(p, "NMI: ");
  221. for_each_online_cpu(j)
  222. seq_printf(p, "%10u ", nmi_count(j));
  223. seq_printf(p, " Non-maskable interrupts\n");
  224. #ifdef CONFIG_X86_LOCAL_APIC
  225. seq_printf(p, "LOC: ");
  226. for_each_online_cpu(j)
  227. seq_printf(p, "%10u ", per_cpu(irq_stat,j).apic_timer_irqs);
  228. seq_printf(p, " Local timer interrupts\n");
  229. #endif
  230. #ifdef CONFIG_SMP
  231. seq_printf(p, "RES: ");
  232. for_each_online_cpu(j)
  233. seq_printf(p, "%10u ", per_cpu(irq_stat,j).irq_resched_count);
  234. seq_printf(p, " Rescheduling interrupts\n");
  235. seq_printf(p, "CAL: ");
  236. for_each_online_cpu(j)
  237. seq_printf(p, "%10u ", per_cpu(irq_stat,j).irq_call_count);
  238. seq_printf(p, " Function call interrupts\n");
  239. seq_printf(p, "TLB: ");
  240. for_each_online_cpu(j)
  241. seq_printf(p, "%10u ", per_cpu(irq_stat,j).irq_tlb_count);
  242. seq_printf(p, " TLB shootdowns\n");
  243. #endif
  244. #ifdef CONFIG_X86_MCE
  245. seq_printf(p, "TRM: ");
  246. for_each_online_cpu(j)
  247. seq_printf(p, "%10u ", per_cpu(irq_stat,j).irq_thermal_count);
  248. seq_printf(p, " Thermal event interrupts\n");
  249. #endif
  250. #ifdef CONFIG_X86_LOCAL_APIC
  251. seq_printf(p, "SPU: ");
  252. for_each_online_cpu(j)
  253. seq_printf(p, "%10u ", per_cpu(irq_stat,j).irq_spurious_count);
  254. seq_printf(p, " Spurious interrupts\n");
  255. #endif
  256. seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
  257. #if defined(CONFIG_X86_IO_APIC)
  258. seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
  259. #endif
  260. return 0;
  261. }
  262. int show_interrupts(struct seq_file *p, void *v)
  263. {
  264. unsigned long flags, any_count = 0;
  265. int i = *(loff_t *) v, j;
  266. struct irqaction *action;
  267. struct irq_desc *desc;
  268. if (i > nr_irqs)
  269. return 0;
  270. if (i == nr_irqs)
  271. return show_other_interrupts(p);
  272. /* print header */
  273. if (i == 0) {
  274. seq_printf(p, " ");
  275. for_each_online_cpu(j)
  276. seq_printf(p, "CPU%-8d",j);
  277. seq_putc(p, '\n');
  278. }
  279. desc = irq_to_desc(i);
  280. spin_lock_irqsave(&desc->lock, flags);
  281. #ifndef CONFIG_SMP
  282. any_count = kstat_irqs(i);
  283. #else
  284. for_each_online_cpu(j)
  285. any_count |= kstat_irqs_cpu(i, j);
  286. #endif
  287. action = desc->action;
  288. if (!action && !any_count)
  289. goto out;
  290. seq_printf(p, "%3d: ", i);
  291. #ifndef CONFIG_SMP
  292. seq_printf(p, "%10u ", kstat_irqs(i));
  293. #else
  294. for_each_online_cpu(j)
  295. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  296. #endif
  297. seq_printf(p, " %8s", desc->chip->name);
  298. seq_printf(p, "-%-8s", desc->name);
  299. if (action) {
  300. seq_printf(p, " %s", action->name);
  301. while ((action = action->next) != NULL)
  302. seq_printf(p, ", %s", action->name);
  303. }
  304. seq_putc(p, '\n');
  305. out:
  306. spin_unlock_irqrestore(&desc->lock, flags);
  307. return 0;
  308. }
  309. /*
  310. * /proc/stat helpers
  311. */
  312. u64 arch_irq_stat_cpu(unsigned int cpu)
  313. {
  314. u64 sum = nmi_count(cpu);
  315. #ifdef CONFIG_X86_LOCAL_APIC
  316. sum += per_cpu(irq_stat, cpu).apic_timer_irqs;
  317. #endif
  318. #ifdef CONFIG_SMP
  319. sum += per_cpu(irq_stat, cpu).irq_resched_count;
  320. sum += per_cpu(irq_stat, cpu).irq_call_count;
  321. sum += per_cpu(irq_stat, cpu).irq_tlb_count;
  322. #endif
  323. #ifdef CONFIG_X86_MCE
  324. sum += per_cpu(irq_stat, cpu).irq_thermal_count;
  325. #endif
  326. #ifdef CONFIG_X86_LOCAL_APIC
  327. sum += per_cpu(irq_stat, cpu).irq_spurious_count;
  328. #endif
  329. return sum;
  330. }
  331. u64 arch_irq_stat(void)
  332. {
  333. u64 sum = atomic_read(&irq_err_count);
  334. #ifdef CONFIG_X86_IO_APIC
  335. sum += atomic_read(&irq_mis_count);
  336. #endif
  337. return sum;
  338. }
  339. #ifdef CONFIG_HOTPLUG_CPU
  340. #include <mach_apic.h>
  341. void fixup_irqs(cpumask_t map)
  342. {
  343. unsigned int irq;
  344. static int warned;
  345. struct irq_desc *desc;
  346. for_each_irq_desc(irq, desc) {
  347. cpumask_t mask;
  348. if (irq == 2)
  349. continue;
  350. cpus_and(mask, desc->affinity, map);
  351. if (any_online_cpu(mask) == NR_CPUS) {
  352. printk("Breaking affinity for irq %i\n", irq);
  353. mask = map;
  354. }
  355. if (desc->chip->set_affinity)
  356. desc->chip->set_affinity(irq, mask);
  357. else if (desc->action && !(warned++))
  358. printk("Cannot set affinity for irq %i\n", irq);
  359. }
  360. #if 0
  361. barrier();
  362. /* Ingo Molnar says: "after the IO-APIC masks have been redirected
  363. [note the nop - the interrupt-enable boundary on x86 is two
  364. instructions from sti] - to flush out pending hardirqs and
  365. IPIs. After this point nothing is supposed to reach this CPU." */
  366. __asm__ __volatile__("sti; nop; cli");
  367. barrier();
  368. #else
  369. /* That doesn't seem sufficient. Give it 1ms. */
  370. local_irq_enable();
  371. mdelay(1);
  372. local_irq_disable();
  373. #endif
  374. }
  375. #endif