irq_32.c 8.3 KB

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