irq.c 8.4 KB

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