irq_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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]
  73. __attribute__((__section__(".bss.page_aligned")));
  74. static char hardirq_stack[NR_CPUS * THREAD_SIZE]
  75. __attribute__((__section__(".bss.page_aligned")));
  76. static void call_on_stack(void *func, void *stack)
  77. {
  78. asm volatile("xchgl %%ebx,%%esp \n"
  79. "call *%%edi \n"
  80. "movl %%ebx,%%esp \n"
  81. : "=b" (stack)
  82. : "0" (stack),
  83. "D"(func)
  84. : "memory", "cc", "edx", "ecx", "eax");
  85. }
  86. static inline int
  87. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
  88. {
  89. union irq_ctx *curctx, *irqctx;
  90. u32 *isp, arg1, arg2;
  91. curctx = (union irq_ctx *) current_thread_info();
  92. irqctx = hardirq_ctx[smp_processor_id()];
  93. /*
  94. * this is where we switch to the IRQ stack. However, if we are
  95. * already using the IRQ stack (because we interrupted a hardirq
  96. * handler) we can't do that and just have to keep using the
  97. * current stack (which is the irq stack already after all)
  98. */
  99. if (unlikely(curctx == irqctx))
  100. return 0;
  101. /* build the stack frame on the IRQ stack */
  102. isp = (u32 *) ((char*)irqctx + sizeof(*irqctx));
  103. irqctx->tinfo.task = curctx->tinfo.task;
  104. irqctx->tinfo.previous_esp = current_stack_pointer;
  105. /*
  106. * Copy the softirq bits in preempt_count so that the
  107. * softirq checks work in the hardirq context.
  108. */
  109. irqctx->tinfo.preempt_count =
  110. (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
  111. (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
  112. if (unlikely(overflow))
  113. call_on_stack(print_stack_overflow, isp);
  114. asm volatile("xchgl %%ebx,%%esp \n"
  115. "call *%%edi \n"
  116. "movl %%ebx,%%esp \n"
  117. : "=a" (arg1), "=d" (arg2), "=b" (isp)
  118. : "0" (irq), "1" (desc), "2" (isp),
  119. "D" (desc->handle_irq)
  120. : "memory", "cc", "ecx");
  121. return 1;
  122. }
  123. /*
  124. * allocate per-cpu stacks for hardirq and for softirq processing
  125. */
  126. void __cpuinit irq_ctx_init(int cpu)
  127. {
  128. union irq_ctx *irqctx;
  129. if (hardirq_ctx[cpu])
  130. return;
  131. irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
  132. irqctx->tinfo.task = NULL;
  133. irqctx->tinfo.exec_domain = NULL;
  134. irqctx->tinfo.cpu = cpu;
  135. irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
  136. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  137. hardirq_ctx[cpu] = irqctx;
  138. irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
  139. irqctx->tinfo.task = NULL;
  140. irqctx->tinfo.exec_domain = NULL;
  141. irqctx->tinfo.cpu = cpu;
  142. irqctx->tinfo.preempt_count = 0;
  143. irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
  144. softirq_ctx[cpu] = irqctx;
  145. printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
  146. cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
  147. }
  148. void irq_ctx_exit(int cpu)
  149. {
  150. hardirq_ctx[cpu] = NULL;
  151. }
  152. asmlinkage void do_softirq(void)
  153. {
  154. unsigned long flags;
  155. struct thread_info *curctx;
  156. union irq_ctx *irqctx;
  157. u32 *isp;
  158. if (in_interrupt())
  159. return;
  160. local_irq_save(flags);
  161. if (local_softirq_pending()) {
  162. curctx = current_thread_info();
  163. irqctx = softirq_ctx[smp_processor_id()];
  164. irqctx->tinfo.task = curctx->task;
  165. irqctx->tinfo.previous_esp = current_stack_pointer;
  166. /* build the stack frame on the softirq stack */
  167. isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
  168. call_on_stack(__do_softirq, isp);
  169. /*
  170. * Shouldnt happen, we returned above if in_interrupt():
  171. */
  172. WARN_ON_ONCE(softirq_count());
  173. }
  174. local_irq_restore(flags);
  175. }
  176. #else
  177. static inline int
  178. execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; }
  179. #endif
  180. /*
  181. * do_IRQ handles all normal device IRQ's (the special
  182. * SMP cross-CPU interrupts have their own specific
  183. * handlers).
  184. */
  185. unsigned int do_IRQ(struct pt_regs *regs)
  186. {
  187. struct pt_regs *old_regs;
  188. /* high bit used in ret_from_ code */
  189. int overflow, irq = ~regs->orig_ax;
  190. struct irq_desc *desc = irq_desc + irq;
  191. if (unlikely((unsigned)irq >= NR_IRQS)) {
  192. printk(KERN_EMERG "%s: cannot handle IRQ %d\n",
  193. __func__, irq);
  194. BUG();
  195. }
  196. old_regs = set_irq_regs(regs);
  197. irq_enter();
  198. overflow = check_stack_overflow();
  199. if (!execute_on_irq_stack(overflow, desc, irq)) {
  200. if (unlikely(overflow))
  201. print_stack_overflow();
  202. desc->handle_irq(irq, desc);
  203. }
  204. irq_exit();
  205. set_irq_regs(old_regs);
  206. return 1;
  207. }
  208. /*
  209. * Interrupt statistics:
  210. */
  211. atomic_t irq_err_count;
  212. /*
  213. * /proc/interrupts printing:
  214. */
  215. int show_interrupts(struct seq_file *p, void *v)
  216. {
  217. int i = *(loff_t *) v, j;
  218. struct irqaction * action;
  219. unsigned long flags;
  220. if (i == 0) {
  221. seq_printf(p, " ");
  222. for_each_online_cpu(j)
  223. seq_printf(p, "CPU%-8d",j);
  224. seq_putc(p, '\n');
  225. }
  226. if (i < NR_IRQS) {
  227. unsigned any_count = 0;
  228. spin_lock_irqsave(&irq_desc[i].lock, flags);
  229. #ifndef CONFIG_SMP
  230. any_count = kstat_irqs(i);
  231. #else
  232. for_each_online_cpu(j)
  233. any_count |= kstat_cpu(j).irqs[i];
  234. #endif
  235. action = irq_desc[i].action;
  236. if (!action && !any_count)
  237. goto skip;
  238. seq_printf(p, "%3d: ",i);
  239. #ifndef CONFIG_SMP
  240. seq_printf(p, "%10u ", kstat_irqs(i));
  241. #else
  242. for_each_online_cpu(j)
  243. seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
  244. #endif
  245. seq_printf(p, " %8s", irq_desc[i].chip->name);
  246. seq_printf(p, "-%-8s", irq_desc[i].name);
  247. if (action) {
  248. seq_printf(p, " %s", action->name);
  249. while ((action = action->next) != NULL)
  250. seq_printf(p, ", %s", action->name);
  251. }
  252. seq_putc(p, '\n');
  253. skip:
  254. spin_unlock_irqrestore(&irq_desc[i].lock, flags);
  255. } else if (i == NR_IRQS) {
  256. seq_printf(p, "NMI: ");
  257. for_each_online_cpu(j)
  258. seq_printf(p, "%10u ", nmi_count(j));
  259. seq_printf(p, " Non-maskable interrupts\n");
  260. #ifdef CONFIG_X86_LOCAL_APIC
  261. seq_printf(p, "LOC: ");
  262. for_each_online_cpu(j)
  263. seq_printf(p, "%10u ",
  264. per_cpu(irq_stat,j).apic_timer_irqs);
  265. seq_printf(p, " Local timer interrupts\n");
  266. #endif
  267. #ifdef CONFIG_SMP
  268. seq_printf(p, "RES: ");
  269. for_each_online_cpu(j)
  270. seq_printf(p, "%10u ",
  271. per_cpu(irq_stat,j).irq_resched_count);
  272. seq_printf(p, " Rescheduling interrupts\n");
  273. seq_printf(p, "CAL: ");
  274. for_each_online_cpu(j)
  275. seq_printf(p, "%10u ",
  276. per_cpu(irq_stat,j).irq_call_count);
  277. seq_printf(p, " function call interrupts\n");
  278. seq_printf(p, "TLB: ");
  279. for_each_online_cpu(j)
  280. seq_printf(p, "%10u ",
  281. per_cpu(irq_stat,j).irq_tlb_count);
  282. seq_printf(p, " TLB shootdowns\n");
  283. #endif
  284. #ifdef CONFIG_X86_MCE
  285. seq_printf(p, "TRM: ");
  286. for_each_online_cpu(j)
  287. seq_printf(p, "%10u ",
  288. per_cpu(irq_stat,j).irq_thermal_count);
  289. seq_printf(p, " Thermal event interrupts\n");
  290. #endif
  291. #ifdef CONFIG_X86_LOCAL_APIC
  292. seq_printf(p, "SPU: ");
  293. for_each_online_cpu(j)
  294. seq_printf(p, "%10u ",
  295. per_cpu(irq_stat,j).irq_spurious_count);
  296. seq_printf(p, " Spurious interrupts\n");
  297. #endif
  298. seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
  299. #if defined(CONFIG_X86_IO_APIC)
  300. seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
  301. #endif
  302. }
  303. return 0;
  304. }
  305. /*
  306. * /proc/stat helpers
  307. */
  308. u64 arch_irq_stat_cpu(unsigned int cpu)
  309. {
  310. u64 sum = nmi_count(cpu);
  311. #ifdef CONFIG_X86_LOCAL_APIC
  312. sum += per_cpu(irq_stat, cpu).apic_timer_irqs;
  313. #endif
  314. #ifdef CONFIG_SMP
  315. sum += per_cpu(irq_stat, cpu).irq_resched_count;
  316. sum += per_cpu(irq_stat, cpu).irq_call_count;
  317. sum += per_cpu(irq_stat, cpu).irq_tlb_count;
  318. #endif
  319. #ifdef CONFIG_X86_MCE
  320. sum += per_cpu(irq_stat, cpu).irq_thermal_count;
  321. #endif
  322. #ifdef CONFIG_X86_LOCAL_APIC
  323. sum += per_cpu(irq_stat, cpu).irq_spurious_count;
  324. #endif
  325. return sum;
  326. }
  327. u64 arch_irq_stat(void)
  328. {
  329. u64 sum = atomic_read(&irq_err_count);
  330. #ifdef CONFIG_X86_IO_APIC
  331. sum += atomic_read(&irq_mis_count);
  332. #endif
  333. return sum;
  334. }
  335. #ifdef CONFIG_HOTPLUG_CPU
  336. #include <mach_apic.h>
  337. void fixup_irqs(cpumask_t map)
  338. {
  339. unsigned int irq;
  340. static int warned;
  341. for (irq = 0; irq < NR_IRQS; irq++) {
  342. cpumask_t mask;
  343. if (irq == 2)
  344. continue;
  345. cpus_and(mask, irq_desc[irq].affinity, map);
  346. if (any_online_cpu(mask) == NR_CPUS) {
  347. printk("Breaking affinity for irq %i\n", irq);
  348. mask = map;
  349. }
  350. if (irq_desc[irq].chip->set_affinity)
  351. irq_desc[irq].chip->set_affinity(irq, mask);
  352. else if (irq_desc[irq].action && !(warned++))
  353. printk("Cannot set affinity for irq %i\n", irq);
  354. }
  355. #if 0
  356. barrier();
  357. /* Ingo Molnar says: "after the IO-APIC masks have been redirected
  358. [note the nop - the interrupt-enable boundary on x86 is two
  359. instructions from sti] - to flush out pending hardirqs and
  360. IPIs. After this point nothing is supposed to reach this CPU." */
  361. __asm__ __volatile__("sti; nop; cli");
  362. barrier();
  363. #else
  364. /* That doesn't seem sufficient. Give it 1ms. */
  365. local_irq_enable();
  366. mdelay(1);
  367. local_irq_disable();
  368. #endif
  369. }
  370. #endif