ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / kernel / irq.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Code to handle x86 style IRQs plus some generic interrupt stuff.
7  *
8  * Copyright (C) 1992 Linus Torvalds
9  * Copyright (C) 1994 - 2000 Ralf Baechle
10  */
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/module.h>
18 #include <linux/proc_fs.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <linux/random.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/kallsyms.h>
25
26 #include <asm/atomic.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29
30 /*
31  * Controller mappings for all interrupt sources:
32  */
33 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
34         [0 ... NR_IRQS-1] = {
35                 .handler = &no_irq_type,
36                 .lock = SPIN_LOCK_UNLOCKED
37         }
38 };
39
40 static void register_irq_proc (unsigned int irq);
41
42 /*
43  * Special irq handlers.
44  */
45
46 irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
47 { return IRQ_NONE; }
48
49 /*
50  * Generic no controller code
51  */
52
53 static void enable_none(unsigned int irq) { }
54 static unsigned int startup_none(unsigned int irq) { return 0; }
55 static void disable_none(unsigned int irq) { }
56 static void ack_none(unsigned int irq)
57 {
58         /*
59          * 'what should we do if we get a hw irq event on an illegal vector'.
60          * each architecture has to answer this themselves, it doesn't deserve
61          * a generic callback i think.
62          */
63         printk("unexpected interrupt %d\n", irq);
64 }
65
66 /* startup is the same as "enable", shutdown is same as "disable" */
67 #define shutdown_none   disable_none
68 #define end_none        enable_none
69
70 struct hw_interrupt_type no_irq_type = {
71         "none",
72         startup_none,
73         shutdown_none,
74         enable_none,
75         disable_none,
76         ack_none,
77         end_none
78 };
79
80 atomic_t irq_err_count;
81
82 /*
83  * Generic, controller-independent functions:
84  */
85
86 int show_interrupts(struct seq_file *p, void *v)
87 {
88         int i = *(loff_t *) v, j;
89         struct irqaction * action;
90         unsigned long flags;
91
92         if (i == 0) {
93                 seq_printf(p, "           ");
94                 for (j=0; j<NR_CPUS; j++)
95                         if (cpu_online(j))
96                                 seq_printf(p, "CPU%d       ",j);
97                 seq_putc(p, '\n');
98         }
99
100         if (i < NR_IRQS) {
101                 spin_lock_irqsave(&irq_desc[i].lock, flags);
102                 action = irq_desc[i].action;
103                 if (!action) 
104                         goto skip;
105                 seq_printf(p, "%3d: ",i);
106 #ifndef CONFIG_SMP
107                 seq_printf(p, "%10u ", kstat_irqs(i));
108 #else
109                 for (j = 0; j < NR_CPUS; j++)
110                         if (cpu_online(j))
111                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
112 #endif
113                 seq_printf(p, " %14s", irq_desc[i].handler->typename);
114                 seq_printf(p, "  %s", action->name);
115
116                 for (action=action->next; action; action = action->next)
117                         seq_printf(p, ", %s", action->name);
118
119                 seq_putc(p, '\n');
120 skip:
121                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
122         } else if (i == NR_IRQS) {
123                 seq_putc(p, '\n');
124                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
125         }
126         return 0;
127 }
128
129 #ifdef CONFIG_SMP
130 inline void synchronize_irq(unsigned int irq)
131 {
132         while (irq_desc[irq].status & IRQ_INPROGRESS)
133                 cpu_relax();
134 }
135 #endif
136
137 /*
138  * This should really return information about whether
139  * we should do bottom half handling etc. Right now we
140  * end up _always_ checking the bottom half, which is a
141  * waste of time and is not what some drivers would
142  * prefer.
143  */
144 int handle_IRQ_event(unsigned int irq, struct pt_regs * regs, struct irqaction * action)
145 {
146         int status = 1; /* Force the "do bottom halves" bit */
147         int retval = 0;
148
149         if (!(action->flags & SA_INTERRUPT))
150                 local_irq_enable();
151
152         do {
153                 status |= action->flags;
154                 retval |= action->handler(irq, action->dev_id, regs);
155                 action = action->next;
156         } while (action);
157         if (status & SA_SAMPLE_RANDOM)
158                 add_interrupt_randomness(irq);
159         local_irq_disable();
160
161         return retval;
162 }
163
164 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
165 {
166         struct irqaction *action;
167
168         if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
169                 printk(KERN_ERR "irq event %d: bogus return value %x\n",
170                                 irq, action_ret);
171         } else {
172                 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
173         }
174         dump_stack();
175         printk(KERN_ERR "handlers:\n");
176         action = desc->action;
177         do {
178                 printk(KERN_ERR "[<%p>]", action->handler);
179                 print_symbol(" (%s)",
180                         (unsigned long)action->handler);
181                 printk("\n");
182                 action = action->next;
183         } while (action);
184 }
185
186 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
187 {
188         static int count = 100;
189
190         if (count) {
191                 count--;
192                 __report_bad_irq(irq, desc, action_ret);
193         }
194 }
195
196 static int noirqdebug;
197
198 static int __init noirqdebug_setup(char *str)
199 {
200         noirqdebug = 1;
201         printk("IRQ lockup detection disabled\n");
202         return 1;
203 }
204
205 __setup("noirqdebug", noirqdebug_setup);
206
207 /*
208  * If 99,900 of the previous 100,000 interrupts have not been handled then
209  * assume that the IRQ is stuck in some manner.  Drop a diagnostic and try to
210  * turn the IRQ off.
211  *
212  * (The other 100-of-100,000 interrupts may have been a correctly-functioning
213  *  device sharing an IRQ with the failing one)
214  *
215  * Called under desc->lock
216  */
217 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
218 {
219         if (action_ret != IRQ_HANDLED) {
220                 desc->irqs_unhandled++;
221                 if (action_ret != IRQ_NONE)
222                         report_bad_irq(irq, desc, action_ret);
223         }
224
225         desc->irq_count++;
226         if (desc->irq_count < 100000)
227                 return;
228
229         desc->irq_count = 0;
230         if (desc->irqs_unhandled > 99900) {
231                 /*
232                  * The interrupt is stuck
233                  */
234                 __report_bad_irq(irq, desc, action_ret);
235                 /*
236                  * Now kill the IRQ
237                  */
238                 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
239                 desc->status |= IRQ_DISABLED;
240                 desc->handler->disable(irq);
241         }
242         desc->irqs_unhandled = 0;
243 }
244
245 /*
246  * Generic enable/disable code: this just calls
247  * down into the PIC-specific version for the actual
248  * hardware disable after having gotten the irq
249  * controller lock.
250  */
251
252 /**
253  *      disable_irq_nosync - disable an irq without waiting
254  *      @irq: Interrupt to disable
255  *
256  *      Disable the selected interrupt line. Disables of an interrupt
257  *      stack. Unlike disable_irq(), this function does not ensure existing
258  *      instances of the IRQ handler have completed before returning.
259  *
260  *      This function may be called from IRQ context.
261  */
262
263 void inline disable_irq_nosync(unsigned int irq)
264 {
265         irq_desc_t *desc = irq_desc + irq;
266         unsigned long flags;
267
268         spin_lock_irqsave(&desc->lock, flags);
269         if (!desc->depth++) {
270                 desc->status |= IRQ_DISABLED;
271                 desc->handler->disable(irq);
272         }
273         spin_unlock_irqrestore(&desc->lock, flags);
274 }
275
276 /**
277  *      disable_irq - disable an irq and wait for completion
278  *      @irq: Interrupt to disable
279  *
280  *      Disable the selected interrupt line. Disables of an interrupt
281  *      stack. That is for two disables you need two enables. This
282  *      function waits for any pending IRQ handlers for this interrupt
283  *      to complete before returning. If you use this function while
284  *      holding a resource the IRQ handler may need you will deadlock.
285  *
286  *      This function may be called - with care - from IRQ context.
287  */
288
289 void disable_irq(unsigned int irq)
290 {
291         irq_desc_t *desc = irq_desc + irq;
292         disable_irq_nosync(irq);
293         if (desc->action)
294                 synchronize_irq(irq);
295 }
296
297 /**
298  *      enable_irq - enable interrupt handling on an irq
299  *      @irq: Interrupt to enable
300  *
301  *      Re-enables the processing of interrupts on this IRQ line
302  *      providing no disable_irq calls are now in effect.
303  *
304  *      This function may be called from IRQ context.
305  */
306
307 void enable_irq(unsigned int irq)
308 {
309         irq_desc_t *desc = irq_desc + irq;
310         unsigned long flags;
311
312         spin_lock_irqsave(&desc->lock, flags);
313         switch (desc->depth) {
314         case 1: {
315                 unsigned int status = desc->status & ~(IRQ_DISABLED | IRQ_INPROGRESS);
316                 desc->status = status;
317                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
318                         desc->status = status | IRQ_REPLAY;
319                         hw_resend_irq(desc->handler,irq);
320                 }
321                 desc->handler->enable(irq);
322                 /* fall-through */
323         }
324         default:
325                 desc->depth--;
326                 break;
327         case 0:
328                 printk("enable_irq(%u) unbalanced from %p\n", irq,
329                        __builtin_return_address(0));
330         }
331         spin_unlock_irqrestore(&desc->lock, flags);
332 }
333
334 /*
335  * do_IRQ handles all normal device IRQ's (the special
336  * SMP cross-CPU interrupts have their own specific
337  * handlers).
338  */
339 asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs)
340 {
341         /*
342          * We ack quickly, we don't want the irq controller
343          * thinking we're snobs just because some other CPU has
344          * disabled global interrupts (we have already done the
345          * INT_ACK cycles, it's too late to try to pretend to the
346          * controller that we aren't taking the interrupt).
347          *
348          * 0 return value means that this irq is already being
349          * handled by some other CPU. (or is disabled)
350          */
351         irq_desc_t *desc = irq_desc + irq;
352         struct irqaction * action;
353         unsigned int status;
354
355         irq_enter();
356         kstat_this_cpu.irqs[irq]++;
357         spin_lock(&desc->lock);
358         desc->handler->ack(irq);
359         /*
360            REPLAY is when Linux resends an IRQ that was dropped earlier
361            WAITING is used by probe to mark irqs that are being tested
362            */
363         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
364         status |= IRQ_PENDING; /* we _want_ to handle it */
365
366         /*
367          * If the IRQ is disabled for whatever reason, we cannot
368          * use the action we have.
369          */
370         action = NULL;
371         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
372                 action = desc->action;
373                 status &= ~IRQ_PENDING; /* we commit to handling */
374                 status |= IRQ_INPROGRESS; /* we are handling it */
375         }
376         desc->status = status;
377
378         /*
379          * If there is no IRQ handler or it was disabled, exit early.
380            Since we set PENDING, if another processor is handling
381            a different instance of this same irq, the other processor
382            will take care of it.
383          */
384         if (unlikely(!action))
385                 goto out;
386
387         /*
388          * Edge triggered interrupts need to remember
389          * pending events.
390          * This applies to any hw interrupts that allow a second
391          * instance of the same irq to arrive while we are in do_IRQ
392          * or in the handler. But the code here only handles the _second_
393          * instance of the irq, not the third or fourth. So it is mostly
394          * useful for irq hardware that does not mask cleanly in an
395          * SMP environment.
396          */
397         for (;;) {
398                 irqreturn_t action_ret;
399
400                 spin_unlock(&desc->lock);
401                 action_ret = handle_IRQ_event(irq, regs, action);
402                 spin_lock(&desc->lock);
403                 if (!noirqdebug)
404                         note_interrupt(irq, desc, action_ret);
405                 if (likely(!(desc->status & IRQ_PENDING)))
406                         break;
407                 desc->status &= ~IRQ_PENDING;
408         }
409         desc->status &= ~IRQ_INPROGRESS;
410
411 out:
412         /*
413          * The ->end() handler has to deal with interrupts which got
414          * disabled while the handler was running.
415          */
416         desc->handler->end(irq);
417         spin_unlock(&desc->lock);
418
419         irq_exit();
420
421         return 1;
422 }
423
424 /**
425  *      request_irq - allocate an interrupt line
426  *      @irq: Interrupt line to allocate
427  *      @handler: Function to be called when the IRQ occurs
428  *      @irqflags: Interrupt type flags
429  *      @devname: An ascii name for the claiming device
430  *      @dev_id: A cookie passed back to the handler function
431  *
432  *      This call allocates interrupt resources and enables the
433  *      interrupt line and IRQ handling. From the point this
434  *      call is made your handler function may be invoked. Since
435  *      your handler function must clear any interrupt the board
436  *      raises, you must take care both to initialise your hardware
437  *      and to set up the interrupt handler in the right order.
438  *
439  *      Dev_id must be globally unique. Normally the address of the
440  *      device data structure is used as the cookie. Since the handler
441  *      receives this value it makes sense to use it.
442  *
443  *      If your interrupt is shared you must pass a non NULL dev_id
444  *      as this is required when freeing the interrupt.
445  *
446  *      Flags:
447  *
448  *      SA_SHIRQ                Interrupt is shared
449  *
450  *      SA_INTERRUPT            Disable local interrupts while processing
451  *
452  *      SA_SAMPLE_RANDOM        The interrupt can be used for entropy
453  *
454  */
455
456 int request_irq(unsigned int irq,
457                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
458                 unsigned long irqflags,
459                 const char * devname,
460                 void *dev_id)
461 {
462         int retval;
463         struct irqaction * action;
464
465 #if 1
466         /*
467          * Sanity-check: shared interrupts should REALLY pass in
468          * a real dev-ID, otherwise we'll have trouble later trying
469          * to figure out which interrupt is which (messes up the
470          * interrupt freeing logic etc).
471          */
472         if (irqflags & SA_SHIRQ) {
473                 if (!dev_id)
474                         printk("Bad boy: %s (at 0x%x) called us without a dev_id!\n", devname, (&irq)[-1]);
475         }
476 #endif
477
478         if (irq >= NR_IRQS)
479                 return -EINVAL;
480         if (!handler)
481                 return -EINVAL;
482
483         action = (struct irqaction *)
484                         kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
485         if (!action)
486                 return -ENOMEM;
487
488         action->handler = handler;
489         action->flags = irqflags;
490         action->mask = 0;
491         action->name = devname;
492         action->next = NULL;
493         action->dev_id = dev_id;
494
495         retval = setup_irq(irq, action);
496         if (retval)
497                 kfree(action);
498         return retval;
499 }
500
501 EXPORT_SYMBOL(request_irq);
502
503 /**
504  *      free_irq - free an interrupt
505  *      @irq: Interrupt line to free
506  *      @dev_id: Device identity to free
507  *
508  *      Remove an interrupt handler. The handler is removed and if the
509  *      interrupt line is no longer in use by any driver it is disabled.
510  *      On a shared IRQ the caller must ensure the interrupt is disabled
511  *      on the card it drives before calling this function. The function
512  *      does not return until any executing interrupts for this IRQ
513  *      have completed.
514  *
515  *      This function must not be called from interrupt context.
516  */
517
518 void free_irq(unsigned int irq, void *dev_id)
519 {
520         irq_desc_t *desc;
521         struct irqaction **p;
522         unsigned long flags;
523
524         if (irq >= NR_IRQS)
525                 return;
526
527         desc = irq_desc + irq;
528         spin_lock_irqsave(&desc->lock,flags);
529         p = &desc->action;
530         for (;;) {
531                 struct irqaction * action = *p;
532                 if (action) {
533                         struct irqaction **pp = p;
534                         p = &action->next;
535                         if (action->dev_id != dev_id)
536                                 continue;
537
538                         /* Found it - now remove it from the list of entries */
539                         *pp = action->next;
540                         if (!desc->action) {
541                                 desc->status |= IRQ_DISABLED;
542                                 desc->handler->shutdown(irq);
543                         }
544                         spin_unlock_irqrestore(&desc->lock,flags);
545
546                         /* Wait to make sure it's not being used on another CPU */
547                         synchronize_irq(irq);
548                         kfree(action);
549                         return;
550                 }
551                 printk("Trying to free free IRQ%d\n",irq);
552                 spin_unlock_irqrestore(&desc->lock,flags);
553                 return;
554         }
555 }
556
557 EXPORT_SYMBOL(free_irq);
558
559 /*
560  * IRQ autodetection code..
561  *
562  * This depends on the fact that any interrupt that
563  * comes in on to an unassigned handler will get stuck
564  * with "IRQ_WAITING" cleared and the interrupt
565  * disabled.
566  */
567
568 static DECLARE_MUTEX(probe_sem);
569
570 /**
571  *      probe_irq_on    - begin an interrupt autodetect
572  *
573  *      Commence probing for an interrupt. The interrupts are scanned
574  *      and a mask of potential interrupt lines is returned.
575  *
576  */
577
578 unsigned long probe_irq_on(void)
579 {
580         unsigned int i;
581         irq_desc_t *desc;
582         unsigned long val;
583         unsigned long delay;
584
585         down(&probe_sem);
586         /*
587          * something may have generated an irq long ago and we want to
588          * flush such a longstanding irq before considering it as spurious.
589          */
590         for (i = NR_IRQS-1; i > 0; i--)  {
591                 desc = irq_desc + i;
592
593                 spin_lock_irq(&desc->lock);
594                 if (!irq_desc[i].action)
595                         irq_desc[i].handler->startup(i);
596                 spin_unlock_irq(&desc->lock);
597         }
598
599         /* Wait for longstanding interrupts to trigger. */
600         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
601                 /* about 20ms delay */ barrier();
602
603         /*
604          * enable any unassigned irqs
605          * (we must startup again here because if a longstanding irq
606          * happened in the previous stage, it may have masked itself)
607          */
608         for (i = NR_IRQS-1; i > 0; i--) {
609                 desc = irq_desc + i;
610
611                 spin_lock_irq(&desc->lock);
612                 if (!desc->action) {
613                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
614                         if (desc->handler->startup(i))
615                                 desc->status |= IRQ_PENDING;
616                 }
617                 spin_unlock_irq(&desc->lock);
618         }
619
620         /*
621          * Wait for spurious interrupts to trigger
622          */
623         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
624                 /* about 100ms delay */ barrier();
625
626         /*
627          * Now filter out any obviously spurious interrupts
628          */
629         val = 0;
630         for (i = 0; i < NR_IRQS; i++) {
631                 irq_desc_t *desc = irq_desc + i;
632                 unsigned int status;
633
634                 spin_lock_irq(&desc->lock);
635                 status = desc->status;
636
637                 if (status & IRQ_AUTODETECT) {
638                         /* It triggered already - consider it spurious. */
639                         if (!(status & IRQ_WAITING)) {
640                                 desc->status = status & ~IRQ_AUTODETECT;
641                                 desc->handler->shutdown(i);
642                         } else
643                                 if (i < 32)
644                                         val |= 1 << i;
645                 }
646                 spin_unlock_irq(&desc->lock);
647         }
648
649         return val;
650 }
651
652 EXPORT_SYMBOL(probe_irq_on);
653
654 /*
655  * Return a mask of triggered interrupts (this
656  * can handle only legacy ISA interrupts).
657  */
658
659 /**
660  *      probe_irq_mask - scan a bitmap of interrupt lines
661  *      @val:   mask of interrupts to consider
662  *
663  *      Scan the ISA bus interrupt lines and return a bitmap of
664  *      active interrupts. The interrupt probe logic state is then
665  *      returned to its previous value.
666  *
667  *      Note: we need to scan all the irq's even though we will
668  *      only return ISA irq numbers - just so that we reset them
669  *      all to a known state.
670  */
671 unsigned int probe_irq_mask(unsigned long val)
672 {
673         int i;
674         unsigned int mask;
675
676         mask = 0;
677         for (i = 0; i < NR_IRQS; i++) {
678                 irq_desc_t *desc = irq_desc + i;
679                 unsigned int status;
680
681                 spin_lock_irq(&desc->lock);
682                 status = desc->status;
683
684                 if (status & IRQ_AUTODETECT) {
685                         if (i < 16 && !(status & IRQ_WAITING))
686                                 mask |= 1 << i;
687
688                         desc->status = status & ~IRQ_AUTODETECT;
689                         desc->handler->shutdown(i);
690                 }
691                 spin_unlock_irq(&desc->lock);
692         }
693         up(&probe_sem);
694
695         return mask & val;
696 }
697
698 /*
699  * Return the one interrupt that triggered (this can
700  * handle any interrupt source).
701  */
702
703 /**
704  *      probe_irq_off   - end an interrupt autodetect
705  *      @val: mask of potential interrupts (unused)
706  *
707  *      Scans the unused interrupt lines and returns the line which
708  *      appears to have triggered the interrupt. If no interrupt was
709  *      found then zero is returned. If more than one interrupt is
710  *      found then minus the first candidate is returned to indicate
711  *      there is doubt.
712  *
713  *      The interrupt probe logic state is returned to its previous
714  *      value.
715  *
716  *      BUGS: When used in a module (which arguably shouldnt happen)
717  *      nothing prevents two IRQ probe callers from overlapping. The
718  *      results of this are non-optimal.
719  */
720
721 int probe_irq_off(unsigned long val)
722 {
723         int i, irq_found, nr_irqs;
724
725         nr_irqs = 0;
726         irq_found = 0;
727         for (i = 0; i < NR_IRQS; i++) {
728                 irq_desc_t *desc = irq_desc + i;
729                 unsigned int status;
730
731                 spin_lock_irq(&desc->lock);
732                 status = desc->status;
733
734                 if (status & IRQ_AUTODETECT) {
735                         if (!(status & IRQ_WAITING)) {
736                                 if (!nr_irqs)
737                                         irq_found = i;
738                                 nr_irqs++;
739                         }
740                         desc->status = status & ~IRQ_AUTODETECT;
741                         desc->handler->shutdown(i);
742                 }
743                 spin_unlock_irq(&desc->lock);
744         }
745         up(&probe_sem);
746
747         if (nr_irqs > 1)
748                 irq_found = -irq_found;
749         return irq_found;
750 }
751
752 EXPORT_SYMBOL(probe_irq_off);
753
754 /* this was setup_x86_irq but it seems pretty generic */
755 int setup_irq(unsigned int irq, struct irqaction * new)
756 {
757         int shared = 0;
758         unsigned long flags;
759         struct irqaction *old, **p;
760         irq_desc_t *desc = irq_desc + irq;
761
762         /*
763          * Some drivers like serial.c use request_irq() heavily,
764          * so we have to be careful not to interfere with a
765          * running system.
766          */
767         if (new->flags & SA_SAMPLE_RANDOM) {
768                 /*
769                  * This function might sleep, we want to call it first,
770                  * outside of the atomic block.
771                  * Yes, this might clear the entropy pool if the wrong
772                  * driver is attempted to be loaded, without actually
773                  * installing a new handler, but is this really a problem,
774                  * only the sysadmin is able to do this.
775                  */
776                 rand_initialize_irq(irq);
777         }
778
779         /*
780          * The following block of code has to be executed atomically
781          */
782         spin_lock_irqsave(&desc->lock,flags);
783         p = &desc->action;
784         if ((old = *p) != NULL) {
785                 /* Can't share interrupts unless both agree to */
786                 if (!(old->flags & new->flags & SA_SHIRQ)) {
787                         spin_unlock_irqrestore(&desc->lock,flags);
788                         return -EBUSY;
789                 }
790
791                 /* add new interrupt at end of irq queue */
792                 do {
793                         p = &old->next;
794                         old = *p;
795                 } while (old);
796                 shared = 1;
797         }
798
799         *p = new;
800
801         if (!shared) {
802                 desc->depth = 0;
803                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
804                 desc->handler->startup(irq);
805         }
806         spin_unlock_irqrestore(&desc->lock,flags);
807
808         register_irq_proc(irq);
809         return 0;
810 }
811
812 void __init init_generic_irq(void)
813 {
814         int i;
815
816         for (i = 0; i < NR_IRQS; i++) {
817                 irq_desc[i].status  = IRQ_DISABLED;
818                 irq_desc[i].action  = NULL;
819                 irq_desc[i].depth   = 1;
820                 irq_desc[i].handler = &no_irq_type;
821         }
822 }
823
824 EXPORT_SYMBOL(disable_irq_nosync);
825 EXPORT_SYMBOL(disable_irq);
826 EXPORT_SYMBOL(enable_irq);
827 EXPORT_SYMBOL(probe_irq_mask);
828
829 static struct proc_dir_entry * root_irq_dir;
830 static struct proc_dir_entry * irq_dir [NR_IRQS];
831
832 #ifdef CONFIG_SMP
833
834 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
835
836 static cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
837 static int irq_affinity_read_proc (char *page, char **start, off_t off,
838                         int count, int *eof, void *data)
839 {
840         int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
841         if (count - len < 2)
842                 return -EINVAL;
843         len += sprintf(page + len, "\n");
844         return len;
845 }
846
847 static int irq_affinity_write_proc (struct file *file, const char *buffer,
848                                         unsigned long count, void *data)
849 {
850         int irq = (long) data, full_count = count, err;
851         cpumask_t new_value, tmp;
852
853         if (!irq_desc[irq].handler->set_affinity)
854                 return -EIO;
855
856         err = cpumask_parse(buffer, count, new_value);
857
858         /*
859          * Do not allow disabling IRQs completely - it's a too easy
860          * way to make the system unusable accidentally :-) At least
861          * one online CPU still has to be targeted.
862          */
863         cpus_and(tmp, new_value, cpu_online_map);
864         if (cpus_empty(tmp))
865                 return -EINVAL;
866
867         irq_affinity[irq] = new_value;
868         irq_desc[irq].handler->set_affinity(irq, new_value);
869
870         return full_count;
871 }
872
873 #endif
874
875 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
876                         int count, int *eof, void *data)
877 {
878         int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
879         if (count - len < 2)
880                 return -EINVAL;
881         len += sprintf(page + len, "\n");
882         return len;
883 }
884
885 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
886                                         unsigned long count, void *data)
887 {
888         cpumask_t *mask = (cpumask_t *)data, new_value;
889         unsigned long full_count = count, err;
890
891         err = cpumask_parse(buffer, count, new_value);
892         if (err)
893                 return err;
894
895         *mask = new_value;
896         return full_count;
897 }
898
899 #define MAX_NAMELEN 10
900
901 static void register_irq_proc (unsigned int irq)
902 {
903         char name [MAX_NAMELEN];
904
905         if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type) ||
906                         irq_dir[irq])
907                 return;
908
909         memset(name, 0, MAX_NAMELEN);
910         sprintf(name, "%d", irq);
911
912         /* create /proc/irq/1234 */
913         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
914
915 #ifdef CONFIG_SMP
916         {
917                 struct proc_dir_entry *entry;
918
919                 /* create /proc/irq/1234/smp_affinity */
920                 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
921
922                 if (entry) {
923                         entry->nlink = 1;
924                         entry->data = (void *)(long)irq;
925                         entry->read_proc = irq_affinity_read_proc;
926                         entry->write_proc = irq_affinity_write_proc;
927                 }
928
929                 smp_affinity_entry[irq] = entry;
930         }
931 #endif
932 }
933
934 unsigned long prof_cpu_mask = -1;
935
936 void init_irq_proc (void)
937 {
938         struct proc_dir_entry *entry;
939         int i;
940
941         /* create /proc/irq */
942         root_irq_dir = proc_mkdir("irq", 0);
943
944         /* create /proc/irq/prof_cpu_mask */
945         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
946
947         if (!entry)
948             return;
949
950         entry->nlink = 1;
951         entry->data = (void *)&prof_cpu_mask;
952         entry->read_proc = prof_cpu_mask_read_proc;
953         entry->write_proc = prof_cpu_mask_write_proc;
954
955         /*
956          * Create entries for all existing IRQs.
957          */
958         for (i = 0; i < NR_IRQS; i++)
959                 register_irq_proc(i);
960 }