fedora core 6 1.2949 + vserver 2.2.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/pci.h>
56 #include <linux/vs_context.h>
57
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
60 #include <asm/io.h>
61 #include <asm/pgtable.h>
62 #include <asm/irq.h>
63 #include <asm/cache.h>
64 #include <asm/prom.h>
65 #include <asm/ptrace.h>
66 #include <asm/machdep.h>
67 #include <asm/udbg.h>
68 #ifdef CONFIG_PPC64
69 #include <asm/paca.h>
70 #include <asm/firmware.h>
71 #endif
72
73 int __irq_offset_value;
74 static int ppc_spurious_interrupts;
75
76 #ifdef CONFIG_PPC32
77 EXPORT_SYMBOL(__irq_offset_value);
78 atomic_t ppc_n_lost_interrupts;
79
80 #ifndef CONFIG_PPC_MERGE
81 #define NR_MASK_WORDS   ((NR_IRQS + 31) / 32)
82 unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
83 #endif
84
85 #ifdef CONFIG_TAU_INT
86 extern int tau_initialized;
87 extern int tau_interrupts(int);
88 #endif
89 #endif /* CONFIG_PPC32 */
90
91 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
92 extern atomic_t ipi_recv;
93 extern atomic_t ipi_sent;
94 #endif
95
96 #ifdef CONFIG_PPC64
97 EXPORT_SYMBOL(irq_desc);
98
99 int distribute_irqs = 1;
100
101 static inline unsigned long get_hard_enabled(void)
102 {
103         unsigned long enabled;
104
105         __asm__ __volatile__("lbz %0,%1(13)"
106         : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
107
108         return enabled;
109 }
110
111 static inline void set_soft_enabled(unsigned long enable)
112 {
113         __asm__ __volatile__("stb %0,%1(13)"
114         : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
115 }
116
117 void local_irq_restore(unsigned long en)
118 {
119         /*
120          * get_paca()->soft_enabled = en;
121          * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
122          * That was allowed before, and in such a case we do need to take care
123          * that gcc will set soft_enabled directly via r13, not choose to use
124          * an intermediate register, lest we're preempted to a different cpu.
125          */
126         set_soft_enabled(en);
127         if (!en)
128                 return;
129
130         if (firmware_has_feature(FW_FEATURE_ISERIES)) {
131                 /*
132                  * Do we need to disable preemption here?  Not really: in the
133                  * unlikely event that we're preempted to a different cpu in
134                  * between getting r13, loading its lppaca_ptr, and loading
135                  * its any_int, we might call iseries_handle_interrupts without
136                  * an interrupt pending on the new cpu, but that's no disaster,
137                  * is it?  And the business of preempting us off the old cpu
138                  * would itself involve a local_irq_restore which handles the
139                  * interrupt to that cpu.
140                  *
141                  * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
142                  * to avoid any preemption checking added into get_paca().
143                  */
144                 if (local_paca->lppaca_ptr->int_dword.any_int)
145                         iseries_handle_interrupts();
146                 return;
147         }
148
149         /*
150          * if (get_paca()->hard_enabled) return;
151          * But again we need to take care that gcc gets hard_enabled directly
152          * via r13, not choose to use an intermediate register, lest we're
153          * preempted to a different cpu in between the two instructions.
154          */
155         if (get_hard_enabled())
156                 return;
157
158         /*
159          * Need to hard-enable interrupts here.  Since currently disabled,
160          * no need to take further asm precautions against preemption; but
161          * use local_paca instead of get_paca() to avoid preemption checking.
162          */
163         local_paca->hard_enabled = en;
164         if ((int)mfspr(SPRN_DEC) < 0)
165                 mtspr(SPRN_DEC, 1);
166         hard_irq_enable();
167 }
168 #endif /* CONFIG_PPC64 */
169
170 int show_interrupts(struct seq_file *p, void *v)
171 {
172         int i = *(loff_t *)v, j;
173         struct irqaction *action;
174         irq_desc_t *desc;
175         unsigned long flags;
176
177         if (i == 0) {
178                 seq_puts(p, "           ");
179                 for_each_online_cpu(j)
180                         seq_printf(p, "CPU%d       ", j);
181                 seq_putc(p, '\n');
182         }
183
184         if (i < NR_IRQS) {
185                 desc = get_irq_desc(i);
186                 spin_lock_irqsave(&desc->lock, flags);
187                 action = desc->action;
188                 if (!action || !action->handler)
189                         goto skip;
190                 seq_printf(p, "%3d: ", i);
191 #ifdef CONFIG_SMP
192                 for_each_online_cpu(j)
193                         seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
194 #else
195                 seq_printf(p, "%10u ", kstat_irqs(i));
196 #endif /* CONFIG_SMP */
197                 if (desc->chip)
198                         seq_printf(p, " %s ", desc->chip->typename);
199                 else
200                         seq_puts(p, "  None      ");
201                 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
202                 seq_printf(p, "    %s", action->name);
203                 for (action = action->next; action; action = action->next)
204                         seq_printf(p, ", %s", action->name);
205                 seq_putc(p, '\n');
206 skip:
207                 spin_unlock_irqrestore(&desc->lock, flags);
208         } else if (i == NR_IRQS) {
209 #ifdef CONFIG_PPC32
210 #ifdef CONFIG_TAU_INT
211                 if (tau_initialized){
212                         seq_puts(p, "TAU: ");
213                         for_each_online_cpu(j)
214                                 seq_printf(p, "%10u ", tau_interrupts(j));
215                         seq_puts(p, "  PowerPC             Thermal Assist (cpu temp)\n");
216                 }
217 #endif
218 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
219                 /* should this be per processor send/receive? */
220                 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
221                                 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
222 #endif
223 #endif /* CONFIG_PPC32 */
224                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
225         }
226         return 0;
227 }
228
229 #ifdef CONFIG_HOTPLUG_CPU
230 void fixup_irqs(cpumask_t map)
231 {
232         unsigned int irq;
233         static int warned;
234
235         for_each_irq(irq) {
236                 cpumask_t mask;
237
238                 if (irq_desc[irq].status & IRQ_PER_CPU)
239                         continue;
240
241                 cpus_and(mask, irq_desc[irq].affinity, map);
242                 if (any_online_cpu(mask) == NR_CPUS) {
243                         printk("Breaking affinity for irq %i\n", irq);
244                         mask = map;
245                 }
246                 if (irq_desc[irq].chip->set_affinity)
247                         irq_desc[irq].chip->set_affinity(irq, mask);
248                 else if (irq_desc[irq].action && !(warned++))
249                         printk("Cannot set affinity for irq %i\n", irq);
250         }
251
252         local_irq_enable();
253         mdelay(1);
254         local_irq_disable();
255 }
256 #endif
257
258 void do_IRQ(struct pt_regs *regs)
259 {
260         struct pt_regs *old_regs = set_irq_regs(regs);
261         unsigned int irq;
262 #ifdef CONFIG_IRQSTACKS
263         struct thread_info *curtp, *irqtp;
264 #endif
265
266         irq_enter();
267
268 #ifdef CONFIG_DEBUG_STACKOVERFLOW
269         /* Debugging check for stack overflow: is there less than 2KB free? */
270         {
271                 long sp;
272
273                 sp = __get_SP() & (THREAD_SIZE-1);
274
275                 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
276                         printk("do_IRQ: stack overflow: %ld\n",
277                                 sp - sizeof(struct thread_info));
278                         dump_stack();
279                 }
280         }
281 #endif
282
283         /*
284          * Every platform is required to implement ppc_md.get_irq.
285          * This function will either return an irq number or -1 to
286          * indicate there are no more pending.
287          * The value -2 is for buggy hardware and means that this IRQ
288          * has already been handled. -- Tom
289          */
290         irq = ppc_md.get_irq();
291
292         if (irq != NO_IRQ && irq != NO_IRQ_IGNORE) {
293 #ifdef CONFIG_IRQSTACKS
294                 /* Switch to the irq stack to handle this */
295                 curtp = current_thread_info();
296                 irqtp = hardirq_ctx[smp_processor_id()];
297                 if (curtp != irqtp) {
298                         struct irq_desc *desc = irq_desc + irq;
299                         void *handler = desc->handle_irq;
300                         if (handler == NULL)
301                                 handler = &__do_IRQ;
302                         irqtp->task = curtp->task;
303                         irqtp->flags = 0;
304                         call_handle_irq(irq, desc, irqtp, handler);
305                         irqtp->task = NULL;
306                         if (irqtp->flags)
307                                 set_bits(irqtp->flags, &curtp->flags);
308                 } else
309 #endif
310                         generic_handle_irq(irq);
311         } else if (irq != NO_IRQ_IGNORE)
312                 /* That's not SMP safe ... but who cares ? */
313                 ppc_spurious_interrupts++;
314
315         irq_exit();
316         set_irq_regs(old_regs);
317
318 #ifdef CONFIG_PPC_ISERIES
319         if (firmware_has_feature(FW_FEATURE_ISERIES) &&
320                         get_lppaca()->int_dword.fields.decr_int) {
321                 get_lppaca()->int_dword.fields.decr_int = 0;
322                 /* Signal a fake decrementer interrupt */
323                 timer_interrupt(regs);
324         }
325 #endif
326 }
327
328 void __init init_IRQ(void)
329 {
330         ppc_md.init_IRQ();
331 #ifdef CONFIG_PPC64
332         irq_ctx_init();
333 #endif
334 }
335
336
337 #ifdef CONFIG_IRQSTACKS
338 struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
339 struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
340
341 void irq_ctx_init(void)
342 {
343         struct thread_info *tp;
344         int i;
345
346         for_each_possible_cpu(i) {
347                 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
348                 tp = softirq_ctx[i];
349                 tp->cpu = i;
350                 tp->preempt_count = SOFTIRQ_OFFSET;
351
352                 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
353                 tp = hardirq_ctx[i];
354                 tp->cpu = i;
355                 tp->preempt_count = HARDIRQ_OFFSET;
356         }
357 }
358
359 static inline void do_softirq_onstack(void)
360 {
361         struct thread_info *curtp, *irqtp;
362
363         curtp = current_thread_info();
364         irqtp = softirq_ctx[smp_processor_id()];
365         irqtp->task = curtp->task;
366         call_do_softirq(irqtp);
367         irqtp->task = NULL;
368 }
369
370 #else
371 #define do_softirq_onstack()    __do_softirq()
372 #endif /* CONFIG_IRQSTACKS */
373
374 void do_softirq(void)
375 {
376         unsigned long flags;
377
378         if (in_interrupt())
379                 return;
380
381         local_irq_save(flags);
382
383         if (local_softirq_pending())
384                 do_softirq_onstack();
385
386         local_irq_restore(flags);
387 }
388 EXPORT_SYMBOL(do_softirq);
389
390
391 /*
392  * IRQ controller and virtual interrupts
393  */
394
395 #ifdef CONFIG_PPC_MERGE
396
397 static LIST_HEAD(irq_hosts);
398 static spinlock_t irq_big_lock = SPIN_LOCK_UNLOCKED;
399 static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
400 static unsigned int irq_radix_writer;
401 struct irq_map_entry irq_map[NR_IRQS];
402 static unsigned int irq_virq_count = NR_IRQS;
403 static struct irq_host *irq_default_host;
404
405 struct irq_host *irq_alloc_host(unsigned int revmap_type,
406                                 unsigned int revmap_arg,
407                                 struct irq_host_ops *ops,
408                                 irq_hw_number_t inval_irq)
409 {
410         struct irq_host *host;
411         unsigned int size = sizeof(struct irq_host);
412         unsigned int i;
413         unsigned int *rmap;
414         unsigned long flags;
415
416         /* Allocate structure and revmap table if using linear mapping */
417         if (revmap_type == IRQ_HOST_MAP_LINEAR)
418                 size += revmap_arg * sizeof(unsigned int);
419         if (mem_init_done)
420                 host = kzalloc(size, GFP_KERNEL);
421         else {
422                 host = alloc_bootmem(size);
423                 if (host)
424                         memset(host, 0, size);
425         }
426         if (host == NULL)
427                 return NULL;
428
429         /* Fill structure */
430         host->revmap_type = revmap_type;
431         host->inval_irq = inval_irq;
432         host->ops = ops;
433
434         spin_lock_irqsave(&irq_big_lock, flags);
435
436         /* If it's a legacy controller, check for duplicates and
437          * mark it as allocated (we use irq 0 host pointer for that
438          */
439         if (revmap_type == IRQ_HOST_MAP_LEGACY) {
440                 if (irq_map[0].host != NULL) {
441                         spin_unlock_irqrestore(&irq_big_lock, flags);
442                         /* If we are early boot, we can't free the structure,
443                          * too bad...
444                          * this will be fixed once slab is made available early
445                          * instead of the current cruft
446                          */
447                         if (mem_init_done)
448                                 kfree(host);
449                         return NULL;
450                 }
451                 irq_map[0].host = host;
452         }
453
454         list_add(&host->link, &irq_hosts);
455         spin_unlock_irqrestore(&irq_big_lock, flags);
456
457         /* Additional setups per revmap type */
458         switch(revmap_type) {
459         case IRQ_HOST_MAP_LEGACY:
460                 /* 0 is always the invalid number for legacy */
461                 host->inval_irq = 0;
462                 /* setup us as the host for all legacy interrupts */
463                 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
464                         irq_map[i].hwirq = 0;
465                         smp_wmb();
466                         irq_map[i].host = host;
467                         smp_wmb();
468
469                         /* Clear norequest flags */
470                         get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
471
472                         /* Legacy flags are left to default at this point,
473                          * one can then use irq_create_mapping() to
474                          * explicitely change them
475                          */
476                         ops->map(host, i, i);
477                 }
478                 break;
479         case IRQ_HOST_MAP_LINEAR:
480                 rmap = (unsigned int *)(host + 1);
481                 for (i = 0; i < revmap_arg; i++)
482                         rmap[i] = IRQ_NONE;
483                 host->revmap_data.linear.size = revmap_arg;
484                 smp_wmb();
485                 host->revmap_data.linear.revmap = rmap;
486                 break;
487         default:
488                 break;
489         }
490
491         pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
492
493         return host;
494 }
495
496 struct irq_host *irq_find_host(struct device_node *node)
497 {
498         struct irq_host *h, *found = NULL;
499         unsigned long flags;
500
501         /* We might want to match the legacy controller last since
502          * it might potentially be set to match all interrupts in
503          * the absence of a device node. This isn't a problem so far
504          * yet though...
505          */
506         spin_lock_irqsave(&irq_big_lock, flags);
507         list_for_each_entry(h, &irq_hosts, link)
508                 if (h->ops->match == NULL || h->ops->match(h, node)) {
509                         found = h;
510                         break;
511                 }
512         spin_unlock_irqrestore(&irq_big_lock, flags);
513         return found;
514 }
515 EXPORT_SYMBOL_GPL(irq_find_host);
516
517 void irq_set_default_host(struct irq_host *host)
518 {
519         pr_debug("irq: Default host set to @0x%p\n", host);
520
521         irq_default_host = host;
522 }
523
524 void irq_set_virq_count(unsigned int count)
525 {
526         pr_debug("irq: Trying to set virq count to %d\n", count);
527
528         BUG_ON(count < NUM_ISA_INTERRUPTS);
529         if (count < NR_IRQS)
530                 irq_virq_count = count;
531 }
532
533 /* radix tree not lockless safe ! we use a brlock-type mecanism
534  * for now, until we can use a lockless radix tree
535  */
536 static void irq_radix_wrlock(unsigned long *flags)
537 {
538         unsigned int cpu, ok;
539
540         spin_lock_irqsave(&irq_big_lock, *flags);
541         irq_radix_writer = 1;
542         smp_mb();
543         do {
544                 barrier();
545                 ok = 1;
546                 for_each_possible_cpu(cpu) {
547                         if (per_cpu(irq_radix_reader, cpu)) {
548                                 ok = 0;
549                                 break;
550                         }
551                 }
552                 if (!ok)
553                         cpu_relax();
554         } while(!ok);
555 }
556
557 static void irq_radix_wrunlock(unsigned long flags)
558 {
559         smp_wmb();
560         irq_radix_writer = 0;
561         spin_unlock_irqrestore(&irq_big_lock, flags);
562 }
563
564 static void irq_radix_rdlock(unsigned long *flags)
565 {
566         local_irq_save(*flags);
567         __get_cpu_var(irq_radix_reader) = 1;
568         smp_mb();
569         if (likely(irq_radix_writer == 0))
570                 return;
571         __get_cpu_var(irq_radix_reader) = 0;
572         smp_wmb();
573         spin_lock(&irq_big_lock);
574         __get_cpu_var(irq_radix_reader) = 1;
575         spin_unlock(&irq_big_lock);
576 }
577
578 static void irq_radix_rdunlock(unsigned long flags)
579 {
580         __get_cpu_var(irq_radix_reader) = 0;
581         local_irq_restore(flags);
582 }
583
584
585 unsigned int irq_create_mapping(struct irq_host *host,
586                                 irq_hw_number_t hwirq)
587 {
588         unsigned int virq, hint;
589
590         pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
591
592         /* Look for default host if nececssary */
593         if (host == NULL)
594                 host = irq_default_host;
595         if (host == NULL) {
596                 printk(KERN_WARNING "irq_create_mapping called for"
597                        " NULL host, hwirq=%lx\n", hwirq);
598                 WARN_ON(1);
599                 return NO_IRQ;
600         }
601         pr_debug("irq: -> using host @%p\n", host);
602
603         /* Check if mapping already exist, if it does, call
604          * host->ops->map() to update the flags
605          */
606         virq = irq_find_mapping(host, hwirq);
607         if (virq != IRQ_NONE) {
608                 pr_debug("irq: -> existing mapping on virq %d\n", virq);
609                 return virq;
610         }
611
612         /* Get a virtual interrupt number */
613         if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
614                 /* Handle legacy */
615                 virq = (unsigned int)hwirq;
616                 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
617                         return NO_IRQ;
618                 return virq;
619         } else {
620                 /* Allocate a virtual interrupt number */
621                 hint = hwirq % irq_virq_count;
622                 virq = irq_alloc_virt(host, 1, hint);
623                 if (virq == NO_IRQ) {
624                         pr_debug("irq: -> virq allocation failed\n");
625                         return NO_IRQ;
626                 }
627         }
628         pr_debug("irq: -> obtained virq %d\n", virq);
629
630         /* Clear IRQ_NOREQUEST flag */
631         get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
632
633         /* map it */
634         smp_wmb();
635         irq_map[virq].hwirq = hwirq;
636         smp_mb();
637         if (host->ops->map(host, virq, hwirq)) {
638                 pr_debug("irq: -> mapping failed, freeing\n");
639                 irq_free_virt(virq, 1);
640                 return NO_IRQ;
641         }
642         return virq;
643 }
644 EXPORT_SYMBOL_GPL(irq_create_mapping);
645
646 unsigned int irq_create_of_mapping(struct device_node *controller,
647                                    u32 *intspec, unsigned int intsize)
648 {
649         struct irq_host *host;
650         irq_hw_number_t hwirq;
651         unsigned int type = IRQ_TYPE_NONE;
652         unsigned int virq;
653
654         if (controller == NULL)
655                 host = irq_default_host;
656         else
657                 host = irq_find_host(controller);
658         if (host == NULL) {
659                 printk(KERN_WARNING "irq: no irq host found for %s !\n",
660                        controller->full_name);
661                 return NO_IRQ;
662         }
663
664         /* If host has no translation, then we assume interrupt line */
665         if (host->ops->xlate == NULL)
666                 hwirq = intspec[0];
667         else {
668                 if (host->ops->xlate(host, controller, intspec, intsize,
669                                      &hwirq, &type))
670                         return NO_IRQ;
671         }
672
673         /* Create mapping */
674         virq = irq_create_mapping(host, hwirq);
675         if (virq == NO_IRQ)
676                 return virq;
677
678         /* Set type if specified and different than the current one */
679         if (type != IRQ_TYPE_NONE &&
680             type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
681                 set_irq_type(virq, type);
682         return virq;
683 }
684 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
685
686 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
687 {
688         struct of_irq oirq;
689
690         if (of_irq_map_one(dev, index, &oirq))
691                 return NO_IRQ;
692
693         return irq_create_of_mapping(oirq.controller, oirq.specifier,
694                                      oirq.size);
695 }
696 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
697
698 void irq_dispose_mapping(unsigned int virq)
699 {
700         struct irq_host *host;
701         irq_hw_number_t hwirq;
702         unsigned long flags;
703
704         if (virq == NO_IRQ)
705                 return;
706
707         host = irq_map[virq].host;
708         WARN_ON (host == NULL);
709         if (host == NULL)
710                 return;
711
712         /* Never unmap legacy interrupts */
713         if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
714                 return;
715
716         /* remove chip and handler */
717         set_irq_chip_and_handler(virq, NULL, NULL);
718
719         /* Make sure it's completed */
720         synchronize_irq(virq);
721
722         /* Tell the PIC about it */
723         if (host->ops->unmap)
724                 host->ops->unmap(host, virq);
725         smp_mb();
726
727         /* Clear reverse map */
728         hwirq = irq_map[virq].hwirq;
729         switch(host->revmap_type) {
730         case IRQ_HOST_MAP_LINEAR:
731                 if (hwirq < host->revmap_data.linear.size)
732                         host->revmap_data.linear.revmap[hwirq] = IRQ_NONE;
733                 break;
734         case IRQ_HOST_MAP_TREE:
735                 /* Check if radix tree allocated yet */
736                 if (host->revmap_data.tree.gfp_mask == 0)
737                         break;
738                 irq_radix_wrlock(&flags);
739                 radix_tree_delete(&host->revmap_data.tree, hwirq);
740                 irq_radix_wrunlock(flags);
741                 break;
742         }
743
744         /* Destroy map */
745         smp_mb();
746         irq_map[virq].hwirq = host->inval_irq;
747
748         /* Set some flags */
749         get_irq_desc(virq)->status |= IRQ_NOREQUEST;
750
751         /* Free it */
752         irq_free_virt(virq, 1);
753 }
754 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
755
756 unsigned int irq_find_mapping(struct irq_host *host,
757                               irq_hw_number_t hwirq)
758 {
759         unsigned int i;
760         unsigned int hint = hwirq % irq_virq_count;
761
762         /* Look for default host if nececssary */
763         if (host == NULL)
764                 host = irq_default_host;
765         if (host == NULL)
766                 return NO_IRQ;
767
768         /* legacy -> bail early */
769         if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
770                 return hwirq;
771
772         /* Slow path does a linear search of the map */
773         if (hint < NUM_ISA_INTERRUPTS)
774                 hint = NUM_ISA_INTERRUPTS;
775         i = hint;
776         do  {
777                 if (irq_map[i].host == host &&
778                     irq_map[i].hwirq == hwirq)
779                         return i;
780                 i++;
781                 if (i >= irq_virq_count)
782                         i = NUM_ISA_INTERRUPTS;
783         } while(i != hint);
784         return NO_IRQ;
785 }
786 EXPORT_SYMBOL_GPL(irq_find_mapping);
787
788
789 unsigned int irq_radix_revmap(struct irq_host *host,
790                               irq_hw_number_t hwirq)
791 {
792         struct radix_tree_root *tree;
793         struct irq_map_entry *ptr;
794         unsigned int virq;
795         unsigned long flags;
796
797         WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
798
799         /* Check if the radix tree exist yet. We test the value of
800          * the gfp_mask for that. Sneaky but saves another int in the
801          * structure. If not, we fallback to slow mode
802          */
803         tree = &host->revmap_data.tree;
804         if (tree->gfp_mask == 0)
805                 return irq_find_mapping(host, hwirq);
806
807         /* Now try to resolve */
808         irq_radix_rdlock(&flags);
809         ptr = radix_tree_lookup(tree, hwirq);
810         irq_radix_rdunlock(flags);
811
812         /* Found it, return */
813         if (ptr) {
814                 virq = ptr - irq_map;
815                 return virq;
816         }
817
818         /* If not there, try to insert it */
819         virq = irq_find_mapping(host, hwirq);
820         if (virq != NO_IRQ) {
821                 irq_radix_wrlock(&flags);
822                 radix_tree_insert(tree, hwirq, &irq_map[virq]);
823                 irq_radix_wrunlock(flags);
824         }
825         return virq;
826 }
827
828 unsigned int irq_linear_revmap(struct irq_host *host,
829                                irq_hw_number_t hwirq)
830 {
831         unsigned int *revmap;
832
833         WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
834
835         /* Check revmap bounds */
836         if (unlikely(hwirq >= host->revmap_data.linear.size))
837                 return irq_find_mapping(host, hwirq);
838
839         /* Check if revmap was allocated */
840         revmap = host->revmap_data.linear.revmap;
841         if (unlikely(revmap == NULL))
842                 return irq_find_mapping(host, hwirq);
843
844         /* Fill up revmap with slow path if no mapping found */
845         if (unlikely(revmap[hwirq] == NO_IRQ))
846                 revmap[hwirq] = irq_find_mapping(host, hwirq);
847
848         return revmap[hwirq];
849 }
850
851 unsigned int irq_alloc_virt(struct irq_host *host,
852                             unsigned int count,
853                             unsigned int hint)
854 {
855         unsigned long flags;
856         unsigned int i, j, found = NO_IRQ;
857
858         if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
859                 return NO_IRQ;
860
861         spin_lock_irqsave(&irq_big_lock, flags);
862
863         /* Use hint for 1 interrupt if any */
864         if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
865             hint < irq_virq_count && irq_map[hint].host == NULL) {
866                 found = hint;
867                 goto hint_found;
868         }
869
870         /* Look for count consecutive numbers in the allocatable
871          * (non-legacy) space
872          */
873         for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
874                 if (irq_map[i].host != NULL)
875                         j = 0;
876                 else
877                         j++;
878
879                 if (j == count) {
880                         found = i - count + 1;
881                         break;
882                 }
883         }
884         if (found == NO_IRQ) {
885                 spin_unlock_irqrestore(&irq_big_lock, flags);
886                 return NO_IRQ;
887         }
888  hint_found:
889         for (i = found; i < (found + count); i++) {
890                 irq_map[i].hwirq = host->inval_irq;
891                 smp_wmb();
892                 irq_map[i].host = host;
893         }
894         spin_unlock_irqrestore(&irq_big_lock, flags);
895         return found;
896 }
897
898 void irq_free_virt(unsigned int virq, unsigned int count)
899 {
900         unsigned long flags;
901         unsigned int i;
902
903         WARN_ON (virq < NUM_ISA_INTERRUPTS);
904         WARN_ON (count == 0 || (virq + count) > irq_virq_count);
905
906         spin_lock_irqsave(&irq_big_lock, flags);
907         for (i = virq; i < (virq + count); i++) {
908                 struct irq_host *host;
909
910                 if (i < NUM_ISA_INTERRUPTS ||
911                     (virq + count) > irq_virq_count)
912                         continue;
913
914                 host = irq_map[i].host;
915                 irq_map[i].hwirq = host->inval_irq;
916                 smp_wmb();
917                 irq_map[i].host = NULL;
918         }
919         spin_unlock_irqrestore(&irq_big_lock, flags);
920 }
921
922 void irq_early_init(void)
923 {
924         unsigned int i;
925
926         for (i = 0; i < NR_IRQS; i++)
927                 get_irq_desc(i)->status |= IRQ_NOREQUEST;
928 }
929
930 /* We need to create the radix trees late */
931 static int irq_late_init(void)
932 {
933         struct irq_host *h;
934         unsigned long flags;
935
936         irq_radix_wrlock(&flags);
937         list_for_each_entry(h, &irq_hosts, link) {
938                 if (h->revmap_type == IRQ_HOST_MAP_TREE)
939                         INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
940         }
941         irq_radix_wrunlock(flags);
942
943         return 0;
944 }
945 arch_initcall(irq_late_init);
946
947 #endif /* CONFIG_PPC_MERGE */
948
949 #ifdef CONFIG_PCI_MSI
950 int pci_enable_msi(struct pci_dev * pdev)
951 {
952         if (ppc_md.enable_msi)
953                 return ppc_md.enable_msi(pdev);
954         else
955                 return -1;
956 }
957 EXPORT_SYMBOL(pci_enable_msi);
958
959 void pci_disable_msi(struct pci_dev * pdev)
960 {
961         if (ppc_md.disable_msi)
962                 ppc_md.disable_msi(pdev);
963 }
964 EXPORT_SYMBOL(pci_disable_msi);
965
966 void pci_scan_msi_device(struct pci_dev *dev) {}
967 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) {return -1;}
968 void pci_disable_msix(struct pci_dev *dev) {}
969 void msi_remove_pci_irq_vectors(struct pci_dev *dev) {}
970 void disable_msi_mode(struct pci_dev *dev, int pos, int type) {}
971 void pci_no_msi(void) {}
972 EXPORT_SYMBOL(pci_enable_msix);
973 EXPORT_SYMBOL(pci_disable_msix);
974
975 #endif
976
977 #ifdef CONFIG_PPC64
978 static int __init setup_noirqdistrib(char *str)
979 {
980         distribute_irqs = 0;
981         return 1;
982 }
983
984 __setup("noirqdistrib", setup_noirqdistrib);
985 #endif /* CONFIG_PPC64 */