Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / powerpc / kernel / irq.c
1 /*
2  *  Derived from arch/i386/kernel/irq.c
3  *    Copyright (C) 1992 Linus Torvalds
4  *  Adapted from arch/i386 by Gary Thomas
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
7  *    Copyright (C) 1996-2001 Cort Dougan
8  *  Adapted for Power Macintosh by Paul Mackerras
9  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version
15  * 2 of the License, or (at your option) any later version.
16  *
17  * This file contains the code used by various IRQ handling routines:
18  * asking for different IRQ's should be done through these routines
19  * instead of just grabbing them. Thus setups with different IRQ numbers
20  * shouldn't result in any weird surprises, and installing new handlers
21  * should be easier.
22  *
23  * The MPC8xx has an interrupt mask in the SIU.  If a bit is set, the
24  * interrupt is _enabled_.  As expected, IRQ0 is bit 0 in the 32-bit
25  * mask register (of which only 16 are defined), hence the weird shifting
26  * and complement of the cached_irq_mask.  I want to be able to stuff
27  * this right into the SIU SMASK register.
28  * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
29  * to reduce code space and undefined function references.
30  */
31
32 #undef DEBUG
33
34 #include <linux/module.h>
35 #include <linux/threads.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/interrupt.h>
42 #include <linux/timex.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/delay.h>
46 #include <linux/irq.h>
47 #include <linux/seq_file.h>
48 #include <linux/cpumask.h>
49 #include <linux/profile.h>
50 #include <linux/bitops.h>
51 #include <linux/list.h>
52 #include <linux/radix-tree.h>
53 #include <linux/mutex.h>
54 #include <linux/bootmem.h>
55 #include <linux/vs_context.h>
56
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
59 #include <asm/io.h>
60 #include <asm/pgtable.h>
61 #include <asm/irq.h>
62 #include <asm/cache.h>
63 #include <asm/prom.h>
64 #include <asm/ptrace.h>
65 #include <asm/machdep.h>
66 #include <asm/udbg.h>
67 #ifdef CONFIG_PPC_ISERIES
68 #include <asm/paca.h>
69 #endif
70
71 int __irq_offset_value;
72 static int ppc_spurious_interrupts;
73
74 #ifdef CONFIG_PPC32
75 EXPORT_SYMBOL(__irq_offset_value);
76 atomic_t ppc_n_lost_interrupts;
77
78 #ifndef CONFIG_PPC_MERGE
79 #define NR_MASK_WORDS   ((NR_IRQS + 31) / 32)
80 unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
81 #endif
82
83 #ifdef CONFIG_TAU_INT
84 extern int tau_initialized;
85 extern int tau_interrupts(int);
86 #endif
87 #endif /* CONFIG_PPC32 */
88
89 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
90 extern atomic_t ipi_recv;
91 extern atomic_t ipi_sent;
92 #endif
93
94 #ifdef CONFIG_PPC64
95 EXPORT_SYMBOL(irq_desc);
96
97 int distribute_irqs = 1;
98 #endif /* CONFIG_PPC64 */
99
100 int show_interrupts(struct seq_file *p, void *v)
101 {
102         int i = *(loff_t *)v, j;
103         struct irqaction *action;
104         irq_desc_t *desc;
105         unsigned long flags;
106
107         if (i == 0) {
108                 seq_puts(p, "           ");
109                 for_each_online_cpu(j)
110                         seq_printf(p, "CPU%d       ", j);
111                 seq_putc(p, '\n');
112         }
113
114         if (i < NR_IRQS) {
115                 desc = get_irq_desc(i);
116                 spin_lock_irqsave(&desc->lock, flags);
117                 action = desc->action;
118                 if (!action || !action->handler)
119                         goto skip;
120                 seq_printf(p, "%3d: ", i);
121 #ifdef CONFIG_SMP
122                 for_each_online_cpu(j)
123                         seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
124 #else
125                 seq_printf(p, "%10u ", kstat_irqs(i));
126 #endif /* CONFIG_SMP */
127                 if (desc->chip)
128                         seq_printf(p, " %s ", desc->chip->typename);
129                 else
130                         seq_puts(p, "  None      ");
131                 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
132                 seq_printf(p, "    %s", action->name);
133                 for (action = action->next; action; action = action->next)
134                         seq_printf(p, ", %s", action->name);
135                 seq_putc(p, '\n');
136 skip:
137                 spin_unlock_irqrestore(&desc->lock, flags);
138         } else if (i == NR_IRQS) {
139 #ifdef CONFIG_PPC32
140 #ifdef CONFIG_TAU_INT
141                 if (tau_initialized){
142                         seq_puts(p, "TAU: ");
143                         for_each_online_cpu(j)
144                                 seq_printf(p, "%10u ", tau_interrupts(j));
145                         seq_puts(p, "  PowerPC             Thermal Assist (cpu temp)\n");
146                 }
147 #endif
148 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
149                 /* should this be per processor send/receive? */
150                 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
151                                 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
152 #endif
153 #endif /* CONFIG_PPC32 */
154                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
155         }
156         return 0;
157 }
158
159 #ifdef CONFIG_HOTPLUG_CPU
160 void fixup_irqs(cpumask_t map)
161 {
162         unsigned int irq;
163         static int warned;
164
165         for_each_irq(irq) {
166                 cpumask_t mask;
167
168                 if (irq_desc[irq].status & IRQ_PER_CPU)
169                         continue;
170
171                 cpus_and(mask, irq_desc[irq].affinity, map);
172                 if (any_online_cpu(mask) == NR_CPUS) {
173                         printk("Breaking affinity for irq %i\n", irq);
174                         mask = map;
175                 }
176                 if (irq_desc[irq].chip->set_affinity)
177                         irq_desc[irq].chip->set_affinity(irq, mask);
178                 else if (irq_desc[irq].action && !(warned++))
179                         printk("Cannot set affinity for irq %i\n", irq);
180         }
181
182         local_irq_enable();
183         mdelay(1);
184         local_irq_disable();
185 }
186 #endif
187
188 void do_IRQ(struct pt_regs *regs)
189 {
190         unsigned int irq;
191 #ifdef CONFIG_IRQSTACKS
192         struct thread_info *curtp, *irqtp;
193 #endif
194
195         irq_enter();
196
197 #ifdef CONFIG_DEBUG_STACKOVERFLOW
198         /* Debugging check for stack overflow: is there less than 2KB free? */
199         {
200                 long sp;
201
202                 sp = __get_SP() & (THREAD_SIZE-1);
203
204                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
205                         printk("do_IRQ: stack overflow: %ld\n",
206                                 sp - sizeof(struct thread_info));
207                         dump_stack();
208                 }
209         }
210 #endif
211
212         /*
213          * Every platform is required to implement ppc_md.get_irq.
214          * This function will either return an irq number or -1 to
215          * indicate there are no more pending.
216          * The value -2 is for buggy hardware and means that this IRQ
217          * has already been handled. -- Tom
218          */
219         irq = ppc_md.get_irq(regs);
220
221         if (irq != NO_IRQ && irq != NO_IRQ_IGNORE) {
222                 struct vx_info_save vxis;
223
224                 __enter_vx_admin(&vxis);
225 #ifdef CONFIG_IRQSTACKS
226                 /* Switch to the irq stack to handle this */
227                 curtp = current_thread_info();
228                 irqtp = hardirq_ctx[smp_processor_id()];
229                 if (curtp != irqtp) {
230                         struct irq_desc *desc = irq_desc + irq;
231                         void *handler = desc->handle_irq;
232                         if (handler == NULL)
233                                 handler = &__do_IRQ;
234                         irqtp->task = curtp->task;
235                         irqtp->flags = 0;
236                         call_handle_irq(irq, desc, regs, irqtp, handler);
237                         irqtp->task = NULL;
238                         if (irqtp->flags)
239                                 set_bits(irqtp->flags, &curtp->flags);
240                 } else
241 #endif
242                         generic_handle_irq(irq, regs);
243                 __leave_vx_admin(&vxis);
244         } else if (irq != NO_IRQ_IGNORE)
245                 /* That's not SMP safe ... but who cares ? */
246                 ppc_spurious_interrupts++;
247
248         irq_exit();
249
250 #ifdef CONFIG_PPC_ISERIES
251         if (get_lppaca()->int_dword.fields.decr_int) {
252
253                 get_lppaca()->int_dword.fields.decr_int = 0;
254                 /* Signal a fake decrementer interrupt */
255                 timer_interrupt(regs);
256         }
257 #endif
258 }
259
260 void __init init_IRQ(void)
261 {
262         ppc_md.init_IRQ();
263 #ifdef CONFIG_PPC64
264         irq_ctx_init();
265 #endif
266 }
267
268
269 #ifdef CONFIG_IRQSTACKS
270 struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
271 struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
272
273 void irq_ctx_init(void)
274 {
275         struct thread_info *tp;
276         int i;
277
278         for_each_possible_cpu(i) {
279                 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
280                 tp = softirq_ctx[i];
281                 tp->cpu = i;
282                 tp->preempt_count = SOFTIRQ_OFFSET;
283
284                 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
285                 tp = hardirq_ctx[i];
286                 tp->cpu = i;
287                 tp->preempt_count = HARDIRQ_OFFSET;
288         }
289 }
290
291 static inline void do_softirq_onstack(void)
292 {
293         struct thread_info *curtp, *irqtp;
294
295         curtp = current_thread_info();
296         irqtp = softirq_ctx[smp_processor_id()];
297         irqtp->task = curtp->task;
298         call_do_softirq(irqtp);
299         irqtp->task = NULL;
300 }
301
302 #else
303 #define do_softirq_onstack()    __do_softirq()
304 #endif /* CONFIG_IRQSTACKS */
305
306 void do_softirq(void)
307 {
308         unsigned long flags;
309
310         if (in_interrupt())
311                 return;
312
313         local_irq_save(flags);
314
315         if (local_softirq_pending())
316                 do_softirq_onstack();
317
318         local_irq_restore(flags);
319 }
320 EXPORT_SYMBOL(do_softirq);
321
322
323 /*
324  * IRQ controller and virtual interrupts
325  */
326
327 #ifdef CONFIG_PPC_MERGE
328
329 static LIST_HEAD(irq_hosts);
330 static spinlock_t irq_big_lock = SPIN_LOCK_UNLOCKED;
331 static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
332 static unsigned int irq_radix_writer;
333 struct irq_map_entry irq_map[NR_IRQS];
334 static unsigned int irq_virq_count = NR_IRQS;
335 static struct irq_host *irq_default_host;
336
337 struct irq_host *irq_alloc_host(unsigned int revmap_type,
338                                 unsigned int revmap_arg,
339                                 struct irq_host_ops *ops,
340                                 irq_hw_number_t inval_irq)
341 {
342         struct irq_host *host;
343         unsigned int size = sizeof(struct irq_host);
344         unsigned int i;
345         unsigned int *rmap;
346         unsigned long flags;
347
348         /* Allocate structure and revmap table if using linear mapping */
349         if (revmap_type == IRQ_HOST_MAP_LINEAR)
350                 size += revmap_arg * sizeof(unsigned int);
351         if (mem_init_done)
352                 host = kzalloc(size, GFP_KERNEL);
353         else {
354                 host = alloc_bootmem(size);
355                 if (host)
356                         memset(host, 0, size);
357         }
358         if (host == NULL)
359                 return NULL;
360
361         /* Fill structure */
362         host->revmap_type = revmap_type;
363         host->inval_irq = inval_irq;
364         host->ops = ops;
365
366         spin_lock_irqsave(&irq_big_lock, flags);
367
368         /* If it's a legacy controller, check for duplicates and
369          * mark it as allocated (we use irq 0 host pointer for that
370          */
371         if (revmap_type == IRQ_HOST_MAP_LEGACY) {
372                 if (irq_map[0].host != NULL) {
373                         spin_unlock_irqrestore(&irq_big_lock, flags);
374                         /* If we are early boot, we can't free the structure,
375                          * too bad...
376                          * this will be fixed once slab is made available early
377                          * instead of the current cruft
378                          */
379                         if (mem_init_done)
380                                 kfree(host);
381                         return NULL;
382                 }
383                 irq_map[0].host = host;
384         }
385
386         list_add(&host->link, &irq_hosts);
387         spin_unlock_irqrestore(&irq_big_lock, flags);
388
389         /* Additional setups per revmap type */
390         switch(revmap_type) {
391         case IRQ_HOST_MAP_LEGACY:
392                 /* 0 is always the invalid number for legacy */
393                 host->inval_irq = 0;
394                 /* setup us as the host for all legacy interrupts */
395                 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
396                         irq_map[i].hwirq = 0;
397                         smp_wmb();
398                         irq_map[i].host = host;
399                         smp_wmb();
400
401                         /* Clear norequest flags */
402                         get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
403
404                         /* Legacy flags are left to default at this point,
405                          * one can then use irq_create_mapping() to
406                          * explicitely change them
407                          */
408                         ops->map(host, i, i);
409                 }
410                 break;
411         case IRQ_HOST_MAP_LINEAR:
412                 rmap = (unsigned int *)(host + 1);
413                 for (i = 0; i < revmap_arg; i++)
414                         rmap[i] = IRQ_NONE;
415                 host->revmap_data.linear.size = revmap_arg;
416                 smp_wmb();
417                 host->revmap_data.linear.revmap = rmap;
418                 break;
419         default:
420                 break;
421         }
422
423         pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
424
425         return host;
426 }
427
428 struct irq_host *irq_find_host(struct device_node *node)
429 {
430         struct irq_host *h, *found = NULL;
431         unsigned long flags;
432
433         /* We might want to match the legacy controller last since
434          * it might potentially be set to match all interrupts in
435          * the absence of a device node. This isn't a problem so far
436          * yet though...
437          */
438         spin_lock_irqsave(&irq_big_lock, flags);
439         list_for_each_entry(h, &irq_hosts, link)
440                 if (h->ops->match == NULL || h->ops->match(h, node)) {
441                         found = h;
442                         break;
443                 }
444         spin_unlock_irqrestore(&irq_big_lock, flags);
445         return found;
446 }
447 EXPORT_SYMBOL_GPL(irq_find_host);
448
449 void irq_set_default_host(struct irq_host *host)
450 {
451         pr_debug("irq: Default host set to @0x%p\n", host);
452
453         irq_default_host = host;
454 }
455
456 void irq_set_virq_count(unsigned int count)
457 {
458         pr_debug("irq: Trying to set virq count to %d\n", count);
459
460         BUG_ON(count < NUM_ISA_INTERRUPTS);
461         if (count < NR_IRQS)
462                 irq_virq_count = count;
463 }
464
465 /* radix tree not lockless safe ! we use a brlock-type mecanism
466  * for now, until we can use a lockless radix tree
467  */
468 static void irq_radix_wrlock(unsigned long *flags)
469 {
470         unsigned int cpu, ok;
471
472         spin_lock_irqsave(&irq_big_lock, *flags);
473         irq_radix_writer = 1;
474         smp_mb();
475         do {
476                 barrier();
477                 ok = 1;
478                 for_each_possible_cpu(cpu) {
479                         if (per_cpu(irq_radix_reader, cpu)) {
480                                 ok = 0;
481                                 break;
482                         }
483                 }
484                 if (!ok)
485                         cpu_relax();
486         } while(!ok);
487 }
488
489 static void irq_radix_wrunlock(unsigned long flags)
490 {
491         smp_wmb();
492         irq_radix_writer = 0;
493         spin_unlock_irqrestore(&irq_big_lock, flags);
494 }
495
496 static void irq_radix_rdlock(unsigned long *flags)
497 {
498         local_irq_save(*flags);
499         __get_cpu_var(irq_radix_reader) = 1;
500         smp_mb();
501         if (likely(irq_radix_writer == 0))
502                 return;
503         __get_cpu_var(irq_radix_reader) = 0;
504         smp_wmb();
505         spin_lock(&irq_big_lock);
506         __get_cpu_var(irq_radix_reader) = 1;
507         spin_unlock(&irq_big_lock);
508 }
509
510 static void irq_radix_rdunlock(unsigned long flags)
511 {
512         __get_cpu_var(irq_radix_reader) = 0;
513         local_irq_restore(flags);
514 }
515
516
517 unsigned int irq_create_mapping(struct irq_host *host,
518                                 irq_hw_number_t hwirq)
519 {
520         unsigned int virq, hint;
521
522         pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
523
524         /* Look for default host if nececssary */
525         if (host == NULL)
526                 host = irq_default_host;
527         if (host == NULL) {
528                 printk(KERN_WARNING "irq_create_mapping called for"
529                        " NULL host, hwirq=%lx\n", hwirq);
530                 WARN_ON(1);
531                 return NO_IRQ;
532         }
533         pr_debug("irq: -> using host @%p\n", host);
534
535         /* Check if mapping already exist, if it does, call
536          * host->ops->map() to update the flags
537          */
538         virq = irq_find_mapping(host, hwirq);
539         if (virq != IRQ_NONE) {
540                 pr_debug("irq: -> existing mapping on virq %d\n", virq);
541                 return virq;
542         }
543
544         /* Get a virtual interrupt number */
545         if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
546                 /* Handle legacy */
547                 virq = (unsigned int)hwirq;
548                 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
549                         return NO_IRQ;
550                 return virq;
551         } else {
552                 /* Allocate a virtual interrupt number */
553                 hint = hwirq % irq_virq_count;
554                 virq = irq_alloc_virt(host, 1, hint);
555                 if (virq == NO_IRQ) {
556                         pr_debug("irq: -> virq allocation failed\n");
557                         return NO_IRQ;
558                 }
559         }
560         pr_debug("irq: -> obtained virq %d\n", virq);
561
562         /* Clear IRQ_NOREQUEST flag */
563         get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
564
565         /* map it */
566         smp_wmb();
567         irq_map[virq].hwirq = hwirq;
568         smp_mb();
569         if (host->ops->map(host, virq, hwirq)) {
570                 pr_debug("irq: -> mapping failed, freeing\n");
571                 irq_free_virt(virq, 1);
572                 return NO_IRQ;
573         }
574         return virq;
575 }
576 EXPORT_SYMBOL_GPL(irq_create_mapping);
577
578 extern unsigned int irq_create_of_mapping(struct device_node *controller,
579                                           u32 *intspec, unsigned int intsize)
580 {
581         struct irq_host *host;
582         irq_hw_number_t hwirq;
583         unsigned int type = IRQ_TYPE_NONE;
584         unsigned int virq;
585
586         if (controller == NULL)
587                 host = irq_default_host;
588         else
589                 host = irq_find_host(controller);
590         if (host == NULL) {
591                 printk(KERN_WARNING "irq: no irq host found for %s !\n",
592                        controller->full_name);
593                 return NO_IRQ;
594         }
595
596         /* If host has no translation, then we assume interrupt line */
597         if (host->ops->xlate == NULL)
598                 hwirq = intspec[0];
599         else {
600                 if (host->ops->xlate(host, controller, intspec, intsize,
601                                      &hwirq, &type))
602                         return NO_IRQ;
603         }
604
605         /* Create mapping */
606         virq = irq_create_mapping(host, hwirq);
607         if (virq == NO_IRQ)
608                 return virq;
609
610         /* Set type if specified and different than the current one */
611         if (type != IRQ_TYPE_NONE &&
612             type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
613                 set_irq_type(virq, type);
614         return virq;
615 }
616 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
617
618 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
619 {
620         struct of_irq oirq;
621
622         if (of_irq_map_one(dev, index, &oirq))
623                 return NO_IRQ;
624
625         return irq_create_of_mapping(oirq.controller, oirq.specifier,
626                                      oirq.size);
627 }
628 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
629
630 void irq_dispose_mapping(unsigned int virq)
631 {
632         struct irq_host *host = irq_map[virq].host;
633         irq_hw_number_t hwirq;
634         unsigned long flags;
635
636         WARN_ON (host == NULL);
637         if (host == NULL)
638                 return;
639
640         /* Never unmap legacy interrupts */
641         if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
642                 return;
643
644         /* remove chip and handler */
645         set_irq_chip_and_handler(virq, NULL, NULL);
646
647         /* Make sure it's completed */
648         synchronize_irq(virq);
649
650         /* Tell the PIC about it */
651         if (host->ops->unmap)
652                 host->ops->unmap(host, virq);
653         smp_mb();
654
655         /* Clear reverse map */
656         hwirq = irq_map[virq].hwirq;
657         switch(host->revmap_type) {
658         case IRQ_HOST_MAP_LINEAR:
659                 if (hwirq < host->revmap_data.linear.size)
660                         host->revmap_data.linear.revmap[hwirq] = IRQ_NONE;
661                 break;
662         case IRQ_HOST_MAP_TREE:
663                 /* Check if radix tree allocated yet */
664                 if (host->revmap_data.tree.gfp_mask == 0)
665                         break;
666                 irq_radix_wrlock(&flags);
667                 radix_tree_delete(&host->revmap_data.tree, hwirq);
668                 irq_radix_wrunlock(flags);
669                 break;
670         }
671
672         /* Destroy map */
673         smp_mb();
674         irq_map[virq].hwirq = host->inval_irq;
675
676         /* Set some flags */
677         get_irq_desc(virq)->status |= IRQ_NOREQUEST;
678
679         /* Free it */
680         irq_free_virt(virq, 1);
681 }
682 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
683
684 unsigned int irq_find_mapping(struct irq_host *host,
685                               irq_hw_number_t hwirq)
686 {
687         unsigned int i;
688         unsigned int hint = hwirq % irq_virq_count;
689
690         /* Look for default host if nececssary */
691         if (host == NULL)
692                 host = irq_default_host;
693         if (host == NULL)
694                 return NO_IRQ;
695
696         /* legacy -> bail early */
697         if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
698                 return hwirq;
699
700         /* Slow path does a linear search of the map */
701         if (hint < NUM_ISA_INTERRUPTS)
702                 hint = NUM_ISA_INTERRUPTS;
703         i = hint;
704         do  {
705                 if (irq_map[i].host == host &&
706                     irq_map[i].hwirq == hwirq)
707                         return i;
708                 i++;
709                 if (i >= irq_virq_count)
710                         i = NUM_ISA_INTERRUPTS;
711         } while(i != hint);
712         return NO_IRQ;
713 }
714 EXPORT_SYMBOL_GPL(irq_find_mapping);
715
716
717 unsigned int irq_radix_revmap(struct irq_host *host,
718                               irq_hw_number_t hwirq)
719 {
720         struct radix_tree_root *tree;
721         struct irq_map_entry *ptr;
722         unsigned int virq;
723         unsigned long flags;
724
725         WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
726
727         /* Check if the radix tree exist yet. We test the value of
728          * the gfp_mask for that. Sneaky but saves another int in the
729          * structure. If not, we fallback to slow mode
730          */
731         tree = &host->revmap_data.tree;
732         if (tree->gfp_mask == 0)
733                 return irq_find_mapping(host, hwirq);
734
735         /* Now try to resolve */
736         irq_radix_rdlock(&flags);
737         ptr = radix_tree_lookup(tree, hwirq);
738         irq_radix_rdunlock(flags);
739
740         /* Found it, return */
741         if (ptr) {
742                 virq = ptr - irq_map;
743                 return virq;
744         }
745
746         /* If not there, try to insert it */
747         virq = irq_find_mapping(host, hwirq);
748         if (virq != NO_IRQ) {
749                 irq_radix_wrlock(&flags);
750                 radix_tree_insert(tree, hwirq, &irq_map[virq]);
751                 irq_radix_wrunlock(flags);
752         }
753         return virq;
754 }
755
756 unsigned int irq_linear_revmap(struct irq_host *host,
757                                irq_hw_number_t hwirq)
758 {
759         unsigned int *revmap;
760
761         WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
762
763         /* Check revmap bounds */
764         if (unlikely(hwirq >= host->revmap_data.linear.size))
765                 return irq_find_mapping(host, hwirq);
766
767         /* Check if revmap was allocated */
768         revmap = host->revmap_data.linear.revmap;
769         if (unlikely(revmap == NULL))
770                 return irq_find_mapping(host, hwirq);
771
772         /* Fill up revmap with slow path if no mapping found */
773         if (unlikely(revmap[hwirq] == NO_IRQ))
774                 revmap[hwirq] = irq_find_mapping(host, hwirq);
775
776         return revmap[hwirq];
777 }
778
779 unsigned int irq_alloc_virt(struct irq_host *host,
780                             unsigned int count,
781                             unsigned int hint)
782 {
783         unsigned long flags;
784         unsigned int i, j, found = NO_IRQ;
785         unsigned int limit = irq_virq_count - count;
786
787         if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
788                 return NO_IRQ;
789
790         spin_lock_irqsave(&irq_big_lock, flags);
791
792         /* Use hint for 1 interrupt if any */
793         if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
794             hint < irq_virq_count && irq_map[hint].host == NULL) {
795                 found = hint;
796                 goto hint_found;
797         }
798
799         /* Look for count consecutive numbers in the allocatable
800          * (non-legacy) space
801          */
802         for (i = NUM_ISA_INTERRUPTS; i <= limit; ) {
803                 for (j = i; j < (i + count); j++)
804                         if (irq_map[j].host != NULL) {
805                                 i = j + 1;
806                                 continue;
807                         }
808                 found = i;
809                 break;
810         }
811         if (found == NO_IRQ) {
812                 spin_unlock_irqrestore(&irq_big_lock, flags);
813                 return NO_IRQ;
814         }
815  hint_found:
816         for (i = found; i < (found + count); i++) {
817                 irq_map[i].hwirq = host->inval_irq;
818                 smp_wmb();
819                 irq_map[i].host = host;
820         }
821         spin_unlock_irqrestore(&irq_big_lock, flags);
822         return found;
823 }
824
825 void irq_free_virt(unsigned int virq, unsigned int count)
826 {
827         unsigned long flags;
828         unsigned int i;
829
830         WARN_ON (virq < NUM_ISA_INTERRUPTS);
831         WARN_ON (count == 0 || (virq + count) > irq_virq_count);
832
833         spin_lock_irqsave(&irq_big_lock, flags);
834         for (i = virq; i < (virq + count); i++) {
835                 struct irq_host *host;
836
837                 if (i < NUM_ISA_INTERRUPTS ||
838                     (virq + count) > irq_virq_count)
839                         continue;
840
841                 host = irq_map[i].host;
842                 irq_map[i].hwirq = host->inval_irq;
843                 smp_wmb();
844                 irq_map[i].host = NULL;
845         }
846         spin_unlock_irqrestore(&irq_big_lock, flags);
847 }
848
849 void irq_early_init(void)
850 {
851         unsigned int i;
852
853         for (i = 0; i < NR_IRQS; i++)
854                 get_irq_desc(i)->status |= IRQ_NOREQUEST;
855 }
856
857 /* We need to create the radix trees late */
858 static int irq_late_init(void)
859 {
860         struct irq_host *h;
861         unsigned long flags;
862
863         irq_radix_wrlock(&flags);
864         list_for_each_entry(h, &irq_hosts, link) {
865                 if (h->revmap_type == IRQ_HOST_MAP_TREE)
866                         INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
867         }
868         irq_radix_wrunlock(flags);
869
870         return 0;
871 }
872 arch_initcall(irq_late_init);
873
874 #endif /* CONFIG_PPC_MERGE */
875
876 #ifdef CONFIG_PCI_MSI
877 int pci_enable_msi(struct pci_dev * pdev)
878 {
879         if (ppc_md.enable_msi)
880                 return ppc_md.enable_msi(pdev);
881         else
882                 return -1;
883 }
884
885 void pci_disable_msi(struct pci_dev * pdev)
886 {
887         if (ppc_md.disable_msi)
888                 ppc_md.disable_msi(pdev);
889 }
890
891 void pci_scan_msi_device(struct pci_dev *dev) {}
892 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) {return -1;}
893 void pci_disable_msix(struct pci_dev *dev) {}
894 void msi_remove_pci_irq_vectors(struct pci_dev *dev) {}
895 void disable_msi_mode(struct pci_dev *dev, int pos, int type) {}
896 void pci_no_msi(void) {}
897
898 #endif
899
900 #ifdef CONFIG_PPC64
901 static int __init setup_noirqdistrib(char *str)
902 {
903         distribute_irqs = 0;
904         return 1;
905 }
906
907 __setup("noirqdistrib", setup_noirqdistrib);
908 #endif /* CONFIG_PPC64 */