This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / i386 / kernel / irq.c
1 /*
2  *      linux/arch/i386/kernel/irq.c
3  *
4  *      Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
5  *
6  * This file contains the code used by various IRQ handling routines:
7  * asking for different IRQ's should be done through these routines
8  * instead of just grabbing them. Thus setups with different IRQ numbers
9  * shouldn't result in any weird surprises, and installing new handlers
10  * should be easier.
11  */
12
13 /*
14  * (mostly architecture independent, will move to kernel/irq.c in 2.5.)
15  *
16  * IRQs are in fact implemented a bit like signal handlers for the kernel.
17  * Naturally it's not a 1:1 relation, but there are similarities.
18  */
19
20 #include <linux/config.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/ioport.h>
26 #include <linux/interrupt.h>
27 #include <linux/timex.h>
28 #include <linux/slab.h>
29 #include <linux/random.h>
30 #include <linux/smp_lock.h>
31 #include <linux/init.h>
32 #include <linux/kernel_stat.h>
33 #include <linux/irq.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/kallsyms.h>
37
38 #include <asm/atomic.h>
39 #include <asm/io.h>
40 #include <asm/smp.h>
41 #include <asm/system.h>
42 #include <asm/bitops.h>
43 #include <asm/uaccess.h>
44 #include <asm/delay.h>
45 #include <asm/desc.h>
46 #include <asm/irq.h>
47
48 /*
49  * Linux has a controller-independent x86 interrupt architecture.
50  * every controller has a 'controller-template', that is used
51  * by the main code to do the right thing. Each driver-visible
52  * interrupt source is transparently wired to the apropriate
53  * controller. Thus drivers need not be aware of the
54  * interrupt-controller.
55  *
56  * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
57  * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
58  * (IO-APICs assumed to be messaging to Pentium local-APICs)
59  *
60  * the code is designed to be easily extended with new/different
61  * interrupt controllers, without having to do assembly magic.
62  */
63
64 /*
65  * Controller mappings for all interrupt sources:
66  */
67 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
68         [0 ... NR_IRQS-1] = {
69                 .handler = &no_irq_type,
70                 .lock = SPIN_LOCK_UNLOCKED
71         }
72 };
73
74 static void register_irq_proc (unsigned int irq);
75
76 /*
77  * per-CPU IRQ handling stacks
78  */
79 #ifdef CONFIG_IRQSTACKS
80 union irq_ctx *hardirq_ctx[NR_CPUS];
81 union irq_ctx *softirq_ctx[NR_CPUS];
82 #endif
83
84 /*
85  * Special irq handlers.
86  */
87
88 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
89 { return IRQ_NONE; }
90
91 /*
92  * Generic no controller code
93  */
94
95 static void enable_none(unsigned int irq) { }
96 static unsigned int startup_none(unsigned int irq) { return 0; }
97 static void disable_none(unsigned int irq) { }
98 static void ack_none(unsigned int irq)
99 {
100 /*
101  * 'what should we do if we get a hw irq event on an illegal vector'.
102  * each architecture has to answer this themselves, it doesn't deserve
103  * a generic callback i think.
104  */
105 #ifdef CONFIG_X86
106         printk("unexpected IRQ trap at vector %02x\n", irq);
107 #ifdef CONFIG_X86_LOCAL_APIC
108         /*
109          * Currently unexpected vectors happen only on SMP and APIC.
110          * We _must_ ack these because every local APIC has only N
111          * irq slots per priority level, and a 'hanging, unacked' IRQ
112          * holds up an irq slot - in excessive cases (when multiple
113          * unexpected vectors occur) that might lock up the APIC
114          * completely.
115          */
116         ack_APIC_irq();
117 #endif
118 #endif
119 }
120
121 /* startup is the same as "enable", shutdown is same as "disable" */
122 #define shutdown_none   disable_none
123 #define end_none        enable_none
124
125 struct hw_interrupt_type no_irq_type = {
126         "none",
127         startup_none,
128         shutdown_none,
129         enable_none,
130         disable_none,
131         ack_none,
132         end_none
133 };
134
135 atomic_t irq_err_count;
136 #if defined(CONFIG_X86_IO_APIC) && defined(APIC_MISMATCH_DEBUG)
137 atomic_t irq_mis_count;
138 #endif
139
140 /*
141  * Generic, controller-independent functions:
142  */
143
144 int show_interrupts(struct seq_file *p, void *v)
145 {
146         int i = *(loff_t *) v, j;
147         struct irqaction * action;
148         unsigned long flags;
149
150         if (i == 0) {
151                 seq_printf(p, "           ");
152                 for (j=0; j<NR_CPUS; j++)
153                         if (cpu_online(j))
154                                 seq_printf(p, "CPU%d       ",j);
155                 seq_putc(p, '\n');
156         }
157
158         if (i < NR_IRQS) {
159                 spin_lock_irqsave(&irq_desc[i].lock, flags);
160                 action = irq_desc[i].action;
161                 if (!action) 
162                         goto skip;
163                 seq_printf(p, "%3d: ",i);
164 #ifndef CONFIG_SMP
165                 seq_printf(p, "%10u ", kstat_irqs(i));
166 #else
167                 for (j = 0; j < NR_CPUS; j++)
168                         if (cpu_online(j))
169                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
170 #endif
171                 seq_printf(p, " %14s", irq_desc[i].handler->typename);
172                 seq_printf(p, "  %s", action->name);
173
174                 for (action=action->next; action; action = action->next)
175                         seq_printf(p, ", %s", action->name);
176
177                 seq_putc(p, '\n');
178 skip:
179                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
180         } else if (i == NR_IRQS) {
181                 seq_printf(p, "NMI: ");
182                 for (j = 0; j < NR_CPUS; j++)
183                         if (cpu_online(j))
184                                 seq_printf(p, "%10u ", nmi_count(j));
185                 seq_putc(p, '\n');
186 #ifdef CONFIG_X86_LOCAL_APIC
187                 seq_printf(p, "LOC: ");
188                 for (j = 0; j < NR_CPUS; j++)
189                         if (cpu_online(j))
190                                 seq_printf(p, "%10u ", irq_stat[j].apic_timer_irqs);
191                 seq_putc(p, '\n');
192 #endif
193                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
194 #if defined(CONFIG_X86_IO_APIC) && defined(APIC_MISMATCH_DEBUG)
195                 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
196 #endif
197         }
198         return 0;
199 }
200
201
202
203
204 #ifdef CONFIG_SMP
205 inline void synchronize_irq(unsigned int irq)
206 {
207         while (irq_desc[irq].status & IRQ_INPROGRESS)
208                 cpu_relax();
209 }
210 #endif
211
212 /*
213  * This should really return information about whether
214  * we should do bottom half handling etc. Right now we
215  * end up _always_ checking the bottom half, which is a
216  * waste of time and is not what some drivers would
217  * prefer.
218  */
219 asmlinkage int handle_IRQ_event(unsigned int irq,
220                 struct pt_regs *regs, struct irqaction *action)
221 {
222         int status = 1; /* Force the "do bottom halves" bit */
223         int retval = 0;
224
225         if (!(action->flags & SA_INTERRUPT))
226                 local_irq_enable();
227
228         do {
229                 status |= action->flags;
230                 retval |= action->handler(irq, action->dev_id, regs);
231                 action = action->next;
232         } while (action);
233         if (status & SA_SAMPLE_RANDOM)
234                 add_interrupt_randomness(irq);
235         local_irq_disable();
236         return retval;
237 }
238
239 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
240 {
241         struct irqaction *action;
242
243         if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
244                 printk(KERN_ERR "irq event %d: bogus return value %x\n",
245                                 irq, action_ret);
246         } else {
247                 printk(KERN_ERR "irq %d: nobody cared! (screaming interrupt?)\n", irq);
248                 printk(KERN_ERR "irq %d: Please try booting with acpi=off and report a bug\n", irq);
249         }
250         dump_stack();
251         printk(KERN_ERR "handlers:\n");
252         action = desc->action;
253         do {
254                 printk(KERN_ERR "[<%p>]", action->handler);
255                 print_symbol(" (%s)",
256                         (unsigned long)action->handler);
257                 printk("\n");
258                 action = action->next;
259         } while (action);
260 }
261
262 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
263 {
264         static int count = 100;
265
266         if (count) {
267                 count--;
268                 __report_bad_irq(irq, desc, action_ret);
269         }
270 }
271
272 static int noirqdebug;
273
274 static int __init noirqdebug_setup(char *str)
275 {
276         noirqdebug = 1;
277         printk("IRQ lockup detection disabled\n");
278         return 1;
279 }
280
281 __setup("noirqdebug", noirqdebug_setup);
282
283 /*
284  * If 99,900 of the previous 100,000 interrupts have not been handled then
285  * assume that the IRQ is stuck in some manner.  Drop a diagnostic and try to
286  * turn the IRQ off.
287  *
288  * (The other 100-of-100,000 interrupts may have been a correctly-functioning
289  *  device sharing an IRQ with the failing one)
290  *
291  * Called under desc->lock
292  */
293 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
294 {
295         if (action_ret != IRQ_HANDLED) {
296                 desc->irqs_unhandled++;
297                 if (action_ret != IRQ_NONE)
298                         report_bad_irq(irq, desc, action_ret);
299         }
300
301         desc->irq_count++;
302         if (desc->irq_count < 100000)
303                 return;
304
305         desc->irq_count = 0;
306         if (desc->irqs_unhandled > 99900) {
307                 /*
308                  * The interrupt is stuck
309                  */
310                 __report_bad_irq(irq, desc, action_ret);
311                 /*
312                  * Now kill the IRQ
313                  */
314                 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
315                 desc->status |= IRQ_DISABLED;
316                 desc->handler->disable(irq);
317         }
318         desc->irqs_unhandled = 0;
319 }
320
321 /*
322  * Generic enable/disable code: this just calls
323  * down into the PIC-specific version for the actual
324  * hardware disable after having gotten the irq
325  * controller lock. 
326  */
327  
328 /**
329  *      disable_irq_nosync - disable an irq without waiting
330  *      @irq: Interrupt to disable
331  *
332  *      Disable the selected interrupt line.  Disables and Enables are
333  *      nested.
334  *      Unlike disable_irq(), this function does not ensure existing
335  *      instances of the IRQ handler have completed before returning.
336  *
337  *      This function may be called from IRQ context.
338  */
339  
340 inline void disable_irq_nosync(unsigned int irq)
341 {
342         irq_desc_t *desc = irq_desc + irq;
343         unsigned long flags;
344
345         spin_lock_irqsave(&desc->lock, flags);
346         if (!desc->depth++) {
347                 desc->status |= IRQ_DISABLED;
348                 desc->handler->disable(irq);
349         }
350         spin_unlock_irqrestore(&desc->lock, flags);
351 }
352
353 /**
354  *      disable_irq - disable an irq and wait for completion
355  *      @irq: Interrupt to disable
356  *
357  *      Disable the selected interrupt line.  Enables and Disables are
358  *      nested.
359  *      This function waits for any pending IRQ handlers for this interrupt
360  *      to complete before returning. If you use this function while
361  *      holding a resource the IRQ handler may need you will deadlock.
362  *
363  *      This function may be called - with care - from IRQ context.
364  */
365  
366 void disable_irq(unsigned int irq)
367 {
368         irq_desc_t *desc = irq_desc + irq;
369         disable_irq_nosync(irq);
370         if (desc->action)
371                 synchronize_irq(irq);
372 }
373
374 /**
375  *      enable_irq - enable handling of an irq
376  *      @irq: Interrupt to enable
377  *
378  *      Undoes the effect of one call to disable_irq().  If this
379  *      matches the last disable, processing of interrupts on this
380  *      IRQ line is re-enabled.
381  *
382  *      This function may be called from IRQ context.
383  */
384  
385 void enable_irq(unsigned int irq)
386 {
387         irq_desc_t *desc = irq_desc + irq;
388         unsigned long flags;
389
390         spin_lock_irqsave(&desc->lock, flags);
391         switch (desc->depth) {
392         case 1: {
393                 unsigned int status = desc->status & ~IRQ_DISABLED;
394                 desc->status = status;
395                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
396                         desc->status = status | IRQ_REPLAY;
397                         hw_resend_irq(desc->handler,irq);
398                 }
399                 desc->handler->enable(irq);
400                 /* fall-through */
401         }
402         default:
403                 desc->depth--;
404                 break;
405         case 0:
406                 printk("enable_irq(%u) unbalanced from %p\n", irq,
407                        __builtin_return_address(0));
408         }
409         spin_unlock_irqrestore(&desc->lock, flags);
410 }
411
412 /*
413  * do_IRQ handles all normal device IRQ's (the special
414  * SMP cross-CPU interrupts have their own specific
415  * handlers).
416  */
417 asmlinkage unsigned int do_IRQ(struct pt_regs regs)
418 {       
419         /* 
420          * We ack quickly, we don't want the irq controller
421          * thinking we're snobs just because some other CPU has
422          * disabled global interrupts (we have already done the
423          * INT_ACK cycles, it's too late to try to pretend to the
424          * controller that we aren't taking the interrupt).
425          *
426          * 0 return value means that this irq is already being
427          * handled by some other CPU. (or is disabled)
428          */
429         int irq = regs.orig_eax & 0xff; /* high bits used in ret_from_ code  */
430         irq_desc_t *desc = irq_desc + irq;
431         struct irqaction * action;
432         unsigned int status;
433
434         irq_enter();
435
436 #ifdef CONFIG_DEBUG_STACKOVERFLOW
437         /* Debugging check for stack overflow: is there less than 1KB free? */
438         {
439                 long esp;
440
441                 __asm__ __volatile__("andl %%esp,%0" :
442                                         "=r" (esp) : "0" (THREAD_SIZE - 1));
443                 if (unlikely(esp < (sizeof(struct thread_info) + STACK_WARN))) {
444                         printk("do_IRQ: stack overflow: %ld\n",
445                                 esp - sizeof(struct thread_info));
446                         dump_stack();
447                 }
448         }
449 #endif
450         kstat_this_cpu.irqs[irq]++;
451         spin_lock(&desc->lock);
452         desc->handler->ack(irq);
453         /*
454            REPLAY is when Linux resends an IRQ that was dropped earlier
455            WAITING is used by probe to mark irqs that are being tested
456            */
457         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
458         status |= IRQ_PENDING; /* we _want_ to handle it */
459
460         /*
461          * If the IRQ is disabled for whatever reason, we cannot
462          * use the action we have.
463          */
464         action = NULL;
465         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
466                 action = desc->action;
467                 status &= ~IRQ_PENDING; /* we commit to handling */
468                 status |= IRQ_INPROGRESS; /* we are handling it */
469         }
470         desc->status = status;
471
472         /*
473          * If there is no IRQ handler or it was disabled, exit early.
474            Since we set PENDING, if another processor is handling
475            a different instance of this same irq, the other processor
476            will take care of it.
477          */
478         if (unlikely(!action))
479                 goto out;
480
481         /*
482          * Edge triggered interrupts need to remember
483          * pending events.
484          * This applies to any hw interrupts that allow a second
485          * instance of the same irq to arrive while we are in do_IRQ
486          * or in the handler. But the code here only handles the _second_
487          * instance of the irq, not the third or fourth. So it is mostly
488          * useful for irq hardware that does not mask cleanly in an
489          * SMP environment.
490          */
491
492         for (;;) {
493                 irqreturn_t action_ret;
494                 u32 *isp;
495                 union irq_ctx * curctx;
496                 union irq_ctx * irqctx;
497 #ifdef CONFIG_IRQSTACKS
498                 curctx = (union irq_ctx *) current_thread_info();
499                 irqctx = hardirq_ctx[smp_processor_id()];
500 #else
501                 curctx = irqctx = (union irq_ctx *)0;
502 #endif
503                 spin_unlock(&desc->lock);
504
505                 /*
506                  * this is where we switch to the IRQ stack. However, if we are already using
507                  * the IRQ stack (because we interrupted a hardirq handler) we can't do that
508                  * and just have to keep using the current stack (which is the irq stack already
509                  * after all)
510                  */
511
512                 if (curctx == irqctx)
513                         action_ret = handle_IRQ_event(irq, &regs, action);
514                 else {
515                         /* build the stack frame on the IRQ stack */
516                         isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
517                         irqctx->tinfo.task = curctx->tinfo.task;
518                         irqctx->tinfo.real_stack = curctx->tinfo.real_stack;
519                         irqctx->tinfo.virtual_stack = curctx->tinfo.virtual_stack;
520                         irqctx->tinfo.previous_esp = current_stack_pointer();
521
522                         *--isp = (u32) action;
523                         *--isp = (u32) &regs;
524                         *--isp = (u32) irq;
525
526                         asm volatile(
527                                 "       xchgl   %%ebx,%%esp     \n"
528                                 "       call    handle_IRQ_event \n"
529                                 "       xchgl   %%ebx,%%esp     \n"
530                                 : "=a"(action_ret)
531                                 : "b"(isp)
532                                 : "memory", "cc", "edx", "ecx"
533                         );
534
535
536                 }
537                 spin_lock(&desc->lock);
538                 if (!noirqdebug)
539                         note_interrupt(irq, desc, action_ret);
540                 if (curctx != irqctx)
541                         irqctx->tinfo.task = NULL;
542                 if (likely(!(desc->status & IRQ_PENDING)))
543                         break;
544                 desc->status &= ~IRQ_PENDING;
545         }
546         desc->status &= ~IRQ_INPROGRESS;
547
548 out:
549         /*
550          * The ->end() handler has to deal with interrupts which got
551          * disabled while the handler was running.
552          */
553         desc->handler->end(irq);
554         spin_unlock(&desc->lock);
555
556         irq_exit();
557
558         return 1;
559 }
560
561 int can_request_irq(unsigned int irq, unsigned long irqflags)
562 {
563         struct irqaction *action;
564
565         if (irq >= NR_IRQS)
566                 return 0;
567         action = irq_desc[irq].action;
568         if (action) {
569                 if (irqflags & action->flags & SA_SHIRQ)
570                         action = NULL;
571         }
572         return !action;
573 }
574
575 /**
576  *      request_irq - allocate an interrupt line
577  *      @irq: Interrupt line to allocate
578  *      @handler: Function to be called when the IRQ occurs
579  *      @irqflags: Interrupt type flags
580  *      @devname: An ascii name for the claiming device
581  *      @dev_id: A cookie passed back to the handler function
582  *
583  *      This call allocates interrupt resources and enables the
584  *      interrupt line and IRQ handling. From the point this
585  *      call is made your handler function may be invoked. Since
586  *      your handler function must clear any interrupt the board 
587  *      raises, you must take care both to initialise your hardware
588  *      and to set up the interrupt handler in the right order.
589  *
590  *      Dev_id must be globally unique. Normally the address of the
591  *      device data structure is used as the cookie. Since the handler
592  *      receives this value it makes sense to use it.
593  *
594  *      If your interrupt is shared you must pass a non NULL dev_id
595  *      as this is required when freeing the interrupt.
596  *
597  *      Flags:
598  *
599  *      SA_SHIRQ                Interrupt is shared
600  *
601  *      SA_INTERRUPT            Disable local interrupts while processing
602  *
603  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
604  *
605  */
606  
607 int request_irq(unsigned int irq, 
608                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
609                 unsigned long irqflags, 
610                 const char * devname,
611                 void *dev_id)
612 {
613         int retval;
614         struct irqaction * action;
615
616 #if 1
617         /*
618          * Sanity-check: shared interrupts should REALLY pass in
619          * a real dev-ID, otherwise we'll have trouble later trying
620          * to figure out which interrupt is which (messes up the
621          * interrupt freeing logic etc).
622          */
623         if (irqflags & SA_SHIRQ) {
624                 if (!dev_id)
625                         printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
626         }
627 #endif
628
629         if (irq >= NR_IRQS)
630                 return -EINVAL;
631         if (!handler)
632                 return -EINVAL;
633
634         action = (struct irqaction *)
635                         kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
636         if (!action)
637                 return -ENOMEM;
638
639         action->handler = handler;
640         action->flags = irqflags;
641         cpus_clear(action->mask);
642         action->name = devname;
643         action->next = NULL;
644         action->dev_id = dev_id;
645
646         retval = setup_irq(irq, action);
647         if (retval)
648                 kfree(action);
649         return retval;
650 }
651
652 EXPORT_SYMBOL(request_irq);
653
654 /**
655  *      free_irq - free an interrupt
656  *      @irq: Interrupt line to free
657  *      @dev_id: Device identity to free
658  *
659  *      Remove an interrupt handler. The handler is removed and if the
660  *      interrupt line is no longer in use by any driver it is disabled.
661  *      On a shared IRQ the caller must ensure the interrupt is disabled
662  *      on the card it drives before calling this function. The function
663  *      does not return until any executing interrupts for this IRQ
664  *      have completed.
665  *
666  *      This function must not be called from interrupt context. 
667  */
668  
669 void free_irq(unsigned int irq, void *dev_id)
670 {
671         irq_desc_t *desc;
672         struct irqaction **p;
673         unsigned long flags;
674
675         if (irq >= NR_IRQS)
676                 return;
677
678         desc = irq_desc + irq;
679         spin_lock_irqsave(&desc->lock,flags);
680         p = &desc->action;
681         for (;;) {
682                 struct irqaction * action = *p;
683                 if (action) {
684                         struct irqaction **pp = p;
685                         p = &action->next;
686                         if (action->dev_id != dev_id)
687                                 continue;
688
689                         /* Found it - now remove it from the list of entries */
690                         *pp = action->next;
691                         if (!desc->action) {
692                                 desc->status |= IRQ_DISABLED;
693                                 desc->handler->shutdown(irq);
694                         }
695                         spin_unlock_irqrestore(&desc->lock,flags);
696
697                         /* Wait to make sure it's not being used on another CPU */
698                         synchronize_irq(irq);
699                         kfree(action);
700                         return;
701                 }
702                 printk("Trying to free free IRQ%d\n",irq);
703                 spin_unlock_irqrestore(&desc->lock,flags);
704                 return;
705         }
706 }
707
708 EXPORT_SYMBOL(free_irq);
709
710 /*
711  * IRQ autodetection code..
712  *
713  * This depends on the fact that any interrupt that
714  * comes in on to an unassigned handler will get stuck
715  * with "IRQ_WAITING" cleared and the interrupt
716  * disabled.
717  */
718
719 static DECLARE_MUTEX(probe_sem);
720
721 /**
722  *      probe_irq_on    - begin an interrupt autodetect
723  *
724  *      Commence probing for an interrupt. The interrupts are scanned
725  *      and a mask of potential interrupt lines is returned.
726  *
727  */
728  
729 unsigned long probe_irq_on(void)
730 {
731         unsigned int i;
732         irq_desc_t *desc;
733         unsigned long val;
734         unsigned long delay;
735
736         down(&probe_sem);
737         /* 
738          * something may have generated an irq long ago and we want to
739          * flush such a longstanding irq before considering it as spurious. 
740          */
741         for (i = NR_IRQS-1; i > 0; i--)  {
742                 desc = irq_desc + i;
743
744                 spin_lock_irq(&desc->lock);
745                 if (!irq_desc[i].action) 
746                         irq_desc[i].handler->startup(i);
747                 spin_unlock_irq(&desc->lock);
748         }
749
750         /* Wait for longstanding interrupts to trigger. */
751         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
752                 /* about 20ms delay */ barrier();
753
754         /*
755          * enable any unassigned irqs
756          * (we must startup again here because if a longstanding irq
757          * happened in the previous stage, it may have masked itself)
758          */
759         for (i = NR_IRQS-1; i > 0; i--) {
760                 desc = irq_desc + i;
761
762                 spin_lock_irq(&desc->lock);
763                 if (!desc->action) {
764                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
765                         if (desc->handler->startup(i))
766                                 desc->status |= IRQ_PENDING;
767                 }
768                 spin_unlock_irq(&desc->lock);
769         }
770
771         /*
772          * Wait for spurious interrupts to trigger
773          */
774         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
775                 /* about 100ms delay */ barrier();
776
777         /*
778          * Now filter out any obviously spurious interrupts
779          */
780         val = 0;
781         for (i = 0; i < NR_IRQS; i++) {
782                 irq_desc_t *desc = irq_desc + i;
783                 unsigned int status;
784
785                 spin_lock_irq(&desc->lock);
786                 status = desc->status;
787
788                 if (status & IRQ_AUTODETECT) {
789                         /* It triggered already - consider it spurious. */
790                         if (!(status & IRQ_WAITING)) {
791                                 desc->status = status & ~IRQ_AUTODETECT;
792                                 desc->handler->shutdown(i);
793                         } else
794                                 if (i < 32)
795                                         val |= 1 << i;
796                 }
797                 spin_unlock_irq(&desc->lock);
798         }
799
800         return val;
801 }
802
803 EXPORT_SYMBOL(probe_irq_on);
804
805 /*
806  * Return a mask of triggered interrupts (this
807  * can handle only legacy ISA interrupts).
808  */
809  
810 /**
811  *      probe_irq_mask - scan a bitmap of interrupt lines
812  *      @val:   mask of interrupts to consider
813  *
814  *      Scan the ISA bus interrupt lines and return a bitmap of
815  *      active interrupts. The interrupt probe logic state is then
816  *      returned to its previous value.
817  *
818  *      Note: we need to scan all the irq's even though we will
819  *      only return ISA irq numbers - just so that we reset them
820  *      all to a known state.
821  */
822 unsigned int probe_irq_mask(unsigned long val)
823 {
824         int i;
825         unsigned int mask;
826
827         mask = 0;
828         for (i = 0; i < NR_IRQS; i++) {
829                 irq_desc_t *desc = irq_desc + i;
830                 unsigned int status;
831
832                 spin_lock_irq(&desc->lock);
833                 status = desc->status;
834
835                 if (status & IRQ_AUTODETECT) {
836                         if (i < 16 && !(status & IRQ_WAITING))
837                                 mask |= 1 << i;
838
839                         desc->status = status & ~IRQ_AUTODETECT;
840                         desc->handler->shutdown(i);
841                 }
842                 spin_unlock_irq(&desc->lock);
843         }
844         up(&probe_sem);
845
846         return mask & val;
847 }
848
849 /*
850  * Return the one interrupt that triggered (this can
851  * handle any interrupt source).
852  */
853
854 /**
855  *      probe_irq_off   - end an interrupt autodetect
856  *      @val: mask of potential interrupts (unused)
857  *
858  *      Scans the unused interrupt lines and returns the line which
859  *      appears to have triggered the interrupt. If no interrupt was
860  *      found then zero is returned. If more than one interrupt is
861  *      found then minus the first candidate is returned to indicate
862  *      their is doubt.
863  *
864  *      The interrupt probe logic state is returned to its previous
865  *      value.
866  *
867  *      BUGS: When used in a module (which arguably shouldnt happen)
868  *      nothing prevents two IRQ probe callers from overlapping. The
869  *      results of this are non-optimal.
870  */
871  
872 int probe_irq_off(unsigned long val)
873 {
874         int i, irq_found, nr_irqs;
875
876         nr_irqs = 0;
877         irq_found = 0;
878         for (i = 0; i < NR_IRQS; i++) {
879                 irq_desc_t *desc = irq_desc + i;
880                 unsigned int status;
881
882                 spin_lock_irq(&desc->lock);
883                 status = desc->status;
884
885                 if (status & IRQ_AUTODETECT) {
886                         if (!(status & IRQ_WAITING)) {
887                                 if (!nr_irqs)
888                                         irq_found = i;
889                                 nr_irqs++;
890                         }
891                         desc->status = status & ~IRQ_AUTODETECT;
892                         desc->handler->shutdown(i);
893                 }
894                 spin_unlock_irq(&desc->lock);
895         }
896         up(&probe_sem);
897
898         if (nr_irqs > 1)
899                 irq_found = -irq_found;
900         return irq_found;
901 }
902
903 EXPORT_SYMBOL(probe_irq_off);
904
905 /* this was setup_x86_irq but it seems pretty generic */
906 int setup_irq(unsigned int irq, struct irqaction * new)
907 {
908         int shared = 0;
909         unsigned long flags;
910         struct irqaction *old, **p;
911         irq_desc_t *desc = irq_desc + irq;
912
913         if (desc->handler == &no_irq_type)
914                 return -ENOSYS;
915         /*
916          * Some drivers like serial.c use request_irq() heavily,
917          * so we have to be careful not to interfere with a
918          * running system.
919          */
920         if (new->flags & SA_SAMPLE_RANDOM) {
921                 /*
922                  * This function might sleep, we want to call it first,
923                  * outside of the atomic block.
924                  * Yes, this might clear the entropy pool if the wrong
925                  * driver is attempted to be loaded, without actually
926                  * installing a new handler, but is this really a problem,
927                  * only the sysadmin is able to do this.
928                  */
929                 rand_initialize_irq(irq);
930         }
931
932         /*
933          * The following block of code has to be executed atomically
934          */
935         spin_lock_irqsave(&desc->lock,flags);
936         p = &desc->action;
937         if ((old = *p) != NULL) {
938                 /* Can't share interrupts unless both agree to */
939                 if (!(old->flags & new->flags & SA_SHIRQ)) {
940                         spin_unlock_irqrestore(&desc->lock,flags);
941                         return -EBUSY;
942                 }
943
944                 /* add new interrupt at end of irq queue */
945                 do {
946                         p = &old->next;
947                         old = *p;
948                 } while (old);
949                 shared = 1;
950         }
951
952         *p = new;
953
954         if (!shared) {
955                 desc->depth = 0;
956                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
957                 desc->handler->startup(irq);
958         }
959         spin_unlock_irqrestore(&desc->lock,flags);
960
961         register_irq_proc(irq);
962         return 0;
963 }
964
965 static struct proc_dir_entry * root_irq_dir;
966 static struct proc_dir_entry * irq_dir [NR_IRQS];
967
968 #ifdef CONFIG_SMP
969
970 static struct proc_dir_entry *smp_affinity_entry[NR_IRQS];
971
972 cpumask_t irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
973
974 static int irq_affinity_read_proc(char *page, char **start, off_t off,
975                         int count, int *eof, void *data)
976 {
977         int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
978         if (count - len < 2)
979                 return -EINVAL;
980         len += sprintf(page + len, "\n");
981         return len;
982 }
983
984 static int irq_affinity_write_proc(struct file *file, const char __user *buffer,
985                                         unsigned long count, void *data)
986 {
987         int irq = (long)data, full_count = count, err;
988         cpumask_t new_value, tmp;
989
990         if (!irq_desc[irq].handler->set_affinity)
991                 return -EIO;
992
993         err = cpumask_parse(buffer, count, new_value);
994         if (err)
995                 return err;
996
997         /*
998          * Do not allow disabling IRQs completely - it's a too easy
999          * way to make the system unusable accidentally :-) At least
1000          * one online CPU still has to be targeted.
1001          */
1002         cpus_and(tmp, new_value, cpu_online_map);
1003         if (cpus_empty(tmp))
1004                 return -EINVAL;
1005
1006         irq_affinity[irq] = new_value;
1007         irq_desc[irq].handler->set_affinity(irq,
1008                                         cpumask_of_cpu(first_cpu(new_value)));
1009
1010         return full_count;
1011 }
1012
1013 #endif
1014
1015 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
1016                         int count, int *eof, void *data)
1017 {
1018         int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
1019         if (count - len < 2)
1020                 return -EINVAL;
1021         len += sprintf(page + len, "\n");
1022         return len;
1023 }
1024
1025 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
1026                                         unsigned long count, void *data)
1027 {
1028         cpumask_t *mask = (cpumask_t *)data;
1029         unsigned long full_count = count, err;
1030         cpumask_t new_value;
1031
1032         err = cpumask_parse(buffer, count, new_value);
1033         if (err)
1034                 return err;
1035
1036         *mask = new_value;
1037         return full_count;
1038 }
1039
1040 #define MAX_NAMELEN 10
1041
1042 static void register_irq_proc (unsigned int irq)
1043 {
1044         char name [MAX_NAMELEN];
1045
1046         if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
1047                         irq_dir[irq])
1048                 return;
1049
1050         memset(name, 0, MAX_NAMELEN);
1051         sprintf(name, "%d", irq);
1052
1053         /* create /proc/irq/1234 */
1054         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1055
1056 #ifdef CONFIG_SMP
1057         {
1058                 struct proc_dir_entry *entry;
1059
1060                 /* create /proc/irq/1234/smp_affinity */
1061                 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1062
1063                 if (entry) {
1064                         entry->nlink = 1;
1065                         entry->data = (void *)(long)irq;
1066                         entry->read_proc = irq_affinity_read_proc;
1067                         entry->write_proc = irq_affinity_write_proc;
1068                 }
1069
1070                 smp_affinity_entry[irq] = entry;
1071         }
1072 #endif
1073 }
1074
1075 unsigned long prof_cpu_mask = -1;
1076
1077 void init_irq_proc (void)
1078 {
1079         struct proc_dir_entry *entry;
1080         int i;
1081
1082         /* create /proc/irq */
1083         root_irq_dir = proc_mkdir("irq", NULL);
1084
1085         /* create /proc/irq/prof_cpu_mask */
1086         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
1087
1088         if (!entry)
1089             return;
1090
1091         entry->nlink = 1;
1092         entry->data = (void *)&prof_cpu_mask;
1093         entry->read_proc = prof_cpu_mask_read_proc;
1094         entry->write_proc = prof_cpu_mask_write_proc;
1095
1096         /*
1097          * Create entries for all existing IRQs.
1098          */
1099         for (i = 0; i < NR_IRQS; i++)
1100                 register_irq_proc(i);
1101 }
1102
1103
1104 #ifdef CONFIG_IRQSTACKS
1105 /*
1106  * These should really be __section__(".bss.page_aligned") as well, but
1107  * gcc's 3.0 and earlier don't handle that correctly.
1108  */
1109 static char softirq_stack[NR_CPUS * THREAD_SIZE]  __attribute__((__aligned__(THREAD_SIZE)));
1110 static char hardirq_stack[NR_CPUS * THREAD_SIZE]  __attribute__((__aligned__(THREAD_SIZE)));
1111
1112 /*
1113  * allocate per-cpu stacks for hardirq and for softirq processing
1114  */
1115 void irq_ctx_init(int cpu)
1116 {
1117         union irq_ctx *irqctx;
1118
1119         if (hardirq_ctx[cpu])
1120                 return;
1121
1122         irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
1123         irqctx->tinfo.task              = NULL;
1124         irqctx->tinfo.exec_domain       = NULL;
1125         irqctx->tinfo.cpu               = cpu;
1126         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
1127         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
1128
1129         hardirq_ctx[cpu] = irqctx;
1130
1131         irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
1132         irqctx->tinfo.task              = NULL;
1133         irqctx->tinfo.exec_domain       = NULL;
1134         irqctx->tinfo.cpu               = cpu;
1135         irqctx->tinfo.preempt_count     = SOFTIRQ_OFFSET;
1136         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
1137
1138         softirq_ctx[cpu] = irqctx;
1139
1140         printk("CPU %u irqstacks, hard=%p soft=%p\n",
1141                 cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
1142 }
1143
1144 extern asmlinkage void __do_softirq(void);
1145
1146 asmlinkage void do_softirq(void)
1147 {
1148         unsigned long flags;
1149         struct thread_info *curctx;
1150         union irq_ctx *irqctx;
1151         u32 *isp;
1152
1153         if (in_interrupt())
1154                 return;
1155
1156         local_irq_save(flags);
1157
1158         if (local_softirq_pending()) {
1159                 curctx = current_thread_info();
1160                 irqctx = softirq_ctx[smp_processor_id()];
1161                 irqctx->tinfo.task = curctx->task;
1162                 irqctx->tinfo.real_stack = curctx->real_stack;
1163                 irqctx->tinfo.virtual_stack = curctx->virtual_stack;
1164                 irqctx->tinfo.previous_esp = current_stack_pointer();
1165
1166                 /* build the stack frame on the softirq stack */
1167                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
1168
1169
1170                 asm volatile(
1171                         "       xchgl   %%ebx,%%esp     \n"
1172                         "       call    __do_softirq    \n"
1173                         "       movl    %%ebx,%%esp     \n"
1174                         : "=b"(isp)
1175                         : "0"(isp)
1176                         : "memory", "cc", "edx", "ecx", "eax"
1177                 );
1178         }
1179
1180         local_irq_restore(flags);
1181 }
1182
1183 EXPORT_SYMBOL(do_softirq);
1184 #endif