Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 
 11 #include <linux/module.h>
 12 #include <linux/seq_file.h>
 13 #include <linux/interrupt.h>
 14 #include <linux/kernel_stat.h>
 15 #include <linux/notifier.h>
 16 #include <linux/cpu.h>
 17 #include <linux/delay.h>
 18 
 19 #include <linux/ftrace.h>
 20 
 21 #include <asm/apic.h>
 22 #include <asm/uaccess.h>
 23 
 24 DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
 25 EXPORT_PER_CPU_SYMBOL(irq_stat);
 26 
 27 DEFINE_PER_CPU(struct pt_regs *, irq_regs);
 28 EXPORT_PER_CPU_SYMBOL(irq_regs);
 29 
 30 /*
 31  * 'what should we do if we get a hw irq event on an illegal vector'.
 32  * each architecture has to answer this themselves.
 33  */
 34 void ack_bad_irq(unsigned int irq)
 35 {
 36         printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
 37 
 38 #ifdef CONFIG_X86_LOCAL_APIC
 39         /*
 40          * Currently unexpected vectors happen only on SMP and APIC.
 41          * We _must_ ack these because every local APIC has only N
 42          * irq slots per priority level, and a 'hanging, unacked' IRQ
 43          * holds up an irq slot - in excessive cases (when multiple
 44          * unexpected vectors occur) that might lock up the APIC
 45          * completely.
 46          * But only ack when the APIC is enabled -AK
 47          */
 48         if (cpu_has_apic)
 49                 ack_APIC_irq();
 50 #endif
 51 }
 52 
 53 #ifdef CONFIG_4KSTACKS
 54 /*
 55  * per-CPU IRQ handling contexts (thread information and stack)
 56  */
 57 union irq_ctx {
 58         struct thread_info      tinfo;
 59         u32                     stack[THREAD_SIZE/sizeof(u32)];
 60 };
 61 
 62 static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
 63 static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
 64 #endif
 65 
 66 /*
 67  * do_IRQ handles all normal device IRQ's (the special
 68  * SMP cross-CPU interrupts have their own specific
 69  * handlers).
 70  */
 71 unsigned int do_IRQ(struct pt_regs *regs)
 72 {       
 73         struct pt_regs *old_regs;
 74         /* high bit used in ret_from_ code */
 75         int irq = ~regs->orig_ax;
 76         struct irq_desc *desc = irq_desc + irq;
 77 #ifdef CONFIG_4KSTACKS
 78         union irq_ctx *curctx, *irqctx;
 79         u32 *isp;
 80 #endif
 81 
 82 #ifdef CONFIG_X86_LOCAL_APIC
 83         irq_show_regs_callback(smp_processor_id(), regs);
 84 #endif
 85 
 86         if (unlikely((unsigned)irq >= NR_IRQS)) {
 87                 printk(KERN_EMERG "%s: cannot handle IRQ %d\n",
 88                                         __FUNCTION__, irq);
 89                 BUG();
 90         }
 91 
 92         old_regs = set_irq_regs(regs);
 93         irq_enter();
 94         ftrace_event_irq(irq, user_mode(regs), regs->ip);
 95 #ifdef CONFIG_DEBUG_STACKOVERFLOW
 96         /* Debugging check for stack overflow: is there less than 1KB free? */
 97         {
 98                 long sp;
 99 
100                 __asm__ __volatile__("andl %%esp,%0" :
101                                         "=r" (sp) : "" (THREAD_SIZE - 1));
102                 if (unlikely(sp < (sizeof(struct thread_info) + STACK_WARN))) {
103                         printk("BUG: do_IRQ: stack overflow: %ld\n",
104                                 sp - sizeof(struct thread_info));
105                         dump_stack();
106                 }
107         }
108 #endif
109 
110 #ifdef CONFIG_4KSTACKS
111 
112         curctx = (union irq_ctx *) current_thread_info();
113         irqctx = hardirq_ctx[smp_processor_id()];
114 
115         /*
116          * this is where we switch to the IRQ stack. However, if we are
117          * already using the IRQ stack (because we interrupted a hardirq
118          * handler) we can't do that and just have to keep using the
119          * current stack (which is the irq stack already after all)
120          */
121         if (curctx != irqctx) {
122                 int arg1, arg2, bx;
123 
124                 /* build the stack frame on the IRQ stack */
125                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
126                 irqctx->tinfo.task = curctx->tinfo.task;
127                 irqctx->tinfo.previous_esp = current_stack_pointer;
128 
129                 /*
130                  * Copy the softirq bits in preempt_count so that the
131                  * softirq checks work in the hardirq context.
132                  */
133                 irqctx->tinfo.preempt_count =
134                         (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
135                         (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
136 
137                 asm volatile(
138                         "       xchgl  %%ebx,%%esp    \n"
139                         "       call   *%%edi         \n"
140                         "       movl   %%ebx,%%esp    \n"
141                         : "=a" (arg1), "=d" (arg2), "=b" (bx)
142                         :  "" (irq),   "1" (desc),  "2" (isp),
143                            "D" (desc->handle_irq)
144                         : "memory", "cc", "ecx"
145                 );
146         } else
147 #endif
148                 desc->handle_irq(irq, desc);
149 
150         irq_exit();
151         set_irq_regs(old_regs);
152         return 1;
153 }
154 
155 #ifdef CONFIG_4KSTACKS
156 
157 static char softirq_stack[NR_CPUS * THREAD_SIZE]
158                 __attribute__((__section__(".bss.page_aligned")));
159 
160 static char hardirq_stack[NR_CPUS * THREAD_SIZE]
161                 __attribute__((__section__(".bss.page_aligned")));
162 
163 /*
164  * allocate per-cpu stacks for hardirq and for softirq processing
165  */
166 void irq_ctx_init(int cpu)
167 {
168         union irq_ctx *irqctx;
169 
170         if (hardirq_ctx[cpu])
171                 return;
172 
173         irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
174         irqctx->tinfo.task              = NULL;
175         irqctx->tinfo.exec_domain       = NULL;
176         irqctx->tinfo.cpu               = cpu;
177         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
178         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
179 
180         hardirq_ctx[cpu] = irqctx;
181 
182         irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
183         irqctx->tinfo.task              = NULL;
184         irqctx->tinfo.exec_domain       = NULL;
185         irqctx->tinfo.cpu               = cpu;
186         irqctx->tinfo.preempt_count     = 0;
187         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
188 
189         softirq_ctx[cpu] = irqctx;
190 
191         printk("CPU %u irqstacks, hard=%p soft=%p\n",
192                 cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
193 }
194 
195 void irq_ctx_exit(int cpu)
196 {
197         hardirq_ctx[cpu] = NULL;
198 }
199 
200 extern asmlinkage void __do_softirq(void);
201 
202 asmlinkage void do_softirq(void)
203 {
204         unsigned long flags;
205         struct thread_info *curctx;
206         union irq_ctx *irqctx;
207         u32 *isp;
208 
209         if (in_interrupt())
210                 return;
211 
212         local_irq_save(flags);
213 
214         if (local_softirq_pending()) {
215                 curctx = current_thread_info();
216                 irqctx = softirq_ctx[smp_processor_id()];
217                 irqctx->tinfo.task = curctx->task;
218                 irqctx->tinfo.previous_esp = current_stack_pointer;
219 
220                 /* build the stack frame on the softirq stack */
221                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
222 
223                 asm volatile(
224                         "       xchgl   %%ebx,%%esp     \n"
225                         "       call    __do_softirq    \n"
226                         "       movl    %%ebx,%%esp     \n"
227                         : "=b"(isp)
228                         : ""(isp)
229                         : "memory", "cc", "edx", "ecx", "eax"
230                 );
231                 /*
232                  * Shouldnt happen, we returned above if in_interrupt():
233                  */
234                 WARN_ON_ONCE(softirq_count());
235         }
236 
237         local_irq_restore(flags);
238 }
239 #endif
240 
241 /*
242  * Interrupt statistics:
243  */
244 
245 atomic_t irq_err_count;
246 
247 /*
248  * /proc/interrupts printing:
249  */
250 
251 int show_interrupts(struct seq_file *p, void *v)
252 {
253         int i = *(loff_t *) v, j;
254         struct irqaction * action;
255         unsigned long flags;
256 
257         if (i == 0) {
258                 seq_printf(p, "           ");
259                 for_each_online_cpu(j)
260                         seq_printf(p, "CPU%-8d",j);
261                 seq_putc(p, '\n');
262         }
263 
264         if (i < NR_IRQS) {
265                 unsigned any_count = 0;
266 
267                 spin_lock_irqsave(&irq_desc[i].lock, flags);
268 #ifndef CONFIG_SMP
269                 any_count = kstat_irqs(i);
270 #else
271                 for_each_online_cpu(j)
272                         any_count |= kstat_cpu(j).irqs[i];
273 #endif
274                 action = irq_desc[i].action;
275                 if (!action && !any_count)
276                         goto skip;
277                 seq_printf(p, "%3d: ",i);
278 #ifndef CONFIG_SMP
279                 seq_printf(p, "%10u ", kstat_irqs(i));
280 #else
281                 for_each_online_cpu(j)
282                         seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
283 #endif
284                 seq_printf(p, " %8s", irq_desc[i].chip->name);
285                 seq_printf(p, "-%-8s", irq_desc[i].name);
286 
287                 if (action) {
288                         seq_printf(p, "  %s", action->name);
289                         while ((action = action->next) != NULL)
290                                 seq_printf(p, ", %s", action->name);
291                 }
292 
293                 seq_putc(p, '\n');
294 skip:
295                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
296         } else if (i == NR_IRQS) {
297                 seq_printf(p, "NMI: ");
298                 for_each_online_cpu(j)
299                         seq_printf(p, "%10u ", nmi_count(j));
300                 seq_printf(p, "  Non-maskable interrupts\n");
301 #ifdef CONFIG_X86_LOCAL_APIC
302                 seq_printf(p, "LOC: ");
303                 for_each_online_cpu(j)
304                         seq_printf(p, "%10u ",
305                                 per_cpu(irq_stat,j).apic_timer_irqs);
306                 seq_printf(p, "  Local timer interrupts\n");
307 #endif
308 #ifdef CONFIG_SMP
309                 seq_printf(p, "RES: ");
310                 for_each_online_cpu(j)
311                         seq_printf(p, "%10u ",
312                                 per_cpu(irq_stat,j).irq_resched_count);
313                 seq_printf(p, "  Rescheduling interrupts\n");
314                 seq_printf(p, "CAL: ");
315                 for_each_online_cpu(j)
316                         seq_printf(p, "%10u ",
317                                 per_cpu(irq_stat,j).irq_call_count);
318                 seq_printf(p, "  function call interrupts\n");
319                 seq_printf(p, "TLB: ");
320                 for_each_online_cpu(j)
321                         seq_printf(p, "%10u ",
322                                 per_cpu(irq_stat,j).irq_tlb_count);
323                 seq_printf(p, "  TLB shootdowns\n");
324 #endif
325                 seq_printf(p, "TRM: ");
326                 for_each_online_cpu(j)
327                         seq_printf(p, "%10u ",
328                                 per_cpu(irq_stat,j).irq_thermal_count);
329                 seq_printf(p, "  Thermal event interrupts\n");
330                 seq_printf(p, "SPU: ");
331                 for_each_online_cpu(j)
332                         seq_printf(p, "%10u ",
333                                 per_cpu(irq_stat,j).irq_spurious_count);
334                 seq_printf(p, "  Spurious interrupts\n");
335                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
336 #if defined(CONFIG_X86_IO_APIC)
337                 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
338 #endif
339         }
340         return 0;
341 }
342 
343 #ifdef CONFIG_HOTPLUG_CPU
344 #include <mach_apic.h>
345 
346 void fixup_irqs(cpumask_t map)
347 {
348         unsigned int irq;
349         static int warned;
350 
351         for (irq = 0; irq < NR_IRQS; irq++) {
352                 cpumask_t mask;
353                 if (irq == 2)
354                         continue;
355 
356                 cpus_and(mask, irq_desc[irq].affinity, map);
357                 if (any_online_cpu(mask) == NR_CPUS) {
358                         printk("Breaking affinity for irq %i\n", irq);
359                         mask = map;
360                 }
361                 if (irq_desc[irq].chip->set_affinity)
362                         irq_desc[irq].chip->set_affinity(irq, mask);
363                 else if (irq_desc[irq].action && !(warned++))
364                         printk("Cannot set affinity for irq %i\n", irq);
365         }
366 
367 #if 0
368         barrier();
369         /* Ingo Molnar says: "after the IO-APIC masks have been redirected
370            [note the nop - the interrupt-enable boundary on x86 is two
371            instructions from sti] - to flush out pending hardirqs and
372            IPIs. After this point nothing is supposed to reach this CPU." */
373         __asm__ __volatile__("sti; nop; cli");
374         barrier();
375 #else
376         /* That doesn't seem sufficient.  Give it 1ms. */
377         local_irq_enable();
378         mdelay(1);
379         local_irq_disable();
380 #endif
381 }
382 #endif
383 
384 
  This page was automatically generated by the LXR engine.