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