ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / irq.c
1 /* 
2  * Code to handle x86 style IRQs plus some generic interrupt stuff.
3  *
4  * Copyright (C) 1992 Linus Torvalds
5  * Copyright (C) 1994, 1995, 1996, 1997, 1998 Ralf Baechle
6  * Copyright (C) 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
7  * Copyright (C) 1999-2000 Grant Grundler
8  *
9  *    This program is free software; you can redistribute it and/or modify
10  *    it under the terms of the GNU General Public License as published by
11  *    the Free Software Foundation; either version 2, or (at your option)
12  *    any later version.
13  *
14  *    This program is distributed in the hope that it will be useful,
15  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *    GNU General Public License for more details.
18  *
19  *    You should have received a copy of the GNU General Public License
20  *    along with this program; if not, write to the Free Software
21  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 #include <linux/bitops.h>
24 #include <linux/config.h>
25 #include <linux/eisa.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/signal.h>
30 #include <linux/types.h>
31 #include <linux/ioport.h>
32 #include <linux/timex.h>
33 #include <linux/slab.h>
34 #include <linux/random.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37 #include <linux/kernel_stat.h>
38 #include <linux/irq.h>
39 #include <linux/seq_file.h>
40 #include <linux/spinlock.h>
41
42 #include <asm/cache.h>
43 #include <asm/pdc.h>
44
45 #undef DEBUG_IRQ
46 #undef PARISC_IRQ_CR16_COUNTS
47
48 extern irqreturn_t timer_interrupt(int, void *, struct pt_regs *);
49 extern irqreturn_t ipi_interrupt(int, void *, struct pt_regs *);
50
51 #ifdef DEBUG_IRQ
52 #define DBG_IRQ(irq, x) if ((irq) != TIMER_IRQ) printk x
53 #else /* DEBUG_IRQ */
54 #define DBG_IRQ(irq, x) do { } while (0)
55 #endif /* DEBUG_IRQ */
56
57 #define EIEM_MASK(irq)       (1UL<<(MAX_CPU_IRQ-IRQ_OFFSET(irq)))
58
59 /* Bits in EIEM correlate with cpu_irq_action[].
60 ** Numbered *Big Endian*! (ie bit 0 is MSB)
61 */
62 static volatile unsigned long cpu_eiem = 0;
63
64 static spinlock_t irq_lock = SPIN_LOCK_UNLOCKED;  /* protect IRQ regions */
65
66 static void cpu_set_eiem(void *info)
67 {
68         set_eiem((unsigned long) info);
69 }
70
71 static inline void disable_cpu_irq(void *unused, int irq)
72 {
73         unsigned long eirr_bit = EIEM_MASK(irq);
74
75         cpu_eiem &= ~eirr_bit;
76         on_each_cpu(cpu_set_eiem, (void *) cpu_eiem, 1, 1);
77 }
78
79 static void enable_cpu_irq(void *unused, int irq)
80 {
81         unsigned long eirr_bit = EIEM_MASK(irq);
82
83         mtctl(eirr_bit, 23);    /* clear EIRR bit before unmasking */
84         cpu_eiem |= eirr_bit;
85         on_each_cpu(cpu_set_eiem, (void *) cpu_eiem, 1, 1);
86 }
87
88 /* mask and disable are the same at the CPU level
89 ** Difference is enable clears pending interrupts
90 */
91 #define mask_cpu_irq    disable_cpu_irq
92
93 static inline void unmask_cpu_irq(void *unused, int irq)
94 {
95         unsigned long eirr_bit = EIEM_MASK(irq);
96         cpu_eiem |= eirr_bit;
97         /* NOTE: sending an IPI will cause do_cpu_irq_mask() to
98         ** handle *any* unmasked pending interrupts.
99         ** ie We don't need to check for pending interrupts here.
100         */
101         on_each_cpu(cpu_set_eiem, (void *) cpu_eiem, 1, 1);
102 }
103
104 /*
105  * XXX cpu_irq_actions[] will become 2 dimensional for per CPU EIR support.
106  * correspond changes needed in:
107  *      processor_probe()       initialize additional action arrays
108  *      request_irq()           handle CPU IRQ region specially
109  *      do_cpu_irq_mask()       index into the matching irq_action array.
110  */
111 struct irqaction cpu_irq_actions[IRQ_PER_REGION] = {
112         [IRQ_OFFSET(TIMER_IRQ)] = {
113                                         .handler = timer_interrupt,
114                                         .name = "timer",
115                                 },
116 #ifdef CONFIG_SMP
117         [IRQ_OFFSET(IPI_IRQ)]   = {
118                                         .handler = ipi_interrupt,
119                                         .name = "IPI",
120                                 },
121 #endif
122 };
123
124 struct irq_region_ops cpu_irq_ops = {
125         .disable_irq    = disable_cpu_irq,
126         .enable_irq     = enable_cpu_irq,
127         .mask_irq       = unmask_cpu_irq,
128         .unmask_irq     = unmask_cpu_irq
129 };
130
131 struct irq_region cpu0_irq_region = {
132         .ops    = {
133                         .disable_irq    = disable_cpu_irq,
134                         .enable_irq     = enable_cpu_irq,
135                         .mask_irq       = unmask_cpu_irq,
136                         .unmask_irq     = unmask_cpu_irq
137         },
138         .data   = {
139                         .dev            = &cpu_data[0],
140                         .name           = "PARISC-CPU",
141                         .irqbase        = IRQ_FROM_REGION(CPU_IRQ_REGION),
142         },
143         .action = cpu_irq_actions,
144 };
145
146 struct irq_region *irq_region[NR_IRQ_REGS] = {
147         [ 0 ]              = NULL, /* reserved for EISA, else causes data page fault (aka code 15) */
148         [ CPU_IRQ_REGION ] = &cpu0_irq_region,
149 };
150
151
152 /*
153 ** Generic interfaces that device drivers can use:
154 **    mask_irq()        block IRQ
155 **    unmask_irq()      re-enable IRQ and trigger if IRQ is pending
156 **    disable_irq()     block IRQ
157 **    enable_irq()      clear pending and re-enable IRQ
158 */
159
160 void mask_irq(int irq)
161 {
162         struct irq_region *region;
163
164         DBG_IRQ(irq, ("mask_irq(%d) %d+%d eiem 0x%lx\n", irq,
165                                 IRQ_REGION(irq), IRQ_OFFSET(irq), cpu_eiem));
166         irq = irq_canonicalize(irq);
167         region = irq_region[IRQ_REGION(irq)];
168         if (region->ops.mask_irq)
169                 region->ops.mask_irq(region->data.dev, IRQ_OFFSET(irq));
170 }
171
172 void unmask_irq(int irq)
173 {
174         struct irq_region *region;
175
176         DBG_IRQ(irq, ("unmask_irq(%d) %d+%d eiem 0x%lx\n", irq,
177                                 IRQ_REGION(irq), IRQ_OFFSET(irq), cpu_eiem));
178         irq = irq_canonicalize(irq);
179         region = irq_region[IRQ_REGION(irq)];
180         if (region->ops.unmask_irq)
181                 region->ops.unmask_irq(region->data.dev, IRQ_OFFSET(irq));
182 }
183
184 void disable_irq(int irq)
185 {
186         struct irq_region *region;
187
188         DBG_IRQ(irq, ("disable_irq(%d) %d+%d eiem 0x%lx\n", irq,
189                                 IRQ_REGION(irq), IRQ_OFFSET(irq), cpu_eiem));
190         irq = irq_canonicalize(irq);
191         region = irq_region[IRQ_REGION(irq)];
192         if (region->ops.disable_irq)
193                 region->ops.disable_irq(region->data.dev, IRQ_OFFSET(irq));
194         else
195                 BUG();
196 }
197 EXPORT_SYMBOL(disable_irq);
198
199 void enable_irq(int irq)
200 {
201         struct irq_region *region;
202
203         DBG_IRQ(irq, ("enable_irq(%d) %d+%d eiem 0x%lx\n", irq,
204                                 IRQ_REGION(irq), IRQ_OFFSET(irq), cpu_eiem));
205         irq = irq_canonicalize(irq);
206         region = irq_region[IRQ_REGION(irq)];
207
208         if (region->ops.enable_irq)
209                 region->ops.enable_irq(region->data.dev, IRQ_OFFSET(irq));
210         else
211                 BUG();
212 }
213 EXPORT_SYMBOL(enable_irq);
214
215 int show_interrupts(struct seq_file *p, void *v)
216 {
217 #ifdef CONFIG_PROC_FS
218         unsigned int regnr = *(loff_t *) v, i = 0;
219
220         if (regnr == 0) {
221                 seq_puts(p, "     ");
222 #ifdef CONFIG_SMP
223                 for (i = 0; i < NR_CPUS; i++)
224 #endif
225                         seq_printf(p, "      CPU%02d ", i);
226
227 #ifdef PARISC_IRQ_CR16_COUNTS
228                 seq_printf(p, "[min/avg/max] (CPU cycle counts)");
229 #endif
230                 seq_putc(p, '\n');
231         }
232
233         /* We don't need *irqsave lock variants since this is
234         ** only allowed to change while in the base context.
235         */
236         spin_lock(&irq_lock);
237         if (regnr < NR_IRQ_REGS) {
238             struct irq_region *region = irq_region[regnr];
239
240             if (!region || !region->action)
241                     goto skip;
242
243             for (i = 0; i <= MAX_CPU_IRQ; i++) {
244                 struct irqaction *action = &region->action[i];
245                 unsigned int irq_no = IRQ_FROM_REGION(regnr) + i;
246                 int j = 0;
247                 if (!action->handler)
248                         continue;
249
250                 seq_printf(p, "%3d: ", irq_no);
251 #ifdef CONFIG_SMP
252                 for (; j < NR_CPUS; j++)
253 #endif
254                   seq_printf(p, "%10u ", kstat_cpu(j).irqs[irq_no]);
255
256                 seq_printf(p, " %14s",
257                             region->data.name ? region->data.name : "N/A");
258 #ifndef PARISC_IRQ_CR16_COUNTS
259                 seq_printf(p, "  %s", action->name);
260
261                 while ((action = action->next))
262                         seq_printf(p, ", %s", action->name);
263 #else
264                 for ( ;action; action = action->next) {
265                         unsigned int k, avg, min, max;
266
267                         min = max = action->cr16_hist[0];
268
269                         for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
270                                 int hist = action->cr16_hist[k];
271
272                                 if (hist) {
273                                         avg += hist;
274                                 } else
275                                         break;
276
277                                 if (hist > max) max = hist;
278                                 if (hist < min) min = hist;
279                         }
280
281                         avg /= k;
282                         seq_printf(p, " %s[%d/%d/%d]", action->name,
283                                         min,avg,max);
284                 }
285 #endif
286
287                 seq_putc(p, '\n');
288             }
289         }
290   skip:
291         spin_unlock(&irq_lock);
292
293 #endif  /* CONFIG_PROC_FS */
294         return 0;
295 }
296
297
298
299 /*
300 ** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
301 ** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
302 **
303 ** To use txn_XXX() interfaces, get a Virtual IRQ first.
304 ** Then use that to get the Transaction address and data.
305 */
306
307 int
308 txn_alloc_irq(void)
309 {
310         int irq;
311
312         /* never return irq 0 cause that's the interval timer */
313         for (irq = 1; irq <= MAX_CPU_IRQ; irq++) {
314                 if (cpu_irq_actions[irq].handler == NULL) {
315                         return (IRQ_FROM_REGION(CPU_IRQ_REGION) + irq);
316                 }
317         }
318
319         /* unlikely, but be prepared */
320         return -1;
321 }
322
323 int
324 txn_claim_irq(int irq)
325 {
326         if (irq_region[IRQ_REGION(irq)]->action[IRQ_OFFSET(irq)].handler ==NULL)
327                 return irq;
328
329         /* unlikely, but be prepared */
330         return -1;
331 }
332
333 unsigned long
334 txn_alloc_addr(int virt_irq)
335 {
336         static int next_cpu = -1;
337
338         next_cpu++; /* assign to "next" CPU we want this bugger on */
339
340         /* validate entry */
341         while ((next_cpu < NR_CPUS) && !cpu_data[next_cpu].txn_addr)
342                 next_cpu++;
343
344         if (next_cpu >= NR_CPUS) 
345                 next_cpu = 0;   /* nothing else, assign monarch */
346
347         return cpu_data[next_cpu].txn_addr;
348 }
349
350
351 /*
352 ** The alloc process needs to accept a parameter to accommodate limitations
353 ** of the HW/SW which use these bits:
354 ** Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
355 ** V-class (EPIC):          6 bits
356 ** N/L-class/A500:          8 bits (iosapic)
357 ** PCI 2.2 MSI:             16 bits (I think)
358 ** Existing PCI devices:    32-bits (all Symbios SCSI/ATM/HyperFabric)
359 **
360 ** On the service provider side:
361 ** o PA 1.1 (and PA2.0 narrow mode)     5-bits (width of EIR register)
362 ** o PA 2.0 wide mode                   6-bits (per processor)
363 ** o IA64                               8-bits (0-256 total)
364 **
365 ** So a Legacy PA I/O device on a PA 2.0 box can't use all
366 ** the bits supported by the processor...and the N/L-class
367 ** I/O subsystem supports more bits than PA2.0 has. The first
368 ** case is the problem.
369 */
370 unsigned int
371 txn_alloc_data(int virt_irq, unsigned int bits_wide)
372 {
373         /* XXX FIXME : bits_wide indicates how wide the transaction
374         ** data is allowed to be...we may need a different virt_irq
375         ** if this one won't work. Another reason to index virtual
376         ** irq's into a table which can manage CPU/IRQ bit separately.
377         */
378         if (IRQ_OFFSET(virt_irq) > (1 << (bits_wide -1)))
379         {
380                 panic("Sorry -- didn't allocate valid IRQ for this device\n");
381         }
382
383         return (IRQ_OFFSET(virt_irq));
384 }
385
386 void do_irq(struct irqaction *action, int irq, struct pt_regs * regs)
387 {
388         int cpu = smp_processor_id();
389
390         irq_enter();
391         ++kstat_cpu(cpu).irqs[irq];
392
393         DBG_IRQ(irq, ("do_irq(%d) %d+%d\n", irq, IRQ_REGION(irq), IRQ_OFFSET(irq)));
394
395         for (; action; action = action->next) {
396 #ifdef PARISC_IRQ_CR16_COUNTS
397                 unsigned long cr_start = mfctl(16);
398 #endif
399
400                 if (action->handler == NULL) {
401                         if (IRQ_REGION(irq) == EISA_IRQ_REGION && irq_region[EISA_IRQ_REGION]) {
402                                 /* were we called due to autodetecting (E)ISA irqs ? */
403                                 unsigned int *status;
404                                 status = &irq_region[EISA_IRQ_REGION]->data.status[IRQ_OFFSET(irq)];
405                                 if (*status & IRQ_AUTODETECT) {
406                                         *status &= ~IRQ_WAITING;
407                                         continue; 
408                                 }
409                         }
410                         printk(KERN_ERR "IRQ:  CPU:%d No handler for IRQ %d !\n", cpu, irq);
411                         continue;
412                 }
413
414                 action->handler(irq, action->dev_id, regs);
415
416 #ifdef PARISC_IRQ_CR16_COUNTS
417                 {
418                         unsigned long cr_end = mfctl(16);
419                         unsigned long tmp = cr_end - cr_start;
420                         /* check for roll over */
421                         cr_start = (cr_end < cr_start) ?  -(tmp) : (tmp);
422                 }
423                 action->cr16_hist[action->cr16_idx++] = (int) cr_start;
424                 action->cr16_idx &= PARISC_CR16_HIST_SIZE - 1;
425 #endif
426         }
427
428         irq_exit();
429 }
430
431
432 /* ONLY called from entry.S:intr_extint() */
433 void do_cpu_irq_mask(struct pt_regs *regs)
434 {
435         unsigned long eirr_val;
436         unsigned int i=3;       /* limit time in interrupt context */
437
438         /*
439          * PSW_I or EIEM bits cannot be enabled until after the
440          * interrupts are processed.
441          * timer_interrupt() assumes it won't get interrupted when it
442          * holds the xtime_lock...an unmasked interrupt source could
443          * interrupt and deadlock by trying to grab xtime_lock too.
444          * Keeping PSW_I and EIEM disabled avoids this.
445          */
446         set_eiem(0UL);  /* disable all extr interrupt for now */
447
448         /* 1) only process IRQs that are enabled/unmasked (cpu_eiem)
449          * 2) We loop here on EIRR contents in order to avoid
450          *    nested interrupts or having to take another interrupt
451          *    when we could have just handled it right away.
452          * 3) Limit the number of times we loop to make sure other
453          *    processing can occur.
454          */
455         while ((eirr_val = (mfctl(23) & cpu_eiem)) && --i) {
456                 unsigned long bit = (1UL<<MAX_CPU_IRQ);
457                 unsigned int irq;
458
459                 mtctl(eirr_val, 23); /* reset bits we are going to process */
460
461 #ifdef DEBUG_IRQ
462                 if (eirr_val != (1UL << MAX_CPU_IRQ))
463                         printk(KERN_DEBUG "do_cpu_irq_mask  %x\n", eirr_val);
464 #endif
465
466                 /* Work our way from MSb to LSb...same order we alloc EIRs */
467                 for (irq = 0; eirr_val && bit; bit>>=1, irq++)
468                 {
469                         if (!(bit & eirr_val & cpu_eiem))
470                                 continue;
471
472                         /* clear bit in mask - can exit loop sooner */
473                         eirr_val &= ~bit;
474
475                         do_irq(&cpu_irq_actions[irq], TIMER_IRQ+irq, regs);
476                 }
477         }
478         set_eiem(cpu_eiem);
479 }
480
481
482 /* Called from second level IRQ regions: eg dino or iosapic. */
483 void do_irq_mask(unsigned long mask, struct irq_region *region, struct pt_regs *regs)
484 {
485         unsigned long bit;
486         unsigned int irq;
487
488 #ifdef DEBUG_IRQ
489         if (mask != (1L<<MAX_CPU_IRQ))
490             printk(KERN_DEBUG "do_irq_mask %08lx %p %p\n", mask, region, regs);
491 #endif
492
493         for (bit = (1L<<MAX_CPU_IRQ), irq = 0; mask && bit; bit>>=1, irq++) {
494                 unsigned int irq_num;
495                 if (!(bit&mask))
496                         continue;
497
498                 mask &= ~bit;   /* clear bit in mask - can exit loop sooner */
499                 irq_num = region->data.irqbase + irq;
500
501                 mask_irq(irq_num);
502                 do_irq(&region->action[irq], irq_num, regs);
503                 unmask_irq(irq_num);
504         }
505 }
506
507
508 static inline int find_free_region(void)
509 {
510         int irqreg;
511
512         for (irqreg=1; irqreg <= (NR_IRQ_REGS); irqreg++) {
513                 if (irq_region[irqreg] == NULL)
514                         return irqreg;
515         }
516
517         return 0;
518 }
519
520
521 /*****
522  * alloc_irq_region - allocate/init a new IRQ region
523  * @count: number of IRQs in this region.
524  * @ops: function table with request/release/mask/unmask/etc.. entries.
525  * @name: name of region owner for /proc/interrupts output.
526  * @dev: private data to associate with the new IRQ region.
527  *
528  * Every IRQ must become a MMIO write to the CPU's EIRR in
529  * order to get CPU service. The IRQ region represents the
530  * number of unique events the region handler can (or must)
531  * identify. For PARISC CPU, that's the width of the EIR Register.
532  * IRQ regions virtualize IRQs (eg EISA or PCI host bus controllers)
533  * for line based devices.
534  */
535 struct irq_region *alloc_irq_region( int count, struct irq_region_ops *ops,
536                                         const char *name, void *dev)
537 {
538         struct irq_region *region;
539         int index;
540
541         index = find_free_region();
542         if (index == 0) {
543                 printk(KERN_ERR "Maximum number of irq regions exceeded. Increase NR_IRQ_REGS!\n");
544                 return NULL;
545         }
546
547         if ((IRQ_REGION(count-1)))
548                 return NULL;
549
550         if (count < IRQ_PER_REGION) {
551             DBG_IRQ(0, ("alloc_irq_region() using minimum of %d irq lines for %s (%d)\n",
552                         IRQ_PER_REGION, name, count));
553             count = IRQ_PER_REGION;
554         }
555
556         /* if either mask *or* unmask is set, both have to be set. */
557         if((ops->mask_irq || ops->unmask_irq) &&
558                 !(ops->mask_irq && ops->unmask_irq))
559                         return NULL;
560
561         /* ditto for enable/disable */
562         if( (ops->disable_irq || ops->enable_irq) &&
563                 !(ops->disable_irq && ops->enable_irq) )
564                         return NULL;
565
566         region = kmalloc(sizeof(*region), GFP_ATOMIC);
567         if (!region)
568                 return NULL;
569         memset(region, 0, sizeof(*region));
570
571         region->action = kmalloc(count * sizeof(*region->action), GFP_ATOMIC);
572         if (!region->action) {
573                 kfree(region);
574                 return NULL;
575         }
576         memset(region->action, 0, count * sizeof(*region->action));
577
578         region->ops = *ops;
579         region->data.irqbase = IRQ_FROM_REGION(index);
580         region->data.name = name;
581         region->data.dev = dev;
582
583         irq_region[index] = region;
584
585         return irq_region[index];
586 }
587
588 /* FIXME: SMP, flags, bottom halves, rest */
589
590 int request_irq(unsigned int irq,
591                 irqreturn_t (*handler)(int, void *, struct pt_regs *),
592                 unsigned long irqflags,
593                 const char * devname,
594                 void *dev_id)
595 {
596         struct irqaction * action;
597
598 #if 0
599         printk(KERN_INFO "request_irq(%d, %p, 0x%lx, %s, %p)\n",irq, handler, irqflags, devname, dev_id);
600 #endif
601
602         irq = irq_canonicalize(irq);
603         /* request_irq()/free_irq() may not be called from interrupt context. */
604         if (in_interrupt())
605                 BUG();
606
607         if (!handler) {
608                 printk(KERN_ERR "request_irq(%d,...): Augh! No handler for irq!\n",
609                         irq);
610                 return -EINVAL;
611         }
612
613         if (irq_region[IRQ_REGION(irq)] == NULL) {
614                 /*
615                 ** Bug catcher for drivers which use "char" or u8 for
616                 ** the IRQ number. They lose the region number which
617                 ** is in pcidev->irq (an int).
618                 */
619                 printk(KERN_ERR "%p (%s?) called request_irq with an invalid irq %d\n",
620                         __builtin_return_address(0), devname, irq);
621                 return -EINVAL;
622         }
623
624         spin_lock(&irq_lock);
625         action = &(irq_region[IRQ_REGION(irq)]->action[IRQ_OFFSET(irq)]);
626
627         /* First one is preallocated. */
628         if (action->handler) {
629                 /* But it's in use...find the tail and allocate a new one */
630                 while (action->next)
631                         action = action->next;
632
633                 action->next = kmalloc(sizeof(*action), GFP_ATOMIC);
634                 memset(action->next, 0, sizeof(*action));
635
636                 action = action->next;
637         }
638
639         if (!action) {
640                 spin_unlock(&irq_lock);
641                 printk(KERN_ERR "request_irq(): Augh! No action!\n") ;
642                 return -ENOMEM;
643         }
644
645         action->handler = handler;
646         action->flags = irqflags;
647         action->mask = 0;
648         action->name = devname;
649         action->next = NULL;
650         action->dev_id = dev_id;
651         spin_unlock(&irq_lock);
652
653         enable_irq(irq);
654         return 0;
655 }
656
657 EXPORT_SYMBOL(request_irq);
658
659 void free_irq(unsigned int irq, void *dev_id)
660 {
661         struct irqaction *action, **p;
662
663         /* See comments in request_irq() about interrupt context */
664         irq = irq_canonicalize(irq);
665         
666         if (in_interrupt()) BUG();
667
668         spin_lock(&irq_lock);
669         action = &irq_region[IRQ_REGION(irq)]->action[IRQ_OFFSET(irq)];
670
671         if (action->dev_id == dev_id) {
672                 if (action->next == NULL) {
673                         action->handler = NULL;
674                 } else {
675                         memcpy(action, action->next, sizeof(*action));
676                 }
677
678                 spin_unlock(&irq_lock);
679                 return;
680         }
681
682         p = &action->next;
683         action = action->next;
684
685         for (; (action = *p) != NULL; p = &action->next) {
686                 if (action->dev_id != dev_id)
687                         continue;
688
689                 /* Found it - now free it */
690                 *p = action->next;
691                 kfree(action);
692
693                 spin_unlock(&irq_lock);
694                 return;
695         }
696
697         spin_unlock(&irq_lock);
698         printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
699 }
700
701 EXPORT_SYMBOL(free_irq);
702
703
704 #ifdef CONFIG_SMP
705 void synchronize_irq(unsigned int irqnum)
706 {
707         while (in_irq()) ;
708 }
709 EXPORT_SYMBOL(synchronize_irq);
710 #endif
711
712
713 /*
714  * IRQ autodetection code..
715  *
716  * This depends on the fact that any interrupt that
717  * comes in on to an unassigned handler will get stuck
718  * with "IRQ_WAITING" cleared and the interrupt
719  * disabled.
720  */
721
722 static DECLARE_MUTEX(probe_sem);
723
724 /**
725  *      probe_irq_on    - begin an interrupt autodetect
726  *
727  *      Commence probing for an interrupt. The interrupts are scanned
728  *      and a mask of potential interrupt lines is returned.
729  *
730  */
731
732 /* TODO: spin_lock_irq(desc->lock -> irq_lock) */
733
734 unsigned long probe_irq_on(void)
735 {
736         unsigned int i;
737         unsigned long val;
738         unsigned long delay;
739         struct irq_region *region;
740
741         /* support for irq autoprobing is limited to EISA (irq region 0) */
742         region = irq_region[EISA_IRQ_REGION];
743         if (!EISA_bus || !region)
744                 return 0;
745
746         down(&probe_sem);
747
748         /*
749          * enable any unassigned irqs
750          * (we must startup again here because if a longstanding irq
751          * happened in the previous stage, it may have masked itself)
752          */
753         for (i = EISA_MAX_IRQS-1; i > 0; i--) {
754                 struct irqaction *action;
755                 
756                 spin_lock_irq(&irq_lock);
757                 action = region->action + i;
758                 if (!action->handler) {
759                         region->data.status[i] |= IRQ_AUTODETECT | IRQ_WAITING;
760                         region->ops.enable_irq(region->data.dev,i);
761                 }
762                 spin_unlock_irq(&irq_lock);
763         }
764
765         /*
766          * Wait for spurious interrupts to trigger
767          */
768         for (delay = jiffies + HZ/10; time_after(delay, jiffies); )
769                 /* about 100ms delay */ barrier();
770
771         /*
772          * Now filter out any obviously spurious interrupts
773          */
774         val = 0;
775         for (i = 0; i < EISA_MAX_IRQS; i++) {
776                 unsigned int status;
777
778                 spin_lock_irq(&irq_lock);
779                 status = region->data.status[i];
780
781                 if (status & IRQ_AUTODETECT) {
782                         /* It triggered already - consider it spurious. */
783                         if (!(status & IRQ_WAITING)) {
784                                 region->data.status[i] = status & ~IRQ_AUTODETECT;
785                                 region->ops.disable_irq(region->data.dev,i);
786                         } else
787                                 if (i < BITS_PER_LONG)
788                                         val |= (1 << i);
789                 }
790                 spin_unlock_irq(&irq_lock);
791         }
792
793         return val;
794 }
795
796 EXPORT_SYMBOL(probe_irq_on);
797
798 /*
799  * Return the one interrupt that triggered (this can
800  * handle any interrupt source).
801  */
802
803 /**
804  *      probe_irq_off   - end an interrupt autodetect
805  *      @val: mask of potential interrupts (unused)
806  *
807  *      Scans the unused interrupt lines and returns the line which
808  *      appears to have triggered the interrupt. If no interrupt was
809  *      found then zero is returned. If more than one interrupt is
810  *      found then minus the first candidate is returned to indicate
811  *      their is doubt.
812  *
813  *      The interrupt probe logic state is returned to its previous
814  *      value.
815  *
816  *      BUGS: When used in a module (which arguably shouldnt happen)
817  *      nothing prevents two IRQ probe callers from overlapping. The
818  *      results of this are non-optimal.
819  */
820  
821 int probe_irq_off(unsigned long val)
822 {
823         struct irq_region *region;
824         int i, irq_found, nr_irqs;
825
826         /* support for irq autoprobing is limited to EISA (irq region 0) */
827         region = irq_region[EISA_IRQ_REGION];
828         if (!EISA_bus || !region)
829                 return 0;
830
831         nr_irqs = 0;
832         irq_found = 0;
833         for (i = 0; i < EISA_MAX_IRQS; i++) {
834                 unsigned int status;
835                 
836                 spin_lock_irq(&irq_lock);
837                 status = region->data.status[i];
838
839                 if (status & IRQ_AUTODETECT) {
840                         if (!(status & IRQ_WAITING)) {
841                                 if (!nr_irqs)
842                                         irq_found = i;
843                                 nr_irqs++;
844                         }
845                         region->ops.disable_irq(region->data.dev,i);
846                         region->data.status[i] = status & ~IRQ_AUTODETECT;
847                 }
848                 spin_unlock_irq(&irq_lock);
849         }
850         up(&probe_sem);
851
852         if (nr_irqs > 1)
853                 irq_found = -irq_found;
854         return irq_found;
855 }
856
857 EXPORT_SYMBOL(probe_irq_off);
858
859 unsigned int probe_irq_mask(unsigned long irqs)
860 {
861         return 0;
862 }
863 EXPORT_SYMBOL(probe_irq_mask);
864
865 void __init init_IRQ(void)
866 {
867         local_irq_disable();    /* PARANOID - should already be disabled */
868         mtctl(-1L, 23);         /* EIRR : clear all pending external intr */
869 #ifdef CONFIG_SMP
870         if (!cpu_eiem)
871                 cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
872 #else
873         cpu_eiem = EIEM_MASK(TIMER_IRQ);
874 #endif
875         set_eiem(cpu_eiem);     /* EIEM : enable all external intr */
876
877 }
878
879 #ifdef CONFIG_PROC_FS
880 /* called from kernel/sysctl.c:sysctl_init() */
881 void __init init_irq_proc(void)
882 {
883 }
884 #endif