VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / ppc64 / kernel / irq.c
1 /*
2  *  arch/ppc/kernel/irq.c
3  *
4  *  Derived from arch/i386/kernel/irq.c
5  *    Copyright (C) 1992 Linus Torvalds
6  *  Adapted from arch/i386 by Gary Thomas
7  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu)
9  *    Copyright (C) 1996 Cort Dougan
10  *  Adapted for Power Macintosh by Paul Mackerras
11  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
12  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  *
19  * This file contains the code used by various IRQ handling routines:
20  * asking for different IRQ's should be done through these routines
21  * instead of just grabbing them. Thus setups with different IRQ numbers
22  * shouldn't result in any weird surprises, and installing new handlers
23  * should be easier.
24  */
25
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/threads.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/timex.h>
35 #include <linux/config.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/pci.h>
39 #include <linux/delay.h>
40 #include <linux/irq.h>
41 #include <linux/proc_fs.h>
42 #include <linux/random.h>
43 #include <linux/kallsyms.h>
44
45 #include <asm/uaccess.h>
46 #include <asm/bitops.h>
47 #include <asm/system.h>
48 #include <asm/io.h>
49 #include <asm/pgtable.h>
50 #include <asm/irq.h>
51 #include <asm/cache.h>
52 #include <asm/prom.h>
53 #include <asm/ptrace.h>
54 #include <asm/iSeries/LparData.h>
55 #include <asm/machdep.h>
56 #include <asm/paca.h>
57
58 #ifdef CONFIG_SMP
59 extern void iSeries_smp_message_recv( struct pt_regs * );
60 #endif
61
62 static void register_irq_proc (unsigned int irq);
63
64 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
65         [0 ... NR_IRQS-1] = {
66                 .lock = SPIN_LOCK_UNLOCKED
67         }
68 };
69
70 int __irq_offset_value;
71 int ppc_spurious_interrupts;
72 unsigned long lpevent_count;
73
74 int
75 setup_irq(unsigned int irq, struct irqaction * new)
76 {
77         int shared = 0;
78         unsigned long flags;
79         struct irqaction *old, **p;
80         irq_desc_t *desc = get_irq_desc(irq);
81
82         /*
83          * Some drivers like serial.c use request_irq() heavily,
84          * so we have to be careful not to interfere with a
85          * running system.
86          */
87         if (new->flags & SA_SAMPLE_RANDOM) {
88                 /*
89                  * This function might sleep, we want to call it first,
90                  * outside of the atomic block.
91                  * Yes, this might clear the entropy pool if the wrong
92                  * driver is attempted to be loaded, without actually
93                  * installing a new handler, but is this really a problem,
94                  * only the sysadmin is able to do this.
95                  */
96                 rand_initialize_irq(irq);
97         }
98
99         /*
100          * The following block of code has to be executed atomically
101          */
102         spin_lock_irqsave(&desc->lock,flags);
103         p = &desc->action;
104         if ((old = *p) != NULL) {
105                 /* Can't share interrupts unless both agree to */
106                 if (!(old->flags & new->flags & SA_SHIRQ)) {
107                         spin_unlock_irqrestore(&desc->lock,flags);
108                         return -EBUSY;
109                 }
110
111                 /* add new interrupt at end of irq queue */
112                 do {
113                         p = &old->next;
114                         old = *p;
115                 } while (old);
116                 shared = 1;
117         }
118
119         *p = new;
120
121         if (!shared) {
122                 desc->depth = 0;
123                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING | IRQ_INPROGRESS);
124                 if (desc->handler && desc->handler->startup)
125                         desc->handler->startup(irq);
126                 unmask_irq(irq);
127         }
128         spin_unlock_irqrestore(&desc->lock,flags);
129
130         register_irq_proc(irq);
131         return 0;
132 }
133
134 #ifdef CONFIG_SMP
135
136 inline void synchronize_irq(unsigned int irq)
137 {
138         while (get_irq_desc(irq)->status & IRQ_INPROGRESS)
139                 cpu_relax();
140 }
141
142 EXPORT_SYMBOL(synchronize_irq);
143
144 #endif /* CONFIG_SMP */
145
146 int request_irq(unsigned int irq,
147         irqreturn_t (*handler)(int, void *, struct pt_regs *),
148         unsigned long irqflags, const char * devname, void *dev_id)
149 {
150         struct irqaction *action;
151         int retval;
152
153         if (irq >= NR_IRQS)
154                 return -EINVAL;
155         if (!handler)
156                 return -EINVAL;
157
158         action = (struct irqaction *)
159                 kmalloc(sizeof(struct irqaction), GFP_KERNEL);
160         if (!action) {
161                 printk(KERN_ERR "kmalloc() failed for irq %d !\n", irq);
162                 return -ENOMEM;
163         }
164
165         action->handler = handler;
166         action->flags = irqflags;
167         cpus_clear(action->mask);
168         action->name = devname;
169         action->dev_id = dev_id;
170         action->next = NULL;
171
172         retval = setup_irq(irq, action);
173         if (retval)
174                 kfree(action);
175
176         return 0;
177 }
178
179 EXPORT_SYMBOL(request_irq);
180
181 void free_irq(unsigned int irq, void *dev_id)
182 {
183         irq_desc_t *desc = get_irq_desc(irq);
184         struct irqaction **p;
185         unsigned long flags;
186
187         spin_lock_irqsave(&desc->lock,flags);
188         p = &desc->action;
189         for (;;) {
190                 struct irqaction * action = *p;
191                 if (action) {
192                         struct irqaction **pp = p;
193                         p = &action->next;
194                         if (action->dev_id != dev_id)
195                                 continue;
196
197                         /* Found it - now remove it from the list of entries */
198                         *pp = action->next;
199                         if (!desc->action) {
200                                 desc->status |= IRQ_DISABLED;
201                                 mask_irq(irq);
202                         }
203                         spin_unlock_irqrestore(&desc->lock,flags);
204
205                         /* Wait to make sure it's not being used on another CPU */
206                         synchronize_irq(irq);
207                         kfree(action);
208                         return;
209                 }
210                 printk("Trying to free free IRQ%d\n",irq);
211                 spin_unlock_irqrestore(&desc->lock,flags);
212                 break;
213         }
214         return;
215 }
216
217 EXPORT_SYMBOL(free_irq);
218
219 /*
220  * Generic enable/disable code: this just calls
221  * down into the PIC-specific version for the actual
222  * hardware disable after having gotten the irq
223  * controller lock. 
224  */
225  
226 /**
227  *      disable_irq_nosync - disable an irq without waiting
228  *      @irq: Interrupt to disable
229  *
230  *      Disable the selected interrupt line. Disables of an interrupt
231  *      stack. Unlike disable_irq(), this function does not ensure existing
232  *      instances of the IRQ handler have completed before returning.
233  *
234  *      This function may be called from IRQ context.
235  */
236  
237 inline void disable_irq_nosync(unsigned int irq)
238 {
239         irq_desc_t *desc = get_irq_desc(irq);
240         unsigned long flags;
241
242         spin_lock_irqsave(&desc->lock, flags);
243         if (!desc->depth++) {
244                 if (!(desc->status & IRQ_PER_CPU))
245                         desc->status |= IRQ_DISABLED;
246                 mask_irq(irq);
247         }
248         spin_unlock_irqrestore(&desc->lock, flags);
249 }
250
251 EXPORT_SYMBOL(disable_irq_nosync);
252
253 /**
254  *      disable_irq - disable an irq and wait for completion
255  *      @irq: Interrupt to disable
256  *
257  *      Disable the selected interrupt line. Disables of an interrupt
258  *      stack. That is for two disables you need two enables. This
259  *      function waits for any pending IRQ handlers for this interrupt
260  *      to complete before returning. If you use this function while
261  *      holding a resource the IRQ handler may need you will deadlock.
262  *
263  *      This function may be called - with care - from IRQ context.
264  */
265  
266 void disable_irq(unsigned int irq)
267 {
268         irq_desc_t *desc = get_irq_desc(irq);
269         disable_irq_nosync(irq);
270         if (desc->action)
271                 synchronize_irq(irq);
272 }
273
274 EXPORT_SYMBOL(disable_irq);
275
276 /**
277  *      enable_irq - enable interrupt handling on an irq
278  *      @irq: Interrupt to enable
279  *
280  *      Re-enables the processing of interrupts on this IRQ line
281  *      providing no disable_irq calls are now in effect.
282  *
283  *      This function may be called from IRQ context.
284  */
285  
286 void enable_irq(unsigned int irq)
287 {
288         irq_desc_t *desc = get_irq_desc(irq);
289         unsigned long flags;
290
291         spin_lock_irqsave(&desc->lock, flags);
292         switch (desc->depth) {
293         case 1: {
294                 unsigned int status = desc->status & ~IRQ_DISABLED;
295                 desc->status = status;
296                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
297                         desc->status = status | IRQ_REPLAY;
298                         hw_resend_irq(desc->handler,irq);
299                 }
300                 unmask_irq(irq);
301                 /* fall-through */
302         }
303         default:
304                 desc->depth--;
305                 break;
306         case 0:
307                 printk("enable_irq(%u) unbalanced from %p\n", irq,
308                        __builtin_return_address(0));
309         }
310         spin_unlock_irqrestore(&desc->lock, flags);
311 }
312
313 EXPORT_SYMBOL(enable_irq);
314
315 int show_interrupts(struct seq_file *p, void *v)
316 {
317         int i = *(loff_t *) v, j;
318         struct irqaction * action;
319         irq_desc_t *desc;
320         unsigned long flags;
321
322         if (i == 0) {
323                 seq_printf(p, "           ");
324                 for (j=0; j<NR_CPUS; j++) {
325                         if (cpu_online(j))
326                                 seq_printf(p, "CPU%d       ",j);
327                 }
328                 seq_putc(p, '\n');
329         }
330
331         if (i < NR_IRQS) {
332                 desc = get_irq_desc(i);
333                 spin_lock_irqsave(&desc->lock, flags);
334                 action = desc->action;
335                 if (!action || !action->handler)
336                         goto skip;
337                 seq_printf(p, "%3d: ", i);
338 #ifdef CONFIG_SMP
339                 for (j = 0; j < NR_CPUS; j++) {
340                         if (cpu_online(j))
341                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
342                 }
343 #else
344                 seq_printf(p, "%10u ", kstat_irqs(i));
345 #endif /* CONFIG_SMP */
346                 if (desc->handler)
347                         seq_printf(p, " %s ", desc->handler->typename );
348                 else
349                         seq_printf(p, "  None      ");
350                 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge  ");
351                 seq_printf(p, "    %s",action->name);
352                 for (action=action->next; action; action = action->next)
353                         seq_printf(p, ", %s", action->name);
354                 seq_putc(p, '\n');
355 skip:
356                 spin_unlock_irqrestore(&desc->lock, flags);
357         } else if (i == NR_IRQS)
358                 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
359         return 0;
360 }
361
362 int handle_irq_event(int irq, struct pt_regs *regs, struct irqaction *action)
363 {
364         int status = 0;
365         int retval = 0;
366
367         if (!(action->flags & SA_INTERRUPT))
368                 local_irq_enable();
369
370         do {
371                 status |= action->flags;
372                 retval |= action->handler(irq, action->dev_id, regs);
373                 action = action->next;
374         } while (action);
375         if (status & SA_SAMPLE_RANDOM)
376                 add_interrupt_randomness(irq);
377         local_irq_disable();
378         return retval;
379 }
380
381 static void __report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
382 {
383         struct irqaction *action;
384
385         if (action_ret != IRQ_HANDLED && action_ret != IRQ_NONE) {
386                 printk(KERN_ERR "irq event %d: bogus return value %x\n",
387                                 irq, action_ret);
388         } else {
389                 printk(KERN_ERR "irq %d: nobody cared!\n", irq);
390         }
391         dump_stack();
392         printk(KERN_ERR "handlers:\n");
393         action = desc->action;
394         do {
395                 printk(KERN_ERR "[<%p>]", action->handler);
396                 print_symbol(" (%s)",
397                         (unsigned long)action->handler);
398                 printk("\n");
399                 action = action->next;
400         } while (action);
401 }
402
403 static void report_bad_irq(int irq, irq_desc_t *desc, irqreturn_t action_ret)
404 {
405         static int count = 100;
406
407         if (count) {
408                 count--;
409                 __report_bad_irq(irq, desc, action_ret);
410         }
411 }
412
413 static int noirqdebug;
414
415 static int __init noirqdebug_setup(char *str)
416 {
417         noirqdebug = 1;
418         printk("IRQ lockup detection disabled\n");
419         return 1;
420 }
421
422 __setup("noirqdebug", noirqdebug_setup);
423
424 /*
425  * If 99,900 of the previous 100,000 interrupts have not been handled then
426  * assume that the IRQ is stuck in some manner.  Drop a diagnostic and try to
427  * turn the IRQ off.
428  *
429  * (The other 100-of-100,000 interrupts may have been a correctly-functioning
430  *  device sharing an IRQ with the failing one)
431  *
432  * Called under desc->lock
433  */
434 static void note_interrupt(int irq, irq_desc_t *desc, irqreturn_t action_ret)
435 {
436         if (action_ret != IRQ_HANDLED) {
437                 desc->irqs_unhandled++;
438                 if (action_ret != IRQ_NONE)
439                         report_bad_irq(irq, desc, action_ret);
440         }
441
442         desc->irq_count++;
443         if (desc->irq_count < 100000)
444                 return;
445
446         desc->irq_count = 0;
447         if (desc->irqs_unhandled > 99900) {
448                 /*
449                  * The interrupt is stuck
450                  */
451                 __report_bad_irq(irq, desc, action_ret);
452                 /*
453                  * Now kill the IRQ
454                  */
455                 printk(KERN_EMERG "Disabling IRQ #%d\n", irq);
456                 desc->status |= IRQ_DISABLED;
457                 desc->handler->disable(irq);
458         }
459         desc->irqs_unhandled = 0;
460 }
461
462 /*
463  * Eventually, this should take an array of interrupts and an array size
464  * so it can dispatch multiple interrupts.
465  */
466 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
467 {
468         int status;
469         struct irqaction *action;
470         int cpu = smp_processor_id();
471         irq_desc_t *desc = get_irq_desc(irq);
472         irqreturn_t action_ret;
473 #ifdef CONFIG_IRQSTACKS
474         struct thread_info *curtp, *irqtp;
475 #endif
476
477         kstat_cpu(cpu).irqs[irq]++;
478
479         if (desc->status & IRQ_PER_CPU) {
480                 /* no locking required for CPU-local interrupts: */
481                 ack_irq(irq);
482                 action_ret = handle_irq_event(irq, regs, desc->action);
483                 desc->handler->end(irq);
484                 return;
485         }
486
487         spin_lock(&desc->lock);
488         ack_irq(irq);   
489         /*
490            REPLAY is when Linux resends an IRQ that was dropped earlier
491            WAITING is used by probe to mark irqs that are being tested
492            */
493         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
494         status |= IRQ_PENDING; /* we _want_ to handle it */
495
496         /*
497          * If the IRQ is disabled for whatever reason, we cannot
498          * use the action we have.
499          */
500         action = NULL;
501         if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
502                 action = desc->action;
503                 if (!action || !action->handler) {
504                         ppc_spurious_interrupts++;
505                         printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
506                         /* We can't call disable_irq here, it would deadlock */
507                         if (!desc->depth)
508                                 desc->depth = 1;
509                         desc->status |= IRQ_DISABLED;
510                         /* This is not a real spurrious interrupt, we
511                          * have to eoi it, so we jump to out
512                          */
513                         mask_irq(irq);
514                         goto out;
515                 }
516                 status &= ~IRQ_PENDING; /* we commit to handling */
517                 status |= IRQ_INPROGRESS; /* we are handling it */
518         }
519         desc->status = status;
520
521         /*
522          * If there is no IRQ handler or it was disabled, exit early.
523            Since we set PENDING, if another processor is handling
524            a different instance of this same irq, the other processor
525            will take care of it.
526          */
527         if (unlikely(!action))
528                 goto out;
529
530         /*
531          * Edge triggered interrupts need to remember
532          * pending events.
533          * This applies to any hw interrupts that allow a second
534          * instance of the same irq to arrive while we are in do_IRQ
535          * or in the handler. But the code here only handles the _second_
536          * instance of the irq, not the third or fourth. So it is mostly
537          * useful for irq hardware that does not mask cleanly in an
538          * SMP environment.
539          */
540         for (;;) {
541                 spin_unlock(&desc->lock);
542
543 #ifdef CONFIG_IRQSTACKS
544                 /* Switch to the irq stack to handle this */
545                 curtp = current_thread_info();
546                 irqtp = hardirq_ctx[smp_processor_id()];
547                 if (curtp != irqtp) {
548                         irqtp->task = curtp->task;
549                         irqtp->flags = 0;
550                         action_ret = call_handle_irq_event(irq, regs, action, irqtp);
551                         irqtp->task = NULL;
552                         if (irqtp->flags)
553                                 set_bits(irqtp->flags, &curtp->flags);
554                 } else
555 #endif
556                         action_ret = handle_irq_event(irq, regs, action);
557
558                 spin_lock(&desc->lock);
559                 if (!noirqdebug)
560                         note_interrupt(irq, desc, action_ret);
561                 if (likely(!(desc->status & IRQ_PENDING)))
562                         break;
563                 desc->status &= ~IRQ_PENDING;
564         }
565 out:
566         desc->status &= ~IRQ_INPROGRESS;
567         /*
568          * The ->end() handler has to deal with interrupts which got
569          * disabled while the handler was running.
570          */
571         if (desc->handler) {
572                 if (desc->handler->end)
573                         desc->handler->end(irq);
574                 else if (desc->handler->enable)
575                         desc->handler->enable(irq);
576         }
577         spin_unlock(&desc->lock);
578 }
579
580 #ifdef CONFIG_PPC_ISERIES
581 void do_IRQ(struct pt_regs *regs)
582 {
583         struct paca_struct *lpaca;
584         struct ItLpQueue *lpq;
585
586         irq_enter();
587
588 #ifdef CONFIG_DEBUG_STACKOVERFLOW
589         /* Debugging check for stack overflow: is there less than 4KB free? */
590         {
591                 long sp;
592
593                 sp = __get_SP() & (THREAD_SIZE-1);
594
595                 if (unlikely(sp < (sizeof(struct thread_info) + 4096))) {
596                         printk("do_IRQ: stack overflow: %ld\n",
597                                 sp - sizeof(struct thread_info));
598                         dump_stack();
599                 }
600         }
601 #endif
602
603         lpaca = get_paca();
604 #ifdef CONFIG_SMP
605         if (lpaca->lppaca.xIntDword.xFields.xIpiCnt) {
606                 lpaca->lppaca.xIntDword.xFields.xIpiCnt = 0;
607                 iSeries_smp_message_recv(regs);
608         }
609 #endif /* CONFIG_SMP */
610         lpq = lpaca->lpqueue_ptr;
611         if (lpq && ItLpQueue_isLpIntPending(lpq))
612                 lpevent_count += ItLpQueue_process(lpq, regs);
613
614         irq_exit();
615
616         if (lpaca->lppaca.xIntDword.xFields.xDecrInt) {
617                 lpaca->lppaca.xIntDword.xFields.xDecrInt = 0;
618                 /* Signal a fake decrementer interrupt */
619                 timer_interrupt(regs);
620         }
621 }
622
623 #else   /* CONFIG_PPC_ISERIES */
624
625 void do_IRQ(struct pt_regs *regs)
626 {
627         int irq;
628
629         irq_enter();
630
631 #ifdef CONFIG_DEBUG_STACKOVERFLOW
632         /* Debugging check for stack overflow: is there less than 4KB free? */
633         {
634                 long sp;
635
636                 sp = __get_SP() & (THREAD_SIZE-1);
637
638                 if (unlikely(sp < (sizeof(struct thread_info) + 4096))) {
639                         printk("do_IRQ: stack overflow: %ld\n",
640                                 sp - sizeof(struct thread_info));
641                         dump_stack();
642                 }
643         }
644 #endif
645
646         irq = ppc_md.get_irq(regs);
647
648         if (irq >= 0)
649                 ppc_irq_dispatch_handler(regs, irq);
650         else
651                 /* That's not SMP safe ... but who cares ? */
652                 ppc_spurious_interrupts++;
653
654         irq_exit();
655 }
656 #endif  /* CONFIG_PPC_ISERIES */
657
658 unsigned long probe_irq_on (void)
659 {
660         return 0;
661 }
662
663 EXPORT_SYMBOL(probe_irq_on);
664
665 int probe_irq_off (unsigned long irqs)
666 {
667         return 0;
668 }
669
670 EXPORT_SYMBOL(probe_irq_off);
671
672 unsigned int probe_irq_mask(unsigned long irqs)
673 {
674         return 0;
675 }
676
677 void __init init_IRQ(void)
678 {
679         static int once = 0;
680
681         if (once)
682                 return;
683
684         once++;
685
686         ppc_md.init_IRQ();
687         irq_ctx_init();
688 }
689
690 static struct proc_dir_entry * root_irq_dir;
691 static struct proc_dir_entry * irq_dir [NR_IRQS];
692 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
693
694 /* Protected by get_irq_desc(irq)->lock. */
695 #ifdef CONFIG_IRQ_ALL_CPUS
696 cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_ALL };
697 #else  /* CONFIG_IRQ_ALL_CPUS */
698 cpumask_t irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = CPU_MASK_NONE };
699 #endif /* CONFIG_IRQ_ALL_CPUS */
700
701 static int irq_affinity_read_proc (char *page, char **start, off_t off,
702                         int count, int *eof, void *data)
703 {
704         int len = cpumask_scnprintf(page, count, irq_affinity[(long)data]);
705         if (count - len < 2)
706                 return -EINVAL;
707         len += sprintf(page + len, "\n");
708         return len;
709 }
710
711 static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
712                                         unsigned long count, void *data)
713 {
714         unsigned int irq = (long)data;
715         irq_desc_t *desc = get_irq_desc(irq);
716         int ret;
717         cpumask_t new_value, tmp;
718
719         if (!desc->handler->set_affinity)
720                 return -EIO;
721
722         ret = cpumask_parse(buffer, count, new_value);
723         if (ret != 0)
724                 return ret;
725
726         /*
727          * We check for CPU_MASK_ALL in xics to send irqs to all cpus.
728          * In some cases CPU_MASK_ALL is smaller than the cpumask (eg
729          * NR_CPUS == 32 and cpumask is a long), so we mask it here to
730          * be consistent.
731          */
732         cpus_and(new_value, new_value, CPU_MASK_ALL);
733
734         /*
735          * Grab lock here so cpu_online_map can't change, and also
736          * protect irq_affinity[].
737          */
738         spin_lock(&desc->lock);
739
740         /*
741          * Do not allow disabling IRQs completely - it's a too easy
742          * way to make the system unusable accidentally :-) At least
743          * one online CPU still has to be targeted.
744          */
745         cpus_and(tmp, new_value, cpu_online_map);
746         if (cpus_empty(tmp)) {
747                 ret = -EINVAL;
748                 goto out;
749         }
750
751         irq_affinity[irq] = new_value;
752         desc->handler->set_affinity(irq, new_value);
753         ret = count;
754
755 out:
756         spin_unlock(&desc->lock);
757         return ret;
758 }
759
760 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
761                         int count, int *eof, void *data)
762 {
763         int len = cpumask_scnprintf(page, count, *(cpumask_t *)data);
764         if (count - len < 2)
765                 return -EINVAL;
766         len += sprintf(page + len, "\n");
767         return len;
768 }
769
770 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,
771                                         unsigned long count, void *data)
772 {
773         cpumask_t *mask = (cpumask_t *)data;
774         unsigned long full_count = count, err;
775         cpumask_t new_value;
776
777         err = cpumask_parse(buffer, count, new_value);
778         if (err)
779                 return err;
780
781         *mask = new_value;
782
783 #ifdef CONFIG_PPC_ISERIES
784         {
785                 unsigned i;
786                 for (i=0; i<NR_CPUS; ++i) {
787                         if ( paca[i].prof_buffer && cpu_isset(i, new_value) )
788                                 paca[i].prof_enabled = 1;
789                         else
790                                 paca[i].prof_enabled = 0;
791                 }
792         }
793 #endif
794
795         return full_count;
796 }
797
798 #define MAX_NAMELEN 10
799
800 static void register_irq_proc (unsigned int irq)
801 {
802         struct proc_dir_entry *entry;
803         char name [MAX_NAMELEN];
804
805         if (!root_irq_dir || (irq_desc[irq].handler == NULL) || irq_dir[irq])
806                 return;
807
808         memset(name, 0, MAX_NAMELEN);
809         sprintf(name, "%d", irq);
810
811         /* create /proc/irq/1234 */
812         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
813
814         /* create /proc/irq/1234/smp_affinity */
815         entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
816
817         if (entry) {
818                 entry->nlink = 1;
819                 entry->data = (void *)(long)irq;
820                 entry->read_proc = irq_affinity_read_proc;
821                 entry->write_proc = irq_affinity_write_proc;
822         }
823
824         smp_affinity_entry[irq] = entry;
825 }
826
827 unsigned long prof_cpu_mask = -1;
828
829 void init_irq_proc (void)
830 {
831         struct proc_dir_entry *entry;
832         int i;
833
834         /* create /proc/irq */
835         root_irq_dir = proc_mkdir("irq", NULL);
836
837         /* create /proc/irq/prof_cpu_mask */
838         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
839
840         if (!entry)
841                 return;
842
843         entry->nlink = 1;
844         entry->data = (void *)&prof_cpu_mask;
845         entry->read_proc = prof_cpu_mask_read_proc;
846         entry->write_proc = prof_cpu_mask_write_proc;
847
848         /*
849          * Create entries for all existing IRQs.
850          */
851         for_each_irq(i) {
852                 if (get_irq_desc(i)->handler == NULL)
853                         continue;
854                 register_irq_proc(i);
855         }
856 }
857
858 irqreturn_t no_action(int irq, void *dev, struct pt_regs *regs)
859 {
860         return IRQ_NONE;
861 }
862
863 #ifndef CONFIG_PPC_ISERIES
864 /*
865  * Virtual IRQ mapping code, used on systems with XICS interrupt controllers.
866  */
867
868 #define UNDEFINED_IRQ 0xffffffff
869 unsigned int virt_irq_to_real_map[NR_IRQS];
870
871 /*
872  * Don't use virtual irqs 0, 1, 2 for devices.
873  * The pcnet32 driver considers interrupt numbers < 2 to be invalid,
874  * and 2 is the XICS IPI interrupt.
875  * We limit virtual irqs to 17 less than NR_IRQS so that when we
876  * offset them by 16 (to reserve the first 16 for ISA interrupts)
877  * we don't end up with an interrupt number >= NR_IRQS.
878  */
879 #define MIN_VIRT_IRQ    3
880 #define MAX_VIRT_IRQ    (NR_IRQS - NUM_ISA_INTERRUPTS - 1)
881 #define NR_VIRT_IRQS    (MAX_VIRT_IRQ - MIN_VIRT_IRQ + 1)
882
883 void
884 virt_irq_init(void)
885 {
886         int i;
887         for (i = 0; i < NR_IRQS; i++)
888                 virt_irq_to_real_map[i] = UNDEFINED_IRQ;
889 }
890
891 /* Create a mapping for a real_irq if it doesn't already exist.
892  * Return the virtual irq as a convenience.
893  */
894 int virt_irq_create_mapping(unsigned int real_irq)
895 {
896         unsigned int virq, first_virq;
897         static int warned;
898
899         if (naca->interrupt_controller == IC_OPEN_PIC)
900                 return real_irq;        /* no mapping for openpic (for now) */
901
902         /* don't map interrupts < MIN_VIRT_IRQ */
903         if (real_irq < MIN_VIRT_IRQ) {
904                 virt_irq_to_real_map[real_irq] = real_irq;
905                 return real_irq;
906         }
907
908         /* map to a number between MIN_VIRT_IRQ and MAX_VIRT_IRQ */
909         virq = real_irq;
910         if (virq > MAX_VIRT_IRQ)
911                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
912
913         /* search for this number or a free slot */
914         first_virq = virq;
915         while (virt_irq_to_real_map[virq] != UNDEFINED_IRQ) {
916                 if (virt_irq_to_real_map[virq] == real_irq)
917                         return virq;
918                 if (++virq > MAX_VIRT_IRQ)
919                         virq = MIN_VIRT_IRQ;
920                 if (virq == first_virq)
921                         goto nospace;   /* oops, no free slots */
922         }
923
924         virt_irq_to_real_map[virq] = real_irq;
925         return virq;
926
927  nospace:
928         if (!warned) {
929                 printk(KERN_CRIT "Interrupt table is full\n");
930                 printk(KERN_CRIT "Increase NR_IRQS (currently %d) "
931                        "in your kernel sources and rebuild.\n", NR_IRQS);
932                 warned = 1;
933         }
934         return NO_IRQ;
935 }
936
937 /*
938  * In most cases will get a hit on the very first slot checked in the
939  * virt_irq_to_real_map.  Only when there are a large number of
940  * IRQs will this be expensive.
941  */
942 unsigned int real_irq_to_virt_slowpath(unsigned int real_irq)
943 {
944         unsigned int virq;
945         unsigned int first_virq;
946
947         virq = real_irq;
948
949         if (virq > MAX_VIRT_IRQ)
950                 virq = (virq % NR_VIRT_IRQS) + MIN_VIRT_IRQ;
951
952         first_virq = virq;
953
954         do {
955                 if (virt_irq_to_real_map[virq] == real_irq)
956                         return virq;
957
958                 virq++;
959
960                 if (virq >= MAX_VIRT_IRQ)
961                         virq = 0;
962
963         } while (first_virq != virq);
964
965         return NO_IRQ;
966
967 }
968
969 #endif /* CONFIG_PPC_ISERIES */
970
971 #ifdef CONFIG_IRQSTACKS
972 struct thread_info *softirq_ctx[NR_CPUS];
973 struct thread_info *hardirq_ctx[NR_CPUS];
974
975 void irq_ctx_init(void)
976 {
977         struct thread_info *tp;
978         int i;
979
980         for (i = 0; i < NR_CPUS; i++) {
981                 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
982                 tp = softirq_ctx[i];
983                 tp->cpu = i;
984                 tp->preempt_count = SOFTIRQ_OFFSET;
985
986                 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
987                 tp = hardirq_ctx[i];
988                 tp->cpu = i;
989                 tp->preempt_count = HARDIRQ_OFFSET;
990         }
991 }
992
993 void do_softirq(void)
994 {
995         unsigned long flags;
996         struct thread_info *curtp, *irqtp;
997
998         if (in_interrupt())
999                 return;
1000
1001         local_irq_save(flags);
1002
1003         if (local_softirq_pending()) {
1004                 curtp = current_thread_info();
1005                 irqtp = softirq_ctx[smp_processor_id()];
1006                 irqtp->task = curtp->task;
1007                 call_do_softirq(irqtp);
1008                 irqtp->task = NULL;
1009         }
1010
1011         local_irq_restore(flags);
1012 }
1013 EXPORT_SYMBOL(do_softirq);
1014
1015 #endif /* CONFIG_IRQSTACKS */
1016