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