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