v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / arch / alpha / kernel / irq.c
1 /*
2  *      linux/arch/alpha/kernel/irq.c
3  *
4  *      Copyright (C) 1995 Linus Torvalds
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 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/ptrace.h>
16 #include <linux/errno.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
22 #include <linux/random.h>
23 #include <linux/init.h>
24 #include <linux/irq.h>
25 #include <linux/proc_fs.h>
26
27 #include <asm/system.h>
28 #include <asm/io.h>
29 #include <asm/bitops.h>
30 #include <asm/uaccess.h>
31
32 /*
33  * Controller mappings for all interrupt sources:
34  */
35 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
36         [0 ... NR_IRQS-1] = { 0, &no_irq_type, NULL, 0, SPIN_LOCK_UNLOCKED}
37 };
38
39 static void register_irq_proc(unsigned int irq);
40
41 unsigned long irq_err_count;
42
43 /*
44  * Special irq handlers.
45  */
46
47 void no_action(int cpl, void *dev_id, struct pt_regs *regs) { }
48
49 /*
50  * Generic no controller code
51  */
52
53 static void no_irq_enable_disable(unsigned int irq) { }
54 static unsigned int no_irq_startup(unsigned int irq) { return 0; }
55
56 static void
57 no_irq_ack(unsigned int irq)
58 {
59         irq_err_count++;
60         printk(KERN_CRIT "Unexpected IRQ trap at vector %u\n", irq);
61 }
62
63 struct hw_interrupt_type no_irq_type = {
64         typename:       "none",
65         startup:        no_irq_startup,
66         shutdown:       no_irq_enable_disable,
67         enable:         no_irq_enable_disable,
68         disable:        no_irq_enable_disable,
69         ack:            no_irq_ack,
70         end:            no_irq_enable_disable,
71 };
72
73 int
74 handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
75                  struct irqaction *action)
76 {
77         int status;
78         int cpu = smp_processor_id();
79
80         kstat.irqs[cpu][irq]++;
81         irq_enter(cpu, irq);
82
83         status = 1;     /* Force the "do bottom halves" bit */
84
85         do {
86                 if (!(action->flags & SA_INTERRUPT))
87                         __sti();
88                 else
89                         __cli();
90
91                 status |= action->flags;
92                 action->handler(irq, action->dev_id, regs);
93                 action = action->next;
94         } while (action);
95         if (status & SA_SAMPLE_RANDOM)
96                 add_interrupt_randomness(irq);
97         __cli();
98
99         irq_exit(cpu, irq);
100
101         return status;
102 }
103
104 /*
105  * Generic enable/disable code: this just calls
106  * down into the PIC-specific version for the actual
107  * hardware disable after having gotten the irq
108  * controller lock. 
109  */
110 void inline
111 disable_irq_nosync(unsigned int irq)
112 {
113         irq_desc_t *desc = irq_desc + irq;
114         unsigned long flags;
115
116         spin_lock_irqsave(&desc->lock, flags);
117         if (!desc->depth++) {
118                 desc->status |= IRQ_DISABLED;
119                 desc->handler->disable(irq);
120         }
121         spin_unlock_irqrestore(&desc->lock, flags);
122 }
123
124 /*
125  * Synchronous version of the above, making sure the IRQ is
126  * no longer running on any other IRQ..
127  */
128 void
129 disable_irq(unsigned int irq)
130 {
131         disable_irq_nosync(irq);
132
133         if (!local_irq_count(smp_processor_id())) {
134                 do {
135                         barrier();
136                 } while (irq_desc[irq].status & IRQ_INPROGRESS);
137         }
138 }
139
140 void
141 enable_irq(unsigned int irq)
142 {
143         irq_desc_t *desc = irq_desc + irq;
144         unsigned long flags;
145
146         spin_lock_irqsave(&desc->lock, flags);
147         switch (desc->depth) {
148         case 1: {
149                 unsigned int status = desc->status & ~IRQ_DISABLED;
150                 desc->status = status;
151                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
152                         desc->status = status | IRQ_REPLAY;
153                         hw_resend_irq(desc->handler,irq);
154                 }
155                 desc->handler->enable(irq);
156                 /* fall-through */
157         }
158         default:
159                 desc->depth--;
160                 break;
161         case 0:
162                 printk(KERN_ERR "enable_irq() unbalanced from %p\n",
163                        __builtin_return_address(0));
164         }
165         spin_unlock_irqrestore(&desc->lock, flags);
166 }
167
168 int
169 setup_irq(unsigned int irq, struct irqaction * new)
170 {
171         int shared = 0;
172         struct irqaction *old, **p;
173         unsigned long flags;
174         irq_desc_t *desc = irq_desc + irq;
175
176         /*
177          * Some drivers like serial.c use request_irq() heavily,
178          * so we have to be careful not to interfere with a
179          * running system.
180          */
181         if (new->flags & SA_SAMPLE_RANDOM) {
182                 /*
183                  * This function might sleep, we want to call it first,
184                  * outside of the atomic block.
185                  * Yes, this might clear the entropy pool if the wrong
186                  * driver is attempted to be loaded, without actually
187                  * installing a new handler, but is this really a problem,
188                  * only the sysadmin is able to do this.
189                  */
190                 rand_initialize_irq(irq);
191         }
192
193         /*
194          * The following block of code has to be executed atomically
195          */
196         spin_lock_irqsave(&desc->lock,flags);
197         p = &desc->action;
198         if ((old = *p) != NULL) {
199                 /* Can't share interrupts unless both agree to */
200                 if (!(old->flags & new->flags & SA_SHIRQ)) {
201                         spin_unlock_irqrestore(&desc->lock,flags);
202                         return -EBUSY;
203                 }
204
205                 /* add new interrupt at end of irq queue */
206                 do {
207                         p = &old->next;
208                         old = *p;
209                 } while (old);
210                 shared = 1;
211         }
212
213         *p = new;
214
215         if (!shared) {
216                 desc->depth = 0;
217                 desc->status &= ~IRQ_DISABLED;
218                 desc->handler->startup(irq);
219         }
220         spin_unlock_irqrestore(&desc->lock,flags);
221
222         return 0;
223 }
224
225 static struct proc_dir_entry * root_irq_dir;
226 static struct proc_dir_entry * irq_dir[NR_IRQS];
227
228 #ifdef CONFIG_SMP
229 static struct proc_dir_entry * smp_affinity_entry[NR_IRQS];
230 static char irq_user_affinity[NR_IRQS];
231 static unsigned long irq_affinity[NR_IRQS] = { [0 ... NR_IRQS-1] = ~0UL };
232
233 static void
234 select_smp_affinity(int irq)
235 {
236         static int last_cpu;
237         int cpu = last_cpu + 1;
238
239         if (! irq_desc[irq].handler->set_affinity || irq_user_affinity[irq])
240                 return;
241
242         while (((cpu_present_mask >> cpu) & 1) == 0)
243                 cpu = (cpu < (NR_CPUS-1) ? cpu + 1 : 0);
244         last_cpu = cpu;
245
246         irq_affinity[irq] = 1UL << cpu;
247         irq_desc[irq].handler->set_affinity(irq, 1UL << cpu);
248 }
249
250 #define HEX_DIGITS 16
251
252 static int
253 irq_affinity_read_proc (char *page, char **start, off_t off,
254                         int count, int *eof, void *data)
255 {
256         if (count < HEX_DIGITS+1)
257                 return -EINVAL;
258         return sprintf (page, "%016lx\n", irq_affinity[(long)data]);
259 }
260
261 static unsigned int
262 parse_hex_value (const char *buffer,
263                  unsigned long count, unsigned long *ret)
264 {
265         unsigned char hexnum [HEX_DIGITS];
266         unsigned long value;
267         int i;
268
269         if (!count)
270                 return -EINVAL;
271         if (count > HEX_DIGITS)
272                 count = HEX_DIGITS;
273         if (copy_from_user(hexnum, buffer, count))
274                 return -EFAULT;
275
276         /*
277          * Parse the first 8 characters as a hex string, any non-hex char
278          * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
279          */
280         value = 0;
281
282         for (i = 0; i < count; i++) {
283                 unsigned int c = hexnum[i];
284
285                 switch (c) {
286                         case '0' ... '9': c -= '0'; break;
287                         case 'a' ... 'f': c -= 'a'-10; break;
288                         case 'A' ... 'F': c -= 'A'-10; break;
289                 default:
290                         goto out;
291                 }
292                 value = (value << 4) | c;
293         }
294 out:
295         *ret = value;
296         return 0;
297 }
298
299 static int
300 irq_affinity_write_proc(struct file *file, const char *buffer,
301                         unsigned long count, void *data)
302 {
303         int irq = (long) data, full_count = count, err;
304         unsigned long new_value;
305
306         if (!irq_desc[irq].handler->set_affinity)
307                 return -EIO;
308
309         err = parse_hex_value(buffer, count, &new_value);
310
311         /* The special value 0 means release control of the
312            affinity to kernel.  */
313         if (new_value == 0) {
314                 irq_user_affinity[irq] = 0;
315                 select_smp_affinity(irq);
316         }
317         /* Do not allow disabling IRQs completely - it's a too easy
318            way to make the system unusable accidentally :-) At least
319            one online CPU still has to be targeted.  */
320         else if (!(new_value & cpu_present_mask))
321                 return -EINVAL;
322         else {
323                 irq_affinity[irq] = new_value;
324                 irq_user_affinity[irq] = 1;
325                 irq_desc[irq].handler->set_affinity(irq, new_value);
326         }
327
328         return full_count;
329 }
330
331 static int
332 prof_cpu_mask_read_proc(char *page, char **start, off_t off,
333                         int count, int *eof, void *data)
334 {
335         unsigned long *mask = (unsigned long *) data;
336         if (count < HEX_DIGITS+1)
337                 return -EINVAL;
338         return sprintf (page, "%016lx\n", *mask);
339 }
340
341 static int
342 prof_cpu_mask_write_proc(struct file *file, const char *buffer,
343                          unsigned long count, void *data)
344 {
345         unsigned long *mask = (unsigned long *) data, full_count = count, err;
346         unsigned long new_value;
347
348         err = parse_hex_value(buffer, count, &new_value);
349         if (err)
350                 return err;
351
352         *mask = new_value;
353         return full_count;
354 }
355 #endif /* CONFIG_SMP */
356
357 #define MAX_NAMELEN 10
358
359 static void
360 register_irq_proc (unsigned int irq)
361 {
362 #ifdef CONFIG_SMP
363         struct proc_dir_entry *entry;
364 #endif
365         char name [MAX_NAMELEN];
366
367         if (!root_irq_dir || (irq_desc[irq].handler == &no_irq_type))
368                 return;
369
370         memset(name, 0, MAX_NAMELEN);
371         sprintf(name, "%d", irq);
372
373         /* create /proc/irq/1234 */
374         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
375
376 #ifdef CONFIG_SMP
377         /* create /proc/irq/1234/smp_affinity */
378         entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
379
380         entry->nlink = 1;
381         entry->data = (void *)(long)irq;
382         entry->read_proc = irq_affinity_read_proc;
383         entry->write_proc = irq_affinity_write_proc;
384
385         smp_affinity_entry[irq] = entry;
386 #endif
387 }
388
389 unsigned long prof_cpu_mask = ~0UL;
390
391 void
392 init_irq_proc (void)
393 {
394 #ifdef CONFIG_SMP
395         struct proc_dir_entry *entry;
396 #endif
397         int i;
398
399         /* create /proc/irq */
400         root_irq_dir = proc_mkdir("irq", 0);
401
402 #ifdef CONFIG_SMP
403         /* create /proc/irq/prof_cpu_mask */
404         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
405
406         entry->nlink = 1;
407         entry->data = (void *)&prof_cpu_mask;
408         entry->read_proc = prof_cpu_mask_read_proc;
409         entry->write_proc = prof_cpu_mask_write_proc;
410 #endif
411
412         /*
413          * Create entries for all existing IRQs.
414          */
415         for (i = 0; i < NR_IRQS; i++) {
416                 if (irq_desc[i].handler == &no_irq_type)
417                         continue;
418                 register_irq_proc(i);
419         }
420 }
421
422 int
423 request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *),
424             unsigned long irqflags, const char * devname, void *dev_id)
425 {
426         int retval;
427         struct irqaction * action;
428
429         if (irq >= ACTUAL_NR_IRQS)
430                 return -EINVAL;
431         if (!handler)
432                 return -EINVAL;
433
434 #if 1
435         /*
436          * Sanity-check: shared interrupts should REALLY pass in
437          * a real dev-ID, otherwise we'll have trouble later trying
438          * to figure out which interrupt is which (messes up the
439          * interrupt freeing logic etc).
440          */
441         if ((irqflags & SA_SHIRQ) && !dev_id) {
442                 printk(KERN_ERR
443                        "Bad boy: %s (at %p) called us without a dev_id!\n",
444                        devname, __builtin_return_address(0));
445         }
446 #endif
447
448         action = (struct irqaction *)
449                         kmalloc(sizeof(struct irqaction), GFP_KERNEL);
450         if (!action)
451                 return -ENOMEM;
452
453         action->handler = handler;
454         action->flags = irqflags;
455         action->mask = 0;
456         action->name = devname;
457         action->next = NULL;
458         action->dev_id = dev_id;
459
460 #ifdef CONFIG_SMP
461         select_smp_affinity(irq);
462 #endif
463
464         retval = setup_irq(irq, action);
465         if (retval)
466                 kfree(action);
467         return retval;
468 }
469
470 void
471 free_irq(unsigned int irq, void *dev_id)
472 {
473         irq_desc_t *desc;
474         struct irqaction **p;
475         unsigned long flags;
476
477         if (irq >= ACTUAL_NR_IRQS) {
478                 printk(KERN_CRIT "Trying to free IRQ%d\n", irq);
479                 return;
480         }
481
482         desc = irq_desc + irq;
483         spin_lock_irqsave(&desc->lock,flags);
484         p = &desc->action;
485         for (;;) {
486                 struct irqaction * action = *p;
487                 if (action) {
488                         struct irqaction **pp = p;
489                         p = &action->next;
490                         if (action->dev_id != dev_id)
491                                 continue;
492
493                         /* Found - now remove it from the list of entries.  */
494                         *pp = action->next;
495                         if (!desc->action) {
496                                 desc->status |= IRQ_DISABLED;
497                                 desc->handler->shutdown(irq);
498                         }
499                         spin_unlock_irqrestore(&desc->lock,flags);
500
501 #ifdef CONFIG_SMP
502                         /* Wait to make sure it's not being used on
503                            another CPU.  */
504                         while (desc->status & IRQ_INPROGRESS)
505                                 barrier();
506 #endif
507                         kfree(action);
508                         return;
509                 }
510                 printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
511                 spin_unlock_irqrestore(&desc->lock,flags);
512                 return;
513         }
514 }
515
516 int
517 get_irq_list(char *buf)
518 {
519 #ifdef CONFIG_SMP
520         int j;
521 #endif
522         int i;
523         struct irqaction * action;
524         char *p = buf;
525
526 #ifdef CONFIG_SMP
527         p += sprintf(p, "           ");
528         for (i = 0; i < smp_num_cpus; i++)
529                 p += sprintf(p, "CPU%d       ", i);
530 #ifdef DO_BROADCAST_INTS
531         for (i = 0; i < smp_num_cpus; i++)
532                 p += sprintf(p, "TRY%d       ", i);
533 #endif
534         *p++ = '\n';
535 #endif
536
537         for (i = 0; i < NR_IRQS; i++) {
538                 action = irq_desc[i].action;
539                 if (!action) 
540                         continue;
541                 p += sprintf(p, "%3d: ",i);
542 #ifndef CONFIG_SMP
543                 p += sprintf(p, "%10u ", kstat_irqs(i));
544 #else
545                 for (j = 0; j < smp_num_cpus; j++)
546                         p += sprintf(p, "%10u ",
547                                      kstat.irqs[cpu_logical_map(j)][i]);
548 #ifdef DO_BROADCAST_INTS
549                 for (j = 0; j < smp_num_cpus; j++)
550                         p += sprintf(p, "%10lu ",
551                                      irq_attempt(cpu_logical_map(j), i));
552 #endif
553 #endif
554                 p += sprintf(p, " %14s", irq_desc[i].handler->typename);
555                 p += sprintf(p, "  %c%s",
556                              (action->flags & SA_INTERRUPT)?'+':' ',
557                              action->name);
558
559                 for (action=action->next; action; action = action->next) {
560                         p += sprintf(p, ", %c%s",
561                                      (action->flags & SA_INTERRUPT)?'+':' ',
562                                      action->name);
563                 }
564                 *p++ = '\n';
565         }
566 #if CONFIG_SMP
567         p += sprintf(p, "IPI: ");
568         for (j = 0; j < smp_num_cpus; j++)
569                 p += sprintf(p, "%10lu ",
570                              cpu_data[cpu_logical_map(j)].ipi_count);
571         p += sprintf(p, "\n");
572 #endif
573         p += sprintf(p, "ERR: %10lu\n", irq_err_count);
574         return p - buf;
575 }
576
577
578 /*
579  * handle_irq handles all normal device IRQ's (the special
580  * SMP cross-CPU interrupts have their own specific
581  * handlers).
582  */
583
584 #define MAX_ILLEGAL_IRQS 16
585
586 void
587 handle_irq(int irq, struct pt_regs * regs)
588 {       
589         /* 
590          * We ack quickly, we don't want the irq controller
591          * thinking we're snobs just because some other CPU has
592          * disabled global interrupts (we have already done the
593          * INT_ACK cycles, it's too late to try to pretend to the
594          * controller that we aren't taking the interrupt).
595          *
596          * 0 return value means that this irq is already being
597          * handled by some other CPU. (or is disabled)
598          */
599         int cpu = smp_processor_id();
600         irq_desc_t *desc = irq_desc + irq;
601         struct irqaction * action;
602         unsigned int status;
603         static unsigned int illegal_count=0;
604         
605         if ((unsigned) irq > ACTUAL_NR_IRQS && illegal_count < MAX_ILLEGAL_IRQS ) {
606                 irq_err_count++;
607                 illegal_count++;
608                 printk(KERN_CRIT "device_interrupt: illegal interrupt %d\n",
609                        irq);
610                 return;
611         }
612
613         irq_attempt(cpu, irq)++;
614         spin_lock_irq(&desc->lock); /* mask also the higher prio events */
615         desc->handler->ack(irq);
616         /*
617          * REPLAY is when Linux resends an IRQ that was dropped earlier.
618          * WAITING is used by probe to mark irqs that are being tested.
619          */
620         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
621         status |= IRQ_PENDING; /* we _want_ to handle it */
622
623         /*
624          * If the IRQ is disabled for whatever reason, we cannot
625          * use the action we have.
626          */
627         action = NULL;
628         if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
629                 action = desc->action;
630                 status &= ~IRQ_PENDING; /* we commit to handling */
631                 status |= IRQ_INPROGRESS; /* we are handling it */
632         }
633         desc->status = status;
634
635         /*
636          * If there is no IRQ handler or it was disabled, exit early.
637          * Since we set PENDING, if another processor is handling
638          * a different instance of this same irq, the other processor
639          * will take care of it.
640          */
641         if (!action)
642                 goto out;
643
644         /*
645          * Edge triggered interrupts need to remember pending events.
646          * This applies to any hw interrupts that allow a second
647          * instance of the same irq to arrive while we are in handle_irq
648          * or in the handler. But the code here only handles the _second_
649          * instance of the irq, not the third or fourth. So it is mostly
650          * useful for irq hardware that does not mask cleanly in an
651          * SMP environment.
652          */
653         for (;;) {
654                 spin_unlock(&desc->lock);
655                 handle_IRQ_event(irq, regs, action);
656                 spin_lock(&desc->lock);
657                 
658                 if (!(desc->status & IRQ_PENDING)
659                     || (desc->status & IRQ_LEVEL))
660                         break;
661                 desc->status &= ~IRQ_PENDING;
662         }
663         desc->status &= ~IRQ_INPROGRESS;
664 out:
665         /*
666          * The ->end() handler has to deal with interrupts which got
667          * disabled while the handler was running.
668          */
669         desc->handler->end(irq);
670         spin_unlock(&desc->lock);
671
672         if (softirq_pending(cpu))
673                 do_softirq();
674 }
675
676 /*
677  * IRQ autodetection code..
678  *
679  * This depends on the fact that any interrupt that
680  * comes in on to an unassigned handler will get stuck
681  * with "IRQ_WAITING" cleared and the interrupt
682  * disabled.
683  */
684 unsigned long
685 probe_irq_on(void)
686 {
687         int i;
688         irq_desc_t *desc;
689         unsigned long delay;
690         unsigned long val;
691
692         /* Something may have generated an irq long ago and we want to
693            flush such a longstanding irq before considering it as spurious. */
694         for (i = NR_IRQS-1; i >= 0; i--) {
695                 desc = irq_desc + i;
696
697                 spin_lock_irq(&desc->lock);
698                 if (!irq_desc[i].action) 
699                         irq_desc[i].handler->startup(i);
700                 spin_unlock_irq(&desc->lock);
701         }
702
703         /* Wait for longstanding interrupts to trigger. */
704         for (delay = jiffies + HZ/50; time_after(delay, jiffies); )
705                 /* about 20ms delay */ synchronize_irq();
706
707         /* enable any unassigned irqs (we must startup again here because
708            if a longstanding irq happened in the previous stage, it may have
709            masked itself) first, enable any unassigned irqs. */
710         for (i = NR_IRQS-1; i >= 0; i--) {
711                 desc = irq_desc + i;
712
713                 spin_lock_irq(&desc->lock);
714                 if (!desc->action) {
715                         desc->status |= IRQ_AUTODETECT | IRQ_WAITING;
716                         if (desc->handler->startup(i))
717                                 desc->status |= IRQ_PENDING;
718                 }
719                 spin_unlock_irq(&desc->lock);
720         }
721
722         /*
723          * Wait for spurious interrupts to trigger
724          */
725         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
726                 /* about 100ms delay */ synchronize_irq();
727
728         /*
729          * Now filter out any obviously spurious interrupts
730          */
731         val = 0;
732         for (i=0; i<NR_IRQS; i++) {
733                 irq_desc_t *desc = irq_desc + i;
734                 unsigned int status;
735
736                 spin_lock_irq(&desc->lock);
737                 status = desc->status;
738
739                 if (status & IRQ_AUTODETECT) {
740                         /* It triggered already - consider it spurious. */
741                         if (!(status & IRQ_WAITING)) {
742                                 desc->status = status & ~IRQ_AUTODETECT;
743                                 desc->handler->shutdown(i);
744                         } else
745                                 if (i < 32)
746                                         val |= 1 << i;
747                 }
748                 spin_unlock_irq(&desc->lock);
749         }
750
751         return val;
752 }
753
754 /*
755  * Return a mask of triggered interrupts (this
756  * can handle only legacy ISA interrupts).
757  */
758 unsigned int
759 probe_irq_mask(unsigned long val)
760 {
761         int i;
762         unsigned int mask;
763
764         mask = 0;
765         for (i = 0; i < NR_IRQS; i++) {
766                 irq_desc_t *desc = irq_desc + i;
767                 unsigned int status;
768
769                 spin_lock_irq(&desc->lock);
770                 status = desc->status;
771
772                 if (status & IRQ_AUTODETECT) {
773                         /* We only react to ISA interrupts */
774                         if (!(status & IRQ_WAITING)) {
775                                 if (i < 16)
776                                         mask |= 1 << i;
777                         }
778
779                         desc->status = status & ~IRQ_AUTODETECT;
780                         desc->handler->shutdown(i);
781                 }
782                 spin_unlock_irq(&desc->lock);
783         }
784
785         return mask & val;
786 }
787
788 /*
789  * Get the result of the IRQ probe.. A negative result means that
790  * we have several candidates (but we return the lowest-numbered
791  * one).
792  */
793
794 int
795 probe_irq_off(unsigned long val)
796 {
797         int i, irq_found, nr_irqs;
798
799         nr_irqs = 0;
800         irq_found = 0;
801         for (i=0; i<NR_IRQS; i++) {
802                 irq_desc_t *desc = irq_desc + i;
803                 unsigned int status;
804
805                 spin_lock_irq(&desc->lock);
806                 status = desc->status;
807
808                 if (status & IRQ_AUTODETECT) {
809                         if (!(status & IRQ_WAITING)) {
810                                 if (!nr_irqs)
811                                         irq_found = i;
812                                 nr_irqs++;
813                         }
814                         desc->status = status & ~IRQ_AUTODETECT;
815                         desc->handler->shutdown(i);
816                 }
817                 spin_unlock_irq(&desc->lock);
818         }
819
820         if (nr_irqs > 1)
821                 irq_found = -irq_found;
822         return irq_found;
823 }