irq.c 8.0 KB

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