irq_32.c 9.9 KB

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